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