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