1 /* $NetBSD: raw_ip6.c,v 1.155 2017/01/24 07:09:25 ozaki-r Exp $ */ 2 /* $KAME: raw_ip6.c,v 1.82 2001/07/23 18:57:56 jinmei Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1988, 1993 35 * The Regents of the University of California. 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 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.155 2017/01/24 07:09:25 ozaki-r Exp $"); 66 67 #ifdef _KERNEL_OPT 68 #include "opt_ipsec.h" 69 #include "opt_net_mpsafe.h" 70 #endif 71 72 #include <sys/param.h> 73 #include <sys/sysctl.h> 74 #include <sys/mbuf.h> 75 #include <sys/socket.h> 76 #include <sys/protosw.h> 77 #include <sys/socketvar.h> 78 #include <sys/systm.h> 79 #include <sys/proc.h> 80 #include <sys/kauth.h> 81 #include <sys/kmem.h> 82 83 #include <net/if.h> 84 #include <net/if_types.h> 85 #include <net/net_stats.h> 86 87 #include <netinet/in.h> 88 #include <netinet/in_var.h> 89 #include <netinet/ip6.h> 90 #include <netinet6/ip6_var.h> 91 #include <netinet6/ip6_private.h> 92 #include <netinet6/ip6_mroute.h> 93 #include <netinet/icmp6.h> 94 #include <netinet6/icmp6_private.h> 95 #include <netinet6/in6_pcb.h> 96 #include <netinet6/ip6protosw.h> 97 #include <netinet6/scope6_var.h> 98 #include <netinet6/raw_ip6.h> 99 100 #ifdef IPSEC 101 #include <netipsec/ipsec.h> 102 #include <netipsec/ipsec_var.h> 103 #include <netipsec/ipsec_private.h> 104 #include <netipsec/ipsec6.h> 105 #endif 106 107 #include "faith.h" 108 #if defined(NFAITH) && 0 < NFAITH 109 #include <net/if_faith.h> 110 #endif 111 112 extern struct inpcbtable rawcbtable; 113 struct inpcbtable raw6cbtable; 114 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 115 116 /* 117 * Raw interface to IP6 protocol. 118 */ 119 120 static percpu_t *rip6stat_percpu; 121 122 #define RIP6_STATINC(x) _NET_STATINC(rip6stat_percpu, x) 123 124 static void sysctl_net_inet6_raw6_setup(struct sysctllog **); 125 126 /* 127 * Initialize raw connection block queue. 128 */ 129 void 130 rip6_init(void) 131 { 132 133 sysctl_net_inet6_raw6_setup(NULL); 134 in6_pcbinit(&raw6cbtable, 1, 1); 135 136 rip6stat_percpu = percpu_alloc(sizeof(uint64_t) * RIP6_NSTATS); 137 } 138 139 /* 140 * Setup generic address and protocol structures 141 * for raw_input routine, then pass them along with 142 * mbuf chain. 143 */ 144 int 145 rip6_input(struct mbuf **mp, int *offp, int proto) 146 { 147 struct mbuf *m = *mp; 148 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 149 struct inpcb_hdr *inph; 150 struct in6pcb *in6p; 151 struct in6pcb *last = NULL; 152 struct sockaddr_in6 rip6src; 153 struct mbuf *opts = NULL; 154 155 RIP6_STATINC(RIP6_STAT_IPACKETS); 156 157 #if defined(NFAITH) && 0 < NFAITH 158 if (faithprefix(&ip6->ip6_dst)) { 159 /* send icmp6 host unreach? */ 160 m_freem(m); 161 return IPPROTO_DONE; 162 } 163 #endif 164 165 /* Be proactive about malicious use of IPv4 mapped address */ 166 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 167 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 168 /* XXX stat */ 169 m_freem(m); 170 return IPPROTO_DONE; 171 } 172 173 sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0); 174 if (sa6_recoverscope(&rip6src) != 0) { 175 /* XXX: should be impossible. */ 176 m_freem(m); 177 return IPPROTO_DONE; 178 } 179 180 TAILQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) { 181 in6p = (struct in6pcb *)inph; 182 if (in6p->in6p_af != AF_INET6) 183 continue; 184 if (in6p->in6p_ip6.ip6_nxt && 185 in6p->in6p_ip6.ip6_nxt != proto) 186 continue; 187 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 188 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 189 continue; 190 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 191 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 192 continue; 193 if (in6p->in6p_cksum != -1) { 194 RIP6_STATINC(RIP6_STAT_ISUM); 195 if (in6_cksum(m, proto, *offp, 196 m->m_pkthdr.len - *offp)) { 197 RIP6_STATINC(RIP6_STAT_BADSUM); 198 continue; 199 } 200 } 201 if (last) { 202 struct mbuf *n; 203 204 #ifdef IPSEC 205 /* 206 * Check AH/ESP integrity 207 */ 208 if (ipsec_used && !ipsec6_in_reject(m, last)) 209 #endif /* IPSEC */ 210 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { 211 if (last->in6p_flags & IN6P_CONTROLOPTS) 212 ip6_savecontrol(last, &opts, ip6, n); 213 /* strip intermediate headers */ 214 m_adj(n, *offp); 215 if (sbappendaddr(&last->in6p_socket->so_rcv, 216 sin6tosa(&rip6src), n, opts) == 0) { 217 /* should notify about lost packet */ 218 m_freem(n); 219 if (opts) 220 m_freem(opts); 221 RIP6_STATINC(RIP6_STAT_FULLSOCK); 222 } else 223 sorwakeup(last->in6p_socket); 224 opts = NULL; 225 } 226 } 227 last = in6p; 228 } 229 #ifdef IPSEC 230 if (ipsec_used && last && ipsec6_in_reject(m, last)) { 231 m_freem(m); 232 /* 233 * XXX ipsec6_in_reject update stat if there is an error 234 * so we just need to update stats by hand in the case of last is 235 * NULL 236 */ 237 if (!last) 238 IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO); 239 IP6_STATDEC(IP6_STAT_DELIVERED); 240 /* do not inject data into pcb */ 241 } else 242 #endif /* IPSEC */ 243 if (last) { 244 if (last->in6p_flags & IN6P_CONTROLOPTS) 245 ip6_savecontrol(last, &opts, ip6, m); 246 /* strip intermediate headers */ 247 m_adj(m, *offp); 248 if (sbappendaddr(&last->in6p_socket->so_rcv, 249 sin6tosa(&rip6src), m, opts) == 0) { 250 m_freem(m); 251 if (opts) 252 m_freem(opts); 253 RIP6_STATINC(RIP6_STAT_FULLSOCK); 254 } else 255 sorwakeup(last->in6p_socket); 256 } else { 257 RIP6_STATINC(RIP6_STAT_NOSOCK); 258 if (m->m_flags & M_MCAST) 259 RIP6_STATINC(RIP6_STAT_NOSOCKMCAST); 260 if (proto == IPPROTO_NONE) 261 m_freem(m); 262 else { 263 int s; 264 struct ifnet *rcvif = m_get_rcvif(m, &s); 265 u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 266 in6_ifstat_inc(rcvif, ifs6_in_protounknown); 267 m_put_rcvif(rcvif, &s); 268 icmp6_error(m, ICMP6_PARAM_PROB, 269 ICMP6_PARAMPROB_NEXTHEADER, 270 prvnxtp - mtod(m, u_int8_t *)); 271 } 272 IP6_STATDEC(IP6_STAT_DELIVERED); 273 } 274 return IPPROTO_DONE; 275 } 276 277 void * 278 rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d) 279 { 280 struct ip6_hdr *ip6; 281 struct ip6ctlparam *ip6cp = NULL; 282 const struct sockaddr_in6 *sa6_src = NULL; 283 void *cmdarg; 284 void (*notify)(struct in6pcb *, int) = in6_rtchange; 285 int nxt; 286 287 if (sa->sa_family != AF_INET6 || 288 sa->sa_len != sizeof(struct sockaddr_in6)) 289 return NULL; 290 291 if ((unsigned)cmd >= PRC_NCMDS) 292 return NULL; 293 if (PRC_IS_REDIRECT(cmd)) 294 notify = in6_rtchange, d = NULL; 295 else if (cmd == PRC_HOSTDEAD) 296 d = NULL; 297 else if (cmd == PRC_MSGSIZE) 298 ; /* special code is present, see below */ 299 else if (inet6ctlerrmap[cmd] == 0) 300 return NULL; 301 302 /* if the parameter is from icmp6, decode it. */ 303 if (d != NULL) { 304 ip6cp = (struct ip6ctlparam *)d; 305 ip6 = ip6cp->ip6c_ip6; 306 cmdarg = ip6cp->ip6c_cmdarg; 307 sa6_src = ip6cp->ip6c_src; 308 nxt = ip6cp->ip6c_nxt; 309 } else { 310 ip6 = NULL; 311 cmdarg = NULL; 312 sa6_src = &sa6_any; 313 nxt = -1; 314 } 315 316 if (ip6 && cmd == PRC_MSGSIZE) { 317 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa; 318 int valid = 0; 319 struct in6pcb *in6p; 320 321 /* 322 * Check to see if we have a valid raw IPv6 socket 323 * corresponding to the address in the ICMPv6 message 324 * payload, and the protocol (ip6_nxt) meets the socket. 325 * XXX chase extension headers, or pass final nxt value 326 * from icmp6_notify_error() 327 */ 328 in6p = NULL; 329 in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0, 330 (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0, 0); 331 #if 0 332 if (!in6p) { 333 /* 334 * As the use of sendto(2) is fairly popular, 335 * we may want to allow non-connected pcb too. 336 * But it could be too weak against attacks... 337 * We should at least check if the local 338 * address (= s) is really ours. 339 */ 340 in6p = in6_pcblookup_bind(&raw6cbtable, 341 &sa6->sin6_addr, 0, 0); 342 } 343 #endif 344 345 if (in6p && in6p->in6p_ip6.ip6_nxt && 346 in6p->in6p_ip6.ip6_nxt == nxt) 347 valid++; 348 349 /* 350 * Depending on the value of "valid" and routing table 351 * size (mtudisc_{hi,lo}wat), we will: 352 * - recalculate the new MTU and create the 353 * corresponding routing entry, or 354 * - ignore the MTU change notification. 355 */ 356 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 357 358 /* 359 * regardless of if we called icmp6_mtudisc_update(), 360 * we need to call in6_pcbnotify(), to notify path MTU 361 * change to the userland (RFC3542), because some 362 * unconnected sockets may share the same destination 363 * and want to know the path MTU. 364 */ 365 } 366 367 (void) in6_pcbnotify(&raw6cbtable, sa, 0, 368 sin6tocsa(sa6_src), 0, cmd, cmdarg, notify); 369 return NULL; 370 } 371 372 /* 373 * Generate IPv6 header and pass packet to ip6_output. 374 * Tack on options user may have setup with control call. 375 */ 376 int 377 rip6_output(struct mbuf *m, struct socket * const so, 378 struct sockaddr_in6 * const dstsock, struct mbuf * const control) 379 { 380 struct in6_addr *dst; 381 struct ip6_hdr *ip6; 382 struct in6pcb *in6p; 383 u_int plen = m->m_pkthdr.len; 384 int error = 0; 385 struct ip6_pktopts opt, *optp = NULL; 386 struct ifnet *oifp = NULL; 387 int type, code; /* for ICMPv6 output statistics only */ 388 int scope_ambiguous = 0; 389 int bound = curlwp_bind(); 390 struct psref psref; 391 392 in6p = sotoin6pcb(so); 393 394 dst = &dstsock->sin6_addr; 395 if (control) { 396 if ((error = ip6_setpktopts(control, &opt, 397 in6p->in6p_outputopts, 398 kauth_cred_get(), so->so_proto->pr_protocol)) != 0) { 399 goto bad; 400 } 401 optp = &opt; 402 } else 403 optp = in6p->in6p_outputopts; 404 405 /* 406 * Check and convert scope zone ID into internal form. 407 * XXX: we may still need to determine the zone later. 408 */ 409 if (!(so->so_state & SS_ISCONNECTED)) { 410 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone) 411 scope_ambiguous = 1; 412 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0) 413 goto bad; 414 } 415 416 /* 417 * For an ICMPv6 packet, we should know its type and code 418 * to update statistics. 419 */ 420 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 421 struct icmp6_hdr *icmp6; 422 if (m->m_len < sizeof(struct icmp6_hdr) && 423 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 424 error = ENOBUFS; 425 goto bad; 426 } 427 icmp6 = mtod(m, struct icmp6_hdr *); 428 type = icmp6->icmp6_type; 429 code = icmp6->icmp6_code; 430 } else { 431 type = 0; 432 code = 0; 433 } 434 435 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 436 if (!m) { 437 error = ENOBUFS; 438 goto bad; 439 } 440 ip6 = mtod(m, struct ip6_hdr *); 441 442 /* 443 * Next header might not be ICMP6 but use its pseudo header anyway. 444 */ 445 ip6->ip6_dst = *dst; 446 447 /* 448 * Source address selection. 449 */ 450 error = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, 451 &in6p->in6p_route, &in6p->in6p_laddr, &oifp, &psref, &ip6->ip6_src); 452 if (error != 0) 453 goto bad; 454 455 if (oifp && scope_ambiguous) { 456 /* 457 * Application should provide a proper zone ID or the use of 458 * default zone IDs should be enabled. Unfortunately, some 459 * applications do not behave as it should, so we need a 460 * workaround. Even if an appropriate ID is not determined 461 * (when it's required), if we can determine the outgoing 462 * interface. determine the zone ID based on the interface. 463 */ 464 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 465 if (error != 0) 466 goto bad; 467 } 468 ip6->ip6_dst = dstsock->sin6_addr; 469 470 /* fill in the rest of the IPv6 header fields */ 471 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 472 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 473 ip6->ip6_vfc |= IPV6_VERSION; 474 /* ip6_plen will be filled in ip6_output, so not fill it here. */ 475 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt; 476 ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 477 478 if_put(oifp, &psref); 479 oifp = NULL; 480 481 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 482 in6p->in6p_cksum != -1) { 483 int off; 484 u_int16_t sum; 485 486 /* compute checksum */ 487 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 488 off = offsetof(struct icmp6_hdr, icmp6_cksum); 489 else 490 off = in6p->in6p_cksum; 491 if (plen < off + 1) { 492 error = EINVAL; 493 goto bad; 494 } 495 off += sizeof(struct ip6_hdr); 496 497 sum = 0; 498 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum, 499 M_DONTWAIT); 500 if (m == NULL) { 501 error = ENOBUFS; 502 goto bad; 503 } 504 sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 505 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum, 506 M_DONTWAIT); 507 if (m == NULL) { 508 error = ENOBUFS; 509 goto bad; 510 } 511 } 512 513 { 514 struct ifnet *ret_oifp = NULL; 515 516 error = ip6_output(m, optp, &in6p->in6p_route, 0, 517 in6p->in6p_moptions, so, &ret_oifp); 518 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 519 if (ret_oifp) 520 icmp6_ifoutstat_inc(ret_oifp, type, code); 521 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type); 522 } else 523 RIP6_STATINC(RIP6_STAT_OPACKETS); 524 } 525 526 goto freectl; 527 528 bad: 529 if (m) 530 m_freem(m); 531 532 freectl: 533 if (control) { 534 ip6_clearpktopts(&opt, -1); 535 m_freem(control); 536 } 537 if_put(oifp, &psref); 538 curlwp_bindx(bound); 539 return error; 540 } 541 542 /* 543 * Raw IPv6 socket option processing. 544 */ 545 int 546 rip6_ctloutput(int op, struct socket *so, struct sockopt *sopt) 547 { 548 int error = 0; 549 550 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) { 551 int optval; 552 553 /* need to fiddle w/ opt(IPPROTO_IPV6, IPV6_CHECKSUM)? */ 554 if (op == PRCO_GETOPT) { 555 optval = 1; 556 error = sockopt_set(sopt, &optval, sizeof(optval)); 557 } else if (op == PRCO_SETOPT) { 558 error = sockopt_getint(sopt, &optval); 559 if (error) 560 goto out; 561 if (optval == 0) 562 error = EINVAL; 563 } 564 565 goto out; 566 } else if (sopt->sopt_level != IPPROTO_IPV6) 567 return ip6_ctloutput(op, so, sopt); 568 569 switch (sopt->sopt_name) { 570 case MRT6_INIT: 571 case MRT6_DONE: 572 case MRT6_ADD_MIF: 573 case MRT6_DEL_MIF: 574 case MRT6_ADD_MFC: 575 case MRT6_DEL_MFC: 576 case MRT6_PIM: 577 if (op == PRCO_SETOPT) 578 error = ip6_mrouter_set(so, sopt); 579 else if (op == PRCO_GETOPT) 580 error = ip6_mrouter_get(so, sopt); 581 else 582 error = EINVAL; 583 break; 584 case IPV6_CHECKSUM: 585 return ip6_raw_ctloutput(op, so, sopt); 586 default: 587 return ip6_ctloutput(op, so, sopt); 588 } 589 out: 590 return error; 591 } 592 593 extern u_long rip6_sendspace; 594 extern u_long rip6_recvspace; 595 596 int 597 rip6_attach(struct socket *so, int proto) 598 { 599 struct in6pcb *in6p; 600 int s, error; 601 602 KASSERT(sotoin6pcb(so) == NULL); 603 sosetlock(so); 604 605 error = kauth_authorize_network(curlwp->l_cred, 606 KAUTH_NETWORK_SOCKET, KAUTH_REQ_NETWORK_SOCKET_RAWSOCK, 607 KAUTH_ARG(AF_INET6), 608 KAUTH_ARG(SOCK_RAW), 609 KAUTH_ARG(so->so_proto->pr_protocol)); 610 if (error) { 611 return error; 612 } 613 s = splsoftnet(); 614 error = soreserve(so, rip6_sendspace, rip6_recvspace); 615 if (error) { 616 splx(s); 617 return error; 618 } 619 if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) { 620 splx(s); 621 return error; 622 } 623 splx(s); 624 in6p = sotoin6pcb(so); 625 in6p->in6p_ip6.ip6_nxt = proto; 626 in6p->in6p_cksum = -1; 627 628 in6p->in6p_icmp6filt = kmem_alloc(sizeof(struct icmp6_filter), KM_SLEEP); 629 if (in6p->in6p_icmp6filt == NULL) { 630 in6_pcbdetach(in6p); 631 return ENOMEM; 632 } 633 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt); 634 KASSERT(solocked(so)); 635 return error; 636 } 637 638 static void 639 rip6_detach(struct socket *so) 640 { 641 struct in6pcb *in6p = sotoin6pcb(so); 642 643 KASSERT(solocked(so)); 644 KASSERT(in6p != NULL); 645 646 if (so == ip6_mrouter) { 647 ip6_mrouter_done(); 648 } 649 /* xxx: RSVP */ 650 if (in6p->in6p_icmp6filt != NULL) { 651 kmem_free(in6p->in6p_icmp6filt, sizeof(struct icmp6_filter)); 652 in6p->in6p_icmp6filt = NULL; 653 } 654 in6_pcbdetach(in6p); 655 } 656 657 static int 658 rip6_accept(struct socket *so, struct sockaddr *nam) 659 { 660 KASSERT(solocked(so)); 661 662 return EOPNOTSUPP; 663 } 664 665 static int 666 rip6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 667 { 668 struct in6pcb *in6p = sotoin6pcb(so); 669 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 670 struct ifaddr *ifa = NULL; 671 int error = 0; 672 int s; 673 674 KASSERT(solocked(so)); 675 KASSERT(in6p != NULL); 676 KASSERT(nam != NULL); 677 678 if (addr->sin6_len != sizeof(*addr)) 679 return EINVAL; 680 if (IFNET_READER_EMPTY() || addr->sin6_family != AF_INET6) 681 return EADDRNOTAVAIL; 682 683 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 684 return error; 685 686 /* 687 * we don't support mapped address here, it would confuse 688 * users so reject it 689 */ 690 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) 691 return EADDRNOTAVAIL; 692 s = pserialize_read_enter(); 693 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 694 (ifa = ifa_ifwithaddr(sin6tosa(addr))) == NULL) { 695 error = EADDRNOTAVAIL; 696 goto out; 697 } 698 if (ifa && (ifatoia6(ifa))->ia6_flags & 699 (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) { 700 error = EADDRNOTAVAIL; 701 goto out; 702 } 703 704 in6p->in6p_laddr = addr->sin6_addr; 705 error = 0; 706 out: 707 pserialize_read_exit(s); 708 return error; 709 } 710 711 static int 712 rip6_listen(struct socket *so, struct lwp *l) 713 { 714 KASSERT(solocked(so)); 715 716 return EOPNOTSUPP; 717 } 718 719 static int 720 rip6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) 721 { 722 struct in6pcb *in6p = sotoin6pcb(so); 723 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 724 struct in6_addr in6a; 725 struct ifnet *ifp = NULL; 726 int scope_ambiguous = 0; 727 int error = 0; 728 struct psref psref; 729 int bound; 730 731 KASSERT(solocked(so)); 732 KASSERT(in6p != NULL); 733 KASSERT(nam != NULL); 734 735 if (IFNET_READER_EMPTY()) 736 return EADDRNOTAVAIL; 737 if (addr->sin6_family != AF_INET6) 738 return EAFNOSUPPORT; 739 740 /* 741 * Application should provide a proper zone ID or the use of 742 * default zone IDs should be enabled. Unfortunately, some 743 * applications do not behave as it should, so we need a 744 * workaround. Even if an appropriate ID is not determined, 745 * we'll see if we can determine the outgoing interface. If we 746 * can, determine the zone ID based on the interface below. 747 */ 748 if (addr->sin6_scope_id == 0 && !ip6_use_defzone) 749 scope_ambiguous = 1; 750 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 751 return error; 752 753 bound = curlwp_bind(); 754 /* Source address selection. XXX: need pcblookup? */ 755 error = in6_selectsrc(addr, in6p->in6p_outputopts, 756 in6p->in6p_moptions, &in6p->in6p_route, 757 &in6p->in6p_laddr, &ifp, &psref, &in6a); 758 if (error != 0) 759 goto out; 760 /* XXX: see above */ 761 if (ifp && scope_ambiguous && 762 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 763 goto out; 764 } 765 in6p->in6p_laddr = in6a; 766 in6p->in6p_faddr = addr->sin6_addr; 767 soisconnected(so); 768 out: 769 if_put(ifp, &psref); 770 curlwp_bindx(bound); 771 return error; 772 } 773 774 static int 775 rip6_connect2(struct socket *so, struct socket *so2) 776 { 777 KASSERT(solocked(so)); 778 779 return EOPNOTSUPP; 780 } 781 782 static int 783 rip6_disconnect(struct socket *so) 784 { 785 struct in6pcb *in6p = sotoin6pcb(so); 786 787 KASSERT(solocked(so)); 788 KASSERT(in6p != NULL); 789 790 if ((so->so_state & SS_ISCONNECTED) == 0) 791 return ENOTCONN; 792 793 in6p->in6p_faddr = in6addr_any; 794 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 795 return 0; 796 } 797 798 static int 799 rip6_shutdown(struct socket *so) 800 { 801 KASSERT(solocked(so)); 802 803 /* 804 * Mark the connection as being incapable of futther input. 805 */ 806 socantsendmore(so); 807 return 0; 808 } 809 810 static int 811 rip6_abort(struct socket *so) 812 { 813 KASSERT(solocked(so)); 814 815 soisdisconnected(so); 816 rip6_detach(so); 817 return 0; 818 } 819 820 static int 821 rip6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 822 { 823 return in6_control(so, cmd, nam, ifp); 824 } 825 826 static int 827 rip6_stat(struct socket *so, struct stat *ub) 828 { 829 KASSERT(solocked(so)); 830 831 /* stat: don't bother with a blocksize */ 832 return 0; 833 } 834 835 static int 836 rip6_peeraddr(struct socket *so, struct sockaddr *nam) 837 { 838 KASSERT(solocked(so)); 839 KASSERT(sotoin6pcb(so) != NULL); 840 KASSERT(nam != NULL); 841 842 in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam); 843 return 0; 844 } 845 846 static int 847 rip6_sockaddr(struct socket *so, struct sockaddr *nam) 848 { 849 KASSERT(solocked(so)); 850 KASSERT(sotoin6pcb(so) != NULL); 851 KASSERT(nam != NULL); 852 853 in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam); 854 return 0; 855 } 856 857 static int 858 rip6_rcvd(struct socket *so, int flags, struct lwp *l) 859 { 860 KASSERT(solocked(so)); 861 862 return EOPNOTSUPP; 863 } 864 865 static int 866 rip6_recvoob(struct socket *so, struct mbuf *m, int flags) 867 { 868 KASSERT(solocked(so)); 869 870 return EOPNOTSUPP; 871 } 872 873 static int 874 rip6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam, 875 struct mbuf *control, struct lwp *l) 876 { 877 struct in6pcb *in6p = sotoin6pcb(so); 878 struct sockaddr_in6 tmp; 879 struct sockaddr_in6 *dst; 880 int error = 0; 881 882 KASSERT(solocked(so)); 883 KASSERT(in6p != NULL); 884 KASSERT(m != NULL); 885 886 /* 887 * Ship a packet out. The appropriate raw output 888 * routine handles any messaging necessary. 889 */ 890 891 /* always copy sockaddr to avoid overwrites */ 892 if (so->so_state & SS_ISCONNECTED) { 893 if (nam) { 894 error = EISCONN; 895 goto release; 896 } 897 /* XXX */ 898 sockaddr_in6_init(&tmp, &in6p->in6p_faddr, 0, 0, 0); 899 dst = &tmp; 900 } else { 901 if (nam == NULL) { 902 error = ENOTCONN; 903 goto release; 904 } 905 tmp = *(struct sockaddr_in6 *)nam; 906 dst = &tmp; 907 908 if (dst->sin6_family != AF_INET6) { 909 error = EAFNOSUPPORT; 910 goto release; 911 } 912 } 913 error = rip6_output(m, so, dst, control); 914 m = NULL; 915 916 release: 917 if (m) 918 m_freem(m); 919 920 return error; 921 } 922 923 static int 924 rip6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control) 925 { 926 KASSERT(solocked(so)); 927 928 if (m) 929 m_freem(m); 930 931 return EOPNOTSUPP; 932 } 933 934 static int 935 rip6_purgeif(struct socket *so, struct ifnet *ifp) 936 { 937 938 mutex_enter(softnet_lock); 939 in6_pcbpurgeif0(&raw6cbtable, ifp); 940 #ifdef NET_MPSAFE 941 mutex_exit(softnet_lock); 942 #endif 943 in6_purgeif(ifp); 944 #ifdef NET_MPSAFE 945 mutex_enter(softnet_lock); 946 #endif 947 in6_pcbpurgeif(&raw6cbtable, ifp); 948 mutex_exit(softnet_lock); 949 950 return 0; 951 } 952 953 static int 954 sysctl_net_inet6_raw6_stats(SYSCTLFN_ARGS) 955 { 956 957 return (NETSTAT_SYSCTL(rip6stat_percpu, RIP6_NSTATS)); 958 } 959 960 static void 961 sysctl_net_inet6_raw6_setup(struct sysctllog **clog) 962 { 963 964 sysctl_createv(clog, 0, NULL, NULL, 965 CTLFLAG_PERMANENT, 966 CTLTYPE_NODE, "inet6", NULL, 967 NULL, 0, NULL, 0, 968 CTL_NET, PF_INET6, CTL_EOL); 969 sysctl_createv(clog, 0, NULL, NULL, 970 CTLFLAG_PERMANENT, 971 CTLTYPE_NODE, "raw6", 972 SYSCTL_DESCR("Raw IPv6 settings"), 973 NULL, 0, NULL, 0, 974 CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL); 975 976 sysctl_createv(clog, 0, NULL, NULL, 977 CTLFLAG_PERMANENT, 978 CTLTYPE_STRUCT, "pcblist", 979 SYSCTL_DESCR("Raw IPv6 control block list"), 980 sysctl_inpcblist, 0, &raw6cbtable, 0, 981 CTL_NET, PF_INET6, IPPROTO_RAW, 982 CTL_CREATE, CTL_EOL); 983 sysctl_createv(clog, 0, NULL, NULL, 984 CTLFLAG_PERMANENT, 985 CTLTYPE_STRUCT, "stats", 986 SYSCTL_DESCR("Raw IPv6 statistics"), 987 sysctl_net_inet6_raw6_stats, 0, NULL, 0, 988 CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS, 989 CTL_EOL); 990 } 991 992 PR_WRAP_USRREQS(rip6) 993 #define rip6_attach rip6_attach_wrapper 994 #define rip6_detach rip6_detach_wrapper 995 #define rip6_accept rip6_accept_wrapper 996 #define rip6_bind rip6_bind_wrapper 997 #define rip6_listen rip6_listen_wrapper 998 #define rip6_connect rip6_connect_wrapper 999 #define rip6_connect2 rip6_connect2_wrapper 1000 #define rip6_disconnect rip6_disconnect_wrapper 1001 #define rip6_shutdown rip6_shutdown_wrapper 1002 #define rip6_abort rip6_abort_wrapper 1003 #define rip6_ioctl rip6_ioctl_wrapper 1004 #define rip6_stat rip6_stat_wrapper 1005 #define rip6_peeraddr rip6_peeraddr_wrapper 1006 #define rip6_sockaddr rip6_sockaddr_wrapper 1007 #define rip6_rcvd rip6_rcvd_wrapper 1008 #define rip6_recvoob rip6_recvoob_wrapper 1009 #define rip6_send rip6_send_wrapper 1010 #define rip6_sendoob rip6_sendoob_wrapper 1011 #define rip6_purgeif rip6_purgeif_wrapper 1012 1013 const struct pr_usrreqs rip6_usrreqs = { 1014 .pr_attach = rip6_attach, 1015 .pr_detach = rip6_detach, 1016 .pr_accept = rip6_accept, 1017 .pr_bind = rip6_bind, 1018 .pr_listen = rip6_listen, 1019 .pr_connect = rip6_connect, 1020 .pr_connect2 = rip6_connect2, 1021 .pr_disconnect = rip6_disconnect, 1022 .pr_shutdown = rip6_shutdown, 1023 .pr_abort = rip6_abort, 1024 .pr_ioctl = rip6_ioctl, 1025 .pr_stat = rip6_stat, 1026 .pr_peeraddr = rip6_peeraddr, 1027 .pr_sockaddr = rip6_sockaddr, 1028 .pr_rcvd = rip6_rcvd, 1029 .pr_recvoob = rip6_recvoob, 1030 .pr_send = rip6_send, 1031 .pr_sendoob = rip6_sendoob, 1032 .pr_purgeif = rip6_purgeif, 1033 }; 1034