nd6_nbr.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the project nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * $KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
  32. */
  33. #include <sys/cdefs.h>
  34. __FBSDID("$FreeBSD$");
  35. #include "opt_inet.h"
  36. #include "opt_inet6.h"
  37. #include "opt_ipsec.h"
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/eventhandler.h>
  41. #include <sys/malloc.h>
  42. #include <sys/libkern.h>
  43. #include <sys/lock.h>
  44. #include <sys/rwlock.h>
  45. #include <sys/mbuf.h>
  46. #include <sys/socket.h>
  47. #include <sys/sockio.h>
  48. #include <sys/time.h>
  49. #include <sys/kernel.h>
  50. #include <sys/errno.h>
  51. #include <sys/sysctl.h>
  52. #include <sys/syslog.h>
  53. #include <sys/queue.h>
  54. #include <sys/callout.h>
  55. #include <sys/refcount.h>
  56. #include <net/if.h>
  57. #include <net/if_types.h>
  58. #include <net/if_dl.h>
  59. #include <net/if_var.h>
  60. #include <net/route.h>
  61. #include <net/vnet.h>
  62. #include <netinet/in.h>
  63. #include <netinet/in_var.h>
  64. #include <net/if_llatbl.h>
  65. #include <netinet6/in6_var.h>
  66. #include <netinet6/in6_ifattach.h>
  67. #include <netinet/ip6.h>
  68. #include <netinet6/ip6_var.h>
  69. #include <netinet6/scope6_var.h>
  70. #include <netinet6/nd6.h>
  71. #include <netinet/icmp6.h>
  72. #include <netinet/ip_carp.h>
  73. #include <netinet6/send.h>
  74. #define SDL(s) ((struct sockaddr_dl *)s)
  75. struct dadq;
  76. static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *);
  77. static void nd6_dad_add(struct dadq *dp);
  78. static void nd6_dad_del(struct dadq *dp);
  79. static void nd6_dad_rele(struct dadq *);
  80. static void nd6_dad_starttimer(struct dadq *, int, int);
  81. static void nd6_dad_stoptimer(struct dadq *);
  82. static void nd6_dad_timer(struct dadq *);
  83. static void nd6_dad_duplicated(struct ifaddr *, struct dadq *);
  84. static void nd6_dad_ns_output(struct dadq *);
  85. static void nd6_dad_ns_input(struct ifaddr *, struct nd_opt_nonce *);
  86. static void nd6_dad_na_input(struct ifaddr *);
  87. static void nd6_na_output_fib(struct ifnet *, const struct in6_addr *,
  88. const struct in6_addr *, u_long, int, struct sockaddr *, u_int);
  89. static void nd6_ns_output_fib(struct ifnet *, const struct in6_addr *,
  90. const struct in6_addr *, const struct in6_addr *, uint8_t *, u_int);
  91. VNET_DEFINE_STATIC(int, dad_enhanced) = 1;
  92. #define V_dad_enhanced VNET(dad_enhanced)
  93. SYSCTL_DECL(_net_inet6_ip6);
  94. SYSCTL_INT(_net_inet6_ip6, OID_AUTO, dad_enhanced, CTLFLAG_VNET | CTLFLAG_RW,
  95. &VNET_NAME(dad_enhanced), 0,
  96. "Enable Enhanced DAD, which adds a random nonce to NS messages for DAD.");
  97. VNET_DEFINE_STATIC(int, dad_maxtry) = 15; /* max # of *tries* to
  98. transmit DAD packet */
  99. #define V_dad_maxtry VNET(dad_maxtry)
  100. /*
  101. * Input a Neighbor Solicitation Message.
  102. *
  103. * Based on RFC 2461
  104. * Based on RFC 2462 (duplicate address detection)
  105. */
  106. void
  107. nd6_ns_input(struct mbuf *m, int off, int icmp6len)
  108. {
  109. struct ifnet *ifp;
  110. struct ip6_hdr *ip6;
  111. struct nd_neighbor_solicit *nd_ns;
  112. struct in6_addr daddr6, myaddr6, saddr6, taddr6;
  113. struct ifaddr *ifa;
  114. struct sockaddr_dl proxydl;
  115. union nd_opts ndopts;
  116. char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
  117. char *lladdr;
  118. int anycast, lladdrlen, proxy, rflag, tentative, tlladdr;
  119. ifa = NULL;
  120. /* RFC 6980: Nodes MUST silently ignore fragments */
  121. if(m->m_flags & M_FRAGMENTED)
  122. goto freeit;
  123. ifp = m->m_pkthdr.rcvif;
  124. ip6 = mtod(m, struct ip6_hdr *);
  125. if (__predict_false(ip6->ip6_hlim != 255)) {
  126. ICMP6STAT_INC(icp6s_invlhlim);
  127. nd6log((LOG_ERR,
  128. "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
  129. ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
  130. ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
  131. goto bads;
  132. }
  133. if (m->m_len < off + icmp6len) {
  134. m = m_pullup(m, off + icmp6len);
  135. if (m == NULL) {
  136. IP6STAT_INC(ip6s_exthdrtoolong);
  137. return;
  138. }
  139. }
  140. ip6 = mtod(m, struct ip6_hdr *);
  141. nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
  142. saddr6 = ip6->ip6_src;
  143. daddr6 = ip6->ip6_dst;
  144. taddr6 = nd_ns->nd_ns_target;
  145. if (in6_setscope(&taddr6, ifp, NULL) != 0)
  146. goto bad;
  147. rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0;
  148. if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif)
  149. rflag = 0;
  150. if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
  151. /* dst has to be a solicited node multicast address. */
  152. if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
  153. /* don't check ifindex portion */
  154. daddr6.s6_addr32[1] == 0 &&
  155. daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
  156. daddr6.s6_addr8[12] == 0xff) {
  157. ; /* good */
  158. } else {
  159. nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
  160. "(wrong ip6 dst)\n"));
  161. goto bad;
  162. }
  163. } else if (!V_nd6_onlink_ns_rfc4861) {
  164. struct sockaddr_in6 src_sa6;
  165. /*
  166. * According to recent IETF discussions, it is not a good idea
  167. * to accept a NS from an address which would not be deemed
  168. * to be a neighbor otherwise. This point is expected to be
  169. * clarified in future revisions of the specification.
  170. */
  171. bzero(&src_sa6, sizeof(src_sa6));
  172. src_sa6.sin6_family = AF_INET6;
  173. src_sa6.sin6_len = sizeof(src_sa6);
  174. src_sa6.sin6_addr = saddr6;
  175. if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
  176. nd6log((LOG_INFO, "nd6_ns_input: "
  177. "NS packet from non-neighbor\n"));
  178. goto bad;
  179. }
  180. }
  181. if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
  182. nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
  183. goto bad;
  184. }
  185. icmp6len -= sizeof(*nd_ns);
  186. nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
  187. if (nd6_options(&ndopts) < 0) {
  188. nd6log((LOG_INFO,
  189. "nd6_ns_input: invalid ND option, ignored\n"));
  190. /* nd6_options have incremented stats */
  191. goto freeit;
  192. }
  193. lladdr = NULL;
  194. lladdrlen = 0;
  195. if (ndopts.nd_opts_src_lladdr) {
  196. lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
  197. lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
  198. }
  199. if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
  200. nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
  201. "(link-layer address option)\n"));
  202. goto bad;
  203. }
  204. /*
  205. * Attaching target link-layer address to the NA?
  206. * (RFC 2461 7.2.4)
  207. *
  208. * NS IP dst is unicast/anycast MUST NOT add
  209. * NS IP dst is solicited-node multicast MUST add
  210. *
  211. * In implementation, we add target link-layer address by default.
  212. * We do not add one in MUST NOT cases.
  213. */
  214. if (!IN6_IS_ADDR_MULTICAST(&daddr6))
  215. tlladdr = 0;
  216. else
  217. tlladdr = 1;
  218. /*
  219. * Target address (taddr6) must be either:
  220. * (1) Valid unicast/anycast address for my receiving interface,
  221. * (2) Unicast address for which I'm offering proxy service, or
  222. * (3) "tentative" address on which DAD is being performed.
  223. */
  224. /* (1) and (3) check. */
  225. if (ifp->if_carp)
  226. ifa = (*carp_iamatch6_p)(ifp, &taddr6);
  227. else
  228. ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
  229. /* (2) check. */
  230. proxy = 0;
  231. if (ifa == NULL) {
  232. struct sockaddr_dl rt_gateway;
  233. struct rt_addrinfo info;
  234. struct sockaddr_in6 dst6;
  235. bzero(&dst6, sizeof(dst6));
  236. dst6.sin6_len = sizeof(struct sockaddr_in6);
  237. dst6.sin6_family = AF_INET6;
  238. dst6.sin6_addr = taddr6;
  239. bzero(&rt_gateway, sizeof(rt_gateway));
  240. rt_gateway.sdl_len = sizeof(rt_gateway);
  241. bzero(&info, sizeof(info));
  242. info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
  243. if (rib_lookup_info(ifp->if_fib, (struct sockaddr *)&dst6,
  244. 0, 0, &info) == 0) {
  245. if ((info.rti_flags & RTF_ANNOUNCE) != 0 &&
  246. rt_gateway.sdl_family == AF_LINK) {
  247. /*
  248. * proxy NDP for single entry
  249. */
  250. proxydl = *SDL(&rt_gateway);
  251. ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(
  252. ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
  253. if (ifa)
  254. proxy = 1;
  255. }
  256. }
  257. }
  258. if (ifa == NULL) {
  259. /*
  260. * We've got an NS packet, and we don't have that adddress
  261. * assigned for us. We MUST silently ignore it.
  262. * See RFC2461 7.2.3.
  263. */
  264. goto freeit;
  265. }
  266. myaddr6 = *IFA_IN6(ifa);
  267. anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
  268. tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
  269. if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
  270. goto freeit;
  271. if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  272. nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
  273. "(if %d, NS packet %d)\n",
  274. ip6_sprintf(ip6bufs, &taddr6),
  275. ifp->if_addrlen, lladdrlen - 2));
  276. goto bad;
  277. }
  278. if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
  279. nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
  280. ip6_sprintf(ip6bufs, &saddr6)));
  281. goto freeit;
  282. }
  283. /*
  284. * We have neighbor solicitation packet, with target address equals to
  285. * one of my tentative address.
  286. *
  287. * src addr how to process?
  288. * --- ---
  289. * multicast of course, invalid (rejected in ip6_input)
  290. * unicast somebody is doing address resolution -> ignore
  291. * unspec dup address detection
  292. *
  293. * The processing is defined in RFC 2462.
  294. */
  295. if (tentative) {
  296. /*
  297. * If source address is unspecified address, it is for
  298. * duplicate address detection.
  299. *
  300. * If not, the packet is for addess resolution;
  301. * silently ignore it.
  302. */
  303. if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
  304. nd6_dad_ns_input(ifa, ndopts.nd_opts_nonce);
  305. goto freeit;
  306. }
  307. /*
  308. * If the source address is unspecified address, entries must not
  309. * be created or updated.
  310. * It looks that sender is performing DAD. Output NA toward
  311. * all-node multicast address, to tell the sender that I'm using
  312. * the address.
  313. * S bit ("solicited") must be zero.
  314. */
  315. if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
  316. struct in6_addr in6_all;
  317. in6_all = in6addr_linklocal_allnodes;
  318. if (in6_setscope(&in6_all, ifp, NULL) != 0)
  319. goto bad;
  320. nd6_na_output_fib(ifp, &in6_all, &taddr6,
  321. ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
  322. rflag, tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL,
  323. M_GETFIB(m));
  324. goto freeit;
  325. }
  326. nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
  327. ND_NEIGHBOR_SOLICIT, 0);
  328. nd6_na_output_fib(ifp, &saddr6, &taddr6,
  329. ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
  330. rflag | ND_NA_FLAG_SOLICITED, tlladdr,
  331. proxy ? (struct sockaddr *)&proxydl : NULL, M_GETFIB(m));
  332. freeit:
  333. if (ifa != NULL)
  334. ifa_free(ifa);
  335. m_freem(m);
  336. return;
  337. bad:
  338. nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
  339. ip6_sprintf(ip6bufs, &saddr6)));
  340. nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
  341. ip6_sprintf(ip6bufs, &daddr6)));
  342. nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
  343. ip6_sprintf(ip6bufs, &taddr6)));
  344. bads:
  345. ICMP6STAT_INC(icp6s_badns);
  346. if (ifa != NULL)
  347. ifa_free(ifa);
  348. m_freem(m);
  349. }
  350. /*
  351. * Output a Neighbor Solicitation Message. Caller specifies:
  352. * - ICMP6 header source IP6 address
  353. * - ND6 header target IP6 address
  354. * - ND6 header source datalink address
  355. *
  356. * Based on RFC 2461
  357. * Based on RFC 2462 (duplicate address detection)
  358. *
  359. * ln - for source address determination
  360. * nonce - If non-NULL, NS is used for duplicate address detection and
  361. * the value (length is ND_OPT_NONCE_LEN) is used as a random nonce.
  362. */
  363. static void
  364. nd6_ns_output_fib(struct ifnet *ifp, const struct in6_addr *saddr6,
  365. const struct in6_addr *daddr6, const struct in6_addr *taddr6,
  366. uint8_t *nonce, u_int fibnum)
  367. {
  368. struct mbuf *m;
  369. struct m_tag *mtag;
  370. struct ip6_hdr *ip6;
  371. struct nd_neighbor_solicit *nd_ns;
  372. struct ip6_moptions im6o;
  373. int icmp6len;
  374. int maxlen;
  375. caddr_t mac;
  376. if (IN6_IS_ADDR_MULTICAST(taddr6))
  377. return;
  378. /* estimate the size of message */
  379. maxlen = sizeof(*ip6) + sizeof(*nd_ns);
  380. maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
  381. KASSERT(max_linkhdr + maxlen <= MCLBYTES, (
  382. "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
  383. __func__, max_linkhdr, maxlen, MCLBYTES));
  384. if (max_linkhdr + maxlen > MHLEN)
  385. m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
  386. else
  387. m = m_gethdr(M_NOWAIT, MT_DATA);
  388. if (m == NULL)
  389. return;
  390. M_SETFIB(m, fibnum);
  391. if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
  392. m->m_flags |= M_MCAST;
  393. im6o.im6o_multicast_ifp = ifp;
  394. im6o.im6o_multicast_hlim = 255;
  395. im6o.im6o_multicast_loop = 0;
  396. }
  397. icmp6len = sizeof(*nd_ns);
  398. m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
  399. m->m_data += max_linkhdr; /* or M_ALIGN() equivalent? */
  400. /* fill neighbor solicitation packet */
  401. ip6 = mtod(m, struct ip6_hdr *);
  402. ip6->ip6_flow = 0;
  403. ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  404. ip6->ip6_vfc |= IPV6_VERSION;
  405. /* ip6->ip6_plen will be set later */
  406. ip6->ip6_nxt = IPPROTO_ICMPV6;
  407. ip6->ip6_hlim = 255;
  408. if (daddr6)
  409. ip6->ip6_dst = *daddr6;
  410. else {
  411. ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
  412. ip6->ip6_dst.s6_addr16[1] = 0;
  413. ip6->ip6_dst.s6_addr32[1] = 0;
  414. ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
  415. ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
  416. ip6->ip6_dst.s6_addr8[12] = 0xff;
  417. if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
  418. goto bad;
  419. }
  420. if (nonce == NULL) {
  421. struct ifaddr *ifa = NULL;
  422. /*
  423. * RFC2461 7.2.2:
  424. * "If the source address of the packet prompting the
  425. * solicitation is the same as one of the addresses assigned
  426. * to the outgoing interface, that address SHOULD be placed
  427. * in the IP Source Address of the outgoing solicitation.
  428. * Otherwise, any one of the addresses assigned to the
  429. * interface should be used."
  430. *
  431. * We use the source address for the prompting packet
  432. * (saddr6), if saddr6 belongs to the outgoing interface.
  433. * Otherwise, we perform the source address selection as usual.
  434. */
  435. if (saddr6 != NULL)
  436. ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, saddr6);
  437. if (ifa != NULL) {
  438. /* ip6_src set already. */
  439. ip6->ip6_src = *saddr6;
  440. ifa_free(ifa);
  441. } else {
  442. int error;
  443. struct in6_addr dst6, src6;
  444. uint32_t scopeid;
  445. in6_splitscope(&ip6->ip6_dst, &dst6, &scopeid);
  446. error = in6_selectsrc_addr(fibnum, &dst6,
  447. scopeid, ifp, &src6, NULL);
  448. if (error) {
  449. char ip6buf[INET6_ADDRSTRLEN];
  450. nd6log((LOG_DEBUG, "%s: source can't be "
  451. "determined: dst=%s, error=%d\n", __func__,
  452. ip6_sprintf(ip6buf, &dst6),
  453. error));
  454. goto bad;
  455. }
  456. ip6->ip6_src = src6;
  457. }
  458. } else {
  459. /*
  460. * Source address for DAD packet must always be IPv6
  461. * unspecified address. (0::0)
  462. * We actually don't have to 0-clear the address (we did it
  463. * above), but we do so here explicitly to make the intention
  464. * clearer.
  465. */
  466. bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
  467. }
  468. nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
  469. nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
  470. nd_ns->nd_ns_code = 0;
  471. nd_ns->nd_ns_reserved = 0;
  472. nd_ns->nd_ns_target = *taddr6;
  473. in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
  474. /*
  475. * Add source link-layer address option.
  476. *
  477. * spec implementation
  478. * --- ---
  479. * DAD packet MUST NOT do not add the option
  480. * there's no link layer address:
  481. * impossible do not add the option
  482. * there's link layer address:
  483. * Multicast NS MUST add one add the option
  484. * Unicast NS SHOULD add one add the option
  485. */
  486. if (nonce == NULL && (mac = nd6_ifptomac(ifp))) {
  487. int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
  488. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
  489. /* 8 byte alignments... */
  490. optlen = (optlen + 7) & ~7;
  491. m->m_pkthdr.len += optlen;
  492. m->m_len += optlen;
  493. icmp6len += optlen;
  494. bzero((caddr_t)nd_opt, optlen);
  495. nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
  496. nd_opt->nd_opt_len = optlen >> 3;
  497. bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
  498. }
  499. /*
  500. * Add a Nonce option (RFC 3971) to detect looped back NS messages.
  501. * This behavior is documented as Enhanced Duplicate Address
  502. * Detection in RFC 7527.
  503. * net.inet6.ip6.dad_enhanced=0 disables this.
  504. */
  505. if (V_dad_enhanced != 0 && nonce != NULL) {
  506. int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN;
  507. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
  508. /* 8-byte alignment is required. */
  509. optlen = (optlen + 7) & ~7;
  510. m->m_pkthdr.len += optlen;
  511. m->m_len += optlen;
  512. icmp6len += optlen;
  513. bzero((caddr_t)nd_opt, optlen);
  514. nd_opt->nd_opt_type = ND_OPT_NONCE;
  515. nd_opt->nd_opt_len = optlen >> 3;
  516. bcopy(nonce, (caddr_t)(nd_opt + 1), ND_OPT_NONCE_LEN);
  517. }
  518. ip6->ip6_plen = htons((u_short)icmp6len);
  519. nd_ns->nd_ns_cksum = 0;
  520. nd_ns->nd_ns_cksum =
  521. in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
  522. if (send_sendso_input_hook != NULL) {
  523. mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
  524. sizeof(unsigned short), M_NOWAIT);
  525. if (mtag == NULL)
  526. goto bad;
  527. *(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type;
  528. m_tag_prepend(m, mtag);
  529. }
  530. ip6_output(m, NULL, NULL, (nonce != NULL) ? IPV6_UNSPECSRC : 0,
  531. &im6o, NULL, NULL);
  532. icmp6_ifstat_inc(ifp, ifs6_out_msg);
  533. icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
  534. ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
  535. return;
  536. bad:
  537. m_freem(m);
  538. }
  539. #ifndef BURN_BRIDGES
  540. void
  541. nd6_ns_output(struct ifnet *ifp, const struct in6_addr *saddr6,
  542. const struct in6_addr *daddr6, const struct in6_addr *taddr6,uint8_t *nonce)
  543. {
  544. nd6_ns_output_fib(ifp, saddr6, daddr6, taddr6, nonce, RT_DEFAULT_FIB);
  545. }
  546. #endif
  547. /*
  548. * Neighbor advertisement input handling.
  549. *
  550. * Based on RFC 2461
  551. * Based on RFC 2462 (duplicate address detection)
  552. *
  553. * the following items are not implemented yet:
  554. * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
  555. * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
  556. */
  557. void
  558. nd6_na_input(struct mbuf *m, int off, int icmp6len)
  559. {
  560. struct ifnet *ifp;
  561. struct ip6_hdr *ip6;
  562. struct ifaddr *ifa;
  563. struct llentry *ln;
  564. struct mbuf *chain;
  565. struct nd_neighbor_advert *nd_na;
  566. struct in6_addr daddr6, taddr6;
  567. struct sockaddr_in6 sin6;
  568. union nd_opts ndopts;
  569. u_char linkhdr[LLE_MAX_LINKHDR];
  570. char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
  571. char *lladdr;
  572. size_t linkhdrsize;
  573. int flags, is_override, is_router, is_solicited;
  574. int lladdr_off, lladdrlen, checklink;
  575. NET_EPOCH_ASSERT();
  576. chain = NULL;
  577. ln = NULL;
  578. checklink = 0;
  579. /* RFC 6980: Nodes MUST silently ignore fragments */
  580. if(m->m_flags & M_FRAGMENTED)
  581. goto freeit;
  582. ifp = m->m_pkthdr.rcvif;
  583. ip6 = mtod(m, struct ip6_hdr *);
  584. if (__predict_false(ip6->ip6_hlim != 255)) {
  585. ICMP6STAT_INC(icp6s_invlhlim);
  586. nd6log((LOG_ERR,
  587. "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
  588. ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
  589. ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
  590. goto bad;
  591. }
  592. if (m->m_len < off + icmp6len) {
  593. m = m_pullup(m, off + icmp6len);
  594. if (m == NULL) {
  595. IP6STAT_INC(ip6s_exthdrtoolong);
  596. return;
  597. }
  598. }
  599. ip6 = mtod(m, struct ip6_hdr *);
  600. nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
  601. flags = nd_na->nd_na_flags_reserved;
  602. is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
  603. is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
  604. is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
  605. taddr6 = nd_na->nd_na_target;
  606. if (in6_setscope(&taddr6, ifp, NULL))
  607. goto bad; /* XXX: impossible */
  608. if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
  609. nd6log((LOG_ERR,
  610. "nd6_na_input: invalid target address %s\n",
  611. ip6_sprintf(ip6bufs, &taddr6)));
  612. goto bad;
  613. }
  614. daddr6 = ip6->ip6_dst;
  615. if (IN6_IS_ADDR_MULTICAST(&daddr6))
  616. if (is_solicited) {
  617. nd6log((LOG_ERR,
  618. "nd6_na_input: a solicited adv is multicasted\n"));
  619. goto bad;
  620. }
  621. icmp6len -= sizeof(*nd_na);
  622. nd6_option_init(nd_na + 1, icmp6len, &ndopts);
  623. if (nd6_options(&ndopts) < 0) {
  624. nd6log((LOG_INFO,
  625. "nd6_na_input: invalid ND option, ignored\n"));
  626. /* nd6_options have incremented stats */
  627. goto freeit;
  628. }
  629. lladdr = NULL;
  630. lladdrlen = 0;
  631. if (ndopts.nd_opts_tgt_lladdr) {
  632. lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
  633. lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
  634. }
  635. /*
  636. * This effectively disables the DAD check on a non-master CARP
  637. * address.
  638. */
  639. if (ifp->if_carp)
  640. ifa = (*carp_iamatch6_p)(ifp, &taddr6);
  641. else
  642. ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
  643. /*
  644. * Target address matches one of my interface address.
  645. *
  646. * If my address is tentative, this means that there's somebody
  647. * already using the same address as mine. This indicates DAD failure.
  648. * This is defined in RFC 2462.
  649. *
  650. * Otherwise, process as defined in RFC 2461.
  651. */
  652. if (ifa
  653. && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
  654. nd6_dad_na_input(ifa);
  655. ifa_free(ifa);
  656. goto freeit;
  657. }
  658. /* Just for safety, maybe unnecessary. */
  659. if (ifa) {
  660. ifa_free(ifa);
  661. log(LOG_ERR,
  662. "nd6_na_input: duplicate IP6 address %s\n",
  663. ip6_sprintf(ip6bufs, &taddr6));
  664. goto freeit;
  665. }
  666. if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  667. nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
  668. "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
  669. ifp->if_addrlen, lladdrlen - 2));
  670. goto bad;
  671. }
  672. /*
  673. * If no neighbor cache entry is found, NA SHOULD silently be
  674. * discarded.
  675. */
  676. ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
  677. if (ln == NULL) {
  678. goto freeit;
  679. }
  680. /*
  681. * Do not try to override static entry.
  682. */
  683. if (ln->la_flags & LLE_STATIC)
  684. goto freeit;
  685. if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
  686. /*
  687. * If the link-layer has address, and no lladdr option came,
  688. * discard the packet.
  689. */
  690. if (ifp->if_addrlen && lladdr == NULL) {
  691. goto freeit;
  692. }
  693. /*
  694. * Record link-layer address, and update the state.
  695. */
  696. linkhdrsize = sizeof(linkhdr);
  697. if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
  698. linkhdr, &linkhdrsize, &lladdr_off) != 0)
  699. return;
  700. if (lltable_try_set_entry_addr(ifp, ln, linkhdr, linkhdrsize,
  701. lladdr_off) == 0) {
  702. ln = NULL;
  703. goto freeit;
  704. }
  705. EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
  706. if (is_solicited)
  707. nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE);
  708. else
  709. nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
  710. if ((ln->ln_router = is_router) != 0) {
  711. /*
  712. * This means a router's state has changed from
  713. * non-reachable to probably reachable, and might
  714. * affect the status of associated prefixes..
  715. */
  716. checklink = 1;
  717. }
  718. } else {
  719. int llchange;
  720. /*
  721. * Check if the link-layer address has changed or not.
  722. */
  723. if (lladdr == NULL)
  724. llchange = 0;
  725. else {
  726. if (ln->la_flags & LLE_VALID) {
  727. if (bcmp(lladdr, ln->ll_addr, ifp->if_addrlen))
  728. llchange = 1;
  729. else
  730. llchange = 0;
  731. } else
  732. llchange = 1;
  733. }
  734. /*
  735. * This is VERY complex. Look at it with care.
  736. *
  737. * override solicit lladdr llchange action
  738. * (L: record lladdr)
  739. *
  740. * 0 0 n -- (2c)
  741. * 0 0 y n (2b) L
  742. * 0 0 y y (1) REACHABLE->STALE
  743. * 0 1 n -- (2c) *->REACHABLE
  744. * 0 1 y n (2b) L *->REACHABLE
  745. * 0 1 y y (1) REACHABLE->STALE
  746. * 1 0 n -- (2a)
  747. * 1 0 y n (2a) L
  748. * 1 0 y y (2a) L *->STALE
  749. * 1 1 n -- (2a) *->REACHABLE
  750. * 1 1 y n (2a) L *->REACHABLE
  751. * 1 1 y y (2a) L *->REACHABLE
  752. */
  753. if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
  754. /*
  755. * If state is REACHABLE, make it STALE.
  756. * no other updates should be done.
  757. */
  758. if (ln->ln_state == ND6_LLINFO_REACHABLE)
  759. nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
  760. goto freeit;
  761. } else if (is_override /* (2a) */
  762. || (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
  763. || lladdr == NULL) { /* (2c) */
  764. /*
  765. * Update link-local address, if any.
  766. */
  767. if (lladdr != NULL) {
  768. linkhdrsize = sizeof(linkhdr);
  769. if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
  770. linkhdr, &linkhdrsize, &lladdr_off) != 0)
  771. goto freeit;
  772. if (lltable_try_set_entry_addr(ifp, ln, linkhdr,
  773. linkhdrsize, lladdr_off) == 0) {
  774. ln = NULL;
  775. goto freeit;
  776. }
  777. EVENTHANDLER_INVOKE(lle_event, ln,
  778. LLENTRY_RESOLVED);
  779. }
  780. /*
  781. * If solicited, make the state REACHABLE.
  782. * If not solicited and the link-layer address was
  783. * changed, make it STALE.
  784. */
  785. if (is_solicited)
  786. nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE);
  787. else {
  788. if (lladdr != NULL && llchange)
  789. nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
  790. }
  791. }
  792. if (ln->ln_router && !is_router) {
  793. /*
  794. * The peer dropped the router flag.
  795. * Remove the sender from the Default Router List and
  796. * update the Destination Cache entries.
  797. */
  798. struct ifnet *nd6_ifp;
  799. nd6_ifp = lltable_get_ifp(ln->lle_tbl);
  800. if (!defrouter_remove(&ln->r_l3addr.addr6, nd6_ifp) &&
  801. (ND_IFINFO(nd6_ifp)->flags &
  802. ND6_IFF_ACCEPT_RTADV) != 0)
  803. /*
  804. * Even if the neighbor is not in the default
  805. * router list, the neighbor may be used as a
  806. * next hop for some destinations (e.g. redirect
  807. * case). So we must call rt6_flush explicitly.
  808. */
  809. rt6_flush(&ip6->ip6_src, ifp);
  810. }
  811. ln->ln_router = is_router;
  812. }
  813. /* XXX - QL
  814. * Does this matter?
  815. * rt->rt_flags &= ~RTF_REJECT;
  816. */
  817. ln->la_asked = 0;
  818. if (ln->la_hold != NULL) {
  819. memset(&sin6, 0, sizeof(sin6));
  820. nd6_grab_holdchain(ln, &chain, &sin6);
  821. }
  822. freeit:
  823. if (ln != NULL)
  824. LLE_WUNLOCK(ln);
  825. if (chain != NULL)
  826. nd6_flush_holdchain(ifp, chain, &sin6);
  827. if (checklink)
  828. pfxlist_onlink_check();
  829. m_freem(m);
  830. return;
  831. bad:
  832. if (ln != NULL)
  833. LLE_WUNLOCK(ln);
  834. ICMP6STAT_INC(icp6s_badna);
  835. m_freem(m);
  836. }
  837. /*
  838. * Neighbor advertisement output handling.
  839. *
  840. * Based on RFC 2461
  841. *
  842. * the following items are not implemented yet:
  843. * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
  844. * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
  845. *
  846. * tlladdr - 1 if include target link-layer address
  847. * sdl0 - sockaddr_dl (= proxy NA) or NULL
  848. */
  849. static void
  850. nd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0,
  851. const struct in6_addr *taddr6, u_long flags, int tlladdr,
  852. struct sockaddr *sdl0, u_int fibnum)
  853. {
  854. struct mbuf *m;
  855. struct m_tag *mtag;
  856. struct ip6_hdr *ip6;
  857. struct nd_neighbor_advert *nd_na;
  858. struct ip6_moptions im6o;
  859. struct in6_addr daddr6, dst6, src6;
  860. uint32_t scopeid;
  861. int icmp6len, maxlen, error;
  862. caddr_t mac = NULL;
  863. daddr6 = *daddr6_0; /* make a local copy for modification */
  864. /* estimate the size of message */
  865. maxlen = sizeof(*ip6) + sizeof(*nd_na);
  866. maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
  867. KASSERT(max_linkhdr + maxlen <= MCLBYTES, (
  868. "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
  869. __func__, max_linkhdr, maxlen, MCLBYTES));
  870. if (max_linkhdr + maxlen > MHLEN)
  871. m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
  872. else
  873. m = m_gethdr(M_NOWAIT, MT_DATA);
  874. if (m == NULL)
  875. return;
  876. M_SETFIB(m, fibnum);
  877. if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
  878. m->m_flags |= M_MCAST;
  879. im6o.im6o_multicast_ifp = ifp;
  880. im6o.im6o_multicast_hlim = 255;
  881. im6o.im6o_multicast_loop = 0;
  882. }
  883. icmp6len = sizeof(*nd_na);
  884. m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
  885. m->m_data += max_linkhdr; /* or M_ALIGN() equivalent? */
  886. /* fill neighbor advertisement packet */
  887. ip6 = mtod(m, struct ip6_hdr *);
  888. ip6->ip6_flow = 0;
  889. ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  890. ip6->ip6_vfc |= IPV6_VERSION;
  891. ip6->ip6_nxt = IPPROTO_ICMPV6;
  892. ip6->ip6_hlim = 255;
  893. if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
  894. /* reply to DAD */
  895. daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
  896. daddr6.s6_addr16[1] = 0;
  897. daddr6.s6_addr32[1] = 0;
  898. daddr6.s6_addr32[2] = 0;
  899. daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
  900. if (in6_setscope(&daddr6, ifp, NULL))
  901. goto bad;
  902. flags &= ~ND_NA_FLAG_SOLICITED;
  903. }
  904. ip6->ip6_dst = daddr6;
  905. /*
  906. * Select a source whose scope is the same as that of the dest.
  907. */
  908. in6_splitscope(&daddr6, &dst6, &scopeid);
  909. error = in6_selectsrc_addr(fibnum, &dst6,
  910. scopeid, ifp, &src6, NULL);
  911. if (error) {
  912. char ip6buf[INET6_ADDRSTRLEN];
  913. nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
  914. "determined: dst=%s, error=%d\n",
  915. ip6_sprintf(ip6buf, &daddr6), error));
  916. goto bad;
  917. }
  918. ip6->ip6_src = src6;
  919. nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
  920. nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
  921. nd_na->nd_na_code = 0;
  922. nd_na->nd_na_target = *taddr6;
  923. in6_clearscope(&nd_na->nd_na_target); /* XXX */
  924. /*
  925. * "tlladdr" indicates NS's condition for adding tlladdr or not.
  926. * see nd6_ns_input() for details.
  927. * Basically, if NS packet is sent to unicast/anycast addr,
  928. * target lladdr option SHOULD NOT be included.
  929. */
  930. if (tlladdr) {
  931. /*
  932. * sdl0 != NULL indicates proxy NA. If we do proxy, use
  933. * lladdr in sdl0. If we are not proxying (sending NA for
  934. * my address) use lladdr configured for the interface.
  935. */
  936. if (sdl0 == NULL) {
  937. if (ifp->if_carp)
  938. mac = (*carp_macmatch6_p)(ifp, m, taddr6);
  939. if (mac == NULL)
  940. mac = nd6_ifptomac(ifp);
  941. } else if (sdl0->sa_family == AF_LINK) {
  942. struct sockaddr_dl *sdl;
  943. sdl = (struct sockaddr_dl *)sdl0;
  944. if (sdl->sdl_alen == ifp->if_addrlen)
  945. mac = LLADDR(sdl);
  946. }
  947. }
  948. if (tlladdr && mac) {
  949. int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
  950. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
  951. /* roundup to 8 bytes alignment! */
  952. optlen = (optlen + 7) & ~7;
  953. m->m_pkthdr.len += optlen;
  954. m->m_len += optlen;
  955. icmp6len += optlen;
  956. bzero((caddr_t)nd_opt, optlen);
  957. nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
  958. nd_opt->nd_opt_len = optlen >> 3;
  959. bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
  960. } else
  961. flags &= ~ND_NA_FLAG_OVERRIDE;
  962. ip6->ip6_plen = htons((u_short)icmp6len);
  963. nd_na->nd_na_flags_reserved = flags;
  964. nd_na->nd_na_cksum = 0;
  965. nd_na->nd_na_cksum =
  966. in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
  967. if (send_sendso_input_hook != NULL) {
  968. mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
  969. sizeof(unsigned short), M_NOWAIT);
  970. if (mtag == NULL)
  971. goto bad;
  972. *(unsigned short *)(mtag + 1) = nd_na->nd_na_type;
  973. m_tag_prepend(m, mtag);
  974. }
  975. ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
  976. icmp6_ifstat_inc(ifp, ifs6_out_msg);
  977. icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
  978. ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
  979. return;
  980. bad:
  981. m_freem(m);
  982. }
  983. #ifndef BURN_BRIDGES
  984. void
  985. nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
  986. const struct in6_addr *taddr6, u_long flags, int tlladdr,
  987. struct sockaddr *sdl0)
  988. {
  989. nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0,
  990. RT_DEFAULT_FIB);
  991. }
  992. #endif
  993. caddr_t
  994. nd6_ifptomac(struct ifnet *ifp)
  995. {
  996. switch (ifp->if_type) {
  997. case IFT_ETHER:
  998. case IFT_IEEE1394:
  999. case IFT_L2VLAN:
  1000. case IFT_INFINIBAND:
  1001. case IFT_BRIDGE:
  1002. return IF_LLADDR(ifp);
  1003. default:
  1004. return NULL;
  1005. }
  1006. }
  1007. struct dadq {
  1008. TAILQ_ENTRY(dadq) dad_list;
  1009. struct ifaddr *dad_ifa;
  1010. int dad_count; /* max NS to send */
  1011. int dad_ns_tcount; /* # of trials to send NS */
  1012. int dad_ns_ocount; /* NS sent so far */
  1013. int dad_ns_icount;
  1014. int dad_na_icount;
  1015. int dad_ns_lcount; /* looped back NS */
  1016. int dad_loopbackprobe; /* probing state for loopback detection */
  1017. struct callout dad_timer_ch;
  1018. struct vnet *dad_vnet;
  1019. u_int dad_refcnt;
  1020. #define ND_OPT_NONCE_LEN32 \
  1021. ((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t))
  1022. uint32_t dad_nonce[ND_OPT_NONCE_LEN32];
  1023. bool dad_ondadq; /* on dadq? Protected by DADQ_WLOCK. */
  1024. };
  1025. VNET_DEFINE_STATIC(TAILQ_HEAD(, dadq), dadq);
  1026. VNET_DEFINE_STATIC(struct rwlock, dad_rwlock);
  1027. #define V_dadq VNET(dadq)
  1028. #define V_dad_rwlock VNET(dad_rwlock)
  1029. #define DADQ_RLOCK() rw_rlock(&V_dad_rwlock)
  1030. #define DADQ_RUNLOCK() rw_runlock(&V_dad_rwlock)
  1031. #define DADQ_WLOCK() rw_wlock(&V_dad_rwlock)
  1032. #define DADQ_WUNLOCK() rw_wunlock(&V_dad_rwlock)
  1033. static void
  1034. nd6_dad_add(struct dadq *dp)
  1035. {
  1036. DADQ_WLOCK();
  1037. TAILQ_INSERT_TAIL(&V_dadq, dp, dad_list);
  1038. dp->dad_ondadq = true;
  1039. DADQ_WUNLOCK();
  1040. }
  1041. static void
  1042. nd6_dad_del(struct dadq *dp)
  1043. {
  1044. DADQ_WLOCK();
  1045. if (dp->dad_ondadq) {
  1046. /*
  1047. * Remove dp from the dadq and release the dadq's
  1048. * reference.
  1049. */
  1050. TAILQ_REMOVE(&V_dadq, dp, dad_list);
  1051. dp->dad_ondadq = false;
  1052. DADQ_WUNLOCK();
  1053. nd6_dad_rele(dp);
  1054. } else
  1055. DADQ_WUNLOCK();
  1056. }
  1057. static struct dadq *
  1058. nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *n)
  1059. {
  1060. struct dadq *dp;
  1061. DADQ_RLOCK();
  1062. TAILQ_FOREACH(dp, &V_dadq, dad_list) {
  1063. if (dp->dad_ifa != ifa)
  1064. continue;
  1065. /*
  1066. * Skip if the nonce matches the received one.
  1067. * +2 in the length is required because of type and
  1068. * length fields are included in a header.
  1069. */
  1070. if (n != NULL &&
  1071. n->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 &&
  1072. memcmp(&n->nd_opt_nonce[0], &dp->dad_nonce[0],
  1073. ND_OPT_NONCE_LEN) == 0) {
  1074. dp->dad_ns_lcount++;
  1075. continue;
  1076. }
  1077. refcount_acquire(&dp->dad_refcnt);
  1078. break;
  1079. }
  1080. DADQ_RUNLOCK();
  1081. return (dp);
  1082. }
  1083. static void
  1084. nd6_dad_starttimer(struct dadq *dp, int ticks, int send_ns)
  1085. {
  1086. NET_EPOCH_ASSERT();
  1087. if (send_ns != 0)
  1088. nd6_dad_ns_output(dp);
  1089. callout_reset(&dp->dad_timer_ch, ticks,
  1090. (void (*)(void *))nd6_dad_timer, (void *)dp);
  1091. }
  1092. static void
  1093. nd6_dad_stoptimer(struct dadq *dp)
  1094. {
  1095. callout_drain(&dp->dad_timer_ch);
  1096. }
  1097. static void
  1098. nd6_dad_rele(struct dadq *dp)
  1099. {
  1100. if (refcount_release(&dp->dad_refcnt)) {
  1101. ifa_free(dp->dad_ifa);
  1102. free(dp, M_IP6NDP);
  1103. }
  1104. }
  1105. void
  1106. nd6_dad_init(void)
  1107. {
  1108. rw_init(&V_dad_rwlock, "nd6 DAD queue");
  1109. TAILQ_INIT(&V_dadq);
  1110. }
  1111. /*
  1112. * Start Duplicate Address Detection (DAD) for specified interface address.
  1113. */
  1114. void
  1115. nd6_dad_start(struct ifaddr *ifa, int delay)
  1116. {
  1117. struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
  1118. struct dadq *dp;
  1119. char ip6buf[INET6_ADDRSTRLEN];
  1120. struct epoch_tracker et;
  1121. KASSERT((ia->ia6_flags & IN6_IFF_TENTATIVE) != 0,
  1122. ("starting DAD on non-tentative address %p", ifa));
  1123. /*
  1124. * If we don't need DAD, don't do it.
  1125. * There are several cases:
  1126. * - DAD is disabled globally or on the interface
  1127. * - the interface address is anycast
  1128. */
  1129. if ((ia->ia6_flags & IN6_IFF_ANYCAST) != 0 ||
  1130. V_ip6_dad_count == 0 ||
  1131. (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_NO_DAD) != 0) {
  1132. ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
  1133. return;
  1134. }
  1135. if ((ifa->ifa_ifp->if_flags & IFF_UP) == 0 ||
  1136. (ifa->ifa_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
  1137. (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_IFDISABLED) != 0)
  1138. return;
  1139. if ((dp = nd6_dad_find(ifa, NULL)) != NULL) {
  1140. /*
  1141. * DAD is already in progress. Let the existing entry
  1142. * finish it.
  1143. */
  1144. nd6_dad_rele(dp);
  1145. return;
  1146. }
  1147. dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
  1148. if (dp == NULL) {
  1149. log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
  1150. "%s(%s)\n",
  1151. ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
  1152. ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
  1153. return;
  1154. }
  1155. callout_init(&dp->dad_timer_ch, 0);
  1156. #ifdef VIMAGE
  1157. dp->dad_vnet = curvnet;
  1158. #endif
  1159. nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
  1160. ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
  1161. /*
  1162. * Send NS packet for DAD, ip6_dad_count times.
  1163. * Note that we must delay the first transmission, if this is the
  1164. * first packet to be sent from the interface after interface
  1165. * (re)initialization.
  1166. */
  1167. dp->dad_ifa = ifa;
  1168. ifa_ref(dp->dad_ifa);
  1169. dp->dad_count = V_ip6_dad_count;
  1170. dp->dad_ns_icount = dp->dad_na_icount = 0;
  1171. dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
  1172. dp->dad_ns_lcount = dp->dad_loopbackprobe = 0;
  1173. /* Add this to the dadq and add a reference for the dadq. */
  1174. refcount_init(&dp->dad_refcnt, 1);
  1175. nd6_dad_add(dp);
  1176. NET_EPOCH_ENTER(et);
  1177. nd6_dad_starttimer(dp, delay, 0);
  1178. NET_EPOCH_EXIT(et);
  1179. }
  1180. /*
  1181. * terminate DAD unconditionally. used for address removals.
  1182. */
  1183. void
  1184. nd6_dad_stop(struct ifaddr *ifa)
  1185. {
  1186. struct dadq *dp;
  1187. dp = nd6_dad_find(ifa, NULL);
  1188. if (!dp) {
  1189. /* DAD wasn't started yet */
  1190. return;
  1191. }
  1192. nd6_dad_stoptimer(dp);
  1193. nd6_dad_del(dp);
  1194. /* Release this function's reference, acquired by nd6_dad_find(). */
  1195. nd6_dad_rele(dp);
  1196. }
  1197. static void
  1198. nd6_dad_timer(struct dadq *dp)
  1199. {
  1200. CURVNET_SET(dp->dad_vnet);
  1201. struct ifaddr *ifa = dp->dad_ifa;
  1202. struct ifnet *ifp = dp->dad_ifa->ifa_ifp;
  1203. struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
  1204. char ip6buf[INET6_ADDRSTRLEN];
  1205. struct epoch_tracker et;
  1206. KASSERT(ia != NULL, ("DAD entry %p with no address", dp));
  1207. NET_EPOCH_ENTER(et);
  1208. if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
  1209. /* Do not need DAD for ifdisabled interface. */
  1210. log(LOG_ERR, "nd6_dad_timer: cancel DAD on %s because of "
  1211. "ND6_IFF_IFDISABLED.\n", ifp->if_xname);
  1212. goto err;
  1213. }
  1214. if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
  1215. log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
  1216. "%s(%s)\n",
  1217. ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
  1218. ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
  1219. goto err;
  1220. }
  1221. if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
  1222. log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
  1223. "%s(%s)\n",
  1224. ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
  1225. ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
  1226. goto err;
  1227. }
  1228. /* Stop DAD if the interface is down even after dad_maxtry attempts. */
  1229. if ((dp->dad_ns_tcount > V_dad_maxtry) &&
  1230. (((ifp->if_flags & IFF_UP) == 0) ||
  1231. ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0))) {
  1232. nd6log((LOG_INFO, "%s: could not run DAD "
  1233. "because the interface was down or not running.\n",
  1234. if_name(ifa->ifa_ifp)));
  1235. goto err;
  1236. }
  1237. /* Need more checks? */
  1238. if (dp->dad_ns_ocount < dp->dad_count) {
  1239. /*
  1240. * We have more NS to go. Send NS packet for DAD.
  1241. */
  1242. nd6_dad_starttimer(dp,
  1243. (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000, 1);
  1244. goto done;
  1245. } else {
  1246. /*
  1247. * We have transmitted sufficient number of DAD packets.
  1248. * See what we've got.
  1249. */
  1250. if (dp->dad_ns_icount > 0 || dp->dad_na_icount > 0)
  1251. /* We've seen NS or NA, means DAD has failed. */
  1252. nd6_dad_duplicated(ifa, dp);
  1253. else if (V_dad_enhanced != 0 &&
  1254. dp->dad_ns_lcount > 0 &&
  1255. dp->dad_ns_lcount > dp->dad_loopbackprobe) {
  1256. /*
  1257. * Sec. 4.1 in RFC 7527 requires transmission of
  1258. * additional probes until the loopback condition
  1259. * becomes clear when a looped back probe is detected.
  1260. */
  1261. log(LOG_ERR, "%s: a looped back NS message is "
  1262. "detected during DAD for %s. "
  1263. "Another DAD probes are being sent.\n",
  1264. if_name(ifa->ifa_ifp),
  1265. ip6_sprintf(ip6buf, IFA_IN6(ifa)));
  1266. dp->dad_loopbackprobe = dp->dad_ns_lcount;
  1267. /*
  1268. * Send an NS immediately and increase dad_count by
  1269. * V_nd6_mmaxtries - 1.
  1270. */
  1271. dp->dad_count =
  1272. dp->dad_ns_ocount + V_nd6_mmaxtries - 1;
  1273. nd6_dad_starttimer(dp,
  1274. (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000,
  1275. 1);
  1276. goto done;
  1277. } else {
  1278. /*
  1279. * We are done with DAD. No NA came, no NS came.
  1280. * No duplicate address found. Check IFDISABLED flag
  1281. * again in case that it is changed between the
  1282. * beginning of this function and here.
  1283. */
  1284. if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) == 0)
  1285. ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
  1286. nd6log((LOG_DEBUG,
  1287. "%s: DAD complete for %s - no duplicates found\n",
  1288. if_name(ifa->ifa_ifp),
  1289. ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
  1290. if (dp->dad_ns_lcount > 0)
  1291. log(LOG_ERR, "%s: DAD completed while "
  1292. "a looped back NS message is detected "
  1293. "during DAD for %s.\n",
  1294. if_name(ifa->ifa_ifp),
  1295. ip6_sprintf(ip6buf, IFA_IN6(ifa)));
  1296. }
  1297. }
  1298. err:
  1299. nd6_dad_del(dp);
  1300. done:
  1301. NET_EPOCH_EXIT(et);
  1302. CURVNET_RESTORE();
  1303. }
  1304. static void
  1305. nd6_dad_duplicated(struct ifaddr *ifa, struct dadq *dp)
  1306. {
  1307. struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
  1308. struct ifnet *ifp;
  1309. char ip6buf[INET6_ADDRSTRLEN];
  1310. log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
  1311. "NS in/out/loopback=%d/%d/%d, NA in=%d\n",
  1312. if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
  1313. dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount,
  1314. dp->dad_na_icount);
  1315. ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
  1316. ia->ia6_flags |= IN6_IFF_DUPLICATED;
  1317. ifp = ifa->ifa_ifp;
  1318. log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
  1319. if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
  1320. log(LOG_ERR, "%s: manual intervention required\n",
  1321. if_name(ifp));
  1322. /*
  1323. * If the address is a link-local address formed from an interface
  1324. * identifier based on the hardware address which is supposed to be
  1325. * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
  1326. * operation on the interface SHOULD be disabled.
  1327. * [RFC 4862, Section 5.4.5]
  1328. */
  1329. if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
  1330. struct in6_addr in6;
  1331. /*
  1332. * To avoid over-reaction, we only apply this logic when we are
  1333. * very sure that hardware addresses are supposed to be unique.
  1334. */
  1335. switch (ifp->if_type) {
  1336. case IFT_ETHER:
  1337. case IFT_ATM:
  1338. case IFT_IEEE1394:
  1339. case IFT_INFINIBAND:
  1340. in6 = ia->ia_addr.sin6_addr;
  1341. if (in6_get_hw_ifid(ifp, &in6) == 0 &&
  1342. IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
  1343. ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
  1344. log(LOG_ERR, "%s: possible hardware address "
  1345. "duplication detected, disable IPv6\n",
  1346. if_name(ifp));
  1347. }
  1348. break;
  1349. }
  1350. }
  1351. }
  1352. static void
  1353. nd6_dad_ns_output(struct dadq *dp)
  1354. {
  1355. struct in6_ifaddr *ia = (struct in6_ifaddr *)dp->dad_ifa;
  1356. struct ifnet *ifp = dp->dad_ifa->ifa_ifp;
  1357. int i;
  1358. dp->dad_ns_tcount++;
  1359. if ((ifp->if_flags & IFF_UP) == 0) {
  1360. return;
  1361. }
  1362. if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
  1363. return;
  1364. }
  1365. dp->dad_ns_ocount++;
  1366. if (V_dad_enhanced != 0) {
  1367. for (i = 0; i < ND_OPT_NONCE_LEN32; i++)
  1368. dp->dad_nonce[i] = arc4random();
  1369. /*
  1370. * XXXHRS: Note that in the case that
  1371. * DupAddrDetectTransmits > 1, multiple NS messages with
  1372. * different nonces can be looped back in an unexpected
  1373. * order. The current implementation recognizes only
  1374. * the latest nonce on the sender side. Practically it
  1375. * should work well in almost all cases.
  1376. */
  1377. }
  1378. nd6_ns_output(ifp, NULL, NULL, &ia->ia_addr.sin6_addr,
  1379. (uint8_t *)&dp->dad_nonce[0]);
  1380. }
  1381. static void
  1382. nd6_dad_ns_input(struct ifaddr *ifa, struct nd_opt_nonce *ndopt_nonce)
  1383. {
  1384. struct dadq *dp;
  1385. if (ifa == NULL)
  1386. panic("ifa == NULL in nd6_dad_ns_input");
  1387. /* Ignore Nonce option when Enhanced DAD is disabled. */
  1388. if (V_dad_enhanced == 0)
  1389. ndopt_nonce = NULL;
  1390. dp = nd6_dad_find(ifa, ndopt_nonce);
  1391. if (dp == NULL)
  1392. return;
  1393. dp->dad_ns_icount++;
  1394. nd6_dad_rele(dp);
  1395. }
  1396. static void
  1397. nd6_dad_na_input(struct ifaddr *ifa)
  1398. {
  1399. struct dadq *dp;
  1400. if (ifa == NULL)
  1401. panic("ifa == NULL in nd6_dad_na_input");
  1402. dp = nd6_dad_find(ifa, NULL);
  1403. if (dp != NULL) {
  1404. dp->dad_na_icount++;
  1405. nd6_dad_rele(dp);
  1406. }
  1407. }