ip6_input.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /* $OpenBSD: ip6_input.c,v 1.144 2015/07/16 21:14:21 mpi Exp $ */
  2. /* $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 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. /*
  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_input.c 8.2 (Berkeley) 1/4/94
  60. */
  61. #include "pf.h"
  62. #include "carp.h"
  63. #include <sys/param.h>
  64. #include <sys/systm.h>
  65. #include <sys/mbuf.h>
  66. #include <sys/domain.h>
  67. #include <sys/sysctl.h>
  68. #include <sys/protosw.h>
  69. #include <sys/socket.h>
  70. #include <sys/socketvar.h>
  71. #include <sys/errno.h>
  72. #include <sys/time.h>
  73. #include <sys/timeout.h>
  74. #include <sys/kernel.h>
  75. #include <sys/syslog.h>
  76. #include <net/if.h>
  77. #include <net/if_var.h>
  78. #include <net/if_types.h>
  79. #include <net/route.h>
  80. #include <net/netisr.h>
  81. #include <netinet/in.h>
  82. #include <netinet/ip.h>
  83. #include <netinet/in_pcb.h>
  84. #include <netinet6/in6_var.h>
  85. #include <netinet/ip6.h>
  86. #include <netinet6/ip6_var.h>
  87. #include <netinet/icmp6.h>
  88. #include <netinet6/nd6.h>
  89. #include <netinet6/ip6protosw.h>
  90. #include "gif.h"
  91. #include "bpfilter.h"
  92. #ifdef MROUTING
  93. #include <netinet6/ip6_mroute.h>
  94. #endif
  95. #if NPF > 0
  96. #include <net/pfvar.h>
  97. #endif
  98. #if NCARP > 0
  99. #include <netinet/ip_carp.h>
  100. #endif
  101. struct in6_ifaddrhead in6_ifaddr;
  102. struct niqueue ip6intrq = NIQUEUE_INITIALIZER(IFQ_MAXLEN, NETISR_IPV6);
  103. struct ip6stat ip6stat;
  104. void ip6_init2(void *);
  105. int ip6_check_rh0hdr(struct mbuf *, int *);
  106. int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
  107. struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
  108. /*
  109. * IP6 initialization: fill in IP6 protocol switch table.
  110. * All protocols not implemented in kernel go to raw IP6 protocol handler.
  111. */
  112. void
  113. ip6_init(void)
  114. {
  115. struct ip6protosw *pr;
  116. int i;
  117. pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
  118. if (pr == NULL)
  119. panic("ip6_init");
  120. for (i = 0; i < IPPROTO_MAX; i++)
  121. ip6_protox[i] = pr - inet6sw;
  122. for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
  123. pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
  124. if (pr->pr_domain->dom_family == PF_INET6 &&
  125. pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW &&
  126. pr->pr_protocol < IPPROTO_MAX)
  127. ip6_protox[pr->pr_protocol] = pr - inet6sw;
  128. TAILQ_INIT(&in6_ifaddr);
  129. ip6_randomid_init();
  130. nd6_init();
  131. frag6_init();
  132. ip6_init2((void *)0);
  133. }
  134. void
  135. ip6_init2(void *dummy)
  136. {
  137. /* nd6_timer_init */
  138. bzero(&nd6_timer_ch, sizeof(nd6_timer_ch));
  139. timeout_set(&nd6_timer_ch, nd6_timer, NULL);
  140. timeout_add_sec(&nd6_timer_ch, 1);
  141. }
  142. /*
  143. * IP6 input interrupt handling. Just pass the packet to ip6_input.
  144. */
  145. void
  146. ip6intr(void)
  147. {
  148. struct mbuf *m;
  149. while ((m = niq_dequeue(&ip6intrq)) != NULL)
  150. ip6_input(m);
  151. }
  152. extern struct route_in6 ip6_forward_rt;
  153. void
  154. ip6_input(struct mbuf *m)
  155. {
  156. struct ifnet *ifp;
  157. struct ip6_hdr *ip6;
  158. int off, nest;
  159. u_int16_t src_scope, dst_scope;
  160. u_int32_t plen, rtalert = ~0;
  161. int nxt, ours = 0;
  162. struct ifnet *deliverifp = NULL;
  163. #if NPF > 0
  164. struct in6_addr odst;
  165. #endif
  166. int srcrt = 0, isanycast = 0;
  167. u_int rtableid = 0;
  168. ifp = if_get(m->m_pkthdr.ph_ifidx);
  169. if (ifp == NULL)
  170. goto bad;
  171. if (m->m_flags & M_EXT) {
  172. if (m->m_next)
  173. ip6stat.ip6s_mext2m++;
  174. else
  175. ip6stat.ip6s_mext1++;
  176. } else {
  177. if (m->m_next) {
  178. if (m->m_flags & M_LOOP) {
  179. ip6stat.ip6s_m2m[lo0ifp->if_index]++; /*XXX*/
  180. } else if (ifp->if_index < nitems(ip6stat.ip6s_m2m))
  181. ip6stat.ip6s_m2m[ifp->if_index]++;
  182. else
  183. ip6stat.ip6s_m2m[0]++;
  184. } else
  185. ip6stat.ip6s_m1++;
  186. }
  187. in6_ifstat_inc(ifp, ifs6_in_receive);
  188. ip6stat.ip6s_total++;
  189. if (m->m_len < sizeof(struct ip6_hdr)) {
  190. if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
  191. ip6stat.ip6s_toosmall++;
  192. in6_ifstat_inc(ifp, ifs6_in_hdrerr);
  193. return;
  194. }
  195. }
  196. ip6 = mtod(m, struct ip6_hdr *);
  197. if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
  198. ip6stat.ip6s_badvers++;
  199. in6_ifstat_inc(ifp, ifs6_in_hdrerr);
  200. goto bad;
  201. }
  202. #if NCARP > 0
  203. if (ifp->if_type == IFT_CARP && ip6->ip6_nxt != IPPROTO_ICMPV6 &&
  204. carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
  205. ip6->ip6_dst.s6_addr32))
  206. goto bad;
  207. #endif
  208. ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
  209. /*
  210. * Check against address spoofing/corruption.
  211. */
  212. if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
  213. IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
  214. /*
  215. * XXX: "badscope" is not very suitable for a multicast source.
  216. */
  217. ip6stat.ip6s_badscope++;
  218. in6_ifstat_inc(ifp, ifs6_in_addrerr);
  219. goto bad;
  220. }
  221. if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
  222. IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
  223. (ifp->if_flags & IFF_LOOPBACK) == 0) {
  224. ip6stat.ip6s_badscope++;
  225. in6_ifstat_inc(ifp, ifs6_in_addrerr);
  226. goto bad;
  227. }
  228. /* Drop packets if interface ID portion is already filled. */
  229. if (((IN6_IS_SCOPE_EMBED(&ip6->ip6_src) && ip6->ip6_src.s6_addr16[1]) ||
  230. (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) && ip6->ip6_dst.s6_addr16[1])) &&
  231. (ifp->if_flags & IFF_LOOPBACK) == 0) {
  232. ip6stat.ip6s_badscope++;
  233. goto bad;
  234. }
  235. if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
  236. !(m->m_flags & M_LOOP)) {
  237. /*
  238. * In this case, the packet should come from the loopback
  239. * interface. However, we cannot just check the if_flags,
  240. * because ip6_mloopback() passes the "actual" interface
  241. * as the outgoing/incoming interface.
  242. */
  243. ip6stat.ip6s_badscope++;
  244. in6_ifstat_inc(ifp, ifs6_in_addrerr);
  245. goto bad;
  246. }
  247. /*
  248. * The following check is not documented in specs. A malicious
  249. * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
  250. * and bypass security checks (act as if it was from 127.0.0.1 by using
  251. * IPv6 src ::ffff:127.0.0.1). Be cautious.
  252. *
  253. * This check chokes if we are in an SIIT cloud. As none of BSDs
  254. * support IPv4-less kernel compilation, we cannot support SIIT
  255. * environment at all. So, it makes more sense for us to reject any
  256. * malicious packets for non-SIIT environment, than try to do a
  257. * partial support for SIIT environment.
  258. */
  259. if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
  260. IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
  261. ip6stat.ip6s_badscope++;
  262. in6_ifstat_inc(ifp, ifs6_in_addrerr);
  263. goto bad;
  264. }
  265. #if 0
  266. /*
  267. * Reject packets with IPv4 compatible addresses (auto tunnel).
  268. *
  269. * The code forbids auto tunnel relay case in RFC1933 (the check is
  270. * stronger than RFC1933). We may want to re-enable it if mech-xx
  271. * is revised to forbid relaying case.
  272. */
  273. if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
  274. IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
  275. ip6stat.ip6s_badscope++;
  276. in6_ifstat_inc(ifp, ifs6_in_addrerr);
  277. goto bad;
  278. }
  279. #endif
  280. /*
  281. * If the packet has been received on a loopback interface it
  282. * can be destinated to any local address, not necessarily to
  283. * an address configured on `ifp'.
  284. */
  285. if (ifp->if_flags & IFF_LOOPBACK) {
  286. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
  287. src_scope = ip6->ip6_src.s6_addr16[1];
  288. ip6->ip6_src.s6_addr16[1] = 0;
  289. }
  290. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
  291. dst_scope = ip6->ip6_dst.s6_addr16[1];
  292. ip6->ip6_dst.s6_addr16[1] = 0;
  293. }
  294. }
  295. #if NPF > 0
  296. /*
  297. * Packet filter
  298. */
  299. odst = ip6->ip6_dst;
  300. if (pf_test(AF_INET6, PF_IN, ifp, &m) != PF_PASS)
  301. goto bad;
  302. if (m == NULL)
  303. return;
  304. ip6 = mtod(m, struct ip6_hdr *);
  305. srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
  306. #endif
  307. /*
  308. * Without embedded scope ID we cannot find link-local
  309. * addresses in the routing table.
  310. */
  311. if (ifp->if_flags & IFF_LOOPBACK) {
  312. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
  313. ip6->ip6_src.s6_addr16[1] = src_scope;
  314. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
  315. ip6->ip6_dst.s6_addr16[1] = dst_scope;
  316. } else {
  317. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
  318. ip6->ip6_src.s6_addr16[1] = htons(ifp->if_index);
  319. if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
  320. ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
  321. }
  322. /*
  323. * Be more secure than RFC5095 and scan for type 0 routing headers.
  324. * If pf has already scanned the header chain, do not do it twice.
  325. */
  326. if (!(m->m_pkthdr.pf.flags & PF_TAG_PROCESSED) &&
  327. ip6_check_rh0hdr(m, &off)) {
  328. ip6stat.ip6s_badoptions++;
  329. in6_ifstat_inc(ifp, ifs6_in_discard);
  330. in6_ifstat_inc(ifp, ifs6_in_hdrerr);
  331. icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, off);
  332. /* m is already freed */
  333. return;
  334. }
  335. if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
  336. IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
  337. ours = 1;
  338. deliverifp = ifp;
  339. goto hbhcheck;
  340. }
  341. if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
  342. ours = 1;
  343. deliverifp = ifp;
  344. goto hbhcheck;
  345. }
  346. /*
  347. * Multicast check
  348. */
  349. if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  350. struct in6_multi *in6m = NULL;
  351. /*
  352. * Make sure M_MCAST is set. It should theoretically
  353. * already be there, but let's play safe because upper
  354. * layers check for this flag.
  355. */
  356. m->m_flags |= M_MCAST;
  357. in6_ifstat_inc(ifp, ifs6_in_mcast);
  358. /*
  359. * See if we belong to the destination multicast group on the
  360. * arrival interface.
  361. */
  362. IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
  363. if (in6m)
  364. ours = 1;
  365. #ifdef MROUTING
  366. else if (!ip6_mforwarding || !ip6_mrouter)
  367. #else
  368. else
  369. #endif
  370. {
  371. ip6stat.ip6s_notmember++;
  372. if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
  373. ip6stat.ip6s_cantforward++;
  374. in6_ifstat_inc(ifp, ifs6_in_discard);
  375. goto bad;
  376. }
  377. deliverifp = ifp;
  378. goto hbhcheck;
  379. }
  380. #if NPF > 0
  381. rtableid = m->m_pkthdr.ph_rtableid;
  382. #endif
  383. /*
  384. * Unicast check
  385. */
  386. if (ip6_forward_rt.ro_rt != NULL &&
  387. (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
  388. IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
  389. &ip6_forward_rt.ro_dst.sin6_addr) &&
  390. rtableid == ip6_forward_rt.ro_tableid)
  391. ip6stat.ip6s_forward_cachehit++;
  392. else {
  393. if (ip6_forward_rt.ro_rt) {
  394. /* route is down or destination is different */
  395. ip6stat.ip6s_forward_cachemiss++;
  396. rtfree(ip6_forward_rt.ro_rt);
  397. ip6_forward_rt.ro_rt = NULL;
  398. }
  399. bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
  400. ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
  401. ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
  402. ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
  403. ip6_forward_rt.ro_tableid = rtableid;
  404. ip6_forward_rt.ro_rt = rtalloc_mpath(
  405. sin6tosa(&ip6_forward_rt.ro_dst),
  406. &ip6->ip6_src.s6_addr32[0],
  407. ip6_forward_rt.ro_tableid);
  408. }
  409. /*
  410. * Accept the packet if the route to the destination is marked
  411. * as local.
  412. */
  413. if (ip6_forward_rt.ro_rt &&
  414. ISSET(ip6_forward_rt.ro_rt->rt_flags, RTF_LOCAL)) {
  415. struct in6_ifaddr *ia6 =
  416. ifatoia6(ip6_forward_rt.ro_rt->rt_ifa);
  417. if (ia6->ia6_flags & IN6_IFF_ANYCAST)
  418. isanycast = 1;
  419. /*
  420. * packets to a tentative, duplicated, or somehow invalid
  421. * address must not be accepted.
  422. */
  423. if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
  424. /* this address is ready */
  425. ours = 1;
  426. deliverifp = ia6->ia_ifp; /* correct? */
  427. goto hbhcheck;
  428. } else {
  429. char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
  430. inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src));
  431. inet_ntop(AF_INET6, &ip6->ip6_dst, dst, sizeof(dst));
  432. /* address is not ready, so discard the packet. */
  433. nd6log((LOG_INFO,
  434. "ip6_input: packet to an unready address %s->%s\n",
  435. src, dst));
  436. goto bad;
  437. }
  438. }
  439. #if NCARP > 0
  440. if (ifp->if_type == IFT_CARP && ip6->ip6_nxt == IPPROTO_ICMPV6 &&
  441. carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
  442. ip6->ip6_dst.s6_addr32))
  443. goto bad;
  444. #endif
  445. /*
  446. * Now there is no reason to process the packet if it's not our own
  447. * and we're not a router.
  448. */
  449. if (!ip6_forwarding) {
  450. ip6stat.ip6s_cantforward++;
  451. in6_ifstat_inc(ifp, ifs6_in_discard);
  452. goto bad;
  453. }
  454. hbhcheck:
  455. /*
  456. * Process Hop-by-Hop options header if it's contained.
  457. * m may be modified in ip6_hopopts_input().
  458. * If a JumboPayload option is included, plen will also be modified.
  459. */
  460. plen = (u_int32_t)ntohs(ip6->ip6_plen);
  461. off = sizeof(struct ip6_hdr);
  462. if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
  463. struct ip6_hbh *hbh;
  464. if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
  465. #if 0 /*touches NULL pointer*/
  466. in6_ifstat_inc(ifp, ifs6_in_discard);
  467. #endif
  468. return; /* m have already been freed */
  469. }
  470. /* adjust pointer */
  471. ip6 = mtod(m, struct ip6_hdr *);
  472. /*
  473. * if the payload length field is 0 and the next header field
  474. * indicates Hop-by-Hop Options header, then a Jumbo Payload
  475. * option MUST be included.
  476. */
  477. if (ip6->ip6_plen == 0 && plen == 0) {
  478. /*
  479. * Note that if a valid jumbo payload option is
  480. * contained, ip6_hoptops_input() must set a valid
  481. * (non-zero) payload length to the variable plen.
  482. */
  483. ip6stat.ip6s_badoptions++;
  484. in6_ifstat_inc(ifp, ifs6_in_discard);
  485. in6_ifstat_inc(ifp, ifs6_in_hdrerr);
  486. icmp6_error(m, ICMP6_PARAM_PROB,
  487. ICMP6_PARAMPROB_HEADER,
  488. (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
  489. return;
  490. }
  491. IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
  492. sizeof(struct ip6_hbh));
  493. if (hbh == NULL) {
  494. ip6stat.ip6s_tooshort++;
  495. return;
  496. }
  497. nxt = hbh->ip6h_nxt;
  498. /*
  499. * accept the packet if a router alert option is included
  500. * and we act as an IPv6 router.
  501. */
  502. if (rtalert != ~0 && ip6_forwarding)
  503. ours = 1;
  504. } else
  505. nxt = ip6->ip6_nxt;
  506. /*
  507. * Check that the amount of data in the buffers
  508. * is as at least much as the IPv6 header would have us expect.
  509. * Trim mbufs if longer than we expect.
  510. * Drop packet if shorter than we expect.
  511. */
  512. if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
  513. ip6stat.ip6s_tooshort++;
  514. in6_ifstat_inc(ifp, ifs6_in_truncated);
  515. goto bad;
  516. }
  517. if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
  518. if (m->m_len == m->m_pkthdr.len) {
  519. m->m_len = sizeof(struct ip6_hdr) + plen;
  520. m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
  521. } else
  522. m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
  523. }
  524. /*
  525. * Forward if desirable.
  526. */
  527. if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  528. /*
  529. * If we are acting as a multicast router, all
  530. * incoming multicast packets are passed to the
  531. * kernel-level multicast forwarding function.
  532. * The packet is returned (relatively) intact; if
  533. * ip6_mforward() returns a non-zero value, the packet
  534. * must be discarded, else it may be accepted below.
  535. */
  536. #ifdef MROUTING
  537. if (ip6_mforwarding && ip6_mrouter &&
  538. ip6_mforward(ip6, ifp, m)) {
  539. ip6stat.ip6s_cantforward++;
  540. m_freem(m);
  541. return;
  542. }
  543. #endif
  544. if (!ours) {
  545. m_freem(m);
  546. return;
  547. }
  548. } else if (!ours) {
  549. ip6_forward(m, srcrt);
  550. return;
  551. }
  552. /* pf might have changed things */
  553. in6_proto_cksum_out(m, NULL);
  554. ip6 = mtod(m, struct ip6_hdr *);
  555. /*
  556. * Malicious party may be able to use IPv4 mapped addr to confuse
  557. * tcp/udp stack and bypass security checks (act as if it was from
  558. * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
  559. *
  560. * For SIIT end node behavior, you may want to disable the check.
  561. * However, you will become vulnerable to attacks using IPv4 mapped
  562. * source.
  563. */
  564. if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
  565. IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
  566. ip6stat.ip6s_badscope++;
  567. in6_ifstat_inc(ifp, ifs6_in_addrerr);
  568. goto bad;
  569. }
  570. /*
  571. * Tell launch routine the next header
  572. */
  573. ip6stat.ip6s_delivered++;
  574. in6_ifstat_inc(deliverifp, ifs6_in_deliver);
  575. nest = 0;
  576. while (nxt != IPPROTO_DONE) {
  577. if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
  578. ip6stat.ip6s_toomanyhdr++;
  579. goto bad;
  580. }
  581. /*
  582. * protection against faulty packet - there should be
  583. * more sanity checks in header chain processing.
  584. */
  585. if (m->m_pkthdr.len < off) {
  586. ip6stat.ip6s_tooshort++;
  587. in6_ifstat_inc(ifp, ifs6_in_truncated);
  588. goto bad;
  589. }
  590. /* draft-itojun-ipv6-tcp-to-anycast */
  591. if (isanycast && nxt == IPPROTO_TCP) {
  592. if (m->m_len >= sizeof(struct ip6_hdr)) {
  593. ip6 = mtod(m, struct ip6_hdr *);
  594. icmp6_error(m, ICMP6_DST_UNREACH,
  595. ICMP6_DST_UNREACH_ADDR,
  596. (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
  597. break;
  598. } else
  599. goto bad;
  600. }
  601. nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
  602. }
  603. return;
  604. bad:
  605. m_freem(m);
  606. }
  607. /* scan packet for RH0 routing header. Mostly stolen from pf.c:pf_test() */
  608. int
  609. ip6_check_rh0hdr(struct mbuf *m, int *offp)
  610. {
  611. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  612. struct ip6_rthdr rthdr;
  613. struct ip6_ext opt6;
  614. u_int8_t proto = ip6->ip6_nxt;
  615. int done = 0, lim, off, rh_cnt = 0;
  616. off = ((caddr_t)ip6 - m->m_data) + sizeof(struct ip6_hdr);
  617. lim = min(m->m_pkthdr.len, ntohs(ip6->ip6_plen) + sizeof(*ip6));
  618. do {
  619. switch (proto) {
  620. case IPPROTO_ROUTING:
  621. *offp = off;
  622. if (rh_cnt++) {
  623. /* more then one rh header present */
  624. return (1);
  625. }
  626. if (off + sizeof(rthdr) > lim) {
  627. /* packet to short to make sense */
  628. return (1);
  629. }
  630. m_copydata(m, off, sizeof(rthdr), (caddr_t)&rthdr);
  631. if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) {
  632. *offp += offsetof(struct ip6_rthdr, ip6r_type);
  633. return (1);
  634. }
  635. off += (rthdr.ip6r_len + 1) * 8;
  636. proto = rthdr.ip6r_nxt;
  637. break;
  638. case IPPROTO_AH:
  639. case IPPROTO_HOPOPTS:
  640. case IPPROTO_DSTOPTS:
  641. /* get next header and header length */
  642. if (off + sizeof(opt6) > lim) {
  643. /*
  644. * Packet to short to make sense, we could
  645. * reject the packet but as a router we
  646. * should not do that so forward it.
  647. */
  648. return (0);
  649. }
  650. m_copydata(m, off, sizeof(opt6), (caddr_t)&opt6);
  651. if (proto == IPPROTO_AH)
  652. off += (opt6.ip6e_len + 2) * 4;
  653. else
  654. off += (opt6.ip6e_len + 1) * 8;
  655. proto = opt6.ip6e_nxt;
  656. break;
  657. case IPPROTO_FRAGMENT:
  658. default:
  659. /* end of header stack */
  660. done = 1;
  661. break;
  662. }
  663. } while (!done);
  664. return (0);
  665. }
  666. /*
  667. * Hop-by-Hop options header processing. If a valid jumbo payload option is
  668. * included, the real payload length will be stored in plenp.
  669. *
  670. * rtalertp - XXX: should be stored in a more smart way
  671. */
  672. int
  673. ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp, struct mbuf **mp,
  674. int *offp)
  675. {
  676. struct mbuf *m = *mp;
  677. int off = *offp, hbhlen;
  678. struct ip6_hbh *hbh;
  679. /* validation of the length of the header */
  680. IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
  681. sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
  682. if (hbh == NULL) {
  683. ip6stat.ip6s_tooshort++;
  684. return -1;
  685. }
  686. hbhlen = (hbh->ip6h_len + 1) << 3;
  687. IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
  688. hbhlen);
  689. if (hbh == NULL) {
  690. ip6stat.ip6s_tooshort++;
  691. return -1;
  692. }
  693. off += hbhlen;
  694. hbhlen -= sizeof(struct ip6_hbh);
  695. if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
  696. hbhlen, rtalertp, plenp) < 0)
  697. return (-1);
  698. *offp = off;
  699. *mp = m;
  700. return (0);
  701. }
  702. /*
  703. * Search header for all Hop-by-hop options and process each option.
  704. * This function is separate from ip6_hopopts_input() in order to
  705. * handle a case where the sending node itself process its hop-by-hop
  706. * options header. In such a case, the function is called from ip6_output().
  707. *
  708. * The function assumes that hbh header is located right after the IPv6 header
  709. * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
  710. * opthead + hbhlen is located in continuous memory region.
  711. */
  712. int
  713. ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
  714. u_int32_t *rtalertp, u_int32_t *plenp)
  715. {
  716. struct ip6_hdr *ip6;
  717. int optlen = 0;
  718. u_int8_t *opt = opthead;
  719. u_int16_t rtalert_val;
  720. u_int32_t jumboplen;
  721. const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
  722. for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
  723. switch (*opt) {
  724. case IP6OPT_PAD1:
  725. optlen = 1;
  726. break;
  727. case IP6OPT_PADN:
  728. if (hbhlen < IP6OPT_MINLEN) {
  729. ip6stat.ip6s_toosmall++;
  730. goto bad;
  731. }
  732. optlen = *(opt + 1) + 2;
  733. break;
  734. case IP6OPT_ROUTER_ALERT:
  735. /* XXX may need check for alignment */
  736. if (hbhlen < IP6OPT_RTALERT_LEN) {
  737. ip6stat.ip6s_toosmall++;
  738. goto bad;
  739. }
  740. if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
  741. /* XXX stat */
  742. icmp6_error(m, ICMP6_PARAM_PROB,
  743. ICMP6_PARAMPROB_HEADER,
  744. erroff + opt + 1 - opthead);
  745. return (-1);
  746. }
  747. optlen = IP6OPT_RTALERT_LEN;
  748. bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
  749. *rtalertp = ntohs(rtalert_val);
  750. break;
  751. case IP6OPT_JUMBO:
  752. /* XXX may need check for alignment */
  753. if (hbhlen < IP6OPT_JUMBO_LEN) {
  754. ip6stat.ip6s_toosmall++;
  755. goto bad;
  756. }
  757. if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
  758. /* XXX stat */
  759. icmp6_error(m, ICMP6_PARAM_PROB,
  760. ICMP6_PARAMPROB_HEADER,
  761. erroff + opt + 1 - opthead);
  762. return (-1);
  763. }
  764. optlen = IP6OPT_JUMBO_LEN;
  765. /*
  766. * IPv6 packets that have non 0 payload length
  767. * must not contain a jumbo payload option.
  768. */
  769. ip6 = mtod(m, struct ip6_hdr *);
  770. if (ip6->ip6_plen) {
  771. ip6stat.ip6s_badoptions++;
  772. icmp6_error(m, ICMP6_PARAM_PROB,
  773. ICMP6_PARAMPROB_HEADER,
  774. erroff + opt - opthead);
  775. return (-1);
  776. }
  777. /*
  778. * We may see jumbolen in unaligned location, so
  779. * we'd need to perform bcopy().
  780. */
  781. bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
  782. jumboplen = (u_int32_t)htonl(jumboplen);
  783. #if 1
  784. /*
  785. * if there are multiple jumbo payload options,
  786. * *plenp will be non-zero and the packet will be
  787. * rejected.
  788. * the behavior may need some debate in ipngwg -
  789. * multiple options does not make sense, however,
  790. * there's no explicit mention in specification.
  791. */
  792. if (*plenp != 0) {
  793. ip6stat.ip6s_badoptions++;
  794. icmp6_error(m, ICMP6_PARAM_PROB,
  795. ICMP6_PARAMPROB_HEADER,
  796. erroff + opt + 2 - opthead);
  797. return (-1);
  798. }
  799. #endif
  800. /*
  801. * jumbo payload length must be larger than 65535.
  802. */
  803. if (jumboplen <= IPV6_MAXPACKET) {
  804. ip6stat.ip6s_badoptions++;
  805. icmp6_error(m, ICMP6_PARAM_PROB,
  806. ICMP6_PARAMPROB_HEADER,
  807. erroff + opt + 2 - opthead);
  808. return (-1);
  809. }
  810. *plenp = jumboplen;
  811. break;
  812. default: /* unknown option */
  813. if (hbhlen < IP6OPT_MINLEN) {
  814. ip6stat.ip6s_toosmall++;
  815. goto bad;
  816. }
  817. optlen = ip6_unknown_opt(opt, m,
  818. erroff + opt - opthead);
  819. if (optlen == -1)
  820. return (-1);
  821. optlen += 2;
  822. break;
  823. }
  824. }
  825. return (0);
  826. bad:
  827. m_freem(m);
  828. return (-1);
  829. }
  830. /*
  831. * Unknown option processing.
  832. * The third argument `off' is the offset from the IPv6 header to the option,
  833. * which allows returning an ICMPv6 error even if the IPv6 header and the
  834. * option header are not continuous.
  835. */
  836. int
  837. ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
  838. {
  839. struct ip6_hdr *ip6;
  840. switch (IP6OPT_TYPE(*optp)) {
  841. case IP6OPT_TYPE_SKIP: /* ignore the option */
  842. return ((int)*(optp + 1));
  843. case IP6OPT_TYPE_DISCARD: /* silently discard */
  844. m_freem(m);
  845. return (-1);
  846. case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
  847. ip6stat.ip6s_badoptions++;
  848. icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
  849. return (-1);
  850. case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
  851. ip6stat.ip6s_badoptions++;
  852. ip6 = mtod(m, struct ip6_hdr *);
  853. if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
  854. (m->m_flags & (M_BCAST|M_MCAST)))
  855. m_freem(m);
  856. else
  857. icmp6_error(m, ICMP6_PARAM_PROB,
  858. ICMP6_PARAMPROB_OPTION, off);
  859. return (-1);
  860. }
  861. m_freem(m); /* XXX: NOTREACHED */
  862. return (-1);
  863. }
  864. /*
  865. * Create the "control" list for this pcb.
  866. *
  867. * The routine will be called from upper layer handlers like tcp6_input().
  868. * Thus the routine assumes that the caller (tcp6_input) have already
  869. * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
  870. * very first mbuf on the mbuf chain.
  871. * We may want to add some infinite loop prevention or sanity checks for safety.
  872. * (This applies only when you are using KAME mbuf chain restriction, i.e.
  873. * you are using IP6_EXTHDR_CHECK() not m_pulldown())
  874. */
  875. void
  876. ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
  877. {
  878. #define IS2292(x, y) ((in6p->inp_flags & IN6P_RFC2292) ? (x) : (y))
  879. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  880. #ifdef SO_TIMESTAMP
  881. if (in6p->inp_socket->so_options & SO_TIMESTAMP) {
  882. struct timeval tv;
  883. microtime(&tv);
  884. *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
  885. SCM_TIMESTAMP, SOL_SOCKET);
  886. if (*mp)
  887. mp = &(*mp)->m_next;
  888. }
  889. #endif
  890. /* RFC 2292 sec. 5 */
  891. if ((in6p->inp_flags & IN6P_PKTINFO) != 0) {
  892. struct in6_pktinfo pi6;
  893. bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
  894. if (IN6_IS_SCOPE_EMBED(&pi6.ipi6_addr))
  895. pi6.ipi6_addr.s6_addr16[1] = 0;
  896. pi6.ipi6_ifindex = m ? m->m_pkthdr.ph_ifidx : 0;
  897. *mp = sbcreatecontrol((caddr_t) &pi6,
  898. sizeof(struct in6_pktinfo),
  899. IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
  900. if (*mp)
  901. mp = &(*mp)->m_next;
  902. }
  903. if ((in6p->inp_flags & IN6P_HOPLIMIT) != 0) {
  904. int hlim = ip6->ip6_hlim & 0xff;
  905. *mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
  906. IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
  907. if (*mp)
  908. mp = &(*mp)->m_next;
  909. }
  910. if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
  911. u_int32_t flowinfo;
  912. int tclass;
  913. flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
  914. flowinfo >>= 20;
  915. tclass = flowinfo & 0xff;
  916. *mp = sbcreatecontrol((caddr_t)&tclass, sizeof(tclass),
  917. IPV6_TCLASS, IPPROTO_IPV6);
  918. if (*mp)
  919. mp = &(*mp)->m_next;
  920. }
  921. /*
  922. * IPV6_HOPOPTS socket option. Recall that we required super-user
  923. * privilege for the option (see ip6_ctloutput), but it might be too
  924. * strict, since there might be some hop-by-hop options which can be
  925. * returned to normal user.
  926. * See also RFC 2292 section 6 (or RFC 3542 section 8).
  927. */
  928. if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
  929. /*
  930. * Check if a hop-by-hop options header is contatined in the
  931. * received packet, and if so, store the options as ancillary
  932. * data. Note that a hop-by-hop options header must be
  933. * just after the IPv6 header, which is assured through the
  934. * IPv6 input processing.
  935. */
  936. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  937. if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
  938. struct ip6_hbh *hbh;
  939. int hbhlen = 0;
  940. struct mbuf *ext;
  941. ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
  942. ip6->ip6_nxt);
  943. if (ext == NULL) {
  944. ip6stat.ip6s_tooshort++;
  945. return;
  946. }
  947. hbh = mtod(ext, struct ip6_hbh *);
  948. hbhlen = (hbh->ip6h_len + 1) << 3;
  949. if (hbhlen != ext->m_len) {
  950. m_freem(ext);
  951. ip6stat.ip6s_tooshort++;
  952. return;
  953. }
  954. /*
  955. * XXX: We copy the whole header even if a
  956. * jumbo payload option is included, the option which
  957. * is to be removed before returning according to
  958. * RFC2292.
  959. * Note: this constraint is removed in RFC3542.
  960. */
  961. *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
  962. IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
  963. IPPROTO_IPV6);
  964. if (*mp)
  965. mp = &(*mp)->m_next;
  966. m_freem(ext);
  967. }
  968. }
  969. /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
  970. if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
  971. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  972. int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
  973. /*
  974. * Search for destination options headers or routing
  975. * header(s) through the header chain, and stores each
  976. * header as ancillary data.
  977. * Note that the order of the headers remains in
  978. * the chain of ancillary data.
  979. */
  980. while (1) { /* is explicit loop prevention necessary? */
  981. struct ip6_ext *ip6e = NULL;
  982. int elen;
  983. struct mbuf *ext = NULL;
  984. /*
  985. * if it is not an extension header, don't try to
  986. * pull it from the chain.
  987. */
  988. switch (nxt) {
  989. case IPPROTO_DSTOPTS:
  990. case IPPROTO_ROUTING:
  991. case IPPROTO_HOPOPTS:
  992. case IPPROTO_AH: /* is it possible? */
  993. break;
  994. default:
  995. goto loopend;
  996. }
  997. ext = ip6_pullexthdr(m, off, nxt);
  998. if (ext == NULL) {
  999. ip6stat.ip6s_tooshort++;
  1000. return;
  1001. }
  1002. ip6e = mtod(ext, struct ip6_ext *);
  1003. if (nxt == IPPROTO_AH)
  1004. elen = (ip6e->ip6e_len + 2) << 2;
  1005. else
  1006. elen = (ip6e->ip6e_len + 1) << 3;
  1007. if (elen != ext->m_len) {
  1008. m_freem(ext);
  1009. ip6stat.ip6s_tooshort++;
  1010. return;
  1011. }
  1012. switch (nxt) {
  1013. case IPPROTO_DSTOPTS:
  1014. if (!(in6p->inp_flags & IN6P_DSTOPTS))
  1015. break;
  1016. *mp = sbcreatecontrol((caddr_t)ip6e, elen,
  1017. IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
  1018. IPPROTO_IPV6);
  1019. if (*mp)
  1020. mp = &(*mp)->m_next;
  1021. break;
  1022. case IPPROTO_ROUTING:
  1023. if (!(in6p->inp_flags & IN6P_RTHDR))
  1024. break;
  1025. *mp = sbcreatecontrol((caddr_t)ip6e, elen,
  1026. IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
  1027. IPPROTO_IPV6);
  1028. if (*mp)
  1029. mp = &(*mp)->m_next;
  1030. break;
  1031. case IPPROTO_HOPOPTS:
  1032. case IPPROTO_AH: /* is it possible? */
  1033. break;
  1034. default:
  1035. /*
  1036. * other cases have been filtered in the above.
  1037. * none will visit this case. here we supply
  1038. * the code just in case (nxt overwritten or
  1039. * other cases).
  1040. */
  1041. m_freem(ext);
  1042. goto loopend;
  1043. }
  1044. /* proceed with the next header. */
  1045. off += elen;
  1046. nxt = ip6e->ip6e_nxt;
  1047. ip6e = NULL;
  1048. m_freem(ext);
  1049. ext = NULL;
  1050. }
  1051. loopend:
  1052. ;
  1053. }
  1054. }
  1055. /*
  1056. * pull single extension header from mbuf chain. returns single mbuf that
  1057. * contains the result, or NULL on error.
  1058. */
  1059. struct mbuf *
  1060. ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
  1061. {
  1062. struct ip6_ext ip6e;
  1063. size_t elen;
  1064. struct mbuf *n;
  1065. #ifdef DIAGNOSTIC
  1066. switch (nxt) {
  1067. case IPPROTO_DSTOPTS:
  1068. case IPPROTO_ROUTING:
  1069. case IPPROTO_HOPOPTS:
  1070. case IPPROTO_AH: /* is it possible? */
  1071. break;
  1072. default:
  1073. printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
  1074. }
  1075. #endif
  1076. m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
  1077. if (nxt == IPPROTO_AH)
  1078. elen = (ip6e.ip6e_len + 2) << 2;
  1079. else
  1080. elen = (ip6e.ip6e_len + 1) << 3;
  1081. MGET(n, M_DONTWAIT, MT_DATA);
  1082. if (n && elen >= MLEN) {
  1083. MCLGET(n, M_DONTWAIT);
  1084. if ((n->m_flags & M_EXT) == 0) {
  1085. m_free(n);
  1086. n = NULL;
  1087. }
  1088. }
  1089. if (!n)
  1090. return NULL;
  1091. n->m_len = 0;
  1092. if (elen >= M_TRAILINGSPACE(n)) {
  1093. m_free(n);
  1094. return NULL;
  1095. }
  1096. m_copydata(m, off, elen, mtod(n, caddr_t));
  1097. n->m_len = elen;
  1098. return n;
  1099. }
  1100. /*
  1101. * Get pointer to the previous header followed by the header
  1102. * currently processed.
  1103. * XXX: This function supposes that
  1104. * M includes all headers,
  1105. * the next header field and the header length field of each header
  1106. * are valid, and
  1107. * the sum of each header length equals to OFF.
  1108. * Because of these assumptions, this function must be called very
  1109. * carefully. Moreover, it will not be used in the near future when
  1110. * we develop `neater' mechanism to process extension headers.
  1111. */
  1112. u_int8_t *
  1113. ip6_get_prevhdr(struct mbuf *m, int off)
  1114. {
  1115. struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  1116. if (off == sizeof(struct ip6_hdr))
  1117. return (&ip6->ip6_nxt);
  1118. else {
  1119. int len, nxt;
  1120. struct ip6_ext *ip6e = NULL;
  1121. nxt = ip6->ip6_nxt;
  1122. len = sizeof(struct ip6_hdr);
  1123. while (len < off) {
  1124. ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
  1125. switch (nxt) {
  1126. case IPPROTO_FRAGMENT:
  1127. len += sizeof(struct ip6_frag);
  1128. break;
  1129. case IPPROTO_AH:
  1130. len += (ip6e->ip6e_len + 2) << 2;
  1131. break;
  1132. default:
  1133. len += (ip6e->ip6e_len + 1) << 3;
  1134. break;
  1135. }
  1136. nxt = ip6e->ip6e_nxt;
  1137. }
  1138. if (ip6e)
  1139. return (&ip6e->ip6e_nxt);
  1140. else
  1141. return NULL;
  1142. }
  1143. }
  1144. /*
  1145. * get next header offset. m will be retained.
  1146. */
  1147. int
  1148. ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
  1149. {
  1150. struct ip6_hdr ip6;
  1151. struct ip6_ext ip6e;
  1152. struct ip6_frag fh;
  1153. /* just in case */
  1154. if (m == NULL)
  1155. panic("ip6_nexthdr: m == NULL");
  1156. if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
  1157. return -1;
  1158. switch (proto) {
  1159. case IPPROTO_IPV6:
  1160. if (m->m_pkthdr.len < off + sizeof(ip6))
  1161. return -1;
  1162. m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
  1163. if (nxtp)
  1164. *nxtp = ip6.ip6_nxt;
  1165. off += sizeof(ip6);
  1166. return off;
  1167. case IPPROTO_FRAGMENT:
  1168. /*
  1169. * terminate parsing if it is not the first fragment,
  1170. * it does not make sense to parse through it.
  1171. */
  1172. if (m->m_pkthdr.len < off + sizeof(fh))
  1173. return -1;
  1174. m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
  1175. if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
  1176. return -1;
  1177. if (nxtp)
  1178. *nxtp = fh.ip6f_nxt;
  1179. off += sizeof(struct ip6_frag);
  1180. return off;
  1181. case IPPROTO_AH:
  1182. if (m->m_pkthdr.len < off + sizeof(ip6e))
  1183. return -1;
  1184. m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
  1185. if (nxtp)
  1186. *nxtp = ip6e.ip6e_nxt;
  1187. off += (ip6e.ip6e_len + 2) << 2;
  1188. if (m->m_pkthdr.len < off)
  1189. return -1;
  1190. return off;
  1191. case IPPROTO_HOPOPTS:
  1192. case IPPROTO_ROUTING:
  1193. case IPPROTO_DSTOPTS:
  1194. if (m->m_pkthdr.len < off + sizeof(ip6e))
  1195. return -1;
  1196. m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
  1197. if (nxtp)
  1198. *nxtp = ip6e.ip6e_nxt;
  1199. off += (ip6e.ip6e_len + 1) << 3;
  1200. if (m->m_pkthdr.len < off)
  1201. return -1;
  1202. return off;
  1203. case IPPROTO_NONE:
  1204. case IPPROTO_ESP:
  1205. case IPPROTO_IPCOMP:
  1206. /* give up */
  1207. return -1;
  1208. default:
  1209. return -1;
  1210. }
  1211. return -1;
  1212. }
  1213. /*
  1214. * get offset for the last header in the chain. m will be kept untainted.
  1215. */
  1216. int
  1217. ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
  1218. {
  1219. int newoff;
  1220. int nxt;
  1221. if (!nxtp) {
  1222. nxt = -1;
  1223. nxtp = &nxt;
  1224. }
  1225. while (1) {
  1226. newoff = ip6_nexthdr(m, off, proto, nxtp);
  1227. if (newoff < 0)
  1228. return off;
  1229. else if (newoff < off)
  1230. return -1; /* invalid */
  1231. else if (newoff == off)
  1232. return newoff;
  1233. off = newoff;
  1234. proto = *nxtp;
  1235. }
  1236. }
  1237. /*
  1238. * System control for IP6
  1239. */
  1240. u_char inet6ctlerrmap[PRC_NCMDS] = {
  1241. 0, 0, 0, 0,
  1242. 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
  1243. EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
  1244. EMSGSIZE, EHOSTUNREACH, 0, 0,
  1245. 0, 0, 0, 0,
  1246. ENOPROTOOPT
  1247. };
  1248. int *ipv6ctl_vars[IPV6CTL_MAXID] = IPV6CTL_VARS;
  1249. int
  1250. ip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
  1251. void *newp, size_t newlen)
  1252. {
  1253. #ifdef MROUTING
  1254. extern int ip6_mrtproto;
  1255. extern struct mrt6stat mrt6stat;
  1256. #endif
  1257. int error, s;
  1258. /* Almost all sysctl names at this level are terminal. */
  1259. if (namelen != 1 && name[0] != IPV6CTL_IFQUEUE)
  1260. return (ENOTDIR);
  1261. switch (name[0]) {
  1262. case IPV6CTL_V6ONLY:
  1263. return sysctl_rdint(oldp, oldlenp, newp, ip6_v6only);
  1264. case IPV6CTL_DAD_PENDING:
  1265. return sysctl_rdint(oldp, oldlenp, newp, ip6_dad_pending);
  1266. case IPV6CTL_STATS:
  1267. if (newp != NULL)
  1268. return (EPERM);
  1269. return (sysctl_struct(oldp, oldlenp, newp, newlen,
  1270. &ip6stat, sizeof(ip6stat)));
  1271. #ifdef MROUTING
  1272. case IPV6CTL_MRTSTATS:
  1273. if (newp != NULL)
  1274. return (EPERM);
  1275. return (sysctl_struct(oldp, oldlenp, newp, newlen,
  1276. &mrt6stat, sizeof(mrt6stat)));
  1277. case IPV6CTL_MRTPROTO:
  1278. return sysctl_rdint(oldp, oldlenp, newp, ip6_mrtproto);
  1279. case IPV6CTL_MRTMIF:
  1280. if (newp)
  1281. return (EPERM);
  1282. return mrt6_sysctl_mif(oldp, oldlenp);
  1283. case IPV6CTL_MRTMFC:
  1284. if (newp)
  1285. return (EPERM);
  1286. return mrt6_sysctl_mfc(oldp, oldlenp);
  1287. #else
  1288. case IPV6CTL_MRTSTATS:
  1289. case IPV6CTL_MRTPROTO:
  1290. case IPV6CTL_MRTMIF:
  1291. case IPV6CTL_MRTMFC:
  1292. return (EOPNOTSUPP);
  1293. #endif
  1294. case IPV6CTL_MTUDISCTIMEOUT:
  1295. error = sysctl_int(oldp, oldlenp, newp, newlen,
  1296. &ip6_mtudisc_timeout);
  1297. if (icmp6_mtudisc_timeout_q != NULL) {
  1298. s = splsoftnet();
  1299. rt_timer_queue_change(icmp6_mtudisc_timeout_q,
  1300. ip6_mtudisc_timeout);
  1301. splx(s);
  1302. }
  1303. return (error);
  1304. case IPV6CTL_IFQUEUE:
  1305. return (sysctl_niq(name + 1, namelen - 1,
  1306. oldp, oldlenp, newp, newlen, &ip6intrq));
  1307. default:
  1308. if (name[0] < IPV6CTL_MAXID)
  1309. return (sysctl_int_arr(ipv6ctl_vars, name, namelen,
  1310. oldp, oldlenp, newp, newlen));
  1311. return (EOPNOTSUPP);
  1312. }
  1313. /* NOTREACHED */
  1314. }