nd6_nbr.c 44 KB

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