IA_PRF_RTEXISTOK is no longer needed, in_{add,scrub}prefix() does more
[games.git] / sys / netinet / sctp_usrreq.c
1 /*      $KAME: sctp_usrreq.c,v 1.47 2005/03/06 16:04:18 itojun Exp $    */
2 /*      $DragonFly: src/sys/netinet/sctp_usrreq.c,v 1.14 2008/04/20 13:44:25 swildner Exp $     */
3
4 /*
5  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Cisco Systems, Inc.
19  * 4. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #if !(defined(__OpenBSD__) || defined(__APPLE__))
36 #include "opt_ipsec.h"
37 #endif
38 #if defined(__FreeBSD__) || defined(__DragonFly__)
39 #include "opt_inet6.h"
40 #include "opt_inet.h"
41 #endif
42 #if defined(__NetBSD__)
43 #include "opt_inet.h"
44 #endif
45
46 #ifdef __APPLE__
47 #include <sctp.h>
48 #elif !defined(__OpenBSD__)
49 #include "opt_sctp.h"
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/domain.h>
58 #include <sys/proc.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63 #include <sys/syslog.h>
64 #include <sys/thread2.h>
65 #include <net/if.h>
66 #include <net/if_types.h>
67 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
68 #include <net/if_var.h>
69 #endif
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip6.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/in6_var.h>
80
81 #include <netinet/ip_icmp.h>
82 #include <netinet/icmp_var.h>
83 #include <netinet/sctp_pcb.h>
84 #include <netinet/sctp_header.h>
85 #include <netinet/sctp_var.h>
86 #include <netinet/sctp_output.h>
87 #include <netinet/sctp_uio.h>
88 #include <netinet/sctp_asconf.h>
89 #include <netinet/sctputil.h>
90 #include <netinet/sctp_indata.h>
91 #include <netinet/sctp_asconf.h>
92 #ifdef IPSEC
93 #ifndef __OpenBSD__
94 #include <netinet6/ipsec.h>
95 #include <netproto/key/key.h>
96 #else
97 #undef IPSEC
98 #endif
99 #endif /* IPSEC */
100
101 #include <net/net_osdep.h>
102
103 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__) || defined(__DragonFly__)
104 #ifndef in6pcb
105 #define in6pcb          inpcb
106 #endif
107 #ifndef sotoin6pcb
108 #define sotoin6pcb      sotoinpcb
109 #endif
110 #endif
111
112 #ifdef SCTP_DEBUG
113 extern u_int32_t sctp_debug_on;
114 #endif /* SCTP_DEBUG */
115
116 /*
117  * sysctl tunable variables
118  */
119 int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF;
120 int sctp_max_burst_default = SCTP_DEF_MAX_BURST;
121 int sctp_peer_chunk_oh = sizeof(struct mbuf);
122 int sctp_strict_init = 1;
123 int sctp_no_csum_on_loopback = 1;
124 unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE;
125 int sctp_sendspace = (128 * 1024);
126 int sctp_recvspace = 128 * (1024 +
127 #ifdef INET6
128                                 sizeof(struct sockaddr_in6)
129 #else
130                                 sizeof(struct sockaddr_in)
131 #endif
132         );
133 int sctp_strict_sacks = 0;
134 int sctp_ecn = 1;
135 int sctp_ecn_nonce = 0;
136
137 unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC;
138 unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC;
139 unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC;
140 unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC;
141 unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC;
142 unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND;
143 unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND;
144 unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL;
145 unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND;
146 unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE;
147 unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT;
148 unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND;
149 unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2;
150 unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL;
151
152 void
153 sctp_init(void)
154 {
155 #ifdef __OpenBSD__
156 #define nmbclusters     nmbclust
157 #endif
158         /* Init the SCTP pcb in sctp_pcb.c */
159         u_long sb_max_adj;
160
161         sctp_pcb_init();
162
163 #ifndef __OpenBSD__
164         if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
165                 sctp_max_chunks_on_queue = nmbclusters;
166 #else
167 /*      if (nmbclust > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
168         sctp_max_chunks_on_queue = nmbclust; FIX ME */
169         sctp_max_chunks_on_queue = nmbclust * 2;
170 #endif
171         /*
172          * Allow a user to take no more than 1/2 the number of clusters
173          * or the SB_MAX whichever is smaller for the send window.
174          */
175         sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
176         sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
177 #ifndef __OpenBSD__
178                              ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT));
179 #else
180                              ((nmbclust/2) * SCTP_DEFAULT_MAXSEGMENT));
181 #endif
182         /*
183          * Now for the recv window, should we take the same amount?
184          * or should I do 1/2 the SB_MAX instead in the SB_MAX min above.
185          * For now I will just copy.
186          */
187         sctp_recvspace = sctp_sendspace;
188 #ifdef __OpenBSD__
189 #undef nmbclusters
190 #endif
191 }
192
193 #ifdef INET6
194 void
195 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
196 {
197         bzero(ip6, sizeof(*ip6));
198
199         ip6->ip6_vfc = IPV6_VERSION;
200         ip6->ip6_plen = ip->ip_len;
201         ip6->ip6_nxt = ip->ip_p;
202         ip6->ip6_hlim = ip->ip_ttl;
203         ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
204                 IPV6_ADDR_INT32_SMP;
205         ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
206         ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
207 }
208 #endif /* INET6 */
209
210 static void
211 sctp_split_chunks(struct sctp_association *asoc,
212                   struct sctp_stream_out *strm,
213                   struct sctp_tmit_chunk *chk)
214 {
215         struct sctp_tmit_chunk *new_chk;
216
217         /* First we need a chunk */
218         new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
219         if (new_chk == NULL) {
220                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
221                 return;
222         }
223         sctppcbinfo.ipi_count_chunk++;
224         sctppcbinfo.ipi_gencnt_chunk++;
225         /* Copy it all */
226         *new_chk = *chk;
227         /*  split the data */
228         new_chk->data = m_split(chk->data, (chk->send_size>>1), MB_DONTWAIT);
229         if (new_chk->data == NULL) {
230                 /* Can't split */
231                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
232                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk);
233                 sctppcbinfo.ipi_count_chunk--;
234                 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
235                         panic("Chunk count is negative");
236                 }
237                 sctppcbinfo.ipi_gencnt_chunk++;
238                 return;
239
240         }
241         /* Data is now split adjust sizes */
242         chk->send_size >>= 1;
243         new_chk->send_size >>= 1;
244
245         chk->book_size >>= 1;
246         new_chk->book_size >>= 1;
247
248         /* now adjust the marks */
249         chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
250         chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG;
251
252         new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG;
253         new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
254
255         /* Increase ref count if dest is set */
256         if (chk->whoTo) {
257                 new_chk->whoTo->ref_count++;
258         }
259         /* now drop it on the end of the list*/
260         asoc->stream_queue_cnt++;
261         TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next);
262 }
263
264 static void
265 sctp_notify_mbuf(struct sctp_inpcb *inp,
266                  struct sctp_tcb *stcb,
267                  struct sctp_nets *net,
268                  struct ip *ip,
269                  struct sctphdr *sh)
270
271 {
272         struct icmp *icmph;
273         int totsz;
274         uint16_t nxtsz;
275
276         /* protection */
277         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
278             (ip == NULL) || (sh == NULL)) {
279                 if (stcb != NULL)
280                         SCTP_TCB_UNLOCK(stcb);
281                 return;
282         }
283         /* First job is to verify the vtag matches what I would send */
284         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
285                 SCTP_TCB_UNLOCK(stcb);
286                 return;
287         }
288         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
289                                                sizeof(struct ip)));
290         if (icmph->icmp_type != ICMP_UNREACH) {
291                 /* We only care about unreachable */
292                 SCTP_TCB_UNLOCK(stcb);
293                 return;
294         }
295         if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
296                 /* not a unreachable message due to frag. */
297                 SCTP_TCB_UNLOCK(stcb);
298                 return;
299         }
300         totsz = ip->ip_len;
301         nxtsz = ntohs(icmph->icmp_seq);
302         if (nxtsz == 0) {
303                 /*
304                  * old type router that does not tell us what the next size
305                  * mtu is. Rats we will have to guess (in a educated fashion
306                  * of course)
307                  */
308                 nxtsz = find_next_best_mtu(totsz);
309         }
310
311         /* Stop any PMTU timer */
312         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
313
314         /* Adjust destination size limit */
315         if (net->mtu > nxtsz) {
316                 net->mtu = nxtsz;
317         }
318         /* now what about the ep? */
319         if (stcb->asoc.smallest_mtu > nxtsz) {
320                 struct sctp_tmit_chunk *chk, *nchk;
321                 struct sctp_stream_out *strm;
322                 /* Adjust that too */
323                 stcb->asoc.smallest_mtu = nxtsz;
324                 /* now off to subtract IP_DF flag if needed */
325
326                 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
327                         if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
328                                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
329                         }
330                 }
331                 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
332                         if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
333                                 /*
334                                  * For this guy we also mark for immediate
335                                  * resend since we sent to big of chunk
336                                  */
337                                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
338                                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
339                                         stcb->asoc.sent_queue_retran_cnt++;
340                                 }
341                                 chk->sent = SCTP_DATAGRAM_RESEND;
342                                 chk->rec.data.doing_fast_retransmit = 0;
343
344                                 /* Clear any time so NO RTT is being done */
345                                 chk->do_rtt = 0;
346                                 stcb->asoc.total_flight -= chk->book_size;
347                                 if (stcb->asoc.total_flight < 0) {
348                                         stcb->asoc.total_flight = 0;
349                                 }
350                                 stcb->asoc.total_flight_count--;
351                                 if (stcb->asoc.total_flight_count < 0) {
352                                         stcb->asoc.total_flight_count = 0;
353                                 }
354                                 net->flight_size -= chk->book_size;
355                                 if (net->flight_size < 0) {
356                                         net->flight_size = 0;
357                                 }
358                         }
359                 }
360                 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
361                         chk = TAILQ_FIRST(&strm->outqueue);
362                         while (chk) {
363                                 nchk = TAILQ_NEXT(chk, sctp_next);
364                                 if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) {
365                                         sctp_split_chunks(&stcb->asoc, strm, chk);
366                                 }
367                                 chk = nchk;
368                         }
369                 }
370         }
371         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
372         SCTP_TCB_UNLOCK(stcb);
373 }
374
375
376 void
377 sctp_notify(struct sctp_inpcb *inp,
378             int error,
379             struct sctphdr *sh,
380             struct sockaddr *to,
381             struct sctp_tcb *stcb,
382             struct sctp_nets *net)
383 {
384         /* protection */
385         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
386             (sh == NULL) || (to == NULL)) {
387 #ifdef SCTP_DEBUG
388                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
389                         kprintf("sctp-notify, bad call\n");
390                 }
391 #endif /* SCTP_DEBUG */
392                 return;
393         }
394         /* First job is to verify the vtag matches what I would send */
395         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
396                 return;
397         }
398
399 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
400
401         if ((error == EHOSTUNREACH) ||  /* Host is not reachable */
402             (error == EHOSTDOWN) ||     /* Host is down */
403             (error == ECONNREFUSED) ||  /* Host refused the connection, (not an abort?) */
404             (error == ENOPROTOOPT)      /* SCTP is not present on host */
405                 ) {
406                 /*
407                  * Hmm reachablity problems we must examine closely.
408                  * If its not reachable, we may have lost a network.
409                  * Or if there is NO protocol at the other end named SCTP.
410                  * well we consider it a OOTB abort.
411                  */
412                 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
413                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
414                                 /* Ok that destination is NOT reachable */
415                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
416                                 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
417                                 net->error_count = net->failure_threshold + 1;
418                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
419                                                 stcb, SCTP_FAILED_THRESHOLD,
420                                                 (void *)net);
421                         }
422                         if (stcb)
423                                 SCTP_TCB_UNLOCK(stcb);
424                 } else {
425                         /*
426                          * Here the peer is either playing tricks on us,
427                          * including an address that belongs to someone who
428                          * does not support SCTP OR was a userland
429                          * implementation that shutdown and now is dead. In
430                          * either case treat it like a OOTB abort with no TCB
431                          */
432                         sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
433                         sctp_free_assoc(inp, stcb);
434                         /* no need to unlock here, since the TCB is gone */
435                 }
436         } else {
437                 /* Send all others to the app */
438                 if (inp->sctp_socket) {
439                         SOCK_LOCK(inp->sctp_socket);
440                         inp->sctp_socket->so_error = error;
441                         sctp_sowwakeup(inp, inp->sctp_socket);
442                         SOCK_UNLOCK(inp->sctp_socket);
443                 }
444                 if (stcb)
445                         SCTP_TCB_UNLOCK(stcb);
446         }
447 }
448
449 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
450 void
451 #else
452 void *
453 #endif
454 sctp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
455 {
456         struct ip *ip = vip;
457         struct sctphdr *sh;
458
459
460         if (sa->sa_family != AF_INET ||
461             ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
462 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
463                 return;
464 #else
465                 return (NULL);
466 #endif
467         }
468
469         if (PRC_IS_REDIRECT(cmd)) {
470                 ip = 0;
471         } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
472 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
473                 return;
474 #else
475                 return (NULL);
476 #endif
477         }
478         if (ip) {
479                 struct sctp_inpcb *inp;
480                 struct sctp_tcb *stcb;
481                 struct sctp_nets *net;
482                 struct sockaddr_in to, from;
483
484                 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
485                 bzero(&to, sizeof(to));
486                 bzero(&from, sizeof(from));
487                 from.sin_family = to.sin_family = AF_INET;
488                 from.sin_len = to.sin_len = sizeof(to);
489                 from.sin_port = sh->src_port;
490                 from.sin_addr = ip->ip_src;
491                 to.sin_port = sh->dest_port;
492                 to.sin_addr = ip->ip_dst;
493
494                 /*
495                  * 'to' holds the dest of the packet that failed to be sent.
496                  * 'from' holds our local endpoint address.
497                  * Thus we reverse the to and the from in the lookup.
498                  */
499                 crit_enter();
500                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
501                                                     (struct sockaddr *)&to,
502                                                     &inp, &net, 1);
503                 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
504                         if (cmd != PRC_MSGSIZE) {
505                                 int cm;
506                                 if (cmd == PRC_HOSTDEAD) {
507                                         cm = EHOSTUNREACH;
508                                 } else {
509                                         cm = inetctlerrmap[cmd];
510                                 }
511                                 sctp_notify(inp, cm, sh,
512                                             (struct sockaddr *)&to, stcb,
513                                             net);
514                         } else {
515                                 /* handle possible ICMP size messages */
516                                 sctp_notify_mbuf(inp, stcb, net, ip, sh);
517                         }
518                 } else {
519 #if (defined(__FreeBSD__) && __FreeBSD_version < 500000) || defined(__DragonFly__)
520                         /* XXX must be fixed for 5.x and higher, leave for 4.x */
521                         if (PRC_IS_REDIRECT(cmd) && inp) {
522                                 in_rtchange((struct inpcb *)inp,
523                                             inetctlerrmap[cmd]);
524                         }
525 #endif
526                         if ((stcb == NULL) && (inp != NULL)) {
527                                 /* reduce ref-count */
528                                 SCTP_INP_WLOCK(inp);
529                                 SCTP_INP_DECR_REF(inp);
530                                 SCTP_INP_WUNLOCK(inp);
531                         }
532
533                 }
534                 crit_exit();
535         }
536 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
537         return;
538 #else
539         return (NULL);
540 #endif
541 }
542
543 #if defined(__FreeBSD__) || defined(__DragonFly__)
544 static int
545 sctp_getcred(SYSCTL_HANDLER_ARGS)
546 {
547         struct sockaddr_in addrs[2];
548         struct sctp_inpcb *inp;
549         struct sctp_nets *net;
550         struct sctp_tcb *stcb;
551         int error;
552
553 #if __FreeBSD_version >= 500000 || defined(__DragonFly__)
554         error = suser(req->td);
555 #else
556         error = suser(req->p);
557 #endif
558         if (error)
559                 return (error);
560         error = SYSCTL_IN(req, addrs, sizeof(addrs));
561         if (error)
562                 return (error);
563
564         crit_enter();
565         stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
566                                            sintosa(&addrs[1]),
567                                            &inp, &net, 1);
568         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
569                 if ((inp != NULL) && (stcb == NULL)) {
570                         /* reduce ref-count */
571                         SCTP_INP_WLOCK(inp);
572                         SCTP_INP_DECR_REF(inp);
573                         SCTP_INP_WUNLOCK(inp);
574                 }
575                 error = ENOENT;
576                 goto out;
577         }
578         error = SYSCTL_OUT(req, inp->sctp_socket->so_cred, sizeof(struct ucred));
579         SCTP_TCB_UNLOCK(stcb);
580 out:
581         crit_exit();
582         return (error);
583 }
584
585 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
586             0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
587 #endif /* #if defined(__FreeBSD__) || defined(__DragonFly__) */
588
589 /*
590  * sysctl definitions
591  */
592 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
593
594 SYSCTL_DECL(_net_inet);
595
596 SYSCTL_NODE(_net_inet, OID_AUTO, sctp, CTLFLAG_RD, 0,
597         "sctp values");
598
599 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxdgram, CTLFLAG_RW,
600            &sctp_sendspace, 0, "Maximum outgoing SCTP buffer size");
601
602 SYSCTL_INT(_net_inet_sctp, OID_AUTO, recvspace, CTLFLAG_RW,
603            &sctp_recvspace, 0, "Maximum incoming SCTP buffer size");
604
605 SYSCTL_INT(_net_inet_sctp, OID_AUTO, auto_asconf, CTLFLAG_RW,
606            &sctp_auto_asconf, 0, "Enable SCTP Auto-ASCONF");
607
608 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_enable, CTLFLAG_RW,
609            &sctp_ecn, 0, "Enable SCTP ECN");
610
611 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_nonce, CTLFLAG_RW,
612            &sctp_ecn_nonce, 0, "Enable SCTP ECN Nonce");
613
614 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_sacks, CTLFLAG_RW,
615            &sctp_strict_sacks, 0, "Enable SCTP Strict SACK checking");
616
617 SYSCTL_INT(_net_inet_sctp, OID_AUTO, loopback_nocsum, CTLFLAG_RW,
618            &sctp_no_csum_on_loopback, 0,
619            "Enable NO Csum on packets sent on loopback");
620
621 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_init, CTLFLAG_RW,
622            &sctp_strict_init, 0,
623            "Enable strict INIT/INIT-ACK singleton enforcement");
624
625 SYSCTL_INT(_net_inet_sctp, OID_AUTO, peer_chkoh, CTLFLAG_RW,
626            &sctp_peer_chunk_oh, 0,
627            "Amount to debit peers rwnd per chunk sent");
628
629 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxburst, CTLFLAG_RW,
630            &sctp_max_burst_default, 0,
631            "Default max burst for sctp endpoints");
632
633 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxchunks, CTLFLAG_RW,
634            &sctp_max_chunks_on_queue, 0,
635            "Default max chunks on queue per asoc");
636
637 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, delayed_sack_time, CTLFLAG_RW,
638             &sctp_delayed_sack_time_default, 0,
639             "Default delayed SACK timer in msec");
640
641 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, heartbeat_interval, CTLFLAG_RW,
642             &sctp_heartbeat_interval_default, 0,
643             "Default heartbeat interval in msec");
644
645 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, pmtu_raise_time, CTLFLAG_RW,
646             &sctp_pmtu_raise_time_default, 0,
647             "Default PMTU raise timer in sec");
648
649 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, shutdown_guard_time, CTLFLAG_RW,
650             &sctp_shutdown_guard_time_default, 0,
651             "Default shutdown guard timer in sec");
652
653 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, secret_lifetime, CTLFLAG_RW,
654             &sctp_secret_lifetime_default, 0,
655             "Default secret lifetime in sec");
656
657 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_max, CTLFLAG_RW,
658             &sctp_rto_max_default, 0,
659             "Default maximum retransmission timeout in msec");
660
661 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_min, CTLFLAG_RW,
662             &sctp_rto_min_default, 0,
663             "Default minimum retransmission timeout in msec");
664
665 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_initial, CTLFLAG_RW,
666             &sctp_rto_initial_default, 0,
667             "Default initial retransmission timeout in msec");
668
669 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rto_max, CTLFLAG_RW,
670             &sctp_init_rto_max_default, 0,
671             "Default maximum retransmission timeout during association setup in msec");
672
673 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, valid_cookie_life, CTLFLAG_RW,
674             &sctp_valid_cookie_life_default, 0,
675             "Default cookie lifetime in sec");
676
677 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rtx_max, CTLFLAG_RW,
678             &sctp_init_rtx_max_default, 0,
679             "Default maximum number of retransmission for INIT chunks");
680
681 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, assoc_rtx_max, CTLFLAG_RW,
682             &sctp_assoc_rtx_max_default, 0,
683             "Default maximum number of retransmissions per association");
684
685 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, path_rtx_max, CTLFLAG_RW,
686             &sctp_path_rtx_max_default, 0,
687             "Default maximum of retransmissions per path");
688
689 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, nr_outgoing_streams, CTLFLAG_RW,
690             &sctp_nr_outgoing_streams_default, 0,
691             "Default number of outgoing streams");
692
693 #ifdef SCTP_DEBUG
694 SYSCTL_INT(_net_inet_sctp, OID_AUTO, debug, CTLFLAG_RW,
695            &sctp_debug_on, 0, "Configure debug output");
696 #endif /* SCTP_DEBUG */
697 #endif
698
699 static int
700 sctp_abort(struct socket *so)
701 {
702         struct sctp_inpcb *inp;
703
704         inp = (struct sctp_inpcb *)so->so_pcb;
705         if (inp == 0)
706                 return EINVAL;  /* ??? possible? panic instead? */
707
708         crit_enter();
709         sctp_inpcb_free(inp, 1);
710         crit_exit();
711         return 0;
712 }
713
714 static int
715 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
716 sctp_attach(struct socket *so, int proto, struct thread *p)
717 #elif defined(__DragonFly__)
718 sctp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
719 #else
720 sctp_attach(struct socket *so, int proto, struct proc *p)
721 #endif
722 {
723         struct sctp_inpcb *inp;
724         struct inpcb *ip_inp;
725         int error;
726
727         crit_enter();
728         inp = (struct sctp_inpcb *)so->so_pcb;
729         if (inp != 0) {
730                 crit_exit();
731                 return EINVAL;
732         }
733         error = soreserve(so, sctp_sendspace, sctp_recvspace, NULL);
734         if (error) {
735                 crit_exit();
736                 return error;
737         }
738         error = sctp_inpcb_alloc(so);
739         if (error) {
740                 crit_exit();
741                 return error;
742         }
743         inp = (struct sctp_inpcb *)so->so_pcb;
744         SCTP_INP_WLOCK(inp);
745
746         inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;    /* I'm not v6! */
747         ip_inp = &inp->ip_inp.inp;
748 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
749         ip_inp->inp_vflag |= INP_IPV4;
750         ip_inp->inp_ip_ttl = ip_defttl;
751 #else
752         inp->inp_vflag |= INP_IPV4;
753         inp->inp_ip_ttl = ip_defttl;
754 #endif
755
756 #ifdef IPSEC
757 #if !(defined(__OpenBSD__) || defined(__APPLE__))
758         error = ipsec_init_policy(so, &ip_inp->inp_sp);
759         if (error != 0) {
760                 sctp_inpcb_free(inp, 1);
761                 return error;
762         }
763 #endif
764 #endif /*IPSEC*/
765         SCTP_INP_WUNLOCK(inp);
766 #if defined(__NetBSD__)
767         so->so_send = sctp_sosend;
768 #endif
769         crit_exit();
770         return 0;
771 }
772
773 static int
774 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
775 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
776 {
777 #elif defined(__FreeBSD__) || defined(__APPLE__)
778 sctp_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
779 {
780 #else
781 sctp_bind(struct socket *so, struct mbuf *nam, struct proc *p)
782 {
783         struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
784 #endif
785         struct sctp_inpcb *inp;
786         int error;
787
788 #ifdef INET6
789         if (addr && addr->sa_family != AF_INET)
790                 /* must be a v4 address! */
791                 return EINVAL;
792 #endif /* INET6 */
793
794         inp = (struct sctp_inpcb *)so->so_pcb;
795         if (inp == 0)
796                 return EINVAL;
797
798         crit_enter();
799         error = sctp_inpcb_bind(so, addr, p);
800         crit_exit();
801         return error;
802 }
803
804
805 static int
806 sctp_detach(struct socket *so)
807 {
808         struct sctp_inpcb *inp;
809
810         inp = (struct sctp_inpcb *)so->so_pcb;
811         if (inp == 0)
812                 return EINVAL;
813         crit_enter();
814         if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
815             (so->so_rcv.ssb_cc > 0)) {
816                 sctp_inpcb_free(inp, 1);
817         } else {
818                 sctp_inpcb_free(inp, 0);
819         }
820         crit_exit();
821         return 0;
822 }
823
824 int
825 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
826 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
827           struct mbuf *control, struct thread *p);
828 #else
829 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
830           struct mbuf *control, struct proc *p);
831 #endif
832
833 int
834 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
835 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
836           struct mbuf *control, struct thread *p)
837 {
838 #else
839 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
840           struct mbuf *control, struct proc *p)
841 {
842 #endif
843         struct sctp_inpcb *inp;
844         int error;
845         inp = (struct sctp_inpcb *)so->so_pcb;
846         if (inp == 0) {
847                 if (control) {
848                         sctp_m_freem(control);
849                         control = NULL;
850                 }
851                 sctp_m_freem(m);
852                 return EINVAL;
853         }
854         /* Got to have an to address if we are NOT a connected socket */
855         if ((addr == NULL) &&
856             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
857              (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
858                 ) {
859                 goto connected_type;
860         } else if (addr == NULL) {
861                 error = EDESTADDRREQ;
862                 sctp_m_freem(m);
863                 if (control) {
864                         sctp_m_freem(control);
865                         control = NULL;
866                 }
867                 return (error);
868         }
869 #ifdef INET6
870         if (addr->sa_family != AF_INET) {
871                 /* must be a v4 address! */
872                 sctp_m_freem(m);
873                 if (control) {
874                         sctp_m_freem(control);
875                         control = NULL;
876                 }
877                 error = EDESTADDRREQ;
878                 return EINVAL;
879         }
880 #endif /* INET6 */
881  connected_type:
882         /* now what about control */
883         if (control) {
884                 if (inp->control) {
885                         kprintf("huh? control set?\n");
886                         sctp_m_freem(inp->control);
887                         inp->control = NULL;
888                 }
889                 inp->control = control;
890         }
891         /* add it in possibly */
892         if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) {
893                 struct mbuf *x;
894                 int c_len;
895
896                 c_len = 0;
897                 /* How big is it */
898                 for (x=m;x;x = x->m_next) {
899                         c_len += x->m_len;
900                 }
901                 inp->pkt->m_pkthdr.len += c_len;
902         }
903         /* Place the data */
904         if (inp->pkt) {
905                 inp->pkt_last->m_next = m;
906                 inp->pkt_last = m;
907         } else {
908                 inp->pkt_last = inp->pkt = m;
909         }
910         if (
911 #if defined (__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
912             /* FreeBSD uses a flag passed */
913             ((flags & PRUS_MORETOCOME) == 0)
914 #elif defined( __NetBSD__)
915             /* NetBSD uses the so_state field */
916             ((so->so_state & SS_MORETOCOME) == 0)
917 #else
918             1   /* Open BSD does not have any "more to come" indication */
919 #endif
920             ) {
921                 /*
922                  * note with the current version this code will only be used
923                  * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
924                  * re-defining sosend to use the sctp_sosend. One can
925                  * optionally switch back to this code (by changing back the
926                  * definitions) but this is not advisable.
927              */
928                 int ret;
929                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
930                 inp->pkt = NULL;
931                 inp->control = NULL;
932                 return (ret);
933         } else {
934                 return (0);
935         }
936 }
937
938 static int
939 sctp_disconnect(struct socket *so)
940 {
941         struct sctp_inpcb *inp;
942
943         crit_enter();
944         inp = (struct sctp_inpcb *)so->so_pcb;
945         if (inp == NULL) {
946                 crit_exit();
947                 return (ENOTCONN);
948         }
949         SCTP_INP_RLOCK(inp);
950         if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
951                 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
952                         /* No connection */
953                         crit_exit();
954                         SCTP_INP_RUNLOCK(inp);
955                         return (0);
956                 } else {
957                         int some_on_streamwheel = 0;
958                         struct sctp_association *asoc;
959                         struct sctp_tcb *stcb;
960
961                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
962                         if (stcb == NULL) {
963                                 crit_exit();
964                                 SCTP_INP_RUNLOCK(inp);
965                                 return (EINVAL);
966                         }
967                         asoc = &stcb->asoc;
968                         SCTP_TCB_LOCK(stcb);
969                         if (((so->so_options & SO_LINGER) &&
970                              (so->so_linger == 0)) ||
971                             (so->so_rcv.ssb_cc > 0)) {
972                                 if (SCTP_GET_STATE(asoc) !=
973                                     SCTP_STATE_COOKIE_WAIT) {
974                                         /* Left with Data unread */
975                                         struct mbuf *err;
976                                         err = NULL;
977                                         MGET(err, MB_DONTWAIT, MT_DATA);
978                                         if (err) {
979                                                 /* Fill in the user initiated abort */
980                                                 struct sctp_paramhdr *ph;
981                                                 ph = mtod(err, struct sctp_paramhdr *);
982                                                 err->m_len = sizeof(struct sctp_paramhdr);
983                                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
984                                                 ph->param_length = htons(err->m_len);
985                                         }
986                                         sctp_send_abort_tcb(stcb, err);
987                                 }
988                                 SCTP_INP_RUNLOCK(inp);
989                                 sctp_free_assoc(inp, stcb);
990                                 /* No unlock tcb assoc is gone */
991                                 crit_exit();
992                                 return (0);
993                         }
994                         if (!TAILQ_EMPTY(&asoc->out_wheel)) {
995                                 /* Check to see if some data queued */
996                                 struct sctp_stream_out *outs;
997                                 TAILQ_FOREACH(outs, &asoc->out_wheel,
998                                               next_spoke) {
999                                         if (!TAILQ_EMPTY(&outs->outqueue)) {
1000                                                 some_on_streamwheel = 1;
1001                                                 break;
1002                                         }
1003                                 }
1004                         }
1005
1006                         if (TAILQ_EMPTY(&asoc->send_queue) &&
1007                             TAILQ_EMPTY(&asoc->sent_queue) &&
1008                             (some_on_streamwheel == 0)) {
1009                                 /* there is nothing queued to send, so done */
1010                                 if ((SCTP_GET_STATE(asoc) !=
1011                                      SCTP_STATE_SHUTDOWN_SENT) &&
1012                                     (SCTP_GET_STATE(asoc) !=
1013                                      SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1014                                         /* only send SHUTDOWN 1st time thru */
1015 #ifdef SCTP_DEBUG
1016                                         if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1017                                                 kprintf("%s:%d sends a shutdown\n",
1018                                                        __FILE__,
1019                                                        __LINE__
1020                                                         );
1021                                         }
1022 #endif
1023                                         sctp_send_shutdown(stcb,
1024                                                            stcb->asoc.primary_destination);
1025                                         sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1026                                         asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1027                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1028                                                          stcb->sctp_ep, stcb,
1029                                                          asoc->primary_destination);
1030                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1031                                                          stcb->sctp_ep, stcb,
1032                                                          asoc->primary_destination);
1033                                 }
1034                         } else {
1035                                 /*
1036                                  * we still got (or just got) data to send,
1037                                  * so set SHUTDOWN_PENDING
1038                                  */
1039                                 /*
1040                                  * XXX sockets draft says that MSG_EOF should
1041                                  * be sent with no data.
1042                                  * currently, we will allow user data to be
1043                                  * sent first and move to SHUTDOWN-PENDING
1044                                  */
1045                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1046                         }
1047                         SCTP_TCB_UNLOCK(stcb);
1048                         SCTP_INP_RUNLOCK(inp);
1049                         crit_exit();
1050                         return (0);
1051                 }
1052                 /* not reached */
1053         } else {
1054                 /* UDP model does not support this */
1055                 SCTP_INP_RUNLOCK(inp);
1056                 crit_exit();
1057                 return EOPNOTSUPP;
1058         }
1059 }
1060
1061 int
1062 sctp_shutdown(struct socket *so)
1063 {
1064         struct sctp_inpcb *inp;
1065
1066         crit_enter();
1067         inp = (struct sctp_inpcb *)so->so_pcb;
1068         if (inp == 0) {
1069                 crit_exit();
1070                 return EINVAL;
1071         }
1072         SCTP_INP_RLOCK(inp);
1073         /* For UDP model this is a invalid call */
1074         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
1075                 /* Restore the flags that the soshutdown took away. */
1076 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
1077                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1078 #else
1079                 so->so_state &= ~SS_CANTRCVMORE;
1080 #endif
1081                 /* This proc will wakeup for read and do nothing (I hope) */
1082                 crit_exit();
1083                 SCTP_INP_RUNLOCK(inp);
1084                 return (EOPNOTSUPP);
1085         }
1086         /*
1087          * Ok if we reach here its the TCP model and it is either a SHUT_WR
1088          * or SHUT_RDWR. This means we put the shutdown flag against it.
1089          */
1090         {
1091                 int some_on_streamwheel = 0;
1092                 struct sctp_tcb *stcb;
1093                 struct sctp_association *asoc;
1094                 socantsendmore(so);
1095
1096                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1097                 if (stcb == NULL) {
1098                         /*
1099                          * Ok we hit the case that the shutdown call was made
1100                          * after an abort or something. Nothing to do now.
1101                          */
1102                         crit_exit();
1103                         return (0);
1104                 }
1105                 SCTP_TCB_LOCK(stcb);
1106                 asoc = &stcb->asoc;
1107
1108                 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
1109                         /* Check to see if some data queued */
1110                         struct sctp_stream_out *outs;
1111                         TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
1112                                 if (!TAILQ_EMPTY(&outs->outqueue)) {
1113                                         some_on_streamwheel = 1;
1114                                         break;
1115                                 }
1116                         }
1117                 }
1118                 if (TAILQ_EMPTY(&asoc->send_queue) &&
1119                     TAILQ_EMPTY(&asoc->sent_queue) &&
1120                     (some_on_streamwheel == 0)) {
1121                         /* there is nothing queued to send, so I'm done... */
1122                         if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1123                                 /* only send SHUTDOWN the first time through */
1124 #ifdef SCTP_DEBUG
1125                                 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1126                                         kprintf("%s:%d sends a shutdown\n",
1127                                                __FILE__,
1128                                                __LINE__
1129                                                 );
1130                                 }
1131 #endif
1132                                 sctp_send_shutdown(stcb,
1133                                                    stcb->asoc.primary_destination);
1134                                 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1135                                 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1136                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1137                                                  stcb->sctp_ep, stcb,
1138                                                  asoc->primary_destination);
1139                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1140                                                  stcb->sctp_ep, stcb,
1141                                                  asoc->primary_destination);
1142                         }
1143                 } else {
1144                         /*
1145                          * we still got (or just got) data to send, so
1146                          * set SHUTDOWN_PENDING
1147                          */
1148                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1149                 }
1150                 SCTP_TCB_UNLOCK(stcb);
1151         }
1152         SCTP_INP_RUNLOCK(inp);
1153         crit_exit();
1154         return 0;
1155 }
1156
1157 /*
1158  * copies a "user" presentable address and removes embedded scope, etc.
1159  * returns 0 on success, 1 on error
1160  */
1161 static uint32_t
1162 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1163 {
1164         struct sockaddr_in6 lsa6;
1165         sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1166                                                    &lsa6);
1167         memcpy(ss, sa, sa->sa_len);
1168         return (0);
1169 }
1170
1171
1172 #if defined(__NetBSD__) || defined(__OpenBSD__)
1173 /*
1174  * On NetBSD and OpenBSD in6_sin_2_v4mapsin6() not used and not exported,
1175  * so we have to export it here.
1176  */
1177 void    in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6);
1178 #endif
1179
1180 static int
1181 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1182                        struct sctp_tcb *stcb,
1183                        int limit,
1184                        struct sockaddr_storage *sas)
1185 {
1186         struct ifnet *ifn;
1187         int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
1188         int ipv4_addr_legal, ipv6_addr_legal;
1189         actual = 0;
1190         if (limit <= 0)
1191                 return (actual);
1192
1193         if (stcb) {
1194                 /* Turn on all the appropriate scope */
1195                 loopback_scope = stcb->asoc.loopback_scope;
1196                 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1197                 local_scope = stcb->asoc.local_scope;
1198                 site_scope = stcb->asoc.site_scope;
1199         } else {
1200                 /* Turn on ALL scope, since we look at the EP */
1201                 loopback_scope = ipv4_local_scope = local_scope =
1202                         site_scope = 1;
1203         }
1204         ipv4_addr_legal = ipv6_addr_legal = 0;
1205         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1206                 ipv6_addr_legal = 1;
1207                 if (
1208 #if defined(__OpenBSD__)
1209                 (0) /* we always do dual bind */
1210 #elif defined (__NetBSD__)
1211                 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1212 #else
1213                 (((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
1214 #endif
1215                 == 0) {
1216                         ipv4_addr_legal = 1;
1217                 }
1218         } else {
1219                 ipv4_addr_legal = 1;
1220         }
1221
1222         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1223                 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1224                         struct ifaddr_container *ifac;
1225
1226                         if ((loopback_scope == 0) &&
1227                             (ifn->if_type == IFT_LOOP)) {
1228                                 /* Skip loopback if loopback_scope not set */
1229                                 continue;
1230                         }
1231                         TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid],
1232                                       ifa_link) {
1233                                 struct ifaddr *ifa = ifac->ifa;
1234
1235                                 if (stcb) {
1236                                 /*
1237                                  * For the BOUND-ALL case, the list
1238                                  * associated with a TCB is Always
1239                                  * considered a reverse list.. i.e.
1240                                  * it lists addresses that are NOT
1241                                  * part of the association. If this
1242                                  * is one of those we must skip it.
1243                                  */
1244                                         if (sctp_is_addr_restricted(stcb,
1245                                                                     ifa->ifa_addr)) {
1246                                                 continue;
1247                                         }
1248                                 }
1249                                 if ((ifa->ifa_addr->sa_family == AF_INET) &&
1250                                     (ipv4_addr_legal)) {
1251                                         struct sockaddr_in *sin;
1252                                         sin = (struct sockaddr_in *)ifa->ifa_addr;
1253                                         if (sin->sin_addr.s_addr == 0) {
1254                                                 /* we skip unspecifed addresses */
1255                                                 continue;
1256                                         }
1257                                         if ((ipv4_local_scope == 0) &&
1258                                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1259                                                 continue;
1260                                         }
1261                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1262                                                 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1263                                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1264                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1265                                                 actual += sizeof(sizeof(struct sockaddr_in6));
1266                                         } else {
1267                                                 memcpy(sas, sin, sizeof(*sin));
1268                                                 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1269                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1270                                                 actual += sizeof(*sin);
1271                                         }
1272                                         if (actual >= limit) {
1273                                                 return (actual);
1274                                         }
1275                                 } else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
1276                                            (ipv6_addr_legal)) {
1277                                         struct sockaddr_in6 *sin6, lsa6;
1278                                         sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1279                                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1280                                                 /* we skip unspecifed addresses */
1281                                                 continue;
1282                                         }
1283                                         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1284                                                 if (local_scope == 0)
1285                                                         continue;
1286                                                 if (sin6->sin6_scope_id == 0) {
1287                                                         lsa6 = *sin6;
1288                                                         if (in6_recoverscope(&lsa6,
1289                                                                              &lsa6.sin6_addr,
1290                                                                              NULL))
1291                                                                 /* bad link local address */
1292                                                                 continue;
1293                                                         sin6 = &lsa6;
1294                                                 }
1295                                         }
1296                                         if ((site_scope == 0) &&
1297                                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1298                                                 continue;
1299                                         }
1300                                         memcpy(sas, sin6, sizeof(*sin6));
1301                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1302                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1303                                         actual += sizeof(*sin6);
1304                                         if (actual >= limit) {
1305                                                 return (actual);
1306                                         }
1307                                 }
1308                         }
1309                 }
1310         } else {
1311                 struct sctp_laddr *laddr;
1312                 /*
1313                  * If we have a TCB and we do NOT support ASCONF (it's
1314                  * turned off or otherwise) then the list is always the
1315                  * true list of addresses (the else case below).  Otherwise
1316                  * the list on the association is a list of addresses that
1317                  * are NOT part of the association.
1318                  */
1319                 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1320                         /* The list is a NEGATIVE list */
1321                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1322                                 if (stcb) {
1323                                         if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
1324                                                 continue;
1325                                         }
1326                                 }
1327                                 if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
1328                                         continue;
1329
1330                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1331                                 sas = (struct sockaddr_storage *)((caddr_t)sas +
1332                                                                   laddr->ifa->ifa_addr->sa_len);
1333                                 actual += laddr->ifa->ifa_addr->sa_len;
1334                                 if (actual >= limit) {
1335                                         return (actual);
1336                                 }
1337                         }
1338                 } else {
1339                         /* The list is a positive list if present */
1340                         if (stcb) {
1341                                 /* Must use the specific association list */
1342                                 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1343                                              sctp_nxt_addr) {
1344                                         if (sctp_fill_user_address(sas,
1345                                                                    laddr->ifa->ifa_addr))
1346                                                 continue;
1347                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1348                                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1349                                                                           laddr->ifa->ifa_addr->sa_len);
1350                                         actual += laddr->ifa->ifa_addr->sa_len;
1351                                         if (actual >= limit) {
1352                                                 return (actual);
1353                                         }
1354                                 }
1355                         } else {
1356                                 /* No endpoint so use the endpoints individual list */
1357                                 LIST_FOREACH(laddr, &inp->sctp_addr_list,
1358                                              sctp_nxt_addr) {
1359                                         if (sctp_fill_user_address(sas,
1360                                                                    laddr->ifa->ifa_addr))
1361                                                 continue;
1362                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1363                                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1364                                                                           laddr->ifa->ifa_addr->sa_len);
1365                                         actual += laddr->ifa->ifa_addr->sa_len;
1366                                         if (actual >= limit) {
1367                                                 return (actual);
1368                                         }
1369                                 }
1370                         }
1371                 }
1372         }
1373         return (actual);
1374 }
1375
1376 static int
1377 sctp_count_max_addresses(struct sctp_inpcb *inp)
1378 {
1379         int cnt = 0;
1380         /*
1381          * In both sub-set bound an bound_all cases we return the MAXIMUM
1382          * number of addresses that you COULD get. In reality the sub-set
1383          * bound may have an exclusion list for a given TCB OR in the
1384          * bound-all case a TCB may NOT include the loopback or other
1385          * addresses as well.
1386          */
1387         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1388                 struct ifnet *ifn;
1389
1390                 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1391                         struct ifaddr_container *ifac;
1392
1393                         TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1394                                 struct ifaddr *ifa = ifac->ifa;
1395
1396                                 /* Count them if they are the right type */
1397                                 if (ifa->ifa_addr->sa_family == AF_INET) {
1398                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1399                                                 cnt += sizeof(struct sockaddr_in6);
1400                                         else
1401                                                 cnt += sizeof(struct sockaddr_in);
1402
1403                                 } else if (ifa->ifa_addr->sa_family == AF_INET6)
1404                                         cnt += sizeof(struct sockaddr_in6);
1405                         }
1406                 }
1407         } else {
1408                 struct sctp_laddr *laddr;
1409                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1410                         if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
1411                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1412                                         cnt += sizeof(struct sockaddr_in6);
1413                                 else
1414                                         cnt += sizeof(struct sockaddr_in);
1415
1416                         } else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
1417                                 cnt += sizeof(struct sockaddr_in6);
1418                 }
1419         }
1420         return (cnt);
1421 }
1422
1423 static int
1424 sctp_do_connect_x(struct socket *so,
1425                   struct sctp_inpcb *inp,
1426                   struct mbuf *m,
1427 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1428                   struct thread *p,
1429 #else
1430                   struct proc *p,
1431 #endif
1432                   int delay
1433         )
1434 {
1435         int error = 0;
1436         struct sctp_tcb *stcb = NULL;
1437         struct sockaddr *sa;
1438         int num_v6=0, num_v4=0, *totaddrp, totaddr, i, incr, at;
1439 #ifdef SCTP_DEBUG
1440         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1441                 kprintf("Connectx called\n");
1442         }
1443 #endif /* SCTP_DEBUG */
1444
1445         crit_enter();
1446         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1447             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1448                 /* We are already connected AND the TCP model */
1449                 crit_exit();
1450                 return (EADDRINUSE);
1451         }
1452         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1453                 SCTP_INP_RLOCK(inp);
1454                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1455                 SCTP_INP_RUNLOCK(inp);
1456         }
1457         if (stcb) {
1458                 crit_exit();
1459                 return (EALREADY);
1460
1461         }
1462         SCTP_ASOC_CREATE_LOCK(inp);
1463         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1464             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1465                 SCTP_ASOC_CREATE_UNLOCK(inp);
1466                 crit_exit();
1467                 return (EFAULT);
1468         }
1469
1470         totaddrp = mtod(m, int *);
1471         totaddr = *totaddrp;
1472         sa = (struct sockaddr *)(totaddrp + 1);
1473         at = incr = 0;
1474         /* account and validate addresses */
1475         SCTP_INP_WLOCK(inp);
1476         SCTP_INP_INCR_REF(inp);
1477         SCTP_INP_WUNLOCK(inp);
1478         for (i = 0; i < totaddr; i++) {
1479                 if (sa->sa_family == AF_INET) {
1480                         num_v4++;
1481                         incr = sizeof(struct sockaddr_in);
1482                 } else if (sa->sa_family == AF_INET6) {
1483                         struct sockaddr_in6 *sin6;
1484                         sin6 = (struct sockaddr_in6 *)sa;
1485                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1486                                 /* Must be non-mapped for connectx */
1487                                 SCTP_ASOC_CREATE_UNLOCK(inp);
1488                                 crit_exit();
1489                                 return EINVAL;
1490                         }
1491                         num_v6++;
1492                         incr = sizeof(struct sockaddr_in6);
1493                 } else {
1494                         totaddr = i;
1495                         break;
1496                 }
1497                 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1498                 if (stcb != NULL) {
1499                         /* Already have or am bring up an association */
1500                         SCTP_ASOC_CREATE_UNLOCK(inp);
1501                         SCTP_TCB_UNLOCK(stcb);
1502                         crit_exit();
1503                         return (EALREADY);
1504                 }
1505                 if ((at + incr) > m->m_len) {
1506                         totaddr = i;
1507                         break;
1508                 }
1509                 sa = (struct sockaddr *)((caddr_t)sa + incr);
1510         }
1511         sa = (struct sockaddr *)(totaddrp + 1);
1512         SCTP_INP_WLOCK(inp);
1513         SCTP_INP_DECR_REF(inp);
1514         SCTP_INP_WUNLOCK(inp);
1515 #ifdef INET6
1516         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1517             (num_v6 > 0)) {
1518                 crit_exit();
1519                 SCTP_INP_WUNLOCK(inp);
1520                 SCTP_ASOC_CREATE_UNLOCK(inp);
1521                 return (EINVAL);
1522         }
1523         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1524             (num_v4 > 0)) {
1525                 struct in6pcb *inp6;
1526                 inp6 = (struct in6pcb *)inp;
1527                 if (
1528 #if defined(__OpenBSD__)
1529                         (0) /* we always do dual bind */
1530 #elif defined (__NetBSD__)
1531                         (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1532 #else
1533                         (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1534 #endif
1535                         ) {
1536                         /*
1537                          * if IPV6_V6ONLY flag, ignore connections
1538                          * destined to a v4 addr or v4-mapped addr
1539                          */
1540                         SCTP_INP_WUNLOCK(inp);
1541                         SCTP_ASOC_CREATE_UNLOCK(inp);
1542                         crit_exit();
1543                         return EINVAL;
1544                 }
1545         }
1546 #endif /* INET6 */
1547         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1548             SCTP_PCB_FLAGS_UNBOUND) {
1549                 /* Bind a ephemeral port */
1550                 SCTP_INP_WUNLOCK(inp);
1551                 error = sctp_inpcb_bind(so, NULL, p);
1552                 if (error) {
1553                         SCTP_ASOC_CREATE_UNLOCK(inp);
1554                         crit_exit();
1555                         return (error);
1556                 }
1557         } else {
1558                 SCTP_INP_WUNLOCK(inp);
1559         }
1560         /* We are GOOD to go */
1561         stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
1562         if (stcb == NULL) {
1563                 /* Gak! no memory */
1564                 SCTP_ASOC_CREATE_UNLOCK(inp);
1565                 crit_exit();
1566                 return (error);
1567         }
1568         /* move to second address */
1569         if (sa->sa_family == AF_INET)
1570                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1571         else
1572                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1573
1574         for (i = 1; i < totaddr; i++) {
1575                 if (sa->sa_family == AF_INET) {
1576                         incr = sizeof(struct sockaddr_in);
1577                         if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1578                                 /* assoc gone no un-lock */
1579                                 sctp_free_assoc(inp, stcb);
1580                                 SCTP_ASOC_CREATE_UNLOCK(inp);
1581                                 crit_exit();
1582                                 return (ENOBUFS);
1583                         }
1584
1585                 } else if (sa->sa_family == AF_INET6) {
1586                         incr = sizeof(struct sockaddr_in6);
1587                         if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1588                                 /* assoc gone no un-lock */
1589                                 sctp_free_assoc(inp, stcb);
1590                                 SCTP_ASOC_CREATE_UNLOCK(inp);
1591                                 crit_exit();
1592                                 return (ENOBUFS);
1593                         }
1594                 }
1595                 sa = (struct sockaddr *)((caddr_t)sa + incr);
1596         }
1597         stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1598         if (delay) {
1599                 /* doing delayed connection */
1600                 stcb->asoc.delayed_connection = 1;
1601                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1602         } else {
1603                 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1604                 sctp_send_initiate(inp, stcb);
1605         }
1606         SCTP_TCB_UNLOCK(stcb);
1607         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1608                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1609                 /* Set the connected flag so we can queue data */
1610                 soisconnecting(so);
1611         }
1612         SCTP_ASOC_CREATE_UNLOCK(inp);
1613         crit_exit();
1614         return error;
1615 }
1616
1617
1618 static int
1619 sctp_optsget(struct socket *so,
1620              int opt,
1621              struct mbuf **mp,
1622 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1623              struct thread *p
1624 #else
1625              struct proc *p
1626 #endif
1627         )
1628 {
1629         struct sctp_inpcb *inp;
1630         struct mbuf *m;
1631         int error, optval=0;
1632         struct sctp_tcb *stcb = NULL;
1633
1634         inp = (struct sctp_inpcb *)so->so_pcb;
1635         if (inp == 0)
1636                 return EINVAL;
1637         error = 0;
1638
1639         if (mp == NULL) {
1640 #ifdef SCTP_DEBUG
1641                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1642                         kprintf("optsget:MP is NULL EINVAL\n");
1643                 }
1644 #endif /* SCTP_DEBUG */
1645                 return (EINVAL);
1646         }
1647         m = *mp;
1648         if (m == NULL) {
1649                 /* Got to have a mbuf */
1650 #ifdef SCTP_DEBUG
1651                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1652                         kprintf("Huh no mbuf\n");
1653                 }
1654 #endif /* SCTP_DEBUG */
1655                 return (EINVAL);
1656         }
1657 #ifdef SCTP_DEBUG
1658         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1659                 kprintf("optsget opt:%lxx sz:%u\n", (unsigned long)opt,
1660                        m->m_len);
1661         }
1662 #endif /* SCTP_DEBUG */
1663
1664         switch (opt) {
1665         case SCTP_NODELAY:
1666         case SCTP_AUTOCLOSE:
1667         case SCTP_AUTO_ASCONF:
1668         case SCTP_DISABLE_FRAGMENTS:
1669         case SCTP_I_WANT_MAPPED_V4_ADDR:
1670 #ifdef SCTP_DEBUG
1671                 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1672                         kprintf("other stuff\n");
1673                 }
1674 #endif /* SCTP_DEBUG */
1675                 SCTP_INP_RLOCK(inp);
1676                 switch (opt) {
1677                 case SCTP_DISABLE_FRAGMENTS:
1678                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
1679                         break;
1680                 case SCTP_I_WANT_MAPPED_V4_ADDR:
1681                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
1682                         break;
1683                 case SCTP_AUTO_ASCONF:
1684                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
1685                         break;
1686                 case SCTP_NODELAY:
1687                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
1688                         break;
1689                 case SCTP_AUTOCLOSE:
1690                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
1691                             SCTP_PCB_FLAGS_AUTOCLOSE)
1692                                 optval = inp->sctp_ep.auto_close_time;
1693                         else
1694                                 optval = 0;
1695                         break;
1696
1697                 default:
1698                         error = ENOPROTOOPT;
1699                 } /* end switch (sopt->sopt_name) */
1700                 if (opt != SCTP_AUTOCLOSE) {
1701                         /* make it an "on/off" value */
1702                         optval = (optval != 0);
1703                 }
1704                 if ((size_t)m->m_len < sizeof(int)) {
1705                         error = EINVAL;
1706                 }
1707                 SCTP_INP_RUNLOCK(inp);
1708                 if (error == 0) {
1709                         /* return the option value */
1710                         *mtod(m, int *) = optval;
1711                         m->m_len = sizeof(optval);
1712                 }
1713                 break;
1714         case SCTP_GET_ASOC_ID_LIST:
1715         {
1716                 struct sctp_assoc_ids *ids;
1717                 int cnt, at;
1718                 u_int16_t orig;
1719
1720                 if ((size_t)m->m_len < sizeof(struct sctp_assoc_ids)) {
1721                         error = EINVAL;
1722                         break;
1723                 }
1724                 ids = mtod(m, struct sctp_assoc_ids *);
1725                 cnt = 0;
1726                 SCTP_INP_RLOCK(inp);
1727                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1728                 if (stcb == NULL) {
1729                 none_out_now:
1730                         ids->asls_numb_present = 0;
1731                         ids->asls_more_to_get = 0;
1732                         SCTP_INP_RUNLOCK(inp);
1733                         break;
1734                 }
1735                 orig = ids->asls_assoc_start;
1736                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1737                 while( orig ) {
1738                         stcb = LIST_NEXT(stcb , sctp_tcblist);
1739                         orig--;
1740                         cnt--;
1741                 }
1742                 if ( stcb == NULL)
1743                         goto none_out_now;
1744
1745                 at = 0;
1746                 ids->asls_numb_present = 0;
1747                 ids->asls_more_to_get = 1;
1748                 while(at < MAX_ASOC_IDS_RET) {
1749                         ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1750                         at++;
1751                         ids->asls_numb_present++;
1752                         stcb = LIST_NEXT(stcb , sctp_tcblist);
1753                         if (stcb == NULL) {
1754                                 ids->asls_more_to_get = 0;
1755                                 break;
1756                         }
1757                 }
1758                 SCTP_INP_RUNLOCK(inp);
1759         }
1760         break;
1761         case SCTP_GET_NONCE_VALUES:
1762         {
1763                 struct sctp_get_nonce_values *gnv;
1764                 if ((size_t)m->m_len < sizeof(struct sctp_get_nonce_values)) {
1765                         error = EINVAL;
1766                         break;
1767                 }
1768                 gnv = mtod(m, struct sctp_get_nonce_values *);
1769                 stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
1770                 if (stcb == NULL) {
1771                         error = ENOTCONN;
1772                 } else {
1773                         gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1774                         gnv->gn_local_tag = stcb->asoc.my_vtag;
1775                         SCTP_TCB_UNLOCK(stcb);
1776                 }
1777
1778         }
1779         break;
1780         case SCTP_PEER_PUBLIC_KEY:
1781         case SCTP_MY_PUBLIC_KEY:
1782         case SCTP_SET_AUTH_CHUNKS:
1783         case SCTP_SET_AUTH_SECRET:
1784                 /* not supported yet and until we refine the draft */
1785                 error = EOPNOTSUPP;
1786                 break;
1787
1788         case SCTP_DELAYED_ACK_TIME:
1789         {
1790                 int32_t *tm;
1791                 if ((size_t)m->m_len < sizeof(int32_t)) {
1792                         error = EINVAL;
1793                         break;
1794                 }
1795                 tm = mtod(m, int32_t *);
1796
1797                 *tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1798         }
1799         break;
1800
1801         case SCTP_GET_SNDBUF_USE:
1802                 if ((size_t)m->m_len < sizeof(struct sctp_sockstat)) {
1803                         error = EINVAL;
1804                 } else {
1805                         struct sctp_sockstat *ss;
1806                         struct sctp_tcb *stcb;
1807                         struct sctp_association *asoc;
1808                         ss = mtod(m, struct sctp_sockstat *);
1809                         stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
1810                         if (stcb == NULL) {
1811                                 error = ENOTCONN;
1812                         } else {
1813                                 asoc = &stcb->asoc;
1814                                 ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
1815                                 ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
1816                                 ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
1817                                                                     asoc->size_on_reasm_queue +
1818                                                                     asoc->size_on_all_streams);
1819                                 SCTP_TCB_UNLOCK(stcb);
1820                                 error = 0;
1821                                 m->m_len = sizeof(struct sctp_sockstat);
1822                         }
1823                 }
1824                 break;
1825         case SCTP_MAXBURST:
1826         {
1827                 u_int8_t *burst;
1828                 burst = mtod(m, u_int8_t *);
1829                 SCTP_INP_RLOCK(inp);
1830                 *burst = inp->sctp_ep.max_burst;
1831                 SCTP_INP_RUNLOCK(inp);
1832                 m->m_len = sizeof(u_int8_t);
1833         }
1834         break;
1835         case SCTP_MAXSEG:
1836         {
1837                 u_int32_t *segsize;
1838                 sctp_assoc_t *assoc_id;
1839                 int ovh;
1840
1841                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1842                         error = EINVAL;
1843                         break;
1844                 }
1845                 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
1846                         error = EINVAL;
1847                         break;
1848                 }
1849                 assoc_id = mtod(m, sctp_assoc_t *);
1850                 segsize = mtod(m, u_int32_t *);
1851                 m->m_len = sizeof(u_int32_t);
1852
1853                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1854                      (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
1855                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1856                         struct sctp_tcb *stcb;
1857                         SCTP_INP_RLOCK(inp);
1858                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1859                         if (stcb) {
1860                                 SCTP_TCB_LOCK(stcb);
1861                                 SCTP_INP_RUNLOCK(inp);
1862                                 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1863                                 SCTP_TCB_UNLOCK(stcb);
1864                         } else {
1865                                 SCTP_INP_RUNLOCK(inp);
1866                                 goto skipit;
1867                         }
1868                 } else {
1869                         stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1870                         if (stcb) {
1871                                 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1872                                 SCTP_TCB_UNLOCK(stcb);
1873                                 break;
1874                         }
1875                 skipit:
1876                         /* default is to get the max, if I
1877                          * can't calculate from an existing association.
1878                          */
1879                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1880                                 ovh = SCTP_MED_OVERHEAD;
1881                         } else {
1882                                 ovh = SCTP_MED_V4_OVERHEAD;
1883                         }
1884                         *segsize = inp->sctp_frag_point - ovh;
1885                 }
1886         }
1887         break;
1888
1889         case SCTP_SET_DEBUG_LEVEL:
1890 #ifdef SCTP_DEBUG
1891         {
1892                 u_int32_t *level;
1893                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1894                         error = EINVAL;
1895                         break;
1896                 }
1897                 level = mtod(m, u_int32_t *);
1898                 error = 0;
1899                 *level = sctp_debug_on;
1900                 m->m_len = sizeof(u_int32_t);
1901                 kprintf("Returning DEBUG LEVEL %x is set\n",
1902                        (u_int)sctp_debug_on);
1903         }
1904 #else /* SCTP_DEBUG */
1905         error = EOPNOTSUPP;
1906 #endif
1907         break;
1908         case SCTP_GET_STAT_LOG:
1909 #ifdef SCTP_STAT_LOGGING
1910                 error = sctp_fill_stat_log(m);
1911 #else /* SCTP_DEBUG */
1912                 error = EOPNOTSUPP;
1913 #endif
1914                 break;
1915         case SCTP_GET_PEGS:
1916         {
1917                 u_int32_t *pt;
1918                 if ((size_t)m->m_len < sizeof(sctp_pegs)) {
1919                         error = EINVAL;
1920                         break;
1921                 }
1922                 pt = mtod(m, u_int32_t *);
1923                 memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
1924                 m->m_len = sizeof(sctp_pegs);
1925         }
1926         break;
1927         case SCTP_EVENTS:
1928         {
1929                 struct sctp_event_subscribe *events;
1930 #ifdef SCTP_DEBUG
1931                 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1932                         kprintf("get events\n");
1933                 }
1934 #endif /* SCTP_DEBUG */
1935                 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
1936 #ifdef SCTP_DEBUG
1937                         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1938                                 kprintf("M->M_LEN is %d not %d\n",
1939                                        (int)m->m_len,
1940                                        (int)sizeof(struct sctp_event_subscribe));
1941                         }
1942 #endif /* SCTP_DEBUG */
1943                         error = EINVAL;
1944                         break;
1945                 }
1946                 events = mtod(m, struct sctp_event_subscribe *);
1947                 memset(events, 0, sizeof(events));
1948                 SCTP_INP_RLOCK(inp);
1949                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
1950                         events->sctp_data_io_event = 1;
1951
1952                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
1953                         events->sctp_association_event = 1;
1954
1955                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
1956                         events->sctp_address_event = 1;
1957
1958                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
1959                         events->sctp_send_failure_event = 1;
1960
1961                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
1962                         events->sctp_peer_error_event = 1;
1963
1964                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
1965                         events->sctp_shutdown_event = 1;
1966
1967                 if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
1968                         events->sctp_partial_delivery_event = 1;
1969
1970                 if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
1971                         events->sctp_adaption_layer_event = 1;
1972
1973                 if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
1974                         events->sctp_stream_reset_events = 1;
1975                 SCTP_INP_RUNLOCK(inp);
1976                 m->m_len = sizeof(struct sctp_event_subscribe);
1977
1978         }
1979         break;
1980
1981         case SCTP_ADAPTION_LAYER:
1982                 if ((size_t)m->m_len < sizeof(int)) {
1983                         error = EINVAL;
1984                         break;
1985                 }
1986 #ifdef SCTP_DEBUG
1987                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1988                         kprintf("getadaption ind\n");
1989                 }
1990 #endif /* SCTP_DEBUG */
1991                 SCTP_INP_RLOCK(inp);
1992                 *mtod(m, int *) = inp->sctp_ep.adaption_layer_indicator;
1993                 SCTP_INP_RUNLOCK(inp);
1994                 m->m_len = sizeof(int);
1995                 break;
1996         case SCTP_SET_INITIAL_DBG_SEQ:
1997                 if ((size_t)m->m_len < sizeof(int)) {
1998                         error = EINVAL;
1999                         break;
2000                 }
2001 #ifdef SCTP_DEBUG
2002                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2003                         kprintf("get initial dbg seq\n");
2004                 }
2005 #endif /* SCTP_DEBUG */
2006                 SCTP_INP_RLOCK(inp);
2007                 *mtod(m, int *) = inp->sctp_ep.initial_sequence_debug;
2008                 SCTP_INP_RUNLOCK(inp);
2009                 m->m_len = sizeof(int);
2010                 break;
2011         case SCTP_GET_LOCAL_ADDR_SIZE:
2012                 if ((size_t)m->m_len < sizeof(int)) {
2013                         error = EINVAL;
2014                         break;
2015                 }
2016 #ifdef SCTP_DEBUG
2017                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2018                         kprintf("get local sizes\n");
2019                 }
2020 #endif /* SCTP_DEBUG */
2021                 SCTP_INP_RLOCK(inp);
2022                 *mtod(m, int *) = sctp_count_max_addresses(inp);
2023                 SCTP_INP_RUNLOCK(inp);
2024                 m->m_len = sizeof(int);
2025                 break;
2026         case SCTP_GET_REMOTE_ADDR_SIZE:
2027         {
2028                 sctp_assoc_t *assoc_id;
2029                 u_int32_t *val, sz;
2030                 struct sctp_nets *net;
2031 #ifdef SCTP_DEBUG
2032                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2033                         kprintf("get remote size\n");
2034                 }
2035 #endif /* SCTP_DEBUG */
2036                 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
2037 #ifdef SCTP_DEBUG
2038                         kprintf("m->m_len:%d not %d\n",
2039                                m->m_len, sizeof(sctp_assoc_t));
2040 #endif /* SCTP_DEBUG */
2041                         error = EINVAL;
2042                         break;
2043                 }
2044                 stcb = NULL;
2045                 val = mtod(m, u_int32_t *);
2046                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2047                         SCTP_INP_RLOCK(inp);
2048                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2049                         if (stcb)
2050                                 SCTP_TCB_LOCK(stcb);
2051                         SCTP_INP_RUNLOCK(inp);
2052                 }
2053                 if (stcb == NULL) {
2054                         assoc_id = mtod(m, sctp_assoc_t *);
2055                         stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
2056                 }
2057
2058                 if (stcb == NULL) {
2059                         error = EINVAL;
2060                         break;
2061                 }
2062                 *val = 0;
2063                 sz = 0;
2064                 /* Count the sizes */
2065                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2066                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2067                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2068                                 sz += sizeof(struct sockaddr_in6);
2069                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2070                                 sz += sizeof(struct sockaddr_in);
2071                         } else {
2072                                 /* huh */
2073                                 break;
2074                         }
2075                 }
2076                 SCTP_TCB_UNLOCK(stcb);
2077                 *val = sz;
2078                 m->m_len = sizeof(u_int32_t);
2079         }
2080         break;
2081         case SCTP_GET_PEER_ADDRESSES:
2082                 /*
2083                  * Get the address information, an array
2084                  * is passed in to fill up we pack it.
2085                  */
2086         {
2087                 int cpsz, left;
2088                 struct sockaddr_storage *sas;
2089                 struct sctp_nets *net;
2090                 struct sctp_getaddresses *saddr;
2091 #ifdef SCTP_DEBUG
2092                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2093                         kprintf("get peer addresses\n");
2094                 }
2095 #endif /* SCTP_DEBUG */
2096                 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2097                         error = EINVAL;
2098                         break;
2099                 }
2100                 left = m->m_len - sizeof(struct sctp_getaddresses);
2101                 saddr = mtod(m, struct sctp_getaddresses *);
2102                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2103                         SCTP_INP_RLOCK(inp);
2104                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2105                         if (stcb)
2106                                 SCTP_TCB_LOCK(stcb);
2107                         SCTP_INP_RUNLOCK(inp);
2108                 } else
2109                         stcb = sctp_findassociation_ep_asocid(inp,
2110                                                               saddr->sget_assoc_id);
2111                 if (stcb == NULL) {
2112                         error = ENOENT;
2113                         break;
2114                 }
2115                 m->m_len = sizeof(struct sctp_getaddresses);
2116                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2117
2118                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2119                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2120                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2121                                 cpsz = sizeof(struct sockaddr_in6);
2122                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2123                                 cpsz = sizeof(struct sockaddr_in);
2124                         } else {
2125                                 /* huh */
2126                                 break;
2127                         }
2128                         if (left < cpsz) {
2129                                 /* not enough room. */
2130 #ifdef SCTP_DEBUG
2131                                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2132                                         kprintf("Out of room\n");
2133                                 }
2134 #endif /* SCTP_DEBUG */
2135                                 break;
2136                         }
2137                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
2138                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2139                                 /* Must map the address */
2140                                 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2141                                                     (struct sockaddr_in6 *)sas);
2142                         } else {
2143                                 memcpy(sas, &net->ro._l_addr, cpsz);
2144                         }
2145                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2146
2147                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2148                         left -= cpsz;
2149                         m->m_len += cpsz;
2150 #ifdef SCTP_DEBUG
2151                         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2152                                 kprintf("left now:%d mlen:%d\n",
2153                                        left, m->m_len);
2154                         }
2155 #endif /* SCTP_DEBUG */
2156                 }
2157                 SCTP_TCB_UNLOCK(stcb);
2158         }
2159 #ifdef SCTP_DEBUG
2160         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2161                 kprintf("All done\n");
2162         }
2163 #endif /* SCTP_DEBUG */
2164         break;
2165         case SCTP_GET_LOCAL_ADDRESSES:
2166         {
2167                 int limit, actual;
2168                 struct sockaddr_storage *sas;
2169                 struct sctp_getaddresses *saddr;
2170 #ifdef SCTP_DEBUG
2171                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2172                         kprintf("get local addresses\n");
2173                 }
2174 #endif /* SCTP_DEBUG */
2175                 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2176                         error = EINVAL;
2177                         break;
2178                 }
2179                 saddr = mtod(m, struct sctp_getaddresses *);
2180
2181                 if (saddr->sget_assoc_id) {
2182                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2183                                 SCTP_INP_RLOCK(inp);
2184                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2185                                 if (stcb)
2186                                         SCTP_TCB_LOCK(stcb);
2187                                 SCTP_INP_RUNLOCK(inp);
2188                         } else
2189                                 stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
2190
2191                 } else {
2192                         stcb = NULL;
2193                 }
2194                 /*
2195                  * assure that the TCP model does not need a assoc id
2196                  * once connected.
2197                  */
2198                 if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
2199                      (stcb == NULL) ) {
2200                         SCTP_INP_RLOCK(inp);
2201                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2202                         if (stcb)
2203                                 SCTP_TCB_LOCK(stcb);
2204                         SCTP_INP_RUNLOCK(inp);
2205                 }
2206                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2207                 limit = m->m_len - sizeof(sctp_assoc_t);
2208                 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2209                 SCTP_TCB_UNLOCK(stcb);
2210                 m->m_len = sizeof(struct sockaddr_storage) + actual;
2211         }
2212         break;
2213         case SCTP_PEER_ADDR_PARAMS:
2214         {
2215                 struct sctp_paddrparams *paddrp;
2216                 struct sctp_nets *net;
2217
2218 #ifdef SCTP_DEBUG
2219                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2220                         kprintf("Getting peer_addr_params\n");
2221                 }
2222 #endif /* SCTP_DEBUG */
2223                 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
2224 #ifdef SCTP_DEBUG
2225                         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2226                                 kprintf("Hmm m->m_len:%d is to small\n",
2227                                        m->m_len);
2228                         }
2229 #endif /* SCTP_DEBUG */
2230                         error = EINVAL;
2231                         break;
2232                 }
2233                 paddrp = mtod(m, struct sctp_paddrparams *);
2234
2235                 net = NULL;
2236                 if (paddrp->spp_assoc_id) {
2237 #ifdef SCTP_DEBUG
2238                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2239                                 kprintf("In spp_assoc_id find type\n");
2240                         }
2241 #endif /* SCTP_DEBUG */
2242                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2243                                 SCTP_INP_RLOCK(inp);
2244                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2245                                 if (stcb) {
2246                                         SCTP_TCB_LOCK(stcb);
2247                                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2248                                 }
2249                                 SCTP_INP_RLOCK(inp);
2250                         } else {
2251                                 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
2252                         }
2253                         if (stcb == NULL) {
2254                                 error = ENOENT;
2255                                 break;
2256                         }
2257                 }
2258                 if (    (stcb == NULL) &&
2259                         ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
2260                          (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
2261                         /* Lookup via address */
2262 #ifdef SCTP_DEBUG
2263                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2264                                 kprintf("Ok we need to lookup a param\n");
2265                         }
2266 #endif /* SCTP_DEBUG */
2267                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2268                                 SCTP_INP_RLOCK(inp);
2269                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2270                                 if (stcb) {
2271                                         SCTP_TCB_LOCK(stcb);
2272                                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2273                                 }
2274                                 SCTP_INP_RUNLOCK(inp);
2275                         } else {
2276                                 SCTP_INP_WLOCK(inp);
2277                                 SCTP_INP_INCR_REF(inp);
2278                                 SCTP_INP_WUNLOCK(inp);
2279                                 stcb = sctp_findassociation_ep_addr(&inp,
2280                                                                     (struct sockaddr *)&paddrp->spp_address,
2281                                                                     &net, NULL, NULL);
2282                                 if (stcb == NULL) {
2283                                         SCTP_INP_WLOCK(inp);
2284                                         SCTP_INP_DECR_REF(inp);
2285                                         SCTP_INP_WUNLOCK(inp);
2286                                 }
2287                         }
2288
2289                         if (stcb == NULL) {
2290                                 error = ENOENT;
2291                                 break;
2292                         }
2293                 } else {
2294                         /* Effects the Endpoint */
2295 #ifdef SCTP_DEBUG
2296                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2297                                 kprintf("User wants EP level info\n");
2298                         }
2299 #endif /* SCTP_DEBUG */
2300                         stcb = NULL;
2301                 }
2302                 if (stcb) {
2303                         /* Applys to the specific association */
2304 #ifdef SCTP_DEBUG
2305                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2306                                 kprintf("In TCB side\n");
2307                         }
2308 #endif /* SCTP_DEBUG */
2309                         if (net) {
2310                                 paddrp->spp_pathmaxrxt = net->failure_threshold;
2311                         } else {
2312                                 /* No destination so return default value */
2313                                 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2314                         }
2315                         paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2316                         paddrp->spp_assoc_id = sctp_get_associd(stcb);
2317                         SCTP_TCB_UNLOCK(stcb);
2318                 } else {
2319                         /* Use endpoint defaults */
2320                         SCTP_INP_RLOCK(inp);
2321 #ifdef SCTP_DEBUG
2322                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2323                                 kprintf("In EP level info\n");
2324                         }
2325 #endif /* SCTP_DEBUG */
2326                         paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2327                         paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
2328                         paddrp->spp_assoc_id = (sctp_assoc_t)0;
2329                         SCTP_INP_RUNLOCK(inp);
2330                 }
2331                 m->m_len = sizeof(struct sctp_paddrparams);
2332         }
2333         break;
2334         case SCTP_GET_PEER_ADDR_INFO:
2335         {
2336                 struct sctp_paddrinfo *paddri;
2337                 struct sctp_nets *net;
2338 #ifdef SCTP_DEBUG
2339                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2340                         kprintf("GetPEER ADDR_INFO\n");
2341                 }
2342 #endif /* SCTP_DEBUG */
2343                 if ((size_t)m->m_len < sizeof(struct sctp_paddrinfo)) {
2344                         error = EINVAL;
2345                         break;
2346                 }
2347                 paddri = mtod(m, struct sctp_paddrinfo *);
2348                 net = NULL;
2349                 if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
2350                     (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
2351                         /* Lookup via address */
2352                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2353                                 SCTP_INP_RLOCK(inp);
2354                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2355                                 if (stcb) {
2356                                         SCTP_TCB_LOCK(stcb);
2357                                         net = sctp_findnet(stcb,
2358                                                             (struct sockaddr *)&paddri->spinfo_address);
2359                                 }
2360                                 SCTP_INP_RUNLOCK(inp);
2361                         } else {
2362                                 SCTP_INP_WLOCK(inp);
2363                                 SCTP_INP_INCR_REF(inp);
2364                                 SCTP_INP_WUNLOCK(inp);
2365                                 stcb = sctp_findassociation_ep_addr(&inp,
2366                                     (struct sockaddr *)&paddri->spinfo_address,
2367                                     &net, NULL, NULL);
2368                                 if (stcb == NULL) {
2369                                         SCTP_INP_WLOCK(inp);
2370                                         SCTP_INP_DECR_REF(inp);
2371                                         SCTP_INP_WUNLOCK(inp);
2372                                 }
2373                         }
2374
2375                 } else {
2376                         stcb = NULL;
2377                 }
2378                 if ((stcb == NULL) || (net == NULL)) {
2379                         error = ENOENT;
2380                         break;
2381                 }
2382                 m->m_len = sizeof(struct sctp_paddrinfo);
2383                 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
2384                 paddri->spinfo_cwnd = net->cwnd;
2385                 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2386                 paddri->spinfo_rto = net->RTO;
2387                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2388                 SCTP_TCB_UNLOCK(stcb);
2389         }
2390         break;
2391         case SCTP_PCB_STATUS:
2392         {
2393                 struct sctp_pcbinfo *spcb;
2394 #ifdef SCTP_DEBUG
2395                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2396                         kprintf("PCB status\n");
2397                 }
2398 #endif /* SCTP_DEBUG */
2399                 if ((size_t)m->m_len < sizeof(struct sctp_pcbinfo)) {
2400                         error = EINVAL;
2401                         break;
2402                 }
2403                 spcb = mtod(m, struct sctp_pcbinfo *);
2404                 sctp_fill_pcbinfo(spcb);
2405                 m->m_len = sizeof(struct sctp_pcbinfo);
2406         }
2407         break;
2408         case SCTP_STATUS:
2409         {
2410                 struct sctp_nets *net;
2411                 struct sctp_status *sstat;
2412 #ifdef SCTP_DEBUG
2413                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2414                         kprintf("SCTP status\n");
2415                 }
2416 #endif /* SCTP_DEBUG */
2417
2418                 if ((size_t)m->m_len < sizeof(struct sctp_status)) {
2419                         error = EINVAL;
2420                         break;
2421                 }
2422                 sstat = mtod(m, struct sctp_status *);
2423
2424                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2425                         SCTP_INP_RLOCK(inp);
2426                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2427                         if (stcb)
2428                                 SCTP_TCB_LOCK(stcb);
2429                         SCTP_INP_RUNLOCK(inp);
2430                 } else
2431                         stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
2432
2433                 if (stcb == NULL) {
2434                         error = EINVAL;
2435                         break;
2436                 }
2437                 /*
2438                  * I think passing the state is fine since
2439                  * sctp_constants.h will be available to the user
2440                  * land.
2441                  */
2442                 sstat->sstat_state = stcb->asoc.state;
2443                 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2444                 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2445                 /*
2446                  * We can't include chunks that have been passed
2447                  * to the socket layer. Only things in queue.
2448                  */
2449                 sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
2450                                          stcb->asoc.cnt_on_reasm_queue +
2451                                          stcb->asoc.cnt_on_all_streams);
2452
2453
2454                 sstat->sstat_instrms = stcb->asoc.streamincnt;
2455                 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2456                 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2457                 memcpy(&sstat->sstat_primary.spinfo_address,
2458                        &stcb->asoc.primary_destination->ro._l_addr,
2459                        ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2460                 net = stcb->asoc.primary_destination;
2461                 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2462                 /*
2463                  * Again the user can get info from sctp_constants.h
2464                  * for what the state of the network is.
2465                  */
2466                 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2467                 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2468                 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2469                 sstat->sstat_primary.spinfo_rto = net->RTO;
2470                 sstat->sstat_primary.spinfo_mtu = net->mtu;
2471                 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2472                 SCTP_TCB_UNLOCK(stcb);
2473                 m->m_len = sizeof(*sstat);
2474         }
2475         break;
2476         case SCTP_RTOINFO:
2477         {
2478                 struct sctp_rtoinfo *srto;
2479 #ifdef SCTP_DEBUG
2480                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2481                         kprintf("RTO Info\n");
2482                 }
2483 #endif /* SCTP_DEBUG */
2484                 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
2485                         error = EINVAL;
2486                         break;
2487                 }
2488                 srto = mtod(m, struct sctp_rtoinfo *);
2489                 if (srto->srto_assoc_id == 0) {
2490                         /* Endpoint only please */
2491                         SCTP_INP_RLOCK(inp);
2492                         srto->srto_initial = inp->sctp_ep.initial_rto;
2493                         srto->srto_max = inp->sctp_ep.sctp_maxrto;
2494                         srto->srto_min = inp->sctp_ep.sctp_minrto;
2495                         SCTP_INP_RUNLOCK(inp);
2496                         break;
2497                 }
2498                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2499                         SCTP_INP_RLOCK(inp);
2500                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2501                         if (stcb)
2502                                 SCTP_TCB_LOCK(stcb);
2503                         SCTP_INP_RUNLOCK(inp);
2504                 } else
2505                         stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2506
2507                 if (stcb == NULL) {
2508                         error = EINVAL;
2509                         break;
2510                 }
2511                 srto->srto_initial = stcb->asoc.initial_rto;
2512                 srto->srto_max = stcb->asoc.maxrto;
2513                 srto->srto_min = stcb->asoc.minrto;
2514                 SCTP_TCB_UNLOCK(stcb);
2515                 m->m_len = sizeof(*srto);
2516         }
2517         break;
2518         case SCTP_ASSOCINFO:
2519         {
2520                 struct sctp_assocparams *sasoc;
2521 #ifdef SCTP_DEBUG
2522                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2523                         kprintf("Associnfo\n");
2524                 }
2525 #endif /* SCTP_DEBUG */
2526                 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
2527                         error = EINVAL;
2528                         break;
2529                 }
2530                 sasoc = mtod(m, struct sctp_assocparams *);
2531                 stcb = NULL;
2532
2533                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2534                         SCTP_INP_RLOCK(inp);
2535                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2536                         if (stcb)
2537                                 SCTP_TCB_LOCK(stcb);
2538                         SCTP_INP_RUNLOCK(inp);
2539                 }
2540                 if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
2541                         stcb = sctp_findassociation_ep_asocid(inp,
2542                                                              sasoc->sasoc_assoc_id);
2543                         if (stcb == NULL) {
2544                                 error = ENOENT;
2545                                 break;
2546                         }
2547                 } else {
2548                         stcb = NULL;
2549                 }
2550
2551                 if (stcb) {
2552                         sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2553                         sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2554                         sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2555                         sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2556                         sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2557                         SCTP_TCB_UNLOCK(stcb);
2558                 } else {
2559                         SCTP_INP_RLOCK(inp);
2560                         sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2561                         sasoc->sasoc_number_peer_destinations = 0;
2562                         sasoc->sasoc_peer_rwnd = 0;
2563                         sasoc->sasoc_local_rwnd = ssb_space(&inp->sctp_socket->so_rcv);
2564                         sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2565                         SCTP_INP_RUNLOCK(inp);
2566                 }
2567                 m->m_len = sizeof(*sasoc);
2568         }
2569         break;
2570         case SCTP_DEFAULT_SEND_PARAM:
2571         {
2572                 struct sctp_sndrcvinfo *s_info;
2573
2574                 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
2575                         error = EINVAL;
2576                         break;
2577                 }
2578                 s_info = mtod(m, struct sctp_sndrcvinfo *);
2579                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2580                         SCTP_INP_RLOCK(inp);
2581                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2582                         if (stcb)
2583                                 SCTP_TCB_LOCK(stcb);
2584                         SCTP_INP_RUNLOCK(inp);
2585                 } else
2586                         stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2587
2588                 if (stcb == NULL) {
2589                         error = ENOENT;
2590                         break;
2591                 }
2592                 /* Copy it out */
2593                 *s_info = stcb->asoc.def_send;
2594                 SCTP_TCB_UNLOCK(stcb);
2595                 m->m_len = sizeof(*s_info);
2596         }
2597         case SCTP_INITMSG:
2598         {
2599                 struct sctp_initmsg *sinit;
2600 #ifdef SCTP_DEBUG
2601                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2602                         kprintf("initmsg\n");
2603                 }
2604 #endif /* SCTP_DEBUG */
2605                 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
2606                         error = EINVAL;
2607                         break;
2608                 }
2609                 sinit = mtod(m, struct sctp_initmsg *);
2610                 SCTP_INP_RLOCK(inp);
2611                 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2612                 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2613                 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2614                 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2615                 SCTP_INP_RUNLOCK(inp);
2616                 m->m_len = sizeof(*sinit);
2617         }
2618         break;
2619         case SCTP_PRIMARY_ADDR:
2620                 /* we allow a "get" operation on this */
2621         {
2622                 struct sctp_setprim *ssp;
2623
2624 #ifdef SCTP_DEBUG
2625                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2626                         kprintf("setprimary\n");
2627                 }
2628 #endif /* SCTP_DEBUG */
2629                 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
2630                         error = EINVAL;
2631                         break;
2632                 }
2633                 ssp = mtod(m, struct sctp_setprim *);
2634                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2635                         SCTP_INP_RLOCK(inp);
2636                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2637                         if (stcb)
2638                                 SCTP_TCB_LOCK(stcb);
2639                         SCTP_INP_RUNLOCK(inp);
2640                 } else {
2641                         stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
2642                         if (stcb == NULL) {
2643                                 /* one last shot, try it by the address in */
2644                                 struct sctp_nets *net;
2645
2646                                 SCTP_INP_WLOCK(inp);
2647                                 SCTP_INP_INCR_REF(inp);
2648                                 SCTP_INP_WUNLOCK(inp);
2649                                 stcb = sctp_findassociation_ep_addr(&inp,
2650                                                             (struct sockaddr *)&ssp->ssp_addr,
2651                                                             &net, NULL, NULL);
2652                                 if (stcb == NULL) {
2653                                         SCTP_INP_WLOCK(inp);
2654                                         SCTP_INP_DECR_REF(inp);
2655                                         SCTP_INP_WUNLOCK(inp);
2656                                 }
2657                         }
2658                         if (stcb == NULL) {
2659                                 error = EINVAL;
2660                                 break;
2661                         }
2662                 }
2663                 /* simply copy out the sockaddr_storage... */
2664                 memcpy(&ssp->ssp_addr,
2665                        &stcb->asoc.primary_destination->ro._l_addr,
2666                        ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2667                 SCTP_TCB_UNLOCK(stcb);
2668                 m->m_len = sizeof(*ssp);
2669         }
2670         break;
2671         default:
2672                 error = ENOPROTOOPT;
2673                 m->m_len = 0;
2674                 break;
2675         } /* end switch (sopt->sopt_name) */
2676         return (error);
2677 }
2678
2679 static int
2680 sctp_optsset(struct socket *so,
2681              int opt,
2682              struct mbuf **mp,
2683 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
2684              struct thread *p
2685 #else
2686              struct proc *p
2687 #endif
2688         )
2689 {
2690         int error, *mopt, set_opt;
2691         struct mbuf *m;
2692         struct sctp_tcb *stcb = NULL;
2693         struct sctp_inpcb *inp;
2694
2695         if (mp == NULL) {
2696 #ifdef SCTP_DEBUG
2697                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2698                         kprintf("optsset:MP is NULL EINVAL\n");
2699                 }
2700 #endif /* SCTP_DEBUG */
2701                 return (EINVAL);
2702         }
2703         m = *mp;
2704         if (m == NULL)
2705                 return (EINVAL);
2706
2707         inp = (struct sctp_inpcb *)so->so_pcb;
2708         if (inp == 0)
2709                 return EINVAL;
2710
2711         error = 0;
2712         switch (opt) {
2713         case SCTP_NODELAY:
2714         case SCTP_AUTOCLOSE:
2715         case SCTP_AUTO_ASCONF:
2716         case SCTP_DISABLE_FRAGMENTS:
2717         case SCTP_I_WANT_MAPPED_V4_ADDR:
2718                 /* copy in the option value */
2719                 if ((size_t)m->m_len < sizeof(int)) {
2720                         error = EINVAL;
2721                         break;
2722                 }
2723                 mopt = mtod(m, int *);
2724                 set_opt = 0;
2725                 if (error)
2726                         break;
2727                 switch (opt) {
2728                 case SCTP_DISABLE_FRAGMENTS:
2729                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2730                         break;
2731                 case SCTP_AUTO_ASCONF:
2732                         set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2733                         break;
2734
2735                 case SCTP_I_WANT_MAPPED_V4_ADDR:
2736                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2737                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2738                         } else {
2739                                 return (EINVAL);
2740                         }
2741                         break;
2742                 case SCTP_NODELAY:
2743                         set_opt = SCTP_PCB_FLAGS_NODELAY;
2744                         break;
2745                 case SCTP_AUTOCLOSE:
2746                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2747                         /*
2748                          * The value is in ticks.
2749                          * Note this does not effect old associations, only
2750                          * new ones.
2751                          */
2752                         inp->sctp_ep.auto_close_time = (*mopt * hz);
2753                         break;
2754                 }
2755                 SCTP_INP_WLOCK(inp);
2756                 if (*mopt != 0) {
2757                         inp->sctp_flags |= set_opt;
2758                 } else {
2759                         inp->sctp_flags &= ~set_opt;
2760                 }
2761                 SCTP_INP_WUNLOCK(inp);
2762                 break;
2763         case SCTP_MY_PUBLIC_KEY:    /* set my public key */
2764         case SCTP_SET_AUTH_CHUNKS:  /* set the authenticated chunks required */
2765         case SCTP_SET_AUTH_SECRET:  /* set the actual secret for the endpoint */
2766                 /* not supported yet and until we refine the draft */
2767                 error = EOPNOTSUPP;
2768                 break;
2769
2770         case SCTP_CLR_STAT_LOG:
2771 #ifdef SCTP_STAT_LOGGING
2772                 sctp_clr_stat_log();
2773 #else
2774                 error = EOPNOTSUPP;
2775 #endif
2776                 break;
2777         case SCTP_DELAYED_ACK_TIME:
2778         {
2779                 int32_t *tm;
2780                 if ((size_t)m->m_len < sizeof(int32_t)) {
2781                         error = EINVAL;
2782                         break;
2783                 }
2784                 tm = mtod(m, int32_t *);
2785
2786                 if ((*tm < 10) || (*tm > 500)) {
2787                         /* can't be smaller than 10ms */
2788                         /* MUST NOT be larger than 500ms */
2789                         error = EINVAL;
2790                         break;
2791                 }
2792                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
2793         }
2794                 break;
2795         case SCTP_RESET_STREAMS:
2796         {
2797                 struct sctp_stream_reset *strrst;
2798                 uint8_t two_way, not_peer;
2799
2800                 if ((size_t)m->m_len < sizeof(struct sctp_stream_reset)) {
2801                         error = EINVAL;
2802                         break;
2803                 }
2804                 strrst = mtod(m, struct sctp_stream_reset *);
2805
2806                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2807                         SCTP_INP_RLOCK(inp);
2808                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2809                         if (stcb)
2810                                 SCTP_TCB_LOCK(stcb);
2811                         SCTP_INP_RUNLOCK(inp);
2812                 } else
2813                         stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
2814                 if (stcb == NULL) {
2815                         error = ENOENT;
2816                         break;
2817                 }
2818                 if (stcb->asoc.peer_supports_strreset == 0) {
2819                         /* Peer does not support it,
2820                          * we return protocol not supported since
2821                          * this is true for this feature and this
2822                          * peer, not the socket request in general.
2823                          */
2824                         error = EPROTONOSUPPORT;
2825                         SCTP_TCB_UNLOCK(stcb);
2826                         break;
2827                 }
2828
2829 /* Having re-thought this code I added as I write the I-D there
2830  * is NO need for it. The peer, if we are requesting a stream-reset
2831  * will send a request to us but will itself do what we do, take
2832  * and copy off the "reset information" we send and queue TSN's
2833  * larger than the send-next in our response message. Thus they
2834  * will handle it.
2835  */
2836 /*              if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
2837                 /* Must have all sending data ack'd before we
2838                  * start this procedure. This is a bit restrictive
2839                  * and we SHOULD work on changing this so ONLY the
2840                  * streams being RESET get held up. So, a reset-all
2841                  * would require this.. but a reset specific just
2842                  * needs to be sure that the ones being reset have
2843                  * nothing on the send_queue. For now we will
2844                  * skip this more detailed method and do a course
2845                  * way.. i.e. nothing pending ... for future FIX ME!
2846                  */
2847 /*                      error = EBUSY;*/
2848 /*                      break;*/
2849 /*              }*/
2850
2851                 if (stcb->asoc.stream_reset_outstanding) {
2852                         error = EALREADY;
2853                         SCTP_TCB_UNLOCK(stcb);
2854                         break;
2855                 }
2856                 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2857                         two_way = 0;
2858                         not_peer = 0;
2859                 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2860                         two_way = 1;
2861                         not_peer = 1;
2862                 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2863                         two_way = 1;
2864                         not_peer = 0;
2865                 } else {
2866                         error = EINVAL;
2867                         SCTP_TCB_UNLOCK(stcb);
2868                         break;
2869                 }
2870                 sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2871                                         strrst->strrst_list, two_way, not_peer);
2872                 crit_enter();
2873                 sctp_chunk_output(inp, stcb, 12);
2874                 SCTP_TCB_UNLOCK(stcb);
2875                 crit_exit();
2876
2877         }
2878         break;
2879         case SCTP_RESET_PEGS:
2880                 memset(sctp_pegs, 0, sizeof(sctp_pegs));
2881                 error = 0;
2882                 break;
2883         case SCTP_CONNECT_X:
2884                 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2885                         error = EINVAL;
2886                         break;
2887                 }
2888                 error = sctp_do_connect_x(so, inp, m, p, 0);
2889                 break;
2890
2891         case SCTP_CONNECT_X_DELAYED:
2892                 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2893                         error = EINVAL;
2894                         break;
2895                 }
2896                 error = sctp_do_connect_x(so, inp, m, p, 1);
2897                 break;
2898
2899         case SCTP_CONNECT_X_COMPLETE:
2900         {
2901                 struct sockaddr *sa;
2902                 struct sctp_nets *net;
2903                 if ((size_t)m->m_len < sizeof(struct sockaddr_in)) {
2904                         error = EINVAL;
2905                         break;
2906                 }
2907                 sa = mtod(m, struct sockaddr *);
2908                 /* find tcb */
2909                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2910                         SCTP_INP_RLOCK(inp);
2911                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2912                         if (stcb) {
2913                                 SCTP_TCB_LOCK(stcb);
2914                                 net = sctp_findnet(stcb, sa);
2915                         }
2916                         SCTP_INP_RUNLOCK(inp);
2917                 } else {
2918                         SCTP_INP_WLOCK(inp);
2919                         SCTP_INP_INCR_REF(inp);
2920                         SCTP_INP_WUNLOCK(inp);
2921                         stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2922                         if (stcb == NULL) {
2923                                 SCTP_INP_WLOCK(inp);
2924                                 SCTP_INP_DECR_REF(inp);
2925                                 SCTP_INP_WUNLOCK(inp);
2926                         }
2927                 }
2928
2929                 if (stcb == NULL) {
2930                         error = ENOENT;
2931                         break;
2932                 }
2933                 if (stcb->asoc.delayed_connection == 1) {
2934                         stcb->asoc.delayed_connection = 0;
2935                         SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2936                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2937                         sctp_send_initiate(inp, stcb);
2938                 } else {
2939                         /* already expired or did not use delayed connectx */
2940                         error = EALREADY;
2941                 }
2942                 SCTP_TCB_UNLOCK(stcb);
2943         }
2944         break;
2945         case SCTP_MAXBURST:
2946         {
2947                 u_int8_t *burst;
2948                 SCTP_INP_WLOCK(inp);
2949                 burst = mtod(m, u_int8_t *);
2950                 if (*burst) {
2951                         inp->sctp_ep.max_burst = *burst;
2952                 }
2953                 SCTP_INP_WUNLOCK(inp);
2954         }
2955         break;
2956         case SCTP_MAXSEG:
2957         {
2958                 u_int32_t *segsize;
2959                 int ovh;
2960                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2961                         ovh = SCTP_MED_OVERHEAD;
2962                 } else {
2963                         ovh = SCTP_MED_V4_OVERHEAD;
2964                 }
2965                 segsize = mtod(m, u_int32_t *);
2966                 if (*segsize < 1) {
2967                         error = EINVAL;
2968                         break;
2969                 }
2970                 SCTP_INP_WLOCK(inp);
2971                 inp->sctp_frag_point = (*segsize+ovh);
2972                 if (inp->sctp_frag_point < MHLEN) {
2973                         inp->sctp_frag_point = MHLEN;
2974                 }
2975                 SCTP_INP_WUNLOCK(inp);
2976         }
2977         break;
2978         case SCTP_SET_DEBUG_LEVEL:
2979 #ifdef SCTP_DEBUG
2980         {
2981                 u_int32_t *level;
2982                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
2983                         error = EINVAL;
2984                         break;
2985                 }
2986                 level = mtod(m, u_int32_t *);
2987                 error = 0;
2988                 sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
2989                                            SCTP_DEBUG_NOISY));
2990                 kprintf("SETTING DEBUG LEVEL to %x\n",
2991                        (u_int)sctp_debug_on);
2992
2993         }
2994 #else
2995         error = EOPNOTSUPP;
2996 #endif /* SCTP_DEBUG */
2997         break;
2998         case SCTP_EVENTS:
2999         {
3000                 struct sctp_event_subscribe *events;
3001                 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
3002                         error = EINVAL;
3003                         break;
3004                 }
3005                 SCTP_INP_WLOCK(inp);
3006                 events = mtod(m, struct sctp_event_subscribe *);
3007                 if (events->sctp_data_io_event) {
3008                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
3009                 } else {
3010                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
3011                 }
3012
3013                 if (events->sctp_association_event) {
3014                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
3015                 } else {
3016                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
3017                 }
3018
3019                 if (events->sctp_address_event) {
3020                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
3021                 } else {
3022                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
3023                 }
3024
3025                 if (events->sctp_send_failure_event) {
3026                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3027                 } else {
3028                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3029                 }
3030
3031                 if (events->sctp_peer_error_event) {
3032                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
3033                 } else {
3034                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
3035                 }
3036
3037                 if (events->sctp_shutdown_event) {
3038                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3039                 } else {
3040                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3041                 }
3042
3043                 if (events->sctp_partial_delivery_event) {
3044                         inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
3045                 } else {
3046                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
3047                 }
3048
3049                 if (events->sctp_adaption_layer_event) {
3050                         inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
3051                 } else {
3052                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
3053                 }
3054
3055                 if (events->sctp_stream_reset_events) {
3056                         inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3057                 } else {
3058                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3059                 }
3060                 SCTP_INP_WUNLOCK(inp);
3061         }
3062         break;
3063
3064         case SCTP_ADAPTION_LAYER:
3065         {
3066                 struct sctp_setadaption *adap_bits;
3067                 if ((size_t)m->m_len < sizeof(struct sctp_setadaption)) {
3068                         error = EINVAL;
3069                         break;
3070                 }
3071                 SCTP_INP_WLOCK(inp);
3072                 adap_bits = mtod(m, struct sctp_setadaption *);
3073                 inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
3074                 SCTP_INP_WUNLOCK(inp);
3075         }
3076         break;
3077         case SCTP_SET_INITIAL_DBG_SEQ:
3078         {
3079                 u_int32_t *vvv;
3080                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
3081                         error = EINVAL;
3082                         break;
3083                 }
3084                 SCTP_INP_WLOCK(inp);
3085                 vvv = mtod(m, u_int32_t *);
3086                 inp->sctp_ep.initial_sequence_debug = *vvv;
3087                 SCTP_INP_WUNLOCK(inp);
3088         }
3089         break;
3090         case SCTP_DEFAULT_SEND_PARAM:
3091         {
3092                 struct sctp_sndrcvinfo *s_info;
3093
3094                 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
3095                         error = EINVAL;
3096                         break;
3097                 }
3098                 s_info = mtod(m, struct sctp_sndrcvinfo *);
3099
3100                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3101                         SCTP_INP_RLOCK(inp);
3102                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3103                         if (stcb)
3104                                 SCTP_TCB_LOCK(stcb);
3105                         SCTP_INP_RUNLOCK(inp);
3106                 } else
3107                         stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
3108
3109                 if (stcb == NULL) {
3110                         error = ENOENT;
3111                         break;
3112                 }
3113                 /* Validate things */
3114                 if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
3115                         SCTP_TCB_UNLOCK(stcb);
3116                         error = EINVAL;
3117                         break;
3118                 }
3119                 /* Mask off the flags that are allowed */
3120                 s_info->sinfo_flags = (s_info->sinfo_flags &
3121                                        (MSG_UNORDERED | MSG_ADDR_OVER |
3122                                         MSG_PR_SCTP_TTL | MSG_PR_SCTP_BUF));
3123                 /* Copy it in */
3124                 stcb->asoc.def_send = *s_info;
3125                 SCTP_TCB_UNLOCK(stcb);
3126         }
3127         break;
3128         case SCTP_PEER_ADDR_PARAMS:
3129         {
3130                 struct sctp_paddrparams *paddrp;
3131                 struct sctp_nets *net;
3132                 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
3133                         error = EINVAL;
3134                         break;
3135                 }
3136                 paddrp = mtod(m, struct sctp_paddrparams *);
3137                 net = NULL;
3138                 if (paddrp->spp_assoc_id) {
3139                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3140                                 SCTP_INP_RLOCK(inp);
3141                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3142                                 if (stcb) {
3143                                         SCTP_TCB_LOCK(stcb);
3144                                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3145                                 }
3146                                 SCTP_INP_RUNLOCK(inp);
3147                         } else
3148                                 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
3149                         if (stcb == NULL) {
3150                                 error = ENOENT;
3151                                 break;
3152                         }
3153
3154                 }
3155                 if ((stcb == NULL) &&
3156                     ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
3157                      (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
3158                         /* Lookup via address */
3159                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3160                                 SCTP_INP_RLOCK(inp);
3161                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3162                                 if (stcb) {
3163                                         SCTP_TCB_LOCK(stcb);
3164                                         net = sctp_findnet(stcb,
3165                                                            (struct sockaddr *)&paddrp->spp_address);
3166                                 }
3167                                 SCTP_INP_RUNLOCK(inp);
3168                         } else {
3169                                 SCTP_INP_WLOCK(inp);
3170                                 SCTP_INP_INCR_REF(inp);
3171                                 SCTP_INP_WUNLOCK(inp);
3172                                 stcb = sctp_findassociation_ep_addr(&inp,
3173                                                                     (struct sockaddr *)&paddrp->spp_address,
3174                                                                     &net, NULL, NULL);
3175                                 if (stcb == NULL) {
3176                                         SCTP_INP_WLOCK(inp);
3177                                         SCTP_INP_DECR_REF(inp);
3178                                         SCTP_INP_WUNLOCK(inp);
3179                                 }
3180                         }
3181                 } else {
3182                         /* Effects the Endpoint */
3183                         stcb = NULL;
3184                 }
3185                 if (stcb) {
3186                         /* Applies to the specific association */
3187                         if (paddrp->spp_pathmaxrxt) {
3188                                 if (net) {
3189                                         if (paddrp->spp_pathmaxrxt)
3190                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
3191                                 } else {
3192                                         if (paddrp->spp_pathmaxrxt)
3193                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3194                                 }
3195                         }
3196                         if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
3197                                 /* Just a set */
3198                                 int old;
3199                                 if (net) {
3200                                         net->dest_state &= ~SCTP_ADDR_NOHB;
3201                                 } else {
3202                                         old = stcb->asoc.heart_beat_delay;
3203                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3204                                         if (old == 0) {
3205                                                 /* Turn back on the timer */
3206                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3207                                         }
3208                                 }
3209                         } else if (paddrp->spp_hbinterval == 0xffffffff) {
3210                                 /* on demand HB */
3211                                 sctp_send_hb(stcb, 1, net);
3212                         } else {
3213                                 if (net == NULL) {
3214                                         /* off on association */
3215                                         if (stcb->asoc.heart_beat_delay) {
3216                                                 int cnt_of_unconf = 0;
3217                                                 struct sctp_nets *lnet;
3218                                                 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3219                                                         if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3220                                                                 cnt_of_unconf++;
3221                                                         }
3222                                                 }
3223                                                 /* stop the timer ONLY if we have no unconfirmed addresses
3224                                                  */
3225                                                 if (cnt_of_unconf == 0)
3226                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3227                                         }
3228                                         stcb->asoc.heart_beat_delay = 0;
3229                                 } else {
3230                                         net->dest_state |= SCTP_ADDR_NOHB;
3231                                 }
3232                         }
3233                         SCTP_TCB_UNLOCK(stcb);
3234                 } else {
3235                         /* Use endpoint defaults */
3236                         SCTP_INP_WLOCK(inp);
3237                         if (paddrp->spp_pathmaxrxt)
3238                                 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3239                         if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
3240                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
3241                         SCTP_INP_WUNLOCK(inp);
3242                 }
3243         }
3244         break;
3245         case SCTP_RTOINFO:
3246         {
3247                 struct sctp_rtoinfo *srto;
3248                 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
3249                         error = EINVAL;
3250                         break;
3251                 }
3252                 srto = mtod(m, struct sctp_rtoinfo *);
3253                 if (srto->srto_assoc_id == 0) {
3254                         SCTP_INP_WLOCK(inp);
3255                         /* If we have a null asoc, its default for the endpoint */
3256                         if (srto->srto_initial > 10)
3257                                 inp->sctp_ep.initial_rto = srto->srto_initial;
3258                         if (srto->srto_max > 10)
3259                                 inp->sctp_ep.sctp_maxrto = srto->srto_max;
3260                         if (srto->srto_min > 10)
3261                                 inp->sctp_ep.sctp_minrto = srto->srto_min;
3262                         SCTP_INP_WUNLOCK(inp);
3263                         break;
3264                 }
3265                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3266                         SCTP_INP_RLOCK(inp);
3267                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3268                         if (stcb)
3269                                 SCTP_TCB_LOCK(stcb);
3270                         SCTP_INP_RUNLOCK(inp);
3271                 } else
3272                         stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
3273                 if (stcb == NULL) {
3274                         error = EINVAL;
3275                         break;
3276                 }
3277                 /* Set in ms we hope :-) */
3278                 if (srto->srto_initial > 10)
3279                         stcb->asoc.initial_rto = srto->srto_initial;
3280                 if (srto->srto_max > 10)
3281                         stcb->asoc.maxrto = srto->srto_max;
3282                 if (srto->srto_min > 10)
3283                         stcb->asoc.minrto = srto->srto_min;
3284                 SCTP_TCB_UNLOCK(stcb);
3285         }
3286         break;
3287         case SCTP_ASSOCINFO:
3288         {
3289                 struct sctp_assocparams *sasoc;
3290
3291                 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
3292                         error = EINVAL;
3293                         break;
3294                 }
3295                 sasoc = mtod(m, struct sctp_assocparams *);
3296                 if (sasoc->sasoc_assoc_id) {
3297                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3298                                 SCTP_INP_RLOCK(inp);
3299                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3300                                 if (stcb)
3301                                         SCTP_TCB_LOCK(stcb);
3302                                 SCTP_INP_RUNLOCK(inp);
3303                         } else
3304                                 stcb = sctp_findassociation_ep_asocid(inp,
3305                                                                       sasoc->sasoc_assoc_id);
3306                         if (stcb == NULL) {
3307                                 error = ENOENT;
3308                                 break;
3309                         }
3310
3311                 } else {
3312                         stcb = NULL;
3313                 }
3314                 if (stcb) {
3315                         if (sasoc->sasoc_asocmaxrxt)
3316                                 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3317                         sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3318                         sasoc->sasoc_peer_rwnd = 0;
3319                         sasoc->sasoc_local_rwnd = 0;
3320                         if (stcb->asoc.cookie_life)
3321                                 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3322                         SCTP_TCB_UNLOCK(stcb);
3323                 } else {
3324                         SCTP_INP_WLOCK(inp);
3325                         if (sasoc->sasoc_asocmaxrxt)
3326                                 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3327                         sasoc->sasoc_number_peer_destinations = 0;
3328                         sasoc->sasoc_peer_rwnd = 0;
3329                         sasoc->sasoc_local_rwnd = 0;
3330                         if (sasoc->sasoc_cookie_life)
3331                                 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3332                         SCTP_INP_WUNLOCK(inp);
3333                 }
3334         }
3335         break;
3336         case SCTP_INITMSG:
3337         {
3338                 struct sctp_initmsg *sinit;
3339
3340                 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
3341                         error = EINVAL;
3342                         break;
3343                 }
3344                 sinit = mtod(m, struct sctp_initmsg *);
3345                 SCTP_INP_WLOCK(inp);
3346                 if (sinit->sinit_num_ostreams)
3347                         inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3348
3349                 if (sinit->sinit_max_instreams)
3350                         inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3351
3352                 if (sinit->sinit_max_attempts)
3353                         inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3354
3355                 if (sinit->sinit_max_init_timeo > 10)
3356                         /* We must be at least a 100ms (we set in ticks) */
3357                         inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3358                 SCTP_INP_WUNLOCK(inp);
3359         }
3360         break;
3361         case SCTP_PRIMARY_ADDR:
3362         {
3363                 struct sctp_setprim *spa;
3364                 struct sctp_nets *net, *lnet;
3365                 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
3366                         error = EINVAL;
3367                         break;
3368                 }
3369                 spa = mtod(m, struct sctp_setprim *);
3370
3371                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3372                         SCTP_INP_RLOCK(inp);
3373                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3374                         if (stcb) {
3375                                 SCTP_TCB_LOCK(stcb);
3376                         } else {
3377                                 error = EINVAL;
3378                                 break;
3379                         }
3380                         SCTP_INP_RUNLOCK(inp);
3381                 } else
3382                         stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
3383                 if (stcb == NULL) {
3384                         /* One last shot */
3385                         SCTP_INP_WLOCK(inp);
3386                         SCTP_INP_INCR_REF(inp);
3387                         SCTP_INP_WUNLOCK(inp);
3388                         stcb = sctp_findassociation_ep_addr(&inp,
3389                                                             (struct sockaddr *)&spa->ssp_addr,
3390                                                             &net, NULL, NULL);
3391                         if (stcb == NULL) {
3392                                 SCTP_INP_WLOCK(inp);
3393                                 SCTP_INP_DECR_REF(inp);
3394                                 SCTP_INP_WUNLOCK(inp);
3395                                 error = EINVAL;
3396                                 break;
3397                         }
3398                 } else {
3399                         /* find the net, associd or connected lookup type */
3400                         net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3401                         if (net == NULL) {
3402                                 SCTP_TCB_UNLOCK(stcb);
3403                                 error = EINVAL;
3404                                 break;
3405                         }
3406                 }
3407                 if ((net != stcb->asoc.primary_destination) &&
3408                     (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3409                         /* Ok we need to set it */
3410                         lnet = stcb->asoc.primary_destination;
3411                         lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
3412                         if (sctp_set_primary_addr(stcb,
3413                                                   (struct sockaddr *)NULL,
3414                                                   net) == 0) {
3415                                 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3416                                         net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3417                                 }
3418                                 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3419                         }
3420                 }
3421                 SCTP_TCB_UNLOCK(stcb);
3422         }
3423         break;
3424
3425         case SCTP_SET_PEER_PRIMARY_ADDR:
3426         {
3427                 struct sctp_setpeerprim *sspp;
3428                 if ((size_t)m->m_len < sizeof(struct sctp_setpeerprim)) {
3429                         error = EINVAL;
3430                         break;
3431                 }
3432                 sspp = mtod(m, struct sctp_setpeerprim *);
3433
3434
3435                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3436                         SCTP_INP_RLOCK(inp);
3437                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3438                         if (stcb)
3439                                 SCTP_TCB_UNLOCK(stcb);
3440                         SCTP_INP_RUNLOCK(inp);
3441                 } else
3442                         stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
3443                 if (stcb == NULL) {
3444                         error = EINVAL;
3445                         break;
3446                 }
3447                 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3448                         error = EINVAL;
3449                 }
3450                 SCTP_TCB_UNLOCK(stcb);
3451         }
3452         break;
3453         case SCTP_BINDX_ADD_ADDR:
3454         {
3455                 struct sctp_getaddresses *addrs;
3456                 struct sockaddr *addr_touse;
3457                 struct sockaddr_in sin;
3458                 /* see if we're bound all already! */
3459                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3460                         error = EINVAL;
3461                         break;
3462                 }
3463                 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3464                         error = EINVAL;
3465                         break;
3466                 }
3467                 addrs = mtod(m, struct sctp_getaddresses *);
3468                 addr_touse = addrs->addr;
3469                 if (addrs->addr->sa_family == AF_INET6) {
3470                         struct sockaddr_in6 *sin6;
3471                         sin6 = (struct sockaddr_in6 *)addr_touse;
3472                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3473                                 in6_sin6_2_sin(&sin, sin6);
3474                                 addr_touse = (struct sockaddr *)&sin;
3475                         }
3476                 }
3477                 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3478                         if (p == NULL) {
3479                                 /* Can't get proc for Net/Open BSD */
3480                                 error = EINVAL;
3481                                 break;
3482                         }
3483                         error = sctp_inpcb_bind(so, addr_touse, p);
3484                         break;
3485                 }
3486                 /* No locks required here since bind and mgmt_ep_sa all
3487                  * do their own locking. If we do something for the FIX:
3488                  * below we may need to lock in that case.
3489                  */
3490                 if (addrs->sget_assoc_id == 0) {
3491                         /* add the address */
3492                         struct sctp_inpcb  *lep;
3493                         ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3494                         lep = sctp_pcb_findep(addr_touse, 1, 0);
3495                         if (lep != NULL) {
3496                                 /* We must decrement the refcount
3497                                  * since we have the ep already and
3498                                  * are binding. No remove going on
3499                                  * here.
3500                                  */
3501                                 SCTP_INP_WLOCK(inp);
3502                                 SCTP_INP_DECR_REF(inp);
3503                                 SCTP_INP_WUNLOCK(inp);
3504                         }
3505                         if (lep == inp) {
3506                                 /* already bound to it.. ok */
3507                                 break;
3508                         } else if (lep == NULL) {
3509                                 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3510                                 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3511                                                              SCTP_ADD_IP_ADDRESS);
3512                         } else {
3513                                 error = EADDRNOTAVAIL;
3514                         }
3515                         if (error)
3516                                 break;
3517
3518                 } else {
3519                         /* FIX: decide whether we allow assoc based bindx */
3520                 }
3521         }
3522         break;
3523         case SCTP_BINDX_REM_ADDR:
3524         {
3525                 struct sctp_getaddresses *addrs;
3526                 struct sockaddr *addr_touse;
3527                 struct sockaddr_in sin;
3528                 /* see if we're bound all already! */
3529                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3530                         error = EINVAL;
3531                         break;
3532                 }
3533                 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3534                         error = EINVAL;
3535                         break;
3536                 }
3537                 addrs = mtod(m, struct sctp_getaddresses *);
3538                 addr_touse = addrs->addr;
3539                 if (addrs->addr->sa_family == AF_INET6) {
3540                         struct sockaddr_in6 *sin6;
3541                         sin6 = (struct sockaddr_in6 *)addr_touse;
3542                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3543                                 in6_sin6_2_sin(&sin, sin6);
3544                                 addr_touse = (struct sockaddr *)&sin;
3545                         }
3546                 }
3547                 /* No lock required mgmt_ep_sa does its own locking. If
3548                  * the FIX: below is ever changed we may need to
3549                  * lock before calling association level binding.
3550                  */
3551                 if (addrs->sget_assoc_id == 0) {
3552                         /* delete the address */
3553                         sctp_addr_mgmt_ep_sa(inp, addr_touse,
3554                                              SCTP_DEL_IP_ADDRESS);
3555                 } else {
3556                         /* FIX: decide whether we allow assoc based bindx */
3557                 }
3558         }
3559         break;
3560         default:
3561                 error = ENOPROTOOPT;
3562                 break;
3563         } /* end switch (opt) */
3564         return (error);
3565 }
3566
3567
3568 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3569 int
3570 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3571 {
3572         struct mbuf *m = NULL;
3573         struct sctp_inpcb *inp;
3574         int error;
3575
3576         inp = (struct sctp_inpcb *)so->so_pcb;
3577         crit_enter();
3578         if (inp == 0) {
3579                 crit_exit();
3580                 /* I made the same as TCP since we are not setup? */
3581                 return (ECONNRESET);
3582         }
3583         if (sopt->sopt_level != IPPROTO_SCTP) {
3584                 /* wrong proto level... send back up to IP */
3585 #ifdef INET6
3586                 if (INP_CHECK_SOCKAF(so, AF_INET6))
3587                         error = ip6_ctloutput(so, sopt);
3588                 else
3589 #endif /* INET6 */
3590                         error = ip_ctloutput(so, sopt);
3591                 crit_exit();
3592                 return (error);
3593         }
3594         if (sopt->sopt_valsize > MCLBYTES) {
3595                 /*
3596                  * Restrict us down to a cluster size, that's all we can
3597                  * pass either way...
3598                  */
3599                 sopt->sopt_valsize = MCLBYTES;
3600         }
3601         if (sopt->sopt_valsize) {
3602
3603                 m = m_get(MB_WAIT, MT_DATA);
3604                 if (sopt->sopt_valsize > MLEN) {
3605                         MCLGET(m, MB_DONTWAIT);
3606                         if ((m->m_flags & M_EXT) == 0) {
3607                                 sctp_m_freem(m);
3608                                 crit_exit();
3609                                 return (ENOBUFS);
3610                         }
3611                 }
3612                 error = sooptcopyin(sopt, mtod(m, caddr_t), sopt->sopt_valsize,
3613                                     sopt->sopt_valsize);
3614                 if (error) {
3615                         m_free(m);
3616                         goto out;
3617                 }
3618                 m->m_len = sopt->sopt_valsize;
3619         }
3620         if (sopt->sopt_dir == SOPT_SET) {
3621 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3622                 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_td);
3623 #else
3624                 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_p);
3625 #endif
3626         } else if (sopt->sopt_dir == SOPT_GET) {
3627 #if (defined (__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3628                 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_td);
3629 #else
3630                 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_p);
3631 #endif
3632         } else {
3633                 error = EINVAL;
3634         }
3635         if ( (error == 0) && (m != NULL)) {
3636                 error = sooptcopyout(sopt, mtod(m, caddr_t), m->m_len);
3637                 sctp_m_freem(m);
3638         } else if (m != NULL) {
3639                 sctp_m_freem(m);
3640         }
3641  out:
3642         crit_exit();
3643         return (error);
3644 }
3645
3646 #else
3647 /* NetBSD and OpenBSD */
3648 int
3649 sctp_ctloutput(int op, struct socket *so, int level, int optname,
3650                struct mbuf **mp)
3651 {
3652         int s, error;
3653         struct inpcb *inp;
3654 #ifdef INET6
3655         struct in6pcb *in6p;
3656 #endif
3657         int family;     /* family of the socket */
3658
3659         family = so->so_proto->pr_domain->dom_family;
3660         error = 0;
3661         crit_exit();
3662         switch (family) {
3663         case PF_INET:
3664                 inp = sotoinpcb(so);
3665 #ifdef INET6
3666                 in6p = NULL;
3667 #endif
3668                 break;
3669 #ifdef INET6
3670         case PF_INET6:
3671                 inp = NULL;
3672                 in6p = sotoin6pcb(so);
3673                 break;
3674 #endif
3675         default:
3676                 crit_exit();
3677                 return EAFNOSUPPORT;
3678         }
3679 #ifndef INET6
3680         if (inp == NULL)
3681 #else
3682                 if (inp == NULL && in6p == NULL)
3683 #endif
3684                 {
3685                         crit_exit();
3686                         if (op == PRCO_SETOPT && *mp)
3687                                 m_free(*mp);
3688                         return (ECONNRESET);
3689                 }
3690         if (level != IPPROTO_SCTP) {
3691                 switch (family) {
3692                 case PF_INET:
3693                         error = ip_ctloutput(op, so, level, optname, mp);
3694                         break;
3695 #ifdef INET6
3696                 case PF_INET6:
3697                         error = ip6_ctloutput(op, so, level, optname, mp);
3698                         break;
3699 #endif
3700                 }
3701                 crit_exit();
3702                 return (error);
3703         }
3704         /* Ok if we reach here it is a SCTP option we hope */
3705         if (op == PRCO_SETOPT) {
3706                 error = sctp_optsset(so, optname, mp, (struct proc *)NULL);
3707                 if (*mp)
3708                         m_free(*mp);
3709         } else if (op ==  PRCO_GETOPT) {
3710                 error = sctp_optsget(so, optname, mp, (struct proc *)NULL);
3711         } else {
3712                 error = EINVAL;
3713         }
3714         crit_exit();
3715         return (error);
3716 }
3717
3718 #endif
3719
3720 static int
3721 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3722 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3723 {
3724 #else
3725 #if defined(__FreeBSD__) || defined(__APPLE__)
3726 sctp_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
3727 {
3728 #else
3729 sctp_connect(struct socket *so, struct mbuf *nam, struct proc *p)
3730 {
3731         struct sockaddr *addr = mtod(nam, struct sockaddr *);
3732 #endif
3733 #endif
3734         int error = 0;
3735         struct sctp_inpcb *inp;
3736         struct sctp_tcb *stcb;
3737
3738 #ifdef SCTP_DEBUG
3739         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3740                 kprintf("Connect called in SCTP to ");
3741                 sctp_print_address(addr);
3742                 kprintf("Port %d\n", ntohs(((struct sockaddr_in *)addr)->sin_port));
3743         }
3744 #endif /* SCTP_DEBUG */
3745         crit_enter();
3746         inp = (struct sctp_inpcb *)so->so_pcb;
3747         if (inp == 0) {
3748                 crit_exit();
3749                 /* I made the same as TCP since we are not setup? */
3750                 return (ECONNRESET);
3751         }
3752         SCTP_ASOC_CREATE_LOCK(inp);
3753         SCTP_INP_WLOCK(inp);
3754         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3755             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3756                 /* Should I really unlock ? */
3757                 SCTP_INP_WUNLOCK(inp);
3758                 SCTP_ASOC_CREATE_UNLOCK(inp);
3759                 crit_exit();
3760                 return (EFAULT);
3761         }
3762 #ifdef INET6
3763         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3764             (addr->sa_family == AF_INET6)) {
3765                 SCTP_INP_WUNLOCK(inp);
3766                 SCTP_ASOC_CREATE_UNLOCK(inp);
3767                 crit_exit();
3768                 return (EINVAL);
3769         }
3770 #endif /* INET6 */
3771         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3772             SCTP_PCB_FLAGS_UNBOUND) {
3773                 /* Bind a ephemeral port */
3774                 SCTP_INP_WUNLOCK(inp);
3775                 error = sctp_inpcb_bind(so, NULL, p);
3776                 if (error) {
3777                         SCTP_ASOC_CREATE_UNLOCK(inp);
3778                         crit_exit();
3779                         return (error);
3780                 }
3781                 SCTP_INP_WLOCK(inp);
3782         }
3783         /* Now do we connect? */
3784         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3785             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3786                 /* We are already connected AND the TCP model */
3787                 crit_exit();
3788                 SCTP_INP_WUNLOCK(inp);
3789                 SCTP_ASOC_CREATE_UNLOCK(inp);
3790                 return (EADDRINUSE);
3791         }
3792         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3793                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3794                 if (stcb)
3795                         SCTP_TCB_UNLOCK(stcb);
3796                 SCTP_INP_WUNLOCK(inp);
3797         } else {
3798                 SCTP_INP_INCR_REF(inp);
3799                 SCTP_INP_WUNLOCK(inp);
3800                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3801                 if (stcb == NULL) {
3802                         SCTP_INP_WLOCK(inp);
3803                         SCTP_INP_DECR_REF(inp);
3804                         SCTP_INP_WUNLOCK(inp);
3805                 }
3806         }
3807         if (stcb != NULL) {
3808                 /* Already have or am bring up an association */
3809                 SCTP_ASOC_CREATE_UNLOCK(inp);
3810                 SCTP_TCB_UNLOCK(stcb);
3811                 crit_exit();
3812                 return (EALREADY);
3813         }
3814         /* We are GOOD to go */
3815         stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
3816         if (stcb == NULL) {
3817                 /* Gak! no memory */
3818                 crit_exit();
3819                 return (error);
3820         }
3821         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3822                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3823                 /* Set the connected flag so we can queue data */
3824                 soisconnecting(so);
3825         }
3826         stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3827         SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3828         sctp_send_initiate(inp, stcb);
3829         SCTP_ASOC_CREATE_UNLOCK(inp);
3830         SCTP_TCB_UNLOCK(stcb);
3831         crit_exit();
3832         return error;
3833 }
3834
3835 int
3836 sctp_usr_recvd(struct socket *so, int flags)
3837 {
3838         struct sctp_socket_q_list *sq=NULL;
3839         /*
3840          * The user has received some data, we may be able to stuff more
3841          * up the socket. And we need to possibly update the rwnd.
3842          */
3843         struct sctp_inpcb *inp;
3844         struct sctp_tcb *stcb=NULL;
3845
3846         inp = (struct sctp_inpcb *)so->so_pcb;
3847 #ifdef SCTP_DEBUG
3848         if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3849                 kprintf("Read for so:%x inp:%x Flags:%x\n",
3850                        (u_int)so, (u_int)inp, (u_int)flags);
3851 #endif
3852
3853         if (inp == 0) {
3854                 /* I made the same as TCP since we are not setup? */
3855 #ifdef SCTP_DEBUG
3856                 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3857                         kprintf("Nope, connection reset\n");
3858 #endif
3859                 return (ECONNRESET);
3860         }
3861         crit_enter();
3862         /*
3863          * Grab the first one on the list. It will re-insert itself if
3864          * it runs out of room
3865          */
3866         SCTP_INP_WLOCK(inp);
3867         if ((flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3868             && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3869                 /* Ok the other part of our grubby tracking
3870                  * stuff for our horrible layer violation that
3871                  * the tsvwg thinks is ok for sctp_peeloff.. gak!
3872                  * We must update the next vtag pending on the
3873                  * socket buffer (if any).
3874                  */
3875                 inp->sctp_vtag_first = sctp_get_first_vtag_from_sb(so);
3876                 sq = TAILQ_FIRST(&inp->sctp_queue_list);
3877                 if (sq) {
3878                         stcb = sq->tcb;
3879                 } else {
3880                         stcb = NULL;
3881                 }
3882         } else {
3883                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3884         }
3885         if (stcb)
3886                 SCTP_TCB_LOCK(stcb);
3887         if (stcb) {
3888                 long incr;
3889                 /* all code in normal stcb path assumes
3890                  * that you have a tcb_lock only. Thus
3891                  * we must release the inp write lock.
3892                  */
3893                 if (flags & MSG_EOR) {
3894                         if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3895                            && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3896                                 stcb = sctp_remove_from_socket_q(inp);
3897                         }
3898 #ifdef SCTP_DEBUG
3899                         if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3900                                 kprintf("remove from socket queue for inp:%x tcbret:%x\n",
3901                                        (u_int)inp, (u_int)stcb);
3902 #endif
3903
3904                         stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3905                                                                           sizeof(struct mbuf));
3906                         if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) {
3907                                 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3908                                                                                   CMSG_LEN(sizeof(struct sctp_sndrcvinfo)));
3909                         }
3910                 }
3911                 if ((TAILQ_EMPTY(&stcb->asoc.delivery_queue) == 0) ||
3912                     (TAILQ_EMPTY(&stcb->asoc.reasmqueue) == 0)) {
3913                         /* Deliver if there is something to be delivered */
3914                         sctp_service_queues(stcb, &stcb->asoc, 1);
3915                 }
3916                 sctp_set_rwnd(stcb, &stcb->asoc);
3917                 /* if we increase by 1 or more MTU's (smallest MTUs of all
3918                  * nets) we send a window update sack
3919                  */
3920                 incr = stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd;
3921                 if (incr < 0) {
3922                         incr = 0;
3923                 }
3924                 if (((uint32_t)incr >= (stcb->asoc.smallest_mtu * SCTP_SEG_TO_RWND_UPD)) ||
3925                     ((((uint32_t)incr)*SCTP_SCALE_OF_RWND_TO_UPD) >= so->so_rcv.ssb_hiwat)) {
3926                         if (callout_pending(&stcb->asoc.dack_timer.timer)) {
3927                                 /* If the timer is up, stop it */
3928                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
3929                                                 stcb->sctp_ep, stcb, NULL);
3930                         }
3931                         /* Send the sack, with the new rwnd */
3932                         sctp_send_sack(stcb);
3933                         /* Now do the output */
3934                         sctp_chunk_output(inp, stcb, 10);
3935                 }
3936         } else {
3937                 if ((( sq ) && (flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0))
3938                     && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3939                         stcb = sctp_remove_from_socket_q(inp);
3940                 }
3941         }
3942         SOCKBUF_LOCK(&so->so_rcv);
3943         if (( so->so_rcv.ssb_mb == NULL ) &&
3944             (TAILQ_EMPTY(&inp->sctp_queue_list) == 0)) {
3945                 int sq_cnt=0;
3946 #ifdef SCTP_DEBUG
3947                 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3948                         kprintf("Something off, inp:%x so->so_rcv->ssb_mb is empty and sockq is not.. cleaning\n",
3949                                (u_int)inp);
3950 #endif
3951                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3952                    && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3953                         int done_yet;
3954                         done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3955                         while (!done_yet) {
3956                                 sq_cnt++;
3957                                 sctp_remove_from_socket_q(inp);
3958                                 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3959                         }
3960                 }
3961 #ifdef SCTP_DEBUG
3962                 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3963                         kprintf("Cleaned up %d sockq's\n", sq_cnt);
3964 #endif
3965         }
3966         SOCKBUF_UNLOCK(&so->so_rcv);
3967         if (stcb)
3968                 SCTP_TCB_UNLOCK(stcb);
3969         SCTP_INP_WUNLOCK(inp);
3970         crit_exit();
3971         return (0);
3972 }
3973
3974 int
3975 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3976 sctp_listen(struct socket *so, struct thread *p)
3977 #else
3978 sctp_listen(struct socket *so, struct proc *p)
3979 #endif
3980 {
3981         /*
3982          * Note this module depends on the protocol processing being
3983          * called AFTER any socket level flags and backlog are applied
3984          * to the socket. The traditional way that the socket flags are
3985          * applied is AFTER protocol processing. We have made a change
3986          * to the sys/kern/uipc_socket.c module to reverse this but this
3987          * MUST be in place if the socket API for SCTP is to work properly.
3988          */
3989         int error = 0;
3990         struct sctp_inpcb *inp;
3991
3992         crit_enter();
3993         inp = (struct sctp_inpcb *)so->so_pcb;
3994         if (inp == 0) {
3995                 crit_exit();
3996                 /* I made the same as TCP since we are not setup? */
3997                 return (ECONNRESET);
3998         }
3999         SCTP_INP_RLOCK(inp);
4000         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4001             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
4002                 /* We are already connected AND the TCP model */
4003                 crit_exit();
4004                 SCTP_INP_RUNLOCK(inp);
4005                 return (EADDRINUSE);
4006         }
4007         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4008                 /* We must do a bind. */
4009                 SCTP_INP_RUNLOCK(inp);
4010                 if ((error = sctp_inpcb_bind(so, NULL, p))) {
4011                         /* bind error, probably perm */
4012                         crit_exit();
4013                         return (error);
4014                 }
4015         } else {
4016                 SCTP_INP_RUNLOCK(inp);
4017         }
4018         SOCK_LOCK(so);
4019         SCTP_INP_WLOCK(inp);
4020         if (inp->sctp_socket->so_qlimit) {
4021                 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4022                         /*
4023                          * For the UDP model we must TURN OFF the ACCEPT
4024                          * flags since we do NOT allow the accept() call.
4025                          * The TCP model (when present) will do accept which
4026                          * then prohibits connect().
4027                          */
4028                         inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
4029                 }
4030                 inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
4031         } else {
4032                 if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
4033                         /*
4034                          * Turning off the listen flags if the backlog is
4035                          * set to 0 (i.e. qlimit is 0).
4036                          */
4037                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
4038                 }
4039                 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
4040         }
4041         SCTP_INP_WUNLOCK(inp);
4042         SOCK_UNLOCK(so);
4043         crit_exit();
4044         return (error);
4045 }
4046
4047 int
4048 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4049 sctp_accept(struct socket *so, struct sockaddr **addr)
4050 {
4051 #else
4052 sctp_accept(struct socket *so, struct mbuf *nam)
4053 {
4054         struct sockaddr *addr = mtod(nam, struct sockaddr *);
4055 #endif
4056         struct sctp_tcb *stcb;
4057         struct sockaddr *prim;
4058         struct sctp_inpcb *inp;
4059
4060         crit_enter();
4061         inp = (struct sctp_inpcb *)so->so_pcb;
4062
4063         if (inp == 0) {
4064                 crit_exit();
4065                 return (ECONNRESET);
4066         }
4067         SCTP_INP_RLOCK(inp);
4068         if (so->so_state & SS_ISDISCONNECTED) {
4069                 crit_exit();
4070                 SCTP_INP_RUNLOCK(inp);
4071                 return (ECONNABORTED);
4072         }
4073         stcb = LIST_FIRST(&inp->sctp_asoc_list);
4074         if (stcb == NULL) {
4075                 crit_exit();
4076                 SCTP_INP_RUNLOCK(inp);
4077                 return (ECONNRESET);
4078         }
4079         SCTP_TCB_LOCK(stcb);
4080         SCTP_INP_RUNLOCK(inp);
4081         prim = (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr;
4082         if (prim->sa_family == AF_INET) {
4083                 struct sockaddr_in *sin;
4084 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4085                 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
4086                        M_WAITOK | M_ZERO);
4087 #else
4088                 sin = (struct sockaddr_in *)addr;
4089                 bzero((caddr_t)sin, sizeof (*sin));
4090 #endif
4091                 sin->sin_family = AF_INET;
4092                 sin->sin_len = sizeof(*sin);
4093                 sin->sin_port = ((struct sockaddr_in *)prim)->sin_port;
4094                 sin->sin_addr = ((struct sockaddr_in *)prim)->sin_addr;
4095 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4096                 *addr = (struct sockaddr *)sin;
4097 #else
4098                 nam->m_len = sizeof(*sin);
4099 #endif
4100         } else {
4101                 struct sockaddr_in6 *sin6;
4102 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4103                 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
4104                        M_WAITOK | M_ZERO);
4105 #else
4106                 sin6 = (struct sockaddr_in6 *)addr;
4107 #endif
4108                 bzero((caddr_t)sin6, sizeof (*sin6));
4109                 sin6->sin6_family = AF_INET6;
4110                 sin6->sin6_len = sizeof(*sin6);
4111                 sin6->sin6_port = ((struct sockaddr_in6 *)prim)->sin6_port;
4112
4113                 sin6->sin6_addr = ((struct sockaddr_in6 *)prim)->sin6_addr;
4114                 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
4115                         /*      sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);*/
4116                         in6_recoverscope(sin6, &sin6->sin6_addr, NULL);  /* skip ifp check */
4117                 else
4118                         sin6->sin6_scope_id = 0;        /*XXX*/
4119 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
4120                 *addr= (struct sockaddr *)sin6;
4121 #else
4122                 nam->m_len = sizeof(*sin6);
4123 #endif
4124         }
4125         /* Wake any delayed sleep action */
4126         SCTP_TCB_UNLOCK(stcb);
4127         SCTP_INP_WLOCK(inp);
4128         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4129                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4130                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4131                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4132 #if defined(__NetBSD__)
4133                         if (sowritable(inp->sctp_socket))
4134                                 sowwakeup(inp->sctp_socket);
4135 #else
4136                         if (sowriteable(inp->sctp_socket))
4137                                 sowwakeup(inp->sctp_socket);
4138 #endif
4139                 }
4140                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4141                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4142                         if (soreadable(inp->sctp_socket))
4143                                 sorwakeup(inp->sctp_socket);
4144                 }
4145
4146         }
4147         SCTP_INP_WUNLOCK(inp);
4148         crit_exit();
4149         return (0);
4150 }
4151
4152 int
4153 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4154 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4155 #else
4156 sctp_ingetaddr(struct socket *so, struct mbuf *nam)
4157 #endif
4158 {
4159 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4160         struct sockaddr_in *sin;
4161 #else
4162         struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
4163 #endif
4164         struct sctp_inpcb *inp;
4165         /*
4166          * Do the malloc first in case it blocks.
4167          */
4168 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4169         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK |
4170                M_ZERO);
4171 #else
4172         nam->m_len = sizeof(*sin);
4173         memset(sin, 0, sizeof(*sin));
4174 #endif
4175         sin->sin_family = AF_INET;
4176         sin->sin_len = sizeof(*sin);
4177         crit_enter();
4178         inp = (struct sctp_inpcb *)so->so_pcb;
4179         if (!inp) {
4180                 crit_exit();
4181 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4182                 FREE(sin, M_SONAME);
4183 #endif
4184                 return ECONNRESET;
4185         }
4186         SCTP_INP_RLOCK(inp);
4187         sin->sin_port = inp->sctp_lport;
4188         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4189                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4190                         struct sctp_tcb *stcb;
4191                         struct sockaddr_in *sin_a;
4192                         struct sctp_nets *net;
4193                         int fnd;
4194
4195                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
4196                         if (stcb == NULL) {
4197                                 goto notConn;
4198                         }
4199                         fnd = 0;
4200                         sin_a = NULL;
4201                         SCTP_TCB_LOCK(stcb);
4202                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4203                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4204                                 if (sin_a->sin_family == AF_INET) {
4205                                         fnd = 1;
4206                                         break;
4207                                 }
4208                         }
4209                         if ((!fnd) || (sin_a == NULL)) {
4210                                 /* punt */
4211                                 SCTP_TCB_UNLOCK(stcb);
4212                                 goto notConn;
4213                         }
4214                         sin->sin_addr = sctp_ipv4_source_address_selection(inp,
4215                             stcb, (struct route *)&net->ro, net, 0);
4216                         SCTP_TCB_UNLOCK(stcb);
4217                 } else {
4218                         /* For the bound all case you get back 0 */
4219                 notConn:
4220                         sin->sin_addr.s_addr = 0;
4221                 }
4222
4223         } else {
4224                 /* Take the first IPv4 address in the list */
4225                 struct sctp_laddr *laddr;
4226                 int fnd = 0;
4227                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4228                         if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
4229                                 struct sockaddr_in *sin_a;
4230                                 sin_a = (struct sockaddr_in *)laddr->ifa->ifa_addr;
4231                                 sin->sin_addr = sin_a->sin_addr;
4232                                 fnd = 1;
4233                                 break;
4234                         }
4235                 }
4236                 if (!fnd) {
4237                         crit_exit();
4238 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4239                         FREE(sin, M_SONAME);
4240 #endif
4241                         SCTP_INP_RUNLOCK(inp);
4242                         return ENOENT;
4243                 }
4244         }
4245         SCTP_INP_RUNLOCK(inp);
4246         crit_exit();
4247 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4248         (*addr) = (struct sockaddr *)sin;
4249 #endif
4250         return (0);
4251 }
4252
4253 int
4254 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4255 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4256 {
4257         struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4258 #else
4259 sctp_peeraddr(struct socket *so, struct mbuf *nam)
4260 {
4261         struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
4262 #endif
4263         int fnd;
4264         struct sockaddr_in *sin_a;
4265         struct sctp_inpcb *inp;
4266         struct sctp_tcb *stcb;
4267         struct sctp_nets *net;
4268
4269         /* Do the malloc first in case it blocks. */
4270         inp = (struct sctp_inpcb *)so->so_pcb;
4271         if ((inp == NULL) ||
4272             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4273                 /* UDP type and listeners will drop out here */
4274                 return (ENOTCONN);
4275         }
4276         crit_enter();
4277
4278 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4279         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK |
4280                M_ZERO);
4281 #else
4282         nam->m_len = sizeof(*sin);
4283         memset(sin, 0, sizeof(*sin));
4284 #endif
4285         sin->sin_family = AF_INET;
4286         sin->sin_len = sizeof(*sin);
4287
4288         /* We must recapture incase we blocked */
4289         inp = (struct sctp_inpcb *)so->so_pcb;
4290         if (!inp) {
4291                 crit_exit();
4292 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4293                 FREE(sin, M_SONAME);
4294 #endif
4295                 return ECONNRESET;
4296         }
4297         SCTP_INP_RLOCK(inp);
4298         stcb = LIST_FIRST(&inp->sctp_asoc_list);
4299         if (stcb)
4300                 SCTP_TCB_LOCK(stcb);
4301         SCTP_INP_RUNLOCK(inp);
4302         if (stcb == NULL) {
4303                 crit_exit();
4304 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4305                 FREE(sin, M_SONAME);
4306 #endif
4307                 return ECONNRESET;
4308         }
4309         fnd = 0;
4310         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4311                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4312                 if (sin_a->sin_family == AF_INET) {
4313                         fnd = 1;
4314                         sin->sin_port = stcb->rport;
4315                         sin->sin_addr = sin_a->sin_addr;
4316                         break;
4317                 }
4318         }
4319         SCTP_TCB_UNLOCK(stcb);
4320         if (!fnd) {
4321                 /* No IPv4 address */
4322                 crit_exit();
4323 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4324                 FREE(sin, M_SONAME);
4325 #endif
4326                 return ENOENT;
4327         }
4328         crit_exit();
4329         return (0);
4330 }
4331
4332 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4333 struct pr_usrreqs sctp_usrreqs = {
4334         .pru_abort = sctp_abort,
4335         .pru_accept = sctp_accept,
4336         .pru_attach = sctp_attach,
4337         .pru_bind = sctp_bind,
4338         .pru_connect = sctp_connect,
4339         .pru_connect2 = pru_connect2_notsupp,
4340         .pru_control = in_control,
4341         .pru_detach = sctp_detach,
4342         .pru_disconnect = sctp_disconnect,
4343         .pru_listen = sctp_listen,
4344         .pru_peeraddr = sctp_peeraddr,
4345         .pru_rcvd = sctp_usr_recvd,
4346         .pru_rcvoob = pru_rcvoob_notsupp,
4347         .pru_send = sctp_send,
4348         .pru_sense = pru_sense_null,
4349         .pru_shutdown = sctp_shutdown,
4350         .pru_sockaddr = sctp_ingetaddr,
4351         .pru_sosend = sctp_sosend,
4352         .pru_soreceive = soreceive,
4353         .pru_sopoll = sopoll
4354 };
4355
4356 #else
4357 #if defined(__NetBSD__)
4358 int
4359 sctp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
4360             struct mbuf *control, struct proc *p)
4361 {
4362 #else
4363 int
4364 sctp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
4365             struct mbuf *control)
4366 {
4367         struct proc *p = curproc;
4368 #endif
4369         int error = 0;
4370         int family;
4371
4372         family = so->so_proto->pr_domain->dom_family;
4373
4374         crit_enter();
4375         if (req == PRU_CONTROL) {
4376                 switch (family) {
4377                 case PF_INET:
4378                         error = in_control(so, (long)m, (caddr_t)nam,
4379                             (struct ifnet *)control
4380 #if defined(__NetBSD__)
4381                             , p
4382 #endif
4383                             );
4384                         break;
4385 #ifdef INET6
4386                 case PF_INET6:
4387                         error = in6_control(so, (long)m, (caddr_t)nam,
4388                             (struct ifnet *)control, p);
4389                         break;
4390 #endif
4391                 default:
4392                         error =  EAFNOSUPPORT;
4393                 }
4394                 crit_exit();
4395                 return (error);
4396         }
4397 #ifdef __NetBSD__
4398         if (req == PRU_PURGEIF) {
4399                 struct ifnet *ifn;
4400                 struct ifaddr *ifa;
4401                 ifn = (struct ifnet *)control;
4402                 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
4403                         if (ifa->ifa_addr->sa_family == family) {
4404                                 sctp_delete_ip_address(ifa);
4405                         }
4406                 }
4407                 switch (family) {
4408                 case PF_INET:
4409                         in_purgeif (ifn);
4410                         break;
4411 #ifdef INET6
4412                 case PF_INET6:
4413                         in6_purgeif (ifn);
4414                         break;
4415 #endif /* INET6 */
4416                 default:
4417                         crit_exit();
4418                         return (EAFNOSUPPORT);
4419                 }
4420                 crit_exit();
4421                 return (0);
4422         }
4423 #endif
4424         switch (req) {
4425         case PRU_ATTACH:
4426                 error = sctp_attach(so, family, p);
4427                 break;
4428         case PRU_DETACH:
4429                 error = sctp_detach(so);
4430                 break;
4431         case PRU_BIND:
4432                 if (nam == NULL) {
4433                         crit_exit();
4434                         return (EINVAL);
4435                 }
4436                 error  = sctp_bind(so, nam, p);
4437                 break;
4438         case PRU_LISTEN:
4439                 error = sctp_listen(so, p);
4440                 break;
4441         case PRU_CONNECT:
4442                 if (nam == NULL) {
4443                         crit_exit();
4444                         return (EINVAL);
4445                 }
4446                 error = sctp_connect(so, nam, p);
4447                 break;
4448         case PRU_DISCONNECT:
4449                 error = sctp_disconnect(so);
4450                 break;
4451         case PRU_ACCEPT:
4452                 if (nam == NULL) {
4453                         crit_exit();
4454                         return (EINVAL);
4455                 }
4456                 error = sctp_accept(so, nam);
4457         break;
4458         case PRU_SHUTDOWN:
4459                 error = sctp_shutdown(so);
4460                 break;
4461
4462         case PRU_RCVD:
4463                 /*
4464                  * For Open and Net BSD, this is real
4465                  * ugly. The mbuf *nam that is passed
4466                  * (by soreceive()) is the int flags c
4467                  * ast as a (mbuf *) yuck!
4468                  */
4469                 error = sctp_usr_recvd(so, (int)((long)nam));
4470                 break;
4471
4472         case PRU_SEND:
4473                 /* Flags are ignored */
4474 #ifdef SCTP_DEBUG
4475                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
4476                         kprintf("Send called on V4 side\n");
4477                 }
4478 #endif
4479                 {
4480                     struct sockaddr *addr;
4481                     if (nam == NULL)
4482                         addr = NULL;
4483                     else
4484                         addr = mtod(nam, struct sockaddr *);
4485
4486                     error = sctp_send(so, 0, m, addr, control, p);
4487                 }
4488                 break;
4489         case PRU_ABORT:
4490                 error = sctp_abort(so);
4491                 break;
4492
4493         case PRU_SENSE:
4494                 error = 0;
4495                 break;
4496         case PRU_RCVOOB:
4497                 error = EAFNOSUPPORT;
4498                 break;
4499         case PRU_SENDOOB:
4500                 error = EAFNOSUPPORT;
4501                 break;
4502         case PRU_PEERADDR:
4503                 error = sctp_peeraddr(so, nam);
4504                 break;
4505         case PRU_SOCKADDR:
4506                 error = sctp_ingetaddr(so, nam);
4507                 break;
4508         case PRU_SLOWTIMO:
4509                 error = 0;
4510                 break;
4511         default:
4512                 break;
4513         }
4514         crit_exit();
4515         return (error);
4516 }
4517 #endif
4518
4519 /* #if defined(__NetBSD__) || defined(__OpenBSD__) */
4520 #if __OpenBSD__
4521 /*
4522  * Sysctl for sctp variables.
4523  */
4524 int
4525 sctp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
4526             size_t newlen)
4527 {
4528
4529         /* All sysctl names at this level are terminal. */
4530         if (namelen != 1)
4531                 return (ENOTDIR);
4532 sysctl_int();
4533
4534         switch (name[0]) {
4535         case SCTPCTL_MAXDGRAM:
4536                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4537                      &sctp_sendspace));
4538         case SCTPCTL_RECVSPACE:
4539                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4540                     &sctp_recvspace));
4541         case SCTPCTL_AUTOASCONF:
4542                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4543                     &sctp_auto_asconf));
4544         case SCTPCTL_ECN_ENABLE:
4545                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4546                                    &sctp_ecn));
4547         case SCTPCTL_ECN_NONCE:
4548                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4549                                    &sctp_ecn_nonce));
4550         case SCTPCTL_STRICT_SACK:
4551                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4552                                    &sctp_strict_sacks));
4553         case SCTPCTL_NOCSUM_LO:
4554                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4555                                    &sctp_no_csum_on_loopback));
4556         case SCTPCTL_STRICT_INIT:
4557                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4558                                    &sctp_strict_init));
4559         case SCTPCTL_PEER_CHK_OH:
4560                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4561                                    &sctp_peer_chunk_oh));
4562         case SCTPCTL_MAXBURST:
4563                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4564                                    &sctp_max_burst_default));
4565         case SCTPCTL_MAXCHUNKONQ:
4566                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4567                                    &sctp_max_chunks_on_queue));
4568         case SCTPCTL_DELAYED_SACK:
4569                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4570                                    &sctp_delayed_sack_time_default));
4571         case SCTPCTL_HB_INTERVAL:
4572                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4573                                    &sctp_heartbeat_interval_default));
4574         case SCTPCTL_PMTU_RAISE:
4575                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4576                                    &sctp_pmtu_raise_time_default));
4577         case SCTPCTL_SHUTDOWN_GUARD:
4578                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4579                                    &sctp_shutdown_guard_time_default));
4580         case SCTPCTL_SECRET_LIFETIME:
4581                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4582                                    &sctp_secret_lifetime_default));
4583         case SCTPCTL_RTO_MAX:
4584                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4585                                    &sctp_rto_max_default));
4586         case SCTPCTL_RTO_MIN:
4587                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4588                                    &sctp_rto_min_default));
4589         case SCTPCTL_RTO_INITIAL:
4590                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4591                                    &sctp_rto_initial_default));
4592         case SCTPCTL_INIT_RTO_MAX:
4593                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4594                                    &sctp_init_rto_max_default));
4595         case SCTPCTL_COOKIE_LIFE:
4596                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4597                                    &sctp_valid_cookie_life_default));
4598         case SCTPCTL_INIT_RTX_MAX:
4599                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4600                                    &sctp_init_rtx_max_default));
4601         case SCTPCTL_ASSOC_RTX_MAX:
4602                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4603                                    &sctp_assoc_rtx_max_default));
4604         case SCTPCTL_PATH_RTX_MAX:
4605                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4606                                    &sctp_path_rtx_max_default));
4607         case SCTPCTL_NR_OUTGOING_STREAMS:
4608                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4609                                    &sctp_nr_outgoing_streams_default));
4610 #ifdef SCTP_DEBUG
4611         case SCTPCTL_DEBUG:
4612                 return (sysctl_int(oldp, oldlenp, newp, newlen,
4613                                    &sctp_debug_on));
4614 #endif
4615         default:
4616                 return (ENOPROTOOPT);
4617         }
4618         /* NOTREACHED */
4619 }
4620 #endif
4621 #if __NetBSD__
4622 /*
4623  * Sysctl for sctp variables.
4624  */
4625 SYSCTL_SETUP(sysctl_net_inet_sctp_setup, "sysctl net.inet.sctp subtree setup")
4626 {
4627
4628         sysctl_createv(clog, 0, NULL, NULL,
4629                        CTLFLAG_PERMANENT,
4630                        CTLTYPE_NODE, "net", NULL,
4631                        NULL, 0, NULL, 0,
4632                        CTL_NET, CTL_EOL);
4633         sysctl_createv(clog, 0, NULL, NULL,
4634                        CTLFLAG_PERMANENT,
4635                        CTLTYPE_NODE, "inet", NULL,
4636                        NULL, 0, NULL, 0,
4637                        CTL_NET, PF_INET, CTL_EOL);
4638         sysctl_createv(clog, 0, NULL, NULL,
4639                        CTLFLAG_PERMANENT,
4640                        CTLTYPE_NODE, "sctp",
4641                        SYSCTL_DESCR("sctp related settings"),
4642                        NULL, 0, NULL, 0,
4643                        CTL_NET, PF_INET, IPPROTO_SCTP, CTL_EOL);
4644
4645        sysctl_createv(clog, 0, NULL, NULL,
4646                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4647                        CTLTYPE_INT, "maxdgram",
4648                        SYSCTL_DESCR("Maximum outgoing SCTP buffer size"),
4649                        NULL, 0, &sctp_sendspace, 0,
4650                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXDGRAM,
4651                        CTL_EOL);
4652
4653        sysctl_createv(clog, 0, NULL, NULL,
4654                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4655                        CTLTYPE_INT, "recvspace",
4656                        SYSCTL_DESCR("Maximum incoming SCTP buffer size"),
4657                        NULL, 0, &sctp_recvspace, 0,
4658                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_RECVSPACE,
4659                        CTL_EOL);
4660
4661        sysctl_createv(clog, 0, NULL, NULL,
4662                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4663                        CTLTYPE_INT, "autoasconf",
4664                        SYSCTL_DESCR("Enable SCTP Auto-ASCONF"),
4665                        NULL, 0, &sctp_auto_asconf, 0,
4666                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_AUTOASCONF,
4667                        CTL_EOL);
4668
4669        sysctl_createv(clog, 0, NULL, NULL,
4670                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4671                        CTLTYPE_INT, "ecn_enable",
4672                        SYSCTL_DESCR("Enable SCTP ECN"),
4673                        NULL, 0, &sctp_ecn, 0,
4674                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_ENABLE,
4675                        CTL_EOL);
4676
4677        sysctl_createv(clog, 0, NULL, NULL,
4678                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4679                        CTLTYPE_INT, "ecn_nonce",
4680                        SYSCTL_DESCR("Enable SCTP ECN Nonce"),
4681                        NULL, 0, &sctp_ecn_nonce, 0,
4682                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_NONCE,
4683                        CTL_EOL);
4684
4685        sysctl_createv(clog, 0, NULL, NULL,
4686                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4687                        CTLTYPE_INT, "strict_sack",
4688                        SYSCTL_DESCR("Enable SCTP Strict SACK checking"),
4689                        NULL, 0, &sctp_strict_sacks, 0,
4690                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_SACK,
4691                        CTL_EOL);
4692
4693        sysctl_createv(clog, 0, NULL, NULL,
4694                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4695                        CTLTYPE_INT, "loopback_nocsum",
4696                        SYSCTL_DESCR("Enable NO Csum on packets sent on loopback"),
4697                        NULL, 0, &sctp_no_csum_on_loopback, 0,
4698                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_NOCSUM_LO,
4699                        CTL_EOL);
4700
4701        sysctl_createv(clog, 0, NULL, NULL,
4702                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4703                        CTLTYPE_INT, "strict_init",
4704                        SYSCTL_DESCR("Enable strict INIT/INIT-ACK singleton enforcement"),
4705                        NULL, 0, &sctp_strict_init, 0,
4706                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_INIT,
4707                        CTL_EOL);
4708
4709        sysctl_createv(clog, 0, NULL, NULL,
4710                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4711                        CTLTYPE_INT, "peer_chkoh",
4712                        SYSCTL_DESCR("Amount to debit peers rwnd per chunk sent"),
4713                        NULL, 0, &sctp_peer_chunk_oh, 0,
4714                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_PEER_CHK_OH,
4715                        CTL_EOL);
4716
4717        sysctl_createv(clog, 0, NULL, NULL,
4718                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4719                        CTLTYPE_INT, "maxburst",
4720                        SYSCTL_DESCR("Default max burst for sctp endpoints"),
4721                        NULL, 0, &sctp_max_burst_default, 0,
4722                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXBURST,
4723                        CTL_EOL);
4724
4725        sysctl_createv(clog, 0, NULL, NULL,
4726                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4727                        CTLTYPE_INT, "maxchunks",
4728                        SYSCTL_DESCR("Default max chunks on queue per asoc"),
4729                        NULL, 0, &sctp_max_chunks_on_queue, 0,
4730                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXCHUNKONQ,
4731                        CTL_EOL);
4732
4733 }
4734 #endif