1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1982, 1986, 1988, 1993 34 * The Regents of the University of California. 35 * 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 62 #include <sys/cdefs.h> 63 #include "opt_ipsec.h" 64 #include "opt_inet6.h" 65 #include "opt_route.h" 66 67 #include <sys/param.h> 68 #include <sys/errno.h> 69 #include <sys/jail.h> 70 #include <sys/kernel.h> 71 #include <sys/lock.h> 72 #include <sys/malloc.h> 73 #include <sys/mbuf.h> 74 #include <sys/priv.h> 75 #include <sys/proc.h> 76 #include <sys/protosw.h> 77 #include <sys/signalvar.h> 78 #include <sys/socket.h> 79 #include <sys/socketvar.h> 80 #include <sys/sx.h> 81 #include <sys/syslog.h> 82 83 #include <net/if.h> 84 #include <net/if_var.h> 85 #include <net/if_private.h> 86 #include <net/if_types.h> 87 #include <net/route.h> 88 #include <net/vnet.h> 89 90 #include <netinet/in.h> 91 #include <netinet/in_var.h> 92 #include <netinet/in_systm.h> 93 #include <netinet/in_pcb.h> 94 95 #include <netinet/icmp6.h> 96 #include <netinet/ip6.h> 97 #include <netinet/ip_var.h> 98 #include <netinet6/ip6_mroute.h> 99 #include <netinet6/in6_pcb.h> 100 #include <netinet6/ip6_var.h> 101 #include <netinet6/nd6.h> 102 #include <netinet6/raw_ip6.h> 103 #include <netinet6/in6_fib.h> 104 #include <netinet6/scope6_var.h> 105 #include <netinet6/send.h> 106 107 #include <netipsec/ipsec_support.h> 108 109 #include <machine/stdarg.h> 110 111 #define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 112 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 113 114 /* 115 * Raw interface to IP6 protocol. 116 */ 117 118 VNET_DECLARE(struct inpcbinfo, ripcbinfo); 119 #define V_ripcbinfo VNET(ripcbinfo) 120 121 extern u_long rip_sendspace; 122 extern u_long rip_recvspace; 123 124 VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat); 125 VNET_PCPUSTAT_SYSINIT(rip6stat); 126 127 #ifdef VIMAGE 128 VNET_PCPUSTAT_SYSUNINIT(rip6stat); 129 #endif /* VIMAGE */ 130 131 /* 132 * Hooks for multicast routing. They all default to NULL, so leave them not 133 * initialized and rely on BSS being set to 0. 134 */ 135 136 /* 137 * The socket used to communicate with the multicast routing daemon. 138 */ 139 VNET_DEFINE(struct socket *, ip6_mrouter); 140 141 /* 142 * The various mrouter functions. 143 */ 144 int (*ip6_mrouter_set)(struct socket *, struct sockopt *); 145 int (*ip6_mrouter_get)(struct socket *, struct sockopt *); 146 int (*ip6_mrouter_done)(void); 147 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 148 int (*mrt6_ioctl)(u_long, caddr_t); 149 150 struct rip6_inp_match_ctx { 151 struct ip6_hdr *ip6; 152 int proto; 153 }; 154 155 static bool 156 rip6_inp_match(const struct inpcb *inp, void *v) 157 { 158 struct rip6_inp_match_ctx *c = v; 159 struct ip6_hdr *ip6 = c->ip6; 160 int proto = c->proto; 161 162 /* XXX inp locking */ 163 if ((inp->inp_vflag & INP_IPV6) == 0) 164 return (false); 165 if (inp->inp_ip_p && inp->inp_ip_p != proto) 166 return (false); 167 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 168 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst)) 169 return (false); 170 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 171 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src)) 172 return (false); 173 174 return (true); 175 } 176 177 /* 178 * Setup generic address and protocol structures for raw_input routine, then 179 * pass them along with mbuf chain. 180 */ 181 int 182 rip6_input(struct mbuf **mp, int *offp, int proto) 183 { 184 struct ifnet *ifp; 185 struct mbuf *n, *m = *mp; 186 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 187 struct inpcb *inp; 188 struct mbuf *opts = NULL; 189 struct sockaddr_in6 fromsa; 190 struct rip6_inp_match_ctx ctx = { .ip6 = ip6, .proto = proto }; 191 struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo, 192 INPLOOKUP_RLOCKPCB, rip6_inp_match, &ctx); 193 int delivered = 0; 194 195 NET_EPOCH_ASSERT(); 196 197 RIP6STAT_INC(rip6s_ipackets); 198 199 init_sin6(&fromsa, m, 0); /* general init */ 200 201 ifp = m->m_pkthdr.rcvif; 202 203 while ((inp = inp_next(&inpi)) != NULL) { 204 INP_RLOCK_ASSERT(inp); 205 #if defined(IPSEC) || defined(IPSEC_SUPPORT) 206 /* 207 * Check AH/ESP integrity. 208 */ 209 if (IPSEC_ENABLED(ipv6) && 210 IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 211 /* Do not inject data into pcb. */ 212 continue; 213 } 214 #endif /* IPSEC */ 215 if (jailed_without_vnet(inp->inp_cred) && 216 !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 217 prison_check_ip6(inp->inp_cred, &ip6->ip6_dst) != 0) 218 /* 219 * Allow raw socket in jail to receive multicast; 220 * assume process had PRIV_NETINET_RAW at attach, 221 * and fall through into normal filter path if so. 222 */ 223 continue; 224 if (inp->in6p_cksum != -1) { 225 RIP6STAT_INC(rip6s_isum); 226 if (m->m_pkthdr.len - (*offp + inp->in6p_cksum) < 2 || 227 in6_cksum(m, proto, *offp, 228 m->m_pkthdr.len - *offp)) { 229 RIP6STAT_INC(rip6s_badsum); 230 /* 231 * Drop the received message, don't send an 232 * ICMP6 message. Set proto to IPPROTO_NONE 233 * to achieve that. 234 */ 235 INP_RUNLOCK(inp); 236 proto = IPPROTO_NONE; 237 break; 238 } 239 } 240 /* 241 * If this raw socket has multicast state, and we 242 * have received a multicast, check if this socket 243 * should receive it, as multicast filtering is now 244 * the responsibility of the transport layer. 245 */ 246 if (inp->in6p_moptions && 247 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 248 /* 249 * If the incoming datagram is for MLD, allow it 250 * through unconditionally to the raw socket. 251 * 252 * Use the M_RTALERT_MLD flag to check for MLD 253 * traffic without having to inspect the mbuf chain 254 * more deeply, as all MLDv1/v2 host messages MUST 255 * contain the Router Alert option. 256 * 257 * In the case of MLDv1, we may not have explicitly 258 * joined the group, and may have set IFF_ALLMULTI 259 * on the interface. im6o_mc_filter() may discard 260 * control traffic we actually need to see. 261 * 262 * Userland multicast routing daemons should continue 263 * filter the control traffic appropriately. 264 */ 265 int blocked; 266 267 blocked = MCAST_PASS; 268 if ((m->m_flags & M_RTALERT_MLD) == 0) { 269 struct sockaddr_in6 mcaddr; 270 271 bzero(&mcaddr, sizeof(struct sockaddr_in6)); 272 mcaddr.sin6_len = sizeof(struct sockaddr_in6); 273 mcaddr.sin6_family = AF_INET6; 274 mcaddr.sin6_addr = ip6->ip6_dst; 275 276 blocked = im6o_mc_filter(inp->in6p_moptions, 277 ifp, 278 (struct sockaddr *)&mcaddr, 279 (struct sockaddr *)&fromsa); 280 } 281 if (blocked != MCAST_PASS) { 282 IP6STAT_INC(ip6s_notmember); 283 continue; 284 } 285 } 286 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) 287 continue; 288 if (inp->inp_flags & INP_CONTROLOPTS || 289 inp->inp_socket->so_options & SO_TIMESTAMP) 290 ip6_savecontrol(inp, n, &opts); 291 /* strip intermediate headers */ 292 m_adj(n, *offp); 293 if (sbappendaddr(&inp->inp_socket->so_rcv, 294 (struct sockaddr *)&fromsa, n, opts) == 0) { 295 soroverflow(inp->inp_socket); 296 m_freem(n); 297 if (opts) 298 m_freem(opts); 299 RIP6STAT_INC(rip6s_fullsock); 300 } else { 301 sorwakeup(inp->inp_socket); 302 delivered++; 303 } 304 opts = NULL; 305 } 306 if (delivered == 0) { 307 RIP6STAT_INC(rip6s_nosock); 308 if (m->m_flags & M_MCAST) 309 RIP6STAT_INC(rip6s_nosockmcast); 310 if (proto == IPPROTO_NONE) 311 m_freem(m); 312 else 313 icmp6_error(m, ICMP6_PARAM_PROB, 314 ICMP6_PARAMPROB_NEXTHEADER, 315 ip6_get_prevhdr(m, *offp)); 316 IP6STAT_DEC(ip6s_delivered); 317 } else 318 m_freem(m); 319 return (IPPROTO_DONE); 320 } 321 322 void 323 rip6_ctlinput(struct ip6ctlparam *ip6cp) 324 { 325 int errno; 326 327 if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) != 0) 328 in6_pcbnotify(&V_ripcbinfo, ip6cp->ip6c_finaldst, 0, 329 ip6cp->ip6c_src, 0, errno, ip6cp->ip6c_cmdarg, 330 in6_rtchange); 331 } 332 333 /* 334 * Generate IPv6 header and pass packet to ip6_output. Tack on options user 335 * may have setup with control call. 336 */ 337 static int 338 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 339 struct mbuf *control, struct thread *td) 340 { 341 struct epoch_tracker et; 342 struct inpcb *inp; 343 struct sockaddr_in6 tmp, *dstsock; 344 struct m_tag *mtag; 345 struct ip6_hdr *ip6; 346 u_int plen = m->m_pkthdr.len; 347 struct ip6_pktopts opt, *optp; 348 struct ifnet *oifp = NULL; 349 int error; 350 int type = 0, code = 0; /* for ICMPv6 output statistics only */ 351 int scope_ambiguous = 0; 352 int use_defzone = 0; 353 int hlim = 0; 354 struct in6_addr in6a; 355 356 inp = sotoinpcb(so); 357 KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 358 359 /* Always copy sockaddr to avoid overwrites. */ 360 /* Unlocked read. */ 361 if (so->so_state & SS_ISCONNECTED) { 362 if (nam) { 363 error = EISCONN; 364 goto release; 365 } 366 tmp = (struct sockaddr_in6 ){ 367 .sin6_family = AF_INET6, 368 .sin6_len = sizeof(struct sockaddr_in6), 369 }; 370 INP_RLOCK(inp); 371 bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 372 sizeof(struct in6_addr)); 373 INP_RUNLOCK(inp); 374 dstsock = &tmp; 375 } else { 376 if (nam == NULL) 377 error = ENOTCONN; 378 else if (nam->sa_family != AF_INET6) 379 error = EAFNOSUPPORT; 380 else if (nam->sa_len != sizeof(struct sockaddr_in6)) 381 error = EINVAL; 382 else 383 error = 0; 384 if (error != 0) 385 goto release; 386 dstsock = (struct sockaddr_in6 *)nam; 387 if (dstsock->sin6_family != AF_INET6) { 388 error = EAFNOSUPPORT; 389 goto release; 390 } 391 } 392 393 INP_WLOCK(inp); 394 395 if (control != NULL) { 396 NET_EPOCH_ENTER(et); 397 error = ip6_setpktopts(control, &opt, inp->in6p_outputopts, 398 so->so_cred, inp->inp_ip_p); 399 NET_EPOCH_EXIT(et); 400 401 if (error != 0) { 402 goto bad; 403 } 404 optp = &opt; 405 } else 406 optp = inp->in6p_outputopts; 407 408 /* 409 * Check and convert scope zone ID into internal form. 410 * 411 * XXX: we may still need to determine the zone later. 412 */ 413 if (!(so->so_state & SS_ISCONNECTED)) { 414 if (!optp || !optp->ip6po_pktinfo || 415 !optp->ip6po_pktinfo->ipi6_ifindex) 416 use_defzone = V_ip6_use_defzone; 417 if (dstsock->sin6_scope_id == 0 && !use_defzone) 418 scope_ambiguous = 1; 419 if ((error = sa6_embedscope(dstsock, use_defzone)) != 0) 420 goto bad; 421 } 422 423 /* 424 * For an ICMPv6 packet, we should know its type and code to update 425 * statistics. 426 */ 427 if (inp->inp_ip_p == IPPROTO_ICMPV6) { 428 struct icmp6_hdr *icmp6; 429 if (m->m_len < sizeof(struct icmp6_hdr) && 430 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 431 error = ENOBUFS; 432 goto bad; 433 } 434 icmp6 = mtod(m, struct icmp6_hdr *); 435 type = icmp6->icmp6_type; 436 code = icmp6->icmp6_code; 437 } 438 439 M_PREPEND(m, sizeof(*ip6), M_NOWAIT); 440 if (m == NULL) { 441 error = ENOBUFS; 442 goto bad; 443 } 444 ip6 = mtod(m, struct ip6_hdr *); 445 446 #ifdef ROUTE_MPATH 447 if (CALC_FLOWID_OUTBOUND) { 448 uint32_t hash_type, hash_val; 449 450 hash_val = fib6_calc_software_hash(&inp->in6p_laddr, 451 &dstsock->sin6_addr, 0, 0, inp->inp_ip_p, &hash_type); 452 inp->inp_flowid = hash_val; 453 inp->inp_flowtype = hash_type; 454 } 455 #endif 456 /* 457 * Source address selection. 458 */ 459 NET_EPOCH_ENTER(et); 460 error = in6_selectsrc_socket(dstsock, optp, inp, so->so_cred, 461 scope_ambiguous, &in6a, &hlim); 462 NET_EPOCH_EXIT(et); 463 464 if (error) 465 goto bad; 466 error = prison_check_ip6(inp->inp_cred, &in6a); 467 if (error != 0) 468 goto bad; 469 ip6->ip6_src = in6a; 470 471 ip6->ip6_dst = dstsock->sin6_addr; 472 473 /* 474 * Fill in the rest of the IPv6 header fields. 475 */ 476 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 477 (inp->inp_flow & IPV6_FLOWINFO_MASK); 478 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 479 (IPV6_VERSION & IPV6_VERSION_MASK); 480 481 /* 482 * ip6_plen will be filled in ip6_output, so not fill it here. 483 */ 484 ip6->ip6_nxt = inp->inp_ip_p; 485 ip6->ip6_hlim = hlim; 486 487 if (inp->inp_ip_p == IPPROTO_ICMPV6 || inp->in6p_cksum != -1) { 488 struct mbuf *n; 489 int off; 490 u_int16_t *p; 491 492 /* Compute checksum. */ 493 if (inp->inp_ip_p == IPPROTO_ICMPV6) 494 off = offsetof(struct icmp6_hdr, icmp6_cksum); 495 else 496 off = inp->in6p_cksum; 497 if (plen < off + 2) { 498 error = EINVAL; 499 goto bad; 500 } 501 off += sizeof(struct ip6_hdr); 502 503 n = m; 504 while (n && n->m_len <= off) { 505 off -= n->m_len; 506 n = n->m_next; 507 } 508 if (!n) 509 goto bad; 510 p = (u_int16_t *)(mtod(n, caddr_t) + off); 511 *p = 0; 512 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 513 } 514 515 /* 516 * Send RA/RS messages to user land for protection, before sending 517 * them to rtadvd/rtsol. 518 */ 519 if ((send_sendso_input_hook != NULL) && 520 inp->inp_ip_p == IPPROTO_ICMPV6) { 521 switch (type) { 522 case ND_ROUTER_ADVERT: 523 case ND_ROUTER_SOLICIT: 524 mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 525 sizeof(unsigned short), M_NOWAIT); 526 if (mtag == NULL) 527 goto bad; 528 m_tag_prepend(m, mtag); 529 } 530 } 531 532 NET_EPOCH_ENTER(et); 533 error = ip6_output(m, optp, NULL, 0, inp->in6p_moptions, &oifp, inp); 534 NET_EPOCH_EXIT(et); 535 if (inp->inp_ip_p == IPPROTO_ICMPV6) { 536 if (oifp) 537 icmp6_ifoutstat_inc(oifp, type, code); 538 ICMP6STAT_INC(icp6s_outhist[type]); 539 } else 540 RIP6STAT_INC(rip6s_opackets); 541 542 goto freectl; 543 544 bad: 545 if (m) 546 m_freem(m); 547 548 freectl: 549 if (control != NULL) { 550 ip6_clearpktopts(&opt, -1); 551 m_freem(control); 552 } 553 INP_WUNLOCK(inp); 554 return (error); 555 556 release: 557 if (control != NULL) 558 m_freem(control); 559 m_freem(m); 560 return (error); 561 } 562 563 /* 564 * Raw IPv6 socket option processing. 565 */ 566 int 567 rip6_ctloutput(struct socket *so, struct sockopt *sopt) 568 { 569 struct inpcb *inp = sotoinpcb(so); 570 int error; 571 572 if (sopt->sopt_level == IPPROTO_ICMPV6) 573 /* 574 * XXX: is it better to call icmp6_ctloutput() directly 575 * from protosw? 576 */ 577 return (icmp6_ctloutput(so, sopt)); 578 else if (sopt->sopt_level != IPPROTO_IPV6) { 579 if (sopt->sopt_level == SOL_SOCKET && 580 sopt->sopt_name == SO_SETFIB) { 581 INP_WLOCK(inp); 582 inp->inp_inc.inc_fibnum = so->so_fibnum; 583 INP_WUNLOCK(inp); 584 return (0); 585 } 586 return (EINVAL); 587 } 588 589 error = 0; 590 591 switch (sopt->sopt_dir) { 592 case SOPT_GET: 593 switch (sopt->sopt_name) { 594 case MRT6_INIT: 595 case MRT6_DONE: 596 case MRT6_ADD_MIF: 597 case MRT6_DEL_MIF: 598 case MRT6_ADD_MFC: 599 case MRT6_DEL_MFC: 600 case MRT6_PIM: 601 if (inp->inp_ip_p != IPPROTO_ICMPV6) 602 return (EOPNOTSUPP); 603 error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 604 EOPNOTSUPP; 605 break; 606 case IPV6_CHECKSUM: 607 error = ip6_raw_ctloutput(so, sopt); 608 break; 609 default: 610 error = ip6_ctloutput(so, sopt); 611 break; 612 } 613 break; 614 615 case SOPT_SET: 616 switch (sopt->sopt_name) { 617 case MRT6_INIT: 618 case MRT6_DONE: 619 case MRT6_ADD_MIF: 620 case MRT6_DEL_MIF: 621 case MRT6_ADD_MFC: 622 case MRT6_DEL_MFC: 623 case MRT6_PIM: 624 if (inp->inp_ip_p != IPPROTO_ICMPV6) 625 return (EOPNOTSUPP); 626 error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 627 EOPNOTSUPP; 628 break; 629 case IPV6_CHECKSUM: 630 error = ip6_raw_ctloutput(so, sopt); 631 break; 632 default: 633 error = ip6_ctloutput(so, sopt); 634 break; 635 } 636 break; 637 } 638 639 return (error); 640 } 641 642 static int 643 rip6_attach(struct socket *so, int proto, struct thread *td) 644 { 645 struct inpcb *inp; 646 struct icmp6_filter *filter; 647 int error; 648 649 inp = sotoinpcb(so); 650 KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 651 652 error = priv_check(td, PRIV_NETINET_RAW); 653 if (error) 654 return (error); 655 if (proto >= IPPROTO_MAX || proto < 0) 656 return (EPROTONOSUPPORT); 657 error = soreserve(so, rip_sendspace, rip_recvspace); 658 if (error) 659 return (error); 660 filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 661 if (filter == NULL) 662 return (ENOMEM); 663 error = in_pcballoc(so, &V_ripcbinfo); 664 if (error) { 665 free(filter, M_PCB); 666 return (error); 667 } 668 inp = (struct inpcb *)so->so_pcb; 669 inp->inp_ip_p = proto; 670 inp->in6p_cksum = -1; 671 inp->in6p_icmp6filt = filter; 672 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 673 INP_WUNLOCK(inp); 674 return (0); 675 } 676 677 static void 678 rip6_detach(struct socket *so) 679 { 680 struct inpcb *inp; 681 682 inp = sotoinpcb(so); 683 KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 684 685 if (so == V_ip6_mrouter && ip6_mrouter_done) 686 ip6_mrouter_done(); 687 /* xxx: RSVP */ 688 INP_WLOCK(inp); 689 free(inp->in6p_icmp6filt, M_PCB); 690 in_pcbfree(inp); 691 } 692 693 /* XXXRW: This can't ever be called. */ 694 static void 695 rip6_abort(struct socket *so) 696 { 697 struct inpcb *inp __diagused; 698 699 inp = sotoinpcb(so); 700 KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 701 702 soisdisconnected(so); 703 } 704 705 static void 706 rip6_close(struct socket *so) 707 { 708 struct inpcb *inp __diagused; 709 710 inp = sotoinpcb(so); 711 KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 712 713 soisdisconnected(so); 714 } 715 716 static int 717 rip6_disconnect(struct socket *so) 718 { 719 struct inpcb *inp; 720 721 inp = sotoinpcb(so); 722 KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 723 724 if ((so->so_state & SS_ISCONNECTED) == 0) 725 return (ENOTCONN); 726 inp->in6p_faddr = in6addr_any; 727 rip6_abort(so); 728 return (0); 729 } 730 731 static int 732 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 733 { 734 struct epoch_tracker et; 735 struct inpcb *inp; 736 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 737 struct ifaddr *ifa = NULL; 738 int error = 0; 739 740 inp = sotoinpcb(so); 741 KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 742 743 if (nam->sa_family != AF_INET6) 744 return (EAFNOSUPPORT); 745 if (nam->sa_len != sizeof(*addr)) 746 return (EINVAL); 747 if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0) 748 return (error); 749 if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 750 return (EADDRNOTAVAIL); 751 if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 752 return (error); 753 754 NET_EPOCH_ENTER(et); 755 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 756 (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) { 757 NET_EPOCH_EXIT(et); 758 return (EADDRNOTAVAIL); 759 } 760 if (ifa != NULL && 761 ((struct in6_ifaddr *)ifa)->ia6_flags & 762 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 763 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 764 NET_EPOCH_EXIT(et); 765 return (EADDRNOTAVAIL); 766 } 767 NET_EPOCH_EXIT(et); 768 INP_WLOCK(inp); 769 INP_INFO_WLOCK(&V_ripcbinfo); 770 inp->in6p_laddr = addr->sin6_addr; 771 INP_INFO_WUNLOCK(&V_ripcbinfo); 772 INP_WUNLOCK(inp); 773 return (0); 774 } 775 776 static int 777 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 778 { 779 struct inpcb *inp; 780 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 781 struct in6_addr in6a; 782 struct epoch_tracker et; 783 int error = 0, scope_ambiguous = 0; 784 785 inp = sotoinpcb(so); 786 KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 787 788 if (nam->sa_len != sizeof(*addr)) 789 return (EINVAL); 790 if (CK_STAILQ_EMPTY(&V_ifnet)) 791 return (EADDRNOTAVAIL); 792 if (addr->sin6_family != AF_INET6) 793 return (EAFNOSUPPORT); 794 795 /* 796 * Application should provide a proper zone ID or the use of default 797 * zone IDs should be enabled. Unfortunately, some applications do 798 * not behave as it should, so we need a workaround. Even if an 799 * appropriate ID is not determined, we'll see if we can determine 800 * the outgoing interface. If we can, determine the zone ID based on 801 * the interface below. 802 */ 803 if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 804 scope_ambiguous = 1; 805 if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 806 return (error); 807 808 INP_WLOCK(inp); 809 INP_INFO_WLOCK(&V_ripcbinfo); 810 /* Source address selection. XXX: need pcblookup? */ 811 NET_EPOCH_ENTER(et); 812 error = in6_selectsrc_socket(addr, inp->in6p_outputopts, 813 inp, so->so_cred, scope_ambiguous, &in6a, NULL); 814 NET_EPOCH_EXIT(et); 815 if (error) { 816 INP_INFO_WUNLOCK(&V_ripcbinfo); 817 INP_WUNLOCK(inp); 818 return (error); 819 } 820 821 inp->in6p_faddr = addr->sin6_addr; 822 inp->in6p_laddr = in6a; 823 soisconnected(so); 824 INP_INFO_WUNLOCK(&V_ripcbinfo); 825 INP_WUNLOCK(inp); 826 return (0); 827 } 828 829 static int 830 rip6_shutdown(struct socket *so) 831 { 832 struct inpcb *inp; 833 834 inp = sotoinpcb(so); 835 KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 836 837 INP_WLOCK(inp); 838 socantsendmore(so); 839 INP_WUNLOCK(inp); 840 return (0); 841 } 842 843 struct protosw rip6_protosw = { 844 .pr_type = SOCK_RAW, 845 .pr_flags = PR_ATOMIC|PR_ADDR, 846 .pr_ctloutput = rip6_ctloutput, 847 .pr_abort = rip6_abort, 848 .pr_attach = rip6_attach, 849 .pr_bind = rip6_bind, 850 .pr_connect = rip6_connect, 851 .pr_control = in6_control, 852 .pr_detach = rip6_detach, 853 .pr_disconnect = rip6_disconnect, 854 .pr_peeraddr = in6_getpeeraddr, 855 .pr_send = rip6_send, 856 .pr_shutdown = rip6_shutdown, 857 .pr_sockaddr = in6_getsockaddr, 858 .pr_close = rip6_close 859 }; 860