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