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