1 /* $KAME: sctp_pcb.c,v 1.37 2004/08/17 06:28:02 t-momose Exp $ */
2 /* $DragonFly: src/sys/netinet/sctp_pcb.c,v 1.2 2005/07/15 15:02:02 eirikn Exp $ */
5 * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Cisco Systems, Inc.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if !(defined(__OpenBSD__) || defined(__APPLE__))
36 #include "opt_ipsec.h"
38 #if defined(__FreeBSD__) || defined(__DragonFly__)
39 #include "opt_compat.h"
40 #include "opt_inet6.h"
43 #if defined(__NetBSD__)
48 #elif !defined(__OpenBSD__)
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
61 #include <sys/kernel.h>
62 #include <sys/sysctl.h>
63 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
64 #include <sys/random.h>
66 #if defined(__NetBSD__)
69 #if defined(__OpenBSD__)
70 #include <dev/rndvar.h>
73 #if defined(__APPLE__)
74 #include <netinet/sctp_callout.h>
75 #elif defined(__OpenBSD__)
76 #include <sys/timeout.h>
78 #include <sys/callout.h>
81 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
82 #include <sys/limits.h>
84 #include <machine/limits.h>
86 #include <machine/cpu.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/scope6_var.h>
102 #if defined(__FreeBSD__) || (__NetBSD__) || defined(__DragonFly__)
103 #include <netinet6/in6_pcb.h>
104 #elif defined(__OpenBSD__)
105 #include <netinet/in_pcb.h>
111 #include <netinet6/ipsec.h>
112 #include <netkey/key.h>
118 #include <netinet/sctp_var.h>
119 #include <netinet/sctp_pcb.h>
120 #include <netinet/sctputil.h>
121 #include <netinet/sctp.h>
122 #include <netinet/sctp_header.h>
123 #include <netinet/sctp_asconf.h>
124 #include <netinet/sctp_output.h>
125 #include <netinet/sctp_timer.h>
127 #ifndef SCTP_PCBHASHSIZE
128 /* default number of association hash buckets in each endpoint */
129 #define SCTP_PCBHASHSIZE 256
133 u_int32_t sctp_debug_on = 0;
134 #endif /* SCTP_DEBUG */
136 u_int32_t sctp_pegs[SCTP_NUMBER_OF_PEGS];
138 int sctp_pcbtblsize = SCTP_PCBHASHSIZE;
140 struct sctp_epinfo sctppcbinfo;
142 /* FIX: we don't handle multiple link local scopes */
143 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
145 SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b)
147 struct in6_addr tmp_a, tmp_b;
148 /* use a copy of a and b */
151 in6_clearscope(&tmp_a);
152 in6_clearscope(&tmp_b);
153 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
157 extern int ipport_firstauto;
158 extern int ipport_lastauto;
159 extern int ipport_hifirstauto;
160 extern int ipport_hilastauto;
163 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
166 void sctp_validate_no_locks(void);
169 SCTP_INP_RLOCK(struct sctp_inpcb *inp)
171 struct sctp_tcb *stcb;
172 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
173 if (mtx_owned(&(stcb)->tcb_mtx))
174 panic("I own TCB lock?");
176 if (mtx_owned(&(inp)->inp_mtx))
177 panic("INP Recursive Lock-R");
178 mtx_lock(&(inp)->inp_mtx);
182 SCTP_INP_WLOCK(struct sctp_inpcb *inp)
188 SCTP_INP_INFO_RLOCK()
190 struct sctp_inpcb *inp;
191 struct sctp_tcb *stcb;
192 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
193 if (mtx_owned(&(inp)->inp_mtx))
194 panic("info-lock and own inp lock?");
195 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
196 if (mtx_owned(&(stcb)->tcb_mtx))
197 panic("Info lock and own a tcb lock?");
200 if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
201 panic("INP INFO Recursive Lock-R");
202 mtx_lock(&sctppcbinfo.ipi_ep_mtx);
206 SCTP_INP_INFO_WLOCK()
208 SCTP_INP_INFO_RLOCK();
212 void sctp_validate_no_locks()
214 struct sctp_inpcb *inp;
215 struct sctp_tcb *stcb;
217 if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
218 panic("INP INFO lock is owned?");
220 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
221 if (mtx_owned(&(inp)->inp_mtx))
222 panic("You own an INP lock?");
223 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
224 if (mtx_owned(&(stcb)->tcb_mtx))
225 panic("You own a TCB lock?");
234 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
236 /* We really don't need
237 * to lock this, but I will
238 * just because it does not hurt.
240 SCTP_INP_INFO_RLOCK();
241 spcb->ep_count = sctppcbinfo.ipi_count_ep;
242 spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
243 spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
244 spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
245 spcb->chk_count = sctppcbinfo.ipi_count_chunk;
246 spcb->sockq_count = sctppcbinfo.ipi_count_sockq;
247 spcb->mbuf_track = sctppcbinfo.mbuf_track;
248 SCTP_INP_INFO_RUNLOCK();
253 * Notes on locks for FreeBSD 5 and up. All association
254 * lookups that have a definte ep, the INP structure is
255 * assumed to be locked for reading. If we need to go
256 * find the INP (ususally when a **inp is passed) then
257 * we must lock the INFO structure first and if needed
258 * lock the INP too. Note that if we lock it we must
264 * Given a endpoint, look and find in its association list any association
265 * with the "to" address given. This can be a "from" address, too, for
266 * inbound packets. For outbound packets it is a true "to" address.
268 static struct sctp_tcb *
269 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
270 struct sockaddr *to, struct sctp_nets **netp)
272 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
275 * Note for this module care must be taken when observing what to is
276 * for. In most of the rest of the code the TO field represents my
277 * peer and the FROM field represents my address. For this module it
278 * is reversed of that.
281 * If we support the TCP model, then we must now dig through to
282 * see if we can find our endpoint in the list of tcp ep's.
284 uint16_t lport, rport;
285 struct sctppcbhead *ephead;
286 struct sctp_inpcb *inp;
287 struct sctp_laddr *laddr;
288 struct sctp_tcb *stcb;
289 struct sctp_nets *net;
291 if ((to == NULL) || (from == NULL)) {
295 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
296 lport = ((struct sockaddr_in *)to)->sin_port;
297 rport = ((struct sockaddr_in *)from)->sin_port;
298 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
299 lport = ((struct sockaddr_in6 *)to)->sin6_port;
300 rport = ((struct sockaddr_in6 *)from)->sin6_port;
304 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
305 (lport + rport), sctppcbinfo.hashtcpmark)];
307 * Ok now for each of the guys in this bucket we must look
309 * - Does the remote port match.
310 * - Does there single association's addresses match this
312 * If so we update p_ep to point to this ep and return the
315 LIST_FOREACH(inp, ephead, sctp_hash) {
316 if (lport != inp->sctp_lport) {
320 /* check to see if the ep has one of the addresses */
321 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
322 /* We are NOT bound all, so look further */
325 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
326 if (laddr->ifa == NULL) {
328 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
329 printf("An ounce of prevention is worth a pound of cure\n");
334 if (laddr->ifa->ifa_addr == NULL) {
336 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
337 printf("ifa with a NULL address\n");
342 if (laddr->ifa->ifa_addr->sa_family ==
344 /* see if it matches */
345 struct sockaddr_in *intf_addr, *sin;
346 intf_addr = (struct sockaddr_in *)
347 laddr->ifa->ifa_addr;
348 sin = (struct sockaddr_in *)to;
349 if (from->sa_family == AF_INET) {
350 if (sin->sin_addr.s_addr ==
351 intf_addr->sin_addr.s_addr) {
353 SCTP_INP_RUNLOCK(inp);
357 struct sockaddr_in6 *intf_addr6;
358 struct sockaddr_in6 *sin6;
359 sin6 = (struct sockaddr_in6 *)
361 intf_addr6 = (struct sockaddr_in6 *)
362 laddr->ifa->ifa_addr;
364 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
365 &intf_addr6->sin6_addr)) {
367 SCTP_INP_RUNLOCK(inp);
374 /* This endpoint does not have this address */
375 SCTP_INP_RUNLOCK(inp);
380 * Ok if we hit here the ep has the address, does it hold the
384 stcb = LIST_FIRST(&inp->sctp_asoc_list);
386 SCTP_INP_RUNLOCK(inp);
390 if (stcb->rport != rport) {
391 /* remote port does not match. */
392 SCTP_TCB_UNLOCK(stcb);
393 SCTP_INP_RUNLOCK(inp);
396 /* Does this TCB have a matching address? */
397 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
398 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
399 /* not the same family, can't be a match */
402 if (from->sa_family == AF_INET) {
403 struct sockaddr_in *sin, *rsin;
404 sin = (struct sockaddr_in *)&net->ro._l_addr;
405 rsin = (struct sockaddr_in *)from;
406 if (sin->sin_addr.s_addr ==
407 rsin->sin_addr.s_addr) {
412 /* Update the endpoint pointer */
414 SCTP_INP_RUNLOCK(inp);
418 struct sockaddr_in6 *sin6, *rsin6;
419 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
420 rsin6 = (struct sockaddr_in6 *)from;
421 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
422 &rsin6->sin6_addr)) {
427 /* Update the endpoint pointer */
429 SCTP_INP_RUNLOCK(inp);
434 SCTP_TCB_UNLOCK(stcb);
436 SCTP_INP_RUNLOCK(inp);
442 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
443 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
445 struct sctp_tcb *stcb;
446 struct sockaddr_in *sin;
447 struct sockaddr_in6 *sin6;
448 struct sockaddr_storage local_store, remote_store;
450 struct sctp_paramhdr parm_buf, *phdr;
453 memset(&local_store, 0, sizeof(local_store));
454 memset(&remote_store, 0, sizeof(remote_store));
456 /* First get the destination address setup too. */
457 iph = mtod(m, struct ip *);
458 if (iph->ip_v == IPVERSION) {
460 sin = (struct sockaddr_in *)&local_store;
461 sin->sin_family = AF_INET;
462 sin->sin_len = sizeof(*sin);
463 sin->sin_port = sh->dest_port;
464 sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
465 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
468 ip6 = mtod(m, struct ip6_hdr *);
469 sin6 = (struct sockaddr_in6 *)&local_store;
470 sin6->sin6_family = AF_INET6;
471 sin6->sin6_len = sizeof(*sin6);
472 sin6->sin6_port = sh->dest_port;
473 sin6->sin6_addr = ip6->ip6_dst;
478 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
479 &parm_buf, sizeof(struct sctp_paramhdr));
482 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
483 printf("sctp_process_control: failed to get asconf lookup addr\n");
485 #endif /* SCTP_DEBUG */
488 ptype = (int)((u_int)ntohs(phdr->param_type));
489 /* get the correlation address */
490 if (ptype == SCTP_IPV6_ADDRESS) {
491 /* ipv6 address param */
492 struct sctp_ipv6addr_param *p6, p6_buf;
493 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
497 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
498 offset + sizeof(struct sctp_asconf_chunk),
499 &p6_buf.ph, sizeof(*p6));
502 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
503 printf("sctp_process_control: failed to get asconf v6 lookup addr\n");
505 #endif /* SCTP_DEBUG */
508 sin6 = (struct sockaddr_in6 *)&remote_store;
509 sin6->sin6_family = AF_INET6;
510 sin6->sin6_len = sizeof(*sin6);
511 sin6->sin6_port = sh->src_port;
512 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
513 } else if (ptype == SCTP_IPV4_ADDRESS) {
514 /* ipv4 address param */
515 struct sctp_ipv4addr_param *p4, p4_buf;
516 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
520 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
521 offset + sizeof(struct sctp_asconf_chunk),
522 &p4_buf.ph, sizeof(*p4));
525 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
526 printf("sctp_process_control: failed to get asconf v4 lookup addr\n");
528 #endif /* SCTP_DEBUG */
531 sin = (struct sockaddr_in *)&remote_store;
532 sin->sin_family = AF_INET;
533 sin->sin_len = sizeof(*sin);
534 sin->sin_port = sh->src_port;
535 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
537 /* invalid address param type */
541 stcb = sctp_findassociation_ep_addr(inp_p,
542 (struct sockaddr *)&remote_store, netp,
543 (struct sockaddr *)&local_store, NULL);
548 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
549 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
551 struct sctpasochead *head;
552 struct sctp_inpcb *inp;
553 struct sctp_tcb *stcb;
554 struct sctp_nets *net;
558 if (remote->sa_family == AF_INET) {
559 rport = (((struct sockaddr_in *)remote)->sin_port);
560 } else if (remote->sa_family == AF_INET6) {
561 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
566 /* UN-lock so we can do proper locking here
567 * this occurs when called from load_addresses_from_init.
569 SCTP_TCB_UNLOCK(locked_tcb);
571 SCTP_INP_INFO_RLOCK();
572 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
574 * Now either this guy is our listner or it's the connector.
575 * If it is the one that issued the connect, then it's only
576 * chance is to be the first TCB in the list. If it is the
577 * acceptor, then do the special_lookup to hash and find the
580 if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
581 /* to is peer addr, from is my addr */
582 stcb = sctp_tcb_special_locate(inp_p, remote, local,
584 if ((stcb != NULL) && (locked_tcb == NULL)){
585 /* we have a locked tcb, lower refcount */
587 SCTP_INP_DECR_REF(inp);
588 SCTP_INP_WUNLOCK(inp);
590 if (locked_tcb != NULL) {
591 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
592 SCTP_TCB_LOCK(locked_tcb);
593 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
595 SCTP_TCB_UNLOCK(stcb);
597 SCTP_INP_INFO_RUNLOCK();
601 stcb = LIST_FIRST(&inp->sctp_asoc_list);
606 if (stcb->rport != rport) {
607 /* remote port does not match. */
608 SCTP_TCB_UNLOCK(stcb);
611 /* now look at the list of remote addresses */
612 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
613 if (net->ro._l_addr.sa.sa_family !=
615 /* not the same family */
618 if (remote->sa_family == AF_INET) {
619 struct sockaddr_in *sin, *rsin;
620 sin = (struct sockaddr_in *)
622 rsin = (struct sockaddr_in *)remote;
623 if (sin->sin_addr.s_addr ==
624 rsin->sin_addr.s_addr) {
629 if (locked_tcb == NULL) {
630 SCTP_INP_DECR_REF(inp);
632 SCTP_INP_WUNLOCK(inp);
633 SCTP_INP_INFO_RUNLOCK();
636 } else if (remote->sa_family == AF_INET6) {
637 struct sockaddr_in6 *sin6, *rsin6;
638 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
639 rsin6 = (struct sockaddr_in6 *)remote;
640 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
641 &rsin6->sin6_addr)) {
646 if (locked_tcb == NULL) {
647 SCTP_INP_DECR_REF(inp);
649 SCTP_INP_WUNLOCK(inp);
650 SCTP_INP_INFO_RUNLOCK();
655 SCTP_TCB_UNLOCK(stcb);
659 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
660 inp->sctp_hashmark)];
664 LIST_FOREACH(stcb, head, sctp_tcbhash) {
665 if (stcb->rport != rport) {
666 /* remote port does not match */
669 /* now look at the list of remote addresses */
671 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
672 if (net->ro._l_addr.sa.sa_family !=
674 /* not the same family */
677 if (remote->sa_family == AF_INET) {
678 struct sockaddr_in *sin, *rsin;
679 sin = (struct sockaddr_in *)
681 rsin = (struct sockaddr_in *)remote;
682 if (sin->sin_addr.s_addr ==
683 rsin->sin_addr.s_addr) {
688 if (locked_tcb == NULL) {
689 SCTP_INP_DECR_REF(inp);
691 SCTP_INP_WUNLOCK(inp);
692 SCTP_INP_INFO_RUNLOCK();
695 } else if (remote->sa_family == AF_INET6) {
696 struct sockaddr_in6 *sin6, *rsin6;
697 sin6 = (struct sockaddr_in6 *)
699 rsin6 = (struct sockaddr_in6 *)remote;
700 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
701 &rsin6->sin6_addr)) {
706 if (locked_tcb == NULL) {
707 SCTP_INP_DECR_REF(inp);
709 SCTP_INP_WUNLOCK(inp);
710 SCTP_INP_INFO_RUNLOCK();
715 SCTP_TCB_UNLOCK(stcb);
719 /* clean up for returning null */
721 if (locked_tcb->sctp_ep != inp) {
722 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
723 SCTP_TCB_LOCK(locked_tcb);
724 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
726 SCTP_TCB_LOCK(locked_tcb);
728 SCTP_INP_WUNLOCK(inp);
729 SCTP_INP_INFO_RUNLOCK();
735 * Find an association for a specific endpoint using the association id
736 * given out in the COMM_UP notification
739 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, caddr_t asoc_id)
742 * Use my the assoc_id to find a endpoint
744 struct sctpasochead *head;
745 struct sctp_tcb *stcb;
748 if (asoc_id == 0 || inp == NULL) {
751 SCTP_INP_INFO_RLOCK();
752 vtag = (u_int32_t)asoc_id;
753 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
754 sctppcbinfo.hashasocmark)];
757 SCTP_INP_INFO_RUNLOCK();
760 LIST_FOREACH(stcb, head, sctp_asocs) {
761 SCTP_INP_RLOCK(stcb->sctp_ep);
763 SCTP_INP_RUNLOCK(stcb->sctp_ep);
764 if (stcb->asoc.my_vtag == vtag) {
766 if (inp != stcb->sctp_ep) {
767 /* some other guy has the
768 * same vtag active (vtag collision).
770 sctp_pegs[SCTP_VTAG_BOGUS]++;
771 SCTP_TCB_UNLOCK(stcb);
774 sctp_pegs[SCTP_VTAG_EXPR]++;
775 SCTP_INP_INFO_RUNLOCK();
778 SCTP_TCB_UNLOCK(stcb);
780 SCTP_INP_INFO_RUNLOCK();
784 static struct sctp_inpcb *
785 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
788 struct sctp_inpcb *inp;
789 struct sockaddr_in *sin;
790 struct sockaddr_in6 *sin6;
791 struct sctp_laddr *laddr;
793 /* Endpoing probe expects
794 * that the INP_INFO is locked.
796 if (nam->sa_family == AF_INET) {
797 sin = (struct sockaddr_in *)nam;
799 } else if (nam->sa_family == AF_INET6) {
800 sin6 = (struct sockaddr_in6 *)nam;
803 /* unsupported family */
809 LIST_FOREACH(inp, head, sctp_hash) {
812 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
813 (inp->sctp_lport == lport)) {
815 if ((nam->sa_family == AF_INET) &&
816 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
817 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
818 (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
820 #if defined(__OpenBSD__)
821 (0) /* For open bsd we do dual bind only */
823 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
827 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
828 SCTP_INP_RUNLOCK(inp);
831 /* A V6 address and the endpoint is NOT bound V6 */
832 if (nam->sa_family == AF_INET6 &&
833 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
834 SCTP_INP_RUNLOCK(inp);
837 SCTP_INP_RUNLOCK(inp);
840 SCTP_INP_RUNLOCK(inp);
843 if ((nam->sa_family == AF_INET) &&
844 (sin->sin_addr.s_addr == INADDR_ANY)) {
845 /* Can't hunt for one that has no address specified */
847 } else if ((nam->sa_family == AF_INET6) &&
848 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
849 /* Can't hunt for one that has no address specified */
853 * ok, not bound to all so see if we can find a EP bound to this
857 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
858 printf("Ok, there is NO bound-all available for port:%x\n", ntohs(lport));
861 LIST_FOREACH(inp, head, sctp_hash) {
863 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
864 SCTP_INP_RUNLOCK(inp);
868 * Ok this could be a likely candidate, look at all of
871 if (inp->sctp_lport != lport) {
872 SCTP_INP_RUNLOCK(inp);
876 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
877 printf("Ok, found maching local port\n");
880 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
881 if (laddr->ifa == NULL) {
883 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
884 printf("An ounce of prevention is worth a pound of cure\n");
890 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
891 printf("Ok laddr->ifa:%p is possible, ",
895 if (laddr->ifa->ifa_addr == NULL) {
897 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
898 printf("Huh IFA as an ifa_addr=NULL, ");
904 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
905 printf("Ok laddr->ifa:%p is possible, ",
906 laddr->ifa->ifa_addr);
907 sctp_print_address(laddr->ifa->ifa_addr);
908 printf("looking for ");
909 sctp_print_address(nam);
912 if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
913 /* possible, see if it matches */
914 struct sockaddr_in *intf_addr;
915 intf_addr = (struct sockaddr_in *)
916 laddr->ifa->ifa_addr;
917 if (nam->sa_family == AF_INET) {
918 if (sin->sin_addr.s_addr ==
919 intf_addr->sin_addr.s_addr) {
921 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
922 printf("YES, return ep:%p\n", inp);
925 SCTP_INP_RUNLOCK(inp);
928 } else if (nam->sa_family == AF_INET6) {
929 struct sockaddr_in6 *intf_addr6;
930 intf_addr6 = (struct sockaddr_in6 *)
931 laddr->ifa->ifa_addr;
932 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
933 &intf_addr6->sin6_addr)) {
935 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
936 printf("YES, return ep:%p\n", inp);
939 SCTP_INP_RUNLOCK(inp);
944 SCTP_INP_RUNLOCK(inp);
948 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
949 printf("NO, Falls out to NULL\n");
957 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
960 * First we check the hash table to see if someone has this port
961 * bound with just the port.
963 struct sctp_inpcb *inp;
964 struct sctppcbhead *head;
965 struct sockaddr_in *sin;
966 struct sockaddr_in6 *sin6;
969 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
970 printf("Looking for endpoint %d :",
971 ntohs(((struct sockaddr_in *)nam)->sin_port));
972 sctp_print_address(nam);
975 if (nam->sa_family == AF_INET) {
976 sin = (struct sockaddr_in *)nam;
977 lport = ((struct sockaddr_in *)nam)->sin_port;
978 } else if (nam->sa_family == AF_INET6) {
979 sin6 = (struct sockaddr_in6 *)nam;
980 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
982 /* unsupported family */
986 * I could cheat here and just cast to one of the types but we will
987 * do it right. It also provides the check against an Unsupported
990 /* Find the head of the ALLADDR chain */
992 SCTP_INP_INFO_RLOCK();
993 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
994 sctppcbinfo.hashmark)];
996 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
997 printf("Main hash to lookup at head:%p\n", head);
1000 inp = sctp_endpoint_probe(nam, head, lport);
1003 * If the TCP model exists it could be that the main listening
1004 * endpoint is gone but there exists a connected socket for this
1005 * guy yet. If so we can return the first one that we find. This
1006 * may NOT be the correct one but the sctp_findassociation_ep_addr
1007 * has further code to look at all TCP models.
1009 if (inp == NULL && find_tcp_pool) {
1012 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1013 printf("EP was NULL and TCP model is supported\n");
1016 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
1018 * This is real gross, but we do NOT have a remote
1019 * port at this point depending on who is calling. We
1020 * must therefore look for ANY one that matches our
1023 head = &sctppcbinfo.sctp_tcpephash[i];
1024 if (LIST_FIRST(head)) {
1025 inp = sctp_endpoint_probe(nam, head, lport);
1034 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1035 printf("EP to return is %p\n", inp);
1038 if (have_lock == 0) {
1040 SCTP_INP_WLOCK(inp);
1041 SCTP_INP_INCR_REF(inp);
1042 SCTP_INP_WUNLOCK(inp);
1044 SCTP_INP_INFO_RUNLOCK();
1047 SCTP_INP_WLOCK(inp);
1048 SCTP_INP_INCR_REF(inp);
1049 SCTP_INP_WUNLOCK(inp);
1056 * Find an association for an endpoint with the pointer to whom you want
1057 * to send to and the endpoint pointer. The address can be IPv4 or IPv6.
1058 * We may need to change the *to to some other struct like a mbuf...
1061 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1062 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
1064 struct sctp_inpcb *inp;
1065 struct sctp_tcb *retval;
1067 SCTP_INP_INFO_RLOCK();
1068 if (find_tcp_pool) {
1069 if (inp_p != NULL) {
1070 retval = sctp_tcb_special_locate(inp_p, from, to, netp);
1072 retval = sctp_tcb_special_locate(&inp, from, to, netp);
1074 if (retval != NULL) {
1075 SCTP_INP_INFO_RUNLOCK();
1079 inp = sctp_pcb_findep(to, 0, 1);
1080 if (inp_p != NULL) {
1083 SCTP_INP_INFO_RUNLOCK();
1090 * ok, we have an endpoint, now lets find the assoc for it (if any)
1091 * we now place the source address or from in the to of the find
1092 * endpoint call. Since in reality this chain is used from the
1093 * inbound packet side.
1095 if (inp_p != NULL) {
1096 return (sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL));
1098 return (sctp_findassociation_ep_addr(&inp, from, netp, to, NULL));
1104 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1105 * find all addresses that the sender has specified in any address list.
1106 * Each address will be used to lookup the TCB and see if one exits.
1108 static struct sctp_tcb *
1109 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1110 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1111 struct sockaddr *dest)
1113 struct sockaddr_in sin4;
1114 struct sockaddr_in6 sin6;
1115 struct sctp_paramhdr *phdr, parm_buf;
1116 struct sctp_tcb *retval;
1117 u_int32_t ptype, plen;
1119 memset(&sin4, 0, sizeof(sin4));
1120 memset(&sin6, 0, sizeof(sin6));
1121 sin4.sin_len = sizeof(sin4);
1122 sin4.sin_family = AF_INET;
1123 sin4.sin_port = sh->src_port;
1124 sin6.sin6_len = sizeof(sin6);
1125 sin6.sin6_family = AF_INET6;
1126 sin6.sin6_port = sh->src_port;
1129 offset += sizeof(struct sctp_init_chunk);
1131 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1132 while (phdr != NULL) {
1133 /* now we must see if we want the parameter */
1134 ptype = ntohs(phdr->param_type);
1135 plen = ntohs(phdr->param_length);
1138 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1139 printf("sctp_findassociation_special_addr: Impossible length in parameter\n");
1141 #endif /* SCTP_DEBUG */
1144 if (ptype == SCTP_IPV4_ADDRESS &&
1145 plen == sizeof(struct sctp_ipv4addr_param)) {
1146 /* Get the rest of the address */
1147 struct sctp_ipv4addr_param ip4_parm, *p4;
1149 phdr = sctp_get_next_param(m, offset,
1150 (struct sctp_paramhdr *)&ip4_parm, plen);
1154 p4 = (struct sctp_ipv4addr_param *)phdr;
1155 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1157 retval = sctp_findassociation_ep_addr(inp_p,
1158 (struct sockaddr *)&sin4, netp, dest, NULL);
1159 if (retval != NULL) {
1162 } else if (ptype == SCTP_IPV6_ADDRESS &&
1163 plen == sizeof(struct sctp_ipv6addr_param)) {
1164 /* Get the rest of the address */
1165 struct sctp_ipv6addr_param ip6_parm, *p6;
1167 phdr = sctp_get_next_param(m, offset,
1168 (struct sctp_paramhdr *)&ip6_parm, plen);
1172 p6 = (struct sctp_ipv6addr_param *)phdr;
1173 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1175 retval = sctp_findassociation_ep_addr(inp_p,
1176 (struct sockaddr *)&sin6, netp, dest, NULL);
1177 if (retval != NULL) {
1181 offset += SCTP_SIZE32(plen);
1182 phdr = sctp_get_next_param(m, offset, &parm_buf,
1188 static struct sctp_tcb *
1189 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1190 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1194 * Use my vtag to hash. If we find it we then verify the source addr
1195 * is in the assoc. If all goes well we save a bit on rec of a packet.
1197 struct sctpasochead *head;
1198 struct sctp_nets *net;
1199 struct sctp_tcb *stcb;
1201 SCTP_INP_INFO_RLOCK();
1202 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1203 sctppcbinfo.hashasocmark)];
1206 SCTP_INP_INFO_RUNLOCK();
1209 LIST_FOREACH(stcb, head, sctp_asocs) {
1210 SCTP_INP_RLOCK(stcb->sctp_ep);
1211 SCTP_TCB_LOCK(stcb);
1212 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1213 if (stcb->asoc.my_vtag == vtag) {
1215 if (stcb->rport != rport) {
1217 * we could remove this if vtags are unique
1218 * across the system.
1220 SCTP_TCB_UNLOCK(stcb);
1223 if (stcb->sctp_ep->sctp_lport != lport) {
1225 * we could remove this if vtags are unique
1226 * across the system.
1228 SCTP_TCB_UNLOCK(stcb);
1231 net = sctp_findnet(stcb, from);
1235 sctp_pegs[SCTP_VTAG_EXPR]++;
1236 *inp_p = stcb->sctp_ep;
1237 SCTP_INP_INFO_RUNLOCK();
1240 /* not him, this should only
1241 * happen in rare cases so
1244 sctp_pegs[SCTP_VTAG_BOGUS]++;
1247 SCTP_TCB_UNLOCK(stcb);
1249 SCTP_INP_INFO_RUNLOCK();
1254 * Find an association with the pointer to the inbound IP packet. This
1255 * can be a IPv4 or IPv6 packet.
1258 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1259 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1260 struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1264 struct sctp_tcb *retval;
1265 struct sockaddr_storage to_store, from_store;
1266 struct sockaddr *to = (struct sockaddr *)&to_store;
1267 struct sockaddr *from = (struct sockaddr *)&from_store;
1268 struct sctp_inpcb *inp;
1271 iph = mtod(m, struct ip *);
1272 if (iph->ip_v == IPVERSION) {
1274 struct sockaddr_in *to4, *from4;
1276 to4 = (struct sockaddr_in *)&to_store;
1277 from4 = (struct sockaddr_in *)&from_store;
1278 bzero(to4, sizeof(*to4));
1279 bzero(from4, sizeof(*from4));
1280 from4->sin_family = to4->sin_family = AF_INET;
1281 from4->sin_len = to4->sin_len = sizeof(struct sockaddr_in);
1282 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1283 to4->sin_addr.s_addr = iph->ip_dst.s_addr ;
1284 from4->sin_port = sh->src_port;
1285 to4->sin_port = sh->dest_port;
1286 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1288 struct ip6_hdr *ip6;
1289 struct sockaddr_in6 *to6, *from6;
1291 ip6 = mtod(m, struct ip6_hdr *);
1292 to6 = (struct sockaddr_in6 *)&to_store;
1293 from6 = (struct sockaddr_in6 *)&from_store;
1294 bzero(to6, sizeof(*to6));
1295 bzero(from6, sizeof(*from6));
1296 from6->sin6_family = to6->sin6_family = AF_INET6;
1297 from6->sin6_len = to6->sin6_len = sizeof(struct sockaddr_in6);
1298 to6->sin6_addr = ip6->ip6_dst;
1299 from6->sin6_addr = ip6->ip6_src;
1300 from6->sin6_port = sh->src_port;
1301 to6->sin6_port = sh->dest_port;
1302 /* Get the scopes in properly to the sin6 addr's */
1303 (void)in6_recoverscope(to6, &to6->sin6_addr, NULL);
1304 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
1305 (void)in6_embedscope(&to6->sin6_addr, to6, NULL, NULL);
1307 (void)in6_embedscope(&to6->sin6_addr, to6);
1310 (void)in6_recoverscope(from6, &from6->sin6_addr, NULL);
1311 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
1312 (void)in6_embedscope(&from6->sin6_addr, from6, NULL, NULL);
1314 (void)in6_embedscope(&from6->sin6_addr, from6);
1317 /* Currently not supported. */
1321 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1322 printf("Looking for port %d address :",
1323 ntohs(((struct sockaddr_in *)to)->sin_port));
1324 sctp_print_address(to);
1325 printf("From for port %d address :",
1326 ntohs(((struct sockaddr_in *)from)->sin_port));
1327 sctp_print_address(from);
1332 /* we only go down this path if vtag is non-zero */
1333 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1334 inp_p, netp, sh->src_port, sh->dest_port);
1340 if ((ch->chunk_type != SCTP_INITIATION) &&
1341 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1342 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1343 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1344 /* Other chunk types go to the tcp pool. */
1348 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1352 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1356 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1357 printf("retval:%p inp:%p\n", retval, inp);
1360 if (retval == NULL && inp) {
1361 /* Found a EP but not this address */
1363 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1364 printf("Found endpoint %p but no asoc - ep state:%x\n",
1365 inp, inp->sctp_flags);
1368 if ((ch->chunk_type == SCTP_INITIATION) ||
1369 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1371 * special hook, we do NOT return linp or an
1372 * association that is linked to an existing
1373 * association that is under the TCP pool (i.e. no
1374 * listener exists). The endpoint finding routine
1375 * will always find a listner before examining the
1378 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1380 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1381 printf("Gak, its in the TCP pool... return NULL");
1390 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1391 printf("Now doing SPECIAL find\n");
1394 retval = sctp_findassociation_special_addr(m, iphlen,
1395 offset, sh, inp_p, netp, to);
1399 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1400 printf("retval is %p\n", retval);
1406 extern int sctp_max_burst_default;
1408 extern unsigned int sctp_delayed_sack_time_default;
1409 extern unsigned int sctp_heartbeat_interval_default;
1410 extern unsigned int sctp_pmtu_raise_time_default;
1411 extern unsigned int sctp_shutdown_guard_time_default;
1412 extern unsigned int sctp_secret_lifetime_default;
1414 extern unsigned int sctp_rto_max_default;
1415 extern unsigned int sctp_rto_min_default;
1416 extern unsigned int sctp_rto_initial_default;
1417 extern unsigned int sctp_init_rto_max_default;
1418 extern unsigned int sctp_valid_cookie_life_default;
1419 extern unsigned int sctp_init_rtx_max_default;
1420 extern unsigned int sctp_assoc_rtx_max_default;
1421 extern unsigned int sctp_path_rtx_max_default;
1422 extern unsigned int sctp_nr_outgoing_streams_default;
1425 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1426 * addresses. This way if we don't get a bind we by default pick a ephemeral
1427 * port with all addresses bound.
1430 sctp_inpcb_alloc(struct socket *so)
1433 * we get called when a new endpoint starts up. We need to allocate
1434 * the sctp_inpcb structure from the zone and init it. Mark it as
1435 * unbound and find a port that we can use as an ephemeral with
1436 * INADDR_ANY. If the user binds later no problem we can then add
1437 * in the specific addresses. And setup the default parameters for
1441 struct sctp_inpcb *inp, *n_inp;
1443 struct timeval time;
1449 * This code audits the entire INP list to see if
1450 * any ep's that are in the GONE state are now
1451 * all free. This should not happen really since when
1452 * the last association if freed we should end up deleting
1453 * the inpcb. This code including the locks should
1454 * be taken out ... since the last set of fixes I
1455 * have not seen the "Found a GONE on list" has not
1456 * came out. But i am paranoid and we will leave this
1457 * in at the cost of efficency on allocation of PCB's.
1458 * Probably we should move this to the invariant
1461 /* #ifdef INVARIANTS*/
1462 SCTP_INP_INFO_RLOCK();
1463 inp = LIST_FIRST(&sctppcbinfo.listhead);
1465 n_inp = LIST_NEXT(inp, sctp_list);
1466 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1467 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
1468 /* finish the job now */
1469 printf("Found a GONE on list\n");
1470 SCTP_INP_INFO_RUNLOCK();
1471 sctp_inpcb_free(inp, 1);
1472 SCTP_INP_INFO_RLOCK();
1477 SCTP_INP_INFO_RUNLOCK();
1478 /* #endif INVARIANTS*/
1480 SCTP_INP_INFO_WLOCK();
1481 inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
1483 printf("Out of SCTP-INPCB structures - no resources\n");
1484 SCTP_INP_INFO_WUNLOCK();
1489 bzero(inp, sizeof(*inp));
1491 /* bump generations */
1492 inp->ip_inp.inp.inp_socket = so;
1494 /* setup socket pointers */
1495 inp->sctp_socket = so;
1497 /* setup inpcb socket too */
1498 inp->ip_inp.inp.inp_socket = so;
1499 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1501 #if !(defined(__OpenBSD__) || defined(__APPLE__))
1503 struct inpcbpolicy *pcb_sp = NULL;
1504 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1505 /* Arrange to share the policy */
1506 inp->ip_inp.inp.inp_sp = pcb_sp;
1507 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1510 /* not sure what to do for openbsd here */
1514 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1515 SCTP_INP_INFO_WUNLOCK();
1519 sctppcbinfo.ipi_count_ep++;
1520 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1521 inp->ip_inp.inp.inp_gencnt = ++sctppcbinfo.ipi_gencnt_ep;
1522 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1524 inp->inp_ip_ttl = ip_defttl;
1525 inp->inp_ip_tos = 0;
1528 so->so_pcb = (caddr_t)inp;
1530 if ((so->so_type == SOCK_DGRAM) ||
1531 (so->so_type == SOCK_SEQPACKET)) {
1532 /* UDP style socket */
1533 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1534 SCTP_PCB_FLAGS_UNBOUND);
1535 inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1536 /* Be sure it is NON-BLOCKING IO for UDP */
1537 /*so->so_state |= SS_NBIO;*/
1538 } else if (so->so_type == SOCK_STREAM) {
1539 /* TCP style socket */
1540 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1541 SCTP_PCB_FLAGS_UNBOUND);
1542 inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1543 /* Be sure we have blocking IO bu default */
1544 so->so_state &= ~SS_NBIO;
1547 * unsupported socket type (RAW, etc)- in case we missed
1550 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1551 SCTP_INP_INFO_WUNLOCK();
1552 return (EOPNOTSUPP);
1554 inp->sctp_tcbhash = hashinit(sctp_pcbtblsize,
1559 #if defined(__NetBSD__) || defined(__OpenBSD__)
1562 &inp->sctp_hashmark);
1563 if (inp->sctp_tcbhash == NULL) {
1564 printf("Out of SCTP-INPCB->hashinit - no resources\n");
1565 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1566 SCTP_INP_INFO_WUNLOCK();
1570 SCTP_INP_LOCK_INIT(inp);
1571 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1572 /* lock the new ep */
1573 SCTP_INP_WLOCK(inp);
1575 /* add it to the info area */
1576 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1577 SCTP_INP_INFO_WUNLOCK();
1579 LIST_INIT(&inp->sctp_addr_list);
1580 LIST_INIT(&inp->sctp_asoc_list);
1581 TAILQ_INIT(&inp->sctp_queue_list);
1582 /* Init the timer structure for signature change */
1583 #if defined (__FreeBSD__) && __FreeBSD_version >= 500000
1584 callout_init(&inp->sctp_ep.signature_change.timer, 0);
1586 callout_init(&inp->sctp_ep.signature_change.timer);
1588 printf("signature_change.timer has been initiated!\n");
1589 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1591 /* now init the actual endpoint default data */
1594 /* setup the base timeout information */
1595 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1596 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1597 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1598 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_heartbeat_interval_default; /* this is in MSEC */
1599 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1600 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1601 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1602 /* all max/min max are in ms */
1603 m->sctp_maxrto = sctp_rto_max_default;
1604 m->sctp_minrto = sctp_rto_min_default;
1605 m->initial_rto = sctp_rto_initial_default;
1606 m->initial_init_rto_max = sctp_init_rto_max_default;
1608 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1610 m->max_init_times = sctp_init_rtx_max_default;
1611 m->max_send_times = sctp_assoc_rtx_max_default;
1612 m->def_net_failure = sctp_path_rtx_max_default;
1613 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1614 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1615 m->max_burst = sctp_max_burst_default;
1616 /* number of streams to pre-open on a association */
1617 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1619 /* Add adaption cookie */
1620 m->adaption_layer_indicator = 0x504C5253;
1622 /* seed random number generator */
1623 m->random_counter = 1;
1624 m->store_at = SCTP_SIGNATURE_SIZE;
1625 #if (defined(__FreeBSD__) && (__FreeBSD_version < 500000)) || defined(__DragonFly__)
1626 read_random_unlimited(m->random_numbers, sizeof(m->random_numbers));
1627 #elif defined(__APPLE__) || (__FreeBSD_version > 500000)
1628 read_random(m->random_numbers, sizeof(m->random_numbers));
1629 #elif defined(__OpenBSD__)
1630 get_random_bytes(m->random_numbers, sizeof(m->random_numbers));
1631 #elif defined(__NetBSD__) && NRND > 0
1632 rnd_extract_data(m->random_numbers, sizeof(m->random_numbers),
1636 u_int32_t *ranm, *ranp;
1637 ranp = (u_int32_t *)&m->random_numbers;
1638 ranm = ranp + (SCTP_SIGNATURE_ALOC_SIZE/sizeof(u_int32_t));
1639 if ((u_long)ranp % 4) {
1640 /* not a even boundary? */
1641 ranp = (u_int32_t *)SCTP_SIZE32((u_long)ranp);
1643 while (ranp < ranm) {
1649 sctp_fill_random_store(m);
1651 /* Minimum cookie size */
1652 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1653 sizeof(struct sctp_state_cookie);
1654 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1656 /* Setup the initial secret */
1657 SCTP_GETTIME_TIMEVAL(&time);
1658 m->time_of_secret_change = time.tv_sec;
1660 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1661 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1663 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1665 /* How long is a cookie good for ? */
1666 m->def_cookie_life = sctp_valid_cookie_life_default;
1667 SCTP_INP_WUNLOCK(inp);
1673 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1674 struct sctp_tcb *stcb)
1676 uint16_t lport, rport;
1677 struct sctppcbhead *head;
1678 struct sctp_laddr *laddr, *oladdr;
1680 SCTP_TCB_UNLOCK(stcb);
1681 SCTP_INP_INFO_WLOCK();
1682 SCTP_INP_WLOCK(old_inp);
1683 SCTP_INP_WLOCK(new_inp);
1684 SCTP_TCB_LOCK(stcb);
1686 new_inp->sctp_ep.time_of_secret_change =
1687 old_inp->sctp_ep.time_of_secret_change;
1688 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1689 sizeof(old_inp->sctp_ep.secret_key));
1690 new_inp->sctp_ep.current_secret_number =
1691 old_inp->sctp_ep.current_secret_number;
1692 new_inp->sctp_ep.last_secret_number =
1693 old_inp->sctp_ep.last_secret_number;
1694 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1696 /* Copy the port across */
1697 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1698 rport = stcb->rport;
1699 /* Pull the tcb from the old association */
1700 LIST_REMOVE(stcb, sctp_tcbhash);
1701 LIST_REMOVE(stcb, sctp_tcblist);
1703 /* Now insert the new_inp into the TCP connected hash */
1704 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1705 sctppcbinfo.hashtcpmark)];
1707 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1709 /* Now move the tcb into the endpoint list */
1710 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1712 * Question, do we even need to worry about the ep-hash since
1713 * we only have one connection? Probably not :> so lets
1714 * get rid of it and not suck up any kernel memory in that.
1716 SCTP_INP_INFO_WUNLOCK();
1717 stcb->sctp_socket = new_inp->sctp_socket;
1718 stcb->sctp_ep = new_inp;
1719 if (new_inp->sctp_tcbhash != NULL) {
1720 FREE(new_inp->sctp_tcbhash, M_PCB);
1721 new_inp->sctp_tcbhash = NULL;
1723 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1724 /* Subset bound, so copy in the laddr list from the old_inp */
1725 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1726 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
1727 sctppcbinfo.ipi_zone_laddr);
1728 if (laddr == NULL) {
1730 * Gak, what can we do? This assoc is really
1731 * HOSED. We probably should send an abort
1735 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1736 printf("Association hosed in TCP model, out of laddr memory\n");
1738 #endif /* SCTP_DEBUG */
1741 sctppcbinfo.ipi_count_laddr++;
1742 sctppcbinfo.ipi_gencnt_laddr++;
1743 bzero(laddr, sizeof(*laddr));
1744 laddr->ifa = oladdr->ifa;
1745 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1747 new_inp->laddr_count++;
1750 SCTP_INP_WUNLOCK(new_inp);
1751 SCTP_INP_WUNLOCK(old_inp);
1755 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
1757 struct sctppcbhead *head;
1758 struct sctp_inpcb *t_inp;
1760 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1761 sctppcbinfo.hashmark)];
1762 LIST_FOREACH(t_inp, head, sctp_hash) {
1763 if (t_inp->sctp_lport != lport) {
1766 /* This one is in use. */
1767 /* check the v6/v4 binding issue */
1768 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1769 #if defined(__FreeBSD__)
1770 (((struct inpcb *)t_inp)->inp_flags & IN6P_IPV6_V6ONLY)
1772 #if defined(__OpenBSD__)
1773 (0) /* For open bsd we do dual bind only */
1775 (((struct in6pcb *)t_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1779 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1780 /* collision in V6 space */
1783 /* inp is BOUND_V4 no conflict */
1786 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1787 /* t_inp is bound v4 and v6, conflict always */
1790 /* t_inp is bound only V4 */
1791 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1792 #if defined(__FreeBSD__)
1793 (((struct inpcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
1795 #if defined(__OpenBSD__)
1796 (0) /* For open bsd we do dual bind only */
1798 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1805 /* else fall through to conflict */
1812 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
1814 * Don't know why, but without this there is an unknown reference when
1815 * compiling NetBSD... hmm
1817 extern void in6_sin6_2_sin (struct sockaddr_in *, struct sockaddr_in6 *sin6);
1822 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1823 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
1825 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
1828 /* bind a ep to a socket address */
1829 struct sctppcbhead *head;
1830 struct sctp_inpcb *inp, *inp_tmp;
1831 struct inpcb *ip_inp;
1839 inp = (struct sctp_inpcb *)so->so_pcb;
1840 ip_inp = (struct inpcb *)so->so_pcb;
1842 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1844 printf("Bind called port:%d\n",
1845 ntohs(((struct sockaddr_in *)addr)->sin_port));
1847 sctp_print_address(addr);
1850 #endif /* SCTP_DEBUG */
1851 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1852 /* already did a bind, subsequent binds NOT allowed ! */
1857 if (addr->sa_family == AF_INET) {
1858 struct sockaddr_in *sin;
1860 /* IPV6_V6ONLY socket? */
1862 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1863 (ip_inp->inp_flags & IN6P_IPV6_V6ONLY)
1865 #if defined(__OpenBSD__)
1866 (0) /* For openbsd we do dual bind only */
1868 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1875 if (addr->sa_len != sizeof(*sin))
1878 sin = (struct sockaddr_in *)addr;
1879 lport = sin->sin_port;
1881 if (sin->sin_addr.s_addr != INADDR_ANY) {
1884 } else if (addr->sa_family == AF_INET6) {
1885 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
1886 struct sockaddr_in6 *sin6;
1888 sin6 = (struct sockaddr_in6 *)addr;
1890 if (addr->sa_len != sizeof(*sin6))
1893 lport = sin6->sin6_port;
1894 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1896 /* KAME hack: embed scopeid */
1897 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
1898 if (in6_embedscope(&sin6->sin6_addr, sin6,
1901 #elif defined(__FreeBSD__)
1902 error = scope6_check_id(sin6, ip6_use_defzone);
1906 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
1911 #ifndef SCOPEDROUTING
1912 /* this must be cleared for ifa_ifwithaddr() */
1913 sin6->sin6_scope_id = 0;
1914 #endif /* SCOPEDROUTING */
1916 return (EAFNOSUPPORT);
1919 SCTP_INP_INFO_WLOCK();
1920 SCTP_INP_WLOCK(inp);
1921 /* increase our count due to the unlock we do */
1922 SCTP_INP_INCR_REF(inp);
1925 * Did the caller specify a port? if so we must see if a
1926 * ep already has this one bound.
1928 /* got to be root to get at low ports */
1929 if (ntohs(lport) < IPPORT_RESERVED) {
1932 #if __FreeBSD_version >= 500000
1933 suser_cred(p->td_ucred, 0)
1937 #elif defined(__NetBSD__) || defined(__APPLE__)
1938 suser(p->p_ucred, &p->p_acflag)
1943 SCTP_INP_DECR_REF(inp);
1944 SCTP_INP_WUNLOCK(inp);
1945 SCTP_INP_INFO_WUNLOCK();
1950 SCTP_INP_DECR_REF(inp);
1951 SCTP_INP_WUNLOCK(inp);
1952 SCTP_INP_INFO_WUNLOCK();
1955 SCTP_INP_WUNLOCK(inp);
1956 inp_tmp = sctp_pcb_findep(addr, 0, 1);
1957 if (inp_tmp != NULL) {
1958 /* lock guy returned and lower count
1959 * note that we are not bound so inp_tmp
1960 * should NEVER be inp. And it is this
1961 * inp (inp_tmp) that gets the reference
1962 * bump, so we must lower it.
1964 SCTP_INP_WLOCK(inp_tmp);
1965 SCTP_INP_DECR_REF(inp_tmp);
1966 SCTP_INP_WUNLOCK(inp_tmp);
1969 SCTP_INP_INFO_WUNLOCK();
1970 return (EADDRNOTAVAIL);
1972 SCTP_INP_WLOCK(inp);
1974 /* verify that no lport is not used by a singleton */
1975 if (sctp_isport_inuse(inp, lport)) {
1976 /* Sorry someone already has this one bound */
1977 SCTP_INP_DECR_REF(inp);
1978 SCTP_INP_WUNLOCK(inp);
1979 SCTP_INP_INFO_WUNLOCK();
1980 return (EADDRNOTAVAIL);
1985 * get any port but lets make sure no one has any address
1986 * with this port bound
1990 * setup the inp to the top (I could use the union but this
1993 uint32_t port_guess;
1994 uint16_t port_attempt;
1998 port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
1999 port_attempt = (port_guess & 0x0000ffff);
2000 if (port_attempt == 0) {
2003 if (port_attempt < IPPORT_RESERVED) {
2004 port_attempt += IPPORT_RESERVED;
2007 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
2008 /* got a port we can use */
2012 /* try upper half */
2014 port_attempt = ((port_guess >> 16) & 0x0000ffff);
2015 if (port_attempt == 0) {
2018 if (port_attempt < IPPORT_RESERVED) {
2019 port_attempt += IPPORT_RESERVED;
2021 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
2022 /* got a port we can use */
2026 /* try two half's added together */
2028 port_attempt = (((port_guess >> 16) & 0x0000ffff) + (port_guess & 0x0000ffff));
2029 if (port_attempt == 0) {
2030 /* get a new random number */
2033 if (port_attempt < IPPORT_RESERVED) {
2034 port_attempt += IPPORT_RESERVED;
2036 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
2037 /* got a port we can use */
2042 /* we don't get out of the loop until we have a port */
2043 lport = htons(port_attempt);
2045 SCTP_INP_DECR_REF(inp);
2046 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2047 /* this really should not happen. The guy
2048 * did a non-blocking bind and then did a close
2051 SCTP_INP_WUNLOCK(inp);
2052 SCTP_INP_INFO_WUNLOCK();
2055 /* ok we look clear to give out this port, so lets setup the binding */
2057 /* binding to all addresses, so just set in the proper flags */
2058 inp->sctp_flags |= (SCTP_PCB_FLAGS_BOUNDALL |
2059 SCTP_PCB_FLAGS_DO_ASCONF);
2060 /* set the automatic addr changes from kernel flag */
2061 if (sctp_auto_asconf == 0) {
2062 inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
2064 inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
2068 * bind specific, make sure flags is off and add a new address
2069 * structure to the sctp_addr_list inside the ep structure.
2071 * We will need to allocate one and insert it at the head.
2072 * The socketopt call can just insert new addresses in there
2073 * as well. It will also have to do the embed scope kame hack
2074 * too (before adding).
2077 struct sockaddr_storage store_sa;
2079 memset(&store_sa, 0, sizeof(store_sa));
2080 if (addr->sa_family == AF_INET) {
2081 struct sockaddr_in *sin;
2083 sin = (struct sockaddr_in *)&store_sa;
2084 memcpy(sin, addr, sizeof(struct sockaddr_in));
2086 } else if (addr->sa_family == AF_INET6) {
2087 struct sockaddr_in6 *sin6;
2089 sin6 = (struct sockaddr_in6 *)&store_sa;
2090 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
2091 sin6->sin6_port = 0;
2094 * first find the interface with the bound address
2095 * need to zero out the port to find the address! yuck!
2096 * can't do this earlier since need port for sctp_pcb_findep()
2098 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
2100 /* Can't find an interface with that address */
2101 SCTP_INP_WUNLOCK(inp);
2102 SCTP_INP_INFO_WUNLOCK();
2103 return (EADDRNOTAVAIL);
2105 if (addr->sa_family == AF_INET6) {
2106 struct in6_ifaddr *ifa6;
2107 ifa6 = (struct in6_ifaddr *)ifa;
2109 * allow binding of deprecated addresses as per
2110 * RFC 2462 and ipng discussion
2112 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
2114 IN6_IFF_NOTREADY)) {
2115 /* Can't bind a non-existent addr. */
2116 SCTP_INP_WUNLOCK(inp);
2117 SCTP_INP_INFO_WUNLOCK();
2121 /* we're not bound all */
2122 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
2123 #if 0 /* use sysctl now */
2124 /* don't allow automatic addr changes from kernel */
2125 inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
2127 /* set the automatic addr changes from kernel flag */
2128 if (sctp_auto_asconf == 0) {
2129 inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
2131 inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
2133 /* allow bindx() to send ASCONF's for binding changes */
2134 inp->sctp_flags |= SCTP_PCB_FLAGS_DO_ASCONF;
2135 /* add this address to the endpoint list */
2136 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
2138 SCTP_INP_WUNLOCK(inp);
2139 SCTP_INP_INFO_WUNLOCK();
2144 /* find the bucket */
2145 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2146 sctppcbinfo.hashmark)];
2147 /* put it in the bucket */
2148 LIST_INSERT_HEAD(head, inp, sctp_hash);
2150 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2151 printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
2154 /* set in the port */
2155 inp->sctp_lport = lport;
2157 /* turn off just the unbound flag */
2158 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2159 SCTP_INP_WUNLOCK(inp);
2160 SCTP_INP_INFO_WUNLOCK();
2166 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
2168 struct sctp_iterator *it;
2169 /* We enter with the only the ITERATOR_LOCK in place and
2170 * A write lock on the inp_info stuff.
2173 /* Go through all iterators, we must do this since
2174 * it is possible that some iterator does NOT have
2175 * the lock, but is waiting for it. And the one that
2176 * had the lock has either moved in the last iteration
2177 * or we just cleared it above. We need to find all
2178 * of those guys. The list of iterators should never
2179 * be very big though.
2181 LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
2182 if (it == inp->inp_starting_point_for_iterator)
2183 /* skip this guy, he's special */
2185 if (it->inp == inp) {
2186 /* This is tricky and we DON'T lock the iterator.
2187 * Reason is he's running but waiting for me since
2188 * inp->inp_starting_point_for_iterator has the lock
2189 * on me (the guy above we skipped). This tells us
2190 * its is not running but waiting for inp->inp_starting_point_for_iterator
2191 * to be released by the guy that does have our INP in a lock.
2193 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2197 /* set him up to do the next guy not me */
2203 it = inp->inp_starting_point_for_iterator;
2205 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2214 /* release sctp_inpcb unbind the port */
2216 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate)
2219 * Here we free a endpoint. We must find it (if it is in the Hash
2220 * table) and remove it from there. Then we must also find it in
2221 * the overall list and remove it from there. After all removals are
2222 * complete then any timer has to be stopped. Then start the actual
2224 * a) Any local lists.
2225 * b) Any associations.
2226 * c) The hash of all associations.
2227 * d) finally the ep itself.
2230 struct sctp_inpcb *inp_save;
2231 struct sctp_tcb *asoc, *nasoc;
2232 struct sctp_laddr *laddr, *nladdr;
2233 struct inpcb *ip_pcb;
2235 struct sctp_socket_q_list *sq;
2236 #if !defined(__FreeBSD__) || __FreeBSD_version < 500000
2241 #if defined(__NetBSD__) || defined(__OpenBSD__)
2246 SCTP_ASOC_CREATE_LOCK(inp);
2247 SCTP_INP_WLOCK(inp);
2249 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2250 /* been here before */
2252 printf("Endpoint was all gone (dup free)?\n");
2253 SCTP_INP_WUNLOCK(inp);
2254 SCTP_ASOC_CREATE_UNLOCK(inp);
2257 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2260 sctp_m_freem(inp->control);
2261 inp->control = NULL;
2264 sctp_m_freem(inp->pkt);
2267 so = inp->sctp_socket;
2269 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main
2270 * pointer here but I will
2271 * be nice :> (i.e. ip_pcb = ep;)
2274 if (immediate == 0) {
2277 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2279 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2280 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2281 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2282 /* Just abandon things in the front states */
2283 SCTP_TCB_LOCK(asoc);
2284 SCTP_INP_WUNLOCK(inp);
2285 sctp_free_assoc(inp, asoc);
2286 SCTP_INP_WLOCK(inp);
2289 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2291 if ((asoc->asoc.size_on_delivery_queue > 0) ||
2292 (asoc->asoc.size_on_reasm_queue > 0) ||
2293 (asoc->asoc.size_on_all_streams > 0) ||
2294 (so && (so->so_rcv.sb_cc > 0))
2296 /* Left with Data unread */
2297 struct mbuf *op_err;
2298 MGET(op_err, M_DONTWAIT, MT_DATA);
2300 /* Fill in the user initiated abort */
2301 struct sctp_paramhdr *ph;
2303 sizeof(struct sctp_paramhdr);
2305 struct sctp_paramhdr *);
2306 ph->param_type = htons(
2307 SCTP_CAUSE_USER_INITIATED_ABT);
2308 ph->param_length = htons(op_err->m_len);
2310 SCTP_TCB_LOCK(asoc);
2311 sctp_send_abort_tcb(asoc, op_err);
2313 SCTP_INP_WUNLOCK(inp);
2314 sctp_free_assoc(inp, asoc);
2315 SCTP_INP_WLOCK(inp);
2317 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2318 TAILQ_EMPTY(&asoc->asoc.sent_queue)) {
2319 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2320 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2321 /* there is nothing queued to send, so I send shutdown */
2322 SCTP_TCB_LOCK(asoc);
2323 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2324 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2325 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2326 asoc->asoc.primary_destination);
2327 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2328 asoc->asoc.primary_destination);
2329 sctp_chunk_output(inp, asoc, 1);
2330 SCTP_TCB_UNLOCK(asoc);
2333 /* mark into shutdown pending */
2334 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2338 /* now is there some left in our SHUTDOWN state? */
2340 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE;
2342 SCTP_INP_WUNLOCK(inp);
2343 SCTP_ASOC_CREATE_UNLOCK(inp);
2347 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
2348 if (inp->refcount) {
2349 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2350 SCTP_INP_WUNLOCK(inp);
2351 SCTP_ASOC_CREATE_UNLOCK(inp);
2355 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2356 #if !defined(__FreeBSD__) || __FreeBSD_version < 500000
2357 rt = ip_pcb->inp_route.ro_rt;
2360 callout_stop(&inp->sctp_ep.signature_change.timer);
2363 /* First take care of socket level things */
2366 /* XXX IPsec cleanup here */
2368 if (ip_pcb->inp_tdb_in)
2369 TAILQ_REMOVE(&ip_pcb->inp_tdb_in->tdb_inp_in,
2370 ip_pcb, inp_tdb_in_next);
2371 if (ip_pcb->inp_tdb_out)
2372 TAILQ_REMOVE(&ip_pcb->inp_tdb_out->tdb_inp_out, ip_pcb,
2374 if (ip_pcb->inp_ipsec_localid)
2375 ipsp_reffree(ip_pcb->inp_ipsec_localid);
2376 if (ip_pcb->inp_ipsec_remoteid)
2377 ipsp_reffree(ip_pcb->inp_ipsec_remoteid);
2378 if (ip_pcb->inp_ipsec_localcred)
2379 ipsp_reffree(ip_pcb->inp_ipsec_localcred);
2380 if (ip_pcb->inp_ipsec_remotecred)
2381 ipsp_reffree(ip_pcb->inp_ipsec_remotecred);
2382 if (ip_pcb->inp_ipsec_localauth)
2383 ipsp_reffree(ip_pcb->inp_ipsec_localauth);
2384 if (ip_pcb->inp_ipsec_remoteauth)
2385 ipsp_reffree(ip_pcb->inp_ipsec_remoteauth);
2388 ipsec4_delete_pcbpolicy(ip_pcb);
2391 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
2396 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
2403 if (ip_pcb->inp_options) {
2404 (void)m_free(ip_pcb->inp_options);
2405 ip_pcb->inp_options = 0;
2407 #if !defined(__FreeBSD__) || __FreeBSD_version < 500000
2410 ip_pcb->inp_route.ro_rt = 0;
2413 if (ip_pcb->inp_moptions) {
2414 ip_freemoptions(ip_pcb->inp_moptions);
2415 ip_pcb->inp_moptions = 0;
2417 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
2420 ip_pcb->inp_vflag = 0;
2423 /* Now the sctp_pcb things */
2426 * free each asoc if it is not already closed/free. we can't use
2427 * the macro here since le_next will get freed as part of the
2428 * sctp_free_assoc() call.
2431 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2433 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2434 SCTP_TCB_LOCK(asoc);
2435 if (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) {
2436 struct mbuf *op_err;
2437 MGET(op_err, M_DONTWAIT, MT_DATA);
2439 /* Fill in the user initiated abort */
2440 struct sctp_paramhdr *ph;
2441 op_err->m_len = sizeof(struct sctp_paramhdr);
2442 ph = mtod(op_err, struct sctp_paramhdr *);
2443 ph->param_type = htons(
2444 SCTP_CAUSE_USER_INITIATED_ABT);
2445 ph->param_length = htons(op_err->m_len);
2447 sctp_send_abort_tcb(asoc, op_err);
2451 * sctp_free_assoc() will call sctp_inpcb_free(),
2452 * if SCTP_PCB_FLAGS_SOCKET_GONE set.
2453 * So, we clear it before sctp_free_assoc() making sure
2454 * no double sctp_inpcb_free().
2456 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SOCKET_GONE;
2457 SCTP_INP_WUNLOCK(inp);
2458 sctp_free_assoc(inp, asoc);
2459 SCTP_INP_WLOCK(inp);
2461 while ((sq = TAILQ_FIRST(&inp->sctp_queue_list)) != NULL) {
2462 TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
2463 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
2464 sctppcbinfo.ipi_count_sockq--;
2465 sctppcbinfo.ipi_gencnt_sockq++;
2467 inp->sctp_socket = 0;
2468 /* Now first we remove ourselves from the overall list of all EP's */
2470 /* Unlock inp first, need correct order */
2471 SCTP_INP_WUNLOCK(inp);
2472 /* now iterator lock */
2473 SCTP_ITERATOR_LOCK();
2475 SCTP_INP_INFO_WLOCK();
2476 /* now reget the inp lock */
2477 SCTP_INP_WLOCK(inp);
2479 inp_save = LIST_NEXT(inp, sctp_list);
2480 LIST_REMOVE(inp, sctp_list);
2482 * Now the question comes as to if this EP was ever bound at all.
2483 * If it was, then we must pull it out of the EP hash list.
2485 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2486 SCTP_PCB_FLAGS_UNBOUND) {
2488 * ok, this guy has been bound. It's port is somewhere
2489 * in the sctppcbinfo hash table. Remove it!
2491 LIST_REMOVE(inp, sctp_hash);
2493 /* fix any iterators only after out of the list */
2494 sctp_iterator_inp_being_freed(inp, inp_save);
2495 SCTP_ITERATOR_UNLOCK();
2497 * if we have an address list the following will free the list of
2498 * ifaddr's that are set into this ep. Again macro limitations here,
2499 * since the LIST_FOREACH could be a bad idea.
2501 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2503 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2504 LIST_REMOVE(laddr, sctp_nxt_addr);
2505 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
2506 sctppcbinfo.ipi_gencnt_laddr++;
2507 sctppcbinfo.ipi_count_laddr--;
2509 /* Now lets see about freeing the EP hash table. */
2510 if (inp->sctp_tcbhash != NULL) {
2511 FREE(inp->sctp_tcbhash, M_PCB);
2512 inp->sctp_tcbhash = 0;
2514 SCTP_INP_WUNLOCK(inp);
2515 SCTP_ASOC_CREATE_UNLOCK(inp);
2516 SCTP_INP_LOCK_DESTROY(inp);
2517 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2519 /* Now we must put the ep memory back into the zone pool */
2520 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2521 sctppcbinfo.ipi_count_ep--;
2523 SCTP_INP_INFO_WUNLOCK();
2529 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2531 struct sctp_nets *net;
2532 struct sockaddr_in *sin;
2533 struct sockaddr_in6 *sin6;
2534 /* use the peer's/remote port for lookup if unspecified */
2535 sin = (struct sockaddr_in *)addr;
2536 sin6 = (struct sockaddr_in6 *)addr;
2537 #if 0 /* why do we need to check the port for a nets list on an assoc? */
2538 if (stcb->rport != sin->sin_port) {
2539 /* we cheat and just a sin for this test */
2543 /* locate the address */
2544 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2545 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2553 * add's a remote endpoint address, done with the INIT/INIT-ACK
2554 * as well as when a ASCONF arrives that adds it. It will also
2555 * initialize all the cwnd stats of stuff.
2558 sctp_is_address_on_local_host(struct sockaddr *addr)
2562 TAILQ_FOREACH(ifn, &ifnet, if_list) {
2563 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
2564 if (addr->sa_family == ifa->ifa_addr->sa_family) {
2566 if (addr->sa_family == AF_INET) {
2567 struct sockaddr_in *sin, *sin_c;
2568 sin = (struct sockaddr_in *)addr;
2569 sin_c = (struct sockaddr_in *)
2571 if (sin->sin_addr.s_addr ==
2572 sin_c->sin_addr.s_addr) {
2573 /* we are on the same machine */
2576 } else if (addr->sa_family == AF_INET6) {
2577 struct sockaddr_in6 *sin6, *sin_c6;
2578 sin6 = (struct sockaddr_in6 *)addr;
2579 sin_c6 = (struct sockaddr_in6 *)
2581 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2582 &sin_c6->sin6_addr)) {
2583 /* we are on the same machine */
2594 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2595 int set_scope, int from)
2598 * The following is redundant to the same lines in the
2599 * sctp_aloc_assoc() but is needed since other's call the add
2602 struct sctp_nets *net, *netfirst;
2606 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2607 printf("Adding an address (from:%d) to the peer: ", from);
2608 sctp_print_address(newaddr);
2611 netfirst = sctp_findnet(stcb, newaddr);
2614 * Lie and return ok, we don't want to make the association
2615 * go away for this behavior. It will happen in the TCP model
2616 * in a connected socket. It does not reach the hash table
2617 * until after the association is built so it can't be found.
2618 * Mark as reachable, since the initial creation will have
2619 * been cleared and the NOT_IN_ASSOC flag will have been
2620 * added... and we don't want to end up removing it back out.
2622 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2623 netfirst->dest_state = (SCTP_ADDR_REACHABLE|
2624 SCTP_ADDR_UNCONFIRMED);
2626 netfirst->dest_state = SCTP_ADDR_REACHABLE;
2632 if (newaddr->sa_family == AF_INET) {
2633 struct sockaddr_in *sin;
2634 sin = (struct sockaddr_in *)newaddr;
2635 if (sin->sin_addr.s_addr == 0) {
2636 /* Invalid address */
2639 /* zero out the bzero area */
2640 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2642 /* assure len is set */
2643 sin->sin_len = sizeof(struct sockaddr_in);
2645 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2646 stcb->ipv4_local_scope = 1;
2648 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2649 stcb->asoc.ipv4_local_scope = 1;
2651 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2653 if (sctp_is_address_on_local_host(newaddr)) {
2654 stcb->asoc.loopback_scope = 1;
2655 stcb->asoc.ipv4_local_scope = 1;
2656 stcb->asoc.local_scope = 1;
2657 stcb->asoc.site_scope = 1;
2662 if (sctp_is_address_on_local_host(newaddr)) {
2663 stcb->asoc.loopback_scope = 1;
2664 stcb->asoc.ipv4_local_scope = 1;
2665 stcb->asoc.local_scope = 1;
2666 stcb->asoc.site_scope = 1;
2669 /* Validate the address is in scope */
2670 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2671 (stcb->asoc.ipv4_local_scope == 0)) {
2675 } else if (newaddr->sa_family == AF_INET6) {
2676 struct sockaddr_in6 *sin6;
2677 sin6 = (struct sockaddr_in6 *)newaddr;
2678 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2679 /* Invalid address */
2682 /* assure len is set */
2683 sin6->sin6_len = sizeof(struct sockaddr_in6);
2685 if (sctp_is_address_on_local_host(newaddr)) {
2686 stcb->asoc.loopback_scope = 1;
2687 stcb->asoc.local_scope = 1;
2688 stcb->asoc.ipv4_local_scope = 1;
2689 stcb->asoc.site_scope = 1;
2690 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2692 * If the new destination is a LINK_LOCAL
2693 * we must have common site scope. Don't set
2694 * the local scope since we may not share all
2695 * links, only loopback can do this.
2696 * Links on the local network would also
2697 * be on our private network for v4 too.
2699 stcb->asoc.ipv4_local_scope = 1;
2700 stcb->asoc.site_scope = 1;
2701 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2703 * If the new destination is SITE_LOCAL
2704 * then we must have site scope in common.
2706 stcb->asoc.site_scope = 1;
2711 if (sctp_is_address_on_local_host(newaddr)) {
2712 stcb->asoc.loopback_scope = 1;
2713 stcb->asoc.ipv4_local_scope = 1;
2714 stcb->asoc.local_scope = 1;
2715 stcb->asoc.site_scope = 1;
2718 /* Validate the address is in scope */
2719 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2720 (stcb->asoc.loopback_scope == 0)) {
2722 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2723 (stcb->asoc.local_scope == 0)) {
2725 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2726 (stcb->asoc.site_scope == 0)) {
2731 /* not supported family type */
2734 net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
2738 sctppcbinfo.ipi_count_raddr++;
2739 sctppcbinfo.ipi_gencnt_raddr++;
2740 bzero(net, sizeof(*net));
2741 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
2742 if (newaddr->sa_family == AF_INET) {
2743 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
2744 } else if (newaddr->sa_family == AF_INET6) {
2745 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
2747 net->addr_is_local = sctp_is_address_on_local_host(newaddr);
2748 net->failure_threshold = stcb->asoc.def_net_failure;
2749 if (addr_inscope == 0) {
2751 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2752 printf("Adding an address which is OUT OF SCOPE\n");
2754 #endif /* SCTP_DEBUG */
2755 net->dest_state = (SCTP_ADDR_REACHABLE |
2756 SCTP_ADDR_OUT_OF_SCOPE);
2759 /* 8 is passed by connect_x */
2760 net->dest_state = SCTP_ADDR_REACHABLE;
2762 net->dest_state = SCTP_ADDR_REACHABLE |
2763 SCTP_ADDR_UNCONFIRMED;
2765 net->RTO = stcb->asoc.initial_rto;
2766 stcb->asoc.numnets++;
2769 /* Init the timer structure */
2770 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2771 callout_init(&net->rxt_timer.timer, 0);
2773 callout_init(&net->rxt_timer.timer);
2776 /* Now generate a route for this guy */
2777 /* KAME hack: embed scopeid */
2778 if (newaddr->sa_family == AF_INET6) {
2779 struct sockaddr_in6 *sin6;
2780 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2781 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
2782 (void)in6_embedscope(&sin6->sin6_addr, sin6,
2783 &stcb->sctp_ep->ip_inp.inp, NULL);
2785 (void)in6_embedscope(&sin6->sin6_addr, sin6);
2787 #ifndef SCOPEDROUTING
2788 sin6->sin6_scope_id = 0;
2791 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
2792 rtalloc_ign((struct route *)&net->ro, 0UL);
2794 rtalloc((struct route *)&net->ro);
2796 if (newaddr->sa_family == AF_INET6) {
2797 struct sockaddr_in6 *sin6;
2798 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2799 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
2801 if ((net->ro.ro_rt) &&
2802 (net->ro.ro_rt->rt_ifp)) {
2803 net->mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2805 stcb->asoc.smallest_mtu = net->mtu;
2807 /* start things off to match mtu of interface please. */
2808 net->ro.ro_rt->rt_rmx.rmx_mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2810 net->mtu = stcb->asoc.smallest_mtu;
2812 if (stcb->asoc.smallest_mtu > net->mtu) {
2813 stcb->asoc.smallest_mtu = net->mtu;
2815 /* We take the max of the burst limit times a MTU or the INITIAL_CWND.
2816 * We then limit this to 4 MTU's of sending.
2818 net->cwnd = min((net->mtu * 4), max((stcb->asoc.max_burst * net->mtu), SCTP_INITIAL_CWND));
2820 /* we always get at LEAST 2 MTU's */
2821 if (net->cwnd < (2 * net->mtu)) {
2822 net->cwnd = 2 * net->mtu;
2825 net->ssthresh = stcb->asoc.peers_rwnd;
2827 net->src_addr_selected = 0;
2828 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
2829 if (net->ro.ro_rt == NULL) {
2830 /* Since we have no route put it at the back */
2831 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
2832 } else if (netfirst == NULL) {
2833 /* We are the first one in the pool. */
2834 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2835 } else if (netfirst->ro.ro_rt == NULL) {
2837 * First one has NO route. Place this one ahead of the
2840 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2841 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
2843 * This one has a different interface than the one at the
2844 * top of the list. Place it ahead.
2846 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2849 * Ok we have the same interface as the first one. Move
2850 * forward until we find either
2851 * a) one with a NULL route... insert ahead of that
2852 * b) one with a different ifp.. insert after that.
2853 * c) end of the list.. insert at the tail.
2855 struct sctp_nets *netlook;
2857 netlook = TAILQ_NEXT(netfirst, sctp_next);
2858 if (netlook == NULL) {
2859 /* End of the list */
2860 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
2863 } else if (netlook->ro.ro_rt == NULL) {
2864 /* next one has NO route */
2865 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
2867 } else if (netlook->ro.ro_rt->rt_ifp !=
2868 net->ro.ro_rt->rt_ifp) {
2869 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
2875 } while (netlook != NULL);
2877 /* got to have a primary set */
2878 if (stcb->asoc.primary_destination == 0) {
2879 stcb->asoc.primary_destination = net;
2880 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
2882 /* No route to current primary adopt new primary */
2883 stcb->asoc.primary_destination = net;
2885 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
2893 * allocate an association and add it to the endpoint. The caller must
2894 * be careful to add all additional addresses once they are know right
2895 * away or else the assoc will be may experience a blackout scenario.
2898 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
2899 int for_a_init, int *error, uint32_t override_tag)
2901 struct sctp_tcb *stcb;
2902 struct sctp_association *asoc;
2903 struct sctpasochead *head;
2908 * Assumption made here:
2909 * Caller has done a sctp_findassociation_ep_addr(ep, addr's);
2910 * to make sure the address does not exist already.
2912 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
2913 /* Hit max assoc, sorry no more */
2917 SCTP_INP_RLOCK(inp);
2918 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2920 * If its in the TCP pool, its NOT allowed to create an
2921 * association. The parent listener needs to call
2922 * sctp_aloc_assoc.. or the one-2-many socket. If a
2923 * peeled off, or connected one does this.. its an error.
2925 SCTP_INP_RUNLOCK(inp);
2931 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2932 printf("Allocate an association for peer:");
2934 sctp_print_address(firstaddr);
2938 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
2940 #endif /* SCTP_DEBUG */
2941 if (firstaddr->sa_family == AF_INET) {
2942 struct sockaddr_in *sin;
2943 sin = (struct sockaddr_in *)firstaddr;
2944 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
2945 /* Invalid address */
2947 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2948 printf("peer address invalid\n");
2951 SCTP_INP_RUNLOCK(inp);
2955 rport = sin->sin_port;
2956 } else if (firstaddr->sa_family == AF_INET6) {
2957 struct sockaddr_in6 *sin6;
2958 sin6 = (struct sockaddr_in6 *)firstaddr;
2959 if ((sin6->sin6_port == 0) ||
2960 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
2961 /* Invalid address */
2963 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2964 printf("peer address invalid\n");
2967 SCTP_INP_RUNLOCK(inp);
2971 rport = sin6->sin6_port;
2973 /* not supported family type */
2975 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2976 printf("BAD family %d\n", firstaddr->sa_family);
2979 SCTP_INP_RUNLOCK(inp);
2983 SCTP_INP_RUNLOCK(inp);
2984 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
2986 * If you have not performed a bind, then we need to do
2987 * the ephemerial bind for you.
2990 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2991 printf("Doing implicit BIND\n");
2995 if ((err = sctp_inpcb_bind(inp->sctp_socket,
2996 (struct sockaddr *)NULL,
2997 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
2998 (struct thread *)NULL
3003 /* bind error, probably perm */
3005 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
3006 printf("BIND FAILS ret:%d\n", err);
3014 stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
3016 /* out of memory? */
3018 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
3019 printf("aloc_assoc: no assoc mem left, stcb=NULL\n");
3025 sctppcbinfo.ipi_count_asoc++;
3026 sctppcbinfo.ipi_gencnt_asoc++;
3028 bzero(stcb, sizeof(*stcb));
3030 SCTP_TCB_LOCK_INIT(stcb);
3031 /* setup back pointer's */
3032 stcb->sctp_ep = inp;
3033 stcb->sctp_socket = inp->sctp_socket;
3034 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
3036 SCTP_TCB_LOCK_DESTROY (stcb);
3037 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3038 sctppcbinfo.ipi_count_asoc--;
3040 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
3041 printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
3048 stcb->rport = rport;
3049 SCTP_INP_INFO_WLOCK();
3050 SCTP_INP_WLOCK(inp);
3051 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3052 /* inpcb freed while alloc going on */
3053 SCTP_TCB_LOCK_DESTROY (stcb);
3054 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3055 SCTP_INP_WUNLOCK(inp);
3056 SCTP_INP_INFO_WUNLOCK();
3057 sctppcbinfo.ipi_count_asoc--;
3059 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
3060 printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
3066 SCTP_TCB_LOCK(stcb);
3068 /* now that my_vtag is set, add it to the hash */
3069 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
3070 sctppcbinfo.hashasocmark)];
3071 /* put it in the bucket in the vtag hash of assoc's for the system */
3072 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
3073 SCTP_INP_INFO_WUNLOCK();
3076 if ((err = sctp_add_remote_addr(stcb, firstaddr, 1, 1))) {
3077 /* failure.. memory error? */
3079 FREE(asoc->strmout, M_PCB);
3080 if (asoc->mapping_array)
3081 FREE(asoc->mapping_array, M_PCB);
3083 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3084 sctppcbinfo.ipi_count_asoc--;
3086 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
3087 printf("aloc_assoc: couldn't add remote addr!\n");
3090 SCTP_TCB_LOCK_DESTROY (stcb);
3094 /* Init all the timers */
3095 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3096 callout_init(&asoc->hb_timer.timer, 0);
3097 callout_init(&asoc->dack_timer.timer, 0);
3098 callout_init(&asoc->asconf_timer.timer, 0);
3099 callout_init(&asoc->shut_guard_timer.timer, 0);
3100 callout_init(&asoc->autoclose_timer.timer, 0);
3102 callout_init(&asoc->hb_timer.timer);
3103 callout_init(&asoc->dack_timer.timer);
3104 callout_init(&asoc->asconf_timer.timer);
3105 callout_init(&asoc->shut_guard_timer.timer);
3106 callout_init(&asoc->autoclose_timer.timer);
3108 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
3109 /* now file the port under the hash as well */
3110 if (inp->sctp_tcbhash != NULL) {
3111 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
3112 inp->sctp_hashmark)];
3113 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
3115 SCTP_INP_WUNLOCK(inp);
3117 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3118 printf("Association %p now allocated\n", stcb);
3125 sctp_free_remote_addr(struct sctp_nets *net)
3130 if (net->ref_count <= 0) {
3131 /* stop timer if running */
3132 callout_stop(&net->rxt_timer.timer);
3133 callout_stop(&net->pmtu_timer.timer);
3134 net->dest_state = SCTP_ADDR_NOT_REACHABLE;
3135 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
3136 sctppcbinfo.ipi_count_raddr--;
3141 * remove a remote endpoint address from an association, it
3142 * will fail if the address does not exist.
3145 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3148 * Here we need to remove a remote address. This is quite simple, we
3149 * first find it in the list of address for the association
3150 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE on
3152 * Note we do not allow it to be removed if there are no other
3155 struct sctp_association *asoc;
3156 struct sctp_nets *net, *net_tmp;
3158 if (asoc->numnets < 2) {
3159 /* Must have at LEAST two remote addresses */
3162 /* locate the address */
3163 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3164 net_tmp = TAILQ_NEXT(net, sctp_next);
3165 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3168 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3170 /* we found the guy */
3172 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3173 sctp_free_remote_addr(net);
3174 if (net == asoc->primary_destination) {
3176 struct sctp_nets *lnet;
3177 lnet = TAILQ_FIRST(&asoc->nets);
3178 /* Try to find a confirmed primary */
3179 asoc->primary_destination =
3180 sctp_find_alternate_net(stcb, lnet);
3182 if (net == asoc->last_data_chunk_from) {
3184 asoc->last_data_chunk_from =
3185 TAILQ_FIRST(&asoc->nets);
3187 if (net == asoc->last_control_chunk_from) {
3189 asoc->last_control_chunk_from =
3190 TAILQ_FIRST(&asoc->nets);
3192 if (net == asoc->asconf_last_sent_to) {
3194 asoc->asconf_last_sent_to =
3195 TAILQ_FIRST(&asoc->nets);
3206 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, u_int32_t tag)
3208 struct sctpvtaghead *chain;
3209 struct sctp_tagblock *twait_block;
3212 SCTP_GETTIME_TIMEVAL(&now);
3213 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3215 if (!LIST_EMPTY(chain)) {
3216 /* Block(s) present, lets find space, and expire on the fly */
3217 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3218 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3219 if ((twait_block->vtag_block[i].v_tag == 0) &&
3221 twait_block->vtag_block[0].tv_sec_at_expire =
3222 now.tv_sec + SCTP_TIME_WAIT;
3223 twait_block->vtag_block[0].v_tag = tag;
3225 } else if ((twait_block->vtag_block[i].v_tag) &&
3226 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3228 /* Audit expires this guy */
3229 twait_block->vtag_block[i].tv_sec_at_expire = 0;
3230 twait_block->vtag_block[i].v_tag = 0;
3232 /* Reuse it for my new tag */
3233 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3234 twait_block->vtag_block[0].v_tag = tag;
3241 * We only do up to the block where we can
3242 * place our tag for audits
3248 /* Need to add a new block to chain */
3250 MALLOC(twait_block, struct sctp_tagblock *,
3251 sizeof(struct sctp_tagblock), M_PCB, M_NOWAIT);
3252 if (twait_block == NULL) {
3255 memset(twait_block, 0, sizeof(struct sctp_timewait));
3256 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3257 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3259 twait_block->vtag_block[0].v_tag = tag;
3265 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3267 struct sctp_iterator *it;
3271 /* Unlock the tcb lock we do this so
3272 * we avoid a dead lock scenario where
3273 * the iterator is waiting on the TCB lock
3274 * and the TCB lock is waiting on the iterator
3277 SCTP_ITERATOR_LOCK();
3278 SCTP_INP_INFO_WLOCK();
3279 SCTP_INP_WLOCK(inp);
3280 SCTP_TCB_LOCK(stcb);
3282 it = stcb->asoc.stcb_starting_point_for_iterator;
3286 if (it->inp != stcb->sctp_ep) {
3287 /* hm, focused on the wrong one? */
3290 if (it->stcb != stcb) {
3293 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3294 if (it->stcb == NULL) {
3295 /* done with all asoc's in this assoc */
3296 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3300 it->inp = LIST_NEXT(inp, sctp_list);
3306 * Free the association after un-hashing the remote port.
3309 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3311 struct sctp_association *asoc;
3312 struct sctp_nets *net, *prev;
3313 struct sctp_laddr *laddr;
3314 struct sctp_tmit_chunk *chk;
3315 struct sctp_asconf_addr *aparam;
3316 struct sctp_socket_q_list *sq;
3319 /* first, lets purge the entry from the hash table. */
3320 #if defined(__NetBSD__) || defined(__OpenBSD__)
3325 if (stcb->asoc.state == 0) {
3326 printf("Freeing already free association:%p - huh??\n",
3333 /* now clean up any other timers */
3334 callout_stop(&asoc->hb_timer.timer);
3335 callout_stop(&asoc->dack_timer.timer);
3336 callout_stop(&asoc->asconf_timer.timer);
3337 callout_stop(&asoc->shut_guard_timer.timer);
3338 callout_stop(&asoc->autoclose_timer.timer);
3339 callout_stop(&asoc->delayed_event_timer.timer);
3340 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3341 callout_stop(&net->rxt_timer.timer);
3342 callout_stop(&net->pmtu_timer.timer);
3345 /* Iterator asoc being freed we send an
3346 * unlocked TCB. It returns with INP_INFO
3347 * and INP write locked and the TCB locked
3348 * too and of course the iterator lock
3349 * in place as well..
3351 SCTP_TCB_UNLOCK(stcb);
3352 sctp_iterator_asoc_being_freed(inp, stcb);
3354 /* Null all of my entry's on the socket q */
3355 TAILQ_FOREACH(sq, &inp->sctp_queue_list, next_sq) {
3356 if (sq->tcb == stcb) {
3361 if (inp->sctp_tcb_at_block == (void *)stcb) {
3362 inp->error_on_block = ECONNRESET;
3365 if (inp->sctp_tcbhash) {
3366 LIST_REMOVE(stcb, sctp_tcbhash);
3368 /* Now lets remove it from the list of ALL associations in the EP */
3369 LIST_REMOVE(stcb, sctp_tcblist);
3370 SCTP_INP_WUNLOCK(inp);
3371 SCTP_ITERATOR_UNLOCK();
3374 /* pull from vtag hash */
3375 LIST_REMOVE(stcb, sctp_asocs);
3378 * Now before we can free the assoc, we must remove all of the
3379 * networks and any other allocated space.. i.e. add removes here
3380 * before the SCTP_ZONE_FREE() of the tasoc entry.
3383 sctp_add_vtag_to_timewait(inp, asoc->my_vtag);
3384 SCTP_INP_INFO_WUNLOCK();
3386 while (!TAILQ_EMPTY(&asoc->nets)) {
3387 net = TAILQ_FIRST(&asoc->nets);
3388 /* pull from list */
3389 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3393 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3396 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
3397 sctppcbinfo.ipi_count_raddr--;
3400 * The chunk lists and such SHOULD be empty but we check them
3403 /* anything on the wheel needs to be removed */
3404 while (!TAILQ_EMPTY(&asoc->out_wheel)) {
3405 struct sctp_stream_out *outs;
3406 outs = TAILQ_FIRST(&asoc->out_wheel);
3407 TAILQ_REMOVE(&asoc->out_wheel, outs, next_spoke);
3408 /* now clean up any chunks here */
3409 chk = TAILQ_FIRST(&outs->outqueue);
3411 TAILQ_REMOVE(&outs->outqueue, chk, sctp_next);
3413 sctp_m_freem(chk->data);
3418 /* Free the chunk */
3419 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3420 sctppcbinfo.ipi_count_chunk--;
3421 sctppcbinfo.ipi_gencnt_chunk++;
3422 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3423 panic("Chunk count is negative");
3425 chk = TAILQ_FIRST(&outs->outqueue);
3427 outs = TAILQ_FIRST(&asoc->out_wheel);
3430 if (asoc->pending_reply) {
3431 FREE(asoc->pending_reply, M_PCB);
3432 asoc->pending_reply = NULL;
3434 chk = TAILQ_FIRST(&asoc->pending_reply_queue);
3436 TAILQ_REMOVE(&asoc->pending_reply_queue, chk, sctp_next);
3438 sctp_m_freem(chk->data);
3443 /* Free the chunk */
3444 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3445 sctppcbinfo.ipi_count_chunk--;
3446 sctppcbinfo.ipi_gencnt_chunk++;
3447 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3448 panic("Chunk count is negative");
3450 chk = TAILQ_FIRST(&asoc->pending_reply_queue);
3452 /* pending send queue SHOULD be empty */
3453 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3454 chk = TAILQ_FIRST(&asoc->send_queue);
3456 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3458 sctp_m_freem(chk->data);
3461 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3462 sctppcbinfo.ipi_count_chunk--;
3463 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3464 panic("Chunk count is negative");
3466 sctppcbinfo.ipi_gencnt_chunk++;
3467 chk = TAILQ_FIRST(&asoc->send_queue);
3470 /* sent queue SHOULD be empty */
3471 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3472 chk = TAILQ_FIRST(&asoc->sent_queue);
3474 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3476 sctp_m_freem(chk->data);
3479 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3480 sctppcbinfo.ipi_count_chunk--;
3481 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3482 panic("Chunk count is negative");
3484 sctppcbinfo.ipi_gencnt_chunk++;
3485 chk = TAILQ_FIRST(&asoc->sent_queue);
3488 /* control queue MAY not be empty */
3489 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3490 chk = TAILQ_FIRST(&asoc->control_send_queue);
3492 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3494 sctp_m_freem(chk->data);
3497 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3498 sctppcbinfo.ipi_count_chunk--;
3499 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3500 panic("Chunk count is negative");
3502 sctppcbinfo.ipi_gencnt_chunk++;
3503 chk = TAILQ_FIRST(&asoc->control_send_queue);
3506 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3507 chk = TAILQ_FIRST(&asoc->reasmqueue);
3509 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3511 sctp_m_freem(chk->data);
3514 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3515 sctppcbinfo.ipi_count_chunk--;
3516 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3517 panic("Chunk count is negative");
3519 sctppcbinfo.ipi_gencnt_chunk++;
3520 chk = TAILQ_FIRST(&asoc->reasmqueue);
3523 if (!TAILQ_EMPTY(&asoc->delivery_queue)) {
3524 chk = TAILQ_FIRST(&asoc->delivery_queue);
3526 TAILQ_REMOVE(&asoc->delivery_queue, chk, sctp_next);
3528 sctp_m_freem(chk->data);
3531 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3532 sctppcbinfo.ipi_count_chunk--;
3533 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3534 panic("Chunk count is negative");
3536 sctppcbinfo.ipi_gencnt_chunk++;
3537 chk = TAILQ_FIRST(&asoc->delivery_queue);
3540 if (asoc->mapping_array) {
3541 FREE(asoc->mapping_array, M_PCB);
3542 asoc->mapping_array = NULL;
3545 /* the stream outs */
3546 if (asoc->strmout) {
3547 FREE(asoc->strmout, M_PCB);
3548 asoc->strmout = NULL;
3550 asoc->streamoutcnt = 0;
3553 for (i = 0; i < asoc->streamincnt; i++) {
3554 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3555 /* We have somethings on the streamin queue */
3556 chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3558 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3561 sctp_m_freem(chk->data);
3564 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk,
3566 sctppcbinfo.ipi_count_chunk--;
3567 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3568 panic("Chunk count is negative");
3570 sctppcbinfo.ipi_gencnt_chunk++;
3571 chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3575 FREE(asoc->strmin, M_PCB);
3576 asoc->strmin = NULL;
3578 asoc->streamincnt = 0;
3579 /* local addresses, if any */
3580 while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
3581 laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
3582 LIST_REMOVE(laddr, sctp_nxt_addr);
3583 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3584 sctppcbinfo.ipi_count_laddr--;
3586 /* pending asconf (address) parameters */
3587 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3588 aparam = TAILQ_FIRST(&asoc->asconf_queue);
3589 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3590 FREE(aparam, M_PCB);
3592 if (asoc->last_asconf_ack_sent != NULL) {
3593 sctp_m_freem(asoc->last_asconf_ack_sent);
3594 asoc->last_asconf_ack_sent = NULL;
3596 /* Insert new items here :> */
3598 /* Get rid of LOCK */
3599 SCTP_TCB_LOCK_DESTROY(stcb);
3601 /* now clean up the tasoc itself */
3602 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3603 sctppcbinfo.ipi_count_asoc--;
3604 if ((inp->sctp_socket->so_snd.sb_cc) ||
3605 (inp->sctp_socket->so_snd.sb_mbcnt)) {
3606 /* This will happen when a abort is done */
3607 inp->sctp_socket->so_snd.sb_cc = 0;
3608 inp->sctp_socket->so_snd.sb_mbcnt = 0;
3610 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3611 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
3612 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3614 * For the base fd, that is NOT in TCP pool we
3615 * turn off the connected flag. This allows
3616 * non-listening endpoints to connect/shutdown/
3619 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3620 soisdisconnected(inp->sctp_socket);
3623 * For those that are in the TCP pool we just leave
3624 * so it cannot be used. When they close the fd we
3629 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3630 sctp_inpcb_free(inp, 0);
3637 * determine if a destination is "reachable" based upon the addresses
3638 * bound to the current endpoint (e.g. only v4 or v6 currently bound)
3641 * FIX: if we allow assoc-level bindx(), then this needs to be fixed
3642 * to use assoc level v4/v6 flags, as the assoc *may* not have the
3643 * same address types bound as its endpoint
3646 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
3648 struct sctp_inpcb *inp;
3651 /* No locks here, the TCB, in all cases is already
3652 * locked and an assoc is up. There is either a
3653 * INP lock by the caller applied (in asconf case when
3654 * deleting an address) or NOT in the HB case, however
3655 * if HB then the INP increment is up and the INP
3656 * will not be removed (on top of the fact that
3657 * we have a TCB lock). So we only want to
3658 * read the sctp_flags, which is either bound-all
3659 * or not.. no protection needed since once an
3660 * assoc is up you can't be changing your binding.
3662 inp = stcb->sctp_ep;
3663 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3664 /* if bound all, destination is not restricted */
3665 /* RRS: Question during lock work: Is this
3666 * correct? If you are bound-all you still
3667 * might need to obey the V4--V6 flags???
3668 * IMO this bound-all stuff needs to be removed!
3672 /* NOTE: all "scope" checks are done when local addresses are added */
3673 if (destaddr->sa_family == AF_INET6) {
3674 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
3675 answer = inp->inp_vflag & INP_IPV6;
3677 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
3679 } else if (destaddr->sa_family == AF_INET) {
3680 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
3681 answer = inp->inp_vflag & INP_IPV4;
3683 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
3686 /* invalid family, so it's unreachable */
3693 * update the inp_vflags on an endpoint
3696 sctp_update_ep_vflag(struct sctp_inpcb *inp) {
3697 struct sctp_laddr *laddr;
3699 /* first clear the flag */
3700 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
3703 inp->ip_inp.inp.inp_vflag = 0;
3705 /* set the flag based on addresses on the ep list */
3706 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3707 if (laddr->ifa == NULL) {
3709 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3710 printf("An ounce of prevention is worth a pound of cure\n");
3712 #endif /* SCTP_DEBUG */
3715 if (laddr->ifa->ifa_addr) {
3718 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
3719 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
3720 inp->inp_vflag |= INP_IPV6;
3722 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3724 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3725 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
3726 inp->inp_vflag |= INP_IPV4;
3728 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3735 * Add the address to the endpoint local address list
3736 * There is nothing to be done if we are bound to all addresses
3739 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3741 struct sctp_laddr *laddr;
3745 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3746 /* You are already bound to all. You have it already */
3749 if (ifa->ifa_addr->sa_family == AF_INET6) {
3750 struct in6_ifaddr *ifa6;
3751 ifa6 = (struct in6_ifaddr *)ifa;
3752 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3753 IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
3754 /* Can't bind a non-existent addr. */
3757 /* first, is it already present? */
3758 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3759 if (laddr->ifa == ifa) {
3765 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
3766 /* Not bound to all */
3767 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
3771 /* update inp_vflag flags */
3772 if (ifa->ifa_addr->sa_family == AF_INET6) {
3773 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
3774 inp->inp_vflag |= INP_IPV6;
3776 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3778 } else if (ifa->ifa_addr->sa_family == AF_INET) {
3779 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__))
3780 inp->inp_vflag |= INP_IPV4;
3782 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3791 * select a new (hopefully reachable) destination net
3792 * (should only be used when we deleted an ep addr that is the
3793 * only usable source address to reach the destination net)
3796 sctp_select_primary_destination(struct sctp_tcb *stcb)
3798 struct sctp_nets *net;
3800 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3801 /* for now, we'll just pick the first reachable one we find */
3802 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
3804 if (sctp_destination_is_reachable(stcb,
3805 (struct sockaddr *)&net->ro._l_addr)) {
3806 /* found a reachable destination */
3807 stcb->asoc.primary_destination = net;
3810 /* I can't there from here! ...we're gonna die shortly... */
3815 * Delete the address from the endpoint local address list
3816 * There is nothing to be done if we are bound to all addresses
3819 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3821 struct sctp_laddr *laddr;
3824 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3825 /* You are already bound to all. You have it already */
3829 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3830 if (laddr->ifa == ifa) {
3835 if (fnd && (inp->laddr_count < 2)) {
3836 /* can't delete unless there are at LEAST 2 addresses */
3839 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
3841 * clean up any use of this address
3842 * go through our associations and clear any
3843 * last_used_address that match this one
3844 * for each assoc, see if a new primary_destination is needed
3846 struct sctp_tcb *stcb;
3848 /* clean up "next_addr_touse" */
3849 if (inp->next_addr_touse == laddr)
3850 /* delete this address */
3851 inp->next_addr_touse = NULL;
3853 /* clean up "last_used_address" */
3854 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3855 if (stcb->asoc.last_used_address == laddr)
3856 /* delete this address */
3857 stcb->asoc.last_used_address = NULL;
3858 } /* for each tcb */
3860 /* remove it from the ep list */
3861 sctp_remove_laddr(laddr);
3863 /* update inp_vflag flags */
3864 sctp_update_ep_vflag(inp);
3865 /* select a new primary destination if needed */
3866 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3867 /* presume caller (sctp_asconf.c) already owns INP lock */
3868 SCTP_TCB_LOCK(stcb);
3869 if (sctp_destination_is_reachable(stcb,
3870 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr) == 0) {
3871 sctp_select_primary_destination(stcb);
3873 SCTP_TCB_UNLOCK(stcb);
3874 } /* for each tcb */
3880 * Add the addr to the TCB local address list
3881 * For the BOUNDALL or dynamic case, this is a "pending" address list
3882 * (eg. addresses waiting for an ASCONF-ACK response)
3883 * For the subset binding, static case, this is a "valid" address list
3886 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3888 struct sctp_inpcb *inp;
3889 struct sctp_laddr *laddr;
3892 /* Assumes TCP is locked.. and possiblye
3893 * the INP. May need to confirm/fix that if
3894 * we need it and is not the case.
3896 inp = stcb->sctp_ep;
3897 if (ifa->ifa_addr->sa_family == AF_INET6) {
3898 struct in6_ifaddr *ifa6;
3899 ifa6 = (struct in6_ifaddr *)ifa;
3900 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3901 /* IN6_IFF_DEPRECATED | */
3904 /* Can't bind a non-existent addr. */
3907 /* does the address already exist? */
3908 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3909 if (laddr->ifa == ifa) {
3914 /* add to the list */
3915 error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
3922 * insert an laddr entry with the given ifa for the desired list
3925 sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa) {
3926 struct sctp_laddr *laddr;
3929 #if defined(__NetBSD__) || defined(__OpenBSD__)
3935 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
3936 if (laddr == NULL) {
3937 /* out of memory? */
3941 sctppcbinfo.ipi_count_laddr++;
3942 sctppcbinfo.ipi_gencnt_laddr++;
3943 bzero(laddr, sizeof(*laddr));
3946 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3953 * Remove an laddr entry from the local address list (on an assoc)
3956 sctp_remove_laddr(struct sctp_laddr *laddr)
3959 #if defined(__NetBSD__) || defined(__OpenBSD__)
3964 /* remove from the list */
3965 LIST_REMOVE(laddr, sctp_nxt_addr);
3966 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3967 sctppcbinfo.ipi_count_laddr--;
3968 sctppcbinfo.ipi_gencnt_laddr++;
3974 * Remove an address from the TCB local address list
3977 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3979 struct sctp_inpcb *inp;
3980 struct sctp_laddr *laddr;
3982 /* This is called by asconf work. It is assumed that
3983 * a) The TCB is locked
3985 * b) The INP is locked.
3986 * This is true in as much as I can trace through
3987 * the entry asconf code where I did these locks.
3988 * Again, the ASCONF code is a bit different in
3989 * that it does lock the INP during its work often
3990 * times. This must be since we don't want other
3991 * proc's looking up things while what they are
3992 * looking up is changing :-D
3995 inp = stcb->sctp_ep;
3996 /* if subset bound and don't allow ASCONF's, can't delete last */
3997 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
3998 ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
3999 if (stcb->asoc.numnets < 2) {
4000 /* can't delete last address */
4005 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4006 /* remove the address if it exists */
4007 if (laddr->ifa == NULL)
4009 if (laddr->ifa == ifa) {
4010 sctp_remove_laddr(laddr);
4015 /* address not found! */
4020 * Remove an address from the TCB local address list
4021 * lookup using a sockaddr addr
4024 sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
4026 struct sctp_inpcb *inp;
4027 struct sctp_laddr *laddr;
4028 struct sockaddr *l_sa;
4031 * This function I find does not seem to have a caller.
4032 * As such we NEED TO DELETE this code. If we do
4033 * find a caller, the caller MUST have locked the TCB
4034 * at the least and probably the INP as well.
4036 inp = stcb->sctp_ep;
4037 /* if subset bound and don't allow ASCONF's, can't delete last */
4038 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4039 ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4040 if (stcb->asoc.numnets < 2) {
4041 /* can't delete last address */
4046 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4047 /* make sure the address exists */
4048 if (laddr->ifa == NULL)
4050 if (laddr->ifa->ifa_addr == NULL)
4053 l_sa = laddr->ifa->ifa_addr;
4054 if (l_sa->sa_family == AF_INET6) {
4056 struct sockaddr_in6 *sin1, *sin2;
4057 sin1 = (struct sockaddr_in6 *)l_sa;
4058 sin2 = (struct sockaddr_in6 *)sa;
4059 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4060 sizeof(struct in6_addr)) == 0) {
4062 sctp_remove_laddr(laddr);
4065 } else if (l_sa->sa_family == AF_INET) {
4067 struct sockaddr_in *sin1, *sin2;
4068 sin1 = (struct sockaddr_in *)l_sa;
4069 sin2 = (struct sockaddr_in *)sa;
4070 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4072 sctp_remove_laddr(laddr);
4076 /* invalid family */
4080 /* address not found! */
4084 static char sctp_pcb_initialized = 0;
4086 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4088 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4089 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4091 #endif /* FreeBSD || APPLE || DragonFly */
4093 #ifndef SCTP_TCBHASHSIZE
4094 #define SCTP_TCBHASHSIZE 1024
4097 #ifndef SCTP_CHUNKQUEUE_SCALE
4098 #define SCTP_CHUNKQUEUE_SCALE 10
4105 * SCTP initialization for the PCB structures
4106 * should be called by the sctp_init() funciton.
4109 int hashtblsize = SCTP_TCBHASHSIZE;
4111 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4112 int sctp_chunkscale = SCTP_CHUNKQUEUE_SCALE;
4115 if (sctp_pcb_initialized != 0) {
4116 /* error I was called twice */
4119 sctp_pcb_initialized = 1;
4121 /* Init all peg counts */
4122 for (i = 0; i < SCTP_NUMBER_OF_PEGS; i++) {
4126 /* init the empty list of (All) Endpoints */
4127 LIST_INIT(&sctppcbinfo.listhead);
4129 /* init the iterator head */
4130 LIST_INIT(&sctppcbinfo.iteratorhead);
4132 /* init the hash table of endpoints */
4133 #if defined(__FreeBSD__)
4134 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
4135 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &hashtblsize);
4136 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4137 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4139 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
4141 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
4143 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
4148 sctppcbinfo.sctp_asochash = hashinit((hashtblsize * 31),
4153 #if defined(__NetBSD__) || defined(__OpenBSD__)
4156 &sctppcbinfo.hashasocmark);
4158 sctppcbinfo.sctp_ephash = hashinit(hashtblsize,
4163 #if defined(__NetBSD__) || defined(__OpenBSD__)
4166 &sctppcbinfo.hashmark);
4168 sctppcbinfo.sctp_tcpephash = hashinit(hashtblsize,
4173 #if defined(__NetBSD__) || defined(__OpenBSD__)
4176 &sctppcbinfo.hashtcpmark);
4178 sctppcbinfo.hashtblsize = hashtblsize;
4180 /* init the zones */
4182 * FIX ME: Should check for NULL returns, but if it does fail we
4183 * are doomed to panic anyways... add later maybe.
4185 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4186 sizeof(struct sctp_inpcb), maxsockets);
4188 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4189 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4191 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4192 sizeof(struct sctp_laddr),
4193 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4195 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4196 sizeof(struct sctp_nets),
4197 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4199 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4200 sizeof(struct sctp_tmit_chunk),
4201 (sctp_max_number_of_assoc * sctp_scale_up_for_address *
4204 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_sockq, "sctp_sockq",
4205 sizeof(struct sctp_socket_q_list),
4206 (sctp_max_number_of_assoc * sctp_scale_up_for_address *
4209 /* Master Lock INIT for info structure */
4210 SCTP_INP_INFO_LOCK_INIT();
4211 SCTP_ITERATOR_LOCK_INIT();
4212 /* not sure if we need all the counts */
4213 sctppcbinfo.ipi_count_ep = 0;
4214 sctppcbinfo.ipi_gencnt_ep = 0;
4215 /* assoc/tcb zone info */
4216 sctppcbinfo.ipi_count_asoc = 0;
4217 sctppcbinfo.ipi_gencnt_asoc = 0;
4218 /* local addrlist zone info */
4219 sctppcbinfo.ipi_count_laddr = 0;
4220 sctppcbinfo.ipi_gencnt_laddr = 0;
4221 /* remote addrlist zone info */
4222 sctppcbinfo.ipi_count_raddr = 0;
4223 sctppcbinfo.ipi_gencnt_raddr = 0;
4225 sctppcbinfo.ipi_count_chunk = 0;
4226 sctppcbinfo.ipi_gencnt_chunk = 0;
4228 /* socket queue zone info */
4229 sctppcbinfo.ipi_count_sockq = 0;
4230 sctppcbinfo.ipi_gencnt_sockq = 0;
4233 sctppcbinfo.mbuf_track = 0;
4235 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__) \
4236 || defined(__DragonFly__)
4237 sctppcbinfo.lastlow = ipport_firstauto;
4239 sctppcbinfo.lastlow = anonportmin;
4241 /* Init the TIMEWAIT list */
4242 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
4243 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4246 #if defined(_SCTP_NEEDS_CALLOUT_) && !defined(__APPLE__)
4247 TAILQ_INIT(&sctppcbinfo.callqueue);
4253 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4254 int iphlen, int offset, int limit, struct sctphdr *sh,
4255 struct sockaddr *altsa)
4258 * grub through the INIT pulling addresses and
4259 * loading them to the nets structure in the asoc.
4260 * The from address in the mbuf should also be loaded
4261 * (if it is not already). This routine can be called
4262 * with either INIT or INIT-ACK's as long as the
4263 * m points to the IP packet and the offset points
4264 * to the beginning of the parameters.
4266 struct sctp_inpcb *inp, *l_inp;
4267 struct sctp_nets *net, *net_tmp;
4269 struct sctp_paramhdr *phdr, parm_buf;
4270 struct sctp_tcb *stcb_tmp;
4271 u_int16_t ptype, plen;
4272 struct sockaddr *sa;
4273 struct sockaddr_storage dest_store;
4274 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4275 struct sockaddr_in sin;
4276 struct sockaddr_in6 sin6;
4278 /* First get the destination address setup too. */
4279 memset(&sin, 0, sizeof(sin));
4280 memset(&sin6, 0, sizeof(sin6));
4282 sin.sin_family = AF_INET;
4283 sin.sin_len = sizeof(sin);
4284 sin.sin_port = stcb->rport;
4286 sin6.sin6_family = AF_INET6;
4287 sin6.sin6_len = sizeof(struct sockaddr_in6);
4288 sin6.sin6_port = stcb->rport;
4289 if (altsa == NULL) {
4290 iph = mtod(m, struct ip *);
4291 if (iph->ip_v == IPVERSION) {
4293 struct sockaddr_in *sin_2;
4294 sin_2 = (struct sockaddr_in *)(local_sa);
4295 memset(sin_2, 0, sizeof(sin));
4296 sin_2->sin_family = AF_INET;
4297 sin_2->sin_len = sizeof(sin);
4298 sin_2->sin_port = sh->dest_port;
4299 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr ;
4300 sin.sin_addr = iph->ip_src;
4301 sa = (struct sockaddr *)&sin;
4302 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4304 struct ip6_hdr *ip6;
4305 struct sockaddr_in6 *sin6_2;
4307 ip6 = mtod(m, struct ip6_hdr *);
4308 sin6_2 = (struct sockaddr_in6 *)(local_sa);
4309 memset(sin6_2, 0, sizeof(sin6));
4310 sin6_2->sin6_family = AF_INET6;
4311 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4312 sin6_2->sin6_port = sh->dest_port;
4313 sin6.sin6_addr = ip6->ip6_src;
4314 sa = (struct sockaddr *)&sin6;
4320 * For cookies we use the src address NOT from the packet
4321 * but from the original INIT
4325 /* Turn off ECN until we get through all params */
4326 stcb->asoc.ecn_allowed = 0;
4328 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4329 /* mark all addresses that we have currently on the list */
4330 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4332 /* does the source address already exist? if so skip it */
4333 l_inp = inp = stcb->sctp_ep;
4334 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4335 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4336 /* we must add the source address */
4337 /* no scope set here since we have a tcb already. */
4338 if ((sa->sa_family == AF_INET) &&
4339 (stcb->asoc.ipv4_addr_legal)) {
4340 if (sctp_add_remote_addr(stcb, sa, 0, 2)) {
4343 } else if ((sa->sa_family == AF_INET6) &&
4344 (stcb->asoc.ipv6_addr_legal)) {
4345 if (sctp_add_remote_addr(stcb, sa, 0, 3)) {
4350 if (net_tmp != NULL && stcb_tmp == stcb) {
4351 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4352 } else if (stcb_tmp != stcb) {
4353 /* It belongs to another association? */
4357 /* since a unlock occured we must check the
4358 * TCB's state and the pcb's gone flags.
4360 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4361 /* the user freed the ep */
4364 if (stcb->asoc.state == 0) {
4365 /* the assoc was freed? */
4369 /* now we must go through each of the params. */
4370 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4372 ptype = ntohs(phdr->param_type);
4373 plen = ntohs(phdr->param_length);
4374 /*printf("ptype => %d, plen => %d\n", ptype, plen);*/
4375 if (offset + plen > limit) {
4381 if ((ptype == SCTP_IPV4_ADDRESS) &&
4382 (stcb->asoc.ipv4_addr_legal)) {
4383 struct sctp_ipv4addr_param *p4, p4_buf;
4384 /* ok get the v4 address and check/add */
4385 phdr = sctp_get_next_param(m, offset,
4386 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4387 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4391 p4 = (struct sctp_ipv4addr_param *)phdr;
4392 sin.sin_addr.s_addr = p4->addr;
4393 sa = (struct sockaddr *)&sin;
4394 inp = stcb->sctp_ep;
4395 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4398 if ((stcb_tmp== NULL && inp == stcb->sctp_ep) ||
4400 /* we must add the source address */
4401 /* no scope set since we have a tcb already */
4403 /* we must validate the state again here */
4404 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4405 /* the user freed the ep */
4408 if (stcb->asoc.state == 0) {
4409 /* the assoc was freed? */
4412 if (sctp_add_remote_addr(stcb, sa, 0, 4)) {
4415 } else if (stcb_tmp == stcb) {
4416 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4417 /* the user freed the ep */
4420 if (stcb->asoc.state == 0) {
4421 /* the assoc was freed? */
4427 ~SCTP_ADDR_NOT_IN_ASSOC;
4430 /* strange, address is in another assoc?
4431 * straighten out locks.
4433 SCTP_TCB_UNLOCK(stcb_tmp);
4434 SCTP_INP_RLOCK(inp);
4435 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4436 /* the user freed the ep */
4437 SCTP_INP_RUNLOCK(l_inp);
4440 if (stcb->asoc.state == 0) {
4441 /* the assoc was freed? */
4442 SCTP_INP_RUNLOCK(l_inp);
4445 SCTP_TCB_LOCK(stcb);
4446 SCTP_INP_RUNLOCK(stcb->sctp_ep);
4449 } else if ((ptype == SCTP_IPV6_ADDRESS) &&
4450 (stcb->asoc.ipv6_addr_legal)) {
4451 /* ok get the v6 address and check/add */
4452 struct sctp_ipv6addr_param *p6, p6_buf;
4453 phdr = sctp_get_next_param(m, offset,
4454 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4455 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4459 p6 = (struct sctp_ipv6addr_param *)phdr;
4460 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4462 sa = (struct sockaddr *)&sin6;
4463 inp = stcb->sctp_ep;
4464 stcb_tmp= sctp_findassociation_ep_addr(&inp, sa, &net,
4466 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4468 /* we must validate the state again here */
4469 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4470 /* the user freed the ep */
4473 if (stcb->asoc.state == 0) {
4474 /* the assoc was freed? */
4477 /* we must add the address, no scope set */
4478 if (sctp_add_remote_addr(stcb, sa, 0, 5)) {
4481 } else if (stcb_tmp == stcb) {
4482 /* we must validate the state again here */
4483 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4484 /* the user freed the ep */
4487 if (stcb->asoc.state == 0) {
4488 /* the assoc was freed? */
4494 ~SCTP_ADDR_NOT_IN_ASSOC;
4497 /* strange, address is in another assoc?
4498 * straighten out locks.
4500 SCTP_TCB_UNLOCK(stcb_tmp);
4501 SCTP_INP_RLOCK(l_inp);
4502 /* we must validate the state again here */
4503 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4504 /* the user freed the ep */
4505 SCTP_INP_RUNLOCK(l_inp);
4508 if (stcb->asoc.state == 0) {
4509 /* the assoc was freed? */
4510 SCTP_INP_RUNLOCK(l_inp);
4513 SCTP_TCB_LOCK(stcb);
4514 SCTP_INP_RUNLOCK(l_inp);
4517 } else if (ptype == SCTP_ECN_CAPABLE) {
4518 stcb->asoc.ecn_allowed = 1;
4519 } else if (ptype == SCTP_ULP_ADAPTION) {
4520 if (stcb->asoc.state != SCTP_STATE_OPEN) {
4521 struct sctp_adaption_layer_indication ai, *aip;
4523 phdr = sctp_get_next_param(m, offset,
4524 (struct sctp_paramhdr *)&ai, sizeof(ai));
4525 aip = (struct sctp_adaption_layer_indication *)phdr;
4526 sctp_ulp_notify(SCTP_NOTIFY_ADAPTION_INDICATION,
4527 stcb, ntohl(aip->indication), NULL);
4529 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4530 struct sctp_asconf_addr_param lstore, *fee;
4531 struct sctp_asconf_addrv4_param *fii;
4533 struct sockaddr *lsa = NULL;
4535 stcb->asoc.peer_supports_asconf = 1;
4536 stcb->asoc.peer_supports_asconf_setprim = 1;
4537 if (plen > sizeof(lstore)) {
4540 phdr = sctp_get_next_param(m, offset,
4541 (struct sctp_paramhdr *)&lstore, plen);
4546 fee = (struct sctp_asconf_addr_param *)phdr;
4547 lptype = ntohs(fee->addrp.ph.param_type);
4548 if (lptype == SCTP_IPV4_ADDRESS) {
4550 sizeof(struct sctp_asconf_addrv4_param)) {
4551 printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4552 (int)sizeof(struct sctp_asconf_addrv4_param),
4555 fii = (struct sctp_asconf_addrv4_param *)fee;
4556 sin.sin_addr.s_addr = fii->addrp.addr;
4557 lsa = (struct sockaddr *)&sin;
4559 } else if (lptype == SCTP_IPV6_ADDRESS) {
4561 sizeof(struct sctp_asconf_addr_param)) {
4562 printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4563 (int)sizeof(struct sctp_asconf_addr_param),
4566 memcpy(sin6.sin6_addr.s6_addr,
4568 sizeof(fee->addrp.addr));
4569 lsa = (struct sockaddr *)&sin6;
4573 sctp_set_primary_addr(stcb, sa, NULL);
4576 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4577 /* Peer supports pr-sctp */
4578 stcb->asoc.peer_supports_prsctp = 1;
4579 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4580 /* A supported extension chunk */
4581 struct sctp_supported_chunk_types_param *pr_supported;
4582 uint8_t local_store[128];
4585 phdr = sctp_get_next_param(m, offset,
4586 (struct sctp_paramhdr *)&local_store, plen);
4590 stcb->asoc.peer_supports_asconf = 0;
4591 stcb->asoc.peer_supports_asconf_setprim = 0;
4592 stcb->asoc.peer_supports_prsctp = 0;
4593 stcb->asoc.peer_supports_pktdrop = 0;
4594 stcb->asoc.peer_supports_strreset = 0;
4595 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4596 num_ent = plen - sizeof(struct sctp_paramhdr);
4597 for (i=0; i<num_ent; i++) {
4598 switch (pr_supported->chunk_types[i]) {
4600 stcb->asoc.peer_supports_asconf = 1;
4601 stcb->asoc.peer_supports_asconf_setprim = 1;
4603 case SCTP_ASCONF_ACK:
4604 stcb->asoc.peer_supports_asconf = 1;
4605 stcb->asoc.peer_supports_asconf_setprim = 1;
4607 case SCTP_FORWARD_CUM_TSN:
4608 stcb->asoc.peer_supports_prsctp = 1;
4610 case SCTP_PACKET_DROPPED:
4611 stcb->asoc.peer_supports_pktdrop = 1;
4613 case SCTP_STREAM_RESET:
4614 stcb->asoc.peer_supports_strreset = 1;
4617 /* one I have not learned yet */
4622 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4623 /* Peer supports ECN-nonce */
4624 stcb->asoc.peer_supports_ecn_nonce = 1;
4625 stcb->asoc.ecn_nonce_allowed = 1;
4626 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4627 (ptype == SCTP_STATE_COOKIE) ||
4628 (ptype == SCTP_UNRECOG_PARAM) ||
4629 (ptype == SCTP_COOKIE_PRESERVE) ||
4630 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4631 (ptype == SCTP_ADD_IP_ADDRESS) ||
4632 (ptype == SCTP_DEL_IP_ADDRESS) ||
4633 (ptype == SCTP_ERROR_CAUSE_IND) ||
4634 (ptype == SCTP_SUCCESS_REPORT)) {
4637 if ((ptype & 0x8000) == 0x0000) {
4638 /* must stop processing the rest of
4639 * the param's. Any report bits were
4640 * handled with the call to sctp_arethere_unrecognized_parameters()
4641 * when the INIT or INIT-ACK was first seen.
4646 offset += SCTP_SIZE32(plen);
4647 if (offset >= limit) {
4650 phdr = sctp_get_next_param(m, offset, &parm_buf,
4653 /* Now check to see if we need to purge any addresses */
4654 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
4655 net_tmp = TAILQ_NEXT(net, sctp_next);
4656 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
4657 SCTP_ADDR_NOT_IN_ASSOC) {
4658 /* This address has been removed from the asoc */
4659 /* remove and free it */
4660 stcb->asoc.numnets--;
4661 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
4662 sctp_free_remote_addr(net);
4663 if (net == stcb->asoc.primary_destination) {
4664 stcb->asoc.primary_destination = NULL;
4665 sctp_select_primary_destination(stcb);
4673 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
4674 struct sctp_nets *net)
4676 /* make sure the requested primary address exists in the assoc */
4677 if (net == NULL && sa)
4678 net = sctp_findnet(stcb, sa);
4681 /* didn't find the requested primary address! */
4684 /* set the primary address */
4685 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
4686 /* Must be confirmed */
4689 stcb->asoc.primary_destination = net;
4690 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
4697 sctp_is_vtag_good(struct sctp_inpcb *inp, u_int32_t tag, struct timeval *now)
4700 * This function serves two purposes. It will see if a TAG can be
4701 * re-used and return 1 for yes it is ok and 0 for don't use that
4703 * A secondary function it will do is purge out old tags that can
4706 struct sctpasochead *head;
4707 struct sctpvtaghead *chain;
4708 struct sctp_tagblock *twait_block;
4709 struct sctp_tcb *stcb;
4712 SCTP_INP_INFO_WLOCK();
4713 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4714 /* First is the vtag in use ? */
4716 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
4717 sctppcbinfo.hashasocmark)];
4719 SCTP_INP_INFO_WUNLOCK();
4722 LIST_FOREACH(stcb, head, sctp_asocs) {
4723 if (stcb->asoc.my_vtag == tag) {
4724 /* We should remove this if and
4725 * return 0 always if we want vtags
4726 * unique across all endpoints. For
4727 * now within a endpoint is ok.
4729 if (inp == stcb->sctp_ep) {
4730 /* bad tag, in use */
4731 SCTP_INP_INFO_WUNLOCK();
4736 if (!LIST_EMPTY(chain)) {
4738 * Block(s) are present, lets see if we have this tag in
4741 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4742 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4743 if (twait_block->vtag_block[i].v_tag == 0) {
4746 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
4748 /* Audit expires this guy */
4749 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4750 twait_block->vtag_block[i].v_tag = 0;
4751 } else if (twait_block->vtag_block[i].v_tag ==
4753 /* Bad tag, sorry :< */
4754 SCTP_INP_INFO_WUNLOCK();
4760 /* Not found, ok to use the tag */
4761 SCTP_INP_INFO_WUNLOCK();
4767 * Delete the address from the endpoint local address list
4768 * Lookup using a sockaddr address (ie. not an ifaddr)
4771 sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
4773 struct sctp_laddr *laddr;
4774 struct sockaddr *l_sa;
4776 /* Here is another function I cannot find a
4777 * caller for. As such we SHOULD delete it
4778 * if we have no users. If we find a user that
4779 * user MUST have the INP locked.
4783 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4784 /* You are already bound to all. You have it already */
4788 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4789 /* make sure the address exists */
4790 if (laddr->ifa == NULL)
4792 if (laddr->ifa->ifa_addr == NULL)
4795 l_sa = laddr->ifa->ifa_addr;
4796 if (l_sa->sa_family == AF_INET6) {
4798 struct sockaddr_in6 *sin1, *sin2;
4799 sin1 = (struct sockaddr_in6 *)l_sa;
4800 sin2 = (struct sockaddr_in6 *)sa;
4801 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4802 sizeof(struct in6_addr)) == 0) {
4807 } else if (l_sa->sa_family == AF_INET) {
4809 struct sockaddr_in *sin1, *sin2;
4810 sin1 = (struct sockaddr_in *)l_sa;
4811 sin2 = (struct sockaddr_in *)sa;
4812 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4818 /* invalid family */
4823 if (found && inp->laddr_count < 2) {
4824 /* can't delete unless there are at LEAST 2 addresses */
4828 if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4830 * remove it from the ep list, this should NOT be
4831 * done until its really gone from the interface list and
4832 * we won't be receiving more of these. Probably right
4833 * away. If we do allow a removal of an address from
4834 * an association (sub-set bind) than this should NOT
4835 * be called until the all ASCONF come back from this
4838 sctp_remove_laddr(laddr);
4846 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4849 * We must hunt this association for MBUF's past the cumack
4850 * (i.e. out of order data that we can renege on).
4852 struct sctp_association *asoc;
4853 struct sctp_tmit_chunk *chk, *nchk;
4854 u_int32_t cumulative_tsn_p1, tsn;
4855 int cnt, strmat, gap;
4856 /* We look for anything larger than the cum-ack + 1 */
4859 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
4861 /* First look in the re-assembly queue */
4862 chk = TAILQ_FIRST(&asoc->reasmqueue);
4864 /* Get the next one */
4865 nchk = TAILQ_NEXT(chk, sctp_next);
4866 if (compare_with_wrap(chk->rec.data.TSN_seq,
4867 cumulative_tsn_p1, MAX_TSN)) {
4868 /* Yep it is above cum-ack */
4870 tsn = chk->rec.data.TSN_seq;
4871 if (tsn >= asoc->mapping_array_base_tsn) {
4872 gap = tsn - asoc->mapping_array_base_tsn;
4874 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
4877 asoc->size_on_reasm_queue -= chk->send_size;
4878 asoc->cnt_on_reasm_queue--;
4879 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
4880 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4882 sctp_m_freem(chk->data);
4885 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4886 sctppcbinfo.ipi_count_chunk--;
4887 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4888 panic("Chunk count is negative");
4890 sctppcbinfo.ipi_gencnt_chunk++;
4894 /* Ok that was fun, now we will drain all the inbound streams? */
4895 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
4896 chk = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
4898 nchk = TAILQ_NEXT(chk, sctp_next);
4899 if (compare_with_wrap(chk->rec.data.TSN_seq,
4900 cumulative_tsn_p1, MAX_TSN)) {
4901 /* Yep it is above cum-ack */
4903 tsn = chk->rec.data.TSN_seq;
4904 if (tsn >= asoc->mapping_array_base_tsn) {
4906 asoc->mapping_array_base_tsn;
4909 asoc->mapping_array_base_tsn) +
4912 asoc->size_on_all_streams -= chk->send_size;
4913 asoc->cnt_on_all_streams--;
4915 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
4917 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
4920 sctp_m_freem(chk->data);
4923 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4924 sctppcbinfo.ipi_count_chunk--;
4925 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4926 panic("Chunk count is negative");
4928 sctppcbinfo.ipi_gencnt_chunk++;
4934 * Question, should we go through the delivery queue?
4935 * The only reason things are on here is the app not reading OR a
4936 * p-d-api up. An attacker COULD send enough in to initiate the
4937 * PD-API and then send a bunch of stuff to other streams... these
4938 * would wind up on the delivery queue.. and then we would not get
4939 * to them. But in order to do this I then have to back-track and
4940 * un-deliver sequence numbers in streams.. el-yucko. I think for
4941 * now we will NOT look at the delivery queue and leave it to be
4942 * something to consider later. An alternative would be to abort
4943 * the P-D-API with a notification and then deliver the data....
4944 * Or another method might be to keep track of how many times the
4945 * situation occurs and if we see a possible attack underway just
4946 * abort the association.
4949 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
4951 printf("Freed %d chunks from reneg harvest\n", cnt);
4954 #endif /* SCTP_DEBUG */
4957 * Another issue, in un-setting the TSN's in the mapping array we
4958 * DID NOT adjust the higest_tsn marker. This will cause one of
4959 * two things to occur. It may cause us to do extra work in checking
4960 * for our mapping array movement. More importantly it may cause us
4961 * to SACK every datagram. This may not be a bad thing though since
4962 * we will recover once we get our cum-ack above and all this stuff
4963 * we dumped recovered.
4971 * We must walk the PCB lists for ALL associations here. The system
4972 * is LOW on MBUF's and needs help. This is where reneging will
4973 * occur. We really hope this does NOT happen!
4975 struct sctp_inpcb *inp;
4976 struct sctp_tcb *stcb;
4978 SCTP_INP_INFO_RLOCK();
4979 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
4980 /* For each endpoint */
4981 SCTP_INP_RLOCK(inp);
4982 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4983 /* For each association */
4984 SCTP_TCB_LOCK(stcb);
4985 sctp_drain_mbufs(inp, stcb);
4986 SCTP_TCB_UNLOCK(stcb);
4988 SCTP_INP_RUNLOCK(inp);
4990 SCTP_INP_INFO_RUNLOCK();
4994 sctp_add_to_socket_q(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4996 struct sctp_socket_q_list *sq;
4998 /* write lock on INP assumed */
4999 if ((inp == NULL) || (stcb == NULL)) {
5003 sq = (struct sctp_socket_q_list *)SCTP_ZONE_GET(
5004 sctppcbinfo.ipi_zone_sockq);
5006 /* out of sq structs */
5009 sctppcbinfo.ipi_count_sockq++;
5010 sctppcbinfo.ipi_gencnt_sockq++;
5012 stcb->asoc.cnt_msg_on_sb++;
5014 TAILQ_INSERT_TAIL(&inp->sctp_queue_list, sq, next_sq);
5020 sctp_remove_from_socket_q(struct sctp_inpcb *inp)
5022 struct sctp_tcb *stcb = NULL;
5023 struct sctp_socket_q_list *sq;
5025 /* W-Lock on INP assumed held */
5026 sq = TAILQ_FIRST(&inp->sctp_queue_list);
5031 TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
5032 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
5033 sctppcbinfo.ipi_count_sockq--;
5034 sctppcbinfo.ipi_gencnt_sockq++;
5036 stcb->asoc.cnt_msg_on_sb--;
5042 sctp_initiate_iterator(asoc_func af, uint32_t pcb_state, uint32_t asoc_state,
5043 void *argp, uint32_t argi, end_func ef,
5044 struct sctp_inpcb *s_inp)
5046 struct sctp_iterator *it=NULL;
5051 MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), M_PCB,
5056 memset(it, 0, sizeof(*it));
5057 it->function_toapply = af;
5058 it->function_atend = ef;
5061 it->pcb_flags = pcb_state;
5062 it->asoc_state = asoc_state;
5065 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5067 SCTP_INP_INFO_RLOCK();
5068 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5069 SCTP_INP_INFO_RUNLOCK();
5070 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5073 /* Init the timer */
5074 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
5075 callout_init(&it->tmr.timer, 0);
5077 callout_init(&it->tmr.timer);
5079 /* add to the list of all iterators */
5080 SCTP_INP_INFO_WLOCK();
5081 LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5082 SCTP_INP_INFO_WUNLOCK();
5083 #if defined(__NetBSD__) || defined(__OpenBSD__)
5088 sctp_iterator_timer(it);
5095 * Callout/Timer routines for OS that doesn't have them
5097 #ifdef _SCTP_NEEDS_CALLOUT_
5103 callout_init(struct callout *c)
5105 bzero(c, sizeof(*c));
5109 callout_reset(struct callout *c, int to_ticks, void (*ftn)(void *), void *arg)
5114 if (c->c_flags & CALLOUT_PENDING)
5118 * We could spl down here and back up at the TAILQ_INSERT_TAIL,
5119 * but there's no point since doing this setup doesn't take much
5126 c->c_flags = (CALLOUT_ACTIVE | CALLOUT_PENDING);
5129 c->c_time = to_ticks; /* just store the requested timeout */
5130 timeout(ftn, arg, to_ticks);
5132 c->c_time = ticks + to_ticks;
5133 TAILQ_INSERT_TAIL(&sctppcbinfo.callqueue, c, tqe);
5139 callout_stop(struct callout *c)
5145 * Don't attempt to delete a callout that's not on the queue.
5147 if (!(c->c_flags & CALLOUT_PENDING)) {
5148 c->c_flags &= ~CALLOUT_ACTIVE;
5152 c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING| CALLOUT_FIRED);
5154 /* thread_call_cancel(c->c_call); */
5155 untimeout(c->c_func, c->c_arg);
5157 TAILQ_REMOVE(&sctppcbinfo.callqueue, c, tqe);
5164 #if !defined(__APPLE__)
5168 struct callout *c, *n;
5169 struct calloutlist locallist;
5173 /* run through and subtract and mark all callouts */
5174 c = TAILQ_FIRST(&sctppcbinfo.callqueue);
5176 n = TAILQ_NEXT(c, tqe);
5177 if (c->c_time <= ticks) {
5178 c->c_flags |= CALLOUT_FIRED;
5180 TAILQ_REMOVE(&sctppcbinfo.callqueue, c, tqe);
5182 TAILQ_INIT(&locallist);
5185 /* move off of main list */
5186 TAILQ_INSERT_TAIL(&locallist, c, tqe);
5190 /* Now all the ones on the locallist must be called */
5192 c = TAILQ_FIRST(&locallist);
5195 TAILQ_REMOVE(&locallist, c, tqe);
5196 /* now validate that it did not get canceled */
5197 if (c->c_flags & CALLOUT_FIRED) {
5198 c->c_flags &= ~CALLOUT_PENDING;
5200 (*c->c_func)(c->c_arg);
5203 c = TAILQ_FIRST(&locallist);
5209 #endif /* _SCTP_NEEDS_CALLOUT_ */