1 /* $NetBSD: raw_ip6.c,v 1.74 2005/12/11 12:25:02 christos Exp $ */ 2 /* $KAME: raw_ip6.c,v 1.82 2001/07/23 18:57:56 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. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.74 2005/12/11 12:25:02 christos Exp $"); 66 67 #include "opt_ipsec.h" 68 69 #include <sys/param.h> 70 #include <sys/sysctl.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/socket.h> 74 #include <sys/protosw.h> 75 #include <sys/socketvar.h> 76 #include <sys/errno.h> 77 #include <sys/systm.h> 78 #include <sys/proc.h> 79 80 #include <net/if.h> 81 #include <net/route.h> 82 #include <net/if_types.h> 83 84 #include <netinet/in.h> 85 #include <netinet/in_var.h> 86 #include <netinet/ip6.h> 87 #include <netinet6/ip6_var.h> 88 #include <netinet6/ip6_mroute.h> 89 #include <netinet/icmp6.h> 90 #include <netinet6/in6_pcb.h> 91 #include <netinet6/nd6.h> 92 #include <netinet6/ip6protosw.h> 93 #ifdef ENABLE_DEFAULT_SCOPE 94 #include <netinet6/scope6_var.h> 95 #endif 96 #include <netinet6/raw_ip6.h> 97 98 #ifdef IPSEC 99 #include <netinet6/ipsec.h> 100 #endif /* IPSEC */ 101 102 #include <machine/stdarg.h> 103 104 #include "faith.h" 105 #if defined(NFAITH) && 0 < NFAITH 106 #include <net/if_faith.h> 107 #endif 108 109 extern struct inpcbtable rawcbtable; 110 struct inpcbtable raw6cbtable; 111 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 112 113 /* 114 * Raw interface to IP6 protocol. 115 */ 116 117 struct rip6stat rip6stat; 118 119 /* 120 * Initialize raw connection block queue. 121 */ 122 void 123 rip6_init() 124 { 125 126 in6_pcbinit(&raw6cbtable, 1, 1); 127 } 128 129 /* 130 * Setup generic address and protocol structures 131 * for raw_input routine, then pass them along with 132 * mbuf chain. 133 */ 134 int 135 rip6_input(mp, offp, proto) 136 struct mbuf **mp; 137 int *offp, proto; 138 { 139 struct mbuf *m = *mp; 140 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 141 struct inpcb_hdr *inph; 142 struct in6pcb *in6p; 143 struct in6pcb *last = NULL; 144 struct sockaddr_in6 rip6src; 145 struct mbuf *opts = NULL; 146 147 rip6stat.rip6s_ipackets++; 148 149 #if defined(NFAITH) && 0 < NFAITH 150 if (faithprefix(&ip6->ip6_dst)) { 151 /* send icmp6 host unreach? */ 152 m_freem(m); 153 return IPPROTO_DONE; 154 } 155 #endif 156 157 /* Be proactive about malicious use of IPv4 mapped address */ 158 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 159 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 160 /* XXX stat */ 161 m_freem(m); 162 return IPPROTO_DONE; 163 } 164 165 bzero(&rip6src, sizeof(rip6src)); 166 rip6src.sin6_len = sizeof(struct sockaddr_in6); 167 rip6src.sin6_family = AF_INET6; 168 /* KAME hack: recover scopeid */ 169 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif); 170 171 CIRCLEQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) { 172 in6p = (struct in6pcb *)inph; 173 if (in6p->in6p_af != AF_INET6) 174 continue; 175 if (in6p->in6p_ip6.ip6_nxt && 176 in6p->in6p_ip6.ip6_nxt != proto) 177 continue; 178 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 179 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 180 continue; 181 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 182 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 183 continue; 184 if (in6p->in6p_cksum != -1) { 185 rip6stat.rip6s_isum++; 186 if (in6_cksum(m, proto, *offp, 187 m->m_pkthdr.len - *offp)) { 188 rip6stat.rip6s_badsum++; 189 continue; 190 } 191 } 192 if (last) { 193 struct mbuf *n; 194 195 #ifdef IPSEC 196 /* 197 * Check AH/ESP integrity. 198 */ 199 if (ipsec6_in_reject(m, last)) { 200 ipsec6stat.in_polvio++; 201 /* do not inject data into pcb */ 202 } else 203 #endif /* IPSEC */ 204 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 205 if (last->in6p_flags & IN6P_CONTROLOPTS) 206 ip6_savecontrol(last, &opts, ip6, n); 207 /* strip intermediate headers */ 208 m_adj(n, *offp); 209 if (sbappendaddr(&last->in6p_socket->so_rcv, 210 (struct sockaddr *)&rip6src, n, opts) == 0) { 211 /* should notify about lost packet */ 212 m_freem(n); 213 if (opts) 214 m_freem(opts); 215 rip6stat.rip6s_fullsock++; 216 } else 217 sorwakeup(last->in6p_socket); 218 opts = NULL; 219 } 220 } 221 last = in6p; 222 } 223 #ifdef IPSEC 224 /* 225 * Check AH/ESP integrity. 226 */ 227 if (last && ipsec6_in_reject(m, last)) { 228 m_freem(m); 229 ipsec6stat.in_polvio++; 230 ip6stat.ip6s_delivered--; 231 /* do not inject data into pcb */ 232 } else 233 #endif /* IPSEC */ 234 if (last) { 235 if (last->in6p_flags & IN6P_CONTROLOPTS) 236 ip6_savecontrol(last, &opts, ip6, m); 237 /* strip intermediate headers */ 238 m_adj(m, *offp); 239 if (sbappendaddr(&last->in6p_socket->so_rcv, 240 (struct sockaddr *)&rip6src, m, opts) == 0) { 241 m_freem(m); 242 if (opts) 243 m_freem(opts); 244 rip6stat.rip6s_fullsock++; 245 } else 246 sorwakeup(last->in6p_socket); 247 } else { 248 rip6stat.rip6s_nosock++; 249 if (m->m_flags & M_MCAST) 250 rip6stat.rip6s_nosockmcast++; 251 if (proto == IPPROTO_NONE) 252 m_freem(m); 253 else { 254 u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 255 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown); 256 icmp6_error(m, ICMP6_PARAM_PROB, 257 ICMP6_PARAMPROB_NEXTHEADER, 258 prvnxtp - mtod(m, u_int8_t *)); 259 } 260 ip6stat.ip6s_delivered--; 261 } 262 return IPPROTO_DONE; 263 } 264 265 void 266 rip6_ctlinput(cmd, sa, d) 267 int cmd; 268 struct sockaddr *sa; 269 void *d; 270 { 271 struct ip6_hdr *ip6; 272 struct ip6ctlparam *ip6cp = NULL; 273 const struct sockaddr_in6 *sa6_src = NULL; 274 void *cmdarg; 275 void (*notify) __P((struct in6pcb *, int)) = in6_rtchange; 276 int nxt; 277 278 if (sa->sa_family != AF_INET6 || 279 sa->sa_len != sizeof(struct sockaddr_in6)) 280 return; 281 282 if ((unsigned)cmd >= PRC_NCMDS) 283 return; 284 if (PRC_IS_REDIRECT(cmd)) 285 notify = in6_rtchange, d = NULL; 286 else if (cmd == PRC_HOSTDEAD) 287 d = NULL; 288 else if (cmd == PRC_MSGSIZE) 289 ; /* special code is present, see below */ 290 else if (inet6ctlerrmap[cmd] == 0) 291 return; 292 293 /* if the parameter is from icmp6, decode it. */ 294 if (d != NULL) { 295 ip6cp = (struct ip6ctlparam *)d; 296 ip6 = ip6cp->ip6c_ip6; 297 cmdarg = ip6cp->ip6c_cmdarg; 298 sa6_src = ip6cp->ip6c_src; 299 nxt = ip6cp->ip6c_nxt; 300 } else { 301 ip6 = NULL; 302 cmdarg = NULL; 303 sa6_src = &sa6_any; 304 nxt = -1; 305 } 306 307 if (ip6 && cmd == PRC_MSGSIZE) { 308 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 309 int valid = 0; 310 struct in6pcb *in6p; 311 312 /* 313 * Check to see if we have a valid raw IPv6 socket 314 * corresponding to the address in the ICMPv6 message 315 * payload, and the protocol (ip6_nxt) meets the socket. 316 * XXX chase extension headers, or pass final nxt value 317 * from icmp6_notify_error() 318 */ 319 in6p = NULL; 320 in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0, 321 (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0); 322 #if 0 323 if (!in6p) { 324 /* 325 * As the use of sendto(2) is fairly popular, 326 * we may want to allow non-connected pcb too. 327 * But it could be too weak against attacks... 328 * We should at least check if the local 329 * address (= s) is really ours. 330 */ 331 in6p = in6_pcblookup_bind(&raw6cbtable, 332 &sa6->sin6_addr, 0, 0)) 333 } 334 #endif 335 336 if (in6p && in6p->in6p_ip6.ip6_nxt && 337 in6p->in6p_ip6.ip6_nxt == nxt) 338 valid++; 339 340 /* 341 * Depending on the value of "valid" and routing table 342 * size (mtudisc_{hi,lo}wat), we will: 343 * - recalculate the new MTU and create the 344 * corresponding routing entry, or 345 * - ignore the MTU change notification. 346 */ 347 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 348 349 /* 350 * regardless of if we called icmp6_mtudisc_update(), 351 * we need to call in6_pcbnotify(), to notify path 352 * MTU change to the userland (2292bis-02), because 353 * some unconnected sockets may share the same 354 * destination and want to know the path MTU. 355 */ 356 } 357 358 (void) in6_pcbnotify(&raw6cbtable, sa, 0, 359 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 360 } 361 362 /* 363 * Generate IPv6 header and pass packet to ip6_output. 364 * Tack on options user may have setup with control call. 365 */ 366 int 367 #if __STDC__ 368 rip6_output(struct mbuf *m, ...) 369 #else 370 rip6_output(m, va_alist) 371 struct mbuf *m; 372 va_dcl 373 #endif 374 { 375 struct socket *so; 376 struct sockaddr_in6 *dstsock; 377 struct mbuf *control; 378 struct in6_addr *dst; 379 struct ip6_hdr *ip6; 380 struct in6pcb *in6p; 381 u_int plen = m->m_pkthdr.len; 382 int error = 0; 383 struct ip6_pktopts opt, *optp = NULL, *origoptp; 384 struct ifnet *oifp = NULL; 385 int type, code; /* for ICMPv6 output statistics only */ 386 int priv = 0; 387 va_list ap; 388 int flags; 389 390 va_start(ap, m); 391 so = va_arg(ap, struct socket *); 392 dstsock = va_arg(ap, struct sockaddr_in6 *); 393 control = va_arg(ap, struct mbuf *); 394 va_end(ap); 395 396 in6p = sotoin6pcb(so); 397 398 priv = 0; 399 { 400 struct proc *p = curproc; /* XXX */ 401 402 if (p && !suser(p->p_ucred, &p->p_acflag)) 403 priv = 1; 404 } 405 dst = &dstsock->sin6_addr; 406 if (control) { 407 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0) 408 goto bad; 409 optp = &opt; 410 } else 411 optp = in6p->in6p_outputopts; 412 413 /* 414 * For an ICMPv6 packet, we should know its type and code 415 * to update statistics. 416 */ 417 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 418 struct icmp6_hdr *icmp6; 419 if (m->m_len < sizeof(struct icmp6_hdr) && 420 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 421 error = ENOBUFS; 422 goto bad; 423 } 424 icmp6 = mtod(m, struct icmp6_hdr *); 425 type = icmp6->icmp6_type; 426 code = icmp6->icmp6_code; 427 } else { 428 type = 0; 429 code = 0; 430 } 431 432 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 433 if (!m) { 434 error = ENOBUFS; 435 goto bad; 436 } 437 ip6 = mtod(m, struct ip6_hdr *); 438 439 /* 440 * Next header might not be ICMP6 but use its pseudo header anyway. 441 */ 442 ip6->ip6_dst = *dst; 443 444 /* KAME hack: embed scopeid */ 445 origoptp = in6p->in6p_outputopts; 446 in6p->in6p_outputopts = optp; 447 if (in6_embedscope(&ip6->ip6_dst, dstsock, in6p, &oifp) != 0) { 448 error = EINVAL; 449 goto bad; 450 } 451 in6p->in6p_outputopts = origoptp; 452 453 /* 454 * Source address selection. 455 */ 456 { 457 struct in6_addr *in6a; 458 459 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, 460 &in6p->in6p_route, &in6p->in6p_laddr, &error)) == 0) { 461 if (error == 0) 462 error = EADDRNOTAVAIL; 463 goto bad; 464 } 465 ip6->ip6_src = *in6a; 466 if (in6p->in6p_route.ro_rt) { 467 /* what if oifp contradicts ? */ 468 oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index]; 469 } 470 } 471 472 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 473 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 474 ip6->ip6_vfc |= IPV6_VERSION; 475 #if 0 /* ip6_plen will be filled in ip6_output. */ 476 ip6->ip6_plen = htons((u_int16_t)plen); 477 #endif 478 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt; 479 ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 480 481 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 482 in6p->in6p_cksum != -1) { 483 int off; 484 u_int16_t sum; 485 486 /* compute checksum */ 487 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 488 off = offsetof(struct icmp6_hdr, icmp6_cksum); 489 else 490 off = in6p->in6p_cksum; 491 if (plen < off + 1) { 492 error = EINVAL; 493 goto bad; 494 } 495 off += sizeof(struct ip6_hdr); 496 497 sum = 0; 498 m = m_copyback_cow(m, off, sizeof(sum), (caddr_t)&sum, 499 M_DONTWAIT); 500 if (m == NULL) { 501 error = ENOBUFS; 502 goto bad; 503 } 504 sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 505 m = m_copyback_cow(m, off, sizeof(sum), (caddr_t)&sum, 506 M_DONTWAIT); 507 if (m == NULL) { 508 error = ENOBUFS; 509 goto bad; 510 } 511 } 512 513 flags = 0; 514 if (in6p->in6p_flags & IN6P_MINMTU) 515 flags |= IPV6_MINMTU; 516 517 error = ip6_output(m, optp, &in6p->in6p_route, flags, 518 in6p->in6p_moptions, so, &oifp); 519 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 520 if (oifp) 521 icmp6_ifoutstat_inc(oifp, type, code); 522 icmp6stat.icp6s_outhist[type]++; 523 } else 524 rip6stat.rip6s_opackets++; 525 526 goto freectl; 527 528 bad: 529 if (m) 530 m_freem(m); 531 532 freectl: 533 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt) 534 RTFREE(optp->ip6po_route.ro_rt); 535 if (control) 536 m_freem(control); 537 return (error); 538 } 539 540 /* 541 * Raw IPv6 socket option processing. 542 */ 543 int 544 rip6_ctloutput(op, so, level, optname, mp) 545 int op; 546 struct socket *so; 547 int level, optname; 548 struct mbuf **mp; 549 { 550 int error = 0; 551 552 switch (level) { 553 case IPPROTO_IPV6: 554 switch (optname) { 555 case MRT6_INIT: 556 case MRT6_DONE: 557 case MRT6_ADD_MIF: 558 case MRT6_DEL_MIF: 559 case MRT6_ADD_MFC: 560 case MRT6_DEL_MFC: 561 case MRT6_PIM: 562 if (op == PRCO_SETOPT) { 563 error = ip6_mrouter_set(optname, so, *mp); 564 if (*mp) 565 (void)m_free(*mp); 566 } else if (op == PRCO_GETOPT) 567 error = ip6_mrouter_get(optname, so, mp); 568 else 569 error = EINVAL; 570 return (error); 571 case IPV6_CHECKSUM: 572 return (ip6_raw_ctloutput(op, so, level, optname, mp)); 573 default: 574 return (ip6_ctloutput(op, so, level, optname, mp)); 575 } 576 577 case IPPROTO_ICMPV6: 578 /* 579 * XXX: is it better to call icmp6_ctloutput() directly 580 * from protosw? 581 */ 582 return (icmp6_ctloutput(op, so, level, optname, mp)); 583 584 default: 585 if (op == PRCO_SETOPT && *mp) 586 m_free(*mp); 587 return EINVAL; 588 } 589 } 590 591 extern u_long rip6_sendspace; 592 extern u_long rip6_recvspace; 593 594 int 595 rip6_usrreq(so, req, m, nam, control, l) 596 struct socket *so; 597 int req; 598 struct mbuf *m, *nam, *control; 599 struct lwp *l; 600 { 601 struct in6pcb *in6p = sotoin6pcb(so); 602 struct proc *p; 603 int s; 604 int error = 0; 605 int priv; 606 607 priv = 0; 608 p = l ? l->l_proc : NULL; 609 if (p && !suser(p->p_ucred, &p->p_acflag)) 610 priv++; 611 612 if (req == PRU_CONTROL) 613 return (in6_control(so, (u_long)m, (caddr_t)nam, 614 (struct ifnet *)control, p)); 615 616 if (req == PRU_PURGEIF) { 617 in6_pcbpurgeif0(&raw6cbtable, (struct ifnet *)control); 618 in6_purgeif((struct ifnet *)control); 619 in6_pcbpurgeif(&raw6cbtable, (struct ifnet *)control); 620 return (0); 621 } 622 623 switch (req) { 624 case PRU_ATTACH: 625 if (in6p) 626 panic("rip6_attach"); 627 if (!priv) { 628 error = EACCES; 629 break; 630 } 631 s = splsoftnet(); 632 if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) { 633 splx(s); 634 break; 635 } 636 if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) 637 { 638 splx(s); 639 break; 640 } 641 splx(s); 642 in6p = sotoin6pcb(so); 643 in6p->in6p_ip6.ip6_nxt = (long)nam; 644 in6p->in6p_cksum = -1; 645 646 MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *, 647 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 648 if (in6p->in6p_icmp6filt == NULL) { 649 in6_pcbdetach(in6p); 650 error = ENOMEM; 651 break; 652 } 653 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt); 654 break; 655 656 case PRU_DISCONNECT: 657 if ((so->so_state & SS_ISCONNECTED) == 0) { 658 error = ENOTCONN; 659 break; 660 } 661 in6p->in6p_faddr = in6addr_any; 662 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 663 break; 664 665 case PRU_ABORT: 666 soisdisconnected(so); 667 /* Fallthrough */ 668 case PRU_DETACH: 669 if (in6p == 0) 670 panic("rip6_detach"); 671 if (so == ip6_mrouter) 672 ip6_mrouter_done(); 673 /* xxx: RSVP */ 674 if (in6p->in6p_icmp6filt) { 675 FREE(in6p->in6p_icmp6filt, M_PCB); 676 in6p->in6p_icmp6filt = NULL; 677 } 678 in6_pcbdetach(in6p); 679 break; 680 681 case PRU_BIND: 682 { 683 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *); 684 struct ifaddr *ia = NULL; 685 686 if (nam->m_len != sizeof(*addr)) { 687 error = EINVAL; 688 break; 689 } 690 if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) { 691 error = EADDRNOTAVAIL; 692 break; 693 } 694 #ifdef ENABLE_DEFAULT_SCOPE 695 if (addr->sin6_scope_id == 0) /* not change if specified */ 696 addr->sin6_scope_id = 697 scope6_addr2default(&addr->sin6_addr); 698 #endif 699 /* KAME hack: embed scopeid */ 700 if (in6_embedscope(&addr->sin6_addr, addr, in6p, NULL) != 0) 701 return EINVAL; 702 #ifndef SCOPEDROUTING 703 addr->sin6_scope_id = 0; /* for ifa_ifwithaddr */ 704 #endif 705 706 /* 707 * we don't support mapped address here, it would confuse 708 * users so reject it 709 */ 710 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) { 711 error = EADDRNOTAVAIL; 712 break; 713 } 714 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 715 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) { 716 error = EADDRNOTAVAIL; 717 break; 718 } 719 if (ia && ((struct in6_ifaddr *)ia)->ia6_flags & 720 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 721 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 722 error = EADDRNOTAVAIL; 723 break; 724 } 725 in6p->in6p_laddr = addr->sin6_addr; 726 break; 727 } 728 729 case PRU_CONNECT: 730 { 731 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *); 732 struct in6_addr *in6a = NULL; 733 #ifdef ENABLE_DEFAULT_SCOPE 734 struct sockaddr_in6 sin6; 735 #endif 736 737 if (nam->m_len != sizeof(*addr)) { 738 error = EINVAL; 739 break; 740 } 741 if (ifnet.tqh_first == 0) 742 { 743 error = EADDRNOTAVAIL; 744 break; 745 } 746 if (addr->sin6_family != AF_INET6) { 747 error = EAFNOSUPPORT; 748 break; 749 } 750 751 #ifdef ENABLE_DEFAULT_SCOPE 752 if (addr->sin6_scope_id == 0) { 753 /* protect *addr */ 754 sin6 = *addr; 755 addr = &sin6; 756 addr->sin6_scope_id = 757 scope6_addr2default(&addr->sin6_addr); 758 } 759 #endif 760 761 /* Source address selection. XXX: need pcblookup? */ 762 in6a = in6_selectsrc(addr, in6p->in6p_outputopts, 763 in6p->in6p_moptions, &in6p->in6p_route, 764 &in6p->in6p_laddr, &error); 765 if (in6a == NULL) { 766 if (error == 0) 767 error = EADDRNOTAVAIL; 768 break; 769 } 770 in6p->in6p_laddr = *in6a; 771 in6p->in6p_faddr = addr->sin6_addr; 772 soisconnected(so); 773 break; 774 } 775 776 case PRU_CONNECT2: 777 error = EOPNOTSUPP; 778 break; 779 780 /* 781 * Mark the connection as being incapable of futther input. 782 */ 783 case PRU_SHUTDOWN: 784 socantsendmore(so); 785 break; 786 /* 787 * Ship a packet out. The appropriate raw output 788 * routine handles any messaging necessary. 789 */ 790 case PRU_SEND: 791 { 792 struct sockaddr_in6 tmp; 793 struct sockaddr_in6 *dst; 794 795 /* always copy sockaddr to avoid overwrites */ 796 if (so->so_state & SS_ISCONNECTED) { 797 if (nam) { 798 error = EISCONN; 799 break; 800 } 801 /* XXX */ 802 bzero(&tmp, sizeof(tmp)); 803 tmp.sin6_family = AF_INET6; 804 tmp.sin6_len = sizeof(struct sockaddr_in6); 805 bcopy(&in6p->in6p_faddr, &tmp.sin6_addr, 806 sizeof(struct in6_addr)); 807 dst = &tmp; 808 } else { 809 if (nam == NULL) { 810 error = ENOTCONN; 811 break; 812 } 813 if (nam->m_len != sizeof(tmp)) { 814 error = EINVAL; 815 break; 816 } 817 818 tmp = *mtod(nam, struct sockaddr_in6 *); 819 dst = &tmp; 820 821 if (dst->sin6_family != AF_INET6) { 822 error = EAFNOSUPPORT; 823 break; 824 } 825 } 826 #ifdef ENABLE_DEFAULT_SCOPE 827 if (dst->sin6_scope_id == 0) { 828 dst->sin6_scope_id = 829 scope6_addr2default(&dst->sin6_addr); 830 } 831 #endif 832 error = rip6_output(m, so, dst, control); 833 m = NULL; 834 break; 835 } 836 837 case PRU_SENSE: 838 /* 839 * stat: don't bother with a blocksize 840 */ 841 return (0); 842 /* 843 * Not supported. 844 */ 845 case PRU_RCVOOB: 846 case PRU_RCVD: 847 case PRU_LISTEN: 848 case PRU_ACCEPT: 849 case PRU_SENDOOB: 850 error = EOPNOTSUPP; 851 break; 852 853 case PRU_SOCKADDR: 854 in6_setsockaddr(in6p, nam); 855 break; 856 857 case PRU_PEERADDR: 858 in6_setpeeraddr(in6p, nam); 859 break; 860 861 default: 862 panic("rip6_usrreq"); 863 } 864 if (m != NULL) 865 m_freem(m); 866 return (error); 867 } 868 869 SYSCTL_SETUP(sysctl_net_inet6_raw6_setup, "sysctl net.inet6.raw6 subtree setup") 870 { 871 872 sysctl_createv(clog, 0, NULL, NULL, 873 CTLFLAG_PERMANENT, 874 CTLTYPE_NODE, "net", NULL, 875 NULL, 0, NULL, 0, 876 CTL_NET, CTL_EOL); 877 sysctl_createv(clog, 0, NULL, NULL, 878 CTLFLAG_PERMANENT, 879 CTLTYPE_NODE, "inet6", NULL, 880 NULL, 0, NULL, 0, 881 CTL_NET, PF_INET6, CTL_EOL); 882 sysctl_createv(clog, 0, NULL, NULL, 883 CTLFLAG_PERMANENT, 884 CTLTYPE_NODE, "raw6", 885 SYSCTL_DESCR("Raw IPv6 settings"), 886 NULL, 0, NULL, 0, 887 CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL); 888 889 sysctl_createv(clog, 0, NULL, NULL, 890 CTLFLAG_PERMANENT, 891 CTLTYPE_STRUCT, "pcblist", 892 SYSCTL_DESCR("Raw IPv6 control block list"), 893 sysctl_inpcblist, 0, &raw6cbtable, 0, 894 CTL_NET, PF_INET6, IPPROTO_RAW, 895 CTL_CREATE, CTL_EOL); 896 sysctl_createv(clog, 0, NULL, NULL, 897 CTLFLAG_PERMANENT, 898 CTLTYPE_STRUCT, "stats", 899 SYSCTL_DESCR("Raw IPv6 statistics"), 900 NULL, 0, &rip6stat, sizeof(rip6stat), 901 CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS, 902 CTL_EOL); 903 } 904