Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / usrsctp / usrsctplib / netinet / sctp_usrreq.c
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifdef __FreeBSD__
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: head/sys/netinet/sctp_usrreq.c 272750 2014-10-08 15:29:49Z tuexen $");
36 #endif
37
38 #include <netinet/sctp_os.h>
39 #ifdef __FreeBSD__
40 #include <sys/proc.h>
41 #endif
42 #include <netinet/sctp_pcb.h>
43 #include <netinet/sctp_header.h>
44 #include <netinet/sctp_var.h>
45 #ifdef INET6
46 #if defined(__Userspace_os_FreeBSD)
47 #include <netinet6/sctp6_var.h>
48 #endif
49 #endif
50 #include <netinet/sctp_sysctl.h>
51 #include <netinet/sctp_output.h>
52 #include <netinet/sctp_uio.h>
53 #include <netinet/sctp_asconf.h>
54 #include <netinet/sctputil.h>
55 #include <netinet/sctp_indata.h>
56 #include <netinet/sctp_timer.h>
57 #include <netinet/sctp_auth.h>
58 #include <netinet/sctp_bsd_addr.h>
59 #if defined(__Userspace__)
60 #include <netinet/sctp_callout.h>
61 #else
62 #include <netinet/udp.h>
63 #endif
64
65 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
66 #include <netinet/sctp_peeloff.h>
67 #endif                          /* HAVE_SCTP_PEELOFF_SOCKOPT */
68
69 #if defined(__APPLE__)
70 #define APPLE_FILE_NO 7
71 #endif
72
73 extern struct sctp_cc_functions sctp_cc_functions[];
74 extern struct sctp_ss_functions sctp_ss_functions[];
75
76 void
77 #if defined(__Userspace__)
78 sctp_init(uint16_t port,
79           int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
80           void (*debug_printf)(const char *format, ...))
81 #elif defined(__APPLE__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) &&!defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION))
82 sctp_init(struct protosw *pp SCTP_UNUSED, struct domain *dp SCTP_UNUSED)
83 #else
84 sctp_init(void)
85 #endif
86 {
87 #if !defined(__Panda__) && !defined(__Userspace__)
88         u_long sb_max_adj;
89
90 #endif
91 #if defined(__Userspace__)
92 #if defined(__Userspace_os_Windows)
93 #if defined(INET) || defined(INET6)
94         WSADATA wsaData;
95
96         if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
97                 SCTP_PRINTF("WSAStartup failed\n");
98                 exit (-1);
99         }
100 #endif
101         InitializeConditionVariable(&accept_cond);
102         InitializeCriticalSection(&accept_mtx);
103 #else
104         pthread_cond_init(&accept_cond, NULL);
105         pthread_mutex_init(&accept_mtx, NULL);
106 #endif
107 #endif
108         /* Initialize and modify the sysctled variables */
109         sctp_init_sysctls();
110 #if defined(__Userspace__)
111 #if defined(__Userspace_os_Windows) || defined(__Userspace_os_NaCl)
112         srand((unsigned int)time(NULL));
113 #else
114         srandom(getpid()); /* so inp->sctp_ep.random_numbers are truly random... */
115 #endif
116 #endif
117 #if defined(__Panda__)
118         sctp_sendspace = SB_MAX;
119         sctp_recvspace = SB_MAX;
120
121 #elif defined(__Userspace__)
122         SCTP_BASE_SYSCTL(sctp_sendspace) = SB_MAX;
123         SCTP_BASE_SYSCTL(sctp_recvspace) = SB_RAW;
124         SCTP_BASE_SYSCTL(sctp_udp_tunneling_port) = port;
125 #else
126 #if !defined(__APPLE__)
127         if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
128                 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue) = (nmbclusters / 8);
129 #endif
130         /*
131          * Allow a user to take no more than 1/2 the number of clusters or
132          * the SB_MAX whichever is smaller for the send window.
133          */
134 #if defined(__APPLE__)
135         sb_max_adj = (u_long)((u_quad_t) (sb_max) * MCLBYTES / (MSIZE + MCLBYTES));
136 #else
137         sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
138 #endif
139 #if defined(__APPLE__)
140         SCTP_BASE_SYSCTL(sctp_sendspace) = sb_max_adj;
141 #else
142         SCTP_BASE_SYSCTL(sctp_sendspace) = min(sb_max_adj,
143             (((uint32_t)nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
144 #endif
145         /*
146          * Now for the recv window, should we take the same amount? or
147          * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
148          * now I will just copy.
149          */
150         SCTP_BASE_SYSCTL(sctp_recvspace) = SCTP_BASE_SYSCTL(sctp_sendspace);
151 #endif
152         SCTP_BASE_VAR(first_time) = 0;
153         SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
154 #if defined(__Userspace__)
155 #if !defined(__Userspace_os_Windows)
156 #if defined(INET) || defined(INET6)
157         SCTP_BASE_VAR(userspace_route) = -1;
158 #endif
159 #endif
160 #ifdef INET
161         SCTP_BASE_VAR(userspace_rawsctp) = -1;
162         SCTP_BASE_VAR(userspace_udpsctp) = -1;
163 #endif
164 #ifdef INET6
165         SCTP_BASE_VAR(userspace_rawsctp6) = -1;
166         SCTP_BASE_VAR(userspace_udpsctp6) = -1;
167 #endif
168         SCTP_BASE_VAR(timer_thread_should_exit) = 0;
169         SCTP_BASE_VAR(conn_output) = conn_output;
170         SCTP_BASE_VAR(debug_printf) = debug_printf;
171 #endif
172         sctp_pcb_init();
173 #if defined(__Userspace__)
174         sctp_start_timer();
175 #endif
176 #if defined(SCTP_PACKET_LOGGING)
177         SCTP_BASE_VAR(packet_log_writers) = 0;
178         SCTP_BASE_VAR(packet_log_end) = 0;
179         bzero(&SCTP_BASE_VAR(packet_log_buffer), SCTP_PACKET_LOG_SIZE);
180 #endif
181 #if defined(__APPLE__)
182         SCTP_BASE_VAR(sctp_main_timer_ticks) = 0;
183         sctp_start_main_timer();
184         timeout(sctp_delayed_startup, NULL, 1);
185 #endif
186 }
187
188 void
189 sctp_finish(void)
190 {
191 #if defined(__APPLE__)
192         untimeout(sctp_delayed_startup, NULL);
193         sctp_over_udp_stop();
194         sctp_address_monitor_stop();
195         sctp_stop_main_timer();
196 #endif
197 #if defined(__Userspace__)
198 #if defined(INET) || defined(INET6)
199         recv_thread_destroy();
200 #endif
201 #if !defined(__Userspace_os_Windows)
202 #if defined(INET) || defined(INET6)
203         if (SCTP_BASE_VAR(userspace_route) != -1) {
204                 pthread_join(SCTP_BASE_VAR(recvthreadroute), NULL);
205         }
206 #endif
207 #endif
208 #ifdef INET
209         if (SCTP_BASE_VAR(userspace_rawsctp) != -1) {
210 #if defined(__Userspace_os_Windows)
211                 WaitForSingleObject(SCTP_BASE_VAR(recvthreadraw), INFINITE);
212                 CloseHandle(SCTP_BASE_VAR(recvthreadraw));
213 #else
214                 pthread_join(SCTP_BASE_VAR(recvthreadraw), NULL);
215 #endif
216         }
217         if (SCTP_BASE_VAR(userspace_udpsctp) != -1) {
218 #if defined(__Userspace_os_Windows)
219                 WaitForSingleObject(SCTP_BASE_VAR(recvthreadudp), INFINITE);
220                 CloseHandle(SCTP_BASE_VAR(recvthreadudp));
221 #else
222                 pthread_join(SCTP_BASE_VAR(recvthreadudp), NULL);
223 #endif
224         }
225 #endif
226 #ifdef INET6
227         if (SCTP_BASE_VAR(userspace_rawsctp6) != -1) {
228 #if defined(__Userspace_os_Windows)
229                 WaitForSingleObject(SCTP_BASE_VAR(recvthreadraw6), INFINITE);
230                 CloseHandle(SCTP_BASE_VAR(recvthreadraw6));
231 #else
232                 pthread_join(SCTP_BASE_VAR(recvthreadraw6), NULL);
233 #endif
234         }
235         if (SCTP_BASE_VAR(userspace_udpsctp6) != -1) {
236 #if defined(__Userspace_os_Windows)
237                 WaitForSingleObject(SCTP_BASE_VAR(recvthreadudp6), INFINITE);
238                 CloseHandle(SCTP_BASE_VAR(recvthreadudp6));
239 #else
240                 pthread_join(SCTP_BASE_VAR(recvthreadudp6), NULL);
241 #endif
242         }
243 #endif
244         SCTP_BASE_VAR(timer_thread_should_exit) = 1;
245 #if defined(__Userspace_os_Windows)
246         WaitForSingleObject(SCTP_BASE_VAR(timer_thread), INFINITE);
247         CloseHandle(SCTP_BASE_VAR(timer_thread));
248 #else
249         pthread_join(SCTP_BASE_VAR(timer_thread), NULL);
250 #endif
251 #endif
252         sctp_pcb_finish();
253 #if defined(__Userspace__)
254 #if defined(__Userspace_os_Windows)
255         DeleteConditionVariable(&accept_cond);
256         DeleteCriticalSection(&accept_mtx);
257 #else
258         pthread_cond_destroy(&accept_cond);
259         pthread_mutex_destroy(&accept_mtx);
260 #endif
261 #endif
262 #if defined(__Windows__)
263         sctp_finish_sysctls();
264 #if defined(INET) || defined(INET6)
265         WSACleanup();
266 #endif
267 #endif
268 }
269
270
271
272 void
273 sctp_pathmtu_adjustment(struct sctp_tcb *stcb, uint16_t nxtsz)
274 {
275         struct sctp_tmit_chunk *chk;
276         uint16_t overhead;
277
278         /* Adjust that too */
279         stcb->asoc.smallest_mtu = nxtsz;
280         /* now off to subtract IP_DF flag if needed */
281         overhead = IP_HDR_SIZE;
282         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
283                 overhead += sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
284         }
285         TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
286                 if ((chk->send_size + overhead) > nxtsz) {
287                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
288                 }
289         }
290         TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
291                 if ((chk->send_size + overhead) > nxtsz) {
292                         /*
293                          * For this guy we also mark for immediate resend
294                          * since we sent to big of chunk
295                          */
296                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
297                         if (chk->sent < SCTP_DATAGRAM_RESEND) {
298                                 sctp_flight_size_decrease(chk);
299                                 sctp_total_flight_decrease(stcb, chk);
300                         }
301                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
302                                 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
303                         }
304                         chk->sent = SCTP_DATAGRAM_RESEND;
305                         chk->rec.data.doing_fast_retransmit = 0;
306                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
307                                 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
308                                                chk->whoTo->flight_size,
309                                                chk->book_size,
310                                                (uintptr_t)chk->whoTo,
311                                                chk->rec.data.TSN_seq);
312                         }
313                         /* Clear any time so NO RTT is being done */
314                         chk->do_rtt = 0;
315                 }
316         }
317 }
318
319 #ifdef INET
320 #if !defined(__Userspace__)
321 #if defined(__Panda__) || defined(__Windows__)
322 void
323 #else
324 static void
325 #endif
326 sctp_notify_mbuf(struct sctp_inpcb *inp,
327     struct sctp_tcb *stcb,
328     struct sctp_nets *net,
329     struct ip *ip,
330     struct sctphdr *sh)
331 {
332         struct icmp *icmph;
333         int totsz, tmr_stopped = 0;
334         uint16_t nxtsz;
335
336         /* protection */
337         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
338             (ip == NULL) || (sh == NULL)) {
339                 if (stcb != NULL) {
340                         SCTP_TCB_UNLOCK(stcb);
341                 }
342                 return;
343         }
344         /* First job is to verify the vtag matches what I would send */
345         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
346                 SCTP_TCB_UNLOCK(stcb);
347                 return;
348         }
349         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
350             sizeof(struct ip)));
351         if (icmph->icmp_type != ICMP_UNREACH) {
352                 /* We only care about unreachable */
353                 SCTP_TCB_UNLOCK(stcb);
354                 return;
355         }
356         if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
357                 /* not a unreachable message due to frag. */
358                 SCTP_TCB_UNLOCK(stcb);
359                 return;
360         }
361 #if defined(__FreeBSD__) && __FreeBSD_version >= 1000000
362         totsz = ntohs(ip->ip_len);
363 #else
364         totsz = ip->ip_len;
365 #endif
366
367         nxtsz = ntohs(icmph->icmp_nextmtu);
368         if (nxtsz == 0) {
369                 /*
370                  * old type router that does not tell us what the next size
371                  * mtu is. Rats we will have to guess (in a educated fashion
372                  * of course)
373                  */
374                 nxtsz = sctp_get_prev_mtu(totsz);
375         }
376         /* Stop any PMTU timer */
377         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
378                 tmr_stopped = 1;
379                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
380                                 SCTP_FROM_SCTP_USRREQ+SCTP_LOC_1);
381         }
382         /* Adjust destination size limit */
383         if (net->mtu > nxtsz) {
384                 net->mtu = nxtsz;
385                 if (net->port) {
386                         net->mtu -= sizeof(struct udphdr);
387                 }
388         }
389         /* now what about the ep? */
390         if (stcb->asoc.smallest_mtu > nxtsz) {
391                 sctp_pathmtu_adjustment(stcb, nxtsz);
392         }
393         if (tmr_stopped)
394                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
395
396         SCTP_TCB_UNLOCK(stcb);
397 }
398
399 void
400 sctp_notify(struct sctp_inpcb *inp,
401     struct ip *ip,
402     struct sctphdr *sh,
403     struct sockaddr *to,
404     struct sctp_tcb *stcb,
405     struct sctp_nets *net)
406 {
407 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
408         struct socket *so;
409
410 #endif
411         struct icmp *icmph;
412
413         /* protection */
414         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
415             (sh == NULL) || (to == NULL)) {
416                 if (stcb)
417                         SCTP_TCB_UNLOCK(stcb);
418                 return;
419         }
420         /* First job is to verify the vtag matches what I would send */
421         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
422                 SCTP_TCB_UNLOCK(stcb);
423                 return;
424         }
425
426         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
427                                                sizeof(struct ip)));
428         if (icmph->icmp_type != ICMP_UNREACH) {
429                 /* We only care about unreachable */
430                 SCTP_TCB_UNLOCK(stcb);
431                 return;
432         }
433         if ((icmph->icmp_code == ICMP_UNREACH_NET) ||
434             (icmph->icmp_code == ICMP_UNREACH_HOST) ||
435             (icmph->icmp_code == ICMP_UNREACH_NET_UNKNOWN) ||
436             (icmph->icmp_code == ICMP_UNREACH_HOST_UNKNOWN) ||
437             (icmph->icmp_code == ICMP_UNREACH_ISOLATED) ||
438             (icmph->icmp_code == ICMP_UNREACH_NET_PROHIB) ||
439             (icmph->icmp_code == ICMP_UNREACH_HOST_PROHIB) ||
440 #if defined(__Panda__)
441             (icmph->icmp_code == ICMP_UNREACH_ADMIN)) {
442 #elif defined(__Userspace_os_NetBSD)
443             (icmph->icmp_code == ICMP_UNREACH_ADMIN_PROHIBIT)) {
444 #else
445             (icmph->icmp_code == ICMP_UNREACH_FILTER_PROHIB)) {
446 #endif
447
448                 /*
449                  * Hmm reachablity problems we must examine closely. If its
450                  * not reachable, we may have lost a network. Or if there is
451                  * NO protocol at the other end named SCTP. well we consider
452                  * it a OOTB abort.
453                  */
454                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
455                         /* Ok that destination is NOT reachable */
456                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
457                         net->dest_state &= ~SCTP_ADDR_PF;
458                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
459                                         stcb, 0,
460                                         (void *)net, SCTP_SO_NOT_LOCKED);
461                 }
462                 SCTP_TCB_UNLOCK(stcb);
463         } else  if ((icmph->icmp_code == ICMP_UNREACH_PROTOCOL) ||
464                     (icmph->icmp_code == ICMP_UNREACH_PORT)) {
465                 /*
466                  * Here the peer is either playing tricks on us,
467                  * including an address that belongs to someone who
468                  * does not support SCTP OR was a userland
469                  * implementation that shutdown and now is dead. In
470                  * either case treat it like a OOTB abort with no
471                  * TCB
472                  */
473                 sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
474 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
475                 so = SCTP_INP_SO(inp);
476                 atomic_add_int(&stcb->asoc.refcnt, 1);
477                 SCTP_TCB_UNLOCK(stcb);
478                 SCTP_SOCKET_LOCK(so, 1);
479                 SCTP_TCB_LOCK(stcb);
480                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
481 #endif
482                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_2);
483 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
484                 SCTP_SOCKET_UNLOCK(so, 1);
485                 /* SCTP_TCB_UNLOCK(stcb); MT: I think this is not needed.*/
486 #endif
487                 /* no need to unlock here, since the TCB is gone */
488         } else {
489                 SCTP_TCB_UNLOCK(stcb);
490         }
491 }
492 #endif
493 #endif
494
495 #ifdef INET
496 #if !defined(__Panda__) && !defined(__Userspace__)
497 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
498 void
499 #else
500 void *
501 #endif
502 sctp_ctlinput(cmd, sa, vip)
503         int cmd;
504         struct sockaddr *sa;
505         void *vip;
506 {
507         struct ip *ip = vip;
508         struct sctphdr *sh;
509         uint32_t vrf_id;
510         /* FIX, for non-bsd is this right? */
511         vrf_id = SCTP_DEFAULT_VRFID;
512         if (sa->sa_family != AF_INET ||
513             ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
514 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
515                 return;
516 #else
517                 return (NULL);
518 #endif
519         }
520         if (PRC_IS_REDIRECT(cmd)) {
521                 ip = 0;
522         } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
523 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
524                 return;
525 #else
526                 return (NULL);
527 #endif
528         }
529         if (ip) {
530                 struct sctp_inpcb *inp = NULL;
531                 struct sctp_tcb *stcb = NULL;
532                 struct sctp_nets *net = NULL;
533                 struct sockaddr_in to, from;
534
535                 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
536                 bzero(&to, sizeof(to));
537                 bzero(&from, sizeof(from));
538                 from.sin_family = to.sin_family = AF_INET;
539 #ifdef HAVE_SIN_LEN
540                 from.sin_len = to.sin_len = sizeof(to);
541 #endif
542                 from.sin_port = sh->src_port;
543                 from.sin_addr = ip->ip_src;
544                 to.sin_port = sh->dest_port;
545                 to.sin_addr = ip->ip_dst;
546
547                 /*
548                  * 'to' holds the dest of the packet that failed to be sent.
549                  * 'from' holds our local endpoint address. Thus we reverse
550                  * the to and the from in the lookup.
551                  */
552                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&to,
553                     (struct sockaddr *)&from,
554                     &inp, &net, 1, vrf_id);
555                 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
556                         if (cmd != PRC_MSGSIZE) {
557                                 sctp_notify(inp, ip, sh,
558                                     (struct sockaddr *)&to, stcb,
559                                     net);
560                         } else {
561                                 /* handle possible ICMP size messages */
562                                 sctp_notify_mbuf(inp, stcb, net, ip, sh);
563                         }
564                 } else {
565 #if defined(__FreeBSD__) && __FreeBSD_version < 500000
566                         /*
567                          * XXX must be fixed for 5.x and higher, leave for
568                          * 4.x
569                          */
570                         if (PRC_IS_REDIRECT(cmd) && inp) {
571                                 in_rtchange((struct inpcb *)inp,
572                                     inetctlerrmap[cmd]);
573                         }
574 #endif
575                         if ((stcb == NULL) && (inp != NULL)) {
576                                 /* reduce ref-count */
577                                 SCTP_INP_WLOCK(inp);
578                                 SCTP_INP_DECR_REF(inp);
579                                 SCTP_INP_WUNLOCK(inp);
580                         }
581                         if (stcb) {
582                                 SCTP_TCB_UNLOCK(stcb);
583                         }
584                 }
585         }
586 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
587         return;
588 #else
589         return (NULL);
590 #endif
591 }
592 #endif
593 #endif
594
595 #if defined(__FreeBSD__)
596 static int
597 sctp_getcred(SYSCTL_HANDLER_ARGS)
598 {
599         struct xucred xuc;
600         struct sockaddr_in addrs[2];
601         struct sctp_inpcb *inp;
602         struct sctp_nets *net;
603         struct sctp_tcb *stcb;
604         int error;
605         uint32_t vrf_id;
606
607         /* FIX, for non-bsd is this right? */
608         vrf_id = SCTP_DEFAULT_VRFID;
609
610 #if __FreeBSD_version > 602000
611         error = priv_check(req->td, PRIV_NETINET_GETCRED);
612
613 #elif __FreeBSD_version >= 500000
614         error = suser(req->td);
615 #else
616         error = suser(req->p);
617 #endif
618         if (error)
619                 return (error);
620
621         error = SYSCTL_IN(req, addrs, sizeof(addrs));
622         if (error)
623                 return (error);
624
625         stcb = sctp_findassociation_addr_sa(sintosa(&addrs[1]),
626             sintosa(&addrs[0]),
627             &inp, &net, 1, vrf_id);
628         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
629                 if ((inp != NULL) && (stcb == NULL)) {
630                         /* reduce ref-count */
631                         SCTP_INP_WLOCK(inp);
632                         SCTP_INP_DECR_REF(inp);
633                         goto cred_can_cont;
634                 }
635
636                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
637                 error = ENOENT;
638                 goto out;
639         }
640         SCTP_TCB_UNLOCK(stcb);
641         /* We use the write lock here, only
642          * since in the error leg we need it.
643          * If we used RLOCK, then we would have
644          * to wlock/decr/unlock/rlock. Which
645          * in theory could create a hole. Better
646          * to use higher wlock.
647          */
648         SCTP_INP_WLOCK(inp);
649  cred_can_cont:
650         error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
651         if (error) {
652                 SCTP_INP_WUNLOCK(inp);
653                 goto out;
654         }
655         cru2x(inp->sctp_socket->so_cred, &xuc);
656         SCTP_INP_WUNLOCK(inp);
657         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
658 out:
659         return (error);
660 }
661
662 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
663     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
664 #endif                          /* #if defined(__FreeBSD__) */
665
666
667 #ifdef INET
668 #if defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)
669 int
670 #elif defined(__FreeBSD__) && __FreeBSD_version > 690000
671 static void
672 #else
673 static int
674 #endif
675 sctp_abort(struct socket *so)
676 {
677         struct sctp_inpcb *inp;
678         uint32_t flags;
679
680         inp = (struct sctp_inpcb *)so->so_pcb;
681         if (inp == NULL) {
682 #if defined(__FreeBSD__) && __FreeBSD_version > 690000
683                 return;
684 #else
685                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
686                 return (EINVAL);
687 #endif
688         }
689
690  sctp_must_try_again:
691         flags = inp->sctp_flags;
692 #ifdef SCTP_LOG_CLOSING
693         sctp_log_closing(inp, NULL, 17);
694 #endif
695         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
696             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
697 #ifdef SCTP_LOG_CLOSING
698                 sctp_log_closing(inp, NULL, 16);
699 #endif
700                 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
701                                 SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
702                 SOCK_LOCK(so);
703                 SCTP_SB_CLEAR(so->so_snd);
704                 /* same for the rcv ones, they are only
705                  * here for the accounting/select.
706                  */
707                 SCTP_SB_CLEAR(so->so_rcv);
708
709 #if defined(__APPLE__)
710                 so->so_usecount--;
711 #else
712                 /* Now null out the reference, we are completely detached. */
713                 so->so_pcb = NULL;
714 #endif
715                 SOCK_UNLOCK(so);
716         } else {
717                 flags = inp->sctp_flags;
718                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
719                         goto sctp_must_try_again;
720                 }
721         }
722 #if defined(__FreeBSD__) && __FreeBSD_version > 690000
723         return;
724 #else
725         return (0);
726 #endif
727 }
728
729 #if defined(__Panda__) || defined(__Userspace__)
730 int
731 #else
732 static int
733 #endif
734 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
735 sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
736 #elif defined(__Panda__) || defined(__Userspace__)
737 sctp_attach(struct socket *so, int proto SCTP_UNUSED, uint32_t vrf_id)
738 #elif defined(__Windows__)
739 sctp_attach(struct socket *so, int proto SCTP_UNUSED, PKTHREAD p SCTP_UNUSED)
740 #else
741 sctp_attach(struct socket *so, int proto SCTP_UNUSED, struct proc *p SCTP_UNUSED)
742 #endif
743 {
744         struct sctp_inpcb *inp;
745         struct inpcb *ip_inp;
746         int error;
747 #if !defined(__Panda__) && !defined(__Userspace__)
748         uint32_t vrf_id = SCTP_DEFAULT_VRFID;
749 #endif
750 #ifdef IPSEC
751         uint32_t flags;
752 #endif
753
754         inp = (struct sctp_inpcb *)so->so_pcb;
755         if (inp != 0) {
756                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
757                 return (EINVAL);
758         }
759         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
760                 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
761                 if (error) {
762                         return (error);
763                 }
764         }
765         error = sctp_inpcb_alloc(so, vrf_id);
766         if (error) {
767                 return (error);
768         }
769         inp = (struct sctp_inpcb *)so->so_pcb;
770         SCTP_INP_WLOCK(inp);
771         inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;    /* I'm not v6! */
772         ip_inp = &inp->ip_inp.inp;
773         ip_inp->inp_vflag |= INP_IPV4;
774         ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
775 #ifdef IPSEC
776 #if !(defined(__APPLE__))
777         error = ipsec_init_policy(so, &ip_inp->inp_sp);
778 #ifdef SCTP_LOG_CLOSING
779         sctp_log_closing(inp, NULL, 17);
780 #endif
781         if (error != 0) {
782         try_again:
783                 flags = inp->sctp_flags;
784                 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
785                     (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
786 #ifdef SCTP_LOG_CLOSING
787                         sctp_log_closing(inp, NULL, 15);
788 #endif
789                         SCTP_INP_WUNLOCK(inp);
790                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
791                                         SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
792                 } else {
793                         flags = inp->sctp_flags;
794                         if ((flags &  SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
795                                 goto try_again;
796                         } else {
797                                 SCTP_INP_WUNLOCK(inp);
798                         }
799                 }
800                 return (error);
801         }
802 #endif
803 #endif                          /* IPSEC */
804         SCTP_INP_WUNLOCK(inp);
805         return (0);
806 }
807
808 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
809 static int
810 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
811 {
812 #elif defined(__FreeBSD__) || defined(__APPLE__)
813 static int
814 sctp_bind(struct socket *so, struct sockaddr *addr, struct proc *p) {
815 #elif defined(__Panda__) || defined(__Userspace__)
816 int
817 sctp_bind(struct socket *so, struct sockaddr *addr) {
818         void *p = NULL;
819 #elif defined(__Windows__)
820 static int
821 sctp_bind(struct socket *so, struct sockaddr *addr, PKTHREAD p) {
822 #else
823 static int
824 sctp_bind(struct socket *so, struct mbuf *nam, struct proc *p)
825 {
826         struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL;
827
828 #endif
829         struct sctp_inpcb *inp;
830
831         inp = (struct sctp_inpcb *)so->so_pcb;
832         if (inp == NULL) {
833                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
834                 return (EINVAL);
835         }
836         if (addr != NULL) {
837 #ifdef HAVE_SA_LEN
838                 if ((addr->sa_family != AF_INET) ||
839                     (addr->sa_len != sizeof(struct sockaddr_in))) {
840 #else
841                 if (addr->sa_family != AF_INET) {
842 #endif
843                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
844                         return (EINVAL);
845                 }
846         }
847         return (sctp_inpcb_bind(so, addr, NULL, p));
848 }
849
850 #endif
851 #if defined(__Userspace__)
852
853 int
854 sctpconn_attach(struct socket *so, int proto SCTP_UNUSED, uint32_t vrf_id)
855 {
856         struct sctp_inpcb *inp;
857         struct inpcb *ip_inp;
858         int error;
859
860         inp = (struct sctp_inpcb *)so->so_pcb;
861         if (inp != NULL) {
862                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
863                 return (EINVAL);
864         }
865         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
866                 error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
867                 if (error) {
868                         return (error);
869                 }
870         }
871         error = sctp_inpcb_alloc(so, vrf_id);
872         if (error) {
873                 return (error);
874         }
875         inp = (struct sctp_inpcb *)so->so_pcb;
876         SCTP_INP_WLOCK(inp);
877         inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;
878         inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_CONN;
879         ip_inp = &inp->ip_inp.inp;
880         ip_inp->inp_vflag |= INP_CONN;
881         ip_inp->inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
882         SCTP_INP_WUNLOCK(inp);
883         return (0);
884 }
885
886 int
887 sctpconn_bind(struct socket *so, struct sockaddr *addr)
888 {
889         struct sctp_inpcb *inp;
890
891         inp = (struct sctp_inpcb *)so->so_pcb;
892         if (inp == NULL) {
893                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
894                 return (EINVAL);
895         }
896         if (addr != NULL) {
897 #ifdef HAVE_SA_LEN
898                 if ((addr->sa_family != AF_CONN) ||
899                     (addr->sa_len != sizeof(struct sockaddr_conn))) {
900 #else
901                 if (addr->sa_family != AF_CONN) {
902 #endif
903                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
904                         return (EINVAL);
905                 }
906         }
907         return (sctp_inpcb_bind(so, addr, NULL, NULL));
908 }
909
910 #endif
911 #if (defined(__FreeBSD__) && __FreeBSD_version > 690000) || defined(__Windows__) || defined(__Userspace__)
912 void
913 sctp_close(struct socket *so)
914 {
915         struct sctp_inpcb *inp;
916         uint32_t flags;
917
918         inp = (struct sctp_inpcb *)so->so_pcb;
919         if (inp == NULL)
920                 return;
921
922         /* Inform all the lower layer assoc that we
923          * are done.
924          */
925  sctp_must_try_again:
926         flags = inp->sctp_flags;
927 #ifdef SCTP_LOG_CLOSING
928         sctp_log_closing(inp, NULL, 17);
929 #endif
930         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
931             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
932 #if defined(__Userspace__)
933                 if (((so->so_options & SCTP_SO_LINGER) && (so->so_linger == 0)) ||
934                     (so->so_rcv.sb_cc > 0)) {
935 #else
936                 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
937                     (so->so_rcv.sb_cc > 0)) {
938 #endif
939 #ifdef SCTP_LOG_CLOSING
940                         sctp_log_closing(inp, NULL, 13);
941 #endif
942                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
943                                         SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
944                 } else {
945 #ifdef SCTP_LOG_CLOSING
946                         sctp_log_closing(inp, NULL, 14);
947 #endif
948                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
949                                         SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
950                 }
951                 /* The socket is now detached, no matter what
952                  * the state of the SCTP association.
953                  */
954                 SOCK_LOCK(so);
955                 SCTP_SB_CLEAR(so->so_snd);
956                 /* same for the rcv ones, they are only
957                  * here for the accounting/select.
958                  */
959                 SCTP_SB_CLEAR(so->so_rcv);
960
961 #if !defined(__APPLE__)
962                 /* Now null out the reference, we are completely detached. */
963                 so->so_pcb = NULL;
964 #endif
965                 SOCK_UNLOCK(so);
966         } else {
967                 flags = inp->sctp_flags;
968                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
969                         goto sctp_must_try_again;
970                 }
971         }
972         return;
973 }
974
975 #else
976
977
978 int
979 sctp_detach(struct socket *so)
980 {
981         struct sctp_inpcb *inp;
982         uint32_t flags;
983
984         inp = (struct sctp_inpcb *)so->so_pcb;
985         if (inp == NULL) {
986 #if defined(__FreeBSD__) && __FreeBSD_version > 690000
987                 return;
988 #else
989                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
990                 return (EINVAL);
991 #endif
992         }
993  sctp_must_try_again:
994         flags = inp->sctp_flags;
995 #ifdef SCTP_LOG_CLOSING
996         sctp_log_closing(inp, NULL, 17);
997 #endif
998         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
999             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
1000 #if defined(__Userspace__)
1001                 if (((so->so_options & SCTP_SO_LINGER) && (so->so_linger == 0)) ||
1002                     (so->so_rcv.sb_cc > 0)) {
1003 #else
1004                 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
1005                     (so->so_rcv.sb_cc > 0)) {
1006 #endif
1007 #ifdef SCTP_LOG_CLOSING
1008                         sctp_log_closing(inp, NULL, 13);
1009 #endif
1010                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
1011                                         SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
1012                 } else {
1013 #ifdef SCTP_LOG_CLOSING
1014                         sctp_log_closing(inp, NULL, 13);
1015 #endif
1016                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
1017                                         SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
1018                 }
1019                 /* The socket is now detached, no matter what
1020                  * the state of the SCTP association.
1021                  */
1022                 SCTP_SB_CLEAR(so->so_snd);
1023                 /* same for the rcv ones, they are only
1024                  * here for the accounting/select.
1025                  */
1026                 SCTP_SB_CLEAR(so->so_rcv);
1027 #if !defined(__APPLE__)
1028                 /* Now disconnect */
1029                 so->so_pcb = NULL;
1030 #endif
1031         } else {
1032                 flags = inp->sctp_flags;
1033                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
1034                         goto sctp_must_try_again;
1035                 }
1036         }
1037 #if defined(__FreeBSD__) && __FreeBSD_version > 690000
1038         return;
1039 #else
1040         return (0);
1041 #endif
1042 }
1043 #endif
1044
1045 #if defined(__Userspace__)
1046 /* __Userspace__ is not calling sctp_sendm */
1047 #endif
1048 #if !(defined(__Panda__) || defined(__Windows__))
1049 int
1050 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1051 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1052     struct mbuf *control, struct thread *p);
1053
1054 #else
1055 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1056     struct mbuf *control, struct proc *p);
1057
1058 #endif
1059
1060 int
1061 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1062 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1063     struct mbuf *control, struct thread *p)
1064 {
1065 #else
1066 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1067     struct mbuf *control, struct proc *p)
1068 {
1069 #endif
1070         struct sctp_inpcb *inp;
1071         int error;
1072
1073         inp = (struct sctp_inpcb *)so->so_pcb;
1074         if (inp == NULL) {
1075                 if (control) {
1076                         sctp_m_freem(control);
1077                         control = NULL;
1078                 }
1079                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1080                 sctp_m_freem(m);
1081                 return (EINVAL);
1082         }
1083         /* Got to have an to address if we are NOT a connected socket */
1084         if ((addr == NULL) &&
1085             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
1086             (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))) {
1087                 goto connected_type;
1088         } else if (addr == NULL) {
1089                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
1090                 error = EDESTADDRREQ;
1091                 sctp_m_freem(m);
1092                 if (control) {
1093                         sctp_m_freem(control);
1094                         control = NULL;
1095                 }
1096                 return (error);
1097         }
1098 #ifdef INET6
1099         if (addr->sa_family != AF_INET) {
1100                 /* must be a v4 address! */
1101                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EDESTADDRREQ);
1102                 sctp_m_freem(m);
1103                 if (control) {
1104                         sctp_m_freem(control);
1105                         control = NULL;
1106                 }
1107                 error = EDESTADDRREQ;
1108                 return (error);
1109         }
1110 #endif                          /* INET6 */
1111 connected_type:
1112         /* now what about control */
1113         if (control) {
1114                 if (inp->control) {
1115                         SCTP_PRINTF("huh? control set?\n");
1116                         sctp_m_freem(inp->control);
1117                         inp->control = NULL;
1118                 }
1119                 inp->control = control;
1120         }
1121         /* Place the data */
1122         if (inp->pkt) {
1123                 SCTP_BUF_NEXT(inp->pkt_last) = m;
1124                 inp->pkt_last = m;
1125         } else {
1126                 inp->pkt_last = inp->pkt = m;
1127         }
1128         if (
1129 #if defined(__FreeBSD__) || defined(__APPLE__)
1130         /* FreeBSD uses a flag passed */
1131             ((flags & PRUS_MORETOCOME) == 0)
1132 #else
1133             1                   /* Open BSD does not have any "more to come"
1134                                  * indication */
1135 #endif
1136             ) {
1137                 /*
1138                  * note with the current version this code will only be used
1139                  * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
1140                  * re-defining sosend to use the sctp_sosend. One can
1141                  * optionally switch back to this code (by changing back the
1142                  * definitions) but this is not advisable. This code is used
1143                  * by FreeBSD when sending a file with sendfile() though.
1144                  */
1145                 int ret;
1146
1147                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
1148                 inp->pkt = NULL;
1149                 inp->control = NULL;
1150                 return (ret);
1151         } else {
1152                 return (0);
1153         }
1154 }
1155 #endif
1156
1157 int
1158 sctp_disconnect(struct socket *so)
1159 {
1160         struct sctp_inpcb *inp;
1161
1162         inp = (struct sctp_inpcb *)so->so_pcb;
1163         if (inp == NULL) {
1164                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
1165                 return (ENOTCONN);
1166         }
1167         SCTP_INP_RLOCK(inp);
1168         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1169             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1170                 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
1171                         /* No connection */
1172                         SCTP_INP_RUNLOCK(inp);
1173                         return (0);
1174                 } else {
1175                         struct sctp_association *asoc;
1176                         struct sctp_tcb *stcb;
1177
1178                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1179                         if (stcb == NULL) {
1180                                 SCTP_INP_RUNLOCK(inp);
1181                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1182                                 return (EINVAL);
1183                         }
1184                         SCTP_TCB_LOCK(stcb);
1185                         asoc = &stcb->asoc;
1186                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1187                                 /* We are about to be freed, out of here */
1188                                 SCTP_TCB_UNLOCK(stcb);
1189                                 SCTP_INP_RUNLOCK(inp);
1190                                 return (0);
1191                         }
1192 #if defined(__Userspace__)
1193                         if (((so->so_options & SCTP_SO_LINGER) &&
1194                              (so->so_linger == 0)) ||
1195                             (so->so_rcv.sb_cc > 0)) {
1196 #else
1197                         if (((so->so_options & SO_LINGER) &&
1198                              (so->so_linger == 0)) ||
1199                             (so->so_rcv.sb_cc > 0)) {
1200 #endif
1201                                 if (SCTP_GET_STATE(asoc) !=
1202                                     SCTP_STATE_COOKIE_WAIT) {
1203                                         /* Left with Data unread */
1204                                         struct mbuf *err;
1205
1206                                         err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
1207                                         if (err) {
1208                                                 /*
1209                                                  * Fill in the user
1210                                                  * initiated abort
1211                                                  */
1212                                                 struct sctp_paramhdr *ph;
1213
1214                                                 ph = mtod(err, struct sctp_paramhdr *);
1215                                                 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
1216                                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
1217                                                 ph->param_length = htons(SCTP_BUF_LEN(err));
1218                                         }
1219                                         sctp_send_abort_tcb(stcb, err, SCTP_SO_LOCKED);
1220                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
1221                                 }
1222                                 SCTP_INP_RUNLOCK(inp);
1223                                 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
1224                                     (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1225                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1226                                 }
1227                                 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_3);
1228                                 /* No unlock tcb assoc is gone */
1229                                 return (0);
1230                         }
1231                         if (TAILQ_EMPTY(&asoc->send_queue) &&
1232                             TAILQ_EMPTY(&asoc->sent_queue) &&
1233                             (asoc->stream_queue_cnt == 0)) {
1234                                 /* there is nothing queued to send, so done */
1235                                 if (asoc->locked_on_sending) {
1236                                         goto abort_anyway;
1237                                 }
1238                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
1239                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1240                                         /* only send SHUTDOWN 1st time thru */
1241                                         struct sctp_nets *netp;
1242
1243                                         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1244                                             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1245                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1246                                         }
1247                                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1248                                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1249                                         sctp_stop_timers_for_shutdown(stcb);
1250                                         if (stcb->asoc.alternate) {
1251                                                 netp = stcb->asoc.alternate;
1252                                         } else {
1253                                                 netp = stcb->asoc.primary_destination;
1254                                         }
1255                                         sctp_send_shutdown(stcb,netp);
1256                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1257                                                          stcb->sctp_ep, stcb, netp);
1258                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1259                                                          stcb->sctp_ep, stcb, netp);
1260                                         sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1261                                 }
1262                         } else {
1263                                 /*
1264                                  * we still got (or just got) data to send,
1265                                  * so set SHUTDOWN_PENDING
1266                                  */
1267                                 /*
1268                                  * XXX sockets draft says that SCTP_EOF
1269                                  * should be sent with no data. currently,
1270                                  * we will allow user data to be sent first
1271                                  * and move to SHUTDOWN-PENDING
1272                                  */
1273                                 struct sctp_nets *netp;
1274                                 if (stcb->asoc.alternate) {
1275                                         netp = stcb->asoc.alternate;
1276                                 } else {
1277                                         netp = stcb->asoc.primary_destination;
1278                                 }
1279
1280                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1281                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1282                                                  netp);
1283                                 if (asoc->locked_on_sending) {
1284                                         /* Locked to send out the data */
1285                                         struct sctp_stream_queue_pending *sp;
1286                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1287                                         if (sp == NULL) {
1288                                                 SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1289                                                             asoc->locked_on_sending->stream_no);
1290                                         } else {
1291                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
1292                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1293                                         }
1294                                 }
1295                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
1296                                     TAILQ_EMPTY(&asoc->sent_queue) &&
1297                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1298                                         struct mbuf *op_err;
1299                                 abort_anyway:
1300                                         op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
1301                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ+SCTP_LOC_4;
1302                                         sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
1303                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
1304                                         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
1305                                             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1306                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1307                                         }
1308                                         SCTP_INP_RUNLOCK(inp);
1309                                         (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_5);
1310                                         return (0);
1311                                 } else {
1312                                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1313                                 }
1314                         }
1315                         soisdisconnecting(so);
1316                         SCTP_TCB_UNLOCK(stcb);
1317                         SCTP_INP_RUNLOCK(inp);
1318                         return (0);
1319                 }
1320                 /* not reached */
1321         } else {
1322                 /* UDP model does not support this */
1323                 SCTP_INP_RUNLOCK(inp);
1324                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1325                 return (EOPNOTSUPP);
1326         }
1327 }
1328
1329 #if defined(__FreeBSD__) || defined(__Windows__) || defined(__Userspace__)
1330 int
1331 sctp_flush(struct socket *so, int how)
1332 {
1333         /*
1334          * We will just clear out the values and let
1335          * subsequent close clear out the data, if any.
1336          * Note if the user did a shutdown(SHUT_RD) they
1337          * will not be able to read the data, the socket
1338          * will block that from happening.
1339          */
1340         struct sctp_inpcb *inp;
1341
1342         inp = (struct sctp_inpcb *)so->so_pcb;
1343         if (inp == NULL) {
1344                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1345                 return (EINVAL);
1346         }
1347         SCTP_INP_RLOCK(inp);
1348         /* For the 1 to many model this does nothing */
1349         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
1350                 SCTP_INP_RUNLOCK(inp);
1351                 return (0);
1352         }
1353         SCTP_INP_RUNLOCK(inp);
1354         if ((how == PRU_FLUSH_RD) || (how == PRU_FLUSH_RDWR)) {
1355                 /* First make sure the sb will be happy, we don't
1356                  * use these except maybe the count
1357                  */
1358                 SCTP_INP_WLOCK(inp);
1359                 SCTP_INP_READ_LOCK(inp);
1360                 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_CANT_READ;
1361                 SCTP_INP_READ_UNLOCK(inp);
1362                 SCTP_INP_WUNLOCK(inp);
1363                 so->so_rcv.sb_cc = 0;
1364                 so->so_rcv.sb_mbcnt = 0;
1365                 so->so_rcv.sb_mb = NULL;
1366         }
1367         if ((how == PRU_FLUSH_WR) || (how == PRU_FLUSH_RDWR)) {
1368                 /* First make sure the sb will be happy, we don't
1369                  * use these except maybe the count
1370                  */
1371                 so->so_snd.sb_cc = 0;
1372                 so->so_snd.sb_mbcnt = 0;
1373                 so->so_snd.sb_mb = NULL;
1374
1375         }
1376         return (0);
1377 }
1378 #endif
1379
1380 int
1381 sctp_shutdown(struct socket *so)
1382 {
1383         struct sctp_inpcb *inp;
1384
1385         inp = (struct sctp_inpcb *)so->so_pcb;
1386         if (inp == NULL) {
1387                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
1388                 return (EINVAL);
1389         }
1390         SCTP_INP_RLOCK(inp);
1391         /* For UDP model this is a invalid call */
1392         if (!((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1393               (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
1394                 /* Restore the flags that the soshutdown took away. */
1395 #if (defined(__FreeBSD__) && __FreeBSD_version >= 502115) || defined(__Windows__)
1396                 SOCKBUF_LOCK(&so->so_rcv);
1397                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1398                 SOCKBUF_UNLOCK(&so->so_rcv);
1399 #else
1400                 so->so_state &= ~SS_CANTRCVMORE;
1401 #endif
1402                 /* This proc will wakeup for read and do nothing (I hope) */
1403                 SCTP_INP_RUNLOCK(inp);
1404                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
1405                 return (EOPNOTSUPP);
1406         }
1407         /*
1408          * Ok if we reach here its the TCP model and it is either a SHUT_WR
1409          * or SHUT_RDWR. This means we put the shutdown flag against it.
1410          */
1411         {
1412                 struct sctp_tcb *stcb;
1413                 struct sctp_association *asoc;
1414
1415                 if ((so->so_state &
1416                      (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING)) == 0) {
1417                         SCTP_INP_RUNLOCK(inp);
1418                         return (ENOTCONN);
1419                 }
1420                 socantsendmore(so);
1421
1422                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1423                 if (stcb == NULL) {
1424                         /*
1425                          * Ok we hit the case that the shutdown call was
1426                          * made after an abort or something. Nothing to do
1427                          * now.
1428                          */
1429                         SCTP_INP_RUNLOCK(inp);
1430                         return (0);
1431                 }
1432                 SCTP_TCB_LOCK(stcb);
1433                 asoc = &stcb->asoc;
1434                 if (TAILQ_EMPTY(&asoc->send_queue) &&
1435                     TAILQ_EMPTY(&asoc->sent_queue) &&
1436                     (asoc->stream_queue_cnt == 0)) {
1437                         if (asoc->locked_on_sending) {
1438                                 goto abort_anyway;
1439                         }
1440                         /* there is nothing queued to send, so I'm done... */
1441                         if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1442                                 /* only send SHUTDOWN the first time through */
1443                                 struct sctp_nets *netp;
1444
1445                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
1446                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
1447                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
1448                                 }
1449                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
1450                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
1451                                 sctp_stop_timers_for_shutdown(stcb);
1452                                 if (stcb->asoc.alternate) {
1453                                         netp = stcb->asoc.alternate;
1454                                 } else {
1455                                         netp = stcb->asoc.primary_destination;
1456                                 }
1457                                 sctp_send_shutdown(stcb, netp);
1458                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1459                                                  stcb->sctp_ep, stcb, netp);
1460                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1461                                                  stcb->sctp_ep, stcb, netp);
1462                                 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_LOCKED);
1463                         }
1464                 } else {
1465                         /*
1466                          * we still got (or just got) data to send, so set
1467                          * SHUTDOWN_PENDING
1468                          */
1469                         struct sctp_nets *netp;
1470                         if (stcb->asoc.alternate) {
1471                                 netp = stcb->asoc.alternate;
1472                         } else {
1473                                 netp = stcb->asoc.primary_destination;
1474                         }
1475
1476                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1477                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
1478                                          netp);
1479
1480                         if (asoc->locked_on_sending) {
1481                                 /* Locked to send out the data */
1482                                 struct sctp_stream_queue_pending *sp;
1483                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
1484                                 if (sp == NULL) {
1485                                         SCTP_PRINTF("Error, sp is NULL, locked on sending is non-null strm:%d\n",
1486                                                     asoc->locked_on_sending->stream_no);
1487                                 } else {
1488                                         if ((sp->length == 0)  && (sp-> msg_is_complete == 0)) {
1489                                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
1490                                         }
1491                                 }
1492                         }
1493                         if (TAILQ_EMPTY(&asoc->send_queue) &&
1494                             TAILQ_EMPTY(&asoc->sent_queue) &&
1495                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
1496                                 struct mbuf *op_err;
1497                         abort_anyway:
1498                                 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
1499                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ+SCTP_LOC_6;
1500                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
1501                                                           op_err, SCTP_SO_LOCKED);
1502                                 goto skip_unlock;
1503                         } else {
1504                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
1505                         }
1506                 }
1507                 SCTP_TCB_UNLOCK(stcb);
1508         }
1509  skip_unlock:
1510         SCTP_INP_RUNLOCK(inp);
1511         return (0);
1512 }
1513
1514 /*
1515  * copies a "user" presentable address and removes embedded scope, etc.
1516  * returns 0 on success, 1 on error
1517  */
1518 static uint32_t
1519 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1520 {
1521 #ifdef INET6
1522 #if defined(SCTP_EMBEDDED_V6_SCOPE)
1523         struct sockaddr_in6 lsa6;
1524
1525         sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1526             &lsa6);
1527 #endif
1528 #endif
1529 #ifdef HAVE_SA_LEN
1530         memcpy(ss, sa, sa->sa_len);
1531 #else
1532         switch (sa->sa_family) {
1533 #ifdef INET
1534         case AF_INET:
1535                 memcpy(ss, sa, sizeof(struct sockaddr_in));
1536                 break;
1537 #endif
1538 #ifdef INET6
1539         case AF_INET6:
1540                 memcpy(ss, sa, sizeof(struct sockaddr_in6));
1541                 break;
1542 #endif
1543 #if defined(__Userspace__)
1544         case AF_CONN:
1545                 memcpy(ss, sa, sizeof(struct sockaddr_conn));
1546                 break;
1547 #endif
1548         default:
1549                 /* TSNH */
1550                 break;
1551         }
1552 #endif
1553         return (0);
1554 }
1555
1556
1557
1558 /*
1559  * NOTE: assumes addr lock is held
1560  */
1561 static size_t
1562 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1563                            struct sctp_tcb *stcb,
1564                            size_t limit,
1565                            struct sockaddr_storage *sas,
1566                            uint32_t vrf_id)
1567 {
1568         struct sctp_ifn *sctp_ifn;
1569         struct sctp_ifa *sctp_ifa;
1570         size_t actual;
1571         int loopback_scope;
1572 #if defined(INET)
1573         int ipv4_local_scope, ipv4_addr_legal;
1574 #endif
1575 #if defined(INET6)
1576         int local_scope, site_scope, ipv6_addr_legal;
1577 #endif
1578 #if defined(__Userspace__)
1579         int conn_addr_legal;
1580 #endif
1581         struct sctp_vrf *vrf;
1582
1583         actual = 0;
1584         if (limit <= 0)
1585                 return (actual);
1586
1587         if (stcb) {
1588                 /* Turn on all the appropriate scope */
1589                 loopback_scope = stcb->asoc.scope.loopback_scope;
1590 #if defined(INET)
1591                 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
1592                 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
1593 #endif
1594 #if defined(INET6)
1595                 local_scope = stcb->asoc.scope.local_scope;
1596                 site_scope = stcb->asoc.scope.site_scope;
1597                 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
1598 #endif
1599 #if defined(__Userspace__)
1600                 conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
1601 #endif
1602         } else {
1603                 /* Use generic values for endpoints. */
1604                 loopback_scope = 1;
1605 #if defined(INET)
1606                 ipv4_local_scope = 1;
1607 #endif
1608 #if defined(INET6)
1609                 local_scope = 1;
1610                 site_scope = 1;
1611 #endif
1612                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1613 #if defined(INET6)
1614                         ipv6_addr_legal = 1;
1615 #endif
1616 #if defined(INET)
1617                         if (SCTP_IPV6_V6ONLY(inp)) {
1618                                 ipv4_addr_legal = 0;
1619                         } else {
1620                                 ipv4_addr_legal = 1;
1621                         }
1622 #endif
1623 #if defined(__Userspace__)
1624                         conn_addr_legal = 0;
1625 #endif
1626                 } else {
1627 #if defined(INET6)
1628                         ipv6_addr_legal = 0;
1629 #endif
1630 #if defined(__Userspace__)
1631                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
1632                                 conn_addr_legal = 1;
1633 #if defined(INET)
1634                                 ipv4_addr_legal = 0;
1635 #endif
1636                         } else {
1637                                 conn_addr_legal = 0;
1638 #if defined(INET)
1639                                 ipv4_addr_legal = 1;
1640 #endif
1641                         }
1642 #else
1643 #if defined(INET)
1644                         ipv4_addr_legal = 1;
1645 #endif
1646 #endif
1647                 }
1648         }
1649         vrf = sctp_find_vrf(vrf_id);
1650         if (vrf == NULL) {
1651                 return (0);
1652         }
1653         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1654                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1655                         if ((loopback_scope == 0) &&
1656                             SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1657                                 /* Skip loopback if loopback_scope not set */
1658                                 continue;
1659                         }
1660                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1661                                 if (stcb) {
1662                                         /*
1663                                          * For the BOUND-ALL case, the list
1664                                          * associated with a TCB is Always
1665                                          * considered a reverse list.. i.e.
1666                                          * it lists addresses that are NOT
1667                                          * part of the association. If this
1668                                          * is one of those we must skip it.
1669                                          */
1670                                         if (sctp_is_addr_restricted(stcb,
1671                                                                     sctp_ifa)) {
1672                                                 continue;
1673                                         }
1674                                 }
1675                                 switch (sctp_ifa->address.sa.sa_family) {
1676 #ifdef INET
1677                                 case AF_INET:
1678                                         if (ipv4_addr_legal) {
1679                                                 struct sockaddr_in *sin;
1680
1681                                                 sin = &sctp_ifa->address.sin;
1682                                                 if (sin->sin_addr.s_addr == 0) {
1683                                                         /*
1684                                                          * we skip unspecifed
1685                                                          * addresses
1686                                                          */
1687                                                         continue;
1688                                                 }
1689 #if defined(__FreeBSD__)
1690                                                 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1691                                                                      &sin->sin_addr) != 0) {
1692                                                         continue;
1693                                                 }
1694 #endif
1695                                                 if ((ipv4_local_scope == 0) &&
1696                                                     (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1697                                                         continue;
1698                                                 }
1699 #ifdef INET6
1700                                                 if (sctp_is_feature_on(inp,SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
1701                                                         in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1702                                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1703                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1704                                                         actual += sizeof(struct sockaddr_in6);
1705                                                 } else {
1706 #endif
1707                                                         memcpy(sas, sin, sizeof(*sin));
1708                                                         ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1709                                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1710                                                         actual += sizeof(*sin);
1711 #ifdef INET6
1712                                                 }
1713 #endif
1714                                                 if (actual >= limit) {
1715                                                         return (actual);
1716                                                 }
1717                                         } else {
1718                                                 continue;
1719                                         }
1720                                         break;
1721 #endif
1722 #ifdef INET6
1723                                 case AF_INET6:
1724                                         if (ipv6_addr_legal) {
1725                                                 struct sockaddr_in6 *sin6;
1726
1727 #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
1728                                                 struct sockaddr_in6 lsa6;
1729 #endif
1730                                                 sin6 = &sctp_ifa->address.sin6;
1731                                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1732                                                         /*
1733                                                          * we skip unspecifed
1734                                                          * addresses
1735                                                          */
1736                                                         continue;
1737                                                 }
1738 #if defined(__FreeBSD__)
1739                                                 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1740                                                                      &sin6->sin6_addr) != 0) {
1741                                                         continue;
1742                                                 }
1743 #endif
1744                                                 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1745                                                         if (local_scope == 0)
1746                                                                 continue;
1747 #if defined(SCTP_EMBEDDED_V6_SCOPE)
1748                                                         if (sin6->sin6_scope_id == 0) {
1749 #ifdef SCTP_KAME
1750                                                                 if (sa6_recoverscope(sin6) != 0)
1751                                                                         /*
1752                                                                          * bad link
1753                                                                          * local
1754                                                                          * address
1755                                                                          */
1756                                                                         continue;
1757 #else
1758                                                                 lsa6 = *sin6;
1759                                                                 if (in6_recoverscope(&lsa6,
1760                                                                                      &lsa6.sin6_addr,
1761                                                                                      NULL))
1762                                                                         /*
1763                                                                          * bad link
1764                                                                          * local
1765                                                                          * address
1766                                                                          */
1767                                                                 continue;
1768                                                                 sin6 = &lsa6;
1769 #endif                          /* SCTP_KAME */
1770                                                         }
1771 #endif /* SCTP_EMBEDDED_V6_SCOPE */
1772                                                 }
1773                                                 if ((site_scope == 0) &&
1774                                                     (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1775                                                         continue;
1776                                                 }
1777                                                 memcpy(sas, sin6, sizeof(*sin6));
1778                                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1779                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1780                                                 actual += sizeof(*sin6);
1781                                                 if (actual >= limit) {
1782                                                         return (actual);
1783                                                 }
1784                                         } else {
1785                                                 continue;
1786                                         }
1787                                         break;
1788 #endif
1789 #if defined(__Userspace__)
1790                                 case AF_CONN:
1791                                         if (conn_addr_legal) {
1792                                                 memcpy(sas, &sctp_ifa->address.sconn, sizeof(struct sockaddr_conn));
1793                                                 ((struct sockaddr_conn *)sas)->sconn_port = inp->sctp_lport;
1794                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_conn));
1795                                                 actual += sizeof(struct sockaddr_conn);
1796                                                 if (actual >= limit) {
1797                                                         return (actual);
1798                                                 }
1799                                         } else {
1800                                                 continue;
1801                                         }
1802 #endif
1803                                 default:
1804                                         /* TSNH */
1805                                         break;
1806                                 }
1807                         }
1808                 }
1809         } else {
1810                 struct sctp_laddr *laddr;
1811 #ifndef HAVE_SA_LEN
1812                 uint32_t sa_len = 0;
1813 #endif
1814
1815                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1816                         if (stcb) {
1817                                 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1818                                         continue;
1819                                 }
1820                         }
1821                         if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1822                                 continue;
1823                         switch (laddr->ifa->address.sa.sa_family) {
1824 #ifdef INET
1825                         case AF_INET:
1826                                 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1827                                 break;
1828 #endif
1829 #ifdef INET6
1830                         case AF_INET6:
1831                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1832                                 break;
1833 #endif
1834 #if defined(__Userspace__)
1835                         case AF_CONN:
1836                                 ((struct sockaddr_conn *)sas)->sconn_port = inp->sctp_lport;
1837                                 break;
1838 #endif
1839                         default:
1840                                 /* TSNH */
1841                                 break;
1842                         }
1843 #ifdef HAVE_SA_LEN
1844                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1845                                                           laddr->ifa->address.sa.sa_len);
1846                         actual += laddr->ifa->address.sa.sa_len;
1847 #else
1848                         switch (laddr->ifa->address.sa.sa_family) {
1849 #ifdef INET
1850                         case AF_INET:
1851                                 sa_len = sizeof(struct sockaddr_in);
1852                                 break;
1853 #endif
1854 #ifdef INET6
1855                         case AF_INET6:
1856                                 sa_len = sizeof(struct sockaddr_in6);
1857                                 break;
1858 #endif
1859 #if defined(__Userspace__)
1860                         case AF_CONN:
1861                                 sa_len = sizeof(struct sockaddr_conn);
1862                                 break;
1863 #endif
1864                         default:
1865                                 /* TSNH */
1866                                 break;
1867                         }
1868                         sas = (struct sockaddr_storage *)((caddr_t)sas + sa_len);
1869                         actual += sa_len;
1870 #endif
1871                         if (actual >= limit) {
1872                                 return (actual);
1873                         }
1874                 }
1875         }
1876         return (actual);
1877 }
1878
1879 static size_t
1880 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1881                        struct sctp_tcb *stcb,
1882                        size_t limit,
1883                        struct sockaddr_storage *sas)
1884 {
1885         size_t size = 0;
1886 #ifdef SCTP_MVRF
1887         uint32_t id;
1888 #endif
1889
1890         SCTP_IPI_ADDR_RLOCK();
1891 #ifdef SCTP_MVRF
1892 /*
1893  * FIX ME: ?? this WILL report duplicate addresses if they appear
1894  * in more than one VRF.
1895  */
1896         /* fill up addresses for all VRFs on the endpoint */
1897         for (id = 0; (id < inp->num_vrfs) && (size < limit); id++) {
1898                 size += sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1899                                                    inp->m_vrf_ids[id]);
1900                 sas = (struct sockaddr_storage *)((caddr_t)sas + size);
1901         }
1902 #else
1903         /* fill up addresses for the endpoint's default vrf */
1904         size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1905                                           inp->def_vrf_id);
1906 #endif
1907         SCTP_IPI_ADDR_RUNLOCK();
1908         return (size);
1909 }
1910
1911 /*
1912  * NOTE: assumes addr lock is held
1913  */
1914 static int
1915 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1916 {
1917         int cnt = 0;
1918         struct sctp_vrf *vrf = NULL;
1919
1920         /*
1921          * In both sub-set bound an bound_all cases we return the MAXIMUM
1922          * number of addresses that you COULD get. In reality the sub-set
1923          * bound may have an exclusion list for a given TCB OR in the
1924          * bound-all case a TCB may NOT include the loopback or other
1925          * addresses as well.
1926          */
1927         vrf = sctp_find_vrf(vrf_id);
1928         if (vrf == NULL) {
1929                 return (0);
1930         }
1931         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1932                 struct sctp_ifn *sctp_ifn;
1933                 struct sctp_ifa *sctp_ifa;
1934
1935                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1936                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1937                                 /* Count them if they are the right type */
1938                                 switch (sctp_ifa->address.sa.sa_family) {
1939 #ifdef INET
1940                                 case AF_INET:
1941                                         if (sctp_is_feature_on(inp,SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1942                                                 cnt += sizeof(struct sockaddr_in6);
1943                                         else
1944                                                 cnt += sizeof(struct sockaddr_in);
1945                                         break;
1946 #endif
1947 #ifdef INET6
1948                                 case AF_INET6:
1949                                         cnt += sizeof(struct sockaddr_in6);
1950                                         break;
1951 #endif
1952 #if defined(__Userspace__)
1953                                 case AF_CONN:
1954                                         cnt += sizeof(struct sockaddr_conn);
1955                                         break;
1956 #endif
1957                                 default:
1958                                         break;
1959                                 }
1960                         }
1961                 }
1962         } else {
1963                 struct sctp_laddr *laddr;
1964
1965                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1966                         switch (laddr->ifa->address.sa.sa_family) {
1967 #ifdef INET
1968                         case AF_INET:
1969                                 if (sctp_is_feature_on(inp,SCTP_PCB_FLAGS_NEEDS_MAPPED_V4))
1970                                         cnt += sizeof(struct sockaddr_in6);
1971                                 else
1972                                         cnt += sizeof(struct sockaddr_in);
1973                                 break;
1974 #endif
1975 #ifdef INET6
1976                         case AF_INET6:
1977                                 cnt += sizeof(struct sockaddr_in6);
1978                                 break;
1979 #endif
1980 #if defined(__Userspace__)
1981                         case AF_CONN:
1982                                 cnt += sizeof(struct sockaddr_conn);
1983                                 break;
1984 #endif
1985                         default:
1986                                 break;
1987                         }
1988                 }
1989         }
1990         return (cnt);
1991 }
1992
1993 static int
1994 sctp_count_max_addresses(struct sctp_inpcb *inp)
1995 {
1996         int cnt = 0;
1997 #ifdef SCTP_MVRF
1998         int id;
1999 #endif
2000
2001         SCTP_IPI_ADDR_RLOCK();
2002 #ifdef SCTP_MVRF
2003 /*
2004  * FIX ME: ?? this WILL count duplicate addresses if they appear
2005  * in more than one VRF.
2006  */
2007         /* count addresses for all VRFs on the endpoint */
2008         for (id = 0; id < inp->num_vrfs; id++) {
2009                 cnt += sctp_count_max_addresses_vrf(inp, inp->m_vrf_ids[id]);
2010         }
2011 #else
2012         /* count addresses for the endpoint's default VRF */
2013         cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
2014 #endif
2015         SCTP_IPI_ADDR_RUNLOCK();
2016         return (cnt);
2017 }
2018
2019 static int
2020 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
2021                   size_t optsize, void *p, int delay)
2022 {
2023         int error = 0;
2024         int creat_lock_on = 0;
2025         struct sctp_tcb *stcb = NULL;
2026         struct sockaddr *sa;
2027         int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
2028         uint32_t vrf_id;
2029         int bad_addresses = 0;
2030         sctp_assoc_t *a_id;
2031
2032         SCTPDBG(SCTP_DEBUG_PCB1, "Connectx called\n");
2033
2034         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2035             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
2036                 /* We are already connected AND the TCP model */
2037                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
2038                 return (EADDRINUSE);
2039         }
2040
2041         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
2042             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
2043                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2044                 return (EINVAL);
2045         }
2046
2047         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2048                 SCTP_INP_RLOCK(inp);
2049                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2050                 SCTP_INP_RUNLOCK(inp);
2051         }
2052         if (stcb) {
2053                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
2054                 return (EALREADY);
2055         }
2056         SCTP_INP_INCR_REF(inp);
2057         SCTP_ASOC_CREATE_LOCK(inp);
2058         creat_lock_on = 1;
2059         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
2060             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
2061                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
2062                 error = EFAULT;
2063                 goto out_now;
2064         }
2065         totaddrp = (int *)optval;
2066         totaddr = *totaddrp;
2067         sa = (struct sockaddr *)(totaddrp + 1);
2068         stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)), &bad_addresses);
2069         if ((stcb != NULL) || bad_addresses) {
2070                 /* Already have or am bring up an association */
2071                 SCTP_ASOC_CREATE_UNLOCK(inp);
2072                 creat_lock_on = 0;
2073                 if (stcb)
2074                         SCTP_TCB_UNLOCK(stcb);
2075                 if (bad_addresses == 0) {
2076                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
2077                         error = EALREADY;
2078                 }
2079                 goto out_now;
2080         }
2081 #ifdef INET6
2082         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
2083             (num_v6 > 0)) {
2084                 error = EINVAL;
2085                 goto out_now;
2086         }
2087         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2088             (num_v4 > 0)) {
2089                 struct in6pcb *inp6;
2090
2091                 inp6 = (struct in6pcb *)inp;
2092                 if (SCTP_IPV6_V6ONLY(inp6)) {
2093                         /*
2094                          * if IPV6_V6ONLY flag, ignore connections destined
2095                          * to a v4 addr or v4-mapped addr
2096                          */
2097                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2098                         error = EINVAL;
2099                         goto out_now;
2100                 }
2101         }
2102 #endif                          /* INET6 */
2103         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
2104             SCTP_PCB_FLAGS_UNBOUND) {
2105                 /* Bind a ephemeral port */
2106                 error = sctp_inpcb_bind(so, NULL, NULL, p);
2107                 if (error) {
2108                         goto out_now;
2109                 }
2110         }
2111
2112         /* FIX ME: do we want to pass in a vrf on the connect call? */
2113         vrf_id = inp->def_vrf_id;
2114
2115
2116         /* We are GOOD to go */
2117         stcb = sctp_aloc_assoc(inp, sa, &error, 0, vrf_id,
2118 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2119                                (struct thread *)p
2120 #elif defined(__Windows__)
2121                                (PKTHREAD)p
2122 #else
2123                                (struct proc *)p
2124 #endif
2125                 );
2126         if (stcb == NULL) {
2127                 /* Gak! no memory */
2128                 goto out_now;
2129         }
2130         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2131                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2132                 /* Set the connected flag so we can queue data */
2133                 soisconnecting(so);
2134         }
2135         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
2136         /* move to second address */
2137         switch (sa->sa_family) {
2138 #ifdef INET
2139         case AF_INET:
2140                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
2141                 break;
2142 #endif
2143 #ifdef INET6
2144         case AF_INET6:
2145                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
2146                 break;
2147 #endif
2148         default:
2149                 break;
2150         }
2151
2152         error = 0;
2153         sctp_connectx_helper_add(stcb, sa, (totaddr-1), &error);
2154         /* Fill in the return id */
2155         if (error) {
2156                 (void)sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_6);
2157                 goto out_now;
2158         }
2159         a_id = (sctp_assoc_t *)optval;
2160         *a_id = sctp_get_associd(stcb);
2161
2162         /* initialize authentication parameters for the assoc */
2163         sctp_initialize_auth_params(inp, stcb);
2164
2165         if (delay) {
2166                 /* doing delayed connection */
2167                 stcb->asoc.delayed_connection = 1;
2168                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2169         } else {
2170                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2171                 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
2172         }
2173         SCTP_TCB_UNLOCK(stcb);
2174         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2175                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2176                 /* Set the connected flag so we can queue data */
2177                 soisconnecting(so);
2178         }
2179  out_now:
2180         if (creat_lock_on) {
2181                 SCTP_ASOC_CREATE_UNLOCK(inp);
2182         }
2183         SCTP_INP_DECR_REF(inp);
2184         return (error);
2185 }
2186
2187 #define SCTP_FIND_STCB(inp, stcb, assoc_id) { \
2188         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||\
2189             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { \
2190                 SCTP_INP_RLOCK(inp); \
2191                 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
2192                 if (stcb) { \
2193                         SCTP_TCB_LOCK(stcb); \
2194                 } \
2195                 SCTP_INP_RUNLOCK(inp); \
2196         } else if (assoc_id > SCTP_ALL_ASSOC) { \
2197                 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
2198                 if (stcb == NULL) { \
2199                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT); \
2200                         error = ENOENT; \
2201                         break; \
2202                 } \
2203         } else { \
2204                 stcb = NULL; \
2205         } \
2206   }
2207
2208
2209 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) {\
2210         if (size < sizeof(type)) { \
2211                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL); \
2212                 error = EINVAL; \
2213                 break; \
2214         } else { \
2215                 destp = (type *)srcp; \
2216         } \
2217       }
2218
2219 #if defined(__Panda__) || defined(__Userspace__)
2220 int
2221 #else
2222 static int
2223 #endif
2224 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
2225             void *p) {
2226         struct sctp_inpcb *inp = NULL;
2227         int error, val = 0;
2228         struct sctp_tcb *stcb = NULL;
2229
2230         if (optval == NULL) {
2231                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2232                 return (EINVAL);
2233         }
2234
2235         inp = (struct sctp_inpcb *)so->so_pcb;
2236         if (inp == NULL) {
2237                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2238                 return EINVAL;
2239         }
2240         error = 0;
2241
2242         switch (optname) {
2243         case SCTP_NODELAY:
2244         case SCTP_AUTOCLOSE:
2245         case SCTP_EXPLICIT_EOR:
2246         case SCTP_AUTO_ASCONF:
2247         case SCTP_DISABLE_FRAGMENTS:
2248         case SCTP_I_WANT_MAPPED_V4_ADDR:
2249         case SCTP_USE_EXT_RCVINFO:
2250                 SCTP_INP_RLOCK(inp);
2251                 switch (optname) {
2252                 case SCTP_DISABLE_FRAGMENTS:
2253                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
2254                         break;
2255                 case SCTP_I_WANT_MAPPED_V4_ADDR:
2256                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
2257                         break;
2258                 case SCTP_AUTO_ASCONF:
2259                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2260                                 /* only valid for bound all sockets */
2261                                 val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2262                         } else {
2263                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2264                                 error = EINVAL;
2265                                 goto flags_out;
2266                         }
2267                         break;
2268                 case SCTP_EXPLICIT_EOR:
2269                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
2270                         break;
2271                 case SCTP_NODELAY:
2272                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
2273                         break;
2274                 case SCTP_USE_EXT_RCVINFO:
2275                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
2276                         break;
2277                 case SCTP_AUTOCLOSE:
2278                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
2279                                 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
2280                         else
2281                                 val = 0;
2282                         break;
2283
2284                 default:
2285                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
2286                         error = ENOPROTOOPT;
2287                 } /* end switch (sopt->sopt_name) */
2288                 if (*optsize < sizeof(val)) {
2289                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2290                         error = EINVAL;
2291                 }
2292         flags_out:
2293                 SCTP_INP_RUNLOCK(inp);
2294                 if (error == 0) {
2295                         /* return the option value */
2296                         *(int *)optval = val;
2297                         *optsize = sizeof(val);
2298                 }
2299                 break;
2300         case SCTP_GET_PACKET_LOG:
2301         {
2302 #ifdef  SCTP_PACKET_LOGGING
2303                 uint8_t *target;
2304                 int ret;
2305
2306                 SCTP_CHECK_AND_CAST(target, optval, uint8_t, *optsize);
2307                 ret = sctp_copy_out_packet_log(target , (int)*optsize);
2308                 *optsize = ret;
2309 #else
2310                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2311                 error = EOPNOTSUPP;
2312 #endif
2313                 break;
2314         }
2315         case SCTP_REUSE_PORT:
2316         {
2317                 uint32_t *value;
2318
2319                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2320                         /* Can't do this for a 1-m socket */
2321                         error = EINVAL;
2322                         break;
2323                 }
2324                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2325                 *value = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
2326                 *optsize = sizeof(uint32_t);
2327                 break;
2328         }
2329         case SCTP_PARTIAL_DELIVERY_POINT:
2330         {
2331                 uint32_t *value;
2332
2333                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2334                 *value = inp->partial_delivery_point;
2335                 *optsize = sizeof(uint32_t);
2336                 break;
2337         }
2338         case SCTP_FRAGMENT_INTERLEAVE:
2339         {
2340                 uint32_t *value;
2341
2342                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2343                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
2344                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
2345                                 *value = SCTP_FRAG_LEVEL_2;
2346                         } else {
2347                                 *value = SCTP_FRAG_LEVEL_1;
2348                         }
2349                 } else {
2350                         *value = SCTP_FRAG_LEVEL_0;
2351                 }
2352                 *optsize = sizeof(uint32_t);
2353                 break;
2354         }
2355         case SCTP_CMT_ON_OFF:
2356         {
2357                 struct sctp_assoc_value *av;
2358
2359                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2360                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2361                 if (stcb) {
2362                         av->assoc_value = stcb->asoc.sctp_cmt_on_off;
2363                         SCTP_TCB_UNLOCK(stcb);
2364                 } else {
2365                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2366                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2367                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2368                                 SCTP_INP_RLOCK(inp);
2369                                 av->assoc_value = inp->sctp_cmt_on_off;
2370                                 SCTP_INP_RUNLOCK(inp);
2371                         } else {
2372                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2373                                 error = EINVAL;
2374                         }
2375                 }
2376                 if (error == 0) {
2377                         *optsize = sizeof(struct sctp_assoc_value);
2378                 }
2379                 break;
2380         }
2381         case SCTP_PLUGGABLE_CC:
2382         {
2383                 struct sctp_assoc_value *av;
2384
2385                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2386                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2387                 if (stcb) {
2388                         av->assoc_value = stcb->asoc.congestion_control_module;
2389                         SCTP_TCB_UNLOCK(stcb);
2390                 } else {
2391                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2392                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2393                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2394                                 SCTP_INP_RLOCK(inp);
2395                                 av->assoc_value = inp->sctp_ep.sctp_default_cc_module;
2396                                 SCTP_INP_RUNLOCK(inp);
2397                         } else {
2398                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2399                                 error = EINVAL;
2400                         }
2401                 }
2402                 if (error == 0) {
2403                         *optsize = sizeof(struct sctp_assoc_value);
2404                 }
2405                 break;
2406         }
2407         case SCTP_CC_OPTION:
2408         {
2409                 struct sctp_cc_option *cc_opt;
2410
2411                 SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, *optsize);
2412                 SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
2413                 if (stcb == NULL) {
2414                         error = EINVAL;
2415                 } else {
2416                         if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
2417                                 error = ENOTSUP;
2418                         } else {
2419                                 error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option)(stcb, 0, cc_opt);
2420                                 *optsize = sizeof(struct sctp_cc_option);
2421                         }
2422                         SCTP_TCB_UNLOCK(stcb);
2423                 }
2424                 break;
2425         }
2426         case SCTP_PLUGGABLE_SS:
2427         {
2428                 struct sctp_assoc_value *av;
2429
2430                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2431                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2432                 if (stcb) {
2433                         av->assoc_value = stcb->asoc.stream_scheduling_module;
2434                         SCTP_TCB_UNLOCK(stcb);
2435                 } else {
2436                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2437                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2438                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2439                                 SCTP_INP_RLOCK(inp);
2440                                 av->assoc_value = inp->sctp_ep.sctp_default_ss_module;
2441                                 SCTP_INP_RUNLOCK(inp);
2442                         } else {
2443                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2444                                 error = EINVAL;
2445                         }
2446                 }
2447                 if (error == 0) {
2448                         *optsize = sizeof(struct sctp_assoc_value);
2449                 }
2450                 break;
2451         }
2452         case SCTP_SS_VALUE:
2453         {
2454                 struct sctp_stream_value *av;
2455
2456                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, *optsize);
2457                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2458                 if (stcb) {
2459                         if (stcb->asoc.ss_functions.sctp_ss_get_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
2460                                                                       &av->stream_value) < 0) {
2461                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2462                                 error = EINVAL;
2463                         } else {
2464                                 *optsize = sizeof(struct sctp_stream_value);
2465                         }
2466                         SCTP_TCB_UNLOCK(stcb);
2467                 } else {
2468                         /* Can't get stream value without association */
2469                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2470                         error = EINVAL;
2471                 }
2472                 break;
2473         }
2474         case SCTP_GET_ADDR_LEN:
2475         {
2476                 struct sctp_assoc_value *av;
2477
2478                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2479                 error = EINVAL;
2480 #ifdef INET
2481                 if (av->assoc_value == AF_INET) {
2482                         av->assoc_value = sizeof(struct sockaddr_in);
2483                         error = 0;
2484                 }
2485 #endif
2486 #ifdef INET6
2487                 if (av->assoc_value == AF_INET6) {
2488                         av->assoc_value = sizeof(struct sockaddr_in6);
2489                         error = 0;
2490                 }
2491 #endif
2492 #if defined(__Userspace__)
2493                 if (av->assoc_value == AF_CONN) {
2494                         av->assoc_value = sizeof(struct sockaddr_conn);
2495                         error = 0;
2496                 }
2497 #endif
2498                 if (error) {
2499                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2500                 } else {
2501                         *optsize = sizeof(struct sctp_assoc_value);
2502                 }
2503                 break;
2504         }
2505         case SCTP_GET_ASSOC_NUMBER:
2506         {
2507                 uint32_t *value, cnt;
2508
2509                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2510                 cnt = 0;
2511                 SCTP_INP_RLOCK(inp);
2512                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2513                         cnt++;
2514                 }
2515                 SCTP_INP_RUNLOCK(inp);
2516                 *value = cnt;
2517                 *optsize = sizeof(uint32_t);
2518                 break;
2519         }
2520         case SCTP_GET_ASSOC_ID_LIST:
2521         {
2522                 struct sctp_assoc_ids *ids;
2523                 unsigned int at, limit;
2524
2525                 SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
2526                 at = 0;
2527                 limit = (*optsize-sizeof(uint32_t))/ sizeof(sctp_assoc_t);
2528                 SCTP_INP_RLOCK(inp);
2529                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
2530                         if (at < limit) {
2531                                 ids->gaids_assoc_id[at++] = sctp_get_associd(stcb);
2532                         } else {
2533                                 error = EINVAL;
2534                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2535                                 break;
2536                         }
2537                 }
2538                 SCTP_INP_RUNLOCK(inp);
2539                 if (error == 0) {
2540                         ids->gaids_number_of_ids = at;
2541                         *optsize = ((at * sizeof(sctp_assoc_t)) + sizeof(uint32_t));
2542                 }
2543                 break;
2544         }
2545         case SCTP_CONTEXT:
2546         {
2547                 struct sctp_assoc_value *av;
2548
2549                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2550                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2551
2552                 if (stcb) {
2553                         av->assoc_value = stcb->asoc.context;
2554                         SCTP_TCB_UNLOCK(stcb);
2555                 } else {
2556                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2557                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2558                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2559                                 SCTP_INP_RLOCK(inp);
2560                                 av->assoc_value = inp->sctp_context;
2561                                 SCTP_INP_RUNLOCK(inp);
2562                         } else {
2563                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2564                                 error = EINVAL;
2565                         }
2566                 }
2567                 if (error == 0) {
2568                         *optsize = sizeof(struct sctp_assoc_value);
2569                 }
2570                 break;
2571         }
2572         case SCTP_VRF_ID:
2573         {
2574                 uint32_t *default_vrfid;
2575
2576                 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, *optsize);
2577                 *default_vrfid = inp->def_vrf_id;
2578                 *optsize = sizeof(uint32_t);
2579                 break;
2580         }
2581         case SCTP_GET_ASOC_VRF:
2582         {
2583                 struct sctp_assoc_value *id;
2584
2585                 SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
2586                 SCTP_FIND_STCB(inp, stcb, id->assoc_id);
2587                 if (stcb == NULL) {
2588                         error = EINVAL;
2589                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2590                 } else {
2591                         id->assoc_value = stcb->asoc.vrf_id;
2592                         *optsize = sizeof(struct sctp_assoc_value);
2593                 }
2594                 break;
2595         }
2596         case SCTP_GET_VRF_IDS:
2597         {
2598 #ifdef SCTP_MVRF
2599                 int siz_needed;
2600                 uint32_t *vrf_ids;
2601
2602                 SCTP_CHECK_AND_CAST(vrf_ids, optval, uint32_t, *optsize);
2603                 siz_needed = inp->num_vrfs * sizeof(uint32_t);
2604                 if (*optsize < siz_needed) {
2605                         error = EINVAL;
2606                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
2607                 } else {
2608                         memcpy(vrf_ids, inp->m_vrf_ids, siz_needed);
2609                         *optsize = siz_needed;
2610                 }
2611 #else
2612                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
2613                 error = EOPNOTSUPP;
2614 #endif
2615                 break;
2616         }
2617         case SCTP_GET_NONCE_VALUES:
2618         {
2619                 struct sctp_get_nonce_values *gnv;
2620
2621                 SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
2622                 SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
2623
2624                 if (stcb) {
2625                         gnv->gn_peers_tag = stcb->asoc.peer_vtag;
2626                         gnv->gn_local_tag = stcb->asoc.my_vtag;
2627                         SCTP_TCB_UNLOCK(stcb);
2628                         *optsize = sizeof(struct sctp_get_nonce_values);
2629                 } else {
2630                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2631                         error = ENOTCONN;
2632                 }
2633                 break;
2634         }
2635         case SCTP_DELAYED_SACK:
2636         {
2637                 struct sctp_sack_info *sack;
2638
2639                 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, *optsize);
2640                 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
2641                 if (stcb) {
2642                         sack->sack_delay = stcb->asoc.delayed_ack;
2643                         sack->sack_freq = stcb->asoc.sack_freq;
2644                         SCTP_TCB_UNLOCK(stcb);
2645                 } else {
2646                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2647                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2648                             (sack->sack_assoc_id == SCTP_FUTURE_ASSOC)) {
2649                                 SCTP_INP_RLOCK(inp);
2650                                 sack->sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2651                                 sack->sack_freq = inp->sctp_ep.sctp_sack_freq;
2652                                 SCTP_INP_RUNLOCK(inp);
2653                         } else {
2654                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2655                                 error = EINVAL;
2656                         }
2657                 }
2658                 if (error == 0) {
2659                         *optsize = sizeof(struct sctp_sack_info);
2660                 }
2661                 break;
2662         }
2663         case SCTP_GET_SNDBUF_USE:
2664         {
2665                 struct sctp_sockstat *ss;
2666
2667                 SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
2668                 SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
2669
2670                 if (stcb) {
2671                         ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
2672                         ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
2673                                                  stcb->asoc.size_on_all_streams);
2674                         SCTP_TCB_UNLOCK(stcb);
2675                         *optsize = sizeof(struct sctp_sockstat);
2676                 } else {
2677                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2678                         error = ENOTCONN;
2679                 }
2680                 break;
2681         }
2682         case SCTP_MAX_BURST:
2683         {
2684 #if defined(__FreeBSD__) && __FreeBSD_version < 900000
2685                 uint8_t *value;
2686
2687                 SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
2688
2689                 SCTP_INP_RLOCK(inp);
2690                 if (inp->sctp_ep.max_burst < 256) {
2691                         *value = inp->sctp_ep.max_burst;
2692                 } else {
2693                         *value = 255;
2694                 }
2695                 SCTP_INP_RUNLOCK(inp);
2696                 *optsize = sizeof(uint8_t);
2697 #else
2698                 struct sctp_assoc_value *av;
2699
2700                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2701                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2702
2703                 if (stcb) {
2704                         av->assoc_value = stcb->asoc.max_burst;
2705                         SCTP_TCB_UNLOCK(stcb);
2706                 } else {
2707                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2708                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2709                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2710                                 SCTP_INP_RLOCK(inp);
2711                                 av->assoc_value = inp->sctp_ep.max_burst;
2712                                 SCTP_INP_RUNLOCK(inp);
2713                         } else {
2714                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2715                                 error = EINVAL;
2716                         }
2717                 }
2718                 if (error == 0) {
2719                         *optsize = sizeof(struct sctp_assoc_value);
2720                 }
2721 #endif
2722                 break;
2723         }
2724         case SCTP_MAXSEG:
2725         {
2726                 struct sctp_assoc_value *av;
2727                 int ovh;
2728
2729                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
2730                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2731
2732                 if (stcb) {
2733                         av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
2734                         SCTP_TCB_UNLOCK(stcb);
2735                 } else {
2736                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2737                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
2738                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
2739                                 SCTP_INP_RLOCK(inp);
2740                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2741                                         ovh = SCTP_MED_OVERHEAD;
2742                                 } else {
2743                                         ovh = SCTP_MED_V4_OVERHEAD;
2744                                 }
2745                                 if (inp->sctp_frag_point >= SCTP_DEFAULT_MAXSEGMENT)
2746                                         av->assoc_value = 0;
2747                                 else
2748                                         av->assoc_value = inp->sctp_frag_point - ovh;
2749                                 SCTP_INP_RUNLOCK(inp);
2750                         } else {
2751                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
2752                                 error = EINVAL;
2753                         }
2754                 }
2755                 if (error == 0) {
2756                         *optsize = sizeof(struct sctp_assoc_value);
2757                 }
2758                 break;
2759         }
2760         case SCTP_GET_STAT_LOG:
2761                 error = sctp_fill_stat_log(optval, optsize);
2762                 break;
2763         case SCTP_EVENTS:
2764         {
2765                 struct sctp_event_subscribe *events;
2766
2767                 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
2768                 memset(events, 0, sizeof(struct sctp_event_subscribe));
2769                 SCTP_INP_RLOCK(inp);
2770                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
2771                         events->sctp_data_io_event = 1;
2772
2773                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
2774                         events->sctp_association_event = 1;
2775
2776                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
2777                         events->sctp_address_event = 1;
2778
2779                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
2780                         events->sctp_send_failure_event = 1;
2781
2782                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
2783                         events->sctp_peer_error_event = 1;
2784
2785                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
2786                         events->sctp_shutdown_event = 1;
2787
2788                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
2789                         events->sctp_partial_delivery_event = 1;
2790
2791                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
2792                         events->sctp_adaptation_layer_event = 1;
2793
2794                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
2795                         events->sctp_authentication_event = 1;
2796
2797                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT))
2798                         events->sctp_sender_dry_event = 1;
2799
2800                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
2801                         events->sctp_stream_reset_event = 1;
2802                 SCTP_INP_RUNLOCK(inp);
2803                 *optsize = sizeof(struct sctp_event_subscribe);
2804                 break;
2805         }
2806         case SCTP_ADAPTATION_LAYER:
2807         {
2808                 uint32_t *value;
2809
2810                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2811
2812                 SCTP_INP_RLOCK(inp);
2813                 *value = inp->sctp_ep.adaptation_layer_indicator;
2814                 SCTP_INP_RUNLOCK(inp);
2815                 *optsize = sizeof(uint32_t);
2816                 break;
2817         }
2818         case SCTP_SET_INITIAL_DBG_SEQ:
2819         {
2820                 uint32_t *value;
2821
2822                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2823                 SCTP_INP_RLOCK(inp);
2824                 *value = inp->sctp_ep.initial_sequence_debug;
2825                 SCTP_INP_RUNLOCK(inp);
2826                 *optsize = sizeof(uint32_t);
2827                 break;
2828         }
2829         case SCTP_GET_LOCAL_ADDR_SIZE:
2830         {
2831                 uint32_t *value;
2832
2833                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2834                 SCTP_INP_RLOCK(inp);
2835                 *value = sctp_count_max_addresses(inp);
2836                 SCTP_INP_RUNLOCK(inp);
2837                 *optsize = sizeof(uint32_t);
2838                 break;
2839         }
2840         case SCTP_GET_REMOTE_ADDR_SIZE:
2841         {
2842                 uint32_t *value;
2843                 size_t size;
2844                 struct sctp_nets *net;
2845
2846                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
2847                 /* FIXME MT: change to sctp_assoc_value? */
2848                 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) *value);
2849
2850                 if (stcb) {
2851                         size = 0;
2852                         /* Count the sizes */
2853                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2854                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2855                                         size += sizeof(struct sockaddr_in6);
2856                                 } else {
2857                                         switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2858 #ifdef INET
2859                                         case AF_INET:
2860                                                 size += sizeof(struct sockaddr_in);
2861                                                 break;
2862 #endif
2863 #ifdef INET6
2864                                         case AF_INET6:
2865                                                 size += sizeof(struct sockaddr_in6);
2866                                                 break;
2867 #endif
2868 #if defined(__Userspace__)
2869                                         case AF_CONN:
2870                                                 size += sizeof(struct sockaddr_conn);
2871                                                 break;
2872 #endif
2873                                         default:
2874                                                 break;
2875                                         }
2876                                 }
2877                         }
2878                         SCTP_TCB_UNLOCK(stcb);
2879                         *value = (uint32_t) size;
2880                         *optsize = sizeof(uint32_t);
2881                 } else {
2882                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
2883                         error = ENOTCONN;
2884                 }
2885                 break;
2886         }
2887         case SCTP_GET_PEER_ADDRESSES:
2888                 /*
2889                  * Get the address information, an array is passed in to
2890                  * fill up we pack it.
2891                  */
2892         {
2893                 size_t cpsz, left;
2894                 struct sockaddr_storage *sas;
2895                 struct sctp_nets *net;
2896                 struct sctp_getaddresses *saddr;
2897
2898                 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2899                 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2900
2901                 if (stcb) {
2902                         left = (*optsize) - sizeof(struct sctp_getaddresses);
2903                         *optsize = sizeof(struct sctp_getaddresses);
2904                         sas = (struct sockaddr_storage *)&saddr->addr[0];
2905
2906                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2907                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
2908                                         cpsz = sizeof(struct sockaddr_in6);
2909                                 } else {
2910                                         switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
2911 #ifdef INET
2912                                         case AF_INET:
2913                                                 cpsz = sizeof(struct sockaddr_in);
2914                                                 break;
2915 #endif
2916 #ifdef INET6
2917                                         case AF_INET6:
2918                                                 cpsz = sizeof(struct sockaddr_in6);
2919                                                 break;
2920 #endif
2921 #if defined(__Userspace__)
2922                                         case AF_CONN:
2923                                                 cpsz = sizeof(struct sockaddr_conn);
2924                                                 break;
2925 #endif
2926                                         default:
2927                                                 cpsz = 0;
2928                                                 break;
2929                                         }
2930                                 }
2931                                 if (cpsz == 0) {
2932                                         break;
2933                                 }
2934                                 if (left < cpsz) {
2935                                         /* not enough room. */
2936                                         break;
2937                                 }
2938 #if defined(INET) && defined(INET6)
2939                                 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) &&
2940                                     (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2941                                         /* Must map the address */
2942                                         in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2943                                                             (struct sockaddr_in6 *)sas);
2944                                 } else {
2945 #endif
2946                                         memcpy(sas, &net->ro._l_addr, cpsz);
2947 #if defined(INET) && defined(INET6)
2948                                 }
2949 #endif
2950                                 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2951
2952                                 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2953                                 left -= cpsz;
2954                                 *optsize += cpsz;
2955                         }
2956                         SCTP_TCB_UNLOCK(stcb);
2957                 } else {
2958                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
2959                         error = ENOENT;
2960                 }
2961                 break;
2962         }
2963         case SCTP_GET_LOCAL_ADDRESSES:
2964         {
2965                 size_t limit, actual;
2966                 struct sockaddr_storage *sas;
2967                 struct sctp_getaddresses *saddr;
2968
2969                 SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
2970                 SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
2971
2972                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2973                 limit = *optsize - sizeof(sctp_assoc_t);
2974                 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2975                 if (stcb) {
2976                         SCTP_TCB_UNLOCK(stcb);
2977                 }
2978                 *optsize = sizeof(struct sockaddr_storage) + actual;
2979                 break;
2980         }
2981         case SCTP_PEER_ADDR_PARAMS:
2982         {
2983                 struct sctp_paddrparams *paddrp;
2984                 struct sctp_nets *net;
2985
2986                 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
2987                 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
2988
2989                 net = NULL;
2990                 if (stcb) {
2991                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2992                 } else {
2993                         /* We increment here since sctp_findassociation_ep_addr() wil
2994                          * do a decrement if it finds the stcb as long as the locked
2995                          * tcb (last argument) is NOT a TCB.. aka NULL.
2996                          */
2997                         SCTP_INP_INCR_REF(inp);
2998                         stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
2999                         if (stcb == NULL) {
3000                                 SCTP_INP_DECR_REF(inp);
3001                         }
3002                 }
3003                 if (stcb && (net == NULL)) {
3004                         struct sockaddr *sa;
3005
3006                         sa = (struct sockaddr *)&paddrp->spp_address;
3007 #ifdef INET
3008                         if (sa->sa_family == AF_INET) {
3009                                 struct sockaddr_in *sin;
3010
3011                                 sin = (struct sockaddr_in *)sa;
3012                                 if (sin->sin_addr.s_addr) {
3013                                         error = EINVAL;
3014                                         SCTP_TCB_UNLOCK(stcb);
3015                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3016                                         break;
3017                                 }
3018                         } else
3019 #endif
3020 #ifdef INET6
3021                         if (sa->sa_family == AF_INET6) {
3022                                 struct sockaddr_in6 *sin6;
3023
3024                                 sin6 = (struct sockaddr_in6 *)sa;
3025                                 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3026                                         error = EINVAL;
3027                                         SCTP_TCB_UNLOCK(stcb);
3028                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3029                                         break;
3030                                 }
3031                         } else
3032 #endif
3033 #if defined(__Userspace__)
3034                         if (sa->sa_family == AF_CONN) {
3035                                 struct sockaddr_conn *sconn;
3036
3037                                 sconn = (struct sockaddr_conn *)sa;
3038                                 if (sconn->sconn_addr != NULL) {
3039                                         error = EINVAL;
3040                                         SCTP_TCB_UNLOCK(stcb);
3041                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3042                                         break;
3043                                 }
3044                         } else
3045 #endif
3046                         {
3047                                 error = EAFNOSUPPORT;
3048                                 SCTP_TCB_UNLOCK(stcb);
3049                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3050                                 break;
3051                         }
3052                 }
3053
3054                 if (stcb) {
3055                         /* Applies to the specific association */
3056                         paddrp->spp_flags = 0;
3057                         if (net) {
3058                                 int ovh;
3059                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3060                                         ovh = SCTP_MED_OVERHEAD;
3061                                 } else {
3062                                         ovh = SCTP_MED_V4_OVERHEAD;
3063                                 }
3064
3065                                 paddrp->spp_hbinterval = net->heart_beat_delay;
3066                                 paddrp->spp_pathmaxrxt = net->failure_threshold;
3067                                 paddrp->spp_pathmtu = net->mtu - ovh;
3068                                 /* get flags for HB */
3069                                 if (net->dest_state & SCTP_ADDR_NOHB) {
3070                                         paddrp->spp_flags |= SPP_HB_DISABLE;
3071                                 } else {
3072                                         paddrp->spp_flags |= SPP_HB_ENABLE;
3073                                 }
3074                                 /* get flags for PMTU */
3075                                 if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
3076                                         paddrp->spp_flags |= SPP_PMTUD_ENABLE;
3077                                 } else {
3078                                         paddrp->spp_flags |= SPP_PMTUD_DISABLE;
3079                                 }
3080                                 if (net->dscp & 0x01) {
3081                                         paddrp->spp_dscp = net->dscp & 0xfc;
3082                                         paddrp->spp_flags |= SPP_DSCP;
3083                                 }
3084 #ifdef INET6
3085                                 if ((net->ro._l_addr.sa.sa_family == AF_INET6) &&
3086                                     (net->flowlabel & 0x80000000)) {
3087                                         paddrp->spp_ipv6_flowlabel = net->flowlabel & 0x000fffff;
3088                                         paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
3089                                 }
3090 #endif
3091                         } else {
3092                                 /*
3093                                  * No destination so return default
3094                                  * value
3095                                  */
3096                                 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
3097                                 paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
3098                                 if (stcb->asoc.default_dscp & 0x01) {
3099                                         paddrp->spp_dscp = stcb->asoc.default_dscp & 0xfc;
3100                                         paddrp->spp_flags |= SPP_DSCP;
3101                                 }
3102 #ifdef INET6
3103                                 if (stcb->asoc.default_flowlabel & 0x80000000) {
3104                                         paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel & 0x000fffff;
3105                                         paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
3106                                 }
3107 #endif
3108                                 /* default settings should be these */
3109                                 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
3110                                         paddrp->spp_flags |= SPP_HB_DISABLE;
3111                                 } else {
3112                                         paddrp->spp_flags |= SPP_HB_ENABLE;
3113                                 }
3114                                 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
3115                                         paddrp->spp_flags |= SPP_PMTUD_DISABLE;
3116                                 } else {
3117                                         paddrp->spp_flags |= SPP_PMTUD_ENABLE;
3118                                 }
3119                                 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
3120                         }
3121                         paddrp->spp_assoc_id = sctp_get_associd(stcb);
3122                         SCTP_TCB_UNLOCK(stcb);
3123                 } else {
3124                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3125                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3126                             (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
3127                                 /* Use endpoint defaults */
3128                                 SCTP_INP_RLOCK(inp);
3129                                 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
3130                                 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
3131                                 paddrp->spp_assoc_id = SCTP_FUTURE_ASSOC;
3132                                 /* get inp's default */
3133                                 if (inp->sctp_ep.default_dscp & 0x01) {
3134                                         paddrp->spp_dscp = inp->sctp_ep.default_dscp & 0xfc;
3135                                         paddrp->spp_flags |= SPP_DSCP;
3136                                 }
3137 #ifdef INET6
3138                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
3139                                     (inp->sctp_ep.default_flowlabel & 0x80000000)) {
3140                                         paddrp->spp_ipv6_flowlabel = inp->sctp_ep.default_flowlabel & 0x000fffff;
3141                                         paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
3142                                 }
3143 #endif
3144                                 /* can't return this */
3145                                 paddrp->spp_pathmtu = 0;
3146
3147                                 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
3148                                         paddrp->spp_flags |= SPP_HB_ENABLE;
3149                                 } else {
3150                                         paddrp->spp_flags |= SPP_HB_DISABLE;
3151                                 }
3152                                 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
3153                                         paddrp->spp_flags |= SPP_PMTUD_ENABLE;
3154                                 } else {
3155                                         paddrp->spp_flags |= SPP_PMTUD_DISABLE;
3156                                 }
3157                                 SCTP_INP_RUNLOCK(inp);
3158                         } else {
3159                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3160                                 error = EINVAL;
3161                         }
3162                 }
3163                 if (error == 0) {
3164                         *optsize = sizeof(struct sctp_paddrparams);
3165                 }
3166                 break;
3167         }
3168         case SCTP_GET_PEER_ADDR_INFO:
3169         {
3170                 struct sctp_paddrinfo *paddri;
3171                 struct sctp_nets *net;
3172
3173                 SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
3174                 SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
3175
3176                 net = NULL;
3177                 if (stcb) {
3178                         net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
3179                 } else {
3180                         /* We increment here since sctp_findassociation_ep_addr() wil
3181                          * do a decrement if it finds the stcb as long as the locked
3182                          * tcb (last argument) is NOT a TCB.. aka NULL.
3183                          */
3184                         SCTP_INP_INCR_REF(inp);
3185                         stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
3186                         if (stcb == NULL) {
3187                                 SCTP_INP_DECR_REF(inp);
3188                         }
3189                 }
3190
3191                 if ((stcb) && (net)) {
3192                         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
3193                                 /* It's unconfirmed */
3194                                 paddri->spinfo_state = SCTP_UNCONFIRMED;
3195                         } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
3196                                 /* It's active */
3197                                 paddri->spinfo_state = SCTP_ACTIVE;
3198                         } else {
3199                                 /* It's inactive */
3200                                 paddri->spinfo_state = SCTP_INACTIVE;
3201                         }
3202                         paddri->spinfo_cwnd = net->cwnd;
3203                         paddri->spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
3204                         paddri->spinfo_rto = net->RTO;
3205                         paddri->spinfo_assoc_id = sctp_get_associd(stcb);
3206                         paddri->spinfo_mtu = net->mtu;
3207                         SCTP_TCB_UNLOCK(stcb);
3208                         *optsize = sizeof(struct sctp_paddrinfo);
3209                 } else {
3210                         if (stcb) {
3211                                 SCTP_TCB_UNLOCK(stcb);
3212                         }
3213                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3214                         error = ENOENT;
3215                 }
3216                 break;
3217         }
3218         case SCTP_PCB_STATUS:
3219         {
3220                 struct sctp_pcbinfo *spcb;
3221
3222                 SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
3223                 sctp_fill_pcbinfo(spcb);
3224                 *optsize = sizeof(struct sctp_pcbinfo);
3225                 break;
3226         }
3227         case SCTP_STATUS:
3228         {
3229                 struct sctp_nets *net;
3230                 struct sctp_status *sstat;
3231
3232                 SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
3233                 SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
3234
3235                 if (stcb == NULL) {
3236                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3237                         error = EINVAL;
3238                         break;
3239                 }
3240                 /*
3241                  * I think passing the state is fine since
3242                  * sctp_constants.h will be available to the user
3243                  * land.
3244                  */
3245                 sstat->sstat_state = stcb->asoc.state;
3246                 sstat->sstat_assoc_id = sctp_get_associd(stcb);
3247                 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
3248                 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
3249                 /*
3250                  * We can't include chunks that have been passed to
3251                  * the socket layer. Only things in queue.
3252                  */
3253                 sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
3254                                          stcb->asoc.cnt_on_all_streams);
3255
3256
3257                 sstat->sstat_instrms = stcb->asoc.streamincnt;
3258                 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
3259                 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
3260 #ifdef HAVE_SA_LEN
3261                 memcpy(&sstat->sstat_primary.spinfo_address,
3262                        &stcb->asoc.primary_destination->ro._l_addr,
3263                        ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
3264 #else
3265                 if (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family == AF_INET) {
3266                         memcpy(&sstat->sstat_primary.spinfo_address,
3267                                &stcb->asoc.primary_destination->ro._l_addr,
3268                                sizeof(struct sockaddr_in));
3269                 } else {
3270                         memcpy(&sstat->sstat_primary.spinfo_address,
3271                                &stcb->asoc.primary_destination->ro._l_addr,
3272                                sizeof(struct sockaddr_in6));
3273                 }
3274 #endif
3275                 net = stcb->asoc.primary_destination;
3276                 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
3277                 /*
3278                  * Again the user can get info from sctp_constants.h
3279                  * for what the state of the network is.
3280                  */
3281                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
3282                         /* It's unconfirmed */
3283                         sstat->sstat_primary.spinfo_state = SCTP_UNCONFIRMED;
3284                 } else if (net->dest_state & SCTP_ADDR_REACHABLE) {
3285                         /* It's active */
3286                         sstat->sstat_primary.spinfo_state = SCTP_ACTIVE;
3287                 } else {
3288                         /* It's inactive */
3289                         sstat->sstat_primary.spinfo_state = SCTP_INACTIVE;
3290                 }
3291                 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
3292                 sstat->sstat_primary.spinfo_srtt = net->lastsa >> SCTP_RTT_SHIFT;
3293                 sstat->sstat_primary.spinfo_rto = net->RTO;
3294                 sstat->sstat_primary.spinfo_mtu = net->mtu;
3295                 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
3296                 SCTP_TCB_UNLOCK(stcb);
3297                 *optsize = sizeof(struct sctp_status);
3298                 break;
3299         }
3300         case SCTP_RTOINFO:
3301         {
3302                 struct sctp_rtoinfo *srto;
3303
3304                 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
3305                 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3306
3307                 if (stcb) {
3308                         srto->srto_initial = stcb->asoc.initial_rto;
3309                         srto->srto_max = stcb->asoc.maxrto;
3310                         srto->srto_min = stcb->asoc.minrto;
3311                         SCTP_TCB_UNLOCK(stcb);
3312                 } else {
3313                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3314                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3315                             (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
3316                                 SCTP_INP_RLOCK(inp);
3317                                 srto->srto_initial = inp->sctp_ep.initial_rto;
3318                                 srto->srto_max = inp->sctp_ep.sctp_maxrto;
3319                                 srto->srto_min = inp->sctp_ep.sctp_minrto;
3320                                 SCTP_INP_RUNLOCK(inp);
3321                         } else {
3322                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3323                                 error = EINVAL;
3324                         }
3325                 }
3326                 if (error == 0) {
3327                         *optsize = sizeof(struct sctp_rtoinfo);
3328                 }
3329                 break;
3330         }
3331         case SCTP_TIMEOUTS:
3332         {
3333                 struct sctp_timeouts *stimo;
3334
3335                 SCTP_CHECK_AND_CAST(stimo, optval, struct sctp_timeouts, *optsize);
3336                 SCTP_FIND_STCB(inp, stcb, stimo->stimo_assoc_id);
3337
3338                 if (stcb) {
3339                         stimo->stimo_init= stcb->asoc.timoinit;
3340                         stimo->stimo_data= stcb->asoc.timodata;
3341                         stimo->stimo_sack= stcb->asoc.timosack;
3342                         stimo->stimo_shutdown= stcb->asoc.timoshutdown;
3343                         stimo->stimo_heartbeat= stcb->asoc.timoheartbeat;
3344                         stimo->stimo_cookie= stcb->asoc.timocookie;
3345                         stimo->stimo_shutdownack= stcb->asoc.timoshutdownack;
3346                         SCTP_TCB_UNLOCK(stcb);
3347                         *optsize = sizeof(struct sctp_timeouts);
3348                 } else {
3349                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3350                         error = EINVAL;
3351                 }
3352                 break;
3353         }
3354         case SCTP_ASSOCINFO:
3355         {
3356                 struct sctp_assocparams *sasoc;
3357
3358                 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
3359                 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3360
3361                 if (stcb) {
3362                         sasoc->sasoc_cookie_life = TICKS_TO_MSEC(stcb->asoc.cookie_life);
3363                         sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
3364                         sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3365                         sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
3366                         sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
3367                         SCTP_TCB_UNLOCK(stcb);
3368                 } else {
3369                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3370                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3371                             (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
3372                                 SCTP_INP_RLOCK(inp);
3373                                 sasoc->sasoc_cookie_life = TICKS_TO_MSEC(inp->sctp_ep.def_cookie_life);
3374                                 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
3375                                 sasoc->sasoc_number_peer_destinations = 0;
3376                                 sasoc->sasoc_peer_rwnd = 0;
3377                                 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
3378                                 SCTP_INP_RUNLOCK(inp);
3379                         } else {
3380                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3381                                 error = EINVAL;
3382                         }
3383                 }
3384                 if (error == 0) {
3385                         *optsize = sizeof(struct sctp_assocparams);
3386                 }
3387                 break;
3388         }
3389         case SCTP_DEFAULT_SEND_PARAM:
3390         {
3391                 struct sctp_sndrcvinfo *s_info;
3392
3393                 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
3394                 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
3395
3396                 if (stcb) {
3397                         memcpy(s_info, &stcb->asoc.def_send, sizeof(stcb->asoc.def_send));
3398                         SCTP_TCB_UNLOCK(stcb);
3399                 } else {
3400                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3401                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3402                             (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC)) {
3403                                 SCTP_INP_RLOCK(inp);
3404                                 memcpy(s_info, &inp->def_send, sizeof(inp->def_send));
3405                                 SCTP_INP_RUNLOCK(inp);
3406                         } else {
3407                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3408                                 error = EINVAL;
3409                         }
3410                 }
3411                 if (error == 0) {
3412                         *optsize = sizeof(struct sctp_sndrcvinfo);
3413                 }
3414                 break;
3415         }
3416         case SCTP_INITMSG:
3417         {
3418                 struct sctp_initmsg *sinit;
3419
3420                 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
3421                 SCTP_INP_RLOCK(inp);
3422                 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
3423                 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
3424                 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
3425                 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
3426                 SCTP_INP_RUNLOCK(inp);
3427                 *optsize = sizeof(struct sctp_initmsg);
3428                 break;
3429         }
3430         case SCTP_PRIMARY_ADDR:
3431                 /* we allow a "get" operation on this */
3432         {
3433                 struct sctp_setprim *ssp;
3434
3435                 SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
3436                 SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
3437
3438                 if (stcb) {
3439                         /* simply copy out the sockaddr_storage... */
3440                         size_t len;
3441
3442                         len = *optsize;
3443 #ifdef HAVE_SA_LEN
3444                         if (len > stcb->asoc.primary_destination->ro._l_addr.sa.sa_len)
3445                                 len = stcb->asoc.primary_destination->ro._l_addr.sa.sa_len;
3446 #else
3447                         if (stcb->asoc.primary_destination->ro._l_addr.sa.sa_family == AF_INET &&
3448                             len > sizeof(struct sockaddr_in))
3449                                 len = sizeof(struct sockaddr_in);
3450                         else if (
3451                             stcb->asoc.primary_destination->ro._l_addr.sa.sa_family == AF_INET6 &&
3452                             len > sizeof(struct sockaddr_in6))
3453                                 len = sizeof(struct sockaddr_in6);
3454 #endif
3455
3456                         memcpy(&ssp->ssp_addr,
3457                                &stcb->asoc.primary_destination->ro._l_addr,
3458                                len);
3459                         SCTP_TCB_UNLOCK(stcb);
3460                         *optsize = sizeof(struct sctp_setprim);
3461                 } else {
3462                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3463                         error = EINVAL;
3464                 }
3465                 break;
3466         }
3467         case SCTP_HMAC_IDENT:
3468         {
3469                 struct sctp_hmacalgo *shmac;
3470                 sctp_hmaclist_t *hmaclist;
3471                 uint32_t size;
3472                 int i;
3473
3474                 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
3475
3476                 SCTP_INP_RLOCK(inp);
3477                 hmaclist = inp->sctp_ep.local_hmacs;
3478                 if (hmaclist == NULL) {
3479                         /* no HMACs to return */
3480                         *optsize = sizeof(*shmac);
3481                         SCTP_INP_RUNLOCK(inp);
3482                         break;
3483                 }
3484                 /* is there room for all of the hmac ids? */
3485                 size = sizeof(*shmac) + (hmaclist->num_algo *
3486                                          sizeof(shmac->shmac_idents[0]));
3487                 if ((size_t)(*optsize) < size) {
3488                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3489                         error = EINVAL;
3490                         SCTP_INP_RUNLOCK(inp);
3491                         break;
3492                 }
3493                 /* copy in the list */
3494                 shmac->shmac_number_of_idents = hmaclist->num_algo;
3495                 for (i = 0; i < hmaclist->num_algo; i++) {
3496                         shmac->shmac_idents[i] = hmaclist->hmac[i];
3497                 }
3498                 SCTP_INP_RUNLOCK(inp);
3499                 *optsize = size;
3500                 break;
3501         }
3502         case SCTP_AUTH_ACTIVE_KEY:
3503         {
3504                 struct sctp_authkeyid *scact;
3505
3506                 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
3507                 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
3508
3509                 if (stcb) {
3510                         /* get the active key on the assoc */
3511                         scact->scact_keynumber = stcb->asoc.authinfo.active_keyid;
3512                         SCTP_TCB_UNLOCK(stcb);
3513                 } else {
3514                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3515                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3516                             (scact->scact_assoc_id == SCTP_FUTURE_ASSOC)) {
3517                                 /* get the endpoint active key */
3518                                 SCTP_INP_RLOCK(inp);
3519                                 scact->scact_keynumber = inp->sctp_ep.default_keyid;
3520                                 SCTP_INP_RUNLOCK(inp);
3521                         } else {
3522                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3523                                 error = EINVAL;
3524                         }
3525                 }
3526                 if (error == 0) {
3527                         *optsize = sizeof(struct sctp_authkeyid);
3528                 }
3529                 break;
3530         }
3531         case SCTP_LOCAL_AUTH_CHUNKS:
3532         {
3533                 struct sctp_authchunks *sac;
3534                 sctp_auth_chklist_t *chklist = NULL;
3535                 size_t size = 0;
3536
3537                 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
3538                 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
3539
3540                 if (stcb) {
3541                         /* get off the assoc */
3542                         chklist = stcb->asoc.local_auth_chunks;
3543                         /* is there enough space? */
3544                         size = sctp_auth_get_chklist_size(chklist);
3545                         if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
3546                                 error = EINVAL;
3547                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3548                         } else {
3549                                 /* copy in the chunks */
3550                                 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3551                                 sac->gauth_number_of_chunks = (uint32_t)size;
3552                                 *optsize = sizeof(struct sctp_authchunks) + size;
3553                         }
3554                         SCTP_TCB_UNLOCK(stcb);
3555                 } else {
3556                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3557                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3558                             (sac->gauth_assoc_id == SCTP_FUTURE_ASSOC)) {
3559                                 /* get off the endpoint */
3560                                 SCTP_INP_RLOCK(inp);
3561                                 chklist = inp->sctp_ep.local_auth_chunks;
3562                                 /* is there enough space? */
3563                                 size = sctp_auth_get_chklist_size(chklist);
3564                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
3565                                         error = EINVAL;
3566                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3567                                 } else {
3568                                         /* copy in the chunks */
3569                                         (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3570                                         sac->gauth_number_of_chunks = (uint32_t)size;
3571                                         *optsize = sizeof(struct sctp_authchunks) + size;
3572                                 }
3573                                 SCTP_INP_RUNLOCK(inp);
3574                         } else {
3575                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3576                                 error = EINVAL;
3577                         }
3578                 }
3579                 break;
3580         }
3581         case SCTP_PEER_AUTH_CHUNKS:
3582         {
3583                 struct sctp_authchunks *sac;
3584                 sctp_auth_chklist_t *chklist = NULL;
3585                 size_t size = 0;
3586
3587                 SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
3588                 SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
3589
3590                 if (stcb) {
3591                         /* get off the assoc */
3592                         chklist = stcb->asoc.peer_auth_chunks;
3593                         /* is there enough space? */
3594                         size = sctp_auth_get_chklist_size(chklist);
3595                         if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
3596                                 error = EINVAL;
3597                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3598                         } else {
3599                                 /* copy in the chunks */
3600                                 (void)sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
3601                                 sac->gauth_number_of_chunks = (uint32_t)size;
3602                                 *optsize = sizeof(struct sctp_authchunks) + size;
3603                         }
3604                         SCTP_TCB_UNLOCK(stcb);
3605                 } else {
3606                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
3607                         error = ENOENT;
3608                 }
3609                 break;
3610         }
3611 #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
3612         case SCTP_PEELOFF:
3613         {
3614                 struct sctp_peeloff_opt *peeloff;
3615
3616                 SCTP_CHECK_AND_CAST(peeloff, optval, struct sctp_peeloff_opt, *optsize);
3617                 /* do the peeloff */
3618                 error = sctp_peeloff_option(p, peeloff);
3619                 if (error == 0) {
3620                         *optsize = sizeof(struct sctp_peeloff_opt);
3621                 }
3622         }
3623         break;
3624 #endif /* HAVE_SCTP_PEELOFF_SOCKOPT */
3625         case SCTP_EVENT:
3626         {
3627                 struct sctp_event *event;
3628                 uint32_t event_type;
3629
3630                 SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, *optsize);
3631                 SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
3632
3633                 switch (event->se_type) {
3634                 case SCTP_ASSOC_CHANGE:
3635                         event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
3636                         break;
3637                 case SCTP_PEER_ADDR_CHANGE:
3638                         event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
3639                         break;
3640                 case SCTP_REMOTE_ERROR:
3641                         event_type = SCTP_PCB_FLAGS_RECVPEERERR;
3642                         break;
3643                 case SCTP_SEND_FAILED:
3644                         event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3645                         break;
3646                 case SCTP_SHUTDOWN_EVENT:
3647                         event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3648                         break;
3649                 case SCTP_ADAPTATION_INDICATION:
3650                         event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
3651                         break;
3652                 case SCTP_PARTIAL_DELIVERY_EVENT:
3653                         event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
3654                         break;
3655                 case SCTP_AUTHENTICATION_EVENT:
3656                         event_type = SCTP_PCB_FLAGS_AUTHEVNT;
3657                         break;
3658                 case SCTP_STREAM_RESET_EVENT:
3659                         event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3660                         break;
3661                 case SCTP_SENDER_DRY_EVENT:
3662                         event_type = SCTP_PCB_FLAGS_DRYEVNT;
3663                         break;
3664                 case SCTP_NOTIFICATIONS_STOPPED_EVENT:
3665                         event_type = 0;
3666                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
3667                         error = ENOTSUP;
3668                         break;
3669                 case SCTP_ASSOC_RESET_EVENT:
3670                         event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
3671                         break;
3672                 case SCTP_STREAM_CHANGE_EVENT:
3673                         event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
3674                         break;
3675                 case SCTP_SEND_FAILED_EVENT:
3676                         event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
3677                         break;
3678                 default:
3679                         event_type = 0;
3680                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3681                         error = EINVAL;
3682                         break;
3683                 }
3684                 if (event_type > 0) {
3685                         if (stcb) {
3686                                 event->se_on = sctp_stcb_is_feature_on(inp, stcb, event_type);
3687                                 SCTP_TCB_UNLOCK(stcb);
3688                         } else {
3689                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3690                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3691                                     (event->se_assoc_id == SCTP_FUTURE_ASSOC)) {
3692                                         SCTP_INP_RLOCK(inp);
3693                                         event->se_on = sctp_is_feature_on(inp, event_type);
3694                                         SCTP_INP_RUNLOCK(inp);
3695                                 } else {
3696                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3697                                         error = EINVAL;
3698                                 }
3699                         }
3700                 }
3701                 if (error == 0) {
3702                         *optsize = sizeof(struct sctp_event);
3703                 }
3704                 break;
3705         }
3706         case SCTP_RECVRCVINFO:
3707         {
3708                 int onoff;
3709
3710                 if (*optsize < sizeof(int)) {
3711                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3712                         error = EINVAL;
3713                 } else {
3714                         SCTP_INP_RLOCK(inp);
3715                         onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
3716                         SCTP_INP_RUNLOCK(inp);
3717                 }
3718                 if (error == 0) {
3719                         /* return the option value */
3720                         *(int *)optval = onoff;
3721                         *optsize = sizeof(int);
3722                 }
3723                 break;
3724         }
3725         case SCTP_RECVNXTINFO:
3726         {
3727                 int onoff;
3728
3729                 if (*optsize < sizeof(int)) {
3730                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3731                         error = EINVAL;
3732                 } else {
3733                         SCTP_INP_RLOCK(inp);
3734                         onoff = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
3735                         SCTP_INP_RUNLOCK(inp);
3736                 }
3737                 if (error == 0) {
3738                         /* return the option value */
3739                         *(int *)optval = onoff;
3740                         *optsize = sizeof(int);
3741                 }
3742                 break;
3743         }
3744         case SCTP_DEFAULT_SNDINFO:
3745         {
3746                 struct sctp_sndinfo *info;
3747
3748                 SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, *optsize);
3749                 SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
3750
3751                 if (stcb) {
3752                         info->snd_sid = stcb->asoc.def_send.sinfo_stream;
3753                         info->snd_flags = stcb->asoc.def_send.sinfo_flags;
3754                         info->snd_flags &= 0xfff0;
3755                         info->snd_ppid = stcb->asoc.def_send.sinfo_ppid;
3756                         info->snd_context = stcb->asoc.def_send.sinfo_context;
3757                         SCTP_TCB_UNLOCK(stcb);
3758                 } else {
3759                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3760                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3761                             (info->snd_assoc_id == SCTP_FUTURE_ASSOC)) {
3762                                 SCTP_INP_RLOCK(inp);
3763                                 info->snd_sid = inp->def_send.sinfo_stream;
3764                                 info->snd_flags = inp->def_send.sinfo_flags;
3765                                 info->snd_flags &= 0xfff0;
3766                                 info->snd_ppid = inp->def_send.sinfo_ppid;
3767                                 info->snd_context = inp->def_send.sinfo_context;
3768                                 SCTP_INP_RUNLOCK(inp);
3769                         } else {
3770                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3771                                 error = EINVAL;
3772                         }
3773                 }
3774                 if (error == 0) {
3775                         *optsize = sizeof(struct sctp_sndinfo);
3776                 }
3777                 break;
3778         }
3779         case SCTP_DEFAULT_PRINFO:
3780         {
3781                 struct sctp_default_prinfo *info;
3782
3783                 SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, *optsize);
3784                 SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
3785
3786                 if (stcb) {
3787                         info->pr_policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
3788                         info->pr_value = stcb->asoc.def_send.sinfo_timetolive;
3789                         SCTP_TCB_UNLOCK(stcb);
3790                 } else {
3791                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3792                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3793                             (info->pr_assoc_id == SCTP_FUTURE_ASSOC)) {
3794                                 SCTP_INP_RLOCK(inp);
3795                                 info->pr_policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
3796                                 info->pr_value = inp->def_send.sinfo_timetolive;
3797                                 SCTP_INP_RUNLOCK(inp);
3798                         } else {
3799                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3800                                 error = EINVAL;
3801                         }
3802                 }
3803                 if (error == 0) {
3804                         *optsize = sizeof(struct sctp_default_prinfo);
3805                 }
3806                 break;
3807         }
3808         case SCTP_PEER_ADDR_THLDS:
3809         {
3810                 struct sctp_paddrthlds *thlds;
3811                 struct sctp_nets *net;
3812
3813                 SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, *optsize);
3814                 SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
3815
3816                 net = NULL;
3817                 if (stcb) {
3818                         net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_address);
3819                 } else {
3820                         /* We increment here since sctp_findassociation_ep_addr() wil
3821                          * do a decrement if it finds the stcb as long as the locked
3822                          * tcb (last argument) is NOT a TCB.. aka NULL.
3823                          */
3824                         SCTP_INP_INCR_REF(inp);
3825                         stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&thlds->spt_address, &net, NULL, NULL);
3826                         if (stcb == NULL) {
3827                                 SCTP_INP_DECR_REF(inp);
3828                         }
3829                 }
3830                 if (stcb && (net == NULL)) {
3831                         struct sockaddr *sa;
3832
3833                         sa = (struct sockaddr *)&thlds->spt_address;
3834 #ifdef INET
3835                         if (sa->sa_family == AF_INET) {
3836                                 struct sockaddr_in *sin;
3837
3838                                 sin = (struct sockaddr_in *)sa;
3839                                 if (sin->sin_addr.s_addr) {
3840                                         error = EINVAL;
3841                                         SCTP_TCB_UNLOCK(stcb);
3842                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3843                                         break;
3844                                 }
3845                         } else
3846 #endif
3847 #ifdef INET6
3848                         if (sa->sa_family == AF_INET6) {
3849                                 struct sockaddr_in6 *sin6;
3850
3851                                 sin6 = (struct sockaddr_in6 *)sa;
3852                                 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3853                                         error = EINVAL;
3854                                         SCTP_TCB_UNLOCK(stcb);
3855                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3856                                         break;
3857                                 }
3858                         } else
3859 #endif
3860 #if defined(__Userspace__)
3861                         if (sa->sa_family == AF_CONN) {
3862                                 struct sockaddr_conn *sconn;
3863
3864                                 sconn = (struct sockaddr_conn *)sa;
3865                                 if (sconn->sconn_addr != NULL) {
3866                                         error = EINVAL;
3867                                         SCTP_TCB_UNLOCK(stcb);
3868                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3869                                         break;
3870                                 }
3871                         } else
3872 #endif
3873                         {
3874                                 error = EAFNOSUPPORT;
3875                                 SCTP_TCB_UNLOCK(stcb);
3876                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3877                                 break;
3878                         }
3879                 }
3880
3881                 if (stcb) {
3882                         if (net) {
3883                                 thlds->spt_pathmaxrxt = net->failure_threshold;
3884                                 thlds->spt_pathpfthld = net->pf_threshold;
3885                         } else {
3886                                 thlds->spt_pathmaxrxt = stcb->asoc.def_net_failure;
3887                                 thlds->spt_pathpfthld = stcb->asoc.def_net_pf_threshold;
3888                         }
3889                         thlds->spt_assoc_id = sctp_get_associd(stcb);
3890                         SCTP_TCB_UNLOCK(stcb);
3891                 } else {
3892                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3893                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3894                             (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
3895                                 /* Use endpoint defaults */
3896                                 SCTP_INP_RLOCK(inp);
3897                                 thlds->spt_pathmaxrxt = inp->sctp_ep.def_net_failure;
3898                                 thlds->spt_pathpfthld = inp->sctp_ep.def_net_pf_threshold;
3899                                 SCTP_INP_RUNLOCK(inp);
3900                         } else {
3901                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3902                                 error = EINVAL;
3903                         }
3904                 }
3905                 if (error == 0) {
3906                         *optsize = sizeof(struct sctp_paddrthlds);
3907                 }
3908                 break;
3909         }
3910         case SCTP_REMOTE_UDP_ENCAPS_PORT:
3911         {
3912                 struct sctp_udpencaps *encaps;
3913                 struct sctp_nets *net;
3914
3915                 SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, *optsize);
3916                 SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
3917
3918                 if (stcb) {
3919                         net = sctp_findnet(stcb, (struct sockaddr *)&encaps->sue_address);
3920                 } else {
3921                         /* We increment here since sctp_findassociation_ep_addr() wil
3922                          * do a decrement if it finds the stcb as long as the locked
3923                          * tcb (last argument) is NOT a TCB.. aka NULL.
3924                          */
3925                         net = NULL;
3926                         SCTP_INP_INCR_REF(inp);
3927                         stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&encaps->sue_address, &net, NULL, NULL);
3928                         if (stcb == NULL) {
3929                                 SCTP_INP_DECR_REF(inp);
3930                         }
3931                 }
3932                 if (stcb && (net == NULL)) {
3933                         struct sockaddr *sa;
3934
3935                         sa = (struct sockaddr *)&encaps->sue_address;
3936 #ifdef INET
3937                         if (sa->sa_family == AF_INET) {
3938                                 struct sockaddr_in *sin;
3939
3940                                 sin = (struct sockaddr_in *)sa;
3941                                 if (sin->sin_addr.s_addr) {
3942                                         error = EINVAL;
3943                                         SCTP_TCB_UNLOCK(stcb);
3944                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3945                                         break;
3946                                 }
3947                         } else
3948 #endif
3949 #ifdef INET6
3950                         if (sa->sa_family == AF_INET6) {
3951                                 struct sockaddr_in6 *sin6;
3952
3953                                 sin6 = (struct sockaddr_in6 *)sa;
3954                                 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3955                                         error = EINVAL;
3956                                         SCTP_TCB_UNLOCK(stcb);
3957                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3958                                         break;
3959                                 }
3960                         } else
3961 #endif
3962 #if defined(__Userspace__)
3963                         if (sa->sa_family == AF_CONN) {
3964                                 struct sockaddr_conn *sconn;
3965
3966                                 sconn = (struct sockaddr_conn *)sa;
3967                                 if (sconn->sconn_addr != NULL) {
3968                                         error = EINVAL;
3969                                         SCTP_TCB_UNLOCK(stcb);
3970                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3971                                         break;
3972                                 }
3973                         } else
3974 #endif
3975                         {
3976                                 error = EAFNOSUPPORT;
3977                                 SCTP_TCB_UNLOCK(stcb);
3978                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
3979                                 break;
3980                         }
3981                 }
3982
3983                 if (stcb) {
3984                         if (net) {
3985                                 encaps->sue_port = net->port;
3986                         } else {
3987                                 encaps->sue_port = stcb->asoc.port;
3988                         }
3989                         SCTP_TCB_UNLOCK(stcb);
3990                 } else {
3991                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3992                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
3993                             (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
3994                                 SCTP_INP_RLOCK(inp);
3995                                 encaps->sue_port = inp->sctp_ep.port;
3996                                 SCTP_INP_RUNLOCK(inp);
3997                         } else {
3998                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
3999                                 error = EINVAL;
4000                         }
4001                 }
4002                 if (error == 0) {
4003                         *optsize = sizeof(struct sctp_udpencaps);
4004                 }
4005                 break;
4006         }
4007         case SCTP_ECN_SUPPORTED:
4008         {
4009                 struct sctp_assoc_value *av;
4010
4011                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4012                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4013
4014                 if (stcb) {
4015                         av->assoc_value = stcb->asoc.ecn_supported;
4016                         SCTP_TCB_UNLOCK(stcb);
4017                 } else {
4018                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4019                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4020                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4021                                 SCTP_INP_RLOCK(inp);
4022                                 av->assoc_value = inp->ecn_supported;
4023                                 SCTP_INP_RUNLOCK(inp);
4024                         } else {
4025                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4026                                 error = EINVAL;
4027                         }
4028                 }
4029                 if (error == 0) {
4030                         *optsize = sizeof(struct sctp_assoc_value);
4031                 }
4032                 break;
4033         }
4034         case SCTP_PR_SUPPORTED:
4035         {
4036                 struct sctp_assoc_value *av;
4037
4038                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4039                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4040
4041                 if (stcb) {
4042                         av->assoc_value = stcb->asoc.prsctp_supported;
4043                         SCTP_TCB_UNLOCK(stcb);
4044                 } else {
4045                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4046                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4047                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4048                                 SCTP_INP_RLOCK(inp);
4049                                 av->assoc_value = inp->prsctp_supported;
4050                                 SCTP_INP_RUNLOCK(inp);
4051                         } else {
4052                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4053                                 error = EINVAL;
4054                         }
4055                 }
4056                 if (error == 0) {
4057                         *optsize = sizeof(struct sctp_assoc_value);
4058                 }
4059                 break;
4060         }
4061         case SCTP_AUTH_SUPPORTED:
4062         {
4063                 struct sctp_assoc_value *av;
4064
4065                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4066                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4067
4068                 if (stcb) {
4069                         av->assoc_value = stcb->asoc.auth_supported;
4070                         SCTP_TCB_UNLOCK(stcb);
4071                 } else {
4072                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4073                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4074                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4075                                 SCTP_INP_RLOCK(inp);
4076                                 av->assoc_value = inp->auth_supported;
4077                                 SCTP_INP_RUNLOCK(inp);
4078                         } else {
4079                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4080                                 error = EINVAL;
4081                         }
4082                 }
4083                 if (error == 0) {
4084                         *optsize = sizeof(struct sctp_assoc_value);
4085                 }
4086                 break;
4087         }
4088         case SCTP_ASCONF_SUPPORTED:
4089         {
4090                 struct sctp_assoc_value *av;
4091
4092                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4093                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4094
4095                 if (stcb) {
4096                         av->assoc_value = stcb->asoc.asconf_supported;
4097                         SCTP_TCB_UNLOCK(stcb);
4098                 } else {
4099                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4100                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4101                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4102                                 SCTP_INP_RLOCK(inp);
4103                                 av->assoc_value = inp->asconf_supported;
4104                                 SCTP_INP_RUNLOCK(inp);
4105                         } else {
4106                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4107                                 error = EINVAL;
4108                         }
4109                 }
4110                 if (error == 0) {
4111                         *optsize = sizeof(struct sctp_assoc_value);
4112                 }
4113                 break;
4114         }
4115         case SCTP_RECONFIG_SUPPORTED:
4116         {
4117                 struct sctp_assoc_value *av;
4118
4119                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4120                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4121
4122                 if (stcb) {
4123                         av->assoc_value = stcb->asoc.reconfig_supported;
4124                         SCTP_TCB_UNLOCK(stcb);
4125                 } else {
4126                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4127                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4128                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4129                                 SCTP_INP_RLOCK(inp);
4130                                 av->assoc_value = inp->reconfig_supported;
4131                                 SCTP_INP_RUNLOCK(inp);
4132                         } else {
4133                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4134                                 error = EINVAL;
4135                         }
4136                 }
4137                 if (error == 0) {
4138                         *optsize = sizeof(struct sctp_assoc_value);
4139                 }
4140                 break;
4141         }
4142         case SCTP_NRSACK_SUPPORTED:
4143         {
4144                 struct sctp_assoc_value *av;
4145
4146                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4147                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4148
4149                 if (stcb) {
4150                         av->assoc_value = stcb->asoc.nrsack_supported;
4151                         SCTP_TCB_UNLOCK(stcb);
4152                 } else {
4153                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4154                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4155                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4156                                 SCTP_INP_RLOCK(inp);
4157                                 av->assoc_value = inp->nrsack_supported;
4158                                 SCTP_INP_RUNLOCK(inp);
4159                         } else {
4160                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4161                                 error = EINVAL;
4162                         }
4163                 }
4164                 if (error == 0) {
4165                         *optsize = sizeof(struct sctp_assoc_value);
4166                 }
4167                 break;
4168         }
4169         case SCTP_PKTDROP_SUPPORTED:
4170         {
4171                 struct sctp_assoc_value *av;
4172
4173                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4174                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4175
4176                 if (stcb) {
4177                         av->assoc_value = stcb->asoc.pktdrop_supported;
4178                         SCTP_TCB_UNLOCK(stcb);
4179                 } else {
4180                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4181                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4182                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4183                                 SCTP_INP_RLOCK(inp);
4184                                 av->assoc_value = inp->pktdrop_supported;
4185                                 SCTP_INP_RUNLOCK(inp);
4186                         } else {
4187                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4188                                 error = EINVAL;
4189                         }
4190                 }
4191                 if (error == 0) {
4192                         *optsize = sizeof(struct sctp_assoc_value);
4193                 }
4194                 break;
4195         }
4196         case SCTP_ENABLE_STREAM_RESET:
4197         {
4198                 struct sctp_assoc_value *av;
4199
4200                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
4201                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4202
4203                 if (stcb) {
4204                         av->assoc_value = (uint32_t)stcb->asoc.local_strreset_support;
4205                         SCTP_TCB_UNLOCK(stcb);
4206                 } else {
4207                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4208                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4209                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
4210                                 SCTP_INP_RLOCK(inp);
4211                                 av->assoc_value = (uint32_t)inp->local_strreset_support;
4212                                 SCTP_INP_RUNLOCK(inp);
4213                         } else {
4214                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4215                                 error = EINVAL;
4216                         }
4217                 }
4218                 if (error == 0) {
4219                         *optsize = sizeof(struct sctp_assoc_value);
4220                 }
4221                 break;
4222         }
4223         case SCTP_PR_STREAM_STATUS:
4224         {
4225                 struct sctp_prstatus *sprstat;
4226                 uint16_t sid;
4227                 uint16_t policy;
4228
4229                 SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
4230                 SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
4231
4232                 sid = sprstat->sprstat_sid;
4233                 policy = sprstat->sprstat_policy;
4234 #if defined(SCTP_DETAILED_STR_STATS)
4235                 if ((stcb != NULL) &&
4236                     (policy != SCTP_PR_SCTP_NONE) &&
4237                     (sid < stcb->asoc.streamoutcnt) &&
4238                     ((policy == SCTP_PR_SCTP_ALL) ||
4239                      (PR_SCTP_VALID_POLICY(policy)))) {
4240 #else
4241                 if ((stcb != NULL) &&
4242                     (policy != SCTP_PR_SCTP_NONE) &&
4243                     (sid < stcb->asoc.streamoutcnt) &&
4244                     (policy == SCTP_PR_SCTP_ALL)) {
4245 #endif
4246                         if (policy == SCTP_PR_SCTP_ALL) {
4247                                 sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[0];
4248                                 sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[0];
4249                         } else {
4250                                 sprstat->sprstat_abandoned_unsent = stcb->asoc.strmout[sid].abandoned_unsent[policy];
4251                                 sprstat->sprstat_abandoned_sent = stcb->asoc.strmout[sid].abandoned_sent[policy];
4252                         }
4253                         SCTP_TCB_UNLOCK(stcb);
4254                         *optsize = sizeof(struct sctp_prstatus);
4255                 } else {
4256                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4257                         error = EINVAL;
4258                 }
4259                 break;
4260         }
4261         case SCTP_PR_ASSOC_STATUS:
4262         {
4263                 struct sctp_prstatus *sprstat;
4264                 uint16_t policy;
4265
4266                 SCTP_CHECK_AND_CAST(sprstat, optval, struct sctp_prstatus, *optsize);
4267                 SCTP_FIND_STCB(inp, stcb, sprstat->sprstat_assoc_id);
4268
4269                 policy = sprstat->sprstat_policy;
4270                 if ((stcb != NULL) &&
4271                     (policy != SCTP_PR_SCTP_NONE) &&
4272                     ((policy == SCTP_PR_SCTP_ALL) ||
4273                      (PR_SCTP_VALID_POLICY(policy)))) {
4274                         if (policy == SCTP_PR_SCTP_ALL) {
4275                                 sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[0];
4276                                 sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[0];
4277                         } else {
4278                                 sprstat->sprstat_abandoned_unsent = stcb->asoc.abandoned_unsent[policy];
4279                                 sprstat->sprstat_abandoned_sent = stcb->asoc.abandoned_sent[policy];
4280                         }
4281                         SCTP_TCB_UNLOCK(stcb);
4282                         *optsize = sizeof(struct sctp_prstatus);
4283                 } else {
4284                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4285                         error = EINVAL;
4286                 }
4287                 break;
4288         }
4289         default:
4290                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4291                 error = ENOPROTOOPT;
4292                 break;
4293         } /* end switch (sopt->sopt_name) */
4294         if (error) {
4295                 *optsize = 0;
4296         }
4297         return (error);
4298 }
4299
4300 #if defined(__Panda__) || defined(__Userspace__)
4301 int
4302 #else
4303 static int
4304 #endif
4305 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
4306             void *p)
4307 {
4308         int error, set_opt;
4309         uint32_t *mopt;
4310         struct sctp_tcb *stcb = NULL;
4311         struct sctp_inpcb *inp = NULL;
4312         uint32_t vrf_id;
4313
4314         if (optval == NULL) {
4315                 SCTP_PRINTF("optval is NULL\n");
4316                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4317                 return (EINVAL);
4318         }
4319         inp = (struct sctp_inpcb *)so->so_pcb;
4320         if (inp == NULL) {
4321                 SCTP_PRINTF("inp is NULL?\n");
4322                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4323                 return (EINVAL);
4324         }
4325         vrf_id = inp->def_vrf_id;
4326
4327         error = 0;
4328         switch (optname) {
4329         case SCTP_NODELAY:
4330         case SCTP_AUTOCLOSE:
4331         case SCTP_AUTO_ASCONF:
4332         case SCTP_EXPLICIT_EOR:
4333         case SCTP_DISABLE_FRAGMENTS:
4334         case SCTP_USE_EXT_RCVINFO:
4335         case SCTP_I_WANT_MAPPED_V4_ADDR:
4336                 /* copy in the option value */
4337                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
4338                 set_opt = 0;
4339                 if (error)
4340                         break;
4341                 switch (optname) {
4342                 case SCTP_DISABLE_FRAGMENTS:
4343                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
4344                         break;
4345                 case SCTP_AUTO_ASCONF:
4346                         /*
4347                          * NOTE: we don't really support this flag
4348                          */
4349                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4350                                 /* only valid for bound all sockets */
4351                                 if ((SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) &&
4352                                     (*mopt != 0)) {
4353                                         /* forbidden by admin */
4354                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EPERM);
4355                                         return (EPERM);
4356                                 }
4357                                 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
4358                         } else {
4359                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4360                                 return (EINVAL);
4361                         }
4362                         break;
4363                 case SCTP_EXPLICIT_EOR:
4364                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
4365                         break;
4366                 case SCTP_USE_EXT_RCVINFO:
4367                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
4368                         break;
4369                 case SCTP_I_WANT_MAPPED_V4_ADDR:
4370                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4371                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
4372                         } else {
4373                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4374                                 return (EINVAL);
4375                         }
4376                         break;
4377                 case SCTP_NODELAY:
4378                         set_opt = SCTP_PCB_FLAGS_NODELAY;
4379                         break;
4380                 case SCTP_AUTOCLOSE:
4381                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4382                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4383                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4384                                 return (EINVAL);
4385                         }
4386                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
4387                         /*
4388                          * The value is in ticks. Note this does not effect
4389                          * old associations, only new ones.
4390                          */
4391                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
4392                         break;
4393                 }
4394                 SCTP_INP_WLOCK(inp);
4395                 if (*mopt != 0) {
4396                         sctp_feature_on(inp, set_opt);
4397                 } else {
4398                         sctp_feature_off(inp, set_opt);
4399                 }
4400                 SCTP_INP_WUNLOCK(inp);
4401                 break;
4402         case SCTP_REUSE_PORT:
4403         {
4404                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
4405                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND)  == 0) {
4406                         /* Can't set it after we are bound */
4407                         error = EINVAL;
4408                         break;
4409                 }
4410                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
4411                         /* Can't do this for a 1-m socket */
4412                         error = EINVAL;
4413                         break;
4414                 }
4415                 if (optval)
4416                         sctp_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE);
4417                 else
4418                         sctp_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE);
4419                 break;
4420         }
4421         case SCTP_PARTIAL_DELIVERY_POINT:
4422         {
4423                 uint32_t *value;
4424
4425                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
4426                 if (*value > SCTP_SB_LIMIT_RCV(so)) {
4427                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4428                         error = EINVAL;
4429                         break;
4430                 }
4431                 inp->partial_delivery_point = *value;
4432                 break;
4433         }
4434         case SCTP_FRAGMENT_INTERLEAVE:
4435                 /* not yet until we re-write sctp_recvmsg() */
4436         {
4437                 uint32_t *level;
4438
4439                 SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
4440                 if (*level == SCTP_FRAG_LEVEL_2) {
4441                         sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
4442                         sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
4443                 } else if (*level == SCTP_FRAG_LEVEL_1) {
4444                         sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
4445                         sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
4446                 } else if (*level == SCTP_FRAG_LEVEL_0) {
4447                         sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
4448                         sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
4449
4450                 } else {
4451                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4452                         error = EINVAL;
4453                 }
4454                 break;
4455         }
4456         case SCTP_CMT_ON_OFF:
4457                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
4458                         struct sctp_assoc_value *av;
4459
4460                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4461                         if (av->assoc_value > SCTP_CMT_MAX) {
4462                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4463                                 error = EINVAL;
4464                                 break;
4465                         }
4466                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4467                         if (stcb) {
4468                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
4469                                 SCTP_TCB_UNLOCK(stcb);
4470                         } else {
4471                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4472                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4473                                     (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4474                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4475                                         SCTP_INP_WLOCK(inp);
4476                                         inp->sctp_cmt_on_off = av->assoc_value;
4477                                         SCTP_INP_WUNLOCK(inp);
4478                                 }
4479                                 if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4480                                     (av->assoc_id == SCTP_ALL_ASSOC)) {
4481                                         SCTP_INP_RLOCK(inp);
4482                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4483                                                 SCTP_TCB_LOCK(stcb);
4484                                                 stcb->asoc.sctp_cmt_on_off = av->assoc_value;
4485                                                 SCTP_TCB_UNLOCK(stcb);
4486                                         }
4487                                         SCTP_INP_RUNLOCK(inp);
4488                                 }
4489                         }
4490                 } else {
4491                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
4492                         error = ENOPROTOOPT;
4493                 }
4494                 break;
4495         case SCTP_PLUGGABLE_CC:
4496         {
4497                 struct sctp_assoc_value *av;
4498                 struct sctp_nets *net;
4499
4500                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4501                 if ((av->assoc_value != SCTP_CC_RFC2581) &&
4502                     (av->assoc_value != SCTP_CC_HSTCP) &&
4503                     (av->assoc_value != SCTP_CC_HTCP) &&
4504                     (av->assoc_value != SCTP_CC_RTCC)) {
4505                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4506                         error = EINVAL;
4507                         break;
4508                 }
4509                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4510                 if (stcb) {
4511                         stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4512                         stcb->asoc.congestion_control_module = av->assoc_value;
4513                         if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4514                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4515                                         stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4516                                 }
4517                         }
4518                         SCTP_TCB_UNLOCK(stcb);
4519                 } else {
4520                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4521                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4522                             (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4523                             (av->assoc_id == SCTP_ALL_ASSOC)) {
4524                                 SCTP_INP_WLOCK(inp);
4525                                 inp->sctp_ep.sctp_default_cc_module = av->assoc_value;
4526                                 SCTP_INP_WUNLOCK(inp);
4527                         }
4528                         if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4529                             (av->assoc_id == SCTP_ALL_ASSOC)) {
4530                                 SCTP_INP_RLOCK(inp);
4531                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4532                                         SCTP_TCB_LOCK(stcb);
4533                                         stcb->asoc.cc_functions = sctp_cc_functions[av->assoc_value];
4534                                         stcb->asoc.congestion_control_module = av->assoc_value;
4535                                         if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) {
4536                                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4537                                                         stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
4538                                                 }
4539                                         }
4540                                         SCTP_TCB_UNLOCK(stcb);
4541                                 }
4542                                 SCTP_INP_RUNLOCK(inp);
4543                         }
4544                 }
4545                 break;
4546         }
4547         case SCTP_CC_OPTION:
4548         {
4549                 struct sctp_cc_option *cc_opt;
4550
4551                 SCTP_CHECK_AND_CAST(cc_opt, optval, struct sctp_cc_option, optsize);
4552                 SCTP_FIND_STCB(inp, stcb, cc_opt->aid_value.assoc_id);
4553                 if (stcb == NULL) {
4554                         if (cc_opt->aid_value.assoc_id == SCTP_CURRENT_ASSOC) {
4555                                 SCTP_INP_RLOCK(inp);
4556                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4557                                         SCTP_TCB_LOCK(stcb);
4558                                         if (stcb->asoc.cc_functions.sctp_cwnd_socket_option) {
4559                                                 (*stcb->asoc.cc_functions.sctp_cwnd_socket_option)(stcb, 1, cc_opt);
4560                                         }
4561                                         SCTP_TCB_UNLOCK(stcb);
4562                                 }
4563                                 SCTP_INP_RUNLOCK(inp);
4564                         } else {
4565                                 error = EINVAL;
4566                         }
4567                 } else {
4568                         if (stcb->asoc.cc_functions.sctp_cwnd_socket_option == NULL) {
4569                                 error = ENOTSUP;
4570                         } else {
4571                                 error = (*stcb->asoc.cc_functions.sctp_cwnd_socket_option)(stcb, 1,
4572                                                                                            cc_opt);
4573                         }
4574                         SCTP_TCB_UNLOCK(stcb);
4575                 }
4576                 break;
4577         }
4578         case SCTP_PLUGGABLE_SS:
4579         {
4580                 struct sctp_assoc_value *av;
4581
4582                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4583                 if ((av->assoc_value != SCTP_SS_DEFAULT) &&
4584                     (av->assoc_value != SCTP_SS_ROUND_ROBIN) &&
4585                     (av->assoc_value != SCTP_SS_ROUND_ROBIN_PACKET) &&
4586                     (av->assoc_value != SCTP_SS_PRIORITY) &&
4587                     (av->assoc_value != SCTP_SS_FAIR_BANDWITH) &&
4588                     (av->assoc_value != SCTP_SS_FIRST_COME)) {
4589                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4590                         error = EINVAL;
4591                         break;
4592                 }
4593                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4594                 if (stcb) {
4595                         stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4596                         stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4597                         stcb->asoc.stream_scheduling_module = av->assoc_value;
4598                         stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4599                         SCTP_TCB_UNLOCK(stcb);
4600                 } else {
4601                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4602                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4603                             (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4604                             (av->assoc_id == SCTP_ALL_ASSOC)) {
4605                                 SCTP_INP_WLOCK(inp);
4606                                 inp->sctp_ep.sctp_default_ss_module = av->assoc_value;
4607                                 SCTP_INP_WUNLOCK(inp);
4608                         }
4609                         if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4610                             (av->assoc_id == SCTP_ALL_ASSOC)) {
4611                                 SCTP_INP_RLOCK(inp);
4612                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4613                                         SCTP_TCB_LOCK(stcb);
4614                                         stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 1, 1);
4615                                         stcb->asoc.ss_functions = sctp_ss_functions[av->assoc_value];
4616                                         stcb->asoc.stream_scheduling_module = av->assoc_value;
4617                                         stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
4618                                         SCTP_TCB_UNLOCK(stcb);
4619                                 }
4620                                 SCTP_INP_RUNLOCK(inp);
4621                         }
4622                 }
4623                 break;
4624         }
4625         case SCTP_SS_VALUE:
4626         {
4627                 struct sctp_stream_value *av;
4628
4629                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_stream_value, optsize);
4630                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4631                 if (stcb) {
4632                         if (stcb->asoc.ss_functions.sctp_ss_set_value(stcb, &stcb->asoc, &stcb->asoc.strmout[av->stream_id],
4633                                                                       av->stream_value) < 0) {
4634                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4635                                 error = EINVAL;
4636                         }
4637                         SCTP_TCB_UNLOCK(stcb);
4638                 } else {
4639                         if (av->assoc_id == SCTP_CURRENT_ASSOC) {
4640                                 SCTP_INP_RLOCK(inp);
4641                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4642                                         SCTP_TCB_LOCK(stcb);
4643                                         stcb->asoc.ss_functions.sctp_ss_set_value(stcb,
4644                                                                                   &stcb->asoc,
4645                                                                                   &stcb->asoc.strmout[av->stream_id],
4646                                                                                   av->stream_value);
4647                                         SCTP_TCB_UNLOCK(stcb);
4648                                 }
4649                                 SCTP_INP_RUNLOCK(inp);
4650
4651                         } else {
4652                                 /* Can't set stream value without association */
4653                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4654                                 error = EINVAL;
4655                         }
4656                 }
4657                 break;
4658         }
4659         case SCTP_CLR_STAT_LOG:
4660                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4661                 error = EOPNOTSUPP;
4662                 break;
4663         case SCTP_CONTEXT:
4664         {
4665                 struct sctp_assoc_value *av;
4666
4667                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
4668                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
4669
4670                 if (stcb) {
4671                         stcb->asoc.context = av->assoc_value;
4672                         SCTP_TCB_UNLOCK(stcb);
4673                 } else {
4674                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4675                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4676                             (av->assoc_id == SCTP_FUTURE_ASSOC) ||
4677                             (av->assoc_id == SCTP_ALL_ASSOC)) {
4678                                 SCTP_INP_WLOCK(inp);
4679                                 inp->sctp_context = av->assoc_value;
4680                                 SCTP_INP_WUNLOCK(inp);
4681                         }
4682                         if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
4683                             (av->assoc_id == SCTP_ALL_ASSOC)) {
4684                                 SCTP_INP_RLOCK(inp);
4685                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4686                                         SCTP_TCB_LOCK(stcb);
4687                                         stcb->asoc.context = av->assoc_value;
4688                                         SCTP_TCB_UNLOCK(stcb);
4689                                 }
4690                                 SCTP_INP_RUNLOCK(inp);
4691                         }
4692                 }
4693                 break;
4694         }
4695         case SCTP_VRF_ID:
4696         {
4697                 uint32_t *default_vrfid;
4698 #ifdef SCTP_MVRF
4699                 int i;
4700 #endif
4701                 SCTP_CHECK_AND_CAST(default_vrfid, optval, uint32_t, optsize);
4702                 if (*default_vrfid > SCTP_MAX_VRF_ID) {
4703                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4704                         error = EINVAL;
4705                         break;
4706                 }
4707 #ifdef SCTP_MVRF
4708                 for (i = 0; i < inp->num_vrfs; i++) {
4709                         /* The VRF must be in the VRF list */
4710                         if (*default_vrfid == inp->m_vrf_ids[i]) {
4711                                 SCTP_INP_WLOCK(inp);
4712                                 inp->def_vrf_id = *default_vrfid;
4713                                 SCTP_INP_WUNLOCK(inp);
4714                                 goto sctp_done;
4715                         }
4716                 }
4717                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4718                 error = EINVAL;
4719 #else
4720                 inp->def_vrf_id = *default_vrfid;
4721 #endif
4722 #ifdef SCTP_MVRF
4723         sctp_done:
4724 #endif
4725                 break;
4726         }
4727         case SCTP_DEL_VRF_ID:
4728         {
4729 #ifdef SCTP_MVRF
4730                 uint32_t *del_vrfid;
4731                 int i, fnd = 0;
4732
4733                 SCTP_CHECK_AND_CAST(del_vrfid, optval, uint32_t, optsize);
4734                 if (*del_vrfid > SCTP_MAX_VRF_ID) {
4735                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4736                         error = EINVAL;
4737                         break;
4738                 }
4739                 if (inp->num_vrfs == 1) {
4740                         /* Can't delete last one */
4741                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4742                         error = EINVAL;
4743                         break;
4744                 }
4745                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
4746                         /* Can't add more once you are bound */
4747                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4748                         error = EINVAL;
4749                         break;
4750                 }
4751                 SCTP_INP_WLOCK(inp);
4752                 for (i = 0; i < inp->num_vrfs; i++) {
4753                         if (*del_vrfid == inp->m_vrf_ids[i]) {
4754                                 fnd = 1;
4755                                 break;
4756                         }
4757                 }
4758                 if (!fnd) {
4759                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4760                         error = EINVAL;
4761                         break;
4762                 }
4763                 if (i != (inp->num_vrfs - 1)) {
4764                         /* Take bottom one and move to this slot */
4765                         inp->m_vrf_ids[i] = inp->m_vrf_ids[(inp->num_vrfs-1)];
4766                 }
4767                 if (*del_vrfid == inp->def_vrf_id) {
4768                         /* Take the first one as the new default */
4769                         inp->def_vrf_id = inp->m_vrf_ids[0];
4770                 }
4771                 /* Drop the number by one killing last one */
4772                 inp->num_vrfs--;
4773 #else
4774                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4775                 error = EOPNOTSUPP;
4776 #endif
4777                 break;
4778         }
4779         case SCTP_ADD_VRF_ID:
4780         {
4781 #ifdef SCTP_MVRF
4782                 uint32_t *add_vrfid;
4783                 int i;
4784
4785                 SCTP_CHECK_AND_CAST(add_vrfid, optval, uint32_t, optsize);
4786                 if (*add_vrfid > SCTP_MAX_VRF_ID) {
4787                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4788                         error = EINVAL;
4789                         break;
4790                 }
4791                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
4792                         /* Can't add more once you are bound */
4793                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4794                         error = EINVAL;
4795                         break;
4796                 }
4797                 SCTP_INP_WLOCK(inp);
4798                 /* Verify its not already here */
4799                 for (i = 0; i < inp->num_vrfs; i++) {
4800                         if (*add_vrfid == inp->m_vrf_ids[i]) {
4801                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
4802                                 error = EALREADY;
4803                                 SCTP_INP_WUNLOCK(inp);
4804                                 break;
4805                         }
4806                 }
4807                 if ((inp->num_vrfs + 1) > inp->vrf_size) {
4808                         /* need to grow array */
4809                         uint32_t *tarray;
4810                         SCTP_MALLOC(tarray, uint32_t *,
4811                                     (sizeof(uint32_t) * (inp->vrf_size + SCTP_DEFAULT_VRF_SIZE)),
4812                                     SCTP_M_MVRF);
4813                         if (tarray == NULL) {
4814                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4815                                 error = ENOMEM;
4816                                 SCTP_INP_WUNLOCK(inp);
4817                                 break;
4818                         }
4819                         memcpy(tarray, inp->m_vrf_ids, (sizeof(uint32_t) * inp->vrf_size));
4820                         SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4821                         inp->m_vrf_ids = tarray;
4822                         inp->vrf_size += SCTP_DEFAULT_VRF_SIZE;
4823                 }
4824                 inp->m_vrf_ids[inp->num_vrfs] = *add_vrfid;
4825                 inp->num_vrfs++;
4826                 SCTP_INP_WUNLOCK(inp);
4827 #else
4828                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
4829                 error = EOPNOTSUPP;
4830 #endif
4831                 break;
4832         }
4833         case SCTP_DELAYED_SACK:
4834         {
4835                 struct sctp_sack_info *sack;
4836
4837                 SCTP_CHECK_AND_CAST(sack, optval, struct sctp_sack_info, optsize);
4838                 SCTP_FIND_STCB(inp, stcb, sack->sack_assoc_id);
4839                 if (sack->sack_delay) {
4840                         if (sack->sack_delay > SCTP_MAX_SACK_DELAY)
4841                                 sack->sack_delay = SCTP_MAX_SACK_DELAY;
4842                         if (MSEC_TO_TICKS(sack->sack_delay) < 1) {
4843                                 sack->sack_delay = TICKS_TO_MSEC(1);
4844                         }
4845                 }
4846                 if (stcb) {
4847                         if (sack->sack_delay) {
4848                                 stcb->asoc.delayed_ack = sack->sack_delay;
4849                         }
4850                         if (sack->sack_freq) {
4851                                 stcb->asoc.sack_freq = sack->sack_freq;
4852                         }
4853                         SCTP_TCB_UNLOCK(stcb);
4854                 } else {
4855                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4856                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4857                             (sack->sack_assoc_id == SCTP_FUTURE_ASSOC) ||
4858                             (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4859                                 SCTP_INP_WLOCK(inp);
4860                                 if (sack->sack_delay) {
4861                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sack->sack_delay);
4862                                 }
4863                                 if (sack->sack_freq) {
4864                                         inp->sctp_ep.sctp_sack_freq = sack->sack_freq;
4865                                 }
4866                                 SCTP_INP_WUNLOCK(inp);
4867                         }
4868                         if ((sack->sack_assoc_id == SCTP_CURRENT_ASSOC) ||
4869                             (sack->sack_assoc_id == SCTP_ALL_ASSOC)) {
4870                                 SCTP_INP_RLOCK(inp);
4871                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4872                                         SCTP_TCB_LOCK(stcb);
4873                                         if (sack->sack_delay) {
4874                                                 stcb->asoc.delayed_ack = sack->sack_delay;
4875                                         }
4876                                         if (sack->sack_freq) {
4877                                                 stcb->asoc.sack_freq = sack->sack_freq;
4878                                         }
4879                                         SCTP_TCB_UNLOCK(stcb);
4880                                 }
4881                                 SCTP_INP_RUNLOCK(inp);
4882                         }
4883                 }
4884                 break;
4885         }
4886         case SCTP_AUTH_CHUNK:
4887         {
4888                 struct sctp_authchunk *sauth;
4889
4890                 SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
4891
4892                 SCTP_INP_WLOCK(inp);
4893                 if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks)) {
4894                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4895                         error = EINVAL;
4896                 }
4897                 SCTP_INP_WUNLOCK(inp);
4898                 break;
4899         }
4900         case SCTP_AUTH_KEY:
4901         {
4902                 struct sctp_authkey *sca;
4903                 struct sctp_keyhead *shared_keys;
4904                 sctp_sharedkey_t *shared_key;
4905                 sctp_key_t *key = NULL;
4906                 size_t size;
4907
4908                 SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
4909                 if (sca->sca_keylength == 0) {
4910                         size = optsize - sizeof(struct sctp_authkey);
4911                 } else {
4912                         if (sca->sca_keylength + sizeof(struct sctp_authkey) <= optsize) {
4913                                 size = sca->sca_keylength;
4914                         } else {
4915                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
4916                                 error = EINVAL;
4917                                 break;
4918                         }
4919                 }
4920                 SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id);
4921
4922                 if (stcb) {
4923                         shared_keys = &stcb->asoc.shared_keys;
4924                         /* clear the cached keys for this key id */
4925                         sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4926                         /*
4927                          * create the new shared key and
4928                          * insert/replace it
4929                          */
4930                         if (size > 0) {
4931                                 key = sctp_set_key(sca->sca_key, (uint32_t) size);
4932                                 if (key == NULL) {
4933                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4934                                         error = ENOMEM;
4935                                         SCTP_TCB_UNLOCK(stcb);
4936                                         break;
4937                                 }
4938                         }
4939                         shared_key = sctp_alloc_sharedkey();
4940                         if (shared_key == NULL) {
4941                                 sctp_free_key(key);
4942                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4943                                 error = ENOMEM;
4944                                 SCTP_TCB_UNLOCK(stcb);
4945                                 break;
4946                         }
4947                         shared_key->key = key;
4948                         shared_key->keyid = sca->sca_keynumber;
4949                         error = sctp_insert_sharedkey(shared_keys, shared_key);
4950                         SCTP_TCB_UNLOCK(stcb);
4951                 } else {
4952                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4953                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4954                             (sca->sca_assoc_id == SCTP_FUTURE_ASSOC) ||
4955                             (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4956                                 SCTP_INP_WLOCK(inp);
4957                                 shared_keys = &inp->sctp_ep.shared_keys;
4958                                 /*
4959                                  * clear the cached keys on all assocs for
4960                                  * this key id
4961                                  */
4962                                 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
4963                                 /*
4964                                  * create the new shared key and
4965                                  * insert/replace it
4966                                  */
4967                                 if (size > 0) {
4968                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
4969                                         if (key == NULL) {
4970                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4971                                                 error = ENOMEM;
4972                                                 SCTP_INP_WUNLOCK(inp);
4973                                                 break;
4974                                         }
4975                                 }
4976                                 shared_key = sctp_alloc_sharedkey();
4977                                 if (shared_key == NULL) {
4978                                         sctp_free_key(key);
4979                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
4980                                         error = ENOMEM;
4981                                         SCTP_INP_WUNLOCK(inp);
4982                                         break;
4983                                 }
4984                                 shared_key->key = key;
4985                                 shared_key->keyid = sca->sca_keynumber;
4986                                 error = sctp_insert_sharedkey(shared_keys, shared_key);
4987                                 SCTP_INP_WUNLOCK(inp);
4988                         }
4989                         if ((sca->sca_assoc_id == SCTP_CURRENT_ASSOC) ||
4990                             (sca->sca_assoc_id == SCTP_ALL_ASSOC)) {
4991                                 SCTP_INP_RLOCK(inp);
4992                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4993                                         SCTP_TCB_LOCK(stcb);
4994                                         shared_keys = &stcb->asoc.shared_keys;
4995                                         /* clear the cached keys for this key id */
4996                                         sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
4997                                         /*
4998                                          * create the new shared key and
4999                                          * insert/replace it
5000                                          */
5001                                         if (size > 0) {
5002                                                 key = sctp_set_key(sca->sca_key, (uint32_t) size);
5003                                                 if (key == NULL) {
5004                                                         SCTP_TCB_UNLOCK(stcb);
5005                                                         continue;
5006                                                 }
5007                                         }
5008                                         shared_key = sctp_alloc_sharedkey();
5009                                         if (shared_key == NULL) {
5010                                                 sctp_free_key(key);
5011                                                 SCTP_TCB_UNLOCK(stcb);
5012                                                 continue;
5013                                         }
5014                                         shared_key->key = key;
5015                                         shared_key->keyid = sca->sca_keynumber;
5016                                         error = sctp_insert_sharedkey(shared_keys, shared_key);
5017                                         SCTP_TCB_UNLOCK(stcb);
5018                                 }
5019                                 SCTP_INP_RUNLOCK(inp);
5020                         }
5021                 }
5022                 break;
5023         }
5024         case SCTP_HMAC_IDENT:
5025         {
5026                 struct sctp_hmacalgo *shmac;
5027                 sctp_hmaclist_t *hmaclist;
5028                 uint16_t hmacid;
5029                 uint32_t i;
5030
5031                 SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
5032                 if ((optsize < sizeof(struct sctp_hmacalgo) + shmac->shmac_number_of_idents * sizeof(uint16_t)) ||
5033                     (shmac->shmac_number_of_idents > 0xffff)) {
5034                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5035                         error = EINVAL;
5036                         break;
5037                 }
5038
5039                 hmaclist = sctp_alloc_hmaclist((uint16_t)shmac->shmac_number_of_idents);
5040                 if (hmaclist == NULL) {
5041                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOMEM);
5042                         error = ENOMEM;
5043                         break;
5044                 }
5045                 for (i = 0; i < shmac->shmac_number_of_idents; i++) {
5046                         hmacid = shmac->shmac_idents[i];
5047                         if (sctp_auth_add_hmacid(hmaclist, hmacid)) {
5048                                 /* invalid HMACs were found */;
5049                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5050                                 error = EINVAL;
5051                                 sctp_free_hmaclist(hmaclist);
5052                                 goto sctp_set_hmac_done;
5053                         }
5054                 }
5055                 for (i = 0; i < hmaclist->num_algo; i++) {
5056                         if (hmaclist->hmac[i] == SCTP_AUTH_HMAC_ID_SHA1) {
5057                                 /* already in list */
5058                                 break;
5059                         }
5060                 }
5061                 if (i == hmaclist->num_algo) {
5062                         /* not found in list */
5063                         sctp_free_hmaclist(hmaclist);
5064                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5065                         error = EINVAL;
5066                         break;
5067                 }
5068                 /* set it on the endpoint */
5069                 SCTP_INP_WLOCK(inp);
5070                 if (inp->sctp_ep.local_hmacs)
5071                         sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
5072                 inp->sctp_ep.local_hmacs = hmaclist;
5073                 SCTP_INP_WUNLOCK(inp);
5074         sctp_set_hmac_done:
5075                 break;
5076         }
5077         case SCTP_AUTH_ACTIVE_KEY:
5078         {
5079                 struct sctp_authkeyid *scact;
5080
5081                 SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
5082                 SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
5083
5084                 /* set the active key on the right place */
5085                 if (stcb) {
5086                         /* set the active key on the assoc */
5087                         if (sctp_auth_setactivekey(stcb,
5088                                                    scact->scact_keynumber)) {
5089                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL,
5090                                                     SCTP_FROM_SCTP_USRREQ,
5091                                                     EINVAL);
5092                                 error = EINVAL;
5093                         }
5094                         SCTP_TCB_UNLOCK(stcb);
5095                 } else {
5096                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5097                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5098                             (scact->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
5099                             (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
5100                                 SCTP_INP_WLOCK(inp);
5101                                 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber)) {
5102                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5103                                         error = EINVAL;
5104                                 }
5105                                 SCTP_INP_WUNLOCK(inp);
5106                         }
5107                         if ((scact->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
5108                             (scact->scact_assoc_id == SCTP_ALL_ASSOC)) {
5109                                 SCTP_INP_RLOCK(inp);
5110                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5111                                         SCTP_TCB_LOCK(stcb);
5112                                         sctp_auth_setactivekey(stcb, scact->scact_keynumber);
5113                                         SCTP_TCB_UNLOCK(stcb);
5114                                 }
5115                                 SCTP_INP_RUNLOCK(inp);
5116                         }
5117                 }
5118                 break;
5119         }
5120         case SCTP_AUTH_DELETE_KEY:
5121         {
5122                 struct sctp_authkeyid *scdel;
5123
5124                 SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
5125                 SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
5126
5127                 /* delete the key from the right place */
5128                 if (stcb) {
5129                         if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber)) {
5130                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5131                                 error = EINVAL;
5132                         }
5133                         SCTP_TCB_UNLOCK(stcb);
5134                 } else {
5135                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5136                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5137                             (scdel->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
5138                             (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
5139                                 SCTP_INP_WLOCK(inp);
5140                                 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber)) {
5141                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5142                                         error = EINVAL;
5143                                 }
5144                                 SCTP_INP_WUNLOCK(inp);
5145                         }
5146                         if ((scdel->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
5147                             (scdel->scact_assoc_id == SCTP_ALL_ASSOC)) {
5148                                 SCTP_INP_RLOCK(inp);
5149                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5150                                         SCTP_TCB_LOCK(stcb);
5151                                         sctp_delete_sharedkey(stcb, scdel->scact_keynumber);
5152                                         SCTP_TCB_UNLOCK(stcb);
5153                                 }
5154                                 SCTP_INP_RUNLOCK(inp);
5155                         }
5156                 }
5157                 break;
5158         }
5159         case SCTP_AUTH_DEACTIVATE_KEY:
5160         {
5161                 struct sctp_authkeyid *keyid;
5162
5163                 SCTP_CHECK_AND_CAST(keyid, optval, struct sctp_authkeyid, optsize);
5164                 SCTP_FIND_STCB(inp, stcb, keyid->scact_assoc_id);
5165
5166                 /* deactivate the key from the right place */
5167                 if (stcb) {
5168                         if (sctp_deact_sharedkey(stcb, keyid->scact_keynumber)) {
5169                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5170                                 error = EINVAL;
5171                         }
5172                         SCTP_TCB_UNLOCK(stcb);
5173                 } else {
5174                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5175                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5176                             (keyid->scact_assoc_id == SCTP_FUTURE_ASSOC) ||
5177                             (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
5178                                 SCTP_INP_WLOCK(inp);
5179                                 if (sctp_deact_sharedkey_ep(inp, keyid->scact_keynumber)) {
5180                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5181                                         error = EINVAL;
5182                                 }
5183                                 SCTP_INP_WUNLOCK(inp);
5184                         }
5185                         if ((keyid->scact_assoc_id == SCTP_CURRENT_ASSOC) ||
5186                             (keyid->scact_assoc_id == SCTP_ALL_ASSOC)) {
5187                                 SCTP_INP_RLOCK(inp);
5188                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5189                                         SCTP_TCB_LOCK(stcb);
5190                                         sctp_deact_sharedkey(stcb, keyid->scact_keynumber);
5191                                         SCTP_TCB_UNLOCK(stcb);
5192                                 }
5193                                 SCTP_INP_RUNLOCK(inp);
5194                         }
5195                 }
5196                 break;
5197         }
5198         case SCTP_ENABLE_STREAM_RESET:
5199         {
5200                 struct sctp_assoc_value *av;
5201
5202                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
5203                 if (av->assoc_value & (~SCTP_ENABLE_VALUE_MASK)) {
5204                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5205                         error = EINVAL;
5206                         break;
5207                 }
5208                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
5209                 if (stcb) {
5210                         stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
5211                         SCTP_TCB_UNLOCK(stcb);
5212                 } else {
5213                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5214                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5215                             (av->assoc_id == SCTP_FUTURE_ASSOC) ||
5216                             (av->assoc_id == SCTP_ALL_ASSOC)) {
5217                                 SCTP_INP_WLOCK(inp);
5218                                 inp->local_strreset_support = (uint8_t)av->assoc_value;
5219                                 SCTP_INP_WUNLOCK(inp);
5220                         }
5221                         if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
5222                             (av->assoc_id == SCTP_ALL_ASSOC)) {
5223                                 SCTP_INP_RLOCK(inp);
5224                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5225                                         SCTP_TCB_LOCK(stcb);
5226                                         stcb->asoc.local_strreset_support = (uint8_t)av->assoc_value;
5227                                         SCTP_TCB_UNLOCK(stcb);
5228                                 }
5229                                 SCTP_INP_RUNLOCK(inp);
5230                         }
5231
5232                 }
5233                 break;
5234         }
5235         case SCTP_RESET_STREAMS:
5236         {
5237                 struct sctp_reset_streams *strrst;
5238                 int i, send_out = 0;
5239                 int send_in = 0;
5240
5241                 SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_reset_streams, optsize);
5242                 SCTP_FIND_STCB(inp, stcb, strrst->srs_assoc_id);
5243                 if (stcb == NULL) {
5244                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
5245                         error = ENOENT;
5246                         break;
5247                 }
5248                 if (stcb->asoc.reconfig_supported == 0) {
5249                         /*
5250                          * Peer does not support the chunk type.
5251                          */
5252                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
5253                         error = EOPNOTSUPP;
5254                         SCTP_TCB_UNLOCK(stcb);
5255                         break;
5256                 }
5257                 if (sizeof(struct sctp_reset_streams) +
5258                     strrst->srs_number_streams * sizeof(uint16_t) > optsize) {
5259                         error = EINVAL;
5260                         SCTP_TCB_UNLOCK(stcb);
5261                         break;
5262                 }
5263                 if (stcb->asoc.stream_reset_outstanding) {
5264                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
5265                         error = EALREADY;
5266                         SCTP_TCB_UNLOCK(stcb);
5267                         break;
5268                 }
5269                 if (strrst->srs_flags & SCTP_STREAM_RESET_INCOMING) {
5270                         send_in = 1;
5271                 }
5272                 if (strrst->srs_flags & SCTP_STREAM_RESET_OUTGOING) {
5273                         send_out = 1;
5274                 }
5275                 if ((send_in == 0) && (send_out == 0)) {
5276                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5277                         error = EINVAL;
5278                         SCTP_TCB_UNLOCK(stcb);
5279                         break;
5280                 }
5281                 for (i = 0; i < strrst->srs_number_streams; i++) {
5282                         if ((send_in) &&
5283                             (strrst->srs_stream_list[i] > stcb->asoc.streamincnt)) {
5284                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5285                                 error = EINVAL;
5286                                 break;
5287                         }
5288                         if ((send_out) &&
5289                             (strrst->srs_stream_list[i] > stcb->asoc.streamoutcnt)) {
5290                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5291                                 error = EINVAL;
5292                                 break;
5293                         }
5294                 }
5295                 if (error) {
5296                         SCTP_TCB_UNLOCK(stcb);
5297                         break;
5298                 }
5299                 error = sctp_send_str_reset_req(stcb, strrst->srs_number_streams,
5300                                                 strrst->srs_stream_list,
5301                                                 send_out, send_in, 0, 0, 0, 0, 0);
5302
5303                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
5304                 SCTP_TCB_UNLOCK(stcb);
5305                 break;
5306         }
5307         case SCTP_ADD_STREAMS:
5308         {
5309                 struct sctp_add_streams *stradd;
5310                 uint8_t addstream = 0;
5311                 uint16_t add_o_strmcnt = 0;
5312                 uint16_t add_i_strmcnt = 0;
5313
5314                 SCTP_CHECK_AND_CAST(stradd, optval, struct sctp_add_streams, optsize);
5315                 SCTP_FIND_STCB(inp, stcb, stradd->sas_assoc_id);
5316                 if (stcb == NULL) {
5317                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
5318                         error = ENOENT;
5319                         break;
5320                 }
5321                 if (stcb->asoc.reconfig_supported == 0) {
5322                         /*
5323                          * Peer does not support the chunk type.
5324                          */
5325                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
5326                         error = EOPNOTSUPP;
5327                         SCTP_TCB_UNLOCK(stcb);
5328                         break;
5329                 }
5330                 if (stcb->asoc.stream_reset_outstanding) {
5331                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
5332                         error = EALREADY;
5333                         SCTP_TCB_UNLOCK(stcb);
5334                         break;
5335                 }
5336                 if ((stradd->sas_outstrms == 0) &&
5337                     (stradd->sas_instrms == 0)) {
5338                         error = EINVAL;
5339                         goto skip_stuff;
5340                 }
5341                 if (stradd->sas_outstrms) {
5342                         addstream = 1;
5343                         /* We allocate here */
5344                         add_o_strmcnt = stradd->sas_outstrms;
5345                         if ((((int)add_o_strmcnt) + ((int)stcb->asoc.streamoutcnt)) > 0x0000ffff) {
5346                                 /* You can't have more than 64k */
5347                                 error = EINVAL;
5348                                 goto skip_stuff;
5349                         }
5350                 }
5351                 if (stradd->sas_instrms) {
5352                         int cnt;
5353
5354                         addstream |= 2;
5355                         /* We allocate inside sctp_send_str_reset_req() */
5356                         add_i_strmcnt = stradd->sas_instrms;
5357                         cnt = add_i_strmcnt;
5358                         cnt += stcb->asoc.streamincnt;
5359                         if (cnt > 0x0000ffff) {
5360                                 /* You can't have more than 64k */
5361                                 error = EINVAL;
5362                                 goto skip_stuff;
5363                         }
5364                         if (cnt > (int)stcb->asoc.max_inbound_streams) {
5365                                 /* More than you are allowed */
5366                                 error = EINVAL;
5367                                 goto skip_stuff;
5368                         }
5369                 }
5370                 error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 0, addstream, add_o_strmcnt, add_i_strmcnt, 0);
5371                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
5372         skip_stuff:
5373                 SCTP_TCB_UNLOCK(stcb);
5374                 break;
5375         }
5376         case SCTP_RESET_ASSOC:
5377         {
5378                 uint32_t *value;
5379
5380                 SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
5381                 SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) *value);
5382                 if (stcb == NULL) {
5383                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
5384                         error = ENOENT;
5385                         break;
5386                 }
5387                 if (stcb->asoc.reconfig_supported == 0) {
5388                         /*
5389                          * Peer does not support the chunk type.
5390                          */
5391                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
5392                         error = EOPNOTSUPP;
5393                         SCTP_TCB_UNLOCK(stcb);
5394                         break;
5395                 }
5396                 if (stcb->asoc.stream_reset_outstanding) {
5397                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
5398                         error = EALREADY;
5399                         SCTP_TCB_UNLOCK(stcb);
5400                         break;
5401                 }
5402                 error = sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, 0, 0, 0, 0);
5403                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ, SCTP_SO_LOCKED);
5404                 SCTP_TCB_UNLOCK(stcb);
5405                 break;
5406         }
5407         case SCTP_CONNECT_X:
5408                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
5409                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5410                         error = EINVAL;
5411                         break;
5412                 }
5413                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
5414                 break;
5415         case SCTP_CONNECT_X_DELAYED:
5416                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
5417                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5418                         error = EINVAL;
5419                         break;
5420                 }
5421                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
5422                 break;
5423         case SCTP_CONNECT_X_COMPLETE:
5424         {
5425                 struct sockaddr *sa;
5426                 struct sctp_nets *net;
5427
5428                 /* FIXME MT: check correct? */
5429                 SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
5430
5431                 /* find tcb */
5432                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5433                         SCTP_INP_RLOCK(inp);
5434                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
5435                         if (stcb) {
5436                                 SCTP_TCB_LOCK(stcb);
5437                                 net = sctp_findnet(stcb, sa);
5438                         }
5439                         SCTP_INP_RUNLOCK(inp);
5440                 } else {
5441                         /* We increment here since sctp_findassociation_ep_addr() wil
5442                          * do a decrement if it finds the stcb as long as the locked
5443                          * tcb (last argument) is NOT a TCB.. aka NULL.
5444                          */
5445                         SCTP_INP_INCR_REF(inp);
5446                         stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
5447                         if (stcb == NULL) {
5448                                 SCTP_INP_DECR_REF(inp);
5449                         }
5450                 }
5451
5452                 if (stcb == NULL) {
5453                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
5454                         error = ENOENT;
5455                         break;
5456                 }
5457                 if (stcb->asoc.delayed_connection == 1) {
5458                         stcb->asoc.delayed_connection = 0;
5459                         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
5460                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
5461                                         stcb->asoc.primary_destination,
5462                                         SCTP_FROM_SCTP_USRREQ+SCTP_LOC_9);
5463                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
5464                 } else {
5465                         /*
5466                          * already expired or did not use delayed
5467                          * connectx
5468                          */
5469                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
5470                         error = EALREADY;
5471                 }
5472                 SCTP_TCB_UNLOCK(stcb);
5473                 break;
5474         }
5475         case SCTP_MAX_BURST:
5476         {
5477 #if defined(__FreeBSD__) && __FreeBSD_version < 900000
5478                 uint8_t *burst;
5479
5480                 SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
5481
5482                 SCTP_INP_WLOCK(inp);
5483                 inp->sctp_ep.max_burst = *burst;
5484                 SCTP_INP_WUNLOCK(inp);
5485 #else
5486                 struct sctp_assoc_value *av;
5487
5488                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
5489                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
5490
5491                 if (stcb) {
5492                         stcb->asoc.max_burst = av->assoc_value;
5493                         SCTP_TCB_UNLOCK(stcb);
5494                 } else {
5495                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5496                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5497                             (av->assoc_id == SCTP_FUTURE_ASSOC) ||
5498                             (av->assoc_id == SCTP_ALL_ASSOC)) {
5499                                 SCTP_INP_WLOCK(inp);
5500                                 inp->sctp_ep.max_burst = av->assoc_value;
5501                                 SCTP_INP_WUNLOCK(inp);
5502                         }
5503                         if ((av->assoc_id == SCTP_CURRENT_ASSOC) ||
5504                             (av->assoc_id == SCTP_ALL_ASSOC)) {
5505                                 SCTP_INP_RLOCK(inp);
5506                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5507                                         SCTP_TCB_LOCK(stcb);
5508                                         stcb->asoc.max_burst = av->assoc_value;
5509                                         SCTP_TCB_UNLOCK(stcb);
5510                                 }
5511                                 SCTP_INP_RUNLOCK(inp);
5512                         }
5513                 }
5514 #endif
5515                 break;
5516         }
5517         case SCTP_MAXSEG:
5518         {
5519                 struct sctp_assoc_value *av;
5520                 int ovh;
5521
5522                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
5523                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
5524
5525                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5526                         ovh = SCTP_MED_OVERHEAD;
5527                 } else {
5528                         ovh = SCTP_MED_V4_OVERHEAD;
5529                 }
5530                 if (stcb) {
5531                         if (av->assoc_value) {
5532                                 stcb->asoc.sctp_frag_point = (av->assoc_value + ovh);
5533                         } else {
5534                                 stcb->asoc.sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
5535                         }
5536                         SCTP_TCB_UNLOCK(stcb);
5537                 } else {
5538                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5539                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5540                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
5541                                 SCTP_INP_WLOCK(inp);
5542                                 /* FIXME MT: I think this is not in tune with the API ID */
5543                                 if (av->assoc_value) {
5544                                         inp->sctp_frag_point = (av->assoc_value + ovh);
5545                                 } else {
5546                                         inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
5547                                 }
5548                                 SCTP_INP_WUNLOCK(inp);
5549                         } else {
5550                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5551                                 error = EINVAL;
5552                         }
5553                 }
5554                 break;
5555         }
5556         case SCTP_EVENTS:
5557         {
5558                 struct sctp_event_subscribe *events;
5559
5560                 SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
5561
5562                 SCTP_INP_WLOCK(inp);
5563                 if (events->sctp_data_io_event) {
5564                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5565                 } else {
5566                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
5567                 }
5568
5569                 if (events->sctp_association_event) {
5570                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5571                 } else {
5572                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5573                 }
5574
5575                 if (events->sctp_address_event) {
5576                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5577                 } else {
5578                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
5579                 }
5580
5581                 if (events->sctp_send_failure_event) {
5582                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5583                 } else {
5584                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5585                 }
5586
5587                 if (events->sctp_peer_error_event) {
5588                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5589                 } else {
5590                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
5591                 }
5592
5593                 if (events->sctp_shutdown_event) {
5594                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5595                 } else {
5596                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5597                 }
5598
5599                 if (events->sctp_partial_delivery_event) {
5600                         sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5601                 } else {
5602                         sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
5603                 }
5604
5605                 if (events->sctp_adaptation_layer_event) {
5606                         sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5607                 } else {
5608                         sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5609                 }
5610
5611                 if (events->sctp_authentication_event) {
5612                         sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5613                 } else {
5614                         sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
5615                 }
5616
5617                 if (events->sctp_sender_dry_event) {
5618                         sctp_feature_on(inp, SCTP_PCB_FLAGS_DRYEVNT);
5619                 } else {
5620                         sctp_feature_off(inp, SCTP_PCB_FLAGS_DRYEVNT);
5621                 }
5622
5623                 if (events->sctp_stream_reset_event) {
5624                         sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5625                 } else {
5626                         sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5627                 }
5628                 SCTP_INP_WUNLOCK(inp);
5629
5630                 SCTP_INP_RLOCK(inp);
5631                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5632                         SCTP_TCB_LOCK(stcb);
5633                         if (events->sctp_association_event) {
5634                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5635                         } else {
5636                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT);
5637                         }
5638                         if (events->sctp_address_event) {
5639                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5640                         } else {
5641                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT);
5642                         }
5643                         if (events->sctp_send_failure_event) {
5644                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5645                         } else {
5646                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
5647                         }
5648                         if (events->sctp_peer_error_event) {
5649                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5650                         } else {
5651                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVPEERERR);
5652                         }
5653                         if (events->sctp_shutdown_event) {
5654                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5655                         } else {
5656                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
5657                         }
5658                         if (events->sctp_partial_delivery_event) {
5659                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5660                         } else {
5661                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT);
5662                         }
5663                         if (events->sctp_adaptation_layer_event) {
5664                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5665                         } else {
5666                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
5667                         }
5668                         if (events->sctp_authentication_event) {
5669                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5670                         } else {
5671                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_AUTHEVNT);
5672                         }
5673                         if (events->sctp_sender_dry_event) {
5674                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5675                         } else {
5676                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DRYEVNT);
5677                         }
5678                         if (events->sctp_stream_reset_event) {
5679                                 sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5680                         } else {
5681                                 sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
5682                         }
5683                         SCTP_TCB_UNLOCK(stcb);
5684                 }
5685                 /* Send up the sender dry event only for 1-to-1 style sockets. */
5686                 if (events->sctp_sender_dry_event) {
5687                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5688                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5689                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
5690                                 if (stcb) {
5691                                         SCTP_TCB_LOCK(stcb);
5692                                         if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5693                                             TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5694                                             (stcb->asoc.stream_queue_cnt == 0)) {
5695                                                 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb,  0, NULL, SCTP_SO_LOCKED);
5696                                         }
5697                                         SCTP_TCB_UNLOCK(stcb);
5698                                 }
5699                         }
5700                 }
5701                 SCTP_INP_RUNLOCK(inp);
5702                 break;
5703         }
5704         case SCTP_ADAPTATION_LAYER:
5705         {
5706                 struct sctp_setadaptation *adap_bits;
5707
5708                 SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
5709                 SCTP_INP_WLOCK(inp);
5710                 inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
5711                 inp->sctp_ep.adaptation_layer_indicator_provided = 1;
5712                 SCTP_INP_WUNLOCK(inp);
5713                 break;
5714         }
5715 #ifdef SCTP_DEBUG
5716         case SCTP_SET_INITIAL_DBG_SEQ:
5717         {
5718                 uint32_t *vvv;
5719
5720                 SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
5721                 SCTP_INP_WLOCK(inp);
5722                 inp->sctp_ep.initial_sequence_debug = *vvv;
5723                 SCTP_INP_WUNLOCK(inp);
5724                 break;
5725         }
5726 #endif
5727         case SCTP_DEFAULT_SEND_PARAM:
5728         {
5729                 struct sctp_sndrcvinfo *s_info;
5730
5731                 SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
5732                 SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
5733
5734                 if (stcb) {
5735                         if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5736                                 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5737                         } else {
5738                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5739                                 error = EINVAL;
5740                         }
5741                         SCTP_TCB_UNLOCK(stcb);
5742                 } else {
5743                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5744                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
5745                             (s_info->sinfo_assoc_id == SCTP_FUTURE_ASSOC) ||
5746                             (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5747                                 SCTP_INP_WLOCK(inp);
5748                                 memcpy(&inp->def_send, s_info, min(optsize, sizeof(inp->def_send)));
5749                                 SCTP_INP_WUNLOCK(inp);
5750                         }
5751                         if ((s_info->sinfo_assoc_id == SCTP_CURRENT_ASSOC) ||
5752                             (s_info->sinfo_assoc_id == SCTP_ALL_ASSOC)) {
5753                                 SCTP_INP_RLOCK(inp);
5754                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5755                                         SCTP_TCB_LOCK(stcb);
5756                                         if (s_info->sinfo_stream < stcb->asoc.streamoutcnt) {
5757                                                 memcpy(&stcb->asoc.def_send, s_info, min(optsize, sizeof(stcb->asoc.def_send)));
5758                                         }
5759                                         SCTP_TCB_UNLOCK(stcb);
5760                                 }
5761                                 SCTP_INP_RUNLOCK(inp);
5762                         }
5763                 }
5764                 break;
5765         }
5766         case SCTP_PEER_ADDR_PARAMS:
5767         {
5768                 struct sctp_paddrparams *paddrp;
5769                 struct sctp_nets *net;
5770
5771                 SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
5772                 SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
5773                 net = NULL;
5774                 if (stcb) {
5775                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
5776                 } else {
5777                         /* We increment here since sctp_findassociation_ep_addr() wil
5778                          * do a decrement if it finds the stcb as long as the locked
5779                          * tcb (last argument) is NOT a TCB.. aka NULL.
5780                          */
5781                         SCTP_INP_INCR_REF(inp);
5782                         stcb = sctp_findassociation_ep_addr(&inp,
5783                                                             (struct sockaddr *)&paddrp->spp_address,
5784                                                             &net, NULL, NULL);
5785                         if (stcb == NULL) {
5786                                 SCTP_INP_DECR_REF(inp);
5787                         }
5788                 }
5789                 if (stcb && (net == NULL)) {
5790                         struct sockaddr *sa;
5791
5792                         sa = (struct sockaddr *)&paddrp->spp_address;
5793 #ifdef INET
5794                         if (sa->sa_family == AF_INET) {
5795
5796                                 struct sockaddr_in *sin;
5797                                 sin = (struct sockaddr_in *)sa;
5798                                 if (sin->sin_addr.s_addr) {
5799                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5800                                         SCTP_TCB_UNLOCK(stcb);
5801                                         error = EINVAL;
5802                                         break;
5803                                 }
5804                         } else
5805 #endif
5806 #ifdef INET6
5807                         if (sa->sa_family == AF_INET6) {
5808                                 struct sockaddr_in6 *sin6;
5809
5810                                 sin6 = (struct sockaddr_in6 *)sa;
5811                                 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
5812                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5813                                         SCTP_TCB_UNLOCK(stcb);
5814                                         error = EINVAL;
5815                                         break;
5816                                 }
5817                         } else
5818 #endif
5819 #if defined(__Userspace__)
5820                         if (sa->sa_family == AF_CONN) {
5821                                 struct sockaddr_conn *sconn;
5822
5823                                 sconn = (struct sockaddr_conn *)sa;
5824                                 if (sconn->sconn_addr != NULL) {
5825                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5826                                         SCTP_TCB_UNLOCK(stcb);
5827                                         error = EINVAL;
5828                                         break;
5829                                 }
5830                         } else
5831 #endif
5832                         {
5833                                 error = EAFNOSUPPORT;
5834                                 SCTP_TCB_UNLOCK(stcb);
5835                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
5836                                 break;
5837                         }
5838                 }
5839                 /* sanity checks */
5840                 if ((paddrp->spp_flags & SPP_HB_ENABLE) && (paddrp->spp_flags & SPP_HB_DISABLE)) {
5841                         if (stcb)
5842                                 SCTP_TCB_UNLOCK(stcb);
5843                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5844                         return (EINVAL);
5845                 }
5846
5847                 if ((paddrp->spp_flags & SPP_PMTUD_ENABLE) && (paddrp->spp_flags & SPP_PMTUD_DISABLE)) {
5848                         if (stcb)
5849                                 SCTP_TCB_UNLOCK(stcb);
5850                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
5851                         return (EINVAL);
5852                 }
5853
5854                 if (stcb) {
5855                         /************************TCB SPECIFIC SET ******************/
5856                         /*
5857                          * do we change the timer for HB, we run
5858                          * only one?
5859                          */
5860                         int ovh = 0;
5861
5862                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5863                                 ovh = SCTP_MED_OVERHEAD;
5864                         } else {
5865                                 ovh = SCTP_MED_V4_OVERHEAD;
5866                         }
5867
5868                         /* network sets ? */
5869                         if (net) {
5870                                 /************************NET SPECIFIC SET ******************/
5871                                 if (paddrp->spp_flags & SPP_HB_DISABLE) {
5872                                         if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
5873                                             !(net->dest_state & SCTP_ADDR_NOHB)) {
5874                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5875                                                                 SCTP_FROM_SCTP_USRREQ+SCTP_LOC_10);
5876                                         }
5877                                         net->dest_state |= SCTP_ADDR_NOHB;
5878                                 }
5879                                 if (paddrp->spp_flags & SPP_HB_ENABLE) {
5880                                         if (paddrp->spp_hbinterval) {
5881                                                 net->heart_beat_delay = paddrp->spp_hbinterval;
5882                                         } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5883                                                 net->heart_beat_delay = 0;
5884                                         }
5885                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5886                                                         SCTP_FROM_SCTP_USRREQ+SCTP_LOC_10);
5887                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5888                                         net->dest_state &= ~SCTP_ADDR_NOHB;
5889                                 }
5890                                 if (paddrp->spp_flags & SPP_HB_DEMAND) {
5891                                         /* on demand HB */
5892                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5893                                         sctp_chunk_output(inp, stcb,  SCTP_OUTPUT_FROM_SOCKOPT, SCTP_SO_LOCKED);
5894                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
5895                                 }
5896                                 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
5897                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5898                                                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5899                                                                 SCTP_FROM_SCTP_USRREQ+SCTP_LOC_10);
5900                                         }
5901                                         net->dest_state |= SCTP_ADDR_NO_PMTUD;
5902                                         net->mtu = paddrp->spp_pathmtu + ovh;
5903                                         if (net->mtu < stcb->asoc.smallest_mtu) {
5904                                                 sctp_pathmtu_adjustment(stcb, net->mtu);
5905                                         }
5906                                 }
5907                                 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
5908                                         if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
5909                                                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
5910                                         }
5911                                         net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
5912                                 }
5913                                 if (paddrp->spp_pathmaxrxt) {
5914                                         if (net->dest_state & SCTP_ADDR_PF) {
5915                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5916                                                         net->dest_state &= ~SCTP_ADDR_PF;
5917                                                 }
5918                                         } else {
5919                                                 if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5920                                                     (net->error_count > net->pf_threshold)) {
5921                                                         net->dest_state |= SCTP_ADDR_PF;
5922                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5923                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5924                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5925                                                 }
5926                                         }
5927                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
5928                                                 if (net->error_count > paddrp->spp_pathmaxrxt) {
5929                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
5930                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5931                                                 }
5932                                         } else {
5933                                                 if (net->error_count <= paddrp->spp_pathmaxrxt) {
5934                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
5935                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5936                                                 }
5937                                         }
5938                                         net->failure_threshold = paddrp->spp_pathmaxrxt;
5939                                 }
5940                                 if (paddrp->spp_flags & SPP_DSCP) {
5941                                         net->dscp = paddrp->spp_dscp & 0xfc;
5942                                         net->dscp |= 0x01;
5943                                 }
5944 #ifdef INET6
5945                                 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
5946                                         if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5947                                                 net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
5948                                                 net->flowlabel |= 0x80000000;
5949                                         }
5950                                 }
5951 #endif
5952                         } else {
5953                                 /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
5954                                 if (paddrp->spp_pathmaxrxt) {
5955                                         stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
5956                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5957                                                 if (net->dest_state & SCTP_ADDR_PF) {
5958                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5959                                                                 net->dest_state &= ~SCTP_ADDR_PF;
5960                                                         }
5961                                                 } else {
5962                                                         if ((net->error_count <= paddrp->spp_pathmaxrxt) &&
5963                                                             (net->error_count > net->pf_threshold)) {
5964                                                                 net->dest_state |= SCTP_ADDR_PF;
5965                                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
5966                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
5967                                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5968                                                         }
5969                                                 }
5970                                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
5971                                                         if (net->error_count > paddrp->spp_pathmaxrxt) {
5972                                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5973                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
5974                                                         }
5975                                                 } else {
5976                                                         if (net->error_count <= paddrp->spp_pathmaxrxt) {
5977                                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
5978                                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
5979                                                         }
5980                                                 }
5981                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
5982                                         }
5983                                 }
5984
5985                                 if (paddrp->spp_flags & SPP_HB_ENABLE) {
5986                                         if (paddrp->spp_hbinterval) {
5987                                                 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
5988                                         } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5989                                                 stcb->asoc.heart_beat_delay = 0;
5990                                         }
5991                                         /* Turn back on the timer */
5992                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5993                                                 if (paddrp->spp_hbinterval) {
5994                                                         net->heart_beat_delay = paddrp->spp_hbinterval;
5995                                                 } else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
5996                                                         net->heart_beat_delay = 0;
5997                                                 }
5998                                                 if (net->dest_state & SCTP_ADDR_NOHB) {
5999                                                         net->dest_state &= ~SCTP_ADDR_NOHB;
6000                                                 }
6001                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
6002                                                                 SCTP_FROM_SCTP_USRREQ+SCTP_LOC_10);
6003                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
6004                                         }
6005                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
6006                                 }
6007                                 if (paddrp->spp_flags & SPP_HB_DISABLE) {
6008                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6009                                                 if (!(net->dest_state & SCTP_ADDR_NOHB)) {
6010                                                         net->dest_state |= SCTP_ADDR_NOHB;
6011                                                         if (!(net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
6012                                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_10);
6013                                                         }
6014                                                 }
6015                                         }
6016                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
6017                                 }
6018                                 if ((paddrp->spp_flags & SPP_PMTUD_DISABLE) && (paddrp->spp_pathmtu >= SCTP_SMALLEST_PMTU)) {
6019                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6020                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
6021                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
6022                                                                         SCTP_FROM_SCTP_USRREQ+SCTP_LOC_10);
6023                                                 }
6024                                                 net->dest_state |= SCTP_ADDR_NO_PMTUD;
6025                                                 net->mtu = paddrp->spp_pathmtu + ovh;
6026                                                 if (net->mtu < stcb->asoc.smallest_mtu) {
6027                                                         sctp_pathmtu_adjustment(stcb, net->mtu);
6028                                                 }
6029                                         }
6030                                         sctp_stcb_feature_on(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
6031                                 }
6032                                 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
6033                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6034                                                 if (!SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
6035                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
6036                                                 }
6037                                                 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
6038                                         }
6039                                         sctp_stcb_feature_off(inp, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
6040                                 }
6041                                 if (paddrp->spp_flags & SPP_DSCP) {
6042                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6043                                                 net->dscp = paddrp->spp_dscp & 0xfc;
6044                                                 net->dscp |= 0x01;
6045                                         }
6046                                         stcb->asoc.default_dscp = paddrp->spp_dscp & 0xfc;
6047                                         stcb->asoc.default_dscp |= 0x01;
6048                                 }
6049 #ifdef INET6
6050                                 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
6051                                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6052                                                 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
6053                                                         net->flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
6054                                                         net->flowlabel |= 0x80000000;
6055                                                 }
6056                                         }
6057                                         stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
6058                                         stcb->asoc.default_flowlabel |= 0x80000000;
6059                                 }
6060 #endif
6061                         }
6062                         SCTP_TCB_UNLOCK(stcb);
6063                 } else {
6064                         /************************NO TCB, SET TO default stuff ******************/
6065                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6066                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6067                             (paddrp->spp_assoc_id == SCTP_FUTURE_ASSOC)) {
6068                                 SCTP_INP_WLOCK(inp);
6069                                 /*
6070                                  * For the TOS/FLOWLABEL stuff you set it
6071                                  * with the options on the socket
6072                                  */
6073                                 if (paddrp->spp_pathmaxrxt) {
6074                                         inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
6075                                 }
6076
6077                                 if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
6078                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
6079                                 else if (paddrp->spp_hbinterval) {
6080                                         if (paddrp->spp_hbinterval > SCTP_MAX_HB_INTERVAL)
6081                                                 paddrp->spp_hbinterval= SCTP_MAX_HB_INTERVAL;
6082                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
6083                                 }
6084
6085                                 if (paddrp->spp_flags & SPP_HB_ENABLE) {
6086                                         if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO) {
6087                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = 0;
6088                                         } else if (paddrp->spp_hbinterval) {
6089                                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
6090                                         }
6091                                         sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
6092                                 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
6093                                         sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
6094                                 }
6095                                 if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
6096                                         sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
6097                                 } else if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
6098                                         sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_NOT_PMTUD);
6099                                 }
6100                                 if (paddrp->spp_flags & SPP_DSCP) {
6101                                         inp->sctp_ep.default_dscp = paddrp->spp_dscp & 0xfc;
6102                                         inp->sctp_ep.default_dscp |= 0x01;
6103                                 }
6104 #ifdef INET6
6105                                 if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
6106                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6107                                                 inp->sctp_ep.default_flowlabel = paddrp->spp_ipv6_flowlabel & 0x000fffff;
6108                                                 inp->sctp_ep.default_flowlabel |= 0x80000000;
6109                                         }
6110                                 }
6111 #endif
6112                                 SCTP_INP_WUNLOCK(inp);
6113                         } else {
6114                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6115                                 error = EINVAL;
6116                         }
6117                 }
6118                 break;
6119         }
6120         case SCTP_RTOINFO:
6121         {
6122                 struct sctp_rtoinfo *srto;
6123                 uint32_t new_init, new_min, new_max;
6124
6125                 SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
6126                 SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
6127
6128                 if (stcb) {
6129                         if (srto->srto_initial)
6130                                 new_init = srto->srto_initial;
6131                         else
6132                                 new_init = stcb->asoc.initial_rto;
6133                         if (srto->srto_max)
6134                                 new_max = srto->srto_max;
6135                         else
6136                                 new_max = stcb->asoc.maxrto;
6137                         if (srto->srto_min)
6138                                 new_min = srto->srto_min;
6139                         else
6140                                 new_min = stcb->asoc.minrto;
6141                         if ((new_min <= new_init) && (new_init <= new_max)) {
6142                                 stcb->asoc.initial_rto = new_init;
6143                                 stcb->asoc.maxrto = new_max;
6144                                 stcb->asoc.minrto = new_min;
6145                         } else {
6146                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6147                                 error = EINVAL;
6148                         }
6149                         SCTP_TCB_UNLOCK(stcb);
6150                 } else {
6151                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6152                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6153                             (srto->srto_assoc_id == SCTP_FUTURE_ASSOC)) {
6154                                 SCTP_INP_WLOCK(inp);
6155                                 if (srto->srto_initial)
6156                                         new_init = srto->srto_initial;
6157                                 else
6158                                         new_init = inp->sctp_ep.initial_rto;
6159                                 if (srto->srto_max)
6160                                         new_max = srto->srto_max;
6161                                 else
6162                                         new_max = inp->sctp_ep.sctp_maxrto;
6163                                 if (srto->srto_min)
6164                                         new_min = srto->srto_min;
6165                                 else
6166                                         new_min = inp->sctp_ep.sctp_minrto;
6167                                 if ((new_min <= new_init) && (new_init <= new_max)) {
6168                                         inp->sctp_ep.initial_rto = new_init;
6169                                         inp->sctp_ep.sctp_maxrto = new_max;
6170                                         inp->sctp_ep.sctp_minrto = new_min;
6171                                 } else {
6172                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6173                                         error = EINVAL;
6174                                 }
6175                                 SCTP_INP_WUNLOCK(inp);
6176                         } else {
6177                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6178                                 error = EINVAL;
6179                         }
6180                 }
6181                 break;
6182         }
6183         case SCTP_ASSOCINFO:
6184         {
6185                 struct sctp_assocparams *sasoc;
6186
6187                 SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
6188                 SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
6189                 if (sasoc->sasoc_cookie_life) {
6190                         /* boundary check the cookie life */
6191                         if (sasoc->sasoc_cookie_life < 1000)
6192                                 sasoc->sasoc_cookie_life = 1000;
6193                         if (sasoc->sasoc_cookie_life > SCTP_MAX_COOKIE_LIFE) {
6194                                 sasoc->sasoc_cookie_life = SCTP_MAX_COOKIE_LIFE;
6195                         }
6196                 }
6197                 if (stcb) {
6198                         if (sasoc->sasoc_asocmaxrxt)
6199                                 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
6200                         if (sasoc->sasoc_cookie_life) {
6201                                 stcb->asoc.cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
6202                         }
6203                         SCTP_TCB_UNLOCK(stcb);
6204                 } else {
6205                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6206                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6207                             (sasoc->sasoc_assoc_id == SCTP_FUTURE_ASSOC)) {
6208                                 SCTP_INP_WLOCK(inp);
6209                                 if (sasoc->sasoc_asocmaxrxt)
6210                                         inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
6211                                 if (sasoc->sasoc_cookie_life) {
6212                                         inp->sctp_ep.def_cookie_life = MSEC_TO_TICKS(sasoc->sasoc_cookie_life);
6213                                 }
6214                                 SCTP_INP_WUNLOCK(inp);
6215                         } else {
6216                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6217                                 error = EINVAL;
6218                         }
6219                 }
6220                 break;
6221         }
6222         case SCTP_INITMSG:
6223         {
6224                 struct sctp_initmsg *sinit;
6225
6226                 SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
6227                 SCTP_INP_WLOCK(inp);
6228                 if (sinit->sinit_num_ostreams)
6229                         inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
6230
6231                 if (sinit->sinit_max_instreams)
6232                         inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
6233
6234                 if (sinit->sinit_max_attempts)
6235                         inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
6236
6237                 if (sinit->sinit_max_init_timeo)
6238                         inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
6239                 SCTP_INP_WUNLOCK(inp);
6240                 break;
6241         }
6242         case SCTP_PRIMARY_ADDR:
6243         {
6244                 struct sctp_setprim *spa;
6245                 struct sctp_nets *net;
6246
6247                 SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
6248                 SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
6249
6250                 net = NULL;
6251                 if (stcb) {
6252                         net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
6253                 } else {
6254                         /* We increment here since sctp_findassociation_ep_addr() wil
6255                          * do a decrement if it finds the stcb as long as the locked
6256                          * tcb (last argument) is NOT a TCB.. aka NULL.
6257                          */
6258                         SCTP_INP_INCR_REF(inp);
6259                         stcb = sctp_findassociation_ep_addr(&inp,
6260                                                             (struct sockaddr *)&spa->ssp_addr,
6261                                                             &net, NULL, NULL);
6262                         if (stcb == NULL) {
6263                                 SCTP_INP_DECR_REF(inp);
6264                         }
6265                 }
6266
6267                 if ((stcb) && (net)) {
6268                         if ((net != stcb->asoc.primary_destination) &&
6269                             (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
6270                                 /* Ok we need to set it */
6271                                 if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
6272                                         if ((stcb->asoc.alternate) &&
6273                                             (!(net->dest_state & SCTP_ADDR_PF)) &&
6274                                             (net->dest_state & SCTP_ADDR_REACHABLE)) {
6275                                                 sctp_free_remote_addr(stcb->asoc.alternate);
6276                                                 stcb->asoc.alternate = NULL;
6277                                         }
6278                                 }
6279                         }
6280                 } else {
6281                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6282                         error = EINVAL;
6283                 }
6284                 if (stcb) {
6285                         SCTP_TCB_UNLOCK(stcb);
6286                 }
6287                 break;
6288         }
6289         case SCTP_SET_DYNAMIC_PRIMARY:
6290         {
6291                 union sctp_sockstore *ss;
6292 #ifdef SCTP_MVRF
6293                 int i, fnd = 0;
6294 #endif
6295 #if !defined(__Windows__) && !defined(__Userspace__)
6296 #if defined(__APPLE__)
6297                 struct proc *proc;
6298 #endif
6299 #ifdef __FreeBSD__
6300 #if __FreeBSD_version > 602000
6301                 error = priv_check(curthread,
6302                                    PRIV_NETINET_RESERVEDPORT);
6303 #elif __FreeBSD_version >= 500000
6304                 error = suser((struct thread *)p);
6305 #else
6306                 error = suser(p);
6307 #endif
6308 #elif defined(__APPLE__)
6309                 proc = (struct proc *)p;
6310                 if (p) {
6311                         error = suser(proc->p_ucred, &proc->p_acflag);
6312                 } else {
6313                         break;
6314                 }
6315 #else
6316                 error = suser(p, 0);
6317 #endif
6318 #endif
6319                 if (error)
6320                         break;
6321
6322                 SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
6323                 /* SUPER USER CHECK? */
6324 #ifdef SCTP_MVRF
6325                 for (i = 0; i < inp->num_vrfs; i++) {
6326                         if (vrf_id == inp->m_vrf_ids[i]) {
6327                                 fnd = 1;
6328                                 break;
6329                         }
6330                 }
6331                 if (!fnd) {
6332                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6333                         error = EINVAL;
6334                         break;
6335                 }
6336 #endif
6337                 error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
6338                 break;
6339         }
6340         case SCTP_SET_PEER_PRIMARY_ADDR:
6341         {
6342                 struct sctp_setpeerprim *sspp;
6343
6344                 SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
6345                 SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
6346                 if (stcb != NULL) {
6347                         struct sctp_ifa *ifa;
6348                         ifa = sctp_find_ifa_by_addr((struct sockaddr *)&sspp->sspp_addr,
6349                                                     stcb->asoc.vrf_id, SCTP_ADDR_NOT_LOCKED);
6350                         if (ifa == NULL) {
6351                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6352                                 error = EINVAL;
6353                                 goto out_of_it;
6354                         }
6355                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
6356                                 /* Must validate the ifa found is in our ep */
6357                                 struct sctp_laddr *laddr;
6358                                 int found = 0;
6359                                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6360                                         if (laddr->ifa == NULL) {
6361                                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
6362                                                         __FUNCTION__);
6363                                                 continue;
6364                                         }
6365                                         if (laddr->ifa == ifa) {
6366                                                 found = 1;
6367                                                 break;
6368                                         }
6369                                 }
6370                                 if (!found) {
6371                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6372                                         error = EINVAL;
6373                                         goto out_of_it;
6374                                 }
6375 #if defined(__FreeBSD__)
6376                         } else {
6377                                 switch (sspp->sspp_addr.ss_family) {
6378 #ifdef INET
6379                                 case AF_INET:
6380                                 {
6381                                         struct sockaddr_in *sin;
6382
6383                                         sin = (struct sockaddr_in *)&sspp->sspp_addr;
6384                                         if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
6385                                                              &sin->sin_addr) != 0) {
6386                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6387                                                 error = EINVAL;
6388                                                 goto out_of_it;
6389                                         }
6390                                         break;
6391                                 }
6392 #endif
6393 #ifdef INET6
6394                                 case AF_INET6:
6395                                 {
6396                                         struct sockaddr_in6 *sin6;
6397
6398                                         sin6 = (struct sockaddr_in6 *)&sspp->sspp_addr;
6399                                         if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
6400                                                              &sin6->sin6_addr) != 0) {
6401                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6402                                                 error = EINVAL;
6403                                                 goto out_of_it;
6404                                         }
6405                                         break;
6406                                 }
6407 #endif
6408                                 default:
6409                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6410                                         error = EINVAL;
6411                                         goto out_of_it;
6412                                 }
6413 #endif
6414                         }
6415                         if (sctp_set_primary_ip_address_sa(stcb,
6416                                                            (struct sockaddr *)&sspp->sspp_addr) != 0) {
6417                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6418                                 error = EINVAL;
6419                         }
6420                 out_of_it:
6421                         SCTP_TCB_UNLOCK(stcb);
6422                 } else {
6423                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6424                         error = EINVAL;
6425                 }
6426                 break;
6427         }
6428         case SCTP_BINDX_ADD_ADDR:
6429         {
6430                 struct sctp_getaddresses *addrs;
6431 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
6432                 struct thread *td;
6433
6434                 td = (struct thread *)p;
6435 #endif
6436                 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses,
6437                                     optsize);
6438 #ifdef INET
6439                 if (addrs->addr->sa_family == AF_INET) {
6440                         if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
6441                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6442                                 error = EINVAL;
6443                                 break;
6444                         }
6445 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
6446                         if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
6447                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6448                                 break;
6449                         }
6450 #endif
6451                 } else
6452 #endif
6453 #ifdef INET6
6454                 if (addrs->addr->sa_family == AF_INET6) {
6455                         if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
6456                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6457                                 error = EINVAL;
6458                                 break;
6459                         }
6460 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
6461                         if (td != NULL && (error = prison_local_ip6(td->td_ucred, &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
6462                                                                                            (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
6463                           SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6464                           break;
6465                         }
6466 #endif
6467                 } else
6468 #endif
6469                 {
6470                        error = EAFNOSUPPORT;
6471                        break;
6472                 }
6473                 sctp_bindx_add_address(so, inp, addrs->addr,
6474                                        addrs->sget_assoc_id, vrf_id,
6475                                        &error, p);
6476                 break;
6477         }
6478         case SCTP_BINDX_REM_ADDR:
6479         {
6480                 struct sctp_getaddresses *addrs;
6481 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
6482                 struct thread *td;
6483                 td = (struct thread *)p;
6484
6485 #endif
6486                 SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
6487 #ifdef INET
6488                 if (addrs->addr->sa_family == AF_INET) {
6489                         if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in)) {
6490                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6491                                 error = EINVAL;
6492                                 break;
6493                         }
6494 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
6495                 if (td != NULL && (error = prison_local_ip4(td->td_ucred, &(((struct sockaddr_in *)(addrs->addr))->sin_addr)))) {
6496                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6497                                 break;
6498                         }
6499 #endif
6500                 } else
6501 #endif
6502 #ifdef INET6
6503                 if (addrs->addr->sa_family == AF_INET6) {
6504                         if (optsize < sizeof(struct sctp_getaddresses) - sizeof(struct sockaddr) + sizeof(struct sockaddr_in6)) {
6505                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6506                                 error = EINVAL;
6507                                 break;
6508                         }
6509 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
6510                         if (td != NULL &&
6511                             (error = prison_local_ip6(td->td_ucred,
6512                                                       &(((struct sockaddr_in6 *)(addrs->addr))->sin6_addr),
6513                                                       (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
6514                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, error);
6515                                 break;
6516                         }
6517 #endif
6518                 } else
6519 #endif
6520                 {
6521                         error = EAFNOSUPPORT;
6522                         break;
6523                 }
6524                 sctp_bindx_delete_address(inp, addrs->addr,
6525                                           addrs->sget_assoc_id, vrf_id,
6526                                           &error);
6527                 break;
6528         }
6529 #ifdef __APPLE__
6530         case SCTP_LISTEN_FIX:
6531                 /* only applies to one-to-many sockets */
6532                 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
6533                         /* make sure the ACCEPTCONN flag is OFF */
6534                         so->so_options &= ~SO_ACCEPTCONN;
6535                 } else {
6536                         /* otherwise, not allowed */
6537                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6538                         error = EINVAL;
6539                 }
6540                 break;
6541 #endif                          /* __APPLE__ */
6542         case SCTP_EVENT:
6543         {
6544                 struct sctp_event *event;
6545                 uint32_t event_type;
6546
6547                 SCTP_CHECK_AND_CAST(event, optval, struct sctp_event, optsize);
6548                 SCTP_FIND_STCB(inp, stcb, event->se_assoc_id);
6549                 switch (event->se_type) {
6550                 case SCTP_ASSOC_CHANGE:
6551                         event_type = SCTP_PCB_FLAGS_RECVASSOCEVNT;
6552                         break;
6553                 case SCTP_PEER_ADDR_CHANGE:
6554                         event_type = SCTP_PCB_FLAGS_RECVPADDREVNT;
6555                         break;
6556                 case SCTP_REMOTE_ERROR:
6557                         event_type = SCTP_PCB_FLAGS_RECVPEERERR;
6558                         break;
6559                 case SCTP_SEND_FAILED:
6560                         event_type = SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
6561                         break;
6562                 case SCTP_SHUTDOWN_EVENT:
6563                         event_type = SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
6564                         break;
6565                 case SCTP_ADAPTATION_INDICATION:
6566                         event_type = SCTP_PCB_FLAGS_ADAPTATIONEVNT;
6567                         break;
6568                 case SCTP_PARTIAL_DELIVERY_EVENT:
6569                         event_type = SCTP_PCB_FLAGS_PDAPIEVNT;
6570                         break;
6571                 case SCTP_AUTHENTICATION_EVENT:
6572                         event_type = SCTP_PCB_FLAGS_AUTHEVNT;
6573                         break;
6574                 case SCTP_STREAM_RESET_EVENT:
6575                         event_type = SCTP_PCB_FLAGS_STREAM_RESETEVNT;
6576                         break;
6577                 case SCTP_SENDER_DRY_EVENT:
6578                         event_type = SCTP_PCB_FLAGS_DRYEVNT;
6579                         break;
6580                 case SCTP_NOTIFICATIONS_STOPPED_EVENT:
6581                         event_type = 0;
6582                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6583                         error = ENOTSUP;
6584                         break;
6585                 case SCTP_ASSOC_RESET_EVENT:
6586                         event_type = SCTP_PCB_FLAGS_ASSOC_RESETEVNT;
6587                         break;
6588                 case SCTP_STREAM_CHANGE_EVENT:
6589                         event_type = SCTP_PCB_FLAGS_STREAM_CHANGEEVNT;
6590                         break;
6591                 case SCTP_SEND_FAILED_EVENT:
6592                         event_type = SCTP_PCB_FLAGS_RECVNSENDFAILEVNT;
6593                         break;
6594                 default:
6595                         event_type = 0;
6596                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6597                         error = EINVAL;
6598                         break;
6599                 }
6600                 if (event_type > 0) {
6601                         if (stcb) {
6602                                 if (event->se_on) {
6603                                         sctp_stcb_feature_on(inp, stcb, event_type);
6604                                         if (event_type == SCTP_PCB_FLAGS_DRYEVNT) {
6605                                                 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
6606                                                     TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
6607                                                     (stcb->asoc.stream_queue_cnt == 0)) {
6608                                                         sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb,  0, NULL, SCTP_SO_LOCKED);
6609                                                 }
6610                                         }
6611                                 } else {
6612                                         sctp_stcb_feature_off(inp, stcb, event_type);
6613                                 }
6614                                 SCTP_TCB_UNLOCK(stcb);
6615                         } else {
6616                                 /*
6617                                  * We don't want to send up a storm of events,
6618                                  * so return an error for sender dry events
6619                                  */
6620                                 if ((event_type == SCTP_PCB_FLAGS_DRYEVNT) &&
6621                                     ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) == 0) &&
6622                                     ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) &&
6623                                     ((event->se_assoc_id == SCTP_ALL_ASSOC) ||
6624                                      (event->se_assoc_id == SCTP_CURRENT_ASSOC))) {
6625                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTSUP);
6626                                         error = ENOTSUP;
6627                                         break;
6628                                 }
6629                                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6630                                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6631                                     (event->se_assoc_id == SCTP_FUTURE_ASSOC) ||
6632                                     (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6633                                         SCTP_INP_WLOCK(inp);
6634                                         if (event->se_on) {
6635                                                 sctp_feature_on(inp, event_type);
6636                                         } else {
6637                                                 sctp_feature_off(inp, event_type);
6638                                         }
6639                                         SCTP_INP_WUNLOCK(inp);
6640                                 }
6641                                 if ((event->se_assoc_id == SCTP_CURRENT_ASSOC) ||
6642                                     (event->se_assoc_id == SCTP_ALL_ASSOC)) {
6643                                         SCTP_INP_RLOCK(inp);
6644                                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6645                                                 SCTP_TCB_LOCK(stcb);
6646                                                 if (event->se_on) {
6647                                                         sctp_stcb_feature_on(inp, stcb, event_type);
6648                                                 } else {
6649                                                         sctp_stcb_feature_off(inp, stcb, event_type);
6650                                                 }
6651                                                 SCTP_TCB_UNLOCK(stcb);
6652                                         }
6653                                         SCTP_INP_RUNLOCK(inp);
6654                                 }
6655                         }
6656                 }
6657                 break;
6658         }
6659         case SCTP_RECVRCVINFO:
6660         {
6661                 int *onoff;
6662
6663                 SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6664                 SCTP_INP_WLOCK(inp);
6665                 if (*onoff != 0) {
6666                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6667                 } else {
6668                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO);
6669                 }
6670                 SCTP_INP_WUNLOCK(inp);
6671                 break;
6672         }
6673         case SCTP_RECVNXTINFO:
6674         {
6675                 int *onoff;
6676
6677                 SCTP_CHECK_AND_CAST(onoff, optval, int, optsize);
6678                 SCTP_INP_WLOCK(inp);
6679                 if (*onoff != 0) {
6680                         sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6681                 } else {
6682                         sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO);
6683                 }
6684                 SCTP_INP_WUNLOCK(inp);
6685                 break;
6686         }
6687         case SCTP_DEFAULT_SNDINFO:
6688         {
6689                 struct sctp_sndinfo *info;
6690                 uint16_t policy;
6691
6692                 SCTP_CHECK_AND_CAST(info, optval, struct sctp_sndinfo, optsize);
6693                 SCTP_FIND_STCB(inp, stcb, info->snd_assoc_id);
6694
6695                 if (stcb) {
6696                         if (info->snd_sid < stcb->asoc.streamoutcnt) {
6697                                 stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6698                                 policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6699                                 stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6700                                 stcb->asoc.def_send.sinfo_flags |= policy;
6701                                 stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6702                                 stcb->asoc.def_send.sinfo_context = info->snd_context;
6703                         } else {
6704                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6705                                 error = EINVAL;
6706                         }
6707                         SCTP_TCB_UNLOCK(stcb);
6708                 } else {
6709                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6710                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6711                             (info->snd_assoc_id == SCTP_FUTURE_ASSOC) ||
6712                             (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6713                                 SCTP_INP_WLOCK(inp);
6714                                 inp->def_send.sinfo_stream = info->snd_sid;
6715                                 policy = PR_SCTP_POLICY(inp->def_send.sinfo_flags);
6716                                 inp->def_send.sinfo_flags = info->snd_flags;
6717                                 inp->def_send.sinfo_flags |= policy;
6718                                 inp->def_send.sinfo_ppid = info->snd_ppid;
6719                                 inp->def_send.sinfo_context = info->snd_context;
6720                                 SCTP_INP_WUNLOCK(inp);
6721                         }
6722                         if ((info->snd_assoc_id == SCTP_CURRENT_ASSOC) ||
6723                             (info->snd_assoc_id == SCTP_ALL_ASSOC)) {
6724                                 SCTP_INP_RLOCK(inp);
6725                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6726                                         SCTP_TCB_LOCK(stcb);
6727                                         if (info->snd_sid < stcb->asoc.streamoutcnt) {
6728                                                 stcb->asoc.def_send.sinfo_stream = info->snd_sid;
6729                                                 policy = PR_SCTP_POLICY(stcb->asoc.def_send.sinfo_flags);
6730                                                 stcb->asoc.def_send.sinfo_flags = info->snd_flags;
6731                                                 stcb->asoc.def_send.sinfo_flags |= policy;
6732                                                 stcb->asoc.def_send.sinfo_ppid = info->snd_ppid;
6733                                                 stcb->asoc.def_send.sinfo_context = info->snd_context;
6734                                         }
6735                                         SCTP_TCB_UNLOCK(stcb);
6736                                 }
6737                                 SCTP_INP_RUNLOCK(inp);
6738                         }
6739                 }
6740                 break;
6741         }
6742         case SCTP_DEFAULT_PRINFO:
6743         {
6744                 struct sctp_default_prinfo *info;
6745
6746                 SCTP_CHECK_AND_CAST(info, optval, struct sctp_default_prinfo, optsize);
6747                 SCTP_FIND_STCB(inp, stcb, info->pr_assoc_id);
6748
6749                 if (PR_SCTP_INVALID_POLICY(info->pr_policy)) {
6750                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6751                         error = EINVAL;
6752                         break;
6753                 }
6754                 if (stcb) {
6755                         stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6756                         stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6757                         stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6758                         SCTP_TCB_UNLOCK(stcb);
6759                 } else {
6760                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6761                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6762                             (info->pr_assoc_id == SCTP_FUTURE_ASSOC) ||
6763                             (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6764                                 SCTP_INP_WLOCK(inp);
6765                                 inp->def_send.sinfo_flags &= 0xfff0;
6766                                 inp->def_send.sinfo_flags |= info->pr_policy;
6767                                 inp->def_send.sinfo_timetolive = info->pr_value;
6768                                 SCTP_INP_WUNLOCK(inp);
6769                         }
6770                         if ((info->pr_assoc_id == SCTP_CURRENT_ASSOC) ||
6771                             (info->pr_assoc_id == SCTP_ALL_ASSOC)) {
6772                                 SCTP_INP_RLOCK(inp);
6773                                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6774                                         SCTP_TCB_LOCK(stcb);
6775                                         stcb->asoc.def_send.sinfo_flags &= 0xfff0;
6776                                         stcb->asoc.def_send.sinfo_flags |= info->pr_policy;
6777                                         stcb->asoc.def_send.sinfo_timetolive = info->pr_value;
6778                                         SCTP_TCB_UNLOCK(stcb);
6779                                 }
6780                                 SCTP_INP_RUNLOCK(inp);
6781                         }
6782                 }
6783                 break;
6784         }
6785         case SCTP_PEER_ADDR_THLDS:
6786                 /* Applies to the specific association */
6787         {
6788                 struct sctp_paddrthlds *thlds;
6789                 struct sctp_nets *net;
6790
6791                 SCTP_CHECK_AND_CAST(thlds, optval, struct sctp_paddrthlds, optsize);
6792                 SCTP_FIND_STCB(inp, stcb, thlds->spt_assoc_id);
6793                 net = NULL;
6794                 if (stcb) {
6795                         net = sctp_findnet(stcb, (struct sockaddr *)&thlds->spt_address);
6796                 } else {
6797                         /* We increment here since sctp_findassociation_ep_addr() wil
6798                          * do a decrement if it finds the stcb as long as the locked
6799                          * tcb (last argument) is NOT a TCB.. aka NULL.
6800                          */
6801                         SCTP_INP_INCR_REF(inp);
6802                         stcb = sctp_findassociation_ep_addr(&inp,
6803                                                             (struct sockaddr *)&thlds->spt_address,
6804                                                             &net, NULL, NULL);
6805                         if (stcb == NULL) {
6806                                 SCTP_INP_DECR_REF(inp);
6807                         }
6808                 }
6809                 if (stcb && (net == NULL)) {
6810                         struct sockaddr *sa;
6811
6812                         sa = (struct sockaddr *)&thlds->spt_address;
6813 #ifdef INET
6814                         if (sa->sa_family == AF_INET) {
6815
6816                                 struct sockaddr_in *sin;
6817                                 sin = (struct sockaddr_in *)sa;
6818                                 if (sin->sin_addr.s_addr) {
6819                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6820                                         SCTP_TCB_UNLOCK(stcb);
6821                                         error = EINVAL;
6822                                         break;
6823                                 }
6824                         } else
6825 #endif
6826 #ifdef INET6
6827                         if (sa->sa_family == AF_INET6) {
6828                                 struct sockaddr_in6 *sin6;
6829
6830                                 sin6 = (struct sockaddr_in6 *)sa;
6831                                 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6832                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6833                                         SCTP_TCB_UNLOCK(stcb);
6834                                         error = EINVAL;
6835                                         break;
6836                                 }
6837                         } else
6838 #endif
6839 #if defined(__Userspace__)
6840                         if (sa->sa_family == AF_CONN) {
6841                                 struct sockaddr_conn *sconn;
6842
6843                                 sconn = (struct sockaddr_conn *)sa;
6844                                 if (sconn->sconn_addr != NULL) {
6845                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6846                                         SCTP_TCB_UNLOCK(stcb);
6847                                         error = EINVAL;
6848                                         break;
6849                                 }
6850                         } else
6851 #endif
6852                         {
6853                                 error = EAFNOSUPPORT;
6854                                 SCTP_TCB_UNLOCK(stcb);
6855                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
6856                                 break;
6857                         }
6858                 }
6859                 if (stcb) {
6860                         if (net) {
6861                                 if (net->dest_state & SCTP_ADDR_PF) {
6862                                         if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
6863                                             (net->failure_threshold <= thlds->spt_pathpfthld)) {
6864                                                 net->dest_state &= ~SCTP_ADDR_PF;
6865                                         }
6866                                 } else {
6867                                         if ((net->failure_threshold > thlds->spt_pathpfthld) &&
6868                                             (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
6869                                                 net->dest_state |= SCTP_ADDR_PF;
6870                                                 sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6871                                                 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
6872                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6873                                         }
6874                                 }
6875                                 if (net->dest_state & SCTP_ADDR_REACHABLE) {
6876                                         if (net->failure_threshold > thlds->spt_pathmaxrxt) {
6877                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
6878                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6879                                         }
6880                                 } else {
6881                                         if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
6882                                                 net->dest_state |= SCTP_ADDR_REACHABLE;
6883                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6884                                         }
6885                                 }
6886                                 net->failure_threshold = thlds->spt_pathmaxrxt;
6887                                 net->pf_threshold = thlds->spt_pathpfthld;
6888                         } else {
6889                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6890                                         if (net->dest_state & SCTP_ADDR_PF) {
6891                                                 if ((net->failure_threshold > thlds->spt_pathmaxrxt) ||
6892                                                     (net->failure_threshold <= thlds->spt_pathpfthld)) {
6893                                                         net->dest_state &= ~SCTP_ADDR_PF;
6894                                                 }
6895                                         } else {
6896                                                 if ((net->failure_threshold > thlds->spt_pathpfthld) &&
6897                                                     (net->failure_threshold <= thlds->spt_pathmaxrxt)) {
6898                                                         net->dest_state |= SCTP_ADDR_PF;
6899                                                         sctp_send_hb(stcb, net, SCTP_SO_LOCKED);
6900                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_TIMER + SCTP_LOC_3);
6901                                                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
6902                                                 }
6903                                         }
6904                                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
6905                                                 if (net->failure_threshold > thlds->spt_pathmaxrxt) {
6906                                                         net->dest_state &= ~SCTP_ADDR_REACHABLE;
6907                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN, stcb, 0, net, SCTP_SO_LOCKED);
6908                                                 }
6909                                         } else {
6910                                                 if (net->failure_threshold <= thlds->spt_pathmaxrxt) {
6911                                                         net->dest_state |= SCTP_ADDR_REACHABLE;
6912                                                         sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 0, net, SCTP_SO_LOCKED);
6913                                                 }
6914                                         }
6915                                         net->failure_threshold = thlds->spt_pathmaxrxt;
6916                                         net->pf_threshold = thlds->spt_pathpfthld;
6917                                 }
6918                                 stcb->asoc.def_net_failure = thlds->spt_pathmaxrxt;
6919                                 stcb->asoc.def_net_pf_threshold = thlds->spt_pathpfthld;
6920                         }
6921                 } else {
6922                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6923                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
6924                             (thlds->spt_assoc_id == SCTP_FUTURE_ASSOC)) {
6925                                 SCTP_INP_WLOCK(inp);
6926                                 inp->sctp_ep.def_net_failure = thlds->spt_pathmaxrxt;
6927                                 inp->sctp_ep.def_net_pf_threshold = thlds->spt_pathpfthld;
6928                                 SCTP_INP_WUNLOCK(inp);
6929                         } else {
6930                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6931                                 error = EINVAL;
6932                         }
6933                 }
6934                 break;
6935         }
6936         case SCTP_REMOTE_UDP_ENCAPS_PORT:
6937         {
6938                 struct sctp_udpencaps *encaps;
6939                 struct sctp_nets *net;
6940
6941                 SCTP_CHECK_AND_CAST(encaps, optval, struct sctp_udpencaps, optsize);
6942                 SCTP_FIND_STCB(inp, stcb, encaps->sue_assoc_id);
6943                 if (stcb) {
6944                         net = sctp_findnet(stcb, (struct sockaddr *)&encaps->sue_address);
6945                 } else {
6946                         /* We increment here since sctp_findassociation_ep_addr() wil
6947                          * do a decrement if it finds the stcb as long as the locked
6948                          * tcb (last argument) is NOT a TCB.. aka NULL.
6949                          */
6950                         net = NULL;
6951                         SCTP_INP_INCR_REF(inp);
6952                         stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&encaps->sue_address, &net, NULL, NULL);
6953                         if (stcb == NULL) {
6954                                 SCTP_INP_DECR_REF(inp);
6955                         }
6956                 }
6957                 if (stcb && (net == NULL)) {
6958                         struct sockaddr *sa;
6959
6960                         sa = (struct sockaddr *)&encaps->sue_address;
6961 #ifdef INET
6962                         if (sa->sa_family == AF_INET) {
6963
6964                                 struct sockaddr_in *sin;
6965                                 sin = (struct sockaddr_in *)sa;
6966                                 if (sin->sin_addr.s_addr) {
6967                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6968                                         SCTP_TCB_UNLOCK(stcb);
6969                                         error = EINVAL;
6970                                         break;
6971                                 }
6972                         } else
6973 #endif
6974 #ifdef INET6
6975                         if (sa->sa_family == AF_INET6) {
6976                                 struct sockaddr_in6 *sin6;
6977
6978                                 sin6 = (struct sockaddr_in6 *)sa;
6979                                 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
6980                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6981                                         SCTP_TCB_UNLOCK(stcb);
6982                                         error = EINVAL;
6983                                         break;
6984                                 }
6985                         } else
6986 #endif
6987 #if defined(__Userspace__)
6988                         if (sa->sa_family == AF_CONN) {
6989                                 struct sockaddr_conn *sconn;
6990
6991                                 sconn = (struct sockaddr_conn *)sa;
6992                                 if (sconn->sconn_addr != NULL) {
6993                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
6994                                         SCTP_TCB_UNLOCK(stcb);
6995                                         error = EINVAL;
6996                                         break;
6997                                 }
6998                         } else
6999 #endif
7000                         {
7001                                         error = EAFNOSUPPORT;
7002                                         SCTP_TCB_UNLOCK(stcb);
7003                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
7004                                         break;
7005                                 }
7006                 }
7007
7008                 if (stcb) {
7009                         if (net) {
7010                                 net->port = encaps->sue_port;
7011                         } else {
7012                                 stcb->asoc.port = encaps->sue_port;
7013                         }
7014                         SCTP_TCB_UNLOCK(stcb);
7015                 } else {
7016                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7017                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7018                             (encaps->sue_assoc_id == SCTP_FUTURE_ASSOC)) {
7019                                 SCTP_INP_WLOCK(inp);
7020                                 inp->sctp_ep.port = encaps->sue_port;
7021                                 SCTP_INP_WUNLOCK(inp);
7022                         } else {
7023                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7024                                 error = EINVAL;
7025                         }
7026                 }
7027                 break;
7028         }
7029         case SCTP_ECN_SUPPORTED:
7030         {
7031                 struct sctp_assoc_value *av;
7032
7033                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
7034                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
7035
7036                 if (stcb) {
7037                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7038                         error = EINVAL;
7039                         SCTP_TCB_UNLOCK(stcb);
7040                 } else {
7041                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7042                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7043                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
7044                                 SCTP_INP_WLOCK(inp);
7045                                 if (av->assoc_value == 0) {
7046                                         inp->ecn_supported = 0;
7047                                 } else {
7048                                         inp->ecn_supported = 1;
7049                                 }
7050                                 SCTP_INP_WUNLOCK(inp);
7051                         } else {
7052                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7053                                 error = EINVAL;
7054                         }
7055                 }
7056                 break;
7057         }
7058         case SCTP_PR_SUPPORTED:
7059         {
7060                 struct sctp_assoc_value *av;
7061
7062                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
7063                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
7064
7065                 if (stcb) {
7066                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7067                         error = EINVAL;
7068                         SCTP_TCB_UNLOCK(stcb);
7069                 } else {
7070                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7071                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7072                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
7073                                 SCTP_INP_WLOCK(inp);
7074                                 if (av->assoc_value == 0) {
7075                                         inp->prsctp_supported = 0;
7076                                 } else {
7077                                         inp->prsctp_supported = 1;
7078                                 }
7079                                 SCTP_INP_WUNLOCK(inp);
7080                         } else {
7081                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7082                                 error = EINVAL;
7083                         }
7084                 }
7085                 break;
7086         }
7087         case SCTP_AUTH_SUPPORTED:
7088         {
7089                 struct sctp_assoc_value *av;
7090
7091                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
7092                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
7093
7094                 if (stcb) {
7095                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7096                         error = EINVAL;
7097                         SCTP_TCB_UNLOCK(stcb);
7098                 } else {
7099                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7100                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7101                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
7102                                 if ((av->assoc_value == 0) &&
7103                                     (inp->asconf_supported == 1)) {
7104                                         /* AUTH is required for ASCONF */
7105                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7106                                         error = EINVAL;
7107                                 } else {
7108                                         SCTP_INP_WLOCK(inp);
7109                                         if (av->assoc_value == 0) {
7110                                                 inp->auth_supported = 0;
7111                                         } else {
7112                                                 inp->auth_supported = 1;
7113                                         }
7114                                         SCTP_INP_WUNLOCK(inp);
7115                                 }
7116                         } else {
7117                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7118                                 error = EINVAL;
7119                         }
7120                 }
7121                 break;
7122         }
7123         case SCTP_ASCONF_SUPPORTED:
7124         {
7125                 struct sctp_assoc_value *av;
7126
7127                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
7128                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
7129
7130                 if (stcb) {
7131                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7132                         error = EINVAL;
7133                         SCTP_TCB_UNLOCK(stcb);
7134                 } else {
7135                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7136                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7137                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
7138                                 if ((av->assoc_value != 0) &&
7139                                     (inp->auth_supported == 0)) {
7140                                         /* AUTH is required for ASCONF */
7141                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7142                                         error = EINVAL;
7143                                 } else {
7144                                         SCTP_INP_WLOCK(inp);
7145                                         if (av->assoc_value == 0) {
7146                                                 inp->asconf_supported = 0;
7147                                                 sctp_auth_delete_chunk(SCTP_ASCONF,
7148                                                                        inp->sctp_ep.local_auth_chunks);
7149                                                 sctp_auth_delete_chunk(SCTP_ASCONF_ACK,
7150                                                                        inp->sctp_ep.local_auth_chunks);
7151                                         } else {
7152                                                 inp->asconf_supported = 1;
7153                                                 sctp_auth_add_chunk(SCTP_ASCONF,
7154                                                                     inp->sctp_ep.local_auth_chunks);
7155                                                 sctp_auth_add_chunk(SCTP_ASCONF_ACK,
7156                                                                     inp->sctp_ep.local_auth_chunks);
7157                                         }
7158                                         SCTP_INP_WUNLOCK(inp);
7159                                 }
7160                         } else {
7161                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7162                                 error = EINVAL;
7163                         }
7164                 }
7165                 break;
7166         }
7167         case SCTP_RECONFIG_SUPPORTED:
7168         {
7169                 struct sctp_assoc_value *av;
7170
7171                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
7172                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
7173
7174                 if (stcb) {
7175                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7176                         error = EINVAL;
7177                         SCTP_TCB_UNLOCK(stcb);
7178                 } else {
7179                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7180                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7181                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
7182                                 SCTP_INP_WLOCK(inp);
7183                                 if (av->assoc_value == 0) {
7184                                         inp->reconfig_supported = 0;
7185                                 } else {
7186                                         inp->reconfig_supported = 1;
7187                                 }
7188                                 SCTP_INP_WUNLOCK(inp);
7189                         } else {
7190                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7191                                 error = EINVAL;
7192                         }
7193                 }
7194                 break;
7195         }
7196         case SCTP_NRSACK_SUPPORTED:
7197         {
7198                 struct sctp_assoc_value *av;
7199
7200                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
7201                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
7202
7203                 if (stcb) {
7204                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7205                         error = EINVAL;
7206                         SCTP_TCB_UNLOCK(stcb);
7207                 } else {
7208                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7209                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7210                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
7211                                 SCTP_INP_WLOCK(inp);
7212                                 if (av->assoc_value == 0) {
7213                                         inp->nrsack_supported = 0;
7214                                 } else {
7215                                         inp->nrsack_supported = 1;
7216                                 }
7217                                 SCTP_INP_WUNLOCK(inp);
7218                         } else {
7219                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7220                                 error = EINVAL;
7221                         }
7222                 }
7223                 break;
7224         }
7225         case SCTP_PKTDROP_SUPPORTED:
7226         {
7227                 struct sctp_assoc_value *av;
7228
7229                 SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
7230                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
7231
7232                 if (stcb) {
7233                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7234                         error = EINVAL;
7235                         SCTP_TCB_UNLOCK(stcb);
7236                 } else {
7237                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7238                             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
7239                             (av->assoc_id == SCTP_FUTURE_ASSOC)) {
7240                                 SCTP_INP_WLOCK(inp);
7241                                 if (av->assoc_value == 0) {
7242                                         inp->pktdrop_supported = 0;
7243                                 } else {
7244                                         inp->pktdrop_supported = 1;
7245                                 }
7246                                 SCTP_INP_WUNLOCK(inp);
7247                         } else {
7248                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7249                                 error = EINVAL;
7250                         }
7251                 }
7252                 break;
7253         }
7254         default:
7255                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOPROTOOPT);
7256                 error = ENOPROTOOPT;
7257                 break;
7258         } /* end switch (opt) */
7259         return (error);
7260 }
7261
7262 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
7263 int
7264 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
7265 {
7266         void *optval = NULL;
7267         size_t optsize = 0;
7268         void *p;
7269         int error = 0;
7270
7271         if (sopt->sopt_level != IPPROTO_SCTP) {
7272                 /* wrong proto level... send back up to IP */
7273 #ifdef INET6
7274                 if (INP_CHECK_SOCKAF(so, AF_INET6))
7275                         error = ip6_ctloutput(so, sopt);
7276 #endif                          /* INET6 */
7277 #if defined(INET) && defined(INET6)
7278                 else
7279 #endif
7280 #ifdef INET
7281                         error = ip_ctloutput(so, sopt);
7282 #endif
7283                 return (error);
7284         }
7285         optsize = sopt->sopt_valsize;
7286         if (optsize) {
7287                 SCTP_MALLOC(optval, void *, optsize, SCTP_M_SOCKOPT);
7288                 if (optval == NULL) {
7289                         SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOBUFS);
7290                         return (ENOBUFS);
7291                 }
7292                 error = sooptcopyin(sopt, optval, optsize, optsize);
7293                 if (error) {
7294                         SCTP_FREE(optval, SCTP_M_SOCKOPT);
7295                         goto out;
7296                 }
7297         }
7298 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__Windows__)
7299         p = (void *)sopt->sopt_td;
7300 #else
7301         p = (void *)sopt->sopt_p;
7302 #endif
7303         if (sopt->sopt_dir == SOPT_SET) {
7304                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
7305         } else if (sopt->sopt_dir == SOPT_GET) {
7306                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
7307         } else {
7308                 SCTP_LTRACE_ERR_RET(so->so_pcb, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7309                 error = EINVAL;
7310         }
7311         if ((error == 0) && (optval != NULL)) {
7312                 error = sooptcopyout(sopt, optval, optsize);
7313                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
7314         } else if (optval != NULL) {
7315                 SCTP_FREE(optval, SCTP_M_SOCKOPT);
7316         }
7317 out:
7318         return (error);
7319 }
7320 #endif
7321
7322 #ifdef INET
7323 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
7324 static int
7325 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
7326 {
7327 #else
7328 #if defined(__FreeBSD__) || defined(__APPLE__)
7329 static int
7330 sctp_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
7331 {
7332 #elif defined(__Panda__) || defined(__Userspace__)
7333 int
7334 sctp_connect(struct socket *so, struct sockaddr *addr)
7335 {
7336         void *p = NULL;
7337 #elif defined(__Windows__)
7338 static int
7339 sctp_connect(struct socket *so, struct sockaddr *addr, PKTHREAD p)
7340 {
7341 #else
7342 static int
7343 sctp_connect(struct socket *so, struct mbuf *nam, struct proc *p)
7344 {
7345         struct sockaddr *addr = mtod(nam, struct sockaddr *);
7346
7347 #endif
7348 #endif
7349 #ifdef SCTP_MVRF
7350         int i, fnd = 0;
7351 #endif
7352         int error = 0;
7353         int create_lock_on = 0;
7354         uint32_t vrf_id;
7355         struct sctp_inpcb *inp;
7356         struct sctp_tcb *stcb = NULL;
7357
7358         inp = (struct sctp_inpcb *)so->so_pcb;
7359         if (inp == NULL) {
7360                 /* I made the same as TCP since we are not setup? */
7361                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7362                 return (ECONNRESET);
7363         }
7364         if (addr == NULL) {
7365                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7366                 return EINVAL;
7367         }
7368
7369 #if defined(__Userspace__)
7370         /* TODO __Userspace__ falls into this code for IPv6 stuff at the moment... */
7371 #endif
7372 #if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
7373         switch (addr->sa_family) {
7374 #ifdef INET6
7375         case AF_INET6:
7376         {
7377 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
7378                 struct sockaddr_in6 *sin6p;
7379
7380 #endif
7381                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
7382                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7383                         return (EINVAL);
7384                 }
7385 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
7386                 sin6p = (struct sockaddr_in6 *)addr;
7387                 if (p != NULL && (error = prison_remote_ip6(p->td_ucred, &sin6p->sin6_addr)) != 0) {
7388                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
7389                         return (error);
7390                 }
7391 #endif
7392                 break;
7393         }
7394 #endif
7395 #ifdef INET
7396         case AF_INET:
7397         {
7398 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
7399                 struct sockaddr_in *sinp;
7400
7401 #endif
7402 #if !defined(__Userspace_os_Windows)
7403                 if (addr->sa_len != sizeof(struct sockaddr_in)) {
7404                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7405                         return (EINVAL);
7406                 }
7407 #endif
7408 #if defined(__FreeBSD__) && __FreeBSD_version >= 800000
7409                 sinp = (struct sockaddr_in *)addr;
7410                 if (p != NULL && (error = prison_remote_ip4(p->td_ucred, &sinp->sin_addr)) != 0) {
7411                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, error);
7412                         return (error);
7413                 }
7414 #endif
7415                 break;
7416         }
7417 #endif
7418         default:
7419                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
7420                 return (EAFNOSUPPORT);
7421         }
7422 #endif
7423         SCTP_INP_INCR_REF(inp);
7424         SCTP_ASOC_CREATE_LOCK(inp);
7425         create_lock_on = 1;
7426
7427
7428         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
7429             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
7430                 /* Should I really unlock ? */
7431                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
7432                 error = EFAULT;
7433                 goto out_now;
7434         }
7435 #ifdef INET6
7436         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7437             (addr->sa_family == AF_INET6)) {
7438                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7439                 error = EINVAL;
7440                 goto out_now;
7441         }
7442 #endif
7443 #if defined(__Userspace__)
7444         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) &&
7445             (addr->sa_family != AF_CONN)) {
7446                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7447                 error = EINVAL;
7448                 goto out_now;
7449         }
7450 #endif
7451         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
7452             SCTP_PCB_FLAGS_UNBOUND) {
7453                 /* Bind a ephemeral port */
7454                 error = sctp_inpcb_bind(so, NULL, NULL, p);
7455                 if (error) {
7456                         goto out_now;
7457                 }
7458         }
7459         /* Now do we connect? */
7460         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
7461             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
7462                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7463                 error = EINVAL;
7464                 goto out_now;
7465         }
7466         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7467             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7468                 /* We are already connected AND the TCP model */
7469                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7470                 error = EADDRINUSE;
7471                 goto out_now;
7472         }
7473         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7474                 SCTP_INP_RLOCK(inp);
7475                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7476                 SCTP_INP_RUNLOCK(inp);
7477         } else {
7478                 /* We increment here since sctp_findassociation_ep_addr() will
7479                  * do a decrement if it finds the stcb as long as the locked
7480                  * tcb (last argument) is NOT a TCB.. aka NULL.
7481                  */
7482                 SCTP_INP_INCR_REF(inp);
7483                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
7484                 if (stcb == NULL) {
7485                         SCTP_INP_DECR_REF(inp);
7486                 } else {
7487                         SCTP_TCB_UNLOCK(stcb);
7488                 }
7489         }
7490         if (stcb != NULL) {
7491                 /* Already have or am bring up an association */
7492                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
7493                 error = EALREADY;
7494                 goto out_now;
7495         }
7496
7497         vrf_id = inp->def_vrf_id;
7498 #ifdef SCTP_MVRF
7499         for (i = 0; i < inp->num_vrfs; i++) {
7500                 if (vrf_id == inp->m_vrf_ids[i]) {
7501                         fnd = 1;
7502                         break;
7503                 }
7504         }
7505         if (!fnd) {
7506                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7507                 error = EINVAL;
7508                 goto out_now;
7509         }
7510 #endif
7511         /* We are GOOD to go */
7512         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
7513         if (stcb == NULL) {
7514                 /* Gak! no memory */
7515                 goto out_now;
7516         }
7517         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
7518                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
7519                 /* Set the connected flag so we can queue data */
7520                 soisconnecting(so);
7521         }
7522         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
7523         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
7524
7525         /* initialize authentication parameters for the assoc */
7526         sctp_initialize_auth_params(inp, stcb);
7527
7528         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
7529         SCTP_TCB_UNLOCK(stcb);
7530  out_now:
7531         if (create_lock_on) {
7532                 SCTP_ASOC_CREATE_UNLOCK(inp);
7533         }
7534
7535         SCTP_INP_DECR_REF(inp);
7536         return (error);
7537 }
7538 #endif
7539
7540 #if defined(__Userspace__)
7541 int
7542 sctpconn_connect(struct socket *so, struct sockaddr *addr)
7543 {
7544 #ifdef SCTP_MVRF
7545         int i, fnd = 0;
7546 #endif
7547         void *p = NULL;
7548         int error = 0;
7549         int create_lock_on = 0;
7550         uint32_t vrf_id;
7551         struct sctp_inpcb *inp;
7552         struct sctp_tcb *stcb = NULL;
7553
7554         inp = (struct sctp_inpcb *)so->so_pcb;
7555         if (inp == NULL) {
7556                 /* I made the same as TCP since we are not setup? */
7557                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7558                 return (ECONNRESET);
7559         }
7560         if (addr == NULL) {
7561                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7562                 return EINVAL;
7563         }
7564         switch (addr->sa_family) {
7565 #ifdef INET
7566         case AF_INET:
7567 #ifdef HAVE_SA_LEN
7568                 if (addr->sa_len != sizeof(struct sockaddr_in)) {
7569                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7570                         return (EINVAL);
7571                 }
7572 #endif
7573                 break;
7574 #endif
7575 #ifdef INET6
7576         case AF_INET6:
7577 #ifdef HAVE_SA_LEN
7578                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
7579                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7580                         return (EINVAL);
7581                 }
7582 #endif
7583                 break;
7584 #endif
7585         case AF_CONN:
7586 #ifdef HAVE_SA_LEN
7587                 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
7588                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7589                         return (EINVAL);
7590                 }
7591 #endif
7592                 break;
7593         default:
7594                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
7595                 return (EAFNOSUPPORT);
7596         }
7597         SCTP_INP_INCR_REF(inp);
7598         SCTP_ASOC_CREATE_LOCK(inp);
7599         create_lock_on = 1;
7600
7601
7602         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
7603             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
7604                 /* Should I really unlock ? */
7605                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EFAULT);
7606                 error = EFAULT;
7607                 goto out_now;
7608         }
7609 #ifdef INET6
7610         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7611             (addr->sa_family == AF_INET6)) {
7612                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7613                 error = EINVAL;
7614                 goto out_now;
7615         }
7616 #endif
7617         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == SCTP_PCB_FLAGS_UNBOUND) {
7618                 /* Bind a ephemeral port */
7619                 error = sctp_inpcb_bind(so, NULL, NULL, p);
7620                 if (error) {
7621                         goto out_now;
7622                 }
7623         }
7624         /* Now do we connect? */
7625         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
7626             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE))) {
7627                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7628                 error = EINVAL;
7629                 goto out_now;
7630         }
7631         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7632             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7633                 /* We are already connected AND the TCP model */
7634                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7635                 error = EADDRINUSE;
7636                 goto out_now;
7637         }
7638         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7639                 SCTP_INP_RLOCK(inp);
7640                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7641                 SCTP_INP_RUNLOCK(inp);
7642         } else {
7643                 /* We increment here since sctp_findassociation_ep_addr() will
7644                  * do a decrement if it finds the stcb as long as the locked
7645                  * tcb (last argument) is NOT a TCB.. aka NULL.
7646                  */
7647                 SCTP_INP_INCR_REF(inp);
7648                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
7649                 if (stcb == NULL) {
7650                         SCTP_INP_DECR_REF(inp);
7651                 } else {
7652                         SCTP_TCB_UNLOCK(stcb);
7653                 }
7654         }
7655         if (stcb != NULL) {
7656                 /* Already have or am bring up an association */
7657                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EALREADY);
7658                 error = EALREADY;
7659                 goto out_now;
7660         }
7661
7662         vrf_id = inp->def_vrf_id;
7663 #ifdef SCTP_MVRF
7664         for (i = 0; i < inp->num_vrfs; i++) {
7665                 if (vrf_id == inp->m_vrf_ids[i]) {
7666                         fnd = 1;
7667                         break;
7668                 }
7669         }
7670         if (!fnd) {
7671                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7672                 error = EINVAL;
7673                 goto out_now;
7674         }
7675 #endif
7676         /* We are GOOD to go */
7677         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id, p);
7678         if (stcb == NULL) {
7679                 /* Gak! no memory */
7680                 goto out_now;
7681         }
7682         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
7683                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
7684                 /* Set the connected flag so we can queue data */
7685                 soisconnecting(so);
7686         }
7687         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
7688         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
7689
7690         /* initialize authentication parameters for the assoc */
7691         sctp_initialize_auth_params(inp, stcb);
7692
7693         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
7694         SCTP_TCB_UNLOCK(stcb);
7695  out_now:
7696         if (create_lock_on) {
7697                 SCTP_ASOC_CREATE_UNLOCK(inp);
7698         }
7699
7700         SCTP_INP_DECR_REF(inp);
7701         return (error);
7702 }
7703 #endif
7704 int
7705 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
7706 #if __FreeBSD_version >= 700000
7707 sctp_listen(struct socket *so, int backlog, struct thread *p)
7708 #else
7709 sctp_listen(struct socket *so, struct thread *p)
7710 #endif
7711 #elif defined(__Windows__)
7712 sctp_listen(struct socket *so, int backlog, PKTHREAD p)
7713 #elif defined(__Userspace__)
7714 sctp_listen(struct socket *so, int backlog, struct proc *p)
7715 #else
7716 sctp_listen(struct socket *so, struct proc *p)
7717 #endif
7718 {
7719         /*
7720          * Note this module depends on the protocol processing being called
7721          * AFTER any socket level flags and backlog are applied to the
7722          * socket. The traditional way that the socket flags are applied is
7723          * AFTER protocol processing. We have made a change to the
7724          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
7725          * place if the socket API for SCTP is to work properly.
7726          */
7727
7728         int error = 0;
7729         struct sctp_inpcb *inp;
7730
7731         inp = (struct sctp_inpcb *)so->so_pcb;
7732         if (inp == NULL) {
7733                 /* I made the same as TCP since we are not setup? */
7734                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7735                 return (ECONNRESET);
7736         }
7737         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
7738                 /* See if we have a listener */
7739                 struct sctp_inpcb *tinp;
7740                 union sctp_sockstore store;
7741
7742                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
7743                         /* not bound all */
7744                         struct sctp_laddr *laddr;
7745
7746                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
7747                                 memcpy(&store, &laddr->ifa->address, sizeof(store));
7748                                 switch (store.sa.sa_family) {
7749 #ifdef INET
7750                                 case AF_INET:
7751                                         store.sin.sin_port = inp->sctp_lport;
7752                                         break;
7753 #endif
7754 #ifdef INET6
7755                                 case AF_INET6:
7756                                         store.sin6.sin6_port = inp->sctp_lport;
7757                                         break;
7758 #endif
7759 #if defined(__Userspace__)
7760                                 case AF_CONN:
7761                                         store.sconn.sconn_port = inp->sctp_lport;
7762                                         break;
7763 #endif
7764                                 default:
7765                                         break;
7766                                 }
7767                                 tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7768                                 if (tinp && (tinp != inp) &&
7769                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7770                                     ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7771                                     (tinp->sctp_socket->so_qlimit)) {
7772                                         /* we have a listener already and its not this inp. */
7773                                         SCTP_INP_DECR_REF(tinp);
7774                                         return (EADDRINUSE);
7775                                 } else if (tinp) {
7776                                         SCTP_INP_DECR_REF(tinp);
7777                                 }
7778                         }
7779                 } else {
7780                         /* Setup a local addr bound all */
7781                         memset(&store, 0, sizeof(store));
7782 #ifdef INET6
7783                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7784                                 store.sa.sa_family = AF_INET6;
7785 #ifdef HAVE_SA_LEN
7786                                 store.sa.sa_len = sizeof(struct sockaddr_in6);
7787 #endif
7788                         }
7789 #endif
7790 #if defined(__Userspace__)
7791                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
7792                                 store.sa.sa_family = AF_CONN;
7793 #ifdef HAVE_SA_LEN
7794                                 store.sa.sa_len = sizeof(struct sockaddr_conn);
7795 #endif
7796                         }
7797 #endif
7798 #ifdef INET
7799 #if defined(__Userspace__)
7800                         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7801                             ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) == 0)) {
7802 #else
7803                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7804 #endif
7805                                 store.sa.sa_family = AF_INET;
7806 #ifdef HAVE_SA_LEN
7807                                 store.sa.sa_len = sizeof(struct sockaddr_in);
7808 #endif
7809                         }
7810 #endif
7811                         switch (store.sa.sa_family) {
7812 #ifdef INET
7813                         case AF_INET:
7814                                 store.sin.sin_port = inp->sctp_lport;
7815                                 break;
7816 #endif
7817 #ifdef INET6
7818                         case AF_INET6:
7819                                 store.sin6.sin6_port = inp->sctp_lport;
7820                                 break;
7821 #endif
7822 #if defined(__Userspace__)
7823                         case AF_CONN:
7824                                 store.sconn.sconn_port = inp->sctp_lport;
7825                                 break;
7826 #endif
7827                         default:
7828                                 break;
7829                         }
7830                         tinp = sctp_pcb_findep(&store.sa, 0, 0, inp->def_vrf_id);
7831                         if (tinp && (tinp != inp) &&
7832                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0) &&
7833                             ((tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
7834                             (tinp->sctp_socket->so_qlimit)) {
7835                                 /* we have a listener already and its not this inp. */
7836                                 SCTP_INP_DECR_REF(tinp);
7837                                 return (EADDRINUSE);
7838                         } else if (tinp) {
7839                                 SCTP_INP_DECR_REF(inp);
7840                         }
7841                 }
7842         }
7843         SCTP_INP_RLOCK(inp);
7844 #ifdef SCTP_LOCK_LOGGING
7845         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) {
7846                 sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
7847         }
7848 #endif
7849         SOCK_LOCK(so);
7850 #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Userspace__)
7851         error = solisten_proto_check(so);
7852         if (error) {
7853                 SOCK_UNLOCK(so);
7854                 SCTP_INP_RUNLOCK(inp);
7855                 return (error);
7856         }
7857 #endif
7858         if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
7859             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
7860                 /* The unlucky case
7861                  * - We are in the tcp pool with this guy.
7862                  * - Someone else is in the main inp slot.
7863                  * - We must move this guy (the listener) to the main slot
7864                  * - We must then move the guy that was listener to the TCP Pool.
7865                  */
7866                 if (sctp_swap_inpcb_for_listen(inp)) {
7867                         goto in_use;
7868                 }
7869         }
7870
7871         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7872             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
7873                 /* We are already connected AND the TCP model */
7874         in_use:
7875                 SCTP_INP_RUNLOCK(inp);
7876                 SOCK_UNLOCK(so);
7877                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EADDRINUSE);
7878                 return (EADDRINUSE);
7879         }
7880         SCTP_INP_RUNLOCK(inp);
7881         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7882                 /* We must do a bind. */
7883                 SOCK_UNLOCK(so);
7884                 if ((error = sctp_inpcb_bind(so, NULL, NULL, p))) {
7885                         /* bind error, probably perm */
7886                         return (error);
7887                 }
7888                 SOCK_LOCK(so);
7889         }
7890 #if (defined(__FreeBSD__) && __FreeBSD_version > 500000) || defined(__Windows__) || defined(__Userspace__)
7891 #if __FreeBSD_version >= 700000 || defined(__Windows__) || defined(__Userspace__)
7892         /* It appears for 7.0 and on, we must always call this. */
7893         solisten_proto(so, backlog);
7894 #else
7895         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) == 0) {
7896                 solisten_proto(so);
7897         }
7898 #endif
7899 #endif
7900         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7901                 /* remove the ACCEPTCONN flag for one-to-many sockets */
7902 #if defined(__Userspace__)
7903                 so->so_options &= ~SCTP_SO_ACCEPTCONN;
7904 #else
7905                 so->so_options &= ~SO_ACCEPTCONN;
7906 #endif
7907         }
7908
7909 #if __FreeBSD_version >= 700000 || defined(__Windows__) || defined(__Userspace__)
7910         if (backlog == 0) {
7911                 /* turning off listen */
7912 #if defined(__Userspace__)
7913                 so->so_options &= ~SCTP_SO_ACCEPTCONN;
7914 #else
7915                 so->so_options &= ~SO_ACCEPTCONN;
7916 #endif
7917         }
7918 #endif
7919         SOCK_UNLOCK(so);
7920         return (error);
7921 }
7922
7923 static int sctp_defered_wakeup_cnt = 0;
7924
7925 int
7926 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
7927 sctp_accept(struct socket *so, struct sockaddr **addr)
7928 {
7929 #elif defined(__Panda__)
7930 sctp_accept(struct socket *so, struct sockaddr *addr, int *namelen,
7931             void *accept_info, int *accept_info_len)
7932 {
7933 #else
7934 sctp_accept(struct socket *so, struct mbuf *nam)
7935 {
7936         struct sockaddr *addr = mtod(nam, struct sockaddr *);
7937 #endif
7938         struct sctp_tcb *stcb;
7939         struct sctp_inpcb *inp;
7940         union sctp_sockstore store;
7941 #ifdef INET6
7942 #ifdef SCTP_KAME
7943         int error;
7944 #endif /* SCTP_KAME */
7945 #endif
7946         inp = (struct sctp_inpcb *)so->so_pcb;
7947
7948         if (inp == NULL) {
7949                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7950                 return (ECONNRESET);
7951         }
7952         SCTP_INP_RLOCK(inp);
7953         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
7954                 SCTP_INP_RUNLOCK(inp);
7955                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EOPNOTSUPP);
7956                 return (EOPNOTSUPP);
7957         }
7958         if (so->so_state & SS_ISDISCONNECTED) {
7959                 SCTP_INP_RUNLOCK(inp);
7960                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ECONNABORTED);
7961                 return (ECONNABORTED);
7962         }
7963         stcb = LIST_FIRST(&inp->sctp_asoc_list);
7964         if (stcb == NULL) {
7965                 SCTP_INP_RUNLOCK(inp);
7966                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
7967                 return (ECONNRESET);
7968         }
7969         SCTP_TCB_LOCK(stcb);
7970         SCTP_INP_RUNLOCK(inp);
7971         store = stcb->asoc.primary_destination->ro._l_addr;
7972         stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
7973         SCTP_TCB_UNLOCK(stcb);
7974         switch (store.sa.sa_family) {
7975 #ifdef INET
7976         case AF_INET:
7977         {
7978                 struct sockaddr_in *sin;
7979
7980 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
7981                 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
7982                 if (sin == NULL)
7983                         return (ENOMEM);
7984 #else
7985                 sin = (struct sockaddr_in *)addr;
7986                 bzero((caddr_t)sin, sizeof(*sin));
7987 #endif
7988                 sin->sin_family = AF_INET;
7989 #ifdef HAVE_SIN_LEN
7990                 sin->sin_len = sizeof(*sin);
7991 #endif
7992                 sin->sin_port = store.sin.sin_port;
7993                 sin->sin_addr = store.sin.sin_addr;
7994 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
7995                 *addr = (struct sockaddr *)sin;
7996 #elif !defined(__Panda__)
7997                 SCTP_BUF_LEN(nam) = sizeof(*sin);
7998 #endif
7999                 break;
8000         }
8001 #endif
8002 #ifdef INET6
8003         case AF_INET6:
8004         {
8005                 struct sockaddr_in6 *sin6;
8006
8007 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
8008                 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
8009                 if (sin6 == NULL)
8010                         return (ENOMEM);
8011 #else
8012                 sin6 = (struct sockaddr_in6 *)addr;
8013                 bzero((caddr_t)sin6, sizeof(*sin6));
8014 #endif
8015                 sin6->sin6_family = AF_INET6;
8016 #ifdef HAVE_SIN6_LEN
8017                 sin6->sin6_len = sizeof(*sin6);
8018 #endif
8019                 sin6->sin6_port = store.sin6.sin6_port;
8020                 sin6->sin6_addr = store.sin6.sin6_addr;
8021 #if defined(SCTP_EMBEDDED_V6_SCOPE)
8022 #ifdef SCTP_KAME
8023                 if ((error = sa6_recoverscope(sin6)) != 0) {
8024                         SCTP_FREE_SONAME(sin6);
8025                         return (error);
8026                 }
8027 #else
8028                 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
8029                         /*
8030                          * sin6->sin6_scope_id =
8031                          * ntohs(sin6->sin6_addr.s6_addr16[1]);
8032                          */
8033                         in6_recoverscope(sin6, &sin6->sin6_addr, NULL); /* skip ifp check */
8034                 else
8035                         sin6->sin6_scope_id = 0;        /* XXX */
8036 #endif /* SCTP_KAME */
8037 #endif /* SCTP_EMBEDDED_V6_SCOPE */
8038 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)
8039                 *addr = (struct sockaddr *)sin6;
8040 #elif !defined(__Panda__)
8041                 SCTP_BUF_LEN(nam) = sizeof(*sin6);
8042 #endif
8043                 break;
8044         }
8045 #endif
8046 #if defined(__Userspace__)
8047         case AF_CONN:
8048         {
8049                 struct sockaddr_conn *sconn;
8050
8051                 SCTP_MALLOC_SONAME(sconn, struct sockaddr_conn *, sizeof(struct sockaddr_conn));
8052                 if (sconn == NULL) {
8053                         return (ENOMEM);
8054                 }
8055                 sconn->sconn_family = AF_CONN;
8056 #ifdef HAVE_SCONN_LEN
8057                 sconn->sconn_len = sizeof(struct sockaddr_conn);
8058 #endif
8059                 sconn->sconn_port = store.sconn.sconn_port;
8060                 sconn->sconn_addr = store.sconn.sconn_addr;
8061                 *addr = (struct sockaddr *)sconn;
8062                 break;
8063         }
8064 #endif
8065         default:
8066                 /* TSNH */
8067                 break;
8068         }
8069         /* Wake any delayed sleep action */
8070         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
8071                 SCTP_INP_WLOCK(inp);
8072                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
8073                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
8074                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
8075                         SCTP_INP_WUNLOCK(inp);
8076                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
8077                         if (sowriteable(inp->sctp_socket)) {
8078 #if defined(__Userspace__)
8079                             /*__Userspace__ calling sowwakup_locked because of SOCKBUF_LOCK above. */
8080 #endif
8081 #if defined(__FreeBSD__) || defined(__Windows__) || defined(__Userspace__)
8082                                 sowwakeup_locked(inp->sctp_socket);
8083 #else
8084 #if defined(__APPLE__)
8085                                 /* socket is locked */
8086 #endif
8087                                 sowwakeup(inp->sctp_socket);
8088 #endif
8089                         } else {
8090                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
8091                         }
8092                         SCTP_INP_WLOCK(inp);
8093                 }
8094                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
8095                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
8096                         SCTP_INP_WUNLOCK(inp);
8097                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
8098                         if (soreadable(inp->sctp_socket)) {
8099                                 sctp_defered_wakeup_cnt++;
8100 #if defined(__Userspace__)
8101                                 /*__Userspace__ calling sorwakup_locked because of SOCKBUF_LOCK above */
8102 #endif
8103 #if defined(__FreeBSD__) || defined(__Windows__) || defined(__Userspace__)
8104                                 sorwakeup_locked(inp->sctp_socket);
8105 #else
8106 #if defined(__APPLE__)
8107                                 /* socket is locked */
8108 #endif
8109                                 sorwakeup(inp->sctp_socket);
8110 #endif
8111                         } else {
8112                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
8113                         }
8114                         SCTP_INP_WLOCK(inp);
8115                 }
8116                 SCTP_INP_WUNLOCK(inp);
8117         }
8118         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
8119                 SCTP_TCB_LOCK(stcb);
8120                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ+SCTP_LOC_7);
8121         }
8122         return (0);
8123 }
8124
8125 #ifdef INET
8126 int
8127 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8128 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
8129 {
8130         struct sockaddr_in *sin;
8131 #elif defined(__Panda__)
8132 sctp_ingetaddr(struct socket *so, struct sockaddr *addr)
8133 {
8134         struct sockaddr_in *sin = (struct sockaddr_in *)addr;
8135 #else
8136 sctp_ingetaddr(struct socket *so, struct mbuf *nam)
8137 {
8138         struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
8139 #endif
8140         uint32_t vrf_id;
8141         struct sctp_inpcb *inp;
8142         struct sctp_ifa *sctp_ifa;
8143
8144         /*
8145          * Do the malloc first in case it blocks.
8146          */
8147 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8148         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
8149         if (sin == NULL)
8150                 return (ENOMEM);
8151 #elif defined(__Panda__)
8152         bzero(sin, sizeof(*sin));
8153 #else
8154         SCTP_BUF_LEN(nam) = sizeof(*sin);
8155         memset(sin, 0, sizeof(*sin));
8156 #endif
8157         sin->sin_family = AF_INET;
8158 #ifdef HAVE_SIN_LEN
8159         sin->sin_len = sizeof(*sin);
8160 #endif
8161         inp = (struct sctp_inpcb *)so->so_pcb;
8162         if (!inp) {
8163 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8164                 SCTP_FREE_SONAME(sin);
8165 #endif
8166                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
8167                 return (ECONNRESET);
8168         }
8169         SCTP_INP_RLOCK(inp);
8170         sin->sin_port = inp->sctp_lport;
8171         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
8172                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
8173                         struct sctp_tcb *stcb;
8174                         struct sockaddr_in *sin_a;
8175                         struct sctp_nets *net;
8176                         int fnd;
8177
8178                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
8179                         if (stcb == NULL) {
8180                                 goto notConn;
8181                         }
8182                         fnd = 0;
8183                         sin_a = NULL;
8184                         SCTP_TCB_LOCK(stcb);
8185                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8186                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
8187                                 if (sin_a == NULL)
8188                                         /* this will make coverity happy */
8189                                         continue;
8190
8191                                 if (sin_a->sin_family == AF_INET) {
8192                                         fnd = 1;
8193                                         break;
8194                                 }
8195                         }
8196                         if ((!fnd) || (sin_a == NULL)) {
8197                                 /* punt */
8198                                 SCTP_TCB_UNLOCK(stcb);
8199                                 goto notConn;
8200                         }
8201
8202                         vrf_id = inp->def_vrf_id;
8203                         sctp_ifa = sctp_source_address_selection(inp,
8204                                                                  stcb,
8205                                                                  (sctp_route_t *)&net->ro,
8206                                                                  net, 0, vrf_id);
8207                         if (sctp_ifa) {
8208                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
8209                                 sctp_free_ifa(sctp_ifa);
8210                         }
8211                         SCTP_TCB_UNLOCK(stcb);
8212                 } else {
8213                         /* For the bound all case you get back 0 */
8214         notConn:
8215                         sin->sin_addr.s_addr = 0;
8216                 }
8217
8218         } else {
8219                 /* Take the first IPv4 address in the list */
8220                 struct sctp_laddr *laddr;
8221                 int fnd = 0;
8222
8223                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
8224                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
8225                                 struct sockaddr_in *sin_a;
8226
8227                                 sin_a = &laddr->ifa->address.sin;
8228                                 sin->sin_addr = sin_a->sin_addr;
8229                                 fnd = 1;
8230                                 break;
8231                         }
8232                 }
8233                 if (!fnd) {
8234 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8235                         SCTP_FREE_SONAME(sin);
8236 #endif
8237                         SCTP_INP_RUNLOCK(inp);
8238                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
8239                         return (ENOENT);
8240                 }
8241         }
8242         SCTP_INP_RUNLOCK(inp);
8243 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8244         (*addr) = (struct sockaddr *)sin;
8245 #endif
8246         return (0);
8247 }
8248
8249 int
8250 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8251 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
8252 {
8253         struct sockaddr_in *sin;
8254 #elif defined(__Panda__)
8255 sctp_peeraddr(struct socket *so, struct sockaddr *addr)
8256 {
8257         struct sockaddr_in *sin = (struct sockaddr_in *)addr;
8258 #else
8259 sctp_peeraddr(struct socket *so, struct mbuf *nam)
8260 {
8261         struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
8262
8263 #endif
8264         int fnd;
8265         struct sockaddr_in *sin_a;
8266         struct sctp_inpcb *inp;
8267         struct sctp_tcb *stcb;
8268         struct sctp_nets *net;
8269
8270         /* Do the malloc first in case it blocks. */
8271 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8272         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
8273         if (sin == NULL)
8274                 return (ENOMEM);
8275 #elif defined(__Panda__)
8276         memset(sin, 0, sizeof(*sin));
8277 #else
8278         SCTP_BUF_LEN(nam) = sizeof(*sin);
8279         memset(sin, 0, sizeof(*sin));
8280 #endif
8281         sin->sin_family = AF_INET;
8282 #ifdef HAVE_SIN_LEN
8283         sin->sin_len = sizeof(*sin);
8284 #endif
8285
8286         inp = (struct sctp_inpcb *)so->so_pcb;
8287         if ((inp == NULL) ||
8288             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
8289                 /* UDP type and listeners will drop out here */
8290 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8291                 SCTP_FREE_SONAME(sin);
8292 #endif
8293                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOTCONN);
8294                 return (ENOTCONN);
8295         }
8296         SCTP_INP_RLOCK(inp);
8297         stcb = LIST_FIRST(&inp->sctp_asoc_list);
8298         if (stcb) {
8299                 SCTP_TCB_LOCK(stcb);
8300         }
8301         SCTP_INP_RUNLOCK(inp);
8302         if (stcb == NULL) {
8303 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8304                 SCTP_FREE_SONAME(sin);
8305 #endif
8306                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
8307                 return (ECONNRESET);
8308         }
8309         fnd = 0;
8310         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8311                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
8312                 if (sin_a->sin_family == AF_INET) {
8313                         fnd = 1;
8314                         sin->sin_port = stcb->rport;
8315                         sin->sin_addr = sin_a->sin_addr;
8316                         break;
8317                 }
8318         }
8319         SCTP_TCB_UNLOCK(stcb);
8320         if (!fnd) {
8321                 /* No IPv4 address */
8322 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8323                 SCTP_FREE_SONAME(sin);
8324 #endif
8325                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, ENOENT);
8326                 return (ENOENT);
8327         }
8328 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8329         (*addr) = (struct sockaddr *)sin;
8330 #endif
8331         return (0);
8332 }
8333
8334 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__)
8335 struct pr_usrreqs sctp_usrreqs = {
8336 #if defined(__FreeBSD__)
8337         .pru_abort = sctp_abort,
8338         .pru_accept = sctp_accept,
8339         .pru_attach = sctp_attach,
8340         .pru_bind = sctp_bind,
8341         .pru_connect = sctp_connect,
8342         .pru_control = in_control,
8343 #if __FreeBSD_version >= 690000
8344         .pru_close = sctp_close,
8345         .pru_detach = sctp_close,
8346         .pru_sopoll = sopoll_generic,
8347         .pru_flush = sctp_flush,
8348 #else
8349         .pru_detach = sctp_detach,
8350         .pru_sopoll = sopoll,
8351 #endif
8352         .pru_disconnect = sctp_disconnect,
8353         .pru_listen = sctp_listen,
8354         .pru_peeraddr = sctp_peeraddr,
8355         .pru_send = sctp_sendm,
8356         .pru_shutdown = sctp_shutdown,
8357         .pru_sockaddr = sctp_ingetaddr,
8358         .pru_sosend = sctp_sosend,
8359         .pru_soreceive = sctp_soreceive
8360 #elif defined(__APPLE__)
8361         .pru_abort = sctp_abort,
8362         .pru_accept = sctp_accept,
8363         .pru_attach = sctp_attach,
8364         .pru_bind = sctp_bind,
8365         .pru_connect = sctp_connect,
8366         .pru_connect2 = pru_connect2_notsupp,
8367         .pru_control = in_control,
8368         .pru_detach = sctp_detach,
8369         .pru_disconnect = sctp_disconnect,
8370         .pru_listen = sctp_listen,
8371         .pru_peeraddr = sctp_peeraddr,
8372         .pru_rcvd = NULL,
8373         .pru_rcvoob = pru_rcvoob_notsupp,
8374         .pru_send = sctp_sendm,
8375         .pru_sense = pru_sense_null,
8376         .pru_shutdown = sctp_shutdown,
8377         .pru_sockaddr = sctp_ingetaddr,
8378         .pru_sosend = sctp_sosend,
8379         .pru_soreceive = sctp_soreceive,
8380         .pru_sopoll = sopoll
8381 #elif defined(__Windows__)
8382         sctp_abort,
8383         sctp_accept,
8384         sctp_attach,
8385         sctp_bind,
8386         sctp_connect,
8387         pru_connect2_notsupp,
8388         NULL,
8389         NULL,
8390         sctp_disconnect,
8391         sctp_listen,
8392         sctp_peeraddr,
8393         NULL,
8394         pru_rcvoob_notsupp,
8395         NULL,
8396         pru_sense_null,
8397         sctp_shutdown,
8398         sctp_flush,
8399         sctp_ingetaddr,
8400         sctp_sosend,
8401         sctp_soreceive,
8402         sopoll_generic,
8403         NULL,
8404         sctp_close
8405 #endif
8406 };
8407 #elif !defined(__Panda__) && !defined(__Userspace__)
8408 int
8409 sctp_usrreq(so, req, m, nam, control)
8410         struct socket *so;
8411         int req;
8412         struct mbuf *m, *nam, *control;
8413 {
8414         struct proc *p = curproc;
8415         uint32_t vrf_id;
8416         struct sctp_vrf *vrf;
8417         int error;
8418         int family;
8419         struct sctp_inpcb *inp = (struct sctp_inpcb *)so->so_pcb;
8420
8421         error = 0;
8422         family = so->so_proto->pr_domain->dom_family;
8423         if (req == PRU_CONTROL) {
8424                 switch (family) {
8425                 case PF_INET:
8426                         error = in_control(so, (long)m, (caddr_t)nam,
8427                             (struct ifnet *)control);
8428                         break;
8429 #ifdef INET6
8430                 case PF_INET6:
8431                         error = in6_control(so, (long)m, (caddr_t)nam,
8432                             (struct ifnet *)control, p);
8433                         break;
8434 #endif
8435                 default:
8436                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
8437                         error = EAFNOSUPPORT;
8438                 }
8439                 return (error);
8440         }
8441         switch (req) {
8442         case PRU_ATTACH:
8443                 error = sctp_attach(so, family, p);
8444                 break;
8445         case PRU_DETACH:
8446                 error = sctp_detach(so);
8447                 break;
8448         case PRU_BIND:
8449                 if (nam == NULL) {
8450                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
8451                         return (EINVAL);
8452                 }
8453                 error = sctp_bind(so, nam, p);
8454                 break;
8455         case PRU_LISTEN:
8456                 error = sctp_listen(so, p);
8457                 break;
8458         case PRU_CONNECT:
8459                 if (nam == NULL) {
8460                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
8461                         return (EINVAL);
8462                 }
8463                 error = sctp_connect(so, nam, p);
8464                 break;
8465         case PRU_DISCONNECT:
8466                 error = sctp_disconnect(so);
8467                 break;
8468         case PRU_ACCEPT:
8469                 if (nam == NULL) {
8470                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
8471                         return (EINVAL);
8472                 }
8473                 error = sctp_accept(so, nam);
8474                 break;
8475         case PRU_SHUTDOWN:
8476                 error = sctp_shutdown(so);
8477                 break;
8478
8479         case PRU_RCVD:
8480                 /*
8481                  * For Open and Net BSD, this is real ugly. The mbuf *nam
8482                  * that is passed (by soreceive()) is the int flags c ast as
8483                  * a (mbuf *) yuck!
8484                  */
8485                 break;
8486
8487         case PRU_SEND:
8488                 /* Flags are ignored */
8489                 {
8490                         struct sockaddr *addr;
8491
8492                         if (nam == NULL)
8493                                 addr = NULL;
8494                         else
8495                                 addr = mtod(nam, struct sockaddr *);
8496
8497                         error = sctp_sendm(so, 0, m, addr, control, p);
8498                 }
8499                 break;
8500         case PRU_ABORT:
8501                 error = sctp_abort(so);
8502                 break;
8503
8504         case PRU_SENSE:
8505                 error = 0;
8506                 break;
8507         case PRU_RCVOOB:
8508                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
8509                 error = EAFNOSUPPORT;
8510                 break;
8511         case PRU_SENDOOB:
8512                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EAFNOSUPPORT);
8513                 error = EAFNOSUPPORT;
8514                 break;
8515         case PRU_PEERADDR:
8516                 error = sctp_peeraddr(so, nam);
8517                 break;
8518         case PRU_SOCKADDR:
8519                 error = sctp_ingetaddr(so, nam);
8520                 break;
8521         case PRU_SLOWTIMO:
8522                 error = 0;
8523                 break;
8524         default:
8525                 break;
8526         }
8527         return (error);
8528 }
8529
8530 #endif
8531 #endif
8532
8533 #if defined(__Userspace__)
8534 int
8535 register_recv_cb(struct socket *so,
8536                  int (*receive_cb)(struct socket *sock, union sctp_sockstore addr, void *data,
8537                  size_t datalen, struct sctp_rcvinfo, int flags, void *ulp_info))
8538 {
8539         struct sctp_inpcb *inp;
8540
8541         inp = (struct sctp_inpcb *) so->so_pcb;
8542         if (inp == NULL) {
8543                 return (0);
8544         }
8545         SCTP_INP_WLOCK(inp);
8546         inp->recv_callback = receive_cb;
8547         SCTP_INP_WUNLOCK(inp);
8548         return (1);
8549 }
8550
8551 int
8552 register_send_cb(struct socket *so, uint32_t sb_threshold, int (*send_cb)(struct socket *sock, uint32_t sb_free))
8553 {
8554         struct sctp_inpcb *inp;
8555
8556         inp = (struct sctp_inpcb *) so->so_pcb;
8557         if (inp == NULL) {
8558                 return (0);
8559         }
8560         SCTP_INP_WLOCK(inp);
8561         inp->send_callback = send_cb;
8562         inp->send_sb_threshold = sb_threshold;
8563         SCTP_INP_WUNLOCK(inp);
8564         /* FIXME change to current amount free. This will be the full buffer
8565          * the first time this is registered but it could be only a portion
8566          * of the send buffer if this is called a second time e.g. if the
8567          * threshold changes.
8568          */
8569         return (1);
8570 }
8571
8572 int
8573 register_ulp_info (struct socket *so, void *ulp_info)
8574 {
8575         struct sctp_inpcb *inp;
8576
8577         inp = (struct sctp_inpcb *) so->so_pcb;
8578         if (inp == NULL) {
8579                 return (0);
8580         }
8581         SCTP_INP_WLOCK(inp);
8582         inp->ulp_info = ulp_info;
8583         SCTP_INP_WUNLOCK(inp);
8584         return (1);
8585 }
8586 #endif