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