]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_usrreq.c
- Get rid of the sctp_inpcb_free() "magic numbers", now they
[FreeBSD/FreeBSD.git] / sys / netinet / sctp_usrreq.c
1 /*-
2  * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /* $KAME: sctp_usrreq.c,v 1.48 2005/03/07 23:26:08 itojun Exp $  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <netinet/sctp_os.h>
36 #include <sys/proc.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_var.h>
40 #if defined(INET6)
41 #include <netinet6/sctp6_var.h>
42 #endif
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_auth.h>
51
52
53
54 void
55 sctp_init(void)
56 {
57         /* Init the SCTP pcb in sctp_pcb.c */
58         u_long sb_max_adj;
59
60         sctp_pcb_init();
61
62         if ((nmbclusters / 8) > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
63                 sctp_max_chunks_on_queue = (nmbclusters / 8);
64         /*
65          * Allow a user to take no more than 1/2 the number of clusters or
66          * the SB_MAX whichever is smaller for the send window.
67          */
68         sb_max_adj = (u_long)((u_quad_t) (SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
69         sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
70             ((nmbclusters / 2) * SCTP_DEFAULT_MAXSEGMENT));
71         /*
72          * Now for the recv window, should we take the same amount? or
73          * should I do 1/2 the SB_MAX instead in the SB_MAX min above. For
74          * now I will just copy.
75          */
76         sctp_recvspace = sctp_sendspace;
77
78
79 }
80
81
82
83 /*
84  * cleanup of the sctppcbinfo structure.
85  * Assumes that the sctppcbinfo lock is held.
86  */
87 void
88 sctp_pcbinfo_cleanup(void)
89 {
90         /* free the hash tables */
91         if (sctppcbinfo.sctp_asochash != NULL)
92                 SCTP_HASH_FREE(sctppcbinfo.sctp_asochash, sctppcbinfo.hashasocmark);
93         if (sctppcbinfo.sctp_ephash != NULL)
94                 SCTP_HASH_FREE(sctppcbinfo.sctp_ephash, sctppcbinfo.hashmark);
95         if (sctppcbinfo.sctp_tcpephash != NULL)
96                 SCTP_HASH_FREE(sctppcbinfo.sctp_tcpephash, sctppcbinfo.hashtcpmark);
97         if (sctppcbinfo.sctp_restarthash != NULL)
98                 SCTP_HASH_FREE(sctppcbinfo.sctp_restarthash, sctppcbinfo.hashrestartmark);
99 }
100
101
102 static void
103 sctp_pathmtu_adjustment(struct sctp_inpcb *inp,
104     struct sctp_tcb *stcb,
105     struct sctp_nets *net,
106     uint16_t nxtsz)
107 {
108         struct sctp_tmit_chunk *chk;
109
110         /* Adjust that too */
111         stcb->asoc.smallest_mtu = nxtsz;
112         /* now off to subtract IP_DF flag if needed */
113 #ifdef SCTP_PRINT_FOR_B_AND_M
114         printf("sctp_pathmtu_adjust called inp:%p stcb:%p net:%p nxtsz:%d\n",
115             inp, stcb, net, nxtsz);
116 #endif
117         TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
118                 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
119                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
120                 }
121         }
122         TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
123                 if ((chk->send_size + IP_HDR_SIZE) > nxtsz) {
124                         /*
125                          * For this guy we also mark for immediate resend
126                          * since we sent to big of chunk
127                          */
128                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
129                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
130                                 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
131                         }
132                         chk->sent = SCTP_DATAGRAM_RESEND;
133                         chk->rec.data.doing_fast_retransmit = 0;
134 #ifdef SCTP_FLIGHT_LOGGING
135                         sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PMTU,
136                             chk->whoTo->flight_size,
137                             chk->book_size,
138                             (uintptr_t) chk->whoTo,
139                             chk->rec.data.TSN_seq);
140 #endif
141                         /* Clear any time so NO RTT is being done */
142                         chk->do_rtt = 0;
143                         sctp_flight_size_decrease(chk);
144                         sctp_total_flight_decrease(stcb, chk);
145                 }
146         }
147 }
148
149 static void
150 sctp_notify_mbuf(struct sctp_inpcb *inp,
151     struct sctp_tcb *stcb,
152     struct sctp_nets *net,
153     struct ip *ip,
154     struct sctphdr *sh)
155 {
156         struct icmp *icmph;
157         int totsz, tmr_stopped = 0;
158         uint16_t nxtsz;
159
160         /* protection */
161         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
162             (ip == NULL) || (sh == NULL)) {
163                 if (stcb != NULL)
164                         SCTP_TCB_UNLOCK(stcb);
165                 return;
166         }
167         /* First job is to verify the vtag matches what I would send */
168         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
169                 SCTP_TCB_UNLOCK(stcb);
170                 return;
171         }
172         icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
173             sizeof(struct ip)));
174         if (icmph->icmp_type != ICMP_UNREACH) {
175                 /* We only care about unreachable */
176                 SCTP_TCB_UNLOCK(stcb);
177                 return;
178         }
179         if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
180                 /* not a unreachable message due to frag. */
181                 SCTP_TCB_UNLOCK(stcb);
182                 return;
183         }
184         totsz = ip->ip_len;
185
186         nxtsz = ntohs(icmph->icmp_seq);
187         if (nxtsz == 0) {
188                 /*
189                  * old type router that does not tell us what the next size
190                  * mtu is. Rats we will have to guess (in a educated fashion
191                  * of course)
192                  */
193                 nxtsz = find_next_best_mtu(totsz);
194         }
195         /* Stop any PMTU timer */
196         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
197                 tmr_stopped = 1;
198                 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
199                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
200         }
201         /* Adjust destination size limit */
202         if (net->mtu > nxtsz) {
203                 net->mtu = nxtsz;
204         }
205         /* now what about the ep? */
206         if (stcb->asoc.smallest_mtu > nxtsz) {
207 #ifdef SCTP_PRINT_FOR_B_AND_M
208                 printf("notify_mbuf (ICMP) calls sctp_pathmtu_adjust mtu:%d\n",
209                     nxtsz);
210 #endif
211                 sctp_pathmtu_adjustment(inp, stcb, net, nxtsz);
212         }
213         if (tmr_stopped)
214                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
215
216         SCTP_TCB_UNLOCK(stcb);
217 }
218
219
220 void
221 sctp_notify(struct sctp_inpcb *inp,
222     int error,
223     struct sctphdr *sh,
224     struct sockaddr *to,
225     struct sctp_tcb *stcb,
226     struct sctp_nets *net)
227 {
228         /* protection */
229         if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
230             (sh == NULL) || (to == NULL)) {
231                 return;
232         }
233         /* First job is to verify the vtag matches what I would send */
234         if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
235                 return;
236         }
237         /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
238
239         if ((error == EHOSTUNREACH) ||  /* Host is not reachable */
240             (error == EHOSTDOWN) ||     /* Host is down */
241             (error == ECONNREFUSED) ||  /* Host refused the connection, (not
242                                          * an abort?) */
243             (error == ENOPROTOOPT)      /* SCTP is not present on host */
244             ) {
245                 /*
246                  * Hmm reachablity problems we must examine closely. If its
247                  * not reachable, we may have lost a network. Or if there is
248                  * NO protocol at the other end named SCTP. well we consider
249                  * it a OOTB abort.
250                  */
251                 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
252                         if (net->dest_state & SCTP_ADDR_REACHABLE) {
253                                 /* Ok that destination is NOT reachable */
254                                 printf("ICMP (thresh %d/%d) takes interface %p down\n",
255                                     net->error_count,
256                                     net->failure_threshold,
257                                     net);
258
259                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
260                                 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
261                                 net->error_count = net->failure_threshold + 1;
262                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
263                                     stcb, SCTP_FAILED_THRESHOLD,
264                                     (void *)net);
265                         }
266                         if (stcb)
267                                 SCTP_TCB_UNLOCK(stcb);
268                 } else {
269                         /*
270                          * Here the peer is either playing tricks on us,
271                          * including an address that belongs to someone who
272                          * does not support SCTP OR was a userland
273                          * implementation that shutdown and now is dead. In
274                          * either case treat it like a OOTB abort with no
275                          * TCB
276                          */
277                         sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
278                         sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
279                         /* no need to unlock here, since the TCB is gone */
280                 }
281         } else {
282                 /* Send all others to the app */
283                 if (stcb)
284                         SCTP_TCB_UNLOCK(stcb);
285
286
287                 if (inp->sctp_socket) {
288 #ifdef SCTP_LOCK_LOGGING
289                         sctp_log_lock(inp, stcb, SCTP_LOG_LOCK_SOCK);
290 #endif
291                         SOCK_LOCK(inp->sctp_socket);
292                         inp->sctp_socket->so_error = error;
293                         sctp_sowwakeup(inp, inp->sctp_socket);
294                         SOCK_UNLOCK(inp->sctp_socket);
295                 }
296         }
297 }
298
299 void
300 sctp_ctlinput(cmd, sa, vip)
301         int cmd;
302         struct sockaddr *sa;
303         void *vip;
304 {
305         struct ip *ip = vip;
306         struct sctphdr *sh;
307         uint32_t vrf_id;
308
309         /* FIX, for non-bsd is this right? */
310         vrf_id = SCTP_DEFAULT_VRFID;
311         if (sa->sa_family != AF_INET ||
312             ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
313                 return;
314         }
315         if (PRC_IS_REDIRECT(cmd)) {
316                 ip = 0;
317         } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
318                 return;
319         }
320         if (ip) {
321                 struct sctp_inpcb *inp = NULL;
322                 struct sctp_tcb *stcb = NULL;
323                 struct sctp_nets *net = NULL;
324                 struct sockaddr_in to, from;
325
326                 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
327                 bzero(&to, sizeof(to));
328                 bzero(&from, sizeof(from));
329                 from.sin_family = to.sin_family = AF_INET;
330                 from.sin_len = to.sin_len = sizeof(to);
331                 from.sin_port = sh->src_port;
332                 from.sin_addr = ip->ip_src;
333                 to.sin_port = sh->dest_port;
334                 to.sin_addr = ip->ip_dst;
335
336                 /*
337                  * 'to' holds the dest of the packet that failed to be sent.
338                  * 'from' holds our local endpoint address. Thus we reverse
339                  * the to and the from in the lookup.
340                  */
341                 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
342                     (struct sockaddr *)&to,
343                     &inp, &net, 1, vrf_id);
344                 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
345                         if (cmd != PRC_MSGSIZE) {
346                                 int cm;
347
348                                 if (cmd == PRC_HOSTDEAD) {
349                                         cm = EHOSTUNREACH;
350                                 } else {
351                                         cm = inetctlerrmap[cmd];
352                                 }
353                                 sctp_notify(inp, cm, sh,
354                                     (struct sockaddr *)&to, stcb,
355                                     net);
356                         } else {
357                                 /* handle possible ICMP size messages */
358                                 sctp_notify_mbuf(inp, stcb, net, ip, sh);
359                         }
360                 } else {
361                         if ((stcb == NULL) && (inp != NULL)) {
362                                 /* reduce ref-count */
363                                 SCTP_INP_WLOCK(inp);
364                                 SCTP_INP_DECR_REF(inp);
365                                 SCTP_INP_WUNLOCK(inp);
366                         }
367                 }
368         }
369         return;
370 }
371
372 static int
373 sctp_getcred(SYSCTL_HANDLER_ARGS)
374 {
375         struct xucred xuc;
376         struct sockaddr_in addrs[2];
377         struct sctp_inpcb *inp;
378         struct sctp_nets *net;
379         struct sctp_tcb *stcb;
380         int error;
381         uint32_t vrf_id;
382
383
384         /* FIX, for non-bsd is this right? */
385         vrf_id = SCTP_DEFAULT_VRFID;
386
387         /*
388          * XXXRW: Other instances of getcred use SUSER_ALLOWJAIL, as socket
389          * visibility is scoped using cr_canseesocket(), which it is not
390          * here.
391          */
392         error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
393             SUSER_ALLOWJAIL);
394         if (error)
395                 return (error);
396
397         error = SYSCTL_IN(req, addrs, sizeof(addrs));
398         if (error)
399                 return (error);
400
401         stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
402             sintosa(&addrs[1]),
403             &inp, &net, 1, vrf_id);
404         if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
405                 if ((inp != NULL) && (stcb == NULL)) {
406                         /* reduce ref-count */
407                         SCTP_INP_WLOCK(inp);
408                         SCTP_INP_DECR_REF(inp);
409                         goto cred_can_cont;
410                 }
411                 error = ENOENT;
412                 goto out;
413         }
414         SCTP_TCB_UNLOCK(stcb);
415         /*
416          * We use the write lock here, only since in the error leg we need
417          * it. If we used RLOCK, then we would have to
418          * wlock/decr/unlock/rlock. Which in theory could create a hole.
419          * Better to use higher wlock.
420          */
421         SCTP_INP_WLOCK(inp);
422 cred_can_cont:
423         error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
424         if (error) {
425                 SCTP_INP_WUNLOCK(inp);
426                 goto out;
427         }
428         cru2x(inp->sctp_socket->so_cred, &xuc);
429         SCTP_INP_WUNLOCK(inp);
430         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
431 out:
432         return (error);
433 }
434
435 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
436     0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
437
438
439 static void
440 sctp_abort(struct socket *so)
441 {
442         struct sctp_inpcb *inp;
443         uint32_t flags;
444
445         inp = (struct sctp_inpcb *)so->so_pcb;
446         if (inp == 0)
447                 return;
448
449 sctp_must_try_again:
450         flags = inp->sctp_flags;
451 #ifdef SCTP_LOG_CLOSING
452         sctp_log_closing(inp, NULL, 17);
453 #endif
454         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
455             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
456 #ifdef SCTP_LOG_CLOSING
457                 sctp_log_closing(inp, NULL, 16);
458 #endif
459                 sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
460                     SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
461                 SOCK_LOCK(so);
462                 SCTP_SB_CLEAR(so->so_snd);
463                 /*
464                  * same for the rcv ones, they are only here for the
465                  * accounting/select.
466                  */
467                 SCTP_SB_CLEAR(so->so_rcv);
468
469                 /* Now null out the reference, we are completely detached. */
470                 so->so_pcb = NULL;
471                 SOCK_UNLOCK(so);
472         } else {
473                 flags = inp->sctp_flags;
474                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
475                         goto sctp_must_try_again;
476                 }
477         }
478         return;
479 }
480
481 static int
482 sctp_attach(struct socket *so, int proto, struct thread *p)
483 {
484         struct sctp_inpcb *inp;
485         struct inpcb *ip_inp;
486         int error;
487
488 #ifdef IPSEC
489         uint32_t flags;
490
491 #endif
492         inp = (struct sctp_inpcb *)so->so_pcb;
493         if (inp != 0) {
494                 return EINVAL;
495         }
496         error = SCTP_SORESERVE(so, sctp_sendspace, sctp_recvspace);
497         if (error) {
498                 return error;
499         }
500         error = sctp_inpcb_alloc(so);
501         if (error) {
502                 return error;
503         }
504         inp = (struct sctp_inpcb *)so->so_pcb;
505         SCTP_INP_WLOCK(inp);
506
507         inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;    /* I'm not v6! */
508         ip_inp = &inp->ip_inp.inp;
509         ip_inp->inp_vflag |= INP_IPV4;
510         ip_inp->inp_ip_ttl = ip_defttl;
511
512 #ifdef IPSEC
513         error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
514 #ifdef SCTP_LOG_CLOSING
515         sctp_log_closing(inp, NULL, 17);
516 #endif
517         if (error != 0) {
518                 flags = inp->sctp_flags;
519                 if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
520                     (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
521 #ifdef SCTP_LOG_CLOSING
522                         sctp_log_closing(inp, NULL, 15);
523 #endif
524                         SCTP_INP_WUNLOCK(inp);
525                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
526                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
527                 } else {
528                         SCTP_INP_WUNLOCK(inp);
529                 }
530                 return error;
531         }
532 #endif                          /* IPSEC */
533         SCTP_INP_WUNLOCK(inp);
534         return 0;
535 }
536
537 static int
538 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
539 {
540         struct sctp_inpcb *inp;
541         int error;
542
543 #ifdef INET6
544         if (addr && addr->sa_family != AF_INET)
545                 /* must be a v4 address! */
546                 return EINVAL;
547 #endif                          /* INET6 */
548
549         inp = (struct sctp_inpcb *)so->so_pcb;
550         if (inp == 0)
551                 return EINVAL;
552
553         error = sctp_inpcb_bind(so, addr, p);
554         return error;
555 }
556
557 static void
558 sctp_close(struct socket *so)
559 {
560         struct sctp_inpcb *inp;
561         uint32_t flags;
562
563         inp = (struct sctp_inpcb *)so->so_pcb;
564         if (inp == 0)
565                 return;
566
567         /*
568          * Inform all the lower layer assoc that we are done.
569          */
570 sctp_must_try_again:
571         flags = inp->sctp_flags;
572 #ifdef SCTP_LOG_CLOSING
573         sctp_log_closing(inp, NULL, 17);
574 #endif
575         if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
576             (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
577                 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
578                     (so->so_rcv.sb_cc > 0)) {
579 #ifdef SCTP_LOG_CLOSING
580                         sctp_log_closing(inp, NULL, 13);
581 #endif
582                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
583                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
584                 } else {
585 #ifdef SCTP_LOG_CLOSING
586                         sctp_log_closing(inp, NULL, 14);
587 #endif
588                         sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
589                             SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
590                 }
591                 /*
592                  * The socket is now detached, no matter what the state of
593                  * the SCTP association.
594                  */
595                 SOCK_LOCK(so);
596                 SCTP_SB_CLEAR(so->so_snd);
597                 /*
598                  * same for the rcv ones, they are only here for the
599                  * accounting/select.
600                  */
601                 SCTP_SB_CLEAR(so->so_rcv);
602
603                 /* Now null out the reference, we are completely detached. */
604                 so->so_pcb = NULL;
605                 SOCK_UNLOCK(so);
606         } else {
607                 flags = inp->sctp_flags;
608                 if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
609                         goto sctp_must_try_again;
610                 }
611         }
612         return;
613 }
614
615
616 int
617 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
618     struct mbuf *control, struct thread *p);
619
620
621 int
622 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
623     struct mbuf *control, struct thread *p)
624 {
625         struct sctp_inpcb *inp;
626         int error;
627
628         inp = (struct sctp_inpcb *)so->so_pcb;
629         if (inp == 0) {
630                 if (control) {
631                         sctp_m_freem(control);
632                         control = NULL;
633                 }
634                 sctp_m_freem(m);
635                 return EINVAL;
636         }
637         /* Got to have an to address if we are NOT a connected socket */
638         if ((addr == NULL) &&
639             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
640             (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
641             ) {
642                 goto connected_type;
643         } else if (addr == NULL) {
644                 error = EDESTADDRREQ;
645                 sctp_m_freem(m);
646                 if (control) {
647                         sctp_m_freem(control);
648                         control = NULL;
649                 }
650                 return (error);
651         }
652 #ifdef INET6
653         if (addr->sa_family != AF_INET) {
654                 /* must be a v4 address! */
655                 sctp_m_freem(m);
656                 if (control) {
657                         sctp_m_freem(control);
658                         control = NULL;
659                 }
660                 error = EDESTADDRREQ;
661                 return EINVAL;
662         }
663 #endif                          /* INET6 */
664 connected_type:
665         /* now what about control */
666         if (control) {
667                 if (inp->control) {
668                         printf("huh? control set?\n");
669                         sctp_m_freem(inp->control);
670                         inp->control = NULL;
671                 }
672                 inp->control = control;
673         }
674         /* Place the data */
675         if (inp->pkt) {
676                 SCTP_BUF_NEXT(inp->pkt_last) = m;
677                 inp->pkt_last = m;
678         } else {
679                 inp->pkt_last = inp->pkt = m;
680         }
681         if (
682         /* FreeBSD uses a flag passed */
683             ((flags & PRUS_MORETOCOME) == 0)
684             ) {
685                 /*
686                  * note with the current version this code will only be used
687                  * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
688                  * re-defining sosend to use the sctp_sosend. One can
689                  * optionally switch back to this code (by changing back the
690                  * definitions) but this is not advisable. This code is used
691                  * by FreeBSD when sending a file with sendfile() though.
692                  */
693                 int ret;
694
695                 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
696                 inp->pkt = NULL;
697                 inp->control = NULL;
698                 return (ret);
699         } else {
700                 return (0);
701         }
702 }
703
704 static int
705 sctp_disconnect(struct socket *so)
706 {
707         struct sctp_inpcb *inp;
708
709         inp = (struct sctp_inpcb *)so->so_pcb;
710         if (inp == NULL) {
711                 return (ENOTCONN);
712         }
713         SCTP_INP_RLOCK(inp);
714         if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
715                 if (SCTP_LIST_EMPTY(&inp->sctp_asoc_list)) {
716                         /* No connection */
717                         SCTP_INP_RUNLOCK(inp);
718                         return (0);
719                 } else {
720                         struct sctp_association *asoc;
721                         struct sctp_tcb *stcb;
722
723                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
724                         if (stcb == NULL) {
725                                 SCTP_INP_RUNLOCK(inp);
726                                 return (EINVAL);
727                         }
728                         SCTP_TCB_LOCK(stcb);
729                         asoc = &stcb->asoc;
730                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
731                                 /* We are about to be freed, out of here */
732                                 SCTP_TCB_UNLOCK(stcb);
733                                 SCTP_INP_RUNLOCK(inp);
734                                 return (0);
735                         }
736                         if (((so->so_options & SO_LINGER) &&
737                             (so->so_linger == 0)) ||
738                             (so->so_rcv.sb_cc > 0)) {
739                                 if (SCTP_GET_STATE(asoc) !=
740                                     SCTP_STATE_COOKIE_WAIT) {
741                                         /* Left with Data unread */
742                                         struct mbuf *err;
743
744                                         err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_DONTWAIT, 1, MT_DATA);
745                                         if (err) {
746                                                 /*
747                                                  * Fill in the user
748                                                  * initiated abort
749                                                  */
750                                                 struct sctp_paramhdr *ph;
751
752                                                 ph = mtod(err, struct sctp_paramhdr *);
753                                                 SCTP_BUF_LEN(err) = sizeof(struct sctp_paramhdr);
754                                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
755                                                 ph->param_length = htons(SCTP_BUF_LEN(err));
756                                         }
757                                         sctp_send_abort_tcb(stcb, err);
758                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
759                                 }
760                                 SCTP_INP_RUNLOCK(inp);
761                                 if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
762                                     (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
763                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
764                                 }
765                                 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_3);
766                                 /* No unlock tcb assoc is gone */
767                                 return (0);
768                         }
769                         if (TAILQ_EMPTY(&asoc->send_queue) &&
770                             TAILQ_EMPTY(&asoc->sent_queue) &&
771                             (asoc->stream_queue_cnt == 0)) {
772                                 /* there is nothing queued to send, so done */
773                                 if (asoc->locked_on_sending) {
774                                         goto abort_anyway;
775                                 }
776                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
777                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
778                                         /* only send SHUTDOWN 1st time thru */
779                                         sctp_stop_timers_for_shutdown(stcb);
780                                         sctp_send_shutdown(stcb,
781                                             stcb->asoc.primary_destination);
782                                         sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
783                                         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
784                                             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
785                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
786                                         }
787                                         asoc->state = SCTP_STATE_SHUTDOWN_SENT;
788                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
789                                             stcb->sctp_ep, stcb,
790                                             asoc->primary_destination);
791                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
792                                             stcb->sctp_ep, stcb,
793                                             asoc->primary_destination);
794                                 }
795                         } else {
796                                 /*
797                                  * we still got (or just got) data to send,
798                                  * so set SHUTDOWN_PENDING
799                                  */
800                                 /*
801                                  * XXX sockets draft says that SCTP_EOF
802                                  * should be sent with no data. currently,
803                                  * we will allow user data to be sent first
804                                  * and move to SHUTDOWN-PENDING
805                                  */
806                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
807                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
808                                     asoc->primary_destination);
809                                 if (asoc->locked_on_sending) {
810                                         /* Locked to send out the data */
811                                         struct sctp_stream_queue_pending *sp;
812
813                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
814                                         if (sp == NULL) {
815                                                 printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
816                                                     asoc->locked_on_sending->stream_no);
817                                         } else {
818                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
819                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
820                                         }
821                                 }
822                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
823                                     TAILQ_EMPTY(&asoc->sent_queue) &&
824                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
825                                         struct mbuf *op_err;
826
827                         abort_anyway:
828                                         op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
829                                             0, M_DONTWAIT, 1, MT_DATA);
830                                         if (op_err) {
831                                                 /*
832                                                  * Fill in the user
833                                                  * initiated abort
834                                                  */
835                                                 struct sctp_paramhdr *ph;
836                                                 uint32_t *ippp;
837
838                                                 SCTP_BUF_LEN(op_err) =
839                                                     (sizeof(struct sctp_paramhdr) + sizeof(uint32_t));
840                                                 ph = mtod(op_err,
841                                                     struct sctp_paramhdr *);
842                                                 ph->param_type = htons(
843                                                     SCTP_CAUSE_USER_INITIATED_ABT);
844                                                 ph->param_length = htons(SCTP_BUF_LEN(op_err));
845                                                 ippp = (uint32_t *) (ph + 1);
846                                                 *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4);
847                                         }
848                                         stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_4;
849                                         sctp_send_abort_tcb(stcb, op_err);
850                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
851                                         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) ||
852                                             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
853                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
854                                         }
855                                         SCTP_INP_RUNLOCK(inp);
856                                         sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_5);
857                                         return (0);
858                                 }
859                         }
860                         SCTP_TCB_UNLOCK(stcb);
861                         SCTP_INP_RUNLOCK(inp);
862                         return (0);
863                 }
864                 /* not reached */
865                 printf("Not reached reached?\n");
866         } else {
867                 /* UDP model does not support this */
868                 SCTP_INP_RUNLOCK(inp);
869                 return EOPNOTSUPP;
870         }
871 }
872
873 int
874 sctp_shutdown(struct socket *so)
875 {
876         struct sctp_inpcb *inp;
877
878         inp = (struct sctp_inpcb *)so->so_pcb;
879         if (inp == 0) {
880                 return EINVAL;
881         }
882         SCTP_INP_RLOCK(inp);
883         /* For UDP model this is a invalid call */
884         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
885                 /* Restore the flags that the soshutdown took away. */
886                 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
887                 /* This proc will wakeup for read and do nothing (I hope) */
888                 SCTP_INP_RUNLOCK(inp);
889                 return (EOPNOTSUPP);
890         }
891         /*
892          * Ok if we reach here its the TCP model and it is either a SHUT_WR
893          * or SHUT_RDWR. This means we put the shutdown flag against it.
894          */
895         {
896                 struct sctp_tcb *stcb;
897                 struct sctp_association *asoc;
898
899                 socantsendmore(so);
900
901                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
902                 if (stcb == NULL) {
903                         /*
904                          * Ok we hit the case that the shutdown call was
905                          * made after an abort or something. Nothing to do
906                          * now.
907                          */
908                         SCTP_INP_RUNLOCK(inp);
909                         return (0);
910                 }
911                 SCTP_TCB_LOCK(stcb);
912                 asoc = &stcb->asoc;
913                 if (TAILQ_EMPTY(&asoc->send_queue) &&
914                     TAILQ_EMPTY(&asoc->sent_queue) &&
915                     (asoc->stream_queue_cnt == 0)) {
916                         if (asoc->locked_on_sending) {
917                                 goto abort_anyway;
918                         }
919                         /* there is nothing queued to send, so I'm done... */
920                         if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
921                                 /* only send SHUTDOWN the first time through */
922                                 sctp_stop_timers_for_shutdown(stcb);
923                                 sctp_send_shutdown(stcb,
924                                     stcb->asoc.primary_destination);
925                                 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_T3);
926                                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
927                                     (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
928                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
929                                 }
930                                 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
931                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
932                                     stcb->sctp_ep, stcb,
933                                     asoc->primary_destination);
934                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
935                                     stcb->sctp_ep, stcb,
936                                     asoc->primary_destination);
937                         }
938                 } else {
939                         /*
940                          * we still got (or just got) data to send, so set
941                          * SHUTDOWN_PENDING
942                          */
943                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
944                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
945                             asoc->primary_destination);
946
947                         if (asoc->locked_on_sending) {
948                                 /* Locked to send out the data */
949                                 struct sctp_stream_queue_pending *sp;
950
951                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
952                                 if (sp == NULL) {
953                                         printf("Error, sp is NULL, locked on sending is non-null strm:%d\n",
954                                             asoc->locked_on_sending->stream_no);
955                                 } else {
956                                         if ((sp->length == 0) && (sp->msg_is_complete == 0)) {
957                                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
958                                         }
959                                 }
960                         }
961                         if (TAILQ_EMPTY(&asoc->send_queue) &&
962                             TAILQ_EMPTY(&asoc->sent_queue) &&
963                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
964                                 struct mbuf *op_err;
965
966                 abort_anyway:
967                                 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
968                                     0, M_DONTWAIT, 1, MT_DATA);
969                                 if (op_err) {
970                                         /* Fill in the user initiated abort */
971                                         struct sctp_paramhdr *ph;
972                                         uint32_t *ippp;
973
974                                         SCTP_BUF_LEN(op_err) =
975                                             sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
976                                         ph = mtod(op_err,
977                                             struct sctp_paramhdr *);
978                                         ph->param_type = htons(
979                                             SCTP_CAUSE_USER_INITIATED_ABT);
980                                         ph->param_length = htons(SCTP_BUF_LEN(op_err));
981                                         ippp = (uint32_t *) (ph + 1);
982                                         *ippp = htonl(SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6);
983                                 }
984                                 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_USRREQ + SCTP_LOC_6;
985                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
986                                     SCTP_RESPONSE_TO_USER_REQ,
987                                     op_err);
988                                 goto skip_unlock;
989                         }
990                 }
991                 SCTP_TCB_UNLOCK(stcb);
992         }
993 skip_unlock:
994         SCTP_INP_RUNLOCK(inp);
995         return 0;
996 }
997
998 /*
999  * copies a "user" presentable address and removes embedded scope, etc.
1000  * returns 0 on success, 1 on error
1001  */
1002 static uint32_t
1003 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1004 {
1005         struct sockaddr_in6 lsa6;
1006
1007         sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1008             &lsa6);
1009         memcpy(ss, sa, sa->sa_len);
1010         return (0);
1011 }
1012
1013
1014
1015 static size_t
1016 sctp_fill_up_addresses_vrf(struct sctp_inpcb *inp,
1017     struct sctp_tcb *stcb,
1018     size_t limit,
1019     struct sockaddr_storage *sas,
1020     uint32_t vrf_id)
1021 {
1022         struct sctp_ifn *sctp_ifn;
1023         struct sctp_ifa *sctp_ifa;
1024         int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1025         size_t actual;
1026         int ipv4_addr_legal, ipv6_addr_legal;
1027         struct sctp_vrf *vrf;
1028
1029         actual = 0;
1030         if (limit <= 0)
1031                 return (actual);
1032
1033         if (stcb) {
1034                 /* Turn on all the appropriate scope */
1035                 loopback_scope = stcb->asoc.loopback_scope;
1036                 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1037                 local_scope = stcb->asoc.local_scope;
1038                 site_scope = stcb->asoc.site_scope;
1039         } else {
1040                 /* Turn on ALL scope, since we look at the EP */
1041                 loopback_scope = ipv4_local_scope = local_scope =
1042                     site_scope = 1;
1043         }
1044         ipv4_addr_legal = ipv6_addr_legal = 0;
1045         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1046                 ipv6_addr_legal = 1;
1047                 if (SCTP_IPV6_V6ONLY(inp) == 0) {
1048                         ipv4_addr_legal = 1;
1049                 }
1050         } else {
1051                 ipv4_addr_legal = 1;
1052         }
1053         vrf = sctp_find_vrf(vrf_id);
1054         if (vrf == NULL) {
1055                 return (0);
1056         }
1057         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1058                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1059                         if ((loopback_scope == 0) &&
1060                             SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1061                                 /* Skip loopback if loopback_scope not set */
1062                                 continue;
1063                         }
1064                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1065                                 if (stcb) {
1066                                         /*
1067                                          * For the BOUND-ALL case, the list
1068                                          * associated with a TCB is Always
1069                                          * considered a reverse list.. i.e.
1070                                          * it lists addresses that are NOT
1071                                          * part of the association. If this
1072                                          * is one of those we must skip it.
1073                                          */
1074                                         if (sctp_is_addr_restricted(stcb,
1075                                             sctp_ifa)) {
1076                                                 continue;
1077                                         }
1078                                 }
1079                                 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
1080                                     (ipv4_addr_legal)) {
1081                                         struct sockaddr_in *sin;
1082
1083                                         sin = (struct sockaddr_in *)&sctp_ifa->address.sa;
1084                                         if (sin->sin_addr.s_addr == 0) {
1085                                                 /*
1086                                                  * we skip unspecifed
1087                                                  * addresses
1088                                                  */
1089                                                 continue;
1090                                         }
1091                                         if ((ipv4_local_scope == 0) &&
1092                                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1093                                                 continue;
1094                                         }
1095                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1096                                                 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1097                                                 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1098                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1099                                                 actual += sizeof(sizeof(struct sockaddr_in6));
1100                                         } else {
1101                                                 memcpy(sas, sin, sizeof(*sin));
1102                                                 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1103                                                 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1104                                                 actual += sizeof(*sin);
1105                                         }
1106                                         if (actual >= limit) {
1107                                                 return (actual);
1108                                         }
1109                                 } else if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
1110                                     (ipv6_addr_legal)) {
1111                                         struct sockaddr_in6 *sin6;
1112
1113                                         sin6 = (struct sockaddr_in6 *)&sctp_ifa->address.sa;
1114                                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1115                                                 /*
1116                                                  * we skip unspecifed
1117                                                  * addresses
1118                                                  */
1119                                                 continue;
1120                                         }
1121                                         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1122                                                 if (local_scope == 0)
1123                                                         continue;
1124                                                 if (sin6->sin6_scope_id == 0) {
1125                                                         if (sa6_recoverscope(sin6) != 0)
1126                                                                 /*
1127                                                                  * bad link
1128                                                                  * local
1129                                                                  * address
1130                                                                  */
1131                                                                 continue;
1132                                                 }
1133                                         }
1134                                         if ((site_scope == 0) &&
1135                                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1136                                                 continue;
1137                                         }
1138                                         memcpy(sas, sin6, sizeof(*sin6));
1139                                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1140                                         sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1141                                         actual += sizeof(*sin6);
1142                                         if (actual >= limit) {
1143                                                 return (actual);
1144                                         }
1145                                 }
1146                         }
1147                 }
1148         } else {
1149                 struct sctp_laddr *laddr;
1150
1151                 /* The list is a NEGATIVE list */
1152                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1153                         if (stcb) {
1154                                 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1155                                         continue;
1156                                 }
1157                         }
1158                         if (sctp_fill_user_address(sas, &laddr->ifa->address.sa))
1159                                 continue;
1160
1161                         ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1162                         sas = (struct sockaddr_storage *)((caddr_t)sas +
1163                             laddr->ifa->address.sa.sa_len);
1164                         actual += laddr->ifa->address.sa.sa_len;
1165                         if (actual >= limit) {
1166                                 return (actual);
1167                         }
1168                 }
1169         }
1170         return (actual);
1171 }
1172
1173 static size_t
1174 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1175     struct sctp_tcb *stcb,
1176     size_t limit,
1177     struct sockaddr_storage *sas)
1178 {
1179         size_t size = 0;
1180
1181         /* fill up addresses for the endpoint's default vrf */
1182         size = sctp_fill_up_addresses_vrf(inp, stcb, limit, sas,
1183             inp->def_vrf_id);
1184         return (size);
1185 }
1186
1187 static int
1188 sctp_count_max_addresses_vrf(struct sctp_inpcb *inp, uint32_t vrf_id)
1189 {
1190         int cnt = 0;
1191         struct sctp_vrf *vrf = NULL;
1192
1193         /*
1194          * In both sub-set bound an bound_all cases we return the MAXIMUM
1195          * number of addresses that you COULD get. In reality the sub-set
1196          * bound may have an exclusion list for a given TCB OR in the
1197          * bound-all case a TCB may NOT include the loopback or other
1198          * addresses as well.
1199          */
1200         vrf = sctp_find_vrf(vrf_id);
1201         if (vrf == NULL) {
1202                 return (0);
1203         }
1204         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1205                 struct sctp_ifn *sctp_ifn;
1206                 struct sctp_ifa *sctp_ifa;
1207
1208                 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1209                         LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1210                                 /* Count them if they are the right type */
1211                                 if (sctp_ifa->address.sa.sa_family == AF_INET) {
1212                                         if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1213                                                 cnt += sizeof(struct sockaddr_in6);
1214                                         else
1215                                                 cnt += sizeof(struct sockaddr_in);
1216
1217                                 } else if (sctp_ifa->address.sa.sa_family == AF_INET6)
1218                                         cnt += sizeof(struct sockaddr_in6);
1219                         }
1220                 }
1221         } else {
1222                 struct sctp_laddr *laddr;
1223
1224                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1225                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
1226                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1227                                         cnt += sizeof(struct sockaddr_in6);
1228                                 else
1229                                         cnt += sizeof(struct sockaddr_in);
1230
1231                         } else if (laddr->ifa->address.sa.sa_family == AF_INET6)
1232                                 cnt += sizeof(struct sockaddr_in6);
1233                 }
1234         }
1235         return (cnt);
1236 }
1237
1238 static int
1239 sctp_count_max_addresses(struct sctp_inpcb *inp)
1240 {
1241         int cnt = 0;
1242
1243         /* count addresses for the endpoint's default VRF */
1244         cnt = sctp_count_max_addresses_vrf(inp, inp->def_vrf_id);
1245         return (cnt);
1246 }
1247
1248 static int
1249 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, void *optval,
1250     size_t optsize, void *p, int delay)
1251 {
1252         int error = 0;
1253         int creat_lock_on = 0;
1254         struct sctp_tcb *stcb = NULL;
1255         struct sockaddr *sa;
1256         int num_v6 = 0, num_v4 = 0, *totaddrp, totaddr;
1257         int added = 0;
1258         uint32_t vrf_id;
1259         sctp_assoc_t *a_id;
1260
1261 #ifdef SCTP_DEBUG
1262         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1263                 printf("Connectx called\n");
1264         }
1265 #endif                          /* SCTP_DEBUG */
1266
1267         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1268             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1269                 /* We are already connected AND the TCP model */
1270                 return (EADDRINUSE);
1271         }
1272         if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1273                 return (EINVAL);
1274         }
1275         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1276                 SCTP_INP_RLOCK(inp);
1277                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1278                 SCTP_INP_RUNLOCK(inp);
1279         }
1280         if (stcb) {
1281                 return (EALREADY);
1282         }
1283         SCTP_INP_INCR_REF(inp);
1284         SCTP_ASOC_CREATE_LOCK(inp);
1285         creat_lock_on = 1;
1286         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1287             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1288                 error = EFAULT;
1289                 goto out_now;
1290         }
1291         totaddrp = (int *)optval;
1292         totaddr = *totaddrp;
1293         sa = (struct sockaddr *)(totaddrp + 1);
1294         stcb = sctp_connectx_helper_find(inp, sa, &totaddr, &num_v4, &num_v6, &error, (optsize - sizeof(int)));
1295         if (stcb != NULL) {
1296                 /* Already have or am bring up an association */
1297                 SCTP_ASOC_CREATE_UNLOCK(inp);
1298                 creat_lock_on = 0;
1299                 SCTP_TCB_UNLOCK(stcb);
1300                 error = EALREADY;
1301                 goto out_now;
1302         }
1303 #ifdef INET6
1304         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1305             (num_v6 > 0)) {
1306                 error = EINVAL;
1307                 goto out_now;
1308         }
1309         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1310             (num_v4 > 0)) {
1311                 struct in6pcb *inp6;
1312
1313                 inp6 = (struct in6pcb *)inp;
1314                 if (SCTP_IPV6_V6ONLY(inp6)) {
1315                         /*
1316                          * if IPV6_V6ONLY flag, ignore connections destined
1317                          * to a v4 addr or v4-mapped addr
1318                          */
1319                         error = EINVAL;
1320                         goto out_now;
1321                 }
1322         }
1323 #endif                          /* INET6 */
1324         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1325             SCTP_PCB_FLAGS_UNBOUND) {
1326                 /* Bind a ephemeral port */
1327                 error = sctp_inpcb_bind(so, NULL, p);
1328                 if (error) {
1329                         goto out_now;
1330                 }
1331         }
1332         /* FIX ME: do we want to pass in a vrf on the connect call? */
1333         vrf_id = inp->def_vrf_id;
1334
1335         /* We are GOOD to go */
1336         stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0, vrf_id);
1337         if (stcb == NULL) {
1338                 /* Gak! no memory */
1339                 goto out_now;
1340         }
1341         stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1342         /* move to second address */
1343         if (sa->sa_family == AF_INET)
1344                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1345         else
1346                 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1347
1348         added = sctp_connectx_helper_add(stcb, sa, (totaddr - 1), &error);
1349         /* Fill in the return id */
1350         a_id = (sctp_assoc_t *) optval;
1351         *a_id = sctp_get_associd(stcb);
1352
1353         /* initialize authentication parameters for the assoc */
1354         sctp_initialize_auth_params(inp, stcb);
1355
1356         if (delay) {
1357                 /* doing delayed connection */
1358                 stcb->asoc.delayed_connection = 1;
1359                 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1360         } else {
1361                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1362                 sctp_send_initiate(inp, stcb);
1363         }
1364         SCTP_TCB_UNLOCK(stcb);
1365         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1366                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1367                 /* Set the connected flag so we can queue data */
1368                 soisconnecting(so);
1369         }
1370 out_now:
1371         if (creat_lock_on)
1372                 SCTP_ASOC_CREATE_UNLOCK(inp);
1373         SCTP_INP_DECR_REF(inp);
1374         return error;
1375 }
1376
1377 #define SCTP_FIND_STCB(inp, stcb, assoc_id) \
1378         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { \
1379                 SCTP_INP_RLOCK(inp); \
1380                 stcb = LIST_FIRST(&inp->sctp_asoc_list); \
1381                 if (stcb) \
1382                         SCTP_TCB_LOCK(stcb); \
1383                 SCTP_INP_RUNLOCK(inp); \
1384         } else if (assoc_id != 0) { \
1385                 stcb = sctp_findassociation_ep_asocid(inp, assoc_id, 1); \
1386                 if (stcb == NULL) { \
1387                         error = ENOENT; \
1388                         break; \
1389                 } \
1390         } else { \
1391                 stcb = NULL; \
1392         }
1393
1394 #define SCTP_CHECK_AND_CAST(destp, srcp, type, size) \
1395         if (size < sizeof(type)) { \
1396                 error = EINVAL; \
1397                 break; \
1398         } else { \
1399                 destp = (type *)srcp; \
1400         }
1401
1402 static int
1403 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
1404     void *p)
1405 {
1406         struct sctp_inpcb *inp;
1407         int error, val = 0;
1408         struct sctp_tcb *stcb = NULL;
1409
1410         if (optval == NULL) {
1411                 return (EINVAL);
1412         }
1413         inp = (struct sctp_inpcb *)so->so_pcb;
1414         if (inp == 0)
1415                 return EINVAL;
1416         error = 0;
1417
1418         switch (optname) {
1419         case SCTP_NODELAY:
1420         case SCTP_AUTOCLOSE:
1421         case SCTP_EXPLICIT_EOR:
1422         case SCTP_AUTO_ASCONF:
1423         case SCTP_DISABLE_FRAGMENTS:
1424         case SCTP_I_WANT_MAPPED_V4_ADDR:
1425         case SCTP_USE_EXT_RCVINFO:
1426                 SCTP_INP_RLOCK(inp);
1427                 switch (optname) {
1428                 case SCTP_DISABLE_FRAGMENTS:
1429                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT);
1430                         break;
1431                 case SCTP_I_WANT_MAPPED_V4_ADDR:
1432                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4);
1433                         break;
1434                 case SCTP_AUTO_ASCONF:
1435                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1436                         break;
1437                 case SCTP_EXPLICIT_EOR:
1438                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
1439                         break;
1440                 case SCTP_NODELAY:
1441                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY);
1442                         break;
1443                 case SCTP_USE_EXT_RCVINFO:
1444                         val = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO);
1445                         break;
1446                 case SCTP_AUTOCLOSE:
1447                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))
1448                                 val = TICKS_TO_SEC(inp->sctp_ep.auto_close_time);
1449                         else
1450                                 val = 0;
1451                         break;
1452
1453                 default:
1454                         error = ENOPROTOOPT;
1455                 }               /* end switch (sopt->sopt_name) */
1456                 if (optname != SCTP_AUTOCLOSE) {
1457                         /* make it an "on/off" value */
1458                         val = (val != 0);
1459                 }
1460                 if (*optsize < sizeof(val)) {
1461                         error = EINVAL;
1462                 }
1463                 SCTP_INP_RUNLOCK(inp);
1464                 if (error == 0) {
1465                         /* return the option value */
1466                         *(int *)optval = val;
1467                         *optsize = sizeof(val);
1468                 }
1469                 break;
1470
1471         case SCTP_PARTIAL_DELIVERY_POINT:
1472                 {
1473                         uint32_t *value;
1474
1475                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1476                         *value = inp->partial_delivery_point;
1477                         *optsize = sizeof(uint32_t);
1478                 }
1479                 break;
1480         case SCTP_FRAGMENT_INTERLEAVE:
1481                 {
1482                         uint32_t *value;
1483
1484                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1485                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) {
1486                                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) {
1487                                         *value = SCTP_FRAG_LEVEL_2;
1488                                 } else {
1489                                         *value = SCTP_FRAG_LEVEL_1;
1490                                 }
1491                         } else {
1492                                 *value = SCTP_FRAG_LEVEL_0;
1493                         }
1494                         *optsize = sizeof(uint32_t);
1495                 }
1496                 break;
1497         case SCTP_CMT_ON_OFF:
1498                 {
1499                         struct sctp_assoc_value *av;
1500
1501                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1502                         if (sctp_cmt_on_off) {
1503                                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1504                                 if (stcb) {
1505                                         av->assoc_value = stcb->asoc.sctp_cmt_on_off;
1506                                         SCTP_TCB_UNLOCK(stcb);
1507
1508                                 } else {
1509                                         error = ENOTCONN;
1510                                 }
1511                         } else {
1512                                 error = ENOPROTOOPT;
1513                         }
1514                         *optsize = sizeof(*av);
1515                 }
1516                 break;
1517         case SCTP_GET_ADDR_LEN:
1518                 {
1519                         struct sctp_assoc_value *av;
1520
1521                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1522                         error = EINVAL;
1523 #ifdef INET
1524                         if (av->assoc_value == AF_INET) {
1525                                 av->assoc_value = sizeof(struct sockaddr_in);
1526                                 error = 0;
1527                         }
1528 #endif
1529 #ifdef INET6
1530                         if (av->assoc_value == AF_INET6) {
1531                                 av->assoc_value = sizeof(struct sockaddr_in6);
1532                                 error = 0;
1533                         }
1534 #endif
1535                         *optsize = sizeof(*av);
1536                 }
1537                 break;
1538         case SCTP_GET_ASOC_ID_LIST:
1539                 {
1540                         struct sctp_assoc_ids *ids;
1541                         int cnt, at;
1542                         uint16_t orig;
1543
1544                         SCTP_CHECK_AND_CAST(ids, optval, struct sctp_assoc_ids, *optsize);
1545                         cnt = 0;
1546                         SCTP_INP_RLOCK(inp);
1547                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1548                         if (stcb == NULL) {
1549                 none_out_now:
1550                                 ids->asls_numb_present = 0;
1551                                 ids->asls_more_to_get = 0;
1552                                 SCTP_INP_RUNLOCK(inp);
1553                                 break;
1554                         }
1555                         orig = ids->asls_assoc_start;
1556                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
1557                         while (orig) {
1558                                 stcb = LIST_NEXT(stcb, sctp_tcblist);
1559                                 orig--;
1560                                 cnt--;
1561                                 if (stcb == NULL)
1562                                         goto none_out_now;
1563                         }
1564                         if (stcb == NULL)
1565                                 goto none_out_now;
1566
1567                         at = 0;
1568                         ids->asls_numb_present = 0;
1569                         ids->asls_more_to_get = 1;
1570                         while (at < MAX_ASOC_IDS_RET) {
1571                                 ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1572                                 at++;
1573                                 ids->asls_numb_present++;
1574                                 stcb = LIST_NEXT(stcb, sctp_tcblist);
1575                                 if (stcb == NULL) {
1576                                         ids->asls_more_to_get = 0;
1577                                         break;
1578                                 }
1579                         }
1580                         SCTP_INP_RUNLOCK(inp);
1581                 }
1582                 break;
1583         case SCTP_CONTEXT:
1584                 {
1585                         struct sctp_assoc_value *av;
1586
1587                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1588                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1589
1590                         if (stcb) {
1591                                 av->assoc_value = stcb->asoc.context;
1592                                 SCTP_TCB_UNLOCK(stcb);
1593                         } else {
1594                                 SCTP_INP_RLOCK(inp);
1595                                 av->assoc_value = inp->sctp_context;
1596                                 SCTP_INP_RUNLOCK(inp);
1597                         }
1598                         *optsize = sizeof(*av);
1599                 }
1600                 break;
1601         case SCTP_VRF_ID:
1602                 {
1603                         uint32_t *vrf_id;
1604
1605                         SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, *optsize);
1606                         *vrf_id = inp->def_vrf_id;
1607                         break;
1608                 }
1609         case SCTP_GET_ASOC_VRF:
1610                 {
1611                         struct sctp_assoc_value *id;
1612
1613                         SCTP_CHECK_AND_CAST(id, optval, struct sctp_assoc_value, *optsize);
1614                         SCTP_FIND_STCB(inp, stcb, id->assoc_id);
1615                         if (stcb == NULL) {
1616                                 error = EINVAL;
1617                                 break;
1618                         }
1619                         id->assoc_value = stcb->asoc.vrf_id;
1620                         break;
1621                 }
1622         case SCTP_GET_VRF_IDS:
1623                 {
1624                         error = EOPNOTSUPP;
1625                         break;
1626                 }
1627         case SCTP_GET_NONCE_VALUES:
1628                 {
1629                         struct sctp_get_nonce_values *gnv;
1630
1631                         SCTP_CHECK_AND_CAST(gnv, optval, struct sctp_get_nonce_values, *optsize);
1632                         SCTP_FIND_STCB(inp, stcb, gnv->gn_assoc_id);
1633
1634                         if (stcb) {
1635                                 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1636                                 gnv->gn_local_tag = stcb->asoc.my_vtag;
1637                                 SCTP_TCB_UNLOCK(stcb);
1638                         } else {
1639                                 error = ENOTCONN;
1640                         }
1641                         *optsize = sizeof(*gnv);
1642                 }
1643                 break;
1644         case SCTP_DELAYED_ACK_TIME:
1645                 {
1646                         struct sctp_assoc_value *tm;
1647
1648                         SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, *optsize);
1649                         SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
1650
1651                         if (stcb) {
1652                                 tm->assoc_value = stcb->asoc.delayed_ack;
1653                                 SCTP_TCB_UNLOCK(stcb);
1654                         } else {
1655                                 SCTP_INP_RLOCK(inp);
1656                                 tm->assoc_value = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1657                                 SCTP_INP_RUNLOCK(inp);
1658                         }
1659                         *optsize = sizeof(*tm);
1660                 }
1661                 break;
1662
1663         case SCTP_GET_SNDBUF_USE:
1664                 {
1665                         struct sctp_sockstat *ss;
1666
1667                         SCTP_CHECK_AND_CAST(ss, optval, struct sctp_sockstat, *optsize);
1668                         SCTP_FIND_STCB(inp, stcb, ss->ss_assoc_id);
1669
1670                         if (stcb) {
1671                                 ss->ss_total_sndbuf = stcb->asoc.total_output_queue_size;
1672                                 ss->ss_total_recv_buf = (stcb->asoc.size_on_reasm_queue +
1673                                     stcb->asoc.size_on_all_streams);
1674                                 SCTP_TCB_UNLOCK(stcb);
1675                         } else {
1676                                 error = ENOTCONN;
1677                         }
1678                         *optsize = sizeof(struct sctp_sockstat);
1679                 }
1680                 break;
1681         case SCTP_MAXBURST:
1682                 {
1683                         uint8_t *value;
1684
1685                         SCTP_CHECK_AND_CAST(value, optval, uint8_t, *optsize);
1686
1687                         SCTP_INP_RLOCK(inp);
1688                         *value = inp->sctp_ep.max_burst;
1689                         SCTP_INP_RUNLOCK(inp);
1690                         *optsize = sizeof(uint8_t);
1691                 }
1692                 break;
1693         case SCTP_MAXSEG:
1694                 {
1695                         struct sctp_assoc_value *av;
1696                         int ovh;
1697
1698                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, *optsize);
1699                         if (av->assoc_id) {
1700                                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
1701                         } else {
1702                                 stcb = NULL;
1703                         }
1704
1705                         if (stcb) {
1706                                 av->assoc_value = sctp_get_frag_point(stcb, &stcb->asoc);
1707                                 SCTP_TCB_UNLOCK(stcb);
1708                         } else {
1709                                 SCTP_INP_RLOCK(inp);
1710                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1711                                         ovh = SCTP_MED_OVERHEAD;
1712                                 } else {
1713                                         ovh = SCTP_MED_V4_OVERHEAD;
1714                                 }
1715                                 av->assoc_value = inp->sctp_frag_point - ovh;
1716                                 SCTP_INP_RUNLOCK(inp);
1717                         }
1718                         *optsize = sizeof(struct sctp_assoc_value);
1719                 }
1720                 break;
1721         case SCTP_GET_STAT_LOG:
1722 #ifdef SCTP_STAT_LOGGING
1723                 error = sctp_fill_stat_log(optval, optsize);
1724 #else
1725                 error = EOPNOTSUPP;
1726 #endif
1727                 break;
1728         case SCTP_EVENTS:
1729                 {
1730                         struct sctp_event_subscribe *events;
1731
1732                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, *optsize);
1733                         memset(events, 0, sizeof(*events));
1734                         SCTP_INP_RLOCK(inp);
1735                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT))
1736                                 events->sctp_data_io_event = 1;
1737
1738                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT))
1739                                 events->sctp_association_event = 1;
1740
1741                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT))
1742                                 events->sctp_address_event = 1;
1743
1744                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT))
1745                                 events->sctp_send_failure_event = 1;
1746
1747                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR))
1748                                 events->sctp_peer_error_event = 1;
1749
1750                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT))
1751                                 events->sctp_shutdown_event = 1;
1752
1753                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT))
1754                                 events->sctp_partial_delivery_event = 1;
1755
1756                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT))
1757                                 events->sctp_adaptation_layer_event = 1;
1758
1759                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT))
1760                                 events->sctp_authentication_event = 1;
1761
1762                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT))
1763                                 events->sctp_stream_reset_events = 1;
1764                         SCTP_INP_RUNLOCK(inp);
1765                         *optsize = sizeof(struct sctp_event_subscribe);
1766                 }
1767                 break;
1768
1769         case SCTP_ADAPTATION_LAYER:
1770                 {
1771                         uint32_t *value;
1772
1773                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1774
1775                         SCTP_INP_RLOCK(inp);
1776                         *value = inp->sctp_ep.adaptation_layer_indicator;
1777                         SCTP_INP_RUNLOCK(inp);
1778                         *optsize = sizeof(uint32_t);
1779                 }
1780                 break;
1781         case SCTP_SET_INITIAL_DBG_SEQ:
1782                 {
1783                         uint32_t *value;
1784
1785                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1786                         SCTP_INP_RLOCK(inp);
1787                         *value = inp->sctp_ep.initial_sequence_debug;
1788                         SCTP_INP_RUNLOCK(inp);
1789                         *optsize = sizeof(uint32_t);
1790                 }
1791                 break;
1792         case SCTP_GET_LOCAL_ADDR_SIZE:
1793                 {
1794                         uint32_t *value;
1795
1796                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1797                         SCTP_INP_RLOCK(inp);
1798                         *value = sctp_count_max_addresses(inp);
1799                         SCTP_INP_RUNLOCK(inp);
1800                         *optsize = sizeof(uint32_t);
1801                 }
1802                 break;
1803         case SCTP_GET_REMOTE_ADDR_SIZE:
1804                 {
1805                         uint32_t *value;
1806                         size_t size;
1807                         struct sctp_nets *net;
1808
1809                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, *optsize);
1810                         /* FIXME MT: change to sctp_assoc_value? */
1811                         SCTP_FIND_STCB(inp, stcb, (sctp_assoc_t) * value);
1812
1813                         if (stcb) {
1814                                 size = 0;
1815                                 /* Count the sizes */
1816                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1817                                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1818                                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1819                                                 size += sizeof(struct sockaddr_in6);
1820                                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1821                                                 size += sizeof(struct sockaddr_in);
1822                                         } else {
1823                                                 /* huh */
1824                                                 break;
1825                                         }
1826                                 }
1827                                 SCTP_TCB_UNLOCK(stcb);
1828                                 *value = (uint32_t) size;
1829                         } else {
1830                                 error = ENOTCONN;
1831                         }
1832                         *optsize = sizeof(uint32_t);
1833                 }
1834                 break;
1835         case SCTP_GET_PEER_ADDRESSES:
1836                 /*
1837                  * Get the address information, an array is passed in to
1838                  * fill up we pack it.
1839                  */
1840                 {
1841                         size_t cpsz, left;
1842                         struct sockaddr_storage *sas;
1843                         struct sctp_nets *net;
1844                         struct sctp_getaddresses *saddr;
1845
1846                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1847                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1848
1849                         if (stcb) {
1850                                 left = (*optsize) - sizeof(struct sctp_getaddresses);
1851                                 *optsize = sizeof(struct sctp_getaddresses);
1852                                 sas = (struct sockaddr_storage *)&saddr->addr[0];
1853
1854                                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1855                                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1856                                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
1857                                                 cpsz = sizeof(struct sockaddr_in6);
1858                                         } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
1859                                                 cpsz = sizeof(struct sockaddr_in);
1860                                         } else {
1861                                                 /* huh */
1862                                                 break;
1863                                         }
1864                                         if (left < cpsz) {
1865                                                 /* not enough room. */
1866                                                 break;
1867                                         }
1868                                         if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1869                                             (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
1870                                                 /* Must map the address */
1871                                                 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
1872                                                     (struct sockaddr_in6 *)sas);
1873                                         } else {
1874                                                 memcpy(sas, &net->ro._l_addr, cpsz);
1875                                         }
1876                                         ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1877
1878                                         sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
1879                                         left -= cpsz;
1880                                         *optsize += cpsz;
1881                                 }
1882                                 SCTP_TCB_UNLOCK(stcb);
1883                         } else {
1884                                 error = ENOENT;
1885                         }
1886                 }
1887                 break;
1888         case SCTP_GET_LOCAL_ADDRESSES:
1889                 {
1890                         size_t limit, actual;
1891                         struct sockaddr_storage *sas;
1892                         struct sctp_getaddresses *saddr;
1893
1894                         SCTP_CHECK_AND_CAST(saddr, optval, struct sctp_getaddresses, *optsize);
1895                         SCTP_FIND_STCB(inp, stcb, saddr->sget_assoc_id);
1896
1897                         sas = (struct sockaddr_storage *)&saddr->addr[0];
1898                         limit = *optsize - sizeof(sctp_assoc_t);
1899                         actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1900                         if (stcb)
1901                                 SCTP_TCB_UNLOCK(stcb);
1902                         *optsize = sizeof(struct sockaddr_storage) + actual;
1903                 }
1904                 break;
1905         case SCTP_PEER_ADDR_PARAMS:
1906                 {
1907                         struct sctp_paddrparams *paddrp;
1908                         struct sctp_nets *net;
1909
1910                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, *optsize);
1911                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
1912
1913                         net = NULL;
1914                         if (stcb) {
1915                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1916                         } else {
1917                                 /*
1918                                  * We increment here since
1919                                  * sctp_findassociation_ep_addr() wil do a
1920                                  * decrement if it finds the stcb as long as
1921                                  * the locked tcb (last argument) is NOT a
1922                                  * TCB.. aka NULL.
1923                                  */
1924                                 SCTP_INP_INCR_REF(inp);
1925                                 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddrp->spp_address, &net, NULL, NULL);
1926                                 if (stcb == NULL) {
1927                                         SCTP_INP_DECR_REF(inp);
1928                                 }
1929                         }
1930
1931                         if (stcb) {
1932                                 /* Applys to the specific association */
1933                                 paddrp->spp_flags = 0;
1934                                 if (net) {
1935                                         paddrp->spp_pathmaxrxt = net->failure_threshold;
1936                                         paddrp->spp_pathmtu = net->mtu;
1937                                         /* get flags for HB */
1938                                         if (net->dest_state & SCTP_ADDR_NOHB)
1939                                                 paddrp->spp_flags |= SPP_HB_DISABLE;
1940                                         else
1941                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
1942                                         /* get flags for PMTU */
1943                                         if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
1944                                                 paddrp->spp_flags |= SPP_PMTUD_ENABLE;
1945                                         } else {
1946                                                 paddrp->spp_flags |= SPP_PMTUD_DISABLE;
1947                                         }
1948 #ifdef INET
1949                                         if (net->ro._l_addr.sin.sin_family == AF_INET) {
1950                                                 paddrp->spp_ipv4_tos = net->tos_flowlabel & 0x000000fc;
1951                                                 paddrp->spp_flags |= SPP_IPV4_TOS;
1952                                         }
1953 #endif
1954 #ifdef INET6
1955                                         if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
1956                                                 paddrp->spp_ipv6_flowlabel = net->tos_flowlabel;
1957                                                 paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1958                                         }
1959 #endif
1960                                 } else {
1961                                         /*
1962                                          * No destination so return default
1963                                          * value
1964                                          */
1965                                         paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
1966                                         paddrp->spp_pathmtu = sctp_get_frag_point(stcb, &stcb->asoc);
1967 #ifdef INET
1968                                         paddrp->spp_ipv4_tos = stcb->asoc.default_tos & 0x000000fc;
1969                                         paddrp->spp_flags |= SPP_IPV4_TOS;
1970 #endif
1971 #ifdef INET6
1972                                         paddrp->spp_ipv6_flowlabel = stcb->asoc.default_flowlabel;
1973                                         paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1974 #endif
1975                                         /* default settings should be these */
1976                                         if (sctp_is_hb_timer_running(stcb)) {
1977                                                 paddrp->spp_flags |= SPP_HB_ENABLE;
1978                                         }
1979                                 }
1980                                 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
1981                                 paddrp->spp_assoc_id = sctp_get_associd(stcb);
1982                                 SCTP_TCB_UNLOCK(stcb);
1983                         } else {
1984                                 /* Use endpoint defaults */
1985                                 SCTP_INP_RLOCK(inp);
1986                                 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
1987                                 paddrp->spp_hbinterval = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1988                                 paddrp->spp_assoc_id = (sctp_assoc_t) 0;
1989                                 /* get inp's default */
1990 #ifdef INET
1991                                 paddrp->spp_ipv4_tos = inp->ip_inp.inp.inp_ip_tos;
1992                                 paddrp->spp_flags |= SPP_IPV4_TOS;
1993 #endif
1994 #ifdef INET6
1995                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1996                                         paddrp->spp_ipv6_flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
1997                                         paddrp->spp_flags |= SPP_IPV6_FLOWLABEL;
1998                                 }
1999 #endif
2000                                 /* can't return this */
2001                                 paddrp->spp_pathmaxrxt = 0;
2002                                 paddrp->spp_pathmtu = 0;
2003                                 /* default behavior, no stcb */
2004                                 paddrp->spp_flags = SPP_HB_ENABLE | SPP_PMTUD_ENABLE;
2005
2006                                 SCTP_INP_RUNLOCK(inp);
2007                         }
2008                         *optsize = sizeof(struct sctp_paddrparams);
2009                 }
2010                 break;
2011         case SCTP_GET_PEER_ADDR_INFO:
2012                 {
2013                         struct sctp_paddrinfo *paddri;
2014                         struct sctp_nets *net;
2015
2016                         SCTP_CHECK_AND_CAST(paddri, optval, struct sctp_paddrinfo, *optsize);
2017                         SCTP_FIND_STCB(inp, stcb, paddri->spinfo_assoc_id);
2018
2019                         net = NULL;
2020                         if (stcb) {
2021                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddri->spinfo_address);
2022                         } else {
2023                                 /*
2024                                  * We increment here since
2025                                  * sctp_findassociation_ep_addr() wil do a
2026                                  * decrement if it finds the stcb as long as
2027                                  * the locked tcb (last argument) is NOT a
2028                                  * TCB.. aka NULL.
2029                                  */
2030                                 SCTP_INP_INCR_REF(inp);
2031                                 stcb = sctp_findassociation_ep_addr(&inp, (struct sockaddr *)&paddri->spinfo_address, &net, NULL, NULL);
2032                                 if (stcb == NULL) {
2033                                         SCTP_INP_DECR_REF(inp);
2034                                 }
2035                         }
2036
2037                         if ((stcb) && (net)) {
2038                                 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK | SCTP_ADDR_NOHB);
2039                                 paddri->spinfo_cwnd = net->cwnd;
2040                                 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2041                                 paddri->spinfo_rto = net->RTO;
2042                                 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2043                                 SCTP_TCB_UNLOCK(stcb);
2044                         } else {
2045                                 if (stcb) {
2046                                         SCTP_TCB_UNLOCK(stcb);
2047                                 }
2048                                 error = ENOENT;
2049                         }
2050                         *optsize = sizeof(struct sctp_paddrinfo);
2051                 }
2052                 break;
2053         case SCTP_PCB_STATUS:
2054                 {
2055                         struct sctp_pcbinfo *spcb;
2056
2057                         SCTP_CHECK_AND_CAST(spcb, optval, struct sctp_pcbinfo, *optsize);
2058                         sctp_fill_pcbinfo(spcb);
2059                         *optsize = sizeof(struct sctp_pcbinfo);
2060                 }
2061                 break;
2062
2063         case SCTP_STATUS:
2064                 {
2065                         struct sctp_nets *net;
2066                         struct sctp_status *sstat;
2067
2068                         SCTP_CHECK_AND_CAST(sstat, optval, struct sctp_status, *optsize);
2069                         SCTP_FIND_STCB(inp, stcb, sstat->sstat_assoc_id);
2070
2071                         if (stcb == NULL) {
2072                                 error = EINVAL;
2073                                 break;
2074                         }
2075                         /*
2076                          * I think passing the state is fine since
2077                          * sctp_constants.h will be available to the user
2078                          * land.
2079                          */
2080                         sstat->sstat_state = stcb->asoc.state;
2081                         sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2082                         sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2083                         /*
2084                          * We can't include chunks that have been passed to
2085                          * the socket layer. Only things in queue.
2086                          */
2087                         sstat->sstat_penddata = (stcb->asoc.cnt_on_reasm_queue +
2088                             stcb->asoc.cnt_on_all_streams);
2089
2090
2091                         sstat->sstat_instrms = stcb->asoc.streamincnt;
2092                         sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2093                         sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2094                         memcpy(&sstat->sstat_primary.spinfo_address,
2095                             &stcb->asoc.primary_destination->ro._l_addr,
2096                             ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2097                         net = stcb->asoc.primary_destination;
2098                         ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2099                         /*
2100                          * Again the user can get info from sctp_constants.h
2101                          * for what the state of the network is.
2102                          */
2103                         sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2104                         sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2105                         sstat->sstat_primary.spinfo_srtt = net->lastsa;
2106                         sstat->sstat_primary.spinfo_rto = net->RTO;
2107                         sstat->sstat_primary.spinfo_mtu = net->mtu;
2108                         sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2109                         SCTP_TCB_UNLOCK(stcb);
2110                         *optsize = sizeof(*sstat);
2111                 }
2112                 break;
2113         case SCTP_RTOINFO:
2114                 {
2115                         struct sctp_rtoinfo *srto;
2116
2117                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, *optsize);
2118                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
2119
2120                         if (stcb) {
2121                                 srto->srto_initial = stcb->asoc.initial_rto;
2122                                 srto->srto_max = stcb->asoc.maxrto;
2123                                 srto->srto_min = stcb->asoc.minrto;
2124                                 SCTP_TCB_UNLOCK(stcb);
2125                         } else {
2126                                 SCTP_INP_RLOCK(inp);
2127                                 srto->srto_initial = inp->sctp_ep.initial_rto;
2128                                 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2129                                 srto->srto_min = inp->sctp_ep.sctp_minrto;
2130                                 SCTP_INP_RUNLOCK(inp);
2131                         }
2132                         *optsize = sizeof(*srto);
2133                 }
2134                 break;
2135         case SCTP_ASSOCINFO:
2136                 {
2137                         struct sctp_assocparams *sasoc;
2138
2139                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, *optsize);
2140                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
2141
2142                         if (stcb) {
2143                                 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2144                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2145                                 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2146                                 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2147                                 sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2148                                 sasoc->sasoc_sack_delay = stcb->asoc.delayed_ack;
2149                                 sasoc->sasoc_sack_freq = stcb->asoc.sack_freq;
2150                                 SCTP_TCB_UNLOCK(stcb);
2151                         } else {
2152                                 SCTP_INP_RLOCK(inp);
2153                                 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2154                                 sasoc->sasoc_number_peer_destinations = 0;
2155                                 sasoc->sasoc_peer_rwnd = 0;
2156                                 sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2157                                 sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2158                                 sasoc->sasoc_sack_delay = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
2159                                 sasoc->sasoc_sack_freq = inp->sctp_ep.sctp_sack_freq;
2160                                 SCTP_INP_RUNLOCK(inp);
2161                         }
2162                         *optsize = sizeof(*sasoc);
2163                 }
2164                 break;
2165         case SCTP_DEFAULT_SEND_PARAM:
2166                 {
2167                         struct sctp_sndrcvinfo *s_info;
2168
2169                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, *optsize);
2170                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2171
2172                         if (stcb) {
2173                                 *s_info = stcb->asoc.def_send;
2174                                 SCTP_TCB_UNLOCK(stcb);
2175                         } else {
2176                                 SCTP_INP_RLOCK(inp);
2177                                 *s_info = inp->def_send;
2178                                 SCTP_INP_RUNLOCK(inp);
2179                         }
2180                         *optsize = sizeof(*s_info);
2181                 }
2182                 break;
2183         case SCTP_INITMSG:
2184                 {
2185                         struct sctp_initmsg *sinit;
2186
2187                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, *optsize);
2188                         SCTP_INP_RLOCK(inp);
2189                         sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2190                         sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2191                         sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2192                         sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2193                         SCTP_INP_RUNLOCK(inp);
2194                         *optsize = sizeof(*sinit);
2195                 }
2196                 break;
2197         case SCTP_PRIMARY_ADDR:
2198                 /* we allow a "get" operation on this */
2199                 {
2200                         struct sctp_setprim *ssp;
2201
2202                         SCTP_CHECK_AND_CAST(ssp, optval, struct sctp_setprim, *optsize);
2203                         SCTP_FIND_STCB(inp, stcb, ssp->ssp_assoc_id);
2204
2205                         if (stcb) {
2206                                 /* simply copy out the sockaddr_storage... */
2207                                 memcpy(&ssp->ssp_addr, &stcb->asoc.primary_destination->ro._l_addr,
2208                                     ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2209                                 SCTP_TCB_UNLOCK(stcb);
2210                         } else {
2211                                 error = EINVAL;
2212                         }
2213                         *optsize = sizeof(*ssp);
2214                 }
2215                 break;
2216
2217         case SCTP_HMAC_IDENT:
2218                 {
2219                         struct sctp_hmacalgo *shmac;
2220                         sctp_hmaclist_t *hmaclist;
2221                         uint32_t size;
2222                         int i;
2223
2224                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, *optsize);
2225
2226                         SCTP_INP_RLOCK(inp);
2227                         hmaclist = inp->sctp_ep.local_hmacs;
2228                         if (hmaclist == NULL) {
2229                                 /* no HMACs to return */
2230                                 *optsize = sizeof(*shmac);
2231                                 SCTP_INP_RUNLOCK(inp);
2232                                 break;
2233                         }
2234                         /* is there room for all of the hmac ids? */
2235                         size = sizeof(*shmac) + (hmaclist->num_algo *
2236                             sizeof(shmac->shmac_idents[0]));
2237                         if ((size_t)(*optsize) < size) {
2238                                 error = EINVAL;
2239                                 SCTP_INP_RUNLOCK(inp);
2240                                 break;
2241                         }
2242                         /* copy in the list */
2243                         for (i = 0; i < hmaclist->num_algo; i++)
2244                                 shmac->shmac_idents[i] = hmaclist->hmac[i];
2245                         SCTP_INP_RUNLOCK(inp);
2246                         *optsize = size;
2247                         break;
2248                 }
2249         case SCTP_AUTH_ACTIVE_KEY:
2250                 {
2251                         struct sctp_authkeyid *scact;
2252
2253                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, *optsize);
2254                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2255
2256                         if (stcb) {
2257                                 /* get the active key on the assoc */
2258                                 scact->scact_keynumber = stcb->asoc.authinfo.assoc_keyid;
2259                                 SCTP_TCB_UNLOCK(stcb);
2260                         } else {
2261                                 /* get the endpoint active key */
2262                                 SCTP_INP_RLOCK(inp);
2263                                 scact->scact_keynumber = inp->sctp_ep.default_keyid;
2264                                 SCTP_INP_RUNLOCK(inp);
2265                         }
2266                         *optsize = sizeof(*scact);
2267                         break;
2268                 }
2269         case SCTP_LOCAL_AUTH_CHUNKS:
2270                 {
2271                         struct sctp_authchunks *sac;
2272                         sctp_auth_chklist_t *chklist = NULL;
2273                         size_t size = 0;
2274
2275                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2276                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2277
2278                         if (stcb) {
2279                                 /* get off the assoc */
2280                                 chklist = stcb->asoc.local_auth_chunks;
2281                                 /* is there enough space? */
2282                                 size = sctp_auth_get_chklist_size(chklist);
2283                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2284                                         error = EINVAL;
2285                                 } else {
2286                                         /* copy in the chunks */
2287                                         sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2288                                 }
2289                                 SCTP_TCB_UNLOCK(stcb);
2290                         } else {
2291                                 /* get off the endpoint */
2292                                 SCTP_INP_RLOCK(inp);
2293                                 chklist = inp->sctp_ep.local_auth_chunks;
2294                                 /* is there enough space? */
2295                                 size = sctp_auth_get_chklist_size(chklist);
2296                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2297                                         error = EINVAL;
2298                                 } else {
2299                                         /* copy in the chunks */
2300                                         sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2301                                 }
2302                                 SCTP_INP_RUNLOCK(inp);
2303                         }
2304                         *optsize = sizeof(struct sctp_authchunks) + size;
2305                         break;
2306                 }
2307         case SCTP_PEER_AUTH_CHUNKS:
2308                 {
2309                         struct sctp_authchunks *sac;
2310                         sctp_auth_chklist_t *chklist = NULL;
2311                         size_t size = 0;
2312
2313                         SCTP_CHECK_AND_CAST(sac, optval, struct sctp_authchunks, *optsize);
2314                         SCTP_FIND_STCB(inp, stcb, sac->gauth_assoc_id);
2315
2316                         if (stcb) {
2317                                 /* get off the assoc */
2318                                 chklist = stcb->asoc.peer_auth_chunks;
2319                                 /* is there enough space? */
2320                                 size = sctp_auth_get_chklist_size(chklist);
2321                                 if (*optsize < (sizeof(struct sctp_authchunks) + size)) {
2322                                         error = EINVAL;
2323                                 } else {
2324                                         /* copy in the chunks */
2325                                         sctp_serialize_auth_chunks(chklist, sac->gauth_chunks);
2326                                 }
2327                                 SCTP_TCB_UNLOCK(stcb);
2328                         } else {
2329                                 error = ENOENT;
2330                         }
2331                         *optsize = sizeof(struct sctp_authchunks) + size;
2332                         break;
2333                 }
2334
2335
2336         default:
2337                 error = ENOPROTOOPT;
2338                 *optsize = 0;
2339                 break;
2340         }                       /* end switch (sopt->sopt_name) */
2341         return (error);
2342 }
2343
2344 static int
2345 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
2346     void *p)
2347 {
2348         int error, set_opt;
2349         uint32_t *mopt;
2350         struct sctp_tcb *stcb = NULL;
2351         struct sctp_inpcb *inp;
2352         uint32_t vrf_id;
2353
2354         if (optval == NULL) {
2355                 printf("optval is NULL\n");
2356                 return (EINVAL);
2357         }
2358         inp = (struct sctp_inpcb *)so->so_pcb;
2359         if (inp == 0) {
2360                 printf("inp is NULL?\n");
2361                 return EINVAL;
2362         }
2363         vrf_id = inp->def_vrf_id;
2364
2365         error = 0;
2366         switch (optname) {
2367         case SCTP_NODELAY:
2368         case SCTP_AUTOCLOSE:
2369         case SCTP_AUTO_ASCONF:
2370         case SCTP_EXPLICIT_EOR:
2371         case SCTP_DISABLE_FRAGMENTS:
2372         case SCTP_USE_EXT_RCVINFO:
2373         case SCTP_I_WANT_MAPPED_V4_ADDR:
2374                 /* copy in the option value */
2375                 SCTP_CHECK_AND_CAST(mopt, optval, uint32_t, optsize);
2376                 set_opt = 0;
2377                 if (error)
2378                         break;
2379                 switch (optname) {
2380                 case SCTP_DISABLE_FRAGMENTS:
2381                         set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2382                         break;
2383                 case SCTP_AUTO_ASCONF:
2384                         set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2385                         break;
2386                 case SCTP_EXPLICIT_EOR:
2387                         set_opt = SCTP_PCB_FLAGS_EXPLICIT_EOR;
2388                         break;
2389                 case SCTP_USE_EXT_RCVINFO:
2390                         set_opt = SCTP_PCB_FLAGS_EXT_RCVINFO;
2391                         break;
2392                 case SCTP_I_WANT_MAPPED_V4_ADDR:
2393                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2394                                 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2395                         } else {
2396                                 return (EINVAL);
2397                         }
2398                         break;
2399                 case SCTP_NODELAY:
2400                         set_opt = SCTP_PCB_FLAGS_NODELAY;
2401                         break;
2402                 case SCTP_AUTOCLOSE:
2403                         set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2404                         /*
2405                          * The value is in ticks. Note this does not effect
2406                          * old associations, only new ones.
2407                          */
2408                         inp->sctp_ep.auto_close_time = SEC_TO_TICKS(*mopt);
2409                         break;
2410                 }
2411                 SCTP_INP_WLOCK(inp);
2412                 if (*mopt != 0) {
2413                         sctp_feature_on(inp, set_opt);
2414                 } else {
2415                         sctp_feature_off(inp, set_opt);
2416                 }
2417                 SCTP_INP_WUNLOCK(inp);
2418                 break;
2419         case SCTP_PARTIAL_DELIVERY_POINT:
2420                 {
2421                         uint32_t *value;
2422
2423                         SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
2424                         if (*value > SCTP_SB_LIMIT_RCV(so)) {
2425                                 error = EINVAL;
2426                                 break;
2427                         }
2428                         inp->partial_delivery_point = *value;
2429                 }
2430                 break;
2431         case SCTP_FRAGMENT_INTERLEAVE:
2432                 /* not yet until we re-write sctp_recvmsg() */
2433                 {
2434                         uint32_t *level;
2435
2436                         SCTP_CHECK_AND_CAST(level, optval, uint32_t, optsize);
2437                         if (*level == SCTP_FRAG_LEVEL_2) {
2438                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2439                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2440                         } else if (*level == SCTP_FRAG_LEVEL_1) {
2441                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2442                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2443                         } else if (*level == SCTP_FRAG_LEVEL_0) {
2444                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2445                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2446
2447                         } else {
2448                                 error = EINVAL;
2449                         }
2450                 }
2451                 break;
2452         case SCTP_CMT_ON_OFF:
2453                 {
2454                         struct sctp_assoc_value *av;
2455
2456                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2457                         if (sctp_cmt_on_off) {
2458                                 SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2459                                 if (stcb) {
2460                                         stcb->asoc.sctp_cmt_on_off = (uint8_t) av->assoc_value;
2461                                         SCTP_TCB_UNLOCK(stcb);
2462                                 } else {
2463                                         error = ENOTCONN;
2464                                 }
2465                         } else {
2466                                 error = ENOPROTOOPT;
2467                         }
2468                 }
2469                 break;
2470         case SCTP_CLR_STAT_LOG:
2471 #ifdef SCTP_STAT_LOGGING
2472                 sctp_clr_stat_log();
2473 #else
2474                 error = EOPNOTSUPP;
2475 #endif
2476                 break;
2477         case SCTP_CONTEXT:
2478                 {
2479                         struct sctp_assoc_value *av;
2480
2481                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2482                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2483
2484                         if (stcb) {
2485                                 stcb->asoc.context = av->assoc_value;
2486                                 SCTP_TCB_UNLOCK(stcb);
2487                         } else {
2488                                 SCTP_INP_WLOCK(inp);
2489                                 inp->sctp_context = av->assoc_value;
2490                                 SCTP_INP_WUNLOCK(inp);
2491                         }
2492                 }
2493                 break;
2494         case SCTP_VRF_ID:
2495                 {
2496                         uint32_t *vrf_id;
2497
2498                         SCTP_CHECK_AND_CAST(vrf_id, optval, uint32_t, optsize);
2499                         if (*vrf_id > SCTP_MAX_VRF_ID) {
2500                                 error = EINVAL;
2501                                 break;
2502                         }
2503                         inp->def_vrf_id = *vrf_id;
2504                         break;
2505                 }
2506         case SCTP_DEL_VRF_ID:
2507                 {
2508                         error = EOPNOTSUPP;
2509                         break;
2510                 }
2511         case SCTP_ADD_VRF_ID:
2512                 {
2513                         error = EOPNOTSUPP;
2514                         break;
2515                 }
2516
2517         case SCTP_DELAYED_ACK_TIME:
2518                 {
2519                         struct sctp_assoc_value *tm;
2520
2521                         SCTP_CHECK_AND_CAST(tm, optval, struct sctp_assoc_value, optsize);
2522                         SCTP_FIND_STCB(inp, stcb, tm->assoc_id);
2523
2524                         if (stcb) {
2525                                 stcb->asoc.delayed_ack = tm->assoc_value;
2526                                 SCTP_TCB_UNLOCK(stcb);
2527                         } else {
2528                                 SCTP_INP_WLOCK(inp);
2529                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(tm->assoc_value);
2530                                 SCTP_INP_WUNLOCK(inp);
2531                         }
2532                         break;
2533                 }
2534         case SCTP_AUTH_CHUNK:
2535                 {
2536                         struct sctp_authchunk *sauth;
2537
2538                         SCTP_CHECK_AND_CAST(sauth, optval, struct sctp_authchunk, optsize);
2539
2540                         SCTP_INP_WLOCK(inp);
2541                         if (sctp_auth_add_chunk(sauth->sauth_chunk, inp->sctp_ep.local_auth_chunks))
2542                                 error = EINVAL;
2543                         SCTP_INP_WUNLOCK(inp);
2544                         break;
2545                 }
2546         case SCTP_AUTH_KEY:
2547                 {
2548                         struct sctp_authkey *sca;
2549                         struct sctp_keyhead *shared_keys;
2550                         sctp_sharedkey_t *shared_key;
2551                         sctp_key_t *key = NULL;
2552                         size_t size;
2553
2554                         SCTP_CHECK_AND_CAST(sca, optval, struct sctp_authkey, optsize);
2555                         SCTP_FIND_STCB(inp, stcb, sca->sca_assoc_id)
2556                             size = optsize - sizeof(*sca);
2557
2558                         if (stcb) {
2559                                 /* set it on the assoc */
2560                                 shared_keys = &stcb->asoc.shared_keys;
2561                                 /* clear the cached keys for this key id */
2562                                 sctp_clear_cachedkeys(stcb, sca->sca_keynumber);
2563                                 /*
2564                                  * create the new shared key and
2565                                  * insert/replace it
2566                                  */
2567                                 if (size > 0) {
2568                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
2569                                         if (key == NULL) {
2570                                                 error = ENOMEM;
2571                                                 SCTP_TCB_UNLOCK(stcb);
2572                                                 break;
2573                                         }
2574                                 }
2575                                 shared_key = sctp_alloc_sharedkey();
2576                                 if (shared_key == NULL) {
2577                                         sctp_free_key(key);
2578                                         error = ENOMEM;
2579                                         SCTP_TCB_UNLOCK(stcb);
2580                                         break;
2581                                 }
2582                                 shared_key->key = key;
2583                                 shared_key->keyid = sca->sca_keynumber;
2584                                 sctp_insert_sharedkey(shared_keys, shared_key);
2585                                 SCTP_TCB_UNLOCK(stcb);
2586                         } else {
2587                                 /* set it on the endpoint */
2588                                 SCTP_INP_WLOCK(inp);
2589                                 shared_keys = &inp->sctp_ep.shared_keys;
2590                                 /*
2591                                  * clear the cached keys on all assocs for
2592                                  * this key id
2593                                  */
2594                                 sctp_clear_cachedkeys_ep(inp, sca->sca_keynumber);
2595                                 /*
2596                                  * create the new shared key and
2597                                  * insert/replace it
2598                                  */
2599                                 if (size > 0) {
2600                                         key = sctp_set_key(sca->sca_key, (uint32_t) size);
2601                                         if (key == NULL) {
2602                                                 error = ENOMEM;
2603                                                 SCTP_INP_WUNLOCK(inp);
2604                                                 break;
2605                                         }
2606                                 }
2607                                 shared_key = sctp_alloc_sharedkey();
2608                                 if (shared_key == NULL) {
2609                                         sctp_free_key(key);
2610                                         error = ENOMEM;
2611                                         SCTP_INP_WUNLOCK(inp);
2612                                         break;
2613                                 }
2614                                 shared_key->key = key;
2615                                 shared_key->keyid = sca->sca_keynumber;
2616                                 sctp_insert_sharedkey(shared_keys, shared_key);
2617                                 SCTP_INP_WUNLOCK(inp);
2618                         }
2619                         break;
2620                 }
2621         case SCTP_HMAC_IDENT:
2622                 {
2623                         struct sctp_hmacalgo *shmac;
2624                         sctp_hmaclist_t *hmaclist;
2625                         uint32_t hmacid;
2626                         size_t size, i;
2627
2628                         SCTP_CHECK_AND_CAST(shmac, optval, struct sctp_hmacalgo, optsize);
2629                         size = (optsize - sizeof(*shmac)) / sizeof(shmac->shmac_idents[0]);
2630                         hmaclist = sctp_alloc_hmaclist(size);
2631                         if (hmaclist == NULL) {
2632                                 error = ENOMEM;
2633                                 break;
2634                         }
2635                         for (i = 0; i < size; i++) {
2636                                 hmacid = shmac->shmac_idents[i];
2637                                 if (sctp_auth_add_hmacid(hmaclist, (uint16_t) hmacid)) {
2638                                          /* invalid HMACs were found */ ;
2639                                         error = EINVAL;
2640                                         sctp_free_hmaclist(hmaclist);
2641                                         goto sctp_set_hmac_done;
2642                                 }
2643                         }
2644                         /* set it on the endpoint */
2645                         SCTP_INP_WLOCK(inp);
2646                         if (inp->sctp_ep.local_hmacs)
2647                                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2648                         inp->sctp_ep.local_hmacs = hmaclist;
2649                         SCTP_INP_WUNLOCK(inp);
2650         sctp_set_hmac_done:
2651                         break;
2652                 }
2653         case SCTP_AUTH_ACTIVE_KEY:
2654                 {
2655                         struct sctp_authkeyid *scact;
2656
2657                         SCTP_CHECK_AND_CAST(scact, optval, struct sctp_authkeyid, optsize);
2658                         SCTP_FIND_STCB(inp, stcb, scact->scact_assoc_id);
2659
2660                         /* set the active key on the right place */
2661                         if (stcb) {
2662                                 /* set the active key on the assoc */
2663                                 if (sctp_auth_setactivekey(stcb, scact->scact_keynumber))
2664                                         error = EINVAL;
2665                                 SCTP_TCB_UNLOCK(stcb);
2666                         } else {
2667                                 /* set the active key on the endpoint */
2668                                 SCTP_INP_WLOCK(inp);
2669                                 if (sctp_auth_setactivekey_ep(inp, scact->scact_keynumber))
2670                                         error = EINVAL;
2671                                 SCTP_INP_WUNLOCK(inp);
2672                         }
2673                         break;
2674                 }
2675         case SCTP_AUTH_DELETE_KEY:
2676                 {
2677                         struct sctp_authkeyid *scdel;
2678
2679                         SCTP_CHECK_AND_CAST(scdel, optval, struct sctp_authkeyid, optsize);
2680                         SCTP_FIND_STCB(inp, stcb, scdel->scact_assoc_id);
2681
2682                         /* delete the key from the right place */
2683                         if (stcb) {
2684                                 if (sctp_delete_sharedkey(stcb, scdel->scact_keynumber))
2685                                         error = EINVAL;
2686                                 SCTP_TCB_UNLOCK(stcb);
2687                         } else {
2688                                 SCTP_INP_WLOCK(inp);
2689                                 if (sctp_delete_sharedkey_ep(inp, scdel->scact_keynumber))
2690                                         error = EINVAL;
2691                                 SCTP_INP_WUNLOCK(inp);
2692                         }
2693                         break;
2694                 }
2695
2696         case SCTP_RESET_STREAMS:
2697                 {
2698                         struct sctp_stream_reset *strrst;
2699                         uint8_t send_in = 0, send_tsn = 0, send_out = 0;
2700                         int i;
2701
2702                         SCTP_CHECK_AND_CAST(strrst, optval, struct sctp_stream_reset, optsize);
2703                         SCTP_FIND_STCB(inp, stcb, strrst->strrst_assoc_id);
2704
2705                         if (stcb == NULL) {
2706                                 error = ENOENT;
2707                                 break;
2708                         }
2709                         if (stcb->asoc.peer_supports_strreset == 0) {
2710                                 /*
2711                                  * Peer does not support it, we return
2712                                  * protocol not supported since this is true
2713                                  * for this feature and this peer, not the
2714                                  * socket request in general.
2715                                  */
2716                                 error = EPROTONOSUPPORT;
2717                                 SCTP_TCB_UNLOCK(stcb);
2718                                 break;
2719                         }
2720                         if (stcb->asoc.stream_reset_outstanding) {
2721                                 error = EALREADY;
2722                                 SCTP_TCB_UNLOCK(stcb);
2723                                 break;
2724                         }
2725                         if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2726                                 send_in = 1;
2727                         } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2728                                 send_out = 1;
2729                         } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2730                                 send_in = 1;
2731                                 send_out = 1;
2732                         } else if (strrst->strrst_flags == SCTP_RESET_TSN) {
2733                                 send_tsn = 1;
2734                         } else {
2735                                 error = EINVAL;
2736                                 SCTP_TCB_UNLOCK(stcb);
2737                                 break;
2738                         }
2739                         for (i = 0; i < strrst->strrst_num_streams; i++) {
2740                                 if ((send_in) &&
2741
2742                                     (strrst->strrst_list[i] > stcb->asoc.streamincnt)) {
2743                                         error = EINVAL;
2744                                         goto get_out;
2745                                 }
2746                                 if ((send_out) &&
2747                                     (strrst->strrst_list[i] > stcb->asoc.streamoutcnt)) {
2748                                         error = EINVAL;
2749                                         goto get_out;
2750                                 }
2751                         }
2752                         if (error) {
2753                 get_out:
2754                                 SCTP_TCB_UNLOCK(stcb);
2755                                 break;
2756                         }
2757                         error = sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2758                             strrst->strrst_list,
2759                             send_out, (stcb->asoc.str_reset_seq_in - 3),
2760                             send_in, send_tsn);
2761
2762                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_REQ);
2763                         SCTP_TCB_UNLOCK(stcb);
2764                 }
2765                 break;
2766
2767         case SCTP_CONNECT_X:
2768                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2769                         error = EINVAL;
2770                         break;
2771                 }
2772                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 0);
2773                 break;
2774
2775         case SCTP_CONNECT_X_DELAYED:
2776                 if (optsize < (sizeof(int) + sizeof(struct sockaddr_in))) {
2777                         error = EINVAL;
2778                         break;
2779                 }
2780                 error = sctp_do_connect_x(so, inp, optval, optsize, p, 1);
2781                 break;
2782
2783         case SCTP_CONNECT_X_COMPLETE:
2784                 {
2785                         struct sockaddr *sa;
2786                         struct sctp_nets *net;
2787
2788                         /* FIXME MT: check correct? */
2789                         SCTP_CHECK_AND_CAST(sa, optval, struct sockaddr, optsize);
2790
2791                         /* find tcb */
2792                         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2793                                 SCTP_INP_RLOCK(inp);
2794                                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2795                                 if (stcb) {
2796                                         SCTP_TCB_LOCK(stcb);
2797                                         net = sctp_findnet(stcb, sa);
2798                                 }
2799                                 SCTP_INP_RUNLOCK(inp);
2800                         } else {
2801                                 /*
2802                                  * We increment here since
2803                                  * sctp_findassociation_ep_addr() wil do a
2804                                  * decrement if it finds the stcb as long as
2805                                  * the locked tcb (last argument) is NOT a
2806                                  * TCB.. aka NULL.
2807                                  */
2808                                 SCTP_INP_INCR_REF(inp);
2809                                 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2810                                 if (stcb == NULL) {
2811                                         SCTP_INP_DECR_REF(inp);
2812                                 }
2813                         }
2814
2815                         if (stcb == NULL) {
2816                                 error = ENOENT;
2817                                 break;
2818                         }
2819                         if (stcb->asoc.delayed_connection == 1) {
2820                                 stcb->asoc.delayed_connection = 0;
2821                                 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2822                                 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb,
2823                                     stcb->asoc.primary_destination,
2824                                     SCTP_FROM_SCTP_USRREQ + SCTP_LOC_9);
2825                                 sctp_send_initiate(inp, stcb);
2826                         } else {
2827                                 /*
2828                                  * already expired or did not use delayed
2829                                  * connectx
2830                                  */
2831                                 error = EALREADY;
2832                         }
2833                         SCTP_TCB_UNLOCK(stcb);
2834                 }
2835                 break;
2836         case SCTP_MAXBURST:
2837                 {
2838                         uint8_t *burst;
2839
2840                         SCTP_CHECK_AND_CAST(burst, optval, uint8_t, optsize);
2841
2842                         SCTP_INP_WLOCK(inp);
2843                         if (*burst) {
2844                                 inp->sctp_ep.max_burst = *burst;
2845                         }
2846                         SCTP_INP_WUNLOCK(inp);
2847                 }
2848                 break;
2849         case SCTP_MAXSEG:
2850                 {
2851                         struct sctp_assoc_value *av;
2852                         int ovh;
2853
2854                         SCTP_CHECK_AND_CAST(av, optval, struct sctp_assoc_value, optsize);
2855                         SCTP_FIND_STCB(inp, stcb, av->assoc_id);
2856
2857                         if (stcb) {
2858                                 error = EINVAL;
2859                                 SCTP_TCB_UNLOCK(stcb);
2860                         } else {
2861                                 SCTP_INP_WLOCK(inp);
2862                                 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2863                                         ovh = SCTP_MED_OVERHEAD;
2864                                 } else {
2865                                         ovh = SCTP_MED_V4_OVERHEAD;
2866                                 }
2867                                 /*
2868                                  * FIXME MT: I think this is not in tune
2869                                  * with the API ID
2870                                  */
2871                                 if (av->assoc_value) {
2872                                         inp->sctp_frag_point = (av->assoc_value + ovh);
2873                                 } else {
2874                                         error = EINVAL;
2875                                 }
2876                                 SCTP_INP_WUNLOCK(inp);
2877                         }
2878                 }
2879                 break;
2880         case SCTP_EVENTS:
2881                 {
2882                         struct sctp_event_subscribe *events;
2883
2884                         SCTP_CHECK_AND_CAST(events, optval, struct sctp_event_subscribe, optsize);
2885
2886                         SCTP_INP_WLOCK(inp);
2887                         if (events->sctp_data_io_event) {
2888                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2889                         } else {
2890                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
2891                         }
2892
2893                         if (events->sctp_association_event) {
2894                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2895                         } else {
2896                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVASSOCEVNT);
2897                         }
2898
2899                         if (events->sctp_address_event) {
2900                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2901                         } else {
2902                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPADDREVNT);
2903                         }
2904
2905                         if (events->sctp_send_failure_event) {
2906                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2907                         } else {
2908                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSENDFAILEVNT);
2909                         }
2910
2911                         if (events->sctp_peer_error_event) {
2912                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2913                         } else {
2914                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVPEERERR);
2915                         }
2916
2917                         if (events->sctp_shutdown_event) {
2918                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2919                         } else {
2920                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT);
2921                         }
2922
2923                         if (events->sctp_partial_delivery_event) {
2924                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2925                         } else {
2926                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_PDAPIEVNT);
2927                         }
2928
2929                         if (events->sctp_adaptation_layer_event) {
2930                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2931                         } else {
2932                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_ADAPTATIONEVNT);
2933                         }
2934
2935                         if (events->sctp_authentication_event) {
2936                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2937                         } else {
2938                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTHEVNT);
2939                         }
2940
2941                         if (events->sctp_stream_reset_events) {
2942                                 sctp_feature_on(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2943                         } else {
2944                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_STREAM_RESETEVNT);
2945                         }
2946                         SCTP_INP_WUNLOCK(inp);
2947                 }
2948                 break;
2949
2950         case SCTP_ADAPTATION_LAYER:
2951                 {
2952                         struct sctp_setadaptation *adap_bits;
2953
2954                         SCTP_CHECK_AND_CAST(adap_bits, optval, struct sctp_setadaptation, optsize);
2955                         SCTP_INP_WLOCK(inp);
2956                         inp->sctp_ep.adaptation_layer_indicator = adap_bits->ssb_adaptation_ind;
2957                         SCTP_INP_WUNLOCK(inp);
2958                 }
2959                 break;
2960 #ifdef SCTP_DEBUG
2961         case SCTP_SET_INITIAL_DBG_SEQ:
2962                 {
2963                         uint32_t *vvv;
2964
2965                         SCTP_CHECK_AND_CAST(vvv, optval, uint32_t, optsize);
2966                         SCTP_INP_WLOCK(inp);
2967                         inp->sctp_ep.initial_sequence_debug = *vvv;
2968                         SCTP_INP_WUNLOCK(inp);
2969                 }
2970                 break;
2971 #endif
2972         case SCTP_DEFAULT_SEND_PARAM:
2973                 {
2974                         struct sctp_sndrcvinfo *s_info;
2975
2976                         SCTP_CHECK_AND_CAST(s_info, optval, struct sctp_sndrcvinfo, optsize);
2977                         SCTP_FIND_STCB(inp, stcb, s_info->sinfo_assoc_id);
2978
2979                         if (stcb) {
2980                                 if (s_info->sinfo_stream <= stcb->asoc.streamoutcnt) {
2981                                         stcb->asoc.def_send = *s_info;
2982                                 } else {
2983                                         error = EINVAL;
2984                                 }
2985                                 SCTP_TCB_UNLOCK(stcb);
2986                         } else {
2987                                 SCTP_INP_WLOCK(inp);
2988                                 inp->def_send = *s_info;
2989                                 SCTP_INP_WUNLOCK(inp);
2990                         }
2991                 }
2992                 break;
2993         case SCTP_PEER_ADDR_PARAMS:
2994                 /* Applys to the specific association */
2995                 {
2996                         struct sctp_paddrparams *paddrp;
2997                         struct sctp_nets *net;
2998
2999                         SCTP_CHECK_AND_CAST(paddrp, optval, struct sctp_paddrparams, optsize);
3000                         SCTP_FIND_STCB(inp, stcb, paddrp->spp_assoc_id);
3001                         net = NULL;
3002                         if (stcb) {
3003                                 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3004                         } else {
3005                                 /*
3006                                  * We increment here since
3007                                  * sctp_findassociation_ep_addr() wil do a
3008                                  * decrement if it finds the stcb as long as
3009                                  * the locked tcb (last argument) is NOT a
3010                                  * TCB.. aka NULL.
3011                                  */
3012                                 SCTP_INP_INCR_REF(inp);
3013                                 stcb = sctp_findassociation_ep_addr(&inp,
3014                                     (struct sockaddr *)&paddrp->spp_address,
3015                                     &net, NULL, NULL);
3016                                 if (stcb == NULL) {
3017                                         SCTP_INP_DECR_REF(inp);
3018                                 }
3019                         }
3020
3021
3022                         if (stcb) {
3023                                 /************************TCB SPECIFIC SET ******************/
3024                                 /*
3025                                  * do we change the timer for HB, we run
3026                                  * only one?
3027                                  */
3028                                 if (paddrp->spp_hbinterval)
3029                                         stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3030                                 else if (paddrp->spp_flags & SPP_HB_TIME_IS_ZERO)
3031                                         stcb->asoc.heart_beat_delay = 0;
3032
3033                                 /* network sets ? */
3034                                 if (net) {
3035                                         /************************NET SPECIFIC SET ******************/
3036                                         if (paddrp->spp_flags & SPP_HB_DEMAND) {
3037                                                 /* on demand HB */
3038                                                 (void)sctp_send_hb(stcb, 1, net);
3039                                         }
3040                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
3041                                                 net->dest_state |= SCTP_ADDR_NOHB;
3042                                         }
3043                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
3044                                                 net->dest_state &= ~SCTP_ADDR_NOHB;
3045                                         }
3046                                         if (paddrp->spp_flags & SPP_PMTUD_DISABLE) {
3047                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3048                                                         sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
3049                                                             SCTP_FROM_SCTP_USRREQ + SCTP_LOC_10);
3050                                                 }
3051                                                 if (paddrp->spp_pathmtu > SCTP_DEFAULT_MINSEGMENT) {
3052                                                         net->mtu = paddrp->spp_pathmtu;
3053                                                         if (net->mtu < stcb->asoc.smallest_mtu) {
3054 #ifdef SCTP_PRINT_FOR_B_AND_M
3055                                                                 printf("SCTP_PMTU_DISABLE calls sctp_pathmtu_adjustment:%d\n",
3056                                                                     net->mtu);
3057 #endif
3058                                                                 sctp_pathmtu_adjustment(inp, stcb, net, net->mtu);
3059                                                         }
3060                                                 }
3061                                         }
3062                                         if (paddrp->spp_flags & SPP_PMTUD_ENABLE) {
3063                                                 if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
3064                                                         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
3065                                                 }
3066                                         }
3067                                         if (paddrp->spp_pathmaxrxt)
3068                                                 net->failure_threshold = paddrp->spp_pathmaxrxt;
3069 #ifdef INET
3070                                         if (paddrp->spp_flags & SPP_IPV4_TOS) {
3071                                                 if (net->ro._l_addr.sin.sin_family == AF_INET) {
3072                                                         net->tos_flowlabel = paddrp->spp_ipv4_tos & 0x000000fc;
3073                                                 }
3074                                         }
3075 #endif
3076 #ifdef INET6
3077                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL) {
3078                                                 if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
3079                                                         net->tos_flowlabel = paddrp->spp_ipv6_flowlabel;
3080                                                 }
3081                                         }
3082 #endif
3083                                 } else {
3084                                         /************************ASSOC ONLY -- NO NET SPECIFIC SET ******************/
3085                                         if (paddrp->spp_pathmaxrxt)
3086                                                 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3087
3088                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
3089                                                 /* Turn back on the timer */
3090                                                 stcb->asoc.hb_is_disabled = 0;
3091                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3092                                         }
3093                                         if (paddrp->spp_flags & SPP_HB_DISABLE) {
3094                                                 int cnt_of_unconf = 0;
3095                                                 struct sctp_nets *lnet;
3096
3097                                                 stcb->asoc.hb_is_disabled = 1;
3098                                                 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3099                                                         if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3100                                                                 cnt_of_unconf++;
3101                                                         }
3102                                                 }
3103                                                 /*
3104                                                  * stop the timer ONLY if we
3105                                                  * have no unconfirmed
3106                                                  * addresses
3107                                                  */
3108                                                 if (cnt_of_unconf == 0) {
3109                                                         sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, SCTP_FROM_SCTP_USRREQ + SCTP_LOC_11);
3110                                                 }
3111                                         }
3112                                         if (paddrp->spp_flags & SPP_HB_ENABLE) {
3113                                                 /* start up the timer. */
3114                                                 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3115                                         }
3116 #ifdef INET
3117                                         if (paddrp->spp_flags & SPP_IPV4_TOS)
3118                                                 stcb->asoc.default_tos = paddrp->spp_ipv4_tos & 0x000000fc;
3119 #endif
3120 #ifdef INET6
3121                                         if (paddrp->spp_flags & SPP_IPV6_FLOWLABEL)
3122                                                 stcb->asoc.default_flowlabel = paddrp->spp_ipv6_flowlabel;
3123 #endif
3124
3125                                 }
3126                                 SCTP_TCB_UNLOCK(stcb);
3127                         } else {
3128                                 /************************NO TCB, SET TO default stuff ******************/
3129                                 SCTP_INP_WLOCK(inp);
3130                                 /*
3131                                  * For the TOS/FLOWLABEL stuff you set it
3132                                  * with the options on the socket
3133                                  */
3134                                 if (paddrp->spp_pathmaxrxt) {
3135                                         inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3136                                 }
3137                                 if (paddrp->spp_flags & SPP_HB_ENABLE) {
3138                                         inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(paddrp->spp_hbinterval);
3139                                         sctp_feature_off(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3140                                 } else if (paddrp->spp_flags & SPP_HB_DISABLE) {
3141                                         sctp_feature_on(inp, SCTP_PCB_FLAGS_DONOT_HEARTBEAT);
3142                                 }
3143                                 SCTP_INP_WUNLOCK(inp);
3144                         }
3145                 }
3146                 break;
3147         case SCTP_RTOINFO:
3148                 {
3149                         struct sctp_rtoinfo *srto;
3150
3151                         SCTP_CHECK_AND_CAST(srto, optval, struct sctp_rtoinfo, optsize);
3152                         SCTP_FIND_STCB(inp, stcb, srto->srto_assoc_id);
3153
3154                         if (stcb) {
3155                                 /* Set in ms we hope :-) */
3156                                 if (srto->srto_initial)
3157                                         stcb->asoc.initial_rto = srto->srto_initial;
3158                                 if (srto->srto_max)
3159                                         stcb->asoc.maxrto = srto->srto_max;
3160                                 if (srto->srto_min)
3161                                         stcb->asoc.minrto = srto->srto_min;
3162                                 SCTP_TCB_UNLOCK(stcb);
3163                         } else {
3164                                 SCTP_INP_WLOCK(inp);
3165                                 /*
3166                                  * If we have a null asoc, its default for
3167                                  * the endpoint
3168                                  */
3169                                 if (srto->srto_initial)
3170                                         inp->sctp_ep.initial_rto = srto->srto_initial;
3171                                 if (srto->srto_max)
3172                                         inp->sctp_ep.sctp_maxrto = srto->srto_max;
3173                                 if (srto->srto_min)
3174                                         inp->sctp_ep.sctp_minrto = srto->srto_min;
3175                                 SCTP_INP_WUNLOCK(inp);
3176                         }
3177                 }
3178                 break;
3179         case SCTP_ASSOCINFO:
3180                 {
3181                         struct sctp_assocparams *sasoc;
3182
3183                         SCTP_CHECK_AND_CAST(sasoc, optval, struct sctp_assocparams, optsize);
3184                         SCTP_FIND_STCB(inp, stcb, sasoc->sasoc_assoc_id);
3185
3186                         if (stcb) {
3187                                 if (sasoc->sasoc_asocmaxrxt)
3188                                         stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3189                                 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3190                                 sasoc->sasoc_peer_rwnd = 0;
3191                                 sasoc->sasoc_local_rwnd = 0;
3192                                 if (stcb->asoc.cookie_life)
3193                                         stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3194                                 stcb->asoc.delayed_ack = sasoc->sasoc_sack_delay;
3195                                 if (sasoc->sasoc_sack_freq) {
3196                                         stcb->asoc.sack_freq = sasoc->sasoc_sack_freq;
3197                                 }
3198                                 SCTP_TCB_UNLOCK(stcb);
3199                         } else {
3200                                 SCTP_INP_WLOCK(inp);
3201                                 if (sasoc->sasoc_asocmaxrxt)
3202                                         inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3203                                 sasoc->sasoc_number_peer_destinations = 0;
3204                                 sasoc->sasoc_peer_rwnd = 0;
3205                                 sasoc->sasoc_local_rwnd = 0;
3206                                 if (sasoc->sasoc_cookie_life)
3207                                         inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3208                                 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sasoc->sasoc_sack_delay);
3209                                 if (sasoc->sasoc_sack_freq) {
3210                                         inp->sctp_ep.sctp_sack_freq = sasoc->sasoc_sack_freq;
3211                                 }
3212                                 SCTP_INP_WUNLOCK(inp);
3213                         }
3214                 }
3215                 break;
3216         case SCTP_INITMSG:
3217                 {
3218                         struct sctp_initmsg *sinit;
3219
3220                         SCTP_CHECK_AND_CAST(sinit, optval, struct sctp_initmsg, optsize);
3221                         SCTP_INP_WLOCK(inp);
3222                         if (sinit->sinit_num_ostreams)
3223                                 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3224
3225                         if (sinit->sinit_max_instreams)
3226                                 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3227
3228                         if (sinit->sinit_max_attempts)
3229                                 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3230
3231                         if (sinit->sinit_max_init_timeo)
3232                                 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3233                         SCTP_INP_WUNLOCK(inp);
3234                 }
3235                 break;
3236         case SCTP_PRIMARY_ADDR:
3237                 {
3238                         struct sctp_setprim *spa;
3239                         struct sctp_nets *net, *lnet;
3240
3241                         SCTP_CHECK_AND_CAST(spa, optval, struct sctp_setprim, optsize);
3242                         SCTP_FIND_STCB(inp, stcb, spa->ssp_assoc_id);
3243
3244                         net = NULL;
3245                         if (stcb) {
3246                                 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3247                         } else {
3248                                 /*
3249                                  * We increment here since
3250                                  * sctp_findassociation_ep_addr() wil do a
3251                                  * decrement if it finds the stcb as long as
3252                                  * the locked tcb (last argument) is NOT a
3253                                  * TCB.. aka NULL.
3254                                  */
3255                                 SCTP_INP_INCR_REF(inp);
3256                                 stcb = sctp_findassociation_ep_addr(&inp,
3257                                     (struct sockaddr *)&spa->ssp_addr,
3258                                     &net, NULL, NULL);
3259                                 if (stcb == NULL) {
3260                                         SCTP_INP_DECR_REF(inp);
3261                                 }
3262                         }
3263
3264                         if ((stcb) && (net)) {
3265                                 if ((net != stcb->asoc.primary_destination) &&
3266                                     (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3267                                         /* Ok we need to set it */
3268                                         lnet = stcb->asoc.primary_destination;
3269                                         if (sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net) == 0) {
3270                                                 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3271                                                         net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3272                                                 }
3273                                                 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3274                                         }
3275                                 }
3276                         } else {
3277                                 error = EINVAL;
3278                         }
3279                         if (stcb) {
3280                                 SCTP_TCB_UNLOCK(stcb);
3281                         }
3282                 }
3283                 break;
3284         case SCTP_SET_DYNAMIC_PRIMARY:
3285                 {
3286                         union sctp_sockstore *ss;
3287
3288                         error = priv_check_cred(curthread->td_ucred,
3289                             PRIV_NETINET_RESERVEDPORT,
3290                             SUSER_ALLOWJAIL);
3291                         if (error)
3292                                 break;
3293
3294                         SCTP_CHECK_AND_CAST(ss, optval, union sctp_sockstore, optsize);
3295                         /* SUPER USER CHECK? */
3296                         error = sctp_dynamic_set_primary(&ss->sa, vrf_id);
3297                 }
3298                 break;
3299         case SCTP_SET_PEER_PRIMARY_ADDR:
3300                 {
3301                         struct sctp_setpeerprim *sspp;
3302
3303                         SCTP_CHECK_AND_CAST(sspp, optval, struct sctp_setpeerprim, optsize);
3304                         SCTP_FIND_STCB(inp, stcb, sspp->sspp_assoc_id);
3305                         if (stcb != NULL) {
3306                                 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3307                                         error = EINVAL;
3308                                 }
3309                                 SCTP_TCB_UNLOCK(stcb);
3310                         } else {
3311                                 error = EINVAL;
3312                         }
3313
3314                 }
3315                 break;
3316         case SCTP_BINDX_ADD_ADDR:
3317                 {
3318                         struct sctp_getaddresses *addrs;
3319                         struct sockaddr *addr_touse;
3320                         struct sockaddr_in sin;
3321
3322                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3323
3324                         /* see if we're bound all already! */
3325                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3326                                 error = EINVAL;
3327                                 break;
3328                         }
3329                         /* Is the VRF one we have */
3330                         addr_touse = addrs->addr;
3331 #if defined(INET6)
3332                         if (addrs->addr->sa_family == AF_INET6) {
3333                                 struct sockaddr_in6 *sin6;
3334
3335                                 sin6 = (struct sockaddr_in6 *)addr_touse;
3336                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3337                                         in6_sin6_2_sin(&sin, sin6);
3338                                         addr_touse = (struct sockaddr *)&sin;
3339                                 }
3340                         }
3341 #endif
3342                         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3343                                 if (p == NULL) {
3344                                         /* Can't get proc for Net/Open BSD */
3345                                         error = EINVAL;
3346                                         break;
3347                                 }
3348                                 error = sctp_inpcb_bind(so, addr_touse, p);
3349                                 break;
3350                         }
3351                         /*
3352                          * No locks required here since bind and mgmt_ep_sa
3353                          * all do their own locking. If we do something for
3354                          * the FIX: below we may need to lock in that case.
3355                          */
3356                         if (addrs->sget_assoc_id == 0) {
3357                                 /* add the address */
3358                                 struct sctp_inpcb *lep;
3359
3360                                 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3361                                 lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
3362                                 if (lep != NULL) {
3363                                         /*
3364                                          * We must decrement the refcount
3365                                          * since we have the ep already and
3366                                          * are binding. No remove going on
3367                                          * here.
3368                                          */
3369                                         SCTP_INP_DECR_REF(inp);
3370                                 }
3371                                 if (lep == inp) {
3372                                         /* already bound to it.. ok */
3373                                         break;
3374                                 } else if (lep == NULL) {
3375                                         ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3376                                         error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3377                                             SCTP_ADD_IP_ADDRESS, vrf_id);
3378                                 } else {
3379                                         error = EADDRNOTAVAIL;
3380                                 }
3381                                 if (error)
3382                                         break;
3383
3384                         } else {
3385                                 /*
3386                                  * FIX: decide whether we allow assoc based
3387                                  * bindx
3388                                  */
3389                         }
3390                 }
3391                 break;
3392         case SCTP_BINDX_REM_ADDR:
3393                 {
3394                         struct sctp_getaddresses *addrs;
3395                         struct sockaddr *addr_touse;
3396                         struct sockaddr_in sin;
3397
3398                         SCTP_CHECK_AND_CAST(addrs, optval, struct sctp_getaddresses, optsize);
3399                         /* see if we're bound all already! */
3400                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3401                                 error = EINVAL;
3402                                 break;
3403                         }
3404                         addr_touse = addrs->addr;
3405 #if defined(INET6)
3406                         if (addrs->addr->sa_family == AF_INET6) {
3407                                 struct sockaddr_in6 *sin6;
3408
3409                                 sin6 = (struct sockaddr_in6 *)addr_touse;
3410                                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3411                                         in6_sin6_2_sin(&sin, sin6);
3412                                         addr_touse = (struct sockaddr *)&sin;
3413                                 }
3414                         }
3415 #endif
3416                         /*
3417                          * No lock required mgmt_ep_sa does its own locking.
3418                          * If the FIX: below is ever changed we may need to
3419                          * lock before calling association level binding.
3420                          */
3421                         if (addrs->sget_assoc_id == 0) {
3422                                 /* delete the address */
3423                                 sctp_addr_mgmt_ep_sa(inp, addr_touse,
3424                                     SCTP_DEL_IP_ADDRESS, vrf_id);
3425                         } else {
3426                                 /*
3427                                  * FIX: decide whether we allow assoc based
3428                                  * bindx
3429                                  */
3430                         }
3431                 }
3432                 break;
3433         default:
3434                 error = ENOPROTOOPT;
3435                 break;
3436         }                       /* end switch (opt) */
3437         return (error);
3438 }
3439
3440
3441 int
3442 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3443 {
3444         void *optval = NULL;
3445         size_t optsize = 0;
3446         struct sctp_inpcb *inp;
3447         void *p;
3448         int error = 0;
3449
3450         inp = (struct sctp_inpcb *)so->so_pcb;
3451         if (inp == 0) {
3452                 /* I made the same as TCP since we are not setup? */
3453                 return (ECONNRESET);
3454         }
3455         if (sopt->sopt_level != IPPROTO_SCTP) {
3456                 /* wrong proto level... send back up to IP */
3457 #ifdef INET6
3458                 if (INP_CHECK_SOCKAF(so, AF_INET6))
3459                         error = ip6_ctloutput(so, sopt);
3460                 else
3461 #endif                          /* INET6 */
3462                         error = ip_ctloutput(so, sopt);
3463                 return (error);
3464         }
3465         optsize = sopt->sopt_valsize;
3466         if (optsize) {
3467                 SCTP_MALLOC(optval, void *, optsize, "SCTPSockOpt");
3468                 if (optval == NULL) {
3469                         return (ENOBUFS);
3470                 }
3471                 error = sooptcopyin(sopt, optval, optsize, optsize);
3472                 if (error) {
3473                         SCTP_FREE(optval);
3474                         goto out;
3475                 }
3476         }
3477         p = (void *)sopt->sopt_td;
3478         if (sopt->sopt_dir == SOPT_SET) {
3479                 error = sctp_setopt(so, sopt->sopt_name, optval, optsize, p);
3480         } else if (sopt->sopt_dir == SOPT_GET) {
3481                 error = sctp_getopt(so, sopt->sopt_name, optval, &optsize, p);
3482         } else {
3483                 error = EINVAL;
3484         }
3485         if ((error == 0) && (optval != NULL)) {
3486                 error = sooptcopyout(sopt, optval, optsize);
3487                 SCTP_FREE(optval);
3488         } else if (optval != NULL) {
3489                 SCTP_FREE(optval);
3490         }
3491 out:
3492         return (error);
3493 }
3494
3495
3496 static int
3497 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3498 {
3499         int error = 0;
3500         int create_lock_on = 0;
3501         uint32_t vrf_id;
3502         struct sctp_inpcb *inp;
3503         struct sctp_tcb *stcb = NULL;
3504
3505         inp = (struct sctp_inpcb *)so->so_pcb;
3506         if (inp == 0) {
3507                 /* I made the same as TCP since we are not setup? */
3508                 return (ECONNRESET);
3509         }
3510         SCTP_ASOC_CREATE_LOCK(inp);
3511         create_lock_on = 1;
3512
3513         SCTP_INP_INCR_REF(inp);
3514         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3515             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3516                 /* Should I really unlock ? */
3517                 error = EFAULT;
3518                 goto out_now;
3519         }
3520 #ifdef INET6
3521         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3522             (addr->sa_family == AF_INET6)) {
3523                 error = EINVAL;
3524                 goto out_now;
3525         }
3526 #endif                          /* INET6 */
3527         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3528             SCTP_PCB_FLAGS_UNBOUND) {
3529                 /* Bind a ephemeral port */
3530                 error = sctp_inpcb_bind(so, NULL, p);
3531                 if (error) {
3532                         goto out_now;
3533                 }
3534         }
3535         /* Now do we connect? */
3536         if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3537                 error = EINVAL;
3538                 goto out_now;
3539         }
3540         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3541             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3542                 /* We are already connected AND the TCP model */
3543                 error = EADDRINUSE;
3544                 goto out_now;
3545         }
3546         if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3547                 SCTP_INP_RLOCK(inp);
3548                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3549                 SCTP_INP_RUNLOCK(inp);
3550         } else {
3551                 /*
3552                  * We increment here since sctp_findassociation_ep_addr()
3553                  * wil do a decrement if it finds the stcb as long as the
3554                  * locked tcb (last argument) is NOT a TCB.. aka NULL.
3555                  */
3556                 SCTP_INP_INCR_REF(inp);
3557                 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3558                 if (stcb == NULL) {
3559                         SCTP_INP_DECR_REF(inp);
3560                 } else {
3561                         SCTP_TCB_LOCK(stcb);
3562                 }
3563         }
3564         if (stcb != NULL) {
3565                 /* Already have or am bring up an association */
3566                 error = EALREADY;
3567                 goto out_now;
3568         }
3569         vrf_id = inp->def_vrf_id;
3570         /* We are GOOD to go */
3571         stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id);
3572         if (stcb == NULL) {
3573                 /* Gak! no memory */
3574                 goto out_now;
3575         }
3576         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3577                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3578                 /* Set the connected flag so we can queue data */
3579                 soisconnecting(so);
3580         }
3581         stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3582         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3583
3584         /* initialize authentication parameters for the assoc */
3585         sctp_initialize_auth_params(inp, stcb);
3586
3587         sctp_send_initiate(inp, stcb);
3588         SCTP_TCB_UNLOCK(stcb);
3589 out_now:
3590         if (create_lock_on)
3591                 SCTP_ASOC_CREATE_UNLOCK(inp);
3592
3593         SCTP_INP_DECR_REF(inp);
3594         return error;
3595 }
3596
3597 int
3598 sctp_listen(struct socket *so, int backlog, struct thread *p)
3599 {
3600         /*
3601          * Note this module depends on the protocol processing being called
3602          * AFTER any socket level flags and backlog are applied to the
3603          * socket. The traditional way that the socket flags are applied is
3604          * AFTER protocol processing. We have made a change to the
3605          * sys/kern/uipc_socket.c module to reverse this but this MUST be in
3606          * place if the socket API for SCTP is to work properly.
3607          */
3608
3609         int error = 0;
3610         struct sctp_inpcb *inp;
3611
3612         inp = (struct sctp_inpcb *)so->so_pcb;
3613         if (inp == 0) {
3614                 /* I made the same as TCP since we are not setup? */
3615                 return (ECONNRESET);
3616         }
3617         SCTP_INP_RLOCK(inp);
3618 #ifdef SCTP_LOCK_LOGGING
3619         sctp_log_lock(inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_SOCK);
3620 #endif
3621         SOCK_LOCK(so);
3622         error = solisten_proto_check(so);
3623         if (error) {
3624                 SOCK_UNLOCK(so);
3625                 SCTP_INP_RUNLOCK(inp);
3626                 return (error);
3627         }
3628         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3629             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3630                 /* We are already connected AND the TCP model */
3631                 SCTP_INP_RUNLOCK(inp);
3632                 SOCK_UNLOCK(so);
3633                 return (EADDRINUSE);
3634         }
3635         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3636                 /* We must do a bind. */
3637                 SOCK_UNLOCK(so);
3638                 SCTP_INP_RUNLOCK(inp);
3639                 if ((error = sctp_inpcb_bind(so, NULL, p))) {
3640                         /* bind error, probably perm */
3641                         return (error);
3642                 }
3643                 SOCK_LOCK(so);
3644         } else {
3645                 SCTP_INP_RUNLOCK(inp);
3646         }
3647         /* It appears for 7.0 and on, we must always call this. */
3648         solisten_proto(so, backlog);
3649         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3650                 /* remove the ACCEPTCONN flag for one-to-many sockets */
3651                 so->so_options &= ~SO_ACCEPTCONN;
3652         }
3653         if (backlog == 0) {
3654                 /* turning off listen */
3655                 so->so_options &= ~SO_ACCEPTCONN;
3656         }
3657         SOCK_UNLOCK(so);
3658         return (error);
3659 }
3660
3661 static int sctp_defered_wakeup_cnt = 0;
3662
3663 int
3664 sctp_accept(struct socket *so, struct sockaddr **addr)
3665 {
3666         struct sctp_tcb *stcb;
3667         struct sctp_inpcb *inp;
3668         union sctp_sockstore store;
3669
3670         int error;
3671
3672         inp = (struct sctp_inpcb *)so->so_pcb;
3673
3674         if (inp == 0) {
3675                 return (ECONNRESET);
3676         }
3677         SCTP_INP_RLOCK(inp);
3678         if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3679                 SCTP_INP_RUNLOCK(inp);
3680                 return (ENOTSUP);
3681         }
3682         if (so->so_state & SS_ISDISCONNECTED) {
3683                 SCTP_INP_RUNLOCK(inp);
3684                 return (ECONNABORTED);
3685         }
3686         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3687         if (stcb == NULL) {
3688                 SCTP_INP_RUNLOCK(inp);
3689                 return (ECONNRESET);
3690         }
3691         SCTP_TCB_LOCK(stcb);
3692         SCTP_INP_RUNLOCK(inp);
3693         store = stcb->asoc.primary_destination->ro._l_addr;
3694         SCTP_TCB_UNLOCK(stcb);
3695         if (store.sa.sa_family == AF_INET) {
3696                 struct sockaddr_in *sin;
3697
3698                 SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3699                 sin->sin_family = AF_INET;
3700                 sin->sin_len = sizeof(*sin);
3701                 sin->sin_port = ((struct sockaddr_in *)&store)->sin_port;
3702                 sin->sin_addr = ((struct sockaddr_in *)&store)->sin_addr;
3703                 *addr = (struct sockaddr *)sin;
3704         } else {
3705                 struct sockaddr_in6 *sin6;
3706
3707                 SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
3708                 sin6->sin6_family = AF_INET6;
3709                 sin6->sin6_len = sizeof(*sin6);
3710                 sin6->sin6_port = ((struct sockaddr_in6 *)&store)->sin6_port;
3711
3712                 sin6->sin6_addr = ((struct sockaddr_in6 *)&store)->sin6_addr;
3713                 if ((error = sa6_recoverscope(sin6)) != 0) {
3714                         SCTP_FREE_SONAME(sin6);
3715                         return (error);
3716                 }
3717                 *addr = (struct sockaddr *)sin6;
3718         }
3719         /* Wake any delayed sleep action */
3720         if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3721                 SCTP_INP_WLOCK(inp);
3722                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3723                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3724                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3725                         SCTP_INP_WUNLOCK(inp);
3726                         SOCKBUF_LOCK(&inp->sctp_socket->so_snd);
3727                         if (sowriteable(inp->sctp_socket)) {
3728                                 sowwakeup_locked(inp->sctp_socket);
3729                         } else {
3730                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_snd);
3731                         }
3732                         SCTP_INP_WLOCK(inp);
3733                 }
3734                 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3735                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3736                         SCTP_INP_WUNLOCK(inp);
3737                         SOCKBUF_LOCK(&inp->sctp_socket->so_rcv);
3738                         if (soreadable(inp->sctp_socket)) {
3739                                 sctp_defered_wakeup_cnt++;
3740                                 sorwakeup_locked(inp->sctp_socket);
3741                         } else {
3742                                 SOCKBUF_UNLOCK(&inp->sctp_socket->so_rcv);
3743                         }
3744                         SCTP_INP_WLOCK(inp);
3745                 }
3746                 SCTP_INP_WUNLOCK(inp);
3747         }
3748         return (0);
3749 }
3750
3751 int
3752 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
3753 {
3754         struct sockaddr_in *sin;
3755         uint32_t vrf_id;
3756         struct sctp_inpcb *inp;
3757         struct sctp_ifa *sctp_ifa;
3758
3759         /*
3760          * Do the malloc first in case it blocks.
3761          */
3762         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3763         sin->sin_family = AF_INET;
3764         sin->sin_len = sizeof(*sin);
3765         inp = (struct sctp_inpcb *)so->so_pcb;
3766         if (!inp) {
3767                 SCTP_FREE_SONAME(sin);
3768                 return ECONNRESET;
3769         }
3770         SCTP_INP_RLOCK(inp);
3771         sin->sin_port = inp->sctp_lport;
3772         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3773                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3774                         struct sctp_tcb *stcb;
3775                         struct sockaddr_in *sin_a;
3776                         struct sctp_nets *net;
3777                         int fnd;
3778
3779                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3780                         if (stcb == NULL) {
3781                                 goto notConn;
3782                         }
3783                         fnd = 0;
3784                         sin_a = NULL;
3785                         SCTP_TCB_LOCK(stcb);
3786                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3787                                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3788                                 if (sin_a == NULL)
3789                                         /* this will make coverity happy */
3790                                         continue;
3791
3792                                 if (sin_a->sin_family == AF_INET) {
3793                                         fnd = 1;
3794                                         break;
3795                                 }
3796                         }
3797                         if ((!fnd) || (sin_a == NULL)) {
3798                                 /* punt */
3799                                 SCTP_TCB_UNLOCK(stcb);
3800                                 goto notConn;
3801                         }
3802                         vrf_id = inp->def_vrf_id;
3803                         sctp_ifa = sctp_source_address_selection(inp,
3804                             stcb,
3805                             (sctp_route_t *) & net->ro,
3806                             net, 0, vrf_id);
3807                         if (sctp_ifa) {
3808                                 sin->sin_addr = sctp_ifa->address.sin.sin_addr;
3809                                 sctp_free_ifa(sctp_ifa);
3810                         }
3811                         SCTP_TCB_UNLOCK(stcb);
3812                 } else {
3813                         /* For the bound all case you get back 0 */
3814         notConn:
3815                         sin->sin_addr.s_addr = 0;
3816                 }
3817
3818         } else {
3819                 /* Take the first IPv4 address in the list */
3820                 struct sctp_laddr *laddr;
3821                 int fnd = 0;
3822
3823                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3824                         if (laddr->ifa->address.sa.sa_family == AF_INET) {
3825                                 struct sockaddr_in *sin_a;
3826
3827                                 sin_a = (struct sockaddr_in *)&laddr->ifa->address.sa;
3828                                 sin->sin_addr = sin_a->sin_addr;
3829                                 fnd = 1;
3830                                 break;
3831                         }
3832                 }
3833                 if (!fnd) {
3834                         SCTP_FREE_SONAME(sin);
3835                         SCTP_INP_RUNLOCK(inp);
3836                         return ENOENT;
3837                 }
3838         }
3839         SCTP_INP_RUNLOCK(inp);
3840         (*addr) = (struct sockaddr *)sin;
3841         return (0);
3842 }
3843
3844 int
3845 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
3846 {
3847         struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
3848         int fnd;
3849         struct sockaddr_in *sin_a;
3850         struct sctp_inpcb *inp;
3851         struct sctp_tcb *stcb;
3852         struct sctp_nets *net;
3853
3854         /* Do the malloc first in case it blocks. */
3855         inp = (struct sctp_inpcb *)so->so_pcb;
3856         if ((inp == NULL) ||
3857             ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3858                 /* UDP type and listeners will drop out here */
3859                 return (ENOTCONN);
3860         }
3861         SCTP_MALLOC_SONAME(sin, struct sockaddr_in *, sizeof *sin);
3862         sin->sin_family = AF_INET;
3863         sin->sin_len = sizeof(*sin);
3864
3865         /* We must recapture incase we blocked */
3866         inp = (struct sctp_inpcb *)so->so_pcb;
3867         if (!inp) {
3868                 SCTP_FREE_SONAME(sin);
3869                 return ECONNRESET;
3870         }
3871         SCTP_INP_RLOCK(inp);
3872         stcb = LIST_FIRST(&inp->sctp_asoc_list);
3873         if (stcb)
3874                 SCTP_TCB_LOCK(stcb);
3875         SCTP_INP_RUNLOCK(inp);
3876         if (stcb == NULL) {
3877                 SCTP_FREE_SONAME(sin);
3878                 return ECONNRESET;
3879         }
3880         fnd = 0;
3881         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3882                 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
3883                 if (sin_a->sin_family == AF_INET) {
3884                         fnd = 1;
3885                         sin->sin_port = stcb->rport;
3886                         sin->sin_addr = sin_a->sin_addr;
3887                         break;
3888                 }
3889         }
3890         SCTP_TCB_UNLOCK(stcb);
3891         if (!fnd) {
3892                 /* No IPv4 address */
3893                 SCTP_FREE_SONAME(sin);
3894                 return ENOENT;
3895         }
3896         (*addr) = (struct sockaddr *)sin;
3897         return (0);
3898 }
3899
3900 struct pr_usrreqs sctp_usrreqs = {
3901         .pru_abort = sctp_abort,
3902         .pru_accept = sctp_accept,
3903         .pru_attach = sctp_attach,
3904         .pru_bind = sctp_bind,
3905         .pru_connect = sctp_connect,
3906         .pru_control = in_control,
3907         .pru_close = sctp_close,
3908         .pru_detach = sctp_close,
3909         .pru_sopoll = sopoll_generic,
3910         .pru_disconnect = sctp_disconnect,
3911         .pru_listen = sctp_listen,
3912         .pru_peeraddr = sctp_peeraddr,
3913         .pru_send = sctp_sendm,
3914         .pru_shutdown = sctp_shutdown,
3915         .pru_sockaddr = sctp_ingetaddr,
3916         .pru_sosend = sctp_sosend,
3917         .pru_soreceive = sctp_soreceive
3918 };