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