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