]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/netinet/sctp_usrreq.c
MFC r293828:
[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             inp->sctp_ep.pre_open_stream_count,
1505             (struct thread *)p
1506             );
1507         if (stcb == NULL) {
1508                 /* Gak! no memory */
1509                 goto out_now;
1510         }
1511         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1512                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1513                 /* Set the connected flag so we can queue data */
1514                 soisconnecting(so);
1515         }
1516         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
1517         /* move to second address */
1518         switch (sa->sa_family) {
1519 #ifdef INET
1520         case AF_INET:
1521                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1522                 break;
1523 #endif
1524 #ifdef INET6
1525         case AF_INET6:
1526                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1527                 break;
1528 #endif
1529         default:
1530                 break;
1531         }
1532
1533         error = 0;
1534         sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1535         /* Fill in the return id */
1536         if (error) {
1537                 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
1538                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1539                 goto out_now;
1540         }
1541         a_id = (sctp_assoc_t *) optval;
1542         *a_id = sctp_get_associd(stcb);
1543
1544         /* initialize authentication parameters for the assoc */
1545         sctp_initialize_auth_params(inp, stcb);
1546
1547         if (delay) {
1548                 /* doing delayed connection */
1549                 stcb->asoc.delayed_connection = 1;
1550                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1551         } else {
1552                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1553                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1554         }
1555         SCTP_TCB_UNLOCK(stcb);
1556         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1557                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1558                 /* Set the connected flag so we can queue data */
1559                 soisconnecting(so);
1560         }
1561 out_now:
1562         if (creat_lock_on) {
1563                 SCTP_ASOC_CREATE_UNLOCK(inp);
1564         }
1565         SCTP_INP_DECR_REF(inp);
1566         return (error);
1567 }
1568
1569 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1570         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1571             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1572                 SCTP_INP_RLOCK(inp); \
1573                 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1574                 if (stcb) { \
1575                         SCTP_TCB_LOCK(stcb); \
1576                 } \
1577                 SCTP_INP_RUNLOCK(inp); \
1578         } else if (assoc_id > SCTP_ALL_ASSOC) { \
1579                 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1580                 if (stcb == NULL) { \
1581                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1582                         error = ENOENT; \
1583                         break; \
1584                 } \
1585         } else { \
1586                 stcb = NULL; \
1587         } \
1588   }
1589
1590
1591 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1592         if (size < sizeof(type)) { \
1593                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1594                 error = EINVAL; \
1595                 break; \
1596         } else { \
1597                 destp = (type *)srcp; \
1598         } \
1599       }
1600
1601 static int
1602 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1603     void *p)
1604 {
1605         struct sctp_inpcb *inp = NULL;
1606         int error, val = 0;
1607         struct sctp_tcb *stcb = NULL;
1608
1609         if (optval == NULL) {
1610                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1611                 return (EINVAL);
1612         }
1613         inp = (struct sctp_inpcb *)so->so_pcb;
1614         if (inp == NULL) {
1615                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1616                 return EINVAL;
1617         }
1618         error = 0;
1619
1620         switch (optname) {
1621         case SCTP_NODELAY:
1622         case SCTP_AUTOCLOSE:
1623         case SCTP_EXPLICIT_EOR:
1624         case SCTP_AUTO_ASCONF:
1625         case SCTP_DISABLE_FRAGMENTS:
1626         case SCTP_I_WANT_MAPPED_V4_ADDR:
1627         case SCTP_USE_EXT_RCVINFO:
1628                 SCTP_INP_RLOCK(inp);
1629                 switch (optname) {
1630                 case SCTP_DISABLE_FRAGMENTS:
1631                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1632                         break;
1633                 case SCTP_I_WANT_MAPPED_V4_ADDR:
1634                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1635                         break;
1636                 case SCTP_AUTO_ASCONF:
1637                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1638                                 /* only valid for bound all sockets */
1639                                 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1640                         } else {
1641                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1642                                 error = EINVAL;
1643                                 goto flags_out;
1644                         }
1645                         break;
1646                 case SCTP_EXPLICIT_EOR:
1647                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1648                         break;
1649                 case SCTP_NODELAY:
1650                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1651                         break;
1652                 case SCTP_USE_EXT_RCVINFO:
1653                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1654                         break;
1655                 case SCTP_AUTOCLOSE:
1656                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1657                                 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1658                         else
1659                                 val = 0;
1660                         break;
1661
1662                 default:
1663                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1664                         error = ENOPROTOOPT;
1665                 }               /* end switch (sopt->sopt_name) */
1666                 if (*optsize < sizeof(val)) {
1667                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1668                         error = EINVAL;
1669                 }
1670 flags_out:
1671                 SCTP_INP_RUNLOCK(inp);
1672                 if (error == 0) {
1673                         /* return the option value */
1674                         *(int *)optval = val;
1675                         *optsize = sizeof(val);
1676                 }
1677                 break;
1678         case SCTP_GET_PACKET_LOG:
1679                 {
1680 #ifdef  SCTP_PACKET_LOGGING
1681                         uint8_t *target;
1682                         int ret;
1683
1684                         SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1685                         ret = sctp_copy_out_packet_log(target, (int)*optsize);
1686                         *optsize = ret;
1687 #else
1688                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1689                         error = EOPNOTSUPP;
1690 #endif
1691                         break;
1692                 }
1693         case SCTP_REUSE_PORT:
1694                 {
1695                         uint32_t *value;
1696
1697                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1698                                 /* Can't do this for a 1-m socket */
1699                                 error = EINVAL;
1700                                 break;
1701                         }
1702                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1703                         *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1704                         *optsize = sizeof(uint32_t);
1705                         break;
1706                 }
1707         case SCTP_PARTIAL_DELIVERY_POINT:
1708                 {
1709                         uint32_t *value;
1710
1711                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1712                         *value = inp->partial_delivery_point;
1713                         *optsize = sizeof(uint32_t);
1714                         break;
1715                 }
1716         case SCTP_FRAGMENT_INTERLEAVE:
1717                 {
1718                         uint32_t *value;
1719
1720                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1721                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1722                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1723                                         *value = SCTP_FRAG_LEVEL_2;
1724                                 } else {
1725                                         *value = SCTP_FRAG_LEVEL_1;
1726                                 }
1727                         } else {
1728                                 *value = SCTP_FRAG_LEVEL_0;
1729                         }
1730                         *optsize = sizeof(uint32_t);
1731                         break;
1732                 }
1733         case SCTP_CMT_ON_OFF:
1734                 {
1735                         struct sctp_assoc_value *av;
1736
1737                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1738                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1739                         if (stcb) {
1740                                 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1741                                 SCTP_TCB_UNLOCK(stcb);
1742                         } else {
1743                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1744                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1745                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1746                                         SCTP_INP_RLOCK(inp);
1747                                         av->assoc_value = inp->sctp_cmt_on_off;
1748                                         SCTP_INP_RUNLOCK(inp);
1749                                 } else {
1750                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1751                                         error = EINVAL;
1752                                 }
1753                         }
1754                         if (error == 0) {
1755                                 *optsize = sizeof(struct sctp_assoc_value);
1756                         }
1757                         break;
1758                 }
1759         case SCTP_PLUGGABLE_CC:
1760                 {
1761                         struct sctp_assoc_value *av;
1762
1763                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1764                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1765                         if (stcb) {
1766                                 av->assoc_value = stcb->asoc.congestion_control_module;
1767                                 SCTP_TCB_UNLOCK(stcb);
1768                         } else {
1769                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1770                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1771                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1772                                         SCTP_INP_RLOCK(inp);
1773                                         av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1774                                         SCTP_INP_RUNLOCK(inp);
1775                                 } else {
1776                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1777                                         error = EINVAL;
1778                                 }
1779                         }
1780                         if (error == 0) {
1781                                 *optsize = sizeof(struct sctp_assoc_value);
1782                         }
1783                         break;
1784                 }
1785         case SCTP_CC_OPTION:
1786                 {
1787                         struct sctp_cc_option *cc_opt;
1788
1789                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1790                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1791                         if (stcb == NULL) {
1792                                 error = EINVAL;
1793                         } else {
1794                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1795                                         error = ENOTSUP;
1796                                 } else {
1797                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt);
1798                                         *optsize = sizeof(struct sctp_cc_option);
1799                                 }
1800                                 SCTP_TCB_UNLOCK(stcb);
1801                         }
1802                         break;
1803                 }
1804         case SCTP_PLUGGABLE_SS:
1805                 {
1806                         struct sctp_assoc_value *av;
1807
1808                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1809                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1810                         if (stcb) {
1811                                 av->assoc_value = stcb->asoc.stream_scheduling_module;
1812                                 SCTP_TCB_UNLOCK(stcb);
1813                         } else {
1814                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1815                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1816                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1817                                         SCTP_INP_RLOCK(inp);
1818                                         av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1819                                         SCTP_INP_RUNLOCK(inp);
1820                                 } else {
1821                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1822                                         error = EINVAL;
1823                                 }
1824                         }
1825                         if (error == 0) {
1826                                 *optsize = sizeof(struct sctp_assoc_value);
1827                         }
1828                         break;
1829                 }
1830         case SCTP_SS_VALUE:
1831                 {
1832                         struct sctp_stream_value *av;
1833
1834                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1835                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1836                         if (stcb) {
1837                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
1838                                     (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1839                                     &av->stream_value) < 0)) {
1840                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1841                                         error = EINVAL;
1842                                 } else {
1843                                         *optsize = sizeof(struct sctp_stream_value);
1844                                 }
1845                                 SCTP_TCB_UNLOCK(stcb);
1846                         } else {
1847                                 /*
1848                                  * Can't get stream value without
1849                                  * association
1850                                  */
1851                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1852                                 error = EINVAL;
1853                         }
1854                         break;
1855                 }
1856         case SCTP_GET_ADDR_LEN:
1857                 {
1858                         struct sctp_assoc_value *av;
1859
1860                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1861                         error = EINVAL;
1862 #ifdef INET
1863                         if (av->assoc_value == AF_INET) {
1864                                 av->assoc_value = sizeof(struct sockaddr_in);
1865                                 error = 0;
1866                         }
1867 #endif
1868 #ifdef INET6
1869                         if (av->assoc_value == AF_INET6) {
1870                                 av->assoc_value = sizeof(struct sockaddr_in6);
1871                                 error = 0;
1872                         }
1873 #endif
1874                         if (error) {
1875                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1876                         } else {
1877                                 *optsize = sizeof(struct sctp_assoc_value);
1878                         }
1879                         break;
1880                 }
1881         case SCTP_GET_ASSOC_NUMBER:
1882                 {
1883                         uint32_t *value, cnt;
1884
1885                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1886                         cnt = 0;
1887                         SCTP_INP_RLOCK(inp);
1888                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1889                                 cnt++;
1890                         }
1891                         SCTP_INP_RUNLOCK(inp);
1892                         *value = cnt;
1893                         *optsize = sizeof(uint32_t);
1894                         break;
1895                 }
1896         case SCTP_GET_ASSOC_ID_LIST:
1897                 {
1898                         struct sctp_assoc_ids *ids;
1899                         unsigned int at, limit;
1900
1901                         SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1902                         at = 0;
1903                         limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1904                         SCTP_INP_RLOCK(inp);
1905                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1906                                 if (at < limit) {
1907                                         ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1908                                 } else {
1909                                         error = EINVAL;
1910                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1911                                         break;
1912                                 }
1913                         }
1914                         SCTP_INP_RUNLOCK(inp);
1915                         if (error == 0) {
1916                                 ids->gaids_number_of_ids = at;
1917                                 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1918                         }
1919                         break;
1920                 }
1921         case SCTP_CONTEXT:
1922                 {
1923                         struct sctp_assoc_value *av;
1924
1925                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1926                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1927
1928                         if (stcb) {
1929                                 av->assoc_value = stcb->asoc.context;
1930                                 SCTP_TCB_UNLOCK(stcb);
1931                         } else {
1932                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1933                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1934                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1935                                         SCTP_INP_RLOCK(inp);
1936                                         av->assoc_value = inp->sctp_context;
1937                                         SCTP_INP_RUNLOCK(inp);
1938                                 } else {
1939                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1940                                         error = EINVAL;
1941                                 }
1942                         }
1943                         if (error == 0) {
1944                                 *optsize = sizeof(struct sctp_assoc_value);
1945                         }
1946                         break;
1947                 }
1948         case SCTP_VRF_ID:
1949                 {
1950                         uint32_t *default_vrfid;
1951
1952                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1953                         *default_vrfid = inp->def_vrf_id;
1954                         *optsize = sizeof(uint32_t);
1955                         break;
1956                 }
1957         case SCTP_GET_ASOC_VRF:
1958                 {
1959                         struct sctp_assoc_value *id;
1960
1961                         SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1962                         SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1963                         if (stcb == NULL) {
1964                                 error = EINVAL;
1965                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1966                         } else {
1967                                 id->assoc_value = stcb->asoc.vrf_id;
1968                                 *optsize = sizeof(struct sctp_assoc_value);
1969                         }
1970                         break;
1971                 }
1972         case SCTP_GET_VRF_IDS:
1973                 {
1974                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1975                         error = EOPNOTSUPP;
1976                         break;
1977                 }
1978         case SCTP_GET_NONCE_VALUES:
1979                 {
1980                         struct sctp_get_nonce_values *gnv;
1981
1982                         SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1983                         SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1984
1985                         if (stcb) {
1986                                 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1987                                 gnv->gn_local_tag = stcb->asoc.my_vtag;
1988                                 SCTP_TCB_UNLOCK(stcb);
1989                                 *optsize = sizeof(struct sctp_get_nonce_values);
1990                         } else {
1991                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1992                                 error = ENOTCONN;
1993                         }
1994                         break;
1995                 }
1996         case SCTP_DELAYED_SACK:
1997                 {
1998                         struct sctp_sack_info *sack;
1999
2000                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
2001                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2002                         if (stcb) {
2003                                 sack->sack_delay = stcb->asoc.delayed_ack;
2004                                 sack->sack_freq = stcb->asoc.sack_freq;
2005                                 SCTP_TCB_UNLOCK(stcb);
2006                         } else {
2007                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2008                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2009                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
2010                                         SCTP_INP_RLOCK(inp);
2011                                         sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2012                                         sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2013                                         SCTP_INP_RUNLOCK(inp);
2014                                 } else {
2015                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2016                                         error = EINVAL;
2017                                 }
2018                         }
2019                         if (error == 0) {
2020                                 *optsize = sizeof(struct sctp_sack_info);
2021                         }
2022                         break;
2023                 }
2024         case SCTP_GET_SNDBUF_USE:
2025                 {
2026                         struct sctp_sockstat *ss;
2027
2028                         SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2029                         SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2030
2031                         if (stcb) {
2032                                 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2033                                 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2034                                     stcb->asoc.size_on_all_streams);
2035                                 SCTP_TCB_UNLOCK(stcb);
2036                                 *optsize = sizeof(struct sctp_sockstat);
2037                         } else {
2038                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2039                                 error = ENOTCONN;
2040                         }
2041                         break;
2042                 }
2043         case SCTP_MAX_BURST:
2044                 {
2045                         struct sctp_assoc_value *av;
2046
2047                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2048                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2049
2050                         if (stcb) {
2051                                 av->assoc_value = stcb->asoc.max_burst;
2052                                 SCTP_TCB_UNLOCK(stcb);
2053                         } else {
2054                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2055                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2056                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2057                                         SCTP_INP_RLOCK(inp);
2058                                         av->assoc_value = inp->sctp_ep.max_burst;
2059                                         SCTP_INP_RUNLOCK(inp);
2060                                 } else {
2061                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2062                                         error = EINVAL;
2063                                 }
2064                         }
2065                         if (error == 0) {
2066                                 *optsize = sizeof(struct sctp_assoc_value);
2067                         }
2068                         break;
2069                 }
2070         case SCTP_MAXSEG:
2071                 {
2072                         struct sctp_assoc_value *av;
2073                         int ovh;
2074
2075                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2076                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2077
2078                         if (stcb) {
2079                                 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2080                                 SCTP_TCB_UNLOCK(stcb);
2081                         } else {
2082                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2083                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2084                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2085                                         SCTP_INP_RLOCK(inp);
2086                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2087                                                 ovh = SCTP_MED_OVERHEAD;
2088                                         } else {
2089                                                 ovh = SCTP_MED_V4_OVERHEAD;
2090                                         }
2091                                         if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2092                                                 av->assoc_value = 0;
2093                                         else
2094                                                 av->assoc_value = inp->sctp_frag_point - ovh;
2095                                         SCTP_INP_RUNLOCK(inp);
2096                                 } else {
2097                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2098                                         error = EINVAL;
2099                                 }
2100                         }
2101                         if (error == 0) {
2102                                 *optsize = sizeof(struct sctp_assoc_value);
2103                         }
2104                         break;
2105                 }
2106         case SCTP_GET_STAT_LOG:
2107                 error = sctp_fill_stat_log(optval, optsize);
2108                 break;
2109         case SCTP_EVENTS:
2110                 {
2111                         struct sctp_event_subscribe *events;
2112
2113                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2114                         memset(events, 0, sizeof(struct sctp_event_subscribe));
2115                         SCTP_INP_RLOCK(inp);
2116                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2117                                 events->sctp_data_io_event = 1;
2118
2119                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2120                                 events->sctp_association_event = 1;
2121
2122                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2123                                 events->sctp_address_event = 1;
2124
2125                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2126                                 events->sctp_send_failure_event = 1;
2127
2128                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2129                                 events->sctp_peer_error_event = 1;
2130
2131                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2132                                 events->sctp_shutdown_event = 1;
2133
2134                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2135                                 events->sctp_partial_delivery_event = 1;
2136
2137                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2138                                 events->sctp_adaptation_layer_event = 1;
2139
2140                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2141                                 events->sctp_authentication_event = 1;
2142
2143                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2144                                 events->sctp_sender_dry_event = 1;
2145
2146                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2147                                 events->sctp_stream_reset_event = 1;
2148                         SCTP_INP_RUNLOCK(inp);
2149                         *optsize = sizeof(struct sctp_event_subscribe);
2150                         break;
2151                 }
2152         case SCTP_ADAPTATION_LAYER:
2153                 {
2154                         uint32_t *value;
2155
2156                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2157
2158                         SCTP_INP_RLOCK(inp);
2159                         *value = inp->sctp_ep.adaptation_layer_indicator;
2160                         SCTP_INP_RUNLOCK(inp);
2161                         *optsize = sizeof(uint32_t);
2162                         break;
2163                 }
2164         case SCTP_SET_INITIAL_DBG_SEQ:
2165                 {
2166                         uint32_t *value;
2167
2168                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2169                         SCTP_INP_RLOCK(inp);
2170                         *value = inp->sctp_ep.initial_sequence_debug;
2171                         SCTP_INP_RUNLOCK(inp);
2172                         *optsize = sizeof(uint32_t);
2173                         break;
2174                 }
2175         case SCTP_GET_LOCAL_ADDR_SIZE:
2176                 {
2177                         uint32_t *value;
2178
2179                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2180                         SCTP_INP_RLOCK(inp);
2181                         *value = sctp_count_max_addresses(inp);
2182                         SCTP_INP_RUNLOCK(inp);
2183                         *optsize = sizeof(uint32_t);
2184                         break;
2185                 }
2186         case SCTP_GET_REMOTE_ADDR_SIZE:
2187                 {
2188                         uint32_t *value;
2189                         size_t size;
2190                         struct sctp_nets *net;
2191
2192                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2193                         /* FIXME MT: change to sctp_assoc_value? */
2194                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2195
2196                         if (stcb) {
2197                                 size = 0;
2198                                 /* Count the sizes */
2199                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2200                                         switch (net->ro._l_addr.sa.sa_family) {
2201 #ifdef INET
2202                                         case AF_INET:
2203 #ifdef INET6
2204                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2205                                                         size += sizeof(struct sockaddr_in6);
2206                                                 } else {
2207                                                         size += sizeof(struct sockaddr_in);
2208                                                 }
2209 #else
2210                                                 size += sizeof(struct sockaddr_in);
2211 #endif
2212                                                 break;
2213 #endif
2214 #ifdef INET6
2215                                         case AF_INET6:
2216                                                 size += sizeof(struct sockaddr_in6);
2217                                                 break;
2218 #endif
2219                                         default:
2220                                                 break;
2221                                         }
2222                                 }
2223                                 SCTP_TCB_UNLOCK(stcb);
2224                                 *value = (uint32_t) size;
2225                                 *optsize = sizeof(uint32_t);
2226                         } else {
2227                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2228                                 error = ENOTCONN;
2229                         }
2230                         break;
2231                 }
2232         case SCTP_GET_PEER_ADDRESSES:
2233                 /*
2234                  * Get the address information, an array is passed in to
2235                  * fill up we pack it.
2236                  */
2237                 {
2238                         size_t cpsz, left;
2239                         struct sockaddr_storage *sas;
2240                         struct sctp_nets *net;
2241                         struct sctp_getaddresses *saddr;
2242
2243                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2244                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2245
2246                         if (stcb) {
2247                                 left = (*optsize) - sizeof(struct sctp_getaddresses);
2248                                 *optsize = sizeof(struct sctp_getaddresses);
2249                                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2250
2251                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2252                                         switch (net->ro._l_addr.sa.sa_family) {
2253 #ifdef INET
2254                                         case AF_INET:
2255 #ifdef INET6
2256                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2257                                                         cpsz = sizeof(struct sockaddr_in6);
2258                                                 } else {
2259                                                         cpsz = sizeof(struct sockaddr_in);
2260                                                 }
2261 #else
2262                                                 cpsz = sizeof(struct sockaddr_in);
2263 #endif
2264                                                 break;
2265 #endif
2266 #ifdef INET6
2267                                         case AF_INET6:
2268                                                 cpsz = sizeof(struct sockaddr_in6);
2269                                                 break;
2270 #endif
2271                                         default:
2272                                                 cpsz = 0;
2273                                                 break;
2274                                         }
2275                                         if (cpsz == 0) {
2276                                                 break;
2277                                         }
2278                                         if (left < cpsz) {
2279                                                 /* not enough room. */
2280                                                 break;
2281                                         }
2282 #if defined(INET) && defined(INET6)
2283                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2284                                             (net->ro._l_addr.sa.sa_family == AF_INET)) {
2285                                                 /* Must map the address */
2286                                                 in6_sin_2_v4mapsin6(&net->ro._l_addr.sin,
2287                                                     (struct sockaddr_in6 *)sas);
2288                                         } else {
2289                                                 memcpy(sas, &net->ro._l_addr, cpsz);
2290                                         }
2291 #else
2292                                         memcpy(sas, &net->ro._l_addr, cpsz);
2293 #endif
2294                                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2295
2296                                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2297                                         left -= cpsz;
2298                                         *optsize += cpsz;
2299                                 }
2300                                 SCTP_TCB_UNLOCK(stcb);
2301                         } else {
2302                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2303                                 error = ENOENT;
2304                         }
2305                         break;
2306                 }
2307         case SCTP_GET_LOCAL_ADDRESSES:
2308                 {
2309                         size_t limit, actual;
2310                         struct sockaddr_storage *sas;
2311                         struct sctp_getaddresses *saddr;
2312
2313                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2314                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2315
2316                         sas = (struct sockaddr_storage *)&saddr->addr[0];
2317                         limit = *optsize - sizeof(sctp_assoc_t);
2318                         actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2319                         if (stcb) {
2320                                 SCTP_TCB_UNLOCK(stcb);
2321                         }
2322                         *optsize = sizeof(struct sockaddr_storage) + actual;
2323                         break;
2324                 }
2325         case SCTP_PEER_ADDR_PARAMS:
2326                 {
2327                         struct sctp_paddrparams *paddrp;
2328                         struct sctp_nets *net;
2329                         struct sockaddr *addr;
2330
2331 #if defined(INET) && defined(INET6)
2332                         struct sockaddr_in sin_store;
2333
2334 #endif
2335
2336                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2337                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2338
2339 #if defined(INET) && defined(INET6)
2340                         if (paddrp->spp_address.ss_family == AF_INET6) {
2341                                 struct sockaddr_in6 *sin6;
2342
2343                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
2344                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2345                                         in6_sin6_2_sin(&sin_store, sin6);
2346                                         addr = (struct sockaddr *)&sin_store;
2347                                 } else {
2348                                         addr = (struct sockaddr *)&paddrp->spp_address;
2349                                 }
2350                         } else {
2351                                 addr = (struct sockaddr *)&paddrp->spp_address;
2352                         }
2353 #else
2354                         addr = (struct sockaddr *)&paddrp->spp_address;
2355 #endif
2356                         if (stcb != NULL) {
2357                                 net = sctp_findnet(stcb, addr);
2358                         } else {
2359                                 /*
2360                                  * We increment here since
2361                                  * sctp_findassociation_ep_addr() wil do a
2362                                  * decrement if it finds the stcb as long as
2363                                  * the locked tcb (last argument) is NOT a
2364                                  * TCB.. aka NULL.
2365                                  */
2366                                 net = NULL;
2367                                 SCTP_INP_INCR_REF(inp);
2368                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2369                                 if (stcb == NULL) {
2370                                         SCTP_INP_DECR_REF(inp);
2371                                 }
2372                         }
2373                         if ((stcb != NULL) && (net == NULL)) {
2374 #ifdef INET
2375                                 if (addr->sa_family == AF_INET) {
2376                                         struct sockaddr_in *sin;
2377
2378                                         sin = (struct sockaddr_in *)addr;
2379                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
2380                                                 error = EINVAL;
2381                                                 SCTP_TCB_UNLOCK(stcb);
2382                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2383                                                 break;
2384                                         }
2385                                 } else
2386 #endif
2387 #ifdef INET6
2388                                 if (addr->sa_family == AF_INET6) {
2389                                         struct sockaddr_in6 *sin6;
2390
2391                                         sin6 = (struct sockaddr_in6 *)addr;
2392                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2393                                                 error = EINVAL;
2394                                                 SCTP_TCB_UNLOCK(stcb);
2395                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2396                                                 break;
2397                                         }
2398                                 } else
2399 #endif
2400                                 {
2401                                         error = EAFNOSUPPORT;
2402                                         SCTP_TCB_UNLOCK(stcb);
2403                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2404                                         break;
2405                                 }
2406                         }
2407                         if (stcb != NULL) {
2408                                 /* Applies to the specific association */
2409                                 paddrp->spp_flags = 0;
2410                                 if (net != NULL) {
2411                                         paddrp->spp_hbinterval = net->heart_beat_delay;
2412                                         paddrp->spp_pathmaxrxt = net->failure_threshold;
2413                                         paddrp->spp_pathmtu = net->mtu;
2414                                         switch (net->ro._l_addr.sa.sa_family) {
2415 #ifdef INET
2416                                         case AF_INET:
2417                                                 paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2418                                                 break;
2419 #endif
2420 #ifdef INET6
2421                                         case AF_INET6:
2422                                                 paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2423                                                 break;
2424 #endif
2425                                         default:
2426                                                 break;
2427                                         }
2428                                         /* get flags for HB */
2429                                         if (net->dest_state & SCTP_ADDR_NOHB) {
2430                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2431                                         } else {
2432                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2433                                         }
2434                                         /* get flags for PMTU */
2435                                         if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2436                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2437                                         } else {
2438                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2439                                         }
2440                                         if (net->dscp & 0x01) {
2441                                                 paddrp->spp_dscp = net->dscp & 0xfc;
2442                                                 paddrp->spp_flags |= SPP_DSCP;
2443                                         }
2444 #ifdef INET6
2445                                         if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2446                                             (net->flowlabel & 0x80000000)) {
2447                                                 paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2448                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2449                                         }
2450 #endif
2451                                 } else {
2452                                         /*
2453                                          * No destination so return default
2454                                          * value
2455                                          */
2456                                         paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2457                                         paddrp->spp_pathmtu = 0;
2458                                         if (stcb->asoc.default_dscp & 0x01) {
2459                                                 paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2460                                                 paddrp->spp_flags |= SPP_DSCP;
2461                                         }
2462 #ifdef INET6
2463                                         if (stcb->asoc.default_flowlabel & 0x80000000) {
2464                                                 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2465                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2466                                         }
2467 #endif
2468                                         /* default settings should be these */
2469                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2470                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2471                                         } else {
2472                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2473                                         }
2474                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2475                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2476                                         } else {
2477                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2478                                         }
2479                                         paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2480                                 }
2481                                 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2482                                 SCTP_TCB_UNLOCK(stcb);
2483                         } else {
2484                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2485                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2486                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2487                                         /* Use endpoint defaults */
2488                                         SCTP_INP_RLOCK(inp);
2489                                         paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2490                                         paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2491                                         paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2492                                         /* get inp's default */
2493                                         if (inp->sctp_ep.default_dscp & 0x01) {
2494                                                 paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2495                                                 paddrp->spp_flags |= SPP_DSCP;
2496                                         }
2497 #ifdef INET6
2498                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2499                                             (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2500                                                 paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2501                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2502                                         }
2503 #endif
2504                                         /* can't return this */
2505                                         paddrp->spp_pathmtu = 0;
2506
2507                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2508                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2509                                         } else {
2510                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2511                                         }
2512                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2513                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2514                                         } else {
2515                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2516                                         }
2517                                         SCTP_INP_RUNLOCK(inp);
2518                                 } else {
2519                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2520                                         error = EINVAL;
2521                                 }
2522                         }
2523                         if (error == 0) {
2524                                 *optsize = sizeof(struct sctp_paddrparams);
2525                         }
2526                         break;
2527                 }
2528         case SCTP_GET_PEER_ADDR_INFO:
2529                 {
2530                         struct sctp_paddrinfo *paddri;
2531                         struct sctp_nets *net;
2532                         struct sockaddr *addr;
2533
2534 #if defined(INET) && defined(INET6)
2535                         struct sockaddr_in sin_store;
2536
2537 #endif
2538
2539                         SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2540                         SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2541
2542 #if defined(INET) && defined(INET6)
2543                         if (paddri->spinfo_address.ss_family == AF_INET6) {
2544                                 struct sockaddr_in6 *sin6;
2545
2546                                 sin6 = (struct sockaddr_in6 *)&paddri->spinfo_address;
2547                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2548                                         in6_sin6_2_sin(&sin_store, sin6);
2549                                         addr = (struct sockaddr *)&sin_store;
2550                                 } else {
2551                                         addr = (struct sockaddr *)&paddri->spinfo_address;
2552                                 }
2553                         } else {
2554                                 addr = (struct sockaddr *)&paddri->spinfo_address;
2555                         }
2556 #else
2557                         addr = (struct sockaddr *)&paddri->spinfo_address;
2558 #endif
2559                         if (stcb != NULL) {
2560                                 net = sctp_findnet(stcb, addr);
2561                         } else {
2562                                 /*
2563                                  * We increment here since
2564                                  * sctp_findassociation_ep_addr() wil do a
2565                                  * decrement if it finds the stcb as long as
2566                                  * the locked tcb (last argument) is NOT a
2567                                  * TCB.. aka NULL.
2568                                  */
2569                                 net = NULL;
2570                                 SCTP_INP_INCR_REF(inp);
2571                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2572                                 if (stcb == NULL) {
2573                                         SCTP_INP_DECR_REF(inp);
2574                                 }
2575                         }
2576
2577                         if ((stcb != NULL) && (net != NULL)) {
2578                                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2579                                         /* It's unconfirmed */
2580                                         paddri->spinfo_state = SCTP_UNCONFIRMED;
2581                                 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2582                                         /* It's active */
2583                                         paddri->spinfo_state = SCTP_ACTIVE;
2584                                 } else {
2585                                         /* It's inactive */
2586                                         paddri->spinfo_state = SCTP_INACTIVE;
2587                                 }
2588                                 paddri->spinfo_cwnd = net->cwnd;
2589                                 paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2590                                 paddri->spinfo_rto = net->RTO;
2591                                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2592                                 paddri->spinfo_mtu = net->mtu;
2593                                 switch (addr->sa_family) {
2594 #if defined(INET)
2595                                 case AF_INET:
2596                                         paddri->spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2597                                         break;
2598 #endif
2599 #if defined(INET6)
2600                                 case AF_INET6:
2601                                         paddri->spinfo_mtu -= SCTP_MIN_OVERHEAD;
2602                                         break;
2603 #endif
2604                                 default:
2605                                         break;
2606                                 }
2607                                 SCTP_TCB_UNLOCK(stcb);
2608                                 *optsize = sizeof(struct sctp_paddrinfo);
2609                         } else {
2610                                 if (stcb != NULL) {
2611                                         SCTP_TCB_UNLOCK(stcb);
2612                                 }
2613                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2614                                 error = ENOENT;
2615                         }
2616                         break;
2617                 }
2618         case SCTP_PCB_STATUS:
2619                 {
2620                         struct sctp_pcbinfo *spcb;
2621
2622                         SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2623                         sctp_fill_pcbinfo(spcb);
2624                         *optsize = sizeof(struct sctp_pcbinfo);
2625                         break;
2626                 }
2627         case SCTP_STATUS:
2628                 {
2629                         struct sctp_nets *net;
2630                         struct sctp_status *sstat;
2631
2632                         SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2633                         SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2634
2635                         if (stcb == NULL) {
2636                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2637                                 error = EINVAL;
2638                                 break;
2639                         }
2640                         sstat->sstat_state = sctp_map_assoc_state(stcb->asoc.state);
2641                         sstat->sstat_assoc_id = sctp_get_associd(stcb);
2642                         sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2643                         sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2644                         /*
2645                          * We can't include chunks that have been passed to
2646                          * the socket layer. Only things in queue.
2647                          */
2648                         sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2649                             stcb->asoc.cnt_on_all_streams);
2650
2651
2652                         sstat->sstat_instrms = stcb->asoc.streamincnt;
2653                         sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2654                         sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2655                         memcpy(&sstat->sstat_primary.spinfo_address,
2656                             &stcb->asoc.primary_destination->ro._l_addr,
2657                             ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2658                         net = stcb->asoc.primary_destination;
2659                         ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2660                         /*
2661                          * Again the user can get info from sctp_constants.h
2662                          * for what the state of the network is.
2663                          */
2664                         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2665                                 /* It's unconfirmed */
2666                                 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2667                         } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2668                                 /* It's active */
2669                                 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2670                         } else {
2671                                 /* It's inactive */
2672                                 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2673                         }
2674                         sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2675                         sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2676                         sstat->sstat_primary.spinfo_rto = net->RTO;
2677                         sstat->sstat_primary.spinfo_mtu = net->mtu;
2678                         switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
2679 #if defined(INET)
2680                         case AF_INET:
2681                                 sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2682                                 break;
2683 #endif
2684 #if defined(INET6)
2685                         case AF_INET6:
2686                                 sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_OVERHEAD;
2687                                 break;
2688 #endif
2689                         default:
2690                                 break;
2691                         }
2692                         sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2693                         SCTP_TCB_UNLOCK(stcb);
2694                         *optsize = sizeof(struct sctp_status);
2695                         break;
2696                 }
2697         case SCTP_RTOINFO:
2698                 {
2699                         struct sctp_rtoinfo *srto;
2700
2701                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2702                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2703
2704                         if (stcb) {
2705                                 srto->srto_initial = stcb->asoc.initial_rto;
2706                                 srto->srto_max = stcb->asoc.maxrto;
2707                                 srto->srto_min = stcb->asoc.minrto;
2708                                 SCTP_TCB_UNLOCK(stcb);
2709                         } else {
2710                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2711                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2712                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
2713                                         SCTP_INP_RLOCK(inp);
2714                                         srto->srto_initial = inp->sctp_ep.initial_rto;
2715                                         srto->srto_max = inp->sctp_ep.sctp_maxrto;
2716                                         srto->srto_min = inp->sctp_ep.sctp_minrto;
2717                                         SCTP_INP_RUNLOCK(inp);
2718                                 } else {
2719                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2720                                         error = EINVAL;
2721                                 }
2722                         }
2723                         if (error == 0) {
2724                                 *optsize = sizeof(struct sctp_rtoinfo);
2725                         }
2726                         break;
2727                 }
2728         case SCTP_TIMEOUTS:
2729                 {
2730                         struct sctp_timeouts *stimo;
2731
2732                         SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2733                         SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2734
2735                         if (stcb) {
2736                                 stimo->stimo_init = stcb->asoc.timoinit;
2737                                 stimo->stimo_data = stcb->asoc.timodata;
2738                                 stimo->stimo_sack = stcb->asoc.timosack;
2739                                 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2740                                 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2741                                 stimo->stimo_cookie = stcb->asoc.timocookie;
2742                                 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2743                                 SCTP_TCB_UNLOCK(stcb);
2744                                 *optsize = sizeof(struct sctp_timeouts);
2745                         } else {
2746                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2747                                 error = EINVAL;
2748                         }
2749                         break;
2750                 }
2751         case SCTP_ASSOCINFO:
2752                 {
2753                         struct sctp_assocparams *sasoc;
2754
2755                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2756                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2757
2758                         if (stcb) {
2759                                 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2760                                 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2761                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2762                                 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2763                                 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2764                                 SCTP_TCB_UNLOCK(stcb);
2765                         } else {
2766                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2767                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2768                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
2769                                         SCTP_INP_RLOCK(inp);
2770                                         sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2771                                         sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2772                                         sasoc->sasoc_number_peer_destinations = 0;
2773                                         sasoc->sasoc_peer_rwnd = 0;
2774                                         sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2775                                         SCTP_INP_RUNLOCK(inp);
2776                                 } else {
2777                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2778                                         error = EINVAL;
2779                                 }
2780                         }
2781                         if (error == 0) {
2782                                 *optsize = sizeof(struct sctp_assocparams);
2783                         }
2784                         break;
2785                 }
2786         case SCTP_DEFAULT_SEND_PARAM:
2787                 {
2788                         struct sctp_sndrcvinfo *s_info;
2789
2790                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2791                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2792
2793                         if (stcb) {
2794                                 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2795                                 SCTP_TCB_UNLOCK(stcb);
2796                         } else {
2797                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2798                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2799                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
2800                                         SCTP_INP_RLOCK(inp);
2801                                         memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2802                                         SCTP_INP_RUNLOCK(inp);
2803                                 } else {
2804                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2805                                         error = EINVAL;
2806                                 }
2807                         }
2808                         if (error == 0) {
2809                                 *optsize = sizeof(struct sctp_sndrcvinfo);
2810                         }
2811                         break;
2812                 }
2813         case SCTP_INITMSG:
2814                 {
2815                         struct sctp_initmsg *sinit;
2816
2817                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2818                         SCTP_INP_RLOCK(inp);
2819                         sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2820                         sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2821                         sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2822                         sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2823                         SCTP_INP_RUNLOCK(inp);
2824                         *optsize = sizeof(struct sctp_initmsg);
2825                         break;
2826                 }
2827         case SCTP_PRIMARY_ADDR:
2828                 /* we allow a "get" operation on this */
2829                 {
2830                         struct sctp_setprim *ssp;
2831
2832                         SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2833                         SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2834
2835                         if (stcb) {
2836                                 union sctp_sockstore *addr;
2837
2838                                 addr = &stcb->asoc.primary_destination->ro._l_addr;
2839                                 switch (addr->sa.sa_family) {
2840 #ifdef INET
2841                                 case AF_INET:
2842 #ifdef INET6
2843                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2844                                                 in6_sin_2_v4mapsin6(&addr->sin,
2845                                                     (struct sockaddr_in6 *)&ssp->ssp_addr);
2846                                         } else {
2847                                                 memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2848                                         }
2849 #else
2850                                         memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2851 #endif
2852                                         break;
2853 #endif
2854 #ifdef INET6
2855                                 case AF_INET6:
2856                                         memcpy(&ssp->ssp_addr, &addr->sin6, sizeof(struct sockaddr_in6));
2857                                         break;
2858 #endif
2859                                 default:
2860                                         break;
2861                                 }
2862                                 SCTP_TCB_UNLOCK(stcb);
2863                                 *optsize = sizeof(struct sctp_setprim);
2864                         } else {
2865                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2866                                 error = EINVAL;
2867                         }
2868                         break;
2869                 }
2870         case SCTP_HMAC_IDENT:
2871                 {
2872                         struct sctp_hmacalgo *shmac;
2873                         sctp_hmaclist_t *hmaclist;
2874                         uint32_t size;
2875                         int i;
2876
2877                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2878
2879                         SCTP_INP_RLOCK(inp);
2880                         hmaclist = inp->sctp_ep.local_hmacs;
2881                         if (hmaclist == NULL) {
2882                                 /* no HMACs to return */
2883                                 *optsize = sizeof(*shmac);
2884                                 SCTP_INP_RUNLOCK(inp);
2885                                 break;
2886                         }
2887                         /* is there room for all of the hmac ids? */
2888                         size = sizeof(*shmac) + (hmaclist->num_algo *
2889                             sizeof(shmac->shmac_idents[0]));
2890                         if ((size_t)(*optsize) < size) {
2891                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2892                                 error = EINVAL;
2893                                 SCTP_INP_RUNLOCK(inp);
2894                                 break;
2895                         }
2896                         /* copy in the list */
2897                         shmac->shmac_number_of_idents = hmaclist->num_algo;
2898                         for (i = 0; i < hmaclist->num_algo; i++) {
2899                                 shmac->shmac_idents[i] = hmaclist->hmac[i];
2900                         }
2901                         SCTP_INP_RUNLOCK(inp);
2902                         *optsize = size;
2903                         break;
2904                 }
2905         case SCTP_AUTH_ACTIVE_KEY:
2906                 {
2907                         struct sctp_authkeyid *scact;
2908
2909                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2910                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2911
2912                         if (stcb) {
2913                                 /* get the active key on the assoc */
2914                                 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2915                                 SCTP_TCB_UNLOCK(stcb);
2916                         } else {
2917                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2918                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2919                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
2920                                         /* get the endpoint active key */
2921                                         SCTP_INP_RLOCK(inp);
2922                                         scact->scact_keynumber = inp->sctp_ep.default_keyid;
2923                                         SCTP_INP_RUNLOCK(inp);
2924                                 } else {
2925                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2926                                         error = EINVAL;
2927                                 }
2928                         }
2929                         if (error == 0) {
2930                                 *optsize = sizeof(struct sctp_authkeyid);
2931                         }
2932                         break;
2933                 }
2934         case SCTP_LOCAL_AUTH_CHUNKS:
2935                 {
2936                         struct sctp_authchunks *sac;
2937                         sctp_auth_chklist_t *chklist = NULL;
2938                         size_t size = 0;
2939
2940                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2941                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2942
2943                         if (stcb) {
2944                                 /* get off the assoc */
2945                                 chklist = stcb->asoc.local_auth_chunks;
2946                                 /* is there enough space? */
2947                                 size = sctp_auth_get_chklist_size(chklist);
2948                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2949                                         error = EINVAL;
2950                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2951                                 } else {
2952                                         /* copy in the chunks */
2953                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2954                                         sac->gauth_number_of_chunks = (uint32_t) size;
2955                                         *optsize = sizeof(struct sctp_authchunks) + size;
2956                                 }
2957                                 SCTP_TCB_UNLOCK(stcb);
2958                         } else {
2959                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2960                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2961                                     (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
2962                                         /* get off the endpoint */
2963                                         SCTP_INP_RLOCK(inp);
2964                                         chklist = inp->sctp_ep.local_auth_chunks;
2965                                         /* is there enough space? */
2966                                         size = sctp_auth_get_chklist_size(chklist);
2967                                         if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2968                                                 error = EINVAL;
2969                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2970                                         } else {
2971                                                 /* copy in the chunks */
2972                                                 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2973                                                 sac->gauth_number_of_chunks = (uint32_t) size;
2974                                                 *optsize = sizeof(struct sctp_authchunks) + size;
2975                                         }
2976                                         SCTP_INP_RUNLOCK(inp);
2977                                 } else {
2978                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2979                                         error = EINVAL;
2980                                 }
2981                         }
2982                         break;
2983                 }
2984         case SCTP_PEER_AUTH_CHUNKS:
2985                 {
2986                         struct sctp_authchunks *sac;
2987                         sctp_auth_chklist_t *chklist = NULL;
2988                         size_t size = 0;
2989
2990                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2991                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2992
2993                         if (stcb) {
2994                                 /* get off the assoc */
2995                                 chklist = stcb->asoc.peer_auth_chunks;
2996                                 /* is there enough space? */
2997                                 size = sctp_auth_get_chklist_size(chklist);
2998                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2999                                         error = EINVAL;
3000                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3001                                 } else {
3002                                         /* copy in the chunks */
3003                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3004                                         sac->gauth_number_of_chunks = (uint32_t) size;
3005                                         *optsize = sizeof(struct sctp_authchunks) + size;
3006                                 }
3007                                 SCTP_TCB_UNLOCK(stcb);
3008                         } else {
3009                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3010                                 error = ENOENT;
3011                         }
3012                         break;
3013                 }
3014         case SCTP_EVENT:
3015                 {
3016                         struct sctp_event *event;
3017                         uint32_t event_type;
3018
3019                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
3020                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
3021
3022                         switch (event->se_type) {
3023                         case SCTP_ASSOC_CHANGE:
3024                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
3025                                 break;
3026                         case SCTP_PEER_ADDR_CHANGE:
3027                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
3028                                 break;
3029                         case SCTP_REMOTE_ERROR:
3030                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
3031                                 break;
3032                         case SCTP_SEND_FAILED:
3033                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3034                                 break;
3035                         case SCTP_SHUTDOWN_EVENT:
3036                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3037                                 break;
3038                         case SCTP_ADAPTATION_INDICATION:
3039                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
3040                                 break;
3041                         case SCTP_PARTIAL_DELIVERY_EVENT:
3042                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
3043                                 break;
3044                         case SCTP_AUTHENTICATION_EVENT:
3045                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
3046                                 break;
3047                         case SCTP_STREAM_RESET_EVENT:
3048                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3049                                 break;
3050                         case SCTP_SENDER_DRY_EVENT:
3051                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
3052                                 break;
3053                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
3054                                 event_type = 0;
3055                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
3056                                 error = ENOTSUP;
3057                                 break;
3058                         case SCTP_ASSOC_RESET_EVENT:
3059                                 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
3060                                 break;
3061                         case SCTP_STREAM_CHANGE_EVENT:
3062                                 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
3063                                 break;
3064                         case SCTP_SEND_FAILED_EVENT:
3065                                 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
3066                                 break;
3067                         default:
3068                                 event_type = 0;
3069                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3070                                 error = EINVAL;
3071                                 break;
3072                         }
3073                         if (event_type > 0) {
3074                                 if (stcb) {
3075                                         event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
3076                                         SCTP_TCB_UNLOCK(stcb);
3077                                 } else {
3078                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3079                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3080                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3081                                                 SCTP_INP_RLOCK(inp);
3082                                                 event->se_on = sctp_is_feature_on(inp, event_type);
3083                                                 SCTP_INP_RUNLOCK(inp);
3084                                         } else {
3085                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3086                                                 error = EINVAL;
3087                                         }
3088                                 }
3089                         }
3090                         if (error == 0) {
3091                                 *optsize = sizeof(struct sctp_event);
3092                         }
3093                         break;
3094                 }
3095         case SCTP_RECVRCVINFO:
3096                 {
3097                         int onoff;
3098
3099                         if (*optsize < sizeof(int)) {
3100                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3101                                 error = EINVAL;
3102                         } else {
3103                                 SCTP_INP_RLOCK(inp);
3104                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3105                                 SCTP_INP_RUNLOCK(inp);
3106                         }
3107                         if (error == 0) {
3108                                 /* return the option value */
3109                                 *(int *)optval = onoff;
3110                                 *optsize = sizeof(int);
3111                         }
3112                         break;
3113                 }
3114         case SCTP_RECVNXTINFO:
3115                 {
3116                         int onoff;
3117
3118                         if (*optsize < sizeof(int)) {
3119                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3120                                 error = EINVAL;
3121                         } else {
3122                                 SCTP_INP_RLOCK(inp);
3123                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3124                                 SCTP_INP_RUNLOCK(inp);
3125                         }
3126                         if (error == 0) {
3127                                 /* return the option value */
3128                                 *(int *)optval = onoff;
3129                                 *optsize = sizeof(int);
3130                         }
3131                         break;
3132                 }
3133         case SCTP_DEFAULT_SNDINFO:
3134                 {
3135                         struct sctp_sndinfo *info;
3136
3137                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3138                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3139
3140                         if (stcb) {
3141                                 info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3142                                 info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3143                                 info->snd_flags &= 0xfff0;
3144                                 info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3145                                 info->snd_context = stcb->asoc.def_send.sinfo_context;
3146                                 SCTP_TCB_UNLOCK(stcb);
3147                         } else {
3148                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3149                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3150                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3151                                         SCTP_INP_RLOCK(inp);
3152                                         info->snd_sid = inp->def_send.sinfo_stream;
3153                                         info->snd_flags = inp->def_send.sinfo_flags;
3154                                         info->snd_flags &= 0xfff0;
3155                                         info->snd_ppid = inp->def_send.sinfo_ppid;
3156                                         info->snd_context = inp->def_send.sinfo_context;
3157                                         SCTP_INP_RUNLOCK(inp);
3158                                 } else {
3159                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3160                                         error = EINVAL;
3161                                 }
3162                         }
3163                         if (error == 0) {
3164                                 *optsize = sizeof(struct sctp_sndinfo);
3165                         }
3166                         break;
3167                 }
3168         case SCTP_DEFAULT_PRINFO:
3169                 {
3170                         struct sctp_default_prinfo *info;
3171
3172                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3173                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3174
3175                         if (stcb) {
3176                                 info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3177                                 info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3178                                 SCTP_TCB_UNLOCK(stcb);
3179                         } else {
3180                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3181                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3182                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3183                                         SCTP_INP_RLOCK(inp);
3184                                         info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3185                                         info->pr_value = inp->def_send.sinfo_timetolive;
3186                                         SCTP_INP_RUNLOCK(inp);
3187                                 } else {
3188                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3189                                         error = EINVAL;
3190                                 }
3191                         }
3192                         if (error == 0) {
3193                                 *optsize = sizeof(struct sctp_default_prinfo);
3194                         }
3195                         break;
3196                 }
3197         case SCTP_PEER_ADDR_THLDS:
3198                 {
3199                         struct sctp_paddrthlds *thlds;
3200                         struct sctp_nets *net;
3201                         struct sockaddr *addr;
3202
3203 #if defined(INET) && defined(INET6)
3204                         struct sockaddr_in sin_store;
3205
3206 #endif
3207
3208                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3209                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3210
3211 #if defined(INET) && defined(INET6)
3212                         if (thlds->spt_address.ss_family == AF_INET6) {
3213                                 struct sockaddr_in6 *sin6;
3214
3215                                 sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
3216                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3217                                         in6_sin6_2_sin(&sin_store, sin6);
3218                                         addr = (struct sockaddr *)&sin_store;
3219                                 } else {
3220                                         addr = (struct sockaddr *)&thlds->spt_address;
3221                                 }
3222                         } else {
3223                                 addr = (struct sockaddr *)&thlds->spt_address;
3224                         }
3225 #else
3226                         addr = (struct sockaddr *)&thlds->spt_address;
3227 #endif
3228                         if (stcb != NULL) {
3229                                 net = sctp_findnet(stcb, addr);
3230                         } else {
3231                                 /*
3232                                  * We increment here since
3233                                  * sctp_findassociation_ep_addr() wil do a
3234                                  * decrement if it finds the stcb as long as
3235                                  * the locked tcb (last argument) is NOT a
3236                                  * TCB.. aka NULL.
3237                                  */
3238                                 net = NULL;
3239                                 SCTP_INP_INCR_REF(inp);
3240                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3241                                 if (stcb == NULL) {
3242                                         SCTP_INP_DECR_REF(inp);
3243                                 }
3244                         }
3245                         if ((stcb != NULL) && (net == NULL)) {
3246 #ifdef INET
3247                                 if (addr->sa_family == AF_INET) {
3248                                         struct sockaddr_in *sin;
3249
3250                                         sin = (struct sockaddr_in *)addr;
3251                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
3252                                                 error = EINVAL;
3253                                                 SCTP_TCB_UNLOCK(stcb);
3254                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3255                                                 break;
3256                                         }
3257                                 } else
3258 #endif
3259 #ifdef INET6
3260                                 if (addr->sa_family == AF_INET6) {
3261                                         struct sockaddr_in6 *sin6;
3262
3263                                         sin6 = (struct sockaddr_in6 *)addr;
3264                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3265                                                 error = EINVAL;
3266                                                 SCTP_TCB_UNLOCK(stcb);
3267                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3268                                                 break;
3269                                         }
3270                                 } else
3271 #endif
3272                                 {
3273                                         error = EAFNOSUPPORT;
3274                                         SCTP_TCB_UNLOCK(stcb);
3275                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3276                                         break;
3277                                 }
3278                         }
3279                         if (stcb != NULL) {
3280                                 if (net != NULL) {
3281                                         thlds->spt_pathmaxrxt = net->failure_threshold;
3282                                         thlds->spt_pathpfthld = net->pf_threshold;
3283                                 } else {
3284                                         thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3285                                         thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3286                                 }
3287                                 thlds->spt_assoc_id = sctp_get_associd(stcb);
3288                                 SCTP_TCB_UNLOCK(stcb);
3289                         } else {
3290                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3291                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3292                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3293                                         /* Use endpoint defaults */
3294                                         SCTP_INP_RLOCK(inp);
3295                                         thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3296                                         thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3297                                         SCTP_INP_RUNLOCK(inp);
3298                                 } else {
3299                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3300                                         error = EINVAL;
3301                                 }
3302                         }
3303                         if (error == 0) {
3304                                 *optsize = sizeof(struct sctp_paddrthlds);
3305                         }
3306                         break;
3307                 }
3308         case SCTP_REMOTE_UDP_ENCAPS_PORT:
3309                 {
3310                         struct sctp_udpencaps *encaps;
3311                         struct sctp_nets *net;
3312                         struct sockaddr *addr;
3313
3314 #if defined(INET) && defined(INET6)
3315                         struct sockaddr_in sin_store;
3316
3317 #endif
3318
3319                         SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3320                         SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3321
3322 #if defined(INET) && defined(INET6)
3323                         if (encaps->sue_address.ss_family == AF_INET6) {
3324                                 struct sockaddr_in6 *sin6;
3325
3326                                 sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
3327                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3328                                         in6_sin6_2_sin(&sin_store, sin6);
3329                                         addr = (struct sockaddr *)&sin_store;
3330                                 } else {
3331                                         addr = (struct sockaddr *)&encaps->sue_address;
3332                                 }
3333                         } else {
3334                                 addr = (struct sockaddr *)&encaps->sue_address;
3335                         }
3336 #else
3337                         addr = (struct sockaddr *)&encaps->sue_address;
3338 #endif
3339                         if (stcb) {
3340                                 net = sctp_findnet(stcb, addr);
3341                         } else {
3342                                 /*
3343                                  * We increment here since
3344                                  * sctp_findassociation_ep_addr() wil do a
3345                                  * decrement if it finds the stcb as long as
3346                                  * the locked tcb (last argument) is NOT a
3347                                  * TCB.. aka NULL.
3348                                  */
3349                                 net = NULL;
3350                                 SCTP_INP_INCR_REF(inp);
3351                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3352                                 if (stcb == NULL) {
3353                                         SCTP_INP_DECR_REF(inp);
3354                                 }
3355                         }
3356                         if ((stcb != NULL) && (net == NULL)) {
3357 #ifdef INET
3358                                 if (addr->sa_family == AF_INET) {
3359                                         struct sockaddr_in *sin;
3360
3361                                         sin = (struct sockaddr_in *)addr;
3362                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
3363                                                 error = EINVAL;
3364                                                 SCTP_TCB_UNLOCK(stcb);
3365                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3366                                                 break;
3367                                         }
3368                                 } else
3369 #endif
3370 #ifdef INET6
3371                                 if (addr->sa_family == AF_INET6) {
3372                                         struct sockaddr_in6 *sin6;
3373
3374                                         sin6 = (struct sockaddr_in6 *)addr;
3375                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3376                                                 error = EINVAL;
3377                                                 SCTP_TCB_UNLOCK(stcb);
3378                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3379                                                 break;
3380                                         }
3381                                 } else
3382 #endif
3383                                 {
3384                                         error = EAFNOSUPPORT;
3385                                         SCTP_TCB_UNLOCK(stcb);
3386                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3387                                         break;
3388                                 }
3389                         }
3390                         if (stcb != NULL) {
3391                                 if (net) {
3392                                         encaps->sue_port = net->port;
3393                                 } else {
3394                                         encaps->sue_port = stcb->asoc.port;
3395                                 }
3396                                 SCTP_TCB_UNLOCK(stcb);
3397                         } else {
3398                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3399                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3400                                     (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3401                                         SCTP_INP_RLOCK(inp);
3402                                         encaps->sue_port = inp->sctp_ep.port;
3403                                         SCTP_INP_RUNLOCK(inp);
3404                                 } else {
3405                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3406                                         error = EINVAL;
3407                                 }
3408                         }
3409                         if (error == 0) {
3410                                 *optsize = sizeof(struct sctp_udpencaps);
3411                         }
3412                         break;
3413                 }
3414         case SCTP_ECN_SUPPORTED:
3415                 {
3416                         struct sctp_assoc_value *av;
3417
3418                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3419                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3420
3421                         if (stcb) {
3422                                 av->assoc_value = stcb->asoc.ecn_supported;
3423                                 SCTP_TCB_UNLOCK(stcb);
3424                         } else {
3425                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3426                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3427                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3428                                         SCTP_INP_RLOCK(inp);
3429                                         av->assoc_value = inp->ecn_supported;
3430                                         SCTP_INP_RUNLOCK(inp);
3431                                 } else {
3432                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3433                                         error = EINVAL;
3434                                 }
3435                         }
3436                         if (error == 0) {
3437                                 *optsize = sizeof(struct sctp_assoc_value);
3438                         }
3439                         break;
3440                 }
3441         case SCTP_PR_SUPPORTED:
3442                 {
3443                         struct sctp_assoc_value *av;
3444
3445                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3446                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3447
3448                         if (stcb) {
3449                                 av->assoc_value = stcb->asoc.prsctp_supported;
3450                                 SCTP_TCB_UNLOCK(stcb);
3451                         } else {
3452                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3453                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3454                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3455                                         SCTP_INP_RLOCK(inp);
3456                                         av->assoc_value = inp->prsctp_supported;
3457                                         SCTP_INP_RUNLOCK(inp);
3458                                 } else {
3459                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3460                                         error = EINVAL;
3461                                 }
3462                         }
3463                         if (error == 0) {
3464                                 *optsize = sizeof(struct sctp_assoc_value);
3465                         }
3466                         break;
3467                 }
3468         case SCTP_AUTH_SUPPORTED:
3469                 {
3470                         struct sctp_assoc_value *av;
3471
3472                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3473                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3474
3475                         if (stcb) {
3476                                 av->assoc_value = stcb->asoc.auth_supported;
3477                                 SCTP_TCB_UNLOCK(stcb);
3478                         } else {
3479                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3480                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3481                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3482                                         SCTP_INP_RLOCK(inp);
3483                                         av->assoc_value = inp->auth_supported;
3484                                         SCTP_INP_RUNLOCK(inp);
3485                                 } else {
3486                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3487                                         error = EINVAL;
3488                                 }
3489                         }
3490                         if (error == 0) {
3491                                 *optsize = sizeof(struct sctp_assoc_value);
3492                         }
3493                         break;
3494                 }
3495         case SCTP_ASCONF_SUPPORTED:
3496                 {
3497                         struct sctp_assoc_value *av;
3498
3499                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3500                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3501
3502                         if (stcb) {
3503                                 av->assoc_value = stcb->asoc.asconf_supported;
3504                                 SCTP_TCB_UNLOCK(stcb);
3505                         } else {
3506                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3507                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3508                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3509                                         SCTP_INP_RLOCK(inp);
3510                                         av->assoc_value = inp->asconf_supported;
3511                                         SCTP_INP_RUNLOCK(inp);
3512                                 } else {
3513                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3514                                         error = EINVAL;
3515                                 }
3516                         }
3517                         if (error == 0) {
3518                                 *optsize = sizeof(struct sctp_assoc_value);
3519                         }
3520                         break;
3521                 }
3522         case SCTP_RECONFIG_SUPPORTED:
3523                 {
3524                         struct sctp_assoc_value *av;
3525
3526                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3527                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3528
3529                         if (stcb) {
3530                                 av->assoc_value = stcb->asoc.reconfig_supported;
3531                                 SCTP_TCB_UNLOCK(stcb);
3532                         } else {
3533                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3534                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3535                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3536                                         SCTP_INP_RLOCK(inp);
3537                                         av->assoc_value = inp->reconfig_supported;
3538                                         SCTP_INP_RUNLOCK(inp);
3539                                 } else {
3540                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3541                                         error = EINVAL;
3542                                 }
3543                         }
3544                         if (error == 0) {
3545                                 *optsize = sizeof(struct sctp_assoc_value);
3546                         }
3547                         break;
3548                 }
3549         case SCTP_NRSACK_SUPPORTED:
3550                 {
3551                         struct sctp_assoc_value *av;
3552
3553                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3554                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3555
3556                         if (stcb) {
3557                                 av->assoc_value = stcb->asoc.nrsack_supported;
3558                                 SCTP_TCB_UNLOCK(stcb);
3559                         } else {
3560                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3561                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3562                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3563                                         SCTP_INP_RLOCK(inp);
3564                                         av->assoc_value = inp->nrsack_supported;
3565                                         SCTP_INP_RUNLOCK(inp);
3566                                 } else {
3567                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3568                                         error = EINVAL;
3569                                 }
3570                         }
3571                         if (error == 0) {
3572                                 *optsize = sizeof(struct sctp_assoc_value);
3573                         }
3574                         break;
3575                 }
3576         case SCTP_PKTDROP_SUPPORTED:
3577                 {
3578                         struct sctp_assoc_value *av;
3579
3580                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3581                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3582
3583                         if (stcb) {
3584                                 av->assoc_value = stcb->asoc.pktdrop_supported;
3585                                 SCTP_TCB_UNLOCK(stcb);
3586                         } else {
3587                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3588                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3589                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3590                                         SCTP_INP_RLOCK(inp);
3591                                         av->assoc_value = inp->pktdrop_supported;
3592                                         SCTP_INP_RUNLOCK(inp);
3593                                 } else {
3594                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3595                                         error = EINVAL;
3596                                 }
3597                         }
3598                         if (error == 0) {
3599                                 *optsize = sizeof(struct sctp_assoc_value);
3600                         }
3601                         break;
3602                 }
3603         case SCTP_ENABLE_STREAM_RESET:
3604                 {
3605                         struct sctp_assoc_value *av;
3606
3607                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3608                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3609
3610                         if (stcb) {
3611                                 av->assoc_value = (uint32_t) stcb->asoc.local_strreset_support;
3612                                 SCTP_TCB_UNLOCK(stcb);
3613                         } else {
3614                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3615                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3616                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3617                                         SCTP_INP_RLOCK(inp);
3618                                         av->assoc_value = (uint32_t) inp->local_strreset_support;
3619                                         SCTP_INP_RUNLOCK(inp);
3620                                 } else {
3621                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3622                                         error = EINVAL;
3623                                 }
3624                         }
3625                         if (error == 0) {
3626                                 *optsize = sizeof(struct sctp_assoc_value);
3627                         }
3628                         break;
3629                 }
3630         case SCTP_PR_STREAM_STATUS:
3631                 {
3632                         struct sctp_prstatus *sprstat;
3633                         uint16_t sid;
3634                         uint16_t policy;
3635
3636                         SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3637                         SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3638
3639                         sid = sprstat->sprstat_sid;
3640                         policy = sprstat->sprstat_policy;
3641 #if defined(SCTP_DETAILED_STR_STATS)
3642                         if ((stcb != NULL) &&
3643                             (sid < stcb->asoc.streamoutcnt) &&
3644                             (policy != SCTP_PR_SCTP_NONE) &&
3645                             ((policy <= SCTP_PR_SCTP_MAX) ||
3646                             (policy == SCTP_PR_SCTP_ALL))) {
3647                                 if (policy == SCTP_PR_SCTP_ALL) {
3648                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3649                                         sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3650                                 } else {
3651                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
3652                                         sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
3653                                 }
3654 #else
3655                         if ((stcb != NULL) &&
3656                             (sid < stcb->asoc.streamoutcnt) &&
3657                             (policy == SCTP_PR_SCTP_ALL)) {
3658                                 sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3659                                 sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3660 #endif
3661                                 SCTP_TCB_UNLOCK(stcb);
3662                                 *optsize = sizeof(struct sctp_prstatus);
3663                         } else {
3664                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3665                                 error = EINVAL;
3666                         }
3667                         break;
3668                 }
3669         case SCTP_PR_ASSOC_STATUS:
3670                 {
3671                         struct sctp_prstatus *sprstat;
3672                         uint16_t policy;
3673
3674                         SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3675                         SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3676
3677                         policy = sprstat->sprstat_policy;
3678                         if ((stcb != NULL) &&
3679                             (policy != SCTP_PR_SCTP_NONE) &&
3680                             ((policy <= SCTP_PR_SCTP_MAX) ||
3681                             (policy == SCTP_PR_SCTP_ALL))) {
3682                                 if (policy == SCTP_PR_SCTP_ALL) {
3683                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
3684                                         sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
3685                                 } else {
3686                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
3687                                         sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
3688                                 }
3689                                 SCTP_TCB_UNLOCK(stcb);
3690                                 *optsize = sizeof(struct sctp_prstatus);
3691                         } else {
3692                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3693                                 error = EINVAL;
3694                         }
3695                         break;
3696                 }
3697         case SCTP_MAX_CWND:
3698                 {
3699                         struct sctp_assoc_value *av;
3700
3701                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3702                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3703
3704                         if (stcb) {
3705                                 av->assoc_value = stcb->asoc.max_cwnd;
3706                                 SCTP_TCB_UNLOCK(stcb);
3707                         } else {
3708                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3709                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3710                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3711                                         SCTP_INP_RLOCK(inp);
3712                                         av->assoc_value = inp->max_cwnd;
3713                                         SCTP_INP_RUNLOCK(inp);
3714                                 } else {
3715                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3716                                         error = EINVAL;
3717                                 }
3718                         }
3719                         if (error == 0) {
3720                                 *optsize = sizeof(struct sctp_assoc_value);
3721                         }
3722                         break;
3723                 }
3724         default:
3725                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3726                 error = ENOPROTOOPT;
3727                 break;
3728         }                       /* end switch (sopt->sopt_name) */
3729         if (error) {
3730                 *optsize = 0;
3731         }
3732         return (error);
3733 }
3734
3735 static int
3736 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3737     void *p)
3738 {
3739         int error, set_opt;
3740         uint32_t *mopt;
3741         struct sctp_tcb *stcb = NULL;
3742         struct sctp_inpcb *inp = NULL;
3743         uint32_t vrf_id;
3744
3745         if (optval == NULL) {
3746                 SCTP_PRINTF("optval is NULL\n");
3747                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3748                 return (EINVAL);
3749         }
3750         inp = (struct sctp_inpcb *)so->so_pcb;
3751         if (inp == NULL) {
3752                 SCTP_PRINTF("inp is NULL?\n");
3753                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3754                 return (EINVAL);
3755         }
3756         vrf_id = inp->def_vrf_id;
3757
3758         error = 0;
3759         switch (optname) {
3760         case SCTP_NODELAY:
3761         case SCTP_AUTOCLOSE:
3762         case SCTP_AUTO_ASCONF:
3763         case SCTP_EXPLICIT_EOR:
3764         case SCTP_DISABLE_FRAGMENTS:
3765         case SCTP_USE_EXT_RCVINFO:
3766         case SCTP_I_WANT_MAPPED_V4_ADDR:
3767                 /* copy in the option value */
3768                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3769                 set_opt = 0;
3770                 if (error)
3771                         break;
3772                 switch (optname) {
3773                 case SCTP_DISABLE_FRAGMENTS:
3774                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3775                         break;
3776                 case SCTP_AUTO_ASCONF:
3777                         /*
3778                          * NOTE: we don't really support this flag
3779                          */
3780                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3781                                 /* only valid for bound all sockets */
3782                                 if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3783                                     (*mopt != 0)) {
3784                                         /* forbidden by admin */
3785                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3786                                         return (EPERM);
3787                                 }
3788                                 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3789                         } else {
3790                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3791                                 return (EINVAL);
3792                         }
3793                         break;
3794                 case SCTP_EXPLICIT_EOR:
3795                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3796                         break;
3797                 case SCTP_USE_EXT_RCVINFO:
3798                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3799                         break;
3800                 case SCTP_I_WANT_MAPPED_V4_ADDR:
3801                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3802                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3803                         } else {
3804                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3805                                 return (EINVAL);
3806                         }
3807                         break;
3808                 case SCTP_NODELAY:
3809                         set_opt = SCTP_PCB_FLAGS_NODELAY;
3810                         break;
3811                 case SCTP_AUTOCLOSE:
3812                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3813                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3814                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3815                                 return (EINVAL);
3816                         }
3817                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3818                         /*
3819                          * The value is in ticks. Note this does not effect
3820                          * old associations, only new ones.
3821                          */
3822                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3823                         break;
3824                 }
3825                 SCTP_INP_WLOCK(inp);
3826                 if (*mopt != 0) {
3827                         sctp_feature_on(inp, set_opt);
3828                 } else {
3829                         sctp_feature_off(inp, set_opt);
3830                 }
3831                 SCTP_INP_WUNLOCK(inp);
3832                 break;
3833         case SCTP_REUSE_PORT:
3834                 {
3835                         SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3836                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3837                                 /* Can't set it after we are bound */
3838                                 error = EINVAL;
3839                                 break;
3840                         }
3841                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3842                                 /* Can't do this for a 1-m socket */
3843                                 error = EINVAL;
3844                                 break;
3845                         }
3846                         if (optval)
3847                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3848                         else
3849                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3850                         break;
3851                 }
3852         case SCTP_PARTIAL_DELIVERY_POINT:
3853                 {
3854                         uint32_t *value;
3855
3856                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3857                         if (*value > SCTP_SB_LIMIT_RCV(so)) {
3858                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3859                                 error = EINVAL;
3860                                 break;
3861                         }
3862                         inp->partial_delivery_point = *value;
3863                         break;
3864                 }
3865         case SCTP_FRAGMENT_INTERLEAVE:
3866                 /* not yet until we re-write sctp_recvmsg() */
3867                 {
3868                         uint32_t *level;
3869
3870                         SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3871                         if (*level == SCTP_FRAG_LEVEL_2) {
3872                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3873                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3874                         } else if (*level == SCTP_FRAG_LEVEL_1) {
3875                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3876                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3877                         } else if (*level == SCTP_FRAG_LEVEL_0) {
3878                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3879                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3880
3881                         } else {
3882                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3883                                 error = EINVAL;
3884                         }
3885                         break;
3886                 }
3887         case SCTP_CMT_ON_OFF:
3888                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3889                         struct sctp_assoc_value *av;
3890
3891                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3892                         if (av->assoc_value > SCTP_CMT_MAX) {
3893                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3894                                 error = EINVAL;
3895                                 break;
3896                         }
3897                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3898                         if (stcb) {
3899                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3900                                 SCTP_TCB_UNLOCK(stcb);
3901                         } else {
3902                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3903                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3904                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3905                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3906                                         SCTP_INP_WLOCK(inp);
3907                                         inp->sctp_cmt_on_off = av->assoc_value;
3908                                         SCTP_INP_WUNLOCK(inp);
3909                                 }
3910                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3911                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3912                                         SCTP_INP_RLOCK(inp);
3913                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3914                                                 SCTP_TCB_LOCK(stcb);
3915                                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3916                                                 SCTP_TCB_UNLOCK(stcb);
3917                                         }
3918                                         SCTP_INP_RUNLOCK(inp);
3919                                 }
3920                         }
3921                 } else {
3922                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3923                         error = ENOPROTOOPT;
3924                 }
3925                 break;
3926         case SCTP_PLUGGABLE_CC:
3927                 {
3928                         struct sctp_assoc_value *av;
3929                         struct sctp_nets *net;
3930
3931                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3932                         if ((av->assoc_value != SCTP_CC_RFC2581) &&
3933                             (av->assoc_value != SCTP_CC_HSTCP) &&
3934                             (av->assoc_value != SCTP_CC_HTCP) &&
3935                             (av->assoc_value != SCTP_CC_RTCC)) {
3936                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3937                                 error = EINVAL;
3938                                 break;
3939                         }
3940                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3941                         if (stcb) {
3942                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3943                                 stcb->asoc.congestion_control_module = av->assoc_value;
3944                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3945                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3946                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3947                                         }
3948                                 }
3949                                 SCTP_TCB_UNLOCK(stcb);
3950                         } else {
3951                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3952                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3953                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3954                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3955                                         SCTP_INP_WLOCK(inp);
3956                                         inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
3957                                         SCTP_INP_WUNLOCK(inp);
3958                                 }
3959                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3960                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3961                                         SCTP_INP_RLOCK(inp);
3962                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3963                                                 SCTP_TCB_LOCK(stcb);
3964                                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3965                                                 stcb->asoc.congestion_control_module = av->assoc_value;
3966                                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3967                                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3968                                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3969                                                         }
3970                                                 }
3971                                                 SCTP_TCB_UNLOCK(stcb);
3972                                         }
3973                                         SCTP_INP_RUNLOCK(inp);
3974                                 }
3975                         }
3976                         break;
3977                 }
3978         case SCTP_CC_OPTION:
3979                 {
3980                         struct sctp_cc_option *cc_opt;
3981
3982                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
3983                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
3984                         if (stcb == NULL) {
3985                                 if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
3986                                         SCTP_INP_RLOCK(inp);
3987                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3988                                                 SCTP_TCB_LOCK(stcb);
3989                                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
3990                                                         (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
3991                                                 }
3992                                                 SCTP_TCB_UNLOCK(stcb);
3993                                         }
3994                                         SCTP_INP_RUNLOCK(inp);
3995                                 } else {
3996                                         error = EINVAL;
3997                                 }
3998                         } else {
3999                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
4000                                         error = ENOTSUP;
4001                                 } else {
4002                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
4003                                             cc_opt);
4004                                 }
4005                                 SCTP_TCB_UNLOCK(stcb);
4006                         }
4007                         break;
4008                 }
4009         case SCTP_PLUGGABLE_SS:
4010                 {
4011                         struct sctp_assoc_value *av;
4012
4013                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4014                         if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4015                             (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4016                             (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4017                             (av->assoc_value != SCTP_SS_PRIORITY) &&
4018                             (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4019                             (av->assoc_value != SCTP_SS_FIRST_COME)) {
4020                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4021                                 error = EINVAL;
4022                                 break;
4023                         }
4024                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4025                         if (stcb) {
4026                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4027                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4028                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4029                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4030                                 SCTP_TCB_UNLOCK(stcb);
4031                         } else {
4032                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4033                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4034                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4035                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4036                                         SCTP_INP_WLOCK(inp);
4037                                         inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
4038                                         SCTP_INP_WUNLOCK(inp);
4039                                 }
4040                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4041                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4042                                         SCTP_INP_RLOCK(inp);
4043                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4044                                                 SCTP_TCB_LOCK(stcb);
4045                                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4046                                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4047                                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4048                                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4049                                                 SCTP_TCB_UNLOCK(stcb);
4050                                         }
4051                                         SCTP_INP_RUNLOCK(inp);
4052                                 }
4053                         }
4054                         break;
4055                 }
4056         case SCTP_SS_VALUE:
4057                 {
4058                         struct sctp_stream_value *av;
4059
4060                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4061                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4062                         if (stcb) {
4063                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
4064                                     (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4065                                     av->stream_value) < 0)) {
4066                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4067                                         error = EINVAL;
4068                                 }
4069                                 SCTP_TCB_UNLOCK(stcb);
4070                         } else {
4071                                 if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4072                                         SCTP_INP_RLOCK(inp);
4073                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4074                                                 SCTP_TCB_LOCK(stcb);
4075                                                 if (av->stream_id < stcb->asoc.streamoutcnt) {
4076                                                         stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4077                                                             &stcb->asoc,
4078                                                             &stcb->asoc.strmout[av->stream_id],
4079                                                             av->stream_value);
4080                                                 }
4081                                                 SCTP_TCB_UNLOCK(stcb);
4082                                         }
4083                                         SCTP_INP_RUNLOCK(inp);
4084                                 } else {
4085                                         /*
4086                                          * Can't set stream value without
4087                                          * association
4088                                          */
4089                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4090                                         error = EINVAL;
4091                                 }
4092                         }
4093                         break;
4094                 }
4095         case SCTP_CLR_STAT_LOG:
4096                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4097                 error = EOPNOTSUPP;
4098                 break;
4099         case SCTP_CONTEXT:
4100                 {
4101                         struct sctp_assoc_value *av;
4102
4103                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4104                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4105
4106                         if (stcb) {
4107                                 stcb->asoc.context = av->assoc_value;
4108                                 SCTP_TCB_UNLOCK(stcb);
4109                         } else {
4110                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4111                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4112                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4113                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4114                                         SCTP_INP_WLOCK(inp);
4115                                         inp->sctp_context = av->assoc_value;
4116                                         SCTP_INP_WUNLOCK(inp);
4117                                 }
4118                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4119                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4120                                         SCTP_INP_RLOCK(inp);
4121                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4122                                                 SCTP_TCB_LOCK(stcb);
4123                                                 stcb->asoc.context = av->assoc_value;
4124                                                 SCTP_TCB_UNLOCK(stcb);
4125                                         }
4126                                         SCTP_INP_RUNLOCK(inp);
4127                                 }
4128                         }
4129                         break;
4130                 }
4131         case SCTP_VRF_ID:
4132                 {
4133                         uint32_t *default_vrfid;
4134
4135                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4136                         if (*default_vrfid > SCTP_MAX_VRF_ID) {
4137                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4138                                 error = EINVAL;
4139                                 break;
4140                         }
4141                         inp->def_vrf_id = *default_vrfid;
4142                         break;
4143                 }
4144         case SCTP_DEL_VRF_ID:
4145                 {
4146                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4147                         error = EOPNOTSUPP;
4148                         break;
4149                 }
4150         case SCTP_ADD_VRF_ID:
4151                 {
4152                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4153                         error = EOPNOTSUPP;
4154                         break;
4155                 }
4156         case SCTP_DELAYED_SACK:
4157                 {
4158                         struct sctp_sack_info *sack;
4159
4160                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4161                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4162                         if (sack->sack_delay) {
4163                                 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4164                                         sack->sack_delay = SCTP_MAX_SACK_DELAY;
4165                                 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4166                                         sack->sack_delay = TICKS_TO_MSEC(1);
4167                                 }
4168                         }
4169                         if (stcb) {
4170                                 if (sack->sack_delay) {
4171                                         stcb->asoc.delayed_ack = sack->sack_delay;
4172                                 }
4173                                 if (sack->sack_freq) {
4174                                         stcb->asoc.sack_freq = sack->sack_freq;
4175                                 }
4176                                 SCTP_TCB_UNLOCK(stcb);
4177                         } else {
4178                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4179                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4180                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4181                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4182                                         SCTP_INP_WLOCK(inp);
4183                                         if (sack->sack_delay) {
4184                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4185                                         }
4186                                         if (sack->sack_freq) {
4187                                                 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4188                                         }
4189                                         SCTP_INP_WUNLOCK(inp);
4190                                 }
4191                                 if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4192                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4193                                         SCTP_INP_RLOCK(inp);
4194                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4195                                                 SCTP_TCB_LOCK(stcb);
4196                                                 if (sack->sack_delay) {
4197                                                         stcb->asoc.delayed_ack = sack->sack_delay;
4198                                                 }
4199                                                 if (sack->sack_freq) {
4200                                                         stcb->asoc.sack_freq = sack->sack_freq;
4201                                                 }
4202                                                 SCTP_TCB_UNLOCK(stcb);
4203                                         }
4204                                         SCTP_INP_RUNLOCK(inp);
4205                                 }
4206                         }
4207                         break;
4208                 }
4209         case SCTP_AUTH_CHUNK:
4210                 {
4211                         struct sctp_authchunk *sauth;
4212
4213                         SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4214
4215                         SCTP_INP_WLOCK(inp);
4216                         if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4217                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4218                                 error = EINVAL;
4219                         }
4220                         SCTP_INP_WUNLOCK(inp);
4221                         break;
4222                 }
4223         case SCTP_AUTH_KEY:
4224                 {
4225                         struct sctp_authkey *sca;
4226                         struct sctp_keyhead *shared_keys;
4227                         sctp_sharedkey_t *shared_key;
4228                         sctp_key_t *key = NULL;
4229                         size_t size;
4230
4231                         SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4232                         if (sca->sca_keylength == 0) {
4233                                 size = optsize - sizeof(struct sctp_authkey);
4234                         } else {
4235                                 if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4236                                         size = sca->sca_keylength;
4237                                 } else {
4238                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4239                                         error = EINVAL;
4240                                         break;
4241                                 }
4242                         }
4243                         SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4244
4245                         if (stcb) {
4246                                 shared_keys = &stcb->asoc.shared_keys;
4247                                 /* clear the cached keys for this key id */
4248                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4249                                 /*
4250                                  * create the new shared key and
4251                                  * insert/replace it
4252                                  */
4253                                 if (size > 0) {
4254                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
4255                                         if (key == NULL) {
4256                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4257                                                 error = ENOMEM;
4258                                                 SCTP_TCB_UNLOCK(stcb);
4259                                                 break;
4260                                         }
4261                                 }
4262                                 shared_key = sctp_alloc_sharedkey();
4263                                 if (shared_key == NULL) {
4264                                         sctp_free_key(key);
4265                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4266                                         error = ENOMEM;
4267                                         SCTP_TCB_UNLOCK(stcb);
4268                                         break;
4269                                 }
4270                                 shared_key->key = key;
4271                                 shared_key->keyid = sca->sca_keynumber;
4272                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4273                                 SCTP_TCB_UNLOCK(stcb);
4274                         } else {
4275                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4276                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4277                                     (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4278                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4279                                         SCTP_INP_WLOCK(inp);
4280                                         shared_keys = &inp->sctp_ep.shared_keys;
4281                                         /*
4282                                          * clear the cached keys on all
4283                                          * assocs for this key id
4284                                          */
4285                                         sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4286                                         /*
4287                                          * create the new shared key and
4288                                          * insert/replace it
4289                                          */
4290                                         if (size > 0) {
4291                                                 key = sctp_set_key(sca->sca_key, (uint32_t) size);
4292                                                 if (key == NULL) {
4293                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4294                                                         error = ENOMEM;
4295                                                         SCTP_INP_WUNLOCK(inp);
4296                                                         break;
4297                                                 }
4298                                         }
4299                                         shared_key = sctp_alloc_sharedkey();
4300                                         if (shared_key == NULL) {
4301                                                 sctp_free_key(key);
4302                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4303                                                 error = ENOMEM;
4304                                                 SCTP_INP_WUNLOCK(inp);
4305                                                 break;
4306                                         }
4307                                         shared_key->key = key;
4308                                         shared_key->keyid = sca->sca_keynumber;
4309                                         error = sctp_insert_sharedkey(shared_keys, shared_key);
4310                                         SCTP_INP_WUNLOCK(inp);
4311                                 }
4312                                 if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4313                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4314                                         SCTP_INP_RLOCK(inp);
4315                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4316                                                 SCTP_TCB_LOCK(stcb);
4317                                                 shared_keys = &stcb->asoc.shared_keys;
4318                                                 /*
4319                                                  * clear the cached keys for
4320                                                  * this key id
4321                                                  */
4322                                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4323                                                 /*
4324                                                  * create the new shared key
4325                                                  * and insert/replace it
4326                                                  */
4327                                                 if (size > 0) {
4328                                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
4329                                                         if (key == NULL) {
4330                                                                 SCTP_TCB_UNLOCK(stcb);
4331                                                                 continue;
4332                                                         }
4333                                                 }
4334                                                 shared_key = sctp_alloc_sharedkey();
4335                                                 if (shared_key == NULL) {
4336                                                         sctp_free_key(key);
4337                                                         SCTP_TCB_UNLOCK(stcb);
4338                                                         continue;
4339                                                 }
4340                                                 shared_key->key = key;
4341                                                 shared_key->keyid = sca->sca_keynumber;
4342                                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4343                                                 SCTP_TCB_UNLOCK(stcb);
4344                                         }
4345                                         SCTP_INP_RUNLOCK(inp);
4346                                 }
4347                         }
4348                         break;
4349                 }
4350         case SCTP_HMAC_IDENT:
4351                 {
4352                         struct sctp_hmacalgo *shmac;
4353                         sctp_hmaclist_t *hmaclist;
4354                         uint16_t hmacid;
4355                         uint32_t i;
4356
4357                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4358                         if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
4359                             (shmac->shmac_number_of_idents > 0xffff)) {
4360                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4361                                 error = EINVAL;
4362                                 break;
4363                         }
4364                         hmaclist = sctp_alloc_hmaclist((uint16_t) shmac->shmac_number_of_idents);
4365                         if (hmaclist == NULL) {
4366                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4367                                 error = ENOMEM;
4368                                 break;
4369                         }
4370                         for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4371                                 hmacid = shmac->shmac_idents[i];
4372                                 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4373                                          /* invalid HMACs were found */ ;
4374                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4375                                         error = EINVAL;
4376                                         sctp_free_hmaclist(hmaclist);
4377                                         goto sctp_set_hmac_done;
4378                                 }
4379                         }
4380                         for (i = 0; i < hmaclist->num_algo; i++) {
4381                                 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4382                                         /* already in list */
4383                                         break;
4384                                 }
4385                         }
4386                         if (i == hmaclist->num_algo) {
4387                                 /* not found in list */
4388                                 sctp_free_hmaclist(hmaclist);
4389                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4390                                 error = EINVAL;
4391                                 break;
4392                         }
4393                         /* set it on the endpoint */
4394                         SCTP_INP_WLOCK(inp);
4395                         if (inp->sctp_ep.local_hmacs)
4396                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4397                         inp->sctp_ep.local_hmacs = hmaclist;
4398                         SCTP_INP_WUNLOCK(inp);
4399         sctp_set_hmac_done:
4400                         break;
4401                 }
4402         case SCTP_AUTH_ACTIVE_KEY:
4403                 {
4404                         struct sctp_authkeyid *scact;
4405
4406                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4407                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4408
4409                         /* set the active key on the right place */
4410                         if (stcb) {
4411                                 /* set the active key on the assoc */
4412                                 if (sctp_auth_setactivekey(stcb,
4413                                     scact->scact_keynumber)) {
4414                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4415                                             SCTP_FROM_SCTP_USRREQ,
4416                                             EINVAL);
4417                                         error = EINVAL;
4418                                 }
4419                                 SCTP_TCB_UNLOCK(stcb);
4420                         } else {
4421                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4422                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4423                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4424                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4425                                         SCTP_INP_WLOCK(inp);
4426                                         if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4427                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4428                                                 error = EINVAL;
4429                                         }
4430                                         SCTP_INP_WUNLOCK(inp);
4431                                 }
4432                                 if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4433                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4434                                         SCTP_INP_RLOCK(inp);
4435                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4436                                                 SCTP_TCB_LOCK(stcb);
4437                                                 sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4438                                                 SCTP_TCB_UNLOCK(stcb);
4439                                         }
4440                                         SCTP_INP_RUNLOCK(inp);
4441                                 }
4442                         }
4443                         break;
4444                 }
4445         case SCTP_AUTH_DELETE_KEY:
4446                 {
4447                         struct sctp_authkeyid *scdel;
4448
4449                         SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4450                         SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4451
4452                         /* delete the key from the right place */
4453                         if (stcb) {
4454                                 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4455                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4456                                         error = EINVAL;
4457                                 }
4458                                 SCTP_TCB_UNLOCK(stcb);
4459                         } else {
4460                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4461                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4462                                     (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4463                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4464                                         SCTP_INP_WLOCK(inp);
4465                                         if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4466                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4467                                                 error = EINVAL;
4468                                         }
4469                                         SCTP_INP_WUNLOCK(inp);
4470                                 }
4471                                 if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4472                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4473                                         SCTP_INP_RLOCK(inp);
4474                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4475                                                 SCTP_TCB_LOCK(stcb);
4476                                                 sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4477                                                 SCTP_TCB_UNLOCK(stcb);
4478                                         }
4479                                         SCTP_INP_RUNLOCK(inp);
4480                                 }
4481                         }
4482                         break;
4483                 }
4484         case SCTP_AUTH_DEACTIVATE_KEY:
4485                 {
4486                         struct sctp_authkeyid *keyid;
4487
4488                         SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4489                         SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4490
4491                         /* deactivate the key from the right place */
4492                         if (stcb) {
4493                                 if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4494                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4495                                         error = EINVAL;
4496                                 }
4497                                 SCTP_TCB_UNLOCK(stcb);
4498                         } else {
4499                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4500                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4501                                     (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4502                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4503                                         SCTP_INP_WLOCK(inp);
4504                                         if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4505                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4506                                                 error = EINVAL;
4507                                         }
4508                                         SCTP_INP_WUNLOCK(inp);
4509                                 }
4510                                 if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4511                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4512                                         SCTP_INP_RLOCK(inp);
4513                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4514                                                 SCTP_TCB_LOCK(stcb);
4515                                                 sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4516                                                 SCTP_TCB_UNLOCK(stcb);
4517                                         }
4518                                         SCTP_INP_RUNLOCK(inp);
4519                                 }
4520                         }
4521                         break;
4522                 }
4523         case SCTP_ENABLE_STREAM_RESET:
4524                 {
4525                         struct sctp_assoc_value *av;
4526
4527                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4528                         if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4529                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4530                                 error = EINVAL;
4531                                 break;
4532                         }
4533                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4534                         if (stcb) {
4535                                 stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4536                                 SCTP_TCB_UNLOCK(stcb);
4537                         } else {
4538                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4539                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4540                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4541                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4542                                         SCTP_INP_WLOCK(inp);
4543                                         inp->local_strreset_support = (uint8_t) av->assoc_value;
4544                                         SCTP_INP_WUNLOCK(inp);
4545                                 }
4546                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4547                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4548                                         SCTP_INP_RLOCK(inp);
4549                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4550                                                 SCTP_TCB_LOCK(stcb);
4551                                                 stcb->asoc.local_strreset_support = (uint8_t) av->assoc_value;
4552                                                 SCTP_TCB_UNLOCK(stcb);
4553                                         }
4554                                         SCTP_INP_RUNLOCK(inp);
4555                                 }
4556                         }
4557                         break;
4558                 }
4559         case SCTP_RESET_STREAMS:
4560                 {
4561                         struct sctp_reset_streams *strrst;
4562                         int i, send_out = 0;
4563                         int send_in = 0;
4564
4565                         SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4566                         SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4567                         if (stcb == NULL) {
4568                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4569                                 error = ENOENT;
4570                                 break;
4571                         }
4572                         if (stcb->asoc.reconfig_supported == 0) {
4573                                 /*
4574                                  * Peer does not support the chunk type.
4575                                  */
4576                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4577                                 error = EOPNOTSUPP;
4578                                 SCTP_TCB_UNLOCK(stcb);
4579                                 break;
4580                         }
4581                         if (sizeof(struct sctp_reset_streams) +
4582                             strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
4583                                 error = EINVAL;
4584                                 SCTP_TCB_UNLOCK(stcb);
4585                                 break;
4586                         }
4587                         if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4588                                 send_in = 1;
4589                                 if (stcb->asoc.stream_reset_outstanding) {
4590                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4591                                         error = EALREADY;
4592                                         SCTP_TCB_UNLOCK(stcb);
4593                                         break;
4594                                 }
4595                         }
4596                         if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4597                                 send_out = 1;
4598                         }
4599                         if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
4600                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4601                                 error = ENOMEM;
4602                                 SCTP_TCB_UNLOCK(stcb);
4603                                 break;
4604                         }
4605                         if ((send_in == 0) && (send_out == 0)) {
4606                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4607                                 error = EINVAL;
4608                                 SCTP_TCB_UNLOCK(stcb);
4609                                 break;
4610                         }
4611                         for (i = 0; i < strrst->srs_number_streams; i++) {
4612                                 if ((send_in) &&
4613                                     (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
4614                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4615                                         error = EINVAL;
4616                                         break;
4617                                 }
4618                                 if ((send_out) &&
4619                                     (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
4620                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4621                                         error = EINVAL;
4622                                         break;
4623                                 }
4624                         }
4625                         if (error) {
4626                                 SCTP_TCB_UNLOCK(stcb);
4627                                 break;
4628                         }
4629                         if (send_out) {
4630                                 int cnt;
4631                                 uint16_t strm;
4632
4633                                 if (strrst->srs_number_streams) {
4634                                         for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
4635                                                 strm = strrst->srs_stream_list[i];
4636                                                 if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
4637                                                         stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
4638                                                         cnt++;
4639                                                 }
4640                                         }
4641                                 } else {
4642                                         /* Its all */
4643                                         for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
4644                                                 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
4645                                                         stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
4646                                                         cnt++;
4647                                                 }
4648                                         }
4649                                 }
4650                         }
4651                         if (send_in) {
4652                                 error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4653                                     strrst->srs_stream_list,
4654                                     send_in, 0, 0, 0, 0, 0);
4655                         } else {
4656                                 error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
4657                         }
4658                         if (error == 0) {
4659                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4660                         } else {
4661                                 /*
4662                                  * For outgoing streams don't report any
4663                                  * problems in sending the request to the
4664                                  * application. XXX: Double check resetting
4665                                  * incoming streams.
4666                                  */
4667                                 error = 0;
4668                         }
4669                         SCTP_TCB_UNLOCK(stcb);
4670                         break;
4671                 }
4672         case SCTP_ADD_STREAMS:
4673                 {
4674                         struct sctp_add_streams *stradd;
4675                         uint8_t addstream = 0;
4676                         uint16_t add_o_strmcnt = 0;
4677                         uint16_t add_i_strmcnt = 0;
4678
4679                         SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4680                         SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4681                         if (stcb == NULL) {
4682                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4683                                 error = ENOENT;
4684                                 break;
4685                         }
4686                         if (stcb->asoc.reconfig_supported == 0) {
4687                                 /*
4688                                  * Peer does not support the chunk type.
4689                                  */
4690                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4691                                 error = EOPNOTSUPP;
4692                                 SCTP_TCB_UNLOCK(stcb);
4693                                 break;
4694                         }
4695                         if (stcb->asoc.stream_reset_outstanding) {
4696                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4697                                 error = EALREADY;
4698                                 SCTP_TCB_UNLOCK(stcb);
4699                                 break;
4700                         }
4701                         if ((stradd->sas_outstrms == 0) &&
4702                             (stradd->sas_instrms == 0)) {
4703                                 error = EINVAL;
4704                                 goto skip_stuff;
4705                         }
4706                         if (stradd->sas_outstrms) {
4707                                 addstream = 1;
4708                                 /* We allocate here */
4709                                 add_o_strmcnt = stradd->sas_outstrms;
4710                                 if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4711                                         /* You can't have more than 64k */
4712                                         error = EINVAL;
4713                                         goto skip_stuff;
4714                                 }
4715                         }
4716                         if (stradd->sas_instrms) {
4717                                 int cnt;
4718
4719                                 addstream |= 2;
4720                                 /*
4721                                  * We allocate inside
4722                                  * sctp_send_str_reset_req()
4723                                  */
4724                                 add_i_strmcnt = stradd->sas_instrms;
4725                                 cnt = add_i_strmcnt;
4726                                 cnt += stcb->asoc.streamincnt;
4727                                 if (cnt > 0x0000ffff) {
4728                                         /* You can't have more than 64k */
4729                                         error = EINVAL;
4730                                         goto skip_stuff;
4731                                 }
4732                                 if (cnt > (int)stcb->asoc.max_inbound_streams) {
4733                                         /* More than you are allowed */
4734                                         error = EINVAL;
4735                                         goto skip_stuff;
4736                                 }
4737                         }
4738                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4739                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4740         skip_stuff:
4741                         SCTP_TCB_UNLOCK(stcb);
4742                         break;
4743                 }
4744         case SCTP_RESET_ASSOC:
4745                 {
4746                         int i;
4747                         uint32_t *value;
4748
4749                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4750                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
4751                         if (stcb == NULL) {
4752                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4753                                 error = ENOENT;
4754                                 break;
4755                         }
4756                         if (stcb->asoc.reconfig_supported == 0) {
4757                                 /*
4758                                  * Peer does not support the chunk type.
4759                                  */
4760                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4761                                 error = EOPNOTSUPP;
4762                                 SCTP_TCB_UNLOCK(stcb);
4763                                 break;
4764                         }
4765                         if (stcb->asoc.stream_reset_outstanding) {
4766                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4767                                 error = EALREADY;
4768                                 SCTP_TCB_UNLOCK(stcb);
4769                                 break;
4770                         }
4771                         /*
4772                          * Is there any data pending in the send or sent
4773                          * queues?
4774                          */
4775                         if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
4776                             !TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4777                 busy_out:
4778                                 error = EBUSY;
4779                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4780                                 SCTP_TCB_UNLOCK(stcb);
4781                                 break;
4782                         }
4783                         /* Do any streams have data queued? */
4784                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4785                                 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
4786                                         goto busy_out;
4787                                 }
4788                         }
4789                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
4790                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4791                         SCTP_TCB_UNLOCK(stcb);
4792                         break;
4793                 }
4794         case SCTP_CONNECT_X:
4795                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4796                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4797                         error = EINVAL;
4798                         break;
4799                 }
4800                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4801                 break;
4802         case SCTP_CONNECT_X_DELAYED:
4803                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4804                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4805                         error = EINVAL;
4806                         break;
4807                 }
4808                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4809                 break;
4810         case SCTP_CONNECT_X_COMPLETE:
4811                 {
4812                         struct sockaddr *sa;
4813
4814                         /* FIXME MT: check correct? */
4815                         SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4816
4817                         /* find tcb */
4818                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4819                                 SCTP_INP_RLOCK(inp);
4820                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4821                                 if (stcb) {
4822                                         SCTP_TCB_LOCK(stcb);
4823                                 }
4824                                 SCTP_INP_RUNLOCK(inp);
4825                         } else {
4826                                 /*
4827                                  * We increment here since
4828                                  * sctp_findassociation_ep_addr() wil do a
4829                                  * decrement if it finds the stcb as long as
4830                                  * the locked tcb (last argument) is NOT a
4831                                  * TCB.. aka NULL.
4832                                  */
4833                                 SCTP_INP_INCR_REF(inp);
4834                                 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
4835                                 if (stcb == NULL) {
4836                                         SCTP_INP_DECR_REF(inp);
4837                                 }
4838                         }
4839
4840                         if (stcb == NULL) {
4841                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4842                                 error = ENOENT;
4843                                 break;
4844                         }
4845                         if (stcb->asoc.delayed_connection == 1) {
4846                                 stcb->asoc.delayed_connection = 0;
4847                                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4848                                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4849                                     stcb->asoc.primary_destination,
4850                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
4851                                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4852                         } else {
4853                                 /*
4854                                  * already expired or did not use delayed
4855                                  * connectx
4856                                  */
4857                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4858                                 error = EALREADY;
4859                         }
4860                         SCTP_TCB_UNLOCK(stcb);
4861                         break;
4862                 }
4863         case SCTP_MAX_BURST:
4864                 {
4865                         struct sctp_assoc_value *av;
4866
4867                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4868                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4869
4870                         if (stcb) {
4871                                 stcb->asoc.max_burst = av->assoc_value;
4872                                 SCTP_TCB_UNLOCK(stcb);
4873                         } else {
4874                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4875                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4876                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4877                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4878                                         SCTP_INP_WLOCK(inp);
4879                                         inp->sctp_ep.max_burst = av->assoc_value;
4880                                         SCTP_INP_WUNLOCK(inp);
4881                                 }
4882                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4883                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4884                                         SCTP_INP_RLOCK(inp);
4885                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4886                                                 SCTP_TCB_LOCK(stcb);
4887                                                 stcb->asoc.max_burst = av->assoc_value;
4888                                                 SCTP_TCB_UNLOCK(stcb);
4889                                         }
4890                                         SCTP_INP_RUNLOCK(inp);
4891                                 }
4892                         }
4893                         break;
4894                 }
4895         case SCTP_MAXSEG:
4896                 {
4897                         struct sctp_assoc_value *av;
4898                         int ovh;
4899
4900                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4901                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4902
4903                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4904                                 ovh = SCTP_MED_OVERHEAD;
4905                         } else {
4906                                 ovh = SCTP_MED_V4_OVERHEAD;
4907                         }
4908                         if (stcb) {
4909                                 if (av->assoc_value) {
4910                                         stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4911                                 } else {
4912                                         stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4913                                 }
4914                                 SCTP_TCB_UNLOCK(stcb);
4915                         } else {
4916                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4917                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4918                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4919                                         SCTP_INP_WLOCK(inp);
4920                                         /*
4921                                          * FIXME MT: I think this is not in
4922                                          * tune with the API ID
4923                                          */
4924                                         if (av->assoc_value) {
4925                                                 inp->sctp_frag_point = (av->assoc_value + ovh);
4926                                         } else {
4927                                                 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4928                                         }
4929                                         SCTP_INP_WUNLOCK(inp);
4930                                 } else {
4931                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4932                                         error = EINVAL;
4933                                 }
4934                         }
4935                         break;
4936                 }
4937         case SCTP_EVENTS:
4938                 {
4939                         struct sctp_event_subscribe *events;
4940
4941                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
4942
4943                         SCTP_INP_WLOCK(inp);
4944                         if (events->sctp_data_io_event) {
4945                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4946                         } else {
4947                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4948                         }
4949
4950                         if (events->sctp_association_event) {
4951                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4952                         } else {
4953                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4954                         }
4955
4956                         if (events->sctp_address_event) {
4957                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4958                         } else {
4959                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4960                         }
4961
4962                         if (events->sctp_send_failure_event) {
4963                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4964                         } else {
4965                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4966                         }
4967
4968                         if (events->sctp_peer_error_event) {
4969                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4970                         } else {
4971                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4972                         }
4973
4974                         if (events->sctp_shutdown_event) {
4975                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4976                         } else {
4977                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4978                         }
4979
4980                         if (events->sctp_partial_delivery_event) {
4981                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4982                         } else {
4983                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4984                         }
4985
4986                         if (events->sctp_adaptation_layer_event) {
4987                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4988                         } else {
4989                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4990                         }
4991
4992                         if (events->sctp_authentication_event) {
4993                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4994                         } else {
4995                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4996                         }
4997
4998                         if (events->sctp_sender_dry_event) {
4999                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
5000                         } else {
5001                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5002                         }
5003
5004                         if (events->sctp_stream_reset_event) {
5005                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5006                         } else {
5007                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5008                         }
5009                         SCTP_INP_WUNLOCK(inp);
5010
5011                         SCTP_INP_RLOCK(inp);
5012                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5013                                 SCTP_TCB_LOCK(stcb);
5014                                 if (events->sctp_association_event) {
5015                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5016                                 } else {
5017                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5018                                 }
5019                                 if (events->sctp_address_event) {
5020                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5021                                 } else {
5022                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5023                                 }
5024                                 if (events->sctp_send_failure_event) {
5025                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5026                                 } else {
5027                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5028                                 }
5029                                 if (events->sctp_peer_error_event) {
5030                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5031                                 } else {
5032                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5033                                 }
5034                                 if (events->sctp_shutdown_event) {
5035                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5036                                 } else {
5037                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5038                                 }
5039                                 if (events->sctp_partial_delivery_event) {
5040                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5041                                 } else {
5042                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5043                                 }
5044                                 if (events->sctp_adaptation_layer_event) {
5045                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5046                                 } else {
5047                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5048                                 }
5049                                 if (events->sctp_authentication_event) {
5050                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5051                                 } else {
5052                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5053                                 }
5054                                 if (events->sctp_sender_dry_event) {
5055                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5056                                 } else {
5057                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5058                                 }
5059                                 if (events->sctp_stream_reset_event) {
5060                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5061                                 } else {
5062                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5063                                 }
5064                                 SCTP_TCB_UNLOCK(stcb);
5065                         }
5066                         /*
5067                          * Send up the sender dry event only for 1-to-1
5068                          * style sockets.
5069                          */
5070                         if (events->sctp_sender_dry_event) {
5071                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5072                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5073                                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
5074                                         if (stcb) {
5075                                                 SCTP_TCB_LOCK(stcb);
5076                                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5077                                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5078                                                     (stcb->asoc.stream_queue_cnt == 0)) {
5079                                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5080                                                 }
5081                                                 SCTP_TCB_UNLOCK(stcb);
5082                                         }
5083                                 }
5084                         }
5085                         SCTP_INP_RUNLOCK(inp);
5086                         break;
5087                 }
5088         case SCTP_ADAPTATION_LAYER:
5089                 {
5090                         struct sctp_setadaptation *adap_bits;
5091
5092                         SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5093                         SCTP_INP_WLOCK(inp);
5094                         inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5095                         inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5096                         SCTP_INP_WUNLOCK(inp);
5097                         break;
5098                 }
5099 #ifdef SCTP_DEBUG
5100         case SCTP_SET_INITIAL_DBG_SEQ:
5101                 {
5102                         uint32_t *vvv;
5103
5104                         SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5105                         SCTP_INP_WLOCK(inp);
5106                         inp->sctp_ep.initial_sequence_debug = *vvv;
5107                         SCTP_INP_WUNLOCK(inp);
5108                         break;
5109                 }
5110 #endif
5111         case SCTP_DEFAULT_SEND_PARAM:
5112                 {
5113                         struct sctp_sndrcvinfo *s_info;
5114
5115                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5116                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5117
5118                         if (stcb) {
5119                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5120                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5121                                 } else {
5122                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5123                                         error = EINVAL;
5124                                 }
5125                                 SCTP_TCB_UNLOCK(stcb);
5126                         } else {
5127                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5128                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5129                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5130                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5131                                         SCTP_INP_WLOCK(inp);
5132                                         memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5133                                         SCTP_INP_WUNLOCK(inp);
5134                                 }
5135                                 if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5136                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5137                                         SCTP_INP_RLOCK(inp);
5138                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5139                                                 SCTP_TCB_LOCK(stcb);
5140                                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5141                                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5142                                                 }
5143                                                 SCTP_TCB_UNLOCK(stcb);
5144                                         }
5145                                         SCTP_INP_RUNLOCK(inp);
5146                                 }
5147                         }
5148                         break;
5149                 }
5150         case SCTP_PEER_ADDR_PARAMS:
5151                 {
5152                         struct sctp_paddrparams *paddrp;
5153                         struct sctp_nets *net;
5154                         struct sockaddr *addr;
5155
5156 #if defined(INET) && defined(INET6)
5157                         struct sockaddr_in sin_store;
5158
5159 #endif
5160
5161                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5162                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5163
5164 #if defined(INET) && defined(INET6)
5165                         if (paddrp->spp_address.ss_family == AF_INET6) {
5166                                 struct sockaddr_in6 *sin6;
5167
5168                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5169                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5170                                         in6_sin6_2_sin(&sin_store, sin6);
5171                                         addr = (struct sockaddr *)&sin_store;
5172                                 } else {
5173                                         addr = (struct sockaddr *)&paddrp->spp_address;
5174                                 }
5175                         } else {
5176                                 addr = (struct sockaddr *)&paddrp->spp_address;
5177                         }
5178 #else
5179                         addr = (struct sockaddr *)&paddrp->spp_address;
5180 #endif
5181                         if (stcb != NULL) {
5182                                 net = sctp_findnet(stcb, addr);
5183                         } else {
5184                                 /*
5185                                  * We increment here since
5186                                  * sctp_findassociation_ep_addr() wil do a
5187                                  * decrement if it finds the stcb as long as
5188                                  * the locked tcb (last argument) is NOT a
5189                                  * TCB.. aka NULL.
5190                                  */
5191                                 net = NULL;
5192                                 SCTP_INP_INCR_REF(inp);
5193                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5194                                     &net, NULL, NULL);
5195                                 if (stcb == NULL) {
5196                                         SCTP_INP_DECR_REF(inp);
5197                                 }
5198                         }
5199                         if ((stcb != NULL) && (net == NULL)) {
5200 #ifdef INET
5201                                 if (addr->sa_family == AF_INET) {
5202
5203                                         struct sockaddr_in *sin;
5204
5205                                         sin = (struct sockaddr_in *)addr;
5206                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
5207                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5208                                                 SCTP_TCB_UNLOCK(stcb);
5209                                                 error = EINVAL;
5210                                                 break;
5211                                         }
5212                                 } else
5213 #endif
5214 #ifdef INET6
5215                                 if (addr->sa_family == AF_INET6) {
5216                                         struct sockaddr_in6 *sin6;
5217
5218                                         sin6 = (struct sockaddr_in6 *)addr;
5219                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5220                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5221                                                 SCTP_TCB_UNLOCK(stcb);
5222                                                 error = EINVAL;
5223                                                 break;
5224                                         }
5225                                 } else
5226 #endif
5227                                 {
5228                                         error = EAFNOSUPPORT;
5229                                         SCTP_TCB_UNLOCK(stcb);
5230                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5231                                         break;
5232                                 }
5233                         }
5234                         /* sanity checks */
5235                         if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5236                                 if (stcb)
5237                                         SCTP_TCB_UNLOCK(stcb);
5238                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5239                                 return (EINVAL);
5240                         }
5241                         if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5242                                 if (stcb)
5243                                         SCTP_TCB_UNLOCK(stcb);
5244                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5245                                 return (EINVAL);
5246                         }
5247                         if (stcb != NULL) {
5248                                 /************************TCB SPECIFIC SET ******************/
5249                                 if (net != NULL) {
5250                                         /************************NET SPECIFIC SET ******************/
5251                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5252                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5253                                                     !(net->dest_state & SCTP_ADDR_NOHB)) {
5254                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5255                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
5256                                                 }
5257                                                 net->dest_state |= SCTP_ADDR_NOHB;
5258                                         }
5259                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5260                                                 if (paddrp->spp_hbinterval) {
5261                                                         net->heart_beat_delay = paddrp->spp_hbinterval;
5262                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5263                                                         net->heart_beat_delay = 0;
5264                                                 }
5265                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5266                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5267                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5268                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5269                                         }
5270                                         if (paddrp->spp_flags & SPP_HB_DEMAND) {
5271                                                 /* on demand HB */
5272                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5273                                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5274                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5275                                         }
5276                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5277                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5278                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5279                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
5280                                                 }
5281                                                 net->dest_state |= SCTP_ADDR_NO_PMTUD;
5282                                                 net->mtu = paddrp->spp_pathmtu;
5283                                                 switch (net->ro._l_addr.sa.sa_family) {
5284 #ifdef INET
5285                                                 case AF_INET:
5286                                                         net->mtu += SCTP_MIN_V4_OVERHEAD;
5287                                                         break;
5288 #endif
5289 #ifdef INET6
5290                                                 case AF_INET6:
5291                                                         net->mtu += SCTP_MIN_OVERHEAD;
5292                                                         break;
5293 #endif
5294                                                 default:
5295                                                         break;
5296                                                 }
5297                                                 if (net->mtu < stcb->asoc.smallest_mtu) {
5298                                                         sctp_pathmtu_adjustment(stcb, net->mtu);
5299                                                 }
5300                                         }
5301                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5302                                                 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5303                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5304                                                 }
5305                                                 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5306                                         }
5307                                         if (paddrp->spp_pathmaxrxt) {
5308                                                 if (net->dest_state & SCTP_ADDR_PF) {
5309                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5310                                                                 net->dest_state &= ~SCTP_ADDR_PF;
5311                                                         }
5312                                                 } else {
5313                                                         if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5314                                                             (net->error_count > net->pf_threshold)) {
5315                                                                 net->dest_state |= SCTP_ADDR_PF;
5316                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5317                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5318                                                                     stcb->sctp_ep, stcb, net,
5319                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
5320                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5321                                                         }
5322                                                 }
5323                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
5324                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5325                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5326                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5327                                                         }
5328                                                 } else {
5329                                                         if (net->error_count <= paddrp->spp_pathmaxrxt) {
5330                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
5331                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5332                                                         }
5333                                                 }
5334                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
5335                                         }
5336                                         if (paddrp->spp_flags & SPP_DSCP) {
5337                                                 net->dscp = paddrp->spp_dscp & 0xfc;
5338                                                 net->dscp |= 0x01;
5339                                         }
5340 #ifdef INET6
5341                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5342                                                 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5343                                                         net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5344                                                         net->flowlabel |= 0x80000000;
5345                                                 }
5346                                         }
5347 #endif
5348                                 } else {
5349                                         /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5350                                         if (paddrp->spp_pathmaxrxt != 0) {
5351                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5352                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5353                                                         if (net->dest_state & SCTP_ADDR_PF) {
5354                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5355                                                                         net->dest_state &= ~SCTP_ADDR_PF;
5356                                                                 }
5357                                                         } else {
5358                                                                 if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5359                                                                     (net->error_count > net->pf_threshold)) {
5360                                                                         net->dest_state |= SCTP_ADDR_PF;
5361                                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5362                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5363                                                                             stcb->sctp_ep, stcb, net,
5364                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
5365                                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5366                                                                 }
5367                                                         }
5368                                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
5369                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5370                                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
5371                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5372                                                                 }
5373                                                         } else {
5374                                                                 if (net->error_count <= paddrp->spp_pathmaxrxt) {
5375                                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
5376                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5377                                                                 }
5378                                                         }
5379                                                         net->failure_threshold = paddrp->spp_pathmaxrxt;
5380                                                 }
5381                                         }
5382                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5383                                                 if (paddrp->spp_hbinterval != 0) {
5384                                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5385                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5386                                                         stcb->asoc.heart_beat_delay = 0;
5387                                                 }
5388                                                 /* Turn back on the timer */
5389                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5390                                                         if (paddrp->spp_hbinterval != 0) {
5391                                                                 net->heart_beat_delay = paddrp->spp_hbinterval;
5392                                                         } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5393                                                                 net->heart_beat_delay = 0;
5394                                                         }
5395                                                         if (net->dest_state & SCTP_ADDR_NOHB) {
5396                                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5397                                                         }
5398                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5399                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
5400                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5401                                                 }
5402                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5403                                         }
5404                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5405                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5406                                                         if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5407                                                                 net->dest_state |= SCTP_ADDR_NOHB;
5408                                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5409                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5410                                                                             inp, stcb, net,
5411                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
5412                                                                 }
5413                                                         }
5414                                                 }
5415                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5416                                         }
5417                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5418                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5419                                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5420                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5421                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
5422                                                         }
5423                                                         net->dest_state |= SCTP_ADDR_NO_PMTUD;
5424                                                         net->mtu = paddrp->spp_pathmtu;
5425                                                         switch (net->ro._l_addr.sa.sa_family) {
5426 #ifdef INET
5427                                                         case AF_INET:
5428                                                                 net->mtu += SCTP_MIN_V4_OVERHEAD;
5429                                                                 break;
5430 #endif
5431 #ifdef INET6
5432                                                         case AF_INET6:
5433                                                                 net->mtu += SCTP_MIN_OVERHEAD;
5434                                                                 break;
5435 #endif
5436                                                         default:
5437                                                                 break;
5438                                                         }
5439                                                         if (net->mtu < stcb->asoc.smallest_mtu) {
5440                                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5441                                                         }
5442                                                 }
5443                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5444                                         }
5445                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5446                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5447                                                         if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5448                                                                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5449                                                         }
5450                                                         net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5451                                                 }
5452                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5453                                         }
5454                                         if (paddrp->spp_flags & SPP_DSCP) {
5455                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5456                                                         net->dscp = paddrp->spp_dscp & 0xfc;
5457                                                         net->dscp |= 0x01;
5458                                                 }
5459                                                 stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5460                                                 stcb->asoc.default_dscp |= 0x01;
5461                                         }
5462 #ifdef INET6
5463                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5464                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5465                                                         if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5466                                                                 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5467                                                                 net->flowlabel |= 0x80000000;
5468                                                         }
5469                                                 }
5470                                                 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5471                                                 stcb->asoc.default_flowlabel |= 0x80000000;
5472                                         }
5473 #endif
5474                                 }
5475                                 SCTP_TCB_UNLOCK(stcb);
5476                         } else {
5477                                 /************************NO TCB, SET TO default stuff ******************/
5478                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5479                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5480                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5481                                         SCTP_INP_WLOCK(inp);
5482                                         /*
5483                                          * For the TOS/FLOWLABEL stuff you
5484                                          * set it with the options on the
5485                                          * socket
5486                                          */
5487                                         if (paddrp->spp_pathmaxrxt != 0) {
5488                                                 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5489                                         }
5490                                         if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5491                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5492                                         else if (paddrp->spp_hbinterval != 0) {
5493                                                 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5494                                                         paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5495                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5496                                         }
5497                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5498                                                 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5499                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5500                                                 } else if (paddrp->spp_hbinterval) {
5501                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5502                                                 }
5503                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5504                                         } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5505                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5506                                         }
5507                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5508                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5509                                         } else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5510                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5511                                         }
5512                                         if (paddrp->spp_flags & SPP_DSCP) {
5513                                                 inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5514                                                 inp->sctp_ep.default_dscp |= 0x01;
5515                                         }
5516 #ifdef INET6
5517                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5518                                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5519                                                         inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5520                                                         inp->sctp_ep.default_flowlabel |= 0x80000000;
5521                                                 }
5522                                         }
5523 #endif
5524                                         SCTP_INP_WUNLOCK(inp);
5525                                 } else {
5526                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5527                                         error = EINVAL;
5528                                 }
5529                         }
5530                         break;
5531                 }
5532         case SCTP_RTOINFO:
5533                 {
5534                         struct sctp_rtoinfo *srto;
5535                         uint32_t new_init, new_min, new_max;
5536
5537                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5538                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5539
5540                         if (stcb) {
5541                                 if (srto->srto_initial)
5542                                         new_init = srto->srto_initial;
5543                                 else
5544                                         new_init = stcb->asoc.initial_rto;
5545                                 if (srto->srto_max)
5546                                         new_max = srto->srto_max;
5547                                 else
5548                                         new_max = stcb->asoc.maxrto;
5549                                 if (srto->srto_min)
5550                                         new_min = srto->srto_min;
5551                                 else
5552                                         new_min = stcb->asoc.minrto;
5553                                 if ((new_min <= new_init) && (new_init <= new_max)) {
5554                                         stcb->asoc.initial_rto = new_init;
5555                                         stcb->asoc.maxrto = new_max;
5556                                         stcb->asoc.minrto = new_min;
5557                                 } else {
5558                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5559                                         error = EINVAL;
5560                                 }
5561                                 SCTP_TCB_UNLOCK(stcb);
5562                         } else {
5563                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5564                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5565                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5566                                         SCTP_INP_WLOCK(inp);
5567                                         if (srto->srto_initial)
5568                                                 new_init = srto->srto_initial;
5569                                         else
5570                                                 new_init = inp->sctp_ep.initial_rto;
5571                                         if (srto->srto_max)
5572                                                 new_max = srto->srto_max;
5573                                         else
5574                                                 new_max = inp->sctp_ep.sctp_maxrto;
5575                                         if (srto->srto_min)
5576                                                 new_min = srto->srto_min;
5577                                         else
5578                                                 new_min = inp->sctp_ep.sctp_minrto;
5579                                         if ((new_min <= new_init) && (new_init <= new_max)) {
5580                                                 inp->sctp_ep.initial_rto = new_init;
5581                                                 inp->sctp_ep.sctp_maxrto = new_max;
5582                                                 inp->sctp_ep.sctp_minrto = new_min;
5583                                         } else {
5584                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5585                                                 error = EINVAL;
5586                                         }
5587                                         SCTP_INP_WUNLOCK(inp);
5588                                 } else {
5589                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5590                                         error = EINVAL;
5591                                 }
5592                         }
5593                         break;
5594                 }
5595         case SCTP_ASSOCINFO:
5596                 {
5597                         struct sctp_assocparams *sasoc;
5598
5599                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5600                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5601                         if (sasoc->sasoc_cookie_life) {
5602                                 /* boundary check the cookie life */
5603                                 if (sasoc->sasoc_cookie_life < 1000)
5604                                         sasoc->sasoc_cookie_life = 1000;
5605                                 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5606                                         sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5607                                 }
5608                         }
5609                         if (stcb) {
5610                                 if (sasoc->sasoc_asocmaxrxt)
5611                                         stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5612                                 if (sasoc->sasoc_cookie_life) {
5613                                         stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5614                                 }
5615                                 SCTP_TCB_UNLOCK(stcb);
5616                         } else {
5617                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5618                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5619                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5620                                         SCTP_INP_WLOCK(inp);
5621                                         if (sasoc->sasoc_asocmaxrxt)
5622                                                 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5623                                         if (sasoc->sasoc_cookie_life) {
5624                                                 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5625                                         }
5626                                         SCTP_INP_WUNLOCK(inp);
5627                                 } else {
5628                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5629                                         error = EINVAL;
5630                                 }
5631                         }
5632                         break;
5633                 }
5634         case SCTP_INITMSG:
5635                 {
5636                         struct sctp_initmsg *sinit;
5637
5638                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5639                         SCTP_INP_WLOCK(inp);
5640                         if (sinit->sinit_num_ostreams)
5641                                 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5642
5643                         if (sinit->sinit_max_instreams)
5644                                 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5645
5646                         if (sinit->sinit_max_attempts)
5647                                 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5648
5649                         if (sinit->sinit_max_init_timeo)
5650                                 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5651                         SCTP_INP_WUNLOCK(inp);
5652                         break;
5653                 }
5654         case SCTP_PRIMARY_ADDR:
5655                 {
5656                         struct sctp_setprim *spa;
5657                         struct sctp_nets *net;
5658                         struct sockaddr *addr;
5659
5660 #if defined(INET) && defined(INET6)
5661                         struct sockaddr_in sin_store;
5662
5663 #endif
5664
5665                         SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5666                         SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5667
5668 #if defined(INET) && defined(INET6)
5669                         if (spa->ssp_addr.ss_family == AF_INET6) {
5670                                 struct sockaddr_in6 *sin6;
5671
5672                                 sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5673                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5674                                         in6_sin6_2_sin(&sin_store, sin6);
5675                                         addr = (struct sockaddr *)&sin_store;
5676                                 } else {
5677                                         addr = (struct sockaddr *)&spa->ssp_addr;
5678                                 }
5679                         } else {
5680                                 addr = (struct sockaddr *)&spa->ssp_addr;
5681                         }
5682 #else
5683                         addr = (struct sockaddr *)&spa->ssp_addr;
5684 #endif
5685                         if (stcb != NULL) {
5686                                 net = sctp_findnet(stcb, addr);
5687                         } else {
5688                                 /*
5689                                  * We increment here since
5690                                  * sctp_findassociation_ep_addr() wil do a
5691                                  * decrement if it finds the stcb as long as
5692                                  * the locked tcb (last argument) is NOT a
5693                                  * TCB.. aka NULL.
5694                                  */
5695                                 net = NULL;
5696                                 SCTP_INP_INCR_REF(inp);
5697                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5698                                     &net, NULL, NULL);
5699                                 if (stcb == NULL) {
5700                                         SCTP_INP_DECR_REF(inp);
5701                                 }
5702                         }
5703
5704                         if ((stcb != NULL) && (net != NULL)) {
5705                                 if (net != stcb->asoc.primary_destination) {
5706                                         if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5707                                                 /* Ok we need to set it */
5708                                                 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5709                                                         if ((stcb->asoc.alternate) &&
5710                                                             (!(net->dest_state & SCTP_ADDR_PF)) &&
5711                                                             (net->dest_state & SCTP_ADDR_REACHABLE)) {
5712                                                                 sctp_free_remote_addr(stcb->asoc.alternate);
5713                                                                 stcb->asoc.alternate = NULL;
5714                                                         }
5715                                                 } else {
5716                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5717                                                         error = EINVAL;
5718                                                 }
5719                                         } else {
5720                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5721                                                 error = EINVAL;
5722                                         }
5723                                 }
5724                         } else {
5725                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5726                                 error = EINVAL;
5727                         }
5728                         if (stcb != NULL) {
5729                                 SCTP_TCB_UNLOCK(stcb);
5730                         }
5731                         break;
5732                 }
5733         case SCTP_SET_DYNAMIC_PRIMARY:
5734                 {
5735                         union sctp_sockstore *ss;
5736
5737                         error = priv_check(curthread,
5738                             PRIV_NETINET_RESERVEDPORT);
5739                         if (error)
5740                                 break;
5741
5742                         SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5743                         /* SUPER USER CHECK? */
5744                         error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5745                         break;
5746                 }
5747         case SCTP_SET_PEER_PRIMARY_ADDR:
5748                 {
5749                         struct sctp_setpeerprim *sspp;
5750                         struct sockaddr *addr;
5751
5752 #if defined(INET) && defined(INET6)
5753                         struct sockaddr_in sin_store;
5754
5755 #endif
5756
5757                         SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5758                         SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5759                         if (stcb != NULL) {
5760                                 struct sctp_ifa *ifa;
5761
5762 #if defined(INET) && defined(INET6)
5763                                 if (sspp->sspp_addr.ss_family == AF_INET6) {
5764                                         struct sockaddr_in6 *sin6;
5765
5766                                         sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5767                                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5768                                                 in6_sin6_2_sin(&sin_store, sin6);
5769                                                 addr = (struct sockaddr *)&sin_store;
5770                                         } else {
5771                                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5772                                         }
5773                                 } else {
5774                                         addr = (struct sockaddr *)&sspp->sspp_addr;
5775                                 }
5776 #else
5777                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5778 #endif
5779                                 ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5780                                 if (ifa == NULL) {
5781                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5782                                         error = EINVAL;
5783                                         goto out_of_it;
5784                                 }
5785                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5786                                         /*
5787                                          * Must validate the ifa found is in
5788                                          * our ep
5789                                          */
5790                                         struct sctp_laddr *laddr;
5791                                         int found = 0;
5792
5793                                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5794                                                 if (laddr->ifa == NULL) {
5795                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5796                                                             __func__);
5797                                                         continue;
5798                                                 }
5799                                                 if (laddr->ifa == ifa) {
5800                                                         found = 1;
5801                                                         break;
5802                                                 }
5803                                         }
5804                                         if (!found) {
5805                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5806                                                 error = EINVAL;
5807                                                 goto out_of_it;
5808                                         }
5809                                 } else {
5810                                         switch (addr->sa_family) {
5811 #ifdef INET
5812                                         case AF_INET:
5813                                                 {
5814                                                         struct sockaddr_in *sin;
5815
5816                                                         sin = (struct sockaddr_in *)addr;
5817                                                         if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5818                                                             &sin->sin_addr) != 0) {
5819                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5820                                                                 error = EINVAL;
5821                                                                 goto out_of_it;
5822                                                         }
5823                                                         break;
5824                                                 }
5825 #endif
5826 #ifdef INET6
5827                                         case AF_INET6:
5828                                                 {
5829                                                         struct sockaddr_in6 *sin6;
5830
5831                                                         sin6 = (struct sockaddr_in6 *)addr;
5832                                                         if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5833                                                             &sin6->sin6_addr) != 0) {
5834                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5835                                                                 error = EINVAL;
5836                                                                 goto out_of_it;
5837                                                         }
5838                                                         break;
5839                                                 }
5840 #endif
5841                                         default:
5842                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5843                                                 error = EINVAL;
5844                                                 goto out_of_it;
5845                                         }
5846                                 }
5847                                 if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5848                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5849                                         error = EINVAL;
5850                                 }
5851                 out_of_it:
5852                                 SCTP_TCB_UNLOCK(stcb);
5853                         } else {
5854                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5855                                 error = EINVAL;
5856                         }
5857                         break;
5858                 }
5859         case SCTP_BINDX_ADD_ADDR:
5860                 {
5861                         struct sctp_getaddresses *addrs;
5862                         struct thread *td;
5863
5864                         td = (struct thread *)p;
5865                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5866                             optsize);
5867 #ifdef INET
5868                         if (addrs->addr->sa_family == AF_INET) {
5869                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5870                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5871                                         error = EINVAL;
5872                                         break;
5873                                 }
5874                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5875                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5876                                         break;
5877                                 }
5878                         } else
5879 #endif
5880 #ifdef INET6
5881                         if (addrs->addr->sa_family == AF_INET6) {
5882                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5883                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5884                                         error = EINVAL;
5885                                         break;
5886                                 }
5887                                 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5888                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5889                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5890                                         break;
5891                                 }
5892                         } else
5893 #endif
5894                         {
5895                                 error = EAFNOSUPPORT;
5896                                 break;
5897                         }
5898                         sctp_bindx_add_address(so, inp, addrs->addr,
5899                             addrs->sget_assoc_id, vrf_id,
5900                             &error, p);
5901                         break;
5902                 }
5903         case SCTP_BINDX_REM_ADDR:
5904                 {
5905                         struct sctp_getaddresses *addrs;
5906                         struct thread *td;
5907
5908                         td = (struct thread *)p;
5909
5910                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5911 #ifdef INET
5912                         if (addrs->addr->sa_family == AF_INET) {
5913                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5914                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5915                                         error = EINVAL;
5916                                         break;
5917                                 }
5918                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5919                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5920                                         break;
5921                                 }
5922                         } else
5923 #endif
5924 #ifdef INET6
5925                         if (addrs->addr->sa_family == AF_INET6) {
5926                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5927                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5928                                         error = EINVAL;
5929                                         break;
5930                                 }
5931                                 if (td != NULL &&
5932                                     (error = prison_local_ip6(td->td_ucred,
5933                                     &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5934                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5935                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5936                                         break;
5937                                 }
5938                         } else
5939 #endif
5940                         {
5941                                 error = EAFNOSUPPORT;
5942                                 break;
5943                         }
5944                         sctp_bindx_delete_address(inp, addrs->addr,
5945                             addrs->sget_assoc_id, vrf_id,
5946                             &error);
5947                         break;
5948                 }
5949         case SCTP_EVENT:
5950                 {
5951                         struct sctp_event *event;
5952                         uint32_t event_type;
5953
5954                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
5955                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
5956                         switch (event->se_type) {
5957                         case SCTP_ASSOC_CHANGE:
5958                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
5959                                 break;
5960                         case SCTP_PEER_ADDR_CHANGE:
5961                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
5962                                 break;
5963                         case SCTP_REMOTE_ERROR:
5964                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
5965                                 break;
5966                         case SCTP_SEND_FAILED:
5967                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
5968                                 break;
5969                         case SCTP_SHUTDOWN_EVENT:
5970                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
5971                                 break;
5972                         case SCTP_ADAPTATION_INDICATION:
5973                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
5974                                 break;
5975                         case SCTP_PARTIAL_DELIVERY_EVENT:
5976                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
5977                                 break;
5978                         case SCTP_AUTHENTICATION_EVENT:
5979                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
5980                                 break;
5981                         case SCTP_STREAM_RESET_EVENT:
5982                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
5983                                 break;
5984                         case SCTP_SENDER_DRY_EVENT:
5985                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
5986                                 break;
5987                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
5988                                 event_type = 0;
5989                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5990                                 error = ENOTSUP;
5991                                 break;
5992                         case SCTP_ASSOC_RESET_EVENT:
5993                                 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
5994                                 break;
5995                         case SCTP_STREAM_CHANGE_EVENT:
5996                                 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
5997                                 break;
5998                         case SCTP_SEND_FAILED_EVENT:
5999                                 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
6000                                 break;
6001                         default:
6002                                 event_type = 0;
6003                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6004                                 error = EINVAL;
6005                                 break;
6006                         }
6007                         if (event_type > 0) {
6008                                 if (stcb) {
6009                                         if (event->se_on) {
6010                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6011                                                 if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6012                                                         if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6013                                                             TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6014                                                             (stcb->asoc.stream_queue_cnt == 0)) {
6015                                                                 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
6016                                                         }
6017                                                 }
6018                                         } else {
6019                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6020                                         }
6021                                         SCTP_TCB_UNLOCK(stcb);
6022                                 } else {
6023                                         /*
6024                                          * We don't want to send up a storm
6025                                          * of events, so return an error for
6026                                          * sender dry events
6027                                          */
6028                                         if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6029                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6030                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6031                                             ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6032                                             (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6033                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6034                                                 error = ENOTSUP;
6035                                                 break;
6036                                         }
6037                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6038                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6039                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6040                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6041                                                 SCTP_INP_WLOCK(inp);
6042                                                 if (event->se_on) {
6043                                                         sctp_feature_on(inp, event_type);
6044                                                 } else {
6045                                                         sctp_feature_off(inp, event_type);
6046                                                 }
6047                                                 SCTP_INP_WUNLOCK(inp);
6048                                         }
6049                                         if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6050                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6051                                                 SCTP_INP_RLOCK(inp);
6052                                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6053                                                         SCTP_TCB_LOCK(stcb);
6054                                                         if (event->se_on) {
6055                                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6056                                                         } else {
6057                                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6058                                                         }
6059                                                         SCTP_TCB_UNLOCK(stcb);
6060                                                 }
6061                                                 SCTP_INP_RUNLOCK(inp);
6062                                         }
6063                                 }
6064                         }
6065                         break;
6066                 }
6067         case SCTP_RECVRCVINFO:
6068                 {
6069                         int *onoff;
6070
6071                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6072                         SCTP_INP_WLOCK(inp);
6073                         if (*onoff != 0) {
6074                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6075                         } else {
6076                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6077                         }
6078                         SCTP_INP_WUNLOCK(inp);
6079                         break;
6080                 }
6081         case SCTP_RECVNXTINFO:
6082                 {
6083                         int *onoff;
6084
6085                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6086                         SCTP_INP_WLOCK(inp);
6087                         if (*onoff != 0) {
6088                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6089                         } else {
6090                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6091                         }
6092                         SCTP_INP_WUNLOCK(inp);
6093                         break;
6094                 }
6095         case SCTP_DEFAULT_SNDINFO:
6096                 {
6097                         struct sctp_sndinfo *info;
6098                         uint16_t policy;
6099
6100                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6101                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6102
6103                         if (stcb) {
6104                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6105                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6106                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6107                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6108                                         stcb->asoc.def_send.sinfo_flags |= policy;
6109                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6110                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6111                                 } else {
6112                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6113                                         error = EINVAL;
6114                                 }
6115                                 SCTP_TCB_UNLOCK(stcb);
6116                         } else {
6117                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6118                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6119                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6120                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6121                                         SCTP_INP_WLOCK(inp);
6122                                         inp->def_send.sinfo_stream = info->snd_sid;
6123                                         policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6124                                         inp->def_send.sinfo_flags = info->snd_flags;
6125                                         inp->def_send.sinfo_flags |= policy;
6126                                         inp->def_send.sinfo_ppid = info->snd_ppid;
6127                                         inp->def_send.sinfo_context = info->snd_context;
6128                                         SCTP_INP_WUNLOCK(inp);
6129                                 }
6130                                 if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6131                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6132                                         SCTP_INP_RLOCK(inp);
6133                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6134                                                 SCTP_TCB_LOCK(stcb);
6135                                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6136                                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6137                                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6138                                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6139                                                         stcb->asoc.def_send.sinfo_flags |= policy;
6140                                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6141                                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6142                                                 }
6143                                                 SCTP_TCB_UNLOCK(stcb);
6144                                         }
6145                                         SCTP_INP_RUNLOCK(inp);
6146                                 }
6147                         }
6148                         break;
6149                 }
6150         case SCTP_DEFAULT_PRINFO:
6151                 {
6152                         struct sctp_default_prinfo *info;
6153
6154                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6155                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6156
6157                         if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6158                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6159                                 error = EINVAL;
6160                                 break;
6161                         }
6162                         if (stcb) {
6163                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6164                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6165                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6166                                 SCTP_TCB_UNLOCK(stcb);
6167                         } else {
6168                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6169                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6170                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6171                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6172                                         SCTP_INP_WLOCK(inp);
6173                                         inp->def_send.sinfo_flags &= 0xfff0;
6174                                         inp->def_send.sinfo_flags |= info->pr_policy;
6175                                         inp->def_send.sinfo_timetolive = info->pr_value;
6176                                         SCTP_INP_WUNLOCK(inp);
6177                                 }
6178                                 if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6179                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6180                                         SCTP_INP_RLOCK(inp);
6181                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6182                                                 SCTP_TCB_LOCK(stcb);
6183                                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6184                                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6185                                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6186                                                 SCTP_TCB_UNLOCK(stcb);
6187                                         }
6188                                         SCTP_INP_RUNLOCK(inp);
6189                                 }
6190                         }
6191                         break;
6192                 }
6193         case SCTP_PEER_ADDR_THLDS:
6194                 /* Applies to the specific association */
6195                 {
6196                         struct sctp_paddrthlds *thlds;
6197                         struct sctp_nets *net;
6198                         struct sockaddr *addr;
6199
6200 #if defined(INET) && defined(INET6)
6201                         struct sockaddr_in sin_store;
6202
6203 #endif
6204
6205                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6206                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6207
6208 #if defined(INET) && defined(INET6)
6209                         if (thlds->spt_address.ss_family == AF_INET6) {
6210                                 struct sockaddr_in6 *sin6;
6211
6212                                 sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6213                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6214                                         in6_sin6_2_sin(&sin_store, sin6);
6215                                         addr = (struct sockaddr *)&sin_store;
6216                                 } else {
6217                                         addr = (struct sockaddr *)&thlds->spt_address;
6218                                 }
6219                         } else {
6220                                 addr = (struct sockaddr *)&thlds->spt_address;
6221                         }
6222 #else
6223                         addr = (struct sockaddr *)&thlds->spt_address;
6224 #endif
6225                         if (stcb != NULL) {
6226                                 net = sctp_findnet(stcb, addr);
6227                         } else {
6228                                 /*
6229                                  * We increment here since
6230                                  * sctp_findassociation_ep_addr() wil do a
6231                                  * decrement if it finds the stcb as long as
6232                                  * the locked tcb (last argument) is NOT a
6233                                  * TCB.. aka NULL.
6234                                  */
6235                                 net = NULL;
6236                                 SCTP_INP_INCR_REF(inp);
6237                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
6238                                     &net, NULL, NULL);
6239                                 if (stcb == NULL) {
6240                                         SCTP_INP_DECR_REF(inp);
6241                                 }
6242                         }
6243                         if ((stcb != NULL) && (net == NULL)) {
6244 #ifdef INET
6245                                 if (addr->sa_family == AF_INET) {
6246
6247                                         struct sockaddr_in *sin;
6248
6249                                         sin = (struct sockaddr_in *)addr;
6250                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6251                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6252                                                 SCTP_TCB_UNLOCK(stcb);
6253                                                 error = EINVAL;
6254                                                 break;
6255                                         }
6256                                 } else
6257 #endif
6258 #ifdef INET6
6259                                 if (addr->sa_family == AF_INET6) {
6260                                         struct sockaddr_in6 *sin6;
6261
6262                                         sin6 = (struct sockaddr_in6 *)addr;
6263                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6264                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6265                                                 SCTP_TCB_UNLOCK(stcb);
6266                                                 error = EINVAL;
6267                                                 break;
6268                                         }
6269                                 } else
6270 #endif
6271                                 {
6272                                         error = EAFNOSUPPORT;
6273                                         SCTP_TCB_UNLOCK(stcb);
6274                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6275                                         break;
6276                                 }
6277                         }
6278                         if (stcb != NULL) {
6279                                 if (net != NULL) {
6280                                         net->failure_threshold = thlds->spt_pathmaxrxt;
6281                                         net->pf_threshold = thlds->spt_pathpfthld;
6282                                         if (net->dest_state & SCTP_ADDR_PF) {
6283                                                 if ((net->error_count > net->failure_threshold) ||
6284                                                     (net->error_count <= net->pf_threshold)) {
6285                                                         net->dest_state &= ~SCTP_ADDR_PF;
6286                                                 }
6287                                         } else {
6288                                                 if ((net->error_count > net->pf_threshold) &&
6289                                                     (net->error_count <= net->failure_threshold)) {
6290                                                         net->dest_state |= SCTP_ADDR_PF;
6291                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6292                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6293                                                             stcb->sctp_ep, stcb, net,
6294                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
6295                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6296                                                 }
6297                                         }
6298                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
6299                                                 if (net->error_count > net->failure_threshold) {
6300                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
6301                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6302                                                 }
6303                                         } else {
6304                                                 if (net->error_count <= net->failure_threshold) {
6305                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
6306                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6307                                                 }
6308                                         }
6309                                 } else {
6310                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6311                                                 net->failure_threshold = thlds->spt_pathmaxrxt;
6312                                                 net->pf_threshold = thlds->spt_pathpfthld;
6313                                                 if (net->dest_state & SCTP_ADDR_PF) {
6314                                                         if ((net->error_count > net->failure_threshold) ||
6315                                                             (net->error_count <= net->pf_threshold)) {
6316                                                                 net->dest_state &= ~SCTP_ADDR_PF;
6317                                                         }
6318                                                 } else {
6319                                                         if ((net->error_count > net->pf_threshold) &&
6320                                                             (net->error_count <= net->failure_threshold)) {
6321                                                                 net->dest_state |= SCTP_ADDR_PF;
6322                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6323                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6324                                                                     stcb->sctp_ep, stcb, net,
6325                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_18);
6326                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6327                                                         }
6328                                                 }
6329                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
6330                                                         if (net->error_count > net->failure_threshold) {
6331                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
6332                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6333                                                         }
6334                                                 } else {
6335                                                         if (net->error_count <= net->failure_threshold) {
6336                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
6337                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6338                                                         }
6339                                                 }
6340                                         }
6341                                         stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6342                                         stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6343                                 }
6344                                 SCTP_TCB_UNLOCK(stcb);
6345                         } else {
6346                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6347                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6348                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6349                                         SCTP_INP_WLOCK(inp);
6350                                         inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6351                                         inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6352                                         SCTP_INP_WUNLOCK(inp);
6353                                 } else {
6354                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6355                                         error = EINVAL;
6356                                 }
6357                         }
6358                         break;
6359                 }
6360         case SCTP_REMOTE_UDP_ENCAPS_PORT:
6361                 {
6362                         struct sctp_udpencaps *encaps;
6363                         struct sctp_nets *net;
6364                         struct sockaddr *addr;
6365
6366 #if defined(INET) && defined(INET6)
6367                         struct sockaddr_in sin_store;
6368
6369 #endif
6370
6371                         SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6372                         SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6373
6374 #if defined(INET) && defined(INET6)
6375                         if (encaps->sue_address.ss_family == AF_INET6) {
6376                                 struct sockaddr_in6 *sin6;
6377
6378                                 sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6379                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6380                                         in6_sin6_2_sin(&sin_store, sin6);
6381                                         addr = (struct sockaddr *)&sin_store;
6382                                 } else {
6383                                         addr = (struct sockaddr *)&encaps->sue_address;
6384                                 }
6385                         } else {
6386                                 addr = (struct sockaddr *)&encaps->sue_address;
6387                         }
6388 #else
6389                         addr = (struct sockaddr *)&encaps->sue_address;
6390 #endif
6391                         if (stcb != NULL) {
6392                                 net = sctp_findnet(stcb, addr);
6393                         } else {
6394                                 /*
6395                                  * We increment here since
6396                                  * sctp_findassociation_ep_addr() wil do a
6397                                  * decrement if it finds the stcb as long as
6398                                  * the locked tcb (last argument) is NOT a
6399                                  * TCB.. aka NULL.
6400                                  */
6401                                 net = NULL;
6402                                 SCTP_INP_INCR_REF(inp);
6403                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6404                                 if (stcb == NULL) {
6405                                         SCTP_INP_DECR_REF(inp);
6406                                 }
6407                         }
6408                         if ((stcb != NULL) && (net == NULL)) {
6409 #ifdef INET
6410                                 if (addr->sa_family == AF_INET) {
6411
6412                                         struct sockaddr_in *sin;
6413
6414                                         sin = (struct sockaddr_in *)addr;
6415                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6416                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6417                                                 SCTP_TCB_UNLOCK(stcb);
6418                                                 error = EINVAL;
6419                                                 break;
6420                                         }
6421                                 } else
6422 #endif
6423 #ifdef INET6
6424                                 if (addr->sa_family == AF_INET6) {
6425                                         struct sockaddr_in6 *sin6;
6426
6427                                         sin6 = (struct sockaddr_in6 *)addr;
6428                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6429                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6430                                                 SCTP_TCB_UNLOCK(stcb);
6431                                                 error = EINVAL;
6432                                                 break;
6433                                         }
6434                                 } else
6435 #endif
6436                                 {
6437                                         error = EAFNOSUPPORT;
6438                                         SCTP_TCB_UNLOCK(stcb);
6439                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6440                                         break;
6441                                 }
6442                         }
6443                         if (stcb != NULL) {
6444                                 if (net != NULL) {
6445                                         net->port = encaps->sue_port;
6446                                 } else {
6447                                         stcb->asoc.port = encaps->sue_port;
6448                                 }
6449                                 SCTP_TCB_UNLOCK(stcb);
6450                         } else {
6451                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6452                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6453                                     (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6454                                         SCTP_INP_WLOCK(inp);
6455                                         inp->sctp_ep.port = encaps->sue_port;
6456                                         SCTP_INP_WUNLOCK(inp);
6457                                 } else {
6458                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6459                                         error = EINVAL;
6460                                 }
6461                         }
6462                         break;
6463                 }
6464         case SCTP_ECN_SUPPORTED:
6465                 {
6466                         struct sctp_assoc_value *av;
6467
6468                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6469                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6470
6471                         if (stcb) {
6472                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6473                                 error = EINVAL;
6474                                 SCTP_TCB_UNLOCK(stcb);
6475                         } else {
6476                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6477                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6478                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6479                                         SCTP_INP_WLOCK(inp);
6480                                         if (av->assoc_value == 0) {
6481                                                 inp->ecn_supported = 0;
6482                                         } else {
6483                                                 inp->ecn_supported = 1;
6484                                         }
6485                                         SCTP_INP_WUNLOCK(inp);
6486                                 } else {
6487                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6488                                         error = EINVAL;
6489                                 }
6490                         }
6491                         break;
6492                 }
6493         case SCTP_PR_SUPPORTED:
6494                 {
6495                         struct sctp_assoc_value *av;
6496
6497                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6498                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6499
6500                         if (stcb) {
6501                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6502                                 error = EINVAL;
6503                                 SCTP_TCB_UNLOCK(stcb);
6504                         } else {
6505                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6506                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6507                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6508                                         SCTP_INP_WLOCK(inp);
6509                                         if (av->assoc_value == 0) {
6510                                                 inp->prsctp_supported = 0;
6511                                         } else {
6512                                                 inp->prsctp_supported = 1;
6513                                         }
6514                                         SCTP_INP_WUNLOCK(inp);
6515                                 } else {
6516                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6517                                         error = EINVAL;
6518                                 }
6519                         }
6520                         break;
6521                 }
6522         case SCTP_AUTH_SUPPORTED:
6523                 {
6524                         struct sctp_assoc_value *av;
6525
6526                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6527                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6528
6529                         if (stcb) {
6530                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6531                                 error = EINVAL;
6532                                 SCTP_TCB_UNLOCK(stcb);
6533                         } else {
6534                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6535                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6536                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6537                                         if ((av->assoc_value == 0) &&
6538                                             (inp->asconf_supported == 1)) {
6539                                                 /*
6540                                                  * AUTH is required for
6541                                                  * ASCONF
6542                                                  */
6543                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6544                                                 error = EINVAL;
6545                                         } else {
6546                                                 SCTP_INP_WLOCK(inp);
6547                                                 if (av->assoc_value == 0) {
6548                                                         inp->auth_supported = 0;
6549                                                 } else {
6550                                                         inp->auth_supported = 1;
6551                                                 }
6552                                                 SCTP_INP_WUNLOCK(inp);
6553                                         }
6554                                 } else {
6555                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6556                                         error = EINVAL;
6557                                 }
6558                         }
6559                         break;
6560                 }
6561         case SCTP_ASCONF_SUPPORTED:
6562                 {
6563                         struct sctp_assoc_value *av;
6564
6565                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6566                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6567
6568                         if (stcb) {
6569                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6570                                 error = EINVAL;
6571                                 SCTP_TCB_UNLOCK(stcb);
6572                         } else {
6573                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6574                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6575                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6576                                         if ((av->assoc_value != 0) &&
6577                                             (inp->auth_supported == 0)) {
6578                                                 /*
6579                                                  * AUTH is required for
6580                                                  * ASCONF
6581                                                  */
6582                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6583                                                 error = EINVAL;
6584                                         } else {
6585                                                 SCTP_INP_WLOCK(inp);
6586                                                 if (av->assoc_value == 0) {
6587                                                         inp->asconf_supported = 0;
6588                                                         sctp_auth_delete_chunk(SCTP_ASCONF,
6589                                                             inp->sctp_ep.local_auth_chunks);
6590                                                         sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6591                                                             inp->sctp_ep.local_auth_chunks);
6592                                                 } else {
6593                                                         inp->asconf_supported = 1;
6594                                                         sctp_auth_add_chunk(SCTP_ASCONF,
6595                                                             inp->sctp_ep.local_auth_chunks);
6596                                                         sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6597                                                             inp->sctp_ep.local_auth_chunks);
6598                                                 }
6599                                                 SCTP_INP_WUNLOCK(inp);
6600                                         }
6601                                 } else {
6602                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6603                                         error = EINVAL;
6604                                 }
6605                         }
6606                         break;
6607                 }
6608         case SCTP_RECONFIG_SUPPORTED:
6609                 {
6610                         struct sctp_assoc_value *av;
6611
6612                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6613                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6614
6615                         if (stcb) {
6616                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6617                                 error = EINVAL;
6618                                 SCTP_TCB_UNLOCK(stcb);
6619                         } else {
6620                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6621                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6622                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6623                                         SCTP_INP_WLOCK(inp);
6624                                         if (av->assoc_value == 0) {
6625                                                 inp->reconfig_supported = 0;
6626                                         } else {
6627                                                 inp->reconfig_supported = 1;
6628                                         }
6629                                         SCTP_INP_WUNLOCK(inp);
6630                                 } else {
6631                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6632                                         error = EINVAL;
6633                                 }
6634                         }
6635                         break;
6636                 }
6637         case SCTP_NRSACK_SUPPORTED:
6638                 {
6639                         struct sctp_assoc_value *av;
6640
6641                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6642                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6643
6644                         if (stcb) {
6645                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6646                                 error = EINVAL;
6647                                 SCTP_TCB_UNLOCK(stcb);
6648                         } else {
6649                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6650                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6651                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6652                                         SCTP_INP_WLOCK(inp);
6653                                         if (av->assoc_value == 0) {
6654                                                 inp->nrsack_supported = 0;
6655                                         } else {
6656                                                 inp->nrsack_supported = 1;
6657                                         }
6658                                         SCTP_INP_WUNLOCK(inp);
6659                                 } else {
6660                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6661                                         error = EINVAL;
6662                                 }
6663                         }
6664                         break;
6665                 }
6666         case SCTP_PKTDROP_SUPPORTED:
6667                 {
6668                         struct sctp_assoc_value *av;
6669
6670                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6671                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6672
6673                         if (stcb) {
6674                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6675                                 error = EINVAL;
6676                                 SCTP_TCB_UNLOCK(stcb);
6677                         } else {
6678                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6679                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6680                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6681                                         SCTP_INP_WLOCK(inp);
6682                                         if (av->assoc_value == 0) {
6683                                                 inp->pktdrop_supported = 0;
6684                                         } else {
6685                                                 inp->pktdrop_supported = 1;
6686                                         }
6687                                         SCTP_INP_WUNLOCK(inp);
6688                                 } else {
6689                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6690                                         error = EINVAL;
6691                                 }
6692                         }
6693                         break;
6694                 }
6695         case SCTP_MAX_CWND:
6696                 {
6697                         struct sctp_assoc_value *av;
6698                         struct sctp_nets *net;
6699
6700                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6701                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6702
6703                         if (stcb) {
6704                                 stcb->asoc.max_cwnd = av->assoc_value;
6705                                 if (stcb->asoc.max_cwnd > 0) {
6706                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6707                                                 if ((net->cwnd > stcb->asoc.max_cwnd) &&
6708                                                     (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
6709                                                         net->cwnd = stcb->asoc.max_cwnd;
6710                                                         if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
6711                                                                 net->cwnd = net->mtu - sizeof(struct sctphdr);
6712                                                         }
6713                                                 }
6714                                         }
6715                                 }
6716                                 SCTP_TCB_UNLOCK(stcb);
6717                         } else {
6718                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6719                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6720                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6721                                         SCTP_INP_WLOCK(inp);
6722                                         inp->max_cwnd = av->assoc_value;
6723                                         SCTP_INP_WUNLOCK(inp);
6724                                 } else {
6725                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6726                                         error = EINVAL;
6727                                 }
6728                         }
6729                         break;
6730                 }
6731         default:
6732                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6733                 error = ENOPROTOOPT;
6734                 break;
6735         }                       /* end switch (opt) */
6736         return (error);
6737 }
6738
6739 int
6740 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6741 {
6742         void *optval = NULL;
6743         size_t optsize = 0;
6744         void *p;
6745         int error = 0;
6746         struct sctp_inpcb *inp;
6747
6748         if ((sopt->sopt_level == SOL_SOCKET) &&
6749             (sopt->sopt_name == SO_SETFIB)) {
6750                 inp = (struct sctp_inpcb *)so->so_pcb;
6751                 if (inp == NULL) {
6752                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6753                         return (EINVAL);
6754                 }
6755                 SCTP_INP_WLOCK(inp);
6756                 inp->fibnum = so->so_fibnum;
6757                 SCTP_INP_WUNLOCK(inp);
6758                 return (0);
6759         }
6760         if (sopt->sopt_level != IPPROTO_SCTP) {
6761                 /* wrong proto level... send back up to IP */
6762 #ifdef INET6
6763                 if (INP_CHECK_SOCKAF(so, AF_INET6))
6764                         error = ip6_ctloutput(so, sopt);
6765 #endif                          /* INET6 */
6766 #if defined(INET) && defined(INET6)
6767                 else
6768 #endif
6769 #ifdef INET
6770                         error = ip_ctloutput(so, sopt);
6771 #endif
6772                 return (error);
6773         }
6774         optsize = sopt->sopt_valsize;
6775         if (optsize) {
6776                 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6777                 if (optval == NULL) {
6778                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6779                         return (ENOBUFS);
6780                 }
6781                 error = sooptcopyin(sopt, optval, optsize, optsize);
6782                 if (error) {
6783                         SCTP_FREE(optval, SCTP_M_SOCKOPT);
6784                         goto out;
6785                 }
6786         }
6787         p = (void *)sopt->sopt_td;
6788         if (sopt->sopt_dir == SOPT_SET) {
6789                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6790         } else if (sopt->sopt_dir == SOPT_GET) {
6791                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6792         } else {
6793                 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6794                 error = EINVAL;
6795         }
6796         if ((error == 0) && (optval != NULL)) {
6797                 error = sooptcopyout(sopt, optval, optsize);
6798                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6799         } else if (optval != NULL) {
6800                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6801         }
6802 out:
6803         return (error);
6804 }
6805
6806 #ifdef INET
6807 static int
6808 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6809 {
6810         int error = 0;
6811         int create_lock_on = 0;
6812         uint32_t vrf_id;
6813         struct sctp_inpcb *inp;
6814         struct sctp_tcb *stcb = NULL;
6815
6816         inp = (struct sctp_inpcb *)so->so_pcb;
6817         if (inp == NULL) {
6818                 /* I made the same as TCP since we are not setup? */
6819                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6820                 return (ECONNRESET);
6821         }
6822         if (addr == NULL) {
6823                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6824                 return EINVAL;
6825         }
6826         switch (addr->sa_family) {
6827 #ifdef INET6
6828         case AF_INET6:
6829                 {
6830                         struct sockaddr_in6 *sin6p;
6831
6832                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6833                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6834                                 return (EINVAL);
6835                         }
6836                         sin6p = (struct sockaddr_in6 *)addr;
6837                         if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6838                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6839                                 return (error);
6840                         }
6841                         break;
6842                 }
6843 #endif
6844 #ifdef INET
6845         case AF_INET:
6846                 {
6847                         struct sockaddr_in *sinp;
6848
6849                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
6850                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6851                                 return (EINVAL);
6852                         }
6853                         sinp = (struct sockaddr_in *)addr;
6854                         if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6855                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6856                                 return (error);
6857                         }
6858                         break;
6859                 }
6860 #endif
6861         default:
6862                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6863                 return (EAFNOSUPPORT);
6864         }
6865         SCTP_INP_INCR_REF(inp);
6866         SCTP_ASOC_CREATE_LOCK(inp);
6867         create_lock_on = 1;
6868
6869
6870         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6871             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6872                 /* Should I really unlock ? */
6873                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6874                 error = EFAULT;
6875                 goto out_now;
6876         }
6877 #ifdef INET6
6878         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6879             (addr->sa_family == AF_INET6)) {
6880                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6881                 error = EINVAL;
6882                 goto out_now;
6883         }
6884 #endif
6885         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6886             SCTP_PCB_FLAGS_UNBOUND) {
6887                 /* Bind a ephemeral port */
6888                 error = sctp_inpcb_bind(so, NULL, NULL, p);
6889                 if (error) {
6890                         goto out_now;
6891                 }
6892         }
6893         /* Now do we connect? */
6894         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6895             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6896                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6897                 error = EINVAL;
6898                 goto out_now;
6899         }
6900         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6901             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6902                 /* We are already connected AND the TCP model */
6903                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6904                 error = EADDRINUSE;
6905                 goto out_now;
6906         }
6907         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6908                 SCTP_INP_RLOCK(inp);
6909                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
6910                 SCTP_INP_RUNLOCK(inp);
6911         } else {
6912                 /*
6913                  * We increment here since sctp_findassociation_ep_addr()
6914                  * will do a decrement if it finds the stcb as long as the
6915                  * locked tcb (last argument) is NOT a TCB.. aka NULL.
6916                  */
6917                 SCTP_INP_INCR_REF(inp);
6918                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
6919                 if (stcb == NULL) {
6920                         SCTP_INP_DECR_REF(inp);
6921                 } else {
6922                         SCTP_TCB_UNLOCK(stcb);
6923                 }
6924         }
6925         if (stcb != NULL) {
6926                 /* Already have or am bring up an association */
6927                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
6928                 error = EALREADY;
6929                 goto out_now;
6930         }
6931         vrf_id = inp->def_vrf_id;
6932         /* We are GOOD to go */
6933         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, inp->sctp_ep.pre_open_stream_count, p);
6934         if (stcb == NULL) {
6935                 /* Gak! no memory */
6936                 goto out_now;
6937         }
6938         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
6939                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
6940                 /* Set the connected flag so we can queue data */
6941                 soisconnecting(so);
6942         }
6943         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
6944         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
6945
6946         /* initialize authentication parameters for the assoc */
6947         sctp_initialize_auth_params(inp, stcb);
6948
6949         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
6950         SCTP_TCB_UNLOCK(stcb);
6951 out_now:
6952         if (create_lock_on) {
6953                 SCTP_ASOC_CREATE_UNLOCK(inp);
6954         }
6955         SCTP_INP_DECR_REF(inp);
6956         return (error);
6957 }
6958
6959 #endif
6960
6961 int
6962 sctp_listen(struct socket *so, int backlog, struct thread *p)
6963 {
6964         /*
6965          * Note this module depends on the protocol processing being called
6966          * AFTER any socket level flags and backlog are applied to the
6967          * socket. The traditional way that the socket flags are applied is
6968          * AFTER protocol processing. We have made a change to the
6969          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
6970          * place if the socket API for SCTP is to work properly.
6971          */
6972
6973         int error = 0;
6974         struct sctp_inpcb *inp;
6975
6976         inp = (struct sctp_inpcb *)so->so_pcb;
6977         if (inp == NULL) {
6978                 /* I made the same as TCP since we are not setup? */
6979                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6980                 return (ECONNRESET);
6981         }
6982         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
6983                 /* See if we have a listener */
6984                 struct sctp_inpcb *tinp;
6985                 union sctp_sockstore store;
6986
6987                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
6988                         /* not bound all */
6989                         struct sctp_laddr *laddr;
6990
6991                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6992                                 memcpy(&store, &laddr->ifa->address, sizeof(store));
6993                                 switch (store.sa.sa_family) {
6994 #ifdef INET
6995                                 case AF_INET:
6996                                         store.sin.sin_port = inp->sctp_lport;
6997                                         break;
6998 #endif
6999 #ifdef INET6
7000                                 case AF_INET6:
7001                                         store.sin6.sin6_port = inp->sctp_lport;
7002                                         break;
7003 #endif
7004                                 default:
7005                                         break;
7006                                 }
7007                                 tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7008                                 if (tinp && (tinp != inp) &&
7009                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7010                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7011                                     (tinp->sctp_socket->so_qlimit)) {
7012                                         /*
7013                                          * we have a listener already and
7014                                          * its not this inp.
7015                                          */
7016                                         SCTP_INP_DECR_REF(tinp);
7017                                         return (EADDRINUSE);
7018                                 } else if (tinp) {
7019                                         SCTP_INP_DECR_REF(tinp);
7020                                 }
7021                         }
7022                 } else {
7023                         /* Setup a local addr bound all */
7024                         memset(&store, 0, sizeof(store));
7025 #ifdef INET6
7026                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7027                                 store.sa.sa_family = AF_INET6;
7028                                 store.sa.sa_len = sizeof(struct sockaddr_in6);
7029                         }
7030 #endif
7031 #ifdef INET
7032                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7033                                 store.sa.sa_family = AF_INET;
7034                                 store.sa.sa_len = sizeof(struct sockaddr_in);
7035                         }
7036 #endif
7037                         switch (store.sa.sa_family) {
7038 #ifdef INET
7039                         case AF_INET:
7040                                 store.sin.sin_port = inp->sctp_lport;
7041                                 break;
7042 #endif
7043 #ifdef INET6
7044                         case AF_INET6:
7045                                 store.sin6.sin6_port = inp->sctp_lport;
7046                                 break;
7047 #endif
7048                         default:
7049                                 break;
7050                         }
7051                         tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7052                         if (tinp && (tinp != inp) &&
7053                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7054                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7055                             (tinp->sctp_socket->so_qlimit)) {
7056                                 /*
7057                                  * we have a listener already and its not
7058                                  * this inp.
7059                                  */
7060                                 SCTP_INP_DECR_REF(tinp);
7061                                 return (EADDRINUSE);
7062                         } else if (tinp) {
7063                                 SCTP_INP_DECR_REF(tinp);
7064                         }
7065                 }
7066         }
7067         SCTP_INP_RLOCK(inp);
7068 #ifdef SCTP_LOCK_LOGGING
7069         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7070                 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7071         }
7072 #endif
7073         SOCK_LOCK(so);
7074         error = solisten_proto_check(so);
7075         SOCK_UNLOCK(so);
7076         if (error) {
7077                 SCTP_INP_RUNLOCK(inp);
7078                 return (error);
7079         }
7080         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7081             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7082                 /*
7083                  * The unlucky case - We are in the tcp pool with this guy.
7084                  * - Someone else is in the main inp slot. - We must move
7085                  * this guy (the listener) to the main slot - We must then
7086                  * move the guy that was listener to the TCP Pool.
7087                  */
7088                 if (sctp_swap_inpcb_for_listen(inp)) {
7089                         SCTP_INP_RUNLOCK(inp);
7090                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7091                         return (EADDRINUSE);
7092                 }
7093         }
7094         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7095             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7096                 /* We are already connected AND the TCP model */
7097                 SCTP_INP_RUNLOCK(inp);
7098                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7099                 return (EADDRINUSE);
7100         }
7101         SCTP_INP_RUNLOCK(inp);
7102         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7103                 /* We must do a bind. */
7104                 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7105                         /* bind error, probably perm */
7106                         return (error);
7107                 }
7108         }
7109         SOCK_LOCK(so);
7110         /* It appears for 7.0 and on, we must always call this. */
7111         solisten_proto(so, backlog);
7112         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7113                 /* remove the ACCEPTCONN flag for one-to-many sockets */
7114                 so->so_options &= ~SO_ACCEPTCONN;
7115         }
7116         if (backlog == 0) {
7117                 /* turning off listen */
7118                 so->so_options &= ~SO_ACCEPTCONN;
7119         }
7120         SOCK_UNLOCK(so);
7121         return (error);
7122 }
7123
7124 static int sctp_defered_wakeup_cnt = 0;
7125
7126 int
7127 sctp_accept(struct socket *so, struct sockaddr **addr)
7128 {
7129         struct sctp_tcb *stcb;
7130         struct sctp_inpcb *inp;
7131         union sctp_sockstore store;
7132
7133 #ifdef INET6
7134         int error;
7135
7136 #endif
7137         inp = (struct sctp_inpcb *)so->so_pcb;
7138
7139         if (inp == NULL) {
7140                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7141                 return (ECONNRESET);
7142         }
7143         SCTP_INP_RLOCK(inp);
7144         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7145                 SCTP_INP_RUNLOCK(inp);
7146                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7147                 return (EOPNOTSUPP);
7148         }
7149         if (so->so_state & SS_ISDISCONNECTED) {
7150                 SCTP_INP_RUNLOCK(inp);
7151                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7152                 return (ECONNABORTED);
7153         }
7154         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7155         if (stcb == NULL) {
7156                 SCTP_INP_RUNLOCK(inp);
7157                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7158                 return (ECONNRESET);
7159         }
7160         SCTP_TCB_LOCK(stcb);
7161         SCTP_INP_RUNLOCK(inp);
7162         store = stcb->asoc.primary_destination->ro._l_addr;
7163         stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
7164         SCTP_TCB_UNLOCK(stcb);
7165         switch (store.sa.sa_family) {
7166 #ifdef INET
7167         case AF_INET:
7168                 {
7169                         struct sockaddr_in *sin;
7170
7171                         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7172                         if (sin == NULL)
7173                                 return (ENOMEM);
7174                         sin->sin_family = AF_INET;
7175                         sin->sin_len = sizeof(*sin);
7176                         sin->sin_port = store.sin.sin_port;
7177                         sin->sin_addr = store.sin.sin_addr;
7178                         *addr = (struct sockaddr *)sin;
7179                         break;
7180                 }
7181 #endif
7182 #ifdef INET6
7183         case AF_INET6:
7184                 {
7185                         struct sockaddr_in6 *sin6;
7186
7187                         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7188                         if (sin6 == NULL)
7189                                 return (ENOMEM);
7190                         sin6->sin6_family = AF_INET6;
7191                         sin6->sin6_len = sizeof(*sin6);
7192                         sin6->sin6_port = store.sin6.sin6_port;
7193                         sin6->sin6_addr = store.sin6.sin6_addr;
7194                         if ((error = sa6_recoverscope(sin6)) != 0) {
7195                                 SCTP_FREE_SONAME(sin6);
7196                                 return (error);
7197                         }
7198                         *addr = (struct sockaddr *)sin6;
7199                         break;
7200                 }
7201 #endif
7202         default:
7203                 /* TSNH */
7204                 break;
7205         }
7206         /* Wake any delayed sleep action */
7207         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7208                 SCTP_INP_WLOCK(inp);
7209                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7210                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7211                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7212                         SCTP_INP_WUNLOCK(inp);
7213                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7214                         if (sowriteable(inp->sctp_socket)) {
7215                                 sowwakeup_locked(inp->sctp_socket);
7216                         } else {
7217                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7218                         }
7219                         SCTP_INP_WLOCK(inp);
7220                 }
7221                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7222                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7223                         SCTP_INP_WUNLOCK(inp);
7224                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7225                         if (soreadable(inp->sctp_socket)) {
7226                                 sctp_defered_wakeup_cnt++;
7227                                 sorwakeup_locked(inp->sctp_socket);
7228                         } else {
7229                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7230                         }
7231                         SCTP_INP_WLOCK(inp);
7232                 }
7233                 SCTP_INP_WUNLOCK(inp);
7234         }
7235         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7236                 SCTP_TCB_LOCK(stcb);
7237                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7238                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
7239         }
7240         return (0);
7241 }
7242
7243 #ifdef INET
7244 int
7245 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7246 {
7247         struct sockaddr_in *sin;
7248         uint32_t vrf_id;
7249         struct sctp_inpcb *inp;
7250         struct sctp_ifa *sctp_ifa;
7251
7252         /*
7253          * Do the malloc first in case it blocks.
7254          */
7255         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7256         if (sin == NULL)
7257                 return (ENOMEM);
7258         sin->sin_family = AF_INET;
7259         sin->sin_len = sizeof(*sin);
7260         inp = (struct sctp_inpcb *)so->so_pcb;
7261         if (!inp) {
7262                 SCTP_FREE_SONAME(sin);
7263                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7264                 return (ECONNRESET);
7265         }
7266         SCTP_INP_RLOCK(inp);
7267         sin->sin_port = inp->sctp_lport;
7268         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7269                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7270                         struct sctp_tcb *stcb;
7271                         struct sockaddr_in *sin_a;
7272                         struct sctp_nets *net;
7273                         int fnd;
7274
7275                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7276                         if (stcb == NULL) {
7277                                 goto notConn;
7278                         }
7279                         fnd = 0;
7280                         sin_a = NULL;
7281                         SCTP_TCB_LOCK(stcb);
7282                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7283                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7284                                 if (sin_a == NULL)
7285                                         /* this will make coverity happy */
7286                                         continue;
7287
7288                                 if (sin_a->sin_family == AF_INET) {
7289                                         fnd = 1;
7290                                         break;
7291                                 }
7292                         }
7293                         if ((!fnd) || (sin_a == NULL)) {
7294                                 /* punt */
7295                                 SCTP_TCB_UNLOCK(stcb);
7296                                 goto notConn;
7297                         }
7298                         vrf_id = inp->def_vrf_id;
7299                         sctp_ifa = sctp_source_address_selection(inp,
7300                             stcb,
7301                             (sctp_route_t *) & net->ro,
7302                             net, 0, vrf_id);
7303                         if (sctp_ifa) {
7304                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7305                                 sctp_free_ifa(sctp_ifa);
7306                         }
7307                         SCTP_TCB_UNLOCK(stcb);
7308                 } else {
7309                         /* For the bound all case you get back 0 */
7310         notConn:
7311                         sin->sin_addr.s_addr = 0;
7312                 }
7313
7314         } else {
7315                 /* Take the first IPv4 address in the list */
7316                 struct sctp_laddr *laddr;
7317                 int fnd = 0;
7318
7319                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7320                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
7321                                 struct sockaddr_in *sin_a;
7322
7323                                 sin_a = &laddr->ifa->address.sin;
7324                                 sin->sin_addr = sin_a->sin_addr;
7325                                 fnd = 1;
7326                                 break;
7327                         }
7328                 }
7329                 if (!fnd) {
7330                         SCTP_FREE_SONAME(sin);
7331                         SCTP_INP_RUNLOCK(inp);
7332                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7333                         return (ENOENT);
7334                 }
7335         }
7336         SCTP_INP_RUNLOCK(inp);
7337         (*addr) = (struct sockaddr *)sin;
7338         return (0);
7339 }
7340
7341 int
7342 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7343 {
7344         struct sockaddr_in *sin;
7345         int fnd;
7346         struct sockaddr_in *sin_a;
7347         struct sctp_inpcb *inp;
7348         struct sctp_tcb *stcb;
7349         struct sctp_nets *net;
7350
7351         /* Do the malloc first in case it blocks. */
7352         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7353         if (sin == NULL)
7354                 return (ENOMEM);
7355         sin->sin_family = AF_INET;
7356         sin->sin_len = sizeof(*sin);
7357
7358         inp = (struct sctp_inpcb *)so->so_pcb;
7359         if ((inp == NULL) ||
7360             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7361                 /* UDP type and listeners will drop out here */
7362                 SCTP_FREE_SONAME(sin);
7363                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7364                 return (ENOTCONN);
7365         }
7366         SCTP_INP_RLOCK(inp);
7367         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7368         if (stcb) {
7369                 SCTP_TCB_LOCK(stcb);
7370         }
7371         SCTP_INP_RUNLOCK(inp);
7372         if (stcb == NULL) {
7373                 SCTP_FREE_SONAME(sin);
7374                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7375                 return (ECONNRESET);
7376         }
7377         fnd = 0;
7378         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7379                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7380                 if (sin_a->sin_family == AF_INET) {
7381                         fnd = 1;
7382                         sin->sin_port = stcb->rport;
7383                         sin->sin_addr = sin_a->sin_addr;
7384                         break;
7385                 }
7386         }
7387         SCTP_TCB_UNLOCK(stcb);
7388         if (!fnd) {
7389                 /* No IPv4 address */
7390                 SCTP_FREE_SONAME(sin);
7391                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7392                 return (ENOENT);
7393         }
7394         (*addr) = (struct sockaddr *)sin;
7395         return (0);
7396 }
7397
7398 struct pr_usrreqs sctp_usrreqs = {
7399         .pru_abort = sctp_abort,
7400         .pru_accept = sctp_accept,
7401         .pru_attach = sctp_attach,
7402         .pru_bind = sctp_bind,
7403         .pru_connect = sctp_connect,
7404         .pru_control = in_control,
7405         .pru_close = sctp_close,
7406         .pru_detach = sctp_close,
7407         .pru_sopoll = sopoll_generic,
7408         .pru_flush = sctp_flush,
7409         .pru_disconnect = sctp_disconnect,
7410         .pru_listen = sctp_listen,
7411         .pru_peeraddr = sctp_peeraddr,
7412         .pru_send = sctp_sendm,
7413         .pru_shutdown = sctp_shutdown,
7414         .pru_sockaddr = sctp_ingetaddr,
7415         .pru_sosend = sctp_sosend,
7416         .pru_soreceive = sctp_soreceive
7417 };
7418
7419 #endif