blob: d5dc0973b8ed33123697025d49bcbec2aa598746 [file] [log] [blame]
tuexendd729232011-11-01 23:04:43 +00001/*-
Michael Tuexen866a7312017-11-24 12:44:05 +01002 * SPDX-License-Identifier: BSD-3-Clause
3 *
tuexendd729232011-11-01 23:04:43 +00004 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
tuexen194eae12012-05-23 12:03:48 +00005 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
tuexendd729232011-11-01 23:04:43 +00007 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * a) Redistributions of source code must retain the above copyright notice,
tuexen9784e9a2011-12-18 13:04:23 +000012 * this list of conditions and the following disclaimer.
tuexendd729232011-11-01 23:04:43 +000013 *
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
tuexen9784e9a2011-12-18 13:04:23 +000016 * the documentation and/or other materials provided with the distribution.
tuexendd729232011-11-01 23:04:43 +000017 *
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
Michael Tuexen5be0c252020-06-13 00:53:56 +020035#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +000036#include <sys/cdefs.h>
Michael Tuexen6e00fe22021-02-21 17:31:55 +010037__FBSDID("$FreeBSD$");
tuexendd729232011-11-01 23:04:43 +000038#endif
39
40#include <netinet/sctp_os.h>
Michael Tuexen5be0c252020-06-13 00:53:56 +020041#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +000042#include <sys/proc.h>
43#endif
44#include <netinet/sctp_var.h>
45#include <netinet/sctp_sysctl.h>
46#include <netinet/sctp_pcb.h>
47#include <netinet/sctputil.h>
48#include <netinet/sctp.h>
49#include <netinet/sctp_header.h>
50#include <netinet/sctp_asconf.h>
51#include <netinet/sctp_output.h>
52#include <netinet/sctp_timer.h>
53#include <netinet/sctp_bsd_addr.h>
t00fcxen1ce83bb2014-07-11 07:17:36 +000054#if defined(INET) || defined(INET6)
Michael Tuexeneb611572020-06-13 17:27:02 +020055#if !defined(_WIN32)
tuexendd729232011-11-01 23:04:43 +000056#include <netinet/udp.h>
tuexen6019b072011-12-15 18:38:03 +000057#endif
t00fcxen5ab37c82014-06-30 20:55:44 +000058#endif
tuexendd729232011-11-01 23:04:43 +000059#ifdef INET6
tuexen5154a002011-12-23 18:47:55 +000060#if defined(__Userspace__)
61#include "user_ip6_var.h"
62#else
tuexendd729232011-11-01 23:04:43 +000063#include <netinet6/ip6_var.h>
64#endif
tuexen5154a002011-12-23 18:47:55 +000065#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +020066#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +000067#include <sys/sched.h>
68#include <sys/smp.h>
tuexenc0bb6122012-01-15 14:21:21 +000069#include <sys/unistd.h>
tuexendd729232011-11-01 23:04:43 +000070#endif
tuexen5154a002011-12-23 18:47:55 +000071#if defined(__Userspace__)
72#include <user_socketvar.h>
Michael Tuexen1a0619d2019-07-28 22:10:44 +020073#include <user_atomic.h>
Michael Tuexeneb611572020-06-13 17:27:02 +020074#if !defined(_WIN32)
t00fcxen37058da2015-03-16 21:59:21 +000075#include <netdb.h>
76#endif
tuexen5154a002011-12-23 18:47:55 +000077#endif
tuexendd729232011-11-01 23:04:43 +000078
Michael Tuexen47987ce2020-06-22 16:41:44 +020079#if !defined(__FreeBSD__) || defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +000080struct sctp_base_info system_base_info;
tuexendd729232011-11-01 23:04:43 +000081
Michael Tuexen47987ce2020-06-22 16:41:44 +020082#endif
tuexendd729232011-11-01 23:04:43 +000083/* FIX: we don't handle multiple link local scopes */
84/* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
85#ifdef INET6
86int
87SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
88{
89#ifdef SCTP_EMBEDDED_V6_SCOPE
Michael Tuexen5be0c252020-06-13 00:53:56 +020090#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +000091 struct in6_addr tmp_a, tmp_b;
92
93 tmp_a = a->sin6_addr;
tuexen9a218b12012-08-04 21:17:58 +000094#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +000095 if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) {
tuexen9a218b12012-08-04 21:17:58 +000096#else
97 if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +000098#endif
tuexen63fc0bb2011-12-27 12:24:52 +000099 return (0);
tuexendd729232011-11-01 23:04:43 +0000100 }
101 tmp_b = b->sin6_addr;
tuexen9a218b12012-08-04 21:17:58 +0000102#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +0000103 if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) {
tuexen9a218b12012-08-04 21:17:58 +0000104#else
105 if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +0000106#endif
tuexen63fc0bb2011-12-27 12:24:52 +0000107 return (0);
tuexendd729232011-11-01 23:04:43 +0000108 }
109 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
110#elif defined(SCTP_KAME)
111 struct sockaddr_in6 tmp_a, tmp_b;
112
113 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
114 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000115 return (0);
tuexendd729232011-11-01 23:04:43 +0000116 }
117 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
118 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000119 return (0);
tuexendd729232011-11-01 23:04:43 +0000120 }
121 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
122#else
123 struct in6_addr tmp_a, tmp_b;
124
125 tmp_a = a->sin6_addr;
126 if (in6_embedscope(&tmp_a, a) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000127 return (0);
tuexendd729232011-11-01 23:04:43 +0000128 }
129 tmp_b = b->sin6_addr;
130 if (in6_embedscope(&tmp_b, b) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000131 return (0);
tuexendd729232011-11-01 23:04:43 +0000132 }
133 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
134#endif
135#else
tuexen5154a002011-12-23 18:47:55 +0000136 return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr)));
tuexendd729232011-11-01 23:04:43 +0000137#endif /* SCTP_EMBEDDED_V6_SCOPE */
138}
139#endif
140
141void
142sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
143{
144 /*
145 * We really don't need to lock this, but I will just because it
146 * does not hurt.
147 */
tuexendd729232011-11-01 23:04:43 +0000148 SCTP_INP_INFO_RLOCK();
149 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
150 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
151 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
152 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
153 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
154 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
155 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
156 spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
tuexendd729232011-11-01 23:04:43 +0000157 SCTP_INP_INFO_RUNLOCK();
158}
159
t00fcxend0ad16b2013-02-09 18:34:24 +0000160/*-
tuexendd729232011-11-01 23:04:43 +0000161 * Addresses are added to VRF's (Virtual Router's). For BSD we
162 * have only the default VRF 0. We maintain a hash list of
163 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
164 * these has a list of addresses. When we add a new address
165 * to a VRF we lookup the ifn/ifn_index, if the ifn does
166 * not exist we create it and add it to the list of IFN's
167 * within the VRF. Once we have the sctp_ifn, we add the
168 * address to the list. So we look something like:
169 *
170 * hash-vrf-table
171 * vrf-> ifn-> ifn -> ifn
172 * vrf |
173 * ... +--ifa-> ifa -> ifa
174 * vrf
175 *
176 * We keep these separate lists since the SCTP subsystem will
177 * point to these from its source address selection nets structure.
178 * When an address is deleted it does not happen right away on
179 * the SCTP side, it gets scheduled. What we do when a
180 * delete happens is immediately remove the address from
181 * the master list and decrement the refcount. As our
182 * addip iterator works through and frees the src address
183 * selection pointing to the sctp_ifa, eventually the refcount
184 * will reach 0 and we will delete it. Note that it is assumed
185 * that any locking on system level ifn/ifa is done at the
186 * caller of these functions and these routines will only
187 * lock the SCTP structures as they add or delete things.
188 *
189 * Other notes on VRF concepts.
190 * - An endpoint can be in multiple VRF's
191 * - An association lives within a VRF and only one VRF.
192 * - Any incoming packet we can deduce the VRF for by
193 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
194 * - Any downward send call or connect call must supply the
195 * VRF via ancillary data or via some sort of set default
196 * VRF socket option call (again for BSD no brainer since
197 * the VRF is always 0).
198 * - An endpoint may add multiple VRF's to it.
199 * - Listening sockets can accept associations in any
200 * of the VRF's they are in but the assoc will end up
201 * in only one VRF (gotten from the packet or connect/send).
202 *
203 */
204
205struct sctp_vrf *
206sctp_allocate_vrf(int vrf_id)
207{
tuexen63fc0bb2011-12-27 12:24:52 +0000208 struct sctp_vrf *vrf = NULL;
tuexendd729232011-11-01 23:04:43 +0000209 struct sctp_vrflist *bucket;
210
211 /* First allocate the VRF structure */
212 vrf = sctp_find_vrf(vrf_id);
213 if (vrf) {
214 /* Already allocated */
215 return (vrf);
216 }
217 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
218 SCTP_M_VRF);
t00fcxen8dd95b82014-07-11 20:41:28 +0000219 if (vrf == NULL) {
220 /* No memory */
tuexendd729232011-11-01 23:04:43 +0000221#ifdef INVARIANTS
222 panic("No memory for VRF:%d", vrf_id);
223#endif
224 return (NULL);
225 }
226 /* setup the VRF */
227 memset(vrf, 0, sizeof(struct sctp_vrf));
228 vrf->vrf_id = vrf_id;
229 LIST_INIT(&vrf->ifnlist);
230 vrf->total_ifa_count = 0;
231 vrf->refcount = 0;
232 /* now also setup table ids */
233 SCTP_INIT_VRF_TABLEID(vrf);
234 /* Init the HASH of addresses */
235 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
236 &vrf->vrf_addr_hashmark);
237 if (vrf->vrf_addr_hash == NULL) {
t00fcxen8dd95b82014-07-11 20:41:28 +0000238 /* No memory */
tuexendd729232011-11-01 23:04:43 +0000239#ifdef INVARIANTS
240 panic("No memory for VRF:%d", vrf_id);
241#endif
242 SCTP_FREE(vrf, SCTP_M_VRF);
243 return (NULL);
244 }
245
246 /* Add it to the hash table */
247 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
248 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
249 atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
250 return (vrf);
251}
252
tuexendd729232011-11-01 23:04:43 +0000253struct sctp_ifn *
254sctp_find_ifn(void *ifn, uint32_t ifn_index)
255{
256 struct sctp_ifn *sctp_ifnp;
257 struct sctp_ifnlist *hash_ifn_head;
258
259 /* We assume the lock is held for the addresses
260 * if that's wrong problems could occur :-)
261 */
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200262 SCTP_IPI_ADDR_LOCK_ASSERT();
tuexendd729232011-11-01 23:04:43 +0000263 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
264 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
265 if (sctp_ifnp->ifn_index == ifn_index) {
tuexen63fc0bb2011-12-27 12:24:52 +0000266 return (sctp_ifnp);
tuexendd729232011-11-01 23:04:43 +0000267 }
268 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
tuexen63fc0bb2011-12-27 12:24:52 +0000269 return (sctp_ifnp);
tuexendd729232011-11-01 23:04:43 +0000270 }
271 }
tuexen63fc0bb2011-12-27 12:24:52 +0000272 return (NULL);
tuexendd729232011-11-01 23:04:43 +0000273}
274
tuexendd729232011-11-01 23:04:43 +0000275struct sctp_vrf *
276sctp_find_vrf(uint32_t vrf_id)
277{
278 struct sctp_vrflist *bucket;
279 struct sctp_vrf *liste;
t00fcxend0ad16b2013-02-09 18:34:24 +0000280
tuexendd729232011-11-01 23:04:43 +0000281 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
282 LIST_FOREACH(liste, bucket, next_vrf) {
283 if (vrf_id == liste->vrf_id) {
tuexen63fc0bb2011-12-27 12:24:52 +0000284 return (liste);
tuexendd729232011-11-01 23:04:43 +0000285 }
286 }
287 return (NULL);
288}
289
290void
291sctp_free_vrf(struct sctp_vrf *vrf)
292{
293 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
294 if (vrf->vrf_addr_hash) {
295 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
296 vrf->vrf_addr_hash = NULL;
297 }
298 /* We zero'd the count */
299 LIST_REMOVE(vrf, next_vrf);
300 SCTP_FREE(vrf, SCTP_M_VRF);
301 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
302 }
303}
304
305void
306sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
307{
308 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
309 /* We zero'd the count */
310 if (sctp_ifnp->vrf) {
311 sctp_free_vrf(sctp_ifnp->vrf);
312 }
313 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
314 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
315 }
316}
317
318void
319sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
320{
321 struct sctp_ifn *sctp_ifnp;
322
323 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
324 if (sctp_ifnp != NULL) {
325 sctp_ifnp->ifn_mtu = mtu;
326 }
327}
328
tuexendd729232011-11-01 23:04:43 +0000329void
330sctp_free_ifa(struct sctp_ifa *sctp_ifap)
331{
332 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
333 /* We zero'd the count */
334 if (sctp_ifap->ifn_p) {
335 sctp_free_ifn(sctp_ifap->ifn_p);
336 }
337 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
338 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
339 }
340}
341
342static void
343sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
344{
345 struct sctp_ifn *found;
346
347 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
348 if (found == NULL) {
349 /* Not in the list.. sorry */
350 return;
351 }
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200352 if (hold_addr_lock == 0) {
tuexendd729232011-11-01 23:04:43 +0000353 SCTP_IPI_ADDR_WLOCK();
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200354 } else {
355 SCTP_IPI_ADDR_WLOCK_ASSERT();
356 }
tuexendd729232011-11-01 23:04:43 +0000357 LIST_REMOVE(sctp_ifnp, next_bucket);
358 LIST_REMOVE(sctp_ifnp, next_ifn);
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200359 if (hold_addr_lock == 0) {
tuexendd729232011-11-01 23:04:43 +0000360 SCTP_IPI_ADDR_WUNLOCK();
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200361 }
tuexendd729232011-11-01 23:04:43 +0000362 /* Take away the reference, and possibly free it */
363 sctp_free_ifn(sctp_ifnp);
364}
365
366void
367sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
368 const char *if_name, uint32_t ifn_index)
369{
370 struct sctp_vrf *vrf;
t00fcxend0ad16b2013-02-09 18:34:24 +0000371 struct sctp_ifa *sctp_ifap;
372
tuexendd729232011-11-01 23:04:43 +0000373 SCTP_IPI_ADDR_RLOCK();
374 vrf = sctp_find_vrf(vrf_id);
375 if (vrf == NULL) {
376 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
377 goto out;
tuexendd729232011-11-01 23:04:43 +0000378 }
379 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
380 if (sctp_ifap == NULL) {
381 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
382 goto out;
tuexen0e813942011-11-18 09:18:32 +0000383 }
tuexendd729232011-11-01 23:04:43 +0000384 if (sctp_ifap->ifn_p == NULL) {
Michael Tuexen34488e72016-05-03 22:11:59 +0200385 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
tuexendd729232011-11-01 23:04:43 +0000386 goto out;
387 }
388 if (if_name) {
tuexen0e813942011-11-18 09:18:32 +0000389 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
tuexendd729232011-11-01 23:04:43 +0000390 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
tuexen0e813942011-11-18 09:18:32 +0000391 sctp_ifap->ifn_p->ifn_name, if_name);
tuexendd729232011-11-01 23:04:43 +0000392 goto out;
393 }
394 } else {
395 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
396 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
397 sctp_ifap->ifn_p->ifn_index, ifn_index);
398 goto out;
399 }
400 }
tuexen0e813942011-11-18 09:18:32 +0000401
tuexendd729232011-11-01 23:04:43 +0000402 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
403 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
404 out:
405 SCTP_IPI_ADDR_RUNLOCK();
406}
407
408void
409sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
410 const char *if_name, uint32_t ifn_index)
411{
412 struct sctp_vrf *vrf;
t00fcxend0ad16b2013-02-09 18:34:24 +0000413 struct sctp_ifa *sctp_ifap;
414
tuexendd729232011-11-01 23:04:43 +0000415 SCTP_IPI_ADDR_RLOCK();
416 vrf = sctp_find_vrf(vrf_id);
417 if (vrf == NULL) {
418 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
419 goto out;
tuexendd729232011-11-01 23:04:43 +0000420 }
421 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
422 if (sctp_ifap == NULL) {
423 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
424 goto out;
tuexen0e813942011-11-18 09:18:32 +0000425 }
tuexendd729232011-11-01 23:04:43 +0000426 if (sctp_ifap->ifn_p == NULL) {
Michael Tuexen34488e72016-05-03 22:11:59 +0200427 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
tuexendd729232011-11-01 23:04:43 +0000428 goto out;
429 }
430 if (if_name) {
tuexen0e813942011-11-18 09:18:32 +0000431 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
tuexendd729232011-11-01 23:04:43 +0000432 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
tuexen0e813942011-11-18 09:18:32 +0000433 sctp_ifap->ifn_p->ifn_name, if_name);
tuexendd729232011-11-01 23:04:43 +0000434 goto out;
435 }
436 } else {
437 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
438 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
439 sctp_ifap->ifn_p->ifn_index, ifn_index);
440 goto out;
441 }
442 }
443
444 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
445 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
446 out:
447 SCTP_IPI_ADDR_RUNLOCK();
448}
449
450/*-
451 * Add an ifa to an ifn.
452 * Register the interface as necessary.
453 * NOTE: ADDR write lock MUST be held.
454 */
455static void
456sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
457{
458 int ifa_af;
459
460 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
461 sctp_ifap->ifn_p = sctp_ifnp;
462 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
463 /* update address counts */
464 sctp_ifnp->ifa_count++;
465 ifa_af = sctp_ifap->address.sa.sa_family;
466 switch (ifa_af) {
467#ifdef INET
468 case AF_INET:
469 sctp_ifnp->num_v4++;
470 break;
471#endif
472#ifdef INET6
473 case AF_INET6:
474 sctp_ifnp->num_v6++;
475 break;
476#endif
477 default:
478 break;
479 }
480 if (sctp_ifnp->ifa_count == 1) {
481 /* register the new interface */
tuexendd729232011-11-01 23:04:43 +0000482 sctp_ifnp->registered_af = ifa_af;
483 }
484}
485
486/*-
487 * Remove an ifa from its ifn.
488 * If no more addresses exist, remove the ifn too. Otherwise, re-register
489 * the interface based on the remaining address families left.
490 * NOTE: ADDR write lock MUST be held.
491 */
492static void
493sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
494{
tuexendd729232011-11-01 23:04:43 +0000495 LIST_REMOVE(sctp_ifap, next_ifa);
496 if (sctp_ifap->ifn_p) {
497 /* update address counts */
498 sctp_ifap->ifn_p->ifa_count--;
499 switch (sctp_ifap->address.sa.sa_family) {
500#ifdef INET
501 case AF_INET:
502 sctp_ifap->ifn_p->num_v4--;
503 break;
504#endif
505#ifdef INET6
506 case AF_INET6:
507 sctp_ifap->ifn_p->num_v6--;
508 break;
509#endif
510 default:
511 break;
512 }
513
tuexendd729232011-11-01 23:04:43 +0000514 if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
515 /* remove the ifn, possibly freeing it */
516 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
517 } else {
518 /* re-register address family type, if needed */
519 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
520 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
tuexendd729232011-11-01 23:04:43 +0000521 sctp_ifap->ifn_p->registered_af = AF_INET;
522 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
523 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
tuexendd729232011-11-01 23:04:43 +0000524 sctp_ifap->ifn_p->registered_af = AF_INET6;
525 }
526 /* free the ifn refcount */
527 sctp_free_ifn(sctp_ifap->ifn_p);
528 }
529 sctp_ifap->ifn_p = NULL;
530 }
531}
532
533struct sctp_ifa *
534sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
535 uint32_t ifn_type, const char *if_name, void *ifa,
536 struct sockaddr *addr, uint32_t ifa_flags,
537 int dynamic_add)
538{
539 struct sctp_vrf *vrf;
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200540 struct sctp_ifn *sctp_ifnp, *new_sctp_ifnp;
541 struct sctp_ifa *sctp_ifap, *new_sctp_ifap;
tuexendd729232011-11-01 23:04:43 +0000542 struct sctp_ifalist *hash_addr_head;
543 struct sctp_ifnlist *hash_ifn_head;
544 uint32_t hash_of_addr;
545 int new_ifn_af = 0;
546
547#ifdef SCTP_DEBUG
548 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
549 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
550#endif
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200551 SCTP_MALLOC(new_sctp_ifnp, struct sctp_ifn *,
552 sizeof(struct sctp_ifn), SCTP_M_IFN);
553 if (new_sctp_ifnp == NULL) {
554#ifdef INVARIANTS
555 panic("No memory for IFN");
556#endif
557 return (NULL);
558 }
559 SCTP_MALLOC(new_sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
560 if (new_sctp_ifap == NULL) {
561#ifdef INVARIANTS
562 panic("No memory for IFA");
563#endif
564 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
565 return (NULL);
566 }
567
tuexendd729232011-11-01 23:04:43 +0000568 SCTP_IPI_ADDR_WLOCK();
569 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
570 if (sctp_ifnp) {
571 vrf = sctp_ifnp->vrf;
572 } else {
573 vrf = sctp_find_vrf(vrf_id);
574 if (vrf == NULL) {
575 vrf = sctp_allocate_vrf(vrf_id);
576 if (vrf == NULL) {
577 SCTP_IPI_ADDR_WUNLOCK();
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200578 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
579 SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
tuexendd729232011-11-01 23:04:43 +0000580 return (NULL);
581 }
582 }
583 }
584 if (sctp_ifnp == NULL) {
585 /* build one and add it, can't hold lock
586 * until after malloc done though.
587 */
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200588 sctp_ifnp = new_sctp_ifnp;
589 new_sctp_ifnp = NULL;
tuexendd729232011-11-01 23:04:43 +0000590 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
591 sctp_ifnp->ifn_index = ifn_index;
592 sctp_ifnp->ifn_p = ifn;
593 sctp_ifnp->ifn_type = ifn_type;
594 sctp_ifnp->refcount = 0;
595 sctp_ifnp->vrf = vrf;
596 atomic_add_int(&vrf->refcount, 1);
597 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
598 if (if_name != NULL) {
Michael Tuexenedd369d2020-05-19 09:42:15 +0200599 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
tuexendd729232011-11-01 23:04:43 +0000600 } else {
Michael Tuexenedd369d2020-05-19 09:42:15 +0200601 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
tuexendd729232011-11-01 23:04:43 +0000602 }
603 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
604 LIST_INIT(&sctp_ifnp->ifalist);
tuexendd729232011-11-01 23:04:43 +0000605 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
606 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
607 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
608 new_ifn_af = 1;
609 }
610 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
611 if (sctp_ifap) {
612 /* Hmm, it already exists? */
613 if ((sctp_ifap->ifn_p) &&
614 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
615 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
616 sctp_ifap->ifn_p->ifn_name, ifn_index,
t00fcxen8fca7ff2012-09-05 18:58:42 +0000617 (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000618 if (new_ifn_af) {
619 /* Remove the created one that we don't want */
620 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
621 }
622 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
623 /* easy to solve, just switch back to active */
624 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
625 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
626 sctp_ifap->ifn_p = sctp_ifnp;
627 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
628 }
629 exit_stage_left:
630 SCTP_IPI_ADDR_WUNLOCK();
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200631 if (new_sctp_ifnp != NULL) {
632 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
633 }
634 SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
tuexendd729232011-11-01 23:04:43 +0000635 return (sctp_ifap);
636 } else {
637 if (sctp_ifap->ifn_p) {
638 /*
639 * The last IFN gets the address, remove the
640 * old one
641 */
642 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +0000643 (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name,
tuexendd729232011-11-01 23:04:43 +0000644 sctp_ifap->ifn_p->ifn_index, if_name,
645 ifn_index);
646 /* remove the address from the old ifn */
647 sctp_remove_ifa_from_ifn(sctp_ifap);
648 /* move the address over to the new ifn */
649 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
t00fcxen8dd95b82014-07-11 20:41:28 +0000650 goto exit_stage_left;
tuexendd729232011-11-01 23:04:43 +0000651 } else {
652 /* repair ifnp which was NULL ? */
653 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
654 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +0000655 (void *)sctp_ifnp, (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000656 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
657 }
658 goto exit_stage_left;
659 }
660 }
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200661 sctp_ifap = new_sctp_ifap;
tuexendd729232011-11-01 23:04:43 +0000662 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
663 sctp_ifap->ifn_p = sctp_ifnp;
664 atomic_add_int(&sctp_ifnp->refcount, 1);
665 sctp_ifap->vrf_id = vrf_id;
666 sctp_ifap->ifa = ifa;
t00fcxena8a26f62012-09-12 19:24:54 +0000667#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +0000668 memcpy(&sctp_ifap->address, addr, addr->sa_len);
669#else
t00fcxend0ad16b2013-02-09 18:34:24 +0000670 switch (addr->sa_family) {
671#ifdef INET
672 case AF_INET:
tuexendd729232011-11-01 23:04:43 +0000673 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in));
t00fcxend0ad16b2013-02-09 18:34:24 +0000674 break;
675#endif
676#ifdef INET6
677 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +0000678 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6));
t00fcxend0ad16b2013-02-09 18:34:24 +0000679 break;
680#endif
681#if defined(__Userspace__)
682 case AF_CONN:
t00fcxen6df84312013-02-12 08:58:40 +0000683 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn));
t00fcxend0ad16b2013-02-09 18:34:24 +0000684 break;
685#endif
686 default:
687 /* TSNH */
688 break;
tuexendd729232011-11-01 23:04:43 +0000689 }
690#endif
691 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
692 sctp_ifap->flags = ifa_flags;
693 /* Set scope */
694 switch (sctp_ifap->address.sa.sa_family) {
695#ifdef INET
696 case AF_INET:
697 {
698 struct sockaddr_in *sin;
t00fcxend0ad16b2013-02-09 18:34:24 +0000699
t00fcxen05f07c02014-09-07 10:15:26 +0000700 sin = &sctp_ifap->address.sin;
tuexendd729232011-11-01 23:04:43 +0000701 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
702 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
703 sctp_ifap->src_is_loop = 1;
704 }
705 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
706 sctp_ifap->src_is_priv = 1;
707 }
708 sctp_ifnp->num_v4++;
709 if (new_ifn_af)
710 new_ifn_af = AF_INET;
711 break;
712 }
713#endif
714#ifdef INET6
715 case AF_INET6:
716 {
717 /* ok to use deprecated addresses? */
718 struct sockaddr_in6 *sin6;
t00fcxend0ad16b2013-02-09 18:34:24 +0000719
t00fcxen05f07c02014-09-07 10:15:26 +0000720 sin6 = &sctp_ifap->address.sin6;
tuexendd729232011-11-01 23:04:43 +0000721 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
722 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
723 sctp_ifap->src_is_loop = 1;
724 }
725 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
726 sctp_ifap->src_is_priv = 1;
727 }
728 sctp_ifnp->num_v6++;
729 if (new_ifn_af)
730 new_ifn_af = AF_INET6;
731 break;
732 }
733#endif
t00fcxend0ad16b2013-02-09 18:34:24 +0000734#if defined(__Userspace__)
735 case AF_CONN:
736 if (new_ifn_af)
737 new_ifn_af = AF_CONN;
738 break;
739#endif
tuexendd729232011-11-01 23:04:43 +0000740 default:
741 new_ifn_af = 0;
742 break;
743 }
744 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
745
746 if ((sctp_ifap->src_is_priv == 0) &&
747 (sctp_ifap->src_is_loop == 0)) {
748 sctp_ifap->src_is_glob = 1;
749 }
tuexendd729232011-11-01 23:04:43 +0000750 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
751 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
752 sctp_ifap->refcount = 1;
753 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
754 sctp_ifnp->ifa_count++;
755 vrf->total_ifa_count++;
756 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
757 if (new_ifn_af) {
tuexendd729232011-11-01 23:04:43 +0000758 sctp_ifnp->registered_af = new_ifn_af;
759 }
760 SCTP_IPI_ADDR_WUNLOCK();
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200761 if (new_sctp_ifnp != NULL) {
762 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
763 }
764
tuexendd729232011-11-01 23:04:43 +0000765 if (dynamic_add) {
766 /* Bump up the refcount so that when the timer
767 * completes it will drop back down.
768 */
769 struct sctp_laddr *wi;
770
771 atomic_add_int(&sctp_ifap->refcount, 1);
772 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
773 if (wi == NULL) {
774 /*
775 * Gak, what can we do? We have lost an address
776 * change can you say HOSED?
777 */
778 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
779 /* Opps, must decrement the count */
780 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
781 if_name);
782 return (NULL);
783 }
784 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +0200785 memset(wi, 0, sizeof(*wi));
tuexendd729232011-11-01 23:04:43 +0000786 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
787 wi->ifa = sctp_ifap;
788 wi->action = SCTP_ADD_IP_ADDRESS;
tuexen09631162012-04-18 11:23:02 +0000789
tuexendd729232011-11-01 23:04:43 +0000790 SCTP_WQ_ADDR_LOCK();
791 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
tuexendd729232011-11-01 23:04:43 +0000792 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
793 (struct sctp_inpcb *)NULL,
794 (struct sctp_tcb *)NULL,
795 (struct sctp_nets *)NULL);
Michael Tuexen96939f12017-09-20 23:31:24 +0200796 SCTP_WQ_ADDR_UNLOCK();
tuexendd729232011-11-01 23:04:43 +0000797 } else {
798 /* it's ready for use */
799 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
800 }
801 return (sctp_ifap);
802}
803
804void
805sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
806 uint32_t ifn_index, const char *if_name)
807{
808 struct sctp_vrf *vrf;
809 struct sctp_ifa *sctp_ifap = NULL;
810
811 SCTP_IPI_ADDR_WLOCK();
812 vrf = sctp_find_vrf(vrf_id);
813 if (vrf == NULL) {
814 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
815 goto out_now;
816 }
817
818#ifdef SCTP_DEBUG
819 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
820 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
821#endif
822 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
823 if (sctp_ifap) {
824 /* Validate the delete */
825 if (sctp_ifap->ifn_p) {
tuexenb740ef62011-11-13 11:58:06 +0000826 int valid = 0;
tuexendd729232011-11-01 23:04:43 +0000827 /*-
828 * The name has priority over the ifn_index
Michael Tuexen3b4263d2020-06-06 19:52:50 +0200829 * if its given.
tuexendd729232011-11-01 23:04:43 +0000830 */
831 if (if_name) {
tuexenb740ef62011-11-13 11:58:06 +0000832 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
833 /* They match its a correct delete */
834 valid = 1;
tuexendd729232011-11-01 23:04:43 +0000835 }
836 }
837 if (!valid) {
838 /* last ditch check ifn_index */
839 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
840 valid = 1;
841 }
842 }
843 if (!valid) {
844 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
845 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
846 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
847 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
848 SCTP_IPI_ADDR_WUNLOCK();
849 return;
850 }
851 }
t00fcxen8fca7ff2012-09-05 18:58:42 +0000852 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000853 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
Michael Tuexen2c5953e2020-07-17 17:25:19 +0200854 /*
t00fcxen97887d62014-03-29 21:31:12 +0000855 * We don't set the flag. This means that the structure will
856 * hang around in EP's that have bound specific to it until
857 * they close. This gives us TCP like behavior if someone
858 * removes an address (or for that matter adds it right back).
859 */
860 /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */
tuexendd729232011-11-01 23:04:43 +0000861 vrf->total_ifa_count--;
862 LIST_REMOVE(sctp_ifap, next_bucket);
863 sctp_remove_ifa_from_ifn(sctp_ifap);
864 }
865#ifdef SCTP_DEBUG
866 else {
867 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
868 ifn_index);
869 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
870 }
871#endif
872
873 out_now:
874 SCTP_IPI_ADDR_WUNLOCK();
875 if (sctp_ifap) {
876 struct sctp_laddr *wi;
877
878 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
879 if (wi == NULL) {
880 /*
881 * Gak, what can we do? We have lost an address
882 * change can you say HOSED?
883 */
884 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
885
886 /* Oops, must decrement the count */
887 sctp_free_ifa(sctp_ifap);
888 return;
889 }
890 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +0200891 memset(wi, 0, sizeof(*wi));
tuexendd729232011-11-01 23:04:43 +0000892 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
893 wi->ifa = sctp_ifap;
894 wi->action = SCTP_DEL_IP_ADDRESS;
895 SCTP_WQ_ADDR_LOCK();
896 /*
897 * Should this really be a tailq? As it is we will process the
898 * newest first :-0
899 */
900 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
tuexendd729232011-11-01 23:04:43 +0000901 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
902 (struct sctp_inpcb *)NULL,
903 (struct sctp_tcb *)NULL,
904 (struct sctp_nets *)NULL);
Michael Tuexen96939f12017-09-20 23:31:24 +0200905 SCTP_WQ_ADDR_UNLOCK();
tuexendd729232011-11-01 23:04:43 +0000906 }
907 return;
908}
909
t00fcxen60d0dea2013-02-10 20:04:03 +0000910static int
911sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
912{
t00fcxenaff14b62013-11-02 18:02:02 +0000913 int loopback_scope;
914#if defined(INET)
915 int ipv4_local_scope, ipv4_addr_legal;
916#endif
917#if defined(INET6)
918 int local_scope, site_scope, ipv6_addr_legal;
919#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000920#if defined(__Userspace__)
921 int conn_addr_legal;
922#endif
923 struct sctp_vrf *vrf;
924 struct sctp_ifn *sctp_ifn;
925 struct sctp_ifa *sctp_ifa;
926
927 loopback_scope = stcb->asoc.scope.loopback_scope;
t00fcxenaff14b62013-11-02 18:02:02 +0000928#if defined(INET)
t00fcxen60d0dea2013-02-10 20:04:03 +0000929 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
t00fcxenaff14b62013-11-02 18:02:02 +0000930 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
931#endif
932#if defined(INET6)
t00fcxen60d0dea2013-02-10 20:04:03 +0000933 local_scope = stcb->asoc.scope.local_scope;
934 site_scope = stcb->asoc.scope.site_scope;
t00fcxen60d0dea2013-02-10 20:04:03 +0000935 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
t00fcxenaff14b62013-11-02 18:02:02 +0000936#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000937#if defined(__Userspace__)
938 conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
939#endif
940
941 SCTP_IPI_ADDR_RLOCK();
942 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
943 if (vrf == NULL) {
944 /* no vrf, no addresses */
945 SCTP_IPI_ADDR_RUNLOCK();
946 return (0);
947 }
948
949 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
950 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
951 if ((loopback_scope == 0) &&
952 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
953 continue;
954 }
955 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
956 if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
957 (!sctp_is_addr_pending(stcb, sctp_ifa))) {
958 /* We allow pending addresses, where we
959 * have sent an asconf-add to be considered
960 * valid.
961 */
962 continue;
963 }
t00fcxenba8aa202013-11-16 15:37:17 +0000964 if (sctp_ifa->address.sa.sa_family != to->sa_family) {
965 continue;
966 }
t00fcxen60d0dea2013-02-10 20:04:03 +0000967 switch (sctp_ifa->address.sa.sa_family) {
968#ifdef INET
969 case AF_INET:
970 if (ipv4_addr_legal) {
971 struct sockaddr_in *sin, *rsin;
972
973 sin = &sctp_ifa->address.sin;
974 rsin = (struct sockaddr_in *)to;
975 if ((ipv4_local_scope == 0) &&
976 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
977 continue;
978 }
Michael Tuexen5be0c252020-06-13 00:53:56 +0200979#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +0000980 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
981 &sin->sin_addr) != 0) {
982 continue;
983 }
984#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000985 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
986 SCTP_IPI_ADDR_RUNLOCK();
987 return (1);
988 }
989 }
990 break;
991#endif
992#ifdef INET6
993 case AF_INET6:
994 if (ipv6_addr_legal) {
995 struct sockaddr_in6 *sin6, *rsin6;
996#if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
997 struct sockaddr_in6 lsa6;
998#endif
999 sin6 = &sctp_ifa->address.sin6;
1000 rsin6 = (struct sockaddr_in6 *)to;
Michael Tuexen5be0c252020-06-13 00:53:56 +02001001#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00001002 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
1003 &sin6->sin6_addr) != 0) {
1004 continue;
1005 }
1006#endif
t00fcxen60d0dea2013-02-10 20:04:03 +00001007 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1008 if (local_scope == 0)
1009 continue;
1010#if defined(SCTP_EMBEDDED_V6_SCOPE)
1011 if (sin6->sin6_scope_id == 0) {
1012#ifdef SCTP_KAME
1013 if (sa6_recoverscope(sin6) != 0)
1014 continue;
1015#else
1016 lsa6 = *sin6;
1017 if (in6_recoverscope(&lsa6,
1018 &lsa6.sin6_addr,
1019 NULL))
1020 continue;
1021 sin6 = &lsa6;
1022#endif /* SCTP_KAME */
1023 }
1024#endif /* SCTP_EMBEDDED_V6_SCOPE */
1025 }
1026 if ((site_scope == 0) &&
1027 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1028 continue;
1029 }
1030 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1031 SCTP_IPI_ADDR_RUNLOCK();
1032 return (1);
1033 }
1034 }
1035 break;
1036#endif
1037#if defined(__Userspace__)
1038 case AF_CONN:
1039 if (conn_addr_legal) {
1040 struct sockaddr_conn *sconn, *rsconn;
1041
1042 sconn = &sctp_ifa->address.sconn;
1043 rsconn = (struct sockaddr_conn *)to;
1044 if (sconn->sconn_addr == rsconn->sconn_addr) {
1045 SCTP_IPI_ADDR_RUNLOCK();
1046 return (1);
1047 }
1048 }
1049 break;
1050#endif
1051 default:
1052 /* TSNH */
1053 break;
1054 }
1055 }
1056 }
1057 } else {
1058 struct sctp_laddr *laddr;
1059
1060 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1061 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1062 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1063 continue;
1064 }
1065 if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
1066 (!sctp_is_addr_pending(stcb, laddr->ifa))) {
1067 /* We allow pending addresses, where we
1068 * have sent an asconf-add to be considered
1069 * valid.
1070 */
1071 continue;
1072 }
1073 if (laddr->ifa->address.sa.sa_family != to->sa_family) {
1074 continue;
1075 }
1076 switch (to->sa_family) {
1077#ifdef INET
1078 case AF_INET:
1079 {
1080 struct sockaddr_in *sin, *rsin;
1081
t00fcxen05f07c02014-09-07 10:15:26 +00001082 sin = &laddr->ifa->address.sin;
t00fcxen60d0dea2013-02-10 20:04:03 +00001083 rsin = (struct sockaddr_in *)to;
1084 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1085 SCTP_IPI_ADDR_RUNLOCK();
1086 return (1);
1087 }
1088 break;
1089 }
1090#endif
1091#ifdef INET6
1092 case AF_INET6:
1093 {
1094 struct sockaddr_in6 *sin6, *rsin6;
1095
t00fcxen05f07c02014-09-07 10:15:26 +00001096 sin6 = &laddr->ifa->address.sin6;
t00fcxen60d0dea2013-02-10 20:04:03 +00001097 rsin6 = (struct sockaddr_in6 *)to;
1098 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1099 SCTP_IPI_ADDR_RUNLOCK();
1100 return (1);
1101 }
1102 break;
1103 }
1104
1105#endif
1106#if defined(__Userspace__)
1107 case AF_CONN:
1108 {
1109 struct sockaddr_conn *sconn, *rsconn;
1110
t00fcxen05f07c02014-09-07 10:15:26 +00001111 sconn = &laddr->ifa->address.sconn;
t00fcxen60d0dea2013-02-10 20:04:03 +00001112 rsconn = (struct sockaddr_conn *)to;
1113 if (sconn->sconn_addr == rsconn->sconn_addr) {
1114 SCTP_IPI_ADDR_RUNLOCK();
1115 return (1);
1116 }
1117 break;
1118 }
1119#endif
1120 default:
1121 /* TSNH */
1122 break;
1123 }
t00fcxen60d0dea2013-02-10 20:04:03 +00001124 }
1125 }
1126 SCTP_IPI_ADDR_RUNLOCK();
1127 return (0);
1128}
1129
tuexendd729232011-11-01 23:04:43 +00001130static struct sctp_tcb *
1131sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
1132 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
1133{
1134 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
1135 /*
1136 * If we support the TCP model, then we must now dig through to see
1137 * if we can find our endpoint in the list of tcp ep's.
1138 */
1139 uint16_t lport, rport;
1140 struct sctppcbhead *ephead;
1141 struct sctp_inpcb *inp;
1142 struct sctp_laddr *laddr;
1143 struct sctp_tcb *stcb;
1144 struct sctp_nets *net;
1145#ifdef SCTP_MVRF
1146 int fnd, i;
1147#endif
t00fcxend0ad16b2013-02-09 18:34:24 +00001148
tuexendd729232011-11-01 23:04:43 +00001149 if ((to == NULL) || (from == NULL)) {
1150 return (NULL);
1151 }
1152
1153 switch (to->sa_family) {
1154#ifdef INET
1155 case AF_INET:
1156 if (from->sa_family == AF_INET) {
1157 lport = ((struct sockaddr_in *)to)->sin_port;
1158 rport = ((struct sockaddr_in *)from)->sin_port;
1159 } else {
1160 return (NULL);
1161 }
1162 break;
1163#endif
1164#ifdef INET6
1165 case AF_INET6:
1166 if (from->sa_family == AF_INET6) {
1167 lport = ((struct sockaddr_in6 *)to)->sin6_port;
1168 rport = ((struct sockaddr_in6 *)from)->sin6_port;
1169 } else {
1170 return (NULL);
1171 }
1172 break;
1173#endif
tuexen5d9b9742012-07-11 21:52:01 +00001174#if defined(__Userspace__)
1175 case AF_CONN:
1176 if (from->sa_family == AF_CONN) {
1177 lport = ((struct sockaddr_conn *)to)->sconn_port;
1178 rport = ((struct sockaddr_conn *)from)->sconn_port;
1179 } else {
1180 return (NULL);
1181 }
1182 break;
1183#endif
tuexendd729232011-11-01 23:04:43 +00001184 default:
1185 return (NULL);
1186 }
1187 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
1188 /*
1189 * Ok now for each of the guys in this bucket we must look and see:
1190 * - Does the remote port match. - Does there single association's
1191 * addresses match this address (to). If so we update p_ep to point
1192 * to this ep and return the tcb from it.
1193 */
1194 LIST_FOREACH(inp, ephead, sctp_hash) {
1195 SCTP_INP_RLOCK(inp);
1196 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1197 SCTP_INP_RUNLOCK(inp);
1198 continue;
1199 }
1200 if (lport != inp->sctp_lport) {
1201 SCTP_INP_RUNLOCK(inp);
1202 continue;
1203 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02001204#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00001205 switch (to->sa_family) {
1206#ifdef INET
1207 case AF_INET:
1208 {
1209 struct sockaddr_in *sin;
1210
1211 sin = (struct sockaddr_in *)to;
1212 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1213 &sin->sin_addr) != 0) {
1214 SCTP_INP_RUNLOCK(inp);
1215 continue;
1216 }
1217 break;
1218 }
1219#endif
1220#ifdef INET6
1221 case AF_INET6:
1222 {
1223 struct sockaddr_in6 *sin6;
1224
1225 sin6 = (struct sockaddr_in6 *)to;
1226 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1227 &sin6->sin6_addr) != 0) {
1228 SCTP_INP_RUNLOCK(inp);
1229 continue;
1230 }
1231 break;
1232 }
1233#endif
1234 default:
1235 SCTP_INP_RUNLOCK(inp);
1236 continue;
1237 }
1238#endif
tuexendd729232011-11-01 23:04:43 +00001239#ifdef SCTP_MVRF
1240 fnd = 0;
tuexen9784e9a2011-12-18 13:04:23 +00001241 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00001242 if (inp->m_vrf_ids[i] == vrf_id) {
1243 fnd = 1;
1244 break;
1245 }
1246 }
1247 if (fnd == 0) {
1248 SCTP_INP_RUNLOCK(inp);
1249 continue;
1250 }
1251#else
1252 if (inp->def_vrf_id != vrf_id) {
1253 SCTP_INP_RUNLOCK(inp);
1254 continue;
1255 }
1256#endif
1257 /* check to see if the ep has one of the addresses */
1258 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1259 /* We are NOT bound all, so look further */
1260 int match = 0;
1261
1262 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
tuexendd729232011-11-01 23:04:43 +00001263 if (laddr->ifa == NULL) {
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02001264 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__);
tuexendd729232011-11-01 23:04:43 +00001265 continue;
1266 }
1267 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1268 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1269 continue;
1270 }
1271 if (laddr->ifa->address.sa.sa_family ==
1272 to->sa_family) {
1273 /* see if it matches */
tuexendd729232011-11-01 23:04:43 +00001274#ifdef INET
1275 if (from->sa_family == AF_INET) {
1276 struct sockaddr_in *intf_addr, *sin;
1277
1278 intf_addr = &laddr->ifa->address.sin;
1279 sin = (struct sockaddr_in *)to;
1280 if (sin->sin_addr.s_addr ==
1281 intf_addr->sin_addr.s_addr) {
1282 match = 1;
1283 break;
1284 }
1285 }
1286#endif
1287#ifdef INET6
1288 if (from->sa_family == AF_INET6) {
1289 struct sockaddr_in6 *intf_addr6;
1290 struct sockaddr_in6 *sin6;
1291
1292 sin6 = (struct sockaddr_in6 *)
1293 to;
1294 intf_addr6 = &laddr->ifa->address.sin6;
1295
1296 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1297 intf_addr6)) {
1298 match = 1;
1299 break;
1300 }
1301 }
1302#endif
tuexen5d9b9742012-07-11 21:52:01 +00001303#if defined(__Userspace__)
1304 if (from->sa_family == AF_CONN) {
1305 struct sockaddr_conn *intf_addr, *sconn;
1306
1307 intf_addr = &laddr->ifa->address.sconn;
1308 sconn = (struct sockaddr_conn *)to;
1309 if (sconn->sconn_addr ==
1310 intf_addr->sconn_addr) {
1311 match = 1;
1312 break;
1313 }
1314 }
1315#endif
tuexendd729232011-11-01 23:04:43 +00001316 }
1317 }
1318 if (match == 0) {
1319 /* This endpoint does not have this address */
1320 SCTP_INP_RUNLOCK(inp);
1321 continue;
1322 }
1323 }
1324 /*
1325 * Ok if we hit here the ep has the address, does it hold
1326 * the tcb?
1327 */
t00fcxen60d0dea2013-02-10 20:04:03 +00001328 /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
tuexendd729232011-11-01 23:04:43 +00001329 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1330 if (stcb == NULL) {
1331 SCTP_INP_RUNLOCK(inp);
1332 continue;
1333 }
1334 SCTP_TCB_LOCK(stcb);
t00fcxen60d0dea2013-02-10 20:04:03 +00001335 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1336 SCTP_TCB_UNLOCK(stcb);
1337 SCTP_INP_RUNLOCK(inp);
1338 continue;
1339 }
tuexendd729232011-11-01 23:04:43 +00001340 if (stcb->rport != rport) {
1341 /* remote port does not match. */
tuexen09631162012-04-18 11:23:02 +00001342 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001343 SCTP_INP_RUNLOCK(inp);
1344 continue;
1345 }
1346 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001347 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001348 SCTP_INP_RUNLOCK(inp);
1349 continue;
1350 }
t00fcxen60d0dea2013-02-10 20:04:03 +00001351 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1352 SCTP_TCB_UNLOCK(stcb);
1353 SCTP_INP_RUNLOCK(inp);
1354 continue;
1355 }
tuexendd729232011-11-01 23:04:43 +00001356 /* Does this TCB have a matching address? */
1357 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
tuexendd729232011-11-01 23:04:43 +00001358 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1359 /* not the same family, can't be a match */
1360 continue;
1361 }
1362 switch (from->sa_family) {
1363#ifdef INET
1364 case AF_INET:
1365 {
1366 struct sockaddr_in *sin, *rsin;
1367
1368 sin = (struct sockaddr_in *)&net->ro._l_addr;
1369 rsin = (struct sockaddr_in *)from;
1370 if (sin->sin_addr.s_addr ==
1371 rsin->sin_addr.s_addr) {
1372 /* found it */
1373 if (netp != NULL) {
1374 *netp = net;
1375 }
1376 /* Update the endpoint pointer */
1377 *inp_p = inp;
1378 SCTP_INP_RUNLOCK(inp);
1379 return (stcb);
1380 }
1381 break;
1382 }
1383#endif
1384#ifdef INET6
1385 case AF_INET6:
1386 {
1387 struct sockaddr_in6 *sin6, *rsin6;
1388
1389 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1390 rsin6 = (struct sockaddr_in6 *)from;
1391 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1392 rsin6)) {
1393 /* found it */
1394 if (netp != NULL) {
1395 *netp = net;
1396 }
1397 /* Update the endpoint pointer */
1398 *inp_p = inp;
1399 SCTP_INP_RUNLOCK(inp);
1400 return (stcb);
1401 }
1402 break;
1403 }
1404#endif
tuexen5d9b9742012-07-11 21:52:01 +00001405#if defined(__Userspace__)
1406 case AF_CONN:
1407 {
1408 struct sockaddr_conn *sconn, *rsconn;
1409
1410 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1411 rsconn = (struct sockaddr_conn *)from;
1412 if (sconn->sconn_addr == rsconn->sconn_addr) {
1413 /* found it */
1414 if (netp != NULL) {
1415 *netp = net;
1416 }
1417 /* Update the endpoint pointer */
1418 *inp_p = inp;
1419 SCTP_INP_RUNLOCK(inp);
1420 return (stcb);
1421 }
1422 break;
1423 }
1424#endif
tuexendd729232011-11-01 23:04:43 +00001425 default:
1426 /* TSNH */
1427 break;
1428 }
1429 }
1430 SCTP_TCB_UNLOCK(stcb);
1431 SCTP_INP_RUNLOCK(inp);
1432 }
1433 return (NULL);
1434}
1435
tuexendd729232011-11-01 23:04:43 +00001436/*
1437 * rules for use
1438 *
1439 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1440 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1441 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1442 * NULL.
1443 */
1444
1445struct sctp_tcb *
1446sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1447 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1448{
1449 struct sctpasochead *head;
1450 struct sctp_inpcb *inp;
tuexen63fc0bb2011-12-27 12:24:52 +00001451 struct sctp_tcb *stcb = NULL;
tuexendd729232011-11-01 23:04:43 +00001452 struct sctp_nets *net;
1453 uint16_t rport;
1454
1455 inp = *inp_p;
tuexen5d9b9742012-07-11 21:52:01 +00001456 switch (remote->sa_family) {
1457#ifdef INET
1458 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00001459 rport = (((struct sockaddr_in *)remote)->sin_port);
tuexen5d9b9742012-07-11 21:52:01 +00001460 break;
1461#endif
1462#ifdef INET6
1463 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00001464 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
tuexen5d9b9742012-07-11 21:52:01 +00001465 break;
1466#endif
1467#if defined(__Userspace__)
1468 case AF_CONN:
Taylor Brandstetter74670512018-03-27 10:56:58 -07001469 rport = (((struct sockaddr_conn *)remote)->sconn_port);
tuexen5d9b9742012-07-11 21:52:01 +00001470 break;
1471#endif
1472 default:
tuexendd729232011-11-01 23:04:43 +00001473 return (NULL);
1474 }
1475 if (locked_tcb) {
1476 /*
1477 * UN-lock so we can do proper locking here this occurs when
1478 * called from load_addresses_from_init.
1479 */
1480 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1481 SCTP_TCB_UNLOCK(locked_tcb);
1482 }
1483 SCTP_INP_INFO_RLOCK();
t00fcxenf2d781c2012-11-26 16:47:12 +00001484 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1485 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
tuexendd729232011-11-01 23:04:43 +00001486 /*-
1487 * Now either this guy is our listener or it's the
1488 * connector. If it is the one that issued the connect, then
1489 * it's only chance is to be the first TCB in the list. If
1490 * it is the acceptor, then do the special_lookup to hash
1491 * and find the real inp.
1492 */
Michael Tuexend900e5f2017-06-23 10:52:43 +02001493 if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) {
tuexendd729232011-11-01 23:04:43 +00001494 /* to is peer addr, from is my addr */
1495#ifndef SCTP_MVRF
1496 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1497 netp, inp->def_vrf_id);
1498 if ((stcb != NULL) && (locked_tcb == NULL)) {
1499 /* we have a locked tcb, lower refcount */
1500 SCTP_INP_DECR_REF(inp);
1501 }
1502 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1503 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1504 SCTP_TCB_LOCK(locked_tcb);
1505 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1506 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1507 }
1508#else
1509 /*-
1510 * MVRF is tricky, we must look in every VRF
1511 * the endpoint has.
1512 */
t00fcxen60d0dea2013-02-10 20:04:03 +00001513 int i;
tuexendd729232011-11-01 23:04:43 +00001514
t00fcxen60d0dea2013-02-10 20:04:03 +00001515 for (i = 0; i < inp->num_vrfs; i++) {
1516 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1517 netp, inp->m_vrf_ids[i]);
1518 if ((stcb != NULL) && (locked_tcb == NULL)) {
1519 /* we have a locked tcb, lower refcount */
1520 SCTP_INP_DECR_REF(inp);
1521 break;
1522 }
1523 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1524 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1525 SCTP_TCB_LOCK(locked_tcb);
1526 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1527 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1528 break;
tuexendd729232011-11-01 23:04:43 +00001529 }
1530 }
1531#endif
1532 SCTP_INP_INFO_RUNLOCK();
1533 return (stcb);
1534 } else {
1535 SCTP_INP_WLOCK(inp);
1536 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1537 goto null_return;
1538 }
1539 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1540 if (stcb == NULL) {
1541 goto null_return;
1542 }
1543 SCTP_TCB_LOCK(stcb);
1544
1545 if (stcb->rport != rport) {
1546 /* remote port does not match. */
1547 SCTP_TCB_UNLOCK(stcb);
1548 goto null_return;
1549 }
1550 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001551 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001552 goto null_return;
1553 }
1554 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1555 SCTP_TCB_UNLOCK(stcb);
1556 goto null_return;
1557 }
1558 /* now look at the list of remote addresses */
1559 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1560#ifdef INVARIANTS
1561 if (net == (TAILQ_NEXT(net, sctp_next))) {
1562 panic("Corrupt net list");
1563 }
1564#endif
1565 if (net->ro._l_addr.sa.sa_family !=
1566 remote->sa_family) {
1567 /* not the same family */
1568 continue;
1569 }
1570 switch (remote->sa_family) {
1571#ifdef INET
1572 case AF_INET:
1573 {
1574 struct sockaddr_in *sin, *rsin;
1575
1576 sin = (struct sockaddr_in *)
1577 &net->ro._l_addr;
1578 rsin = (struct sockaddr_in *)remote;
1579 if (sin->sin_addr.s_addr ==
1580 rsin->sin_addr.s_addr) {
1581 /* found it */
1582 if (netp != NULL) {
1583 *netp = net;
1584 }
1585 if (locked_tcb == NULL) {
1586 SCTP_INP_DECR_REF(inp);
1587 } else if (locked_tcb != stcb) {
1588 SCTP_TCB_LOCK(locked_tcb);
1589 }
1590 if (locked_tcb) {
1591 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1592 }
1593
1594 SCTP_INP_WUNLOCK(inp);
1595 SCTP_INP_INFO_RUNLOCK();
1596 return (stcb);
1597 }
1598 break;
1599 }
1600#endif
1601#ifdef INET6
1602 case AF_INET6:
1603 {
1604 struct sockaddr_in6 *sin6, *rsin6;
1605
1606 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1607 rsin6 = (struct sockaddr_in6 *)remote;
1608 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1609 rsin6)) {
1610 /* found it */
1611 if (netp != NULL) {
1612 *netp = net;
1613 }
1614 if (locked_tcb == NULL) {
1615 SCTP_INP_DECR_REF(inp);
1616 } else if (locked_tcb != stcb) {
1617 SCTP_TCB_LOCK(locked_tcb);
1618 }
1619 if (locked_tcb) {
1620 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1621 }
1622 SCTP_INP_WUNLOCK(inp);
1623 SCTP_INP_INFO_RUNLOCK();
1624 return (stcb);
1625 }
1626 break;
1627 }
1628#endif
tuexen5d9b9742012-07-11 21:52:01 +00001629#if defined(__Userspace__)
1630 case AF_CONN:
1631 {
1632 struct sockaddr_conn *sconn, *rsconn;
1633
1634 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1635 rsconn = (struct sockaddr_conn *)remote;
1636 if (sconn->sconn_addr == rsconn->sconn_addr) {
1637 /* found it */
1638 if (netp != NULL) {
1639 *netp = net;
1640 }
1641 if (locked_tcb == NULL) {
1642 SCTP_INP_DECR_REF(inp);
1643 } else if (locked_tcb != stcb) {
1644 SCTP_TCB_LOCK(locked_tcb);
1645 }
1646 if (locked_tcb) {
1647 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1648 }
1649 SCTP_INP_WUNLOCK(inp);
1650 SCTP_INP_INFO_RUNLOCK();
1651 return (stcb);
1652 }
1653 break;
1654 }
1655#endif
tuexendd729232011-11-01 23:04:43 +00001656 default:
1657 /* TSNH */
1658 break;
1659 }
1660 }
1661 SCTP_TCB_UNLOCK(stcb);
1662 }
1663 } else {
1664 SCTP_INP_WLOCK(inp);
1665 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1666 goto null_return;
1667 }
1668 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
t00fcxen1369cd42015-01-11 21:57:30 +00001669 inp->sctp_hashmark)];
tuexendd729232011-11-01 23:04:43 +00001670 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1671 if (stcb->rport != rport) {
1672 /* remote port does not match */
1673 continue;
1674 }
1675 SCTP_TCB_LOCK(stcb);
1676 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1677 SCTP_TCB_UNLOCK(stcb);
1678 continue;
1679 }
1680 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1681 SCTP_TCB_UNLOCK(stcb);
1682 continue;
1683 }
1684 /* now look at the list of remote addresses */
1685 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1686#ifdef INVARIANTS
1687 if (net == (TAILQ_NEXT(net, sctp_next))) {
1688 panic("Corrupt net list");
1689 }
1690#endif
1691 if (net->ro._l_addr.sa.sa_family !=
1692 remote->sa_family) {
1693 /* not the same family */
1694 continue;
1695 }
1696 switch (remote->sa_family) {
1697#ifdef INET
1698 case AF_INET:
1699 {
1700 struct sockaddr_in *sin, *rsin;
1701
1702 sin = (struct sockaddr_in *)
1703 &net->ro._l_addr;
1704 rsin = (struct sockaddr_in *)remote;
1705 if (sin->sin_addr.s_addr ==
1706 rsin->sin_addr.s_addr) {
1707 /* found it */
1708 if (netp != NULL) {
1709 *netp = net;
1710 }
1711 if (locked_tcb == NULL) {
1712 SCTP_INP_DECR_REF(inp);
1713 } else if (locked_tcb != stcb) {
1714 SCTP_TCB_LOCK(locked_tcb);
1715 }
1716 if (locked_tcb) {
1717 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1718 }
1719 SCTP_INP_WUNLOCK(inp);
1720 SCTP_INP_INFO_RUNLOCK();
1721 return (stcb);
1722 }
1723 break;
1724 }
1725#endif
1726#ifdef INET6
1727 case AF_INET6:
1728 {
1729 struct sockaddr_in6 *sin6, *rsin6;
1730
1731 sin6 = (struct sockaddr_in6 *)
1732 &net->ro._l_addr;
1733 rsin6 = (struct sockaddr_in6 *)remote;
1734 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1735 rsin6)) {
1736 /* found it */
1737 if (netp != NULL) {
1738 *netp = net;
1739 }
1740 if (locked_tcb == NULL) {
1741 SCTP_INP_DECR_REF(inp);
1742 } else if (locked_tcb != stcb) {
1743 SCTP_TCB_LOCK(locked_tcb);
1744 }
1745 if (locked_tcb) {
1746 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1747 }
1748 SCTP_INP_WUNLOCK(inp);
1749 SCTP_INP_INFO_RUNLOCK();
1750 return (stcb);
1751 }
1752 break;
1753 }
1754#endif
tuexen5d9b9742012-07-11 21:52:01 +00001755#if defined(__Userspace__)
1756 case AF_CONN:
1757 {
1758 struct sockaddr_conn *sconn, *rsconn;
1759
1760 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1761 rsconn = (struct sockaddr_conn *)remote;
1762 if (sconn->sconn_addr == rsconn->sconn_addr) {
1763 /* found it */
1764 if (netp != NULL) {
1765 *netp = net;
1766 }
1767 if (locked_tcb == NULL) {
1768 SCTP_INP_DECR_REF(inp);
1769 } else if (locked_tcb != stcb) {
1770 SCTP_TCB_LOCK(locked_tcb);
1771 }
1772 if (locked_tcb) {
1773 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1774 }
1775 SCTP_INP_WUNLOCK(inp);
1776 SCTP_INP_INFO_RUNLOCK();
1777 return (stcb);
1778 }
1779 break;
1780 }
1781#endif
tuexendd729232011-11-01 23:04:43 +00001782 default:
1783 /* TSNH */
1784 break;
1785 }
1786 }
1787 SCTP_TCB_UNLOCK(stcb);
1788 }
1789 }
1790null_return:
1791 /* clean up for returning null */
1792 if (locked_tcb) {
1793 SCTP_TCB_LOCK(locked_tcb);
1794 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1795 }
1796 SCTP_INP_WUNLOCK(inp);
1797 SCTP_INP_INFO_RUNLOCK();
1798 /* not found */
1799 return (NULL);
1800}
1801
1802/*
1803 * Find an association for a specific endpoint using the association id given
1804 * out in the COMM_UP notification
1805 */
tuexendd729232011-11-01 23:04:43 +00001806struct sctp_tcb *
1807sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1808{
1809 /*
1810 * Use my the assoc_id to find a endpoint
1811 */
1812 struct sctpasochead *head;
1813 struct sctp_tcb *stcb;
1814 uint32_t id;
t00fcxend0ad16b2013-02-09 18:34:24 +00001815
tuexendd729232011-11-01 23:04:43 +00001816 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1817 SCTP_PRINTF("TSNH ep_associd0\n");
1818 return (NULL);
1819 }
1820 id = (uint32_t)asoc_id;
1821 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1822 if (head == NULL) {
1823 /* invalid id TSNH */
1824 SCTP_PRINTF("TSNH ep_associd1\n");
1825 return (NULL);
1826 }
1827 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1828 if (stcb->asoc.assoc_id == id) {
1829 if (inp != stcb->sctp_ep) {
1830 /*
1831 * some other guy has the same id active (id
1832 * collision ??).
1833 */
1834 SCTP_PRINTF("TSNH ep_associd2\n");
1835 continue;
1836 }
1837 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1838 continue;
1839 }
1840 if (want_lock) {
1841 SCTP_TCB_LOCK(stcb);
1842 }
1843 return (stcb);
1844 }
1845 }
1846 return (NULL);
1847}
1848
tuexendd729232011-11-01 23:04:43 +00001849struct sctp_tcb *
1850sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1851{
t00fcxend0ad16b2013-02-09 18:34:24 +00001852 struct sctp_tcb *stcb;
1853
1854 SCTP_INP_RLOCK(inp);
1855 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1856 SCTP_INP_RUNLOCK(inp);
1857 return (stcb);
tuexendd729232011-11-01 23:04:43 +00001858}
1859
t00fcxend0ad16b2013-02-09 18:34:24 +00001860/*
1861 * Endpoint probe expects that the INP_INFO is locked.
1862 */
tuexendd729232011-11-01 23:04:43 +00001863static struct sctp_inpcb *
1864sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1865 uint16_t lport, uint32_t vrf_id)
1866{
1867 struct sctp_inpcb *inp;
1868 struct sctp_laddr *laddr;
1869#ifdef INET
1870 struct sockaddr_in *sin;
1871#endif
1872#ifdef INET6
1873 struct sockaddr_in6 *sin6;
1874 struct sockaddr_in6 *intf_addr6;
1875#endif
tuexen09a676e2012-07-08 21:03:18 +00001876#if defined(__Userspace__)
1877 struct sockaddr_conn *sconn;
1878#endif
tuexendd729232011-11-01 23:04:43 +00001879#ifdef SCTP_MVRF
1880 int i;
1881#endif
1882 int fnd;
t00fcxend0ad16b2013-02-09 18:34:24 +00001883
tuexendd729232011-11-01 23:04:43 +00001884#ifdef INET
1885 sin = NULL;
1886#endif
1887#ifdef INET6
1888 sin6 = NULL;
1889#endif
t00fcxenb4c54ac2012-09-15 09:17:50 +00001890#if defined(__Userspace__)
1891 sconn = NULL;
1892#endif
tuexendd729232011-11-01 23:04:43 +00001893 switch (nam->sa_family) {
1894#ifdef INET
1895 case AF_INET:
1896 sin = (struct sockaddr_in *)nam;
1897 break;
1898#endif
1899#ifdef INET6
1900 case AF_INET6:
1901 sin6 = (struct sockaddr_in6 *)nam;
1902 break;
1903#endif
tuexen09a676e2012-07-08 21:03:18 +00001904#if defined(__Userspace__)
1905 case AF_CONN:
1906 sconn = (struct sockaddr_conn *)nam;
1907 break;
1908#endif
tuexendd729232011-11-01 23:04:43 +00001909 default:
1910 /* unsupported family */
1911 return (NULL);
1912 }
1913
1914 if (head == NULL)
1915 return (NULL);
tuexen09631162012-04-18 11:23:02 +00001916
tuexendd729232011-11-01 23:04:43 +00001917 LIST_FOREACH(inp, head, sctp_hash) {
1918 SCTP_INP_RLOCK(inp);
1919 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1920 SCTP_INP_RUNLOCK(inp);
1921 continue;
1922 }
1923 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1924 (inp->sctp_lport == lport)) {
1925 /* got it */
t00fcxen326aa4b2014-06-20 13:42:13 +00001926 switch (nam->sa_family) {
tuexendd729232011-11-01 23:04:43 +00001927#ifdef INET
t00fcxen326aa4b2014-06-20 13:42:13 +00001928 case AF_INET:
1929 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02001930 SCTP_IPV6_V6ONLY(inp)) {
t00fcxen326aa4b2014-06-20 13:42:13 +00001931 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1932 SCTP_INP_RUNLOCK(inp);
1933 continue;
1934 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02001935#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00001936 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1937 &sin->sin_addr) != 0) {
1938 SCTP_INP_RUNLOCK(inp);
1939 continue;
1940 }
1941#endif
1942 break;
tuexendd729232011-11-01 23:04:43 +00001943#endif
1944#ifdef INET6
t00fcxen326aa4b2014-06-20 13:42:13 +00001945 case AF_INET6:
1946 /* A V6 address and the endpoint is NOT bound V6 */
1947 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1948 SCTP_INP_RUNLOCK(inp);
1949 continue;
1950 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02001951#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00001952 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1953 &sin6->sin6_addr) != 0) {
1954 SCTP_INP_RUNLOCK(inp);
1955 continue;
1956 }
tuexendd729232011-11-01 23:04:43 +00001957#endif
t00fcxen326aa4b2014-06-20 13:42:13 +00001958 break;
1959#endif
1960 default:
1961 break;
1962 }
tuexendd729232011-11-01 23:04:43 +00001963 /* does a VRF id match? */
1964 fnd = 0;
1965#ifdef SCTP_MVRF
1966 for (i = 0; i < inp->num_vrfs; i++) {
1967 if (inp->m_vrf_ids[i] == vrf_id) {
1968 fnd = 1;
1969 break;
1970 }
1971 }
1972#else
1973 if (inp->def_vrf_id == vrf_id)
1974 fnd = 1;
1975#endif
1976
1977 SCTP_INP_RUNLOCK(inp);
1978 if (!fnd)
1979 continue;
1980 return (inp);
1981 }
1982 SCTP_INP_RUNLOCK(inp);
1983 }
1984 switch (nam->sa_family) {
1985#ifdef INET
1986 case AF_INET:
1987 if (sin->sin_addr.s_addr == INADDR_ANY) {
1988 /* Can't hunt for one that has no address specified */
1989 return (NULL);
1990 }
1991 break;
1992#endif
1993#ifdef INET6
1994 case AF_INET6:
tuexen63fc0bb2011-12-27 12:24:52 +00001995 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
tuexendd729232011-11-01 23:04:43 +00001996 /* Can't hunt for one that has no address specified */
1997 return (NULL);
1998 }
1999 break;
2000#endif
tuexen09a676e2012-07-08 21:03:18 +00002001#if defined(__Userspace__)
2002 case AF_CONN:
2003 if (sconn->sconn_addr == NULL) {
2004 return (NULL);
2005 }
2006 break;
2007#endif
tuexendd729232011-11-01 23:04:43 +00002008 default:
2009 break;
2010 }
2011 /*
2012 * ok, not bound to all so see if we can find a EP bound to this
2013 * address.
2014 */
2015 LIST_FOREACH(inp, head, sctp_hash) {
2016 SCTP_INP_RLOCK(inp);
2017 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2018 SCTP_INP_RUNLOCK(inp);
2019 continue;
2020 }
2021 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
2022 SCTP_INP_RUNLOCK(inp);
2023 continue;
2024 }
2025 /*
2026 * Ok this could be a likely candidate, look at all of its
2027 * addresses
2028 */
2029 if (inp->sctp_lport != lport) {
2030 SCTP_INP_RUNLOCK(inp);
2031 continue;
2032 }
2033 /* does a VRF id match? */
2034 fnd = 0;
2035#ifdef SCTP_MVRF
2036 for (i = 0; i < inp->num_vrfs; i++) {
2037 if (inp->m_vrf_ids[i] == vrf_id) {
2038 fnd = 1;
2039 break;
2040 }
2041 }
2042#else
2043 if (inp->def_vrf_id == vrf_id)
2044 fnd = 1;
2045
2046#endif
2047 if (!fnd) {
2048 SCTP_INP_RUNLOCK(inp);
2049 continue;
2050 }
2051 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2052 if (laddr->ifa == NULL) {
2053 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002054 __func__);
tuexendd729232011-11-01 23:04:43 +00002055 continue;
2056 }
2057 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
t00fcxen8fca7ff2012-09-05 18:58:42 +00002058 (void *)laddr->ifa);
tuexendd729232011-11-01 23:04:43 +00002059 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2060 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
2061 continue;
2062 }
2063 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
2064 /* possible, see if it matches */
2065 switch (nam->sa_family) {
2066#ifdef INET
2067 case AF_INET:
Michael Tuexen5be0c252020-06-13 00:53:56 +02002068#if defined(__APPLE__) && !defined(__Userspace__)
tuexen63fc0bb2011-12-27 12:24:52 +00002069 if (sin == NULL) {
2070 /* TSNH */
2071 break;
2072 }
2073#endif
tuexendd729232011-11-01 23:04:43 +00002074 if (sin->sin_addr.s_addr ==
2075 laddr->ifa->address.sin.sin_addr.s_addr) {
2076 SCTP_INP_RUNLOCK(inp);
2077 return (inp);
2078 }
2079 break;
2080#endif
2081#ifdef INET6
2082 case AF_INET6:
2083 intf_addr6 = &laddr->ifa->address.sin6;
2084 if (SCTP6_ARE_ADDR_EQUAL(sin6,
2085 intf_addr6)) {
2086 SCTP_INP_RUNLOCK(inp);
2087 return (inp);
2088 }
2089 break;
2090#endif
tuexen09a676e2012-07-08 21:03:18 +00002091#if defined(__Userspace__)
2092 case AF_CONN:
2093 if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
2094 SCTP_INP_RUNLOCK(inp);
2095 return (inp);
2096 }
2097 break;
2098#endif
tuexendd729232011-11-01 23:04:43 +00002099 }
2100 }
2101 }
2102 SCTP_INP_RUNLOCK(inp);
2103 }
2104 return (NULL);
2105}
2106
tuexendd729232011-11-01 23:04:43 +00002107static struct sctp_inpcb *
2108sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
2109{
2110 struct sctppcbhead *head;
2111 struct sctp_inpcb *t_inp;
2112#ifdef SCTP_MVRF
2113 int i;
2114#endif
2115 int fnd;
2116
2117 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2118 SCTP_BASE_INFO(hashmark))];
2119 LIST_FOREACH(t_inp, head, sctp_hash) {
2120 if (t_inp->sctp_lport != lport) {
2121 continue;
2122 }
2123 /* is it in the VRF in question */
2124 fnd = 0;
2125#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00002126 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00002127 if (t_inp->m_vrf_ids[i] == vrf_id) {
2128 fnd = 1;
2129 break;
2130 }
2131 }
2132#else
2133 if (t_inp->def_vrf_id == vrf_id)
2134 fnd = 1;
2135#endif
2136 if (!fnd)
2137 continue;
2138
2139 /* This one is in use. */
2140 /* check the v6/v4 binding issue */
2141 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02002142 SCTP_IPV6_V6ONLY(t_inp)) {
tuexendd729232011-11-01 23:04:43 +00002143 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2144 /* collision in V6 space */
2145 return (t_inp);
2146 } else {
2147 /* inp is BOUND_V4 no conflict */
2148 continue;
2149 }
2150 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2151 /* t_inp is bound v4 and v6, conflict always */
2152 return (t_inp);
2153 } else {
2154 /* t_inp is bound only V4 */
2155 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02002156 SCTP_IPV6_V6ONLY(inp)) {
tuexendd729232011-11-01 23:04:43 +00002157 /* no conflict */
2158 continue;
2159 }
2160 /* else fall through to conflict */
2161 }
2162 return (t_inp);
2163 }
2164 return (NULL);
2165}
2166
tuexendd729232011-11-01 23:04:43 +00002167int
2168sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
2169{
2170 /* For 1-2-1 with port reuse */
2171 struct sctppcbhead *head;
t00fcxenc00f4bc2015-03-24 21:17:16 +00002172 struct sctp_inpcb *tinp, *ninp;
tuexendd729232011-11-01 23:04:43 +00002173
Michael Tuexenb43ed922021-09-08 22:46:39 +02002174 SCTP_INP_INFO_WLOCK_ASSERT();
2175 SCTP_INP_WLOCK_ASSERT(inp);
2176
tuexendd729232011-11-01 23:04:43 +00002177 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
2178 /* only works with port reuse on */
tuexen63fc0bb2011-12-27 12:24:52 +00002179 return (-1);
tuexendd729232011-11-01 23:04:43 +00002180 }
2181 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
2182 return (0);
2183 }
Michael Tuexenb43ed922021-09-08 22:46:39 +02002184 SCTP_INP_WUNLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00002185 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
2186 SCTP_BASE_INFO(hashmark))];
2187 /* Kick out all non-listeners to the TCP hash */
t00fcxenc00f4bc2015-03-24 21:17:16 +00002188 LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
tuexendd729232011-11-01 23:04:43 +00002189 if (tinp->sctp_lport != inp->sctp_lport) {
2190 continue;
2191 }
2192 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2193 continue;
2194 }
2195 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2196 continue;
2197 }
Michael Tuexend900e5f2017-06-23 10:52:43 +02002198 if (SCTP_IS_LISTENING(tinp)) {
tuexendd729232011-11-01 23:04:43 +00002199 continue;
2200 }
2201 SCTP_INP_WLOCK(tinp);
2202 LIST_REMOVE(tinp, sctp_hash);
2203 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
2204 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
2205 LIST_INSERT_HEAD(head, tinp, sctp_hash);
2206 SCTP_INP_WUNLOCK(tinp);
2207 }
2208 SCTP_INP_WLOCK(inp);
2209 /* Pull from where he was */
2210 LIST_REMOVE(inp, sctp_hash);
2211 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
2212 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
2213 LIST_INSERT_HEAD(head, inp, sctp_hash);
tuexendd729232011-11-01 23:04:43 +00002214 return (0);
2215}
2216
tuexendd729232011-11-01 23:04:43 +00002217struct sctp_inpcb *
2218sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
2219 uint32_t vrf_id)
2220{
2221 /*
2222 * First we check the hash table to see if someone has this port
2223 * bound with just the port.
2224 */
2225 struct sctp_inpcb *inp;
2226 struct sctppcbhead *head;
2227 int lport;
2228 unsigned int i;
2229#ifdef INET
2230 struct sockaddr_in *sin;
2231#endif
2232#ifdef INET6
2233 struct sockaddr_in6 *sin6;
2234#endif
tuexen09a676e2012-07-08 21:03:18 +00002235#if defined(__Userspace__)
2236 struct sockaddr_conn *sconn;
2237#endif
tuexendd729232011-11-01 23:04:43 +00002238
2239 switch (nam->sa_family) {
2240#ifdef INET
2241 case AF_INET:
2242 sin = (struct sockaddr_in *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002243 lport = sin->sin_port;
tuexendd729232011-11-01 23:04:43 +00002244 break;
2245#endif
2246#ifdef INET6
2247 case AF_INET6:
2248 sin6 = (struct sockaddr_in6 *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002249 lport = sin6->sin6_port;
tuexendd729232011-11-01 23:04:43 +00002250 break;
2251#endif
tuexen09a676e2012-07-08 21:03:18 +00002252#if defined(__Userspace__)
2253 case AF_CONN:
2254 sconn = (struct sockaddr_conn *)nam;
2255 lport = sconn->sconn_port;
2256 break;
2257#endif
tuexendd729232011-11-01 23:04:43 +00002258 default:
2259 return (NULL);
2260 }
2261 /*
2262 * I could cheat here and just cast to one of the types but we will
2263 * do it right. It also provides the check against an Unsupported
2264 * type too.
2265 */
2266 /* Find the head of the ALLADDR chain */
2267 if (have_lock == 0) {
2268 SCTP_INP_INFO_RLOCK();
2269 }
2270 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2271 SCTP_BASE_INFO(hashmark))];
2272 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2273
2274 /*
2275 * If the TCP model exists it could be that the main listening
2276 * endpoint is gone but there still exists a connected socket for this
2277 * guy. If so we can return the first one that we find. This may NOT
2278 * be the correct one so the caller should be wary on the returned INP.
2279 * Currently the only caller that sets find_tcp_pool is in bindx where
2280 * we are verifying that a user CAN bind the address. He either
2281 * has bound it already, or someone else has, or its open to bind,
2282 * so this is good enough.
2283 */
2284 if (inp == NULL && find_tcp_pool) {
2285 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
2286 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
2287 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2288 if (inp) {
2289 break;
2290 }
2291 }
2292 }
2293 if (inp) {
2294 SCTP_INP_INCR_REF(inp);
2295 }
2296 if (have_lock == 0) {
2297 SCTP_INP_INFO_RUNLOCK();
2298 }
2299 return (inp);
2300}
2301
2302/*
2303 * Find an association for an endpoint with the pointer to whom you want to
2304 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
2305 * need to change the *to to some other struct like a mbuf...
2306 */
2307struct sctp_tcb *
tuexen664967a2012-06-28 16:24:36 +00002308sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
tuexendd729232011-11-01 23:04:43 +00002309 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
2310 uint32_t vrf_id)
2311{
2312 struct sctp_inpcb *inp = NULL;
t00fcxen60d0dea2013-02-10 20:04:03 +00002313 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00002314
2315 SCTP_INP_INFO_RLOCK();
2316 if (find_tcp_pool) {
2317 if (inp_p != NULL) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002318 stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
2319 vrf_id);
tuexendd729232011-11-01 23:04:43 +00002320 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002321 stcb = sctp_tcb_special_locate(&inp, from, to, netp,
2322 vrf_id);
tuexendd729232011-11-01 23:04:43 +00002323 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002324 if (stcb != NULL) {
tuexendd729232011-11-01 23:04:43 +00002325 SCTP_INP_INFO_RUNLOCK();
t00fcxen60d0dea2013-02-10 20:04:03 +00002326 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002327 }
2328 }
2329 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
2330 if (inp_p != NULL) {
2331 *inp_p = inp;
2332 }
2333 SCTP_INP_INFO_RUNLOCK();
tuexendd729232011-11-01 23:04:43 +00002334 if (inp == NULL) {
2335 return (NULL);
2336 }
2337 /*
2338 * ok, we have an endpoint, now lets find the assoc for it (if any)
2339 * we now place the source address or from in the to of the find
2340 * endpoint call. Since in reality this chain is used from the
2341 * inbound packet side.
2342 */
2343 if (inp_p != NULL) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002344 stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
2345 NULL);
tuexendd729232011-11-01 23:04:43 +00002346 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002347 stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
2348 NULL);
tuexendd729232011-11-01 23:04:43 +00002349 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002350 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002351}
2352
tuexendd729232011-11-01 23:04:43 +00002353/*
2354 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
2355 * find all addresses that the sender has specified in any address list. Each
2356 * address will be used to lookup the TCB and see if one exits.
2357 */
2358static struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002359sctp_findassociation_special_addr(struct mbuf *m, int offset,
tuexendd729232011-11-01 23:04:43 +00002360 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
tuexen664967a2012-06-28 16:24:36 +00002361 struct sockaddr *dst)
tuexendd729232011-11-01 23:04:43 +00002362{
Michael Tuexen473225f2017-06-23 23:05:43 +02002363 struct sctp_paramhdr *phdr, param_buf;
t00fcxenaff14b62013-11-02 18:02:02 +00002364#if defined(INET) || defined(INET6)
t00fcxen6ebb6032013-11-02 18:03:31 +00002365 struct sctp_tcb *stcb;
t00fcxenaff14b62013-11-02 18:02:02 +00002366 uint16_t ptype;
2367#endif
2368 uint16_t plen;
tuexendd729232011-11-01 23:04:43 +00002369#ifdef INET
2370 struct sockaddr_in sin4;
2371#endif
2372#ifdef INET6
2373 struct sockaddr_in6 sin6;
2374#endif
2375
2376#ifdef INET
2377 memset(&sin4, 0, sizeof(sin4));
t00fcxen8fbaee32012-09-04 16:41:39 +00002378#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002379 sin4.sin_len = sizeof(sin4);
2380#endif
2381 sin4.sin_family = AF_INET;
2382 sin4.sin_port = sh->src_port;
2383#endif
2384#ifdef INET6
2385 memset(&sin6, 0, sizeof(sin6));
t00fcxen8fbaee32012-09-04 16:41:39 +00002386#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002387 sin6.sin6_len = sizeof(sin6);
2388#endif
2389 sin6.sin6_family = AF_INET6;
2390 sin6.sin6_port = sh->src_port;
2391#endif
2392
tuexendd729232011-11-01 23:04:43 +00002393 offset += sizeof(struct sctp_init_chunk);
2394
Michael Tuexen473225f2017-06-23 23:05:43 +02002395 phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00002396 while (phdr != NULL) {
2397 /* now we must see if we want the parameter */
t00fcxenaff14b62013-11-02 18:02:02 +00002398#if defined(INET) || defined(INET6)
tuexendd729232011-11-01 23:04:43 +00002399 ptype = ntohs(phdr->param_type);
t00fcxenaff14b62013-11-02 18:02:02 +00002400#endif
tuexendd729232011-11-01 23:04:43 +00002401 plen = ntohs(phdr->param_length);
2402 if (plen == 0) {
2403 break;
2404 }
2405#ifdef INET
2406 if (ptype == SCTP_IPV4_ADDRESS &&
2407 plen == sizeof(struct sctp_ipv4addr_param)) {
2408 /* Get the rest of the address */
Michael Tuexen473225f2017-06-23 23:05:43 +02002409 struct sctp_ipv4addr_param ip4_param, *p4;
tuexendd729232011-11-01 23:04:43 +00002410
2411 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02002412 (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
tuexendd729232011-11-01 23:04:43 +00002413 if (phdr == NULL) {
2414 return (NULL);
2415 }
2416 p4 = (struct sctp_ipv4addr_param *)phdr;
2417 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2418 /* look it up */
t00fcxen60d0dea2013-02-10 20:04:03 +00002419 stcb = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002420 (struct sockaddr *)&sin4, netp, dst, NULL);
t00fcxen60d0dea2013-02-10 20:04:03 +00002421 if (stcb != NULL) {
2422 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002423 }
2424 }
2425#endif
2426#ifdef INET6
2427 if (ptype == SCTP_IPV6_ADDRESS &&
2428 plen == sizeof(struct sctp_ipv6addr_param)) {
2429 /* Get the rest of the address */
Michael Tuexen473225f2017-06-23 23:05:43 +02002430 struct sctp_ipv6addr_param ip6_param, *p6;
tuexendd729232011-11-01 23:04:43 +00002431
2432 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02002433 (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
tuexendd729232011-11-01 23:04:43 +00002434 if (phdr == NULL) {
2435 return (NULL);
2436 }
2437 p6 = (struct sctp_ipv6addr_param *)phdr;
2438 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2439 /* look it up */
t00fcxen60d0dea2013-02-10 20:04:03 +00002440 stcb = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002441 (struct sockaddr *)&sin6, netp, dst, NULL);
t00fcxen60d0dea2013-02-10 20:04:03 +00002442 if (stcb != NULL) {
2443 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002444 }
2445 }
2446#endif
2447 offset += SCTP_SIZE32(plen);
Michael Tuexen473225f2017-06-23 23:05:43 +02002448 phdr = sctp_get_next_param(m, offset, &param_buf,
2449 sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00002450 }
2451 return (NULL);
2452}
2453
2454static struct sctp_tcb *
2455sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2456 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2457 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2458{
2459 /*
2460 * Use my vtag to hash. If we find it we then verify the source addr
2461 * is in the assoc. If all goes well we save a bit on rec of a
2462 * packet.
2463 */
2464 struct sctpasochead *head;
2465 struct sctp_nets *net;
2466 struct sctp_tcb *stcb;
tuexen9784e9a2011-12-18 13:04:23 +00002467#ifdef SCTP_MVRF
2468 unsigned int i;
2469#endif
tuexendd729232011-11-01 23:04:43 +00002470
tuexendd729232011-11-01 23:04:43 +00002471 SCTP_INP_INFO_RLOCK();
2472 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
t00fcxen0d664882015-01-11 21:47:36 +00002473 SCTP_BASE_INFO(hashasocmark))];
tuexendd729232011-11-01 23:04:43 +00002474 LIST_FOREACH(stcb, head, sctp_asocs) {
2475 SCTP_INP_RLOCK(stcb->sctp_ep);
2476 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2477 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2478 continue;
2479 }
tuexen9784e9a2011-12-18 13:04:23 +00002480#ifdef SCTP_MVRF
2481 for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) {
2482 if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) {
2483 break;
2484 }
2485 }
2486 if (i == stcb->sctp_ep->num_vrfs) {
2487 SCTP_INP_RUNLOCK(inp);
2488 continue;
2489 }
2490#else
2491 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2492 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2493 continue;
2494 }
2495#endif
tuexendd729232011-11-01 23:04:43 +00002496 SCTP_TCB_LOCK(stcb);
2497 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2498 if (stcb->asoc.my_vtag == vtag) {
2499 /* candidate */
2500 if (stcb->rport != rport) {
2501 SCTP_TCB_UNLOCK(stcb);
2502 continue;
2503 }
2504 if (stcb->sctp_ep->sctp_lport != lport) {
2505 SCTP_TCB_UNLOCK(stcb);
2506 continue;
2507 }
2508 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2509 SCTP_TCB_UNLOCK(stcb);
2510 continue;
2511 }
2512 /* RRS:Need toaddr check here */
2513 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2514 /* Endpoint does not own this address */
2515 SCTP_TCB_UNLOCK(stcb);
2516 continue;
2517 }
2518 if (remote_tag) {
2519 /* If we have both vtags that's all we match on */
2520 if (stcb->asoc.peer_vtag == remote_tag) {
2521 /* If both tags match we consider it conclusive
2522 * and check NO source/destination addresses
2523 */
2524 goto conclusive;
2525 }
2526 }
2527 if (skip_src_check) {
2528 conclusive:
2529 if (from) {
tuexen63fc0bb2011-12-27 12:24:52 +00002530 *netp = sctp_findnet(stcb, from);
tuexendd729232011-11-01 23:04:43 +00002531 } else {
2532 *netp = NULL; /* unknown */
2533 }
2534 if (inp_p)
2535 *inp_p = stcb->sctp_ep;
2536 SCTP_INP_INFO_RUNLOCK();
2537 return (stcb);
2538 }
2539 net = sctp_findnet(stcb, from);
2540 if (net) {
2541 /* yep its him. */
2542 *netp = net;
2543 SCTP_STAT_INCR(sctps_vtagexpress);
2544 *inp_p = stcb->sctp_ep;
2545 SCTP_INP_INFO_RUNLOCK();
2546 return (stcb);
2547 } else {
2548 /*
2549 * not him, this should only happen in rare
2550 * cases so I peg it.
2551 */
2552 SCTP_STAT_INCR(sctps_vtagbogus);
2553 }
2554 }
2555 SCTP_TCB_UNLOCK(stcb);
2556 }
2557 SCTP_INP_INFO_RUNLOCK();
2558 return (NULL);
2559}
2560
2561/*
2562 * Find an association with the pointer to the inbound IP packet. This can be
2563 * a IPv4 or IPv6 packet.
2564 */
2565struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002566sctp_findassociation_addr(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002567 struct sockaddr *src, struct sockaddr *dst,
tuexendd729232011-11-01 23:04:43 +00002568 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2569 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2570{
t00fcxen60d0dea2013-02-10 20:04:03 +00002571 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00002572 struct sctp_inpcb *inp;
2573
tuexendd729232011-11-01 23:04:43 +00002574 if (sh->v_tag) {
2575 /* we only go down this path if vtag is non-zero */
t00fcxen60d0dea2013-02-10 20:04:03 +00002576 stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
t00fcxenafd21192013-05-10 18:15:02 +00002577 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
t00fcxen60d0dea2013-02-10 20:04:03 +00002578 if (stcb) {
2579 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002580 }
2581 }
2582
tuexendd729232011-11-01 23:04:43 +00002583 if (inp_p) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002584 stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
Michael Tuexen7f202782016-01-28 17:09:30 +01002585 1, vrf_id);
tuexendd729232011-11-01 23:04:43 +00002586 inp = *inp_p;
2587 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002588 stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
Michael Tuexen7f202782016-01-28 17:09:30 +01002589 1, vrf_id);
tuexendd729232011-11-01 23:04:43 +00002590 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002591 SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2592 if (stcb == NULL && inp) {
tuexendd729232011-11-01 23:04:43 +00002593 /* Found a EP but not this address */
2594 if ((ch->chunk_type == SCTP_INITIATION) ||
2595 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2596 /*-
2597 * special hook, we do NOT return linp or an
2598 * association that is linked to an existing
2599 * association that is under the TCP pool (i.e. no
2600 * listener exists). The endpoint finding routine
2601 * will always find a listener before examining the
2602 * TCP pool.
2603 */
2604 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2605 if (inp_p) {
2606 *inp_p = NULL;
2607 }
2608 return (NULL);
2609 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002610 stcb = sctp_findassociation_special_addr(m,
tuexen664967a2012-06-28 16:24:36 +00002611 offset, sh, &inp, netp, dst);
tuexendd729232011-11-01 23:04:43 +00002612 if (inp_p != NULL) {
2613 *inp_p = inp;
2614 }
2615 }
2616 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002617 SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2618 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002619}
2620
2621/*
2622 * lookup an association by an ASCONF lookup address.
2623 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2624 */
2625struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002626sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002627 struct sockaddr *dst, struct sctphdr *sh,
2628 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
tuexendd729232011-11-01 23:04:43 +00002629{
2630 struct sctp_tcb *stcb;
t00fcxen05f07c02014-09-07 10:15:26 +00002631 union sctp_sockstore remote_store;
Michael Tuexen473225f2017-06-23 23:05:43 +02002632 struct sctp_paramhdr param_buf, *phdr;
tuexendd729232011-11-01 23:04:43 +00002633 int ptype;
2634 int zero_address = 0;
2635#ifdef INET
2636 struct sockaddr_in *sin;
2637#endif
2638#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00002639 struct sockaddr_in6 *sin6;
2640#endif
2641
tuexendd729232011-11-01 23:04:43 +00002642 memset(&remote_store, 0, sizeof(remote_store));
tuexendd729232011-11-01 23:04:43 +00002643 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002644 &param_buf, sizeof(struct sctp_paramhdr));
tuexendd729232011-11-01 23:04:43 +00002645 if (phdr == NULL) {
2646 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002647 __func__);
tuexendd729232011-11-01 23:04:43 +00002648 return NULL;
2649 }
2650 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2651 /* get the correlation address */
2652 switch (ptype) {
2653#ifdef INET6
2654 case SCTP_IPV6_ADDRESS:
2655 {
2656 /* ipv6 address param */
2657 struct sctp_ipv6addr_param *p6, p6_buf;
2658
2659 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2660 return NULL;
2661 }
2662 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2663 offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002664 &p6_buf.ph, sizeof(p6_buf));
tuexendd729232011-11-01 23:04:43 +00002665 if (p6 == NULL) {
2666 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002667 __func__);
tuexendd729232011-11-01 23:04:43 +00002668 return (NULL);
2669 }
t00fcxen05f07c02014-09-07 10:15:26 +00002670 sin6 = &remote_store.sin6;
tuexendd729232011-11-01 23:04:43 +00002671 sin6->sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00002672#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002673 sin6->sin6_len = sizeof(*sin6);
2674#endif
2675 sin6->sin6_port = sh->src_port;
2676 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2677 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2678 zero_address = 1;
2679 break;
2680 }
2681#endif
2682#ifdef INET
2683 case SCTP_IPV4_ADDRESS:
2684 {
2685 /* ipv4 address param */
2686 struct sctp_ipv4addr_param *p4, p4_buf;
2687
2688 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2689 return NULL;
2690 }
2691 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2692 offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002693 &p4_buf.ph, sizeof(p4_buf));
tuexendd729232011-11-01 23:04:43 +00002694 if (p4 == NULL) {
2695 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002696 __func__);
tuexendd729232011-11-01 23:04:43 +00002697 return (NULL);
2698 }
t00fcxen05f07c02014-09-07 10:15:26 +00002699 sin = &remote_store.sin;
tuexendd729232011-11-01 23:04:43 +00002700 sin->sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00002701#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002702 sin->sin_len = sizeof(*sin);
2703#endif
2704 sin->sin_port = sh->src_port;
2705 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2706 if (sin->sin_addr.s_addr == INADDR_ANY)
2707 zero_address = 1;
2708 break;
2709 }
2710#endif
2711 default:
2712 /* invalid address param type */
2713 return NULL;
2714 }
2715
2716 if (zero_address) {
tuexen664967a2012-06-28 16:24:36 +00002717 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
tuexendd729232011-11-01 23:04:43 +00002718 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
tuexenb53ec512012-08-05 10:55:12 +00002719 if (stcb != NULL) {
2720 SCTP_INP_DECR_REF(*inp_p);
2721 }
tuexendd729232011-11-01 23:04:43 +00002722 } else {
2723 stcb = sctp_findassociation_ep_addr(inp_p,
t00fcxen05f07c02014-09-07 10:15:26 +00002724 &remote_store.sa, netp,
tuexen664967a2012-06-28 16:24:36 +00002725 dst, NULL);
tuexendd729232011-11-01 23:04:43 +00002726 }
2727 return (stcb);
2728}
2729
tuexendd729232011-11-01 23:04:43 +00002730/*
2731 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2732 * addresses. This way if we don't get a bind we by default pick a ephemeral
2733 * port with all addresses bound.
2734 */
2735int
2736sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2737{
2738 /*
2739 * we get called when a new endpoint starts up. We need to allocate
2740 * the sctp_inpcb structure from the zone and init it. Mark it as
2741 * unbound and find a port that we can use as an ephemeral with
2742 * INADDR_ANY. If the user binds later no problem we can then add in
2743 * the specific addresses. And setup the default parameters for the
2744 * EP.
2745 */
2746 int i, error;
2747 struct sctp_inpcb *inp;
2748 struct sctp_pcb *m;
2749 struct timeval time;
2750 sctp_sharedkey_t *null_key;
2751
2752 error = 0;
2753
2754 SCTP_INP_INFO_WLOCK();
2755 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2756 if (inp == NULL) {
2757 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2758 SCTP_INP_INFO_WUNLOCK();
2759 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2760 return (ENOBUFS);
2761 }
2762 /* zap it */
Michael Tuexend0853302017-07-19 17:01:31 +02002763 memset(inp, 0, sizeof(*inp));
tuexendd729232011-11-01 23:04:43 +00002764
2765 /* bump generations */
Michael Tuexen5be0c252020-06-13 00:53:56 +02002766#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00002767 inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
2768#endif
2769 /* setup socket pointers */
2770 inp->sctp_socket = so;
2771 inp->ip_inp.inp.inp_socket = so;
Michael Tuexen5be0c252020-06-13 00:53:56 +02002772#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00002773 inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
2774#endif
tuexenc0bb6122012-01-15 14:21:21 +00002775#ifdef INET6
Michael Tuexeneb611572020-06-13 17:27:02 +02002776#if !defined(__Userspace__) && !defined(_WIN32)
t00fcxenafd21192013-05-10 18:15:02 +00002777 if (INP_SOCKAF(so) == AF_INET6) {
2778 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2779 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2780 }
2781 if (MODULE_GLOBAL(ip6_v6only)) {
2782 inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
2783 }
tuexendd729232011-11-01 23:04:43 +00002784 }
2785#endif
tuexenc0bb6122012-01-15 14:21:21 +00002786#endif
tuexendd729232011-11-01 23:04:43 +00002787 inp->sctp_associd_counter = 1;
2788 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
Michael Tuexendbb9f552021-12-28 00:48:48 +01002789 inp->sctp_frag_point = 0;
t00fcxen3eda0f22015-03-10 20:10:45 +00002790 inp->max_cwnd = 0;
tuexendd729232011-11-01 23:04:43 +00002791 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
t00fcxen478b7722014-08-02 18:24:12 +00002792 inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
t00fcxen0e78cef2014-08-02 22:05:33 +00002793 inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
t00fcxenb3969a12014-08-12 13:19:21 +00002794 inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
t00fcxenaac0f452014-08-12 12:51:45 +00002795 inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
t00fcxenfa7e1a02014-08-04 20:41:36 +00002796 inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
t00fcxen3656cf92014-08-03 15:25:02 +00002797 inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
t00fcxen1cc0ba82014-08-03 20:32:08 +00002798 inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
Michael Tuexene5001952016-04-17 19:25:27 +02002799 inp->idata_supported = 0;
2800
Michael Tuexen5be0c252020-06-13 00:53:56 +02002801#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxena51e4462015-06-17 15:53:23 +00002802 inp->fibnum = so->so_fibnum;
2803#else
2804 inp->fibnum = 0;
2805#endif
tuexen09631162012-04-18 11:23:02 +00002806#if defined(__Userspace__)
tuexeneda9dbb2012-05-27 11:10:08 +00002807 inp->ulp_info = NULL;
tuexen09631162012-04-18 11:23:02 +00002808 inp->recv_callback = NULL;
2809 inp->send_callback = NULL;
2810 inp->send_sb_threshold = 0;
tuexen09631162012-04-18 11:23:02 +00002811#endif
tuexendd729232011-11-01 23:04:43 +00002812 /* init the small hash table we use to track asocid <-> tcb */
2813 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2814 if (inp->sctp_asocidhash == NULL) {
Michael Tuexen5be0c252020-06-13 00:53:56 +02002815#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00002816 crfree(inp->ip_inp.inp.inp_cred);
2817#endif
tuexendd729232011-11-01 23:04:43 +00002818 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2819 SCTP_INP_INFO_WUNLOCK();
2820 return (ENOBUFS);
2821 }
tuexendd729232011-11-01 23:04:43 +00002822 SCTP_INCR_EP_COUNT();
tuexendd729232011-11-01 23:04:43 +00002823 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
tuexendd729232011-11-01 23:04:43 +00002824 SCTP_INP_INFO_WUNLOCK();
2825
2826 so->so_pcb = (caddr_t)inp;
2827
tuexendd729232011-11-01 23:04:43 +00002828 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
tuexendd729232011-11-01 23:04:43 +00002829 /* UDP style socket */
2830 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2831 SCTP_PCB_FLAGS_UNBOUND);
2832 /* Be sure it is NON-BLOCKING IO for UDP */
2833 /* SCTP_SET_SO_NBIO(so); */
2834 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2835 /* TCP style socket */
2836 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2837 SCTP_PCB_FLAGS_UNBOUND);
2838 /* Be sure we have blocking IO by default */
Michael Tuexenfe223572017-09-22 00:19:22 +02002839 SOCK_LOCK(so);
tuexendd729232011-11-01 23:04:43 +00002840 SCTP_CLEAR_SO_NBIO(so);
Michael Tuexenfe223572017-09-22 00:19:22 +02002841 SOCK_UNLOCK(so);
tuexendd729232011-11-01 23:04:43 +00002842 } else {
2843 /*
2844 * unsupported socket type (RAW, etc)- in case we missed it
2845 * in protosw
2846 */
2847 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2848 so->so_pcb = NULL;
Michael Tuexen5be0c252020-06-13 00:53:56 +02002849#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00002850 crfree(inp->ip_inp.inp.inp_cred);
2851#endif
tuexendd729232011-11-01 23:04:43 +00002852 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2853 return (EOPNOTSUPP);
2854 }
2855 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2856 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2857 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2858 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2859 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2860 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2861 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2862 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2863 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2864 }
2865 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2866 &inp->sctp_hashmark);
2867 if (inp->sctp_tcbhash == NULL) {
2868 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2869 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2870 so->so_pcb = NULL;
Michael Tuexen5be0c252020-06-13 00:53:56 +02002871#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00002872 crfree(inp->ip_inp.inp.inp_cred);
2873#endif
tuexendd729232011-11-01 23:04:43 +00002874 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2875 return (ENOBUFS);
2876 }
2877#ifdef SCTP_MVRF
2878 inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
2879 SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
2880 (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
2881 if (inp->m_vrf_ids == NULL) {
2882 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2883 so->so_pcb = NULL;
2884 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
Michael Tuexen5be0c252020-06-13 00:53:56 +02002885#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00002886 crfree(inp->ip_inp.inp.inp_cred);
2887#endif
tuexendd729232011-11-01 23:04:43 +00002888 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2889 return (ENOBUFS);
2890 }
2891 inp->m_vrf_ids[0] = vrf_id;
2892 inp->num_vrfs = 1;
2893#endif
2894 inp->def_vrf_id = vrf_id;
2895
Michael Tuexen5be0c252020-06-13 00:53:56 +02002896#if defined(__APPLE__) && !defined(__Userspace__)
tuexen9a218b12012-08-04 21:17:58 +00002897#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
t00fcxenbc766ab2014-01-03 19:29:31 +00002898 inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
tuexendd729232011-11-01 23:04:43 +00002899 if (inp->ip_inp.inp.inpcb_mtx == NULL) {
t00fcxen8fca7ff2012-09-05 18:58:42 +00002900 SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so);
tuexendd729232011-11-01 23:04:43 +00002901#ifdef SCTP_MVRF
2902 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
2903#endif
2904 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2905 so->so_pcb = NULL;
2906 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
t00fcxenbc766ab2014-01-03 19:29:31 +00002907 SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock);
tuexendd729232011-11-01 23:04:43 +00002908 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
2909 return (ENOMEM);
2910 }
t00fcxenbc766ab2014-01-03 19:29:31 +00002911#elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2912 lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
tuexen9a218b12012-08-04 21:17:58 +00002913#else
t00fcxenbc766ab2014-01-03 19:29:31 +00002914 lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).ipi_lock_grp, SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
tuexendd729232011-11-01 23:04:43 +00002915#endif
2916#endif
2917 SCTP_INP_INFO_WLOCK();
2918 SCTP_INP_LOCK_INIT(inp);
Michael Tuexen5be0c252020-06-13 00:53:56 +02002919#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00002920 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2921#endif
2922 SCTP_INP_READ_INIT(inp);
2923 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2924 /* lock the new ep */
2925 SCTP_INP_WLOCK(inp);
2926
2927 /* add it to the info area */
2928 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
Michael Tuexen5be0c252020-06-13 00:53:56 +02002929#if defined(__APPLE__) && !defined(__Userspace__)
t00fcxenbc766ab2014-01-03 19:29:31 +00002930 inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo);
2931#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2932 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list);
2933#else
2934 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list);
2935#endif
tuexendd729232011-11-01 23:04:43 +00002936#endif
2937 SCTP_INP_INFO_WUNLOCK();
2938
2939 TAILQ_INIT(&inp->read_queue);
2940 LIST_INIT(&inp->sctp_addr_list);
2941
2942 LIST_INIT(&inp->sctp_asoc_list);
2943
2944#ifdef SCTP_TRACK_FREED_ASOCS
2945 /* TEMP CODE */
2946 LIST_INIT(&inp->sctp_asoc_free_list);
2947#endif
2948 /* Init the timer structure for signature change */
2949 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2950 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2951
2952 /* now init the actual endpoint default data */
2953 m = &inp->sctp_ep;
2954
2955 /* setup the base timeout information */
Michael Tuexen25935e82020-03-28 22:55:28 +01002956 m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */
2957 m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */
2958 m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2959 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2960 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2961 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2962 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
tuexendd729232011-11-01 23:04:43 +00002963 /* all max/min max are in ms */
2964 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2965 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2966 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2967 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2968 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
tuexendd729232011-11-01 23:04:43 +00002969 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2970 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2971 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2972 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2973 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2974 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2975 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2976 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2977
2978 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2979 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
t00fcxenc45180a2013-07-05 10:17:17 +00002980 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
tuexendd729232011-11-01 23:04:43 +00002981 /* number of streams to pre-open on a association */
2982 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2983
Michael Tuexende0630d2017-11-03 22:04:01 +01002984 m->default_mtu = 0;
tuexendd729232011-11-01 23:04:43 +00002985 /* Add adaptation cookie */
t00fcxen36377202013-02-11 21:07:33 +00002986 m->adaptation_layer_indicator = 0;
2987 m->adaptation_layer_indicator_provided = 0;
tuexendd729232011-11-01 23:04:43 +00002988
2989 /* seed random number generator */
2990 m->random_counter = 1;
2991 m->store_at = SCTP_SIGNATURE_SIZE;
2992 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2993 sctp_fill_random_store(m);
2994
2995 /* Minimum cookie size */
2996 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2997 sizeof(struct sctp_state_cookie);
2998 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2999
3000 /* Setup the initial secret */
3001 (void)SCTP_GETTIME_TIMEVAL(&time);
Michael Tuexenf2c2b602021-09-07 15:50:13 +02003002 m->time_of_secret_change = (unsigned int)time.tv_sec;
tuexendd729232011-11-01 23:04:43 +00003003
3004 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
3005 m->secret_key[0][i] = sctp_select_initial_TSN(m);
3006 }
3007 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
3008
3009 /* How long is a cookie good for ? */
Michael Tuexen25935e82020-03-28 22:55:28 +01003010 m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
tuexendd729232011-11-01 23:04:43 +00003011 /*
3012 * Initialize authentication parameters
3013 */
3014 m->local_hmacs = sctp_default_supported_hmaclist();
3015 m->local_auth_chunks = sctp_alloc_chunklist();
t00fcxenaac0f452014-08-12 12:51:45 +00003016 if (inp->asconf_supported) {
3017 sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
3018 sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
3019 }
tuexendd729232011-11-01 23:04:43 +00003020 m->default_dscp = 0;
3021#ifdef INET6
3022 m->default_flowlabel = 0;
3023#endif
tuexen9d7b2072011-11-20 16:35:17 +00003024 m->port = 0; /* encapsulation disabled by default */
tuexendd729232011-11-01 23:04:43 +00003025 LIST_INIT(&m->shared_keys);
3026 /* add default NULL key as key id 0 */
3027 null_key = sctp_alloc_sharedkey();
3028 sctp_insert_sharedkey(&m->shared_keys, null_key);
3029 SCTP_INP_WUNLOCK(inp);
3030#ifdef SCTP_LOG_CLOSING
3031 sctp_log_closing(inp, NULL, 12);
3032#endif
3033 return (error);
3034}
3035
tuexendd729232011-11-01 23:04:43 +00003036void
3037sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
3038 struct sctp_tcb *stcb)
3039{
3040 struct sctp_nets *net;
3041 uint16_t lport, rport;
3042 struct sctppcbhead *head;
3043 struct sctp_laddr *laddr, *oladdr;
3044
3045 atomic_add_int(&stcb->asoc.refcnt, 1);
3046 SCTP_TCB_UNLOCK(stcb);
3047 SCTP_INP_INFO_WLOCK();
3048 SCTP_INP_WLOCK(old_inp);
3049 SCTP_INP_WLOCK(new_inp);
3050 SCTP_TCB_LOCK(stcb);
3051 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3052
Michael Tuexen2dfe1942021-12-04 11:39:46 +01003053#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexenc4e5de52021-12-04 19:22:59 +01003054#ifdef INET6
Michael Tuexen2dfe1942021-12-04 11:39:46 +01003055 if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3056 new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS;
3057 if (old_inp->ip_inp.inp.in6p_outputopts) {
3058 new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT);
3059 }
Michael Tuexenc4e5de52021-12-04 19:22:59 +01003060 }
3061#endif
3062#if defined(INET) && defined(INET6)
3063 else
3064#endif
3065#ifdef INET
3066 {
Michael Tuexen2dfe1942021-12-04 11:39:46 +01003067 new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos;
3068 new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl;
3069 }
3070#endif
Michael Tuexenc4e5de52021-12-04 19:22:59 +01003071#endif
tuexendd729232011-11-01 23:04:43 +00003072 new_inp->sctp_ep.time_of_secret_change =
3073 old_inp->sctp_ep.time_of_secret_change;
3074 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
3075 sizeof(old_inp->sctp_ep.secret_key));
3076 new_inp->sctp_ep.current_secret_number =
3077 old_inp->sctp_ep.current_secret_number;
3078 new_inp->sctp_ep.last_secret_number =
3079 old_inp->sctp_ep.last_secret_number;
3080 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
3081
3082 /* make it so new data pours into the new socket */
3083 stcb->sctp_socket = new_inp->sctp_socket;
3084 stcb->sctp_ep = new_inp;
3085
3086 /* Copy the port across */
3087 lport = new_inp->sctp_lport = old_inp->sctp_lport;
3088 rport = stcb->rport;
3089 /* Pull the tcb from the old association */
3090 LIST_REMOVE(stcb, sctp_tcbhash);
3091 LIST_REMOVE(stcb, sctp_tcblist);
3092 if (stcb->asoc.in_asocid_hash) {
3093 LIST_REMOVE(stcb, sctp_tcbasocidhash);
3094 }
3095 /* Now insert the new_inp into the TCP connected hash */
3096 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
3097
3098 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
3099 /* Its safe to access */
3100 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3101
3102 /* Now move the tcb into the endpoint list */
3103 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
3104 /*
3105 * Question, do we even need to worry about the ep-hash since we
3106 * only have one connection? Probably not :> so lets get rid of it
3107 * and not suck up any kernel memory in that.
3108 */
3109 if (stcb->asoc.in_asocid_hash) {
3110 struct sctpasochead *lhd;
3111 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
3112 new_inp->hashasocidmark)];
3113 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
3114 }
3115 /* Ok. Let's restart timer. */
3116 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3117 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
3118 stcb, net);
3119 }
3120
3121 SCTP_INP_INFO_WUNLOCK();
3122 if (new_inp->sctp_tcbhash != NULL) {
3123 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
3124 new_inp->sctp_tcbhash = NULL;
3125 }
3126 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3127 /* Subset bound, so copy in the laddr list from the old_inp */
3128 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3129 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3130 if (laddr == NULL) {
3131 /*
3132 * Gak, what can we do? This assoc is really
3133 * HOSED. We probably should send an abort
3134 * here.
3135 */
3136 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3137 continue;
3138 }
3139 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003140 memset(laddr, 0, sizeof(*laddr));
tuexendd729232011-11-01 23:04:43 +00003141 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3142 laddr->ifa = oladdr->ifa;
3143 atomic_add_int(&laddr->ifa->refcount, 1);
3144 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3145 sctp_nxt_addr);
3146 new_inp->laddr_count++;
3147 if (oladdr == stcb->asoc.last_used_address) {
3148 stcb->asoc.last_used_address = laddr;
3149 }
3150 }
3151 }
Michael Tuexen939d48f2020-07-19 14:44:43 +02003152 /* Now any running timers need to be adjusted. */
3153 if (stcb->asoc.dack_timer.ep == old_inp) {
3154 SCTP_INP_DECR_REF(old_inp);
3155 stcb->asoc.dack_timer.ep = new_inp;
3156 SCTP_INP_INCR_REF(new_inp);
3157 }
3158 if (stcb->asoc.asconf_timer.ep == old_inp) {
3159 SCTP_INP_DECR_REF(old_inp);
3160 stcb->asoc.asconf_timer.ep = new_inp;
3161 SCTP_INP_INCR_REF(new_inp);
3162 }
3163 if (stcb->asoc.strreset_timer.ep == old_inp) {
3164 SCTP_INP_DECR_REF(old_inp);
3165 stcb->asoc.strreset_timer.ep = new_inp;
3166 SCTP_INP_INCR_REF(new_inp);
3167 }
3168 if (stcb->asoc.shut_guard_timer.ep == old_inp) {
3169 SCTP_INP_DECR_REF(old_inp);
3170 stcb->asoc.shut_guard_timer.ep = new_inp;
3171 SCTP_INP_INCR_REF(new_inp);
3172 }
3173 if (stcb->asoc.autoclose_timer.ep == old_inp) {
3174 SCTP_INP_DECR_REF(old_inp);
3175 stcb->asoc.autoclose_timer.ep = new_inp;
3176 SCTP_INP_INCR_REF(new_inp);
3177 }
3178 if (stcb->asoc.delete_prim_timer.ep == old_inp) {
3179 SCTP_INP_DECR_REF(old_inp);
3180 stcb->asoc.delete_prim_timer.ep = new_inp;
3181 SCTP_INP_INCR_REF(new_inp);
3182 }
tuexendd729232011-11-01 23:04:43 +00003183 /* now what about the nets? */
3184 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
Michael Tuexen939d48f2020-07-19 14:44:43 +02003185 if (net->pmtu_timer.ep == old_inp) {
3186 SCTP_INP_DECR_REF(old_inp);
3187 net->pmtu_timer.ep = new_inp;
3188 SCTP_INP_INCR_REF(new_inp);
3189 }
3190 if (net->hb_timer.ep == old_inp) {
3191 SCTP_INP_DECR_REF(old_inp);
3192 net->hb_timer.ep = new_inp;
3193 SCTP_INP_INCR_REF(new_inp);
3194 }
3195 if (net->rxt_timer.ep == old_inp) {
3196 SCTP_INP_DECR_REF(old_inp);
3197 net->rxt_timer.ep = new_inp;
3198 SCTP_INP_INCR_REF(new_inp);
3199 }
tuexendd729232011-11-01 23:04:43 +00003200 }
3201 SCTP_INP_WUNLOCK(new_inp);
3202 SCTP_INP_WUNLOCK(old_inp);
3203}
3204
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003205/*
3206 * insert an laddr entry with the given ifa for the desired list
3207 */
3208static int
3209sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
3210{
3211 struct sctp_laddr *laddr;
3212
3213 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3214 if (laddr == NULL) {
3215 /* out of memory? */
3216 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3217 return (EINVAL);
3218 }
3219 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003220 memset(laddr, 0, sizeof(*laddr));
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003221 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3222 laddr->ifa = ifa;
3223 laddr->action = act;
3224 atomic_add_int(&ifa->refcount, 1);
3225 /* insert it */
3226 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3227
3228 return (0);
3229}
3230
3231/*
3232 * Remove an laddr entry from the local address list (on an assoc)
3233 */
3234static void
3235sctp_remove_laddr(struct sctp_laddr *laddr)
3236{
3237
3238 /* remove from the list */
3239 LIST_REMOVE(laddr, sctp_nxt_addr);
3240 sctp_free_ifa(laddr->ifa);
3241 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
3242 SCTP_DECR_LADDR_COUNT();
3243}
tuexendd729232011-11-01 23:04:43 +00003244#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
Michael Tuexenf7f9cd82020-09-24 15:14:13 +02003245
tuexendd729232011-11-01 23:04:43 +00003246/*
3247 * Don't know why, but without this there is an unknown reference when
3248 * compiling NetBSD... hmm
3249 */
3250extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3251#endif
3252
Michael Tuexena8409b32021-09-02 01:50:45 +02003253/*
3254 * Bind the socket, with the PCB and global info locks held. Note, if a
3255 * socket address is specified, the PCB lock may be dropped and re-acquired.
3256 *
3257 * sctp_ifap is used to bypass normal local address validation checks.
3258 */
tuexendd729232011-11-01 23:04:43 +00003259int
Michael Tuexen5be0c252020-06-13 00:53:56 +02003260#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003261sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3262 struct sctp_ifa *sctp_ifap, struct thread *td)
Michael Tuexeneb611572020-06-13 17:27:02 +02003263#elif defined(_WIN32) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003264sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3265 struct sctp_ifa *sctp_ifap, PKTHREAD p)
tuexendd729232011-11-01 23:04:43 +00003266#else
Michael Tuexena8409b32021-09-02 01:50:45 +02003267sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3268 struct sctp_ifa *sctp_ifap, struct proc *p)
tuexendd729232011-11-01 23:04:43 +00003269#endif
3270{
3271 /* bind a ep to a socket address */
3272 struct sctppcbhead *head;
Michael Tuexena8409b32021-09-02 01:50:45 +02003273 struct sctp_inpcb *inp_tmp;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003274#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003275 struct inpcb *ip_inp;
t00fcxenaff14b62013-11-02 18:02:02 +00003276#endif
tuexendd729232011-11-01 23:04:43 +00003277 int port_reuse_active = 0;
3278 int bindall;
3279#ifdef SCTP_MVRF
3280 int i;
3281#endif
3282 uint16_t lport;
3283 int error;
3284 uint32_t vrf_id;
3285
Michael Tuexenff681752021-09-01 13:00:05 +02003286#if defined(__FreeBSD__) && !defined(__Userspace__)
3287 KASSERT(td != NULL, ("%s: null thread", __func__));
3288
3289#endif
3290 error = 0;
tuexendd729232011-11-01 23:04:43 +00003291 lport = 0;
tuexendd729232011-11-01 23:04:43 +00003292 bindall = 1;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003293#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003294 ip_inp = &inp->ip_inp.inp;
t00fcxenaff14b62013-11-02 18:02:02 +00003295#endif
Michael Tuexena8409b32021-09-02 01:50:45 +02003296
3297 SCTP_INP_INFO_WLOCK_ASSERT();
3298 SCTP_INP_WLOCK_ASSERT(inp);
3299
tuexendd729232011-11-01 23:04:43 +00003300#ifdef SCTP_DEBUG
3301 if (addr) {
t00fcxen94dfd1e2012-12-27 08:14:10 +00003302 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
tuexendd729232011-11-01 23:04:43 +00003303 ntohs(((struct sockaddr_in *)addr)->sin_port));
t00fcxen94dfd1e2012-12-27 08:14:10 +00003304 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
tuexendd729232011-11-01 23:04:43 +00003305 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3306 }
3307#endif
3308 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003309 error = EINVAL;
tuexendd729232011-11-01 23:04:43 +00003310 /* already did a bind, subsequent binds NOT allowed ! */
Michael Tuexenff681752021-09-01 13:00:05 +02003311 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3312 goto out;
tuexendd729232011-11-01 23:04:43 +00003313 }
tuexendd729232011-11-01 23:04:43 +00003314 if (addr != NULL) {
3315 switch (addr->sa_family) {
3316#ifdef INET
3317 case AF_INET:
3318 {
3319 struct sockaddr_in *sin;
3320
3321 /* IPV6_V6ONLY socket? */
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003322 if (SCTP_IPV6_V6ONLY(inp)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003323 error = EINVAL;
3324 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3325 goto out;
tuexendd729232011-11-01 23:04:43 +00003326 }
t00fcxena8a26f62012-09-12 19:24:54 +00003327#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003328 if (addr->sa_len != sizeof(*sin)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003329 error = EINVAL;
3330 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3331 goto out;
tuexendd729232011-11-01 23:04:43 +00003332 }
3333#endif
3334
3335 sin = (struct sockaddr_in *)addr;
3336 lport = sin->sin_port;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003337#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxenc24796c2014-05-06 15:39:13 +00003338 /*
3339 * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3340 * to the proper value.
3341 */
Michael Tuexenff681752021-09-01 13:00:05 +02003342 if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) {
t00fcxenc24796c2014-05-06 15:39:13 +00003343 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003344 goto out;
t00fcxenc24796c2014-05-06 15:39:13 +00003345 }
tuexendd729232011-11-01 23:04:43 +00003346#endif
3347 if (sin->sin_addr.s_addr != INADDR_ANY) {
3348 bindall = 0;
3349 }
3350 break;
3351 }
3352#endif
3353#ifdef INET6
3354 case AF_INET6:
3355 {
3356 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3357 struct sockaddr_in6 *sin6;
3358
3359 sin6 = (struct sockaddr_in6 *)addr;
t00fcxena8a26f62012-09-12 19:24:54 +00003360#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003361 if (addr->sa_len != sizeof(*sin6)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003362 error = EINVAL;
3363 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3364 goto out;
tuexendd729232011-11-01 23:04:43 +00003365 }
3366#endif
tuexendd729232011-11-01 23:04:43 +00003367 lport = sin6->sin6_port;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003368#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxenc24796c2014-05-06 15:39:13 +00003369 /*
3370 * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
3371 * to the proper value.
3372 */
Michael Tuexenff681752021-09-01 13:00:05 +02003373 if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr,
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003374 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
t00fcxenc24796c2014-05-06 15:39:13 +00003375 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003376 goto out;
t00fcxenc24796c2014-05-06 15:39:13 +00003377 }
tuexendd729232011-11-01 23:04:43 +00003378#endif
3379 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3380 bindall = 0;
3381#ifdef SCTP_EMBEDDED_V6_SCOPE
3382 /* KAME hack: embed scopeid */
3383#if defined(SCTP_KAME)
3384 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003385 error = EINVAL;
3386 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3387 goto out;
tuexendd729232011-11-01 23:04:43 +00003388 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02003389#elif defined(__APPLE__) && !defined(__Userspace__)
tuexen9a218b12012-08-04 21:17:58 +00003390#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
3391 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003392#else
tuexen9a218b12012-08-04 21:17:58 +00003393 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003394#endif
Michael Tuexenff681752021-09-01 13:00:05 +02003395 error = EINVAL;
3396 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3397 goto out;
tuexendd729232011-11-01 23:04:43 +00003398 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02003399#elif defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003400 error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
3401 if (error != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003402 error = EINVAL;
tuexendd729232011-11-01 23:04:43 +00003403 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003404 goto out;
tuexendd729232011-11-01 23:04:43 +00003405 }
tuexendd729232011-11-01 23:04:43 +00003406#else
3407 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003408 error = EINVAL;
3409 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3410 goto out;
tuexendd729232011-11-01 23:04:43 +00003411 }
3412#endif
3413#endif /* SCTP_EMBEDDED_V6_SCOPE */
3414 }
3415#ifndef SCOPEDROUTING
3416 /* this must be cleared for ifa_ifwithaddr() */
3417 sin6->sin6_scope_id = 0;
3418#endif /* SCOPEDROUTING */
3419 break;
3420 }
3421#endif
tuexen09a676e2012-07-08 21:03:18 +00003422#if defined(__Userspace__)
3423 case AF_CONN:
3424 {
3425 struct sockaddr_conn *sconn;
t00fcxenefb47e72012-09-09 08:16:51 +00003426
t00fcxena8a26f62012-09-12 19:24:54 +00003427#ifdef HAVE_SA_LEN
tuexen09a676e2012-07-08 21:03:18 +00003428 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003429 error = EINVAL;
3430 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3431 goto out;
tuexen09a676e2012-07-08 21:03:18 +00003432 }
3433#endif
3434 sconn = (struct sockaddr_conn *)addr;
3435 lport = sconn->sconn_port;
3436 if (sconn->sconn_addr != NULL) {
3437 bindall = 0;
3438 }
3439 break;
3440 }
3441#endif
tuexendd729232011-11-01 23:04:43 +00003442 default:
Michael Tuexenff681752021-09-01 13:00:05 +02003443 error = EAFNOSUPPORT;
3444 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3445 goto out;
tuexendd729232011-11-01 23:04:43 +00003446 }
3447 }
tuexendd729232011-11-01 23:04:43 +00003448 /* Setup a vrf_id to be the default for the non-bind-all case. */
t00fcxenc24796c2014-05-06 15:39:13 +00003449 vrf_id = inp->def_vrf_id;
tuexendd729232011-11-01 23:04:43 +00003450
tuexendd729232011-11-01 23:04:43 +00003451 if (lport) {
3452 /*
t00fcxenefb47e72012-09-09 08:16:51 +00003453 * Did the caller specify a port? if so we must see if an ep
tuexendd729232011-11-01 23:04:43 +00003454 * already has this one bound.
3455 */
3456 /* got to be root to get at low ports */
Michael Tuexeneb611572020-06-13 17:27:02 +02003457#if !(defined(_WIN32) && !defined(__Userspace__))
Michael Tuexena8409b32021-09-02 01:50:45 +02003458 if (ntohs(lport) < IPPORT_RESERVED &&
Michael Tuexen5be0c252020-06-13 00:53:56 +02003459#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003460 (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
Michael Tuexen5be0c252020-06-13 00:53:56 +02003461#elif defined(__APPLE__) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003462 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003463#elif defined(__Userspace__)
3464 /* TODO ensure uid is 0, etc... */
Michael Tuexena8409b32021-09-02 01:50:45 +02003465 0) {
tuexendd729232011-11-01 23:04:43 +00003466#else
Michael Tuexena8409b32021-09-02 01:50:45 +02003467 (error = suser(p, 0)) != 0) {
tuexendd729232011-11-01 23:04:43 +00003468#endif
Michael Tuexena8409b32021-09-02 01:50:45 +02003469 goto out;
tuexendd729232011-11-01 23:04:43 +00003470 }
Michael Tuexeneb611572020-06-13 17:27:02 +02003471#endif
Michael Tuexena8409b32021-09-02 01:50:45 +02003472 SCTP_INP_INCR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00003473 SCTP_INP_WUNLOCK(inp);
3474 if (bindall) {
3475#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003476 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003477 vrf_id = inp->m_vrf_ids[i];
3478#else
3479 vrf_id = inp->def_vrf_id;
3480#endif
3481 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3482 if (inp_tmp != NULL) {
3483 /*
3484 * lock guy returned and lower count
3485 * note that we are not bound so
3486 * inp_tmp should NEVER be inp. And
3487 * it is this inp (inp_tmp) that gets
3488 * the reference bump, so we must
3489 * lower it.
3490 */
3491 SCTP_INP_DECR_REF(inp_tmp);
3492 /* unlock info */
3493 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3494 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3495 /* Ok, must be one-2-one and allowing port re-use */
3496 port_reuse_active = 1;
3497 goto continue_anyway;
3498 }
Michael Tuexena8409b32021-09-02 01:50:45 +02003499 SCTP_INP_WLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00003500 SCTP_INP_DECR_REF(inp);
Michael Tuexenff681752021-09-01 13:00:05 +02003501 error = EADDRINUSE;
3502 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexena8409b32021-09-02 01:50:45 +02003503 goto out;
tuexendd729232011-11-01 23:04:43 +00003504 }
3505#ifdef SCTP_MVRF
3506 }
3507#endif
3508 } else {
3509 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3510 if (inp_tmp != NULL) {
3511 /*
3512 * lock guy returned and lower count note
3513 * that we are not bound so inp_tmp should
3514 * NEVER be inp. And it is this inp (inp_tmp)
3515 * that gets the reference bump, so we must
3516 * lower it.
3517 */
3518 SCTP_INP_DECR_REF(inp_tmp);
3519 /* unlock info */
3520 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3521 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3522 /* Ok, must be one-2-one and allowing port re-use */
3523 port_reuse_active = 1;
3524 goto continue_anyway;
3525 }
Michael Tuexena8409b32021-09-02 01:50:45 +02003526 SCTP_INP_WLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00003527 SCTP_INP_DECR_REF(inp);
Michael Tuexenff681752021-09-01 13:00:05 +02003528 error = EADDRINUSE;
3529 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexena8409b32021-09-02 01:50:45 +02003530 goto out;
tuexendd729232011-11-01 23:04:43 +00003531 }
3532 }
3533 continue_anyway:
3534 SCTP_INP_WLOCK(inp);
Michael Tuexenff681752021-09-01 13:00:05 +02003535 SCTP_INP_DECR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00003536 if (bindall) {
3537 /* verify that no lport is not used by a singleton */
t00fcxenefb47e72012-09-09 08:16:51 +00003538 if ((port_reuse_active == 0) &&
3539 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
tuexendd729232011-11-01 23:04:43 +00003540 /* Sorry someone already has this one bound */
3541 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3542 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3543 port_reuse_active = 1;
3544 } else {
Michael Tuexenff681752021-09-01 13:00:05 +02003545 error = EADDRINUSE;
3546 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3547 goto out;
tuexendd729232011-11-01 23:04:43 +00003548 }
3549 }
3550 }
3551 } else {
3552 uint16_t first, last, candidate;
t00fcxenc24796c2014-05-06 15:39:13 +00003553 uint16_t count;
tuexendd729232011-11-01 23:04:43 +00003554
Michael Tuexenff681752021-09-01 13:00:05 +02003555#if defined(__Userspace__)
3556 first = MODULE_GLOBAL(ipport_firstauto);
3557 last = MODULE_GLOBAL(ipport_lastauto);
3558#elif defined(_WIN32)
tuexendd729232011-11-01 23:04:43 +00003559 first = 1;
3560 last = 0xffff;
Michael Tuexenff681752021-09-01 13:00:05 +02003561#elif defined(__FreeBSD__) || defined(__APPLE__)
t00fcxenc24796c2014-05-06 15:39:13 +00003562 if (ip_inp->inp_flags & INP_HIGHPORT) {
3563 first = MODULE_GLOBAL(ipport_hifirstauto);
3564 last = MODULE_GLOBAL(ipport_hilastauto);
3565 } else if (ip_inp->inp_flags & INP_LOWPORT) {
Michael Tuexena5089952020-06-09 23:11:08 +02003566#if defined(__FreeBSD__)
Michael Tuexenff681752021-09-01 13:00:05 +02003567 if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
tuexendd729232011-11-01 23:04:43 +00003568#else
Michael Tuexenff681752021-09-01 13:00:05 +02003569 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
tuexendd729232011-11-01 23:04:43 +00003570#endif
tuexendd729232011-11-01 23:04:43 +00003571 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003572 goto out;
tuexendd729232011-11-01 23:04:43 +00003573 }
t00fcxenc24796c2014-05-06 15:39:13 +00003574 first = MODULE_GLOBAL(ipport_lowfirstauto);
3575 last = MODULE_GLOBAL(ipport_lowlastauto);
3576 } else {
t00fcxen8dd95b82014-07-11 20:41:28 +00003577 first = MODULE_GLOBAL(ipport_firstauto);
3578 last = MODULE_GLOBAL(ipport_lastauto);
t00fcxenc24796c2014-05-06 15:39:13 +00003579 }
tuexendd729232011-11-01 23:04:43 +00003580#endif
tuexendd729232011-11-01 23:04:43 +00003581 if (first > last) {
3582 uint16_t temp;
3583
3584 temp = first;
3585 first = last;
3586 last = temp;
3587 }
3588 count = last - first + 1; /* number of candidates */
3589 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3590
Michael Tuexenff681752021-09-01 13:00:05 +02003591 for (;;) {
tuexendd729232011-11-01 23:04:43 +00003592#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003593 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003594 if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
3595 break;
3596 }
3597 }
3598 if (i == inp->num_vrfs) {
Michael Tuexenff681752021-09-01 13:00:05 +02003599 lport = htons(candidate);
3600 break;
tuexendd729232011-11-01 23:04:43 +00003601 }
3602#else
3603 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
Michael Tuexenff681752021-09-01 13:00:05 +02003604 lport = htons(candidate);
3605 break;
tuexendd729232011-11-01 23:04:43 +00003606 }
3607#endif
Michael Tuexenff681752021-09-01 13:00:05 +02003608 if (--count == 0) {
3609 error = EADDRINUSE;
3610 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3611 goto out;
tuexendd729232011-11-01 23:04:43 +00003612 }
Michael Tuexenff681752021-09-01 13:00:05 +02003613 if (candidate == last)
3614 candidate = first;
3615 else
3616 candidate = candidate + 1;
tuexendd729232011-11-01 23:04:43 +00003617 }
tuexendd729232011-11-01 23:04:43 +00003618 }
tuexendd729232011-11-01 23:04:43 +00003619 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3620 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3621 /*
3622 * this really should not happen. The guy did a non-blocking
3623 * bind and then did a close at the same time.
3624 */
Michael Tuexenff681752021-09-01 13:00:05 +02003625 error = EINVAL;
3626 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3627 goto out;
tuexendd729232011-11-01 23:04:43 +00003628 }
3629 /* ok we look clear to give out this port, so lets setup the binding */
3630 if (bindall) {
3631 /* binding to all addresses, so just set in the proper flags */
3632 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3633 /* set the automatic addr changes from kernel flag */
3634 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3635 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3636 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3637 } else {
3638 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3639 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3640 }
3641 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3642 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3643 } else {
3644 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3645 }
3646 /* set the automatic mobility_base from kernel
3647 flag (by micchie)
3648 */
3649 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3650 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3651 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003652 } else {
tuexendd729232011-11-01 23:04:43 +00003653 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3654 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3655 }
3656 /* set the automatic mobility_fasthandoff from kernel
3657 flag (by micchie)
3658 */
3659 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3660 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3661 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003662 } else {
tuexendd729232011-11-01 23:04:43 +00003663 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3664 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3665 }
3666 } else {
3667 /*
3668 * bind specific, make sure flags is off and add a new
3669 * address structure to the sctp_addr_list inside the ep
3670 * structure.
3671 *
3672 * We will need to allocate one and insert it at the head. The
3673 * socketopt call can just insert new addresses in there as
3674 * well. It will also have to do the embed scope kame hack
3675 * too (before adding).
3676 */
3677 struct sctp_ifa *ifa;
t00fcxen05f07c02014-09-07 10:15:26 +00003678 union sctp_sockstore store;
tuexendd729232011-11-01 23:04:43 +00003679
t00fcxen05f07c02014-09-07 10:15:26 +00003680 memset(&store, 0, sizeof(store));
tuexendd729232011-11-01 23:04:43 +00003681 switch (addr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00003682#ifdef INET
tuexendd729232011-11-01 23:04:43 +00003683 case AF_INET:
t00fcxen05f07c02014-09-07 10:15:26 +00003684 memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
3685 store.sin.sin_port = 0;
tuexendd729232011-11-01 23:04:43 +00003686 break;
tuexen310f1bc2012-07-15 11:20:56 +00003687#endif
3688#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00003689 case AF_INET6:
t00fcxen05f07c02014-09-07 10:15:26 +00003690 memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
3691 store.sin6.sin6_port = 0;
tuexendd729232011-11-01 23:04:43 +00003692 break;
tuexen310f1bc2012-07-15 11:20:56 +00003693#endif
tuexencc2b1422012-07-10 21:50:32 +00003694#if defined(__Userspace__)
3695 case AF_CONN:
t00fcxen05f07c02014-09-07 10:15:26 +00003696 memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn));
3697 store.sconn.sconn_port = 0;
tuexencc2b1422012-07-10 21:50:32 +00003698 break;
tuexencc2b1422012-07-10 21:50:32 +00003699#endif
tuexendd729232011-11-01 23:04:43 +00003700 default:
3701 break;
3702 }
3703 /*
3704 * first find the interface with the bound address need to
3705 * zero out the port to find the address! yuck! can't do
3706 * this earlier since need port for sctp_pcb_findep()
3707 */
t00fcxenefb47e72012-09-09 08:16:51 +00003708 if (sctp_ifap != NULL) {
tuexendd729232011-11-01 23:04:43 +00003709 ifa = sctp_ifap;
t00fcxenefb47e72012-09-09 08:16:51 +00003710 } else {
tuexendd729232011-11-01 23:04:43 +00003711 /* Note for BSD we hit here always other
3712 * O/S's will pass things in via the
Michael Tuexen3b4263d2020-06-06 19:52:50 +02003713 * sctp_ifap argument.
tuexendd729232011-11-01 23:04:43 +00003714 */
t00fcxen05f07c02014-09-07 10:15:26 +00003715 ifa = sctp_find_ifa_by_addr(&store.sa,
tuexendd729232011-11-01 23:04:43 +00003716 vrf_id, SCTP_ADDR_NOT_LOCKED);
3717 }
3718 if (ifa == NULL) {
Michael Tuexenff681752021-09-01 13:00:05 +02003719 error = EADDRNOTAVAIL;
tuexendd729232011-11-01 23:04:43 +00003720 /* Can't find an interface with that address */
Michael Tuexenff681752021-09-01 13:00:05 +02003721 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3722 goto out;
tuexendd729232011-11-01 23:04:43 +00003723 }
3724#ifdef INET6
3725 if (addr->sa_family == AF_INET6) {
3726 /* GAK, more FIXME IFA lock? */
3727 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3728 /* Can't bind a non-existent addr. */
Michael Tuexenff681752021-09-01 13:00:05 +02003729 error = EINVAL;
3730 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3731 goto out;
tuexendd729232011-11-01 23:04:43 +00003732 }
3733 }
3734#endif
3735 /* we're not bound all */
3736 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3737 /* allow bindx() to send ASCONF's for binding changes */
3738 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3739 /* clear automatic addr changes from kernel flag */
3740 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3741
3742 /* add this address to the endpoint list */
3743 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
Michael Tuexenff681752021-09-01 13:00:05 +02003744 if (error != 0)
3745 goto out;
tuexendd729232011-11-01 23:04:43 +00003746 inp->laddr_count++;
3747 }
3748 /* find the bucket */
tuexen98456cf2012-04-19 15:37:07 +00003749 if (port_reuse_active) {
tuexendd729232011-11-01 23:04:43 +00003750 /* Put it into tcp 1-2-1 hash */
3751 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3752 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
t00fcxenc24796c2014-05-06 15:39:13 +00003753 } else {
tuexendd729232011-11-01 23:04:43 +00003754 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3755 }
3756 /* put it in the bucket */
3757 LIST_INSERT_HEAD(head, inp, sctp_hash);
3758 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +00003759 (void *)head, ntohs(lport), port_reuse_active);
tuexendd729232011-11-01 23:04:43 +00003760 /* set in the port */
3761 inp->sctp_lport = lport;
3762
3763 /* turn off just the unbound flag */
Michael Tuexenff681752021-09-01 13:00:05 +02003764 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0,
3765 ("%s: inp %p is already bound", __func__, inp));
tuexendd729232011-11-01 23:04:43 +00003766 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
Michael Tuexenff681752021-09-01 13:00:05 +02003767out:
Michael Tuexena8409b32021-09-02 01:50:45 +02003768 return (error);
3769}
3770
3771int
3772#if defined(__FreeBSD__) && !defined(__Userspace__)
3773sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3774 struct sctp_ifa *sctp_ifap, struct thread *td)
3775#elif defined(_WIN32) && !defined(__Userspace__)
3776sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3777 struct sctp_ifa *sctp_ifap, PKTHREAD p)
3778#else
3779sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3780 struct sctp_ifa *sctp_ifap, struct proc *p)
3781#endif
3782{
3783 struct sctp_inpcb *inp;
3784 int error;
3785
3786 inp = so->so_pcb;
3787 SCTP_INP_INFO_WLOCK();
3788 SCTP_INP_WLOCK(inp);
3789#if defined(__FreeBSD__) && !defined(__Userspace__)
3790 error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td);
3791#else
3792 error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, p);
3793#endif
tuexendd729232011-11-01 23:04:43 +00003794 SCTP_INP_WUNLOCK(inp);
3795 SCTP_INP_INFO_WUNLOCK();
Michael Tuexenff681752021-09-01 13:00:05 +02003796 return (error);
tuexendd729232011-11-01 23:04:43 +00003797}
3798
tuexendd729232011-11-01 23:04:43 +00003799static void
3800sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3801{
3802 struct sctp_iterator *it, *nit;
3803
3804 /*
3805 * We enter with the only the ITERATOR_LOCK in place and a write
3806 * lock on the inp_info stuff.
3807 */
3808 it = sctp_it_ctl.cur_it;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003809#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003810 if (it && (it->vn != curvnet)) {
3811 /* Its not looking at our VNET */
3812 return;
3813 }
3814#endif
3815 if (it && (it->inp == inp)) {
3816 /*
3817 * This is tricky and we hold the iterator lock,
3818 * but when it returns and gets the lock (when we
3819 * release it) the iterator will try to operate on
3820 * inp. We need to stop that from happening. But
3821 * of course the iterator has a reference on the
3822 * stcb and inp. We can mark it and it will stop.
3823 *
3824 * If its a single iterator situation, we
3825 * set the end iterator flag. Otherwise
3826 * we set the iterator to go to the next inp.
3827 *
3828 */
3829 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3830 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3831 } else {
3832 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3833 }
3834 }
3835 /* Now go through and remove any single reference to
3836 * our inp that may be still pending on the list
3837 */
3838 SCTP_IPI_ITERATOR_WQ_LOCK();
3839 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
Michael Tuexen5be0c252020-06-13 00:53:56 +02003840#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003841 if (it->vn != curvnet) {
3842 continue;
tuexen09631162012-04-18 11:23:02 +00003843 }
tuexendd729232011-11-01 23:04:43 +00003844#endif
3845 if (it->inp == inp) {
3846 /* This one points to me is it inp specific? */
3847 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3848 /* Remove and free this one */
3849 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3850 it, sctp_nxt_itr);
3851 if (it->function_atend != NULL) {
3852 (*it->function_atend) (it->pointer, it->val);
3853 }
3854 SCTP_FREE(it, SCTP_M_ITER);
3855 } else {
3856 it->inp = LIST_NEXT(it->inp, sctp_list);
3857 if (it->inp) {
3858 SCTP_INP_INCR_REF(it->inp);
3859 }
3860 }
3861 /* When its put in the refcnt is incremented so decr it */
3862 SCTP_INP_DECR_REF(inp);
3863 }
3864 }
3865 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3866}
3867
3868/* release sctp_inpcb unbind the port */
3869void
3870sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3871{
3872 /*
3873 * Here we free a endpoint. We must find it (if it is in the Hash
3874 * table) and remove it from there. Then we must also find it in the
3875 * overall list and remove it from there. After all removals are
3876 * complete then any timer has to be stopped. Then start the actual
3877 * freeing. a) Any local lists. b) Any associations. c) The hash of
3878 * all associations. d) finally the ep itself.
3879 */
tuexendd729232011-11-01 23:04:43 +00003880 struct sctp_tcb *asoc, *nasoc;
3881 struct sctp_laddr *laddr, *nladdr;
3882 struct inpcb *ip_pcb;
3883 struct socket *so;
tuexen63fc0bb2011-12-27 12:24:52 +00003884 int being_refed = 0;
tuexendd729232011-11-01 23:04:43 +00003885 struct sctp_queued_to_read *sq, *nsq;
Michael Tuexen3b4263d2020-06-06 19:52:50 +02003886#if !defined(__Userspace__)
Michael Tuexena5089952020-06-09 23:11:08 +02003887#if !defined(__FreeBSD__)
tuexendd729232011-11-01 23:04:43 +00003888 sctp_rtentry_t *rt;
3889#endif
3890#endif
3891 int cnt;
3892 sctp_sharedkey_t *shared_key, *nshared_key;
3893
Michael Tuexen5be0c252020-06-13 00:53:56 +02003894#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003895 sctp_lock_assert(SCTP_INP_SO(inp));
3896#endif
3897#ifdef SCTP_LOG_CLOSING
3898 sctp_log_closing(inp, NULL, 0);
3899#endif
3900 SCTP_ITERATOR_LOCK();
3901 /* mark any iterators on the list or being processed */
3902 sctp_iterator_inp_being_freed(inp);
3903 SCTP_ITERATOR_UNLOCK();
Michael Tuexen900def62021-09-08 13:12:50 +02003904
tuexendd729232011-11-01 23:04:43 +00003905 SCTP_ASOC_CREATE_LOCK(inp);
3906 SCTP_INP_INFO_WLOCK();
tuexendd729232011-11-01 23:04:43 +00003907 SCTP_INP_WLOCK(inp);
Michael Tuexen900def62021-09-08 13:12:50 +02003908 so = inp->sctp_socket;
3909 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0,
3910 ("%s: inp %p still has socket", __func__, inp));
3911 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0,
3912 ("%s: double free of inp %p", __func__, inp));
tuexendd729232011-11-01 23:04:43 +00003913 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3914 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3915 /* socket is gone, so no more wakeups allowed */
3916 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3917 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3918 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
tuexendd729232011-11-01 23:04:43 +00003919 }
3920 /* First time through we have the socket lock, after that no more. */
3921 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
Michael Tuexen390beeb2020-02-04 15:05:45 +01003922 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
tuexendd729232011-11-01 23:04:43 +00003923
3924 if (inp->control) {
3925 sctp_m_freem(inp->control);
3926 inp->control = NULL;
3927 }
3928 if (inp->pkt) {
3929 sctp_m_freem(inp->pkt);
3930 inp->pkt = NULL;
3931 }
tuexendd729232011-11-01 23:04:43 +00003932 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3933 * here but I will be nice :> (i.e.
3934 * ip_pcb = ep;) */
3935 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3936 int cnt_in_sd;
3937
3938 cnt_in_sd = 0;
3939 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3940 SCTP_TCB_LOCK(asoc);
3941 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
Michael Tuexen45533ff2021-09-09 17:55:38 +02003942 asoc->sctp_socket = NULL;
tuexendd729232011-11-01 23:04:43 +00003943 /* Skip guys being freed */
3944 cnt_in_sd++;
3945 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3946 /*
3947 * Special case - we did not start a kill
3948 * timer on the asoc due to it was not
3949 * closed. So go ahead and start it now.
3950 */
Michael Tuexen348a36c2018-08-13 16:24:47 +02003951 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00003952 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3953 }
3954 SCTP_TCB_UNLOCK(asoc);
3955 continue;
3956 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02003957 if (((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3958 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
tuexendd729232011-11-01 23:04:43 +00003959 (asoc->asoc.total_output_queue_size == 0)) {
3960 /* If we have data in queue, we don't want to just
3961 * free since the app may have done, send()/close
3962 * or connect/send/close. And it wants the data
3963 * to get across first.
3964 */
3965 /* Just abandon things in the front states */
3966 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00003967 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
tuexendd729232011-11-01 23:04:43 +00003968 cnt_in_sd++;
3969 }
3970 continue;
3971 }
3972 /* Disconnect the socket please */
3973 asoc->sctp_socket = NULL;
Michael Tuexen348a36c2018-08-13 16:24:47 +02003974 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET);
tuexendd729232011-11-01 23:04:43 +00003975 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3976 (asoc->asoc.control_pdapi) ||
3977 (asoc->asoc.size_on_all_streams > 0) ||
tuexen63fc0bb2011-12-27 12:24:52 +00003978 (so && (so->so_rcv.sb_cc > 0))) {
tuexendd729232011-11-01 23:04:43 +00003979 /* Left with Data unread */
3980 struct mbuf *op_err;
3981
t00fcxen08f9ff92014-03-16 13:38:54 +00003982 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00003983 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
tuexendd729232011-11-01 23:04:43 +00003984 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3985 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen348a36c2018-08-13 16:24:47 +02003986 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3987 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00003988 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3989 }
3990 if (sctp_free_assoc(inp, asoc,
t00fcxen0057a6d2015-05-28 16:42:49 +00003991 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
tuexendd729232011-11-01 23:04:43 +00003992 cnt_in_sd++;
3993 }
3994 continue;
3995 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3996 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
t00fcxen28611aa2012-09-23 07:45:40 +00003997 (asoc->asoc.stream_queue_cnt == 0)) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02003998 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
tuexendd729232011-11-01 23:04:43 +00003999 goto abort_anyway;
4000 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004001 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4002 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
tuexendd729232011-11-01 23:04:43 +00004003 struct sctp_nets *netp;
4004
t00fcxend0ad16b2013-02-09 18:34:24 +00004005 /*
4006 * there is nothing queued to send,
4007 * so I send shutdown
4008 */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004009 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4010 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004011 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4012 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004013 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
t00fcxend0ad16b2013-02-09 18:34:24 +00004014 sctp_stop_timers_for_shutdown(asoc);
tuexendd729232011-11-01 23:04:43 +00004015 if (asoc->asoc.alternate) {
4016 netp = asoc->asoc.alternate;
4017 } else {
4018 netp = asoc->asoc.primary_destination;
4019 }
tuexendd729232011-11-01 23:04:43 +00004020 sctp_send_shutdown(asoc, netp);
tuexendd729232011-11-01 23:04:43 +00004021 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
4022 netp);
Michael Tuexend07a5f22020-03-19 23:34:46 +01004023 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
tuexendd729232011-11-01 23:04:43 +00004024 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
4025 }
4026 } else {
4027 /* mark into shutdown pending */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004028 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
Michael Tuexend07a5f22020-03-19 23:34:46 +01004029 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02004030 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02004031 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT);
tuexendd729232011-11-01 23:04:43 +00004032 }
4033 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
4034 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
4035 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4036 struct mbuf *op_err;
4037 abort_anyway:
t00fcxen08f9ff92014-03-16 13:38:54 +00004038 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00004039 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
tuexendd729232011-11-01 23:04:43 +00004040 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4041 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen348a36c2018-08-13 16:24:47 +02004042 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4043 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004044 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4045 }
4046 if (sctp_free_assoc(inp, asoc,
4047 SCTP_PCBFREE_NOFORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00004048 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
tuexendd729232011-11-01 23:04:43 +00004049 cnt_in_sd++;
4050 }
4051 continue;
4052 } else {
4053 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
4054 }
4055 }
4056 cnt_in_sd++;
4057 SCTP_TCB_UNLOCK(asoc);
4058 }
4059 /* now is there some left in our SHUTDOWN state? */
4060 if (cnt_in_sd) {
4061#ifdef SCTP_LOG_CLOSING
4062 sctp_log_closing(inp, NULL, 2);
4063#endif
4064 inp->sctp_socket = NULL;
4065 SCTP_INP_WUNLOCK(inp);
4066 SCTP_ASOC_CREATE_UNLOCK(inp);
4067 SCTP_INP_INFO_WUNLOCK();
4068 return;
4069 }
4070 }
4071 inp->sctp_socket = NULL;
4072 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
4073 SCTP_PCB_FLAGS_UNBOUND) {
4074 /*
4075 * ok, this guy has been bound. It's port is
4076 * somewhere in the SCTP_BASE_INFO(hash table). Remove
4077 * it!
4078 */
4079 LIST_REMOVE(inp, sctp_hash);
4080 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
4081 }
4082
4083 /* If there is a timer running to kill us,
4084 * forget it, since it may have a contest
4085 * on the INP lock.. which would cause us
4086 * to die ...
4087 */
4088 cnt = 0;
4089 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
4090 SCTP_TCB_LOCK(asoc);
Michael Tuexen547d3b42020-07-23 21:49:32 +02004091 if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
4092 /* Disconnect the socket please */
4093 asoc->sctp_socket = NULL;
4094 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET);
4095 }
tuexendd729232011-11-01 23:04:43 +00004096 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4097 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02004098 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
t00fcxen28611aa2012-09-23 07:45:40 +00004099 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
tuexendd729232011-11-01 23:04:43 +00004100 }
Michael Tuexen61f66832020-02-09 23:15:19 +01004101 cnt++;
tuexendd729232011-11-01 23:04:43 +00004102 SCTP_TCB_UNLOCK(asoc);
4103 continue;
4104 }
4105 /* Free associations that are NOT killing us */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004106 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
tuexendd729232011-11-01 23:04:43 +00004107 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
4108 struct mbuf *op_err;
t00fcxen28611aa2012-09-23 07:45:40 +00004109
t00fcxen08f9ff92014-03-16 13:38:54 +00004110 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00004111 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
tuexendd729232011-11-01 23:04:43 +00004112 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4113 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4114 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4115 cnt++;
4116 SCTP_TCB_UNLOCK(asoc);
4117 continue;
4118 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004119 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4120 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004121 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4122 }
t00fcxen0057a6d2015-05-28 16:42:49 +00004123 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE,
4124 SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
tuexendd729232011-11-01 23:04:43 +00004125 cnt++;
4126 }
4127 }
4128 if (cnt) {
4129 /* Ok we have someone out there that will kill us */
tuexendd729232011-11-01 23:04:43 +00004130#ifdef SCTP_LOG_CLOSING
4131 sctp_log_closing(inp, NULL, 3);
4132#endif
4133 SCTP_INP_WUNLOCK(inp);
4134 SCTP_ASOC_CREATE_UNLOCK(inp);
4135 SCTP_INP_INFO_WUNLOCK();
4136 return;
4137 }
4138 if (SCTP_INP_LOCK_CONTENDED(inp))
4139 being_refed++;
4140 if (SCTP_INP_READ_CONTENDED(inp))
4141 being_refed++;
tuexen63fc0bb2011-12-27 12:24:52 +00004142 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
tuexendd729232011-11-01 23:04:43 +00004143 being_refed++;
Michael Tuexen939d48f2020-07-19 14:44:43 +02004144 /* NOTE: 0 refcount also means no timers are referencing us. */
tuexen63fc0bb2011-12-27 12:24:52 +00004145 if ((inp->refcount) ||
t00fcxen28611aa2012-09-23 07:45:40 +00004146 (being_refed) ||
4147 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
tuexendd729232011-11-01 23:04:43 +00004148#ifdef SCTP_LOG_CLOSING
4149 sctp_log_closing(inp, NULL, 4);
4150#endif
4151 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4152 SCTP_INP_WUNLOCK(inp);
4153 SCTP_ASOC_CREATE_UNLOCK(inp);
4154 SCTP_INP_INFO_WUNLOCK();
4155 return;
4156 }
4157 inp->sctp_ep.signature_change.type = 0;
4158 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4159 /* Remove it from the list .. last thing we need a
4160 * lock for.
4161 */
4162 LIST_REMOVE(inp, sctp_list);
4163 SCTP_INP_WUNLOCK(inp);
4164 SCTP_ASOC_CREATE_UNLOCK(inp);
4165 SCTP_INP_INFO_WUNLOCK();
tuexendd729232011-11-01 23:04:43 +00004166
4167#ifdef SCTP_LOG_CLOSING
4168 sctp_log_closing(inp, NULL, 5);
4169#endif
Michael Tuexeneb611572020-06-13 17:27:02 +02004170#if !(defined(_WIN32) || defined(__Userspace__))
Michael Tuexen5be0c252020-06-13 00:53:56 +02004171#if !(defined(__FreeBSD__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004172 rt = ip_pcb->inp_route.ro_rt;
4173#endif
4174#endif
tuexendd729232011-11-01 23:04:43 +00004175 if ((inp->sctp_asocidhash) != NULL) {
4176 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4177 inp->sctp_asocidhash = NULL;
4178 }
4179 /*sa_ignore FREED_MEMORY*/
4180 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4181 /* Its only abandoned if it had data left */
4182 if (sq->length)
4183 SCTP_STAT_INCR(sctps_left_abandon);
4184
4185 TAILQ_REMOVE(&inp->read_queue, sq, next);
4186 sctp_free_remote_addr(sq->whoFrom);
4187 if (so)
4188 so->so_rcv.sb_cc -= sq->length;
4189 if (sq->data) {
4190 sctp_m_freem(sq->data);
4191 sq->data = NULL;
4192 }
4193 /*
4194 * no need to free the net count, since at this point all
4195 * assoc's are gone.
4196 */
Michael Tuexene5001952016-04-17 19:25:27 +02004197 sctp_free_a_readq(NULL, sq);
tuexendd729232011-11-01 23:04:43 +00004198 }
4199 /* Now the sctp_pcb things */
4200 /*
4201 * free each asoc if it is not already closed/free. we can't use the
4202 * macro here since le_next will get freed as part of the
4203 * sctp_free_assoc() call.
4204 */
tuexendd729232011-11-01 23:04:43 +00004205 if (ip_pcb->inp_options) {
4206 (void)sctp_m_free(ip_pcb->inp_options);
4207 ip_pcb->inp_options = 0;
4208 }
Michael Tuexeneb611572020-06-13 17:27:02 +02004209#if !(defined(_WIN32) || defined(__Userspace__))
Michael Tuexena5089952020-06-09 23:11:08 +02004210#if !defined(__FreeBSD__)
tuexendd729232011-11-01 23:04:43 +00004211 if (rt) {
4212 RTFREE(rt);
4213 ip_pcb->inp_route.ro_rt = 0;
4214 }
4215#endif
tuexendd729232011-11-01 23:04:43 +00004216#endif
tuexendd729232011-11-01 23:04:43 +00004217#ifdef INET6
Michael Tuexeneb611572020-06-13 17:27:02 +02004218#if !(defined(_WIN32) || defined(__Userspace__))
Michael Tuexen5be0c252020-06-13 00:53:56 +02004219#if (defined(__FreeBSD__) || defined(__APPLE__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004220 if (ip_pcb->inp_vflag & INP_IPV6) {
t00fcxen4c76a912012-09-14 14:31:21 +00004221#else
4222 if (inp->inp_vflag & INP_IPV6) {
tuexendd729232011-11-01 23:04:43 +00004223#endif
Michael Tuexen801a64b2019-08-05 15:33:37 +02004224 ip6_freepcbopts(ip_pcb->in6p_outputopts);
tuexendd729232011-11-01 23:04:43 +00004225 }
t00fcxen4c76a912012-09-14 14:31:21 +00004226#endif
tuexendd729232011-11-01 23:04:43 +00004227#endif /* INET6 */
tuexendd729232011-11-01 23:04:43 +00004228 ip_pcb->inp_vflag = 0;
tuexendd729232011-11-01 23:04:43 +00004229 /* free up authentication fields */
4230 if (inp->sctp_ep.local_auth_chunks != NULL)
4231 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4232 if (inp->sctp_ep.local_hmacs != NULL)
4233 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4234
4235 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4236 LIST_REMOVE(shared_key, next);
4237 sctp_free_sharedkey(shared_key);
4238 /*sa_ignore FREED_MEMORY*/
4239 }
4240
Michael Tuexen5be0c252020-06-13 00:53:56 +02004241#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004242 inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
tuexen6d01b942012-05-04 18:15:17 +00004243 if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
tuexen49fdcc82012-05-04 10:13:12 +00004244#ifdef INVARIANTS
t00fcxen8fca7ff2012-09-05 18:58:42 +00004245 panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen6d01b942012-05-04 18:15:17 +00004246#else
t00fcxen8fca7ff2012-09-05 18:58:42 +00004247 SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen49fdcc82012-05-04 10:13:12 +00004248#endif
tuexen6d01b942012-05-04 18:15:17 +00004249 }
tuexendd729232011-11-01 23:04:43 +00004250 inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4251#endif
4252 /*
4253 * if we have an address list the following will free the list of
4254 * ifaddr's that are set into this ep. Again macro limitations here,
4255 * since the LIST_FOREACH could be a bad idea.
4256 */
4257 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4258 sctp_remove_laddr(laddr);
4259 }
4260
4261#ifdef SCTP_TRACK_FREED_ASOCS
4262 /* TEMP CODE */
4263 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
4264 LIST_REMOVE(asoc, sctp_tcblist);
4265 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
4266 SCTP_DECR_ASOC_COUNT();
4267 }
4268 /* *** END TEMP CODE ****/
4269#endif
4270#ifdef SCTP_MVRF
4271 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4272#endif
4273 /* Now lets see about freeing the EP hash table. */
4274 if (inp->sctp_tcbhash != NULL) {
4275 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4276 inp->sctp_tcbhash = NULL;
4277 }
4278 /* Now we must put the ep memory back into the zone pool */
Michael Tuexen5be0c252020-06-13 00:53:56 +02004279#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00004280 crfree(inp->ip_inp.inp.inp_cred);
tuexendd729232011-11-01 23:04:43 +00004281 INP_LOCK_DESTROY(&inp->ip_inp.inp);
4282#endif
4283 SCTP_INP_LOCK_DESTROY(inp);
4284 SCTP_INP_READ_DESTROY(inp);
4285 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004286#if !(defined(__APPLE__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004287 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4288 SCTP_DECR_EP_COUNT();
4289#else
4290 /* For Tiger, we will do this later... */
4291#endif
4292}
4293
tuexendd729232011-11-01 23:04:43 +00004294struct sctp_nets *
4295sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4296{
4297 struct sctp_nets *net;
4298 /* locate the address */
4299 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4300 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4301 return (net);
4302 }
4303 return (NULL);
4304}
4305
tuexendd729232011-11-01 23:04:43 +00004306int
4307sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4308{
tuexendd729232011-11-01 23:04:43 +00004309 struct sctp_ifa *sctp_ifa;
4310 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4311 if (sctp_ifa) {
4312 return (1);
4313 } else {
4314 return (0);
4315 }
tuexendd729232011-11-01 23:04:43 +00004316}
4317
4318/*
4319 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4320 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4321 * stats of stuff.
4322 */
4323int
4324sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
Michael Tuexen27574422016-04-30 16:35:29 +02004325 struct sctp_nets **netp, uint16_t port, int set_scope, int from)
tuexendd729232011-11-01 23:04:43 +00004326{
4327 /*
4328 * The following is redundant to the same lines in the
4329 * sctp_aloc_assoc() but is needed since others call the add
4330 * address function
4331 */
4332 struct sctp_nets *net, *netfirst;
4333 int addr_inscope;
4334
4335 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4336 from);
4337 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4338
4339 netfirst = sctp_findnet(stcb, newaddr);
4340 if (netfirst) {
4341 /*
4342 * Lie and return ok, we don't want to make the association
4343 * go away for this behavior. It will happen in the TCP
4344 * model in a connected socket. It does not reach the hash
4345 * table until after the association is built so it can't be
4346 * found. Mark as reachable, since the initial creation will
4347 * have been cleared and the NOT_IN_ASSOC flag will have
4348 * been added... and we don't want to end up removing it
4349 * back out.
4350 */
4351 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4352 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4353 SCTP_ADDR_UNCONFIRMED);
4354 } else {
4355 netfirst->dest_state = SCTP_ADDR_REACHABLE;
4356 }
4357
4358 return (0);
4359 }
4360 addr_inscope = 1;
4361 switch (newaddr->sa_family) {
4362#ifdef INET
4363 case AF_INET:
4364 {
4365 struct sockaddr_in *sin;
4366
4367 sin = (struct sockaddr_in *)newaddr;
4368 if (sin->sin_addr.s_addr == 0) {
4369 /* Invalid address */
4370 return (-1);
4371 }
Michael Tuexend0853302017-07-19 17:01:31 +02004372 /* zero out the zero area */
tuexendd729232011-11-01 23:04:43 +00004373 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4374
4375 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004376#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00004377 sin->sin_len = sizeof(struct sockaddr_in);
4378#endif
4379 if (set_scope) {
tuexendd729232011-11-01 23:04:43 +00004380 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004381 stcb->asoc.scope.ipv4_local_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004382 }
tuexendd729232011-11-01 23:04:43 +00004383 } else {
4384 /* Validate the address is in scope */
4385 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004386 (stcb->asoc.scope.ipv4_local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004387 addr_inscope = 0;
4388 }
4389 }
4390 break;
4391 }
4392#endif
4393#ifdef INET6
4394 case AF_INET6:
4395 {
4396 struct sockaddr_in6 *sin6;
4397
4398 sin6 = (struct sockaddr_in6 *)newaddr;
4399 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4400 /* Invalid address */
4401 return (-1);
4402 }
4403 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004404#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00004405 sin6->sin6_len = sizeof(struct sockaddr_in6);
4406#endif
4407 if (set_scope) {
4408 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004409 stcb->asoc.scope.loopback_scope = 1;
4410 stcb->asoc.scope.local_scope = 0;
4411 stcb->asoc.scope.ipv4_local_scope = 1;
4412 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004413 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4414 /*
4415 * If the new destination is a LINK_LOCAL we
4416 * must have common site scope. Don't set
4417 * the local scope since we may not share
4418 * all links, only loopback can do this.
4419 * Links on the local network would also be
4420 * on our private network for v4 too.
4421 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004422 stcb->asoc.scope.ipv4_local_scope = 1;
4423 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004424 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4425 /*
4426 * If the new destination is SITE_LOCAL then
4427 * we must have site scope in common.
4428 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004429 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004430 }
4431 } else {
4432 /* Validate the address is in scope */
4433 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004434 (stcb->asoc.scope.loopback_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004435 addr_inscope = 0;
4436 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004437 (stcb->asoc.scope.local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004438 addr_inscope = 0;
4439 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004440 (stcb->asoc.scope.site_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004441 addr_inscope = 0;
4442 }
4443 }
4444 break;
4445 }
4446#endif
tuexencc2b1422012-07-10 21:50:32 +00004447#if defined(__Userspace__)
4448 case AF_CONN:
4449 {
4450 struct sockaddr_conn *sconn;
4451
4452 sconn = (struct sockaddr_conn *)newaddr;
4453 if (sconn->sconn_addr == NULL) {
4454 /* Invalid address */
4455 return (-1);
4456 }
t00fcxen8fbaee32012-09-04 16:41:39 +00004457#ifdef HAVE_SCONN_LEN
tuexencc2b1422012-07-10 21:50:32 +00004458 sconn->sconn_len = sizeof(struct sockaddr_conn);
4459#endif
4460 break;
4461 }
4462#endif
tuexendd729232011-11-01 23:04:43 +00004463 default:
4464 /* not supported family type */
4465 return (-1);
4466 }
4467 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4468 if (net == NULL) {
4469 return (-1);
4470 }
4471 SCTP_INCR_RADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02004472 memset(net, 0, sizeof(struct sctp_nets));
tuexendd729232011-11-01 23:04:43 +00004473 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
t00fcxen907930b2012-09-12 19:43:22 +00004474#ifdef HAVE_SA_LEN
4475 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
4476#endif
tuexendd729232011-11-01 23:04:43 +00004477 switch (newaddr->sa_family) {
4478#ifdef INET
4479 case AF_INET:
t00fcxen907930b2012-09-12 19:43:22 +00004480#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004481 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4482#endif
4483 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4484 break;
4485#endif
4486#ifdef INET6
4487 case AF_INET6:
t00fcxen907930b2012-09-12 19:43:22 +00004488#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004489 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4490#endif
4491 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4492 break;
4493#endif
tuexencc2b1422012-07-10 21:50:32 +00004494#if defined(__Userspace__)
4495 case AF_CONN:
t00fcxen907930b2012-09-12 19:43:22 +00004496#ifndef HAVE_SA_LEN
tuexencc2b1422012-07-10 21:50:32 +00004497 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4498#endif
4499 ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4500 break;
4501#endif
tuexendd729232011-11-01 23:04:43 +00004502 default:
4503 break;
4504 }
4505 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4506 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004507 stcb->asoc.scope.loopback_scope = 1;
4508 stcb->asoc.scope.ipv4_local_scope = 1;
4509 stcb->asoc.scope.local_scope = 0;
4510 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004511 addr_inscope = 1;
4512 }
4513 net->failure_threshold = stcb->asoc.def_net_failure;
4514 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4515 if (addr_inscope == 0) {
4516 net->dest_state = (SCTP_ADDR_REACHABLE |
4517 SCTP_ADDR_OUT_OF_SCOPE);
4518 } else {
4519 if (from == SCTP_ADDR_IS_CONFIRMED)
4520 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4521 net->dest_state = SCTP_ADDR_REACHABLE;
4522 else
4523 net->dest_state = SCTP_ADDR_REACHABLE |
4524 SCTP_ADDR_UNCONFIRMED;
4525 }
4526 /* We set this to 0, the timer code knows that
4527 * this means its an initial value
4528 */
4529 net->rto_needed = 1;
t00fcxen8dd95b82014-07-11 20:41:28 +00004530 net->RTO = 0;
tuexendd729232011-11-01 23:04:43 +00004531 net->RTO_measured = 0;
4532 stcb->asoc.numnets++;
tuexen9d7b2072011-11-20 16:35:17 +00004533 net->ref_count = 1;
tuexendd729232011-11-01 23:04:43 +00004534 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
Michael Tuexen27574422016-04-30 16:35:29 +02004535 net->port = port;
tuexendd729232011-11-01 23:04:43 +00004536 net->dscp = stcb->asoc.default_dscp;
4537#ifdef INET6
4538 net->flowlabel = stcb->asoc.default_flowlabel;
4539#endif
4540 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4541 net->dest_state |= SCTP_ADDR_NOHB;
4542 } else {
4543 net->dest_state &= ~SCTP_ADDR_NOHB;
4544 }
4545 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4546 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4547 } else {
4548 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4549 }
4550 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4551 /* Init the timer structure */
4552 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4553 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4554 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4555
4556 /* Now generate a route for this guy */
4557#ifdef INET6
4558#ifdef SCTP_EMBEDDED_V6_SCOPE
4559 /* KAME hack: embed scopeid */
4560 if (newaddr->sa_family == AF_INET6) {
4561 struct sockaddr_in6 *sin6;
4562
4563 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004564#if defined(__APPLE__) && !defined(__Userspace__)
tuexen9a218b12012-08-04 21:17:58 +00004565#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4566 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
tuexendd729232011-11-01 23:04:43 +00004567#else
tuexen9a218b12012-08-04 21:17:58 +00004568 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
tuexendd729232011-11-01 23:04:43 +00004569#endif
4570#elif defined(SCTP_KAME)
4571 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4572#else
4573 (void)in6_embedscope(&sin6->sin6_addr, sin6);
4574#endif
4575#ifndef SCOPEDROUTING
4576 sin6->sin6_scope_id = 0;
4577#endif
4578 }
4579#endif /* SCTP_EMBEDDED_V6_SCOPE */
4580#endif
t00fcxena51e4462015-06-17 15:53:23 +00004581 SCTP_RTALLOC((sctp_route_t *)&net->ro,
4582 stcb->asoc.vrf_id,
4583 stcb->sctp_ep->fibnum);
tuexendd729232011-11-01 23:04:43 +00004584
t00fcxen8b6ef092014-07-11 17:37:40 +00004585 net->src_addr_selected = 0;
Michael Tuexend900e5f2017-06-23 10:52:43 +02004586#if !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004587 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4588 /* Get source address */
4589 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004590 stcb,
4591 (sctp_route_t *)&net->ro,
4592 net,
4593 0,
4594 stcb->asoc.vrf_id);
Michael Tuexende0630d2017-11-03 22:04:01 +01004595 if (stcb->asoc.default_mtu > 0) {
4596 net->mtu = stcb->asoc.default_mtu;
4597 switch (net->ro._l_addr.sa.sa_family) {
4598#ifdef INET
4599 case AF_INET:
4600 net->mtu += SCTP_MIN_V4_OVERHEAD;
4601 break;
4602#endif
4603#ifdef INET6
4604 case AF_INET6:
4605 net->mtu += SCTP_MIN_OVERHEAD;
4606 break;
4607#endif
4608#if defined(__Userspace__)
4609 case AF_CONN:
4610 net->mtu += sizeof(struct sctphdr);
4611 break;
4612#endif
4613 default:
4614 break;
4615 }
4616#if defined(INET) || defined(INET6)
4617 if (net->port) {
4618 net->mtu += (uint32_t)sizeof(struct udphdr);
4619 }
4620#endif
4621 } else if (net->ro._s_addr != NULL) {
Michael Tuexend900e5f2017-06-23 10:52:43 +02004622 uint32_t imtu, rmtu, hcmtu;
4623
t00fcxen8b6ef092014-07-11 17:37:40 +00004624 net->src_addr_selected = 1;
4625 /* Now get the interface MTU */
4626 if (net->ro._s_addr->ifn_p != NULL) {
Michael Tuexend900e5f2017-06-23 10:52:43 +02004627 imtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
4628 } else {
4629 imtu = 0;
t00fcxen8b6ef092014-07-11 17:37:40 +00004630 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02004631#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004632 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh);
Michael Tuexend900e5f2017-06-23 10:52:43 +02004633 hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
4634#else
Michael Tuexena0515422020-05-18 00:47:04 +02004635 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
Michael Tuexend900e5f2017-06-23 10:52:43 +02004636 hcmtu = 0;
4637#endif
4638 net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004639#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004640#else
tuexendd729232011-11-01 23:04:43 +00004641 if (rmtu == 0) {
4642 /* Start things off to match mtu of interface please. */
4643 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004644 net->ro.ro_rt, net->mtu);
tuexendd729232011-11-01 23:04:43 +00004645 }
Michael Tuexena0515422020-05-18 00:47:04 +02004646#endif
t00fcxen8b6ef092014-07-11 17:37:40 +00004647 }
tuexen09631162012-04-18 11:23:02 +00004648 }
t00fcxen6d60eb02013-08-30 07:37:01 +00004649#endif
tuexene81177b2011-11-15 20:48:08 +00004650 if (net->mtu == 0) {
Michael Tuexende0630d2017-11-03 22:04:01 +01004651 if (stcb->asoc.default_mtu > 0) {
4652 net->mtu = stcb->asoc.default_mtu;
4653 switch (net->ro._l_addr.sa.sa_family) {
tuexene81177b2011-11-15 20:48:08 +00004654#ifdef INET
Michael Tuexende0630d2017-11-03 22:04:01 +01004655 case AF_INET:
4656 net->mtu += SCTP_MIN_V4_OVERHEAD;
4657 break;
tuexene81177b2011-11-15 20:48:08 +00004658#endif
4659#ifdef INET6
Michael Tuexende0630d2017-11-03 22:04:01 +01004660 case AF_INET6:
4661 net->mtu += SCTP_MIN_OVERHEAD;
4662 break;
tuexene81177b2011-11-15 20:48:08 +00004663#endif
tuexencc2b1422012-07-10 21:50:32 +00004664#if defined(__Userspace__)
Michael Tuexende0630d2017-11-03 22:04:01 +01004665 case AF_CONN:
4666 net->mtu += sizeof(struct sctphdr);
4667 break;
tuexencc2b1422012-07-10 21:50:32 +00004668#endif
Michael Tuexende0630d2017-11-03 22:04:01 +01004669 default:
4670 break;
4671 }
4672#if defined(INET) || defined(INET6)
4673 if (net->port) {
4674 net->mtu += (uint32_t)sizeof(struct udphdr);
4675 }
4676#endif
4677 } else {
4678 switch (newaddr->sa_family) {
4679#ifdef INET
4680 case AF_INET:
4681 net->mtu = SCTP_DEFAULT_MTU;
4682 break;
4683#endif
4684#ifdef INET6
4685 case AF_INET6:
4686 net->mtu = 1280;
4687 break;
4688#endif
4689#if defined(__Userspace__)
4690 case AF_CONN:
4691 net->mtu = 1280;
4692 break;
4693#endif
4694 default:
4695 break;
4696 }
tuexendd729232011-11-01 23:04:43 +00004697 }
tuexene81177b2011-11-15 20:48:08 +00004698 }
t00fcxen1ce83bb2014-07-11 07:17:36 +00004699#if defined(INET) || defined(INET6)
tuexene81177b2011-11-15 20:48:08 +00004700 if (net->port) {
4701 net->mtu -= (uint32_t)sizeof(struct udphdr);
4702 }
t00fcxen5ab37c82014-06-30 20:55:44 +00004703#endif
tuexene81177b2011-11-15 20:48:08 +00004704 if (from == SCTP_ALLOC_ASOC) {
4705 stcb->asoc.smallest_mtu = net->mtu;
4706 }
4707 if (stcb->asoc.smallest_mtu > net->mtu) {
Michael Tuexena40c76b2021-12-30 15:53:03 +01004708 sctp_pathmtu_adjustment(stcb, net->mtu, true);
tuexendd729232011-11-01 23:04:43 +00004709 }
4710#ifdef INET6
4711#ifdef SCTP_EMBEDDED_V6_SCOPE
4712 if (newaddr->sa_family == AF_INET6) {
4713 struct sockaddr_in6 *sin6;
4714
4715 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4716#ifdef SCTP_KAME
4717 (void)sa6_recoverscope(sin6);
4718#else
4719 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4720#endif /* SCTP_KAME */
4721 }
4722#endif /* SCTP_EMBEDDED_V6_SCOPE */
4723#endif
tuexendd729232011-11-01 23:04:43 +00004724
4725 /* JRS - Use the congestion control given in the CC module */
tuexen09631162012-04-18 11:23:02 +00004726 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
tuexendd729232011-11-01 23:04:43 +00004727 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4728
4729 /*
4730 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4731 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4732 */
4733 net->find_pseudo_cumack = 1;
4734 net->find_rtx_pseudo_cumack = 1;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004735#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004736 /* Choose an initial flowid. */
4737 net->flowid = stcb->asoc.my_vtag ^
4738 ntohs(stcb->rport) ^
4739 ntohs(stcb->sctp_ep->sctp_lport);
Michael Tuexen15f35f82016-06-07 09:19:50 +02004740 net->flowtype = M_HASHTYPE_OPAQUE_HASH;
tuexendd729232011-11-01 23:04:43 +00004741#endif
4742 if (netp) {
4743 *netp = net;
4744 }
4745 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004746#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004747 if (net->ro.ro_nh == NULL) {
4748#else
tuexendd729232011-11-01 23:04:43 +00004749 if (net->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004750#endif
tuexendd729232011-11-01 23:04:43 +00004751 /* Since we have no route put it at the back */
4752 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4753 } else if (netfirst == NULL) {
4754 /* We are the first one in the pool. */
4755 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004756#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004757 } else if (netfirst->ro.ro_nh == NULL) {
4758#else
tuexendd729232011-11-01 23:04:43 +00004759 } else if (netfirst->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004760#endif
tuexendd729232011-11-01 23:04:43 +00004761 /*
4762 * First one has NO route. Place this one ahead of the first
4763 * one.
4764 */
4765 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004766#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004767 } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
4768#else
tuexendd729232011-11-01 23:04:43 +00004769 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
Michael Tuexena0515422020-05-18 00:47:04 +02004770#endif
tuexendd729232011-11-01 23:04:43 +00004771 /*
4772 * This one has a different interface than the one at the
4773 * top of the list. Place it ahead.
4774 */
4775 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004776 } else {
4777 /*
4778 * Ok we have the same interface as the first one. Move
4779 * forward until we find either a) one with a NULL route...
4780 * insert ahead of that b) one with a different ifp.. insert
4781 * after that. c) end of the list.. insert at the tail.
4782 */
4783 struct sctp_nets *netlook;
4784
4785 do {
4786 netlook = TAILQ_NEXT(netfirst, sctp_next);
4787 if (netlook == NULL) {
4788 /* End of the list */
4789 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4790 break;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004791#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004792 } else if (netlook->ro.ro_nh == NULL) {
4793#else
tuexendd729232011-11-01 23:04:43 +00004794 } else if (netlook->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004795#endif
tuexendd729232011-11-01 23:04:43 +00004796 /* next one has NO route */
4797 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4798 break;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004799#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004800 } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
tuexendd729232011-11-01 23:04:43 +00004801#else
Michael Tuexena0515422020-05-18 00:47:04 +02004802 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
tuexendd729232011-11-01 23:04:43 +00004803#endif
tuexendd729232011-11-01 23:04:43 +00004804 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
Michael Tuexena0515422020-05-18 00:47:04 +02004805 net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004806 break;
4807 }
tuexendd729232011-11-01 23:04:43 +00004808 /* Shift forward */
4809 netfirst = netlook;
tuexendd729232011-11-01 23:04:43 +00004810 } while (netlook != NULL);
4811 }
4812
4813 /* got to have a primary set */
4814 if (stcb->asoc.primary_destination == 0) {
4815 stcb->asoc.primary_destination = net;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004816#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004817 } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
4818 (net->ro.ro_nh) &&
4819#else
tuexendd729232011-11-01 23:04:43 +00004820 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
Michael Tuexena0515422020-05-18 00:47:04 +02004821 (net->ro.ro_rt) &&
4822#endif
4823 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
tuexendd729232011-11-01 23:04:43 +00004824 /* No route to current primary adopt new primary */
4825 stcb->asoc.primary_destination = net;
4826 }
4827 /* Validate primary is first */
4828 net = TAILQ_FIRST(&stcb->asoc.nets);
4829 if ((net != stcb->asoc.primary_destination) &&
4830 (stcb->asoc.primary_destination)) {
4831 /* first one on the list is NOT the primary
4832 * sctp_cmpaddr() is much more efficient if
4833 * the primary is the first on the list, make it
4834 * so.
4835 */
4836 TAILQ_REMOVE(&stcb->asoc.nets,
4837 stcb->asoc.primary_destination, sctp_next);
4838 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4839 stcb->asoc.primary_destination, sctp_next);
4840 }
4841 return (0);
4842}
4843
tuexendd729232011-11-01 23:04:43 +00004844static uint32_t
4845sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4846{
4847 uint32_t id;
4848 struct sctpasochead *head;
4849 struct sctp_tcb *lstcb;
4850
tuexendd729232011-11-01 23:04:43 +00004851 try_again:
4852 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4853 /* TSNH */
tuexendd729232011-11-01 23:04:43 +00004854 return (0);
4855 }
4856 /*
4857 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4858 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4859 */
4860 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4861 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4862 }
4863 id = inp->sctp_associd_counter;
4864 inp->sctp_associd_counter++;
4865 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4866 if (lstcb) {
4867 goto try_again;
4868 }
4869 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4870 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4871 stcb->asoc.in_asocid_hash = 1;
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01004872 return (id);
tuexendd729232011-11-01 23:04:43 +00004873}
4874
4875/*
4876 * allocate an association and add it to the endpoint. The caller must be
4877 * careful to add all additional addresses once they are know right away or
4878 * else the assoc will be may experience a blackout scenario.
4879 */
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004880static struct sctp_tcb *
4881sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4882 int *error, uint32_t override_tag, uint32_t initial_tsn,
4883 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
Michael Tuexen5be0c252020-06-13 00:53:56 +02004884#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004885 struct thread *p,
Michael Tuexeneb611572020-06-13 17:27:02 +02004886#elif defined(_WIN32) && !defined(__Userspace__)
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004887 PKTHREAD p,
tuexendd729232011-11-01 23:04:43 +00004888#else
4889#if defined(__Userspace__)
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004890 /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
tuexendd729232011-11-01 23:04:43 +00004891#endif
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004892 struct proc *p,
tuexendd729232011-11-01 23:04:43 +00004893#endif
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004894 int initialize_auth_params)
tuexendd729232011-11-01 23:04:43 +00004895{
4896 /* note the p argument is only valid in unbound sockets */
4897
4898 struct sctp_tcb *stcb;
4899 struct sctp_association *asoc;
4900 struct sctpasochead *head;
4901 uint16_t rport;
4902 int err;
4903
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004904 SCTP_INP_INFO_WLOCK_ASSERT();
4905 SCTP_INP_WLOCK_ASSERT(inp);
4906
tuexendd729232011-11-01 23:04:43 +00004907 /*
4908 * Assumption made here: Caller has done a
4909 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4910 * address does not exist already.
4911 */
4912 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4913 /* Hit max assoc, sorry no more */
4914 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4915 *error = ENOBUFS;
4916 return (NULL);
4917 }
4918 if (firstaddr == NULL) {
4919 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4920 *error = EINVAL;
4921 return (NULL);
4922 }
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004923 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4924 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4925 *error = EINVAL;
4926 return (NULL);
4927 }
tuexendd729232011-11-01 23:04:43 +00004928 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4929 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4930 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4931 /*
4932 * If its in the TCP pool, its NOT allowed to create an
4933 * association. The parent listener needs to call
4934 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4935 * off, or connected one does this.. its an error.
4936 */
tuexendd729232011-11-01 23:04:43 +00004937 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4938 *error = EINVAL;
4939 return (NULL);
4940 }
4941 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4942 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4943 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4944 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
tuexendd729232011-11-01 23:04:43 +00004945 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4946 *error = EINVAL;
4947 return (NULL);
4948 }
4949 }
4950 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4951#ifdef SCTP_DEBUG
4952 if (firstaddr) {
4953 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4954 switch (firstaddr->sa_family) {
4955#ifdef INET
4956 case AF_INET:
4957 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4958 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4959 break;
4960#endif
4961#ifdef INET6
4962 case AF_INET6:
4963 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4964 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4965 break;
4966#endif
tuexencc2b1422012-07-10 21:50:32 +00004967#if defined(__Userspace__)
4968 case AF_CONN:
4969 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4970 ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
4971 break;
4972#endif
tuexendd729232011-11-01 23:04:43 +00004973 default:
4974 break;
4975 }
4976 } else {
4977 SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
4978 }
4979#endif /* SCTP_DEBUG */
4980 switch (firstaddr->sa_family) {
4981#ifdef INET
4982 case AF_INET:
4983 {
4984 struct sockaddr_in *sin;
4985
4986 sin = (struct sockaddr_in *)firstaddr;
4987 if ((ntohs(sin->sin_port) == 0) ||
4988 (sin->sin_addr.s_addr == INADDR_ANY) ||
4989 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
Michael Tuexend844a692020-09-29 11:38:11 +02004990 IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) ||
4991#if defined(__Userspace__)
4992 (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) != 0) ||
4993 (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) != 0) &&
4994 (SCTP_IPV6_V6ONLY(inp) != 0)))) {
4995#else
4996 (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) != 0) &&
4997 (SCTP_IPV6_V6ONLY(inp) != 0))) {
4998#endif
tuexendd729232011-11-01 23:04:43 +00004999 /* Invalid address */
tuexendd729232011-11-01 23:04:43 +00005000 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5001 *error = EINVAL;
5002 return (NULL);
5003 }
5004 rport = sin->sin_port;
5005 break;
5006 }
5007#endif
5008#ifdef INET6
5009 case AF_INET6:
5010 {
5011 struct sockaddr_in6 *sin6;
5012
5013 sin6 = (struct sockaddr_in6 *)firstaddr;
5014 if ((ntohs(sin6->sin6_port) == 0) ||
5015 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
Michael Tuexend844a692020-09-29 11:38:11 +02005016 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) ||
5017 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) {
tuexendd729232011-11-01 23:04:43 +00005018 /* Invalid address */
tuexendd729232011-11-01 23:04:43 +00005019 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5020 *error = EINVAL;
5021 return (NULL);
5022 }
5023 rport = sin6->sin6_port;
5024 break;
5025 }
5026#endif
tuexencc2b1422012-07-10 21:50:32 +00005027#if defined(__Userspace__)
5028 case AF_CONN:
5029 {
5030 struct sockaddr_conn *sconn;
5031
5032 sconn = (struct sockaddr_conn *)firstaddr;
5033 if ((ntohs(sconn->sconn_port) == 0) ||
Michael Tuexend844a692020-09-29 11:38:11 +02005034 (sconn->sconn_addr == NULL) ||
5035 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) == 0)) {
tuexencc2b1422012-07-10 21:50:32 +00005036 /* Invalid address */
tuexencc2b1422012-07-10 21:50:32 +00005037 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5038 *error = EINVAL;
5039 return (NULL);
5040 }
5041 rport = sconn->sconn_port;
5042 break;
5043 }
5044#endif
tuexendd729232011-11-01 23:04:43 +00005045 default:
5046 /* not supported family type */
tuexendd729232011-11-01 23:04:43 +00005047 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5048 *error = EINVAL;
5049 return (NULL);
5050 }
tuexendd729232011-11-01 23:04:43 +00005051 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
5052 /*
5053 * If you have not performed a bind, then we need to do the
5054 * ephemeral bind for you.
5055 */
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005056 if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) {
tuexendd729232011-11-01 23:04:43 +00005057 /* bind error, probably perm */
5058 *error = err;
5059 return (NULL);
5060 }
5061 }
5062 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
5063 if (stcb == NULL) {
5064 /* out of memory? */
5065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
5066 *error = ENOMEM;
5067 return (NULL);
5068 }
5069 SCTP_INCR_ASOC_COUNT();
5070
Michael Tuexend0853302017-07-19 17:01:31 +02005071 memset(stcb, 0, sizeof(*stcb));
tuexendd729232011-11-01 23:04:43 +00005072 asoc = &stcb->asoc;
5073
tuexendd729232011-11-01 23:04:43 +00005074 SCTP_TCB_LOCK_INIT(stcb);
5075 SCTP_TCB_SEND_LOCK_INIT(stcb);
5076 stcb->rport = rport;
5077 /* setup back pointer's */
5078 stcb->sctp_ep = inp;
5079 stcb->sctp_socket = inp->sctp_socket;
Michael Tuexen965b19a2021-06-27 19:57:20 +02005080 if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) {
tuexendd729232011-11-01 23:04:43 +00005081 /* failed */
5082 SCTP_TCB_LOCK_DESTROY(stcb);
5083 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
tuexendd729232011-11-01 23:04:43 +00005084 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5085 SCTP_DECR_ASOC_COUNT();
5086 *error = err;
5087 return (NULL);
5088 }
tuexendd729232011-11-01 23:04:43 +00005089 SCTP_TCB_LOCK(stcb);
5090
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01005091 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
tuexendd729232011-11-01 23:04:43 +00005092 /* now that my_vtag is set, add it to the hash */
5093 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
5094 /* put it in the bucket in the vtag hash of assoc's for the system */
5095 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
tuexendd729232011-11-01 23:04:43 +00005096
Michael Tuexen5cd6fe92020-10-06 13:14:37 +02005097 if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) {
tuexendd729232011-11-01 23:04:43 +00005098 /* failure.. memory error? */
5099 if (asoc->strmout) {
5100 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5101 asoc->strmout = NULL;
5102 }
5103 if (asoc->mapping_array) {
5104 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5105 asoc->mapping_array = NULL;
5106 }
5107 if (asoc->nr_mapping_array) {
5108 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5109 asoc->nr_mapping_array = NULL;
5110 }
5111 SCTP_DECR_ASOC_COUNT();
t00fcxen2c2ff922013-11-03 14:03:35 +00005112 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005113 SCTP_TCB_LOCK_DESTROY(stcb);
5114 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005115 LIST_REMOVE(stcb, sctp_asocs);
tuexendd729232011-11-01 23:04:43 +00005116 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5117 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5118 SCTP_INP_WUNLOCK(inp);
5119 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
5120 *error = ENOBUFS;
5121 return (NULL);
5122 }
5123 /* Init all the timers */
5124 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
5125 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
5126 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
5127 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
5128 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
tuexendd729232011-11-01 23:04:43 +00005129 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5130
5131 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5132 /* now file the port under the hash as well */
5133 if (inp->sctp_tcbhash != NULL) {
5134 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5135 inp->sctp_hashmark)];
5136 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5137 }
Michael Tuexenbfb04f32019-07-14 14:42:17 +02005138 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
5139 sctp_initialize_auth_params(inp, stcb);
5140 }
t00fcxen8fca7ff2012-09-05 18:58:42 +00005141 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
tuexendd729232011-11-01 23:04:43 +00005142 return (stcb);
5143}
5144
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005145struct sctp_tcb *
5146sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
5147 int *error, uint32_t override_tag, uint32_t initial_tsn,
5148 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
5149#if defined(__FreeBSD__) && !defined(__Userspace__)
5150 struct thread *p,
5151#elif defined(_WIN32) && !defined(__Userspace__)
5152 PKTHREAD p,
5153#else
5154 struct proc *p,
5155#endif
5156 int initialize_auth_params)
5157{
5158 struct sctp_tcb *stcb;
5159
5160 SCTP_INP_INFO_WLOCK();
5161 SCTP_INP_WLOCK(inp);
5162 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
5163 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
5164 SCTP_INP_INFO_WUNLOCK();
5165 SCTP_INP_WUNLOCK(inp);
5166 return (stcb);
5167}
5168
5169struct sctp_tcb *
5170sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
5171 int *error, uint32_t override_tag, uint32_t initial_tsn,
5172 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
5173#if defined(__FreeBSD__) && !defined(__Userspace__)
5174 struct thread *p,
5175#elif defined(_WIN32) && !defined(__Userspace__)
5176 PKTHREAD p,
5177#else
5178 struct proc *p,
5179#endif
5180 int initialize_auth_params)
5181{
5182 struct sctp_tcb *stcb;
5183
5184 SCTP_INP_INFO_WLOCK();
5185 SCTP_INP_WLOCK(inp);
5186 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5187 SCTP_IS_LISTENING(inp)) {
5188 SCTP_INP_INFO_WUNLOCK();
5189 SCTP_INP_WUNLOCK(inp);
5190 *error = EINVAL;
5191 return (NULL);
5192 }
5193 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
5194 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
5195 SCTP_INP_INFO_WUNLOCK();
5196 if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
5197 inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
5198 soisconnecting(inp->sctp_socket);
5199 }
5200 SCTP_INP_WUNLOCK(inp);
5201 return (stcb);
5202}
5203
tuexendd729232011-11-01 23:04:43 +00005204void
5205sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5206{
Michael Tuexen21b55df2020-02-09 23:46:32 +01005207 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00005208 struct sctp_association *asoc;
5209
Michael Tuexen21b55df2020-02-09 23:46:32 +01005210 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00005211 asoc = &stcb->asoc;
5212 asoc->numnets--;
5213 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5214 if (net == asoc->primary_destination) {
5215 /* Reset primary */
5216 struct sctp_nets *lnet;
5217
5218 lnet = TAILQ_FIRST(&asoc->nets);
5219 /* Mobility adaptation
5220 Ideally, if deleted destination is the primary, it becomes
5221 a fast retransmission trigger by the subsequent SET PRIMARY.
5222 (by micchie)
5223 */
5224 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005225 SCTP_MOBILITY_BASE) ||
tuexendd729232011-11-01 23:04:43 +00005226 sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005227 SCTP_MOBILITY_FASTHANDOFF)) {
tuexendd729232011-11-01 23:04:43 +00005228 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5229 if (asoc->deleted_primary != NULL) {
5230 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5231 goto out;
5232 }
5233 asoc->deleted_primary = net;
5234 atomic_add_int(&net->ref_count, 1);
5235 memset(&net->lastsa, 0, sizeof(net->lastsa));
5236 memset(&net->lastsv, 0, sizeof(net->lastsv));
5237 sctp_mobility_feature_on(stcb->sctp_ep,
5238 SCTP_MOBILITY_PRIM_DELETED);
5239 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5240 stcb->sctp_ep, stcb, NULL);
5241 }
5242out:
5243 /* Try to find a confirmed primary */
5244 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5245 }
5246 if (net == asoc->last_data_chunk_from) {
5247 /* Reset primary */
5248 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5249 }
5250 if (net == asoc->last_control_chunk_from) {
5251 /* Clear net */
5252 asoc->last_control_chunk_from = NULL;
5253 }
Michael Tuexen6e00fe22021-02-21 17:31:55 +01005254 if (net == asoc->last_net_cmt_send_started) {
5255 /* Clear net */
5256 asoc->last_net_cmt_send_started = NULL;
5257 }
tuexendd729232011-11-01 23:04:43 +00005258 if (net == stcb->asoc.alternate) {
5259 sctp_free_remote_addr(stcb->asoc.alternate);
5260 stcb->asoc.alternate = NULL;
5261 }
Michael Tuexen21b55df2020-02-09 23:46:32 +01005262 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5263 SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
5264 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5265 SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
Michael Tuexenf52d3df2020-02-11 19:24:34 +01005266 net->dest_state |= SCTP_ADDR_BEING_DELETED;
tuexendd729232011-11-01 23:04:43 +00005267 sctp_free_remote_addr(net);
5268}
5269
5270/*
5271 * remove a remote endpoint address from an association, it will fail if the
5272 * address does not exist.
5273 */
5274int
5275sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5276{
5277 /*
5278 * Here we need to remove a remote address. This is quite simple, we
5279 * first find it in the list of address for the association
5280 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5281 * on that item. Note we do not allow it to be removed if there are
5282 * no other addresses.
5283 */
5284 struct sctp_association *asoc;
5285 struct sctp_nets *net, *nnet;
5286
5287 asoc = &stcb->asoc;
5288
5289 /* locate the address */
5290 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5291 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5292 continue;
5293 }
5294 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5295 remaddr)) {
5296 /* we found the guy */
5297 if (asoc->numnets < 2) {
5298 /* Must have at LEAST two remote addresses */
5299 return (-1);
5300 } else {
5301 sctp_remove_net(stcb, net);
5302 return (0);
5303 }
5304 }
5305 }
5306 /* not found. */
5307 return (-2);
5308}
5309
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005310static bool
5311sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, uint32_t now)
tuexendd729232011-11-01 23:04:43 +00005312{
5313 struct sctpvtaghead *chain;
5314 struct sctp_tagblock *twait_block;
tuexendd729232011-11-01 23:04:43 +00005315 int i;
5316
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005317 SCTP_INP_INFO_LOCK_ASSERT();
tuexendd729232011-11-01 23:04:43 +00005318 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
t00fcxen14f8fbb2013-06-25 09:32:47 +00005319 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5320 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005321 if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) &&
5322 (twait_block->vtag_block[i].v_tag == tag) &&
5323 (twait_block->vtag_block[i].lport == lport) &&
t00fcxen14f8fbb2013-06-25 09:32:47 +00005324 (twait_block->vtag_block[i].rport == rport)) {
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005325 return (true);
t00fcxen14f8fbb2013-06-25 09:32:47 +00005326 }
tuexendd729232011-11-01 23:04:43 +00005327 }
5328 }
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005329 return (false);
tuexendd729232011-11-01 23:04:43 +00005330}
5331
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005332static void
5333sctp_set_vtag_block(struct sctp_timewait *vtag_block, uint32_t time,
5334 uint32_t tag, uint16_t lport, uint16_t rport)
5335{
5336 vtag_block->tv_sec_at_expire = time;
5337 vtag_block->v_tag = tag;
5338 vtag_block->lport = lport;
5339 vtag_block->rport = rport;
5340}
5341
5342static void
5343sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
tuexendd729232011-11-01 23:04:43 +00005344{
5345 struct sctpvtaghead *chain;
5346 struct sctp_tagblock *twait_block;
5347 struct timeval now;
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005348 uint32_t time;
5349 int i;
5350 bool set;
tuexendd729232011-11-01 23:04:43 +00005351
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005352 SCTP_INP_INFO_WLOCK_ASSERT();
tuexendd729232011-11-01 23:04:43 +00005353 (void)SCTP_GETTIME_TIMEVAL(&now);
Michael Tuexen64707062021-09-09 20:08:10 +02005354 time = (uint32_t)now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait);
tuexendd729232011-11-01 23:04:43 +00005355 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005356 set = false;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005357 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
tuexendd729232011-11-01 23:04:43 +00005358 /* Block(s) present, lets find space, and expire on the fly */
t00fcxen14f8fbb2013-06-25 09:32:47 +00005359 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005360 if ((twait_block->vtag_block[i].v_tag == 0) && !set) {
5361 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
5362 set = true;
5363 continue;
5364 }
5365 if ((twait_block->vtag_block[i].v_tag != 0) &&
5366 (twait_block->vtag_block[i].tv_sec_at_expire < (uint32_t)now.tv_sec)) {
5367 if (set) {
5368 /* Audit expires this guy */
5369 sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0);
5370 } else {
5371 /* Reuse it for the new tag */
5372 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
5373 set = true;
tuexendd729232011-11-01 23:04:43 +00005374 }
5375 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005376 }
5377 if (set) {
5378 /*
5379 * We only do up to the block where we can
5380 * place our tag for audits
5381 */
5382 break;
tuexendd729232011-11-01 23:04:43 +00005383 }
5384 }
5385 /* Need to add a new block to chain */
5386 if (!set) {
5387 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5388 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5389 if (twait_block == NULL) {
tuexendd729232011-11-01 23:04:43 +00005390 return;
5391 }
5392 memset(twait_block, 0, sizeof(struct sctp_tagblock));
5393 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005394 sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport);
tuexendd729232011-11-01 23:04:43 +00005395 }
5396}
5397
Michael Tuexene5001952016-04-17 19:25:27 +02005398void
5399sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
5400{
5401 struct sctp_tmit_chunk *chk, *nchk;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005402 struct sctp_queued_to_read *control, *ncontrol;
5403
5404 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
5405 TAILQ_REMOVE(rh, control, next_instrm);
5406 control->on_strm_q = 0;
5407 if (control->on_read_q == 0) {
5408 sctp_free_remote_addr(control->whoFrom);
5409 if (control->data) {
5410 sctp_m_freem(control->data);
5411 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02005412 }
5413 }
5414 /* Reassembly free? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005415 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5416 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02005417 if (chk->data) {
5418 sctp_m_freem(chk->data);
5419 chk->data = NULL;
5420 }
5421 if (chk->holds_key_ref)
5422 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5423 sctp_free_remote_addr(chk->whoTo);
5424 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5425 SCTP_DECR_CHK_COUNT();
5426 /*sa_ignore FREED_MEMORY*/
5427 }
5428 /*
5429 * We don't free the address here
5430 * since all the net's were freed
5431 * above.
5432 */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005433 if (control->on_read_q == 0) {
5434 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02005435 }
5436 }
5437}
tuexendd729232011-11-01 23:04:43 +00005438
tuexendd729232011-11-01 23:04:43 +00005439/*-
5440 * Free the association after un-hashing the remote port. This
5441 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5442 * expect that the input to this function IS a locked TCB.
5443 * It will return 0, if it did NOT destroy the association (instead
5444 * it unlocks it. It will return NON-zero if it either destroyed the
5445 * association OR the association is already destroyed.
5446 */
5447int
5448sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5449{
5450 int i;
5451 struct sctp_association *asoc;
5452 struct sctp_nets *net, *nnet;
5453 struct sctp_laddr *laddr, *naddr;
5454 struct sctp_tmit_chunk *chk, *nchk;
5455 struct sctp_asconf_addr *aparam, *naparam;
5456 struct sctp_asconf_ack *aack, *naack;
5457 struct sctp_stream_reset_list *strrst, *nstrrst;
5458 struct sctp_queued_to_read *sq, *nsq;
5459 struct sctp_stream_queue_pending *sp, *nsp;
5460 sctp_sharedkey_t *shared_key, *nshared_key;
5461 struct socket *so;
5462
5463 /* first, lets purge the entry from the hash table. */
Michael Tuexen5be0c252020-06-13 00:53:56 +02005464#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00005465 sctp_lock_assert(SCTP_INP_SO(inp));
5466#endif
5467
5468#ifdef SCTP_LOG_CLOSING
5469 sctp_log_closing(inp, stcb, 6);
5470#endif
5471 if (stcb->asoc.state == 0) {
5472#ifdef SCTP_LOG_CLOSING
5473 sctp_log_closing(inp, NULL, 7);
5474#endif
5475 /* there is no asoc, really TSNH :-0 */
5476 return (1);
5477 }
Michael Tuexen31f4eb52020-08-16 15:36:30 +02005478 SCTP_TCB_SEND_LOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005479 if (stcb->asoc.alternate) {
5480 sctp_free_remote_addr(stcb->asoc.alternate);
5481 stcb->asoc.alternate = NULL;
5482 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02005483#if !(defined(__APPLE__) && !defined(__Userspace__))
5484 /* TEMP CODE */
tuexendd729232011-11-01 23:04:43 +00005485 if (stcb->freed_from_where == 0) {
5486 /* Only record the first place free happened from */
5487 stcb->freed_from_where = from_location;
5488 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02005489 /* TEMP CODE */
tuexendd729232011-11-01 23:04:43 +00005490#endif
5491
5492 asoc = &stcb->asoc;
5493 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5494 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5495 /* nothing around */
5496 so = NULL;
5497 else
5498 so = inp->sctp_socket;
5499
5500 /*
5501 * We used timer based freeing if a reader or writer is in the way.
5502 * So we first check if we are actually being called from a timer,
5503 * if so we abort early if a reader or writer is still in the way.
5504 */
5505 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5506 (from_inpcbfree == SCTP_NORMAL_PROC)) {
5507 /*
5508 * is it the timer driving us? if so are the reader/writers
5509 * gone?
5510 */
5511 if (stcb->asoc.refcnt) {
5512 /* nope, reader or writer in the way */
5513 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5514 /* no asoc destroyed */
Michael Tuexen31f4eb52020-08-16 15:36:30 +02005515 SCTP_TCB_SEND_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005516 SCTP_TCB_UNLOCK(stcb);
5517#ifdef SCTP_LOG_CLOSING
5518 sctp_log_closing(inp, stcb, 8);
5519#endif
5520 return (0);
5521 }
5522 }
Michael Tuexen61f66832020-02-09 23:15:19 +01005523 /* Now clean up any other timers */
Michael Tuexen5f976392020-02-11 21:20:07 +01005524 sctp_stop_association_timers(stcb, false);
tuexendd729232011-11-01 23:04:43 +00005525 /* Now the read queue needs to be cleaned up (only once) */
5526 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005527 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
tuexendd729232011-11-01 23:04:43 +00005528 SCTP_INP_READ_LOCK(inp);
5529 TAILQ_FOREACH(sq, &inp->read_queue, next) {
5530 if (sq->stcb == stcb) {
5531 sq->do_not_ref_stcb = 1;
5532 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5533 /* If there is no end, there never
5534 * will be now.
5535 */
5536 if (sq->end_added == 0) {
5537 /* Held for PD-API clear that. */
5538 sq->pdapi_aborted = 1;
5539 sq->held_length = 0;
5540 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
5541 /*
5542 * Need to add a PD-API aborted indication.
5543 * Setting the control_pdapi assures that it will
5544 * be added right after this msg.
5545 */
5546 uint32_t strseq;
5547 stcb->asoc.control_pdapi = sq;
Michael Tuexen00657ac2016-12-07 21:53:26 +01005548 strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff);
tuexendd729232011-11-01 23:04:43 +00005549 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5550 stcb,
5551 SCTP_PARTIAL_DELIVERY_ABORTED,
5552 (void *)&strseq,
5553 SCTP_SO_LOCKED);
5554 stcb->asoc.control_pdapi = NULL;
5555 }
5556 }
5557 /* Add an end to wake them */
5558 sq->end_added = 1;
5559 }
5560 }
5561 SCTP_INP_READ_UNLOCK(inp);
5562 if (stcb->block_entry) {
5563 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
5564 stcb->block_entry->error = ECONNRESET;
5565 stcb->block_entry = NULL;
5566 }
5567 }
5568 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
5569 /* Someone holds a reference OR the socket is unaccepted yet.
5570 */
5571 if ((stcb->asoc.refcnt) ||
5572 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5573 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005574 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00005575 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5576 }
tuexendd729232011-11-01 23:04:43 +00005577 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5578 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5579 /* nothing around */
5580 so = NULL;
5581 if (so) {
5582 /* Wake any reader/writers */
5583 sctp_sorwakeup(inp, so);
5584 sctp_sowwakeup(inp, so);
5585 }
Michael Tuexena8409b32021-09-02 01:50:45 +02005586 SCTP_TCB_SEND_UNLOCK(stcb);
5587 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005588
5589#ifdef SCTP_LOG_CLOSING
5590 sctp_log_closing(inp, stcb, 9);
5591#endif
5592 /* no asoc destroyed */
5593 return (0);
5594 }
5595#ifdef SCTP_LOG_CLOSING
5596 sctp_log_closing(inp, stcb, 10);
5597#endif
5598 /* When I reach here, no others want
5599 * to kill the assoc yet.. and I own
5600 * the lock. Now its possible an abort
5601 * comes in when I do the lock exchange
5602 * below to grab all the locks to do
5603 * the final take out. to prevent this
5604 * we increment the count, which will
5605 * start a timer and blow out above thus
5606 * assuring us that we hold exclusive
5607 * killing of the asoc. Note that
5608 * after getting back the TCB lock
5609 * we will go ahead and increment the
5610 * counter back up and stop any timer
5611 * a passing stranger may have started :-S
5612 */
5613 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5614 atomic_add_int(&stcb->asoc.refcnt, 1);
5615
Michael Tuexen31f4eb52020-08-16 15:36:30 +02005616 SCTP_TCB_SEND_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005617 SCTP_TCB_UNLOCK(stcb);
5618 SCTP_INP_INFO_WLOCK();
5619 SCTP_INP_WLOCK(inp);
5620 SCTP_TCB_LOCK(stcb);
Michael Tuexen31f4eb52020-08-16 15:36:30 +02005621 SCTP_TCB_SEND_LOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005622 }
5623 /* Double check the GONE flag */
5624 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5625 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5626 /* nothing around */
5627 so = NULL;
5628
Michael Tuexen1f495442020-03-25 16:41:21 +01005629 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5630 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5631 /*
5632 * For TCP type we need special handling when we are
5633 * connected. We also include the peel'ed off ones to.
5634 */
5635 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5636 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5637 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5638 if (so) {
5639 SOCKBUF_LOCK(&so->so_rcv);
5640 so->so_state &= ~(SS_ISCONNECTING |
5641 SS_ISDISCONNECTING |
5642 SS_ISCONFIRMING |
5643 SS_ISCONNECTED);
5644 so->so_state |= SS_ISDISCONNECTED;
Michael Tuexen5be0c252020-06-13 00:53:56 +02005645#if defined(__APPLE__) && !defined(__Userspace__)
Michael Tuexen1f495442020-03-25 16:41:21 +01005646 socantrcvmore(so);
5647#else
5648 socantrcvmore_locked(so);
5649#endif
5650 socantsendmore(so);
5651 sctp_sowwakeup(inp, so);
5652 sctp_sorwakeup(inp, so);
5653 SCTP_SOWAKEUP(so);
5654 }
5655 }
5656 }
5657
tuexendd729232011-11-01 23:04:43 +00005658 /* Make it invalid too, that way if its
5659 * about to run it will abort and return.
5660 */
5661 /* re-increment the lock */
5662 if (from_inpcbfree == SCTP_NORMAL_PROC) {
Michael Tuexena106ab82021-11-26 14:03:15 +01005663 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00005664 }
5665 if (stcb->asoc.refcnt) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005666 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00005667 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5668 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5669 SCTP_INP_INFO_WUNLOCK();
5670 SCTP_INP_WUNLOCK(inp);
5671 }
Michael Tuexen31f4eb52020-08-16 15:36:30 +02005672 SCTP_TCB_SEND_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005673 SCTP_TCB_UNLOCK(stcb);
5674 return (0);
5675 }
5676 asoc->state = 0;
5677 if (inp->sctp_tcbhash) {
5678 LIST_REMOVE(stcb, sctp_tcbhash);
5679 }
5680 if (stcb->asoc.in_asocid_hash) {
5681 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5682 }
Michael Tuexena8409b32021-09-02 01:50:45 +02005683 if (inp->sctp_socket == NULL) {
5684 stcb->sctp_socket = NULL;
5685 }
tuexendd729232011-11-01 23:04:43 +00005686 /* Now lets remove it from the list of ALL associations in the EP */
5687 LIST_REMOVE(stcb, sctp_tcblist);
5688 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5689 SCTP_INP_INCR_REF(inp);
5690 SCTP_INP_WUNLOCK(inp);
5691 }
5692 /* pull from vtag hash */
5693 LIST_REMOVE(stcb, sctp_asocs);
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005694 sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport);
tuexendd729232011-11-01 23:04:43 +00005695
5696 /* Now restop the timers to be sure
5697 * this is paranoia at is finest!
5698 */
Michael Tuexen5f976392020-02-11 21:20:07 +01005699 sctp_stop_association_timers(stcb, true);
tuexendd729232011-11-01 23:04:43 +00005700
tuexendd729232011-11-01 23:04:43 +00005701 /*
5702 * The chunk lists and such SHOULD be empty but we check them just
5703 * in case.
5704 */
5705 /* anything on the wheel needs to be removed */
5706 for (i = 0; i < asoc->streamoutcnt; i++) {
5707 struct sctp_stream_out *outs;
5708
5709 outs = &asoc->strmout[i];
5710 /* now clean up any chunks here */
5711 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
Michael Tuexenc6ae6c62016-08-07 15:17:05 +02005712 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
tuexendd729232011-11-01 23:04:43 +00005713 TAILQ_REMOVE(&outs->outqueue, sp, next);
Michael Tuexen5e95fea2021-09-21 16:39:42 +02005714 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
tuexen30ca8172012-07-17 13:09:48 +00005715 sctp_free_spbufspace(stcb, asoc, sp);
tuexendd729232011-11-01 23:04:43 +00005716 if (sp->data) {
5717 if (so) {
5718 /* Still an open socket - report */
5719 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
tuexenda53ff02012-05-14 09:00:59 +00005720 0, (void *)sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005721 }
5722 if (sp->data) {
5723 sctp_m_freem(sp->data);
5724 sp->data = NULL;
5725 sp->tail_mbuf = NULL;
tuexen30ca8172012-07-17 13:09:48 +00005726 sp->length = 0;
tuexendd729232011-11-01 23:04:43 +00005727 }
5728 }
5729 if (sp->net) {
5730 sctp_free_remote_addr(sp->net);
5731 sp->net = NULL;
5732 }
tuexen30ca8172012-07-17 13:09:48 +00005733 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005734 }
5735 }
5736 /*sa_ignore FREED_MEMORY*/
5737 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5738 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5739 SCTP_FREE(strrst, SCTP_M_STRESET);
5740 }
5741 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5742 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5743 if (sq->data) {
5744 sctp_m_freem(sq->data);
5745 sq->data = NULL;
5746 }
5747 sctp_free_remote_addr(sq->whoFrom);
5748 sq->whoFrom = NULL;
5749 sq->stcb = NULL;
5750 /* Free the ctl entry */
Michael Tuexene5001952016-04-17 19:25:27 +02005751 sctp_free_a_readq(stcb, sq);
tuexendd729232011-11-01 23:04:43 +00005752 /*sa_ignore FREED_MEMORY*/
5753 }
5754 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5755 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5756 if (chk->data) {
5757 sctp_m_freem(chk->data);
5758 chk->data = NULL;
5759 }
5760 if (chk->holds_key_ref)
5761 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5762 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5763 SCTP_DECR_CHK_COUNT();
5764 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5765 asoc->free_chunk_cnt--;
5766 /*sa_ignore FREED_MEMORY*/
5767 }
5768 /* pending send queue SHOULD be empty */
5769 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005770 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5771 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen8fcc5142012-11-16 19:46:12 +00005772#ifdef INVARIANTS
5773 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005774 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen8fcc5142012-11-16 19:46:12 +00005775#endif
5776 }
tuexendd729232011-11-01 23:04:43 +00005777 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5778 if (chk->data) {
5779 if (so) {
5780 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005781 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5782 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005783 }
5784 if (chk->data) {
5785 sctp_m_freem(chk->data);
5786 chk->data = NULL;
5787 }
5788 }
5789 if (chk->holds_key_ref)
5790 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5791 if (chk->whoTo) {
5792 sctp_free_remote_addr(chk->whoTo);
5793 chk->whoTo = NULL;
5794 }
5795 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5796 SCTP_DECR_CHK_COUNT();
5797 /*sa_ignore FREED_MEMORY*/
5798 }
5799 /* sent queue SHOULD be empty */
5800 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
t00fcxen8fcc5142012-11-16 19:46:12 +00005801 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005802 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5803 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen9ad90772012-11-07 22:19:57 +00005804#ifdef INVARIANTS
5805 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005806 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen9ad90772012-11-07 22:19:57 +00005807#endif
5808 }
5809 }
tuexendd729232011-11-01 23:04:43 +00005810 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5811 if (chk->data) {
5812 if (so) {
5813 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005814 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5815 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005816 }
5817 if (chk->data) {
5818 sctp_m_freem(chk->data);
5819 chk->data = NULL;
5820 }
5821 }
5822 if (chk->holds_key_ref)
5823 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5824 sctp_free_remote_addr(chk->whoTo);
5825 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5826 SCTP_DECR_CHK_COUNT();
5827 /*sa_ignore FREED_MEMORY*/
5828 }
t00fcxen8fcc5142012-11-16 19:46:12 +00005829#ifdef INVARIANTS
5830 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
5831 if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
5832 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
5833 }
5834 }
5835#endif
tuexendd729232011-11-01 23:04:43 +00005836 /* control queue MAY not be empty */
5837 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5838 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5839 if (chk->data) {
5840 sctp_m_freem(chk->data);
5841 chk->data = NULL;
5842 }
5843 if (chk->holds_key_ref)
5844 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5845 sctp_free_remote_addr(chk->whoTo);
5846 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5847 SCTP_DECR_CHK_COUNT();
5848 /*sa_ignore FREED_MEMORY*/
5849 }
5850 /* ASCONF queue MAY not be empty */
5851 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5852 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5853 if (chk->data) {
5854 sctp_m_freem(chk->data);
5855 chk->data = NULL;
5856 }
5857 if (chk->holds_key_ref)
5858 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5859 sctp_free_remote_addr(chk->whoTo);
5860 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5861 SCTP_DECR_CHK_COUNT();
5862 /*sa_ignore FREED_MEMORY*/
5863 }
tuexendd729232011-11-01 23:04:43 +00005864 if (asoc->mapping_array) {
5865 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5866 asoc->mapping_array = NULL;
5867 }
5868 if (asoc->nr_mapping_array) {
5869 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5870 asoc->nr_mapping_array = NULL;
5871 }
5872 /* the stream outs */
5873 if (asoc->strmout) {
5874 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5875 asoc->strmout = NULL;
5876 }
5877 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5878 if (asoc->strmin) {
tuexendd729232011-11-01 23:04:43 +00005879 for (i = 0; i < asoc->streamincnt; i++) {
Michael Tuexene5001952016-04-17 19:25:27 +02005880 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
5881 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
tuexendd729232011-11-01 23:04:43 +00005882 }
5883 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5884 asoc->strmin = NULL;
5885 }
5886 asoc->streamincnt = 0;
5887 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5888#ifdef INVARIANTS
5889 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5890 panic("no net's left alloc'ed, or list points to itself");
5891 }
5892#endif
5893 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5894 sctp_free_remote_addr(net);
5895 }
5896 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5897 /*sa_ignore FREED_MEMORY*/
5898 sctp_remove_laddr(laddr);
5899 }
5900
5901 /* pending asconf (address) parameters */
5902 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5903 /*sa_ignore FREED_MEMORY*/
5904 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5905 SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5906 }
5907 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5908 /*sa_ignore FREED_MEMORY*/
5909 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5910 if (aack->data != NULL) {
5911 sctp_m_freem(aack->data);
5912 }
5913 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5914 }
5915 /* clean up auth stuff */
5916 if (asoc->local_hmacs)
5917 sctp_free_hmaclist(asoc->local_hmacs);
5918 if (asoc->peer_hmacs)
5919 sctp_free_hmaclist(asoc->peer_hmacs);
5920
5921 if (asoc->local_auth_chunks)
5922 sctp_free_chunklist(asoc->local_auth_chunks);
5923 if (asoc->peer_auth_chunks)
5924 sctp_free_chunklist(asoc->peer_auth_chunks);
5925
5926 sctp_free_authinfo(&asoc->authinfo);
5927
5928 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5929 LIST_REMOVE(shared_key, next);
5930 sctp_free_sharedkey(shared_key);
5931 /*sa_ignore FREED_MEMORY*/
5932 }
5933
5934 /* Insert new items here :> */
5935
5936 /* Get rid of LOCK */
Michael Tuexen31f4eb52020-08-16 15:36:30 +02005937 SCTP_TCB_SEND_UNLOCK(stcb);
t00fcxen2c2ff922013-11-03 14:03:35 +00005938 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005939 SCTP_TCB_LOCK_DESTROY(stcb);
5940 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5941 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5942 SCTP_INP_INFO_WUNLOCK();
5943 SCTP_INP_RLOCK(inp);
5944 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02005945#if defined(__APPLE__) && !defined(__Userspace__)
5946 /* TEMP CODE */
tuexendd729232011-11-01 23:04:43 +00005947 stcb->freed_from_where = from_location;
5948#endif
5949#ifdef SCTP_TRACK_FREED_ASOCS
5950 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5951 /* now clean up the tasoc itself */
5952 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5953 SCTP_DECR_ASOC_COUNT();
5954 } else {
5955 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5956 }
5957#else
5958 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5959 SCTP_DECR_ASOC_COUNT();
5960#endif
5961 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5962 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5963 /* If its NOT the inp_free calling us AND
5964 * sctp_close as been called, we
5965 * call back...
5966 */
5967 SCTP_INP_RUNLOCK(inp);
5968 /* This will start the kill timer (if we are
5969 * the last one) since we hold an increment yet. But
5970 * this is the only safe way to do this
5971 * since otherwise if the socket closes
5972 * at the same time we are here we might
5973 * collide in the cleanup.
5974 */
5975 sctp_inpcb_free(inp,
5976 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5977 SCTP_CALLED_DIRECTLY_NOCMPSET);
5978 SCTP_INP_DECR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00005979 } else {
5980 /* The socket is still open. */
5981 SCTP_INP_DECR_REF(inp);
Michael Tuexenc6c994c2020-05-18 21:50:12 +02005982 SCTP_INP_RUNLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00005983 }
5984 }
tuexendd729232011-11-01 23:04:43 +00005985 /* destroyed the asoc */
5986#ifdef SCTP_LOG_CLOSING
5987 sctp_log_closing(inp, NULL, 11);
5988#endif
5989 return (1);
5990}
5991
tuexendd729232011-11-01 23:04:43 +00005992/*
5993 * determine if a destination is "reachable" based upon the addresses bound
5994 * to the current endpoint (e.g. only v4 or v6 currently bound)
5995 */
5996/*
5997 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5998 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5999 * types bound as its endpoint
6000 */
6001int
6002sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
6003{
6004 struct sctp_inpcb *inp;
6005 int answer;
6006
6007 /*
6008 * No locks here, the TCB, in all cases is already locked and an
6009 * assoc is up. There is either a INP lock by the caller applied (in
6010 * asconf case when deleting an address) or NOT in the HB case,
6011 * however if HB then the INP increment is up and the INP will not
6012 * be removed (on top of the fact that we have a TCB lock). So we
6013 * only want to read the sctp_flags, which is either bound-all or
6014 * not.. no protection needed since once an assoc is up you can't be
6015 * changing your binding.
6016 */
6017 inp = stcb->sctp_ep;
6018 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6019 /* if bound all, destination is not restricted */
6020 /*
6021 * RRS: Question during lock work: Is this correct? If you
6022 * are bound-all you still might need to obey the V4--V6
6023 * flags??? IMO this bound-all stuff needs to be removed!
6024 */
6025 return (1);
6026 }
6027 /* NOTE: all "scope" checks are done when local addresses are added */
6028 switch (destaddr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00006029#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00006030 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00006031 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
tuexendd729232011-11-01 23:04:43 +00006032 break;
tuexen310f1bc2012-07-15 11:20:56 +00006033#endif
6034#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006035 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00006036 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
tuexen46570542012-07-14 20:37:24 +00006037 break;
tuexen310f1bc2012-07-15 11:20:56 +00006038#endif
tuexen5d9b9742012-07-11 21:52:01 +00006039#if defined(__Userspace__)
6040 case AF_CONN:
6041 answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
tuexendd729232011-11-01 23:04:43 +00006042 break;
tuexen5d9b9742012-07-11 21:52:01 +00006043#endif
tuexendd729232011-11-01 23:04:43 +00006044 default:
6045 /* invalid family, so it's unreachable */
6046 answer = 0;
6047 break;
6048 }
6049 return (answer);
6050}
6051
6052/*
6053 * update the inp_vflags on an endpoint
6054 */
6055static void
6056sctp_update_ep_vflag(struct sctp_inpcb *inp)
6057{
6058 struct sctp_laddr *laddr;
6059
6060 /* first clear the flag */
tuexendd729232011-11-01 23:04:43 +00006061 inp->ip_inp.inp.inp_vflag = 0;
tuexendd729232011-11-01 23:04:43 +00006062 /* set the flag based on addresses on the ep list */
6063 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6064 if (laddr->ifa == NULL) {
6065 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02006066 __func__);
tuexendd729232011-11-01 23:04:43 +00006067 continue;
6068 }
6069
6070 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
6071 continue;
6072 }
6073 switch (laddr->ifa->address.sa.sa_family) {
6074#ifdef INET6
6075 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00006076 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
tuexendd729232011-11-01 23:04:43 +00006077 break;
6078#endif
6079#ifdef INET
6080 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00006081 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
tuexendd729232011-11-01 23:04:43 +00006082 break;
6083#endif
tuexen5d9b9742012-07-11 21:52:01 +00006084#if defined(__Userspace__)
6085 case AF_CONN:
6086 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6087 break;
6088#endif
tuexendd729232011-11-01 23:04:43 +00006089 default:
6090 break;
6091 }
6092 }
6093}
6094
6095/*
6096 * Add the address to the endpoint local address list There is nothing to be
6097 * done if we are bound to all addresses
6098 */
6099void
6100sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
6101{
6102 struct sctp_laddr *laddr;
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006103 struct sctp_tcb *stcb;
tuexen63fc0bb2011-12-27 12:24:52 +00006104 int fnd, error = 0;
tuexendd729232011-11-01 23:04:43 +00006105
6106 fnd = 0;
6107
6108 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6109 /* You are already bound to all. You have it already */
6110 return;
6111 }
6112#ifdef INET6
6113 if (ifa->address.sa.sa_family == AF_INET6) {
6114 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6115 /* Can't bind a non-useable addr. */
6116 return;
6117 }
6118 }
6119#endif
6120 /* first, is it already present? */
6121 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6122 if (laddr->ifa == ifa) {
6123 fnd = 1;
6124 break;
6125 }
6126 }
6127
6128 if (fnd == 0) {
6129 /* Not in the ep list */
6130 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
6131 if (error != 0)
6132 return;
6133 inp->laddr_count++;
6134 /* update inp_vflag flags */
6135 switch (ifa->address.sa.sa_family) {
6136#ifdef INET6
6137 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00006138 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
tuexendd729232011-11-01 23:04:43 +00006139 break;
6140#endif
tuexen310f1bc2012-07-15 11:20:56 +00006141#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006142 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00006143 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
tuexendd729232011-11-01 23:04:43 +00006144 break;
6145#endif
tuexen5d9b9742012-07-11 21:52:01 +00006146#if defined(__Userspace__)
6147 case AF_CONN:
6148 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6149 break;
6150#endif
tuexendd729232011-11-01 23:04:43 +00006151 default:
6152 break;
6153 }
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006154 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6155 sctp_add_local_addr_restricted(stcb, ifa);
6156 }
tuexendd729232011-11-01 23:04:43 +00006157 }
6158 return;
6159}
6160
tuexendd729232011-11-01 23:04:43 +00006161/*
6162 * select a new (hopefully reachable) destination net (should only be used
6163 * when we deleted an ep addr that is the only usable source address to reach
6164 * the destination net)
6165 */
6166static void
6167sctp_select_primary_destination(struct sctp_tcb *stcb)
6168{
6169 struct sctp_nets *net;
6170
6171 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6172 /* for now, we'll just pick the first reachable one we find */
6173 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6174 continue;
6175 if (sctp_destination_is_reachable(stcb,
6176 (struct sockaddr *)&net->ro._l_addr)) {
6177 /* found a reachable destination */
6178 stcb->asoc.primary_destination = net;
6179 }
6180 }
6181 /* I can't there from here! ...we're gonna die shortly... */
6182}
6183
tuexendd729232011-11-01 23:04:43 +00006184/*
Michael Tuexen24e6c732016-02-19 12:28:21 +01006185 * Delete the address from the endpoint local address list. There is nothing
tuexendd729232011-11-01 23:04:43 +00006186 * to be done if we are bound to all addresses
6187 */
6188void
6189sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6190{
6191 struct sctp_laddr *laddr;
6192 int fnd;
6193
6194 fnd = 0;
6195 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6196 /* You are already bound to all. You have it already */
6197 return;
6198 }
6199 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6200 if (laddr->ifa == ifa) {
6201 fnd = 1;
6202 break;
6203 }
6204 }
6205 if (fnd && (inp->laddr_count < 2)) {
6206 /* can't delete unless there are at LEAST 2 addresses */
6207 return;
6208 }
6209 if (fnd) {
6210 /*
6211 * clean up any use of this address go through our
6212 * associations and clear any last_used_address that match
6213 * this one for each assoc, see if a new primary_destination
6214 * is needed
6215 */
6216 struct sctp_tcb *stcb;
6217
6218 /* clean up "next_addr_touse" */
6219 if (inp->next_addr_touse == laddr)
6220 /* delete this address */
6221 inp->next_addr_touse = NULL;
6222
6223 /* clean up "last_used_address" */
6224 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6225 struct sctp_nets *net;
Michael Tuexen24e6c732016-02-19 12:28:21 +01006226
tuexendd729232011-11-01 23:04:43 +00006227 SCTP_TCB_LOCK(stcb);
6228 if (stcb->asoc.last_used_address == laddr)
6229 /* delete this address */
6230 stcb->asoc.last_used_address = NULL;
6231 /* Now spin through all the nets and purge any ref to laddr */
6232 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
Michael Tuexen24e6c732016-02-19 12:28:21 +01006233 if (net->ro._s_addr == laddr->ifa) {
tuexendd729232011-11-01 23:04:43 +00006234 /* Yep, purge src address selected */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006235#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02006236 RO_NHFREE(&net->ro);
6237#else
tuexendd729232011-11-01 23:04:43 +00006238 sctp_rtentry_t *rt;
6239
6240 /* delete this address if cached */
6241 rt = net->ro.ro_rt;
6242 if (rt != NULL) {
6243 RTFREE(rt);
6244 net->ro.ro_rt = NULL;
6245 }
Michael Tuexena0515422020-05-18 00:47:04 +02006246#endif
tuexendd729232011-11-01 23:04:43 +00006247 sctp_free_ifa(net->ro._s_addr);
6248 net->ro._s_addr = NULL;
6249 net->src_addr_selected = 0;
6250 }
6251 }
6252 SCTP_TCB_UNLOCK(stcb);
6253 } /* for each tcb */
6254 /* remove it from the ep list */
6255 sctp_remove_laddr(laddr);
6256 inp->laddr_count--;
6257 /* update inp_vflag flags */
6258 sctp_update_ep_vflag(inp);
6259 }
6260 return;
6261}
6262
6263/*
6264 * Add the address to the TCB local address restricted list.
6265 * This is a "pending" address list (eg. addresses waiting for an
6266 * ASCONF-ACK response) and cannot be used as a valid source address.
6267 */
6268void
6269sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6270{
tuexendd729232011-11-01 23:04:43 +00006271 struct sctp_laddr *laddr;
6272 struct sctpladdr *list;
6273
6274 /*
6275 * Assumes TCB is locked.. and possibly the INP. May need to
6276 * confirm/fix that if we need it and is not the case.
6277 */
6278 list = &stcb->asoc.sctp_restricted_addrs;
6279
tuexendd729232011-11-01 23:04:43 +00006280#ifdef INET6
6281 if (ifa->address.sa.sa_family == AF_INET6) {
6282 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6283 /* Can't bind a non-existent addr. */
6284 return;
6285 }
6286 }
6287#endif
6288 /* does the address already exist? */
6289 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6290 if (laddr->ifa == ifa) {
6291 return;
6292 }
6293 }
6294
6295 /* add to the list */
6296 (void)sctp_insert_laddr(list, ifa, 0);
6297 return;
6298}
6299
6300/*
tuexendd729232011-11-01 23:04:43 +00006301 * Remove a local address from the TCB local address restricted list
6302 */
6303void
6304sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6305{
6306 struct sctp_inpcb *inp;
6307 struct sctp_laddr *laddr;
6308
6309 /*
6310 * This is called by asconf work. It is assumed that a) The TCB is
6311 * locked and b) The INP is locked. This is true in as much as I can
6312 * trace through the entry asconf code where I did these locks.
6313 * Again, the ASCONF code is a bit different in that it does lock
6314 * the INP during its work often times. This must be since we don't
6315 * want other proc's looking up things while what they are looking
6316 * up is changing :-D
6317 */
6318
6319 inp = stcb->sctp_ep;
6320 /* if subset bound and don't allow ASCONF's, can't delete last */
6321 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6322 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6323 if (stcb->sctp_ep->laddr_count < 2) {
6324 /* can't delete last address */
6325 return;
6326 }
6327 }
6328 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6329 /* remove the address if it exists */
6330 if (laddr->ifa == NULL)
6331 continue;
6332 if (laddr->ifa == ifa) {
6333 sctp_remove_laddr(laddr);
6334 return;
6335 }
6336 }
6337
6338 /* address not found! */
6339 return;
6340}
6341
Michael Tuexen5be0c252020-06-13 00:53:56 +02006342#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006343/* sysctl */
6344static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6345static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006346#endif
tuexendd729232011-11-01 23:04:43 +00006347
Michael Tuexen5be0c252020-06-13 00:53:56 +02006348#if defined(__FreeBSD__) && !defined(__Userspace__)
6349#if defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexen63fc0bb2011-12-27 12:24:52 +00006350struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6351int *sctp_cpuarry = NULL;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006352
tuexendd729232011-11-01 23:04:43 +00006353void
6354sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6355{
6356 /* Queue a packet to a processor for the specified core */
6357 struct sctp_mcore_queue *qent;
6358 struct sctp_mcore_ctrl *wkq;
tuexen63fc0bb2011-12-27 12:24:52 +00006359 int need_wake = 0;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006360
tuexendd729232011-11-01 23:04:43 +00006361 if (sctp_mcore_workers == NULL) {
6362 /* Something went way bad during setup */
6363 sctp_input_with_port(m, off, 0);
6364 return;
6365 }
6366 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6367 (sizeof(struct sctp_mcore_queue)),
6368 SCTP_M_MCORE);
6369 if (qent == NULL) {
6370 /* This is trouble */
6371 sctp_input_with_port(m, off, 0);
6372 return;
6373 }
tuexendd729232011-11-01 23:04:43 +00006374 qent->vn = curvnet;
tuexendd729232011-11-01 23:04:43 +00006375 qent->m = m;
6376 qent->off = off;
6377 qent->v6 = 0;
6378 wkq = &sctp_mcore_workers[cpu_to_use];
6379 SCTP_MCORE_QLOCK(wkq);
6380
6381 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6382 if (wkq->running == 0) {
6383 need_wake = 1;
6384 }
6385 SCTP_MCORE_QUNLOCK(wkq);
tuexen63fc0bb2011-12-27 12:24:52 +00006386 if (need_wake) {
tuexendd729232011-11-01 23:04:43 +00006387 wakeup(&wkq->running);
6388 }
6389}
6390
6391static void
6392sctp_mcore_thread(void *arg)
6393{
6394
6395 struct sctp_mcore_ctrl *wkq;
6396 struct sctp_mcore_queue *qent;
6397
6398 wkq = (struct sctp_mcore_ctrl *)arg;
6399 struct mbuf *m;
6400 int off, v6;
6401
6402 /* Wait for first tickle */
6403 SCTP_MCORE_LOCK(wkq);
6404 wkq->running = 0;
6405 msleep(&wkq->running,
6406 &wkq->core_mtx,
6407 0, "wait for pkt", 0);
6408 SCTP_MCORE_UNLOCK(wkq);
6409
6410 /* Bind to our cpu */
6411 thread_lock(curthread);
6412 sched_bind(curthread, wkq->cpuid);
6413 thread_unlock(curthread);
6414
6415 /* Now lets start working */
6416 SCTP_MCORE_LOCK(wkq);
6417 /* Now grab lock and go */
tuexen9784e9a2011-12-18 13:04:23 +00006418 for (;;) {
tuexendd729232011-11-01 23:04:43 +00006419 SCTP_MCORE_QLOCK(wkq);
6420 skip_sleep:
6421 wkq->running = 1;
6422 qent = TAILQ_FIRST(&wkq->que);
6423 if (qent) {
6424 TAILQ_REMOVE(&wkq->que, qent, next);
6425 SCTP_MCORE_QUNLOCK(wkq);
tuexendd729232011-11-01 23:04:43 +00006426 CURVNET_SET(qent->vn);
tuexendd729232011-11-01 23:04:43 +00006427 m = qent->m;
6428 off = qent->off;
6429 v6 = qent->v6;
6430 SCTP_FREE(qent, SCTP_M_MCORE);
6431 if (v6 == 0) {
6432 sctp_input_with_port(m, off, 0);
6433 } else {
tuexencb5fe8d2012-05-04 09:50:27 +00006434 SCTP_PRINTF("V6 not yet supported\n");
tuexendd729232011-11-01 23:04:43 +00006435 sctp_m_freem(m);
6436 }
tuexendd729232011-11-01 23:04:43 +00006437 CURVNET_RESTORE();
tuexendd729232011-11-01 23:04:43 +00006438 SCTP_MCORE_QLOCK(wkq);
6439 }
6440 wkq->running = 0;
6441 if (!TAILQ_EMPTY(&wkq->que)) {
6442 goto skip_sleep;
6443 }
6444 SCTP_MCORE_QUNLOCK(wkq);
6445 msleep(&wkq->running,
6446 &wkq->core_mtx,
6447 0, "wait for pkt", 0);
tuexen63fc0bb2011-12-27 12:24:52 +00006448 }
tuexendd729232011-11-01 23:04:43 +00006449}
6450
6451static void
6452sctp_startup_mcore_threads(void)
6453{
6454 int i, cpu;
6455
6456 if (mp_ncpus == 1)
6457 return;
6458
6459 if (sctp_mcore_workers != NULL) {
6460 /* Already been here in some previous
6461 * vnet?
6462 */
6463 return;
6464 }
6465 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6466 ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6467 SCTP_M_MCORE);
6468 if (sctp_mcore_workers == NULL) {
6469 /* TSNH I hope */
6470 return;
6471 }
tuexen09631162012-04-18 11:23:02 +00006472 memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
tuexendd729232011-11-01 23:04:43 +00006473 sizeof(struct sctp_mcore_ctrl)));
6474 /* Init the structures */
tuexen63fc0bb2011-12-27 12:24:52 +00006475 for (i = 0; i<=mp_maxid; i++) {
tuexendd729232011-11-01 23:04:43 +00006476 TAILQ_INIT(&sctp_mcore_workers[i].que);
6477 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6478 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6479 sctp_mcore_workers[i].cpuid = i;
6480 }
6481 if (sctp_cpuarry == NULL) {
6482 SCTP_MALLOC(sctp_cpuarry, int *,
6483 (mp_ncpus * sizeof(int)),
6484 SCTP_M_MCORE);
tuexen63fc0bb2011-12-27 12:24:52 +00006485 i = 0;
tuexendd729232011-11-01 23:04:43 +00006486 CPU_FOREACH(cpu) {
6487 sctp_cpuarry[i] = cpu;
6488 i++;
6489 }
6490 }
tuexendd729232011-11-01 23:04:43 +00006491 /* Now start them all */
6492 CPU_FOREACH(cpu) {
tuexendd729232011-11-01 23:04:43 +00006493 (void)kproc_create(sctp_mcore_thread,
6494 (void *)&sctp_mcore_workers[cpu],
6495 &sctp_mcore_workers[cpu].thread_proc,
Michael Tuexenad4f4f02021-03-21 16:54:26 +01006496 0,
tuexendd729232011-11-01 23:04:43 +00006497 SCTP_KTHREAD_PAGES,
6498 SCTP_MCORE_NAME);
tuexendd729232011-11-01 23:04:43 +00006499 }
6500}
6501#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006502#endif
6503
6504#if defined(__FreeBSD__) && !defined(__Userspace__)
6505#if defined(SCTP_NOT_YET)
tuexendd729232011-11-01 23:04:43 +00006506static struct mbuf *
6507sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6508{
6509 struct ip *ip;
6510 struct sctphdr *sh;
6511 int offset;
6512 uint32_t flowid, tag;
tuexen76eb2092012-04-01 16:35:28 +00006513
tuexendd729232011-11-01 23:04:43 +00006514 /*
6515 * No flow id built by lower layers fix it so we
6516 * create one.
6517 */
6518 ip = mtod(m, struct ip *);
tuexen664967a2012-06-28 16:24:36 +00006519 offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
tuexendd729232011-11-01 23:04:43 +00006520 if (SCTP_BUF_LEN(m) < offset) {
tuexen664967a2012-06-28 16:24:36 +00006521 if ((m = m_pullup(m, offset)) == NULL) {
tuexendd729232011-11-01 23:04:43 +00006522 SCTP_STAT_INCR(sctps_hdrops);
6523 return (NULL);
6524 }
6525 ip = mtod(m, struct ip *);
6526 }
6527 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
tuexendd729232011-11-01 23:04:43 +00006528 tag = htonl(sh->v_tag);
6529 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6530 m->m_pkthdr.flowid = flowid;
Michael Tuexena5089952020-06-09 23:11:08 +02006531 /* FIX ME */
tuexendd729232011-11-01 23:04:43 +00006532 m->m_flags |= M_FLOWID;
6533 return (m);
6534}
tuexendd729232011-11-01 23:04:43 +00006535
Michael Tuexen5be0c252020-06-13 00:53:56 +02006536#endif
6537#endif
tuexendd729232011-11-01 23:04:43 +00006538void
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006539#if defined(__Userspace__)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006540sctp_pcb_init(int start_threads)
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006541#else
6542sctp_pcb_init(void)
6543#endif
tuexendd729232011-11-01 23:04:43 +00006544{
6545 /*
6546 * SCTP initialization for the PCB structures should be called by
Michael Tuexen34488e72016-05-03 22:11:59 +02006547 * the sctp_init() function.
tuexendd729232011-11-01 23:04:43 +00006548 */
6549 int i;
6550 struct timeval tv;
tuexen6019b072011-12-15 18:38:03 +00006551
tuexendd729232011-11-01 23:04:43 +00006552 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6553 /* error I was called twice */
6554 return;
6555 }
6556 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6557
Michael Tuexencb2878b2015-11-13 12:59:40 +01006558#if defined(SCTP_PROCESS_LEVEL_LOCKS)
Michael Tuexeneb611572020-06-13 17:27:02 +02006559#if !defined(_WIN32)
Michael Tuexencb2878b2015-11-13 12:59:40 +01006560 pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr));
Paul-Louis Ageneau9d6b99b2021-10-07 22:30:51 +02006561 pthread_rwlockattr_init(&SCTP_BASE_VAR(rwlock_attr));
Michael Tuexencb2878b2015-11-13 12:59:40 +01006562#ifdef INVARIANTS
6563 pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK);
6564#endif
6565#endif
6566#endif
tuexendd729232011-11-01 23:04:43 +00006567#if defined(SCTP_LOCAL_TRACE_BUF)
Michael Tuexeneb611572020-06-13 17:27:02 +02006568#if defined(_WIN32) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006569 if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02006570 memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006571 }
6572#else
Michael Tuexend0853302017-07-19 17:01:31 +02006573 memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006574#endif
6575#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006576#if defined(__FreeBSD__) && !defined(__Userspace__)
6577#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexendd729232011-11-01 23:04:43 +00006578 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6579 ((mp_maxid+1) * sizeof(struct sctpstat)),
6580 SCTP_M_MCORE);
6581#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006582#endif
tuexendd729232011-11-01 23:04:43 +00006583 (void)SCTP_GETTIME_TIMEVAL(&tv);
Michael Tuexen5be0c252020-06-13 00:53:56 +02006584#if defined(__FreeBSD__) && !defined(__Userspace__)
6585#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
Michael Tuexend0853302017-07-19 17:01:31 +02006586 memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1));
tuexendd729232011-11-01 23:04:43 +00006587 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6588 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6589#else
Michael Tuexend0853302017-07-19 17:01:31 +02006590 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
tuexendd729232011-11-01 23:04:43 +00006591 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6592 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6593#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006594#else
6595 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
6596 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6597 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6598#endif
tuexendd729232011-11-01 23:04:43 +00006599 /* init the empty list of (All) Endpoints */
6600 LIST_INIT(&SCTP_BASE_INFO(listhead));
Michael Tuexen5be0c252020-06-13 00:53:56 +02006601#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006602 LIST_INIT(&SCTP_BASE_INFO(inplisthead));
t00fcxenbc766ab2014-01-03 19:29:31 +00006603#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6604 SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead);
6605 SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init();
6606 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6607 SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6608 SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init();
6609 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6610#else
6611 SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead);
6612 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init();
6613 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6614 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6615 SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init();
6616 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6617#endif
6618#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6619 SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc;
6620 in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo));
6621#endif
tuexendd729232011-11-01 23:04:43 +00006622#endif
6623
tuexendd729232011-11-01 23:04:43 +00006624 /* init the hash table of endpoints */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006625#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006626 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6627 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6628 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
tuexendd729232011-11-01 23:04:43 +00006629#endif
6630 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6631 &SCTP_BASE_INFO(hashasocmark));
6632 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6633 &SCTP_BASE_INFO(hashmark));
6634 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6635 &SCTP_BASE_INFO(hashtcpmark));
6636 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
tuexendd729232011-11-01 23:04:43 +00006637 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6638 &SCTP_BASE_INFO(hashvrfmark));
6639
6640 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6641 &SCTP_BASE_INFO(vrf_ifn_hashmark));
6642 /* init the zones */
6643 /*
6644 * FIX ME: Should check for NULL returns, but if it does fail we are
6645 * doomed to panic anyways... add later maybe.
6646 */
6647 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6648 sizeof(struct sctp_inpcb), maxsockets);
6649
6650 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6651 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6652
6653 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6654 sizeof(struct sctp_laddr),
6655 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6656
6657 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6658 sizeof(struct sctp_nets),
6659 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6660
6661 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6662 sizeof(struct sctp_tmit_chunk),
6663 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6664
6665 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6666 sizeof(struct sctp_queued_to_read),
6667 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6668
6669 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6670 sizeof(struct sctp_stream_queue_pending),
6671 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6672
6673 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6674 sizeof(struct sctp_asconf),
6675 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6676
6677 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6678 sizeof(struct sctp_asconf_ack),
6679 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6680
tuexendd729232011-11-01 23:04:43 +00006681 /* Master Lock INIT for info structure */
tuexendd729232011-11-01 23:04:43 +00006682 SCTP_INP_INFO_LOCK_INIT();
6683 SCTP_STATLOG_INIT_LOCK();
6684
6685 SCTP_IPI_COUNT_INIT();
6686 SCTP_IPI_ADDR_INIT();
6687#ifdef SCTP_PACKET_LOGGING
6688 SCTP_IP_PKTLOG_INIT();
6689#endif
6690 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6691
6692 SCTP_WQ_ADDR_INIT();
6693 /* not sure if we need all the counts */
6694 SCTP_BASE_INFO(ipi_count_ep) = 0;
6695 /* assoc/tcb zone info */
6696 SCTP_BASE_INFO(ipi_count_asoc) = 0;
6697 /* local addrlist zone info */
6698 SCTP_BASE_INFO(ipi_count_laddr) = 0;
6699 /* remote addrlist zone info */
6700 SCTP_BASE_INFO(ipi_count_raddr) = 0;
6701 /* chunk info */
6702 SCTP_BASE_INFO(ipi_count_chunk) = 0;
6703
6704 /* socket queue zone info */
6705 SCTP_BASE_INFO(ipi_count_readq) = 0;
6706
6707 /* stream out queue cont */
6708 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6709
6710 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6711 SCTP_BASE_INFO(ipi_free_chunks) = 0;
6712
6713 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6714
6715 /* Init the TIMEWAIT list */
6716 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6717 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6718 }
tuexendd729232011-11-01 23:04:43 +00006719#if defined(SCTP_PROCESS_LEVEL_LOCKS)
Michael Tuexeneb611572020-06-13 17:27:02 +02006720#if defined(_WIN32)
tuexen6019b072011-12-15 18:38:03 +00006721 InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6722#else
tuexendd729232011-11-01 23:04:43 +00006723 (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6724#endif
tuexen6019b072011-12-15 18:38:03 +00006725#endif
tuexendd729232011-11-01 23:04:43 +00006726 sctp_startup_iterator();
6727
Michael Tuexen5be0c252020-06-13 00:53:56 +02006728#if defined(__FreeBSD__) && !defined(__Userspace__)
6729#if defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexendd729232011-11-01 23:04:43 +00006730 sctp_startup_mcore_threads();
6731#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006732#endif
tuexendd729232011-11-01 23:04:43 +00006733
tuexendd729232011-11-01 23:04:43 +00006734 /*
6735 * INIT the default VRF which for BSD is the only one, other O/S's
6736 * may have more. But initially they must start with one and then
6737 * add the VRF's as addresses are added.
6738 */
6739 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
Michael Tuexen5be0c252020-06-13 00:53:56 +02006740#if defined(__FreeBSD__) && !defined(__Userspace__) && defined(SCTP_NOT_YET)
tuexendd729232011-11-01 23:04:43 +00006741 if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
tuexencb5fe8d2012-05-04 09:50:27 +00006742 SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
tuexendd729232011-11-01 23:04:43 +00006743 }
6744#endif
6745#if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6746 /* allocate the lock for the callout/timer queue */
6747 SCTP_TIMERQ_LOCK_INIT();
6748 TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6749#endif
6750#if defined(__Userspace__)
Michael Tuexen2f6478e2017-03-02 09:57:46 +01006751 mbuf_initialize(NULL);
tuexendd729232011-11-01 23:04:43 +00006752 atomic_init();
tuexenc4178972012-07-15 20:40:54 +00006753#if defined(INET) || defined(INET6)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006754 if (start_threads)
6755 recv_thread_init();
tuexendd729232011-11-01 23:04:43 +00006756#endif
tuexenc4178972012-07-15 20:40:54 +00006757#endif
tuexendd729232011-11-01 23:04:43 +00006758}
6759
6760/*
6761 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6762 */
6763void
6764sctp_pcb_finish(void)
6765{
6766 struct sctp_vrflist *vrf_bucket;
6767 struct sctp_vrf *vrf, *nvrf;
6768 struct sctp_ifn *ifn, *nifn;
6769 struct sctp_ifa *ifa, *nifa;
6770 struct sctpvtaghead *chain;
6771 struct sctp_tagblock *twait_block, *prev_twait_block;
6772 struct sctp_laddr *wi, *nwi;
6773 int i;
t00fcxenc7932282013-11-16 14:57:41 +00006774 struct sctp_iterator *it, *nit;
Michael Tuexen20785d42015-11-13 12:48:35 +01006775
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006776 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6777 SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
6778 return;
6779 }
6780 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006781#if !(defined(__FreeBSD__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00006782 /* Notify the iterator to exit. */
6783 SCTP_IPI_ITERATOR_WQ_LOCK();
6784 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6785 sctp_wakeup_iterator();
6786 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6787#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006788#if defined(__APPLE__) && !defined(__Userspace__)
t00fcxenbc766ab2014-01-03 19:29:31 +00006789#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6790 in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo));
6791#endif
tuexendd729232011-11-01 23:04:43 +00006792 SCTP_IPI_ITERATOR_WQ_LOCK();
6793 do {
6794 msleep(&sctp_it_ctl.iterator_flags,
6795 sctp_it_ctl.ipi_iterator_wq_mtx,
6796 0, "waiting_for_work", 0);
6797 } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6798 thread_deallocate(sctp_it_ctl.thread_proc);
6799 SCTP_IPI_ITERATOR_WQ_UNLOCK();
tuexendd729232011-11-01 23:04:43 +00006800#endif
Michael Tuexeneb611572020-06-13 17:27:02 +02006801#if defined(_WIN32) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006802 if (sctp_it_ctl.iterator_thread_obj != NULL) {
6803 NTSTATUS status = STATUS_SUCCESS;
6804
6805 KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6806 status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6807 Executive,
6808 KernelMode,
6809 FALSE,
6810 NULL);
6811 ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6812 }
6813#endif
t00fcxen92a5ac42013-11-16 12:58:54 +00006814#if defined(__Userspace__)
Michael Tuexen3212d542020-03-16 12:30:04 +01006815 if (SCTP_BASE_VAR(iterator_thread_started)) {
Michael Tuexeneb611572020-06-13 17:27:02 +02006816#if defined(_WIN32)
t00fcxen92a5ac42013-11-16 12:58:54 +00006817 WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE);
6818 CloseHandle(sctp_it_ctl.thread_proc);
6819 sctp_it_ctl.thread_proc = NULL;
6820#else
6821 pthread_join(sctp_it_ctl.thread_proc, NULL);
6822 sctp_it_ctl.thread_proc = 0;
6823#endif
6824 }
6825#endif
6826#if defined(SCTP_PROCESS_LEVEL_LOCKS)
Michael Tuexeneb611572020-06-13 17:27:02 +02006827#if defined(_WIN32)
t00fcxen92a5ac42013-11-16 12:58:54 +00006828 DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup);
6829#else
6830 pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup);
Michael Tuexen89e15a72015-11-13 12:42:55 +01006831 pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr));
Paul-Louis Ageneau9d6b99b2021-10-07 22:30:51 +02006832 pthread_rwlockattr_destroy(&SCTP_BASE_VAR(rwlock_attr));
t00fcxen92a5ac42013-11-16 12:58:54 +00006833#endif
6834#endif
t00fcxen9232a652013-11-30 09:34:24 +00006835 /* In FreeBSD the iterator thread never exits
6836 * but we do clean up.
6837 * The only way FreeBSD reaches here is if we have VRF's
6838 * but we still add the ifdef to make it compile on old versions.
6839 */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006840#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006841retry:
6842#endif
t00fcxen9232a652013-11-30 09:34:24 +00006843 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen5be0c252020-06-13 00:53:56 +02006844#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006845 /*
6846 * sctp_iterator_worker() might be working on an it entry without
6847 * holding the lock. We won't find it on the list either and
6848 * continue and free/destroy it. While holding the lock, spin, to
6849 * avoid the race condition as sctp_iterator_worker() will have to
Michael Tuexen5ac79522020-06-06 23:32:08 +02006850 * wait to re-acquire the lock.
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006851 */
6852 if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
6853 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6854 SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
6855 "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
6856 DELAY(10);
6857 goto retry;
6858 }
6859#endif
t00fcxen9232a652013-11-30 09:34:24 +00006860 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
Michael Tuexen5be0c252020-06-13 00:53:56 +02006861#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen9232a652013-11-30 09:34:24 +00006862 if (it->vn != curvnet) {
6863 continue;
6864 }
6865#endif
6866 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6867 if (it->function_atend != NULL) {
6868 (*it->function_atend) (it->pointer, it->val);
6869 }
6870 SCTP_FREE(it,SCTP_M_ITER);
6871 }
6872 SCTP_IPI_ITERATOR_WQ_UNLOCK();
Michael Tuexen5be0c252020-06-13 00:53:56 +02006873#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen9232a652013-11-30 09:34:24 +00006874 SCTP_ITERATOR_LOCK();
6875 if ((sctp_it_ctl.cur_it) &&
6876 (sctp_it_ctl.cur_it->vn == curvnet)) {
6877 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6878 }
6879 SCTP_ITERATOR_UNLOCK();
6880#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006881#if !(defined(__FreeBSD__) && !defined(__Userspace__))
t00fcxen92a5ac42013-11-16 12:58:54 +00006882 SCTP_IPI_ITERATOR_WQ_DESTROY();
t00fcxen8dd95b82014-07-11 20:41:28 +00006883 SCTP_ITERATOR_LOCK_DESTROY();
t00fcxenc7932282013-11-16 14:57:41 +00006884#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006885 SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
tuexendd729232011-11-01 23:04:43 +00006886 SCTP_WQ_ADDR_LOCK();
6887 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6888 LIST_REMOVE(wi, sctp_nxt_addr);
6889 SCTP_DECR_LADDR_COUNT();
t00fcxen9892b7f2014-09-06 20:06:39 +00006890 if (wi->action == SCTP_DEL_IP_ADDRESS) {
6891 SCTP_FREE(wi->ifa, SCTP_M_IFA);
6892 }
tuexendd729232011-11-01 23:04:43 +00006893 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6894 }
6895 SCTP_WQ_ADDR_UNLOCK();
tuexen09631162012-04-18 11:23:02 +00006896
tuexendd729232011-11-01 23:04:43 +00006897 /*
6898 * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6899 * is destroyed first).
6900 */
Michael Tuexen2c5953e2020-07-17 17:25:19 +02006901 SCTP_IPI_ADDR_WLOCK();
tuexendd729232011-11-01 23:04:43 +00006902 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6903 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6904 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6905 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6906 /* free the ifa */
6907 LIST_REMOVE(ifa, next_bucket);
6908 LIST_REMOVE(ifa, next_ifa);
6909 SCTP_FREE(ifa, SCTP_M_IFA);
6910 }
6911 /* free the ifn */
6912 LIST_REMOVE(ifn, next_bucket);
6913 LIST_REMOVE(ifn, next_ifn);
6914 SCTP_FREE(ifn, SCTP_M_IFN);
6915 }
6916 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6917 /* free the vrf */
6918 LIST_REMOVE(vrf, next_vrf);
6919 SCTP_FREE(vrf, SCTP_M_VRF);
6920 }
Michael Tuexen2c5953e2020-07-17 17:25:19 +02006921 SCTP_IPI_ADDR_WUNLOCK();
tuexendd729232011-11-01 23:04:43 +00006922 /* free the vrf hashes */
6923 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6924 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
tuexendd729232011-11-01 23:04:43 +00006925
6926 /* free the TIMEWAIT list elements malloc'd in the function
6927 * sctp_add_vtag_to_timewait()...
6928 */
6929 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6930 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6931 if (!LIST_EMPTY(chain)) {
6932 prev_twait_block = NULL;
6933 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6934 if (prev_twait_block) {
6935 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6936 }
6937 prev_twait_block = twait_block;
6938 }
6939 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6940 }
6941 }
6942
6943 /* free the locks and mutexes */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006944#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006945 SCTP_TIMERQ_LOCK_DESTROY();
6946#endif
6947#ifdef SCTP_PACKET_LOGGING
6948 SCTP_IP_PKTLOG_DESTROY();
6949#endif
6950 SCTP_IPI_ADDR_DESTROY();
Michael Tuexen5be0c252020-06-13 00:53:56 +02006951#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006952 SCTP_IPI_COUNT_DESTROY();
6953#endif
6954 SCTP_STATLOG_DESTROY();
tuexendd729232011-11-01 23:04:43 +00006955 SCTP_INP_INFO_LOCK_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006956
tuexendd729232011-11-01 23:04:43 +00006957 SCTP_WQ_ADDR_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006958
Michael Tuexen5be0c252020-06-13 00:53:56 +02006959#if defined(__APPLE__) && !defined(__Userspace__)
t00fcxenbc766ab2014-01-03 19:29:31 +00006960#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6961 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6962 lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp);
6963 lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6964#else
6965 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6966 lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp);
6967 lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6968#endif
tuexendd729232011-11-01 23:04:43 +00006969#endif
6970#if defined(__Userspace__)
6971 SCTP_TIMERQ_LOCK_DESTROY();
6972 SCTP_ZONE_DESTROY(zone_mbuf);
6973 SCTP_ZONE_DESTROY(zone_clust);
6974 SCTP_ZONE_DESTROY(zone_ext_refcnt);
6975#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006976 /* Get rid of other stuff too. */
6977 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6978 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6979 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6980 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6981 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6982 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6983
Michael Tuexeneb611572020-06-13 17:27:02 +02006984#if defined(_WIN32) || defined(__FreeBSD__) || defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006985 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6986 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6987 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6988 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6989 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6990 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6991 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6992 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6993 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6994#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006995#if defined(__FreeBSD__) && !defined(__Userspace__)
6996#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexen09631162012-04-18 11:23:02 +00006997 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
tuexendd729232011-11-01 23:04:43 +00006998#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006999#endif
tuexendd729232011-11-01 23:04:43 +00007000}
7001
tuexendd729232011-11-01 23:04:43 +00007002int
7003sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
tuexen664967a2012-06-28 16:24:36 +00007004 int offset, int limit,
7005 struct sockaddr *src, struct sockaddr *dst,
Michael Tuexen923f7352016-05-01 23:55:35 +02007006 struct sockaddr *altsa, uint16_t port)
tuexendd729232011-11-01 23:04:43 +00007007{
7008 /*
7009 * grub through the INIT pulling addresses and loading them to the
7010 * nets structure in the asoc. The from address in the mbuf should
7011 * also be loaded (if it is not already). This routine can be called
7012 * with either INIT or INIT-ACK's as long as the m points to the IP
7013 * packet and the offset points to the beginning of the parameters.
7014 */
tuexen63fc0bb2011-12-27 12:24:52 +00007015 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00007016 struct sctp_nets *net, *nnet, *net_tmp;
Michael Tuexen473225f2017-06-23 23:05:43 +02007017 struct sctp_paramhdr *phdr, param_buf;
tuexendd729232011-11-01 23:04:43 +00007018 struct sctp_tcb *stcb_tmp;
7019 uint16_t ptype, plen;
7020 struct sockaddr *sa;
tuexendd729232011-11-01 23:04:43 +00007021 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
7022 struct sctp_auth_random *p_random = NULL;
7023 uint16_t random_len = 0;
7024 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
7025 struct sctp_auth_hmac_algo *hmacs = NULL;
7026 uint16_t hmacs_len = 0;
tuexen63fc0bb2011-12-27 12:24:52 +00007027 uint8_t saw_asconf = 0;
7028 uint8_t saw_asconf_ack = 0;
tuexendd729232011-11-01 23:04:43 +00007029 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
7030 struct sctp_auth_chunk_list *chunks = NULL;
7031 uint16_t num_chunks = 0;
7032 sctp_key_t *new_key;
7033 uint32_t keylen;
7034 int got_random = 0, got_hmacs = 0, got_chklist = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007035 uint8_t peer_supports_ecn;
7036 uint8_t peer_supports_prsctp;
7037 uint8_t peer_supports_auth;
7038 uint8_t peer_supports_asconf;
7039 uint8_t peer_supports_asconf_ack;
7040 uint8_t peer_supports_reconfig;
7041 uint8_t peer_supports_nrsack;
7042 uint8_t peer_supports_pktdrop;
Michael Tuexene5001952016-04-17 19:25:27 +02007043 uint8_t peer_supports_idata;
tuexendd729232011-11-01 23:04:43 +00007044#ifdef INET
7045 struct sockaddr_in sin;
7046#endif
7047#ifdef INET6
7048 struct sockaddr_in6 sin6;
7049#endif
7050
7051 /* First get the destination address setup too. */
7052#ifdef INET
7053 memset(&sin, 0, sizeof(sin));
7054 sin.sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00007055#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00007056 sin.sin_len = sizeof(sin);
7057#endif
7058 sin.sin_port = stcb->rport;
7059#endif
7060#ifdef INET6
7061 memset(&sin6, 0, sizeof(sin6));
7062 sin6.sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00007063#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00007064 sin6.sin6_len = sizeof(struct sockaddr_in6);
7065#endif
7066 sin6.sin6_port = stcb->rport;
7067#endif
tuexen664967a2012-06-28 16:24:36 +00007068 if (altsa) {
7069 sa = altsa;
7070 } else {
7071 sa = src;
tuexendd729232011-11-01 23:04:43 +00007072 }
Michael Tuexene5001952016-04-17 19:25:27 +02007073 peer_supports_idata = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007074 peer_supports_ecn = 0;
7075 peer_supports_prsctp = 0;
7076 peer_supports_auth = 0;
7077 peer_supports_asconf = 0;
Michael Tuexen4544fa22020-10-06 13:35:42 +02007078 peer_supports_asconf_ack = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007079 peer_supports_reconfig = 0;
7080 peer_supports_nrsack = 0;
7081 peer_supports_pktdrop = 0;
tuexendd729232011-11-01 23:04:43 +00007082 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7083 /* mark all addresses that we have currently on the list */
7084 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
7085 }
7086 /* does the source address already exist? if so skip it */
tuexen63fc0bb2011-12-27 12:24:52 +00007087 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00007088 atomic_add_int(&stcb->asoc.refcnt, 1);
tuexen664967a2012-06-28 16:24:36 +00007089 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
Michael Tuexena106ab82021-11-26 14:03:15 +01007090 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00007091
7092 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
7093 /* we must add the source address */
7094 /* no scope set here since we have a tcb already. */
7095 switch (sa->sa_family) {
7096#ifdef INET
7097 case AF_INET:
t00fcxend0ad16b2013-02-09 18:34:24 +00007098 if (stcb->asoc.scope.ipv4_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007099 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
tuexendd729232011-11-01 23:04:43 +00007100 return (-1);
7101 }
7102 }
7103 break;
7104#endif
7105#ifdef INET6
7106 case AF_INET6:
t00fcxend0ad16b2013-02-09 18:34:24 +00007107 if (stcb->asoc.scope.ipv6_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007108 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
tuexendd729232011-11-01 23:04:43 +00007109 return (-2);
7110 }
7111 }
7112 break;
7113#endif
tuexen430ca6c2012-07-10 23:36:36 +00007114#if defined(__Userspace__)
7115 case AF_CONN:
t00fcxend0ad16b2013-02-09 18:34:24 +00007116 if (stcb->asoc.scope.conn_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007117 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007118 return (-2);
7119 }
tuexen430ca6c2012-07-10 23:36:36 +00007120 }
7121 break;
7122#endif
tuexendd729232011-11-01 23:04:43 +00007123 default:
7124 break;
7125 }
7126 } else {
7127 if (net_tmp != NULL && stcb_tmp == stcb) {
7128 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
7129 } else if (stcb_tmp != stcb) {
7130 /* It belongs to another association? */
7131 if (stcb_tmp)
7132 SCTP_TCB_UNLOCK(stcb_tmp);
7133 return (-3);
7134 }
7135 }
7136 if (stcb->asoc.state == 0) {
7137 /* the assoc was freed? */
7138 return (-4);
7139 }
tuexendd729232011-11-01 23:04:43 +00007140 /* now we must go through each of the params. */
Michael Tuexen473225f2017-06-23 23:05:43 +02007141 phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007142 while (phdr) {
7143 ptype = ntohs(phdr->param_type);
7144 plen = ntohs(phdr->param_length);
7145 /*
tuexencb5fe8d2012-05-04 09:50:27 +00007146 * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
tuexendd729232011-11-01 23:04:43 +00007147 * (int)plen);
7148 */
7149 if (offset + plen > limit) {
7150 break;
7151 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007152 if (plen < sizeof(struct sctp_paramhdr)) {
tuexendd729232011-11-01 23:04:43 +00007153 break;
7154 }
7155#ifdef INET
7156 if (ptype == SCTP_IPV4_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007157 if (stcb->asoc.scope.ipv4_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007158 struct sctp_ipv4addr_param *p4, p4_buf;
7159
7160 /* ok get the v4 address and check/add */
7161 phdr = sctp_get_next_param(m, offset,
7162 (struct sctp_paramhdr *)&p4_buf,
7163 sizeof(p4_buf));
7164 if (plen != sizeof(struct sctp_ipv4addr_param) ||
7165 phdr == NULL) {
7166 return (-5);
7167 }
7168 p4 = (struct sctp_ipv4addr_param *)phdr;
7169 sin.sin_addr.s_addr = p4->addr;
7170 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7171 /* Skip multi-cast addresses */
7172 goto next_param;
7173 }
7174 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
7175 (sin.sin_addr.s_addr == INADDR_ANY)) {
7176 goto next_param;
7177 }
7178 sa = (struct sockaddr *)&sin;
7179 inp = stcb->sctp_ep;
7180 atomic_add_int(&stcb->asoc.refcnt, 1);
7181 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007182 dst, stcb);
Michael Tuexena106ab82021-11-26 14:03:15 +01007183 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00007184
7185 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7186 inp == NULL) {
7187 /* we must add the source address */
7188 /*
7189 * no scope set since we have a tcb
7190 * already
7191 */
7192
7193 /*
7194 * we must validate the state again
7195 * here
7196 */
7197 add_it_now:
7198 if (stcb->asoc.state == 0) {
7199 /* the assoc was freed? */
7200 return (-7);
7201 }
Michael Tuexen923f7352016-05-01 23:55:35 +02007202 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
tuexendd729232011-11-01 23:04:43 +00007203 return (-8);
7204 }
7205 } else if (stcb_tmp == stcb) {
7206 if (stcb->asoc.state == 0) {
7207 /* the assoc was freed? */
7208 return (-10);
7209 }
7210 if (net != NULL) {
7211 /* clear flag */
7212 net->dest_state &=
7213 ~SCTP_ADDR_NOT_IN_ASSOC;
7214 }
7215 } else {
7216 /*
7217 * strange, address is in another
7218 * assoc? straighten out locks.
7219 */
7220 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007221 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007222 struct mbuf *op_err;
7223 char msg[SCTP_DIAG_INFO_LEN];
7224
tuexendd729232011-11-01 23:04:43 +00007225 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007226 SCTP_SNPRINTF(msg, sizeof(msg),
7227 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007228 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7229 msg);
tuexendd729232011-11-01 23:04:43 +00007230 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tuexen1ade45c2021-07-09 23:32:42 +02007231 stcb_tmp, op_err, false,
Michael Tüxenac142282015-07-28 00:42:03 +02007232 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007233 goto add_it_now;
7234 }
7235 SCTP_TCB_UNLOCK(stcb_tmp);
7236 }
7237
7238 if (stcb->asoc.state == 0) {
7239 /* the assoc was freed? */
7240 return (-12);
7241 }
7242 return (-13);
7243 }
7244 }
7245 } else
7246#endif
7247#ifdef INET6
7248 if (ptype == SCTP_IPV6_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007249 if (stcb->asoc.scope.ipv6_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007250 /* ok get the v6 address and check/add */
7251 struct sctp_ipv6addr_param *p6, p6_buf;
7252
7253 phdr = sctp_get_next_param(m, offset,
7254 (struct sctp_paramhdr *)&p6_buf,
7255 sizeof(p6_buf));
7256 if (plen != sizeof(struct sctp_ipv6addr_param) ||
7257 phdr == NULL) {
7258 return (-14);
7259 }
7260 p6 = (struct sctp_ipv6addr_param *)phdr;
7261 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7262 sizeof(p6->addr));
7263 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7264 /* Skip multi-cast addresses */
7265 goto next_param;
7266 }
7267 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7268 /* Link local make no sense without scope */
7269 goto next_param;
7270 }
7271 sa = (struct sockaddr *)&sin6;
7272 inp = stcb->sctp_ep;
7273 atomic_add_int(&stcb->asoc.refcnt, 1);
7274 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007275 dst, stcb);
Michael Tuexena106ab82021-11-26 14:03:15 +01007276 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00007277 if (stcb_tmp == NULL &&
7278 (inp == stcb->sctp_ep || inp == NULL)) {
7279 /*
7280 * we must validate the state again
7281 * here
7282 */
7283 add_it_now6:
7284 if (stcb->asoc.state == 0) {
7285 /* the assoc was freed? */
7286 return (-16);
7287 }
7288 /*
7289 * we must add the address, no scope
7290 * set
7291 */
Michael Tuexen923f7352016-05-01 23:55:35 +02007292 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
tuexendd729232011-11-01 23:04:43 +00007293 return (-17);
7294 }
7295 } else if (stcb_tmp == stcb) {
7296 /*
7297 * we must validate the state again
7298 * here
7299 */
7300 if (stcb->asoc.state == 0) {
7301 /* the assoc was freed? */
7302 return (-19);
7303 }
7304 if (net != NULL) {
7305 /* clear flag */
7306 net->dest_state &=
7307 ~SCTP_ADDR_NOT_IN_ASSOC;
7308 }
7309 } else {
7310 /*
7311 * strange, address is in another
7312 * assoc? straighten out locks.
7313 */
Michael Tüxenac142282015-07-28 00:42:03 +02007314 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007315 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007316 struct mbuf *op_err;
7317 char msg[SCTP_DIAG_INFO_LEN];
7318
tuexendd729232011-11-01 23:04:43 +00007319 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007320 SCTP_SNPRINTF(msg, sizeof(msg),
7321 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007322 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7323 msg);
tuexendd729232011-11-01 23:04:43 +00007324 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tuexen1ade45c2021-07-09 23:32:42 +02007325 stcb_tmp, op_err, false,
Michael Tüxenac142282015-07-28 00:42:03 +02007326 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007327 goto add_it_now6;
7328 }
Michael Tüxenac142282015-07-28 00:42:03 +02007329 SCTP_TCB_UNLOCK(stcb_tmp);
7330 }
tuexendd729232011-11-01 23:04:43 +00007331 if (stcb->asoc.state == 0) {
7332 /* the assoc was freed? */
7333 return (-21);
7334 }
7335 return (-22);
7336 }
7337 }
7338 } else
7339#endif
7340 if (ptype == SCTP_ECN_CAPABLE) {
t00fcxenaac0f452014-08-12 12:51:45 +00007341 peer_supports_ecn = 1;
tuexendd729232011-11-01 23:04:43 +00007342 } else if (ptype == SCTP_ULP_ADAPTATION) {
7343 if (stcb->asoc.state != SCTP_STATE_OPEN) {
7344 struct sctp_adaptation_layer_indication ai, *aip;
7345
7346 phdr = sctp_get_next_param(m, offset,
7347 (struct sctp_paramhdr *)&ai, sizeof(ai));
7348 aip = (struct sctp_adaptation_layer_indication *)phdr;
7349 if (aip) {
7350 stcb->asoc.peers_adaptation = ntohl(aip->indication);
7351 stcb->asoc.adaptation_needed = 1;
7352 }
7353 }
7354 } else if (ptype == SCTP_SET_PRIM_ADDR) {
7355 struct sctp_asconf_addr_param lstore, *fee;
7356 int lptype;
7357 struct sockaddr *lsa = NULL;
7358#ifdef INET
7359 struct sctp_asconf_addrv4_param *fii;
7360#endif
7361
t00fcxenaac0f452014-08-12 12:51:45 +00007362 if (stcb->asoc.asconf_supported == 0) {
7363 return (-100);
7364 }
tuexendd729232011-11-01 23:04:43 +00007365 if (plen > sizeof(lstore)) {
7366 return (-23);
7367 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007368 if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
7369 return (-101);
7370 }
tuexendd729232011-11-01 23:04:43 +00007371 phdr = sctp_get_next_param(m, offset,
7372 (struct sctp_paramhdr *)&lstore,
Michael Tuexen473225f2017-06-23 23:05:43 +02007373 plen);
tuexendd729232011-11-01 23:04:43 +00007374 if (phdr == NULL) {
7375 return (-24);
7376 }
7377 fee = (struct sctp_asconf_addr_param *)phdr;
7378 lptype = ntohs(fee->addrp.ph.param_type);
7379 switch (lptype) {
7380#ifdef INET
7381 case SCTP_IPV4_ADDRESS:
7382 if (plen !=
7383 sizeof(struct sctp_asconf_addrv4_param)) {
7384 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7385 (int)sizeof(struct sctp_asconf_addrv4_param),
7386 plen);
7387 } else {
7388 fii = (struct sctp_asconf_addrv4_param *)fee;
7389 sin.sin_addr.s_addr = fii->addrp.addr;
7390 lsa = (struct sockaddr *)&sin;
7391 }
7392 break;
7393#endif
7394#ifdef INET6
7395 case SCTP_IPV6_ADDRESS:
7396 if (plen !=
7397 sizeof(struct sctp_asconf_addr_param)) {
7398 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7399 (int)sizeof(struct sctp_asconf_addr_param),
7400 plen);
7401 } else {
7402 memcpy(sin6.sin6_addr.s6_addr,
7403 fee->addrp.addr,
7404 sizeof(fee->addrp.addr));
7405 lsa = (struct sockaddr *)&sin6;
7406 }
7407 break;
7408#endif
7409 default:
7410 break;
7411 }
7412 if (lsa) {
7413 (void)sctp_set_primary_addr(stcb, sa, NULL);
7414 }
7415 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7416 stcb->asoc.peer_supports_nat = 1;
7417 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7418 /* Peer supports pr-sctp */
t00fcxenaac0f452014-08-12 12:51:45 +00007419 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007420 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7421 /* A supported extension chunk */
7422 struct sctp_supported_chunk_types_param *pr_supported;
7423 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7424 int num_ent, i;
7425
Michael Tuexen473225f2017-06-23 23:05:43 +02007426 if (plen > sizeof(local_store)) {
7427 return (-35);
7428 }
tuexendd729232011-11-01 23:04:43 +00007429 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02007430 (struct sctp_paramhdr *)&local_store, plen);
tuexendd729232011-11-01 23:04:43 +00007431 if (phdr == NULL) {
7432 return (-25);
7433 }
tuexendd729232011-11-01 23:04:43 +00007434 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7435 num_ent = plen - sizeof(struct sctp_paramhdr);
7436 for (i = 0; i < num_ent; i++) {
7437 switch (pr_supported->chunk_types[i]) {
7438 case SCTP_ASCONF:
t00fcxenaac0f452014-08-12 12:51:45 +00007439 peer_supports_asconf = 1;
t00fcxen10c96982014-12-17 20:37:01 +00007440 break;
tuexendd729232011-11-01 23:04:43 +00007441 case SCTP_ASCONF_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007442 peer_supports_asconf_ack = 1;
tuexendd729232011-11-01 23:04:43 +00007443 break;
7444 case SCTP_FORWARD_CUM_TSN:
t00fcxenaac0f452014-08-12 12:51:45 +00007445 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007446 break;
7447 case SCTP_PACKET_DROPPED:
t00fcxenaac0f452014-08-12 12:51:45 +00007448 peer_supports_pktdrop = 1;
tuexendd729232011-11-01 23:04:43 +00007449 break;
7450 case SCTP_NR_SELECTIVE_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007451 peer_supports_nrsack = 1;
tuexendd729232011-11-01 23:04:43 +00007452 break;
7453 case SCTP_STREAM_RESET:
t00fcxenaac0f452014-08-12 12:51:45 +00007454 peer_supports_reconfig = 1;
tuexendd729232011-11-01 23:04:43 +00007455 break;
7456 case SCTP_AUTHENTICATION:
t00fcxenaac0f452014-08-12 12:51:45 +00007457 peer_supports_auth = 1;
tuexendd729232011-11-01 23:04:43 +00007458 break;
Michael Tuexene5001952016-04-17 19:25:27 +02007459 case SCTP_IDATA:
7460 peer_supports_idata = 1;
7461 break;
tuexendd729232011-11-01 23:04:43 +00007462 default:
7463 /* one I have not learned yet */
7464 break;
tuexendd729232011-11-01 23:04:43 +00007465 }
7466 }
7467 } else if (ptype == SCTP_RANDOM) {
7468 if (plen > sizeof(random_store))
7469 break;
7470 if (got_random) {
7471 /* already processed a RANDOM */
7472 goto next_param;
7473 }
7474 phdr = sctp_get_next_param(m, offset,
7475 (struct sctp_paramhdr *)random_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007476 plen);
tuexendd729232011-11-01 23:04:43 +00007477 if (phdr == NULL)
7478 return (-26);
7479 p_random = (struct sctp_auth_random *)phdr;
7480 random_len = plen - sizeof(*p_random);
7481 /* enforce the random length */
7482 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7483 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7484 return (-27);
7485 }
7486 got_random = 1;
7487 } else if (ptype == SCTP_HMAC_LIST) {
t00fcxen05d0bb92014-09-16 14:25:27 +00007488 uint16_t num_hmacs;
7489 uint16_t i;
tuexendd729232011-11-01 23:04:43 +00007490
7491 if (plen > sizeof(hmacs_store))
7492 break;
7493 if (got_hmacs) {
7494 /* already processed a HMAC list */
7495 goto next_param;
7496 }
7497 phdr = sctp_get_next_param(m, offset,
7498 (struct sctp_paramhdr *)hmacs_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007499 plen);
tuexendd729232011-11-01 23:04:43 +00007500 if (phdr == NULL)
7501 return (-28);
7502 hmacs = (struct sctp_auth_hmac_algo *)phdr;
7503 hmacs_len = plen - sizeof(*hmacs);
7504 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7505 /* validate the hmac list */
7506 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7507 return (-29);
7508 }
7509 if (stcb->asoc.peer_hmacs != NULL)
7510 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7511 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7512 if (stcb->asoc.peer_hmacs != NULL) {
7513 for (i = 0; i < num_hmacs; i++) {
7514 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7515 ntohs(hmacs->hmac_ids[i]));
7516 }
7517 }
7518 got_hmacs = 1;
7519 } else if (ptype == SCTP_CHUNK_LIST) {
7520 int i;
7521
7522 if (plen > sizeof(chunks_store))
7523 break;
7524 if (got_chklist) {
7525 /* already processed a Chunks list */
7526 goto next_param;
7527 }
7528 phdr = sctp_get_next_param(m, offset,
7529 (struct sctp_paramhdr *)chunks_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007530 plen);
tuexendd729232011-11-01 23:04:43 +00007531 if (phdr == NULL)
7532 return (-30);
7533 chunks = (struct sctp_auth_chunk_list *)phdr;
7534 num_chunks = plen - sizeof(*chunks);
7535 if (stcb->asoc.peer_auth_chunks != NULL)
7536 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7537 else
7538 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7539 for (i = 0; i < num_chunks; i++) {
7540 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7541 stcb->asoc.peer_auth_chunks);
7542 /* record asconf/asconf-ack if listed */
7543 if (chunks->chunk_types[i] == SCTP_ASCONF)
7544 saw_asconf = 1;
7545 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7546 saw_asconf_ack = 1;
tuexendd729232011-11-01 23:04:43 +00007547 }
7548 got_chklist = 1;
7549 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7550 (ptype == SCTP_STATE_COOKIE) ||
7551 (ptype == SCTP_UNRECOG_PARAM) ||
7552 (ptype == SCTP_COOKIE_PRESERVE) ||
7553 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7554 (ptype == SCTP_ADD_IP_ADDRESS) ||
7555 (ptype == SCTP_DEL_IP_ADDRESS) ||
7556 (ptype == SCTP_ERROR_CAUSE_IND) ||
7557 (ptype == SCTP_SUCCESS_REPORT)) {
Michael Tuexen196fbbe2020-09-28 17:04:25 +02007558 /* don't care */
tuexendd729232011-11-01 23:04:43 +00007559 } else {
7560 if ((ptype & 0x8000) == 0x0000) {
7561 /*
7562 * must stop processing the rest of the
7563 * param's. Any report bits were handled
7564 * with the call to
7565 * sctp_arethere_unrecognized_parameters()
7566 * when the INIT or INIT-ACK was first seen.
7567 */
7568 break;
7569 }
7570 }
tuexen09631162012-04-18 11:23:02 +00007571
tuexendd729232011-11-01 23:04:43 +00007572 next_param:
7573 offset += SCTP_SIZE32(plen);
7574 if (offset >= limit) {
7575 break;
7576 }
Michael Tuexen473225f2017-06-23 23:05:43 +02007577 phdr = sctp_get_next_param(m, offset, &param_buf,
Michael Tuexen6e026692021-05-03 02:56:21 +02007578 sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007579 }
7580 /* Now check to see if we need to purge any addresses */
7581 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7582 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7583 SCTP_ADDR_NOT_IN_ASSOC) {
7584 /* This address has been removed from the asoc */
7585 /* remove and free it */
7586 stcb->asoc.numnets--;
7587 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
Michael Tuexen6e026692021-05-03 02:56:21 +02007588 if (net == stcb->asoc.alternate) {
7589 sctp_free_remote_addr(stcb->asoc.alternate);
7590 stcb->asoc.alternate = NULL;
7591 }
tuexendd729232011-11-01 23:04:43 +00007592 if (net == stcb->asoc.primary_destination) {
7593 stcb->asoc.primary_destination = NULL;
7594 sctp_select_primary_destination(stcb);
7595 }
Michael Tuexen6e026692021-05-03 02:56:21 +02007596 sctp_free_remote_addr(net);
tuexendd729232011-11-01 23:04:43 +00007597 }
7598 }
t00fcxenaac0f452014-08-12 12:51:45 +00007599 if ((stcb->asoc.ecn_supported == 1) &&
7600 (peer_supports_ecn == 0)) {
7601 stcb->asoc.ecn_supported = 0;
tuexendd729232011-11-01 23:04:43 +00007602 }
t00fcxenaac0f452014-08-12 12:51:45 +00007603 if ((stcb->asoc.prsctp_supported == 1) &&
7604 (peer_supports_prsctp == 0)) {
7605 stcb->asoc.prsctp_supported = 0;
7606 }
7607 if ((stcb->asoc.auth_supported == 1) &&
7608 ((peer_supports_auth == 0) ||
7609 (got_random == 0) || (got_hmacs == 0))) {
7610 stcb->asoc.auth_supported = 0;
7611 }
7612 if ((stcb->asoc.asconf_supported == 1) &&
7613 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
7614 (stcb->asoc.auth_supported == 0) ||
7615 (saw_asconf == 0) || (saw_asconf_ack == 0))) {
7616 stcb->asoc.asconf_supported = 0;
7617 }
7618 if ((stcb->asoc.reconfig_supported == 1) &&
7619 (peer_supports_reconfig == 0)) {
7620 stcb->asoc.reconfig_supported = 0;
7621 }
Michael Tuexene5001952016-04-17 19:25:27 +02007622 if ((stcb->asoc.idata_supported == 1) &&
7623 (peer_supports_idata == 0)) {
7624 stcb->asoc.idata_supported = 0;
7625 }
t00fcxenaac0f452014-08-12 12:51:45 +00007626 if ((stcb->asoc.nrsack_supported == 1) &&
7627 (peer_supports_nrsack == 0)) {
7628 stcb->asoc.nrsack_supported = 0;
7629 }
7630 if ((stcb->asoc.pktdrop_supported == 1) &&
7631 (peer_supports_pktdrop == 0)){
7632 stcb->asoc.pktdrop_supported = 0;
7633 }
7634 /* validate authentication required parameters */
7635 if ((peer_supports_auth == 0) && (got_chklist == 1)) {
tuexendd729232011-11-01 23:04:43 +00007636 /* peer does not support auth but sent a chunks list? */
7637 return (-31);
7638 }
t00fcxenaac0f452014-08-12 12:51:45 +00007639 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
tuexendd729232011-11-01 23:04:43 +00007640 /* peer supports asconf but not auth? */
7641 return (-32);
t00fcxenaac0f452014-08-12 12:51:45 +00007642 } else if ((peer_supports_asconf == 1) &&
7643 (peer_supports_auth == 1) &&
tuexendd729232011-11-01 23:04:43 +00007644 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7645 return (-33);
7646 }
7647 /* concatenate the full random key */
7648 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7649 if (chunks != NULL) {
7650 keylen += sizeof(*chunks) + num_chunks;
7651 }
7652 new_key = sctp_alloc_key(keylen);
7653 if (new_key != NULL) {
7654 /* copy in the RANDOM */
7655 if (p_random != NULL) {
7656 keylen = sizeof(*p_random) + random_len;
Michael Tuexend0853302017-07-19 17:01:31 +02007657 memcpy(new_key->key, p_random, keylen);
Michael Tuexen8789a6d2018-06-02 18:33:47 +02007658 } else {
7659 keylen = 0;
tuexendd729232011-11-01 23:04:43 +00007660 }
7661 /* append in the AUTH chunks */
7662 if (chunks != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007663 memcpy(new_key->key + keylen, chunks,
7664 sizeof(*chunks) + num_chunks);
tuexendd729232011-11-01 23:04:43 +00007665 keylen += sizeof(*chunks) + num_chunks;
7666 }
7667 /* append in the HMACs */
7668 if (hmacs != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007669 memcpy(new_key->key + keylen, hmacs,
7670 sizeof(*hmacs) + hmacs_len);
tuexendd729232011-11-01 23:04:43 +00007671 }
7672 } else {
7673 /* failed to get memory for the key */
7674 return (-34);
7675 }
7676 if (stcb->asoc.authinfo.peer_random != NULL)
7677 sctp_free_key(stcb->asoc.authinfo.peer_random);
7678 stcb->asoc.authinfo.peer_random = new_key;
7679 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7680 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7681
7682 return (0);
7683}
7684
7685int
7686sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7687 struct sctp_nets *net)
7688{
7689 /* make sure the requested primary address exists in the assoc */
7690 if (net == NULL && sa)
7691 net = sctp_findnet(stcb, sa);
7692
7693 if (net == NULL) {
7694 /* didn't find the requested primary address! */
7695 return (-1);
7696 } else {
7697 /* set the primary address */
7698 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7699 /* Must be confirmed, so queue to set */
7700 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7701 return (0);
7702 }
7703 stcb->asoc.primary_destination = net;
7704 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
7705 sctp_free_remote_addr(stcb->asoc.alternate);
7706 stcb->asoc.alternate = NULL;
7707 }
7708 net = TAILQ_FIRST(&stcb->asoc.nets);
7709 if (net != stcb->asoc.primary_destination) {
7710 /* first one on the list is NOT the primary
7711 * sctp_cmpaddr() is much more efficient if
7712 * the primary is the first on the list, make it
7713 * so.
7714 */
7715 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7716 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7717 }
7718 return (0);
7719 }
7720}
7721
Michael Tuexen4ec13f72021-09-09 01:22:43 +02007722bool
tuexen9784e9a2011-12-18 13:04:23 +00007723sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
tuexendd729232011-11-01 23:04:43 +00007724{
tuexendd729232011-11-01 23:04:43 +00007725 struct sctpasochead *head;
7726 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00007727
Michael Tuexen8dd0be72021-09-12 19:27:59 +02007728 SCTP_INP_INFO_LOCK_ASSERT();
7729
Michael Tuexen4ec13f72021-09-09 01:22:43 +02007730 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))];
tuexendd729232011-11-01 23:04:43 +00007731 LIST_FOREACH(stcb, head, sctp_asocs) {
7732 /* We choose not to lock anything here. TCB's can't be
7733 * removed since we have the read lock, so they can't
7734 * be freed on us, same thing for the INP. I may
7735 * be wrong with this assumption, but we will go
7736 * with it for now :-)
7737 */
7738 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7739 continue;
7740 }
7741 if (stcb->asoc.my_vtag == tag) {
7742 /* candidate */
7743 if (stcb->rport != rport) {
7744 continue;
7745 }
7746 if (stcb->sctp_ep->sctp_lport != lport) {
7747 continue;
7748 }
Michael Tuexen4ec13f72021-09-09 01:22:43 +02007749 /* The tag is currently used, so don't use it. */
Michael Tuexen8dd0be72021-09-12 19:27:59 +02007750 return (false);
tuexendd729232011-11-01 23:04:43 +00007751 }
7752 }
Michael Tuexen8dd0be72021-09-12 19:27:59 +02007753 return (!sctp_is_in_timewait(tag, lport, rport, (uint32_t)now->tv_sec));
tuexendd729232011-11-01 23:04:43 +00007754}
7755
tuexendd729232011-11-01 23:04:43 +00007756static void
tuexen9784e9a2011-12-18 13:04:23 +00007757sctp_drain_mbufs(struct sctp_tcb *stcb)
tuexendd729232011-11-01 23:04:43 +00007758{
7759 /*
7760 * We must hunt this association for MBUF's past the cumack (i.e.
7761 * out of order data that we can renege on).
7762 */
7763 struct sctp_association *asoc;
7764 struct sctp_tmit_chunk *chk, *nchk;
7765 uint32_t cumulative_tsn_p1;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007766 struct sctp_queued_to_read *control, *ncontrol;
tuexendd729232011-11-01 23:04:43 +00007767 int cnt, strmat;
7768 uint32_t gap, i;
tuexen63fc0bb2011-12-27 12:24:52 +00007769 int fnd = 0;
tuexendd729232011-11-01 23:04:43 +00007770
7771 /* We look for anything larger than the cum-ack + 1 */
7772
7773 asoc = &stcb->asoc;
7774 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7775 /* none we can reneg on. */
7776 return;
7777 }
7778 SCTP_STAT_INCR(sctps_protocol_drains_done);
7779 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7780 cnt = 0;
tuexendd729232011-11-01 23:04:43 +00007781 /* Ok that was fun, now we will drain all the inbound streams? */
7782 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007783 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007784#ifdef INVARIANTS
Michael Tuexen53579be2020-10-07 17:39:17 +02007785 if (control->on_strm_q != SCTP_ON_ORDERED) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007786 panic("Huh control: %p on_q: %d -- not ordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007787 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007788 }
7789#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007790 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
tuexendd729232011-11-01 23:04:43 +00007791 /* Yep it is above cum-ack */
7792 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007793 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7794 KASSERT(control->length > 0, ("control has zero length"));
7795 if (asoc->size_on_all_streams >= control->length) {
7796 asoc->size_on_all_streams -= control->length;
7797 } else {
7798#ifdef INVARIANTS
7799 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7800#else
7801 asoc->size_on_all_streams = 0;
7802#endif
7803 }
tuexendd729232011-11-01 23:04:43 +00007804 sctp_ucount_decr(asoc->cnt_on_all_streams);
7805 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007806 if (control->on_read_q) {
7807 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7808 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007809 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007810 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
7811 control->on_strm_q = 0;
7812 if (control->data) {
7813 sctp_m_freem(control->data);
7814 control->data = NULL;
tuexendd729232011-11-01 23:04:43 +00007815 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007816 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007817 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007818 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007819 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007820 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007821 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7822 if (asoc->size_on_reasm_queue >= chk->send_size) {
7823 asoc->size_on_reasm_queue -= chk->send_size;
7824 } else {
7825#ifdef INVARIANTS
7826 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7827#else
7828 asoc->size_on_reasm_queue = 0;
7829#endif
7830 }
Michael Tuexene5001952016-04-17 19:25:27 +02007831 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7832 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007833 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02007834 if (chk->data) {
7835 sctp_m_freem(chk->data);
7836 chk->data = NULL;
7837 }
7838 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7839 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007840 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02007841 }
7842 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007843 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007844#ifdef INVARIANTS
Michael Tuexen53579be2020-10-07 17:39:17 +02007845 if (control->on_strm_q != SCTP_ON_UNORDERED) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007846 panic("Huh control: %p on_q: %d -- not unordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007847 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007848 }
7849#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007850 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
Michael Tuexene5001952016-04-17 19:25:27 +02007851 /* Yep it is above cum-ack */
7852 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007853 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7854 KASSERT(control->length > 0, ("control has zero length"));
7855 if (asoc->size_on_all_streams >= control->length) {
7856 asoc->size_on_all_streams -= control->length;
7857 } else {
7858#ifdef INVARIANTS
7859 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7860#else
7861 asoc->size_on_all_streams = 0;
7862#endif
7863 }
Michael Tuexene5001952016-04-17 19:25:27 +02007864 sctp_ucount_decr(asoc->cnt_on_all_streams);
7865 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007866 if (control->on_read_q) {
7867 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7868 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007869 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007870 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
7871 control->on_strm_q = 0;
7872 if (control->data) {
7873 sctp_m_freem(control->data);
7874 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02007875 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007876 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007877 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007878 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007879 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007880 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007881 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7882 if (asoc->size_on_reasm_queue >= chk->send_size) {
7883 asoc->size_on_reasm_queue -= chk->send_size;
7884 } else {
7885#ifdef INVARIANTS
7886 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7887#else
7888 asoc->size_on_reasm_queue = 0;
7889#endif
7890 }
Michael Tuexene5001952016-04-17 19:25:27 +02007891 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7892 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007893 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02007894 if (chk->data) {
7895 sctp_m_freem(chk->data);
7896 chk->data = NULL;
7897 }
7898 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7899 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007900 sctp_free_a_readq(stcb, control);
tuexendd729232011-11-01 23:04:43 +00007901 }
7902 }
7903 }
7904 if (cnt) {
7905 /* We must back down to see what the new highest is */
7906 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
7907 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
7908 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
7909 asoc->highest_tsn_inside_map = i;
7910 fnd = 1;
7911 break;
7912 }
7913 }
7914 if (!fnd) {
7915 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
7916 }
7917
7918 /*
7919 * Question, should we go through the delivery queue? The only
7920 * reason things are on here is the app not reading OR a p-d-api up.
7921 * An attacker COULD send enough in to initiate the PD-API and then
7922 * send a bunch of stuff to other streams... these would wind up on
7923 * the delivery queue.. and then we would not get to them. But in
7924 * order to do this I then have to back-track and un-deliver
7925 * sequence numbers in streams.. el-yucko. I think for now we will
7926 * NOT look at the delivery queue and leave it to be something to
7927 * consider later. An alternative would be to abort the P-D-API with
7928 * a notification and then deliver the data.... Or another method
7929 * might be to keep track of how many times the situation occurs and
7930 * if we see a possible attack underway just abort the association.
7931 */
7932#ifdef SCTP_DEBUG
7933 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
7934#endif
7935 /*
7936 * Now do we need to find a new
7937 * asoc->highest_tsn_inside_map?
7938 */
7939 asoc->last_revoke_count = cnt;
Michael Tuexen61f66832020-02-09 23:15:19 +01007940 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
Michael Tuexen21b55df2020-02-09 23:46:32 +01007941 SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
tuexendd729232011-11-01 23:04:43 +00007942 /*sa_ignore NO_NULL_CHK*/
7943 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7944 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007945 }
7946 /*
7947 * Another issue, in un-setting the TSN's in the mapping array we
7948 * DID NOT adjust the highest_tsn marker. This will cause one of two
7949 * things to occur. It may cause us to do extra work in checking for
7950 * our mapping array movement. More importantly it may cause us to
7951 * SACK every datagram. This may not be a bad thing though since we
7952 * will recover once we get our cum-ack above and all this stuff we
7953 * dumped recovered.
7954 */
7955}
7956
7957void
7958sctp_drain()
7959{
7960 /*
7961 * We must walk the PCB lists for ALL associations here. The system
7962 * is LOW on MBUF's and needs help. This is where reneging will
7963 * occur. We really hope this does NOT happen!
7964 */
Michael Tuexen5be0c252020-06-13 00:53:56 +02007965#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00007966 VNET_ITERATOR_DECL(vnet_iter);
7967#else
7968 struct sctp_inpcb *inp;
7969 struct sctp_tcb *stcb;
7970
7971 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7972 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7973 return;
7974 }
7975#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02007976#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00007977 VNET_LIST_RLOCK_NOSLEEP();
7978 VNET_FOREACH(vnet_iter) {
7979 CURVNET_SET(vnet_iter);
7980 struct sctp_inpcb *inp;
7981 struct sctp_tcb *stcb;
7982#endif
7983
Michael Tuexen5be0c252020-06-13 00:53:56 +02007984#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00007985 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7986 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7987#ifdef VIMAGE
7988 continue;
7989#else
7990 return;
tuexen09631162012-04-18 11:23:02 +00007991#endif
tuexendd729232011-11-01 23:04:43 +00007992 }
7993#endif
7994 SCTP_INP_INFO_RLOCK();
7995 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7996 /* For each endpoint */
7997 SCTP_INP_RLOCK(inp);
7998 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7999 /* For each association */
8000 SCTP_TCB_LOCK(stcb);
tuexen9784e9a2011-12-18 13:04:23 +00008001 sctp_drain_mbufs(stcb);
tuexendd729232011-11-01 23:04:43 +00008002 SCTP_TCB_UNLOCK(stcb);
8003 }
8004 SCTP_INP_RUNLOCK(inp);
8005 }
8006 SCTP_INP_INFO_RUNLOCK();
Michael Tuexen5be0c252020-06-13 00:53:56 +02008007#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00008008 CURVNET_RESTORE();
8009 }
8010 VNET_LIST_RUNLOCK_NOSLEEP();
8011#endif
8012}
8013
8014/*
8015 * start a new iterator
8016 * iterates through all endpoints and associations based on the pcb_state
8017 * flags and asoc_state. "af" (mandatory) is executed for all matching
8018 * assocs and "ef" (optional) is executed when the iterator completes.
8019 * "inpf" (optional) is executed for each new endpoint as it is being
8020 * iterated through. inpe (optional) is called when the inp completes
8021 * its way through all the stcbs.
8022 */
8023int
8024sctp_initiate_iterator(inp_func inpf,
8025 asoc_func af,
8026 inp_func inpe,
8027 uint32_t pcb_state,
8028 uint32_t pcb_features,
8029 uint32_t asoc_state,
8030 void *argp,
8031 uint32_t argi,
8032 end_func ef,
8033 struct sctp_inpcb *s_inp,
8034 uint8_t chunk_output_off)
8035{
8036 struct sctp_iterator *it = NULL;
8037
8038 if (af == NULL) {
8039 return (-1);
8040 }
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008041 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8042 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
8043 SCTP_BASE_VAR(sctp_pcb_initialized));
8044 return (-1);
8045 }
tuexendd729232011-11-01 23:04:43 +00008046 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
8047 SCTP_M_ITER);
8048 if (it == NULL) {
8049 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
Michael Tuexen287284c2020-01-12 14:34:21 +01008050 return (-1);
tuexendd729232011-11-01 23:04:43 +00008051 }
8052 memset(it, 0, sizeof(*it));
8053 it->function_assoc = af;
8054 it->function_inp = inpf;
8055 if (inpf)
8056 it->done_current_ep = 0;
8057 else
8058 it->done_current_ep = 1;
8059 it->function_atend = ef;
8060 it->pointer = argp;
8061 it->val = argi;
8062 it->pcb_flags = pcb_state;
8063 it->pcb_features = pcb_features;
8064 it->asoc_state = asoc_state;
8065 it->function_inp_end = inpe;
8066 it->no_chunk_output = chunk_output_off;
Michael Tuexen5be0c252020-06-13 00:53:56 +02008067#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00008068 it->vn = curvnet;
tuexen09631162012-04-18 11:23:02 +00008069#endif
tuexendd729232011-11-01 23:04:43 +00008070 if (s_inp) {
8071 /* Assume lock is held here */
8072 it->inp = s_inp;
8073 SCTP_INP_INCR_REF(it->inp);
8074 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
8075 } else {
8076 SCTP_INP_INFO_RLOCK();
8077 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
8078 if (it->inp) {
8079 SCTP_INP_INCR_REF(it->inp);
8080 }
8081 SCTP_INP_INFO_RUNLOCK();
8082 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
tuexendd729232011-11-01 23:04:43 +00008083 }
8084 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008085 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8086 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8087 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
8088 SCTP_BASE_VAR(sctp_pcb_initialized), it);
8089 SCTP_FREE(it, SCTP_M_ITER);
8090 return (-1);
8091 }
tuexendd729232011-11-01 23:04:43 +00008092 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
8093 if (sctp_it_ctl.iterator_running == 0) {
8094 sctp_wakeup_iterator();
8095 }
8096 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8097 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
8098 return (0);
8099}