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