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