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