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