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