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