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