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