]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/netinet/sctp_usrreq.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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                         struct sctp_assoc_value *av;
2077
2078                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2079                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2080
2081                         if (stcb) {
2082                                 av->assoc_value = stcb->asoc.max_burst;
2083                                 SCTP_TCB_UNLOCK(stcb);
2084                         } else {
2085                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2086                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2087                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2088                                         SCTP_INP_RLOCK(inp);
2089                                         av->assoc_value = inp->sctp_ep.max_burst;
2090                                         SCTP_INP_RUNLOCK(inp);
2091                                 } else {
2092                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2093                                         error = EINVAL;
2094                                 }
2095                         }
2096                         if (error == 0) {
2097                                 *optsize = sizeof(struct sctp_assoc_value);
2098                         }
2099                         break;
2100                 }
2101         case SCTP_MAXSEG:
2102                 {
2103                         struct sctp_assoc_value *av;
2104                         int ovh;
2105
2106                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2107                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2108
2109                         if (stcb) {
2110                                 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2111                                 SCTP_TCB_UNLOCK(stcb);
2112                         } else {
2113                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2114                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2115                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2116                                         SCTP_INP_RLOCK(inp);
2117                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2118                                                 ovh = SCTP_MED_OVERHEAD;
2119                                         } else {
2120                                                 ovh = SCTP_MED_V4_OVERHEAD;
2121                                         }
2122                                         if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2123                                                 av->assoc_value = 0;
2124                                         else
2125                                                 av->assoc_value = inp->sctp_frag_point - ovh;
2126                                         SCTP_INP_RUNLOCK(inp);
2127                                 } else {
2128                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2129                                         error = EINVAL;
2130                                 }
2131                         }
2132                         if (error == 0) {
2133                                 *optsize = sizeof(struct sctp_assoc_value);
2134                         }
2135                         break;
2136                 }
2137         case SCTP_GET_STAT_LOG:
2138                 error = sctp_fill_stat_log(optval, optsize);
2139                 break;
2140         case SCTP_EVENTS:
2141                 {
2142                         struct sctp_event_subscribe *events;
2143
2144                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2145                         memset(events, 0, sizeof(struct sctp_event_subscribe));
2146                         SCTP_INP_RLOCK(inp);
2147                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2148                                 events->sctp_data_io_event = 1;
2149
2150                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2151                                 events->sctp_association_event = 1;
2152
2153                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2154                                 events->sctp_address_event = 1;
2155
2156                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2157                                 events->sctp_send_failure_event = 1;
2158
2159                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2160                                 events->sctp_peer_error_event = 1;
2161
2162                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2163                                 events->sctp_shutdown_event = 1;
2164
2165                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2166                                 events->sctp_partial_delivery_event = 1;
2167
2168                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2169                                 events->sctp_adaptation_layer_event = 1;
2170
2171                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2172                                 events->sctp_authentication_event = 1;
2173
2174                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2175                                 events->sctp_sender_dry_event = 1;
2176
2177                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2178                                 events->sctp_stream_reset_event = 1;
2179                         SCTP_INP_RUNLOCK(inp);
2180                         *optsize = sizeof(struct sctp_event_subscribe);
2181                         break;
2182                 }
2183         case SCTP_ADAPTATION_LAYER:
2184                 {
2185                         uint32_t *value;
2186
2187                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2188
2189                         SCTP_INP_RLOCK(inp);
2190                         *value = inp->sctp_ep.adaptation_layer_indicator;
2191                         SCTP_INP_RUNLOCK(inp);
2192                         *optsize = sizeof(uint32_t);
2193                         break;
2194                 }
2195         case SCTP_SET_INITIAL_DBG_SEQ:
2196                 {
2197                         uint32_t *value;
2198
2199                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2200                         SCTP_INP_RLOCK(inp);
2201                         *value = inp->sctp_ep.initial_sequence_debug;
2202                         SCTP_INP_RUNLOCK(inp);
2203                         *optsize = sizeof(uint32_t);
2204                         break;
2205                 }
2206         case SCTP_GET_LOCAL_ADDR_SIZE:
2207                 {
2208                         uint32_t *value;
2209
2210                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2211                         SCTP_INP_RLOCK(inp);
2212                         *value = sctp_count_max_addresses(inp);
2213                         SCTP_INP_RUNLOCK(inp);
2214                         *optsize = sizeof(uint32_t);
2215                         break;
2216                 }
2217         case SCTP_GET_REMOTE_ADDR_SIZE:
2218                 {
2219                         uint32_t *value;
2220                         size_t size;
2221                         struct sctp_nets *net;
2222
2223                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2224                         /* FIXME MT: change to sctp_assoc_value? */
2225                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
2226
2227                         if (stcb) {
2228                                 size = 0;
2229                                 /* Count the sizes */
2230                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2231                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2232                                                 size += sizeof(struct sockaddr_in6);
2233                                         } else {
2234                                                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2235 #ifdef INET
2236                                                 case AF_INET:
2237                                                         size += sizeof(struct sockaddr_in);
2238                                                         break;
2239 #endif
2240 #ifdef INET6
2241                                                 case AF_INET6:
2242                                                         size += sizeof(struct sockaddr_in6);
2243                                                         break;
2244 #endif
2245                                                 default:
2246                                                         break;
2247                                                 }
2248                                         }
2249                                 }
2250                                 SCTP_TCB_UNLOCK(stcb);
2251                                 *value = (uint32_t) size;
2252                                 *optsize = sizeof(uint32_t);
2253                         } else {
2254                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2255                                 error = ENOTCONN;
2256                         }
2257                         break;
2258                 }
2259         case SCTP_GET_PEER_ADDRESSES:
2260                 /*
2261                  * Get the address information, an array is passed in to
2262                  * fill up we pack it.
2263                  */
2264                 {
2265                         size_t cpsz, left;
2266                         struct sockaddr_storage *sas;
2267                         struct sctp_nets *net;
2268                         struct sctp_getaddresses *saddr;
2269
2270                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2271                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2272
2273                         if (stcb) {
2274                                 left = (*optsize) - sizeof(struct sctp_getaddresses);
2275                                 *optsize = sizeof(struct sctp_getaddresses);
2276                                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2277
2278                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2279                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2280                                                 cpsz = sizeof(struct sockaddr_in6);
2281                                         } else {
2282                                                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2283 #ifdef INET
2284                                                 case AF_INET:
2285                                                         cpsz = sizeof(struct sockaddr_in);
2286                                                         break;
2287 #endif
2288 #ifdef INET6
2289                                                 case AF_INET6:
2290                                                         cpsz = sizeof(struct sockaddr_in6);
2291                                                         break;
2292 #endif
2293                                                 default:
2294                                                         cpsz = 0;
2295                                                         break;
2296                                                 }
2297                                         }
2298                                         if (cpsz == 0) {
2299                                                 break;
2300                                         }
2301                                         if (left < cpsz) {
2302                                                 /* not enough room. */
2303                                                 break;
2304                                         }
2305 #if defined(INET) && defined(INET6)
2306                                         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2307                                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2308                                                 /* Must map the address */
2309                                                 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2310                                                     (struct sockaddr_in6 *)sas);
2311                                         } else {
2312 #endif
2313                                                 memcpy(sas, &net->ro._l_addr, cpsz);
2314 #if defined(INET) && defined(INET6)
2315                                         }
2316 #endif
2317                                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2318
2319                                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2320                                         left -= cpsz;
2321                                         *optsize += cpsz;
2322                                 }
2323                                 SCTP_TCB_UNLOCK(stcb);
2324                         } else {
2325                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2326                                 error = ENOENT;
2327                         }
2328                         break;
2329                 }
2330         case SCTP_GET_LOCAL_ADDRESSES:
2331                 {
2332                         size_t limit, actual;
2333                         struct sockaddr_storage *sas;
2334                         struct sctp_getaddresses *saddr;
2335
2336                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2337                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2338
2339                         sas = (struct sockaddr_storage *)&saddr->addr[0];
2340                         limit = *optsize - sizeof(sctp_assoc_t);
2341                         actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2342                         if (stcb) {
2343                                 SCTP_TCB_UNLOCK(stcb);
2344                         }
2345                         *optsize = sizeof(struct sockaddr_storage) + actual;
2346                         break;
2347                 }
2348         case SCTP_PEER_ADDR_PARAMS:
2349                 {
2350                         struct sctp_paddrparams *paddrp;
2351                         struct sctp_nets *net;
2352
2353                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2354                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2355
2356                         net = NULL;
2357                         if (stcb) {
2358                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2359                         } else {
2360                                 /*
2361                                  * We increment here since
2362                                  * sctp_findassociation_ep_addr() wil do a
2363                                  * decrement if it finds the stcb as long as
2364                                  * the locked tcb (last argument) is NOT a
2365                                  * TCB.. aka NULL.
2366                                  */
2367                                 SCTP_INP_INCR_REF(inp);
2368                                 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2369                                 if (stcb == NULL) {
2370                                         SCTP_INP_DECR_REF(inp);
2371                                 }
2372                         }
2373                         if (stcb && (net == NULL)) {
2374                                 struct sockaddr *sa;
2375
2376                                 sa = (struct sockaddr *)&paddrp->spp_address;
2377 #ifdef INET
2378                                 if (sa->sa_family == AF_INET) {
2379                                         struct sockaddr_in *sin;
2380
2381                                         sin = (struct sockaddr_in *)sa;
2382                                         if (sin->sin_addr.s_addr) {
2383                                                 error = EINVAL;
2384                                                 SCTP_TCB_UNLOCK(stcb);
2385                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2386                                                 break;
2387                                         }
2388                                 } else
2389 #endif
2390 #ifdef INET6
2391                                 if (sa->sa_family == AF_INET6) {
2392                                         struct sockaddr_in6 *sin6;
2393
2394                                         sin6 = (struct sockaddr_in6 *)sa;
2395                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2396                                                 error = EINVAL;
2397                                                 SCTP_TCB_UNLOCK(stcb);
2398                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2399                                                 break;
2400                                         }
2401                                 } else
2402 #endif
2403                                 {
2404                                         error = EAFNOSUPPORT;
2405                                         SCTP_TCB_UNLOCK(stcb);
2406                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2407                                         break;
2408                                 }
2409                         }
2410                         if (stcb) {
2411                                 /* Applies to the specific association */
2412                                 paddrp->spp_flags = 0;
2413                                 if (net) {
2414                                         int ovh;
2415
2416                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2417                                                 ovh = SCTP_MED_OVERHEAD;
2418                                         } else {
2419                                                 ovh = SCTP_MED_V4_OVERHEAD;
2420                                         }
2421
2422                                         paddrp->spp_hbinterval = net->heart_beat_delay;
2423                                         paddrp->spp_pathmaxrxt = net->failure_threshold;
2424                                         paddrp->spp_pathmtu = net->mtu - ovh;
2425                                         /* get flags for HB */
2426                                         if (net->dest_state & SCTP_ADDR_NOHB) {
2427                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2428                                         } else {
2429                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2430                                         }
2431                                         /* get flags for PMTU */
2432                                         if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2433                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2434                                         } else {
2435                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2436                                         }
2437                                         if (net->dscp & 0x01) {
2438                                                 paddrp->spp_dscp = net->dscp & 0xfc;
2439                                                 paddrp->spp_flags |= SPP_DSCP;
2440                                         }
2441 #ifdef INET6
2442                                         if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
2443                                             (net->flowlabel & 0x80000000)) {
2444                                                 paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
2445                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2446                                         }
2447 #endif
2448                                 } else {
2449                                         /*
2450                                          * No destination so return default
2451                                          * value
2452                                          */
2453                                         paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2454                                         paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
2455                                         if (stcb->asoc.default_dscp & 0x01) {
2456                                                 paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
2457                                                 paddrp->spp_flags |= SPP_DSCP;
2458                                         }
2459 #ifdef INET6
2460                                         if (stcb->asoc.default_flowlabel & 0x80000000) {
2461                                                 paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
2462                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2463                                         }
2464 #endif
2465                                         /* default settings should be these */
2466                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2467                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2468                                         } else {
2469                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2470                                         }
2471                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2472                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2473                                         } else {
2474                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2475                                         }
2476                                         paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2477                                 }
2478                                 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2479                                 SCTP_TCB_UNLOCK(stcb);
2480                         } else {
2481                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2482                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2483                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
2484                                         /* Use endpoint defaults */
2485                                         SCTP_INP_RLOCK(inp);
2486                                         paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2487                                         paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
2488                                         paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
2489                                         /* get inp's default */
2490                                         if (inp->sctp_ep.default_dscp & 0x01) {
2491                                                 paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
2492                                                 paddrp->spp_flags |= SPP_DSCP;
2493                                         }
2494 #ifdef INET6
2495                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2496                                             (inp->sctp_ep.default_flowlabel & 0x80000000)) {
2497                                                 paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
2498                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
2499                                         }
2500 #endif
2501                                         /* can't return this */
2502                                         paddrp->spp_pathmtu = 0;
2503
2504                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
2505                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
2506                                         } else {
2507                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
2508                                         }
2509                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
2510                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
2511                                         } else {
2512                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
2513                                         }
2514                                         SCTP_INP_RUNLOCK(inp);
2515                                 } else {
2516                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2517                                         error = EINVAL;
2518                                 }
2519                         }
2520                         if (error == 0) {
2521                                 *optsize = sizeof(struct sctp_paddrparams);
2522                         }
2523                         break;
2524                 }
2525         case SCTP_GET_PEER_ADDR_INFO:
2526                 {
2527                         struct sctp_paddrinfo *paddri;
2528                         struct sctp_nets *net;
2529
2530                         SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2531                         SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2532
2533                         net = NULL;
2534                         if (stcb) {
2535                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2536                         } else {
2537                                 /*
2538                                  * We increment here since
2539                                  * sctp_findassociation_ep_addr() wil do a
2540                                  * decrement if it finds the stcb as long as
2541                                  * the locked tcb (last argument) is NOT a
2542                                  * TCB.. aka NULL.
2543                                  */
2544                                 SCTP_INP_INCR_REF(inp);
2545                                 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2546                                 if (stcb == NULL) {
2547                                         SCTP_INP_DECR_REF(inp);
2548                                 }
2549                         }
2550
2551                         if ((stcb) && (net)) {
2552                                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2553                                         /* It's unconfirmed */
2554                                         paddri->spinfo_state = SCTP_UNCONFIRMED;
2555                                 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2556                                         /* It's active */
2557                                         paddri->spinfo_state = SCTP_ACTIVE;
2558                                 } else {
2559                                         /* It's inactive */
2560                                         paddri->spinfo_state = SCTP_INACTIVE;
2561                                 }
2562                                 paddri->spinfo_cwnd = net->cwnd;
2563                                 paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2564                                 paddri->spinfo_rto = net->RTO;
2565                                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2566                                 paddri->spinfo_mtu = net->mtu;
2567                                 SCTP_TCB_UNLOCK(stcb);
2568                                 *optsize = sizeof(struct sctp_paddrinfo);
2569                         } else {
2570                                 if (stcb) {
2571                                         SCTP_TCB_UNLOCK(stcb);
2572                                 }
2573                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2574                                 error = ENOENT;
2575                         }
2576                         break;
2577                 }
2578         case SCTP_PCB_STATUS:
2579                 {
2580                         struct sctp_pcbinfo *spcb;
2581
2582                         SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2583                         sctp_fill_pcbinfo(spcb);
2584                         *optsize = sizeof(struct sctp_pcbinfo);
2585                         break;
2586                 }
2587         case SCTP_STATUS:
2588                 {
2589                         struct sctp_nets *net;
2590                         struct sctp_status *sstat;
2591
2592                         SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2593                         SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2594
2595                         if (stcb == NULL) {
2596                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2597                                 error = EINVAL;
2598                                 break;
2599                         }
2600                         /*
2601                          * I think passing the state is fine since
2602                          * sctp_constants.h will be available to the user
2603                          * land.
2604                          */
2605                         sstat->sstat_state = stcb->asoc.state;
2606                         sstat->sstat_assoc_id = sctp_get_associd(stcb);
2607                         sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2608                         sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2609                         /*
2610                          * We can't include chunks that have been passed to
2611                          * the socket layer. Only things in queue.
2612                          */
2613                         sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2614                             stcb->asoc.cnt_on_all_streams);
2615
2616
2617                         sstat->sstat_instrms = stcb->asoc.streamincnt;
2618                         sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2619                         sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2620                         memcpy(&sstat->sstat_primary.spinfo_address,
2621                             &stcb->asoc.primary_destination->ro._l_addr,
2622                             ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2623                         net = stcb->asoc.primary_destination;
2624                         ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2625                         /*
2626                          * Again the user can get info from sctp_constants.h
2627                          * for what the state of the network is.
2628                          */
2629                         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
2630                                 /* It's unconfirmed */
2631                                 sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
2632                         } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
2633                                 /* It's active */
2634                                 sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
2635                         } else {
2636                                 /* It's inactive */
2637                                 sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
2638                         }
2639                         sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2640                         sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
2641                         sstat->sstat_primary.spinfo_rto = net->RTO;
2642                         sstat->sstat_primary.spinfo_mtu = net->mtu;
2643                         sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2644                         SCTP_TCB_UNLOCK(stcb);
2645                         *optsize = sizeof(struct sctp_status);
2646                         break;
2647                 }
2648         case SCTP_RTOINFO:
2649                 {
2650                         struct sctp_rtoinfo *srto;
2651
2652                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2653                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2654
2655                         if (stcb) {
2656                                 srto->srto_initial = stcb->asoc.initial_rto;
2657                                 srto->srto_max = stcb->asoc.maxrto;
2658                                 srto->srto_min = stcb->asoc.minrto;
2659                                 SCTP_TCB_UNLOCK(stcb);
2660                         } else {
2661                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2662                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2663                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
2664                                         SCTP_INP_RLOCK(inp);
2665                                         srto->srto_initial = inp->sctp_ep.initial_rto;
2666                                         srto->srto_max = inp->sctp_ep.sctp_maxrto;
2667                                         srto->srto_min = inp->sctp_ep.sctp_minrto;
2668                                         SCTP_INP_RUNLOCK(inp);
2669                                 } else {
2670                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2671                                         error = EINVAL;
2672                                 }
2673                         }
2674                         if (error == 0) {
2675                                 *optsize = sizeof(struct sctp_rtoinfo);
2676                         }
2677                         break;
2678                 }
2679         case SCTP_TIMEOUTS:
2680                 {
2681                         struct sctp_timeouts *stimo;
2682
2683                         SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
2684                         SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
2685
2686                         if (stcb) {
2687                                 stimo->stimo_init = stcb->asoc.timoinit;
2688                                 stimo->stimo_data = stcb->asoc.timodata;
2689                                 stimo->stimo_sack = stcb->asoc.timosack;
2690                                 stimo->stimo_shutdown = stcb->asoc.timoshutdown;
2691                                 stimo->stimo_heartbeat = stcb->asoc.timoheartbeat;
2692                                 stimo->stimo_cookie = stcb->asoc.timocookie;
2693                                 stimo->stimo_shutdownack = stcb->asoc.timoshutdownack;
2694                                 SCTP_TCB_UNLOCK(stcb);
2695                                 *optsize = sizeof(struct sctp_timeouts);
2696                         } else {
2697                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2698                                 error = EINVAL;
2699                         }
2700                         break;
2701                 }
2702         case SCTP_ASSOCINFO:
2703                 {
2704                         struct sctp_assocparams *sasoc;
2705
2706                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2707                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2708
2709                         if (stcb) {
2710                                 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
2711                                 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2712                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2713                                 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2714                                 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2715                                 SCTP_TCB_UNLOCK(stcb);
2716                         } else {
2717                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2718                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2719                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
2720                                         SCTP_INP_RLOCK(inp);
2721                                         sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
2722                                         sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2723                                         sasoc->sasoc_number_peer_destinations = 0;
2724                                         sasoc->sasoc_peer_rwnd = 0;
2725                                         sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2726                                         SCTP_INP_RUNLOCK(inp);
2727                                 } else {
2728                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2729                                         error = EINVAL;
2730                                 }
2731                         }
2732                         if (error == 0) {
2733                                 *optsize = sizeof(struct sctp_assocparams);
2734                         }
2735                         break;
2736                 }
2737         case SCTP_DEFAULT_SEND_PARAM:
2738                 {
2739                         struct sctp_sndrcvinfo *s_info;
2740
2741                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2742                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2743
2744                         if (stcb) {
2745                                 memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
2746                                 SCTP_TCB_UNLOCK(stcb);
2747                         } else {
2748                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2749                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2750                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
2751                                         SCTP_INP_RLOCK(inp);
2752                                         memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
2753                                         SCTP_INP_RUNLOCK(inp);
2754                                 } else {
2755                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2756                                         error = EINVAL;
2757                                 }
2758                         }
2759                         if (error == 0) {
2760                                 *optsize = sizeof(struct sctp_sndrcvinfo);
2761                         }
2762                         break;
2763                 }
2764         case SCTP_INITMSG:
2765                 {
2766                         struct sctp_initmsg *sinit;
2767
2768                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2769                         SCTP_INP_RLOCK(inp);
2770                         sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2771                         sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2772                         sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2773                         sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2774                         SCTP_INP_RUNLOCK(inp);
2775                         *optsize = sizeof(struct sctp_initmsg);
2776                         break;
2777                 }
2778         case SCTP_PRIMARY_ADDR:
2779                 /* we allow a "get" operation on this */
2780                 {
2781                         struct sctp_setprim *ssp;
2782
2783                         SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2784                         SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2785
2786                         if (stcb) {
2787                                 /* simply copy out the sockaddr_storage... */
2788                                 int len;
2789
2790                                 len = *optsize;
2791                                 if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
2792                                         len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
2793
2794                                 memcpy(&ssp->ssp_addr,
2795                                     &stcb->asoc.primary_destination->ro._l_addr,
2796                                     len);
2797                                 SCTP_TCB_UNLOCK(stcb);
2798                                 *optsize = sizeof(struct sctp_setprim);
2799                         } else {
2800                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2801                                 error = EINVAL;
2802                         }
2803                         break;
2804                 }
2805         case SCTP_HMAC_IDENT:
2806                 {
2807                         struct sctp_hmacalgo *shmac;
2808                         sctp_hmaclist_t *hmaclist;
2809                         uint32_t size;
2810                         int i;
2811
2812                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2813
2814                         SCTP_INP_RLOCK(inp);
2815                         hmaclist = inp->sctp_ep.local_hmacs;
2816                         if (hmaclist == NULL) {
2817                                 /* no HMACs to return */
2818                                 *optsize = sizeof(*shmac);
2819                                 SCTP_INP_RUNLOCK(inp);
2820                                 break;
2821                         }
2822                         /* is there room for all of the hmac ids? */
2823                         size = sizeof(*shmac) + (hmaclist->num_algo *
2824                             sizeof(shmac->shmac_idents[0]));
2825                         if ((size_t)(*optsize) < size) {
2826                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2827                                 error = EINVAL;
2828                                 SCTP_INP_RUNLOCK(inp);
2829                                 break;
2830                         }
2831                         /* copy in the list */
2832                         shmac->shmac_number_of_idents = hmaclist->num_algo;
2833                         for (i = 0; i < hmaclist->num_algo; i++) {
2834                                 shmac->shmac_idents[i] = hmaclist->hmac[i];
2835                         }
2836                         SCTP_INP_RUNLOCK(inp);
2837                         *optsize = size;
2838                         break;
2839                 }
2840         case SCTP_AUTH_ACTIVE_KEY:
2841                 {
2842                         struct sctp_authkeyid *scact;
2843
2844                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2845                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2846
2847                         if (stcb) {
2848                                 /* get the active key on the assoc */
2849                                 scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
2850                                 SCTP_TCB_UNLOCK(stcb);
2851                         } else {
2852                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2853                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2854                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
2855                                         /* get the endpoint active key */
2856                                         SCTP_INP_RLOCK(inp);
2857                                         scact->scact_keynumber = inp->sctp_ep.default_keyid;
2858                                         SCTP_INP_RUNLOCK(inp);
2859                                 } else {
2860                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2861                                         error = EINVAL;
2862                                 }
2863                         }
2864                         if (error == 0) {
2865                                 *optsize = sizeof(struct sctp_authkeyid);
2866                         }
2867                         break;
2868                 }
2869         case SCTP_LOCAL_AUTH_CHUNKS:
2870                 {
2871                         struct sctp_authchunks *sac;
2872                         sctp_auth_chklist_t *chklist = NULL;
2873                         size_t size = 0;
2874
2875                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2876                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2877
2878                         if (stcb) {
2879                                 /* get off the assoc */
2880                                 chklist = stcb->asoc.local_auth_chunks;
2881                                 /* is there enough space? */
2882                                 size = sctp_auth_get_chklist_size(chklist);
2883                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2884                                         error = EINVAL;
2885                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2886                                 } else {
2887                                         /* copy in the chunks */
2888                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2889                                         *optsize = sizeof(struct sctp_authchunks) + size;
2890                                 }
2891                                 SCTP_TCB_UNLOCK(stcb);
2892                         } else {
2893                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2894                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2895                                     (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
2896                                         /* get off the endpoint */
2897                                         SCTP_INP_RLOCK(inp);
2898                                         chklist = inp->sctp_ep.local_auth_chunks;
2899                                         /* is there enough space? */
2900                                         size = sctp_auth_get_chklist_size(chklist);
2901                                         if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2902                                                 error = EINVAL;
2903                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2904                                         } else {
2905                                                 /* copy in the chunks */
2906                                                 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2907                                                 *optsize = sizeof(struct sctp_authchunks) + size;
2908                                         }
2909                                         SCTP_INP_RUNLOCK(inp);
2910                                 } else {
2911                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2912                                         error = EINVAL;
2913                                 }
2914                         }
2915                         break;
2916                 }
2917         case SCTP_PEER_AUTH_CHUNKS:
2918                 {
2919                         struct sctp_authchunks *sac;
2920                         sctp_auth_chklist_t *chklist = NULL;
2921                         size_t size = 0;
2922
2923                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2924                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2925
2926                         if (stcb) {
2927                                 /* get off the assoc */
2928                                 chklist = stcb->asoc.peer_auth_chunks;
2929                                 /* is there enough space? */
2930                                 size = sctp_auth_get_chklist_size(chklist);
2931                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2932                                         error = EINVAL;
2933                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2934                                 } else {
2935                                         /* copy in the chunks */
2936                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2937                                         *optsize = sizeof(struct sctp_authchunks) + size;
2938                                 }
2939                                 SCTP_TCB_UNLOCK(stcb);
2940                         } else {
2941                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2942                                 error = ENOENT;
2943                         }
2944                         break;
2945                 }
2946         case SCTP_EVENT:
2947                 {
2948                         struct sctp_event *event;
2949                         uint32_t event_type;
2950
2951                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
2952                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
2953
2954                         switch (event->se_type) {
2955                         case SCTP_ASSOC_CHANGE:
2956                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
2957                                 break;
2958                         case SCTP_PEER_ADDR_CHANGE:
2959                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
2960                                 break;
2961                         case SCTP_REMOTE_ERROR:
2962                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
2963                                 break;
2964                         case SCTP_SEND_FAILED:
2965                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2966                                 break;
2967                         case SCTP_SHUTDOWN_EVENT:
2968                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
2969                                 break;
2970                         case SCTP_ADAPTATION_INDICATION:
2971                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
2972                                 break;
2973                         case SCTP_PARTIAL_DELIVERY_EVENT:
2974                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
2975                                 break;
2976                         case SCTP_AUTHENTICATION_EVENT:
2977                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
2978                                 break;
2979                         case SCTP_STREAM_RESET_EVENT:
2980                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
2981                                 break;
2982                         case SCTP_SENDER_DRY_EVENT:
2983                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
2984                                 break;
2985                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
2986                                 event_type = 0;
2987                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
2988                                 error = ENOTSUP;
2989                                 break;
2990                         default:
2991                                 event_type = 0;
2992                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2993                                 error = EINVAL;
2994                                 break;
2995                         }
2996                         if (event_type > 0) {
2997                                 if (stcb) {
2998                                         event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
2999                                         SCTP_TCB_UNLOCK(stcb);
3000                                 } else {
3001                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3002                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3003                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3004                                                 SCTP_INP_RLOCK(inp);
3005                                                 event->se_on = sctp_is_feature_on(inp, event_type);
3006                                                 SCTP_INP_RUNLOCK(inp);
3007                                         } else {
3008                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3009                                                 error = EINVAL;
3010                                         }
3011                                 }
3012                         }
3013                         if (error == 0) {
3014                                 *optsize = sizeof(struct sctp_event);
3015                         }
3016                         break;
3017                 }
3018         case SCTP_RECVRCVINFO:
3019                 {
3020                         int onoff;
3021
3022                         if (*optsize < sizeof(int)) {
3023                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3024                                 error = EINVAL;
3025                         } else {
3026                                 SCTP_INP_RUNLOCK(inp);
3027                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3028                                 SCTP_INP_RUNLOCK(inp);
3029                         }
3030                         if (error == 0) {
3031                                 /* return the option value */
3032                                 *(int *)optval = onoff;
3033                                 *optsize = sizeof(int);
3034                         }
3035                         break;
3036                 }
3037         case SCTP_RECVNXTINFO:
3038                 {
3039                         int onoff;
3040
3041                         if (*optsize < sizeof(int)) {
3042                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3043                                 error = EINVAL;
3044                         } else {
3045                                 SCTP_INP_RUNLOCK(inp);
3046                                 onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3047                                 SCTP_INP_RUNLOCK(inp);
3048                         }
3049                         if (error == 0) {
3050                                 /* return the option value */
3051                                 *(int *)optval = onoff;
3052                                 *optsize = sizeof(int);
3053                         }
3054                         break;
3055                 }
3056         case SCTP_DEFAULT_SNDINFO:
3057                 {
3058                         struct sctp_sndinfo *info;
3059
3060                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3061                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3062
3063                         if (stcb) {
3064                                 info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3065                                 info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3066                                 info->snd_flags &= 0xfff0;
3067                                 info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3068                                 info->snd_context = stcb->asoc.def_send.sinfo_context;
3069                                 SCTP_TCB_UNLOCK(stcb);
3070                         } else {
3071                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3072                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3073                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3074                                         SCTP_INP_RLOCK(inp);
3075                                         info->snd_sid = inp->def_send.sinfo_stream;
3076                                         info->snd_flags = inp->def_send.sinfo_flags;
3077                                         info->snd_flags &= 0xfff0;
3078                                         info->snd_ppid = inp->def_send.sinfo_ppid;
3079                                         info->snd_context = inp->def_send.sinfo_context;
3080                                         SCTP_INP_RUNLOCK(inp);
3081                                 } else {
3082                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3083                                         error = EINVAL;
3084                                 }
3085                         }
3086                         if (error == 0) {
3087                                 *optsize = sizeof(struct sctp_sndinfo);
3088                         }
3089                         break;
3090                 }
3091         case SCTP_DEFAULT_PRINFO:
3092                 {
3093                         struct sctp_default_prinfo *info;
3094
3095                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3096                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3097
3098                         if (stcb) {
3099                                 info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3100                                 info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3101                                 SCTP_TCB_UNLOCK(stcb);
3102                         } else {
3103                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3104                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3105                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3106                                         SCTP_INP_RLOCK(inp);
3107                                         info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3108                                         info->pr_value = inp->def_send.sinfo_timetolive;
3109                                         SCTP_INP_RUNLOCK(inp);
3110                                 } else {
3111                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3112                                         error = EINVAL;
3113                                 }
3114                         }
3115                         if (error == 0) {
3116                                 *optsize = sizeof(struct sctp_default_prinfo);
3117                         }
3118                         break;
3119                 }
3120         case SCTP_PEER_ADDR_THLDS:
3121                 {
3122                         struct sctp_paddrthlds *thlds;
3123                         struct sctp_nets *net;
3124
3125                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3126                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3127
3128                         net = NULL;
3129                         if (stcb) {
3130                                 net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_address);
3131                         } else {
3132                                 /*
3133                                  * We increment here since
3134                                  * sctp_findassociation_ep_addr() wil do a
3135                                  * decrement if it finds the stcb as long as
3136                                  * the locked tcb (last argument) is NOT a
3137                                  * TCB.. aka NULL.
3138                                  */
3139                                 SCTP_INP_INCR_REF(inp);
3140                                 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&thlds->spt_address, &net, NULL, NULL);
3141                                 if (stcb == NULL) {
3142                                         SCTP_INP_DECR_REF(inp);
3143                                 }
3144                         }
3145                         if (stcb && (net == NULL)) {
3146                                 struct sockaddr *sa;
3147
3148                                 sa = (struct sockaddr *)&thlds->spt_address;
3149 #ifdef INET
3150                                 if (sa->sa_family == AF_INET) {
3151                                         struct sockaddr_in *sin;
3152
3153                                         sin = (struct sockaddr_in *)sa;
3154                                         if (sin->sin_addr.s_addr) {
3155                                                 error = EINVAL;
3156                                                 SCTP_TCB_UNLOCK(stcb);
3157                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3158                                                 break;
3159                                         }
3160                                 } else
3161 #endif
3162 #ifdef INET6
3163                                 if (sa->sa_family == AF_INET6) {
3164                                         struct sockaddr_in6 *sin6;
3165
3166                                         sin6 = (struct sockaddr_in6 *)sa;
3167                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3168                                                 error = EINVAL;
3169                                                 SCTP_TCB_UNLOCK(stcb);
3170                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3171                                                 break;
3172                                         }
3173                                 } else
3174 #endif
3175                                 {
3176                                         error = EAFNOSUPPORT;
3177                                         SCTP_TCB_UNLOCK(stcb);
3178                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3179                                         break;
3180                                 }
3181                         }
3182                         if (stcb) {
3183                                 if (net) {
3184                                         thlds->spt_pathmaxrxt = net->failure_threshold;
3185                                         thlds->spt_pathpfthld = net->pf_threshold;
3186                                 } else {
3187                                         thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3188                                         thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3189                                 }
3190                                 thlds->spt_assoc_id = sctp_get_associd(stcb);
3191                                 SCTP_TCB_UNLOCK(stcb);
3192                         } else {
3193                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3194                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3195                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3196                                         /* Use endpoint defaults */
3197                                         SCTP_INP_RLOCK(inp);
3198                                         thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3199                                         thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3200                                         SCTP_INP_RUNLOCK(inp);
3201                                 } else {
3202                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3203                                         error = EINVAL;
3204                                 }
3205                         }
3206                         if (error == 0) {
3207                                 *optsize = sizeof(struct sctp_paddrthlds);
3208                         }
3209                         break;
3210                 }
3211         default:
3212                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3213                 error = ENOPROTOOPT;
3214                 break;
3215         }                       /* end switch (sopt->sopt_name) */
3216         if (error) {
3217                 *optsize = 0;
3218         }
3219         return (error);
3220 }
3221
3222 static int
3223 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
3224     void *p)
3225 {
3226         int error, set_opt;
3227         uint32_t *mopt;
3228         struct sctp_tcb *stcb = NULL;
3229         struct sctp_inpcb *inp = NULL;
3230         uint32_t vrf_id;
3231
3232         if (optval == NULL) {
3233                 SCTP_PRINTF("optval is NULL\n");
3234                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3235                 return (EINVAL);
3236         }
3237         inp = (struct sctp_inpcb *)so->so_pcb;
3238         if (inp == 0) {
3239                 SCTP_PRINTF("inp is NULL?\n");
3240                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3241                 return EINVAL;
3242         }
3243         vrf_id = inp->def_vrf_id;
3244
3245         error = 0;
3246         switch (optname) {
3247         case SCTP_NODELAY:
3248         case SCTP_AUTOCLOSE:
3249         case SCTP_AUTO_ASCONF:
3250         case SCTP_EXPLICIT_EOR:
3251         case SCTP_DISABLE_FRAGMENTS:
3252         case SCTP_USE_EXT_RCVINFO:
3253         case SCTP_I_WANT_MAPPED_V4_ADDR:
3254                 /* copy in the option value */
3255                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3256                 set_opt = 0;
3257                 if (error)
3258                         break;
3259                 switch (optname) {
3260                 case SCTP_DISABLE_FRAGMENTS:
3261                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
3262                         break;
3263                 case SCTP_AUTO_ASCONF:
3264                         /*
3265                          * NOTE: we don't really support this flag
3266                          */
3267                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3268                                 /* only valid for bound all sockets */
3269                                 if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
3270                                     (*mopt != 0)) {
3271                                         /* forbidden by admin */
3272                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
3273                                         return (EPERM);
3274                                 }
3275                                 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
3276                         } else {
3277                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3278                                 return (EINVAL);
3279                         }
3280                         break;
3281                 case SCTP_EXPLICIT_EOR:
3282                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
3283                         break;
3284                 case SCTP_USE_EXT_RCVINFO:
3285                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
3286                         break;
3287                 case SCTP_I_WANT_MAPPED_V4_ADDR:
3288                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3289                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
3290                         } else {
3291                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3292                                 return (EINVAL);
3293                         }
3294                         break;
3295                 case SCTP_NODELAY:
3296                         set_opt = SCTP_PCB_FLAGS_NODELAY;
3297                         break;
3298                 case SCTP_AUTOCLOSE:
3299                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3300                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3301                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3302                                 return (EINVAL);
3303                         }
3304                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
3305                         /*
3306                          * The value is in ticks. Note this does not effect
3307                          * old associations, only new ones.
3308                          */
3309                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
3310                         break;
3311                 }
3312                 SCTP_INP_WLOCK(inp);
3313                 if (*mopt != 0) {
3314                         sctp_feature_on(inp, set_opt);
3315                 } else {
3316                         sctp_feature_off(inp, set_opt);
3317                 }
3318                 SCTP_INP_WUNLOCK(inp);
3319                 break;
3320         case SCTP_REUSE_PORT:
3321                 {
3322                         SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
3323                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3324                                 /* Can't set it after we are bound */
3325                                 error = EINVAL;
3326                                 break;
3327                         }
3328                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
3329                                 /* Can't do this for a 1-m socket */
3330                                 error = EINVAL;
3331                                 break;
3332                         }
3333                         if (optval)
3334                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
3335                         else
3336                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
3337                         break;
3338                 }
3339         case SCTP_PARTIAL_DELIVERY_POINT:
3340                 {
3341                         uint32_t *value;
3342
3343                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
3344                         if (*value > SCTP_SB_LIMIT_RCV(so)) {
3345                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3346                                 error = EINVAL;
3347                                 break;
3348                         }
3349                         inp->partial_delivery_point = *value;
3350                         break;
3351                 }
3352         case SCTP_FRAGMENT_INTERLEAVE:
3353                 /* not yet until we re-write sctp_recvmsg() */
3354                 {
3355                         uint32_t *level;
3356
3357                         SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
3358                         if (*level == SCTP_FRAG_LEVEL_2) {
3359                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3360                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3361                         } else if (*level == SCTP_FRAG_LEVEL_1) {
3362                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3363                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3364                         } else if (*level == SCTP_FRAG_LEVEL_0) {
3365                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
3366                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
3367
3368                         } else {
3369                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3370                                 error = EINVAL;
3371                         }
3372                         break;
3373                 }
3374         case SCTP_CMT_ON_OFF:
3375                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3376                         struct sctp_assoc_value *av;
3377
3378                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3379                         if (av->assoc_value > SCTP_CMT_MAX) {
3380                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3381                                 error = EINVAL;
3382                                 break;
3383                         }
3384                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3385                         if (stcb) {
3386                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3387                                 SCTP_TCB_UNLOCK(stcb);
3388                         } else {
3389                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3390                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3391                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3392                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3393                                         SCTP_INP_WLOCK(inp);
3394                                         inp->sctp_cmt_on_off = av->assoc_value;
3395                                         SCTP_INP_WUNLOCK(inp);
3396                                 }
3397                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3398                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3399                                         SCTP_INP_RLOCK(inp);
3400                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3401                                                 SCTP_TCB_LOCK(stcb);
3402                                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
3403                                                 SCTP_TCB_UNLOCK(stcb);
3404                                         }
3405                                         SCTP_INP_RUNLOCK(inp);
3406                                 }
3407                         }
3408                 } else {
3409                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
3410                         error = ENOPROTOOPT;
3411                 }
3412                 break;
3413         case SCTP_PLUGGABLE_CC:
3414                 {
3415                         struct sctp_assoc_value *av;
3416                         struct sctp_nets *net;
3417
3418                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3419                         if ((av->assoc_value != SCTP_CC_RFC2581) &&
3420                             (av->assoc_value != SCTP_CC_HSTCP) &&
3421                             (av->assoc_value != SCTP_CC_HTCP) &&
3422                             (av->assoc_value != SCTP_CC_RTCC)) {
3423                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3424                                 error = EINVAL;
3425                                 break;
3426                         }
3427                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3428                         if (stcb) {
3429                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3430                                 stcb->asoc.congestion_control_module = av->assoc_value;
3431                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3432                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3433                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3434                                         }
3435                                 }
3436                                 SCTP_TCB_UNLOCK(stcb);
3437                         } else {
3438                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3439                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3440                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3441                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3442                                         SCTP_INP_WLOCK(inp);
3443                                         inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
3444                                         SCTP_INP_WUNLOCK(inp);
3445                                 }
3446                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3447                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3448                                         SCTP_INP_RLOCK(inp);
3449                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3450                                                 SCTP_TCB_LOCK(stcb);
3451                                                 stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
3452                                                 stcb->asoc.congestion_control_module = av->assoc_value;
3453                                                 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
3454                                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3455                                                                 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3456                                                         }
3457                                                 }
3458                                                 SCTP_TCB_UNLOCK(stcb);
3459                                         }
3460                                         SCTP_INP_RUNLOCK(inp);
3461                                 }
3462                         }
3463                         break;
3464                 }
3465         case SCTP_CC_OPTION:
3466                 {
3467                         struct sctp_cc_option *cc_opt;
3468
3469                         SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
3470                         SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
3471                         if (stcb == NULL) {
3472                                 if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
3473                                         SCTP_INP_RLOCK(inp);
3474                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3475                                                 SCTP_TCB_LOCK(stcb);
3476                                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
3477                                                         (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1, cc_opt);
3478                                                 }
3479                                                 SCTP_TCB_UNLOCK(stcb);
3480                                         }
3481                                         SCTP_INP_RUNLOCK(inp);
3482                                 } else {
3483                                         error = EINVAL;
3484                                 }
3485                         } else {
3486                                 if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
3487                                         error = ENOTSUP;
3488                                 } else {
3489                                         error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option) (stcb, 1,
3490                                             cc_opt);
3491                                 }
3492                                 SCTP_TCB_UNLOCK(stcb);
3493                         }
3494                         break;
3495                 }
3496         case SCTP_PLUGGABLE_SS:
3497                 {
3498                         struct sctp_assoc_value *av;
3499
3500                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3501                         if ((av->assoc_value != SCTP_SS_DEFAULT) &&
3502                             (av->assoc_value != SCTP_SS_DEFAULT) &&
3503                             (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
3504                             (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
3505                             (av->assoc_value != SCTP_SS_PRIORITY) &&
3506                             (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
3507                             (av->assoc_value != SCTP_SS_FIRST_COME)) {
3508                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3509                                 error = EINVAL;
3510                                 break;
3511                         }
3512                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3513                         if (stcb) {
3514                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
3515                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
3516                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
3517                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3518                                 SCTP_TCB_UNLOCK(stcb);
3519                         } else {
3520                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3521                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3522                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3523                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3524                                         SCTP_INP_WLOCK(inp);
3525                                         inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
3526                                         SCTP_INP_WUNLOCK(inp);
3527                                 }
3528                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3529                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3530                                         SCTP_INP_RLOCK(inp);
3531                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3532                                                 SCTP_TCB_LOCK(stcb);
3533                                                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
3534                                                 stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
3535                                                 stcb->asoc.stream_scheduling_module = av->assoc_value;
3536                                                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
3537                                                 SCTP_TCB_UNLOCK(stcb);
3538                                         }
3539                                         SCTP_INP_RUNLOCK(inp);
3540                                 }
3541                         }
3542                         break;
3543                 }
3544         case SCTP_SS_VALUE:
3545                 {
3546                         struct sctp_stream_value *av;
3547
3548                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
3549                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3550                         if (stcb) {
3551                                 if (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
3552                                     av->stream_value) < 0) {
3553                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3554                                         error = EINVAL;
3555                                 }
3556                                 SCTP_TCB_UNLOCK(stcb);
3557                         } else {
3558                                 if (av->assoc_id == SCTP_CURRENT_ASSOC) {
3559                                         SCTP_INP_RLOCK(inp);
3560                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3561                                                 SCTP_TCB_LOCK(stcb);
3562                                                 stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
3563                                                     &stcb->asoc,
3564                                                     &stcb->asoc.strmout[av->stream_id],
3565                                                     av->stream_value);
3566                                                 SCTP_TCB_UNLOCK(stcb);
3567                                         }
3568                                         SCTP_INP_RUNLOCK(inp);
3569
3570                                 } else {
3571                                         /*
3572                                          * Can't set stream value without
3573                                          * association
3574                                          */
3575                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3576                                         error = EINVAL;
3577                                 }
3578                         }
3579                         break;
3580                 }
3581         case SCTP_CLR_STAT_LOG:
3582                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3583                 error = EOPNOTSUPP;
3584                 break;
3585         case SCTP_CONTEXT:
3586                 {
3587                         struct sctp_assoc_value *av;
3588
3589                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
3590                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
3591
3592                         if (stcb) {
3593                                 stcb->asoc.context = av->assoc_value;
3594                                 SCTP_TCB_UNLOCK(stcb);
3595                         } else {
3596                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3597                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3598                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
3599                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3600                                         SCTP_INP_WLOCK(inp);
3601                                         inp->sctp_context = av->assoc_value;
3602                                         SCTP_INP_WUNLOCK(inp);
3603                                 }
3604                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
3605                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
3606                                         SCTP_INP_RLOCK(inp);
3607                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3608                                                 SCTP_TCB_LOCK(stcb);
3609                                                 stcb->asoc.context = av->assoc_value;
3610                                                 SCTP_TCB_UNLOCK(stcb);
3611                                         }
3612                                         SCTP_INP_RUNLOCK(inp);
3613                                 }
3614                         }
3615                         break;
3616                 }
3617         case SCTP_VRF_ID:
3618                 {
3619                         uint32_t *default_vrfid;
3620
3621                         SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
3622                         if (*default_vrfid > SCTP_MAX_VRF_ID) {
3623                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3624                                 error = EINVAL;
3625                                 break;
3626                         }
3627                         inp->def_vrf_id = *default_vrfid;
3628                         break;
3629                 }
3630         case SCTP_DEL_VRF_ID:
3631                 {
3632                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3633                         error = EOPNOTSUPP;
3634                         break;
3635                 }
3636         case SCTP_ADD_VRF_ID:
3637                 {
3638                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
3639                         error = EOPNOTSUPP;
3640                         break;
3641                 }
3642         case SCTP_DELAYED_SACK:
3643                 {
3644                         struct sctp_sack_info *sack;
3645
3646                         SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
3647                         SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
3648                         if (sack->sack_delay) {
3649                                 if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
3650                                         sack->sack_delay = SCTP_MAX_SACK_DELAY;
3651                                 if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
3652                                         sack->sack_delay = TICKS_TO_MSEC(1);
3653                                 }
3654                         }
3655                         if (stcb) {
3656                                 if (sack->sack_delay) {
3657                                         stcb->asoc.delayed_ack = sack->sack_delay;
3658                                 }
3659                                 if (sack->sack_freq) {
3660                                         stcb->asoc.sack_freq = sack->sack_freq;
3661                                 }
3662                                 SCTP_TCB_UNLOCK(stcb);
3663                         } else {
3664                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3665                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3666                                     (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
3667                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
3668                                         SCTP_INP_WLOCK(inp);
3669                                         if (sack->sack_delay) {
3670                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
3671                                         }
3672                                         if (sack->sack_freq) {
3673                                                 inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
3674                                         }
3675                                         SCTP_INP_WUNLOCK(inp);
3676                                 }
3677                                 if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
3678                                     (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
3679                                         SCTP_INP_RLOCK(inp);
3680                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3681                                                 SCTP_TCB_LOCK(stcb);
3682                                                 if (sack->sack_delay) {
3683                                                         stcb->asoc.delayed_ack = sack->sack_delay;
3684                                                 }
3685                                                 if (sack->sack_freq) {
3686                                                         stcb->asoc.sack_freq = sack->sack_freq;
3687                                                 }
3688                                                 SCTP_TCB_UNLOCK(stcb);
3689                                         }
3690                                         SCTP_INP_RUNLOCK(inp);
3691                                 }
3692                         }
3693                         break;
3694                 }
3695         case SCTP_AUTH_CHUNK:
3696                 {
3697                         struct sctp_authchunk *sauth;
3698
3699                         SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
3700
3701                         SCTP_INP_WLOCK(inp);
3702                         if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
3703                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3704                                 error = EINVAL;
3705                         }
3706                         SCTP_INP_WUNLOCK(inp);
3707                         break;
3708                 }
3709         case SCTP_AUTH_KEY:
3710                 {
3711                         struct sctp_authkey *sca;
3712                         struct sctp_keyhead *shared_keys;
3713                         sctp_sharedkey_t *shared_key;
3714                         sctp_key_t *key = NULL;
3715                         size_t size;
3716
3717                         SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
3718                         if (sca->sca_keylength == 0) {
3719                                 size = optsize - sizeof(struct sctp_authkey);
3720                         } else {
3721                                 if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
3722                                         size = sca->sca_keylength;
3723                                 } else {
3724                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3725                                         error = EINVAL;
3726                                         break;
3727                                 }
3728                         }
3729                         SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
3730
3731                         if (stcb) {
3732                                 shared_keys = &stcb->asoc.shared_keys;
3733                                 /* clear the cached keys for this key id */
3734                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3735                                 /*
3736                                  * create the new shared key and
3737                                  * insert/replace it
3738                                  */
3739                                 if (size > 0) {
3740                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
3741                                         if (key == NULL) {
3742                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3743                                                 error = ENOMEM;
3744                                                 SCTP_TCB_UNLOCK(stcb);
3745                                                 break;
3746                                         }
3747                                 }
3748                                 shared_key = sctp_alloc_sharedkey();
3749                                 if (shared_key == NULL) {
3750                                         sctp_free_key(key);
3751                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3752                                         error = ENOMEM;
3753                                         SCTP_TCB_UNLOCK(stcb);
3754                                         break;
3755                                 }
3756                                 shared_key->key = key;
3757                                 shared_key->keyid = sca->sca_keynumber;
3758                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
3759                                 SCTP_TCB_UNLOCK(stcb);
3760                         } else {
3761                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3762                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3763                                     (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
3764                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
3765                                         SCTP_INP_WLOCK(inp);
3766                                         shared_keys = &inp->sctp_ep.shared_keys;
3767                                         /*
3768                                          * clear the cached keys on all
3769                                          * assocs for this key id
3770                                          */
3771                                         sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
3772                                         /*
3773                                          * create the new shared key and
3774                                          * insert/replace it
3775                                          */
3776                                         if (size > 0) {
3777                                                 key = sctp_set_key(sca->sca_key, (uint32_t) size);
3778                                                 if (key == NULL) {
3779                                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3780                                                         error = ENOMEM;
3781                                                         SCTP_INP_WUNLOCK(inp);
3782                                                         break;
3783                                                 }
3784                                         }
3785                                         shared_key = sctp_alloc_sharedkey();
3786                                         if (shared_key == NULL) {
3787                                                 sctp_free_key(key);
3788                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3789                                                 error = ENOMEM;
3790                                                 SCTP_INP_WUNLOCK(inp);
3791                                                 break;
3792                                         }
3793                                         shared_key->key = key;
3794                                         shared_key->keyid = sca->sca_keynumber;
3795                                         error = sctp_insert_sharedkey(shared_keys, shared_key);
3796                                         SCTP_INP_WUNLOCK(inp);
3797                                 }
3798                                 if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
3799                                     (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
3800                                         SCTP_INP_RLOCK(inp);
3801                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3802                                                 SCTP_TCB_LOCK(stcb);
3803                                                 shared_keys = &stcb->asoc.shared_keys;
3804                                                 /*
3805                                                  * clear the cached keys for
3806                                                  * this key id
3807                                                  */
3808                                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
3809                                                 /*
3810                                                  * create the new shared key
3811                                                  * and insert/replace it
3812                                                  */
3813                                                 if (size > 0) {
3814                                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
3815                                                         if (key == NULL) {
3816                                                                 SCTP_TCB_UNLOCK(stcb);
3817                                                                 continue;
3818                                                         }
3819                                                 }
3820                                                 shared_key = sctp_alloc_sharedkey();
3821                                                 if (shared_key == NULL) {
3822                                                         sctp_free_key(key);
3823                                                         SCTP_TCB_UNLOCK(stcb);
3824                                                         continue;
3825                                                 }
3826                                                 shared_key->key = key;
3827                                                 shared_key->keyid = sca->sca_keynumber;
3828                                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
3829                                                 SCTP_TCB_UNLOCK(stcb);
3830                                         }
3831                                         SCTP_INP_RUNLOCK(inp);
3832                                 }
3833                         }
3834                         break;
3835                 }
3836         case SCTP_HMAC_IDENT:
3837                 {
3838                         struct sctp_hmacalgo *shmac;
3839                         sctp_hmaclist_t *hmaclist;
3840                         uint16_t hmacid;
3841                         uint32_t i;
3842                         size_t found;
3843
3844                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
3845                         if (optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) {
3846                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3847                                 error = EINVAL;
3848                                 break;
3849                         }
3850                         hmaclist = sctp_alloc_hmaclist(shmac->shmac_number_of_idents);
3851                         if (hmaclist == NULL) {
3852                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
3853                                 error = ENOMEM;
3854                                 break;
3855                         }
3856                         for (i = 0; i < shmac->shmac_number_of_idents; i++) {
3857                                 hmacid = shmac->shmac_idents[i];
3858                                 if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
3859                                          /* invalid HMACs were found */ ;
3860                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3861                                         error = EINVAL;
3862                                         sctp_free_hmaclist(hmaclist);
3863                                         goto sctp_set_hmac_done;
3864                                 }
3865                         }
3866                         found = 0;
3867                         for (i = 0; i < hmaclist->num_algo; i++) {
3868                                 if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
3869                                         /* already in list */
3870                                         found = 1;
3871                                 }
3872                         }
3873                         if (!found) {
3874                                 sctp_free_hmaclist(hmaclist);
3875                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3876                                 error = EINVAL;
3877                                 break;
3878                         }
3879                         /* set it on the endpoint */
3880                         SCTP_INP_WLOCK(inp);
3881                         if (inp->sctp_ep.local_hmacs)
3882                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3883                         inp->sctp_ep.local_hmacs = hmaclist;
3884                         SCTP_INP_WUNLOCK(inp);
3885         sctp_set_hmac_done:
3886                         break;
3887                 }
3888         case SCTP_AUTH_ACTIVE_KEY:
3889                 {
3890                         struct sctp_authkeyid *scact;
3891
3892                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
3893                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3894
3895                         /* set the active key on the right place */
3896                         if (stcb) {
3897                                 /* set the active key on the assoc */
3898                                 if (sctp_auth_setactivekey(stcb,
3899                                     scact->scact_keynumber)) {
3900                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
3901                                             SCTP_FROM_SCTP_USRREQ,
3902                                             EINVAL);
3903                                         error = EINVAL;
3904                                 }
3905                                 SCTP_TCB_UNLOCK(stcb);
3906                         } else {
3907                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3908                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3909                                     (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
3910                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
3911                                         SCTP_INP_WLOCK(inp);
3912                                         if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
3913                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3914                                                 error = EINVAL;
3915                                         }
3916                                         SCTP_INP_WUNLOCK(inp);
3917                                 }
3918                                 if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
3919                                     (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
3920                                         SCTP_INP_RLOCK(inp);
3921                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3922                                                 SCTP_TCB_LOCK(stcb);
3923                                                 sctp_auth_setactivekey(stcb, scact->scact_keynumber);
3924                                                 SCTP_TCB_UNLOCK(stcb);
3925                                         }
3926                                         SCTP_INP_RUNLOCK(inp);
3927                                 }
3928                         }
3929                         break;
3930                 }
3931         case SCTP_AUTH_DELETE_KEY:
3932                 {
3933                         struct sctp_authkeyid *scdel;
3934
3935                         SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
3936                         SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
3937
3938                         /* delete the key from the right place */
3939                         if (stcb) {
3940                                 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
3941                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3942                                         error = EINVAL;
3943                                 }
3944                                 SCTP_TCB_UNLOCK(stcb);
3945                         } else {
3946                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3947                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3948                                     (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
3949                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
3950                                         SCTP_INP_WLOCK(inp);
3951                                         if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
3952                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3953                                                 error = EINVAL;
3954                                         }
3955                                         SCTP_INP_WUNLOCK(inp);
3956                                 }
3957                                 if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
3958                                     (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
3959                                         SCTP_INP_RLOCK(inp);
3960                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3961                                                 SCTP_TCB_LOCK(stcb);
3962                                                 sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
3963                                                 SCTP_TCB_UNLOCK(stcb);
3964                                         }
3965                                         SCTP_INP_RUNLOCK(inp);
3966                                 }
3967                         }
3968                         break;
3969                 }
3970         case SCTP_AUTH_DEACTIVATE_KEY:
3971                 {
3972                         struct sctp_authkeyid *keyid;
3973
3974                         SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
3975                         SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
3976
3977                         /* deactivate the key from the right place */
3978                         if (stcb) {
3979                                 if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
3980                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3981                                         error = EINVAL;
3982                                 }
3983                                 SCTP_TCB_UNLOCK(stcb);
3984                         } else {
3985                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3986                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3987                                     (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
3988                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
3989                                         SCTP_INP_WLOCK(inp);
3990                                         if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
3991                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3992                                                 error = EINVAL;
3993                                         }
3994                                         SCTP_INP_WUNLOCK(inp);
3995                                 }
3996                                 if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
3997                                     (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
3998                                         SCTP_INP_RLOCK(inp);
3999                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4000                                                 SCTP_TCB_LOCK(stcb);
4001                                                 sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
4002                                                 SCTP_TCB_UNLOCK(stcb);
4003                                         }
4004                                         SCTP_INP_RUNLOCK(inp);
4005                                 }
4006                         }
4007                         break;
4008                 }
4009
4010         case SCTP_RESET_STREAMS:
4011                 {
4012                         struct sctp_stream_reset *strrst;
4013                         uint8_t send_in = 0, send_tsn = 0, send_out = 0,
4014                                 addstream = 0;
4015                         uint16_t addstrmcnt = 0;
4016                         int i;
4017
4018                         SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
4019                         SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
4020
4021                         if (stcb == NULL) {
4022                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4023                                 error = ENOENT;
4024                                 break;
4025                         }
4026                         if (stcb->asoc.peer_supports_strreset == 0) {
4027                                 /*
4028                                  * Peer does not support it, we return
4029                                  * protocol not supported since this is true
4030                                  * for this feature and this peer, not the
4031                                  * socket request in general.
4032                                  */
4033                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPROTONOSUPPORT);
4034                                 error = EPROTONOSUPPORT;
4035                                 SCTP_TCB_UNLOCK(stcb);
4036                                 break;
4037                         }
4038                         if (stcb->asoc.stream_reset_outstanding) {
4039                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4040                                 error = EALREADY;
4041                                 SCTP_TCB_UNLOCK(stcb);
4042                                 break;
4043                         }
4044                         if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
4045                                 send_in = 1;
4046                         } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
4047                                 send_out = 1;
4048                         } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
4049                                 send_in = 1;
4050                                 send_out = 1;
4051                         } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
4052                                 send_tsn = 1;
4053                         } else if (strrst->strrst_flags == SCTP_RESET_ADD_STREAMS) {
4054                                 if (send_tsn ||
4055                                     send_in ||
4056                                     send_out) {
4057                                         /* We can't do that and add streams */
4058                                         error = EINVAL;
4059                                         goto skip_stuff;
4060                                 }
4061                                 if (stcb->asoc.stream_reset_outstanding) {
4062                                         error = EBUSY;
4063                                         goto skip_stuff;
4064                                 }
4065                                 addstream = 1;
4066                                 /* We allocate here */
4067                                 addstrmcnt = strrst->strrst_num_streams;
4068                                 if ((int)(addstrmcnt + stcb->asoc.streamoutcnt) > 0xffff) {
4069                                         /* You can't have more than 64k */
4070                                         error = EINVAL;
4071                                         goto skip_stuff;
4072                                 }
4073                                 if ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < addstrmcnt) {
4074                                         /* Need to allocate more */
4075                                         struct sctp_stream_out *oldstream;
4076                                         struct sctp_stream_queue_pending *sp,
4077                                                                  *nsp;
4078
4079                                         oldstream = stcb->asoc.strmout;
4080                                         /* get some more */
4081                                         SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
4082                                             ((stcb->asoc.streamoutcnt + addstrmcnt) * sizeof(struct sctp_stream_out)),
4083                                             SCTP_M_STRMO);
4084                                         if (stcb->asoc.strmout == NULL) {
4085                                                 stcb->asoc.strmout = oldstream;
4086                                                 error = ENOMEM;
4087                                                 goto skip_stuff;
4088                                         }
4089                                         /*
4090                                          * Ok now we proceed with copying
4091                                          * the old out stuff and
4092                                          * initializing the new stuff.
4093                                          */
4094                                         SCTP_TCB_SEND_LOCK(stcb);
4095                                         stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1);
4096                                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
4097                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
4098                                                 stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
4099                                                 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
4100                                                 stcb->asoc.strmout[i].stream_no = i;
4101                                                 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]);
4102                                                 /*
4103                                                  * now anything on those
4104                                                  * queues?
4105                                                  */
4106                                                 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
4107                                                         TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
4108                                                         TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
4109                                                 }
4110                                                 /*
4111                                                  * Now move assoc pointers
4112                                                  * too
4113                                                  */
4114                                                 if (stcb->asoc.last_out_stream == &oldstream[i]) {
4115                                                         stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
4116                                                 }
4117                                                 if (stcb->asoc.locked_on_sending == &oldstream[i]) {
4118                                                         stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
4119                                                 }
4120                                         }
4121                                         /* now the new streams */
4122                                         stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4123                                         for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + addstrmcnt); i++) {
4124                                                 stcb->asoc.strmout[i].next_sequence_sent = 0x0;
4125                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
4126                                                 stcb->asoc.strmout[i].stream_no = i;
4127                                                 stcb->asoc.strmout[i].last_msg_incomplete = 0;
4128                                                 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
4129                                         }
4130                                         stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + addstrmcnt;
4131                                         SCTP_FREE(oldstream, SCTP_M_STRMO);
4132                                 }
4133                                 SCTP_TCB_SEND_UNLOCK(stcb);
4134                                 goto skip_stuff;
4135                         } else {
4136                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4137                                 error = EINVAL;
4138                                 SCTP_TCB_UNLOCK(stcb);
4139                                 break;
4140                         }
4141                         for (i = 0; i < strrst->strrst_num_streams; i++) {
4142                                 if ((send_in) &&
4143
4144                                     (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
4145                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4146                                         error = EINVAL;
4147                                         goto get_out;
4148                                 }
4149                                 if ((send_out) &&
4150                                     (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
4151                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4152                                         error = EINVAL;
4153                                         goto get_out;
4154                                 }
4155                         }
4156         skip_stuff:
4157                         if (error) {
4158                 get_out:
4159                                 SCTP_TCB_UNLOCK(stcb);
4160                                 break;
4161                         }
4162                         error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
4163                             strrst->strrst_list,
4164                             send_out, (stcb->asoc.str_reset_seq_in - 3),
4165                             send_in, send_tsn, addstream, addstrmcnt);
4166
4167                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
4168                         SCTP_TCB_UNLOCK(stcb);
4169                         break;
4170                 }
4171         case SCTP_CONNECT_X:
4172                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4173                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4174                         error = EINVAL;
4175                         break;
4176                 }
4177                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
4178                 break;
4179         case SCTP_CONNECT_X_DELAYED:
4180                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
4181                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4182                         error = EINVAL;
4183                         break;
4184                 }
4185                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
4186                 break;
4187         case SCTP_CONNECT_X_COMPLETE:
4188                 {
4189                         struct sockaddr *sa;
4190                         struct sctp_nets *net;
4191
4192                         /* FIXME MT: check correct? */
4193                         SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
4194
4195                         /* find tcb */
4196                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4197                                 SCTP_INP_RLOCK(inp);
4198                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4199                                 if (stcb) {
4200                                         SCTP_TCB_LOCK(stcb);
4201                                         net = sctp_findnet(stcb, sa);
4202                                 }
4203                                 SCTP_INP_RUNLOCK(inp);
4204                         } else {
4205                                 /*
4206                                  * We increment here since
4207                                  * sctp_findassociation_ep_addr() wil do a
4208                                  * decrement if it finds the stcb as long as
4209                                  * the locked tcb (last argument) is NOT a
4210                                  * TCB.. aka NULL.
4211                                  */
4212                                 SCTP_INP_INCR_REF(inp);
4213                                 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
4214                                 if (stcb == NULL) {
4215                                         SCTP_INP_DECR_REF(inp);
4216                                 }
4217                         }
4218
4219                         if (stcb == NULL) {
4220                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
4221                                 error = ENOENT;
4222                                 break;
4223                         }
4224                         if (stcb->asoc.delayed_connection == 1) {
4225                                 stcb->asoc.delayed_connection = 0;
4226                                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
4227                                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
4228                                     stcb->asoc.primary_destination,
4229                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
4230                                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
4231                         } else {
4232                                 /*
4233                                  * already expired or did not use delayed
4234                                  * connectx
4235                                  */
4236                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4237                                 error = EALREADY;
4238                         }
4239                         SCTP_TCB_UNLOCK(stcb);
4240                         break;
4241                 }
4242         case SCTP_MAX_BURST:
4243                 {
4244                         struct sctp_assoc_value *av;
4245
4246                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4247                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4248
4249                         if (stcb) {
4250                                 stcb->asoc.max_burst = av->assoc_value;
4251                                 SCTP_TCB_UNLOCK(stcb);
4252                         } else {
4253                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4254                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4255                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4256                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4257                                         SCTP_INP_WLOCK(inp);
4258                                         inp->sctp_ep.max_burst = av->assoc_value;
4259                                         SCTP_INP_WUNLOCK(inp);
4260                                 }
4261                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4262                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4263                                         SCTP_INP_RLOCK(inp);
4264                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4265                                                 SCTP_TCB_LOCK(stcb);
4266                                                 stcb->asoc.max_burst = av->assoc_value;
4267                                                 SCTP_TCB_UNLOCK(stcb);
4268                                         }
4269                                         SCTP_INP_RUNLOCK(inp);
4270                                 }
4271                         }
4272                         break;
4273                 }
4274         case SCTP_MAXSEG:
4275                 {
4276                         struct sctp_assoc_value *av;
4277                         int ovh;
4278
4279                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4280                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4281
4282                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4283                                 ovh = SCTP_MED_OVERHEAD;
4284                         } else {
4285                                 ovh = SCTP_MED_V4_OVERHEAD;
4286                         }
4287                         if (stcb) {
4288                                 if (av->assoc_value) {
4289                                         stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
4290                                 } else {
4291                                         stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4292                                 }
4293                                 SCTP_TCB_UNLOCK(stcb);
4294                         } else {
4295                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4296                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4297                                     (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4298                                         SCTP_INP_WLOCK(inp);
4299                                         /*
4300                                          * FIXME MT: I think this is not in
4301                                          * tune with the API ID
4302                                          */
4303                                         if (av->assoc_value) {
4304                                                 inp->sctp_frag_point = (av->assoc_value + ovh);
4305                                         } else {
4306                                                 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
4307                                         }
4308                                         SCTP_INP_WUNLOCK(inp);
4309                                 } else {
4310                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4311                                         error = EINVAL;
4312                                 }
4313                         }
4314                         break;
4315                 }
4316         case SCTP_EVENTS:
4317                 {
4318                         struct sctp_event_subscribe *events;
4319
4320                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
4321
4322                         SCTP_INP_WLOCK(inp);
4323                         if (events->sctp_data_io_event) {
4324                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4325                         } else {
4326                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
4327                         }
4328
4329                         if (events->sctp_association_event) {
4330                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4331                         } else {
4332                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4333                         }
4334
4335                         if (events->sctp_address_event) {
4336                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4337                         } else {
4338                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
4339                         }
4340
4341                         if (events->sctp_send_failure_event) {
4342                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4343                         } else {
4344                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4345                         }
4346
4347                         if (events->sctp_peer_error_event) {
4348                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4349                         } else {
4350                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
4351                         }
4352
4353                         if (events->sctp_shutdown_event) {
4354                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4355                         } else {
4356                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4357                         }
4358
4359                         if (events->sctp_partial_delivery_event) {
4360                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4361                         } else {
4362                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
4363                         }
4364
4365                         if (events->sctp_adaptation_layer_event) {
4366                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4367                         } else {
4368                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4369                         }
4370
4371                         if (events->sctp_authentication_event) {
4372                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4373                         } else {
4374                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
4375                         }
4376
4377                         if (events->sctp_sender_dry_event) {
4378                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
4379                         } else {
4380                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
4381                         }
4382
4383                         if (events->sctp_stream_reset_event) {
4384                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4385                         } else {
4386                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4387                         }
4388                         SCTP_INP_WUNLOCK(inp);
4389
4390                         SCTP_INP_RLOCK(inp);
4391                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4392                                 SCTP_TCB_LOCK(stcb);
4393                                 if (events->sctp_association_event) {
4394                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4395                                 } else {
4396                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
4397                                 }
4398                                 if (events->sctp_address_event) {
4399                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
4400                                 } else {
4401                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
4402                                 }
4403                                 if (events->sctp_send_failure_event) {
4404                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4405                                 } else {
4406                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
4407                                 }
4408                                 if (events->sctp_peer_error_event) {
4409                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
4410                                 } else {
4411                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
4412                                 }
4413                                 if (events->sctp_shutdown_event) {
4414                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4415                                 } else {
4416                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
4417                                 }
4418                                 if (events->sctp_partial_delivery_event) {
4419                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
4420                                 } else {
4421                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
4422                                 }
4423                                 if (events->sctp_adaptation_layer_event) {
4424                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4425                                 } else {
4426                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
4427                                 }
4428                                 if (events->sctp_authentication_event) {
4429                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
4430                                 } else {
4431                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
4432                                 }
4433                                 if (events->sctp_sender_dry_event) {
4434                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
4435                                 } else {
4436                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
4437                                 }
4438                                 if (events->sctp_stream_reset_event) {
4439                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4440                                 } else {
4441                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
4442                                 }
4443                                 SCTP_TCB_UNLOCK(stcb);
4444                         }
4445                         /*
4446                          * Send up the sender dry event only for 1-to-1
4447                          * style sockets.
4448                          */
4449                         if (events->sctp_sender_dry_event) {
4450                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4451                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4452                                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
4453                                         if (stcb) {
4454                                                 SCTP_TCB_LOCK(stcb);
4455                                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4456                                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4457                                                     (stcb->asoc.stream_queue_cnt == 0)) {
4458                                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
4459                                                 }
4460                                                 SCTP_TCB_UNLOCK(stcb);
4461                                         }
4462                                 }
4463                         }
4464                         SCTP_INP_RUNLOCK(inp);
4465                         break;
4466                 }
4467         case SCTP_ADAPTATION_LAYER:
4468                 {
4469                         struct sctp_setadaptation *adap_bits;
4470
4471                         SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
4472                         SCTP_INP_WLOCK(inp);
4473                         inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
4474                         SCTP_INP_WUNLOCK(inp);
4475                         break;
4476                 }
4477 #ifdef SCTP_DEBUG
4478         case SCTP_SET_INITIAL_DBG_SEQ:
4479                 {
4480                         uint32_t *vvv;
4481
4482                         SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
4483                         SCTP_INP_WLOCK(inp);
4484                         inp->sctp_ep.initial_sequence_debug = *vvv;
4485                         SCTP_INP_WUNLOCK(inp);
4486                         break;
4487                 }
4488 #endif
4489         case SCTP_DEFAULT_SEND_PARAM:
4490                 {
4491                         struct sctp_sndrcvinfo *s_info;
4492
4493                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
4494                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
4495
4496                         if (stcb) {
4497                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
4498                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
4499                                 } else {
4500                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4501                                         error = EINVAL;
4502                                 }
4503                                 SCTP_TCB_UNLOCK(stcb);
4504                         } else {
4505                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4506                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4507                                     (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
4508                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
4509                                         SCTP_INP_WLOCK(inp);
4510                                         memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
4511                                         SCTP_INP_WUNLOCK(inp);
4512                                 }
4513                                 if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
4514                                     (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
4515                                         SCTP_INP_RLOCK(inp);
4516                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4517                                                 SCTP_TCB_LOCK(stcb);
4518                                                 if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
4519                                                         memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
4520                                                 }
4521                                                 SCTP_TCB_UNLOCK(stcb);
4522                                         }
4523                                         SCTP_INP_RUNLOCK(inp);
4524                                 }
4525                         }
4526                         break;
4527                 }
4528         case SCTP_PEER_ADDR_PARAMS:
4529                 /* Applies to the specific association */
4530                 {
4531                         struct sctp_paddrparams *paddrp;
4532                         struct sctp_nets *net;
4533
4534                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
4535                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
4536                         net = NULL;
4537                         if (stcb) {
4538                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
4539                         } else {
4540                                 /*
4541                                  * We increment here since
4542                                  * sctp_findassociation_ep_addr() wil do a
4543                                  * decrement if it finds the stcb as long as
4544                                  * the locked tcb (last argument) is NOT a
4545                                  * TCB.. aka NULL.
4546                                  */
4547                                 SCTP_INP_INCR_REF(inp);
4548                                 stcb = sctp_findassociation_ep_addr(&inp,
4549                                     (struct sockaddr *)&paddrp->spp_address,
4550                                     &net, NULL, NULL);
4551                                 if (stcb == NULL) {
4552                                         SCTP_INP_DECR_REF(inp);
4553                                 }
4554                         }
4555                         if (stcb && (net == NULL)) {
4556                                 struct sockaddr *sa;
4557
4558                                 sa = (struct sockaddr *)&paddrp->spp_address;
4559 #ifdef INET
4560                                 if (sa->sa_family == AF_INET) {
4561
4562                                         struct sockaddr_in *sin;
4563
4564                                         sin = (struct sockaddr_in *)sa;
4565                                         if (sin->sin_addr.s_addr) {
4566                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4567                                                 SCTP_TCB_UNLOCK(stcb);
4568                                                 error = EINVAL;
4569                                                 break;
4570                                         }
4571                                 } else
4572 #endif
4573 #ifdef INET6
4574                                 if (sa->sa_family == AF_INET6) {
4575                                         struct sockaddr_in6 *sin6;
4576
4577                                         sin6 = (struct sockaddr_in6 *)sa;
4578                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4579                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4580                                                 SCTP_TCB_UNLOCK(stcb);
4581                                                 error = EINVAL;
4582                                                 break;
4583                                         }
4584                                 } else
4585 #endif
4586                                 {
4587                                         error = EAFNOSUPPORT;
4588                                         SCTP_TCB_UNLOCK(stcb);
4589                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
4590                                         break;
4591                                 }
4592                         }
4593                         /* sanity checks */
4594                         if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
4595                                 if (stcb)
4596                                         SCTP_TCB_UNLOCK(stcb);
4597                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4598                                 return (EINVAL);
4599                         }
4600                         if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
4601                                 if (stcb)
4602                                         SCTP_TCB_UNLOCK(stcb);
4603                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4604                                 return (EINVAL);
4605                         }
4606                         if (stcb) {
4607                                 /************************TCB SPECIFIC SET ******************/
4608                                 /*
4609                                  * do we change the timer for HB, we run
4610                                  * only one?
4611                                  */
4612                                 int ovh = 0;
4613
4614                                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4615                                         ovh = SCTP_MED_OVERHEAD;
4616                                 } else {
4617                                         ovh = SCTP_MED_V4_OVERHEAD;
4618                                 }
4619
4620                                 /* network sets ? */
4621                                 if (net) {
4622                                         /************************NET SPECIFIC SET ******************/
4623                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
4624                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
4625                                                     !(net->dest_state & SCTP_ADDR_NOHB)) {
4626                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
4627                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4628                                                 }
4629                                                 net->dest_state |= SCTP_ADDR_NOHB;
4630                                         }
4631                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
4632                                                 if (paddrp->spp_hbinterval) {
4633                                                         net->heart_beat_delay = paddrp->spp_hbinterval;
4634                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
4635                                                         net->heart_beat_delay = 0;
4636                                                 }
4637                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
4638                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4639                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
4640                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
4641                                         }
4642                                         if (paddrp->spp_flags & SPP_HB_DEMAND) {
4643                                                 /* on demand HB */
4644                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
4645                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
4646                                         }
4647                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
4648                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4649                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
4650                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4651                                                 }
4652                                                 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4653                                                 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
4654                                                         net->mtu = paddrp->spp_pathmtu + ovh;
4655                                                         if (net->mtu < stcb->asoc.smallest_mtu) {
4656                                                                 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
4657                                                         }
4658                                                 }
4659                                         }
4660                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
4661                                                 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4662                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
4663                                                 }
4664                                                 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4665                                         }
4666                                         if (paddrp->spp_pathmaxrxt) {
4667                                                 if (net->dest_state & SCTP_ADDR_PF) {
4668                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
4669                                                                 net->dest_state &= ~SCTP_ADDR_PF;
4670                                                         }
4671                                                 } else {
4672                                                         if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
4673                                                             (net->error_count > net->pf_threshold)) {
4674                                                                 net->dest_state |= SCTP_ADDR_PF;
4675                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
4676                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
4677                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4678                                                         }
4679                                                 }
4680                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
4681                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
4682                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
4683                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
4684                                                         }
4685                                                 } else {
4686                                                         if (net->error_count <= paddrp->spp_pathmaxrxt) {
4687                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
4688                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
4689                                                         }
4690                                                 }
4691                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
4692                                         }
4693                                         if (paddrp->spp_flags & SPP_DSCP) {
4694                                                 net->dscp = paddrp->spp_dscp & 0xfc;
4695                                                 net->dscp |= 0x01;
4696                                         }
4697 #ifdef INET6
4698                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
4699                                                 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4700                                                         net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
4701                                                         net->flowlabel |= 0x80000000;
4702                                                 }
4703                                         }
4704 #endif
4705                                 } else {
4706                                         /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
4707                                         if (paddrp->spp_pathmaxrxt) {
4708                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
4709                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4710                                                         if (net->dest_state & SCTP_ADDR_PF) {
4711                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
4712                                                                         net->dest_state &= ~SCTP_ADDR_PF;
4713                                                                 }
4714                                                         } else {
4715                                                                 if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
4716                                                                     (net->error_count > net->pf_threshold)) {
4717                                                                         net->dest_state |= SCTP_ADDR_PF;
4718                                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
4719                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
4720                                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4721                                                                 }
4722                                                         }
4723                                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
4724                                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
4725                                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
4726                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
4727                                                                 }
4728                                                         } else {
4729                                                                 if (net->error_count <= paddrp->spp_pathmaxrxt) {
4730                                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
4731                                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
4732                                                                 }
4733                                                         }
4734                                                         net->failure_threshold = paddrp->spp_pathmaxrxt;
4735                                                 }
4736                                         }
4737                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
4738                                                 if (paddrp->spp_hbinterval) {
4739                                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
4740                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
4741                                                         stcb->asoc.heart_beat_delay = 0;
4742                                                 }
4743                                                 /* Turn back on the timer */
4744                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4745                                                         if (paddrp->spp_hbinterval) {
4746                                                                 net->heart_beat_delay = paddrp->spp_hbinterval;
4747                                                         } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
4748                                                                 net->heart_beat_delay = 0;
4749                                                         }
4750                                                         if (net->dest_state & SCTP_ADDR_NOHB) {
4751                                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
4752                                                         }
4753                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
4754                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4755                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
4756                                                 }
4757                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4758                                         }
4759                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
4760                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4761                                                         if (!(net->dest_state & SCTP_ADDR_NOHB)) {
4762                                                                 net->dest_state |= SCTP_ADDR_NOHB;
4763                                                                 if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
4764                                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4765                                                                 }
4766                                                         }
4767                                                 }
4768                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4769                                         }
4770                                         if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
4771                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4772                                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4773                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
4774                                                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
4775                                                         }
4776                                                         net->dest_state |= SCTP_ADDR_NO_PMTUD;
4777                                                         if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
4778                                                                 net->mtu = paddrp->spp_pathmtu + ovh;
4779                                                                 if (net->mtu < stcb->asoc.smallest_mtu) {
4780                                                                         sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
4781                                                                 }
4782                                                         }
4783                                                 }
4784                                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
4785                                         }
4786                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
4787                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4788                                                         if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
4789                                                                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
4790                                                         }
4791                                                         net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4792                                                 }
4793                                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
4794                                         }
4795                                         if (paddrp->spp_flags & SPP_DSCP) {
4796                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4797                                                         net->dscp = paddrp->spp_dscp & 0xfc;
4798                                                         net->dscp |= 0x01;
4799                                                 }
4800                                                 stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
4801                                                 stcb->asoc.default_dscp |= 0x01;
4802                                         }
4803 #ifdef INET6
4804                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
4805                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4806                                                         if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4807                                                                 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
4808                                                                 net->flowlabel |= 0x80000000;
4809                                                         }
4810                                                 }
4811                                                 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
4812                                                 stcb->asoc.default_flowlabel |= 0x80000000;
4813                                         }
4814 #endif
4815                                 }
4816                                 SCTP_TCB_UNLOCK(stcb);
4817                         } else {
4818                                 /************************NO TCB, SET TO default stuff ******************/
4819                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4820                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4821                                     (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
4822                                         SCTP_INP_WLOCK(inp);
4823                                         /*
4824                                          * For the TOS/FLOWLABEL stuff you
4825                                          * set it with the options on the
4826                                          * socket
4827                                          */
4828                                         if (paddrp->spp_pathmaxrxt) {
4829                                                 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
4830                                         }
4831                                         if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
4832                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
4833                                         else if (paddrp->spp_hbinterval) {
4834                                                 if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
4835                                                         paddrp->spp_hbinterval = SCTP_MAX_HB_INTERVAL;
4836                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
4837                                         }
4838                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
4839                                                 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
4840                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
4841                                                 } else if (paddrp->spp_hbinterval) {
4842                                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
4843                                                 }
4844                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4845                                         } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
4846                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
4847                                         }
4848                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
4849                                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
4850                                         } else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
4851                                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
4852                                         }
4853                                         if (paddrp->spp_flags & SPP_DSCP) {
4854                                                 inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
4855                                                 inp->sctp_ep.default_dscp |= 0x01;
4856                                         }
4857 #ifdef INET6
4858                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
4859                                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4860                                                         inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
4861                                                         inp->sctp_ep.default_flowlabel |= 0x80000000;
4862                                                 }
4863                                         }
4864 #endif
4865                                         SCTP_INP_WUNLOCK(inp);
4866                                 } else {
4867                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4868                                         error = EINVAL;
4869                                 }
4870                         }
4871                         break;
4872                 }
4873         case SCTP_RTOINFO:
4874                 {
4875                         struct sctp_rtoinfo *srto;
4876                         uint32_t new_init, new_min, new_max;
4877
4878                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
4879                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
4880
4881                         if (stcb) {
4882                                 if (srto->srto_initial)
4883                                         new_init = srto->srto_initial;
4884                                 else
4885                                         new_init = stcb->asoc.initial_rto;
4886                                 if (srto->srto_max)
4887                                         new_max = srto->srto_max;
4888                                 else
4889                                         new_max = stcb->asoc.maxrto;
4890                                 if (srto->srto_min)
4891                                         new_min = srto->srto_min;
4892                                 else
4893                                         new_min = stcb->asoc.minrto;
4894                                 if ((new_min <= new_init) && (new_init <= new_max)) {
4895                                         stcb->asoc.initial_rto = new_init;
4896                                         stcb->asoc.maxrto = new_max;
4897                                         stcb->asoc.minrto = new_min;
4898                                 } else {
4899                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4900                                         error = EINVAL;
4901                                 }
4902                                 SCTP_TCB_UNLOCK(stcb);
4903                         } else {
4904                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4905                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4906                                     (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
4907                                         SCTP_INP_WLOCK(inp);
4908                                         if (srto->srto_initial)
4909                                                 new_init = srto->srto_initial;
4910                                         else
4911                                                 new_init = inp->sctp_ep.initial_rto;
4912                                         if (srto->srto_max)
4913                                                 new_max = srto->srto_max;
4914                                         else
4915                                                 new_max = inp->sctp_ep.sctp_maxrto;
4916                                         if (srto->srto_min)
4917                                                 new_min = srto->srto_min;
4918                                         else
4919                                                 new_min = inp->sctp_ep.sctp_minrto;
4920                                         if ((new_min <= new_init) && (new_init <= new_max)) {
4921                                                 inp->sctp_ep.initial_rto = new_init;
4922                                                 inp->sctp_ep.sctp_maxrto = new_max;
4923                                                 inp->sctp_ep.sctp_minrto = new_min;
4924                                         } else {
4925                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4926                                                 error = EINVAL;
4927                                         }
4928                                         SCTP_INP_WUNLOCK(inp);
4929                                 } else {
4930                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4931                                         error = EINVAL;
4932                                 }
4933                         }
4934                         break;
4935                 }
4936         case SCTP_ASSOCINFO:
4937                 {
4938                         struct sctp_assocparams *sasoc;
4939
4940                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
4941                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
4942                         if (sasoc->sasoc_cookie_life) {
4943                                 /* boundary check the cookie life */
4944                                 if (sasoc->sasoc_cookie_life < 1000)
4945                                         sasoc->sasoc_cookie_life = 1000;
4946                                 if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
4947                                         sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
4948                                 }
4949                         }
4950                         if (stcb) {
4951                                 if (sasoc->sasoc_asocmaxrxt)
4952                                         stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
4953                                 if (sasoc->sasoc_cookie_life) {
4954                                         stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
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                                     (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
4961                                         SCTP_INP_WLOCK(inp);
4962                                         if (sasoc->sasoc_asocmaxrxt)
4963                                                 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
4964                                         if (sasoc->sasoc_cookie_life) {
4965                                                 inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
4966                                         }
4967                                         SCTP_INP_WUNLOCK(inp);
4968                                 } else {
4969                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4970                                         error = EINVAL;
4971                                 }
4972                         }
4973                         break;
4974                 }
4975         case SCTP_INITMSG:
4976                 {
4977                         struct sctp_initmsg *sinit;
4978
4979                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
4980                         SCTP_INP_WLOCK(inp);
4981                         if (sinit->sinit_num_ostreams)
4982                                 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
4983
4984                         if (sinit->sinit_max_instreams)
4985                                 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
4986
4987                         if (sinit->sinit_max_attempts)
4988                                 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
4989
4990                         if (sinit->sinit_max_init_timeo)
4991                                 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
4992                         SCTP_INP_WUNLOCK(inp);
4993                         break;
4994                 }
4995         case SCTP_PRIMARY_ADDR:
4996                 {
4997                         struct sctp_setprim *spa;
4998                         struct sctp_nets *net;
4999
5000                         SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
5001                         SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
5002
5003                         net = NULL;
5004                         if (stcb) {
5005                                 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
5006                         } else {
5007                                 /*
5008                                  * We increment here since
5009                                  * sctp_findassociation_ep_addr() wil do a
5010                                  * decrement if it finds the stcb as long as
5011                                  * the locked tcb (last argument) is NOT a
5012                                  * TCB.. aka NULL.
5013                                  */
5014                                 SCTP_INP_INCR_REF(inp);
5015                                 stcb = sctp_findassociation_ep_addr(&inp,
5016                                     (struct sockaddr *)&spa->ssp_addr,
5017                                     &net, NULL, NULL);
5018                                 if (stcb == NULL) {
5019                                         SCTP_INP_DECR_REF(inp);
5020                                 }
5021                         }
5022
5023                         if ((stcb) && (net)) {
5024                                 if ((net != stcb->asoc.primary_destination) &&
5025                                     (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
5026                                         /* Ok we need to set it */
5027                                         if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
5028                                                 if ((stcb->asoc.alternate) &&
5029                                                     (!(net->dest_state & SCTP_ADDR_PF)) &&
5030                                                     (net->dest_state & SCTP_ADDR_REACHABLE)) {
5031                                                         sctp_free_remote_addr(stcb->asoc.alternate);
5032                                                         stcb->asoc.alternate = NULL;
5033                                                 }
5034                                         }
5035                                 }
5036                         } else {
5037                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5038                                 error = EINVAL;
5039                         }
5040                         if (stcb) {
5041                                 SCTP_TCB_UNLOCK(stcb);
5042                         }
5043                         break;
5044                 }
5045         case SCTP_SET_DYNAMIC_PRIMARY:
5046                 {
5047                         union sctp_sockstore *ss;
5048
5049                         error = priv_check(curthread,
5050                             PRIV_NETINET_RESERVEDPORT);
5051                         if (error)
5052                                 break;
5053
5054                         SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
5055                         /* SUPER USER CHECK? */
5056                         error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
5057                         break;
5058                 }
5059         case SCTP_SET_PEER_PRIMARY_ADDR:
5060                 {
5061                         struct sctp_setpeerprim *sspp;
5062
5063                         SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
5064                         SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
5065                         if (stcb != NULL) {
5066                                 struct sctp_ifa *ifa;
5067
5068                                 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
5069                                     stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
5070                                 if (ifa == NULL) {
5071                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5072                                         error = EINVAL;
5073                                         goto out_of_it;
5074                                 }
5075                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5076                                         /*
5077                                          * Must validate the ifa found is in
5078                                          * our ep
5079                                          */
5080                                         struct sctp_laddr *laddr;
5081                                         int found = 0;
5082
5083                                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5084                                                 if (laddr->ifa == NULL) {
5085                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
5086                                                             __FUNCTION__);
5087                                                         continue;
5088                                                 }
5089                                                 if (laddr->ifa == ifa) {
5090                                                         found = 1;
5091                                                         break;
5092                                                 }
5093                                         }
5094                                         if (!found) {
5095                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5096                                                 error = EINVAL;
5097                                                 goto out_of_it;
5098                                         }
5099                                 }
5100                                 if (sctp_set_primary_ip_address_sa(stcb,
5101                                     (struct sockaddr *)&sspp->sspp_addr) != 0) {
5102                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5103                                         error = EINVAL;
5104                                 }
5105                 out_of_it:
5106                                 SCTP_TCB_UNLOCK(stcb);
5107                         } else {
5108                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5109                                 error = EINVAL;
5110                         }
5111                         break;
5112                 }
5113         case SCTP_BINDX_ADD_ADDR:
5114                 {
5115                         struct sctp_getaddresses *addrs;
5116                         size_t sz;
5117                         struct thread *td;
5118
5119                         td = (struct thread *)p;
5120                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
5121                             optsize);
5122 #ifdef INET
5123                         if (addrs->addr->sa_family == AF_INET) {
5124                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
5125                                 if (optsize < sz) {
5126                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5127                                         error = EINVAL;
5128                                         break;
5129                                 }
5130                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5131                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5132                                         break;
5133                                 }
5134                         } else
5135 #endif
5136 #ifdef INET6
5137                         if (addrs->addr->sa_family == AF_INET6) {
5138                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
5139                                 if (optsize < sz) {
5140                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5141                                         error = EINVAL;
5142                                         break;
5143                                 }
5144                                 if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5145                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5146                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5147                                         break;
5148                                 }
5149                         } else
5150 #endif
5151                         {
5152                                 error = EAFNOSUPPORT;
5153                                 break;
5154                         }
5155                         sctp_bindx_add_address(so, inp, addrs->addr,
5156                             addrs->sget_assoc_id, vrf_id,
5157                             &error, p);
5158                         break;
5159                 }
5160         case SCTP_BINDX_REM_ADDR:
5161                 {
5162                         struct sctp_getaddresses *addrs;
5163                         size_t sz;
5164                         struct thread *td;
5165
5166                         td = (struct thread *)p;
5167
5168                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
5169 #ifdef INET
5170                         if (addrs->addr->sa_family == AF_INET) {
5171                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in);
5172                                 if (optsize < sz) {
5173                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5174                                         error = EINVAL;
5175                                         break;
5176                                 }
5177                                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
5178                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5179                                         break;
5180                                 }
5181                         } else
5182 #endif
5183 #ifdef INET6
5184                         if (addrs->addr->sa_family == AF_INET6) {
5185                                 sz = sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6);
5186                                 if (optsize < sz) {
5187                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5188                                         error = EINVAL;
5189                                         break;
5190                                 }
5191                                 if (td != NULL &&
5192                                     (error = prison_local_ip6(td->td_ucred,
5193                                     &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
5194                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
5195                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
5196                                         break;
5197                                 }
5198                         } else
5199 #endif
5200                         {
5201                                 error = EAFNOSUPPORT;
5202                                 break;
5203                         }
5204                         sctp_bindx_delete_address(so, inp, addrs->addr,
5205                             addrs->sget_assoc_id, vrf_id,
5206                             &error);
5207                         break;
5208                 }
5209         case SCTP_EVENT:
5210                 {
5211                         struct sctp_event *event;
5212                         uint32_t event_type;
5213
5214                         SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
5215                         SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
5216                         switch (event->se_type) {
5217                         case SCTP_ASSOC_CHANGE:
5218                                 event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
5219                                 break;
5220                         case SCTP_PEER_ADDR_CHANGE:
5221                                 event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
5222                                 break;
5223                         case SCTP_REMOTE_ERROR:
5224                                 event_type = SCTP_PCB_FLAGS_RECVPEERERR;
5225                                 break;
5226                         case SCTP_SEND_FAILED:
5227                                 event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
5228                                 break;
5229                         case SCTP_SHUTDOWN_EVENT:
5230                                 event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
5231                                 break;
5232                         case SCTP_ADAPTATION_INDICATION:
5233                                 event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
5234                                 break;
5235                         case SCTP_PARTIAL_DELIVERY_EVENT:
5236                                 event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
5237                                 break;
5238                         case SCTP_AUTHENTICATION_EVENT:
5239                                 event_type = SCTP_PCB_FLAGS_AUTHEVNT;
5240                                 break;
5241                         case SCTP_STREAM_RESET_EVENT:
5242                                 event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
5243                                 break;
5244                         case SCTP_SENDER_DRY_EVENT:
5245                                 event_type = SCTP_PCB_FLAGS_DRYEVNT;
5246                                 break;
5247                         case SCTP_NOTIFICATIONS_STOPPED_EVENT:
5248                                 event_type = 0;
5249                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5250                                 error = ENOTSUP;
5251                                 break;
5252                         default:
5253                                 event_type = 0;
5254                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5255                                 error = EINVAL;
5256                                 break;
5257                         }
5258                         if (event_type > 0) {
5259                                 if (stcb) {
5260                                         if (event->se_on) {
5261                                                 sctp_stcb_feature_on(inp, stcb, event_type);
5262                                                 if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
5263                                                         if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5264                                                             TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5265                                                             (stcb->asoc.stream_queue_cnt == 0)) {
5266                                                                 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_LOCKED);
5267                                                         }
5268                                                 }
5269                                         } else {
5270                                                 sctp_stcb_feature_off(inp, stcb, event_type);
5271                                         }
5272                                         SCTP_TCB_UNLOCK(stcb);
5273                                 } else {
5274                                         /*
5275                                          * We don't want to send up a storm
5276                                          * of events, so return an error for
5277                                          * sender dry events
5278                                          */
5279                                         if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
5280                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
5281                                             ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
5282                                             ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
5283                                             (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
5284                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
5285                                                 error = ENOTSUP;
5286                                                 break;
5287                                         }
5288                                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5289                                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5290                                             (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
5291                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
5292                                                 SCTP_INP_WLOCK(inp);
5293                                                 if (event->se_on) {
5294                                                         sctp_feature_on(inp, event_type);
5295                                                 } else {
5296                                                         sctp_feature_off(inp, event_type);
5297                                                 }
5298                                                 SCTP_INP_WUNLOCK(inp);
5299                                         }
5300                                         if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
5301                                             (event->se_assoc_id == SCTP_ALL_ASSOC)) {
5302                                                 SCTP_INP_RLOCK(inp);
5303                                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5304                                                         SCTP_TCB_LOCK(stcb);
5305                                                         if (event->se_on) {
5306                                                                 sctp_stcb_feature_on(inp, stcb, event_type);
5307                                                         } else {
5308                                                                 sctp_stcb_feature_off(inp, stcb, event_type);
5309                                                         }
5310                                                         SCTP_TCB_UNLOCK(stcb);
5311                                                 }
5312                                                 SCTP_INP_RUNLOCK(inp);
5313                                         }
5314                                 }
5315                         }
5316                         break;
5317                 }
5318         case SCTP_RECVRCVINFO:
5319                 {
5320                         int *onoff;
5321
5322                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
5323                         SCTP_INP_WLOCK(inp);
5324                         if (*onoff != 0) {
5325                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
5326                         } else {
5327                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
5328                         }
5329                         SCTP_INP_WUNLOCK(inp);
5330                         break;
5331                 }
5332         case SCTP_RECVNXTINFO:
5333                 {
5334                         int *onoff;
5335
5336                         SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
5337                         SCTP_INP_WLOCK(inp);
5338                         if (*onoff != 0) {
5339                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
5340                         } else {
5341                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
5342                         }
5343                         SCTP_INP_WUNLOCK(inp);
5344                         break;
5345                 }
5346         case SCTP_DEFAULT_SNDINFO:
5347                 {
5348                         struct sctp_sndinfo *info;
5349                         uint16_t policy;
5350
5351                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
5352                         SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
5353
5354                         if (stcb) {
5355                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
5356                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
5357                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
5358                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
5359                                         stcb->asoc.def_send.sinfo_flags |= policy;
5360                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
5361                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
5362                                 } else {
5363                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5364                                         error = EINVAL;
5365                                 }
5366                                 SCTP_TCB_UNLOCK(stcb);
5367                         } else {
5368                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5369                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5370                                     (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
5371                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
5372                                         SCTP_INP_WLOCK(inp);
5373                                         inp->def_send.sinfo_stream = info->snd_sid;
5374                                         policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
5375                                         inp->def_send.sinfo_flags = info->snd_flags;
5376                                         inp->def_send.sinfo_flags |= policy;
5377                                         inp->def_send.sinfo_ppid = info->snd_ppid;
5378                                         inp->def_send.sinfo_context = info->snd_context;
5379                                         SCTP_INP_WUNLOCK(inp);
5380                                 }
5381                                 if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
5382                                     (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
5383                                         SCTP_INP_RLOCK(inp);
5384                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5385                                                 SCTP_TCB_LOCK(stcb);
5386                                                 if (info->snd_sid < stcb->asoc.streamoutcnt) {
5387                                                         stcb->asoc.def_send.sinfo_stream = info->snd_sid;
5388                                                         policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
5389                                                         stcb->asoc.def_send.sinfo_flags = info->snd_flags;
5390                                                         stcb->asoc.def_send.sinfo_flags |= policy;
5391                                                         stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
5392                                                         stcb->asoc.def_send.sinfo_context = info->snd_context;
5393                                                 }
5394                                                 SCTP_TCB_UNLOCK(stcb);
5395                                         }
5396                                         SCTP_INP_RUNLOCK(inp);
5397                                 }
5398                         }
5399                         break;
5400                 }
5401         case SCTP_DEFAULT_PRINFO:
5402                 {
5403                         struct sctp_default_prinfo *info;
5404
5405                         SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
5406                         SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
5407
5408                         if (PR_SCTP_INVALID_POLICY(info->pr_policy)) {
5409                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5410                                 error = EINVAL;
5411                                 break;
5412                         }
5413                         if (stcb) {
5414                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
5415                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
5416                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
5417                                 SCTP_TCB_UNLOCK(stcb);
5418                         } else {
5419                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5420                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5421                                     (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
5422                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
5423                                         SCTP_INP_WLOCK(inp);
5424                                         inp->def_send.sinfo_flags &= 0xfff0;
5425                                         inp->def_send.sinfo_flags |= info->pr_policy;
5426                                         inp->def_send.sinfo_timetolive = info->pr_value;
5427                                         SCTP_INP_WUNLOCK(inp);
5428                                 }
5429                                 if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
5430                                     (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
5431                                         SCTP_INP_RLOCK(inp);
5432                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5433                                                 SCTP_TCB_LOCK(stcb);
5434                                                 stcb->asoc.def_send.sinfo_flags &= 0xfff0;
5435                                                 stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
5436                                                 stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
5437                                                 SCTP_TCB_UNLOCK(stcb);
5438                                         }
5439                                         SCTP_INP_RUNLOCK(inp);
5440                                 }
5441                         }
5442                         break;
5443                 }
5444         case SCTP_PEER_ADDR_THLDS:
5445                 /* Applies to the specific association */
5446                 {
5447                         struct sctp_paddrthlds *thlds;
5448                         struct sctp_nets *net;
5449
5450                         SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
5451                         SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
5452                         net = NULL;
5453                         if (stcb) {
5454                                 net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_assoc_id);
5455                         } else {
5456                                 /*
5457                                  * We increment here since
5458                                  * sctp_findassociation_ep_addr() wil do a
5459                                  * decrement if it finds the stcb as long as
5460                                  * the locked tcb (last argument) is NOT a
5461                                  * TCB.. aka NULL.
5462                                  */
5463                                 SCTP_INP_INCR_REF(inp);
5464                                 stcb = sctp_findassociation_ep_addr(&inp,
5465                                     (struct sockaddr *)&thlds->spt_assoc_id,
5466                                     &net, NULL, NULL);
5467                                 if (stcb == NULL) {
5468                                         SCTP_INP_DECR_REF(inp);
5469                                 }
5470                         }
5471                         if (stcb && (net == NULL)) {
5472                                 struct sockaddr *sa;
5473
5474                                 sa = (struct sockaddr *)&thlds->spt_assoc_id;
5475 #ifdef INET
5476                                 if (sa->sa_family == AF_INET) {
5477
5478                                         struct sockaddr_in *sin;
5479
5480                                         sin = (struct sockaddr_in *)sa;
5481                                         if (sin->sin_addr.s_addr) {
5482                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5483                                                 SCTP_TCB_UNLOCK(stcb);
5484                                                 error = EINVAL;
5485                                                 break;
5486                                         }
5487                                 } else
5488 #endif
5489 #ifdef INET6
5490                                 if (sa->sa_family == AF_INET6) {
5491                                         struct sockaddr_in6 *sin6;
5492
5493                                         sin6 = (struct sockaddr_in6 *)sa;
5494                                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5495                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5496                                                 SCTP_TCB_UNLOCK(stcb);
5497                                                 error = EINVAL;
5498                                                 break;
5499                                         }
5500                                 } else
5501 #endif
5502                                 {
5503                                         error = EAFNOSUPPORT;
5504                                         SCTP_TCB_UNLOCK(stcb);
5505                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5506                                         break;
5507                                 }
5508                         }
5509                         if (stcb) {
5510                                 if (net) {
5511                                         if (net->dest_state & SCTP_ADDR_PF) {
5512                                                 if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
5513                                                     (net->failure_threshold <= thlds->spt_pathpfthld)) {
5514                                                         net->dest_state &= ~SCTP_ADDR_PF;
5515                                                 }
5516                                         } else {
5517                                                 if ((net->failure_threshold > thlds->spt_pathpfthld) &&
5518                                                     (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
5519                                                         net->dest_state |= SCTP_ADDR_PF;
5520                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5521                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5522                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5523                                                 }
5524                                         }
5525                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
5526                                                 if (net->failure_threshold > thlds->spt_pathmaxrxt) {
5527                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
5528                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
5529                                                 }
5530                                         } else {
5531                                                 if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
5532                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
5533                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
5534                                                 }
5535                                         }
5536                                         net->failure_threshold = thlds->spt_pathmaxrxt;
5537                                         net->pf_threshold = thlds->spt_pathpfthld;
5538                                 } else {
5539                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5540                                                 if (net->dest_state & SCTP_ADDR_PF) {
5541                                                         if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
5542                                                             (net->failure_threshold <= thlds->spt_pathpfthld)) {
5543                                                                 net->dest_state &= ~SCTP_ADDR_PF;
5544                                                         }
5545                                                 } else {
5546                                                         if ((net->failure_threshold > thlds->spt_pathpfthld) &&
5547                                                             (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
5548                                                                 net->dest_state |= SCTP_ADDR_PF;
5549                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5550                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5551                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5552                                                         }
5553                                                 }
5554                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
5555                                                         if (net->failure_threshold > thlds->spt_pathmaxrxt) {
5556                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5557                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
5558                                                         }
5559                                                 } else {
5560                                                         if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
5561                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
5562                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, SCTP_RESPONSE_TO_USER_REQ, net, SCTP_SO_LOCKED);
5563                                                         }
5564                                                 }
5565                                                 net->failure_threshold = thlds->spt_pathmaxrxt;
5566                                                 net->pf_threshold = thlds->spt_pathpfthld;
5567                                         }
5568                                         stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
5569                                         stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
5570                                 }
5571                         } else {
5572                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5573                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5574                                     (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
5575                                         SCTP_INP_WLOCK(inp);
5576                                         inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
5577                                         inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
5578                                         SCTP_INP_WUNLOCK(inp);
5579                                 } else {
5580                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5581                                         error = EINVAL;
5582                                 }
5583                         }
5584                         break;
5585                 }
5586         default:
5587                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
5588                 error = ENOPROTOOPT;
5589                 break;
5590         }                       /* end switch (opt) */
5591         return (error);
5592 }
5593
5594 int
5595 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
5596 {
5597         void *optval = NULL;
5598         size_t optsize = 0;
5599         struct sctp_inpcb *inp;
5600         void *p;
5601         int error = 0;
5602
5603         inp = (struct sctp_inpcb *)so->so_pcb;
5604         if (inp == 0) {
5605                 /* I made the same as TCP since we are not setup? */
5606                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5607                 return (ECONNRESET);
5608         }
5609         if (sopt->sopt_level != IPPROTO_SCTP) {
5610                 /* wrong proto level... send back up to IP */
5611 #ifdef INET6
5612                 if (INP_CHECK_SOCKAF(so, AF_INET6))
5613                         error = ip6_ctloutput(so, sopt);
5614 #endif                          /* INET6 */
5615 #if defined(INET) && defined (INET6)
5616                 else
5617 #endif
5618 #ifdef INET
5619                         error = ip_ctloutput(so, sopt);
5620 #endif
5621                 return (error);
5622         }
5623         optsize = sopt->sopt_valsize;
5624         if (optsize) {
5625                 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
5626                 if (optval == NULL) {
5627                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
5628                         return (ENOBUFS);
5629                 }
5630                 error = sooptcopyin(sopt, optval, optsize, optsize);
5631                 if (error) {
5632                         SCTP_FREE(optval, SCTP_M_SOCKOPT);
5633                         goto out;
5634                 }
5635         }
5636         p = (void *)sopt->sopt_td;
5637         if (sopt->sopt_dir == SOPT_SET) {
5638                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
5639         } else if (sopt->sopt_dir == SOPT_GET) {
5640                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
5641         } else {
5642                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5643                 error = EINVAL;
5644         }
5645         if ((error == 0) && (optval != NULL)) {
5646                 error = sooptcopyout(sopt, optval, optsize);
5647                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
5648         } else if (optval != NULL) {
5649                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
5650         }
5651 out:
5652         return (error);
5653 }
5654
5655 #ifdef INET
5656 static int
5657 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
5658 {
5659         int error = 0;
5660         int create_lock_on = 0;
5661         uint32_t vrf_id;
5662         struct sctp_inpcb *inp;
5663         struct sctp_tcb *stcb = NULL;
5664
5665         inp = (struct sctp_inpcb *)so->so_pcb;
5666         if (inp == 0) {
5667                 /* I made the same as TCP since we are not setup? */
5668                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5669                 return (ECONNRESET);
5670         }
5671         if (addr == NULL) {
5672                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5673                 return EINVAL;
5674         }
5675         switch (addr->sa_family) {
5676 #ifdef INET6
5677         case AF_INET6:
5678                 {
5679                         struct sockaddr_in6 *sin6p;
5680
5681                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
5682                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5683                                 return (EINVAL);
5684                         }
5685                         sin6p = (struct sockaddr_in6 *)addr;
5686                         if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
5687                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5688                                 return (error);
5689                         }
5690                         break;
5691                 }
5692 #endif
5693 #ifdef INET
5694         case AF_INET:
5695                 {
5696                         struct sockaddr_in *sinp;
5697
5698                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
5699                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5700                                 return (EINVAL);
5701                         }
5702                         sinp = (struct sockaddr_in *)addr;
5703                         if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
5704                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5705                                 return (error);
5706                         }
5707                         break;
5708                 }
5709 #endif
5710         default:
5711                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
5712                 return (EAFNOSUPPORT);
5713         }
5714         SCTP_INP_INCR_REF(inp);
5715         SCTP_ASOC_CREATE_LOCK(inp);
5716         create_lock_on = 1;
5717
5718
5719         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5720             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
5721                 /* Should I really unlock ? */
5722                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
5723                 error = EFAULT;
5724                 goto out_now;
5725         }
5726 #ifdef INET6
5727         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
5728             (addr->sa_family == AF_INET6)) {
5729                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5730                 error = EINVAL;
5731                 goto out_now;
5732         }
5733 #endif                          /* INET6 */
5734         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
5735             SCTP_PCB_FLAGS_UNBOUND) {
5736                 /* Bind a ephemeral port */
5737                 error = sctp_inpcb_bind(so, NULL, NULL, p);
5738                 if (error) {
5739                         goto out_now;
5740                 }
5741         }
5742         /* Now do we connect? */
5743         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
5744             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
5745                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5746                 error = EINVAL;
5747                 goto out_now;
5748         }
5749         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5750             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
5751                 /* We are already connected AND the TCP model */
5752                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
5753                 error = EADDRINUSE;
5754                 goto out_now;
5755         }
5756         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5757                 SCTP_INP_RLOCK(inp);
5758                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
5759                 SCTP_INP_RUNLOCK(inp);
5760         } else {
5761                 /*
5762                  * We increment here since sctp_findassociation_ep_addr()
5763                  * will do a decrement if it finds the stcb as long as the
5764                  * locked tcb (last argument) is NOT a TCB.. aka NULL.
5765                  */
5766                 SCTP_INP_INCR_REF(inp);
5767                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
5768                 if (stcb == NULL) {
5769                         SCTP_INP_DECR_REF(inp);
5770                 } else {
5771                         SCTP_TCB_UNLOCK(stcb);
5772                 }
5773         }
5774         if (stcb != NULL) {
5775                 /* Already have or am bring up an association */
5776                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
5777                 error = EALREADY;
5778                 goto out_now;
5779         }
5780         vrf_id = inp->def_vrf_id;
5781         /* We are GOOD to go */
5782         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
5783         if (stcb == NULL) {
5784                 /* Gak! no memory */
5785                 goto out_now;
5786         }
5787         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
5788                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
5789                 /* Set the connected flag so we can queue data */
5790                 soisconnecting(so);
5791         }
5792         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
5793         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
5794
5795         /* initialize authentication parameters for the assoc */
5796         sctp_initialize_auth_params(inp, stcb);
5797
5798         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
5799         SCTP_TCB_UNLOCK(stcb);
5800 out_now:
5801         if (create_lock_on) {
5802                 SCTP_ASOC_CREATE_UNLOCK(inp);
5803         }
5804         SCTP_INP_DECR_REF(inp);
5805         return error;
5806 }
5807
5808 #endif
5809
5810 int
5811 sctp_listen(struct socket *so, int backlog, struct thread *p)
5812 {
5813         /*
5814          * Note this module depends on the protocol processing being called
5815          * AFTER any socket level flags and backlog are applied to the
5816          * socket. The traditional way that the socket flags are applied is
5817          * AFTER protocol processing. We have made a change to the
5818          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
5819          * place if the socket API for SCTP is to work properly.
5820          */
5821
5822         int error = 0;
5823         struct sctp_inpcb *inp;
5824
5825         inp = (struct sctp_inpcb *)so->so_pcb;
5826         if (inp == 0) {
5827                 /* I made the same as TCP since we are not setup? */
5828                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5829                 return (ECONNRESET);
5830         }
5831         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
5832                 /* See if we have a listener */
5833                 struct sctp_inpcb *tinp;
5834                 union sctp_sockstore store, *sp;
5835
5836                 sp = &store;
5837                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5838                         /* not bound all */
5839                         struct sctp_laddr *laddr;
5840
5841                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5842                                 memcpy(&store, &laddr->ifa->address, sizeof(store));
5843                                 switch (sp->sa.sa_family) {
5844 #ifdef INET
5845                                 case AF_INET:
5846                                         sp->sin.sin_port = inp->sctp_lport;
5847                                         break;
5848 #endif
5849 #ifdef INET6
5850                                 case AF_INET6:
5851                                         sp->sin6.sin6_port = inp->sctp_lport;
5852                                         break;
5853 #endif
5854                                 default:
5855                                         break;
5856                                 }
5857                                 tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
5858                                 if (tinp && (tinp != inp) &&
5859                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
5860                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
5861                                     (tinp->sctp_socket->so_qlimit)) {
5862                                         /*
5863                                          * we have a listener already and
5864                                          * its not this inp.
5865                                          */
5866                                         SCTP_INP_DECR_REF(tinp);
5867                                         return (EADDRINUSE);
5868                                 } else if (tinp) {
5869                                         SCTP_INP_DECR_REF(tinp);
5870                                 }
5871                         }
5872                 } else {
5873                         /* Setup a local addr bound all */
5874                         memset(&store, 0, sizeof(store));
5875                         switch (sp->sa.sa_family) {
5876 #ifdef INET
5877                         case AF_INET:
5878                                 store.sin.sin_port = inp->sctp_lport;
5879                                 break;
5880 #endif
5881 #ifdef INET6
5882                         case AF_INET6:
5883                                 sp->sin6.sin6_port = inp->sctp_lport;
5884                                 break;
5885 #endif
5886                         default:
5887                                 break;
5888                         }
5889 #ifdef INET6
5890                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5891                                 store.sa.sa_family = AF_INET6;
5892                                 store.sa.sa_len = sizeof(struct sockaddr_in6);
5893                         }
5894 #endif
5895 #ifdef INET
5896                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
5897                                 store.sa.sa_family = AF_INET;
5898                                 store.sa.sa_len = sizeof(struct sockaddr_in);
5899                         }
5900 #endif
5901                         tinp = sctp_pcb_findep(&sp->sa, 0, 0, inp->def_vrf_id);
5902                         if (tinp && (tinp != inp) &&
5903                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
5904                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
5905                             (tinp->sctp_socket->so_qlimit)) {
5906                                 /*
5907                                  * we have a listener already and its not
5908                                  * this inp.
5909                                  */
5910                                 SCTP_INP_DECR_REF(tinp);
5911                                 return (EADDRINUSE);
5912                         } else if (tinp) {
5913                                 SCTP_INP_DECR_REF(inp);
5914                         }
5915                 }
5916         }
5917         SCTP_INP_RLOCK(inp);
5918 #ifdef SCTP_LOCK_LOGGING
5919         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
5920                 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
5921         }
5922 #endif
5923         SOCK_LOCK(so);
5924         error = solisten_proto_check(so);
5925         if (error) {
5926                 SOCK_UNLOCK(so);
5927                 SCTP_INP_RUNLOCK(inp);
5928                 return (error);
5929         }
5930         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
5931             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5932                 /*
5933                  * The unlucky case - We are in the tcp pool with this guy.
5934                  * - Someone else is in the main inp slot. - We must move
5935                  * this guy (the listener) to the main slot - We must then
5936                  * move the guy that was listener to the TCP Pool.
5937                  */
5938                 if (sctp_swap_inpcb_for_listen(inp)) {
5939                         goto in_use;
5940                 }
5941         }
5942         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5943             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
5944                 /* We are already connected AND the TCP model */
5945 in_use:
5946                 SCTP_INP_RUNLOCK(inp);
5947                 SOCK_UNLOCK(so);
5948                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
5949                 return (EADDRINUSE);
5950         }
5951         SCTP_INP_RUNLOCK(inp);
5952         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
5953                 /* We must do a bind. */
5954                 SOCK_UNLOCK(so);
5955                 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
5956                         /* bind error, probably perm */
5957                         return (error);
5958                 }
5959                 SOCK_LOCK(so);
5960         }
5961         /* It appears for 7.0 and on, we must always call this. */
5962         solisten_proto(so, backlog);
5963         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
5964                 /* remove the ACCEPTCONN flag for one-to-many sockets */
5965                 so->so_options &= ~SO_ACCEPTCONN;
5966         }
5967         if (backlog == 0) {
5968                 /* turning off listen */
5969                 so->so_options &= ~SO_ACCEPTCONN;
5970         }
5971         SOCK_UNLOCK(so);
5972         return (error);
5973 }
5974
5975 static int sctp_defered_wakeup_cnt = 0;
5976
5977 int
5978 sctp_accept(struct socket *so, struct sockaddr **addr)
5979 {
5980         struct sctp_tcb *stcb;
5981         struct sctp_inpcb *inp;
5982         union sctp_sockstore store;
5983
5984 #ifdef INET6
5985         int error;
5986
5987 #endif
5988         inp = (struct sctp_inpcb *)so->so_pcb;
5989
5990         if (inp == 0) {
5991                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5992                 return (ECONNRESET);
5993         }
5994         SCTP_INP_RLOCK(inp);
5995         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
5996                 SCTP_INP_RUNLOCK(inp);
5997                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
5998                 return (EOPNOTSUPP);
5999         }
6000         if (so->so_state & SS_ISDISCONNECTED) {
6001                 SCTP_INP_RUNLOCK(inp);
6002                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
6003                 return (ECONNABORTED);
6004         }
6005         stcb = LIST_FIRST(&inp->sctp_asoc_list);
6006         if (stcb == NULL) {
6007                 SCTP_INP_RUNLOCK(inp);
6008                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6009                 return (ECONNRESET);
6010         }
6011         SCTP_TCB_LOCK(stcb);
6012         SCTP_INP_RUNLOCK(inp);
6013         store = stcb->asoc.primary_destination->ro._l_addr;
6014         stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
6015         SCTP_TCB_UNLOCK(stcb);
6016         switch (store.sa.sa_family) {
6017 #ifdef INET
6018         case AF_INET:
6019                 {
6020                         struct sockaddr_in *sin;
6021
6022                         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
6023                         if (sin == NULL)
6024                                 return (ENOMEM);
6025                         sin->sin_family = AF_INET;
6026                         sin->sin_len = sizeof(*sin);
6027                         sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
6028                         sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
6029                         *addr = (struct sockaddr *)sin;
6030                         break;
6031                 }
6032 #endif
6033 #ifdef INET6
6034         case AF_INET6:
6035                 {
6036                         struct sockaddr_in6 *sin6;
6037
6038                         SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
6039                         if (sin6 == NULL)
6040                                 return (ENOMEM);
6041                         sin6->sin6_family = AF_INET6;
6042                         sin6->sin6_len = sizeof(*sin6);
6043                         sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
6044
6045                         sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
6046                         if ((error = sa6_recoverscope(sin6)) != 0) {
6047                                 SCTP_FREE_SONAME(sin6);
6048                                 return (error);
6049                         }
6050                         *addr = (struct sockaddr *)sin6;
6051                         break;
6052                 }
6053 #endif
6054         default:
6055                 /* TSNH */
6056                 break;
6057         }
6058         /* Wake any delayed sleep action */
6059         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
6060                 SCTP_INP_WLOCK(inp);
6061                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
6062                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
6063                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
6064                         SCTP_INP_WUNLOCK(inp);
6065                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
6066                         if (sowriteable(inp->sctp_socket)) {
6067                                 sowwakeup_locked(inp->sctp_socket);
6068                         } else {
6069                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
6070                         }
6071                         SCTP_INP_WLOCK(inp);
6072                 }
6073                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
6074                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
6075                         SCTP_INP_WUNLOCK(inp);
6076                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
6077                         if (soreadable(inp->sctp_socket)) {
6078                                 sctp_defered_wakeup_cnt++;
6079                                 sorwakeup_locked(inp->sctp_socket);
6080                         } else {
6081                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
6082                         }
6083                         SCTP_INP_WLOCK(inp);
6084                 }
6085                 SCTP_INP_WUNLOCK(inp);
6086         }
6087         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6088                 SCTP_TCB_LOCK(stcb);
6089                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_7);
6090         }
6091         return (0);
6092 }
6093
6094 #ifdef INET
6095 int
6096 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
6097 {
6098         struct sockaddr_in *sin;
6099         uint32_t vrf_id;
6100         struct sctp_inpcb *inp;
6101         struct sctp_ifa *sctp_ifa;
6102
6103         /*
6104          * Do the malloc first in case it blocks.
6105          */
6106         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
6107         if (sin == NULL)
6108                 return (ENOMEM);
6109         sin->sin_family = AF_INET;
6110         sin->sin_len = sizeof(*sin);
6111         inp = (struct sctp_inpcb *)so->so_pcb;
6112         if (!inp) {
6113                 SCTP_FREE_SONAME(sin);
6114                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6115                 return ECONNRESET;
6116         }
6117         SCTP_INP_RLOCK(inp);
6118         sin->sin_port = inp->sctp_lport;
6119         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6120                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
6121                         struct sctp_tcb *stcb;
6122                         struct sockaddr_in *sin_a;
6123                         struct sctp_nets *net;
6124                         int fnd;
6125
6126                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
6127                         if (stcb == NULL) {
6128                                 goto notConn;
6129                         }
6130                         fnd = 0;
6131                         sin_a = NULL;
6132                         SCTP_TCB_LOCK(stcb);
6133                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6134                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
6135                                 if (sin_a == NULL)
6136                                         /* this will make coverity happy */
6137                                         continue;
6138
6139                                 if (sin_a->sin_family == AF_INET) {
6140                                         fnd = 1;
6141                                         break;
6142                                 }
6143                         }
6144                         if ((!fnd) || (sin_a == NULL)) {
6145                                 /* punt */
6146                                 SCTP_TCB_UNLOCK(stcb);
6147                                 goto notConn;
6148                         }
6149                         vrf_id = inp->def_vrf_id;
6150                         sctp_ifa = sctp_source_address_selection(inp,
6151                             stcb,
6152                             (sctp_route_t *) & net->ro,
6153                             net, 0, vrf_id);
6154                         if (sctp_ifa) {
6155                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
6156                                 sctp_free_ifa(sctp_ifa);
6157                         }
6158                         SCTP_TCB_UNLOCK(stcb);
6159                 } else {
6160                         /* For the bound all case you get back 0 */
6161         notConn:
6162                         sin->sin_addr.s_addr = 0;
6163                 }
6164
6165         } else {
6166                 /* Take the first IPv4 address in the list */
6167                 struct sctp_laddr *laddr;
6168                 int fnd = 0;
6169
6170                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6171                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
6172                                 struct sockaddr_in *sin_a;
6173
6174                                 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
6175                                 sin->sin_addr = sin_a->sin_addr;
6176                                 fnd = 1;
6177                                 break;
6178                         }
6179                 }
6180                 if (!fnd) {
6181                         SCTP_FREE_SONAME(sin);
6182                         SCTP_INP_RUNLOCK(inp);
6183                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
6184                         return ENOENT;
6185                 }
6186         }
6187         SCTP_INP_RUNLOCK(inp);
6188         (*addr) = (struct sockaddr *)sin;
6189         return (0);
6190 }
6191
6192 int
6193 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
6194 {
6195         struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
6196         int fnd;
6197         struct sockaddr_in *sin_a;
6198         struct sctp_inpcb *inp;
6199         struct sctp_tcb *stcb;
6200         struct sctp_nets *net;
6201
6202         /* Do the malloc first in case it blocks. */
6203         inp = (struct sctp_inpcb *)so->so_pcb;
6204         if ((inp == NULL) ||
6205             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
6206                 /* UDP type and listeners will drop out here */
6207                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
6208                 return (ENOTCONN);
6209         }
6210         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
6211         if (sin == NULL)
6212                 return (ENOMEM);
6213         sin->sin_family = AF_INET;
6214         sin->sin_len = sizeof(*sin);
6215
6216         /* We must recapture incase we blocked */
6217         inp = (struct sctp_inpcb *)so->so_pcb;
6218         if (!inp) {
6219                 SCTP_FREE_SONAME(sin);
6220                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6221                 return ECONNRESET;
6222         }
6223         SCTP_INP_RLOCK(inp);
6224         stcb = LIST_FIRST(&inp->sctp_asoc_list);
6225         if (stcb) {
6226                 SCTP_TCB_LOCK(stcb);
6227         }
6228         SCTP_INP_RUNLOCK(inp);
6229         if (stcb == NULL) {
6230                 SCTP_FREE_SONAME(sin);
6231                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6232                 return ECONNRESET;
6233         }
6234         fnd = 0;
6235         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6236                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
6237                 if (sin_a->sin_family == AF_INET) {
6238                         fnd = 1;
6239                         sin->sin_port = stcb->rport;
6240                         sin->sin_addr = sin_a->sin_addr;
6241                         break;
6242                 }
6243         }
6244         SCTP_TCB_UNLOCK(stcb);
6245         if (!fnd) {
6246                 /* No IPv4 address */
6247                 SCTP_FREE_SONAME(sin);
6248                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
6249                 return ENOENT;
6250         }
6251         (*addr) = (struct sockaddr *)sin;
6252         return (0);
6253 }
6254
6255 #ifdef INET
6256 struct pr_usrreqs sctp_usrreqs = {
6257         .pru_abort = sctp_abort,
6258         .pru_accept = sctp_accept,
6259         .pru_attach = sctp_attach,
6260         .pru_bind = sctp_bind,
6261         .pru_connect = sctp_connect,
6262         .pru_control = in_control,
6263         .pru_close = sctp_close,
6264         .pru_detach = sctp_close,
6265         .pru_sopoll = sopoll_generic,
6266         .pru_flush = sctp_flush,
6267         .pru_disconnect = sctp_disconnect,
6268         .pru_listen = sctp_listen,
6269         .pru_peeraddr = sctp_peeraddr,
6270         .pru_send = sctp_sendm,
6271         .pru_shutdown = sctp_shutdown,
6272         .pru_sockaddr = sctp_ingetaddr,
6273         .pru_sosend = sctp_sosend,
6274         .pru_soreceive = sctp_soreceive
6275 };
6276
6277 #endif
6278 #endif