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