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