]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/netinet/sctp_usrreq.c
MFC r291410:
[FreeBSD/stable/10.git] / sys / netinet / sctp_usrreq.c
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_pcb.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_var.h>
41 #ifdef INET6
42 #include <netinet6/sctp6_var.h>
43 #endif
44 #include <netinet/sctp_sysctl.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/udp.h>
54
55
56
57 extern struct sctp_cc_functions sctp_cc_functions[];
58 extern struct sctp_ss_functions sctp_ss_functions[];
59
60 void
61 sctp_init(void)
62 {
63         u_long sb_max_adj;
64
65         /* Initialize and modify the sysctled variables */
66         sctp_init_sysctls();
67         if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
68                 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
69         /*
70          * Allow a user to take no more than 1/2 the number of clusters or
71          * the SB_MAX whichever is smaller for the send window.
72          */
73         sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
74         SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
75             (((uint32_t) nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
76         /*
77          * Now for the recv window, should we take the same amount? or
78          * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
79          * now I will just copy.
80          */
81         SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
82         SCTP_BASE_VAR(first_time) = 0;
83         SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
84         sctp_pcb_init();
85 #if defined(SCTP_PACKET_LOGGING)
86         SCTP_BASE_VAR(packet_log_writers) = 0;
87         SCTP_BASE_VAR(packet_log_end) = 0;
88         bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
89 #endif
90 }
91
92 void
93 sctp_finish(void)
94 {
95         sctp_pcb_finish();
96 }
97
98
99
100 void
101 sctp_pathmtu_adjustment(struct sctp_tcb *stcb, uint16_t nxtsz)
102 {
103         struct sctp_tmit_chunk *chk;
104         uint16_t overhead;
105
106         /* Adjust that too */
107         stcb->asoc.smallest_mtu = nxtsz;
108         /* now off to subtract IP_DF flag if needed */
109         overhead = IP_HDR_SIZE;
110         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
111                 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
112         }
113         TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
114                 if ((chk->send_size + overhead) > nxtsz) {
115                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
116                 }
117         }
118         TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
119                 if ((chk->send_size + overhead) > nxtsz) {
120                         /*
121                          * For this guy we also mark for immediate resend
122                          * since we sent to big of chunk
123                          */
124                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
125                         if (chk->sent < SCTP_DATAGRAM_RESEND) {
126                                 sctp_flight_size_decrease(chk);
127                                 sctp_total_flight_decrease(stcb, chk);
128                                 chk->sent = SCTP_DATAGRAM_RESEND;
129                                 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
130                                 chk->rec.data.doing_fast_retransmit = 0;
131                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
132                                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
133                                             chk->whoTo->flight_size,
134                                             chk->book_size,
135                                             (uintptr_t) chk->whoTo,
136                                             chk->rec.data.TSN_seq);
137                                 }
138                                 /* Clear any time so NO RTT is being done */
139                                 chk->do_rtt = 0;
140                         }
141                 }
142         }
143 }
144
145 #ifdef INET
146 static void
147 sctp_notify_mbuf(struct sctp_inpcb *inp,
148     struct sctp_tcb *stcb,
149     struct sctp_nets *net,
150     struct ip *ip,
151     struct sctphdr *sh)
152 {
153         struct icmp *icmph;
154         int totsz, tmr_stopped = 0;
155         uint16_t nxtsz;
156
157         /* protection */
158         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
159             (ip == NULL) || (sh == NULL)) {
160                 if (stcb != NULL) {
161                         SCTP_TCB_UNLOCK(stcb);
162                 }
163                 return;
164         }
165         /* First job is to verify the vtag matches what I would send */
166         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
167                 SCTP_TCB_UNLOCK(stcb);
168                 return;
169         }
170         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
171             sizeof(struct ip)));
172         if (icmph->icmp_type != ICMP_UNREACH) {
173                 /* We only care about unreachable */
174                 SCTP_TCB_UNLOCK(stcb);
175                 return;
176         }
177         if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
178                 /* not a unreachable message due to frag. */
179                 SCTP_TCB_UNLOCK(stcb);
180                 return;
181         }
182         totsz = ntohs(ip->ip_len);
183
184         nxtsz = ntohs(icmph->icmp_nextmtu);
185         if (nxtsz == 0) {
186                 /*
187                  * old type router that does not tell us what the next size
188                  * mtu is. Rats we will have to guess (in a educated fashion
189                  * of course)
190                  */
191                 nxtsz = sctp_get_prev_mtu(totsz);
192         }
193         /* Stop any PMTU timer */
194         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
195                 tmr_stopped = 1;
196                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
197                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
198         }
199         /* Adjust destination size limit */
200         if (net->mtu > nxtsz) {
201                 net->mtu = nxtsz;
202                 if (net->port) {
203                         net->mtu -= sizeof(struct udphdr);
204                 }
205         }
206         /* now what about the ep? */
207         if (stcb->asoc.smallest_mtu > nxtsz) {
208                 sctp_pathmtu_adjustment(stcb, nxtsz);
209         }
210         if (tmr_stopped)
211                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
212
213         SCTP_TCB_UNLOCK(stcb);
214 }
215
216 void
217 sctp_notify(struct sctp_inpcb *inp,
218     struct ip *ip,
219     struct sctphdr *sh,
220     struct sockaddr *to,
221     struct sctp_tcb *stcb,
222     struct sctp_nets *net)
223 {
224 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
225         struct socket *so;
226
227 #endif
228         struct icmp *icmph;
229
230         /* protection */
231         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
232             (sh == NULL) || (to == NULL)) {
233                 if (stcb)
234                         SCTP_TCB_UNLOCK(stcb);
235                 return;
236         }
237         /* First job is to verify the vtag matches what I would send */
238         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
239                 SCTP_TCB_UNLOCK(stcb);
240                 return;
241         }
242         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
243             sizeof(struct ip)));
244         if (icmph->icmp_type != ICMP_UNREACH) {
245                 /* We only care about unreachable */
246                 SCTP_TCB_UNLOCK(stcb);
247                 return;
248         }
249         if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
250             (icmph->icmp_code == ICMP_UNREACH_HOST) ||
251             (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
252             (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
253             (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
254             (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
255             (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
256             (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
257
258                 /*
259                  * Hmm reachablity problems we must examine closely. If its
260                  * not reachable, we may have lost a network. Or if there is
261                  * NO protocol at the other end named SCTP. well we consider
262                  * it a OOTB abort.
263                  */
264                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
265                         /* Ok that destination is NOT reachable */
266                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
267                         net->dest_state &= ~SCTP_ADDR_PF;
268                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
269                             stcb, 0,
270                             (void *)net, SCTP_SO_NOT_LOCKED);
271                 }
272                 SCTP_TCB_UNLOCK(stcb);
273         } else if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
274             (icmph->icmp_code == ICMP_UNREACH_PORT)) {
275                 /*
276                  * Here the peer is either playing tricks on us, including
277                  * an address that belongs to someone who does not support
278                  * SCTP OR was a userland implementation that shutdown and
279                  * now is dead. In either case treat it like a OOTB abort
280                  * with no TCB
281                  */
282                 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
283 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
284                 so = SCTP_INP_SO(inp);
285                 atomic_add_int(&stcb->asoc.refcnt, 1);
286                 SCTP_TCB_UNLOCK(stcb);
287                 SCTP_SOCKET_LOCK(so, 1);
288                 SCTP_TCB_LOCK(stcb);
289                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
290 #endif
291                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
292                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
293 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
294                 SCTP_SOCKET_UNLOCK(so, 1);
295                 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed. */
296 #endif
297                 /* no need to unlock here, since the TCB is gone */
298         } else {
299                 SCTP_TCB_UNLOCK(stcb);
300         }
301 }
302
303 #endif
304
305 #ifdef INET
306 void
307 sctp_ctlinput(cmd, sa, vip)
308         int cmd;
309         struct sockaddr *sa;
310         void *vip;
311 {
312         struct ip *ip = vip;
313         struct sctphdr *sh;
314         uint32_t vrf_id;
315
316         /* FIX, for non-bsd is this right? */
317         vrf_id = SCTP_DEFAULT_VRFID;
318         if (sa->sa_family != AF_INET ||
319             ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
320                 return;
321         }
322         if (PRC_IS_REDIRECT(cmd)) {
323                 ip = 0;
324         } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
325                 return;
326         }
327         if (ip) {
328                 struct sctp_inpcb *inp = NULL;
329                 struct sctp_tcb *stcb = NULL;
330                 struct sctp_nets *net = NULL;
331                 struct sockaddr_in to, from;
332
333                 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
334                 bzero(&to, sizeof(to));
335                 bzero(&from, sizeof(from));
336                 from.sin_family = to.sin_family = AF_INET;
337                 from.sin_len = to.sin_len = sizeof(to);
338                 from.sin_port = sh->src_port;
339                 from.sin_addr = ip->ip_src;
340                 to.sin_port = sh->dest_port;
341                 to.sin_addr = ip->ip_dst;
342
343                 /*
344                  * 'to' holds the dest of the packet that failed to be sent.
345                  * 'from' holds our local endpoint address. Thus we reverse
346                  * the to and the from in the lookup.
347                  */
348                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&to,
349                     (struct sockaddr *)&from,
350                     &inp, &net, 1, vrf_id);
351                 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
352                         if (cmd != PRC_MSGSIZE) {
353                                 sctp_notify(inp, ip, sh,
354                                     (struct sockaddr *)&to, stcb,
355                                     net);
356                         } else {
357                                 /* handle possible ICMP size messages */
358                                 sctp_notify_mbuf(inp, stcb, net, ip, sh);
359                         }
360                 } else {
361                         if ((stcb == NULL) && (inp != NULL)) {
362                                 /* reduce ref-count */
363                                 SCTP_INP_WLOCK(inp);
364                                 SCTP_INP_DECR_REF(inp);
365                                 SCTP_INP_WUNLOCK(inp);
366                         }
367                         if (stcb) {
368                                 SCTP_TCB_UNLOCK(stcb);
369                         }
370                 }
371         }
372         return;
373 }
374
375 #endif
376
377 static int
378 sctp_getcred(SYSCTL_HANDLER_ARGS)
379 {
380         struct xucred xuc;
381         struct sockaddr_in addrs[2];
382         struct sctp_inpcb *inp;
383         struct sctp_nets *net;
384         struct sctp_tcb *stcb;
385         int error;
386         uint32_t vrf_id;
387
388         /* FIX, for non-bsd is this right? */
389         vrf_id = SCTP_DEFAULT_VRFID;
390
391         error = priv_check(req->td, PRIV_NETINET_GETCRED);
392
393         if (error)
394                 return (error);
395
396         error = SYSCTL_IN(req, addrs, sizeof(addrs));
397         if (error)
398                 return (error);
399
400         stcb = sctp_findassociation_addr_sa(sintosa(&addrs[1]),
401             sintosa(&addrs[0]),
402             &inp, &net, 1, vrf_id);
403         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
404                 if ((inp != NULL) && (stcb == NULL)) {
405                         /* reduce ref-count */
406                         SCTP_INP_WLOCK(inp);
407                         SCTP_INP_DECR_REF(inp);
408                         goto cred_can_cont;
409                 }
410                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
411                 error = ENOENT;
412                 goto out;
413         }
414         SCTP_TCB_UNLOCK(stcb);
415         /*
416          * We use the write lock here, only since in the error leg we need
417          * it. If we used RLOCK, then we would have to
418          * wlock/decr/unlock/rlock. Which in theory could create a hole.
419          * Better to use higher wlock.
420          */
421         SCTP_INP_WLOCK(inp);
422 cred_can_cont:
423         error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
424         if (error) {
425                 SCTP_INP_WUNLOCK(inp);
426                 goto out;
427         }
428         cru2x(inp->sctp_socket->so_cred, &xuc);
429         SCTP_INP_WUNLOCK(inp);
430         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
431 out:
432         return (error);
433 }
434
435 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
436     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
437
438
439 #ifdef INET
440 static void
441 sctp_abort(struct socket *so)
442 {
443         struct sctp_inpcb *inp;
444         uint32_t flags;
445
446         inp = (struct sctp_inpcb *)so->so_pcb;
447         if (inp == NULL) {
448                 return;
449         }
450 sctp_must_try_again:
451         flags = inp->sctp_flags;
452 #ifdef SCTP_LOG_CLOSING
453         sctp_log_closing(inp, NULL, 17);
454 #endif
455         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
456             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
457 #ifdef SCTP_LOG_CLOSING
458                 sctp_log_closing(inp, NULL, 16);
459 #endif
460                 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
461                     SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
462                 SOCK_LOCK(so);
463                 SCTP_SB_CLEAR(so->so_snd);
464                 /*
465                  * same for the rcv ones, they are only here for the
466                  * accounting/select.
467                  */
468                 SCTP_SB_CLEAR(so->so_rcv);
469
470                 /* Now null out the reference, we are completely detached. */
471                 so->so_pcb = NULL;
472                 SOCK_UNLOCK(so);
473         } else {
474                 flags = inp->sctp_flags;
475                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
476                         goto sctp_must_try_again;
477                 }
478         }
479         return;
480 }
481
482 static int
483 sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
484 {
485         struct sctp_inpcb *inp;
486         struct inpcb *ip_inp;
487         int error;
488         uint32_t vrf_id = SCTP_DEFAULT_VRFID;
489
490         inp = (struct sctp_inpcb *)so->so_pcb;
491         if (inp != 0) {
492                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
493                 return (EINVAL);
494         }
495         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
496                 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
497                 if (error) {
498                         return (error);
499                 }
500         }
501         error = sctp_inpcb_alloc(so, vrf_id);
502         if (error) {
503                 return (error);
504         }
505         inp = (struct sctp_inpcb *)so->so_pcb;
506         SCTP_INP_WLOCK(inp);
507         inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;    /* I'm not v6! */
508         ip_inp = &inp->ip_inp.inp;
509         ip_inp->inp_vflag |= INP_IPV4;
510         ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
511         SCTP_INP_WUNLOCK(inp);
512         return (0);
513 }
514
515 static int
516 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
517 {
518         struct sctp_inpcb *inp;
519
520         inp = (struct sctp_inpcb *)so->so_pcb;
521         if (inp == NULL) {
522                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
523                 return (EINVAL);
524         }
525         if (addr != NULL) {
526                 if ((addr->sa_family != AF_INET) ||
527                     (addr->sa_len != sizeof(struct sockaddr_in))) {
528                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
529                         return (EINVAL);
530                 }
531         }
532         return (sctp_inpcb_bind(so, addr, NULL, p));
533 }
534
535 #endif
536 void
537 sctp_close(struct socket *so)
538 {
539         struct sctp_inpcb *inp;
540         uint32_t flags;
541
542         inp = (struct sctp_inpcb *)so->so_pcb;
543         if (inp == NULL)
544                 return;
545
546         /*
547          * Inform all the lower layer assoc that we are done.
548          */
549 sctp_must_try_again:
550         flags = inp->sctp_flags;
551 #ifdef SCTP_LOG_CLOSING
552         sctp_log_closing(inp, NULL, 17);
553 #endif
554         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
555             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
556                 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
557                     (so->so_rcv.sb_cc > 0)) {
558 #ifdef SCTP_LOG_CLOSING
559                         sctp_log_closing(inp, NULL, 13);
560 #endif
561                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
562                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
563                 } else {
564 #ifdef SCTP_LOG_CLOSING
565                         sctp_log_closing(inp, NULL, 14);
566 #endif
567                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
568                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
569                 }
570                 /*
571                  * The socket is now detached, no matter what the state of
572                  * the SCTP association.
573                  */
574                 SOCK_LOCK(so);
575                 SCTP_SB_CLEAR(so->so_snd);
576                 /*
577                  * same for the rcv ones, they are only here for the
578                  * accounting/select.
579                  */
580                 SCTP_SB_CLEAR(so->so_rcv);
581
582                 /* Now null out the reference, we are completely detached. */
583                 so->so_pcb = NULL;
584                 SOCK_UNLOCK(so);
585         } else {
586                 flags = inp->sctp_flags;
587                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
588                         goto sctp_must_try_again;
589                 }
590         }
591         return;
592 }
593
594
595 int
596 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
597     struct mbuf *control, struct thread *p);
598
599
600 int
601 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
602     struct mbuf *control, struct thread *p)
603 {
604         struct sctp_inpcb *inp;
605         int error;
606
607         inp = (struct sctp_inpcb *)so->so_pcb;
608         if (inp == NULL) {
609                 if (control) {
610                         sctp_m_freem(control);
611                         control = NULL;
612                 }
613                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
614                 sctp_m_freem(m);
615                 return (EINVAL);
616         }
617         /* Got to have an to address if we are NOT a connected socket */
618         if ((addr == NULL) &&
619             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
620             (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))) {
621                 goto connected_type;
622         } else if (addr == NULL) {
623                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
624                 error = EDESTADDRREQ;
625                 sctp_m_freem(m);
626                 if (control) {
627                         sctp_m_freem(control);
628                         control = NULL;
629                 }
630                 return (error);
631         }
632 #ifdef INET6
633         if (addr->sa_family != AF_INET) {
634                 /* must be a v4 address! */
635                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
636                 sctp_m_freem(m);
637                 if (control) {
638                         sctp_m_freem(control);
639                         control = NULL;
640                 }
641                 error = EDESTADDRREQ;
642                 return (error);
643         }
644 #endif                          /* INET6 */
645 connected_type:
646         /* now what about control */
647         if (control) {
648                 if (inp->control) {
649                         SCTP_PRINTF("huh? control set?\n");
650                         sctp_m_freem(inp->control);
651                         inp->control = NULL;
652                 }
653                 inp->control = control;
654         }
655         /* Place the data */
656         if (inp->pkt) {
657                 SCTP_BUF_NEXT(inp->pkt_last) = m;
658                 inp->pkt_last = m;
659         } else {
660                 inp->pkt_last = inp->pkt = m;
661         }
662         if (
663         /* FreeBSD uses a flag passed */
664             ((flags & PRUS_MORETOCOME) == 0)
665             ) {
666                 /*
667                  * note with the current version this code will only be used
668                  * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
669                  * re-defining sosend to use the sctp_sosend. One can
670                  * optionally switch back to this code (by changing back the
671                  * definitions) but this is not advisable. This code is used
672                  * by FreeBSD when sending a file with sendfile() though.
673                  */
674                 int ret;
675
676                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
677                 inp->pkt = NULL;
678                 inp->control = NULL;
679                 return (ret);
680         } else {
681                 return (0);
682         }
683 }
684
685 int
686 sctp_disconnect(struct socket *so)
687 {
688         struct sctp_inpcb *inp;
689
690         inp = (struct sctp_inpcb *)so->so_pcb;
691         if (inp == NULL) {
692                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
693                 return (ENOTCONN);
694         }
695         SCTP_INP_RLOCK(inp);
696         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
697             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
698                 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
699                         /* No connection */
700                         SCTP_INP_RUNLOCK(inp);
701                         return (0);
702                 } else {
703                         struct sctp_association *asoc;
704                         struct sctp_tcb *stcb;
705
706                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
707                         if (stcb == NULL) {
708                                 SCTP_INP_RUNLOCK(inp);
709                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
710                                 return (EINVAL);
711                         }
712                         SCTP_TCB_LOCK(stcb);
713                         asoc = &stcb->asoc;
714                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
715                                 /* We are about to be freed, out of here */
716                                 SCTP_TCB_UNLOCK(stcb);
717                                 SCTP_INP_RUNLOCK(inp);
718                                 return (0);
719                         }
720                         if (((so->so_options & SO_LINGER) &&
721                             (so->so_linger == 0)) ||
722                             (so->so_rcv.sb_cc > 0)) {
723                                 if (SCTP_GET_STATE(asoc) !=
724                                     SCTP_STATE_COOKIE_WAIT) {
725                                         /* Left with Data unread */
726                                         struct mbuf *err;
727
728                                         err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
729                                         if (err) {
730                                                 /*
731                                                  * Fill in the user
732                                                  * initiated abort
733                                                  */
734                                                 struct sctp_paramhdr *ph;
735
736                                                 ph = mtod(err, struct sctp_paramhdr *);
737                                                 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
738                                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
739                                                 ph->param_length = htons(SCTP_BUF_LEN(err));
740                                         }
741                                         sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
742                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
743                                 }
744                                 SCTP_INP_RUNLOCK(inp);
745                                 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
746                                     (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
747                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
748                                 }
749                                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
750                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
751                                 /* No unlock tcb assoc is gone */
752                                 return (0);
753                         }
754                         if (TAILQ_EMPTY(&asoc->send_queue) &&
755                             TAILQ_EMPTY(&asoc->sent_queue) &&
756                             (asoc->stream_queue_cnt == 0)) {
757                                 /* there is nothing queued to send, so done */
758                                 if (asoc->locked_on_sending) {
759                                         goto abort_anyway;
760                                 }
761                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
762                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
763                                         /* only send SHUTDOWN 1st time thru */
764                                         struct sctp_nets *netp;
765
766                                         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
767                                             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
768                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
769                                         }
770                                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
771                                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
772                                         sctp_stop_timers_for_shutdown(stcb);
773                                         if (stcb->asoc.alternate) {
774                                                 netp = stcb->asoc.alternate;
775                                         } else {
776                                                 netp = stcb->asoc.primary_destination;
777                                         }
778                                         sctp_send_shutdown(stcb, netp);
779                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
780                                             stcb->sctp_ep, stcb, netp);
781                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
782                                             stcb->sctp_ep, stcb, netp);
783                                         sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
784                                 }
785                         } else {
786                                 /*
787                                  * we still got (or just got) data to send,
788                                  * so set SHUTDOWN_PENDING
789                                  */
790                                 /*
791                                  * XXX sockets draft says that SCTP_EOF
792                                  * should be sent with no data. currently,
793                                  * we will allow user data to be sent first
794                                  * and move to SHUTDOWN-PENDING
795                                  */
796                                 struct sctp_nets *netp;
797
798                                 if (stcb->asoc.alternate) {
799                                         netp = stcb->asoc.alternate;
800                                 } else {
801                                         netp = stcb->asoc.primary_destination;
802                                 }
803
804                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
805                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
806                                     netp);
807                                 if (asoc->locked_on_sending) {
808                                         /* Locked to send out the data */
809                                         struct sctp_stream_queue_pending *sp;
810
811                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
812                                         if (sp == NULL) {
813                                                 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
814                                                     asoc->locked_on_sending->stream_no);
815                                         } else {
816                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
817                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
818                                         }
819                                 }
820                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
821                                     TAILQ_EMPTY(&asoc->sent_queue) &&
822                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
823                                         struct mbuf *op_err;
824
825                         abort_anyway:
826                                         op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
827                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
828                                         sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
829                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
830                                         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
831                                             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
832                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
833                                         }
834                                         SCTP_INP_RUNLOCK(inp);
835                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
836                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
837                                         return (0);
838                                 } else {
839                                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
840                                 }
841                         }
842                         soisdisconnecting(so);
843                         SCTP_TCB_UNLOCK(stcb);
844                         SCTP_INP_RUNLOCK(inp);
845                         return (0);
846                 }
847                 /* not reached */
848         } else {
849                 /* UDP model does not support this */
850                 SCTP_INP_RUNLOCK(inp);
851                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
852                 return (EOPNOTSUPP);
853         }
854 }
855
856 int
857 sctp_flush(struct socket *so, int how)
858 {
859         /*
860          * We will just clear out the values and let subsequent close clear
861          * out the data, if any. Note if the user did a shutdown(SHUT_RD)
862          * they will not be able to read the data, the socket will block
863          * that from happening.
864          */
865         struct sctp_inpcb *inp;
866
867         inp = (struct sctp_inpcb *)so->so_pcb;
868         if (inp == NULL) {
869                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
870                 return (EINVAL);
871         }
872         SCTP_INP_RLOCK(inp);
873         /* For the 1 to many model this does nothing */
874         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
875                 SCTP_INP_RUNLOCK(inp);
876                 return (0);
877         }
878         SCTP_INP_RUNLOCK(inp);
879         if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
880                 /*
881                  * First make sure the sb will be happy, we don't use these
882                  * except maybe the count
883                  */
884                 SCTP_INP_WLOCK(inp);
885                 SCTP_INP_READ_LOCK(inp);
886                 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
887                 SCTP_INP_READ_UNLOCK(inp);
888                 SCTP_INP_WUNLOCK(inp);
889                 so->so_rcv.sb_cc = 0;
890                 so->so_rcv.sb_mbcnt = 0;
891                 so->so_rcv.sb_mb = NULL;
892         }
893         if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
894                 /*
895                  * First make sure the sb will be happy, we don't use these
896                  * except maybe the count
897                  */
898                 so->so_snd.sb_cc = 0;
899                 so->so_snd.sb_mbcnt = 0;
900                 so->so_snd.sb_mb = NULL;
901
902         }
903         return (0);
904 }
905
906 int
907 sctp_shutdown(struct socket *so)
908 {
909         struct sctp_inpcb *inp;
910
911         inp = (struct sctp_inpcb *)so->so_pcb;
912         if (inp == NULL) {
913                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
914                 return (EINVAL);
915         }
916         SCTP_INP_RLOCK(inp);
917         /* For UDP model this is a invalid call */
918         if (!((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
919             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
920                 /* Restore the flags that the soshutdown took away. */
921                 SOCKBUF_LOCK(&so->so_rcv);
922                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
923                 SOCKBUF_UNLOCK(&so->so_rcv);
924                 /* This proc will wakeup for read and do nothing (I hope) */
925                 SCTP_INP_RUNLOCK(inp);
926                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
927                 return (EOPNOTSUPP);
928         } else {
929                 /*
930                  * Ok, if we reach here its the TCP model and it is either a
931                  * SHUT_WR or SHUT_RDWR. This means we put the shutdown flag
932                  * against it.
933                  */
934                 struct sctp_tcb *stcb;
935                 struct sctp_association *asoc;
936                 struct sctp_nets *netp;
937
938                 if ((so->so_state &
939                     (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
940                         SCTP_INP_RUNLOCK(inp);
941                         return (ENOTCONN);
942                 }
943                 socantsendmore(so);
944
945                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
946                 if (stcb == NULL) {
947                         /*
948                          * Ok, we hit the case that the shutdown call was
949                          * made after an abort or something. Nothing to do
950                          * now.
951                          */
952                         SCTP_INP_RUNLOCK(inp);
953                         return (0);
954                 }
955                 SCTP_TCB_LOCK(stcb);
956                 asoc = &stcb->asoc;
957                 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
958                         SCTP_TCB_UNLOCK(stcb);
959                         SCTP_INP_RUNLOCK(inp);
960                         return (0);
961                 }
962                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
963                     (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED) &&
964                     (SCTP_GET_STATE(asoc) != SCTP_STATE_OPEN)) {
965                         /*
966                          * If we are not in or before ESTABLISHED, there is
967                          * no protocol action required.
968                          */
969                         SCTP_TCB_UNLOCK(stcb);
970                         SCTP_INP_RUNLOCK(inp);
971                         return (0);
972                 }
973                 if (stcb->asoc.alternate) {
974                         netp = stcb->asoc.alternate;
975                 } else {
976                         netp = stcb->asoc.primary_destination;
977                 }
978                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) &&
979                     TAILQ_EMPTY(&asoc->send_queue) &&
980                     TAILQ_EMPTY(&asoc->sent_queue) &&
981                     (asoc->stream_queue_cnt == 0)) {
982                         if (asoc->locked_on_sending) {
983                                 goto abort_anyway;
984                         }
985                         /* there is nothing queued to send, so I'm done... */
986                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
987                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
988                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
989                         sctp_stop_timers_for_shutdown(stcb);
990                         sctp_send_shutdown(stcb, netp);
991                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
992                             stcb->sctp_ep, stcb, netp);
993                 } else {
994                         /*
995                          * We still got (or just got) data to send, so set
996                          * SHUTDOWN_PENDING.
997                          */
998                         SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
999                         if (asoc->locked_on_sending) {
1000                                 /* Locked to send out the data */
1001                                 struct sctp_stream_queue_pending *sp;
1002
1003                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1004                                 if (sp == NULL) {
1005                                         SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1006                                             asoc->locked_on_sending->stream_no);
1007                                 } else {
1008                                         if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
1009                                                 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT);
1010                                         }
1011                                 }
1012                         }
1013                         if (TAILQ_EMPTY(&asoc->send_queue) &&
1014                             TAILQ_EMPTY(&asoc->sent_queue) &&
1015                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1016                                 struct mbuf *op_err;
1017
1018                 abort_anyway:
1019                                 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
1020                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
1021                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1022                                     op_err, SCTP_SO_LOCKED);
1023                                 SCTP_INP_RUNLOCK(inp);
1024                                 return (0);
1025                         }
1026                 }
1027                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, netp);
1028                 /*
1029                  * XXX: Why do this in the case where we have still data
1030                  * queued?
1031                  */
1032                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1033                 SCTP_TCB_UNLOCK(stcb);
1034                 SCTP_INP_RUNLOCK(inp);
1035                 return (0);
1036         }
1037 }
1038
1039 /*
1040  * copies a "user" presentable address and removes embedded scope, etc.
1041  * returns 0 on success, 1 on error
1042  */
1043 static uint32_t
1044 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1045 {
1046 #ifdef INET6
1047         struct sockaddr_in6 lsa6;
1048
1049         sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1050             &lsa6);
1051 #endif
1052         memcpy(ss, sa, sa->sa_len);
1053         return (0);
1054 }
1055
1056
1057
1058 /*
1059  * NOTE: assumes addr lock is held
1060  */
1061 static size_t
1062 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1063     struct sctp_tcb *stcb,
1064     size_t limit,
1065     struct sockaddr_storage *sas,
1066     uint32_t vrf_id)
1067 {
1068         struct sctp_ifn *sctp_ifn;
1069         struct sctp_ifa *sctp_ifa;
1070         size_t actual;
1071         int loopback_scope;
1072
1073 #if defined(INET)
1074         int ipv4_local_scope, ipv4_addr_legal;
1075
1076 #endif
1077 #if defined(INET6)
1078         int local_scope, site_scope, ipv6_addr_legal;
1079
1080 #endif
1081         struct sctp_vrf *vrf;
1082
1083         actual = 0;
1084         if (limit <= 0)
1085                 return (actual);
1086
1087         if (stcb) {
1088                 /* Turn on all the appropriate scope */
1089                 loopback_scope = stcb->asoc.scope.loopback_scope;
1090 #if defined(INET)
1091                 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
1092                 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
1093 #endif
1094 #if defined(INET6)
1095                 local_scope = stcb->asoc.scope.local_scope;
1096                 site_scope = stcb->asoc.scope.site_scope;
1097                 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
1098 #endif
1099         } else {
1100                 /* Use generic values for endpoints. */
1101                 loopback_scope = 1;
1102 #if defined(INET)
1103                 ipv4_local_scope = 1;
1104 #endif
1105 #if defined(INET6)
1106                 local_scope = 1;
1107                 site_scope = 1;
1108 #endif
1109                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1110 #if defined(INET6)
1111                         ipv6_addr_legal = 1;
1112 #endif
1113 #if defined(INET)
1114                         if (SCTP_IPV6_V6ONLY(inp)) {
1115                                 ipv4_addr_legal = 0;
1116                         } else {
1117                                 ipv4_addr_legal = 1;
1118                         }
1119 #endif
1120                 } else {
1121 #if defined(INET6)
1122                         ipv6_addr_legal = 0;
1123 #endif
1124 #if defined(INET)
1125                         ipv4_addr_legal = 1;
1126 #endif
1127                 }
1128         }
1129         vrf = sctp_find_vrf(vrf_id);
1130         if (vrf == NULL) {
1131                 return (0);
1132         }
1133         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1134                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1135                         if ((loopback_scope == 0) &&
1136                             SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1137                                 /* Skip loopback if loopback_scope not set */
1138                                 continue;
1139                         }
1140                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1141                                 if (stcb) {
1142                                         /*
1143                                          * For the BOUND-ALL case, the list
1144                                          * associated with a TCB is Always
1145                                          * considered a reverse list.. i.e.
1146                                          * it lists addresses that are NOT
1147                                          * part of the association. If this
1148                                          * is one of those we must skip it.
1149                                          */
1150                                         if (sctp_is_addr_restricted(stcb,
1151                                             sctp_ifa)) {
1152                                                 continue;
1153                                         }
1154                                 }
1155                                 switch (sctp_ifa->address.sa.sa_family) {
1156 #ifdef INET
1157                                 case AF_INET:
1158                                         if (ipv4_addr_legal) {
1159                                                 struct sockaddr_in *sin;
1160
1161                                                 sin = &sctp_ifa->address.sin;
1162                                                 if (sin->sin_addr.s_addr == 0) {
1163                                                         /*
1164                                                          * we skip
1165                                                          * unspecifed
1166                                                          * addresses
1167                                                          */
1168                                                         continue;
1169                                                 }
1170                                                 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1171                                                     &sin->sin_addr) != 0) {
1172                                                         continue;
1173                                                 }
1174                                                 if ((ipv4_local_scope == 0) &&
1175                                                     (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1176                                                         continue;
1177                                                 }
1178 #ifdef INET6
1179                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1180                                                         in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1181                                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1182                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1183                                                         actual += sizeof(struct sockaddr_in6);
1184                                                 } else {
1185 #endif
1186                                                         memcpy(sas, sin, sizeof(*sin));
1187                                                         ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1188                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1189                                                         actual += sizeof(*sin);
1190 #ifdef INET6
1191                                                 }
1192 #endif
1193                                                 if (actual >= limit) {
1194                                                         return (actual);
1195                                                 }
1196                                         } else {
1197                                                 continue;
1198                                         }
1199                                         break;
1200 #endif
1201 #ifdef INET6
1202                                 case AF_INET6:
1203                                         if (ipv6_addr_legal) {
1204                                                 struct sockaddr_in6 *sin6;
1205
1206                                                 sin6 = &sctp_ifa->address.sin6;
1207                                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1208                                                         /*
1209                                                          * we skip
1210                                                          * unspecifed
1211                                                          * addresses
1212                                                          */
1213                                                         continue;
1214                                                 }
1215                                                 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1216                                                     &sin6->sin6_addr) != 0) {
1217                                                         continue;
1218                                                 }
1219                                                 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1220                                                         if (local_scope == 0)
1221                                                                 continue;
1222                                                         if (sin6->sin6_scope_id == 0) {
1223                                                                 if (sa6_recoverscope(sin6) != 0)
1224                                                                         /*
1225                                                                          * 
1226                                                                          * bad
1227                                                                          * 
1228                                                                          * li
1229                                                                          * nk
1230                                                                          * 
1231                                                                          * loc
1232                                                                          * al
1233                                                                          * 
1234                                                                          * add
1235                                                                          * re
1236                                                                          * ss
1237                                                                          * */
1238                                                                         continue;
1239                                                         }
1240                                                 }
1241                                                 if ((site_scope == 0) &&
1242                                                     (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1243                                                         continue;
1244                                                 }
1245                                                 memcpy(sas, sin6, sizeof(*sin6));
1246                                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1247                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1248                                                 actual += sizeof(*sin6);
1249                                                 if (actual >= limit) {
1250                                                         return (actual);
1251                                                 }
1252                                         } else {
1253                                                 continue;
1254                                         }
1255                                         break;
1256 #endif
1257                                 default:
1258                                         /* TSNH */
1259                                         break;
1260                                 }
1261                         }
1262                 }
1263         } else {
1264                 struct sctp_laddr *laddr;
1265
1266                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1267                         if (stcb) {
1268                                 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1269                                         continue;
1270                                 }
1271                         }
1272                         if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1273                                 continue;
1274                         switch (laddr->ifa->address.sa.sa_family) {
1275 #ifdef INET
1276                         case AF_INET:
1277                                 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1278                                 break;
1279 #endif
1280 #ifdef INET6
1281                         case AF_INET6:
1282                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1283                                 break;
1284 #endif
1285                         default:
1286                                 /* TSNH */
1287                                 break;
1288                         }
1289                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1290                             laddr->ifa->address.sa.sa_len);
1291                         actual += laddr->ifa->address.sa.sa_len;
1292                         if (actual >= limit) {
1293                                 return (actual);
1294                         }
1295                 }
1296         }
1297         return (actual);
1298 }
1299
1300 static size_t
1301 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1302     struct sctp_tcb *stcb,
1303     size_t limit,
1304     struct sockaddr_storage *sas)
1305 {
1306         size_t size = 0;
1307
1308         SCTP_IPI_ADDR_RLOCK();
1309         /* fill up addresses for the endpoint's default vrf */
1310         size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1311             inp->def_vrf_id);
1312         SCTP_IPI_ADDR_RUNLOCK();
1313         return (size);
1314 }
1315
1316 /*
1317  * NOTE: assumes addr lock is held
1318  */
1319 static int
1320 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1321 {
1322         int cnt = 0;
1323         struct sctp_vrf *vrf = NULL;
1324
1325         /*
1326          * In both sub-set bound an bound_all cases we return the MAXIMUM
1327          * number of addresses that you COULD get. In reality the sub-set
1328          * bound may have an exclusion list for a given TCB OR in the
1329          * bound-all case a TCB may NOT include the loopback or other
1330          * addresses as well.
1331          */
1332         vrf = sctp_find_vrf(vrf_id);
1333         if (vrf == NULL) {
1334                 return (0);
1335         }
1336         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1337                 struct sctp_ifn *sctp_ifn;
1338                 struct sctp_ifa *sctp_ifa;
1339
1340                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1341                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1342                                 /* Count them if they are the right type */
1343                                 switch (sctp_ifa->address.sa.sa_family) {
1344 #ifdef INET
1345                                 case AF_INET:
1346 #ifdef INET6
1347                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1348                                                 cnt += sizeof(struct sockaddr_in6);
1349                                         else
1350                                                 cnt += sizeof(struct sockaddr_in);
1351 #else
1352                                         cnt += sizeof(struct sockaddr_in);
1353 #endif
1354                                         break;
1355 #endif
1356 #ifdef INET6
1357                                 case AF_INET6:
1358                                         cnt += sizeof(struct sockaddr_in6);
1359                                         break;
1360 #endif
1361                                 default:
1362                                         break;
1363                                 }
1364                         }
1365                 }
1366         } else {
1367                 struct sctp_laddr *laddr;
1368
1369                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1370                         switch (laddr->ifa->address.sa.sa_family) {
1371 #ifdef INET
1372                         case AF_INET:
1373 #ifdef INET6
1374                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1375                                         cnt += sizeof(struct sockaddr_in6);
1376                                 else
1377                                         cnt += sizeof(struct sockaddr_in);
1378 #else
1379                                 cnt += sizeof(struct sockaddr_in);
1380 #endif
1381                                 break;
1382 #endif
1383 #ifdef INET6
1384                         case AF_INET6:
1385                                 cnt += sizeof(struct sockaddr_in6);
1386                                 break;
1387 #endif
1388                         default:
1389                                 break;
1390                         }
1391                 }
1392         }
1393         return (cnt);
1394 }
1395
1396 static int
1397 sctp_count_max_addresses(struct sctp_inpcb *inp)
1398 {
1399         int cnt = 0;
1400
1401         SCTP_IPI_ADDR_RLOCK();
1402         /* count addresses for the endpoint's default VRF */
1403         cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1404         SCTP_IPI_ADDR_RUNLOCK();
1405         return (cnt);
1406 }
1407
1408 static int
1409 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1410     size_t optsize, void *p, int delay)
1411 {
1412         int error = 0;
1413         int creat_lock_on = 0;
1414         struct sctp_tcb *stcb = NULL;
1415         struct sockaddr *sa;
1416         int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1417         uint32_t vrf_id;
1418         int bad_addresses = 0;
1419         sctp_assoc_t *a_id;
1420
1421         SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1422
1423         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1424             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1425                 /* We are already connected AND the TCP model */
1426                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1427                 return (EADDRINUSE);
1428         }
1429         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1430             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1431                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1432                 return (EINVAL);
1433         }
1434         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1435                 SCTP_INP_RLOCK(inp);
1436                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1437                 SCTP_INP_RUNLOCK(inp);
1438         }
1439         if (stcb) {
1440                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1441                 return (EALREADY);
1442         }
1443         SCTP_INP_INCR_REF(inp);
1444         SCTP_ASOC_CREATE_LOCK(inp);
1445         creat_lock_on = 1;
1446         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1447             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1448                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1449                 error = EFAULT;
1450                 goto out_now;
1451         }
1452         totaddrp = (int *)optval;
1453         totaddr = *totaddrp;
1454         sa = (struct sockaddr *)(totaddrp + 1);
1455         stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
1456         if ((stcb != NULL) || bad_addresses) {
1457                 /* Already have or am bring up an association */
1458                 SCTP_ASOC_CREATE_UNLOCK(inp);
1459                 creat_lock_on = 0;
1460                 if (stcb)
1461                         SCTP_TCB_UNLOCK(stcb);
1462                 if (bad_addresses == 0) {
1463                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1464                         error = EALREADY;
1465                 }
1466                 goto out_now;
1467         }
1468 #ifdef INET6
1469         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1470             (num_v6 > 0)) {
1471                 error = EINVAL;
1472                 goto out_now;
1473         }
1474         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1475             (num_v4 > 0)) {
1476                 struct in6pcb *inp6;
1477
1478                 inp6 = (struct in6pcb *)inp;
1479                 if (SCTP_IPV6_V6ONLY(inp6)) {
1480                         /*
1481                          * if IPV6_V6ONLY flag, ignore connections destined
1482                          * to a v4 addr or v4-mapped addr
1483                          */
1484                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1485                         error = EINVAL;
1486                         goto out_now;
1487                 }
1488         }
1489 #endif                          /* INET6 */
1490         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1491             SCTP_PCB_FLAGS_UNBOUND) {
1492                 /* Bind a ephemeral port */
1493                 error = sctp_inpcb_bind(so, NULL, NULL, p);
1494                 if (error) {
1495                         goto out_now;
1496                 }
1497         }
1498         /* FIX ME: do we want to pass in a vrf on the connect call? */
1499         vrf_id = inp->def_vrf_id;
1500
1501
1502         /* We are GOOD to go */
1503         stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1504             (struct thread *)p
1505             );
1506         if (stcb == NULL) {
1507                 /* Gak! no memory */
1508                 goto out_now;
1509         }
1510         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1511                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1512                 /* Set the connected flag so we can queue data */
1513                 soisconnecting(so);
1514         }
1515         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1516         /* move to second address */
1517         switch (sa->sa_family) {
1518 #ifdef INET
1519         case AF_INET:
1520                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1521                 break;
1522 #endif
1523 #ifdef INET6
1524         case AF_INET6:
1525                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1526                 break;
1527 #endif
1528         default:
1529                 break;
1530         }
1531
1532         error = 0;
1533         sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1534         /* Fill in the return id */
1535         if (error) {
1536                 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
1537                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1538                 goto out_now;
1539         }
1540         a_id = (sctp_assoc_t *) optval;
1541         *a_id = sctp_get_associd(stcb);
1542
1543         /* initialize authentication parameters for the assoc */
1544         sctp_initialize_auth_params(inp, stcb);
1545
1546         if (delay) {
1547                 /* doing delayed connection */
1548                 stcb->asoc.delayed_connection = 1;
1549                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1550         } else {
1551                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1552                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1553         }
1554         SCTP_TCB_UNLOCK(stcb);
1555         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1556                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1557                 /* Set the connected flag so we can queue data */
1558                 soisconnecting(so);
1559         }
1560 out_now:
1561         if (creat_lock_on) {
1562                 SCTP_ASOC_CREATE_UNLOCK(inp);
1563         }
1564         SCTP_INP_DECR_REF(inp);
1565         return (error);
1566 }
1567
1568 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1569         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1570             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1571                 SCTP_INP_RLOCK(inp); \
1572                 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1573                 if (stcb) { \
1574                         SCTP_TCB_LOCK(stcb); \
1575                 } \
1576                 SCTP_INP_RUNLOCK(inp); \
1577         } else if (assoc_id > SCTP_ALL_ASSOC) { \
1578                 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1579                 if (stcb == NULL) { \
1580                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1581                         error = ENOENT; \
1582                         break; \
1583                 } \
1584         } else { \
1585                 stcb = NULL; \
1586         } \
1587   }
1588
1589
1590 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1591         if (size < sizeof(type)) { \
1592                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1593                 error = EINVAL; \
1594                 break; \
1595         } else { \
1596                 destp = (type *)srcp; \
1597         } \
1598       }
1599
1600 static int
1601 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1602     void *p)
1603 {
1604         struct sctp_inpcb *inp = NULL;
1605         int error, val = 0;
1606         struct sctp_tcb *stcb = NULL;
1607
1608         if (optval == NULL) {
1609                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1610                 return (EINVAL);
1611         }
1612         inp = (struct sctp_inpcb *)so->so_pcb;
1613         if (inp == NULL) {
1614                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1615                 return EINVAL;
1616         }
1617         error = 0;
1618
1619         switch (optname) {
1620         case SCTP_NODELAY:
1621         case SCTP_AUTOCLOSE:
1622         case SCTP_EXPLICIT_EOR:
1623         case SCTP_AUTO_ASCONF:
1624         case SCTP_DISABLE_FRAGMENTS:
1625         case SCTP_I_WANT_MAPPED_V4_ADDR:
1626         case SCTP_USE_EXT_RCVINFO:
1627                 SCTP_INP_RLOCK(inp);
1628                 switch (optname) {
1629                 case SCTP_DISABLE_FRAGMENTS:
1630                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1631                         break;
1632                 case SCTP_I_WANT_MAPPED_V4_ADDR:
1633                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1634                         break;
1635                 case SCTP_AUTO_ASCONF:
1636                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1637                                 /* only valid for bound all sockets */
1638                                 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1639                         } else {
1640                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1641                                 error = EINVAL;
1642                                 goto flags_out;
1643                         }
1644                         break;
1645                 case SCTP_EXPLICIT_EOR:
1646                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1647                         break;
1648                 case SCTP_NODELAY:
1649                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1650                         break;
1651                 case SCTP_USE_EXT_RCVINFO:
1652                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1653                         break;
1654                 case SCTP_AUTOCLOSE:
1655                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1656                                 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1657                         else
1658                                 val = 0;
1659                         break;
1660
1661                 default:
1662                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1663                         error = ENOPROTOOPT;
1664                 }               /* end switch (sopt->sopt_name) */
1665                 if (*optsize < sizeof(val)) {
1666                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1667                         error = EINVAL;
1668                 }
1669 flags_out:
1670                 SCTP_INP_RUNLOCK(inp);
1671                 if (error == 0) {
1672                         /* return the option value */
1673                         *(int *)optval = val;
1674                         *optsize = sizeof(val);
1675                 }
1676                 break;
1677         case SCTP_GET_PACKET_LOG:
1678                 {
1679 #ifdef  SCTP_PACKET_LOGGING
1680                         uint8_t *target;
1681                         int ret;
1682
1683                         SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1684                         ret = sctp_copy_out_packet_log(target, (int)*optsize);
1685                         *optsize = ret;
1686 #else
1687                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1688                         error = EOPNOTSUPP;
1689 #endif
1690                         break;
1691                 }
1692         case SCTP_REUSE_PORT:
1693                 {
1694                         uint32_t *value;
1695
1696                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1697                                 /* Can't do this for a 1-m socket */
1698                                 error = EINVAL;
1699                                 break;
1700                         }
1701                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1702                         *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1703                         *optsize = sizeof(uint32_t);
1704                         break;
1705                 }
1706         case SCTP_PARTIAL_DELIVERY_POINT:
1707                 {
1708                         uint32_t *value;
1709
1710                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1711                         *value = inp->partial_delivery_point;
1712                         *optsize = sizeof(uint32_t);
1713                         break;
1714                 }
1715         case SCTP_FRAGMENT_INTERLEAVE:
1716                 {
1717                         uint32_t *value;
1718
1719                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1720                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1721                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1722                                         *value = SCTP_FRAG_LEVEL_2;
1723                                 } else {
1724                                         *value = SCTP_FRAG_LEVEL_1;
1725                                 }
1726                         } else {
1727                                 *value = SCTP_FRAG_LEVEL_0;
1728                         }
1729                         *optsize = sizeof(uint32_t);
1730                         break;
1731                 }
1732         case SCTP_CMT_ON_OFF:
1733                 {
1734                         struct sctp_assoc_value *av;
1735
1736                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1737                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1738                         if (stcb) {
1739                                 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1740                                 SCTP_TCB_UNLOCK(stcb);
1741                         } else {
1742                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1743                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1744                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1745                                         SCTP_INP_RLOCK(inp);
1746                                         av->assoc_value = inp->sctp_cmt_on_off;
1747                                         SCTP_INP_RUNLOCK(inp);
1748                                 } else {
1749                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1750                                         error = EINVAL;
1751                                 }
1752                         }
1753                         if (error == 0) {
1754                                 *optsize = sizeof(struct sctp_assoc_value);
1755                         }
1756                         break;
1757                 }
1758         case SCTP_PLUGGABLE_CC:
1759                 {
1760                         struct sctp_assoc_value *av;
1761
1762                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1763                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1764                         if (stcb) {
1765                                 av->assoc_value = stcb->asoc.congestion_control_module;
1766                                 SCTP_TCB_UNLOCK(stcb);
1767                         } else {
1768                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1769                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1770                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1771                                         SCTP_INP_RLOCK(inp);
1772                                         av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1773                                         SCTP_INP_RUNLOCK(inp);
1774                                 } else {
1775                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1776                                         error = EINVAL;
1777                                 }
1778                         }
1779                         if (error == 0) {
1780                                 *optsize = sizeof(struct sctp_assoc_value);
1781                         }
1782                         break;
1783                 }
1784         case SCTP_CC_OPTION:
1785                 {
1786                         struct sctp_cc_option *cc_opt;
1787
1788                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1789                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1790                         if (stcb == NULL) {
1791                                 error = EINVAL;
1792                         } else {
1793                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1794                                         error = ENOTSUP;
1795                                 } else {
1796                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt);
1797                                         *optsize = sizeof(struct sctp_cc_option);
1798                                 }
1799                                 SCTP_TCB_UNLOCK(stcb);
1800                         }
1801                         break;
1802                 }
1803         case SCTP_PLUGGABLE_SS:
1804                 {
1805                         struct sctp_assoc_value *av;
1806
1807                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1808                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1809                         if (stcb) {
1810                                 av->assoc_value = stcb->asoc.stream_scheduling_module;
1811                                 SCTP_TCB_UNLOCK(stcb);
1812                         } else {
1813                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1814                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1815                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1816                                         SCTP_INP_RLOCK(inp);
1817                                         av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1818                                         SCTP_INP_RUNLOCK(inp);
1819                                 } else {
1820                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1821                                         error = EINVAL;
1822                                 }
1823                         }
1824                         if (error == 0) {
1825                                 *optsize = sizeof(struct sctp_assoc_value);
1826                         }
1827                         break;
1828                 }
1829         case SCTP_SS_VALUE:
1830                 {
1831                         struct sctp_stream_value *av;
1832
1833                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1834                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1835                         if (stcb) {
1836                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
1837                                     (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1838                                     &av->stream_value) < 0)) {
1839                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1840                                         error = EINVAL;
1841                                 } else {
1842                                         *optsize = sizeof(struct sctp_stream_value);
1843                                 }
1844                                 SCTP_TCB_UNLOCK(stcb);
1845                         } else {
1846                                 /*
1847                                  * Can't get stream value without
1848                                  * association
1849                                  */
1850                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1851                                 error = EINVAL;
1852                         }
1853                         break;
1854                 }
1855         case SCTP_GET_ADDR_LEN:
1856                 {
1857                         struct sctp_assoc_value *av;
1858
1859                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1860                         error = EINVAL;
1861 #ifdef INET
1862                         if (av->assoc_value == AF_INET) {
1863                                 av->assoc_value = sizeof(struct sockaddr_in);
1864                                 error = 0;
1865                         }
1866 #endif
1867 #ifdef INET6
1868                         if (av->assoc_value == AF_INET6) {
1869                                 av->assoc_value = sizeof(struct sockaddr_in6);
1870                                 error = 0;
1871                         }
1872 #endif
1873                         if (error) {
1874                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1875                         } else {
1876                                 *optsize = sizeof(struct sctp_assoc_value);
1877                         }
1878                         break;
1879                 }
1880         case SCTP_GET_ASSOC_NUMBER:
1881                 {
1882                         uint32_t *value, cnt;
1883
1884                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1885                         cnt = 0;
1886                         SCTP_INP_RLOCK(inp);
1887                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1888                                 cnt++;
1889                         }
1890                         SCTP_INP_RUNLOCK(inp);
1891                         *value = cnt;
1892                         *optsize = sizeof(uint32_t);
1893                         break;
1894                 }
1895         case SCTP_GET_ASSOC_ID_LIST:
1896                 {
1897                         struct sctp_assoc_ids *ids;
1898                         unsigned int at, limit;
1899
1900                         SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1901                         at = 0;
1902                         limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1903                         SCTP_INP_RLOCK(inp);
1904                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1905                                 if (at < limit) {
1906                                         ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1907                                 } else {
1908                                         error = EINVAL;
1909                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1910                                         break;
1911                                 }
1912                         }
1913                         SCTP_INP_RUNLOCK(inp);
1914                         if (error == 0) {
1915                                 ids->gaids_number_of_ids = at;
1916                                 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1917                         }
1918                         break;
1919                 }
1920         case SCTP_CONTEXT:
1921                 {
1922                         struct sctp_assoc_value *av;
1923
1924                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1925                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1926
1927                         if (stcb) {
1928                                 av->assoc_value = stcb->asoc.context;
1929                                 SCTP_TCB_UNLOCK(stcb);
1930                         } else {
1931                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1932                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1933                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1934                                         SCTP_INP_RLOCK(inp);
1935                                         av->assoc_value = inp->sctp_context;
1936                                         SCTP_INP_RUNLOCK(inp);
1937                                 } else {
1938                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1939                                         error = EINVAL;
1940                                 }
1941                         }
1942                         if (error == 0) {
1943                                 *optsize = sizeof(struct sctp_assoc_value);
1944                         }
1945                         break;
1946                 }
1947         case SCTP_VRF_ID:
1948                 {
1949                         uint32_t *default_vrfid;
1950
1951                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1952                         *default_vrfid = inp->def_vrf_id;
1953                         *optsize = sizeof(uint32_t);
1954                         break;
1955                 }
1956         case SCTP_GET_ASOC_VRF:
1957                 {
1958                         struct sctp_assoc_value *id;
1959
1960                         SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1961                         SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1962                         if (stcb == NULL) {
1963                                 error = EINVAL;
1964                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1965                         } else {
1966                                 id->assoc_value = stcb->asoc.vrf_id;
1967                                 *optsize = sizeof(struct sctp_assoc_value);
1968                         }
1969                         break;
1970                 }
1971         case SCTP_GET_VRF_IDS:
1972                 {
1973                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1974                         error = EOPNOTSUPP;
1975                         break;
1976                 }
1977         case SCTP_GET_NONCE_VALUES:
1978                 {
1979                         struct sctp_get_nonce_values *gnv;
1980
1981                         SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1982                         SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1983
1984                         if (stcb) {
1985                                 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1986                                 gnv->gn_local_tag = stcb->asoc.my_vtag;
1987                                 SCTP_TCB_UNLOCK(stcb);
1988                                 *optsize = sizeof(struct sctp_get_nonce_values);
1989                         } else {
1990                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1991                                 error = ENOTCONN;
1992                         }
1993                         break;
1994                 }
1995         case SCTP_DELAYED_SACK:
1996                 {
1997                         struct sctp_sack_info *sack;
1998
1999                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
2000                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2001                         if (stcb) {
2002                                 sack->sack_delay = stcb->asoc.delayed_ack;
2003                                 sack->sack_freq = stcb->asoc.sack_freq;
2004                                 SCTP_TCB_UNLOCK(stcb);
2005                         } else {
2006                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2007                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2008                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
2009                                         SCTP_INP_RLOCK(inp);
2010                                         sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2011                                         sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2012                                         SCTP_INP_RUNLOCK(inp);
2013                                 } else {
2014                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2015                                         error = EINVAL;
2016                                 }
2017                         }
2018                         if (error == 0) {
2019                                 *optsize = sizeof(struct sctp_sack_info);
2020                         }
2021                         break;
2022                 }
2023         case SCTP_GET_SNDBUF_USE:
2024                 {
2025                         struct sctp_sockstat *ss;
2026
2027                         SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2028                         SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2029
2030                         if (stcb) {
2031                                 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2032                                 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2033                                     stcb->asoc.size_on_all_streams);
2034                                 SCTP_TCB_UNLOCK(stcb);
2035                                 *optsize = sizeof(struct sctp_sockstat);
2036                         } else {
2037                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2038                                 error = ENOTCONN;
2039                         }
2040                         break;
2041                 }
2042         case SCTP_MAX_BURST:
2043                 {
2044                         struct sctp_assoc_value *av;
2045
2046                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2047                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2048
2049                         if (stcb) {
2050                                 av->assoc_value = stcb->asoc.max_burst;
2051                                 SCTP_TCB_UNLOCK(stcb);
2052                         } else {
2053                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2054                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2055                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2056                                         SCTP_INP_RLOCK(inp);
2057                                         av->assoc_value = inp->sctp_ep.max_burst;
2058                                         SCTP_INP_RUNLOCK(inp);
2059                                 } else {
2060                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2061                                         error = EINVAL;
2062                                 }
2063                         }
2064                         if (error == 0) {
2065                                 *optsize = sizeof(struct sctp_assoc_value);
2066                         }
2067                         break;
2068                 }
2069         case SCTP_MAXSEG:
2070                 {
2071                         struct sctp_assoc_value *av;
2072                         int ovh;
2073
2074                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2075                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2076
2077                         if (stcb) {
2078                                 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2079                                 SCTP_TCB_UNLOCK(stcb);
2080                         } else {
2081                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2082                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2083                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2084                                         SCTP_INP_RLOCK(inp);
2085                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2086                                                 ovh = SCTP_MED_OVERHEAD;
2087                                         } else {
2088                                                 ovh = SCTP_MED_V4_OVERHEAD;
2089                                         }
2090                                         if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2091                                                 av->assoc_value = 0;
2092                                         else
2093                                                 av->assoc_value = inp->sctp_frag_point - ovh;
2094                                         SCTP_INP_RUNLOCK(inp);
2095                                 } else {
2096                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2097                                         error = EINVAL;
2098                                 }
2099                         }
2100                         if (error == 0) {
2101                                 *optsize = sizeof(struct sctp_assoc_value);
2102                         }
2103                         break;
2104                 }
2105         case SCTP_GET_STAT_LOG:
2106                 error = sctp_fill_stat_log(optval, optsize);
2107                 break;
2108         case SCTP_EVENTS:
2109                 {
2110                         struct sctp_event_subscribe *events;
2111
2112                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2113                         memset(events, 0, sizeof(struct sctp_event_subscribe));
2114                         SCTP_INP_RLOCK(inp);
2115                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2116                                 events->sctp_data_io_event = 1;
2117
2118                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2119                                 events->sctp_association_event = 1;
2120
2121                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2122                                 events->sctp_address_event = 1;
2123
2124                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2125                                 events->sctp_send_failure_event = 1;
2126
2127                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2128                                 events->sctp_peer_error_event = 1;
2129
2130                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2131                                 events->sctp_shutdown_event = 1;
2132
2133                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2134                                 events->sctp_partial_delivery_event = 1;
2135
2136                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2137                                 events->sctp_adaptation_layer_event = 1;
2138
2139                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2140                                 events->sctp_authentication_event = 1;
2141
2142                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2143                                 events->sctp_sender_dry_event = 1;
2144
2145                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2146                                 events->sctp_stream_reset_event = 1;
2147                         SCTP_INP_RUNLOCK(inp);
2148                         *optsize = sizeof(struct sctp_event_subscribe);
2149                         break;
2150                 }
2151         case SCTP_ADAPTATION_LAYER:
2152                 {
2153                         uint32_t *value;
2154
2155                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2156
2157                         SCTP_INP_RLOCK(inp);
2158                         *value = inp->sctp_ep.adaptation_layer_indicator;
2159                         SCTP_INP_RUNLOCK(inp);
2160                         *optsize = sizeof(uint32_t);
2161                         break;
2162                 }
2163         case SCTP_SET_INITIAL_DBG_SEQ:
2164                 {
2165                         uint32_t *value;
2166
2167                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2168                         SCTP_INP_RLOCK(inp);
2169                         *value = inp->sctp_ep.initial_sequence_debug;
2170                         SCTP_INP_RUNLOCK(inp);
2171                         *optsize = sizeof(uint32_t);
2172                         break;
2173                 }
2174         case SCTP_GET_LOCAL_ADDR_SIZE:
2175                 {
2176                         uint32_t *value;
2177
2178                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2179                         SCTP_INP_RLOCK(inp);
2180                         *value = sctp_count_max_addresses(inp);
2181                         SCTP_INP_RUNLOCK(inp);
2182                         *optsize = sizeof(uint32_t);
2183                         break;
2184                 }
2185         case SCTP_GET_REMOTE_ADDR_SIZE:
2186                 {
2187                         uint32_t *value;
2188                         size_t size;
2189                         struct sctp_nets *net;
2190
2191                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2192                         /* FIXME MT: change to sctp_assoc_value? */
2193                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2194
2195                         if (stcb) {
2196                                 size = 0;
2197                                 /* Count the sizes */
2198                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2199                                         switch (net->ro._l_addr.sa.sa_family) {
2200 #ifdef INET
2201                                         case AF_INET:
2202 #ifdef INET6
2203                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2204                                                         size += sizeof(struct sockaddr_in6);
2205                                                 } else {
2206                                                         size += sizeof(struct sockaddr_in);
2207                                                 }
2208 #else
2209                                                 size += sizeof(struct sockaddr_in);
2210 #endif
2211                                                 break;
2212 #endif
2213 #ifdef INET6
2214                                         case AF_INET6:
2215                                                 size += sizeof(struct sockaddr_in6);
2216                                                 break;
2217 #endif
2218                                         default:
2219                                                 break;
2220                                         }
2221                                 }
2222                                 SCTP_TCB_UNLOCK(stcb);
2223                                 *value = (uint32_t) size;
2224                                 *optsize = sizeof(uint32_t);
2225                         } else {
2226                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2227                                 error = ENOTCONN;
2228                         }
2229                         break;
2230                 }
2231         case SCTP_GET_PEER_ADDRESSES:
2232                 /*
2233                  * Get the address information, an array is passed in to
2234                  * fill up we pack it.
2235                  */
2236                 {
2237                         size_t cpsz, left;
2238                         struct sockaddr_storage *sas;
2239                         struct sctp_nets *net;
2240                         struct sctp_getaddresses *saddr;
2241
2242                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2243                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2244
2245                         if (stcb) {
2246                                 left = (*optsize) - sizeof(struct sctp_getaddresses);
2247                                 *optsize = sizeof(struct sctp_getaddresses);
2248                                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2249
2250                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2251                                         switch (net->ro._l_addr.sa.sa_family) {
2252 #ifdef INET
2253                                         case AF_INET:
2254 #ifdef INET6
2255                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2256                                                         cpsz = sizeof(struct sockaddr_in6);
2257                                                 } else {
2258                                                         cpsz = sizeof(struct sockaddr_in);
2259                                                 }
2260 #else
2261                                                 cpsz = sizeof(struct sockaddr_in);
2262 #endif
2263                                                 break;
2264 #endif
2265 #ifdef INET6
2266                                         case AF_INET6:
2267                                                 cpsz = sizeof(struct sockaddr_in6);
2268                                                 break;
2269 #endif
2270                                         default:
2271                                                 cpsz = 0;
2272                                                 break;
2273                                         }
2274                                         if (cpsz == 0) {
2275                                                 break;
2276                                         }
2277                                         if (left < cpsz) {
2278                                                 /* not enough room. */
2279                                                 break;
2280                                         }
2281 #if defined(INET) && defined(INET6)
2282                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2283                                             (net->ro._l_addr.sa.sa_family == AF_INET)) {
2284                                                 /* Must map the address */
2285                                                 in6_sin_2_v4mapsin6(&net->ro._l_addr.sin,
2286                                                     (struct sockaddr_in6 *)sas);
2287                                         } else {
2288                                                 memcpy(sas, &net->ro._l_addr, cpsz);
2289                                         }
2290 #else
2291                                         memcpy(sas, &net->ro._l_addr, cpsz);
2292 #endif
2293                                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2294
2295                                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2296                                         left -= cpsz;
2297                                         *optsize += cpsz;
2298                                 }
2299                                 SCTP_TCB_UNLOCK(stcb);
2300                         } else {
2301                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2302                                 error = ENOENT;
2303                         }
2304                         break;
2305                 }
2306         case SCTP_GET_LOCAL_ADDRESSES:
2307                 {
2308                         size_t limit, actual;
2309                         struct sockaddr_storage *sas;
2310                         struct sctp_getaddresses *saddr;
2311
2312                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2313                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2314
2315                         sas = (struct sockaddr_storage *)&saddr->addr[0];
2316                         limit = *optsize - sizeof(sctp_assoc_t);
2317                         actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2318                         if (stcb) {
2319                                 SCTP_TCB_UNLOCK(stcb);
2320                         }
2321                         *optsize = sizeof(struct sockaddr_storage) + actual;
2322                         break;
2323                 }
2324         case SCTP_PEER_ADDR_PARAMS:
2325                 {
2326                         struct sctp_paddrparams *paddrp;
2327                         struct sctp_nets *net;
2328                         struct sockaddr *addr;
2329
2330 #if defined(INET) && defined(INET6)
2331                         struct sockaddr_in sin_store;
2332
2333 #endif
2334
2335                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2336                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2337
2338 #if defined(INET) && defined(INET6)
2339                         if (paddrp->spp_address.ss_family == AF_INET6) {
2340                                 struct sockaddr_in6 *sin6;
2341
2342                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
2343                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2344                                         in6_sin6_2_sin(&sin_store, sin6);
2345                                         addr = (struct sockaddr *)&sin_store;
2346                                 } else {
2347                                         addr = (struct sockaddr *)&paddrp->spp_address;
2348                                 }
2349                         } else {
2350                                 addr = (struct sockaddr *)&paddrp->spp_address;
2351                         }
2352 #else
2353                         addr = (struct sockaddr *)&paddrp->spp_address;
2354 #endif
2355                         if (stcb != NULL) {
2356                                 net = sctp_findnet(stcb, addr);
2357                         } else {
2358                                 /*
2359                                  * We increment here since
2360                                  * sctp_findassociation_ep_addr() wil do a
2361                                  * decrement if it finds the stcb as long as
2362                                  * the locked tcb (last argument) is NOT a
2363                                  * TCB.. aka NULL.
2364                                  */
2365                                 net = NULL;
2366                                 SCTP_INP_INCR_REF(inp);
2367                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2368                                 if (stcb == NULL) {
2369                                         SCTP_INP_DECR_REF(inp);
2370                                 }
2371                         }
2372                         if ((stcb != NULL) && (net == NULL)) {
2373 #ifdef INET
2374                                 if (addr->sa_family == AF_INET) {
2375                                         struct sockaddr_in *sin;
2376
2377                                         sin = (struct sockaddr_in *)addr;
2378                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
2379                                                 error = EINVAL;
2380                                                 SCTP_TCB_UNLOCK(stcb);
2381                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2382                                                 break;
2383                                         }
2384                                 } else
2385 #endif
2386 #ifdef INET6
2387                                 if (addr->sa_family == AF_INET6) {
2388                                         struct sockaddr_in6 *sin6;
2389
2390                                         sin6 = (struct sockaddr_in6 *)addr;
2391                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2392                                                 error = EINVAL;
2393                                                 SCTP_TCB_UNLOCK(stcb);
2394                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2395                                                 break;
2396                                         }
2397                                 } else
2398 #endif
2399                                 {
2400                                         error = EAFNOSUPPORT;
2401                                         SCTP_TCB_UNLOCK(stcb);
2402                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2403                                         break;
2404                                 }
2405                         }
2406                         if (stcb != NULL) {
2407                                 /* Applies to the specific association */
2408                                 paddrp->spp_flags = 0;
2409                                 if (net != NULL) {
2410                                         paddrp->spp_hbinterval = net->heart_beat_delay;
2411                                         paddrp->spp_pathmaxrxt = net->failure_threshold;
2412                                         paddrp->spp_pathmtu = net->mtu;
2413                                         switch (net->ro._l_addr.sa.sa_family) {
2414 #ifdef INET
2415                                         case AF_INET:
2416                                                 paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2417                                                 break;
2418 #endif
2419 #ifdef INET6
2420                                         case AF_INET6:
2421                                                 paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2422                                                 break;
2423 #endif
2424                                         default:
2425                                                 break;
2426                                         }
2427                                         /* get flags for HB */
2428                                         if (net->dest_state & SCTP_ADDR_NOHB) {
2429                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2430                                         } else {
2431                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2432                                         }
2433                                         /* get flags for PMTU */
2434                                         if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2435                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2436                                         } else {
2437                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2438                                         }
2439                                         if (net->dscp & 0x01) {
2440                                                 paddrp->spp_dscp = net->dscp & 0xfc;
2441                                                 paddrp->spp_flags |= SPP_DSCP;
2442                                         }
2443 #ifdef INET6
2444                                         if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2445                                             (net->flowlabel & 0x80000000)) {
2446                                                 paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2447                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2448                                         }
2449 #endif
2450                                 } else {
2451                                         /*
2452                                          * No destination so return default
2453                                          * value
2454                                          */
2455                                         paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2456                                         paddrp->spp_pathmtu = 0;
2457                                         if (stcb->asoc.default_dscp & 0x01) {
2458                                                 paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2459                                                 paddrp->spp_flags |= SPP_DSCP;
2460                                         }
2461 #ifdef INET6
2462                                         if (stcb->asoc.default_flowlabel & 0x80000000) {
2463                                                 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2464                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2465                                         }
2466 #endif
2467                                         /* default settings should be these */
2468                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2469                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2470                                         } else {
2471                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2472                                         }
2473                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2474                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2475                                         } else {
2476                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2477                                         }
2478                                         paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2479                                 }
2480                                 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2481                                 SCTP_TCB_UNLOCK(stcb);
2482                         } else {
2483                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2484                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2485                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2486                                         /* Use endpoint defaults */
2487                                         SCTP_INP_RLOCK(inp);
2488                                         paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2489                                         paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2490                                         paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2491                                         /* get inp's default */
2492                                         if (inp->sctp_ep.default_dscp & 0x01) {
2493                                                 paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2494                                                 paddrp->spp_flags |= SPP_DSCP;
2495                                         }
2496 #ifdef INET6
2497                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2498                                             (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2499                                                 paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2500                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2501                                         }
2502 #endif
2503                                         /* can't return this */
2504                                         paddrp->spp_pathmtu = 0;
2505
2506                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2507                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2508                                         } else {
2509                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2510                                         }
2511                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2512                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2513                                         } else {
2514                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2515                                         }
2516                                         SCTP_INP_RUNLOCK(inp);
2517                                 } else {
2518                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2519                                         error = EINVAL;
2520                                 }
2521                         }
2522                         if (error == 0) {
2523                                 *optsize = sizeof(struct sctp_paddrparams);
2524                         }
2525                         break;
2526                 }
2527         case SCTP_GET_PEER_ADDR_INFO:
2528                 {
2529                         struct sctp_paddrinfo *paddri;
2530                         struct sctp_nets *net;
2531                         struct sockaddr *addr;
2532
2533 #if defined(INET) && defined(INET6)
2534                         struct sockaddr_in sin_store;
2535
2536 #endif
2537
2538                         SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2539                         SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2540
2541 #if defined(INET) && defined(INET6)
2542                         if (paddri->spinfo_address.ss_family == AF_INET6) {
2543                                 struct sockaddr_in6 *sin6;
2544
2545                                 sin6 = (struct sockaddr_in6 *)&paddri->spinfo_address;
2546                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2547                                         in6_sin6_2_sin(&sin_store, sin6);
2548                                         addr = (struct sockaddr *)&sin_store;
2549                                 } else {
2550                                         addr = (struct sockaddr *)&paddri->spinfo_address;
2551                                 }
2552                         } else {
2553                                 addr = (struct sockaddr *)&paddri->spinfo_address;
2554                         }
2555 #else
2556                         addr = (struct sockaddr *)&paddri->spinfo_address;
2557 #endif
2558                         if (stcb != NULL) {
2559                                 net = sctp_findnet(stcb, addr);
2560                         } else {
2561                                 /*
2562                                  * We increment here since
2563                                  * sctp_findassociation_ep_addr() wil do a
2564                                  * decrement if it finds the stcb as long as
2565                                  * the locked tcb (last argument) is NOT a
2566                                  * TCB.. aka NULL.
2567                                  */
2568                                 net = NULL;
2569                                 SCTP_INP_INCR_REF(inp);
2570                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2571                                 if (stcb == NULL) {
2572                                         SCTP_INP_DECR_REF(inp);
2573                                 }
2574                         }
2575
2576                         if ((stcb != NULL) && (net != NULL)) {
2577                                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2578                                         /* It's unconfirmed */
2579                                         paddri->spinfo_state = SCTP_UNCONFIRMED;
2580                                 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2581                                         /* It's active */
2582                                         paddri->spinfo_state = SCTP_ACTIVE;
2583                                 } else {
2584                                         /* It's inactive */
2585                                         paddri->spinfo_state = SCTP_INACTIVE;
2586                                 }
2587                                 paddri->spinfo_cwnd = net->cwnd;
2588                                 paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2589                                 paddri->spinfo_rto = net->RTO;
2590                                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2591                                 paddri->spinfo_mtu = net->mtu;
2592                                 switch (addr->sa_family) {
2593 #if defined(INET)
2594                                 case AF_INET:
2595                                         paddri->spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2596                                         break;
2597 #endif
2598 #if defined(INET6)
2599                                 case AF_INET6:
2600                                         paddri->spinfo_mtu -= SCTP_MIN_OVERHEAD;
2601                                         break;
2602 #endif
2603                                 default:
2604                                         break;
2605                                 }
2606                                 SCTP_TCB_UNLOCK(stcb);
2607                                 *optsize = sizeof(struct sctp_paddrinfo);
2608                         } else {
2609                                 if (stcb != NULL) {
2610                                         SCTP_TCB_UNLOCK(stcb);
2611                                 }
2612                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2613                                 error = ENOENT;
2614                         }
2615                         break;
2616                 }
2617         case SCTP_PCB_STATUS:
2618                 {
2619                         struct sctp_pcbinfo *spcb;
2620
2621                         SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2622                         sctp_fill_pcbinfo(spcb);
2623                         *optsize = sizeof(struct sctp_pcbinfo);
2624                         break;
2625                 }
2626         case SCTP_STATUS:
2627                 {
2628                         struct sctp_nets *net;
2629                         struct sctp_status *sstat;
2630
2631                         SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2632                         SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2633
2634                         if (stcb == NULL) {
2635                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2636                                 error = EINVAL;
2637                                 break;
2638                         }
2639                         sstat->sstat_state = sctp_map_assoc_state(stcb->asoc.state);
2640                         sstat->sstat_assoc_id = sctp_get_associd(stcb);
2641                         sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2642                         sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2643                         /*
2644                          * We can't include chunks that have been passed to
2645                          * the socket layer. Only things in queue.
2646                          */
2647                         sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2648                             stcb->asoc.cnt_on_all_streams);
2649
2650
2651                         sstat->sstat_instrms = stcb->asoc.streamincnt;
2652                         sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2653                         sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2654                         memcpy(&sstat->sstat_primary.spinfo_address,
2655                             &stcb->asoc.primary_destination->ro._l_addr,
2656                             ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2657                         net = stcb->asoc.primary_destination;
2658                         ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2659                         /*
2660                          * Again the user can get info from sctp_constants.h
2661                          * for what the state of the network is.
2662                          */
2663                         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2664                                 /* It's unconfirmed */
2665                                 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2666                         } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2667                                 /* It's active */
2668                                 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2669                         } else {
2670                                 /* It's inactive */
2671                                 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2672                         }
2673                         sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2674                         sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2675                         sstat->sstat_primary.spinfo_rto = net->RTO;
2676                         sstat->sstat_primary.spinfo_mtu = net->mtu;
2677                         switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
2678 #if defined(INET)
2679                         case AF_INET:
2680                                 sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2681                                 break;
2682 #endif
2683 #if defined(INET6)
2684                         case AF_INET6:
2685                                 sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_OVERHEAD;
2686                                 break;
2687 #endif
2688                         default:
2689                                 break;
2690                         }
2691                         sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2692                         SCTP_TCB_UNLOCK(stcb);
2693                         *optsize = sizeof(struct sctp_status);
2694                         break;
2695                 }
2696         case SCTP_RTOINFO:
2697                 {
2698                         struct sctp_rtoinfo *srto;
2699
2700                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2701                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2702
2703                         if (stcb) {
2704                                 srto->srto_initial = stcb->asoc.initial_rto;
2705                                 srto->srto_max = stcb->asoc.maxrto;
2706                                 srto->srto_min = stcb->asoc.minrto;
2707                                 SCTP_TCB_UNLOCK(stcb);
2708                         } else {
2709                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2710                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2711                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
2712                                         SCTP_INP_RLOCK(inp);
2713                                         srto->srto_initial = inp->sctp_ep.initial_rto;
2714                                         srto->srto_max = inp->sctp_ep.sctp_maxrto;
2715                                         srto->srto_min = inp->sctp_ep.sctp_minrto;
2716                                         SCTP_INP_RUNLOCK(inp);
2717                                 } else {
2718                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2719                                         error = EINVAL;
2720                                 }
2721                         }
2722                         if (error == 0) {
2723                                 *optsize = sizeof(struct sctp_rtoinfo);
2724                         }
2725                         break;
2726                 }
2727         case SCTP_TIMEOUTS:
2728                 {
2729                         struct sctp_timeouts *stimo;
2730
2731                         SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2732                         SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2733
2734                         if (stcb) {
2735                                 stimo->stimo_init = stcb->asoc.timoinit;
2736                                 stimo->stimo_data = stcb->asoc.timodata;
2737                                 stimo->stimo_sack = stcb->asoc.timosack;
2738                                 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2739                                 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2740                                 stimo->stimo_cookie = stcb->asoc.timocookie;
2741                                 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2742                                 SCTP_TCB_UNLOCK(stcb);
2743                                 *optsize = sizeof(struct sctp_timeouts);
2744                         } else {
2745                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2746                                 error = EINVAL;
2747                         }
2748                         break;
2749                 }
2750         case SCTP_ASSOCINFO:
2751                 {
2752                         struct sctp_assocparams *sasoc;
2753
2754                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2755                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2756
2757                         if (stcb) {
2758                                 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2759                                 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2760                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2761                                 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2762                                 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2763                                 SCTP_TCB_UNLOCK(stcb);
2764                         } else {
2765                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2766                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2767                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
2768                                         SCTP_INP_RLOCK(inp);
2769                                         sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2770                                         sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2771                                         sasoc->sasoc_number_peer_destinations = 0;
2772                                         sasoc->sasoc_peer_rwnd = 0;
2773                                         sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2774                                         SCTP_INP_RUNLOCK(inp);
2775                                 } else {
2776                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2777                                         error = EINVAL;
2778                                 }
2779                         }
2780                         if (error == 0) {
2781                                 *optsize = sizeof(struct sctp_assocparams);
2782                         }
2783                         break;
2784                 }
2785         case SCTP_DEFAULT_SEND_PARAM:
2786                 {
2787                         struct sctp_sndrcvinfo *s_info;
2788
2789                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2790                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2791
2792                         if (stcb) {
2793                                 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2794                                 SCTP_TCB_UNLOCK(stcb);
2795                         } else {
2796                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2797                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2798                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
2799                                         SCTP_INP_RLOCK(inp);
2800                                         memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2801                                         SCTP_INP_RUNLOCK(inp);
2802                                 } else {
2803                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2804                                         error = EINVAL;
2805                                 }
2806                         }
2807                         if (error == 0) {
2808                                 *optsize = sizeof(struct sctp_sndrcvinfo);
2809                         }
2810                         break;
2811                 }
2812         case SCTP_INITMSG:
2813                 {
2814                         struct sctp_initmsg *sinit;
2815
2816                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2817                         SCTP_INP_RLOCK(inp);
2818                         sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2819                         sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2820                         sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2821                         sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2822                         SCTP_INP_RUNLOCK(inp);
2823                         *optsize = sizeof(struct sctp_initmsg);
2824                         break;
2825                 }
2826         case SCTP_PRIMARY_ADDR:
2827                 /* we allow a "get" operation on this */
2828                 {
2829                         struct sctp_setprim *ssp;
2830
2831                         SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2832                         SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2833
2834                         if (stcb) {
2835                                 union sctp_sockstore *addr;
2836
2837                                 addr = &stcb->asoc.primary_destination->ro._l_addr;
2838                                 switch (addr->sa.sa_family) {
2839 #ifdef INET
2840                                 case AF_INET:
2841 #ifdef INET6
2842                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2843                                                 in6_sin_2_v4mapsin6(&addr->sin,
2844                                                     (struct sockaddr_in6 *)&ssp->ssp_addr);
2845                                         } else {
2846                                                 memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2847                                         }
2848 #else
2849                                         memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2850 #endif
2851                                         break;
2852 #endif
2853 #ifdef INET6
2854                                 case AF_INET6:
2855                                         memcpy(&ssp->ssp_addr, &addr->sin6, sizeof(struct sockaddr_in6));
2856                                         break;
2857 #endif
2858                                 default:
2859                                         break;
2860                                 }
2861                                 SCTP_TCB_UNLOCK(stcb);
2862                                 *optsize = sizeof(struct sctp_setprim);
2863                         } else {
2864                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2865                                 error = EINVAL;
2866                         }
2867                         break;
2868                 }
2869         case SCTP_HMAC_IDENT:
2870                 {
2871                         struct sctp_hmacalgo *shmac;
2872                         sctp_hmaclist_t *hmaclist;
2873                         uint32_t size;
2874                         int i;
2875
2876                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2877
2878                         SCTP_INP_RLOCK(inp);
2879                         hmaclist = inp->sctp_ep.local_hmacs;
2880                         if (hmaclist == NULL) {
2881                                 /* no HMACs to return */
2882                                 *optsize = sizeof(*shmac);
2883                                 SCTP_INP_RUNLOCK(inp);
2884                                 break;
2885                         }
2886                         /* is there room for all of the hmac ids? */
2887                         size = sizeof(*shmac) + (hmaclist->num_algo *
2888                             sizeof(shmac->shmac_idents[0]));
2889                         if ((size_t)(*optsize) < size) {
2890                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2891                                 error = EINVAL;
2892                                 SCTP_INP_RUNLOCK(inp);
2893                                 break;
2894                         }
2895                         /* copy in the list */
2896                         shmac->shmac_number_of_idents = hmaclist->num_algo;
2897                         for (i = 0; i < hmaclist->num_algo; i++) {
2898                                 shmac->shmac_idents[i] = hmaclist->hmac[i];
2899                         }
2900                         SCTP_INP_RUNLOCK(inp);
2901                         *optsize = size;
2902                         break;
2903                 }
2904         case SCTP_AUTH_ACTIVE_KEY:
2905                 {
2906                         struct sctp_authkeyid *scact;
2907
2908                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2909                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2910
2911                         if (stcb) {
2912                                 /* get the active key on the assoc */
2913                                 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2914                                 SCTP_TCB_UNLOCK(stcb);
2915                         } else {
2916                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2917                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2918                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
2919                                         /* get the endpoint active key */
2920                                         SCTP_INP_RLOCK(inp);
2921                                         scact->scact_keynumber = inp->sctp_ep.default_keyid;
2922                                         SCTP_INP_RUNLOCK(inp);
2923                                 } else {
2924                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2925                                         error = EINVAL;
2926                                 }
2927                         }
2928                         if (error == 0) {
2929                                 *optsize = sizeof(struct sctp_authkeyid);
2930                         }
2931                         break;
2932                 }
2933         case SCTP_LOCAL_AUTH_CHUNKS:
2934                 {
2935                         struct sctp_authchunks *sac;
2936                         sctp_auth_chklist_t *chklist = NULL;
2937                         size_t size = 0;
2938
2939                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2940                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2941
2942                         if (stcb) {
2943                                 /* get off the assoc */
2944                                 chklist = stcb->asoc.local_auth_chunks;
2945                                 /* is there enough space? */
2946                                 size = sctp_auth_get_chklist_size(chklist);
2947                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2948                                         error = EINVAL;
2949                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2950                                 } else {
2951                                         /* copy in the chunks */
2952                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2953                                         sac->gauth_number_of_chunks = (uint32_t) size;
2954                                         *optsize = sizeof(struct sctp_authchunks) + size;
2955                                 }
2956                                 SCTP_TCB_UNLOCK(stcb);
2957                         } else {
2958                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2959                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2960                                     (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
2961                                         /* get off the endpoint */
2962                                         SCTP_INP_RLOCK(inp);
2963                                         chklist = inp->sctp_ep.local_auth_chunks;
2964                                         /* is there enough space? */
2965                                         size = sctp_auth_get_chklist_size(chklist);
2966                                         if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2967                                                 error = EINVAL;
2968                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2969                                         } else {
2970                                                 /* copy in the chunks */
2971                                                 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2972                                                 sac->gauth_number_of_chunks = (uint32_t) size;
2973                                                 *optsize = sizeof(struct sctp_authchunks) + size;
2974                                         }
2975                                         SCTP_INP_RUNLOCK(inp);
2976                                 } else {
2977                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2978                                         error = EINVAL;
2979                                 }
2980                         }
2981                         break;
2982                 }
2983         case SCTP_PEER_AUTH_CHUNKS:
2984                 {
2985                         struct sctp_authchunks *sac;
2986                         sctp_auth_chklist_t *chklist = NULL;
2987                         size_t size = 0;
2988
2989                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2990                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2991
2992                         if (stcb) {
2993                                 /* get off the assoc */
2994                                 chklist = stcb->asoc.peer_auth_chunks;
2995                                 /* is there enough space? */
2996                                 size = sctp_auth_get_chklist_size(chklist);
2997                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2998                                         error = EINVAL;
2999                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3000                                 } else {
3001                                         /* copy in the chunks */
3002                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3003                                         sac->gauth_number_of_chunks = (uint32_t) size;
3004                                         *optsize = sizeof(struct sctp_authchunks) + size;
3005                                 }
3006                                 SCTP_TCB_UNLOCK(stcb);
3007                         } else {
3008                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3009                                 error = ENOENT;
3010                         }
3011                         break;
3012                 }
3013         case SCTP_EVENT:
3014                 {
3015                         struct sctp_event *event;
3016                         uint32_t event_type;
3017
3018                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
3019                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
3020
3021                         switch (event->se_type) {
3022                         case SCTP_ASSOC_CHANGE:
3023                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
3024                                 break;
3025                         case SCTP_PEER_ADDR_CHANGE:
3026                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
3027                                 break;
3028                         case SCTP_REMOTE_ERROR:
3029                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
3030                                 break;
3031                         case SCTP_SEND_FAILED:
3032                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3033                                 break;
3034                         case SCTP_SHUTDOWN_EVENT:
3035                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3036                                 break;
3037                         case SCTP_ADAPTATION_INDICATION:
3038                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
3039                                 break;
3040                         case SCTP_PARTIAL_DELIVERY_EVENT:
3041                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
3042                                 break;
3043                         case SCTP_AUTHENTICATION_EVENT:
3044                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
3045                                 break;
3046                         case SCTP_STREAM_RESET_EVENT:
3047                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3048                                 break;
3049                         case SCTP_SENDER_DRY_EVENT:
3050                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
3051                                 break;
3052                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
3053                                 event_type = 0;
3054                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
3055                                 error = ENOTSUP;
3056                                 break;
3057                         case SCTP_ASSOC_RESET_EVENT:
3058                                 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
3059                                 break;
3060                         case SCTP_STREAM_CHANGE_EVENT:
3061                                 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
3062                                 break;
3063                         case SCTP_SEND_FAILED_EVENT:
3064                                 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
3065                                 break;
3066                         default:
3067                                 event_type = 0;
3068                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3069                                 error = EINVAL;
3070                                 break;
3071                         }
3072                         if (event_type > 0) {
3073                                 if (stcb) {
3074                                         event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
3075                                         SCTP_TCB_UNLOCK(stcb);
3076                                 } else {
3077                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3078                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3079                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3080                                                 SCTP_INP_RLOCK(inp);
3081                                                 event->se_on = sctp_is_feature_on(inp, event_type);
3082                                                 SCTP_INP_RUNLOCK(inp);
3083                                         } else {
3084                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3085                                                 error = EINVAL;
3086                                         }
3087                                 }
3088                         }
3089                         if (error == 0) {
3090                                 *optsize = sizeof(struct sctp_event);
3091                         }
3092                         break;
3093                 }
3094         case SCTP_RECVRCVINFO:
3095                 {
3096                         int onoff;
3097
3098                         if (*optsize < sizeof(int)) {
3099                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3100                                 error = EINVAL;
3101                         } else {
3102                                 SCTP_INP_RLOCK(inp);
3103                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3104                                 SCTP_INP_RUNLOCK(inp);
3105                         }
3106                         if (error == 0) {
3107                                 /* return the option value */
3108                                 *(int *)optval = onoff;
3109                                 *optsize = sizeof(int);
3110                         }
3111                         break;
3112                 }
3113         case SCTP_RECVNXTINFO:
3114                 {
3115                         int onoff;
3116
3117                         if (*optsize < sizeof(int)) {
3118                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3119                                 error = EINVAL;
3120                         } else {
3121                                 SCTP_INP_RLOCK(inp);
3122                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3123                                 SCTP_INP_RUNLOCK(inp);
3124                         }
3125                         if (error == 0) {
3126                                 /* return the option value */
3127                                 *(int *)optval = onoff;
3128                                 *optsize = sizeof(int);
3129                         }
3130                         break;
3131                 }
3132         case SCTP_DEFAULT_SNDINFO:
3133                 {
3134                         struct sctp_sndinfo *info;
3135
3136                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3137                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3138
3139                         if (stcb) {
3140                                 info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3141                                 info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3142                                 info->snd_flags &= 0xfff0;
3143                                 info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3144                                 info->snd_context = stcb->asoc.def_send.sinfo_context;
3145                                 SCTP_TCB_UNLOCK(stcb);
3146                         } else {
3147                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3148                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3149                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3150                                         SCTP_INP_RLOCK(inp);
3151                                         info->snd_sid = inp->def_send.sinfo_stream;
3152                                         info->snd_flags = inp->def_send.sinfo_flags;
3153                                         info->snd_flags &= 0xfff0;
3154                                         info->snd_ppid = inp->def_send.sinfo_ppid;
3155                                         info->snd_context = inp->def_send.sinfo_context;
3156                                         SCTP_INP_RUNLOCK(inp);
3157                                 } else {
3158                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3159                                         error = EINVAL;
3160                                 }
3161                         }
3162                         if (error == 0) {
3163                                 *optsize = sizeof(struct sctp_sndinfo);
3164                         }
3165                         break;
3166                 }
3167         case SCTP_DEFAULT_PRINFO:
3168                 {
3169                         struct sctp_default_prinfo *info;
3170
3171                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3172                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3173
3174                         if (stcb) {
3175                                 info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3176                                 info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3177                                 SCTP_TCB_UNLOCK(stcb);
3178                         } else {
3179                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3180                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3181                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3182                                         SCTP_INP_RLOCK(inp);
3183                                         info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3184                                         info->pr_value = inp->def_send.sinfo_timetolive;
3185                                         SCTP_INP_RUNLOCK(inp);
3186                                 } else {
3187                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3188                                         error = EINVAL;
3189                                 }
3190                         }
3191                         if (error == 0) {
3192                                 *optsize = sizeof(struct sctp_default_prinfo);
3193                         }
3194                         break;
3195                 }
3196         case SCTP_PEER_ADDR_THLDS:
3197                 {
3198                         struct sctp_paddrthlds *thlds;
3199                         struct sctp_nets *net;
3200                         struct sockaddr *addr;
3201
3202 #if defined(INET) && defined(INET6)
3203                         struct sockaddr_in sin_store;
3204
3205 #endif
3206
3207                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3208                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3209
3210 #if defined(INET) && defined(INET6)
3211                         if (thlds->spt_address.ss_family == AF_INET6) {
3212                                 struct sockaddr_in6 *sin6;
3213
3214                                 sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
3215                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3216                                         in6_sin6_2_sin(&sin_store, sin6);
3217                                         addr = (struct sockaddr *)&sin_store;
3218                                 } else {
3219                                         addr = (struct sockaddr *)&thlds->spt_address;
3220                                 }
3221                         } else {
3222                                 addr = (struct sockaddr *)&thlds->spt_address;
3223                         }
3224 #else
3225                         addr = (struct sockaddr *)&thlds->spt_address;
3226 #endif
3227                         if (stcb != NULL) {
3228                                 net = sctp_findnet(stcb, addr);
3229                         } else {
3230                                 /*
3231                                  * We increment here since
3232                                  * sctp_findassociation_ep_addr() wil do a
3233                                  * decrement if it finds the stcb as long as
3234                                  * the locked tcb (last argument) is NOT a
3235                                  * TCB.. aka NULL.
3236                                  */
3237                                 net = NULL;
3238                                 SCTP_INP_INCR_REF(inp);
3239                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3240                                 if (stcb == NULL) {
3241                                         SCTP_INP_DECR_REF(inp);
3242                                 }
3243                         }
3244                         if ((stcb != NULL) && (net == NULL)) {
3245 #ifdef INET
3246                                 if (addr->sa_family == AF_INET) {
3247                                         struct sockaddr_in *sin;
3248
3249                                         sin = (struct sockaddr_in *)addr;
3250                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
3251                                                 error = EINVAL;
3252                                                 SCTP_TCB_UNLOCK(stcb);
3253                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3254                                                 break;
3255                                         }
3256                                 } else
3257 #endif
3258 #ifdef INET6
3259                                 if (addr->sa_family == AF_INET6) {
3260                                         struct sockaddr_in6 *sin6;
3261
3262                                         sin6 = (struct sockaddr_in6 *)addr;
3263                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3264                                                 error = EINVAL;
3265                                                 SCTP_TCB_UNLOCK(stcb);
3266                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3267                                                 break;
3268                                         }
3269                                 } else
3270 #endif
3271                                 {
3272                                         error = EAFNOSUPPORT;
3273                                         SCTP_TCB_UNLOCK(stcb);
3274                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3275                                         break;
3276                                 }
3277                         }
3278                         if (stcb != NULL) {
3279                                 if (net != NULL) {
3280                                         thlds->spt_pathmaxrxt = net->failure_threshold;
3281                                         thlds->spt_pathpfthld = net->pf_threshold;
3282                                 } else {
3283                                         thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3284                                         thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3285                                 }
3286                                 thlds->spt_assoc_id = sctp_get_associd(stcb);
3287                                 SCTP_TCB_UNLOCK(stcb);
3288                         } else {
3289                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3290                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3291                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3292                                         /* Use endpoint defaults */
3293                                         SCTP_INP_RLOCK(inp);
3294                                         thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3295                                         thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3296                                         SCTP_INP_RUNLOCK(inp);
3297                                 } else {
3298                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3299                                         error = EINVAL;
3300                                 }
3301                         }
3302                         if (error == 0) {
3303                                 *optsize = sizeof(struct sctp_paddrthlds);
3304                         }
3305                         break;
3306                 }
3307         case SCTP_REMOTE_UDP_ENCAPS_PORT:
3308                 {
3309                         struct sctp_udpencaps *encaps;
3310                         struct sctp_nets *net;
3311                         struct sockaddr *addr;
3312
3313 #if defined(INET) && defined(INET6)
3314                         struct sockaddr_in sin_store;
3315
3316 #endif
3317
3318                         SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3319                         SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3320
3321 #if defined(INET) && defined(INET6)
3322                         if (encaps->sue_address.ss_family == AF_INET6) {
3323                                 struct sockaddr_in6 *sin6;
3324
3325                                 sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
3326                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3327                                         in6_sin6_2_sin(&sin_store, sin6);
3328                                         addr = (struct sockaddr *)&sin_store;
3329                                 } else {
3330                                         addr = (struct sockaddr *)&encaps->sue_address;
3331                                 }
3332                         } else {
3333                                 addr = (struct sockaddr *)&encaps->sue_address;
3334                         }
3335 #else
3336                         addr = (struct sockaddr *)&encaps->sue_address;
3337 #endif
3338                         if (stcb) {
3339                                 net = sctp_findnet(stcb, addr);
3340                         } else {
3341                                 /*
3342                                  * We increment here since
3343                                  * sctp_findassociation_ep_addr() wil do a
3344                                  * decrement if it finds the stcb as long as
3345                                  * the locked tcb (last argument) is NOT a
3346                                  * TCB.. aka NULL.
3347                                  */
3348                                 net = NULL;
3349                                 SCTP_INP_INCR_REF(inp);
3350                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3351                                 if (stcb == NULL) {
3352                                         SCTP_INP_DECR_REF(inp);
3353                                 }
3354                         }
3355                         if ((stcb != NULL) && (net == NULL)) {
3356 #ifdef INET
3357                                 if (addr->sa_family == AF_INET) {
3358                                         struct sockaddr_in *sin;
3359
3360                                         sin = (struct sockaddr_in *)addr;
3361                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
3362                                                 error = EINVAL;
3363                                                 SCTP_TCB_UNLOCK(stcb);
3364                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3365                                                 break;
3366                                         }
3367                                 } else
3368 #endif
3369 #ifdef INET6
3370                                 if (addr->sa_family == AF_INET6) {
3371                                         struct sockaddr_in6 *sin6;
3372
3373                                         sin6 = (struct sockaddr_in6 *)addr;
3374                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3375                                                 error = EINVAL;
3376                                                 SCTP_TCB_UNLOCK(stcb);
3377                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3378                                                 break;
3379                                         }
3380                                 } else
3381 #endif
3382                                 {
3383                                         error = EAFNOSUPPORT;
3384                                         SCTP_TCB_UNLOCK(stcb);
3385                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3386                                         break;
3387                                 }
3388                         }
3389                         if (stcb != NULL) {
3390                                 if (net) {
3391                                         encaps->sue_port = net->port;
3392                                 } else {
3393                                         encaps->sue_port = stcb->asoc.port;
3394                                 }
3395                                 SCTP_TCB_UNLOCK(stcb);
3396                         } else {
3397                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3398                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3399                                     (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3400                                         SCTP_INP_RLOCK(inp);
3401                                         encaps->sue_port = inp->sctp_ep.port;
3402                                         SCTP_INP_RUNLOCK(inp);
3403                                 } else {
3404                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3405                                         error = EINVAL;
3406                                 }
3407                         }
3408                         if (error == 0) {
3409                                 *optsize = sizeof(struct sctp_udpencaps);
3410                         }
3411                         break;
3412                 }
3413         case SCTP_ECN_SUPPORTED:
3414                 {
3415                         struct sctp_assoc_value *av;
3416
3417                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3418                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3419
3420                         if (stcb) {
3421                                 av->assoc_value = stcb->asoc.ecn_supported;
3422                                 SCTP_TCB_UNLOCK(stcb);
3423                         } else {
3424                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3425                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3426                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3427                                         SCTP_INP_RLOCK(inp);
3428                                         av->assoc_value = inp->ecn_supported;
3429                                         SCTP_INP_RUNLOCK(inp);
3430                                 } else {
3431                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3432                                         error = EINVAL;
3433                                 }
3434                         }
3435                         if (error == 0) {
3436                                 *optsize = sizeof(struct sctp_assoc_value);
3437                         }
3438                         break;
3439                 }
3440         case SCTP_PR_SUPPORTED:
3441                 {
3442                         struct sctp_assoc_value *av;
3443
3444                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3445                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3446
3447                         if (stcb) {
3448                                 av->assoc_value = stcb->asoc.prsctp_supported;
3449                                 SCTP_TCB_UNLOCK(stcb);
3450                         } else {
3451                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3452                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3453                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3454                                         SCTP_INP_RLOCK(inp);
3455                                         av->assoc_value = inp->prsctp_supported;
3456                                         SCTP_INP_RUNLOCK(inp);
3457                                 } else {
3458                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3459                                         error = EINVAL;
3460                                 }
3461                         }
3462                         if (error == 0) {
3463                                 *optsize = sizeof(struct sctp_assoc_value);
3464                         }
3465                         break;
3466                 }
3467         case SCTP_AUTH_SUPPORTED:
3468                 {
3469                         struct sctp_assoc_value *av;
3470
3471                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3472                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3473
3474                         if (stcb) {
3475                                 av->assoc_value = stcb->asoc.auth_supported;
3476                                 SCTP_TCB_UNLOCK(stcb);
3477                         } else {
3478                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3479                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3480                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3481                                         SCTP_INP_RLOCK(inp);
3482                                         av->assoc_value = inp->auth_supported;
3483                                         SCTP_INP_RUNLOCK(inp);
3484                                 } else {
3485                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3486                                         error = EINVAL;
3487                                 }
3488                         }
3489                         if (error == 0) {
3490                                 *optsize = sizeof(struct sctp_assoc_value);
3491                         }
3492                         break;
3493                 }
3494         case SCTP_ASCONF_SUPPORTED:
3495                 {
3496                         struct sctp_assoc_value *av;
3497
3498                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3499                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3500
3501                         if (stcb) {
3502                                 av->assoc_value = stcb->asoc.asconf_supported;
3503                                 SCTP_TCB_UNLOCK(stcb);
3504                         } else {
3505                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3506                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3507                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3508                                         SCTP_INP_RLOCK(inp);
3509                                         av->assoc_value = inp->asconf_supported;
3510                                         SCTP_INP_RUNLOCK(inp);
3511                                 } else {
3512                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3513                                         error = EINVAL;
3514                                 }
3515                         }
3516                         if (error == 0) {
3517                                 *optsize = sizeof(struct sctp_assoc_value);
3518                         }
3519                         break;
3520                 }
3521         case SCTP_RECONFIG_SUPPORTED:
3522                 {
3523                         struct sctp_assoc_value *av;
3524
3525                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3526                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3527
3528                         if (stcb) {
3529                                 av->assoc_value = stcb->asoc.reconfig_supported;
3530                                 SCTP_TCB_UNLOCK(stcb);
3531                         } else {
3532                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3533                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3534                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3535                                         SCTP_INP_RLOCK(inp);
3536                                         av->assoc_value = inp->reconfig_supported;
3537                                         SCTP_INP_RUNLOCK(inp);
3538                                 } else {
3539                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3540                                         error = EINVAL;
3541                                 }
3542                         }
3543                         if (error == 0) {
3544                                 *optsize = sizeof(struct sctp_assoc_value);
3545                         }
3546                         break;
3547                 }
3548         case SCTP_NRSACK_SUPPORTED:
3549                 {
3550                         struct sctp_assoc_value *av;
3551
3552                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3553                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3554
3555                         if (stcb) {
3556                                 av->assoc_value = stcb->asoc.nrsack_supported;
3557                                 SCTP_TCB_UNLOCK(stcb);
3558                         } else {
3559                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3560                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3561                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3562                                         SCTP_INP_RLOCK(inp);
3563                                         av->assoc_value = inp->nrsack_supported;
3564                                         SCTP_INP_RUNLOCK(inp);
3565                                 } else {
3566                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3567                                         error = EINVAL;
3568                                 }
3569                         }
3570                         if (error == 0) {
3571                                 *optsize = sizeof(struct sctp_assoc_value);
3572                         }
3573                         break;
3574                 }
3575         case SCTP_PKTDROP_SUPPORTED:
3576                 {
3577                         struct sctp_assoc_value *av;
3578
3579                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3580                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3581
3582                         if (stcb) {
3583                                 av->assoc_value = stcb->asoc.pktdrop_supported;
3584                                 SCTP_TCB_UNLOCK(stcb);
3585                         } else {
3586                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3587                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3588                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3589                                         SCTP_INP_RLOCK(inp);
3590                                         av->assoc_value = inp->pktdrop_supported;
3591                                         SCTP_INP_RUNLOCK(inp);
3592                                 } else {
3593                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3594                                         error = EINVAL;
3595                                 }
3596                         }
3597                         if (error == 0) {
3598                                 *optsize = sizeof(struct sctp_assoc_value);
3599                         }
3600                         break;
3601                 }
3602         case SCTP_ENABLE_STREAM_RESET:
3603                 {
3604                         struct sctp_assoc_value *av;
3605
3606                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3607                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3608
3609                         if (stcb) {
3610                                 av->assoc_value = (uint32_t) stcb->asoc.local_strreset_support;
3611                                 SCTP_TCB_UNLOCK(stcb);
3612                         } else {
3613                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3614                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3615                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3616                                         SCTP_INP_RLOCK(inp);
3617                                         av->assoc_value = (uint32_t) inp->local_strreset_support;
3618                                         SCTP_INP_RUNLOCK(inp);
3619                                 } else {
3620                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3621                                         error = EINVAL;
3622                                 }
3623                         }
3624                         if (error == 0) {
3625                                 *optsize = sizeof(struct sctp_assoc_value);
3626                         }
3627                         break;
3628                 }
3629         case SCTP_PR_STREAM_STATUS:
3630                 {
3631                         struct sctp_prstatus *sprstat;
3632                         uint16_t sid;
3633                         uint16_t policy;
3634
3635                         SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3636                         SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3637
3638                         sid = sprstat->sprstat_sid;
3639                         policy = sprstat->sprstat_policy;
3640 #if defined(SCTP_DETAILED_STR_STATS)
3641                         if ((stcb != NULL) &&
3642                             (sid < stcb->asoc.streamoutcnt) &&
3643                             (policy != SCTP_PR_SCTP_NONE) &&
3644                             ((policy <= SCTP_PR_SCTP_MAX) ||
3645                             (policy == SCTP_PR_SCTP_ALL))) {
3646                                 if (policy == SCTP_PR_SCTP_ALL) {
3647                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3648                                         sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3649                                 } else {
3650                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
3651                                         sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
3652                                 }
3653 #else
3654                         if ((stcb != NULL) &&
3655                             (sid < stcb->asoc.streamoutcnt) &&
3656                             (policy == SCTP_PR_SCTP_ALL)) {
3657                                 sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3658                                 sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3659 #endif
3660                                 SCTP_TCB_UNLOCK(stcb);
3661                                 *optsize = sizeof(struct sctp_prstatus);
3662                         } else {
3663                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3664                                 error = EINVAL;
3665                         }
3666                         break;
3667                 }
3668         case SCTP_PR_ASSOC_STATUS:
3669                 {
3670                         struct sctp_prstatus *sprstat;
3671                         uint16_t policy;
3672
3673                         SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3674                         SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3675
3676                         policy = sprstat->sprstat_policy;
3677                         if ((stcb != NULL) &&
3678                             (policy != SCTP_PR_SCTP_NONE) &&
3679                             ((policy <= SCTP_PR_SCTP_MAX) ||
3680                             (policy == SCTP_PR_SCTP_ALL))) {
3681                                 if (policy == SCTP_PR_SCTP_ALL) {
3682                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
3683                                         sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
3684                                 } else {
3685                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
3686                                         sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
3687                                 }
3688                                 SCTP_TCB_UNLOCK(stcb);
3689                                 *optsize = sizeof(struct sctp_prstatus);
3690                         } else {
3691                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3692                                 error = EINVAL;
3693                         }
3694                         break;
3695                 }
3696         case SCTP_MAX_CWND:
3697                 {
3698                         struct sctp_assoc_value *av;
3699
3700                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3701                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3702
3703                         if (stcb) {
3704                                 av->assoc_value = stcb->asoc.max_cwnd;
3705                                 SCTP_TCB_UNLOCK(stcb);
3706                         } else {
3707                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3708                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3709                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3710                                         SCTP_INP_RLOCK(inp);
3711                                         av->assoc_value = inp->max_cwnd;
3712                                         SCTP_INP_RUNLOCK(inp);
3713                                 } else {
3714                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3715                                         error = EINVAL;
3716                                 }
3717                         }
3718                         if (error == 0) {
3719                                 *optsize = sizeof(struct sctp_assoc_value);
3720                         }
3721                         break;
3722                 }
3723         default:
3724                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3725                 error = ENOPROTOOPT;
3726                 break;
3727         }                       /* end switch (sopt->sopt_name) */
3728         if (error) {
3729                 *optsize = 0;
3730         }
3731         return (error);
3732 }
3733
3734 static int
3735 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3736     void *p)
3737 {
3738         int error, set_opt;
3739         uint32_t *mopt;
3740         struct sctp_tcb *stcb = NULL;
3741         struct sctp_inpcb *inp = NULL;
3742         uint32_t vrf_id;
3743
3744         if (optval == NULL) {
3745                 SCTP_PRINTF("optval is NULL\n");
3746                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3747                 return (EINVAL);
3748         }
3749         inp = (struct sctp_inpcb *)so->so_pcb;
3750         if (inp == NULL) {
3751                 SCTP_PRINTF("inp is NULL?\n");
3752                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3753                 return (EINVAL);
3754         }
3755         vrf_id = inp->def_vrf_id;
3756
3757         error = 0;
3758         switch (optname) {
3759         case SCTP_NODELAY:
3760         case SCTP_AUTOCLOSE:
3761         case SCTP_AUTO_ASCONF:
3762         case SCTP_EXPLICIT_EOR:
3763         case SCTP_DISABLE_FRAGMENTS:
3764         case SCTP_USE_EXT_RCVINFO:
3765         case SCTP_I_WANT_MAPPED_V4_ADDR:
3766                 /* copy in the option value */
3767                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3768                 set_opt = 0;
3769                 if (error)
3770                         break;
3771                 switch (optname) {
3772                 case SCTP_DISABLE_FRAGMENTS:
3773                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3774                         break;
3775                 case SCTP_AUTO_ASCONF:
3776                         /*
3777                          * NOTE: we don't really support this flag
3778                          */
3779                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3780                                 /* only valid for bound all sockets */
3781                                 if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3782                                     (*mopt != 0)) {
3783                                         /* forbidden by admin */
3784                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3785                                         return (EPERM);
3786                                 }
3787                                 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3788                         } else {
3789                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3790                                 return (EINVAL);
3791                         }
3792                         break;
3793                 case SCTP_EXPLICIT_EOR:
3794                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3795                         break;
3796                 case SCTP_USE_EXT_RCVINFO:
3797                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3798                         break;
3799                 case SCTP_I_WANT_MAPPED_V4_ADDR:
3800                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3801                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3802                         } else {
3803                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3804                                 return (EINVAL);
3805                         }
3806                         break;
3807                 case SCTP_NODELAY:
3808                         set_opt = SCTP_PCB_FLAGS_NODELAY;
3809                         break;
3810                 case SCTP_AUTOCLOSE:
3811                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3812                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3813                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3814                                 return (EINVAL);
3815                         }
3816                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3817                         /*
3818                          * The value is in ticks. Note this does not effect
3819                          * old associations, only new ones.
3820                          */
3821                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3822                         break;
3823                 }
3824                 SCTP_INP_WLOCK(inp);
3825                 if (*mopt != 0) {
3826                         sctp_feature_on(inp, set_opt);
3827                 } else {
3828                         sctp_feature_off(inp, set_opt);
3829                 }
3830                 SCTP_INP_WUNLOCK(inp);
3831                 break;
3832         case SCTP_REUSE_PORT:
3833                 {
3834                         SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3835                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3836                                 /* Can't set it after we are bound */
3837                                 error = EINVAL;
3838                                 break;
3839                         }
3840                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3841                                 /* Can't do this for a 1-m socket */
3842                                 error = EINVAL;
3843                                 break;
3844                         }
3845                         if (optval)
3846                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3847                         else
3848                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3849                         break;
3850                 }
3851         case SCTP_PARTIAL_DELIVERY_POINT:
3852                 {
3853                         uint32_t *value;
3854
3855                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3856                         if (*value > SCTP_SB_LIMIT_RCV(so)) {
3857                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3858                                 error = EINVAL;
3859                                 break;
3860                         }
3861                         inp->partial_delivery_point = *value;
3862                         break;
3863                 }
3864         case SCTP_FRAGMENT_INTERLEAVE:
3865                 /* not yet until we re-write sctp_recvmsg() */
3866                 {
3867                         uint32_t *level;
3868
3869                         SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3870                         if (*level == SCTP_FRAG_LEVEL_2) {
3871                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3872                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3873                         } else if (*level == SCTP_FRAG_LEVEL_1) {
3874                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3875                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3876                         } else if (*level == SCTP_FRAG_LEVEL_0) {
3877                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3878                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3879
3880                         } else {
3881                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3882                                 error = EINVAL;
3883                         }
3884                         break;
3885                 }
3886         case SCTP_CMT_ON_OFF:
3887                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3888                         struct sctp_assoc_value *av;
3889
3890                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3891                         if (av->assoc_value > SCTP_CMT_MAX) {
3892                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3893                                 error = EINVAL;
3894                                 break;
3895                         }
3896                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3897                         if (stcb) {
3898                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3899                                 SCTP_TCB_UNLOCK(stcb);
3900                         } else {
3901                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3902                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3903                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3904                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3905                                         SCTP_INP_WLOCK(inp);
3906                                         inp->sctp_cmt_on_off = av->assoc_value;
3907                                         SCTP_INP_WUNLOCK(inp);
3908                                 }
3909                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3910                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3911                                         SCTP_INP_RLOCK(inp);
3912                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3913                                                 SCTP_TCB_LOCK(stcb);
3914                                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3915                                                 SCTP_TCB_UNLOCK(stcb);
3916                                         }
3917                                         SCTP_INP_RUNLOCK(inp);
3918                                 }
3919                         }
3920                 } else {
3921                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3922                         error = ENOPROTOOPT;
3923                 }
3924                 break;
3925         case SCTP_PLUGGABLE_CC:
3926                 {
3927                         struct sctp_assoc_value *av;
3928                         struct sctp_nets *net;
3929
3930                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3931                         if ((av->assoc_value != SCTP_CC_RFC2581) &&
3932                             (av->assoc_value != SCTP_CC_HSTCP) &&
3933                             (av->assoc_value != SCTP_CC_HTCP) &&
3934                             (av->assoc_value != SCTP_CC_RTCC)) {
3935                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3936                                 error = EINVAL;
3937                                 break;
3938                         }
3939                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3940                         if (stcb) {
3941                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3942                                 stcb->asoc.congestion_control_module = av->assoc_value;
3943                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3944                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3945                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3946                                         }
3947                                 }
3948                                 SCTP_TCB_UNLOCK(stcb);
3949                         } else {
3950                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3951                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3952                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3953                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3954                                         SCTP_INP_WLOCK(inp);
3955                                         inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
3956                                         SCTP_INP_WUNLOCK(inp);
3957                                 }
3958                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3959                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3960                                         SCTP_INP_RLOCK(inp);
3961                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3962                                                 SCTP_TCB_LOCK(stcb);
3963                                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3964                                                 stcb->asoc.congestion_control_module = av->assoc_value;
3965                                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3966                                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3967                                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3968                                                         }
3969                                                 }
3970                                                 SCTP_TCB_UNLOCK(stcb);
3971                                         }
3972                                         SCTP_INP_RUNLOCK(inp);
3973                                 }
3974                         }
3975                         break;
3976                 }
3977         case SCTP_CC_OPTION:
3978                 {
3979                         struct sctp_cc_option *cc_opt;
3980
3981                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
3982                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
3983                         if (stcb == NULL) {
3984                                 if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
3985                                         SCTP_INP_RLOCK(inp);
3986                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3987                                                 SCTP_TCB_LOCK(stcb);
3988                                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
3989                                                         (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
3990                                                 }
3991                                                 SCTP_TCB_UNLOCK(stcb);
3992                                         }
3993                                         SCTP_INP_RUNLOCK(inp);
3994                                 } else {
3995                                         error = EINVAL;
3996                                 }
3997                         } else {
3998                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
3999                                         error = ENOTSUP;
4000                                 } else {
4001                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
4002                                             cc_opt);
4003                                 }
4004                                 SCTP_TCB_UNLOCK(stcb);
4005                         }
4006                         break;
4007                 }
4008         case SCTP_PLUGGABLE_SS:
4009                 {
4010                         struct sctp_assoc_value *av;
4011
4012                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4013                         if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4014                             (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4015                             (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4016                             (av->assoc_value != SCTP_SS_PRIORITY) &&
4017                             (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4018                             (av->assoc_value != SCTP_SS_FIRST_COME)) {
4019                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4020                                 error = EINVAL;
4021                                 break;
4022                         }
4023                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4024                         if (stcb) {
4025                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4026                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4027                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4028                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4029                                 SCTP_TCB_UNLOCK(stcb);
4030                         } else {
4031                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4032                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4033                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4034                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4035                                         SCTP_INP_WLOCK(inp);
4036                                         inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
4037                                         SCTP_INP_WUNLOCK(inp);
4038                                 }
4039                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4040                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4041                                         SCTP_INP_RLOCK(inp);
4042                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4043                                                 SCTP_TCB_LOCK(stcb);
4044                                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4045                                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4046                                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4047                                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4048                                                 SCTP_TCB_UNLOCK(stcb);
4049                                         }
4050                                         SCTP_INP_RUNLOCK(inp);
4051                                 }
4052                         }
4053                         break;
4054                 }
4055         case SCTP_SS_VALUE:
4056                 {
4057                         struct sctp_stream_value *av;
4058
4059                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4060                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4061                         if (stcb) {
4062                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
4063                                     (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4064                                     av->stream_value) < 0)) {
4065                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4066                                         error = EINVAL;
4067                                 }
4068                                 SCTP_TCB_UNLOCK(stcb);
4069                         } else {
4070                                 if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4071                                         SCTP_INP_RLOCK(inp);
4072                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4073                                                 SCTP_TCB_LOCK(stcb);
4074                                                 if (av->stream_id < stcb->asoc.streamoutcnt) {
4075                                                         stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4076                                                             &stcb->asoc,
4077                                                             &stcb->asoc.strmout[av->stream_id],
4078                                                             av->stream_value);
4079                                                 }
4080                                                 SCTP_TCB_UNLOCK(stcb);
4081                                         }
4082                                         SCTP_INP_RUNLOCK(inp);
4083                                 } else {
4084                                         /*
4085                                          * Can't set stream value without
4086                                          * association
4087                                          */
4088                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4089                                         error = EINVAL;
4090                                 }
4091                         }
4092                         break;
4093                 }
4094         case SCTP_CLR_STAT_LOG:
4095                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4096                 error = EOPNOTSUPP;
4097                 break;
4098         case SCTP_CONTEXT:
4099                 {
4100                         struct sctp_assoc_value *av;
4101
4102                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4103                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4104
4105                         if (stcb) {
4106                                 stcb->asoc.context = av->assoc_value;
4107                                 SCTP_TCB_UNLOCK(stcb);
4108                         } else {
4109                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4110                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4111                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4112                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4113                                         SCTP_INP_WLOCK(inp);
4114                                         inp->sctp_context = av->assoc_value;
4115                                         SCTP_INP_WUNLOCK(inp);
4116                                 }
4117                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4118                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4119                                         SCTP_INP_RLOCK(inp);
4120                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4121                                                 SCTP_TCB_LOCK(stcb);
4122                                                 stcb->asoc.context = av->assoc_value;
4123                                                 SCTP_TCB_UNLOCK(stcb);
4124                                         }
4125                                         SCTP_INP_RUNLOCK(inp);
4126                                 }
4127                         }
4128                         break;
4129                 }
4130         case SCTP_VRF_ID:
4131                 {
4132                         uint32_t *default_vrfid;
4133
4134                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4135                         if (*default_vrfid > SCTP_MAX_VRF_ID) {
4136                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4137                                 error = EINVAL;
4138                                 break;
4139                         }
4140                         inp->def_vrf_id = *default_vrfid;
4141                         break;
4142                 }
4143         case SCTP_DEL_VRF_ID:
4144                 {
4145                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4146                         error = EOPNOTSUPP;
4147                         break;
4148                 }
4149         case SCTP_ADD_VRF_ID:
4150                 {
4151                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4152                         error = EOPNOTSUPP;
4153                         break;
4154                 }
4155         case SCTP_DELAYED_SACK:
4156                 {
4157                         struct sctp_sack_info *sack;
4158
4159                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4160                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4161                         if (sack->sack_delay) {
4162                                 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4163                                         sack->sack_delay = SCTP_MAX_SACK_DELAY;
4164                                 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4165                                         sack->sack_delay = TICKS_TO_MSEC(1);
4166                                 }
4167                         }
4168                         if (stcb) {
4169                                 if (sack->sack_delay) {
4170                                         stcb->asoc.delayed_ack = sack->sack_delay;
4171                                 }
4172                                 if (sack->sack_freq) {
4173                                         stcb->asoc.sack_freq = sack->sack_freq;
4174                                 }
4175                                 SCTP_TCB_UNLOCK(stcb);
4176                         } else {
4177                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4178                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4179                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4180                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4181                                         SCTP_INP_WLOCK(inp);
4182                                         if (sack->sack_delay) {
4183                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4184                                         }
4185                                         if (sack->sack_freq) {
4186                                                 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4187                                         }
4188                                         SCTP_INP_WUNLOCK(inp);
4189                                 }
4190                                 if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4191                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4192                                         SCTP_INP_RLOCK(inp);
4193                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4194                                                 SCTP_TCB_LOCK(stcb);
4195                                                 if (sack->sack_delay) {
4196                                                         stcb->asoc.delayed_ack = sack->sack_delay;
4197                                                 }
4198                                                 if (sack->sack_freq) {
4199                                                         stcb->asoc.sack_freq = sack->sack_freq;
4200                                                 }
4201                                                 SCTP_TCB_UNLOCK(stcb);
4202                                         }
4203                                         SCTP_INP_RUNLOCK(inp);
4204                                 }
4205                         }
4206                         break;
4207                 }
4208         case SCTP_AUTH_CHUNK:
4209                 {
4210                         struct sctp_authchunk *sauth;
4211
4212                         SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4213
4214                         SCTP_INP_WLOCK(inp);
4215                         if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4216                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4217                                 error = EINVAL;
4218                         }
4219                         SCTP_INP_WUNLOCK(inp);
4220                         break;
4221                 }
4222         case SCTP_AUTH_KEY:
4223                 {
4224                         struct sctp_authkey *sca;
4225                         struct sctp_keyhead *shared_keys;
4226                         sctp_sharedkey_t *shared_key;
4227                         sctp_key_t *key = NULL;
4228                         size_t size;
4229
4230                         SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4231                         if (sca->sca_keylength == 0) {
4232                                 size = optsize - sizeof(struct sctp_authkey);
4233                         } else {
4234                                 if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4235                                         size = sca->sca_keylength;
4236                                 } else {
4237                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4238                                         error = EINVAL;
4239                                         break;
4240                                 }
4241                         }
4242                         SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4243
4244                         if (stcb) {
4245                                 shared_keys = &stcb->asoc.shared_keys;
4246                                 /* clear the cached keys for this key id */
4247                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4248                                 /*
4249                                  * create the new shared key and
4250                                  * insert/replace it
4251                                  */
4252                                 if (size > 0) {
4253                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
4254                                         if (key == NULL) {
4255                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4256                                                 error = ENOMEM;
4257                                                 SCTP_TCB_UNLOCK(stcb);
4258                                                 break;
4259                                         }
4260                                 }
4261                                 shared_key = sctp_alloc_sharedkey();
4262                                 if (shared_key == NULL) {
4263                                         sctp_free_key(key);
4264                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4265                                         error = ENOMEM;
4266                                         SCTP_TCB_UNLOCK(stcb);
4267                                         break;
4268                                 }
4269                                 shared_key->key = key;
4270                                 shared_key->keyid = sca->sca_keynumber;
4271                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4272                                 SCTP_TCB_UNLOCK(stcb);
4273                         } else {
4274                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4275                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4276                                     (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4277                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4278                                         SCTP_INP_WLOCK(inp);
4279                                         shared_keys = &inp->sctp_ep.shared_keys;
4280                                         /*
4281                                          * clear the cached keys on all
4282                                          * assocs for this key id
4283                                          */
4284                                         sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4285                                         /*
4286                                          * create the new shared key and
4287                                          * insert/replace it
4288                                          */
4289                                         if (size > 0) {
4290                                                 key = sctp_set_key(sca->sca_key, (uint32_t) size);
4291                                                 if (key == NULL) {
4292                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4293                                                         error = ENOMEM;
4294                                                         SCTP_INP_WUNLOCK(inp);
4295                                                         break;
4296                                                 }
4297                                         }
4298                                         shared_key = sctp_alloc_sharedkey();
4299                                         if (shared_key == NULL) {
4300                                                 sctp_free_key(key);
4301                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4302                                                 error = ENOMEM;
4303                                                 SCTP_INP_WUNLOCK(inp);
4304                                                 break;
4305                                         }
4306                                         shared_key->key = key;
4307                                         shared_key->keyid = sca->sca_keynumber;
4308                                         error = sctp_insert_sharedkey(shared_keys, shared_key);
4309                                         SCTP_INP_WUNLOCK(inp);
4310                                 }
4311                                 if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4312                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4313                                         SCTP_INP_RLOCK(inp);
4314                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4315                                                 SCTP_TCB_LOCK(stcb);
4316                                                 shared_keys = &stcb->asoc.shared_keys;
4317                                                 /*
4318                                                  * clear the cached keys for
4319                                                  * this key id
4320                                                  */
4321                                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4322                                                 /*
4323                                                  * create the new shared key
4324                                                  * and insert/replace it
4325                                                  */
4326                                                 if (size > 0) {
4327                                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
4328                                                         if (key == NULL) {
4329                                                                 SCTP_TCB_UNLOCK(stcb);
4330                                                                 continue;
4331                                                         }
4332                                                 }
4333                                                 shared_key = sctp_alloc_sharedkey();
4334                                                 if (shared_key == NULL) {
4335                                                         sctp_free_key(key);
4336                                                         SCTP_TCB_UNLOCK(stcb);
4337                                                         continue;
4338                                                 }
4339                                                 shared_key->key = key;
4340                                                 shared_key->keyid = sca->sca_keynumber;
4341                                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4342                                                 SCTP_TCB_UNLOCK(stcb);
4343                                         }
4344                                         SCTP_INP_RUNLOCK(inp);
4345                                 }
4346                         }
4347                         break;
4348                 }
4349         case SCTP_HMAC_IDENT:
4350                 {
4351                         struct sctp_hmacalgo *shmac;
4352                         sctp_hmaclist_t *hmaclist;
4353                         uint16_t hmacid;
4354                         uint32_t i;
4355
4356                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4357                         if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
4358                             (shmac->shmac_number_of_idents > 0xffff)) {
4359                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4360                                 error = EINVAL;
4361                                 break;
4362                         }
4363                         hmaclist = sctp_alloc_hmaclist((uint16_t) shmac->shmac_number_of_idents);
4364                         if (hmaclist == NULL) {
4365                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4366                                 error = ENOMEM;
4367                                 break;
4368                         }
4369                         for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4370                                 hmacid = shmac->shmac_idents[i];
4371                                 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4372                                          /* invalid HMACs were found */ ;
4373                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4374                                         error = EINVAL;
4375                                         sctp_free_hmaclist(hmaclist);
4376                                         goto sctp_set_hmac_done;
4377                                 }
4378                         }
4379                         for (i = 0; i < hmaclist->num_algo; i++) {
4380                                 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4381                                         /* already in list */
4382                                         break;
4383                                 }
4384                         }
4385                         if (i == hmaclist->num_algo) {
4386                                 /* not found in list */
4387                                 sctp_free_hmaclist(hmaclist);
4388                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4389                                 error = EINVAL;
4390                                 break;
4391                         }
4392                         /* set it on the endpoint */
4393                         SCTP_INP_WLOCK(inp);
4394                         if (inp->sctp_ep.local_hmacs)
4395                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4396                         inp->sctp_ep.local_hmacs = hmaclist;
4397                         SCTP_INP_WUNLOCK(inp);
4398         sctp_set_hmac_done:
4399                         break;
4400                 }
4401         case SCTP_AUTH_ACTIVE_KEY:
4402                 {
4403                         struct sctp_authkeyid *scact;
4404
4405                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4406                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4407
4408                         /* set the active key on the right place */
4409                         if (stcb) {
4410                                 /* set the active key on the assoc */
4411                                 if (sctp_auth_setactivekey(stcb,
4412                                     scact->scact_keynumber)) {
4413                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4414                                             SCTP_FROM_SCTP_USRREQ,
4415                                             EINVAL);
4416                                         error = EINVAL;
4417                                 }
4418                                 SCTP_TCB_UNLOCK(stcb);
4419                         } else {
4420                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4421                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4422                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4423                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4424                                         SCTP_INP_WLOCK(inp);
4425                                         if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4426                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4427                                                 error = EINVAL;
4428                                         }
4429                                         SCTP_INP_WUNLOCK(inp);
4430                                 }
4431                                 if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4432                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4433                                         SCTP_INP_RLOCK(inp);
4434                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4435                                                 SCTP_TCB_LOCK(stcb);
4436                                                 sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4437                                                 SCTP_TCB_UNLOCK(stcb);
4438                                         }
4439                                         SCTP_INP_RUNLOCK(inp);
4440                                 }
4441                         }
4442                         break;
4443                 }
4444         case SCTP_AUTH_DELETE_KEY:
4445                 {
4446                         struct sctp_authkeyid *scdel;
4447
4448                         SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4449                         SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4450
4451                         /* delete the key from the right place */
4452                         if (stcb) {
4453                                 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4454                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4455                                         error = EINVAL;
4456                                 }
4457                                 SCTP_TCB_UNLOCK(stcb);
4458                         } else {
4459                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4460                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4461                                     (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4462                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4463                                         SCTP_INP_WLOCK(inp);
4464                                         if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4465                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4466                                                 error = EINVAL;
4467                                         }
4468                                         SCTP_INP_WUNLOCK(inp);
4469                                 }
4470                                 if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4471                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4472                                         SCTP_INP_RLOCK(inp);
4473                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4474                                                 SCTP_TCB_LOCK(stcb);
4475                                                 sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4476                                                 SCTP_TCB_UNLOCK(stcb);
4477                                         }
4478                                         SCTP_INP_RUNLOCK(inp);
4479                                 }
4480                         }
4481                         break;
4482                 }
4483         case SCTP_AUTH_DEACTIVATE_KEY:
4484                 {
4485                         struct sctp_authkeyid *keyid;
4486
4487                         SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4488                         SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4489
4490                         /* deactivate the key from the right place */
4491                         if (stcb) {
4492                                 if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4493                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4494                                         error = EINVAL;
4495                                 }
4496                                 SCTP_TCB_UNLOCK(stcb);
4497                         } else {
4498                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4499                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4500                                     (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4501                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4502                                         SCTP_INP_WLOCK(inp);
4503                                         if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4504                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4505                                                 error = EINVAL;
4506                                         }
4507                                         SCTP_INP_WUNLOCK(inp);
4508                                 }
4509                                 if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4510                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4511                                         SCTP_INP_RLOCK(inp);
4512                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4513                                                 SCTP_TCB_LOCK(stcb);
4514                                                 sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4515                                                 SCTP_TCB_UNLOCK(stcb);
4516                                         }
4517                                         SCTP_INP_RUNLOCK(inp);
4518                                 }
4519                         }
4520                         break;
4521                 }
4522         case SCTP_ENABLE_STREAM_RESET:
4523                 {
4524                         struct sctp_assoc_value *av;
4525
4526                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4527                         if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4528                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4529                                 error = EINVAL;
4530                                 break;
4531                         }
4532                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4533                         if (stcb) {
4534                                 stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4535                                 SCTP_TCB_UNLOCK(stcb);
4536                         } else {
4537                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4538                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4539                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4540                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4541                                         SCTP_INP_WLOCK(inp);
4542                                         inp->local_strreset_support = (uint8_t) av->assoc_value;
4543                                         SCTP_INP_WUNLOCK(inp);
4544                                 }
4545                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4546                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4547                                         SCTP_INP_RLOCK(inp);
4548                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4549                                                 SCTP_TCB_LOCK(stcb);
4550                                                 stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4551                                                 SCTP_TCB_UNLOCK(stcb);
4552                                         }
4553                                         SCTP_INP_RUNLOCK(inp);
4554                                 }
4555                         }
4556                         break;
4557                 }
4558         case SCTP_RESET_STREAMS:
4559                 {
4560                         struct sctp_reset_streams *strrst;
4561                         int i, send_out = 0;
4562                         int send_in = 0;
4563
4564                         SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4565                         SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4566                         if (stcb == NULL) {
4567                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4568                                 error = ENOENT;
4569                                 break;
4570                         }
4571                         if (stcb->asoc.reconfig_supported == 0) {
4572                                 /*
4573                                  * Peer does not support the chunk type.
4574                                  */
4575                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4576                                 error = EOPNOTSUPP;
4577                                 SCTP_TCB_UNLOCK(stcb);
4578                                 break;
4579                         }
4580                         if (sizeof(struct sctp_reset_streams) +
4581                             strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
4582                                 error = EINVAL;
4583                                 SCTP_TCB_UNLOCK(stcb);
4584                                 break;
4585                         }
4586                         if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4587                                 send_in = 1;
4588                                 if (stcb->asoc.stream_reset_outstanding) {
4589                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4590                                         error = EALREADY;
4591                                         SCTP_TCB_UNLOCK(stcb);
4592                                         break;
4593                                 }
4594                         }
4595                         if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4596                                 send_out = 1;
4597                         }
4598                         if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
4599                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4600                                 error = ENOMEM;
4601                                 SCTP_TCB_UNLOCK(stcb);
4602                                 break;
4603                         }
4604                         if ((send_in == 0) && (send_out == 0)) {
4605                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4606                                 error = EINVAL;
4607                                 SCTP_TCB_UNLOCK(stcb);
4608                                 break;
4609                         }
4610                         for (i = 0; i < strrst->srs_number_streams; i++) {
4611                                 if ((send_in) &&
4612                                     (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
4613                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4614                                         error = EINVAL;
4615                                         break;
4616                                 }
4617                                 if ((send_out) &&
4618                                     (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
4619                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4620                                         error = EINVAL;
4621                                         break;
4622                                 }
4623                         }
4624                         if (error) {
4625                                 SCTP_TCB_UNLOCK(stcb);
4626                                 break;
4627                         }
4628                         if (send_out) {
4629                                 int cnt;
4630                                 uint16_t strm;
4631
4632                                 if (strrst->srs_number_streams) {
4633                                         for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
4634                                                 strm = strrst->srs_stream_list[i];
4635                                                 if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
4636                                                         stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
4637                                                         cnt++;
4638                                                 }
4639                                         }
4640                                 } else {
4641                                         /* Its all */
4642                                         for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
4643                                                 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
4644                                                         stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
4645                                                         cnt++;
4646                                                 }
4647                                         }
4648                                 }
4649                         }
4650                         if (send_in) {
4651                                 error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4652                                     strrst->srs_stream_list,
4653                                     send_in, 0, 0, 0, 0, 0);
4654                         } else {
4655                                 error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
4656                         }
4657                         if (error == 0) {
4658                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4659                         } else {
4660                                 /*
4661                                  * For outgoing streams don't report any
4662                                  * problems in sending the request to the
4663                                  * application. XXX: Double check resetting
4664                                  * incoming streams.
4665                                  */
4666                                 error = 0;
4667                         }
4668                         SCTP_TCB_UNLOCK(stcb);
4669                         break;
4670                 }
4671         case SCTP_ADD_STREAMS:
4672                 {
4673                         struct sctp_add_streams *stradd;
4674                         uint8_t addstream = 0;
4675                         uint16_t add_o_strmcnt = 0;
4676                         uint16_t add_i_strmcnt = 0;
4677
4678                         SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4679                         SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4680                         if (stcb == NULL) {
4681                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4682                                 error = ENOENT;
4683                                 break;
4684                         }
4685                         if (stcb->asoc.reconfig_supported == 0) {
4686                                 /*
4687                                  * Peer does not support the chunk type.
4688                                  */
4689                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4690                                 error = EOPNOTSUPP;
4691                                 SCTP_TCB_UNLOCK(stcb);
4692                                 break;
4693                         }
4694                         if (stcb->asoc.stream_reset_outstanding) {
4695                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4696                                 error = EALREADY;
4697                                 SCTP_TCB_UNLOCK(stcb);
4698                                 break;
4699                         }
4700                         if ((stradd->sas_outstrms == 0) &&
4701                             (stradd->sas_instrms == 0)) {
4702                                 error = EINVAL;
4703                                 goto skip_stuff;
4704                         }
4705                         if (stradd->sas_outstrms) {
4706                                 addstream = 1;
4707                                 /* We allocate here */
4708                                 add_o_strmcnt = stradd->sas_outstrms;
4709                                 if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4710                                         /* You can't have more than 64k */
4711                                         error = EINVAL;
4712                                         goto skip_stuff;
4713                                 }
4714                         }
4715                         if (stradd->sas_instrms) {
4716                                 int cnt;
4717
4718                                 addstream |= 2;
4719                                 /*
4720                                  * We allocate inside
4721                                  * sctp_send_str_reset_req()
4722                                  */
4723                                 add_i_strmcnt = stradd->sas_instrms;
4724                                 cnt = add_i_strmcnt;
4725                                 cnt += stcb->asoc.streamincnt;
4726                                 if (cnt > 0x0000ffff) {
4727                                         /* You can't have more than 64k */
4728                                         error = EINVAL;
4729                                         goto skip_stuff;
4730                                 }
4731                                 if (cnt > (int)stcb->asoc.max_inbound_streams) {
4732                                         /* More than you are allowed */
4733                                         error = EINVAL;
4734                                         goto skip_stuff;
4735                                 }
4736                         }
4737                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4738                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4739         skip_stuff:
4740                         SCTP_TCB_UNLOCK(stcb);
4741                         break;
4742                 }
4743         case SCTP_RESET_ASSOC:
4744                 {
4745                         int i;
4746                         uint32_t *value;
4747
4748                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4749                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
4750                         if (stcb == NULL) {
4751                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4752                                 error = ENOENT;
4753                                 break;
4754                         }
4755                         if (stcb->asoc.reconfig_supported == 0) {
4756                                 /*
4757                                  * Peer does not support the chunk type.
4758                                  */
4759                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4760                                 error = EOPNOTSUPP;
4761                                 SCTP_TCB_UNLOCK(stcb);
4762                                 break;
4763                         }
4764                         if (stcb->asoc.stream_reset_outstanding) {
4765                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4766                                 error = EALREADY;
4767                                 SCTP_TCB_UNLOCK(stcb);
4768                                 break;
4769                         }
4770                         /*
4771                          * Is there any data pending in the send or sent
4772                          * queues?
4773                          */
4774                         if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
4775                             !TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4776                 busy_out:
4777                                 error = EBUSY;
4778                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4779                                 SCTP_TCB_UNLOCK(stcb);
4780                                 break;
4781                         }
4782                         /* Do any streams have data queued? */
4783                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4784                                 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
4785                                         goto busy_out;
4786                                 }
4787                         }
4788                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
4789                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4790                         SCTP_TCB_UNLOCK(stcb);
4791                         break;
4792                 }
4793         case SCTP_CONNECT_X:
4794                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4795                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4796                         error = EINVAL;
4797                         break;
4798                 }
4799                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4800                 break;
4801         case SCTP_CONNECT_X_DELAYED:
4802                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4803                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4804                         error = EINVAL;
4805                         break;
4806                 }
4807                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4808                 break;
4809         case SCTP_CONNECT_X_COMPLETE:
4810                 {
4811                         struct sockaddr *sa;
4812
4813                         /* FIXME MT: check correct? */
4814                         SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4815
4816                         /* find tcb */
4817                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4818                                 SCTP_INP_RLOCK(inp);
4819                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4820                                 if (stcb) {
4821                                         SCTP_TCB_LOCK(stcb);
4822                                 }
4823                                 SCTP_INP_RUNLOCK(inp);
4824                         } else {
4825                                 /*
4826                                  * We increment here since
4827                                  * sctp_findassociation_ep_addr() wil do a
4828                                  * decrement if it finds the stcb as long as
4829                                  * the locked tcb (last argument) is NOT a
4830                                  * TCB.. aka NULL.
4831                                  */
4832                                 SCTP_INP_INCR_REF(inp);
4833                                 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
4834                                 if (stcb == NULL) {
4835                                         SCTP_INP_DECR_REF(inp);
4836                                 }
4837                         }
4838
4839                         if (stcb == NULL) {
4840                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4841                                 error = ENOENT;
4842                                 break;
4843                         }
4844                         if (stcb->asoc.delayed_connection == 1) {
4845                                 stcb->asoc.delayed_connection = 0;
4846                                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4847                                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4848                                     stcb->asoc.primary_destination,
4849                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
4850                                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4851                         } else {
4852                                 /*
4853                                  * already expired or did not use delayed
4854                                  * connectx
4855                                  */
4856                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4857                                 error = EALREADY;
4858                         }
4859                         SCTP_TCB_UNLOCK(stcb);
4860                         break;
4861                 }
4862         case SCTP_MAX_BURST:
4863                 {
4864                         struct sctp_assoc_value *av;
4865
4866                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4867                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4868
4869                         if (stcb) {
4870                                 stcb->asoc.max_burst = av->assoc_value;
4871                                 SCTP_TCB_UNLOCK(stcb);
4872                         } else {
4873                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4874                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4875                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4876                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4877                                         SCTP_INP_WLOCK(inp);
4878                                         inp->sctp_ep.max_burst = av->assoc_value;
4879                                         SCTP_INP_WUNLOCK(inp);
4880                                 }
4881                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4882                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4883                                         SCTP_INP_RLOCK(inp);
4884                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4885                                                 SCTP_TCB_LOCK(stcb);
4886                                                 stcb->asoc.max_burst = av->assoc_value;
4887                                                 SCTP_TCB_UNLOCK(stcb);
4888                                         }
4889                                         SCTP_INP_RUNLOCK(inp);
4890                                 }
4891                         }
4892                         break;
4893                 }
4894         case SCTP_MAXSEG:
4895                 {
4896                         struct sctp_assoc_value *av;
4897                         int ovh;
4898
4899                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4900                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4901
4902                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4903                                 ovh = SCTP_MED_OVERHEAD;
4904                         } else {
4905                                 ovh = SCTP_MED_V4_OVERHEAD;
4906                         }
4907                         if (stcb) {
4908                                 if (av->assoc_value) {
4909                                         stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4910                                 } else {
4911                                         stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4912                                 }
4913                                 SCTP_TCB_UNLOCK(stcb);
4914                         } else {
4915                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4916                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4917                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4918                                         SCTP_INP_WLOCK(inp);
4919                                         /*
4920                                          * FIXME MT: I think this is not in
4921                                          * tune with the API ID
4922                                          */
4923                                         if (av->assoc_value) {
4924                                                 inp->sctp_frag_point = (av->assoc_value + ovh);
4925                                         } else {
4926                                                 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4927                                         }
4928                                         SCTP_INP_WUNLOCK(inp);
4929                                 } else {
4930                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4931                                         error = EINVAL;
4932                                 }
4933                         }
4934                         break;
4935                 }
4936         case SCTP_EVENTS:
4937                 {
4938                         struct sctp_event_subscribe *events;
4939
4940                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
4941
4942                         SCTP_INP_WLOCK(inp);
4943                         if (events->sctp_data_io_event) {
4944                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4945                         } else {
4946                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4947                         }
4948
4949                         if (events->sctp_association_event) {
4950                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4951                         } else {
4952                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4953                         }
4954
4955                         if (events->sctp_address_event) {
4956                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4957                         } else {
4958                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4959                         }
4960
4961                         if (events->sctp_send_failure_event) {
4962                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4963                         } else {
4964                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4965                         }
4966
4967                         if (events->sctp_peer_error_event) {
4968                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4969                         } else {
4970                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4971                         }
4972
4973                         if (events->sctp_shutdown_event) {
4974                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4975                         } else {
4976                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4977                         }
4978
4979                         if (events->sctp_partial_delivery_event) {
4980                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4981                         } else {
4982                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4983                         }
4984
4985                         if (events->sctp_adaptation_layer_event) {
4986                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4987                         } else {
4988                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4989                         }
4990
4991                         if (events->sctp_authentication_event) {
4992                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4993                         } else {
4994                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4995                         }
4996
4997                         if (events->sctp_sender_dry_event) {
4998                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
4999                         } else {
5000                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5001                         }
5002
5003                         if (events->sctp_stream_reset_event) {
5004                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5005                         } else {
5006                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5007                         }
5008                         SCTP_INP_WUNLOCK(inp);
5009
5010                         SCTP_INP_RLOCK(inp);
5011                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5012                                 SCTP_TCB_LOCK(stcb);
5013                                 if (events->sctp_association_event) {
5014                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5015                                 } else {
5016                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5017                                 }
5018                                 if (events->sctp_address_event) {
5019                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5020                                 } else {
5021                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5022                                 }
5023                                 if (events->sctp_send_failure_event) {
5024                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5025                                 } else {
5026                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5027                                 }
5028                                 if (events->sctp_peer_error_event) {
5029                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5030                                 } else {
5031                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5032                                 }
5033                                 if (events->sctp_shutdown_event) {
5034                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5035                                 } else {
5036                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5037                                 }
5038                                 if (events->sctp_partial_delivery_event) {
5039                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5040                                 } else {
5041                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5042                                 }
5043                                 if (events->sctp_adaptation_layer_event) {
5044                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5045                                 } else {
5046                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5047                                 }
5048                                 if (events->sctp_authentication_event) {
5049                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5050                                 } else {
5051                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5052                                 }
5053                                 if (events->sctp_sender_dry_event) {
5054                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5055                                 } else {
5056                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5057                                 }
5058                                 if (events->sctp_stream_reset_event) {
5059                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5060                                 } else {
5061                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5062                                 }
5063                                 SCTP_TCB_UNLOCK(stcb);
5064                         }
5065                         /*
5066                          * Send up the sender dry event only for 1-to-1
5067                          * style sockets.
5068                          */
5069                         if (events->sctp_sender_dry_event) {
5070                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5071                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5072                                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
5073                                         if (stcb) {
5074                                                 SCTP_TCB_LOCK(stcb);
5075                                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5076                                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5077                                                     (stcb->asoc.stream_queue_cnt == 0)) {
5078                                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5079                                                 }
5080                                                 SCTP_TCB_UNLOCK(stcb);
5081                                         }
5082                                 }
5083                         }
5084                         SCTP_INP_RUNLOCK(inp);
5085                         break;
5086                 }
5087         case SCTP_ADAPTATION_LAYER:
5088                 {
5089                         struct sctp_setadaptation *adap_bits;
5090
5091                         SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5092                         SCTP_INP_WLOCK(inp);
5093                         inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5094                         inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5095                         SCTP_INP_WUNLOCK(inp);
5096                         break;
5097                 }
5098 #ifdef SCTP_DEBUG
5099         case SCTP_SET_INITIAL_DBG_SEQ:
5100                 {
5101                         uint32_t *vvv;
5102
5103                         SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5104                         SCTP_INP_WLOCK(inp);
5105                         inp->sctp_ep.initial_sequence_debug = *vvv;
5106                         SCTP_INP_WUNLOCK(inp);
5107                         break;
5108                 }
5109 #endif
5110         case SCTP_DEFAULT_SEND_PARAM:
5111                 {
5112                         struct sctp_sndrcvinfo *s_info;
5113
5114                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5115                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5116
5117                         if (stcb) {
5118                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5119                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5120                                 } else {
5121                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5122                                         error = EINVAL;
5123                                 }
5124                                 SCTP_TCB_UNLOCK(stcb);
5125                         } else {
5126                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5127                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5128                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5129                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5130                                         SCTP_INP_WLOCK(inp);
5131                                         memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5132                                         SCTP_INP_WUNLOCK(inp);
5133                                 }
5134                                 if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5135                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5136                                         SCTP_INP_RLOCK(inp);
5137                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5138                                                 SCTP_TCB_LOCK(stcb);
5139                                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5140                                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5141                                                 }
5142                                                 SCTP_TCB_UNLOCK(stcb);
5143                                         }
5144                                         SCTP_INP_RUNLOCK(inp);
5145                                 }
5146                         }
5147                         break;
5148                 }
5149         case SCTP_PEER_ADDR_PARAMS:
5150                 {
5151                         struct sctp_paddrparams *paddrp;
5152                         struct sctp_nets *net;
5153                         struct sockaddr *addr;
5154
5155 #if defined(INET) && defined(INET6)
5156                         struct sockaddr_in sin_store;
5157
5158 #endif
5159
5160                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5161                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5162
5163 #if defined(INET) && defined(INET6)
5164                         if (paddrp->spp_address.ss_family == AF_INET6) {
5165                                 struct sockaddr_in6 *sin6;
5166
5167                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5168                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5169                                         in6_sin6_2_sin(&sin_store, sin6);
5170                                         addr = (struct sockaddr *)&sin_store;
5171                                 } else {
5172                                         addr = (struct sockaddr *)&paddrp->spp_address;
5173                                 }
5174                         } else {
5175                                 addr = (struct sockaddr *)&paddrp->spp_address;
5176                         }
5177 #else
5178                         addr = (struct sockaddr *)&paddrp->spp_address;
5179 #endif
5180                         if (stcb != NULL) {
5181                                 net = sctp_findnet(stcb, addr);
5182                         } else {
5183                                 /*
5184                                  * We increment here since
5185                                  * sctp_findassociation_ep_addr() wil do a
5186                                  * decrement if it finds the stcb as long as
5187                                  * the locked tcb (last argument) is NOT a
5188                                  * TCB.. aka NULL.
5189                                  */
5190                                 net = NULL;
5191                                 SCTP_INP_INCR_REF(inp);
5192                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5193                                     &net, NULL, NULL);
5194                                 if (stcb == NULL) {
5195                                         SCTP_INP_DECR_REF(inp);
5196                                 }
5197                         }
5198                         if ((stcb != NULL) && (net == NULL)) {
5199 #ifdef INET
5200                                 if (addr->sa_family == AF_INET) {
5201
5202                                         struct sockaddr_in *sin;
5203
5204                                         sin = (struct sockaddr_in *)addr;
5205                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
5206                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5207                                                 SCTP_TCB_UNLOCK(stcb);
5208                                                 error = EINVAL;
5209                                                 break;
5210                                         }
5211                                 } else
5212 #endif
5213 #ifdef INET6
5214                                 if (addr->sa_family == AF_INET6) {
5215                                         struct sockaddr_in6 *sin6;
5216
5217                                         sin6 = (struct sockaddr_in6 *)addr;
5218                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5219                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5220                                                 SCTP_TCB_UNLOCK(stcb);
5221                                                 error = EINVAL;
5222                                                 break;
5223                                         }
5224                                 } else
5225 #endif
5226                                 {
5227                                         error = EAFNOSUPPORT;
5228                                         SCTP_TCB_UNLOCK(stcb);
5229                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5230                                         break;
5231                                 }
5232                         }
5233                         /* sanity checks */
5234                         if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5235                                 if (stcb)
5236                                         SCTP_TCB_UNLOCK(stcb);
5237                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5238                                 return (EINVAL);
5239                         }
5240                         if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5241                                 if (stcb)
5242                                         SCTP_TCB_UNLOCK(stcb);
5243                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5244                                 return (EINVAL);
5245                         }
5246                         if (stcb != NULL) {
5247                                 /************************TCB SPECIFIC SET ******************/
5248                                 if (net != NULL) {
5249                                         /************************NET SPECIFIC SET ******************/
5250                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5251                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5252                                                     !(net->dest_state & SCTP_ADDR_NOHB)) {
5253                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5254                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
5255                                                 }
5256                                                 net->dest_state |= SCTP_ADDR_NOHB;
5257                                         }
5258                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5259                                                 if (paddrp->spp_hbinterval) {
5260                                                         net->heart_beat_delay = paddrp->spp_hbinterval;
5261                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5262                                                         net->heart_beat_delay = 0;
5263                                                 }
5264                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5265                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5266                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5267                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5268                                         }
5269                                         if (paddrp->spp_flags & SPP_HB_DEMAND) {
5270                                                 /* on demand HB */
5271                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5272                                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5273                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5274                                         }
5275                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5276                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5277                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5278                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
5279                                                 }
5280                                                 net->dest_state |= SCTP_ADDR_NO_PMTUD;
5281                                                 net->mtu = paddrp->spp_pathmtu;
5282                                                 switch (net->ro._l_addr.sa.sa_family) {
5283 #ifdef INET
5284                                                 case AF_INET:
5285                                                         net->mtu += SCTP_MIN_V4_OVERHEAD;
5286                                                         break;
5287 #endif
5288 #ifdef INET6
5289                                                 case AF_INET6:
5290                                                         net->mtu += SCTP_MIN_OVERHEAD;
5291                                                         break;
5292 #endif
5293                                                 default:
5294                                                         break;
5295                                                 }
5296                                                 if (net->mtu < stcb->asoc.smallest_mtu) {
5297                                                         sctp_pathmtu_adjustment(stcb, net->mtu);
5298                                                 }
5299                                         }
5300                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5301                                                 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5302                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5303                                                 }
5304                                                 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5305                                         }
5306                                         if (paddrp->spp_pathmaxrxt) {
5307                                                 if (net->dest_state & SCTP_ADDR_PF) {
5308                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5309                                                                 net->dest_state &= ~SCTP_ADDR_PF;
5310                                                         }
5311                                                 } else {
5312                                                         if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5313                                                             (net->error_count > net->pf_threshold)) {
5314                                                                 net->dest_state |= SCTP_ADDR_PF;
5315                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5316                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5317                                                                     stcb->sctp_ep, stcb, net,
5318                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
5319                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5320                                                         }
5321                                                 }
5322                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
5323                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5324                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5325                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5326                                                         }
5327                                                 } else {
5328                                                         if (net->error_count <= paddrp->spp_pathmaxrxt) {
5329                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
5330                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5331                                                         }
5332                                                 }
5333                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
5334                                         }
5335                                         if (paddrp->spp_flags & SPP_DSCP) {
5336                                                 net->dscp = paddrp->spp_dscp & 0xfc;
5337                                                 net->dscp |= 0x01;
5338                                         }
5339 #ifdef INET6
5340                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5341                                                 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5342                                                         net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5343                                                         net->flowlabel |= 0x80000000;
5344                                                 }
5345                                         }
5346 #endif
5347                                 } else {
5348                                         /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5349                                         if (paddrp->spp_pathmaxrxt != 0) {
5350                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5351                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5352                                                         if (net->dest_state & SCTP_ADDR_PF) {
5353                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5354                                                                         net->dest_state &= ~SCTP_ADDR_PF;
5355                                                                 }
5356                                                         } else {
5357                                                                 if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5358                                                                     (net->error_count > net->pf_threshold)) {
5359                                                                         net->dest_state |= SCTP_ADDR_PF;
5360                                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5361                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5362                                                                             stcb->sctp_ep, stcb, net,
5363                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
5364                                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5365                                                                 }
5366                                                         }
5367                                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
5368                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5369                                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
5370                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5371                                                                 }
5372                                                         } else {
5373                                                                 if (net->error_count <= paddrp->spp_pathmaxrxt) {
5374                                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
5375                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5376                                                                 }
5377                                                         }
5378                                                         net->failure_threshold = paddrp->spp_pathmaxrxt;
5379                                                 }
5380                                         }
5381                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5382                                                 if (paddrp->spp_hbinterval != 0) {
5383                                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5384                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5385                                                         stcb->asoc.heart_beat_delay = 0;
5386                                                 }
5387                                                 /* Turn back on the timer */
5388                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5389                                                         if (paddrp->spp_hbinterval != 0) {
5390                                                                 net->heart_beat_delay = paddrp->spp_hbinterval;
5391                                                         } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5392                                                                 net->heart_beat_delay = 0;
5393                                                         }
5394                                                         if (net->dest_state & SCTP_ADDR_NOHB) {
5395                                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5396                                                         }
5397                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5398                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
5399                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5400                                                 }
5401                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5402                                         }
5403                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5404                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5405                                                         if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5406                                                                 net->dest_state |= SCTP_ADDR_NOHB;
5407                                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5408                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5409                                                                             inp, stcb, net,
5410                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
5411                                                                 }
5412                                                         }
5413                                                 }
5414                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5415                                         }
5416                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5417                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5418                                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5419                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5420                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
5421                                                         }
5422                                                         net->dest_state |= SCTP_ADDR_NO_PMTUD;
5423                                                         net->mtu = paddrp->spp_pathmtu;
5424                                                         switch (net->ro._l_addr.sa.sa_family) {
5425 #ifdef INET
5426                                                         case AF_INET:
5427                                                                 net->mtu += SCTP_MIN_V4_OVERHEAD;
5428                                                                 break;
5429 #endif
5430 #ifdef INET6
5431                                                         case AF_INET6:
5432                                                                 net->mtu += SCTP_MIN_OVERHEAD;
5433                                                                 break;
5434 #endif
5435                                                         default:
5436                                                                 break;
5437                                                         }
5438                                                         if (net->mtu < stcb->asoc.smallest_mtu) {
5439                                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5440                                                         }
5441                                                 }
5442                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5443                                         }
5444                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5445                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5446                                                         if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5447                                                                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5448                                                         }
5449                                                         net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5450                                                 }
5451                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5452                                         }
5453                                         if (paddrp->spp_flags & SPP_DSCP) {
5454                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5455                                                         net->dscp = paddrp->spp_dscp & 0xfc;
5456                                                         net->dscp |= 0x01;
5457                                                 }
5458                                                 stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5459                                                 stcb->asoc.default_dscp |= 0x01;
5460                                         }
5461 #ifdef INET6
5462                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5463                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5464                                                         if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5465                                                                 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5466                                                                 net->flowlabel |= 0x80000000;
5467                                                         }
5468                                                 }
5469                                                 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5470                                                 stcb->asoc.default_flowlabel |= 0x80000000;
5471                                         }
5472 #endif
5473                                 }
5474                                 SCTP_TCB_UNLOCK(stcb);
5475                         } else {
5476                                 /************************NO TCB, SET TO default stuff ******************/
5477                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5478                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5479                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5480                                         SCTP_INP_WLOCK(inp);
5481                                         /*
5482                                          * For the TOS/FLOWLABEL stuff you
5483                                          * set it with the options on the
5484                                          * socket
5485                                          */
5486                                         if (paddrp->spp_pathmaxrxt != 0) {
5487                                                 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5488                                         }
5489                                         if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5490                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5491                                         else if (paddrp->spp_hbinterval != 0) {
5492                                                 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5493                                                         paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5494                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5495                                         }
5496                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5497                                                 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5498                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5499                                                 } else if (paddrp->spp_hbinterval) {
5500                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5501                                                 }
5502                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5503                                         } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5504                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5505                                         }
5506                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5507                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5508                                         } else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5509                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5510                                         }
5511                                         if (paddrp->spp_flags & SPP_DSCP) {
5512                                                 inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5513                                                 inp->sctp_ep.default_dscp |= 0x01;
5514                                         }
5515 #ifdef INET6
5516                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5517                                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5518                                                         inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5519                                                         inp->sctp_ep.default_flowlabel |= 0x80000000;
5520                                                 }
5521                                         }
5522 #endif
5523                                         SCTP_INP_WUNLOCK(inp);
5524                                 } else {
5525                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5526                                         error = EINVAL;
5527                                 }
5528                         }
5529                         break;
5530                 }
5531         case SCTP_RTOINFO:
5532                 {
5533                         struct sctp_rtoinfo *srto;
5534                         uint32_t new_init, new_min, new_max;
5535
5536                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5537                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5538
5539                         if (stcb) {
5540                                 if (srto->srto_initial)
5541                                         new_init = srto->srto_initial;
5542                                 else
5543                                         new_init = stcb->asoc.initial_rto;
5544                                 if (srto->srto_max)
5545                                         new_max = srto->srto_max;
5546                                 else
5547                                         new_max = stcb->asoc.maxrto;
5548                                 if (srto->srto_min)
5549                                         new_min = srto->srto_min;
5550                                 else
5551                                         new_min = stcb->asoc.minrto;
5552                                 if ((new_min <= new_init) && (new_init <= new_max)) {
5553                                         stcb->asoc.initial_rto = new_init;
5554                                         stcb->asoc.maxrto = new_max;
5555                                         stcb->asoc.minrto = new_min;
5556                                 } else {
5557                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5558                                         error = EINVAL;
5559                                 }
5560                                 SCTP_TCB_UNLOCK(stcb);
5561                         } else {
5562                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5563                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5564                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5565                                         SCTP_INP_WLOCK(inp);
5566                                         if (srto->srto_initial)
5567                                                 new_init = srto->srto_initial;
5568                                         else
5569                                                 new_init = inp->sctp_ep.initial_rto;
5570                                         if (srto->srto_max)
5571                                                 new_max = srto->srto_max;
5572                                         else
5573                                                 new_max = inp->sctp_ep.sctp_maxrto;
5574                                         if (srto->srto_min)
5575                                                 new_min = srto->srto_min;
5576                                         else
5577                                                 new_min = inp->sctp_ep.sctp_minrto;
5578                                         if ((new_min <= new_init) && (new_init <= new_max)) {
5579                                                 inp->sctp_ep.initial_rto = new_init;
5580                                                 inp->sctp_ep.sctp_maxrto = new_max;
5581                                                 inp->sctp_ep.sctp_minrto = new_min;
5582                                         } else {
5583                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5584                                                 error = EINVAL;
5585                                         }
5586                                         SCTP_INP_WUNLOCK(inp);
5587                                 } else {
5588                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5589                                         error = EINVAL;
5590                                 }
5591                         }
5592                         break;
5593                 }
5594         case SCTP_ASSOCINFO:
5595                 {
5596                         struct sctp_assocparams *sasoc;
5597
5598                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5599                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5600                         if (sasoc->sasoc_cookie_life) {
5601                                 /* boundary check the cookie life */
5602                                 if (sasoc->sasoc_cookie_life < 1000)
5603                                         sasoc->sasoc_cookie_life = 1000;
5604                                 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5605                                         sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5606                                 }
5607                         }
5608                         if (stcb) {
5609                                 if (sasoc->sasoc_asocmaxrxt)
5610                                         stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5611                                 if (sasoc->sasoc_cookie_life) {
5612                                         stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5613                                 }
5614                                 SCTP_TCB_UNLOCK(stcb);
5615                         } else {
5616                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5617                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5618                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5619                                         SCTP_INP_WLOCK(inp);
5620                                         if (sasoc->sasoc_asocmaxrxt)
5621                                                 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5622                                         if (sasoc->sasoc_cookie_life) {
5623                                                 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5624                                         }
5625                                         SCTP_INP_WUNLOCK(inp);
5626                                 } else {
5627                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5628                                         error = EINVAL;
5629                                 }
5630                         }
5631                         break;
5632                 }
5633         case SCTP_INITMSG:
5634                 {
5635                         struct sctp_initmsg *sinit;
5636
5637                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5638                         SCTP_INP_WLOCK(inp);
5639                         if (sinit->sinit_num_ostreams)
5640                                 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5641
5642                         if (sinit->sinit_max_instreams)
5643                                 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5644
5645                         if (sinit->sinit_max_attempts)
5646                                 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5647
5648                         if (sinit->sinit_max_init_timeo)
5649                                 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5650                         SCTP_INP_WUNLOCK(inp);
5651                         break;
5652                 }
5653         case SCTP_PRIMARY_ADDR:
5654                 {
5655                         struct sctp_setprim *spa;
5656                         struct sctp_nets *net;
5657                         struct sockaddr *addr;
5658
5659 #if defined(INET) && defined(INET6)
5660                         struct sockaddr_in sin_store;
5661
5662 #endif
5663
5664                         SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5665                         SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5666
5667 #if defined(INET) && defined(INET6)
5668                         if (spa->ssp_addr.ss_family == AF_INET6) {
5669                                 struct sockaddr_in6 *sin6;
5670
5671                                 sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5672                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5673                                         in6_sin6_2_sin(&sin_store, sin6);
5674                                         addr = (struct sockaddr *)&sin_store;
5675                                 } else {
5676                                         addr = (struct sockaddr *)&spa->ssp_addr;
5677                                 }
5678                         } else {
5679                                 addr = (struct sockaddr *)&spa->ssp_addr;
5680                         }
5681 #else
5682                         addr = (struct sockaddr *)&spa->ssp_addr;
5683 #endif
5684                         if (stcb != NULL) {
5685                                 net = sctp_findnet(stcb, addr);
5686                         } else {
5687                                 /*
5688                                  * We increment here since
5689                                  * sctp_findassociation_ep_addr() wil do a
5690                                  * decrement if it finds the stcb as long as
5691                                  * the locked tcb (last argument) is NOT a
5692                                  * TCB.. aka NULL.
5693                                  */
5694                                 net = NULL;
5695                                 SCTP_INP_INCR_REF(inp);
5696                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5697                                     &net, NULL, NULL);
5698                                 if (stcb == NULL) {
5699                                         SCTP_INP_DECR_REF(inp);
5700                                 }
5701                         }
5702
5703                         if ((stcb != NULL) && (net != NULL)) {
5704                                 if (net != stcb->asoc.primary_destination) {
5705                                         if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5706                                                 /* Ok we need to set it */
5707                                                 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5708                                                         if ((stcb->asoc.alternate) &&
5709                                                             (!(net->dest_state & SCTP_ADDR_PF)) &&
5710                                                             (net->dest_state & SCTP_ADDR_REACHABLE)) {
5711                                                                 sctp_free_remote_addr(stcb->asoc.alternate);
5712                                                                 stcb->asoc.alternate = NULL;
5713                                                         }
5714                                                 } else {
5715                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5716                                                         error = EINVAL;
5717                                                 }
5718                                         } else {
5719                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5720                                                 error = EINVAL;
5721                                         }
5722                                 }
5723                         } else {
5724                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5725                                 error = EINVAL;
5726                         }
5727                         if (stcb != NULL) {
5728                                 SCTP_TCB_UNLOCK(stcb);
5729                         }
5730                         break;
5731                 }
5732         case SCTP_SET_DYNAMIC_PRIMARY:
5733                 {
5734                         union sctp_sockstore *ss;
5735
5736                         error = priv_check(curthread,
5737                             PRIV_NETINET_RESERVEDPORT);
5738                         if (error)
5739                                 break;
5740
5741                         SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5742                         /* SUPER USER CHECK? */
5743                         error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5744                         break;
5745                 }
5746         case SCTP_SET_PEER_PRIMARY_ADDR:
5747                 {
5748                         struct sctp_setpeerprim *sspp;
5749                         struct sockaddr *addr;
5750
5751 #if defined(INET) && defined(INET6)
5752                         struct sockaddr_in sin_store;
5753
5754 #endif
5755
5756                         SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5757                         SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5758                         if (stcb != NULL) {
5759                                 struct sctp_ifa *ifa;
5760
5761 #if defined(INET) && defined(INET6)
5762                                 if (sspp->sspp_addr.ss_family == AF_INET6) {
5763                                         struct sockaddr_in6 *sin6;
5764
5765                                         sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5766                                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5767                                                 in6_sin6_2_sin(&sin_store, sin6);
5768                                                 addr = (struct sockaddr *)&sin_store;
5769                                         } else {
5770                                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5771                                         }
5772                                 } else {
5773                                         addr = (struct sockaddr *)&sspp->sspp_addr;
5774                                 }
5775 #else
5776                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5777 #endif
5778                                 ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5779                                 if (ifa == NULL) {
5780                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5781                                         error = EINVAL;
5782                                         goto out_of_it;
5783                                 }
5784                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5785                                         /*
5786                                          * Must validate the ifa found is in
5787                                          * our ep
5788                                          */
5789                                         struct sctp_laddr *laddr;
5790                                         int found = 0;
5791
5792                                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5793                                                 if (laddr->ifa == NULL) {
5794                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5795                                                             __func__);
5796                                                         continue;
5797                                                 }
5798                                                 if (laddr->ifa == ifa) {
5799                                                         found = 1;
5800                                                         break;
5801                                                 }
5802                                         }
5803                                         if (!found) {
5804                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5805                                                 error = EINVAL;
5806                                                 goto out_of_it;
5807                                         }
5808                                 } else {
5809                                         switch (addr->sa_family) {
5810 #ifdef INET
5811                                         case AF_INET:
5812                                                 {
5813                                                         struct sockaddr_in *sin;
5814
5815                                                         sin = (struct sockaddr_in *)addr;
5816                                                         if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5817                                                             &sin->sin_addr) != 0) {
5818                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5819                                                                 error = EINVAL;
5820                                                                 goto out_of_it;
5821                                                         }
5822                                                         break;
5823                                                 }
5824 #endif
5825 #ifdef INET6
5826                                         case AF_INET6:
5827                                                 {
5828                                                         struct sockaddr_in6 *sin6;
5829
5830                                                         sin6 = (struct sockaddr_in6 *)addr;
5831                                                         if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5832                                                             &sin6->sin6_addr) != 0) {
5833                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5834                                                                 error = EINVAL;
5835                                                                 goto out_of_it;
5836                                                         }
5837                                                         break;
5838                                                 }
5839 #endif
5840                                         default:
5841                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5842                                                 error = EINVAL;
5843                                                 goto out_of_it;
5844                                         }
5845                                 }
5846                                 if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5847                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5848                                         error = EINVAL;
5849                                 }
5850                 out_of_it:
5851                                 SCTP_TCB_UNLOCK(stcb);
5852                         } else {
5853                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5854                                 error = EINVAL;
5855                         }
5856                         break;
5857                 }
5858         case SCTP_BINDX_ADD_ADDR:
5859                 {
5860                         struct sctp_getaddresses *addrs;
5861                         struct thread *td;
5862
5863                         td = (struct thread *)p;
5864                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5865                             optsize);
5866 #ifdef INET
5867                         if (addrs->addr->sa_family == AF_INET) {
5868                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5869                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5870                                         error = EINVAL;
5871                                         break;
5872                                 }
5873                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5874                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5875                                         break;
5876                                 }
5877                         } else
5878 #endif
5879 #ifdef INET6
5880                         if (addrs->addr->sa_family == AF_INET6) {
5881                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5882                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5883                                         error = EINVAL;
5884                                         break;
5885                                 }
5886                                 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5887                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5888                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5889                                         break;
5890                                 }
5891                         } else
5892 #endif
5893                         {
5894                                 error = EAFNOSUPPORT;
5895                                 break;
5896                         }
5897                         sctp_bindx_add_address(so, inp, addrs->addr,
5898                             addrs->sget_assoc_id, vrf_id,
5899                             &error, p);
5900                         break;
5901                 }
5902         case SCTP_BINDX_REM_ADDR:
5903                 {
5904                         struct sctp_getaddresses *addrs;
5905                         struct thread *td;
5906
5907                         td = (struct thread *)p;
5908
5909                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5910 #ifdef INET
5911                         if (addrs->addr->sa_family == AF_INET) {
5912                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5913                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5914                                         error = EINVAL;
5915                                         break;
5916                                 }
5917                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5918                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5919                                         break;
5920                                 }
5921                         } else
5922 #endif
5923 #ifdef INET6
5924                         if (addrs->addr->sa_family == AF_INET6) {
5925                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5926                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5927                                         error = EINVAL;
5928                                         break;
5929                                 }
5930                                 if (td != NULL &&
5931                                     (error = prison_local_ip6(td->td_ucred,
5932                                     &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5933                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5934                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5935                                         break;
5936                                 }
5937                         } else
5938 #endif
5939                         {
5940                                 error = EAFNOSUPPORT;
5941                                 break;
5942                         }
5943                         sctp_bindx_delete_address(inp, addrs->addr,
5944                             addrs->sget_assoc_id, vrf_id,
5945                             &error);
5946                         break;
5947                 }
5948         case SCTP_EVENT:
5949                 {
5950                         struct sctp_event *event;
5951                         uint32_t event_type;
5952
5953                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
5954                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
5955                         switch (event->se_type) {
5956                         case SCTP_ASSOC_CHANGE:
5957                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
5958                                 break;
5959                         case SCTP_PEER_ADDR_CHANGE:
5960                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
5961                                 break;
5962                         case SCTP_REMOTE_ERROR:
5963                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
5964                                 break;
5965                         case SCTP_SEND_FAILED:
5966                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
5967                                 break;
5968                         case SCTP_SHUTDOWN_EVENT:
5969                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
5970                                 break;
5971                         case SCTP_ADAPTATION_INDICATION:
5972                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
5973                                 break;
5974                         case SCTP_PARTIAL_DELIVERY_EVENT:
5975                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
5976                                 break;
5977                         case SCTP_AUTHENTICATION_EVENT:
5978                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
5979                                 break;
5980                         case SCTP_STREAM_RESET_EVENT:
5981                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
5982                                 break;
5983                         case SCTP_SENDER_DRY_EVENT:
5984                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
5985                                 break;
5986                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
5987                                 event_type = 0;
5988                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5989                                 error = ENOTSUP;
5990                                 break;
5991                         case SCTP_ASSOC_RESET_EVENT:
5992                                 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
5993                                 break;
5994                         case SCTP_STREAM_CHANGE_EVENT:
5995                                 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
5996                                 break;
5997                         case SCTP_SEND_FAILED_EVENT:
5998                                 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
5999                                 break;
6000                         default:
6001                                 event_type = 0;
6002                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6003                                 error = EINVAL;
6004                                 break;
6005                         }
6006                         if (event_type > 0) {
6007                                 if (stcb) {
6008                                         if (event->se_on) {
6009                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6010                                                 if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6011                                                         if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6012                                                             TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6013                                                             (stcb->asoc.stream_queue_cnt == 0)) {
6014                                                                 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
6015                                                         }
6016                                                 }
6017                                         } else {
6018                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6019                                         }
6020                                         SCTP_TCB_UNLOCK(stcb);
6021                                 } else {
6022                                         /*
6023                                          * We don't want to send up a storm
6024                                          * of events, so return an error for
6025                                          * sender dry events
6026                                          */
6027                                         if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6028                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6029                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6030                                             ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6031                                             (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6032                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6033                                                 error = ENOTSUP;
6034                                                 break;
6035                                         }
6036                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6037                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6038                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6039                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6040                                                 SCTP_INP_WLOCK(inp);
6041                                                 if (event->se_on) {
6042                                                         sctp_feature_on(inp, event_type);
6043                                                 } else {
6044                                                         sctp_feature_off(inp, event_type);
6045                                                 }
6046                                                 SCTP_INP_WUNLOCK(inp);
6047                                         }
6048                                         if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6049                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6050                                                 SCTP_INP_RLOCK(inp);
6051                                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6052                                                         SCTP_TCB_LOCK(stcb);
6053                                                         if (event->se_on) {
6054                                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6055                                                         } else {
6056                                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6057                                                         }
6058                                                         SCTP_TCB_UNLOCK(stcb);
6059                                                 }
6060                                                 SCTP_INP_RUNLOCK(inp);
6061                                         }
6062                                 }
6063                         }
6064                         break;
6065                 }
6066         case SCTP_RECVRCVINFO:
6067                 {
6068                         int *onoff;
6069
6070                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6071                         SCTP_INP_WLOCK(inp);
6072                         if (*onoff != 0) {
6073                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6074                         } else {
6075                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6076                         }
6077                         SCTP_INP_WUNLOCK(inp);
6078                         break;
6079                 }
6080         case SCTP_RECVNXTINFO:
6081                 {
6082                         int *onoff;
6083
6084                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6085                         SCTP_INP_WLOCK(inp);
6086                         if (*onoff != 0) {
6087                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6088                         } else {
6089                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6090                         }
6091                         SCTP_INP_WUNLOCK(inp);
6092                         break;
6093                 }
6094         case SCTP_DEFAULT_SNDINFO:
6095                 {
6096                         struct sctp_sndinfo *info;
6097                         uint16_t policy;
6098
6099                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6100                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6101
6102                         if (stcb) {
6103                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6104                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6105                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6106                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6107                                         stcb->asoc.def_send.sinfo_flags |= policy;
6108                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6109                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6110                                 } else {
6111                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6112                                         error = EINVAL;
6113                                 }
6114                                 SCTP_TCB_UNLOCK(stcb);
6115                         } else {
6116                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6117                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6118                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6119                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6120                                         SCTP_INP_WLOCK(inp);
6121                                         inp->def_send.sinfo_stream = info->snd_sid;
6122                                         policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6123                                         inp->def_send.sinfo_flags = info->snd_flags;
6124                                         inp->def_send.sinfo_flags |= policy;
6125                                         inp->def_send.sinfo_ppid = info->snd_ppid;
6126                                         inp->def_send.sinfo_context = info->snd_context;
6127                                         SCTP_INP_WUNLOCK(inp);
6128                                 }
6129                                 if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6130                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6131                                         SCTP_INP_RLOCK(inp);
6132                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6133                                                 SCTP_TCB_LOCK(stcb);
6134                                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6135                                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6136                                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6137                                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6138                                                         stcb->asoc.def_send.sinfo_flags |= policy;
6139                                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6140                                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6141                                                 }
6142                                                 SCTP_TCB_UNLOCK(stcb);
6143                                         }
6144                                         SCTP_INP_RUNLOCK(inp);
6145                                 }
6146                         }
6147                         break;
6148                 }
6149         case SCTP_DEFAULT_PRINFO:
6150                 {
6151                         struct sctp_default_prinfo *info;
6152
6153                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6154                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6155
6156                         if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6157                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6158                                 error = EINVAL;
6159                                 break;
6160                         }
6161                         if (stcb) {
6162                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6163                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6164                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6165                                 SCTP_TCB_UNLOCK(stcb);
6166                         } else {
6167                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6168                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6169                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6170                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6171                                         SCTP_INP_WLOCK(inp);
6172                                         inp->def_send.sinfo_flags &= 0xfff0;
6173                                         inp->def_send.sinfo_flags |= info->pr_policy;
6174                                         inp->def_send.sinfo_timetolive = info->pr_value;
6175                                         SCTP_INP_WUNLOCK(inp);
6176                                 }
6177                                 if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6178                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6179                                         SCTP_INP_RLOCK(inp);
6180                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6181                                                 SCTP_TCB_LOCK(stcb);
6182                                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6183                                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6184                                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6185                                                 SCTP_TCB_UNLOCK(stcb);
6186                                         }
6187                                         SCTP_INP_RUNLOCK(inp);
6188                                 }
6189                         }
6190                         break;
6191                 }
6192         case SCTP_PEER_ADDR_THLDS:
6193                 /* Applies to the specific association */
6194                 {
6195                         struct sctp_paddrthlds *thlds;
6196                         struct sctp_nets *net;
6197                         struct sockaddr *addr;
6198
6199 #if defined(INET) && defined(INET6)
6200                         struct sockaddr_in sin_store;
6201
6202 #endif
6203
6204                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6205                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6206
6207 #if defined(INET) && defined(INET6)
6208                         if (thlds->spt_address.ss_family == AF_INET6) {
6209                                 struct sockaddr_in6 *sin6;
6210
6211                                 sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6212                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6213                                         in6_sin6_2_sin(&sin_store, sin6);
6214                                         addr = (struct sockaddr *)&sin_store;
6215                                 } else {
6216                                         addr = (struct sockaddr *)&thlds->spt_address;
6217                                 }
6218                         } else {
6219                                 addr = (struct sockaddr *)&thlds->spt_address;
6220                         }
6221 #else
6222                         addr = (struct sockaddr *)&thlds->spt_address;
6223 #endif
6224                         if (stcb != NULL) {
6225                                 net = sctp_findnet(stcb, addr);
6226                         } else {
6227                                 /*
6228                                  * We increment here since
6229                                  * sctp_findassociation_ep_addr() wil do a
6230                                  * decrement if it finds the stcb as long as
6231                                  * the locked tcb (last argument) is NOT a
6232                                  * TCB.. aka NULL.
6233                                  */
6234                                 net = NULL;
6235                                 SCTP_INP_INCR_REF(inp);
6236                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
6237                                     &net, NULL, NULL);
6238                                 if (stcb == NULL) {
6239                                         SCTP_INP_DECR_REF(inp);
6240                                 }
6241                         }
6242                         if ((stcb != NULL) && (net == NULL)) {
6243 #ifdef INET
6244                                 if (addr->sa_family == AF_INET) {
6245
6246                                         struct sockaddr_in *sin;
6247
6248                                         sin = (struct sockaddr_in *)addr;
6249                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6250                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6251                                                 SCTP_TCB_UNLOCK(stcb);
6252                                                 error = EINVAL;
6253                                                 break;
6254                                         }
6255                                 } else
6256 #endif
6257 #ifdef INET6
6258                                 if (addr->sa_family == AF_INET6) {
6259                                         struct sockaddr_in6 *sin6;
6260
6261                                         sin6 = (struct sockaddr_in6 *)addr;
6262                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6263                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6264                                                 SCTP_TCB_UNLOCK(stcb);
6265                                                 error = EINVAL;
6266                                                 break;
6267                                         }
6268                                 } else
6269 #endif
6270                                 {
6271                                         error = EAFNOSUPPORT;
6272                                         SCTP_TCB_UNLOCK(stcb);
6273                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6274                                         break;
6275                                 }
6276                         }
6277                         if (stcb != NULL) {
6278                                 if (net != NULL) {
6279                                         net->failure_threshold = thlds->spt_pathmaxrxt;
6280                                         net->pf_threshold = thlds->spt_pathpfthld;
6281                                         if (net->dest_state & SCTP_ADDR_PF) {
6282                                                 if ((net->error_count > net->failure_threshold) ||
6283                                                     (net->error_count <= net->pf_threshold)) {
6284                                                         net->dest_state &= ~SCTP_ADDR_PF;
6285                                                 }
6286                                         } else {
6287                                                 if ((net->error_count > net->pf_threshold) &&
6288                                                     (net->error_count <= net->failure_threshold)) {
6289                                                         net->dest_state |= SCTP_ADDR_PF;
6290                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6291                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6292                                                             stcb->sctp_ep, stcb, net,
6293                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
6294                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6295                                                 }
6296                                         }
6297                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
6298                                                 if (net->error_count > net->failure_threshold) {
6299                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
6300                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6301                                                 }
6302                                         } else {
6303                                                 if (net->error_count <= net->failure_threshold) {
6304                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
6305                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6306                                                 }
6307                                         }
6308                                 } else {
6309                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6310                                                 net->failure_threshold = thlds->spt_pathmaxrxt;
6311                                                 net->pf_threshold = thlds->spt_pathpfthld;
6312                                                 if (net->dest_state & SCTP_ADDR_PF) {
6313                                                         if ((net->error_count > net->failure_threshold) ||
6314                                                             (net->error_count <= net->pf_threshold)) {
6315                                                                 net->dest_state &= ~SCTP_ADDR_PF;
6316                                                         }
6317                                                 } else {
6318                                                         if ((net->error_count > net->pf_threshold) &&
6319                                                             (net->error_count <= net->failure_threshold)) {
6320                                                                 net->dest_state |= SCTP_ADDR_PF;
6321                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6322                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6323                                                                     stcb->sctp_ep, stcb, net,
6324                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_18);
6325                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6326                                                         }
6327                                                 }
6328                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
6329                                                         if (net->error_count > net->failure_threshold) {
6330                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
6331                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6332                                                         }
6333                                                 } else {
6334                                                         if (net->error_count <= net->failure_threshold) {
6335                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
6336                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6337                                                         }
6338                                                 }
6339                                         }
6340                                         stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6341                                         stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6342                                 }
6343                                 SCTP_TCB_UNLOCK(stcb);
6344                         } else {
6345                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6346                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6347                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6348                                         SCTP_INP_WLOCK(inp);
6349                                         inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6350                                         inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6351                                         SCTP_INP_WUNLOCK(inp);
6352                                 } else {
6353                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6354                                         error = EINVAL;
6355                                 }
6356                         }
6357                         break;
6358                 }
6359         case SCTP_REMOTE_UDP_ENCAPS_PORT:
6360                 {
6361                         struct sctp_udpencaps *encaps;
6362                         struct sctp_nets *net;
6363                         struct sockaddr *addr;
6364
6365 #if defined(INET) && defined(INET6)
6366                         struct sockaddr_in sin_store;
6367
6368 #endif
6369
6370                         SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6371                         SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6372
6373 #if defined(INET) && defined(INET6)
6374                         if (encaps->sue_address.ss_family == AF_INET6) {
6375                                 struct sockaddr_in6 *sin6;
6376
6377                                 sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6378                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6379                                         in6_sin6_2_sin(&sin_store, sin6);
6380                                         addr = (struct sockaddr *)&sin_store;
6381                                 } else {
6382                                         addr = (struct sockaddr *)&encaps->sue_address;
6383                                 }
6384                         } else {
6385                                 addr = (struct sockaddr *)&encaps->sue_address;
6386                         }
6387 #else
6388                         addr = (struct sockaddr *)&encaps->sue_address;
6389 #endif
6390                         if (stcb != NULL) {
6391                                 net = sctp_findnet(stcb, addr);
6392                         } else {
6393                                 /*
6394                                  * We increment here since
6395                                  * sctp_findassociation_ep_addr() wil do a
6396                                  * decrement if it finds the stcb as long as
6397                                  * the locked tcb (last argument) is NOT a
6398                                  * TCB.. aka NULL.
6399                                  */
6400                                 net = NULL;
6401                                 SCTP_INP_INCR_REF(inp);
6402                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6403                                 if (stcb == NULL) {
6404                                         SCTP_INP_DECR_REF(inp);
6405                                 }
6406                         }
6407                         if ((stcb != NULL) && (net == NULL)) {
6408 #ifdef INET
6409                                 if (addr->sa_family == AF_INET) {
6410
6411                                         struct sockaddr_in *sin;
6412
6413                                         sin = (struct sockaddr_in *)addr;
6414                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6415                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6416                                                 SCTP_TCB_UNLOCK(stcb);
6417                                                 error = EINVAL;
6418                                                 break;
6419                                         }
6420                                 } else
6421 #endif
6422 #ifdef INET6
6423                                 if (addr->sa_family == AF_INET6) {
6424                                         struct sockaddr_in6 *sin6;
6425
6426                                         sin6 = (struct sockaddr_in6 *)addr;
6427                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6428                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6429                                                 SCTP_TCB_UNLOCK(stcb);
6430                                                 error = EINVAL;
6431                                                 break;
6432                                         }
6433                                 } else
6434 #endif
6435                                 {
6436                                         error = EAFNOSUPPORT;
6437                                         SCTP_TCB_UNLOCK(stcb);
6438                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6439                                         break;
6440                                 }
6441                         }
6442                         if (stcb != NULL) {
6443                                 if (net != NULL) {
6444                                         net->port = encaps->sue_port;
6445                                 } else {
6446                                         stcb->asoc.port = encaps->sue_port;
6447                                 }
6448                                 SCTP_TCB_UNLOCK(stcb);
6449                         } else {
6450                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6451                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6452                                     (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6453                                         SCTP_INP_WLOCK(inp);
6454                                         inp->sctp_ep.port = encaps->sue_port;
6455                                         SCTP_INP_WUNLOCK(inp);
6456                                 } else {
6457                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6458                                         error = EINVAL;
6459                                 }
6460                         }
6461                         break;
6462                 }
6463         case SCTP_ECN_SUPPORTED:
6464                 {
6465                         struct sctp_assoc_value *av;
6466
6467                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6468                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6469
6470                         if (stcb) {
6471                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6472                                 error = EINVAL;
6473                                 SCTP_TCB_UNLOCK(stcb);
6474                         } else {
6475                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6476                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6477                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6478                                         SCTP_INP_WLOCK(inp);
6479                                         if (av->assoc_value == 0) {
6480                                                 inp->ecn_supported = 0;
6481                                         } else {
6482                                                 inp->ecn_supported = 1;
6483                                         }
6484                                         SCTP_INP_WUNLOCK(inp);
6485                                 } else {
6486                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6487                                         error = EINVAL;
6488                                 }
6489                         }
6490                         break;
6491                 }
6492         case SCTP_PR_SUPPORTED:
6493                 {
6494                         struct sctp_assoc_value *av;
6495
6496                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6497                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6498
6499                         if (stcb) {
6500                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6501                                 error = EINVAL;
6502                                 SCTP_TCB_UNLOCK(stcb);
6503                         } else {
6504                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6505                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6506                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6507                                         SCTP_INP_WLOCK(inp);
6508                                         if (av->assoc_value == 0) {
6509                                                 inp->prsctp_supported = 0;
6510                                         } else {
6511                                                 inp->prsctp_supported = 1;
6512                                         }
6513                                         SCTP_INP_WUNLOCK(inp);
6514                                 } else {
6515                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6516                                         error = EINVAL;
6517                                 }
6518                         }
6519                         break;
6520                 }
6521         case SCTP_AUTH_SUPPORTED:
6522                 {
6523                         struct sctp_assoc_value *av;
6524
6525                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6526                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6527
6528                         if (stcb) {
6529                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6530                                 error = EINVAL;
6531                                 SCTP_TCB_UNLOCK(stcb);
6532                         } else {
6533                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6534                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6535                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6536                                         if ((av->assoc_value == 0) &&
6537                                             (inp->asconf_supported == 1)) {
6538                                                 /*
6539                                                  * AUTH is required for
6540                                                  * ASCONF
6541                                                  */
6542                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6543                                                 error = EINVAL;
6544                                         } else {
6545                                                 SCTP_INP_WLOCK(inp);
6546                                                 if (av->assoc_value == 0) {
6547                                                         inp->auth_supported = 0;
6548                                                 } else {
6549                                                         inp->auth_supported = 1;
6550                                                 }
6551                                                 SCTP_INP_WUNLOCK(inp);
6552                                         }
6553                                 } else {
6554                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6555                                         error = EINVAL;
6556                                 }
6557                         }
6558                         break;
6559                 }
6560         case SCTP_ASCONF_SUPPORTED:
6561                 {
6562                         struct sctp_assoc_value *av;
6563
6564                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6565                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6566
6567                         if (stcb) {
6568                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6569                                 error = EINVAL;
6570                                 SCTP_TCB_UNLOCK(stcb);
6571                         } else {
6572                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6573                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6574                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6575                                         if ((av->assoc_value != 0) &&
6576                                             (inp->auth_supported == 0)) {
6577                                                 /*
6578                                                  * AUTH is required for
6579                                                  * ASCONF
6580                                                  */
6581                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6582                                                 error = EINVAL;
6583                                         } else {
6584                                                 SCTP_INP_WLOCK(inp);
6585                                                 if (av->assoc_value == 0) {
6586                                                         inp->asconf_supported = 0;
6587                                                         sctp_auth_delete_chunk(SCTP_ASCONF,
6588                                                             inp->sctp_ep.local_auth_chunks);
6589                                                         sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6590                                                             inp->sctp_ep.local_auth_chunks);
6591                                                 } else {
6592                                                         inp->asconf_supported = 1;
6593                                                         sctp_auth_add_chunk(SCTP_ASCONF,
6594                                                             inp->sctp_ep.local_auth_chunks);
6595                                                         sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6596                                                             inp->sctp_ep.local_auth_chunks);
6597                                                 }
6598                                                 SCTP_INP_WUNLOCK(inp);
6599                                         }
6600                                 } else {
6601                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6602                                         error = EINVAL;
6603                                 }
6604                         }
6605                         break;
6606                 }
6607         case SCTP_RECONFIG_SUPPORTED:
6608                 {
6609                         struct sctp_assoc_value *av;
6610
6611                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6612                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6613
6614                         if (stcb) {
6615                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6616                                 error = EINVAL;
6617                                 SCTP_TCB_UNLOCK(stcb);
6618                         } else {
6619                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6620                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6621                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6622                                         SCTP_INP_WLOCK(inp);
6623                                         if (av->assoc_value == 0) {
6624                                                 inp->reconfig_supported = 0;
6625                                         } else {
6626                                                 inp->reconfig_supported = 1;
6627                                         }
6628                                         SCTP_INP_WUNLOCK(inp);
6629                                 } else {
6630                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6631                                         error = EINVAL;
6632                                 }
6633                         }
6634                         break;
6635                 }
6636         case SCTP_NRSACK_SUPPORTED:
6637                 {
6638                         struct sctp_assoc_value *av;
6639
6640                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6641                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6642
6643                         if (stcb) {
6644                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6645                                 error = EINVAL;
6646                                 SCTP_TCB_UNLOCK(stcb);
6647                         } else {
6648                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6649                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6650                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6651                                         SCTP_INP_WLOCK(inp);
6652                                         if (av->assoc_value == 0) {
6653                                                 inp->nrsack_supported = 0;
6654                                         } else {
6655                                                 inp->nrsack_supported = 1;
6656                                         }
6657                                         SCTP_INP_WUNLOCK(inp);
6658                                 } else {
6659                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6660                                         error = EINVAL;
6661                                 }
6662                         }
6663                         break;
6664                 }
6665         case SCTP_PKTDROP_SUPPORTED:
6666                 {
6667                         struct sctp_assoc_value *av;
6668
6669                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6670                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6671
6672                         if (stcb) {
6673                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6674                                 error = EINVAL;
6675                                 SCTP_TCB_UNLOCK(stcb);
6676                         } else {
6677                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6678                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6679                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6680                                         SCTP_INP_WLOCK(inp);
6681                                         if (av->assoc_value == 0) {
6682                                                 inp->pktdrop_supported = 0;
6683                                         } else {
6684                                                 inp->pktdrop_supported = 1;
6685                                         }
6686                                         SCTP_INP_WUNLOCK(inp);
6687                                 } else {
6688                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6689                                         error = EINVAL;
6690                                 }
6691                         }
6692                         break;
6693                 }
6694         case SCTP_MAX_CWND:
6695                 {
6696                         struct sctp_assoc_value *av;
6697                         struct sctp_nets *net;
6698
6699                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6700                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6701
6702                         if (stcb) {
6703                                 stcb->asoc.max_cwnd = av->assoc_value;
6704                                 if (stcb->asoc.max_cwnd > 0) {
6705                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6706                                                 if ((net->cwnd > stcb->asoc.max_cwnd) &&
6707                                                     (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
6708                                                         net->cwnd = stcb->asoc.max_cwnd;
6709                                                         if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
6710                                                                 net->cwnd = net->mtu - sizeof(struct sctphdr);
6711                                                         }
6712                                                 }
6713                                         }
6714                                 }
6715                                 SCTP_TCB_UNLOCK(stcb);
6716                         } else {
6717                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6718                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6719                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6720                                         SCTP_INP_WLOCK(inp);
6721                                         inp->max_cwnd = av->assoc_value;
6722                                         SCTP_INP_WUNLOCK(inp);
6723                                 } else {
6724                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6725                                         error = EINVAL;
6726                                 }
6727                         }
6728                         break;
6729                 }
6730         default:
6731                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6732                 error = ENOPROTOOPT;
6733                 break;
6734         }                       /* end switch (opt) */
6735         return (error);
6736 }
6737
6738 int
6739 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6740 {
6741         void *optval = NULL;
6742         size_t optsize = 0;
6743         void *p;
6744         int error = 0;
6745         struct sctp_inpcb *inp;
6746
6747         if ((sopt->sopt_level == SOL_SOCKET) &&
6748             (sopt->sopt_name == SO_SETFIB)) {
6749                 inp = (struct sctp_inpcb *)so->so_pcb;
6750                 if (inp == NULL) {
6751                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6752                         return (EINVAL);
6753                 }
6754                 SCTP_INP_WLOCK(inp);
6755                 inp->fibnum = so->so_fibnum;
6756                 SCTP_INP_WUNLOCK(inp);
6757                 return (0);
6758         }
6759         if (sopt->sopt_level != IPPROTO_SCTP) {
6760                 /* wrong proto level... send back up to IP */
6761 #ifdef INET6
6762                 if (INP_CHECK_SOCKAF(so, AF_INET6))
6763                         error = ip6_ctloutput(so, sopt);
6764 #endif                          /* INET6 */
6765 #if defined(INET) && defined(INET6)
6766                 else
6767 #endif
6768 #ifdef INET
6769                         error = ip_ctloutput(so, sopt);
6770 #endif
6771                 return (error);
6772         }
6773         optsize = sopt->sopt_valsize;
6774         if (optsize) {
6775                 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6776                 if (optval == NULL) {
6777                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6778                         return (ENOBUFS);
6779                 }
6780                 error = sooptcopyin(sopt, optval, optsize, optsize);
6781                 if (error) {
6782                         SCTP_FREE(optval, SCTP_M_SOCKOPT);
6783                         goto out;
6784                 }
6785         }
6786         p = (void *)sopt->sopt_td;
6787         if (sopt->sopt_dir == SOPT_SET) {
6788                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6789         } else if (sopt->sopt_dir == SOPT_GET) {
6790                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6791         } else {
6792                 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6793                 error = EINVAL;
6794         }
6795         if ((error == 0) && (optval != NULL)) {
6796                 error = sooptcopyout(sopt, optval, optsize);
6797                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6798         } else if (optval != NULL) {
6799                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6800         }
6801 out:
6802         return (error);
6803 }
6804
6805 #ifdef INET
6806 static int
6807 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6808 {
6809         int error = 0;
6810         int create_lock_on = 0;
6811         uint32_t vrf_id;
6812         struct sctp_inpcb *inp;
6813         struct sctp_tcb *stcb = NULL;
6814
6815         inp = (struct sctp_inpcb *)so->so_pcb;
6816         if (inp == NULL) {
6817                 /* I made the same as TCP since we are not setup? */
6818                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6819                 return (ECONNRESET);
6820         }
6821         if (addr == NULL) {
6822                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6823                 return EINVAL;
6824         }
6825         switch (addr->sa_family) {
6826 #ifdef INET6
6827         case AF_INET6:
6828                 {
6829                         struct sockaddr_in6 *sin6p;
6830
6831                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6832                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6833                                 return (EINVAL);
6834                         }
6835                         sin6p = (struct sockaddr_in6 *)addr;
6836                         if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6837                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6838                                 return (error);
6839                         }
6840                         break;
6841                 }
6842 #endif
6843 #ifdef INET
6844         case AF_INET:
6845                 {
6846                         struct sockaddr_in *sinp;
6847
6848                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
6849                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6850                                 return (EINVAL);
6851                         }
6852                         sinp = (struct sockaddr_in *)addr;
6853                         if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6854                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6855                                 return (error);
6856                         }
6857                         break;
6858                 }
6859 #endif
6860         default:
6861                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6862                 return (EAFNOSUPPORT);
6863         }
6864         SCTP_INP_INCR_REF(inp);
6865         SCTP_ASOC_CREATE_LOCK(inp);
6866         create_lock_on = 1;
6867
6868
6869         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6870             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6871                 /* Should I really unlock ? */
6872                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6873                 error = EFAULT;
6874                 goto out_now;
6875         }
6876 #ifdef INET6
6877         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6878             (addr->sa_family == AF_INET6)) {
6879                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6880                 error = EINVAL;
6881                 goto out_now;
6882         }
6883 #endif
6884         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6885             SCTP_PCB_FLAGS_UNBOUND) {
6886                 /* Bind a ephemeral port */
6887                 error = sctp_inpcb_bind(so, NULL, NULL, p);
6888                 if (error) {
6889                         goto out_now;
6890                 }
6891         }
6892         /* Now do we connect? */
6893         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6894             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6895                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6896                 error = EINVAL;
6897                 goto out_now;
6898         }
6899         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6900             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6901                 /* We are already connected AND the TCP model */
6902                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6903                 error = EADDRINUSE;
6904                 goto out_now;
6905         }
6906         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6907                 SCTP_INP_RLOCK(inp);
6908                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
6909                 SCTP_INP_RUNLOCK(inp);
6910         } else {
6911                 /*
6912                  * We increment here since sctp_findassociation_ep_addr()
6913                  * will do a decrement if it finds the stcb as long as the
6914                  * locked tcb (last argument) is NOT a TCB.. aka NULL.
6915                  */
6916                 SCTP_INP_INCR_REF(inp);
6917                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
6918                 if (stcb == NULL) {
6919                         SCTP_INP_DECR_REF(inp);
6920                 } else {
6921                         SCTP_TCB_UNLOCK(stcb);
6922                 }
6923         }
6924         if (stcb != NULL) {
6925                 /* Already have or am bring up an association */
6926                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
6927                 error = EALREADY;
6928                 goto out_now;
6929         }
6930         vrf_id = inp->def_vrf_id;
6931         /* We are GOOD to go */
6932         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
6933         if (stcb == NULL) {
6934                 /* Gak! no memory */
6935                 goto out_now;
6936         }
6937         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
6938                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
6939                 /* Set the connected flag so we can queue data */
6940                 soisconnecting(so);
6941         }
6942         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
6943         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
6944
6945         /* initialize authentication parameters for the assoc */
6946         sctp_initialize_auth_params(inp, stcb);
6947
6948         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
6949         SCTP_TCB_UNLOCK(stcb);
6950 out_now:
6951         if (create_lock_on) {
6952                 SCTP_ASOC_CREATE_UNLOCK(inp);
6953         }
6954         SCTP_INP_DECR_REF(inp);
6955         return (error);
6956 }
6957
6958 #endif
6959
6960 int
6961 sctp_listen(struct socket *so, int backlog, struct thread *p)
6962 {
6963         /*
6964          * Note this module depends on the protocol processing being called
6965          * AFTER any socket level flags and backlog are applied to the
6966          * socket. The traditional way that the socket flags are applied is
6967          * AFTER protocol processing. We have made a change to the
6968          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
6969          * place if the socket API for SCTP is to work properly.
6970          */
6971
6972         int error = 0;
6973         struct sctp_inpcb *inp;
6974
6975         inp = (struct sctp_inpcb *)so->so_pcb;
6976         if (inp == NULL) {
6977                 /* I made the same as TCP since we are not setup? */
6978                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6979                 return (ECONNRESET);
6980         }
6981         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
6982                 /* See if we have a listener */
6983                 struct sctp_inpcb *tinp;
6984                 union sctp_sockstore store;
6985
6986                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
6987                         /* not bound all */
6988                         struct sctp_laddr *laddr;
6989
6990                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6991                                 memcpy(&store, &laddr->ifa->address, sizeof(store));
6992                                 switch (store.sa.sa_family) {
6993 #ifdef INET
6994                                 case AF_INET:
6995                                         store.sin.sin_port = inp->sctp_lport;
6996                                         break;
6997 #endif
6998 #ifdef INET6
6999                                 case AF_INET6:
7000                                         store.sin6.sin6_port = inp->sctp_lport;
7001                                         break;
7002 #endif
7003                                 default:
7004                                         break;
7005                                 }
7006                                 tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7007                                 if (tinp && (tinp != inp) &&
7008                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7009                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7010                                     (tinp->sctp_socket->so_qlimit)) {
7011                                         /*
7012                                          * we have a listener already and
7013                                          * its not this inp.
7014                                          */
7015                                         SCTP_INP_DECR_REF(tinp);
7016                                         return (EADDRINUSE);
7017                                 } else if (tinp) {
7018                                         SCTP_INP_DECR_REF(tinp);
7019                                 }
7020                         }
7021                 } else {
7022                         /* Setup a local addr bound all */
7023                         memset(&store, 0, sizeof(store));
7024 #ifdef INET6
7025                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7026                                 store.sa.sa_family = AF_INET6;
7027                                 store.sa.sa_len = sizeof(struct sockaddr_in6);
7028                         }
7029 #endif
7030 #ifdef INET
7031                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7032                                 store.sa.sa_family = AF_INET;
7033                                 store.sa.sa_len = sizeof(struct sockaddr_in);
7034                         }
7035 #endif
7036                         switch (store.sa.sa_family) {
7037 #ifdef INET
7038                         case AF_INET:
7039                                 store.sin.sin_port = inp->sctp_lport;
7040                                 break;
7041 #endif
7042 #ifdef INET6
7043                         case AF_INET6:
7044                                 store.sin6.sin6_port = inp->sctp_lport;
7045                                 break;
7046 #endif
7047                         default:
7048                                 break;
7049                         }
7050                         tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7051                         if (tinp && (tinp != inp) &&
7052                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7053                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7054                             (tinp->sctp_socket->so_qlimit)) {
7055                                 /*
7056                                  * we have a listener already and its not
7057                                  * this inp.
7058                                  */
7059                                 SCTP_INP_DECR_REF(tinp);
7060                                 return (EADDRINUSE);
7061                         } else if (tinp) {
7062                                 SCTP_INP_DECR_REF(tinp);
7063                         }
7064                 }
7065         }
7066         SCTP_INP_RLOCK(inp);
7067 #ifdef SCTP_LOCK_LOGGING
7068         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7069                 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7070         }
7071 #endif
7072         SOCK_LOCK(so);
7073         error = solisten_proto_check(so);
7074         SOCK_UNLOCK(so);
7075         if (error) {
7076                 SCTP_INP_RUNLOCK(inp);
7077                 return (error);
7078         }
7079         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7080             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7081                 /*
7082                  * The unlucky case - We are in the tcp pool with this guy.
7083                  * - Someone else is in the main inp slot. - We must move
7084                  * this guy (the listener) to the main slot - We must then
7085                  * move the guy that was listener to the TCP Pool.
7086                  */
7087                 if (sctp_swap_inpcb_for_listen(inp)) {
7088                         SCTP_INP_RUNLOCK(inp);
7089                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7090                         return (EADDRINUSE);
7091                 }
7092         }
7093         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7094             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7095                 /* We are already connected AND the TCP model */
7096                 SCTP_INP_RUNLOCK(inp);
7097                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7098                 return (EADDRINUSE);
7099         }
7100         SCTP_INP_RUNLOCK(inp);
7101         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7102                 /* We must do a bind. */
7103                 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7104                         /* bind error, probably perm */
7105                         return (error);
7106                 }
7107         }
7108         SOCK_LOCK(so);
7109         /* It appears for 7.0 and on, we must always call this. */
7110         solisten_proto(so, backlog);
7111         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7112                 /* remove the ACCEPTCONN flag for one-to-many sockets */
7113                 so->so_options &= ~SO_ACCEPTCONN;
7114         }
7115         if (backlog == 0) {
7116                 /* turning off listen */
7117                 so->so_options &= ~SO_ACCEPTCONN;
7118         }
7119         SOCK_UNLOCK(so);
7120         return (error);
7121 }
7122
7123 static int sctp_defered_wakeup_cnt = 0;
7124
7125 int
7126 sctp_accept(struct socket *so, struct sockaddr **addr)
7127 {
7128         struct sctp_tcb *stcb;
7129         struct sctp_inpcb *inp;
7130         union sctp_sockstore store;
7131
7132 #ifdef INET6
7133         int error;
7134
7135 #endif
7136         inp = (struct sctp_inpcb *)so->so_pcb;
7137
7138         if (inp == NULL) {
7139                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7140                 return (ECONNRESET);
7141         }
7142         SCTP_INP_RLOCK(inp);
7143         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7144                 SCTP_INP_RUNLOCK(inp);
7145                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7146                 return (EOPNOTSUPP);
7147         }
7148         if (so->so_state & SS_ISDISCONNECTED) {
7149                 SCTP_INP_RUNLOCK(inp);
7150                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7151                 return (ECONNABORTED);
7152         }
7153         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7154         if (stcb == NULL) {
7155                 SCTP_INP_RUNLOCK(inp);
7156                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7157                 return (ECONNRESET);
7158         }
7159         SCTP_TCB_LOCK(stcb);
7160         SCTP_INP_RUNLOCK(inp);
7161         store = stcb->asoc.primary_destination->ro._l_addr;
7162         stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
7163         SCTP_TCB_UNLOCK(stcb);
7164         switch (store.sa.sa_family) {
7165 #ifdef INET
7166         case AF_INET:
7167                 {
7168                         struct sockaddr_in *sin;
7169
7170                         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7171                         if (sin == NULL)
7172                                 return (ENOMEM);
7173                         sin->sin_family = AF_INET;
7174                         sin->sin_len = sizeof(*sin);
7175                         sin->sin_port = store.sin.sin_port;
7176                         sin->sin_addr = store.sin.sin_addr;
7177                         *addr = (struct sockaddr *)sin;
7178                         break;
7179                 }
7180 #endif
7181 #ifdef INET6
7182         case AF_INET6:
7183                 {
7184                         struct sockaddr_in6 *sin6;
7185
7186                         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7187                         if (sin6 == NULL)
7188                                 return (ENOMEM);
7189                         sin6->sin6_family = AF_INET6;
7190                         sin6->sin6_len = sizeof(*sin6);
7191                         sin6->sin6_port = store.sin6.sin6_port;
7192                         sin6->sin6_addr = store.sin6.sin6_addr;
7193                         if ((error = sa6_recoverscope(sin6)) != 0) {
7194                                 SCTP_FREE_SONAME(sin6);
7195                                 return (error);
7196                         }
7197                         *addr = (struct sockaddr *)sin6;
7198                         break;
7199                 }
7200 #endif
7201         default:
7202                 /* TSNH */
7203                 break;
7204         }
7205         /* Wake any delayed sleep action */
7206         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7207                 SCTP_INP_WLOCK(inp);
7208                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7209                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7210                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7211                         SCTP_INP_WUNLOCK(inp);
7212                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7213                         if (sowriteable(inp->sctp_socket)) {
7214                                 sowwakeup_locked(inp->sctp_socket);
7215                         } else {
7216                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7217                         }
7218                         SCTP_INP_WLOCK(inp);
7219                 }
7220                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7221                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7222                         SCTP_INP_WUNLOCK(inp);
7223                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7224                         if (soreadable(inp->sctp_socket)) {
7225                                 sctp_defered_wakeup_cnt++;
7226                                 sorwakeup_locked(inp->sctp_socket);
7227                         } else {
7228                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7229                         }
7230                         SCTP_INP_WLOCK(inp);
7231                 }
7232                 SCTP_INP_WUNLOCK(inp);
7233         }
7234         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7235                 SCTP_TCB_LOCK(stcb);
7236                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7237                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
7238         }
7239         return (0);
7240 }
7241
7242 #ifdef INET
7243 int
7244 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7245 {
7246         struct sockaddr_in *sin;
7247         uint32_t vrf_id;
7248         struct sctp_inpcb *inp;
7249         struct sctp_ifa *sctp_ifa;
7250
7251         /*
7252          * Do the malloc first in case it blocks.
7253          */
7254         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7255         if (sin == NULL)
7256                 return (ENOMEM);
7257         sin->sin_family = AF_INET;
7258         sin->sin_len = sizeof(*sin);
7259         inp = (struct sctp_inpcb *)so->so_pcb;
7260         if (!inp) {
7261                 SCTP_FREE_SONAME(sin);
7262                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7263                 return (ECONNRESET);
7264         }
7265         SCTP_INP_RLOCK(inp);
7266         sin->sin_port = inp->sctp_lport;
7267         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7268                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7269                         struct sctp_tcb *stcb;
7270                         struct sockaddr_in *sin_a;
7271                         struct sctp_nets *net;
7272                         int fnd;
7273
7274                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7275                         if (stcb == NULL) {
7276                                 goto notConn;
7277                         }
7278                         fnd = 0;
7279                         sin_a = NULL;
7280                         SCTP_TCB_LOCK(stcb);
7281                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7282                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7283                                 if (sin_a == NULL)
7284                                         /* this will make coverity happy */
7285                                         continue;
7286
7287                                 if (sin_a->sin_family == AF_INET) {
7288                                         fnd = 1;
7289                                         break;
7290                                 }
7291                         }
7292                         if ((!fnd) || (sin_a == NULL)) {
7293                                 /* punt */
7294                                 SCTP_TCB_UNLOCK(stcb);
7295                                 goto notConn;
7296                         }
7297                         vrf_id = inp->def_vrf_id;
7298                         sctp_ifa = sctp_source_address_selection(inp,
7299                             stcb,
7300                             (sctp_route_t *) & net->ro,
7301                             net, 0, vrf_id);
7302                         if (sctp_ifa) {
7303                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7304                                 sctp_free_ifa(sctp_ifa);
7305                         }
7306                         SCTP_TCB_UNLOCK(stcb);
7307                 } else {
7308                         /* For the bound all case you get back 0 */
7309         notConn:
7310                         sin->sin_addr.s_addr = 0;
7311                 }
7312
7313         } else {
7314                 /* Take the first IPv4 address in the list */
7315                 struct sctp_laddr *laddr;
7316                 int fnd = 0;
7317
7318                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7319                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
7320                                 struct sockaddr_in *sin_a;
7321
7322                                 sin_a = &laddr->ifa->address.sin;
7323                                 sin->sin_addr = sin_a->sin_addr;
7324                                 fnd = 1;
7325                                 break;
7326                         }
7327                 }
7328                 if (!fnd) {
7329                         SCTP_FREE_SONAME(sin);
7330                         SCTP_INP_RUNLOCK(inp);
7331                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7332                         return (ENOENT);
7333                 }
7334         }
7335         SCTP_INP_RUNLOCK(inp);
7336         (*addr) = (struct sockaddr *)sin;
7337         return (0);
7338 }
7339
7340 int
7341 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7342 {
7343         struct sockaddr_in *sin;
7344         int fnd;
7345         struct sockaddr_in *sin_a;
7346         struct sctp_inpcb *inp;
7347         struct sctp_tcb *stcb;
7348         struct sctp_nets *net;
7349
7350         /* Do the malloc first in case it blocks. */
7351         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7352         if (sin == NULL)
7353                 return (ENOMEM);
7354         sin->sin_family = AF_INET;
7355         sin->sin_len = sizeof(*sin);
7356
7357         inp = (struct sctp_inpcb *)so->so_pcb;
7358         if ((inp == NULL) ||
7359             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7360                 /* UDP type and listeners will drop out here */
7361                 SCTP_FREE_SONAME(sin);
7362                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7363                 return (ENOTCONN);
7364         }
7365         SCTP_INP_RLOCK(inp);
7366         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7367         if (stcb) {
7368                 SCTP_TCB_LOCK(stcb);
7369         }
7370         SCTP_INP_RUNLOCK(inp);
7371         if (stcb == NULL) {
7372                 SCTP_FREE_SONAME(sin);
7373                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7374                 return (ECONNRESET);
7375         }
7376         fnd = 0;
7377         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7378                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7379                 if (sin_a->sin_family == AF_INET) {
7380                         fnd = 1;
7381                         sin->sin_port = stcb->rport;
7382                         sin->sin_addr = sin_a->sin_addr;
7383                         break;
7384                 }
7385         }
7386         SCTP_TCB_UNLOCK(stcb);
7387         if (!fnd) {
7388                 /* No IPv4 address */
7389                 SCTP_FREE_SONAME(sin);
7390                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7391                 return (ENOENT);
7392         }
7393         (*addr) = (struct sockaddr *)sin;
7394         return (0);
7395 }
7396
7397 struct pr_usrreqs sctp_usrreqs = {
7398         .pru_abort = sctp_abort,
7399         .pru_accept = sctp_accept,
7400         .pru_attach = sctp_attach,
7401         .pru_bind = sctp_bind,
7402         .pru_connect = sctp_connect,
7403         .pru_control = in_control,
7404         .pru_close = sctp_close,
7405         .pru_detach = sctp_close,
7406         .pru_sopoll = sopoll_generic,
7407         .pru_flush = sctp_flush,
7408         .pru_disconnect = sctp_disconnect,
7409         .pru_listen = sctp_listen,
7410         .pru_peeraddr = sctp_peeraddr,
7411         .pru_send = sctp_sendm,
7412         .pru_shutdown = sctp_shutdown,
7413         .pru_sockaddr = sctp_ingetaddr,
7414         .pru_sosend = sctp_sosend,
7415         .pru_soreceive = sctp_soreceive
7416 };
7417
7418 #endif