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.3 2005/07/15 15:52:00 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>
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__) || defined(__DragonFly__)
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__) || defined(__DragonFly__))
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__) || defined(__DragonFly__)
423 #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 501113) || \
424 defined(__APPLE__) || defined(__DragonFly__)
425 ip6_savecontrol(in6p_ip, &opts, ip6, m);
426 #elif __FreeBSD_version >= 440000 || (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version >= 501113)
427 ip6_savecontrol(in6p_ip, m, &opts);
429 ip6_savecontrol(in6p_ip, m, &opts, NULL);
432 ip6_savecontrol((struct in6pcb *)in6p_ip, m, &opts);
437 * CONTROL chunk processing
439 length = ntohs(ip6->ip6_plen) + iphlen;
440 offset -= sizeof(*ch);
441 ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
442 #if defined(__NetBSD__) || defined(__OpenBSD__)
447 (void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
448 in6p, stcb, net, ecn_bits);
449 /* inp's ref-count reduced && stcb unlocked */
451 /* XXX this stuff below gets moved to appropriate parts later... */
457 if ((in6p) && refcount_up){
458 /* reduce ref-count */
459 SCTP_INP_WLOCK(in6p);
460 SCTP_INP_DECR_REF(in6p);
461 SCTP_INP_WUNLOCK(in6p);
468 SCTP_TCB_UNLOCK(stcb);
470 if ((in6p) && refcount_up){
471 /* reduce ref-count */
472 SCTP_INP_WLOCK(in6p);
473 SCTP_INP_DECR_REF(in6p);
474 SCTP_INP_WUNLOCK(in6p);
485 sctp6_notify_mbuf(struct sctp_inpcb *inp,
486 struct icmp6_hdr *icmp6,
488 struct sctp_tcb *stcb,
489 struct sctp_nets *net)
493 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
494 (icmp6 == NULL) || (sh == NULL)) {
498 /* First do we even look at it? */
499 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
502 if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
503 /* not PACKET TO BIG */
507 * ok we need to look closely. We could even get smarter and
508 * look at anyone that we sent to in case we get a different
509 * ICMP that tells us there is no way to reach a host, but for
510 * this impl, all we care about is MTU discovery.
512 nxtsz = ntohl(icmp6->icmp6_mtu);
513 /* Stop any PMTU timer */
514 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
516 /* Adjust destination size limit */
517 if (net->mtu > nxtsz) {
520 /* now what about the ep? */
521 if (stcb->asoc.smallest_mtu > nxtsz) {
522 struct sctp_tmit_chunk *chk;
523 struct sctp_stream_out *strm;
524 /* Adjust that too */
525 stcb->asoc.smallest_mtu = nxtsz;
526 /* now off to subtract IP_DF flag if needed */
528 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
529 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
530 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
533 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
534 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
536 * For this guy we also mark for immediate
537 * resend since we sent to big of chunk
539 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
540 if (chk->sent != SCTP_DATAGRAM_RESEND)
541 stcb->asoc.sent_queue_retran_cnt++;
542 chk->sent = SCTP_DATAGRAM_RESEND;
543 chk->rec.data.doing_fast_retransmit = 0;
545 chk->sent = SCTP_DATAGRAM_RESEND;
546 /* Clear any time so NO RTT is being done */
547 chk->sent_rcv_time.tv_sec = 0;
548 chk->sent_rcv_time.tv_usec = 0;
549 stcb->asoc.total_flight -= chk->send_size;
550 net->flight_size -= chk->send_size;
553 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
554 TAILQ_FOREACH(chk, &strm->outqueue, sctp_next) {
555 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
556 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
561 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
564 /* reduce inp's ref-count */
566 SCTP_INP_DECR_REF(inp);
567 SCTP_INP_WUNLOCK(inp);
570 SCTP_TCB_UNLOCK(stcb);
575 sctp6_ctlinput(cmd, pktdst, d)
577 struct sockaddr *pktdst;
581 struct ip6ctlparam *ip6cp = NULL;
584 if (pktdst->sa_family != AF_INET6 ||
585 pktdst->sa_len != sizeof(struct sockaddr_in6))
588 if ((unsigned)cmd >= PRC_NCMDS)
590 if (PRC_IS_REDIRECT(cmd)) {
592 } else if (inet6ctlerrmap[cmd] == 0) {
596 /* if the parameter is from icmp6, decode it. */
598 ip6cp = (struct ip6ctlparam *)d;
600 ip6cp = (struct ip6ctlparam *)NULL;
605 * XXX: We assume that when IPV6 is non NULL,
606 * M and OFF are valid.
608 /* check if we can safely examine src and dst ports */
609 struct sctp_inpcb *inp;
610 struct sctp_tcb *stcb;
611 struct sctp_nets *net;
612 struct sockaddr_in6 final;
614 if (ip6cp->ip6c_m == NULL ||
615 (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh)))
618 bzero(&sh, sizeof(sh));
619 bzero(&final, sizeof(final));
622 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
624 ip6cp->ip6c_src->sin6_port = sh.src_port;
625 final.sin6_len = sizeof(final);
626 final.sin6_family = AF_INET6;
627 #if defined(__FreeBSD__) && __FreeBSD_cc_version < 440000
628 final.sin6_addr = *ip6cp->ip6c_finaldst;
630 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
631 #endif /* __FreeBSD_cc_version */
632 final.sin6_port = sh.dest_port;
633 #if defined(__NetBSD__) || defined(__OpenBSD__)
638 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
639 (struct sockaddr *)&final,
641 /* inp's ref-count increased && stcb locked */
642 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
643 if (cmd == PRC_MSGSIZE) {
644 sctp6_notify_mbuf(inp,
649 /* inp's ref-count reduced && stcb unlocked */
651 if (cmd == PRC_HOSTDEAD) {
654 cm = inet6ctlerrmap[cmd];
656 sctp_notify(inp, cm, &sh,
657 (struct sockaddr *)&final,
659 /* inp's ref-count reduced && stcb unlocked */
662 if (PRC_IS_REDIRECT(cmd) && inp) {
664 in_rtchange((struct inpcb *)inp,
667 in6_rtchange((struct in6pcb *)inp,
668 inet6ctlerrmap[cmd]);
672 /* reduce inp's ref-count */
674 SCTP_INP_DECR_REF(inp);
675 SCTP_INP_WUNLOCK(inp);
678 SCTP_TCB_UNLOCK(stcb);
685 * this routine can probably be collasped into the one in sctp_userreq.c
686 * since they do the same thing and now we lookup with a sockaddr
690 sctp6_getcred(SYSCTL_HANDLER_ARGS)
692 struct sockaddr_in6 addrs[2];
693 struct sctp_inpcb *inp;
694 struct sctp_nets *net;
695 struct sctp_tcb *stcb;
698 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
699 error = suser(req->td);
701 error = suser(req->p);
706 if (req->newlen != sizeof(addrs))
708 if (req->oldlen != sizeof(struct ucred))
710 error = SYSCTL_IN(req, addrs, sizeof(addrs));
713 #if defined(__NetBSD__) || defined(__OpenBSD__)
719 stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
722 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
726 SCTP_INP_DECR_REF(inp);
727 SCTP_INP_WUNLOCK(inp);
731 error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
732 sizeof(struct ucred));
734 SCTP_TCB_UNLOCK (stcb);
740 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
742 sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
746 /* This is the same as the sctp_abort() could be made common */
748 sctp6_abort(struct socket *so)
750 struct sctp_inpcb *inp;
753 inp = (struct sctp_inpcb *)so->so_pcb;
755 return EINVAL; /* ??? possible? panic instead? */
756 soisdisconnected(so);
757 #if defined(__NetBSD__) || defined(__OpenBSD__)
762 sctp_inpcb_free(inp, 1);
768 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
769 sctp6_attach(struct socket *so, int proto, struct thread *p)
770 #elif defined(__DragonFly__)
771 sctp6_attach(struct socket *so, int proto, struct pru_attach_info *ai)
773 sctp6_attach(struct socket *so, int proto, struct proc *p)
778 struct sctp_inpcb *inp;
780 inp = (struct sctp_inpcb *)so->so_pcb;
784 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
785 error = soreserve(so, sctp_sendspace, sctp_recvspace, NULL);
789 #if defined(__NetBSD__) || defined(__OpenBSD__)
794 error = sctp_inpcb_alloc(so);
798 inp = (struct sctp_inpcb *)so->so_pcb;
799 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
800 inp6 = (struct in6pcb *)inp;
802 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
803 inp6->inp_vflag |= INP_IPV6;
805 #if defined(__OpenBSD__)
806 inp->ip_inp.inp.inp_flags |= INP_IPV6;
808 inp->inp_vflag |= INP_IPV6;
811 #if defined(__NetBSD__)
813 inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
815 so->so_send = sctp_sosend;
817 inp6->in6p_hops = -1; /* use kernel default */
818 inp6->in6p_cksum = -1; /* just to be sure */
822 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
823 * because the socket may be bound to an IPv6 wildcard address,
824 * which may match an IPv4-mapped IPv6 address.
826 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
827 inp6->inp_ip_ttl = ip_defttl;
829 inp->inp_ip_ttl = ip_defttl;
833 * Hmm what about the IPSEC stuff that is missing here but
840 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
841 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
844 #if defined(__FreeBSD__) || defined(__APPLE__)
845 sctp6_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
848 sctp6_bind(struct socket *so, struct mbuf *nam, struct proc *p)
850 struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
853 struct sctp_inpcb *inp;
857 inp = (struct sctp_inpcb *)so->so_pcb;
861 inp6 = (struct in6pcb *)inp;
862 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
863 inp6->inp_vflag &= ~INP_IPV4;
864 inp6->inp_vflag |= INP_IPV6;
866 #if defined(__OpenBSD__)
867 inp->ip_inp.inp.inp_flags &= ~INP_IPV4;
868 inp->ip_inp.inp.inp_flags |= INP_IPV6;
870 inp->inp_vflag &= ~INP_IPV4;
871 inp->inp_vflag |= INP_IPV6;
875 #if defined(__OpenBSD__)
876 (0) /* we always do dual bind */
877 #elif defined (__NetBSD__)
878 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
880 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
883 if (addr->sa_family == AF_INET) {
884 /* binding v4 addr to v6 socket, so reset flags */
885 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
886 inp6->inp_vflag |= INP_IPV4;
887 inp6->inp_vflag &= ~INP_IPV6;
889 #if defined(__OpenBSD__)
890 inp->ip_inp.inp.inp_flags |= INP_IPV4;
891 inp->ip_inp.inp.inp_flags &= ~INP_IPV6;
893 inp->inp_vflag |= INP_IPV4;
894 inp->inp_vflag &= ~INP_IPV6;
898 struct sockaddr_in6 *sin6_p;
899 sin6_p = (struct sockaddr_in6 *)addr;
901 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
902 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
903 inp6->inp_vflag |= INP_IPV4;
905 #if defined(__OpenBSD__)
906 inp->ip_inp.inp.inp_flags |= INP_IPV4;
908 inp->inp_vflag |= INP_IPV4;
912 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
913 struct sockaddr_in sin;
914 in6_sin6_2_sin(&sin, sin6_p);
915 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
916 inp6->inp_vflag |= INP_IPV4;
917 inp6->inp_vflag &= ~INP_IPV6;
919 #if defined(__OpenBSD__)
920 inp->ip_inp.inp.inp_flags |= INP_IPV4;
921 inp->ip_inp.inp.inp_flags &= ~INP_IPV6;
924 inp->inp_vflag |= INP_IPV4;
925 inp->inp_vflag &= ~INP_IPV6;
928 #if defined(__NetBSD__) || defined(__OpenBSD__)
933 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p);
938 } else if (addr != NULL) {
939 /* IPV6_V6ONLY socket */
940 if (addr->sa_family == AF_INET) {
941 /* can't bind v4 addr to v6 only socket! */
944 struct sockaddr_in6 *sin6_p;
945 sin6_p = (struct sockaddr_in6 *)addr;
947 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
948 /* can't bind v4-mapped addrs either! */
949 /* NOTE: we don't support SIIT */
953 #if defined(__NetBSD__) || defined(__OpenBSD__)
958 error = sctp_inpcb_bind(so, addr, p);
963 /*This could be made common with sctp_detach() since they are identical */
965 sctp6_detach(struct socket *so)
967 struct sctp_inpcb *inp;
970 inp = (struct sctp_inpcb *)so->so_pcb;
973 #if defined(__NetBSD__) || defined(__OpenBSD__)
978 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
979 (so->so_rcv.sb_cc > 0))
980 sctp_inpcb_free(inp, 1);
982 sctp_inpcb_free(inp, 0);
988 sctp6_disconnect(struct socket *so)
990 struct sctp_inpcb *inp;
993 #if defined(__NetBSD__) || defined(__OpenBSD__)
996 s = splnet(); /* XXX */
998 inp = (struct sctp_inpcb *)so->so_pcb;
1003 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1004 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
1009 int some_on_streamwheel = 0;
1010 struct sctp_association *asoc;
1011 struct sctp_tcb *stcb;
1013 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1019 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
1020 /* Check to see if some data queued */
1021 struct sctp_stream_out *outs;
1022 TAILQ_FOREACH(outs, &asoc->out_wheel,
1024 if (!TAILQ_EMPTY(&outs->outqueue)) {
1025 some_on_streamwheel = 1;
1031 if (TAILQ_EMPTY(&asoc->send_queue) &&
1032 TAILQ_EMPTY(&asoc->sent_queue) &&
1033 (some_on_streamwheel == 0)) {
1034 /* nothing queued to send, so I'm done... */
1035 if ((SCTP_GET_STATE(asoc) !=
1036 SCTP_STATE_SHUTDOWN_SENT) &&
1037 (SCTP_GET_STATE(asoc) !=
1038 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1039 /* only send SHUTDOWN the first time */
1041 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1042 printf("%s:%d sends a shutdown\n",
1048 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
1049 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1050 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1051 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1052 stcb->sctp_ep, stcb,
1053 asoc->primary_destination);
1054 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1055 stcb->sctp_ep, stcb,
1056 asoc->primary_destination);
1060 * we still got (or just got) data to send,
1061 * so set SHUTDOWN_PENDING
1064 * XXX sockets draft says that MSG_EOF should
1065 * be sent with no data. currently, we will
1066 * allow user data to be sent first and move
1067 * to SHUTDOWN-PENDING
1069 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1075 /* UDP model does not support this */
1082 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1083 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1084 struct mbuf *control, struct thread *p);
1086 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1087 struct mbuf *control, struct proc *p);
1092 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1093 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1094 struct mbuf *control, struct thread *p)
1097 #if defined(__FreeBSD__) || defined(__APPLE__)
1098 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1099 struct mbuf *control, struct proc *p)
1102 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam,
1103 struct mbuf *control, struct proc *p)
1105 struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
1108 struct sctp_inpcb *inp;
1109 struct inpcb *in_inp;
1110 struct in6pcb *inp6;
1112 struct sockaddr_in6 *sin6;
1114 /* No SPL needed since sctp_output does this */
1116 inp = (struct sctp_inpcb *)so->so_pcb;
1125 in_inp = (struct inpcb *)inp;
1126 inp6 = (struct in6pcb *)inp;
1127 /* For the TCP model we may get a NULL addr, if we
1128 * are a connected socket thats ok.
1130 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
1132 goto connected_type;
1140 return (EDESTADDRREQ);
1144 sin6 = (struct sockaddr_in6 *)addr;
1147 #if defined(__OpenBSD__)
1148 (0) /* we always do dual bind */
1149 #elif defined (__NetBSD__)
1150 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1152 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1156 * if IPV6_V6ONLY flag, we discard datagrams
1157 * destined to a v4 addr or v4-mapped addr
1159 if (addr->sa_family == AF_INET) {
1162 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1167 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1169 struct sockaddr_in sin;
1170 /* convert v4-mapped into v4 addr and send */
1171 in6_sin6_2_sin(&sin, sin6);
1172 return sctp_send(so, flags, m, (struct sockaddr *)&sin,
1175 /* mapped addresses aren't enabled */
1181 /* now what about control */
1184 printf("huh? control set?\n");
1185 m_freem(inp->control);
1186 inp->control = NULL;
1188 inp->control = control;
1190 /* add it in possibly */
1192 (inp->pkt->m_flags & M_PKTHDR)) {
1198 for (x=m;x;x = x->m_next) {
1201 inp->pkt->m_pkthdr.len += c_len;
1203 /* Place the data */
1205 inp->pkt_last->m_next = m;
1208 inp->pkt_last = inp->pkt = m;
1211 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1212 /* FreeBSD and MacOSX uses a flag passed */
1213 ((flags & PRUS_MORETOCOME) == 0)
1214 #elif defined(__NetBSD__)
1215 /* NetBSD uses the so_state field */
1216 ((so->so_state & SS_MORETOCOME) == 0)
1218 1 /* Open BSD does not have any "more to come" indication */
1222 * note with the current version this code will only be
1223 * used by OpenBSD, NetBSD and FreeBSD have methods for
1224 * re-defining sosend() to use sctp_sosend(). One can
1225 * optionaly switch back to this code (by changing back
1226 * the defininitions but this is not advisable.
1229 ret = sctp_output(inp, inp->pkt , addr, inp->control, p, flags);
1231 inp->control = NULL;
1239 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1240 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
1243 #if defined(__FreeBSD__) || defined(__APPLE__)
1244 sctp6_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
1247 sctp6_connect(struct socket *so, struct mbuf *nam, struct proc *p)
1249 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1252 #if defined(__NetBSD__) || defined(__OpenBSD__)
1253 int s = splsoftnet();
1258 struct sctp_inpcb *inp;
1259 struct in6pcb *inp6;
1260 struct sctp_tcb *stcb;
1262 struct sockaddr_in6 *sin6;
1263 struct sockaddr_storage ss;
1266 inp6 = (struct in6pcb *)so->so_pcb;
1267 inp = (struct sctp_inpcb *)so->so_pcb;
1270 return (ECONNRESET); /* I made the same as TCP since
1271 * we are not setup? */
1273 SCTP_ASOC_CREATE_LOCK(inp);
1274 SCTP_INP_RLOCK(inp);
1275 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1276 SCTP_PCB_FLAGS_UNBOUND) {
1277 /* Bind a ephemeral port */
1278 SCTP_INP_RUNLOCK(inp);
1279 error = sctp6_bind(so, NULL, p);
1282 SCTP_ASOC_CREATE_UNLOCK(inp);
1286 SCTP_INP_RLOCK(inp);
1289 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1290 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1291 /* We are already connected AND the TCP model */
1293 SCTP_INP_RUNLOCK(inp);
1294 SCTP_ASOC_CREATE_UNLOCK(inp);
1295 return (EADDRINUSE);
1299 sin6 = (struct sockaddr_in6 *)addr;
1301 #if defined(__OpenBSD__)
1302 (0) /* we always do dual bind */
1303 #elif defined (__NetBSD__)
1304 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1306 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1310 * if IPV6_V6ONLY flag, ignore connections
1311 * destined to a v4 addr or v4-mapped addr
1313 if (addr->sa_family == AF_INET) {
1315 SCTP_INP_RUNLOCK(inp);
1316 SCTP_ASOC_CREATE_UNLOCK(inp);
1319 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1321 SCTP_INP_RUNLOCK(inp);
1322 SCTP_ASOC_CREATE_UNLOCK(inp);
1327 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1329 /* convert v4-mapped into v4 addr */
1330 in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1331 addr = (struct sockaddr *)&ss;
1333 /* mapped addresses aren't enabled */
1335 SCTP_INP_RUNLOCK(inp);
1336 SCTP_ASOC_CREATE_UNLOCK(inp);
1341 addr = addr; /* for true v6 address case */
1343 /* Now do we connect? */
1344 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1345 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1347 SCTP_TCB_UNLOCK (stcb);
1348 SCTP_INP_RUNLOCK(inp);
1350 SCTP_INP_RUNLOCK(inp);
1351 SCTP_INP_WLOCK(inp);
1352 SCTP_INP_INCR_REF(inp);
1353 SCTP_INP_WUNLOCK(inp);
1354 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1356 SCTP_INP_WLOCK(inp);
1357 SCTP_INP_DECR_REF(inp);
1358 SCTP_INP_WUNLOCK(inp);
1363 /* Already have or am bring up an association */
1364 SCTP_ASOC_CREATE_UNLOCK(inp);
1365 SCTP_TCB_UNLOCK (stcb);
1369 /* We are GOOD to go */
1370 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
1371 SCTP_ASOC_CREATE_UNLOCK(inp);
1373 /* Gak! no memory */
1377 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1378 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1379 /* Set the connected flag so we can queue data */
1382 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1383 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1384 sctp_send_initiate(inp, stcb);
1385 SCTP_TCB_UNLOCK (stcb);
1391 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1392 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1394 struct sockaddr_in6 *sin6;
1396 sctp6_getaddr(struct socket *so, struct mbuf *nam)
1398 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1400 struct sctp_inpcb *inp;
1402 * Do the malloc first in case it blocks.
1404 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1405 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
1408 nam->m_len = sizeof(*sin6);
1410 bzero(sin6, sizeof(*sin6));
1411 sin6->sin6_family = AF_INET6;
1412 sin6->sin6_len = sizeof(*sin6);
1414 inp = (struct sctp_inpcb *)so->so_pcb;
1416 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1417 FREE(sin6, M_SONAME);
1422 sin6->sin6_port = inp->sctp_lport;
1423 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1424 /* For the bound all case you get back 0 */
1425 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1426 struct sctp_tcb *stcb;
1427 struct sockaddr_in6 *sin_a6;
1428 struct sctp_nets *net;
1431 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1437 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1438 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1439 if (sin_a6->sin6_family == AF_INET6) {
1444 if ((!fnd) || (sin_a6 == NULL)) {
1448 sin6->sin6_addr = sctp_ipv6_source_address_selection(
1449 inp, stcb, (struct route *)&net->ro, net, 0);
1452 /* For the bound all case you get back 0 */
1454 memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1457 /* Take the first IPv6 address in the list */
1458 struct sctp_laddr *laddr;
1460 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1461 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
1462 struct sockaddr_in6 *sin_a;
1463 sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1464 sin6->sin6_addr = sin_a->sin6_addr;
1470 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1471 FREE(sin6, M_SONAME);
1476 /* Scoping things for v6 */
1477 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1478 /* skip ifp check below */
1479 in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1481 sin6->sin6_scope_id = 0; /*XXX*/
1482 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1483 (*addr) = (struct sockaddr *)sin6;
1489 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1490 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1492 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1494 sctp6_peeraddr(struct socket *so, struct mbuf *nam)
1496 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1499 struct sockaddr_in6 *sin_a6;
1500 struct sctp_inpcb *inp;
1501 struct sctp_tcb *stcb;
1502 struct sctp_nets *net;
1504 * Do the malloc first in case it blocks.
1506 inp = (struct sctp_inpcb *)so->so_pcb;
1507 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1508 /* UDP type and listeners will drop out here */
1511 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1512 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
1515 nam->m_len = sizeof(*sin6);
1517 bzero(sin6, sizeof(*sin6));
1518 sin6->sin6_family = AF_INET6;
1519 sin6->sin6_len = sizeof(*sin6);
1521 /* We must recapture incase we blocked */
1522 inp = (struct sctp_inpcb *)so->so_pcb;
1524 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1525 FREE(sin6, M_SONAME);
1529 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1531 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1532 FREE(sin6, M_SONAME);
1537 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1538 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1539 if (sin_a6->sin6_family == AF_INET6) {
1541 sin6->sin6_port = stcb->rport;
1542 sin6->sin6_addr = sin_a6->sin6_addr;
1547 /* No IPv4 address */
1548 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1549 FREE(sin6, M_SONAME);
1553 in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1554 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1555 *addr = (struct sockaddr *)sin6;
1561 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1562 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1564 struct sockaddr *addr;
1566 sctp6_in6getaddr(struct socket *so, struct mbuf *nam)
1568 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1570 struct in6pcb *inp6 = sotoin6pcb(so);
1576 #if defined(__NetBSD__) || defined(__OpenBSD__)
1581 /* allow v6 addresses precedence */
1582 error = sctp6_getaddr(so, nam);
1584 /* try v4 next if v6 failed */
1585 error = sctp_ingetaddr(so, nam);
1590 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1593 /* if I'm V6ONLY, convert it to v4-mapped */
1595 #if defined(__OpenBSD__)
1596 (0) /* we always do dual bind */
1597 #elif defined (__NetBSD__)
1598 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1600 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1603 struct sockaddr_in6 sin6;
1604 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1605 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1606 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1607 nam->m_len = sizeof(sin6);
1609 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1611 nam->m_len = sizeof(struct sockaddr_in);
1614 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1616 nam->m_len = sizeof(struct sockaddr_in6);
1625 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1626 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1628 struct sockaddr *addr = *nam;
1630 sctp6_getpeeraddr(struct socket *so, struct mbuf *nam)
1632 struct sockaddr *addr = mtod(nam, struct sockaddr *);
1634 struct in6pcb *inp6 = sotoin6pcb(so);
1640 #if defined(__NetBSD__) || defined(__OpenBSD__)
1645 /* allow v6 addresses precedence */
1646 error = sctp6_peeraddr(so, nam);
1648 /* try v4 next if v6 failed */
1649 error = sctp_peeraddr(so, nam);
1654 /* if I'm V6ONLY, convert it to v4-mapped */
1656 #if defined(__OpenBSD__)
1657 (0) /* we always do dual bind */
1658 #elif defined (__NetBSD__)
1659 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1661 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1664 struct sockaddr_in6 sin6;
1665 in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1666 memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1667 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1668 nam->m_len = sizeof(sin6);
1670 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1672 nam->m_len = sizeof(struct sockaddr_in);
1675 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1677 nam->m_len = sizeof(struct sockaddr_in6);
1684 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1685 struct pr_usrreqs sctp6_usrreqs = {
1686 sctp6_abort, sctp_accept, sctp6_attach, sctp6_bind,
1687 sctp6_connect, pru_connect2_notsupp, in6_control,
1688 sctp6_detach, sctp6_disconnect, sctp_listen, sctp6_getpeeraddr,
1689 sctp_usr_recvd, pru_rcvoob_notsupp, sctp6_send, pru_sense_null,
1690 sctp_shutdown, sctp6_in6getaddr, sctp_sosend, soreceive, sopoll
1696 sctp6_usrreq(so, req, m, nam, control, p)
1699 struct mbuf *m, *nam, *control;
1706 #if defined(__OpenBSD__)
1710 family = so->so_proto->pr_domain->dom_family;
1712 if (req == PRU_CONTROL) {
1715 error = in_control(so, (long)m, (caddr_t)nam,
1716 (struct ifnet *)control
1717 #if defined(__NetBSD__)
1723 error = in6_control(so, (long)m, (caddr_t)nam,
1724 (struct ifnet *)control, p);
1727 error = EAFNOSUPPORT;
1733 if (req == PRU_PURGEIF) {
1736 ifn = (struct ifnet *)control;
1737 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
1738 if (ifa->ifa_addr->sa_family == family) {
1739 sctp_delete_ip_address(ifa);
1753 return (EAFNOSUPPORT);
1761 error = sctp6_attach(so, family, p);
1764 error = sctp6_detach(so);
1769 error = sctp6_bind(so, nam, p);
1772 error = sctp_listen(so, p);
1777 error = sctp6_connect(so, nam, p);
1779 case PRU_DISCONNECT:
1780 error = sctp6_disconnect(so);
1785 error = sctp_accept(so, nam);
1788 error = sctp_shutdown(so);
1793 * For OpenBSD and NetBSD, this is real ugly. The (mbuf *)
1794 * nam that is passed (by soreceive()) is the int flags
1795 * cast as a (mbuf *) yuck!
1797 error = sctp_usr_recvd(so, (int)((long)nam));
1801 /* Flags are ignored */
1802 error = sctp6_send(so, 0, m, nam, control, p);
1805 error = sctp6_abort(so);
1812 error = EAFNOSUPPORT;
1815 error = EAFNOSUPPORT;
1818 error = sctp6_getpeeraddr(so, nam);
1821 error = sctp6_in6getaddr(so, nam);