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