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