nd6.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. /* $OpenBSD: nd6.c,v 1.143 2015/07/16 15:31:35 mpi Exp $ */
  2. /* $KAME: nd6.c,v 1.280 2002/06/08 19:52:07 itojun 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/timeout.h>
  34. #include <sys/malloc.h>
  35. #include <sys/mbuf.h>
  36. #include <sys/socket.h>
  37. #include <sys/sockio.h>
  38. #include <sys/time.h>
  39. #include <sys/kernel.h>
  40. #include <sys/protosw.h>
  41. #include <sys/errno.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/syslog.h>
  44. #include <sys/queue.h>
  45. #include <sys/task.h>
  46. #include <net/if.h>
  47. #include <net/if_dl.h>
  48. #include <net/if_types.h>
  49. #include <net/route.h>
  50. #include <netinet/in.h>
  51. #include <netinet/if_ether.h>
  52. #include <netinet/ip_ipsp.h>
  53. #include <netinet6/in6_var.h>
  54. #include <netinet/ip6.h>
  55. #include <netinet6/ip6_var.h>
  56. #include <netinet6/nd6.h>
  57. #include <netinet/icmp6.h>
  58. #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
  59. #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
  60. #define SDL(s) ((struct sockaddr_dl *)s)
  61. /* timer values */
  62. int nd6_prune = 1; /* walk list every 1 seconds */
  63. int nd6_delay = 5; /* delay first probe time 5 second */
  64. int nd6_umaxtries = 3; /* maximum unicast query */
  65. int nd6_mmaxtries = 3; /* maximum multicast query */
  66. int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */
  67. /* preventing too many loops in ND option parsing */
  68. int nd6_maxndopt = 10; /* max # of ND options allowed */
  69. int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */
  70. #ifdef ND6_DEBUG
  71. int nd6_debug = 1;
  72. #else
  73. int nd6_debug = 0;
  74. #endif
  75. static int nd6_inuse, nd6_allocated;
  76. struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
  77. struct nd_drhead nd_defrouter;
  78. struct nd_prhead nd_prefix = { 0 };
  79. int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
  80. void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
  81. void nd6_slowtimo(void *);
  82. struct llinfo_nd6 *nd6_free(struct rtentry *, int);
  83. void nd6_llinfo_timer(void *);
  84. struct timeout nd6_slowtimo_ch;
  85. struct timeout nd6_timer_ch;
  86. struct task nd6_timer_task;
  87. void nd6_timer_work(void *);
  88. int fill_drlist(void *, size_t *, size_t);
  89. int fill_prlist(void *, size_t *, size_t);
  90. #define LN_DEQUEUE(ln) do { \
  91. (ln)->ln_next->ln_prev = (ln)->ln_prev; \
  92. (ln)->ln_prev->ln_next = (ln)->ln_next; \
  93. } while (0)
  94. #define LN_INSERTHEAD(ln) do { \
  95. (ln)->ln_next = llinfo_nd6.ln_next; \
  96. llinfo_nd6.ln_next = (ln); \
  97. (ln)->ln_prev = &llinfo_nd6; \
  98. (ln)->ln_next->ln_prev = (ln); \
  99. } while (0)
  100. void
  101. nd6_init(void)
  102. {
  103. static int nd6_init_done = 0;
  104. if (nd6_init_done) {
  105. log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
  106. return;
  107. }
  108. /* initialization of the default router list */
  109. TAILQ_INIT(&nd_defrouter);
  110. task_set(&nd6_timer_task, nd6_timer_work, NULL);
  111. nd6_init_done = 1;
  112. /* start timer */
  113. timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
  114. timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
  115. nd6_rs_init();
  116. }
  117. struct nd_ifinfo *
  118. nd6_ifattach(struct ifnet *ifp)
  119. {
  120. struct nd_ifinfo *nd;
  121. nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO);
  122. nd->initialized = 1;
  123. nd->chlim = IPV6_DEFHLIM;
  124. nd->basereachable = REACHABLE_TIME;
  125. nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
  126. nd->retrans = RETRANS_TIMER;
  127. /* per-interface IFXF_AUTOCONF6 needs to be set too to accept RAs */
  128. nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
  129. /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
  130. nd6_setmtu0(ifp, nd);
  131. return nd;
  132. }
  133. void
  134. nd6_ifdetach(struct nd_ifinfo *nd)
  135. {
  136. free(nd, M_IP6NDP, 0);
  137. }
  138. void
  139. nd6_setmtu(struct ifnet *ifp)
  140. {
  141. nd6_setmtu0(ifp, ND_IFINFO(ifp));
  142. }
  143. void
  144. nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
  145. {
  146. u_int32_t omaxmtu;
  147. omaxmtu = ndi->maxmtu;
  148. ndi->maxmtu = ifp->if_mtu;
  149. /*
  150. * Decreasing the interface MTU under IPV6 minimum MTU may cause
  151. * undesirable situation. We thus notify the operator of the change
  152. * explicitly. The check for omaxmtu is necessary to restrict the
  153. * log to the case of changing the MTU, not initializing it.
  154. */
  155. if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
  156. log(LOG_NOTICE, "nd6_setmtu0: "
  157. "new link MTU on %s (%lu) is too small for IPv6\n",
  158. ifp->if_xname, (unsigned long)ndi->maxmtu);
  159. }
  160. }
  161. void
  162. nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
  163. {
  164. bzero(ndopts, sizeof(*ndopts));
  165. ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
  166. ndopts->nd_opts_last
  167. = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
  168. if (icmp6len == 0) {
  169. ndopts->nd_opts_done = 1;
  170. ndopts->nd_opts_search = NULL;
  171. }
  172. }
  173. /*
  174. * Take one ND option.
  175. */
  176. struct nd_opt_hdr *
  177. nd6_option(union nd_opts *ndopts)
  178. {
  179. struct nd_opt_hdr *nd_opt;
  180. int olen;
  181. if (!ndopts)
  182. panic("ndopts == NULL in nd6_option");
  183. if (!ndopts->nd_opts_last)
  184. panic("uninitialized ndopts in nd6_option");
  185. if (!ndopts->nd_opts_search)
  186. return NULL;
  187. if (ndopts->nd_opts_done)
  188. return NULL;
  189. nd_opt = ndopts->nd_opts_search;
  190. /* make sure nd_opt_len is inside the buffer */
  191. if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
  192. bzero(ndopts, sizeof(*ndopts));
  193. return NULL;
  194. }
  195. olen = nd_opt->nd_opt_len << 3;
  196. if (olen == 0) {
  197. /*
  198. * Message validation requires that all included
  199. * options have a length that is greater than zero.
  200. */
  201. bzero(ndopts, sizeof(*ndopts));
  202. return NULL;
  203. }
  204. ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
  205. if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
  206. /* option overruns the end of buffer, invalid */
  207. bzero(ndopts, sizeof(*ndopts));
  208. return NULL;
  209. } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
  210. /* reached the end of options chain */
  211. ndopts->nd_opts_done = 1;
  212. ndopts->nd_opts_search = NULL;
  213. }
  214. return nd_opt;
  215. }
  216. /*
  217. * Parse multiple ND options.
  218. * This function is much easier to use, for ND routines that do not need
  219. * multiple options of the same type.
  220. */
  221. int
  222. nd6_options(union nd_opts *ndopts)
  223. {
  224. struct nd_opt_hdr *nd_opt;
  225. int i = 0;
  226. if (!ndopts)
  227. panic("ndopts == NULL in nd6_options");
  228. if (!ndopts->nd_opts_last)
  229. panic("uninitialized ndopts in nd6_options");
  230. if (!ndopts->nd_opts_search)
  231. return 0;
  232. while (1) {
  233. nd_opt = nd6_option(ndopts);
  234. if (!nd_opt && !ndopts->nd_opts_last) {
  235. /*
  236. * Message validation requires that all included
  237. * options have a length that is greater than zero.
  238. */
  239. icmp6stat.icp6s_nd_badopt++;
  240. bzero(ndopts, sizeof(*ndopts));
  241. return -1;
  242. }
  243. if (!nd_opt)
  244. goto skip1;
  245. switch (nd_opt->nd_opt_type) {
  246. case ND_OPT_SOURCE_LINKADDR:
  247. case ND_OPT_TARGET_LINKADDR:
  248. case ND_OPT_MTU:
  249. case ND_OPT_REDIRECTED_HEADER:
  250. if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
  251. nd6log((LOG_INFO,
  252. "duplicated ND6 option found (type=%d)\n",
  253. nd_opt->nd_opt_type));
  254. /* XXX bark? */
  255. } else {
  256. ndopts->nd_opt_array[nd_opt->nd_opt_type]
  257. = nd_opt;
  258. }
  259. break;
  260. case ND_OPT_PREFIX_INFORMATION:
  261. if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
  262. ndopts->nd_opt_array[nd_opt->nd_opt_type]
  263. = nd_opt;
  264. }
  265. ndopts->nd_opts_pi_end =
  266. (struct nd_opt_prefix_info *)nd_opt;
  267. break;
  268. default:
  269. /*
  270. * Unknown options must be silently ignored,
  271. * to accommodate future extension to the protocol.
  272. */
  273. nd6log((LOG_DEBUG,
  274. "nd6_options: unsupported option %d - "
  275. "option ignored\n", nd_opt->nd_opt_type));
  276. }
  277. skip1:
  278. i++;
  279. if (i > nd6_maxndopt) {
  280. icmp6stat.icp6s_nd_toomanyopt++;
  281. nd6log((LOG_INFO, "too many loop in nd opt\n"));
  282. break;
  283. }
  284. if (ndopts->nd_opts_done)
  285. break;
  286. }
  287. return 0;
  288. }
  289. /*
  290. * ND6 timer routine to handle ND6 entries
  291. */
  292. void
  293. nd6_llinfo_settimer(struct llinfo_nd6 *ln, long tick)
  294. {
  295. int s;
  296. s = splsoftnet();
  297. if (tick < 0) {
  298. ln->ln_expire = 0;
  299. ln->ln_ntick = 0;
  300. timeout_del(&ln->ln_timer_ch);
  301. } else {
  302. ln->ln_expire = time_second + tick / hz;
  303. if (tick > INT_MAX) {
  304. ln->ln_ntick = tick - INT_MAX;
  305. timeout_add(&ln->ln_timer_ch, INT_MAX);
  306. } else {
  307. ln->ln_ntick = 0;
  308. timeout_add(&ln->ln_timer_ch, tick);
  309. }
  310. }
  311. splx(s);
  312. }
  313. void
  314. nd6_llinfo_timer(void *arg)
  315. {
  316. int s;
  317. struct llinfo_nd6 *ln;
  318. struct rtentry *rt;
  319. struct sockaddr_in6 *dst;
  320. struct ifnet *ifp;
  321. struct nd_ifinfo *ndi = NULL;
  322. s = splsoftnet();
  323. ln = (struct llinfo_nd6 *)arg;
  324. if (ln->ln_ntick > 0) {
  325. if (ln->ln_ntick > INT_MAX) {
  326. ln->ln_ntick -= INT_MAX;
  327. nd6_llinfo_settimer(ln, INT_MAX);
  328. } else {
  329. ln->ln_ntick = 0;
  330. nd6_llinfo_settimer(ln, ln->ln_ntick);
  331. }
  332. splx(s);
  333. return;
  334. }
  335. if ((rt = ln->ln_rt) == NULL)
  336. panic("ln->ln_rt == NULL");
  337. if ((ifp = rt->rt_ifp) == NULL)
  338. panic("ln->ln_rt->rt_ifp == NULL");
  339. ndi = ND_IFINFO(ifp);
  340. dst = satosin6(rt_key(rt));
  341. /* sanity check */
  342. if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
  343. panic("rt_llinfo(%p) is not equal to ln(%p)",
  344. rt->rt_llinfo, ln);
  345. if (!dst)
  346. panic("dst=0 in nd6_timer(ln=%p)", ln);
  347. switch (ln->ln_state) {
  348. case ND6_LLINFO_INCOMPLETE:
  349. if (ln->ln_asked < nd6_mmaxtries) {
  350. ln->ln_asked++;
  351. nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
  352. nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
  353. } else {
  354. struct mbuf *m = ln->ln_hold;
  355. if (m) {
  356. ln->ln_hold = NULL;
  357. /*
  358. * Fake rcvif to make the ICMP error
  359. * more helpful in diagnosing for the
  360. * receiver.
  361. * XXX: should we consider
  362. * older rcvif?
  363. */
  364. m->m_pkthdr.ph_ifidx = rt->rt_ifp->if_index;
  365. icmp6_error(m, ICMP6_DST_UNREACH,
  366. ICMP6_DST_UNREACH_ADDR, 0);
  367. if (ln->ln_hold == m) {
  368. /* m is back in ln_hold. Discard. */
  369. m_freem(ln->ln_hold);
  370. ln->ln_hold = NULL;
  371. }
  372. }
  373. (void)nd6_free(rt, 0);
  374. ln = NULL;
  375. }
  376. break;
  377. case ND6_LLINFO_REACHABLE:
  378. if (!ND6_LLINFO_PERMANENT(ln)) {
  379. ln->ln_state = ND6_LLINFO_STALE;
  380. nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  381. }
  382. break;
  383. case ND6_LLINFO_STALE:
  384. case ND6_LLINFO_PURGE:
  385. /* Garbage Collection(RFC 2461 5.3) */
  386. if (!ND6_LLINFO_PERMANENT(ln)) {
  387. (void)nd6_free(rt, 1);
  388. ln = NULL;
  389. }
  390. break;
  391. case ND6_LLINFO_DELAY:
  392. if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
  393. /* We need NUD */
  394. ln->ln_asked = 1;
  395. ln->ln_state = ND6_LLINFO_PROBE;
  396. nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
  397. nd6_ns_output(ifp, &dst->sin6_addr,
  398. &dst->sin6_addr, ln, 0);
  399. } else {
  400. ln->ln_state = ND6_LLINFO_STALE; /* XXX */
  401. nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  402. }
  403. break;
  404. case ND6_LLINFO_PROBE:
  405. if (ln->ln_asked < nd6_umaxtries) {
  406. ln->ln_asked++;
  407. nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
  408. nd6_ns_output(ifp, &dst->sin6_addr,
  409. &dst->sin6_addr, ln, 0);
  410. } else {
  411. (void)nd6_free(rt, 0);
  412. ln = NULL;
  413. }
  414. break;
  415. }
  416. splx(s);
  417. }
  418. /*
  419. * ND6 timer routine to expire default route list and prefix list
  420. */
  421. void
  422. nd6_timer_work(void *null)
  423. {
  424. int s;
  425. struct nd_defrouter *dr, *ndr;
  426. struct nd_prefix *pr, *npr;
  427. struct in6_ifaddr *ia6, *nia6;
  428. s = splsoftnet();
  429. timeout_set(&nd6_timer_ch, nd6_timer, NULL);
  430. timeout_add_sec(&nd6_timer_ch, nd6_prune);
  431. /* expire default router list */
  432. TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
  433. if (dr->expire && dr->expire < time_second)
  434. defrtrlist_del(dr);
  435. /*
  436. * expire interface addresses.
  437. * in the past the loop was inside prefix expiry processing.
  438. * However, from a stricter spec-conformance standpoint, we should
  439. * rather separate address lifetimes and prefix lifetimes.
  440. */
  441. TAILQ_FOREACH_SAFE(ia6, &in6_ifaddr, ia_list, nia6) {
  442. /* check address lifetime */
  443. if (IFA6_IS_INVALID(ia6)) {
  444. in6_purgeaddr(&ia6->ia_ifa);
  445. } else if (IFA6_IS_DEPRECATED(ia6)) {
  446. ia6->ia6_flags |= IN6_IFF_DEPRECATED;
  447. } else {
  448. /*
  449. * A new RA might have made a deprecated address
  450. * preferred.
  451. */
  452. ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
  453. }
  454. }
  455. /* expire prefix list */
  456. LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
  457. /*
  458. * check prefix lifetime.
  459. * since pltime is just for autoconf, pltime processing for
  460. * prefix is not necessary.
  461. */
  462. if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
  463. time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
  464. /*
  465. * address expiration and prefix expiration are
  466. * separate. NEVER perform in6_purgeaddr here.
  467. */
  468. prelist_remove(pr);
  469. }
  470. }
  471. splx(s);
  472. }
  473. void
  474. nd6_timer(void *ignored_arg)
  475. {
  476. task_add(systq, &nd6_timer_task);
  477. }
  478. /*
  479. * Nuke neighbor cache/prefix/default router management table, right before
  480. * ifp goes away.
  481. */
  482. void
  483. nd6_purge(struct ifnet *ifp)
  484. {
  485. struct llinfo_nd6 *ln, *nln;
  486. struct nd_defrouter *dr, *ndr;
  487. struct nd_prefix *pr, *npr;
  488. /*
  489. * Nuke default router list entries toward ifp.
  490. * We defer removal of default router list entries that is installed
  491. * in the routing table, in order to keep additional side effects as
  492. * small as possible.
  493. */
  494. TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
  495. if (dr->installed)
  496. continue;
  497. if (dr->ifp == ifp)
  498. defrtrlist_del(dr);
  499. }
  500. TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
  501. if (!dr->installed)
  502. continue;
  503. if (dr->ifp == ifp)
  504. defrtrlist_del(dr);
  505. }
  506. /* Nuke prefix list entries toward ifp */
  507. LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
  508. if (pr->ndpr_ifp == ifp)
  509. prelist_remove(pr);
  510. }
  511. if (ifp->if_xflags & IFXF_AUTOCONF6) {
  512. /* refresh default router list */
  513. defrouter_select();
  514. }
  515. /*
  516. * Nuke neighbor cache entries for the ifp.
  517. * Note that rt->rt_ifp may not be the same as ifp,
  518. * due to KAME goto ours hack. See RTM_RESOLVE case in
  519. * nd6_rtrequest(), and ip6_input().
  520. */
  521. ln = llinfo_nd6.ln_next;
  522. while (ln && ln != &llinfo_nd6) {
  523. struct rtentry *rt;
  524. struct sockaddr_dl *sdl;
  525. nln = ln->ln_next;
  526. rt = ln->ln_rt;
  527. if (rt && rt->rt_gateway &&
  528. rt->rt_gateway->sa_family == AF_LINK) {
  529. sdl = (struct sockaddr_dl *)rt->rt_gateway;
  530. if (sdl->sdl_index == ifp->if_index)
  531. nln = nd6_free(rt, 0);
  532. }
  533. ln = nln;
  534. }
  535. }
  536. struct rtentry *
  537. nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp,
  538. u_int rtableid)
  539. {
  540. struct rtentry *rt;
  541. struct sockaddr_in6 sin6;
  542. int flags;
  543. bzero(&sin6, sizeof(sin6));
  544. sin6.sin6_len = sizeof(struct sockaddr_in6);
  545. sin6.sin6_family = AF_INET6;
  546. sin6.sin6_addr = *addr6;
  547. flags = (create) ? (RT_REPORT|RT_RESOLVE) : 0;
  548. rt = rtalloc(sin6tosa(&sin6), flags, rtableid);
  549. if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
  550. /*
  551. * This is the case for the default route.
  552. * If we want to create a neighbor cache for the address, we
  553. * should free the route for the destination and allocate an
  554. * interface route.
  555. */
  556. if (create) {
  557. rtfree(rt);
  558. rt = NULL;
  559. }
  560. }
  561. if (!rt) {
  562. if (create && ifp) {
  563. struct rt_addrinfo info;
  564. int e;
  565. /*
  566. * If no route is available and create is set,
  567. * we allocate a host route for the destination
  568. * and treat it like an interface route.
  569. * This hack is necessary for a neighbor which can't
  570. * be covered by our own prefix.
  571. */
  572. struct ifaddr *ifa =
  573. ifaof_ifpforaddr(sin6tosa(&sin6), ifp);
  574. if (ifa == NULL)
  575. return (NULL);
  576. /*
  577. * Create a new route. RTF_LLINFO is necessary
  578. * to create a Neighbor Cache entry for the
  579. * destination in nd6_rtrequest which will be
  580. * called in rtrequest1 via ifa->ifa_rtrequest.
  581. */
  582. bzero(&info, sizeof(info));
  583. info.rti_flags = RTF_UP | RTF_HOST | RTF_LLINFO;
  584. info.rti_info[RTAX_DST] = sin6tosa(&sin6);
  585. info.rti_info[RTAX_GATEWAY] =
  586. (struct sockaddr *)ifp->if_sadl;
  587. if ((e = rtrequest1(RTM_ADD, &info, RTP_CONNECTED,
  588. &rt, rtableid)) != 0) {
  589. #if 0
  590. char ip[INET6_ADDRSTRLEN];
  591. log(LOG_ERR,
  592. "nd6_lookup: failed to add route for a "
  593. "neighbor(%s), errno=%d\n",
  594. inet_ntop(AF_INET6, addr6, ip, sizeof(ip)),
  595. e);
  596. #endif
  597. return (NULL);
  598. }
  599. if (rt == NULL)
  600. return (NULL);
  601. if (rt->rt_llinfo) {
  602. struct llinfo_nd6 *ln =
  603. (struct llinfo_nd6 *)rt->rt_llinfo;
  604. ln->ln_state = ND6_LLINFO_NOSTATE;
  605. }
  606. } else
  607. return (NULL);
  608. }
  609. rt->rt_refcnt--;
  610. /*
  611. * Validation for the entry.
  612. * Note that the check for rt_llinfo is necessary because a cloned
  613. * route from a parent route that has the L flag (e.g. the default
  614. * route to a p2p interface) may have the flag, too, while the
  615. * destination is not actually a neighbor.
  616. * XXX: we can't use rt->rt_ifp to check for the interface, since
  617. * it might be the loopback interface if the entry is for our
  618. * own address on a non-loopback interface. Instead, we should
  619. * use rt->rt_ifa->ifa_ifp, which would specify the REAL
  620. * interface.
  621. */
  622. if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
  623. rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
  624. (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
  625. if (create) {
  626. char addr[INET6_ADDRSTRLEN];
  627. nd6log((LOG_DEBUG,
  628. "nd6_lookup: failed to lookup %s (if = %s)\n",
  629. inet_ntop(AF_INET6, addr6, addr, sizeof(addr)),
  630. ifp ? ifp->if_xname : "unspec"));
  631. }
  632. return (NULL);
  633. }
  634. return (rt);
  635. }
  636. /*
  637. * Detect if a given IPv6 address identifies a neighbor on a given link.
  638. * XXX: should take care of the destination of a p2p link?
  639. */
  640. int
  641. nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
  642. {
  643. struct nd_prefix *pr;
  644. struct rtentry *rt;
  645. /*
  646. * A link-local address is always a neighbor.
  647. * XXX: we should use the sin6_scope_id field rather than the embedded
  648. * interface index.
  649. * XXX: a link does not necessarily specify a single interface.
  650. */
  651. if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
  652. ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
  653. return (1);
  654. /*
  655. * If the address matches one of our on-link prefixes, it should be a
  656. * neighbor.
  657. */
  658. LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
  659. if (pr->ndpr_ifp != ifp)
  660. continue;
  661. if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
  662. continue;
  663. if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
  664. &addr->sin6_addr, &pr->ndpr_mask))
  665. return (1);
  666. }
  667. /*
  668. * Even if the address matches none of our addresses, it might be
  669. * in the neighbor cache.
  670. */
  671. if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp,
  672. ifp->if_rdomain)) != NULL)
  673. return (1);
  674. return (0);
  675. }
  676. /*
  677. * Free an nd6 llinfo entry.
  678. * Since the function would cause significant changes in the kernel, DO NOT
  679. * make it global, unless you have a strong reason for the change, and are sure
  680. * that the change is safe.
  681. */
  682. struct llinfo_nd6 *
  683. nd6_free(struct rtentry *rt, int gc)
  684. {
  685. struct rt_addrinfo info;
  686. struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
  687. struct in6_addr in6 = satosin6(rt_key(rt))->sin6_addr;
  688. struct nd_defrouter *dr;
  689. int s;
  690. /*
  691. * we used to have pfctlinput(PRC_HOSTDEAD) here.
  692. * even though it is not harmful, it was not really necessary.
  693. */
  694. s = splsoftnet();
  695. if (!ip6_forwarding) {
  696. dr = defrouter_lookup(&satosin6(rt_key(rt))->sin6_addr,
  697. rt->rt_ifp);
  698. if (dr != NULL && dr->expire &&
  699. ln->ln_state == ND6_LLINFO_STALE && gc) {
  700. /*
  701. * If the reason for the deletion is just garbage
  702. * collection, and the neighbor is an active default
  703. * router, do not delete it. Instead, reset the GC
  704. * timer using the router's lifetime.
  705. * Simply deleting the entry would affect default
  706. * router selection, which is not necessarily a good
  707. * thing, especially when we're using router preference
  708. * values.
  709. * XXX: the check for ln_state would be redundant,
  710. * but we intentionally keep it just in case.
  711. */
  712. if (dr->expire > time_second * hz) {
  713. nd6_llinfo_settimer(ln,
  714. dr->expire - time_second * hz);
  715. } else
  716. nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  717. splx(s);
  718. return (ln->ln_next);
  719. }
  720. if (ln->ln_router || dr) {
  721. /*
  722. * rt6_flush must be called whether or not the neighbor
  723. * is in the Default Router List.
  724. * See a corresponding comment in nd6_na_input().
  725. */
  726. rt6_flush(&in6, rt->rt_ifp);
  727. }
  728. if (dr) {
  729. /*
  730. * Unreachability of a router might affect the default
  731. * router selection and on-link detection of advertised
  732. * prefixes.
  733. */
  734. /*
  735. * Temporarily fake the state to choose a new default
  736. * router and to perform on-link determination of
  737. * prefixes correctly.
  738. * Below the state will be set correctly,
  739. * or the entry itself will be deleted.
  740. */
  741. ln->ln_state = ND6_LLINFO_INCOMPLETE;
  742. /*
  743. * Since defrouter_select() does not affect the
  744. * on-link determination and MIP6 needs the check
  745. * before the default router selection, we perform
  746. * the check now.
  747. */
  748. pfxlist_onlink_check();
  749. /*
  750. * refresh default router list
  751. */
  752. defrouter_select();
  753. }
  754. }
  755. /*
  756. * Before deleting the entry, remember the next entry as the
  757. * return value. We need this because pfxlist_onlink_check() above
  758. * might have freed other entries (particularly the old next entry) as
  759. * a side effect (XXX).
  760. */
  761. next = ln->ln_next;
  762. /*
  763. * Detach the route from the routing tree and the list of neighbor
  764. * caches, and disable the route entry not to be used in already
  765. * cached routes.
  766. */
  767. bzero(&info, sizeof(info));
  768. info.rti_info[RTAX_DST] = rt_key(rt);
  769. info.rti_info[RTAX_NETMASK] = rt_mask(rt);
  770. rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL,
  771. rt->rt_ifp->if_rdomain);
  772. splx(s);
  773. return (next);
  774. }
  775. /*
  776. * Upper-layer reachability hint for Neighbor Unreachability Detection.
  777. *
  778. * XXX cost-effective methods?
  779. */
  780. void
  781. nd6_nud_hint(struct rtentry *rt, u_int rtableid)
  782. {
  783. struct llinfo_nd6 *ln;
  784. if (rt == NULL) {
  785. return;
  786. }
  787. if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
  788. (rt->rt_flags & RTF_LLINFO) == 0 ||
  789. !rt->rt_llinfo || !rt->rt_gateway ||
  790. rt->rt_gateway->sa_family != AF_LINK) {
  791. /* This is not a host route. */
  792. return;
  793. }
  794. ln = (struct llinfo_nd6 *)rt->rt_llinfo;
  795. if (ln->ln_state < ND6_LLINFO_REACHABLE)
  796. return;
  797. /*
  798. * if we get upper-layer reachability confirmation many times,
  799. * it is possible we have false information.
  800. */
  801. ln->ln_byhint++;
  802. if (ln->ln_byhint > nd6_maxnudhint)
  803. return;
  804. ln->ln_state = ND6_LLINFO_REACHABLE;
  805. if (!ND6_LLINFO_PERMANENT(ln)) {
  806. nd6_llinfo_settimer(ln,
  807. (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
  808. }
  809. }
  810. void
  811. nd6_rtrequest(int req, struct rtentry *rt)
  812. {
  813. struct sockaddr *gate = rt->rt_gateway;
  814. struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
  815. struct ifnet *ifp = rt->rt_ifp;
  816. struct ifaddr *ifa;
  817. struct nd_defrouter *dr;
  818. if (req == RTM_DELETE && (rt->rt_flags & RTF_GATEWAY) &&
  819. (IN6_ARE_ADDR_EQUAL(&(satosin6(rt_key(rt)))->sin6_addr,
  820. &in6addr_any) && rt_mask(rt) && (rt_mask(rt)->sa_len == 0 ||
  821. IN6_ARE_ADDR_EQUAL(&(satosin6(rt_mask(rt)))->sin6_addr,
  822. &in6addr_any)))) {
  823. dr = defrouter_lookup(&satosin6(gate)->sin6_addr, ifp);
  824. if (dr)
  825. dr->installed = 0;
  826. }
  827. if ((rt->rt_flags & RTF_GATEWAY) != 0)
  828. return;
  829. if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
  830. /*
  831. * This is probably an interface direct route for a link
  832. * which does not need neighbor caches (e.g. fe80::%lo0/64).
  833. * We do not need special treatment below for such a route.
  834. * Moreover, the RTF_LLINFO flag which would be set below
  835. * would annoy the ndp(8) command.
  836. */
  837. return;
  838. }
  839. if (req == RTM_RESOLVE && nd6_need_cache(ifp) == 0) {
  840. /*
  841. * For routing daemons like ospf6d we allow neighbor discovery
  842. * based on the cloning route only. This allows us to sent
  843. * packets directly into a network without having an address
  844. * with matching prefix on the interface. If the cloning
  845. * route is used for an stf interface, we would mistakenly
  846. * make a neighbor cache for the host route, and would see
  847. * strange neighbor solicitation for the corresponding
  848. * destination. In order to avoid confusion, we check if the
  849. * interface is suitable for neighbor discovery, and stop the
  850. * process if not. Additionally, we remove the LLINFO flag
  851. * so that ndp(8) will not try to get the neighbor information
  852. * of the destination.
  853. */
  854. rt->rt_flags &= ~RTF_LLINFO;
  855. return;
  856. }
  857. switch (req) {
  858. case RTM_ADD:
  859. /*
  860. * There is no backward compatibility :)
  861. *
  862. * if ((rt->rt_flags & RTF_HOST) == 0 &&
  863. * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
  864. * rt->rt_flags |= RTF_CLONING;
  865. */
  866. if ((rt->rt_flags & RTF_CLONING) ||
  867. ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !ln)) {
  868. if (ln)
  869. nd6_llinfo_settimer(ln, 0);
  870. if ((rt->rt_flags & RTF_CLONING) != 0)
  871. break;
  872. }
  873. /*
  874. * In IPv4 code, we try to announce new RTF_ANNOUNCE entry here.
  875. * We don't do that here since llinfo is not ready yet.
  876. *
  877. * There are also couple of other things to be discussed:
  878. * - unsolicited NA code needs improvement beforehand
  879. * - RFC2461 says we MAY send multicast unsolicited NA
  880. * (7.2.6 paragraph 4), however, it also says that we
  881. * SHOULD provide a mechanism to prevent multicast NA storm.
  882. * we don't have anything like it right now.
  883. * note that the mechanism needs a mutual agreement
  884. * between proxies, which means that we need to implement
  885. * a new protocol, or a new kludge.
  886. * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
  887. * we need to check ip6forwarding before sending it.
  888. * (or should we allow proxy ND configuration only for
  889. * routers? there's no mention about proxy ND from hosts)
  890. */
  891. #if 0
  892. /* XXX it does not work */
  893. if (rt->rt_flags & RTF_ANNOUNCE)
  894. nd6_na_output(ifp,
  895. &satosin6(rt_key(rt))->sin6_addr,
  896. &satosin6(rt_key(rt))->sin6_addr,
  897. ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
  898. 1, NULL);
  899. #endif
  900. /* FALLTHROUGH */
  901. case RTM_RESOLVE:
  902. if (gate->sa_family != AF_LINK ||
  903. gate->sa_len < sizeof(struct sockaddr_dl)) {
  904. log(LOG_DEBUG, "%s: bad gateway value: %s\n",
  905. __func__, ifp->if_xname);
  906. break;
  907. }
  908. SDL(gate)->sdl_type = ifp->if_type;
  909. SDL(gate)->sdl_index = ifp->if_index;
  910. if (ln != NULL)
  911. break; /* This happens on a route change */
  912. /*
  913. * Case 2: This route may come from cloning, or a manual route
  914. * add with a LL address.
  915. */
  916. ln = malloc(sizeof(*ln), M_RTABLE, M_NOWAIT | M_ZERO);
  917. rt->rt_llinfo = (caddr_t)ln;
  918. if (!ln) {
  919. log(LOG_DEBUG, "%s: malloc failed\n", __func__);
  920. break;
  921. }
  922. nd6_inuse++;
  923. nd6_allocated++;
  924. ln->ln_rt = rt;
  925. timeout_set(&ln->ln_timer_ch, nd6_llinfo_timer, ln);
  926. /* this is required for "ndp" command. - shin */
  927. if (req == RTM_ADD) {
  928. /*
  929. * gate should have some valid AF_LINK entry,
  930. * and ln->ln_expire should have some lifetime
  931. * which is specified by ndp command.
  932. */
  933. ln->ln_state = ND6_LLINFO_REACHABLE;
  934. ln->ln_byhint = 0;
  935. } else {
  936. /*
  937. * When req == RTM_RESOLVE, rt is created and
  938. * initialized in rtrequest(), so rt_expire is 0.
  939. */
  940. ln->ln_state = ND6_LLINFO_NOSTATE;
  941. nd6_llinfo_settimer(ln, 0);
  942. }
  943. rt->rt_flags |= RTF_LLINFO;
  944. ln->ln_next = llinfo_nd6.ln_next;
  945. llinfo_nd6.ln_next = ln;
  946. ln->ln_prev = &llinfo_nd6;
  947. ln->ln_next->ln_prev = ln;
  948. /*
  949. * If we have too many cache entries, initiate immediate
  950. * purging for some "less recently used" entries. Note that
  951. * we cannot directly call nd6_free() here because it would
  952. * cause re-entering rtable related routines triggering an LOR
  953. * problem for FreeBSD.
  954. */
  955. if (ip6_neighborgcthresh >= 0 &&
  956. nd6_inuse >= ip6_neighborgcthresh) {
  957. int i;
  958. for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
  959. struct llinfo_nd6 *ln_end = llinfo_nd6.ln_prev;
  960. /* Move this entry to the head */
  961. LN_DEQUEUE(ln_end);
  962. LN_INSERTHEAD(ln_end);
  963. if (ND6_LLINFO_PERMANENT(ln_end))
  964. continue;
  965. if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
  966. ln_end->ln_state = ND6_LLINFO_STALE;
  967. else
  968. ln_end->ln_state = ND6_LLINFO_PURGE;
  969. nd6_llinfo_settimer(ln_end, 0);
  970. }
  971. }
  972. /*
  973. * check if rt_key(rt) is one of my address assigned
  974. * to the interface.
  975. */
  976. ifa = &in6ifa_ifpwithaddr(ifp,
  977. &satosin6(rt_key(rt))->sin6_addr)->ia_ifa;
  978. if (ifa) {
  979. nd6_llinfo_settimer(ln, -1);
  980. ln->ln_state = ND6_LLINFO_REACHABLE;
  981. ln->ln_byhint = 0;
  982. /*
  983. * XXX Since lo0 is in the default rdomain we
  984. * should not (ab)use it for any route related
  985. * to an interface of a different rdomain.
  986. */
  987. rt->rt_ifp = lo0ifp;
  988. /*
  989. * Make sure rt_ifa be equal to the ifaddr
  990. * corresponding to the address.
  991. * We need this because when we refer
  992. * rt_ifa->ia6_flags in ip6_input, we assume
  993. * that the rt_ifa points to the address instead
  994. * of the loopback address.
  995. */
  996. if (ifa != rt->rt_ifa) {
  997. ifafree(rt->rt_ifa);
  998. ifa->ifa_refcnt++;
  999. rt->rt_ifa = ifa;
  1000. }
  1001. } else if (rt->rt_flags & RTF_ANNOUNCE) {
  1002. nd6_llinfo_settimer(ln, -1);
  1003. ln->ln_state = ND6_LLINFO_REACHABLE;
  1004. ln->ln_byhint = 0;
  1005. /* join solicited node multicast for proxy ND */
  1006. if (ifp->if_flags & IFF_MULTICAST) {
  1007. struct in6_addr llsol;
  1008. int error;
  1009. llsol = satosin6(rt_key(rt))->sin6_addr;
  1010. llsol.s6_addr16[0] = htons(0xff02);
  1011. llsol.s6_addr16[1] = htons(ifp->if_index);
  1012. llsol.s6_addr32[1] = 0;
  1013. llsol.s6_addr32[2] = htonl(1);
  1014. llsol.s6_addr8[12] = 0xff;
  1015. if (in6_addmulti(&llsol, ifp, &error)) {
  1016. char addr[INET6_ADDRSTRLEN];
  1017. nd6log((LOG_ERR, "%s: failed to join "
  1018. "%s (errno=%d)\n", ifp->if_xname,
  1019. inet_ntop(AF_INET6, &llsol,
  1020. addr, sizeof(addr)),
  1021. error));
  1022. }
  1023. }
  1024. }
  1025. break;
  1026. case RTM_DELETE:
  1027. if (!ln)
  1028. break;
  1029. /* leave from solicited node multicast for proxy ND */
  1030. if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
  1031. (ifp->if_flags & IFF_MULTICAST) != 0) {
  1032. struct in6_addr llsol;
  1033. struct in6_multi *in6m;
  1034. llsol = satosin6(rt_key(rt))->sin6_addr;
  1035. llsol.s6_addr16[0] = htons(0xff02);
  1036. llsol.s6_addr16[1] = htons(ifp->if_index);
  1037. llsol.s6_addr32[1] = 0;
  1038. llsol.s6_addr32[2] = htonl(1);
  1039. llsol.s6_addr8[12] = 0xff;
  1040. IN6_LOOKUP_MULTI(llsol, ifp, in6m);
  1041. if (in6m)
  1042. in6_delmulti(in6m);
  1043. }
  1044. nd6_inuse--;
  1045. ln->ln_next->ln_prev = ln->ln_prev;
  1046. ln->ln_prev->ln_next = ln->ln_next;
  1047. ln->ln_prev = NULL;
  1048. nd6_llinfo_settimer(ln, -1);
  1049. rt->rt_llinfo = 0;
  1050. rt->rt_flags &= ~RTF_LLINFO;
  1051. m_freem(ln->ln_hold);
  1052. free(ln, M_RTABLE, 0);
  1053. }
  1054. }
  1055. int
  1056. nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
  1057. {
  1058. struct in6_ndireq *ndi = (struct in6_ndireq *)data;
  1059. struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
  1060. struct rtentry *rt;
  1061. int error = 0;
  1062. int s;
  1063. switch (cmd) {
  1064. case SIOCGIFINFO_IN6:
  1065. ndi->ndi = *ND_IFINFO(ifp);
  1066. break;
  1067. case SIOCSIFINFO_FLAGS:
  1068. ND_IFINFO(ifp)->flags = ndi->ndi.flags;
  1069. break;
  1070. case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
  1071. /* sync kernel routing table with the default router list */
  1072. defrouter_reset();
  1073. defrouter_select();
  1074. break;
  1075. case SIOCSPFXFLUSH_IN6:
  1076. {
  1077. /* flush all the prefix advertised by routers */
  1078. struct nd_prefix *pr, *npr;
  1079. s = splsoftnet();
  1080. /* First purge the addresses referenced by a prefix. */
  1081. LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
  1082. struct in6_ifaddr *ia6, *ia6_next;
  1083. if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
  1084. continue; /* XXX */
  1085. /* do we really have to remove addresses as well? */
  1086. TAILQ_FOREACH_SAFE(ia6, &in6_ifaddr, ia_list, ia6_next) {
  1087. if ((ia6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
  1088. continue;
  1089. if (ia6->ia6_ndpr == pr)
  1090. in6_purgeaddr(&ia6->ia_ifa);
  1091. }
  1092. }
  1093. /*
  1094. * Purging the addresses might remove the prefix as well.
  1095. * So run the loop again to access only prefixes that have
  1096. * not been freed already.
  1097. */
  1098. LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
  1099. if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
  1100. continue; /* XXX */
  1101. prelist_remove(pr);
  1102. }
  1103. splx(s);
  1104. break;
  1105. }
  1106. case SIOCSRTRFLUSH_IN6:
  1107. {
  1108. /* flush all the default routers */
  1109. struct nd_defrouter *dr, *ndr;
  1110. s = splsoftnet();
  1111. defrouter_reset();
  1112. TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
  1113. defrtrlist_del(dr);
  1114. defrouter_select();
  1115. splx(s);
  1116. break;
  1117. }
  1118. case SIOCGNBRINFO_IN6:
  1119. {
  1120. struct llinfo_nd6 *ln;
  1121. struct in6_addr nb_addr = nbi->addr; /* make local for safety */
  1122. /*
  1123. * XXX: KAME specific hack for scoped addresses
  1124. * XXXX: for other scopes than link-local?
  1125. */
  1126. if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
  1127. IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
  1128. u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
  1129. if (*idp == 0)
  1130. *idp = htons(ifp->if_index);
  1131. }
  1132. s = splsoftnet();
  1133. if ((rt = nd6_lookup(&nb_addr, 0, ifp, ifp->if_rdomain)) == NULL ||
  1134. (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
  1135. error = EINVAL;
  1136. splx(s);
  1137. break;
  1138. }
  1139. nbi->state = ln->ln_state;
  1140. nbi->asked = ln->ln_asked;
  1141. nbi->isrouter = ln->ln_router;
  1142. nbi->expire = ln->ln_expire;
  1143. splx(s);
  1144. break;
  1145. }
  1146. }
  1147. return (error);
  1148. }
  1149. /*
  1150. * Create neighbor cache entry and cache link-layer address,
  1151. * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
  1152. *
  1153. * type - ICMP6 type
  1154. * code - type dependent information
  1155. */
  1156. struct rtentry *
  1157. nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
  1158. int lladdrlen, int type, int code)
  1159. {
  1160. struct rtentry *rt = NULL;
  1161. struct llinfo_nd6 *ln = NULL;
  1162. int is_newentry;
  1163. struct sockaddr_dl *sdl = NULL;
  1164. int do_update;
  1165. int olladdr;
  1166. int llchange;
  1167. int newstate = 0;
  1168. if (!ifp)
  1169. panic("ifp == NULL in nd6_cache_lladdr");
  1170. if (!from)
  1171. panic("from == NULL in nd6_cache_lladdr");
  1172. /* nothing must be updated for unspecified address */
  1173. if (IN6_IS_ADDR_UNSPECIFIED(from))
  1174. return NULL;
  1175. /*
  1176. * Validation about ifp->if_addrlen and lladdrlen must be done in
  1177. * the caller.
  1178. *
  1179. * XXX If the link does not have link-layer address, what should
  1180. * we do? (ifp->if_addrlen == 0)
  1181. * Spec says nothing in sections for RA, RS and NA. There's small
  1182. * description on it in NS section (RFC 2461 7.2.3).
  1183. */
  1184. rt = nd6_lookup(from, 0, ifp, ifp->if_rdomain);
  1185. if (!rt) {
  1186. #if 0
  1187. /* nothing must be done if there's no lladdr */
  1188. if (!lladdr || !lladdrlen)
  1189. return NULL;
  1190. #endif
  1191. rt = nd6_lookup(from, 1, ifp, ifp->if_rdomain);
  1192. is_newentry = 1;
  1193. } else {
  1194. /* do nothing if static ndp is set */
  1195. if (rt->rt_flags & RTF_STATIC)
  1196. return NULL;
  1197. is_newentry = 0;
  1198. }
  1199. if (!rt)
  1200. return NULL;
  1201. if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
  1202. fail:
  1203. (void)nd6_free(rt, 0);
  1204. return NULL;
  1205. }
  1206. ln = (struct llinfo_nd6 *)rt->rt_llinfo;
  1207. if (!ln)
  1208. goto fail;
  1209. if (!rt->rt_gateway)
  1210. goto fail;
  1211. if (rt->rt_gateway->sa_family != AF_LINK)
  1212. goto fail;
  1213. sdl = SDL(rt->rt_gateway);
  1214. olladdr = (sdl->sdl_alen) ? 1 : 0;
  1215. if (olladdr && lladdr) {
  1216. if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
  1217. llchange = 1;
  1218. else
  1219. llchange = 0;
  1220. } else
  1221. llchange = 0;
  1222. /*
  1223. * newentry olladdr lladdr llchange (*=record)
  1224. * 0 n n -- (1)
  1225. * 0 y n -- (2)
  1226. * 0 n y -- (3) * STALE
  1227. * 0 y y n (4) *
  1228. * 0 y y y (5) * STALE
  1229. * 1 -- n -- (6) NOSTATE(= PASSIVE)
  1230. * 1 -- y -- (7) * STALE
  1231. */
  1232. if (llchange) {
  1233. char addr[INET6_ADDRSTRLEN];
  1234. log(LOG_INFO, "ndp info overwritten for %s by %s on %s\n",
  1235. inet_ntop(AF_INET6, from, addr, sizeof(addr)),
  1236. ether_sprintf(lladdr), ifp->if_xname);
  1237. }
  1238. if (lladdr) { /* (3-5) and (7) */
  1239. /*
  1240. * Record source link-layer address
  1241. * XXX is it dependent to ifp->if_type?
  1242. */
  1243. sdl->sdl_alen = ifp->if_addrlen;
  1244. bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
  1245. }
  1246. if (!is_newentry) {
  1247. if ((!olladdr && lladdr) || /* (3) */
  1248. (olladdr && lladdr && llchange)) { /* (5) */
  1249. do_update = 1;
  1250. newstate = ND6_LLINFO_STALE;
  1251. } else /* (1-2,4) */
  1252. do_update = 0;
  1253. } else {
  1254. do_update = 1;
  1255. if (!lladdr) /* (6) */
  1256. newstate = ND6_LLINFO_NOSTATE;
  1257. else /* (7) */
  1258. newstate = ND6_LLINFO_STALE;
  1259. }
  1260. if (do_update) {
  1261. /*
  1262. * Update the state of the neighbor cache.
  1263. */
  1264. ln->ln_state = newstate;
  1265. if (ln->ln_state == ND6_LLINFO_STALE) {
  1266. /*
  1267. * XXX: since nd6_output() below will cause
  1268. * state transition to DELAY and reset the timer,
  1269. * we must set the timer now, although it is actually
  1270. * meaningless.
  1271. */
  1272. nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  1273. if (ln->ln_hold) {
  1274. struct mbuf *n = ln->ln_hold;
  1275. ln->ln_hold = NULL;
  1276. /*
  1277. * we assume ifp is not a p2p here, so just
  1278. * set the 2nd argument as the 1st one.
  1279. */
  1280. nd6_output(ifp, n, satosin6(rt_key(rt)), rt);
  1281. if (ln->ln_hold == n) {
  1282. /* n is back in ln_hold. Discard. */
  1283. m_freem(ln->ln_hold);
  1284. ln->ln_hold = NULL;
  1285. }
  1286. }
  1287. } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
  1288. /* probe right away */
  1289. nd6_llinfo_settimer((void *)ln, 0);
  1290. }
  1291. }
  1292. /*
  1293. * ICMP6 type dependent behavior.
  1294. *
  1295. * NS: clear IsRouter if new entry
  1296. * RS: clear IsRouter
  1297. * RA: set IsRouter if there's lladdr
  1298. * redir: clear IsRouter if new entry
  1299. *
  1300. * RA case, (1):
  1301. * The spec says that we must set IsRouter in the following cases:
  1302. * - If lladdr exist, set IsRouter. This means (1-5).
  1303. * - If it is old entry (!newentry), set IsRouter. This means (7).
  1304. * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
  1305. * A question arises for (1) case. (1) case has no lladdr in the
  1306. * neighbor cache, this is similar to (6).
  1307. * This case is rare but we figured that we MUST NOT set IsRouter.
  1308. *
  1309. * newentry olladdr lladdr llchange NS RS RA redir
  1310. * D R
  1311. * 0 n n -- (1) c ? s
  1312. * 0 y n -- (2) c s s
  1313. * 0 n y -- (3) c s s
  1314. * 0 y y n (4) c s s
  1315. * 0 y y y (5) c s s
  1316. * 1 -- n -- (6) c c c s
  1317. * 1 -- y -- (7) c c s c s
  1318. *
  1319. * (c=clear s=set)
  1320. */
  1321. switch (type & 0xff) {
  1322. case ND_NEIGHBOR_SOLICIT:
  1323. /*
  1324. * New entry must have is_router flag cleared.
  1325. */
  1326. if (is_newentry) /* (6-7) */
  1327. ln->ln_router = 0;
  1328. break;
  1329. case ND_REDIRECT:
  1330. /*
  1331. * If the icmp is a redirect to a better router, always set the
  1332. * is_router flag. Otherwise, if the entry is newly created,
  1333. * clear the flag. [RFC 2461, sec 8.3]
  1334. */
  1335. if (code == ND_REDIRECT_ROUTER)
  1336. ln->ln_router = 1;
  1337. else if (is_newentry) /* (6-7) */
  1338. ln->ln_router = 0;
  1339. break;
  1340. case ND_ROUTER_SOLICIT:
  1341. /*
  1342. * is_router flag must always be cleared.
  1343. */
  1344. ln->ln_router = 0;
  1345. break;
  1346. case ND_ROUTER_ADVERT:
  1347. /*
  1348. * Mark an entry with lladdr as a router.
  1349. */
  1350. if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
  1351. (is_newentry && lladdr)) { /* (7) */
  1352. ln->ln_router = 1;
  1353. }
  1354. break;
  1355. }
  1356. /*
  1357. * When the link-layer address of a router changes, select the
  1358. * best router again. In particular, when the neighbor entry is newly
  1359. * created, it might affect the selection policy.
  1360. * Question: can we restrict the first condition to the "is_newentry"
  1361. * case?
  1362. * XXX: when we hear an RA from a new router with the link-layer
  1363. * address option, defrouter_select() is called twice, since
  1364. * defrtrlist_update called the function as well. However, I believe
  1365. * we can compromise the overhead, since it only happens the first
  1366. * time.
  1367. */
  1368. if (do_update && ln->ln_router && (ifp->if_xflags & IFXF_AUTOCONF6))
  1369. defrouter_select();
  1370. return rt;
  1371. }
  1372. void
  1373. nd6_slowtimo(void *ignored_arg)
  1374. {
  1375. int s = splsoftnet();
  1376. struct nd_ifinfo *nd6if;
  1377. struct ifnet *ifp;
  1378. timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
  1379. timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
  1380. TAILQ_FOREACH(ifp, &ifnet, if_list) {
  1381. nd6if = ND_IFINFO(ifp);
  1382. if (nd6if->basereachable && /* already initialized */
  1383. (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
  1384. /*
  1385. * Since reachable time rarely changes by router
  1386. * advertisements, we SHOULD insure that a new random
  1387. * value gets recomputed at least once every few hours.
  1388. * (RFC 2461, 6.3.4)
  1389. */
  1390. nd6if->recalctm = nd6_recalc_reachtm_interval;
  1391. nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
  1392. }
  1393. }
  1394. splx(s);
  1395. }
  1396. #define senderr(e) { error = (e); goto bad;}
  1397. int
  1398. nd6_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr_in6 *dst,
  1399. struct rtentry *rt0)
  1400. {
  1401. struct mbuf *m = m0;
  1402. struct rtentry *rt = rt0;
  1403. struct llinfo_nd6 *ln = NULL;
  1404. int error = 0;
  1405. if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
  1406. goto sendpkt;
  1407. if (nd6_need_cache(ifp) == 0)
  1408. goto sendpkt;
  1409. /*
  1410. * next hop determination.
  1411. */
  1412. if (rt0 != NULL) {
  1413. error = rt_checkgate(ifp, rt0, sin6tosa(dst),
  1414. m->m_pkthdr.ph_rtableid, &rt);
  1415. if (error) {
  1416. m_freem(m);
  1417. return (error);
  1418. }
  1419. /*
  1420. * We skip link-layer address resolution and NUD
  1421. * if the gateway is not a neighbor from ND point
  1422. * of view, regardless of the value of nd_ifinfo.flags.
  1423. * The second condition is a bit tricky; we skip
  1424. * if the gateway is our own address, which is
  1425. * sometimes used to install a route to a p2p link.
  1426. */
  1427. if ((ifp->if_flags & IFF_POINTOPOINT) &&
  1428. ((nd6_is_addr_neighbor(satosin6(rt_key(rt)), ifp) == 0) ||
  1429. in6ifa_ifpwithaddr(ifp, &satosin6(rt_key(rt))->sin6_addr)))
  1430. goto sendpkt;
  1431. }
  1432. /*
  1433. * Address resolution or Neighbor Unreachability Detection
  1434. * for the next hop.
  1435. * At this point, the destination of the packet must be a unicast
  1436. * or an anycast address(i.e. not a multicast).
  1437. */
  1438. /* Look up the neighbor cache for the nexthop */
  1439. if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
  1440. ln = (struct llinfo_nd6 *)rt->rt_llinfo;
  1441. else {
  1442. /*
  1443. * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
  1444. * the condition below is not very efficient. But we believe
  1445. * it is tolerable, because this should be a rare case.
  1446. */
  1447. if (nd6_is_addr_neighbor(dst, ifp) &&
  1448. (rt = nd6_lookup(&dst->sin6_addr, 1, ifp,
  1449. ifp->if_rdomain)) != NULL)
  1450. ln = (struct llinfo_nd6 *)rt->rt_llinfo;
  1451. }
  1452. if (!ln || !rt) {
  1453. if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
  1454. !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
  1455. char addr[INET6_ADDRSTRLEN];
  1456. log(LOG_DEBUG,
  1457. "nd6_output: can't allocate llinfo for %s "
  1458. "(ln=%p, rt=%p)\n",
  1459. inet_ntop(AF_INET6, &dst->sin6_addr,
  1460. addr, sizeof(addr)),
  1461. ln, rt);
  1462. senderr(EIO); /* XXX: good error? */
  1463. }
  1464. goto sendpkt; /* send anyway */
  1465. }
  1466. /*
  1467. * Move this entry to the head of the queue so that it is less likely
  1468. * for this entry to be a target of forced garbage collection (see
  1469. * nd6_rtrequest()).
  1470. */
  1471. LN_DEQUEUE(ln);
  1472. LN_INSERTHEAD(ln);
  1473. /* We don't have to do link-layer address resolution on a p2p link. */
  1474. if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
  1475. ln->ln_state < ND6_LLINFO_REACHABLE) {
  1476. ln->ln_state = ND6_LLINFO_STALE;
  1477. nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
  1478. }
  1479. /*
  1480. * The first time we send a packet to a neighbor whose entry is
  1481. * STALE, we have to change the state to DELAY and a sets a timer to
  1482. * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
  1483. * neighbor unreachability detection on expiration.
  1484. * (RFC 2461 7.3.3)
  1485. */
  1486. if (ln->ln_state == ND6_LLINFO_STALE) {
  1487. ln->ln_asked = 0;
  1488. ln->ln_state = ND6_LLINFO_DELAY;
  1489. nd6_llinfo_settimer(ln, nd6_delay * hz);
  1490. }
  1491. /*
  1492. * If the neighbor cache entry has a state other than INCOMPLETE
  1493. * (i.e. its link-layer address is already resolved), just
  1494. * send the packet.
  1495. */
  1496. if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
  1497. goto sendpkt;
  1498. /*
  1499. * There is a neighbor cache entry, but no ethernet address
  1500. * response yet. Replace the held mbuf (if any) with this
  1501. * latest one.
  1502. */
  1503. if (ln->ln_state == ND6_LLINFO_NOSTATE)
  1504. ln->ln_state = ND6_LLINFO_INCOMPLETE;
  1505. m_freem(ln->ln_hold);
  1506. ln->ln_hold = m;
  1507. /*
  1508. * If there has been no NS for the neighbor after entering the
  1509. * INCOMPLETE state, send the first solicitation.
  1510. */
  1511. if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
  1512. ln->ln_asked++;
  1513. nd6_llinfo_settimer(ln,
  1514. (long)ND_IFINFO(ifp)->retrans * hz / 1000);
  1515. nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
  1516. }
  1517. return (0);
  1518. sendpkt:
  1519. return ((*ifp->if_output)(ifp, m, sin6tosa(dst), rt));
  1520. bad:
  1521. m_freem(m);
  1522. return (error);
  1523. }
  1524. #undef senderr
  1525. int
  1526. nd6_need_cache(struct ifnet *ifp)
  1527. {
  1528. /*
  1529. * RFC2893 says:
  1530. * - unidirectional tunnels needs no ND
  1531. */
  1532. switch (ifp->if_type) {
  1533. case IFT_ETHER:
  1534. case IFT_IEEE1394:
  1535. case IFT_PROPVIRTUAL:
  1536. case IFT_IEEE80211:
  1537. case IFT_CARP:
  1538. case IFT_GIF: /* XXX need more cases? */
  1539. return (1);
  1540. default:
  1541. return (0);
  1542. }
  1543. }
  1544. int
  1545. nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
  1546. struct sockaddr *dst, u_char *desten)
  1547. {
  1548. struct sockaddr_dl *sdl;
  1549. struct rtentry *rt;
  1550. int error;
  1551. if (m->m_flags & M_MCAST) {
  1552. switch (ifp->if_type) {
  1553. case IFT_ETHER:
  1554. case IFT_CARP:
  1555. ETHER_MAP_IPV6_MULTICAST(&satosin6(dst)->sin6_addr,
  1556. desten);
  1557. return (0);
  1558. break;
  1559. default:
  1560. m_freem(m);
  1561. return (EINVAL);
  1562. }
  1563. }
  1564. if (rt0 == NULL) {
  1565. /* this could happen, if we could not allocate memory */
  1566. m_freem(m);
  1567. return (ENOMEM);
  1568. }
  1569. error = rt_checkgate(ifp, rt0, dst, m->m_pkthdr.ph_rtableid, &rt);
  1570. if (error) {
  1571. m_freem(m);
  1572. return (error);
  1573. }
  1574. if (rt->rt_gateway->sa_family != AF_LINK) {
  1575. printf("nd6_storelladdr: something odd happens\n");
  1576. m_freem(m);
  1577. return (EINVAL);
  1578. }
  1579. sdl = SDL(rt->rt_gateway);
  1580. if (sdl->sdl_alen != ETHER_ADDR_LEN) {
  1581. char addr[INET6_ADDRSTRLEN];
  1582. log(LOG_DEBUG, "%s: %s: incorrect nd6 information\n", __func__,
  1583. inet_ntop(AF_INET6, &satosin6(dst)->sin6_addr,
  1584. addr, sizeof(addr)));
  1585. m_freem(m);
  1586. return (EINVAL);
  1587. }
  1588. bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
  1589. return (0);
  1590. }
  1591. /*
  1592. * oldp - syscall arg, need copyout
  1593. * newp - syscall arg, need copyin
  1594. */
  1595. int
  1596. nd6_sysctl(int name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
  1597. {
  1598. void *p;
  1599. size_t ol;
  1600. int error;
  1601. error = 0;
  1602. if (newp)
  1603. return EPERM;
  1604. if (oldp && !oldlenp)
  1605. return EINVAL;
  1606. ol = oldlenp ? *oldlenp : 0;
  1607. if (oldp) {
  1608. p = malloc(*oldlenp, M_TEMP, M_WAITOK | M_CANFAIL);
  1609. if (!p)
  1610. return ENOMEM;
  1611. } else
  1612. p = NULL;
  1613. switch (name) {
  1614. case ICMPV6CTL_ND6_DRLIST:
  1615. error = fill_drlist(p, oldlenp, ol);
  1616. if (!error && p && oldp)
  1617. error = copyout(p, oldp, *oldlenp);
  1618. break;
  1619. case ICMPV6CTL_ND6_PRLIST:
  1620. error = fill_prlist(p, oldlenp, ol);
  1621. if (!error && p && oldp)
  1622. error = copyout(p, oldp, *oldlenp);
  1623. break;
  1624. default:
  1625. error = ENOPROTOOPT;
  1626. break;
  1627. }
  1628. if (p)
  1629. free(p, M_TEMP, 0);
  1630. return (error);
  1631. }
  1632. int
  1633. fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
  1634. {
  1635. int error = 0, s;
  1636. struct in6_defrouter *d = NULL, *de = NULL;
  1637. struct nd_defrouter *dr;
  1638. size_t l;
  1639. s = splsoftnet();
  1640. if (oldp) {
  1641. d = (struct in6_defrouter *)oldp;
  1642. de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
  1643. }
  1644. l = 0;
  1645. TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
  1646. if (oldp && d + 1 <= de) {
  1647. bzero(d, sizeof(*d));
  1648. d->rtaddr.sin6_family = AF_INET6;
  1649. d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
  1650. d->rtaddr.sin6_addr = dr->rtaddr;
  1651. in6_recoverscope(&d->rtaddr, &d->rtaddr.sin6_addr,
  1652. dr->ifp);
  1653. d->flags = dr->flags;
  1654. d->rtlifetime = dr->rtlifetime;
  1655. d->expire = dr->expire;
  1656. d->if_index = dr->ifp->if_index;
  1657. }
  1658. l += sizeof(*d);
  1659. if (d)
  1660. d++;
  1661. }
  1662. if (oldp) {
  1663. *oldlenp = l; /* (caddr_t)d - (caddr_t)oldp */
  1664. if (l > ol)
  1665. error = ENOMEM;
  1666. } else
  1667. *oldlenp = l;
  1668. splx(s);
  1669. return (error);
  1670. }
  1671. int
  1672. fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
  1673. {
  1674. int error = 0, s;
  1675. struct nd_prefix *pr;
  1676. struct in6_prefix *p = NULL;
  1677. struct in6_prefix *pe = NULL;
  1678. size_t l;
  1679. s = splsoftnet();
  1680. if (oldp) {
  1681. p = (struct in6_prefix *)oldp;
  1682. pe = (struct in6_prefix *)((caddr_t)oldp + *oldlenp);
  1683. }
  1684. l = 0;
  1685. LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
  1686. u_short advrtrs;
  1687. size_t advance;
  1688. struct sockaddr_in6 *sin6;
  1689. struct sockaddr_in6 *s6;
  1690. struct nd_pfxrouter *pfr;
  1691. char addr[INET6_ADDRSTRLEN];
  1692. if (oldp && p + 1 <= pe)
  1693. {
  1694. bzero(p, sizeof(*p));
  1695. sin6 = (struct sockaddr_in6 *)(p + 1);
  1696. p->prefix = pr->ndpr_prefix;
  1697. if (in6_recoverscope(&p->prefix,
  1698. &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
  1699. log(LOG_ERR,
  1700. "scope error in prefix list (%s)\n",
  1701. inet_ntop(AF_INET6, &p->prefix.sin6_addr,
  1702. addr, sizeof(addr)));
  1703. p->raflags = pr->ndpr_raf;
  1704. p->prefixlen = pr->ndpr_plen;
  1705. p->vltime = pr->ndpr_vltime;
  1706. p->pltime = pr->ndpr_pltime;
  1707. p->if_index = pr->ndpr_ifp->if_index;
  1708. if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
  1709. p->expire = 0;
  1710. else {
  1711. time_t maxexpire;
  1712. /* XXX: we assume time_t is signed. */
  1713. maxexpire = (time_t)~(1ULL <<
  1714. ((sizeof(maxexpire) * 8) - 1));
  1715. if (pr->ndpr_vltime <
  1716. maxexpire - pr->ndpr_lastupdate) {
  1717. p->expire = pr->ndpr_lastupdate +
  1718. pr->ndpr_vltime;
  1719. } else
  1720. p->expire = maxexpire;
  1721. }
  1722. p->refcnt = pr->ndpr_refcnt;
  1723. p->flags = pr->ndpr_stateflags;
  1724. p->origin = PR_ORIG_RA;
  1725. advrtrs = 0;
  1726. LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
  1727. if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
  1728. advrtrs++;
  1729. continue;
  1730. }
  1731. s6 = &sin6[advrtrs];
  1732. s6->sin6_family = AF_INET6;
  1733. s6->sin6_len = sizeof(struct sockaddr_in6);
  1734. s6->sin6_addr = pfr->router->rtaddr;
  1735. in6_recoverscope(s6, &pfr->router->rtaddr,
  1736. pfr->router->ifp);
  1737. advrtrs++;
  1738. }
  1739. p->advrtrs = advrtrs;
  1740. }
  1741. else {
  1742. advrtrs = 0;
  1743. LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
  1744. advrtrs++;
  1745. }
  1746. advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
  1747. l += advance;
  1748. if (p)
  1749. p = (struct in6_prefix *)((caddr_t)p + advance);
  1750. }
  1751. if (oldp) {
  1752. *oldlenp = l; /* (caddr_t)d - (caddr_t)oldp */
  1753. if (l > ol)
  1754. error = ENOMEM;
  1755. } else
  1756. *oldlenp = l;
  1757. splx(s);
  1758. return (error);
  1759. }