]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/sctp6_usrreq.c
Add UPDATING entries and bump version.
[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
273         if ((unsigned)cmd >= PRC_NCMDS) {
274                 return;
275         }
276         if (PRC_IS_REDIRECT(cmd)) {
277                 d = NULL;
278         } else if (inet6ctlerrmap[cmd] == 0) {
279                 return;
280         }
281         /* If the parameter is from icmp6, decode it. */
282         if (d != NULL) {
283                 ip6cp = (struct ip6ctlparam *)d;
284         } else {
285                 ip6cp = (struct ip6ctlparam *)NULL;
286         }
287
288         if (ip6cp != NULL) {
289                 /*
290                  * XXX: We assume that when IPV6 is non NULL, M and OFF are
291                  * valid.
292                  */
293                 if (ip6cp->ip6c_m == NULL) {
294                         return;
295                 }
296
297                 /*
298                  * Check if we can safely examine the ports and the
299                  * verification tag of the SCTP common header.
300                  */
301                 if (ip6cp->ip6c_m->m_pkthdr.len <
302                     (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
303                         return;
304                 }
305
306                 /* Copy out the port numbers and the verification tag. */
307                 memset(&sh, 0, sizeof(sh));
308                 m_copydata(ip6cp->ip6c_m,
309                     ip6cp->ip6c_off,
310                     sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
311                     (caddr_t)&sh);
312                 memset(&src, 0, sizeof(struct sockaddr_in6));
313                 src.sin6_family = AF_INET6;
314                 src.sin6_len = sizeof(struct sockaddr_in6);
315                 src.sin6_port = sh.src_port;
316                 src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
317                 if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
318                         return;
319                 }
320                 memset(&dst, 0, sizeof(struct sockaddr_in6));
321                 dst.sin6_family = AF_INET6;
322                 dst.sin6_len = sizeof(struct sockaddr_in6);
323                 dst.sin6_port = sh.dest_port;
324                 dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
325                 if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
326                         return;
327                 }
328                 inp = NULL;
329                 net = NULL;
330                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
331                     (struct sockaddr *)&src,
332                     &inp, &net, 1, SCTP_DEFAULT_VRFID);
333                 if ((stcb != NULL) &&
334                     (net != NULL) &&
335                     (inp != NULL)) {
336                         /* Check the verification tag */
337                         if (ntohl(sh.v_tag) != 0) {
338                                 /*
339                                  * This must be the verification tag used
340                                  * for sending out packets. We don't
341                                  * consider packets reflecting the
342                                  * verification tag.
343                                  */
344                                 if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
345                                         SCTP_TCB_UNLOCK(stcb);
346                                         return;
347                                 }
348                         } else {
349                                 if (ip6cp->ip6c_m->m_pkthdr.len >=
350                                     ip6cp->ip6c_off + sizeof(struct sctphdr) +
351                                     sizeof(struct sctp_chunkhdr) +
352                                     offsetof(struct sctp_init, a_rwnd)) {
353                                         /*
354                                          * In this case we can check if we
355                                          * got an INIT chunk and if the
356                                          * initiate tag matches.
357                                          */
358                                         uint32_t initiate_tag;
359                                         uint8_t chunk_type;
360
361                                         m_copydata(ip6cp->ip6c_m,
362                                             ip6cp->ip6c_off +
363                                             sizeof(struct sctphdr),
364                                             sizeof(uint8_t),
365                                             (caddr_t)&chunk_type);
366                                         m_copydata(ip6cp->ip6c_m,
367                                             ip6cp->ip6c_off +
368                                             sizeof(struct sctphdr) +
369                                             sizeof(struct sctp_chunkhdr),
370                                             sizeof(uint32_t),
371                                             (caddr_t)&initiate_tag);
372                                         if ((chunk_type != SCTP_INITIATION) ||
373                                             (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
374                                                 SCTP_TCB_UNLOCK(stcb);
375                                                 return;
376                                         }
377                                 } else {
378                                         SCTP_TCB_UNLOCK(stcb);
379                                         return;
380                                 }
381                         }
382                         sctp6_notify(inp, stcb, net,
383                             ip6cp->ip6c_icmp6->icmp6_type,
384                             ip6cp->ip6c_icmp6->icmp6_code,
385                             ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
386                 } else {
387                         if ((stcb == NULL) && (inp != NULL)) {
388                                 /* reduce inp's ref-count */
389                                 SCTP_INP_WLOCK(inp);
390                                 SCTP_INP_DECR_REF(inp);
391                                 SCTP_INP_WUNLOCK(inp);
392                         }
393                         if (stcb) {
394                                 SCTP_TCB_UNLOCK(stcb);
395                         }
396                 }
397         }
398 }
399
400 /*
401  * this routine can probably be collasped into the one in sctp_userreq.c
402  * since they do the same thing and now we lookup with a sockaddr
403  */
404 static int
405 sctp6_getcred(SYSCTL_HANDLER_ARGS)
406 {
407         struct xucred xuc;
408         struct sockaddr_in6 addrs[2];
409         struct sctp_inpcb *inp;
410         struct sctp_nets *net;
411         struct sctp_tcb *stcb;
412         int error;
413         uint32_t vrf_id;
414
415         vrf_id = SCTP_DEFAULT_VRFID;
416
417         error = priv_check(req->td, PRIV_NETINET_GETCRED);
418         if (error)
419                 return (error);
420
421         if (req->newlen != sizeof(addrs)) {
422                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
423                 return (EINVAL);
424         }
425         if (req->oldlen != sizeof(struct ucred)) {
426                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
427                 return (EINVAL);
428         }
429         error = SYSCTL_IN(req, addrs, sizeof(addrs));
430         if (error)
431                 return (error);
432
433         stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
434             sin6tosa(&addrs[0]),
435             &inp, &net, 1, vrf_id);
436         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
437                 if ((inp != NULL) && (stcb == NULL)) {
438                         /* reduce ref-count */
439                         SCTP_INP_WLOCK(inp);
440                         SCTP_INP_DECR_REF(inp);
441                         goto cred_can_cont;
442                 }
443                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
444                 error = ENOENT;
445                 goto out;
446         }
447         SCTP_TCB_UNLOCK(stcb);
448         /*
449          * We use the write lock here, only since in the error leg we need
450          * it. If we used RLOCK, then we would have to
451          * wlock/decr/unlock/rlock. Which in theory could create a hole.
452          * Better to use higher wlock.
453          */
454         SCTP_INP_WLOCK(inp);
455 cred_can_cont:
456         error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
457         if (error) {
458                 SCTP_INP_WUNLOCK(inp);
459                 goto out;
460         }
461         cru2x(inp->sctp_socket->so_cred, &xuc);
462         SCTP_INP_WUNLOCK(inp);
463         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
464 out:
465         return (error);
466 }
467
468 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
469     0, 0,
470     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
471
472
473 /* This is the same as the sctp_abort() could be made common */
474 static void
475 sctp6_abort(struct socket *so)
476 {
477         struct sctp_inpcb *inp;
478         uint32_t flags;
479
480         inp = (struct sctp_inpcb *)so->so_pcb;
481         if (inp == NULL) {
482                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
483                 return;
484         }
485 sctp_must_try_again:
486         flags = inp->sctp_flags;
487 #ifdef SCTP_LOG_CLOSING
488         sctp_log_closing(inp, NULL, 17);
489 #endif
490         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
491             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
492 #ifdef SCTP_LOG_CLOSING
493                 sctp_log_closing(inp, NULL, 16);
494 #endif
495                 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
496                     SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
497                 SOCK_LOCK(so);
498                 SCTP_SB_CLEAR(so->so_snd);
499                 /*
500                  * same for the rcv ones, they are only here for the
501                  * accounting/select.
502                  */
503                 SCTP_SB_CLEAR(so->so_rcv);
504                 /* Now null out the reference, we are completely detached. */
505                 so->so_pcb = NULL;
506                 SOCK_UNLOCK(so);
507         } else {
508                 flags = inp->sctp_flags;
509                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
510                         goto sctp_must_try_again;
511                 }
512         }
513         return;
514 }
515
516 static int
517 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
518 {
519         struct in6pcb *inp6;
520         int error;
521         struct sctp_inpcb *inp;
522         uint32_t vrf_id = SCTP_DEFAULT_VRFID;
523
524         inp = (struct sctp_inpcb *)so->so_pcb;
525         if (inp != NULL) {
526                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
527                 return (EINVAL);
528         }
529
530         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
531                 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
532                 if (error)
533                         return (error);
534         }
535         error = sctp_inpcb_alloc(so, vrf_id);
536         if (error)
537                 return (error);
538         inp = (struct sctp_inpcb *)so->so_pcb;
539         SCTP_INP_WLOCK(inp);
540         inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;     /* I'm v6! */
541         inp6 = (struct in6pcb *)inp;
542
543         inp6->inp_vflag |= INP_IPV6;
544         inp6->in6p_hops = -1;   /* use kernel default */
545         inp6->in6p_cksum = -1;  /* just to be sure */
546 #ifdef INET
547         /*
548          * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
549          * socket as well, because the socket may be bound to an IPv6
550          * wildcard address, which may match an IPv4-mapped IPv6 address.
551          */
552         inp6->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
553 #endif
554         SCTP_INP_WUNLOCK(inp);
555         return (0);
556 }
557
558 static int
559 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
560 {
561         struct sctp_inpcb *inp;
562         struct in6pcb *inp6;
563         int error;
564         u_char vflagsav;
565
566         inp = (struct sctp_inpcb *)so->so_pcb;
567         if (inp == NULL) {
568                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
569                 return (EINVAL);
570         }
571
572         if (addr) {
573                 switch (addr->sa_family) {
574 #ifdef INET
575                 case AF_INET:
576                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
577                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
578                                 return (EINVAL);
579                         }
580                         break;
581 #endif
582 #ifdef INET6
583                 case AF_INET6:
584                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
585                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
586                                 return (EINVAL);
587                         }
588                         break;
589 #endif
590                 default:
591                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
592                         return (EINVAL);
593                 }
594         }
595         inp6 = (struct in6pcb *)inp;
596         vflagsav = inp6->inp_vflag;
597         inp6->inp_vflag &= ~INP_IPV4;
598         inp6->inp_vflag |= INP_IPV6;
599         if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp6) == 0)) {
600                 switch (addr->sa_family) {
601 #ifdef INET
602                 case AF_INET:
603                         /* binding v4 addr to v6 socket, so reset flags */
604                         inp6->inp_vflag |= INP_IPV4;
605                         inp6->inp_vflag &= ~INP_IPV6;
606                         break;
607 #endif
608 #ifdef INET6
609                 case AF_INET6:
610                         {
611                                 struct sockaddr_in6 *sin6_p;
612
613                                 sin6_p = (struct sockaddr_in6 *)addr;
614
615                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
616                                         inp6->inp_vflag |= INP_IPV4;
617                                 }
618 #ifdef INET
619                                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
620                                         struct sockaddr_in sin;
621
622                                         in6_sin6_2_sin(&sin, sin6_p);
623                                         inp6->inp_vflag |= INP_IPV4;
624                                         inp6->inp_vflag &= ~INP_IPV6;
625                                         error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
626                                         goto out;
627                                 }
628 #endif
629                                 break;
630                         }
631 #endif
632                 default:
633                         break;
634                 }
635         } else if (addr != NULL) {
636                 struct sockaddr_in6 *sin6_p;
637
638                 /* IPV6_V6ONLY socket */
639 #ifdef INET
640                 if (addr->sa_family == AF_INET) {
641                         /* can't bind v4 addr to v6 only socket! */
642                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
643                         error = EINVAL;
644                         goto out;
645                 }
646 #endif
647                 sin6_p = (struct sockaddr_in6 *)addr;
648
649                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
650                         /* can't bind v4-mapped addrs either! */
651                         /* NOTE: we don't support SIIT */
652                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
653                         error = EINVAL;
654                         goto out;
655                 }
656         }
657         error = sctp_inpcb_bind(so, addr, NULL, p);
658 out:
659         if (error != 0)
660                 inp6->inp_vflag = vflagsav;
661         return (error);
662 }
663
664
665 static void
666 sctp6_close(struct socket *so)
667 {
668         sctp_close(so);
669 }
670
671 /* This could be made common with sctp_detach() since they are identical */
672
673 static
674 int
675 sctp6_disconnect(struct socket *so)
676 {
677         return (sctp_disconnect(so));
678 }
679
680
681 int
682 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
683     struct mbuf *control, struct thread *p);
684
685
686 static int
687 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
688     struct mbuf *control, struct thread *p)
689 {
690         struct sctp_inpcb *inp;
691         struct in6pcb *inp6;
692
693 #ifdef INET
694         struct sockaddr_in6 *sin6;
695 #endif                          /* INET */
696         /* No SPL needed since sctp_output does this */
697
698         inp = (struct sctp_inpcb *)so->so_pcb;
699         if (inp == NULL) {
700                 if (control) {
701                         SCTP_RELEASE_PKT(control);
702                         control = NULL;
703                 }
704                 SCTP_RELEASE_PKT(m);
705                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
706                 return (EINVAL);
707         }
708         inp6 = (struct in6pcb *)inp;
709         /*
710          * For the TCP model we may get a NULL addr, if we are a connected
711          * socket thats ok.
712          */
713         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
714             (addr == NULL)) {
715                 goto connected_type;
716         }
717         if (addr == NULL) {
718                 SCTP_RELEASE_PKT(m);
719                 if (control) {
720                         SCTP_RELEASE_PKT(control);
721                         control = NULL;
722                 }
723                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
724                 return (EDESTADDRREQ);
725         }
726 #ifdef INET
727         sin6 = (struct sockaddr_in6 *)addr;
728         if (SCTP_IPV6_V6ONLY(inp6)) {
729                 /*
730                  * if IPV6_V6ONLY flag, we discard datagrams destined to a
731                  * v4 addr or v4-mapped addr
732                  */
733                 if (addr->sa_family == AF_INET) {
734                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
735                         return (EINVAL);
736                 }
737                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
738                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
739                         return (EINVAL);
740                 }
741         }
742         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
743                 struct sockaddr_in sin;
744
745                 /* convert v4-mapped into v4 addr and send */
746                 in6_sin6_2_sin(&sin, sin6);
747                 return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
748         }
749 #endif                          /* INET */
750 connected_type:
751         /* now what about control */
752         if (control) {
753                 if (inp->control) {
754                         SCTP_PRINTF("huh? control set?\n");
755                         SCTP_RELEASE_PKT(inp->control);
756                         inp->control = NULL;
757                 }
758                 inp->control = control;
759         }
760         /* Place the data */
761         if (inp->pkt) {
762                 SCTP_BUF_NEXT(inp->pkt_last) = m;
763                 inp->pkt_last = m;
764         } else {
765                 inp->pkt_last = inp->pkt = m;
766         }
767         if (
768         /* FreeBSD and MacOSX uses a flag passed */
769             ((flags & PRUS_MORETOCOME) == 0)
770             ) {
771                 /*
772                  * note with the current version this code will only be used
773                  * by OpenBSD, NetBSD and FreeBSD have methods for
774                  * re-defining sosend() to use sctp_sosend().  One can
775                  * optionaly switch back to this code (by changing back the
776                  * defininitions but this is not advisable.
777                  */
778                 int ret;
779
780                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
781                 inp->pkt = NULL;
782                 inp->control = NULL;
783                 return (ret);
784         } else {
785                 return (0);
786         }
787 }
788
789 static int
790 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
791 {
792         uint32_t vrf_id;
793         int error = 0;
794         struct sctp_inpcb *inp;
795         struct sctp_tcb *stcb;
796 #ifdef INET
797         struct in6pcb *inp6;
798         struct sockaddr_in6 *sin6;
799         union sctp_sockstore store;
800 #endif
801
802 #ifdef INET
803         inp6 = (struct in6pcb *)so->so_pcb;
804 #endif
805         inp = (struct sctp_inpcb *)so->so_pcb;
806         if (inp == NULL) {
807                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
808                 return (ECONNRESET);    /* I made the same as TCP since we are
809                                          * not setup? */
810         }
811         if (addr == NULL) {
812                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
813                 return (EINVAL);
814         }
815         switch (addr->sa_family) {
816 #ifdef INET
817         case AF_INET:
818                 if (addr->sa_len != sizeof(struct sockaddr_in)) {
819                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
820                         return (EINVAL);
821                 }
822                 break;
823 #endif
824 #ifdef INET6
825         case AF_INET6:
826                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
827                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
828                         return (EINVAL);
829                 }
830                 break;
831 #endif
832         default:
833                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
834                 return (EINVAL);
835         }
836
837         vrf_id = inp->def_vrf_id;
838         SCTP_ASOC_CREATE_LOCK(inp);
839         SCTP_INP_RLOCK(inp);
840         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
841             SCTP_PCB_FLAGS_UNBOUND) {
842                 /* Bind a ephemeral port */
843                 SCTP_INP_RUNLOCK(inp);
844                 error = sctp6_bind(so, NULL, p);
845                 if (error) {
846                         SCTP_ASOC_CREATE_UNLOCK(inp);
847
848                         return (error);
849                 }
850                 SCTP_INP_RLOCK(inp);
851         }
852         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
853             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
854                 /* We are already connected AND the TCP model */
855                 SCTP_INP_RUNLOCK(inp);
856                 SCTP_ASOC_CREATE_UNLOCK(inp);
857                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
858                 return (EADDRINUSE);
859         }
860 #ifdef INET
861         sin6 = (struct sockaddr_in6 *)addr;
862         if (SCTP_IPV6_V6ONLY(inp6)) {
863                 /*
864                  * if IPV6_V6ONLY flag, ignore connections destined to a v4
865                  * addr or v4-mapped addr
866                  */
867                 if (addr->sa_family == AF_INET) {
868                         SCTP_INP_RUNLOCK(inp);
869                         SCTP_ASOC_CREATE_UNLOCK(inp);
870                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
871                         return (EINVAL);
872                 }
873                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
874                         SCTP_INP_RUNLOCK(inp);
875                         SCTP_ASOC_CREATE_UNLOCK(inp);
876                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
877                         return (EINVAL);
878                 }
879         }
880         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
881                 /* convert v4-mapped into v4 addr */
882                 in6_sin6_2_sin(&store.sin, sin6);
883                 addr = &store.sa;
884         }
885 #endif                          /* INET */
886         /* Now do we connect? */
887         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
888                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
889                 if (stcb) {
890                         SCTP_TCB_LOCK(stcb);
891                 }
892                 SCTP_INP_RUNLOCK(inp);
893         } else {
894                 SCTP_INP_RUNLOCK(inp);
895                 SCTP_INP_WLOCK(inp);
896                 SCTP_INP_INCR_REF(inp);
897                 SCTP_INP_WUNLOCK(inp);
898                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
899                 if (stcb == NULL) {
900                         SCTP_INP_WLOCK(inp);
901                         SCTP_INP_DECR_REF(inp);
902                         SCTP_INP_WUNLOCK(inp);
903                 }
904         }
905
906         if (stcb != NULL) {
907                 /* Already have or am bring up an association */
908                 SCTP_ASOC_CREATE_UNLOCK(inp);
909                 SCTP_TCB_UNLOCK(stcb);
910                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
911                 return (EALREADY);
912         }
913         /* We are GOOD to go */
914         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
915             inp->sctp_ep.pre_open_stream_count,
916             inp->sctp_ep.port, p);
917         SCTP_ASOC_CREATE_UNLOCK(inp);
918         if (stcb == NULL) {
919                 /* Gak! no memory */
920                 return (error);
921         }
922         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
923                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
924                 /* Set the connected flag so we can queue data */
925                 soisconnecting(so);
926         }
927         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
928         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
929
930         /* initialize authentication parameters for the assoc */
931         sctp_initialize_auth_params(inp, stcb);
932
933         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
934         SCTP_TCB_UNLOCK(stcb);
935         return (error);
936 }
937
938 static int
939 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
940 {
941         struct sockaddr_in6 *sin6;
942         struct sctp_inpcb *inp;
943         uint32_t vrf_id;
944         struct sctp_ifa *sctp_ifa;
945
946         int error;
947
948         /*
949          * Do the malloc first in case it blocks.
950          */
951         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
952         if (sin6 == NULL)
953                 return (ENOMEM);
954         sin6->sin6_family = AF_INET6;
955         sin6->sin6_len = sizeof(*sin6);
956
957         inp = (struct sctp_inpcb *)so->so_pcb;
958         if (inp == NULL) {
959                 SCTP_FREE_SONAME(sin6);
960                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
961                 return (ECONNRESET);
962         }
963         SCTP_INP_RLOCK(inp);
964         sin6->sin6_port = inp->sctp_lport;
965         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
966                 /* For the bound all case you get back 0 */
967                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
968                         struct sctp_tcb *stcb;
969                         struct sockaddr_in6 *sin_a6;
970                         struct sctp_nets *net;
971                         int fnd;
972
973                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
974                         if (stcb == NULL) {
975                                 SCTP_INP_RUNLOCK(inp);
976                                 SCTP_FREE_SONAME(sin6);
977                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
978                                 return (ENOENT);
979                         }
980                         fnd = 0;
981                         sin_a6 = NULL;
982                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
983                                 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
984                                 if (sin_a6 == NULL)
985                                         /* this will make coverity happy */
986                                         continue;
987
988                                 if (sin_a6->sin6_family == AF_INET6) {
989                                         fnd = 1;
990                                         break;
991                                 }
992                         }
993                         if ((!fnd) || (sin_a6 == NULL)) {
994                                 /* punt */
995                                 SCTP_INP_RUNLOCK(inp);
996                                 SCTP_FREE_SONAME(sin6);
997                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
998                                 return (ENOENT);
999                         }
1000                         vrf_id = inp->def_vrf_id;
1001                         sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1002                         if (sctp_ifa) {
1003                                 sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1004                         }
1005                 } else {
1006                         /* For the bound all case you get back 0 */
1007                         memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1008                 }
1009         } else {
1010                 /* Take the first IPv6 address in the list */
1011                 struct sctp_laddr *laddr;
1012                 int fnd = 0;
1013
1014                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1015                         if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1016                                 struct sockaddr_in6 *sin_a;
1017
1018                                 sin_a = &laddr->ifa->address.sin6;
1019                                 sin6->sin6_addr = sin_a->sin6_addr;
1020                                 fnd = 1;
1021                                 break;
1022                         }
1023                 }
1024                 if (!fnd) {
1025                         SCTP_FREE_SONAME(sin6);
1026                         SCTP_INP_RUNLOCK(inp);
1027                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1028                         return (ENOENT);
1029                 }
1030         }
1031         SCTP_INP_RUNLOCK(inp);
1032         /* Scoping things for v6 */
1033         if ((error = sa6_recoverscope(sin6)) != 0) {
1034                 SCTP_FREE_SONAME(sin6);
1035                 return (error);
1036         }
1037         (*addr) = (struct sockaddr *)sin6;
1038         return (0);
1039 }
1040
1041 static int
1042 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1043 {
1044         struct sockaddr_in6 *sin6;
1045         int fnd;
1046         struct sockaddr_in6 *sin_a6;
1047         struct sctp_inpcb *inp;
1048         struct sctp_tcb *stcb;
1049         struct sctp_nets *net;
1050         int error;
1051
1052         /* Do the malloc first in case it blocks. */
1053         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1054         if (sin6 == NULL)
1055                 return (ENOMEM);
1056         sin6->sin6_family = AF_INET6;
1057         sin6->sin6_len = sizeof(*sin6);
1058
1059         inp = (struct sctp_inpcb *)so->so_pcb;
1060         if ((inp == NULL) ||
1061             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1062                 /* UDP type and listeners will drop out here */
1063                 SCTP_FREE_SONAME(sin6);
1064                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1065                 return (ENOTCONN);
1066         }
1067         SCTP_INP_RLOCK(inp);
1068         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1069         if (stcb) {
1070                 SCTP_TCB_LOCK(stcb);
1071         }
1072         SCTP_INP_RUNLOCK(inp);
1073         if (stcb == NULL) {
1074                 SCTP_FREE_SONAME(sin6);
1075                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1076                 return (ECONNRESET);
1077         }
1078         fnd = 0;
1079         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1080                 sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1081                 if (sin_a6->sin6_family == AF_INET6) {
1082                         fnd = 1;
1083                         sin6->sin6_port = stcb->rport;
1084                         sin6->sin6_addr = sin_a6->sin6_addr;
1085                         break;
1086                 }
1087         }
1088         SCTP_TCB_UNLOCK(stcb);
1089         if (!fnd) {
1090                 /* No IPv4 address */
1091                 SCTP_FREE_SONAME(sin6);
1092                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1093                 return (ENOENT);
1094         }
1095         if ((error = sa6_recoverscope(sin6)) != 0) {
1096                 SCTP_FREE_SONAME(sin6);
1097                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1098                 return (error);
1099         }
1100         *addr = (struct sockaddr *)sin6;
1101         return (0);
1102 }
1103
1104 static int
1105 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1106 {
1107         struct in6pcb *inp6 = sotoin6pcb(so);
1108         int error;
1109
1110         if (inp6 == NULL) {
1111                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1112                 return (EINVAL);
1113         }
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
1151         /* allow v6 addresses precedence */
1152         error = sctp6_peeraddr(so, nam);
1153 #ifdef INET
1154         if (error) {
1155                 struct sockaddr_in6 *sin6;
1156
1157                 /* try v4 next if v6 failed */
1158                 error = sctp_peeraddr(so, nam);
1159                 if (error) {
1160                         return (error);
1161                 }
1162                 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1163                 if (sin6 == NULL) {
1164                         SCTP_FREE_SONAME(*nam);
1165                         return (ENOMEM);
1166                 }
1167                 in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1168                 SCTP_FREE_SONAME(*nam);
1169                 *nam = (struct sockaddr *)sin6;
1170         }
1171 #endif
1172         return (error);
1173 }
1174
1175 struct pr_usrreqs sctp6_usrreqs = {
1176         .pru_abort = sctp6_abort,
1177         .pru_accept = sctp_accept,
1178         .pru_attach = sctp6_attach,
1179         .pru_bind = sctp6_bind,
1180         .pru_connect = sctp6_connect,
1181         .pru_control = in6_control,
1182         .pru_close = sctp6_close,
1183         .pru_detach = sctp6_close,
1184         .pru_sopoll = sopoll_generic,
1185         .pru_flush = sctp_flush,
1186         .pru_disconnect = sctp6_disconnect,
1187         .pru_listen = sctp_listen,
1188         .pru_peeraddr = sctp6_getpeeraddr,
1189         .pru_send = sctp6_send,
1190         .pru_shutdown = sctp_shutdown,
1191         .pru_sockaddr = sctp6_in6getaddr,
1192         .pru_sosend = sctp_sosend,
1193         .pru_soreceive = sctp_soreceive
1194 };
1195
1196 #endif