icmp6.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /* $OpenBSD: icmp6.c,v 1.163 2015/07/28 12:22:07 bluhm Exp $ */
  2. /* $KAME: icmp6.c,v 1.217 2001/06/20 15:03:29 jinmei Exp $ */
  3. /*
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the project nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. /*
  32. * Copyright (c) 1982, 1986, 1988, 1993
  33. * The Regents of the University of California. All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. * 3. Neither the name of the University nor the names of its contributors
  44. * may be used to endorse or promote products derived from this software
  45. * without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  48. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  51. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  52. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  53. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  57. * SUCH DAMAGE.
  58. *
  59. * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
  60. */
  61. #include "carp.h"
  62. #include "pf.h"
  63. #include <sys/param.h>
  64. #include <sys/systm.h>
  65. #include <sys/malloc.h>
  66. #include <sys/mbuf.h>
  67. #include <sys/sysctl.h>
  68. #include <sys/protosw.h>
  69. #include <sys/socket.h>
  70. #include <sys/socketvar.h>
  71. #include <sys/time.h>
  72. #include <sys/kernel.h>
  73. #include <sys/syslog.h>
  74. #include <sys/domain.h>
  75. #include <net/if.h>
  76. #include <net/if_var.h>
  77. #include <net/route.h>
  78. #include <net/if_dl.h>
  79. #include <net/if_types.h>
  80. #include <netinet/in.h>
  81. #include <netinet/ip.h>
  82. #include <netinet6/in6_var.h>
  83. #include <netinet/ip6.h>
  84. #include <netinet6/ip6_var.h>
  85. #include <netinet/icmp6.h>
  86. #include <netinet6/mld6_var.h>
  87. #include <netinet/in_pcb.h>
  88. #include <netinet6/nd6.h>
  89. #include <netinet6/ip6protosw.h>
  90. #if NCARP > 0
  91. #include <netinet/ip_carp.h>
  92. #endif
  93. #if NPF > 0
  94. #include <net/pfvar.h>
  95. #endif
  96. struct icmp6stat icmp6stat;
  97. extern struct inpcbtable rawin6pcbtable;
  98. extern int icmp6errppslim;
  99. static int icmp6errpps_count = 0;
  100. static struct timeval icmp6errppslim_last;
  101. /*
  102. * List of callbacks to notify when Path MTU changes are made.
  103. */
  104. struct icmp6_mtudisc_callback {
  105. LIST_ENTRY(icmp6_mtudisc_callback) mc_list;
  106. void (*mc_func)(struct sockaddr_in6 *, u_int);
  107. };
  108. LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks =
  109. LIST_HEAD_INITIALIZER(icmp6_mtudisc_callbacks);
  110. struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
  111. /* XXX do these values make any sense? */
  112. static int icmp6_mtudisc_hiwat = 1280;
  113. static int icmp6_mtudisc_lowat = 256;
  114. /*
  115. * keep track of # of redirect routes.
  116. */
  117. static struct rttimer_queue *icmp6_redirect_timeout_q = NULL;
  118. /* XXX experimental, turned off */
  119. static int icmp6_redirect_lowat = -1;
  120. void icmp6_errcount(struct icmp6errstat *, int, int);
  121. int icmp6_rip6_input(struct mbuf **, int);
  122. int icmp6_ratelimit(const struct in6_addr *, const int, const int);
  123. const char *icmp6_redirect_diag(struct in6_addr *, struct in6_addr *,
  124. struct in6_addr *);
  125. int icmp6_notify_error(struct mbuf *, int, int, int);
  126. struct rtentry *icmp6_mtudisc_clone(struct sockaddr *, u_int);
  127. void icmp6_mtudisc_timeout(struct rtentry *, struct rttimer *);
  128. void icmp6_redirect_timeout(struct rtentry *, struct rttimer *);
  129. void
  130. icmp6_init(void)
  131. {
  132. mld6_init();
  133. icmp6_mtudisc_timeout_q = rt_timer_queue_create(ip6_mtudisc_timeout);
  134. icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
  135. }
  136. void
  137. icmp6_errcount(struct icmp6errstat *stat, int type, int code)
  138. {
  139. switch (type) {
  140. case ICMP6_DST_UNREACH:
  141. switch (code) {
  142. case ICMP6_DST_UNREACH_NOROUTE:
  143. stat->icp6errs_dst_unreach_noroute++;
  144. return;
  145. case ICMP6_DST_UNREACH_ADMIN:
  146. stat->icp6errs_dst_unreach_admin++;
  147. return;
  148. case ICMP6_DST_UNREACH_BEYONDSCOPE:
  149. stat->icp6errs_dst_unreach_beyondscope++;
  150. return;
  151. case ICMP6_DST_UNREACH_ADDR:
  152. stat->icp6errs_dst_unreach_addr++;
  153. return;
  154. case ICMP6_DST_UNREACH_NOPORT:
  155. stat->icp6errs_dst_unreach_noport++;
  156. return;
  157. }
  158. break;
  159. case ICMP6_PACKET_TOO_BIG:
  160. stat->icp6errs_packet_too_big++;
  161. return;
  162. case ICMP6_TIME_EXCEEDED:
  163. switch (code) {
  164. case ICMP6_TIME_EXCEED_TRANSIT:
  165. stat->icp6errs_time_exceed_transit++;
  166. return;
  167. case ICMP6_TIME_EXCEED_REASSEMBLY:
  168. stat->icp6errs_time_exceed_reassembly++;
  169. return;
  170. }
  171. break;
  172. case ICMP6_PARAM_PROB:
  173. switch (code) {
  174. case ICMP6_PARAMPROB_HEADER:
  175. stat->icp6errs_paramprob_header++;
  176. return;
  177. case ICMP6_PARAMPROB_NEXTHEADER:
  178. stat->icp6errs_paramprob_nextheader++;
  179. return;
  180. case ICMP6_PARAMPROB_OPTION:
  181. stat->icp6errs_paramprob_option++;
  182. return;
  183. }
  184. break;
  185. case ND_REDIRECT:
  186. stat->icp6errs_redirect++;
  187. return;
  188. }
  189. stat->icp6errs_unknown++;
  190. }
  191. /*
  192. * Register a Path MTU Discovery callback.
  193. */
  194. void
  195. icmp6_mtudisc_callback_register(void (*func)(struct sockaddr_in6 *, u_int))
  196. {
  197. struct icmp6_mtudisc_callback *mc;
  198. for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
  199. mc = LIST_NEXT(mc, mc_list)) {
  200. if (mc->mc_func == func)
  201. return;
  202. }
  203. mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
  204. if (mc == NULL)
  205. panic("icmp6_mtudisc_callback_register");
  206. mc->mc_func = func;
  207. LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list);
  208. }
  209. /*
  210. * Generate an error packet of type error in response to bad IP6 packet.
  211. */
  212. void
  213. icmp6_error(struct mbuf *m, int type, int code, int param)
  214. {
  215. struct ip6_hdr *oip6, *nip6;
  216. struct icmp6_hdr *icmp6;
  217. u_int preplen;
  218. int off;
  219. int nxt;
  220. icmp6stat.icp6s_error++;
  221. /* count per-type-code statistics */
  222. icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
  223. if (m->m_len < sizeof(struct ip6_hdr)) {
  224. m = m_pullup(m, sizeof(struct ip6_hdr));
  225. if (m == NULL)
  226. return;
  227. }
  228. oip6 = mtod(m, struct ip6_hdr *);
  229. /*
  230. * If the destination address of the erroneous packet is a multicast
  231. * address, or the packet was sent using link-layer multicast,
  232. * we should basically suppress sending an error (RFC 2463, Section
  233. * 2.4).
  234. * We have two exceptions (the item e.2 in that section):
  235. * - the Packet Too Big message can be sent for path MTU discovery.
  236. * - the Parameter Problem Message that can be allowed an icmp6 error
  237. * in the option type field. This check has been done in
  238. * ip6_unknown_opt(), so we can just check the type and code.
  239. */
  240. if ((m->m_flags & (M_BCAST|M_MCAST) ||
  241. IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
  242. (type != ICMP6_PACKET_TOO_BIG &&
  243. (type != ICMP6_PARAM_PROB ||
  244. code != ICMP6_PARAMPROB_OPTION)))
  245. goto freeit;
  246. /*
  247. * RFC 2463, 2.4 (e.5): source address check.
  248. * XXX: the case of anycast source?
  249. */
  250. if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
  251. IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
  252. goto freeit;
  253. /*
  254. * If we are about to send ICMPv6 against ICMPv6 error/redirect,
  255. * don't do it.
  256. */
  257. nxt = -1;
  258. off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
  259. if (off >= 0 && nxt == IPPROTO_ICMPV6) {
  260. struct icmp6_hdr *icp;
  261. IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
  262. sizeof(*icp));
  263. if (icp == NULL) {
  264. icmp6stat.icp6s_tooshort++;
  265. return;
  266. }
  267. if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
  268. icp->icmp6_type == ND_REDIRECT) {
  269. /*
  270. * ICMPv6 error
  271. * Special case: for redirect (which is
  272. * informational) we must not send icmp6 error.
  273. */
  274. icmp6stat.icp6s_canterror++;
  275. goto freeit;
  276. } else {
  277. /* ICMPv6 informational - send the error */
  278. }
  279. }
  280. else {
  281. /* non-ICMPv6 - send the error */
  282. }
  283. oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
  284. /* Finally, do rate limitation check. */
  285. if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
  286. icmp6stat.icp6s_toofreq++;
  287. goto freeit;
  288. }
  289. /*
  290. * OK, ICMP6 can be generated.
  291. */
  292. if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
  293. m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
  294. preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
  295. M_PREPEND(m, preplen, M_DONTWAIT);
  296. if (m && m->m_len < preplen)
  297. m = m_pullup(m, preplen);
  298. if (m == NULL) {
  299. nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
  300. return;
  301. }
  302. nip6 = mtod(m, struct ip6_hdr *);
  303. nip6->ip6_src = oip6->ip6_src;
  304. nip6->ip6_dst = oip6->ip6_dst;
  305. if (IN6_IS_SCOPE_EMBED(&oip6->ip6_src))
  306. oip6->ip6_src.s6_addr16[1] = 0;
  307. if (IN6_IS_SCOPE_EMBED(&oip6->ip6_dst))
  308. oip6->ip6_dst.s6_addr16[1] = 0;
  309. icmp6 = (struct icmp6_hdr *)(nip6 + 1);
  310. icmp6->icmp6_type = type;
  311. icmp6->icmp6_code = code;
  312. icmp6->icmp6_pptr = htonl((u_int32_t)param);
  313. /*
  314. * icmp6_reflect() is designed to be in the input path.
  315. * icmp6_error() can be called from both input and outut path,
  316. * and if we are in output path rcvif could contain bogus value.
  317. * clear m->m_pkthdr.ph_ifidx for safety, we should have enough
  318. * scope information in ip header (nip6).
  319. */
  320. m->m_pkthdr.ph_ifidx = 0;
  321. icmp6stat.icp6s_outhist[type]++;
  322. icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
  323. return;
  324. freeit:
  325. /*
  326. * If we can't tell wheter or not we can generate ICMP6, free it.
  327. */
  328. m_freem(m);
  329. }
  330. /*
  331. * Process a received ICMP6 message.
  332. */
  333. int
  334. icmp6_input(struct mbuf **mp, int *offp, int proto)
  335. {
  336. struct ifnet *ifp;
  337. struct mbuf *m = *mp, *n;
  338. struct ip6_hdr *ip6, *nip6;
  339. struct icmp6_hdr *icmp6, *nicmp6;
  340. int off = *offp;
  341. int icmp6len = m->m_pkthdr.len - *offp;
  342. int code, sum, noff;
  343. char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
  344. ifp = if_get(m->m_pkthdr.ph_ifidx);
  345. if (ifp == NULL)
  346. goto freeit;
  347. icmp6_ifstat_inc(ifp, ifs6_in_msg);
  348. /*
  349. * Locate icmp6 structure in mbuf, and check
  350. * that not corrupted and of at least minimum length
  351. */
  352. ip6 = mtod(m, struct ip6_hdr *);
  353. if (icmp6len < sizeof(struct icmp6_hdr)) {
  354. icmp6stat.icp6s_tooshort++;
  355. icmp6_ifstat_inc(ifp, ifs6_in_error);
  356. goto freeit;
  357. }
  358. /*
  359. * calculate the checksum
  360. */
  361. IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
  362. if (icmp6 == NULL) {
  363. icmp6stat.icp6s_tooshort++;
  364. return IPPROTO_DONE;
  365. }
  366. code = icmp6->icmp6_code;
  367. if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
  368. nd6log((LOG_ERR,
  369. "ICMP6 checksum error(%d|%x) %s\n",
  370. icmp6->icmp6_type, sum,
  371. inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src))));
  372. icmp6stat.icp6s_checksum++;
  373. icmp6_ifstat_inc(ifp, ifs6_in_error);
  374. goto freeit;
  375. }
  376. #if NPF > 0
  377. if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
  378. switch (icmp6->icmp6_type) {
  379. /*
  380. * These ICMP6 types map to other connections. They must be
  381. * delivered to pr_ctlinput() also for diverted connections.
  382. */
  383. case ICMP6_DST_UNREACH:
  384. case ICMP6_PACKET_TOO_BIG:
  385. case ICMP6_TIME_EXCEEDED:
  386. case ICMP6_PARAM_PROB:
  387. break;
  388. default:
  389. goto raw;
  390. }
  391. }
  392. #endif /* NPF */
  393. #if NCARP > 0
  394. if (ifp->if_type == IFT_CARP &&
  395. icmp6->icmp6_type == ICMP6_ECHO_REQUEST &&
  396. carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
  397. ip6->ip6_dst.s6_addr32))
  398. goto freeit;
  399. #endif
  400. icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
  401. switch (icmp6->icmp6_type) {
  402. case ICMP6_DST_UNREACH:
  403. icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
  404. switch (code) {
  405. case ICMP6_DST_UNREACH_NOROUTE:
  406. code = PRC_UNREACH_NET;
  407. break;
  408. case ICMP6_DST_UNREACH_ADMIN:
  409. icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
  410. code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
  411. break;
  412. case ICMP6_DST_UNREACH_ADDR:
  413. code = PRC_HOSTDEAD;
  414. break;
  415. #ifdef COMPAT_RFC1885
  416. case ICMP6_DST_UNREACH_NOTNEIGHBOR:
  417. code = PRC_UNREACH_SRCFAIL;
  418. break;
  419. #else
  420. case ICMP6_DST_UNREACH_BEYONDSCOPE:
  421. /* I mean "source address was incorrect." */
  422. code = PRC_PARAMPROB;
  423. break;
  424. #endif
  425. case ICMP6_DST_UNREACH_NOPORT:
  426. code = PRC_UNREACH_PORT;
  427. break;
  428. default:
  429. goto badcode;
  430. }
  431. goto deliver;
  432. case ICMP6_PACKET_TOO_BIG:
  433. icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
  434. /* MTU is checked in icmp6_mtudisc_update. */
  435. code = PRC_MSGSIZE;
  436. /*
  437. * Updating the path MTU will be done after examining
  438. * intermediate extension headers.
  439. */
  440. goto deliver;
  441. case ICMP6_TIME_EXCEEDED:
  442. icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
  443. switch (code) {
  444. case ICMP6_TIME_EXCEED_TRANSIT:
  445. code = PRC_TIMXCEED_INTRANS;
  446. break;
  447. case ICMP6_TIME_EXCEED_REASSEMBLY:
  448. code = PRC_TIMXCEED_REASS;
  449. break;
  450. default:
  451. goto badcode;
  452. }
  453. goto deliver;
  454. case ICMP6_PARAM_PROB:
  455. icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
  456. switch (code) {
  457. case ICMP6_PARAMPROB_NEXTHEADER:
  458. code = PRC_UNREACH_PROTOCOL;
  459. break;
  460. case ICMP6_PARAMPROB_HEADER:
  461. case ICMP6_PARAMPROB_OPTION:
  462. code = PRC_PARAMPROB;
  463. break;
  464. default:
  465. goto badcode;
  466. }
  467. goto deliver;
  468. case ICMP6_ECHO_REQUEST:
  469. icmp6_ifstat_inc(ifp, ifs6_in_echo);
  470. if (code != 0)
  471. goto badcode;
  472. /*
  473. * Copy mbuf to send to two data paths: userland socket(s),
  474. * and to the querier (echo reply).
  475. * m: a copy for socket, n: a copy for querier
  476. */
  477. if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
  478. /* Give up local */
  479. n = m;
  480. m = NULL;
  481. goto deliverecho;
  482. }
  483. /*
  484. * If the first mbuf is shared, or the first mbuf is too short,
  485. * copy the first part of the data into a fresh mbuf.
  486. * Otherwise, we will wrongly overwrite both copies.
  487. */
  488. if ((n->m_flags & M_EXT) != 0 ||
  489. n->m_len < off + sizeof(struct icmp6_hdr)) {
  490. struct mbuf *n0 = n;
  491. const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
  492. /*
  493. * Prepare an internal mbuf. m_pullup() doesn't
  494. * always copy the length we specified.
  495. */
  496. if (maxlen >= MCLBYTES) {
  497. /* Give up remote */
  498. m_freem(n0);
  499. break;
  500. }
  501. MGETHDR(n, M_DONTWAIT, n0->m_type);
  502. if (n && maxlen >= MHLEN) {
  503. MCLGET(n, M_DONTWAIT);
  504. if ((n->m_flags & M_EXT) == 0) {
  505. m_free(n);
  506. n = NULL;
  507. }
  508. }
  509. if (n == NULL) {
  510. /* Give up local */
  511. m_freem(n0);
  512. n = m;
  513. m = NULL;
  514. goto deliverecho;
  515. }
  516. M_MOVE_PKTHDR(n, n0);
  517. /*
  518. * Copy IPv6 and ICMPv6 only.
  519. */
  520. nip6 = mtod(n, struct ip6_hdr *);
  521. bcopy(ip6, nip6, sizeof(struct ip6_hdr));
  522. nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
  523. bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
  524. noff = sizeof(struct ip6_hdr);
  525. n->m_len = noff + sizeof(struct icmp6_hdr);
  526. /*
  527. * Adjust mbuf. ip6_plen will be adjusted in
  528. * ip6_output().
  529. * n->m_pkthdr.len == n0->m_pkthdr.len at this point.
  530. */
  531. n->m_pkthdr.len += noff + sizeof(struct icmp6_hdr);
  532. n->m_pkthdr.len -= (off + sizeof(struct icmp6_hdr));
  533. m_adj(n0, off + sizeof(struct icmp6_hdr));
  534. n->m_next = n0;
  535. } else {
  536. deliverecho:
  537. nip6 = mtod(n, struct ip6_hdr *);
  538. IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
  539. sizeof(*nicmp6));
  540. noff = off;
  541. }
  542. nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
  543. nicmp6->icmp6_code = 0;
  544. if (n) {
  545. icmp6stat.icp6s_reflect++;
  546. icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
  547. icmp6_reflect(n, noff);
  548. }
  549. if (!m)
  550. goto freeit;
  551. break;
  552. case ICMP6_ECHO_REPLY:
  553. icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
  554. if (code != 0)
  555. goto badcode;
  556. break;
  557. case MLD_LISTENER_QUERY:
  558. case MLD_LISTENER_REPORT:
  559. if (icmp6len < sizeof(struct mld_hdr))
  560. goto badlen;
  561. if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
  562. icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
  563. else
  564. icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
  565. if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
  566. /* give up local */
  567. mld6_input(m, off);
  568. m = NULL;
  569. goto freeit;
  570. }
  571. mld6_input(n, off);
  572. /* m stays. */
  573. break;
  574. case MLD_LISTENER_DONE:
  575. icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
  576. if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */
  577. goto badlen;
  578. break; /* nothing to be done in kernel */
  579. case MLD_MTRACE_RESP:
  580. case MLD_MTRACE:
  581. /* XXX: these two are experimental. not officially defined. */
  582. /* XXX: per-interface statistics? */
  583. break; /* just pass it to applications */
  584. case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */
  585. /* IPv6 Node Information Queries are not supported */
  586. break;
  587. case ICMP6_WRUREPLY:
  588. break;
  589. case ND_ROUTER_SOLICIT:
  590. icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
  591. if (code != 0)
  592. goto badcode;
  593. if (icmp6len < sizeof(struct nd_router_solicit))
  594. goto badlen;
  595. if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
  596. /* give up local */
  597. nd6_rs_input(m, off, icmp6len);
  598. m = NULL;
  599. goto freeit;
  600. }
  601. nd6_rs_input(n, off, icmp6len);
  602. /* m stays. */
  603. break;
  604. case ND_ROUTER_ADVERT:
  605. icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
  606. if (code != 0)
  607. goto badcode;
  608. if (icmp6len < sizeof(struct nd_router_advert))
  609. goto badlen;
  610. if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
  611. /* give up local */
  612. nd6_ra_input(m, off, icmp6len);
  613. m = NULL;
  614. goto freeit;
  615. }
  616. nd6_ra_input(n, off, icmp6len);
  617. /* m stays. */
  618. break;
  619. case ND_NEIGHBOR_SOLICIT:
  620. icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
  621. if (code != 0)
  622. goto badcode;
  623. if (icmp6len < sizeof(struct nd_neighbor_solicit))
  624. goto badlen;
  625. if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
  626. /* give up local */
  627. nd6_ns_input(m, off, icmp6len);
  628. m = NULL;
  629. goto freeit;
  630. }
  631. nd6_ns_input(n, off, icmp6len);
  632. /* m stays. */
  633. break;
  634. case ND_NEIGHBOR_ADVERT:
  635. icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
  636. if (code != 0)
  637. goto badcode;
  638. if (icmp6len < sizeof(struct nd_neighbor_advert))
  639. goto badlen;
  640. if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
  641. /* give up local */
  642. nd6_na_input(m, off, icmp6len);
  643. m = NULL;
  644. goto freeit;
  645. }
  646. nd6_na_input(n, off, icmp6len);
  647. /* m stays. */
  648. break;
  649. case ND_REDIRECT:
  650. icmp6_ifstat_inc(ifp, ifs6_in_redirect);
  651. if (code != 0)
  652. goto badcode;
  653. if (icmp6len < sizeof(struct nd_redirect))
  654. goto badlen;
  655. if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
  656. /* give up local */
  657. icmp6_redirect_input(m, off);
  658. m = NULL;
  659. goto freeit;
  660. }
  661. icmp6_redirect_input(n, off);
  662. /* m stays. */
  663. break;
  664. case ICMP6_ROUTER_RENUMBERING:
  665. if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
  666. code != ICMP6_ROUTER_RENUMBERING_RESULT)
  667. goto badcode;
  668. if (icmp6len < sizeof(struct icmp6_router_renum))
  669. goto badlen;
  670. break;
  671. default:
  672. nd6log((LOG_DEBUG,
  673. "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
  674. icmp6->icmp6_type,
  675. inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src)),
  676. inet_ntop(AF_INET6, &ip6->ip6_dst, dst, sizeof(dst)),
  677. ifp ? ifp->if_index : 0));
  678. if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
  679. /* ICMPv6 error: MUST deliver it by spec... */
  680. code = PRC_NCMDS;
  681. /* deliver */
  682. } else {
  683. /* ICMPv6 informational: MUST not deliver */
  684. break;
  685. }
  686. deliver:
  687. if (icmp6_notify_error(m, off, icmp6len, code)) {
  688. /* In this case, m should've been freed. */
  689. return (IPPROTO_DONE);
  690. }
  691. break;
  692. badcode:
  693. icmp6stat.icp6s_badcode++;
  694. break;
  695. badlen:
  696. icmp6stat.icp6s_badlen++;
  697. break;
  698. }
  699. #if NPF > 0
  700. raw:
  701. #endif
  702. /* deliver the packet to appropriate sockets */
  703. icmp6_rip6_input(&m, *offp);
  704. return IPPROTO_DONE;
  705. freeit:
  706. m_freem(m);
  707. return IPPROTO_DONE;
  708. }
  709. int
  710. icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
  711. {
  712. struct icmp6_hdr *icmp6;
  713. struct ip6_hdr *eip6;
  714. u_int32_t notifymtu;
  715. struct sockaddr_in6 icmp6src, icmp6dst;
  716. if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
  717. icmp6stat.icp6s_tooshort++;
  718. goto freeit;
  719. }
  720. IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
  721. sizeof(*icmp6) + sizeof(struct ip6_hdr));
  722. if (icmp6 == NULL) {
  723. icmp6stat.icp6s_tooshort++;
  724. return (-1);
  725. }
  726. eip6 = (struct ip6_hdr *)(icmp6 + 1);
  727. /* Detect the upper level protocol */
  728. {
  729. void (*ctlfunc)(int, struct sockaddr *, u_int, void *);
  730. u_int8_t nxt = eip6->ip6_nxt;
  731. int eoff = off + sizeof(struct icmp6_hdr) +
  732. sizeof(struct ip6_hdr);
  733. struct ip6ctlparam ip6cp;
  734. struct in6_addr *finaldst = NULL;
  735. int icmp6type = icmp6->icmp6_type;
  736. struct ip6_frag *fh;
  737. struct ip6_rthdr *rth;
  738. struct ip6_rthdr0 *rth0;
  739. int rthlen;
  740. while (1) { /* XXX: should avoid infinite loop explicitly? */
  741. struct ip6_ext *eh;
  742. switch (nxt) {
  743. case IPPROTO_HOPOPTS:
  744. case IPPROTO_DSTOPTS:
  745. case IPPROTO_AH:
  746. IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
  747. eoff, sizeof(*eh));
  748. if (eh == NULL) {
  749. icmp6stat.icp6s_tooshort++;
  750. return (-1);
  751. }
  752. if (nxt == IPPROTO_AH)
  753. eoff += (eh->ip6e_len + 2) << 2;
  754. else
  755. eoff += (eh->ip6e_len + 1) << 3;
  756. nxt = eh->ip6e_nxt;
  757. break;
  758. case IPPROTO_ROUTING:
  759. /*
  760. * When the erroneous packet contains a
  761. * routing header, we should examine the
  762. * header to determine the final destination.
  763. * Otherwise, we can't properly update
  764. * information that depends on the final
  765. * destination (e.g. path MTU).
  766. */
  767. IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
  768. eoff, sizeof(*rth));
  769. if (rth == NULL) {
  770. icmp6stat.icp6s_tooshort++;
  771. return (-1);
  772. }
  773. rthlen = (rth->ip6r_len + 1) << 3;
  774. /*
  775. * XXX: currently there is no
  776. * officially defined type other
  777. * than type-0.
  778. * Note that if the segment left field
  779. * is 0, all intermediate hops must
  780. * have been passed.
  781. */
  782. if (rth->ip6r_segleft &&
  783. rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
  784. int hops;
  785. IP6_EXTHDR_GET(rth0,
  786. struct ip6_rthdr0 *, m,
  787. eoff, rthlen);
  788. if (rth0 == NULL) {
  789. icmp6stat.icp6s_tooshort++;
  790. return (-1);
  791. }
  792. /* just ignore a bogus header */
  793. if ((rth0->ip6r0_len % 2) == 0 &&
  794. (hops = rth0->ip6r0_len/2))
  795. finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
  796. }
  797. eoff += rthlen;
  798. nxt = rth->ip6r_nxt;
  799. break;
  800. case IPPROTO_FRAGMENT:
  801. IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
  802. eoff, sizeof(*fh));
  803. if (fh == NULL) {
  804. icmp6stat.icp6s_tooshort++;
  805. return (-1);
  806. }
  807. /*
  808. * Data after a fragment header is meaningless
  809. * unless it is the first fragment, but
  810. * we'll go to the notify label for path MTU
  811. * discovery.
  812. */
  813. if (fh->ip6f_offlg & IP6F_OFF_MASK)
  814. goto notify;
  815. eoff += sizeof(struct ip6_frag);
  816. nxt = fh->ip6f_nxt;
  817. break;
  818. default:
  819. /*
  820. * This case includes ESP and the No Next
  821. * Header. In such cases going to the notify
  822. * label does not have any meaning
  823. * (i.e. ctlfunc will be NULL), but we go
  824. * anyway since we might have to update
  825. * path MTU information.
  826. */
  827. goto notify;
  828. }
  829. }
  830. notify:
  831. IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
  832. sizeof(*icmp6) + sizeof(struct ip6_hdr));
  833. if (icmp6 == NULL) {
  834. icmp6stat.icp6s_tooshort++;
  835. return (-1);
  836. }
  837. eip6 = (struct ip6_hdr *)(icmp6 + 1);
  838. bzero(&icmp6dst, sizeof(icmp6dst));
  839. icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
  840. icmp6dst.sin6_family = AF_INET6;
  841. if (finaldst == NULL)
  842. icmp6dst.sin6_addr = eip6->ip6_dst;
  843. else
  844. icmp6dst.sin6_addr = *finaldst;
  845. icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.ph_ifidx,
  846. &icmp6dst.sin6_addr);
  847. if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
  848. NULL, NULL)) {
  849. /* should be impossbile */
  850. nd6log((LOG_DEBUG,
  851. "icmp6_notify_error: in6_embedscope failed\n"));
  852. goto freeit;
  853. }
  854. /*
  855. * retrieve parameters from the inner IPv6 header, and convert
  856. * them into sockaddr structures.
  857. */
  858. bzero(&icmp6src, sizeof(icmp6src));
  859. icmp6src.sin6_len = sizeof(struct sockaddr_in6);
  860. icmp6src.sin6_family = AF_INET6;
  861. icmp6src.sin6_addr = eip6->ip6_src;
  862. icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.ph_ifidx,
  863. &icmp6src.sin6_addr);
  864. if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
  865. NULL, NULL)) {
  866. /* should be impossbile */
  867. nd6log((LOG_DEBUG,
  868. "icmp6_notify_error: in6_embedscope failed\n"));
  869. goto freeit;
  870. }
  871. icmp6src.sin6_flowinfo =
  872. (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
  873. if (finaldst == NULL)
  874. finaldst = &eip6->ip6_dst;
  875. ip6cp.ip6c_m = m;
  876. ip6cp.ip6c_icmp6 = icmp6;
  877. ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
  878. ip6cp.ip6c_off = eoff;
  879. ip6cp.ip6c_finaldst = finaldst;
  880. ip6cp.ip6c_src = &icmp6src;
  881. ip6cp.ip6c_nxt = nxt;
  882. #if NPF > 0
  883. pf_pkt_addr_changed(m);
  884. #endif
  885. if (icmp6type == ICMP6_PACKET_TOO_BIG) {
  886. notifymtu = ntohl(icmp6->icmp6_mtu);
  887. ip6cp.ip6c_cmdarg = (void *)&notifymtu;
  888. }
  889. ctlfunc = inet6sw[ip6_protox[nxt]].pr_ctlinput;
  890. if (ctlfunc)
  891. (*ctlfunc)(code, sin6tosa(&icmp6dst),
  892. m->m_pkthdr.ph_rtableid, &ip6cp);
  893. }
  894. return (0);
  895. freeit:
  896. m_freem(m);
  897. return (-1);
  898. }
  899. void
  900. icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
  901. {
  902. unsigned long rtcount;
  903. struct icmp6_mtudisc_callback *mc;
  904. struct in6_addr *dst = ip6cp->ip6c_finaldst;
  905. struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
  906. struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */
  907. u_int mtu = ntohl(icmp6->icmp6_mtu);
  908. struct rtentry *rt = NULL;
  909. struct sockaddr_in6 sin6;
  910. /*
  911. * The MTU may not be less then the minimal IPv6 MTU except for the
  912. * hack in ip6_output/ip6_setpmtu where we always include a frag header.
  913. */
  914. if (mtu < IPV6_MMTU - sizeof(struct ip6_frag))
  915. return;
  916. /*
  917. * allow non-validated cases if memory is plenty, to make traffic
  918. * from non-connected pcb happy.
  919. */
  920. rtcount = rt_timer_queue_count(icmp6_mtudisc_timeout_q);
  921. if (validated) {
  922. if (0 <= icmp6_mtudisc_hiwat && rtcount > icmp6_mtudisc_hiwat)
  923. return;
  924. else if (0 <= icmp6_mtudisc_lowat &&
  925. rtcount > icmp6_mtudisc_lowat) {
  926. /*
  927. * XXX nuke a victim, install the new one.
  928. */
  929. }
  930. } else {
  931. if (0 <= icmp6_mtudisc_lowat && rtcount > icmp6_mtudisc_lowat)
  932. return;
  933. }
  934. bzero(&sin6, sizeof(sin6));
  935. sin6.sin6_family = PF_INET6;
  936. sin6.sin6_len = sizeof(struct sockaddr_in6);
  937. sin6.sin6_addr = *dst;
  938. /* XXX normally, this won't happen */
  939. if (IN6_IS_ADDR_LINKLOCAL(dst)) {
  940. sin6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.ph_ifidx);
  941. }
  942. sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.ph_ifidx,
  943. &sin6.sin6_addr);
  944. rt = icmp6_mtudisc_clone(sin6tosa(&sin6), m->m_pkthdr.ph_rtableid);
  945. if (rt && (rt->rt_flags & RTF_HOST) &&
  946. !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
  947. (rt->rt_rmx.rmx_mtu > mtu || rt->rt_rmx.rmx_mtu == 0)) {
  948. if (mtu < IN6_LINKMTU(rt->rt_ifp)) {
  949. icmp6stat.icp6s_pmtuchg++;
  950. rt->rt_rmx.rmx_mtu = mtu;
  951. }
  952. }
  953. if (rt)
  954. rtfree(rt);
  955. /*
  956. * Notify protocols that the MTU for this destination
  957. * has changed.
  958. */
  959. for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
  960. mc = LIST_NEXT(mc, mc_list))
  961. (*mc->mc_func)(&sin6, m->m_pkthdr.ph_rtableid);
  962. }
  963. /*
  964. * XXX almost dup'ed code with rip6_input.
  965. */
  966. int
  967. icmp6_rip6_input(struct mbuf **mp, int off)
  968. {
  969. struct mbuf *m = *mp;
  970. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  971. struct inpcb *in6p;
  972. struct inpcb *last = NULL;
  973. struct sockaddr_in6 rip6src;
  974. struct icmp6_hdr *icmp6;
  975. struct mbuf *opts = NULL;
  976. IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
  977. if (icmp6 == NULL) {
  978. /* m is already reclaimed */
  979. return IPPROTO_DONE;
  980. }
  981. bzero(&rip6src, sizeof(rip6src));
  982. rip6src.sin6_len = sizeof(struct sockaddr_in6);
  983. rip6src.sin6_family = AF_INET6;
  984. /* KAME hack: recover scopeid */
  985. (void)in6_recoverscope(&rip6src, &ip6->ip6_src, NULL);
  986. TAILQ_FOREACH(in6p, &rawin6pcbtable.inpt_queue, inp_queue) {
  987. if (!(in6p->inp_flags & INP_IPV6))
  988. continue;
  989. if (in6p->inp_ipv6.ip6_nxt != IPPROTO_ICMPV6)
  990. continue;
  991. #if NPF > 0
  992. if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
  993. struct pf_divert *divert;
  994. /* XXX rdomain support */
  995. if ((divert = pf_find_divert(m)) == NULL)
  996. continue;
  997. if (IN6_IS_ADDR_UNSPECIFIED(&divert->addr.v6))
  998. goto divert_reply;
  999. if (!IN6_ARE_ADDR_EQUAL(&in6p->inp_laddr6,
  1000. &divert->addr.v6))
  1001. continue;
  1002. } else
  1003. divert_reply:
  1004. #endif
  1005. if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_laddr6) &&
  1006. !IN6_ARE_ADDR_EQUAL(&in6p->inp_laddr6, &ip6->ip6_dst))
  1007. continue;
  1008. if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_faddr6) &&
  1009. !IN6_ARE_ADDR_EQUAL(&in6p->inp_faddr6, &ip6->ip6_src))
  1010. continue;
  1011. if (in6p->inp_icmp6filt
  1012. && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
  1013. in6p->inp_icmp6filt))
  1014. continue;
  1015. if (last) {
  1016. struct mbuf *n;
  1017. if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
  1018. if (last->inp_flags & IN6P_CONTROLOPTS)
  1019. ip6_savecontrol(last, n, &opts);
  1020. /* strip intermediate headers */
  1021. m_adj(n, off);
  1022. if (sbappendaddr(&last->inp_socket->so_rcv,
  1023. sin6tosa(&rip6src), n, opts) == 0) {
  1024. /* should notify about lost packet */
  1025. m_freem(n);
  1026. m_freem(opts);
  1027. } else
  1028. sorwakeup(last->inp_socket);
  1029. opts = NULL;
  1030. }
  1031. }
  1032. last = in6p;
  1033. }
  1034. if (last) {
  1035. if (last->inp_flags & IN6P_CONTROLOPTS)
  1036. ip6_savecontrol(last, m, &opts);
  1037. /* strip intermediate headers */
  1038. m_adj(m, off);
  1039. if (sbappendaddr(&last->inp_socket->so_rcv,
  1040. sin6tosa(&rip6src), m, opts) == 0) {
  1041. m_freem(m);
  1042. m_freem(opts);
  1043. } else
  1044. sorwakeup(last->inp_socket);
  1045. } else {
  1046. m_freem(m);
  1047. ip6stat.ip6s_delivered--;
  1048. }
  1049. return IPPROTO_DONE;
  1050. }
  1051. /*
  1052. * Reflect the ip6 packet back to the source.
  1053. * OFF points to the icmp6 header, counted from the top of the mbuf.
  1054. *
  1055. * Note: RFC 1885 required that an echo reply should be truncated if it
  1056. * did not fit in with (return) path MTU, and KAME code supported the
  1057. * behavior. However, as a clarification after the RFC, this limitation
  1058. * was removed in a revised version of the spec, RFC 2463. We had kept the
  1059. * old behavior, with a (non-default) ifdef block, while the new version of
  1060. * the spec was an internet-draft status, and even after the new RFC was
  1061. * published. But it would rather make sense to clean the obsoleted part
  1062. * up, and to make the code simpler at this stage.
  1063. */
  1064. void
  1065. icmp6_reflect(struct mbuf *m, size_t off)
  1066. {
  1067. struct ip6_hdr *ip6;
  1068. struct icmp6_hdr *icmp6;
  1069. struct in6_ifaddr *ia6;
  1070. struct in6_addr t, *src = NULL;
  1071. int plen;
  1072. int type, code;
  1073. struct ifnet *outif = NULL;
  1074. struct sockaddr_in6 sa6_src, sa6_dst;
  1075. /* too short to reflect */
  1076. if (off < sizeof(struct ip6_hdr)) {
  1077. nd6log((LOG_DEBUG,
  1078. "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
  1079. (u_long)off, (u_long)sizeof(struct ip6_hdr),
  1080. __FILE__, __LINE__));
  1081. goto bad;
  1082. }
  1083. /*
  1084. * If there are extra headers between IPv6 and ICMPv6, strip
  1085. * off that header first.
  1086. */
  1087. #ifdef DIAGNOSTIC
  1088. if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
  1089. panic("assumption failed in icmp6_reflect");
  1090. #endif
  1091. if (off > sizeof(struct ip6_hdr)) {
  1092. size_t l;
  1093. struct ip6_hdr nip6;
  1094. l = off - sizeof(struct ip6_hdr);
  1095. m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
  1096. m_adj(m, l);
  1097. l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
  1098. if (m->m_len < l) {
  1099. if ((m = m_pullup(m, l)) == NULL)
  1100. return;
  1101. }
  1102. bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
  1103. } else /* off == sizeof(struct ip6_hdr) */ {
  1104. size_t l;
  1105. l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
  1106. if (m->m_len < l) {
  1107. if ((m = m_pullup(m, l)) == NULL)
  1108. return;
  1109. }
  1110. }
  1111. plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
  1112. ip6 = mtod(m, struct ip6_hdr *);
  1113. ip6->ip6_nxt = IPPROTO_ICMPV6;
  1114. icmp6 = (struct icmp6_hdr *)(ip6 + 1);
  1115. type = icmp6->icmp6_type; /* keep type for statistics */
  1116. code = icmp6->icmp6_code; /* ditto. */
  1117. t = ip6->ip6_dst;
  1118. /*
  1119. * ip6_input() drops a packet if its src is multicast.
  1120. * So, the src is never multicast.
  1121. */
  1122. ip6->ip6_dst = ip6->ip6_src;
  1123. /*
  1124. * XXX: make sure to embed scope zone information, using
  1125. * already embedded IDs or the received interface (if any).
  1126. * Note that rcvif may be NULL.
  1127. * TODO: scoped routing case (XXX).
  1128. */
  1129. bzero(&sa6_src, sizeof(sa6_src));
  1130. sa6_src.sin6_family = AF_INET6;
  1131. sa6_src.sin6_len = sizeof(sa6_src);
  1132. sa6_src.sin6_addr = ip6->ip6_dst;
  1133. in6_recoverscope(&sa6_src, &ip6->ip6_dst, if_get(m->m_pkthdr.ph_ifidx));
  1134. in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
  1135. bzero(&sa6_dst, sizeof(sa6_dst));
  1136. sa6_dst.sin6_family = AF_INET6;
  1137. sa6_dst.sin6_len = sizeof(sa6_dst);
  1138. sa6_dst.sin6_addr = t;
  1139. in6_recoverscope(&sa6_dst, &t, if_get(m->m_pkthdr.ph_ifidx));
  1140. in6_embedscope(&t, &sa6_dst, NULL, NULL);
  1141. /*
  1142. * If the incoming packet was addressed directly to us (i.e. unicast),
  1143. * use dst as the src for the reply.
  1144. * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
  1145. * (for example) when we encounter an error while forwarding procedure
  1146. * destined to a duplicated address of ours.
  1147. */
  1148. TAILQ_FOREACH(ia6, &in6_ifaddr, ia_list)
  1149. if (IN6_ARE_ADDR_EQUAL(&t, &ia6->ia_addr.sin6_addr) &&
  1150. (ia6->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
  1151. src = &t;
  1152. break;
  1153. }
  1154. if (ia6 == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
  1155. /*
  1156. * This is the case if the dst is our link-local address
  1157. * and the sender is also ourselves.
  1158. */
  1159. src = &t;
  1160. }
  1161. if (src == NULL) {
  1162. int error;
  1163. struct route_in6 ro;
  1164. char addr[INET6_ADDRSTRLEN];
  1165. /*
  1166. * This case matches to multicasts, our anycast, or unicasts
  1167. * that we do not own. Select a source address based on the
  1168. * source address of the erroneous packet.
  1169. */
  1170. bzero(&ro, sizeof(ro));
  1171. error = in6_selectsrc(&src, &sa6_src, NULL, NULL, &ro, NULL,
  1172. m->m_pkthdr.ph_rtableid);
  1173. if (ro.ro_rt)
  1174. rtfree(ro.ro_rt); /* XXX: we could use this */
  1175. if (error) {
  1176. nd6log((LOG_DEBUG,
  1177. "icmp6_reflect: source can't be determined: "
  1178. "dst=%s, error=%d\n",
  1179. inet_ntop(AF_INET6, &sa6_src.sin6_addr,
  1180. addr, sizeof(addr)),
  1181. error));
  1182. goto bad;
  1183. }
  1184. }
  1185. ip6->ip6_src = *src;
  1186. ip6->ip6_flow = 0;
  1187. ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  1188. ip6->ip6_vfc |= IPV6_VERSION;
  1189. ip6->ip6_nxt = IPPROTO_ICMPV6;
  1190. if (if_get(m->m_pkthdr.ph_ifidx) != NULL) {
  1191. /* XXX: This may not be the outgoing interface */
  1192. ip6->ip6_hlim = ND_IFINFO(if_get(m->m_pkthdr.ph_ifidx))->chlim;
  1193. } else
  1194. ip6->ip6_hlim = ip6_defhlim;
  1195. icmp6->icmp6_cksum = 0;
  1196. m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
  1197. /*
  1198. * XXX option handling
  1199. */
  1200. m->m_flags &= ~(M_BCAST|M_MCAST);
  1201. /*
  1202. * To avoid a "too big" situation at an intermediate router
  1203. * and the path MTU discovery process, specify the IPV6_MINMTU flag.
  1204. * Note that only echo and node information replies are affected,
  1205. * since the length of ICMP6 errors is limited to the minimum MTU.
  1206. */
  1207. #if NPF > 0
  1208. pf_pkt_addr_changed(m);
  1209. #endif
  1210. if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, &outif, NULL) != 0 &&
  1211. outif)
  1212. icmp6_ifstat_inc(outif, ifs6_out_error);
  1213. if (outif)
  1214. icmp6_ifoutstat_inc(outif, type, code);
  1215. return;
  1216. bad:
  1217. m_freem(m);
  1218. return;
  1219. }
  1220. void
  1221. icmp6_fasttimo(void)
  1222. {
  1223. mld6_fasttimeo();
  1224. }
  1225. const char *
  1226. icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
  1227. struct in6_addr *tgt6)
  1228. {
  1229. static char buf[1024]; /* XXX */
  1230. char src[INET6_ADDRSTRLEN];
  1231. char dst[INET6_ADDRSTRLEN];
  1232. char tgt[INET6_ADDRSTRLEN];
  1233. snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
  1234. inet_ntop(AF_INET6, src6, src, sizeof(src)),
  1235. inet_ntop(AF_INET6, dst6, dst, sizeof(dst)),
  1236. inet_ntop(AF_INET6, tgt6, tgt, sizeof(tgt)));
  1237. return buf;
  1238. }
  1239. void
  1240. icmp6_redirect_input(struct mbuf *m, int off)
  1241. {
  1242. struct ifnet *ifp;
  1243. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  1244. struct nd_redirect *nd_rd;
  1245. int icmp6len = ntohs(ip6->ip6_plen);
  1246. char *lladdr = NULL;
  1247. int lladdrlen = 0;
  1248. struct rtentry *rt = NULL;
  1249. int is_router;
  1250. int is_onlink;
  1251. struct in6_addr src6 = ip6->ip6_src;
  1252. struct in6_addr redtgt6;
  1253. struct in6_addr reddst6;
  1254. union nd_opts ndopts;
  1255. char addr[INET6_ADDRSTRLEN];
  1256. ifp = if_get(m->m_pkthdr.ph_ifidx);
  1257. if (ifp == NULL)
  1258. return;
  1259. /* XXX if we are router, we don't update route by icmp6 redirect */
  1260. if (ip6_forwarding)
  1261. goto freeit;
  1262. if (!(ifp->if_xflags & IFXF_AUTOCONF6))
  1263. goto freeit;
  1264. IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
  1265. if (nd_rd == NULL) {
  1266. icmp6stat.icp6s_tooshort++;
  1267. return;
  1268. }
  1269. redtgt6 = nd_rd->nd_rd_target;
  1270. reddst6 = nd_rd->nd_rd_dst;
  1271. if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
  1272. redtgt6.s6_addr16[1] = htons(ifp->if_index);
  1273. if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
  1274. reddst6.s6_addr16[1] = htons(ifp->if_index);
  1275. /* validation */
  1276. if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
  1277. nd6log((LOG_ERR,
  1278. "ICMP6 redirect sent from %s rejected; "
  1279. "must be from linklocal\n",
  1280. inet_ntop(AF_INET6, &src6, addr, sizeof(addr))));
  1281. goto bad;
  1282. }
  1283. if (ip6->ip6_hlim != 255) {
  1284. nd6log((LOG_ERR,
  1285. "ICMP6 redirect sent from %s rejected; "
  1286. "hlim=%d (must be 255)\n",
  1287. inet_ntop(AF_INET6, &src6, addr, sizeof(addr)),
  1288. ip6->ip6_hlim));
  1289. goto bad;
  1290. }
  1291. if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
  1292. nd6log((LOG_ERR,
  1293. "ICMP6 redirect rejected; "
  1294. "redirect dst must be unicast: %s\n",
  1295. icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
  1296. goto bad;
  1297. }
  1298. {
  1299. /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
  1300. struct sockaddr_in6 sin6;
  1301. struct in6_addr *gw6;
  1302. bzero(&sin6, sizeof(sin6));
  1303. sin6.sin6_family = AF_INET6;
  1304. sin6.sin6_len = sizeof(struct sockaddr_in6);
  1305. bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
  1306. rt = rtalloc(sin6tosa(&sin6), 0, m->m_pkthdr.ph_rtableid);
  1307. if (rt) {
  1308. if (rt->rt_gateway == NULL ||
  1309. rt->rt_gateway->sa_family != AF_INET6) {
  1310. nd6log((LOG_ERR,
  1311. "ICMP6 redirect rejected; no route "
  1312. "with inet6 gateway found for redirect dst: %s\n",
  1313. icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
  1314. rtfree(rt);
  1315. goto bad;
  1316. }
  1317. gw6 = &(satosin6(rt->rt_gateway)->sin6_addr);
  1318. if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
  1319. nd6log((LOG_ERR,
  1320. "ICMP6 redirect rejected; "
  1321. "not equal to gw-for-src=%s (must be same): "
  1322. "%s\n",
  1323. inet_ntop(AF_INET6, gw6, addr, sizeof(addr)),
  1324. icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
  1325. rtfree(rt);
  1326. goto bad;
  1327. }
  1328. } else {
  1329. nd6log((LOG_ERR,
  1330. "ICMP6 redirect rejected; "
  1331. "no route found for redirect dst: %s\n",
  1332. icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
  1333. goto bad;
  1334. }
  1335. rtfree(rt);
  1336. rt = NULL;
  1337. }
  1338. is_router = is_onlink = 0;
  1339. if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
  1340. is_router = 1; /* router case */
  1341. if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
  1342. is_onlink = 1; /* on-link destination case */
  1343. if (!is_router && !is_onlink) {
  1344. nd6log((LOG_ERR,
  1345. "ICMP6 redirect rejected; "
  1346. "neither router case nor onlink case: %s\n",
  1347. icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
  1348. goto bad;
  1349. }
  1350. /* validation passed */
  1351. icmp6len -= sizeof(*nd_rd);
  1352. nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
  1353. if (nd6_options(&ndopts) < 0) {
  1354. nd6log((LOG_INFO, "icmp6_redirect_input: "
  1355. "invalid ND option, rejected: %s\n",
  1356. icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
  1357. /* nd6_options have incremented stats */
  1358. goto freeit;
  1359. }
  1360. if (ndopts.nd_opts_tgt_lladdr) {
  1361. lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
  1362. lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
  1363. }
  1364. if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  1365. nd6log((LOG_INFO,
  1366. "icmp6_redirect_input: lladdrlen mismatch for %s "
  1367. "(if %d, icmp6 packet %d): %s\n",
  1368. inet_ntop(AF_INET6, &redtgt6, addr, sizeof(addr)),
  1369. ifp->if_addrlen, lladdrlen - 2,
  1370. icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
  1371. goto bad;
  1372. }
  1373. /* RFC 2461 8.3 */
  1374. nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
  1375. is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
  1376. if (!is_onlink) { /* better router case. perform rtredirect. */
  1377. /* perform rtredirect */
  1378. struct sockaddr_in6 sdst;
  1379. struct sockaddr_in6 sgw;
  1380. struct sockaddr_in6 ssrc;
  1381. unsigned long rtcount;
  1382. struct rtentry *newrt = NULL;
  1383. /*
  1384. * do not install redirect route, if the number of entries
  1385. * is too much (> hiwat). note that, the node (= host) will
  1386. * work just fine even if we do not install redirect route
  1387. * (there will be additional hops, though).
  1388. */
  1389. rtcount = rt_timer_queue_count(icmp6_redirect_timeout_q);
  1390. if (0 <= ip6_maxdynroutes && rtcount >= ip6_maxdynroutes)
  1391. goto freeit;
  1392. else if (0 <= icmp6_redirect_lowat &&
  1393. rtcount > icmp6_redirect_lowat) {
  1394. /*
  1395. * XXX nuke a victim, install the new one.
  1396. */
  1397. }
  1398. bzero(&sdst, sizeof(sdst));
  1399. bzero(&sgw, sizeof(sgw));
  1400. bzero(&ssrc, sizeof(ssrc));
  1401. sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
  1402. sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
  1403. sizeof(struct sockaddr_in6);
  1404. bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
  1405. bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
  1406. bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
  1407. rtredirect(sin6tosa(&sdst), sin6tosa(&sgw), NULL,
  1408. RTF_GATEWAY | RTF_HOST, sin6tosa(&ssrc),
  1409. &newrt, m->m_pkthdr.ph_rtableid);
  1410. if (newrt) {
  1411. (void)rt_timer_add(newrt, icmp6_redirect_timeout,
  1412. icmp6_redirect_timeout_q, m->m_pkthdr.ph_rtableid);
  1413. rtfree(newrt);
  1414. }
  1415. }
  1416. /* finally update cached route in each socket via pfctlinput */
  1417. {
  1418. struct sockaddr_in6 sdst;
  1419. bzero(&sdst, sizeof(sdst));
  1420. sdst.sin6_family = AF_INET6;
  1421. sdst.sin6_len = sizeof(struct sockaddr_in6);
  1422. bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
  1423. pfctlinput(PRC_REDIRECT_HOST, sin6tosa(&sdst));
  1424. }
  1425. freeit:
  1426. m_freem(m);
  1427. return;
  1428. bad:
  1429. icmp6stat.icp6s_badredirect++;
  1430. m_freem(m);
  1431. }
  1432. void
  1433. icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
  1434. {
  1435. struct ifnet *ifp; /* my outgoing interface */
  1436. struct in6_addr *ifp_ll6;
  1437. struct in6_addr *nexthop;
  1438. struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */
  1439. struct mbuf *m = NULL; /* newly allocated one */
  1440. struct ip6_hdr *ip6; /* m as struct ip6_hdr */
  1441. struct nd_redirect *nd_rd;
  1442. size_t maxlen;
  1443. u_char *p;
  1444. struct sockaddr_in6 src_sa;
  1445. icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
  1446. /* if we are not router, we don't send icmp6 redirect */
  1447. if (!ip6_forwarding)
  1448. goto fail;
  1449. /* sanity check */
  1450. if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
  1451. goto fail;
  1452. /*
  1453. * Address check:
  1454. * the source address must identify a neighbor, and
  1455. * the destination address must not be a multicast address
  1456. * [RFC 2461, sec 8.2]
  1457. */
  1458. sip6 = mtod(m0, struct ip6_hdr *);
  1459. bzero(&src_sa, sizeof(src_sa));
  1460. src_sa.sin6_family = AF_INET6;
  1461. src_sa.sin6_len = sizeof(src_sa);
  1462. src_sa.sin6_addr = sip6->ip6_src;
  1463. /* we don't currently use sin6_scope_id, but eventually use it */
  1464. src_sa.sin6_scope_id = in6_addr2scopeid(ifp->if_index, &sip6->ip6_src);
  1465. if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
  1466. goto fail;
  1467. if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
  1468. goto fail; /* what should we do here? */
  1469. /* rate limit */
  1470. if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
  1471. goto fail;
  1472. /*
  1473. * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
  1474. * we almost always ask for an mbuf cluster for simplicity.
  1475. * (MHLEN < IPV6_MMTU is almost always true)
  1476. */
  1477. #if IPV6_MMTU >= MCLBYTES
  1478. # error assumption failed about IPV6_MMTU and MCLBYTES
  1479. #endif
  1480. MGETHDR(m, M_DONTWAIT, MT_HEADER);
  1481. if (m && IPV6_MMTU >= MHLEN)
  1482. MCLGET(m, M_DONTWAIT);
  1483. if (!m)
  1484. goto fail;
  1485. m->m_pkthdr.ph_ifidx = 0;
  1486. m->m_len = 0;
  1487. maxlen = M_TRAILINGSPACE(m);
  1488. maxlen = min(IPV6_MMTU, maxlen);
  1489. /* just for safety */
  1490. if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
  1491. ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
  1492. goto fail;
  1493. }
  1494. {
  1495. /* get ip6 linklocal address for ifp(my outgoing interface). */
  1496. struct in6_ifaddr *ia6;
  1497. if ((ia6 = in6ifa_ifpforlinklocal(ifp,
  1498. IN6_IFF_NOTREADY|
  1499. IN6_IFF_ANYCAST)) == NULL)
  1500. goto fail;
  1501. ifp_ll6 = &ia6->ia_addr.sin6_addr;
  1502. }
  1503. /* get ip6 linklocal address for the router. */
  1504. if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
  1505. struct sockaddr_in6 *sin6;
  1506. sin6 = satosin6(rt->rt_gateway);
  1507. nexthop = &sin6->sin6_addr;
  1508. if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
  1509. nexthop = NULL;
  1510. } else
  1511. nexthop = NULL;
  1512. /* ip6 */
  1513. ip6 = mtod(m, struct ip6_hdr *);
  1514. ip6->ip6_flow = 0;
  1515. ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  1516. ip6->ip6_vfc |= IPV6_VERSION;
  1517. /* ip6->ip6_plen will be set later */
  1518. ip6->ip6_nxt = IPPROTO_ICMPV6;
  1519. ip6->ip6_hlim = 255;
  1520. /* ip6->ip6_src must be linklocal addr for my outgoing if. */
  1521. bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
  1522. bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
  1523. /* ND Redirect */
  1524. nd_rd = (struct nd_redirect *)(ip6 + 1);
  1525. nd_rd->nd_rd_type = ND_REDIRECT;
  1526. nd_rd->nd_rd_code = 0;
  1527. nd_rd->nd_rd_reserved = 0;
  1528. if (rt->rt_flags & RTF_GATEWAY) {
  1529. /*
  1530. * nd_rd->nd_rd_target must be a link-local address in
  1531. * better router cases.
  1532. */
  1533. if (!nexthop)
  1534. goto fail;
  1535. bcopy(nexthop, &nd_rd->nd_rd_target,
  1536. sizeof(nd_rd->nd_rd_target));
  1537. bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
  1538. sizeof(nd_rd->nd_rd_dst));
  1539. } else {
  1540. /* make sure redtgt == reddst */
  1541. nexthop = &sip6->ip6_dst;
  1542. bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
  1543. sizeof(nd_rd->nd_rd_target));
  1544. bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
  1545. sizeof(nd_rd->nd_rd_dst));
  1546. }
  1547. p = (u_char *)(nd_rd + 1);
  1548. {
  1549. /* target lladdr option */
  1550. struct rtentry *rt_nexthop = NULL;
  1551. int len;
  1552. struct sockaddr_dl *sdl;
  1553. struct nd_opt_hdr *nd_opt;
  1554. char *lladdr;
  1555. rt_nexthop = nd6_lookup(nexthop, 0, ifp, ifp->if_rdomain);
  1556. if (!rt_nexthop)
  1557. goto nolladdropt;
  1558. len = sizeof(*nd_opt) + ifp->if_addrlen;
  1559. len = (len + 7) & ~7; /* round by 8 */
  1560. /* safety check */
  1561. if (len + (p - (u_char *)ip6) > maxlen)
  1562. goto nolladdropt;
  1563. if (!(rt_nexthop->rt_flags & RTF_GATEWAY) &&
  1564. (rt_nexthop->rt_flags & RTF_LLINFO) &&
  1565. (rt_nexthop->rt_gateway->sa_family == AF_LINK) &&
  1566. (sdl = (struct sockaddr_dl *)rt_nexthop->rt_gateway) &&
  1567. sdl->sdl_alen) {
  1568. nd_opt = (struct nd_opt_hdr *)p;
  1569. nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
  1570. nd_opt->nd_opt_len = len >> 3;
  1571. lladdr = (char *)(nd_opt + 1);
  1572. bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
  1573. p += len;
  1574. }
  1575. }
  1576. nolladdropt:;
  1577. m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
  1578. /* just to be safe */
  1579. if (p - (u_char *)ip6 > maxlen)
  1580. goto noredhdropt;
  1581. {
  1582. /* redirected header option */
  1583. int len;
  1584. struct nd_opt_rd_hdr *nd_opt_rh;
  1585. /*
  1586. * compute the maximum size for icmp6 redirect header option.
  1587. * XXX room for auth header?
  1588. */
  1589. len = maxlen - (p - (u_char *)ip6);
  1590. len &= ~7;
  1591. /*
  1592. * Redirected header option spec (RFC2461 4.6.3) talks nothing
  1593. * about padding/truncate rule for the original IP packet.
  1594. * From the discussion on IPv6imp in Feb 1999,
  1595. * the consensus was:
  1596. * - "attach as much as possible" is the goal
  1597. * - pad if not aligned (original size can be guessed by
  1598. * original ip6 header)
  1599. * Following code adds the padding if it is simple enough,
  1600. * and truncates if not.
  1601. */
  1602. if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
  1603. /* not enough room, truncate */
  1604. m_adj(m0, (len - sizeof(*nd_opt_rh)) -
  1605. m0->m_pkthdr.len);
  1606. } else {
  1607. /*
  1608. * enough room, truncate if not aligned.
  1609. * we don't pad here for simplicity.
  1610. */
  1611. size_t extra;
  1612. extra = m0->m_pkthdr.len % 8;
  1613. if (extra) {
  1614. /* truncate */
  1615. m_adj(m0, -extra);
  1616. }
  1617. len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
  1618. }
  1619. nd_opt_rh = (struct nd_opt_rd_hdr *)p;
  1620. bzero(nd_opt_rh, sizeof(*nd_opt_rh));
  1621. nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
  1622. nd_opt_rh->nd_opt_rh_len = len >> 3;
  1623. p += sizeof(*nd_opt_rh);
  1624. m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
  1625. /* connect m0 to m */
  1626. m->m_pkthdr.len += m0->m_pkthdr.len;
  1627. m_cat(m, m0);
  1628. m0 = NULL;
  1629. }
  1630. noredhdropt:
  1631. m_freem(m0);
  1632. m0 = NULL;
  1633. sip6 = mtod(m, struct ip6_hdr *);
  1634. if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
  1635. sip6->ip6_src.s6_addr16[1] = 0;
  1636. if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
  1637. sip6->ip6_dst.s6_addr16[1] = 0;
  1638. #if 0
  1639. if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
  1640. ip6->ip6_src.s6_addr16[1] = 0;
  1641. if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
  1642. ip6->ip6_dst.s6_addr16[1] = 0;
  1643. #endif
  1644. if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
  1645. nd_rd->nd_rd_target.s6_addr16[1] = 0;
  1646. if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
  1647. nd_rd->nd_rd_dst.s6_addr16[1] = 0;
  1648. ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
  1649. nd_rd->nd_rd_cksum = 0;
  1650. m->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
  1651. /* send the packet to outside... */
  1652. if (ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL) != 0)
  1653. icmp6_ifstat_inc(ifp, ifs6_out_error);
  1654. icmp6_ifstat_inc(ifp, ifs6_out_msg);
  1655. icmp6_ifstat_inc(ifp, ifs6_out_redirect);
  1656. icmp6stat.icp6s_outhist[ND_REDIRECT]++;
  1657. return;
  1658. fail:
  1659. m_freem(m);
  1660. m_freem(m0);
  1661. }
  1662. /*
  1663. * ICMPv6 socket option processing.
  1664. */
  1665. int
  1666. icmp6_ctloutput(int op, struct socket *so, int level, int optname,
  1667. struct mbuf **mp)
  1668. {
  1669. int error = 0;
  1670. struct inpcb *in6p = sotoinpcb(so);
  1671. struct mbuf *m = *mp;
  1672. if (level != IPPROTO_ICMPV6) {
  1673. if (op == PRCO_SETOPT)
  1674. (void)m_free(m);
  1675. return EINVAL;
  1676. }
  1677. switch (op) {
  1678. case PRCO_SETOPT:
  1679. switch (optname) {
  1680. case ICMP6_FILTER:
  1681. {
  1682. struct icmp6_filter *p;
  1683. if (m == NULL || m->m_len != sizeof(*p)) {
  1684. error = EMSGSIZE;
  1685. break;
  1686. }
  1687. p = mtod(m, struct icmp6_filter *);
  1688. if (!p || !in6p->inp_icmp6filt) {
  1689. error = EINVAL;
  1690. break;
  1691. }
  1692. bcopy(p, in6p->inp_icmp6filt,
  1693. sizeof(struct icmp6_filter));
  1694. error = 0;
  1695. break;
  1696. }
  1697. default:
  1698. error = ENOPROTOOPT;
  1699. break;
  1700. }
  1701. m_freem(m);
  1702. break;
  1703. case PRCO_GETOPT:
  1704. switch (optname) {
  1705. case ICMP6_FILTER:
  1706. {
  1707. struct icmp6_filter *p;
  1708. if (!in6p->inp_icmp6filt) {
  1709. error = EINVAL;
  1710. break;
  1711. }
  1712. *mp = m = m_get(M_WAIT, MT_SOOPTS);
  1713. m->m_len = sizeof(struct icmp6_filter);
  1714. p = mtod(m, struct icmp6_filter *);
  1715. bcopy(in6p->inp_icmp6filt, p,
  1716. sizeof(struct icmp6_filter));
  1717. error = 0;
  1718. break;
  1719. }
  1720. default:
  1721. error = ENOPROTOOPT;
  1722. break;
  1723. }
  1724. break;
  1725. }
  1726. return (error);
  1727. }
  1728. /*
  1729. * Perform rate limit check.
  1730. * Returns 0 if it is okay to send the icmp6 packet.
  1731. * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
  1732. * limitation.
  1733. *
  1734. * XXX per-destination/type check necessary?
  1735. *
  1736. * dst - not used at this moment
  1737. * type - not used at this moment
  1738. * code - not used at this moment
  1739. */
  1740. int
  1741. icmp6_ratelimit(const struct in6_addr *dst, const int type, const int code)
  1742. {
  1743. /* PPS limit */
  1744. if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
  1745. icmp6errppslim))
  1746. return 1; /* The packet is subject to rate limit */
  1747. return 0; /* okay to send */
  1748. }
  1749. struct rtentry *
  1750. icmp6_mtudisc_clone(struct sockaddr *dst, u_int rdomain)
  1751. {
  1752. struct rtentry *rt;
  1753. int error;
  1754. rt = rtalloc(dst, RT_REPORT|RT_RESOLVE, rdomain);
  1755. if (rt == NULL)
  1756. return NULL;
  1757. /* If we didn't get a host route, allocate one */
  1758. if ((rt->rt_flags & RTF_HOST) == 0) {
  1759. struct rt_addrinfo info;
  1760. struct rtentry *nrt;
  1761. int s;
  1762. bzero(&info, sizeof(info));
  1763. info.rti_flags = RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC;
  1764. info.rti_info[RTAX_DST] = dst;
  1765. info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
  1766. s = splsoftnet();
  1767. error = rtrequest1(RTM_ADD, &info, rt->rt_priority, &nrt,
  1768. rdomain);
  1769. splx(s);
  1770. if (error) {
  1771. rtfree(rt);
  1772. return NULL;
  1773. }
  1774. nrt->rt_rmx = rt->rt_rmx;
  1775. rtfree(rt);
  1776. rt = nrt;
  1777. }
  1778. error = rt_timer_add(rt, icmp6_mtudisc_timeout,
  1779. icmp6_mtudisc_timeout_q, rdomain);
  1780. if (error) {
  1781. rtfree(rt);
  1782. return NULL;
  1783. }
  1784. return rt; /* caller need to call rtfree() */
  1785. }
  1786. void
  1787. icmp6_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
  1788. {
  1789. if (rt == NULL)
  1790. panic("icmp6_mtudisc_timeout: bad route to timeout");
  1791. if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
  1792. (RTF_DYNAMIC | RTF_HOST)) {
  1793. int s;
  1794. s = splsoftnet();
  1795. rtdeletemsg(rt, r->rtt_tableid);
  1796. splx(s);
  1797. } else {
  1798. if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
  1799. rt->rt_rmx.rmx_mtu = 0;
  1800. }
  1801. }
  1802. void
  1803. icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r)
  1804. {
  1805. if (rt == NULL)
  1806. panic("icmp6_redirect_timeout: bad route to timeout");
  1807. if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
  1808. (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
  1809. int s;
  1810. s = splsoftnet();
  1811. rtdeletemsg(rt, r->rtt_tableid);
  1812. splx(s);
  1813. }
  1814. }
  1815. int *icmpv6ctl_vars[ICMPV6CTL_MAXID] = ICMPV6CTL_VARS;
  1816. int
  1817. icmp6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
  1818. void *newp, size_t newlen)
  1819. {
  1820. /* All sysctl names at this level are terminal. */
  1821. if (namelen != 1)
  1822. return ENOTDIR;
  1823. switch (name[0]) {
  1824. case ICMPV6CTL_STATS:
  1825. return sysctl_rdstruct(oldp, oldlenp, newp,
  1826. &icmp6stat, sizeof(icmp6stat));
  1827. case ICMPV6CTL_ND6_DRLIST:
  1828. case ICMPV6CTL_ND6_PRLIST:
  1829. return nd6_sysctl(name[0], oldp, oldlenp, newp, newlen);
  1830. default:
  1831. if (name[0] < ICMPV6CTL_MAXID)
  1832. return (sysctl_int_arr(icmpv6ctl_vars, name, namelen,
  1833. oldp, oldlenp, newp, newlen));
  1834. return ENOPROTOOPT;
  1835. }
  1836. /* NOTREACHED */
  1837. }