nd6_rtr.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  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_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
  32. */
  33. #include <sys/cdefs.h>
  34. __FBSDID("$FreeBSD$");
  35. #include "opt_inet.h"
  36. #include "opt_inet6.h"
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/malloc.h>
  40. #include <sys/mbuf.h>
  41. #include <sys/refcount.h>
  42. #include <sys/socket.h>
  43. #include <sys/sockio.h>
  44. #include <sys/time.h>
  45. #include <sys/kernel.h>
  46. #include <sys/lock.h>
  47. #include <sys/errno.h>
  48. #include <sys/rmlock.h>
  49. #include <sys/rwlock.h>
  50. #include <sys/sysctl.h>
  51. #include <sys/syslog.h>
  52. #include <sys/queue.h>
  53. #include <net/if.h>
  54. #include <net/if_var.h>
  55. #include <net/if_types.h>
  56. #include <net/if_dl.h>
  57. #include <net/route.h>
  58. #include <net/route/nhop.h>
  59. #include <net/route/route_ctl.h>
  60. #include <net/route/route_var.h>
  61. #include <net/radix.h>
  62. #include <net/vnet.h>
  63. #include <netinet/in.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/nd6.h>
  70. #include <netinet/icmp6.h>
  71. #include <netinet6/scope6_var.h>
  72. static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
  73. static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
  74. struct mbuf *, int);
  75. static int nd6_prefix_onlink(struct nd_prefix *);
  76. TAILQ_HEAD(nd6_drhead, nd_defrouter);
  77. VNET_DEFINE_STATIC(struct nd6_drhead, nd6_defrouter);
  78. #define V_nd6_defrouter VNET(nd6_defrouter)
  79. VNET_DECLARE(int, nd6_recalc_reachtm_interval);
  80. #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
  81. VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
  82. VNET_DEFINE(int, nd6_defifindex);
  83. #define V_nd6_defifp VNET(nd6_defifp)
  84. VNET_DEFINE(int, ip6_use_tempaddr) = 0;
  85. VNET_DEFINE(int, ip6_desync_factor);
  86. VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
  87. VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
  88. VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
  89. #ifdef EXPERIMENTAL
  90. VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
  91. #endif
  92. SYSCTL_DECL(_net_inet6_icmp6);
  93. /* RTPREF_MEDIUM has to be 0! */
  94. #define RTPREF_HIGH 1
  95. #define RTPREF_MEDIUM 0
  96. #define RTPREF_LOW (-1)
  97. #define RTPREF_RESERVED (-2)
  98. #define RTPREF_INVALID (-3) /* internal */
  99. static void
  100. defrouter_ref(struct nd_defrouter *dr)
  101. {
  102. refcount_acquire(&dr->refcnt);
  103. }
  104. void
  105. defrouter_rele(struct nd_defrouter *dr)
  106. {
  107. if (refcount_release(&dr->refcnt))
  108. free(dr, M_IP6NDP);
  109. }
  110. /*
  111. * Remove a router from the global list and optionally stash it in a
  112. * caller-supplied queue.
  113. */
  114. static void
  115. defrouter_unlink(struct nd_defrouter *dr, struct nd6_drhead *drq)
  116. {
  117. ND6_WLOCK_ASSERT();
  118. TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
  119. V_nd6_list_genid++;
  120. if (drq != NULL)
  121. TAILQ_INSERT_TAIL(drq, dr, dr_entry);
  122. }
  123. /*
  124. * Receive Router Solicitation Message - just for routers.
  125. * Router solicitation/advertisement is mostly managed by userland program
  126. * (rtadvd) so here we have no function like nd6_ra_output().
  127. *
  128. * Based on RFC 2461
  129. */
  130. void
  131. nd6_rs_input(struct mbuf *m, int off, int icmp6len)
  132. {
  133. struct ifnet *ifp;
  134. struct ip6_hdr *ip6;
  135. struct nd_router_solicit *nd_rs;
  136. struct in6_addr saddr6;
  137. union nd_opts ndopts;
  138. char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
  139. char *lladdr;
  140. int lladdrlen;
  141. ifp = m->m_pkthdr.rcvif;
  142. /*
  143. * Accept RS only when V_ip6_forwarding=1 and the interface has
  144. * no ND6_IFF_ACCEPT_RTADV.
  145. */
  146. if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
  147. goto freeit;
  148. /* RFC 6980: Nodes MUST silently ignore fragments */
  149. if(m->m_flags & M_FRAGMENTED)
  150. goto freeit;
  151. /* Sanity checks */
  152. ip6 = mtod(m, struct ip6_hdr *);
  153. if (__predict_false(ip6->ip6_hlim != 255)) {
  154. ICMP6STAT_INC(icp6s_invlhlim);
  155. nd6log((LOG_ERR,
  156. "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
  157. ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
  158. ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
  159. goto bad;
  160. }
  161. /*
  162. * Don't update the neighbor cache, if src = ::.
  163. * This indicates that the src has no IP address assigned yet.
  164. */
  165. saddr6 = ip6->ip6_src;
  166. if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
  167. goto freeit;
  168. if (m->m_len < off + icmp6len) {
  169. m = m_pullup(m, off + icmp6len);
  170. if (m == NULL) {
  171. IP6STAT_INC(ip6s_exthdrtoolong);
  172. return;
  173. }
  174. }
  175. ip6 = mtod(m, struct ip6_hdr *);
  176. nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
  177. icmp6len -= sizeof(*nd_rs);
  178. nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
  179. if (nd6_options(&ndopts) < 0) {
  180. nd6log((LOG_INFO,
  181. "%s: invalid ND option, ignored\n", __func__));
  182. /* nd6_options have incremented stats */
  183. goto freeit;
  184. }
  185. lladdr = NULL;
  186. lladdrlen = 0;
  187. if (ndopts.nd_opts_src_lladdr) {
  188. lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
  189. lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
  190. }
  191. if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  192. nd6log((LOG_INFO,
  193. "%s: lladdrlen mismatch for %s (if %d, RS packet %d)\n",
  194. __func__, ip6_sprintf(ip6bufs, &saddr6),
  195. ifp->if_addrlen, lladdrlen - 2));
  196. goto bad;
  197. }
  198. nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
  199. freeit:
  200. m_freem(m);
  201. return;
  202. bad:
  203. ICMP6STAT_INC(icp6s_badrs);
  204. m_freem(m);
  205. }
  206. #ifdef EXPERIMENTAL
  207. /*
  208. * An initial update routine for draft-ietf-6man-ipv6only-flag.
  209. * We need to iterate over all default routers for the given
  210. * interface to see whether they are all advertising the "S"
  211. * (IPv6-Only) flag. If they do set, otherwise unset, the
  212. * interface flag we later use to filter on.
  213. */
  214. static void
  215. defrtr_ipv6_only_ifp(struct ifnet *ifp)
  216. {
  217. struct nd_defrouter *dr;
  218. bool ipv6_only, ipv6_only_old;
  219. #ifdef INET
  220. struct epoch_tracker et;
  221. struct ifaddr *ifa;
  222. bool has_ipv4_addr;
  223. #endif
  224. if (V_nd6_ignore_ipv6_only_ra != 0)
  225. return;
  226. ipv6_only = true;
  227. ND6_RLOCK();
  228. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
  229. if (dr->ifp == ifp &&
  230. (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
  231. ipv6_only = false;
  232. ND6_RUNLOCK();
  233. IF_AFDATA_WLOCK(ifp);
  234. ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
  235. IF_AFDATA_WUNLOCK(ifp);
  236. /* If nothing changed, we have an early exit. */
  237. if (ipv6_only == ipv6_only_old)
  238. return;
  239. #ifdef INET
  240. /*
  241. * Should we want to set the IPV6-ONLY flag, check if the
  242. * interface has a non-0/0 and non-link-local IPv4 address
  243. * configured on it. If it has we will assume working
  244. * IPv4 operations and will clear the interface flag.
  245. */
  246. has_ipv4_addr = false;
  247. if (ipv6_only) {
  248. NET_EPOCH_ENTER(et);
  249. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  250. if (ifa->ifa_addr->sa_family != AF_INET)
  251. continue;
  252. if (in_canforward(
  253. satosin(ifa->ifa_addr)->sin_addr)) {
  254. has_ipv4_addr = true;
  255. break;
  256. }
  257. }
  258. NET_EPOCH_EXIT(et);
  259. }
  260. if (ipv6_only && has_ipv4_addr) {
  261. log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
  262. "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
  263. ipv6_only = false;
  264. }
  265. #endif
  266. IF_AFDATA_WLOCK(ifp);
  267. if (ipv6_only)
  268. ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
  269. else
  270. ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
  271. IF_AFDATA_WUNLOCK(ifp);
  272. #ifdef notyet
  273. /* Send notification of flag change. */
  274. #endif
  275. }
  276. static void
  277. defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
  278. {
  279. IF_AFDATA_WLOCK(ifp);
  280. ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
  281. IF_AFDATA_WUNLOCK(ifp);
  282. }
  283. #endif /* EXPERIMENTAL */
  284. void
  285. nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
  286. {
  287. /*
  288. * XXX-BZ we might want to trigger re-evaluation of our default router
  289. * availability. E.g., on link down the default router might be
  290. * unreachable but a different interface might still have connectivity.
  291. */
  292. #ifdef EXPERIMENTAL
  293. if (linkstate == LINK_STATE_DOWN)
  294. defrtr_ipv6_only_ipf_down(ifp);
  295. #endif
  296. }
  297. /*
  298. * Receive Router Advertisement Message.
  299. *
  300. * Based on RFC 2461
  301. * TODO: on-link bit on prefix information
  302. * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
  303. */
  304. void
  305. nd6_ra_input(struct mbuf *m, int off, int icmp6len)
  306. {
  307. struct ifnet *ifp;
  308. struct nd_ifinfo *ndi;
  309. struct ip6_hdr *ip6;
  310. struct nd_router_advert *nd_ra;
  311. struct in6_addr saddr6;
  312. struct nd_defrouter *dr;
  313. union nd_opts ndopts;
  314. char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
  315. int mcast;
  316. /*
  317. * We only accept RAs only when the per-interface flag
  318. * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
  319. */
  320. ifp = m->m_pkthdr.rcvif;
  321. ndi = ND_IFINFO(ifp);
  322. if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
  323. goto freeit;
  324. /* RFC 6980: Nodes MUST silently ignore fragments */
  325. if(m->m_flags & M_FRAGMENTED)
  326. goto freeit;
  327. ip6 = mtod(m, struct ip6_hdr *);
  328. if (__predict_false(ip6->ip6_hlim != 255)) {
  329. ICMP6STAT_INC(icp6s_invlhlim);
  330. nd6log((LOG_ERR,
  331. "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
  332. ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
  333. ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
  334. goto bad;
  335. }
  336. saddr6 = ip6->ip6_src;
  337. if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
  338. nd6log((LOG_ERR,
  339. "%s: src %s is not link-local\n", __func__,
  340. ip6_sprintf(ip6bufs, &saddr6)));
  341. goto bad;
  342. }
  343. if (m->m_len < off + icmp6len) {
  344. m = m_pullup(m, off + icmp6len);
  345. if (m == NULL) {
  346. IP6STAT_INC(ip6s_exthdrtoolong);
  347. return;
  348. }
  349. }
  350. ip6 = mtod(m, struct ip6_hdr *);
  351. nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
  352. icmp6len -= sizeof(*nd_ra);
  353. nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
  354. if (nd6_options(&ndopts) < 0) {
  355. nd6log((LOG_INFO,
  356. "%s: invalid ND option, ignored\n", __func__));
  357. /* nd6_options have incremented stats */
  358. goto freeit;
  359. }
  360. mcast = 0;
  361. dr = NULL;
  362. {
  363. struct nd_defrouter dr0;
  364. u_int32_t advreachable = nd_ra->nd_ra_reachable;
  365. /* remember if this is a multicasted advertisement */
  366. if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
  367. mcast = 1;
  368. bzero(&dr0, sizeof(dr0));
  369. dr0.rtaddr = saddr6;
  370. dr0.raflags = nd_ra->nd_ra_flags_reserved;
  371. /*
  372. * Effectively-disable routes from RA messages when
  373. * ND6_IFF_NO_RADR enabled on the receiving interface or
  374. * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
  375. */
  376. if (ndi->flags & ND6_IFF_NO_RADR)
  377. dr0.rtlifetime = 0;
  378. else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
  379. dr0.rtlifetime = 0;
  380. else
  381. dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
  382. dr0.expire = time_uptime + dr0.rtlifetime;
  383. dr0.ifp = ifp;
  384. /* unspecified or not? (RFC 2461 6.3.4) */
  385. if (advreachable) {
  386. advreachable = ntohl(advreachable);
  387. if (advreachable <= MAX_REACHABLE_TIME &&
  388. ndi->basereachable != advreachable) {
  389. ndi->basereachable = advreachable;
  390. ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
  391. ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
  392. }
  393. }
  394. if (nd_ra->nd_ra_retransmit)
  395. ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
  396. if (nd_ra->nd_ra_curhoplimit) {
  397. if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
  398. ndi->chlim = nd_ra->nd_ra_curhoplimit;
  399. else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
  400. log(LOG_ERR, "RA with a lower CurHopLimit sent from "
  401. "%s on %s (current = %d, received = %d). "
  402. "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
  403. if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
  404. }
  405. }
  406. dr = defrtrlist_update(&dr0);
  407. #ifdef EXPERIMENTAL
  408. defrtr_ipv6_only_ifp(ifp);
  409. #endif
  410. }
  411. /*
  412. * prefix
  413. */
  414. if (ndopts.nd_opts_pi) {
  415. struct nd_opt_hdr *pt;
  416. struct nd_opt_prefix_info *pi = NULL;
  417. struct nd_prefixctl pr;
  418. for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
  419. pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
  420. pt = (struct nd_opt_hdr *)((caddr_t)pt +
  421. (pt->nd_opt_len << 3))) {
  422. if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
  423. continue;
  424. pi = (struct nd_opt_prefix_info *)pt;
  425. if (pi->nd_opt_pi_len != 4) {
  426. nd6log((LOG_INFO,
  427. "%s: invalid option len %d for prefix "
  428. "information option, ignored\n", __func__,
  429. pi->nd_opt_pi_len));
  430. continue;
  431. }
  432. if (128 < pi->nd_opt_pi_prefix_len) {
  433. nd6log((LOG_INFO,
  434. "%s: invalid prefix len %d for prefix "
  435. "information option, ignored\n", __func__,
  436. pi->nd_opt_pi_prefix_len));
  437. continue;
  438. }
  439. if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
  440. || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
  441. nd6log((LOG_INFO,
  442. "%s: invalid prefix %s, ignored\n",
  443. __func__, ip6_sprintf(ip6bufs,
  444. &pi->nd_opt_pi_prefix)));
  445. continue;
  446. }
  447. bzero(&pr, sizeof(pr));
  448. pr.ndpr_prefix.sin6_family = AF_INET6;
  449. pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
  450. pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
  451. pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
  452. pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
  453. ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
  454. pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
  455. ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
  456. pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
  457. pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
  458. pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
  459. (void)prelist_update(&pr, dr, m, mcast);
  460. }
  461. }
  462. if (dr != NULL) {
  463. defrouter_rele(dr);
  464. dr = NULL;
  465. }
  466. /*
  467. * MTU
  468. */
  469. if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
  470. u_long mtu;
  471. u_long maxmtu;
  472. mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
  473. /* lower bound */
  474. if (mtu < IPV6_MMTU) {
  475. nd6log((LOG_INFO, "%s: bogus mtu option mtu=%lu sent "
  476. "from %s, ignoring\n", __func__,
  477. mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
  478. goto skip;
  479. }
  480. /* upper bound */
  481. maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
  482. ? ndi->maxmtu : ifp->if_mtu;
  483. if (mtu <= maxmtu) {
  484. int change = (ndi->linkmtu != mtu);
  485. ndi->linkmtu = mtu;
  486. if (change) {
  487. /* in6_maxmtu may change */
  488. in6_setmaxmtu();
  489. rt_updatemtu(ifp);
  490. }
  491. } else {
  492. nd6log((LOG_INFO, "%s: bogus mtu=%lu sent from %s; "
  493. "exceeds maxmtu %lu, ignoring\n", __func__,
  494. mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
  495. }
  496. }
  497. skip:
  498. /*
  499. * Source link layer address
  500. */
  501. {
  502. char *lladdr = NULL;
  503. int lladdrlen = 0;
  504. if (ndopts.nd_opts_src_lladdr) {
  505. lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
  506. lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
  507. }
  508. if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  509. nd6log((LOG_INFO,
  510. "%s: lladdrlen mismatch for %s (if %d, RA packet %d)\n",
  511. __func__, ip6_sprintf(ip6bufs, &saddr6),
  512. ifp->if_addrlen, lladdrlen - 2));
  513. goto bad;
  514. }
  515. nd6_cache_lladdr(ifp, &saddr6, lladdr,
  516. lladdrlen, ND_ROUTER_ADVERT, 0);
  517. /*
  518. * Installing a link-layer address might change the state of the
  519. * router's neighbor cache, which might also affect our on-link
  520. * detection of adveritsed prefixes.
  521. */
  522. pfxlist_onlink_check();
  523. }
  524. freeit:
  525. m_freem(m);
  526. return;
  527. bad:
  528. ICMP6STAT_INC(icp6s_badra);
  529. m_freem(m);
  530. }
  531. /* PFXRTR */
  532. static struct nd_pfxrouter *
  533. pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
  534. {
  535. struct nd_pfxrouter *search;
  536. ND6_LOCK_ASSERT();
  537. LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
  538. if (search->router == dr)
  539. break;
  540. }
  541. return (search);
  542. }
  543. static void
  544. pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
  545. {
  546. struct nd_pfxrouter *new;
  547. bool update;
  548. ND6_UNLOCK_ASSERT();
  549. ND6_RLOCK();
  550. if (pfxrtr_lookup(pr, dr) != NULL) {
  551. ND6_RUNLOCK();
  552. return;
  553. }
  554. ND6_RUNLOCK();
  555. new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
  556. if (new == NULL)
  557. return;
  558. defrouter_ref(dr);
  559. new->router = dr;
  560. ND6_WLOCK();
  561. if (pfxrtr_lookup(pr, dr) == NULL) {
  562. LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
  563. update = true;
  564. } else {
  565. /* We lost a race to add the reference. */
  566. defrouter_rele(dr);
  567. free(new, M_IP6NDP);
  568. update = false;
  569. }
  570. ND6_WUNLOCK();
  571. if (update)
  572. pfxlist_onlink_check();
  573. }
  574. static void
  575. pfxrtr_del(struct nd_pfxrouter *pfr)
  576. {
  577. ND6_WLOCK_ASSERT();
  578. LIST_REMOVE(pfr, pfr_entry);
  579. defrouter_rele(pfr->router);
  580. free(pfr, M_IP6NDP);
  581. }
  582. /* Default router list processing sub routines. */
  583. static void
  584. defrouter_addreq(struct nd_defrouter *new)
  585. {
  586. struct sockaddr_in6 def, mask, gate;
  587. struct rt_addrinfo info;
  588. struct rib_cmd_info rc;
  589. unsigned int fibnum;
  590. int error;
  591. bzero(&def, sizeof(def));
  592. bzero(&mask, sizeof(mask));
  593. bzero(&gate, sizeof(gate));
  594. def.sin6_len = mask.sin6_len = gate.sin6_len =
  595. sizeof(struct sockaddr_in6);
  596. def.sin6_family = gate.sin6_family = AF_INET6;
  597. gate.sin6_addr = new->rtaddr;
  598. fibnum = new->ifp->if_fib;
  599. bzero((caddr_t)&info, sizeof(info));
  600. info.rti_flags = RTF_GATEWAY;
  601. info.rti_info[RTAX_DST] = (struct sockaddr *)&def;
  602. info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gate;
  603. info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask;
  604. NET_EPOCH_ASSERT();
  605. error = rib_action(fibnum, RTM_ADD, &info, &rc);
  606. if (rc.rc_rt != NULL) {
  607. struct nhop_object *nh = nhop_select(rc.rc_nh_new, 0);
  608. rt_routemsg(RTM_ADD, rc.rc_rt, nh, fibnum);
  609. }
  610. if (error == 0)
  611. new->installed = 1;
  612. }
  613. /*
  614. * Remove the default route for a given router.
  615. * This is just a subroutine function for defrouter_select_fib(), and
  616. * should not be called from anywhere else.
  617. */
  618. static void
  619. defrouter_delreq(struct nd_defrouter *dr)
  620. {
  621. struct sockaddr_in6 def, mask, gate;
  622. struct rt_addrinfo info;
  623. struct rib_cmd_info rc;
  624. struct epoch_tracker et;
  625. unsigned int fibnum;
  626. bzero(&def, sizeof(def));
  627. bzero(&mask, sizeof(mask));
  628. bzero(&gate, sizeof(gate));
  629. def.sin6_len = mask.sin6_len = gate.sin6_len =
  630. sizeof(struct sockaddr_in6);
  631. def.sin6_family = gate.sin6_family = AF_INET6;
  632. gate.sin6_addr = dr->rtaddr;
  633. fibnum = dr->ifp->if_fib;
  634. bzero((caddr_t)&info, sizeof(info));
  635. info.rti_flags = RTF_GATEWAY;
  636. info.rti_info[RTAX_DST] = (struct sockaddr *)&def;
  637. info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gate;
  638. info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask;
  639. NET_EPOCH_ENTER(et);
  640. rib_action(fibnum, RTM_DELETE, &info, &rc);
  641. if (rc.rc_rt != NULL) {
  642. struct nhop_object *nh = nhop_select(rc.rc_nh_old, 0);
  643. rt_routemsg(RTM_DELETE, rc.rc_rt, nh, fibnum);
  644. }
  645. NET_EPOCH_EXIT(et);
  646. dr->installed = 0;
  647. }
  648. static void
  649. defrouter_del(struct nd_defrouter *dr)
  650. {
  651. struct nd_defrouter *deldr = NULL;
  652. struct nd_prefix *pr;
  653. struct nd_pfxrouter *pfxrtr;
  654. ND6_UNLOCK_ASSERT();
  655. /*
  656. * Flush all the routing table entries that use the router
  657. * as a next hop.
  658. */
  659. if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
  660. rt6_flush(&dr->rtaddr, dr->ifp);
  661. #ifdef EXPERIMENTAL
  662. defrtr_ipv6_only_ifp(dr->ifp);
  663. #endif
  664. if (dr->installed) {
  665. deldr = dr;
  666. defrouter_delreq(dr);
  667. }
  668. /*
  669. * Also delete all the pointers to the router in each prefix lists.
  670. */
  671. ND6_WLOCK();
  672. LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
  673. if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
  674. pfxrtr_del(pfxrtr);
  675. }
  676. ND6_WUNLOCK();
  677. pfxlist_onlink_check();
  678. /*
  679. * If the router is the primary one, choose a new one.
  680. * Note that defrouter_select_fib() will remove the current
  681. * gateway from the routing table.
  682. */
  683. if (deldr)
  684. defrouter_select_fib(deldr->ifp->if_fib);
  685. /*
  686. * Release the list reference.
  687. */
  688. defrouter_rele(dr);
  689. }
  690. struct nd_defrouter *
  691. defrouter_lookup_locked(const struct in6_addr *addr, struct ifnet *ifp)
  692. {
  693. struct nd_defrouter *dr;
  694. ND6_LOCK_ASSERT();
  695. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
  696. if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
  697. defrouter_ref(dr);
  698. return (dr);
  699. }
  700. return (NULL);
  701. }
  702. struct nd_defrouter *
  703. defrouter_lookup(const struct in6_addr *addr, struct ifnet *ifp)
  704. {
  705. struct nd_defrouter *dr;
  706. ND6_RLOCK();
  707. dr = defrouter_lookup_locked(addr, ifp);
  708. ND6_RUNLOCK();
  709. return (dr);
  710. }
  711. /*
  712. * Remove all default routes from default router list.
  713. */
  714. void
  715. defrouter_reset(void)
  716. {
  717. struct nd_defrouter *dr, **dra;
  718. int count, i;
  719. count = i = 0;
  720. /*
  721. * We can't delete routes with the ND lock held, so make a copy of the
  722. * current default router list and use that when deleting routes.
  723. */
  724. ND6_RLOCK();
  725. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
  726. count++;
  727. ND6_RUNLOCK();
  728. dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
  729. ND6_RLOCK();
  730. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
  731. if (i == count)
  732. break;
  733. defrouter_ref(dr);
  734. dra[i++] = dr;
  735. }
  736. ND6_RUNLOCK();
  737. for (i = 0; i < count && dra[i] != NULL; i++) {
  738. defrouter_delreq(dra[i]);
  739. defrouter_rele(dra[i]);
  740. }
  741. free(dra, M_TEMP);
  742. /*
  743. * XXX should we also nuke any default routers in the kernel, by
  744. * going through them by rtalloc1()?
  745. */
  746. }
  747. /*
  748. * Look up a matching default router list entry and remove it. Returns true if a
  749. * matching entry was found, false otherwise.
  750. */
  751. bool
  752. defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
  753. {
  754. struct nd_defrouter *dr;
  755. ND6_WLOCK();
  756. dr = defrouter_lookup_locked(addr, ifp);
  757. if (dr == NULL) {
  758. ND6_WUNLOCK();
  759. return (false);
  760. }
  761. defrouter_unlink(dr, NULL);
  762. ND6_WUNLOCK();
  763. defrouter_del(dr);
  764. defrouter_rele(dr);
  765. return (true);
  766. }
  767. /*
  768. * for default router selection
  769. * regards router-preference field as a 2-bit signed integer
  770. */
  771. static int
  772. rtpref(struct nd_defrouter *dr)
  773. {
  774. switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
  775. case ND_RA_FLAG_RTPREF_HIGH:
  776. return (RTPREF_HIGH);
  777. case ND_RA_FLAG_RTPREF_MEDIUM:
  778. case ND_RA_FLAG_RTPREF_RSV:
  779. return (RTPREF_MEDIUM);
  780. case ND_RA_FLAG_RTPREF_LOW:
  781. return (RTPREF_LOW);
  782. default:
  783. /*
  784. * This case should never happen. If it did, it would mean a
  785. * serious bug of kernel internal. We thus always bark here.
  786. * Or, can we even panic?
  787. */
  788. log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
  789. return (RTPREF_INVALID);
  790. }
  791. /* NOTREACHED */
  792. }
  793. /*
  794. * Default Router Selection according to Section 6.3.6 of RFC 2461 and
  795. * draft-ietf-ipngwg-router-selection:
  796. * 1) Routers that are reachable or probably reachable should be preferred.
  797. * If we have more than one (probably) reachable router, prefer ones
  798. * with the highest router preference.
  799. * 2) When no routers on the list are known to be reachable or
  800. * probably reachable, routers SHOULD be selected in a round-robin
  801. * fashion, regardless of router preference values.
  802. * 3) If the Default Router List is empty, assume that all
  803. * destinations are on-link.
  804. *
  805. * We assume nd_defrouter is sorted by router preference value.
  806. * Since the code below covers both with and without router preference cases,
  807. * we do not need to classify the cases by ifdef.
  808. *
  809. * At this moment, we do not try to install more than one default router,
  810. * even when the multipath routing is available, because we're not sure about
  811. * the benefits for stub hosts comparing to the risk of making the code
  812. * complicated and the possibility of introducing bugs.
  813. *
  814. * We maintain a single list of routers for multiple FIBs, only considering one
  815. * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
  816. * we do the whole thing multiple times.
  817. */
  818. void
  819. defrouter_select_fib(int fibnum)
  820. {
  821. struct epoch_tracker et;
  822. struct nd_defrouter *dr, *selected_dr, *installed_dr;
  823. struct llentry *ln = NULL;
  824. if (fibnum == RT_ALL_FIBS) {
  825. for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
  826. defrouter_select_fib(fibnum);
  827. }
  828. }
  829. ND6_RLOCK();
  830. /*
  831. * Let's handle easy case (3) first:
  832. * If default router list is empty, there's nothing to be done.
  833. */
  834. if (TAILQ_EMPTY(&V_nd6_defrouter)) {
  835. ND6_RUNLOCK();
  836. return;
  837. }
  838. /*
  839. * Search for a (probably) reachable router from the list.
  840. * We just pick up the first reachable one (if any), assuming that
  841. * the ordering rule of the list described in defrtrlist_update().
  842. */
  843. selected_dr = installed_dr = NULL;
  844. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
  845. NET_EPOCH_ENTER(et);
  846. if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
  847. (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
  848. ND6_IS_LLINFO_PROBREACH(ln)) {
  849. selected_dr = dr;
  850. defrouter_ref(selected_dr);
  851. }
  852. NET_EPOCH_EXIT(et);
  853. if (ln != NULL) {
  854. LLE_RUNLOCK(ln);
  855. ln = NULL;
  856. }
  857. if (dr->installed && dr->ifp->if_fib == fibnum) {
  858. if (installed_dr == NULL) {
  859. installed_dr = dr;
  860. defrouter_ref(installed_dr);
  861. } else {
  862. /*
  863. * this should not happen.
  864. * warn for diagnosis.
  865. */
  866. log(LOG_ERR, "defrouter_select_fib: more than "
  867. "one router is installed\n");
  868. }
  869. }
  870. }
  871. /*
  872. * If none of the default routers was found to be reachable,
  873. * round-robin the list regardless of preference.
  874. * Otherwise, if we have an installed router, check if the selected
  875. * (reachable) router should really be preferred to the installed one.
  876. * We only prefer the new router when the old one is not reachable
  877. * or when the new one has a really higher preference value.
  878. */
  879. if (selected_dr == NULL) {
  880. if (installed_dr == NULL ||
  881. TAILQ_NEXT(installed_dr, dr_entry) == NULL)
  882. dr = TAILQ_FIRST(&V_nd6_defrouter);
  883. else
  884. dr = TAILQ_NEXT(installed_dr, dr_entry);
  885. /* Ensure we select a router for this FIB. */
  886. TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
  887. if (dr->ifp->if_fib == fibnum) {
  888. selected_dr = dr;
  889. defrouter_ref(selected_dr);
  890. break;
  891. }
  892. }
  893. } else if (installed_dr != NULL) {
  894. NET_EPOCH_ENTER(et);
  895. if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
  896. installed_dr->ifp)) &&
  897. ND6_IS_LLINFO_PROBREACH(ln) &&
  898. installed_dr->ifp->if_fib == fibnum &&
  899. rtpref(selected_dr) <= rtpref(installed_dr)) {
  900. defrouter_rele(selected_dr);
  901. selected_dr = installed_dr;
  902. }
  903. NET_EPOCH_EXIT(et);
  904. if (ln != NULL)
  905. LLE_RUNLOCK(ln);
  906. }
  907. ND6_RUNLOCK();
  908. NET_EPOCH_ENTER(et);
  909. /*
  910. * If we selected a router for this FIB and it's different
  911. * than the installed one, remove the installed router and
  912. * install the selected one in its place.
  913. */
  914. if (installed_dr != selected_dr) {
  915. if (installed_dr != NULL) {
  916. defrouter_delreq(installed_dr);
  917. defrouter_rele(installed_dr);
  918. }
  919. if (selected_dr != NULL)
  920. defrouter_addreq(selected_dr);
  921. }
  922. if (selected_dr != NULL)
  923. defrouter_rele(selected_dr);
  924. NET_EPOCH_EXIT(et);
  925. }
  926. static struct nd_defrouter *
  927. defrtrlist_update(struct nd_defrouter *new)
  928. {
  929. struct nd_defrouter *dr, *n;
  930. uint64_t genid;
  931. int oldpref;
  932. bool writelocked;
  933. if (new->rtlifetime == 0) {
  934. defrouter_remove(&new->rtaddr, new->ifp);
  935. return (NULL);
  936. }
  937. ND6_RLOCK();
  938. writelocked = false;
  939. restart:
  940. dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
  941. if (dr != NULL) {
  942. oldpref = rtpref(dr);
  943. /* override */
  944. dr->raflags = new->raflags; /* XXX flag check */
  945. dr->rtlifetime = new->rtlifetime;
  946. dr->expire = new->expire;
  947. /*
  948. * If the preference does not change, there's no need
  949. * to sort the entries. Also make sure the selected
  950. * router is still installed in the kernel.
  951. */
  952. if (dr->installed && rtpref(new) == oldpref) {
  953. if (writelocked)
  954. ND6_WUNLOCK();
  955. else
  956. ND6_RUNLOCK();
  957. return (dr);
  958. }
  959. }
  960. /*
  961. * The router needs to be reinserted into the default router
  962. * list, so upgrade to a write lock. If that fails and the list
  963. * has potentially changed while the lock was dropped, we'll
  964. * redo the lookup with the write lock held.
  965. */
  966. if (!writelocked) {
  967. writelocked = true;
  968. if (!ND6_TRY_UPGRADE()) {
  969. genid = V_nd6_list_genid;
  970. ND6_RUNLOCK();
  971. ND6_WLOCK();
  972. if (genid != V_nd6_list_genid)
  973. goto restart;
  974. }
  975. }
  976. if (dr != NULL) {
  977. /*
  978. * The preferred router may have changed, so relocate this
  979. * router.
  980. */
  981. TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
  982. n = dr;
  983. } else {
  984. n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
  985. if (n == NULL) {
  986. ND6_WUNLOCK();
  987. return (NULL);
  988. }
  989. memcpy(n, new, sizeof(*n));
  990. /* Initialize with an extra reference for the caller. */
  991. refcount_init(&n->refcnt, 2);
  992. }
  993. /*
  994. * Insert the new router in the Default Router List;
  995. * The Default Router List should be in the descending order
  996. * of router-preferece. Routers with the same preference are
  997. * sorted in the arriving time order.
  998. */
  999. /* insert at the end of the group */
  1000. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
  1001. if (rtpref(n) > rtpref(dr))
  1002. break;
  1003. }
  1004. if (dr != NULL)
  1005. TAILQ_INSERT_BEFORE(dr, n, dr_entry);
  1006. else
  1007. TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
  1008. V_nd6_list_genid++;
  1009. ND6_WUNLOCK();
  1010. defrouter_select_fib(new->ifp->if_fib);
  1011. return (n);
  1012. }
  1013. static int
  1014. in6_init_prefix_ltimes(struct nd_prefix *ndpr)
  1015. {
  1016. if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
  1017. ndpr->ndpr_preferred = 0;
  1018. else
  1019. ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
  1020. if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
  1021. ndpr->ndpr_expire = 0;
  1022. else
  1023. ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
  1024. return 0;
  1025. }
  1026. static void
  1027. in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
  1028. {
  1029. /* init ia6t_expire */
  1030. if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
  1031. lt6->ia6t_expire = 0;
  1032. else {
  1033. lt6->ia6t_expire = time_uptime;
  1034. lt6->ia6t_expire += lt6->ia6t_vltime;
  1035. }
  1036. /* init ia6t_preferred */
  1037. if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
  1038. lt6->ia6t_preferred = 0;
  1039. else {
  1040. lt6->ia6t_preferred = time_uptime;
  1041. lt6->ia6t_preferred += lt6->ia6t_pltime;
  1042. }
  1043. }
  1044. static struct in6_ifaddr *
  1045. in6_ifadd(struct nd_prefixctl *pr, int mcast)
  1046. {
  1047. struct ifnet *ifp = pr->ndpr_ifp;
  1048. struct ifaddr *ifa;
  1049. struct in6_aliasreq ifra;
  1050. struct in6_ifaddr *ia, *ib;
  1051. int error, plen0;
  1052. struct in6_addr mask;
  1053. int prefixlen = pr->ndpr_plen;
  1054. int updateflags;
  1055. char ip6buf[INET6_ADDRSTRLEN];
  1056. in6_prefixlen2mask(&mask, prefixlen);
  1057. /*
  1058. * find a link-local address (will be interface ID).
  1059. * Is it really mandatory? Theoretically, a global or a site-local
  1060. * address can be configured without a link-local address, if we
  1061. * have a unique interface identifier...
  1062. *
  1063. * it is not mandatory to have a link-local address, we can generate
  1064. * interface identifier on the fly. we do this because:
  1065. * (1) it should be the easiest way to find interface identifier.
  1066. * (2) RFC2462 5.4 suggesting the use of the same interface identifier
  1067. * for multiple addresses on a single interface, and possible shortcut
  1068. * of DAD. we omitted DAD for this reason in the past.
  1069. * (3) a user can prevent autoconfiguration of global address
  1070. * by removing link-local address by hand (this is partly because we
  1071. * don't have other way to control the use of IPv6 on an interface.
  1072. * this has been our design choice - cf. NRL's "ifconfig auto").
  1073. * (4) it is easier to manage when an interface has addresses
  1074. * with the same interface identifier, than to have multiple addresses
  1075. * with different interface identifiers.
  1076. */
  1077. ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
  1078. if (ifa)
  1079. ib = (struct in6_ifaddr *)ifa;
  1080. else
  1081. return NULL;
  1082. /* prefixlen + ifidlen must be equal to 128 */
  1083. plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
  1084. if (prefixlen != plen0) {
  1085. ifa_free(ifa);
  1086. nd6log((LOG_INFO,
  1087. "%s: wrong prefixlen for %s (prefix=%d ifid=%d)\n",
  1088. __func__, if_name(ifp), prefixlen, 128 - plen0));
  1089. return NULL;
  1090. }
  1091. /* make ifaddr */
  1092. in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
  1093. IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
  1094. /* interface ID */
  1095. ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
  1096. (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
  1097. ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
  1098. (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
  1099. ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
  1100. (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
  1101. ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
  1102. (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
  1103. ifa_free(ifa);
  1104. /* lifetimes. */
  1105. ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
  1106. ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
  1107. /* XXX: scope zone ID? */
  1108. ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
  1109. /*
  1110. * Make sure that we do not have this address already. This should
  1111. * usually not happen, but we can still see this case, e.g., if we
  1112. * have manually configured the exact address to be configured.
  1113. */
  1114. ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
  1115. &ifra.ifra_addr.sin6_addr);
  1116. if (ifa != NULL) {
  1117. ifa_free(ifa);
  1118. /* this should be rare enough to make an explicit log */
  1119. log(LOG_INFO, "in6_ifadd: %s is already configured\n",
  1120. ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
  1121. return (NULL);
  1122. }
  1123. /*
  1124. * Allocate ifaddr structure, link into chain, etc.
  1125. * If we are going to create a new address upon receiving a multicasted
  1126. * RA, we need to impose a random delay before starting DAD.
  1127. * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
  1128. */
  1129. updateflags = 0;
  1130. if (mcast)
  1131. updateflags |= IN6_IFAUPDATE_DADDELAY;
  1132. if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
  1133. nd6log((LOG_ERR,
  1134. "%s: failed to make ifaddr %s on %s (errno=%d)\n", __func__,
  1135. ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
  1136. if_name(ifp), error));
  1137. return (NULL); /* ifaddr must not have been allocated. */
  1138. }
  1139. ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
  1140. /*
  1141. * XXXRW: Assumption of non-NULLness here might not be true with
  1142. * fine-grained locking -- should we validate it? Or just return
  1143. * earlier ifa rather than looking it up again?
  1144. */
  1145. return (ia); /* this is always non-NULL and referenced. */
  1146. }
  1147. static struct nd_prefix *
  1148. nd6_prefix_lookup_locked(struct nd_prefixctl *key)
  1149. {
  1150. struct nd_prefix *search;
  1151. ND6_LOCK_ASSERT();
  1152. LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
  1153. if (key->ndpr_ifp == search->ndpr_ifp &&
  1154. key->ndpr_plen == search->ndpr_plen &&
  1155. in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
  1156. &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
  1157. nd6_prefix_ref(search);
  1158. break;
  1159. }
  1160. }
  1161. return (search);
  1162. }
  1163. struct nd_prefix *
  1164. nd6_prefix_lookup(struct nd_prefixctl *key)
  1165. {
  1166. struct nd_prefix *search;
  1167. ND6_RLOCK();
  1168. search = nd6_prefix_lookup_locked(key);
  1169. ND6_RUNLOCK();
  1170. return (search);
  1171. }
  1172. void
  1173. nd6_prefix_ref(struct nd_prefix *pr)
  1174. {
  1175. refcount_acquire(&pr->ndpr_refcnt);
  1176. }
  1177. void
  1178. nd6_prefix_rele(struct nd_prefix *pr)
  1179. {
  1180. if (refcount_release(&pr->ndpr_refcnt)) {
  1181. KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
  1182. ("prefix %p has advertising routers", pr));
  1183. free(pr, M_IP6NDP);
  1184. }
  1185. }
  1186. int
  1187. nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
  1188. struct nd_prefix **newp)
  1189. {
  1190. struct nd_prefix *new;
  1191. char ip6buf[INET6_ADDRSTRLEN];
  1192. int error;
  1193. new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
  1194. if (new == NULL)
  1195. return (ENOMEM);
  1196. refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
  1197. new->ndpr_ifp = pr->ndpr_ifp;
  1198. new->ndpr_prefix = pr->ndpr_prefix;
  1199. new->ndpr_plen = pr->ndpr_plen;
  1200. new->ndpr_vltime = pr->ndpr_vltime;
  1201. new->ndpr_pltime = pr->ndpr_pltime;
  1202. new->ndpr_flags = pr->ndpr_flags;
  1203. if ((error = in6_init_prefix_ltimes(new)) != 0) {
  1204. free(new, M_IP6NDP);
  1205. return (error);
  1206. }
  1207. new->ndpr_lastupdate = time_uptime;
  1208. /* initialization */
  1209. LIST_INIT(&new->ndpr_advrtrs);
  1210. in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
  1211. /* make prefix in the canonical form */
  1212. IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
  1213. ND6_WLOCK();
  1214. LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
  1215. V_nd6_list_genid++;
  1216. ND6_WUNLOCK();
  1217. /* ND_OPT_PI_FLAG_ONLINK processing */
  1218. if (new->ndpr_raf_onlink) {
  1219. struct epoch_tracker et;
  1220. ND6_ONLINK_LOCK();
  1221. NET_EPOCH_ENTER(et);
  1222. if ((error = nd6_prefix_onlink(new)) != 0) {
  1223. nd6log((LOG_ERR, "%s: failed to make the prefix %s/%d "
  1224. "on-link on %s (errno=%d)\n", __func__,
  1225. ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
  1226. pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
  1227. /* proceed anyway. XXX: is it correct? */
  1228. }
  1229. NET_EPOCH_EXIT(et);
  1230. ND6_ONLINK_UNLOCK();
  1231. }
  1232. if (dr != NULL)
  1233. pfxrtr_add(new, dr);
  1234. if (newp != NULL)
  1235. *newp = new;
  1236. return (0);
  1237. }
  1238. /*
  1239. * Remove a prefix from the prefix list and optionally stash it in a
  1240. * caller-provided list.
  1241. *
  1242. * The ND6 lock must be held.
  1243. */
  1244. void
  1245. nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
  1246. {
  1247. ND6_WLOCK_ASSERT();
  1248. LIST_REMOVE(pr, ndpr_entry);
  1249. V_nd6_list_genid++;
  1250. if (list != NULL)
  1251. LIST_INSERT_HEAD(list, pr, ndpr_entry);
  1252. }
  1253. /*
  1254. * Free an unlinked prefix, first marking it off-link if necessary.
  1255. */
  1256. void
  1257. nd6_prefix_del(struct nd_prefix *pr)
  1258. {
  1259. struct nd_pfxrouter *pfr, *next;
  1260. int e;
  1261. char ip6buf[INET6_ADDRSTRLEN];
  1262. KASSERT(pr->ndpr_addrcnt == 0,
  1263. ("prefix %p has referencing addresses", pr));
  1264. ND6_UNLOCK_ASSERT();
  1265. /*
  1266. * Though these flags are now meaningless, we'd rather keep the value
  1267. * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
  1268. * when executing "ndp -p".
  1269. */
  1270. if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
  1271. ND6_ONLINK_LOCK();
  1272. if ((e = nd6_prefix_offlink(pr)) != 0) {
  1273. nd6log((LOG_ERR,
  1274. "%s: failed to make the prefix %s/%d offlink on %s "
  1275. "(errno=%d)\n", __func__,
  1276. ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
  1277. pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
  1278. /* what should we do? */
  1279. }
  1280. ND6_ONLINK_UNLOCK();
  1281. }
  1282. /* Release references to routers that have advertised this prefix. */
  1283. ND6_WLOCK();
  1284. LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
  1285. pfxrtr_del(pfr);
  1286. ND6_WUNLOCK();
  1287. nd6_prefix_rele(pr);
  1288. pfxlist_onlink_check();
  1289. }
  1290. static int
  1291. prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
  1292. struct mbuf *m, int mcast)
  1293. {
  1294. struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
  1295. struct ifaddr *ifa;
  1296. struct ifnet *ifp = new->ndpr_ifp;
  1297. struct nd_prefix *pr;
  1298. int error = 0;
  1299. int auth;
  1300. struct in6_addrlifetime lt6_tmp;
  1301. char ip6buf[INET6_ADDRSTRLEN];
  1302. NET_EPOCH_ASSERT();
  1303. auth = 0;
  1304. if (m) {
  1305. /*
  1306. * Authenticity for NA consists authentication for
  1307. * both IP header and IP datagrams, doesn't it ?
  1308. */
  1309. #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
  1310. auth = ((m->m_flags & M_AUTHIPHDR) &&
  1311. (m->m_flags & M_AUTHIPDGM));
  1312. #endif
  1313. }
  1314. if ((pr = nd6_prefix_lookup(new)) != NULL) {
  1315. /*
  1316. * nd6_prefix_lookup() ensures that pr and new have the same
  1317. * prefix on a same interface.
  1318. */
  1319. /*
  1320. * Update prefix information. Note that the on-link (L) bit
  1321. * and the autonomous (A) bit should NOT be changed from 1
  1322. * to 0.
  1323. */
  1324. if (new->ndpr_raf_onlink == 1)
  1325. pr->ndpr_raf_onlink = 1;
  1326. if (new->ndpr_raf_auto == 1)
  1327. pr->ndpr_raf_auto = 1;
  1328. if (new->ndpr_raf_onlink) {
  1329. pr->ndpr_vltime = new->ndpr_vltime;
  1330. pr->ndpr_pltime = new->ndpr_pltime;
  1331. (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
  1332. pr->ndpr_lastupdate = time_uptime;
  1333. }
  1334. if (new->ndpr_raf_onlink &&
  1335. (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
  1336. ND6_ONLINK_LOCK();
  1337. if ((error = nd6_prefix_onlink(pr)) != 0) {
  1338. nd6log((LOG_ERR,
  1339. "%s: failed to make the prefix %s/%d "
  1340. "on-link on %s (errno=%d)\n", __func__,
  1341. ip6_sprintf(ip6buf,
  1342. &pr->ndpr_prefix.sin6_addr),
  1343. pr->ndpr_plen, if_name(pr->ndpr_ifp),
  1344. error));
  1345. /* proceed anyway. XXX: is it correct? */
  1346. }
  1347. ND6_ONLINK_UNLOCK();
  1348. }
  1349. if (dr != NULL)
  1350. pfxrtr_add(pr, dr);
  1351. } else {
  1352. if (new->ndpr_vltime == 0)
  1353. goto end;
  1354. if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
  1355. goto end;
  1356. error = nd6_prelist_add(new, dr, &pr);
  1357. if (error != 0) {
  1358. nd6log((LOG_NOTICE, "%s: nd6_prelist_add() failed for "
  1359. "the prefix %s/%d on %s (errno=%d)\n", __func__,
  1360. ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
  1361. new->ndpr_plen, if_name(new->ndpr_ifp), error));
  1362. goto end; /* we should just give up in this case. */
  1363. }
  1364. /*
  1365. * XXX: from the ND point of view, we can ignore a prefix
  1366. * with the on-link bit being zero. However, we need a
  1367. * prefix structure for references from autoconfigured
  1368. * addresses. Thus, we explicitly make sure that the prefix
  1369. * itself expires now.
  1370. */
  1371. if (pr->ndpr_raf_onlink == 0) {
  1372. pr->ndpr_vltime = 0;
  1373. pr->ndpr_pltime = 0;
  1374. in6_init_prefix_ltimes(pr);
  1375. }
  1376. }
  1377. /*
  1378. * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
  1379. * Note that pr must be non NULL at this point.
  1380. */
  1381. /* 5.5.3 (a). Ignore the prefix without the A bit set. */
  1382. if (!new->ndpr_raf_auto)
  1383. goto end;
  1384. /*
  1385. * 5.5.3 (b). the link-local prefix should have been ignored in
  1386. * nd6_ra_input.
  1387. */
  1388. /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
  1389. if (new->ndpr_pltime > new->ndpr_vltime) {
  1390. error = EINVAL; /* XXX: won't be used */
  1391. goto end;
  1392. }
  1393. /*
  1394. * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
  1395. * an address configured by stateless autoconfiguration already in the
  1396. * list of addresses associated with the interface, and the Valid
  1397. * Lifetime is not 0, form an address. We first check if we have
  1398. * a matching prefix.
  1399. * Note: we apply a clarification in rfc2462bis-02 here. We only
  1400. * consider autoconfigured addresses while RFC2462 simply said
  1401. * "address".
  1402. */
  1403. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  1404. struct in6_ifaddr *ifa6;
  1405. u_int32_t remaininglifetime;
  1406. if (ifa->ifa_addr->sa_family != AF_INET6)
  1407. continue;
  1408. ifa6 = (struct in6_ifaddr *)ifa;
  1409. /*
  1410. * We only consider autoconfigured addresses as per rfc2462bis.
  1411. */
  1412. if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
  1413. continue;
  1414. /*
  1415. * Spec is not clear here, but I believe we should concentrate
  1416. * on unicast (i.e. not anycast) addresses.
  1417. * XXX: other ia6_flags? detached or duplicated?
  1418. */
  1419. if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
  1420. continue;
  1421. /*
  1422. * Ignore the address if it is not associated with a prefix
  1423. * or is associated with a prefix that is different from this
  1424. * one. (pr is never NULL here)
  1425. */
  1426. if (ifa6->ia6_ndpr != pr)
  1427. continue;
  1428. if (ia6_match == NULL) /* remember the first one */
  1429. ia6_match = ifa6;
  1430. /*
  1431. * An already autoconfigured address matched. Now that we
  1432. * are sure there is at least one matched address, we can
  1433. * proceed to 5.5.3. (e): update the lifetimes according to the
  1434. * "two hours" rule and the privacy extension.
  1435. * We apply some clarifications in rfc2462bis:
  1436. * - use remaininglifetime instead of storedlifetime as a
  1437. * variable name
  1438. * - remove the dead code in the "two-hour" rule
  1439. */
  1440. #define TWOHOUR (120*60)
  1441. lt6_tmp = ifa6->ia6_lifetime;
  1442. if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
  1443. remaininglifetime = ND6_INFINITE_LIFETIME;
  1444. else if (time_uptime - ifa6->ia6_updatetime >
  1445. lt6_tmp.ia6t_vltime) {
  1446. /*
  1447. * The case of "invalid" address. We should usually
  1448. * not see this case.
  1449. */
  1450. remaininglifetime = 0;
  1451. } else
  1452. remaininglifetime = lt6_tmp.ia6t_vltime -
  1453. (time_uptime - ifa6->ia6_updatetime);
  1454. /* when not updating, keep the current stored lifetime. */
  1455. lt6_tmp.ia6t_vltime = remaininglifetime;
  1456. if (TWOHOUR < new->ndpr_vltime ||
  1457. remaininglifetime < new->ndpr_vltime) {
  1458. lt6_tmp.ia6t_vltime = new->ndpr_vltime;
  1459. } else if (remaininglifetime <= TWOHOUR) {
  1460. if (auth) {
  1461. lt6_tmp.ia6t_vltime = new->ndpr_vltime;
  1462. }
  1463. } else {
  1464. /*
  1465. * new->ndpr_vltime <= TWOHOUR &&
  1466. * TWOHOUR < remaininglifetime
  1467. */
  1468. lt6_tmp.ia6t_vltime = TWOHOUR;
  1469. }
  1470. /* The 2 hour rule is not imposed for preferred lifetime. */
  1471. lt6_tmp.ia6t_pltime = new->ndpr_pltime;
  1472. in6_init_address_ltimes(pr, &lt6_tmp);
  1473. /*
  1474. * We need to treat lifetimes for temporary addresses
  1475. * differently, according to
  1476. * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
  1477. * we only update the lifetimes when they are in the maximum
  1478. * intervals.
  1479. */
  1480. if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
  1481. u_int32_t maxvltime, maxpltime;
  1482. if (V_ip6_temp_valid_lifetime >
  1483. (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
  1484. V_ip6_desync_factor)) {
  1485. maxvltime = V_ip6_temp_valid_lifetime -
  1486. (time_uptime - ifa6->ia6_createtime) -
  1487. V_ip6_desync_factor;
  1488. } else
  1489. maxvltime = 0;
  1490. if (V_ip6_temp_preferred_lifetime >
  1491. (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
  1492. V_ip6_desync_factor)) {
  1493. maxpltime = V_ip6_temp_preferred_lifetime -
  1494. (time_uptime - ifa6->ia6_createtime) -
  1495. V_ip6_desync_factor;
  1496. } else
  1497. maxpltime = 0;
  1498. if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
  1499. lt6_tmp.ia6t_vltime > maxvltime) {
  1500. lt6_tmp.ia6t_vltime = maxvltime;
  1501. }
  1502. if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
  1503. lt6_tmp.ia6t_pltime > maxpltime) {
  1504. lt6_tmp.ia6t_pltime = maxpltime;
  1505. }
  1506. }
  1507. ifa6->ia6_lifetime = lt6_tmp;
  1508. ifa6->ia6_updatetime = time_uptime;
  1509. }
  1510. if (ia6_match == NULL && new->ndpr_vltime) {
  1511. int ifidlen;
  1512. /*
  1513. * 5.5.3 (d) (continued)
  1514. * No address matched and the valid lifetime is non-zero.
  1515. * Create a new address.
  1516. */
  1517. /*
  1518. * Prefix Length check:
  1519. * If the sum of the prefix length and interface identifier
  1520. * length does not equal 128 bits, the Prefix Information
  1521. * option MUST be ignored. The length of the interface
  1522. * identifier is defined in a separate link-type specific
  1523. * document.
  1524. */
  1525. ifidlen = in6_if2idlen(ifp);
  1526. if (ifidlen < 0) {
  1527. /* this should not happen, so we always log it. */
  1528. log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
  1529. if_name(ifp));
  1530. goto end;
  1531. }
  1532. if (ifidlen + pr->ndpr_plen != 128) {
  1533. nd6log((LOG_INFO,
  1534. "%s: invalid prefixlen %d for %s, ignored\n",
  1535. __func__, pr->ndpr_plen, if_name(ifp)));
  1536. goto end;
  1537. }
  1538. if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
  1539. /*
  1540. * note that we should use pr (not new) for reference.
  1541. */
  1542. pr->ndpr_addrcnt++;
  1543. ia6->ia6_ndpr = pr;
  1544. /*
  1545. * RFC 3041 3.3 (2).
  1546. * When a new public address is created as described
  1547. * in RFC2462, also create a new temporary address.
  1548. *
  1549. * RFC 3041 3.5.
  1550. * When an interface connects to a new link, a new
  1551. * randomized interface identifier should be generated
  1552. * immediately together with a new set of temporary
  1553. * addresses. Thus, we specifiy 1 as the 2nd arg of
  1554. * in6_tmpifadd().
  1555. */
  1556. if (V_ip6_use_tempaddr) {
  1557. int e;
  1558. if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
  1559. nd6log((LOG_NOTICE, "%s: failed to "
  1560. "create a temporary address "
  1561. "(errno=%d)\n", __func__, e));
  1562. }
  1563. }
  1564. ifa_free(&ia6->ia_ifa);
  1565. /*
  1566. * A newly added address might affect the status
  1567. * of other addresses, so we check and update it.
  1568. * XXX: what if address duplication happens?
  1569. */
  1570. pfxlist_onlink_check();
  1571. } else {
  1572. /* just set an error. do not bark here. */
  1573. error = EADDRNOTAVAIL; /* XXX: might be unused. */
  1574. }
  1575. }
  1576. end:
  1577. if (pr != NULL)
  1578. nd6_prefix_rele(pr);
  1579. return (error);
  1580. }
  1581. /*
  1582. * A supplement function used in the on-link detection below;
  1583. * detect if a given prefix has a (probably) reachable advertising router.
  1584. * XXX: lengthy function name...
  1585. */
  1586. static struct nd_pfxrouter *
  1587. find_pfxlist_reachable_router(struct nd_prefix *pr)
  1588. {
  1589. struct epoch_tracker et;
  1590. struct nd_pfxrouter *pfxrtr;
  1591. struct llentry *ln;
  1592. int canreach;
  1593. ND6_LOCK_ASSERT();
  1594. NET_EPOCH_ENTER(et);
  1595. LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
  1596. ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
  1597. if (ln == NULL)
  1598. continue;
  1599. canreach = ND6_IS_LLINFO_PROBREACH(ln);
  1600. LLE_RUNLOCK(ln);
  1601. if (canreach)
  1602. break;
  1603. }
  1604. NET_EPOCH_EXIT(et);
  1605. return (pfxrtr);
  1606. }
  1607. /*
  1608. * Check if each prefix in the prefix list has at least one available router
  1609. * that advertised the prefix (a router is "available" if its neighbor cache
  1610. * entry is reachable or probably reachable).
  1611. * If the check fails, the prefix may be off-link, because, for example,
  1612. * we have moved from the network but the lifetime of the prefix has not
  1613. * expired yet. So we should not use the prefix if there is another prefix
  1614. * that has an available router.
  1615. * But, if there is no prefix that has an available router, we still regard
  1616. * all the prefixes as on-link. This is because we can't tell if all the
  1617. * routers are simply dead or if we really moved from the network and there
  1618. * is no router around us.
  1619. */
  1620. void
  1621. pfxlist_onlink_check(void)
  1622. {
  1623. struct nd_prefix *pr;
  1624. struct in6_ifaddr *ifa;
  1625. struct nd_defrouter *dr;
  1626. struct nd_pfxrouter *pfxrtr = NULL;
  1627. struct rm_priotracker in6_ifa_tracker;
  1628. uint64_t genid;
  1629. uint32_t flags;
  1630. ND6_ONLINK_LOCK();
  1631. ND6_RLOCK();
  1632. /*
  1633. * Check if there is a prefix that has a reachable advertising
  1634. * router.
  1635. */
  1636. LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
  1637. if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
  1638. break;
  1639. }
  1640. /*
  1641. * If we have no such prefix, check whether we still have a router
  1642. * that does not advertise any prefixes.
  1643. */
  1644. if (pr == NULL) {
  1645. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
  1646. struct nd_prefix *pr0;
  1647. LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
  1648. if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
  1649. break;
  1650. }
  1651. if (pfxrtr != NULL)
  1652. break;
  1653. }
  1654. }
  1655. if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
  1656. /*
  1657. * There is at least one prefix that has a reachable router,
  1658. * or at least a router which probably does not advertise
  1659. * any prefixes. The latter would be the case when we move
  1660. * to a new link where we have a router that does not provide
  1661. * prefixes and we configure an address by hand.
  1662. * Detach prefixes which have no reachable advertising
  1663. * router, and attach other prefixes.
  1664. */
  1665. LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
  1666. /* XXX: a link-local prefix should never be detached */
  1667. if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
  1668. pr->ndpr_raf_onlink == 0 ||
  1669. pr->ndpr_raf_auto == 0)
  1670. continue;
  1671. if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
  1672. find_pfxlist_reachable_router(pr) == NULL)
  1673. pr->ndpr_stateflags |= NDPRF_DETACHED;
  1674. else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
  1675. find_pfxlist_reachable_router(pr) != NULL)
  1676. pr->ndpr_stateflags &= ~NDPRF_DETACHED;
  1677. }
  1678. } else {
  1679. /* there is no prefix that has a reachable router */
  1680. LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
  1681. if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
  1682. pr->ndpr_raf_onlink == 0 ||
  1683. pr->ndpr_raf_auto == 0)
  1684. continue;
  1685. pr->ndpr_stateflags &= ~NDPRF_DETACHED;
  1686. }
  1687. }
  1688. /*
  1689. * Remove each interface route associated with a (just) detached
  1690. * prefix, and reinstall the interface route for a (just) attached
  1691. * prefix. Note that all attempt of reinstallation does not
  1692. * necessarily success, when a same prefix is shared among multiple
  1693. * interfaces. Such cases will be handled in nd6_prefix_onlink,
  1694. * so we don't have to care about them.
  1695. */
  1696. restart:
  1697. LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
  1698. char ip6buf[INET6_ADDRSTRLEN];
  1699. int e;
  1700. if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
  1701. pr->ndpr_raf_onlink == 0 ||
  1702. pr->ndpr_raf_auto == 0)
  1703. continue;
  1704. flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
  1705. if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
  1706. genid = V_nd6_list_genid;
  1707. ND6_RUNLOCK();
  1708. if ((flags & NDPRF_ONLINK) != 0 &&
  1709. (e = nd6_prefix_offlink(pr)) != 0) {
  1710. nd6log((LOG_ERR,
  1711. "%s: failed to make %s/%d offlink "
  1712. "(errno=%d)\n", __func__,
  1713. ip6_sprintf(ip6buf,
  1714. &pr->ndpr_prefix.sin6_addr),
  1715. pr->ndpr_plen, e));
  1716. } else if ((flags & NDPRF_ONLINK) == 0 &&
  1717. (e = nd6_prefix_onlink(pr)) != 0) {
  1718. nd6log((LOG_ERR,
  1719. "%s: failed to make %s/%d onlink "
  1720. "(errno=%d)\n", __func__,
  1721. ip6_sprintf(ip6buf,
  1722. &pr->ndpr_prefix.sin6_addr),
  1723. pr->ndpr_plen, e));
  1724. }
  1725. ND6_RLOCK();
  1726. if (genid != V_nd6_list_genid)
  1727. goto restart;
  1728. }
  1729. }
  1730. /*
  1731. * Changes on the prefix status might affect address status as well.
  1732. * Make sure that all addresses derived from an attached prefix are
  1733. * attached, and that all addresses derived from a detached prefix are
  1734. * detached. Note, however, that a manually configured address should
  1735. * always be attached.
  1736. * The precise detection logic is same as the one for prefixes.
  1737. */
  1738. IN6_IFADDR_RLOCK(&in6_ifa_tracker);
  1739. CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
  1740. if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
  1741. continue;
  1742. if (ifa->ia6_ndpr == NULL) {
  1743. /*
  1744. * This can happen when we first configure the address
  1745. * (i.e. the address exists, but the prefix does not).
  1746. * XXX: complicated relationships...
  1747. */
  1748. continue;
  1749. }
  1750. if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
  1751. break;
  1752. }
  1753. if (ifa) {
  1754. CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
  1755. if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
  1756. continue;
  1757. if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
  1758. continue;
  1759. if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
  1760. if (ifa->ia6_flags & IN6_IFF_DETACHED) {
  1761. ifa->ia6_flags &= ~IN6_IFF_DETACHED;
  1762. ifa->ia6_flags |= IN6_IFF_TENTATIVE;
  1763. nd6_dad_start((struct ifaddr *)ifa, 0);
  1764. }
  1765. } else {
  1766. ifa->ia6_flags |= IN6_IFF_DETACHED;
  1767. }
  1768. }
  1769. } else {
  1770. CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
  1771. if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
  1772. continue;
  1773. if (ifa->ia6_flags & IN6_IFF_DETACHED) {
  1774. ifa->ia6_flags &= ~IN6_IFF_DETACHED;
  1775. ifa->ia6_flags |= IN6_IFF_TENTATIVE;
  1776. /* Do we need a delay in this case? */
  1777. nd6_dad_start((struct ifaddr *)ifa, 0);
  1778. }
  1779. }
  1780. }
  1781. IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
  1782. ND6_RUNLOCK();
  1783. ND6_ONLINK_UNLOCK();
  1784. }
  1785. /*
  1786. * Add or remove interface route specified by @dst, @netmask and @ifp.
  1787. * ifa can be NULL.
  1788. * Returns 0 on success
  1789. */
  1790. static int
  1791. nd6_prefix_rtrequest(uint32_t fibnum, int cmd, struct sockaddr_in6 *dst,
  1792. struct sockaddr_in6 *netmask, struct ifnet *ifp, struct ifaddr *ifa)
  1793. {
  1794. struct epoch_tracker et;
  1795. int error;
  1796. /* Prepare gateway */
  1797. struct sockaddr_dl_short sdl = {
  1798. .sdl_family = AF_LINK,
  1799. .sdl_len = sizeof(struct sockaddr_dl_short),
  1800. .sdl_type = ifp->if_type,
  1801. .sdl_index = ifp->if_index,
  1802. };
  1803. struct rt_addrinfo info = {
  1804. .rti_ifa = ifa,
  1805. .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
  1806. .rti_info = {
  1807. [RTAX_DST] = (struct sockaddr *)dst,
  1808. [RTAX_NETMASK] = (struct sockaddr *)netmask,
  1809. [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
  1810. },
  1811. };
  1812. /* Don't set additional per-gw filters on removal */
  1813. NET_EPOCH_ENTER(et);
  1814. error = rib_handle_ifaddr_info(fibnum, cmd, &info);
  1815. NET_EPOCH_EXIT(et);
  1816. return (error);
  1817. }
  1818. static int
  1819. nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
  1820. {
  1821. int error;
  1822. struct sockaddr_in6 mask6 = {
  1823. .sin6_family = AF_INET6,
  1824. .sin6_len = sizeof(struct sockaddr_in6),
  1825. .sin6_addr = pr->ndpr_mask,
  1826. };
  1827. struct sockaddr_in6 *pmask6 = (pr->ndpr_plen != 128) ? &mask6 : NULL;
  1828. error = nd6_prefix_rtrequest(pr->ndpr_ifp->if_fib, RTM_ADD,
  1829. &pr->ndpr_prefix, pmask6, pr->ndpr_ifp, ifa);
  1830. if (error == 0)
  1831. pr->ndpr_stateflags |= NDPRF_ONLINK;
  1832. return (error);
  1833. }
  1834. static int
  1835. nd6_prefix_onlink(struct nd_prefix *pr)
  1836. {
  1837. struct epoch_tracker et;
  1838. struct ifaddr *ifa;
  1839. struct ifnet *ifp = pr->ndpr_ifp;
  1840. struct nd_prefix *opr;
  1841. char ip6buf[INET6_ADDRSTRLEN];
  1842. int error;
  1843. ND6_ONLINK_LOCK_ASSERT();
  1844. ND6_UNLOCK_ASSERT();
  1845. if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
  1846. return (EEXIST);
  1847. /*
  1848. * Add the interface route associated with the prefix. Before
  1849. * installing the route, check if there's the same prefix on another
  1850. * interface, and the prefix has already installed the interface route.
  1851. * Although such a configuration is expected to be rare, we explicitly
  1852. * allow it.
  1853. */
  1854. ND6_RLOCK();
  1855. LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
  1856. if (opr == pr)
  1857. continue;
  1858. if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
  1859. continue;
  1860. if (!V_rt_add_addr_allfibs &&
  1861. opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
  1862. continue;
  1863. if (opr->ndpr_plen == pr->ndpr_plen &&
  1864. in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
  1865. &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
  1866. ND6_RUNLOCK();
  1867. return (0);
  1868. }
  1869. }
  1870. ND6_RUNLOCK();
  1871. /*
  1872. * We prefer link-local addresses as the associated interface address.
  1873. */
  1874. /* search for a link-local addr */
  1875. NET_EPOCH_ENTER(et);
  1876. ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
  1877. IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
  1878. if (ifa == NULL) {
  1879. /* XXX: freebsd does not have ifa_ifwithaf */
  1880. CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  1881. if (ifa->ifa_addr->sa_family == AF_INET6) {
  1882. ifa_ref(ifa);
  1883. break;
  1884. }
  1885. }
  1886. /* should we care about ia6_flags? */
  1887. }
  1888. if (ifa == NULL) {
  1889. /*
  1890. * This can still happen, when, for example, we receive an RA
  1891. * containing a prefix with the L bit set and the A bit clear,
  1892. * after removing all IPv6 addresses on the receiving
  1893. * interface. This should, of course, be rare though.
  1894. */
  1895. nd6log((LOG_NOTICE,
  1896. "%s: failed to find any ifaddr to add route for a "
  1897. "prefix(%s/%d) on %s\n", __func__,
  1898. ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
  1899. pr->ndpr_plen, if_name(ifp)));
  1900. error = 0;
  1901. } else {
  1902. error = nd6_prefix_onlink_rtrequest(pr, ifa);
  1903. ifa_free(ifa);
  1904. }
  1905. NET_EPOCH_EXIT(et);
  1906. return (error);
  1907. }
  1908. int
  1909. nd6_prefix_offlink(struct nd_prefix *pr)
  1910. {
  1911. int error = 0;
  1912. struct ifnet *ifp = pr->ndpr_ifp;
  1913. struct nd_prefix *opr;
  1914. struct sockaddr_in6 sa6;
  1915. char ip6buf[INET6_ADDRSTRLEN];
  1916. uint64_t genid;
  1917. int a_failure;
  1918. ND6_ONLINK_LOCK_ASSERT();
  1919. ND6_UNLOCK_ASSERT();
  1920. if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
  1921. return (EEXIST);
  1922. struct sockaddr_in6 mask6 = {
  1923. .sin6_family = AF_INET6,
  1924. .sin6_len = sizeof(struct sockaddr_in6),
  1925. .sin6_addr = pr->ndpr_mask,
  1926. };
  1927. struct sockaddr_in6 *pmask6 = (pr->ndpr_plen != 128) ? &mask6 : NULL;
  1928. error = nd6_prefix_rtrequest(ifp->if_fib, RTM_DELETE,
  1929. &pr->ndpr_prefix, pmask6, ifp, NULL);
  1930. a_failure = 1;
  1931. if (error == 0) {
  1932. pr->ndpr_stateflags &= ~NDPRF_ONLINK;
  1933. /*
  1934. * There might be the same prefix on another interface,
  1935. * the prefix which could not be on-link just because we have
  1936. * the interface route (see comments in nd6_prefix_onlink).
  1937. * If there's one, try to make the prefix on-link on the
  1938. * interface.
  1939. */
  1940. ND6_RLOCK();
  1941. restart:
  1942. LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
  1943. /*
  1944. * KAME specific: detached prefixes should not be
  1945. * on-link.
  1946. */
  1947. if (opr == pr || (opr->ndpr_stateflags &
  1948. (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
  1949. continue;
  1950. if (opr->ndpr_plen == pr->ndpr_plen &&
  1951. in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
  1952. &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
  1953. int e;
  1954. genid = V_nd6_list_genid;
  1955. ND6_RUNLOCK();
  1956. if ((e = nd6_prefix_onlink(opr)) != 0) {
  1957. nd6log((LOG_ERR,
  1958. "%s: failed to recover a prefix "
  1959. "%s/%d from %s to %s (errno=%d)\n",
  1960. __func__, ip6_sprintf(ip6buf,
  1961. &opr->ndpr_prefix.sin6_addr),
  1962. opr->ndpr_plen, if_name(ifp),
  1963. if_name(opr->ndpr_ifp), e));
  1964. } else
  1965. a_failure = 0;
  1966. ND6_RLOCK();
  1967. if (genid != V_nd6_list_genid)
  1968. goto restart;
  1969. }
  1970. }
  1971. ND6_RUNLOCK();
  1972. } else {
  1973. /* XXX: can we still set the NDPRF_ONLINK flag? */
  1974. nd6log((LOG_ERR,
  1975. "%s: failed to delete route: %s/%d on %s (errno=%d)\n",
  1976. __func__, ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
  1977. pr->ndpr_plen, if_name(ifp), error));
  1978. }
  1979. if (a_failure)
  1980. lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
  1981. (struct sockaddr *)&mask6, LLE_STATIC);
  1982. return (error);
  1983. }
  1984. /*
  1985. * ia0 - corresponding public address
  1986. */
  1987. int
  1988. in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
  1989. {
  1990. struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
  1991. struct in6_ifaddr *newia;
  1992. struct in6_aliasreq ifra;
  1993. int error;
  1994. int trylimit = 3; /* XXX: adhoc value */
  1995. int updateflags;
  1996. u_int32_t randid[2];
  1997. time_t vltime0, pltime0;
  1998. in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
  1999. &ia0->ia_prefixmask.sin6_addr);
  2000. ifra.ifra_addr = ia0->ia_addr; /* XXX: do we need this ? */
  2001. /* clear the old IFID */
  2002. IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
  2003. &ifra.ifra_prefixmask.sin6_addr);
  2004. again:
  2005. if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
  2006. (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
  2007. nd6log((LOG_NOTICE, "%s: failed to find a good random IFID\n",
  2008. __func__));
  2009. return (EINVAL);
  2010. }
  2011. ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
  2012. (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
  2013. ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
  2014. (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
  2015. /*
  2016. * in6_get_tmpifid() quite likely provided a unique interface ID.
  2017. * However, we may still have a chance to see collision, because
  2018. * there may be a time lag between generation of the ID and generation
  2019. * of the address. So, we'll do one more sanity check.
  2020. */
  2021. if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
  2022. if (trylimit-- > 0) {
  2023. forcegen = 1;
  2024. goto again;
  2025. }
  2026. /* Give up. Something strange should have happened. */
  2027. nd6log((LOG_NOTICE, "%s: failed to find a unique random IFID\n",
  2028. __func__));
  2029. return (EEXIST);
  2030. }
  2031. /*
  2032. * The Valid Lifetime is the lower of the Valid Lifetime of the
  2033. * public address or TEMP_VALID_LIFETIME.
  2034. * The Preferred Lifetime is the lower of the Preferred Lifetime
  2035. * of the public address or TEMP_PREFERRED_LIFETIME -
  2036. * DESYNC_FACTOR.
  2037. */
  2038. if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
  2039. vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
  2040. (ia0->ia6_lifetime.ia6t_vltime -
  2041. (time_uptime - ia0->ia6_updatetime));
  2042. if (vltime0 > V_ip6_temp_valid_lifetime)
  2043. vltime0 = V_ip6_temp_valid_lifetime;
  2044. } else
  2045. vltime0 = V_ip6_temp_valid_lifetime;
  2046. if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
  2047. pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
  2048. (ia0->ia6_lifetime.ia6t_pltime -
  2049. (time_uptime - ia0->ia6_updatetime));
  2050. if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
  2051. pltime0 = V_ip6_temp_preferred_lifetime -
  2052. V_ip6_desync_factor;
  2053. }
  2054. } else
  2055. pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
  2056. ifra.ifra_lifetime.ia6t_vltime = vltime0;
  2057. ifra.ifra_lifetime.ia6t_pltime = pltime0;
  2058. /*
  2059. * A temporary address is created only if this calculated Preferred
  2060. * Lifetime is greater than REGEN_ADVANCE time units.
  2061. */
  2062. if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
  2063. return (0);
  2064. /* XXX: scope zone ID? */
  2065. ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
  2066. /* allocate ifaddr structure, link into chain, etc. */
  2067. updateflags = 0;
  2068. if (delay)
  2069. updateflags |= IN6_IFAUPDATE_DADDELAY;
  2070. if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
  2071. return (error);
  2072. newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
  2073. if (newia == NULL) { /* XXX: can it happen? */
  2074. nd6log((LOG_ERR,
  2075. "%s: ifa update succeeded, but we got no ifaddr\n",
  2076. __func__));
  2077. return (EINVAL); /* XXX */
  2078. }
  2079. newia->ia6_ndpr = ia0->ia6_ndpr;
  2080. newia->ia6_ndpr->ndpr_addrcnt++;
  2081. ifa_free(&newia->ia_ifa);
  2082. /*
  2083. * A newly added address might affect the status of other addresses.
  2084. * XXX: when the temporary address is generated with a new public
  2085. * address, the onlink check is redundant. However, it would be safe
  2086. * to do the check explicitly everywhere a new address is generated,
  2087. * and, in fact, we surely need the check when we create a new
  2088. * temporary address due to deprecation of an old temporary address.
  2089. */
  2090. pfxlist_onlink_check();
  2091. return (0);
  2092. }
  2093. static int
  2094. rt6_deleteroute(const struct rtentry *rt, const struct nhop_object *nh,
  2095. void *arg)
  2096. {
  2097. struct in6_addr *gate = (struct in6_addr *)arg;
  2098. int nh_rt_flags;
  2099. if (nh->gw_sa.sa_family != AF_INET6)
  2100. return (0);
  2101. if (!IN6_ARE_ADDR_EQUAL(gate, &nh->gw6_sa.sin6_addr)) {
  2102. return (0);
  2103. }
  2104. /*
  2105. * Do not delete a static route.
  2106. * XXX: this seems to be a bit ad-hoc. Should we consider the
  2107. * 'cloned' bit instead?
  2108. */
  2109. nh_rt_flags = nhop_get_rtflags(nh);
  2110. if ((nh_rt_flags & RTF_STATIC) != 0)
  2111. return (0);
  2112. /*
  2113. * We delete only host route. This means, in particular, we don't
  2114. * delete default route.
  2115. */
  2116. if ((nh_rt_flags & RTF_HOST) == 0)
  2117. return (0);
  2118. return (1);
  2119. #undef SIN6
  2120. }
  2121. /*
  2122. * Delete all the routing table entries that use the specified gateway.
  2123. * XXX: this function causes search through all entries of routing table, so
  2124. * it shouldn't be called when acting as a router.
  2125. */
  2126. void
  2127. rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
  2128. {
  2129. /* We'll care only link-local addresses */
  2130. if (!IN6_IS_ADDR_LINKLOCAL(gateway))
  2131. return;
  2132. /* XXX Do we really need to walk any but the default FIB? */
  2133. rib_foreach_table_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
  2134. }
  2135. int
  2136. nd6_setdefaultiface(int ifindex)
  2137. {
  2138. int error = 0;
  2139. if (ifindex < 0 || V_if_index < ifindex)
  2140. return (EINVAL);
  2141. if (ifindex != 0 && !ifnet_byindex(ifindex))
  2142. return (EINVAL);
  2143. if (V_nd6_defifindex != ifindex) {
  2144. V_nd6_defifindex = ifindex;
  2145. if (V_nd6_defifindex > 0)
  2146. V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
  2147. else
  2148. V_nd6_defifp = NULL;
  2149. /*
  2150. * Our current implementation assumes one-to-one maping between
  2151. * interfaces and links, so it would be natural to use the
  2152. * default interface as the default link.
  2153. */
  2154. scope6_setdefault(V_nd6_defifp);
  2155. }
  2156. return (error);
  2157. }
  2158. bool
  2159. nd6_defrouter_list_empty(void)
  2160. {
  2161. return (TAILQ_EMPTY(&V_nd6_defrouter));
  2162. }
  2163. void
  2164. nd6_defrouter_timer(void)
  2165. {
  2166. struct nd_defrouter *dr, *ndr;
  2167. struct nd6_drhead drq;
  2168. TAILQ_INIT(&drq);
  2169. ND6_WLOCK();
  2170. TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
  2171. if (dr->expire && dr->expire < time_uptime)
  2172. defrouter_unlink(dr, &drq);
  2173. ND6_WUNLOCK();
  2174. while ((dr = TAILQ_FIRST(&drq)) != NULL) {
  2175. TAILQ_REMOVE(&drq, dr, dr_entry);
  2176. defrouter_del(dr);
  2177. }
  2178. }
  2179. /*
  2180. * Nuke default router list entries toward ifp.
  2181. * We defer removal of default router list entries that is installed in the
  2182. * routing table, in order to keep additional side effects as small as possible.
  2183. */
  2184. void
  2185. nd6_defrouter_purge(struct ifnet *ifp)
  2186. {
  2187. struct nd_defrouter *dr, *ndr;
  2188. struct nd6_drhead drq;
  2189. TAILQ_INIT(&drq);
  2190. ND6_WLOCK();
  2191. TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
  2192. if (dr->installed)
  2193. continue;
  2194. if (dr->ifp == ifp)
  2195. defrouter_unlink(dr, &drq);
  2196. }
  2197. TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
  2198. if (!dr->installed)
  2199. continue;
  2200. if (dr->ifp == ifp)
  2201. defrouter_unlink(dr, &drq);
  2202. }
  2203. ND6_WUNLOCK();
  2204. /* Delete the unlinked router objects. */
  2205. while ((dr = TAILQ_FIRST(&drq)) != NULL) {
  2206. TAILQ_REMOVE(&drq, dr, dr_entry);
  2207. defrouter_del(dr);
  2208. }
  2209. }
  2210. void
  2211. nd6_defrouter_flush_all(void)
  2212. {
  2213. struct nd_defrouter *dr;
  2214. struct nd6_drhead drq;
  2215. TAILQ_INIT(&drq);
  2216. ND6_WLOCK();
  2217. while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
  2218. defrouter_unlink(dr, &drq);
  2219. ND6_WUNLOCK();
  2220. while ((dr = TAILQ_FIRST(&drq)) != NULL) {
  2221. TAILQ_REMOVE(&drq, dr, dr_entry);
  2222. defrouter_del(dr);
  2223. }
  2224. }
  2225. void
  2226. nd6_defrouter_init(void)
  2227. {
  2228. TAILQ_INIT(&V_nd6_defrouter);
  2229. }
  2230. static int
  2231. nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
  2232. {
  2233. struct in6_defrouter d;
  2234. struct nd_defrouter *dr;
  2235. int error;
  2236. if (req->newptr != NULL)
  2237. return (EPERM);
  2238. error = sysctl_wire_old_buffer(req, 0);
  2239. if (error != 0)
  2240. return (error);
  2241. bzero(&d, sizeof(d));
  2242. d.rtaddr.sin6_family = AF_INET6;
  2243. d.rtaddr.sin6_len = sizeof(d.rtaddr);
  2244. ND6_RLOCK();
  2245. TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
  2246. d.rtaddr.sin6_addr = dr->rtaddr;
  2247. error = sa6_recoverscope(&d.rtaddr);
  2248. if (error != 0)
  2249. break;
  2250. d.flags = dr->raflags;
  2251. d.rtlifetime = dr->rtlifetime;
  2252. d.expire = dr->expire + (time_second - time_uptime);
  2253. d.if_index = dr->ifp->if_index;
  2254. error = SYSCTL_OUT(req, &d, sizeof(d));
  2255. if (error != 0)
  2256. break;
  2257. }
  2258. ND6_RUNLOCK();
  2259. return (error);
  2260. }
  2261. SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
  2262. CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
  2263. NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
  2264. "NDP default router list");