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