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