]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_usrreq.c
ddb(4): Add 'sysctl' command
[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                 if (SCTP_IPV6_V6ONLY(inp)) {
1416                         /*
1417                          * if IPV6_V6ONLY flag, ignore connections destined
1418                          * to a v4 addr or v4-mapped addr
1419                          */
1420                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1421                         error = EINVAL;
1422                         goto out_now;
1423                 }
1424         }
1425 #endif                          /* INET6 */
1426         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1427             SCTP_PCB_FLAGS_UNBOUND) {
1428                 /* Bind a ephemeral port */
1429                 error = sctp_inpcb_bind(so, NULL, NULL, p);
1430                 if (error) {
1431                         goto out_now;
1432                 }
1433         }
1434
1435         /* FIX ME: do we want to pass in a vrf on the connect call? */
1436         vrf_id = inp->def_vrf_id;
1437
1438
1439         /* We are GOOD to go */
1440         stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
1441             inp->sctp_ep.pre_open_stream_count,
1442             inp->sctp_ep.port,
1443             (struct thread *)p,
1444             SCTP_INITIALIZE_AUTH_PARAMS);
1445         if (stcb == NULL) {
1446                 /* Gak! no memory */
1447                 goto out_now;
1448         }
1449         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1450                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1451                 /* Set the connected flag so we can queue data */
1452                 soisconnecting(so);
1453         }
1454         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1455         /* move to second address */
1456         switch (sa->sa_family) {
1457 #ifdef INET
1458         case AF_INET:
1459                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1460                 break;
1461 #endif
1462 #ifdef INET6
1463         case AF_INET6:
1464                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1465                 break;
1466 #endif
1467         default:
1468                 break;
1469         }
1470
1471         error = 0;
1472         sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1473         /* Fill in the return id */
1474         if (error) {
1475                 goto out_now;
1476         }
1477         a_id = (sctp_assoc_t *)optval;
1478         *a_id = sctp_get_associd(stcb);
1479
1480         if (delay) {
1481                 /* doing delayed connection */
1482                 stcb->asoc.delayed_connection = 1;
1483                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1484         } else {
1485                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1486                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1487         }
1488         SCTP_TCB_UNLOCK(stcb);
1489 out_now:
1490         if (creat_lock_on) {
1491                 SCTP_ASOC_CREATE_UNLOCK(inp);
1492         }
1493         SCTP_INP_DECR_REF(inp);
1494         return (error);
1495 }
1496
1497 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
1498         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
1499             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
1500                 SCTP_INP_RLOCK(inp); \
1501                 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1502                 if (stcb) { \
1503                         SCTP_TCB_LOCK(stcb); \
1504                 } \
1505                 SCTP_INP_RUNLOCK(inp); \
1506         } else if (assoc_id > SCTP_ALL_ASSOC) { \
1507                 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1508                 if (stcb == NULL) { \
1509                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
1510                         error = ENOENT; \
1511                         break; \
1512                 } \
1513         } else { \
1514                 stcb = NULL; \
1515         } \
1516 }
1517
1518
1519 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
1520         if (size < sizeof(type)) { \
1521                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
1522                 error = EINVAL; \
1523                 break; \
1524         } else { \
1525                 destp = (type *)srcp; \
1526         } \
1527 }
1528
1529 static int
1530 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1531     void *p)
1532 {
1533         struct sctp_inpcb *inp = NULL;
1534         int error, val = 0;
1535         struct sctp_tcb *stcb = NULL;
1536
1537         if (optval == NULL) {
1538                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1539                 return (EINVAL);
1540         }
1541
1542         inp = (struct sctp_inpcb *)so->so_pcb;
1543         if (inp == NULL) {
1544                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1545                 return EINVAL;
1546         }
1547         error = 0;
1548
1549         switch (optname) {
1550         case SCTP_NODELAY:
1551         case SCTP_AUTOCLOSE:
1552         case SCTP_EXPLICIT_EOR:
1553         case SCTP_AUTO_ASCONF:
1554         case SCTP_DISABLE_FRAGMENTS:
1555         case SCTP_I_WANT_MAPPED_V4_ADDR:
1556         case SCTP_USE_EXT_RCVINFO:
1557                 SCTP_INP_RLOCK(inp);
1558                 switch (optname) {
1559                 case SCTP_DISABLE_FRAGMENTS:
1560                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1561                         break;
1562                 case SCTP_I_WANT_MAPPED_V4_ADDR:
1563                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1564                         break;
1565                 case SCTP_AUTO_ASCONF:
1566                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1567                                 /* only valid for bound all sockets */
1568                                 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1569                         } else {
1570                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1571                                 error = EINVAL;
1572                                 goto flags_out;
1573                         }
1574                         break;
1575                 case SCTP_EXPLICIT_EOR:
1576                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1577                         break;
1578                 case SCTP_NODELAY:
1579                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1580                         break;
1581                 case SCTP_USE_EXT_RCVINFO:
1582                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1583                         break;
1584                 case SCTP_AUTOCLOSE:
1585                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1586                                 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1587                         else
1588                                 val = 0;
1589                         break;
1590
1591                 default:
1592                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
1593                         error = ENOPROTOOPT;
1594                 }               /* end switch (sopt->sopt_name) */
1595                 if (*optsize < sizeof(val)) {
1596                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1597                         error = EINVAL;
1598                 }
1599 flags_out:
1600                 SCTP_INP_RUNLOCK(inp);
1601                 if (error == 0) {
1602                         /* return the option value */
1603                         *(int *)optval = val;
1604                         *optsize = sizeof(val);
1605                 }
1606                 break;
1607         case SCTP_GET_PACKET_LOG:
1608                 {
1609 #ifdef  SCTP_PACKET_LOGGING
1610                         uint8_t *target;
1611                         int ret;
1612
1613                         SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
1614                         ret = sctp_copy_out_packet_log(target, (int)*optsize);
1615                         *optsize = ret;
1616 #else
1617                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1618                         error = EOPNOTSUPP;
1619 #endif
1620                         break;
1621                 }
1622         case SCTP_REUSE_PORT:
1623                 {
1624                         uint32_t *value;
1625
1626                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
1627                                 /* Can't do this for a 1-m socket */
1628                                 error = EINVAL;
1629                                 break;
1630                         }
1631                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1632                         *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
1633                         *optsize = sizeof(uint32_t);
1634                         break;
1635                 }
1636         case SCTP_PARTIAL_DELIVERY_POINT:
1637                 {
1638                         uint32_t *value;
1639
1640                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1641                         *value = inp->partial_delivery_point;
1642                         *optsize = sizeof(uint32_t);
1643                         break;
1644                 }
1645         case SCTP_FRAGMENT_INTERLEAVE:
1646                 {
1647                         uint32_t *value;
1648
1649                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1650                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1651                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1652                                         *value = SCTP_FRAG_LEVEL_2;
1653                                 } else {
1654                                         *value = SCTP_FRAG_LEVEL_1;
1655                                 }
1656                         } else {
1657                                 *value = SCTP_FRAG_LEVEL_0;
1658                         }
1659                         *optsize = sizeof(uint32_t);
1660                         break;
1661                 }
1662         case SCTP_INTERLEAVING_SUPPORTED:
1663                 {
1664                         struct sctp_assoc_value *av;
1665
1666                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1667                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1668
1669                         if (stcb) {
1670                                 av->assoc_value = stcb->asoc.idata_supported;
1671                                 SCTP_TCB_UNLOCK(stcb);
1672                         } else {
1673                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1674                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1675                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1676                                         SCTP_INP_RLOCK(inp);
1677                                         if (inp->idata_supported) {
1678                                                 av->assoc_value = 1;
1679                                         } else {
1680                                                 av->assoc_value = 0;
1681                                         }
1682                                         SCTP_INP_RUNLOCK(inp);
1683                                 } else {
1684                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1685                                         error = EINVAL;
1686                                 }
1687                         }
1688                         if (error == 0) {
1689                                 *optsize = sizeof(struct sctp_assoc_value);
1690                         }
1691                         break;
1692                 }
1693         case SCTP_CMT_ON_OFF:
1694                 {
1695                         struct sctp_assoc_value *av;
1696
1697                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1698                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1699                         if (stcb) {
1700                                 av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1701                                 SCTP_TCB_UNLOCK(stcb);
1702                         } else {
1703                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1704                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1705                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1706                                         SCTP_INP_RLOCK(inp);
1707                                         av->assoc_value = inp->sctp_cmt_on_off;
1708                                         SCTP_INP_RUNLOCK(inp);
1709                                 } else {
1710                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1711                                         error = EINVAL;
1712                                 }
1713                         }
1714                         if (error == 0) {
1715                                 *optsize = sizeof(struct sctp_assoc_value);
1716                         }
1717                         break;
1718                 }
1719         case SCTP_PLUGGABLE_CC:
1720                 {
1721                         struct sctp_assoc_value *av;
1722
1723                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1724                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1725                         if (stcb) {
1726                                 av->assoc_value = stcb->asoc.congestion_control_module;
1727                                 SCTP_TCB_UNLOCK(stcb);
1728                         } else {
1729                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1730                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1731                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1732                                         SCTP_INP_RLOCK(inp);
1733                                         av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
1734                                         SCTP_INP_RUNLOCK(inp);
1735                                 } else {
1736                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1737                                         error = EINVAL;
1738                                 }
1739                         }
1740                         if (error == 0) {
1741                                 *optsize = sizeof(struct sctp_assoc_value);
1742                         }
1743                         break;
1744                 }
1745         case SCTP_CC_OPTION:
1746                 {
1747                         struct sctp_cc_option *cc_opt;
1748
1749                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
1750                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
1751                         if (stcb == NULL) {
1752                                 error = EINVAL;
1753                         } else {
1754                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
1755                                         error = ENOTSUP;
1756                                 } else {
1757                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 0, cc_opt);
1758                                         *optsize = sizeof(struct sctp_cc_option);
1759                                 }
1760                                 SCTP_TCB_UNLOCK(stcb);
1761                         }
1762                         break;
1763                 }
1764         case SCTP_PLUGGABLE_SS:
1765                 {
1766                         struct sctp_assoc_value *av;
1767
1768                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1769                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1770                         if (stcb) {
1771                                 av->assoc_value = stcb->asoc.stream_scheduling_module;
1772                                 SCTP_TCB_UNLOCK(stcb);
1773                         } else {
1774                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1775                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1776                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1777                                         SCTP_INP_RLOCK(inp);
1778                                         av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
1779                                         SCTP_INP_RUNLOCK(inp);
1780                                 } else {
1781                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1782                                         error = EINVAL;
1783                                 }
1784                         }
1785                         if (error == 0) {
1786                                 *optsize = sizeof(struct sctp_assoc_value);
1787                         }
1788                         break;
1789                 }
1790         case SCTP_SS_VALUE:
1791                 {
1792                         struct sctp_stream_value *av;
1793
1794                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
1795                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1796                         if (stcb) {
1797                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
1798                                     (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
1799                                     &av->stream_value) < 0)) {
1800                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1801                                         error = EINVAL;
1802                                 } else {
1803                                         *optsize = sizeof(struct sctp_stream_value);
1804                                 }
1805                                 SCTP_TCB_UNLOCK(stcb);
1806                         } else {
1807                                 /*
1808                                  * Can't get stream value without
1809                                  * association
1810                                  */
1811                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1812                                 error = EINVAL;
1813                         }
1814                         break;
1815                 }
1816         case SCTP_GET_ADDR_LEN:
1817                 {
1818                         struct sctp_assoc_value *av;
1819
1820                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1821                         error = EINVAL;
1822 #ifdef INET
1823                         if (av->assoc_value == AF_INET) {
1824                                 av->assoc_value = sizeof(struct sockaddr_in);
1825                                 error = 0;
1826                         }
1827 #endif
1828 #ifdef INET6
1829                         if (av->assoc_value == AF_INET6) {
1830                                 av->assoc_value = sizeof(struct sockaddr_in6);
1831                                 error = 0;
1832                         }
1833 #endif
1834                         if (error) {
1835                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1836                         } else {
1837                                 *optsize = sizeof(struct sctp_assoc_value);
1838                         }
1839                         break;
1840                 }
1841         case SCTP_GET_ASSOC_NUMBER:
1842                 {
1843                         uint32_t *value, cnt;
1844
1845                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1846                         SCTP_INP_RLOCK(inp);
1847                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1848                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1849                                 /* Can't do this for a 1-1 socket */
1850                                 error = EINVAL;
1851                                 SCTP_INP_RUNLOCK(inp);
1852                                 break;
1853                         }
1854                         cnt = 0;
1855                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1856                                 cnt++;
1857                         }
1858                         SCTP_INP_RUNLOCK(inp);
1859                         *value = cnt;
1860                         *optsize = sizeof(uint32_t);
1861                         break;
1862                 }
1863         case SCTP_GET_ASSOC_ID_LIST:
1864                 {
1865                         struct sctp_assoc_ids *ids;
1866                         uint32_t at;
1867                         size_t limit;
1868
1869                         SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1870                         SCTP_INP_RLOCK(inp);
1871                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1872                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1873                                 /* Can't do this for a 1-1 socket */
1874                                 error = EINVAL;
1875                                 SCTP_INP_RUNLOCK(inp);
1876                                 break;
1877                         }
1878                         at = 0;
1879                         limit = (*optsize - sizeof(uint32_t)) / sizeof(sctp_assoc_t);
1880                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1881                                 if (at < limit) {
1882                                         ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
1883                                         if (at == 0) {
1884                                                 error = EINVAL;
1885                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1886                                                 break;
1887                                         }
1888                                 } else {
1889                                         error = EINVAL;
1890                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1891                                         break;
1892                                 }
1893                         }
1894                         SCTP_INP_RUNLOCK(inp);
1895                         if (error == 0) {
1896                                 ids->gaids_number_of_ids = at;
1897                                 *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
1898                         }
1899                         break;
1900                 }
1901         case SCTP_CONTEXT:
1902                 {
1903                         struct sctp_assoc_value *av;
1904
1905                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1906                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1907
1908                         if (stcb) {
1909                                 av->assoc_value = stcb->asoc.context;
1910                                 SCTP_TCB_UNLOCK(stcb);
1911                         } else {
1912                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1913                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1914                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
1915                                         SCTP_INP_RLOCK(inp);
1916                                         av->assoc_value = inp->sctp_context;
1917                                         SCTP_INP_RUNLOCK(inp);
1918                                 } else {
1919                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1920                                         error = EINVAL;
1921                                 }
1922                         }
1923                         if (error == 0) {
1924                                 *optsize = sizeof(struct sctp_assoc_value);
1925                         }
1926                         break;
1927                 }
1928         case SCTP_VRF_ID:
1929                 {
1930                         uint32_t *default_vrfid;
1931
1932                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
1933                         *default_vrfid = inp->def_vrf_id;
1934                         *optsize = sizeof(uint32_t);
1935                         break;
1936                 }
1937         case SCTP_GET_ASOC_VRF:
1938                 {
1939                         struct sctp_assoc_value *id;
1940
1941                         SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1942                         SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1943                         if (stcb == NULL) {
1944                                 error = EINVAL;
1945                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
1946                         } else {
1947                                 id->assoc_value = stcb->asoc.vrf_id;
1948                                 SCTP_TCB_UNLOCK(stcb);
1949                                 *optsize = sizeof(struct sctp_assoc_value);
1950                         }
1951                         break;
1952                 }
1953         case SCTP_GET_VRF_IDS:
1954                 {
1955                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1956                         error = EOPNOTSUPP;
1957                         break;
1958                 }
1959         case SCTP_GET_NONCE_VALUES:
1960                 {
1961                         struct sctp_get_nonce_values *gnv;
1962
1963                         SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1964                         SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1965
1966                         if (stcb) {
1967                                 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1968                                 gnv->gn_local_tag = stcb->asoc.my_vtag;
1969                                 SCTP_TCB_UNLOCK(stcb);
1970                                 *optsize = sizeof(struct sctp_get_nonce_values);
1971                         } else {
1972                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1973                                 error = ENOTCONN;
1974                         }
1975                         break;
1976                 }
1977         case SCTP_DELAYED_SACK:
1978                 {
1979                         struct sctp_sack_info *sack;
1980
1981                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
1982                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
1983                         if (stcb) {
1984                                 sack->sack_delay = stcb->asoc.delayed_ack;
1985                                 sack->sack_freq = stcb->asoc.sack_freq;
1986                                 SCTP_TCB_UNLOCK(stcb);
1987                         } else {
1988                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1989                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
1990                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
1991                                         SCTP_INP_RLOCK(inp);
1992                                         sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1993                                         sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
1994                                         SCTP_INP_RUNLOCK(inp);
1995                                 } else {
1996                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1997                                         error = EINVAL;
1998                                 }
1999                         }
2000                         if (error == 0) {
2001                                 *optsize = sizeof(struct sctp_sack_info);
2002                         }
2003                         break;
2004                 }
2005         case SCTP_GET_SNDBUF_USE:
2006                 {
2007                         struct sctp_sockstat *ss;
2008
2009                         SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2010                         SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2011
2012                         if (stcb) {
2013                                 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2014                                 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2015                                     stcb->asoc.size_on_all_streams);
2016                                 SCTP_TCB_UNLOCK(stcb);
2017                                 *optsize = sizeof(struct sctp_sockstat);
2018                         } else {
2019                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2020                                 error = ENOTCONN;
2021                         }
2022                         break;
2023                 }
2024         case SCTP_MAX_BURST:
2025                 {
2026                         struct sctp_assoc_value *av;
2027
2028                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2029                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2030
2031                         if (stcb) {
2032                                 av->assoc_value = stcb->asoc.max_burst;
2033                                 SCTP_TCB_UNLOCK(stcb);
2034                         } else {
2035                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2036                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2037                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2038                                         SCTP_INP_RLOCK(inp);
2039                                         av->assoc_value = inp->sctp_ep.max_burst;
2040                                         SCTP_INP_RUNLOCK(inp);
2041                                 } else {
2042                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2043                                         error = EINVAL;
2044                                 }
2045                         }
2046                         if (error == 0) {
2047                                 *optsize = sizeof(struct sctp_assoc_value);
2048                         }
2049                         break;
2050                 }
2051         case SCTP_MAXSEG:
2052                 {
2053                         struct sctp_assoc_value *av;
2054                         int ovh;
2055
2056                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2057                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2058
2059                         if (stcb) {
2060                                 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2061                                 SCTP_TCB_UNLOCK(stcb);
2062                         } else {
2063                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2064                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2065                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2066                                         SCTP_INP_RLOCK(inp);
2067                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2068                                                 ovh = SCTP_MED_OVERHEAD;
2069                                         } else {
2070                                                 ovh = SCTP_MED_V4_OVERHEAD;
2071                                         }
2072                                         if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2073                                                 av->assoc_value = 0;
2074                                         else
2075                                                 av->assoc_value = inp->sctp_frag_point - ovh;
2076                                         SCTP_INP_RUNLOCK(inp);
2077                                 } else {
2078                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2079                                         error = EINVAL;
2080                                 }
2081                         }
2082                         if (error == 0) {
2083                                 *optsize = sizeof(struct sctp_assoc_value);
2084                         }
2085                         break;
2086                 }
2087         case SCTP_GET_STAT_LOG:
2088                 error = sctp_fill_stat_log(optval, optsize);
2089                 break;
2090         case SCTP_EVENTS:
2091                 {
2092                         struct sctp_event_subscribe *events;
2093
2094                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2095                         memset(events, 0, sizeof(struct sctp_event_subscribe));
2096                         SCTP_INP_RLOCK(inp);
2097                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2098                                 events->sctp_data_io_event = 1;
2099
2100                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2101                                 events->sctp_association_event = 1;
2102
2103                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2104                                 events->sctp_address_event = 1;
2105
2106                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2107                                 events->sctp_send_failure_event = 1;
2108
2109                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2110                                 events->sctp_peer_error_event = 1;
2111
2112                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2113                                 events->sctp_shutdown_event = 1;
2114
2115                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2116                                 events->sctp_partial_delivery_event = 1;
2117
2118                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2119                                 events->sctp_adaptation_layer_event = 1;
2120
2121                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2122                                 events->sctp_authentication_event = 1;
2123
2124                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2125                                 events->sctp_sender_dry_event = 1;
2126
2127                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2128                                 events->sctp_stream_reset_event = 1;
2129                         SCTP_INP_RUNLOCK(inp);
2130                         *optsize = sizeof(struct sctp_event_subscribe);
2131                         break;
2132                 }
2133         case SCTP_ADAPTATION_LAYER:
2134                 {
2135                         uint32_t *value;
2136
2137                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2138
2139                         SCTP_INP_RLOCK(inp);
2140                         *value = inp->sctp_ep.adaptation_layer_indicator;
2141                         SCTP_INP_RUNLOCK(inp);
2142                         *optsize = sizeof(uint32_t);
2143                         break;
2144                 }
2145         case SCTP_SET_INITIAL_DBG_SEQ:
2146                 {
2147                         uint32_t *value;
2148
2149                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2150                         SCTP_INP_RLOCK(inp);
2151                         *value = inp->sctp_ep.initial_sequence_debug;
2152                         SCTP_INP_RUNLOCK(inp);
2153                         *optsize = sizeof(uint32_t);
2154                         break;
2155                 }
2156         case SCTP_GET_LOCAL_ADDR_SIZE:
2157                 {
2158                         uint32_t *value;
2159
2160                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2161                         SCTP_INP_RLOCK(inp);
2162                         *value = sctp_count_max_addresses(inp);
2163                         SCTP_INP_RUNLOCK(inp);
2164                         *optsize = sizeof(uint32_t);
2165                         break;
2166                 }
2167         case SCTP_GET_REMOTE_ADDR_SIZE:
2168                 {
2169                         uint32_t *value;
2170                         size_t size;
2171                         struct sctp_nets *net;
2172
2173                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2174                         /* FIXME MT: change to sctp_assoc_value? */
2175                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t)*value);
2176
2177                         if (stcb) {
2178                                 size = 0;
2179                                 /* Count the sizes */
2180                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2181                                         switch (net->ro._l_addr.sa.sa_family) {
2182 #ifdef INET
2183                                         case AF_INET:
2184 #ifdef INET6
2185                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2186                                                         size += sizeof(struct sockaddr_in6);
2187                                                 } else {
2188                                                         size += sizeof(struct sockaddr_in);
2189                                                 }
2190 #else
2191                                                 size += sizeof(struct sockaddr_in);
2192 #endif
2193                                                 break;
2194 #endif
2195 #ifdef INET6
2196                                         case AF_INET6:
2197                                                 size += sizeof(struct sockaddr_in6);
2198                                                 break;
2199 #endif
2200                                         default:
2201                                                 break;
2202                                         }
2203                                 }
2204                                 SCTP_TCB_UNLOCK(stcb);
2205                                 *value = (uint32_t)size;
2206                                 *optsize = sizeof(uint32_t);
2207                         } else {
2208                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2209                                 error = ENOTCONN;
2210                         }
2211                         break;
2212                 }
2213         case SCTP_GET_PEER_ADDRESSES:
2214                 /*
2215                  * Get the address information, an array is passed in to
2216                  * fill up we pack it.
2217                  */
2218                 {
2219                         size_t cpsz, left;
2220                         struct sockaddr_storage *sas;
2221                         struct sctp_nets *net;
2222                         struct sctp_getaddresses *saddr;
2223
2224                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2225                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2226
2227                         if (stcb) {
2228                                 left = (*optsize) - sizeof(sctp_assoc_t);
2229                                 *optsize = sizeof(sctp_assoc_t);
2230                                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2231
2232                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2233                                         switch (net->ro._l_addr.sa.sa_family) {
2234 #ifdef INET
2235                                         case AF_INET:
2236 #ifdef INET6
2237                                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2238                                                         cpsz = sizeof(struct sockaddr_in6);
2239                                                 } else {
2240                                                         cpsz = sizeof(struct sockaddr_in);
2241                                                 }
2242 #else
2243                                                 cpsz = sizeof(struct sockaddr_in);
2244 #endif
2245                                                 break;
2246 #endif
2247 #ifdef INET6
2248                                         case AF_INET6:
2249                                                 cpsz = sizeof(struct sockaddr_in6);
2250                                                 break;
2251 #endif
2252                                         default:
2253                                                 cpsz = 0;
2254                                                 break;
2255                                         }
2256                                         if (cpsz == 0) {
2257                                                 break;
2258                                         }
2259                                         if (left < cpsz) {
2260                                                 /* not enough room. */
2261                                                 break;
2262                                         }
2263 #if defined(INET) && defined(INET6)
2264                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2265                                             (net->ro._l_addr.sa.sa_family == AF_INET)) {
2266                                                 /* Must map the address */
2267                                                 in6_sin_2_v4mapsin6(&net->ro._l_addr.sin,
2268                                                     (struct sockaddr_in6 *)sas);
2269                                         } else {
2270                                                 memcpy(sas, &net->ro._l_addr, cpsz);
2271                                         }
2272 #else
2273                                         memcpy(sas, &net->ro._l_addr, cpsz);
2274 #endif
2275                                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2276
2277                                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2278                                         left -= cpsz;
2279                                         *optsize += cpsz;
2280                                 }
2281                                 SCTP_TCB_UNLOCK(stcb);
2282                         } else {
2283                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2284                                 error = ENOENT;
2285                         }
2286                         break;
2287                 }
2288         case SCTP_GET_LOCAL_ADDRESSES:
2289                 {
2290                         size_t limit, actual;
2291                         struct sockaddr_storage *sas;
2292                         struct sctp_getaddresses *saddr;
2293
2294                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2295                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2296
2297                         sas = (struct sockaddr_storage *)&saddr->addr[0];
2298                         limit = *optsize - sizeof(sctp_assoc_t);
2299                         actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2300                         if (stcb) {
2301                                 SCTP_TCB_UNLOCK(stcb);
2302                         }
2303                         *optsize = sizeof(sctp_assoc_t) + actual;
2304                         break;
2305                 }
2306         case SCTP_PEER_ADDR_PARAMS:
2307                 {
2308                         struct sctp_paddrparams *paddrp;
2309                         struct sctp_nets *net;
2310                         struct sockaddr *addr;
2311 #if defined(INET) && defined(INET6)
2312                         struct sockaddr_in sin_store;
2313 #endif
2314
2315                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2316                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2317
2318 #if defined(INET) && defined(INET6)
2319                         if (paddrp->spp_address.ss_family == AF_INET6) {
2320                                 struct sockaddr_in6 *sin6;
2321
2322                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
2323                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2324                                         in6_sin6_2_sin(&sin_store, sin6);
2325                                         addr = (struct sockaddr *)&sin_store;
2326                                 } else {
2327                                         addr = (struct sockaddr *)&paddrp->spp_address;
2328                                 }
2329                         } else {
2330                                 addr = (struct sockaddr *)&paddrp->spp_address;
2331                         }
2332 #else
2333                         addr = (struct sockaddr *)&paddrp->spp_address;
2334 #endif
2335                         if (stcb != NULL) {
2336                                 net = sctp_findnet(stcb, addr);
2337                         } else {
2338                                 /*
2339                                  * We increment here since
2340                                  * sctp_findassociation_ep_addr() wil do a
2341                                  * decrement if it finds the stcb as long as
2342                                  * the locked tcb (last argument) is NOT a
2343                                  * TCB.. aka NULL.
2344                                  */
2345                                 net = NULL;
2346                                 SCTP_INP_INCR_REF(inp);
2347                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2348                                 if (stcb == NULL) {
2349                                         SCTP_INP_DECR_REF(inp);
2350                                 }
2351                         }
2352                         if ((stcb != NULL) && (net == NULL)) {
2353 #ifdef INET
2354                                 if (addr->sa_family == AF_INET) {
2355                                         struct sockaddr_in *sin;
2356
2357                                         sin = (struct sockaddr_in *)addr;
2358                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
2359                                                 error = EINVAL;
2360                                                 SCTP_TCB_UNLOCK(stcb);
2361                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2362                                                 break;
2363                                         }
2364                                 } else
2365 #endif
2366 #ifdef INET6
2367                                 if (addr->sa_family == AF_INET6) {
2368                                         struct sockaddr_in6 *sin6;
2369
2370                                         sin6 = (struct sockaddr_in6 *)addr;
2371                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2372                                                 error = EINVAL;
2373                                                 SCTP_TCB_UNLOCK(stcb);
2374                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2375                                                 break;
2376                                         }
2377                                 } else
2378 #endif
2379                                 {
2380                                         error = EAFNOSUPPORT;
2381                                         SCTP_TCB_UNLOCK(stcb);
2382                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2383                                         break;
2384                                 }
2385                         }
2386
2387                         if (stcb != NULL) {
2388                                 /* Applies to the specific association */
2389                                 paddrp->spp_flags = 0;
2390                                 if (net != NULL) {
2391                                         paddrp->spp_hbinterval = net->heart_beat_delay;
2392                                         paddrp->spp_pathmaxrxt = net->failure_threshold;
2393                                         paddrp->spp_pathmtu = net->mtu;
2394                                         switch (net->ro._l_addr.sa.sa_family) {
2395 #ifdef INET
2396                                         case AF_INET:
2397                                                 paddrp->spp_pathmtu -= SCTP_MIN_V4_OVERHEAD;
2398                                                 break;
2399 #endif
2400 #ifdef INET6
2401                                         case AF_INET6:
2402                                                 paddrp->spp_pathmtu -= SCTP_MIN_OVERHEAD;
2403                                                 break;
2404 #endif
2405                                         default:
2406                                                 break;
2407                                         }
2408                                         /* get flags for HB */
2409                                         if (net->dest_state & SCTP_ADDR_NOHB) {
2410                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2411                                         } else {
2412                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2413                                         }
2414                                         /* get flags for PMTU */
2415                                         if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2416                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2417                                         } else {
2418                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2419                                         }
2420                                         if (net->dscp & 0x01) {
2421                                                 paddrp->spp_dscp = net->dscp & 0xfc;
2422                                                 paddrp->spp_flags |= SPP_DSCP;
2423                                         }
2424 #ifdef INET6
2425                                         if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2426                                             (net->flowlabel & 0x80000000)) {
2427                                                 paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2428                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2429                                         }
2430 #endif
2431                                 } else {
2432                                         /*
2433                                          * No destination so return default
2434                                          * value
2435                                          */
2436                                         paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2437                                         paddrp->spp_pathmtu = stcb->asoc.default_mtu;
2438                                         if (stcb->asoc.default_dscp & 0x01) {
2439                                                 paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2440                                                 paddrp->spp_flags |= SPP_DSCP;
2441                                         }
2442 #ifdef INET6
2443                                         if (stcb->asoc.default_flowlabel & 0x80000000) {
2444                                                 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2445                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2446                                         }
2447 #endif
2448                                         /* default settings should be these */
2449                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2450                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2451                                         } else {
2452                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2453                                         }
2454                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2455                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2456                                         } else {
2457                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2458                                         }
2459                                         paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2460                                 }
2461                                 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2462                                 SCTP_TCB_UNLOCK(stcb);
2463                         } else {
2464                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2465                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2466                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2467                                         /* Use endpoint defaults */
2468                                         SCTP_INP_RLOCK(inp);
2469                                         paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2470                                         paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2471                                         paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2472                                         /* get inp's default */
2473                                         if (inp->sctp_ep.default_dscp & 0x01) {
2474                                                 paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2475                                                 paddrp->spp_flags |= SPP_DSCP;
2476                                         }
2477 #ifdef INET6
2478                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2479                                             (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2480                                                 paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2481                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2482                                         }
2483 #endif
2484                                         paddrp->spp_pathmtu = inp->sctp_ep.default_mtu;
2485
2486                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2487                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2488                                         } else {
2489                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2490                                         }
2491                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2492                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2493                                         } else {
2494                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2495                                         }
2496                                         SCTP_INP_RUNLOCK(inp);
2497                                 } else {
2498                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2499                                         error = EINVAL;
2500                                 }
2501                         }
2502                         if (error == 0) {
2503                                 *optsize = sizeof(struct sctp_paddrparams);
2504                         }
2505                         break;
2506                 }
2507         case SCTP_GET_PEER_ADDR_INFO:
2508                 {
2509                         struct sctp_paddrinfo *paddri;
2510                         struct sctp_nets *net;
2511                         struct sockaddr *addr;
2512 #if defined(INET) && defined(INET6)
2513                         struct sockaddr_in sin_store;
2514 #endif
2515
2516                         SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2517                         SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2518
2519 #if defined(INET) && defined(INET6)
2520                         if (paddri->spinfo_address.ss_family == AF_INET6) {
2521                                 struct sockaddr_in6 *sin6;
2522
2523                                 sin6 = (struct sockaddr_in6 *)&paddri->spinfo_address;
2524                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
2525                                         in6_sin6_2_sin(&sin_store, sin6);
2526                                         addr = (struct sockaddr *)&sin_store;
2527                                 } else {
2528                                         addr = (struct sockaddr *)&paddri->spinfo_address;
2529                                 }
2530                         } else {
2531                                 addr = (struct sockaddr *)&paddri->spinfo_address;
2532                         }
2533 #else
2534                         addr = (struct sockaddr *)&paddri->spinfo_address;
2535 #endif
2536                         if (stcb != NULL) {
2537                                 net = sctp_findnet(stcb, addr);
2538                         } else {
2539                                 /*
2540                                  * We increment here since
2541                                  * sctp_findassociation_ep_addr() wil do a
2542                                  * decrement if it finds the stcb as long as
2543                                  * the locked tcb (last argument) is NOT a
2544                                  * TCB.. aka NULL.
2545                                  */
2546                                 net = NULL;
2547                                 SCTP_INP_INCR_REF(inp);
2548                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
2549                                 if (stcb == NULL) {
2550                                         SCTP_INP_DECR_REF(inp);
2551                                 }
2552                         }
2553
2554                         if ((stcb != NULL) && (net != NULL)) {
2555                                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2556                                         /* It's unconfirmed */
2557                                         paddri->spinfo_state = SCTP_UNCONFIRMED;
2558                                 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2559                                         /* It's active */
2560                                         paddri->spinfo_state = SCTP_ACTIVE;
2561                                 } else {
2562                                         /* It's inactive */
2563                                         paddri->spinfo_state = SCTP_INACTIVE;
2564                                 }
2565                                 paddri->spinfo_cwnd = net->cwnd;
2566                                 paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2567                                 paddri->spinfo_rto = net->RTO;
2568                                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2569                                 paddri->spinfo_mtu = net->mtu;
2570                                 switch (addr->sa_family) {
2571 #if defined(INET)
2572                                 case AF_INET:
2573                                         paddri->spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2574                                         break;
2575 #endif
2576 #if defined(INET6)
2577                                 case AF_INET6:
2578                                         paddri->spinfo_mtu -= SCTP_MIN_OVERHEAD;
2579                                         break;
2580 #endif
2581                                 default:
2582                                         break;
2583                                 }
2584                                 SCTP_TCB_UNLOCK(stcb);
2585                                 *optsize = sizeof(struct sctp_paddrinfo);
2586                         } else {
2587                                 if (stcb != NULL) {
2588                                         SCTP_TCB_UNLOCK(stcb);
2589                                 }
2590                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2591                                 error = ENOENT;
2592                         }
2593                         break;
2594                 }
2595         case SCTP_PCB_STATUS:
2596                 {
2597                         struct sctp_pcbinfo *spcb;
2598
2599                         SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2600                         sctp_fill_pcbinfo(spcb);
2601                         *optsize = sizeof(struct sctp_pcbinfo);
2602                         break;
2603                 }
2604         case SCTP_STATUS:
2605                 {
2606                         struct sctp_nets *net;
2607                         struct sctp_status *sstat;
2608
2609                         SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2610                         SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2611
2612                         if (stcb == NULL) {
2613                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2614                                 error = EINVAL;
2615                                 break;
2616                         }
2617                         sstat->sstat_state = sctp_map_assoc_state(stcb->asoc.state);
2618                         sstat->sstat_assoc_id = sctp_get_associd(stcb);
2619                         sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2620                         sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2621                         /*
2622                          * We can't include chunks that have been passed to
2623                          * the socket layer. Only things in queue.
2624                          */
2625                         sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2626                             stcb->asoc.cnt_on_all_streams);
2627
2628
2629                         sstat->sstat_instrms = stcb->asoc.streamincnt;
2630                         sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2631                         sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2632                         net = stcb->asoc.primary_destination;
2633                         if (net != NULL) {
2634                                 memcpy(&sstat->sstat_primary.spinfo_address,
2635                                     &stcb->asoc.primary_destination->ro._l_addr,
2636                                     ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2637                                 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2638                                 /*
2639                                  * Again the user can get info from
2640                                  * sctp_constants.h for what the state of
2641                                  * the network is.
2642                                  */
2643                                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2644                                         /* It's unconfirmed */
2645                                         sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2646                                 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2647                                         /* It's active */
2648                                         sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2649                                 } else {
2650                                         /* It's inactive */
2651                                         sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2652                                 }
2653                                 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2654                                 sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2655                                 sstat->sstat_primary.spinfo_rto = net->RTO;
2656                                 sstat->sstat_primary.spinfo_mtu = net->mtu;
2657                                 switch (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family) {
2658 #if defined(INET)
2659                                 case AF_INET:
2660                                         sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_V4_OVERHEAD;
2661                                         break;
2662 #endif
2663 #if defined(INET6)
2664                                 case AF_INET6:
2665                                         sstat->sstat_primary.spinfo_mtu -= SCTP_MIN_OVERHEAD;
2666                                         break;
2667 #endif
2668                                 default:
2669                                         break;
2670                                 }
2671                         } else {
2672                                 memset(&sstat->sstat_primary, 0, sizeof(struct sctp_paddrinfo));
2673                         }
2674                         sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2675                         SCTP_TCB_UNLOCK(stcb);
2676                         *optsize = sizeof(struct sctp_status);
2677                         break;
2678                 }
2679         case SCTP_RTOINFO:
2680                 {
2681                         struct sctp_rtoinfo *srto;
2682
2683                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2684                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2685
2686                         if (stcb) {
2687                                 srto->srto_initial = stcb->asoc.initial_rto;
2688                                 srto->srto_max = stcb->asoc.maxrto;
2689                                 srto->srto_min = stcb->asoc.minrto;
2690                                 SCTP_TCB_UNLOCK(stcb);
2691                         } else {
2692                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2693                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2694                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
2695                                         SCTP_INP_RLOCK(inp);
2696                                         srto->srto_initial = inp->sctp_ep.initial_rto;
2697                                         srto->srto_max = inp->sctp_ep.sctp_maxrto;
2698                                         srto->srto_min = inp->sctp_ep.sctp_minrto;
2699                                         SCTP_INP_RUNLOCK(inp);
2700                                 } else {
2701                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2702                                         error = EINVAL;
2703                                 }
2704                         }
2705                         if (error == 0) {
2706                                 *optsize = sizeof(struct sctp_rtoinfo);
2707                         }
2708                         break;
2709                 }
2710         case SCTP_TIMEOUTS:
2711                 {
2712                         struct sctp_timeouts *stimo;
2713
2714                         SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2715                         SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2716
2717                         if (stcb) {
2718                                 stimo->stimo_init = stcb->asoc.timoinit;
2719                                 stimo->stimo_data = stcb->asoc.timodata;
2720                                 stimo->stimo_sack = stcb->asoc.timosack;
2721                                 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2722                                 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2723                                 stimo->stimo_cookie = stcb->asoc.timocookie;
2724                                 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2725                                 SCTP_TCB_UNLOCK(stcb);
2726                                 *optsize = sizeof(struct sctp_timeouts);
2727                         } else {
2728                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2729                                 error = EINVAL;
2730                         }
2731                         break;
2732                 }
2733         case SCTP_ASSOCINFO:
2734                 {
2735                         struct sctp_assocparams *sasoc;
2736
2737                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2738                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2739
2740                         if (stcb) {
2741                                 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2742                                 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2743                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2744                                 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2745                                 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2746                                 SCTP_TCB_UNLOCK(stcb);
2747                         } else {
2748                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2749                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2750                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
2751                                         SCTP_INP_RLOCK(inp);
2752                                         sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2753                                         sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2754                                         sasoc->sasoc_number_peer_destinations = 0;
2755                                         sasoc->sasoc_peer_rwnd = 0;
2756                                         sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2757                                         SCTP_INP_RUNLOCK(inp);
2758                                 } else {
2759                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2760                                         error = EINVAL;
2761                                 }
2762                         }
2763                         if (error == 0) {
2764                                 *optsize = sizeof(struct sctp_assocparams);
2765                         }
2766                         break;
2767                 }
2768         case SCTP_DEFAULT_SEND_PARAM:
2769                 {
2770                         struct sctp_sndrcvinfo *s_info;
2771
2772                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2773                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2774
2775                         if (stcb) {
2776                                 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2777                                 SCTP_TCB_UNLOCK(stcb);
2778                         } else {
2779                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2780                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2781                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
2782                                         SCTP_INP_RLOCK(inp);
2783                                         memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2784                                         SCTP_INP_RUNLOCK(inp);
2785                                 } else {
2786                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2787                                         error = EINVAL;
2788                                 }
2789                         }
2790                         if (error == 0) {
2791                                 *optsize = sizeof(struct sctp_sndrcvinfo);
2792                         }
2793                         break;
2794                 }
2795         case SCTP_INITMSG:
2796                 {
2797                         struct sctp_initmsg *sinit;
2798
2799                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2800                         SCTP_INP_RLOCK(inp);
2801                         sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2802                         sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2803                         sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2804                         sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2805                         SCTP_INP_RUNLOCK(inp);
2806                         *optsize = sizeof(struct sctp_initmsg);
2807                         break;
2808                 }
2809         case SCTP_PRIMARY_ADDR:
2810                 /* we allow a "get" operation on this */
2811                 {
2812                         struct sctp_setprim *ssp;
2813
2814                         SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2815                         SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2816
2817                         if (stcb) {
2818                                 union sctp_sockstore *addr;
2819
2820                                 addr = &stcb->asoc.primary_destination->ro._l_addr;
2821                                 switch (addr->sa.sa_family) {
2822 #ifdef INET
2823                                 case AF_INET:
2824 #ifdef INET6
2825                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2826                                                 in6_sin_2_v4mapsin6(&addr->sin,
2827                                                     (struct sockaddr_in6 *)&ssp->ssp_addr);
2828                                         } else {
2829                                                 memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2830                                         }
2831 #else
2832                                         memcpy(&ssp->ssp_addr, &addr->sin, sizeof(struct sockaddr_in));
2833 #endif
2834                                         break;
2835 #endif
2836 #ifdef INET6
2837                                 case AF_INET6:
2838                                         memcpy(&ssp->ssp_addr, &addr->sin6, sizeof(struct sockaddr_in6));
2839                                         break;
2840 #endif
2841                                 default:
2842                                         break;
2843                                 }
2844                                 SCTP_TCB_UNLOCK(stcb);
2845                                 *optsize = sizeof(struct sctp_setprim);
2846                         } else {
2847                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2848                                 error = EINVAL;
2849                         }
2850                         break;
2851                 }
2852         case SCTP_HMAC_IDENT:
2853                 {
2854                         struct sctp_hmacalgo *shmac;
2855                         sctp_hmaclist_t *hmaclist;
2856                         uint32_t size;
2857                         int i;
2858
2859                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2860
2861                         SCTP_INP_RLOCK(inp);
2862                         hmaclist = inp->sctp_ep.local_hmacs;
2863                         if (hmaclist == NULL) {
2864                                 /* no HMACs to return */
2865                                 *optsize = sizeof(*shmac);
2866                                 SCTP_INP_RUNLOCK(inp);
2867                                 break;
2868                         }
2869                         /* is there room for all of the hmac ids? */
2870                         size = sizeof(*shmac) + (hmaclist->num_algo *
2871                             sizeof(shmac->shmac_idents[0]));
2872                         if ((size_t)(*optsize) < size) {
2873                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2874                                 error = EINVAL;
2875                                 SCTP_INP_RUNLOCK(inp);
2876                                 break;
2877                         }
2878                         /* copy in the list */
2879                         shmac->shmac_number_of_idents = hmaclist->num_algo;
2880                         for (i = 0; i < hmaclist->num_algo; i++) {
2881                                 shmac->shmac_idents[i] = hmaclist->hmac[i];
2882                         }
2883                         SCTP_INP_RUNLOCK(inp);
2884                         *optsize = size;
2885                         break;
2886                 }
2887         case SCTP_AUTH_ACTIVE_KEY:
2888                 {
2889                         struct sctp_authkeyid *scact;
2890
2891                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2892                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2893
2894                         if (stcb) {
2895                                 /* get the active key on the assoc */
2896                                 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2897                                 SCTP_TCB_UNLOCK(stcb);
2898                         } else {
2899                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2900                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2901                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
2902                                         /* get the endpoint active key */
2903                                         SCTP_INP_RLOCK(inp);
2904                                         scact->scact_keynumber = inp->sctp_ep.default_keyid;
2905                                         SCTP_INP_RUNLOCK(inp);
2906                                 } else {
2907                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2908                                         error = EINVAL;
2909                                 }
2910                         }
2911                         if (error == 0) {
2912                                 *optsize = sizeof(struct sctp_authkeyid);
2913                         }
2914                         break;
2915                 }
2916         case SCTP_LOCAL_AUTH_CHUNKS:
2917                 {
2918                         struct sctp_authchunks *sac;
2919                         sctp_auth_chklist_t *chklist = NULL;
2920                         size_t size = 0;
2921
2922                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2923                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2924
2925                         if (stcb) {
2926                                 /* get off the assoc */
2927                                 chklist = stcb->asoc.local_auth_chunks;
2928                                 /* is there enough space? */
2929                                 size = sctp_auth_get_chklist_size(chklist);
2930                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2931                                         error = EINVAL;
2932                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2933                                 } else {
2934                                         /* copy in the chunks */
2935                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2936                                         sac->gauth_number_of_chunks = (uint32_t)size;
2937                                         *optsize = sizeof(struct sctp_authchunks) + size;
2938                                 }
2939                                 SCTP_TCB_UNLOCK(stcb);
2940                         } else {
2941                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2942                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2943                                     (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
2944                                         /* get off the endpoint */
2945                                         SCTP_INP_RLOCK(inp);
2946                                         chklist = inp->sctp_ep.local_auth_chunks;
2947                                         /* is there enough space? */
2948                                         size = sctp_auth_get_chklist_size(chklist);
2949                                         if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2950                                                 error = EINVAL;
2951                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2952                                         } else {
2953                                                 /* copy in the chunks */
2954                                                 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2955                                                 sac->gauth_number_of_chunks = (uint32_t)size;
2956                                                 *optsize = sizeof(struct sctp_authchunks) + size;
2957                                         }
2958                                         SCTP_INP_RUNLOCK(inp);
2959                                 } else {
2960                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2961                                         error = EINVAL;
2962                                 }
2963                         }
2964                         break;
2965                 }
2966         case SCTP_PEER_AUTH_CHUNKS:
2967                 {
2968                         struct sctp_authchunks *sac;
2969                         sctp_auth_chklist_t *chklist = NULL;
2970                         size_t size = 0;
2971
2972                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2973                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2974
2975                         if (stcb) {
2976                                 /* get off the assoc */
2977                                 chklist = stcb->asoc.peer_auth_chunks;
2978                                 /* is there enough space? */
2979                                 size = sctp_auth_get_chklist_size(chklist);
2980                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2981                                         error = EINVAL;
2982                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2983                                 } else {
2984                                         /* copy in the chunks */
2985                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2986                                         sac->gauth_number_of_chunks = (uint32_t)size;
2987                                         *optsize = sizeof(struct sctp_authchunks) + size;
2988                                 }
2989                                 SCTP_TCB_UNLOCK(stcb);
2990                         } else {
2991                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2992                                 error = ENOENT;
2993                         }
2994                         break;
2995                 }
2996         case SCTP_EVENT:
2997                 {
2998                         struct sctp_event *event;
2999                         uint32_t event_type;
3000
3001                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
3002                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
3003
3004                         switch (event->se_type) {
3005                         case SCTP_ASSOC_CHANGE:
3006                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
3007                                 break;
3008                         case SCTP_PEER_ADDR_CHANGE:
3009                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
3010                                 break;
3011                         case SCTP_REMOTE_ERROR:
3012                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
3013                                 break;
3014                         case SCTP_SEND_FAILED:
3015                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3016                                 break;
3017                         case SCTP_SHUTDOWN_EVENT:
3018                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3019                                 break;
3020                         case SCTP_ADAPTATION_INDICATION:
3021                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
3022                                 break;
3023                         case SCTP_PARTIAL_DELIVERY_EVENT:
3024                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
3025                                 break;
3026                         case SCTP_AUTHENTICATION_EVENT:
3027                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
3028                                 break;
3029                         case SCTP_STREAM_RESET_EVENT:
3030                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3031                                 break;
3032                         case SCTP_SENDER_DRY_EVENT:
3033                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
3034                                 break;
3035                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
3036                                 event_type = 0;
3037                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
3038                                 error = ENOTSUP;
3039                                 break;
3040                         case SCTP_ASSOC_RESET_EVENT:
3041                                 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
3042                                 break;
3043                         case SCTP_STREAM_CHANGE_EVENT:
3044                                 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
3045                                 break;
3046                         case SCTP_SEND_FAILED_EVENT:
3047                                 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
3048                                 break;
3049                         default:
3050                                 event_type = 0;
3051                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3052                                 error = EINVAL;
3053                                 break;
3054                         }
3055                         if (event_type > 0) {
3056                                 if (stcb) {
3057                                         event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
3058                                 } else {
3059                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3060                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3061                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3062                                                 SCTP_INP_RLOCK(inp);
3063                                                 event->se_on = sctp_is_feature_on(inp, event_type);
3064                                                 SCTP_INP_RUNLOCK(inp);
3065                                         } else {
3066                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3067                                                 error = EINVAL;
3068                                         }
3069                                 }
3070                         }
3071                         if (stcb != NULL) {
3072                                 SCTP_TCB_UNLOCK(stcb);
3073                         }
3074                         if (error == 0) {
3075                                 *optsize = sizeof(struct sctp_event);
3076                         }
3077                         break;
3078                 }
3079         case SCTP_RECVRCVINFO:
3080                 {
3081                         int onoff;
3082
3083                         if (*optsize < sizeof(int)) {
3084                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3085                                 error = EINVAL;
3086                         } else {
3087                                 SCTP_INP_RLOCK(inp);
3088                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3089                                 SCTP_INP_RUNLOCK(inp);
3090                         }
3091                         if (error == 0) {
3092                                 /* return the option value */
3093                                 *(int *)optval = onoff;
3094                                 *optsize = sizeof(int);
3095                         }
3096                         break;
3097                 }
3098         case SCTP_RECVNXTINFO:
3099                 {
3100                         int onoff;
3101
3102                         if (*optsize < sizeof(int)) {
3103                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3104                                 error = EINVAL;
3105                         } else {
3106                                 SCTP_INP_RLOCK(inp);
3107                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3108                                 SCTP_INP_RUNLOCK(inp);
3109                         }
3110                         if (error == 0) {
3111                                 /* return the option value */
3112                                 *(int *)optval = onoff;
3113                                 *optsize = sizeof(int);
3114                         }
3115                         break;
3116                 }
3117         case SCTP_DEFAULT_SNDINFO:
3118                 {
3119                         struct sctp_sndinfo *info;
3120
3121                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3122                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3123
3124                         if (stcb) {
3125                                 info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3126                                 info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3127                                 info->snd_flags &= 0xfff0;
3128                                 info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3129                                 info->snd_context = stcb->asoc.def_send.sinfo_context;
3130                                 SCTP_TCB_UNLOCK(stcb);
3131                         } else {
3132                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3133                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3134                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3135                                         SCTP_INP_RLOCK(inp);
3136                                         info->snd_sid = inp->def_send.sinfo_stream;
3137                                         info->snd_flags = inp->def_send.sinfo_flags;
3138                                         info->snd_flags &= 0xfff0;
3139                                         info->snd_ppid = inp->def_send.sinfo_ppid;
3140                                         info->snd_context = inp->def_send.sinfo_context;
3141                                         SCTP_INP_RUNLOCK(inp);
3142                                 } else {
3143                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3144                                         error = EINVAL;
3145                                 }
3146                         }
3147                         if (error == 0) {
3148                                 *optsize = sizeof(struct sctp_sndinfo);
3149                         }
3150                         break;
3151                 }
3152         case SCTP_DEFAULT_PRINFO:
3153                 {
3154                         struct sctp_default_prinfo *info;
3155
3156                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3157                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3158
3159                         if (stcb) {
3160                                 info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3161                                 info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3162                                 SCTP_TCB_UNLOCK(stcb);
3163                         } else {
3164                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3165                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3166                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3167                                         SCTP_INP_RLOCK(inp);
3168                                         info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3169                                         info->pr_value = inp->def_send.sinfo_timetolive;
3170                                         SCTP_INP_RUNLOCK(inp);
3171                                 } else {
3172                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3173                                         error = EINVAL;
3174                                 }
3175                         }
3176                         if (error == 0) {
3177                                 *optsize = sizeof(struct sctp_default_prinfo);
3178                         }
3179                         break;
3180                 }
3181         case SCTP_PEER_ADDR_THLDS:
3182                 {
3183                         struct sctp_paddrthlds *thlds;
3184                         struct sctp_nets *net;
3185                         struct sockaddr *addr;
3186 #if defined(INET) && defined(INET6)
3187                         struct sockaddr_in sin_store;
3188 #endif
3189
3190                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3191                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3192
3193 #if defined(INET) && defined(INET6)
3194                         if (thlds->spt_address.ss_family == AF_INET6) {
3195                                 struct sockaddr_in6 *sin6;
3196
3197                                 sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
3198                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3199                                         in6_sin6_2_sin(&sin_store, sin6);
3200                                         addr = (struct sockaddr *)&sin_store;
3201                                 } else {
3202                                         addr = (struct sockaddr *)&thlds->spt_address;
3203                                 }
3204                         } else {
3205                                 addr = (struct sockaddr *)&thlds->spt_address;
3206                         }
3207 #else
3208                         addr = (struct sockaddr *)&thlds->spt_address;
3209 #endif
3210                         if (stcb != NULL) {
3211                                 net = sctp_findnet(stcb, addr);
3212                         } else {
3213                                 /*
3214                                  * We increment here since
3215                                  * sctp_findassociation_ep_addr() wil do a
3216                                  * decrement if it finds the stcb as long as
3217                                  * the locked tcb (last argument) is NOT a
3218                                  * TCB.. aka NULL.
3219                                  */
3220                                 net = NULL;
3221                                 SCTP_INP_INCR_REF(inp);
3222                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3223                                 if (stcb == NULL) {
3224                                         SCTP_INP_DECR_REF(inp);
3225                                 }
3226                         }
3227                         if ((stcb != NULL) && (net == NULL)) {
3228 #ifdef INET
3229                                 if (addr->sa_family == AF_INET) {
3230                                         struct sockaddr_in *sin;
3231
3232                                         sin = (struct sockaddr_in *)addr;
3233                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
3234                                                 error = EINVAL;
3235                                                 SCTP_TCB_UNLOCK(stcb);
3236                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3237                                                 break;
3238                                         }
3239                                 } else
3240 #endif
3241 #ifdef INET6
3242                                 if (addr->sa_family == AF_INET6) {
3243                                         struct sockaddr_in6 *sin6;
3244
3245                                         sin6 = (struct sockaddr_in6 *)addr;
3246                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3247                                                 error = EINVAL;
3248                                                 SCTP_TCB_UNLOCK(stcb);
3249                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3250                                                 break;
3251                                         }
3252                                 } else
3253 #endif
3254                                 {
3255                                         error = EAFNOSUPPORT;
3256                                         SCTP_TCB_UNLOCK(stcb);
3257                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3258                                         break;
3259                                 }
3260                         }
3261
3262                         if (stcb != NULL) {
3263                                 if (net != NULL) {
3264                                         thlds->spt_pathmaxrxt = net->failure_threshold;
3265                                         thlds->spt_pathpfthld = net->pf_threshold;
3266                                         thlds->spt_pathcpthld = 0xffff;
3267                                 } else {
3268                                         thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3269                                         thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3270                                         thlds->spt_pathcpthld = 0xffff;
3271                                 }
3272                                 thlds->spt_assoc_id = sctp_get_associd(stcb);
3273                                 SCTP_TCB_UNLOCK(stcb);
3274                         } else {
3275                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3276                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3277                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3278                                         /* Use endpoint defaults */
3279                                         SCTP_INP_RLOCK(inp);
3280                                         thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3281                                         thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3282                                         thlds->spt_pathcpthld = 0xffff;
3283                                         SCTP_INP_RUNLOCK(inp);
3284                                 } else {
3285                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3286                                         error = EINVAL;
3287                                 }
3288                         }
3289                         if (error == 0) {
3290                                 *optsize = sizeof(struct sctp_paddrthlds);
3291                         }
3292                         break;
3293                 }
3294         case SCTP_REMOTE_UDP_ENCAPS_PORT:
3295                 {
3296                         struct sctp_udpencaps *encaps;
3297                         struct sctp_nets *net;
3298                         struct sockaddr *addr;
3299 #if defined(INET) && defined(INET6)
3300                         struct sockaddr_in sin_store;
3301 #endif
3302
3303                         SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3304                         SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3305
3306 #if defined(INET) && defined(INET6)
3307                         if (encaps->sue_address.ss_family == AF_INET6) {
3308                                 struct sockaddr_in6 *sin6;
3309
3310                                 sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
3311                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3312                                         in6_sin6_2_sin(&sin_store, sin6);
3313                                         addr = (struct sockaddr *)&sin_store;
3314                                 } else {
3315                                         addr = (struct sockaddr *)&encaps->sue_address;
3316                                 }
3317                         } else {
3318                                 addr = (struct sockaddr *)&encaps->sue_address;
3319                         }
3320 #else
3321                         addr = (struct sockaddr *)&encaps->sue_address;
3322 #endif
3323                         if (stcb) {
3324                                 net = sctp_findnet(stcb, addr);
3325                         } else {
3326                                 /*
3327                                  * We increment here since
3328                                  * sctp_findassociation_ep_addr() wil do a
3329                                  * decrement if it finds the stcb as long as
3330                                  * the locked tcb (last argument) is NOT a
3331                                  * TCB.. aka NULL.
3332                                  */
3333                                 net = NULL;
3334                                 SCTP_INP_INCR_REF(inp);
3335                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
3336                                 if (stcb == NULL) {
3337                                         SCTP_INP_DECR_REF(inp);
3338                                 }
3339                         }
3340                         if ((stcb != NULL) && (net == NULL)) {
3341 #ifdef INET
3342                                 if (addr->sa_family == AF_INET) {
3343                                         struct sockaddr_in *sin;
3344
3345                                         sin = (struct sockaddr_in *)addr;
3346                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
3347                                                 error = EINVAL;
3348                                                 SCTP_TCB_UNLOCK(stcb);
3349                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3350                                                 break;
3351                                         }
3352                                 } else
3353 #endif
3354 #ifdef INET6
3355                                 if (addr->sa_family == AF_INET6) {
3356                                         struct sockaddr_in6 *sin6;
3357
3358                                         sin6 = (struct sockaddr_in6 *)addr;
3359                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3360                                                 error = EINVAL;
3361                                                 SCTP_TCB_UNLOCK(stcb);
3362                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3363                                                 break;
3364                                         }
3365                                 } else
3366 #endif
3367                                 {
3368                                         error = EAFNOSUPPORT;
3369                                         SCTP_TCB_UNLOCK(stcb);
3370                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3371                                         break;
3372                                 }
3373                         }
3374
3375                         if (stcb != NULL) {
3376                                 if (net) {
3377                                         encaps->sue_port = net->port;
3378                                 } else {
3379                                         encaps->sue_port = stcb->asoc.port;
3380                                 }
3381                                 SCTP_TCB_UNLOCK(stcb);
3382                         } else {
3383                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3384                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3385                                     (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3386                                         SCTP_INP_RLOCK(inp);
3387                                         encaps->sue_port = inp->sctp_ep.port;
3388                                         SCTP_INP_RUNLOCK(inp);
3389                                 } else {
3390                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3391                                         error = EINVAL;
3392                                 }
3393                         }
3394                         if (error == 0) {
3395                                 *optsize = sizeof(struct sctp_udpencaps);
3396                         }
3397                         break;
3398                 }
3399         case SCTP_ECN_SUPPORTED:
3400                 {
3401                         struct sctp_assoc_value *av;
3402
3403                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3404                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3405
3406                         if (stcb) {
3407                                 av->assoc_value = stcb->asoc.ecn_supported;
3408                                 SCTP_TCB_UNLOCK(stcb);
3409                         } else {
3410                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3411                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3412                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3413                                         SCTP_INP_RLOCK(inp);
3414                                         av->assoc_value = inp->ecn_supported;
3415                                         SCTP_INP_RUNLOCK(inp);
3416                                 } else {
3417                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3418                                         error = EINVAL;
3419                                 }
3420                         }
3421                         if (error == 0) {
3422                                 *optsize = sizeof(struct sctp_assoc_value);
3423                         }
3424                         break;
3425                 }
3426         case SCTP_PR_SUPPORTED:
3427                 {
3428                         struct sctp_assoc_value *av;
3429
3430                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3431                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3432
3433                         if (stcb) {
3434                                 av->assoc_value = stcb->asoc.prsctp_supported;
3435                                 SCTP_TCB_UNLOCK(stcb);
3436                         } else {
3437                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3438                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3439                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3440                                         SCTP_INP_RLOCK(inp);
3441                                         av->assoc_value = inp->prsctp_supported;
3442                                         SCTP_INP_RUNLOCK(inp);
3443                                 } else {
3444                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3445                                         error = EINVAL;
3446                                 }
3447                         }
3448                         if (error == 0) {
3449                                 *optsize = sizeof(struct sctp_assoc_value);
3450                         }
3451                         break;
3452                 }
3453         case SCTP_AUTH_SUPPORTED:
3454                 {
3455                         struct sctp_assoc_value *av;
3456
3457                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3458                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3459
3460                         if (stcb) {
3461                                 av->assoc_value = stcb->asoc.auth_supported;
3462                                 SCTP_TCB_UNLOCK(stcb);
3463                         } else {
3464                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3465                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3466                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3467                                         SCTP_INP_RLOCK(inp);
3468                                         av->assoc_value = inp->auth_supported;
3469                                         SCTP_INP_RUNLOCK(inp);
3470                                 } else {
3471                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3472                                         error = EINVAL;
3473                                 }
3474                         }
3475                         if (error == 0) {
3476                                 *optsize = sizeof(struct sctp_assoc_value);
3477                         }
3478                         break;
3479                 }
3480         case SCTP_ASCONF_SUPPORTED:
3481                 {
3482                         struct sctp_assoc_value *av;
3483
3484                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3485                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3486
3487                         if (stcb) {
3488                                 av->assoc_value = stcb->asoc.asconf_supported;
3489                                 SCTP_TCB_UNLOCK(stcb);
3490                         } else {
3491                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3492                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3493                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3494                                         SCTP_INP_RLOCK(inp);
3495                                         av->assoc_value = inp->asconf_supported;
3496                                         SCTP_INP_RUNLOCK(inp);
3497                                 } else {
3498                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3499                                         error = EINVAL;
3500                                 }
3501                         }
3502                         if (error == 0) {
3503                                 *optsize = sizeof(struct sctp_assoc_value);
3504                         }
3505                         break;
3506                 }
3507         case SCTP_RECONFIG_SUPPORTED:
3508                 {
3509                         struct sctp_assoc_value *av;
3510
3511                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3512                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3513
3514                         if (stcb) {
3515                                 av->assoc_value = stcb->asoc.reconfig_supported;
3516                                 SCTP_TCB_UNLOCK(stcb);
3517                         } else {
3518                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3519                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3520                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3521                                         SCTP_INP_RLOCK(inp);
3522                                         av->assoc_value = inp->reconfig_supported;
3523                                         SCTP_INP_RUNLOCK(inp);
3524                                 } else {
3525                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3526                                         error = EINVAL;
3527                                 }
3528                         }
3529                         if (error == 0) {
3530                                 *optsize = sizeof(struct sctp_assoc_value);
3531                         }
3532                         break;
3533                 }
3534         case SCTP_NRSACK_SUPPORTED:
3535                 {
3536                         struct sctp_assoc_value *av;
3537
3538                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3539                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3540
3541                         if (stcb) {
3542                                 av->assoc_value = stcb->asoc.nrsack_supported;
3543                                 SCTP_TCB_UNLOCK(stcb);
3544                         } else {
3545                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3546                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3547                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3548                                         SCTP_INP_RLOCK(inp);
3549                                         av->assoc_value = inp->nrsack_supported;
3550                                         SCTP_INP_RUNLOCK(inp);
3551                                 } else {
3552                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3553                                         error = EINVAL;
3554                                 }
3555                         }
3556                         if (error == 0) {
3557                                 *optsize = sizeof(struct sctp_assoc_value);
3558                         }
3559                         break;
3560                 }
3561         case SCTP_PKTDROP_SUPPORTED:
3562                 {
3563                         struct sctp_assoc_value *av;
3564
3565                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3566                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3567
3568                         if (stcb) {
3569                                 av->assoc_value = stcb->asoc.pktdrop_supported;
3570                                 SCTP_TCB_UNLOCK(stcb);
3571                         } else {
3572                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3573                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3574                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3575                                         SCTP_INP_RLOCK(inp);
3576                                         av->assoc_value = inp->pktdrop_supported;
3577                                         SCTP_INP_RUNLOCK(inp);
3578                                 } else {
3579                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3580                                         error = EINVAL;
3581                                 }
3582                         }
3583                         if (error == 0) {
3584                                 *optsize = sizeof(struct sctp_assoc_value);
3585                         }
3586                         break;
3587                 }
3588         case SCTP_ENABLE_STREAM_RESET:
3589                 {
3590                         struct sctp_assoc_value *av;
3591
3592                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3593                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3594
3595                         if (stcb) {
3596                                 av->assoc_value = (uint32_t)stcb->asoc.local_strreset_support;
3597                                 SCTP_TCB_UNLOCK(stcb);
3598                         } else {
3599                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3600                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3601                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3602                                         SCTP_INP_RLOCK(inp);
3603                                         av->assoc_value = (uint32_t)inp->local_strreset_support;
3604                                         SCTP_INP_RUNLOCK(inp);
3605                                 } else {
3606                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3607                                         error = EINVAL;
3608                                 }
3609                         }
3610                         if (error == 0) {
3611                                 *optsize = sizeof(struct sctp_assoc_value);
3612                         }
3613                         break;
3614                 }
3615         case SCTP_PR_STREAM_STATUS:
3616                 {
3617                         struct sctp_prstatus *sprstat;
3618                         uint16_t sid;
3619                         uint16_t policy;
3620
3621                         SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3622                         SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3623
3624                         sid = sprstat->sprstat_sid;
3625                         policy = sprstat->sprstat_policy;
3626 #if defined(SCTP_DETAILED_STR_STATS)
3627                         if ((stcb != NULL) &&
3628                             (sid < stcb->asoc.streamoutcnt) &&
3629                             (policy != SCTP_PR_SCTP_NONE) &&
3630                             ((policy <= SCTP_PR_SCTP_MAX) ||
3631                             (policy == SCTP_PR_SCTP_ALL))) {
3632                                 if (policy == SCTP_PR_SCTP_ALL) {
3633                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3634                                         sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3635                                 } else {
3636                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
3637                                         sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
3638                                 }
3639 #else
3640                         if ((stcb != NULL) &&
3641                             (sid < stcb->asoc.streamoutcnt) &&
3642                             (policy == SCTP_PR_SCTP_ALL)) {
3643                                 sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
3644                                 sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
3645 #endif
3646                         } else {
3647                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3648                                 error = EINVAL;
3649                         }
3650                         if (stcb != NULL) {
3651                                 SCTP_TCB_UNLOCK(stcb);
3652                         }
3653                         if (error == 0) {
3654                                 *optsize = sizeof(struct sctp_prstatus);
3655                         }
3656                         break;
3657                 }
3658         case SCTP_PR_ASSOC_STATUS:
3659                 {
3660                         struct sctp_prstatus *sprstat;
3661                         uint16_t policy;
3662
3663                         SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
3664                         SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
3665
3666                         policy = sprstat->sprstat_policy;
3667                         if ((stcb != NULL) &&
3668                             (policy != SCTP_PR_SCTP_NONE) &&
3669                             ((policy <= SCTP_PR_SCTP_MAX) ||
3670                             (policy == SCTP_PR_SCTP_ALL))) {
3671                                 if (policy == SCTP_PR_SCTP_ALL) {
3672                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
3673                                         sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
3674                                 } else {
3675                                         sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
3676                                         sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
3677                                 }
3678                         } else {
3679                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3680                                 error = EINVAL;
3681                         }
3682                         if (stcb != NULL) {
3683                                 SCTP_TCB_UNLOCK(stcb);
3684                         }
3685                         if (error == 0) {
3686                                 *optsize = sizeof(struct sctp_prstatus);
3687                         }
3688                         break;
3689                 }
3690         case SCTP_MAX_CWND:
3691                 {
3692                         struct sctp_assoc_value *av;
3693
3694                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
3695                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3696
3697                         if (stcb) {
3698                                 av->assoc_value = stcb->asoc.max_cwnd;
3699                                 SCTP_TCB_UNLOCK(stcb);
3700                         } else {
3701                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3702                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3703                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3704                                         SCTP_INP_RLOCK(inp);
3705                                         av->assoc_value = inp->max_cwnd;
3706                                         SCTP_INP_RUNLOCK(inp);
3707                                 } else {
3708                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3709                                         error = EINVAL;
3710                                 }
3711                         }
3712                         if (error == 0) {
3713                                 *optsize = sizeof(struct sctp_assoc_value);
3714                         }
3715                         break;
3716                 }
3717         default:
3718                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3719                 error = ENOPROTOOPT;
3720                 break;
3721         }                       /* end switch (sopt->sopt_name) */
3722         if (error) {
3723                 *optsize = 0;
3724         }
3725         return (error);
3726 }
3727
3728 static int
3729 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3730     void *p)
3731 {
3732         int error, set_opt;
3733         uint32_t *mopt;
3734         struct sctp_tcb *stcb = NULL;
3735         struct sctp_inpcb *inp = NULL;
3736         uint32_t vrf_id;
3737
3738         if (optval == NULL) {
3739                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3740                 return (EINVAL);
3741         }
3742         inp = (struct sctp_inpcb *)so->so_pcb;
3743         if (inp == NULL) {
3744                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3745                 return (EINVAL);
3746         }
3747         vrf_id = inp->def_vrf_id;
3748
3749         error = 0;
3750         switch (optname) {
3751         case SCTP_NODELAY:
3752         case SCTP_AUTOCLOSE:
3753         case SCTP_AUTO_ASCONF:
3754         case SCTP_EXPLICIT_EOR:
3755         case SCTP_DISABLE_FRAGMENTS:
3756         case SCTP_USE_EXT_RCVINFO:
3757         case SCTP_I_WANT_MAPPED_V4_ADDR:
3758                 /* copy in the option value */
3759                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3760                 set_opt = 0;
3761                 if (error)
3762                         break;
3763                 switch (optname) {
3764                 case SCTP_DISABLE_FRAGMENTS:
3765                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3766                         break;
3767                 case SCTP_AUTO_ASCONF:
3768                         /*
3769                          * NOTE: we don't really support this flag
3770                          */
3771                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3772                                 /* only valid for bound all sockets */
3773                                 if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3774                                     (*mopt != 0)) {
3775                                         /* forbidden by admin */
3776                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3777                                         return (EPERM);
3778                                 }
3779                                 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3780                         } else {
3781                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3782                                 return (EINVAL);
3783                         }
3784                         break;
3785                 case SCTP_EXPLICIT_EOR:
3786                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3787                         break;
3788                 case SCTP_USE_EXT_RCVINFO:
3789                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3790                         break;
3791                 case SCTP_I_WANT_MAPPED_V4_ADDR:
3792                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3793                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3794                         } else {
3795                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3796                                 return (EINVAL);
3797                         }
3798                         break;
3799                 case SCTP_NODELAY:
3800                         set_opt = SCTP_PCB_FLAGS_NODELAY;
3801                         break;
3802                 case SCTP_AUTOCLOSE:
3803                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3804                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3805                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3806                                 return (EINVAL);
3807                         }
3808                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3809                         /*
3810                          * The value is in ticks. Note this does not effect
3811                          * old associations, only new ones.
3812                          */
3813                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3814                         break;
3815                 }
3816                 SCTP_INP_WLOCK(inp);
3817                 if (*mopt != 0) {
3818                         sctp_feature_on(inp, set_opt);
3819                 } else {
3820                         sctp_feature_off(inp, set_opt);
3821                 }
3822                 SCTP_INP_WUNLOCK(inp);
3823                 break;
3824         case SCTP_REUSE_PORT:
3825                 {
3826                         SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3827                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3828                                 /* Can't set it after we are bound */
3829                                 error = EINVAL;
3830                                 break;
3831                         }
3832                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3833                                 /* Can't do this for a 1-m socket */
3834                                 error = EINVAL;
3835                                 break;
3836                         }
3837                         if (optval)
3838                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3839                         else
3840                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3841                         break;
3842                 }
3843         case SCTP_PARTIAL_DELIVERY_POINT:
3844                 {
3845                         uint32_t *value;
3846
3847                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3848                         if (*value > SCTP_SB_LIMIT_RCV(so)) {
3849                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3850                                 error = EINVAL;
3851                                 break;
3852                         }
3853                         inp->partial_delivery_point = *value;
3854                         break;
3855                 }
3856         case SCTP_FRAGMENT_INTERLEAVE:
3857                 /* not yet until we re-write sctp_recvmsg() */
3858                 {
3859                         uint32_t *level;
3860
3861                         SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3862                         if (*level == SCTP_FRAG_LEVEL_2) {
3863                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3864                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3865                         } else if (*level == SCTP_FRAG_LEVEL_1) {
3866                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3867                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3868                         } else if (*level == SCTP_FRAG_LEVEL_0) {
3869                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3870                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3871
3872                         } else {
3873                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3874                                 error = EINVAL;
3875                         }
3876                         break;
3877                 }
3878         case SCTP_INTERLEAVING_SUPPORTED:
3879                 {
3880                         struct sctp_assoc_value *av;
3881
3882                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3883                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3884
3885                         if (stcb) {
3886                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3887                                 error = EINVAL;
3888                                 SCTP_TCB_UNLOCK(stcb);
3889                         } else {
3890                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3891                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3892                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
3893                                         SCTP_INP_WLOCK(inp);
3894                                         if (av->assoc_value == 0) {
3895                                                 inp->idata_supported = 0;
3896                                         } else {
3897                                                 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
3898                                                     (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS))) {
3899                                                         inp->idata_supported = 1;
3900                                                 } else {
3901                                                         /*
3902                                                          * Must have Frag
3903                                                          * interleave and
3904                                                          * stream interleave
3905                                                          * on
3906                                                          */
3907                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3908                                                         error = EINVAL;
3909                                                 }
3910                                         }
3911                                         SCTP_INP_WUNLOCK(inp);
3912                                 } else {
3913                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3914                                         error = EINVAL;
3915                                 }
3916                         }
3917                         break;
3918                 }
3919         case SCTP_CMT_ON_OFF:
3920                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3921                         struct sctp_assoc_value *av;
3922
3923                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3924                         if (av->assoc_value > SCTP_CMT_MAX) {
3925                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3926                                 error = EINVAL;
3927                                 break;
3928                         }
3929                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3930                         if (stcb) {
3931                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3932                                 SCTP_TCB_UNLOCK(stcb);
3933                         } else {
3934                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3935                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3936                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3937                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3938                                         SCTP_INP_WLOCK(inp);
3939                                         inp->sctp_cmt_on_off = av->assoc_value;
3940                                         SCTP_INP_WUNLOCK(inp);
3941                                 }
3942                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3943                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3944                                         SCTP_INP_RLOCK(inp);
3945                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3946                                                 SCTP_TCB_LOCK(stcb);
3947                                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3948                                                 SCTP_TCB_UNLOCK(stcb);
3949                                         }
3950                                         SCTP_INP_RUNLOCK(inp);
3951                                 }
3952                         }
3953                 } else {
3954                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3955                         error = ENOPROTOOPT;
3956                 }
3957                 break;
3958         case SCTP_PLUGGABLE_CC:
3959                 {
3960                         struct sctp_assoc_value *av;
3961                         struct sctp_nets *net;
3962
3963                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3964                         if ((av->assoc_value != SCTP_CC_RFC2581) &&
3965                             (av->assoc_value != SCTP_CC_HSTCP) &&
3966                             (av->assoc_value != SCTP_CC_HTCP) &&
3967                             (av->assoc_value != SCTP_CC_RTCC)) {
3968                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3969                                 error = EINVAL;
3970                                 break;
3971                         }
3972                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3973                         if (stcb) {
3974                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3975                                 stcb->asoc.congestion_control_module = av->assoc_value;
3976                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3977                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3978                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3979                                         }
3980                                 }
3981                                 SCTP_TCB_UNLOCK(stcb);
3982                         } else {
3983                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3984                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3985                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3986                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3987                                         SCTP_INP_WLOCK(inp);
3988                                         inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
3989                                         SCTP_INP_WUNLOCK(inp);
3990                                 }
3991                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3992                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3993                                         SCTP_INP_RLOCK(inp);
3994                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3995                                                 SCTP_TCB_LOCK(stcb);
3996                                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3997                                                 stcb->asoc.congestion_control_module = av->assoc_value;
3998                                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3999                                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4000                                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4001                                                         }
4002                                                 }
4003                                                 SCTP_TCB_UNLOCK(stcb);
4004                                         }
4005                                         SCTP_INP_RUNLOCK(inp);
4006                                 }
4007                         }
4008                         break;
4009                 }
4010         case SCTP_CC_OPTION:
4011                 {
4012                         struct sctp_cc_option *cc_opt;
4013
4014                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
4015                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
4016                         if (stcb == NULL) {
4017                                 if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
4018                                         SCTP_INP_RLOCK(inp);
4019                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4020                                                 SCTP_TCB_LOCK(stcb);
4021                                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
4022                                                         (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
4023                                                 }
4024                                                 SCTP_TCB_UNLOCK(stcb);
4025                                         }
4026                                         SCTP_INP_RUNLOCK(inp);
4027                                 } else {
4028                                         error = EINVAL;
4029                                 }
4030                         } else {
4031                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
4032                                         error = ENOTSUP;
4033                                 } else {
4034                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
4035                                             cc_opt);
4036                                 }
4037                                 SCTP_TCB_UNLOCK(stcb);
4038                         }
4039                         break;
4040                 }
4041         case SCTP_PLUGGABLE_SS:
4042                 {
4043                         struct sctp_assoc_value *av;
4044
4045                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4046                         if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4047                             (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4048                             (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4049                             (av->assoc_value != SCTP_SS_PRIORITY) &&
4050                             (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4051                             (av->assoc_value != SCTP_SS_FIRST_COME)) {
4052                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4053                                 error = EINVAL;
4054                                 break;
4055                         }
4056                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4057                         if (stcb) {
4058                                 SCTP_TCB_SEND_LOCK(stcb);
4059                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4060                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4061                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4062                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4063                                 SCTP_TCB_SEND_UNLOCK(stcb);
4064                                 SCTP_TCB_UNLOCK(stcb);
4065                         } else {
4066                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4067                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4068                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4069                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4070                                         SCTP_INP_WLOCK(inp);
4071                                         inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
4072                                         SCTP_INP_WUNLOCK(inp);
4073                                 }
4074                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4075                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4076                                         SCTP_INP_RLOCK(inp);
4077                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4078                                                 SCTP_TCB_LOCK(stcb);
4079                                                 SCTP_TCB_SEND_LOCK(stcb);
4080                                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4081                                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4082                                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
4083                                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4084                                                 SCTP_TCB_SEND_UNLOCK(stcb);
4085                                                 SCTP_TCB_UNLOCK(stcb);
4086                                         }
4087                                         SCTP_INP_RUNLOCK(inp);
4088                                 }
4089                         }
4090                         break;
4091                 }
4092         case SCTP_SS_VALUE:
4093                 {
4094                         struct sctp_stream_value *av;
4095
4096                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4097                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4098                         if (stcb) {
4099                                 if ((av->stream_id >= stcb->asoc.streamoutcnt) ||
4100                                     (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4101                                     av->stream_value) < 0)) {
4102                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4103                                         error = EINVAL;
4104                                 }
4105                                 SCTP_TCB_UNLOCK(stcb);
4106                         } else {
4107                                 if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4108                                         SCTP_INP_RLOCK(inp);
4109                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4110                                                 SCTP_TCB_LOCK(stcb);
4111                                                 if (av->stream_id < stcb->asoc.streamoutcnt) {
4112                                                         stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4113                                                             &stcb->asoc,
4114                                                             &stcb->asoc.strmout[av->stream_id],
4115                                                             av->stream_value);
4116                                                 }
4117                                                 SCTP_TCB_UNLOCK(stcb);
4118                                         }
4119                                         SCTP_INP_RUNLOCK(inp);
4120                                 } else {
4121                                         /*
4122                                          * Can't set stream value without
4123                                          * association
4124                                          */
4125                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4126                                         error = EINVAL;
4127                                 }
4128                         }
4129                         break;
4130                 }
4131         case SCTP_CLR_STAT_LOG:
4132                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4133                 error = EOPNOTSUPP;
4134                 break;
4135         case SCTP_CONTEXT:
4136                 {
4137                         struct sctp_assoc_value *av;
4138
4139                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4140                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4141
4142                         if (stcb) {
4143                                 stcb->asoc.context = av->assoc_value;
4144                                 SCTP_TCB_UNLOCK(stcb);
4145                         } else {
4146                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4147                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4148                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4149                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4150                                         SCTP_INP_WLOCK(inp);
4151                                         inp->sctp_context = av->assoc_value;
4152                                         SCTP_INP_WUNLOCK(inp);
4153                                 }
4154                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4155                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4156                                         SCTP_INP_RLOCK(inp);
4157                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4158                                                 SCTP_TCB_LOCK(stcb);
4159                                                 stcb->asoc.context = av->assoc_value;
4160                                                 SCTP_TCB_UNLOCK(stcb);
4161                                         }
4162                                         SCTP_INP_RUNLOCK(inp);
4163                                 }
4164                         }
4165                         break;
4166                 }
4167         case SCTP_VRF_ID:
4168                 {
4169                         uint32_t *default_vrfid;
4170
4171                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4172                         if (*default_vrfid > SCTP_MAX_VRF_ID) {
4173                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4174                                 error = EINVAL;
4175                                 break;
4176                         }
4177                         inp->def_vrf_id = *default_vrfid;
4178                         break;
4179                 }
4180         case SCTP_DEL_VRF_ID:
4181                 {
4182                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4183                         error = EOPNOTSUPP;
4184                         break;
4185                 }
4186         case SCTP_ADD_VRF_ID:
4187                 {
4188                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4189                         error = EOPNOTSUPP;
4190                         break;
4191                 }
4192         case SCTP_DELAYED_SACK:
4193                 {
4194                         struct sctp_sack_info *sack;
4195
4196                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4197                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4198                         if (sack->sack_delay) {
4199                                 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4200                                         sack->sack_delay = SCTP_MAX_SACK_DELAY;
4201                                 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4202                                         sack->sack_delay = TICKS_TO_MSEC(1);
4203                                 }
4204                         }
4205                         if (stcb) {
4206                                 if (sack->sack_delay) {
4207                                         stcb->asoc.delayed_ack = sack->sack_delay;
4208                                 }
4209                                 if (sack->sack_freq) {
4210                                         stcb->asoc.sack_freq = sack->sack_freq;
4211                                 }
4212                                 SCTP_TCB_UNLOCK(stcb);
4213                         } else {
4214                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4215                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4216                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4217                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4218                                         SCTP_INP_WLOCK(inp);
4219                                         if (sack->sack_delay) {
4220                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4221                                         }
4222                                         if (sack->sack_freq) {
4223                                                 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4224                                         }
4225                                         SCTP_INP_WUNLOCK(inp);
4226                                 }
4227                                 if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4228                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4229                                         SCTP_INP_RLOCK(inp);
4230                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4231                                                 SCTP_TCB_LOCK(stcb);
4232                                                 if (sack->sack_delay) {
4233                                                         stcb->asoc.delayed_ack = sack->sack_delay;
4234                                                 }
4235                                                 if (sack->sack_freq) {
4236                                                         stcb->asoc.sack_freq = sack->sack_freq;
4237                                                 }
4238                                                 SCTP_TCB_UNLOCK(stcb);
4239                                         }
4240                                         SCTP_INP_RUNLOCK(inp);
4241                                 }
4242                         }
4243                         break;
4244                 }
4245         case SCTP_AUTH_CHUNK:
4246                 {
4247                         struct sctp_authchunk *sauth;
4248
4249                         SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4250
4251                         SCTP_INP_WLOCK(inp);
4252                         if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4253                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4254                                 error = EINVAL;
4255                         } else {
4256                                 inp->auth_supported = 1;
4257                         }
4258                         SCTP_INP_WUNLOCK(inp);
4259                         break;
4260                 }
4261         case SCTP_AUTH_KEY:
4262                 {
4263                         struct sctp_authkey *sca;
4264                         struct sctp_keyhead *shared_keys;
4265                         sctp_sharedkey_t *shared_key;
4266                         sctp_key_t *key = NULL;
4267                         size_t size;
4268
4269                         SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4270                         if (sca->sca_keylength == 0) {
4271                                 size = optsize - sizeof(struct sctp_authkey);
4272                         } else {
4273                                 if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4274                                         size = sca->sca_keylength;
4275                                 } else {
4276                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4277                                         error = EINVAL;
4278                                         break;
4279                                 }
4280                         }
4281                         SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4282
4283                         if (stcb) {
4284                                 shared_keys = &stcb->asoc.shared_keys;
4285                                 /* clear the cached keys for this key id */
4286                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4287                                 /*
4288                                  * create the new shared key and
4289                                  * insert/replace it
4290                                  */
4291                                 if (size > 0) {
4292                                         key = sctp_set_key(sca->sca_key, (uint32_t)size);
4293                                         if (key == NULL) {
4294                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4295                                                 error = ENOMEM;
4296                                                 SCTP_TCB_UNLOCK(stcb);
4297                                                 break;
4298                                         }
4299                                 }
4300                                 shared_key = sctp_alloc_sharedkey();
4301                                 if (shared_key == NULL) {
4302                                         sctp_free_key(key);
4303                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4304                                         error = ENOMEM;
4305                                         SCTP_TCB_UNLOCK(stcb);
4306                                         break;
4307                                 }
4308                                 shared_key->key = key;
4309                                 shared_key->keyid = sca->sca_keynumber;
4310                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4311                                 SCTP_TCB_UNLOCK(stcb);
4312                         } else {
4313                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4314                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4315                                     (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4316                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4317                                         SCTP_INP_WLOCK(inp);
4318                                         shared_keys = &inp->sctp_ep.shared_keys;
4319                                         /*
4320                                          * clear the cached keys on all
4321                                          * assocs for this key id
4322                                          */
4323                                         sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4324                                         /*
4325                                          * create the new shared key and
4326                                          * insert/replace it
4327                                          */
4328                                         if (size > 0) {
4329                                                 key = sctp_set_key(sca->sca_key, (uint32_t)size);
4330                                                 if (key == NULL) {
4331                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4332                                                         error = ENOMEM;
4333                                                         SCTP_INP_WUNLOCK(inp);
4334                                                         break;
4335                                                 }
4336                                         }
4337                                         shared_key = sctp_alloc_sharedkey();
4338                                         if (shared_key == NULL) {
4339                                                 sctp_free_key(key);
4340                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4341                                                 error = ENOMEM;
4342                                                 SCTP_INP_WUNLOCK(inp);
4343                                                 break;
4344                                         }
4345                                         shared_key->key = key;
4346                                         shared_key->keyid = sca->sca_keynumber;
4347                                         error = sctp_insert_sharedkey(shared_keys, shared_key);
4348                                         SCTP_INP_WUNLOCK(inp);
4349                                 }
4350                                 if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4351                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4352                                         SCTP_INP_RLOCK(inp);
4353                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4354                                                 SCTP_TCB_LOCK(stcb);
4355                                                 shared_keys = &stcb->asoc.shared_keys;
4356                                                 /*
4357                                                  * clear the cached keys for
4358                                                  * this key id
4359                                                  */
4360                                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4361                                                 /*
4362                                                  * create the new shared key
4363                                                  * and insert/replace it
4364                                                  */
4365                                                 if (size > 0) {
4366                                                         key = sctp_set_key(sca->sca_key, (uint32_t)size);
4367                                                         if (key == NULL) {
4368                                                                 SCTP_TCB_UNLOCK(stcb);
4369                                                                 continue;
4370                                                         }
4371                                                 }
4372                                                 shared_key = sctp_alloc_sharedkey();
4373                                                 if (shared_key == NULL) {
4374                                                         sctp_free_key(key);
4375                                                         SCTP_TCB_UNLOCK(stcb);
4376                                                         continue;
4377                                                 }
4378                                                 shared_key->key = key;
4379                                                 shared_key->keyid = sca->sca_keynumber;
4380                                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4381                                                 SCTP_TCB_UNLOCK(stcb);
4382                                         }
4383                                         SCTP_INP_RUNLOCK(inp);
4384                                 }
4385                         }
4386                         break;
4387                 }
4388         case SCTP_HMAC_IDENT:
4389                 {
4390                         struct sctp_hmacalgo *shmac;
4391                         sctp_hmaclist_t *hmaclist;
4392                         uint16_t hmacid;
4393                         uint32_t i;
4394
4395                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
4396                         if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
4397                             (shmac->shmac_number_of_idents > 0xffff)) {
4398                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4399                                 error = EINVAL;
4400                                 break;
4401                         }
4402
4403                         hmaclist = sctp_alloc_hmaclist((uint16_t)shmac->shmac_number_of_idents);
4404                         if (hmaclist == NULL) {
4405                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4406                                 error = ENOMEM;
4407                                 break;
4408                         }
4409                         for (i = 0; i < shmac->shmac_number_of_idents; i++) {
4410                                 hmacid = shmac->shmac_idents[i];
4411                                 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
4412                                          /* invalid HMACs were found */ ;
4413                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4414                                         error = EINVAL;
4415                                         sctp_free_hmaclist(hmaclist);
4416                                         goto sctp_set_hmac_done;
4417                                 }
4418                         }
4419                         for (i = 0; i < hmaclist->num_algo; i++) {
4420                                 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
4421                                         /* already in list */
4422                                         break;
4423                                 }
4424                         }
4425                         if (i == hmaclist->num_algo) {
4426                                 /* not found in list */
4427                                 sctp_free_hmaclist(hmaclist);
4428                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4429                                 error = EINVAL;
4430                                 break;
4431                         }
4432                         /* set it on the endpoint */
4433                         SCTP_INP_WLOCK(inp);
4434                         if (inp->sctp_ep.local_hmacs)
4435                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4436                         inp->sctp_ep.local_hmacs = hmaclist;
4437                         SCTP_INP_WUNLOCK(inp);
4438         sctp_set_hmac_done:
4439                         break;
4440                 }
4441         case SCTP_AUTH_ACTIVE_KEY:
4442                 {
4443                         struct sctp_authkeyid *scact;
4444
4445                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
4446                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
4447
4448                         /* set the active key on the right place */
4449                         if (stcb) {
4450                                 /* set the active key on the assoc */
4451                                 if (sctp_auth_setactivekey(stcb,
4452                                     scact->scact_keynumber)) {
4453                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
4454                                             SCTP_FROM_SCTP_USRREQ,
4455                                             EINVAL);
4456                                         error = EINVAL;
4457                                 }
4458                                 SCTP_TCB_UNLOCK(stcb);
4459                         } else {
4460                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4461                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4462                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4463                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4464                                         SCTP_INP_WLOCK(inp);
4465                                         if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
4466                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4467                                                 error = EINVAL;
4468                                         }
4469                                         SCTP_INP_WUNLOCK(inp);
4470                                 }
4471                                 if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4472                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
4473                                         SCTP_INP_RLOCK(inp);
4474                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4475                                                 SCTP_TCB_LOCK(stcb);
4476                                                 sctp_auth_setactivekey(stcb, scact->scact_keynumber);
4477                                                 SCTP_TCB_UNLOCK(stcb);
4478                                         }
4479                                         SCTP_INP_RUNLOCK(inp);
4480                                 }
4481                         }
4482                         break;
4483                 }
4484         case SCTP_AUTH_DELETE_KEY:
4485                 {
4486                         struct sctp_authkeyid *scdel;
4487
4488                         SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
4489                         SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
4490
4491                         /* delete the key from the right place */
4492                         if (stcb) {
4493                                 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
4494                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4495                                         error = EINVAL;
4496                                 }
4497                                 SCTP_TCB_UNLOCK(stcb);
4498                         } else {
4499                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4500                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4501                                     (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4502                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4503                                         SCTP_INP_WLOCK(inp);
4504                                         if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
4505                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4506                                                 error = EINVAL;
4507                                         }
4508                                         SCTP_INP_WUNLOCK(inp);
4509                                 }
4510                                 if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4511                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
4512                                         SCTP_INP_RLOCK(inp);
4513                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4514                                                 SCTP_TCB_LOCK(stcb);
4515                                                 sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
4516                                                 SCTP_TCB_UNLOCK(stcb);
4517                                         }
4518                                         SCTP_INP_RUNLOCK(inp);
4519                                 }
4520                         }
4521                         break;
4522                 }
4523         case SCTP_AUTH_DEACTIVATE_KEY:
4524                 {
4525                         struct sctp_authkeyid *keyid;
4526
4527                         SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
4528                         SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
4529
4530                         /* deactivate the key from the right place */
4531                         if (stcb) {
4532                                 if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
4533                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4534                                         error = EINVAL;
4535                                 }
4536                                 SCTP_TCB_UNLOCK(stcb);
4537                         } else {
4538                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4539                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4540                                     (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
4541                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4542                                         SCTP_INP_WLOCK(inp);
4543                                         if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
4544                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4545                                                 error = EINVAL;
4546                                         }
4547                                         SCTP_INP_WUNLOCK(inp);
4548                                 }
4549                                 if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
4550                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
4551                                         SCTP_INP_RLOCK(inp);
4552                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4553                                                 SCTP_TCB_LOCK(stcb);
4554                                                 sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4555                                                 SCTP_TCB_UNLOCK(stcb);
4556                                         }
4557                                         SCTP_INP_RUNLOCK(inp);
4558                                 }
4559                         }
4560                         break;
4561                 }
4562         case SCTP_ENABLE_STREAM_RESET:
4563                 {
4564                         struct sctp_assoc_value *av;
4565
4566                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4567                         if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
4568                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4569                                 error = EINVAL;
4570                                 break;
4571                         }
4572                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4573                         if (stcb) {
4574                                 stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
4575                                 SCTP_TCB_UNLOCK(stcb);
4576                         } else {
4577                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4578                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4579                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4580                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4581                                         SCTP_INP_WLOCK(inp);
4582                                         inp->local_strreset_support = (uint8_t)av->assoc_value;
4583                                         SCTP_INP_WUNLOCK(inp);
4584                                 }
4585                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4586                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4587                                         SCTP_INP_RLOCK(inp);
4588                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4589                                                 SCTP_TCB_LOCK(stcb);
4590                                                 stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
4591                                                 SCTP_TCB_UNLOCK(stcb);
4592                                         }
4593                                         SCTP_INP_RUNLOCK(inp);
4594                                 }
4595
4596                         }
4597                         break;
4598                 }
4599         case SCTP_RESET_STREAMS:
4600                 {
4601                         struct sctp_reset_streams *strrst;
4602                         int i, send_out = 0;
4603                         int send_in = 0;
4604
4605                         SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
4606                         SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
4607                         if (stcb == NULL) {
4608                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4609                                 error = ENOENT;
4610                                 break;
4611                         }
4612                         if (stcb->asoc.reconfig_supported == 0) {
4613                                 /*
4614                                  * Peer does not support the chunk type.
4615                                  */
4616                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4617                                 error = EOPNOTSUPP;
4618                                 SCTP_TCB_UNLOCK(stcb);
4619                                 break;
4620                         }
4621                         if (SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) {
4622                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4623                                 error = EINVAL;
4624                                 SCTP_TCB_UNLOCK(stcb);
4625                                 break;
4626                         }
4627                         if (sizeof(struct sctp_reset_streams) +
4628                             strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
4629                                 error = EINVAL;
4630                                 SCTP_TCB_UNLOCK(stcb);
4631                                 break;
4632                         }
4633                         if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
4634                                 send_in = 1;
4635                                 if (stcb->asoc.stream_reset_outstanding) {
4636                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4637                                         error = EALREADY;
4638                                         SCTP_TCB_UNLOCK(stcb);
4639                                         break;
4640                                 }
4641                         }
4642                         if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
4643                                 send_out = 1;
4644                         }
4645                         if ((strrst->srs_number_streams > SCTP_MAX_STREAMS_AT_ONCE_RESET) && send_in) {
4646                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4647                                 error = ENOMEM;
4648                                 SCTP_TCB_UNLOCK(stcb);
4649                                 break;
4650                         }
4651                         if ((send_in == 0) && (send_out == 0)) {
4652                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4653                                 error = EINVAL;
4654                                 SCTP_TCB_UNLOCK(stcb);
4655                                 break;
4656                         }
4657                         for (i = 0; i < strrst->srs_number_streams; i++) {
4658                                 if ((send_in) &&
4659                                     (strrst->srs_stream_list[i] >= stcb->asoc.streamincnt)) {
4660                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4661                                         error = EINVAL;
4662                                         break;
4663                                 }
4664                                 if ((send_out) &&
4665                                     (strrst->srs_stream_list[i] >= stcb->asoc.streamoutcnt)) {
4666                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4667                                         error = EINVAL;
4668                                         break;
4669                                 }
4670                         }
4671                         if (error) {
4672                                 SCTP_TCB_UNLOCK(stcb);
4673                                 break;
4674                         }
4675                         if (send_out) {
4676                                 int cnt;
4677                                 uint16_t strm;
4678
4679                                 if (strrst->srs_number_streams) {
4680                                         for (i = 0, cnt = 0; i < strrst->srs_number_streams; i++) {
4681                                                 strm = strrst->srs_stream_list[i];
4682                                                 if (stcb->asoc.strmout[strm].state == SCTP_STREAM_OPEN) {
4683                                                         stcb->asoc.strmout[strm].state = SCTP_STREAM_RESET_PENDING;
4684                                                         cnt++;
4685                                                 }
4686                                         }
4687                                 } else {
4688                                         /* Its all */
4689                                         for (i = 0, cnt = 0; i < stcb->asoc.streamoutcnt; i++) {
4690                                                 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN) {
4691                                                         stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
4692                                                         cnt++;
4693                                                 }
4694                                         }
4695                                 }
4696                         }
4697                         if (send_in) {
4698                                 error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
4699                                     strrst->srs_stream_list,
4700                                     send_in, 0, 0, 0, 0, 0);
4701                         } else {
4702                                 error = sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_LOCKED);
4703                         }
4704                         if (error == 0) {
4705                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4706                         } else {
4707                                 /*
4708                                  * For outgoing streams don't report any
4709                                  * problems in sending the request to the
4710                                  * application. XXX: Double check resetting
4711                                  * incoming streams.
4712                                  */
4713                                 error = 0;
4714                         }
4715                         SCTP_TCB_UNLOCK(stcb);
4716                         break;
4717                 }
4718         case SCTP_ADD_STREAMS:
4719                 {
4720                         struct sctp_add_streams *stradd;
4721                         uint8_t addstream = 0;
4722                         uint16_t add_o_strmcnt = 0;
4723                         uint16_t add_i_strmcnt = 0;
4724
4725                         SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
4726                         SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
4727                         if (stcb == NULL) {
4728                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4729                                 error = ENOENT;
4730                                 break;
4731                         }
4732                         if (stcb->asoc.reconfig_supported == 0) {
4733                                 /*
4734                                  * Peer does not support the chunk type.
4735                                  */
4736                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4737                                 error = EOPNOTSUPP;
4738                                 SCTP_TCB_UNLOCK(stcb);
4739                                 break;
4740                         }
4741                         if (SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) {
4742                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4743                                 error = EINVAL;
4744                                 SCTP_TCB_UNLOCK(stcb);
4745                                 break;
4746                         }
4747                         if (stcb->asoc.stream_reset_outstanding) {
4748                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4749                                 error = EALREADY;
4750                                 SCTP_TCB_UNLOCK(stcb);
4751                                 break;
4752                         }
4753                         if ((stradd->sas_outstrms == 0) &&
4754                             (stradd->sas_instrms == 0)) {
4755                                 error = EINVAL;
4756                                 goto skip_stuff;
4757                         }
4758                         if (stradd->sas_outstrms) {
4759                                 addstream = 1;
4760                                 /* We allocate here */
4761                                 add_o_strmcnt = stradd->sas_outstrms;
4762                                 if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
4763                                         /* You can't have more than 64k */
4764                                         error = EINVAL;
4765                                         goto skip_stuff;
4766                                 }
4767                         }
4768                         if (stradd->sas_instrms) {
4769                                 int cnt;
4770
4771                                 addstream |= 2;
4772                                 /*
4773                                  * We allocate inside
4774                                  * sctp_send_str_reset_req()
4775                                  */
4776                                 add_i_strmcnt = stradd->sas_instrms;
4777                                 cnt = add_i_strmcnt;
4778                                 cnt += stcb->asoc.streamincnt;
4779                                 if (cnt > 0x0000ffff) {
4780                                         /* You can't have more than 64k */
4781                                         error = EINVAL;
4782                                         goto skip_stuff;
4783                                 }
4784                                 if (cnt > (int)stcb->asoc.max_inbound_streams) {
4785                                         /* More than you are allowed */
4786                                         error = EINVAL;
4787                                         goto skip_stuff;
4788                                 }
4789                         }
4790                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
4791                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4792         skip_stuff:
4793                         SCTP_TCB_UNLOCK(stcb);
4794                         break;
4795                 }
4796         case SCTP_RESET_ASSOC:
4797                 {
4798                         int i;
4799                         uint32_t *value;
4800
4801                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4802                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t)*value);
4803                         if (stcb == NULL) {
4804                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4805                                 error = ENOENT;
4806                                 break;
4807                         }
4808                         if (stcb->asoc.reconfig_supported == 0) {
4809                                 /*
4810                                  * Peer does not support the chunk type.
4811                                  */
4812                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4813                                 error = EOPNOTSUPP;
4814                                 SCTP_TCB_UNLOCK(stcb);
4815                                 break;
4816                         }
4817                         if (SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) {
4818                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4819                                 error = EINVAL;
4820                                 SCTP_TCB_UNLOCK(stcb);
4821                                 break;
4822                         }
4823                         if (stcb->asoc.stream_reset_outstanding) {
4824                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4825                                 error = EALREADY;
4826                                 SCTP_TCB_UNLOCK(stcb);
4827                                 break;
4828                         }
4829                         /*
4830                          * Is there any data pending in the send or sent
4831                          * queues?
4832                          */
4833                         if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
4834                             !TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
4835                 busy_out:
4836                                 error = EBUSY;
4837                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4838                                 SCTP_TCB_UNLOCK(stcb);
4839                                 break;
4840                         }
4841                         /* Do any streams have data queued? */
4842                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4843                                 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
4844                                         goto busy_out;
4845                                 }
4846                         }
4847                         error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 1, 0, 0, 0, 0);
4848                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4849                         SCTP_TCB_UNLOCK(stcb);
4850                         break;
4851                 }
4852         case SCTP_CONNECT_X:
4853                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4854                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4855                         error = EINVAL;
4856                         break;
4857                 }
4858                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4859                 break;
4860         case SCTP_CONNECT_X_DELAYED:
4861                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4862                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4863                         error = EINVAL;
4864                         break;
4865                 }
4866                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4867                 break;
4868         case SCTP_CONNECT_X_COMPLETE:
4869                 {
4870                         struct sockaddr *sa;
4871
4872                         /* FIXME MT: check correct? */
4873                         SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4874
4875                         /* find tcb */
4876                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4877                                 SCTP_INP_RLOCK(inp);
4878                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4879                                 if (stcb) {
4880                                         SCTP_TCB_LOCK(stcb);
4881                                 }
4882                                 SCTP_INP_RUNLOCK(inp);
4883                         } else {
4884                                 /*
4885                                  * We increment here since
4886                                  * sctp_findassociation_ep_addr() wil do a
4887                                  * decrement if it finds the stcb as long as
4888                                  * the locked tcb (last argument) is NOT a
4889                                  * TCB.. aka NULL.
4890                                  */
4891                                 SCTP_INP_INCR_REF(inp);
4892                                 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
4893                                 if (stcb == NULL) {
4894                                         SCTP_INP_DECR_REF(inp);
4895                                 }
4896                         }
4897
4898                         if (stcb == NULL) {
4899                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4900                                 error = ENOENT;
4901                                 break;
4902                         }
4903                         if (stcb->asoc.delayed_connection == 1) {
4904                                 stcb->asoc.delayed_connection = 0;
4905                                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4906                                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4907                                     stcb->asoc.primary_destination,
4908                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_8);
4909                                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4910                         } else {
4911                                 /*
4912                                  * already expired or did not use delayed
4913                                  * connectx
4914                                  */
4915                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4916                                 error = EALREADY;
4917                         }
4918                         SCTP_TCB_UNLOCK(stcb);
4919                         break;
4920                 }
4921         case SCTP_MAX_BURST:
4922                 {
4923                         struct sctp_assoc_value *av;
4924
4925                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4926                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4927
4928                         if (stcb) {
4929                                 stcb->asoc.max_burst = av->assoc_value;
4930                                 SCTP_TCB_UNLOCK(stcb);
4931                         } else {
4932                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4933                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4934                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4935                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4936                                         SCTP_INP_WLOCK(inp);
4937                                         inp->sctp_ep.max_burst = av->assoc_value;
4938                                         SCTP_INP_WUNLOCK(inp);
4939                                 }
4940                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4941                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4942                                         SCTP_INP_RLOCK(inp);
4943                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4944                                                 SCTP_TCB_LOCK(stcb);
4945                                                 stcb->asoc.max_burst = av->assoc_value;
4946                                                 SCTP_TCB_UNLOCK(stcb);
4947                                         }
4948                                         SCTP_INP_RUNLOCK(inp);
4949                                 }
4950                         }
4951                         break;
4952                 }
4953         case SCTP_MAXSEG:
4954                 {
4955                         struct sctp_assoc_value *av;
4956                         int ovh;
4957
4958                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4959                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4960
4961                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4962                                 ovh = SCTP_MED_OVERHEAD;
4963                         } else {
4964                                 ovh = SCTP_MED_V4_OVERHEAD;
4965                         }
4966                         if (stcb) {
4967                                 if (av->assoc_value) {
4968                                         stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4969                                 } else {
4970                                         stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4971                                 }
4972                                 SCTP_TCB_UNLOCK(stcb);
4973                         } else {
4974                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4975                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4976                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4977                                         SCTP_INP_WLOCK(inp);
4978                                         /*
4979                                          * FIXME MT: I think this is not in
4980                                          * tune with the API ID
4981                                          */
4982                                         if (av->assoc_value) {
4983                                                 inp->sctp_frag_point = (av->assoc_value + ovh);
4984                                         } else {
4985                                                 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4986                                         }
4987                                         SCTP_INP_WUNLOCK(inp);
4988                                 } else {
4989                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4990                                         error = EINVAL;
4991                                 }
4992                         }
4993                         break;
4994                 }
4995         case SCTP_EVENTS:
4996                 {
4997                         struct sctp_event_subscribe *events;
4998
4999                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
5000
5001                         SCTP_INP_WLOCK(inp);
5002                         if (events->sctp_data_io_event) {
5003                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5004                         } else {
5005                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5006                         }
5007
5008                         if (events->sctp_association_event) {
5009                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5010                         } else {
5011                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5012                         }
5013
5014                         if (events->sctp_address_event) {
5015                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5016                         } else {
5017                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5018                         }
5019
5020                         if (events->sctp_send_failure_event) {
5021                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5022                         } else {
5023                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5024                         }
5025
5026                         if (events->sctp_peer_error_event) {
5027                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5028                         } else {
5029                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5030                         }
5031
5032                         if (events->sctp_shutdown_event) {
5033                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5034                         } else {
5035                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5036                         }
5037
5038                         if (events->sctp_partial_delivery_event) {
5039                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5040                         } else {
5041                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5042                         }
5043
5044                         if (events->sctp_adaptation_layer_event) {
5045                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5046                         } else {
5047                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5048                         }
5049
5050                         if (events->sctp_authentication_event) {
5051                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5052                         } else {
5053                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5054                         }
5055
5056                         if (events->sctp_sender_dry_event) {
5057                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
5058                         } else {
5059                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5060                         }
5061
5062                         if (events->sctp_stream_reset_event) {
5063                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5064                         } else {
5065                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5066                         }
5067                         SCTP_INP_WUNLOCK(inp);
5068
5069                         SCTP_INP_RLOCK(inp);
5070                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5071                                 SCTP_TCB_LOCK(stcb);
5072                                 if (events->sctp_association_event) {
5073                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5074                                 } else {
5075                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5076                                 }
5077                                 if (events->sctp_address_event) {
5078                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5079                                 } else {
5080                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5081                                 }
5082                                 if (events->sctp_send_failure_event) {
5083                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5084                                 } else {
5085                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5086                                 }
5087                                 if (events->sctp_peer_error_event) {
5088                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5089                                 } else {
5090                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5091                                 }
5092                                 if (events->sctp_shutdown_event) {
5093                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5094                                 } else {
5095                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5096                                 }
5097                                 if (events->sctp_partial_delivery_event) {
5098                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5099                                 } else {
5100                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5101                                 }
5102                                 if (events->sctp_adaptation_layer_event) {
5103                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5104                                 } else {
5105                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5106                                 }
5107                                 if (events->sctp_authentication_event) {
5108                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5109                                 } else {
5110                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5111                                 }
5112                                 if (events->sctp_sender_dry_event) {
5113                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5114                                 } else {
5115                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5116                                 }
5117                                 if (events->sctp_stream_reset_event) {
5118                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5119                                 } else {
5120                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5121                                 }
5122                                 SCTP_TCB_UNLOCK(stcb);
5123                         }
5124                         /*
5125                          * Send up the sender dry event only for 1-to-1
5126                          * style sockets.
5127                          */
5128                         if (events->sctp_sender_dry_event) {
5129                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5130                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5131                                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
5132                                         if (stcb) {
5133                                                 SCTP_TCB_LOCK(stcb);
5134                                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5135                                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5136                                                     (stcb->asoc.stream_queue_cnt == 0)) {
5137                                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5138                                                 }
5139                                                 SCTP_TCB_UNLOCK(stcb);
5140                                         }
5141                                 }
5142                         }
5143                         SCTP_INP_RUNLOCK(inp);
5144                         break;
5145                 }
5146         case SCTP_ADAPTATION_LAYER:
5147                 {
5148                         struct sctp_setadaptation *adap_bits;
5149
5150                         SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5151                         SCTP_INP_WLOCK(inp);
5152                         inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5153                         inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5154                         SCTP_INP_WUNLOCK(inp);
5155                         break;
5156                 }
5157 #ifdef SCTP_DEBUG
5158         case SCTP_SET_INITIAL_DBG_SEQ:
5159                 {
5160                         uint32_t *vvv;
5161
5162                         SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5163                         SCTP_INP_WLOCK(inp);
5164                         inp->sctp_ep.initial_sequence_debug = *vvv;
5165                         SCTP_INP_WUNLOCK(inp);
5166                         break;
5167                 }
5168 #endif
5169         case SCTP_DEFAULT_SEND_PARAM:
5170                 {
5171                         struct sctp_sndrcvinfo *s_info;
5172
5173                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5174                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5175
5176                         if (stcb) {
5177                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5178                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5179                                 } else {
5180                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5181                                         error = EINVAL;
5182                                 }
5183                                 SCTP_TCB_UNLOCK(stcb);
5184                         } else {
5185                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5186                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5187                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5188                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5189                                         SCTP_INP_WLOCK(inp);
5190                                         memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5191                                         SCTP_INP_WUNLOCK(inp);
5192                                 }
5193                                 if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5194                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5195                                         SCTP_INP_RLOCK(inp);
5196                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5197                                                 SCTP_TCB_LOCK(stcb);
5198                                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5199                                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5200                                                 }
5201                                                 SCTP_TCB_UNLOCK(stcb);
5202                                         }
5203                                         SCTP_INP_RUNLOCK(inp);
5204                                 }
5205                         }
5206                         break;
5207                 }
5208         case SCTP_PEER_ADDR_PARAMS:
5209                 {
5210                         struct sctp_paddrparams *paddrp;
5211                         struct sctp_nets *net;
5212                         struct sockaddr *addr;
5213 #if defined(INET) && defined(INET6)
5214                         struct sockaddr_in sin_store;
5215 #endif
5216
5217                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5218                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5219
5220 #if defined(INET) && defined(INET6)
5221                         if (paddrp->spp_address.ss_family == AF_INET6) {
5222                                 struct sockaddr_in6 *sin6;
5223
5224                                 sin6 = (struct sockaddr_in6 *)&paddrp->spp_address;
5225                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5226                                         in6_sin6_2_sin(&sin_store, sin6);
5227                                         addr = (struct sockaddr *)&sin_store;
5228                                 } else {
5229                                         addr = (struct sockaddr *)&paddrp->spp_address;
5230                                 }
5231                         } else {
5232                                 addr = (struct sockaddr *)&paddrp->spp_address;
5233                         }
5234 #else
5235                         addr = (struct sockaddr *)&paddrp->spp_address;
5236 #endif
5237                         if (stcb != NULL) {
5238                                 net = sctp_findnet(stcb, addr);
5239                         } else {
5240                                 /*
5241                                  * We increment here since
5242                                  * sctp_findassociation_ep_addr() wil do a
5243                                  * decrement if it finds the stcb as long as
5244                                  * the locked tcb (last argument) is NOT a
5245                                  * TCB.. aka NULL.
5246                                  */
5247                                 net = NULL;
5248                                 SCTP_INP_INCR_REF(inp);
5249                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5250                                     &net, NULL, NULL);
5251                                 if (stcb == NULL) {
5252                                         SCTP_INP_DECR_REF(inp);
5253                                 }
5254                         }
5255                         if ((stcb != NULL) && (net == NULL)) {
5256 #ifdef INET
5257                                 if (addr->sa_family == AF_INET) {
5258
5259                                         struct sockaddr_in *sin;
5260
5261                                         sin = (struct sockaddr_in *)addr;
5262                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
5263                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5264                                                 SCTP_TCB_UNLOCK(stcb);
5265                                                 error = EINVAL;
5266                                                 break;
5267                                         }
5268                                 } else
5269 #endif
5270 #ifdef INET6
5271                                 if (addr->sa_family == AF_INET6) {
5272                                         struct sockaddr_in6 *sin6;
5273
5274                                         sin6 = (struct sockaddr_in6 *)addr;
5275                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5276                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5277                                                 SCTP_TCB_UNLOCK(stcb);
5278                                                 error = EINVAL;
5279                                                 break;
5280                                         }
5281                                 } else
5282 #endif
5283                                 {
5284                                         error = EAFNOSUPPORT;
5285                                         SCTP_TCB_UNLOCK(stcb);
5286                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5287                                         break;
5288                                 }
5289                         }
5290                         /* sanity checks */
5291                         if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5292                                 if (stcb)
5293                                         SCTP_TCB_UNLOCK(stcb);
5294                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5295                                 return (EINVAL);
5296                         }
5297
5298                         if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5299                                 if (stcb)
5300                                         SCTP_TCB_UNLOCK(stcb);
5301                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5302                                 return (EINVAL);
5303                         }
5304
5305                         if (stcb != NULL) {
5306                                 /************************TCB SPECIFIC SET ******************/
5307                                 if (net != NULL) {
5308                                         /************************NET SPECIFIC SET ******************/
5309                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5310                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5311                                                     !(net->dest_state & SCTP_ADDR_NOHB)) {
5312                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5313                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
5314                                                 }
5315                                                 net->dest_state |= SCTP_ADDR_NOHB;
5316                                         }
5317                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5318                                                 if (paddrp->spp_hbinterval) {
5319                                                         net->heart_beat_delay = paddrp->spp_hbinterval;
5320                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5321                                                         net->heart_beat_delay = 0;
5322                                                 }
5323                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5324                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
5325                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5326                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5327                                         }
5328                                         if (paddrp->spp_flags & SPP_HB_DEMAND) {
5329                                                 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
5330                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5331                                                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5332                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5333                                                 }
5334                                         }
5335                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5336                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5337                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5338                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
5339                                                 }
5340                                                 net->dest_state |= SCTP_ADDR_NO_PMTUD;
5341                                                 net->mtu = paddrp->spp_pathmtu;
5342                                                 switch (net->ro._l_addr.sa.sa_family) {
5343 #ifdef INET
5344                                                 case AF_INET:
5345                                                         net->mtu += SCTP_MIN_V4_OVERHEAD;
5346                                                         break;
5347 #endif
5348 #ifdef INET6
5349                                                 case AF_INET6:
5350                                                         net->mtu += SCTP_MIN_OVERHEAD;
5351                                                         break;
5352 #endif
5353                                                 default:
5354                                                         break;
5355                                                 }
5356                                                 if (net->mtu < stcb->asoc.smallest_mtu) {
5357                                                         sctp_pathmtu_adjustment(stcb, net->mtu);
5358                                                 }
5359                                         }
5360                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5361                                                 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5362                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5363                                                 }
5364                                                 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5365                                         }
5366                                         if (paddrp->spp_pathmaxrxt) {
5367                                                 if (net->dest_state & SCTP_ADDR_PF) {
5368                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5369                                                                 net->dest_state &= ~SCTP_ADDR_PF;
5370                                                         }
5371                                                 } else {
5372                                                         if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5373                                                             (net->error_count > net->pf_threshold)) {
5374                                                                 net->dest_state |= SCTP_ADDR_PF;
5375                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5376                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5377                                                                     stcb->sctp_ep, stcb, net,
5378                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_12);
5379                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5380                                                         }
5381                                                 }
5382                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
5383                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5384                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5385                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5386                                                         }
5387                                                 } else {
5388                                                         if (net->error_count <= paddrp->spp_pathmaxrxt) {
5389                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
5390                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5391                                                         }
5392                                                 }
5393                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
5394                                         }
5395                                         if (paddrp->spp_flags & SPP_DSCP) {
5396                                                 net->dscp = paddrp->spp_dscp & 0xfc;
5397                                                 net->dscp |= 0x01;
5398                                         }
5399 #ifdef INET6
5400                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5401                                                 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5402                                                         net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5403                                                         net->flowlabel |= 0x80000000;
5404                                                 }
5405                                         }
5406 #endif
5407                                 } else {
5408                                         /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5409                                         if (paddrp->spp_pathmaxrxt != 0) {
5410                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5411                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5412                                                         if (net->dest_state & SCTP_ADDR_PF) {
5413                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5414                                                                         net->dest_state &= ~SCTP_ADDR_PF;
5415                                                                 }
5416                                                         } else {
5417                                                                 if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5418                                                                     (net->error_count > net->pf_threshold)) {
5419                                                                         net->dest_state |= SCTP_ADDR_PF;
5420                                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5421                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5422                                                                             stcb->sctp_ep, stcb, net,
5423                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_13);
5424                                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5425                                                                 }
5426                                                         }
5427                                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
5428                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5429                                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
5430                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5431                                                                 }
5432                                                         } else {
5433                                                                 if (net->error_count <= paddrp->spp_pathmaxrxt) {
5434                                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
5435                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5436                                                                 }
5437                                                         }
5438                                                         net->failure_threshold = paddrp->spp_pathmaxrxt;
5439                                                 }
5440                                         }
5441
5442                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5443                                                 if (paddrp->spp_hbinterval != 0) {
5444                                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5445                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5446                                                         stcb->asoc.heart_beat_delay = 0;
5447                                                 }
5448                                                 /* Turn back on the timer */
5449                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5450                                                         if (paddrp->spp_hbinterval != 0) {
5451                                                                 net->heart_beat_delay = paddrp->spp_hbinterval;
5452                                                         } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5453                                                                 net->heart_beat_delay = 0;
5454                                                         }
5455                                                         if (net->dest_state & SCTP_ADDR_NOHB) {
5456                                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
5457                                                         }
5458                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5459                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_14);
5460                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5461                                                 }
5462                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5463                                         }
5464                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
5465                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5466                                                         if (!(net->dest_state & SCTP_ADDR_NOHB)) {
5467                                                                 net->dest_state |= SCTP_ADDR_NOHB;
5468                                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5469                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5470                                                                             inp, stcb, net,
5471                                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_15);
5472                                                                 }
5473                                                         }
5474                                                 }
5475                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5476                                         }
5477                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5478                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5479                                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5480                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5481                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_16);
5482                                                         }
5483                                                         net->dest_state |= SCTP_ADDR_NO_PMTUD;
5484                                                         net->mtu = paddrp->spp_pathmtu;
5485                                                         switch (net->ro._l_addr.sa.sa_family) {
5486 #ifdef INET
5487                                                         case AF_INET:
5488                                                                 net->mtu += SCTP_MIN_V4_OVERHEAD;
5489                                                                 break;
5490 #endif
5491 #ifdef INET6
5492                                                         case AF_INET6:
5493                                                                 net->mtu += SCTP_MIN_OVERHEAD;
5494                                                                 break;
5495 #endif
5496                                                         default:
5497                                                                 break;
5498                                                         }
5499                                                         if (net->mtu < stcb->asoc.smallest_mtu) {
5500                                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5501                                                         }
5502                                                 }
5503                                                 stcb->asoc.default_mtu = paddrp->spp_pathmtu;
5504                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5505                                         }
5506                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5507                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5508                                                         if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5509                                                                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5510                                                         }
5511                                                         net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5512                                                 }
5513                                                 stcb->asoc.default_mtu = 0;
5514                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5515                                         }
5516                                         if (paddrp->spp_flags & SPP_DSCP) {
5517                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5518                                                         net->dscp = paddrp->spp_dscp & 0xfc;
5519                                                         net->dscp |= 0x01;
5520                                                 }
5521                                                 stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
5522                                                 stcb->asoc.default_dscp |= 0x01;
5523                                         }
5524 #ifdef INET6
5525                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5526                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5527                                                         if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5528                                                                 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5529                                                                 net->flowlabel |= 0x80000000;
5530                                                         }
5531                                                 }
5532                                                 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5533                                                 stcb->asoc.default_flowlabel |= 0x80000000;
5534                                         }
5535 #endif
5536                                 }
5537                                 SCTP_TCB_UNLOCK(stcb);
5538                         } else {
5539                                 /************************NO TCB, SET TO default stuff ******************/
5540                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5541                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5542                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
5543                                         SCTP_INP_WLOCK(inp);
5544                                         /*
5545                                          * For the TOS/FLOWLABEL stuff you
5546                                          * set it with the options on the
5547                                          * socket
5548                                          */
5549                                         if (paddrp->spp_pathmaxrxt != 0) {
5550                                                 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
5551                                         }
5552
5553                                         if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
5554                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5555                                         else if (paddrp->spp_hbinterval != 0) {
5556                                                 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
5557                                                         paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
5558                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5559                                         }
5560
5561                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
5562                                                 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5563                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
5564                                                 } else if (paddrp->spp_hbinterval) {
5565                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
5566                                                 }
5567                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5568                                         } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
5569                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
5570                                         }
5571                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5572                                                 inp->sctp_ep.default_mtu = 0;
5573                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5574                                         } else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
5575                                                 if (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU) {
5576                                                         inp->sctp_ep.default_mtu = paddrp->spp_pathmtu;
5577                                                 }
5578                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
5579                                         }
5580                                         if (paddrp->spp_flags & SPP_DSCP) {
5581                                                 inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
5582                                                 inp->sctp_ep.default_dscp |= 0x01;
5583                                         }
5584 #ifdef INET6
5585                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5586                                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5587                                                         inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5588                                                         inp->sctp_ep.default_flowlabel |= 0x80000000;
5589                                                 }
5590                                         }
5591 #endif
5592                                         SCTP_INP_WUNLOCK(inp);
5593                                 } else {
5594                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5595                                         error = EINVAL;
5596                                 }
5597                         }
5598                         break;
5599                 }
5600         case SCTP_RTOINFO:
5601                 {
5602                         struct sctp_rtoinfo *srto;
5603                         uint32_t new_init, new_min, new_max;
5604
5605                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
5606                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
5607
5608                         if (stcb) {
5609                                 if (srto->srto_initial)
5610                                         new_init = srto->srto_initial;
5611                                 else
5612                                         new_init = stcb->asoc.initial_rto;
5613                                 if (srto->srto_max)
5614                                         new_max = srto->srto_max;
5615                                 else
5616                                         new_max = stcb->asoc.maxrto;
5617                                 if (srto->srto_min)
5618                                         new_min = srto->srto_min;
5619                                 else
5620                                         new_min = stcb->asoc.minrto;
5621                                 if ((new_min <= new_init) && (new_init <= new_max)) {
5622                                         stcb->asoc.initial_rto = new_init;
5623                                         stcb->asoc.maxrto = new_max;
5624                                         stcb->asoc.minrto = new_min;
5625                                 } else {
5626                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5627                                         error = EINVAL;
5628                                 }
5629                                 SCTP_TCB_UNLOCK(stcb);
5630                         } else {
5631                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5632                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5633                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
5634                                         SCTP_INP_WLOCK(inp);
5635                                         if (srto->srto_initial)
5636                                                 new_init = srto->srto_initial;
5637                                         else
5638                                                 new_init = inp->sctp_ep.initial_rto;
5639                                         if (srto->srto_max)
5640                                                 new_max = srto->srto_max;
5641                                         else
5642                                                 new_max = inp->sctp_ep.sctp_maxrto;
5643                                         if (srto->srto_min)
5644                                                 new_min = srto->srto_min;
5645                                         else
5646                                                 new_min = inp->sctp_ep.sctp_minrto;
5647                                         if ((new_min <= new_init) && (new_init <= new_max)) {
5648                                                 inp->sctp_ep.initial_rto = new_init;
5649                                                 inp->sctp_ep.sctp_maxrto = new_max;
5650                                                 inp->sctp_ep.sctp_minrto = new_min;
5651                                         } else {
5652                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5653                                                 error = EINVAL;
5654                                         }
5655                                         SCTP_INP_WUNLOCK(inp);
5656                                 } else {
5657                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5658                                         error = EINVAL;
5659                                 }
5660                         }
5661                         break;
5662                 }
5663         case SCTP_ASSOCINFO:
5664                 {
5665                         struct sctp_assocparams *sasoc;
5666
5667                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
5668                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
5669                         if (sasoc->sasoc_cookie_life) {
5670                                 /* boundary check the cookie life */
5671                                 if (sasoc->sasoc_cookie_life < 1000)
5672                                         sasoc->sasoc_cookie_life = 1000;
5673                                 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
5674                                         sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
5675                                 }
5676                         }
5677                         if (stcb) {
5678                                 if (sasoc->sasoc_asocmaxrxt)
5679                                         stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
5680                                 if (sasoc->sasoc_cookie_life) {
5681                                         stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5682                                 }
5683                                 SCTP_TCB_UNLOCK(stcb);
5684                         } else {
5685                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5686                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5687                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
5688                                         SCTP_INP_WLOCK(inp);
5689                                         if (sasoc->sasoc_asocmaxrxt)
5690                                                 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
5691                                         if (sasoc->sasoc_cookie_life) {
5692                                                 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
5693                                         }
5694                                         SCTP_INP_WUNLOCK(inp);
5695                                 } else {
5696                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5697                                         error = EINVAL;
5698                                 }
5699                         }
5700                         break;
5701                 }
5702         case SCTP_INITMSG:
5703                 {
5704                         struct sctp_initmsg *sinit;
5705
5706                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
5707                         SCTP_INP_WLOCK(inp);
5708                         if (sinit->sinit_num_ostreams)
5709                                 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
5710
5711                         if (sinit->sinit_max_instreams)
5712                                 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
5713
5714                         if (sinit->sinit_max_attempts)
5715                                 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
5716
5717                         if (sinit->sinit_max_init_timeo)
5718                                 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
5719                         SCTP_INP_WUNLOCK(inp);
5720                         break;
5721                 }
5722         case SCTP_PRIMARY_ADDR:
5723                 {
5724                         struct sctp_setprim *spa;
5725                         struct sctp_nets *net;
5726                         struct sockaddr *addr;
5727 #if defined(INET) && defined(INET6)
5728                         struct sockaddr_in sin_store;
5729 #endif
5730
5731                         SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5732                         SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5733
5734 #if defined(INET) && defined(INET6)
5735                         if (spa->ssp_addr.ss_family == AF_INET6) {
5736                                 struct sockaddr_in6 *sin6;
5737
5738                                 sin6 = (struct sockaddr_in6 *)&spa->ssp_addr;
5739                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5740                                         in6_sin6_2_sin(&sin_store, sin6);
5741                                         addr = (struct sockaddr *)&sin_store;
5742                                 } else {
5743                                         addr = (struct sockaddr *)&spa->ssp_addr;
5744                                 }
5745                         } else {
5746                                 addr = (struct sockaddr *)&spa->ssp_addr;
5747                         }
5748 #else
5749                         addr = (struct sockaddr *)&spa->ssp_addr;
5750 #endif
5751                         if (stcb != NULL) {
5752                                 net = sctp_findnet(stcb, addr);
5753                         } else {
5754                                 /*
5755                                  * We increment here since
5756                                  * sctp_findassociation_ep_addr() wil do a
5757                                  * decrement if it finds the stcb as long as
5758                                  * the locked tcb (last argument) is NOT a
5759                                  * TCB.. aka NULL.
5760                                  */
5761                                 net = NULL;
5762                                 SCTP_INP_INCR_REF(inp);
5763                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
5764                                     &net, NULL, NULL);
5765                                 if (stcb == NULL) {
5766                                         SCTP_INP_DECR_REF(inp);
5767                                 }
5768                         }
5769
5770                         if ((stcb != NULL) && (net != NULL)) {
5771                                 if (net != stcb->asoc.primary_destination) {
5772                                         if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
5773                                                 /* Ok we need to set it */
5774                                                 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5775                                                         if ((stcb->asoc.alternate) &&
5776                                                             (!(net->dest_state & SCTP_ADDR_PF)) &&
5777                                                             (net->dest_state & SCTP_ADDR_REACHABLE)) {
5778                                                                 sctp_free_remote_addr(stcb->asoc.alternate);
5779                                                                 stcb->asoc.alternate = NULL;
5780                                                         }
5781                                                 } else {
5782                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5783                                                         error = EINVAL;
5784                                                 }
5785                                         } else {
5786                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5787                                                 error = EINVAL;
5788                                         }
5789                                 }
5790                         } else {
5791                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5792                                 error = EINVAL;
5793                         }
5794                         if (stcb != NULL) {
5795                                 SCTP_TCB_UNLOCK(stcb);
5796                         }
5797                         break;
5798                 }
5799         case SCTP_SET_DYNAMIC_PRIMARY:
5800                 {
5801                         union sctp_sockstore *ss;
5802
5803                         error = priv_check(curthread,
5804                             PRIV_NETINET_RESERVEDPORT);
5805                         if (error)
5806                                 break;
5807
5808                         SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5809                         /* SUPER USER CHECK? */
5810                         error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5811                         break;
5812                 }
5813         case SCTP_SET_PEER_PRIMARY_ADDR:
5814                 {
5815                         struct sctp_setpeerprim *sspp;
5816                         struct sockaddr *addr;
5817 #if defined(INET) && defined(INET6)
5818                         struct sockaddr_in sin_store;
5819 #endif
5820
5821                         SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5822                         SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5823                         if (stcb != NULL) {
5824                                 struct sctp_ifa *ifa;
5825
5826 #if defined(INET) && defined(INET6)
5827                                 if (sspp->sspp_addr.ss_family == AF_INET6) {
5828                                         struct sockaddr_in6 *sin6;
5829
5830                                         sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
5831                                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5832                                                 in6_sin6_2_sin(&sin_store, sin6);
5833                                                 addr = (struct sockaddr *)&sin_store;
5834                                         } else {
5835                                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5836                                         }
5837                                 } else {
5838                                         addr = (struct sockaddr *)&sspp->sspp_addr;
5839                                 }
5840 #else
5841                                 addr = (struct sockaddr *)&sspp->sspp_addr;
5842 #endif
5843                                 ifa = sctp_find_ifa_by_addr(addr, stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5844                                 if (ifa == NULL) {
5845                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5846                                         error = EINVAL;
5847                                         goto out_of_it;
5848                                 }
5849                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5850                                         /*
5851                                          * Must validate the ifa found is in
5852                                          * our ep
5853                                          */
5854                                         struct sctp_laddr *laddr;
5855                                         int found = 0;
5856
5857                                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5858                                                 if (laddr->ifa == NULL) {
5859                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5860                                                             __func__);
5861                                                         continue;
5862                                                 }
5863                                                 if ((sctp_is_addr_restricted(stcb, laddr->ifa)) &&
5864                                                     (!sctp_is_addr_pending(stcb, laddr->ifa))) {
5865                                                         continue;
5866                                                 }
5867                                                 if (laddr->ifa == ifa) {
5868                                                         found = 1;
5869                                                         break;
5870                                                 }
5871                                         }
5872                                         if (!found) {
5873                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5874                                                 error = EINVAL;
5875                                                 goto out_of_it;
5876                                         }
5877                                 } else {
5878                                         switch (addr->sa_family) {
5879 #ifdef INET
5880                                         case AF_INET:
5881                                                 {
5882                                                         struct sockaddr_in *sin;
5883
5884                                                         sin = (struct sockaddr_in *)addr;
5885                                                         if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
5886                                                             &sin->sin_addr) != 0) {
5887                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5888                                                                 error = EINVAL;
5889                                                                 goto out_of_it;
5890                                                         }
5891                                                         break;
5892                                                 }
5893 #endif
5894 #ifdef INET6
5895                                         case AF_INET6:
5896                                                 {
5897                                                         struct sockaddr_in6 *sin6;
5898
5899                                                         sin6 = (struct sockaddr_in6 *)addr;
5900                                                         if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
5901                                                             &sin6->sin6_addr) != 0) {
5902                                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5903                                                                 error = EINVAL;
5904                                                                 goto out_of_it;
5905                                                         }
5906                                                         break;
5907                                                 }
5908 #endif
5909                                         default:
5910                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5911                                                 error = EINVAL;
5912                                                 goto out_of_it;
5913                                         }
5914                                 }
5915                                 if (sctp_set_primary_ip_address_sa(stcb, addr) != 0) {
5916                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5917                                         error = EINVAL;
5918                                 }
5919                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5920                 out_of_it:
5921                                 SCTP_TCB_UNLOCK(stcb);
5922                         } else {
5923                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5924                                 error = EINVAL;
5925                         }
5926                         break;
5927                 }
5928         case SCTP_BINDX_ADD_ADDR:
5929                 {
5930                         struct sctp_getaddresses *addrs;
5931                         struct thread *td;
5932
5933                         td = (struct thread *)p;
5934                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5935                             optsize);
5936 #ifdef INET
5937                         if (addrs->addr->sa_family == AF_INET) {
5938                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5939                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5940                                         error = EINVAL;
5941                                         break;
5942                                 }
5943                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5944                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5945                                         break;
5946                                 }
5947                         } else
5948 #endif
5949 #ifdef INET6
5950                         if (addrs->addr->sa_family == AF_INET6) {
5951                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5952                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5953                                         error = EINVAL;
5954                                         break;
5955                                 }
5956                                 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5957                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5958                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5959                                         break;
5960                                 }
5961                         } else
5962 #endif
5963                         {
5964                                 error = EAFNOSUPPORT;
5965                                 break;
5966                         }
5967                         sctp_bindx_add_address(so, inp, addrs->addr,
5968                             addrs->sget_assoc_id, vrf_id,
5969                             &error, p);
5970                         break;
5971                 }
5972         case SCTP_BINDX_REM_ADDR:
5973                 {
5974                         struct sctp_getaddresses *addrs;
5975                         struct thread *td;
5976
5977                         td = (struct thread *)p;
5978
5979                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5980 #ifdef INET
5981                         if (addrs->addr->sa_family == AF_INET) {
5982                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
5983                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5984                                         error = EINVAL;
5985                                         break;
5986                                 }
5987                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5988                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5989                                         break;
5990                                 }
5991                         } else
5992 #endif
5993 #ifdef INET6
5994                         if (addrs->addr->sa_family == AF_INET6) {
5995                                 if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
5996                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5997                                         error = EINVAL;
5998                                         break;
5999                                 }
6000                                 if (td != NULL &&
6001                                     (error = prison_local_ip6(td->td_ucred,
6002                                     &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
6003                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
6004                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6005                                         break;
6006                                 }
6007                         } else
6008 #endif
6009                         {
6010                                 error = EAFNOSUPPORT;
6011                                 break;
6012                         }
6013                         sctp_bindx_delete_address(inp, addrs->addr,
6014                             addrs->sget_assoc_id, vrf_id,
6015                             &error);
6016                         break;
6017                 }
6018         case SCTP_EVENT:
6019                 {
6020                         struct sctp_event *event;
6021                         uint32_t event_type;
6022
6023                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
6024                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
6025                         switch (event->se_type) {
6026                         case SCTP_ASSOC_CHANGE:
6027                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
6028                                 break;
6029                         case SCTP_PEER_ADDR_CHANGE:
6030                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
6031                                 break;
6032                         case SCTP_REMOTE_ERROR:
6033                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
6034                                 break;
6035                         case SCTP_SEND_FAILED:
6036                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
6037                                 break;
6038                         case SCTP_SHUTDOWN_EVENT:
6039                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
6040                                 break;
6041                         case SCTP_ADAPTATION_INDICATION:
6042                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
6043                                 break;
6044                         case SCTP_PARTIAL_DELIVERY_EVENT:
6045                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
6046                                 break;
6047                         case SCTP_AUTHENTICATION_EVENT:
6048                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
6049                                 break;
6050                         case SCTP_STREAM_RESET_EVENT:
6051                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
6052                                 break;
6053                         case SCTP_SENDER_DRY_EVENT:
6054                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
6055                                 break;
6056                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
6057                                 event_type = 0;
6058                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6059                                 error = ENOTSUP;
6060                                 break;
6061                         case SCTP_ASSOC_RESET_EVENT:
6062                                 event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
6063                                 break;
6064                         case SCTP_STREAM_CHANGE_EVENT:
6065                                 event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
6066                                 break;
6067                         case SCTP_SEND_FAILED_EVENT:
6068                                 event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
6069                                 break;
6070                         default:
6071                                 event_type = 0;
6072                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6073                                 error = EINVAL;
6074                                 break;
6075                         }
6076                         if (event_type > 0) {
6077                                 if (stcb) {
6078                                         if (event->se_on) {
6079                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6080                                                 if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6081                                                         if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6082                                                             TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6083                                                             (stcb->asoc.stream_queue_cnt == 0)) {
6084                                                                 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
6085                                                         }
6086                                                 }
6087                                         } else {
6088                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6089                                         }
6090                                         SCTP_TCB_UNLOCK(stcb);
6091                                 } else {
6092                                         /*
6093                                          * We don't want to send up a storm
6094                                          * of events, so return an error for
6095                                          * sender dry events
6096                                          */
6097                                         if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6098                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6099                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6100                                             ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6101                                             (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6102                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6103                                                 error = ENOTSUP;
6104                                                 break;
6105                                         }
6106                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6107                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6108                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6109                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6110                                                 SCTP_INP_WLOCK(inp);
6111                                                 if (event->se_on) {
6112                                                         sctp_feature_on(inp, event_type);
6113                                                 } else {
6114                                                         sctp_feature_off(inp, event_type);
6115                                                 }
6116                                                 SCTP_INP_WUNLOCK(inp);
6117                                         }
6118                                         if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6119                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6120                                                 SCTP_INP_RLOCK(inp);
6121                                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6122                                                         SCTP_TCB_LOCK(stcb);
6123                                                         if (event->se_on) {
6124                                                                 sctp_stcb_feature_on(inp, stcb, event_type);
6125                                                         } else {
6126                                                                 sctp_stcb_feature_off(inp, stcb, event_type);
6127                                                         }
6128                                                         SCTP_TCB_UNLOCK(stcb);
6129                                                 }
6130                                                 SCTP_INP_RUNLOCK(inp);
6131                                         }
6132                                 }
6133                         } else {
6134                                 if (stcb) {
6135                                         SCTP_TCB_UNLOCK(stcb);
6136                                 }
6137                         }
6138                         break;
6139                 }
6140         case SCTP_RECVRCVINFO:
6141                 {
6142                         int *onoff;
6143
6144                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6145                         SCTP_INP_WLOCK(inp);
6146                         if (*onoff != 0) {
6147                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6148                         } else {
6149                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6150                         }
6151                         SCTP_INP_WUNLOCK(inp);
6152                         break;
6153                 }
6154         case SCTP_RECVNXTINFO:
6155                 {
6156                         int *onoff;
6157
6158                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6159                         SCTP_INP_WLOCK(inp);
6160                         if (*onoff != 0) {
6161                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6162                         } else {
6163                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6164                         }
6165                         SCTP_INP_WUNLOCK(inp);
6166                         break;
6167                 }
6168         case SCTP_DEFAULT_SNDINFO:
6169                 {
6170                         struct sctp_sndinfo *info;
6171                         uint16_t policy;
6172
6173                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6174                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6175
6176                         if (stcb) {
6177                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6178                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6179                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6180                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6181                                         stcb->asoc.def_send.sinfo_flags |= policy;
6182                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6183                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6184                                 } else {
6185                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6186                                         error = EINVAL;
6187                                 }
6188                                 SCTP_TCB_UNLOCK(stcb);
6189                         } else {
6190                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6191                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6192                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6193                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6194                                         SCTP_INP_WLOCK(inp);
6195                                         inp->def_send.sinfo_stream = info->snd_sid;
6196                                         policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6197                                         inp->def_send.sinfo_flags = info->snd_flags;
6198                                         inp->def_send.sinfo_flags |= policy;
6199                                         inp->def_send.sinfo_ppid = info->snd_ppid;
6200                                         inp->def_send.sinfo_context = info->snd_context;
6201                                         SCTP_INP_WUNLOCK(inp);
6202                                 }
6203                                 if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6204                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6205                                         SCTP_INP_RLOCK(inp);
6206                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6207                                                 SCTP_TCB_LOCK(stcb);
6208                                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
6209                                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6210                                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6211                                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6212                                                         stcb->asoc.def_send.sinfo_flags |= policy;
6213                                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6214                                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
6215                                                 }
6216                                                 SCTP_TCB_UNLOCK(stcb);
6217                                         }
6218                                         SCTP_INP_RUNLOCK(inp);
6219                                 }
6220                         }
6221                         break;
6222                 }
6223         case SCTP_DEFAULT_PRINFO:
6224                 {
6225                         struct sctp_default_prinfo *info;
6226
6227                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6228                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6229
6230                         if (info->pr_policy > SCTP_PR_SCTP_MAX) {
6231                                 if (stcb) {
6232                                         SCTP_TCB_UNLOCK(stcb);
6233                                 }
6234                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6235                                 error = EINVAL;
6236                                 break;
6237                         }
6238                         if (stcb) {
6239                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6240                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6241                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6242                                 SCTP_TCB_UNLOCK(stcb);
6243                         } else {
6244                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6245                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6246                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6247                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6248                                         SCTP_INP_WLOCK(inp);
6249                                         inp->def_send.sinfo_flags &= 0xfff0;
6250                                         inp->def_send.sinfo_flags |= info->pr_policy;
6251                                         inp->def_send.sinfo_timetolive = info->pr_value;
6252                                         SCTP_INP_WUNLOCK(inp);
6253                                 }
6254                                 if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6255                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6256                                         SCTP_INP_RLOCK(inp);
6257                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6258                                                 SCTP_TCB_LOCK(stcb);
6259                                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6260                                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6261                                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6262                                                 SCTP_TCB_UNLOCK(stcb);
6263                                         }
6264                                         SCTP_INP_RUNLOCK(inp);
6265                                 }
6266                         }
6267                         break;
6268                 }
6269         case SCTP_PEER_ADDR_THLDS:
6270                 /* Applies to the specific association */
6271                 {
6272                         struct sctp_paddrthlds *thlds;
6273                         struct sctp_nets *net;
6274                         struct sockaddr *addr;
6275 #if defined(INET) && defined(INET6)
6276                         struct sockaddr_in sin_store;
6277 #endif
6278
6279                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6280                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6281
6282 #if defined(INET) && defined(INET6)
6283                         if (thlds->spt_address.ss_family == AF_INET6) {
6284                                 struct sockaddr_in6 *sin6;
6285
6286                                 sin6 = (struct sockaddr_in6 *)&thlds->spt_address;
6287                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6288                                         in6_sin6_2_sin(&sin_store, sin6);
6289                                         addr = (struct sockaddr *)&sin_store;
6290                                 } else {
6291                                         addr = (struct sockaddr *)&thlds->spt_address;
6292                                 }
6293                         } else {
6294                                 addr = (struct sockaddr *)&thlds->spt_address;
6295                         }
6296 #else
6297                         addr = (struct sockaddr *)&thlds->spt_address;
6298 #endif
6299                         if (stcb != NULL) {
6300                                 net = sctp_findnet(stcb, addr);
6301                         } else {
6302                                 /*
6303                                  * We increment here since
6304                                  * sctp_findassociation_ep_addr() wil do a
6305                                  * decrement if it finds the stcb as long as
6306                                  * the locked tcb (last argument) is NOT a
6307                                  * TCB.. aka NULL.
6308                                  */
6309                                 net = NULL;
6310                                 SCTP_INP_INCR_REF(inp);
6311                                 stcb = sctp_findassociation_ep_addr(&inp, addr,
6312                                     &net, NULL, NULL);
6313                                 if (stcb == NULL) {
6314                                         SCTP_INP_DECR_REF(inp);
6315                                 }
6316                         }
6317                         if ((stcb != NULL) && (net == NULL)) {
6318 #ifdef INET
6319                                 if (addr->sa_family == AF_INET) {
6320
6321                                         struct sockaddr_in *sin;
6322
6323                                         sin = (struct sockaddr_in *)addr;
6324                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6325                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6326                                                 SCTP_TCB_UNLOCK(stcb);
6327                                                 error = EINVAL;
6328                                                 break;
6329                                         }
6330                                 } else
6331 #endif
6332 #ifdef INET6
6333                                 if (addr->sa_family == AF_INET6) {
6334                                         struct sockaddr_in6 *sin6;
6335
6336                                         sin6 = (struct sockaddr_in6 *)addr;
6337                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6338                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6339                                                 SCTP_TCB_UNLOCK(stcb);
6340                                                 error = EINVAL;
6341                                                 break;
6342                                         }
6343                                 } else
6344 #endif
6345                                 {
6346                                         error = EAFNOSUPPORT;
6347                                         SCTP_TCB_UNLOCK(stcb);
6348                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6349                                         break;
6350                                 }
6351                         }
6352                         if (thlds->spt_pathcpthld != 0xffff) {
6353                                 if (stcb != NULL) {
6354                                         SCTP_TCB_UNLOCK(stcb);
6355                                 }
6356                                 error = EINVAL;
6357                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6358                                 break;
6359                         }
6360                         if (stcb != NULL) {
6361                                 if (net != NULL) {
6362                                         net->failure_threshold = thlds->spt_pathmaxrxt;
6363                                         net->pf_threshold = thlds->spt_pathpfthld;
6364                                         if (net->dest_state & SCTP_ADDR_PF) {
6365                                                 if ((net->error_count > net->failure_threshold) ||
6366                                                     (net->error_count <= net->pf_threshold)) {
6367                                                         net->dest_state &= ~SCTP_ADDR_PF;
6368                                                 }
6369                                         } else {
6370                                                 if ((net->error_count > net->pf_threshold) &&
6371                                                     (net->error_count <= net->failure_threshold)) {
6372                                                         net->dest_state |= SCTP_ADDR_PF;
6373                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6374                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6375                                                             stcb->sctp_ep, stcb, net,
6376                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_17);
6377                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6378                                                 }
6379                                         }
6380                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
6381                                                 if (net->error_count > net->failure_threshold) {
6382                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
6383                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6384                                                 }
6385                                         } else {
6386                                                 if (net->error_count <= net->failure_threshold) {
6387                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
6388                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6389                                                 }
6390                                         }
6391                                 } else {
6392                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6393                                                 net->failure_threshold = thlds->spt_pathmaxrxt;
6394                                                 net->pf_threshold = thlds->spt_pathpfthld;
6395                                                 if (net->dest_state & SCTP_ADDR_PF) {
6396                                                         if ((net->error_count > net->failure_threshold) ||
6397                                                             (net->error_count <= net->pf_threshold)) {
6398                                                                 net->dest_state &= ~SCTP_ADDR_PF;
6399                                                         }
6400                                                 } else {
6401                                                         if ((net->error_count > net->pf_threshold) &&
6402                                                             (net->error_count <= net->failure_threshold)) {
6403                                                                 net->dest_state |= SCTP_ADDR_PF;
6404                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6405                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
6406                                                                     stcb->sctp_ep, stcb, net,
6407                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_18);
6408                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6409                                                         }
6410                                                 }
6411                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
6412                                                         if (net->error_count > net->failure_threshold) {
6413                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
6414                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6415                                                         }
6416                                                 } else {
6417                                                         if (net->error_count <= net->failure_threshold) {
6418                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
6419                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6420                                                         }
6421                                                 }
6422                                         }
6423                                         stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6424                                         stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6425                                 }
6426                                 SCTP_TCB_UNLOCK(stcb);
6427                         } else {
6428                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6429                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6430                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6431                                         SCTP_INP_WLOCK(inp);
6432                                         inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6433                                         inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6434                                         SCTP_INP_WUNLOCK(inp);
6435                                 } else {
6436                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6437                                         error = EINVAL;
6438                                 }
6439                         }
6440                         break;
6441                 }
6442         case SCTP_REMOTE_UDP_ENCAPS_PORT:
6443                 {
6444                         struct sctp_udpencaps *encaps;
6445                         struct sctp_nets *net;
6446                         struct sockaddr *addr;
6447 #if defined(INET) && defined(INET6)
6448                         struct sockaddr_in sin_store;
6449 #endif
6450
6451                         SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6452                         SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6453
6454 #if defined(INET) && defined(INET6)
6455                         if (encaps->sue_address.ss_family == AF_INET6) {
6456                                 struct sockaddr_in6 *sin6;
6457
6458                                 sin6 = (struct sockaddr_in6 *)&encaps->sue_address;
6459                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
6460                                         in6_sin6_2_sin(&sin_store, sin6);
6461                                         addr = (struct sockaddr *)&sin_store;
6462                                 } else {
6463                                         addr = (struct sockaddr *)&encaps->sue_address;
6464                                 }
6465                         } else {
6466                                 addr = (struct sockaddr *)&encaps->sue_address;
6467                         }
6468 #else
6469                         addr = (struct sockaddr *)&encaps->sue_address;
6470 #endif
6471                         if (stcb != NULL) {
6472                                 net = sctp_findnet(stcb, addr);
6473                         } else {
6474                                 /*
6475                                  * We increment here since
6476                                  * sctp_findassociation_ep_addr() wil do a
6477                                  * decrement if it finds the stcb as long as
6478                                  * the locked tcb (last argument) is NOT a
6479                                  * TCB.. aka NULL.
6480                                  */
6481                                 net = NULL;
6482                                 SCTP_INP_INCR_REF(inp);
6483                                 stcb = sctp_findassociation_ep_addr(&inp, addr, &net, NULL, NULL);
6484                                 if (stcb == NULL) {
6485                                         SCTP_INP_DECR_REF(inp);
6486                                 }
6487                         }
6488                         if ((stcb != NULL) && (net == NULL)) {
6489 #ifdef INET
6490                                 if (addr->sa_family == AF_INET) {
6491
6492                                         struct sockaddr_in *sin;
6493
6494                                         sin = (struct sockaddr_in *)addr;
6495                                         if (sin->sin_addr.s_addr != INADDR_ANY) {
6496                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6497                                                 SCTP_TCB_UNLOCK(stcb);
6498                                                 error = EINVAL;
6499                                                 break;
6500                                         }
6501                                 } else
6502 #endif
6503 #ifdef INET6
6504                                 if (addr->sa_family == AF_INET6) {
6505                                         struct sockaddr_in6 *sin6;
6506
6507                                         sin6 = (struct sockaddr_in6 *)addr;
6508                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6509                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6510                                                 SCTP_TCB_UNLOCK(stcb);
6511                                                 error = EINVAL;
6512                                                 break;
6513                                         }
6514                                 } else
6515 #endif
6516                                 {
6517                                         error = EAFNOSUPPORT;
6518                                         SCTP_TCB_UNLOCK(stcb);
6519                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6520                                         break;
6521                                 }
6522                         }
6523
6524                         if (stcb != NULL) {
6525                                 if (net != NULL) {
6526                                         net->port = encaps->sue_port;
6527                                 } else {
6528                                         stcb->asoc.port = encaps->sue_port;
6529                                 }
6530                                 SCTP_TCB_UNLOCK(stcb);
6531                         } else {
6532                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6533                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6534                                     (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
6535                                         SCTP_INP_WLOCK(inp);
6536                                         inp->sctp_ep.port = encaps->sue_port;
6537                                         SCTP_INP_WUNLOCK(inp);
6538                                 } else {
6539                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6540                                         error = EINVAL;
6541                                 }
6542                         }
6543                         break;
6544                 }
6545         case SCTP_ECN_SUPPORTED:
6546                 {
6547                         struct sctp_assoc_value *av;
6548
6549                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6550                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6551
6552                         if (stcb) {
6553                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6554                                 error = EINVAL;
6555                                 SCTP_TCB_UNLOCK(stcb);
6556                         } else {
6557                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6558                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6559                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6560                                         SCTP_INP_WLOCK(inp);
6561                                         if (av->assoc_value == 0) {
6562                                                 inp->ecn_supported = 0;
6563                                         } else {
6564                                                 inp->ecn_supported = 1;
6565                                         }
6566                                         SCTP_INP_WUNLOCK(inp);
6567                                 } else {
6568                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6569                                         error = EINVAL;
6570                                 }
6571                         }
6572                         break;
6573                 }
6574         case SCTP_PR_SUPPORTED:
6575                 {
6576                         struct sctp_assoc_value *av;
6577
6578                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6579                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6580
6581                         if (stcb) {
6582                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6583                                 error = EINVAL;
6584                                 SCTP_TCB_UNLOCK(stcb);
6585                         } else {
6586                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6587                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6588                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6589                                         SCTP_INP_WLOCK(inp);
6590                                         if (av->assoc_value == 0) {
6591                                                 inp->prsctp_supported = 0;
6592                                         } else {
6593                                                 inp->prsctp_supported = 1;
6594                                         }
6595                                         SCTP_INP_WUNLOCK(inp);
6596                                 } else {
6597                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6598                                         error = EINVAL;
6599                                 }
6600                         }
6601                         break;
6602                 }
6603         case SCTP_AUTH_SUPPORTED:
6604                 {
6605                         struct sctp_assoc_value *av;
6606
6607                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6608                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6609
6610                         if (stcb) {
6611                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6612                                 error = EINVAL;
6613                                 SCTP_TCB_UNLOCK(stcb);
6614                         } else {
6615                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6616                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6617                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6618                                         if ((av->assoc_value == 0) &&
6619                                             (inp->asconf_supported == 1)) {
6620                                                 /*
6621                                                  * AUTH is required for
6622                                                  * ASCONF
6623                                                  */
6624                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6625                                                 error = EINVAL;
6626                                         } else {
6627                                                 SCTP_INP_WLOCK(inp);
6628                                                 if (av->assoc_value == 0) {
6629                                                         inp->auth_supported = 0;
6630                                                 } else {
6631                                                         inp->auth_supported = 1;
6632                                                 }
6633                                                 SCTP_INP_WUNLOCK(inp);
6634                                         }
6635                                 } else {
6636                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6637                                         error = EINVAL;
6638                                 }
6639                         }
6640                         break;
6641                 }
6642         case SCTP_ASCONF_SUPPORTED:
6643                 {
6644                         struct sctp_assoc_value *av;
6645
6646                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6647                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6648
6649                         if (stcb) {
6650                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6651                                 error = EINVAL;
6652                                 SCTP_TCB_UNLOCK(stcb);
6653                         } else {
6654                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6655                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6656                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6657                                         if ((av->assoc_value != 0) &&
6658                                             (inp->auth_supported == 0)) {
6659                                                 /*
6660                                                  * AUTH is required for
6661                                                  * ASCONF
6662                                                  */
6663                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6664                                                 error = EINVAL;
6665                                         } else {
6666                                                 SCTP_INP_WLOCK(inp);
6667                                                 if (av->assoc_value == 0) {
6668                                                         inp->asconf_supported = 0;
6669                                                         sctp_auth_delete_chunk(SCTP_ASCONF,
6670                                                             inp->sctp_ep.local_auth_chunks);
6671                                                         sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
6672                                                             inp->sctp_ep.local_auth_chunks);
6673                                                 } else {
6674                                                         inp->asconf_supported = 1;
6675                                                         sctp_auth_add_chunk(SCTP_ASCONF,
6676                                                             inp->sctp_ep.local_auth_chunks);
6677                                                         sctp_auth_add_chunk(SCTP_ASCONF_ACK,
6678                                                             inp->sctp_ep.local_auth_chunks);
6679                                                 }
6680                                                 SCTP_INP_WUNLOCK(inp);
6681                                         }
6682                                 } else {
6683                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6684                                         error = EINVAL;
6685                                 }
6686                         }
6687                         break;
6688                 }
6689         case SCTP_RECONFIG_SUPPORTED:
6690                 {
6691                         struct sctp_assoc_value *av;
6692
6693                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6694                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6695
6696                         if (stcb) {
6697                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6698                                 error = EINVAL;
6699                                 SCTP_TCB_UNLOCK(stcb);
6700                         } else {
6701                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6702                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6703                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6704                                         SCTP_INP_WLOCK(inp);
6705                                         if (av->assoc_value == 0) {
6706                                                 inp->reconfig_supported = 0;
6707                                         } else {
6708                                                 inp->reconfig_supported = 1;
6709                                         }
6710                                         SCTP_INP_WUNLOCK(inp);
6711                                 } else {
6712                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6713                                         error = EINVAL;
6714                                 }
6715                         }
6716                         break;
6717                 }
6718         case SCTP_NRSACK_SUPPORTED:
6719                 {
6720                         struct sctp_assoc_value *av;
6721
6722                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6723                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6724
6725                         if (stcb) {
6726                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6727                                 error = EINVAL;
6728                                 SCTP_TCB_UNLOCK(stcb);
6729                         } else {
6730                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6731                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6732                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6733                                         SCTP_INP_WLOCK(inp);
6734                                         if (av->assoc_value == 0) {
6735                                                 inp->nrsack_supported = 0;
6736                                         } else {
6737                                                 inp->nrsack_supported = 1;
6738                                         }
6739                                         SCTP_INP_WUNLOCK(inp);
6740                                 } else {
6741                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6742                                         error = EINVAL;
6743                                 }
6744                         }
6745                         break;
6746                 }
6747         case SCTP_PKTDROP_SUPPORTED:
6748                 {
6749                         struct sctp_assoc_value *av;
6750
6751                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6752                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6753
6754                         if (stcb) {
6755                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6756                                 error = EINVAL;
6757                                 SCTP_TCB_UNLOCK(stcb);
6758                         } else {
6759                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6760                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6761                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6762                                         SCTP_INP_WLOCK(inp);
6763                                         if (av->assoc_value == 0) {
6764                                                 inp->pktdrop_supported = 0;
6765                                         } else {
6766                                                 inp->pktdrop_supported = 1;
6767                                         }
6768                                         SCTP_INP_WUNLOCK(inp);
6769                                 } else {
6770                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6771                                         error = EINVAL;
6772                                 }
6773                         }
6774                         break;
6775                 }
6776         case SCTP_MAX_CWND:
6777                 {
6778                         struct sctp_assoc_value *av;
6779                         struct sctp_nets *net;
6780
6781                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
6782                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
6783
6784                         if (stcb) {
6785                                 stcb->asoc.max_cwnd = av->assoc_value;
6786                                 if (stcb->asoc.max_cwnd > 0) {
6787                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6788                                                 if ((net->cwnd > stcb->asoc.max_cwnd) &&
6789                                                     (net->cwnd > (net->mtu - sizeof(struct sctphdr)))) {
6790                                                         net->cwnd = stcb->asoc.max_cwnd;
6791                                                         if (net->cwnd < (net->mtu - sizeof(struct sctphdr))) {
6792                                                                 net->cwnd = net->mtu - sizeof(struct sctphdr);
6793                                                         }
6794                                                 }
6795                                         }
6796                                 }
6797                                 SCTP_TCB_UNLOCK(stcb);
6798                         } else {
6799                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6800                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6801                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
6802                                         SCTP_INP_WLOCK(inp);
6803                                         inp->max_cwnd = av->assoc_value;
6804                                         SCTP_INP_WUNLOCK(inp);
6805                                 } else {
6806                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6807                                         error = EINVAL;
6808                                 }
6809                         }
6810                         break;
6811                 }
6812         default:
6813                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
6814                 error = ENOPROTOOPT;
6815                 break;
6816         }                       /* end switch (opt) */
6817         return (error);
6818 }
6819
6820 int
6821 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
6822 {
6823         void *optval = NULL;
6824         size_t optsize = 0;
6825         void *p;
6826         int error = 0;
6827         struct sctp_inpcb *inp;
6828
6829         if ((sopt->sopt_level == SOL_SOCKET) &&
6830             (sopt->sopt_name == SO_SETFIB)) {
6831                 inp = (struct sctp_inpcb *)so->so_pcb;
6832                 if (inp == NULL) {
6833                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6834                         return (EINVAL);
6835                 }
6836                 SCTP_INP_WLOCK(inp);
6837                 inp->fibnum = so->so_fibnum;
6838                 SCTP_INP_WUNLOCK(inp);
6839                 return (0);
6840         }
6841         if (sopt->sopt_level != IPPROTO_SCTP) {
6842                 /* wrong proto level... send back up to IP */
6843 #ifdef INET6
6844                 if (INP_CHECK_SOCKAF(so, AF_INET6))
6845                         error = ip6_ctloutput(so, sopt);
6846 #endif                          /* INET6 */
6847 #if defined(INET) && defined(INET6)
6848                 else
6849 #endif
6850 #ifdef INET
6851                         error = ip_ctloutput(so, sopt);
6852 #endif
6853                 return (error);
6854         }
6855         optsize = sopt->sopt_valsize;
6856         if (optsize > SCTP_SOCKET_OPTION_LIMIT) {
6857                 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6858                 return (ENOBUFS);
6859         }
6860         if (optsize) {
6861                 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
6862                 if (optval == NULL) {
6863                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
6864                         return (ENOBUFS);
6865                 }
6866                 error = sooptcopyin(sopt, optval, optsize, optsize);
6867                 if (error) {
6868                         SCTP_FREE(optval, SCTP_M_SOCKOPT);
6869                         goto out;
6870                 }
6871         }
6872         p = (void *)sopt->sopt_td;
6873         if (sopt->sopt_dir == SOPT_SET) {
6874                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
6875         } else if (sopt->sopt_dir == SOPT_GET) {
6876                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
6877         } else {
6878                 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6879                 error = EINVAL;
6880         }
6881         if ((error == 0) && (optval != NULL)) {
6882                 error = sooptcopyout(sopt, optval, optsize);
6883                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6884         } else if (optval != NULL) {
6885                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
6886         }
6887 out:
6888         return (error);
6889 }
6890
6891 #ifdef INET
6892 static int
6893 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
6894 {
6895         int error = 0;
6896         int create_lock_on = 0;
6897         uint32_t vrf_id;
6898         struct sctp_inpcb *inp;
6899         struct sctp_tcb *stcb = NULL;
6900
6901         inp = (struct sctp_inpcb *)so->so_pcb;
6902         if (inp == NULL) {
6903                 /* I made the same as TCP since we are not setup? */
6904                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6905                 return (ECONNRESET);
6906         }
6907         if (addr == NULL) {
6908                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6909                 return EINVAL;
6910         }
6911
6912         switch (addr->sa_family) {
6913 #ifdef INET6
6914         case AF_INET6:
6915                 {
6916                         struct sockaddr_in6 *sin6;
6917
6918                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
6919                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6920                                 return (EINVAL);
6921                         }
6922                         sin6 = (struct sockaddr_in6 *)addr;
6923                         if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6->sin6_addr)) != 0) {
6924                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6925                                 return (error);
6926                         }
6927                         break;
6928                 }
6929 #endif
6930 #ifdef INET
6931         case AF_INET:
6932                 {
6933                         struct sockaddr_in *sin;
6934
6935                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
6936                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6937                                 return (EINVAL);
6938                         }
6939                         sin = (struct sockaddr_in *)addr;
6940                         if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
6941                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6942                                 return (error);
6943                         }
6944                         break;
6945                 }
6946 #endif
6947         default:
6948                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
6949                 return (EAFNOSUPPORT);
6950         }
6951         SCTP_INP_INCR_REF(inp);
6952         SCTP_ASOC_CREATE_LOCK(inp);
6953         create_lock_on = 1;
6954
6955
6956         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
6957             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
6958                 /* Should I really unlock ? */
6959                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
6960                 error = EFAULT;
6961                 goto out_now;
6962         }
6963 #ifdef INET6
6964         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
6965             (addr->sa_family == AF_INET6)) {
6966                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6967                 error = EINVAL;
6968                 goto out_now;
6969         }
6970 #endif
6971         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
6972             SCTP_PCB_FLAGS_UNBOUND) {
6973                 /* Bind a ephemeral port */
6974                 error = sctp_inpcb_bind(so, NULL, NULL, p);
6975                 if (error) {
6976                         goto out_now;
6977                 }
6978         }
6979         /* Now do we connect? */
6980         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
6981             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
6982                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6983                 error = EINVAL;
6984                 goto out_now;
6985         }
6986         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
6987             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
6988                 /* We are already connected AND the TCP model */
6989                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
6990                 error = EADDRINUSE;
6991                 goto out_now;
6992         }
6993         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6994                 SCTP_INP_RLOCK(inp);
6995                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
6996                 SCTP_INP_RUNLOCK(inp);
6997         } else {
6998                 /*
6999                  * We increment here since sctp_findassociation_ep_addr()
7000                  * will do a decrement if it finds the stcb as long as the
7001                  * locked tcb (last argument) is NOT a TCB.. aka NULL.
7002                  */
7003                 SCTP_INP_INCR_REF(inp);
7004                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
7005                 if (stcb == NULL) {
7006                         SCTP_INP_DECR_REF(inp);
7007                 } else {
7008                         SCTP_TCB_UNLOCK(stcb);
7009                 }
7010         }
7011         if (stcb != NULL) {
7012                 /* Already have or am bring up an association */
7013                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
7014                 error = EALREADY;
7015                 goto out_now;
7016         }
7017
7018         vrf_id = inp->def_vrf_id;
7019         /* We are GOOD to go */
7020         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
7021             inp->sctp_ep.pre_open_stream_count,
7022             inp->sctp_ep.port, p,
7023             SCTP_INITIALIZE_AUTH_PARAMS);
7024         if (stcb == NULL) {
7025                 /* Gak! no memory */
7026                 goto out_now;
7027         }
7028         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
7029                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
7030                 /* Set the connected flag so we can queue data */
7031                 soisconnecting(so);
7032         }
7033         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
7034         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
7035
7036         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
7037         SCTP_TCB_UNLOCK(stcb);
7038 out_now:
7039         if (create_lock_on) {
7040                 SCTP_ASOC_CREATE_UNLOCK(inp);
7041         }
7042
7043         SCTP_INP_DECR_REF(inp);
7044         return (error);
7045 }
7046 #endif
7047
7048 int
7049 sctp_listen(struct socket *so, int backlog, struct thread *p)
7050 {
7051         /*
7052          * Note this module depends on the protocol processing being called
7053          * AFTER any socket level flags and backlog are applied to the
7054          * socket. The traditional way that the socket flags are applied is
7055          * AFTER protocol processing. We have made a change to the
7056          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
7057          * place if the socket API for SCTP is to work properly.
7058          */
7059
7060         int error = 0;
7061         struct sctp_inpcb *inp;
7062
7063         inp = (struct sctp_inpcb *)so->so_pcb;
7064         if (inp == NULL) {
7065                 /* I made the same as TCP since we are not setup? */
7066                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7067                 return (ECONNRESET);
7068         }
7069         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
7070                 /* See if we have a listener */
7071                 struct sctp_inpcb *tinp;
7072                 union sctp_sockstore store;
7073
7074                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
7075                         /* not bound all */
7076                         struct sctp_laddr *laddr;
7077
7078                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7079                                 memcpy(&store, &laddr->ifa->address, sizeof(store));
7080                                 switch (store.sa.sa_family) {
7081 #ifdef INET
7082                                 case AF_INET:
7083                                         store.sin.sin_port = inp->sctp_lport;
7084                                         break;
7085 #endif
7086 #ifdef INET6
7087                                 case AF_INET6:
7088                                         store.sin6.sin6_port = inp->sctp_lport;
7089                                         break;
7090 #endif
7091                                 default:
7092                                         break;
7093                                 }
7094                                 tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7095                                 if (tinp && (tinp != inp) &&
7096                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7097                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7098                                     (SCTP_IS_LISTENING(tinp))) {
7099                                         /*
7100                                          * we have a listener already and
7101                                          * its not this inp.
7102                                          */
7103                                         SCTP_INP_DECR_REF(tinp);
7104                                         return (EADDRINUSE);
7105                                 } else if (tinp) {
7106                                         SCTP_INP_DECR_REF(tinp);
7107                                 }
7108                         }
7109                 } else {
7110                         /* Setup a local addr bound all */
7111                         memset(&store, 0, sizeof(store));
7112 #ifdef INET6
7113                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7114                                 store.sa.sa_family = AF_INET6;
7115                                 store.sa.sa_len = sizeof(struct sockaddr_in6);
7116                         }
7117 #endif
7118 #ifdef INET
7119                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7120                                 store.sa.sa_family = AF_INET;
7121                                 store.sa.sa_len = sizeof(struct sockaddr_in);
7122                         }
7123 #endif
7124                         switch (store.sa.sa_family) {
7125 #ifdef INET
7126                         case AF_INET:
7127                                 store.sin.sin_port = inp->sctp_lport;
7128                                 break;
7129 #endif
7130 #ifdef INET6
7131                         case AF_INET6:
7132                                 store.sin6.sin6_port = inp->sctp_lport;
7133                                 break;
7134 #endif
7135                         default:
7136                                 break;
7137                         }
7138                         tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7139                         if (tinp && (tinp != inp) &&
7140                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7141                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7142                             (SCTP_IS_LISTENING(tinp))) {
7143                                 /*
7144                                  * we have a listener already and its not
7145                                  * this inp.
7146                                  */
7147                                 SCTP_INP_DECR_REF(tinp);
7148                                 return (EADDRINUSE);
7149                         } else if (tinp) {
7150                                 SCTP_INP_DECR_REF(tinp);
7151                         }
7152                 }
7153         }
7154         SCTP_INP_RLOCK(inp);
7155 #ifdef SCTP_LOCK_LOGGING
7156         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7157                 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7158         }
7159 #endif
7160         SOCK_LOCK(so);
7161         error = solisten_proto_check(so);
7162         SOCK_UNLOCK(so);
7163         if (error) {
7164                 SCTP_INP_RUNLOCK(inp);
7165                 return (error);
7166         }
7167         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7168             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7169                 /*
7170                  * The unlucky case - We are in the tcp pool with this guy.
7171                  * - Someone else is in the main inp slot. - We must move
7172                  * this guy (the listener) to the main slot - We must then
7173                  * move the guy that was listener to the TCP Pool.
7174                  */
7175                 if (sctp_swap_inpcb_for_listen(inp)) {
7176                         SCTP_INP_RUNLOCK(inp);
7177                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7178                         return (EADDRINUSE);
7179                 }
7180         }
7181
7182         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7183             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7184                 /* We are already connected AND the TCP model */
7185                 SCTP_INP_RUNLOCK(inp);
7186                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7187                 return (EADDRINUSE);
7188         }
7189         SCTP_INP_RUNLOCK(inp);
7190         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7191                 /* We must do a bind. */
7192                 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7193                         /* bind error, probably perm */
7194                         return (error);
7195                 }
7196         }
7197         SCTP_INP_WLOCK(inp);
7198         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) == 0) {
7199                 SOCK_LOCK(so);
7200                 solisten_proto(so, backlog);
7201                 SOCK_UNLOCK(so);
7202         }
7203         if (backlog > 0) {
7204                 inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
7205         } else {
7206                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
7207         }
7208         SCTP_INP_WUNLOCK(inp);
7209         return (error);
7210 }
7211
7212 static int sctp_defered_wakeup_cnt = 0;
7213
7214 int
7215 sctp_accept(struct socket *so, struct sockaddr **addr)
7216 {
7217         struct sctp_tcb *stcb;
7218         struct sctp_inpcb *inp;
7219         union sctp_sockstore store;
7220 #ifdef INET6
7221         int error;
7222 #endif
7223         inp = (struct sctp_inpcb *)so->so_pcb;
7224
7225         if (inp == NULL) {
7226                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7227                 return (ECONNRESET);
7228         }
7229         SCTP_INP_WLOCK(inp);
7230         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7231                 SCTP_INP_WUNLOCK(inp);
7232                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7233                 return (EOPNOTSUPP);
7234         }
7235         if (so->so_state & SS_ISDISCONNECTED) {
7236                 SCTP_INP_WUNLOCK(inp);
7237                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7238                 return (ECONNABORTED);
7239         }
7240         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7241         if (stcb == NULL) {
7242                 SCTP_INP_WUNLOCK(inp);
7243                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7244                 return (ECONNRESET);
7245         }
7246         SCTP_TCB_LOCK(stcb);
7247         store = stcb->asoc.primary_destination->ro._l_addr;
7248         SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
7249         /* Wake any delayed sleep action */
7250         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
7251                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
7252                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
7253                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
7254                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
7255                         if (sowriteable(inp->sctp_socket)) {
7256                                 sowwakeup_locked(inp->sctp_socket);
7257                         } else {
7258                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
7259                         }
7260                 }
7261                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
7262                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
7263                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
7264                         if (soreadable(inp->sctp_socket)) {
7265                                 sctp_defered_wakeup_cnt++;
7266                                 sorwakeup_locked(inp->sctp_socket);
7267                         } else {
7268                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
7269                         }
7270                 }
7271         }
7272         SCTP_INP_WUNLOCK(inp);
7273         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
7274                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7275                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_19);
7276         } else {
7277                 SCTP_TCB_UNLOCK(stcb);
7278         }
7279         switch (store.sa.sa_family) {
7280 #ifdef INET
7281         case AF_INET:
7282                 {
7283                         struct sockaddr_in *sin;
7284
7285                         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7286                         if (sin == NULL)
7287                                 return (ENOMEM);
7288                         sin->sin_family = AF_INET;
7289                         sin->sin_len = sizeof(*sin);
7290                         sin->sin_port = store.sin.sin_port;
7291                         sin->sin_addr = store.sin.sin_addr;
7292                         *addr = (struct sockaddr *)sin;
7293                         break;
7294                 }
7295 #endif
7296 #ifdef INET6
7297         case AF_INET6:
7298                 {
7299                         struct sockaddr_in6 *sin6;
7300
7301                         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
7302                         if (sin6 == NULL)
7303                                 return (ENOMEM);
7304                         sin6->sin6_family = AF_INET6;
7305                         sin6->sin6_len = sizeof(*sin6);
7306                         sin6->sin6_port = store.sin6.sin6_port;
7307                         sin6->sin6_addr = store.sin6.sin6_addr;
7308                         if ((error = sa6_recoverscope(sin6)) != 0) {
7309                                 SCTP_FREE_SONAME(sin6);
7310                                 return (error);
7311                         }
7312                         *addr = (struct sockaddr *)sin6;
7313                         break;
7314                 }
7315 #endif
7316         default:
7317                 /* TSNH */
7318                 break;
7319         }
7320         return (0);
7321 }
7322
7323 #ifdef INET
7324 int
7325 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
7326 {
7327         struct sockaddr_in *sin;
7328         uint32_t vrf_id;
7329         struct sctp_inpcb *inp;
7330         struct sctp_ifa *sctp_ifa;
7331
7332         /*
7333          * Do the malloc first in case it blocks.
7334          */
7335         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7336         if (sin == NULL)
7337                 return (ENOMEM);
7338         sin->sin_family = AF_INET;
7339         sin->sin_len = sizeof(*sin);
7340         inp = (struct sctp_inpcb *)so->so_pcb;
7341         if (!inp) {
7342                 SCTP_FREE_SONAME(sin);
7343                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7344                 return (ECONNRESET);
7345         }
7346         SCTP_INP_RLOCK(inp);
7347         sin->sin_port = inp->sctp_lport;
7348         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7349                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7350                         struct sctp_tcb *stcb;
7351                         struct sockaddr_in *sin_a;
7352                         struct sctp_nets *net;
7353                         int fnd;
7354
7355                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7356                         if (stcb == NULL) {
7357                                 goto notConn;
7358                         }
7359                         fnd = 0;
7360                         sin_a = NULL;
7361                         SCTP_TCB_LOCK(stcb);
7362                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7363                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7364                                 if (sin_a == NULL)
7365                                         /* this will make coverity happy */
7366                                         continue;
7367
7368                                 if (sin_a->sin_family == AF_INET) {
7369                                         fnd = 1;
7370                                         break;
7371                                 }
7372                         }
7373                         if ((!fnd) || (sin_a == NULL)) {
7374                                 /* punt */
7375                                 SCTP_TCB_UNLOCK(stcb);
7376                                 goto notConn;
7377                         }
7378
7379                         vrf_id = inp->def_vrf_id;
7380                         sctp_ifa = sctp_source_address_selection(inp,
7381                             stcb,
7382                             (sctp_route_t *)&net->ro,
7383                             net, 0, vrf_id);
7384                         if (sctp_ifa) {
7385                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
7386                                 sctp_free_ifa(sctp_ifa);
7387                         }
7388                         SCTP_TCB_UNLOCK(stcb);
7389                 } else {
7390                         /* For the bound all case you get back 0 */
7391         notConn:
7392                         sin->sin_addr.s_addr = 0;
7393                 }
7394
7395         } else {
7396                 /* Take the first IPv4 address in the list */
7397                 struct sctp_laddr *laddr;
7398                 int fnd = 0;
7399
7400                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7401                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
7402                                 struct sockaddr_in *sin_a;
7403
7404                                 sin_a = &laddr->ifa->address.sin;
7405                                 sin->sin_addr = sin_a->sin_addr;
7406                                 fnd = 1;
7407                                 break;
7408                         }
7409                 }
7410                 if (!fnd) {
7411                         SCTP_FREE_SONAME(sin);
7412                         SCTP_INP_RUNLOCK(inp);
7413                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7414                         return (ENOENT);
7415                 }
7416         }
7417         SCTP_INP_RUNLOCK(inp);
7418         (*addr) = (struct sockaddr *)sin;
7419         return (0);
7420 }
7421
7422 int
7423 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
7424 {
7425         struct sockaddr_in *sin;
7426         int fnd;
7427         struct sockaddr_in *sin_a;
7428         struct sctp_inpcb *inp;
7429         struct sctp_tcb *stcb;
7430         struct sctp_nets *net;
7431
7432         /* Do the malloc first in case it blocks. */
7433         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7434         if (sin == NULL)
7435                 return (ENOMEM);
7436         sin->sin_family = AF_INET;
7437         sin->sin_len = sizeof(*sin);
7438
7439         inp = (struct sctp_inpcb *)so->so_pcb;
7440         if ((inp == NULL) ||
7441             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
7442                 /* UDP type and listeners will drop out here */
7443                 SCTP_FREE_SONAME(sin);
7444                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
7445                 return (ENOTCONN);
7446         }
7447         SCTP_INP_RLOCK(inp);
7448         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7449         if (stcb) {
7450                 SCTP_TCB_LOCK(stcb);
7451         }
7452         SCTP_INP_RUNLOCK(inp);
7453         if (stcb == NULL) {
7454                 SCTP_FREE_SONAME(sin);
7455                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7456                 return (ECONNRESET);
7457         }
7458         fnd = 0;
7459         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7460                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
7461                 if (sin_a->sin_family == AF_INET) {
7462                         fnd = 1;
7463                         sin->sin_port = stcb->rport;
7464                         sin->sin_addr = sin_a->sin_addr;
7465                         break;
7466                 }
7467         }
7468         SCTP_TCB_UNLOCK(stcb);
7469         if (!fnd) {
7470                 /* No IPv4 address */
7471                 SCTP_FREE_SONAME(sin);
7472                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
7473                 return (ENOENT);
7474         }
7475         (*addr) = (struct sockaddr *)sin;
7476         return (0);
7477 }
7478
7479 struct pr_usrreqs sctp_usrreqs = {
7480         .pru_abort = sctp_abort,
7481         .pru_accept = sctp_accept,
7482         .pru_attach = sctp_attach,
7483         .pru_bind = sctp_bind,
7484         .pru_connect = sctp_connect,
7485         .pru_control = in_control,
7486         .pru_close = sctp_close,
7487         .pru_detach = sctp_close,
7488         .pru_sopoll = sopoll_generic,
7489         .pru_flush = sctp_flush,
7490         .pru_disconnect = sctp_disconnect,
7491         .pru_listen = sctp_listen,
7492         .pru_peeraddr = sctp_peeraddr,
7493         .pru_send = sctp_sendm,
7494         .pru_shutdown = sctp_shutdown,
7495         .pru_sockaddr = sctp_ingetaddr,
7496         .pru_sosend = sctp_sosend,
7497         .pru_soreceive = sctp_soreceive
7498 };
7499 #endif