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