]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_usrreq.c
Update OpenSSL to 1.1.1.
[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_PRINTF("huh? control set?\n");
634                         sctp_m_freem(inp->control);
635                         inp->control = NULL;
636                 }
637                 inp->control = control;
638         }
639         /* Place the data */
640         if (inp->pkt) {
641                 SCTP_BUF_NEXT(inp->pkt_last) = m;
642                 inp->pkt_last = m;
643         } else {
644                 inp->pkt_last = inp->pkt = m;
645         }
646         if (
647         /* FreeBSD uses a flag passed */
648             ((flags & PRUS_MORETOCOME) == 0)
649             ) {
650                 /*
651                  * note with the current version this code will only be used
652                  * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
653                  * re-defining sosend to use the sctp_sosend. One can
654                  * optionally switch back to this code (by changing back the
655                  * definitions) but this is not advisable. This code is used
656                  * by FreeBSD when sending a file with sendfile() though.
657                  */
658                 int ret;
659
660                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
661                 inp->pkt = NULL;
662                 inp->control = NULL;
663                 return (ret);
664         } else {
665                 return (0);
666         }
667 }
668
669 int
670 sctp_disconnect(struct socket *so)
671 {
672         struct sctp_inpcb *inp;
673
674         inp = (struct sctp_inpcb *)so->so_pcb;
675         if (inp == NULL) {
676                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
677                 return (ENOTCONN);
678         }
679         SCTP_INP_RLOCK(inp);
680         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
681             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
682                 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
683                         /* No connection */
684                         SCTP_INP_RUNLOCK(inp);
685                         return (0);
686                 } else {
687                         struct sctp_association *asoc;
688                         struct sctp_tcb *stcb;
689
690                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
691                         if (stcb == NULL) {
692                                 SCTP_INP_RUNLOCK(inp);
693                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
694                                 return (EINVAL);
695                         }
696                         SCTP_TCB_LOCK(stcb);
697                         asoc = &stcb->asoc;
698                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
699                                 /* We are about to be freed, out of here */
700                                 SCTP_TCB_UNLOCK(stcb);
701                                 SCTP_INP_RUNLOCK(inp);
702                                 return (0);
703                         }
704                         if (((so->so_options & SO_LINGER) &&
705                             (so->so_linger == 0)) ||
706                             (so->so_rcv.sb_cc > 0)) {
707                                 if (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) {
708                                         /* Left with Data unread */
709                                         struct mbuf *op_err;
710
711                                         op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
712                                         sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
713                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
714                                 }
715                                 SCTP_INP_RUNLOCK(inp);
716                                 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
717                                     (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
718                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
719                                 }
720                                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
721                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
722                                 /* No unlock tcb assoc is gone */
723                                 return (0);
724                         }
725                         if (TAILQ_EMPTY(&asoc->send_queue) &&
726                             TAILQ_EMPTY(&asoc->sent_queue) &&
727                             (asoc->stream_queue_cnt == 0)) {
728                                 /* there is nothing queued to send, so done */
729                                 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
730                                         goto abort_anyway;
731                                 }
732                                 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
733                                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
734                                         /* only send SHUTDOWN 1st time thru */
735                                         struct sctp_nets *netp;
736
737                                         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
738                                             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
739                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
740                                         }
741                                         SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
742                                         sctp_stop_timers_for_shutdown(stcb);
743                                         if (stcb->asoc.alternate) {
744                                                 netp = stcb->asoc.alternate;
745                                         } else {
746                                                 netp = stcb->asoc.primary_destination;
747                                         }
748                                         sctp_send_shutdown(stcb, netp);
749                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
750                                             stcb->sctp_ep, stcb, netp);
751                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
752                                             stcb->sctp_ep, stcb, netp);
753                                         sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
754                                 }
755                         } else {
756                                 /*
757                                  * we still got (or just got) data to send,
758                                  * so set SHUTDOWN_PENDING
759                                  */
760                                 /*
761                                  * XXX sockets draft says that SCTP_EOF
762                                  * should be sent with no data. currently,
763                                  * we will allow user data to be sent first
764                                  * and move to SHUTDOWN-PENDING
765                                  */
766                                 struct sctp_nets *netp;
767
768                                 if (stcb->asoc.alternate) {
769                                         netp = stcb->asoc.alternate;
770                                 } else {
771                                         netp = stcb->asoc.primary_destination;
772                                 }
773
774                                 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
775                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
776                                     netp);
777                                 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
778                                         SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
779                                 }
780                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
781                                     TAILQ_EMPTY(&asoc->sent_queue) &&
782                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
783                                         struct mbuf *op_err;
784
785                         abort_anyway:
786                                         op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
787                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
788                                         sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
789                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
790                                         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
791                                             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
792                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
793                                         }
794                                         SCTP_INP_RUNLOCK(inp);
795                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
796                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
797                                         return (0);
798                                 } else {
799                                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
800                                 }
801                         }
802                         soisdisconnecting(so);
803                         SCTP_TCB_UNLOCK(stcb);
804                         SCTP_INP_RUNLOCK(inp);
805                         return (0);
806                 }
807                 /* not reached */
808         } else {
809                 /* UDP model does not support this */
810                 SCTP_INP_RUNLOCK(inp);
811                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
812                 return (EOPNOTSUPP);
813         }
814 }
815
816 int
817 sctp_flush(struct socket *so, int how)
818 {
819         /*
820          * We will just clear out the values and let subsequent close clear
821          * out the data, if any. Note if the user did a shutdown(SHUT_RD)
822          * they will not be able to read the data, the socket will block
823          * that from happening.
824          */
825         struct sctp_inpcb *inp;
826
827         inp = (struct sctp_inpcb *)so->so_pcb;
828         if (inp == NULL) {
829                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
830                 return (EINVAL);
831         }
832         SCTP_INP_RLOCK(inp);
833         /* For the 1 to many model this does nothing */
834         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
835                 SCTP_INP_RUNLOCK(inp);
836                 return (0);
837         }
838         SCTP_INP_RUNLOCK(inp);
839         if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
840                 /*
841                  * First make sure the sb will be happy, we don't use these
842                  * except maybe the count
843                  */
844                 SCTP_INP_WLOCK(inp);
845                 SCTP_INP_READ_LOCK(inp);
846                 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
847                 SCTP_INP_READ_UNLOCK(inp);
848                 SCTP_INP_WUNLOCK(inp);
849                 so->so_rcv.sb_cc = 0;
850                 so->so_rcv.sb_mbcnt = 0;
851                 so->so_rcv.sb_mb = NULL;
852         }
853         if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
854                 /*
855                  * First make sure the sb will be happy, we don't use these
856                  * except maybe the count
857                  */
858                 so->so_snd.sb_cc = 0;
859                 so->so_snd.sb_mbcnt = 0;
860                 so->so_snd.sb_mb = NULL;
861
862         }
863         return (0);
864 }
865
866 int
867 sctp_shutdown(struct socket *so)
868 {
869         struct sctp_inpcb *inp;
870
871         inp = (struct sctp_inpcb *)so->so_pcb;
872         if (inp == NULL) {
873                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
874                 return (EINVAL);
875         }
876         SCTP_INP_RLOCK(inp);
877         /* For UDP model this is a invalid call */
878         if (!((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
879             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
880                 /* Restore the flags that the soshutdown took away. */
881                 SOCKBUF_LOCK(&so->so_rcv);
882                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
883                 SOCKBUF_UNLOCK(&so->so_rcv);
884                 /* This proc will wakeup for read and do nothing (I hope) */
885                 SCTP_INP_RUNLOCK(inp);
886                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
887                 return (EOPNOTSUPP);
888         } else {
889                 /*
890                  * Ok, if we reach here its the TCP model and it is either a
891                  * SHUT_WR or SHUT_RDWR. This means we put the shutdown flag
892                  * against it.
893                  */
894                 struct sctp_tcb *stcb;
895                 struct sctp_association *asoc;
896                 struct sctp_nets *netp;
897
898                 if ((so->so_state &
899                     (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
900                         SCTP_INP_RUNLOCK(inp);
901                         return (ENOTCONN);
902                 }
903                 socantsendmore(so);
904
905                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
906                 if (stcb == NULL) {
907                         /*
908                          * Ok, we hit the case that the shutdown call was
909                          * made after an abort or something. Nothing to do
910                          * now.
911                          */
912                         SCTP_INP_RUNLOCK(inp);
913                         return (0);
914                 }
915                 SCTP_TCB_LOCK(stcb);
916                 asoc = &stcb->asoc;
917                 if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
918                         SCTP_TCB_UNLOCK(stcb);
919                         SCTP_INP_RUNLOCK(inp);
920                         return (0);
921                 }
922                 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) &&
923                     (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_ECHOED) &&
924                     (SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN)) {
925                         /*
926                          * If we are not in or before ESTABLISHED, there is
927                          * no protocol action required.
928                          */
929                         SCTP_TCB_UNLOCK(stcb);
930                         SCTP_INP_RUNLOCK(inp);
931                         return (0);
932                 }
933                 if (stcb->asoc.alternate) {
934                         netp = stcb->asoc.alternate;
935                 } else {
936                         netp = stcb->asoc.primary_destination;
937                 }
938                 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
939                     TAILQ_EMPTY(&asoc->send_queue) &&
940                     TAILQ_EMPTY(&asoc->sent_queue) &&
941                     (asoc->stream_queue_cnt == 0)) {
942                         if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
943                                 goto abort_anyway;
944                         }
945                         /* there is nothing queued to send, so I'm done... */
946                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
947                         SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
948                         sctp_stop_timers_for_shutdown(stcb);
949                         sctp_send_shutdown(stcb, netp);
950                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
951                             stcb->sctp_ep, stcb, netp);
952                 } else {
953                         /*
954                          * We still got (or just got) data to send, so set
955                          * SHUTDOWN_PENDING.
956                          */
957                         SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
958                         if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
959                                 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
960                         }
961                         if (TAILQ_EMPTY(&asoc->send_queue) &&
962                             TAILQ_EMPTY(&asoc->sent_queue) &&
963                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
964                                 struct mbuf *op_err;
965
966                 abort_anyway:
967                                 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
968                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
969                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
970                                     op_err, SCTP_SO_LOCKED);
971                                 SCTP_INP_RUNLOCK(inp);
972                                 return (0);
973                         }
974                 }
975                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, netp);
976                 /*
977                  * XXX: Why do this in the case where we have still data
978                  * queued?
979                  */
980                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
981                 SCTP_TCB_UNLOCK(stcb);
982                 SCTP_INP_RUNLOCK(inp);
983                 return (0);
984         }
985 }
986
987 /*
988  * copies a "user" presentable address and removes embedded scope, etc.
989  * returns 0 on success, 1 on error
990  */
991 static uint32_t
992 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
993 {
994 #ifdef INET6
995         struct sockaddr_in6 lsa6;
996
997         sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
998             &lsa6);
999 #endif
1000         memcpy(ss, sa, sa->sa_len);
1001         return (0);
1002 }
1003
1004
1005
1006 /*
1007  * NOTE: assumes addr lock is held
1008  */
1009 static size_t
1010 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1011     struct sctp_tcb *stcb,
1012     size_t limit,
1013     struct sockaddr_storage *sas,
1014     uint32_t vrf_id)
1015 {
1016         struct sctp_ifn *sctp_ifn;
1017         struct sctp_ifa *sctp_ifa;
1018         size_t actual;
1019         int loopback_scope;
1020 #if defined(INET)
1021         int ipv4_local_scope, ipv4_addr_legal;
1022 #endif
1023 #if defined(INET6)
1024         int local_scope, site_scope, ipv6_addr_legal;
1025 #endif
1026         struct sctp_vrf *vrf;
1027
1028         actual = 0;
1029         if (limit <= 0)
1030                 return (actual);
1031
1032         if (stcb) {
1033                 /* Turn on all the appropriate scope */
1034                 loopback_scope = stcb->asoc.scope.loopback_scope;
1035 #if defined(INET)
1036                 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
1037                 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
1038 #endif
1039 #if defined(INET6)
1040                 local_scope = stcb->asoc.scope.local_scope;
1041                 site_scope = stcb->asoc.scope.site_scope;
1042                 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
1043 #endif
1044         } else {
1045                 /* Use generic values for endpoints. */
1046                 loopback_scope = 1;
1047 #if defined(INET)
1048                 ipv4_local_scope = 1;
1049 #endif
1050 #if defined(INET6)
1051                 local_scope = 1;
1052                 site_scope = 1;
1053 #endif
1054                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1055 #if defined(INET6)
1056                         ipv6_addr_legal = 1;
1057 #endif
1058 #if defined(INET)
1059                         if (SCTP_IPV6_V6ONLY(inp)) {
1060                                 ipv4_addr_legal = 0;
1061                         } else {
1062                                 ipv4_addr_legal = 1;
1063                         }
1064 #endif
1065                 } else {
1066 #if defined(INET6)
1067                         ipv6_addr_legal = 0;
1068 #endif
1069 #if defined(INET)
1070                         ipv4_addr_legal = 1;
1071 #endif
1072                 }
1073         }
1074         vrf = sctp_find_vrf(vrf_id);
1075         if (vrf == NULL) {
1076                 return (0);
1077         }
1078         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1079                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1080                         if ((loopback_scope == 0) &&
1081                             SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1082                                 /* Skip loopback if loopback_scope not set */
1083                                 continue;
1084                         }
1085                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1086                                 if (stcb) {
1087                                         /*
1088                                          * For the BOUND-ALL case, the list
1089                                          * associated with a TCB is Always
1090                                          * considered a reverse list.. i.e.
1091                                          * it lists addresses that are NOT
1092                                          * part of the association. If this
1093                                          * is one of those we must skip it.
1094                                          */
1095                                         if (sctp_is_addr_restricted(stcb,
1096                                             sctp_ifa)) {
1097                                                 continue;
1098                                         }
1099                                 }
1100                                 switch (sctp_ifa->address.sa.sa_family) {
1101 #ifdef INET
1102                                 case AF_INET:
1103                                         if (ipv4_addr_legal) {
1104                                                 struct sockaddr_in *sin;
1105
1106                                                 sin = &sctp_ifa->address.sin;
1107                                                 if (sin->sin_addr.s_addr == 0) {
1108                                                         /*
1109                                                          * we skip
1110                                                          * unspecifed
1111                                                          * addresses
1112                                                          */
1113                                                         continue;
1114                                                 }
1115                                                 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1116                                                     &sin->sin_addr) != 0) {
1117                                                         continue;
1118                                                 }
1119                                                 if ((ipv4_local_scope == 0) &&
1120                                                     (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1121                                                         continue;
1122                                                 }
1123 #ifdef INET6
1124                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1125                                                         in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1126                                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1127                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1128                                                         actual += sizeof(struct sockaddr_in6);
1129                                                 } else {
1130 #endif
1131                                                         memcpy(sas, sin, sizeof(*sin));
1132                                                         ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1133                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1134                                                         actual += sizeof(*sin);
1135 #ifdef INET6
1136                                                 }
1137 #endif
1138                                                 if (actual >= limit) {
1139                                                         return (actual);
1140                                                 }
1141                                         } else {
1142                                                 continue;
1143                                         }
1144                                         break;
1145 #endif
1146 #ifdef INET6
1147                                 case AF_INET6:
1148                                         if (ipv6_addr_legal) {
1149                                                 struct sockaddr_in6 *sin6;
1150
1151                                                 sin6 = &sctp_ifa->address.sin6;
1152                                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1153                                                         /*
1154                                                          * we skip
1155                                                          * unspecifed
1156                                                          * addresses
1157                                                          */
1158                                                         continue;
1159                                                 }
1160                                                 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1161                                                     &sin6->sin6_addr) != 0) {
1162                                                         continue;
1163                                                 }
1164                                                 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1165                                                         if (local_scope == 0)
1166                                                                 continue;
1167                                                         if (sin6->sin6_scope_id == 0) {
1168                                                                 if (sa6_recoverscope(sin6) != 0)
1169                                                                         /*
1170                                                                          *
1171                                                                          * bad
1172                                                                          * link
1173                                                                          *
1174                                                                          * local
1175                                                                          *
1176                                                                          * address
1177                                                                          */
1178                                                                         continue;
1179                                                         }
1180                                                 }
1181                                                 if ((site_scope == 0) &&
1182                                                     (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1183                                                         continue;
1184                                                 }
1185                                                 memcpy(sas, sin6, sizeof(*sin6));
1186                                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1187                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1188                                                 actual += sizeof(*sin6);
1189                                                 if (actual >= limit) {
1190                                                         return (actual);
1191                                                 }
1192                                         } else {
1193                                                 continue;
1194                                         }
1195                                         break;
1196 #endif
1197                                 default:
1198                                         /* TSNH */
1199                                         break;
1200                                 }
1201                         }
1202                 }
1203         } else {
1204                 struct sctp_laddr *laddr;
1205
1206                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1207                         if (stcb) {
1208                                 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1209                                         continue;
1210                                 }
1211                         }
1212                         if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1213                                 continue;
1214                         switch (laddr->ifa->address.sa.sa_family) {
1215 #ifdef INET
1216                         case AF_INET:
1217                                 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1218                                 break;
1219 #endif
1220 #ifdef INET6
1221                         case AF_INET6:
1222                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1223                                 break;
1224 #endif
1225                         default:
1226                                 /* TSNH */
1227                                 break;
1228                         }
1229                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1230                             laddr->ifa->address.sa.sa_len);
1231                         actual += laddr->ifa->address.sa.sa_len;
1232                         if (actual >= limit) {
1233                                 return (actual);
1234                         }
1235                 }
1236         }
1237         return (actual);
1238 }
1239
1240 static size_t
1241 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1242     struct sctp_tcb *stcb,
1243     size_t limit,
1244     struct sockaddr_storage *sas)
1245 {
1246         size_t size = 0;
1247
1248         SCTP_IPI_ADDR_RLOCK();
1249         /* fill up addresses for the endpoint's default vrf */
1250         size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1251             inp->def_vrf_id);
1252         SCTP_IPI_ADDR_RUNLOCK();
1253         return (size);
1254 }
1255
1256 /*
1257  * NOTE: assumes addr lock is held
1258  */
1259 static int
1260 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1261 {
1262         int cnt = 0;
1263         struct sctp_vrf *vrf = NULL;
1264
1265         /*
1266          * In both sub-set bound an bound_all cases we return the MAXIMUM
1267          * number of addresses that you COULD get. In reality the sub-set
1268          * bound may have an exclusion list for a given TCB OR in the
1269          * bound-all case a TCB may NOT include the loopback or other
1270          * addresses as well.
1271          */
1272         vrf = sctp_find_vrf(vrf_id);
1273         if (vrf == NULL) {
1274                 return (0);
1275         }
1276         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1277                 struct sctp_ifn *sctp_ifn;
1278                 struct sctp_ifa *sctp_ifa;
1279
1280                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1281                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1282                                 /* Count them if they are the right type */
1283                                 switch (sctp_ifa->address.sa.sa_family) {
1284 #ifdef INET
1285                                 case AF_INET:
1286 #ifdef INET6
1287                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1288                                                 cnt += sizeof(struct sockaddr_in6);
1289                                         else
1290                                                 cnt += sizeof(struct sockaddr_in);
1291 #else
1292                                         cnt += sizeof(struct sockaddr_in);
1293 #endif
1294                                         break;
1295 #endif
1296 #ifdef INET6
1297                                 case AF_INET6:
1298                                         cnt += sizeof(struct sockaddr_in6);
1299                                         break;
1300 #endif
1301                                 default:
1302                                         break;
1303                                 }
1304                         }
1305                 }
1306         } else {
1307                 struct sctp_laddr *laddr;
1308
1309                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1310                         switch (laddr->ifa->address.sa.sa_family) {
1311 #ifdef INET
1312                         case AF_INET:
1313 #ifdef INET6
1314                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1315                                         cnt += sizeof(struct sockaddr_in6);
1316                                 else
1317                                         cnt += sizeof(struct sockaddr_in);
1318 #else
1319                                 cnt += sizeof(struct sockaddr_in);
1320 #endif
1321                                 break;
1322 #endif
1323 #ifdef INET6
1324                         case AF_INET6:
1325                                 cnt += sizeof(struct sockaddr_in6);
1326                                 break;
1327 #endif
1328                         default:
1329                                 break;
1330                         }
1331                 }
1332         }
1333         return (cnt);
1334 }
1335
1336 static int
1337 sctp_count_max_addresses(struct sctp_inpcb *inp)
1338 {
1339         int cnt = 0;
1340
1341         SCTP_IPI_ADDR_RLOCK();
1342         /* count addresses for the endpoint's default VRF */
1343         cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1344         SCTP_IPI_ADDR_RUNLOCK();
1345         return (cnt);
1346 }
1347
1348 static int
1349 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1350     size_t optsize, void *p, int delay)
1351 {
1352         int error = 0;
1353         int creat_lock_on = 0;
1354         struct sctp_tcb *stcb = NULL;
1355         struct sockaddr *sa;
1356         unsigned int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1357         uint32_t vrf_id;
1358         int bad_addresses = 0;
1359         sctp_assoc_t *a_id;
1360
1361         SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
1362
1363         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1364             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1365                 /* We are already connected AND the TCP model */
1366                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
1367                 return (EADDRINUSE);
1368         }
1369
1370         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
1371             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
1372                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1373                 return (EINVAL);
1374         }
1375
1376         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1377                 SCTP_INP_RLOCK(inp);
1378                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1379                 SCTP_INP_RUNLOCK(inp);
1380         }
1381         if (stcb) {
1382                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1383                 return (EALREADY);
1384         }
1385         SCTP_INP_INCR_REF(inp);
1386         SCTP_ASOC_CREATE_LOCK(inp);
1387         creat_lock_on = 1;
1388         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1389             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1390                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
1391                 error = EFAULT;
1392                 goto out_now;
1393         }
1394         totaddrp = (unsigned int *)optval;
1395         totaddr = *totaddrp;
1396         sa = (struct sockaddr *)(totaddrp + 1);
1397         stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (unsigned int)(optsize - sizeof(int)), &bad_addresses);
1398         if ((stcb != NULL) || bad_addresses) {
1399                 /* Already have or am bring up an association */
1400                 SCTP_ASOC_CREATE_UNLOCK(inp);
1401                 creat_lock_on = 0;
1402                 if (stcb)
1403                         SCTP_TCB_UNLOCK(stcb);
1404                 if (bad_addresses == 0) {
1405                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
1406                         error = EALREADY;
1407                 }
1408                 goto out_now;
1409         }
1410 #ifdef INET6
1411         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1412             (num_v6 > 0)) {
1413                 error = EINVAL;
1414                 goto out_now;
1415         }
1416         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1417             (num_v4 > 0)) {
1418                 struct in6pcb *inp6;
1419
1420                 inp6 = (struct in6pcb *)inp;
1421                 if (SCTP_IPV6_V6ONLY(inp6)) {
1422                         /*
1423                          * if IPV6_V6ONLY flag, ignore connections destined
1424                          * to a v4 addr or v4-mapped addr
1425                          */
1426                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1427                         error = EINVAL;
1428                         goto out_now;
1429                 }
1430         }
1431 #endif                          /* INET6 */
1432         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1433             SCTP_PCB_FLAGS_UNBOUND) {
1434                 /* Bind a ephemeral port */
1435                 error = sctp_inpcb_bind(so, NULL, NULL, p);
1436                 if (error) {
1437                         goto out_now;
1438                 }
1439         }
1440
1441         /* FIX ME: do we want to pass in a vrf on the connect call? */
1442         vrf_id = inp->def_vrf_id;
1443
1444
1445         /* We are GOOD to go */
1446         stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1447             inp->sctp_ep.pre_open_stream_count,
1448             inp->sctp_ep.port,
1449             (struct thread *)p
1450             );
1451         if (stcb == NULL) {
1452                 /* Gak! no memory */
1453                 goto out_now;
1454         }
1455         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1456                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1457                 /* Set the connected flag so we can queue data */
1458                 soisconnecting(so);
1459         }
1460         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1461         /* move to second address */
1462         switch (sa->sa_family) {
1463 #ifdef INET
1464         case AF_INET:
1465                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1466                 break;
1467 #endif
1468 #ifdef INET6
1469         case AF_INET6:
1470                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1471                 break;
1472 #endif
1473         default:
1474                 break;
1475         }
1476
1477         error = 0;
1478         sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1479         /* Fill in the return id */
1480         if (error) {
1481                 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
1482                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
1483                 goto out_now;
1484         }
1485         a_id = (sctp_assoc_t *)optval;
1486         *a_id = sctp_get_associd(stcb);
1487
1488         /* initialize authentication parameters for the assoc */
1489         sctp_initialize_auth_params(inp, stcb);
1490
1491         if (delay) {
1492                 /* doing delayed connection */
1493                 stcb->asoc.delayed_connection = 1;
1494                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1495         } else {
1496                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1497                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1498         }
1499         SCTP_TCB_UNLOCK(stcb);
1500 out_now:
1501         if (creat_lock_on) {
1502                 SCTP_ASOC_CREATE_UNLOCK(inp);
1503         }
1504         SCTP_INP_DECR_REF(inp);
1505         return (error);
1506 }
1507
1508 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1509         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1510             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1511                 SCTP_INP_RLOCK(inp); \
1512                 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1513                 if (stcb) { \
1514                         SCTP_TCB_LOCK(stcb); \
1515                 } \
1516                 SCTP_INP_RUNLOCK(inp); \
1517         } else if (assoc_id > SCTP_ALL_ASSOC) { \
1518                 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1519                 if (stcb == NULL) { \
1520                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1521                         error = ENOENT; \
1522                         break; \
1523                 } \
1524         } else { \
1525                 stcb = NULL; \
1526         } \
1527 }
1528
1529
1530 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1531         if (size < sizeof(type)) { \
1532                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1533                 error = EINVAL; \
1534                 break; \
1535         } else { \
1536                 destp = (type *)srcp; \
1537         } \
1538 }
1539
1540 static int
1541 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1542     void *p)
1543 {
1544         struct sctp_inpcb *inp = NULL;
1545         int error, val = 0;
1546         struct sctp_tcb *stcb = NULL;
1547
1548         if (optval == NULL) {
1549                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1550                 return (EINVAL);
1551         }
1552
1553         inp = (struct sctp_inpcb *)so->so_pcb;
1554         if (inp == NULL) {
1555                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1556                 return EINVAL;
1557         }
1558         error = 0;
1559
1560         switch (optname) {
1561         case SCTP_NODELAY:
1562         case SCTP_AUTOCLOSE:
1563         case SCTP_EXPLICIT_EOR:
1564         case SCTP_AUTO_ASCONF:
1565         case SCTP_DISABLE_FRAGMENTS:
1566         case SCTP_I_WANT_MAPPED_V4_ADDR:
1567         case SCTP_USE_EXT_RCVINFO:
1568                 SCTP_INP_RLOCK(inp);
1569                 switch (optname) {
1570                 case SCTP_DISABLE_FRAGMENTS:
1571                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1572                         break;
1573                 case SCTP_I_WANT_MAPPED_V4_ADDR:
1574                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1575                         break;
1576                 case SCTP_AUTO_ASCONF:
1577                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1578                                 /* only valid for bound all sockets */
1579                                 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1580                         } else {
1581                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1582                                 error = EINVAL;
1583                                 goto flags_out;
1584                         }
1585                         break;
1586                 case SCTP_EXPLICIT_EOR:
1587                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1588                         break;
1589                 case SCTP_NODELAY:
1590                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1591                         break;
1592                 case SCTP_USE_EXT_RCVINFO:
1593                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1594                         break;
1595                 case SCTP_AUTOCLOSE:
1596                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1597                                 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1598                         else
1599                                 val = 0;
1600                         break;
1601
1602                 default:
1603                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1604                         error = ENOPROTOOPT;
1605                 }               /* end switch (sopt->sopt_name) */
1606                 if (*optsize < sizeof(val)) {
1607                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1608                         error = EINVAL;
1609                 }
1610 flags_out:
1611                 SCTP_INP_RUNLOCK(inp);
1612                 if (error == 0) {
1613                         /* return the option value */
1614                         *(int *)optval = val;
1615                         *optsize = sizeof(val);
1616                 }
1617                 break;
1618         case SCTP_GET_PACKET_LOG:
1619                 {
1620 #ifdef  SCTP_PACKET_LOGGING
1621                         uint8_t *target;
1622                         int ret;
1623
1624                         SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1625                         ret = sctp_copy_out_packet_log(target, (int)*optsize);
1626                         *optsize = ret;
1627 #else
1628                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1629                         error = EOPNOTSUPP;
1630 #endif
1631                         break;
1632                 }
1633         case SCTP_REUSE_PORT:
1634                 {
1635                         uint32_t *value;
1636
1637                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1638                                 /* Can't do this for a 1-m socket */
1639                                 error = EINVAL;
1640                                 break;
1641                         }
1642                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1643                         *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1644                         *optsize = sizeof(uint32_t);
1645                         break;
1646                 }
1647         case SCTP_PARTIAL_DELIVERY_POINT:
1648                 {
1649                         uint32_t *value;
1650
1651                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1652                         *value = inp->partial_delivery_point;
1653                         *optsize = sizeof(uint32_t);
1654                         break;
1655                 }
1656         case SCTP_FRAGMENT_INTERLEAVE:
1657                 {
1658                         uint32_t *value;
1659
1660                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1661                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1662                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1663                                         *value = SCTP_FRAG_LEVEL_2;
1664                                 } else {
1665                                         *value = SCTP_FRAG_LEVEL_1;
1666                                 }
1667                         } else {
1668                                 *value = SCTP_FRAG_LEVEL_0;
1669                         }
1670                         *optsize = sizeof(uint32_t);
1671                         break;
1672                 }
1673         case SCTP_INTERLEAVING_SUPPORTED:
1674                 {
1675                         struct sctp_assoc_value *av;
1676
1677                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1678                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1679
1680                         if (stcb) {
1681                                 av->assoc_value = stcb->asoc.idata_supported;
1682                                 SCTP_TCB_UNLOCK(stcb);
1683                         } else {
1684                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1685                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1686                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1687                                         SCTP_INP_RLOCK(inp);
1688                                         if (inp->idata_supported) {
1689                                                 av->assoc_value = 1;
1690                                         } else {
1691                                                 av->assoc_value = 0;
1692                                         }
1693                                         SCTP_INP_RUNLOCK(inp);
1694                                 } else {
1695                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1696                                         error = EINVAL;
1697                                 }
1698                         }
1699                         if (error == 0) {
1700                                 *optsize = sizeof(struct sctp_assoc_value);
1701                         }
1702                         break;
1703                 }
1704         case SCTP_CMT_ON_OFF:
1705                 {
1706                         struct sctp_assoc_value *av;
1707
1708                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1709                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1710                         if (stcb) {
1711                                 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1712                                 SCTP_TCB_UNLOCK(stcb);
1713                         } else {
1714                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1715                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1716                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1717                                         SCTP_INP_RLOCK(inp);
1718                                         av->assoc_value = inp->sctp_cmt_on_off;
1719                                         SCTP_INP_RUNLOCK(inp);
1720                                 } else {
1721                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1722                                         error = EINVAL;
1723                                 }
1724                         }
1725                         if (error == 0) {
1726                                 *optsize = sizeof(struct sctp_assoc_value);
1727                         }
1728                         break;
1729                 }
1730         case SCTP_PLUGGABLE_CC:
1731                 {
1732                         struct sctp_assoc_value *av;
1733
1734                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1735                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1736                         if (stcb) {
1737                                 av->assoc_value = stcb->asoc.congestion_control_module;
1738                                 SCTP_TCB_UNLOCK(stcb);
1739                         } else {
1740                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1741                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1742                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1743                                         SCTP_INP_RLOCK(inp);
1744                                         av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1745                                         SCTP_INP_RUNLOCK(inp);
1746                                 } else {
1747                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1748                                         error = EINVAL;
1749                                 }
1750                         }
1751                         if (error == 0) {
1752                                 *optsize = sizeof(struct sctp_assoc_value);
1753                         }
1754                         break;
1755                 }
1756         case SCTP_CC_OPTION:
1757                 {
1758                         struct sctp_cc_option *cc_opt;
1759
1760                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1761                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1762                         if (stcb == NULL) {
1763                                 error = EINVAL;
1764                         } else {
1765                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1766                                         error = ENOTSUP;
1767                                 } else {
1768                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt);
1769                                         *optsize = sizeof(struct sctp_cc_option);
1770                                 }
1771                                 SCTP_TCB_UNLOCK(stcb);
1772                         }
1773                         break;
1774                 }
1775         case SCTP_PLUGGABLE_SS:
1776                 {
1777                         struct sctp_assoc_value *av;
1778
1779                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1780                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1781                         if (stcb) {
1782                                 av->assoc_value = stcb->asoc.stream_scheduling_module;
1783                                 SCTP_TCB_UNLOCK(stcb);
1784                         } else {
1785                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1786                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1787                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1788                                         SCTP_INP_RLOCK(inp);
1789                                         av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1790                                         SCTP_INP_RUNLOCK(inp);
1791                                 } else {
1792                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1793                                         error = EINVAL;
1794                                 }
1795                         }
1796                         if (error == 0) {
1797                                 *optsize = sizeof(struct sctp_assoc_value);
1798                         }
1799                         break;
1800                 }
1801         case SCTP_SS_VALUE:
1802                 {
1803                         struct sctp_stream_value *av;
1804
1805                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1806                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1807                         if (stcb) {
1808                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
1809                                     (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1810                                     &av->stream_value) < 0)) {
1811                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1812                                         error = EINVAL;
1813                                 } else {
1814                                         *optsize = sizeof(struct sctp_stream_value);
1815                                 }
1816                                 SCTP_TCB_UNLOCK(stcb);
1817                         } else {
1818                                 /*
1819                                  * Can't get stream value without
1820                                  * association
1821                                  */
1822                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1823                                 error = EINVAL;
1824                         }
1825                         break;
1826                 }
1827         case SCTP_GET_ADDR_LEN:
1828                 {
1829                         struct sctp_assoc_value *av;
1830
1831                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1832                         error = EINVAL;
1833 #ifdef INET
1834                         if (av->assoc_value == AF_INET) {
1835                                 av->assoc_value = sizeof(struct sockaddr_in);
1836                                 error = 0;
1837                         }
1838 #endif
1839 #ifdef INET6
1840                         if (av->assoc_value == AF_INET6) {
1841                                 av->assoc_value = sizeof(struct sockaddr_in6);
1842                                 error = 0;
1843                         }
1844 #endif
1845                         if (error) {
1846                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1847                         } else {
1848                                 *optsize = sizeof(struct sctp_assoc_value);
1849                         }
1850                         break;
1851                 }
1852         case SCTP_GET_ASSOC_NUMBER:
1853                 {
1854                         uint32_t *value, cnt;
1855
1856                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1857                         SCTP_INP_RLOCK(inp);
1858                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1859                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1860                                 /* Can't do this for a 1-1 socket */
1861                                 error = EINVAL;
1862                                 SCTP_INP_RUNLOCK(inp);
1863                                 break;
1864                         }
1865                         cnt = 0;
1866                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1867                                 cnt++;
1868                         }
1869                         SCTP_INP_RUNLOCK(inp);
1870                         *value = cnt;
1871                         *optsize = sizeof(uint32_t);
1872                         break;
1873                 }
1874         case SCTP_GET_ASSOC_ID_LIST:
1875                 {
1876                         struct sctp_assoc_ids *ids;
1877                         uint32_t at;
1878                         size_t limit;
1879
1880                         SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1881                         SCTP_INP_RLOCK(inp);
1882                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1883                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1884                                 /* Can't do this for a 1-1 socket */
1885                                 error = EINVAL;
1886                                 SCTP_INP_RUNLOCK(inp);
1887                                 break;
1888                         }
1889                         at = 0;
1890                         limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1891                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1892                                 if (at < limit) {
1893                                         ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1894                                         if (at == 0) {
1895                                                 error = EINVAL;
1896                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1897                                                 break;
1898                                         }
1899                                 } else {
1900                                         error = EINVAL;
1901                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1902                                         break;
1903                                 }
1904                         }
1905                         SCTP_INP_RUNLOCK(inp);
1906                         if (error == 0) {
1907                                 ids->gaids_number_of_ids = at;
1908                                 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1909                         }
1910                         break;
1911                 }
1912         case SCTP_CONTEXT:
1913                 {
1914                         struct sctp_assoc_value *av;
1915
1916                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1917                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1918
1919                         if (stcb) {
1920                                 av->assoc_value = stcb->asoc.context;
1921                                 SCTP_TCB_UNLOCK(stcb);
1922                         } else {
1923                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1924                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1925                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1926                                         SCTP_INP_RLOCK(inp);
1927                                         av->assoc_value = inp->sctp_context;
1928                                         SCTP_INP_RUNLOCK(inp);
1929                                 } else {
1930                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1931                                         error = EINVAL;
1932                                 }
1933                         }
1934                         if (error == 0) {
1935                                 *optsize = sizeof(struct sctp_assoc_value);
1936                         }
1937                         break;
1938                 }
1939         case SCTP_VRF_ID:
1940                 {
1941                         uint32_t *default_vrfid;
1942
1943                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1944                         *default_vrfid = inp->def_vrf_id;
1945                         *optsize = sizeof(uint32_t);
1946                         break;
1947                 }
1948         case SCTP_GET_ASOC_VRF:
1949                 {
1950                         struct sctp_assoc_value *id;
1951
1952                         SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1953                         SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1954                         if (stcb == NULL) {
1955                                 error = EINVAL;
1956                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1957                         } else {
1958                                 id->assoc_value = stcb->asoc.vrf_id;
1959                                 SCTP_TCB_UNLOCK(stcb);
1960                                 *optsize = sizeof(struct sctp_assoc_value);
1961                         }
1962                         break;
1963                 }
1964         case SCTP_GET_VRF_IDS:
1965                 {
1966                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1967                         error = EOPNOTSUPP;
1968                         break;
1969                 }
1970         case SCTP_GET_NONCE_VALUES:
1971                 {
1972                         struct sctp_get_nonce_values *gnv;
1973
1974                         SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1975                         SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1976
1977                         if (stcb) {
1978                                 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1979                                 gnv->gn_local_tag = stcb->asoc.my_vtag;
1980                                 SCTP_TCB_UNLOCK(stcb);
1981                                 *optsize = sizeof(struct sctp_get_nonce_values);
1982                         } else {
1983                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1984                                 error = ENOTCONN;
1985                         }
1986                         break;
1987                 }
1988         case SCTP_DELAYED_SACK:
1989                 {
1990                         struct sctp_sack_info *sack;
1991
1992                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1993                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1994                         if (stcb) {
1995                                 sack->sack_delay = stcb->asoc.delayed_ack;
1996                                 sack->sack_freq = stcb->asoc.sack_freq;
1997                                 SCTP_TCB_UNLOCK(stcb);
1998                         } else {
1999                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2000                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2001                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
2002                                         SCTP_INP_RLOCK(inp);
2003                                         sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2004                                         sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2005                                         SCTP_INP_RUNLOCK(inp);
2006                                 } else {
2007                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2008                                         error = EINVAL;
2009                                 }
2010                         }
2011                         if (error == 0) {
2012                                 *optsize = sizeof(struct sctp_sack_info);
2013                         }
2014                         break;
2015                 }
2016         case SCTP_GET_SNDBUF_USE:
2017                 {
2018                         struct sctp_sockstat *ss;
2019
2020                         SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2021                         SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2022
2023                         if (stcb) {
2024                                 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2025                                 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2026                                     stcb->asoc.size_on_all_streams);
2027                                 SCTP_TCB_UNLOCK(stcb);
2028                                 *optsize = sizeof(struct sctp_sockstat);
2029                         } else {
2030                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2031                                 error = ENOTCONN;
2032                         }
2033                         break;
2034                 }
2035         case SCTP_MAX_BURST:
2036                 {
2037                         struct sctp_assoc_value *av;
2038
2039                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2040                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2041
2042                         if (stcb) {
2043                                 av->assoc_value = stcb->asoc.max_burst;
2044                                 SCTP_TCB_UNLOCK(stcb);
2045                         } else {
2046                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2047                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2048                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2049                                         SCTP_INP_RLOCK(inp);
2050                                         av->assoc_value = inp->sctp_ep.max_burst;
2051                                         SCTP_INP_RUNLOCK(inp);
2052                                 } else {
2053                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2054                                         error = EINVAL;
2055                                 }
2056                         }
2057                         if (error == 0) {
2058                                 *optsize = sizeof(struct sctp_assoc_value);
2059                         }
2060                         break;
2061                 }
2062         case SCTP_MAXSEG:
2063                 {
2064                         struct sctp_assoc_value *av;
2065                         int ovh;
2066
2067                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2068                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2069
2070                         if (stcb) {
2071                                 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2072                                 SCTP_TCB_UNLOCK(stcb);
2073                         } else {
2074                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2075                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2076                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2077                                         SCTP_INP_RLOCK(inp);
2078                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2079                                                 ovh = SCTP_MED_OVERHEAD;
2080                                         } else {
2081                                                 ovh = SCTP_MED_V4_OVERHEAD;
2082                                         }
2083                                         if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2084                                                 av->assoc_value = 0;
2085                                         else
2086                                                 av->assoc_value = inp->sctp_frag_point - ovh;
2087                                         SCTP_INP_RUNLOCK(inp);
2088                                 } else {
2089                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2090                                         error = EINVAL;
2091                                 }
2092                         }
2093                         if (error == 0) {
2094                                 *optsize = sizeof(struct sctp_assoc_value);
2095                         }
2096                         break;
2097                 }
2098         case SCTP_GET_STAT_LOG:
2099                 error = sctp_fill_stat_log(optval, optsize);
2100                 break;
2101         case SCTP_EVENTS:
2102                 {
2103                         struct sctp_event_subscribe *events;
2104
2105                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2106                         memset(events, 0, sizeof(struct sctp_event_subscribe));
2107                         SCTP_INP_RLOCK(inp);
2108                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2109                                 events->sctp_data_io_event = 1;
2110
2111                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2112                                 events->sctp_association_event = 1;
2113
2114                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2115                                 events->sctp_address_event = 1;
2116
2117                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2118                                 events->sctp_send_failure_event = 1;
2119
2120                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2121                                 events->sctp_peer_error_event = 1;
2122
2123                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2124                                 events->sctp_shutdown_event = 1;
2125
2126                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2127                                 events->sctp_partial_delivery_event = 1;
2128
2129                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2130                                 events->sctp_adaptation_layer_event = 1;
2131
2132                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2133                                 events->sctp_authentication_event = 1;
2134
2135                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2136                                 events->sctp_sender_dry_event = 1;
2137
2138                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2139                                 events->sctp_stream_reset_event = 1;
2140                         SCTP_INP_RUNLOCK(inp);
2141                         *optsize = sizeof(struct sctp_event_subscribe);
2142                         break;
2143                 }
2144         case SCTP_ADAPTATION_LAYER:
2145                 {
2146                         uint32_t *value;
2147
2148                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2149
2150                         SCTP_INP_RLOCK(inp);
2151                         *value = inp->sctp_ep.adaptation_layer_indicator;
2152                         SCTP_INP_RUNLOCK(inp);
2153                         *optsize = sizeof(uint32_t);
2154                         break;
2155                 }
2156         case SCTP_SET_INITIAL_DBG_SEQ:
2157                 {
2158                         uint32_t *value;
2159
2160                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2161                         SCTP_INP_RLOCK(inp);
2162                         *value = inp->sctp_ep.initial_sequence_debug;
2163                         SCTP_INP_RUNLOCK(inp);
2164                         *optsize = sizeof(uint32_t);
2165                         break;
2166                 }
2167         case SCTP_GET_LOCAL_ADDR_SIZE:
2168                 {
2169                         uint32_t *value;
2170
2171                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2172                         SCTP_INP_RLOCK(inp);
2173                         *value = sctp_count_max_addresses(inp);
2174                         SCTP_INP_RUNLOCK(inp);
2175                         *optsize = sizeof(uint32_t);
2176                         break;
2177                 }
2178         case SCTP_GET_REMOTE_ADDR_SIZE:
2179                 {
2180                         uint32_t *value;
2181                         size_t size;
2182                         struct sctp_nets *net;
2183
2184                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2185                         /* FIXME MT: change to sctp_assoc_value? */
2186                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t)*value);
2187
2188                         if (stcb) {
2189                                 size = 0;
2190                                 /* Count the sizes */
2191                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2192                                         switch (net->ro._l_addr.sa.sa_family) {
2193 #ifdef INET
2194                                         case AF_INET:
2195 #ifdef INET6
2196                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2197                                                         size += sizeof(struct sockaddr_in6);
2198                                                 } else {
2199                                                         size += sizeof(struct sockaddr_in);
2200                                                 }
2201 #else
2202                                                 size += sizeof(struct sockaddr_in);
2203 #endif
2204                                                 break;
2205 #endif
2206 #ifdef INET6
2207                                         case AF_INET6:
2208                                                 size += sizeof(struct sockaddr_in6);
2209                                                 break;
2210 #endif
2211                                         default:
2212                                                 break;
2213                                         }
2214                                 }
2215                                 SCTP_TCB_UNLOCK(stcb);
2216                                 *value = (uint32_t)size;
2217                                 *optsize = sizeof(uint32_t);
2218                         } else {
2219                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2220                                 error = ENOTCONN;
2221                         }
2222                         break;
2223                 }
2224         case SCTP_GET_PEER_ADDRESSES:
2225                 /*
2226                  * Get the address information, an array is passed in to
2227                  * fill up we pack it.
2228                  */
2229                 {
2230                         size_t cpsz, left;
2231                         struct sockaddr_storage *sas;
2232                         struct sctp_nets *net;
2233                         struct sctp_getaddresses *saddr;
2234
2235                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2236                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2237
2238                         if (stcb) {
2239                                 left = (*optsize) - sizeof(struct sctp_getaddresses);
2240                                 *optsize = sizeof(struct sctp_getaddresses);
2241                                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2242
2243                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2244                                         switch (net->ro._l_addr.sa.sa_family) {
2245 #ifdef INET
2246                                         case AF_INET:
2247 #ifdef INET6
2248                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2249                                                         cpsz = sizeof(struct sockaddr_in6);
2250                                                 } else {
2251                                                         cpsz = sizeof(struct sockaddr_in);
2252                                                 }
2253 #else
2254                                                 cpsz = sizeof(struct sockaddr_in);
2255 #endif
2256                                                 break;
2257 #endif
2258 #ifdef INET6
2259                                         case AF_INET6:
2260                                                 cpsz = sizeof(struct sockaddr_in6);
2261                                                 break;
2262 #endif
2263                                         default:
2264                                                 cpsz = 0;
2265                                                 break;
2266                                         }
2267                                         if (cpsz == 0) {
2268                                                 break;
2269                                         }
2270                                         if (left < cpsz) {
2271                                                 /* not enough room. */
2272                                                 break;
2273                                         }
2274 #if defined(INET) && defined(INET6)
2275                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2276                                             (net->ro._l_addr.sa.sa_family == AF_INET)) {
2277                                                 /* Must map the address */
2278                                                 in6_sin_2_v4mapsin6(&net->ro._l_addr.sin,
2279                                                     (struct sockaddr_in6 *)sas);
2280                                         } else {
2281                                                 memcpy(sas, &net->ro._l_addr, cpsz);
2282                                         }
2283 #else
2284                                         memcpy(sas, &net->ro._l_addr, cpsz);
2285 #endif
2286                                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2287
2288                                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2289                                         left -= cpsz;
2290                                         *optsize += cpsz;
2291                                 }
2292                                 SCTP_TCB_UNLOCK(stcb);
2293                         } else {
2294                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2295                                 error = ENOENT;
2296                         }
2297                         break;
2298                 }
2299         case SCTP_GET_LOCAL_ADDRESSES:
2300                 {
2301                         size_t limit, actual;
2302                         struct sockaddr_storage *sas;
2303                         struct sctp_getaddresses *saddr;
2304
2305                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2306                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2307
2308                         sas = (struct sockaddr_storage *)&saddr->addr[0];
2309                         limit = *optsize - sizeof(sctp_assoc_t);
2310                         actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2311                         if (stcb) {
2312                                 SCTP_TCB_UNLOCK(stcb);
2313                         }
2314                         *optsize = sizeof(struct sockaddr_storage) + actual;
2315                         break;
2316                 }
2317         case SCTP_PEER_ADDR_PARAMS:
2318                 {
2319                         struct sctp_paddrparams *paddrp;
2320                         struct sctp_nets *net;
2321                         struct sockaddr *addr;
2322 #if defined(INET) && defined(INET6)
2323                         struct sockaddr_in sin_store;
2324 #endif
2325
2326                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2327                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2328
2329 #if defined(INET) && defined(INET6)
2330                         if (paddrp->spp_address.ss_family == AF_INET6) {
2331                                 struct sockaddr_in6 *sin6;
2332
2333                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
2334                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2335                                         in6_sin6_2_sin(&sin_store, sin6);
2336                                         addr = (struct sockaddr *)&sin_store;
2337                                 } else {
2338                                         addr = (struct sockaddr *)&paddrp->spp_address;
2339                                 }
2340                         } else {
2341                                 addr = (struct sockaddr *)&paddrp->spp_address;
2342                         }
2343 #else
2344                         addr = (struct sockaddr *)&paddrp->spp_address;
2345 #endif
2346                         if (stcb != NULL) {
2347                                 net = sctp_findnet(stcb, addr);
2348                         } else {
2349                                 /*
2350                                  * We increment here since
2351                                  * sctp_findassociation_ep_addr() wil do a
2352                                  * decrement if it finds the stcb as long as
2353                                  * the locked tcb (last argument) is NOT a
2354                                  * TCB.. aka NULL.
2355                                  */
2356                                 net = NULL;
2357                                 SCTP_INP_INCR_REF(inp);
2358                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2359                                 if (stcb == NULL) {
2360                                         SCTP_INP_DECR_REF(inp);
2361                                 }
2362                         }
2363                         if ((stcb != NULL) && (net == NULL)) {
2364 #ifdef INET
2365                                 if (addr->sa_family == AF_INET) {
2366                                         struct sockaddr_in *sin;
2367
2368                                         sin = (struct sockaddr_in *)addr;
2369                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
2370                                                 error = EINVAL;
2371                                                 SCTP_TCB_UNLOCK(stcb);
2372                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2373                                                 break;
2374                                         }
2375                                 } else
2376 #endif
2377 #ifdef INET6
2378                                 if (addr->sa_family == AF_INET6) {
2379                                         struct sockaddr_in6 *sin6;
2380
2381                                         sin6 = (struct sockaddr_in6 *)addr;
2382                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2383                                                 error = EINVAL;
2384                                                 SCTP_TCB_UNLOCK(stcb);
2385                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2386                                                 break;
2387                                         }
2388                                 } else
2389 #endif
2390                                 {
2391                                         error = EAFNOSUPPORT;
2392                                         SCTP_TCB_UNLOCK(stcb);
2393                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2394                                         break;
2395                                 }
2396                         }
2397
2398                         if (stcb != NULL) {
2399                                 /* Applies to the specific association */
2400                                 paddrp->spp_flags = 0;
2401                                 if (net != NULL) {
2402                                         paddrp->spp_hbinterval = net->heart_beat_delay;
2403                                         paddrp->spp_pathmaxrxt = net->failure_threshold;
2404                                         paddrp->spp_pathmtu = net->mtu;
2405                                         switch (net->ro._l_addr.sa.sa_family) {
2406 #ifdef INET
2407                                         case AF_INET:
2408                                                 paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2409                                                 break;
2410 #endif
2411 #ifdef INET6
2412                                         case AF_INET6:
2413                                                 paddrp->spp_pathmtu -= SCTP_MIN_OVERHEAD;
2414                                                 break;
2415 #endif
2416                                         default:
2417                                                 break;
2418                                         }
2419                                         /* get flags for HB */
2420                                         if (net->dest_state & SCTP_ADDR_NOHB) {
2421                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2422                                         } else {
2423                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2424                                         }
2425                                         /* get flags for PMTU */
2426                                         if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2427                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2428                                         } else {
2429                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2430                                         }
2431                                         if (net->dscp & 0x01) {
2432                                                 paddrp->spp_dscp = net->dscp & 0xfc;
2433                                                 paddrp->spp_flags |= SPP_DSCP;
2434                                         }
2435 #ifdef INET6
2436                                         if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2437                                             (net->flowlabel & 0x80000000)) {
2438                                                 paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2439                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2440                                         }
2441 #endif
2442                                 } else {
2443                                         /*
2444                                          * No destination so return default
2445                                          * value
2446                                          */
2447                                         paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2448                                         paddrp->spp_pathmtu = stcb->asoc.default_mtu;
2449                                         if (stcb->asoc.default_dscp & 0x01) {
2450                                                 paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2451                                                 paddrp->spp_flags |= SPP_DSCP;
2452                                         }
2453 #ifdef INET6
2454                                         if (stcb->asoc.default_flowlabel & 0x80000000) {
2455                                                 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2456                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2457                                         }
2458 #endif
2459                                         /* default settings should be these */
2460                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2461                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2462                                         } else {
2463                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2464                                         }
2465                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2466                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2467                                         } else {
2468                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2469                                         }
2470                                         paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2471                                 }
2472                                 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2473                                 SCTP_TCB_UNLOCK(stcb);
2474                         } else {
2475                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2476                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2477                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2478                                         /* Use endpoint defaults */
2479                                         SCTP_INP_RLOCK(inp);
2480                                         paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2481                                         paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2482                                         paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2483                                         /* get inp's default */
2484                                         if (inp->sctp_ep.default_dscp & 0x01) {
2485                                                 paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2486                                                 paddrp->spp_flags |= SPP_DSCP;
2487                                         }
2488 #ifdef INET6
2489                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2490                                             (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2491                                                 paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2492                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2493                                         }
2494 #endif
2495                                         paddrp->spp_pathmtu = inp->sctp_ep.default_mtu;
2496
2497                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2498                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2499                                         } else {
2500                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2501                                         }
2502                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2503                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2504                                         } else {
2505                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2506                                         }
2507                                         SCTP_INP_RUNLOCK(inp);
2508                                 } else {
2509                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2510                                         error = EINVAL;
2511                                 }
2512                         }
2513                         if (error == 0) {
2514                                 *optsize = sizeof(struct sctp_paddrparams);
2515                         }
2516                         break;
2517                 }
2518         case SCTP_GET_PEER_ADDR_INFO:
2519                 {
2520                         struct sctp_paddrinfo *paddri;
2521                         struct sctp_nets *net;
2522                         struct sockaddr *addr;
2523 #if defined(INET) && defined(INET6)
2524                         struct sockaddr_in sin_store;
2525 #endif
2526
2527                         SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2528                         SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2529
2530 #if defined(INET) && defined(INET6)
2531                         if (paddri->spinfo_address.ss_family == AF_INET6) {
2532                                 struct sockaddr_in6 *sin6;
2533
2534                                 sin6 = (struct sockaddr_in6 *)&paddri->spinfo_address;
2535                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2536                                         in6_sin6_2_sin(&sin_store, sin6);
2537                                         addr = (struct sockaddr *)&sin_store;
2538                                 } else {
2539                                         addr = (struct sockaddr *)&paddri->spinfo_address;
2540                                 }
2541                         } else {
2542                                 addr = (struct sockaddr *)&paddri->spinfo_address;
2543                         }
2544 #else
2545                         addr = (struct sockaddr *)&paddri->spinfo_address;
2546 #endif
2547                         if (stcb != NULL) {
2548                                 net = sctp_findnet(stcb, addr);
2549                         } else {
2550                                 /*
2551                                  * We increment here since
2552                                  * sctp_findassociation_ep_addr() wil do a
2553                                  * decrement if it finds the stcb as long as
2554                                  * the locked tcb (last argument) is NOT a
2555                                  * TCB.. aka NULL.
2556                                  */
2557                                 net = NULL;
2558                                 SCTP_INP_INCR_REF(inp);
2559                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2560                                 if (stcb == NULL) {
2561                                         SCTP_INP_DECR_REF(inp);
2562                                 }
2563                         }
2564
2565                         if ((stcb != NULL) && (net != NULL)) {
2566                                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2567                                         /* It's unconfirmed */
2568                                         paddri->spinfo_state = SCTP_UNCONFIRMED;
2569                                 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2570                                         /* It's active */
2571                                         paddri->spinfo_state = SCTP_ACTIVE;
2572                                 } else {
2573                                         /* It's inactive */
2574                                         paddri->spinfo_state = SCTP_INACTIVE;
2575                                 }
2576                                 paddri->spinfo_cwnd = net->cwnd;
2577                                 paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2578                                 paddri->spinfo_rto = net->RTO;
2579                                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2580                                 paddri->spinfo_mtu = net->mtu;
2581                                 switch (addr->sa_family) {
2582 #if defined(INET)
2583                                 case AF_INET:
2584                                         paddri->spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2585                                         break;
2586 #endif
2587 #if defined(INET6)
2588                                 case AF_INET6:
2589                                         paddri->spinfo_mtu -= SCTP_MIN_OVERHEAD;
2590                                         break;
2591 #endif
2592                                 default:
2593                                         break;
2594                                 }
2595                                 SCTP_TCB_UNLOCK(stcb);
2596                                 *optsize = sizeof(struct sctp_paddrinfo);
2597                         } else {
2598                                 if (stcb != NULL) {
2599                                         SCTP_TCB_UNLOCK(stcb);
2600                                 }
2601                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2602                                 error = ENOENT;
2603                         }
2604                         break;
2605                 }
2606         case SCTP_PCB_STATUS:
2607                 {
2608                         struct sctp_pcbinfo *spcb;
2609
2610                         SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2611                         sctp_fill_pcbinfo(spcb);
2612                         *optsize = sizeof(struct sctp_pcbinfo);
2613                         break;
2614                 }
2615         case SCTP_STATUS:
2616                 {
2617                         struct sctp_nets *net;
2618                         struct sctp_status *sstat;
2619
2620                         SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2621                         SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2622
2623                         if (stcb == NULL) {
2624                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2625                                 error = EINVAL;
2626                                 break;
2627                         }
2628                         sstat->sstat_state = sctp_map_assoc_state(stcb->asoc.state);
2629                         sstat->sstat_assoc_id = sctp_get_associd(stcb);
2630                         sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2631                         sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2632                         /*
2633                          * We can't include chunks that have been passed to
2634                          * the socket layer. Only things in queue.
2635                          */
2636                         sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2637                             stcb->asoc.cnt_on_all_streams);
2638
2639
2640                         sstat->sstat_instrms = stcb->asoc.streamincnt;
2641                         sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2642                         sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2643                         memcpy(&sstat->sstat_primary.spinfo_address,
2644                             &stcb->asoc.primary_destination->ro._l_addr,
2645                             ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2646                         net = stcb->asoc.primary_destination;
2647                         ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2648                         /*
2649                          * Again the user can get info from sctp_constants.h
2650                          * for what the state of the network is.
2651                          */
2652                         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2653                                 /* It's unconfirmed */
2654                                 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2655                         } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2656                                 /* It's active */
2657                                 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2658                         } else {
2659                                 /* It's inactive */
2660                                 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2661                         }
2662                         sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2663                         sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2664                         sstat->sstat_primary.spinfo_rto = net->RTO;
2665                         sstat->sstat_primary.spinfo_mtu = net->mtu;
2666                         switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
2667 #if defined(INET)
2668                         case AF_INET:
2669                                 sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2670                                 break;
2671 #endif
2672 #if defined(INET6)
2673                         case AF_INET6:
2674                                 sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_OVERHEAD;
2675                                 break;
2676 #endif
2677                         default:
2678                                 break;
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_PRINTF("optval is NULL\n");
3746                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3747                 return (EINVAL);
3748         }
3749         inp = (struct sctp_inpcb *)so->so_pcb;
3750         if (inp == NULL) {
3751                 SCTP_PRINTF("inp is NULL?\n");
3752                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3753                 return (EINVAL);
3754         }
3755         vrf_id = inp->def_vrf_id;
3756
3757         error = 0;
3758         switch (optname) {
3759         case SCTP_NODELAY:
3760         case SCTP_AUTOCLOSE:
3761         case SCTP_AUTO_ASCONF:
3762         case SCTP_EXPLICIT_EOR:
3763         case SCTP_DISABLE_FRAGMENTS:
3764         case SCTP_USE_EXT_RCVINFO:
3765         case SCTP_I_WANT_MAPPED_V4_ADDR:
3766                 /* copy in the option value */
3767                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3768                 set_opt = 0;
3769                 if (error)
3770                         break;
3771                 switch (optname) {
3772                 case SCTP_DISABLE_FRAGMENTS:
3773                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3774                         break;
3775                 case SCTP_AUTO_ASCONF:
3776                         /*
3777                          * NOTE: we don't really support this flag
3778                          */
3779                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3780                                 /* only valid for bound all sockets */
3781                                 if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3782                                     (*mopt != 0)) {
3783                                         /* forbidden by admin */
3784                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3785                                         return (EPERM);
3786                                 }
3787                                 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3788                         } else {
3789                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3790                                 return (EINVAL);
3791                         }
3792                         break;
3793                 case SCTP_EXPLICIT_EOR:
3794                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3795                         break;
3796                 case SCTP_USE_EXT_RCVINFO:
3797                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3798                         break;
3799                 case SCTP_I_WANT_MAPPED_V4_ADDR:
3800                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3801                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3802                         } else {
3803                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3804                                 return (EINVAL);
3805                         }
3806                         break;
3807                 case SCTP_NODELAY:
3808                         set_opt = SCTP_PCB_FLAGS_NODELAY;
3809                         break;
3810                 case SCTP_AUTOCLOSE:
3811                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3812                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3813                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3814                                 return (EINVAL);
3815                         }
3816                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3817                         /*
3818                          * The value is in ticks. Note this does not effect
3819                          * old associations, only new ones.
3820                          */
3821                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3822                         break;
3823                 }
3824                 SCTP_INP_WLOCK(inp);
3825                 if (*mopt != 0) {
3826                         sctp_feature_on(inp, set_opt);
3827                 } else {
3828                         sctp_feature_off(inp, set_opt);
3829                 }
3830                 SCTP_INP_WUNLOCK(inp);
3831                 break;
3832         case SCTP_REUSE_PORT:
3833                 {
3834                         SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3835                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3836                                 /* Can't set it after we are bound */
3837                                 error = EINVAL;
3838                                 break;
3839                         }
3840                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3841                                 /* Can't do this for a 1-m socket */
3842                                 error = EINVAL;
3843                                 break;
3844                         }
3845                         if (optval)
3846                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3847                         else
3848                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3849                         break;
3850                 }
3851         case SCTP_PARTIAL_DELIVERY_POINT:
3852                 {
3853                         uint32_t *value;
3854
3855                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3856                         if (*value > SCTP_SB_LIMIT_RCV(so)) {
3857                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3858                                 error = EINVAL;
3859                                 break;
3860                         }
3861                         inp->partial_delivery_point = *value;
3862                         break;
3863                 }
3864         case SCTP_FRAGMENT_INTERLEAVE:
3865                 /* not yet until we re-write sctp_recvmsg() */
3866                 {
3867                         uint32_t *level;
3868
3869                         SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3870                         if (*level == SCTP_FRAG_LEVEL_2) {
3871                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3872                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3873                         } else if (*level == SCTP_FRAG_LEVEL_1) {
3874                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3875                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3876                         } else if (*level == SCTP_FRAG_LEVEL_0) {
3877                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3878                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3879
3880                         } else {
3881                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3882                                 error = EINVAL;
3883                         }
3884                         break;
3885                 }
3886         case SCTP_INTERLEAVING_SUPPORTED:
3887                 {
3888                         struct sctp_assoc_value *av;
3889
3890                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3891                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3892
3893                         if (stcb) {
3894                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3895                                 error = EINVAL;
3896                                 SCTP_TCB_UNLOCK(stcb);
3897                         } else {
3898                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3899                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3900                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3901                                         SCTP_INP_WLOCK(inp);
3902                                         if (av->assoc_value == 0) {
3903                                                 inp->idata_supported = 0;
3904                                         } else {
3905                                                 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
3906                                                     (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS))) {
3907                                                         inp->idata_supported = 1;
3908                                                 } else {
3909                                                         /*
3910                                                          * Must have Frag
3911                                                          * interleave and
3912                                                          * stream interleave
3913                                                          * on
3914                                                          */
3915                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3916                                                         error = EINVAL;
3917                                                 }
3918                                         }
3919                                         SCTP_INP_WUNLOCK(inp);
3920                                 } else {
3921                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3922                                         error = EINVAL;
3923                                 }
3924                         }
3925                         break;
3926                 }
3927         case SCTP_CMT_ON_OFF:
3928                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3929                         struct sctp_assoc_value *av;
3930
3931                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3932                         if (av->assoc_value > SCTP_CMT_MAX) {
3933                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3934                                 error = EINVAL;
3935                                 break;
3936                         }
3937                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3938                         if (stcb) {
3939                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3940                                 SCTP_TCB_UNLOCK(stcb);
3941                         } else {
3942                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3943                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3944                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3945                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3946                                         SCTP_INP_WLOCK(inp);
3947                                         inp->sctp_cmt_on_off = av->assoc_value;
3948                                         SCTP_INP_WUNLOCK(inp);
3949                                 }
3950                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3951                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3952                                         SCTP_INP_RLOCK(inp);
3953                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3954                                                 SCTP_TCB_LOCK(stcb);
3955                                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3956                                                 SCTP_TCB_UNLOCK(stcb);
3957                                         }
3958                                         SCTP_INP_RUNLOCK(inp);
3959                                 }
3960                         }
3961                 } else {
3962                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3963                         error = ENOPROTOOPT;
3964                 }
3965                 break;
3966         case SCTP_PLUGGABLE_CC:
3967                 {
3968                         struct sctp_assoc_value *av;
3969                         struct sctp_nets *net;
3970
3971                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3972                         if ((av->assoc_value != SCTP_CC_RFC2581) &&
3973                             (av->assoc_value != SCTP_CC_HSTCP) &&
3974                             (av->assoc_value != SCTP_CC_HTCP) &&
3975                             (av->assoc_value != SCTP_CC_RTCC)) {
3976                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3977                                 error = EINVAL;
3978                                 break;
3979                         }
3980                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3981                         if (stcb) {
3982                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3983                                 stcb->asoc.congestion_control_module = av->assoc_value;
3984                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3985                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3986                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3987                                         }
3988                                 }
3989                                 SCTP_TCB_UNLOCK(stcb);
3990                         } else {
3991                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3992                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3993                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3994                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3995                                         SCTP_INP_WLOCK(inp);
3996                                         inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
3997                                         SCTP_INP_WUNLOCK(inp);
3998                                 }
3999                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4000                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4001                                         SCTP_INP_RLOCK(inp);
4002                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4003                                                 SCTP_TCB_LOCK(stcb);
4004                                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4005                                                 stcb->asoc.congestion_control_module = av->assoc_value;
4006                                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4007                                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4008                                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4009                                                         }
4010                                                 }
4011                                                 SCTP_TCB_UNLOCK(stcb);
4012                                         }
4013                                         SCTP_INP_RUNLOCK(inp);
4014                                 }
4015                         }
4016                         break;
4017                 }
4018         case SCTP_CC_OPTION:
4019                 {
4020                         struct sctp_cc_option *cc_opt;
4021
4022                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
4023                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
4024                         if (stcb == NULL) {
4025                                 if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
4026                                         SCTP_INP_RLOCK(inp);
4027                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4028                                                 SCTP_TCB_LOCK(stcb);
4029                                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
4030                                                         (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
4031                                                 }
4032                                                 SCTP_TCB_UNLOCK(stcb);
4033                                         }
4034                                         SCTP_INP_RUNLOCK(inp);
4035                                 } else {
4036                                         error = EINVAL;
4037                                 }
4038                         } else {
4039                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
4040                                         error = ENOTSUP;
4041                                 } else {
4042                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
4043                                             cc_opt);
4044                                 }
4045                                 SCTP_TCB_UNLOCK(stcb);
4046                         }
4047                         break;
4048                 }
4049         case SCTP_PLUGGABLE_SS:
4050                 {
4051                         struct sctp_assoc_value *av;
4052
4053                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4054                         if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4055                             (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4056                             (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4057                             (av->assoc_value != SCTP_SS_PRIORITY) &&
4058                             (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4059                             (av->assoc_value != SCTP_SS_FIRST_COME)) {
4060                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4061                                 error = EINVAL;
4062                                 break;
4063                         }
4064                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4065                         if (stcb) {
4066                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4067                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4068                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4069                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
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                                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4086                                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4087                                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4088                                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4089                                                 SCTP_TCB_UNLOCK(stcb);
4090                                         }
4091                                         SCTP_INP_RUNLOCK(inp);
4092                                 }
4093                         }
4094                         break;
4095                 }
4096         case SCTP_SS_VALUE:
4097                 {
4098                         struct sctp_stream_value *av;
4099
4100                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4101                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4102                         if (stcb) {
4103                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
4104                                     (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4105                                     av->stream_value) < 0)) {
4106                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4107                                         error = EINVAL;
4108                                 }
4109                                 SCTP_TCB_UNLOCK(stcb);
4110                         } else {
4111                                 if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4112                                         SCTP_INP_RLOCK(inp);
4113                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4114                                                 SCTP_TCB_LOCK(stcb);
4115                                                 if (av->stream_id < stcb->asoc.streamoutcnt) {
4116                                                         stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4117                                                             &stcb->asoc,
4118                                                             &stcb->asoc.strmout[av->stream_id],
4119                                                             av->stream_value);
4120                                                 }
4121                                                 SCTP_TCB_UNLOCK(stcb);
4122                                         }
4123                                         SCTP_INP_RUNLOCK(inp);
4124                                 } else {
4125                                         /*
4126                                          * Can't set stream value without
4127                                          * association
4128                                          */
4129                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4130                                         error = EINVAL;
4131                                 }
4132                         }
4133                         break;
4134                 }
4135         case SCTP_CLR_STAT_LOG:
4136                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4137                 error = EOPNOTSUPP;
4138                 break;
4139         case SCTP_CONTEXT:
4140                 {
4141                         struct sctp_assoc_value *av;
4142
4143                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4144                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4145
4146                         if (stcb) {
4147                                 stcb->asoc.context = av->assoc_value;
4148                                 SCTP_TCB_UNLOCK(stcb);
4149                         } else {
4150                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4151                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4152                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4153                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4154                                         SCTP_INP_WLOCK(inp);
4155                                         inp->sctp_context = av->assoc_value;
4156                                         SCTP_INP_WUNLOCK(inp);
4157                                 }
4158                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4159                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4160                                         SCTP_INP_RLOCK(inp);
4161                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4162                                                 SCTP_TCB_LOCK(stcb);
4163                                                 stcb->asoc.context = av->assoc_value;
4164                                                 SCTP_TCB_UNLOCK(stcb);
4165                                         }
4166                                         SCTP_INP_RUNLOCK(inp);
4167                                 }
4168                         }
4169                         break;
4170                 }
4171         case SCTP_VRF_ID:
4172                 {
4173                         uint32_t *default_vrfid;
4174
4175                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4176                         if (*default_vrfid > SCTP_MAX_VRF_ID) {
4177                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4178                                 error = EINVAL;
4179                                 break;
4180                         }
4181                         inp->def_vrf_id = *default_vrfid;
4182                         break;
4183                 }
4184         case SCTP_DEL_VRF_ID:
4185                 {
4186                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4187                         error = EOPNOTSUPP;
4188                         break;
4189                 }
4190         case SCTP_ADD_VRF_ID:
4191                 {
4192                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4193                         error = EOPNOTSUPP;
4194                         break;
4195                 }
4196         case SCTP_DELAYED_SACK:
4197                 {
4198                         struct sctp_sack_info *sack;
4199
4200                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4201                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4202                         if (sack->sack_delay) {
4203                                 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4204                                         sack->sack_delay = SCTP_MAX_SACK_DELAY;
4205                                 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4206                                         sack->sack_delay = TICKS_TO_MSEC(1);
4207                                 }
4208                         }
4209                         if (stcb) {
4210                                 if (sack->sack_delay) {
4211                                         stcb->asoc.delayed_ack = sack->sack_delay;
4212                                 }
4213                                 if (sack->sack_freq) {
4214                                         stcb->asoc.sack_freq = sack->sack_freq;
4215                                 }
4216                                 SCTP_TCB_UNLOCK(stcb);
4217                         } else {
4218                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4219                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4220                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4221                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4222                                         SCTP_INP_WLOCK(inp);
4223                                         if (sack->sack_delay) {
4224                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4225                                         }
4226                                         if (sack->sack_freq) {
4227                                                 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4228                                         }
4229                                         SCTP_INP_WUNLOCK(inp);
4230                                 }
4231                                 if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4232                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4233                                         SCTP_INP_RLOCK(inp);
4234                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4235                                                 SCTP_TCB_LOCK(stcb);
4236                                                 if (sack->sack_delay) {
4237                                                         stcb->asoc.delayed_ack = sack->sack_delay;
4238                                                 }
4239                                                 if (sack->sack_freq) {
4240                                                         stcb->asoc.sack_freq = sack->sack_freq;
4241                                                 }
4242                                                 SCTP_TCB_UNLOCK(stcb);
4243                                         }
4244                                         SCTP_INP_RUNLOCK(inp);
4245                                 }
4246                         }
4247                         break;
4248                 }
4249         case SCTP_AUTH_CHUNK:
4250                 {
4251                         struct sctp_authchunk *sauth;
4252
4253                         SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4254
4255                         SCTP_INP_WLOCK(inp);
4256                         if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4257                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4258                                 error = EINVAL;
4259                         } else {
4260                                 inp->auth_supported = 1;
4261                         }
4262                         SCTP_INP_WUNLOCK(inp);
4263                         break;
4264                 }
4265         case SCTP_AUTH_KEY:
4266                 {
4267                         struct sctp_authkey *sca;
4268                         struct sctp_keyhead *shared_keys;
4269                         sctp_sharedkey_t *shared_key;
4270                         sctp_key_t *key = NULL;
4271                         size_t size;
4272
4273                         SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4274                         if (sca->sca_keylength == 0) {
4275                                 size = optsize - sizeof(struct sctp_authkey);
4276                         } else {
4277                                 if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4278                                         size = sca->sca_keylength;
4279                                 } else {
4280                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4281                                         error = EINVAL;
4282                                         break;
4283                                 }
4284                         }
4285                         SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4286
4287                         if (stcb) {
4288                                 shared_keys = &stcb->asoc.shared_keys;
4289                                 /* clear the cached keys for this key id */
4290                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4291                                 /*
4292                                  * create the new shared key and
4293                                  * insert/replace it
4294                                  */
4295                                 if (size > 0) {
4296                                         key = sctp_set_key(sca->sca_key, (uint32_t)size);
4297                                         if (key == NULL) {
4298                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4299                                                 error = ENOMEM;
4300                                                 SCTP_TCB_UNLOCK(stcb);
4301                                                 break;
4302                                         }
4303                                 }
4304                                 shared_key = sctp_alloc_sharedkey();
4305                                 if (shared_key == NULL) {
4306                                         sctp_free_key(key);
4307                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4308                                         error = ENOMEM;
4309                                         SCTP_TCB_UNLOCK(stcb);
4310                                         break;
4311                                 }
4312                                 shared_key->key = key;
4313                                 shared_key->keyid = sca->sca_keynumber;
4314                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4315                                 SCTP_TCB_UNLOCK(stcb);
4316                         } else {
4317                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4318                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4319                                     (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4320                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4321                                         SCTP_INP_WLOCK(inp);
4322                                         shared_keys = &inp->sctp_ep.shared_keys;
4323                                         /*
4324                                          * clear the cached keys on all
4325                                          * assocs for this key id
4326                                          */
4327                                         sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4328                                         /*
4329                                          * create the new shared key and
4330                                          * insert/replace it
4331                                          */
4332                                         if (size > 0) {
4333                                                 key = sctp_set_key(sca->sca_key, (uint32_t)size);
4334                                                 if (key == NULL) {
4335                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4336                                                         error = ENOMEM;
4337                                                         SCTP_INP_WUNLOCK(inp);
4338                                                         break;
4339                                                 }
4340                                         }
4341                                         shared_key = sctp_alloc_sharedkey();
4342                                         if (shared_key == NULL) {
4343                                                 sctp_free_key(key);
4344                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4345                                                 error = ENOMEM;
4346                                                 SCTP_INP_WUNLOCK(inp);
4347                                                 break;
4348                                         }
4349                                         shared_key->key = key;
4350                                         shared_key->keyid = sca->sca_keynumber;
4351                                         error = sctp_insert_sharedkey(shared_keys, shared_key);
4352                                         SCTP_INP_WUNLOCK(inp);
4353                                 }
4354                                 if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4355                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4356                                         SCTP_INP_RLOCK(inp);
4357                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4358                                                 SCTP_TCB_LOCK(stcb);
4359                                                 shared_keys = &stcb->asoc.shared_keys;
4360                                                 /*
4361                                                  * clear the cached keys for
4362                                                  * this key id
4363                                                  */
4364                                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4365                                                 /*
4366                                                  * create the new shared key
4367                                                  * and insert/replace it
4368                                                  */
4369                                                 if (size > 0) {
4370                                                         key = sctp_set_key(sca->sca_key, (uint32_t)size);
4371                                                         if (key == NULL) {
4372                                                                 SCTP_TCB_UNLOCK(stcb);
4373                                                                 continue;
4374                                                         }
4375                                                 }
4376                                                 shared_key = sctp_alloc_sharedkey();
4377                                                 if (shared_key == NULL) {
4378                                                         sctp_free_key(key);
4379                                                         SCTP_TCB_UNLOCK(stcb);
4380                                                         continue;
4381                                                 }
4382                                                 shared_key->key = key;
4383                                                 shared_key->keyid = sca->sca_keynumber;
4384                                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4385                                                 SCTP_TCB_UNLOCK(stcb);
4386                                         }
4387                                         SCTP_INP_RUNLOCK(inp);
4388                                 }
4389                         }
4390                         break;
4391                 }
4392         case SCTP_HMAC_IDENT:
4393                 {
4394                         struct sctp_hmacalgo *shmac;
4395                         sctp_hmaclist_t *hmaclist;
4396                         uint16_t hmacid;
4397                         uint32_t i;
4398
4399                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4400                         if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
4401                             (shmac->shmac_number_of_idents > 0xffff)) {
4402                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4403                                 error = EINVAL;
4404                                 break;
4405                         }
4406
4407                         hmaclist = sctp_alloc_hmaclist((uint16_t)shmac->shmac_number_of_idents);
4408                         if (hmaclist == NULL) {
4409                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4410                                 error = ENOMEM;
4411                                 break;
4412                         }
4413                         for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4414                                 hmacid = shmac->shmac_idents[i];
4415                                 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4416                                          /* invalid HMACs were found */ ;
4417                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4418                                         error = EINVAL;
4419                                         sctp_free_hmaclist(hmaclist);
4420                                         goto sctp_set_hmac_done;
4421                                 }
4422                         }
4423                         for (i = 0; i < hmaclist->num_algo; i++) {
4424                                 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4425                                         /* already in list */
4426                                         break;
4427                                 }
4428                         }
4429                         if (i == hmaclist->num_algo) {
4430                                 /* not found in list */
4431                                 sctp_free_hmaclist(hmaclist);
4432                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4433                                 error = EINVAL;
4434                                 break;
4435                         }
4436                         /* set it on the endpoint */
4437                         SCTP_INP_WLOCK(inp);
4438                         if (inp->sctp_ep.local_hmacs)
4439                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4440                         inp->sctp_ep.local_hmacs = hmaclist;
4441                         SCTP_INP_WUNLOCK(inp);
4442         sctp_set_hmac_done:
4443                         break;
4444                 }
4445         case SCTP_AUTH_ACTIVE_KEY:
4446                 {
4447                         struct sctp_authkeyid *scact;
4448
4449                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4450                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4451
4452                         /* set the active key on the right place */
4453                         if (stcb) {
4454                                 /* set the active key on the assoc */
4455                                 if (sctp_auth_setactivekey(stcb,
4456                                     scact->scact_keynumber)) {
4457                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4458                                             SCTP_FROM_SCTP_USRREQ,
4459                                             EINVAL);
4460                                         error = EINVAL;
4461                                 }
4462                                 SCTP_TCB_UNLOCK(stcb);
4463                         } else {
4464                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4465                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4466                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4467                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4468                                         SCTP_INP_WLOCK(inp);
4469                                         if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4470                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4471                                                 error = EINVAL;
4472                                         }
4473                                         SCTP_INP_WUNLOCK(inp);
4474                                 }
4475                                 if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4476                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4477                                         SCTP_INP_RLOCK(inp);
4478                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4479                                                 SCTP_TCB_LOCK(stcb);
4480                                                 sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4481                                                 SCTP_TCB_UNLOCK(stcb);
4482                                         }
4483                                         SCTP_INP_RUNLOCK(inp);
4484                                 }
4485                         }
4486                         break;
4487                 }
4488         case SCTP_AUTH_DELETE_KEY:
4489                 {
4490                         struct sctp_authkeyid *scdel;
4491
4492                         SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4493                         SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4494
4495                         /* delete the key from the right place */
4496                         if (stcb) {
4497                                 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4498                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4499                                         error = EINVAL;
4500                                 }
4501                                 SCTP_TCB_UNLOCK(stcb);
4502                         } else {
4503                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4504                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4505                                     (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4506                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4507                                         SCTP_INP_WLOCK(inp);
4508                                         if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4509                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4510                                                 error = EINVAL;
4511                                         }
4512                                         SCTP_INP_WUNLOCK(inp);
4513                                 }
4514                                 if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4515                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4516                                         SCTP_INP_RLOCK(inp);
4517                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4518                                                 SCTP_TCB_LOCK(stcb);
4519                                                 sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4520                                                 SCTP_TCB_UNLOCK(stcb);
4521                                         }
4522                                         SCTP_INP_RUNLOCK(inp);
4523                                 }
4524                         }
4525                         break;
4526                 }
4527         case SCTP_AUTH_DEACTIVATE_KEY:
4528                 {
4529                         struct sctp_authkeyid *keyid;
4530
4531                         SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4532                         SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4533
4534                         /* deactivate the key from the right place */
4535                         if (stcb) {
4536                                 if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4537                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4538                                         error = EINVAL;
4539                                 }
4540                                 SCTP_TCB_UNLOCK(stcb);
4541                         } else {
4542                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4543                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4544                                     (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4545                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4546                                         SCTP_INP_WLOCK(inp);
4547                                         if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4548                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4549                                                 error = EINVAL;
4550                                         }
4551                                         SCTP_INP_WUNLOCK(inp);
4552                                 }
4553                                 if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4554                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4555                                         SCTP_INP_RLOCK(inp);
4556                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4557                                                 SCTP_TCB_LOCK(stcb);
4558                                                 sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4559                                                 SCTP_TCB_UNLOCK(stcb);
4560                                         }
4561                                         SCTP_INP_RUNLOCK(inp);
4562                                 }
4563                         }
4564                         break;
4565                 }
4566         case SCTP_ENABLE_STREAM_RESET:
4567                 {
4568                         struct sctp_assoc_value *av;
4569
4570                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4571                         if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4572                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4573                                 error = EINVAL;
4574                                 break;
4575                         }
4576                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4577                         if (stcb) {
4578                                 stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
4579                                 SCTP_TCB_UNLOCK(stcb);
4580                         } else {
4581                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4582                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4583                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4584                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4585                                         SCTP_INP_WLOCK(inp);
4586                                         inp->local_strreset_support = (uint8_t)av->assoc_value;
4587                                         SCTP_INP_WUNLOCK(inp);
4588                                 }
4589                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4590                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4591                                         SCTP_INP_RLOCK(inp);
4592                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4593                                                 SCTP_TCB_LOCK(stcb);
4594                                                 stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
4595                                                 SCTP_TCB_UNLOCK(stcb);
4596                                         }
4597                                         SCTP_INP_RUNLOCK(inp);
4598                                 }
4599
4600                         }
4601                         break;
4602                 }
4603         case SCTP_RESET_STREAMS:
4604                 {
4605                         struct sctp_reset_streams *strrst;
4606                         int i, send_out = 0;
4607                         int send_in = 0;
4608
4609                         SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4610                         SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4611                         if (stcb == NULL) {
4612                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4613                                 error = ENOENT;
4614                                 break;
4615                         }
4616                         if (stcb->asoc.reconfig_supported == 0) {
4617                                 /*
4618                                  * Peer does not support the chunk type.
4619                                  */
4620                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4621                                 error = EOPNOTSUPP;
4622                                 SCTP_TCB_UNLOCK(stcb);
4623                                 break;
4624                         }
4625                         if (sizeof(struct sctp_reset_streams) +
4626                             strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
4627                                 error = EINVAL;
4628                                 SCTP_TCB_UNLOCK(stcb);
4629                                 break;
4630                         }
4631                         if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4632                                 send_in = 1;
4633                                 if (stcb->asoc.stream_reset_outstanding) {
4634                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4635                                         error = EALREADY;
4636                                         SCTP_TCB_UNLOCK(stcb);
4637                                         break;
4638                                 }
4639                         }
4640                         if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4641                                 send_out = 1;
4642                         }
4643                         if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
4644                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4645                                 error = ENOMEM;
4646                                 SCTP_TCB_UNLOCK(stcb);
4647                                 break;
4648                         }
4649                         if ((send_in == 0) && (send_out == 0)) {
4650                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4651                                 error = EINVAL;
4652                                 SCTP_TCB_UNLOCK(stcb);
4653                                 break;
4654                         }
4655                         for (i = 0; i < strrst->srs_number_streams; i++) {
4656                                 if ((send_in) &&
4657                                     (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
4658                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4659                                         error = EINVAL;
4660                                         break;
4661                                 }
4662                                 if ((send_out) &&
4663                                     (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
4664                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4665                                         error = EINVAL;
4666                                         break;
4667                                 }
4668                         }
4669                         if (error) {
4670                                 SCTP_TCB_UNLOCK(stcb);
4671                                 break;
4672                         }
4673                         if (send_out) {
4674                                 int cnt;
4675                                 uint16_t strm;
4676
4677                                 if (strrst->srs_number_streams) {
4678                                         for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
4679                                                 strm = strrst->srs_stream_list[i];
4680                                                 if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
4681                                                         stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
4682                                                         cnt++;
4683                                                 }
4684                                         }
4685                                 } else {
4686                                         /* Its all */
4687                                         for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
4688                                                 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
4689                                                         stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
4690                                                         cnt++;
4691                                                 }
4692                                         }
4693                                 }
4694                         }
4695                         if (send_in) {
4696                                 error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4697                                     strrst->srs_stream_list,
4698                                     send_in, 0, 0, 0, 0, 0);
4699                         } else {
4700                                 error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
4701                         }
4702                         if (error == 0) {
4703                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4704                         } else {
4705                                 /*
4706                                  * For outgoing streams don't report any
4707                                  * problems in sending the request to the
4708                                  * application. XXX: Double check resetting
4709                                  * incoming streams.
4710                                  */
4711                                 error = 0;
4712                         }
4713                         SCTP_TCB_UNLOCK(stcb);
4714                         break;
4715                 }
4716         case SCTP_ADD_STREAMS:
4717                 {
4718                         struct sctp_add_streams *stradd;
4719                         uint8_t addstream = 0;
4720                         uint16_t add_o_strmcnt = 0;
4721                         uint16_t add_i_strmcnt = 0;
4722
4723                         SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4724                         SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4725                         if (stcb == NULL) {
4726                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4727                                 error = ENOENT;
4728                                 break;
4729                         }
4730                         if (stcb->asoc.reconfig_supported == 0) {
4731                                 /*
4732                                  * Peer does not support the chunk type.
4733                                  */
4734                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4735                                 error = EOPNOTSUPP;
4736                                 SCTP_TCB_UNLOCK(stcb);
4737                                 break;
4738                         }
4739                         if (stcb->asoc.stream_reset_outstanding) {
4740                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4741                                 error = EALREADY;
4742                                 SCTP_TCB_UNLOCK(stcb);
4743                                 break;
4744                         }
4745                         if ((stradd->sas_outstrms == 0) &&
4746                             (stradd->sas_instrms == 0)) {
4747                                 error = EINVAL;
4748                                 goto skip_stuff;
4749                         }
4750                         if (stradd->sas_outstrms) {
4751                                 addstream = 1;
4752                                 /* We allocate here */
4753                                 add_o_strmcnt = stradd->sas_outstrms;
4754                                 if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4755                                         /* You can't have more than 64k */
4756                                         error = EINVAL;
4757                                         goto skip_stuff;
4758                                 }
4759                         }
4760                         if (stradd->sas_instrms) {
4761                                 int cnt;
4762
4763                                 addstream |= 2;
4764                                 /*
4765                                  * We allocate inside
4766                                  * sctp_send_str_reset_req()
4767                                  */
4768                                 add_i_strmcnt = stradd->sas_instrms;
4769                                 cnt = add_i_strmcnt;
4770                                 cnt += stcb->asoc.streamincnt;
4771                                 if (cnt > 0x0000ffff) {
4772                                         /* You can't have more than 64k */
4773                                         error = EINVAL;
4774                                         goto skip_stuff;
4775                                 }
4776                                 if (cnt > (int)stcb->asoc.max_inbound_streams) {
4777                                         /* More than you are allowed */
4778                                         error = EINVAL;
4779                                         goto skip_stuff;
4780                                 }
4781                         }
4782                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4783                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4784         skip_stuff:
4785                         SCTP_TCB_UNLOCK(stcb);
4786                         break;
4787                 }
4788         case SCTP_RESET_ASSOC:
4789                 {
4790                         int i;
4791                         uint32_t *value;
4792
4793                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4794                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t)*value);
4795                         if (stcb == NULL) {
4796                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4797                                 error = ENOENT;
4798                                 break;
4799                         }
4800                         if (stcb->asoc.reconfig_supported == 0) {
4801                                 /*
4802                                  * Peer does not support the chunk type.
4803                                  */
4804                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4805                                 error = EOPNOTSUPP;
4806                                 SCTP_TCB_UNLOCK(stcb);
4807                                 break;
4808                         }
4809                         if (stcb->asoc.stream_reset_outstanding) {
4810                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4811                                 error = EALREADY;
4812                                 SCTP_TCB_UNLOCK(stcb);
4813                                 break;
4814                         }
4815                         /*
4816                          * Is there any data pending in the send or sent
4817                          * queues?
4818                          */
4819                         if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
4820                             !TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4821                 busy_out:
4822                                 error = EBUSY;
4823                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4824                                 SCTP_TCB_UNLOCK(stcb);
4825                                 break;
4826                         }
4827                         /* Do any streams have data queued? */
4828                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4829                                 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
4830                                         goto busy_out;
4831                                 }
4832                         }
4833                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
4834                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4835                         SCTP_TCB_UNLOCK(stcb);
4836                         break;
4837                 }
4838         case SCTP_CONNECT_X:
4839                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4840                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4841                         error = EINVAL;
4842                         break;
4843                 }
4844                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4845                 break;
4846         case SCTP_CONNECT_X_DELAYED:
4847                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4848                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4849                         error = EINVAL;
4850                         break;
4851                 }
4852                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4853                 break;
4854         case SCTP_CONNECT_X_COMPLETE:
4855                 {
4856                         struct sockaddr *sa;
4857
4858                         /* FIXME MT: check correct? */
4859                         SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4860
4861                         /* find tcb */
4862                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4863                                 SCTP_INP_RLOCK(inp);
4864                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4865                                 if (stcb) {
4866                                         SCTP_TCB_LOCK(stcb);
4867                                 }
4868                                 SCTP_INP_RUNLOCK(inp);
4869                         } else {
4870                                 /*
4871                                  * We increment here since
4872                                  * sctp_findassociation_ep_addr() wil do a
4873                                  * decrement if it finds the stcb as long as
4874                                  * the locked tcb (last argument) is NOT a
4875                                  * TCB.. aka NULL.
4876                                  */
4877                                 SCTP_INP_INCR_REF(inp);
4878                                 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
4879                                 if (stcb == NULL) {
4880                                         SCTP_INP_DECR_REF(inp);
4881                                 }
4882                         }
4883
4884                         if (stcb == NULL) {
4885                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4886                                 error = ENOENT;
4887                                 break;
4888                         }
4889                         if (stcb->asoc.delayed_connection == 1) {
4890                                 stcb->asoc.delayed_connection = 0;
4891                                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4892                                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4893                                     stcb->asoc.primary_destination,
4894                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
4895                                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4896                         } else {
4897                                 /*
4898                                  * already expired or did not use delayed
4899                                  * connectx
4900                                  */
4901                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4902                                 error = EALREADY;
4903                         }
4904                         SCTP_TCB_UNLOCK(stcb);
4905                         break;
4906                 }
4907         case SCTP_MAX_BURST:
4908                 {
4909                         struct sctp_assoc_value *av;
4910
4911                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4912                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4913
4914                         if (stcb) {
4915                                 stcb->asoc.max_burst = av->assoc_value;
4916                                 SCTP_TCB_UNLOCK(stcb);
4917                         } else {
4918                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4919                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4920                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4921                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4922                                         SCTP_INP_WLOCK(inp);
4923                                         inp->sctp_ep.max_burst = av->assoc_value;
4924                                         SCTP_INP_WUNLOCK(inp);
4925                                 }
4926                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4927                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4928                                         SCTP_INP_RLOCK(inp);
4929                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4930                                                 SCTP_TCB_LOCK(stcb);
4931                                                 stcb->asoc.max_burst = av->assoc_value;
4932                                                 SCTP_TCB_UNLOCK(stcb);
4933                                         }
4934                                         SCTP_INP_RUNLOCK(inp);
4935                                 }
4936                         }
4937                         break;
4938                 }
4939         case SCTP_MAXSEG:
4940                 {
4941                         struct sctp_assoc_value *av;
4942                         int ovh;
4943
4944                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4945                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4946
4947                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4948                                 ovh = SCTP_MED_OVERHEAD;
4949                         } else {
4950                                 ovh = SCTP_MED_V4_OVERHEAD;
4951                         }
4952                         if (stcb) {
4953                                 if (av->assoc_value) {
4954                                         stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4955                                 } else {
4956                                         stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4957                                 }
4958                                 SCTP_TCB_UNLOCK(stcb);
4959                         } else {
4960                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4961                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4962                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4963                                         SCTP_INP_WLOCK(inp);
4964                                         /*
4965                                          * FIXME MT: I think this is not in
4966                                          * tune with the API ID
4967                                          */
4968                                         if (av->assoc_value) {
4969                                                 inp->sctp_frag_point = (av->assoc_value + ovh);
4970                                         } else {
4971                                                 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4972                                         }
4973                                         SCTP_INP_WUNLOCK(inp);
4974                                 } else {
4975                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4976                                         error = EINVAL;
4977                                 }
4978                         }
4979                         break;
4980                 }
4981         case SCTP_EVENTS:
4982                 {
4983                         struct sctp_event_subscribe *events;
4984
4985                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
4986
4987                         SCTP_INP_WLOCK(inp);
4988                         if (events->sctp_data_io_event) {
4989                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4990                         } else {
4991                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4992                         }
4993
4994                         if (events->sctp_association_event) {
4995                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4996                         } else {
4997                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4998                         }
4999
5000                         if (events->sctp_address_event) {
5001                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5002                         } else {
5003                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5004                         }
5005
5006                         if (events->sctp_send_failure_event) {
5007                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5008                         } else {
5009                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5010                         }
5011
5012                         if (events->sctp_peer_error_event) {
5013                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5014                         } else {
5015                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5016                         }
5017
5018                         if (events->sctp_shutdown_event) {
5019                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5020                         } else {
5021                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5022                         }
5023
5024                         if (events->sctp_partial_delivery_event) {
5025                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5026                         } else {
5027                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5028                         }
5029
5030                         if (events->sctp_adaptation_layer_event) {
5031                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5032                         } else {
5033                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5034                         }
5035
5036                         if (events->sctp_authentication_event) {
5037                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5038                         } else {
5039                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5040                         }
5041
5042                         if (events->sctp_sender_dry_event) {
5043                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
5044                         } else {
5045                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5046                         }
5047
5048                         if (events->sctp_stream_reset_event) {
5049                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5050                         } else {
5051                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5052                         }
5053                         SCTP_INP_WUNLOCK(inp);
5054
5055                         SCTP_INP_RLOCK(inp);
5056                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5057                                 SCTP_TCB_LOCK(stcb);
5058                                 if (events->sctp_association_event) {
5059                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5060                                 } else {
5061                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5062                                 }
5063                                 if (events->sctp_address_event) {
5064                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5065                                 } else {
5066                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5067                                 }
5068                                 if (events->sctp_send_failure_event) {
5069                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5070                                 } else {
5071                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5072                                 }
5073                                 if (events->sctp_peer_error_event) {
5074                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5075                                 } else {
5076                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5077                                 }
5078                                 if (events->sctp_shutdown_event) {
5079                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5080                                 } else {
5081                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5082                                 }
5083                                 if (events->sctp_partial_delivery_event) {
5084                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5085                                 } else {
5086                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5087                                 }
5088                                 if (events->sctp_adaptation_layer_event) {
5089                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5090                                 } else {
5091                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5092                                 }
5093                                 if (events->sctp_authentication_event) {
5094                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5095                                 } else {
5096                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5097                                 }
5098                                 if (events->sctp_sender_dry_event) {
5099                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5100                                 } else {
5101                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5102                                 }
5103                                 if (events->sctp_stream_reset_event) {
5104                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5105                                 } else {
5106                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5107                                 }
5108                                 SCTP_TCB_UNLOCK(stcb);
5109                         }
5110                         /*
5111                          * Send up the sender dry event only for 1-to-1
5112                          * style sockets.
5113                          */
5114                         if (events->sctp_sender_dry_event) {
5115                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5116                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5117                                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
5118                                         if (stcb) {
5119                                                 SCTP_TCB_LOCK(stcb);
5120                                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5121                                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5122                                                     (stcb->asoc.stream_queue_cnt == 0)) {
5123                                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5124                                                 }
5125                                                 SCTP_TCB_UNLOCK(stcb);
5126                                         }
5127                                 }
5128                         }
5129                         SCTP_INP_RUNLOCK(inp);
5130                         break;
5131                 }
5132         case SCTP_ADAPTATION_LAYER:
5133                 {
5134                         struct sctp_setadaptation *adap_bits;
5135
5136                         SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5137                         SCTP_INP_WLOCK(inp);
5138                         inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5139                         inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5140                         SCTP_INP_WUNLOCK(inp);
5141                         break;
5142                 }
5143 #ifdef SCTP_DEBUG
5144         case SCTP_SET_INITIAL_DBG_SEQ:
5145                 {
5146                         uint32_t *vvv;
5147
5148                         SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5149                         SCTP_INP_WLOCK(inp);
5150                         inp->sctp_ep.initial_sequence_debug = *vvv;
5151                         SCTP_INP_WUNLOCK(inp);
5152                         break;
5153                 }
5154 #endif
5155         case SCTP_DEFAULT_SEND_PARAM:
5156                 {
5157                         struct sctp_sndrcvinfo *s_info;
5158
5159                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5160                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5161
5162                         if (stcb) {
5163                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5164                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5165                                 } else {
5166                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5167                                         error = EINVAL;
5168                                 }
5169                                 SCTP_TCB_UNLOCK(stcb);
5170                         } else {
5171                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5172                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5173                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5174                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5175                                         SCTP_INP_WLOCK(inp);
5176                                         memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5177                                         SCTP_INP_WUNLOCK(inp);
5178                                 }
5179                                 if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5180                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5181                                         SCTP_INP_RLOCK(inp);
5182                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5183                                                 SCTP_TCB_LOCK(stcb);
5184                                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5185                                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5186                                                 }
5187                                                 SCTP_TCB_UNLOCK(stcb);
5188                                         }
5189                                         SCTP_INP_RUNLOCK(inp);
5190                                 }
5191                         }
5192                         break;
5193                 }
5194         case SCTP_PEER_ADDR_PARAMS:
5195                 {
5196                         struct sctp_paddrparams *paddrp;
5197                         struct sctp_nets *net;
5198                         struct sockaddr *addr;
5199 #if defined(INET) && defined(INET6)
5200                         struct sockaddr_in sin_store;
5201 #endif
5202
5203                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5204                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5205
5206 #if defined(INET) && defined(INET6)
5207                         if (paddrp->spp_address.ss_family == AF_INET6) {
5208                                 struct sockaddr_in6 *sin6;
5209
5210                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5211                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5212                                         in6_sin6_2_sin(&sin_store, sin6);
5213                                         addr = (struct sockaddr *)&sin_store;
5214                                 } else {
5215                                         addr = (struct sockaddr *)&paddrp->spp_address;
5216                                 }
5217                         } else {
5218                                 addr = (struct sockaddr *)&paddrp->spp_address;
5219                         }
5220 #else
5221                         addr = (struct sockaddr *)&paddrp->spp_address;
5222 #endif
5223                         if (stcb != NULL) {
5224                                 net = sctp_findnet(stcb, addr);
5225                         } else {
5226                                 /*
5227                                  * We increment here since
5228                                  * sctp_findassociation_ep_addr() wil do a
5229                                  * decrement if it finds the stcb as long as
5230                                  * the locked tcb (last argument) is NOT a
5231                                  * TCB.. aka NULL.
5232                                  */
5233                                 net = NULL;
5234                                 SCTP_INP_INCR_REF(inp);
5235                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5236                                     &net, NULL, NULL);
5237                                 if (stcb == NULL) {
5238                                         SCTP_INP_DECR_REF(inp);
5239                                 }
5240                         }
5241                         if ((stcb != NULL) && (net == NULL)) {
5242 #ifdef INET
5243                                 if (addr->sa_family == AF_INET) {
5244
5245                                         struct sockaddr_in *sin;
5246
5247                                         sin = (struct sockaddr_in *)addr;
5248                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
5249                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5250                                                 SCTP_TCB_UNLOCK(stcb);
5251                                                 error = EINVAL;
5252                                                 break;
5253                                         }
5254                                 } else
5255 #endif
5256 #ifdef INET6
5257                                 if (addr->sa_family == AF_INET6) {
5258                                         struct sockaddr_in6 *sin6;
5259
5260                                         sin6 = (struct sockaddr_in6 *)addr;
5261                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5262                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5263                                                 SCTP_TCB_UNLOCK(stcb);
5264                                                 error = EINVAL;
5265                                                 break;
5266                                         }
5267                                 } else
5268 #endif
5269                                 {
5270                                         error = EAFNOSUPPORT;
5271                                         SCTP_TCB_UNLOCK(stcb);
5272                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5273                                         break;
5274                                 }
5275                         }
5276                         /* sanity checks */
5277                         if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5278                                 if (stcb)
5279                                         SCTP_TCB_UNLOCK(stcb);
5280                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5281                                 return (EINVAL);
5282                         }
5283
5284                         if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5285                                 if (stcb)
5286                                         SCTP_TCB_UNLOCK(stcb);
5287                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5288                                 return (EINVAL);
5289                         }
5290
5291                         if (stcb != NULL) {
5292                                 /************************TCB SPECIFIC SET ******************/
5293                                 if (net != NULL) {
5294                                         /************************NET SPECIFIC SET ******************/
5295                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5296                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5297                                                     !(net->dest_state & SCTP_ADDR_NOHB)) {
5298                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5299                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
5300                                                 }
5301                                                 net->dest_state |= SCTP_ADDR_NOHB;
5302                                         }
5303                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5304                                                 if (paddrp->spp_hbinterval) {
5305                                                         net->heart_beat_delay = paddrp->spp_hbinterval;
5306                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5307                                                         net->heart_beat_delay = 0;
5308                                                 }
5309                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5310                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5311                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5312                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5313                                         }
5314                                         if (paddrp->spp_flags & SPP_HB_DEMAND) {
5315                                                 /* on demand HB */
5316                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5317                                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5318                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5319                                         }
5320                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5321                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5322                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5323                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
5324                                                 }
5325                                                 net->dest_state |= SCTP_ADDR_NO_PMTUD;
5326                                                 net->mtu = paddrp->spp_pathmtu;
5327                                                 switch (net->ro._l_addr.sa.sa_family) {
5328 #ifdef INET
5329                                                 case AF_INET:
5330                                                         net->mtu += SCTP_MIN_V4_OVERHEAD;
5331                                                         break;
5332 #endif
5333 #ifdef INET6
5334                                                 case AF_INET6:
5335                                                         net->mtu += SCTP_MIN_OVERHEAD;
5336                                                         break;
5337 #endif
5338                                                 default:
5339                                                         break;
5340                                                 }
5341                                                 if (net->mtu < stcb->asoc.smallest_mtu) {
5342                                                         sctp_pathmtu_adjustment(stcb, net->mtu);
5343                                                 }
5344                                         }
5345                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5346                                                 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5347                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5348                                                 }
5349                                                 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5350                                         }
5351                                         if (paddrp->spp_pathmaxrxt) {
5352                                                 if (net->dest_state & SCTP_ADDR_PF) {
5353                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5354                                                                 net->dest_state &= ~SCTP_ADDR_PF;
5355                                                         }
5356                                                 } else {
5357                                                         if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5358                                                             (net->error_count > net->pf_threshold)) {
5359                                                                 net->dest_state |= SCTP_ADDR_PF;
5360                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5361                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5362                                                                     stcb->sctp_ep, stcb, net,
5363                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
5364                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5365                                                         }
5366                                                 }
5367                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
5368                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5369                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5370                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5371                                                         }
5372                                                 } else {
5373                                                         if (net->error_count <= paddrp->spp_pathmaxrxt) {
5374                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
5375                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5376                                                         }
5377                                                 }
5378                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
5379                                         }
5380                                         if (paddrp->spp_flags & SPP_DSCP) {
5381                                                 net->dscp = paddrp->spp_dscp & 0xfc;
5382                                                 net->dscp |= 0x01;
5383                                         }
5384 #ifdef INET6
5385                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5386                                                 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5387                                                         net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5388                                                         net->flowlabel |= 0x80000000;
5389                                                 }
5390                                         }
5391 #endif
5392                                 } else {
5393                                         /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5394                                         if (paddrp->spp_pathmaxrxt != 0) {
5395                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5396                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5397                                                         if (net->dest_state & SCTP_ADDR_PF) {
5398                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5399                                                                         net->dest_state &= ~SCTP_ADDR_PF;
5400                                                                 }
5401                                                         } else {
5402                                                                 if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5403                                                                     (net->error_count > net->pf_threshold)) {
5404                                                                         net->dest_state |= SCTP_ADDR_PF;
5405                                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5406                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5407                                                                             stcb->sctp_ep, stcb, net,
5408                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
5409                                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5410                                                                 }
5411                                                         }
5412                                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
5413                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5414                                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
5415                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5416                                                                 }
5417                                                         } else {
5418                                                                 if (net->error_count <= paddrp->spp_pathmaxrxt) {
5419                                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
5420                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5421                                                                 }
5422                                                         }
5423                                                         net->failure_threshold = paddrp->spp_pathmaxrxt;
5424                                                 }
5425                                         }
5426
5427                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5428                                                 if (paddrp->spp_hbinterval != 0) {
5429                                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5430                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5431                                                         stcb->asoc.heart_beat_delay = 0;
5432                                                 }
5433                                                 /* Turn back on the timer */
5434                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5435                                                         if (paddrp->spp_hbinterval != 0) {
5436                                                                 net->heart_beat_delay = paddrp->spp_hbinterval;
5437                                                         } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5438                                                                 net->heart_beat_delay = 0;
5439                                                         }
5440                                                         if (net->dest_state & SCTP_ADDR_NOHB) {
5441                                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5442                                                         }
5443                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5444                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
5445                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5446                                                 }
5447                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5448                                         }
5449                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5450                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5451                                                         if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5452                                                                 net->dest_state |= SCTP_ADDR_NOHB;
5453                                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5454                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5455                                                                             inp, stcb, net,
5456                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
5457                                                                 }
5458                                                         }
5459                                                 }
5460                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5461                                         }
5462                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5463                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5464                                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5465                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5466                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
5467                                                         }
5468                                                         net->dest_state |= SCTP_ADDR_NO_PMTUD;
5469                                                         net->mtu = paddrp->spp_pathmtu;
5470                                                         switch (net->ro._l_addr.sa.sa_family) {
5471 #ifdef INET
5472                                                         case AF_INET:
5473                                                                 net->mtu += SCTP_MIN_V4_OVERHEAD;
5474                                                                 break;
5475 #endif
5476 #ifdef INET6
5477                                                         case AF_INET6:
5478                                                                 net->mtu += SCTP_MIN_OVERHEAD;
5479                                                                 break;
5480 #endif
5481                                                         default:
5482                                                                 break;
5483                                                         }
5484                                                         if (net->mtu < stcb->asoc.smallest_mtu) {
5485                                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5486                                                         }
5487                                                 }
5488                                                 stcb->asoc.default_mtu = paddrp->spp_pathmtu;
5489                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5490                                         }
5491                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5492                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5493                                                         if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5494                                                                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5495                                                         }
5496                                                         net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5497                                                 }
5498                                                 stcb->asoc.default_mtu = 0;
5499                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5500                                         }
5501                                         if (paddrp->spp_flags & SPP_DSCP) {
5502                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5503                                                         net->dscp = paddrp->spp_dscp & 0xfc;
5504                                                         net->dscp |= 0x01;
5505                                                 }
5506                                                 stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5507                                                 stcb->asoc.default_dscp |= 0x01;
5508                                         }
5509 #ifdef INET6
5510                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5511                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5512                                                         if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5513                                                                 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5514                                                                 net->flowlabel |= 0x80000000;
5515                                                         }
5516                                                 }
5517                                                 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5518                                                 stcb->asoc.default_flowlabel |= 0x80000000;
5519                                         }
5520 #endif
5521                                 }
5522                                 SCTP_TCB_UNLOCK(stcb);
5523                         } else {
5524                                 /************************NO TCB, SET TO default stuff ******************/
5525                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5526                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5527                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5528                                         SCTP_INP_WLOCK(inp);
5529                                         /*
5530                                          * For the TOS/FLOWLABEL stuff you
5531                                          * set it with the options on the
5532                                          * socket
5533                                          */
5534                                         if (paddrp->spp_pathmaxrxt != 0) {
5535                                                 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5536                                         }
5537
5538                                         if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5539                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5540                                         else if (paddrp->spp_hbinterval != 0) {
5541                                                 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5542                                                         paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5543                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5544                                         }
5545
5546                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5547                                                 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5548                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5549                                                 } else if (paddrp->spp_hbinterval) {
5550                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5551                                                 }
5552                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5553                                         } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5554                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5555                                         }
5556                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5557                                                 inp->sctp_ep.default_mtu = 0;
5558                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5559                                         } else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5560                                                 if (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU) {
5561                                                         inp->sctp_ep.default_mtu = paddrp->spp_pathmtu;
5562                                                 }
5563                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5564                                         }
5565                                         if (paddrp->spp_flags & SPP_DSCP) {
5566                                                 inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5567                                                 inp->sctp_ep.default_dscp |= 0x01;
5568                                         }
5569 #ifdef INET6
5570                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5571                                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5572                                                         inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5573                                                         inp->sctp_ep.default_flowlabel |= 0x80000000;
5574                                                 }
5575                                         }
5576 #endif
5577                                         SCTP_INP_WUNLOCK(inp);
5578                                 } else {
5579                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5580                                         error = EINVAL;
5581                                 }
5582                         }
5583                         break;
5584                 }
5585         case SCTP_RTOINFO:
5586                 {
5587                         struct sctp_rtoinfo *srto;
5588                         uint32_t new_init, new_min, new_max;
5589
5590                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5591                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5592
5593                         if (stcb) {
5594                                 if (srto->srto_initial)
5595                                         new_init = srto->srto_initial;
5596                                 else
5597                                         new_init = stcb->asoc.initial_rto;
5598                                 if (srto->srto_max)
5599                                         new_max = srto->srto_max;
5600                                 else
5601                                         new_max = stcb->asoc.maxrto;
5602                                 if (srto->srto_min)
5603                                         new_min = srto->srto_min;
5604                                 else
5605                                         new_min = stcb->asoc.minrto;
5606                                 if ((new_min <= new_init) && (new_init <= new_max)) {
5607                                         stcb->asoc.initial_rto = new_init;
5608                                         stcb->asoc.maxrto = new_max;
5609                                         stcb->asoc.minrto = new_min;
5610                                 } else {
5611                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5612                                         error = EINVAL;
5613                                 }
5614                                 SCTP_TCB_UNLOCK(stcb);
5615                         } else {
5616                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5617                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5618                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5619                                         SCTP_INP_WLOCK(inp);
5620                                         if (srto->srto_initial)
5621                                                 new_init = srto->srto_initial;
5622                                         else
5623                                                 new_init = inp->sctp_ep.initial_rto;
5624                                         if (srto->srto_max)
5625                                                 new_max = srto->srto_max;
5626                                         else
5627                                                 new_max = inp->sctp_ep.sctp_maxrto;
5628                                         if (srto->srto_min)
5629                                                 new_min = srto->srto_min;
5630                                         else
5631                                                 new_min = inp->sctp_ep.sctp_minrto;
5632                                         if ((new_min <= new_init) && (new_init <= new_max)) {
5633                                                 inp->sctp_ep.initial_rto = new_init;
5634                                                 inp->sctp_ep.sctp_maxrto = new_max;
5635                                                 inp->sctp_ep.sctp_minrto = new_min;
5636                                         } else {
5637                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5638                                                 error = EINVAL;
5639                                         }
5640                                         SCTP_INP_WUNLOCK(inp);
5641                                 } else {
5642                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5643                                         error = EINVAL;
5644                                 }
5645                         }
5646                         break;
5647                 }
5648         case SCTP_ASSOCINFO:
5649                 {
5650                         struct sctp_assocparams *sasoc;
5651
5652                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5653                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5654                         if (sasoc->sasoc_cookie_life) {
5655                                 /* boundary check the cookie life */
5656                                 if (sasoc->sasoc_cookie_life < 1000)
5657                                         sasoc->sasoc_cookie_life = 1000;
5658                                 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5659                                         sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5660                                 }
5661                         }
5662                         if (stcb) {
5663                                 if (sasoc->sasoc_asocmaxrxt)
5664                                         stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5665                                 if (sasoc->sasoc_cookie_life) {
5666                                         stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5667                                 }
5668                                 SCTP_TCB_UNLOCK(stcb);
5669                         } else {
5670                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5671                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5672                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5673                                         SCTP_INP_WLOCK(inp);
5674                                         if (sasoc->sasoc_asocmaxrxt)
5675                                                 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5676                                         if (sasoc->sasoc_cookie_life) {
5677                                                 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5678                                         }
5679                                         SCTP_INP_WUNLOCK(inp);
5680                                 } else {
5681                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5682                                         error = EINVAL;
5683                                 }
5684                         }
5685                         break;
5686                 }
5687         case SCTP_INITMSG:
5688                 {
5689                         struct sctp_initmsg *sinit;
5690
5691                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5692                         SCTP_INP_WLOCK(inp);
5693                         if (sinit->sinit_num_ostreams)
5694                                 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5695
5696                         if (sinit->sinit_max_instreams)
5697                                 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5698
5699                         if (sinit->sinit_max_attempts)
5700                                 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5701
5702                         if (sinit->sinit_max_init_timeo)
5703                                 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5704                         SCTP_INP_WUNLOCK(inp);
5705                         break;
5706                 }
5707         case SCTP_PRIMARY_ADDR:
5708                 {
5709                         struct sctp_setprim *spa;
5710                         struct sctp_nets *net;
5711                         struct sockaddr *addr;
5712 #if defined(INET) && defined(INET6)
5713                         struct sockaddr_in sin_store;
5714 #endif
5715
5716                         SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5717                         SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5718
5719 #if defined(INET) && defined(INET6)
5720                         if (spa->ssp_addr.ss_family == AF_INET6) {
5721                                 struct sockaddr_in6 *sin6;
5722
5723                                 sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5724                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5725                                         in6_sin6_2_sin(&sin_store, sin6);
5726                                         addr = (struct sockaddr *)&sin_store;
5727                                 } else {
5728                                         addr = (struct sockaddr *)&spa->ssp_addr;
5729                                 }
5730                         } else {
5731                                 addr = (struct sockaddr *)&spa->ssp_addr;
5732                         }
5733 #else
5734                         addr = (struct sockaddr *)&spa->ssp_addr;
5735 #endif
5736                         if (stcb != NULL) {
5737                                 net = sctp_findnet(stcb, addr);
5738                         } else {
5739                                 /*
5740                                  * We increment here since
5741                                  * sctp_findassociation_ep_addr() wil do a
5742                                  * decrement if it finds the stcb as long as
5743                                  * the locked tcb (last argument) is NOT a
5744                                  * TCB.. aka NULL.
5745                                  */
5746                                 net = NULL;
5747                                 SCTP_INP_INCR_REF(inp);
5748                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5749                                     &net, NULL, NULL);
5750                                 if (stcb == NULL) {
5751                                         SCTP_INP_DECR_REF(inp);
5752                                 }
5753                         }
5754
5755                         if ((stcb != NULL) && (net != NULL)) {
5756                                 if (net != stcb->asoc.primary_destination) {
5757                                         if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5758                                                 /* Ok we need to set it */
5759                                                 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5760                                                         if ((stcb->asoc.alternate) &&
5761                                                             (!(net->dest_state & SCTP_ADDR_PF)) &&
5762                                                             (net->dest_state & SCTP_ADDR_REACHABLE)) {
5763                                                                 sctp_free_remote_addr(stcb->asoc.alternate);
5764                                                                 stcb->asoc.alternate = NULL;
5765                                                         }
5766                                                 } else {
5767                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5768                                                         error = EINVAL;
5769                                                 }
5770                                         } else {
5771                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5772                                                 error = EINVAL;
5773                                         }
5774                                 }
5775                         } else {
5776                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5777                                 error = EINVAL;
5778                         }
5779                         if (stcb != NULL) {
5780                                 SCTP_TCB_UNLOCK(stcb);
5781                         }
5782                         break;
5783                 }
5784         case SCTP_SET_DYNAMIC_PRIMARY:
5785                 {
5786                         union sctp_sockstore *ss;
5787
5788                         error = priv_check(curthread,
5789                             PRIV_NETINET_RESERVEDPORT);
5790                         if (error)
5791                                 break;
5792
5793                         SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5794                         /* SUPER USER CHECK? */
5795                         error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5796                         break;
5797                 }
5798         case SCTP_SET_PEER_PRIMARY_ADDR:
5799                 {
5800                         struct sctp_setpeerprim *sspp;
5801                         struct sockaddr *addr;
5802 #if defined(INET) && defined(INET6)
5803                         struct sockaddr_in sin_store;
5804 #endif
5805
5806                         SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5807                         SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5808                         if (stcb != NULL) {
5809                                 struct sctp_ifa *ifa;
5810
5811 #if defined(INET) && defined(INET6)
5812                                 if (sspp->sspp_addr.ss_family == AF_INET6) {
5813                                         struct sockaddr_in6 *sin6;
5814
5815                                         sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5816                                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5817                                                 in6_sin6_2_sin(&sin_store, sin6);
5818                                                 addr = (struct sockaddr *)&sin_store;
5819                                         } else {
5820                                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5821                                         }
5822                                 } else {
5823                                         addr = (struct sockaddr *)&sspp->sspp_addr;
5824                                 }
5825 #else
5826                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5827 #endif
5828                                 ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5829                                 if (ifa == NULL) {
5830                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5831                                         error = EINVAL;
5832                                         goto out_of_it;
5833                                 }
5834                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5835                                         /*
5836                                          * Must validate the ifa found is in
5837                                          * our ep
5838                                          */
5839                                         struct sctp_laddr *laddr;
5840                                         int found = 0;
5841
5842                                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5843                                                 if (laddr->ifa == NULL) {
5844                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5845                                                             __func__);
5846                                                         continue;
5847                                                 }
5848                                                 if ((sctp_is_addr_restricted(stcb, laddr->ifa)) &&
5849                                                     (!sctp_is_addr_pending(stcb, laddr->ifa))) {
5850                                                         continue;
5851                                                 }
5852                                                 if (laddr->ifa == ifa) {
5853                                                         found = 1;
5854                                                         break;
5855                                                 }
5856                                         }
5857                                         if (!found) {
5858                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5859                                                 error = EINVAL;
5860                                                 goto out_of_it;
5861                                         }
5862                                 } else {
5863                                         switch (addr->sa_family) {
5864 #ifdef INET
5865                                         case AF_INET:
5866                                                 {
5867                                                         struct sockaddr_in *sin;
5868
5869                                                         sin = (struct sockaddr_in *)addr;
5870                                                         if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5871                                                             &sin->sin_addr) != 0) {
5872                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5873                                                                 error = EINVAL;
5874                                                                 goto out_of_it;
5875                                                         }
5876                                                         break;
5877                                                 }
5878 #endif
5879 #ifdef INET6
5880                                         case AF_INET6:
5881                                                 {
5882                                                         struct sockaddr_in6 *sin6;
5883
5884                                                         sin6 = (struct sockaddr_in6 *)addr;
5885                                                         if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5886                                                             &sin6->sin6_addr) != 0) {
5887                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5888                                                                 error = EINVAL;
5889                                                                 goto out_of_it;
5890                                                         }
5891                                                         break;
5892                                                 }
5893 #endif
5894                                         default:
5895                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5896                                                 error = EINVAL;
5897                                                 goto out_of_it;
5898                                         }
5899                                 }
5900                                 if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5901                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5902                                         error = EINVAL;
5903                                 }
5904                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5905                 out_of_it:
5906                                 SCTP_TCB_UNLOCK(stcb);
5907                         } else {
5908                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5909                                 error = EINVAL;
5910                         }
5911                         break;
5912                 }
5913         case SCTP_BINDX_ADD_ADDR:
5914                 {
5915                         struct sctp_getaddresses *addrs;
5916                         struct thread *td;
5917
5918                         td = (struct thread *)p;
5919                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5920                             optsize);
5921 #ifdef INET
5922                         if (addrs->addr->sa_family == AF_INET) {
5923                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5924                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5925                                         error = EINVAL;
5926                                         break;
5927                                 }
5928                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5929                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5930                                         break;
5931                                 }
5932                         } else
5933 #endif
5934 #ifdef INET6
5935                         if (addrs->addr->sa_family == AF_INET6) {
5936                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5937                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5938                                         error = EINVAL;
5939                                         break;
5940                                 }
5941                                 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5942                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5943                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5944                                         break;
5945                                 }
5946                         } else
5947 #endif
5948                         {
5949                                 error = EAFNOSUPPORT;
5950                                 break;
5951                         }
5952                         sctp_bindx_add_address(so, inp, addrs->addr,
5953                             addrs->sget_assoc_id, vrf_id,
5954                             &error, p);
5955                         break;
5956                 }
5957         case SCTP_BINDX_REM_ADDR:
5958                 {
5959                         struct sctp_getaddresses *addrs;
5960                         struct thread *td;
5961
5962                         td = (struct thread *)p;
5963
5964                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5965 #ifdef INET
5966                         if (addrs->addr->sa_family == AF_INET) {
5967                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5968                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5969                                         error = EINVAL;
5970                                         break;
5971                                 }
5972                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5973                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5974                                         break;
5975                                 }
5976                         } else
5977 #endif
5978 #ifdef INET6
5979                         if (addrs->addr->sa_family == AF_INET6) {
5980                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5981                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5982                                         error = EINVAL;
5983                                         break;
5984                                 }
5985                                 if (td != NULL &&
5986                                     (error = prison_local_ip6(td->td_ucred,
5987                                     &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5988                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5989                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5990                                         break;
5991                                 }
5992                         } else
5993 #endif
5994                         {
5995                                 error = EAFNOSUPPORT;
5996                                 break;
5997                         }
5998                         sctp_bindx_delete_address(inp, addrs->addr,
5999                             addrs->sget_assoc_id, vrf_id,
6000                             &error);
6001                         break;
6002                 }
6003         case SCTP_EVENT:
6004                 {
6005                         struct sctp_event *event;
6006                         uint32_t event_type;
6007
6008                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
6009                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
6010                         switch (event->se_type) {
6011                         case SCTP_ASSOC_CHANGE:
6012                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
6013                                 break;
6014                         case SCTP_PEER_ADDR_CHANGE:
6015                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
6016                                 break;
6017                         case SCTP_REMOTE_ERROR:
6018                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
6019                                 break;
6020                         case SCTP_SEND_FAILED:
6021                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
6022                                 break;
6023                         case SCTP_SHUTDOWN_EVENT:
6024                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
6025                                 break;
6026                         case SCTP_ADAPTATION_INDICATION:
6027                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
6028                                 break;
6029                         case SCTP_PARTIAL_DELIVERY_EVENT:
6030                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
6031                                 break;
6032                         case SCTP_AUTHENTICATION_EVENT:
6033                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
6034                                 break;
6035                         case SCTP_STREAM_RESET_EVENT:
6036                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
6037                                 break;
6038                         case SCTP_SENDER_DRY_EVENT:
6039                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
6040                                 break;
6041                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
6042                                 event_type = 0;
6043                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6044                                 error = ENOTSUP;
6045                                 break;
6046                         case SCTP_ASSOC_RESET_EVENT:
6047                                 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
6048                                 break;
6049                         case SCTP_STREAM_CHANGE_EVENT:
6050                                 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
6051                                 break;
6052                         case SCTP_SEND_FAILED_EVENT:
6053                                 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
6054                                 break;
6055                         default:
6056                                 event_type = 0;
6057                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6058                                 error = EINVAL;
6059                                 break;
6060                         }
6061                         if (event_type > 0) {
6062                                 if (stcb) {
6063                                         if (event->se_on) {
6064                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6065                                                 if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6066                                                         if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6067                                                             TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6068                                                             (stcb->asoc.stream_queue_cnt == 0)) {
6069                                                                 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
6070                                                         }
6071                                                 }
6072                                         } else {
6073                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6074                                         }
6075                                         SCTP_TCB_UNLOCK(stcb);
6076                                 } else {
6077                                         /*
6078                                          * We don't want to send up a storm
6079                                          * of events, so return an error for
6080                                          * sender dry events
6081                                          */
6082                                         if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6083                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6084                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6085                                             ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6086                                             (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6087                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6088                                                 error = ENOTSUP;
6089                                                 break;
6090                                         }
6091                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6092                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6093                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6094                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6095                                                 SCTP_INP_WLOCK(inp);
6096                                                 if (event->se_on) {
6097                                                         sctp_feature_on(inp, event_type);
6098                                                 } else {
6099                                                         sctp_feature_off(inp, event_type);
6100                                                 }
6101                                                 SCTP_INP_WUNLOCK(inp);
6102                                         }
6103                                         if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6104                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6105                                                 SCTP_INP_RLOCK(inp);
6106                                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6107                                                         SCTP_TCB_LOCK(stcb);
6108                                                         if (event->se_on) {
6109                                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6110                                                         } else {
6111                                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6112                                                         }
6113                                                         SCTP_TCB_UNLOCK(stcb);
6114                                                 }
6115                                                 SCTP_INP_RUNLOCK(inp);
6116                                         }
6117                                 }
6118                         }
6119                         break;
6120                 }
6121         case SCTP_RECVRCVINFO:
6122                 {
6123                         int *onoff;
6124
6125                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6126                         SCTP_INP_WLOCK(inp);
6127                         if (*onoff != 0) {
6128                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6129                         } else {
6130                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6131                         }
6132                         SCTP_INP_WUNLOCK(inp);
6133                         break;
6134                 }
6135         case SCTP_RECVNXTINFO:
6136                 {
6137                         int *onoff;
6138
6139                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6140                         SCTP_INP_WLOCK(inp);
6141                         if (*onoff != 0) {
6142                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6143                         } else {
6144                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6145                         }
6146                         SCTP_INP_WUNLOCK(inp);
6147                         break;
6148                 }
6149         case SCTP_DEFAULT_SNDINFO:
6150                 {
6151                         struct sctp_sndinfo *info;
6152                         uint16_t policy;
6153
6154                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6155                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6156
6157                         if (stcb) {
6158                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6159                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6160                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6161                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6162                                         stcb->asoc.def_send.sinfo_flags |= policy;
6163                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6164                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6165                                 } else {
6166                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6167                                         error = EINVAL;
6168                                 }
6169                                 SCTP_TCB_UNLOCK(stcb);
6170                         } else {
6171                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6172                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6173                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6174                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6175                                         SCTP_INP_WLOCK(inp);
6176                                         inp->def_send.sinfo_stream = info->snd_sid;
6177                                         policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6178                                         inp->def_send.sinfo_flags = info->snd_flags;
6179                                         inp->def_send.sinfo_flags |= policy;
6180                                         inp->def_send.sinfo_ppid = info->snd_ppid;
6181                                         inp->def_send.sinfo_context = info->snd_context;
6182                                         SCTP_INP_WUNLOCK(inp);
6183                                 }
6184                                 if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6185                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6186                                         SCTP_INP_RLOCK(inp);
6187                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6188                                                 SCTP_TCB_LOCK(stcb);
6189                                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6190                                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6191                                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6192                                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6193                                                         stcb->asoc.def_send.sinfo_flags |= policy;
6194                                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6195                                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6196                                                 }
6197                                                 SCTP_TCB_UNLOCK(stcb);
6198                                         }
6199                                         SCTP_INP_RUNLOCK(inp);
6200                                 }
6201                         }
6202                         break;
6203                 }
6204         case SCTP_DEFAULT_PRINFO:
6205                 {
6206                         struct sctp_default_prinfo *info;
6207
6208                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6209                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6210
6211                         if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6212                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6213                                 error = EINVAL;
6214                                 break;
6215                         }
6216                         if (stcb) {
6217                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6218                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6219                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6220                                 SCTP_TCB_UNLOCK(stcb);
6221                         } else {
6222                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6223                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6224                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6225                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6226                                         SCTP_INP_WLOCK(inp);
6227                                         inp->def_send.sinfo_flags &= 0xfff0;
6228                                         inp->def_send.sinfo_flags |= info->pr_policy;
6229                                         inp->def_send.sinfo_timetolive = info->pr_value;
6230                                         SCTP_INP_WUNLOCK(inp);
6231                                 }
6232                                 if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6233                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6234                                         SCTP_INP_RLOCK(inp);
6235                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6236                                                 SCTP_TCB_LOCK(stcb);
6237                                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6238                                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6239                                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6240                                                 SCTP_TCB_UNLOCK(stcb);
6241                                         }
6242                                         SCTP_INP_RUNLOCK(inp);
6243                                 }
6244                         }
6245                         break;
6246                 }
6247         case SCTP_PEER_ADDR_THLDS:
6248                 /* Applies to the specific association */
6249                 {
6250                         struct sctp_paddrthlds *thlds;
6251                         struct sctp_nets *net;
6252                         struct sockaddr *addr;
6253 #if defined(INET) && defined(INET6)
6254                         struct sockaddr_in sin_store;
6255 #endif
6256
6257                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6258                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6259
6260 #if defined(INET) && defined(INET6)
6261                         if (thlds->spt_address.ss_family == AF_INET6) {
6262                                 struct sockaddr_in6 *sin6;
6263
6264                                 sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6265                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6266                                         in6_sin6_2_sin(&sin_store, sin6);
6267                                         addr = (struct sockaddr *)&sin_store;
6268                                 } else {
6269                                         addr = (struct sockaddr *)&thlds->spt_address;
6270                                 }
6271                         } else {
6272                                 addr = (struct sockaddr *)&thlds->spt_address;
6273                         }
6274 #else
6275                         addr = (struct sockaddr *)&thlds->spt_address;
6276 #endif
6277                         if (stcb != NULL) {
6278                                 net = sctp_findnet(stcb, addr);
6279                         } else {
6280                                 /*
6281                                  * We increment here since
6282                                  * sctp_findassociation_ep_addr() wil do a
6283                                  * decrement if it finds the stcb as long as
6284                                  * the locked tcb (last argument) is NOT a
6285                                  * TCB.. aka NULL.
6286                                  */
6287                                 net = NULL;
6288                                 SCTP_INP_INCR_REF(inp);
6289                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
6290                                     &net, NULL, NULL);
6291                                 if (stcb == NULL) {
6292                                         SCTP_INP_DECR_REF(inp);
6293                                 }
6294                         }
6295                         if ((stcb != NULL) && (net == NULL)) {
6296 #ifdef INET
6297                                 if (addr->sa_family == AF_INET) {
6298
6299                                         struct sockaddr_in *sin;
6300
6301                                         sin = (struct sockaddr_in *)addr;
6302                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6303                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6304                                                 SCTP_TCB_UNLOCK(stcb);
6305                                                 error = EINVAL;
6306                                                 break;
6307                                         }
6308                                 } else
6309 #endif
6310 #ifdef INET6
6311                                 if (addr->sa_family == AF_INET6) {
6312                                         struct sockaddr_in6 *sin6;
6313
6314                                         sin6 = (struct sockaddr_in6 *)addr;
6315                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6316                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6317                                                 SCTP_TCB_UNLOCK(stcb);
6318                                                 error = EINVAL;
6319                                                 break;
6320                                         }
6321                                 } else
6322 #endif
6323                                 {
6324                                         error = EAFNOSUPPORT;
6325                                         SCTP_TCB_UNLOCK(stcb);
6326                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6327                                         break;
6328                                 }
6329                         }
6330                         if (thlds->spt_pathcpthld != 0xffff) {
6331                                 error = EINVAL;
6332                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6333                                 break;
6334                         }
6335                         if (stcb != NULL) {
6336                                 if (net != NULL) {
6337                                         net->failure_threshold = thlds->spt_pathmaxrxt;
6338                                         net->pf_threshold = thlds->spt_pathpfthld;
6339                                         if (net->dest_state & SCTP_ADDR_PF) {
6340                                                 if ((net->error_count > net->failure_threshold) ||
6341                                                     (net->error_count <= net->pf_threshold)) {
6342                                                         net->dest_state &= ~SCTP_ADDR_PF;
6343                                                 }
6344                                         } else {
6345                                                 if ((net->error_count > net->pf_threshold) &&
6346                                                     (net->error_count <= net->failure_threshold)) {
6347                                                         net->dest_state |= SCTP_ADDR_PF;
6348                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6349                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6350                                                             stcb->sctp_ep, stcb, net,
6351                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
6352                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6353                                                 }
6354                                         }
6355                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
6356                                                 if (net->error_count > net->failure_threshold) {
6357                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
6358                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6359                                                 }
6360                                         } else {
6361                                                 if (net->error_count <= net->failure_threshold) {
6362                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
6363                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6364                                                 }
6365                                         }
6366                                 } else {
6367                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
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_18);
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                                         }
6398                                         stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6399                                         stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6400                                 }
6401                                 SCTP_TCB_UNLOCK(stcb);
6402                         } else {
6403                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6404                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6405                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6406                                         SCTP_INP_WLOCK(inp);
6407                                         inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6408                                         inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6409                                         SCTP_INP_WUNLOCK(inp);
6410                                 } else {
6411                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6412                                         error = EINVAL;
6413                                 }
6414                         }
6415                         break;
6416                 }
6417         case SCTP_REMOTE_UDP_ENCAPS_PORT:
6418                 {
6419                         struct sctp_udpencaps *encaps;
6420                         struct sctp_nets *net;
6421                         struct sockaddr *addr;
6422 #if defined(INET) && defined(INET6)
6423                         struct sockaddr_in sin_store;
6424 #endif
6425
6426                         SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6427                         SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6428
6429 #if defined(INET) && defined(INET6)
6430                         if (encaps->sue_address.ss_family == AF_INET6) {
6431                                 struct sockaddr_in6 *sin6;
6432
6433                                 sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6434                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6435                                         in6_sin6_2_sin(&sin_store, sin6);
6436                                         addr = (struct sockaddr *)&sin_store;
6437                                 } else {
6438                                         addr = (struct sockaddr *)&encaps->sue_address;
6439                                 }
6440                         } else {
6441                                 addr = (struct sockaddr *)&encaps->sue_address;
6442                         }
6443 #else
6444                         addr = (struct sockaddr *)&encaps->sue_address;
6445 #endif
6446                         if (stcb != NULL) {
6447                                 net = sctp_findnet(stcb, addr);
6448                         } else {
6449                                 /*
6450                                  * We increment here since
6451                                  * sctp_findassociation_ep_addr() wil do a
6452                                  * decrement if it finds the stcb as long as
6453                                  * the locked tcb (last argument) is NOT a
6454                                  * TCB.. aka NULL.
6455                                  */
6456                                 net = NULL;
6457                                 SCTP_INP_INCR_REF(inp);
6458                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6459                                 if (stcb == NULL) {
6460                                         SCTP_INP_DECR_REF(inp);
6461                                 }
6462                         }
6463                         if ((stcb != NULL) && (net == NULL)) {
6464 #ifdef INET
6465                                 if (addr->sa_family == AF_INET) {
6466
6467                                         struct sockaddr_in *sin;
6468
6469                                         sin = (struct sockaddr_in *)addr;
6470                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6471                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6472                                                 SCTP_TCB_UNLOCK(stcb);
6473                                                 error = EINVAL;
6474                                                 break;
6475                                         }
6476                                 } else
6477 #endif
6478 #ifdef INET6
6479                                 if (addr->sa_family == AF_INET6) {
6480                                         struct sockaddr_in6 *sin6;
6481
6482                                         sin6 = (struct sockaddr_in6 *)addr;
6483                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6484                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6485                                                 SCTP_TCB_UNLOCK(stcb);
6486                                                 error = EINVAL;
6487                                                 break;
6488                                         }
6489                                 } else
6490 #endif
6491                                 {
6492                                         error = EAFNOSUPPORT;
6493                                         SCTP_TCB_UNLOCK(stcb);
6494                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6495                                         break;
6496                                 }
6497                         }
6498
6499                         if (stcb != NULL) {
6500                                 if (net != NULL) {
6501                                         net->port = encaps->sue_port;
6502                                 } else {
6503                                         stcb->asoc.port = encaps->sue_port;
6504                                 }
6505                                 SCTP_TCB_UNLOCK(stcb);
6506                         } else {
6507                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6508                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6509                                     (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6510                                         SCTP_INP_WLOCK(inp);
6511                                         inp->sctp_ep.port = encaps->sue_port;
6512                                         SCTP_INP_WUNLOCK(inp);
6513                                 } else {
6514                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6515                                         error = EINVAL;
6516                                 }
6517                         }
6518                         break;
6519                 }
6520         case SCTP_ECN_SUPPORTED:
6521                 {
6522                         struct sctp_assoc_value *av;
6523
6524                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6525                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6526
6527                         if (stcb) {
6528                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6529                                 error = EINVAL;
6530                                 SCTP_TCB_UNLOCK(stcb);
6531                         } else {
6532                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6533                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6534                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6535                                         SCTP_INP_WLOCK(inp);
6536                                         if (av->assoc_value == 0) {
6537                                                 inp->ecn_supported = 0;
6538                                         } else {
6539                                                 inp->ecn_supported = 1;
6540                                         }
6541                                         SCTP_INP_WUNLOCK(inp);
6542                                 } else {
6543                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6544                                         error = EINVAL;
6545                                 }
6546                         }
6547                         break;
6548                 }
6549         case SCTP_PR_SUPPORTED:
6550                 {
6551                         struct sctp_assoc_value *av;
6552
6553                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6554                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6555
6556                         if (stcb) {
6557                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6558                                 error = EINVAL;
6559                                 SCTP_TCB_UNLOCK(stcb);
6560                         } else {
6561                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6562                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6563                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6564                                         SCTP_INP_WLOCK(inp);
6565                                         if (av->assoc_value == 0) {
6566                                                 inp->prsctp_supported = 0;
6567                                         } else {
6568                                                 inp->prsctp_supported = 1;
6569                                         }
6570                                         SCTP_INP_WUNLOCK(inp);
6571                                 } else {
6572                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6573                                         error = EINVAL;
6574                                 }
6575                         }
6576                         break;
6577                 }
6578         case SCTP_AUTH_SUPPORTED:
6579                 {
6580                         struct sctp_assoc_value *av;
6581
6582                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6583                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6584
6585                         if (stcb) {
6586                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6587                                 error = EINVAL;
6588                                 SCTP_TCB_UNLOCK(stcb);
6589                         } else {
6590                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6591                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6592                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6593                                         if ((av->assoc_value == 0) &&
6594                                             (inp->asconf_supported == 1)) {
6595                                                 /*
6596                                                  * AUTH is required for
6597                                                  * ASCONF
6598                                                  */
6599                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6600                                                 error = EINVAL;
6601                                         } else {
6602                                                 SCTP_INP_WLOCK(inp);
6603                                                 if (av->assoc_value == 0) {
6604                                                         inp->auth_supported = 0;
6605                                                 } else {
6606                                                         inp->auth_supported = 1;
6607                                                 }
6608                                                 SCTP_INP_WUNLOCK(inp);
6609                                         }
6610                                 } else {
6611                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6612                                         error = EINVAL;
6613                                 }
6614                         }
6615                         break;
6616                 }
6617         case SCTP_ASCONF_SUPPORTED:
6618                 {
6619                         struct sctp_assoc_value *av;
6620
6621                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6622                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6623
6624                         if (stcb) {
6625                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6626                                 error = EINVAL;
6627                                 SCTP_TCB_UNLOCK(stcb);
6628                         } else {
6629                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6630                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6631                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6632                                         if ((av->assoc_value != 0) &&
6633                                             (inp->auth_supported == 0)) {
6634                                                 /*
6635                                                  * AUTH is required for
6636                                                  * ASCONF
6637                                                  */
6638                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6639                                                 error = EINVAL;
6640                                         } else {
6641                                                 SCTP_INP_WLOCK(inp);
6642                                                 if (av->assoc_value == 0) {
6643                                                         inp->asconf_supported = 0;
6644                                                         sctp_auth_delete_chunk(SCTP_ASCONF,
6645                                                             inp->sctp_ep.local_auth_chunks);
6646                                                         sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6647                                                             inp->sctp_ep.local_auth_chunks);
6648                                                 } else {
6649                                                         inp->asconf_supported = 1;
6650                                                         sctp_auth_add_chunk(SCTP_ASCONF,
6651                                                             inp->sctp_ep.local_auth_chunks);
6652                                                         sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6653                                                             inp->sctp_ep.local_auth_chunks);
6654                                                 }
6655                                                 SCTP_INP_WUNLOCK(inp);
6656                                         }
6657                                 } else {
6658                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6659                                         error = EINVAL;
6660                                 }
6661                         }
6662                         break;
6663                 }
6664         case SCTP_RECONFIG_SUPPORTED:
6665                 {
6666                         struct sctp_assoc_value *av;
6667
6668                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6669                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6670
6671                         if (stcb) {
6672                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6673                                 error = EINVAL;
6674                                 SCTP_TCB_UNLOCK(stcb);
6675                         } else {
6676                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6677                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6678                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6679                                         SCTP_INP_WLOCK(inp);
6680                                         if (av->assoc_value == 0) {
6681                                                 inp->reconfig_supported = 0;
6682                                         } else {
6683                                                 inp->reconfig_supported = 1;
6684                                         }
6685                                         SCTP_INP_WUNLOCK(inp);
6686                                 } else {
6687                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6688                                         error = EINVAL;
6689                                 }
6690                         }
6691                         break;
6692                 }
6693         case SCTP_NRSACK_SUPPORTED:
6694                 {
6695                         struct sctp_assoc_value *av;
6696
6697                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6698                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6699
6700                         if (stcb) {
6701                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6702                                 error = EINVAL;
6703                                 SCTP_TCB_UNLOCK(stcb);
6704                         } else {
6705                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6706                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6707                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6708                                         SCTP_INP_WLOCK(inp);
6709                                         if (av->assoc_value == 0) {
6710                                                 inp->nrsack_supported = 0;
6711                                         } else {
6712                                                 inp->nrsack_supported = 1;
6713                                         }
6714                                         SCTP_INP_WUNLOCK(inp);
6715                                 } else {
6716                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6717                                         error = EINVAL;
6718                                 }
6719                         }
6720                         break;
6721                 }
6722         case SCTP_PKTDROP_SUPPORTED:
6723                 {
6724                         struct sctp_assoc_value *av;
6725
6726                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6727                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6728
6729                         if (stcb) {
6730                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6731                                 error = EINVAL;
6732                                 SCTP_TCB_UNLOCK(stcb);
6733                         } else {
6734                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6735                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6736                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6737                                         SCTP_INP_WLOCK(inp);
6738                                         if (av->assoc_value == 0) {
6739                                                 inp->pktdrop_supported = 0;
6740                                         } else {
6741                                                 inp->pktdrop_supported = 1;
6742                                         }
6743                                         SCTP_INP_WUNLOCK(inp);
6744                                 } else {
6745                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6746                                         error = EINVAL;
6747                                 }
6748                         }
6749                         break;
6750                 }
6751         case SCTP_MAX_CWND:
6752                 {
6753                         struct sctp_assoc_value *av;
6754                         struct sctp_nets *net;
6755
6756                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6757                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6758
6759                         if (stcb) {
6760                                 stcb->asoc.max_cwnd = av->assoc_value;
6761                                 if (stcb->asoc.max_cwnd > 0) {
6762                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6763                                                 if ((net->cwnd > stcb->asoc.max_cwnd) &&
6764                                                     (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
6765                                                         net->cwnd = stcb->asoc.max_cwnd;
6766                                                         if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
6767                                                                 net->cwnd = net->mtu - sizeof(struct sctphdr);
6768                                                         }
6769                                                 }
6770                                         }
6771                                 }
6772                                 SCTP_TCB_UNLOCK(stcb);
6773                         } else {
6774                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6775                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6776                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6777                                         SCTP_INP_WLOCK(inp);
6778                                         inp->max_cwnd = av->assoc_value;
6779                                         SCTP_INP_WUNLOCK(inp);
6780                                 } else {
6781                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6782                                         error = EINVAL;
6783                                 }
6784                         }
6785                         break;
6786                 }
6787         default:
6788                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6789                 error = ENOPROTOOPT;
6790                 break;
6791         }                       /* end switch (opt) */
6792         return (error);
6793 }
6794
6795 int
6796 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6797 {
6798         void *optval = NULL;
6799         size_t optsize = 0;
6800         void *p;
6801         int error = 0;
6802         struct sctp_inpcb *inp;
6803
6804         if ((sopt->sopt_level == SOL_SOCKET) &&
6805             (sopt->sopt_name == SO_SETFIB)) {
6806                 inp = (struct sctp_inpcb *)so->so_pcb;
6807                 if (inp == NULL) {
6808                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6809                         return (EINVAL);
6810                 }
6811                 SCTP_INP_WLOCK(inp);
6812                 inp->fibnum = so->so_fibnum;
6813                 SCTP_INP_WUNLOCK(inp);
6814                 return (0);
6815         }
6816         if (sopt->sopt_level != IPPROTO_SCTP) {
6817                 /* wrong proto level... send back up to IP */
6818 #ifdef INET6
6819                 if (INP_CHECK_SOCKAF(so, AF_INET6))
6820                         error = ip6_ctloutput(so, sopt);
6821 #endif                          /* INET6 */
6822 #if defined(INET) && defined(INET6)
6823                 else
6824 #endif
6825 #ifdef INET
6826                         error = ip_ctloutput(so, sopt);
6827 #endif
6828                 return (error);
6829         }
6830         optsize = sopt->sopt_valsize;
6831         if (optsize) {
6832                 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6833                 if (optval == NULL) {
6834                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6835                         return (ENOBUFS);
6836                 }
6837                 error = sooptcopyin(sopt, optval, optsize, optsize);
6838                 if (error) {
6839                         SCTP_FREE(optval, SCTP_M_SOCKOPT);
6840                         goto out;
6841                 }
6842         }
6843         p = (void *)sopt->sopt_td;
6844         if (sopt->sopt_dir == SOPT_SET) {
6845                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6846         } else if (sopt->sopt_dir == SOPT_GET) {
6847                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6848         } else {
6849                 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6850                 error = EINVAL;
6851         }
6852         if ((error == 0) && (optval != NULL)) {
6853                 error = sooptcopyout(sopt, optval, optsize);
6854                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6855         } else if (optval != NULL) {
6856                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6857         }
6858 out:
6859         return (error);
6860 }
6861
6862 #ifdef INET
6863 static int
6864 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6865 {
6866         int error = 0;
6867         int create_lock_on = 0;
6868         uint32_t vrf_id;
6869         struct sctp_inpcb *inp;
6870         struct sctp_tcb *stcb = NULL;
6871
6872         inp = (struct sctp_inpcb *)so->so_pcb;
6873         if (inp == NULL) {
6874                 /* I made the same as TCP since we are not setup? */
6875                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6876                 return (ECONNRESET);
6877         }
6878         if (addr == NULL) {
6879                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6880                 return EINVAL;
6881         }
6882
6883         switch (addr->sa_family) {
6884 #ifdef INET6
6885         case AF_INET6:
6886                 {
6887                         struct sockaddr_in6 *sin6p;
6888
6889                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6890                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6891                                 return (EINVAL);
6892                         }
6893                         sin6p = (struct sockaddr_in6 *)addr;
6894                         if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
6895                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6896                                 return (error);
6897                         }
6898                         break;
6899                 }
6900 #endif
6901 #ifdef INET
6902         case AF_INET:
6903                 {
6904                         struct sockaddr_in *sinp;
6905
6906                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
6907                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6908                                 return (EINVAL);
6909                         }
6910                         sinp = (struct sockaddr_in *)addr;
6911                         if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
6912                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6913                                 return (error);
6914                         }
6915                         break;
6916                 }
6917 #endif
6918         default:
6919                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6920                 return (EAFNOSUPPORT);
6921         }
6922         SCTP_INP_INCR_REF(inp);
6923         SCTP_ASOC_CREATE_LOCK(inp);
6924         create_lock_on = 1;
6925
6926
6927         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6928             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6929                 /* Should I really unlock ? */
6930                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6931                 error = EFAULT;
6932                 goto out_now;
6933         }
6934 #ifdef INET6
6935         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6936             (addr->sa_family == AF_INET6)) {
6937                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6938                 error = EINVAL;
6939                 goto out_now;
6940         }
6941 #endif
6942         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6943             SCTP_PCB_FLAGS_UNBOUND) {
6944                 /* Bind a ephemeral port */
6945                 error = sctp_inpcb_bind(so, NULL, NULL, p);
6946                 if (error) {
6947                         goto out_now;
6948                 }
6949         }
6950         /* Now do we connect? */
6951         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6952             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6953                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6954                 error = EINVAL;
6955                 goto out_now;
6956         }
6957         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6958             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6959                 /* We are already connected AND the TCP model */
6960                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6961                 error = EADDRINUSE;
6962                 goto out_now;
6963         }
6964         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6965                 SCTP_INP_RLOCK(inp);
6966                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
6967                 SCTP_INP_RUNLOCK(inp);
6968         } else {
6969                 /*
6970                  * We increment here since sctp_findassociation_ep_addr()
6971                  * will do a decrement if it finds the stcb as long as the
6972                  * locked tcb (last argument) is NOT a TCB.. aka NULL.
6973                  */
6974                 SCTP_INP_INCR_REF(inp);
6975                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
6976                 if (stcb == NULL) {
6977                         SCTP_INP_DECR_REF(inp);
6978                 } else {
6979                         SCTP_TCB_UNLOCK(stcb);
6980                 }
6981         }
6982         if (stcb != NULL) {
6983                 /* Already have or am bring up an association */
6984                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
6985                 error = EALREADY;
6986                 goto out_now;
6987         }
6988
6989         vrf_id = inp->def_vrf_id;
6990         /* We are GOOD to go */
6991         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
6992             inp->sctp_ep.pre_open_stream_count,
6993             inp->sctp_ep.port, p);
6994         if (stcb == NULL) {
6995                 /* Gak! no memory */
6996                 goto out_now;
6997         }
6998         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
6999                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
7000                 /* Set the connected flag so we can queue data */
7001                 soisconnecting(so);
7002         }
7003         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
7004         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
7005
7006         /* initialize authentication parameters for the assoc */
7007         sctp_initialize_auth_params(inp, stcb);
7008
7009         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
7010         SCTP_TCB_UNLOCK(stcb);
7011 out_now:
7012         if (create_lock_on) {
7013                 SCTP_ASOC_CREATE_UNLOCK(inp);
7014         }
7015
7016         SCTP_INP_DECR_REF(inp);
7017         return (error);
7018 }
7019 #endif
7020
7021 int
7022 sctp_listen(struct socket *so, int backlog, struct thread *p)
7023 {
7024         /*
7025          * Note this module depends on the protocol processing being called
7026          * AFTER any socket level flags and backlog are applied to the
7027          * socket. The traditional way that the socket flags are applied is
7028          * AFTER protocol processing. We have made a change to the
7029          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
7030          * place if the socket API for SCTP is to work properly.
7031          */
7032
7033         int error = 0;
7034         struct sctp_inpcb *inp;
7035
7036         inp = (struct sctp_inpcb *)so->so_pcb;
7037         if (inp == NULL) {
7038                 /* I made the same as TCP since we are not setup? */
7039                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7040                 return (ECONNRESET);
7041         }
7042         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
7043                 /* See if we have a listener */
7044                 struct sctp_inpcb *tinp;
7045                 union sctp_sockstore store;
7046
7047                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
7048                         /* not bound all */
7049                         struct sctp_laddr *laddr;
7050
7051                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7052                                 memcpy(&store, &laddr->ifa->address, sizeof(store));
7053                                 switch (store.sa.sa_family) {
7054 #ifdef INET
7055                                 case AF_INET:
7056                                         store.sin.sin_port = inp->sctp_lport;
7057                                         break;
7058 #endif
7059 #ifdef INET6
7060                                 case AF_INET6:
7061                                         store.sin6.sin6_port = inp->sctp_lport;
7062                                         break;
7063 #endif
7064                                 default:
7065                                         break;
7066                                 }
7067                                 tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7068                                 if (tinp && (tinp != inp) &&
7069                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7070                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7071                                     (SCTP_IS_LISTENING(tinp))) {
7072                                         /*
7073                                          * we have a listener already and
7074                                          * its not this inp.
7075                                          */
7076                                         SCTP_INP_DECR_REF(tinp);
7077                                         return (EADDRINUSE);
7078                                 } else if (tinp) {
7079                                         SCTP_INP_DECR_REF(tinp);
7080                                 }
7081                         }
7082                 } else {
7083                         /* Setup a local addr bound all */
7084                         memset(&store, 0, sizeof(store));
7085 #ifdef INET6
7086                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7087                                 store.sa.sa_family = AF_INET6;
7088                                 store.sa.sa_len = sizeof(struct sockaddr_in6);
7089                         }
7090 #endif
7091 #ifdef INET
7092                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7093                                 store.sa.sa_family = AF_INET;
7094                                 store.sa.sa_len = sizeof(struct sockaddr_in);
7095                         }
7096 #endif
7097                         switch (store.sa.sa_family) {
7098 #ifdef INET
7099                         case AF_INET:
7100                                 store.sin.sin_port = inp->sctp_lport;
7101                                 break;
7102 #endif
7103 #ifdef INET6
7104                         case AF_INET6:
7105                                 store.sin6.sin6_port = inp->sctp_lport;
7106                                 break;
7107 #endif
7108                         default:
7109                                 break;
7110                         }
7111                         tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7112                         if (tinp && (tinp != inp) &&
7113                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7114                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7115                             (SCTP_IS_LISTENING(tinp))) {
7116                                 /*
7117                                  * we have a listener already and its not
7118                                  * this inp.
7119                                  */
7120                                 SCTP_INP_DECR_REF(tinp);
7121                                 return (EADDRINUSE);
7122                         } else if (tinp) {
7123                                 SCTP_INP_DECR_REF(tinp);
7124                         }
7125                 }
7126         }
7127         SCTP_INP_RLOCK(inp);
7128 #ifdef SCTP_LOCK_LOGGING
7129         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7130                 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7131         }
7132 #endif
7133         SOCK_LOCK(so);
7134         error = solisten_proto_check(so);
7135         SOCK_UNLOCK(so);
7136         if (error) {
7137                 SCTP_INP_RUNLOCK(inp);
7138                 return (error);
7139         }
7140         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7141             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7142                 /*
7143                  * The unlucky case - We are in the tcp pool with this guy.
7144                  * - Someone else is in the main inp slot. - We must move
7145                  * this guy (the listener) to the main slot - We must then
7146                  * move the guy that was listener to the TCP Pool.
7147                  */
7148                 if (sctp_swap_inpcb_for_listen(inp)) {
7149                         SCTP_INP_RUNLOCK(inp);
7150                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7151                         return (EADDRINUSE);
7152                 }
7153         }
7154
7155         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7156             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7157                 /* We are already connected AND the TCP model */
7158                 SCTP_INP_RUNLOCK(inp);
7159                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7160                 return (EADDRINUSE);
7161         }
7162         SCTP_INP_RUNLOCK(inp);
7163         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7164                 /* We must do a bind. */
7165                 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7166                         /* bind error, probably perm */
7167                         return (error);
7168                 }
7169         }
7170         SCTP_INP_WLOCK(inp);
7171         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) == 0) {
7172                 SOCK_LOCK(so);
7173                 solisten_proto(so, backlog);
7174                 SOCK_UNLOCK(so);
7175         }
7176         if (backlog > 0) {
7177                 inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
7178         } else {
7179                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
7180         }
7181         SCTP_INP_WUNLOCK(inp);
7182         return (error);
7183 }
7184
7185 static int sctp_defered_wakeup_cnt = 0;
7186
7187 int
7188 sctp_accept(struct socket *so, struct sockaddr **addr)
7189 {
7190         struct sctp_tcb *stcb;
7191         struct sctp_inpcb *inp;
7192         union sctp_sockstore store;
7193 #ifdef INET6
7194         int error;
7195 #endif
7196         inp = (struct sctp_inpcb *)so->so_pcb;
7197
7198         if (inp == NULL) {
7199                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7200                 return (ECONNRESET);
7201         }
7202         SCTP_INP_RLOCK(inp);
7203         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7204                 SCTP_INP_RUNLOCK(inp);
7205                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7206                 return (EOPNOTSUPP);
7207         }
7208         if (so->so_state & SS_ISDISCONNECTED) {
7209                 SCTP_INP_RUNLOCK(inp);
7210                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7211                 return (ECONNABORTED);
7212         }
7213         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7214         if (stcb == NULL) {
7215                 SCTP_INP_RUNLOCK(inp);
7216                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7217                 return (ECONNRESET);
7218         }
7219         SCTP_TCB_LOCK(stcb);
7220         SCTP_INP_RUNLOCK(inp);
7221         store = stcb->asoc.primary_destination->ro._l_addr;
7222         SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
7223         SCTP_TCB_UNLOCK(stcb);
7224         switch (store.sa.sa_family) {
7225 #ifdef INET
7226         case AF_INET:
7227                 {
7228                         struct sockaddr_in *sin;
7229
7230                         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7231                         if (sin == NULL)
7232                                 return (ENOMEM);
7233                         sin->sin_family = AF_INET;
7234                         sin->sin_len = sizeof(*sin);
7235                         sin->sin_port = store.sin.sin_port;
7236                         sin->sin_addr = store.sin.sin_addr;
7237                         *addr = (struct sockaddr *)sin;
7238                         break;
7239                 }
7240 #endif
7241 #ifdef INET6
7242         case AF_INET6:
7243                 {
7244                         struct sockaddr_in6 *sin6;
7245
7246                         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7247                         if (sin6 == NULL)
7248                                 return (ENOMEM);
7249                         sin6->sin6_family = AF_INET6;
7250                         sin6->sin6_len = sizeof(*sin6);
7251                         sin6->sin6_port = store.sin6.sin6_port;
7252                         sin6->sin6_addr = store.sin6.sin6_addr;
7253                         if ((error = sa6_recoverscope(sin6)) != 0) {
7254                                 SCTP_FREE_SONAME(sin6);
7255                                 return (error);
7256                         }
7257                         *addr = (struct sockaddr *)sin6;
7258                         break;
7259                 }
7260 #endif
7261         default:
7262                 /* TSNH */
7263                 break;
7264         }
7265         /* Wake any delayed sleep action */
7266         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7267                 SCTP_INP_WLOCK(inp);
7268                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7269                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7270                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7271                         SCTP_INP_WUNLOCK(inp);
7272                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7273                         if (sowriteable(inp->sctp_socket)) {
7274                                 sowwakeup_locked(inp->sctp_socket);
7275                         } else {
7276                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7277                         }
7278                         SCTP_INP_WLOCK(inp);
7279                 }
7280                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7281                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7282                         SCTP_INP_WUNLOCK(inp);
7283                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7284                         if (soreadable(inp->sctp_socket)) {
7285                                 sctp_defered_wakeup_cnt++;
7286                                 sorwakeup_locked(inp->sctp_socket);
7287                         } else {
7288                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7289                         }
7290                         SCTP_INP_WLOCK(inp);
7291                 }
7292                 SCTP_INP_WUNLOCK(inp);
7293         }
7294         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7295                 SCTP_TCB_LOCK(stcb);
7296                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7297                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
7298         }
7299         return (0);
7300 }
7301
7302 #ifdef INET
7303 int
7304 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7305 {
7306         struct sockaddr_in *sin;
7307         uint32_t vrf_id;
7308         struct sctp_inpcb *inp;
7309         struct sctp_ifa *sctp_ifa;
7310
7311         /*
7312          * Do the malloc first in case it blocks.
7313          */
7314         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7315         if (sin == NULL)
7316                 return (ENOMEM);
7317         sin->sin_family = AF_INET;
7318         sin->sin_len = sizeof(*sin);
7319         inp = (struct sctp_inpcb *)so->so_pcb;
7320         if (!inp) {
7321                 SCTP_FREE_SONAME(sin);
7322                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7323                 return (ECONNRESET);
7324         }
7325         SCTP_INP_RLOCK(inp);
7326         sin->sin_port = inp->sctp_lport;
7327         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7328                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7329                         struct sctp_tcb *stcb;
7330                         struct sockaddr_in *sin_a;
7331                         struct sctp_nets *net;
7332                         int fnd;
7333
7334                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7335                         if (stcb == NULL) {
7336                                 goto notConn;
7337                         }
7338                         fnd = 0;
7339                         sin_a = NULL;
7340                         SCTP_TCB_LOCK(stcb);
7341                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7342                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7343                                 if (sin_a == NULL)
7344                                         /* this will make coverity happy */
7345                                         continue;
7346
7347                                 if (sin_a->sin_family == AF_INET) {
7348                                         fnd = 1;
7349                                         break;
7350                                 }
7351                         }
7352                         if ((!fnd) || (sin_a == NULL)) {
7353                                 /* punt */
7354                                 SCTP_TCB_UNLOCK(stcb);
7355                                 goto notConn;
7356                         }
7357
7358                         vrf_id = inp->def_vrf_id;
7359                         sctp_ifa = sctp_source_address_selection(inp,
7360                             stcb,
7361                             (sctp_route_t *)&net->ro,
7362                             net, 0, vrf_id);
7363                         if (sctp_ifa) {
7364                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7365                                 sctp_free_ifa(sctp_ifa);
7366                         }
7367                         SCTP_TCB_UNLOCK(stcb);
7368                 } else {
7369                         /* For the bound all case you get back 0 */
7370         notConn:
7371                         sin->sin_addr.s_addr = 0;
7372                 }
7373
7374         } else {
7375                 /* Take the first IPv4 address in the list */
7376                 struct sctp_laddr *laddr;
7377                 int fnd = 0;
7378
7379                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7380                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
7381                                 struct sockaddr_in *sin_a;
7382
7383                                 sin_a = &laddr->ifa->address.sin;
7384                                 sin->sin_addr = sin_a->sin_addr;
7385                                 fnd = 1;
7386                                 break;
7387                         }
7388                 }
7389                 if (!fnd) {
7390                         SCTP_FREE_SONAME(sin);
7391                         SCTP_INP_RUNLOCK(inp);
7392                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7393                         return (ENOENT);
7394                 }
7395         }
7396         SCTP_INP_RUNLOCK(inp);
7397         (*addr) = (struct sockaddr *)sin;
7398         return (0);
7399 }
7400
7401 int
7402 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7403 {
7404         struct sockaddr_in *sin;
7405         int fnd;
7406         struct sockaddr_in *sin_a;
7407         struct sctp_inpcb *inp;
7408         struct sctp_tcb *stcb;
7409         struct sctp_nets *net;
7410
7411         /* Do the malloc first in case it blocks. */
7412         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7413         if (sin == NULL)
7414                 return (ENOMEM);
7415         sin->sin_family = AF_INET;
7416         sin->sin_len = sizeof(*sin);
7417
7418         inp = (struct sctp_inpcb *)so->so_pcb;
7419         if ((inp == NULL) ||
7420             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7421                 /* UDP type and listeners will drop out here */
7422                 SCTP_FREE_SONAME(sin);
7423                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7424                 return (ENOTCONN);
7425         }
7426         SCTP_INP_RLOCK(inp);
7427         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7428         if (stcb) {
7429                 SCTP_TCB_LOCK(stcb);
7430         }
7431         SCTP_INP_RUNLOCK(inp);
7432         if (stcb == NULL) {
7433                 SCTP_FREE_SONAME(sin);
7434                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7435                 return (ECONNRESET);
7436         }
7437         fnd = 0;
7438         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7439                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7440                 if (sin_a->sin_family == AF_INET) {
7441                         fnd = 1;
7442                         sin->sin_port = stcb->rport;
7443                         sin->sin_addr = sin_a->sin_addr;
7444                         break;
7445                 }
7446         }
7447         SCTP_TCB_UNLOCK(stcb);
7448         if (!fnd) {
7449                 /* No IPv4 address */
7450                 SCTP_FREE_SONAME(sin);
7451                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7452                 return (ENOENT);
7453         }
7454         (*addr) = (struct sockaddr *)sin;
7455         return (0);
7456 }
7457
7458 struct pr_usrreqs sctp_usrreqs = {
7459         .pru_abort = sctp_abort,
7460         .pru_accept = sctp_accept,
7461         .pru_attach = sctp_attach,
7462         .pru_bind = sctp_bind,
7463         .pru_connect = sctp_connect,
7464         .pru_control = in_control,
7465         .pru_close = sctp_close,
7466         .pru_detach = sctp_close,
7467         .pru_sopoll = sopoll_generic,
7468         .pru_flush = sctp_flush,
7469         .pru_disconnect = sctp_disconnect,
7470         .pru_listen = sctp_listen,
7471         .pru_peeraddr = sctp_peeraddr,
7472         .pru_send = sctp_sendm,
7473         .pru_shutdown = sctp_shutdown,
7474         .pru_sockaddr = sctp_ingetaddr,
7475         .pru_sosend = sctp_sosend,
7476         .pru_soreceive = sctp_soreceive
7477 };
7478 #endif