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