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