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