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