1 /* $KAME: sctp6_usrreq.c,v 1.35 2004/08/17 06:28:03 t-momose Exp $ */
2 /* $DragonFly: src/sys/netinet6/sctp6_usrreq.c,v 1.1 2005/07/15 14:46:17 eirikn Exp $ */
5 * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Cisco Systems, Inc.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if !(defined(__OpenBSD__) || defined(__APPLE__))
39 #include "opt_inet6.h"
45 #if !(defined(__OpenBSD__) || defined(__APPLE__))
46 #include "opt_ipsec.h"
50 #elif !defined(__OpenBSD__)
54 #include <sys/param.h>
55 #include <sys/kernel.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/socket.h>
60 #include <sys/malloc.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63 #include <sys/errno.h>
65 #include <sys/systm.h>
66 #include <sys/syslog.h>
69 #include <net/route.h>
70 #include <net/if_types.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/sctp_pcb.h>
78 #include <netinet/sctp_header.h>
79 #include <netinet/sctp_var.h>
80 #include <netinet/sctputil.h>
81 #include <netinet/sctp_output.h>
82 #include <netinet/sctp_input.h>
83 #include <netinet/sctp_asconf.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet/ip6.h>
86 #if !defined(__OpenBSD__)
87 #include <netinet6/in6_pcb.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/sctp6_var.h>
91 #include <netinet6/ip6protosw.h>
92 #include <netinet6/nd6.h>
96 #include <netinet6/ipsec.h>
102 #if defined(NFAITH) && NFAITH > 0
103 #include <net/if_faith.h>
106 #include <net/net_osdep.h>
108 extern struct protosw inetsw[];
110 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
115 #define sotoin6pcb sotoinpcb
120 extern u_int32_t sctp_debug_on;
123 static int sctp6_detach __P((struct socket *so));
125 #if !(defined(__FreeBSD__) || defined(__APPLE__))
126 extern void in6_sin_2_v4mapsin6 (struct sockaddr_in *sin,
127 struct sockaddr_in6 *sin6);
128 extern void in6_sin6_2_sin (struct sockaddr_in *,
129 struct sockaddr_in6 *sin6);
130 extern void in6_sin6_2_sin_in_sock(struct sockaddr *nam);
133 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
134 * v4 mapped addr or v4 compat addr
137 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
139 bzero(sin, sizeof(*sin));
140 sin->sin_len = sizeof(struct sockaddr_in);
141 sin->sin_family = AF_INET;
142 sin->sin_port = sin6->sin6_port;
143 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
146 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
148 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
150 bzero(sin6, sizeof(*sin6));
151 sin6->sin6_len = sizeof(struct sockaddr_in6);
152 sin6->sin6_family = AF_INET6;
153 sin6->sin6_port = sin->sin_port;
154 sin6->sin6_addr.s6_addr32[0] = 0;
155 sin6->sin6_addr.s6_addr32[1] = 0;
156 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
157 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
160 /* Convert sockaddr_in6 into sockaddr_in. */
162 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
164 struct sockaddr_in *sin_p;
165 struct sockaddr_in6 sin6;
167 /* save original sockaddr_in6 addr and convert it to sockaddr_in */
168 sin6 = *(struct sockaddr_in6 *)nam;
169 sin_p = (struct sockaddr_in *)nam;
170 in6_sin6_2_sin(sin_p, &sin6);
173 #endif /* !(__FreeBSD__ || __APPLE__) */
175 extern int sctp_no_csum_on_loopback;
178 #if defined(__APPLE__)
179 sctp6_input(mp, offp)
181 sctp6_input(mp, offp, proto)
189 struct mbuf *m = *mp;
192 struct sctp_inpcb *in6p = NULL;
193 struct sctp_nets *net;
195 u_int32_t check, calc_check;
196 struct inpcb *in6p_ip;
197 struct sctp_chunkhdr *ch;
198 struct mbuf *opts = NULL;
199 int length, mlen, offset, iphlen;
201 struct sctp_tcb *stcb = NULL;
205 ip6 = mtod(m, struct ip6_hdr *);
206 #ifndef PULLDOWN_TEST
207 /* If PULLDOWN_TEST off, must be in a single mbuf. */
208 IP6_EXTHDR_CHECK(m, off, (int)(sizeof(*sh) + sizeof(*ch)), IPPROTO_DONE);
209 sh = (struct sctphdr *)((caddr_t)ip6 + off);
210 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
212 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
213 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch));
215 sctp_pegs[SCTP_HDR_DROPS]++;
218 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
222 offset = iphlen + sizeof(*sh) + sizeof(*ch);
224 #if defined(NFAITH) && NFAITH > 0
225 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version <= 430000
226 #if defined(NFAITH) && 0 < NFAITH
227 if (faithprefix(&ip6h->ip6_dst)) {
228 /* XXX send icmp6 host/port unreach? */
235 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
236 /* XXX send icmp6 host/port unreach? */
240 if (faithprefix(&ip6->ip6_dst))
243 #endif /* __FreeBSD_cc_version */
245 #endif /* NFAITH defined and > 0 */
246 sctp_pegs[SCTP_INPKTS]++;
248 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
249 printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
252 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
253 /* No multi-cast support in SCTP */
254 sctp_pegs[SCTP_IN_MCAST]++;
257 /* destination port of 0 is illegal, based on RFC2960. */
258 if (sh->dest_port == 0)
260 if ((sctp_no_csum_on_loopback == 0) ||
261 (m->m_pkthdr.rcvif == NULL) ||
262 (m->m_pkthdr.rcvif->if_type != IFT_LOOP)) {
263 /* we do NOT validate things from the loopback if the
264 * sysctl is set to 1.
266 check = sh->checksum; /* save incoming checksum */
267 if ((check == 0) && (sctp_no_csum_on_loopback)) {
268 /* special hook for where we got a local address
269 * somehow routed across a non IFT_LOOP type interface
271 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
274 sh->checksum = 0; /* prepare for calc */
275 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
276 if (calc_check != check) {
278 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
279 printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%x mlen:%d iphlen:%d\n",
280 calc_check, check, (u_int)m,
284 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
285 sh, ch, &in6p, &net);
286 /* in6p's ref-count increased && stcb locked */
287 if ((in6p) && (stcb)) {
288 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
289 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
290 } else if ((in6p != NULL) && (stcb == NULL)) {
293 sctp_pegs[SCTP_BAD_CSUM]++;
296 sh->checksum = calc_check;
299 mlen = m->m_pkthdr.len;
303 * Locate pcb and tcb for datagram
304 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
307 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
308 printf("V6 Find the association\n");
311 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
312 sh, ch, &in6p, &net);
313 /* in6p's ref-count increased */
315 struct sctp_init_chunk *init_chk, chunk_buf;
317 sctp_pegs[SCTP_NOPORTS]++;
318 if (ch->chunk_type == SCTP_INITIATION) {
319 /* we do a trick here to get the INIT tag,
320 * dig in and get the tag from the INIT and
321 * put it in the common header.
323 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
324 iphlen + sizeof(*sh), sizeof(*init_chk),
325 (u_int8_t *)&chunk_buf);
326 sh->v_tag = init_chk->init.initiate_tag;
328 sctp_send_abort(m, iphlen, sh, 0, NULL);
330 } else if (stcb == NULL) {
333 in6p_ip = (struct inpcb *)in6p;
336 * Check AH/ESP integrity.
342 struct tdb_ident *tdbi;
346 /* Find most recent IPsec tag */
347 i_inp = (struct inpcb *)in6p;
348 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
351 tdbi = (struct tdb_ident *)(mtag + 1);
352 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
356 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
364 if (i_inp->inp_tdb_in != tdb) {
366 tdb_add_inp(tdb, i_inp, 1);
367 if (i_inp->inp_ipo == NULL) {
368 i_inp->inp_ipo = ipsec_add_policy(i_inp,
369 af, IPSP_DIRECTION_OUT);
370 if (i_inp->inp_ipo == NULL) {
375 if (i_inp->inp_ipo->ipo_dstid == NULL &&
376 tdb->tdb_srcid != NULL) {
377 i_inp->inp_ipo->ipo_dstid =
379 tdb->tdb_srcid->ref_count++;
381 if (i_inp->inp_ipsec_remotecred == NULL &&
382 tdb->tdb_remote_cred != NULL) {
383 i_inp->inp_ipsec_remotecred =
384 tdb->tdb_remote_cred;
385 tdb->tdb_remote_cred->ref_count++;
387 if (i_inp->inp_ipsec_remoteauth == NULL &&
388 tdb->tdb_remote_auth != NULL) {
389 i_inp->inp_ipsec_remoteauth =
390 tdb->tdb_remote_auth;
391 tdb->tdb_remote_auth->ref_count++;
393 } else { /* Just reset */
394 TAILQ_REMOVE(&i_inp->inp_tdb_in->tdb_inp_in,
395 i_inp, binp_tdb_in_next);
396 i_inp->inp_tdb_in = NULL;
402 if (ipsec6_in_reject_so(m, in6p->sctp_socket)) {
405 /* FIX ME: need to find right stat for __APPLE__ */
406 ipsec6stat.in_polvio++;
414 * Construct sockaddr format source address.
415 * Stuff source address and datagram in user buffer.
417 if ((in6p->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
419 || (in6p->sctp_socket->so_options & SO_TIMESTAMP)
422 #if defined(__FreeBSD__) || defined(__APPLE__)
423 #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 501113) || defined(__APPLE__)
424 ip6_savecontrol(in6p_ip, &opts, ip6, m);
425 #elif __FreeBSD_version >= 440000 || (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version >= 501113)
426 ip6_savecontrol(in6p_ip, m, &opts);
428 ip6_savecontrol(in6p_ip, m, &opts, NULL);
431 ip6_savecontrol((struct in6pcb *)in6p_ip, m, &opts);
436 * CONTROL chunk processing
438 length = ntohs(ip6->ip6_plen) + iphlen;
439 offset -= sizeof(*ch);
440 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
441 #if defined(__NetBSD__) || defined(__OpenBSD__)
446 (void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
447 in6p, stcb, net, ecn_bits);
448 /* inp's ref-count reduced && stcb unlocked */
450 /* XXX this stuff below gets moved to appropriate parts later... */
456 if ((in6p) && refcount_up){
457 /* reduce ref-count */
458 SCTP_INP_WLOCK(in6p);
459 SCTP_INP_DECR_REF(in6p);
460 SCTP_INP_WUNLOCK(in6p);
467 SCTP_TCB_UNLOCK(stcb);
469 if ((in6p) && refcount_up){
470 /* reduce ref-count */
471 SCTP_INP_WLOCK(in6p);
472 SCTP_INP_DECR_REF(in6p);
473 SCTP_INP_WUNLOCK(in6p);
484 sctp6_notify_mbuf(struct sctp_inpcb *inp,
485 struct icmp6_hdr *icmp6,
487 struct sctp_tcb *stcb,
488 struct sctp_nets *net)
492 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
493 (icmp6 == NULL) || (sh == NULL)) {
497 /* First do we even look at it? */
498 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
501 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
502 /* not PACKET TO BIG */
506 * ok we need to look closely. We could even get smarter and
507 * look at anyone that we sent to in case we get a different
508 * ICMP that tells us there is no way to reach a host, but for
509 * this impl, all we care about is MTU discovery.
511 nxtsz = ntohl(icmp6->icmp6_mtu);
512 /* Stop any PMTU timer */
513 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
515 /* Adjust destination size limit */
516 if (net->mtu > nxtsz) {
519 /* now what about the ep? */
520 if (stcb->asoc.smallest_mtu > nxtsz) {
521 struct sctp_tmit_chunk *chk;
522 struct sctp_stream_out *strm;
523 /* Adjust that too */
524 stcb->asoc.smallest_mtu = nxtsz;
525 /* now off to subtract IP_DF flag if needed */
527 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
528 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
529 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
532 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
533 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
535 * For this guy we also mark for immediate
536 * resend since we sent to big of chunk
538 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
539 if (chk->sent != SCTP_DATAGRAM_RESEND)
540 stcb->asoc.sent_queue_retran_cnt++;
541 chk->sent = SCTP_DATAGRAM_RESEND;
542 chk->rec.data.doing_fast_retransmit = 0;
544 chk->sent = SCTP_DATAGRAM_RESEND;
545 /* Clear any time so NO RTT is being done */
546 chk->sent_rcv_time.tv_sec = 0;
547 chk->sent_rcv_time.tv_usec = 0;
548 stcb->asoc.total_flight -= chk->send_size;
549 net->flight_size -= chk->send_size;
552 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
553 TAILQ_FOREACH(chk, &strm->outqueue, sctp_next) {
554 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
555 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
560 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
563 /* reduce inp's ref-count */
565 SCTP_INP_DECR_REF(inp);
566 SCTP_INP_WUNLOCK(inp);
569 SCTP_TCB_UNLOCK(stcb);
574 sctp6_ctlinput(cmd, pktdst, d)
576 struct sockaddr *pktdst;
580 struct ip6ctlparam *ip6cp = NULL;
583 if (pktdst->sa_family != AF_INET6 ||
584 pktdst->sa_len != sizeof(struct sockaddr_in6))
587 if ((unsigned)cmd >= PRC_NCMDS)
589 if (PRC_IS_REDIRECT(cmd)) {
591 } else if (inet6ctlerrmap[cmd] == 0) {
595 /* if the parameter is from icmp6, decode it. */
597 ip6cp = (struct ip6ctlparam *)d;
599 ip6cp = (struct ip6ctlparam *)NULL;
604 * XXX: We assume that when IPV6 is non NULL,
605 * M and OFF are valid.
607 /* check if we can safely examine src and dst ports */
608 struct sctp_inpcb *inp;
609 struct sctp_tcb *stcb;
610 struct sctp_nets *net;
611 struct sockaddr_in6 final;
613 if (ip6cp->ip6c_m == NULL ||
614 (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh)))
617 bzero(&sh, sizeof(sh));
618 bzero(&final, sizeof(final));
621 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
623 ip6cp->ip6c_src->sin6_port = sh.src_port;
624 final.sin6_len = sizeof(final);
625 final.sin6_family = AF_INET6;
626 #if defined(__FreeBSD__) && __FreeBSD_cc_version < 440000
627 final.sin6_addr = *ip6cp->ip6c_finaldst;
629 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
630 #endif /* __FreeBSD_cc_version */
631 final.sin6_port = sh.dest_port;
632 #if defined(__NetBSD__) || defined(__OpenBSD__)
637 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
638 (struct sockaddr *)&final,
640 /* inp's ref-count increased && stcb locked */
641 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
642 if (cmd == PRC_MSGSIZE) {
643 sctp6_notify_mbuf(inp,
648 /* inp's ref-count reduced && stcb unlocked */
650 if (cmd == PRC_HOSTDEAD) {
653 cm = inet6ctlerrmap[cmd];
655 sctp_notify(inp, cm, &sh,
656 (struct sockaddr *)&final,
658 /* inp's ref-count reduced && stcb unlocked */
661 if (PRC_IS_REDIRECT(cmd) && inp) {
663 in_rtchange((struct inpcb *)inp,
666 in6_rtchange((struct in6pcb *)inp,
667 inet6ctlerrmap[cmd]);
671 /* reduce inp's ref-count */
673 SCTP_INP_DECR_REF(inp);
674 SCTP_INP_WUNLOCK(inp);
677 SCTP_TCB_UNLOCK(stcb);
684 * this routine can probably be collasped into the one in sctp_userreq.c
685 * since they do the same thing and now we lookup with a sockaddr
689 sctp6_getcred(SYSCTL_HANDLER_ARGS)
691 struct sockaddr_in6 addrs[2];
692 struct sctp_inpcb *inp;
693 struct sctp_nets *net;
694 struct sctp_tcb *stcb;
697 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
698 error = suser(req->td);
700 error = suser(req->p);
705 if (req->newlen != sizeof(addrs))
707 if (req->oldlen != sizeof(struct ucred))
709 error = SYSCTL_IN(req, addrs, sizeof(addrs));
712 #if defined(__NetBSD__) || defined(__OpenBSD__)
718 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
721 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
725 SCTP_INP_DECR_REF(inp);
726 SCTP_INP_WUNLOCK(inp);
730 error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
731 sizeof(struct ucred));
733 SCTP_TCB_UNLOCK (stcb);
739 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
741 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
745 /* This is the same as the sctp_abort() could be made common */
747 sctp6_abort(struct socket *so)
749 struct sctp_inpcb *inp;
752 inp = (struct sctp_inpcb *)so->so_pcb;
754 return EINVAL; /* ??? possible? panic instead? */
755 soisdisconnected(so);
756 #if defined(__NetBSD__) || defined(__OpenBSD__)
761 sctp_inpcb_free(inp, 1);
767 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
768 sctp6_attach(struct socket *so, int proto, struct thread *p)
770 sctp6_attach(struct socket *so, int proto, struct proc *p)
775 struct sctp_inpcb *inp;
777 inp = (struct sctp_inpcb *)so->so_pcb;
781 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
782 error = soreserve(so, sctp_sendspace, sctp_recvspace);
786 #if defined(__NetBSD__) || defined(__OpenBSD__)
791 error = sctp_inpcb_alloc(so);
795 inp = (struct sctp_inpcb *)so->so_pcb;
796 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
797 inp6 = (struct in6pcb *)inp;
799 #if defined(__FreeBSD__) || defined(__APPLE__)
800 inp6->inp_vflag |= INP_IPV6;
802 #if defined(__OpenBSD__)
803 inp->ip_inp.inp.inp_flags |= INP_IPV6;
805 inp->inp_vflag |= INP_IPV6;
808 #if defined(__NetBSD__)
810 inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
812 so->so_send = sctp_sosend;
814 inp6->in6p_hops = -1; /* use kernel default */
815 inp6->in6p_cksum = -1; /* just to be sure */
819 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
820 * because the socket may be bound to an IPv6 wildcard address,
821 * which may match an IPv4-mapped IPv6 address.
823 #if defined(__FreeBSD__) || defined(__APPLE__)
824 inp6->inp_ip_ttl = ip_defttl;
826 inp->inp_ip_ttl = ip_defttl;
830 * Hmm what about the IPSEC stuff that is missing here but
837 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
838 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
841 #if defined(__FreeBSD__) || defined(__APPLE__)
842 sctp6_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
845 sctp6_bind(struct socket *so, struct mbuf *nam, struct proc *p)
847 struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
850 struct sctp_inpcb *inp;
854 inp = (struct sctp_inpcb *)so->so_pcb;
858 inp6 = (struct in6pcb *)inp;
859 #if defined(__FreeBSD__) || defined(__APPLE__)
860 inp6->inp_vflag &= ~INP_IPV4;
861 inp6->inp_vflag |= INP_IPV6;
863 #if defined(__OpenBSD__)
864 inp->ip_inp.inp.inp_flags &= ~INP_IPV4;
865 inp->ip_inp.inp.inp_flags |= INP_IPV6;
867 inp->inp_vflag &= ~INP_IPV4;
868 inp->inp_vflag |= INP_IPV6;
872 #if defined(__OpenBSD__)
873 (0) /* we always do dual bind */
874 #elif defined (__NetBSD__)
875 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
877 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
880 if (addr->sa_family == AF_INET) {
881 /* binding v4 addr to v6 socket, so reset flags */
882 #if defined(__FreeBSD__) || defined(__APPLE__)
883 inp6->inp_vflag |= INP_IPV4;
884 inp6->inp_vflag &= ~INP_IPV6;
886 #if defined(__OpenBSD__)
887 inp->ip_inp.inp.inp_flags |= INP_IPV4;
888 inp->ip_inp.inp.inp_flags &= ~INP_IPV6;
890 inp->inp_vflag |= INP_IPV4;
891 inp->inp_vflag &= ~INP_IPV6;
895 struct sockaddr_in6 *sin6_p;
896 sin6_p = (struct sockaddr_in6 *)addr;
898 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
899 #if defined(__FreeBSD__) || defined(__APPLE__)
900 inp6->inp_vflag |= INP_IPV4;
902 #if defined(__OpenBSD__)
903 inp->ip_inp.inp.inp_flags |= INP_IPV4;
905 inp->inp_vflag |= INP_IPV4;
909 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
910 struct sockaddr_in sin;
911 in6_sin6_2_sin(&sin, sin6_p);
912 #if defined(__FreeBSD__) || defined(__APPLE__)
913 inp6->inp_vflag |= INP_IPV4;
914 inp6->inp_vflag &= ~INP_IPV6;
916 #if defined(__OpenBSD__)
917 inp->ip_inp.inp.inp_flags |= INP_IPV4;
918 inp->ip_inp.inp.inp_flags &= ~INP_IPV6;
921 inp->inp_vflag |= INP_IPV4;
922 inp->inp_vflag &= ~INP_IPV6;
925 #if defined(__NetBSD__) || defined(__OpenBSD__)
930 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p);
935 } else if (addr != NULL) {
936 /* IPV6_V6ONLY socket */
937 if (addr->sa_family == AF_INET) {
938 /* can't bind v4 addr to v6 only socket! */
941 struct sockaddr_in6 *sin6_p;
942 sin6_p = (struct sockaddr_in6 *)addr;
944 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
945 /* can't bind v4-mapped addrs either! */
946 /* NOTE: we don't support SIIT */
950 #if defined(__NetBSD__) || defined(__OpenBSD__)
955 error = sctp_inpcb_bind(so, addr, p);
960 /*This could be made common with sctp_detach() since they are identical */
962 sctp6_detach(struct socket *so)
964 struct sctp_inpcb *inp;
967 inp = (struct sctp_inpcb *)so->so_pcb;
970 #if defined(__NetBSD__) || defined(__OpenBSD__)
975 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
976 (so->so_rcv.sb_cc > 0))
977 sctp_inpcb_free(inp, 1);
979 sctp_inpcb_free(inp, 0);
985 sctp6_disconnect(struct socket *so)
987 struct sctp_inpcb *inp;
990 #if defined(__NetBSD__) || defined(__OpenBSD__)
993 s = splnet(); /* XXX */
995 inp = (struct sctp_inpcb *)so->so_pcb;
1000 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1001 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
1006 int some_on_streamwheel = 0;
1007 struct sctp_association *asoc;
1008 struct sctp_tcb *stcb;
1010 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1016 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
1017 /* Check to see if some data queued */
1018 struct sctp_stream_out *outs;
1019 TAILQ_FOREACH(outs, &asoc->out_wheel,
1021 if (!TAILQ_EMPTY(&outs->outqueue)) {
1022 some_on_streamwheel = 1;
1028 if (TAILQ_EMPTY(&asoc->send_queue) &&
1029 TAILQ_EMPTY(&asoc->sent_queue) &&
1030 (some_on_streamwheel == 0)) {
1031 /* nothing queued to send, so I'm done... */
1032 if ((SCTP_GET_STATE(asoc) !=
1033 SCTP_STATE_SHUTDOWN_SENT) &&
1034 (SCTP_GET_STATE(asoc) !=
1035 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1036 /* only send SHUTDOWN the first time */
1038 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1039 printf("%s:%d sends a shutdown\n",
1045 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1046 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1047 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1048 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1049 stcb->sctp_ep, stcb,
1050 asoc->primary_destination);
1051 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1052 stcb->sctp_ep, stcb,
1053 asoc->primary_destination);
1057 * we still got (or just got) data to send,
1058 * so set SHUTDOWN_PENDING
1061 * XXX sockets draft says that MSG_EOF should
1062 * be sent with no data. currently, we will
1063 * allow user data to be sent first and move
1064 * to SHUTDOWN-PENDING
1066 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1072 /* UDP model does not support this */
1079 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1080 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1081 struct mbuf *control, struct thread *p);
1083 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1084 struct mbuf *control, struct proc *p);
1089 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1090 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1091 struct mbuf *control, struct thread *p)
1094 #if defined(__FreeBSD__) || defined(__APPLE__)
1095 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1096 struct mbuf *control, struct proc *p)
1099 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam,
1100 struct mbuf *control, struct proc *p)
1102 struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
1105 struct sctp_inpcb *inp;
1106 struct inpcb *in_inp;
1107 struct in6pcb *inp6;
1109 struct sockaddr_in6 *sin6;
1111 /* No SPL needed since sctp_output does this */
1113 inp = (struct sctp_inpcb *)so->so_pcb;
1122 in_inp = (struct inpcb *)inp;
1123 inp6 = (struct in6pcb *)inp;
1124 /* For the TCP model we may get a NULL addr, if we
1125 * are a connected socket thats ok.
1127 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
1129 goto connected_type;
1137 return (EDESTADDRREQ);
1141 sin6 = (struct sockaddr_in6 *)addr;
1144 #if defined(__OpenBSD__)
1145 (0) /* we always do dual bind */
1146 #elif defined (__NetBSD__)
1147 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1149 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1153 * if IPV6_V6ONLY flag, we discard datagrams
1154 * destined to a v4 addr or v4-mapped addr
1156 if (addr->sa_family == AF_INET) {
1159 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1164 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1166 struct sockaddr_in sin;
1167 /* convert v4-mapped into v4 addr and send */
1168 in6_sin6_2_sin(&sin, sin6);
1169 return sctp_send(so, flags, m, (struct sockaddr *)&sin,
1172 /* mapped addresses aren't enabled */
1178 /* now what about control */
1181 printf("huh? control set?\n");
1182 m_freem(inp->control);
1183 inp->control = NULL;
1185 inp->control = control;
1187 /* add it in possibly */
1189 (inp->pkt->m_flags & M_PKTHDR)) {
1195 for (x=m;x;x = x->m_next) {
1198 inp->pkt->m_pkthdr.len += c_len;
1200 /* Place the data */
1202 inp->pkt_last->m_next = m;
1205 inp->pkt_last = inp->pkt = m;
1208 #if defined(__FreeBSD__) || defined(__APPLE__)
1209 /* FreeBSD and MacOSX uses a flag passed */
1210 ((flags & PRUS_MORETOCOME) == 0)
1211 #elif defined(__NetBSD__)
1212 /* NetBSD uses the so_state field */
1213 ((so->so_state & SS_MORETOCOME) == 0)
1215 1 /* Open BSD does not have any "more to come" indication */
1219 * note with the current version this code will only be
1220 * used by OpenBSD, NetBSD and FreeBSD have methods for
1221 * re-defining sosend() to use sctp_sosend(). One can
1222 * optionaly switch back to this code (by changing back
1223 * the defininitions but this is not advisable.
1226 ret = sctp_output(inp, inp->pkt , addr, inp->control, p, flags);
1228 inp->control = NULL;
1236 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1237 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
1240 #if defined(__FreeBSD__) || defined(__APPLE__)
1241 sctp6_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
1244 sctp6_connect(struct socket *so, struct mbuf *nam, struct proc *p)
1246 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1249 #if defined(__NetBSD__) || defined(__OpenBSD__)
1250 int s = splsoftnet();
1255 struct sctp_inpcb *inp;
1256 struct in6pcb *inp6;
1257 struct sctp_tcb *stcb;
1259 struct sockaddr_in6 *sin6;
1260 struct sockaddr_storage ss;
1263 inp6 = (struct in6pcb *)so->so_pcb;
1264 inp = (struct sctp_inpcb *)so->so_pcb;
1267 return (ECONNRESET); /* I made the same as TCP since
1268 * we are not setup? */
1270 SCTP_ASOC_CREATE_LOCK(inp);
1271 SCTP_INP_RLOCK(inp);
1272 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1273 SCTP_PCB_FLAGS_UNBOUND) {
1274 /* Bind a ephemeral port */
1275 SCTP_INP_RUNLOCK(inp);
1276 error = sctp6_bind(so, NULL, p);
1279 SCTP_ASOC_CREATE_UNLOCK(inp);
1283 SCTP_INP_RLOCK(inp);
1286 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1287 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1288 /* We are already connected AND the TCP model */
1290 SCTP_INP_RUNLOCK(inp);
1291 SCTP_ASOC_CREATE_UNLOCK(inp);
1292 return (EADDRINUSE);
1296 sin6 = (struct sockaddr_in6 *)addr;
1298 #if defined(__OpenBSD__)
1299 (0) /* we always do dual bind */
1300 #elif defined (__NetBSD__)
1301 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1303 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1307 * if IPV6_V6ONLY flag, ignore connections
1308 * destined to a v4 addr or v4-mapped addr
1310 if (addr->sa_family == AF_INET) {
1312 SCTP_INP_RUNLOCK(inp);
1313 SCTP_ASOC_CREATE_UNLOCK(inp);
1316 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1318 SCTP_INP_RUNLOCK(inp);
1319 SCTP_ASOC_CREATE_UNLOCK(inp);
1324 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1326 /* convert v4-mapped into v4 addr */
1327 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1328 addr = (struct sockaddr *)&ss;
1330 /* mapped addresses aren't enabled */
1332 SCTP_INP_RUNLOCK(inp);
1333 SCTP_ASOC_CREATE_UNLOCK(inp);
1338 addr = addr; /* for true v6 address case */
1340 /* Now do we connect? */
1341 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1342 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1344 SCTP_TCB_UNLOCK (stcb);
1345 SCTP_INP_RUNLOCK(inp);
1347 SCTP_INP_RUNLOCK(inp);
1348 SCTP_INP_WLOCK(inp);
1349 SCTP_INP_INCR_REF(inp);
1350 SCTP_INP_WUNLOCK(inp);
1351 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1353 SCTP_INP_WLOCK(inp);
1354 SCTP_INP_DECR_REF(inp);
1355 SCTP_INP_WUNLOCK(inp);
1360 /* Already have or am bring up an association */
1361 SCTP_ASOC_CREATE_UNLOCK(inp);
1362 SCTP_TCB_UNLOCK (stcb);
1366 /* We are GOOD to go */
1367 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
1368 SCTP_ASOC_CREATE_UNLOCK(inp);
1370 /* Gak! no memory */
1374 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1375 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1376 /* Set the connected flag so we can queue data */
1379 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1380 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1381 sctp_send_initiate(inp, stcb);
1382 SCTP_TCB_UNLOCK (stcb);
1388 #if defined(__FreeBSD__) || defined(__APPLE__)
1389 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1391 struct sockaddr_in6 *sin6;
1393 sctp6_getaddr(struct socket *so, struct mbuf *nam)
1395 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1397 struct sctp_inpcb *inp;
1399 * Do the malloc first in case it blocks.
1401 #if defined(__FreeBSD__) || defined(__APPLE__)
1402 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
1405 nam->m_len = sizeof(*sin6);
1407 bzero(sin6, sizeof(*sin6));
1408 sin6->sin6_family = AF_INET6;
1409 sin6->sin6_len = sizeof(*sin6);
1411 inp = (struct sctp_inpcb *)so->so_pcb;
1413 #if defined(__FreeBSD__) || defined(__APPLE__)
1414 FREE(sin6, M_SONAME);
1419 sin6->sin6_port = inp->sctp_lport;
1420 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1421 /* For the bound all case you get back 0 */
1422 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1423 struct sctp_tcb *stcb;
1424 struct sockaddr_in6 *sin_a6;
1425 struct sctp_nets *net;
1428 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1434 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1435 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1436 if (sin_a6->sin6_family == AF_INET6) {
1441 if ((!fnd) || (sin_a6 == NULL)) {
1445 sin6->sin6_addr = sctp_ipv6_source_address_selection(
1446 inp, stcb, (struct route *)&net->ro, net, 0);
1449 /* For the bound all case you get back 0 */
1451 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1454 /* Take the first IPv6 address in the list */
1455 struct sctp_laddr *laddr;
1457 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1458 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
1459 struct sockaddr_in6 *sin_a;
1460 sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1461 sin6->sin6_addr = sin_a->sin6_addr;
1467 #if defined(__FreeBSD__) || defined(__APPLE__)
1468 FREE(sin6, M_SONAME);
1473 /* Scoping things for v6 */
1474 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1475 /* skip ifp check below */
1476 in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1478 sin6->sin6_scope_id = 0; /*XXX*/
1479 #if defined(__FreeBSD__) || defined(__APPLE__)
1480 (*addr) = (struct sockaddr *)sin6;
1486 #if defined(__FreeBSD__) || defined(__APPLE__)
1487 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1489 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1491 sctp6_peeraddr(struct socket *so, struct mbuf *nam)
1493 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1496 struct sockaddr_in6 *sin_a6;
1497 struct sctp_inpcb *inp;
1498 struct sctp_tcb *stcb;
1499 struct sctp_nets *net;
1501 * Do the malloc first in case it blocks.
1503 inp = (struct sctp_inpcb *)so->so_pcb;
1504 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1505 /* UDP type and listeners will drop out here */
1508 #if defined(__FreeBSD__) || defined(__APPLE__)
1509 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
1512 nam->m_len = sizeof(*sin6);
1514 bzero(sin6, sizeof(*sin6));
1515 sin6->sin6_family = AF_INET6;
1516 sin6->sin6_len = sizeof(*sin6);
1518 /* We must recapture incase we blocked */
1519 inp = (struct sctp_inpcb *)so->so_pcb;
1521 #if defined(__FreeBSD__) || defined(__APPLE__)
1522 FREE(sin6, M_SONAME);
1526 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1528 #if defined(__FreeBSD__) || defined(__APPLE__)
1529 FREE(sin6, M_SONAME);
1534 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1535 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1536 if (sin_a6->sin6_family == AF_INET6) {
1538 sin6->sin6_port = stcb->rport;
1539 sin6->sin6_addr = sin_a6->sin6_addr;
1544 /* No IPv4 address */
1545 #if defined(__FreeBSD__) || defined(__APPLE__)
1546 FREE(sin6, M_SONAME);
1550 in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1551 #if defined(__FreeBSD__) || defined(__APPLE__)
1552 *addr = (struct sockaddr *)sin6;
1558 #if defined(__FreeBSD__) || defined(__APPLE__)
1559 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1561 struct sockaddr *addr;
1563 sctp6_in6getaddr(struct socket *so, struct mbuf *nam)
1565 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1567 struct in6pcb *inp6 = sotoin6pcb(so);
1573 #if defined(__NetBSD__) || defined(__OpenBSD__)
1578 /* allow v6 addresses precedence */
1579 error = sctp6_getaddr(so, nam);
1581 /* try v4 next if v6 failed */
1582 error = sctp_ingetaddr(so, nam);
1587 #if defined(__FreeBSD__) || defined(__APPLE__)
1590 /* if I'm V6ONLY, convert it to v4-mapped */
1592 #if defined(__OpenBSD__)
1593 (0) /* we always do dual bind */
1594 #elif defined (__NetBSD__)
1595 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1597 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1600 struct sockaddr_in6 sin6;
1601 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1602 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1603 #if !(defined(__FreeBSD__) || defined(__APPLE__))
1604 nam->m_len = sizeof(sin6);
1606 #if !(defined(__FreeBSD__) || defined(__APPLE__))
1608 nam->m_len = sizeof(struct sockaddr_in);
1611 #if !(defined(__FreeBSD__) || defined(__APPLE__))
1613 nam->m_len = sizeof(struct sockaddr_in6);
1622 #if defined(__FreeBSD__) || defined(__APPLE__)
1623 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1625 struct sockaddr *addr = *nam;
1627 sctp6_getpeeraddr(struct socket *so, struct mbuf *nam)
1629 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1631 struct in6pcb *inp6 = sotoin6pcb(so);
1637 #if defined(__NetBSD__) || defined(__OpenBSD__)
1642 /* allow v6 addresses precedence */
1643 error = sctp6_peeraddr(so, nam);
1645 /* try v4 next if v6 failed */
1646 error = sctp_peeraddr(so, nam);
1651 /* if I'm V6ONLY, convert it to v4-mapped */
1653 #if defined(__OpenBSD__)
1654 (0) /* we always do dual bind */
1655 #elif defined (__NetBSD__)
1656 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1658 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1661 struct sockaddr_in6 sin6;
1662 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1663 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1664 #if !(defined(__FreeBSD__) || defined(__APPLE__))
1665 nam->m_len = sizeof(sin6);
1667 #if !(defined(__FreeBSD__) || defined(__APPLE__))
1669 nam->m_len = sizeof(struct sockaddr_in);
1672 #if !(defined(__FreeBSD__) || defined(__APPLE__))
1674 nam->m_len = sizeof(struct sockaddr_in6);
1681 #if defined(__FreeBSD__) || defined(__APPLE__)
1682 struct pr_usrreqs sctp6_usrreqs = {
1683 sctp6_abort, sctp_accept, sctp6_attach, sctp6_bind,
1684 sctp6_connect, pru_connect2_notsupp, in6_control,
1685 sctp6_detach, sctp6_disconnect, sctp_listen, sctp6_getpeeraddr,
1686 sctp_usr_recvd, pru_rcvoob_notsupp, sctp6_send, pru_sense_null,
1687 sctp_shutdown, sctp6_in6getaddr, sctp_sosend, soreceive, sopoll
1693 sctp6_usrreq(so, req, m, nam, control, p)
1696 struct mbuf *m, *nam, *control;
1703 #if defined(__OpenBSD__)
1707 family = so->so_proto->pr_domain->dom_family;
1709 if (req == PRU_CONTROL) {
1712 error = in_control(so, (long)m, (caddr_t)nam,
1713 (struct ifnet *)control
1714 #if defined(__NetBSD__)
1720 error = in6_control(so, (long)m, (caddr_t)nam,
1721 (struct ifnet *)control, p);
1724 error = EAFNOSUPPORT;
1730 if (req == PRU_PURGEIF) {
1733 ifn = (struct ifnet *)control;
1734 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
1735 if (ifa->ifa_addr->sa_family == family) {
1736 sctp_delete_ip_address(ifa);
1750 return (EAFNOSUPPORT);
1758 error = sctp6_attach(so, family, p);
1761 error = sctp6_detach(so);
1766 error = sctp6_bind(so, nam, p);
1769 error = sctp_listen(so, p);
1774 error = sctp6_connect(so, nam, p);
1776 case PRU_DISCONNECT:
1777 error = sctp6_disconnect(so);
1782 error = sctp_accept(so, nam);
1785 error = sctp_shutdown(so);
1790 * For OpenBSD and NetBSD, this is real ugly. The (mbuf *)
1791 * nam that is passed (by soreceive()) is the int flags
1792 * cast as a (mbuf *) yuck!
1794 error = sctp_usr_recvd(so, (int)((long)nam));
1798 /* Flags are ignored */
1799 error = sctp6_send(so, 0, m, nam, control, p);
1802 error = sctp6_abort(so);
1809 error = EAFNOSUPPORT;
1812 error = EAFNOSUPPORT;
1815 error = sctp6_getpeeraddr(so, nam);
1818 error = sctp6_in6getaddr(so, nam);