ip6_input.c revision 53626
1/* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/sys/netinet6/ip6_input.c 53626 1999-11-23 05:42:36Z shin $ 30 */ 31 32/* 33 * Copyright (c) 1982, 1986, 1988, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the University of 47 * California, Berkeley and its contributors. 48 * 4. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 65 */ 66 67#include "opt_inet.h" 68 69#include <sys/param.h> 70#include <sys/systm.h> 71#include <sys/malloc.h> 72#include <sys/mbuf.h> 73#include <sys/domain.h> 74#include <sys/protosw.h> 75#include <sys/socket.h> 76#include <sys/socketvar.h> 77#include <sys/errno.h> 78#include <sys/time.h> 79#include <sys/kernel.h> 80#include <sys/syslog.h> 81#include <sys/proc.h> 82 83#include <net/if.h> 84#include <net/if_types.h> 85#include <net/if_dl.h> 86#include <net/route.h> 87#include <net/netisr.h> 88 89#include <netinet/in.h> 90#include <netinet/in_systm.h> 91#ifdef INET 92#include <netinet/ip.h> 93#include <netinet/ip_icmp.h> 94#endif /*INET*/ 95#include <netinet/in_pcb.h> 96#include <netinet6/in6_var.h> 97#include <netinet6/ip6.h> 98#include <netinet6/ip6_var.h> 99#include <netinet6/icmp6.h> 100#include <netinet6/in6_ifattach.h> 101#include <netinet6/nd6.h> 102#include <netinet6/in6_prefix.h> 103 104#ifdef IPV6FIREWALL 105#include <netinet6/ip6_fw.h> 106#endif 107 108#ifdef ALTQ 109#include <netinet/altq_cdnr.h> 110#endif 111 112#include <netinet6/ip6protosw.h> 113 114/* we need it for NLOOP. */ 115#include "loop.h" 116/* #include "faith.h" */ 117 118/* #include "gif.h" */ 119 120#include <net/net_osdep.h> 121 122extern struct domain inet6domain; 123extern struct ip6protosw inet6sw[]; 124 125u_char ip6_protox[IPPROTO_MAX]; 126static int ip6qmaxlen = IFQ_MAXLEN; 127struct in6_ifaddr *in6_ifaddr; 128struct ifqueue ip6intrq; 129 130int ip6_forward_srcrt; /* XXX */ 131int ip6_sourcecheck; /* XXX */ 132int ip6_sourcecheck_interval; /* XXX */ 133 134#ifdef IPV6FIREWALL 135/* firewall hooks */ 136ip6_fw_chk_t *ip6_fw_chk_ptr; 137ip6_fw_ctl_t *ip6_fw_ctl_ptr; 138#endif 139 140struct ip6stat ip6stat; 141 142static void ip6_init2 __P((void *)); 143 144static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *)); 145 146#if defined(PTR) 147extern int ip6_protocol_tr; 148 149int ptr_in6 __P((struct mbuf *, struct mbuf **)); 150extern void ip_forward __P((struct mbuf *, int)); 151#endif 152 153/* 154 * IP6 initialization: fill in IP6 protocol switch table. 155 * All protocols not implemented in kernel go to raw IP6 protocol handler. 156 */ 157void 158ip6_init() 159{ 160 register struct ip6protosw *pr; 161 register int i; 162 struct timeval tv; 163 164 pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW); 165 if (pr == 0) 166 panic("ip6_init"); 167 for (i = 0; i < IPPROTO_MAX; i++) 168 ip6_protox[i] = pr - inet6sw; 169 for (pr = (struct ip6protosw *)inet6domain.dom_protosw; 170 pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++) 171 if (pr->pr_domain->dom_family == PF_INET6 && 172 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 173 ip6_protox[pr->pr_protocol] = pr - inet6sw; 174 ip6intrq.ifq_maxlen = ip6qmaxlen; 175 nd6_init(); 176 frag6_init(); 177#ifdef IPV6FIREWALL 178 ip6_fw_init(); 179#endif 180 /* 181 * in many cases, random() here does NOT return random number 182 * as initialization during bootstrap time occur in fixed order. 183 */ 184 microtime(&tv); 185 ip6_flow_seq = random() ^ tv.tv_usec; 186} 187 188static void 189ip6_init2(dummy) 190 void *dummy; 191{ 192 int i; 193 int ret; 194 195 /* get EUI64 from somewhere */ 196 ret = in6_ifattach_getifid(NULL); 197 198 /* 199 * to route local address of p2p link to loopback, 200 * assign loopback address first. 201 */ 202 for (i = 0; i < NLOOP; i++) 203 in6_ifattach(&loif[i], IN6_IFT_LOOP, NULL, 0); 204 205 /* attach pseudo interfaces */ 206 if (ret == 0) 207 in6_ifattach_p2p(); 208 209 /* nd6_timer_init */ 210 timeout(nd6_timer, (caddr_t)0, hz); 211 /* router renumbering prefix list maintenance */ 212 timeout(in6_rr_timer, (caddr_t)0, hz); 213} 214 215/* cheat */ 216SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init2, NULL); 217 218/* 219 * IP6 input interrupt handling. Just pass the packet to ip6_input. 220 */ 221void 222ip6intr() 223{ 224 int s; 225 struct mbuf *m; 226 227 for (;;) { 228 s = splimp(); 229 IF_DEQUEUE(&ip6intrq, m); 230 splx(s); 231 if (m == 0) 232 return; 233 ip6_input(m); 234 } 235} 236 237NETISR_SET(NETISR_IPV6, ip6intr); 238 239extern struct route_in6 ip6_forward_rt; 240 241void 242ip6_input(m) 243 struct mbuf *m; 244{ 245 struct ip6_hdr *ip6; 246 int off = sizeof(struct ip6_hdr), nest; 247 u_int32_t plen; 248 u_int32_t rtalert = ~0; 249 int nxt, ours = 0; 250 struct ifnet *deliverifp = NULL; 251 252#ifdef IPSEC 253 /* 254 * should the inner packet be considered authentic? 255 * see comment in ah4_input(). 256 */ 257 if (m) { 258 m->m_flags &= ~M_AUTHIPHDR; 259 m->m_flags &= ~M_AUTHIPDGM; 260 } 261#endif 262 263 /* 264 * mbuf statistics by kazu 265 */ 266 if (m->m_flags & M_EXT) { 267 if (m->m_next) 268 ip6stat.ip6s_mext2m++; 269 else 270 ip6stat.ip6s_mext1++; 271 } else { 272 if (m->m_next) { 273 if (m->m_flags & M_LOOP) { 274 ip6stat.ip6s_m2m[loif[0].if_index]++; /*XXX*/ 275 } else if (m->m_pkthdr.rcvif->if_index <= 31) 276 ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; 277 else 278 ip6stat.ip6s_m2m[0]++; 279 } else 280 ip6stat.ip6s_m1++; 281 } 282 283 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); 284 ip6stat.ip6s_total++; 285 286 IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/); 287 288 if (m->m_len < sizeof(struct ip6_hdr)) { 289 struct ifnet *inifp; 290 inifp = m->m_pkthdr.rcvif; 291 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) { 292 ip6stat.ip6s_toosmall++; 293 in6_ifstat_inc(inifp, ifs6_in_hdrerr); 294 return; 295 } 296 } 297 298 ip6 = mtod(m, struct ip6_hdr *); 299 300 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 301 ip6stat.ip6s_badvers++; 302 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); 303 goto bad; 304 } 305 306 ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; 307 308#ifdef IPV6FIREWALL 309 /* 310 * Check with the firewall... 311 */ 312 if (ip6_fw_chk_ptr) { 313 u_short port = 0; 314 /* If ipfw says divert, we have to just drop packet */ 315 /* use port as a dummy argument */ 316 if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { 317 m_freem(m); 318 m = NULL; 319 } 320 if (!m) 321 return; 322 } 323#endif 324 325#ifdef ALTQ 326 if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) { 327 /* packet is dropped by traffic conditioner */ 328 return; 329 } 330#endif 331 332 /* 333 * Scope check 334 */ 335 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || 336 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { 337 ip6stat.ip6s_badscope++; 338 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 339 goto bad; 340 } 341 if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || 342 IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) { 343 if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) { 344 ours = 1; 345 deliverifp = m->m_pkthdr.rcvif; 346 goto hbhcheck; 347 } else { 348 ip6stat.ip6s_badscope++; 349 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); 350 goto bad; 351 } 352 } 353 354 if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) { 355 if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 356 ours = 1; 357 deliverifp = m->m_pkthdr.rcvif; 358 goto hbhcheck; 359 } 360 } else { 361 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 362 ip6->ip6_src.s6_addr16[1] 363 = htons(m->m_pkthdr.rcvif->if_index); 364 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 365 ip6->ip6_dst.s6_addr16[1] 366 = htons(m->m_pkthdr.rcvif->if_index); 367 } 368 369#if defined(PTR) 370 /* 371 * 372 */ 373 if (ip6_protocol_tr) 374 { 375 struct mbuf *m1 = NULL; 376 377 switch (ptr_in6(m, &m1)) 378 { 379 case IPPROTO_IP: goto mcastcheck; 380 case IPPROTO_IPV4: ip_forward(m1, 0); break; 381 case IPPROTO_IPV6: ip6_forward(m1, 0); break; 382 case IPPROTO_MAX: /* discard this packet */ 383 default: 384 } 385 386 if (m != m1) 387 m_freem(m); 388 389 return; 390 } 391 392 mcastcheck: 393#endif 394 395 /* 396 * Multicast check 397 */ 398 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 399 struct in6_multi *in6m = 0; 400 401 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); 402 /* 403 * See if we belong to the destination multicast group on the 404 * arrival interface. 405 */ 406 IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); 407 if (in6m) 408 ours = 1; 409 else { 410 ip6stat.ip6s_notmember++; 411 ip6stat.ip6s_cantforward++; 412 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 413 goto bad; 414 } 415 deliverifp = m->m_pkthdr.rcvif; 416 goto hbhcheck; 417 } 418 419 /* 420 * Unicast check 421 */ 422 if (ip6_forward_rt.ro_rt == 0 || 423 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, 424 &ip6_forward_rt.ro_dst.sin6_addr)) { 425 if (ip6_forward_rt.ro_rt) { 426 RTFREE(ip6_forward_rt.ro_rt); 427 ip6_forward_rt.ro_rt = 0; 428 } 429 bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); 430 ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6); 431 ip6_forward_rt.ro_dst.sin6_family = AF_INET6; 432 ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst; 433 434 rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); 435 } 436 437#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) 438 439 /* 440 * Accept the packet if the forwarding interface to the destination 441 * according to the routing table is the loopback interface, 442 * unless the associated route has a gateway. 443 * Note that this approach causes to accept a packet if there is a 444 * route to the loopback interface for the destination of the packet. 445 * But we think it's even useful in some situations, e.g. when using 446 * a special daemon which wants to intercept the packet. 447 */ 448 if (ip6_forward_rt.ro_rt && 449 (ip6_forward_rt.ro_rt->rt_flags & 450 (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && 451 /* 452 * The comparison of the destination and the key of the rtentry 453 * has already done through looking up the routing table, 454 * so no need to do such a comparison here again. 455 */ 456 ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { 457 struct in6_ifaddr *ia6 = 458 (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; 459 /* packet to tentative address must not be received */ 460 if (ia6->ia6_flags & IN6_IFF_ANYCAST) 461 m->m_flags |= M_ANYCAST6; 462 if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { 463 /* this interface is ready */ 464 ours = 1; 465 deliverifp = ia6->ia_ifp; /* correct? */ 466 goto hbhcheck; 467 } else { 468 /* this interface is not ready, fall through */ 469 } 470 } 471 472 /* 473 * FAITH(Firewall Aided Internet Translator) 474 */ 475#if defined(NFAITH) && 0 < NFAITH 476 if (ip6_keepfaith) { 477 if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp 478 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { 479 /* XXX do we need more sanity checks? */ 480 ours = 1; 481 deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/ 482 goto hbhcheck; 483 } 484 } 485#endif 486 487 /* 488 * Now there is no reason to process the packet if it's not our own 489 * and we're not a router. 490 */ 491 if (!ip6_forwarding) { 492 ip6stat.ip6s_cantforward++; 493 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 494 goto bad; 495 } 496 497 hbhcheck: 498 /* 499 * Process Hop-by-Hop options header if it's contained. 500 * m may be modified in ip6_hopopts_input(). 501 * If a JumboPayload option is included, plen will also be modified. 502 */ 503 plen = (u_int32_t)ntohs(ip6->ip6_plen); 504 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 505 if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { 506 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); 507 return; /* m have already been freed */ 508 } 509 /* adjust pointer */ 510 ip6 = mtod(m, struct ip6_hdr *); 511 nxt = ((struct ip6_hbh *)(ip6 + 1))->ip6h_nxt; 512 513 /* 514 * accept the packet if a router alert option is included 515 * and we act as an IPv6 router. 516 */ 517 if (rtalert != ~0 && ip6_forwarding) 518 ours = 1; 519 } else 520 nxt = ip6->ip6_nxt; 521 522 /* 523 * Check that the amount of data in the buffers 524 * is as at least much as the IPv6 header would have us expect. 525 * Trim mbufs if longer than we expect. 526 * Drop packet if shorter than we expect. 527 */ 528 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { 529 ip6stat.ip6s_tooshort++; 530 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 531 goto bad; 532 } 533 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { 534 if (m->m_len == m->m_pkthdr.len) { 535 m->m_len = sizeof(struct ip6_hdr) + plen; 536 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; 537 } else 538 m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); 539 } 540 541 /* 542 * Forward if desirable. 543 */ 544 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 545 if (!ours) { 546 m_freem(m); 547 return; 548 } 549 } else if (!ours) { 550 ip6_forward(m, 0); 551 return; 552 } 553 554 /* 555 * Tell launch routine the next header 556 */ 557#if defined(__NetBSD__) && defined(IFA_STATS) 558 if (IFA_STATS && deliverifp != NULL) { 559 struct in6_ifaddr *ia6; 560 ip6 = mtod(m, struct ip6_hdr *); 561 ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); 562 if (ia6) 563 ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len; 564 } 565#endif 566 ip6stat.ip6s_delivered++; 567 in6_ifstat_inc(deliverifp, ifs6_in_deliver); 568 nest = 0; 569 while (nxt != IPPROTO_DONE) { 570 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { 571 ip6stat.ip6s_toomanyhdr++; 572 goto bad; 573 } 574 575 /* 576 * protection against faulty packet - there should be 577 * more sanity checks in header chain processing. 578 */ 579 if (m->m_pkthdr.len < off) { 580 ip6stat.ip6s_tooshort++; 581 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); 582 goto bad; 583 } 584 585 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt); 586 } 587 return; 588 bad: 589 m_freem(m); 590} 591 592/* 593 * Hop-by-Hop options header processing. If a valid jumbo payload option is 594 * included, the real payload length will be stored in plenp. 595 */ 596static int 597ip6_hopopts_input(plenp, rtalertp, mp, offp) 598 u_int32_t *plenp; 599 u_int32_t *rtalertp; /* XXX: should be stored more smart way */ 600 struct mbuf **mp; 601 int *offp; 602{ 603 register struct mbuf *m = *mp; 604 int off = *offp, hbhlen; 605 struct ip6_hbh *hbh; 606 u_int8_t *opt; 607 608 /* validation of the length of the header */ 609 IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1); 610 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 611 hbhlen = (hbh->ip6h_len + 1) << 3; 612 613 IP6_EXTHDR_CHECK(m, off, hbhlen, -1); 614 hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); 615 off += hbhlen; 616 hbhlen -= sizeof(struct ip6_hbh); 617 opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); 618 619 if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), 620 hbhlen, rtalertp, plenp) < 0) 621 return(-1); 622 623 *offp = off; 624 *mp = m; 625 return(0); 626} 627 628/* 629 * Search header for all Hop-by-hop options and process each option. 630 * This function is separate from ip6_hopopts_input() in order to 631 * handle a case where the sending node itself process its hop-by-hop 632 * options header. In such a case, the function is called from ip6_output(). 633 */ 634int 635ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) 636 struct mbuf *m; 637 u_int8_t *opthead; 638 int hbhlen; 639 u_int32_t *rtalertp; 640 u_int32_t *plenp; 641{ 642 struct ip6_hdr *ip6; 643 int optlen = 0; 644 u_int8_t *opt = opthead; 645 u_int16_t rtalert_val; 646 647 for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { 648 switch(*opt) { 649 case IP6OPT_PAD1: 650 optlen = 1; 651 break; 652 case IP6OPT_PADN: 653 if (hbhlen < IP6OPT_MINLEN) { 654 ip6stat.ip6s_toosmall++; 655 goto bad; 656 } 657 optlen = *(opt + 1) + 2; 658 break; 659 case IP6OPT_RTALERT: 660 /* XXX may need check for alignment */ 661 if (hbhlen < IP6OPT_RTALERT_LEN) { 662 ip6stat.ip6s_toosmall++; 663 goto bad; 664 } 665 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) 666 /* XXX: should we discard the packet? */ 667 log(LOG_ERR, "length of router alert opt is inconsitent(%d)", 668 *(opt + 1)); 669 optlen = IP6OPT_RTALERT_LEN; 670 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); 671 *rtalertp = ntohs(rtalert_val); 672 break; 673 case IP6OPT_JUMBO: 674 /* XXX may need check for alignment */ 675 if (hbhlen < IP6OPT_JUMBO_LEN) { 676 ip6stat.ip6s_toosmall++; 677 goto bad; 678 } 679 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) 680 /* XXX: should we discard the packet? */ 681 log(LOG_ERR, "length of jumbopayload opt " 682 "is inconsistent(%d)", 683 *(opt + 1)); 684 optlen = IP6OPT_JUMBO_LEN; 685 686 bcopy(opt + 2, plenp, sizeof(*plenp)); 687 *plenp = htonl(*plenp); 688 if (*plenp <= IPV6_MAXPACKET) { 689 /* 690 * jumbo payload length must be larger 691 * than 65535 692 */ 693 ip6stat.ip6s_badoptions++; 694 icmp6_error(m, ICMP6_PARAM_PROB, 695 ICMP6_PARAMPROB_HEADER, 696 sizeof(struct ip6_hdr) + 697 sizeof(struct ip6_hbh) + 698 opt + 2 - opthead); 699 return(-1); 700 } 701 702 ip6 = mtod(m, struct ip6_hdr *); 703 if (ip6->ip6_plen) { 704 /* 705 * IPv6 packets that have non 0 payload length 706 * must not contain a jumbo paylod option. 707 */ 708 ip6stat.ip6s_badoptions++; 709 icmp6_error(m, ICMP6_PARAM_PROB, 710 ICMP6_PARAMPROB_HEADER, 711 sizeof(struct ip6_hdr) + 712 sizeof(struct ip6_hbh) + 713 opt - opthead); 714 return(-1); 715 } 716 break; 717 default: /* unknown option */ 718 if (hbhlen < IP6OPT_MINLEN) { 719 ip6stat.ip6s_toosmall++; 720 goto bad; 721 } 722 if ((optlen = ip6_unknown_opt(opt, m, 723 sizeof(struct ip6_hdr) + 724 sizeof(struct ip6_hbh) + 725 opt - opthead)) == -1) 726 return(-1); 727 optlen += 2; 728 break; 729 } 730 } 731 732 return(0); 733 734 bad: 735 m_freem(m); 736 return(-1); 737} 738 739/* 740 * Unknown option processing. 741 * The third argument `off' is the offset from the IPv6 header to the option, 742 * which is necessary if the IPv6 header the and option header and IPv6 header 743 * is not continuous in order to return an ICMPv6 error. 744 */ 745int 746ip6_unknown_opt(optp, m, off) 747 u_int8_t *optp; 748 struct mbuf *m; 749 int off; 750{ 751 struct ip6_hdr *ip6; 752 753 switch(IP6OPT_TYPE(*optp)) { 754 case IP6OPT_TYPE_SKIP: /* ignore the option */ 755 return((int)*(optp + 1)); 756 case IP6OPT_TYPE_DISCARD: /* silently discard */ 757 m_freem(m); 758 return(-1); 759 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ 760 ip6stat.ip6s_badoptions++; 761 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); 762 return(-1); 763 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ 764 ip6stat.ip6s_badoptions++; 765 ip6 = mtod(m, struct ip6_hdr *); 766 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 767 (m->m_flags & (M_BCAST|M_MCAST))) 768 m_freem(m); 769 else 770 icmp6_error(m, ICMP6_PARAM_PROB, 771 ICMP6_PARAMPROB_OPTION, off); 772 return(-1); 773 } 774 775 m_freem(m); /* XXX: NOTREACHED */ 776 return(-1); 777} 778 779/* 780 * Create the "control" list for this pcb 781 */ 782void 783ip6_savecontrol(in6p, mp, ip6, m) 784 register struct inpcb *in6p; 785 register struct mbuf **mp; 786 register struct ip6_hdr *ip6; 787 register struct mbuf *m; 788{ 789 struct proc *p = curproc; /* XXX */ 790 int privileged; 791 792 privileged = 0; 793 if (p && !suser(p)) 794 privileged++; 795 796#ifdef SO_TIMESTAMP 797 if (in6p->in6p_socket->so_options & SO_TIMESTAMP) { 798 struct timeval tv; 799 800 microtime(&tv); 801 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 802 SCM_TIMESTAMP, SOL_SOCKET); 803 if (*mp) 804 mp = &(*mp)->m_next; 805 } 806#endif 807 if (in6p->in6p_flags & IN6P_RECVDSTADDR) { 808 *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst, 809 sizeof(struct in6_addr), IPV6_RECVDSTADDR, 810 IPPROTO_IPV6); 811 if (*mp) 812 mp = &(*mp)->m_next; 813 } 814 815 /* RFC 2292 sec. 5 */ 816 if (in6p->in6p_flags & IN6P_PKTINFO) { 817 struct in6_pktinfo pi6; 818 bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); 819 if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) 820 pi6.ipi6_addr.s6_addr16[1] = 0; 821 pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) 822 ? m->m_pkthdr.rcvif->if_index 823 : 0; 824 *mp = sbcreatecontrol((caddr_t) &pi6, 825 sizeof(struct in6_pktinfo), IPV6_PKTINFO, 826 IPPROTO_IPV6); 827 if (*mp) 828 mp = &(*mp)->m_next; 829 } 830 if (in6p->in6p_flags & IN6P_HOPLIMIT) { 831 int hlim = ip6->ip6_hlim & 0xff; 832 *mp = sbcreatecontrol((caddr_t) &hlim, 833 sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6); 834 if (*mp) 835 mp = &(*mp)->m_next; 836 } 837 /* IN6P_NEXTHOP - for outgoing packet only */ 838 839 /* 840 * IPV6_HOPOPTS socket option. We require super-user privilege 841 * for the option, but it might be too strict, since there might 842 * be some hop-by-hop options which can be returned to normal user. 843 * See RFC 2292 section 6. 844 */ 845 if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) { 846 /* 847 * Check if a hop-by-hop options header is contatined in the 848 * received packet, and if so, store the options as ancillary 849 * data. Note that a hop-by-hop options header must be 850 * just after the IPv6 header, which fact is assured through 851 * the IPv6 input processing. 852 */ 853 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 854 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { 855 struct ip6_hbh *hbh = (struct ip6_hbh *)(ip6 + 1); 856 857 /* 858 * XXX: We copy whole the header even if a jumbo 859 * payload option is included, which option is to 860 * be removed before returning in the RFC 2292. 861 * But it's too painful operation... 862 */ 863 *mp = sbcreatecontrol((caddr_t)hbh, 864 (hbh->ip6h_len + 1) << 3, 865 IPV6_HOPOPTS, IPPROTO_IPV6); 866 if (*mp) 867 mp = &(*mp)->m_next; 868 } 869 } 870 871 /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ 872 if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) { 873 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 874 int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);; 875 876 /* 877 * Search for destination options headers or routing 878 * header(s) through the header chain, and stores each 879 * header as ancillary data. 880 * Note that the order of the headers remains in 881 * the chain of ancillary data. 882 */ 883 while(1) { /* is explicit loop prevention necessary? */ 884 struct ip6_ext *ip6e = 885 (struct ip6_ext *)(mtod(m, caddr_t) + off); 886 887 switch(nxt) { 888 case IPPROTO_DSTOPTS: 889 if (!in6p->in6p_flags & IN6P_DSTOPTS) 890 break; 891 892 /* 893 * We also require super-user privilege for 894 * the option. 895 * See the comments on IN6_HOPOPTS. 896 */ 897 if (!privileged) 898 break; 899 900 *mp = sbcreatecontrol((caddr_t)ip6e, 901 (ip6e->ip6e_len + 1) << 3, 902 IPV6_DSTOPTS, 903 IPPROTO_IPV6); 904 if (*mp) 905 mp = &(*mp)->m_next; 906 break; 907 908 case IPPROTO_ROUTING: 909 if (!in6p->in6p_flags & IN6P_RTHDR) 910 break; 911 912 *mp = sbcreatecontrol((caddr_t)ip6e, 913 (ip6e->ip6e_len + 1) << 3, 914 IPV6_RTHDR, 915 IPPROTO_IPV6); 916 if (*mp) 917 mp = &(*mp)->m_next; 918 break; 919 920 case IPPROTO_UDP: 921 case IPPROTO_TCP: 922 case IPPROTO_ICMPV6: 923 default: 924 /* 925 * stop search if we encounter an upper 926 * layer protocol headers. 927 */ 928 goto loopend; 929 930 case IPPROTO_HOPOPTS: 931 case IPPROTO_AH: /* is it possible? */ 932 break; 933 } 934 935 /* proceed with the next header. */ 936 if (nxt == IPPROTO_AH) 937 off += (ip6e->ip6e_len + 2) << 2; 938 else 939 off += (ip6e->ip6e_len + 1) << 3; 940 nxt = ip6e->ip6e_nxt; 941 } 942 loopend: 943 } 944 if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) { 945 /* to be done */ 946 } 947 if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) { 948 /* to be done */ 949 } 950 /* IN6P_RTHDR - to be done */ 951} 952 953/* 954 * Get pointer to the previous header followed by the header 955 * currently processed. 956 * XXX: This function supposes that 957 * M includes all headers, 958 * the next header field and the header length field of each header 959 * are valid, and 960 * the sum of each header length equals to OFF. 961 * Because of these assumptions, this function must be called very 962 * carefully. Moreover, it will not be used in the near future when 963 * we develop `neater' mechanism to process extension headers. 964 */ 965char * 966ip6_get_prevhdr(m, off) 967 struct mbuf *m; 968 int off; 969{ 970 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 971 972 if (off == sizeof(struct ip6_hdr)) 973 return(&ip6->ip6_nxt); 974 else { 975 int len, nxt; 976 struct ip6_ext *ip6e = NULL; 977 978 nxt = ip6->ip6_nxt; 979 len = sizeof(struct ip6_hdr); 980 while (len < off) { 981 ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); 982 983 switch(nxt) { 984 case IPPROTO_FRAGMENT: 985 len += sizeof(struct ip6_frag); 986 break; 987 case IPPROTO_AH: 988 len += (ip6e->ip6e_len + 2) << 2; 989 break; 990 default: 991 len += (ip6e->ip6e_len + 1) << 3; 992 break; 993 } 994 nxt = ip6e->ip6e_nxt; 995 } 996 if (ip6e) 997 return(&ip6e->ip6e_nxt); 998 else 999 return NULL; 1000 } 1001} 1002 1003/* 1004 * System control for IP6 1005 */ 1006 1007u_char inet6ctlerrmap[PRC_NCMDS] = { 1008 0, 0, 0, 0, 1009 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1010 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1011 EMSGSIZE, EHOSTUNREACH, 0, 0, 1012 0, 0, 0, 0, 1013 ENOPROTOOPT 1014}; 1015