ndisc.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Neighbour Discovery for IPv6
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. * Mike Shaver <shaver@ingenia.com>
  9. */
  10. /*
  11. * Changes:
  12. *
  13. * Alexey I. Froloff : RFC6106 (DNSSL) support
  14. * Pierre Ynard : export userland ND options
  15. * through netlink (RDNSS support)
  16. * Lars Fenneberg : fixed MTU setting on receipt
  17. * of an RA.
  18. * Janos Farkas : kmalloc failure checks
  19. * Alexey Kuznetsov : state machine reworked
  20. * and moved to net/core.
  21. * Pekka Savola : RFC2461 validation
  22. * YOSHIFUJI Hideaki @USAGI : Verify ND options properly
  23. */
  24. #define pr_fmt(fmt) "ICMPv6: " fmt
  25. #include <linux/module.h>
  26. #include <linux/errno.h>
  27. #include <linux/types.h>
  28. #include <linux/socket.h>
  29. #include <linux/sockios.h>
  30. #include <linux/sched.h>
  31. #include <linux/net.h>
  32. #include <linux/in6.h>
  33. #include <linux/route.h>
  34. #include <linux/init.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/slab.h>
  37. #ifdef CONFIG_SYSCTL
  38. #include <linux/sysctl.h>
  39. #endif
  40. #include <linux/if_addr.h>
  41. #include <linux/if_ether.h>
  42. #include <linux/if_arp.h>
  43. #include <linux/ipv6.h>
  44. #include <linux/icmpv6.h>
  45. #include <linux/jhash.h>
  46. #include <net/sock.h>
  47. #include <net/snmp.h>
  48. #include <net/ipv6.h>
  49. #include <net/protocol.h>
  50. #include <net/ndisc.h>
  51. #include <net/ip6_route.h>
  52. #include <net/addrconf.h>
  53. #include <net/icmp.h>
  54. #include <net/netlink.h>
  55. #include <linux/rtnetlink.h>
  56. #include <net/flow.h>
  57. #include <net/ip6_checksum.h>
  58. #include <net/inet_common.h>
  59. #include <linux/proc_fs.h>
  60. #include <linux/netfilter.h>
  61. #include <linux/netfilter_ipv6.h>
  62. static u32 ndisc_hash(const void *pkey,
  63. const struct net_device *dev,
  64. __u32 *hash_rnd);
  65. static bool ndisc_key_eq(const struct neighbour *neigh, const void *pkey);
  66. static bool ndisc_allow_add(const struct net_device *dev,
  67. struct netlink_ext_ack *extack);
  68. static int ndisc_constructor(struct neighbour *neigh);
  69. static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
  70. static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
  71. static int pndisc_constructor(struct pneigh_entry *n);
  72. static void pndisc_destructor(struct pneigh_entry *n);
  73. static void pndisc_redo(struct sk_buff *skb);
  74. static int ndisc_is_multicast(const void *pkey);
  75. static const struct neigh_ops ndisc_generic_ops = {
  76. .family = AF_INET6,
  77. .solicit = ndisc_solicit,
  78. .error_report = ndisc_error_report,
  79. .output = neigh_resolve_output,
  80. .connected_output = neigh_connected_output,
  81. };
  82. static const struct neigh_ops ndisc_hh_ops = {
  83. .family = AF_INET6,
  84. .solicit = ndisc_solicit,
  85. .error_report = ndisc_error_report,
  86. .output = neigh_resolve_output,
  87. .connected_output = neigh_resolve_output,
  88. };
  89. static const struct neigh_ops ndisc_direct_ops = {
  90. .family = AF_INET6,
  91. .output = neigh_direct_output,
  92. .connected_output = neigh_direct_output,
  93. };
  94. struct neigh_table nd_tbl = {
  95. .family = AF_INET6,
  96. .key_len = sizeof(struct in6_addr),
  97. .protocol = cpu_to_be16(ETH_P_IPV6),
  98. .hash = ndisc_hash,
  99. .key_eq = ndisc_key_eq,
  100. .constructor = ndisc_constructor,
  101. .pconstructor = pndisc_constructor,
  102. .pdestructor = pndisc_destructor,
  103. .proxy_redo = pndisc_redo,
  104. .is_multicast = ndisc_is_multicast,
  105. .allow_add = ndisc_allow_add,
  106. .id = "ndisc_cache",
  107. .parms = {
  108. .tbl = &nd_tbl,
  109. .reachable_time = ND_REACHABLE_TIME,
  110. .data = {
  111. [NEIGH_VAR_MCAST_PROBES] = 3,
  112. [NEIGH_VAR_UCAST_PROBES] = 3,
  113. [NEIGH_VAR_RETRANS_TIME] = ND_RETRANS_TIMER,
  114. [NEIGH_VAR_BASE_REACHABLE_TIME] = ND_REACHABLE_TIME,
  115. [NEIGH_VAR_DELAY_PROBE_TIME] = 5 * HZ,
  116. [NEIGH_VAR_GC_STALETIME] = 60 * HZ,
  117. [NEIGH_VAR_QUEUE_LEN_BYTES] = SK_WMEM_MAX,
  118. [NEIGH_VAR_PROXY_QLEN] = 64,
  119. [NEIGH_VAR_ANYCAST_DELAY] = 1 * HZ,
  120. [NEIGH_VAR_PROXY_DELAY] = (8 * HZ) / 10,
  121. },
  122. },
  123. .gc_interval = 30 * HZ,
  124. .gc_thresh1 = 128,
  125. .gc_thresh2 = 512,
  126. .gc_thresh3 = 1024,
  127. };
  128. EXPORT_SYMBOL_GPL(nd_tbl);
  129. void __ndisc_fill_addr_option(struct sk_buff *skb, int type, void *data,
  130. int data_len, int pad)
  131. {
  132. int space = __ndisc_opt_addr_space(data_len, pad);
  133. u8 *opt = skb_put(skb, space);
  134. opt[0] = type;
  135. opt[1] = space>>3;
  136. memset(opt + 2, 0, pad);
  137. opt += pad;
  138. space -= pad;
  139. memcpy(opt+2, data, data_len);
  140. data_len += 2;
  141. opt += data_len;
  142. space -= data_len;
  143. if (space > 0)
  144. memset(opt, 0, space);
  145. }
  146. EXPORT_SYMBOL_GPL(__ndisc_fill_addr_option);
  147. static inline void ndisc_fill_addr_option(struct sk_buff *skb, int type,
  148. void *data, u8 icmp6_type)
  149. {
  150. __ndisc_fill_addr_option(skb, type, data, skb->dev->addr_len,
  151. ndisc_addr_option_pad(skb->dev->type));
  152. ndisc_ops_fill_addr_option(skb->dev, skb, icmp6_type);
  153. }
  154. static inline void ndisc_fill_redirect_addr_option(struct sk_buff *skb,
  155. void *ha,
  156. const u8 *ops_data)
  157. {
  158. ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR, ha, NDISC_REDIRECT);
  159. ndisc_ops_fill_redirect_addr_option(skb->dev, skb, ops_data);
  160. }
  161. static struct nd_opt_hdr *ndisc_next_option(struct nd_opt_hdr *cur,
  162. struct nd_opt_hdr *end)
  163. {
  164. int type;
  165. if (!cur || !end || cur >= end)
  166. return NULL;
  167. type = cur->nd_opt_type;
  168. do {
  169. cur = ((void *)cur) + (cur->nd_opt_len << 3);
  170. } while (cur < end && cur->nd_opt_type != type);
  171. return cur <= end && cur->nd_opt_type == type ? cur : NULL;
  172. }
  173. static inline int ndisc_is_useropt(const struct net_device *dev,
  174. struct nd_opt_hdr *opt)
  175. {
  176. return opt->nd_opt_type == ND_OPT_RDNSS ||
  177. opt->nd_opt_type == ND_OPT_DNSSL ||
  178. opt->nd_opt_type == ND_OPT_CAPTIVE_PORTAL ||
  179. opt->nd_opt_type == ND_OPT_PREF64 ||
  180. ndisc_ops_is_useropt(dev, opt->nd_opt_type);
  181. }
  182. static struct nd_opt_hdr *ndisc_next_useropt(const struct net_device *dev,
  183. struct nd_opt_hdr *cur,
  184. struct nd_opt_hdr *end)
  185. {
  186. if (!cur || !end || cur >= end)
  187. return NULL;
  188. do {
  189. cur = ((void *)cur) + (cur->nd_opt_len << 3);
  190. } while (cur < end && !ndisc_is_useropt(dev, cur));
  191. return cur <= end && ndisc_is_useropt(dev, cur) ? cur : NULL;
  192. }
  193. struct ndisc_options *ndisc_parse_options(const struct net_device *dev,
  194. u8 *opt, int opt_len,
  195. struct ndisc_options *ndopts)
  196. {
  197. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)opt;
  198. if (!nd_opt || opt_len < 0 || !ndopts)
  199. return NULL;
  200. memset(ndopts, 0, sizeof(*ndopts));
  201. while (opt_len) {
  202. int l;
  203. if (opt_len < sizeof(struct nd_opt_hdr))
  204. return NULL;
  205. l = nd_opt->nd_opt_len << 3;
  206. if (opt_len < l || l == 0)
  207. return NULL;
  208. if (ndisc_ops_parse_options(dev, nd_opt, ndopts))
  209. goto next_opt;
  210. switch (nd_opt->nd_opt_type) {
  211. case ND_OPT_SOURCE_LL_ADDR:
  212. case ND_OPT_TARGET_LL_ADDR:
  213. case ND_OPT_MTU:
  214. case ND_OPT_NONCE:
  215. case ND_OPT_REDIRECT_HDR:
  216. if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
  217. ND_PRINTK(2, warn,
  218. "%s: duplicated ND6 option found: type=%d\n",
  219. __func__, nd_opt->nd_opt_type);
  220. } else {
  221. ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
  222. }
  223. break;
  224. case ND_OPT_PREFIX_INFO:
  225. ndopts->nd_opts_pi_end = nd_opt;
  226. if (!ndopts->nd_opt_array[nd_opt->nd_opt_type])
  227. ndopts->nd_opt_array[nd_opt->nd_opt_type] = nd_opt;
  228. break;
  229. #ifdef CONFIG_IPV6_ROUTE_INFO
  230. case ND_OPT_ROUTE_INFO:
  231. ndopts->nd_opts_ri_end = nd_opt;
  232. if (!ndopts->nd_opts_ri)
  233. ndopts->nd_opts_ri = nd_opt;
  234. break;
  235. #endif
  236. default:
  237. if (ndisc_is_useropt(dev, nd_opt)) {
  238. ndopts->nd_useropts_end = nd_opt;
  239. if (!ndopts->nd_useropts)
  240. ndopts->nd_useropts = nd_opt;
  241. } else {
  242. /*
  243. * Unknown options must be silently ignored,
  244. * to accommodate future extension to the
  245. * protocol.
  246. */
  247. ND_PRINTK(2, notice,
  248. "%s: ignored unsupported option; type=%d, len=%d\n",
  249. __func__,
  250. nd_opt->nd_opt_type,
  251. nd_opt->nd_opt_len);
  252. }
  253. }
  254. next_opt:
  255. opt_len -= l;
  256. nd_opt = ((void *)nd_opt) + l;
  257. }
  258. return ndopts;
  259. }
  260. int ndisc_mc_map(const struct in6_addr *addr, char *buf, struct net_device *dev, int dir)
  261. {
  262. switch (dev->type) {
  263. case ARPHRD_ETHER:
  264. case ARPHRD_IEEE802: /* Not sure. Check it later. --ANK */
  265. case ARPHRD_FDDI:
  266. ipv6_eth_mc_map(addr, buf);
  267. return 0;
  268. case ARPHRD_ARCNET:
  269. ipv6_arcnet_mc_map(addr, buf);
  270. return 0;
  271. case ARPHRD_INFINIBAND:
  272. ipv6_ib_mc_map(addr, dev->broadcast, buf);
  273. return 0;
  274. case ARPHRD_IPGRE:
  275. return ipv6_ipgre_mc_map(addr, dev->broadcast, buf);
  276. default:
  277. if (dir) {
  278. memcpy(buf, dev->broadcast, dev->addr_len);
  279. return 0;
  280. }
  281. }
  282. return -EINVAL;
  283. }
  284. EXPORT_SYMBOL(ndisc_mc_map);
  285. static u32 ndisc_hash(const void *pkey,
  286. const struct net_device *dev,
  287. __u32 *hash_rnd)
  288. {
  289. return ndisc_hashfn(pkey, dev, hash_rnd);
  290. }
  291. static bool ndisc_key_eq(const struct neighbour *n, const void *pkey)
  292. {
  293. return neigh_key_eq128(n, pkey);
  294. }
  295. static int ndisc_constructor(struct neighbour *neigh)
  296. {
  297. struct in6_addr *addr = (struct in6_addr *)&neigh->primary_key;
  298. struct net_device *dev = neigh->dev;
  299. struct inet6_dev *in6_dev;
  300. struct neigh_parms *parms;
  301. bool is_multicast = ipv6_addr_is_multicast(addr);
  302. in6_dev = in6_dev_get(dev);
  303. if (!in6_dev) {
  304. return -EINVAL;
  305. }
  306. parms = in6_dev->nd_parms;
  307. __neigh_parms_put(neigh->parms);
  308. neigh->parms = neigh_parms_clone(parms);
  309. neigh->type = is_multicast ? RTN_MULTICAST : RTN_UNICAST;
  310. if (!dev->header_ops) {
  311. neigh->nud_state = NUD_NOARP;
  312. neigh->ops = &ndisc_direct_ops;
  313. neigh->output = neigh_direct_output;
  314. } else {
  315. if (is_multicast) {
  316. neigh->nud_state = NUD_NOARP;
  317. ndisc_mc_map(addr, neigh->ha, dev, 1);
  318. } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
  319. neigh->nud_state = NUD_NOARP;
  320. memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
  321. if (dev->flags&IFF_LOOPBACK)
  322. neigh->type = RTN_LOCAL;
  323. } else if (dev->flags&IFF_POINTOPOINT) {
  324. neigh->nud_state = NUD_NOARP;
  325. memcpy(neigh->ha, dev->broadcast, dev->addr_len);
  326. }
  327. if (dev->header_ops->cache)
  328. neigh->ops = &ndisc_hh_ops;
  329. else
  330. neigh->ops = &ndisc_generic_ops;
  331. if (neigh->nud_state&NUD_VALID)
  332. neigh->output = neigh->ops->connected_output;
  333. else
  334. neigh->output = neigh->ops->output;
  335. }
  336. in6_dev_put(in6_dev);
  337. return 0;
  338. }
  339. static int pndisc_constructor(struct pneigh_entry *n)
  340. {
  341. struct in6_addr *addr = (struct in6_addr *)&n->key;
  342. struct in6_addr maddr;
  343. struct net_device *dev = n->dev;
  344. if (!dev || !__in6_dev_get(dev))
  345. return -EINVAL;
  346. addrconf_addr_solict_mult(addr, &maddr);
  347. ipv6_dev_mc_inc(dev, &maddr);
  348. return 0;
  349. }
  350. static void pndisc_destructor(struct pneigh_entry *n)
  351. {
  352. struct in6_addr *addr = (struct in6_addr *)&n->key;
  353. struct in6_addr maddr;
  354. struct net_device *dev = n->dev;
  355. if (!dev || !__in6_dev_get(dev))
  356. return;
  357. addrconf_addr_solict_mult(addr, &maddr);
  358. ipv6_dev_mc_dec(dev, &maddr);
  359. }
  360. /* called with rtnl held */
  361. static bool ndisc_allow_add(const struct net_device *dev,
  362. struct netlink_ext_ack *extack)
  363. {
  364. struct inet6_dev *idev = __in6_dev_get(dev);
  365. if (!idev || idev->cnf.disable_ipv6) {
  366. NL_SET_ERR_MSG(extack, "IPv6 is disabled on this device");
  367. return false;
  368. }
  369. return true;
  370. }
  371. static struct sk_buff *ndisc_alloc_skb(struct net_device *dev,
  372. int len)
  373. {
  374. int hlen = LL_RESERVED_SPACE(dev);
  375. int tlen = dev->needed_tailroom;
  376. struct sock *sk = dev_net(dev)->ipv6.ndisc_sk;
  377. struct sk_buff *skb;
  378. skb = alloc_skb(hlen + sizeof(struct ipv6hdr) + len + tlen, GFP_ATOMIC);
  379. if (!skb) {
  380. ND_PRINTK(0, err, "ndisc: %s failed to allocate an skb\n",
  381. __func__);
  382. return NULL;
  383. }
  384. skb->protocol = htons(ETH_P_IPV6);
  385. skb->dev = dev;
  386. skb_reserve(skb, hlen + sizeof(struct ipv6hdr));
  387. skb_reset_transport_header(skb);
  388. /* Manually assign socket ownership as we avoid calling
  389. * sock_alloc_send_pskb() to bypass wmem buffer limits
  390. */
  391. skb_set_owner_w(skb, sk);
  392. return skb;
  393. }
  394. static void ip6_nd_hdr(struct sk_buff *skb,
  395. const struct in6_addr *saddr,
  396. const struct in6_addr *daddr,
  397. int hop_limit, int len)
  398. {
  399. struct ipv6hdr *hdr;
  400. struct inet6_dev *idev;
  401. unsigned tclass;
  402. rcu_read_lock();
  403. idev = __in6_dev_get(skb->dev);
  404. tclass = idev ? idev->cnf.ndisc_tclass : 0;
  405. rcu_read_unlock();
  406. skb_push(skb, sizeof(*hdr));
  407. skb_reset_network_header(skb);
  408. hdr = ipv6_hdr(skb);
  409. ip6_flow_hdr(hdr, tclass, 0);
  410. hdr->payload_len = htons(len);
  411. hdr->nexthdr = IPPROTO_ICMPV6;
  412. hdr->hop_limit = hop_limit;
  413. hdr->saddr = *saddr;
  414. hdr->daddr = *daddr;
  415. }
  416. static void ndisc_send_skb(struct sk_buff *skb,
  417. const struct in6_addr *daddr,
  418. const struct in6_addr *saddr)
  419. {
  420. struct dst_entry *dst = skb_dst(skb);
  421. struct net *net = dev_net(skb->dev);
  422. struct sock *sk = net->ipv6.ndisc_sk;
  423. struct inet6_dev *idev;
  424. int err;
  425. struct icmp6hdr *icmp6h = icmp6_hdr(skb);
  426. u8 type;
  427. type = icmp6h->icmp6_type;
  428. if (!dst) {
  429. struct flowi6 fl6;
  430. int oif = skb->dev->ifindex;
  431. icmpv6_flow_init(sk, &fl6, type, saddr, daddr, oif);
  432. dst = icmp6_dst_alloc(skb->dev, &fl6);
  433. if (IS_ERR(dst)) {
  434. kfree_skb(skb);
  435. return;
  436. }
  437. skb_dst_set(skb, dst);
  438. }
  439. icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, skb->len,
  440. IPPROTO_ICMPV6,
  441. csum_partial(icmp6h,
  442. skb->len, 0));
  443. ip6_nd_hdr(skb, saddr, daddr, inet6_sk(sk)->hop_limit, skb->len);
  444. rcu_read_lock();
  445. idev = __in6_dev_get(dst->dev);
  446. IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
  447. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
  448. net, sk, skb, NULL, dst->dev,
  449. dst_output);
  450. if (!err) {
  451. ICMP6MSGOUT_INC_STATS(net, idev, type);
  452. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
  453. }
  454. rcu_read_unlock();
  455. }
  456. void ndisc_send_na(struct net_device *dev, const struct in6_addr *daddr,
  457. const struct in6_addr *solicited_addr,
  458. bool router, bool solicited, bool override, bool inc_opt)
  459. {
  460. struct sk_buff *skb;
  461. struct in6_addr tmpaddr;
  462. struct inet6_ifaddr *ifp;
  463. const struct in6_addr *src_addr;
  464. struct nd_msg *msg;
  465. int optlen = 0;
  466. /* for anycast or proxy, solicited_addr != src_addr */
  467. ifp = ipv6_get_ifaddr(dev_net(dev), solicited_addr, dev, 1);
  468. if (ifp) {
  469. src_addr = solicited_addr;
  470. if (ifp->flags & IFA_F_OPTIMISTIC)
  471. override = false;
  472. inc_opt |= ifp->idev->cnf.force_tllao;
  473. in6_ifa_put(ifp);
  474. } else {
  475. if (ipv6_dev_get_saddr(dev_net(dev), dev, daddr,
  476. inet6_sk(dev_net(dev)->ipv6.ndisc_sk)->srcprefs,
  477. &tmpaddr))
  478. return;
  479. src_addr = &tmpaddr;
  480. }
  481. if (!dev->addr_len)
  482. inc_opt = false;
  483. if (inc_opt)
  484. optlen += ndisc_opt_addr_space(dev,
  485. NDISC_NEIGHBOUR_ADVERTISEMENT);
  486. skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
  487. if (!skb)
  488. return;
  489. msg = skb_put(skb, sizeof(*msg));
  490. *msg = (struct nd_msg) {
  491. .icmph = {
  492. .icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT,
  493. .icmp6_router = router,
  494. .icmp6_solicited = solicited,
  495. .icmp6_override = override,
  496. },
  497. .target = *solicited_addr,
  498. };
  499. if (inc_opt)
  500. ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
  501. dev->dev_addr,
  502. NDISC_NEIGHBOUR_ADVERTISEMENT);
  503. ndisc_send_skb(skb, daddr, src_addr);
  504. }
  505. static void ndisc_send_unsol_na(struct net_device *dev)
  506. {
  507. struct inet6_dev *idev;
  508. struct inet6_ifaddr *ifa;
  509. idev = in6_dev_get(dev);
  510. if (!idev)
  511. return;
  512. read_lock_bh(&idev->lock);
  513. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  514. /* skip tentative addresses until dad completes */
  515. if (ifa->flags & IFA_F_TENTATIVE &&
  516. !(ifa->flags & IFA_F_OPTIMISTIC))
  517. continue;
  518. ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifa->addr,
  519. /*router=*/ !!idev->cnf.forwarding,
  520. /*solicited=*/ false, /*override=*/ true,
  521. /*inc_opt=*/ true);
  522. }
  523. read_unlock_bh(&idev->lock);
  524. in6_dev_put(idev);
  525. }
  526. void ndisc_send_ns(struct net_device *dev, const struct in6_addr *solicit,
  527. const struct in6_addr *daddr, const struct in6_addr *saddr,
  528. u64 nonce)
  529. {
  530. struct sk_buff *skb;
  531. struct in6_addr addr_buf;
  532. int inc_opt = dev->addr_len;
  533. int optlen = 0;
  534. struct nd_msg *msg;
  535. if (!saddr) {
  536. if (ipv6_get_lladdr(dev, &addr_buf,
  537. (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
  538. return;
  539. saddr = &addr_buf;
  540. }
  541. if (ipv6_addr_any(saddr))
  542. inc_opt = false;
  543. if (inc_opt)
  544. optlen += ndisc_opt_addr_space(dev,
  545. NDISC_NEIGHBOUR_SOLICITATION);
  546. if (nonce != 0)
  547. optlen += 8;
  548. skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
  549. if (!skb)
  550. return;
  551. msg = skb_put(skb, sizeof(*msg));
  552. *msg = (struct nd_msg) {
  553. .icmph = {
  554. .icmp6_type = NDISC_NEIGHBOUR_SOLICITATION,
  555. },
  556. .target = *solicit,
  557. };
  558. if (inc_opt)
  559. ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
  560. dev->dev_addr,
  561. NDISC_NEIGHBOUR_SOLICITATION);
  562. if (nonce != 0) {
  563. u8 *opt = skb_put(skb, 8);
  564. opt[0] = ND_OPT_NONCE;
  565. opt[1] = 8 >> 3;
  566. memcpy(opt + 2, &nonce, 6);
  567. }
  568. ndisc_send_skb(skb, daddr, saddr);
  569. }
  570. void ndisc_send_rs(struct net_device *dev, const struct in6_addr *saddr,
  571. const struct in6_addr *daddr)
  572. {
  573. struct sk_buff *skb;
  574. struct rs_msg *msg;
  575. int send_sllao = dev->addr_len;
  576. int optlen = 0;
  577. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  578. /*
  579. * According to section 2.2 of RFC 4429, we must not
  580. * send router solicitations with a sllao from
  581. * optimistic addresses, but we may send the solicitation
  582. * if we don't include the sllao. So here we check
  583. * if our address is optimistic, and if so, we
  584. * suppress the inclusion of the sllao.
  585. */
  586. if (send_sllao) {
  587. struct inet6_ifaddr *ifp = ipv6_get_ifaddr(dev_net(dev), saddr,
  588. dev, 1);
  589. if (ifp) {
  590. if (ifp->flags & IFA_F_OPTIMISTIC) {
  591. send_sllao = 0;
  592. }
  593. in6_ifa_put(ifp);
  594. } else {
  595. send_sllao = 0;
  596. }
  597. }
  598. #endif
  599. if (send_sllao)
  600. optlen += ndisc_opt_addr_space(dev, NDISC_ROUTER_SOLICITATION);
  601. skb = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
  602. if (!skb)
  603. return;
  604. msg = skb_put(skb, sizeof(*msg));
  605. *msg = (struct rs_msg) {
  606. .icmph = {
  607. .icmp6_type = NDISC_ROUTER_SOLICITATION,
  608. },
  609. };
  610. if (send_sllao)
  611. ndisc_fill_addr_option(skb, ND_OPT_SOURCE_LL_ADDR,
  612. dev->dev_addr,
  613. NDISC_ROUTER_SOLICITATION);
  614. ndisc_send_skb(skb, daddr, saddr);
  615. }
  616. static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb)
  617. {
  618. /*
  619. * "The sender MUST return an ICMP
  620. * destination unreachable"
  621. */
  622. dst_link_failure(skb);
  623. kfree_skb(skb);
  624. }
  625. /* Called with locked neigh: either read or both */
  626. static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb)
  627. {
  628. struct in6_addr *saddr = NULL;
  629. struct in6_addr mcaddr;
  630. struct net_device *dev = neigh->dev;
  631. struct in6_addr *target = (struct in6_addr *)&neigh->primary_key;
  632. int probes = atomic_read(&neigh->probes);
  633. if (skb && ipv6_chk_addr_and_flags(dev_net(dev), &ipv6_hdr(skb)->saddr,
  634. dev, false, 1,
  635. IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))
  636. saddr = &ipv6_hdr(skb)->saddr;
  637. probes -= NEIGH_VAR(neigh->parms, UCAST_PROBES);
  638. if (probes < 0) {
  639. if (!(neigh->nud_state & NUD_VALID)) {
  640. ND_PRINTK(1, dbg,
  641. "%s: trying to ucast probe in NUD_INVALID: %pI6\n",
  642. __func__, target);
  643. }
  644. ndisc_send_ns(dev, target, target, saddr, 0);
  645. } else if ((probes -= NEIGH_VAR(neigh->parms, APP_PROBES)) < 0) {
  646. neigh_app_ns(neigh);
  647. } else {
  648. addrconf_addr_solict_mult(target, &mcaddr);
  649. ndisc_send_ns(dev, target, &mcaddr, saddr, 0);
  650. }
  651. }
  652. static int pndisc_is_router(const void *pkey,
  653. struct net_device *dev)
  654. {
  655. struct pneigh_entry *n;
  656. int ret = -1;
  657. read_lock_bh(&nd_tbl.lock);
  658. n = __pneigh_lookup(&nd_tbl, dev_net(dev), pkey, dev);
  659. if (n)
  660. ret = !!(n->flags & NTF_ROUTER);
  661. read_unlock_bh(&nd_tbl.lock);
  662. return ret;
  663. }
  664. void ndisc_update(const struct net_device *dev, struct neighbour *neigh,
  665. const u8 *lladdr, u8 new, u32 flags, u8 icmp6_type,
  666. struct ndisc_options *ndopts)
  667. {
  668. neigh_update(neigh, lladdr, new, flags, 0);
  669. /* report ndisc ops about neighbour update */
  670. ndisc_ops_update(dev, neigh, flags, icmp6_type, ndopts);
  671. }
  672. static void ndisc_recv_ns(struct sk_buff *skb)
  673. {
  674. struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
  675. const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  676. const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  677. u8 *lladdr = NULL;
  678. u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
  679. offsetof(struct nd_msg, opt));
  680. struct ndisc_options ndopts;
  681. struct net_device *dev = skb->dev;
  682. struct inet6_ifaddr *ifp;
  683. struct inet6_dev *idev = NULL;
  684. struct neighbour *neigh;
  685. int dad = ipv6_addr_any(saddr);
  686. bool inc;
  687. int is_router = -1;
  688. u64 nonce = 0;
  689. if (skb->len < sizeof(struct nd_msg)) {
  690. ND_PRINTK(2, warn, "NS: packet too short\n");
  691. return;
  692. }
  693. if (ipv6_addr_is_multicast(&msg->target)) {
  694. ND_PRINTK(2, warn, "NS: multicast target address\n");
  695. return;
  696. }
  697. /*
  698. * RFC2461 7.1.1:
  699. * DAD has to be destined for solicited node multicast address.
  700. */
  701. if (dad && !ipv6_addr_is_solict_mult(daddr)) {
  702. ND_PRINTK(2, warn, "NS: bad DAD packet (wrong destination)\n");
  703. return;
  704. }
  705. if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
  706. ND_PRINTK(2, warn, "NS: invalid ND options\n");
  707. return;
  708. }
  709. if (ndopts.nd_opts_src_lladdr) {
  710. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr, dev);
  711. if (!lladdr) {
  712. ND_PRINTK(2, warn,
  713. "NS: invalid link-layer address length\n");
  714. return;
  715. }
  716. /* RFC2461 7.1.1:
  717. * If the IP source address is the unspecified address,
  718. * there MUST NOT be source link-layer address option
  719. * in the message.
  720. */
  721. if (dad) {
  722. ND_PRINTK(2, warn,
  723. "NS: bad DAD packet (link-layer address option)\n");
  724. return;
  725. }
  726. }
  727. if (ndopts.nd_opts_nonce && ndopts.nd_opts_nonce->nd_opt_len == 1)
  728. memcpy(&nonce, (u8 *)(ndopts.nd_opts_nonce + 1), 6);
  729. inc = ipv6_addr_is_multicast(daddr);
  730. ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
  731. if (ifp) {
  732. have_ifp:
  733. if (ifp->flags & (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)) {
  734. if (dad) {
  735. if (nonce != 0 && ifp->dad_nonce == nonce) {
  736. u8 *np = (u8 *)&nonce;
  737. /* Matching nonce if looped back */
  738. ND_PRINTK(2, notice,
  739. "%s: IPv6 DAD loopback for address %pI6c nonce %pM ignored\n",
  740. ifp->idev->dev->name,
  741. &ifp->addr, np);
  742. goto out;
  743. }
  744. /*
  745. * We are colliding with another node
  746. * who is doing DAD
  747. * so fail our DAD process
  748. */
  749. addrconf_dad_failure(skb, ifp);
  750. return;
  751. } else {
  752. /*
  753. * This is not a dad solicitation.
  754. * If we are an optimistic node,
  755. * we should respond.
  756. * Otherwise, we should ignore it.
  757. */
  758. if (!(ifp->flags & IFA_F_OPTIMISTIC))
  759. goto out;
  760. }
  761. }
  762. idev = ifp->idev;
  763. } else {
  764. struct net *net = dev_net(dev);
  765. /* perhaps an address on the master device */
  766. if (netif_is_l3_slave(dev)) {
  767. struct net_device *mdev;
  768. mdev = netdev_master_upper_dev_get_rcu(dev);
  769. if (mdev) {
  770. ifp = ipv6_get_ifaddr(net, &msg->target, mdev, 1);
  771. if (ifp)
  772. goto have_ifp;
  773. }
  774. }
  775. idev = in6_dev_get(dev);
  776. if (!idev) {
  777. /* XXX: count this drop? */
  778. return;
  779. }
  780. if (ipv6_chk_acast_addr(net, dev, &msg->target) ||
  781. (idev->cnf.forwarding &&
  782. (net->ipv6.devconf_all->proxy_ndp || idev->cnf.proxy_ndp) &&
  783. (is_router = pndisc_is_router(&msg->target, dev)) >= 0)) {
  784. if (!(NEIGH_CB(skb)->flags & LOCALLY_ENQUEUED) &&
  785. skb->pkt_type != PACKET_HOST &&
  786. inc &&
  787. NEIGH_VAR(idev->nd_parms, PROXY_DELAY) != 0) {
  788. /*
  789. * for anycast or proxy,
  790. * sender should delay its response
  791. * by a random time between 0 and
  792. * MAX_ANYCAST_DELAY_TIME seconds.
  793. * (RFC2461) -- yoshfuji
  794. */
  795. struct sk_buff *n = skb_clone(skb, GFP_ATOMIC);
  796. if (n)
  797. pneigh_enqueue(&nd_tbl, idev->nd_parms, n);
  798. goto out;
  799. }
  800. } else
  801. goto out;
  802. }
  803. if (is_router < 0)
  804. is_router = idev->cnf.forwarding;
  805. if (dad) {
  806. ndisc_send_na(dev, &in6addr_linklocal_allnodes, &msg->target,
  807. !!is_router, false, (ifp != NULL), true);
  808. goto out;
  809. }
  810. if (inc)
  811. NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_mcast);
  812. else
  813. NEIGH_CACHE_STAT_INC(&nd_tbl, rcv_probes_ucast);
  814. /*
  815. * update / create cache entry
  816. * for the source address
  817. */
  818. neigh = __neigh_lookup(&nd_tbl, saddr, dev,
  819. !inc || lladdr || !dev->addr_len);
  820. if (neigh)
  821. ndisc_update(dev, neigh, lladdr, NUD_STALE,
  822. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  823. NEIGH_UPDATE_F_OVERRIDE,
  824. NDISC_NEIGHBOUR_SOLICITATION, &ndopts);
  825. if (neigh || !dev->header_ops) {
  826. ndisc_send_na(dev, saddr, &msg->target, !!is_router,
  827. true, (ifp != NULL && inc), inc);
  828. if (neigh)
  829. neigh_release(neigh);
  830. }
  831. out:
  832. if (ifp)
  833. in6_ifa_put(ifp);
  834. else
  835. in6_dev_put(idev);
  836. }
  837. static void ndisc_recv_na(struct sk_buff *skb)
  838. {
  839. struct nd_msg *msg = (struct nd_msg *)skb_transport_header(skb);
  840. struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  841. const struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
  842. u8 *lladdr = NULL;
  843. u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
  844. offsetof(struct nd_msg, opt));
  845. struct ndisc_options ndopts;
  846. struct net_device *dev = skb->dev;
  847. struct inet6_dev *idev = __in6_dev_get(dev);
  848. struct inet6_ifaddr *ifp;
  849. struct neighbour *neigh;
  850. if (skb->len < sizeof(struct nd_msg)) {
  851. ND_PRINTK(2, warn, "NA: packet too short\n");
  852. return;
  853. }
  854. if (ipv6_addr_is_multicast(&msg->target)) {
  855. ND_PRINTK(2, warn, "NA: target address is multicast\n");
  856. return;
  857. }
  858. if (ipv6_addr_is_multicast(daddr) &&
  859. msg->icmph.icmp6_solicited) {
  860. ND_PRINTK(2, warn, "NA: solicited NA is multicasted\n");
  861. return;
  862. }
  863. /* For some 802.11 wireless deployments (and possibly other networks),
  864. * there will be a NA proxy and unsolicitd packets are attacks
  865. * and thus should not be accepted.
  866. */
  867. if (!msg->icmph.icmp6_solicited && idev &&
  868. idev->cnf.drop_unsolicited_na)
  869. return;
  870. if (!ndisc_parse_options(dev, msg->opt, ndoptlen, &ndopts)) {
  871. ND_PRINTK(2, warn, "NS: invalid ND option\n");
  872. return;
  873. }
  874. if (ndopts.nd_opts_tgt_lladdr) {
  875. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, dev);
  876. if (!lladdr) {
  877. ND_PRINTK(2, warn,
  878. "NA: invalid link-layer address length\n");
  879. return;
  880. }
  881. }
  882. ifp = ipv6_get_ifaddr(dev_net(dev), &msg->target, dev, 1);
  883. if (ifp) {
  884. if (skb->pkt_type != PACKET_LOOPBACK
  885. && (ifp->flags & IFA_F_TENTATIVE)) {
  886. addrconf_dad_failure(skb, ifp);
  887. return;
  888. }
  889. /* What should we make now? The advertisement
  890. is invalid, but ndisc specs say nothing
  891. about it. It could be misconfiguration, or
  892. an smart proxy agent tries to help us :-)
  893. We should not print the error if NA has been
  894. received from loopback - it is just our own
  895. unsolicited advertisement.
  896. */
  897. if (skb->pkt_type != PACKET_LOOPBACK)
  898. ND_PRINTK(1, warn,
  899. "NA: %pM advertised our address %pI6c on %s!\n",
  900. eth_hdr(skb)->h_source, &ifp->addr, ifp->idev->dev->name);
  901. in6_ifa_put(ifp);
  902. return;
  903. }
  904. neigh = neigh_lookup(&nd_tbl, &msg->target, dev);
  905. if (neigh) {
  906. u8 old_flags = neigh->flags;
  907. struct net *net = dev_net(dev);
  908. if (neigh->nud_state & NUD_FAILED)
  909. goto out;
  910. /*
  911. * Don't update the neighbor cache entry on a proxy NA from
  912. * ourselves because either the proxied node is off link or it
  913. * has already sent a NA to us.
  914. */
  915. if (lladdr && !memcmp(lladdr, dev->dev_addr, dev->addr_len) &&
  916. net->ipv6.devconf_all->forwarding && net->ipv6.devconf_all->proxy_ndp &&
  917. pneigh_lookup(&nd_tbl, net, &msg->target, dev, 0)) {
  918. /* XXX: idev->cnf.proxy_ndp */
  919. goto out;
  920. }
  921. ndisc_update(dev, neigh, lladdr,
  922. msg->icmph.icmp6_solicited ? NUD_REACHABLE : NUD_STALE,
  923. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  924. (msg->icmph.icmp6_override ? NEIGH_UPDATE_F_OVERRIDE : 0)|
  925. NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  926. (msg->icmph.icmp6_router ? NEIGH_UPDATE_F_ISROUTER : 0),
  927. NDISC_NEIGHBOUR_ADVERTISEMENT, &ndopts);
  928. if ((old_flags & ~neigh->flags) & NTF_ROUTER) {
  929. /*
  930. * Change: router to host
  931. */
  932. rt6_clean_tohost(dev_net(dev), saddr);
  933. }
  934. out:
  935. neigh_release(neigh);
  936. }
  937. }
  938. static void ndisc_recv_rs(struct sk_buff *skb)
  939. {
  940. struct rs_msg *rs_msg = (struct rs_msg *)skb_transport_header(skb);
  941. unsigned long ndoptlen = skb->len - sizeof(*rs_msg);
  942. struct neighbour *neigh;
  943. struct inet6_dev *idev;
  944. const struct in6_addr *saddr = &ipv6_hdr(skb)->saddr;
  945. struct ndisc_options ndopts;
  946. u8 *lladdr = NULL;
  947. if (skb->len < sizeof(*rs_msg))
  948. return;
  949. idev = __in6_dev_get(skb->dev);
  950. if (!idev) {
  951. ND_PRINTK(1, err, "RS: can't find in6 device\n");
  952. return;
  953. }
  954. /* Don't accept RS if we're not in router mode */
  955. if (!idev->cnf.forwarding)
  956. goto out;
  957. /*
  958. * Don't update NCE if src = ::;
  959. * this implies that the source node has no ip address assigned yet.
  960. */
  961. if (ipv6_addr_any(saddr))
  962. goto out;
  963. /* Parse ND options */
  964. if (!ndisc_parse_options(skb->dev, rs_msg->opt, ndoptlen, &ndopts)) {
  965. ND_PRINTK(2, notice, "NS: invalid ND option, ignored\n");
  966. goto out;
  967. }
  968. if (ndopts.nd_opts_src_lladdr) {
  969. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
  970. skb->dev);
  971. if (!lladdr)
  972. goto out;
  973. }
  974. neigh = __neigh_lookup(&nd_tbl, saddr, skb->dev, 1);
  975. if (neigh) {
  976. ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
  977. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  978. NEIGH_UPDATE_F_OVERRIDE|
  979. NEIGH_UPDATE_F_OVERRIDE_ISROUTER,
  980. NDISC_ROUTER_SOLICITATION, &ndopts);
  981. neigh_release(neigh);
  982. }
  983. out:
  984. return;
  985. }
  986. static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
  987. {
  988. struct icmp6hdr *icmp6h = (struct icmp6hdr *)skb_transport_header(ra);
  989. struct sk_buff *skb;
  990. struct nlmsghdr *nlh;
  991. struct nduseroptmsg *ndmsg;
  992. struct net *net = dev_net(ra->dev);
  993. int err;
  994. int base_size = NLMSG_ALIGN(sizeof(struct nduseroptmsg)
  995. + (opt->nd_opt_len << 3));
  996. size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
  997. skb = nlmsg_new(msg_size, GFP_ATOMIC);
  998. if (!skb) {
  999. err = -ENOBUFS;
  1000. goto errout;
  1001. }
  1002. nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
  1003. if (!nlh) {
  1004. goto nla_put_failure;
  1005. }
  1006. ndmsg = nlmsg_data(nlh);
  1007. ndmsg->nduseropt_family = AF_INET6;
  1008. ndmsg->nduseropt_ifindex = ra->dev->ifindex;
  1009. ndmsg->nduseropt_icmp_type = icmp6h->icmp6_type;
  1010. ndmsg->nduseropt_icmp_code = icmp6h->icmp6_code;
  1011. ndmsg->nduseropt_opts_len = opt->nd_opt_len << 3;
  1012. memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
  1013. if (nla_put_in6_addr(skb, NDUSEROPT_SRCADDR, &ipv6_hdr(ra)->saddr))
  1014. goto nla_put_failure;
  1015. nlmsg_end(skb, nlh);
  1016. rtnl_notify(skb, net, 0, RTNLGRP_ND_USEROPT, NULL, GFP_ATOMIC);
  1017. return;
  1018. nla_put_failure:
  1019. nlmsg_free(skb);
  1020. err = -EMSGSIZE;
  1021. errout:
  1022. rtnl_set_sk_err(net, RTNLGRP_ND_USEROPT, err);
  1023. }
  1024. static void ndisc_router_discovery(struct sk_buff *skb)
  1025. {
  1026. struct ra_msg *ra_msg = (struct ra_msg *)skb_transport_header(skb);
  1027. struct neighbour *neigh = NULL;
  1028. struct inet6_dev *in6_dev;
  1029. struct fib6_info *rt = NULL;
  1030. struct net *net;
  1031. int lifetime;
  1032. struct ndisc_options ndopts;
  1033. int optlen;
  1034. unsigned int pref = 0;
  1035. __u32 old_if_flags;
  1036. bool send_ifinfo_notify = false;
  1037. __u8 *opt = (__u8 *)(ra_msg + 1);
  1038. optlen = (skb_tail_pointer(skb) - skb_transport_header(skb)) -
  1039. sizeof(struct ra_msg);
  1040. ND_PRINTK(2, info,
  1041. "RA: %s, dev: %s\n",
  1042. __func__, skb->dev->name);
  1043. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
  1044. ND_PRINTK(2, warn, "RA: source address is not link-local\n");
  1045. return;
  1046. }
  1047. if (optlen < 0) {
  1048. ND_PRINTK(2, warn, "RA: packet too short\n");
  1049. return;
  1050. }
  1051. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1052. if (skb->ndisc_nodetype == NDISC_NODETYPE_HOST) {
  1053. ND_PRINTK(2, warn, "RA: from host or unauthorized router\n");
  1054. return;
  1055. }
  1056. #endif
  1057. /*
  1058. * set the RA_RECV flag in the interface
  1059. */
  1060. in6_dev = __in6_dev_get(skb->dev);
  1061. if (!in6_dev) {
  1062. ND_PRINTK(0, err, "RA: can't find inet6 device for %s\n",
  1063. skb->dev->name);
  1064. return;
  1065. }
  1066. if (!ndisc_parse_options(skb->dev, opt, optlen, &ndopts)) {
  1067. ND_PRINTK(2, warn, "RA: invalid ND options\n");
  1068. return;
  1069. }
  1070. if (!ipv6_accept_ra(in6_dev)) {
  1071. ND_PRINTK(2, info,
  1072. "RA: %s, did not accept ra for dev: %s\n",
  1073. __func__, skb->dev->name);
  1074. goto skip_linkparms;
  1075. }
  1076. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1077. /* skip link-specific parameters from interior routers */
  1078. if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
  1079. ND_PRINTK(2, info,
  1080. "RA: %s, nodetype is NODEFAULT, dev: %s\n",
  1081. __func__, skb->dev->name);
  1082. goto skip_linkparms;
  1083. }
  1084. #endif
  1085. if (in6_dev->if_flags & IF_RS_SENT) {
  1086. /*
  1087. * flag that an RA was received after an RS was sent
  1088. * out on this interface.
  1089. */
  1090. in6_dev->if_flags |= IF_RA_RCVD;
  1091. }
  1092. /*
  1093. * Remember the managed/otherconf flags from most recently
  1094. * received RA message (RFC 2462) -- yoshfuji
  1095. */
  1096. old_if_flags = in6_dev->if_flags;
  1097. in6_dev->if_flags = (in6_dev->if_flags & ~(IF_RA_MANAGED |
  1098. IF_RA_OTHERCONF)) |
  1099. (ra_msg->icmph.icmp6_addrconf_managed ?
  1100. IF_RA_MANAGED : 0) |
  1101. (ra_msg->icmph.icmp6_addrconf_other ?
  1102. IF_RA_OTHERCONF : 0);
  1103. if (old_if_flags != in6_dev->if_flags)
  1104. send_ifinfo_notify = true;
  1105. if (!in6_dev->cnf.accept_ra_defrtr) {
  1106. ND_PRINTK(2, info,
  1107. "RA: %s, defrtr is false for dev: %s\n",
  1108. __func__, skb->dev->name);
  1109. goto skip_defrtr;
  1110. }
  1111. /* Do not accept RA with source-addr found on local machine unless
  1112. * accept_ra_from_local is set to true.
  1113. */
  1114. net = dev_net(in6_dev->dev);
  1115. if (!in6_dev->cnf.accept_ra_from_local &&
  1116. ipv6_chk_addr(net, &ipv6_hdr(skb)->saddr, in6_dev->dev, 0)) {
  1117. ND_PRINTK(2, info,
  1118. "RA from local address detected on dev: %s: default router ignored\n",
  1119. skb->dev->name);
  1120. goto skip_defrtr;
  1121. }
  1122. lifetime = ntohs(ra_msg->icmph.icmp6_rt_lifetime);
  1123. #ifdef CONFIG_IPV6_ROUTER_PREF
  1124. pref = ra_msg->icmph.icmp6_router_pref;
  1125. /* 10b is handled as if it were 00b (medium) */
  1126. if (pref == ICMPV6_ROUTER_PREF_INVALID ||
  1127. !in6_dev->cnf.accept_ra_rtr_pref)
  1128. pref = ICMPV6_ROUTER_PREF_MEDIUM;
  1129. #endif
  1130. /* routes added from RAs do not use nexthop objects */
  1131. rt = rt6_get_dflt_router(net, &ipv6_hdr(skb)->saddr, skb->dev);
  1132. if (rt) {
  1133. neigh = ip6_neigh_lookup(&rt->fib6_nh->fib_nh_gw6,
  1134. rt->fib6_nh->fib_nh_dev, NULL,
  1135. &ipv6_hdr(skb)->saddr);
  1136. if (!neigh) {
  1137. ND_PRINTK(0, err,
  1138. "RA: %s got default router without neighbour\n",
  1139. __func__);
  1140. fib6_info_release(rt);
  1141. return;
  1142. }
  1143. }
  1144. if (rt && lifetime == 0) {
  1145. ip6_del_rt(net, rt, false);
  1146. rt = NULL;
  1147. }
  1148. ND_PRINTK(3, info, "RA: rt: %p lifetime: %d, for dev: %s\n",
  1149. rt, lifetime, skb->dev->name);
  1150. if (!rt && lifetime) {
  1151. ND_PRINTK(3, info, "RA: adding default router\n");
  1152. rt = rt6_add_dflt_router(net, &ipv6_hdr(skb)->saddr,
  1153. skb->dev, pref);
  1154. if (!rt) {
  1155. ND_PRINTK(0, err,
  1156. "RA: %s failed to add default route\n",
  1157. __func__);
  1158. return;
  1159. }
  1160. neigh = ip6_neigh_lookup(&rt->fib6_nh->fib_nh_gw6,
  1161. rt->fib6_nh->fib_nh_dev, NULL,
  1162. &ipv6_hdr(skb)->saddr);
  1163. if (!neigh) {
  1164. ND_PRINTK(0, err,
  1165. "RA: %s got default router without neighbour\n",
  1166. __func__);
  1167. fib6_info_release(rt);
  1168. return;
  1169. }
  1170. neigh->flags |= NTF_ROUTER;
  1171. } else if (rt) {
  1172. rt->fib6_flags = (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  1173. }
  1174. if (rt)
  1175. fib6_set_expires(rt, jiffies + (HZ * lifetime));
  1176. if (in6_dev->cnf.accept_ra_min_hop_limit < 256 &&
  1177. ra_msg->icmph.icmp6_hop_limit) {
  1178. if (in6_dev->cnf.accept_ra_min_hop_limit <= ra_msg->icmph.icmp6_hop_limit) {
  1179. in6_dev->cnf.hop_limit = ra_msg->icmph.icmp6_hop_limit;
  1180. fib6_metric_set(rt, RTAX_HOPLIMIT,
  1181. ra_msg->icmph.icmp6_hop_limit);
  1182. } else {
  1183. ND_PRINTK(2, warn, "RA: Got route advertisement with lower hop_limit than minimum\n");
  1184. }
  1185. }
  1186. skip_defrtr:
  1187. /*
  1188. * Update Reachable Time and Retrans Timer
  1189. */
  1190. if (in6_dev->nd_parms) {
  1191. unsigned long rtime = ntohl(ra_msg->retrans_timer);
  1192. if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/HZ) {
  1193. rtime = (rtime*HZ)/1000;
  1194. if (rtime < HZ/100)
  1195. rtime = HZ/100;
  1196. NEIGH_VAR_SET(in6_dev->nd_parms, RETRANS_TIME, rtime);
  1197. in6_dev->tstamp = jiffies;
  1198. send_ifinfo_notify = true;
  1199. }
  1200. rtime = ntohl(ra_msg->reachable_time);
  1201. if (rtime && rtime/1000 < MAX_SCHEDULE_TIMEOUT/(3*HZ)) {
  1202. rtime = (rtime*HZ)/1000;
  1203. if (rtime < HZ/10)
  1204. rtime = HZ/10;
  1205. if (rtime != NEIGH_VAR(in6_dev->nd_parms, BASE_REACHABLE_TIME)) {
  1206. NEIGH_VAR_SET(in6_dev->nd_parms,
  1207. BASE_REACHABLE_TIME, rtime);
  1208. NEIGH_VAR_SET(in6_dev->nd_parms,
  1209. GC_STALETIME, 3 * rtime);
  1210. in6_dev->nd_parms->reachable_time = neigh_rand_reach_time(rtime);
  1211. in6_dev->tstamp = jiffies;
  1212. send_ifinfo_notify = true;
  1213. }
  1214. }
  1215. }
  1216. /*
  1217. * Send a notify if RA changed managed/otherconf flags or timer settings
  1218. */
  1219. if (send_ifinfo_notify)
  1220. inet6_ifinfo_notify(RTM_NEWLINK, in6_dev);
  1221. skip_linkparms:
  1222. /*
  1223. * Process options.
  1224. */
  1225. if (!neigh)
  1226. neigh = __neigh_lookup(&nd_tbl, &ipv6_hdr(skb)->saddr,
  1227. skb->dev, 1);
  1228. if (neigh) {
  1229. u8 *lladdr = NULL;
  1230. if (ndopts.nd_opts_src_lladdr) {
  1231. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_src_lladdr,
  1232. skb->dev);
  1233. if (!lladdr) {
  1234. ND_PRINTK(2, warn,
  1235. "RA: invalid link-layer address length\n");
  1236. goto out;
  1237. }
  1238. }
  1239. ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
  1240. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  1241. NEIGH_UPDATE_F_OVERRIDE|
  1242. NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  1243. NEIGH_UPDATE_F_ISROUTER,
  1244. NDISC_ROUTER_ADVERTISEMENT, &ndopts);
  1245. }
  1246. if (!ipv6_accept_ra(in6_dev)) {
  1247. ND_PRINTK(2, info,
  1248. "RA: %s, accept_ra is false for dev: %s\n",
  1249. __func__, skb->dev->name);
  1250. goto out;
  1251. }
  1252. #ifdef CONFIG_IPV6_ROUTE_INFO
  1253. if (!in6_dev->cnf.accept_ra_from_local &&
  1254. ipv6_chk_addr(dev_net(in6_dev->dev), &ipv6_hdr(skb)->saddr,
  1255. in6_dev->dev, 0)) {
  1256. ND_PRINTK(2, info,
  1257. "RA from local address detected on dev: %s: router info ignored.\n",
  1258. skb->dev->name);
  1259. goto skip_routeinfo;
  1260. }
  1261. if (in6_dev->cnf.accept_ra_rtr_pref && ndopts.nd_opts_ri) {
  1262. struct nd_opt_hdr *p;
  1263. for (p = ndopts.nd_opts_ri;
  1264. p;
  1265. p = ndisc_next_option(p, ndopts.nd_opts_ri_end)) {
  1266. struct route_info *ri = (struct route_info *)p;
  1267. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1268. if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT &&
  1269. ri->prefix_len == 0)
  1270. continue;
  1271. #endif
  1272. if (ri->prefix_len == 0 &&
  1273. !in6_dev->cnf.accept_ra_defrtr)
  1274. continue;
  1275. if (ri->prefix_len < in6_dev->cnf.accept_ra_rt_info_min_plen)
  1276. continue;
  1277. if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
  1278. continue;
  1279. rt6_route_rcv(skb->dev, (u8 *)p, (p->nd_opt_len) << 3,
  1280. &ipv6_hdr(skb)->saddr);
  1281. }
  1282. }
  1283. skip_routeinfo:
  1284. #endif
  1285. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1286. /* skip link-specific ndopts from interior routers */
  1287. if (skb->ndisc_nodetype == NDISC_NODETYPE_NODEFAULT) {
  1288. ND_PRINTK(2, info,
  1289. "RA: %s, nodetype is NODEFAULT (interior routes), dev: %s\n",
  1290. __func__, skb->dev->name);
  1291. goto out;
  1292. }
  1293. #endif
  1294. if (in6_dev->cnf.accept_ra_pinfo && ndopts.nd_opts_pi) {
  1295. struct nd_opt_hdr *p;
  1296. for (p = ndopts.nd_opts_pi;
  1297. p;
  1298. p = ndisc_next_option(p, ndopts.nd_opts_pi_end)) {
  1299. addrconf_prefix_rcv(skb->dev, (u8 *)p,
  1300. (p->nd_opt_len) << 3,
  1301. ndopts.nd_opts_src_lladdr != NULL);
  1302. }
  1303. }
  1304. if (ndopts.nd_opts_mtu && in6_dev->cnf.accept_ra_mtu) {
  1305. __be32 n;
  1306. u32 mtu;
  1307. memcpy(&n, ((u8 *)(ndopts.nd_opts_mtu+1))+2, sizeof(mtu));
  1308. mtu = ntohl(n);
  1309. if (mtu < IPV6_MIN_MTU || mtu > skb->dev->mtu) {
  1310. ND_PRINTK(2, warn, "RA: invalid mtu: %d\n", mtu);
  1311. } else if (in6_dev->cnf.mtu6 != mtu) {
  1312. in6_dev->cnf.mtu6 = mtu;
  1313. fib6_metric_set(rt, RTAX_MTU, mtu);
  1314. rt6_mtu_change(skb->dev, mtu);
  1315. }
  1316. }
  1317. if (ndopts.nd_useropts) {
  1318. struct nd_opt_hdr *p;
  1319. for (p = ndopts.nd_useropts;
  1320. p;
  1321. p = ndisc_next_useropt(skb->dev, p,
  1322. ndopts.nd_useropts_end)) {
  1323. ndisc_ra_useropt(skb, p);
  1324. }
  1325. }
  1326. if (ndopts.nd_opts_tgt_lladdr || ndopts.nd_opts_rh) {
  1327. ND_PRINTK(2, warn, "RA: invalid RA options\n");
  1328. }
  1329. out:
  1330. fib6_info_release(rt);
  1331. if (neigh)
  1332. neigh_release(neigh);
  1333. }
  1334. static void ndisc_redirect_rcv(struct sk_buff *skb)
  1335. {
  1336. u8 *hdr;
  1337. struct ndisc_options ndopts;
  1338. struct rd_msg *msg = (struct rd_msg *)skb_transport_header(skb);
  1339. u32 ndoptlen = skb_tail_pointer(skb) - (skb_transport_header(skb) +
  1340. offsetof(struct rd_msg, opt));
  1341. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  1342. switch (skb->ndisc_nodetype) {
  1343. case NDISC_NODETYPE_HOST:
  1344. case NDISC_NODETYPE_NODEFAULT:
  1345. ND_PRINTK(2, warn,
  1346. "Redirect: from host or unauthorized router\n");
  1347. return;
  1348. }
  1349. #endif
  1350. if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
  1351. ND_PRINTK(2, warn,
  1352. "Redirect: source address is not link-local\n");
  1353. return;
  1354. }
  1355. if (!ndisc_parse_options(skb->dev, msg->opt, ndoptlen, &ndopts))
  1356. return;
  1357. if (!ndopts.nd_opts_rh) {
  1358. ip6_redirect_no_header(skb, dev_net(skb->dev),
  1359. skb->dev->ifindex);
  1360. return;
  1361. }
  1362. hdr = (u8 *)ndopts.nd_opts_rh;
  1363. hdr += 8;
  1364. if (!pskb_pull(skb, hdr - skb_transport_header(skb)))
  1365. return;
  1366. icmpv6_notify(skb, NDISC_REDIRECT, 0, 0);
  1367. }
  1368. static void ndisc_fill_redirect_hdr_option(struct sk_buff *skb,
  1369. struct sk_buff *orig_skb,
  1370. int rd_len)
  1371. {
  1372. u8 *opt = skb_put(skb, rd_len);
  1373. memset(opt, 0, 8);
  1374. *(opt++) = ND_OPT_REDIRECT_HDR;
  1375. *(opt++) = (rd_len >> 3);
  1376. opt += 6;
  1377. skb_copy_bits(orig_skb, skb_network_offset(orig_skb), opt,
  1378. rd_len - 8);
  1379. }
  1380. void ndisc_send_redirect(struct sk_buff *skb, const struct in6_addr *target)
  1381. {
  1382. struct net_device *dev = skb->dev;
  1383. struct net *net = dev_net(dev);
  1384. struct sock *sk = net->ipv6.ndisc_sk;
  1385. int optlen = 0;
  1386. struct inet_peer *peer;
  1387. struct sk_buff *buff;
  1388. struct rd_msg *msg;
  1389. struct in6_addr saddr_buf;
  1390. struct rt6_info *rt;
  1391. struct dst_entry *dst;
  1392. struct flowi6 fl6;
  1393. int rd_len;
  1394. u8 ha_buf[MAX_ADDR_LEN], *ha = NULL,
  1395. ops_data_buf[NDISC_OPS_REDIRECT_DATA_SPACE], *ops_data = NULL;
  1396. bool ret;
  1397. if (netif_is_l3_master(skb->dev)) {
  1398. dev = __dev_get_by_index(dev_net(skb->dev), IPCB(skb)->iif);
  1399. if (!dev)
  1400. return;
  1401. }
  1402. if (ipv6_get_lladdr(dev, &saddr_buf, IFA_F_TENTATIVE)) {
  1403. ND_PRINTK(2, warn, "Redirect: no link-local address on %s\n",
  1404. dev->name);
  1405. return;
  1406. }
  1407. if (!ipv6_addr_equal(&ipv6_hdr(skb)->daddr, target) &&
  1408. ipv6_addr_type(target) != (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  1409. ND_PRINTK(2, warn,
  1410. "Redirect: target address is not link-local unicast\n");
  1411. return;
  1412. }
  1413. icmpv6_flow_init(sk, &fl6, NDISC_REDIRECT,
  1414. &saddr_buf, &ipv6_hdr(skb)->saddr, dev->ifindex);
  1415. dst = ip6_route_output(net, NULL, &fl6);
  1416. if (dst->error) {
  1417. dst_release(dst);
  1418. return;
  1419. }
  1420. dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
  1421. if (IS_ERR(dst))
  1422. return;
  1423. rt = (struct rt6_info *) dst;
  1424. if (rt->rt6i_flags & RTF_GATEWAY) {
  1425. ND_PRINTK(2, warn,
  1426. "Redirect: destination is not a neighbour\n");
  1427. goto release;
  1428. }
  1429. peer = inet_getpeer_v6(net->ipv6.peers, &ipv6_hdr(skb)->saddr, 1);
  1430. ret = inet_peer_xrlim_allow(peer, 1*HZ);
  1431. if (peer)
  1432. inet_putpeer(peer);
  1433. if (!ret)
  1434. goto release;
  1435. if (dev->addr_len) {
  1436. struct neighbour *neigh = dst_neigh_lookup(skb_dst(skb), target);
  1437. if (!neigh) {
  1438. ND_PRINTK(2, warn,
  1439. "Redirect: no neigh for target address\n");
  1440. goto release;
  1441. }
  1442. read_lock_bh(&neigh->lock);
  1443. if (neigh->nud_state & NUD_VALID) {
  1444. memcpy(ha_buf, neigh->ha, dev->addr_len);
  1445. read_unlock_bh(&neigh->lock);
  1446. ha = ha_buf;
  1447. optlen += ndisc_redirect_opt_addr_space(dev, neigh,
  1448. ops_data_buf,
  1449. &ops_data);
  1450. } else
  1451. read_unlock_bh(&neigh->lock);
  1452. neigh_release(neigh);
  1453. }
  1454. rd_len = min_t(unsigned int,
  1455. IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(*msg) - optlen,
  1456. skb->len + 8);
  1457. rd_len &= ~0x7;
  1458. optlen += rd_len;
  1459. buff = ndisc_alloc_skb(dev, sizeof(*msg) + optlen);
  1460. if (!buff)
  1461. goto release;
  1462. msg = skb_put(buff, sizeof(*msg));
  1463. *msg = (struct rd_msg) {
  1464. .icmph = {
  1465. .icmp6_type = NDISC_REDIRECT,
  1466. },
  1467. .target = *target,
  1468. .dest = ipv6_hdr(skb)->daddr,
  1469. };
  1470. /*
  1471. * include target_address option
  1472. */
  1473. if (ha)
  1474. ndisc_fill_redirect_addr_option(buff, ha, ops_data);
  1475. /*
  1476. * build redirect option and copy skb over to the new packet.
  1477. */
  1478. if (rd_len)
  1479. ndisc_fill_redirect_hdr_option(buff, skb, rd_len);
  1480. skb_dst_set(buff, dst);
  1481. ndisc_send_skb(buff, &ipv6_hdr(skb)->saddr, &saddr_buf);
  1482. return;
  1483. release:
  1484. dst_release(dst);
  1485. }
  1486. static void pndisc_redo(struct sk_buff *skb)
  1487. {
  1488. ndisc_recv_ns(skb);
  1489. kfree_skb(skb);
  1490. }
  1491. static int ndisc_is_multicast(const void *pkey)
  1492. {
  1493. return ipv6_addr_is_multicast((struct in6_addr *)pkey);
  1494. }
  1495. static bool ndisc_suppress_frag_ndisc(struct sk_buff *skb)
  1496. {
  1497. struct inet6_dev *idev = __in6_dev_get(skb->dev);
  1498. if (!idev)
  1499. return true;
  1500. if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED &&
  1501. idev->cnf.suppress_frag_ndisc) {
  1502. net_warn_ratelimited("Received fragmented ndisc packet. Carefully consider disabling suppress_frag_ndisc.\n");
  1503. return true;
  1504. }
  1505. return false;
  1506. }
  1507. int ndisc_rcv(struct sk_buff *skb)
  1508. {
  1509. struct nd_msg *msg;
  1510. if (ndisc_suppress_frag_ndisc(skb))
  1511. return 0;
  1512. if (skb_linearize(skb))
  1513. return 0;
  1514. msg = (struct nd_msg *)skb_transport_header(skb);
  1515. __skb_push(skb, skb->data - skb_transport_header(skb));
  1516. if (ipv6_hdr(skb)->hop_limit != 255) {
  1517. ND_PRINTK(2, warn, "NDISC: invalid hop-limit: %d\n",
  1518. ipv6_hdr(skb)->hop_limit);
  1519. return 0;
  1520. }
  1521. if (msg->icmph.icmp6_code != 0) {
  1522. ND_PRINTK(2, warn, "NDISC: invalid ICMPv6 code: %d\n",
  1523. msg->icmph.icmp6_code);
  1524. return 0;
  1525. }
  1526. switch (msg->icmph.icmp6_type) {
  1527. case NDISC_NEIGHBOUR_SOLICITATION:
  1528. memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
  1529. ndisc_recv_ns(skb);
  1530. break;
  1531. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  1532. ndisc_recv_na(skb);
  1533. break;
  1534. case NDISC_ROUTER_SOLICITATION:
  1535. ndisc_recv_rs(skb);
  1536. break;
  1537. case NDISC_ROUTER_ADVERTISEMENT:
  1538. ndisc_router_discovery(skb);
  1539. break;
  1540. case NDISC_REDIRECT:
  1541. ndisc_redirect_rcv(skb);
  1542. break;
  1543. }
  1544. return 0;
  1545. }
  1546. static int ndisc_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1547. {
  1548. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1549. struct netdev_notifier_change_info *change_info;
  1550. struct net *net = dev_net(dev);
  1551. struct inet6_dev *idev;
  1552. switch (event) {
  1553. case NETDEV_CHANGEADDR:
  1554. neigh_changeaddr(&nd_tbl, dev);
  1555. fib6_run_gc(0, net, false);
  1556. fallthrough;
  1557. case NETDEV_UP:
  1558. idev = in6_dev_get(dev);
  1559. if (!idev)
  1560. break;
  1561. if (idev->cnf.ndisc_notify ||
  1562. net->ipv6.devconf_all->ndisc_notify)
  1563. ndisc_send_unsol_na(dev);
  1564. in6_dev_put(idev);
  1565. break;
  1566. case NETDEV_CHANGE:
  1567. change_info = ptr;
  1568. if (change_info->flags_changed & IFF_NOARP)
  1569. neigh_changeaddr(&nd_tbl, dev);
  1570. if (!netif_carrier_ok(dev))
  1571. neigh_carrier_down(&nd_tbl, dev);
  1572. break;
  1573. case NETDEV_DOWN:
  1574. neigh_ifdown(&nd_tbl, dev);
  1575. fib6_run_gc(0, net, false);
  1576. break;
  1577. case NETDEV_NOTIFY_PEERS:
  1578. ndisc_send_unsol_na(dev);
  1579. break;
  1580. default:
  1581. break;
  1582. }
  1583. return NOTIFY_DONE;
  1584. }
  1585. static struct notifier_block ndisc_netdev_notifier = {
  1586. .notifier_call = ndisc_netdev_event,
  1587. .priority = ADDRCONF_NOTIFY_PRIORITY - 5,
  1588. };
  1589. #ifdef CONFIG_SYSCTL
  1590. static void ndisc_warn_deprecated_sysctl(struct ctl_table *ctl,
  1591. const char *func, const char *dev_name)
  1592. {
  1593. static char warncomm[TASK_COMM_LEN];
  1594. static int warned;
  1595. if (strcmp(warncomm, current->comm) && warned < 5) {
  1596. strcpy(warncomm, current->comm);
  1597. pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n",
  1598. warncomm, func,
  1599. dev_name, ctl->procname,
  1600. dev_name, ctl->procname);
  1601. warned++;
  1602. }
  1603. }
  1604. int ndisc_ifinfo_sysctl_change(struct ctl_table *ctl, int write, void *buffer,
  1605. size_t *lenp, loff_t *ppos)
  1606. {
  1607. struct net_device *dev = ctl->extra1;
  1608. struct inet6_dev *idev;
  1609. int ret;
  1610. if ((strcmp(ctl->procname, "retrans_time") == 0) ||
  1611. (strcmp(ctl->procname, "base_reachable_time") == 0))
  1612. ndisc_warn_deprecated_sysctl(ctl, "syscall", dev ? dev->name : "default");
  1613. if (strcmp(ctl->procname, "retrans_time") == 0)
  1614. ret = neigh_proc_dointvec(ctl, write, buffer, lenp, ppos);
  1615. else if (strcmp(ctl->procname, "base_reachable_time") == 0)
  1616. ret = neigh_proc_dointvec_jiffies(ctl, write,
  1617. buffer, lenp, ppos);
  1618. else if ((strcmp(ctl->procname, "retrans_time_ms") == 0) ||
  1619. (strcmp(ctl->procname, "base_reachable_time_ms") == 0))
  1620. ret = neigh_proc_dointvec_ms_jiffies(ctl, write,
  1621. buffer, lenp, ppos);
  1622. else
  1623. ret = -1;
  1624. if (write && ret == 0 && dev && (idev = in6_dev_get(dev)) != NULL) {
  1625. if (ctl->data == &NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME))
  1626. idev->nd_parms->reachable_time =
  1627. neigh_rand_reach_time(NEIGH_VAR(idev->nd_parms, BASE_REACHABLE_TIME));
  1628. idev->tstamp = jiffies;
  1629. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  1630. in6_dev_put(idev);
  1631. }
  1632. return ret;
  1633. }
  1634. #endif
  1635. static int __net_init ndisc_net_init(struct net *net)
  1636. {
  1637. struct ipv6_pinfo *np;
  1638. struct sock *sk;
  1639. int err;
  1640. err = inet_ctl_sock_create(&sk, PF_INET6,
  1641. SOCK_RAW, IPPROTO_ICMPV6, net);
  1642. if (err < 0) {
  1643. ND_PRINTK(0, err,
  1644. "NDISC: Failed to initialize the control socket (err %d)\n",
  1645. err);
  1646. return err;
  1647. }
  1648. net->ipv6.ndisc_sk = sk;
  1649. np = inet6_sk(sk);
  1650. np->hop_limit = 255;
  1651. /* Do not loopback ndisc messages */
  1652. np->mc_loop = 0;
  1653. return 0;
  1654. }
  1655. static void __net_exit ndisc_net_exit(struct net *net)
  1656. {
  1657. inet_ctl_sock_destroy(net->ipv6.ndisc_sk);
  1658. }
  1659. static struct pernet_operations ndisc_net_ops = {
  1660. .init = ndisc_net_init,
  1661. .exit = ndisc_net_exit,
  1662. };
  1663. int __init ndisc_init(void)
  1664. {
  1665. int err;
  1666. err = register_pernet_subsys(&ndisc_net_ops);
  1667. if (err)
  1668. return err;
  1669. /*
  1670. * Initialize the neighbour table
  1671. */
  1672. neigh_table_init(NEIGH_ND_TABLE, &nd_tbl);
  1673. #ifdef CONFIG_SYSCTL
  1674. err = neigh_sysctl_register(NULL, &nd_tbl.parms,
  1675. ndisc_ifinfo_sysctl_change);
  1676. if (err)
  1677. goto out_unregister_pernet;
  1678. out:
  1679. #endif
  1680. return err;
  1681. #ifdef CONFIG_SYSCTL
  1682. out_unregister_pernet:
  1683. unregister_pernet_subsys(&ndisc_net_ops);
  1684. goto out;
  1685. #endif
  1686. }
  1687. int __init ndisc_late_init(void)
  1688. {
  1689. return register_netdevice_notifier(&ndisc_netdev_notifier);
  1690. }
  1691. void ndisc_late_cleanup(void)
  1692. {
  1693. unregister_netdevice_notifier(&ndisc_netdev_notifier);
  1694. }
  1695. void ndisc_cleanup(void)
  1696. {
  1697. #ifdef CONFIG_SYSCTL
  1698. neigh_sysctl_unregister(&nd_tbl.parms);
  1699. #endif
  1700. neigh_table_clear(NEIGH_ND_TABLE, &nd_tbl);
  1701. unregister_pernet_subsys(&ndisc_net_ops);
  1702. }