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