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