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