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