1 /* $KAME: sctp_usrreq.c,v 1.50 2005/06/16 20:45:29 jinmei Exp $ */
2 /* $NetBSD: sctp_usrreq.c,v 1.27 2024/09/08 17:28:37 rillig Exp $ */
3
4 /*
5 * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Cisco Systems, Inc.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sctp_usrreq.c,v 1.27 2024/09/08 17:28:37 rillig Exp $");
37
38 #ifdef _KERNEL_OPT
39 #include "opt_inet.h"
40 #include "opt_sctp.h"
41 #endif /* _KERNEL_OPT */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/domain.h>
49 #include <sys/proc.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip6.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip_var.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/scope6_var.h>
68
69 #include <netinet/ip_icmp.h>
70 #include <netinet/icmp_var.h>
71 #include <netinet/sctp_pcb.h>
72 #include <netinet/sctp_header.h>
73 #include <netinet/sctp_var.h>
74 #include <netinet/sctp_output.h>
75 #include <netinet/sctp_uio.h>
76 #include <netinet/sctp_asconf.h>
77 #include <netinet/sctp_route.h>
78 #include <netinet/sctputil.h>
79 #include <netinet/sctp_indata.h>
80 #include <netinet/sctp_asconf.h>
81 #ifdef IPSEC
82 #include <netipsec/ipsec.h>
83 #include <netipsec/key.h>
84 #endif /* IPSEC */
85
86 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
87 #ifndef in6pcb
88 #define in6pcb inpcb
89 #endif
90 #ifndef sotoin6pcb
91 #define sotoin6pcb sotoinpcb
92 #endif
93 #endif
94
95 #ifdef SCTP_DEBUG
96 extern u_int32_t sctp_debug_on;
97 #endif /* SCTP_DEBUG */
98
99 /*
100 * sysctl tunable variables
101 */
102 int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF;
103 int sctp_max_burst_default = SCTP_DEF_MAX_BURST;
104 int sctp_peer_chunk_oh = sizeof(struct mbuf);
105 int sctp_strict_init = 1;
106 int sctp_no_csum_on_loopback = 1;
107 unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE;
108 int sctp_sendspace = (128 * 1024);
109 int sctp_recvspace = 128 * (1024 +
110 #ifdef INET6
111 sizeof(struct sockaddr_in6)
112 #else
113 sizeof(struct sockaddr_in)
114 #endif
115 );
116 int sctp_strict_sacks = 0;
117 int sctp_ecn = 1;
118 int sctp_ecn_nonce = 0;
119
120 unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC;
121 unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC;
122 unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC;
123 unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC;
124 unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC;
125 unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND;
126 unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND;
127 unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL;
128 unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND;
129 unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE;
130 unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT;
131 unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND;
132 unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2;
133 unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL;
134
135 static void sysctl_net_inet_sctp_setup(struct sysctllog **);
136
137 void
sctp_init(void)138 sctp_init(void)
139 {
140 /* Init the SCTP pcb in sctp_pcb.c */
141 u_long sb_max_adj;
142
143 sysctl_net_inet_sctp_setup(NULL);
144
145 sctp_pcb_init();
146
147 if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
148 sctp_max_chunks_on_queue = nmbclusters;
149 /*
150 * Allow a user to take no more than 1/2 the number of clusters
151 * or the SB_MAX whichever is smaller for the send window.
152 */
153 sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
154 sctp_sendspace = uimin((uimin(SB_MAX, sb_max_adj)),
155 ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT));
156 /*
157 * Now for the recv window, should we take the same amount?
158 * or should I do 1/2 the SB_MAX instead in the SB_MAX min above.
159 * For now I will just copy.
160 */
161 sctp_recvspace = sctp_sendspace;
162 }
163
164 #ifdef INET6
165 void
ip_2_ip6_hdr(struct ip6_hdr * ip6,struct ip * ip)166 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
167 {
168 memset(ip6, 0, sizeof(*ip6));
169
170 ip6->ip6_vfc = IPV6_VERSION;
171 ip6->ip6_plen = ip->ip_len;
172 ip6->ip6_nxt = ip->ip_p;
173 ip6->ip6_hlim = ip->ip_ttl;
174 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
175 IPV6_ADDR_INT32_SMP;
176 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
177 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
178 }
179 #endif /* INET6 */
180
181 static void
sctp_split_chunks(struct sctp_association * asoc,struct sctp_stream_out * strm,struct sctp_tmit_chunk * chk)182 sctp_split_chunks(struct sctp_association *asoc,
183 struct sctp_stream_out *strm,
184 struct sctp_tmit_chunk *chk)
185 {
186 struct sctp_tmit_chunk *new_chk;
187
188 /* First we need a chunk */
189 new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
190 if (new_chk == NULL) {
191 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
192 return;
193 }
194 sctppcbinfo.ipi_count_chunk++;
195 sctppcbinfo.ipi_gencnt_chunk++;
196 /* Copy it all */
197 *new_chk = *chk;
198 /* split the data */
199 new_chk->data = m_split(chk->data, (chk->send_size>>1), M_DONTWAIT);
200 if (new_chk->data == NULL) {
201 /* Can't split */
202 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
203 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk);
204 sctppcbinfo.ipi_count_chunk--;
205 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
206 panic("Chunk count is negative");
207 }
208 sctppcbinfo.ipi_gencnt_chunk++;
209 return;
210
211 }
212 /* Data is now split adjust sizes */
213 chk->send_size >>= 1;
214 new_chk->send_size >>= 1;
215
216 chk->book_size >>= 1;
217 new_chk->book_size >>= 1;
218
219 /* now adjust the marks */
220 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
221 chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG;
222
223 new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG;
224 new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
225
226 /* Increase ref count if dest is set */
227 if (chk->whoTo) {
228 new_chk->whoTo->ref_count++;
229 }
230 /* now drop it on the end of the list*/
231 asoc->stream_queue_cnt++;
232 TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next);
233 }
234
235 static void
sctp_notify_mbuf(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net,struct ip * ip,struct sctphdr * sh)236 sctp_notify_mbuf(struct sctp_inpcb *inp,
237 struct sctp_tcb *stcb,
238 struct sctp_nets *net,
239 struct ip *ip,
240 struct sctphdr *sh)
241
242 {
243 struct icmp *icmph;
244 int totsz;
245 uint16_t nxtsz;
246
247 /* protection */
248 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
249 (ip == NULL) || (sh == NULL)) {
250 if (stcb != NULL) {
251 SCTP_TCB_UNLOCK(stcb);
252 }
253 return;
254 }
255 /* First job is to verify the vtag matches what I would send */
256 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
257 SCTP_TCB_UNLOCK(stcb);
258 return;
259 }
260 icmph = (struct icmp *)((vaddr_t)ip - (sizeof(struct icmp) -
261 sizeof(struct ip)));
262 if (icmph->icmp_type != ICMP_UNREACH) {
263 /* We only care about unreachable */
264 SCTP_TCB_UNLOCK(stcb);
265 return;
266 }
267 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
268 /* not a unreachable message due to frag. */
269 SCTP_TCB_UNLOCK(stcb);
270 return;
271 }
272 totsz = ip->ip_len;
273 nxtsz = ntohs(icmph->icmp_seq);
274 if (nxtsz == 0) {
275 /*
276 * old type router that does not tell us what the next size
277 * mtu is. Rats we will have to guess (in a educated fashion
278 * of course)
279 */
280 nxtsz = find_next_best_mtu(totsz);
281 }
282
283 /* Stop any PMTU timer */
284 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
285
286 /* Adjust destination size limit */
287 if (net->mtu > nxtsz) {
288 net->mtu = nxtsz;
289 }
290 /* now what about the ep? */
291 if (stcb->asoc.smallest_mtu > nxtsz) {
292 struct sctp_tmit_chunk *chk, *nchk;
293 struct sctp_stream_out *strm;
294 /* Adjust that too */
295 stcb->asoc.smallest_mtu = nxtsz;
296 /* now off to subtract IP_DF flag if needed */
297
298 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
299 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
300 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
301 }
302 }
303 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
304 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
305 /*
306 * For this guy we also mark for immediate
307 * resend since we sent to big of chunk
308 */
309 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
310 if (chk->sent != SCTP_DATAGRAM_RESEND) {
311 stcb->asoc.sent_queue_retran_cnt++;
312 }
313 chk->sent = SCTP_DATAGRAM_RESEND;
314 chk->rec.data.doing_fast_retransmit = 0;
315
316 /* Clear any time so NO RTT is being done */
317 chk->do_rtt = 0;
318 sctp_total_flight_decrease(stcb, chk);
319 if (net->flight_size >= chk->book_size) {
320 net->flight_size -= chk->book_size;
321 } else {
322 net->flight_size = 0;
323 }
324 }
325 }
326 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
327 chk = TAILQ_FIRST(&strm->outqueue);
328 while (chk) {
329 nchk = TAILQ_NEXT(chk, sctp_next);
330 if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) {
331 sctp_split_chunks(&stcb->asoc, strm, chk);
332 }
333 chk = nchk;
334 }
335 }
336 }
337 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
338 SCTP_TCB_UNLOCK(stcb);
339 }
340
341
342 void
sctp_notify(struct sctp_inpcb * inp,int errno,struct sctphdr * sh,struct sockaddr * to,struct sctp_tcb * stcb,struct sctp_nets * net)343 sctp_notify(struct sctp_inpcb *inp,
344 int errno,
345 struct sctphdr *sh,
346 struct sockaddr *to,
347 struct sctp_tcb *stcb,
348 struct sctp_nets *net)
349 {
350 /* protection */
351 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
352 (sh == NULL) || (to == NULL)) {
353 #ifdef SCTP_DEBUG
354 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
355 printf("sctp-notify, bad call\n");
356 }
357 #endif /* SCTP_DEBUG */
358 return;
359 }
360 /* First job is to verify the vtag matches what I would send */
361 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
362 return;
363 }
364
365 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
366
367 if ((errno == EHOSTUNREACH) || /* Host is not reachable */
368 (errno == EHOSTDOWN) || /* Host is down */
369 (errno == ECONNREFUSED) || /* Host refused the connection, (not an abort?) */
370 (errno == ENOPROTOOPT) /* SCTP is not present on host */
371 ) {
372 /*
373 * Hmm reachability problems we must examine closely.
374 * If its not reachable, we may have lost a network.
375 * Or if there is NO protocol at the other end named SCTP.
376 * well we consider it a OOTB abort.
377 */
378 if ((errno == EHOSTUNREACH) || (errno == EHOSTDOWN)) {
379 if (net->dest_state & SCTP_ADDR_REACHABLE) {
380 /* Ok that destination is NOT reachable */
381 net->dest_state &= ~SCTP_ADDR_REACHABLE;
382 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
383 net->error_count = net->failure_threshold + 1;
384 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
385 stcb, SCTP_FAILED_THRESHOLD,
386 (void *)net);
387 }
388 if (stcb) {
389 SCTP_TCB_UNLOCK(stcb);
390 }
391 } else {
392 /*
393 * Here the peer is either playing tricks on us,
394 * including an address that belongs to someone who
395 * does not support SCTP OR was a userland
396 * implementation that shutdown and now is dead. In
397 * either case treat it like a OOTB abort with no TCB
398 */
399 sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
400 sctp_free_assoc(inp, stcb);
401 /* no need to unlock here, since the TCB is gone */
402 }
403 } else {
404 /* Send all others to the app */
405 if (inp->sctp_socket) {
406 inp->sctp_socket->so_error = errno;
407 sctp_sowwakeup(inp, inp->sctp_socket);
408 }
409 if (stcb) {
410 SCTP_TCB_UNLOCK(stcb);
411 }
412 }
413 }
414
415 void *
sctp_ctlinput(int cmd,const struct sockaddr * sa,void * vip)416 sctp_ctlinput(int cmd, const struct sockaddr *sa, void *vip)
417 {
418 struct ip *ip = vip;
419 struct sctphdr *sh;
420 int s;
421
422 if (sa->sa_family != AF_INET ||
423 ((const struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
424 return (NULL);
425 }
426
427 if (PRC_IS_REDIRECT(cmd)) {
428 ip = 0;
429 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
430 return (NULL);
431 }
432 if (ip) {
433 struct sctp_inpcb *inp;
434 struct sctp_tcb *stcb;
435 struct sctp_nets *net;
436 struct sockaddr_in to, from;
437
438 sh = (struct sctphdr *)((vaddr_t)ip + (ip->ip_hl << 2));
439 memset(&to, 0, sizeof(to));
440 memset(&from, 0, sizeof(from));
441 from.sin_family = to.sin_family = AF_INET;
442 from.sin_len = to.sin_len = sizeof(to);
443 from.sin_port = sh->src_port;
444 from.sin_addr = ip->ip_src;
445 to.sin_port = sh->dest_port;
446 to.sin_addr = ip->ip_dst;
447
448 /*
449 * 'to' holds the dest of the packet that failed to be sent.
450 * 'from' holds our local endpoint address.
451 * Thus we reverse the to and the from in the lookup.
452 */
453 s = splsoftnet();
454 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
455 (struct sockaddr *)&to,
456 &inp, &net, 1);
457 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
458 if (cmd != PRC_MSGSIZE) {
459 int cm;
460 if (cmd == PRC_HOSTDEAD) {
461 cm = EHOSTUNREACH;
462 } else {
463 cm = inetctlerrmap[cmd];
464 }
465 sctp_notify(inp, cm, sh,
466 (struct sockaddr *)&to, stcb,
467 net);
468 } else {
469 /* handle possible ICMP size messages */
470 sctp_notify_mbuf(inp, stcb, net, ip, sh);
471 }
472 } else {
473 #if defined(__FreeBSD__) && __FreeBSD_version < 500000
474 /* XXX must be fixed for 5.x and higher, leave for 4.x */
475 if (PRC_IS_REDIRECT(cmd) && inp) {
476 inpcb_rtchange((struct inpcb *)inp,
477 inetctlerrmap[cmd]);
478 }
479 #endif
480 if ((stcb == NULL) && (inp != NULL)) {
481 /* reduce ref-count */
482 SCTP_INP_WLOCK(inp);
483 SCTP_INP_DECR_REF(inp);
484 SCTP_INP_WUNLOCK(inp);
485 }
486
487 }
488 splx(s);
489 }
490 return (NULL);
491 }
492
493 static int
sctp_abort(struct socket * so)494 sctp_abort(struct socket *so)
495 {
496 struct sctp_inpcb *inp;
497
498 inp = (struct sctp_inpcb *)so->so_pcb;
499 if (inp == 0)
500 return EINVAL; /* ??? possible? panic instead? */
501
502 sctp_inpcb_free(inp, 1);
503 return 0;
504 }
505
506 static int
sctp_attach(struct socket * so,int proto)507 sctp_attach(struct socket *so, int proto)
508 {
509 struct sctp_inpcb *inp;
510 #ifdef IPSEC
511 struct inpcb *ip_inp;
512 #endif
513 int error;
514
515 sosetlock(so);
516 inp = (struct sctp_inpcb *)so->so_pcb;
517 if (inp != 0) {
518 return EINVAL;
519 }
520 error = soreserve(so, sctp_sendspace, sctp_recvspace);
521 if (error) {
522 return error;
523 }
524 error = sctp_inpcb_alloc(so);
525 if (error) {
526 return error;
527 }
528 inp = (struct sctp_inpcb *)so->so_pcb;
529 SCTP_INP_WLOCK(inp);
530
531 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
532 #ifdef IPSEC
533 ip_inp = &inp->ip_inp.inp;
534 ip_inp->inp_af = proto;
535 #endif
536 inp->inp_vflag |= INP_IPV4;
537 inp->inp_ip_ttl = ip_defttl;
538
539 #ifdef IPSEC
540 error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
541 if (error != 0) {
542 sctp_inpcb_free(inp, 1);
543 return error;
544 }
545 #endif /*IPSEC*/
546 SCTP_INP_WUNLOCK(inp);
547 so->so_send = sctp_sosend;
548 return 0;
549 }
550
551 static int
sctp_bind(struct socket * so,struct sockaddr * nam,struct lwp * l)552 sctp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
553 {
554 struct sctp_inpcb *inp;
555 int error;
556
557 KASSERT(solocked(so));
558
559 #ifdef INET6
560 if (nam && nam->sa_family != AF_INET)
561 /* must be a v4 address! */
562 return EINVAL;
563 #endif /* INET6 */
564
565 inp = (struct sctp_inpcb *)so->so_pcb;
566 if (inp == 0)
567 return EINVAL;
568
569 error = sctp_inpcb_bind(so, nam, l);
570 return error;
571 }
572
573
574 static int
sctp_detach(struct socket * so)575 sctp_detach(struct socket *so)
576 {
577 struct sctp_inpcb *inp;
578
579 inp = (struct sctp_inpcb *)so->so_pcb;
580 if (inp == 0)
581 return EINVAL;
582
583 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
584 (so->so_rcv.sb_cc > 0)) {
585 sctp_inpcb_free(inp, 1);
586 } else {
587 sctp_inpcb_free(inp, 0);
588 }
589 return 0;
590 }
591
592 static int
sctp_recvoob(struct socket * so,struct mbuf * m,int flags)593 sctp_recvoob(struct socket *so, struct mbuf *m, int flags)
594 {
595 KASSERT(solocked(so));
596
597 return EOPNOTSUPP;
598 }
599
600 int
sctp_send(struct socket * so,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct lwp * l)601 sctp_send(struct socket *so, struct mbuf *m, struct sockaddr *addr,
602 struct mbuf *control, struct lwp *l)
603 {
604 struct sctp_inpcb *inp;
605 int error;
606 inp = (struct sctp_inpcb *)so->so_pcb;
607 if (inp == 0) {
608 sctp_m_freem(control);
609 control = NULL;
610 sctp_m_freem(m);
611 return EINVAL;
612 }
613 /* Got to have a to address if we are NOT a connected socket */
614 if ((addr == NULL) &&
615 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
616 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
617 ) {
618 goto connected_type;
619 } else if (addr == NULL) {
620 error = EDESTADDRREQ;
621 sctp_m_freem(m);
622 sctp_m_freem(control);
623 control = NULL;
624 return (error);
625 }
626 #ifdef INET6
627 if (addr->sa_family != AF_INET) {
628 /* must be a v4 address! */
629 sctp_m_freem(m);
630 sctp_m_freem(control);
631 control = NULL;
632 error = EDESTADDRREQ;
633 return EINVAL;
634 }
635 #endif /* INET6 */
636
637 /*
638 * XXX XXX XXX Check addr->sa_len?
639 */
640
641 connected_type:
642 /* now what about control */
643 if (control) {
644 if (inp->control) {
645 printf("huh? control set?\n");
646 sctp_m_freem(inp->control);
647 inp->control = NULL;
648 }
649 inp->control = control;
650 }
651 /* add it in possibly */
652 if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) {
653 struct mbuf *x;
654 int c_len;
655
656 c_len = 0;
657 /* How big is it */
658 for (x=m;x;x = x->m_next) {
659 c_len += x->m_len;
660 }
661 inp->pkt->m_pkthdr.len += c_len;
662 }
663 /* Place the data */
664 if (inp->pkt) {
665 inp->pkt_last->m_next = m;
666 inp->pkt_last = m;
667 } else {
668 inp->pkt_last = inp->pkt = m;
669 }
670 if ((so->so_state & SS_MORETOCOME) == 0) {
671 /*
672 * note with the current version this code will only be used
673 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
674 * re-defining sosend to use the sctp_sosend. One can
675 * optionally switch back to this code (by changing back the
676 * definitions) but this is not advisable.
677 */
678 int ret;
679 ret = sctp_output(inp, inp->pkt, addr, inp->control, l, 0);
680 inp->pkt = NULL;
681 inp->control = NULL;
682 return (ret);
683 } else {
684 return (0);
685 }
686 }
687
688 static int
sctp_disconnect(struct socket * so)689 sctp_disconnect(struct socket *so)
690 {
691 struct sctp_inpcb *inp;
692 int s;
693
694 inp = (struct sctp_inpcb *)so->so_pcb;
695 if (inp == NULL) {
696 return (ENOTCONN);
697 }
698 s = splsoftnet();
699 SCTP_INP_RLOCK(inp);
700 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
701 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
702 /* No connection */
703 SCTP_INP_RUNLOCK(inp);
704 splx(s);
705 return (0);
706 } else {
707 int some_on_streamwheel = 0;
708 struct sctp_association *asoc;
709 struct sctp_tcb *stcb;
710
711 stcb = LIST_FIRST(&inp->sctp_asoc_list);
712 if (stcb == NULL) {
713 SCTP_INP_RUNLOCK(inp);
714 splx(s);
715 return (EINVAL);
716 }
717 asoc = &stcb->asoc;
718 SCTP_TCB_LOCK(stcb);
719 if (((so->so_options & SO_LINGER) &&
720 (so->so_linger == 0)) ||
721 (so->so_rcv.sb_cc > 0)) {
722 if (SCTP_GET_STATE(asoc) !=
723 SCTP_STATE_COOKIE_WAIT) {
724 /* Left with Data unread */
725 struct mbuf *err;
726 err = NULL;
727 MGET(err, M_DONTWAIT, MT_DATA);
728 if (err) {
729 /* Fill in the user initiated abort */
730 struct sctp_paramhdr *ph;
731 ph = mtod(err, struct sctp_paramhdr *);
732 err->m_len = sizeof(struct sctp_paramhdr);
733 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
734 ph->param_length = htons(err->m_len);
735 }
736 sctp_send_abort_tcb(stcb, err);
737 }
738 SCTP_INP_RUNLOCK(inp);
739 sctp_free_assoc(inp, stcb);
740 /* No unlock tcb assoc is gone */
741 splx(s);
742 return (0);
743 }
744 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
745 /* Check to see if some data queued */
746 struct sctp_stream_out *outs;
747 TAILQ_FOREACH(outs, &asoc->out_wheel,
748 next_spoke) {
749 if (!TAILQ_EMPTY(&outs->outqueue)) {
750 some_on_streamwheel = 1;
751 break;
752 }
753 }
754 }
755
756 if (TAILQ_EMPTY(&asoc->send_queue) &&
757 TAILQ_EMPTY(&asoc->sent_queue) &&
758 (some_on_streamwheel == 0)) {
759 /* there is nothing queued to send, so done */
760 if ((SCTP_GET_STATE(asoc) !=
761 SCTP_STATE_SHUTDOWN_SENT) &&
762 (SCTP_GET_STATE(asoc) !=
763 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
764 /* only send SHUTDOWN 1st time thru */
765 #ifdef SCTP_DEBUG
766 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
767 printf("%s:%d sends a shutdown\n",
768 __FILE__,
769 __LINE__
770 );
771 }
772 #endif
773 sctp_send_shutdown(stcb,
774 stcb->asoc.primary_destination);
775 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
776 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
777 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
778 stcb->sctp_ep, stcb,
779 asoc->primary_destination);
780 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
781 stcb->sctp_ep, stcb,
782 asoc->primary_destination);
783 }
784 } else {
785 /*
786 * we still got (or just got) data to send,
787 * so set SHUTDOWN_PENDING
788 */
789 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
790 }
791 SCTP_TCB_UNLOCK(stcb);
792 SCTP_INP_RUNLOCK(inp);
793 splx(s);
794 return (0);
795 }
796 /* not reached */
797 } else {
798 /* UDP model does not support this */
799 SCTP_INP_RUNLOCK(inp);
800 splx(s);
801 return EOPNOTSUPP;
802 }
803 }
804
805 int
sctp_shutdown(struct socket * so)806 sctp_shutdown(struct socket *so)
807 {
808 struct sctp_inpcb *inp;
809
810 inp = (struct sctp_inpcb *)so->so_pcb;
811 if (inp == 0) {
812 return EINVAL;
813 }
814 SCTP_INP_RLOCK(inp);
815 /* For UDP model this is a invalid call */
816 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
817 /* Restore the flags that the soshutdown took away. */
818 so->so_state &= ~SS_CANTRCVMORE;
819 /* This proc will wakeup for read and do nothing (I hope) */
820 SCTP_INP_RUNLOCK(inp);
821 return (EOPNOTSUPP);
822 }
823 /*
824 * Ok if we reach here its the TCP model and it is either a SHUT_WR
825 * or SHUT_RDWR. This means we put the shutdown flag against it.
826 */
827 {
828 int some_on_streamwheel = 0;
829 struct sctp_tcb *stcb;
830 struct sctp_association *asoc;
831 socantsendmore(so);
832
833 stcb = LIST_FIRST(&inp->sctp_asoc_list);
834 if (stcb == NULL) {
835 /*
836 * Ok we hit the case that the shutdown call was made
837 * after an abort or something. Nothing to do now.
838 */
839 return (0);
840 }
841 SCTP_TCB_LOCK(stcb);
842 asoc = &stcb->asoc;
843
844 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
845 /* Check to see if some data queued */
846 struct sctp_stream_out *outs;
847 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
848 if (!TAILQ_EMPTY(&outs->outqueue)) {
849 some_on_streamwheel = 1;
850 break;
851 }
852 }
853 }
854 if (TAILQ_EMPTY(&asoc->send_queue) &&
855 TAILQ_EMPTY(&asoc->sent_queue) &&
856 (some_on_streamwheel == 0)) {
857 /* there is nothing queued to send, so I'm done... */
858 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
859 /* only send SHUTDOWN the first time through */
860 #ifdef SCTP_DEBUG
861 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
862 printf("%s:%d sends a shutdown\n",
863 __FILE__,
864 __LINE__
865 );
866 }
867 #endif
868 sctp_send_shutdown(stcb,
869 stcb->asoc.primary_destination);
870 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
871 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
872 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
873 stcb->sctp_ep, stcb,
874 asoc->primary_destination);
875 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
876 stcb->sctp_ep, stcb,
877 asoc->primary_destination);
878 }
879 } else {
880 /*
881 * we still got (or just got) data to send, so
882 * set SHUTDOWN_PENDING
883 */
884 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
885 }
886 SCTP_TCB_UNLOCK(stcb);
887 }
888 SCTP_INP_RUNLOCK(inp);
889 return 0;
890 }
891
892 /*
893 * copies a "user" presentable address and removes embedded scope, etc.
894 * returns 0 on success, 1 on error
895 */
896 static uint32_t
sctp_fill_user_address(struct sockaddr_storage * ss,struct sockaddr * sa)897 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
898 {
899 struct sockaddr_in6 lsa6;
900
901 sctp_recover_scope((struct sockaddr_in6 *)sa, &lsa6);
902 memcpy(ss, sa, sa->sa_len);
903 return (0);
904 }
905
906
907 static int
sctp_fill_up_addresses(struct sctp_inpcb * inp,struct sctp_tcb * stcb,int limit,struct sockaddr_storage * sas)908 sctp_fill_up_addresses(struct sctp_inpcb *inp,
909 struct sctp_tcb *stcb,
910 int limit,
911 struct sockaddr_storage *sas)
912 {
913 struct ifnet *ifn;
914 struct ifaddr *ifa;
915 int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
916 int ipv4_addr_legal, ipv6_addr_legal;
917 actual = 0;
918 if (limit <= 0)
919 return (actual);
920
921 if (stcb) {
922 /* Turn on all the appropriate scope */
923 loopback_scope = stcb->asoc.loopback_scope;
924 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
925 local_scope = stcb->asoc.local_scope;
926 site_scope = stcb->asoc.site_scope;
927 } else {
928 /* Turn on ALL scope, since we look at the EP */
929 loopback_scope = ipv4_local_scope = local_scope =
930 site_scope = 1;
931 }
932 ipv4_addr_legal = ipv6_addr_legal = 0;
933 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
934 ipv6_addr_legal = 1;
935 if (
936 #if defined(__OpenBSD__)
937 (0) /* we always do dual bind */
938 #elif defined (__NetBSD__)
939 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
940 #else
941 (((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
942 #endif
943 == 0) {
944 ipv4_addr_legal = 1;
945 }
946 } else {
947 ipv4_addr_legal = 1;
948 }
949
950 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
951 int s = pserialize_read_enter();
952 IFNET_READER_FOREACH(ifn) {
953 if ((loopback_scope == 0) &&
954 (ifn->if_type == IFT_LOOP)) {
955 /* Skip loopback if loopback_scope not set */
956 continue;
957 }
958 IFADDR_READER_FOREACH(ifa, ifn) {
959 if (stcb) {
960 /*
961 * For the BOUND-ALL case, the list
962 * associated with a TCB is Always
963 * considered a reverse list.. i.e.
964 * it lists addresses that are NOT
965 * part of the association. If this
966 * is one of those we must skip it.
967 */
968 if (sctp_is_addr_restricted(stcb,
969 ifa->ifa_addr)) {
970 continue;
971 }
972 }
973 if ((ifa->ifa_addr->sa_family == AF_INET) &&
974 (ipv4_addr_legal)) {
975 struct sockaddr_in *sin;
976 sin = (struct sockaddr_in *)ifa->ifa_addr;
977 if (sin->sin_addr.s_addr == 0) {
978 /* we skip unspecified addresses */
979 continue;
980 }
981 if ((ipv4_local_scope == 0) &&
982 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
983 continue;
984 }
985 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
986 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
987 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
988 sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(struct sockaddr_in6));
989 actual += sizeof(struct sockaddr_in6);
990 } else {
991 memcpy(sas, sin, sizeof(*sin));
992 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
993 sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin));
994 actual += sizeof(*sin);
995 }
996 if (actual >= limit) {
997 pserialize_read_exit(s);
998 return (actual);
999 }
1000 } else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
1001 (ipv6_addr_legal)) {
1002 struct sockaddr_in6 *sin6;
1003 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1004 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1005 /*
1006 * we skip unspecified
1007 * addresses
1008 */
1009 continue;
1010 }
1011 if ((site_scope == 0) &&
1012 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1013 continue;
1014 }
1015 memcpy(sas, sin6, sizeof(*sin6));
1016 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1017 sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin6));
1018 actual += sizeof(*sin6);
1019 if (actual >= limit) {
1020 pserialize_read_exit(s);
1021 return (actual);
1022 }
1023 }
1024 }
1025 }
1026 pserialize_read_exit(s);
1027 } else {
1028 struct sctp_laddr *laddr;
1029 /*
1030 * If we have a TCB and we do NOT support ASCONF (it's
1031 * turned off or otherwise) then the list is always the
1032 * true list of addresses (the else case below). Otherwise
1033 * the list on the association is a list of addresses that
1034 * are NOT part of the association.
1035 */
1036 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1037 /* The list is a NEGATIVE list */
1038 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1039 if (stcb) {
1040 if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
1041 continue;
1042 }
1043 }
1044 if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
1045 continue;
1046
1047 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1048 sas = (struct sockaddr_storage *)((vaddr_t)sas +
1049 laddr->ifa->ifa_addr->sa_len);
1050 actual += laddr->ifa->ifa_addr->sa_len;
1051 if (actual >= limit) {
1052 return (actual);
1053 }
1054 }
1055 } else {
1056 /* The list is a positive list if present */
1057 if (stcb) {
1058 /* Must use the specific association list */
1059 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1060 sctp_nxt_addr) {
1061 if (sctp_fill_user_address(sas,
1062 laddr->ifa->ifa_addr))
1063 continue;
1064 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1065 sas = (struct sockaddr_storage *)((vaddr_t)sas +
1066 laddr->ifa->ifa_addr->sa_len);
1067 actual += laddr->ifa->ifa_addr->sa_len;
1068 if (actual >= limit) {
1069 return (actual);
1070 }
1071 }
1072 } else {
1073 /* No endpoint so use the endpoints individual list */
1074 LIST_FOREACH(laddr, &inp->sctp_addr_list,
1075 sctp_nxt_addr) {
1076 if (sctp_fill_user_address(sas,
1077 laddr->ifa->ifa_addr))
1078 continue;
1079 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1080 sas = (struct sockaddr_storage *)((vaddr_t)sas +
1081 laddr->ifa->ifa_addr->sa_len);
1082 actual += laddr->ifa->ifa_addr->sa_len;
1083 if (actual >= limit) {
1084 return (actual);
1085 }
1086 }
1087 }
1088 }
1089 }
1090 return (actual);
1091 }
1092
1093 static int
sctp_count_max_addresses(struct sctp_inpcb * inp)1094 sctp_count_max_addresses(struct sctp_inpcb *inp)
1095 {
1096 int cnt = 0;
1097 /*
1098 * In both sub-set bound and bound_all cases we return the MAXIMUM
1099 * number of addresses that you COULD get. In reality the sub-set
1100 * bound may have an exclusion list for a given TCB OR in the
1101 * bound-all case a TCB may NOT include the loopback or other
1102 * addresses as well.
1103 */
1104 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1105 struct ifnet *ifn;
1106 struct ifaddr *ifa;
1107 int s;
1108
1109 s = pserialize_read_enter();
1110 IFNET_READER_FOREACH(ifn) {
1111 IFADDR_READER_FOREACH(ifa, ifn) {
1112 /* Count them if they are the right type */
1113 if (ifa->ifa_addr->sa_family == AF_INET) {
1114 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1115 cnt += sizeof(struct sockaddr_in6);
1116 else
1117 cnt += sizeof(struct sockaddr_in);
1118
1119 } else if (ifa->ifa_addr->sa_family == AF_INET6)
1120 cnt += sizeof(struct sockaddr_in6);
1121 }
1122 }
1123 pserialize_read_exit(s);
1124 } else {
1125 struct sctp_laddr *laddr;
1126 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1127 if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
1128 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1129 cnt += sizeof(struct sockaddr_in6);
1130 else
1131 cnt += sizeof(struct sockaddr_in);
1132
1133 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
1134 cnt += sizeof(struct sockaddr_in6);
1135 }
1136 }
1137 return (cnt);
1138 }
1139
1140 int
sctp_do_connect_x(struct socket * so,struct sctp_connectx_addrs * sca,struct lwp * l,int delay)1141 sctp_do_connect_x(struct socket *so, struct sctp_connectx_addrs *sca,
1142 struct lwp *l, int delay)
1143 {
1144 int error = 0;
1145 struct sctp_inpcb *inp;
1146 struct sctp_tcb *stcb = NULL;
1147 struct sockaddr *sa;
1148 int num_v6=0, num_v4=0, totaddr, i, incr, at;
1149 char buf[2048];
1150 size_t len;
1151 sctp_assoc_t id;
1152 #ifdef SCTP_DEBUG
1153 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1154 printf("Connectx called\n");
1155 }
1156 #endif /* SCTP_DEBUG */
1157
1158 inp = (struct sctp_inpcb *)so->so_pcb;
1159 if (inp == 0)
1160 return EINVAL;
1161
1162 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1163 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1164 /* We are already connected AND the TCP model */
1165 return (EADDRINUSE);
1166 }
1167 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1168 SCTP_INP_RLOCK(inp);
1169 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1170 SCTP_INP_RUNLOCK(inp);
1171 }
1172 if (stcb) {
1173 return (EALREADY);
1174
1175 }
1176 SCTP_ASOC_CREATE_LOCK(inp);
1177 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1178 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1179 SCTP_ASOC_CREATE_UNLOCK(inp);
1180 return (EFAULT);
1181 }
1182
1183 len = sca->cx_len;
1184 totaddr = sca->cx_num;
1185 if (len > sizeof(buf)) {
1186 return E2BIG;
1187 }
1188 error = copyin(sca->cx_addrs, buf, len);
1189 if (error) {
1190 return error;
1191 }
1192 sa = (struct sockaddr *)buf;
1193 at = incr = 0;
1194 /* account and validate addresses */
1195 SCTP_INP_WLOCK(inp);
1196 SCTP_INP_INCR_REF(inp);
1197 SCTP_INP_WUNLOCK(inp);
1198 for (i = 0; i < totaddr; i++) {
1199 if (sa->sa_family == AF_INET) {
1200 num_v4++;
1201 incr = sizeof(struct sockaddr_in);
1202 } else if (sa->sa_family == AF_INET6) {
1203 struct sockaddr_in6 *sin6;
1204 sin6 = (struct sockaddr_in6 *)sa;
1205 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1206 /* Must be non-mapped for connectx */
1207 SCTP_ASOC_CREATE_UNLOCK(inp);
1208 return EINVAL;
1209 }
1210 num_v6++;
1211 incr = sizeof(struct sockaddr_in6);
1212 } else {
1213 totaddr = i;
1214 break;
1215 }
1216 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1217 if (stcb != NULL) {
1218 /* Already have or am bring up an association */
1219 SCTP_ASOC_CREATE_UNLOCK(inp);
1220 SCTP_TCB_UNLOCK(stcb);
1221 return (EALREADY);
1222 }
1223 if ((at + incr) > len) {
1224 totaddr = i;
1225 break;
1226 }
1227 sa = (struct sockaddr *)((vaddr_t)sa + incr);
1228 }
1229 sa = (struct sockaddr *)buf;
1230 SCTP_INP_WLOCK(inp);
1231 SCTP_INP_DECR_REF(inp);
1232 SCTP_INP_WUNLOCK(inp);
1233 #ifdef INET6
1234 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1235 (num_v6 > 0)) {
1236 SCTP_INP_WUNLOCK(inp);
1237 SCTP_ASOC_CREATE_UNLOCK(inp);
1238 return (EINVAL);
1239 }
1240 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1241 (num_v4 > 0)) {
1242 struct in6pcb *inp6;
1243 inp6 = (struct in6pcb *)inp;
1244 if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
1245 /*
1246 * if IPV6_V6ONLY flag, ignore connections
1247 * destined to a v4 addr or v4-mapped addr
1248 */
1249 SCTP_INP_WUNLOCK(inp);
1250 SCTP_ASOC_CREATE_UNLOCK(inp);
1251 return EINVAL;
1252 }
1253 }
1254 #endif /* INET6 */
1255 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1256 SCTP_PCB_FLAGS_UNBOUND) {
1257 /* Bind a ephemeral port */
1258 SCTP_INP_WUNLOCK(inp);
1259 error = sctp_inpcb_bind(so, NULL, l);
1260 if (error) {
1261 SCTP_ASOC_CREATE_UNLOCK(inp);
1262 return (error);
1263 }
1264 } else {
1265 SCTP_INP_WUNLOCK(inp);
1266 }
1267 /* We are GOOD to go */
1268 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
1269 if (stcb == NULL) {
1270 /* Gak! no memory */
1271 SCTP_ASOC_CREATE_UNLOCK(inp);
1272 return (error);
1273 }
1274
1275 /* move to second address */
1276 if (sa->sa_family == AF_INET)
1277 sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in));
1278 else
1279 sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in6));
1280
1281 for (i = 1; i < totaddr; i++) {
1282 if (sa->sa_family == AF_INET) {
1283 incr = sizeof(struct sockaddr_in);
1284 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1285 /* assoc gone no un-lock */
1286 sctp_free_assoc(inp, stcb);
1287 SCTP_ASOC_CREATE_UNLOCK(inp);
1288 return (ENOBUFS);
1289 }
1290
1291 } else if (sa->sa_family == AF_INET6) {
1292 incr = sizeof(struct sockaddr_in6);
1293 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1294 /* assoc gone no un-lock */
1295 sctp_free_assoc(inp, stcb);
1296 SCTP_ASOC_CREATE_UNLOCK(inp);
1297 return (ENOBUFS);
1298 }
1299 }
1300 sa = (struct sockaddr *)((vaddr_t)sa + incr);
1301 }
1302 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1303
1304 id = sctp_get_associd(stcb);
1305 memcpy(&sca->cx_num, &id, sizeof(sctp_assoc_t));
1306
1307 if (delay) {
1308 /* doing delayed connection */
1309 stcb->asoc.delayed_connection = 1;
1310 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1311 } else {
1312 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1313 sctp_send_initiate(inp, stcb);
1314 }
1315 SCTP_TCB_UNLOCK(stcb);
1316 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1317 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1318 /* Set the connected flag so we can queue data */
1319 soisconnecting(so);
1320 }
1321 SCTP_ASOC_CREATE_UNLOCK(inp);
1322 return error;
1323 }
1324
1325
1326 static int
sctp_optsget(struct socket * so,struct sockopt * sopt)1327 sctp_optsget(struct socket *so, struct sockopt *sopt)
1328 {
1329 struct sctp_inpcb *inp;
1330 int error, optval=0;
1331 int *ovp;
1332 struct sctp_tcb *stcb = NULL;
1333
1334 inp = (struct sctp_inpcb *)so->so_pcb;
1335 if (inp == 0)
1336 return EINVAL;
1337 error = 0;
1338
1339 #ifdef SCTP_DEBUG
1340 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1341 printf("optsget opt:%x sz:%zu\n", sopt->sopt_name,
1342 sopt->sopt_size);
1343 }
1344 #endif /* SCTP_DEBUG */
1345
1346 switch (sopt->sopt_name) {
1347 case SCTP_NODELAY:
1348 case SCTP_AUTOCLOSE:
1349 case SCTP_AUTO_ASCONF:
1350 case SCTP_DISABLE_FRAGMENTS:
1351 case SCTP_I_WANT_MAPPED_V4_ADDR:
1352 #ifdef SCTP_DEBUG
1353 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1354 printf("other stuff\n");
1355 }
1356 #endif /* SCTP_DEBUG */
1357 SCTP_INP_RLOCK(inp);
1358 switch (sopt->sopt_name) {
1359 case SCTP_DISABLE_FRAGMENTS:
1360 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
1361 break;
1362 case SCTP_I_WANT_MAPPED_V4_ADDR:
1363 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
1364 break;
1365 case SCTP_AUTO_ASCONF:
1366 optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
1367 break;
1368 case SCTP_NODELAY:
1369 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
1370 break;
1371 case SCTP_AUTOCLOSE:
1372 if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
1373 SCTP_PCB_FLAGS_AUTOCLOSE)
1374 optval = inp->sctp_ep.auto_close_time;
1375 else
1376 optval = 0;
1377 break;
1378
1379 default:
1380 error = ENOPROTOOPT;
1381 } /* end switch (sopt->sopt_name) */
1382 if (sopt->sopt_name != SCTP_AUTOCLOSE) {
1383 /* make it an "on/off" value */
1384 optval = (optval != 0);
1385 }
1386 if (sopt->sopt_size < sizeof(int)) {
1387 error = EINVAL;
1388 }
1389 SCTP_INP_RUNLOCK(inp);
1390 if (error == 0) {
1391 /* return the option value */
1392 ovp = sopt->sopt_data;
1393 *ovp = optval;
1394 sopt->sopt_size = sizeof(optval);
1395 }
1396 break;
1397 case SCTP_GET_ASOC_ID_LIST:
1398 {
1399 struct sctp_assoc_ids *ids;
1400 int cnt, at;
1401 u_int16_t orig;
1402
1403 if (sopt->sopt_size < sizeof(struct sctp_assoc_ids)) {
1404 error = EINVAL;
1405 break;
1406 }
1407 ids = sopt->sopt_data;
1408 cnt = 0;
1409 SCTP_INP_RLOCK(inp);
1410 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1411 if (stcb == NULL) {
1412 none_out_now:
1413 ids->asls_numb_present = 0;
1414 ids->asls_more_to_get = 0;
1415 SCTP_INP_RUNLOCK(inp);
1416 break;
1417 }
1418 orig = ids->asls_assoc_start;
1419 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1420 while( orig ) {
1421 stcb = LIST_NEXT(stcb , sctp_tcblist);
1422 orig--;
1423 cnt--;
1424 }
1425 if ( stcb == NULL)
1426 goto none_out_now;
1427
1428 at = 0;
1429 ids->asls_numb_present = 0;
1430 ids->asls_more_to_get = 1;
1431 while(at < MAX_ASOC_IDS_RET) {
1432 ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1433 at++;
1434 ids->asls_numb_present++;
1435 stcb = LIST_NEXT(stcb , sctp_tcblist);
1436 if (stcb == NULL) {
1437 ids->asls_more_to_get = 0;
1438 break;
1439 }
1440 }
1441 SCTP_INP_RUNLOCK(inp);
1442 }
1443 break;
1444 case SCTP_GET_NONCE_VALUES:
1445 {
1446 struct sctp_get_nonce_values *gnv;
1447 if (sopt->sopt_size < sizeof(struct sctp_get_nonce_values)) {
1448 error = EINVAL;
1449 break;
1450 }
1451 gnv = sopt->sopt_data;
1452 stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
1453 if (stcb == NULL) {
1454 error = ENOTCONN;
1455 } else {
1456 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1457 gnv->gn_local_tag = stcb->asoc.my_vtag;
1458 SCTP_TCB_UNLOCK(stcb);
1459 }
1460
1461 }
1462 break;
1463 case SCTP_PEER_PUBLIC_KEY:
1464 case SCTP_MY_PUBLIC_KEY:
1465 case SCTP_SET_AUTH_CHUNKS:
1466 case SCTP_SET_AUTH_SECRET:
1467 /* not supported yet and until we refine the draft */
1468 error = EOPNOTSUPP;
1469 break;
1470
1471 case SCTP_DELAYED_ACK_TIME:
1472 {
1473 int32_t *tm;
1474 if (sopt->sopt_size < sizeof(int32_t)) {
1475 error = EINVAL;
1476 break;
1477 }
1478 tm = sopt->sopt_data;
1479
1480 *tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1481 }
1482 break;
1483
1484 case SCTP_GET_SNDBUF_USE:
1485 if (sopt->sopt_size < sizeof(struct sctp_sockstat)) {
1486 error = EINVAL;
1487 } else {
1488 struct sctp_sockstat *ss;
1489 struct sctp_association *asoc;
1490 ss = sopt->sopt_data;
1491 stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
1492 if (stcb == NULL) {
1493 error = ENOTCONN;
1494 } else {
1495 asoc = &stcb->asoc;
1496 ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
1497 ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
1498 ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
1499 asoc->size_on_reasm_queue +
1500 asoc->size_on_all_streams);
1501 SCTP_TCB_UNLOCK(stcb);
1502 error = 0;
1503 sopt->sopt_size = sizeof(struct sctp_sockstat);
1504 }
1505 }
1506 break;
1507 case SCTP_MAXBURST:
1508 {
1509 u_int8_t *burst;
1510 burst = sopt->sopt_data;
1511 SCTP_INP_RLOCK(inp);
1512 *burst = inp->sctp_ep.max_burst;
1513 SCTP_INP_RUNLOCK(inp);
1514 sopt->sopt_size = sizeof(u_int8_t);
1515 }
1516 break;
1517 case SCTP_MAXSEG:
1518 {
1519 u_int32_t *segsize;
1520 sctp_assoc_t *assoc_id;
1521 int ovh;
1522
1523 if (sopt->sopt_size < sizeof(u_int32_t)) {
1524 error = EINVAL;
1525 break;
1526 }
1527 if (sopt->sopt_size < sizeof(sctp_assoc_t)) {
1528 error = EINVAL;
1529 break;
1530 }
1531 assoc_id = sopt->sopt_data;
1532 segsize = sopt->sopt_data;
1533 sopt->sopt_size = sizeof(u_int32_t);
1534
1535 if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1536 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
1537 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1538 SCTP_INP_RLOCK(inp);
1539 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1540 if (stcb) {
1541 SCTP_TCB_LOCK(stcb);
1542 SCTP_INP_RUNLOCK(inp);
1543 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1544 SCTP_TCB_UNLOCK(stcb);
1545 } else {
1546 SCTP_INP_RUNLOCK(inp);
1547 goto skipit;
1548 }
1549 } else {
1550 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1551 if (stcb) {
1552 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1553 SCTP_TCB_UNLOCK(stcb);
1554 break;
1555 }
1556 skipit:
1557 /* default is to get the max, if I
1558 * can't calculate from an existing association.
1559 */
1560 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1561 ovh = SCTP_MED_OVERHEAD;
1562 } else {
1563 ovh = SCTP_MED_V4_OVERHEAD;
1564 }
1565 *segsize = inp->sctp_frag_point - ovh;
1566 }
1567 }
1568 break;
1569
1570 case SCTP_SET_DEBUG_LEVEL:
1571 #ifdef SCTP_DEBUG
1572 {
1573 u_int32_t *level;
1574 if (sopt->sopt_size < sizeof(u_int32_t)) {
1575 error = EINVAL;
1576 break;
1577 }
1578 level = sopt->sopt_data;
1579 error = 0;
1580 *level = sctp_debug_on;
1581 sopt->sopt_size = sizeof(u_int32_t);
1582 printf("Returning DEBUG LEVEL %x is set\n",
1583 (u_int)sctp_debug_on);
1584 }
1585 #else /* SCTP_DEBUG */
1586 error = EOPNOTSUPP;
1587 #endif
1588 break;
1589 case SCTP_GET_STAT_LOG:
1590 #ifdef SCTP_STAT_LOGGING
1591 error = sctp_fill_stat_log(m);
1592 #else /* SCTP_DEBUG */
1593 error = EOPNOTSUPP;
1594 #endif
1595 break;
1596 case SCTP_GET_PEGS:
1597 {
1598 u_int32_t *pt;
1599 if (sopt->sopt_size < sizeof(sctp_pegs)) {
1600 error = EINVAL;
1601 break;
1602 }
1603 pt = sopt->sopt_data;
1604 memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
1605 sopt->sopt_size = sizeof(sctp_pegs);
1606 }
1607 break;
1608 case SCTP_EVENTS:
1609 {
1610 struct sctp_event_subscribe *events;
1611 #ifdef SCTP_DEBUG
1612 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1613 printf("get events\n");
1614 }
1615 #endif /* SCTP_DEBUG */
1616 if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) {
1617 #ifdef SCTP_DEBUG
1618 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1619 printf("sopt->sopt_size is %d not %d\n",
1620 (int)sopt->sopt_size,
1621 (int)sizeof(struct sctp_event_subscribe));
1622 }
1623 #endif /* SCTP_DEBUG */
1624 error = EINVAL;
1625 break;
1626 }
1627 events = sopt->sopt_data;
1628 memset(events, 0, sopt->sopt_size);
1629 SCTP_INP_RLOCK(inp);
1630 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
1631 events->sctp_data_io_event = 1;
1632
1633 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
1634 events->sctp_association_event = 1;
1635
1636 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
1637 events->sctp_address_event = 1;
1638
1639 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
1640 events->sctp_send_failure_event = 1;
1641
1642 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
1643 events->sctp_peer_error_event = 1;
1644
1645 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
1646 events->sctp_shutdown_event = 1;
1647
1648 if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
1649 events->sctp_partial_delivery_event = 1;
1650
1651 if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
1652 events->sctp_adaption_layer_event = 1;
1653
1654 if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
1655 events->sctp_stream_reset_events = 1;
1656 SCTP_INP_RUNLOCK(inp);
1657 sopt->sopt_size = sizeof(struct sctp_event_subscribe);
1658
1659 }
1660 break;
1661
1662 case SCTP_ADAPTION_LAYER:
1663 if (sopt->sopt_size < sizeof(int)) {
1664 error = EINVAL;
1665 break;
1666 }
1667 #ifdef SCTP_DEBUG
1668 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1669 printf("getadaption ind\n");
1670 }
1671 #endif /* SCTP_DEBUG */
1672 SCTP_INP_RLOCK(inp);
1673 ovp = sopt->sopt_data;
1674 *ovp = inp->sctp_ep.adaption_layer_indicator;
1675 SCTP_INP_RUNLOCK(inp);
1676 sopt->sopt_size = sizeof(int);
1677 break;
1678 case SCTP_SET_INITIAL_DBG_SEQ:
1679 if (sopt->sopt_size < sizeof(int)) {
1680 error = EINVAL;
1681 break;
1682 }
1683 #ifdef SCTP_DEBUG
1684 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1685 printf("get initial dbg seq\n");
1686 }
1687 #endif /* SCTP_DEBUG */
1688 SCTP_INP_RLOCK(inp);
1689 ovp = sopt->sopt_data;
1690 *ovp = inp->sctp_ep.initial_sequence_debug;
1691 SCTP_INP_RUNLOCK(inp);
1692 sopt->sopt_size = sizeof(int);
1693 break;
1694 case SCTP_GET_LOCAL_ADDR_SIZE:
1695 if (sopt->sopt_size < sizeof(int)) {
1696 error = EINVAL;
1697 break;
1698 }
1699 #ifdef SCTP_DEBUG
1700 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1701 printf("get local sizes\n");
1702 }
1703 #endif /* SCTP_DEBUG */
1704 SCTP_INP_RLOCK(inp);
1705 ovp = sopt->sopt_data;
1706 *ovp = sctp_count_max_addresses(inp);
1707 SCTP_INP_RUNLOCK(inp);
1708 sopt->sopt_size = sizeof(int);
1709 break;
1710 case SCTP_GET_REMOTE_ADDR_SIZE:
1711 {
1712 sctp_assoc_t *assoc_id;
1713 u_int32_t *val, sz;
1714 struct sctp_nets *net;
1715 #ifdef SCTP_DEBUG
1716 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1717 printf("get remote size\n");
1718 }
1719 #endif /* SCTP_DEBUG */
1720 if (sopt->sopt_size < sizeof(sctp_assoc_t)) {
1721 #ifdef SCTP_DEBUG
1722 printf("sopt->sopt_size:%zu not %zu\n",
1723 sopt->sopt_size, sizeof(sctp_assoc_t));
1724 #endif /* SCTP_DEBUG */
1725 error = EINVAL;
1726 break;
1727 }
1728 stcb = NULL;
1729 val = sopt->sopt_data;
1730 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1731 SCTP_INP_RLOCK(inp);
1732 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1733 if (stcb) {
1734 SCTP_TCB_LOCK(stcb);
1735 }
1736 SCTP_INP_RUNLOCK(inp);
1737 }
1738 if (stcb == NULL) {
1739 assoc_id = sopt->sopt_data;
1740 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1741 }
1742
1743 if (stcb == NULL) {
1744 error = EINVAL;
1745 break;
1746 }
1747 *val = 0;
1748 sz = 0;
1749 /* Count the sizes */
1750 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1751 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1752 (rtcache_getdst(&net->ro)->sa_family == AF_INET6)) {
1753 sz += sizeof(struct sockaddr_in6);
1754 } else if (rtcache_getdst(&net->ro)->sa_family == AF_INET) {
1755 sz += sizeof(struct sockaddr_in);
1756 } else {
1757 /* huh */
1758 break;
1759 }
1760 }
1761 SCTP_TCB_UNLOCK(stcb);
1762 *val = sz;
1763 sopt->sopt_size = sizeof(u_int32_t);
1764 }
1765 break;
1766 case SCTP_GET_PEER_ADDRESSES:
1767 /*
1768 * Get the address information, an array
1769 * is passed in to fill up we pack it.
1770 */
1771 {
1772 int cpsz, left;
1773 struct sockaddr_storage *sas;
1774 struct sctp_nets *net;
1775 struct sctp_getaddresses *saddr;
1776 #ifdef SCTP_DEBUG
1777 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1778 printf("get peer addresses\n");
1779 }
1780 #endif /* SCTP_DEBUG */
1781 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
1782 error = EINVAL;
1783 break;
1784 }
1785 left = sopt->sopt_size - sizeof(struct sctp_getaddresses);
1786 saddr = sopt->sopt_data;
1787 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1788 SCTP_INP_RLOCK(inp);
1789 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1790 if (stcb) {
1791 SCTP_TCB_LOCK(stcb);
1792 }
1793 SCTP_INP_RUNLOCK(inp);
1794 } else
1795 stcb = sctp_findassociation_ep_asocid(inp,
1796 saddr->sget_assoc_id);
1797 if (stcb == NULL) {
1798 error = ENOENT;
1799 break;
1800 }
1801 sopt->sopt_size = sizeof(struct sctp_getaddresses);
1802 sas = (struct sockaddr_storage *)&saddr->addr[0];
1803
1804 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1805 sa_family_t family;
1806
1807 family = rtcache_getdst(&net->ro)->sa_family;
1808 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1809 (family == AF_INET6)) {
1810 cpsz = sizeof(struct sockaddr_in6);
1811 } else if (family == AF_INET) {
1812 cpsz = sizeof(struct sockaddr_in);
1813 } else {
1814 /* huh */
1815 break;
1816 }
1817 if (left < cpsz) {
1818 /* not enough room. */
1819 #ifdef SCTP_DEBUG
1820 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1821 printf("Out of room\n");
1822 }
1823 #endif /* SCTP_DEBUG */
1824 break;
1825 }
1826 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1827 (family == AF_INET)) {
1828 /* Must map the address */
1829 in6_sin_2_v4mapsin6((const struct sockaddr_in *) rtcache_getdst(&net->ro),
1830 (struct sockaddr_in6 *)sas);
1831 } else {
1832 memcpy(sas, rtcache_getdst(&net->ro), cpsz);
1833 }
1834 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1835
1836 sas = (struct sockaddr_storage *)((vaddr_t)sas + cpsz);
1837 left -= cpsz;
1838 sopt->sopt_size += cpsz;
1839 #ifdef SCTP_DEBUG
1840 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1841 printf("left now:%d mlen:%zu\n",
1842 left, sopt->sopt_size);
1843 }
1844 #endif /* SCTP_DEBUG */
1845 }
1846 SCTP_TCB_UNLOCK(stcb);
1847 }
1848 #ifdef SCTP_DEBUG
1849 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1850 printf("All done\n");
1851 }
1852 #endif /* SCTP_DEBUG */
1853 break;
1854 case SCTP_GET_LOCAL_ADDRESSES:
1855 {
1856 int limit, actual;
1857 struct sockaddr_storage *sas;
1858 struct sctp_getaddresses *saddr;
1859 #ifdef SCTP_DEBUG
1860 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1861 printf("get local addresses\n");
1862 }
1863 #endif /* SCTP_DEBUG */
1864 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
1865 error = EINVAL;
1866 break;
1867 }
1868 saddr = sopt->sopt_data;
1869
1870 if (saddr->sget_assoc_id) {
1871 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1872 SCTP_INP_RLOCK(inp);
1873 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1874 if (stcb) {
1875 SCTP_TCB_LOCK(stcb);
1876 }
1877 SCTP_INP_RUNLOCK(inp);
1878 } else
1879 stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
1880
1881 } else {
1882 stcb = NULL;
1883 }
1884 /*
1885 * assure that the TCP model does not need a assoc id
1886 * once connected.
1887 */
1888 if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
1889 (stcb == NULL) ) {
1890 SCTP_INP_RLOCK(inp);
1891 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1892 if (stcb) {
1893 SCTP_TCB_LOCK(stcb);
1894 }
1895 SCTP_INP_RUNLOCK(inp);
1896 }
1897 sas = (struct sockaddr_storage *)&saddr->addr[0];
1898 limit = sopt->sopt_size - sizeof(sctp_assoc_t);
1899 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1900 SCTP_TCB_UNLOCK(stcb);
1901 sopt->sopt_size = sizeof(struct sockaddr_storage) + actual;
1902 }
1903 break;
1904 case SCTP_PEER_ADDR_PARAMS:
1905 {
1906 struct sctp_paddrparams *paddrp;
1907 struct sctp_nets *net;
1908
1909 #ifdef SCTP_DEBUG
1910 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1911 printf("Getting peer_addr_params\n");
1912 }
1913 #endif /* SCTP_DEBUG */
1914 if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) {
1915 #ifdef SCTP_DEBUG
1916 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1917 printf("Hmm m->m_len:%zu is to small\n",
1918 sopt->sopt_size);
1919 }
1920 #endif /* SCTP_DEBUG */
1921 error = EINVAL;
1922 break;
1923 }
1924 paddrp = sopt->sopt_data;
1925
1926 net = NULL;
1927 if (paddrp->spp_assoc_id) {
1928 #ifdef SCTP_DEBUG
1929 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1930 printf("In spp_assoc_id find type\n");
1931 }
1932 #endif /* SCTP_DEBUG */
1933 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1934 SCTP_INP_RLOCK(inp);
1935 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1936 if (stcb) {
1937 SCTP_TCB_LOCK(stcb);
1938 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1939 }
1940 SCTP_INP_RLOCK(inp);
1941 } else {
1942 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
1943 }
1944 if (stcb == NULL) {
1945 error = ENOENT;
1946 break;
1947 }
1948 }
1949 if ((stcb == NULL) &&
1950 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
1951 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
1952 /* Lookup via address */
1953 #ifdef SCTP_DEBUG
1954 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1955 printf("Ok we need to lookup a param\n");
1956 }
1957 #endif /* SCTP_DEBUG */
1958 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1959 SCTP_INP_RLOCK(inp);
1960 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1961 if (stcb) {
1962 SCTP_TCB_LOCK(stcb);
1963 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1964 }
1965 SCTP_INP_RUNLOCK(inp);
1966 } else {
1967 SCTP_INP_WLOCK(inp);
1968 SCTP_INP_INCR_REF(inp);
1969 SCTP_INP_WUNLOCK(inp);
1970 stcb = sctp_findassociation_ep_addr(&inp,
1971 (struct sockaddr *)&paddrp->spp_address,
1972 &net, NULL, NULL);
1973 if (stcb == NULL) {
1974 SCTP_INP_WLOCK(inp);
1975 SCTP_INP_DECR_REF(inp);
1976 SCTP_INP_WUNLOCK(inp);
1977 }
1978 }
1979
1980 if (stcb == NULL) {
1981 error = ENOENT;
1982 break;
1983 }
1984 } else {
1985 /* Effects the Endpoint */
1986 #ifdef SCTP_DEBUG
1987 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1988 printf("User wants EP level info\n");
1989 }
1990 #endif /* SCTP_DEBUG */
1991 stcb = NULL;
1992 }
1993 if (stcb) {
1994 /* Applys to the specific association */
1995 #ifdef SCTP_DEBUG
1996 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1997 printf("In TCB side\n");
1998 }
1999 #endif /* SCTP_DEBUG */
2000 if (net) {
2001 paddrp->spp_pathmaxrxt = net->failure_threshold;
2002 } else {
2003 /* No destination so return default value */
2004 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2005 }
2006 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2007 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2008 SCTP_TCB_UNLOCK(stcb);
2009 } else {
2010 /* Use endpoint defaults */
2011 SCTP_INP_RLOCK(inp);
2012 #ifdef SCTP_DEBUG
2013 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2014 printf("In EP level info\n");
2015 }
2016 #endif /* SCTP_DEBUG */
2017 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2018 paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
2019 paddrp->spp_assoc_id = (sctp_assoc_t)0;
2020 SCTP_INP_RUNLOCK(inp);
2021 }
2022 sopt->sopt_size = sizeof(struct sctp_paddrparams);
2023 }
2024 break;
2025 case SCTP_GET_PEER_ADDR_INFO:
2026 {
2027 struct sctp_paddrinfo *paddri;
2028 struct sctp_nets *net;
2029 #ifdef SCTP_DEBUG
2030 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2031 printf("GetPEER ADDR_INFO\n");
2032 }
2033 #endif /* SCTP_DEBUG */
2034 if (sopt->sopt_size < sizeof(struct sctp_paddrinfo)) {
2035 error = EINVAL;
2036 break;
2037 }
2038 paddri = sopt->sopt_data;
2039 net = NULL;
2040 if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
2041 (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
2042 /* Lookup via address */
2043 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2044 SCTP_INP_RLOCK(inp);
2045 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2046 if (stcb) {
2047 SCTP_TCB_LOCK(stcb);
2048 net = sctp_findnet(stcb,
2049 (struct sockaddr *)&paddri->spinfo_address);
2050 }
2051 SCTP_INP_RUNLOCK(inp);
2052 } else {
2053 SCTP_INP_WLOCK(inp);
2054 SCTP_INP_INCR_REF(inp);
2055 SCTP_INP_WUNLOCK(inp);
2056 stcb = sctp_findassociation_ep_addr(&inp,
2057 (struct sockaddr *)&paddri->spinfo_address,
2058 &net, NULL, NULL);
2059 if (stcb == NULL) {
2060 SCTP_INP_WLOCK(inp);
2061 SCTP_INP_DECR_REF(inp);
2062 SCTP_INP_WUNLOCK(inp);
2063 }
2064 }
2065
2066 } else {
2067 stcb = NULL;
2068 }
2069 if ((stcb == NULL) || (net == NULL)) {
2070 error = ENOENT;
2071 break;
2072 }
2073 sopt->sopt_size = sizeof(struct sctp_paddrinfo);
2074 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
2075 paddri->spinfo_cwnd = net->cwnd;
2076 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2077 paddri->spinfo_rto = net->RTO;
2078 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2079 SCTP_TCB_UNLOCK(stcb);
2080 }
2081 break;
2082 case SCTP_PCB_STATUS:
2083 {
2084 struct sctp_pcbinfo *spcb;
2085 #ifdef SCTP_DEBUG
2086 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2087 printf("PCB status\n");
2088 }
2089 #endif /* SCTP_DEBUG */
2090 if (sopt->sopt_size < sizeof(struct sctp_pcbinfo)) {
2091 error = EINVAL;
2092 break;
2093 }
2094 spcb = sopt->sopt_data;
2095 sctp_fill_pcbinfo(spcb);
2096 sopt->sopt_size = sizeof(struct sctp_pcbinfo);
2097 }
2098 break;
2099 case SCTP_STATUS:
2100 {
2101 struct sctp_nets *net;
2102 struct sctp_status *sstat;
2103 #ifdef SCTP_DEBUG
2104 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2105 printf("SCTP status\n");
2106 }
2107 #endif /* SCTP_DEBUG */
2108
2109 if (sopt->sopt_size < sizeof(struct sctp_status)) {
2110 error = EINVAL;
2111 break;
2112 }
2113 sstat = sopt->sopt_data;
2114
2115 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2116 SCTP_INP_RLOCK(inp);
2117 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2118 if (stcb) {
2119 SCTP_TCB_LOCK(stcb);
2120 }
2121 SCTP_INP_RUNLOCK(inp);
2122 } else
2123 stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
2124
2125 if (stcb == NULL) {
2126 printf("SCTP status, no stcb\n");
2127 error = EINVAL;
2128 break;
2129 }
2130 /*
2131 * I think passing the state is fine since
2132 * sctp_constants.h will be available to the user
2133 * land.
2134 */
2135 sstat->sstat_state = stcb->asoc.state;
2136 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2137 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2138 /*
2139 * We can't include chunks that have been passed
2140 * to the socket layer. Only things in queue.
2141 */
2142 sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
2143 stcb->asoc.cnt_on_reasm_queue +
2144 stcb->asoc.cnt_on_all_streams);
2145
2146
2147 sstat->sstat_instrms = stcb->asoc.streamincnt;
2148 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2149 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2150 memcpy(&sstat->sstat_primary.spinfo_address,
2151 rtcache_getdst(&stcb->asoc.primary_destination->ro),
2152 (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len);
2153 net = stcb->asoc.primary_destination;
2154 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2155 /*
2156 * Again the user can get info from sctp_constants.h
2157 * for what the state of the network is.
2158 */
2159 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2160 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2161 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2162 sstat->sstat_primary.spinfo_rto = net->RTO;
2163 sstat->sstat_primary.spinfo_mtu = net->mtu;
2164 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2165 SCTP_TCB_UNLOCK(stcb);
2166 sopt->sopt_size = sizeof(*sstat);
2167 }
2168 break;
2169 case SCTP_RTOINFO:
2170 {
2171 struct sctp_rtoinfo *srto;
2172 #ifdef SCTP_DEBUG
2173 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2174 printf("RTO Info\n");
2175 }
2176 #endif /* SCTP_DEBUG */
2177 if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) {
2178 error = EINVAL;
2179 break;
2180 }
2181 srto = sopt->sopt_data;
2182 if (srto->srto_assoc_id == 0) {
2183 /* Endpoint only please */
2184 SCTP_INP_RLOCK(inp);
2185 srto->srto_initial = inp->sctp_ep.initial_rto;
2186 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2187 srto->srto_min = inp->sctp_ep.sctp_minrto;
2188 SCTP_INP_RUNLOCK(inp);
2189 break;
2190 }
2191 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2192 SCTP_INP_RLOCK(inp);
2193 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2194 if (stcb) {
2195 SCTP_TCB_LOCK(stcb);
2196 }
2197 SCTP_INP_RUNLOCK(inp);
2198 } else
2199 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2200
2201 if (stcb == NULL) {
2202 error = EINVAL;
2203 break;
2204 }
2205 srto->srto_initial = stcb->asoc.initial_rto;
2206 srto->srto_max = stcb->asoc.maxrto;
2207 srto->srto_min = stcb->asoc.minrto;
2208 SCTP_TCB_UNLOCK(stcb);
2209 sopt->sopt_size = sizeof(*srto);
2210 }
2211 break;
2212 case SCTP_ASSOCINFO:
2213 {
2214 struct sctp_assocparams *sasoc;
2215 #ifdef SCTP_DEBUG
2216 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2217 printf("Associnfo\n");
2218 }
2219 #endif /* SCTP_DEBUG */
2220 if (sopt->sopt_size < sizeof(struct sctp_assocparams)) {
2221 error = EINVAL;
2222 break;
2223 }
2224 sasoc = sopt->sopt_data;
2225 stcb = NULL;
2226
2227 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2228 SCTP_INP_RLOCK(inp);
2229 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2230 if (stcb) {
2231 SCTP_TCB_LOCK(stcb);
2232 }
2233 SCTP_INP_RUNLOCK(inp);
2234 }
2235 if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
2236 stcb = sctp_findassociation_ep_asocid(inp,
2237 sasoc->sasoc_assoc_id);
2238 if (stcb == NULL) {
2239 error = ENOENT;
2240 break;
2241 }
2242 } else {
2243 stcb = NULL;
2244 }
2245
2246 if (stcb) {
2247 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2248 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2249 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2250 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2251 sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2252 SCTP_TCB_UNLOCK(stcb);
2253 } else {
2254 SCTP_INP_RLOCK(inp);
2255 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2256 sasoc->sasoc_number_peer_destinations = 0;
2257 sasoc->sasoc_peer_rwnd = 0;
2258 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2259 sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2260 SCTP_INP_RUNLOCK(inp);
2261 }
2262 sopt->sopt_size = sizeof(*sasoc);
2263 }
2264 break;
2265 case SCTP_DEFAULT_SEND_PARAM:
2266 {
2267 struct sctp_sndrcvinfo *s_info;
2268
2269 if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) {
2270 error = EINVAL;
2271 break;
2272 }
2273 s_info = sopt->sopt_data;
2274 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2275 SCTP_INP_RLOCK(inp);
2276 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2277 if (stcb) {
2278 SCTP_TCB_LOCK(stcb);
2279 }
2280 SCTP_INP_RUNLOCK(inp);
2281 } else
2282 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2283
2284 if (stcb == NULL) {
2285 error = ENOENT;
2286 break;
2287 }
2288 /* Copy it out */
2289 *s_info = stcb->asoc.def_send;
2290 SCTP_TCB_UNLOCK(stcb);
2291 sopt->sopt_size = sizeof(*s_info);
2292 }
2293 break;
2294 case SCTP_INITMSG:
2295 {
2296 struct sctp_initmsg *sinit;
2297 #ifdef SCTP_DEBUG
2298 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2299 printf("initmsg\n");
2300 }
2301 #endif /* SCTP_DEBUG */
2302 if (sopt->sopt_size < sizeof(struct sctp_initmsg)) {
2303 error = EINVAL;
2304 break;
2305 }
2306 sinit = sopt->sopt_data;
2307 SCTP_INP_RLOCK(inp);
2308 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2309 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2310 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2311 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2312 SCTP_INP_RUNLOCK(inp);
2313 sopt->sopt_size = sizeof(*sinit);
2314 }
2315 break;
2316 case SCTP_PRIMARY_ADDR:
2317 /* we allow a "get" operation on this */
2318 {
2319 struct sctp_setprim *ssp;
2320
2321 #ifdef SCTP_DEBUG
2322 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2323 printf("setprimary\n");
2324 }
2325 #endif /* SCTP_DEBUG */
2326 if (sopt->sopt_size < sizeof(struct sctp_setprim)) {
2327 error = EINVAL;
2328 break;
2329 }
2330 ssp = sopt->sopt_data;
2331 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2332 SCTP_INP_RLOCK(inp);
2333 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2334 if (stcb) {
2335 SCTP_TCB_LOCK(stcb);
2336 }
2337 SCTP_INP_RUNLOCK(inp);
2338 } else {
2339 stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
2340 if (stcb == NULL) {
2341 /* one last shot, try it by the address in */
2342 struct sctp_nets *net;
2343
2344 SCTP_INP_WLOCK(inp);
2345 SCTP_INP_INCR_REF(inp);
2346 SCTP_INP_WUNLOCK(inp);
2347 stcb = sctp_findassociation_ep_addr(&inp,
2348 (struct sockaddr *)&ssp->ssp_addr,
2349 &net, NULL, NULL);
2350 if (stcb == NULL) {
2351 SCTP_INP_WLOCK(inp);
2352 SCTP_INP_DECR_REF(inp);
2353 SCTP_INP_WUNLOCK(inp);
2354 }
2355 }
2356 if (stcb == NULL) {
2357 error = EINVAL;
2358 break;
2359 }
2360 }
2361 /* simply copy out the sockaddr_storage... */
2362 memcpy(&ssp->ssp_addr,
2363 rtcache_getdst(&stcb->asoc.primary_destination->ro),
2364 (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len);
2365 SCTP_TCB_UNLOCK(stcb);
2366 sopt->sopt_size = sizeof(*ssp);
2367 }
2368 break;
2369 default:
2370 error = ENOPROTOOPT;
2371 sopt->sopt_size = 0;
2372 break;
2373 } /* end switch (sopt->sopt_name) */
2374 return (error);
2375 }
2376
2377 static int
sctp_optsset(struct socket * so,struct sockopt * sopt)2378 sctp_optsset(struct socket *so, struct sockopt *sopt)
2379 {
2380 int error, *mopt, set_opt;
2381 struct sctp_tcb *stcb = NULL;
2382 struct sctp_inpcb *inp;
2383
2384 if (sopt->sopt_data == NULL) {
2385 #ifdef SCTP_DEBUG
2386 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2387 printf("optsset:MP is NULL EINVAL\n");
2388 }
2389 #endif /* SCTP_DEBUG */
2390 return (EINVAL);
2391 }
2392 inp = (struct sctp_inpcb *)so->so_pcb;
2393 if (inp == 0)
2394 return EINVAL;
2395
2396 error = 0;
2397 switch (sopt->sopt_name) {
2398 case SCTP_NODELAY:
2399 case SCTP_AUTOCLOSE:
2400 case SCTP_AUTO_ASCONF:
2401 case SCTP_DISABLE_FRAGMENTS:
2402 case SCTP_I_WANT_MAPPED_V4_ADDR:
2403 /* copy in the option value */
2404 if (sopt->sopt_size < sizeof(int)) {
2405 error = EINVAL;
2406 break;
2407 }
2408 mopt = sopt->sopt_data;
2409 set_opt = 0;
2410 if (error)
2411 break;
2412 switch (sopt->sopt_name) {
2413 case SCTP_DISABLE_FRAGMENTS:
2414 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2415 break;
2416 case SCTP_AUTO_ASCONF:
2417 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2418 break;
2419
2420 case SCTP_I_WANT_MAPPED_V4_ADDR:
2421 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2422 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2423 } else {
2424 return (EINVAL);
2425 }
2426 break;
2427 case SCTP_NODELAY:
2428 set_opt = SCTP_PCB_FLAGS_NODELAY;
2429 break;
2430 case SCTP_AUTOCLOSE:
2431 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2432 /*
2433 * The value is in ticks.
2434 * Note this does not affect old associations, only
2435 * new ones.
2436 */
2437 inp->sctp_ep.auto_close_time = (*mopt * hz);
2438 break;
2439 }
2440 SCTP_INP_WLOCK(inp);
2441 if (*mopt != 0) {
2442 inp->sctp_flags |= set_opt;
2443 } else {
2444 inp->sctp_flags &= ~set_opt;
2445 }
2446 SCTP_INP_WUNLOCK(inp);
2447 break;
2448 case SCTP_MY_PUBLIC_KEY: /* set my public key */
2449 case SCTP_SET_AUTH_CHUNKS: /* set the authenticated chunks required */
2450 case SCTP_SET_AUTH_SECRET: /* set the actual secret for the endpoint */
2451 /* not supported yet and until we refine the draft */
2452 error = EOPNOTSUPP;
2453 break;
2454
2455 case SCTP_CLR_STAT_LOG:
2456 #ifdef SCTP_STAT_LOGGING
2457 sctp_clr_stat_log();
2458 #else
2459 error = EOPNOTSUPP;
2460 #endif
2461 break;
2462 case SCTP_DELAYED_ACK_TIME:
2463 {
2464 int32_t *tm;
2465 if (sopt->sopt_size < sizeof(int32_t)) {
2466 error = EINVAL;
2467 break;
2468 }
2469 tm = sopt->sopt_data;
2470
2471 if ((*tm < 10) || (*tm > 500)) {
2472 /* can't be smaller than 10ms */
2473 /* MUST NOT be larger than 500ms */
2474 error = EINVAL;
2475 break;
2476 }
2477 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
2478 }
2479 break;
2480 case SCTP_RESET_STREAMS:
2481 {
2482 struct sctp_stream_reset *strrst;
2483 uint8_t two_way, not_peer;
2484
2485 if (sopt->sopt_size < sizeof(struct sctp_stream_reset)) {
2486 error = EINVAL;
2487 break;
2488 }
2489 strrst = sopt->sopt_data;
2490
2491 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2492 SCTP_INP_RLOCK(inp);
2493 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2494 if (stcb) {
2495 SCTP_TCB_LOCK(stcb);
2496 }
2497 SCTP_INP_RUNLOCK(inp);
2498 } else
2499 stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
2500 if (stcb == NULL) {
2501 error = ENOENT;
2502 break;
2503 }
2504 if (stcb->asoc.peer_supports_strreset == 0) {
2505 /* Peer does not support it,
2506 * we return protocol not supported since
2507 * this is true for this feature and this
2508 * peer, not the socket request in general.
2509 */
2510 error = EPROTONOSUPPORT;
2511 SCTP_TCB_UNLOCK(stcb);
2512 break;
2513 }
2514
2515 /* Having re-thought this code I added as I write the I-D there
2516 * is NO need for it. The peer, if we are requesting a stream-reset
2517 * will send a request to us but will itself do what we do, take
2518 * and copy off the "reset information" we send and queue TSN's
2519 * larger than the send-next in our response message. Thus they
2520 * will handle it.
2521 */
2522 /* if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
2523 /* Must have all sending data ack'd before we
2524 * start this procedure. This is a bit restrictive
2525 * and we SHOULD work on changing this so ONLY the
2526 * streams being RESET get held up. So, a reset-all
2527 * would require this.. but a reset specific just
2528 * needs to be sure that the ones being reset have
2529 * nothing on the send_queue. For now we will
2530 * skip this more detailed method and do a course
2531 * way.. i.e. nothing pending ... for future FIX ME!
2532 */
2533 /* error = EBUSY;*/
2534 /* break;*/
2535 /* }*/
2536
2537 if (stcb->asoc.stream_reset_outstanding) {
2538 error = EALREADY;
2539 SCTP_TCB_UNLOCK(stcb);
2540 break;
2541 }
2542 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2543 two_way = 0;
2544 not_peer = 0;
2545 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2546 two_way = 1;
2547 not_peer = 1;
2548 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2549 two_way = 1;
2550 not_peer = 0;
2551 } else {
2552 error = EINVAL;
2553 SCTP_TCB_UNLOCK(stcb);
2554 break;
2555 }
2556 sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2557 strrst->strrst_list, two_way, not_peer);
2558 sctp_chunk_output(inp, stcb, 12);
2559 SCTP_TCB_UNLOCK(stcb);
2560
2561 }
2562 break;
2563 case SCTP_RESET_PEGS:
2564 memset(sctp_pegs, 0, sizeof(sctp_pegs));
2565 error = 0;
2566 break;
2567 case SCTP_CONNECT_X_COMPLETE:
2568 {
2569 struct sockaddr *sa;
2570 struct sctp_nets *net;
2571 if (sopt->sopt_size < sizeof(struct sockaddr_in)) {
2572 error = EINVAL;
2573 break;
2574 }
2575 sa = sopt->sopt_data;
2576 /* find tcb */
2577 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2578 SCTP_INP_RLOCK(inp);
2579 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2580 if (stcb) {
2581 SCTP_TCB_LOCK(stcb);
2582 net = sctp_findnet(stcb, sa);
2583 }
2584 SCTP_INP_RUNLOCK(inp);
2585 } else {
2586 SCTP_INP_WLOCK(inp);
2587 SCTP_INP_INCR_REF(inp);
2588 SCTP_INP_WUNLOCK(inp);
2589 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2590 if (stcb == NULL) {
2591 SCTP_INP_WLOCK(inp);
2592 SCTP_INP_DECR_REF(inp);
2593 SCTP_INP_WUNLOCK(inp);
2594 }
2595 }
2596
2597 if (stcb == NULL) {
2598 error = ENOENT;
2599 break;
2600 }
2601 if (stcb->asoc.delayed_connection == 1) {
2602 stcb->asoc.delayed_connection = 0;
2603 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2604 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2605 sctp_send_initiate(inp, stcb);
2606 } else {
2607 /* already expired or did not use delayed connectx */
2608 error = EALREADY;
2609 }
2610 SCTP_TCB_UNLOCK(stcb);
2611 }
2612 break;
2613 case SCTP_MAXBURST:
2614 {
2615 u_int8_t *burst;
2616 SCTP_INP_WLOCK(inp);
2617 burst = sopt->sopt_data;
2618 if (*burst) {
2619 inp->sctp_ep.max_burst = *burst;
2620 }
2621 SCTP_INP_WUNLOCK(inp);
2622 }
2623 break;
2624 case SCTP_MAXSEG:
2625 {
2626 u_int32_t *segsize;
2627 int ovh;
2628 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2629 ovh = SCTP_MED_OVERHEAD;
2630 } else {
2631 ovh = SCTP_MED_V4_OVERHEAD;
2632 }
2633 segsize = sopt->sopt_data;
2634 if (*segsize < 1) {
2635 error = EINVAL;
2636 break;
2637 }
2638 SCTP_INP_WLOCK(inp);
2639 inp->sctp_frag_point = (*segsize+ovh);
2640 if (inp->sctp_frag_point < MHLEN) {
2641 inp->sctp_frag_point = MHLEN;
2642 }
2643 SCTP_INP_WUNLOCK(inp);
2644 }
2645 break;
2646 case SCTP_SET_DEBUG_LEVEL:
2647 #ifdef SCTP_DEBUG
2648 {
2649 u_int32_t *level;
2650 if (sopt->sopt_size < sizeof(u_int32_t)) {
2651 error = EINVAL;
2652 break;
2653 }
2654 level = sopt->sopt_data;
2655 error = 0;
2656 sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
2657 SCTP_DEBUG_NOISY));
2658 printf("SETTING DEBUG LEVEL to %x\n",
2659 (u_int)sctp_debug_on);
2660
2661 }
2662 #else
2663 error = EOPNOTSUPP;
2664 #endif /* SCTP_DEBUG */
2665 break;
2666 case SCTP_EVENTS:
2667 {
2668 struct sctp_event_subscribe *events;
2669 if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) {
2670 error = EINVAL;
2671 break;
2672 }
2673 SCTP_INP_WLOCK(inp);
2674 events = sopt->sopt_data;
2675 if (events->sctp_data_io_event) {
2676 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2677 } else {
2678 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2679 }
2680
2681 if (events->sctp_association_event) {
2682 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
2683 } else {
2684 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
2685 }
2686
2687 if (events->sctp_address_event) {
2688 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
2689 } else {
2690 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
2691 }
2692
2693 if (events->sctp_send_failure_event) {
2694 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2695 } else {
2696 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2697 }
2698
2699 if (events->sctp_peer_error_event) {
2700 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
2701 } else {
2702 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
2703 }
2704
2705 if (events->sctp_shutdown_event) {
2706 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
2707 } else {
2708 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
2709 }
2710
2711 if (events->sctp_partial_delivery_event) {
2712 inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
2713 } else {
2714 inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
2715 }
2716
2717 if (events->sctp_adaption_layer_event) {
2718 inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
2719 } else {
2720 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
2721 }
2722
2723 if (events->sctp_stream_reset_events) {
2724 inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
2725 } else {
2726 inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
2727 }
2728 SCTP_INP_WUNLOCK(inp);
2729 }
2730 break;
2731
2732 case SCTP_ADAPTION_LAYER:
2733 {
2734 struct sctp_setadaption *adap_bits;
2735 if (sopt->sopt_size < sizeof(struct sctp_setadaption)) {
2736 error = EINVAL;
2737 break;
2738 }
2739 SCTP_INP_WLOCK(inp);
2740 adap_bits = sopt->sopt_data;
2741 inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
2742 SCTP_INP_WUNLOCK(inp);
2743 }
2744 break;
2745 case SCTP_SET_INITIAL_DBG_SEQ:
2746 {
2747 u_int32_t *vvv;
2748 if (sopt->sopt_size < sizeof(u_int32_t)) {
2749 error = EINVAL;
2750 break;
2751 }
2752 SCTP_INP_WLOCK(inp);
2753 vvv = sopt->sopt_data;
2754 inp->sctp_ep.initial_sequence_debug = *vvv;
2755 SCTP_INP_WUNLOCK(inp);
2756 }
2757 break;
2758 case SCTP_DEFAULT_SEND_PARAM:
2759 {
2760 struct sctp_sndrcvinfo *s_info;
2761
2762 if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) {
2763 error = EINVAL;
2764 break;
2765 }
2766 s_info = sopt->sopt_data;
2767
2768 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2769 SCTP_INP_RLOCK(inp);
2770 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2771 if (stcb) {
2772 SCTP_TCB_LOCK(stcb);
2773 }
2774 SCTP_INP_RUNLOCK(inp);
2775 } else
2776 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2777
2778 if (stcb == NULL) {
2779 error = ENOENT;
2780 break;
2781 }
2782 /* Validate things */
2783 if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
2784 SCTP_TCB_UNLOCK(stcb);
2785 error = EINVAL;
2786 break;
2787 }
2788 /* Mask off the flags that are allowed */
2789 s_info->sinfo_flags = (s_info->sinfo_flags &
2790 (SCTP_UNORDERED | SCTP_ADDR_OVER |
2791 SCTP_PR_SCTP_TTL | SCTP_PR_SCTP_BUF));
2792 /* Copy it in */
2793 stcb->asoc.def_send = *s_info;
2794 SCTP_TCB_UNLOCK(stcb);
2795 }
2796 break;
2797 case SCTP_PEER_ADDR_PARAMS:
2798 {
2799 struct sctp_paddrparams *paddrp;
2800 struct sctp_nets *net;
2801 if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) {
2802 error = EINVAL;
2803 break;
2804 }
2805 paddrp = sopt->sopt_data;
2806 net = NULL;
2807 if (paddrp->spp_assoc_id) {
2808 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2809 SCTP_INP_RLOCK(inp);
2810 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2811 if (stcb) {
2812 SCTP_TCB_LOCK(stcb);
2813 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2814 }
2815 SCTP_INP_RUNLOCK(inp);
2816 } else
2817 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
2818 if (stcb == NULL) {
2819 error = ENOENT;
2820 break;
2821 }
2822
2823 }
2824 if ((stcb == NULL) &&
2825 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
2826 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
2827 /* Lookup via address */
2828 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2829 SCTP_INP_RLOCK(inp);
2830 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2831 if (stcb) {
2832 SCTP_TCB_LOCK(stcb);
2833 net = sctp_findnet(stcb,
2834 (struct sockaddr *)&paddrp->spp_address);
2835 }
2836 SCTP_INP_RUNLOCK(inp);
2837 } else {
2838 SCTP_INP_WLOCK(inp);
2839 SCTP_INP_INCR_REF(inp);
2840 SCTP_INP_WUNLOCK(inp);
2841 stcb = sctp_findassociation_ep_addr(&inp,
2842 (struct sockaddr *)&paddrp->spp_address,
2843 &net, NULL, NULL);
2844 if (stcb == NULL) {
2845 SCTP_INP_WLOCK(inp);
2846 SCTP_INP_DECR_REF(inp);
2847 SCTP_INP_WUNLOCK(inp);
2848 }
2849 }
2850 } else {
2851 /* Effects the Endpoint */
2852 stcb = NULL;
2853 }
2854 if (stcb) {
2855 /* Applies to the specific association */
2856 if (paddrp->spp_pathmaxrxt) {
2857 if (net) {
2858 if (paddrp->spp_pathmaxrxt)
2859 net->failure_threshold = paddrp->spp_pathmaxrxt;
2860 } else {
2861 if (paddrp->spp_pathmaxrxt)
2862 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
2863 }
2864 }
2865 if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
2866 /* Just a set */
2867 int old;
2868 if (net) {
2869 net->dest_state &= ~SCTP_ADDR_NOHB;
2870 } else {
2871 old = stcb->asoc.heart_beat_delay;
2872 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
2873 if (old == 0) {
2874 /* Turn back on the timer */
2875 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
2876 }
2877 }
2878 } else if (paddrp->spp_hbinterval == 0xffffffff) {
2879 /* on demand HB */
2880 sctp_send_hb(stcb, 1, net);
2881 } else {
2882 if (net == NULL) {
2883 /* off on association */
2884 if (stcb->asoc.heart_beat_delay) {
2885 int cnt_of_unconf = 0;
2886 struct sctp_nets *lnet;
2887 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2888 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
2889 cnt_of_unconf++;
2890 }
2891 }
2892 /* stop the timer ONLY if we have no unconfirmed addresses
2893 */
2894 if (cnt_of_unconf == 0)
2895 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
2896 }
2897 stcb->asoc.heart_beat_delay = 0;
2898 } else {
2899 net->dest_state |= SCTP_ADDR_NOHB;
2900 }
2901 }
2902 SCTP_TCB_UNLOCK(stcb);
2903 } else {
2904 /* Use endpoint defaults */
2905 SCTP_INP_WLOCK(inp);
2906 if (paddrp->spp_pathmaxrxt)
2907 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
2908 if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
2909 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
2910 SCTP_INP_WUNLOCK(inp);
2911 }
2912 }
2913 break;
2914 case SCTP_RTOINFO:
2915 {
2916 struct sctp_rtoinfo *srto;
2917 if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) {
2918 error = EINVAL;
2919 break;
2920 }
2921 srto = sopt->sopt_data;
2922 if (srto->srto_assoc_id == 0) {
2923 SCTP_INP_WLOCK(inp);
2924 /* If we have a null asoc, its default for the endpoint */
2925 if (srto->srto_initial > 10)
2926 inp->sctp_ep.initial_rto = srto->srto_initial;
2927 if (srto->srto_max > 10)
2928 inp->sctp_ep.sctp_maxrto = srto->srto_max;
2929 if (srto->srto_min > 10)
2930 inp->sctp_ep.sctp_minrto = srto->srto_min;
2931 SCTP_INP_WUNLOCK(inp);
2932 break;
2933 }
2934 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2935 SCTP_INP_RLOCK(inp);
2936 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2937 if (stcb) {
2938 SCTP_TCB_LOCK(stcb);
2939 }
2940 SCTP_INP_RUNLOCK(inp);
2941 } else
2942 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2943 if (stcb == NULL) {
2944 error = EINVAL;
2945 break;
2946 }
2947 /* Set in ms we hope :-) */
2948 if (srto->srto_initial > 10)
2949 stcb->asoc.initial_rto = srto->srto_initial;
2950 if (srto->srto_max > 10)
2951 stcb->asoc.maxrto = srto->srto_max;
2952 if (srto->srto_min > 10)
2953 stcb->asoc.minrto = srto->srto_min;
2954 SCTP_TCB_UNLOCK(stcb);
2955 }
2956 break;
2957 case SCTP_ASSOCINFO:
2958 {
2959 struct sctp_assocparams *sasoc;
2960
2961 if (sopt->sopt_size < sizeof(struct sctp_assocparams)) {
2962 error = EINVAL;
2963 break;
2964 }
2965 sasoc = sopt->sopt_data;
2966 if (sasoc->sasoc_assoc_id) {
2967 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2968 SCTP_INP_RLOCK(inp);
2969 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2970 if (stcb) {
2971 SCTP_TCB_LOCK(stcb);
2972 }
2973 SCTP_INP_RUNLOCK(inp);
2974 } else
2975 stcb = sctp_findassociation_ep_asocid(inp,
2976 sasoc->sasoc_assoc_id);
2977 if (stcb == NULL) {
2978 error = ENOENT;
2979 break;
2980 }
2981
2982 } else {
2983 stcb = NULL;
2984 }
2985 if (stcb) {
2986 if (sasoc->sasoc_asocmaxrxt)
2987 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
2988 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2989 sasoc->sasoc_peer_rwnd = 0;
2990 sasoc->sasoc_local_rwnd = 0;
2991 if (stcb->asoc.cookie_life)
2992 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
2993 SCTP_TCB_UNLOCK(stcb);
2994 } else {
2995 SCTP_INP_WLOCK(inp);
2996 if (sasoc->sasoc_asocmaxrxt)
2997 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
2998 sasoc->sasoc_number_peer_destinations = 0;
2999 sasoc->sasoc_peer_rwnd = 0;
3000 sasoc->sasoc_local_rwnd = 0;
3001 if (sasoc->sasoc_cookie_life)
3002 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3003 SCTP_INP_WUNLOCK(inp);
3004 }
3005 }
3006 break;
3007 case SCTP_INITMSG:
3008 {
3009 struct sctp_initmsg *sinit;
3010
3011 if (sopt->sopt_size < sizeof(struct sctp_initmsg)) {
3012 error = EINVAL;
3013 break;
3014 }
3015 sinit = sopt->sopt_data;
3016 SCTP_INP_WLOCK(inp);
3017 if (sinit->sinit_num_ostreams)
3018 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3019
3020 if (sinit->sinit_max_instreams)
3021 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3022
3023 if (sinit->sinit_max_attempts)
3024 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3025
3026 if (sinit->sinit_max_init_timeo > 10)
3027 /* We must be at least a 100ms (we set in ticks) */
3028 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3029 SCTP_INP_WUNLOCK(inp);
3030 }
3031 break;
3032 case SCTP_PRIMARY_ADDR:
3033 {
3034 struct sctp_setprim *spa;
3035 struct sctp_nets *net, *lnet;
3036 if (sopt->sopt_size < sizeof(struct sctp_setprim)) {
3037 error = EINVAL;
3038 break;
3039 }
3040 spa = sopt->sopt_data;
3041
3042 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3043 SCTP_INP_RLOCK(inp);
3044 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3045 if (stcb) {
3046 SCTP_TCB_LOCK(stcb);
3047 } else {
3048 error = EINVAL;
3049 break;
3050 }
3051 SCTP_INP_RUNLOCK(inp);
3052 } else
3053 stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
3054 if (stcb == NULL) {
3055 /* One last shot */
3056 SCTP_INP_WLOCK(inp);
3057 SCTP_INP_INCR_REF(inp);
3058 SCTP_INP_WUNLOCK(inp);
3059 stcb = sctp_findassociation_ep_addr(&inp,
3060 (struct sockaddr *)&spa->ssp_addr,
3061 &net, NULL, NULL);
3062 if (stcb == NULL) {
3063 SCTP_INP_WLOCK(inp);
3064 SCTP_INP_DECR_REF(inp);
3065 SCTP_INP_WUNLOCK(inp);
3066 error = EINVAL;
3067 break;
3068 }
3069 } else {
3070 /* find the net, associd or connected lookup type */
3071 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3072 if (net == NULL) {
3073 SCTP_TCB_UNLOCK(stcb);
3074 error = EINVAL;
3075 break;
3076 }
3077 }
3078 if ((net != stcb->asoc.primary_destination) &&
3079 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3080 /* Ok we need to set it */
3081 lnet = stcb->asoc.primary_destination;
3082 lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
3083 if (sctp_set_primary_addr(stcb,
3084 (struct sockaddr *)NULL,
3085 net) == 0) {
3086 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3087 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3088 }
3089 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3090 }
3091 }
3092 SCTP_TCB_UNLOCK(stcb);
3093 }
3094 break;
3095
3096 case SCTP_SET_PEER_PRIMARY_ADDR:
3097 {
3098 struct sctp_setpeerprim *sspp;
3099 if (sopt->sopt_size < sizeof(struct sctp_setpeerprim)) {
3100 error = EINVAL;
3101 break;
3102 }
3103 sspp = sopt->sopt_data;
3104
3105 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3106 SCTP_INP_RLOCK(inp);
3107 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3108 if (stcb) {
3109 SCTP_TCB_UNLOCK(stcb);
3110 }
3111 SCTP_INP_RUNLOCK(inp);
3112 } else
3113 stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
3114 if (stcb == NULL) {
3115 error = EINVAL;
3116 break;
3117 }
3118 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3119 error = EINVAL;
3120 }
3121 SCTP_TCB_UNLOCK(stcb);
3122 }
3123 break;
3124 case SCTP_BINDX_ADD_ADDR:
3125 {
3126 struct sctp_getaddresses *addrs;
3127 struct sockaddr *addr_touse;
3128 struct sockaddr_in sin;
3129 /* see if we're bound all already! */
3130 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3131 error = EINVAL;
3132 break;
3133 }
3134 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
3135 error = EINVAL;
3136 break;
3137 }
3138 addrs = sopt->sopt_data;
3139 addr_touse = addrs->addr;
3140 if (addrs->addr->sa_family == AF_INET6) {
3141 struct sockaddr_in6 *sin6;
3142 sin6 = (struct sockaddr_in6 *)addr_touse;
3143 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3144 in6_sin6_2_sin(&sin, sin6);
3145 addr_touse = (struct sockaddr *)&sin;
3146 }
3147 }
3148 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3149 error = sctp_inpcb_bind(so, addr_touse, curlwp);
3150 break;
3151 }
3152 /* No locks required here since bind and mgmt_ep_sa all
3153 * do their own locking. If we do something for the FIX:
3154 * below we may need to lock in that case.
3155 */
3156 if (addrs->sget_assoc_id == 0) {
3157 /* add the address */
3158 struct sctp_inpcb *lep;
3159 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3160 lep = sctp_pcb_findep(addr_touse, 1, 0);
3161 if (lep != NULL) {
3162 /* We must decrement the refcount
3163 * since we have the ep already and
3164 * are binding. No remove going on
3165 * here.
3166 */
3167 SCTP_INP_WLOCK(inp);
3168 SCTP_INP_DECR_REF(inp);
3169 SCTP_INP_WUNLOCK(inp);
3170 }
3171 if (lep == inp) {
3172 /* already bound to it.. ok */
3173 break;
3174 } else if (lep == NULL) {
3175 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3176 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3177 SCTP_ADD_IP_ADDRESS);
3178 } else {
3179 error = EADDRNOTAVAIL;
3180 }
3181 if (error)
3182 break;
3183
3184 } else {
3185 /* FIX: decide whether we allow assoc based bindx */
3186 }
3187 }
3188 break;
3189 case SCTP_BINDX_REM_ADDR:
3190 {
3191 struct sctp_getaddresses *addrs;
3192 struct sockaddr *addr_touse;
3193 struct sockaddr_in sin;
3194 /* see if we're bound all already! */
3195 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3196 error = EINVAL;
3197 break;
3198 }
3199 if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
3200 error = EINVAL;
3201 break;
3202 }
3203 addrs = sopt->sopt_data;
3204 addr_touse = addrs->addr;
3205 if (addrs->addr->sa_family == AF_INET6) {
3206 struct sockaddr_in6 *sin6;
3207 sin6 = (struct sockaddr_in6 *)addr_touse;
3208 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3209 in6_sin6_2_sin(&sin, sin6);
3210 addr_touse = (struct sockaddr *)&sin;
3211 }
3212 }
3213 /* No lock required mgmt_ep_sa does its own locking. If
3214 * the FIX: below is ever changed we may need to
3215 * lock before calling association level binding.
3216 */
3217 if (addrs->sget_assoc_id == 0) {
3218 /* delete the address */
3219 sctp_addr_mgmt_ep_sa(inp, addr_touse,
3220 SCTP_DEL_IP_ADDRESS);
3221 } else {
3222 /* FIX: decide whether we allow assoc based bindx */
3223 }
3224 }
3225 break;
3226 default:
3227 error = ENOPROTOOPT;
3228 break;
3229 } /* end switch (opt) */
3230 return (error);
3231 }
3232
3233 int
sctp_ctloutput(int op,struct socket * so,struct sockopt * sopt)3234 sctp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
3235 {
3236 int s, error = 0;
3237 struct inpcb *inp;
3238 #ifdef INET6
3239 struct in6pcb *in6p;
3240 #endif
3241 int family; /* family of the socket */
3242
3243 family = so->so_proto->pr_domain->dom_family;
3244
3245 s = splsoftnet();
3246 switch (family) {
3247 case PF_INET:
3248 inp = sotoinpcb(so);
3249 #ifdef INET6
3250 in6p = NULL;
3251 #endif
3252 break;
3253 #ifdef INET6
3254 case PF_INET6:
3255 inp = NULL;
3256 in6p = sotoin6pcb(so);
3257 break;
3258 #endif
3259 default:
3260 splx(s);
3261 return EAFNOSUPPORT;
3262 }
3263 #ifndef INET6
3264 if (inp == NULL)
3265 #else
3266 if (inp == NULL && in6p == NULL)
3267 #endif
3268 {
3269 splx(s);
3270 return (ECONNRESET);
3271 }
3272 if (sopt->sopt_level != IPPROTO_SCTP) {
3273 switch (family) {
3274 case PF_INET:
3275 error = ip_ctloutput(op, so, sopt);
3276 break;
3277 #ifdef INET6
3278 case PF_INET6:
3279 error = ip6_ctloutput(op, so, sopt);
3280 break;
3281 #endif
3282 }
3283 splx(s);
3284 return (error);
3285 }
3286 /* Ok if we reach here it is a SCTP option we hope */
3287 if (op == PRCO_SETOPT) {
3288 error = sctp_optsset(so, sopt);
3289 } else if (op == PRCO_GETOPT) {
3290 error = sctp_optsget(so, sopt);
3291 } else {
3292 error = EINVAL;
3293 }
3294 splx(s);
3295 return (error);
3296 }
3297
3298 static int
sctp_connect(struct socket * so,struct sockaddr * nam,struct lwp * l)3299 sctp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
3300 {
3301 int error = 0;
3302 struct sctp_inpcb *inp;
3303 struct sctp_tcb *stcb;
3304
3305 KASSERT(solocked(so));
3306 #ifdef SCTP_DEBUG
3307 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3308 printf("Connect called in SCTP to ");
3309 sctp_print_address(nam);
3310 printf("Port %d\n", ntohs(((struct sockaddr_in *)nam)->sin_port));
3311 }
3312 #endif /* SCTP_DEBUG */
3313 inp = (struct sctp_inpcb *)so->so_pcb;
3314 if (inp == 0) {
3315 /* I made the same as TCP since we are not setup? */
3316 return (ECONNRESET);
3317 }
3318 SCTP_ASOC_CREATE_LOCK(inp);
3319 #ifdef SCTP_DEBUG
3320 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3321 printf("After ASOC lock\n");
3322 }
3323 #endif /* SCTP_DEBUG */
3324 SCTP_INP_WLOCK(inp);
3325 #ifdef SCTP_DEBUG
3326 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3327 printf("After INP_WLOCK lock\n");
3328 }
3329 #endif /* SCTP_DEBUG */
3330 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3331 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3332 /* Should I really unlock ? */
3333 SCTP_INP_WUNLOCK(inp);
3334 SCTP_ASOC_CREATE_UNLOCK(inp);
3335 return (EFAULT);
3336 }
3337 #ifdef INET6
3338 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3339 (nam->sa_family == AF_INET6)) {
3340 SCTP_INP_WUNLOCK(inp);
3341 SCTP_ASOC_CREATE_UNLOCK(inp);
3342 return (EINVAL);
3343 }
3344 #endif /* INET6 */
3345
3346 /*
3347 * XXX XXX XXX Check nam->sa_len?
3348 */
3349
3350 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3351 SCTP_PCB_FLAGS_UNBOUND) {
3352 /* Bind a ephemeral port */
3353 SCTP_INP_WUNLOCK(inp);
3354 error = sctp_inpcb_bind(so, NULL, l);
3355 if (error) {
3356 SCTP_ASOC_CREATE_UNLOCK(inp);
3357 return (error);
3358 }
3359 SCTP_INP_WLOCK(inp);
3360 }
3361 #ifdef SCTP_DEBUG
3362 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3363 printf("After bind\n");
3364 }
3365 #endif /* SCTP_DEBUG */
3366 /* Now do we connect? */
3367 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3368 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3369 /* We are already connected AND the TCP model */
3370 SCTP_INP_WUNLOCK(inp);
3371 SCTP_ASOC_CREATE_UNLOCK(inp);
3372 return (EADDRINUSE);
3373 }
3374 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3375 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3376 if (stcb) {
3377 SCTP_TCB_UNLOCK(stcb);
3378 }
3379 SCTP_INP_WUNLOCK(inp);
3380 } else {
3381 SCTP_INP_INCR_REF(inp);
3382 SCTP_INP_WUNLOCK(inp);
3383 stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
3384 if (stcb == NULL) {
3385 SCTP_INP_WLOCK(inp);
3386 SCTP_INP_DECR_REF(inp);
3387 SCTP_INP_WUNLOCK(inp);
3388 }
3389 }
3390 if (stcb != NULL) {
3391 /* Already have or am bring up an association */
3392 SCTP_ASOC_CREATE_UNLOCK(inp);
3393 SCTP_TCB_UNLOCK(stcb);
3394 return (EALREADY);
3395 }
3396 /* We are GOOD to go */
3397 stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
3398 if (stcb == NULL) {
3399 /* Gak! no memory */
3400 return (error);
3401 }
3402 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3403 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3404 /* Set the connected flag so we can queue data */
3405 soisconnecting(so);
3406 }
3407 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3408 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3409 sctp_send_initiate(inp, stcb);
3410 SCTP_ASOC_CREATE_UNLOCK(inp);
3411 SCTP_TCB_UNLOCK(stcb);
3412 return error;
3413 }
3414
3415 static int
sctp_connect2(struct socket * so,struct socket * so2)3416 sctp_connect2(struct socket *so, struct socket *so2)
3417 {
3418 KASSERT(solocked(so));
3419
3420 return EOPNOTSUPP;
3421 }
3422
3423 int
sctp_rcvd(struct socket * so,int flags,struct lwp * l)3424 sctp_rcvd(struct socket *so, int flags, struct lwp *l)
3425 {
3426 struct sctp_socket_q_list *sq=NULL;
3427 /*
3428 * The user has received some data, we may be able to stuff more
3429 * up the socket. And we need to possibly update the rwnd.
3430 */
3431 struct sctp_inpcb *inp;
3432 struct sctp_tcb *stcb=NULL;
3433
3434 inp = (struct sctp_inpcb *)so->so_pcb;
3435 #ifdef SCTP_DEBUG
3436 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3437 printf("Read for so:%p inp:%p Flags:%x\n",
3438 so, inp, flags);
3439 #endif
3440
3441 if (inp == 0) {
3442 /* I made the same as TCP since we are not setup? */
3443 #ifdef SCTP_DEBUG
3444 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3445 printf("Nope, connection reset\n");
3446 #endif
3447 return (ECONNRESET);
3448 }
3449 /*
3450 * Grab the first one on the list. It will re-insert itself if
3451 * it runs out of room
3452 */
3453 SCTP_INP_WLOCK(inp);
3454 if ((flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3455 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3456 /* Ok the other part of our grubby tracking
3457 * stuff for our horrible layer violation that
3458 * the tsvwg thinks is ok for sctp_peeloff.. gak!
3459 * We must update the next vtag pending on the
3460 * socket buffer (if any).
3461 */
3462 inp->sctp_vtag_first = sctp_get_first_vtag_from_sb(so);
3463 sq = TAILQ_FIRST(&inp->sctp_queue_list);
3464 if (sq) {
3465 stcb = sq->tcb;
3466 } else {
3467 stcb = NULL;
3468 }
3469 } else {
3470 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3471 }
3472 if (stcb) {
3473 SCTP_TCB_LOCK(stcb);
3474 }
3475 if (stcb) {
3476 long incr;
3477 /* all code in normal stcb path assumes
3478 * that you have a tcb_lock only. Thus
3479 * we must release the inp write lock.
3480 */
3481 if (flags & MSG_EOR) {
3482 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3483 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3484 stcb = sctp_remove_from_socket_q(inp);
3485 }
3486 #ifdef SCTP_DEBUG
3487 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3488 printf("remove from socket queue for inp:%p tcbret:%p\n",
3489 inp, stcb);
3490 #endif
3491
3492 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3493 sizeof(struct mbuf));
3494 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) {
3495 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3496 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)));
3497 }
3498 }
3499 if ((TAILQ_EMPTY(&stcb->asoc.delivery_queue) == 0) ||
3500 (TAILQ_EMPTY(&stcb->asoc.reasmqueue) == 0)) {
3501 /* Deliver if there is something to be delivered */
3502 sctp_service_queues(stcb, &stcb->asoc, 1);
3503 }
3504 sctp_set_rwnd(stcb, &stcb->asoc);
3505 /* if we increase by 1 or more MTU's (smallest MTUs of all
3506 * nets) we send a window update sack
3507 */
3508 incr = stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd;
3509 if (incr < 0) {
3510 incr = 0;
3511 }
3512 if (((uint32_t)incr >= (stcb->asoc.smallest_mtu * SCTP_SEG_TO_RWND_UPD)) ||
3513 ((((uint32_t)incr)*SCTP_SCALE_OF_RWND_TO_UPD) >= so->so_rcv.sb_hiwat)) {
3514 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
3515 /* If the timer is up, stop it */
3516 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
3517 stcb->sctp_ep, stcb, NULL);
3518 }
3519 /* Send the sack, with the new rwnd */
3520 sctp_send_sack(stcb);
3521 /* Now do the output */
3522 sctp_chunk_output(inp, stcb, 10);
3523 }
3524 } else {
3525 if ((( sq ) && (flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0))
3526 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3527 stcb = sctp_remove_from_socket_q(inp);
3528 }
3529 }
3530 if ((so->so_rcv.sb_mb == NULL) &&
3531 (TAILQ_EMPTY(&inp->sctp_queue_list) == 0)) {
3532 int sq_cnt=0;
3533 #ifdef SCTP_DEBUG
3534 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3535 printf("Something off, inp:%p so->so_rcv->sb_mb is empty and sockq is not.. cleaning\n",
3536 inp);
3537 #endif
3538 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3539 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3540 int done_yet;
3541 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3542 while (!done_yet) {
3543 sq_cnt++;
3544 (void)sctp_remove_from_socket_q(inp);
3545 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3546 }
3547 }
3548 #ifdef SCTP_DEBUG
3549 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3550 printf("Cleaned up %d sockq's\n", sq_cnt);
3551 #endif
3552 }
3553 if (stcb) {
3554 SCTP_TCB_UNLOCK(stcb);
3555 }
3556 SCTP_INP_WUNLOCK(inp);
3557 return (0);
3558 }
3559
3560 int
sctp_listen(struct socket * so,struct lwp * l)3561 sctp_listen(struct socket *so, struct lwp *l)
3562 {
3563 /*
3564 * Note this module depends on the protocol processing being
3565 * called AFTER any socket level flags and backlog are applied
3566 * to the socket. The traditional way that the socket flags are
3567 * applied is AFTER protocol processing. We have made a change
3568 * to the sys/kern/uipc_socket.c module to reverse this but this
3569 * MUST be in place if the socket API for SCTP is to work properly.
3570 */
3571 int error = 0;
3572 struct sctp_inpcb *inp;
3573
3574 inp = (struct sctp_inpcb *)so->so_pcb;
3575 if (inp == 0) {
3576 /* I made the same as TCP since we are not setup? */
3577 return (ECONNRESET);
3578 }
3579 SCTP_INP_RLOCK(inp);
3580 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3581 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3582 /* We are already connected AND the TCP model */
3583 SCTP_INP_RUNLOCK(inp);
3584 return (EADDRINUSE);
3585 }
3586 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3587 /* We must do a bind. */
3588 SCTP_INP_RUNLOCK(inp);
3589 if ((error = sctp_inpcb_bind(so, NULL, l))) {
3590 /* bind error, probably perm */
3591 return (error);
3592 }
3593 } else {
3594 SCTP_INP_RUNLOCK(inp);
3595 }
3596 SCTP_INP_WLOCK(inp);
3597 if (inp->sctp_socket->so_qlimit) {
3598 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3599 /*
3600 * For the UDP model we must TURN OFF the ACCEPT
3601 * flags since we do NOT allow the accept() call.
3602 * The TCP model (when present) will do accept which
3603 * then prohibits connect().
3604 */
3605 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
3606 }
3607 inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
3608 } else {
3609 if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3610 /*
3611 * Turning off the listen flags if the backlog is
3612 * set to 0 (i.e. qlimit is 0).
3613 */
3614 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
3615 }
3616 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
3617 }
3618 SCTP_INP_WUNLOCK(inp);
3619 return (error);
3620 }
3621
3622 int
sctp_accept(struct socket * so,struct sockaddr * nam)3623 sctp_accept(struct socket *so, struct sockaddr *nam)
3624 {
3625 struct sctp_tcb *stcb;
3626 const struct sockaddr *prim;
3627 struct sctp_inpcb *inp;
3628 int error;
3629
3630 if (nam == NULL) {
3631 return EINVAL;
3632 }
3633 inp = (struct sctp_inpcb *)so->so_pcb;
3634
3635 if (inp == 0) {
3636 return ECONNRESET;
3637 }
3638 SCTP_INP_RLOCK(inp);
3639 if (so->so_state & SS_ISDISCONNECTED) {
3640 SCTP_INP_RUNLOCK(inp);
3641 return ECONNABORTED;
3642 }
3643 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3644 if (stcb == NULL) {
3645 SCTP_INP_RUNLOCK(inp);
3646 return ECONNRESET;
3647 }
3648 SCTP_TCB_LOCK(stcb);
3649 SCTP_INP_RUNLOCK(inp);
3650 prim = (const struct sockaddr *)rtcache_getdst(&stcb->asoc.primary_destination->ro);
3651 if (prim->sa_family == AF_INET) {
3652 struct sockaddr_in *sin;
3653
3654 sin = (struct sockaddr_in *)nam;
3655 memset((void *)sin, 0, sizeof (*sin));
3656
3657 sin->sin_family = AF_INET;
3658 sin->sin_len = sizeof(*sin);
3659 sin->sin_port = ((const struct sockaddr_in *)prim)->sin_port;
3660 sin->sin_addr = ((const struct sockaddr_in *)prim)->sin_addr;
3661 } else {
3662 struct sockaddr_in6 *sin6;
3663
3664 sin6 = (struct sockaddr_in6 *)nam;
3665 memset((void *)sin6, 0, sizeof (*sin6));
3666 sin6->sin6_family = AF_INET6;
3667 sin6->sin6_len = sizeof(*sin6);
3668 sin6->sin6_port = ((const struct sockaddr_in6 *)prim)->sin6_port;
3669
3670 sin6->sin6_addr = ((const struct sockaddr_in6 *)prim)->sin6_addr;
3671 if ((error = sa6_recoverscope(sin6)) != 0)
3672 return error;
3673
3674 }
3675 /* Wake any delayed sleep action */
3676 SCTP_TCB_UNLOCK(stcb);
3677 SCTP_INP_WLOCK(inp);
3678 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3679 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3680 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3681 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3682 if (sowritable(inp->sctp_socket))
3683 sowwakeup(inp->sctp_socket);
3684 }
3685 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3686 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3687 if (soreadable(inp->sctp_socket))
3688 sorwakeup(inp->sctp_socket);
3689 }
3690
3691 }
3692 SCTP_INP_WUNLOCK(inp);
3693 return 0;
3694 }
3695
3696 static int
sctp_stat(struct socket * so,struct stat * ub)3697 sctp_stat(struct socket *so, struct stat *ub)
3698 {
3699 return 0;
3700 }
3701
3702 int
sctp_sockaddr(struct socket * so,struct sockaddr * nam)3703 sctp_sockaddr(struct socket *so, struct sockaddr *nam)
3704 {
3705 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
3706 struct sctp_inpcb *inp;
3707
3708 memset(sin, 0, sizeof(*sin));
3709 sin->sin_family = AF_INET;
3710 sin->sin_len = sizeof(*sin);
3711 inp = (struct sctp_inpcb *)so->so_pcb;
3712 if (!inp) {
3713 return ECONNRESET;
3714 }
3715 SCTP_INP_RLOCK(inp);
3716 sin->sin_port = inp->sctp_lport;
3717 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3718 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3719 struct sctp_tcb *stcb;
3720 const struct sockaddr_in *sin_a;
3721 struct sctp_nets *net;
3722 int fnd;
3723
3724 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3725 if (stcb == NULL) {
3726 goto notConn;
3727 }
3728 fnd = 0;
3729 sin_a = NULL;
3730 SCTP_TCB_LOCK(stcb);
3731 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3732 sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro);
3733 if (sin_a->sin_family == AF_INET) {
3734 fnd = 1;
3735 break;
3736 }
3737 }
3738 if ((!fnd) || (sin_a == NULL)) {
3739 /* punt */
3740 SCTP_TCB_UNLOCK(stcb);
3741 goto notConn;
3742 }
3743 sin->sin_addr = sctp_ipv4_source_address_selection(inp,
3744 stcb, (struct route *)&net->ro, net, 0);
3745 SCTP_TCB_UNLOCK(stcb);
3746 } else {
3747 /* For the bound all case you get back 0 */
3748 notConn:
3749 sin->sin_addr.s_addr = 0;
3750 }
3751
3752 } else {
3753 /* Take the first IPv4 address in the list */
3754 struct sctp_laddr *laddr;
3755 int fnd = 0;
3756 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3757 if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3758 struct sockaddr_in *sin_a;
3759 sin_a = (struct sockaddr_in *)laddr->ifa->ifa_addr;
3760 sin->sin_addr = sin_a->sin_addr;
3761 fnd = 1;
3762 break;
3763 }
3764 }
3765 if (!fnd) {
3766 SCTP_INP_RUNLOCK(inp);
3767 return ENOENT;
3768 }
3769 }
3770 SCTP_INP_RUNLOCK(inp);
3771 return (0);
3772 }
3773
3774 int
sctp_peeraddr(struct socket * so,struct sockaddr * nam)3775 sctp_peeraddr(struct socket *so, struct sockaddr *nam)
3776 {
3777 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
3778 int fnd;
3779 const struct sockaddr_in *sin_a;
3780 struct sctp_inpcb *inp;
3781 struct sctp_tcb *stcb;
3782 struct sctp_nets *net;
3783
3784 /* Do the malloc first in case it blocks. */
3785 inp = (struct sctp_inpcb *)so->so_pcb;
3786 if ((inp == NULL) ||
3787 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3788 /* UDP type and listeners will drop out here */
3789 return (ENOTCONN);
3790 }
3791
3792 memset(sin, 0, sizeof(*sin));
3793 sin->sin_family = AF_INET;
3794 sin->sin_len = sizeof(*sin);
3795
3796 /* We must recapture incase we blocked */
3797 inp = (struct sctp_inpcb *)so->so_pcb;
3798 if (!inp) {
3799 return ECONNRESET;
3800 }
3801 SCTP_INP_RLOCK(inp);
3802 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3803 if (stcb) {
3804 SCTP_TCB_LOCK(stcb);
3805 }
3806 SCTP_INP_RUNLOCK(inp);
3807 if (stcb == NULL) {
3808 return ECONNRESET;
3809 }
3810 fnd = 0;
3811 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3812 sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro);
3813 if (sin_a->sin_family == AF_INET) {
3814 fnd = 1;
3815 sin->sin_port = stcb->rport;
3816 sin->sin_addr = sin_a->sin_addr;
3817 break;
3818 }
3819 }
3820 SCTP_TCB_UNLOCK(stcb);
3821 if (!fnd) {
3822 /* No IPv4 address */
3823 return ENOENT;
3824 }
3825 return (0);
3826 }
3827
3828 static int
sctp_sendoob(struct socket * so,struct mbuf * m,struct mbuf * control)3829 sctp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
3830 {
3831 KASSERT(solocked(so));
3832
3833 m_freem(m);
3834 m_freem(control);
3835
3836 return EOPNOTSUPP;
3837 }
3838
3839 static int
sctp_ioctl(struct socket * so,u_long cmd,void * nam,struct ifnet * ifp)3840 sctp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
3841 {
3842 int error = 0;
3843 int family;
3844
3845 if (cmd == SIOCCONNECTX) {
3846 solock(so);
3847 error = sctp_do_connect_x(so, nam, curlwp, 0);
3848 sounlock(so);
3849 } else if (cmd == SIOCCONNECTXDEL) {
3850 solock(so);
3851 error = sctp_do_connect_x(so, nam, curlwp, 1);
3852 sounlock(so);
3853 } else {
3854 family = so->so_proto->pr_domain->dom_family;
3855 switch (family) {
3856 #ifdef INET
3857 case PF_INET:
3858 error = in_control(so, cmd, nam, ifp);
3859 break;
3860 #endif
3861 #ifdef INET6
3862 case PF_INET6:
3863 error = in6_control(so, cmd, nam, ifp);
3864 break;
3865 #endif
3866 default:
3867 error = EAFNOSUPPORT;
3868 }
3869 }
3870 return (error);
3871 }
3872
3873 static int
sctp_purgeif(struct socket * so,struct ifnet * ifp)3874 sctp_purgeif(struct socket *so, struct ifnet *ifp)
3875 {
3876 struct ifaddr *ifa;
3877 IFADDR_READER_FOREACH(ifa, ifp) {
3878 if (ifa->ifa_addr->sa_family == PF_INET) {
3879 sctp_delete_ip_address(ifa);
3880 }
3881 }
3882
3883 mutex_enter(softnet_lock);
3884 in_purgeif(ifp);
3885 mutex_exit(softnet_lock);
3886
3887 return 0;
3888 }
3889
3890 /*
3891 * Sysctl for sctp variables.
3892 */
3893 static void
sysctl_net_inet_sctp_setup(struct sysctllog ** clog)3894 sysctl_net_inet_sctp_setup(struct sysctllog **clog)
3895 {
3896
3897 sysctl_createv(clog, 0, NULL, NULL,
3898 CTLFLAG_PERMANENT,
3899 CTLTYPE_NODE, "net", NULL,
3900 NULL, 0, NULL, 0,
3901 CTL_NET, CTL_EOL);
3902 sysctl_createv(clog, 0, NULL, NULL,
3903 CTLFLAG_PERMANENT,
3904 CTLTYPE_NODE, "inet", NULL,
3905 NULL, 0, NULL, 0,
3906 CTL_NET, PF_INET, CTL_EOL);
3907 sysctl_createv(clog, 0, NULL, NULL,
3908 CTLFLAG_PERMANENT,
3909 CTLTYPE_NODE, "sctp",
3910 SYSCTL_DESCR("sctp related settings"),
3911 NULL, 0, NULL, 0,
3912 CTL_NET, PF_INET, IPPROTO_SCTP, CTL_EOL);
3913
3914 sysctl_createv(clog, 0, NULL, NULL,
3915 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3916 CTLTYPE_INT, "maxdgram",
3917 SYSCTL_DESCR("Maximum outgoing SCTP buffer size"),
3918 NULL, 0, &sctp_sendspace, 0,
3919 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXDGRAM,
3920 CTL_EOL);
3921
3922 sysctl_createv(clog, 0, NULL, NULL,
3923 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3924 CTLTYPE_INT, "recvspace",
3925 SYSCTL_DESCR("Maximum incoming SCTP buffer size"),
3926 NULL, 0, &sctp_recvspace, 0,
3927 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_RECVSPACE,
3928 CTL_EOL);
3929
3930 sysctl_createv(clog, 0, NULL, NULL,
3931 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3932 CTLTYPE_INT, "auto_asconf",
3933 SYSCTL_DESCR("Enable SCTP Auto-ASCONF"),
3934 NULL, 0, &sctp_auto_asconf, 0,
3935 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_AUTOASCONF,
3936 CTL_EOL);
3937
3938 sysctl_createv(clog, 0, NULL, NULL,
3939 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3940 CTLTYPE_INT, "ecn_enable",
3941 SYSCTL_DESCR("Enable SCTP ECN"),
3942 NULL, 0, &sctp_ecn, 0,
3943 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_ENABLE,
3944 CTL_EOL);
3945
3946 sysctl_createv(clog, 0, NULL, NULL,
3947 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3948 CTLTYPE_INT, "ecn_nonce",
3949 SYSCTL_DESCR("Enable SCTP ECN Nonce"),
3950 NULL, 0, &sctp_ecn_nonce, 0,
3951 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_NONCE,
3952 CTL_EOL);
3953
3954 sysctl_createv(clog, 0, NULL, NULL,
3955 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3956 CTLTYPE_INT, "strict_sack",
3957 SYSCTL_DESCR("Enable SCTP Strict SACK checking"),
3958 NULL, 0, &sctp_strict_sacks, 0,
3959 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_SACK,
3960 CTL_EOL);
3961
3962 sysctl_createv(clog, 0, NULL, NULL,
3963 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3964 CTLTYPE_INT, "loopback_nocsum",
3965 SYSCTL_DESCR("Enable NO Csum on packets sent on loopback"),
3966 NULL, 0, &sctp_no_csum_on_loopback, 0,
3967 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_NOCSUM_LO,
3968 CTL_EOL);
3969
3970 sysctl_createv(clog, 0, NULL, NULL,
3971 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3972 CTLTYPE_INT, "strict_init",
3973 SYSCTL_DESCR("Enable strict INIT/INIT-ACK singleton enforcement"),
3974 NULL, 0, &sctp_strict_init, 0,
3975 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_INIT,
3976 CTL_EOL);
3977
3978 sysctl_createv(clog, 0, NULL, NULL,
3979 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3980 CTLTYPE_INT, "peer_chkoh",
3981 SYSCTL_DESCR("Amount to debit peers rwnd per chunk sent"),
3982 NULL, 0, &sctp_peer_chunk_oh, 0,
3983 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_PEER_CHK_OH,
3984 CTL_EOL);
3985
3986 sysctl_createv(clog, 0, NULL, NULL,
3987 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3988 CTLTYPE_INT, "maxburst",
3989 SYSCTL_DESCR("Default max burst for sctp endpoints"),
3990 NULL, 0, &sctp_max_burst_default, 0,
3991 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXBURST,
3992 CTL_EOL);
3993
3994 sysctl_createv(clog, 0, NULL, NULL,
3995 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3996 CTLTYPE_INT, "maxchunks",
3997 SYSCTL_DESCR("Default max chunks on queue per asoc"),
3998 NULL, 0, &sctp_max_chunks_on_queue, 0,
3999 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXCHUNKONQ,
4000 CTL_EOL);
4001 #ifdef SCTP_DEBUG
4002 sysctl_createv(clog, 0, NULL, NULL,
4003 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4004 CTLTYPE_INT, "debug",
4005 SYSCTL_DESCR("Configure debug output"),
4006 NULL, 0, &sctp_debug_on, 0,
4007 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_DEBUG,
4008 CTL_EOL);
4009 #endif
4010 }
4011
4012 PR_WRAP_USRREQS(sctp)
4013 #define sctp_attach sctp_attach_wrapper
4014 #define sctp_detach sctp_detach_wrapper
4015 #define sctp_accept sctp_accept_wrapper
4016 #define sctp_bind sctp_bind_wrapper
4017 #define sctp_listen sctp_listen_wrapper
4018 #define sctp_connect sctp_connect_wrapper
4019 #define sctp_connect2 sctp_connect2_wrapper
4020 #define sctp_disconnect sctp_disconnect_wrapper
4021 #define sctp_shutdown sctp_shutdown_wrapper
4022 #define sctp_abort sctp_abort_wrapper
4023 #define sctp_ioctl sctp_ioctl_wrapper
4024 #define sctp_stat sctp_stat_wrapper
4025 #define sctp_peeraddr sctp_peeraddr_wrapper
4026 #define sctp_sockaddr sctp_sockaddr_wrapper
4027 #define sctp_rcvd sctp_rcvd_wrapper
4028 #define sctp_recvoob sctp_recvoob_wrapper
4029 #define sctp_send sctp_send_wrapper
4030 #define sctp_sendoob sctp_sendoob_wrapper
4031 #define sctp_purgeif sctp_purgeif_wrapper
4032
4033 const struct pr_usrreqs sctp_usrreqs = {
4034 .pr_attach = sctp_attach,
4035 .pr_detach = sctp_detach,
4036 .pr_accept = sctp_accept,
4037 .pr_bind = sctp_bind,
4038 .pr_listen = sctp_listen,
4039 .pr_connect = sctp_connect,
4040 .pr_connect2 = sctp_connect2,
4041 .pr_disconnect = sctp_disconnect,
4042 .pr_shutdown = sctp_shutdown,
4043 .pr_abort = sctp_abort,
4044 .pr_ioctl = sctp_ioctl,
4045 .pr_stat = sctp_stat,
4046 .pr_peeraddr = sctp_peeraddr,
4047 .pr_sockaddr = sctp_sockaddr,
4048 .pr_rcvd = sctp_rcvd,
4049 .pr_recvoob = sctp_recvoob,
4050 .pr_send = sctp_send,
4051 .pr_sendoob = sctp_sendoob,
4052 .pr_purgeif = sctp_purgeif,
4053 };
4054