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