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