2 * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_pcb.c,v 1.38 2005/03/06 16:04:18 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_pcb.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp.h>
43 #include <netinet/sctp_header.h>
44 #include <netinet/sctp_asconf.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_timer.h>
47 #include <netinet/sctp_bsd_addr.h>
50 struct sctp_epinfo sctppcbinfo;
52 /* FIX: we don't handle multiple link local scopes */
53 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
55 SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b)
57 struct in6_addr tmp_a, tmp_b;
59 /* use a copy of a and b */
62 in6_clearscope(&tmp_a);
63 in6_clearscope(&tmp_b);
64 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
68 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
71 * We really don't need to lock this, but I will just because it
74 SCTP_INP_INFO_RLOCK();
75 spcb->ep_count = sctppcbinfo.ipi_count_ep;
76 spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
77 spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
78 spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
79 spcb->chk_count = sctppcbinfo.ipi_count_chunk;
80 spcb->readq_count = sctppcbinfo.ipi_count_readq;
81 spcb->stream_oque = sctppcbinfo.ipi_count_strmoq;
82 spcb->free_chunks = sctppcbinfo.ipi_free_chunks;
84 SCTP_INP_INFO_RUNLOCK();
88 * Addresses are added to VRF's (Virtual Router's). For BSD we
89 * have only the default VRF 0. We maintain a hash list of
90 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
91 * these has a list of addresses. When we add a new address
92 * to a VRF we lookup the ifn/ifn_index, if the ifn does
93 * not exist we create it and add it to the list of IFN's
94 * within the VRF. Once we have the sctp_ifn, we add the
95 * address to the list. So we look something like:
98 * vrf-> ifn-> ifn -> ifn
100 * ... +--ifa-> ifa -> ifa
103 * We keep these seperate lists since the SCTP subsystem will
104 * point to these from its source address selection nets structure.
105 * When an address is deleted it does not happen right away on
106 * the SCTP side, it gets scheduled. What we do when a
107 * delete happens is immediately remove the address from
108 * the master list and decrement the refcount. As our
109 * addip iterator works through and frees the src address
110 * selection pointing to the sctp_ifa, eventually the refcount
111 * will reach 0 and we will delete it. Note that it is assumed
112 * that any locking on system level ifn/ifa is done at the
113 * caller of these functions and these routines will only
114 * lock the SCTP structures as they add or delete things.
116 * Other notes on VRF concepts.
117 * - An endpoint can be in multiple VRF's
118 * - An association lives within a VRF and only one VRF.
119 * - Any incoming packet we can deduce the VRF for by
120 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
121 * - Any downward send call or connect call must supply the
122 * VRF via ancillary data or via some sort of set default
123 * VRF socket option call (again for BSD no brainer since
124 * the VRF is always 0).
125 * - An endpoint may add multiple VRF's to it.
126 * - Listening sockets can accept associations in any
127 * of the VRF's they are in but the assoc will end up
128 * in only one VRF (gotten from the packet or connect/send).
133 sctp_allocate_vrf(int vrfid)
135 struct sctp_vrf *vrf = NULL;
136 struct sctp_vrflist *bucket;
138 /* First allocate the VRF structure */
139 vrf = sctp_find_vrf(vrfid);
141 /* Already allocated */
144 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
149 panic("No memory for VRF:%d", vrfid);
154 memset(vrf, 0, sizeof(struct sctp_vrf));
156 LIST_INIT(&vrf->ifnlist);
157 vrf->total_ifa_count = 0;
158 /* Init the HASH of addresses */
159 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_HASH_SIZE,
161 if (vrf->vrf_addr_hash == NULL) {
164 panic("No memory for VRF:%d", vrfid);
168 /* Add it to the hash table */
169 bucket = &sctppcbinfo.sctp_vrfhash[(vrfid & sctppcbinfo.hashvrfmark)];
170 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
176 sctp_find_ifn(struct sctp_vrf *vrf, void *ifn, uint32_t ifn_index)
178 struct sctp_ifn *sctp_ifnp;
181 * We assume the lock is held for the addresses if thats wrong
182 * problems could occur :-)
184 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
185 if (sctp_ifnp->ifn_index == ifn_index) {
188 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
196 sctp_find_vrf(uint32_t vrfid)
198 struct sctp_vrflist *bucket;
199 struct sctp_vrf *liste;
201 bucket = &sctppcbinfo.sctp_vrfhash[(vrfid & sctppcbinfo.hashvrfmark)];
202 LIST_FOREACH(liste, bucket, next_vrf) {
203 if (vrfid == liste->vrf_id) {
212 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
216 ret = atomic_fetchadd_int(&sctp_ifap->refcount, -1);
218 /* We zero'd the count */
220 if (sctp_ifap->in_ifa_list) {
221 panic("Attempt to free item in a list");
224 if (sctp_ifap->in_ifa_list) {
225 printf("in_ifa_list was not clear, fixing cnt\n");
226 atomic_add_int(&sctp_ifap->refcount, 1);
230 SCTP_FREE(sctp_ifap);
235 sctp_add_addr_to_vrf(uint32_t vrfid, void *ifn, uint32_t ifn_index,
236 uint32_t ifn_type, const char *if_name,
237 void *ifa, struct sockaddr *addr, uint32_t ifa_flags)
239 struct sctp_vrf *vrf;
240 struct sctp_ifn *sctp_ifnp = NULL;
241 struct sctp_ifa *sctp_ifap = NULL;
242 struct sctp_ifalist *hash_head;
243 uint32_t hash_of_addr;
245 /* How granular do we need the locks to be here? */
246 SCTP_IPI_ADDR_LOCK();
247 vrf = sctp_find_vrf(vrfid);
249 vrf = sctp_allocate_vrf(vrfid);
251 SCTP_IPI_ADDR_UNLOCK();
255 sctp_ifnp = sctp_find_ifn(vrf, ifn, ifn_index);
256 if (sctp_ifnp == NULL) {
258 * build one and add it, can't hold lock until after malloc
261 SCTP_IPI_ADDR_UNLOCK();
262 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, sizeof(struct sctp_ifn), "SCTP_IFN");
263 if (sctp_ifnp == NULL) {
265 panic("No memory for IFN:%u", sctp_ifnp->ifn_index);
269 sctp_ifnp->ifn_index = ifn_index;
270 sctp_ifnp->ifn_p = ifn;
271 sctp_ifnp->ifn_type = ifn_type;
272 sctp_ifnp->ifa_count = 0;
273 sctp_ifnp->refcount = 0;
274 sctp_ifnp->vrf = vrf;
275 memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ);
276 LIST_INIT(&sctp_ifnp->ifalist);
277 SCTP_IPI_ADDR_LOCK();
278 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
280 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
282 /* Hmm, it already exists? */
283 if ((sctp_ifap->ifn_p) &&
284 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
285 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
286 /* easy to solve, just switch back to active */
287 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
288 sctp_ifap->ifn_p = sctp_ifnp;
290 SCTP_IPI_ADDR_UNLOCK();
293 goto exit_stage_left;
296 if (sctp_ifap->ifn_p) {
298 * The first IFN gets the address,
299 * duplicates are ignored.
301 goto exit_stage_left;
303 /* repair ifnp which was NULL ? */
304 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
305 sctp_ifap->ifn_p = sctp_ifnp;
306 atomic_add_int(&sctp_ifnp->refcount, 1);
308 goto exit_stage_left;
311 SCTP_IPI_ADDR_UNLOCK();
312 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), "SCTP_IFA");
313 if (sctp_ifap == NULL) {
315 panic("No memory for IFA");
319 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
320 sctp_ifap->ifn_p = sctp_ifnp;
321 atomic_add_int(&sctp_ifnp->refcount, 1);
323 sctp_ifap->ifa = ifa;
324 memcpy(&sctp_ifap->address, addr, addr->sa_len);
325 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
326 sctp_ifap->flags = ifa_flags;
328 if (sctp_ifap->address.sa.sa_family == AF_INET) {
329 struct sockaddr_in *sin;
331 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
332 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
333 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
334 sctp_ifap->src_is_loop = 1;
336 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
337 sctp_ifap->src_is_priv = 1;
339 } else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
340 /* ok to use deprecated addresses? */
341 struct sockaddr_in6 *sin6;
343 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
344 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
345 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
346 sctp_ifap->src_is_loop = 1;
348 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
349 sctp_ifap->src_is_priv = 1;
352 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
354 if ((sctp_ifap->src_is_priv == 0) &&
355 (sctp_ifap->src_is_loop == 0)) {
356 sctp_ifap->src_is_glob = 1;
358 SCTP_IPI_ADDR_LOCK();
359 hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_hashmark)];
360 LIST_INSERT_HEAD(hash_head, sctp_ifap, next_bucket);
361 sctp_ifap->refcount = 1;
362 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
363 sctp_ifnp->ifa_count++;
364 sctp_ifap->in_ifa_list = 1;
365 vrf->total_ifa_count++;
366 SCTP_IPI_ADDR_UNLOCK();
371 sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
374 struct sctp_vrf *vrf;
375 struct sctp_ifa *sctp_ifap = NULL;
376 struct sctp_ifn *sctp_ifnp = NULL;
378 SCTP_IPI_ADDR_LOCK();
380 vrf = sctp_find_vrf(vrfid);
382 printf("Can't find vrfid:%d\n", vrfid);
385 sctp_ifnp = sctp_find_ifn(vrf, (void *)NULL, ifn_index);
386 if (sctp_ifnp == NULL) {
387 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
389 sctp_ifap = sctp_find_ifa_in_ifn(sctp_ifnp, addr, 1);
393 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
394 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
395 sctp_ifnp->ifa_count--;
396 vrf->total_ifa_count--;
397 LIST_REMOVE(sctp_ifap, next_bucket);
398 LIST_REMOVE(sctp_ifap, next_ifa);
399 sctp_ifap->in_ifa_list = 0;
400 atomic_add_int(&sctp_ifnp->refcount, -1);
402 printf("Del Addr-ifn:%d Could not find address:",
404 sctp_print_address(addr);
407 SCTP_IPI_ADDR_UNLOCK();
412 * Notes on locks for FreeBSD 5 and up. All association lookups that have a
413 * definte ep, the INP structure is assumed to be locked for reading. If we
414 * need to go find the INP (ususally when a **inp is passed) then we must
415 * lock the INFO structure first and if needed lock the INP too. Note that if
422 * Given a endpoint, look and find in its association list any association
423 * with the "to" address given. This can be a "from" address, too, for
424 * inbound packets. For outbound packets it is a true "to" address.
427 static struct sctp_tcb *
428 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
429 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
431 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
434 * Note for this module care must be taken when observing what to is
435 * for. In most of the rest of the code the TO field represents my
436 * peer and the FROM field represents my address. For this module it
437 * is reversed of that.
440 * If we support the TCP model, then we must now dig through to see
441 * if we can find our endpoint in the list of tcp ep's.
443 uint16_t lport, rport;
444 struct sctppcbhead *ephead;
445 struct sctp_inpcb *inp;
446 struct sctp_laddr *laddr;
447 struct sctp_tcb *stcb;
448 struct sctp_nets *net;
450 if ((to == NULL) || (from == NULL)) {
453 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
454 lport = ((struct sockaddr_in *)to)->sin_port;
455 rport = ((struct sockaddr_in *)from)->sin_port;
456 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
457 lport = ((struct sockaddr_in6 *)to)->sin6_port;
458 rport = ((struct sockaddr_in6 *)from)->sin6_port;
462 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
463 (lport + rport), sctppcbinfo.hashtcpmark)];
465 * Ok now for each of the guys in this bucket we must look and see:
466 * - Does the remote port match. - Does there single association's
467 * addresses match this address (to). If so we update p_ep to point
468 * to this ep and return the tcb from it.
470 LIST_FOREACH(inp, ephead, sctp_hash) {
471 if (lport != inp->sctp_lport) {
475 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
476 SCTP_INP_RUNLOCK(inp);
479 if (inp->def_vrf_id != vrf_id) {
480 SCTP_INP_RUNLOCK(inp);
483 /* check to see if the ep has one of the addresses */
484 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
485 /* We are NOT bound all, so look further */
488 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
490 if (laddr->ifa == NULL) {
492 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
493 printf("An ounce of prevention is worth a pound of cure\n");
498 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
500 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
501 printf("ifa being deleted\n");
506 if (laddr->ifa->address.sa.sa_family ==
508 /* see if it matches */
509 struct sockaddr_in *intf_addr, *sin;
511 intf_addr = &laddr->ifa->address.sin;
512 sin = (struct sockaddr_in *)to;
513 if (from->sa_family == AF_INET) {
514 if (sin->sin_addr.s_addr ==
515 intf_addr->sin_addr.s_addr) {
520 struct sockaddr_in6 *intf_addr6;
521 struct sockaddr_in6 *sin6;
523 sin6 = (struct sockaddr_in6 *)
525 intf_addr6 = &laddr->ifa->address.sin6;
527 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
528 &intf_addr6->sin6_addr)) {
536 /* This endpoint does not have this address */
537 SCTP_INP_RUNLOCK(inp);
542 * Ok if we hit here the ep has the address, does it hold
546 stcb = LIST_FIRST(&inp->sctp_asoc_list);
548 SCTP_INP_RUNLOCK(inp);
552 if (stcb->rport != rport) {
553 /* remote port does not match. */
554 SCTP_TCB_UNLOCK(stcb);
555 SCTP_INP_RUNLOCK(inp);
558 /* Does this TCB have a matching address? */
559 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
561 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
562 /* not the same family, can't be a match */
565 if (from->sa_family == AF_INET) {
566 struct sockaddr_in *sin, *rsin;
568 sin = (struct sockaddr_in *)&net->ro._l_addr;
569 rsin = (struct sockaddr_in *)from;
570 if (sin->sin_addr.s_addr ==
571 rsin->sin_addr.s_addr) {
576 /* Update the endpoint pointer */
578 SCTP_INP_RUNLOCK(inp);
582 struct sockaddr_in6 *sin6, *rsin6;
584 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
585 rsin6 = (struct sockaddr_in6 *)from;
586 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
587 &rsin6->sin6_addr)) {
592 /* Update the endpoint pointer */
594 SCTP_INP_RUNLOCK(inp);
599 SCTP_TCB_UNLOCK(stcb);
600 SCTP_INP_RUNLOCK(inp);
608 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
609 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
610 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
615 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
616 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
618 struct sctpasochead *head;
619 struct sctp_inpcb *inp;
620 struct sctp_tcb *stcb = NULL;
621 struct sctp_nets *net;
625 if (remote->sa_family == AF_INET) {
626 rport = (((struct sockaddr_in *)remote)->sin_port);
627 } else if (remote->sa_family == AF_INET6) {
628 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
634 * UN-lock so we can do proper locking here this occurs when
635 * called from load_addresses_from_init.
637 SCTP_TCB_UNLOCK(locked_tcb);
639 SCTP_INP_INFO_RLOCK();
640 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
642 * Now either this guy is our listener or it's the
643 * connector. If it is the one that issued the connect, then
644 * it's only chance is to be the first TCB in the list. If
645 * it is the acceptor, then do the special_lookup to hash
646 * and find the real inp.
648 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
649 /* to is peer addr, from is my addr */
650 stcb = sctp_tcb_special_locate(inp_p, remote, local,
651 netp, inp->def_vrf_id);
652 if ((stcb != NULL) && (locked_tcb == NULL)) {
653 /* we have a locked tcb, lower refcount */
655 SCTP_INP_DECR_REF(inp);
656 SCTP_INP_WUNLOCK(inp);
658 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
659 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
660 SCTP_TCB_LOCK(locked_tcb);
661 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
663 SCTP_INP_INFO_RUNLOCK();
667 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
670 stcb = LIST_FIRST(&inp->sctp_asoc_list);
675 if (stcb->rport != rport) {
676 /* remote port does not match. */
677 SCTP_TCB_UNLOCK(stcb);
680 /* now look at the list of remote addresses */
681 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
683 if (net == (TAILQ_NEXT(net, sctp_next))) {
684 panic("Corrupt net list");
687 if (net->ro._l_addr.sa.sa_family !=
689 /* not the same family */
692 if (remote->sa_family == AF_INET) {
693 struct sockaddr_in *sin, *rsin;
695 sin = (struct sockaddr_in *)
697 rsin = (struct sockaddr_in *)remote;
698 if (sin->sin_addr.s_addr ==
699 rsin->sin_addr.s_addr) {
704 if (locked_tcb == NULL) {
705 SCTP_INP_DECR_REF(inp);
706 } else if (locked_tcb != stcb) {
707 SCTP_TCB_LOCK(locked_tcb);
709 SCTP_INP_WUNLOCK(inp);
710 SCTP_INP_INFO_RUNLOCK();
713 } else if (remote->sa_family == AF_INET6) {
714 struct sockaddr_in6 *sin6, *rsin6;
716 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
717 rsin6 = (struct sockaddr_in6 *)remote;
718 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
719 &rsin6->sin6_addr)) {
724 if (locked_tcb == NULL) {
725 SCTP_INP_DECR_REF(inp);
726 } else if (locked_tcb != stcb) {
727 SCTP_TCB_LOCK(locked_tcb);
729 SCTP_INP_WUNLOCK(inp);
730 SCTP_INP_INFO_RUNLOCK();
735 SCTP_TCB_UNLOCK(stcb);
739 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
742 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
743 inp->sctp_hashmark)];
747 LIST_FOREACH(stcb, head, sctp_tcbhash) {
748 if (stcb->rport != rport) {
749 /* remote port does not match */
752 /* now look at the list of remote addresses */
754 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
756 if (net == (TAILQ_NEXT(net, sctp_next))) {
757 panic("Corrupt net list");
760 if (net->ro._l_addr.sa.sa_family !=
762 /* not the same family */
765 if (remote->sa_family == AF_INET) {
766 struct sockaddr_in *sin, *rsin;
768 sin = (struct sockaddr_in *)
770 rsin = (struct sockaddr_in *)remote;
771 if (sin->sin_addr.s_addr ==
772 rsin->sin_addr.s_addr) {
777 if (locked_tcb == NULL) {
778 SCTP_INP_DECR_REF(inp);
779 } else if (locked_tcb != stcb) {
780 SCTP_TCB_LOCK(locked_tcb);
782 SCTP_INP_WUNLOCK(inp);
783 SCTP_INP_INFO_RUNLOCK();
786 } else if (remote->sa_family == AF_INET6) {
787 struct sockaddr_in6 *sin6, *rsin6;
789 sin6 = (struct sockaddr_in6 *)
791 rsin6 = (struct sockaddr_in6 *)remote;
792 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
793 &rsin6->sin6_addr)) {
798 if (locked_tcb == NULL) {
799 SCTP_INP_DECR_REF(inp);
800 } else if (locked_tcb != stcb) {
801 SCTP_TCB_LOCK(locked_tcb);
803 SCTP_INP_WUNLOCK(inp);
804 SCTP_INP_INFO_RUNLOCK();
809 SCTP_TCB_UNLOCK(stcb);
813 /* clean up for returning null */
815 SCTP_TCB_LOCK(locked_tcb);
817 SCTP_INP_WUNLOCK(inp);
818 SCTP_INP_INFO_RUNLOCK();
824 * Find an association for a specific endpoint using the association id given
825 * out in the COMM_UP notification
829 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
832 * Use my the assoc_id to find a endpoint
834 struct sctpasochead *head;
835 struct sctp_tcb *stcb;
838 if (asoc_id == 0 || inp == NULL) {
841 SCTP_INP_INFO_RLOCK();
842 id = (uint32_t) asoc_id;
843 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
844 sctppcbinfo.hashasocmark)];
846 /* invalid id TSNH */
847 SCTP_INP_INFO_RUNLOCK();
850 LIST_FOREACH(stcb, head, sctp_asocs) {
851 SCTP_INP_RLOCK(stcb->sctp_ep);
852 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
853 SCTP_INP_RUNLOCK(stcb->sctp_ep);
854 SCTP_INP_INFO_RUNLOCK();
857 if (stcb->asoc.assoc_id == id) {
859 if (inp != stcb->sctp_ep) {
861 * some other guy has the same id active (id
864 SCTP_INP_RUNLOCK(stcb->sctp_ep);
870 SCTP_INP_RUNLOCK(stcb->sctp_ep);
871 SCTP_INP_INFO_RUNLOCK();
874 SCTP_INP_RUNLOCK(stcb->sctp_ep);
876 /* Ok if we missed here, lets try the restart hash */
877 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
879 /* invalid id TSNH */
880 SCTP_INP_INFO_RUNLOCK();
883 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
884 SCTP_INP_RLOCK(stcb->sctp_ep);
885 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
886 SCTP_INP_RUNLOCK(stcb->sctp_ep);
887 SCTP_INP_INFO_RUNLOCK();
891 SCTP_INP_RUNLOCK(stcb->sctp_ep);
892 if (stcb->asoc.assoc_id == id) {
894 if (inp != stcb->sctp_ep) {
896 * some other guy has the same id active (id
899 SCTP_TCB_UNLOCK(stcb);
902 SCTP_INP_INFO_RUNLOCK();
905 SCTP_TCB_UNLOCK(stcb);
907 SCTP_INP_INFO_RUNLOCK();
912 static struct sctp_inpcb *
913 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
914 uint16_t lport, uint32_t vrf_id)
916 struct sctp_inpcb *inp;
917 struct sockaddr_in *sin;
918 struct sockaddr_in6 *sin6;
919 struct sctp_laddr *laddr;
923 * Endpoing probe expects that the INP_INFO is locked.
925 if (nam->sa_family == AF_INET) {
926 sin = (struct sockaddr_in *)nam;
928 } else if (nam->sa_family == AF_INET6) {
929 sin6 = (struct sockaddr_in6 *)nam;
932 /* unsupported family */
937 LIST_FOREACH(inp, head, sctp_hash) {
939 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
940 SCTP_INP_RUNLOCK(inp);
943 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
944 (inp->sctp_lport == lport)) {
946 if ((nam->sa_family == AF_INET) &&
947 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
948 SCTP_IPV6_V6ONLY(inp)) {
949 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
950 SCTP_INP_RUNLOCK(inp);
953 /* A V6 address and the endpoint is NOT bound V6 */
954 if (nam->sa_family == AF_INET6 &&
955 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
956 SCTP_INP_RUNLOCK(inp);
959 /* does a VRF id match? */
961 if (inp->def_vrf_id == vrf_id)
964 SCTP_INP_RUNLOCK(inp);
969 SCTP_INP_RUNLOCK(inp);
972 if ((nam->sa_family == AF_INET) &&
973 (sin->sin_addr.s_addr == INADDR_ANY)) {
974 /* Can't hunt for one that has no address specified */
976 } else if ((nam->sa_family == AF_INET6) &&
977 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
978 /* Can't hunt for one that has no address specified */
982 * ok, not bound to all so see if we can find a EP bound to this
985 LIST_FOREACH(inp, head, sctp_hash) {
987 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
988 SCTP_INP_RUNLOCK(inp);
991 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
992 SCTP_INP_RUNLOCK(inp);
996 * Ok this could be a likely candidate, look at all of its
999 if (inp->sctp_lport != lport) {
1000 SCTP_INP_RUNLOCK(inp);
1003 /* does a VRF id match? */
1005 if (inp->def_vrf_id == vrf_id)
1009 SCTP_INP_RUNLOCK(inp);
1012 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1013 if (laddr->ifa == NULL) {
1015 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1016 printf("An ounce of prevention is worth a pound of cure\n");
1022 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1023 printf("Ok laddr->ifa:%p is possible, ",
1027 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1029 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1030 printf("Huh IFA being deleted\n");
1035 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1036 /* possible, see if it matches */
1037 struct sockaddr_in *intf_addr;
1039 intf_addr = &laddr->ifa->address.sin;
1040 if (nam->sa_family == AF_INET) {
1041 if (sin->sin_addr.s_addr ==
1042 intf_addr->sin_addr.s_addr) {
1043 SCTP_INP_RUNLOCK(inp);
1046 } else if (nam->sa_family == AF_INET6) {
1047 struct sockaddr_in6 *intf_addr6;
1049 intf_addr6 = &laddr->ifa->address.sin6;
1050 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1051 &intf_addr6->sin6_addr)) {
1052 SCTP_INP_RUNLOCK(inp);
1058 SCTP_INP_RUNLOCK(inp);
1065 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, uint32_t vrf_id)
1068 * First we check the hash table to see if someone has this port
1069 * bound with just the port.
1071 struct sctp_inpcb *inp;
1072 struct sctppcbhead *head;
1073 struct sockaddr_in *sin;
1074 struct sockaddr_in6 *sin6;
1077 if (nam->sa_family == AF_INET) {
1078 sin = (struct sockaddr_in *)nam;
1079 lport = ((struct sockaddr_in *)nam)->sin_port;
1080 } else if (nam->sa_family == AF_INET6) {
1081 sin6 = (struct sockaddr_in6 *)nam;
1082 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1084 /* unsupported family */
1088 * I could cheat here and just cast to one of the types but we will
1089 * do it right. It also provides the check against an Unsupported
1092 /* Find the head of the ALLADDR chain */
1093 if (have_lock == 0) {
1094 SCTP_INP_INFO_RLOCK();
1097 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1098 sctppcbinfo.hashmark)];
1099 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1102 * If the TCP model exists it could be that the main listening
1103 * endpoint is gone but there exists a connected socket for this guy
1104 * yet. If so we can return the first one that we find. This may NOT
1105 * be the correct one but the sctp_findassociation_ep_addr has
1106 * further code to look at all TCP models.
1108 if (inp == NULL && find_tcp_pool) {
1111 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
1113 * This is real gross, but we do NOT have a remote
1114 * port at this point depending on who is calling.
1115 * We must therefore look for ANY one that matches
1118 head = &sctppcbinfo.sctp_tcpephash[i];
1119 if (LIST_FIRST(head)) {
1120 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1129 SCTP_INP_INCR_REF(inp);
1131 if (have_lock == 0) {
1132 SCTP_INP_INFO_RUNLOCK();
1138 * Find an association for an endpoint with the pointer to whom you want to
1139 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1140 * need to change the *to to some other struct like a mbuf...
1143 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1144 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, uint32_t vrf_id)
1146 struct sctp_inpcb *inp = NULL;
1147 struct sctp_tcb *retval;
1149 SCTP_INP_INFO_RLOCK();
1150 if (find_tcp_pool) {
1151 if (inp_p != NULL) {
1152 retval = sctp_tcb_special_locate(inp_p, from, to, netp, vrf_id);
1154 retval = sctp_tcb_special_locate(&inp, from, to, netp, vrf_id);
1156 if (retval != NULL) {
1157 SCTP_INP_INFO_RUNLOCK();
1161 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1162 if (inp_p != NULL) {
1165 SCTP_INP_INFO_RUNLOCK();
1171 * ok, we have an endpoint, now lets find the assoc for it (if any)
1172 * we now place the source address or from in the to of the find
1173 * endpoint call. Since in reality this chain is used from the
1174 * inbound packet side.
1176 if (inp_p != NULL) {
1177 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL);
1179 retval = sctp_findassociation_ep_addr(&inp, from, netp, to, NULL);
1186 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1187 * find all addresses that the sender has specified in any address list. Each
1188 * address will be used to lookup the TCB and see if one exits.
1190 static struct sctp_tcb *
1191 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1192 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1193 struct sockaddr *dest)
1195 struct sockaddr_in sin4;
1196 struct sockaddr_in6 sin6;
1197 struct sctp_paramhdr *phdr, parm_buf;
1198 struct sctp_tcb *retval;
1199 uint32_t ptype, plen;
1201 memset(&sin4, 0, sizeof(sin4));
1202 memset(&sin6, 0, sizeof(sin6));
1203 sin4.sin_len = sizeof(sin4);
1204 sin4.sin_family = AF_INET;
1205 sin4.sin_port = sh->src_port;
1206 sin6.sin6_len = sizeof(sin6);
1207 sin6.sin6_family = AF_INET6;
1208 sin6.sin6_port = sh->src_port;
1211 offset += sizeof(struct sctp_init_chunk);
1213 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1214 while (phdr != NULL) {
1215 /* now we must see if we want the parameter */
1216 ptype = ntohs(phdr->param_type);
1217 plen = ntohs(phdr->param_length);
1221 if (ptype == SCTP_IPV4_ADDRESS &&
1222 plen == sizeof(struct sctp_ipv4addr_param)) {
1223 /* Get the rest of the address */
1224 struct sctp_ipv4addr_param ip4_parm, *p4;
1226 phdr = sctp_get_next_param(m, offset,
1227 (struct sctp_paramhdr *)&ip4_parm, plen);
1231 p4 = (struct sctp_ipv4addr_param *)phdr;
1232 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1234 retval = sctp_findassociation_ep_addr(inp_p,
1235 (struct sockaddr *)&sin4, netp, dest, NULL);
1236 if (retval != NULL) {
1239 } else if (ptype == SCTP_IPV6_ADDRESS &&
1240 plen == sizeof(struct sctp_ipv6addr_param)) {
1241 /* Get the rest of the address */
1242 struct sctp_ipv6addr_param ip6_parm, *p6;
1244 phdr = sctp_get_next_param(m, offset,
1245 (struct sctp_paramhdr *)&ip6_parm, plen);
1249 p6 = (struct sctp_ipv6addr_param *)phdr;
1250 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1252 retval = sctp_findassociation_ep_addr(inp_p,
1253 (struct sockaddr *)&sin6, netp, dest, NULL);
1254 if (retval != NULL) {
1258 offset += SCTP_SIZE32(plen);
1259 phdr = sctp_get_next_param(m, offset, &parm_buf,
1266 static struct sctp_tcb *
1267 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1268 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1269 uint16_t lport, int skip_src_check)
1272 * Use my vtag to hash. If we find it we then verify the source addr
1273 * is in the assoc. If all goes well we save a bit on rec of a
1276 struct sctpasochead *head;
1277 struct sctp_nets *net;
1278 struct sctp_tcb *stcb;
1282 SCTP_INP_INFO_RLOCK();
1283 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1284 sctppcbinfo.hashasocmark)];
1287 SCTP_INP_INFO_RUNLOCK();
1290 LIST_FOREACH(stcb, head, sctp_asocs) {
1291 SCTP_INP_RLOCK(stcb->sctp_ep);
1292 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1293 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1294 SCTP_INP_INFO_RUNLOCK();
1297 SCTP_TCB_LOCK(stcb);
1298 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1299 if (stcb->asoc.my_vtag == vtag) {
1301 if (stcb->rport != rport) {
1303 * we could remove this if vtags are unique
1304 * across the system.
1306 SCTP_TCB_UNLOCK(stcb);
1309 if (stcb->sctp_ep->sctp_lport != lport) {
1311 * we could remove this if vtags are unique
1312 * across the system.
1314 SCTP_TCB_UNLOCK(stcb);
1317 if (skip_src_check) {
1318 *netp = NULL; /* unknown */
1319 *inp_p = stcb->sctp_ep;
1320 SCTP_INP_INFO_RUNLOCK();
1323 net = sctp_findnet(stcb, from);
1327 SCTP_STAT_INCR(sctps_vtagexpress);
1328 *inp_p = stcb->sctp_ep;
1329 SCTP_INP_INFO_RUNLOCK();
1333 * not him, this should only happen in rare
1334 * cases so I peg it.
1336 SCTP_STAT_INCR(sctps_vtagbogus);
1339 SCTP_TCB_UNLOCK(stcb);
1341 SCTP_INP_INFO_RUNLOCK();
1346 * Find an association with the pointer to the inbound IP packet. This can be
1347 * a IPv4 or IPv6 packet.
1350 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1351 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1352 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
1356 struct sctp_tcb *retval;
1357 struct sockaddr_storage to_store, from_store;
1358 struct sockaddr *to = (struct sockaddr *)&to_store;
1359 struct sockaddr *from = (struct sockaddr *)&from_store;
1360 struct sctp_inpcb *inp;
1362 iph = mtod(m, struct ip *);
1363 if (iph->ip_v == IPVERSION) {
1365 struct sockaddr_in *from4;
1367 from4 = (struct sockaddr_in *)&from_store;
1368 bzero(from4, sizeof(*from4));
1369 from4->sin_family = AF_INET;
1370 from4->sin_len = sizeof(struct sockaddr_in);
1371 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1372 from4->sin_port = sh->src_port;
1373 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1375 struct ip6_hdr *ip6;
1376 struct sockaddr_in6 *from6;
1378 ip6 = mtod(m, struct ip6_hdr *);
1379 from6 = (struct sockaddr_in6 *)&from_store;
1380 bzero(from6, sizeof(*from6));
1381 from6->sin6_family = AF_INET6;
1382 from6->sin6_len = sizeof(struct sockaddr_in6);
1383 from6->sin6_addr = ip6->ip6_src;
1384 from6->sin6_port = sh->src_port;
1385 /* Get the scopes in properly to the sin6 addr's */
1386 /* we probably don't need these operations */
1387 (void)sa6_recoverscope(from6);
1388 sa6_embedscope(from6, ip6_use_defzone);
1390 /* Currently not supported. */
1394 /* we only go down this path if vtag is non-zero */
1395 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1396 inp_p, netp, sh->src_port, sh->dest_port, 0);
1401 if (iph->ip_v == IPVERSION) {
1403 struct sockaddr_in *to4;
1405 to4 = (struct sockaddr_in *)&to_store;
1406 bzero(to4, sizeof(*to4));
1407 to4->sin_family = AF_INET;
1408 to4->sin_len = sizeof(struct sockaddr_in);
1409 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1410 to4->sin_port = sh->dest_port;
1411 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1413 struct ip6_hdr *ip6;
1414 struct sockaddr_in6 *to6;
1416 ip6 = mtod(m, struct ip6_hdr *);
1417 to6 = (struct sockaddr_in6 *)&to_store;
1418 bzero(to6, sizeof(*to6));
1419 to6->sin6_family = AF_INET6;
1420 to6->sin6_len = sizeof(struct sockaddr_in6);
1421 to6->sin6_addr = ip6->ip6_dst;
1422 to6->sin6_port = sh->dest_port;
1423 /* Get the scopes in properly to the sin6 addr's */
1424 /* we probably don't need these operations */
1425 (void)sa6_recoverscope(to6);
1426 sa6_embedscope(to6, ip6_use_defzone);
1430 * FIX FIX?, I think we only need to look in the TCP pool if its an
1431 * INIT or COOKIE-ECHO, We really don't need to find it that way if
1432 * its a INIT-ACK or COOKIE_ACK since these in bot one-2-one and
1433 * one-2-N would be in the main pool anyway.
1435 if ((ch->chunk_type != SCTP_INITIATION) &&
1436 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1437 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1438 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1439 /* Other chunk types go to the tcp pool. */
1443 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1444 find_tcp_pool, vrf_id);
1447 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1448 find_tcp_pool, vrf_id);
1451 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1452 printf("retval:%p inp:%p\n", retval, inp);
1455 if (retval == NULL && inp) {
1456 /* Found a EP but not this address */
1457 if ((ch->chunk_type == SCTP_INITIATION) ||
1458 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1460 * special hook, we do NOT return linp or an
1461 * association that is linked to an existing
1462 * association that is under the TCP pool (i.e. no
1463 * listener exists). The endpoint finding routine
1464 * will always find a listner before examining the
1467 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1473 retval = sctp_findassociation_special_addr(m, iphlen,
1474 offset, sh, &inp, netp, to);
1475 if (inp_p != NULL) {
1481 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1482 printf("retval is %p\n", retval);
1489 * lookup an association by an ASCONF lookup address.
1490 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1493 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
1494 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1496 struct sctp_tcb *stcb;
1497 struct sockaddr_in *sin;
1498 struct sockaddr_in6 *sin6;
1499 struct sockaddr_storage local_store, remote_store;
1501 struct sctp_paramhdr parm_buf, *phdr;
1503 int zero_address = 0;
1506 memset(&local_store, 0, sizeof(local_store));
1507 memset(&remote_store, 0, sizeof(remote_store));
1509 /* First get the destination address setup too. */
1510 iph = mtod(m, struct ip *);
1511 if (iph->ip_v == IPVERSION) {
1513 sin = (struct sockaddr_in *)&local_store;
1514 sin->sin_family = AF_INET;
1515 sin->sin_len = sizeof(*sin);
1516 sin->sin_port = sh->dest_port;
1517 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1518 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1520 struct ip6_hdr *ip6;
1522 ip6 = mtod(m, struct ip6_hdr *);
1523 sin6 = (struct sockaddr_in6 *)&local_store;
1524 sin6->sin6_family = AF_INET6;
1525 sin6->sin6_len = sizeof(*sin6);
1526 sin6->sin6_port = sh->dest_port;
1527 sin6->sin6_addr = ip6->ip6_dst;
1532 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
1533 &parm_buf, sizeof(struct sctp_paramhdr));
1536 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1537 printf("findassociation_ep_asconf: failed to get asconf lookup addr\n");
1539 #endif /* SCTP_DEBUG */
1542 ptype = (int)((uint32_t) ntohs(phdr->param_type));
1543 /* get the correlation address */
1544 if (ptype == SCTP_IPV6_ADDRESS) {
1545 /* ipv6 address param */
1546 struct sctp_ipv6addr_param *p6, p6_buf;
1548 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
1551 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
1552 offset + sizeof(struct sctp_asconf_chunk),
1553 &p6_buf.ph, sizeof(*p6));
1556 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1557 printf("findassociation_ep_asconf: failed to get asconf v6 lookup addr\n");
1559 #endif /* SCTP_DEBUG */
1562 sin6 = (struct sockaddr_in6 *)&remote_store;
1563 sin6->sin6_family = AF_INET6;
1564 sin6->sin6_len = sizeof(*sin6);
1565 sin6->sin6_port = sh->src_port;
1566 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
1567 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1569 } else if (ptype == SCTP_IPV4_ADDRESS) {
1570 /* ipv4 address param */
1571 struct sctp_ipv4addr_param *p4, p4_buf;
1573 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
1576 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
1577 offset + sizeof(struct sctp_asconf_chunk),
1578 &p4_buf.ph, sizeof(*p4));
1581 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1582 printf("findassociation_ep_asconf: failed to get asconf v4 lookup addr\n");
1584 #endif /* SCTP_DEBUG */
1587 sin = (struct sockaddr_in *)&remote_store;
1588 sin->sin_family = AF_INET;
1589 sin->sin_len = sizeof(*sin);
1590 sin->sin_port = sh->src_port;
1591 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
1592 if (sin->sin_addr.s_addr == INADDR_ANY)
1595 /* invalid address param type */
1600 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
1601 netp, sh->src_port, sh->dest_port, 1);
1603 * printf("findassociation_ep_asconf: zero lookup address
1604 * finds stcb 0x%x\n", (uint32_t)stcb);
1607 stcb = sctp_findassociation_ep_addr(inp_p,
1608 (struct sockaddr *)&remote_store, netp,
1609 (struct sockaddr *)&local_store, NULL);
1616 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1617 * addresses. This way if we don't get a bind we by default pick a ephemeral
1618 * port with all addresses bound.
1621 sctp_inpcb_alloc(struct socket *so)
1624 * we get called when a new endpoint starts up. We need to allocate
1625 * the sctp_inpcb structure from the zone and init it. Mark it as
1626 * unbound and find a port that we can use as an ephemeral with
1627 * INADDR_ANY. If the user binds later no problem we can then add in
1628 * the specific addresses. And setup the default parameters for the
1632 struct sctp_inpcb *inp;
1635 struct timeval time;
1636 sctp_sharedkey_t *null_key;
1640 SCTP_INP_INFO_WLOCK();
1641 inp = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep, struct sctp_inpcb);
1643 printf("Out of SCTP-INPCB structures - no resources\n");
1644 SCTP_INP_INFO_WUNLOCK();
1648 bzero(inp, sizeof(*inp));
1650 /* bump generations */
1651 /* setup socket pointers */
1652 inp->sctp_socket = so;
1653 inp->ip_inp.inp.inp_socket = so;
1655 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
1656 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1660 struct inpcbpolicy *pcb_sp = NULL;
1662 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1663 /* Arrange to share the policy */
1664 inp->ip_inp.inp.inp_sp = pcb_sp;
1665 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1668 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1669 SCTP_INP_INFO_WUNLOCK();
1673 SCTP_INCR_EP_COUNT();
1674 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1675 SCTP_INP_INFO_WUNLOCK();
1677 so->so_pcb = (caddr_t)inp;
1679 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
1680 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
1681 /* UDP style socket */
1682 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1683 SCTP_PCB_FLAGS_UNBOUND);
1684 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1685 /* Be sure it is NON-BLOCKING IO for UDP */
1686 /* SCTP_SET_SO_NBIO(so); */
1687 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
1688 /* TCP style socket */
1689 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1690 SCTP_PCB_FLAGS_UNBOUND);
1691 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1692 /* Be sure we have blocking IO by default */
1693 SCTP_CLEAR_SO_NBIO(so);
1696 * unsupported socket type (RAW, etc)- in case we missed it
1699 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1700 return (EOPNOTSUPP);
1702 inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
1703 &inp->sctp_hashmark);
1704 if (inp->sctp_tcbhash == NULL) {
1705 printf("Out of SCTP-INPCB->hashinit - no resources\n");
1706 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1709 inp->def_vrf_id = SCTP_DEFAULT_VRFID;
1711 SCTP_INP_INFO_WLOCK();
1712 SCTP_INP_LOCK_INIT(inp);
1713 SCTP_INP_READ_INIT(inp);
1714 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1715 /* lock the new ep */
1716 SCTP_INP_WLOCK(inp);
1718 /* add it to the info area */
1719 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1720 SCTP_INP_INFO_WUNLOCK();
1722 TAILQ_INIT(&inp->read_queue);
1723 LIST_INIT(&inp->sctp_addr_list);
1725 LIST_INIT(&inp->sctp_asoc_list);
1727 #ifdef SCTP_TRACK_FREED_ASOCS
1729 LIST_INIT(&inp->sctp_asoc_free_list);
1731 /* Init the timer structure for signature change */
1732 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
1733 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1735 /* now init the actual endpoint default data */
1738 /* setup the base timeout information */
1739 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1740 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1741 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1742 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
1743 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1744 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1745 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1746 /* all max/min max are in ms */
1747 m->sctp_maxrto = sctp_rto_max_default;
1748 m->sctp_minrto = sctp_rto_min_default;
1749 m->initial_rto = sctp_rto_initial_default;
1750 m->initial_init_rto_max = sctp_init_rto_max_default;
1751 m->sctp_sack_freq = sctp_sack_freq_default;
1753 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1755 m->max_init_times = sctp_init_rtx_max_default;
1756 m->max_send_times = sctp_assoc_rtx_max_default;
1757 m->def_net_failure = sctp_path_rtx_max_default;
1758 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1759 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1760 m->max_burst = sctp_max_burst_default;
1761 /* number of streams to pre-open on a association */
1762 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1764 /* Add adaptation cookie */
1765 m->adaptation_layer_indicator = 0x504C5253;
1767 /* seed random number generator */
1768 m->random_counter = 1;
1769 m->store_at = SCTP_SIGNATURE_SIZE;
1770 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
1771 sctp_fill_random_store(m);
1773 /* Minimum cookie size */
1774 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1775 sizeof(struct sctp_state_cookie);
1776 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1778 /* Setup the initial secret */
1779 SCTP_GETTIME_TIMEVAL(&time);
1780 m->time_of_secret_change = time.tv_sec;
1782 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1783 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1785 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1787 /* How long is a cookie good for ? */
1788 m->def_cookie_life = sctp_valid_cookie_life_default;
1790 * Initialize authentication parameters
1792 m->local_hmacs = sctp_default_supported_hmaclist();
1793 m->local_auth_chunks = sctp_alloc_chunklist();
1794 sctp_auth_set_default_chunks(m->local_auth_chunks);
1795 LIST_INIT(&m->shared_keys);
1796 /* add default NULL key as key id 0 */
1797 null_key = sctp_alloc_sharedkey();
1798 sctp_insert_sharedkey(&m->shared_keys, null_key);
1799 SCTP_INP_WUNLOCK(inp);
1800 #ifdef SCTP_LOG_CLOSING
1801 sctp_log_closing(inp, NULL, 12);
1808 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1809 struct sctp_tcb *stcb)
1811 struct sctp_nets *net;
1812 uint16_t lport, rport;
1813 struct sctppcbhead *head;
1814 struct sctp_laddr *laddr, *oladdr;
1816 SCTP_TCB_UNLOCK(stcb);
1817 SCTP_INP_INFO_WLOCK();
1818 SCTP_INP_WLOCK(old_inp);
1819 SCTP_INP_WLOCK(new_inp);
1820 SCTP_TCB_LOCK(stcb);
1822 new_inp->sctp_ep.time_of_secret_change =
1823 old_inp->sctp_ep.time_of_secret_change;
1824 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1825 sizeof(old_inp->sctp_ep.secret_key));
1826 new_inp->sctp_ep.current_secret_number =
1827 old_inp->sctp_ep.current_secret_number;
1828 new_inp->sctp_ep.last_secret_number =
1829 old_inp->sctp_ep.last_secret_number;
1830 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1832 /* make it so new data pours into the new socket */
1833 stcb->sctp_socket = new_inp->sctp_socket;
1834 stcb->sctp_ep = new_inp;
1836 /* Copy the port across */
1837 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1838 rport = stcb->rport;
1839 /* Pull the tcb from the old association */
1840 LIST_REMOVE(stcb, sctp_tcbhash);
1841 LIST_REMOVE(stcb, sctp_tcblist);
1843 /* Now insert the new_inp into the TCP connected hash */
1844 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1845 sctppcbinfo.hashtcpmark)];
1847 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1848 /* Its safe to access */
1849 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1851 /* Now move the tcb into the endpoint list */
1852 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1854 * Question, do we even need to worry about the ep-hash since we
1855 * only have one connection? Probably not :> so lets get rid of it
1856 * and not suck up any kernel memory in that.
1859 /* Ok. Let's restart timer. */
1860 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1861 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
1865 SCTP_INP_INFO_WUNLOCK();
1866 if (new_inp->sctp_tcbhash != NULL) {
1867 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
1868 new_inp->sctp_tcbhash = NULL;
1870 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1871 /* Subset bound, so copy in the laddr list from the old_inp */
1872 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1873 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
1874 if (laddr == NULL) {
1876 * Gak, what can we do? This assoc is really
1877 * HOSED. We probably should send an abort
1881 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1882 printf("Association hosed in TCP model, out of laddr memory\n");
1884 #endif /* SCTP_DEBUG */
1887 SCTP_INCR_LADDR_COUNT();
1888 bzero(laddr, sizeof(*laddr));
1889 laddr->ifa = oladdr->ifa;
1890 atomic_add_int(&laddr->ifa->refcount, 1);
1891 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1893 new_inp->laddr_count++;
1897 * Now any running timers need to be adjusted since we really don't
1898 * care if they are running or not just blast in the new_inp into
1902 stcb->asoc.hb_timer.ep = (void *)new_inp;
1903 stcb->asoc.dack_timer.ep = (void *)new_inp;
1904 stcb->asoc.asconf_timer.ep = (void *)new_inp;
1905 stcb->asoc.strreset_timer.ep = (void *)new_inp;
1906 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
1907 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
1908 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
1909 /* now what about the nets? */
1910 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1911 net->pmtu_timer.ep = (void *)new_inp;
1912 net->rxt_timer.ep = (void *)new_inp;
1913 net->fr_timer.ep = (void *)new_inp;
1915 SCTP_INP_WUNLOCK(new_inp);
1916 SCTP_INP_WUNLOCK(old_inp);
1920 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1922 struct sctppcbhead *head;
1923 struct sctp_inpcb *t_inp;
1926 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1927 sctppcbinfo.hashmark)];
1929 LIST_FOREACH(t_inp, head, sctp_hash) {
1930 if (t_inp->sctp_lport != lport) {
1933 /* is it in the VRF in question */
1935 if (t_inp->def_vrf_id == vrf_id)
1940 /* This one is in use. */
1941 /* check the v6/v4 binding issue */
1942 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1943 SCTP_IPV6_V6ONLY(t_inp)) {
1944 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1945 /* collision in V6 space */
1948 /* inp is BOUND_V4 no conflict */
1951 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1952 /* t_inp is bound v4 and v6, conflict always */
1955 /* t_inp is bound only V4 */
1956 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1957 SCTP_IPV6_V6ONLY(inp)) {
1961 /* else fall through to conflict */
1971 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
1973 /* bind a ep to a socket address */
1974 struct sctppcbhead *head;
1975 struct sctp_inpcb *inp, *inp_tmp;
1976 struct inpcb *ip_inp;
1985 inp = (struct sctp_inpcb *)so->so_pcb;
1986 ip_inp = (struct inpcb *)so->so_pcb;
1988 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1990 printf("Bind called port:%d\n",
1991 ntohs(((struct sockaddr_in *)addr)->sin_port));
1993 sctp_print_address(addr);
1996 #endif /* SCTP_DEBUG */
1997 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1998 /* already did a bind, subsequent binds NOT allowed ! */
2002 if (addr->sa_family == AF_INET) {
2003 struct sockaddr_in *sin;
2005 /* IPV6_V6ONLY socket? */
2006 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2009 if (addr->sa_len != sizeof(*sin))
2012 sin = (struct sockaddr_in *)addr;
2013 lport = sin->sin_port;
2015 if (sin->sin_addr.s_addr != INADDR_ANY) {
2018 } else if (addr->sa_family == AF_INET6) {
2019 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
2020 struct sockaddr_in6 *sin6;
2022 sin6 = (struct sockaddr_in6 *)addr;
2024 if (addr->sa_len != sizeof(*sin6))
2027 lport = sin6->sin6_port;
2028 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2030 /* KAME hack: embed scopeid */
2031 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2034 /* this must be cleared for ifa_ifwithaddr() */
2035 sin6->sin6_scope_id = 0;
2037 return (EAFNOSUPPORT);
2041 * Setup a vrf_id to be the default for the non-bind-all case.
2043 vrf_id = inp->def_vrf_id;
2045 SCTP_INP_INFO_WLOCK();
2046 SCTP_INP_WLOCK(inp);
2047 /* increase our count due to the unlock we do */
2048 SCTP_INP_INCR_REF(inp);
2051 * Did the caller specify a port? if so we must see if a ep
2052 * already has this one bound.
2054 /* got to be root to get at low ports */
2055 if (ntohs(lport) < IPPORT_RESERVED) {
2057 priv_check_cred(p->td_ucred,
2058 PRIV_NETINET_RESERVEDPORT,
2062 SCTP_INP_DECR_REF(inp);
2063 SCTP_INP_WUNLOCK(inp);
2064 SCTP_INP_INFO_WUNLOCK();
2069 SCTP_INP_DECR_REF(inp);
2070 SCTP_INP_WUNLOCK(inp);
2071 SCTP_INP_INFO_WUNLOCK();
2074 SCTP_INP_WUNLOCK(inp);
2076 vrf_id = inp->def_vrf_id;
2077 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2078 if (inp_tmp != NULL) {
2080 * lock guy returned and lower count note
2081 * that we are not bound so inp_tmp should
2082 * NEVER be inp. And it is this inp
2083 * (inp_tmp) that gets the reference bump,
2084 * so we must lower it.
2086 SCTP_INP_DECR_REF(inp_tmp);
2087 SCTP_INP_DECR_REF(inp);
2089 SCTP_INP_INFO_WUNLOCK();
2090 return (EADDRNOTAVAIL);
2093 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2094 if (inp_tmp != NULL) {
2096 * lock guy returned and lower count note
2097 * that we are not bound so inp_tmp should
2098 * NEVER be inp. And it is this inp
2099 * (inp_tmp) that gets the reference bump,
2100 * so we must lower it.
2102 SCTP_INP_DECR_REF(inp_tmp);
2103 SCTP_INP_DECR_REF(inp);
2105 SCTP_INP_INFO_WUNLOCK();
2106 return (EADDRNOTAVAIL);
2109 SCTP_INP_WLOCK(inp);
2111 /* verify that no lport is not used by a singleton */
2112 if (sctp_isport_inuse(inp, lport, vrf_id)) {
2113 /* Sorry someone already has this one bound */
2114 SCTP_INP_DECR_REF(inp);
2115 SCTP_INP_WUNLOCK(inp);
2116 SCTP_INP_INFO_WUNLOCK();
2117 return (EADDRNOTAVAIL);
2122 * get any port but lets make sure no one has any address
2123 * with this port bound
2127 * setup the inp to the top (I could use the union but this
2130 uint32_t port_guess;
2131 uint16_t port_attempt;
2136 port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
2137 port_attempt = (port_guess & 0x0000ffff);
2138 if (port_attempt == 0) {
2141 if (port_attempt < IPPORT_RESERVED) {
2142 port_attempt += IPPORT_RESERVED;
2144 vrf_id = inp->def_vrf_id;
2145 if (sctp_isport_inuse(inp, htons(port_attempt), vrf_id) == 1) {
2146 /* got a port we can use */
2150 if (not_found == 1) {
2151 /* We can use this port */
2155 /* try upper half */
2157 port_attempt = ((port_guess >> 16) & 0x0000ffff);
2158 if (port_attempt == 0) {
2161 if (port_attempt < IPPORT_RESERVED) {
2162 port_attempt += IPPORT_RESERVED;
2164 vrf_id = inp->def_vrf_id;
2165 if (sctp_isport_inuse(inp, htons(port_attempt), vrf_id) == 1) {
2166 /* got a port we can use */
2170 if (not_found == 1) {
2171 /* We can use this port */
2175 /* try two half's added together */
2177 port_attempt = (((port_guess >> 16) & 0x0000ffff) +
2178 (port_guess & 0x0000ffff));
2179 if (port_attempt == 0) {
2180 /* get a new random number */
2183 if (port_attempt < IPPORT_RESERVED) {
2184 port_attempt += IPPORT_RESERVED;
2186 vrf_id = inp->def_vrf_id;
2187 if (sctp_isport_inuse(inp, htons(port_attempt), vrf_id) == 1) {
2188 /* got a port we can use */
2192 if (not_found == 1) {
2193 /* We can use this port */
2198 /* we don't get out of the loop until we have a port */
2199 lport = htons(port_attempt);
2201 SCTP_INP_DECR_REF(inp);
2202 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2203 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2205 * this really should not happen. The guy did a non-blocking
2206 * bind and then did a close at the same time.
2208 SCTP_INP_WUNLOCK(inp);
2209 SCTP_INP_INFO_WUNLOCK();
2212 /* ok we look clear to give out this port, so lets setup the binding */
2214 /* binding to all addresses, so just set in the proper flags */
2215 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2216 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2217 /* set the automatic addr changes from kernel flag */
2218 if (sctp_auto_asconf == 0) {
2219 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2221 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2225 * bind specific, make sure flags is off and add a new
2226 * address structure to the sctp_addr_list inside the ep
2229 * We will need to allocate one and insert it at the head. The
2230 * socketopt call can just insert new addresses in there as
2231 * well. It will also have to do the embed scope kame hack
2232 * too (before adding).
2234 struct sctp_ifa *ifa;
2235 struct sockaddr_storage store_sa;
2237 memset(&store_sa, 0, sizeof(store_sa));
2238 if (addr->sa_family == AF_INET) {
2239 struct sockaddr_in *sin;
2241 sin = (struct sockaddr_in *)&store_sa;
2242 memcpy(sin, addr, sizeof(struct sockaddr_in));
2244 } else if (addr->sa_family == AF_INET6) {
2245 struct sockaddr_in6 *sin6;
2247 sin6 = (struct sockaddr_in6 *)&store_sa;
2248 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
2249 sin6->sin6_port = 0;
2252 * first find the interface with the bound address need to
2253 * zero out the port to find the address! yuck! can't do
2254 * this earlier since need port for sctp_pcb_findep()
2256 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa, vrf_id, 0);
2258 /* Can't find an interface with that address */
2259 SCTP_INP_WUNLOCK(inp);
2260 SCTP_INP_INFO_WUNLOCK();
2261 return (EADDRNOTAVAIL);
2263 if (addr->sa_family == AF_INET6) {
2264 /* GAK, more FIXME IFA lock? */
2265 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2266 /* Can't bind a non-existent addr. */
2267 SCTP_INP_WUNLOCK(inp);
2268 SCTP_INP_INFO_WUNLOCK();
2272 /* we're not bound all */
2273 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
2274 /* set the automatic addr changes from kernel flag */
2275 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2276 if (sctp_auto_asconf == 0) {
2277 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2280 * allow bindx() to send ASCONF's for binding
2283 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2286 /* add this address to the endpoint list */
2287 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
2289 SCTP_INP_WUNLOCK(inp);
2290 SCTP_INP_INFO_WUNLOCK();
2295 /* find the bucket */
2296 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2297 sctppcbinfo.hashmark)];
2298 /* put it in the bucket */
2299 LIST_INSERT_HEAD(head, inp, sctp_hash);
2301 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2302 printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
2305 /* set in the port */
2306 inp->sctp_lport = lport;
2308 /* turn off just the unbound flag */
2309 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2310 SCTP_INP_WUNLOCK(inp);
2311 SCTP_INP_INFO_WUNLOCK();
2317 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
2319 struct sctp_iterator *it;
2322 * We enter with the only the ITERATOR_LOCK in place and a write
2323 * lock on the inp_info stuff.
2327 * Go through all iterators, we must do this since it is possible
2328 * that some iterator does NOT have the lock, but is waiting for it.
2329 * And the one that had the lock has either moved in the last
2330 * iteration or we just cleared it above. We need to find all of
2331 * those guys. The list of iterators should never be very big
2334 TAILQ_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
2335 if (it == inp->inp_starting_point_for_iterator)
2336 /* skip this guy, he's special */
2338 if (it->inp == inp) {
2340 * This is tricky and we DON'T lock the iterator.
2341 * Reason is he's running but waiting for me since
2342 * inp->inp_starting_point_for_iterator has the lock
2343 * on me (the guy above we skipped). This tells us
2344 * its is not running but waiting for
2345 * inp->inp_starting_point_for_iterator to be
2346 * released by the guy that does have our INP in a
2349 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2353 /* set him up to do the next guy not me */
2359 it = inp->inp_starting_point_for_iterator;
2361 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2370 /* release sctp_inpcb unbind the port */
2372 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2375 * Here we free a endpoint. We must find it (if it is in the Hash
2376 * table) and remove it from there. Then we must also find it in the
2377 * overall list and remove it from there. After all removals are
2378 * complete then any timer has to be stopped. Then start the actual
2379 * freeing. a) Any local lists. b) Any associations. c) The hash of
2380 * all associations. d) finally the ep itself.
2383 struct sctp_inpcb *inp_save;
2384 struct sctp_tcb *asoc, *nasoc;
2385 struct sctp_laddr *laddr, *nladdr;
2386 struct inpcb *ip_pcb;
2389 struct sctp_queued_to_read *sq;
2393 sctp_sharedkey_t *shared_key;
2396 #ifdef SCTP_LOG_CLOSING
2397 sctp_log_closing(inp, NULL, 0);
2400 SCTP_ITERATOR_LOCK();
2401 so = inp->sctp_socket;
2402 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2403 /* been here before.. eeks.. get out of here */
2404 printf("This conflict in free SHOULD not be happening!\n");
2405 SCTP_ITERATOR_UNLOCK();
2406 #ifdef SCTP_LOG_CLOSING
2407 sctp_log_closing(inp, NULL, 1);
2411 SCTP_ASOC_CREATE_LOCK(inp);
2412 SCTP_INP_INFO_WLOCK();
2414 SCTP_INP_WLOCK(inp);
2416 * First time through we have the socket lock, after that no more.
2420 * Once we are in we can remove the flag from = 1 is only
2421 * passed from the actual closing routines that are called
2422 * via the sockets layer.
2424 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
2426 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
2427 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
2430 sctp_m_freem(inp->control);
2431 inp->control = NULL;
2434 sctp_m_freem(inp->pkt);
2438 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
2439 * here but I will be nice :> (i.e.
2441 if (immediate == 0) {
2445 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2447 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2448 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2449 /* Skip guys being freed */
2450 asoc->sctp_socket = NULL;
2454 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2455 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2456 /* Just abandon things in the front states */
2457 if (asoc->asoc.total_output_queue_size == 0) {
2458 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_2);
2462 SCTP_TCB_LOCK(asoc);
2463 /* Disconnect the socket please */
2464 asoc->sctp_socket = NULL;
2465 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2466 if ((asoc->asoc.size_on_reasm_queue > 0) ||
2467 (asoc->asoc.control_pdapi) ||
2468 (asoc->asoc.size_on_all_streams > 0) ||
2469 (so && (so->so_rcv.sb_cc > 0))
2471 /* Left with Data unread */
2472 struct mbuf *op_err;
2474 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2475 0, M_DONTWAIT, 1, MT_DATA);
2477 /* Fill in the user initiated abort */
2478 struct sctp_paramhdr *ph;
2481 SCTP_BUF_LEN(op_err) =
2482 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
2484 struct sctp_paramhdr *);
2485 ph->param_type = htons(
2486 SCTP_CAUSE_USER_INITIATED_ABT);
2487 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2488 ippp = (uint32_t *) (ph + 1);
2489 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
2491 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
2492 sctp_send_abort_tcb(asoc, op_err);
2493 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2494 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2495 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2496 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2498 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4);
2500 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2501 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2502 (asoc->asoc.stream_queue_cnt == 0)
2504 if (asoc->asoc.locked_on_sending) {
2507 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2508 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2510 * there is nothing queued to send,
2511 * so I send shutdown
2513 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2514 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2515 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2516 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2518 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2519 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2520 asoc->asoc.primary_destination);
2521 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2522 asoc->asoc.primary_destination);
2523 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR);
2526 /* mark into shutdown pending */
2527 struct sctp_stream_queue_pending *sp;
2529 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2530 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2531 asoc->asoc.primary_destination);
2532 if (asoc->asoc.locked_on_sending) {
2533 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
2536 printf("Error, sp is NULL, locked on sending is %p strm:%d\n",
2537 asoc->asoc.locked_on_sending,
2538 asoc->asoc.locked_on_sending->stream_no);
2540 if ((sp->length == 0) && (sp->msg_is_complete == 0))
2541 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
2544 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2545 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2546 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
2547 struct mbuf *op_err;
2550 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2551 0, M_DONTWAIT, 1, MT_DATA);
2557 struct sctp_paramhdr *ph;
2560 SCTP_BUF_LEN(op_err) =
2561 (sizeof(struct sctp_paramhdr) +
2564 struct sctp_paramhdr *);
2565 ph->param_type = htons(
2566 SCTP_CAUSE_USER_INITIATED_ABT);
2567 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2568 ippp = (uint32_t *) (ph + 1);
2569 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
2571 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
2572 sctp_send_abort_tcb(asoc, op_err);
2573 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2574 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2575 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2576 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2578 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_6);
2583 SCTP_TCB_UNLOCK(asoc);
2585 /* now is there some left in our SHUTDOWN state? */
2587 SCTP_INP_WUNLOCK(inp);
2588 SCTP_ASOC_CREATE_UNLOCK(inp);
2589 SCTP_INP_INFO_WUNLOCK();
2590 SCTP_ITERATOR_UNLOCK();
2591 #ifdef SCTP_LOG_CLOSING
2592 sctp_log_closing(inp, NULL, 2);
2597 inp->sctp_socket = NULL;
2598 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2599 SCTP_PCB_FLAGS_UNBOUND) {
2601 * ok, this guy has been bound. It's port is somewhere in
2602 * the sctppcbinfo hash table. Remove it!
2604 LIST_REMOVE(inp, sctp_hash);
2605 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
2608 * If there is a timer running to kill us, forget it, since it may
2609 * have a contest on the INP lock.. which would cause us to die ...
2612 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2614 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2615 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2619 /* Free associations that are NOT killing us */
2620 SCTP_TCB_LOCK(asoc);
2621 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
2622 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
2623 struct mbuf *op_err;
2626 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2627 0, M_DONTWAIT, 1, MT_DATA);
2629 /* Fill in the user initiated abort */
2630 struct sctp_paramhdr *ph;
2632 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
2634 ph = mtod(op_err, struct sctp_paramhdr *);
2635 ph->param_type = htons(
2636 SCTP_CAUSE_USER_INITIATED_ABT);
2637 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2638 ippp = (uint32_t *) (ph + 1);
2639 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
2642 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
2643 sctp_send_abort_tcb(asoc, op_err);
2644 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2645 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2647 SCTP_TCB_UNLOCK(asoc);
2650 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2651 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2652 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2654 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8);
2657 /* Ok we have someone out there that will kill us */
2658 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2659 SCTP_INP_WUNLOCK(inp);
2660 SCTP_ASOC_CREATE_UNLOCK(inp);
2661 SCTP_INP_INFO_WUNLOCK();
2662 SCTP_ITERATOR_UNLOCK();
2663 #ifdef SCTP_LOG_CLOSING
2664 sctp_log_closing(inp, NULL, 3);
2668 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
2669 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2670 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2671 SCTP_INP_WUNLOCK(inp);
2672 SCTP_ASOC_CREATE_UNLOCK(inp);
2673 SCTP_INP_INFO_WUNLOCK();
2674 SCTP_ITERATOR_UNLOCK();
2675 #ifdef SCTP_LOG_CLOSING
2676 sctp_log_closing(inp, NULL, 4);
2680 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2681 inp->sctp_ep.signature_change.type = 0;
2682 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2684 #ifdef SCTP_LOG_CLOSING
2685 sctp_log_closing(inp, NULL, 5);
2688 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2689 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
2690 /* Clear the read queue */
2691 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
2692 /* Its only abandoned if it had data left */
2694 SCTP_STAT_INCR(sctps_left_abandon);
2696 TAILQ_REMOVE(&inp->read_queue, sq, next);
2697 sctp_free_remote_addr(sq->whoFrom);
2699 so->so_rcv.sb_cc -= sq->length;
2701 sctp_m_freem(sq->data);
2705 * no need to free the net count, since at this point all
2708 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
2709 SCTP_DECR_READQ_COUNT();
2711 /* Now the sctp_pcb things */
2713 * free each asoc if it is not already closed/free. we can't use the
2714 * macro here since le_next will get freed as part of the
2715 * sctp_free_assoc() call.
2720 ipsec4_delete_pcbpolicy(ip_pcb);
2723 /* Unlocks not needed since the socket is gone now */
2725 if (ip_pcb->inp_options) {
2726 (void)sctp_m_free(ip_pcb->inp_options);
2727 ip_pcb->inp_options = 0;
2729 if (ip_pcb->inp_moptions) {
2730 ip_freemoptions(ip_pcb->inp_moptions);
2731 ip_pcb->inp_moptions = 0;
2734 if (ip_pcb->inp_vflag & INP_IPV6) {
2735 struct in6pcb *in6p;
2737 in6p = (struct in6pcb *)inp;
2738 ip6_freepcbopts(in6p->in6p_outputopts);
2741 ip_pcb->inp_vflag = 0;
2742 /* free up authentication fields */
2743 if (inp->sctp_ep.local_auth_chunks != NULL)
2744 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2745 if (inp->sctp_ep.local_hmacs != NULL)
2746 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2748 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2749 while (shared_key) {
2750 LIST_REMOVE(shared_key, next);
2751 sctp_free_sharedkey(shared_key);
2752 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2755 inp_save = LIST_NEXT(inp, sctp_list);
2756 LIST_REMOVE(inp, sctp_list);
2758 /* fix any iterators only after out of the list */
2759 sctp_iterator_inp_being_freed(inp, inp_save);
2761 * if we have an address list the following will free the list of
2762 * ifaddr's that are set into this ep. Again macro limitations here,
2763 * since the LIST_FOREACH could be a bad idea.
2765 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2767 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2768 sctp_remove_laddr(laddr);
2771 #ifdef SCTP_TRACK_FREED_ASOCS
2773 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
2775 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2776 LIST_REMOVE(asoc, sctp_tcblist);
2777 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
2778 SCTP_DECR_ASOC_COUNT();
2780 /* *** END TEMP CODE *** */
2782 /* Now lets see about freeing the EP hash table. */
2783 if (inp->sctp_tcbhash != NULL) {
2784 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2785 inp->sctp_tcbhash = NULL;
2787 /* Now we must put the ep memory back into the zone pool */
2788 SCTP_INP_LOCK_DESTROY(inp);
2789 SCTP_INP_READ_DESTROY(inp);
2790 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2791 SCTP_INP_INFO_WUNLOCK();
2793 SCTP_ITERATOR_UNLOCK();
2795 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2796 SCTP_DECR_EP_COUNT();
2802 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2804 struct sctp_nets *net;
2806 /* locate the address */
2807 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2808 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2816 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
2817 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
2821 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
2823 struct sctp_ifa *sctp_ifa;
2825 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, 0);
2834 sctp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
2836 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2837 /* we always get at LEAST 2 MTU's */
2838 if (net->cwnd < (2 * net->mtu)) {
2839 net->cwnd = 2 * net->mtu;
2841 net->ssthresh = stcb->asoc.peers_rwnd;
2846 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2847 int set_scope, int from)
2850 * The following is redundant to the same lines in the
2851 * sctp_aloc_assoc() but is needed since other's call the add
2854 struct sctp_nets *net, *netfirst;
2858 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2859 printf("Adding an address (from:%d) to the peer: ", from);
2860 sctp_print_address(newaddr);
2864 netfirst = sctp_findnet(stcb, newaddr);
2867 * Lie and return ok, we don't want to make the association
2868 * go away for this behavior. It will happen in the TCP
2869 * model in a connected socket. It does not reach the hash
2870 * table until after the association is built so it can't be
2871 * found. Mark as reachable, since the initial creation will
2872 * have been cleared and the NOT_IN_ASSOC flag will have
2873 * been added... and we don't want to end up removing it
2876 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2877 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
2878 SCTP_ADDR_UNCONFIRMED);
2880 netfirst->dest_state = SCTP_ADDR_REACHABLE;
2886 if (newaddr->sa_family == AF_INET) {
2887 struct sockaddr_in *sin;
2889 sin = (struct sockaddr_in *)newaddr;
2890 if (sin->sin_addr.s_addr == 0) {
2891 /* Invalid address */
2894 /* zero out the bzero area */
2895 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2897 /* assure len is set */
2898 sin->sin_len = sizeof(struct sockaddr_in);
2900 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2901 stcb->ipv4_local_scope = 1;
2903 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2904 stcb->asoc.ipv4_local_scope = 1;
2906 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2908 /* Validate the address is in scope */
2909 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2910 (stcb->asoc.ipv4_local_scope == 0)) {
2914 } else if (newaddr->sa_family == AF_INET6) {
2915 struct sockaddr_in6 *sin6;
2917 sin6 = (struct sockaddr_in6 *)newaddr;
2918 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2919 /* Invalid address */
2922 /* assure len is set */
2923 sin6->sin6_len = sizeof(struct sockaddr_in6);
2925 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
2926 stcb->asoc.loopback_scope = 1;
2927 stcb->asoc.local_scope = 0;
2928 stcb->asoc.ipv4_local_scope = 1;
2929 stcb->asoc.site_scope = 1;
2930 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2932 * If the new destination is a LINK_LOCAL we
2933 * must have common site scope. Don't set
2934 * the local scope since we may not share
2935 * all links, only loopback can do this.
2936 * Links on the local network would also be
2937 * on our private network for v4 too.
2939 stcb->asoc.ipv4_local_scope = 1;
2940 stcb->asoc.site_scope = 1;
2941 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2943 * If the new destination is SITE_LOCAL then
2944 * we must have site scope in common.
2946 stcb->asoc.site_scope = 1;
2949 /* Validate the address is in scope */
2950 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2951 (stcb->asoc.loopback_scope == 0)) {
2953 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2954 (stcb->asoc.local_scope == 0)) {
2956 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2957 (stcb->asoc.site_scope == 0)) {
2962 /* not supported family type */
2965 net = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net, struct sctp_nets);
2969 SCTP_INCR_RADDR_COUNT();
2970 bzero(net, sizeof(*net));
2971 SCTP_GETTIME_TIMEVAL(&net->start_time);
2972 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
2973 if (newaddr->sa_family == AF_INET) {
2974 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
2975 } else if (newaddr->sa_family == AF_INET6) {
2976 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
2978 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
2979 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
2980 stcb->asoc.loopback_scope = 1;
2981 stcb->asoc.ipv4_local_scope = 1;
2982 stcb->asoc.local_scope = 0;
2983 stcb->asoc.site_scope = 1;
2986 net->failure_threshold = stcb->asoc.def_net_failure;
2987 if (addr_inscope == 0) {
2988 net->dest_state = (SCTP_ADDR_REACHABLE |
2989 SCTP_ADDR_OUT_OF_SCOPE);
2991 if (from == SCTP_ADDR_IS_CONFIRMED)
2992 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
2993 net->dest_state = SCTP_ADDR_REACHABLE;
2995 net->dest_state = SCTP_ADDR_REACHABLE |
2996 SCTP_ADDR_UNCONFIRMED;
2998 net->RTO = stcb->asoc.initial_rto;
2999 stcb->asoc.numnets++;
3000 *(&net->ref_count) = 1;
3001 net->tos_flowlabel = 0;
3003 if (newaddr->sa_family == AF_INET)
3004 net->tos_flowlabel = stcb->asoc.default_tos;
3007 if (newaddr->sa_family == AF_INET6)
3008 net->tos_flowlabel = stcb->asoc.default_flowlabel;
3010 /* Init the timer structure */
3011 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3012 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
3013 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3015 /* Now generate a route for this guy */
3016 /* KAME hack: embed scopeid */
3017 if (newaddr->sa_family == AF_INET6) {
3018 struct sockaddr_in6 *sin6;
3020 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3021 (void)sa6_embedscope(sin6, ip6_use_defzone);
3022 sin6->sin6_scope_id = 0;
3024 rtalloc_ign((struct route *)&net->ro, 0UL);
3026 if (newaddr->sa_family == AF_INET6) {
3027 struct sockaddr_in6 *sin6;
3029 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3030 (void)sa6_recoverscope(sin6);
3032 if ((net->ro.ro_rt) &&
3033 (net->ro.ro_rt->rt_ifp)) {
3034 net->mtu = net->ro.ro_rt->rt_ifp->if_mtu;
3035 if (from == SCTP_ALLOC_ASOC) {
3036 stcb->asoc.smallest_mtu = net->mtu;
3038 /* start things off to match mtu of interface please. */
3039 net->ro.ro_rt->rt_rmx.rmx_mtu = net->ro.ro_rt->rt_ifp->if_mtu;
3041 net->mtu = stcb->asoc.smallest_mtu;
3044 if (stcb->asoc.smallest_mtu > net->mtu) {
3045 stcb->asoc.smallest_mtu = net->mtu;
3048 * We take the max of the burst limit times a MTU or the
3049 * INITIAL_CWND. We then limit this to 4 MTU's of sending.
3051 sctp_set_initial_cc_param(stcb, net);
3054 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING)
3055 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
3059 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3060 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3062 net->find_pseudo_cumack = 1;
3063 net->find_rtx_pseudo_cumack = 1;
3064 net->src_addr_selected = 0;
3065 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3066 if (net->ro.ro_rt == NULL) {
3067 /* Since we have no route put it at the back */
3068 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3069 } else if (netfirst == NULL) {
3070 /* We are the first one in the pool. */
3071 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3072 } else if (netfirst->ro.ro_rt == NULL) {
3074 * First one has NO route. Place this one ahead of the first
3077 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3078 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3080 * This one has a different interface than the one at the
3081 * top of the list. Place it ahead.
3083 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3086 * Ok we have the same interface as the first one. Move
3087 * forward until we find either a) one with a NULL route...
3088 * insert ahead of that b) one with a different ifp.. insert
3089 * after that. c) end of the list.. insert at the tail.
3091 struct sctp_nets *netlook;
3094 netlook = TAILQ_NEXT(netfirst, sctp_next);
3095 if (netlook == NULL) {
3096 /* End of the list */
3097 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3099 } else if (netlook->ro.ro_rt == NULL) {
3100 /* next one has NO route */
3101 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3103 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
3104 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3110 } while (netlook != NULL);
3113 /* got to have a primary set */
3114 if (stcb->asoc.primary_destination == 0) {
3115 stcb->asoc.primary_destination = net;
3116 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3118 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3119 /* No route to current primary adopt new primary */
3120 stcb->asoc.primary_destination = net;
3122 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3124 /* Validate primary is first */
3125 net = TAILQ_FIRST(&stcb->asoc.nets);
3126 if ((net != stcb->asoc.primary_destination) &&
3127 (stcb->asoc.primary_destination)) {
3129 * first one on the list is NOT the primary sctp_cmpaddr()
3130 * is much more efficent if the primary is the first on the
3133 TAILQ_REMOVE(&stcb->asoc.nets,
3134 stcb->asoc.primary_destination, sctp_next);
3135 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3136 stcb->asoc.primary_destination, sctp_next);
3143 * allocate an association and add it to the endpoint. The caller must be
3144 * careful to add all additional addresses once they are know right away or
3145 * else the assoc will be may experience a blackout scenario.
3148 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3149 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf)
3151 struct sctp_tcb *stcb;
3152 struct sctp_association *asoc;
3153 struct sctpasochead *head;
3158 * Assumption made here: Caller has done a
3159 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3160 * address does not exist already.
3162 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
3163 /* Hit max assoc, sorry no more */
3167 SCTP_INP_RLOCK(inp);
3168 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3170 * If its in the TCP pool, its NOT allowed to create an
3171 * association. The parent listener needs to call
3172 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3173 * off, or connected one does this.. its an error.
3175 SCTP_INP_RUNLOCK(inp);
3180 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
3181 printf("Allocate an association for peer:");
3183 sctp_print_address(firstaddr);
3187 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3189 #endif /* SCTP_DEBUG */
3190 if (firstaddr->sa_family == AF_INET) {
3191 struct sockaddr_in *sin;
3193 sin = (struct sockaddr_in *)firstaddr;
3194 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3195 /* Invalid address */
3196 SCTP_INP_RUNLOCK(inp);
3200 rport = sin->sin_port;
3201 } else if (firstaddr->sa_family == AF_INET6) {
3202 struct sockaddr_in6 *sin6;
3204 sin6 = (struct sockaddr_in6 *)firstaddr;
3205 if ((sin6->sin6_port == 0) ||
3206 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3207 /* Invalid address */
3208 SCTP_INP_RUNLOCK(inp);
3212 rport = sin6->sin6_port;
3214 /* not supported family type */
3215 SCTP_INP_RUNLOCK(inp);
3219 SCTP_INP_RUNLOCK(inp);
3220 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3222 * If you have not performed a bind, then we need to do the
3223 * ephemerial bind for you.
3225 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3226 (struct sockaddr *)NULL,
3227 (struct thread *)NULL
3229 /* bind error, probably perm */
3234 stcb = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc, struct sctp_tcb);
3236 /* out of memory? */
3240 SCTP_INCR_ASOC_COUNT();
3242 bzero(stcb, sizeof(*stcb));
3244 SCTP_TCB_LOCK_INIT(stcb);
3245 SCTP_TCB_SEND_LOCK_INIT(stcb);
3246 /* setup back pointer's */
3247 stcb->sctp_ep = inp;
3248 stcb->sctp_socket = inp->sctp_socket;
3249 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag, vrf))) {
3251 SCTP_TCB_LOCK_DESTROY(stcb);
3252 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3253 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3254 SCTP_DECR_ASOC_COUNT();
3259 stcb->rport = rport;
3260 SCTP_INP_INFO_WLOCK();
3261 SCTP_INP_WLOCK(inp);
3262 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3263 /* inpcb freed while alloc going on */
3264 SCTP_TCB_LOCK_DESTROY(stcb);
3265 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3266 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3267 SCTP_INP_WUNLOCK(inp);
3268 SCTP_INP_INFO_WUNLOCK();
3269 SCTP_DECR_ASOC_COUNT();
3273 SCTP_TCB_LOCK(stcb);
3275 /* now that my_vtag is set, add it to the hash */
3276 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
3277 sctppcbinfo.hashasocmark)];
3278 /* put it in the bucket in the vtag hash of assoc's for the system */
3279 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
3280 SCTP_INP_INFO_WUNLOCK();
3282 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
3283 /* failure.. memory error? */
3285 SCTP_FREE(asoc->strmout);
3286 if (asoc->mapping_array)
3287 SCTP_FREE(asoc->mapping_array);
3289 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3290 SCTP_DECR_ASOC_COUNT();
3291 SCTP_TCB_LOCK_DESTROY(stcb);
3292 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3293 SCTP_INP_WUNLOCK(inp);
3297 /* Init all the timers */
3298 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
3299 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
3300 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
3301 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
3302 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
3303 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
3304 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
3306 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
3307 /* now file the port under the hash as well */
3308 if (inp->sctp_tcbhash != NULL) {
3309 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
3310 inp->sctp_hashmark)];
3311 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
3313 SCTP_INP_WUNLOCK(inp);
3315 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3316 printf("Association %p now allocated\n", stcb);
3324 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
3326 struct sctp_association *asoc;
3330 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3331 if (net == asoc->primary_destination) {
3333 struct sctp_nets *lnet;
3335 lnet = TAILQ_FIRST(&asoc->nets);
3336 /* Try to find a confirmed primary */
3337 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
3339 if (net == asoc->last_data_chunk_from) {
3341 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
3343 if (net == asoc->last_control_chunk_from) {
3345 asoc->last_control_chunk_from = NULL;
3347 sctp_free_remote_addr(net);
3351 * remove a remote endpoint address from an association, it will fail if the
3352 * address does not exist.
3355 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3358 * Here we need to remove a remote address. This is quite simple, we
3359 * first find it in the list of address for the association
3360 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
3361 * on that item. Note we do not allow it to be removed if there are
3362 * no other addresses.
3364 struct sctp_association *asoc;
3365 struct sctp_nets *net, *net_tmp;
3369 /* locate the address */
3370 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3371 net_tmp = TAILQ_NEXT(net, sctp_next);
3372 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3375 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3377 /* we found the guy */
3378 if (asoc->numnets < 2) {
3379 /* Must have at LEAST two remote addresses */
3382 sctp_remove_net(stcb, net);
3393 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time)
3395 struct sctpvtaghead *chain;
3396 struct sctp_tagblock *twait_block;
3400 SCTP_GETTIME_TIMEVAL(&now);
3401 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3403 if (!SCTP_LIST_EMPTY(chain)) {
3404 /* Block(s) present, lets find space, and expire on the fly */
3405 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3406 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3407 if ((twait_block->vtag_block[i].v_tag == 0) &&
3409 twait_block->vtag_block[i].tv_sec_at_expire =
3411 twait_block->vtag_block[i].v_tag = tag;
3413 } else if ((twait_block->vtag_block[i].v_tag) &&
3414 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3416 /* Audit expires this guy */
3417 twait_block->vtag_block[i].tv_sec_at_expire = 0;
3418 twait_block->vtag_block[i].v_tag = 0;
3420 /* Reuse it for my new tag */
3421 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3422 twait_block->vtag_block[0].v_tag = tag;
3429 * We only do up to the block where we can
3430 * place our tag for audits
3436 /* Need to add a new block to chain */
3438 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
3439 sizeof(struct sctp_tagblock), "TimeWait");
3440 if (twait_block == NULL) {
3443 memset(twait_block, 0, sizeof(struct sctp_tagblock));
3444 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3445 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3447 twait_block->vtag_block[0].v_tag = tag;
3453 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3455 struct sctp_iterator *it;
3458 * Unlock the tcb lock we do this so we avoid a dead lock scenario
3459 * where the iterator is waiting on the TCB lock and the TCB lock is
3460 * waiting on the iterator lock.
3462 it = stcb->asoc.stcb_starting_point_for_iterator;
3466 if (it->inp != stcb->sctp_ep) {
3467 /* hmm, focused on the wrong one? */
3470 if (it->stcb != stcb) {
3473 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3474 if (it->stcb == NULL) {
3475 /* done with all asoc's in this assoc */
3476 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3479 it->inp = LIST_NEXT(inp, sctp_list);
3486 * Free the association after un-hashing the remote port.
3489 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
3492 struct sctp_association *asoc;
3493 struct sctp_nets *net, *prev;
3494 struct sctp_laddr *laddr;
3495 struct sctp_tmit_chunk *chk;
3496 struct sctp_asconf_addr *aparam;
3497 struct sctp_stream_reset_list *liste;
3498 struct sctp_queued_to_read *sq;
3499 struct sctp_stream_queue_pending *sp;
3500 sctp_sharedkey_t *shared_key;
3505 /* first, lets purge the entry from the hash table. */
3507 #ifdef SCTP_LOG_CLOSING
3508 sctp_log_closing(inp, stcb, 6);
3510 if (stcb->asoc.state == 0) {
3511 #ifdef SCTP_LOG_CLOSING
3512 sctp_log_closing(inp, NULL, 7);
3514 /* there is no asoc, really TSNH :-0 */
3518 if (stcb->freed_from_where == 0) {
3519 /* Only record the first place free happened from */
3520 stcb->freed_from_where = from_location;
3525 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3526 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3527 /* nothing around */
3530 so = inp->sctp_socket;
3533 * We used timer based freeing if a reader or writer is in the way.
3534 * So we first check if we are actually being called from a timer,
3535 * if so we abort early if a reader or writer is still in the way.
3537 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
3538 (from_inpcbfree == SCTP_NORMAL_PROC)) {
3540 * is it the timer driving us? if so are the reader/writers
3543 if (stcb->asoc.refcnt) {
3544 /* nope, reader or writer in the way */
3545 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3546 /* no asoc destroyed */
3547 SCTP_TCB_UNLOCK(stcb);
3548 #ifdef SCTP_LOG_CLOSING
3549 sctp_log_closing(inp, stcb, 8);
3554 /* now clean up any other timers */
3555 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3556 asoc->hb_timer.self = NULL;
3557 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3558 asoc->dack_timer.self = NULL;
3559 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3561 * For stream reset we don't blast this unless
3562 * it is a str-reset timer, it might be the
3563 * free-asoc timer which we DON'T want to
3566 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
3567 asoc->strreset_timer.self = NULL;
3568 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3569 asoc->asconf_timer.self = NULL;
3570 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3571 asoc->autoclose_timer.self = NULL;
3572 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3573 asoc->shut_guard_timer.self = NULL;
3574 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3575 asoc->delayed_event_timer.self = NULL;
3576 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3577 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3578 net->fr_timer.self = NULL;
3579 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3580 net->rxt_timer.self = NULL;
3581 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3582 net->pmtu_timer.self = NULL;
3584 /* Now the read queue needs to be cleaned up (only once) */
3586 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
3587 SCTP_INP_READ_LOCK(inp);
3588 TAILQ_FOREACH(sq, &inp->read_queue, next) {
3589 if (sq->stcb == stcb) {
3590 sq->do_not_ref_stcb = 1;
3591 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
3593 * If there is no end, there never will be
3596 if (sq->end_added == 0) {
3597 /* Held for PD-API clear that. */
3598 sq->pdapi_aborted = 1;
3599 sq->held_length = 0;
3600 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3602 * Need to add a PD-API
3603 * aborted indication.
3604 * Setting the control_pdapi
3605 * assures that it will be
3606 * added right after this
3611 stcb->asoc.control_pdapi = sq;
3612 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
3613 sctp_notify_partial_delivery_indication(stcb,
3614 SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq);
3615 stcb->asoc.control_pdapi = NULL;
3618 /* Add an end to wake them */
3623 SCTP_INP_READ_UNLOCK(inp);
3624 if (stcb->block_entry) {
3626 stcb->block_entry->error = ECONNRESET;
3627 stcb->block_entry = NULL;
3630 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
3631 if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) {
3633 * reader or writer in the way, we have hopefully given him
3634 * something to chew on above.
3636 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3637 SCTP_TCB_UNLOCK(stcb);
3639 SCTP_INP_RLOCK(inp);
3640 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3641 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3642 /* nothing around */
3645 /* Wake any reader/writers */
3646 sctp_sorwakeup(inp, so);
3647 sctp_sowwakeup(inp, so);
3649 SCTP_INP_RUNLOCK(inp);
3652 #ifdef SCTP_LOG_CLOSING
3653 sctp_log_closing(inp, stcb, 9);
3655 /* no asoc destroyed */
3658 #ifdef SCTP_LOG_CLOSING
3659 sctp_log_closing(inp, stcb, 10);
3662 * When I reach here, no others want to kill the assoc yet.. and I
3663 * own the lock. Now its possible an abort comes in when I do the
3664 * lock exchange below to grab all the locks to do the final take
3665 * out. to prevent this we increment the count, which will start a
3666 * timer and blow out above thus assuring us that we hold exclusive
3667 * killing of the asoc. Note that after getting back the TCB lock we
3668 * will go ahead and increment the counter back up and stop any
3669 * timer a passing stranger may have started :-S
3671 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3672 atomic_add_int(&stcb->asoc.refcnt, 1);
3674 SCTP_TCB_UNLOCK(stcb);
3676 SCTP_ITERATOR_LOCK();
3677 SCTP_INP_INFO_WLOCK();
3678 SCTP_INP_WLOCK(inp);
3679 SCTP_TCB_LOCK(stcb);
3681 /* Double check the GONE flag */
3682 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3683 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3684 /* nothing around */
3687 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3688 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3690 * For TCP type we need special handling when we are
3691 * connected. We also include the peel'ed off ones to.
3693 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3694 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3695 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
3698 if (so->so_rcv.sb_cc == 0) {
3699 so->so_state &= ~(SS_ISCONNECTING |
3700 SS_ISDISCONNECTING |
3705 sctp_sowwakeup(inp, so);
3706 sctp_sorwakeup(inp, so);
3707 wakeup(&so->so_timeo);
3712 * Make it invalid too, that way if its about to run it will abort
3715 sctp_iterator_asoc_being_freed(inp, stcb);
3716 /* re-increment the lock */
3717 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3718 atomic_add_int(&stcb->asoc.refcnt, -1);
3721 if (inp->sctp_tcbhash) {
3722 LIST_REMOVE(stcb, sctp_tcbhash);
3724 if (stcb->asoc.in_restart_hash) {
3725 LIST_REMOVE(stcb, sctp_tcbrestarhash);
3727 /* Now lets remove it from the list of ALL associations in the EP */
3728 LIST_REMOVE(stcb, sctp_tcblist);
3729 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3730 SCTP_INP_INCR_REF(inp);
3731 SCTP_INP_WUNLOCK(inp);
3732 SCTP_ITERATOR_UNLOCK();
3734 /* pull from vtag hash */
3735 LIST_REMOVE(stcb, sctp_asocs);
3736 sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT);
3740 * Now restop the timers to be sure - this is paranoia at is finest!
3742 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3743 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3744 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3745 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3746 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3747 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3748 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3749 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3751 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3752 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3753 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3754 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3757 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
3760 * The chunk lists and such SHOULD be empty but we check them just
3763 /* anything on the wheel needs to be removed */
3764 for (i = 0; i < asoc->streamoutcnt; i++) {
3765 struct sctp_stream_out *outs;
3767 outs = &asoc->strmout[i];
3768 /* now clean up any chunks here */
3769 sp = TAILQ_FIRST(&outs->outqueue);
3771 TAILQ_REMOVE(&outs->outqueue, sp, next);
3773 sctp_m_freem(sp->data);
3775 sp->tail_mbuf = NULL;
3777 sctp_free_remote_addr(sp->net);
3778 sctp_free_spbufspace(stcb, asoc, sp);
3779 /* Free the zone stuff */
3780 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3781 SCTP_DECR_STRMOQ_COUNT();
3782 sp = TAILQ_FIRST(&outs->outqueue);
3786 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
3787 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
3791 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3793 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
3795 sctp_m_freem(sq->data);
3798 sctp_free_remote_addr(sq->whoFrom);
3801 /* Free the ctl entry */
3802 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3803 SCTP_DECR_READQ_COUNT();
3804 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3807 chk = TAILQ_FIRST(&asoc->free_chunks);
3809 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
3811 sctp_m_freem(chk->data);
3815 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3816 SCTP_DECR_CHK_COUNT();
3817 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
3818 asoc->free_chunk_cnt--;
3819 chk = TAILQ_FIRST(&asoc->free_chunks);
3821 /* pending send queue SHOULD be empty */
3822 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3823 chk = TAILQ_FIRST(&asoc->send_queue);
3825 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3827 sctp_m_freem(chk->data);
3831 sctp_free_remote_addr(chk->whoTo);
3832 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3833 SCTP_DECR_CHK_COUNT();
3834 chk = TAILQ_FIRST(&asoc->send_queue);
3839 printf("Freed %d from send_queue\n", ccnt);
3843 /* sent queue SHOULD be empty */
3844 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3845 chk = TAILQ_FIRST(&asoc->sent_queue);
3847 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3849 sctp_m_freem(chk->data);
3853 sctp_free_remote_addr(chk->whoTo);
3854 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3855 SCTP_DECR_CHK_COUNT();
3856 chk = TAILQ_FIRST(&asoc->sent_queue);
3861 printf("Freed %d from sent_queue\n", ccnt);
3865 /* control queue MAY not be empty */
3866 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3867 chk = TAILQ_FIRST(&asoc->control_send_queue);
3869 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3871 sctp_m_freem(chk->data);
3875 sctp_free_remote_addr(chk->whoTo);
3876 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3877 SCTP_DECR_CHK_COUNT();
3878 chk = TAILQ_FIRST(&asoc->control_send_queue);
3883 printf("Freed %d from ctrl_queue\n", ccnt);
3887 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3888 chk = TAILQ_FIRST(&asoc->reasmqueue);
3890 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3892 sctp_m_freem(chk->data);
3895 sctp_free_remote_addr(chk->whoTo);
3897 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3898 SCTP_DECR_CHK_COUNT();
3899 chk = TAILQ_FIRST(&asoc->reasmqueue);
3904 printf("Freed %d from reasm_queue\n", ccnt);
3908 if (asoc->mapping_array) {
3909 SCTP_FREE(asoc->mapping_array);
3910 asoc->mapping_array = NULL;
3912 /* the stream outs */
3913 if (asoc->strmout) {
3914 SCTP_FREE(asoc->strmout);
3915 asoc->strmout = NULL;
3917 asoc->streamoutcnt = 0;
3919 struct sctp_queued_to_read *ctl;
3921 for (i = 0; i < asoc->streamincnt; i++) {
3922 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3923 /* We have somethings on the streamin queue */
3924 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3926 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3928 sctp_free_remote_addr(ctl->whoFrom);
3930 sctp_m_freem(ctl->data);
3934 * We don't free the address here
3935 * since all the net's were freed
3938 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
3939 SCTP_DECR_READQ_COUNT();
3940 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3944 SCTP_FREE(asoc->strmin);
3945 asoc->strmin = NULL;
3947 asoc->streamincnt = 0;
3948 while (!TAILQ_EMPTY(&asoc->nets)) {
3949 net = TAILQ_FIRST(&asoc->nets);
3950 /* pull from list */
3951 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3953 panic("no net's left alloc'ed, or list points to itself");
3958 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3959 sctp_free_remote_addr(net);
3962 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
3963 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
3964 sctp_remove_laddr(laddr);
3967 /* pending asconf (address) parameters */
3968 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3969 aparam = TAILQ_FIRST(&asoc->asconf_queue);
3970 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3973 if (asoc->last_asconf_ack_sent != NULL) {
3974 sctp_m_freem(asoc->last_asconf_ack_sent);
3975 asoc->last_asconf_ack_sent = NULL;
3977 /* clean up auth stuff */
3978 if (asoc->local_hmacs)
3979 sctp_free_hmaclist(asoc->local_hmacs);
3980 if (asoc->peer_hmacs)
3981 sctp_free_hmaclist(asoc->peer_hmacs);
3983 if (asoc->local_auth_chunks)
3984 sctp_free_chunklist(asoc->local_auth_chunks);
3985 if (asoc->peer_auth_chunks)
3986 sctp_free_chunklist(asoc->peer_auth_chunks);
3988 sctp_free_authinfo(&asoc->authinfo);
3990 shared_key = LIST_FIRST(&asoc->shared_keys);
3991 while (shared_key) {
3992 LIST_REMOVE(shared_key, next);
3993 sctp_free_sharedkey(shared_key);
3994 shared_key = LIST_FIRST(&asoc->shared_keys);
3997 /* Insert new items here :> */
3999 /* Get rid of LOCK */
4000 SCTP_TCB_LOCK_DESTROY(stcb);
4001 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4002 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4003 SCTP_INP_INFO_WUNLOCK();
4004 SCTP_INP_RLOCK(inp);
4006 #ifdef SCTP_TRACK_FREED_ASOCS
4007 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4008 /* now clean up the tasoc itself */
4009 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4010 SCTP_DECR_ASOC_COUNT();
4012 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
4015 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4016 SCTP_DECR_ASOC_COUNT();
4018 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4019 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4021 * If its NOT the inp_free calling us AND sctp_close
4022 * as been called, we call back...
4024 SCTP_INP_RUNLOCK(inp);
4026 * This will start the kill timer (if we are the
4027 * lastone) since we hold an increment yet. But this
4028 * is the only safe way to do this since otherwise
4029 * if the socket closes at the same time we are here
4030 * we might collide in the cleanup.
4032 sctp_inpcb_free(inp, 0, 0);
4033 SCTP_INP_DECR_REF(inp);
4036 /* The socket is still open. */
4037 SCTP_INP_DECR_REF(inp);
4040 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4041 SCTP_INP_RUNLOCK(inp);
4044 /* destroyed the asoc */
4045 #ifdef SCTP_LOG_CLOSING
4046 sctp_log_closing(inp, NULL, 11);
4054 * determine if a destination is "reachable" based upon the addresses bound
4055 * to the current endpoint (e.g. only v4 or v6 currently bound)
4058 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4059 * assoc level v4/v6 flags, as the assoc *may* not have the same address
4060 * types bound as its endpoint
4063 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4065 struct sctp_inpcb *inp;
4069 * No locks here, the TCB, in all cases is already locked and an
4070 * assoc is up. There is either a INP lock by the caller applied (in
4071 * asconf case when deleting an address) or NOT in the HB case,
4072 * however if HB then the INP increment is up and the INP will not
4073 * be removed (on top of the fact that we have a TCB lock). So we
4074 * only want to read the sctp_flags, which is either bound-all or
4075 * not.. no protection needed since once an assoc is up you can't be
4076 * changing your binding.
4078 inp = stcb->sctp_ep;
4079 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4080 /* if bound all, destination is not restricted */
4082 * RRS: Question during lock work: Is this correct? If you
4083 * are bound-all you still might need to obey the V4--V6
4084 * flags??? IMO this bound-all stuff needs to be removed!
4088 /* NOTE: all "scope" checks are done when local addresses are added */
4089 if (destaddr->sa_family == AF_INET6) {
4090 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4091 } else if (destaddr->sa_family == AF_INET) {
4092 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4094 /* invalid family, so it's unreachable */
4101 * update the inp_vflags on an endpoint
4104 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4106 struct sctp_laddr *laddr;
4108 /* first clear the flag */
4109 inp->ip_inp.inp.inp_vflag = 0;
4110 /* set the flag based on addresses on the ep list */
4111 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4112 if (laddr->ifa == NULL) {
4114 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
4115 printf("An ounce of prevention is worth a pound of cure\n");
4117 #endif /* SCTP_DEBUG */
4120 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4123 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4124 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4125 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4126 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4132 * Add the address to the endpoint local address list There is nothing to be
4133 * done if we are bound to all addresses
4136 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
4138 struct sctp_laddr *laddr;
4143 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4144 /* You are already bound to all. You have it already */
4147 if (ifa->address.sa.sa_family == AF_INET6) {
4148 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4149 /* Can't bind a non-useable addr. */
4153 /* first, is it already present? */
4154 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4155 if (laddr->ifa == ifa) {
4162 /* Not in the ep list */
4163 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
4167 /* update inp_vflag flags */
4168 if (ifa->address.sa.sa_family == AF_INET6) {
4169 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4170 } else if (ifa->address.sa.sa_family == AF_INET) {
4171 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4179 * select a new (hopefully reachable) destination net (should only be used
4180 * when we deleted an ep addr that is the only usable source address to reach
4181 * the destination net)
4184 sctp_select_primary_destination(struct sctp_tcb *stcb)
4186 struct sctp_nets *net;
4188 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4189 /* for now, we'll just pick the first reachable one we find */
4190 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
4192 if (sctp_destination_is_reachable(stcb,
4193 (struct sockaddr *)&net->ro._l_addr)) {
4194 /* found a reachable destination */
4195 stcb->asoc.primary_destination = net;
4198 /* I can't there from here! ...we're gonna die shortly... */
4203 * Delete the address from the endpoint local address list There is nothing
4204 * to be done if we are bound to all addresses
4207 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4209 struct sctp_laddr *laddr;
4213 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4214 /* You are already bound to all. You have it already */
4217 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4218 if (laddr->ifa == ifa) {
4223 if (fnd && (inp->laddr_count < 2)) {
4224 /* can't delete unless there are at LEAST 2 addresses */
4229 * clean up any use of this address go through our
4230 * associations and clear any last_used_address that match
4231 * this one for each assoc, see if a new primary_destination
4234 struct sctp_tcb *stcb;
4236 /* clean up "next_addr_touse" */
4237 if (inp->next_addr_touse == laddr)
4238 /* delete this address */
4239 inp->next_addr_touse = NULL;
4241 /* clean up "last_used_address" */
4242 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4243 struct sctp_nets *net;
4245 SCTP_TCB_LOCK(stcb);
4246 if (stcb->asoc.last_used_address == laddr)
4247 /* delete this address */
4248 stcb->asoc.last_used_address = NULL;
4250 * Now spin through all the nets and purge any ref
4253 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4254 if (net->ro._s_addr &&
4255 (net->ro._s_addr->ifa == laddr->ifa)) {
4256 /* Yep, purge src address selected */
4259 /* delete this address if cached */
4263 net->ro.ro_rt = NULL;
4265 sctp_free_ifa(net->ro._s_addr);
4266 net->ro._s_addr = NULL;
4267 net->src_addr_selected = 0;
4270 SCTP_TCB_UNLOCK(stcb);
4271 } /* for each tcb */
4272 /* remove it from the ep list */
4273 sctp_remove_laddr(laddr);
4275 /* update inp_vflag flags */
4276 sctp_update_ep_vflag(inp);
4282 * Add the addr to the TCB local address list For the BOUNDALL or dynamic
4283 * case, this is a "pending" address list (eg. addresses waiting for an
4284 * ASCONF-ACK response) For the subset binding, static case, this is a
4285 * "valid" address list
4288 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa, int restricted_list)
4290 struct sctp_inpcb *inp;
4291 struct sctp_laddr *laddr;
4292 struct sctpladdr *list;
4296 * Assumes TCB is locked.. and possibly the INP. May need to
4297 * confirm/fix that if we need it and is not the case.
4299 list = &stcb->asoc.sctp_restricted_addrs;
4301 inp = stcb->sctp_ep;
4302 if (ifa->address.sa.sa_family == AF_INET6) {
4303 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4304 /* Can't bind a non-existent addr. */
4308 /* does the address already exist? */
4309 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
4310 if (laddr->ifa == ifa) {
4315 /* add to the list */
4316 error = sctp_insert_laddr(list, ifa, 0);
4323 * insert an laddr entry with the given ifa for the desired list
4326 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
4328 struct sctp_laddr *laddr;
4330 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
4331 if (laddr == NULL) {
4332 /* out of memory? */
4335 SCTP_INCR_LADDR_COUNT();
4336 bzero(laddr, sizeof(*laddr));
4338 laddr->action = act;
4339 atomic_add_int(&ifa->refcount, 1);
4341 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
4347 * Remove an laddr entry from the local address list (on an assoc)
4350 sctp_remove_laddr(struct sctp_laddr *laddr)
4353 /* remove from the list */
4354 LIST_REMOVE(laddr, sctp_nxt_addr);
4355 sctp_free_ifa(laddr->ifa);
4356 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
4357 SCTP_DECR_LADDR_COUNT();
4361 * Remove an address from the TCB local address list
4364 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4366 struct sctp_inpcb *inp;
4367 struct sctp_laddr *laddr;
4370 * This is called by asconf work. It is assumed that a) The TCB is
4371 * locked and b) The INP is locked. This is true in as much as I can
4372 * trace through the entry asconf code where I did these locks.
4373 * Again, the ASCONF code is a bit different in that it does lock
4374 * the INP during its work often times. This must be since we don't
4375 * want other proc's looking up things while what they are looking
4376 * up is changing :-D
4379 inp = stcb->sctp_ep;
4380 /* if subset bound and don't allow ASCONF's, can't delete last */
4381 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4382 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4383 if (stcb->asoc.numnets < 2) {
4384 /* can't delete last address */
4388 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
4389 /* remove the address if it exists */
4390 if (laddr->ifa == NULL)
4392 if (laddr->ifa == ifa) {
4393 sctp_remove_laddr(laddr);
4398 /* address not found! */
4402 static char sctp_pcb_initialized = 0;
4405 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
4408 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4409 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4415 * SCTP initialization for the PCB structures should be called by
4416 * the sctp_init() funciton.
4420 if (sctp_pcb_initialized != 0) {
4421 /* error I was called twice */
4424 sctp_pcb_initialized = 1;
4426 bzero(&sctpstat, sizeof(struct sctpstat));
4427 SCTP_GETTIME_TIMEVAL(&sctpstat.sctps_discontinuitytime);
4428 /* init the empty list of (All) Endpoints */
4429 LIST_INIT(&sctppcbinfo.listhead);
4431 /* init the iterator head */
4432 TAILQ_INIT(&sctppcbinfo.iteratorhead);
4434 /* init the hash table of endpoints */
4435 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
4436 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4437 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4438 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
4439 &sctppcbinfo.hashasocmark);
4440 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
4441 &sctppcbinfo.hashmark);
4442 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
4443 &sctppcbinfo.hashtcpmark);
4444 sctppcbinfo.hashtblsize = sctp_hashtblsize;
4446 /* init the small hash table we use to track restarted asoc's */
4447 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
4448 &sctppcbinfo.hashrestartmark);
4451 sctppcbinfo.sctp_vrfhash = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
4452 &sctppcbinfo.hashvrfmark);
4454 /* init the zones */
4456 * FIX ME: Should check for NULL returns, but if it does fail we are
4457 * doomed to panic anyways... add later maybe.
4459 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4460 sizeof(struct sctp_inpcb), maxsockets);
4462 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4463 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4465 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4466 sizeof(struct sctp_laddr),
4467 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4469 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4470 sizeof(struct sctp_nets),
4471 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4473 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4474 sizeof(struct sctp_tmit_chunk),
4475 (sctp_max_number_of_assoc * sctp_chunkscale));
4477 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
4478 sizeof(struct sctp_queued_to_read),
4479 (sctp_max_number_of_assoc * sctp_chunkscale));
4481 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
4482 sizeof(struct sctp_stream_queue_pending),
4483 (sctp_max_number_of_assoc * sctp_chunkscale));
4485 /* Master Lock INIT for info structure */
4486 SCTP_INP_INFO_LOCK_INIT();
4487 SCTP_STATLOG_INIT_LOCK();
4488 SCTP_ITERATOR_LOCK_INIT();
4490 SCTP_IPI_COUNT_INIT();
4491 SCTP_IPI_ADDR_INIT();
4492 SCTP_IPI_ITERATOR_WQ_INIT();
4494 LIST_INIT(&sctppcbinfo.addr_wq);
4496 /* not sure if we need all the counts */
4497 sctppcbinfo.ipi_count_ep = 0;
4498 /* assoc/tcb zone info */
4499 sctppcbinfo.ipi_count_asoc = 0;
4500 /* local addrlist zone info */
4501 sctppcbinfo.ipi_count_laddr = 0;
4502 /* remote addrlist zone info */
4503 sctppcbinfo.ipi_count_raddr = 0;
4505 sctppcbinfo.ipi_count_chunk = 0;
4507 /* socket queue zone info */
4508 sctppcbinfo.ipi_count_readq = 0;
4510 /* stream out queue cont */
4511 sctppcbinfo.ipi_count_strmoq = 0;
4513 sctppcbinfo.ipi_free_strmoq = 0;
4514 sctppcbinfo.ipi_free_chunks = 0;
4516 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
4518 /* Init the TIMEWAIT list */
4519 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
4520 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4523 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
4524 sctppcbinfo.iterator_running = 0;
4525 sctp_startup_iterator();
4529 * INIT the default VRF which for BSD is the only one, other O/S's
4530 * may have more. But initially they must start with one and then
4531 * add the VRF's as addresses are added.
4533 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
4539 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4540 int iphlen, int offset, int limit, struct sctphdr *sh,
4541 struct sockaddr *altsa)
4544 * grub through the INIT pulling addresses and loading them to the
4545 * nets structure in the asoc. The from address in the mbuf should
4546 * also be loaded (if it is not already). This routine can be called
4547 * with either INIT or INIT-ACK's as long as the m points to the IP
4548 * packet and the offset points to the beginning of the parameters.
4550 struct sctp_inpcb *inp, *l_inp;
4551 struct sctp_nets *net, *net_tmp;
4553 struct sctp_paramhdr *phdr, parm_buf;
4554 struct sctp_tcb *stcb_tmp;
4555 uint16_t ptype, plen;
4556 struct sockaddr *sa;
4557 struct sockaddr_storage dest_store;
4558 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4559 struct sockaddr_in sin;
4560 struct sockaddr_in6 sin6;
4561 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
4562 struct sctp_auth_random *p_random = NULL;
4563 uint16_t random_len = 0;
4564 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
4565 struct sctp_auth_hmac_algo *hmacs = NULL;
4566 uint16_t hmacs_len = 0;
4567 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
4568 struct sctp_auth_chunk_list *chunks = NULL;
4569 uint16_t num_chunks = 0;
4570 sctp_key_t *new_key;
4572 int got_random = 0, got_hmacs = 0, got_chklist = 0;
4574 /* First get the destination address setup too. */
4575 memset(&sin, 0, sizeof(sin));
4576 memset(&sin6, 0, sizeof(sin6));
4578 sin.sin_family = AF_INET;
4579 sin.sin_len = sizeof(sin);
4580 sin.sin_port = stcb->rport;
4582 sin6.sin6_family = AF_INET6;
4583 sin6.sin6_len = sizeof(struct sockaddr_in6);
4584 sin6.sin6_port = stcb->rport;
4585 if (altsa == NULL) {
4586 iph = mtod(m, struct ip *);
4587 if (iph->ip_v == IPVERSION) {
4589 struct sockaddr_in *sin_2;
4591 sin_2 = (struct sockaddr_in *)(local_sa);
4592 memset(sin_2, 0, sizeof(sin));
4593 sin_2->sin_family = AF_INET;
4594 sin_2->sin_len = sizeof(sin);
4595 sin_2->sin_port = sh->dest_port;
4596 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
4597 sin.sin_addr = iph->ip_src;
4598 sa = (struct sockaddr *)&sin;
4599 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4601 struct ip6_hdr *ip6;
4602 struct sockaddr_in6 *sin6_2;
4604 ip6 = mtod(m, struct ip6_hdr *);
4605 sin6_2 = (struct sockaddr_in6 *)(local_sa);
4606 memset(sin6_2, 0, sizeof(sin6));
4607 sin6_2->sin6_family = AF_INET6;
4608 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4609 sin6_2->sin6_port = sh->dest_port;
4610 sin6.sin6_addr = ip6->ip6_src;
4611 sa = (struct sockaddr *)&sin6;
4617 * For cookies we use the src address NOT from the packet
4618 * but from the original INIT
4622 /* Turn off ECN until we get through all params */
4623 stcb->asoc.ecn_allowed = 0;
4624 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4625 /* mark all addresses that we have currently on the list */
4626 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4628 /* does the source address already exist? if so skip it */
4629 l_inp = inp = stcb->sctp_ep;
4631 atomic_add_int(&stcb->asoc.refcnt, 1);
4632 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4633 atomic_add_int(&stcb->asoc.refcnt, -1);
4635 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4636 /* we must add the source address */
4637 /* no scope set here since we have a tcb already. */
4638 if ((sa->sa_family == AF_INET) &&
4639 (stcb->asoc.ipv4_addr_legal)) {
4640 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
4643 } else if ((sa->sa_family == AF_INET6) &&
4644 (stcb->asoc.ipv6_addr_legal)) {
4645 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
4650 if (net_tmp != NULL && stcb_tmp == stcb) {
4651 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4652 } else if (stcb_tmp != stcb) {
4653 /* It belongs to another association? */
4654 SCTP_TCB_UNLOCK(stcb_tmp);
4658 if (stcb->asoc.state == 0) {
4659 /* the assoc was freed? */
4662 /* now we must go through each of the params. */
4663 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4665 ptype = ntohs(phdr->param_type);
4666 plen = ntohs(phdr->param_length);
4668 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
4671 if (offset + plen > limit) {
4677 if (ptype == SCTP_IPV4_ADDRESS) {
4678 if (stcb->asoc.ipv4_addr_legal) {
4679 struct sctp_ipv4addr_param *p4, p4_buf;
4681 /* ok get the v4 address and check/add */
4682 phdr = sctp_get_next_param(m, offset,
4683 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4684 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4688 p4 = (struct sctp_ipv4addr_param *)phdr;
4689 sin.sin_addr.s_addr = p4->addr;
4690 sa = (struct sockaddr *)&sin;
4691 inp = stcb->sctp_ep;
4692 atomic_add_int(&stcb->asoc.refcnt, 1);
4693 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4695 atomic_add_int(&stcb->asoc.refcnt, -1);
4697 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
4699 /* we must add the source address */
4701 * no scope set since we have a tcb
4706 * we must validate the state again
4709 if (stcb->asoc.state == 0) {
4710 /* the assoc was freed? */
4713 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
4716 } else if (stcb_tmp == stcb) {
4717 if (stcb->asoc.state == 0) {
4718 /* the assoc was freed? */
4724 ~SCTP_ADDR_NOT_IN_ASSOC;
4728 * strange, address is in another
4729 * assoc? straighten out locks.
4731 if (stcb->asoc.state == 0) {
4732 /* the assoc was freed? */
4738 } else if (ptype == SCTP_IPV6_ADDRESS) {
4739 if (stcb->asoc.ipv6_addr_legal) {
4740 /* ok get the v6 address and check/add */
4741 struct sctp_ipv6addr_param *p6, p6_buf;
4743 phdr = sctp_get_next_param(m, offset,
4744 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4745 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4749 p6 = (struct sctp_ipv6addr_param *)phdr;
4750 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4752 sa = (struct sockaddr *)&sin6;
4753 inp = stcb->sctp_ep;
4754 atomic_add_int(&stcb->asoc.refcnt, 1);
4755 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4757 atomic_add_int(&stcb->asoc.refcnt, -1);
4758 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4761 * we must validate the state again
4764 if (stcb->asoc.state == 0) {
4765 /* the assoc was freed? */
4769 * we must add the address, no scope
4772 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
4775 } else if (stcb_tmp == stcb) {
4777 * we must validate the state again
4780 if (stcb->asoc.state == 0) {
4781 /* the assoc was freed? */
4787 ~SCTP_ADDR_NOT_IN_ASSOC;
4791 * strange, address is in another
4792 * assoc? straighten out locks.
4794 if (stcb->asoc.state == 0) {
4795 /* the assoc was freed? */
4801 } else if (ptype == SCTP_ECN_CAPABLE) {
4802 stcb->asoc.ecn_allowed = 1;
4803 } else if (ptype == SCTP_ULP_ADAPTATION) {
4804 if (stcb->asoc.state != SCTP_STATE_OPEN) {
4805 struct sctp_adaptation_layer_indication ai,
4808 phdr = sctp_get_next_param(m, offset,
4809 (struct sctp_paramhdr *)&ai, sizeof(ai));
4810 aip = (struct sctp_adaptation_layer_indication *)phdr;
4811 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION,
4812 stcb, ntohl(aip->indication), NULL);
4814 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4815 struct sctp_asconf_addr_param lstore, *fee;
4816 struct sctp_asconf_addrv4_param *fii;
4818 struct sockaddr *lsa = NULL;
4820 stcb->asoc.peer_supports_asconf = 1;
4821 if (plen > sizeof(lstore)) {
4824 phdr = sctp_get_next_param(m, offset,
4825 (struct sctp_paramhdr *)&lstore, plen);
4829 fee = (struct sctp_asconf_addr_param *)phdr;
4830 lptype = ntohs(fee->addrp.ph.param_type);
4831 if (lptype == SCTP_IPV4_ADDRESS) {
4833 sizeof(struct sctp_asconf_addrv4_param)) {
4834 printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4835 (int)sizeof(struct sctp_asconf_addrv4_param),
4838 fii = (struct sctp_asconf_addrv4_param *)fee;
4839 sin.sin_addr.s_addr = fii->addrp.addr;
4840 lsa = (struct sockaddr *)&sin;
4842 } else if (lptype == SCTP_IPV6_ADDRESS) {
4844 sizeof(struct sctp_asconf_addr_param)) {
4845 printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4846 (int)sizeof(struct sctp_asconf_addr_param),
4849 memcpy(sin6.sin6_addr.s6_addr,
4851 sizeof(fee->addrp.addr));
4852 lsa = (struct sockaddr *)&sin6;
4856 sctp_set_primary_addr(stcb, sa, NULL);
4858 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4859 /* Peer supports pr-sctp */
4860 stcb->asoc.peer_supports_prsctp = 1;
4861 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4862 /* A supported extension chunk */
4863 struct sctp_supported_chunk_types_param *pr_supported;
4864 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
4867 phdr = sctp_get_next_param(m, offset,
4868 (struct sctp_paramhdr *)&local_store, plen);
4872 stcb->asoc.peer_supports_asconf = 0;
4873 stcb->asoc.peer_supports_prsctp = 0;
4874 stcb->asoc.peer_supports_pktdrop = 0;
4875 stcb->asoc.peer_supports_strreset = 0;
4876 stcb->asoc.peer_supports_auth = 0;
4877 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4878 num_ent = plen - sizeof(struct sctp_paramhdr);
4879 for (i = 0; i < num_ent; i++) {
4880 switch (pr_supported->chunk_types[i]) {
4882 case SCTP_ASCONF_ACK:
4883 stcb->asoc.peer_supports_asconf = 1;
4885 case SCTP_FORWARD_CUM_TSN:
4886 stcb->asoc.peer_supports_prsctp = 1;
4888 case SCTP_PACKET_DROPPED:
4889 stcb->asoc.peer_supports_pktdrop = 1;
4891 case SCTP_STREAM_RESET:
4892 stcb->asoc.peer_supports_strreset = 1;
4894 case SCTP_AUTHENTICATION:
4895 stcb->asoc.peer_supports_auth = 1;
4898 /* one I have not learned yet */
4903 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4904 /* Peer supports ECN-nonce */
4905 stcb->asoc.peer_supports_ecn_nonce = 1;
4906 stcb->asoc.ecn_nonce_allowed = 1;
4907 } else if (ptype == SCTP_RANDOM) {
4908 if (plen > sizeof(random_store))
4911 /* already processed a RANDOM */
4914 phdr = sctp_get_next_param(m, offset,
4915 (struct sctp_paramhdr *)random_store,
4919 p_random = (struct sctp_auth_random *)phdr;
4920 random_len = plen - sizeof(*p_random);
4921 /* enforce the random length */
4922 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
4924 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
4925 printf("SCTP: invalid RANDOM len\n");
4930 } else if (ptype == SCTP_HMAC_LIST) {
4934 if (plen > sizeof(hmacs_store))
4937 /* already processed a HMAC list */
4940 phdr = sctp_get_next_param(m, offset,
4941 (struct sctp_paramhdr *)hmacs_store,
4945 hmacs = (struct sctp_auth_hmac_algo *)phdr;
4946 hmacs_len = plen - sizeof(*hmacs);
4947 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
4948 /* validate the hmac list */
4949 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
4952 if (stcb->asoc.peer_hmacs != NULL)
4953 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
4954 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
4955 if (stcb->asoc.peer_hmacs != NULL) {
4956 for (i = 0; i < num_hmacs; i++) {
4957 sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
4958 ntohs(hmacs->hmac_ids[i]));
4962 } else if (ptype == SCTP_CHUNK_LIST) {
4965 if (plen > sizeof(chunks_store))
4968 /* already processed a Chunks list */
4971 phdr = sctp_get_next_param(m, offset,
4972 (struct sctp_paramhdr *)chunks_store,
4976 chunks = (struct sctp_auth_chunk_list *)phdr;
4977 num_chunks = plen - sizeof(*chunks);
4978 if (stcb->asoc.peer_auth_chunks != NULL)
4979 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
4981 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
4982 for (i = 0; i < num_chunks; i++) {
4983 sctp_auth_add_chunk(chunks->chunk_types[i],
4984 stcb->asoc.peer_auth_chunks);
4987 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4988 (ptype == SCTP_STATE_COOKIE) ||
4989 (ptype == SCTP_UNRECOG_PARAM) ||
4990 (ptype == SCTP_COOKIE_PRESERVE) ||
4991 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4992 (ptype == SCTP_ADD_IP_ADDRESS) ||
4993 (ptype == SCTP_DEL_IP_ADDRESS) ||
4994 (ptype == SCTP_ERROR_CAUSE_IND) ||
4995 (ptype == SCTP_SUCCESS_REPORT)) {
4998 if ((ptype & 0x8000) == 0x0000) {
5000 * must stop processing the rest of the
5001 * param's. Any report bits were handled
5003 * sctp_arethere_unrecognized_parameters()
5004 * when the INIT or INIT-ACK was first seen.
5010 offset += SCTP_SIZE32(plen);
5011 if (offset >= limit) {
5014 phdr = sctp_get_next_param(m, offset, &parm_buf,
5017 /* Now check to see if we need to purge any addresses */
5018 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
5019 net_tmp = TAILQ_NEXT(net, sctp_next);
5020 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
5021 SCTP_ADDR_NOT_IN_ASSOC) {
5022 /* This address has been removed from the asoc */
5023 /* remove and free it */
5024 stcb->asoc.numnets--;
5025 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
5026 sctp_free_remote_addr(net);
5027 if (net == stcb->asoc.primary_destination) {
5028 stcb->asoc.primary_destination = NULL;
5029 sctp_select_primary_destination(stcb);
5033 /* validate authentication required parameters */
5034 if (got_random && got_hmacs) {
5035 stcb->asoc.peer_supports_auth = 1;
5037 stcb->asoc.peer_supports_auth = 0;
5039 if (!stcb->asoc.peer_supports_auth && got_chklist) {
5040 /* peer does not support auth but sent a chunks list? */
5043 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
5044 !stcb->asoc.peer_supports_auth) {
5045 /* peer supports asconf but not auth? */
5048 /* concatenate the full random key */
5049 #ifdef SCTP_AUTH_DRAFT_04
5050 keylen = random_len;
5051 new_key = sctp_alloc_key(keylen);
5052 if (new_key != NULL) {
5053 /* copy in the RANDOM */
5054 if (p_random != NULL)
5055 bcopy(p_random->random_data, new_key->key, random_len);
5058 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
5059 sizeof(*hmacs) + hmacs_len;
5060 new_key = sctp_alloc_key(keylen);
5061 if (new_key != NULL) {
5062 /* copy in the RANDOM */
5063 if (p_random != NULL) {
5064 keylen = sizeof(*p_random) + random_len;
5065 bcopy(p_random, new_key->key, keylen);
5067 /* append in the AUTH chunks */
5068 if (chunks != NULL) {
5069 bcopy(chunks, new_key->key + keylen,
5070 sizeof(*chunks) + num_chunks);
5071 keylen += sizeof(*chunks) + num_chunks;
5073 /* append in the HMACs */
5074 if (hmacs != NULL) {
5075 bcopy(hmacs, new_key->key + keylen,
5076 sizeof(*hmacs) + hmacs_len);
5081 /* failed to get memory for the key */
5084 if (stcb->asoc.authinfo.peer_random != NULL)
5085 sctp_free_key(stcb->asoc.authinfo.peer_random);
5086 stcb->asoc.authinfo.peer_random = new_key;
5087 #ifdef SCTP_AUTH_DRAFT_04
5088 /* don't include the chunks and hmacs for draft -04 */
5089 stcb->asoc.authinfo.peer_random->keylen = random_len;
5091 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
5092 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
5098 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
5099 struct sctp_nets *net)
5101 /* make sure the requested primary address exists in the assoc */
5102 if (net == NULL && sa)
5103 net = sctp_findnet(stcb, sa);
5106 /* didn't find the requested primary address! */
5109 /* set the primary address */
5110 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
5111 /* Must be confirmed, so queue to set */
5112 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
5115 stcb->asoc.primary_destination = net;
5116 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
5117 net = TAILQ_FIRST(&stcb->asoc.nets);
5118 if (net != stcb->asoc.primary_destination) {
5120 * first one on the list is NOT the primary
5121 * sctp_cmpaddr() is much more efficent if the
5122 * primary is the first on the list, make it so.
5124 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5125 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5133 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now)
5136 * This function serves two purposes. It will see if a TAG can be
5137 * re-used and return 1 for yes it is ok and 0 for don't use that
5138 * tag. A secondary function it will do is purge out old tags that
5141 struct sctpasochead *head;
5142 struct sctpvtaghead *chain;
5143 struct sctp_tagblock *twait_block;
5144 struct sctp_tcb *stcb;
5147 SCTP_INP_INFO_WLOCK();
5148 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5149 /* First is the vtag in use ? */
5151 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
5152 sctppcbinfo.hashasocmark)];
5156 LIST_FOREACH(stcb, head, sctp_asocs) {
5158 if (stcb->asoc.my_vtag == tag) {
5160 * We should remove this if and return 0 always if
5161 * we want vtags unique across all endpoints. For
5162 * now within a endpoint is ok.
5164 if (inp == stcb->sctp_ep) {
5165 /* bad tag, in use */
5166 SCTP_INP_INFO_WUNLOCK();
5172 /* Now lets check the restart hash */
5173 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
5174 sctppcbinfo.hashrestartmark)];
5176 goto check_time_wait;
5178 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
5179 if (stcb->asoc.assoc_id == tag) {
5181 if (inp == stcb->sctp_ep) {
5182 /* bad tag, in use */
5183 SCTP_INP_INFO_WUNLOCK();
5189 /* Now what about timed wait ? */
5190 if (!SCTP_LIST_EMPTY(chain)) {
5192 * Block(s) are present, lets see if we have this tag in the
5195 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5196 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5197 if (twait_block->vtag_block[i].v_tag == 0) {
5200 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
5202 /* Audit expires this guy */
5203 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5204 twait_block->vtag_block[i].v_tag = 0;
5205 } else if (twait_block->vtag_block[i].v_tag ==
5207 /* Bad tag, sorry :< */
5208 SCTP_INP_INFO_WUNLOCK();
5214 /* Not found, ok to use the tag */
5215 SCTP_INP_INFO_WUNLOCK();
5220 static sctp_assoc_t reneged_asoc_ids[256];
5221 static uint8_t reneged_at = 0;
5225 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
5228 * We must hunt this association for MBUF's past the cumack (i.e.
5229 * out of order data that we can renege on).
5231 struct sctp_association *asoc;
5232 struct sctp_tmit_chunk *chk, *nchk;
5233 uint32_t cumulative_tsn_p1, tsn;
5234 struct sctp_queued_to_read *ctl, *nctl;
5235 int cnt, strmat, gap;
5237 /* We look for anything larger than the cum-ack + 1 */
5239 SCTP_STAT_INCR(sctps_protocol_drain_calls);
5240 if (sctp_do_drain == 0) {
5244 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
5245 /* none we can reneg on. */
5248 SCTP_STAT_INCR(sctps_protocol_drains_done);
5249 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
5251 /* First look in the re-assembly queue */
5252 chk = TAILQ_FIRST(&asoc->reasmqueue);
5254 /* Get the next one */
5255 nchk = TAILQ_NEXT(chk, sctp_next);
5256 if (compare_with_wrap(chk->rec.data.TSN_seq,
5257 cumulative_tsn_p1, MAX_TSN)) {
5258 /* Yep it is above cum-ack */
5260 tsn = chk->rec.data.TSN_seq;
5261 if (tsn >= asoc->mapping_array_base_tsn) {
5262 gap = tsn - asoc->mapping_array_base_tsn;
5264 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5267 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
5268 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5269 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
5270 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5272 sctp_m_freem(chk->data);
5275 sctp_free_remote_addr(chk->whoTo);
5276 sctp_free_a_chunk(stcb, chk);
5280 /* Ok that was fun, now we will drain all the inbound streams? */
5281 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
5282 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
5284 nctl = TAILQ_NEXT(ctl, next);
5285 if (compare_with_wrap(ctl->sinfo_tsn,
5286 cumulative_tsn_p1, MAX_TSN)) {
5287 /* Yep it is above cum-ack */
5289 tsn = ctl->sinfo_tsn;
5290 if (tsn >= asoc->mapping_array_base_tsn) {
5292 asoc->mapping_array_base_tsn;
5295 asoc->mapping_array_base_tsn) +
5298 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
5299 sctp_ucount_decr(asoc->cnt_on_all_streams);
5301 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
5303 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
5306 sctp_m_freem(ctl->data);
5309 sctp_free_remote_addr(ctl->whoFrom);
5310 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
5311 SCTP_DECR_READQ_COUNT();
5317 * Question, should we go through the delivery queue? The only
5318 * reason things are on here is the app not reading OR a p-d-api up.
5319 * An attacker COULD send enough in to initiate the PD-API and then
5320 * send a bunch of stuff to other streams... these would wind up on
5321 * the delivery queue.. and then we would not get to them. But in
5322 * order to do this I then have to back-track and un-deliver
5323 * sequence numbers in streams.. el-yucko. I think for now we will
5324 * NOT look at the delivery queue and leave it to be something to
5325 * consider later. An alternative would be to abort the P-D-API with
5326 * a notification and then deliver the data.... Or another method
5327 * might be to keep track of how many times the situation occurs and
5328 * if we see a possible attack underway just abort the association.
5331 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
5333 printf("Freed %d chunks from reneg harvest\n", cnt);
5336 #endif /* SCTP_DEBUG */
5339 * Now do we need to find a new
5340 * asoc->highest_tsn_inside_map?
5342 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
5343 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
5345 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5346 asoc->highest_tsn_inside_map + 1;
5348 if (gap >= (asoc->mapping_array_size << 3)) {
5350 * Something bad happened or cum-ack and high were
5351 * behind the base, but if so earlier checks should
5352 * have found NO data... wierd... we will start at
5353 * end of mapping array.
5355 printf("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
5357 (int)(asoc->mapping_array_size << 3),
5358 (int)asoc->highest_tsn_inside_map);
5359 gap = asoc->mapping_array_size << 3;
5362 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
5363 /* found the new highest */
5364 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
5370 /* Nothing left in map */
5371 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
5372 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
5373 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
5375 asoc->last_revoke_count = cnt;
5376 SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
5377 sctp_send_sack(stcb);
5378 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN);
5379 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
5383 * Another issue, in un-setting the TSN's in the mapping array we
5384 * DID NOT adjust the higest_tsn marker. This will cause one of two
5385 * things to occur. It may cause us to do extra work in checking for
5386 * our mapping array movement. More importantly it may cause us to
5387 * SACK every datagram. This may not be a bad thing though since we
5388 * will recover once we get our cum-ack above and all this stuff we
5397 * We must walk the PCB lists for ALL associations here. The system
5398 * is LOW on MBUF's and needs help. This is where reneging will
5399 * occur. We really hope this does NOT happen!
5401 struct sctp_inpcb *inp;
5402 struct sctp_tcb *stcb;
5404 SCTP_INP_INFO_RLOCK();
5405 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
5406 /* For each endpoint */
5407 SCTP_INP_RLOCK(inp);
5408 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5409 /* For each association */
5410 SCTP_TCB_LOCK(stcb);
5411 sctp_drain_mbufs(inp, stcb);
5412 SCTP_TCB_UNLOCK(stcb);
5414 SCTP_INP_RUNLOCK(inp);
5416 SCTP_INP_INFO_RUNLOCK();
5420 * start a new iterator
5421 * iterates through all endpoints and associations based on the pcb_state
5422 * flags and asoc_state. "af" (mandatory) is executed for all matching
5423 * assocs and "ef" (optional) is executed when the iterator completes.
5424 * "inpf" (optional) is executed for each new endpoint as it is being
5425 * iterated through. inpe (optional) is called when the inp completes
5426 * its way through all the stcbs.
5429 sctp_initiate_iterator(inp_func inpf,
5433 uint32_t pcb_features,
5434 uint32_t asoc_state,
5438 struct sctp_inpcb *s_inp,
5439 uint8_t chunk_output_off)
5441 struct sctp_iterator *it = NULL;
5446 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
5451 memset(it, 0, sizeof(*it));
5452 it->function_assoc = af;
5453 it->function_inp = inpf;
5455 it->done_current_ep = 0;
5457 it->done_current_ep = 1;
5458 it->function_atend = ef;
5461 it->pcb_flags = pcb_state;
5462 it->pcb_features = pcb_features;
5463 it->asoc_state = asoc_state;
5464 it->function_inp_end = inpe;
5465 it->no_chunk_output = chunk_output_off;
5468 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5470 SCTP_INP_INFO_RLOCK();
5471 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5473 SCTP_INP_INFO_RUNLOCK();
5474 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5477 SCTP_IPI_ITERATOR_WQ_LOCK();
5479 SCTP_INP_INCR_REF(it->inp);
5480 TAILQ_INSERT_TAIL(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5481 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5482 if (sctppcbinfo.iterator_running == 0) {
5483 sctp_wakeup_iterator();
5485 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5488 SCTP_INP_DECR_REF(it->inp);
5489 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5490 /* Init the timer */
5491 SCTP_OS_TIMER_INIT(&it->tmr.timer);
5492 /* add to the list of all iterators */
5493 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,