1 /* $OpenBSD: raw_ip6.c,v 1.180 2024/02/03 22:50:09 mvs 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 "pf.h" 65 66 #include <sys/param.h> 67 #include <sys/malloc.h> 68 #include <sys/mbuf.h> 69 #include <sys/socket.h> 70 #include <sys/protosw.h> 71 #include <sys/socketvar.h> 72 #include <sys/errno.h> 73 #include <sys/systm.h> 74 #include <sys/sysctl.h> 75 76 #include <net/if.h> 77 #include <net/if_var.h> 78 #include <net/route.h> 79 80 #include <netinet/in.h> 81 #include <netinet6/in6_var.h> 82 #include <netinet/ip6.h> 83 #include <netinet6/ip6_var.h> 84 #ifdef MROUTING 85 #include <netinet6/ip6_mroute.h> 86 #endif 87 #include <netinet/icmp6.h> 88 #include <netinet/ip.h> 89 #include <netinet/in_pcb.h> 90 #include <netinet6/nd6.h> 91 #include <netinet6/ip6protosw.h> 92 #include <netinet6/raw_ip6.h> 93 94 #if NPF > 0 95 #include <net/pfvar.h> 96 #endif 97 98 #include <sys/stdarg.h> 99 100 /* 101 * Raw interface to IP6 protocol. 102 */ 103 104 struct inpcbtable rawin6pcbtable; 105 106 struct cpumem *rip6counters; 107 108 const struct pr_usrreqs rip6_usrreqs = { 109 .pru_attach = rip6_attach, 110 .pru_detach = rip6_detach, 111 .pru_lock = rip6_lock, 112 .pru_unlock = rip6_unlock, 113 .pru_locked = rip6_locked, 114 .pru_bind = rip6_bind, 115 .pru_connect = rip6_connect, 116 .pru_disconnect = rip6_disconnect, 117 .pru_shutdown = rip6_shutdown, 118 .pru_send = rip6_send, 119 .pru_control = in6_control, 120 .pru_sockaddr = in6_sockaddr, 121 .pru_peeraddr = in6_peeraddr, 122 }; 123 124 /* 125 * Initialize raw connection block queue. 126 */ 127 void 128 rip6_init(void) 129 { 130 in_pcbinit(&rawin6pcbtable, 1); 131 rip6counters = counters_alloc(rip6s_ncounters); 132 } 133 134 int 135 rip6_input(struct mbuf **mp, int *offp, int proto, int af) 136 { 137 struct mbuf *m = *mp; 138 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 139 struct inpcb *inp; 140 SIMPLEQ_HEAD(, inpcb) inpcblist; 141 struct in6_addr *key; 142 struct sockaddr_in6 rip6src; 143 uint8_t type; 144 145 KASSERT(af == AF_INET6); 146 147 if (proto == IPPROTO_ICMPV6) { 148 struct icmp6_hdr *icmp6; 149 150 IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, *offp, 151 sizeof(*icmp6)); 152 if (icmp6 == NULL) 153 return IPPROTO_DONE; 154 type = icmp6->icmp6_type; 155 } else 156 rip6stat_inc(rip6s_ipackets); 157 158 bzero(&rip6src, sizeof(rip6src)); 159 rip6src.sin6_len = sizeof(struct sockaddr_in6); 160 rip6src.sin6_family = AF_INET6; 161 /* KAME hack: recover scopeid */ 162 in6_recoverscope(&rip6src, &ip6->ip6_src); 163 164 key = &ip6->ip6_dst; 165 #if NPF > 0 166 if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { 167 struct pf_divert *divert; 168 169 divert = pf_find_divert(m); 170 KASSERT(divert != NULL); 171 switch (divert->type) { 172 case PF_DIVERT_TO: 173 key = &divert->addr.v6; 174 break; 175 case PF_DIVERT_REPLY: 176 break; 177 default: 178 panic("%s: unknown divert type %d, mbuf %p, divert %p", 179 __func__, divert->type, m, divert); 180 } 181 } 182 #endif 183 SIMPLEQ_INIT(&inpcblist); 184 rw_enter_write(&rawin6pcbtable.inpt_notify); 185 mtx_enter(&rawin6pcbtable.inpt_mtx); 186 TAILQ_FOREACH(inp, &rawin6pcbtable.inpt_queue, inp_queue) { 187 KASSERT(ISSET(inp->inp_flags, INP_IPV6)); 188 189 if (inp->inp_socket->so_rcv.sb_state & SS_CANTRCVMORE) 190 continue; 191 if (rtable_l2(inp->inp_rtableid) != 192 rtable_l2(m->m_pkthdr.ph_rtableid)) 193 continue; 194 195 if ((inp->inp_ipv6.ip6_nxt || proto == IPPROTO_ICMPV6) && 196 inp->inp_ipv6.ip6_nxt != proto) 197 continue; 198 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) && 199 !IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key)) 200 continue; 201 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) && 202 !IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, &ip6->ip6_src)) 203 continue; 204 if (proto == IPPROTO_ICMPV6 && inp->inp_icmp6filt) { 205 if (ICMP6_FILTER_WILLBLOCK(type, inp->inp_icmp6filt)) 206 continue; 207 } 208 if (proto != IPPROTO_ICMPV6 && inp->inp_cksum6 != -1) { 209 rip6stat_inc(rip6s_isum); 210 /* 211 * Although in6_cksum() does not need the position of 212 * the checksum field for verification, enforce that it 213 * is located within the packet. Userland has given 214 * a checksum offset, a packet too short for that is 215 * invalid. Avoid overflow with user supplied offset. 216 */ 217 if (m->m_pkthdr.len < *offp + 2 || 218 m->m_pkthdr.len - *offp - 2 < inp->inp_cksum6 || 219 in6_cksum(m, proto, *offp, 220 m->m_pkthdr.len - *offp)) { 221 rip6stat_inc(rip6s_badsum); 222 continue; 223 } 224 } 225 226 in_pcbref(inp); 227 SIMPLEQ_INSERT_TAIL(&inpcblist, inp, inp_notify); 228 } 229 mtx_leave(&rawin6pcbtable.inpt_mtx); 230 231 if (SIMPLEQ_EMPTY(&inpcblist)) { 232 struct counters_ref ref; 233 uint64_t *counters; 234 235 rw_exit_write(&rawin6pcbtable.inpt_notify); 236 237 if (proto != IPPROTO_ICMPV6) { 238 rip6stat_inc(rip6s_nosock); 239 if (m->m_flags & M_MCAST) 240 rip6stat_inc(rip6s_nosockmcast); 241 } 242 if (proto == IPPROTO_NONE || proto == IPPROTO_ICMPV6) { 243 m_freem(m); 244 } else { 245 int prvnxt = ip6_get_prevhdr(m, *offp); 246 247 icmp6_error(m, ICMP6_PARAM_PROB, 248 ICMP6_PARAMPROB_NEXTHEADER, prvnxt); 249 } 250 counters = counters_enter(&ref, ip6counters); 251 counters[ip6s_delivered]--; 252 counters_leave(&ref, ip6counters); 253 254 return IPPROTO_DONE; 255 } 256 257 while ((inp = SIMPLEQ_FIRST(&inpcblist)) != NULL) { 258 struct mbuf *n, *opts = NULL; 259 260 SIMPLEQ_REMOVE_HEAD(&inpcblist, inp_notify); 261 if (SIMPLEQ_EMPTY(&inpcblist)) 262 n = m; 263 else 264 n = m_copym(m, 0, M_COPYALL, M_NOWAIT); 265 if (n != NULL) { 266 int ret; 267 268 if (inp->inp_flags & IN6P_CONTROLOPTS) 269 ip6_savecontrol(inp, n, &opts); 270 /* strip intermediate headers */ 271 m_adj(n, *offp); 272 273 mtx_enter(&inp->inp_mtx); 274 ret = sbappendaddr(inp->inp_socket, 275 &inp->inp_socket->so_rcv, 276 sin6tosa(&rip6src), n, opts); 277 mtx_leave(&inp->inp_mtx); 278 279 if (ret == 0) { 280 /* should notify about lost packet */ 281 m_freem(n); 282 m_freem(opts); 283 rip6stat_inc(rip6s_fullsock); 284 } else 285 sorwakeup(inp->inp_socket); 286 } 287 in_pcbunref(inp); 288 } 289 rw_exit_write(&rawin6pcbtable.inpt_notify); 290 291 return IPPROTO_DONE; 292 } 293 294 void 295 rip6_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *d) 296 { 297 struct ip6_hdr *ip6; 298 struct ip6ctlparam *ip6cp = NULL; 299 struct sockaddr_in6 *sa6 = satosin6(sa); 300 const struct sockaddr_in6 *sa6_src = NULL; 301 void *cmdarg; 302 void (*notify)(struct inpcb *, int) = in_rtchange; 303 int nxt; 304 305 if (sa->sa_family != AF_INET6 || 306 sa->sa_len != sizeof(struct sockaddr_in6)) 307 return; 308 309 if ((unsigned)cmd >= PRC_NCMDS) 310 return; 311 if (PRC_IS_REDIRECT(cmd)) 312 notify = in_rtchange, d = NULL; 313 else if (cmd == PRC_HOSTDEAD) 314 d = NULL; 315 else if (cmd == PRC_MSGSIZE) 316 ; /* special code is present, see below */ 317 else if (inet6ctlerrmap[cmd] == 0) 318 return; 319 320 /* if the parameter is from icmp6, decode it. */ 321 if (d != NULL) { 322 ip6cp = (struct ip6ctlparam *)d; 323 ip6 = ip6cp->ip6c_ip6; 324 cmdarg = ip6cp->ip6c_cmdarg; 325 sa6_src = ip6cp->ip6c_src; 326 nxt = ip6cp->ip6c_nxt; 327 } else { 328 ip6 = NULL; 329 cmdarg = NULL; 330 sa6_src = &sa6_any; 331 nxt = -1; 332 } 333 334 if (ip6 && cmd == PRC_MSGSIZE) { 335 int valid = 0; 336 struct inpcb *inp; 337 338 /* 339 * Check to see if we have a valid raw IPv6 socket 340 * corresponding to the address in the ICMPv6 message 341 * payload, and the protocol (ip6_nxt) meets the socket. 342 * XXX chase extension headers, or pass final nxt value 343 * from icmp6_notify_error() 344 */ 345 inp = in6_pcblookup(&rawin6pcbtable, &sa6->sin6_addr, 0, 346 &sa6_src->sin6_addr, 0, rdomain); 347 348 if (inp && inp->inp_ipv6.ip6_nxt && 349 inp->inp_ipv6.ip6_nxt == nxt) 350 valid = 1; 351 352 /* 353 * Depending on the value of "valid" and routing table 354 * size (mtudisc_{hi,lo}wat), we will: 355 * - recalculate the new MTU and create the 356 * corresponding routing entry, or 357 * - ignore the MTU change notification. 358 */ 359 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 360 in_pcbunref(inp); 361 362 /* 363 * regardless of if we called icmp6_mtudisc_update(), 364 * we need to call in6_pcbnotify(), to notify path 365 * MTU change to the userland (2292bis-02), because 366 * some unconnected sockets may share the same 367 * destination and want to know the path MTU. 368 */ 369 } 370 371 in6_pcbnotify(&rawin6pcbtable, sa6, 0, 372 sa6_src, 0, rdomain, cmd, cmdarg, notify); 373 } 374 375 /* 376 * Generate IPv6 header and pass packet to ip6_output. 377 * Tack on options user may have setup with control call. 378 */ 379 int 380 rip6_output(struct mbuf *m, struct socket *so, struct sockaddr *dstaddr, 381 struct mbuf *control) 382 { 383 struct in6_addr *dst; 384 struct ip6_hdr *ip6; 385 struct inpcb *inp; 386 u_int plen = m->m_pkthdr.len; 387 int error = 0; 388 struct ip6_pktopts opt, *optp = NULL; 389 int type; /* for ICMPv6 output statistics only */ 390 int priv = 0; 391 int flags; 392 393 inp = sotoinpcb(so); 394 395 priv = 0; 396 if ((so->so_state & SS_PRIV) != 0) 397 priv = 1; 398 if (control) { 399 if ((error = ip6_setpktopts(control, &opt, 400 inp->inp_outputopts6, 401 priv, so->so_proto->pr_protocol)) != 0) 402 goto bad; 403 optp = &opt; 404 } else 405 optp = inp->inp_outputopts6; 406 407 if (dstaddr->sa_family != AF_INET6) { 408 error = EAFNOSUPPORT; 409 goto bad; 410 } 411 dst = &satosin6(dstaddr)->sin6_addr; 412 if (IN6_IS_ADDR_V4MAPPED(dst)) { 413 error = EADDRNOTAVAIL; 414 goto bad; 415 } 416 417 /* 418 * For an ICMPv6 packet, we should know its type and code 419 * to update statistics. 420 */ 421 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 422 struct icmp6_hdr *icmp6; 423 if (m->m_len < sizeof(struct icmp6_hdr) && 424 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 425 error = ENOBUFS; 426 goto bad; 427 } 428 icmp6 = mtod(m, struct icmp6_hdr *); 429 type = icmp6->icmp6_type; 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 if (in6_embedscope(&ip6->ip6_dst, satosin6(dstaddr), 446 optp, inp->inp_moptions6) != 0) { 447 error = EINVAL; 448 goto bad; 449 } 450 451 /* 452 * Source address selection. 453 */ 454 { 455 const struct in6_addr *in6a; 456 457 error = in6_pcbselsrc(&in6a, satosin6(dstaddr), inp, optp); 458 if (error) 459 goto bad; 460 461 ip6->ip6_src = *in6a; 462 } 463 464 ip6->ip6_flow = inp->inp_flowinfo & IPV6_FLOWINFO_MASK; 465 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 466 ip6->ip6_vfc |= IPV6_VERSION; 467 #if 0 /* ip6_plen will be filled in ip6_output. */ 468 ip6->ip6_plen = htons((u_short)plen); 469 #endif 470 ip6->ip6_nxt = inp->inp_ipv6.ip6_nxt; 471 ip6->ip6_hlim = in6_selecthlim(inp); 472 473 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 474 inp->inp_cksum6 != -1) { 475 struct mbuf *n; 476 int off; 477 u_int16_t *sump; 478 int sumoff; 479 480 /* compute checksum */ 481 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 482 off = offsetof(struct icmp6_hdr, icmp6_cksum); 483 else 484 off = inp->inp_cksum6; 485 if (plen < 2 || plen - 2 < off) { 486 error = EINVAL; 487 goto bad; 488 } 489 off += sizeof(struct ip6_hdr); 490 491 n = m_pulldown(m, off, sizeof(*sump), &sumoff); 492 if (n == NULL) { 493 m = NULL; 494 error = ENOBUFS; 495 goto bad; 496 } 497 sump = (u_int16_t *)(mtod(n, caddr_t) + sumoff); 498 *sump = 0; 499 *sump = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 500 } 501 502 flags = 0; 503 if (inp->inp_flags & IN6P_MINMTU) 504 flags |= IPV6_MINMTU; 505 506 /* force routing table */ 507 m->m_pkthdr.ph_rtableid = inp->inp_rtableid; 508 509 #if NPF > 0 510 if (inp->inp_socket->so_state & SS_ISCONNECTED && 511 so->so_proto->pr_protocol != IPPROTO_ICMPV6) 512 pf_mbuf_link_inpcb(m, inp); 513 #endif 514 515 error = ip6_output(m, optp, &inp->inp_route6, flags, 516 inp->inp_moptions6, inp->inp_seclevel); 517 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 518 icmp6stat_inc(icp6s_outhist + type); 519 } else 520 rip6stat_inc(rip6s_opackets); 521 522 goto freectl; 523 524 bad: 525 m_freem(m); 526 527 freectl: 528 if (control) { 529 ip6_clearpktopts(&opt, -1); 530 m_freem(control); 531 } 532 return (error); 533 } 534 535 /* 536 * Raw IPv6 socket option processing. 537 */ 538 int 539 rip6_ctloutput(int op, struct socket *so, int level, int optname, 540 struct mbuf *m) 541 { 542 #ifdef MROUTING 543 int error; 544 #endif 545 546 switch (level) { 547 case IPPROTO_IPV6: 548 switch (optname) { 549 #ifdef MROUTING 550 case MRT6_INIT: 551 case MRT6_DONE: 552 case MRT6_ADD_MIF: 553 case MRT6_DEL_MIF: 554 case MRT6_ADD_MFC: 555 case MRT6_DEL_MFC: 556 if (op == PRCO_SETOPT) { 557 error = ip6_mrouter_set(optname, so, m); 558 } else if (op == PRCO_GETOPT) 559 error = ip6_mrouter_get(optname, so, m); 560 else 561 error = EINVAL; 562 return (error); 563 #endif 564 case IPV6_CHECKSUM: 565 return (ip6_raw_ctloutput(op, so, level, optname, m)); 566 default: 567 return (ip6_ctloutput(op, so, level, optname, m)); 568 } 569 570 case IPPROTO_ICMPV6: 571 /* 572 * XXX: is it better to call icmp6_ctloutput() directly 573 * from protosw? 574 */ 575 return (icmp6_ctloutput(op, so, level, optname, m)); 576 577 default: 578 return EINVAL; 579 } 580 } 581 582 extern u_long rip6_sendspace; 583 extern u_long rip6_recvspace; 584 585 int 586 rip6_attach(struct socket *so, int proto, int wait) 587 { 588 struct inpcb *inp; 589 int error; 590 591 if (so->so_pcb) 592 panic("%s", __func__); 593 if ((so->so_state & SS_PRIV) == 0) 594 return (EACCES); 595 if (proto < 0 || proto >= IPPROTO_MAX) 596 return EPROTONOSUPPORT; 597 598 if ((error = soreserve(so, rip6_sendspace, rip6_recvspace))) 599 return error; 600 NET_ASSERT_LOCKED(); 601 if ((error = in_pcballoc(so, &rawin6pcbtable, wait))) 602 return error; 603 604 inp = sotoinpcb(so); 605 inp->inp_ipv6.ip6_nxt = proto; 606 inp->inp_cksum6 = -1; 607 608 inp->inp_icmp6filt = malloc(sizeof(struct icmp6_filter), M_PCB, 609 wait == M_WAIT ? M_WAITOK : M_NOWAIT); 610 if (inp->inp_icmp6filt == NULL) { 611 in_pcbdetach(inp); 612 return ENOMEM; 613 } 614 ICMP6_FILTER_SETPASSALL(inp->inp_icmp6filt); 615 return 0; 616 } 617 618 int 619 rip6_detach(struct socket *so) 620 { 621 struct inpcb *inp = sotoinpcb(so); 622 623 soassertlocked(so); 624 625 if (inp == NULL) 626 panic("%s", __func__); 627 #ifdef MROUTING 628 if (so == ip6_mrouter[inp->inp_rtableid]) 629 ip6_mrouter_done(so); 630 #endif 631 free(inp->inp_icmp6filt, M_PCB, sizeof(struct icmp6_filter)); 632 inp->inp_icmp6filt = NULL; 633 634 in_pcbdetach(inp); 635 636 return (0); 637 } 638 639 void 640 rip6_lock(struct socket *so) 641 { 642 struct inpcb *inp = sotoinpcb(so); 643 644 NET_ASSERT_LOCKED(); 645 mtx_enter(&inp->inp_mtx); 646 } 647 648 void 649 rip6_unlock(struct socket *so) 650 { 651 struct inpcb *inp = sotoinpcb(so); 652 653 NET_ASSERT_LOCKED(); 654 mtx_leave(&inp->inp_mtx); 655 } 656 657 int 658 rip6_locked(struct socket *so) 659 { 660 struct inpcb *inp = sotoinpcb(so); 661 662 return mtx_owned(&inp->inp_mtx); 663 } 664 665 int 666 rip6_bind(struct socket *so, struct mbuf *nam, struct proc *p) 667 { 668 struct inpcb *inp = sotoinpcb(so); 669 struct sockaddr_in6 *addr; 670 int error; 671 672 soassertlocked(so); 673 674 if ((error = in6_nam2sin6(nam, &addr))) 675 return (error); 676 677 /* 678 * Make sure to not enter in_pcblookup_local(), local ports 679 * are non-sensical for raw sockets. 680 */ 681 addr->sin6_port = 0; 682 683 if ((error = in6_pcbaddrisavail(inp, addr, 0, p))) 684 return (error); 685 686 mtx_enter(&rawin6pcbtable.inpt_mtx); 687 inp->inp_laddr6 = addr->sin6_addr; 688 mtx_leave(&rawin6pcbtable.inpt_mtx); 689 690 return (0); 691 } 692 693 int 694 rip6_connect(struct socket *so, struct mbuf *nam) 695 { 696 struct inpcb *inp = sotoinpcb(so); 697 struct sockaddr_in6 *addr; 698 const struct in6_addr *in6a; 699 int error; 700 701 soassertlocked(so); 702 703 if ((error = in6_nam2sin6(nam, &addr))) 704 return (error); 705 706 /* Source address selection. XXX: need pcblookup? */ 707 error = in6_pcbselsrc(&in6a, addr, inp, inp->inp_outputopts6); 708 if (error) 709 return (error); 710 711 mtx_enter(&rawin6pcbtable.inpt_mtx); 712 inp->inp_laddr6 = *in6a; 713 inp->inp_faddr6 = addr->sin6_addr; 714 mtx_leave(&rawin6pcbtable.inpt_mtx); 715 soisconnected(so); 716 717 return (0); 718 } 719 720 int 721 rip6_disconnect(struct socket *so) 722 { 723 struct inpcb *inp = sotoinpcb(so); 724 725 soassertlocked(so); 726 727 if ((so->so_state & SS_ISCONNECTED) == 0) 728 return (ENOTCONN); 729 730 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 731 mtx_enter(&rawin6pcbtable.inpt_mtx); 732 inp->inp_faddr6 = in6addr_any; 733 mtx_leave(&rawin6pcbtable.inpt_mtx); 734 735 return (0); 736 } 737 738 int 739 rip6_shutdown(struct socket *so) 740 { 741 /* 742 * Mark the connection as being incapable of further input. 743 */ 744 soassertlocked(so); 745 socantsendmore(so); 746 return (0); 747 } 748 749 int 750 rip6_send(struct socket *so, struct mbuf *m, struct mbuf *nam, 751 struct mbuf *control) 752 { 753 struct inpcb *inp = sotoinpcb(so); 754 struct sockaddr_in6 dst; 755 int error; 756 757 soassertlocked(so); 758 759 /* 760 * Ship a packet out. The appropriate raw output 761 * routine handles any messaging necessary. 762 */ 763 764 /* always copy sockaddr to avoid overwrites */ 765 memset(&dst, 0, sizeof(dst)); 766 dst.sin6_family = AF_INET6; 767 dst.sin6_len = sizeof(dst); 768 if (so->so_state & SS_ISCONNECTED) { 769 if (nam) { 770 error = EISCONN; 771 goto out; 772 } 773 dst.sin6_addr = inp->inp_faddr6; 774 } else { 775 struct sockaddr_in6 *addr6; 776 777 if (nam == NULL) { 778 error = ENOTCONN; 779 goto out; 780 } 781 if ((error = in6_nam2sin6(nam, &addr6))) 782 goto out; 783 dst.sin6_addr = addr6->sin6_addr; 784 dst.sin6_scope_id = addr6->sin6_scope_id; 785 } 786 error = rip6_output(m, so, sin6tosa(&dst), control); 787 control = NULL; 788 m = NULL; 789 790 out: 791 m_freem(control); 792 m_freem(m); 793 794 return (error); 795 } 796 797 int 798 rip6_sysctl_rip6stat(void *oldp, size_t *oldplen, void *newp) 799 { 800 struct rip6stat rip6stat; 801 802 CTASSERT(sizeof(rip6stat) == rip6s_ncounters * sizeof(uint64_t)); 803 counters_read(rip6counters, (uint64_t *)&rip6stat, rip6s_ncounters, 804 NULL); 805 806 return (sysctl_rdstruct(oldp, oldplen, newp, 807 &rip6stat, sizeof(rip6stat))); 808 } 809 810 int 811 rip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 812 void *newp, size_t newlen) 813 { 814 /* All sysctl names at this level are terminal. */ 815 if (namelen != 1) 816 return ENOTDIR; 817 818 switch (name[0]) { 819 case RIPV6CTL_STATS: 820 return (rip6_sysctl_rip6stat(oldp, oldlenp, newp)); 821 default: 822 return (EOPNOTSUPP); 823 } 824 /* NOTREACHED */ 825 } 826