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