1 /* $NetBSD: raw_ip6.c,v 1.184 2024/02/24 21:41:13 mlelstv Exp $ */ 2 /* $KAME: raw_ip6.c,v 1.82 2001/07/23 18:57:56 jinmei Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.184 2024/02/24 21:41:13 mlelstv Exp $"); 66 67 #ifdef _KERNEL_OPT 68 #include "opt_ipsec.h" 69 #include "opt_net_mpsafe.h" 70 #endif 71 72 #include <sys/param.h> 73 #include <sys/sysctl.h> 74 #include <sys/mbuf.h> 75 #include <sys/socket.h> 76 #include <sys/protosw.h> 77 #include <sys/socketvar.h> 78 #include <sys/systm.h> 79 #include <sys/proc.h> 80 #include <sys/kauth.h> 81 #include <sys/kmem.h> 82 83 #include <net/if.h> 84 #include <net/if_types.h> 85 #include <net/net_stats.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_private.h> 92 #include <netinet6/ip6_mroute.h> 93 #include <netinet/icmp6.h> 94 #include <netinet6/icmp6_private.h> 95 #include <netinet6/in6_pcb.h> 96 #include <netinet6/ip6protosw.h> 97 #include <netinet6/scope6_var.h> 98 #include <netinet6/raw_ip6.h> 99 100 #ifdef IPSEC 101 #include <netipsec/ipsec.h> 102 #include <netipsec/ipsec6.h> 103 #endif 104 105 #include "faith.h" 106 #if defined(NFAITH) && 0 < NFAITH 107 #include <net/if_faith.h> 108 #endif 109 110 extern struct inpcbtable rawcbtable; 111 struct inpcbtable raw6cbtable; 112 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 113 114 /* 115 * Raw interface to IP6 protocol. 116 */ 117 118 static percpu_t *rip6stat_percpu; 119 120 #define RIP6_STATINC(x) _NET_STATINC(rip6stat_percpu, x) 121 122 static void sysctl_net_inet6_raw6_setup(struct sysctllog **); 123 124 /* 125 * Initialize raw connection block queue. 126 */ 127 void 128 rip6_init(void) 129 { 130 131 sysctl_net_inet6_raw6_setup(NULL); 132 in6pcb_init(&raw6cbtable, 1, 1); 133 134 rip6stat_percpu = percpu_alloc(sizeof(uint64_t) * RIP6_NSTATS); 135 } 136 137 static void 138 rip6_sbappendaddr(struct inpcb *last, struct ip6_hdr *ip6, 139 const struct sockaddr *sa, int hlen, struct mbuf *n) 140 { 141 struct mbuf *opts = NULL; 142 143 if (last->inp_flags & IN6P_CONTROLOPTS || 144 SOOPT_TIMESTAMP(last->inp_socket->so_options)) 145 ip6_savecontrol(last, &opts, ip6, n); 146 147 m_adj(n, hlen); 148 149 if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) { 150 soroverflow(last->inp_socket); 151 m_freem(n); 152 if (opts) 153 m_freem(opts); 154 RIP6_STATINC(RIP6_STAT_FULLSOCK); 155 } else { 156 sorwakeup(last->inp_socket); 157 } 158 } 159 160 /* 161 * Setup generic address and protocol structures 162 * for raw_input routine, then pass them along with 163 * mbuf chain. 164 */ 165 int 166 rip6_input(struct mbuf **mp, int *offp, int proto) 167 { 168 struct mbuf *m = *mp; 169 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 170 struct inpcb *inp; 171 struct inpcb *last = NULL; 172 struct sockaddr_in6 rip6src; 173 struct mbuf *n; 174 175 RIP6_STATINC(RIP6_STAT_IPACKETS); 176 177 #if defined(NFAITH) && 0 < NFAITH 178 if (faithprefix(&ip6->ip6_dst)) { 179 /* send icmp6 host unreach? */ 180 m_freem(m); 181 return IPPROTO_DONE; 182 } 183 #endif 184 185 sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0); 186 if (sa6_recoverscope(&rip6src) != 0) { 187 /* XXX: should be impossible. */ 188 m_freem(m); 189 return IPPROTO_DONE; 190 } 191 192 TAILQ_FOREACH(inp, &raw6cbtable.inpt_queue, inp_queue) { 193 if (inp->inp_af != AF_INET6) 194 continue; 195 if (in6p_ip6(inp).ip6_nxt && 196 in6p_ip6(inp).ip6_nxt != proto) 197 continue; 198 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p_laddr(inp)) && 199 !IN6_ARE_ADDR_EQUAL(&in6p_laddr(inp), &ip6->ip6_dst)) 200 continue; 201 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p_faddr(inp)) && 202 !IN6_ARE_ADDR_EQUAL(&in6p_faddr(inp), &ip6->ip6_src)) 203 continue; 204 if (in6p_cksum(inp) != -1) { 205 RIP6_STATINC(RIP6_STAT_ISUM); 206 /* 207 * Although in6_cksum() does not need the position of 208 * the checksum field for verification, enforce that it 209 * is located within the packet. Userland has given 210 * a checksum offset, a packet too short for that is 211 * invalid. Avoid overflow with user supplied offset. 212 */ 213 if (m->m_pkthdr.len < *offp + 2 || 214 m->m_pkthdr.len - *offp - 2 < in6p_cksum(inp) || 215 in6_cksum(m, proto, *offp, 216 m->m_pkthdr.len - *offp)) { 217 RIP6_STATINC(RIP6_STAT_BADSUM); 218 continue; 219 } 220 } 221 222 if (last == NULL) { 223 ; 224 } 225 #ifdef IPSEC 226 else if (ipsec_used && ipsec_in_reject(m, last)) { 227 /* do not inject data into pcb */ 228 } 229 #endif 230 else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) { 231 rip6_sbappendaddr(last, ip6, sin6tosa(&rip6src), 232 *offp, n); 233 } 234 235 last = inp; 236 } 237 238 #ifdef IPSEC 239 if (ipsec_used && last && ipsec_in_reject(m, last)) { 240 m_freem(m); 241 IP6_STATDEC(IP6_STAT_DELIVERED); 242 /* do not inject data into pcb */ 243 } else 244 #endif 245 if (last != NULL) { 246 rip6_sbappendaddr(last, ip6, sin6tosa(&rip6src), *offp, m); 247 } else { 248 RIP6_STATINC(RIP6_STAT_NOSOCK); 249 if (m->m_flags & M_MCAST) 250 RIP6_STATINC(RIP6_STAT_NOSOCKMCAST); 251 if (proto == IPPROTO_NONE) 252 m_freem(m); 253 else { 254 int s; 255 struct ifnet *rcvif = m_get_rcvif(m, &s); 256 const int prvnxt = ip6_get_prevhdr(m, *offp); 257 in6_ifstat_inc(rcvif, ifs6_in_protounknown); 258 m_put_rcvif(rcvif, &s); 259 icmp6_error(m, ICMP6_PARAM_PROB, 260 ICMP6_PARAMPROB_NEXTHEADER, 261 prvnxt); 262 } 263 IP6_STATDEC(IP6_STAT_DELIVERED); 264 } 265 return IPPROTO_DONE; 266 } 267 268 void * 269 rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d) 270 { 271 struct ip6_hdr *ip6; 272 struct ip6ctlparam *ip6cp = NULL; 273 const struct sockaddr_in6 *sa6_src = NULL; 274 void *cmdarg; 275 void (*notify)(struct inpcb *, int) = in6pcb_rtchange; 276 int nxt; 277 278 if (sa->sa_family != AF_INET6 || 279 sa->sa_len != sizeof(struct sockaddr_in6)) 280 return NULL; 281 282 if ((unsigned)cmd >= PRC_NCMDS) 283 return NULL; 284 if (PRC_IS_REDIRECT(cmd)) 285 notify = in6pcb_rtchange, d = NULL; 286 else if (cmd == PRC_HOSTDEAD) 287 d = NULL; 288 else if (cmd == PRC_MSGSIZE) 289 ; /* special code is present, see below */ 290 else if (inet6ctlerrmap[cmd] == 0) 291 return NULL; 292 293 /* if the parameter is from icmp6, decode it. */ 294 if (d != NULL) { 295 ip6cp = (struct ip6ctlparam *)d; 296 ip6 = ip6cp->ip6c_ip6; 297 cmdarg = ip6cp->ip6c_cmdarg; 298 sa6_src = ip6cp->ip6c_src; 299 nxt = ip6cp->ip6c_nxt; 300 } else { 301 ip6 = NULL; 302 cmdarg = NULL; 303 sa6_src = &sa6_any; 304 nxt = -1; 305 } 306 307 if (ip6 && cmd == PRC_MSGSIZE) { 308 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa; 309 int valid = 0; 310 struct inpcb *inp; 311 312 /* 313 * Check to see if we have a valid raw IPv6 socket 314 * corresponding to the address in the ICMPv6 message 315 * payload, and the protocol (ip6_nxt) meets the socket. 316 * XXX chase extension headers, or pass final nxt value 317 * from icmp6_notify_error() 318 */ 319 inp = NULL; 320 inp = in6pcb_lookup(&raw6cbtable, &sa6->sin6_addr, 0, 321 (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0, 0); 322 #if 0 323 if (!inp) { 324 /* 325 * As the use of sendto(2) is fairly popular, 326 * we may want to allow non-connected pcb too. 327 * But it could be too weak against attacks... 328 * We should at least check if the local 329 * address (= s) is really ours. 330 */ 331 inp = in6pcb_lookup_bound(&raw6cbtable, 332 &sa6->sin6_addr, 0, 0); 333 } 334 #endif 335 336 if (inp && in6p_ip6(inp).ip6_nxt && 337 in6p_ip6(inp).ip6_nxt == nxt) 338 valid++; 339 340 /* 341 * Depending on the value of "valid" and routing table 342 * size (mtudisc_{hi,lo}wat), we will: 343 * - recalculate the new MTU and create the 344 * corresponding routing entry, or 345 * - ignore the MTU change notification. 346 */ 347 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 348 349 /* 350 * regardless of if we called icmp6_mtudisc_update(), 351 * we need to call in6pcb_notify(), to notify path MTU 352 * change to the userland (RFC3542), because some 353 * unconnected sockets may share the same destination 354 * and want to know the path MTU. 355 */ 356 } 357 358 (void) in6pcb_notify(&raw6cbtable, sa, 0, 359 sin6tocsa(sa6_src), 0, cmd, cmdarg, notify); 360 return NULL; 361 } 362 363 /* 364 * Generate IPv6 header and pass packet to ip6_output. 365 * Tack on options user may have setup with control call. 366 */ 367 int 368 rip6_output(struct mbuf *m, struct socket * const so, 369 struct sockaddr_in6 * const dstsock, struct mbuf * const control) 370 { 371 struct in6_addr *dst; 372 struct ip6_hdr *ip6; 373 struct inpcb *inp; 374 u_int plen = m->m_pkthdr.len; 375 int error = 0; 376 struct ip6_pktopts opt, *optp = NULL; 377 struct ifnet *oifp = NULL; 378 int type, code; /* for ICMPv6 output statistics only */ 379 int scope_ambiguous = 0; 380 int bound = curlwp_bind(); 381 struct psref psref; 382 383 inp = sotoinpcb(so); 384 385 dst = &dstsock->sin6_addr; 386 if (control) { 387 if ((error = ip6_setpktopts(control, &opt, 388 in6p_outputopts(inp), 389 kauth_cred_get(), so->so_proto->pr_protocol)) != 0) { 390 goto bad; 391 } 392 optp = &opt; 393 } else 394 optp = in6p_outputopts(inp); 395 396 /* 397 * Check and convert scope zone ID into internal form. 398 * XXX: we may still need to determine the zone later. 399 */ 400 if (!(so->so_state & SS_ISCONNECTED)) { 401 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone) 402 scope_ambiguous = 1; 403 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0) 404 goto bad; 405 } 406 407 /* 408 * For an ICMPv6 packet, we should know its type and code 409 * to update statistics. 410 */ 411 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 412 struct icmp6_hdr *icmp6; 413 if (m->m_len < sizeof(struct icmp6_hdr) && 414 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 415 error = ENOBUFS; 416 goto bad; 417 } 418 icmp6 = mtod(m, struct icmp6_hdr *); 419 type = icmp6->icmp6_type; 420 code = icmp6->icmp6_code; 421 } else { 422 type = 0; 423 code = 0; 424 } 425 426 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 427 if (!m) { 428 error = ENOBUFS; 429 goto bad; 430 } 431 ip6 = mtod(m, struct ip6_hdr *); 432 433 /* 434 * Next header might not be ICMP6 but use its pseudo header anyway. 435 */ 436 ip6->ip6_dst = *dst; 437 438 /* 439 * Source address selection. 440 */ 441 error = in6_selectsrc(dstsock, optp, in6p_moptions(inp), 442 &inp->inp_route, &in6p_laddr(inp), &oifp, &psref, &ip6->ip6_src); 443 if (error != 0) 444 goto bad; 445 446 if (oifp && scope_ambiguous) { 447 /* 448 * Application should provide a proper zone ID or the use of 449 * default zone IDs should be enabled. Unfortunately, some 450 * applications do not behave as it should, so we need a 451 * workaround. Even if an appropriate ID is not determined 452 * (when it's required), if we can determine the outgoing 453 * interface. determine the zone ID based on the interface. 454 */ 455 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 456 if (error != 0) 457 goto bad; 458 } 459 ip6->ip6_dst = dstsock->sin6_addr; 460 461 /* fill in the rest of the IPv6 header fields */ 462 ip6->ip6_flow = in6p_flowinfo(inp) & IPV6_FLOWINFO_MASK; 463 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 464 ip6->ip6_vfc |= IPV6_VERSION; 465 /* ip6_plen will be filled in ip6_output, so not fill it here. */ 466 ip6->ip6_nxt = in6p_ip6(inp).ip6_nxt; 467 ip6->ip6_hlim = in6pcb_selecthlim(inp, oifp); 468 469 if_put(oifp, &psref); 470 oifp = NULL; 471 472 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 473 in6p_cksum(inp) != -1) { 474 const uint8_t nxt = ip6->ip6_nxt; 475 int off; 476 u_int16_t sum; 477 478 /* compute checksum */ 479 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 480 off = offsetof(struct icmp6_hdr, icmp6_cksum); 481 else 482 off = in6p_cksum(inp); 483 if (plen < 2 || plen - 2 < off) { 484 error = EINVAL; 485 goto bad; 486 } 487 off += sizeof(struct ip6_hdr); 488 489 sum = 0; 490 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum, 491 M_DONTWAIT); 492 if (m == NULL) { 493 error = ENOBUFS; 494 goto bad; 495 } 496 sum = in6_cksum(m, nxt, sizeof(*ip6), plen); 497 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum, 498 M_DONTWAIT); 499 if (m == NULL) { 500 error = ENOBUFS; 501 goto bad; 502 } 503 } 504 505 { 506 struct ifnet *ret_oifp = NULL; 507 508 error = ip6_output(m, optp, &inp->inp_route, 0, 509 in6p_moptions(inp), inp, &ret_oifp); 510 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 511 if (ret_oifp) 512 icmp6_ifoutstat_inc(ret_oifp, type, code); 513 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type); 514 } else 515 RIP6_STATINC(RIP6_STAT_OPACKETS); 516 } 517 518 goto freectl; 519 520 bad: 521 if (m) 522 m_freem(m); 523 524 freectl: 525 if (control) { 526 ip6_clearpktopts(&opt, -1); 527 m_freem(control); 528 } 529 if_put(oifp, &psref); 530 curlwp_bindx(bound); 531 return error; 532 } 533 534 /* 535 * Raw IPv6 socket option processing. 536 */ 537 int 538 rip6_ctloutput(int op, struct socket *so, struct sockopt *sopt) 539 { 540 int error = 0; 541 542 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) { 543 int optval; 544 545 /* need to fiddle w/ opt(IPPROTO_IPV6, IPV6_CHECKSUM)? */ 546 if (op == PRCO_GETOPT) { 547 optval = 1; 548 error = sockopt_set(sopt, &optval, sizeof(optval)); 549 } else if (op == PRCO_SETOPT) { 550 error = sockopt_getint(sopt, &optval); 551 if (error) 552 goto out; 553 if (optval == 0) 554 error = EINVAL; 555 } 556 557 goto out; 558 } else if (sopt->sopt_level != IPPROTO_IPV6) 559 return ip6_ctloutput(op, so, sopt); 560 561 switch (sopt->sopt_name) { 562 case MRT6_INIT: 563 case MRT6_DONE: 564 case MRT6_ADD_MIF: 565 case MRT6_DEL_MIF: 566 case MRT6_ADD_MFC: 567 case MRT6_DEL_MFC: 568 case MRT6_PIM: 569 if (op == PRCO_SETOPT) 570 error = ip6_mrouter_set(so, sopt); 571 else if (op == PRCO_GETOPT) 572 error = ip6_mrouter_get(so, sopt); 573 else 574 error = EINVAL; 575 break; 576 case IPV6_CHECKSUM: 577 return ip6_raw_ctloutput(op, so, sopt); 578 default: 579 return ip6_ctloutput(op, so, sopt); 580 } 581 out: 582 return error; 583 } 584 585 extern u_long rip6_sendspace; 586 extern u_long rip6_recvspace; 587 588 int 589 rip6_attach(struct socket *so, int proto) 590 { 591 struct inpcb *inp; 592 int s, error; 593 594 KASSERT(sotoinpcb(so) == NULL); 595 sosetlock(so); 596 597 error = kauth_authorize_network(kauth_cred_get(), 598 KAUTH_NETWORK_SOCKET, KAUTH_REQ_NETWORK_SOCKET_RAWSOCK, 599 KAUTH_ARG(AF_INET6), 600 KAUTH_ARG(SOCK_RAW), 601 KAUTH_ARG(so->so_proto->pr_protocol)); 602 if (error) { 603 return error; 604 } 605 s = splsoftnet(); 606 error = soreserve(so, rip6_sendspace, rip6_recvspace); 607 if (error) { 608 splx(s); 609 return error; 610 } 611 if ((error = inpcb_create(so, &raw6cbtable)) != 0) { 612 splx(s); 613 return error; 614 } 615 splx(s); 616 inp = sotoinpcb(so); 617 in6p_ip6(inp).ip6_nxt = proto; 618 in6p_cksum(inp) = -1; 619 620 in6p_icmp6filt(inp) = kmem_alloc(sizeof(struct icmp6_filter), KM_SLEEP); 621 ICMP6_FILTER_SETPASSALL(in6p_icmp6filt(inp)); 622 KASSERT(solocked(so)); 623 return error; 624 } 625 626 static void 627 rip6_detach(struct socket *so) 628 { 629 struct inpcb *inp = sotoinpcb(so); 630 631 KASSERT(solocked(so)); 632 KASSERT(inp != NULL); 633 634 if (so == ip6_mrouter) { 635 ip6_mrouter_done(); 636 } 637 /* xxx: RSVP */ 638 if (in6p_icmp6filt(inp) != NULL) { 639 kmem_free(in6p_icmp6filt(inp), sizeof(struct icmp6_filter)); 640 in6p_icmp6filt(inp) = NULL; 641 } 642 inpcb_destroy(inp); 643 } 644 645 static int 646 rip6_accept(struct socket *so, struct sockaddr *nam) 647 { 648 KASSERT(solocked(so)); 649 650 return EOPNOTSUPP; 651 } 652 653 static int 654 rip6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 655 { 656 struct inpcb *inp = sotoinpcb(so); 657 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 658 struct ifaddr *ifa = NULL; 659 int error = 0; 660 int s; 661 662 KASSERT(solocked(so)); 663 KASSERT(inp != NULL); 664 KASSERT(nam != NULL); 665 666 if (addr->sin6_len != sizeof(*addr)) 667 return EINVAL; 668 if (IFNET_READER_EMPTY() || addr->sin6_family != AF_INET6) 669 return EADDRNOTAVAIL; 670 671 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 672 return error; 673 674 /* 675 * we don't support mapped address here, it would confuse 676 * users so reject it 677 */ 678 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) 679 return EADDRNOTAVAIL; 680 s = pserialize_read_enter(); 681 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 682 (ifa = ifa_ifwithaddr(sin6tosa(addr))) == NULL) { 683 error = EADDRNOTAVAIL; 684 goto out; 685 } 686 if (ifa && (ifatoia6(ifa))->ia6_flags & 687 (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) { 688 error = EADDRNOTAVAIL; 689 goto out; 690 } 691 692 in6p_laddr(inp) = addr->sin6_addr; 693 error = 0; 694 out: 695 pserialize_read_exit(s); 696 return error; 697 } 698 699 static int 700 rip6_listen(struct socket *so, struct lwp *l) 701 { 702 KASSERT(solocked(so)); 703 704 return EOPNOTSUPP; 705 } 706 707 static int 708 rip6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) 709 { 710 struct inpcb *inp = sotoinpcb(so); 711 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 712 struct in6_addr in6a; 713 struct ifnet *ifp = NULL; 714 int scope_ambiguous = 0; 715 int error = 0; 716 struct psref psref; 717 int bound; 718 719 KASSERT(solocked(so)); 720 KASSERT(inp != NULL); 721 KASSERT(nam != NULL); 722 723 if (IFNET_READER_EMPTY()) 724 return EADDRNOTAVAIL; 725 if (addr->sin6_family != AF_INET6) 726 return EAFNOSUPPORT; 727 if (addr->sin6_len != sizeof(*addr)) 728 return EINVAL; 729 730 /* 731 * Application should provide a proper zone ID or the use of 732 * default zone IDs should be enabled. Unfortunately, some 733 * applications do not behave as it should, so we need a 734 * workaround. Even if an appropriate ID is not determined, 735 * we'll see if we can determine the outgoing interface. If we 736 * can, determine the zone ID based on the interface below. 737 */ 738 if (addr->sin6_scope_id == 0 && !ip6_use_defzone) 739 scope_ambiguous = 1; 740 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 741 return error; 742 743 bound = curlwp_bind(); 744 /* Source address selection. XXX: need pcblookup? */ 745 error = in6_selectsrc(addr, in6p_outputopts(inp), 746 in6p_moptions(inp), &inp->inp_route, 747 &in6p_laddr(inp), &ifp, &psref, &in6a); 748 if (error != 0) 749 goto out; 750 /* XXX: see above */ 751 if (ifp && scope_ambiguous && 752 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 753 goto out; 754 } 755 in6p_laddr(inp) = in6a; 756 in6p_faddr(inp) = addr->sin6_addr; 757 soisconnected(so); 758 out: 759 if_put(ifp, &psref); 760 curlwp_bindx(bound); 761 return error; 762 } 763 764 static int 765 rip6_connect2(struct socket *so, struct socket *so2) 766 { 767 KASSERT(solocked(so)); 768 769 return EOPNOTSUPP; 770 } 771 772 static int 773 rip6_disconnect(struct socket *so) 774 { 775 struct inpcb *inp = sotoinpcb(so); 776 777 KASSERT(solocked(so)); 778 KASSERT(inp != NULL); 779 780 if ((so->so_state & SS_ISCONNECTED) == 0) 781 return ENOTCONN; 782 783 in6p_faddr(inp) = in6addr_any; 784 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 785 return 0; 786 } 787 788 static int 789 rip6_shutdown(struct socket *so) 790 { 791 KASSERT(solocked(so)); 792 793 /* 794 * Mark the connection as being incapable of further input. 795 */ 796 socantsendmore(so); 797 return 0; 798 } 799 800 static int 801 rip6_abort(struct socket *so) 802 { 803 KASSERT(solocked(so)); 804 805 soisdisconnected(so); 806 rip6_detach(so); 807 return 0; 808 } 809 810 static int 811 rip6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 812 { 813 return in6_control(so, cmd, nam, ifp); 814 } 815 816 static int 817 rip6_stat(struct socket *so, struct stat *ub) 818 { 819 KASSERT(solocked(so)); 820 821 /* stat: don't bother with a blocksize */ 822 return 0; 823 } 824 825 static int 826 rip6_peeraddr(struct socket *so, struct sockaddr *nam) 827 { 828 KASSERT(solocked(so)); 829 KASSERT(sotoinpcb(so) != NULL); 830 KASSERT(nam != NULL); 831 832 in6pcb_fetch_peeraddr(sotoinpcb(so), (struct sockaddr_in6 *)nam); 833 return 0; 834 } 835 836 static int 837 rip6_sockaddr(struct socket *so, struct sockaddr *nam) 838 { 839 KASSERT(solocked(so)); 840 KASSERT(sotoinpcb(so) != NULL); 841 KASSERT(nam != NULL); 842 843 in6pcb_fetch_sockaddr(sotoinpcb(so), (struct sockaddr_in6 *)nam); 844 return 0; 845 } 846 847 static int 848 rip6_rcvd(struct socket *so, int flags, struct lwp *l) 849 { 850 KASSERT(solocked(so)); 851 852 return EOPNOTSUPP; 853 } 854 855 static int 856 rip6_recvoob(struct socket *so, struct mbuf *m, int flags) 857 { 858 KASSERT(solocked(so)); 859 860 return EOPNOTSUPP; 861 } 862 863 static int 864 rip6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam, 865 struct mbuf *control, struct lwp *l) 866 { 867 struct inpcb *inp = sotoinpcb(so); 868 struct sockaddr_in6 tmp; 869 struct sockaddr_in6 *dst; 870 int error = 0; 871 872 KASSERT(solocked(so)); 873 KASSERT(inp != NULL); 874 KASSERT(m != NULL); 875 876 /* 877 * Ship a packet out. The appropriate raw output 878 * routine handles any messaging necessary. 879 */ 880 881 /* always copy sockaddr to avoid overwrites */ 882 if (so->so_state & SS_ISCONNECTED) { 883 if (nam) { 884 error = EISCONN; 885 goto release; 886 } 887 /* XXX */ 888 sockaddr_in6_init(&tmp, &in6p_faddr(inp), 0, 0, 0); 889 dst = &tmp; 890 } else { 891 if (nam == NULL) { 892 error = ENOTCONN; 893 goto release; 894 } 895 tmp = *(struct sockaddr_in6 *)nam; 896 dst = &tmp; 897 898 if (dst->sin6_family != AF_INET6) { 899 error = EAFNOSUPPORT; 900 goto release; 901 } 902 if (dst->sin6_len != sizeof(*dst)) { 903 error = EINVAL; 904 goto release; 905 } 906 } 907 error = rip6_output(m, so, dst, control); 908 m = NULL; 909 910 release: 911 if (m) 912 m_freem(m); 913 914 return error; 915 } 916 917 static int 918 rip6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control) 919 { 920 KASSERT(solocked(so)); 921 922 m_freem(m); 923 m_freem(control); 924 925 return EOPNOTSUPP; 926 } 927 928 static int 929 rip6_purgeif(struct socket *so, struct ifnet *ifp) 930 { 931 932 mutex_enter(softnet_lock); 933 in6pcb_purgeif0(&raw6cbtable, ifp); 934 #ifdef NET_MPSAFE 935 mutex_exit(softnet_lock); 936 #endif 937 in6_purgeif(ifp); 938 #ifdef NET_MPSAFE 939 mutex_enter(softnet_lock); 940 #endif 941 in6pcb_purgeif(&raw6cbtable, ifp); 942 mutex_exit(softnet_lock); 943 944 return 0; 945 } 946 947 static int 948 sysctl_net_inet6_raw6_stats(SYSCTLFN_ARGS) 949 { 950 951 return (NETSTAT_SYSCTL(rip6stat_percpu, RIP6_NSTATS)); 952 } 953 954 static void 955 sysctl_net_inet6_raw6_setup(struct sysctllog **clog) 956 { 957 958 sysctl_createv(clog, 0, NULL, NULL, 959 CTLFLAG_PERMANENT, 960 CTLTYPE_NODE, "inet6", NULL, 961 NULL, 0, NULL, 0, 962 CTL_NET, PF_INET6, CTL_EOL); 963 sysctl_createv(clog, 0, NULL, NULL, 964 CTLFLAG_PERMANENT, 965 CTLTYPE_NODE, "raw6", 966 SYSCTL_DESCR("Raw IPv6 settings"), 967 NULL, 0, NULL, 0, 968 CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL); 969 970 sysctl_createv(clog, 0, NULL, NULL, 971 CTLFLAG_PERMANENT, 972 CTLTYPE_STRUCT, "pcblist", 973 SYSCTL_DESCR("Raw IPv6 control block list"), 974 sysctl_inpcblist, 0, &raw6cbtable, 0, 975 CTL_NET, PF_INET6, IPPROTO_RAW, 976 CTL_CREATE, CTL_EOL); 977 sysctl_createv(clog, 0, NULL, NULL, 978 CTLFLAG_PERMANENT, 979 CTLTYPE_STRUCT, "stats", 980 SYSCTL_DESCR("Raw IPv6 statistics"), 981 sysctl_net_inet6_raw6_stats, 0, NULL, 0, 982 CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS, 983 CTL_EOL); 984 } 985 986 PR_WRAP_USRREQS(rip6) 987 #define rip6_attach rip6_attach_wrapper 988 #define rip6_detach rip6_detach_wrapper 989 #define rip6_accept rip6_accept_wrapper 990 #define rip6_bind rip6_bind_wrapper 991 #define rip6_listen rip6_listen_wrapper 992 #define rip6_connect rip6_connect_wrapper 993 #define rip6_connect2 rip6_connect2_wrapper 994 #define rip6_disconnect rip6_disconnect_wrapper 995 #define rip6_shutdown rip6_shutdown_wrapper 996 #define rip6_abort rip6_abort_wrapper 997 #define rip6_ioctl rip6_ioctl_wrapper 998 #define rip6_stat rip6_stat_wrapper 999 #define rip6_peeraddr rip6_peeraddr_wrapper 1000 #define rip6_sockaddr rip6_sockaddr_wrapper 1001 #define rip6_rcvd rip6_rcvd_wrapper 1002 #define rip6_recvoob rip6_recvoob_wrapper 1003 #define rip6_send rip6_send_wrapper 1004 #define rip6_sendoob rip6_sendoob_wrapper 1005 #define rip6_purgeif rip6_purgeif_wrapper 1006 1007 const struct pr_usrreqs rip6_usrreqs = { 1008 .pr_attach = rip6_attach, 1009 .pr_detach = rip6_detach, 1010 .pr_accept = rip6_accept, 1011 .pr_bind = rip6_bind, 1012 .pr_listen = rip6_listen, 1013 .pr_connect = rip6_connect, 1014 .pr_connect2 = rip6_connect2, 1015 .pr_disconnect = rip6_disconnect, 1016 .pr_shutdown = rip6_shutdown, 1017 .pr_abort = rip6_abort, 1018 .pr_ioctl = rip6_ioctl, 1019 .pr_stat = rip6_stat, 1020 .pr_peeraddr = rip6_peeraddr, 1021 .pr_sockaddr = rip6_sockaddr, 1022 .pr_rcvd = rip6_rcvd, 1023 .pr_recvoob = rip6_recvoob, 1024 .pr_send = rip6_send, 1025 .pr_sendoob = rip6_sendoob, 1026 .pr_purgeif = rip6_purgeif, 1027 }; 1028