1 /* $FreeBSD$ */ 2 /* $KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 66 */ 67 68 #include "opt_ip6fw.h" 69 #include "opt_inet.h" 70 #include "opt_inet6.h" 71 #include "opt_ipsec.h" 72 #include "opt_pfil_hooks.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/malloc.h> 77 #include <sys/mbuf.h> 78 #include <sys/proc.h> 79 #include <sys/domain.h> 80 #include <sys/protosw.h> 81 #include <sys/socket.h> 82 #include <sys/socketvar.h> 83 #include <sys/errno.h> 84 #include <sys/time.h> 85 #include <sys/kernel.h> 86 #include <sys/syslog.h> 87 88 #include <net/if.h> 89 #include <net/if_types.h> 90 #include <net/if_dl.h> 91 #include <net/route.h> 92 #include <net/netisr.h> 93 #ifdef PFIL_HOOKS 94 #include <net/pfil.h> 95 #endif 96 97 #include <netinet/in.h> 98 #include <netinet/in_systm.h> 99 #ifdef INET 100 #include <netinet/ip.h> 101 #include <netinet/ip_icmp.h> 102 #endif /* INET */ 103 #include <netinet/ip6.h> 104 #include <netinet6/in6_var.h> 105 #include <netinet6/ip6_var.h> 106 #include <netinet/in_pcb.h> 107 #include <netinet/icmp6.h> 108 #include <netinet6/in6_ifattach.h> 109 #include <netinet6/nd6.h> 110 #include <netinet6/in6_prefix.h> 111 112 #ifdef IPSEC 113 #include <netinet6/ipsec.h> 114 #ifdef INET6 115 #include <netinet6/ipsec6.h> 116 #endif 117 #endif 118 119 #ifdef FAST_IPSEC 120 #include <netipsec/ipsec.h> 121 #include <netipsec/ipsec6.h> 122 #define IPSEC 123 #endif /* FAST_IPSEC */ 124 125 #include <netinet6/ip6_fw.h> 126 127 #include <netinet6/ip6protosw.h> 128 129 #include <net/net_osdep.h> 130 131 extern struct domain inet6domain; 132 133 u_char ip6_protox[IPPROTO_MAX]; 134 static struct ifqueue ip6intrq; 135 static int ip6qmaxlen = IFQ_MAXLEN; 136 struct in6_ifaddr *in6_ifaddr; 137 138 extern struct callout in6_tmpaddrtimer_ch; 139 140 int ip6_forward_srcrt; /* XXX */ 141 int ip6_sourcecheck; /* XXX */ 142 int ip6_sourcecheck_interval; /* XXX */ 143 144 int ip6_ours_check_algorithm; 145 146 147 /* firewall hooks */ 148 ip6_fw_chk_t *ip6_fw_chk_ptr; 149 ip6_fw_ctl_t *ip6_fw_ctl_ptr; 150 int ip6_fw_enable = 1; 151 152 struct ip6stat ip6stat; 153 154 static void ip6_init2 __P((void *)); 155 static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *)); 156 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 157 #ifdef PULLDOWN_TEST 158 static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int)); 159 #endif 160 161 162 /* 163 * IP6 initialization: fill in IP6 protocol switch table. 164 * All protocols not implemented in kernel go to raw IP6 protocol handler. 165 */ 166 void 167 ip6_init() 168 { 169 struct ip6protosw *pr; 170 int i; 171 struct timeval tv; 172 173 #ifdef DIAGNOSTIC 174 if (sizeof(struct protosw) != sizeof(struct ip6protosw)) 175 panic("sizeof(protosw) != sizeof(ip6protosw)"); 176 #endif 177 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 178 if (pr == 0) 179 panic("ip6_init"); 180 for (i = 0; i < IPPROTO_MAX; i++) 181 ip6_protox[i] = pr - inet6sw; 182 for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 183 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 184 if (pr->pr_domain->dom_family == PF_INET6 && 185 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 186 ip6_protox[pr->pr_protocol] = pr - inet6sw; 187 ip6intrq.ifq_maxlen = ip6qmaxlen; 188 mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF); 189 netisr_register(NETISR_IPV6, ip6_input, &ip6intrq); 190 nd6_init(); 191 frag6_init(); 192 /* 193 * in many cases, random() here does NOT return random number 194 * as initialization during bootstrap time occur in fixed order. 195 */ 196 microtime(&tv); 197 ip6_flow_seq = random() ^ tv.tv_usec; 198 microtime(&tv); 199 ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR; 200 } 201 202 static void 203 ip6_init2(dummy) 204 void *dummy; 205 { 206 207 /* 208 * to route local address of p2p link to loopback, 209 * assign loopback address first. 210 */ 211 in6_ifattach(&loif[0], NULL); 212 213 /* nd6_timer_init */ 214 callout_init(&nd6_timer_ch, 0); 215 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 216 217 /* router renumbering prefix list maintenance */ 218 callout_init(&in6_rr_timer_ch, 0); 219 callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL); 220 221 /* timer for regeneranation of temporary addresses randomize ID */ 222 callout_reset(&in6_tmpaddrtimer_ch, 223 (ip6_temp_preferred_lifetime - ip6_desync_factor - 224 ip6_temp_regen_advance) * hz, 225 in6_tmpaddrtimer, NULL); 226 } 227 228 /* cheat */ 229 /* This must be after route_init(), which is now SI_ORDER_THIRD */ 230 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); 231 232 extern struct route_in6 ip6_forward_rt; 233 234 void 235 ip6_input(m) 236 struct mbuf *m; 237 { 238 struct ip6_hdr *ip6; 239 int off = sizeof(struct ip6_hdr), nest; 240 u_int32_t plen; 241 u_int32_t rtalert = ~0; 242 int nxt, ours = 0; 243 struct ifnet *deliverifp = NULL; 244 #ifdef PFIL_HOOKS 245 struct packet_filter_hook *pfh; 246 struct mbuf *m0; 247 int rv; 248 #endif /* PFIL_HOOKS */ 249 250 #ifdef IPSEC 251 /* 252 * should the inner packet be considered authentic? 253 * see comment in ah4_input(). 254 */ 255 if (m) { 256 m->m_flags &= ~M_AUTHIPHDR; 257 m->m_flags &= ~M_AUTHIPDGM; 258 } 259 #endif 260 261 /* 262 * make sure we don't have onion peering information into m_aux. 263 */ 264 ip6_delaux(m); 265 266 /* 267 * mbuf statistics 268 */ 269 if (m->m_flags & M_EXT) { 270 if (m->m_next) 271 ip6stat.ip6s_mext2m++; 272 else 273 ip6stat.ip6s_mext1++; 274 } else { 275 #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) 276 if (m->m_next) { 277 if (m->m_flags & M_LOOP) { 278 ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */ 279 } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) 280 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 281 else 282 ip6stat.ip6s_m2m[0]++; 283 } else 284 ip6stat.ip6s_m1++; 285 #undef M2MMAX 286 } 287 288 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 289 ip6stat.ip6s_total++; 290 291 #ifndef PULLDOWN_TEST 292 /* 293 * L2 bridge code and some other code can return mbuf chain 294 * that does not conform to KAME requirement. too bad. 295 * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? 296 */ 297 if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { 298 struct mbuf *n; 299 300 MGETHDR(n, M_DONTWAIT, MT_HEADER); 301 if (n) 302 M_MOVE_PKTHDR(n, m); 303 if (n && n->m_pkthdr.len > MHLEN) { 304 MCLGET(n, M_DONTWAIT); 305 if ((n->m_flags & M_EXT) == 0) { 306 m_freem(n); 307 n = NULL; 308 } 309 } 310 if (n == NULL) { 311 m_freem(m); 312 return; /*ENOBUFS*/ 313 } 314 315 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t)); 316 n->m_len = n->m_pkthdr.len; 317 m_freem(m); 318 m = n; 319 } 320 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/); 321 #endif 322 323 if (m->m_len < sizeof(struct ip6_hdr)) { 324 struct ifnet *inifp; 325 inifp = m->m_pkthdr.rcvif; 326 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) { 327 ip6stat.ip6s_toosmall++; 328 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 329 return; 330 } 331 } 332 333 ip6 = mtod(m, struct ip6_hdr *); 334 335 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 336 ip6stat.ip6s_badvers++; 337 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 338 goto bad; 339 } 340 341 #ifdef PFIL_HOOKS 342 /* 343 * Run through list of hooks for input packets. If there are any 344 * filters which require that additional packets in the flow are 345 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 346 * Note that filters must _never_ set this flag, as another filter 347 * in the list may have previously cleared it. 348 */ 349 m0 = m; 350 pfh = pfil_hook_get(PFIL_IN, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh); 351 for (; pfh; pfh = pfh->pfil_link.tqe_next) 352 if (pfh->pfil_func) { 353 rv = pfh->pfil_func(ip6, sizeof(*ip6), 354 m->m_pkthdr.rcvif, 0, &m0); 355 if (rv) 356 return; 357 m = m0; 358 if (m == NULL) 359 return; 360 ip6 = mtod(m, struct ip6_hdr *); 361 } 362 #endif /* PFIL_HOOKS */ 363 364 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 365 366 /* 367 * Check with the firewall... 368 */ 369 if (ip6_fw_enable && ip6_fw_chk_ptr) { 370 u_short port = 0; 371 /* If ipfw says divert, we have to just drop packet */ 372 /* use port as a dummy argument */ 373 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 374 m_freem(m); 375 m = NULL; 376 } 377 if (!m) 378 return; 379 } 380 381 /* 382 * Check against address spoofing/corruption. 383 */ 384 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 385 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 386 /* 387 * XXX: "badscope" is not very suitable for a multicast source. 388 */ 389 ip6stat.ip6s_badscope++; 390 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 391 goto bad; 392 } 393 if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 394 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && 395 (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 396 ip6stat.ip6s_badscope++; 397 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 398 goto bad; 399 } 400 401 /* 402 * The following check is not documented in specs. A malicious 403 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack 404 * and bypass security checks (act as if it was from 127.0.0.1 by using 405 * IPv6 src ::ffff:127.0.0.1). Be cautious. 406 * 407 * This check chokes if we are in an SIIT cloud. As none of BSDs 408 * support IPv4-less kernel compilation, we cannot support SIIT 409 * environment at all. So, it makes more sense for us to reject any 410 * malicious packets for non-SIIT environment, than try to do a 411 * partical support for SIIT environment. 412 */ 413 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 414 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 415 ip6stat.ip6s_badscope++; 416 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 417 goto bad; 418 } 419 #if 0 420 /* 421 * Reject packets with IPv4 compatible addresses (auto tunnel). 422 * 423 * The code forbids auto tunnel relay case in RFC1933 (the check is 424 * stronger than RFC1933). We may want to re-enable it if mech-xx 425 * is revised to forbid relaying case. 426 */ 427 if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || 428 IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { 429 ip6stat.ip6s_badscope++; 430 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 431 goto bad; 432 } 433 #endif 434 435 /* drop packets if interface ID portion is already filled */ 436 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 437 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && 438 ip6->ip6_src.s6_addr16[1]) { 439 ip6stat.ip6s_badscope++; 440 goto bad; 441 } 442 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && 443 ip6->ip6_dst.s6_addr16[1]) { 444 ip6stat.ip6s_badscope++; 445 goto bad; 446 } 447 } 448 449 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 450 ip6->ip6_src.s6_addr16[1] 451 = htons(m->m_pkthdr.rcvif->if_index); 452 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 453 ip6->ip6_dst.s6_addr16[1] 454 = htons(m->m_pkthdr.rcvif->if_index); 455 456 #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */ 457 /* 458 * We use rt->rt_ifp to determine if the address is ours or not. 459 * If rt_ifp is lo0, the address is ours. 460 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0, 461 * so any address under fe80::%lo0/64 will be mistakenly considered 462 * local. The special case is supplied to handle the case properly 463 * by actually looking at interface addresses 464 * (using in6ifa_ifpwithaddr). 465 */ 466 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 && 467 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 468 if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) { 469 icmp6_error(m, ICMP6_DST_UNREACH, 470 ICMP6_DST_UNREACH_ADDR, 0); 471 /* m is already freed */ 472 return; 473 } 474 475 ours = 1; 476 deliverifp = m->m_pkthdr.rcvif; 477 goto hbhcheck; 478 } 479 #endif 480 481 /* 482 * Multicast check 483 */ 484 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 485 struct in6_multi *in6m = 0; 486 487 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 488 /* 489 * See if we belong to the destination multicast group on the 490 * arrival interface. 491 */ 492 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 493 if (in6m) 494 ours = 1; 495 else if (!ip6_mrouter) { 496 ip6stat.ip6s_notmember++; 497 ip6stat.ip6s_cantforward++; 498 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 499 goto bad; 500 } 501 deliverifp = m->m_pkthdr.rcvif; 502 goto hbhcheck; 503 } 504 505 /* 506 * Unicast check 507 */ 508 switch (ip6_ours_check_algorithm) { 509 default: 510 /* 511 * XXX: I intentionally broke our indentation rule here, 512 * since this switch-case is just for measurement and 513 * therefore should soon be removed. 514 */ 515 if (ip6_forward_rt.ro_rt != NULL && 516 (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && 517 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 518 &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) 519 ip6stat.ip6s_forward_cachehit++; 520 else { 521 struct sockaddr_in6 *dst6; 522 523 if (ip6_forward_rt.ro_rt) { 524 /* route is down or destination is different */ 525 ip6stat.ip6s_forward_cachemiss++; 526 RTFREE(ip6_forward_rt.ro_rt); 527 ip6_forward_rt.ro_rt = 0; 528 } 529 530 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 531 dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; 532 dst6->sin6_len = sizeof(struct sockaddr_in6); 533 dst6->sin6_family = AF_INET6; 534 dst6->sin6_addr = ip6->ip6_dst; 535 #ifdef SCOPEDROUTING 536 ip6_forward_rt.ro_dst.sin6_scope_id = 537 in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst); 538 #endif 539 540 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 541 } 542 543 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 544 545 /* 546 * Accept the packet if the forwarding interface to the destination 547 * according to the routing table is the loopback interface, 548 * unless the associated route has a gateway. 549 * Note that this approach causes to accept a packet if there is a 550 * route to the loopback interface for the destination of the packet. 551 * But we think it's even useful in some situations, e.g. when using 552 * a special daemon which wants to intercept the packet. 553 * 554 * XXX: some OSes automatically make a cloned route for the destination 555 * of an outgoing packet. If the outgoing interface of the packet 556 * is a loopback one, the kernel would consider the packet to be 557 * accepted, even if we have no such address assinged on the interface. 558 * We check the cloned flag of the route entry to reject such cases, 559 * assuming that route entries for our own addresses are not made by 560 * cloning (it should be true because in6_addloop explicitly installs 561 * the host route). However, we might have to do an explicit check 562 * while it would be less efficient. Or, should we rather install a 563 * reject route for such a case? 564 */ 565 if (ip6_forward_rt.ro_rt && 566 (ip6_forward_rt.ro_rt->rt_flags & 567 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 568 #ifdef RTF_WASCLONED 569 !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && 570 #endif 571 #ifdef RTF_CLONED 572 !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && 573 #endif 574 #if 0 575 /* 576 * The check below is redundant since the comparison of 577 * the destination and the key of the rtentry has 578 * already done through looking up the routing table. 579 */ 580 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 581 &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) 582 #endif 583 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 584 struct in6_ifaddr *ia6 = 585 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 586 587 /* 588 * record address information into m_aux. 589 */ 590 (void)ip6_setdstifaddr(m, ia6); 591 592 /* 593 * packets to a tentative, duplicated, or somehow invalid 594 * address must not be accepted. 595 */ 596 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 597 /* this address is ready */ 598 ours = 1; 599 deliverifp = ia6->ia_ifp; /* correct? */ 600 /* Count the packet in the ip address stats */ 601 ia6->ia_ifa.if_ipackets++; 602 ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; 603 goto hbhcheck; 604 } else { 605 /* address is not ready, so discard the packet. */ 606 nd6log((LOG_INFO, 607 "ip6_input: packet to an unready address %s->%s\n", 608 ip6_sprintf(&ip6->ip6_src), 609 ip6_sprintf(&ip6->ip6_dst))); 610 611 goto bad; 612 } 613 } 614 } /* XXX indentation (see above) */ 615 616 /* 617 * FAITH(Firewall Aided Internet Translator) 618 */ 619 if (ip6_keepfaith) { 620 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 621 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 622 /* XXX do we need more sanity checks? */ 623 ours = 1; 624 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ 625 goto hbhcheck; 626 } 627 } 628 629 /* 630 * Now there is no reason to process the packet if it's not our own 631 * and we're not a router. 632 */ 633 if (!ip6_forwarding) { 634 ip6stat.ip6s_cantforward++; 635 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 636 goto bad; 637 } 638 639 hbhcheck: 640 /* 641 * record address information into m_aux, if we don't have one yet. 642 * note that we are unable to record it, if the address is not listed 643 * as our interface address (e.g. multicast addresses, addresses 644 * within FAITH prefixes and such). 645 */ 646 if (deliverifp && !ip6_getdstifaddr(m)) { 647 struct in6_ifaddr *ia6; 648 649 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 650 if (ia6) { 651 if (!ip6_setdstifaddr(m, ia6)) { 652 /* 653 * XXX maybe we should drop the packet here, 654 * as we could not provide enough information 655 * to the upper layers. 656 */ 657 } 658 } 659 } 660 661 /* 662 * Process Hop-by-Hop options header if it's contained. 663 * m may be modified in ip6_hopopts_input(). 664 * If a JumboPayload option is included, plen will also be modified. 665 */ 666 plen = (u_int32_t)ntohs(ip6->ip6_plen); 667 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 668 struct ip6_hbh *hbh; 669 670 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 671 #if 0 /*touches NULL pointer*/ 672 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 673 #endif 674 return; /* m have already been freed */ 675 } 676 677 /* adjust pointer */ 678 ip6 = mtod(m, struct ip6_hdr *); 679 680 /* 681 * if the payload length field is 0 and the next header field 682 * indicates Hop-by-Hop Options header, then a Jumbo Payload 683 * option MUST be included. 684 */ 685 if (ip6->ip6_plen == 0 && plen == 0) { 686 /* 687 * Note that if a valid jumbo payload option is 688 * contained, ip6_hoptops_input() must set a valid 689 * (non-zero) payload length to the variable plen. 690 */ 691 ip6stat.ip6s_badoptions++; 692 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 693 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 694 icmp6_error(m, ICMP6_PARAM_PROB, 695 ICMP6_PARAMPROB_HEADER, 696 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); 697 return; 698 } 699 #ifndef PULLDOWN_TEST 700 /* ip6_hopopts_input() ensures that mbuf is contiguous */ 701 hbh = (struct ip6_hbh *)(ip6 + 1); 702 #else 703 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 704 sizeof(struct ip6_hbh)); 705 if (hbh == NULL) { 706 ip6stat.ip6s_tooshort++; 707 return; 708 } 709 #endif 710 nxt = hbh->ip6h_nxt; 711 712 /* 713 * accept the packet if a router alert option is included 714 * and we act as an IPv6 router. 715 */ 716 if (rtalert != ~0 && ip6_forwarding) 717 ours = 1; 718 } else 719 nxt = ip6->ip6_nxt; 720 721 /* 722 * Check that the amount of data in the buffers 723 * is as at least much as the IPv6 header would have us expect. 724 * Trim mbufs if longer than we expect. 725 * Drop packet if shorter than we expect. 726 */ 727 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 728 ip6stat.ip6s_tooshort++; 729 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 730 goto bad; 731 } 732 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 733 if (m->m_len == m->m_pkthdr.len) { 734 m->m_len = sizeof(struct ip6_hdr) + plen; 735 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 736 } else 737 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 738 } 739 740 /* 741 * Forward if desirable. 742 */ 743 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 744 /* 745 * If we are acting as a multicast router, all 746 * incoming multicast packets are passed to the 747 * kernel-level multicast forwarding function. 748 * The packet is returned (relatively) intact; if 749 * ip6_mforward() returns a non-zero value, the packet 750 * must be discarded, else it may be accepted below. 751 */ 752 if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { 753 ip6stat.ip6s_cantforward++; 754 m_freem(m); 755 return; 756 } 757 if (!ours) { 758 m_freem(m); 759 return; 760 } 761 } else if (!ours) { 762 ip6_forward(m, 0); 763 return; 764 } 765 766 ip6 = mtod(m, struct ip6_hdr *); 767 768 /* 769 * Malicious party may be able to use IPv4 mapped addr to confuse 770 * tcp/udp stack and bypass security checks (act as if it was from 771 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. 772 * 773 * For SIIT end node behavior, you may want to disable the check. 774 * However, you will become vulnerable to attacks using IPv4 mapped 775 * source. 776 */ 777 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 778 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 779 ip6stat.ip6s_badscope++; 780 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 781 goto bad; 782 } 783 784 /* 785 * Tell launch routine the next header 786 */ 787 ip6stat.ip6s_delivered++; 788 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 789 nest = 0; 790 791 while (nxt != IPPROTO_DONE) { 792 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 793 ip6stat.ip6s_toomanyhdr++; 794 goto bad; 795 } 796 797 /* 798 * protection against faulty packet - there should be 799 * more sanity checks in header chain processing. 800 */ 801 if (m->m_pkthdr.len < off) { 802 ip6stat.ip6s_tooshort++; 803 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 804 goto bad; 805 } 806 807 #if 0 808 /* 809 * do we need to do it for every header? yeah, other 810 * functions can play with it (like re-allocate and copy). 811 */ 812 mhist = ip6_addaux(m); 813 if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { 814 hist = mtod(mhist, caddr_t) + mhist->m_len; 815 bcopy(&nxt, hist, sizeof(nxt)); 816 mhist->m_len += sizeof(nxt); 817 } else { 818 ip6stat.ip6s_toomanyhdr++; 819 goto bad; 820 } 821 #endif 822 823 #ifdef IPSEC 824 /* 825 * enforce IPsec policy checking if we are seeing last header. 826 * note that we do not visit this with protocols with pcb layer 827 * code - like udp/tcp/raw ip. 828 */ 829 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 && 830 ipsec6_in_reject(m, NULL)) { 831 ipsec6stat.in_polvio++; 832 goto bad; 833 } 834 #endif 835 836 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 837 } 838 return; 839 bad: 840 m_freem(m); 841 } 842 843 /* 844 * set/grab in6_ifaddr correspond to IPv6 destination address. 845 * XXX backward compatibility wrapper 846 */ 847 static struct ip6aux * 848 ip6_setdstifaddr(m, ia6) 849 struct mbuf *m; 850 struct in6_ifaddr *ia6; 851 { 852 struct ip6aux *n; 853 854 n = ip6_addaux(m); 855 if (n) 856 n->ip6a_dstia6 = ia6; 857 return n; /* NULL if failed to set */ 858 } 859 860 struct in6_ifaddr * 861 ip6_getdstifaddr(m) 862 struct mbuf *m; 863 { 864 struct ip6aux *n; 865 866 n = ip6_findaux(m); 867 if (n) 868 return n->ip6a_dstia6; 869 else 870 return NULL; 871 } 872 873 /* 874 * Hop-by-Hop options header processing. If a valid jumbo payload option is 875 * included, the real payload length will be stored in plenp. 876 */ 877 static int 878 ip6_hopopts_input(plenp, rtalertp, mp, offp) 879 u_int32_t *plenp; 880 u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 881 struct mbuf **mp; 882 int *offp; 883 { 884 struct mbuf *m = *mp; 885 int off = *offp, hbhlen; 886 struct ip6_hbh *hbh; 887 u_int8_t *opt; 888 889 /* validation of the length of the header */ 890 #ifndef PULLDOWN_TEST 891 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 892 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 893 hbhlen = (hbh->ip6h_len + 1) << 3; 894 895 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 896 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 897 #else 898 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, 899 sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); 900 if (hbh == NULL) { 901 ip6stat.ip6s_tooshort++; 902 return -1; 903 } 904 hbhlen = (hbh->ip6h_len + 1) << 3; 905 IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), 906 hbhlen); 907 if (hbh == NULL) { 908 ip6stat.ip6s_tooshort++; 909 return -1; 910 } 911 #endif 912 off += hbhlen; 913 hbhlen -= sizeof(struct ip6_hbh); 914 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 915 916 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 917 hbhlen, rtalertp, plenp) < 0) 918 return(-1); 919 920 *offp = off; 921 *mp = m; 922 return(0); 923 } 924 925 /* 926 * Search header for all Hop-by-hop options and process each option. 927 * This function is separate from ip6_hopopts_input() in order to 928 * handle a case where the sending node itself process its hop-by-hop 929 * options header. In such a case, the function is called from ip6_output(). 930 * 931 * The function assumes that hbh header is located right after the IPv6 header 932 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to 933 * opthead + hbhlen is located in continuous memory region. 934 */ 935 int 936 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 937 struct mbuf *m; 938 u_int8_t *opthead; 939 int hbhlen; 940 u_int32_t *rtalertp; 941 u_int32_t *plenp; 942 { 943 struct ip6_hdr *ip6; 944 int optlen = 0; 945 u_int8_t *opt = opthead; 946 u_int16_t rtalert_val; 947 u_int32_t jumboplen; 948 const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); 949 950 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 951 switch (*opt) { 952 case IP6OPT_PAD1: 953 optlen = 1; 954 break; 955 case IP6OPT_PADN: 956 if (hbhlen < IP6OPT_MINLEN) { 957 ip6stat.ip6s_toosmall++; 958 goto bad; 959 } 960 optlen = *(opt + 1) + 2; 961 break; 962 case IP6OPT_RTALERT: 963 /* XXX may need check for alignment */ 964 if (hbhlen < IP6OPT_RTALERT_LEN) { 965 ip6stat.ip6s_toosmall++; 966 goto bad; 967 } 968 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { 969 /* XXX stat */ 970 icmp6_error(m, ICMP6_PARAM_PROB, 971 ICMP6_PARAMPROB_HEADER, 972 erroff + opt + 1 - opthead); 973 return(-1); 974 } 975 optlen = IP6OPT_RTALERT_LEN; 976 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 977 *rtalertp = ntohs(rtalert_val); 978 break; 979 case IP6OPT_JUMBO: 980 /* XXX may need check for alignment */ 981 if (hbhlen < IP6OPT_JUMBO_LEN) { 982 ip6stat.ip6s_toosmall++; 983 goto bad; 984 } 985 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { 986 /* XXX stat */ 987 icmp6_error(m, ICMP6_PARAM_PROB, 988 ICMP6_PARAMPROB_HEADER, 989 erroff + opt + 1 - opthead); 990 return(-1); 991 } 992 optlen = IP6OPT_JUMBO_LEN; 993 994 /* 995 * IPv6 packets that have non 0 payload length 996 * must not contain a jumbo payload option. 997 */ 998 ip6 = mtod(m, struct ip6_hdr *); 999 if (ip6->ip6_plen) { 1000 ip6stat.ip6s_badoptions++; 1001 icmp6_error(m, ICMP6_PARAM_PROB, 1002 ICMP6_PARAMPROB_HEADER, 1003 erroff + opt - opthead); 1004 return(-1); 1005 } 1006 1007 /* 1008 * We may see jumbolen in unaligned location, so 1009 * we'd need to perform bcopy(). 1010 */ 1011 bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); 1012 jumboplen = (u_int32_t)htonl(jumboplen); 1013 1014 #if 1 1015 /* 1016 * if there are multiple jumbo payload options, 1017 * *plenp will be non-zero and the packet will be 1018 * rejected. 1019 * the behavior may need some debate in ipngwg - 1020 * multiple options does not make sense, however, 1021 * there's no explicit mention in specification. 1022 */ 1023 if (*plenp != 0) { 1024 ip6stat.ip6s_badoptions++; 1025 icmp6_error(m, ICMP6_PARAM_PROB, 1026 ICMP6_PARAMPROB_HEADER, 1027 erroff + opt + 2 - opthead); 1028 return(-1); 1029 } 1030 #endif 1031 1032 /* 1033 * jumbo payload length must be larger than 65535. 1034 */ 1035 if (jumboplen <= IPV6_MAXPACKET) { 1036 ip6stat.ip6s_badoptions++; 1037 icmp6_error(m, ICMP6_PARAM_PROB, 1038 ICMP6_PARAMPROB_HEADER, 1039 erroff + opt + 2 - opthead); 1040 return(-1); 1041 } 1042 *plenp = jumboplen; 1043 1044 break; 1045 default: /* unknown option */ 1046 if (hbhlen < IP6OPT_MINLEN) { 1047 ip6stat.ip6s_toosmall++; 1048 goto bad; 1049 } 1050 optlen = ip6_unknown_opt(opt, m, 1051 erroff + opt - opthead); 1052 if (optlen == -1) 1053 return(-1); 1054 optlen += 2; 1055 break; 1056 } 1057 } 1058 1059 return(0); 1060 1061 bad: 1062 m_freem(m); 1063 return(-1); 1064 } 1065 1066 /* 1067 * Unknown option processing. 1068 * The third argument `off' is the offset from the IPv6 header to the option, 1069 * which is necessary if the IPv6 header the and option header and IPv6 header 1070 * is not continuous in order to return an ICMPv6 error. 1071 */ 1072 int 1073 ip6_unknown_opt(optp, m, off) 1074 u_int8_t *optp; 1075 struct mbuf *m; 1076 int off; 1077 { 1078 struct ip6_hdr *ip6; 1079 1080 switch (IP6OPT_TYPE(*optp)) { 1081 case IP6OPT_TYPE_SKIP: /* ignore the option */ 1082 return((int)*(optp + 1)); 1083 case IP6OPT_TYPE_DISCARD: /* silently discard */ 1084 m_freem(m); 1085 return(-1); 1086 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 1087 ip6stat.ip6s_badoptions++; 1088 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 1089 return(-1); 1090 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 1091 ip6stat.ip6s_badoptions++; 1092 ip6 = mtod(m, struct ip6_hdr *); 1093 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1094 (m->m_flags & (M_BCAST|M_MCAST))) 1095 m_freem(m); 1096 else 1097 icmp6_error(m, ICMP6_PARAM_PROB, 1098 ICMP6_PARAMPROB_OPTION, off); 1099 return(-1); 1100 } 1101 1102 m_freem(m); /* XXX: NOTREACHED */ 1103 return(-1); 1104 } 1105 1106 /* 1107 * Create the "control" list for this pcb. 1108 * The function will not modify mbuf chain at all. 1109 * 1110 * with KAME mbuf chain restriction: 1111 * The routine will be called from upper layer handlers like tcp6_input(). 1112 * Thus the routine assumes that the caller (tcp6_input) have already 1113 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the 1114 * very first mbuf on the mbuf chain. 1115 */ 1116 void 1117 ip6_savecontrol(in6p, mp, ip6, m) 1118 struct inpcb *in6p; 1119 struct mbuf **mp; 1120 struct ip6_hdr *ip6; 1121 struct mbuf *m; 1122 { 1123 #if __FreeBSD_version >= 500000 1124 struct thread *td = curthread; /* XXX */ 1125 #else 1126 struct proc *td = curproc; /* XXX */ 1127 #endif 1128 int privileged = 0; 1129 int rthdr_exist = 0; 1130 1131 1132 if (td && !suser(td)) 1133 privileged++; 1134 1135 #ifdef SO_TIMESTAMP 1136 if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { 1137 struct timeval tv; 1138 1139 microtime(&tv); 1140 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1141 SCM_TIMESTAMP, SOL_SOCKET); 1142 if (*mp) { 1143 mp = &(*mp)->m_next; 1144 } 1145 } 1146 #endif 1147 1148 /* RFC 2292 sec. 5 */ 1149 if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { 1150 struct in6_pktinfo pi6; 1151 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 1152 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) 1153 pi6.ipi6_addr.s6_addr16[1] = 0; 1154 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) 1155 ? m->m_pkthdr.rcvif->if_index 1156 : 0; 1157 *mp = sbcreatecontrol((caddr_t) &pi6, 1158 sizeof(struct in6_pktinfo), IPV6_PKTINFO, 1159 IPPROTO_IPV6); 1160 if (*mp) 1161 mp = &(*mp)->m_next; 1162 } 1163 1164 if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { 1165 int hlim = ip6->ip6_hlim & 0xff; 1166 *mp = sbcreatecontrol((caddr_t) &hlim, 1167 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6); 1168 if (*mp) 1169 mp = &(*mp)->m_next; 1170 } 1171 1172 /* 1173 * IPV6_HOPOPTS socket option. We require super-user privilege 1174 * for the option, but it might be too strict, since there might 1175 * be some hop-by-hop options which can be returned to normal user. 1176 * See RFC 2292 section 6. 1177 */ 1178 if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) { 1179 /* 1180 * Check if a hop-by-hop options header is contatined in the 1181 * received packet, and if so, store the options as ancillary 1182 * data. Note that a hop-by-hop options header must be 1183 * just after the IPv6 header, which fact is assured through 1184 * the IPv6 input processing. 1185 */ 1186 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1187 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 1188 struct ip6_hbh *hbh; 1189 int hbhlen = 0; 1190 #ifdef PULLDOWN_TEST 1191 struct mbuf *ext; 1192 #endif 1193 1194 #ifndef PULLDOWN_TEST 1195 hbh = (struct ip6_hbh *)(ip6 + 1); 1196 hbhlen = (hbh->ip6h_len + 1) << 3; 1197 #else 1198 ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), 1199 ip6->ip6_nxt); 1200 if (ext == NULL) { 1201 ip6stat.ip6s_tooshort++; 1202 return; 1203 } 1204 hbh = mtod(ext, struct ip6_hbh *); 1205 hbhlen = (hbh->ip6h_len + 1) << 3; 1206 if (hbhlen != ext->m_len) { 1207 m_freem(ext); 1208 ip6stat.ip6s_tooshort++; 1209 return; 1210 } 1211 #endif 1212 1213 /* 1214 * XXX: We copy whole the header even if a jumbo 1215 * payload option is included, which option is to 1216 * be removed before returning in the RFC 2292. 1217 * Note: this constraint is removed in 2292bis. 1218 */ 1219 *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, 1220 IPV6_HOPOPTS, IPPROTO_IPV6); 1221 if (*mp) 1222 mp = &(*mp)->m_next; 1223 #ifdef PULLDOWN_TEST 1224 m_freem(ext); 1225 #endif 1226 } 1227 } 1228 1229 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 1230 if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1231 int proto, off, nxt; 1232 1233 /* 1234 * go through the header chain to see if a routing header is 1235 * contained in the packet. We need this information to store 1236 * destination options headers (if any) properly. 1237 * XXX: performance issue. We should record this info when 1238 * processing extension headers in incoming routine. 1239 * (todo) use m_aux? 1240 */ 1241 proto = IPPROTO_IPV6; 1242 off = 0; 1243 nxt = -1; 1244 while (1) { 1245 int newoff; 1246 1247 newoff = ip6_nexthdr(m, off, proto, &nxt); 1248 if (newoff < 0) 1249 break; 1250 if (newoff < off) /* invalid, check for safety */ 1251 break; 1252 if ((proto = nxt) == IPPROTO_ROUTING) { 1253 rthdr_exist = 1; 1254 break; 1255 } 1256 off = newoff; 1257 } 1258 } 1259 1260 if ((in6p->in6p_flags & 1261 (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { 1262 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1263 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); 1264 1265 /* 1266 * Search for destination options headers or routing 1267 * header(s) through the header chain, and stores each 1268 * header as ancillary data. 1269 * Note that the order of the headers remains in 1270 * the chain of ancillary data. 1271 */ 1272 while (1) { /* is explicit loop prevention necessary? */ 1273 struct ip6_ext *ip6e = NULL; 1274 int elen; 1275 #ifdef PULLDOWN_TEST 1276 struct mbuf *ext = NULL; 1277 #endif 1278 1279 /* 1280 * if it is not an extension header, don't try to 1281 * pull it from the chain. 1282 */ 1283 switch (nxt) { 1284 case IPPROTO_DSTOPTS: 1285 case IPPROTO_ROUTING: 1286 case IPPROTO_HOPOPTS: 1287 case IPPROTO_AH: /* is it possible? */ 1288 break; 1289 default: 1290 goto loopend; 1291 } 1292 1293 #ifndef PULLDOWN_TEST 1294 if (off + sizeof(*ip6e) > m->m_len) 1295 goto loopend; 1296 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); 1297 if (nxt == IPPROTO_AH) 1298 elen = (ip6e->ip6e_len + 2) << 2; 1299 else 1300 elen = (ip6e->ip6e_len + 1) << 3; 1301 if (off + elen > m->m_len) 1302 goto loopend; 1303 #else 1304 ext = ip6_pullexthdr(m, off, nxt); 1305 if (ext == NULL) { 1306 ip6stat.ip6s_tooshort++; 1307 return; 1308 } 1309 ip6e = mtod(ext, struct ip6_ext *); 1310 if (nxt == IPPROTO_AH) 1311 elen = (ip6e->ip6e_len + 2) << 2; 1312 else 1313 elen = (ip6e->ip6e_len + 1) << 3; 1314 if (elen != ext->m_len) { 1315 m_freem(ext); 1316 ip6stat.ip6s_tooshort++; 1317 return; 1318 } 1319 #endif 1320 1321 switch (nxt) { 1322 case IPPROTO_DSTOPTS: 1323 if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0) 1324 break; 1325 1326 /* 1327 * We also require super-user privilege for 1328 * the option. 1329 * See the comments on IN6_HOPOPTS. 1330 */ 1331 if (!privileged) 1332 break; 1333 1334 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1335 IPV6_DSTOPTS, 1336 IPPROTO_IPV6); 1337 if (*mp) 1338 mp = &(*mp)->m_next; 1339 break; 1340 case IPPROTO_ROUTING: 1341 if (!in6p->in6p_flags & IN6P_RTHDR) 1342 break; 1343 1344 *mp = sbcreatecontrol((caddr_t)ip6e, elen, 1345 IPV6_RTHDR, 1346 IPPROTO_IPV6); 1347 if (*mp) 1348 mp = &(*mp)->m_next; 1349 break; 1350 case IPPROTO_HOPOPTS: 1351 case IPPROTO_AH: /* is it possible? */ 1352 break; 1353 1354 default: 1355 /* 1356 * other cases have been filtered in the above. 1357 * none will visit this case. here we supply 1358 * the code just in case (nxt overwritten or 1359 * other cases). 1360 */ 1361 #ifdef PULLDOWN_TEST 1362 m_freem(ext); 1363 #endif 1364 goto loopend; 1365 1366 } 1367 1368 /* proceed with the next header. */ 1369 off += elen; 1370 nxt = ip6e->ip6e_nxt; 1371 ip6e = NULL; 1372 #ifdef PULLDOWN_TEST 1373 m_freem(ext); 1374 ext = NULL; 1375 #endif 1376 } 1377 loopend: 1378 ; 1379 } 1380 1381 } 1382 1383 #ifdef PULLDOWN_TEST 1384 /* 1385 * pull single extension header from mbuf chain. returns single mbuf that 1386 * contains the result, or NULL on error. 1387 */ 1388 static struct mbuf * 1389 ip6_pullexthdr(m, off, nxt) 1390 struct mbuf *m; 1391 size_t off; 1392 int nxt; 1393 { 1394 struct ip6_ext ip6e; 1395 size_t elen; 1396 struct mbuf *n; 1397 1398 #ifdef DIAGNOSTIC 1399 switch (nxt) { 1400 case IPPROTO_DSTOPTS: 1401 case IPPROTO_ROUTING: 1402 case IPPROTO_HOPOPTS: 1403 case IPPROTO_AH: /* is it possible? */ 1404 break; 1405 default: 1406 printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); 1407 } 1408 #endif 1409 1410 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1411 if (nxt == IPPROTO_AH) 1412 elen = (ip6e.ip6e_len + 2) << 2; 1413 else 1414 elen = (ip6e.ip6e_len + 1) << 3; 1415 1416 MGET(n, M_DONTWAIT, MT_DATA); 1417 if (n && elen >= MLEN) { 1418 MCLGET(n, M_DONTWAIT); 1419 if ((n->m_flags & M_EXT) == 0) { 1420 m_free(n); 1421 n = NULL; 1422 } 1423 } 1424 if (!n) 1425 return NULL; 1426 1427 n->m_len = 0; 1428 if (elen >= M_TRAILINGSPACE(n)) { 1429 m_free(n); 1430 return NULL; 1431 } 1432 1433 m_copydata(m, off, elen, mtod(n, caddr_t)); 1434 n->m_len = elen; 1435 return n; 1436 } 1437 #endif 1438 1439 /* 1440 * Get pointer to the previous header followed by the header 1441 * currently processed. 1442 * XXX: This function supposes that 1443 * M includes all headers, 1444 * the next header field and the header length field of each header 1445 * are valid, and 1446 * the sum of each header length equals to OFF. 1447 * Because of these assumptions, this function must be called very 1448 * carefully. Moreover, it will not be used in the near future when 1449 * we develop `neater' mechanism to process extension headers. 1450 */ 1451 char * 1452 ip6_get_prevhdr(m, off) 1453 struct mbuf *m; 1454 int off; 1455 { 1456 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1457 1458 if (off == sizeof(struct ip6_hdr)) 1459 return(&ip6->ip6_nxt); 1460 else { 1461 int len, nxt; 1462 struct ip6_ext *ip6e = NULL; 1463 1464 nxt = ip6->ip6_nxt; 1465 len = sizeof(struct ip6_hdr); 1466 while (len < off) { 1467 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 1468 1469 switch (nxt) { 1470 case IPPROTO_FRAGMENT: 1471 len += sizeof(struct ip6_frag); 1472 break; 1473 case IPPROTO_AH: 1474 len += (ip6e->ip6e_len + 2) << 2; 1475 break; 1476 default: 1477 len += (ip6e->ip6e_len + 1) << 3; 1478 break; 1479 } 1480 nxt = ip6e->ip6e_nxt; 1481 } 1482 if (ip6e) 1483 return(&ip6e->ip6e_nxt); 1484 else 1485 return NULL; 1486 } 1487 } 1488 1489 /* 1490 * get next header offset. m will be retained. 1491 */ 1492 int 1493 ip6_nexthdr(m, off, proto, nxtp) 1494 struct mbuf *m; 1495 int off; 1496 int proto; 1497 int *nxtp; 1498 { 1499 struct ip6_hdr ip6; 1500 struct ip6_ext ip6e; 1501 struct ip6_frag fh; 1502 1503 /* just in case */ 1504 if (m == NULL) 1505 panic("ip6_nexthdr: m == NULL"); 1506 if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) 1507 return -1; 1508 1509 switch (proto) { 1510 case IPPROTO_IPV6: 1511 if (m->m_pkthdr.len < off + sizeof(ip6)) 1512 return -1; 1513 m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); 1514 if (nxtp) 1515 *nxtp = ip6.ip6_nxt; 1516 off += sizeof(ip6); 1517 return off; 1518 1519 case IPPROTO_FRAGMENT: 1520 /* 1521 * terminate parsing if it is not the first fragment, 1522 * it does not make sense to parse through it. 1523 */ 1524 if (m->m_pkthdr.len < off + sizeof(fh)) 1525 return -1; 1526 m_copydata(m, off, sizeof(fh), (caddr_t)&fh); 1527 if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0) 1528 return -1; 1529 if (nxtp) 1530 *nxtp = fh.ip6f_nxt; 1531 off += sizeof(struct ip6_frag); 1532 return off; 1533 1534 case IPPROTO_AH: 1535 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1536 return -1; 1537 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1538 if (nxtp) 1539 *nxtp = ip6e.ip6e_nxt; 1540 off += (ip6e.ip6e_len + 2) << 2; 1541 return off; 1542 1543 case IPPROTO_HOPOPTS: 1544 case IPPROTO_ROUTING: 1545 case IPPROTO_DSTOPTS: 1546 if (m->m_pkthdr.len < off + sizeof(ip6e)) 1547 return -1; 1548 m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); 1549 if (nxtp) 1550 *nxtp = ip6e.ip6e_nxt; 1551 off += (ip6e.ip6e_len + 1) << 3; 1552 return off; 1553 1554 case IPPROTO_NONE: 1555 case IPPROTO_ESP: 1556 case IPPROTO_IPCOMP: 1557 /* give up */ 1558 return -1; 1559 1560 default: 1561 return -1; 1562 } 1563 1564 return -1; 1565 } 1566 1567 /* 1568 * get offset for the last header in the chain. m will be kept untainted. 1569 */ 1570 int 1571 ip6_lasthdr(m, off, proto, nxtp) 1572 struct mbuf *m; 1573 int off; 1574 int proto; 1575 int *nxtp; 1576 { 1577 int newoff; 1578 int nxt; 1579 1580 if (!nxtp) { 1581 nxt = -1; 1582 nxtp = &nxt; 1583 } 1584 while (1) { 1585 newoff = ip6_nexthdr(m, off, proto, nxtp); 1586 if (newoff < 0) 1587 return off; 1588 else if (newoff < off) 1589 return -1; /* invalid */ 1590 else if (newoff == off) 1591 return newoff; 1592 1593 off = newoff; 1594 proto = *nxtp; 1595 } 1596 } 1597 1598 struct ip6aux * 1599 ip6_addaux(m) 1600 struct mbuf *m; 1601 { 1602 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1603 if (!tag) { 1604 tag = m_tag_get(PACKET_TAG_IPV6_INPUT, 1605 sizeof (struct ip6aux), 1606 M_NOWAIT); 1607 if (tag) 1608 m_tag_prepend(m, tag); 1609 } 1610 if (tag) 1611 bzero(tag+1, sizeof (struct ip6aux)); 1612 return tag ? (struct ip6aux*)(tag+1) : NULL; 1613 } 1614 1615 struct ip6aux * 1616 ip6_findaux(m) 1617 struct mbuf *m; 1618 { 1619 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1620 return tag ? (struct ip6aux*)(tag+1) : NULL; 1621 } 1622 1623 void 1624 ip6_delaux(m) 1625 struct mbuf *m; 1626 { 1627 struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL); 1628 if (tag) 1629 m_tag_delete(m, tag); 1630 } 1631 1632 /* 1633 * System control for IP6 1634 */ 1635 1636 u_char inet6ctlerrmap[PRC_NCMDS] = { 1637 0, 0, 0, 0, 1638 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1639 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1640 EMSGSIZE, EHOSTUNREACH, 0, 0, 1641 0, 0, 0, 0, 1642 ENOPROTOOPT 1643 }; 1644