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