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