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