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