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