nd6_nbr.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /* $OpenBSD: nd6_nbr.c,v 1.91 2015/07/16 15:28:38 mpi Exp $ */
  2. /* $KAME: nd6_nbr.c,v 1.61 2001/02/10 16:06:14 jinmei Exp $ */
  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. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/malloc.h>
  34. #include <sys/mbuf.h>
  35. #include <sys/socket.h>
  36. #include <sys/sockio.h>
  37. #include <sys/time.h>
  38. #include <sys/kernel.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/syslog.h>
  41. #include <sys/queue.h>
  42. #include <sys/timeout.h>
  43. #include <net/if.h>
  44. #include <net/if_var.h>
  45. #include <net/if_types.h>
  46. #include <net/if_dl.h>
  47. #include <net/route.h>
  48. #include <netinet/in.h>
  49. #include <netinet6/in6_var.h>
  50. #include <netinet/ip6.h>
  51. #include <netinet6/ip6_var.h>
  52. #include <netinet6/nd6.h>
  53. #include <netinet/icmp6.h>
  54. #include "carp.h"
  55. #if NCARP > 0
  56. #include <netinet/ip_carp.h>
  57. #endif
  58. #define SDL(s) ((struct sockaddr_dl *)s)
  59. TAILQ_HEAD(dadq_head, dadq);
  60. struct dadq {
  61. TAILQ_ENTRY(dadq) dad_list;
  62. struct ifaddr *dad_ifa;
  63. int dad_count; /* max NS to send */
  64. int dad_ns_tcount; /* # of trials to send NS */
  65. int dad_ns_ocount; /* NS sent so far */
  66. int dad_ns_icount;
  67. int dad_na_icount;
  68. struct timeout dad_timer_ch;
  69. };
  70. struct dadq *nd6_dad_find(struct ifaddr *);
  71. void nd6_dad_starttimer(struct dadq *, int);
  72. void nd6_dad_stoptimer(struct dadq *);
  73. void nd6_dad_timer(struct ifaddr *);
  74. void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
  75. void nd6_dad_ns_input(struct ifaddr *);
  76. void nd6_dad_duplicated(struct dadq *);
  77. static int dad_maxtry = 15; /* max # of *tries* to transmit DAD packet */
  78. /*
  79. * Input an Neighbor Solicitation Message.
  80. *
  81. * Based on RFC 2461
  82. * Based on RFC 2462 (duplicated address detection)
  83. */
  84. void
  85. nd6_ns_input(struct mbuf *m, int off, int icmp6len)
  86. {
  87. struct ifnet *ifp;
  88. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  89. struct nd_neighbor_solicit *nd_ns;
  90. struct in6_addr saddr6 = ip6->ip6_src;
  91. struct in6_addr daddr6 = ip6->ip6_dst;
  92. struct in6_addr taddr6;
  93. struct in6_addr myaddr6;
  94. char *lladdr = NULL;
  95. struct ifaddr *ifa = NULL;
  96. int lladdrlen = 0;
  97. int anycast = 0, proxy = 0, tentative = 0;
  98. int router = ip6_forwarding;
  99. int tlladdr;
  100. union nd_opts ndopts;
  101. struct sockaddr_dl *proxydl = NULL;
  102. char addr[INET6_ADDRSTRLEN], addr0[INET6_ADDRSTRLEN];
  103. ifp = if_get(m->m_pkthdr.ph_ifidx);
  104. if (ifp == NULL)
  105. goto freeit;
  106. IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
  107. if (nd_ns == NULL) {
  108. icmp6stat.icp6s_tooshort++;
  109. return;
  110. }
  111. ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
  112. taddr6 = nd_ns->nd_ns_target;
  113. if (ip6->ip6_hlim != 255) {
  114. nd6log((LOG_ERR,
  115. "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
  116. ip6->ip6_hlim,
  117. inet_ntop(AF_INET6, &ip6->ip6_src, addr, sizeof(addr)),
  118. inet_ntop(AF_INET6, &ip6->ip6_dst, addr0, sizeof(addr0)),
  119. ifp->if_xname));
  120. goto bad;
  121. }
  122. if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
  123. /* dst has to be solicited node multicast address. */
  124. /* don't check ifindex portion */
  125. if (daddr6.s6_addr16[0] == __IPV6_ADDR_INT16_MLL &&
  126. daddr6.s6_addr32[1] == 0 &&
  127. daddr6.s6_addr32[2] == __IPV6_ADDR_INT32_ONE &&
  128. daddr6.s6_addr8[12] == 0xff) {
  129. ; /*good*/
  130. } else {
  131. nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
  132. "(wrong ip6 dst)\n"));
  133. goto bad;
  134. }
  135. } else {
  136. /*
  137. * Make sure the source address is from a neighbor's address.
  138. */
  139. if (!in6_ifpprefix(ifp, &saddr6)) {
  140. nd6log((LOG_INFO, "nd6_ns_input: "
  141. "NS packet from non-neighbor\n"));
  142. goto bad;
  143. }
  144. }
  145. if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
  146. nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
  147. goto bad;
  148. }
  149. if (IN6_IS_SCOPE_EMBED(&taddr6))
  150. taddr6.s6_addr16[1] = htons(ifp->if_index);
  151. icmp6len -= sizeof(*nd_ns);
  152. nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
  153. if (nd6_options(&ndopts) < 0) {
  154. nd6log((LOG_INFO,
  155. "nd6_ns_input: invalid ND option, ignored\n"));
  156. /* nd6_options have incremented stats */
  157. goto freeit;
  158. }
  159. if (ndopts.nd_opts_src_lladdr) {
  160. lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
  161. lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
  162. }
  163. if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
  164. nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
  165. "(link-layer address option)\n"));
  166. goto bad;
  167. }
  168. /*
  169. * Attaching target link-layer address to the NA?
  170. * (RFC 2461 7.2.4)
  171. *
  172. * NS IP dst is unicast/anycast MUST NOT add
  173. * NS IP dst is solicited-node multicast MUST add
  174. *
  175. * In implementation, we add target link-layer address by default.
  176. * We do not add one in MUST NOT cases.
  177. */
  178. #if 0 /* too much! */
  179. ifa = &in6ifa_ifpwithaddr(ifp, &daddr6)->ia_ifa;
  180. if (ifa && (ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST))
  181. tlladdr = 0;
  182. else
  183. #endif
  184. if (!IN6_IS_ADDR_MULTICAST(&daddr6))
  185. tlladdr = 0;
  186. else
  187. tlladdr = 1;
  188. /*
  189. * Target address (taddr6) must be either:
  190. * (1) Valid unicast/anycast address for my receiving interface,
  191. * (2) Unicast address for which I'm offering proxy service, or
  192. * (3) "tentative" address on which DAD is being performed.
  193. */
  194. /* (1) and (3) check. */
  195. ifa = &in6ifa_ifpwithaddr(ifp, &taddr6)->ia_ifa;
  196. #if NCARP > 0
  197. if (ifp->if_type == IFT_CARP && ifa &&
  198. !carp_iamatch6(ifp, lladdr, &proxydl))
  199. ifa = NULL;
  200. #endif
  201. /* (2) check. */
  202. if (!ifa) {
  203. struct rtentry *rt;
  204. struct sockaddr_in6 tsin6;
  205. bzero(&tsin6, sizeof tsin6);
  206. tsin6.sin6_len = sizeof(struct sockaddr_in6);
  207. tsin6.sin6_family = AF_INET6;
  208. tsin6.sin6_addr = taddr6;
  209. rt = rtalloc(sin6tosa(&tsin6), 0, m->m_pkthdr.ph_rtableid);
  210. if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
  211. rt->rt_gateway->sa_family == AF_LINK) {
  212. /*
  213. * proxy NDP for single entry
  214. */
  215. ifa = &in6ifa_ifpforlinklocal(ifp,
  216. IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)->ia_ifa;
  217. if (ifa) {
  218. proxy = 1;
  219. proxydl = SDL(rt->rt_gateway);
  220. router = 0; /* XXX */
  221. }
  222. }
  223. if (rt)
  224. rtfree(rt);
  225. }
  226. if (!ifa) {
  227. /*
  228. * We've got an NS packet, and we don't have that address
  229. * assigned for us. We MUST silently ignore it.
  230. * See RFC2461 7.2.3.
  231. */
  232. goto freeit;
  233. }
  234. myaddr6 = *IFA_IN6(ifa);
  235. anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
  236. tentative = ifatoia6(ifa)->ia6_flags & IN6_IFF_TENTATIVE;
  237. if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DUPLICATED)
  238. goto freeit;
  239. if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  240. nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
  241. "(if %d, NS packet %d)\n",
  242. inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)),
  243. ifp->if_addrlen, lladdrlen - 2));
  244. goto bad;
  245. }
  246. if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
  247. log(LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
  248. inet_ntop(AF_INET6, &saddr6, addr, sizeof(addr)));
  249. goto freeit;
  250. }
  251. /*
  252. * We have neighbor solicitation packet, with target address equals to
  253. * one of my tentative address.
  254. *
  255. * src addr how to process?
  256. * --- ---
  257. * multicast of course, invalid (rejected in ip6_input)
  258. * unicast somebody is doing address resolution -> ignore
  259. * unspec dup address detection
  260. *
  261. * The processing is defined in RFC 2462.
  262. */
  263. if (tentative) {
  264. /*
  265. * If source address is unspecified address, it is for
  266. * duplicated address detection.
  267. *
  268. * If not, the packet is for address resolution;
  269. * silently ignore it.
  270. */
  271. if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
  272. nd6_dad_ns_input(ifa);
  273. goto freeit;
  274. }
  275. /*
  276. * If the source address is unspecified address, entries must not
  277. * be created or updated.
  278. * It looks that sender is performing DAD. Output NA toward
  279. * all-node multicast address, to tell the sender that I'm using
  280. * the address.
  281. * S bit ("solicited") must be zero.
  282. */
  283. if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
  284. saddr6 = in6addr_linklocal_allnodes;
  285. saddr6.s6_addr16[1] = htons(ifp->if_index);
  286. nd6_na_output(ifp, &saddr6, &taddr6,
  287. ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
  288. (router ? ND_NA_FLAG_ROUTER : 0),
  289. tlladdr, (struct sockaddr *)proxydl);
  290. goto freeit;
  291. }
  292. nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
  293. nd6_na_output(ifp, &saddr6, &taddr6,
  294. ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
  295. (router ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
  296. tlladdr, (struct sockaddr *)proxydl);
  297. freeit:
  298. m_freem(m);
  299. return;
  300. bad:
  301. nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
  302. inet_ntop(AF_INET6, &saddr6, addr, sizeof(addr))));
  303. nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
  304. inet_ntop(AF_INET6, &daddr6, addr, sizeof(addr))));
  305. nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
  306. inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr))));
  307. icmp6stat.icp6s_badns++;
  308. m_freem(m);
  309. }
  310. /*
  311. * Output an Neighbor Solicitation Message. Caller specifies:
  312. * - ICMP6 header source IP6 address
  313. * - ND6 header target IP6 address
  314. * - ND6 header source datalink address
  315. *
  316. * Based on RFC 2461
  317. * Based on RFC 2462 (duplicated address detection)
  318. *
  319. * ln - for source address determination
  320. * dad - duplicated address detection
  321. */
  322. void
  323. nd6_ns_output(struct ifnet *ifp, struct in6_addr *daddr6,
  324. struct in6_addr *taddr6, struct llinfo_nd6 *ln, int dad)
  325. {
  326. struct mbuf *m;
  327. struct ip6_hdr *ip6;
  328. struct nd_neighbor_solicit *nd_ns;
  329. struct sockaddr_in6 src_sa, dst_sa;
  330. struct ip6_moptions im6o;
  331. int icmp6len;
  332. int maxlen;
  333. caddr_t mac;
  334. struct route_in6 ro;
  335. bzero(&ro, sizeof(ro));
  336. ro.ro_tableid = ifp->if_rdomain;
  337. if (IN6_IS_ADDR_MULTICAST(taddr6))
  338. return;
  339. /* estimate the size of message */
  340. maxlen = sizeof(*ip6) + sizeof(*nd_ns);
  341. maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
  342. #ifdef DIAGNOSTIC
  343. if (max_linkhdr + maxlen >= MCLBYTES) {
  344. printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
  345. "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
  346. panic("nd6_ns_output: insufficient MCLBYTES");
  347. /* NOTREACHED */
  348. }
  349. #endif
  350. MGETHDR(m, M_DONTWAIT, MT_DATA);
  351. if (m && max_linkhdr + maxlen >= MHLEN) {
  352. MCLGET(m, M_DONTWAIT);
  353. if ((m->m_flags & M_EXT) == 0) {
  354. m_free(m);
  355. m = NULL;
  356. }
  357. }
  358. if (m == NULL)
  359. return;
  360. m->m_pkthdr.ph_ifidx = 0;
  361. m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
  362. if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
  363. m->m_flags |= M_MCAST;
  364. im6o.im6o_ifidx = ifp->if_index;
  365. im6o.im6o_hlim = 255;
  366. im6o.im6o_loop = 0;
  367. }
  368. icmp6len = sizeof(*nd_ns);
  369. m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
  370. m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
  371. /* fill neighbor solicitation packet */
  372. ip6 = mtod(m, struct ip6_hdr *);
  373. ip6->ip6_flow = 0;
  374. ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  375. ip6->ip6_vfc |= IPV6_VERSION;
  376. /* ip6->ip6_plen will be set later */
  377. ip6->ip6_nxt = IPPROTO_ICMPV6;
  378. ip6->ip6_hlim = 255;
  379. /* determine the source and destination addresses */
  380. bzero(&src_sa, sizeof(src_sa));
  381. bzero(&dst_sa, sizeof(dst_sa));
  382. src_sa.sin6_family = dst_sa.sin6_family = AF_INET6;
  383. src_sa.sin6_len = dst_sa.sin6_len = sizeof(struct sockaddr_in6);
  384. if (daddr6)
  385. dst_sa.sin6_addr = *daddr6;
  386. else {
  387. dst_sa.sin6_addr.s6_addr16[0] = __IPV6_ADDR_INT16_MLL;
  388. dst_sa.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
  389. dst_sa.sin6_addr.s6_addr32[1] = 0;
  390. dst_sa.sin6_addr.s6_addr32[2] = __IPV6_ADDR_INT32_ONE;
  391. dst_sa.sin6_addr.s6_addr32[3] = taddr6->s6_addr32[3];
  392. dst_sa.sin6_addr.s6_addr8[12] = 0xff;
  393. }
  394. ip6->ip6_dst = dst_sa.sin6_addr;
  395. if (!dad) {
  396. /*
  397. * RFC2461 7.2.2:
  398. * "If the source address of the packet prompting the
  399. * solicitation is the same as one of the addresses assigned
  400. * to the outgoing interface, that address SHOULD be placed
  401. * in the IP Source Address of the outgoing solicitation.
  402. * Otherwise, any one of the addresses assigned to the
  403. * interface should be used."
  404. *
  405. * We use the source address for the prompting packet
  406. * (saddr6), if:
  407. * - saddr6 is given from the caller (by giving "ln"), and
  408. * - saddr6 belongs to the outgoing interface.
  409. * Otherwise, we perform the source address selection as usual.
  410. */
  411. struct ip6_hdr *hip6; /* hold ip6 */
  412. struct in6_addr *saddr6;
  413. if (ln && ln->ln_hold) {
  414. hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
  415. /* XXX pullup? */
  416. if (sizeof(*hip6) < ln->ln_hold->m_len)
  417. saddr6 = &hip6->ip6_src;
  418. else
  419. saddr6 = NULL;
  420. } else
  421. saddr6 = NULL;
  422. if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
  423. src_sa.sin6_addr = *saddr6;
  424. else {
  425. struct in6_addr *src0;
  426. int error;
  427. bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
  428. error = in6_selectsrc(&src0, &dst_sa, NULL, NULL, &ro,
  429. NULL, m->m_pkthdr.ph_rtableid);
  430. if (error) {
  431. char addr[INET6_ADDRSTRLEN];
  432. nd6log((LOG_DEBUG,
  433. "nd6_ns_output: source can't be "
  434. "determined: dst=%s, error=%d\n",
  435. inet_ntop(AF_INET6, &dst_sa.sin6_addr,
  436. addr, sizeof(addr)),
  437. error));
  438. goto bad;
  439. }
  440. src_sa.sin6_addr = *src0;
  441. }
  442. } else {
  443. /*
  444. * Source address for DAD packet must always be IPv6
  445. * unspecified address. (0::0)
  446. * We actually don't have to 0-clear the address (we did it
  447. * above), but we do so here explicitly to make the intention
  448. * clearer.
  449. */
  450. bzero(&src_sa.sin6_addr, sizeof(src_sa.sin6_addr));
  451. }
  452. ip6->ip6_src = src_sa.sin6_addr;
  453. nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
  454. nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
  455. nd_ns->nd_ns_code = 0;
  456. nd_ns->nd_ns_reserved = 0;
  457. nd_ns->nd_ns_target = *taddr6;
  458. if (IN6_IS_SCOPE_EMBED(&nd_ns->nd_ns_target))
  459. nd_ns->nd_ns_target.s6_addr16[1] = 0;
  460. /*
  461. * Add source link-layer address option.
  462. *
  463. * spec implementation
  464. * --- ---
  465. * DAD packet MUST NOT do not add the option
  466. * there's no link layer address:
  467. * impossible do not add the option
  468. * there's link layer address:
  469. * Multicast NS MUST add one add the option
  470. * Unicast NS SHOULD add one add the option
  471. */
  472. if (!dad && (mac = nd6_ifptomac(ifp))) {
  473. int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
  474. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
  475. /* 8 byte alignments... */
  476. optlen = (optlen + 7) & ~7;
  477. m->m_pkthdr.len += optlen;
  478. m->m_len += optlen;
  479. icmp6len += optlen;
  480. bzero((caddr_t)nd_opt, optlen);
  481. nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
  482. nd_opt->nd_opt_len = optlen >> 3;
  483. bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
  484. }
  485. ip6->ip6_plen = htons((u_short)icmp6len);
  486. nd_ns->nd_ns_cksum = 0;
  487. m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
  488. ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
  489. icmp6_ifstat_inc(ifp, ifs6_out_msg);
  490. icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
  491. icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
  492. if (ro.ro_rt) { /* we don't cache this route. */
  493. rtfree(ro.ro_rt);
  494. }
  495. return;
  496. bad:
  497. if (ro.ro_rt) {
  498. rtfree(ro.ro_rt);
  499. }
  500. m_freem(m);
  501. return;
  502. }
  503. /*
  504. * Neighbor advertisement input handling.
  505. *
  506. * Based on RFC 2461
  507. * Based on RFC 2462 (duplicated address detection)
  508. *
  509. * the following items are not implemented yet:
  510. * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
  511. * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
  512. */
  513. void
  514. nd6_na_input(struct mbuf *m, int off, int icmp6len)
  515. {
  516. struct ifnet *ifp;
  517. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  518. struct nd_neighbor_advert *nd_na;
  519. struct in6_addr saddr6 = ip6->ip6_src;
  520. struct in6_addr daddr6 = ip6->ip6_dst;
  521. struct in6_addr taddr6;
  522. int flags;
  523. int is_router;
  524. int is_solicited;
  525. int is_override;
  526. char *lladdr = NULL;
  527. int lladdrlen = 0;
  528. struct ifaddr *ifa;
  529. struct llinfo_nd6 *ln;
  530. struct rtentry *rt;
  531. struct sockaddr_dl *sdl;
  532. union nd_opts ndopts;
  533. char addr[INET6_ADDRSTRLEN], addr0[INET6_ADDRSTRLEN];
  534. ifp = if_get(m->m_pkthdr.ph_ifidx);
  535. if (ifp == NULL)
  536. goto freeit;
  537. if (ip6->ip6_hlim != 255) {
  538. nd6log((LOG_ERR,
  539. "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
  540. ip6->ip6_hlim,
  541. inet_ntop(AF_INET6, &ip6->ip6_src, addr, sizeof(addr)),
  542. inet_ntop(AF_INET6, &ip6->ip6_dst, addr0, sizeof(addr0)),
  543. ifp->if_xname));
  544. goto bad;
  545. }
  546. IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
  547. if (nd_na == NULL) {
  548. icmp6stat.icp6s_tooshort++;
  549. return;
  550. }
  551. taddr6 = nd_na->nd_na_target;
  552. flags = nd_na->nd_na_flags_reserved;
  553. is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
  554. is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
  555. is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
  556. if (IN6_IS_SCOPE_EMBED(&taddr6))
  557. taddr6.s6_addr16[1] = htons(ifp->if_index);
  558. if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
  559. nd6log((LOG_ERR,
  560. "nd6_na_input: invalid target address %s\n",
  561. inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr))));
  562. goto bad;
  563. }
  564. if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
  565. nd6log((LOG_ERR,
  566. "nd6_na_input: a solicited adv is multicasted\n"));
  567. goto bad;
  568. }
  569. icmp6len -= sizeof(*nd_na);
  570. nd6_option_init(nd_na + 1, icmp6len, &ndopts);
  571. if (nd6_options(&ndopts) < 0) {
  572. nd6log((LOG_INFO,
  573. "nd6_na_input: invalid ND option, ignored\n"));
  574. /* nd6_options have incremented stats */
  575. goto freeit;
  576. }
  577. if (IN6_IS_ADDR_MULTICAST(&daddr6) && !ndopts.nd_opts_tgt_lladdr) {
  578. nd6log((LOG_INFO,
  579. "nd6_na_input: multicast adv without TLLA\n"));
  580. goto bad;
  581. }
  582. if (ndopts.nd_opts_tgt_lladdr) {
  583. lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
  584. lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
  585. }
  586. ifa = &in6ifa_ifpwithaddr(ifp, &taddr6)->ia_ifa;
  587. /*
  588. * Target address matches one of my interface address.
  589. *
  590. * If my address is tentative, this means that there's somebody
  591. * already using the same address as mine. This indicates DAD failure.
  592. * This is defined in RFC 2462.
  593. *
  594. * Otherwise, process as defined in RFC 2461.
  595. */
  596. if (ifa && (ifatoia6(ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
  597. struct dadq *dp;
  598. dp = nd6_dad_find(ifa);
  599. if (dp) {
  600. dp->dad_na_icount++;
  601. /* remove the address. */
  602. nd6_dad_duplicated(dp);
  603. }
  604. goto freeit;
  605. }
  606. if (ifa) {
  607. #if NCARP > 0
  608. struct sockaddr_dl *proxydl = NULL;
  609. /*
  610. * Ignore NAs silently for carp addresses if we're not
  611. * the CARP master.
  612. */
  613. if (ifp->if_type == IFT_CARP &&
  614. !carp_iamatch6(ifp, lladdr, &proxydl))
  615. goto freeit;
  616. #endif
  617. log(LOG_ERR,
  618. "nd6_na_input: duplicate IP6 address %s\n",
  619. inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)));
  620. goto freeit;
  621. }
  622. /*
  623. * Make sure the source address is from a neighbor's address.
  624. */
  625. if (!in6_ifpprefix(ifp, &saddr6)) {
  626. nd6log((LOG_INFO, "nd6_na_input: "
  627. "ND packet from non-neighbor\n"));
  628. goto bad;
  629. }
  630. if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  631. nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
  632. "(if %d, NA packet %d)\n",
  633. inet_ntop(AF_INET6, &taddr6, addr, sizeof(addr)),
  634. ifp->if_addrlen, lladdrlen - 2));
  635. goto bad;
  636. }
  637. /*
  638. * If no neighbor cache entry is found, NA SHOULD silently be
  639. * discarded.
  640. */
  641. rt = nd6_lookup(&taddr6, 0, ifp, ifp->if_rdomain);
  642. if ((rt == NULL) ||
  643. ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
  644. ((sdl = SDL(rt->rt_gateway)) == NULL))
  645. goto freeit;
  646. if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
  647. /*
  648. * If the link-layer has address, and no lladdr option came,
  649. * discard the packet.
  650. */
  651. if (ifp->if_addrlen && !lladdr)
  652. goto freeit;
  653. /*
  654. * Record link-layer address, and update the state.
  655. */
  656. sdl->sdl_alen = ifp->if_addrlen;
  657. bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
  658. if (is_solicited) {
  659. ln->ln_state = ND6_LLINFO_REACHABLE;
  660. ln->ln_byhint = 0;
  661. if (!ND6_LLINFO_PERMANENT(ln)) {
  662. nd6_llinfo_settimer(ln,
  663. (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
  664. }
  665. } else {
  666. ln->ln_state = ND6_LLINFO_STALE;
  667. nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  668. }
  669. if ((ln->ln_router = is_router) != 0) {
  670. /*
  671. * This means a router's state has changed from
  672. * non-reachable to probably reachable, and might
  673. * affect the status of associated prefixes..
  674. */
  675. pfxlist_onlink_check();
  676. if ((rt->rt_flags & RTF_LLINFO) == 0)
  677. goto freeit; /* ln is gone */
  678. }
  679. } else {
  680. int llchange;
  681. /*
  682. * Check if the link-layer address has changed or not.
  683. */
  684. if (!lladdr)
  685. llchange = 0;
  686. else {
  687. if (sdl->sdl_alen) {
  688. if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
  689. llchange = 1;
  690. else
  691. llchange = 0;
  692. } else
  693. llchange = 1;
  694. }
  695. /*
  696. * This is VERY complex. Look at it with care.
  697. *
  698. * override solicit lladdr llchange action
  699. * (L: record lladdr)
  700. *
  701. * 0 0 n -- (2c)
  702. * 0 0 y n (2b) L
  703. * 0 0 y y (1) REACHABLE->STALE
  704. * 0 1 n -- (2c) *->REACHABLE
  705. * 0 1 y n (2b) L *->REACHABLE
  706. * 0 1 y y (1) REACHABLE->STALE
  707. * 1 0 n -- (2a)
  708. * 1 0 y n (2a) L
  709. * 1 0 y y (2a) L *->STALE
  710. * 1 1 n -- (2a) *->REACHABLE
  711. * 1 1 y n (2a) L *->REACHABLE
  712. * 1 1 y y (2a) L *->REACHABLE
  713. */
  714. if (!is_override && (lladdr && llchange)) { /* (1) */
  715. /*
  716. * If state is REACHABLE, make it STALE.
  717. * no other updates should be done.
  718. */
  719. if (ln->ln_state == ND6_LLINFO_REACHABLE) {
  720. ln->ln_state = ND6_LLINFO_STALE;
  721. nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  722. }
  723. goto freeit;
  724. } else if (is_override /* (2a) */
  725. || (!is_override && (lladdr && !llchange)) /* (2b) */
  726. || !lladdr) { /* (2c) */
  727. /*
  728. * Update link-local address, if any.
  729. */
  730. if (llchange) {
  731. log(LOG_INFO, "ndp info overwritten for %s "
  732. "by %s on %s\n",
  733. inet_ntop(AF_INET6, &taddr6,
  734. addr, sizeof(addr)),
  735. ether_sprintf(lladdr), ifp->if_xname);
  736. }
  737. if (lladdr) {
  738. sdl->sdl_alen = ifp->if_addrlen;
  739. bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
  740. }
  741. /*
  742. * If solicited, make the state REACHABLE.
  743. * If not solicited and the link-layer address was
  744. * changed, make it STALE.
  745. */
  746. if (is_solicited) {
  747. ln->ln_state = ND6_LLINFO_REACHABLE;
  748. ln->ln_byhint = 0;
  749. if (!ND6_LLINFO_PERMANENT(ln)) {
  750. nd6_llinfo_settimer(ln,
  751. (long)ND_IFINFO(ifp)->reachable * hz);
  752. }
  753. } else {
  754. if (lladdr && llchange) {
  755. ln->ln_state = ND6_LLINFO_STALE;
  756. nd6_llinfo_settimer(ln,
  757. (long)nd6_gctimer * hz);
  758. }
  759. }
  760. }
  761. if (ln->ln_router && !is_router) {
  762. /*
  763. * The peer dropped the router flag.
  764. * Remove the sender from the Default Router List and
  765. * update the Destination Cache entries.
  766. */
  767. struct nd_defrouter *dr;
  768. struct in6_addr *in6;
  769. int s;
  770. in6 = &satosin6(rt_key(rt))->sin6_addr;
  771. /*
  772. * Lock to protect the default router list.
  773. * XXX: this might be unnecessary, since this function
  774. * is only called under the network software interrupt
  775. * context. However, we keep it just for safety.
  776. */
  777. s = splsoftnet();
  778. dr = defrouter_lookup(in6, rt->rt_ifp);
  779. if (dr)
  780. defrtrlist_del(dr);
  781. else if (!ip6_forwarding) {
  782. /*
  783. * Even if the neighbor is not in the default
  784. * router list, the neighbor may be used
  785. * as a next hop for some destinations
  786. * (e.g. redirect case). So we must
  787. * call rt6_flush explicitly.
  788. */
  789. rt6_flush(&ip6->ip6_src, rt->rt_ifp);
  790. }
  791. splx(s);
  792. }
  793. ln->ln_router = is_router;
  794. }
  795. rt->rt_flags &= ~RTF_REJECT;
  796. ln->ln_asked = 0;
  797. if (ln->ln_hold) {
  798. struct mbuf *n = ln->ln_hold;
  799. ln->ln_hold = NULL;
  800. /*
  801. * we assume ifp is not a loopback here, so just set the 2nd
  802. * argument as the 1st one.
  803. */
  804. nd6_output(ifp, n, satosin6(rt_key(rt)), rt);
  805. if (ln->ln_hold == n) {
  806. /* n is back in ln_hold. Discard. */
  807. m_freem(ln->ln_hold);
  808. ln->ln_hold = NULL;
  809. }
  810. }
  811. freeit:
  812. m_freem(m);
  813. return;
  814. bad:
  815. icmp6stat.icp6s_badna++;
  816. m_freem(m);
  817. }
  818. /*
  819. * Neighbor advertisement output handling.
  820. *
  821. * Based on RFC 2461
  822. *
  823. * the following items are not implemented yet:
  824. * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
  825. * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
  826. *
  827. * tlladdr - 1 if include target link-layer address
  828. * sdl0 - sockaddr_dl (= proxy NA) or NULL
  829. */
  830. void
  831. nd6_na_output(struct ifnet *ifp, struct in6_addr *daddr6,
  832. struct in6_addr *taddr6, u_long flags, int tlladdr,
  833. struct sockaddr *sdl0)
  834. {
  835. struct mbuf *m;
  836. struct ip6_hdr *ip6;
  837. struct nd_neighbor_advert *nd_na;
  838. struct ip6_moptions im6o;
  839. struct sockaddr_in6 src_sa, dst_sa;
  840. struct in6_addr *src0;
  841. int icmp6len, maxlen, error;
  842. caddr_t mac;
  843. struct route_in6 ro;
  844. #if NCARP > 0
  845. struct sockaddr_dl *proxydl = NULL;
  846. #endif
  847. mac = NULL;
  848. bzero(&ro, sizeof(ro));
  849. ro.ro_tableid = ifp->if_rdomain;
  850. /* estimate the size of message */
  851. maxlen = sizeof(*ip6) + sizeof(*nd_na);
  852. maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
  853. #ifdef DIAGNOSTIC
  854. if (max_linkhdr + maxlen >= MCLBYTES) {
  855. printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
  856. "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
  857. panic("nd6_na_output: insufficient MCLBYTES");
  858. /* NOTREACHED */
  859. }
  860. #endif
  861. MGETHDR(m, M_DONTWAIT, MT_DATA);
  862. if (m && max_linkhdr + maxlen >= MHLEN) {
  863. MCLGET(m, M_DONTWAIT);
  864. if ((m->m_flags & M_EXT) == 0) {
  865. m_free(m);
  866. m = NULL;
  867. }
  868. }
  869. if (m == NULL)
  870. return;
  871. m->m_pkthdr.ph_ifidx = 0;
  872. m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
  873. if (IN6_IS_ADDR_MULTICAST(daddr6)) {
  874. m->m_flags |= M_MCAST;
  875. im6o.im6o_ifidx = ifp->if_index;
  876. im6o.im6o_hlim = 255;
  877. im6o.im6o_loop = 0;
  878. }
  879. icmp6len = sizeof(*nd_na);
  880. m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
  881. m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
  882. /* fill neighbor advertisement packet */
  883. ip6 = mtod(m, struct ip6_hdr *);
  884. ip6->ip6_flow = 0;
  885. ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  886. ip6->ip6_vfc |= IPV6_VERSION;
  887. ip6->ip6_nxt = IPPROTO_ICMPV6;
  888. ip6->ip6_hlim = 255;
  889. bzero(&src_sa, sizeof(src_sa));
  890. bzero(&dst_sa, sizeof(dst_sa));
  891. src_sa.sin6_len = dst_sa.sin6_len = sizeof(struct sockaddr_in6);
  892. src_sa.sin6_family = dst_sa.sin6_family = AF_INET6;
  893. dst_sa.sin6_addr = *daddr6;
  894. if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
  895. /* reply to DAD */
  896. dst_sa.sin6_addr.s6_addr16[0] = __IPV6_ADDR_INT16_MLL;
  897. dst_sa.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
  898. dst_sa.sin6_addr.s6_addr32[1] = 0;
  899. dst_sa.sin6_addr.s6_addr32[2] = 0;
  900. dst_sa.sin6_addr.s6_addr32[3] = __IPV6_ADDR_INT32_ONE;
  901. flags &= ~ND_NA_FLAG_SOLICITED;
  902. }
  903. ip6->ip6_dst = dst_sa.sin6_addr;
  904. /*
  905. * Select a source whose scope is the same as that of the dest.
  906. */
  907. bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
  908. error = in6_selectsrc(&src0, &dst_sa, NULL, NULL, &ro, NULL,
  909. m->m_pkthdr.ph_rtableid);
  910. if (error) {
  911. char addr[INET6_ADDRSTRLEN];
  912. nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
  913. "determined: dst=%s, error=%d\n",
  914. inet_ntop(AF_INET6, &dst_sa.sin6_addr, addr, sizeof(addr)),
  915. error));
  916. goto bad;
  917. }
  918. src_sa.sin6_addr = *src0;
  919. ip6->ip6_src = src_sa.sin6_addr;
  920. nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
  921. nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
  922. nd_na->nd_na_code = 0;
  923. nd_na->nd_na_target = *taddr6;
  924. if (IN6_IS_SCOPE_EMBED(&nd_na->nd_na_target))
  925. nd_na->nd_na_target.s6_addr16[1] = 0;
  926. /*
  927. * "tlladdr" indicates NS's condition for adding tlladdr or not.
  928. * see nd6_ns_input() for details.
  929. * Basically, if NS packet is sent to unicast/anycast addr,
  930. * target lladdr option SHOULD NOT be included.
  931. */
  932. if (tlladdr) {
  933. /*
  934. * sdl0 != NULL indicates proxy NA. If we do proxy, use
  935. * lladdr in sdl0. If we are not proxying (sending NA for
  936. * my address) use lladdr configured for the interface.
  937. */
  938. if (sdl0 == NULL) {
  939. mac = nd6_ifptomac(ifp);
  940. } else if (sdl0->sa_family == AF_LINK) {
  941. struct sockaddr_dl *sdl;
  942. sdl = (struct sockaddr_dl *)sdl0;
  943. if (sdl->sdl_alen == ifp->if_addrlen)
  944. mac = LLADDR(sdl);
  945. }
  946. }
  947. if (tlladdr && mac) {
  948. int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
  949. struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
  950. /* roundup to 8 bytes alignment! */
  951. optlen = (optlen + 7) & ~7;
  952. m->m_pkthdr.len += optlen;
  953. m->m_len += optlen;
  954. icmp6len += optlen;
  955. bzero((caddr_t)nd_opt, optlen);
  956. nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
  957. nd_opt->nd_opt_len = optlen >> 3;
  958. bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
  959. } else
  960. flags &= ~ND_NA_FLAG_OVERRIDE;
  961. #if NCARP > 0
  962. /* Do not send NAs for carp addresses if we're not the CARP master. */
  963. if (ifp->if_type == IFT_CARP && !carp_iamatch6(ifp, mac, &proxydl))
  964. goto bad;
  965. #endif
  966. ip6->ip6_plen = htons((u_short)icmp6len);
  967. nd_na->nd_na_flags_reserved = flags;
  968. nd_na->nd_na_cksum = 0;
  969. m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
  970. ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
  971. icmp6_ifstat_inc(ifp, ifs6_out_msg);
  972. icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
  973. icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
  974. if (ro.ro_rt) { /* we don't cache this route. */
  975. rtfree(ro.ro_rt);
  976. }
  977. return;
  978. bad:
  979. if (ro.ro_rt) {
  980. rtfree(ro.ro_rt);
  981. }
  982. m_freem(m);
  983. return;
  984. }
  985. caddr_t
  986. nd6_ifptomac(struct ifnet *ifp)
  987. {
  988. switch (ifp->if_type) {
  989. case IFT_ETHER:
  990. case IFT_IEEE1394:
  991. case IFT_PROPVIRTUAL:
  992. case IFT_CARP:
  993. case IFT_IEEE80211:
  994. return ((caddr_t)(ifp + 1));
  995. default:
  996. return NULL;
  997. }
  998. }
  999. static struct dadq_head dadq;
  1000. static int dad_init = 0;
  1001. struct dadq *
  1002. nd6_dad_find(struct ifaddr *ifa)
  1003. {
  1004. struct dadq *dp;
  1005. TAILQ_FOREACH(dp, &dadq, dad_list) {
  1006. if (dp->dad_ifa == ifa)
  1007. return dp;
  1008. }
  1009. return NULL;
  1010. }
  1011. void
  1012. nd6_dad_starttimer(struct dadq *dp, int ticks)
  1013. {
  1014. timeout_set(&dp->dad_timer_ch, (void (*)(void *))nd6_dad_timer,
  1015. (void *)dp->dad_ifa);
  1016. timeout_add(&dp->dad_timer_ch, ticks);
  1017. }
  1018. void
  1019. nd6_dad_stoptimer(struct dadq *dp)
  1020. {
  1021. timeout_del(&dp->dad_timer_ch);
  1022. }
  1023. /*
  1024. * Start Duplicated Address Detection (DAD) for specified interface address.
  1025. *
  1026. * tick - minimum delay ticks for IFF_UP event
  1027. */
  1028. void
  1029. nd6_dad_start(struct ifaddr *ifa, int *tick)
  1030. {
  1031. struct in6_ifaddr *ia6 = ifatoia6(ifa);
  1032. struct dadq *dp;
  1033. char addr[INET6_ADDRSTRLEN];
  1034. if (!dad_init) {
  1035. TAILQ_INIT(&dadq);
  1036. dad_init++;
  1037. }
  1038. /*
  1039. * If we don't need DAD, don't do it.
  1040. * There are several cases:
  1041. * - DAD is disabled (ip6_dad_count == 0)
  1042. * - the interface address is anycast
  1043. */
  1044. if (!(ia6->ia6_flags & IN6_IFF_TENTATIVE)) {
  1045. log(LOG_DEBUG,
  1046. "nd6_dad_start: called with non-tentative address "
  1047. "%s(%s)\n",
  1048. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
  1049. addr, sizeof(addr)),
  1050. ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
  1051. return;
  1052. }
  1053. if (ia6->ia6_flags & IN6_IFF_ANYCAST) {
  1054. ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
  1055. return;
  1056. }
  1057. if (!ip6_dad_count) {
  1058. ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
  1059. return;
  1060. }
  1061. if (!ifa->ifa_ifp)
  1062. panic("nd6_dad_start: ifa->ifa_ifp == NULL");
  1063. if (!(ifa->ifa_ifp->if_flags & IFF_UP))
  1064. return;
  1065. if (nd6_dad_find(ifa) != NULL) {
  1066. /* DAD already in progress */
  1067. return;
  1068. }
  1069. dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
  1070. if (dp == NULL) {
  1071. log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
  1072. "%s(%s)\n",
  1073. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
  1074. addr, sizeof(addr)),
  1075. ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
  1076. return;
  1077. }
  1078. bzero(&dp->dad_timer_ch, sizeof(dp->dad_timer_ch));
  1079. TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
  1080. ip6_dad_pending++;
  1081. nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", ifa->ifa_ifp->if_xname,
  1082. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr))));
  1083. /*
  1084. * Send NS packet for DAD, ip6_dad_count times.
  1085. * Note that we must delay the first transmission, if this is the
  1086. * first packet to be sent from the interface after interface
  1087. * (re)initialization.
  1088. */
  1089. dp->dad_ifa = ifa;
  1090. ifa->ifa_refcnt++; /* just for safety */
  1091. dp->dad_count = ip6_dad_count;
  1092. dp->dad_ns_icount = dp->dad_na_icount = 0;
  1093. dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
  1094. if (tick == NULL) {
  1095. nd6_dad_ns_output(dp, ifa);
  1096. nd6_dad_starttimer(dp,
  1097. (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
  1098. } else {
  1099. int ntick;
  1100. if (*tick == 0)
  1101. ntick = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY *
  1102. hz);
  1103. else
  1104. ntick = *tick + arc4random_uniform(hz / 2);
  1105. *tick = ntick;
  1106. nd6_dad_starttimer(dp, ntick);
  1107. }
  1108. }
  1109. /*
  1110. * terminate DAD unconditionally. used for address removals.
  1111. */
  1112. void
  1113. nd6_dad_stop(struct ifaddr *ifa)
  1114. {
  1115. struct dadq *dp;
  1116. if (!dad_init)
  1117. return;
  1118. dp = nd6_dad_find(ifa);
  1119. if (!dp) {
  1120. /* DAD wasn't started yet */
  1121. return;
  1122. }
  1123. nd6_dad_stoptimer(dp);
  1124. TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
  1125. free(dp, M_IP6NDP, 0);
  1126. dp = NULL;
  1127. ifafree(ifa);
  1128. ip6_dad_pending--;
  1129. }
  1130. void
  1131. nd6_dad_timer(struct ifaddr *ifa)
  1132. {
  1133. int s;
  1134. struct in6_ifaddr *ia6 = ifatoia6(ifa);
  1135. struct dadq *dp;
  1136. char addr[INET6_ADDRSTRLEN];
  1137. s = splsoftnet(); /* XXX */
  1138. /* Sanity check */
  1139. if (ia6 == NULL) {
  1140. log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
  1141. goto done;
  1142. }
  1143. dp = nd6_dad_find(ifa);
  1144. if (dp == NULL) {
  1145. log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
  1146. goto done;
  1147. }
  1148. if (ia6->ia6_flags & IN6_IFF_DUPLICATED) {
  1149. log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
  1150. "%s(%s)\n",
  1151. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
  1152. addr, sizeof(addr)),
  1153. ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
  1154. goto done;
  1155. }
  1156. if ((ia6->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
  1157. log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
  1158. "%s(%s)\n",
  1159. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
  1160. addr, sizeof(addr)),
  1161. ifa->ifa_ifp ? ifa->ifa_ifp->if_xname : "???");
  1162. goto done;
  1163. }
  1164. /* timeouted with IFF_{RUNNING,UP} check */
  1165. if (dp->dad_ns_tcount > dad_maxtry) {
  1166. nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
  1167. ifa->ifa_ifp->if_xname));
  1168. TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
  1169. free(dp, M_IP6NDP, 0);
  1170. dp = NULL;
  1171. ifafree(ifa);
  1172. ip6_dad_pending--;
  1173. goto done;
  1174. }
  1175. /* Need more checks? */
  1176. if (dp->dad_ns_ocount < dp->dad_count) {
  1177. /*
  1178. * We have more NS to go. Send NS packet for DAD.
  1179. */
  1180. nd6_dad_ns_output(dp, ifa);
  1181. nd6_dad_starttimer(dp,
  1182. (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
  1183. } else {
  1184. /*
  1185. * We have transmitted sufficient number of DAD packets.
  1186. * See what we've got.
  1187. */
  1188. int duplicate;
  1189. duplicate = 0;
  1190. if (dp->dad_na_icount) {
  1191. duplicate++;
  1192. }
  1193. if (dp->dad_ns_icount) {
  1194. /* We've seen NS, means DAD has failed. */
  1195. duplicate++;
  1196. }
  1197. if (duplicate) {
  1198. /* dp will be freed in nd6_dad_duplicated() */
  1199. nd6_dad_duplicated(dp);
  1200. } else {
  1201. /*
  1202. * We are done with DAD. No NA came, no NS came.
  1203. * duplicated address found.
  1204. */
  1205. ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
  1206. nd6log((LOG_DEBUG,
  1207. "%s: DAD complete for %s - no duplicates found\n",
  1208. ifa->ifa_ifp->if_xname,
  1209. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
  1210. addr, sizeof(addr))));
  1211. TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
  1212. free(dp, M_IP6NDP, 0);
  1213. dp = NULL;
  1214. ifafree(ifa);
  1215. ip6_dad_pending--;
  1216. }
  1217. }
  1218. done:
  1219. splx(s);
  1220. }
  1221. void
  1222. nd6_dad_duplicated(struct dadq *dp)
  1223. {
  1224. struct in6_ifaddr *ia6 = ifatoia6(dp->dad_ifa);
  1225. char addr[INET6_ADDRSTRLEN];
  1226. log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
  1227. "NS in/out=%d/%d, NA in=%d\n",
  1228. ia6->ia_ifp->if_xname,
  1229. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr)),
  1230. dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
  1231. ia6->ia6_flags &= ~IN6_IFF_TENTATIVE;
  1232. ia6->ia6_flags |= IN6_IFF_DUPLICATED;
  1233. /* We are done with DAD, with duplicated address found. (failure) */
  1234. nd6_dad_stoptimer(dp);
  1235. log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
  1236. ia6->ia_ifp->if_xname,
  1237. inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr, addr, sizeof(addr)));
  1238. log(LOG_ERR, "%s: manual intervention required\n",
  1239. ia6->ia_ifp->if_xname);
  1240. TAILQ_REMOVE(&dadq, dp, dad_list);
  1241. ifafree(dp->dad_ifa);
  1242. free(dp, M_IP6NDP, 0);
  1243. ip6_dad_pending--;
  1244. }
  1245. void
  1246. nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
  1247. {
  1248. struct in6_ifaddr *ia6 = ifatoia6(ifa);
  1249. struct ifnet *ifp = ifa->ifa_ifp;
  1250. dp->dad_ns_tcount++;
  1251. if ((ifp->if_flags & IFF_UP) == 0) {
  1252. #if 0
  1253. printf("%s: interface down?\n", ifp->if_xname);
  1254. #endif
  1255. return;
  1256. }
  1257. if ((ifp->if_flags & IFF_RUNNING) == 0) {
  1258. #if 0
  1259. printf("%s: interface not running?\n", ifp->if_xname);
  1260. #endif
  1261. return;
  1262. }
  1263. dp->dad_ns_ocount++;
  1264. nd6_ns_output(ifp, NULL, &ia6->ia_addr.sin6_addr, NULL, 1);
  1265. }
  1266. void
  1267. nd6_dad_ns_input(struct ifaddr *ifa)
  1268. {
  1269. struct in6_ifaddr *ia6;
  1270. struct in6_addr *taddr6;
  1271. struct dadq *dp;
  1272. int duplicate;
  1273. if (!ifa)
  1274. panic("ifa == NULL in nd6_dad_ns_input");
  1275. ia6 = ifatoia6(ifa);
  1276. taddr6 = &ia6->ia_addr.sin6_addr;
  1277. duplicate = 0;
  1278. dp = nd6_dad_find(ifa);
  1279. /*
  1280. * if I'm yet to start DAD, someone else started using this address
  1281. * first. I have a duplicate and you win.
  1282. */
  1283. if (!dp || dp->dad_ns_ocount == 0)
  1284. duplicate++;
  1285. /* XXX more checks for loopback situation - see nd6_dad_timer too */
  1286. if (duplicate) {
  1287. /* dp will be freed in nd6_dad_duplicated() */
  1288. nd6_dad_duplicated(dp);
  1289. } else {
  1290. /*
  1291. * not sure if I got a duplicate.
  1292. * increment ns count and see what happens.
  1293. */
  1294. if (dp)
  1295. dp->dad_ns_icount++;
  1296. }
  1297. }