]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/sctp6_usrreq.c
This commit was generated by cvs2svn to compensate for changes in r170222,
[FreeBSD/FreeBSD.git] / sys / netinet6 / sctp6_usrreq.c
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*      $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $      */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34
35 #include <netinet/sctp_os.h>
36 #include <sys/proc.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
40 #if defined(INET6)
41 #include <netinet6/sctp6_var.h>
42 #endif
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_output.h>
53 #include <netinet/sctp_bsd_addr.h>
54
55
56 extern struct protosw inetsw[];
57
58
59
60
61 int
62 sctp6_input(i_pak, offp, proto)
63         struct mbuf **i_pak;
64         int *offp;
65         int proto;
66 {
67         struct mbuf *m;
68         struct ip6_hdr *ip6;
69         struct sctphdr *sh;
70         struct sctp_inpcb *in6p = NULL;
71         struct sctp_nets *net;
72         int refcount_up = 0;
73         uint32_t check, calc_check;
74         uint32_t vrf_id = 0;
75         struct inpcb *in6p_ip;
76         struct sctp_chunkhdr *ch;
77         int length, mlen, offset, iphlen;
78         uint8_t ecn_bits;
79         struct sctp_tcb *stcb = NULL;
80         int pkt_len = 0;
81         int off = *offp;
82
83         /* get the VRF and table id's */
84         if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
85                 SCTP_RELEASE_PKT(*i_pak);
86                 return (-1);
87         }
88         m = SCTP_HEADER_TO_CHAIN(*i_pak);
89         pkt_len = SCTP_HEADER_LEN((*i_pak));
90
91 #ifdef  SCTP_PACKET_LOGGING
92         sctp_packet_log(m, pkt_len);
93 #endif
94         ip6 = mtod(m, struct ip6_hdr *);
95         /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
96         IP6_EXTHDR_GET(sh, struct sctphdr *, m, off,
97             (int)(sizeof(*sh) + sizeof(*ch)));
98         if (sh == NULL) {
99                 SCTP_STAT_INCR(sctps_hdrops);
100                 return IPPROTO_DONE;
101         }
102         ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
103         iphlen = off;
104         offset = iphlen + sizeof(*sh) + sizeof(*ch);
105
106 #if defined(NFAITH) && NFAITH > 0
107
108         if (faithprefix_p != NULL && (*faithprefix_p) (&ip6->ip6_dst)) {
109                 /* XXX send icmp6 host/port unreach? */
110                 goto bad;
111         }
112 #endif                          /* NFAITH defined and > 0 */
113         SCTP_STAT_INCR(sctps_recvpackets);
114         SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
115         SCTPDBG(SCTP_DEBUG_INPUT1, "V6 input gets a packet iphlen:%d pktlen:%d\n",
116             iphlen, pkt_len);
117         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
118                 /* No multi-cast support in SCTP */
119                 goto bad;
120         }
121         /* destination port of 0 is illegal, based on RFC2960. */
122         if (sh->dest_port == 0)
123                 goto bad;
124         if ((sctp_no_csum_on_loopback == 0) ||
125             (!SCTP_IS_IT_LOOPBACK(m))) {
126                 /*
127                  * we do NOT validate things from the loopback if the sysctl
128                  * is set to 1.
129                  */
130                 check = sh->checksum;   /* save incoming checksum */
131                 if ((check == 0) && (sctp_no_csum_on_loopback)) {
132                         /*
133                          * special hook for where we got a local address
134                          * somehow routed across a non IFT_LOOP type
135                          * interface
136                          */
137                         if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
138                                 goto sctp_skip_csum;
139                 }
140                 sh->checksum = 0;       /* prepare for calc */
141                 calc_check = sctp_calculate_sum(m, &mlen, iphlen);
142                 if (calc_check != check) {
143                         SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
144                             calc_check, check, m, mlen, iphlen);
145                         stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
146                             sh, ch, &in6p, &net, vrf_id);
147                         /* in6p's ref-count increased && stcb locked */
148                         if ((in6p) && (stcb)) {
149                                 sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
150                                 sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
151                         } else if ((in6p != NULL) && (stcb == NULL)) {
152                                 refcount_up = 1;
153                         }
154                         SCTP_STAT_INCR(sctps_badsum);
155                         SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
156                         goto bad;
157                 }
158                 sh->checksum = calc_check;
159         }
160 sctp_skip_csum:
161         net = NULL;
162         /*
163          * Locate pcb and tcb for datagram sctp_findassociation_addr() wants
164          * IP/SCTP/first chunk header...
165          */
166         stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
167             sh, ch, &in6p, &net, vrf_id);
168         /* in6p's ref-count increased */
169         if (in6p == NULL) {
170                 struct sctp_init_chunk *init_chk, chunk_buf;
171
172                 SCTP_STAT_INCR(sctps_noport);
173                 if (ch->chunk_type == SCTP_INITIATION) {
174                         /*
175                          * we do a trick here to get the INIT tag, dig in
176                          * and get the tag from the INIT and put it in the
177                          * common header.
178                          */
179                         init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
180                             iphlen + sizeof(*sh), sizeof(*init_chk),
181                             (uint8_t *) & chunk_buf);
182                         if (init_chk)
183                                 sh->v_tag = init_chk->init.initiate_tag;
184                         else
185                                 sh->v_tag = 0;
186                 }
187                 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
188                         sctp_send_shutdown_complete2(m, iphlen, sh, vrf_id);
189                         goto bad;
190                 }
191                 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
192                         goto bad;
193                 }
194                 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION)
195                         sctp_send_abort(m, iphlen, sh, 0, NULL, vrf_id);
196                 goto bad;
197         } else if (stcb == NULL) {
198                 refcount_up = 1;
199         }
200         in6p_ip = (struct inpcb *)in6p;
201 #ifdef IPSEC
202         /*
203          * Check AH/ESP integrity.
204          */
205         if (in6p_ip && (ipsec6_in_reject(m, in6p_ip))) {
206 /* XXX */
207                 ipsec6stat.in_polvio++;
208                 goto bad;
209         }
210 #endif                          /* IPSEC */
211
212         /*
213          * CONTROL chunk processing
214          */
215         offset -= sizeof(*ch);
216         ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
217
218         /* Length now holds the total packet length payload + iphlen */
219         length = ntohs(ip6->ip6_plen) + iphlen;
220
221         /* sa_ignore NO_NULL_CHK */
222         sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
223             in6p, stcb, net, ecn_bits, vrf_id);
224         /* inp's ref-count reduced && stcb unlocked */
225         /* XXX this stuff below gets moved to appropriate parts later... */
226         if (m)
227                 sctp_m_freem(m);
228         if ((in6p) && refcount_up) {
229                 /* reduce ref-count */
230                 SCTP_INP_WLOCK(in6p);
231                 SCTP_INP_DECR_REF(in6p);
232                 SCTP_INP_WUNLOCK(in6p);
233         }
234         return IPPROTO_DONE;
235
236 bad:
237         if (stcb) {
238                 SCTP_TCB_UNLOCK(stcb);
239         }
240         if ((in6p) && refcount_up) {
241                 /* reduce ref-count */
242                 SCTP_INP_WLOCK(in6p);
243                 SCTP_INP_DECR_REF(in6p);
244                 SCTP_INP_WUNLOCK(in6p);
245         }
246         if (m)
247                 sctp_m_freem(m);
248         return IPPROTO_DONE;
249 }
250
251
252 static void
253 sctp6_notify_mbuf(struct sctp_inpcb *inp,
254     struct icmp6_hdr *icmp6,
255     struct sctphdr *sh,
256     struct sctp_tcb *stcb,
257     struct sctp_nets *net)
258 {
259         uint32_t nxtsz;
260
261         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
262             (icmp6 == NULL) || (sh == NULL)) {
263                 goto out;
264         }
265         /* First do we even look at it? */
266         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
267                 goto out;
268
269         if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
270                 /* not PACKET TO BIG */
271                 goto out;
272         }
273         /*
274          * ok we need to look closely. We could even get smarter and look at
275          * anyone that we sent to in case we get a different ICMP that tells
276          * us there is no way to reach a host, but for this impl, all we
277          * care about is MTU discovery.
278          */
279         nxtsz = ntohl(icmp6->icmp6_mtu);
280         /* Stop any PMTU timer */
281         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL, SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_1);
282
283         /* Adjust destination size limit */
284         if (net->mtu > nxtsz) {
285                 net->mtu = nxtsz;
286         }
287         /* now what about the ep? */
288         if (stcb->asoc.smallest_mtu > nxtsz) {
289                 struct sctp_tmit_chunk *chk;
290
291                 /* Adjust that too */
292                 stcb->asoc.smallest_mtu = nxtsz;
293                 /* now off to subtract IP_DF flag if needed */
294
295                 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
296                         if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
297                                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
298                         }
299                 }
300                 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
301                         if ((uint32_t) (chk->send_size + IP_HDR_SIZE) > nxtsz) {
302                                 /*
303                                  * For this guy we also mark for immediate
304                                  * resend since we sent to big of chunk
305                                  */
306                                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
307                                 if (chk->sent != SCTP_DATAGRAM_RESEND)
308                                         stcb->asoc.sent_queue_retran_cnt++;
309                                 chk->sent = SCTP_DATAGRAM_RESEND;
310                                 chk->rec.data.doing_fast_retransmit = 0;
311
312                                 chk->sent = SCTP_DATAGRAM_RESEND;
313                                 /* Clear any time so NO RTT is being done */
314                                 chk->sent_rcv_time.tv_sec = 0;
315                                 chk->sent_rcv_time.tv_usec = 0;
316                                 stcb->asoc.total_flight -= chk->send_size;
317                                 net->flight_size -= chk->send_size;
318                         }
319                 }
320         }
321         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
322 out:
323         if (stcb) {
324                 SCTP_TCB_UNLOCK(stcb);
325         }
326 }
327
328
329 void
330 sctp6_ctlinput(cmd, pktdst, d)
331         int cmd;
332         struct sockaddr *pktdst;
333         void *d;
334 {
335         struct sctphdr sh;
336         struct ip6ctlparam *ip6cp = NULL;
337         uint32_t vrf_id;
338         int cm;
339
340         vrf_id = SCTP_DEFAULT_VRFID;
341
342         if (pktdst->sa_family != AF_INET6 ||
343             pktdst->sa_len != sizeof(struct sockaddr_in6))
344                 return;
345
346         if ((unsigned)cmd >= PRC_NCMDS)
347                 return;
348         if (PRC_IS_REDIRECT(cmd)) {
349                 d = NULL;
350         } else if (inet6ctlerrmap[cmd] == 0) {
351                 return;
352         }
353         /* if the parameter is from icmp6, decode it. */
354         if (d != NULL) {
355                 ip6cp = (struct ip6ctlparam *)d;
356         } else {
357                 ip6cp = (struct ip6ctlparam *)NULL;
358         }
359
360         if (ip6cp) {
361                 /*
362                  * XXX: We assume that when IPV6 is non NULL, M and OFF are
363                  * valid.
364                  */
365                 /* check if we can safely examine src and dst ports */
366                 struct sctp_inpcb *inp = NULL;
367                 struct sctp_tcb *stcb = NULL;
368                 struct sctp_nets *net = NULL;
369                 struct sockaddr_in6 final;
370
371                 if (ip6cp->ip6c_m == NULL)
372                         return;
373
374                 bzero(&sh, sizeof(sh));
375                 bzero(&final, sizeof(final));
376                 inp = NULL;
377                 net = NULL;
378                 m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
379                     (caddr_t)&sh);
380                 ip6cp->ip6c_src->sin6_port = sh.src_port;
381                 final.sin6_len = sizeof(final);
382                 final.sin6_family = AF_INET6;
383                 final.sin6_addr = ((struct sockaddr_in6 *)pktdst)->sin6_addr;
384                 final.sin6_port = sh.dest_port;
385                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)ip6cp->ip6c_src,
386                     (struct sockaddr *)&final,
387                     &inp, &net, 1, vrf_id);
388                 /* inp's ref-count increased && stcb locked */
389                 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
390                         if (cmd == PRC_MSGSIZE) {
391                                 sctp6_notify_mbuf(inp,
392                                     ip6cp->ip6c_icmp6,
393                                     &sh,
394                                     stcb,
395                                     net);
396                                 /* inp's ref-count reduced && stcb unlocked */
397                         } else {
398                                 if (cmd == PRC_HOSTDEAD) {
399                                         cm = EHOSTUNREACH;
400                                 } else {
401                                         cm = inet6ctlerrmap[cmd];
402                                 }
403                                 sctp_notify(inp, cm, &sh,
404                                     (struct sockaddr *)&final,
405                                     stcb, net);
406                                 /* inp's ref-count reduced && stcb unlocked */
407                         }
408                 } else {
409                         if (PRC_IS_REDIRECT(cmd) && inp) {
410                                 in6_rtchange((struct in6pcb *)inp,
411                                     inet6ctlerrmap[cmd]);
412                         }
413                         if (inp) {
414                                 /* reduce inp's ref-count */
415                                 SCTP_INP_WLOCK(inp);
416                                 SCTP_INP_DECR_REF(inp);
417                                 SCTP_INP_WUNLOCK(inp);
418                         }
419                         if (stcb)
420                                 SCTP_TCB_UNLOCK(stcb);
421                 }
422         }
423 }
424
425 /*
426  * this routine can probably be collasped into the one in sctp_userreq.c
427  * since they do the same thing and now we lookup with a sockaddr
428  */
429 static int
430 sctp6_getcred(SYSCTL_HANDLER_ARGS)
431 {
432         struct xucred xuc;
433         struct sockaddr_in6 addrs[2];
434         struct sctp_inpcb *inp;
435         struct sctp_nets *net;
436         struct sctp_tcb *stcb;
437         int error;
438         uint32_t vrf_id;
439
440         vrf_id = SCTP_DEFAULT_VRFID;
441
442         /*
443          * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
444          * visibility is scoped using cr_canseesocket(), which it is not
445          * here.
446          */
447         error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_RESERVEDPORT,
448             0);
449         if (error)
450                 return (error);
451
452         if (req->newlen != sizeof(addrs))
453                 return (EINVAL);
454         if (req->oldlen != sizeof(struct ucred))
455                 return (EINVAL);
456         error = SYSCTL_IN(req, addrs, sizeof(addrs));
457         if (error)
458                 return (error);
459
460         stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
461             sin6tosa(&addrs[1]),
462             &inp, &net, 1, vrf_id);
463         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
464                 if ((inp != NULL) && (stcb == NULL)) {
465                         /* reduce ref-count */
466                         SCTP_INP_WLOCK(inp);
467                         SCTP_INP_DECR_REF(inp);
468                         goto cred_can_cont;
469                 }
470                 error = ENOENT;
471                 goto out;
472         }
473         SCTP_TCB_UNLOCK(stcb);
474         /*
475          * We use the write lock here, only since in the error leg we need
476          * it. If we used RLOCK, then we would have to
477          * wlock/decr/unlock/rlock. Which in theory could create a hole.
478          * Better to use higher wlock.
479          */
480         SCTP_INP_WLOCK(inp);
481 cred_can_cont:
482         error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
483         if (error) {
484                 SCTP_INP_WUNLOCK(inp);
485                 goto out;
486         }
487         cru2x(inp->sctp_socket->so_cred, &xuc);
488         SCTP_INP_WUNLOCK(inp);
489         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
490 out:
491         return (error);
492 }
493
494 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
495     0, 0,
496     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
497
498
499 /* This is the same as the sctp_abort() could be made common */
500 static void
501 sctp6_abort(struct socket *so)
502 {
503         struct sctp_inpcb *inp;
504         uint32_t flags;
505
506         inp = (struct sctp_inpcb *)so->so_pcb;
507         if (inp == 0)
508                 return;
509 sctp_must_try_again:
510         flags = inp->sctp_flags;
511 #ifdef SCTP_LOG_CLOSING
512         sctp_log_closing(inp, NULL, 17);
513 #endif
514         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
515             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
516 #ifdef SCTP_LOG_CLOSING
517                 sctp_log_closing(inp, NULL, 16);
518 #endif
519                 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
520                     SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
521                 SOCK_LOCK(so);
522                 SCTP_SB_CLEAR(so->so_snd);
523                 /*
524                  * same for the rcv ones, they are only here for the
525                  * accounting/select.
526                  */
527                 SCTP_SB_CLEAR(so->so_rcv);
528                 /* Now null out the reference, we are completely detached. */
529                 so->so_pcb = NULL;
530                 SOCK_UNLOCK(so);
531         } else {
532                 flags = inp->sctp_flags;
533                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
534                         goto sctp_must_try_again;
535                 }
536         }
537         return;
538 }
539
540 static int
541 sctp6_attach(struct socket *so, int proto, struct thread *p)
542 {
543         struct in6pcb *inp6;
544         int error;
545         struct sctp_inpcb *inp;
546         uint32_t vrf_id = SCTP_DEFAULT_VRFID;
547
548         inp = (struct sctp_inpcb *)so->so_pcb;
549         if (inp != NULL)
550                 return EINVAL;
551
552         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
553                 error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
554                 if (error)
555                         return error;
556         }
557         error = sctp_inpcb_alloc(so, vrf_id);
558         if (error)
559                 return error;
560         inp = (struct sctp_inpcb *)so->so_pcb;
561         SCTP_INP_WLOCK(inp);
562         inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;     /* I'm v6! */
563         inp6 = (struct in6pcb *)inp;
564
565         inp6->inp_vflag |= INP_IPV6;
566         inp6->in6p_hops = -1;   /* use kernel default */
567         inp6->in6p_cksum = -1;  /* just to be sure */
568 #ifdef INET
569         /*
570          * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
571          * socket as well, because the socket may be bound to an IPv6
572          * wildcard address, which may match an IPv4-mapped IPv6 address.
573          */
574         inp6->inp_ip_ttl = ip_defttl;
575 #endif
576         /*
577          * Hmm what about the IPSEC stuff that is missing here but in
578          * sctp_attach()?
579          */
580         SCTP_INP_WUNLOCK(inp);
581         return 0;
582 }
583
584 static int
585 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
586 {
587         struct sctp_inpcb *inp;
588         struct in6pcb *inp6;
589         int error;
590
591         inp = (struct sctp_inpcb *)so->so_pcb;
592         if (inp == 0)
593                 return EINVAL;
594
595         if (addr) {
596                 if ((addr->sa_family == AF_INET6) &&
597                     (addr->sa_len != sizeof(struct sockaddr_in6))) {
598                         return EINVAL;
599                 }
600                 if ((addr->sa_family == AF_INET) &&
601                     (addr->sa_len != sizeof(struct sockaddr_in))) {
602                         return EINVAL;
603                 }
604         }
605         inp6 = (struct in6pcb *)inp;
606         inp6->inp_vflag &= ~INP_IPV4;
607         inp6->inp_vflag |= INP_IPV6;
608         if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
609                 if (addr->sa_family == AF_INET) {
610                         /* binding v4 addr to v6 socket, so reset flags */
611                         inp6->inp_vflag |= INP_IPV4;
612                         inp6->inp_vflag &= ~INP_IPV6;
613                 } else {
614                         struct sockaddr_in6 *sin6_p;
615
616                         sin6_p = (struct sockaddr_in6 *)addr;
617
618                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
619                                 inp6->inp_vflag |= INP_IPV4;
620                         } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
621                                 struct sockaddr_in sin;
622
623                                 in6_sin6_2_sin(&sin, sin6_p);
624                                 inp6->inp_vflag |= INP_IPV4;
625                                 inp6->inp_vflag &= ~INP_IPV6;
626                                 error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, p);
627                                 return error;
628                         }
629                 }
630         } else if (addr != NULL) {
631                 /* IPV6_V6ONLY socket */
632                 if (addr->sa_family == AF_INET) {
633                         /* can't bind v4 addr to v6 only socket! */
634                         return EINVAL;
635                 } else {
636                         struct sockaddr_in6 *sin6_p;
637
638                         sin6_p = (struct sockaddr_in6 *)addr;
639
640                         if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
641                                 /* can't bind v4-mapped addrs either! */
642                                 /* NOTE: we don't support SIIT */
643                                 return EINVAL;
644                 }
645         }
646         error = sctp_inpcb_bind(so, addr, p);
647         return error;
648 }
649
650
651 static void
652 sctp6_close(struct socket *so)
653 {
654         struct sctp_inpcb *inp;
655         uint32_t flags;
656
657         inp = (struct sctp_inpcb *)so->so_pcb;
658         if (inp == 0)
659                 return;
660
661         /*
662          * Inform all the lower layer assoc that we are done.
663          */
664 sctp_must_try_again:
665         flags = inp->sctp_flags;
666 #ifdef SCTP_LOG_CLOSING
667         sctp_log_closing(inp, NULL, 17);
668 #endif
669         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
670             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
671                 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
672                     (so->so_rcv.sb_cc > 0)) {
673 #ifdef SCTP_LOG_CLOSING
674                         sctp_log_closing(inp, NULL, 13);
675 #endif
676                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT
677                             ,SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
678                 } else {
679 #ifdef SCTP_LOG_CLOSING
680                         sctp_log_closing(inp, NULL, 14);
681 #endif
682                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
683                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
684                 }
685                 /*
686                  * The socket is now detached, no matter what the state of
687                  * the SCTP association.
688                  */
689                 SOCK_LOCK(so);
690                 SCTP_SB_CLEAR(so->so_snd);
691                 /*
692                  * same for the rcv ones, they are only here for the
693                  * accounting/select.
694                  */
695                 SCTP_SB_CLEAR(so->so_rcv);
696                 /* Now null out the reference, we are completely detached. */
697                 so->so_pcb = NULL;
698                 SOCK_UNLOCK(so);
699         } else {
700                 flags = inp->sctp_flags;
701                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
702                         goto sctp_must_try_again;
703                 }
704         }
705         return;
706
707 }
708
709 /* This could be made common with sctp_detach() since they are identical */
710
711 static
712 int
713 sctp6_disconnect(struct socket *so)
714 {
715         struct sctp_inpcb *inp;
716
717         inp = (struct sctp_inpcb *)so->so_pcb;
718         if (inp == NULL) {
719                 return (ENOTCONN);
720         }
721         SCTP_INP_RLOCK(inp);
722         if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
723                 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
724                         /* No connection */
725                         SCTP_INP_RUNLOCK(inp);
726                         return (ENOTCONN);
727                 } else {
728                         int some_on_streamwheel = 0;
729                         struct sctp_association *asoc;
730                         struct sctp_tcb *stcb;
731
732                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
733                         if (stcb == NULL) {
734                                 SCTP_INP_RUNLOCK(inp);
735                                 return (EINVAL);
736                         }
737                         SCTP_TCB_LOCK(stcb);
738                         asoc = &stcb->asoc;
739                         if (((so->so_options & SO_LINGER) &&
740                             (so->so_linger == 0)) ||
741                             (so->so_rcv.sb_cc > 0)) {
742                                 if (SCTP_GET_STATE(asoc) !=
743                                     SCTP_STATE_COOKIE_WAIT) {
744                                         /* Left with Data unread */
745                                         struct mbuf *op_err;
746
747                                         op_err = sctp_generate_invmanparam(SCTP_CAUSE_USER_INITIATED_ABT);
748                                         sctp_send_abort_tcb(stcb, op_err);
749                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
750                                 }
751                                 SCTP_INP_RUNLOCK(inp);
752                                 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
753                                     (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
754                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
755                                 }
756                                 sctp_free_assoc(inp, stcb, SCTP_DONOT_SETSCOPE,
757                                     SCTP_FROM_SCTP6_USRREQ + SCTP_LOC_2);
758                                 /* No unlock tcb assoc is gone */
759                                 return (0);
760                         }
761                         if (!TAILQ_EMPTY(&asoc->out_wheel)) {
762                                 /* Check to see if some data queued */
763                                 struct sctp_stream_out *outs;
764
765                                 TAILQ_FOREACH(outs, &asoc->out_wheel,
766                                     next_spoke) {
767                                         if (!TAILQ_EMPTY(&outs->outqueue)) {
768                                                 some_on_streamwheel = 1;
769                                                 break;
770                                         }
771                                 }
772                         }
773                         if (TAILQ_EMPTY(&asoc->send_queue) &&
774                             TAILQ_EMPTY(&asoc->sent_queue) &&
775                             (some_on_streamwheel == 0)) {
776                                 /* nothing queued to send, so I'm done... */
777                                 if ((SCTP_GET_STATE(asoc) !=
778                                     SCTP_STATE_SHUTDOWN_SENT) &&
779                                     (SCTP_GET_STATE(asoc) !=
780                                     SCTP_STATE_SHUTDOWN_ACK_SENT)) {
781                                         /* only send SHUTDOWN the first time */
782                                         sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
783                                         sctp_chunk_output(stcb->sctp_ep, stcb, 1);
784                                         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
785                                             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
786                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
787                                         }
788                                         asoc->state = SCTP_STATE_SHUTDOWN_SENT;
789                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
790                                             stcb->sctp_ep, stcb,
791                                             asoc->primary_destination);
792                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
793                                             stcb->sctp_ep, stcb,
794                                             asoc->primary_destination);
795                                 }
796                         } else {
797                                 /*
798                                  * we still got (or just got) data to send,
799                                  * so set SHUTDOWN_PENDING
800                                  */
801                                 /*
802                                  * XXX sockets draft says that MSG_EOF
803                                  * should be sent with no data.  currently,
804                                  * we will allow user data to be sent first
805                                  * and move to SHUTDOWN-PENDING
806                                  */
807                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
808                         }
809                         SCTP_TCB_UNLOCK(stcb);
810                         SCTP_INP_RUNLOCK(inp);
811                         return (0);
812                 }
813         } else {
814                 /* UDP model does not support this */
815                 SCTP_INP_RUNLOCK(inp);
816                 return EOPNOTSUPP;
817         }
818 }
819
820
821 int
822 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
823     struct mbuf *control, struct thread *p);
824
825
826 static int
827 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
828     struct mbuf *control, struct thread *p)
829 {
830         struct sctp_inpcb *inp;
831         struct inpcb *in_inp;
832         struct in6pcb *inp6;
833
834 #ifdef INET
835         struct sockaddr_in6 *sin6;
836
837 #endif                          /* INET */
838         /* No SPL needed since sctp_output does this */
839
840         inp = (struct sctp_inpcb *)so->so_pcb;
841         if (inp == NULL) {
842                 if (control) {
843                         SCTP_RELEASE_PKT(control);
844                         control = NULL;
845                 }
846                 SCTP_RELEASE_PKT(m);
847                 return EINVAL;
848         }
849         in_inp = (struct inpcb *)inp;
850         inp6 = (struct in6pcb *)inp;
851         /*
852          * For the TCP model we may get a NULL addr, if we are a connected
853          * socket thats ok.
854          */
855         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
856             (addr == NULL)) {
857                 goto connected_type;
858         }
859         if (addr == NULL) {
860                 SCTP_RELEASE_PKT(m);
861                 if (control) {
862                         SCTP_RELEASE_PKT(control);
863                         control = NULL;
864                 }
865                 return (EDESTADDRREQ);
866         }
867 #ifdef INET
868         sin6 = (struct sockaddr_in6 *)addr;
869         if (SCTP_IPV6_V6ONLY(inp6)) {
870                 /*
871                  * if IPV6_V6ONLY flag, we discard datagrams destined to a
872                  * v4 addr or v4-mapped addr
873                  */
874                 if (addr->sa_family == AF_INET) {
875                         return EINVAL;
876                 }
877                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
878                         return EINVAL;
879                 }
880         }
881         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
882                 if (!ip6_v6only) {
883                         struct sockaddr_in sin;
884
885                         /* convert v4-mapped into v4 addr and send */
886                         in6_sin6_2_sin(&sin, sin6);
887                         return sctp_sendm(so, flags, m, (struct sockaddr *)&sin,
888                             control, p);
889                 } else {
890                         /* mapped addresses aren't enabled */
891                         return EINVAL;
892                 }
893         }
894 #endif                          /* INET */
895 connected_type:
896         /* now what about control */
897         if (control) {
898                 if (inp->control) {
899                         SCTP_PRINTF("huh? control set?\n");
900                         SCTP_RELEASE_PKT(inp->control);
901                         inp->control = NULL;
902                 }
903                 inp->control = control;
904         }
905         /* Place the data */
906         if (inp->pkt) {
907                 SCTP_BUF_NEXT(inp->pkt_last) = m;
908                 inp->pkt_last = m;
909         } else {
910                 inp->pkt_last = inp->pkt = m;
911         }
912         if (
913         /* FreeBSD and MacOSX uses a flag passed */
914             ((flags & PRUS_MORETOCOME) == 0)
915             ) {
916                 /*
917                  * note with the current version this code will only be used
918                  * by OpenBSD, NetBSD and FreeBSD have methods for
919                  * re-defining sosend() to use sctp_sosend().  One can
920                  * optionaly switch back to this code (by changing back the
921                  * defininitions but this is not advisable.
922                  */
923                 int ret;
924
925                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
926                 inp->pkt = NULL;
927                 inp->control = NULL;
928                 return (ret);
929         } else {
930                 return (0);
931         }
932 }
933
934 static int
935 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
936 {
937         uint32_t vrf_id;
938         int error = 0;
939         struct sctp_inpcb *inp;
940         struct in6pcb *inp6;
941         struct sctp_tcb *stcb;
942
943 #ifdef INET
944         struct sockaddr_in6 *sin6;
945         struct sockaddr_storage ss;
946
947 #endif                          /* INET */
948
949         inp6 = (struct in6pcb *)so->so_pcb;
950         inp = (struct sctp_inpcb *)so->so_pcb;
951         if (inp == 0) {
952                 return (ECONNRESET);    /* I made the same as TCP since we are
953                                          * not setup? */
954         }
955         if (addr == NULL) {
956                 return (EINVAL);
957         }
958         if ((addr->sa_family == AF_INET6) && (addr->sa_len != sizeof(struct sockaddr_in6))) {
959                 return (EINVAL);
960         }
961         if ((addr->sa_family == AF_INET) && (addr->sa_len != sizeof(struct sockaddr_in))) {
962                 return (EINVAL);
963         }
964         vrf_id = inp->def_vrf_id;
965         SCTP_ASOC_CREATE_LOCK(inp);
966         SCTP_INP_RLOCK(inp);
967         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
968             SCTP_PCB_FLAGS_UNBOUND) {
969                 /* Bind a ephemeral port */
970                 SCTP_INP_RUNLOCK(inp);
971                 error = sctp6_bind(so, NULL, p);
972                 if (error) {
973                         SCTP_ASOC_CREATE_UNLOCK(inp);
974
975                         return (error);
976                 }
977                 SCTP_INP_RLOCK(inp);
978         }
979         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
980             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
981                 /* We are already connected AND the TCP model */
982                 SCTP_INP_RUNLOCK(inp);
983                 SCTP_ASOC_CREATE_UNLOCK(inp);
984                 return (EADDRINUSE);
985         }
986 #ifdef INET
987         sin6 = (struct sockaddr_in6 *)addr;
988         if (SCTP_IPV6_V6ONLY(inp6)) {
989                 /*
990                  * if IPV6_V6ONLY flag, ignore connections destined to a v4
991                  * addr or v4-mapped addr
992                  */
993                 if (addr->sa_family == AF_INET) {
994                         SCTP_INP_RUNLOCK(inp);
995                         SCTP_ASOC_CREATE_UNLOCK(inp);
996                         return EINVAL;
997                 }
998                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
999                         SCTP_INP_RUNLOCK(inp);
1000                         SCTP_ASOC_CREATE_UNLOCK(inp);
1001                         return EINVAL;
1002                 }
1003         }
1004         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1005                 if (!ip6_v6only) {
1006                         /* convert v4-mapped into v4 addr */
1007                         in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
1008                         addr = (struct sockaddr *)&ss;
1009                 } else {
1010                         /* mapped addresses aren't enabled */
1011                         SCTP_INP_RUNLOCK(inp);
1012                         SCTP_ASOC_CREATE_UNLOCK(inp);
1013                         return EINVAL;
1014                 }
1015         } else
1016 #endif                          /* INET */
1017                 addr = addr;    /* for true v6 address case */
1018
1019         /* Now do we connect? */
1020         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1021                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1022                 if (stcb) {
1023                         SCTP_TCB_UNLOCK(stcb);
1024                 }
1025                 SCTP_INP_RUNLOCK(inp);
1026         } else {
1027                 SCTP_INP_RUNLOCK(inp);
1028                 SCTP_INP_WLOCK(inp);
1029                 SCTP_INP_INCR_REF(inp);
1030                 SCTP_INP_WUNLOCK(inp);
1031                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1032                 if (stcb == NULL) {
1033                         SCTP_INP_WLOCK(inp);
1034                         SCTP_INP_DECR_REF(inp);
1035                         SCTP_INP_WUNLOCK(inp);
1036                 }
1037         }
1038
1039         if (stcb != NULL) {
1040                 /* Already have or am bring up an association */
1041                 SCTP_ASOC_CREATE_UNLOCK(inp);
1042                 SCTP_TCB_UNLOCK(stcb);
1043                 return (EALREADY);
1044         }
1045         /* We are GOOD to go */
1046         stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
1047         SCTP_ASOC_CREATE_UNLOCK(inp);
1048         if (stcb == NULL) {
1049                 /* Gak! no memory */
1050                 return (error);
1051         }
1052         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1053                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1054                 /* Set the connected flag so we can queue data */
1055                 soisconnecting(so);
1056         }
1057         stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1058         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1059
1060         /* initialize authentication parameters for the assoc */
1061         sctp_initialize_auth_params(inp, stcb);
1062
1063         sctp_send_initiate(inp, stcb);
1064         SCTP_TCB_UNLOCK(stcb);
1065         return error;
1066 }
1067
1068 static int
1069 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1070 {
1071         struct sockaddr_in6 *sin6;
1072         struct sctp_inpcb *inp;
1073         uint32_t vrf_id;
1074         struct sctp_ifa *sctp_ifa;
1075
1076         int error;
1077
1078         /*
1079          * Do the malloc first in case it blocks.
1080          */
1081         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1082         sin6->sin6_family = AF_INET6;
1083         sin6->sin6_len = sizeof(*sin6);
1084
1085         inp = (struct sctp_inpcb *)so->so_pcb;
1086         if (inp == NULL) {
1087                 SCTP_FREE_SONAME(sin6);
1088                 return ECONNRESET;
1089         }
1090         SCTP_INP_RLOCK(inp);
1091         sin6->sin6_port = inp->sctp_lport;
1092         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1093                 /* For the bound all case you get back 0 */
1094                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1095                         struct sctp_tcb *stcb;
1096                         struct sockaddr_in6 *sin_a6;
1097                         struct sctp_nets *net;
1098                         int fnd;
1099
1100                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1101                         if (stcb == NULL) {
1102                                 goto notConn6;
1103                         }
1104                         fnd = 0;
1105                         sin_a6 = NULL;
1106                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1107                                 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1108                                 if (sin_a6 == NULL)
1109                                         /* this will make coverity happy */
1110                                         continue;
1111
1112                                 if (sin_a6->sin6_family == AF_INET6) {
1113                                         fnd = 1;
1114                                         break;
1115                                 }
1116                         }
1117                         if ((!fnd) || (sin_a6 == NULL)) {
1118                                 /* punt */
1119                                 goto notConn6;
1120                         }
1121                         vrf_id = inp->def_vrf_id;
1122                         sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *) & net->ro, net, 0, vrf_id);
1123                         if (sctp_ifa) {
1124                                 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1125                         }
1126                 } else {
1127                         /* For the bound all case you get back 0 */
1128         notConn6:
1129                         memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1130                 }
1131         } else {
1132                 /* Take the first IPv6 address in the list */
1133                 struct sctp_laddr *laddr;
1134                 int fnd = 0;
1135
1136                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1137                         if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1138                                 struct sockaddr_in6 *sin_a;
1139
1140                                 sin_a = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1141                                 sin6->sin6_addr = sin_a->sin6_addr;
1142                                 fnd = 1;
1143                                 break;
1144                         }
1145                 }
1146                 if (!fnd) {
1147                         SCTP_FREE_SONAME(sin6);
1148                         SCTP_INP_RUNLOCK(inp);
1149                         return ENOENT;
1150                 }
1151         }
1152         SCTP_INP_RUNLOCK(inp);
1153         /* Scoping things for v6 */
1154         if ((error = sa6_recoverscope(sin6)) != 0) {
1155                 SCTP_FREE_SONAME(sin6);
1156                 return (error);
1157         }
1158         (*addr) = (struct sockaddr *)sin6;
1159         return (0);
1160 }
1161
1162 static int
1163 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1164 {
1165         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)*addr;
1166         int fnd;
1167         struct sockaddr_in6 *sin_a6;
1168         struct sctp_inpcb *inp;
1169         struct sctp_tcb *stcb;
1170         struct sctp_nets *net;
1171
1172         int error;
1173
1174         /*
1175          * Do the malloc first in case it blocks.
1176          */
1177         inp = (struct sctp_inpcb *)so->so_pcb;
1178         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1179                 /* UDP type and listeners will drop out here */
1180                 return (ENOTCONN);
1181         }
1182         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1183         sin6->sin6_family = AF_INET6;
1184         sin6->sin6_len = sizeof(*sin6);
1185
1186         /* We must recapture incase we blocked */
1187         inp = (struct sctp_inpcb *)so->so_pcb;
1188         if (inp == NULL) {
1189                 SCTP_FREE_SONAME(sin6);
1190                 return ECONNRESET;
1191         }
1192         SCTP_INP_RLOCK(inp);
1193         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1194         if (stcb) {
1195                 SCTP_TCB_LOCK(stcb);
1196         }
1197         SCTP_INP_RUNLOCK(inp);
1198         if (stcb == NULL) {
1199                 SCTP_FREE_SONAME(sin6);
1200                 return ECONNRESET;
1201         }
1202         fnd = 0;
1203         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1204                 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1205                 if (sin_a6->sin6_family == AF_INET6) {
1206                         fnd = 1;
1207                         sin6->sin6_port = stcb->rport;
1208                         sin6->sin6_addr = sin_a6->sin6_addr;
1209                         break;
1210                 }
1211         }
1212         SCTP_TCB_UNLOCK(stcb);
1213         if (!fnd) {
1214                 /* No IPv4 address */
1215                 SCTP_FREE_SONAME(sin6);
1216                 return ENOENT;
1217         }
1218         if ((error = sa6_recoverscope(sin6)) != 0)
1219                 return (error);
1220         *addr = (struct sockaddr *)sin6;
1221         return (0);
1222 }
1223
1224 static int
1225 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1226 {
1227         struct sockaddr *addr;
1228         struct in6pcb *inp6 = sotoin6pcb(so);
1229         int error;
1230
1231         if (inp6 == NULL)
1232                 return EINVAL;
1233
1234         /* allow v6 addresses precedence */
1235         error = sctp6_getaddr(so, nam);
1236         if (error) {
1237                 /* try v4 next if v6 failed */
1238                 error = sctp_ingetaddr(so, nam);
1239                 if (error) {
1240                         return (error);
1241                 }
1242                 addr = *nam;
1243                 /* if I'm V6ONLY, convert it to v4-mapped */
1244                 if (SCTP_IPV6_V6ONLY(inp6)) {
1245                         struct sockaddr_in6 sin6;
1246
1247                         in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1248                         memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1249
1250                 }
1251         }
1252         return (error);
1253 }
1254
1255
1256 static int
1257 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1258 {
1259         struct sockaddr *addr = *nam;
1260         struct in6pcb *inp6 = sotoin6pcb(so);
1261         int error;
1262
1263         if (inp6 == NULL)
1264                 return EINVAL;
1265
1266         /* allow v6 addresses precedence */
1267         error = sctp6_peeraddr(so, nam);
1268         if (error) {
1269                 /* try v4 next if v6 failed */
1270                 error = sctp_peeraddr(so, nam);
1271                 if (error) {
1272                         return (error);
1273                 }
1274                 /* if I'm V6ONLY, convert it to v4-mapped */
1275                 if (SCTP_IPV6_V6ONLY(inp6)) {
1276                         struct sockaddr_in6 sin6;
1277
1278                         in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1279                         memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1280                 }
1281         }
1282         return error;
1283 }
1284
1285 struct pr_usrreqs sctp6_usrreqs = {
1286         .pru_abort = sctp6_abort,
1287         .pru_accept = sctp_accept,
1288         .pru_attach = sctp6_attach,
1289         .pru_bind = sctp6_bind,
1290         .pru_connect = sctp6_connect,
1291         .pru_control = in6_control,
1292         .pru_close = sctp6_close,
1293         .pru_detach = sctp6_close,
1294         .pru_sopoll = sopoll_generic,
1295         .pru_disconnect = sctp6_disconnect,
1296         .pru_listen = sctp_listen,
1297         .pru_peeraddr = sctp6_getpeeraddr,
1298         .pru_send = sctp6_send,
1299         .pru_shutdown = sctp_shutdown,
1300         .pru_sockaddr = sctp6_in6getaddr,
1301         .pru_sosend = sctp_sosend,
1302         .pru_soreceive = sctp_soreceive
1303 };