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