kernel: Fix some printf format warnings on x86_64.
[dragonfly.git] / sys / netinet / sctp_usrreq.c
1 /*      $KAME: sctp_usrreq.c,v 1.47 2005/03/06 16:04:18 itojun Exp $    */
2
3 /*
4  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Cisco Systems, Inc.
18  * 4. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #if !(defined(__OpenBSD__) || defined(__APPLE__))
35 #include "opt_ipsec.h"
36 #endif
37 #if defined(__FreeBSD__) || defined(__DragonFly__)
38 #include "opt_inet6.h"
39 #include "opt_inet.h"
40 #endif
41 #if defined(__NetBSD__)
42 #include "opt_inet.h"
43 #endif
44
45 #ifdef __APPLE__
46 #include <sctp.h>
47 #elif !defined(__OpenBSD__)
48 #include "opt_sctp.h"
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/domain.h>
57 #include <sys/proc.h>
58 #include <sys/priv.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/socketvar2.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
65
66 #include <sys/thread2.h>
67 #include <sys/msgport2.h>
68
69 #include <net/if.h>
70 #include <net/if_types.h>
71 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
72 #include <net/if_var.h>
73 #endif
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/in6_var.h>
84
85 #include <netinet/ip_icmp.h>
86 #include <netinet/icmp_var.h>
87 #include <netinet/sctp_pcb.h>
88 #include <netinet/sctp_header.h>
89 #include <netinet/sctp_var.h>
90 #include <netinet/sctp_output.h>
91 #include <netinet/sctp_uio.h>
92 #include <netinet/sctp_asconf.h>
93 #include <netinet/sctputil.h>
94 #include <netinet/sctp_indata.h>
95 #include <netinet/sctp_asconf.h>
96 #ifdef IPSEC
97 #ifndef __OpenBSD__
98 #include <netinet6/ipsec.h>
99 #include <netproto/key/key.h>
100 #else
101 #undef IPSEC
102 #endif
103 #endif /* IPSEC */
104
105 #include <net/net_osdep.h>
106
107 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__) || defined(__DragonFly__)
108 #ifndef in6pcb
109 #define in6pcb          inpcb
110 #endif
111 #ifndef sotoin6pcb
112 #define sotoin6pcb      sotoinpcb
113 #endif
114 #endif
115
116 #ifdef SCTP_DEBUG
117 extern u_int32_t sctp_debug_on;
118 #endif /* SCTP_DEBUG */
119
120 /*
121  * sysctl tunable variables
122  */
123 int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF;
124 int sctp_max_burst_default = SCTP_DEF_MAX_BURST;
125 int sctp_peer_chunk_oh = sizeof(struct mbuf);
126 int sctp_strict_init = 1;
127 int sctp_no_csum_on_loopback = 1;
128 unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE;
129 int sctp_sendspace = (128 * 1024);
130 int sctp_recvspace = 128 * (1024 +
131 #ifdef INET6
132                                 sizeof(struct sockaddr_in6)
133 #else
134                                 sizeof(struct sockaddr_in)
135 #endif
136         );
137 int sctp_strict_sacks = 0;
138 int sctp_ecn = 1;
139 int sctp_ecn_nonce = 0;
140
141 unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC;
142 unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC;
143 unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC;
144 unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC;
145 unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC;
146 unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND;
147 unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND;
148 unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL;
149 unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND;
150 unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE;
151 unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT;
152 unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND;
153 unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2;
154 unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL;
155
156 void
157 sctp_init(void)
158 {
159 #ifdef __OpenBSD__
160 #define nmbclusters     nmbclust
161 #endif
162         /* Init the SCTP pcb in sctp_pcb.c */
163         u_long sb_max_adj;
164
165         sctp_pcb_init();
166
167 #ifndef __OpenBSD__
168         if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
169                 sctp_max_chunks_on_queue = nmbclusters;
170 #else
171 /*      if (nmbclust > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
172         sctp_max_chunks_on_queue = nmbclust; FIX ME */
173         sctp_max_chunks_on_queue = nmbclust * 2;
174 #endif
175         /*
176          * Allow a user to take no more than 1/2 the number of clusters
177          * or the SB_MAX whichever is smaller for the send window.
178          */
179         sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
180         sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
181 #ifndef __OpenBSD__
182                              ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT));
183 #else
184                              ((nmbclust/2) * SCTP_DEFAULT_MAXSEGMENT));
185 #endif
186         /*
187          * Now for the recv window, should we take the same amount?
188          * or should I do 1/2 the SB_MAX instead in the SB_MAX min above.
189          * For now I will just copy.
190          */
191         sctp_recvspace = sctp_sendspace;
192 #ifdef __OpenBSD__
193 #undef nmbclusters
194 #endif
195 }
196
197 #ifdef INET6
198 void
199 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
200 {
201         bzero(ip6, sizeof(*ip6));
202
203         ip6->ip6_vfc = IPV6_VERSION;
204         ip6->ip6_plen = ip->ip_len;
205         ip6->ip6_nxt = ip->ip_p;
206         ip6->ip6_hlim = ip->ip_ttl;
207         ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
208                 IPV6_ADDR_INT32_SMP;
209         ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
210         ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
211 }
212 #endif /* INET6 */
213
214 static void
215 sctp_split_chunks(struct sctp_association *asoc,
216                   struct sctp_stream_out *strm,
217                   struct sctp_tmit_chunk *chk)
218 {
219         struct sctp_tmit_chunk *new_chk;
220
221         /* First we need a chunk */
222         new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
223         if (new_chk == NULL) {
224                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
225                 return;
226         }
227         sctppcbinfo.ipi_count_chunk++;
228         sctppcbinfo.ipi_gencnt_chunk++;
229         /* Copy it all */
230         *new_chk = *chk;
231         /*  split the data */
232         new_chk->data = m_split(chk->data, (chk->send_size>>1), MB_DONTWAIT);
233         if (new_chk->data == NULL) {
234                 /* Can't split */
235                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
236                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk);
237                 sctppcbinfo.ipi_count_chunk--;
238                 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
239                         panic("Chunk count is negative");
240                 }
241                 sctppcbinfo.ipi_gencnt_chunk++;
242                 return;
243
244         }
245         /* Data is now split adjust sizes */
246         chk->send_size >>= 1;
247         new_chk->send_size >>= 1;
248
249         chk->book_size >>= 1;
250         new_chk->book_size >>= 1;
251
252         /* now adjust the marks */
253         chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
254         chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG;
255
256         new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG;
257         new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
258
259         /* Increase ref count if dest is set */
260         if (chk->whoTo) {
261                 new_chk->whoTo->ref_count++;
262         }
263         /* now drop it on the end of the list*/
264         asoc->stream_queue_cnt++;
265         TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next);
266 }
267
268 static void
269 sctp_notify_mbuf(struct sctp_inpcb *inp,
270                  struct sctp_tcb *stcb,
271                  struct sctp_nets *net,
272                  struct ip *ip,
273                  struct sctphdr *sh)
274
275 {
276         struct icmp *icmph;
277         int totsz;
278         uint16_t nxtsz;
279
280         /* protection */
281         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
282             (ip == NULL) || (sh == NULL)) {
283                 if (stcb != NULL)
284                         SCTP_TCB_UNLOCK(stcb);
285                 return;
286         }
287         /* First job is to verify the vtag matches what I would send */
288         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
289                 SCTP_TCB_UNLOCK(stcb);
290                 return;
291         }
292         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
293                                                sizeof(struct ip)));
294         if (icmph->icmp_type != ICMP_UNREACH) {
295                 /* We only care about unreachable */
296                 SCTP_TCB_UNLOCK(stcb);
297                 return;
298         }
299         if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
300                 /* not a unreachable message due to frag. */
301                 SCTP_TCB_UNLOCK(stcb);
302                 return;
303         }
304         totsz = ip->ip_len;
305         nxtsz = ntohs(icmph->icmp_seq);
306         if (nxtsz == 0) {
307                 /*
308                  * old type router that does not tell us what the next size
309                  * mtu is. Rats we will have to guess (in a educated fashion
310                  * of course)
311                  */
312                 nxtsz = find_next_best_mtu(totsz);
313         }
314
315         /* Stop any PMTU timer */
316         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
317
318         /* Adjust destination size limit */
319         if (net->mtu > nxtsz) {
320                 net->mtu = nxtsz;
321         }
322         /* now what about the ep? */
323         if (stcb->asoc.smallest_mtu > nxtsz) {
324                 struct sctp_tmit_chunk *chk, *nchk;
325                 struct sctp_stream_out *strm;
326                 /* Adjust that too */
327                 stcb->asoc.smallest_mtu = nxtsz;
328                 /* now off to subtract IP_DF flag if needed */
329
330                 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
331                         if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
332                                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
333                         }
334                 }
335                 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
336                         if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
337                                 /*
338                                  * For this guy we also mark for immediate
339                                  * resend since we sent to big of chunk
340                                  */
341                                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
342                                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
343                                         stcb->asoc.sent_queue_retran_cnt++;
344                                 }
345                                 chk->sent = SCTP_DATAGRAM_RESEND;
346                                 chk->rec.data.doing_fast_retransmit = 0;
347
348                                 /* Clear any time so NO RTT is being done */
349                                 chk->do_rtt = 0;
350                                 stcb->asoc.total_flight -= chk->book_size;
351                                 if (stcb->asoc.total_flight < 0) {
352                                         stcb->asoc.total_flight = 0;
353                                 }
354                                 stcb->asoc.total_flight_count--;
355                                 if (stcb->asoc.total_flight_count < 0) {
356                                         stcb->asoc.total_flight_count = 0;
357                                 }
358                                 net->flight_size -= chk->book_size;
359                                 if (net->flight_size < 0) {
360                                         net->flight_size = 0;
361                                 }
362                         }
363                 }
364                 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
365                         chk = TAILQ_FIRST(&strm->outqueue);
366                         while (chk) {
367                                 nchk = TAILQ_NEXT(chk, sctp_next);
368                                 if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) {
369                                         sctp_split_chunks(&stcb->asoc, strm, chk);
370                                 }
371                                 chk = nchk;
372                         }
373                 }
374         }
375         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
376         SCTP_TCB_UNLOCK(stcb);
377 }
378
379
380 void
381 sctp_notify(struct sctp_inpcb *inp,
382             int error,
383             struct sctphdr *sh,
384             struct sockaddr *to,
385             struct sctp_tcb *stcb,
386             struct sctp_nets *net)
387 {
388         /* protection */
389         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
390             (sh == NULL) || (to == NULL)) {
391 #ifdef SCTP_DEBUG
392                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
393                         kprintf("sctp-notify, bad call\n");
394                 }
395 #endif /* SCTP_DEBUG */
396                 return;
397         }
398         /* First job is to verify the vtag matches what I would send */
399         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
400                 return;
401         }
402
403 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
404
405         if ((error == EHOSTUNREACH) ||  /* Host is not reachable */
406             (error == EHOSTDOWN) ||     /* Host is down */
407             (error == ECONNREFUSED) ||  /* Host refused the connection, (not an abort?) */
408             (error == ENOPROTOOPT)      /* SCTP is not present on host */
409                 ) {
410                 /*
411                  * Hmm reachablity problems we must examine closely.
412                  * If its not reachable, we may have lost a network.
413                  * Or if there is NO protocol at the other end named SCTP.
414                  * well we consider it a OOTB abort.
415                  */
416                 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
417                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
418                                 /* Ok that destination is NOT reachable */
419                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
420                                 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
421                                 net->error_count = net->failure_threshold + 1;
422                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
423                                                 stcb, SCTP_FAILED_THRESHOLD,
424                                                 (void *)net);
425                         }
426                         if (stcb)
427                                 SCTP_TCB_UNLOCK(stcb);
428                 } else {
429                         /*
430                          * Here the peer is either playing tricks on us,
431                          * including an address that belongs to someone who
432                          * does not support SCTP OR was a userland
433                          * implementation that shutdown and now is dead. In
434                          * either case treat it like a OOTB abort with no TCB
435                          */
436                         sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
437                         sctp_free_assoc(inp, stcb);
438                         /* no need to unlock here, since the TCB is gone */
439                 }
440         } else {
441                 /* Send all others to the app */
442                 if (inp->sctp_socket) {
443                         SOCK_LOCK(inp->sctp_socket);
444                         inp->sctp_socket->so_error = error;
445                         sctp_sowwakeup(inp, inp->sctp_socket);
446                         SOCK_UNLOCK(inp->sctp_socket);
447                 }
448                 if (stcb)
449                         SCTP_TCB_UNLOCK(stcb);
450         }
451 }
452
453 void
454 sctp_ctlinput(netmsg_t msg)
455 {
456         int cmd = msg->ctlinput.nm_cmd;
457         struct sockaddr *sa = msg->ctlinput.nm_arg;
458         struct ip *ip = msg->ctlinput.nm_extra;
459         struct sctphdr *sh;
460
461         if (sa->sa_family != AF_INET ||
462             ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
463                 goto out;
464         }
465
466         if (PRC_IS_REDIRECT(cmd)) {
467                 ip = 0;
468         } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
469                 goto out;
470         }
471         if (ip) {
472                 struct sctp_inpcb *inp;
473                 struct sctp_tcb *stcb;
474                 struct sctp_nets *net;
475                 struct sockaddr_in to, from;
476
477                 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
478                 bzero(&to, sizeof(to));
479                 bzero(&from, sizeof(from));
480                 from.sin_family = to.sin_family = AF_INET;
481                 from.sin_len = to.sin_len = sizeof(to);
482                 from.sin_port = sh->src_port;
483                 from.sin_addr = ip->ip_src;
484                 to.sin_port = sh->dest_port;
485                 to.sin_addr = ip->ip_dst;
486
487                 /*
488                  * 'to' holds the dest of the packet that failed to be sent.
489                  * 'from' holds our local endpoint address.
490                  * Thus we reverse the to and the from in the lookup.
491                  */
492                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
493                                                     (struct sockaddr *)&to,
494                                                     &inp, &net, 1);
495                 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
496                         if (cmd != PRC_MSGSIZE) {
497                                 int cm;
498                                 if (cmd == PRC_HOSTDEAD) {
499                                         cm = EHOSTUNREACH;
500                                 } else {
501                                         cm = inetctlerrmap[cmd];
502                                 }
503                                 sctp_notify(inp, cm, sh,
504                                             (struct sockaddr *)&to, stcb,
505                                             net);
506                         } else {
507                                 /* handle possible ICMP size messages */
508                                 sctp_notify_mbuf(inp, stcb, net, ip, sh);
509                         }
510                 } else {
511 #if (defined(__FreeBSD__) && __FreeBSD_version < 500000) || defined(__DragonFly__)
512                         /* XXX must be fixed for 5.x and higher, leave for 4.x */
513                         if (PRC_IS_REDIRECT(cmd) && inp) {
514                                 in_rtchange((struct inpcb *)inp,
515                                             inetctlerrmap[cmd]);
516                         }
517 #endif
518                         if ((stcb == NULL) && (inp != NULL)) {
519                                 /* reduce ref-count */
520                                 SCTP_INP_WLOCK(inp);
521                                 SCTP_INP_DECR_REF(inp);
522                                 SCTP_INP_WUNLOCK(inp);
523                         }
524
525                 }
526         }
527 out:
528         lwkt_replymsg(&msg->lmsg, 0);
529 }
530
531 #if defined(__FreeBSD__) || defined(__DragonFly__)
532 static int
533 sctp_getcred(SYSCTL_HANDLER_ARGS)
534 {
535         struct sockaddr_in addrs[2];
536         struct sctp_inpcb *inp;
537         struct sctp_nets *net;
538         struct sctp_tcb *stcb;
539         int error;
540
541 #if __FreeBSD_version >= 500000 || defined(__DragonFly__)
542         error = priv_check(req->td, PRIV_ROOT);
543 #else
544         error = suser(req->p);
545 #endif
546         if (error)
547                 return (error);
548         error = SYSCTL_IN(req, addrs, sizeof(addrs));
549         if (error)
550                 return (error);
551
552         stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
553                                            sintosa(&addrs[1]),
554                                            &inp, &net, 1);
555         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
556                 if ((inp != NULL) && (stcb == NULL)) {
557                         /* reduce ref-count */
558                         SCTP_INP_WLOCK(inp);
559                         SCTP_INP_DECR_REF(inp);
560                         SCTP_INP_WUNLOCK(inp);
561                 }
562                 error = ENOENT;
563                 goto out;
564         }
565         error = SYSCTL_OUT(req, inp->sctp_socket->so_cred, sizeof(struct ucred));
566         SCTP_TCB_UNLOCK(stcb);
567 out:
568         return (error);
569 }
570
571 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
572             0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
573 #endif /* #if defined(__FreeBSD__) || defined(__DragonFly__) */
574
575 /*
576  * sysctl definitions
577  */
578 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
579
580 SYSCTL_DECL(_net_inet);
581
582 SYSCTL_NODE(_net_inet, OID_AUTO, sctp, CTLFLAG_RD, 0,
583         "sctp values");
584
585 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxdgram, CTLFLAG_RW,
586            &sctp_sendspace, 0, "Maximum outgoing SCTP buffer size");
587
588 SYSCTL_INT(_net_inet_sctp, OID_AUTO, recvspace, CTLFLAG_RW,
589            &sctp_recvspace, 0, "Maximum incoming SCTP buffer size");
590
591 SYSCTL_INT(_net_inet_sctp, OID_AUTO, auto_asconf, CTLFLAG_RW,
592            &sctp_auto_asconf, 0, "Enable SCTP Auto-ASCONF");
593
594 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_enable, CTLFLAG_RW,
595            &sctp_ecn, 0, "Enable SCTP ECN");
596
597 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_nonce, CTLFLAG_RW,
598            &sctp_ecn_nonce, 0, "Enable SCTP ECN Nonce");
599
600 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_sacks, CTLFLAG_RW,
601            &sctp_strict_sacks, 0, "Enable SCTP Strict SACK checking");
602
603 SYSCTL_INT(_net_inet_sctp, OID_AUTO, loopback_nocsum, CTLFLAG_RW,
604            &sctp_no_csum_on_loopback, 0,
605            "Enable NO Csum on packets sent on loopback");
606
607 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_init, CTLFLAG_RW,
608            &sctp_strict_init, 0,
609            "Enable strict INIT/INIT-ACK singleton enforcement");
610
611 SYSCTL_INT(_net_inet_sctp, OID_AUTO, peer_chkoh, CTLFLAG_RW,
612            &sctp_peer_chunk_oh, 0,
613            "Amount to debit peers rwnd per chunk sent");
614
615 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxburst, CTLFLAG_RW,
616            &sctp_max_burst_default, 0,
617            "Default max burst for sctp endpoints");
618
619 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxchunks, CTLFLAG_RW,
620            &sctp_max_chunks_on_queue, 0,
621            "Default max chunks on queue per asoc");
622
623 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, delayed_sack_time, CTLFLAG_RW,
624             &sctp_delayed_sack_time_default, 0,
625             "Default delayed SACK timer in msec");
626
627 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, heartbeat_interval, CTLFLAG_RW,
628             &sctp_heartbeat_interval_default, 0,
629             "Default heartbeat interval in msec");
630
631 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, pmtu_raise_time, CTLFLAG_RW,
632             &sctp_pmtu_raise_time_default, 0,
633             "Default PMTU raise timer in sec");
634
635 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, shutdown_guard_time, CTLFLAG_RW,
636             &sctp_shutdown_guard_time_default, 0,
637             "Default shutdown guard timer in sec");
638
639 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, secret_lifetime, CTLFLAG_RW,
640             &sctp_secret_lifetime_default, 0,
641             "Default secret lifetime in sec");
642
643 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_max, CTLFLAG_RW,
644             &sctp_rto_max_default, 0,
645             "Default maximum retransmission timeout in msec");
646
647 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_min, CTLFLAG_RW,
648             &sctp_rto_min_default, 0,
649             "Default minimum retransmission timeout in msec");
650
651 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_initial, CTLFLAG_RW,
652             &sctp_rto_initial_default, 0,
653             "Default initial retransmission timeout in msec");
654
655 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rto_max, CTLFLAG_RW,
656             &sctp_init_rto_max_default, 0,
657             "Default maximum retransmission timeout during association setup in msec");
658
659 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, valid_cookie_life, CTLFLAG_RW,
660             &sctp_valid_cookie_life_default, 0,
661             "Default cookie lifetime in sec");
662
663 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rtx_max, CTLFLAG_RW,
664             &sctp_init_rtx_max_default, 0,
665             "Default maximum number of retransmission for INIT chunks");
666
667 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, assoc_rtx_max, CTLFLAG_RW,
668             &sctp_assoc_rtx_max_default, 0,
669             "Default maximum number of retransmissions per association");
670
671 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, path_rtx_max, CTLFLAG_RW,
672             &sctp_path_rtx_max_default, 0,
673             "Default maximum of retransmissions per path");
674
675 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, nr_outgoing_streams, CTLFLAG_RW,
676             &sctp_nr_outgoing_streams_default, 0,
677             "Default number of outgoing streams");
678
679 #ifdef SCTP_DEBUG
680 SYSCTL_INT(_net_inet_sctp, OID_AUTO, debug, CTLFLAG_RW,
681            &sctp_debug_on, 0, "Configure debug output");
682 #endif /* SCTP_DEBUG */
683 #endif
684
685 /*
686  * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
687  *       will sofree() it when we return.
688  */
689 static void
690 sctp_abort(netmsg_t msg)
691 {
692         struct socket *so = msg->abort.base.nm_so;
693         struct sctp_inpcb *inp;
694         int error;
695
696         inp = (struct sctp_inpcb *)so->so_pcb;
697         if (inp) {
698                 sctp_inpcb_free(inp, 1);
699                 error = 0;
700         } else {
701                 error = EINVAL;
702         }
703         lwkt_replymsg(&msg->lmsg, error);
704 }
705
706 static void
707 sctp_attach(netmsg_t msg)
708 {
709         struct socket *so = msg->attach.base.nm_so;
710         struct sctp_inpcb *inp;
711         struct inpcb *ip_inp;
712         int error;
713
714         inp = (struct sctp_inpcb *)so->so_pcb;
715         if (inp) {
716                 error = EINVAL;
717                 goto out;
718         }
719         error = soreserve(so, sctp_sendspace, sctp_recvspace, NULL);
720         if (error)
721                 goto out;
722         error = sctp_inpcb_alloc(so);
723         if (error)
724                 goto out;
725         inp = (struct sctp_inpcb *)so->so_pcb;
726         SCTP_INP_WLOCK(inp);
727
728         inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;    /* I'm not v6! */
729         ip_inp = &inp->ip_inp.inp;
730         ip_inp->inp_vflag |= INP_IPV4;
731         ip_inp->inp_ip_ttl = ip_defttl;
732
733 #ifdef IPSEC
734 #if !(defined(__OpenBSD__) || defined(__APPLE__))
735         error = ipsec_init_policy(so, &ip_inp->inp_sp);
736         if (error != 0) {
737                 sctp_inpcb_free(inp, 1);
738                 goto out;
739         }
740 #endif
741 #endif /*IPSEC*/
742         SCTP_INP_WUNLOCK(inp);
743 #if defined(__NetBSD__)
744         so->so_send = sctp_sosend;
745 #endif
746         error = 0;
747 out:
748         lwkt_replymsg(&msg->lmsg, error);
749 }
750
751 static void
752 sctp_bind(netmsg_t msg)
753 {
754         struct socket *so = msg->bind.base.nm_so;
755         struct sockaddr *addr = msg->bind.nm_nam;
756         thread_t td = msg->bind.nm_td;
757         struct sctp_inpcb *inp;
758         int error;
759
760 #ifdef INET6
761         if (addr && addr->sa_family != AF_INET) {
762                 /* must be a v4 address! */
763                 error= EINVAL;
764                 goto out;
765         }
766 #endif /* INET6 */
767
768         inp = (struct sctp_inpcb *)so->so_pcb;
769         if (inp) {
770                 error = sctp_inpcb_bind(so, addr, td);
771         } else {
772                 error = EINVAL;
773         }
774 out:
775         lwkt_replymsg(&msg->lmsg, error);
776 }
777
778
779 static void
780 sctp_detach(netmsg_t msg)
781 {
782         struct socket *so = msg->detach.base.nm_so;
783         struct sctp_inpcb *inp;
784         int error;
785
786         inp = (struct sctp_inpcb *)so->so_pcb;
787         if (inp == NULL) {
788                 error = EINVAL;
789                 goto out;
790         }
791         if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
792             (so->so_rcv.ssb_cc > 0)) {
793                 sctp_inpcb_free(inp, 1);
794         } else {
795                 sctp_inpcb_free(inp, 0);
796         }
797         error = 0;
798 out:
799         lwkt_replymsg(&msg->lmsg, error);
800 }
801
802 void
803 sctp_send(netmsg_t msg)
804 {
805         struct socket *so = msg->send.base.nm_so;
806         int flags = msg->send.nm_flags;
807         struct mbuf *m = msg->send.nm_m;
808         struct mbuf *control = msg->send.nm_control;
809         struct sockaddr *addr = msg->send.nm_addr;
810         struct thread *td = msg->send.nm_td;
811         int error;
812         struct sctp_inpcb *inp;
813         inp = (struct sctp_inpcb *)so->so_pcb;
814         if (inp == 0) {
815                 if (control) {
816                         sctp_m_freem(control);
817                         control = NULL;
818                 }
819                 sctp_m_freem(m);
820                 error = EINVAL;
821                 goto out;
822         }
823         /* Got to have an to address if we are NOT a connected socket */
824         if ((addr == NULL) &&
825             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
826              (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
827                 ) {
828                 goto connected_type;
829         } else if (addr == NULL) {
830                 error = EDESTADDRREQ;
831                 sctp_m_freem(m);
832                 if (control) {
833                         sctp_m_freem(control);
834                         control = NULL;
835                 }
836                 goto out;
837         }
838 #ifdef INET6
839         if (addr->sa_family != AF_INET) {
840                 /* must be a v4 address! */
841                 sctp_m_freem(m);
842                 if (control) {
843                         sctp_m_freem(control);
844                         control = NULL;
845                 }
846                 error = EDESTADDRREQ;   /* XXX huh? */
847                 error = EINVAL;
848                 goto out;
849         }
850 #endif /* INET6 */
851  connected_type:
852         /* now what about control */
853         if (control) {
854                 if (inp->control) {
855                         kprintf("huh? control set?\n");
856                         sctp_m_freem(inp->control);
857                         inp->control = NULL;
858                 }
859                 inp->control = control;
860         }
861         /* add it in possibly */
862         if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) {
863                 struct mbuf *x;
864                 int c_len;
865
866                 c_len = 0;
867                 /* How big is it */
868                 for (x=m;x;x = x->m_next) {
869                         c_len += x->m_len;
870                 }
871                 inp->pkt->m_pkthdr.len += c_len;
872         }
873         /* Place the data */
874         if (inp->pkt) {
875                 inp->pkt_last->m_next = m;
876                 inp->pkt_last = m;
877         } else {
878                 inp->pkt_last = inp->pkt = m;
879         }
880         if (
881 #if defined (__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
882             /* FreeBSD uses a flag passed */
883             ((flags & PRUS_MORETOCOME) == 0)
884 #elif defined( __NetBSD__)
885             /* NetBSD uses the so_state field */
886             ((so->so_state & SS_MORETOCOME) == 0)
887 #else
888             1   /* Open BSD does not have any "more to come" indication */
889 #endif
890             ) {
891                 /*
892                  * note with the current version this code will only be used
893                  * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
894                  * re-defining sosend to use the sctp_sosend. One can
895                  * optionally switch back to this code (by changing back the
896                  * definitions) but this is not advisable.
897              */
898                 error = sctp_output(inp, inp->pkt, addr,
899                                     inp->control, td, flags);
900                 inp->pkt = NULL;
901                 inp->control = NULL;
902         } else {
903                 error = 0;
904         }
905 out:
906         if (msg->send.nm_flags & PRUS_NAMALLOC) {
907                 kfree(msg->send.nm_addr, M_LWKTMSG);
908                 msg->send.nm_addr = NULL;
909         }
910         lwkt_replymsg(&msg->lmsg, error);
911 }
912
913 static void
914 sctp_disconnect(netmsg_t msg)
915 {
916         struct socket *so = msg->disconnect.base.nm_so;
917         struct sctp_inpcb *inp;
918         int error;
919
920         inp = (struct sctp_inpcb *)so->so_pcb;
921         if (inp == NULL) {
922                 error = ENOTCONN;
923                 goto out;
924         }
925         SCTP_INP_RLOCK(inp);
926         if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
927                 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
928                         /* No connection */
929                         SCTP_INP_RUNLOCK(inp);
930                         error = 0;
931                         goto out;
932                 } else {
933                         int some_on_streamwheel = 0;
934                         struct sctp_association *asoc;
935                         struct sctp_tcb *stcb;
936
937                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
938                         if (stcb == NULL) {
939                                 SCTP_INP_RUNLOCK(inp);
940                                 error = EINVAL;
941                                 goto out;
942                         }
943                         asoc = &stcb->asoc;
944                         SCTP_TCB_LOCK(stcb);
945                         if (((so->so_options & SO_LINGER) &&
946                              (so->so_linger == 0)) ||
947                             (so->so_rcv.ssb_cc > 0)) {
948                                 if (SCTP_GET_STATE(asoc) !=
949                                     SCTP_STATE_COOKIE_WAIT) {
950                                         /* Left with Data unread */
951                                         struct mbuf *err;
952                                         err = NULL;
953                                         MGET(err, MB_DONTWAIT, MT_DATA);
954                                         if (err) {
955                                                 /* Fill in the user initiated abort */
956                                                 struct sctp_paramhdr *ph;
957                                                 ph = mtod(err, struct sctp_paramhdr *);
958                                                 err->m_len = sizeof(struct sctp_paramhdr);
959                                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
960                                                 ph->param_length = htons(err->m_len);
961                                         }
962                                         sctp_send_abort_tcb(stcb, err);
963                                 }
964                                 SCTP_INP_RUNLOCK(inp);
965                                 sctp_free_assoc(inp, stcb);
966                                 /* No unlock tcb assoc is gone */
967                                 error = 0;
968                                 goto out;
969                         }
970                         if (!TAILQ_EMPTY(&asoc->out_wheel)) {
971                                 /* Check to see if some data queued */
972                                 struct sctp_stream_out *outs;
973                                 TAILQ_FOREACH(outs, &asoc->out_wheel,
974                                               next_spoke) {
975                                         if (!TAILQ_EMPTY(&outs->outqueue)) {
976                                                 some_on_streamwheel = 1;
977                                                 break;
978                                         }
979                                 }
980                         }
981
982                         if (TAILQ_EMPTY(&asoc->send_queue) &&
983                             TAILQ_EMPTY(&asoc->sent_queue) &&
984                             (some_on_streamwheel == 0)) {
985                                 /* there is nothing queued to send, so done */
986                                 if ((SCTP_GET_STATE(asoc) !=
987                                      SCTP_STATE_SHUTDOWN_SENT) &&
988                                     (SCTP_GET_STATE(asoc) !=
989                                      SCTP_STATE_SHUTDOWN_ACK_SENT)) {
990                                         /* only send SHUTDOWN 1st time thru */
991 #ifdef SCTP_DEBUG
992                                         if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
993                                                 kprintf("%s:%d sends a shutdown\n",
994                                                        __FILE__,
995                                                        __LINE__
996                                                         );
997                                         }
998 #endif
999                                         sctp_send_shutdown(stcb,
1000                                                            stcb->asoc.primary_destination);
1001                                         sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1002                                         asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1003                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1004                                                          stcb->sctp_ep, stcb,
1005                                                          asoc->primary_destination);
1006                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1007                                                          stcb->sctp_ep, stcb,
1008                                                          asoc->primary_destination);
1009                                 }
1010                         } else {
1011                                 /*
1012                                  * we still got (or just got) data to send,
1013                                  * so set SHUTDOWN_PENDING
1014                                  */
1015                                 /*
1016                                  * XXX sockets draft says that MSG_EOF should
1017                                  * be sent with no data.
1018                                  * currently, we will allow user data to be
1019                                  * sent first and move to SHUTDOWN-PENDING
1020                                  */
1021                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1022                         }
1023                         SCTP_TCB_UNLOCK(stcb);
1024                         SCTP_INP_RUNLOCK(inp);
1025                         error = 0;
1026                 }
1027         } else {
1028                 /* UDP model does not support this */
1029                 SCTP_INP_RUNLOCK(inp);
1030                 error = EOPNOTSUPP;
1031         }
1032 out:
1033         lwkt_replymsg(&msg->lmsg, error);
1034 }
1035
1036 /* also called from ipv6 sctp code */
1037 void
1038 sctp_shutdown(netmsg_t msg)
1039 {
1040         struct socket *so = msg->shutdown.base.nm_so;
1041         struct sctp_inpcb *inp;
1042         int error;
1043
1044         inp = (struct sctp_inpcb *)so->so_pcb;
1045         if (inp == NULL) {
1046                 error = EINVAL;
1047                 goto out;
1048         }
1049         SCTP_INP_RLOCK(inp);
1050         /* For UDP model this is a invalid call */
1051         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
1052                 /* Restore the flags that the soshutdown took away. */
1053 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
1054                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1055 #else
1056                 soclrstate(so, SS_CANTRCVMORE);
1057 #endif
1058                 /* This proc will wakeup for read and do nothing (I hope) */
1059                 SCTP_INP_RUNLOCK(inp);
1060                 error = EOPNOTSUPP;
1061                 goto out;
1062         }
1063         /*
1064          * Ok if we reach here its the TCP model and it is either a SHUT_WR
1065          * or SHUT_RDWR. This means we put the shutdown flag against it.
1066          */
1067         {
1068                 int some_on_streamwheel = 0;
1069                 struct sctp_tcb *stcb;
1070                 struct sctp_association *asoc;
1071                 socantsendmore(so);
1072
1073                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1074                 if (stcb == NULL) {
1075                         /*
1076                          * Ok we hit the case that the shutdown call was made
1077                          * after an abort or something. Nothing to do now.
1078                          */
1079                         error = 0;
1080                         goto out;
1081                 }
1082                 SCTP_TCB_LOCK(stcb);
1083                 asoc = &stcb->asoc;
1084
1085                 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
1086                         /* Check to see if some data queued */
1087                         struct sctp_stream_out *outs;
1088                         TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
1089                                 if (!TAILQ_EMPTY(&outs->outqueue)) {
1090                                         some_on_streamwheel = 1;
1091                                         break;
1092                                 }
1093                         }
1094                 }
1095                 if (TAILQ_EMPTY(&asoc->send_queue) &&
1096                     TAILQ_EMPTY(&asoc->sent_queue) &&
1097                     (some_on_streamwheel == 0)) {
1098                         /* there is nothing queued to send, so I'm done... */
1099                         if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1100                                 /* only send SHUTDOWN the first time through */
1101 #ifdef SCTP_DEBUG
1102                                 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1103                                         kprintf("%s:%d sends a shutdown\n",
1104                                                __FILE__,
1105                                                __LINE__
1106                                                 );
1107                                 }
1108 #endif
1109                                 sctp_send_shutdown(stcb,
1110                                                    stcb->asoc.primary_destination);
1111                                 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1112                                 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1113                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1114                                                  stcb->sctp_ep, stcb,
1115                                                  asoc->primary_destination);
1116                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1117                                                  stcb->sctp_ep, stcb,
1118                                                  asoc->primary_destination);
1119                         }
1120                 } else {
1121                         /*
1122                          * we still got (or just got) data to send, so
1123                          * set SHUTDOWN_PENDING
1124                          */
1125                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1126                 }
1127                 SCTP_TCB_UNLOCK(stcb);
1128         }
1129         SCTP_INP_RUNLOCK(inp);
1130         error = 0;
1131 out:
1132         lwkt_replymsg(&msg->lmsg, error);
1133 }
1134
1135 /*
1136  * copies a "user" presentable address and removes embedded scope, etc.
1137  * returns 0 on success, 1 on error
1138  */
1139 static uint32_t
1140 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1141 {
1142         struct sockaddr_in6 lsa6;
1143         sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1144                                                    &lsa6);
1145         memcpy(ss, sa, sa->sa_len);
1146         return (0);
1147 }
1148
1149
1150 #if defined(__NetBSD__) || defined(__OpenBSD__)
1151 /*
1152  * On NetBSD and OpenBSD in6_sin_2_v4mapsin6() not used and not exported,
1153  * so we have to export it here.
1154  */
1155 void    in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6);
1156 #endif
1157
1158 static int
1159 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1160                        struct sctp_tcb *stcb,
1161                        int limit,
1162                        struct sockaddr_storage *sas)
1163 {
1164         struct ifnet *ifn;
1165         int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
1166         int ipv4_addr_legal, ipv6_addr_legal;
1167         actual = 0;
1168         if (limit <= 0)
1169                 return (actual);
1170
1171         if (stcb) {
1172                 /* Turn on all the appropriate scope */
1173                 loopback_scope = stcb->asoc.loopback_scope;
1174                 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1175                 local_scope = stcb->asoc.local_scope;
1176                 site_scope = stcb->asoc.site_scope;
1177         } else {
1178                 /* Turn on ALL scope, since we look at the EP */
1179                 loopback_scope = ipv4_local_scope = local_scope =
1180                         site_scope = 1;
1181         }
1182         ipv4_addr_legal = ipv6_addr_legal = 0;
1183         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1184                 ipv6_addr_legal = 1;
1185                 if (
1186 #if defined(__OpenBSD__)
1187                 (0) /* we always do dual bind */
1188 #elif defined (__NetBSD__)
1189                 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1190 #else
1191                 (((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
1192 #endif
1193                 == 0) {
1194                         ipv4_addr_legal = 1;
1195                 }
1196         } else {
1197                 ipv4_addr_legal = 1;
1198         }
1199
1200         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1201                 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1202                         struct ifaddr_container *ifac;
1203
1204                         if ((loopback_scope == 0) &&
1205                             (ifn->if_type == IFT_LOOP)) {
1206                                 /* Skip loopback if loopback_scope not set */
1207                                 continue;
1208                         }
1209                         TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid],
1210                                       ifa_link) {
1211                                 struct ifaddr *ifa = ifac->ifa;
1212
1213                                 if (stcb) {
1214                                 /*
1215                                  * For the BOUND-ALL case, the list
1216                                  * associated with a TCB is Always
1217                                  * considered a reverse list.. i.e.
1218                                  * it lists addresses that are NOT
1219                                  * part of the association. If this
1220                                  * is one of those we must skip it.
1221                                  */
1222                                         if (sctp_is_addr_restricted(stcb,
1223                                                                     ifa->ifa_addr)) {
1224                                                 continue;
1225                                         }
1226                                 }
1227                                 if ((ifa->ifa_addr->sa_family == AF_INET) &&
1228                                     (ipv4_addr_legal)) {
1229                                         struct sockaddr_in *sin;
1230                                         sin = (struct sockaddr_in *)ifa->ifa_addr;
1231                                         if (sin->sin_addr.s_addr == 0) {
1232                                                 /* we skip unspecifed addresses */
1233                                                 continue;
1234                                         }
1235                                         if ((ipv4_local_scope == 0) &&
1236                                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1237                                                 continue;
1238                                         }
1239                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1240                                                 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1241                                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1242                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1243                                                 actual += sizeof(sizeof(struct sockaddr_in6));
1244                                         } else {
1245                                                 memcpy(sas, sin, sizeof(*sin));
1246                                                 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1247                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1248                                                 actual += sizeof(*sin);
1249                                         }
1250                                         if (actual >= limit) {
1251                                                 return (actual);
1252                                         }
1253                                 } else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
1254                                            (ipv6_addr_legal)) {
1255                                         struct sockaddr_in6 *sin6, lsa6;
1256                                         sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1257                                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1258                                                 /* we skip unspecifed addresses */
1259                                                 continue;
1260                                         }
1261                                         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1262                                                 if (local_scope == 0)
1263                                                         continue;
1264                                                 if (sin6->sin6_scope_id == 0) {
1265                                                         lsa6 = *sin6;
1266                                                         if (in6_recoverscope(&lsa6,
1267                                                                              &lsa6.sin6_addr,
1268                                                                              NULL))
1269                                                                 /* bad link local address */
1270                                                                 continue;
1271                                                         sin6 = &lsa6;
1272                                                 }
1273                                         }
1274                                         if ((site_scope == 0) &&
1275                                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1276                                                 continue;
1277                                         }
1278                                         memcpy(sas, sin6, sizeof(*sin6));
1279                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1280                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1281                                         actual += sizeof(*sin6);
1282                                         if (actual >= limit) {
1283                                                 return (actual);
1284                                         }
1285                                 }
1286                         }
1287                 }
1288         } else {
1289                 struct sctp_laddr *laddr;
1290                 /*
1291                  * If we have a TCB and we do NOT support ASCONF (it's
1292                  * turned off or otherwise) then the list is always the
1293                  * true list of addresses (the else case below).  Otherwise
1294                  * the list on the association is a list of addresses that
1295                  * are NOT part of the association.
1296                  */
1297                 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1298                         /* The list is a NEGATIVE list */
1299                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1300                                 if (stcb) {
1301                                         if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
1302                                                 continue;
1303                                         }
1304                                 }
1305                                 if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
1306                                         continue;
1307
1308                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1309                                 sas = (struct sockaddr_storage *)((caddr_t)sas +
1310                                                                   laddr->ifa->ifa_addr->sa_len);
1311                                 actual += laddr->ifa->ifa_addr->sa_len;
1312                                 if (actual >= limit) {
1313                                         return (actual);
1314                                 }
1315                         }
1316                 } else {
1317                         /* The list is a positive list if present */
1318                         if (stcb) {
1319                                 /* Must use the specific association list */
1320                                 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1321                                              sctp_nxt_addr) {
1322                                         if (sctp_fill_user_address(sas,
1323                                                                    laddr->ifa->ifa_addr))
1324                                                 continue;
1325                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1326                                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1327                                                                           laddr->ifa->ifa_addr->sa_len);
1328                                         actual += laddr->ifa->ifa_addr->sa_len;
1329                                         if (actual >= limit) {
1330                                                 return (actual);
1331                                         }
1332                                 }
1333                         } else {
1334                                 /* No endpoint so use the endpoints individual list */
1335                                 LIST_FOREACH(laddr, &inp->sctp_addr_list,
1336                                              sctp_nxt_addr) {
1337                                         if (sctp_fill_user_address(sas,
1338                                                                    laddr->ifa->ifa_addr))
1339                                                 continue;
1340                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1341                                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1342                                                                           laddr->ifa->ifa_addr->sa_len);
1343                                         actual += laddr->ifa->ifa_addr->sa_len;
1344                                         if (actual >= limit) {
1345                                                 return (actual);
1346                                         }
1347                                 }
1348                         }
1349                 }
1350         }
1351         return (actual);
1352 }
1353
1354 static int
1355 sctp_count_max_addresses(struct sctp_inpcb *inp)
1356 {
1357         int cnt = 0;
1358         /*
1359          * In both sub-set bound an bound_all cases we return the MAXIMUM
1360          * number of addresses that you COULD get. In reality the sub-set
1361          * bound may have an exclusion list for a given TCB OR in the
1362          * bound-all case a TCB may NOT include the loopback or other
1363          * addresses as well.
1364          */
1365         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1366                 struct ifnet *ifn;
1367
1368                 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1369                         struct ifaddr_container *ifac;
1370
1371                         TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1372                                 struct ifaddr *ifa = ifac->ifa;
1373
1374                                 /* Count them if they are the right type */
1375                                 if (ifa->ifa_addr->sa_family == AF_INET) {
1376                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1377                                                 cnt += sizeof(struct sockaddr_in6);
1378                                         else
1379                                                 cnt += sizeof(struct sockaddr_in);
1380
1381                                 } else if (ifa->ifa_addr->sa_family == AF_INET6)
1382                                         cnt += sizeof(struct sockaddr_in6);
1383                         }
1384                 }
1385         } else {
1386                 struct sctp_laddr *laddr;
1387                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1388                         if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
1389                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1390                                         cnt += sizeof(struct sockaddr_in6);
1391                                 else
1392                                         cnt += sizeof(struct sockaddr_in);
1393
1394                         } else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
1395                                 cnt += sizeof(struct sockaddr_in6);
1396                 }
1397         }
1398         return (cnt);
1399 }
1400
1401 static int
1402 sctp_do_connect_x(struct socket *so,
1403                   struct sctp_inpcb *inp,
1404                   struct mbuf *m,
1405 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1406                   struct thread *p,
1407 #else
1408                   struct proc *p,
1409 #endif
1410                   int delay
1411         )
1412 {
1413         int error = 0;
1414         struct sctp_tcb *stcb = NULL;
1415         struct sockaddr *sa;
1416         int num_v6=0, num_v4=0, *totaddrp, totaddr, i, incr, at;
1417 #ifdef SCTP_DEBUG
1418         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1419                 kprintf("Connectx called\n");
1420         }
1421 #endif /* SCTP_DEBUG */
1422
1423         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1424             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1425                 /* We are already connected AND the TCP model */
1426                 return (EADDRINUSE);
1427         }
1428         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1429                 SCTP_INP_RLOCK(inp);
1430                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1431                 SCTP_INP_RUNLOCK(inp);
1432         }
1433         if (stcb) {
1434                 return (EALREADY);
1435
1436         }
1437         SCTP_ASOC_CREATE_LOCK(inp);
1438         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1439             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1440                 SCTP_ASOC_CREATE_UNLOCK(inp);
1441                 return (EFAULT);
1442         }
1443
1444         totaddrp = mtod(m, int *);
1445         totaddr = *totaddrp;
1446         sa = (struct sockaddr *)(totaddrp + 1);
1447         at = incr = 0;
1448         /* account and validate addresses */
1449         SCTP_INP_WLOCK(inp);
1450         SCTP_INP_INCR_REF(inp);
1451         SCTP_INP_WUNLOCK(inp);
1452         for (i = 0; i < totaddr; i++) {
1453                 if (sa->sa_family == AF_INET) {
1454                         num_v4++;
1455                         incr = sizeof(struct sockaddr_in);
1456                 } else if (sa->sa_family == AF_INET6) {
1457                         struct sockaddr_in6 *sin6;
1458                         sin6 = (struct sockaddr_in6 *)sa;
1459                         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1460                                 /* Must be non-mapped for connectx */
1461                                 SCTP_ASOC_CREATE_UNLOCK(inp);
1462                                 return EINVAL;
1463                         }
1464                         num_v6++;
1465                         incr = sizeof(struct sockaddr_in6);
1466                 } else {
1467                         totaddr = i;
1468                         break;
1469                 }
1470                 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1471                 if (stcb != NULL) {
1472                         /* Already have or am bring up an association */
1473                         SCTP_ASOC_CREATE_UNLOCK(inp);
1474                         SCTP_TCB_UNLOCK(stcb);
1475                         return (EALREADY);
1476                 }
1477                 if ((at + incr) > m->m_len) {
1478                         totaddr = i;
1479                         break;
1480                 }
1481                 sa = (struct sockaddr *)((caddr_t)sa + incr);
1482         }
1483         sa = (struct sockaddr *)(totaddrp + 1);
1484         SCTP_INP_WLOCK(inp);
1485         SCTP_INP_DECR_REF(inp);
1486         SCTP_INP_WUNLOCK(inp);
1487 #ifdef INET6
1488         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1489             (num_v6 > 0)) {
1490                 SCTP_INP_WUNLOCK(inp);
1491                 SCTP_ASOC_CREATE_UNLOCK(inp);
1492                 return (EINVAL);
1493         }
1494         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1495             (num_v4 > 0)) {
1496                 struct in6pcb *inp6;
1497                 inp6 = (struct in6pcb *)inp;
1498                 if (
1499 #if defined(__OpenBSD__)
1500                         (0) /* we always do dual bind */
1501 #elif defined (__NetBSD__)
1502                         (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1503 #else
1504                         (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1505 #endif
1506                         ) {
1507                         /*
1508                          * if IPV6_V6ONLY flag, ignore connections
1509                          * destined to a v4 addr or v4-mapped addr
1510                          */
1511                         SCTP_INP_WUNLOCK(inp);
1512                         SCTP_ASOC_CREATE_UNLOCK(inp);
1513                         return EINVAL;
1514                 }
1515         }
1516 #endif /* INET6 */
1517         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1518             SCTP_PCB_FLAGS_UNBOUND) {
1519                 /* Bind a ephemeral port */
1520                 SCTP_INP_WUNLOCK(inp);
1521                 error = sctp_inpcb_bind(so, NULL, p);
1522                 if (error) {
1523                         SCTP_ASOC_CREATE_UNLOCK(inp);
1524                         return (error);
1525                 }
1526         } else {
1527                 SCTP_INP_WUNLOCK(inp);
1528         }
1529         /* We are GOOD to go */
1530         stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
1531         if (stcb == NULL) {
1532                 /* Gak! no memory */
1533                 SCTP_ASOC_CREATE_UNLOCK(inp);
1534                 return (error);
1535         }
1536         /* move to second address */
1537         if (sa->sa_family == AF_INET)
1538                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1539         else
1540                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1541
1542         for (i = 1; i < totaddr; i++) {
1543                 if (sa->sa_family == AF_INET) {
1544                         incr = sizeof(struct sockaddr_in);
1545                         if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1546                                 /* assoc gone no un-lock */
1547                                 sctp_free_assoc(inp, stcb);
1548                                 SCTP_ASOC_CREATE_UNLOCK(inp);
1549                                 return (ENOBUFS);
1550                         }
1551
1552                 } else if (sa->sa_family == AF_INET6) {
1553                         incr = sizeof(struct sockaddr_in6);
1554                         if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1555                                 /* assoc gone no un-lock */
1556                                 sctp_free_assoc(inp, stcb);
1557                                 SCTP_ASOC_CREATE_UNLOCK(inp);
1558                                 return (ENOBUFS);
1559                         }
1560                 }
1561                 sa = (struct sockaddr *)((caddr_t)sa + incr);
1562         }
1563         stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1564         if (delay) {
1565                 /* doing delayed connection */
1566                 stcb->asoc.delayed_connection = 1;
1567                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1568         } else {
1569                 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1570                 sctp_send_initiate(inp, stcb);
1571         }
1572         SCTP_TCB_UNLOCK(stcb);
1573         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1574                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1575                 /* Set the connected flag so we can queue data */
1576                 soisconnecting(so);
1577         }
1578         SCTP_ASOC_CREATE_UNLOCK(inp);
1579         return error;
1580 }
1581
1582
1583 static int
1584 sctp_optsget(struct socket *so,
1585              int opt,
1586              struct mbuf **mp,
1587 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1588              struct thread *p
1589 #else
1590              struct proc *p
1591 #endif
1592         )
1593 {
1594         struct sctp_inpcb *inp;
1595         struct mbuf *m;
1596         int error, optval=0;
1597         struct sctp_tcb *stcb = NULL;
1598
1599         inp = (struct sctp_inpcb *)so->so_pcb;
1600         if (inp == 0)
1601                 return EINVAL;
1602         error = 0;
1603
1604         if (mp == NULL) {
1605 #ifdef SCTP_DEBUG
1606                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1607                         kprintf("optsget:MP is NULL EINVAL\n");
1608                 }
1609 #endif /* SCTP_DEBUG */
1610                 return (EINVAL);
1611         }
1612         m = *mp;
1613         if (m == NULL) {
1614                 /* Got to have a mbuf */
1615 #ifdef SCTP_DEBUG
1616                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1617                         kprintf("Huh no mbuf\n");
1618                 }
1619 #endif /* SCTP_DEBUG */
1620                 return (EINVAL);
1621         }
1622 #ifdef SCTP_DEBUG
1623         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1624                 kprintf("optsget opt:%lxx sz:%u\n", (unsigned long)opt,
1625                        m->m_len);
1626         }
1627 #endif /* SCTP_DEBUG */
1628
1629         switch (opt) {
1630         case SCTP_NODELAY:
1631         case SCTP_AUTOCLOSE:
1632         case SCTP_AUTO_ASCONF:
1633         case SCTP_DISABLE_FRAGMENTS:
1634         case SCTP_I_WANT_MAPPED_V4_ADDR:
1635 #ifdef SCTP_DEBUG
1636                 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1637                         kprintf("other stuff\n");
1638                 }
1639 #endif /* SCTP_DEBUG */
1640                 SCTP_INP_RLOCK(inp);
1641                 switch (opt) {
1642                 case SCTP_DISABLE_FRAGMENTS:
1643                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
1644                         break;
1645                 case SCTP_I_WANT_MAPPED_V4_ADDR:
1646                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
1647                         break;
1648                 case SCTP_AUTO_ASCONF:
1649                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
1650                         break;
1651                 case SCTP_NODELAY:
1652                         optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
1653                         break;
1654                 case SCTP_AUTOCLOSE:
1655                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
1656                             SCTP_PCB_FLAGS_AUTOCLOSE)
1657                                 optval = inp->sctp_ep.auto_close_time;
1658                         else
1659                                 optval = 0;
1660                         break;
1661
1662                 default:
1663                         error = ENOPROTOOPT;
1664                 } /* end switch (sopt->sopt_name) */
1665                 if (opt != SCTP_AUTOCLOSE) {
1666                         /* make it an "on/off" value */
1667                         optval = (optval != 0);
1668                 }
1669                 if ((size_t)m->m_len < sizeof(int)) {
1670                         error = EINVAL;
1671                 }
1672                 SCTP_INP_RUNLOCK(inp);
1673                 if (error == 0) {
1674                         /* return the option value */
1675                         *mtod(m, int *) = optval;
1676                         m->m_len = sizeof(optval);
1677                 }
1678                 break;
1679         case SCTP_GET_ASOC_ID_LIST:
1680         {
1681                 struct sctp_assoc_ids *ids;
1682                 int cnt, at;
1683                 u_int16_t orig;
1684
1685                 if ((size_t)m->m_len < sizeof(struct sctp_assoc_ids)) {
1686                         error = EINVAL;
1687                         break;
1688                 }
1689                 ids = mtod(m, struct sctp_assoc_ids *);
1690                 cnt = 0;
1691                 SCTP_INP_RLOCK(inp);
1692                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1693                 if (stcb == NULL) {
1694                 none_out_now:
1695                         ids->asls_numb_present = 0;
1696                         ids->asls_more_to_get = 0;
1697                         SCTP_INP_RUNLOCK(inp);
1698                         break;
1699                 }
1700                 orig = ids->asls_assoc_start;
1701                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1702                 while( orig ) {
1703                         stcb = LIST_NEXT(stcb , sctp_tcblist);
1704                         orig--;
1705                         cnt--;
1706                 }
1707                 if ( stcb == NULL)
1708                         goto none_out_now;
1709
1710                 at = 0;
1711                 ids->asls_numb_present = 0;
1712                 ids->asls_more_to_get = 1;
1713                 while(at < MAX_ASOC_IDS_RET) {
1714                         ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1715                         at++;
1716                         ids->asls_numb_present++;
1717                         stcb = LIST_NEXT(stcb , sctp_tcblist);
1718                         if (stcb == NULL) {
1719                                 ids->asls_more_to_get = 0;
1720                                 break;
1721                         }
1722                 }
1723                 SCTP_INP_RUNLOCK(inp);
1724         }
1725         break;
1726         case SCTP_GET_NONCE_VALUES:
1727         {
1728                 struct sctp_get_nonce_values *gnv;
1729                 if ((size_t)m->m_len < sizeof(struct sctp_get_nonce_values)) {
1730                         error = EINVAL;
1731                         break;
1732                 }
1733                 gnv = mtod(m, struct sctp_get_nonce_values *);
1734                 stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
1735                 if (stcb == NULL) {
1736                         error = ENOTCONN;
1737                 } else {
1738                         gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1739                         gnv->gn_local_tag = stcb->asoc.my_vtag;
1740                         SCTP_TCB_UNLOCK(stcb);
1741                 }
1742
1743         }
1744         break;
1745         case SCTP_PEER_PUBLIC_KEY:
1746         case SCTP_MY_PUBLIC_KEY:
1747         case SCTP_SET_AUTH_CHUNKS:
1748         case SCTP_SET_AUTH_SECRET:
1749                 /* not supported yet and until we refine the draft */
1750                 error = EOPNOTSUPP;
1751                 break;
1752
1753         case SCTP_DELAYED_ACK_TIME:
1754         {
1755                 int32_t *tm;
1756                 if ((size_t)m->m_len < sizeof(int32_t)) {
1757                         error = EINVAL;
1758                         break;
1759                 }
1760                 tm = mtod(m, int32_t *);
1761
1762                 *tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1763         }
1764         break;
1765
1766         case SCTP_GET_SNDBUF_USE:
1767                 if ((size_t)m->m_len < sizeof(struct sctp_sockstat)) {
1768                         error = EINVAL;
1769                 } else {
1770                         struct sctp_sockstat *ss;
1771                         struct sctp_tcb *stcb;
1772                         struct sctp_association *asoc;
1773                         ss = mtod(m, struct sctp_sockstat *);
1774                         stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
1775                         if (stcb == NULL) {
1776                                 error = ENOTCONN;
1777                         } else {
1778                                 asoc = &stcb->asoc;
1779                                 ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
1780                                 ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
1781                                 ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
1782                                                                     asoc->size_on_reasm_queue +
1783                                                                     asoc->size_on_all_streams);
1784                                 SCTP_TCB_UNLOCK(stcb);
1785                                 error = 0;
1786                                 m->m_len = sizeof(struct sctp_sockstat);
1787                         }
1788                 }
1789                 break;
1790         case SCTP_MAXBURST:
1791         {
1792                 u_int8_t *burst;
1793                 burst = mtod(m, u_int8_t *);
1794                 SCTP_INP_RLOCK(inp);
1795                 *burst = inp->sctp_ep.max_burst;
1796                 SCTP_INP_RUNLOCK(inp);
1797                 m->m_len = sizeof(u_int8_t);
1798         }
1799         break;
1800         case SCTP_MAXSEG:
1801         {
1802                 u_int32_t *segsize;
1803                 sctp_assoc_t *assoc_id;
1804                 int ovh;
1805
1806                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1807                         error = EINVAL;
1808                         break;
1809                 }
1810                 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
1811                         error = EINVAL;
1812                         break;
1813                 }
1814                 assoc_id = mtod(m, sctp_assoc_t *);
1815                 segsize = mtod(m, u_int32_t *);
1816                 m->m_len = sizeof(u_int32_t);
1817
1818                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1819                      (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
1820                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1821                         struct sctp_tcb *stcb;
1822                         SCTP_INP_RLOCK(inp);
1823                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1824                         if (stcb) {
1825                                 SCTP_TCB_LOCK(stcb);
1826                                 SCTP_INP_RUNLOCK(inp);
1827                                 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1828                                 SCTP_TCB_UNLOCK(stcb);
1829                         } else {
1830                                 SCTP_INP_RUNLOCK(inp);
1831                                 goto skipit;
1832                         }
1833                 } else {
1834                         stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1835                         if (stcb) {
1836                                 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1837                                 SCTP_TCB_UNLOCK(stcb);
1838                                 break;
1839                         }
1840                 skipit:
1841                         /* default is to get the max, if I
1842                          * can't calculate from an existing association.
1843                          */
1844                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1845                                 ovh = SCTP_MED_OVERHEAD;
1846                         } else {
1847                                 ovh = SCTP_MED_V4_OVERHEAD;
1848                         }
1849                         *segsize = inp->sctp_frag_point - ovh;
1850                 }
1851         }
1852         break;
1853
1854         case SCTP_SET_DEBUG_LEVEL:
1855 #ifdef SCTP_DEBUG
1856         {
1857                 u_int32_t *level;
1858                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1859                         error = EINVAL;
1860                         break;
1861                 }
1862                 level = mtod(m, u_int32_t *);
1863                 error = 0;
1864                 *level = sctp_debug_on;
1865                 m->m_len = sizeof(u_int32_t);
1866                 kprintf("Returning DEBUG LEVEL %x is set\n",
1867                        (u_int)sctp_debug_on);
1868         }
1869 #else /* SCTP_DEBUG */
1870         error = EOPNOTSUPP;
1871 #endif
1872         break;
1873         case SCTP_GET_STAT_LOG:
1874 #ifdef SCTP_STAT_LOGGING
1875                 error = sctp_fill_stat_log(m);
1876 #else /* SCTP_DEBUG */
1877                 error = EOPNOTSUPP;
1878 #endif
1879                 break;
1880         case SCTP_GET_PEGS:
1881         {
1882                 u_int32_t *pt;
1883                 if ((size_t)m->m_len < sizeof(sctp_pegs)) {
1884                         error = EINVAL;
1885                         break;
1886                 }
1887                 pt = mtod(m, u_int32_t *);
1888                 memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
1889                 m->m_len = sizeof(sctp_pegs);
1890         }
1891         break;
1892         case SCTP_EVENTS:
1893         {
1894                 struct sctp_event_subscribe *events;
1895 #ifdef SCTP_DEBUG
1896                 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1897                         kprintf("get events\n");
1898                 }
1899 #endif /* SCTP_DEBUG */
1900                 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
1901 #ifdef SCTP_DEBUG
1902                         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1903                                 kprintf("M->M_LEN is %d not %d\n",
1904                                        (int)m->m_len,
1905                                        (int)sizeof(struct sctp_event_subscribe));
1906                         }
1907 #endif /* SCTP_DEBUG */
1908                         error = EINVAL;
1909                         break;
1910                 }
1911                 events = mtod(m, struct sctp_event_subscribe *);
1912                 memset(events, 0, sizeof(events));
1913                 SCTP_INP_RLOCK(inp);
1914                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
1915                         events->sctp_data_io_event = 1;
1916
1917                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
1918                         events->sctp_association_event = 1;
1919
1920                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
1921                         events->sctp_address_event = 1;
1922
1923                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
1924                         events->sctp_send_failure_event = 1;
1925
1926                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
1927                         events->sctp_peer_error_event = 1;
1928
1929                 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
1930                         events->sctp_shutdown_event = 1;
1931
1932                 if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
1933                         events->sctp_partial_delivery_event = 1;
1934
1935                 if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
1936                         events->sctp_adaption_layer_event = 1;
1937
1938                 if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
1939                         events->sctp_stream_reset_events = 1;
1940                 SCTP_INP_RUNLOCK(inp);
1941                 m->m_len = sizeof(struct sctp_event_subscribe);
1942
1943         }
1944         break;
1945
1946         case SCTP_ADAPTION_LAYER:
1947                 if ((size_t)m->m_len < sizeof(int)) {
1948                         error = EINVAL;
1949                         break;
1950                 }
1951 #ifdef SCTP_DEBUG
1952                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1953                         kprintf("getadaption ind\n");
1954                 }
1955 #endif /* SCTP_DEBUG */
1956                 SCTP_INP_RLOCK(inp);
1957                 *mtod(m, int *) = inp->sctp_ep.adaption_layer_indicator;
1958                 SCTP_INP_RUNLOCK(inp);
1959                 m->m_len = sizeof(int);
1960                 break;
1961         case SCTP_SET_INITIAL_DBG_SEQ:
1962                 if ((size_t)m->m_len < sizeof(int)) {
1963                         error = EINVAL;
1964                         break;
1965                 }
1966 #ifdef SCTP_DEBUG
1967                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1968                         kprintf("get initial dbg seq\n");
1969                 }
1970 #endif /* SCTP_DEBUG */
1971                 SCTP_INP_RLOCK(inp);
1972                 *mtod(m, int *) = inp->sctp_ep.initial_sequence_debug;
1973                 SCTP_INP_RUNLOCK(inp);
1974                 m->m_len = sizeof(int);
1975                 break;
1976         case SCTP_GET_LOCAL_ADDR_SIZE:
1977                 if ((size_t)m->m_len < sizeof(int)) {
1978                         error = EINVAL;
1979                         break;
1980                 }
1981 #ifdef SCTP_DEBUG
1982                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1983                         kprintf("get local sizes\n");
1984                 }
1985 #endif /* SCTP_DEBUG */
1986                 SCTP_INP_RLOCK(inp);
1987                 *mtod(m, int *) = sctp_count_max_addresses(inp);
1988                 SCTP_INP_RUNLOCK(inp);
1989                 m->m_len = sizeof(int);
1990                 break;
1991         case SCTP_GET_REMOTE_ADDR_SIZE:
1992         {
1993                 sctp_assoc_t *assoc_id;
1994                 u_int32_t *val, sz;
1995                 struct sctp_nets *net;
1996 #ifdef SCTP_DEBUG
1997                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1998                         kprintf("get remote size\n");
1999                 }
2000 #endif /* SCTP_DEBUG */
2001                 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
2002 #ifdef SCTP_DEBUG
2003                         kprintf("m->m_len:%d not %zd\n",
2004                                m->m_len, sizeof(sctp_assoc_t));
2005 #endif /* SCTP_DEBUG */
2006                         error = EINVAL;
2007                         break;
2008                 }
2009                 stcb = NULL;
2010                 val = mtod(m, u_int32_t *);
2011                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2012                         SCTP_INP_RLOCK(inp);
2013                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2014                         if (stcb)
2015                                 SCTP_TCB_LOCK(stcb);
2016                         SCTP_INP_RUNLOCK(inp);
2017                 }
2018                 if (stcb == NULL) {
2019                         assoc_id = mtod(m, sctp_assoc_t *);
2020                         stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
2021                 }
2022
2023                 if (stcb == NULL) {
2024                         error = EINVAL;
2025                         break;
2026                 }
2027                 *val = 0;
2028                 sz = 0;
2029                 /* Count the sizes */
2030                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2031                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2032                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2033                                 sz += sizeof(struct sockaddr_in6);
2034                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2035                                 sz += sizeof(struct sockaddr_in);
2036                         } else {
2037                                 /* huh */
2038                                 break;
2039                         }
2040                 }
2041                 SCTP_TCB_UNLOCK(stcb);
2042                 *val = sz;
2043                 m->m_len = sizeof(u_int32_t);
2044         }
2045         break;
2046         case SCTP_GET_PEER_ADDRESSES:
2047                 /*
2048                  * Get the address information, an array
2049                  * is passed in to fill up we pack it.
2050                  */
2051         {
2052                 int cpsz, left;
2053                 struct sockaddr_storage *sas;
2054                 struct sctp_nets *net;
2055                 struct sctp_getaddresses *saddr;
2056 #ifdef SCTP_DEBUG
2057                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2058                         kprintf("get peer addresses\n");
2059                 }
2060 #endif /* SCTP_DEBUG */
2061                 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2062                         error = EINVAL;
2063                         break;
2064                 }
2065                 left = m->m_len - sizeof(struct sctp_getaddresses);
2066                 saddr = mtod(m, struct sctp_getaddresses *);
2067                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2068                         SCTP_INP_RLOCK(inp);
2069                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2070                         if (stcb)
2071                                 SCTP_TCB_LOCK(stcb);
2072                         SCTP_INP_RUNLOCK(inp);
2073                 } else
2074                         stcb = sctp_findassociation_ep_asocid(inp,
2075                                                               saddr->sget_assoc_id);
2076                 if (stcb == NULL) {
2077                         error = ENOENT;
2078                         break;
2079                 }
2080                 m->m_len = sizeof(struct sctp_getaddresses);
2081                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2082
2083                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2084                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2085                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2086                                 cpsz = sizeof(struct sockaddr_in6);
2087                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2088                                 cpsz = sizeof(struct sockaddr_in);
2089                         } else {
2090                                 /* huh */
2091                                 break;
2092                         }
2093                         if (left < cpsz) {
2094                                 /* not enough room. */
2095 #ifdef SCTP_DEBUG
2096                                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2097                                         kprintf("Out of room\n");
2098                                 }
2099 #endif /* SCTP_DEBUG */
2100                                 break;
2101                         }
2102                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
2103                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2104                                 /* Must map the address */
2105                                 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2106                                                     (struct sockaddr_in6 *)sas);
2107                         } else {
2108                                 memcpy(sas, &net->ro._l_addr, cpsz);
2109                         }
2110                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2111
2112                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2113                         left -= cpsz;
2114                         m->m_len += cpsz;
2115 #ifdef SCTP_DEBUG
2116                         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2117                                 kprintf("left now:%d mlen:%d\n",
2118                                        left, m->m_len);
2119                         }
2120 #endif /* SCTP_DEBUG */
2121                 }
2122                 SCTP_TCB_UNLOCK(stcb);
2123         }
2124 #ifdef SCTP_DEBUG
2125         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2126                 kprintf("All done\n");
2127         }
2128 #endif /* SCTP_DEBUG */
2129         break;
2130         case SCTP_GET_LOCAL_ADDRESSES:
2131         {
2132                 int limit, actual;
2133                 struct sockaddr_storage *sas;
2134                 struct sctp_getaddresses *saddr;
2135 #ifdef SCTP_DEBUG
2136                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2137                         kprintf("get local addresses\n");
2138                 }
2139 #endif /* SCTP_DEBUG */
2140                 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2141                         error = EINVAL;
2142                         break;
2143                 }
2144                 saddr = mtod(m, struct sctp_getaddresses *);
2145
2146                 if (saddr->sget_assoc_id) {
2147                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2148                                 SCTP_INP_RLOCK(inp);
2149                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2150                                 if (stcb)
2151                                         SCTP_TCB_LOCK(stcb);
2152                                 SCTP_INP_RUNLOCK(inp);
2153                         } else
2154                                 stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
2155
2156                 } else {
2157                         stcb = NULL;
2158                 }
2159                 /*
2160                  * assure that the TCP model does not need a assoc id
2161                  * once connected.
2162                  */
2163                 if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
2164                      (stcb == NULL) ) {
2165                         SCTP_INP_RLOCK(inp);
2166                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2167                         if (stcb)
2168                                 SCTP_TCB_LOCK(stcb);
2169                         SCTP_INP_RUNLOCK(inp);
2170                 }
2171                 sas = (struct sockaddr_storage *)&saddr->addr[0];
2172                 limit = m->m_len - sizeof(sctp_assoc_t);
2173                 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2174                 SCTP_TCB_UNLOCK(stcb);
2175                 m->m_len = sizeof(struct sockaddr_storage) + actual;
2176         }
2177         break;
2178         case SCTP_PEER_ADDR_PARAMS:
2179         {
2180                 struct sctp_paddrparams *paddrp;
2181                 struct sctp_nets *net;
2182
2183 #ifdef SCTP_DEBUG
2184                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2185                         kprintf("Getting peer_addr_params\n");
2186                 }
2187 #endif /* SCTP_DEBUG */
2188                 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
2189 #ifdef SCTP_DEBUG
2190                         if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2191                                 kprintf("Hmm m->m_len:%d is to small\n",
2192                                        m->m_len);
2193                         }
2194 #endif /* SCTP_DEBUG */
2195                         error = EINVAL;
2196                         break;
2197                 }
2198                 paddrp = mtod(m, struct sctp_paddrparams *);
2199
2200                 net = NULL;
2201                 if (paddrp->spp_assoc_id) {
2202 #ifdef SCTP_DEBUG
2203                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2204                                 kprintf("In spp_assoc_id find type\n");
2205                         }
2206 #endif /* SCTP_DEBUG */
2207                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2208                                 SCTP_INP_RLOCK(inp);
2209                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2210                                 if (stcb) {
2211                                         SCTP_TCB_LOCK(stcb);
2212                                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2213                                 }
2214                                 SCTP_INP_RLOCK(inp);
2215                         } else {
2216                                 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
2217                         }
2218                         if (stcb == NULL) {
2219                                 error = ENOENT;
2220                                 break;
2221                         }
2222                 }
2223                 if (    (stcb == NULL) &&
2224                         ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
2225                          (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
2226                         /* Lookup via address */
2227 #ifdef SCTP_DEBUG
2228                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2229                                 kprintf("Ok we need to lookup a param\n");
2230                         }
2231 #endif /* SCTP_DEBUG */
2232                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2233                                 SCTP_INP_RLOCK(inp);
2234                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2235                                 if (stcb) {
2236                                         SCTP_TCB_LOCK(stcb);
2237                                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2238                                 }
2239                                 SCTP_INP_RUNLOCK(inp);
2240                         } else {
2241                                 SCTP_INP_WLOCK(inp);
2242                                 SCTP_INP_INCR_REF(inp);
2243                                 SCTP_INP_WUNLOCK(inp);
2244                                 stcb = sctp_findassociation_ep_addr(&inp,
2245                                                                     (struct sockaddr *)&paddrp->spp_address,
2246                                                                     &net, NULL, NULL);
2247                                 if (stcb == NULL) {
2248                                         SCTP_INP_WLOCK(inp);
2249                                         SCTP_INP_DECR_REF(inp);
2250                                         SCTP_INP_WUNLOCK(inp);
2251                                 }
2252                         }
2253
2254                         if (stcb == NULL) {
2255                                 error = ENOENT;
2256                                 break;
2257                         }
2258                 } else {
2259                         /* Effects the Endpoint */
2260 #ifdef SCTP_DEBUG
2261                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2262                                 kprintf("User wants EP level info\n");
2263                         }
2264 #endif /* SCTP_DEBUG */
2265                         stcb = NULL;
2266                 }
2267                 if (stcb) {
2268                         /* Applys to the specific association */
2269 #ifdef SCTP_DEBUG
2270                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2271                                 kprintf("In TCB side\n");
2272                         }
2273 #endif /* SCTP_DEBUG */
2274                         if (net) {
2275                                 paddrp->spp_pathmaxrxt = net->failure_threshold;
2276                         } else {
2277                                 /* No destination so return default value */
2278                                 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2279                         }
2280                         paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2281                         paddrp->spp_assoc_id = sctp_get_associd(stcb);
2282                         SCTP_TCB_UNLOCK(stcb);
2283                 } else {
2284                         /* Use endpoint defaults */
2285                         SCTP_INP_RLOCK(inp);
2286 #ifdef SCTP_DEBUG
2287                         if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2288                                 kprintf("In EP level info\n");
2289                         }
2290 #endif /* SCTP_DEBUG */
2291                         paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2292                         paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
2293                         paddrp->spp_assoc_id = (sctp_assoc_t)0;
2294                         SCTP_INP_RUNLOCK(inp);
2295                 }
2296                 m->m_len = sizeof(struct sctp_paddrparams);
2297         }
2298         break;
2299         case SCTP_GET_PEER_ADDR_INFO:
2300         {
2301                 struct sctp_paddrinfo *paddri;
2302                 struct sctp_nets *net;
2303 #ifdef SCTP_DEBUG
2304                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2305                         kprintf("GetPEER ADDR_INFO\n");
2306                 }
2307 #endif /* SCTP_DEBUG */
2308                 if ((size_t)m->m_len < sizeof(struct sctp_paddrinfo)) {
2309                         error = EINVAL;
2310                         break;
2311                 }
2312                 paddri = mtod(m, struct sctp_paddrinfo *);
2313                 net = NULL;
2314                 if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
2315                     (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
2316                         /* Lookup via address */
2317                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2318                                 SCTP_INP_RLOCK(inp);
2319                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2320                                 if (stcb) {
2321                                         SCTP_TCB_LOCK(stcb);
2322                                         net = sctp_findnet(stcb,
2323                                                             (struct sockaddr *)&paddri->spinfo_address);
2324                                 }
2325                                 SCTP_INP_RUNLOCK(inp);
2326                         } else {
2327                                 SCTP_INP_WLOCK(inp);
2328                                 SCTP_INP_INCR_REF(inp);
2329                                 SCTP_INP_WUNLOCK(inp);
2330                                 stcb = sctp_findassociation_ep_addr(&inp,
2331                                     (struct sockaddr *)&paddri->spinfo_address,
2332                                     &net, NULL, NULL);
2333                                 if (stcb == NULL) {
2334                                         SCTP_INP_WLOCK(inp);
2335                                         SCTP_INP_DECR_REF(inp);
2336                                         SCTP_INP_WUNLOCK(inp);
2337                                 }
2338                         }
2339
2340                 } else {
2341                         stcb = NULL;
2342                 }
2343                 if ((stcb == NULL) || (net == NULL)) {
2344                         error = ENOENT;
2345                         break;
2346                 }
2347                 m->m_len = sizeof(struct sctp_paddrinfo);
2348                 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
2349                 paddri->spinfo_cwnd = net->cwnd;
2350                 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2351                 paddri->spinfo_rto = net->RTO;
2352                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2353                 SCTP_TCB_UNLOCK(stcb);
2354         }
2355         break;
2356         case SCTP_PCB_STATUS:
2357         {
2358                 struct sctp_pcbinfo *spcb;
2359 #ifdef SCTP_DEBUG
2360                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2361                         kprintf("PCB status\n");
2362                 }
2363 #endif /* SCTP_DEBUG */
2364                 if ((size_t)m->m_len < sizeof(struct sctp_pcbinfo)) {
2365                         error = EINVAL;
2366                         break;
2367                 }
2368                 spcb = mtod(m, struct sctp_pcbinfo *);
2369                 sctp_fill_pcbinfo(spcb);
2370                 m->m_len = sizeof(struct sctp_pcbinfo);
2371         }
2372         break;
2373         case SCTP_STATUS:
2374         {
2375                 struct sctp_nets *net;
2376                 struct sctp_status *sstat;
2377 #ifdef SCTP_DEBUG
2378                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2379                         kprintf("SCTP status\n");
2380                 }
2381 #endif /* SCTP_DEBUG */
2382
2383                 if ((size_t)m->m_len < sizeof(struct sctp_status)) {
2384                         error = EINVAL;
2385                         break;
2386                 }
2387                 sstat = mtod(m, struct sctp_status *);
2388
2389                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2390                         SCTP_INP_RLOCK(inp);
2391                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2392                         if (stcb)
2393                                 SCTP_TCB_LOCK(stcb);
2394                         SCTP_INP_RUNLOCK(inp);
2395                 } else
2396                         stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
2397
2398                 if (stcb == NULL) {
2399                         error = EINVAL;
2400                         break;
2401                 }
2402                 /*
2403                  * I think passing the state is fine since
2404                  * sctp_constants.h will be available to the user
2405                  * land.
2406                  */
2407                 sstat->sstat_state = stcb->asoc.state;
2408                 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2409                 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2410                 /*
2411                  * We can't include chunks that have been passed
2412                  * to the socket layer. Only things in queue.
2413                  */
2414                 sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
2415                                          stcb->asoc.cnt_on_reasm_queue +
2416                                          stcb->asoc.cnt_on_all_streams);
2417
2418
2419                 sstat->sstat_instrms = stcb->asoc.streamincnt;
2420                 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2421                 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2422                 memcpy(&sstat->sstat_primary.spinfo_address,
2423                        &stcb->asoc.primary_destination->ro._l_addr,
2424                        ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2425                 net = stcb->asoc.primary_destination;
2426                 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2427                 /*
2428                  * Again the user can get info from sctp_constants.h
2429                  * for what the state of the network is.
2430                  */
2431                 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2432                 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2433                 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2434                 sstat->sstat_primary.spinfo_rto = net->RTO;
2435                 sstat->sstat_primary.spinfo_mtu = net->mtu;
2436                 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2437                 SCTP_TCB_UNLOCK(stcb);
2438                 m->m_len = sizeof(*sstat);
2439         }
2440         break;
2441         case SCTP_RTOINFO:
2442         {
2443                 struct sctp_rtoinfo *srto;
2444 #ifdef SCTP_DEBUG
2445                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2446                         kprintf("RTO Info\n");
2447                 }
2448 #endif /* SCTP_DEBUG */
2449                 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
2450                         error = EINVAL;
2451                         break;
2452                 }
2453                 srto = mtod(m, struct sctp_rtoinfo *);
2454                 if (srto->srto_assoc_id == 0) {
2455                         /* Endpoint only please */
2456                         SCTP_INP_RLOCK(inp);
2457                         srto->srto_initial = inp->sctp_ep.initial_rto;
2458                         srto->srto_max = inp->sctp_ep.sctp_maxrto;
2459                         srto->srto_min = inp->sctp_ep.sctp_minrto;
2460                         SCTP_INP_RUNLOCK(inp);
2461                         break;
2462                 }
2463                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2464                         SCTP_INP_RLOCK(inp);
2465                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2466                         if (stcb)
2467                                 SCTP_TCB_LOCK(stcb);
2468                         SCTP_INP_RUNLOCK(inp);
2469                 } else
2470                         stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2471
2472                 if (stcb == NULL) {
2473                         error = EINVAL;
2474                         break;
2475                 }
2476                 srto->srto_initial = stcb->asoc.initial_rto;
2477                 srto->srto_max = stcb->asoc.maxrto;
2478                 srto->srto_min = stcb->asoc.minrto;
2479                 SCTP_TCB_UNLOCK(stcb);
2480                 m->m_len = sizeof(*srto);
2481         }
2482         break;
2483         case SCTP_ASSOCINFO:
2484         {
2485                 struct sctp_assocparams *sasoc;
2486 #ifdef SCTP_DEBUG
2487                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2488                         kprintf("Associnfo\n");
2489                 }
2490 #endif /* SCTP_DEBUG */
2491                 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
2492                         error = EINVAL;
2493                         break;
2494                 }
2495                 sasoc = mtod(m, struct sctp_assocparams *);
2496                 stcb = NULL;
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                 }
2505                 if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
2506                         stcb = sctp_findassociation_ep_asocid(inp,
2507                                                              sasoc->sasoc_assoc_id);
2508                         if (stcb == NULL) {
2509                                 error = ENOENT;
2510                                 break;
2511                         }
2512                 } else {
2513                         stcb = NULL;
2514                 }
2515
2516                 if (stcb) {
2517                         sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2518                         sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2519                         sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2520                         sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2521                         sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2522                         SCTP_TCB_UNLOCK(stcb);
2523                 } else {
2524                         SCTP_INP_RLOCK(inp);
2525                         sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2526                         sasoc->sasoc_number_peer_destinations = 0;
2527                         sasoc->sasoc_peer_rwnd = 0;
2528                         sasoc->sasoc_local_rwnd = ssb_space(&inp->sctp_socket->so_rcv);
2529                         sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2530                         SCTP_INP_RUNLOCK(inp);
2531                 }
2532                 m->m_len = sizeof(*sasoc);
2533         }
2534         break;
2535         case SCTP_DEFAULT_SEND_PARAM:
2536         {
2537                 struct sctp_sndrcvinfo *s_info;
2538
2539                 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
2540                         error = EINVAL;
2541                         break;
2542                 }
2543                 s_info = mtod(m, struct sctp_sndrcvinfo *);
2544                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2545                         SCTP_INP_RLOCK(inp);
2546                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2547                         if (stcb)
2548                                 SCTP_TCB_LOCK(stcb);
2549                         SCTP_INP_RUNLOCK(inp);
2550                 } else
2551                         stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2552
2553                 if (stcb == NULL) {
2554                         error = ENOENT;
2555                         break;
2556                 }
2557                 /* Copy it out */
2558                 *s_info = stcb->asoc.def_send;
2559                 SCTP_TCB_UNLOCK(stcb);
2560                 m->m_len = sizeof(*s_info);
2561         }
2562         case SCTP_INITMSG:
2563         {
2564                 struct sctp_initmsg *sinit;
2565 #ifdef SCTP_DEBUG
2566                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2567                         kprintf("initmsg\n");
2568                 }
2569 #endif /* SCTP_DEBUG */
2570                 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
2571                         error = EINVAL;
2572                         break;
2573                 }
2574                 sinit = mtod(m, struct sctp_initmsg *);
2575                 SCTP_INP_RLOCK(inp);
2576                 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2577                 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2578                 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2579                 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2580                 SCTP_INP_RUNLOCK(inp);
2581                 m->m_len = sizeof(*sinit);
2582         }
2583         break;
2584         case SCTP_PRIMARY_ADDR:
2585                 /* we allow a "get" operation on this */
2586         {
2587                 struct sctp_setprim *ssp;
2588
2589 #ifdef SCTP_DEBUG
2590                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2591                         kprintf("setprimary\n");
2592                 }
2593 #endif /* SCTP_DEBUG */
2594                 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
2595                         error = EINVAL;
2596                         break;
2597                 }
2598                 ssp = mtod(m, struct sctp_setprim *);
2599                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2600                         SCTP_INP_RLOCK(inp);
2601                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2602                         if (stcb)
2603                                 SCTP_TCB_LOCK(stcb);
2604                         SCTP_INP_RUNLOCK(inp);
2605                 } else {
2606                         stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
2607                         if (stcb == NULL) {
2608                                 /* one last shot, try it by the address in */
2609                                 struct sctp_nets *net;
2610
2611                                 SCTP_INP_WLOCK(inp);
2612                                 SCTP_INP_INCR_REF(inp);
2613                                 SCTP_INP_WUNLOCK(inp);
2614                                 stcb = sctp_findassociation_ep_addr(&inp,
2615                                                             (struct sockaddr *)&ssp->ssp_addr,
2616                                                             &net, NULL, NULL);
2617                                 if (stcb == NULL) {
2618                                         SCTP_INP_WLOCK(inp);
2619                                         SCTP_INP_DECR_REF(inp);
2620                                         SCTP_INP_WUNLOCK(inp);
2621                                 }
2622                         }
2623                         if (stcb == NULL) {
2624                                 error = EINVAL;
2625                                 break;
2626                         }
2627                 }
2628                 /* simply copy out the sockaddr_storage... */
2629                 memcpy(&ssp->ssp_addr,
2630                        &stcb->asoc.primary_destination->ro._l_addr,
2631                        ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2632                 SCTP_TCB_UNLOCK(stcb);
2633                 m->m_len = sizeof(*ssp);
2634         }
2635         break;
2636         default:
2637                 error = ENOPROTOOPT;
2638                 m->m_len = 0;
2639                 break;
2640         } /* end switch (sopt->sopt_name) */
2641         return (error);
2642 }
2643
2644 static int
2645 sctp_optsset(struct socket *so,
2646              int opt,
2647              struct mbuf **mp,
2648 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
2649              struct thread *p
2650 #else
2651              struct proc *p
2652 #endif
2653         )
2654 {
2655         int error, *mopt, set_opt;
2656         struct mbuf *m;
2657         struct sctp_tcb *stcb = NULL;
2658         struct sctp_inpcb *inp;
2659
2660         if (mp == NULL) {
2661 #ifdef SCTP_DEBUG
2662                 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2663                         kprintf("optsset:MP is NULL EINVAL\n");
2664                 }
2665 #endif /* SCTP_DEBUG */
2666                 return (EINVAL);
2667         }
2668         m = *mp;
2669         if (m == NULL)
2670                 return (EINVAL);
2671
2672         inp = (struct sctp_inpcb *)so->so_pcb;
2673         if (inp == 0)
2674                 return EINVAL;
2675
2676         error = 0;
2677         switch (opt) {
2678         case SCTP_NODELAY:
2679         case SCTP_AUTOCLOSE:
2680         case SCTP_AUTO_ASCONF:
2681         case SCTP_DISABLE_FRAGMENTS:
2682         case SCTP_I_WANT_MAPPED_V4_ADDR:
2683                 /* copy in the option value */
2684                 if ((size_t)m->m_len < sizeof(int)) {
2685                         error = EINVAL;
2686                         break;
2687                 }
2688                 mopt = mtod(m, int *);
2689                 set_opt = 0;
2690                 if (error)
2691                         break;
2692                 switch (opt) {
2693                 case SCTP_DISABLE_FRAGMENTS:
2694                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2695                         break;
2696                 case SCTP_AUTO_ASCONF:
2697                         set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2698                         break;
2699
2700                 case SCTP_I_WANT_MAPPED_V4_ADDR:
2701                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2702                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2703                         } else {
2704                                 return (EINVAL);
2705                         }
2706                         break;
2707                 case SCTP_NODELAY:
2708                         set_opt = SCTP_PCB_FLAGS_NODELAY;
2709                         break;
2710                 case SCTP_AUTOCLOSE:
2711                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2712                         /*
2713                          * The value is in ticks.
2714                          * Note this does not effect old associations, only
2715                          * new ones.
2716                          */
2717                         inp->sctp_ep.auto_close_time = (*mopt * hz);
2718                         break;
2719                 }
2720                 SCTP_INP_WLOCK(inp);
2721                 if (*mopt != 0) {
2722                         inp->sctp_flags |= set_opt;
2723                 } else {
2724                         inp->sctp_flags &= ~set_opt;
2725                 }
2726                 SCTP_INP_WUNLOCK(inp);
2727                 break;
2728         case SCTP_MY_PUBLIC_KEY:    /* set my public key */
2729         case SCTP_SET_AUTH_CHUNKS:  /* set the authenticated chunks required */
2730         case SCTP_SET_AUTH_SECRET:  /* set the actual secret for the endpoint */
2731                 /* not supported yet and until we refine the draft */
2732                 error = EOPNOTSUPP;
2733                 break;
2734
2735         case SCTP_CLR_STAT_LOG:
2736 #ifdef SCTP_STAT_LOGGING
2737                 sctp_clr_stat_log();
2738 #else
2739                 error = EOPNOTSUPP;
2740 #endif
2741                 break;
2742         case SCTP_DELAYED_ACK_TIME:
2743         {
2744                 int32_t *tm;
2745                 if ((size_t)m->m_len < sizeof(int32_t)) {
2746                         error = EINVAL;
2747                         break;
2748                 }
2749                 tm = mtod(m, int32_t *);
2750
2751                 if ((*tm < 10) || (*tm > 500)) {
2752                         /* can't be smaller than 10ms */
2753                         /* MUST NOT be larger than 500ms */
2754                         error = EINVAL;
2755                         break;
2756                 }
2757                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
2758         }
2759                 break;
2760         case SCTP_RESET_STREAMS:
2761         {
2762                 struct sctp_stream_reset *strrst;
2763                 uint8_t two_way, not_peer;
2764
2765                 if ((size_t)m->m_len < sizeof(struct sctp_stream_reset)) {
2766                         error = EINVAL;
2767                         break;
2768                 }
2769                 strrst = mtod(m, struct sctp_stream_reset *);
2770
2771                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2772                         SCTP_INP_RLOCK(inp);
2773                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2774                         if (stcb)
2775                                 SCTP_TCB_LOCK(stcb);
2776                         SCTP_INP_RUNLOCK(inp);
2777                 } else
2778                         stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
2779                 if (stcb == NULL) {
2780                         error = ENOENT;
2781                         break;
2782                 }
2783                 if (stcb->asoc.peer_supports_strreset == 0) {
2784                         /* Peer does not support it,
2785                          * we return protocol not supported since
2786                          * this is true for this feature and this
2787                          * peer, not the socket request in general.
2788                          */
2789                         error = EPROTONOSUPPORT;
2790                         SCTP_TCB_UNLOCK(stcb);
2791                         break;
2792                 }
2793
2794 /* Having re-thought this code I added as I write the I-D there
2795  * is NO need for it. The peer, if we are requesting a stream-reset
2796  * will send a request to us but will itself do what we do, take
2797  * and copy off the "reset information" we send and queue TSN's
2798  * larger than the send-next in our response message. Thus they
2799  * will handle it.
2800  */
2801 /*              if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
2802                 /* Must have all sending data ack'd before we
2803                  * start this procedure. This is a bit restrictive
2804                  * and we SHOULD work on changing this so ONLY the
2805                  * streams being RESET get held up. So, a reset-all
2806                  * would require this.. but a reset specific just
2807                  * needs to be sure that the ones being reset have
2808                  * nothing on the send_queue. For now we will
2809                  * skip this more detailed method and do a course
2810                  * way.. i.e. nothing pending ... for future FIX ME!
2811                  */
2812 /*                      error = EBUSY;*/
2813 /*                      break;*/
2814 /*              }*/
2815
2816                 if (stcb->asoc.stream_reset_outstanding) {
2817                         error = EALREADY;
2818                         SCTP_TCB_UNLOCK(stcb);
2819                         break;
2820                 }
2821                 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2822                         two_way = 0;
2823                         not_peer = 0;
2824                 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2825                         two_way = 1;
2826                         not_peer = 1;
2827                 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2828                         two_way = 1;
2829                         not_peer = 0;
2830                 } else {
2831                         error = EINVAL;
2832                         SCTP_TCB_UNLOCK(stcb);
2833                         break;
2834                 }
2835                 sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2836                                         strrst->strrst_list, two_way, not_peer);
2837                 sctp_chunk_output(inp, stcb, 12);
2838                 SCTP_TCB_UNLOCK(stcb);
2839
2840         }
2841         break;
2842         case SCTP_RESET_PEGS:
2843                 memset(sctp_pegs, 0, sizeof(sctp_pegs));
2844                 error = 0;
2845                 break;
2846         case SCTP_CONNECT_X:
2847                 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2848                         error = EINVAL;
2849                         break;
2850                 }
2851                 error = sctp_do_connect_x(so, inp, m, p, 0);
2852                 break;
2853
2854         case SCTP_CONNECT_X_DELAYED:
2855                 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2856                         error = EINVAL;
2857                         break;
2858                 }
2859                 error = sctp_do_connect_x(so, inp, m, p, 1);
2860                 break;
2861
2862         case SCTP_CONNECT_X_COMPLETE:
2863         {
2864                 struct sockaddr *sa;
2865                 struct sctp_nets *net;
2866                 if ((size_t)m->m_len < sizeof(struct sockaddr_in)) {
2867                         error = EINVAL;
2868                         break;
2869                 }
2870                 sa = mtod(m, struct sockaddr *);
2871                 /* find tcb */
2872                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2873                         SCTP_INP_RLOCK(inp);
2874                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
2875                         if (stcb) {
2876                                 SCTP_TCB_LOCK(stcb);
2877                                 net = sctp_findnet(stcb, sa);
2878                         }
2879                         SCTP_INP_RUNLOCK(inp);
2880                 } else {
2881                         SCTP_INP_WLOCK(inp);
2882                         SCTP_INP_INCR_REF(inp);
2883                         SCTP_INP_WUNLOCK(inp);
2884                         stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2885                         if (stcb == NULL) {
2886                                 SCTP_INP_WLOCK(inp);
2887                                 SCTP_INP_DECR_REF(inp);
2888                                 SCTP_INP_WUNLOCK(inp);
2889                         }
2890                 }
2891
2892                 if (stcb == NULL) {
2893                         error = ENOENT;
2894                         break;
2895                 }
2896                 if (stcb->asoc.delayed_connection == 1) {
2897                         stcb->asoc.delayed_connection = 0;
2898                         SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2899                         sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2900                         sctp_send_initiate(inp, stcb);
2901                 } else {
2902                         /* already expired or did not use delayed connectx */
2903                         error = EALREADY;
2904                 }
2905                 SCTP_TCB_UNLOCK(stcb);
2906         }
2907         break;
2908         case SCTP_MAXBURST:
2909         {
2910                 u_int8_t *burst;
2911                 SCTP_INP_WLOCK(inp);
2912                 burst = mtod(m, u_int8_t *);
2913                 if (*burst) {
2914                         inp->sctp_ep.max_burst = *burst;
2915                 }
2916                 SCTP_INP_WUNLOCK(inp);
2917         }
2918         break;
2919         case SCTP_MAXSEG:
2920         {
2921                 u_int32_t *segsize;
2922                 int ovh;
2923                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2924                         ovh = SCTP_MED_OVERHEAD;
2925                 } else {
2926                         ovh = SCTP_MED_V4_OVERHEAD;
2927                 }
2928                 segsize = mtod(m, u_int32_t *);
2929                 if (*segsize < 1) {
2930                         error = EINVAL;
2931                         break;
2932                 }
2933                 SCTP_INP_WLOCK(inp);
2934                 inp->sctp_frag_point = (*segsize+ovh);
2935                 if (inp->sctp_frag_point < MHLEN) {
2936                         inp->sctp_frag_point = MHLEN;
2937                 }
2938                 SCTP_INP_WUNLOCK(inp);
2939         }
2940         break;
2941         case SCTP_SET_DEBUG_LEVEL:
2942 #ifdef SCTP_DEBUG
2943         {
2944                 u_int32_t *level;
2945                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
2946                         error = EINVAL;
2947                         break;
2948                 }
2949                 level = mtod(m, u_int32_t *);
2950                 error = 0;
2951                 sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
2952                                            SCTP_DEBUG_NOISY));
2953                 kprintf("SETTING DEBUG LEVEL to %x\n",
2954                        (u_int)sctp_debug_on);
2955
2956         }
2957 #else
2958         error = EOPNOTSUPP;
2959 #endif /* SCTP_DEBUG */
2960         break;
2961         case SCTP_EVENTS:
2962         {
2963                 struct sctp_event_subscribe *events;
2964                 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
2965                         error = EINVAL;
2966                         break;
2967                 }
2968                 SCTP_INP_WLOCK(inp);
2969                 events = mtod(m, struct sctp_event_subscribe *);
2970                 if (events->sctp_data_io_event) {
2971                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2972                 } else {
2973                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2974                 }
2975
2976                 if (events->sctp_association_event) {
2977                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
2978                 } else {
2979                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
2980                 }
2981
2982                 if (events->sctp_address_event) {
2983                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
2984                 } else {
2985                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
2986                 }
2987
2988                 if (events->sctp_send_failure_event) {
2989                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2990                 } else {
2991                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2992                 }
2993
2994                 if (events->sctp_peer_error_event) {
2995                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
2996                 } else {
2997                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
2998                 }
2999
3000                 if (events->sctp_shutdown_event) {
3001                         inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3002                 } else {
3003                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3004                 }
3005
3006                 if (events->sctp_partial_delivery_event) {
3007                         inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
3008                 } else {
3009                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
3010                 }
3011
3012                 if (events->sctp_adaption_layer_event) {
3013                         inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
3014                 } else {
3015                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
3016                 }
3017
3018                 if (events->sctp_stream_reset_events) {
3019                         inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3020                 } else {
3021                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3022                 }
3023                 SCTP_INP_WUNLOCK(inp);
3024         }
3025         break;
3026
3027         case SCTP_ADAPTION_LAYER:
3028         {
3029                 struct sctp_setadaption *adap_bits;
3030                 if ((size_t)m->m_len < sizeof(struct sctp_setadaption)) {
3031                         error = EINVAL;
3032                         break;
3033                 }
3034                 SCTP_INP_WLOCK(inp);
3035                 adap_bits = mtod(m, struct sctp_setadaption *);
3036                 inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
3037                 SCTP_INP_WUNLOCK(inp);
3038         }
3039         break;
3040         case SCTP_SET_INITIAL_DBG_SEQ:
3041         {
3042                 u_int32_t *vvv;
3043                 if ((size_t)m->m_len < sizeof(u_int32_t)) {
3044                         error = EINVAL;
3045                         break;
3046                 }
3047                 SCTP_INP_WLOCK(inp);
3048                 vvv = mtod(m, u_int32_t *);
3049                 inp->sctp_ep.initial_sequence_debug = *vvv;
3050                 SCTP_INP_WUNLOCK(inp);
3051         }
3052         break;
3053         case SCTP_DEFAULT_SEND_PARAM:
3054         {
3055                 struct sctp_sndrcvinfo *s_info;
3056
3057                 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
3058                         error = EINVAL;
3059                         break;
3060                 }
3061                 s_info = mtod(m, struct sctp_sndrcvinfo *);
3062
3063                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3064                         SCTP_INP_RLOCK(inp);
3065                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3066                         if (stcb)
3067                                 SCTP_TCB_LOCK(stcb);
3068                         SCTP_INP_RUNLOCK(inp);
3069                 } else
3070                         stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
3071
3072                 if (stcb == NULL) {
3073                         error = ENOENT;
3074                         break;
3075                 }
3076                 /* Validate things */
3077                 if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
3078                         SCTP_TCB_UNLOCK(stcb);
3079                         error = EINVAL;
3080                         break;
3081                 }
3082                 /* Mask off the flags that are allowed */
3083                 s_info->sinfo_flags = (s_info->sinfo_flags &
3084                                        (MSG_UNORDERED | MSG_ADDR_OVER |
3085                                         MSG_PR_SCTP_TTL | MSG_PR_SCTP_BUF));
3086                 /* Copy it in */
3087                 stcb->asoc.def_send = *s_info;
3088                 SCTP_TCB_UNLOCK(stcb);
3089         }
3090         break;
3091         case SCTP_PEER_ADDR_PARAMS:
3092         {
3093                 struct sctp_paddrparams *paddrp;
3094                 struct sctp_nets *net;
3095                 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
3096                         error = EINVAL;
3097                         break;
3098                 }
3099                 paddrp = mtod(m, struct sctp_paddrparams *);
3100                 net = NULL;
3101                 if (paddrp->spp_assoc_id) {
3102                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3103                                 SCTP_INP_RLOCK(inp);
3104                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3105                                 if (stcb) {
3106                                         SCTP_TCB_LOCK(stcb);
3107                                         net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3108                                 }
3109                                 SCTP_INP_RUNLOCK(inp);
3110                         } else
3111                                 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
3112                         if (stcb == NULL) {
3113                                 error = ENOENT;
3114                                 break;
3115                         }
3116
3117                 }
3118                 if ((stcb == NULL) &&
3119                     ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
3120                      (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
3121                         /* Lookup via address */
3122                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3123                                 SCTP_INP_RLOCK(inp);
3124                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3125                                 if (stcb) {
3126                                         SCTP_TCB_LOCK(stcb);
3127                                         net = sctp_findnet(stcb,
3128                                                            (struct sockaddr *)&paddrp->spp_address);
3129                                 }
3130                                 SCTP_INP_RUNLOCK(inp);
3131                         } else {
3132                                 SCTP_INP_WLOCK(inp);
3133                                 SCTP_INP_INCR_REF(inp);
3134                                 SCTP_INP_WUNLOCK(inp);
3135                                 stcb = sctp_findassociation_ep_addr(&inp,
3136                                                                     (struct sockaddr *)&paddrp->spp_address,
3137                                                                     &net, NULL, NULL);
3138                                 if (stcb == NULL) {
3139                                         SCTP_INP_WLOCK(inp);
3140                                         SCTP_INP_DECR_REF(inp);
3141                                         SCTP_INP_WUNLOCK(inp);
3142                                 }
3143                         }
3144                 } else {
3145                         /* Effects the Endpoint */
3146                         stcb = NULL;
3147                 }
3148                 if (stcb) {
3149                         /* Applies to the specific association */
3150                         if (paddrp->spp_pathmaxrxt) {
3151                                 if (net) {
3152                                         if (paddrp->spp_pathmaxrxt)
3153                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
3154                                 } else {
3155                                         if (paddrp->spp_pathmaxrxt)
3156                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3157                                 }
3158                         }
3159                         if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
3160                                 /* Just a set */
3161                                 int old;
3162                                 if (net) {
3163                                         net->dest_state &= ~SCTP_ADDR_NOHB;
3164                                 } else {
3165                                         old = stcb->asoc.heart_beat_delay;
3166                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3167                                         if (old == 0) {
3168                                                 /* Turn back on the timer */
3169                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3170                                         }
3171                                 }
3172                         } else if (paddrp->spp_hbinterval == 0xffffffff) {
3173                                 /* on demand HB */
3174                                 sctp_send_hb(stcb, 1, net);
3175                         } else {
3176                                 if (net == NULL) {
3177                                         /* off on association */
3178                                         if (stcb->asoc.heart_beat_delay) {
3179                                                 int cnt_of_unconf = 0;
3180                                                 struct sctp_nets *lnet;
3181                                                 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3182                                                         if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3183                                                                 cnt_of_unconf++;
3184                                                         }
3185                                                 }
3186                                                 /* stop the timer ONLY if we have no unconfirmed addresses
3187                                                  */
3188                                                 if (cnt_of_unconf == 0)
3189                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3190                                         }
3191                                         stcb->asoc.heart_beat_delay = 0;
3192                                 } else {
3193                                         net->dest_state |= SCTP_ADDR_NOHB;
3194                                 }
3195                         }
3196                         SCTP_TCB_UNLOCK(stcb);
3197                 } else {
3198                         /* Use endpoint defaults */
3199                         SCTP_INP_WLOCK(inp);
3200                         if (paddrp->spp_pathmaxrxt)
3201                                 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3202                         if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
3203                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
3204                         SCTP_INP_WUNLOCK(inp);
3205                 }
3206         }
3207         break;
3208         case SCTP_RTOINFO:
3209         {
3210                 struct sctp_rtoinfo *srto;
3211                 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
3212                         error = EINVAL;
3213                         break;
3214                 }
3215                 srto = mtod(m, struct sctp_rtoinfo *);
3216                 if (srto->srto_assoc_id == 0) {
3217                         SCTP_INP_WLOCK(inp);
3218                         /* If we have a null asoc, its default for the endpoint */
3219                         if (srto->srto_initial > 10)
3220                                 inp->sctp_ep.initial_rto = srto->srto_initial;
3221                         if (srto->srto_max > 10)
3222                                 inp->sctp_ep.sctp_maxrto = srto->srto_max;
3223                         if (srto->srto_min > 10)
3224                                 inp->sctp_ep.sctp_minrto = srto->srto_min;
3225                         SCTP_INP_WUNLOCK(inp);
3226                         break;
3227                 }
3228                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3229                         SCTP_INP_RLOCK(inp);
3230                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3231                         if (stcb)
3232                                 SCTP_TCB_LOCK(stcb);
3233                         SCTP_INP_RUNLOCK(inp);
3234                 } else
3235                         stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
3236                 if (stcb == NULL) {
3237                         error = EINVAL;
3238                         break;
3239                 }
3240                 /* Set in ms we hope :-) */
3241                 if (srto->srto_initial > 10)
3242                         stcb->asoc.initial_rto = srto->srto_initial;
3243                 if (srto->srto_max > 10)
3244                         stcb->asoc.maxrto = srto->srto_max;
3245                 if (srto->srto_min > 10)
3246                         stcb->asoc.minrto = srto->srto_min;
3247                 SCTP_TCB_UNLOCK(stcb);
3248         }
3249         break;
3250         case SCTP_ASSOCINFO:
3251         {
3252                 struct sctp_assocparams *sasoc;
3253
3254                 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
3255                         error = EINVAL;
3256                         break;
3257                 }
3258                 sasoc = mtod(m, struct sctp_assocparams *);
3259                 if (sasoc->sasoc_assoc_id) {
3260                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3261                                 SCTP_INP_RLOCK(inp);
3262                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3263                                 if (stcb)
3264                                         SCTP_TCB_LOCK(stcb);
3265                                 SCTP_INP_RUNLOCK(inp);
3266                         } else
3267                                 stcb = sctp_findassociation_ep_asocid(inp,
3268                                                                       sasoc->sasoc_assoc_id);
3269                         if (stcb == NULL) {
3270                                 error = ENOENT;
3271                                 break;
3272                         }
3273
3274                 } else {
3275                         stcb = NULL;
3276                 }
3277                 if (stcb) {
3278                         if (sasoc->sasoc_asocmaxrxt)
3279                                 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3280                         sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3281                         sasoc->sasoc_peer_rwnd = 0;
3282                         sasoc->sasoc_local_rwnd = 0;
3283                         if (stcb->asoc.cookie_life)
3284                                 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3285                         SCTP_TCB_UNLOCK(stcb);
3286                 } else {
3287                         SCTP_INP_WLOCK(inp);
3288                         if (sasoc->sasoc_asocmaxrxt)
3289                                 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3290                         sasoc->sasoc_number_peer_destinations = 0;
3291                         sasoc->sasoc_peer_rwnd = 0;
3292                         sasoc->sasoc_local_rwnd = 0;
3293                         if (sasoc->sasoc_cookie_life)
3294                                 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3295                         SCTP_INP_WUNLOCK(inp);
3296                 }
3297         }
3298         break;
3299         case SCTP_INITMSG:
3300         {
3301                 struct sctp_initmsg *sinit;
3302
3303                 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
3304                         error = EINVAL;
3305                         break;
3306                 }
3307                 sinit = mtod(m, struct sctp_initmsg *);
3308                 SCTP_INP_WLOCK(inp);
3309                 if (sinit->sinit_num_ostreams)
3310                         inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3311
3312                 if (sinit->sinit_max_instreams)
3313                         inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3314
3315                 if (sinit->sinit_max_attempts)
3316                         inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3317
3318                 if (sinit->sinit_max_init_timeo > 10)
3319                         /* We must be at least a 100ms (we set in ticks) */
3320                         inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3321                 SCTP_INP_WUNLOCK(inp);
3322         }
3323         break;
3324         case SCTP_PRIMARY_ADDR:
3325         {
3326                 struct sctp_setprim *spa;
3327                 struct sctp_nets *net, *lnet;
3328                 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
3329                         error = EINVAL;
3330                         break;
3331                 }
3332                 spa = mtod(m, struct sctp_setprim *);
3333
3334                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3335                         SCTP_INP_RLOCK(inp);
3336                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3337                         if (stcb) {
3338                                 SCTP_TCB_LOCK(stcb);
3339                         } else {
3340                                 error = EINVAL;
3341                                 break;
3342                         }
3343                         SCTP_INP_RUNLOCK(inp);
3344                 } else
3345                         stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
3346                 if (stcb == NULL) {
3347                         /* One last shot */
3348                         SCTP_INP_WLOCK(inp);
3349                         SCTP_INP_INCR_REF(inp);
3350                         SCTP_INP_WUNLOCK(inp);
3351                         stcb = sctp_findassociation_ep_addr(&inp,
3352                                                             (struct sockaddr *)&spa->ssp_addr,
3353                                                             &net, NULL, NULL);
3354                         if (stcb == NULL) {
3355                                 SCTP_INP_WLOCK(inp);
3356                                 SCTP_INP_DECR_REF(inp);
3357                                 SCTP_INP_WUNLOCK(inp);
3358                                 error = EINVAL;
3359                                 break;
3360                         }
3361                 } else {
3362                         /* find the net, associd or connected lookup type */
3363                         net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3364                         if (net == NULL) {
3365                                 SCTP_TCB_UNLOCK(stcb);
3366                                 error = EINVAL;
3367                                 break;
3368                         }
3369                 }
3370                 if ((net != stcb->asoc.primary_destination) &&
3371                     (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3372                         /* Ok we need to set it */
3373                         lnet = stcb->asoc.primary_destination;
3374                         lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
3375                         if (sctp_set_primary_addr(stcb,
3376                                                   NULL,
3377                                                   net) == 0) {
3378                                 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3379                                         net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3380                                 }
3381                                 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3382                         }
3383                 }
3384                 SCTP_TCB_UNLOCK(stcb);
3385         }
3386         break;
3387
3388         case SCTP_SET_PEER_PRIMARY_ADDR:
3389         {
3390                 struct sctp_setpeerprim *sspp;
3391                 if ((size_t)m->m_len < sizeof(struct sctp_setpeerprim)) {
3392                         error = EINVAL;
3393                         break;
3394                 }
3395                 sspp = mtod(m, struct sctp_setpeerprim *);
3396
3397
3398                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3399                         SCTP_INP_RLOCK(inp);
3400                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3401                         if (stcb)
3402                                 SCTP_TCB_UNLOCK(stcb);
3403                         SCTP_INP_RUNLOCK(inp);
3404                 } else
3405                         stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
3406                 if (stcb == NULL) {
3407                         error = EINVAL;
3408                         break;
3409                 }
3410                 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3411                         error = EINVAL;
3412                 }
3413                 SCTP_TCB_UNLOCK(stcb);
3414         }
3415         break;
3416         case SCTP_BINDX_ADD_ADDR:
3417         {
3418              &n