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