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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_pcb.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_var.h>
42 #include <netinet6/sctp6_var.h>
44 #include <netinet/sctp_sysctl.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_input.h>
53 #include <netinet/sctp_output.h>
54 #include <netinet/sctp_bsd_addr.h>
55 #include <netinet/sctp_crc32.h>
56 #include <netinet/udp.h>
59 #include <netipsec/ipsec.h>
61 #include <netipsec/ipsec6.h>
65 extern struct protosw inetsw[];
68 sctp6_input(struct mbuf **i_pak, int *offp, int proto)
73 struct sctp_inpcb *in6p = NULL;
74 struct sctp_nets *net;
79 struct inpcb *in6p_ip;
82 struct sctp_chunkhdr *ch;
83 int length, offset, iphlen;
85 struct sctp_tcb *stcb = NULL;
90 #if !defined(SCTP_WITH_NO_CSUM)
91 uint32_t check, calc_check;
97 /* get the VRF and table id's */
98 if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
99 SCTP_RELEASE_PKT(*i_pak);
102 m = SCTP_HEADER_TO_CHAIN(*i_pak);
103 pkt_len = SCTP_HEADER_LEN((*i_pak));
105 #ifdef SCTP_PACKET_LOGGING
106 sctp_packet_log(m, pkt_len);
108 if (m->m_flags & M_FLOWID) {
109 mflowid = m->m_pkthdr.flowid;
115 ip6 = mtod(m, struct ip6_hdr *);
116 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
117 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
118 (int)(sizeof(*sh) + sizeof(*ch)));
120 SCTP_STAT_INCR(sctps_hdrops);
121 return (IPPROTO_DONE);
123 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
125 offset = iphlen + sizeof(*sh) + sizeof(*ch);
126 SCTPDBG(SCTP_DEBUG_INPUT1,
127 "sctp6_input() length:%d iphlen:%d\n", pkt_len, iphlen);
130 #if defined(NFAITH) && NFAITH > 0
132 if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
133 /* XXX send icmp6 host/port unreach? */
136 #endif /* NFAITH defined and > 0 */
137 SCTP_STAT_INCR(sctps_recvpackets);
138 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
139 SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
141 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
142 /* No multi-cast support in SCTP */
145 /* destination port of 0 is illegal, based on RFC2960. */
146 if (sh->dest_port == 0)
149 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
150 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
152 if_name(m->m_pkthdr.rcvif),
153 m->m_pkthdr.csum_flags);
154 #if defined(SCTP_WITH_NO_CSUM)
155 SCTP_STAT_INCR(sctps_recvnocrc);
157 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
158 SCTP_STAT_INCR(sctps_recvhwcrc);
161 check = sh->checksum; /* save incoming checksum */
162 sh->checksum = 0; /* prepare for calc */
163 calc_check = sctp_calculate_cksum(m, iphlen);
164 SCTP_STAT_INCR(sctps_recvswcrc);
165 if (calc_check != check) {
166 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p phlen:%d\n",
167 calc_check, check, m, iphlen);
168 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
169 sh, ch, &in6p, &net, vrf_id);
170 if ((net) && (port)) {
171 if (net->port == 0) {
172 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
176 if ((net != NULL) && (use_mflowid != 0)) {
177 net->flowid = mflowid;
182 /* in6p's ref-count increased && stcb locked */
183 if ((in6p) && (stcb)) {
184 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
185 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
186 } else if ((in6p != NULL) && (stcb == NULL)) {
189 SCTP_STAT_INCR(sctps_badsum);
190 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
193 sh->checksum = calc_check;
199 * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
200 * IP/SCTP/first chunk header...
202 stcb = sctp_findassociation_addr(m, offset - sizeof(*ch),
203 sh, ch, &in6p, &net, vrf_id);
204 if ((net) && (port)) {
205 if (net->port == 0) {
206 sctp_pathmtu_adjustment(stcb, net->mtu - sizeof(struct udphdr));
210 if ((net != NULL) && (use_mflowid != 0)) {
211 net->flowid = mflowid;
216 /* in6p's ref-count increased */
218 struct sctp_init_chunk *init_chk, chunk_buf;
220 SCTP_STAT_INCR(sctps_noport);
221 if (ch->chunk_type == SCTP_INITIATION) {
223 * we do a trick here to get the INIT tag, dig in
224 * and get the tag from the INIT and put it in the
227 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
228 iphlen + sizeof(*sh), sizeof(*init_chk),
229 (uint8_t *) & chunk_buf);
231 sh->v_tag = init_chk->init.initiate_tag;
235 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
236 sctp_send_shutdown_complete2(m, sh,
237 use_mflowid, mflowid,
241 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
244 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
245 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
246 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
247 (ch->chunk_type != SCTP_INIT))) {
248 sctp_send_abort(m, iphlen, sh, 0, NULL,
249 use_mflowid, mflowid,
254 } else if (stcb == NULL) {
259 * Check AH/ESP integrity.
261 in6p_ip = (struct inpcb *)in6p;
262 if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
264 MODULE_GLOBAL(ipsec6stat).in_polvio++;
270 * CONTROL chunk processing
272 offset -= sizeof(*ch);
273 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
275 /* Length now holds the total packet length payload + iphlen */
276 length = ntohs(ip6->ip6_plen) + iphlen;
278 /* sa_ignore NO_NULL_CHK */
279 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
280 in6p, stcb, net, ecn_bits,
281 use_mflowid, mflowid,
283 /* inp's ref-count reduced && stcb unlocked */
284 /* XXX this stuff below gets moved to appropriate parts later... */
287 if ((in6p) && refcount_up) {
288 /* reduce ref-count */
289 SCTP_INP_WLOCK(in6p);
290 SCTP_INP_DECR_REF(in6p);
291 SCTP_INP_WUNLOCK(in6p);
293 return (IPPROTO_DONE);
297 SCTP_TCB_UNLOCK(stcb);
299 if ((in6p) && refcount_up) {
300 /* reduce ref-count */
301 SCTP_INP_WLOCK(in6p);
302 SCTP_INP_DECR_REF(in6p);
303 SCTP_INP_WUNLOCK(in6p);
307 return (IPPROTO_DONE);
312 sctp6_notify_mbuf(struct sctp_inpcb *inp, struct icmp6_hdr *icmp6,
313 struct sctphdr *sh, struct sctp_tcb *stcb, struct sctp_nets *net)
317 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
318 (icmp6 == NULL) || (sh == NULL)) {
321 /* First do we even look at it? */
322 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
325 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
326 /* not PACKET TO BIG */
330 * ok we need to look closely. We could even get smarter and look at
331 * anyone that we sent to in case we get a different ICMP that tells
332 * us there is no way to reach a host, but for this impl, all we
333 * care about is MTU discovery.
335 nxtsz = ntohl(icmp6->icmp6_mtu);
336 /* Stop any PMTU timer */
337 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
339 /* Adjust destination size limit */
340 if (net->mtu > nxtsz) {
343 net->mtu -= sizeof(struct udphdr);
346 /* now what about the ep? */
347 if (stcb->asoc.smallest_mtu > nxtsz) {
348 struct sctp_tmit_chunk *chk;
350 /* Adjust that too */
351 stcb->asoc.smallest_mtu = nxtsz;
352 /* now off to subtract IP_DF flag if needed */
354 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
355 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
356 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
359 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
360 if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
362 * For this guy we also mark for immediate
363 * resend since we sent to big of chunk
365 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
366 if (chk->sent != SCTP_DATAGRAM_RESEND)
367 stcb->asoc.sent_queue_retran_cnt++;
368 chk->sent = SCTP_DATAGRAM_RESEND;
369 chk->rec.data.doing_fast_retransmit = 0;
371 chk->sent = SCTP_DATAGRAM_RESEND;
372 /* Clear any time so NO RTT is being done */
373 chk->sent_rcv_time.tv_sec = 0;
374 chk->sent_rcv_time.tv_usec = 0;
375 stcb->asoc.total_flight -= chk->send_size;
376 net->flight_size -= chk->send_size;
380 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
383 SCTP_TCB_UNLOCK(stcb);
389 sctp6_notify(struct sctp_inpcb *inp,
390 struct icmp6_hdr *icmph,
393 struct sctp_tcb *stcb,
394 struct sctp_nets *net)
396 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
402 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
403 (sh == NULL) || (to == NULL)) {
405 SCTP_TCB_UNLOCK(stcb);
408 /* First job is to verify the vtag matches what I would send */
409 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
410 SCTP_TCB_UNLOCK(stcb);
413 if (icmph->icmp6_type != ICMP_UNREACH) {
414 /* We only care about unreachable */
415 SCTP_TCB_UNLOCK(stcb);
418 if ((icmph->icmp6_code == ICMP_UNREACH_NET) ||
419 (icmph->icmp6_code == ICMP_UNREACH_HOST) ||
420 (icmph->icmp6_code == ICMP_UNREACH_NET_UNKNOWN) ||
421 (icmph->icmp6_code == ICMP_UNREACH_HOST_UNKNOWN) ||
422 (icmph->icmp6_code == ICMP_UNREACH_ISOLATED) ||
423 (icmph->icmp6_code == ICMP_UNREACH_NET_PROHIB) ||
424 (icmph->icmp6_code == ICMP_UNREACH_HOST_PROHIB) ||
425 (icmph->icmp6_code == ICMP_UNREACH_FILTER_PROHIB)) {
428 * Hmm reachablity problems we must examine closely. If its
429 * not reachable, we may have lost a network. Or if there is
430 * NO protocol at the other end named SCTP. well we consider
433 if (net->dest_state & SCTP_ADDR_REACHABLE) {
434 /* Ok that destination is NOT reachable */
435 net->dest_state &= ~SCTP_ADDR_REACHABLE;
436 net->dest_state &= ~SCTP_ADDR_PF;
437 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
438 stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
440 SCTP_TCB_UNLOCK(stcb);
441 } else if ((icmph->icmp6_code == ICMP_UNREACH_PROTOCOL) ||
442 (icmph->icmp6_code == ICMP_UNREACH_PORT)) {
444 * Here the peer is either playing tricks on us, including
445 * an address that belongs to someone who does not support
446 * SCTP OR was a userland implementation that shutdown and
447 * now is dead. In either case treat it like a OOTB abort
450 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
451 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
452 so = SCTP_INP_SO(inp);
453 atomic_add_int(&stcb->asoc.refcnt, 1);
454 SCTP_TCB_UNLOCK(stcb);
455 SCTP_SOCKET_LOCK(so, 1);
457 atomic_subtract_int(&stcb->asoc.refcnt, 1);
459 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
460 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
461 SCTP_SOCKET_UNLOCK(so, 1);
462 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
464 /* no need to unlock here, since the TCB is gone */
466 SCTP_TCB_UNLOCK(stcb);
473 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
476 struct ip6ctlparam *ip6cp = NULL;
479 vrf_id = SCTP_DEFAULT_VRFID;
481 if (pktdst->sa_family != AF_INET6 ||
482 pktdst->sa_len != sizeof(struct sockaddr_in6))
485 if ((unsigned)cmd >= PRC_NCMDS)
487 if (PRC_IS_REDIRECT(cmd)) {
489 } else if (inet6ctlerrmap[cmd] == 0) {
492 /* if the parameter is from icmp6, decode it. */
494 ip6cp = (struct ip6ctlparam *)d;
496 ip6cp = (struct ip6ctlparam *)NULL;
501 * XXX: We assume that when IPV6 is non NULL, M and OFF are
504 /* check if we can safely examine src and dst ports */
505 struct sctp_inpcb *inp = NULL;
506 struct sctp_tcb *stcb = NULL;
507 struct sctp_nets *net = NULL;
508 struct sockaddr_in6 final;
510 if (ip6cp->ip6c_m == NULL)
513 bzero(&sh, sizeof(sh));
514 bzero(&final, sizeof(final));
517 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
519 ip6cp->ip6c_src->sin6_port = sh.src_port;
520 final.sin6_len = sizeof(final);
521 final.sin6_family = AF_INET6;
522 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
523 final.sin6_port = sh.dest_port;
524 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
525 (struct sockaddr *)&final,
526 &inp, &net, 1, vrf_id);
527 /* inp's ref-count increased && stcb locked */
528 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
529 if (cmd == PRC_MSGSIZE) {
530 sctp6_notify_mbuf(inp,
535 /* inp's ref-count reduced && stcb unlocked */
537 sctp6_notify(inp, ip6cp->ip6c_icmp6, &sh,
538 (struct sockaddr *)&final,
540 /* inp's ref-count reduced && stcb unlocked */
543 if (PRC_IS_REDIRECT(cmd) && inp) {
544 in6_rtchange((struct in6pcb *)inp,
545 inet6ctlerrmap[cmd]);
548 /* reduce inp's ref-count */
550 SCTP_INP_DECR_REF(inp);
551 SCTP_INP_WUNLOCK(inp);
554 SCTP_TCB_UNLOCK(stcb);
560 * this routine can probably be collasped into the one in sctp_userreq.c
561 * since they do the same thing and now we lookup with a sockaddr
564 sctp6_getcred(SYSCTL_HANDLER_ARGS)
567 struct sockaddr_in6 addrs[2];
568 struct sctp_inpcb *inp;
569 struct sctp_nets *net;
570 struct sctp_tcb *stcb;
574 vrf_id = SCTP_DEFAULT_VRFID;
576 error = priv_check(req->td, PRIV_NETINET_GETCRED);
580 if (req->newlen != sizeof(addrs)) {
581 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
584 if (req->oldlen != sizeof(struct ucred)) {
585 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
588 error = SYSCTL_IN(req, addrs, sizeof(addrs));
592 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
594 &inp, &net, 1, vrf_id);
595 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
596 if ((inp != NULL) && (stcb == NULL)) {
597 /* reduce ref-count */
599 SCTP_INP_DECR_REF(inp);
602 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
606 SCTP_TCB_UNLOCK(stcb);
608 * We use the write lock here, only since in the error leg we need
609 * it. If we used RLOCK, then we would have to
610 * wlock/decr/unlock/rlock. Which in theory could create a hole.
611 * Better to use higher wlock.
615 error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
617 SCTP_INP_WUNLOCK(inp);
620 cru2x(inp->sctp_socket->so_cred, &xuc);
621 SCTP_INP_WUNLOCK(inp);
622 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
627 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
629 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
632 /* This is the same as the sctp_abort() could be made common */
634 sctp6_abort(struct socket *so)
636 struct sctp_inpcb *inp;
639 inp = (struct sctp_inpcb *)so->so_pcb;
641 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
645 flags = inp->sctp_flags;
646 #ifdef SCTP_LOG_CLOSING
647 sctp_log_closing(inp, NULL, 17);
649 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
650 (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
651 #ifdef SCTP_LOG_CLOSING
652 sctp_log_closing(inp, NULL, 16);
654 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
655 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
657 SCTP_SB_CLEAR(so->so_snd);
659 * same for the rcv ones, they are only here for the
662 SCTP_SB_CLEAR(so->so_rcv);
663 /* Now null out the reference, we are completely detached. */
667 flags = inp->sctp_flags;
668 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
669 goto sctp_must_try_again;
676 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
680 struct sctp_inpcb *inp;
681 uint32_t vrf_id = SCTP_DEFAULT_VRFID;
683 inp = (struct sctp_inpcb *)so->so_pcb;
685 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
688 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
689 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
693 error = sctp_inpcb_alloc(so, vrf_id);
696 inp = (struct sctp_inpcb *)so->so_pcb;
698 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
699 inp6 = (struct in6pcb *)inp;
701 inp6->inp_vflag |= INP_IPV6;
702 inp6->in6p_hops = -1; /* use kernel default */
703 inp6->in6p_cksum = -1; /* just to be sure */
706 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
707 * socket as well, because the socket may be bound to an IPv6
708 * wildcard address, which may match an IPv4-mapped IPv6 address.
710 inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
713 * Hmm what about the IPSEC stuff that is missing here but in
716 SCTP_INP_WUNLOCK(inp);
721 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
723 struct sctp_inpcb *inp;
727 inp = (struct sctp_inpcb *)so->so_pcb;
729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
733 switch (addr->sa_family) {
736 if (addr->sa_len != sizeof(struct sockaddr_in)) {
737 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
744 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
745 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
751 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
755 inp6 = (struct in6pcb *)inp;
756 inp6->inp_vflag &= ~INP_IPV4;
757 inp6->inp_vflag |= INP_IPV6;
758 if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
759 switch (addr->sa_family) {
762 /* binding v4 addr to v6 socket, so reset flags */
763 inp6->inp_vflag |= INP_IPV4;
764 inp6->inp_vflag &= ~INP_IPV6;
770 struct sockaddr_in6 *sin6_p;
772 sin6_p = (struct sockaddr_in6 *)addr;
774 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
775 inp6->inp_vflag |= INP_IPV4;
778 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
779 struct sockaddr_in sin;
781 in6_sin6_2_sin(&sin, sin6_p);
782 inp6->inp_vflag |= INP_IPV4;
783 inp6->inp_vflag &= ~INP_IPV6;
784 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
794 } else if (addr != NULL) {
795 struct sockaddr_in6 *sin6_p;
797 /* IPV6_V6ONLY socket */
799 if (addr->sa_family == AF_INET) {
800 /* can't bind v4 addr to v6 only socket! */
801 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
805 sin6_p = (struct sockaddr_in6 *)addr;
807 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
808 /* can't bind v4-mapped addrs either! */
809 /* NOTE: we don't support SIIT */
810 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
814 error = sctp_inpcb_bind(so, addr, NULL, p);
820 sctp6_close(struct socket *so)
825 /* This could be made common with sctp_detach() since they are identical */
829 sctp6_disconnect(struct socket *so)
831 return (sctp_disconnect(so));
836 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
837 struct mbuf *control, struct thread *p);
841 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
842 struct mbuf *control, struct thread *p)
844 struct sctp_inpcb *inp;
848 struct sockaddr_in6 *sin6;
851 /* No SPL needed since sctp_output does this */
853 inp = (struct sctp_inpcb *)so->so_pcb;
856 SCTP_RELEASE_PKT(control);
860 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
863 inp6 = (struct in6pcb *)inp;
865 * For the TCP model we may get a NULL addr, if we are a connected
868 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
875 SCTP_RELEASE_PKT(control);
878 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
879 return (EDESTADDRREQ);
882 sin6 = (struct sockaddr_in6 *)addr;
883 if (SCTP_IPV6_V6ONLY(inp6)) {
885 * if IPV6_V6ONLY flag, we discard datagrams destined to a
886 * v4 addr or v4-mapped addr
888 if (addr->sa_family == AF_INET) {
889 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
892 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
893 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
897 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
898 if (!MODULE_GLOBAL(ip6_v6only)) {
899 struct sockaddr_in sin;
901 /* convert v4-mapped into v4 addr and send */
902 in6_sin6_2_sin(&sin, sin6);
903 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
906 /* mapped addresses aren't enabled */
907 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
913 /* now what about control */
916 SCTP_PRINTF("huh? control set?\n");
917 SCTP_RELEASE_PKT(inp->control);
920 inp->control = control;
924 SCTP_BUF_NEXT(inp->pkt_last) = m;
927 inp->pkt_last = inp->pkt = m;
930 /* FreeBSD and MacOSX uses a flag passed */
931 ((flags & PRUS_MORETOCOME) == 0)
934 * note with the current version this code will only be used
935 * by OpenBSD, NetBSD and FreeBSD have methods for
936 * re-defining sosend() to use sctp_sosend(). One can
937 * optionaly switch back to this code (by changing back the
938 * defininitions but this is not advisable.
942 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
952 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
956 struct sctp_inpcb *inp;
958 struct sctp_tcb *stcb;
961 struct sockaddr_in6 *sin6;
962 struct sockaddr_storage ss;
966 inp6 = (struct in6pcb *)so->so_pcb;
967 inp = (struct sctp_inpcb *)so->so_pcb;
969 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
970 return (ECONNRESET); /* I made the same as TCP since we are
974 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
977 switch (addr->sa_family) {
980 if (addr->sa_len != sizeof(struct sockaddr_in)) {
981 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
988 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
989 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
995 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
999 vrf_id = inp->def_vrf_id;
1000 SCTP_ASOC_CREATE_LOCK(inp);
1001 SCTP_INP_RLOCK(inp);
1002 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1003 SCTP_PCB_FLAGS_UNBOUND) {
1004 /* Bind a ephemeral port */
1005 SCTP_INP_RUNLOCK(inp);
1006 error = sctp6_bind(so, NULL, p);
1008 SCTP_ASOC_CREATE_UNLOCK(inp);
1012 SCTP_INP_RLOCK(inp);
1014 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1015 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1016 /* We are already connected AND the TCP model */
1017 SCTP_INP_RUNLOCK(inp);
1018 SCTP_ASOC_CREATE_UNLOCK(inp);
1019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1020 return (EADDRINUSE);
1023 sin6 = (struct sockaddr_in6 *)addr;
1024 if (SCTP_IPV6_V6ONLY(inp6)) {
1026 * if IPV6_V6ONLY flag, ignore connections destined to a v4
1027 * addr or v4-mapped addr
1029 if (addr->sa_family == AF_INET) {
1030 SCTP_INP_RUNLOCK(inp);
1031 SCTP_ASOC_CREATE_UNLOCK(inp);
1032 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1035 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1036 SCTP_INP_RUNLOCK(inp);
1037 SCTP_ASOC_CREATE_UNLOCK(inp);
1038 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1042 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1043 if (!MODULE_GLOBAL(ip6_v6only)) {
1044 /* convert v4-mapped into v4 addr */
1045 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1046 addr = (struct sockaddr *)&ss;
1048 /* mapped addresses aren't enabled */
1049 SCTP_INP_RUNLOCK(inp);
1050 SCTP_ASOC_CREATE_UNLOCK(inp);
1051 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1056 /* Now do we connect? */
1057 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1058 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1060 SCTP_TCB_UNLOCK(stcb);
1062 SCTP_INP_RUNLOCK(inp);
1064 SCTP_INP_RUNLOCK(inp);
1065 SCTP_INP_WLOCK(inp);
1066 SCTP_INP_INCR_REF(inp);
1067 SCTP_INP_WUNLOCK(inp);
1068 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1070 SCTP_INP_WLOCK(inp);
1071 SCTP_INP_DECR_REF(inp);
1072 SCTP_INP_WUNLOCK(inp);
1077 /* Already have or am bring up an association */
1078 SCTP_ASOC_CREATE_UNLOCK(inp);
1079 SCTP_TCB_UNLOCK(stcb);
1080 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1083 /* We are GOOD to go */
1084 stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
1085 SCTP_ASOC_CREATE_UNLOCK(inp);
1087 /* Gak! no memory */
1090 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1091 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1092 /* Set the connected flag so we can queue data */
1095 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1096 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1098 /* initialize authentication parameters for the assoc */
1099 sctp_initialize_auth_params(inp, stcb);
1101 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1102 SCTP_TCB_UNLOCK(stcb);
1107 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1109 struct sockaddr_in6 *sin6;
1110 struct sctp_inpcb *inp;
1112 struct sctp_ifa *sctp_ifa;
1117 * Do the malloc first in case it blocks.
1119 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1122 sin6->sin6_family = AF_INET6;
1123 sin6->sin6_len = sizeof(*sin6);
1125 inp = (struct sctp_inpcb *)so->so_pcb;
1127 SCTP_FREE_SONAME(sin6);
1128 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1129 return (ECONNRESET);
1131 SCTP_INP_RLOCK(inp);
1132 sin6->sin6_port = inp->sctp_lport;
1133 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1134 /* For the bound all case you get back 0 */
1135 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1136 struct sctp_tcb *stcb;
1137 struct sockaddr_in6 *sin_a6;
1138 struct sctp_nets *net;
1141 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1147 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1148 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1150 /* this will make coverity happy */
1153 if (sin_a6->sin6_family == AF_INET6) {
1158 if ((!fnd) || (sin_a6 == NULL)) {
1162 vrf_id = inp->def_vrf_id;
1163 sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1165 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1168 /* For the bound all case you get back 0 */
1170 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1173 /* Take the first IPv6 address in the list */
1174 struct sctp_laddr *laddr;
1177 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1178 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1179 struct sockaddr_in6 *sin_a;
1181 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1182 sin6->sin6_addr = sin_a->sin6_addr;
1188 SCTP_FREE_SONAME(sin6);
1189 SCTP_INP_RUNLOCK(inp);
1190 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1194 SCTP_INP_RUNLOCK(inp);
1195 /* Scoping things for v6 */
1196 if ((error = sa6_recoverscope(sin6)) != 0) {
1197 SCTP_FREE_SONAME(sin6);
1200 (*addr) = (struct sockaddr *)sin6;
1205 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1207 struct sockaddr_in6 *sin6;
1209 struct sockaddr_in6 *sin_a6;
1210 struct sctp_inpcb *inp;
1211 struct sctp_tcb *stcb;
1212 struct sctp_nets *net;
1215 /* Do the malloc first in case it blocks. */
1216 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1219 sin6->sin6_family = AF_INET6;
1220 sin6->sin6_len = sizeof(*sin6);
1222 inp = (struct sctp_inpcb *)so->so_pcb;
1223 if ((inp == NULL) ||
1224 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1225 /* UDP type and listeners will drop out here */
1226 SCTP_FREE_SONAME(sin6);
1227 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1230 SCTP_INP_RLOCK(inp);
1231 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1233 SCTP_TCB_LOCK(stcb);
1235 SCTP_INP_RUNLOCK(inp);
1237 SCTP_FREE_SONAME(sin6);
1238 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1239 return (ECONNRESET);
1242 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1243 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1244 if (sin_a6->sin6_family == AF_INET6) {
1246 sin6->sin6_port = stcb->rport;
1247 sin6->sin6_addr = sin_a6->sin6_addr;
1251 SCTP_TCB_UNLOCK(stcb);
1253 /* No IPv4 address */
1254 SCTP_FREE_SONAME(sin6);
1255 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1258 if ((error = sa6_recoverscope(sin6)) != 0)
1260 *addr = (struct sockaddr *)sin6;
1265 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1268 struct sockaddr *addr;
1271 struct in6pcb *inp6 = sotoin6pcb(so);
1275 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1278 /* allow v6 addresses precedence */
1279 error = sctp6_getaddr(so, nam);
1282 /* try v4 next if v6 failed */
1283 error = sctp_ingetaddr(so, nam);
1288 /* if I'm V6ONLY, convert it to v4-mapped */
1289 if (SCTP_IPV6_V6ONLY(inp6)) {
1290 struct sockaddr_in6 sin6;
1292 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1293 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1302 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1305 struct sockaddr *addr;
1308 struct in6pcb *inp6 = sotoin6pcb(so);
1312 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1315 /* allow v6 addresses precedence */
1316 error = sctp6_peeraddr(so, nam);
1319 /* try v4 next if v6 failed */
1320 error = sctp_peeraddr(so, nam);
1325 /* if I'm V6ONLY, convert it to v4-mapped */
1326 if (SCTP_IPV6_V6ONLY(inp6)) {
1327 struct sockaddr_in6 sin6;
1329 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1330 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1337 struct pr_usrreqs sctp6_usrreqs = {
1338 .pru_abort = sctp6_abort,
1339 .pru_accept = sctp_accept,
1340 .pru_attach = sctp6_attach,
1341 .pru_bind = sctp6_bind,
1342 .pru_connect = sctp6_connect,
1343 .pru_control = in6_control,
1344 .pru_close = sctp6_close,
1345 .pru_detach = sctp6_close,
1346 .pru_sopoll = sopoll_generic,
1347 .pru_flush = sctp_flush,
1348 .pru_disconnect = sctp6_disconnect,
1349 .pru_listen = sctp_listen,
1350 .pru_peeraddr = sctp6_getpeeraddr,
1351 .pru_send = sctp6_send,
1352 .pru_shutdown = sctp_shutdown,
1353 .pru_sockaddr = sctp6_in6getaddr,
1354 .pru_sosend = sctp_sosend,
1355 .pru_soreceive = sctp_soreceive