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