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