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