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