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