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