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