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