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.6 2006/06/23 17:20:14 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__))
38 #if defined(__FreeBSD__) || defined(__DragonFly__)
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>
68 #include <sys/thread2.h>
70 #include <net/route.h>
71 #include <net/if_types.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/sctp_pcb.h>
79 #include <netinet/sctp_header.h>
80 #include <netinet/sctp_var.h>
81 #include <netinet/sctputil.h>
82 #include <netinet/sctp_output.h>
83 #include <netinet/sctp_input.h>
84 #include <netinet/sctp_asconf.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet/ip6.h>
87 #if !defined(__OpenBSD__)
88 #include <netinet6/in6_pcb.h>
90 #include <netinet/icmp6.h>
91 #include <netinet6/sctp6_var.h>
92 #include <netinet6/ip6protosw.h>
93 #include <netinet6/nd6.h>
97 #include <netinet6/ipsec6.h>
98 #include <netinet6/ipsec.h>
104 #if defined(NFAITH) && NFAITH > 0
105 #include <net/if_faith.h>
108 #include <net/net_osdep.h>
110 extern struct protosw inetsw[];
112 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__) || defined(__DragonFly__)
117 #define sotoin6pcb sotoinpcb
122 extern u_int32_t sctp_debug_on;
125 static int sctp6_detach(struct socket *so);
127 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
128 extern void in6_sin_2_v4mapsin6 (struct sockaddr_in *sin,
129 struct sockaddr_in6 *sin6);
130 extern void in6_sin6_2_sin (struct sockaddr_in *,
131 struct sockaddr_in6 *sin6);
132 extern void in6_sin6_2_sin_in_sock(struct sockaddr *nam);
135 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
136 * v4 mapped addr or v4 compat addr
139 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
141 bzero(sin, sizeof(*sin));
142 sin->sin_len = sizeof(struct sockaddr_in);
143 sin->sin_family = AF_INET;
144 sin->sin_port = sin6->sin6_port;
145 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
148 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
150 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
152 bzero(sin6, sizeof(*sin6));
153 sin6->sin6_len = sizeof(struct sockaddr_in6);
154 sin6->sin6_family = AF_INET6;
155 sin6->sin6_port = sin->sin_port;
156 sin6->sin6_addr.s6_addr32[0] = 0;
157 sin6->sin6_addr.s6_addr32[1] = 0;
158 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
159 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
162 /* Convert sockaddr_in6 into sockaddr_in. */
164 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
166 struct sockaddr_in *sin_p;
167 struct sockaddr_in6 sin6;
169 /* save original sockaddr_in6 addr and convert it to sockaddr_in */
170 sin6 = *(struct sockaddr_in6 *)nam;
171 sin_p = (struct sockaddr_in *)nam;
172 in6_sin6_2_sin(sin_p, &sin6);
175 #endif /* !(__FreeBSD__ || __APPLE__) */
177 extern int sctp_no_csum_on_loopback;
180 #if defined(__APPLE__)
181 sctp6_input(struct mbuf **mp, int *offp)
183 sctp6_input(struct mbuf **mp, int *offp, int proto)
186 struct mbuf *m = *mp;
189 struct sctp_inpcb *in6p = NULL;
190 struct sctp_nets *net;
192 u_int32_t check, calc_check;
193 struct inpcb *in6p_ip;
194 struct sctp_chunkhdr *ch;
195 struct mbuf *opts = NULL;
196 int length, mlen, offset, iphlen;
198 struct sctp_tcb *stcb = NULL;
201 ip6 = mtod(m, struct ip6_hdr *);
202 #ifndef PULLDOWN_TEST
203 /* If PULLDOWN_TEST off, must be in a single mbuf. */
204 IP6_EXTHDR_CHECK(m, off, (int)(sizeof(*sh) + sizeof(*ch)), IPPROTO_DONE);
205 sh = (struct sctphdr *)((caddr_t)ip6 + off);
206 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(*sh));
208 /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
209 IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch));
211 sctp_pegs[SCTP_HDR_DROPS]++;
214 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
218 offset = iphlen + sizeof(*sh) + sizeof(*ch);
220 #if defined(NFAITH) && NFAITH > 0
221 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version <= 430000
222 #if defined(NFAITH) && 0 < NFAITH
223 if (faithprefix(&ip6h->ip6_dst)) {
224 /* XXX send icmp6 host/port unreach? */
231 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
232 /* XXX send icmp6 host/port unreach? */
236 if (faithprefix(&ip6->ip6_dst))
239 #endif /* __FreeBSD_cc_version */
241 #endif /* NFAITH defined and > 0 */
242 sctp_pegs[SCTP_INPKTS]++;
244 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
245 printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
248 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
249 /* No multi-cast support in SCTP */
250 sctp_pegs[SCTP_IN_MCAST]++;
253 /* destination port of 0 is illegal, based on RFC2960. */
254 if (sh->dest_port == 0)
256 if ((sctp_no_csum_on_loopback == 0) ||
257 (m->m_pkthdr.rcvif == NULL) ||
258 (m->m_pkthdr.rcvif->if_type != IFT_LOOP)) {
259 /* we do NOT validate things from the loopback if the
260 * sysctl is set to 1.
262 check = sh->checksum; /* save incoming checksum */
263 if ((check == 0) && (sctp_no_csum_on_loopback)) {
264 /* special hook for where we got a local address
265 * somehow routed across a non IFT_LOOP type interface
267 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
270 sh->checksum = 0; /* prepare for calc */
271 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
272 if (calc_check != check) {
274 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
275 printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%x mlen:%d iphlen:%d\n",
276 calc_check, check, (u_int)m,
280 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
281 sh, ch, &in6p, &net);
282 /* in6p's ref-count increased && stcb locked */
283 if ((in6p) && (stcb)) {
284 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
285 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
286 } else if ((in6p != NULL) && (stcb == NULL)) {
289 sctp_pegs[SCTP_BAD_CSUM]++;
292 sh->checksum = calc_check;
295 mlen = m->m_pkthdr.len;
299 * Locate pcb and tcb for datagram
300 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
303 if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
304 printf("V6 Find the association\n");
307 stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
308 sh, ch, &in6p, &net);
309 /* in6p's ref-count increased */
311 struct sctp_init_chunk *init_chk, chunk_buf;
313 sctp_pegs[SCTP_NOPORTS]++;
314 if (ch->chunk_type == SCTP_INITIATION) {
315 /* we do a trick here to get the INIT tag,
316 * dig in and get the tag from the INIT and
317 * put it in the common header.
319 init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
320 iphlen + sizeof(*sh), sizeof(*init_chk),
321 (u_int8_t *)&chunk_buf);
322 sh->v_tag = init_chk->init.initiate_tag;
324 sctp_send_abort(m, iphlen, sh, 0, NULL);
326 } else if (stcb == NULL) {
329 in6p_ip = (struct inpcb *)in6p;
332 * Check AH/ESP integrity.
338 struct tdb_ident *tdbi;
342 /* Find most recent IPsec tag */
343 i_inp = (struct inpcb *)in6p;
344 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
347 tdbi = (struct tdb_ident *)(mtag + 1);
348 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
352 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
360 if (i_inp->inp_tdb_in != tdb) {
362 tdb_add_inp(tdb, i_inp, 1);
363 if (i_inp->inp_ipo == NULL) {
364 i_inp->inp_ipo = ipsec_add_policy(i_inp,
365 af, IPSP_DIRECTION_OUT);
366 if (i_inp->inp_ipo == NULL) {
371 if (i_inp->inp_ipo->ipo_dstid == NULL &&
372 tdb->tdb_srcid != NULL) {
373 i_inp->inp_ipo->ipo_dstid =
375 tdb->tdb_srcid->ref_count++;
377 if (i_inp->inp_ipsec_remotecred == NULL &&
378 tdb->tdb_remote_cred != NULL) {
379 i_inp->inp_ipsec_remotecred =
380 tdb->tdb_remote_cred;
381 tdb->tdb_remote_cred->ref_count++;
383 if (i_inp->inp_ipsec_remoteauth == NULL &&
384 tdb->tdb_remote_auth != NULL) {
385 i_inp->inp_ipsec_remoteauth =
386 tdb->tdb_remote_auth;
387 tdb->tdb_remote_auth->ref_count++;
389 } else { /* Just reset */
390 TAILQ_REMOVE(&i_inp->inp_tdb_in->tdb_inp_in,
391 i_inp, binp_tdb_in_next);
392 i_inp->inp_tdb_in = NULL;
398 if (ipsec6_in_reject_so(m, in6p->sctp_socket)) {
401 /* FIX ME: need to find right stat for __APPLE__ */
402 ipsec6stat.in_polvio++;
410 * Construct sockaddr format source address.
411 * Stuff source address and datagram in user buffer.
413 if ((in6p->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
415 || (in6p->sctp_socket->so_options & SO_TIMESTAMP)
418 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
419 #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 501113) || \
420 defined(__APPLE__) || defined(__DragonFly__)
421 ip6_savecontrol(in6p_ip, &opts, ip6, m);
422 #elif __FreeBSD_version >= 440000 || (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version >= 501113)
423 ip6_savecontrol(in6p_ip, m, &opts);
425 ip6_savecontrol(in6p_ip, m, &opts, NULL);
428 ip6_savecontrol((struct in6pcb *)in6p_ip, m, &opts);
433 * CONTROL chunk processing
435 length = ntohs(ip6->ip6_plen) + iphlen;
436 offset -= sizeof(*ch);
437 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
439 sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
440 in6p, stcb, net, ecn_bits);
441 /* inp's ref-count reduced && stcb unlocked */
443 /* XXX this stuff below gets moved to appropriate parts later... */
449 if ((in6p) && refcount_up){
450 /* reduce ref-count */
451 SCTP_INP_WLOCK(in6p);
452 SCTP_INP_DECR_REF(in6p);
453 SCTP_INP_WUNLOCK(in6p);
460 SCTP_TCB_UNLOCK(stcb);
462 if ((in6p) && refcount_up){
463 /* reduce ref-count */
464 SCTP_INP_WLOCK(in6p);
465 SCTP_INP_DECR_REF(in6p);
466 SCTP_INP_WUNLOCK(in6p);
477 sctp6_notify_mbuf(struct sctp_inpcb *inp,
478 struct icmp6_hdr *icmp6,
480 struct sctp_tcb *stcb,
481 struct sctp_nets *net)
485 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
486 (icmp6 == NULL) || (sh == NULL)) {
490 /* First do we even look at it? */
491 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
494 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
495 /* not PACKET TO BIG */
499 * ok we need to look closely. We could even get smarter and
500 * look at anyone that we sent to in case we get a different
501 * ICMP that tells us there is no way to reach a host, but for
502 * this impl, all we care about is MTU discovery.
504 nxtsz = ntohl(icmp6->icmp6_mtu);
505 /* Stop any PMTU timer */
506 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
508 /* Adjust destination size limit */
509 if (net->mtu > nxtsz) {
512 /* now what about the ep? */
513 if (stcb->asoc.smallest_mtu > nxtsz) {
514 struct sctp_tmit_chunk *chk;
515 struct sctp_stream_out *strm;
516 /* Adjust that too */
517 stcb->asoc.smallest_mtu = nxtsz;
518 /* now off to subtract IP_DF flag if needed */
520 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
521 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
522 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
525 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
526 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
528 * For this guy we also mark for immediate
529 * resend since we sent to big of chunk
531 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
532 if (chk->sent != SCTP_DATAGRAM_RESEND)
533 stcb->asoc.sent_queue_retran_cnt++;
534 chk->sent = SCTP_DATAGRAM_RESEND;
535 chk->rec.data.doing_fast_retransmit = 0;
537 chk->sent = SCTP_DATAGRAM_RESEND;
538 /* Clear any time so NO RTT is being done */
539 chk->sent_rcv_time.tv_sec = 0;
540 chk->sent_rcv_time.tv_usec = 0;
541 stcb->asoc.total_flight -= chk->send_size;
542 net->flight_size -= chk->send_size;
545 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
546 TAILQ_FOREACH(chk, &strm->outqueue, sctp_next) {
547 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
548 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
553 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
556 /* reduce inp's ref-count */
558 SCTP_INP_DECR_REF(inp);
559 SCTP_INP_WUNLOCK(inp);
562 SCTP_TCB_UNLOCK(stcb);
567 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
570 struct ip6ctlparam *ip6cp = NULL;
573 if (pktdst->sa_family != AF_INET6 ||
574 pktdst->sa_len != sizeof(struct sockaddr_in6))
577 if ((unsigned)cmd >= PRC_NCMDS)
579 if (PRC_IS_REDIRECT(cmd)) {
581 } else if (inet6ctlerrmap[cmd] == 0) {
585 /* if the parameter is from icmp6, decode it. */
587 ip6cp = (struct ip6ctlparam *)d;
589 ip6cp = (struct ip6ctlparam *)NULL;
594 * XXX: We assume that when IPV6 is non NULL,
595 * M and OFF are valid.
597 /* check if we can safely examine src and dst ports */
598 struct sctp_inpcb *inp;
599 struct sctp_tcb *stcb;
600 struct sctp_nets *net;
601 struct sockaddr_in6 final;
603 if (ip6cp->ip6c_m == NULL ||
604 (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh)))
607 bzero(&sh, sizeof(sh));
608 bzero(&final, sizeof(final));
611 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
613 ip6cp->ip6c_src->sin6_port = sh.src_port;
614 final.sin6_len = sizeof(final);
615 final.sin6_family = AF_INET6;
616 #if defined(__FreeBSD__) && __FreeBSD_cc_version < 440000
617 final.sin6_addr = *ip6cp->ip6c_finaldst;
619 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
620 #endif /* __FreeBSD_cc_version */
621 final.sin6_port = sh.dest_port;
623 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
624 (struct sockaddr *)&final,
626 /* inp's ref-count increased && stcb locked */
627 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
628 if (cmd == PRC_MSGSIZE) {
629 sctp6_notify_mbuf(inp,
634 /* inp's ref-count reduced && stcb unlocked */
636 if (cmd == PRC_HOSTDEAD) {
639 cm = inet6ctlerrmap[cmd];
641 sctp_notify(inp, cm, &sh,
642 (struct sockaddr *)&final,
644 /* inp's ref-count reduced && stcb unlocked */
647 if (PRC_IS_REDIRECT(cmd) && inp) {
649 in_rtchange((struct inpcb *)inp,
652 in6_rtchange((struct in6pcb *)inp,
653 inet6ctlerrmap[cmd]);
657 /* reduce inp's ref-count */
659 SCTP_INP_DECR_REF(inp);
660 SCTP_INP_WUNLOCK(inp);
663 SCTP_TCB_UNLOCK(stcb);
670 * this routine can probably be collasped into the one in sctp_userreq.c
671 * since they do the same thing and now we lookup with a sockaddr
675 sctp6_getcred(SYSCTL_HANDLER_ARGS)
677 struct sockaddr_in6 addrs[2];
678 struct sctp_inpcb *inp;
679 struct sctp_nets *net;
680 struct sctp_tcb *stcb;
683 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
684 error = suser(req->td);
686 error = suser(req->p);
691 if (req->newlen != sizeof(addrs))
693 if (req->oldlen != sizeof(struct ucred))
695 error = SYSCTL_IN(req, addrs, sizeof(addrs));
700 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
703 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
707 SCTP_INP_DECR_REF(inp);
708 SCTP_INP_WUNLOCK(inp);
712 error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
713 sizeof(struct ucred));
715 SCTP_TCB_UNLOCK (stcb);
721 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
723 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
727 /* This is the same as the sctp_abort() could be made common */
729 sctp6_abort(struct socket *so)
731 struct sctp_inpcb *inp;
733 inp = (struct sctp_inpcb *)so->so_pcb;
735 return EINVAL; /* ??? possible? panic instead? */
736 soisdisconnected(so);
738 sctp_inpcb_free(inp, 1);
744 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
745 sctp6_attach(struct socket *so, int proto, struct thread *p)
746 #elif defined(__DragonFly__)
747 sctp6_attach(struct socket *so, int proto, struct pru_attach_info *ai)
749 sctp6_attach(struct socket *so, int proto, struct proc *p)
754 struct sctp_inpcb *inp;
756 inp = (struct sctp_inpcb *)so->so_pcb;
760 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
761 error = soreserve(so, sctp_sendspace, sctp_recvspace, NULL);
766 error = sctp_inpcb_alloc(so);
770 inp = (struct sctp_inpcb *)so->so_pcb;
771 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
772 inp6 = (struct in6pcb *)inp;
774 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
775 inp6->inp_vflag |= INP_IPV6;
777 #if defined(__OpenBSD__)
778 inp->ip_inp.inp.inp_flags |= INP_IPV6;
780 inp->inp_vflag |= INP_IPV6;
783 #if defined(__NetBSD__)
785 inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
787 so->so_send = sctp_sosend;
789 inp6->in6p_hops = -1; /* use kernel default */
790 inp6->in6p_cksum = -1; /* just to be sure */
794 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
795 * because the socket may be bound to an IPv6 wildcard address,
796 * which may match an IPv4-mapped IPv6 address.
798 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
799 inp6->inp_ip_ttl = ip_defttl;
801 inp->inp_ip_ttl = ip_defttl;
805 * Hmm what about the IPSEC stuff that is missing here but
812 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
813 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
816 #if defined(__FreeBSD__) || defined(__APPLE__)
817 sctp6_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
820 sctp6_bind(struct socket *so, struct mbuf *nam, struct proc *p)
822 struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
825 struct sctp_inpcb *inp;
829 inp = (struct sctp_inpcb *)so->so_pcb;
833 inp6 = (struct in6pcb *)inp;
834 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
835 inp6->inp_vflag &= ~INP_IPV4;
836 inp6->inp_vflag |= INP_IPV6;
838 #if defined(__OpenBSD__)
839 inp->ip_inp.inp.inp_flags &= ~INP_IPV4;
840 inp->ip_inp.inp.inp_flags |= INP_IPV6;
842 inp->inp_vflag &= ~INP_IPV4;
843 inp->inp_vflag |= INP_IPV6;
847 #if defined(__OpenBSD__)
848 (0) /* we always do dual bind */
849 #elif defined (__NetBSD__)
850 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
852 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
855 if (addr->sa_family == AF_INET) {
856 /* binding v4 addr to v6 socket, so reset flags */
857 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
858 inp6->inp_vflag |= INP_IPV4;
859 inp6->inp_vflag &= ~INP_IPV6;
861 #if defined(__OpenBSD__)
862 inp->ip_inp.inp.inp_flags |= INP_IPV4;
863 inp->ip_inp.inp.inp_flags &= ~INP_IPV6;
865 inp->inp_vflag |= INP_IPV4;
866 inp->inp_vflag &= ~INP_IPV6;
870 struct sockaddr_in6 *sin6_p;
871 sin6_p = (struct sockaddr_in6 *)addr;
873 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
874 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
875 inp6->inp_vflag |= INP_IPV4;
877 #if defined(__OpenBSD__)
878 inp->ip_inp.inp.inp_flags |= INP_IPV4;
880 inp->inp_vflag |= INP_IPV4;
884 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
885 struct sockaddr_in sin;
886 in6_sin6_2_sin(&sin, sin6_p);
887 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
888 inp6->inp_vflag |= INP_IPV4;
889 inp6->inp_vflag &= ~INP_IPV6;
891 #if defined(__OpenBSD__)
892 inp->ip_inp.inp.inp_flags |= INP_IPV4;
893 inp->ip_inp.inp.inp_flags &= ~INP_IPV6;
896 inp->inp_vflag |= INP_IPV4;
897 inp->inp_vflag &= ~INP_IPV6;
901 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p);
906 } else if (addr != NULL) {
907 /* IPV6_V6ONLY socket */
908 if (addr->sa_family == AF_INET) {
909 /* can't bind v4 addr to v6 only socket! */
912 struct sockaddr_in6 *sin6_p;
913 sin6_p = (struct sockaddr_in6 *)addr;
915 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
916 /* can't bind v4-mapped addrs either! */
917 /* NOTE: we don't support SIIT */
922 error = sctp_inpcb_bind(so, addr, p);
927 /*This could be made common with sctp_detach() since they are identical */
929 sctp6_detach(struct socket *so)
931 struct sctp_inpcb *inp;
933 inp = (struct sctp_inpcb *)so->so_pcb;
937 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
938 (so->so_rcv.sb_cc > 0))
939 sctp_inpcb_free(inp, 1);
941 sctp_inpcb_free(inp, 0);
947 sctp6_disconnect(struct socket *so)
949 struct sctp_inpcb *inp;
952 inp = (struct sctp_inpcb *)so->so_pcb;
957 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
958 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
963 int some_on_streamwheel = 0;
964 struct sctp_association *asoc;
965 struct sctp_tcb *stcb;
967 stcb = LIST_FIRST(&inp->sctp_asoc_list);
973 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
974 /* Check to see if some data queued */
975 struct sctp_stream_out *outs;
976 TAILQ_FOREACH(outs, &asoc->out_wheel,
978 if (!TAILQ_EMPTY(&outs->outqueue)) {
979 some_on_streamwheel = 1;
985 if (TAILQ_EMPTY(&asoc->send_queue) &&
986 TAILQ_EMPTY(&asoc->sent_queue) &&
987 (some_on_streamwheel == 0)) {
988 /* nothing queued to send, so I'm done... */
989 if ((SCTP_GET_STATE(asoc) !=
990 SCTP_STATE_SHUTDOWN_SENT) &&
991 (SCTP_GET_STATE(asoc) !=
992 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
993 /* only send SHUTDOWN the first time */
995 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
996 printf("%s:%d sends a shutdown\n",
1002 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1003 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1004 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1005 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1006 stcb->sctp_ep, stcb,
1007 asoc->primary_destination);
1008 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1009 stcb->sctp_ep, stcb,
1010 asoc->primary_destination);
1014 * we still got (or just got) data to send,
1015 * so set SHUTDOWN_PENDING
1018 * XXX sockets draft says that MSG_EOF should
1019 * be sent with no data. currently, we will
1020 * allow user data to be sent first and move
1021 * to SHUTDOWN-PENDING
1023 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1029 /* UDP model does not support this */
1036 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1037 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1038 struct mbuf *control, struct thread *p);
1040 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1041 struct mbuf *control, struct proc *p);
1046 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1047 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1048 struct mbuf *control, struct thread *p)
1051 #if defined(__FreeBSD__) || defined(__APPLE__)
1052 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1053 struct mbuf *control, struct proc *p)
1056 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam,
1057 struct mbuf *control, struct proc *p)
1059 struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
1062 struct sctp_inpcb *inp;
1063 struct inpcb *in_inp;
1064 struct in6pcb *inp6;
1066 struct sockaddr_in6 *sin6;
1068 /* No SPL needed since sctp_output does this */
1070 inp = (struct sctp_inpcb *)so->so_pcb;
1079 in_inp = (struct inpcb *)inp;
1080 inp6 = (struct in6pcb *)inp;
1081 /* For the TCP model we may get a NULL addr, if we
1082 * are a connected socket thats ok.
1084 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
1086 goto connected_type;
1094 return (EDESTADDRREQ);
1098 sin6 = (struct sockaddr_in6 *)addr;
1101 #if defined(__OpenBSD__)
1102 (0) /* we always do dual bind */
1103 #elif defined (__NetBSD__)
1104 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1106 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1110 * if IPV6_V6ONLY flag, we discard datagrams
1111 * destined to a v4 addr or v4-mapped addr
1113 if (addr->sa_family == AF_INET) {
1116 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1121 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1123 struct sockaddr_in sin;
1124 /* convert v4-mapped into v4 addr and send */
1125 in6_sin6_2_sin(&sin, sin6);
1126 return sctp_send(so, flags, m, (struct sockaddr *)&sin,
1129 /* mapped addresses aren't enabled */
1135 /* now what about control */
1138 printf("huh? control set?\n");
1139 m_freem(inp->control);
1140 inp->control = NULL;
1142 inp->control = control;
1144 /* add it in possibly */
1146 (inp->pkt->m_flags & M_PKTHDR)) {
1152 for (x=m;x;x = x->m_next) {
1155 inp->pkt->m_pkthdr.len += c_len;
1157 /* Place the data */
1159 inp->pkt_last->m_next = m;
1162 inp->pkt_last = inp->pkt = m;
1165 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1166 /* FreeBSD and MacOSX uses a flag passed */
1167 ((flags & PRUS_MORETOCOME) == 0)
1168 #elif defined(__NetBSD__)
1169 /* NetBSD uses the so_state field */
1170 ((so->so_state & SS_MORETOCOME) == 0)
1172 1 /* Open BSD does not have any "more to come" indication */
1176 * note with the current version this code will only be
1177 * used by OpenBSD, NetBSD and FreeBSD have methods for
1178 * re-defining sosend() to use sctp_sosend(). One can
1179 * optionaly switch back to this code (by changing back
1180 * the defininitions but this is not advisable.
1183 ret = sctp_output(inp, inp->pkt , addr, inp->control, p, flags);
1185 inp->control = NULL;
1193 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1194 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
1197 #if defined(__FreeBSD__) || defined(__APPLE__)
1198 sctp6_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
1201 sctp6_connect(struct socket *so, struct mbuf *nam, struct proc *p)
1203 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1207 struct sctp_inpcb *inp;
1208 struct in6pcb *inp6;
1209 struct sctp_tcb *stcb;
1211 struct sockaddr_in6 *sin6;
1212 struct sockaddr_storage ss;
1216 inp6 = (struct in6pcb *)so->so_pcb;
1217 inp = (struct sctp_inpcb *)so->so_pcb;
1220 return (ECONNRESET); /* I made the same as TCP since
1221 * we are not setup? */
1223 SCTP_ASOC_CREATE_LOCK(inp);
1224 SCTP_INP_RLOCK(inp);
1225 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1226 SCTP_PCB_FLAGS_UNBOUND) {
1227 /* Bind a ephemeral port */
1228 SCTP_INP_RUNLOCK(inp);
1229 error = sctp6_bind(so, NULL, p);
1232 SCTP_ASOC_CREATE_UNLOCK(inp);
1236 SCTP_INP_RLOCK(inp);
1239 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1240 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1241 /* We are already connected AND the TCP model */
1243 SCTP_INP_RUNLOCK(inp);
1244 SCTP_ASOC_CREATE_UNLOCK(inp);
1245 return (EADDRINUSE);
1249 sin6 = (struct sockaddr_in6 *)addr;
1251 #if defined(__OpenBSD__)
1252 (0) /* we always do dual bind */
1253 #elif defined (__NetBSD__)
1254 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1256 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1260 * if IPV6_V6ONLY flag, ignore connections
1261 * destined to a v4 addr or v4-mapped addr
1263 if (addr->sa_family == AF_INET) {
1265 SCTP_INP_RUNLOCK(inp);
1266 SCTP_ASOC_CREATE_UNLOCK(inp);
1269 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1271 SCTP_INP_RUNLOCK(inp);
1272 SCTP_ASOC_CREATE_UNLOCK(inp);
1277 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1279 /* convert v4-mapped into v4 addr */
1280 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1281 addr = (struct sockaddr *)&ss;
1283 /* mapped addresses aren't enabled */
1285 SCTP_INP_RUNLOCK(inp);
1286 SCTP_ASOC_CREATE_UNLOCK(inp);
1291 addr = addr; /* for true v6 address case */
1293 /* Now do we connect? */
1294 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1295 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1297 SCTP_TCB_UNLOCK (stcb);
1298 SCTP_INP_RUNLOCK(inp);
1300 SCTP_INP_RUNLOCK(inp);
1301 SCTP_INP_WLOCK(inp);
1302 SCTP_INP_INCR_REF(inp);
1303 SCTP_INP_WUNLOCK(inp);
1304 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1306 SCTP_INP_WLOCK(inp);
1307 SCTP_INP_DECR_REF(inp);
1308 SCTP_INP_WUNLOCK(inp);
1313 /* Already have or am bring up an association */
1314 SCTP_ASOC_CREATE_UNLOCK(inp);
1315 SCTP_TCB_UNLOCK (stcb);
1319 /* We are GOOD to go */
1320 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
1321 SCTP_ASOC_CREATE_UNLOCK(inp);
1323 /* Gak! no memory */
1327 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1328 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1329 /* Set the connected flag so we can queue data */
1332 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1333 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1334 sctp_send_initiate(inp, stcb);
1335 SCTP_TCB_UNLOCK (stcb);
1341 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1342 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1344 struct sockaddr_in6 *sin6;
1346 sctp6_getaddr(struct socket *so, struct mbuf *nam)
1348 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1350 struct sctp_inpcb *inp;
1352 * Do the malloc first in case it blocks.
1354 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1355 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
1358 nam->m_len = sizeof(*sin6);
1360 bzero(sin6, sizeof(*sin6));
1361 sin6->sin6_family = AF_INET6;
1362 sin6->sin6_len = sizeof(*sin6);
1364 inp = (struct sctp_inpcb *)so->so_pcb;
1366 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1367 FREE(sin6, M_SONAME);
1372 sin6->sin6_port = inp->sctp_lport;
1373 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1374 /* For the bound all case you get back 0 */
1375 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1376 struct sctp_tcb *stcb;
1377 struct sockaddr_in6 *sin_a6;
1378 struct sctp_nets *net;
1381 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1387 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1388 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1389 if (sin_a6->sin6_family == AF_INET6) {
1394 if ((!fnd) || (sin_a6 == NULL)) {
1398 sin6->sin6_addr = sctp_ipv6_source_address_selection(
1399 inp, stcb, (struct route *)&net->ro, net, 0);
1402 /* For the bound all case you get back 0 */
1404 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1407 /* Take the first IPv6 address in the list */
1408 struct sctp_laddr *laddr;
1410 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1411 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
1412 struct sockaddr_in6 *sin_a;
1413 sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1414 sin6->sin6_addr = sin_a->sin6_addr;
1420 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1421 FREE(sin6, M_SONAME);
1426 /* Scoping things for v6 */
1427 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1428 /* skip ifp check below */
1429 in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1431 sin6->sin6_scope_id = 0; /*XXX*/
1432 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1433 (*addr) = (struct sockaddr *)sin6;
1439 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1440 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1442 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1444 sctp6_peeraddr(struct socket *so, struct mbuf *nam)
1446 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1449 struct sockaddr_in6 *sin_a6;
1450 struct sctp_inpcb *inp;
1451 struct sctp_tcb *stcb;
1452 struct sctp_nets *net;
1454 * Do the malloc first in case it blocks.
1456 inp = (struct sctp_inpcb *)so->so_pcb;
1457 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1458 /* UDP type and listeners will drop out here */
1461 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1462 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
1465 nam->m_len = sizeof(*sin6);
1467 bzero(sin6, sizeof(*sin6));
1468 sin6->sin6_family = AF_INET6;
1469 sin6->sin6_len = sizeof(*sin6);
1471 /* We must recapture incase we blocked */
1472 inp = (struct sctp_inpcb *)so->so_pcb;
1474 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1475 FREE(sin6, M_SONAME);
1479 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1481 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1482 FREE(sin6, M_SONAME);
1487 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1488 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1489 if (sin_a6->sin6_family == AF_INET6) {
1491 sin6->sin6_port = stcb->rport;
1492 sin6->sin6_addr = sin_a6->sin6_addr;
1497 /* No IPv4 address */
1498 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1499 FREE(sin6, M_SONAME);
1503 in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1504 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1505 *addr = (struct sockaddr *)sin6;
1511 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1512 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1514 struct sockaddr *addr;
1516 sctp6_in6getaddr(struct socket *so, struct mbuf *nam)
1518 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1520 struct in6pcb *inp6 = sotoin6pcb(so);
1527 /* allow v6 addresses precedence */
1528 error = sctp6_getaddr(so, nam);
1530 /* try v4 next if v6 failed */
1531 error = sctp_ingetaddr(so, nam);
1536 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1539 /* if I'm V6ONLY, convert it to v4-mapped */
1541 #if defined(__OpenBSD__)
1542 (0) /* we always do dual bind */
1543 #elif defined (__NetBSD__)
1544 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1546 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1549 struct sockaddr_in6 sin6;
1550 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1551 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1552 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1553 nam->m_len = sizeof(sin6);
1555 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1557 nam->m_len = sizeof(struct sockaddr_in);
1560 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1562 nam->m_len = sizeof(struct sockaddr_in6);
1571 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1572 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1574 struct sockaddr *addr = *nam;
1576 sctp6_getpeeraddr(struct socket *so, struct mbuf *nam)
1578 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1580 struct in6pcb *inp6 = sotoin6pcb(so);
1587 /* allow v6 addresses precedence */
1588 error = sctp6_peeraddr(so, nam);
1590 /* try v4 next if v6 failed */
1591 error = sctp_peeraddr(so, nam);
1596 /* if I'm V6ONLY, convert it to v4-mapped */
1598 #if defined(__OpenBSD__)
1599 (0) /* we always do dual bind */
1600 #elif defined (__NetBSD__)
1601 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1603 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1606 struct sockaddr_in6 sin6;
1607 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1608 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1609 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1610 nam->m_len = sizeof(sin6);
1612 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1614 nam->m_len = sizeof(struct sockaddr_in);
1617 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1619 nam->m_len = sizeof(struct sockaddr_in6);
1626 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1627 struct pr_usrreqs sctp6_usrreqs = {
1628 sctp6_abort, sctp_accept, sctp6_attach, sctp6_bind,
1629 sctp6_connect, pru_connect2_notsupp, in6_control,
1630 sctp6_detach, sctp6_disconnect, sctp_listen, sctp6_getpeeraddr,
1631 sctp_usr_recvd, pru_rcvoob_notsupp, sctp6_send, pru_sense_null,
1632 sctp_shutdown, sctp6_in6getaddr, sctp_sosend, soreceive, sopoll
1638 sctp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1639 struct mbuf *control, struct proc *p)
1645 #if defined(__OpenBSD__)
1649 family = so->so_proto->pr_domain->dom_family;
1651 if (req == PRU_CONTROL) {
1654 error = in_control(so, (long)m, (caddr_t)nam,
1655 (struct ifnet *)control
1656 #if defined(__NetBSD__)
1662 error = in6_control(so, (long)m, (caddr_t)nam,
1663 (struct ifnet *)control, p);
1666 error = EAFNOSUPPORT;
1672 if (req == PRU_PURGEIF) {
1675 ifn = (struct ifnet *)control;
1676 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
1677 if (ifa->ifa_addr->sa_family == family) {
1678 sctp_delete_ip_address(ifa);
1692 return (EAFNOSUPPORT);
1700 error = sctp6_attach(so, family, p);
1703 error = sctp6_detach(so);
1708 error = sctp6_bind(so, nam, p);
1711 error = sctp_listen(so, p);
1716 error = sctp6_connect(so, nam, p);
1718 case PRU_DISCONNECT:
1719 error = sctp6_disconnect(so);
1724 error = sctp_accept(so, nam);
1727 error = sctp_shutdown(so);
1732 * For OpenBSD and NetBSD, this is real ugly. The (mbuf *)
1733 * nam that is passed (by soreceive()) is the int flags
1734 * cast as a (mbuf *) yuck!
1736 error = sctp_usr_recvd(so, (int)((long)nam));
1740 /* Flags are ignored */
1741 error = sctp6_send(so, 0, m, nam, control, p);
1744 error = sctp6_abort(so);
1751 error = EAFNOSUPPORT;
1754 error = EAFNOSUPPORT;
1757 error = sctp6_getpeeraddr(so, nam);
1760 error = sctp6_in6getaddr(so, nam);