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