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