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