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