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