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