1 /*- 2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * a) Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * b) Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the distribution. 15 * 16 * c) Neither the name of Cisco Systems, Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <netinet/sctp_os.h> 37 #ifdef INET6 38 #include <sys/proc.h> 39 #include <netinet/sctp_pcb.h> 40 #include <netinet/sctp_header.h> 41 #include <netinet/sctp_var.h> 42 #include <netinet6/sctp6_var.h> 43 #include <netinet/sctp_sysctl.h> 44 #include <netinet/sctp_output.h> 45 #include <netinet/sctp_uio.h> 46 #include <netinet/sctp_asconf.h> 47 #include <netinet/sctputil.h> 48 #include <netinet/sctp_indata.h> 49 #include <netinet/sctp_timer.h> 50 #include <netinet/sctp_auth.h> 51 #include <netinet/sctp_input.h> 52 #include <netinet/sctp_output.h> 53 #include <netinet/sctp_bsd_addr.h> 54 #include <netinet/sctp_crc32.h> 55 #include <netinet/icmp6.h> 56 #include <netinet/udp.h> 57 58 #ifdef IPSEC 59 #include <netipsec/ipsec.h> 60 #include <netipsec/ipsec6.h> 61 #endif /* IPSEC */ 62 63 extern struct protosw inetsw[]; 64 65 int 66 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port) 67 { 68 struct mbuf *m; 69 int iphlen; 70 uint32_t vrf_id; 71 uint8_t ecn_bits; 72 struct sockaddr_in6 src, dst; 73 struct ip6_hdr *ip6; 74 struct sctphdr *sh; 75 struct sctp_chunkhdr *ch; 76 int length, offset; 77 78 #if !defined(SCTP_WITH_NO_CSUM) 79 uint8_t compute_crc; 80 81 #endif 82 uint32_t mflowid; 83 uint8_t mflowtype; 84 uint16_t fibnum; 85 86 iphlen = *offp; 87 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) { 88 SCTP_RELEASE_PKT(*i_pak); 89 return (IPPROTO_DONE); 90 } 91 m = SCTP_HEADER_TO_CHAIN(*i_pak); 92 #ifdef SCTP_MBUF_LOGGING 93 /* Log in any input mbufs */ 94 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 95 sctp_log_mbc(m, SCTP_MBUF_INPUT); 96 } 97 #endif 98 #ifdef SCTP_PACKET_LOGGING 99 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) { 100 sctp_packet_log(m); 101 } 102 #endif 103 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD, 104 "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n", 105 m->m_pkthdr.len, 106 if_name(m->m_pkthdr.rcvif), 107 (int)m->m_pkthdr.csum_flags, CSUM_BITS); 108 mflowid = m->m_pkthdr.flowid; 109 mflowtype = M_HASHTYPE_GET(m); 110 fibnum = M_GETFIB(m); 111 SCTP_STAT_INCR(sctps_recvpackets); 112 SCTP_STAT_INCR_COUNTER64(sctps_inpackets); 113 /* Get IP, SCTP, and first chunk header together in the first mbuf. */ 114 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr); 115 ip6 = mtod(m, struct ip6_hdr *); 116 IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen, 117 (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))); 118 if (sh == NULL) { 119 SCTP_STAT_INCR(sctps_hdrops); 120 return (IPPROTO_DONE); 121 } 122 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); 123 offset -= sizeof(struct sctp_chunkhdr); 124 memset(&src, 0, sizeof(struct sockaddr_in6)); 125 src.sin6_family = AF_INET6; 126 src.sin6_len = sizeof(struct sockaddr_in6); 127 src.sin6_port = sh->src_port; 128 src.sin6_addr = ip6->ip6_src; 129 if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) { 130 goto out; 131 } 132 memset(&dst, 0, sizeof(struct sockaddr_in6)); 133 dst.sin6_family = AF_INET6; 134 dst.sin6_len = sizeof(struct sockaddr_in6); 135 dst.sin6_port = sh->dest_port; 136 dst.sin6_addr = ip6->ip6_dst; 137 if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) { 138 goto out; 139 } 140 length = ntohs(ip6->ip6_plen) + iphlen; 141 /* Validate mbuf chain length with IP payload length. */ 142 if (SCTP_HEADER_LEN(m) != length) { 143 SCTPDBG(SCTP_DEBUG_INPUT1, 144 "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m)); 145 SCTP_STAT_INCR(sctps_hdrops); 146 goto out; 147 } 148 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 149 goto out; 150 } 151 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff); 152 #if defined(SCTP_WITH_NO_CSUM) 153 SCTP_STAT_INCR(sctps_recvnocrc); 154 #else 155 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) { 156 SCTP_STAT_INCR(sctps_recvhwcrc); 157 compute_crc = 0; 158 } else { 159 SCTP_STAT_INCR(sctps_recvswcrc); 160 compute_crc = 1; 161 } 162 #endif 163 sctp_common_input_processing(&m, iphlen, offset, length, 164 (struct sockaddr *)&src, 165 (struct sockaddr *)&dst, 166 sh, ch, 167 #if !defined(SCTP_WITH_NO_CSUM) 168 compute_crc, 169 #endif 170 ecn_bits, 171 mflowtype, mflowid, fibnum, 172 vrf_id, port); 173 out: 174 if (m) { 175 sctp_m_freem(m); 176 } 177 return (IPPROTO_DONE); 178 } 179 180 181 int 182 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED) 183 { 184 return (sctp6_input_with_port(i_pak, offp, 0)); 185 } 186 187 void 188 sctp6_notify(struct sctp_inpcb *inp, 189 struct sctp_tcb *stcb, 190 struct sctp_nets *net, 191 uint8_t icmp6_type, 192 uint8_t icmp6_code, 193 uint16_t next_mtu) 194 { 195 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 196 struct socket *so; 197 198 #endif 199 int timer_stopped; 200 201 switch (icmp6_type) { 202 case ICMP6_DST_UNREACH: 203 if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) || 204 (icmp6_code == ICMP6_DST_UNREACH_ADMIN) || 205 (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) || 206 (icmp6_code == ICMP6_DST_UNREACH_ADDR)) { 207 /* Mark the net unreachable. */ 208 if (net->dest_state & SCTP_ADDR_REACHABLE) { 209 /* Ok that destination is not reachable */ 210 net->dest_state &= ~SCTP_ADDR_REACHABLE; 211 net->dest_state &= ~SCTP_ADDR_PF; 212 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, 213 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED); 214 } 215 } 216 SCTP_TCB_UNLOCK(stcb); 217 break; 218 case ICMP6_PARAM_PROB: 219 /* Treat it like an ABORT. */ 220 if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) { 221 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED); 222 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 223 so = SCTP_INP_SO(inp); 224 atomic_add_int(&stcb->asoc.refcnt, 1); 225 SCTP_TCB_UNLOCK(stcb); 226 SCTP_SOCKET_LOCK(so, 1); 227 SCTP_TCB_LOCK(stcb); 228 atomic_subtract_int(&stcb->asoc.refcnt, 1); 229 #endif 230 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, 231 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2); 232 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 233 SCTP_SOCKET_UNLOCK(so, 1); 234 #endif 235 } else { 236 SCTP_TCB_UNLOCK(stcb); 237 } 238 break; 239 case ICMP6_PACKET_TOO_BIG: 240 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) { 241 timer_stopped = 1; 242 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, 243 SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1); 244 } else { 245 timer_stopped = 0; 246 } 247 /* Update the path MTU. */ 248 if (net->mtu > next_mtu) { 249 net->mtu = next_mtu; 250 if (net->port) { 251 net->mtu -= sizeof(struct udphdr); 252 } 253 } 254 /* Update the association MTU */ 255 if (stcb->asoc.smallest_mtu > next_mtu) { 256 sctp_pathmtu_adjustment(stcb, next_mtu); 257 } 258 /* Finally, start the PMTU timer if it was running before. */ 259 if (timer_stopped) { 260 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net); 261 } 262 SCTP_TCB_UNLOCK(stcb); 263 break; 264 default: 265 SCTP_TCB_UNLOCK(stcb); 266 break; 267 } 268 } 269 270 void 271 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d) 272 { 273 struct ip6ctlparam *ip6cp; 274 struct sctp_inpcb *inp; 275 struct sctp_tcb *stcb; 276 struct sctp_nets *net; 277 struct sctphdr sh; 278 struct sockaddr_in6 src, dst; 279 280 if (pktdst->sa_family != AF_INET6 || 281 pktdst->sa_len != sizeof(struct sockaddr_in6)) { 282 return; 283 } 284 if ((unsigned)cmd >= PRC_NCMDS) { 285 return; 286 } 287 if (PRC_IS_REDIRECT(cmd)) { 288 d = NULL; 289 } else if (inet6ctlerrmap[cmd] == 0) { 290 return; 291 } 292 /* If the parameter is from icmp6, decode it. */ 293 if (d != NULL) { 294 ip6cp = (struct ip6ctlparam *)d; 295 } else { 296 ip6cp = (struct ip6ctlparam *)NULL; 297 } 298 299 if (ip6cp != NULL) { 300 /* 301 * XXX: We assume that when IPV6 is non NULL, M and OFF are 302 * valid. 303 */ 304 if (ip6cp->ip6c_m == NULL) { 305 return; 306 } 307 /* 308 * Check if we can safely examine the ports and the 309 * verification tag of the SCTP common header. 310 */ 311 if (ip6cp->ip6c_m->m_pkthdr.len < 312 (int32_t) (ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) { 313 return; 314 } 315 /* Copy out the port numbers and the verification tag. */ 316 bzero(&sh, sizeof(sh)); 317 m_copydata(ip6cp->ip6c_m, 318 ip6cp->ip6c_off, 319 sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t), 320 (caddr_t)&sh); 321 memset(&src, 0, sizeof(struct sockaddr_in6)); 322 src.sin6_family = AF_INET6; 323 src.sin6_len = sizeof(struct sockaddr_in6); 324 src.sin6_port = sh.src_port; 325 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src; 326 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) { 327 return; 328 } 329 memset(&dst, 0, sizeof(struct sockaddr_in6)); 330 dst.sin6_family = AF_INET6; 331 dst.sin6_len = sizeof(struct sockaddr_in6); 332 dst.sin6_port = sh.dest_port; 333 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst; 334 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) { 335 return; 336 } 337 inp = NULL; 338 net = NULL; 339 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst, 340 (struct sockaddr *)&src, 341 &inp, &net, 1, SCTP_DEFAULT_VRFID); 342 if ((stcb != NULL) && 343 (net != NULL) && 344 (inp != NULL)) { 345 /* Check the verification tag */ 346 if (ntohl(sh.v_tag) != 0) { 347 /* 348 * This must be the verification tag used 349 * for sending out packets. We don't 350 * consider packets reflecting the 351 * verification tag. 352 */ 353 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) { 354 SCTP_TCB_UNLOCK(stcb); 355 return; 356 } 357 } else { 358 if (ip6cp->ip6c_m->m_pkthdr.len >= 359 ip6cp->ip6c_off + sizeof(struct sctphdr) + 360 sizeof(struct sctp_chunkhdr) + 361 offsetof(struct sctp_init, a_rwnd)) { 362 /* 363 * In this case we can check if we 364 * got an INIT chunk and if the 365 * initiate tag matches. 366 */ 367 uint32_t initiate_tag; 368 uint8_t chunk_type; 369 370 m_copydata(ip6cp->ip6c_m, 371 ip6cp->ip6c_off + 372 sizeof(struct sctphdr), 373 sizeof(uint8_t), 374 (caddr_t)&chunk_type); 375 m_copydata(ip6cp->ip6c_m, 376 ip6cp->ip6c_off + 377 sizeof(struct sctphdr) + 378 sizeof(struct sctp_chunkhdr), 379 sizeof(uint32_t), 380 (caddr_t)&initiate_tag); 381 if ((chunk_type != SCTP_INITIATION) || 382 (ntohl(initiate_tag) != stcb->asoc.my_vtag)) { 383 SCTP_TCB_UNLOCK(stcb); 384 return; 385 } 386 } else { 387 SCTP_TCB_UNLOCK(stcb); 388 return; 389 } 390 } 391 sctp6_notify(inp, stcb, net, 392 ip6cp->ip6c_icmp6->icmp6_type, 393 ip6cp->ip6c_icmp6->icmp6_code, 394 (uint16_t) ntohl(ip6cp->ip6c_icmp6->icmp6_mtu)); 395 } else { 396 if ((stcb == NULL) && (inp != NULL)) { 397 /* reduce inp's ref-count */ 398 SCTP_INP_WLOCK(inp); 399 SCTP_INP_DECR_REF(inp); 400 SCTP_INP_WUNLOCK(inp); 401 } 402 if (stcb) { 403 SCTP_TCB_UNLOCK(stcb); 404 } 405 } 406 } 407 } 408 409 /* 410 * this routine can probably be collasped into the one in sctp_userreq.c 411 * since they do the same thing and now we lookup with a sockaddr 412 */ 413 static int 414 sctp6_getcred(SYSCTL_HANDLER_ARGS) 415 { 416 struct xucred xuc; 417 struct sockaddr_in6 addrs[2]; 418 struct sctp_inpcb *inp; 419 struct sctp_nets *net; 420 struct sctp_tcb *stcb; 421 int error; 422 uint32_t vrf_id; 423 424 vrf_id = SCTP_DEFAULT_VRFID; 425 426 error = priv_check(req->td, PRIV_NETINET_GETCRED); 427 if (error) 428 return (error); 429 430 if (req->newlen != sizeof(addrs)) { 431 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 432 return (EINVAL); 433 } 434 if (req->oldlen != sizeof(struct ucred)) { 435 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 436 return (EINVAL); 437 } 438 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 439 if (error) 440 return (error); 441 442 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]), 443 sin6tosa(&addrs[0]), 444 &inp, &net, 1, vrf_id); 445 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) { 446 if ((inp != NULL) && (stcb == NULL)) { 447 /* reduce ref-count */ 448 SCTP_INP_WLOCK(inp); 449 SCTP_INP_DECR_REF(inp); 450 goto cred_can_cont; 451 } 452 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 453 error = ENOENT; 454 goto out; 455 } 456 SCTP_TCB_UNLOCK(stcb); 457 /* 458 * We use the write lock here, only since in the error leg we need 459 * it. If we used RLOCK, then we would have to 460 * wlock/decr/unlock/rlock. Which in theory could create a hole. 461 * Better to use higher wlock. 462 */ 463 SCTP_INP_WLOCK(inp); 464 cred_can_cont: 465 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket); 466 if (error) { 467 SCTP_INP_WUNLOCK(inp); 468 goto out; 469 } 470 cru2x(inp->sctp_socket->so_cred, &xuc); 471 SCTP_INP_WUNLOCK(inp); 472 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 473 out: 474 return (error); 475 } 476 477 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW, 478 0, 0, 479 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection"); 480 481 482 /* This is the same as the sctp_abort() could be made common */ 483 static void 484 sctp6_abort(struct socket *so) 485 { 486 struct sctp_inpcb *inp; 487 uint32_t flags; 488 489 inp = (struct sctp_inpcb *)so->so_pcb; 490 if (inp == NULL) { 491 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 492 return; 493 } 494 sctp_must_try_again: 495 flags = inp->sctp_flags; 496 #ifdef SCTP_LOG_CLOSING 497 sctp_log_closing(inp, NULL, 17); 498 #endif 499 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) && 500 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) { 501 #ifdef SCTP_LOG_CLOSING 502 sctp_log_closing(inp, NULL, 16); 503 #endif 504 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT, 505 SCTP_CALLED_AFTER_CMPSET_OFCLOSE); 506 SOCK_LOCK(so); 507 SCTP_SB_CLEAR(so->so_snd); 508 /* 509 * same for the rcv ones, they are only here for the 510 * accounting/select. 511 */ 512 SCTP_SB_CLEAR(so->so_rcv); 513 /* Now null out the reference, we are completely detached. */ 514 so->so_pcb = NULL; 515 SOCK_UNLOCK(so); 516 } else { 517 flags = inp->sctp_flags; 518 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) { 519 goto sctp_must_try_again; 520 } 521 } 522 return; 523 } 524 525 static int 526 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED) 527 { 528 struct in6pcb *inp6; 529 int error; 530 struct sctp_inpcb *inp; 531 uint32_t vrf_id = SCTP_DEFAULT_VRFID; 532 533 inp = (struct sctp_inpcb *)so->so_pcb; 534 if (inp != NULL) { 535 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 536 return (EINVAL); 537 } 538 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 539 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace)); 540 if (error) 541 return (error); 542 } 543 error = sctp_inpcb_alloc(so, vrf_id); 544 if (error) 545 return (error); 546 inp = (struct sctp_inpcb *)so->so_pcb; 547 SCTP_INP_WLOCK(inp); 548 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */ 549 inp6 = (struct in6pcb *)inp; 550 551 inp6->inp_vflag |= INP_IPV6; 552 inp6->in6p_hops = -1; /* use kernel default */ 553 inp6->in6p_cksum = -1; /* just to be sure */ 554 #ifdef INET 555 /* 556 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6 557 * socket as well, because the socket may be bound to an IPv6 558 * wildcard address, which may match an IPv4-mapped IPv6 address. 559 */ 560 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl); 561 #endif 562 /* 563 * Hmm what about the IPSEC stuff that is missing here but in 564 * sctp_attach()? 565 */ 566 SCTP_INP_WUNLOCK(inp); 567 return (0); 568 } 569 570 static int 571 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p) 572 { 573 struct sctp_inpcb *inp; 574 struct in6pcb *inp6; 575 int error; 576 u_char vflagsav; 577 578 inp = (struct sctp_inpcb *)so->so_pcb; 579 if (inp == NULL) { 580 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 581 return (EINVAL); 582 } 583 if (addr) { 584 switch (addr->sa_family) { 585 #ifdef INET 586 case AF_INET: 587 if (addr->sa_len != sizeof(struct sockaddr_in)) { 588 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 589 return (EINVAL); 590 } 591 break; 592 #endif 593 #ifdef INET6 594 case AF_INET6: 595 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 596 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 597 return (EINVAL); 598 } 599 break; 600 #endif 601 default: 602 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 603 return (EINVAL); 604 } 605 } 606 inp6 = (struct in6pcb *)inp; 607 vflagsav = inp6->inp_vflag; 608 inp6->inp_vflag &= ~INP_IPV4; 609 inp6->inp_vflag |= INP_IPV6; 610 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) { 611 switch (addr->sa_family) { 612 #ifdef INET 613 case AF_INET: 614 /* binding v4 addr to v6 socket, so reset flags */ 615 inp6->inp_vflag |= INP_IPV4; 616 inp6->inp_vflag &= ~INP_IPV6; 617 break; 618 #endif 619 #ifdef INET6 620 case AF_INET6: 621 { 622 struct sockaddr_in6 *sin6_p; 623 624 sin6_p = (struct sockaddr_in6 *)addr; 625 626 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { 627 inp6->inp_vflag |= INP_IPV4; 628 } 629 #ifdef INET 630 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 631 struct sockaddr_in sin; 632 633 in6_sin6_2_sin(&sin, sin6_p); 634 inp6->inp_vflag |= INP_IPV4; 635 inp6->inp_vflag &= ~INP_IPV6; 636 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p); 637 goto out; 638 } 639 #endif 640 break; 641 } 642 #endif 643 default: 644 break; 645 } 646 } else if (addr != NULL) { 647 struct sockaddr_in6 *sin6_p; 648 649 /* IPV6_V6ONLY socket */ 650 #ifdef INET 651 if (addr->sa_family == AF_INET) { 652 /* can't bind v4 addr to v6 only socket! */ 653 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 654 error = EINVAL; 655 goto out; 656 } 657 #endif 658 sin6_p = (struct sockaddr_in6 *)addr; 659 660 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 661 /* can't bind v4-mapped addrs either! */ 662 /* NOTE: we don't support SIIT */ 663 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 664 error = EINVAL; 665 goto out; 666 } 667 } 668 error = sctp_inpcb_bind(so, addr, NULL, p); 669 out: 670 if (error != 0) 671 inp6->inp_vflag = vflagsav; 672 return (error); 673 } 674 675 676 static void 677 sctp6_close(struct socket *so) 678 { 679 sctp_close(so); 680 } 681 682 /* This could be made common with sctp_detach() since they are identical */ 683 684 static 685 int 686 sctp6_disconnect(struct socket *so) 687 { 688 return (sctp_disconnect(so)); 689 } 690 691 692 int 693 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 694 struct mbuf *control, struct thread *p); 695 696 697 static int 698 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 699 struct mbuf *control, struct thread *p) 700 { 701 struct sctp_inpcb *inp; 702 struct in6pcb *inp6; 703 704 #ifdef INET 705 struct sockaddr_in6 *sin6; 706 707 #endif /* INET */ 708 /* No SPL needed since sctp_output does this */ 709 710 inp = (struct sctp_inpcb *)so->so_pcb; 711 if (inp == NULL) { 712 if (control) { 713 SCTP_RELEASE_PKT(control); 714 control = NULL; 715 } 716 SCTP_RELEASE_PKT(m); 717 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 718 return (EINVAL); 719 } 720 inp6 = (struct in6pcb *)inp; 721 /* 722 * For the TCP model we may get a NULL addr, if we are a connected 723 * socket thats ok. 724 */ 725 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) && 726 (addr == NULL)) { 727 goto connected_type; 728 } 729 if (addr == NULL) { 730 SCTP_RELEASE_PKT(m); 731 if (control) { 732 SCTP_RELEASE_PKT(control); 733 control = NULL; 734 } 735 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ); 736 return (EDESTADDRREQ); 737 } 738 #ifdef INET 739 sin6 = (struct sockaddr_in6 *)addr; 740 if (SCTP_IPV6_V6ONLY(inp6)) { 741 /* 742 * if IPV6_V6ONLY flag, we discard datagrams destined to a 743 * v4 addr or v4-mapped addr 744 */ 745 if (addr->sa_family == AF_INET) { 746 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 747 return (EINVAL); 748 } 749 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 750 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 751 return (EINVAL); 752 } 753 } 754 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 755 struct sockaddr_in sin; 756 757 /* convert v4-mapped into v4 addr and send */ 758 in6_sin6_2_sin(&sin, sin6); 759 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p)); 760 } 761 #endif /* INET */ 762 connected_type: 763 /* now what about control */ 764 if (control) { 765 if (inp->control) { 766 SCTP_PRINTF("huh? control set?\n"); 767 SCTP_RELEASE_PKT(inp->control); 768 inp->control = NULL; 769 } 770 inp->control = control; 771 } 772 /* Place the data */ 773 if (inp->pkt) { 774 SCTP_BUF_NEXT(inp->pkt_last) = m; 775 inp->pkt_last = m; 776 } else { 777 inp->pkt_last = inp->pkt = m; 778 } 779 if ( 780 /* FreeBSD and MacOSX uses a flag passed */ 781 ((flags & PRUS_MORETOCOME) == 0) 782 ) { 783 /* 784 * note with the current version this code will only be used 785 * by OpenBSD, NetBSD and FreeBSD have methods for 786 * re-defining sosend() to use sctp_sosend(). One can 787 * optionaly switch back to this code (by changing back the 788 * defininitions but this is not advisable. 789 */ 790 int ret; 791 792 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags); 793 inp->pkt = NULL; 794 inp->control = NULL; 795 return (ret); 796 } else { 797 return (0); 798 } 799 } 800 801 static int 802 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p) 803 { 804 uint32_t vrf_id; 805 int error = 0; 806 struct sctp_inpcb *inp; 807 struct sctp_tcb *stcb; 808 809 #ifdef INET 810 struct in6pcb *inp6; 811 struct sockaddr_in6 *sin6; 812 union sctp_sockstore store; 813 814 #endif 815 816 #ifdef INET 817 inp6 = (struct in6pcb *)so->so_pcb; 818 #endif 819 inp = (struct sctp_inpcb *)so->so_pcb; 820 if (inp == NULL) { 821 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 822 return (ECONNRESET); /* I made the same as TCP since we are 823 * not setup? */ 824 } 825 if (addr == NULL) { 826 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 827 return (EINVAL); 828 } 829 switch (addr->sa_family) { 830 #ifdef INET 831 case AF_INET: 832 if (addr->sa_len != sizeof(struct sockaddr_in)) { 833 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 834 return (EINVAL); 835 } 836 break; 837 #endif 838 #ifdef INET6 839 case AF_INET6: 840 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 841 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 842 return (EINVAL); 843 } 844 break; 845 #endif 846 default: 847 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 848 return (EINVAL); 849 } 850 851 vrf_id = inp->def_vrf_id; 852 SCTP_ASOC_CREATE_LOCK(inp); 853 SCTP_INP_RLOCK(inp); 854 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 855 SCTP_PCB_FLAGS_UNBOUND) { 856 /* Bind a ephemeral port */ 857 SCTP_INP_RUNLOCK(inp); 858 error = sctp6_bind(so, NULL, p); 859 if (error) { 860 SCTP_ASOC_CREATE_UNLOCK(inp); 861 862 return (error); 863 } 864 SCTP_INP_RLOCK(inp); 865 } 866 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && 867 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) { 868 /* We are already connected AND the TCP model */ 869 SCTP_INP_RUNLOCK(inp); 870 SCTP_ASOC_CREATE_UNLOCK(inp); 871 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE); 872 return (EADDRINUSE); 873 } 874 #ifdef INET 875 sin6 = (struct sockaddr_in6 *)addr; 876 if (SCTP_IPV6_V6ONLY(inp6)) { 877 /* 878 * if IPV6_V6ONLY flag, ignore connections destined to a v4 879 * addr or v4-mapped addr 880 */ 881 if (addr->sa_family == AF_INET) { 882 SCTP_INP_RUNLOCK(inp); 883 SCTP_ASOC_CREATE_UNLOCK(inp); 884 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 885 return (EINVAL); 886 } 887 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 888 SCTP_INP_RUNLOCK(inp); 889 SCTP_ASOC_CREATE_UNLOCK(inp); 890 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 891 return (EINVAL); 892 } 893 } 894 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 895 /* convert v4-mapped into v4 addr */ 896 in6_sin6_2_sin(&store.sin, sin6); 897 addr = &store.sa; 898 } 899 #endif /* INET */ 900 /* Now do we connect? */ 901 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 902 stcb = LIST_FIRST(&inp->sctp_asoc_list); 903 if (stcb) { 904 SCTP_TCB_UNLOCK(stcb); 905 } 906 SCTP_INP_RUNLOCK(inp); 907 } else { 908 SCTP_INP_RUNLOCK(inp); 909 SCTP_INP_WLOCK(inp); 910 SCTP_INP_INCR_REF(inp); 911 SCTP_INP_WUNLOCK(inp); 912 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL); 913 if (stcb == NULL) { 914 SCTP_INP_WLOCK(inp); 915 SCTP_INP_DECR_REF(inp); 916 SCTP_INP_WUNLOCK(inp); 917 } 918 } 919 920 if (stcb != NULL) { 921 /* Already have or am bring up an association */ 922 SCTP_ASOC_CREATE_UNLOCK(inp); 923 SCTP_TCB_UNLOCK(stcb); 924 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY); 925 return (EALREADY); 926 } 927 /* We are GOOD to go */ 928 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, 929 inp->sctp_ep.pre_open_stream_count, 930 inp->sctp_ep.port, p); 931 SCTP_ASOC_CREATE_UNLOCK(inp); 932 if (stcb == NULL) { 933 /* Gak! no memory */ 934 return (error); 935 } 936 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) { 937 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; 938 /* Set the connected flag so we can queue data */ 939 soisconnecting(so); 940 } 941 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT; 942 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered); 943 944 /* initialize authentication parameters for the assoc */ 945 sctp_initialize_auth_params(inp, stcb); 946 947 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED); 948 SCTP_TCB_UNLOCK(stcb); 949 return (error); 950 } 951 952 static int 953 sctp6_getaddr(struct socket *so, struct sockaddr **addr) 954 { 955 struct sockaddr_in6 *sin6; 956 struct sctp_inpcb *inp; 957 uint32_t vrf_id; 958 struct sctp_ifa *sctp_ifa; 959 960 int error; 961 962 /* 963 * Do the malloc first in case it blocks. 964 */ 965 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6)); 966 if (sin6 == NULL) 967 return (ENOMEM); 968 sin6->sin6_family = AF_INET6; 969 sin6->sin6_len = sizeof(*sin6); 970 971 inp = (struct sctp_inpcb *)so->so_pcb; 972 if (inp == NULL) { 973 SCTP_FREE_SONAME(sin6); 974 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 975 return (ECONNRESET); 976 } 977 SCTP_INP_RLOCK(inp); 978 sin6->sin6_port = inp->sctp_lport; 979 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { 980 /* For the bound all case you get back 0 */ 981 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { 982 struct sctp_tcb *stcb; 983 struct sockaddr_in6 *sin_a6; 984 struct sctp_nets *net; 985 int fnd; 986 987 stcb = LIST_FIRST(&inp->sctp_asoc_list); 988 if (stcb == NULL) { 989 SCTP_INP_RUNLOCK(inp); 990 SCTP_FREE_SONAME(sin6); 991 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 992 return (ENOENT); 993 } 994 fnd = 0; 995 sin_a6 = NULL; 996 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 997 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 998 if (sin_a6 == NULL) 999 /* this will make coverity happy */ 1000 continue; 1001 1002 if (sin_a6->sin6_family == AF_INET6) { 1003 fnd = 1; 1004 break; 1005 } 1006 } 1007 if ((!fnd) || (sin_a6 == NULL)) { 1008 /* punt */ 1009 SCTP_INP_RUNLOCK(inp); 1010 SCTP_FREE_SONAME(sin6); 1011 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1012 return (ENOENT); 1013 } 1014 vrf_id = inp->def_vrf_id; 1015 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id); 1016 if (sctp_ifa) { 1017 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr; 1018 } 1019 } else { 1020 /* For the bound all case you get back 0 */ 1021 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr)); 1022 } 1023 } else { 1024 /* Take the first IPv6 address in the list */ 1025 struct sctp_laddr *laddr; 1026 int fnd = 0; 1027 1028 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { 1029 if (laddr->ifa->address.sa.sa_family == AF_INET6) { 1030 struct sockaddr_in6 *sin_a; 1031 1032 sin_a = &laddr->ifa->address.sin6; 1033 sin6->sin6_addr = sin_a->sin6_addr; 1034 fnd = 1; 1035 break; 1036 } 1037 } 1038 if (!fnd) { 1039 SCTP_FREE_SONAME(sin6); 1040 SCTP_INP_RUNLOCK(inp); 1041 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1042 return (ENOENT); 1043 } 1044 } 1045 SCTP_INP_RUNLOCK(inp); 1046 /* Scoping things for v6 */ 1047 if ((error = sa6_recoverscope(sin6)) != 0) { 1048 SCTP_FREE_SONAME(sin6); 1049 return (error); 1050 } 1051 (*addr) = (struct sockaddr *)sin6; 1052 return (0); 1053 } 1054 1055 static int 1056 sctp6_peeraddr(struct socket *so, struct sockaddr **addr) 1057 { 1058 struct sockaddr_in6 *sin6; 1059 int fnd; 1060 struct sockaddr_in6 *sin_a6; 1061 struct sctp_inpcb *inp; 1062 struct sctp_tcb *stcb; 1063 struct sctp_nets *net; 1064 int error; 1065 1066 /* Do the malloc first in case it blocks. */ 1067 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1068 if (sin6 == NULL) 1069 return (ENOMEM); 1070 sin6->sin6_family = AF_INET6; 1071 sin6->sin6_len = sizeof(*sin6); 1072 1073 inp = (struct sctp_inpcb *)so->so_pcb; 1074 if ((inp == NULL) || 1075 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) { 1076 /* UDP type and listeners will drop out here */ 1077 SCTP_FREE_SONAME(sin6); 1078 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN); 1079 return (ENOTCONN); 1080 } 1081 SCTP_INP_RLOCK(inp); 1082 stcb = LIST_FIRST(&inp->sctp_asoc_list); 1083 if (stcb) { 1084 SCTP_TCB_LOCK(stcb); 1085 } 1086 SCTP_INP_RUNLOCK(inp); 1087 if (stcb == NULL) { 1088 SCTP_FREE_SONAME(sin6); 1089 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET); 1090 return (ECONNRESET); 1091 } 1092 fnd = 0; 1093 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { 1094 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr; 1095 if (sin_a6->sin6_family == AF_INET6) { 1096 fnd = 1; 1097 sin6->sin6_port = stcb->rport; 1098 sin6->sin6_addr = sin_a6->sin6_addr; 1099 break; 1100 } 1101 } 1102 SCTP_TCB_UNLOCK(stcb); 1103 if (!fnd) { 1104 /* No IPv4 address */ 1105 SCTP_FREE_SONAME(sin6); 1106 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT); 1107 return (ENOENT); 1108 } 1109 if ((error = sa6_recoverscope(sin6)) != 0) { 1110 SCTP_FREE_SONAME(sin6); 1111 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error); 1112 return (error); 1113 } 1114 *addr = (struct sockaddr *)sin6; 1115 return (0); 1116 } 1117 1118 static int 1119 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam) 1120 { 1121 struct in6pcb *inp6 = sotoin6pcb(so); 1122 int error; 1123 1124 if (inp6 == NULL) { 1125 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1126 return (EINVAL); 1127 } 1128 /* allow v6 addresses precedence */ 1129 error = sctp6_getaddr(so, nam); 1130 #ifdef INET 1131 if (error) { 1132 struct sockaddr_in6 *sin6; 1133 1134 /* try v4 next if v6 failed */ 1135 error = sctp_ingetaddr(so, nam); 1136 if (error) { 1137 return (error); 1138 } 1139 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1140 if (sin6 == NULL) { 1141 SCTP_FREE_SONAME(*nam); 1142 return (ENOMEM); 1143 } 1144 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6); 1145 SCTP_FREE_SONAME(*nam); 1146 *nam = (struct sockaddr *)sin6; 1147 } 1148 #endif 1149 return (error); 1150 } 1151 1152 1153 static int 1154 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam) 1155 { 1156 struct in6pcb *inp6 = sotoin6pcb(so); 1157 int error; 1158 1159 if (inp6 == NULL) { 1160 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL); 1161 return (EINVAL); 1162 } 1163 /* allow v6 addresses precedence */ 1164 error = sctp6_peeraddr(so, nam); 1165 #ifdef INET 1166 if (error) { 1167 struct sockaddr_in6 *sin6; 1168 1169 /* try v4 next if v6 failed */ 1170 error = sctp_peeraddr(so, nam); 1171 if (error) { 1172 return (error); 1173 } 1174 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6); 1175 if (sin6 == NULL) { 1176 SCTP_FREE_SONAME(*nam); 1177 return (ENOMEM); 1178 } 1179 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6); 1180 SCTP_FREE_SONAME(*nam); 1181 *nam = (struct sockaddr *)sin6; 1182 } 1183 #endif 1184 return (error); 1185 } 1186 1187 struct pr_usrreqs sctp6_usrreqs = { 1188 .pru_abort = sctp6_abort, 1189 .pru_accept = sctp_accept, 1190 .pru_attach = sctp6_attach, 1191 .pru_bind = sctp6_bind, 1192 .pru_connect = sctp6_connect, 1193 .pru_control = in6_control, 1194 .pru_close = sctp6_close, 1195 .pru_detach = sctp6_close, 1196 .pru_sopoll = sopoll_generic, 1197 .pru_flush = sctp_flush, 1198 .pru_disconnect = sctp6_disconnect, 1199 .pru_listen = sctp_listen, 1200 .pru_peeraddr = sctp6_getpeeraddr, 1201 .pru_send = sctp6_send, 1202 .pru_shutdown = sctp_shutdown, 1203 .pru_sockaddr = sctp6_in6getaddr, 1204 .pru_sosend = sctp_sosend, 1205 .pru_soreceive = sctp_soreceive 1206 }; 1207 1208 #endif 1209