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