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