blob: 1f36c3a3b6966f76574afd41645ff6734176d660 [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);
Michael Tuexened352c42022-02-18 15:23:58 +0100597 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index);
tuexendd729232011-11-01 23:04:43 +0000598 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__)
Michael Tuexen24343ce2022-01-03 23:10:33 +01002920 rw_init_flags(&inp->ip_inp.inp.inp_lock, "sctpinp",
2921 RW_RECURSE | RW_DUPOK);
tuexendd729232011-11-01 23:04:43 +00002922#endif
2923 SCTP_INP_READ_INIT(inp);
2924 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2925 /* lock the new ep */
2926 SCTP_INP_WLOCK(inp);
2927
2928 /* add it to the info area */
2929 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
Michael Tuexen5be0c252020-06-13 00:53:56 +02002930#if defined(__APPLE__) && !defined(__Userspace__)
t00fcxenbc766ab2014-01-03 19:29:31 +00002931 inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo);
2932#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2933 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list);
2934#else
2935 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list);
2936#endif
tuexendd729232011-11-01 23:04:43 +00002937#endif
2938 SCTP_INP_INFO_WUNLOCK();
2939
2940 TAILQ_INIT(&inp->read_queue);
2941 LIST_INIT(&inp->sctp_addr_list);
2942
2943 LIST_INIT(&inp->sctp_asoc_list);
2944
2945#ifdef SCTP_TRACK_FREED_ASOCS
2946 /* TEMP CODE */
2947 LIST_INIT(&inp->sctp_asoc_free_list);
2948#endif
2949 /* Init the timer structure for signature change */
2950 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2951 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2952
2953 /* now init the actual endpoint default data */
2954 m = &inp->sctp_ep;
2955
2956 /* setup the base timeout information */
Michael Tuexen25935e82020-03-28 22:55:28 +01002957 m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */
2958 m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */
2959 m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2960 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2961 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2962 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2963 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
tuexendd729232011-11-01 23:04:43 +00002964 /* all max/min max are in ms */
2965 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2966 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2967 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2968 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2969 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
tuexendd729232011-11-01 23:04:43 +00002970 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2971 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2972 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2973 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2974 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2975 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2976 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2977 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2978
2979 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2980 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
t00fcxenc45180a2013-07-05 10:17:17 +00002981 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
tuexendd729232011-11-01 23:04:43 +00002982 /* number of streams to pre-open on a association */
2983 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2984
Michael Tuexende0630d2017-11-03 22:04:01 +01002985 m->default_mtu = 0;
tuexendd729232011-11-01 23:04:43 +00002986 /* Add adaptation cookie */
t00fcxen36377202013-02-11 21:07:33 +00002987 m->adaptation_layer_indicator = 0;
2988 m->adaptation_layer_indicator_provided = 0;
tuexendd729232011-11-01 23:04:43 +00002989
2990 /* seed random number generator */
2991 m->random_counter = 1;
2992 m->store_at = SCTP_SIGNATURE_SIZE;
2993 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2994 sctp_fill_random_store(m);
2995
2996 /* Minimum cookie size */
2997 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2998 sizeof(struct sctp_state_cookie);
2999 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
3000
3001 /* Setup the initial secret */
3002 (void)SCTP_GETTIME_TIMEVAL(&time);
Michael Tuexenf2c2b602021-09-07 15:50:13 +02003003 m->time_of_secret_change = (unsigned int)time.tv_sec;
tuexendd729232011-11-01 23:04:43 +00003004
3005 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
3006 m->secret_key[0][i] = sctp_select_initial_TSN(m);
3007 }
3008 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
3009
3010 /* How long is a cookie good for ? */
Michael Tuexen25935e82020-03-28 22:55:28 +01003011 m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
tuexendd729232011-11-01 23:04:43 +00003012 /*
3013 * Initialize authentication parameters
3014 */
3015 m->local_hmacs = sctp_default_supported_hmaclist();
3016 m->local_auth_chunks = sctp_alloc_chunklist();
t00fcxenaac0f452014-08-12 12:51:45 +00003017 if (inp->asconf_supported) {
3018 sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
3019 sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
3020 }
tuexendd729232011-11-01 23:04:43 +00003021 m->default_dscp = 0;
3022#ifdef INET6
3023 m->default_flowlabel = 0;
3024#endif
tuexen9d7b2072011-11-20 16:35:17 +00003025 m->port = 0; /* encapsulation disabled by default */
tuexendd729232011-11-01 23:04:43 +00003026 LIST_INIT(&m->shared_keys);
3027 /* add default NULL key as key id 0 */
3028 null_key = sctp_alloc_sharedkey();
3029 sctp_insert_sharedkey(&m->shared_keys, null_key);
3030 SCTP_INP_WUNLOCK(inp);
3031#ifdef SCTP_LOG_CLOSING
3032 sctp_log_closing(inp, NULL, 12);
3033#endif
3034 return (error);
3035}
3036
tuexendd729232011-11-01 23:04:43 +00003037void
3038sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
3039 struct sctp_tcb *stcb)
3040{
3041 struct sctp_nets *net;
3042 uint16_t lport, rport;
3043 struct sctppcbhead *head;
3044 struct sctp_laddr *laddr, *oladdr;
3045
3046 atomic_add_int(&stcb->asoc.refcnt, 1);
3047 SCTP_TCB_UNLOCK(stcb);
3048 SCTP_INP_INFO_WLOCK();
3049 SCTP_INP_WLOCK(old_inp);
3050 SCTP_INP_WLOCK(new_inp);
3051 SCTP_TCB_LOCK(stcb);
3052 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3053
Michael Tuexen2dfe1942021-12-04 11:39:46 +01003054#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexenc4e5de52021-12-04 19:22:59 +01003055#ifdef INET6
Michael Tuexen2dfe1942021-12-04 11:39:46 +01003056 if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3057 new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS;
3058 if (old_inp->ip_inp.inp.in6p_outputopts) {
3059 new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT);
3060 }
Michael Tuexenc4e5de52021-12-04 19:22:59 +01003061 }
3062#endif
3063#if defined(INET) && defined(INET6)
3064 else
3065#endif
3066#ifdef INET
3067 {
Michael Tuexen2dfe1942021-12-04 11:39:46 +01003068 new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos;
3069 new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl;
3070 }
3071#endif
Michael Tuexenc4e5de52021-12-04 19:22:59 +01003072#endif
tuexendd729232011-11-01 23:04:43 +00003073 new_inp->sctp_ep.time_of_secret_change =
3074 old_inp->sctp_ep.time_of_secret_change;
3075 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
3076 sizeof(old_inp->sctp_ep.secret_key));
3077 new_inp->sctp_ep.current_secret_number =
3078 old_inp->sctp_ep.current_secret_number;
3079 new_inp->sctp_ep.last_secret_number =
3080 old_inp->sctp_ep.last_secret_number;
3081 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
3082
3083 /* make it so new data pours into the new socket */
3084 stcb->sctp_socket = new_inp->sctp_socket;
3085 stcb->sctp_ep = new_inp;
3086
3087 /* Copy the port across */
3088 lport = new_inp->sctp_lport = old_inp->sctp_lport;
3089 rport = stcb->rport;
3090 /* Pull the tcb from the old association */
3091 LIST_REMOVE(stcb, sctp_tcbhash);
3092 LIST_REMOVE(stcb, sctp_tcblist);
3093 if (stcb->asoc.in_asocid_hash) {
3094 LIST_REMOVE(stcb, sctp_tcbasocidhash);
3095 }
3096 /* Now insert the new_inp into the TCP connected hash */
3097 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
3098
3099 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
3100 /* Its safe to access */
3101 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3102
3103 /* Now move the tcb into the endpoint list */
3104 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
3105 /*
3106 * Question, do we even need to worry about the ep-hash since we
3107 * only have one connection? Probably not :> so lets get rid of it
3108 * and not suck up any kernel memory in that.
3109 */
3110 if (stcb->asoc.in_asocid_hash) {
3111 struct sctpasochead *lhd;
3112 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
3113 new_inp->hashasocidmark)];
3114 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
3115 }
3116 /* Ok. Let's restart timer. */
3117 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3118 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
3119 stcb, net);
3120 }
3121
3122 SCTP_INP_INFO_WUNLOCK();
3123 if (new_inp->sctp_tcbhash != NULL) {
3124 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
3125 new_inp->sctp_tcbhash = NULL;
3126 }
3127 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3128 /* Subset bound, so copy in the laddr list from the old_inp */
3129 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3130 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3131 if (laddr == NULL) {
3132 /*
3133 * Gak, what can we do? This assoc is really
3134 * HOSED. We probably should send an abort
3135 * here.
3136 */
3137 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3138 continue;
3139 }
3140 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003141 memset(laddr, 0, sizeof(*laddr));
tuexendd729232011-11-01 23:04:43 +00003142 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3143 laddr->ifa = oladdr->ifa;
3144 atomic_add_int(&laddr->ifa->refcount, 1);
3145 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3146 sctp_nxt_addr);
3147 new_inp->laddr_count++;
3148 if (oladdr == stcb->asoc.last_used_address) {
3149 stcb->asoc.last_used_address = laddr;
3150 }
3151 }
3152 }
Michael Tuexen939d48f2020-07-19 14:44:43 +02003153 /* Now any running timers need to be adjusted. */
3154 if (stcb->asoc.dack_timer.ep == old_inp) {
3155 SCTP_INP_DECR_REF(old_inp);
3156 stcb->asoc.dack_timer.ep = new_inp;
3157 SCTP_INP_INCR_REF(new_inp);
3158 }
3159 if (stcb->asoc.asconf_timer.ep == old_inp) {
3160 SCTP_INP_DECR_REF(old_inp);
3161 stcb->asoc.asconf_timer.ep = new_inp;
3162 SCTP_INP_INCR_REF(new_inp);
3163 }
3164 if (stcb->asoc.strreset_timer.ep == old_inp) {
3165 SCTP_INP_DECR_REF(old_inp);
3166 stcb->asoc.strreset_timer.ep = new_inp;
3167 SCTP_INP_INCR_REF(new_inp);
3168 }
3169 if (stcb->asoc.shut_guard_timer.ep == old_inp) {
3170 SCTP_INP_DECR_REF(old_inp);
3171 stcb->asoc.shut_guard_timer.ep = new_inp;
3172 SCTP_INP_INCR_REF(new_inp);
3173 }
3174 if (stcb->asoc.autoclose_timer.ep == old_inp) {
3175 SCTP_INP_DECR_REF(old_inp);
3176 stcb->asoc.autoclose_timer.ep = new_inp;
3177 SCTP_INP_INCR_REF(new_inp);
3178 }
3179 if (stcb->asoc.delete_prim_timer.ep == old_inp) {
3180 SCTP_INP_DECR_REF(old_inp);
3181 stcb->asoc.delete_prim_timer.ep = new_inp;
3182 SCTP_INP_INCR_REF(new_inp);
3183 }
tuexendd729232011-11-01 23:04:43 +00003184 /* now what about the nets? */
3185 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
Michael Tuexen939d48f2020-07-19 14:44:43 +02003186 if (net->pmtu_timer.ep == old_inp) {
3187 SCTP_INP_DECR_REF(old_inp);
3188 net->pmtu_timer.ep = new_inp;
3189 SCTP_INP_INCR_REF(new_inp);
3190 }
3191 if (net->hb_timer.ep == old_inp) {
3192 SCTP_INP_DECR_REF(old_inp);
3193 net->hb_timer.ep = new_inp;
3194 SCTP_INP_INCR_REF(new_inp);
3195 }
3196 if (net->rxt_timer.ep == old_inp) {
3197 SCTP_INP_DECR_REF(old_inp);
3198 net->rxt_timer.ep = new_inp;
3199 SCTP_INP_INCR_REF(new_inp);
3200 }
tuexendd729232011-11-01 23:04:43 +00003201 }
3202 SCTP_INP_WUNLOCK(new_inp);
3203 SCTP_INP_WUNLOCK(old_inp);
3204}
3205
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003206/*
3207 * insert an laddr entry with the given ifa for the desired list
3208 */
3209static int
3210sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
3211{
3212 struct sctp_laddr *laddr;
3213
3214 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3215 if (laddr == NULL) {
3216 /* out of memory? */
3217 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3218 return (EINVAL);
3219 }
3220 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003221 memset(laddr, 0, sizeof(*laddr));
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003222 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3223 laddr->ifa = ifa;
3224 laddr->action = act;
3225 atomic_add_int(&ifa->refcount, 1);
3226 /* insert it */
3227 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3228
3229 return (0);
3230}
3231
3232/*
3233 * Remove an laddr entry from the local address list (on an assoc)
3234 */
3235static void
3236sctp_remove_laddr(struct sctp_laddr *laddr)
3237{
3238
3239 /* remove from the list */
3240 LIST_REMOVE(laddr, sctp_nxt_addr);
3241 sctp_free_ifa(laddr->ifa);
3242 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
3243 SCTP_DECR_LADDR_COUNT();
3244}
tuexendd729232011-11-01 23:04:43 +00003245#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
Michael Tuexenf7f9cd82020-09-24 15:14:13 +02003246
tuexendd729232011-11-01 23:04:43 +00003247/*
3248 * Don't know why, but without this there is an unknown reference when
3249 * compiling NetBSD... hmm
3250 */
3251extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3252#endif
3253
Michael Tuexena8409b32021-09-02 01:50:45 +02003254/*
3255 * Bind the socket, with the PCB and global info locks held. Note, if a
3256 * socket address is specified, the PCB lock may be dropped and re-acquired.
3257 *
3258 * sctp_ifap is used to bypass normal local address validation checks.
3259 */
tuexendd729232011-11-01 23:04:43 +00003260int
Michael Tuexen5be0c252020-06-13 00:53:56 +02003261#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003262sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3263 struct sctp_ifa *sctp_ifap, struct thread *td)
Michael Tuexeneb611572020-06-13 17:27:02 +02003264#elif defined(_WIN32) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003265sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3266 struct sctp_ifa *sctp_ifap, PKTHREAD p)
tuexendd729232011-11-01 23:04:43 +00003267#else
Michael Tuexena8409b32021-09-02 01:50:45 +02003268sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3269 struct sctp_ifa *sctp_ifap, struct proc *p)
tuexendd729232011-11-01 23:04:43 +00003270#endif
3271{
3272 /* bind a ep to a socket address */
3273 struct sctppcbhead *head;
Michael Tuexena8409b32021-09-02 01:50:45 +02003274 struct sctp_inpcb *inp_tmp;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003275#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003276 struct inpcb *ip_inp;
t00fcxenaff14b62013-11-02 18:02:02 +00003277#endif
tuexendd729232011-11-01 23:04:43 +00003278 int port_reuse_active = 0;
3279 int bindall;
3280#ifdef SCTP_MVRF
3281 int i;
3282#endif
3283 uint16_t lport;
3284 int error;
3285 uint32_t vrf_id;
3286
Michael Tuexenff681752021-09-01 13:00:05 +02003287#if defined(__FreeBSD__) && !defined(__Userspace__)
3288 KASSERT(td != NULL, ("%s: null thread", __func__));
3289
3290#endif
3291 error = 0;
tuexendd729232011-11-01 23:04:43 +00003292 lport = 0;
tuexendd729232011-11-01 23:04:43 +00003293 bindall = 1;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003294#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003295 ip_inp = &inp->ip_inp.inp;
t00fcxenaff14b62013-11-02 18:02:02 +00003296#endif
Michael Tuexena8409b32021-09-02 01:50:45 +02003297
3298 SCTP_INP_INFO_WLOCK_ASSERT();
3299 SCTP_INP_WLOCK_ASSERT(inp);
3300
tuexendd729232011-11-01 23:04:43 +00003301#ifdef SCTP_DEBUG
3302 if (addr) {
t00fcxen94dfd1e2012-12-27 08:14:10 +00003303 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
tuexendd729232011-11-01 23:04:43 +00003304 ntohs(((struct sockaddr_in *)addr)->sin_port));
t00fcxen94dfd1e2012-12-27 08:14:10 +00003305 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
tuexendd729232011-11-01 23:04:43 +00003306 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3307 }
3308#endif
3309 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003310 error = EINVAL;
tuexendd729232011-11-01 23:04:43 +00003311 /* already did a bind, subsequent binds NOT allowed ! */
Michael Tuexenff681752021-09-01 13:00:05 +02003312 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3313 goto out;
tuexendd729232011-11-01 23:04:43 +00003314 }
tuexendd729232011-11-01 23:04:43 +00003315 if (addr != NULL) {
3316 switch (addr->sa_family) {
3317#ifdef INET
3318 case AF_INET:
3319 {
3320 struct sockaddr_in *sin;
3321
3322 /* IPV6_V6ONLY socket? */
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003323 if (SCTP_IPV6_V6ONLY(inp)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003324 error = EINVAL;
3325 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3326 goto out;
tuexendd729232011-11-01 23:04:43 +00003327 }
t00fcxena8a26f62012-09-12 19:24:54 +00003328#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003329 if (addr->sa_len != sizeof(*sin)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003330 error = EINVAL;
3331 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3332 goto out;
tuexendd729232011-11-01 23:04:43 +00003333 }
3334#endif
3335
3336 sin = (struct sockaddr_in *)addr;
3337 lport = sin->sin_port;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003338#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxenc24796c2014-05-06 15:39:13 +00003339 /*
3340 * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3341 * to the proper value.
3342 */
Michael Tuexenff681752021-09-01 13:00:05 +02003343 if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) {
t00fcxenc24796c2014-05-06 15:39:13 +00003344 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003345 goto out;
t00fcxenc24796c2014-05-06 15:39:13 +00003346 }
tuexendd729232011-11-01 23:04:43 +00003347#endif
3348 if (sin->sin_addr.s_addr != INADDR_ANY) {
3349 bindall = 0;
3350 }
3351 break;
3352 }
3353#endif
3354#ifdef INET6
3355 case AF_INET6:
3356 {
3357 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3358 struct sockaddr_in6 *sin6;
3359
3360 sin6 = (struct sockaddr_in6 *)addr;
t00fcxena8a26f62012-09-12 19:24:54 +00003361#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003362 if (addr->sa_len != sizeof(*sin6)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003363 error = EINVAL;
3364 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3365 goto out;
tuexendd729232011-11-01 23:04:43 +00003366 }
3367#endif
tuexendd729232011-11-01 23:04:43 +00003368 lport = sin6->sin6_port;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003369#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxenc24796c2014-05-06 15:39:13 +00003370 /*
3371 * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
3372 * to the proper value.
3373 */
Michael Tuexenff681752021-09-01 13:00:05 +02003374 if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr,
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003375 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
t00fcxenc24796c2014-05-06 15:39:13 +00003376 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003377 goto out;
t00fcxenc24796c2014-05-06 15:39:13 +00003378 }
tuexendd729232011-11-01 23:04:43 +00003379#endif
3380 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3381 bindall = 0;
3382#ifdef SCTP_EMBEDDED_V6_SCOPE
3383 /* KAME hack: embed scopeid */
3384#if defined(SCTP_KAME)
3385 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003386 error = EINVAL;
3387 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3388 goto out;
tuexendd729232011-11-01 23:04:43 +00003389 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02003390#elif defined(__APPLE__) && !defined(__Userspace__)
tuexen9a218b12012-08-04 21:17:58 +00003391#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
3392 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003393#else
tuexen9a218b12012-08-04 21:17:58 +00003394 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003395#endif
Michael Tuexenff681752021-09-01 13:00:05 +02003396 error = EINVAL;
3397 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3398 goto out;
tuexendd729232011-11-01 23:04:43 +00003399 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02003400#elif defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003401 error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
3402 if (error != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003403 error = EINVAL;
tuexendd729232011-11-01 23:04:43 +00003404 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003405 goto out;
tuexendd729232011-11-01 23:04:43 +00003406 }
tuexendd729232011-11-01 23:04:43 +00003407#else
3408 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003409 error = EINVAL;
3410 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3411 goto out;
tuexendd729232011-11-01 23:04:43 +00003412 }
3413#endif
3414#endif /* SCTP_EMBEDDED_V6_SCOPE */
3415 }
3416#ifndef SCOPEDROUTING
3417 /* this must be cleared for ifa_ifwithaddr() */
3418 sin6->sin6_scope_id = 0;
3419#endif /* SCOPEDROUTING */
3420 break;
3421 }
3422#endif
tuexen09a676e2012-07-08 21:03:18 +00003423#if defined(__Userspace__)
3424 case AF_CONN:
3425 {
3426 struct sockaddr_conn *sconn;
t00fcxenefb47e72012-09-09 08:16:51 +00003427
t00fcxena8a26f62012-09-12 19:24:54 +00003428#ifdef HAVE_SA_LEN
tuexen09a676e2012-07-08 21:03:18 +00003429 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
Michael Tuexenff681752021-09-01 13:00:05 +02003430 error = EINVAL;
3431 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3432 goto out;
tuexen09a676e2012-07-08 21:03:18 +00003433 }
3434#endif
3435 sconn = (struct sockaddr_conn *)addr;
3436 lport = sconn->sconn_port;
3437 if (sconn->sconn_addr != NULL) {
3438 bindall = 0;
3439 }
3440 break;
3441 }
3442#endif
tuexendd729232011-11-01 23:04:43 +00003443 default:
Michael Tuexenff681752021-09-01 13:00:05 +02003444 error = EAFNOSUPPORT;
3445 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3446 goto out;
tuexendd729232011-11-01 23:04:43 +00003447 }
3448 }
tuexendd729232011-11-01 23:04:43 +00003449 /* Setup a vrf_id to be the default for the non-bind-all case. */
t00fcxenc24796c2014-05-06 15:39:13 +00003450 vrf_id = inp->def_vrf_id;
tuexendd729232011-11-01 23:04:43 +00003451
tuexendd729232011-11-01 23:04:43 +00003452 if (lport) {
3453 /*
t00fcxenefb47e72012-09-09 08:16:51 +00003454 * Did the caller specify a port? if so we must see if an ep
tuexendd729232011-11-01 23:04:43 +00003455 * already has this one bound.
3456 */
3457 /* got to be root to get at low ports */
Michael Tuexeneb611572020-06-13 17:27:02 +02003458#if !(defined(_WIN32) && !defined(__Userspace__))
Michael Tuexena8409b32021-09-02 01:50:45 +02003459 if (ntohs(lport) < IPPORT_RESERVED &&
Michael Tuexen5be0c252020-06-13 00:53:56 +02003460#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003461 (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
Michael Tuexen5be0c252020-06-13 00:53:56 +02003462#elif defined(__APPLE__) && !defined(__Userspace__)
Michael Tuexena8409b32021-09-02 01:50:45 +02003463 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
Michael Tuexenff681752021-09-01 13:00:05 +02003464#elif defined(__Userspace__)
3465 /* TODO ensure uid is 0, etc... */
Michael Tuexena8409b32021-09-02 01:50:45 +02003466 0) {
tuexendd729232011-11-01 23:04:43 +00003467#else
Michael Tuexena8409b32021-09-02 01:50:45 +02003468 (error = suser(p, 0)) != 0) {
tuexendd729232011-11-01 23:04:43 +00003469#endif
Michael Tuexena8409b32021-09-02 01:50:45 +02003470 goto out;
tuexendd729232011-11-01 23:04:43 +00003471 }
Michael Tuexeneb611572020-06-13 17:27:02 +02003472#endif
Michael Tuexena8409b32021-09-02 01:50:45 +02003473 SCTP_INP_INCR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00003474 SCTP_INP_WUNLOCK(inp);
3475 if (bindall) {
3476#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003477 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003478 vrf_id = inp->m_vrf_ids[i];
3479#else
3480 vrf_id = inp->def_vrf_id;
3481#endif
3482 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3483 if (inp_tmp != NULL) {
3484 /*
3485 * lock guy returned and lower count
3486 * note that we are not bound so
3487 * inp_tmp should NEVER be inp. And
3488 * it is this inp (inp_tmp) that gets
3489 * the reference bump, so we must
3490 * lower it.
3491 */
3492 SCTP_INP_DECR_REF(inp_tmp);
3493 /* unlock info */
3494 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3495 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3496 /* Ok, must be one-2-one and allowing port re-use */
3497 port_reuse_active = 1;
3498 goto continue_anyway;
3499 }
Michael Tuexena8409b32021-09-02 01:50:45 +02003500 SCTP_INP_WLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00003501 SCTP_INP_DECR_REF(inp);
Michael Tuexenff681752021-09-01 13:00:05 +02003502 error = EADDRINUSE;
3503 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexena8409b32021-09-02 01:50:45 +02003504 goto out;
tuexendd729232011-11-01 23:04:43 +00003505 }
3506#ifdef SCTP_MVRF
3507 }
3508#endif
3509 } else {
3510 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3511 if (inp_tmp != NULL) {
3512 /*
3513 * lock guy returned and lower count note
3514 * that we are not bound so inp_tmp should
3515 * NEVER be inp. And it is this inp (inp_tmp)
3516 * that gets the reference bump, so we must
3517 * lower it.
3518 */
3519 SCTP_INP_DECR_REF(inp_tmp);
3520 /* unlock info */
3521 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3522 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3523 /* Ok, must be one-2-one and allowing port re-use */
3524 port_reuse_active = 1;
3525 goto continue_anyway;
3526 }
Michael Tuexena8409b32021-09-02 01:50:45 +02003527 SCTP_INP_WLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00003528 SCTP_INP_DECR_REF(inp);
Michael Tuexenff681752021-09-01 13:00:05 +02003529 error = EADDRINUSE;
3530 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexena8409b32021-09-02 01:50:45 +02003531 goto out;
tuexendd729232011-11-01 23:04:43 +00003532 }
3533 }
3534 continue_anyway:
3535 SCTP_INP_WLOCK(inp);
Michael Tuexenff681752021-09-01 13:00:05 +02003536 SCTP_INP_DECR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00003537 if (bindall) {
3538 /* verify that no lport is not used by a singleton */
t00fcxenefb47e72012-09-09 08:16:51 +00003539 if ((port_reuse_active == 0) &&
3540 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
tuexendd729232011-11-01 23:04:43 +00003541 /* Sorry someone already has this one bound */
3542 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3543 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3544 port_reuse_active = 1;
3545 } else {
Michael Tuexenff681752021-09-01 13:00:05 +02003546 error = EADDRINUSE;
3547 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3548 goto out;
tuexendd729232011-11-01 23:04:43 +00003549 }
3550 }
3551 }
3552 } else {
3553 uint16_t first, last, candidate;
t00fcxenc24796c2014-05-06 15:39:13 +00003554 uint16_t count;
tuexendd729232011-11-01 23:04:43 +00003555
Michael Tuexenff681752021-09-01 13:00:05 +02003556#if defined(__Userspace__)
3557 first = MODULE_GLOBAL(ipport_firstauto);
3558 last = MODULE_GLOBAL(ipport_lastauto);
3559#elif defined(_WIN32)
tuexendd729232011-11-01 23:04:43 +00003560 first = 1;
3561 last = 0xffff;
Michael Tuexenff681752021-09-01 13:00:05 +02003562#elif defined(__FreeBSD__) || defined(__APPLE__)
t00fcxenc24796c2014-05-06 15:39:13 +00003563 if (ip_inp->inp_flags & INP_HIGHPORT) {
3564 first = MODULE_GLOBAL(ipport_hifirstauto);
3565 last = MODULE_GLOBAL(ipport_hilastauto);
3566 } else if (ip_inp->inp_flags & INP_LOWPORT) {
Michael Tuexena5089952020-06-09 23:11:08 +02003567#if defined(__FreeBSD__)
Michael Tuexenff681752021-09-01 13:00:05 +02003568 if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
tuexendd729232011-11-01 23:04:43 +00003569#else
Michael Tuexenff681752021-09-01 13:00:05 +02003570 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
tuexendd729232011-11-01 23:04:43 +00003571#endif
tuexendd729232011-11-01 23:04:43 +00003572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
Michael Tuexenff681752021-09-01 13:00:05 +02003573 goto out;
tuexendd729232011-11-01 23:04:43 +00003574 }
t00fcxenc24796c2014-05-06 15:39:13 +00003575 first = MODULE_GLOBAL(ipport_lowfirstauto);
3576 last = MODULE_GLOBAL(ipport_lowlastauto);
3577 } else {
t00fcxen8dd95b82014-07-11 20:41:28 +00003578 first = MODULE_GLOBAL(ipport_firstauto);
3579 last = MODULE_GLOBAL(ipport_lastauto);
t00fcxenc24796c2014-05-06 15:39:13 +00003580 }
tuexendd729232011-11-01 23:04:43 +00003581#endif
tuexendd729232011-11-01 23:04:43 +00003582 if (first > last) {
3583 uint16_t temp;
3584
3585 temp = first;
3586 first = last;
3587 last = temp;
3588 }
3589 count = last - first + 1; /* number of candidates */
3590 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3591
Michael Tuexenff681752021-09-01 13:00:05 +02003592 for (;;) {
tuexendd729232011-11-01 23:04:43 +00003593#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003594 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003595 if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
3596 break;
3597 }
3598 }
3599 if (i == inp->num_vrfs) {
Michael Tuexenff681752021-09-01 13:00:05 +02003600 lport = htons(candidate);
3601 break;
tuexendd729232011-11-01 23:04:43 +00003602 }
3603#else
3604 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
Michael Tuexenff681752021-09-01 13:00:05 +02003605 lport = htons(candidate);
3606 break;
tuexendd729232011-11-01 23:04:43 +00003607 }
3608#endif
Michael Tuexenff681752021-09-01 13:00:05 +02003609 if (--count == 0) {
3610 error = EADDRINUSE;
3611 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3612 goto out;
tuexendd729232011-11-01 23:04:43 +00003613 }
Michael Tuexenff681752021-09-01 13:00:05 +02003614 if (candidate == last)
3615 candidate = first;
3616 else
3617 candidate = candidate + 1;
tuexendd729232011-11-01 23:04:43 +00003618 }
tuexendd729232011-11-01 23:04:43 +00003619 }
tuexendd729232011-11-01 23:04:43 +00003620 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3621 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3622 /*
3623 * this really should not happen. The guy did a non-blocking
3624 * bind and then did a close at the same time.
3625 */
Michael Tuexenff681752021-09-01 13:00:05 +02003626 error = EINVAL;
3627 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3628 goto out;
tuexendd729232011-11-01 23:04:43 +00003629 }
3630 /* ok we look clear to give out this port, so lets setup the binding */
3631 if (bindall) {
3632 /* binding to all addresses, so just set in the proper flags */
3633 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3634 /* set the automatic addr changes from kernel flag */
3635 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3636 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3637 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3638 } else {
3639 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3640 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3641 }
3642 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3643 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3644 } else {
3645 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3646 }
3647 /* set the automatic mobility_base from kernel
3648 flag (by micchie)
3649 */
3650 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3651 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3652 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003653 } else {
tuexendd729232011-11-01 23:04:43 +00003654 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3655 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3656 }
3657 /* set the automatic mobility_fasthandoff from kernel
3658 flag (by micchie)
3659 */
3660 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3661 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3662 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003663 } else {
tuexendd729232011-11-01 23:04:43 +00003664 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3665 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3666 }
3667 } else {
3668 /*
3669 * bind specific, make sure flags is off and add a new
3670 * address structure to the sctp_addr_list inside the ep
3671 * structure.
3672 *
3673 * We will need to allocate one and insert it at the head. The
3674 * socketopt call can just insert new addresses in there as
3675 * well. It will also have to do the embed scope kame hack
3676 * too (before adding).
3677 */
3678 struct sctp_ifa *ifa;
t00fcxen05f07c02014-09-07 10:15:26 +00003679 union sctp_sockstore store;
tuexendd729232011-11-01 23:04:43 +00003680
t00fcxen05f07c02014-09-07 10:15:26 +00003681 memset(&store, 0, sizeof(store));
tuexendd729232011-11-01 23:04:43 +00003682 switch (addr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00003683#ifdef INET
tuexendd729232011-11-01 23:04:43 +00003684 case AF_INET:
t00fcxen05f07c02014-09-07 10:15:26 +00003685 memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
3686 store.sin.sin_port = 0;
tuexendd729232011-11-01 23:04:43 +00003687 break;
tuexen310f1bc2012-07-15 11:20:56 +00003688#endif
3689#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00003690 case AF_INET6:
t00fcxen05f07c02014-09-07 10:15:26 +00003691 memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
3692 store.sin6.sin6_port = 0;
tuexendd729232011-11-01 23:04:43 +00003693 break;
tuexen310f1bc2012-07-15 11:20:56 +00003694#endif
tuexencc2b1422012-07-10 21:50:32 +00003695#if defined(__Userspace__)
3696 case AF_CONN:
t00fcxen05f07c02014-09-07 10:15:26 +00003697 memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn));
3698 store.sconn.sconn_port = 0;
tuexencc2b1422012-07-10 21:50:32 +00003699 break;
tuexencc2b1422012-07-10 21:50:32 +00003700#endif
tuexendd729232011-11-01 23:04:43 +00003701 default:
3702 break;
3703 }
3704 /*
3705 * first find the interface with the bound address need to
3706 * zero out the port to find the address! yuck! can't do
3707 * this earlier since need port for sctp_pcb_findep()
3708 */
t00fcxenefb47e72012-09-09 08:16:51 +00003709 if (sctp_ifap != NULL) {
tuexendd729232011-11-01 23:04:43 +00003710 ifa = sctp_ifap;
t00fcxenefb47e72012-09-09 08:16:51 +00003711 } else {
tuexendd729232011-11-01 23:04:43 +00003712 /* Note for BSD we hit here always other
3713 * O/S's will pass things in via the
Michael Tuexen3b4263d2020-06-06 19:52:50 +02003714 * sctp_ifap argument.
tuexendd729232011-11-01 23:04:43 +00003715 */
t00fcxen05f07c02014-09-07 10:15:26 +00003716 ifa = sctp_find_ifa_by_addr(&store.sa,
tuexendd729232011-11-01 23:04:43 +00003717 vrf_id, SCTP_ADDR_NOT_LOCKED);
3718 }
3719 if (ifa == NULL) {
Michael Tuexenff681752021-09-01 13:00:05 +02003720 error = EADDRNOTAVAIL;
tuexendd729232011-11-01 23:04:43 +00003721 /* Can't find an interface with that address */
Michael Tuexenff681752021-09-01 13:00:05 +02003722 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3723 goto out;
tuexendd729232011-11-01 23:04:43 +00003724 }
3725#ifdef INET6
3726 if (addr->sa_family == AF_INET6) {
3727 /* GAK, more FIXME IFA lock? */
3728 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3729 /* Can't bind a non-existent addr. */
Michael Tuexenff681752021-09-01 13:00:05 +02003730 error = EINVAL;
3731 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3732 goto out;
tuexendd729232011-11-01 23:04:43 +00003733 }
3734 }
3735#endif
3736 /* we're not bound all */
3737 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3738 /* allow bindx() to send ASCONF's for binding changes */
3739 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3740 /* clear automatic addr changes from kernel flag */
3741 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3742
3743 /* add this address to the endpoint list */
3744 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
Michael Tuexenff681752021-09-01 13:00:05 +02003745 if (error != 0)
3746 goto out;
tuexendd729232011-11-01 23:04:43 +00003747 inp->laddr_count++;
3748 }
3749 /* find the bucket */
tuexen98456cf2012-04-19 15:37:07 +00003750 if (port_reuse_active) {
tuexendd729232011-11-01 23:04:43 +00003751 /* Put it into tcp 1-2-1 hash */
3752 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3753 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
t00fcxenc24796c2014-05-06 15:39:13 +00003754 } else {
tuexendd729232011-11-01 23:04:43 +00003755 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3756 }
3757 /* put it in the bucket */
3758 LIST_INSERT_HEAD(head, inp, sctp_hash);
3759 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 +00003760 (void *)head, ntohs(lport), port_reuse_active);
tuexendd729232011-11-01 23:04:43 +00003761 /* set in the port */
3762 inp->sctp_lport = lport;
3763
3764 /* turn off just the unbound flag */
Michael Tuexenff681752021-09-01 13:00:05 +02003765 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0,
3766 ("%s: inp %p is already bound", __func__, inp));
tuexendd729232011-11-01 23:04:43 +00003767 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
Michael Tuexenff681752021-09-01 13:00:05 +02003768out:
Michael Tuexena8409b32021-09-02 01:50:45 +02003769 return (error);
3770}
3771
3772int
3773#if defined(__FreeBSD__) && !defined(__Userspace__)
3774sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3775 struct sctp_ifa *sctp_ifap, struct thread *td)
3776#elif defined(_WIN32) && !defined(__Userspace__)
3777sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3778 struct sctp_ifa *sctp_ifap, PKTHREAD p)
3779#else
3780sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3781 struct sctp_ifa *sctp_ifap, struct proc *p)
3782#endif
3783{
3784 struct sctp_inpcb *inp;
3785 int error;
3786
3787 inp = so->so_pcb;
3788 SCTP_INP_INFO_WLOCK();
3789 SCTP_INP_WLOCK(inp);
3790#if defined(__FreeBSD__) && !defined(__Userspace__)
3791 error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td);
3792#else
3793 error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, p);
3794#endif
tuexendd729232011-11-01 23:04:43 +00003795 SCTP_INP_WUNLOCK(inp);
3796 SCTP_INP_INFO_WUNLOCK();
Michael Tuexenff681752021-09-01 13:00:05 +02003797 return (error);
tuexendd729232011-11-01 23:04:43 +00003798}
3799
tuexendd729232011-11-01 23:04:43 +00003800static void
3801sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3802{
3803 struct sctp_iterator *it, *nit;
3804
3805 /*
3806 * We enter with the only the ITERATOR_LOCK in place and a write
3807 * lock on the inp_info stuff.
3808 */
3809 it = sctp_it_ctl.cur_it;
Michael Tuexen5be0c252020-06-13 00:53:56 +02003810#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003811 if (it && (it->vn != curvnet)) {
3812 /* Its not looking at our VNET */
3813 return;
3814 }
3815#endif
3816 if (it && (it->inp == inp)) {
3817 /*
3818 * This is tricky and we hold the iterator lock,
3819 * but when it returns and gets the lock (when we
3820 * release it) the iterator will try to operate on
3821 * inp. We need to stop that from happening. But
3822 * of course the iterator has a reference on the
3823 * stcb and inp. We can mark it and it will stop.
3824 *
3825 * If its a single iterator situation, we
3826 * set the end iterator flag. Otherwise
3827 * we set the iterator to go to the next inp.
3828 *
3829 */
3830 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3831 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3832 } else {
3833 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3834 }
3835 }
3836 /* Now go through and remove any single reference to
3837 * our inp that may be still pending on the list
3838 */
3839 SCTP_IPI_ITERATOR_WQ_LOCK();
3840 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
Michael Tuexen5be0c252020-06-13 00:53:56 +02003841#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003842 if (it->vn != curvnet) {
3843 continue;
tuexen09631162012-04-18 11:23:02 +00003844 }
tuexendd729232011-11-01 23:04:43 +00003845#endif
3846 if (it->inp == inp) {
3847 /* This one points to me is it inp specific? */
3848 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3849 /* Remove and free this one */
3850 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3851 it, sctp_nxt_itr);
3852 if (it->function_atend != NULL) {
3853 (*it->function_atend) (it->pointer, it->val);
3854 }
3855 SCTP_FREE(it, SCTP_M_ITER);
3856 } else {
3857 it->inp = LIST_NEXT(it->inp, sctp_list);
3858 if (it->inp) {
3859 SCTP_INP_INCR_REF(it->inp);
3860 }
3861 }
3862 /* When its put in the refcnt is incremented so decr it */
3863 SCTP_INP_DECR_REF(inp);
3864 }
3865 }
3866 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3867}
3868
3869/* release sctp_inpcb unbind the port */
3870void
3871sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3872{
3873 /*
3874 * Here we free a endpoint. We must find it (if it is in the Hash
3875 * table) and remove it from there. Then we must also find it in the
3876 * overall list and remove it from there. After all removals are
3877 * complete then any timer has to be stopped. Then start the actual
3878 * freeing. a) Any local lists. b) Any associations. c) The hash of
3879 * all associations. d) finally the ep itself.
3880 */
Michael Tuexen2292a602022-04-07 17:49:53 +02003881 struct sctp_tcb *stcb, *nstcb;
tuexendd729232011-11-01 23:04:43 +00003882 struct sctp_laddr *laddr, *nladdr;
3883 struct inpcb *ip_pcb;
3884 struct socket *so;
tuexen63fc0bb2011-12-27 12:24:52 +00003885 int being_refed = 0;
tuexendd729232011-11-01 23:04:43 +00003886 struct sctp_queued_to_read *sq, *nsq;
Michael Tuexen3b4263d2020-06-06 19:52:50 +02003887#if !defined(__Userspace__)
Michael Tuexena5089952020-06-09 23:11:08 +02003888#if !defined(__FreeBSD__)
tuexendd729232011-11-01 23:04:43 +00003889 sctp_rtentry_t *rt;
3890#endif
3891#endif
3892 int cnt;
3893 sctp_sharedkey_t *shared_key, *nshared_key;
3894
Michael Tuexen5be0c252020-06-13 00:53:56 +02003895#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003896 sctp_lock_assert(SCTP_INP_SO(inp));
3897#endif
3898#ifdef SCTP_LOG_CLOSING
3899 sctp_log_closing(inp, NULL, 0);
3900#endif
3901 SCTP_ITERATOR_LOCK();
3902 /* mark any iterators on the list or being processed */
3903 sctp_iterator_inp_being_freed(inp);
3904 SCTP_ITERATOR_UNLOCK();
Michael Tuexen900def62021-09-08 13:12:50 +02003905
tuexendd729232011-11-01 23:04:43 +00003906 SCTP_ASOC_CREATE_LOCK(inp);
3907 SCTP_INP_INFO_WLOCK();
tuexendd729232011-11-01 23:04:43 +00003908 SCTP_INP_WLOCK(inp);
Michael Tuexen900def62021-09-08 13:12:50 +02003909 so = inp->sctp_socket;
3910 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0,
3911 ("%s: inp %p still has socket", __func__, inp));
3912 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0,
3913 ("%s: double free of inp %p", __func__, inp));
tuexendd729232011-11-01 23:04:43 +00003914 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3915 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3916 /* socket is gone, so no more wakeups allowed */
3917 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3918 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3919 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
tuexendd729232011-11-01 23:04:43 +00003920 }
3921 /* First time through we have the socket lock, after that no more. */
3922 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
Michael Tuexen390beeb2020-02-04 15:05:45 +01003923 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
tuexendd729232011-11-01 23:04:43 +00003924
3925 if (inp->control) {
3926 sctp_m_freem(inp->control);
3927 inp->control = NULL;
3928 }
3929 if (inp->pkt) {
3930 sctp_m_freem(inp->pkt);
3931 inp->pkt = NULL;
3932 }
tuexendd729232011-11-01 23:04:43 +00003933 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3934 * here but I will be nice :> (i.e.
3935 * ip_pcb = ep;) */
3936 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3937 int cnt_in_sd;
3938
3939 cnt_in_sd = 0;
Michael Tuexen2292a602022-04-07 17:49:53 +02003940 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
3941 SCTP_TCB_LOCK(stcb);
Michael Tuexen28e2ea72022-04-15 15:23:01 +02003942 /* Disconnect the socket please. */
3943 stcb->sctp_socket = NULL;
3944 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
Michael Tuexen2292a602022-04-07 17:49:53 +02003945 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexendd729232011-11-01 23:04:43 +00003946 /* Skip guys being freed */
3947 cnt_in_sd++;
Michael Tuexen2292a602022-04-07 17:49:53 +02003948 if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
tuexendd729232011-11-01 23:04:43 +00003949 /*
3950 * Special case - we did not start a kill
3951 * timer on the asoc due to it was not
3952 * closed. So go ahead and start it now.
3953 */
Michael Tuexen2292a602022-04-07 17:49:53 +02003954 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
3955 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
tuexendd729232011-11-01 23:04:43 +00003956 }
Michael Tuexen2292a602022-04-07 17:49:53 +02003957 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00003958 continue;
3959 }
Michael Tuexen2292a602022-04-07 17:49:53 +02003960 if (((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3961 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) &&
3962 (stcb->asoc.total_output_queue_size == 0)) {
tuexendd729232011-11-01 23:04:43 +00003963 /* If we have data in queue, we don't want to just
3964 * free since the app may have done, send()/close
3965 * or connect/send/close. And it wants the data
3966 * to get across first.
3967 */
3968 /* Just abandon things in the front states */
Michael Tuexen2292a602022-04-07 17:49:53 +02003969 if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_NOFORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00003970 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
tuexendd729232011-11-01 23:04:43 +00003971 cnt_in_sd++;
3972 }
3973 continue;
3974 }
Michael Tuexen2292a602022-04-07 17:49:53 +02003975 if ((stcb->asoc.size_on_reasm_queue > 0) ||
3976 (stcb->asoc.control_pdapi) ||
3977 (stcb->asoc.size_on_all_streams > 0) ||
Michael Tuexen13a3b532022-05-15 22:51:57 +02003978 ((so != NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 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, "");
Michael Tuexen2292a602022-04-07 17:49:53 +02003983 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3984 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00003985 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen2292a602022-04-07 17:49:53 +02003986 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3987 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00003988 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3989 }
Michael Tuexen2292a602022-04-07 17:49:53 +02003990 if (sctp_free_assoc(inp, stcb,
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;
Michael Tuexen2292a602022-04-07 17:49:53 +02003995 } else if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3996 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3997 (stcb->asoc.stream_queue_cnt == 0)) {
3998 if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) {
tuexendd729232011-11-01 23:04:43 +00003999 goto abort_anyway;
4000 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004001 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
4002 (SCTP_GET_STATE(stcb) != 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 Tuexen2292a602022-04-07 17:49:53 +02004009 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4010 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004011 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4012 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004013 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
4014 sctp_stop_timers_for_shutdown(stcb);
4015 if (stcb->asoc.alternate) {
4016 netp = stcb->asoc.alternate;
tuexendd729232011-11-01 23:04:43 +00004017 } else {
Michael Tuexen2292a602022-04-07 17:49:53 +02004018 netp = stcb->asoc.primary_destination;
tuexendd729232011-11-01 23:04:43 +00004019 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004020 sctp_send_shutdown(stcb, netp);
4021 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
tuexendd729232011-11-01 23:04:43 +00004022 netp);
Michael Tuexen2292a602022-04-07 17:49:53 +02004023 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
4024 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00004025 }
4026 } else {
4027 /* mark into shutdown pending */
Michael Tuexen2292a602022-04-07 17:49:53 +02004028 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
4029 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
4030 if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) {
4031 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
tuexendd729232011-11-01 23:04:43 +00004032 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004033 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
4034 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
4035 (stcb->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
tuexendd729232011-11-01 23:04:43 +00004036 struct mbuf *op_err;
4037 abort_anyway:
t00fcxen08f9ff92014-03-16 13:38:54 +00004038 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
Michael Tuexen2292a602022-04-07 17:49:53 +02004039 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
4040 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00004041 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen2292a602022-04-07 17:49:53 +02004042 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4043 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004044 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4045 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004046 if (sctp_free_assoc(inp, stcb,
tuexendd729232011-11-01 23:04:43 +00004047 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 {
Michael Tuexen2292a602022-04-07 17:49:53 +02004053 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00004054 }
4055 }
4056 cnt_in_sd++;
Michael Tuexen2292a602022-04-07 17:49:53 +02004057 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00004058 }
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;
Michael Tuexen8d6935d2022-05-15 01:51:24 +02004072 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
tuexendd729232011-11-01 23:04:43 +00004073 /*
4074 * ok, this guy has been bound. It's port is
4075 * somewhere in the SCTP_BASE_INFO(hash table). Remove
4076 * it!
4077 */
4078 LIST_REMOVE(inp, sctp_hash);
4079 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
4080 }
4081
4082 /* If there is a timer running to kill us,
4083 * forget it, since it may have a contest
4084 * on the INP lock.. which would cause us
4085 * to die ...
4086 */
4087 cnt = 0;
Michael Tuexen2292a602022-04-07 17:49:53 +02004088 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
4089 SCTP_TCB_LOCK(stcb);
Michael Tuexen547d3b42020-07-23 21:49:32 +02004090 if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
4091 /* Disconnect the socket please */
Michael Tuexen2292a602022-04-07 17:49:53 +02004092 stcb->sctp_socket = NULL;
4093 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
Michael Tuexen547d3b42020-07-23 21:49:32 +02004094 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004095 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4096 if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
4097 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
4098 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
tuexendd729232011-11-01 23:04:43 +00004099 }
Michael Tuexen61f66832020-02-09 23:15:19 +01004100 cnt++;
Michael Tuexen2292a602022-04-07 17:49:53 +02004101 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00004102 continue;
4103 }
4104 /* Free associations that are NOT killing us */
Michael Tuexen2292a602022-04-07 17:49:53 +02004105 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) &&
4106 ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
tuexendd729232011-11-01 23:04:43 +00004107 struct mbuf *op_err;
t00fcxen28611aa2012-09-23 07:45:40 +00004108
t00fcxen08f9ff92014-03-16 13:38:54 +00004109 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
Michael Tuexen2292a602022-04-07 17:49:53 +02004110 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
4111 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00004112 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen2292a602022-04-07 17:49:53 +02004113 } else if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexendd729232011-11-01 23:04:43 +00004114 cnt++;
Michael Tuexen2292a602022-04-07 17:49:53 +02004115 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00004116 continue;
4117 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004118 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4119 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004120 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4121 }
Michael Tuexen2292a602022-04-07 17:49:53 +02004122 if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00004123 SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
tuexendd729232011-11-01 23:04:43 +00004124 cnt++;
4125 }
4126 }
4127 if (cnt) {
4128 /* Ok we have someone out there that will kill us */
tuexendd729232011-11-01 23:04:43 +00004129#ifdef SCTP_LOG_CLOSING
4130 sctp_log_closing(inp, NULL, 3);
4131#endif
4132 SCTP_INP_WUNLOCK(inp);
4133 SCTP_ASOC_CREATE_UNLOCK(inp);
4134 SCTP_INP_INFO_WUNLOCK();
4135 return;
4136 }
4137 if (SCTP_INP_LOCK_CONTENDED(inp))
4138 being_refed++;
4139 if (SCTP_INP_READ_CONTENDED(inp))
4140 being_refed++;
tuexen63fc0bb2011-12-27 12:24:52 +00004141 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
tuexendd729232011-11-01 23:04:43 +00004142 being_refed++;
Michael Tuexen939d48f2020-07-19 14:44:43 +02004143 /* NOTE: 0 refcount also means no timers are referencing us. */
tuexen63fc0bb2011-12-27 12:24:52 +00004144 if ((inp->refcount) ||
t00fcxen28611aa2012-09-23 07:45:40 +00004145 (being_refed) ||
4146 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
tuexendd729232011-11-01 23:04:43 +00004147#ifdef SCTP_LOG_CLOSING
4148 sctp_log_closing(inp, NULL, 4);
4149#endif
4150 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4151 SCTP_INP_WUNLOCK(inp);
4152 SCTP_ASOC_CREATE_UNLOCK(inp);
4153 SCTP_INP_INFO_WUNLOCK();
4154 return;
4155 }
4156 inp->sctp_ep.signature_change.type = 0;
4157 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4158 /* Remove it from the list .. last thing we need a
4159 * lock for.
4160 */
4161 LIST_REMOVE(inp, sctp_list);
4162 SCTP_INP_WUNLOCK(inp);
4163 SCTP_ASOC_CREATE_UNLOCK(inp);
4164 SCTP_INP_INFO_WUNLOCK();
tuexendd729232011-11-01 23:04:43 +00004165
4166#ifdef SCTP_LOG_CLOSING
4167 sctp_log_closing(inp, NULL, 5);
4168#endif
Michael Tuexeneb611572020-06-13 17:27:02 +02004169#if !(defined(_WIN32) || defined(__Userspace__))
Michael Tuexen8d6935d2022-05-15 01:51:24 +02004170#if !(defined(__FreeBSD__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004171 rt = ip_pcb->inp_route.ro_rt;
4172#endif
4173#endif
tuexendd729232011-11-01 23:04:43 +00004174 if ((inp->sctp_asocidhash) != NULL) {
4175 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4176 inp->sctp_asocidhash = NULL;
4177 }
4178 /*sa_ignore FREED_MEMORY*/
4179 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4180 /* Its only abandoned if it had data left */
4181 if (sq->length)
4182 SCTP_STAT_INCR(sctps_left_abandon);
4183
4184 TAILQ_REMOVE(&inp->read_queue, sq, next);
4185 sctp_free_remote_addr(sq->whoFrom);
4186 if (so)
4187 so->so_rcv.sb_cc -= sq->length;
4188 if (sq->data) {
4189 sctp_m_freem(sq->data);
4190 sq->data = NULL;
4191 }
4192 /*
4193 * no need to free the net count, since at this point all
4194 * assoc's are gone.
4195 */
Michael Tuexene5001952016-04-17 19:25:27 +02004196 sctp_free_a_readq(NULL, sq);
tuexendd729232011-11-01 23:04:43 +00004197 }
4198 /* Now the sctp_pcb things */
4199 /*
4200 * free each asoc if it is not already closed/free. we can't use the
4201 * macro here since le_next will get freed as part of the
4202 * sctp_free_assoc() call.
4203 */
tuexendd729232011-11-01 23:04:43 +00004204 if (ip_pcb->inp_options) {
4205 (void)sctp_m_free(ip_pcb->inp_options);
4206 ip_pcb->inp_options = 0;
4207 }
Michael Tuexeneb611572020-06-13 17:27:02 +02004208#if !(defined(_WIN32) || defined(__Userspace__))
Michael Tuexena5089952020-06-09 23:11:08 +02004209#if !defined(__FreeBSD__)
tuexendd729232011-11-01 23:04:43 +00004210 if (rt) {
4211 RTFREE(rt);
4212 ip_pcb->inp_route.ro_rt = 0;
4213 }
4214#endif
tuexendd729232011-11-01 23:04:43 +00004215#endif
tuexendd729232011-11-01 23:04:43 +00004216#ifdef INET6
Michael Tuexeneb611572020-06-13 17:27:02 +02004217#if !(defined(_WIN32) || defined(__Userspace__))
Michael Tuexen5be0c252020-06-13 00:53:56 +02004218#if (defined(__FreeBSD__) || defined(__APPLE__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004219 if (ip_pcb->inp_vflag & INP_IPV6) {
t00fcxen4c76a912012-09-14 14:31:21 +00004220#else
4221 if (inp->inp_vflag & INP_IPV6) {
tuexendd729232011-11-01 23:04:43 +00004222#endif
Michael Tuexen801a64b2019-08-05 15:33:37 +02004223 ip6_freepcbopts(ip_pcb->in6p_outputopts);
tuexendd729232011-11-01 23:04:43 +00004224 }
t00fcxen4c76a912012-09-14 14:31:21 +00004225#endif
tuexendd729232011-11-01 23:04:43 +00004226#endif /* INET6 */
tuexendd729232011-11-01 23:04:43 +00004227 ip_pcb->inp_vflag = 0;
tuexendd729232011-11-01 23:04:43 +00004228 /* free up authentication fields */
4229 if (inp->sctp_ep.local_auth_chunks != NULL)
4230 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4231 if (inp->sctp_ep.local_hmacs != NULL)
4232 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4233
4234 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4235 LIST_REMOVE(shared_key, next);
4236 sctp_free_sharedkey(shared_key);
4237 /*sa_ignore FREED_MEMORY*/
4238 }
4239
Michael Tuexen5be0c252020-06-13 00:53:56 +02004240#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004241 inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
tuexen6d01b942012-05-04 18:15:17 +00004242 if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
tuexen49fdcc82012-05-04 10:13:12 +00004243#ifdef INVARIANTS
Michael Tuexene19d1222022-08-03 17:35:14 +02004244 panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING", (void *)inp);
tuexen6d01b942012-05-04 18:15:17 +00004245#else
t00fcxen8fca7ff2012-09-05 18:58:42 +00004246 SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen49fdcc82012-05-04 10:13:12 +00004247#endif
tuexen6d01b942012-05-04 18:15:17 +00004248 }
tuexendd729232011-11-01 23:04:43 +00004249 inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4250#endif
4251 /*
4252 * if we have an address list the following will free the list of
4253 * ifaddr's that are set into this ep. Again macro limitations here,
4254 * since the LIST_FOREACH could be a bad idea.
4255 */
4256 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4257 sctp_remove_laddr(laddr);
4258 }
4259
4260#ifdef SCTP_TRACK_FREED_ASOCS
4261 /* TEMP CODE */
Michael Tuexen2292a602022-04-07 17:49:53 +02004262 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_free_list, sctp_tcblist, nstcb) {
4263 LIST_REMOVE(stcb, sctp_tcblist);
4264 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
tuexendd729232011-11-01 23:04:43 +00004265 SCTP_DECR_ASOC_COUNT();
4266 }
4267 /* *** END TEMP CODE ****/
4268#endif
4269#ifdef SCTP_MVRF
4270 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4271#endif
4272 /* Now lets see about freeing the EP hash table. */
4273 if (inp->sctp_tcbhash != NULL) {
4274 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4275 inp->sctp_tcbhash = NULL;
4276 }
4277 /* Now we must put the ep memory back into the zone pool */
Michael Tuexen5be0c252020-06-13 00:53:56 +02004278#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen326aa4b2014-06-20 13:42:13 +00004279 crfree(inp->ip_inp.inp.inp_cred);
tuexendd729232011-11-01 23:04:43 +00004280 INP_LOCK_DESTROY(&inp->ip_inp.inp);
4281#endif
4282 SCTP_INP_LOCK_DESTROY(inp);
4283 SCTP_INP_READ_DESTROY(inp);
4284 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004285#if !(defined(__APPLE__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004286 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4287 SCTP_DECR_EP_COUNT();
4288#else
4289 /* For Tiger, we will do this later... */
4290#endif
4291}
4292
tuexendd729232011-11-01 23:04:43 +00004293struct sctp_nets *
4294sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4295{
4296 struct sctp_nets *net;
4297 /* locate the address */
4298 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4299 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4300 return (net);
4301 }
4302 return (NULL);
4303}
4304
tuexendd729232011-11-01 23:04:43 +00004305int
4306sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4307{
tuexendd729232011-11-01 23:04:43 +00004308 struct sctp_ifa *sctp_ifa;
4309 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4310 if (sctp_ifa) {
4311 return (1);
4312 } else {
4313 return (0);
4314 }
tuexendd729232011-11-01 23:04:43 +00004315}
4316
4317/*
4318 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4319 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4320 * stats of stuff.
4321 */
4322int
4323sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
Michael Tuexen27574422016-04-30 16:35:29 +02004324 struct sctp_nets **netp, uint16_t port, int set_scope, int from)
tuexendd729232011-11-01 23:04:43 +00004325{
4326 /*
4327 * The following is redundant to the same lines in the
4328 * sctp_aloc_assoc() but is needed since others call the add
4329 * address function
4330 */
4331 struct sctp_nets *net, *netfirst;
4332 int addr_inscope;
4333
4334 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4335 from);
4336 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4337
4338 netfirst = sctp_findnet(stcb, newaddr);
4339 if (netfirst) {
4340 /*
4341 * Lie and return ok, we don't want to make the association
4342 * go away for this behavior. It will happen in the TCP
4343 * model in a connected socket. It does not reach the hash
4344 * table until after the association is built so it can't be
4345 * found. Mark as reachable, since the initial creation will
4346 * have been cleared and the NOT_IN_ASSOC flag will have
4347 * been added... and we don't want to end up removing it
4348 * back out.
4349 */
4350 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4351 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4352 SCTP_ADDR_UNCONFIRMED);
4353 } else {
4354 netfirst->dest_state = SCTP_ADDR_REACHABLE;
4355 }
4356
4357 return (0);
4358 }
4359 addr_inscope = 1;
4360 switch (newaddr->sa_family) {
4361#ifdef INET
4362 case AF_INET:
4363 {
4364 struct sockaddr_in *sin;
4365
4366 sin = (struct sockaddr_in *)newaddr;
4367 if (sin->sin_addr.s_addr == 0) {
4368 /* Invalid address */
4369 return (-1);
4370 }
Michael Tuexend0853302017-07-19 17:01:31 +02004371 /* zero out the zero area */
tuexendd729232011-11-01 23:04:43 +00004372 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4373
4374 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004375#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00004376 sin->sin_len = sizeof(struct sockaddr_in);
4377#endif
4378 if (set_scope) {
tuexendd729232011-11-01 23:04:43 +00004379 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004380 stcb->asoc.scope.ipv4_local_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004381 }
tuexendd729232011-11-01 23:04:43 +00004382 } else {
4383 /* Validate the address is in scope */
4384 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004385 (stcb->asoc.scope.ipv4_local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004386 addr_inscope = 0;
4387 }
4388 }
4389 break;
4390 }
4391#endif
4392#ifdef INET6
4393 case AF_INET6:
4394 {
4395 struct sockaddr_in6 *sin6;
4396
4397 sin6 = (struct sockaddr_in6 *)newaddr;
4398 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4399 /* Invalid address */
4400 return (-1);
4401 }
4402 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004403#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00004404 sin6->sin6_len = sizeof(struct sockaddr_in6);
4405#endif
4406 if (set_scope) {
4407 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004408 stcb->asoc.scope.loopback_scope = 1;
4409 stcb->asoc.scope.local_scope = 0;
4410 stcb->asoc.scope.ipv4_local_scope = 1;
4411 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004412 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4413 /*
4414 * If the new destination is a LINK_LOCAL we
4415 * must have common site scope. Don't set
4416 * the local scope since we may not share
4417 * all links, only loopback can do this.
4418 * Links on the local network would also be
4419 * on our private network for v4 too.
4420 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004421 stcb->asoc.scope.ipv4_local_scope = 1;
4422 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004423 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4424 /*
4425 * If the new destination is SITE_LOCAL then
4426 * we must have site scope in common.
4427 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004428 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004429 }
4430 } else {
4431 /* Validate the address is in scope */
4432 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004433 (stcb->asoc.scope.loopback_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004434 addr_inscope = 0;
4435 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004436 (stcb->asoc.scope.local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004437 addr_inscope = 0;
4438 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004439 (stcb->asoc.scope.site_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004440 addr_inscope = 0;
4441 }
4442 }
4443 break;
4444 }
4445#endif
tuexencc2b1422012-07-10 21:50:32 +00004446#if defined(__Userspace__)
4447 case AF_CONN:
4448 {
4449 struct sockaddr_conn *sconn;
4450
4451 sconn = (struct sockaddr_conn *)newaddr;
4452 if (sconn->sconn_addr == NULL) {
4453 /* Invalid address */
4454 return (-1);
4455 }
t00fcxen8fbaee32012-09-04 16:41:39 +00004456#ifdef HAVE_SCONN_LEN
tuexencc2b1422012-07-10 21:50:32 +00004457 sconn->sconn_len = sizeof(struct sockaddr_conn);
4458#endif
4459 break;
4460 }
4461#endif
tuexendd729232011-11-01 23:04:43 +00004462 default:
4463 /* not supported family type */
4464 return (-1);
4465 }
4466 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4467 if (net == NULL) {
4468 return (-1);
4469 }
4470 SCTP_INCR_RADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02004471 memset(net, 0, sizeof(struct sctp_nets));
tuexendd729232011-11-01 23:04:43 +00004472 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
t00fcxen907930b2012-09-12 19:43:22 +00004473#ifdef HAVE_SA_LEN
4474 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
4475#endif
tuexendd729232011-11-01 23:04:43 +00004476 switch (newaddr->sa_family) {
4477#ifdef INET
4478 case AF_INET:
t00fcxen907930b2012-09-12 19:43:22 +00004479#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004480 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4481#endif
4482 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4483 break;
4484#endif
4485#ifdef INET6
4486 case AF_INET6:
t00fcxen907930b2012-09-12 19:43:22 +00004487#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004488 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4489#endif
4490 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4491 break;
4492#endif
tuexencc2b1422012-07-10 21:50:32 +00004493#if defined(__Userspace__)
4494 case AF_CONN:
t00fcxen907930b2012-09-12 19:43:22 +00004495#ifndef HAVE_SA_LEN
tuexencc2b1422012-07-10 21:50:32 +00004496 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4497#endif
4498 ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4499 break;
4500#endif
tuexendd729232011-11-01 23:04:43 +00004501 default:
4502 break;
4503 }
4504 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4505 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004506 stcb->asoc.scope.loopback_scope = 1;
4507 stcb->asoc.scope.ipv4_local_scope = 1;
4508 stcb->asoc.scope.local_scope = 0;
4509 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004510 addr_inscope = 1;
4511 }
4512 net->failure_threshold = stcb->asoc.def_net_failure;
4513 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4514 if (addr_inscope == 0) {
4515 net->dest_state = (SCTP_ADDR_REACHABLE |
4516 SCTP_ADDR_OUT_OF_SCOPE);
4517 } else {
4518 if (from == SCTP_ADDR_IS_CONFIRMED)
4519 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4520 net->dest_state = SCTP_ADDR_REACHABLE;
4521 else
4522 net->dest_state = SCTP_ADDR_REACHABLE |
4523 SCTP_ADDR_UNCONFIRMED;
4524 }
4525 /* We set this to 0, the timer code knows that
4526 * this means its an initial value
4527 */
4528 net->rto_needed = 1;
t00fcxen8dd95b82014-07-11 20:41:28 +00004529 net->RTO = 0;
tuexendd729232011-11-01 23:04:43 +00004530 net->RTO_measured = 0;
4531 stcb->asoc.numnets++;
tuexen9d7b2072011-11-20 16:35:17 +00004532 net->ref_count = 1;
tuexendd729232011-11-01 23:04:43 +00004533 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
Michael Tuexen27574422016-04-30 16:35:29 +02004534 net->port = port;
tuexendd729232011-11-01 23:04:43 +00004535 net->dscp = stcb->asoc.default_dscp;
4536#ifdef INET6
4537 net->flowlabel = stcb->asoc.default_flowlabel;
4538#endif
4539 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4540 net->dest_state |= SCTP_ADDR_NOHB;
4541 } else {
4542 net->dest_state &= ~SCTP_ADDR_NOHB;
4543 }
4544 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4545 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4546 } else {
4547 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4548 }
4549 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4550 /* Init the timer structure */
4551 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4552 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4553 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4554
4555 /* Now generate a route for this guy */
4556#ifdef INET6
4557#ifdef SCTP_EMBEDDED_V6_SCOPE
4558 /* KAME hack: embed scopeid */
4559 if (newaddr->sa_family == AF_INET6) {
4560 struct sockaddr_in6 *sin6;
4561
4562 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004563#if defined(__APPLE__) && !defined(__Userspace__)
tuexen9a218b12012-08-04 21:17:58 +00004564#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4565 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
tuexendd729232011-11-01 23:04:43 +00004566#else
tuexen9a218b12012-08-04 21:17:58 +00004567 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
tuexendd729232011-11-01 23:04:43 +00004568#endif
4569#elif defined(SCTP_KAME)
4570 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4571#else
4572 (void)in6_embedscope(&sin6->sin6_addr, sin6);
4573#endif
4574#ifndef SCOPEDROUTING
4575 sin6->sin6_scope_id = 0;
4576#endif
4577 }
4578#endif /* SCTP_EMBEDDED_V6_SCOPE */
4579#endif
t00fcxena51e4462015-06-17 15:53:23 +00004580 SCTP_RTALLOC((sctp_route_t *)&net->ro,
4581 stcb->asoc.vrf_id,
4582 stcb->sctp_ep->fibnum);
tuexendd729232011-11-01 23:04:43 +00004583
t00fcxen8b6ef092014-07-11 17:37:40 +00004584 net->src_addr_selected = 0;
Michael Tuexend900e5f2017-06-23 10:52:43 +02004585#if !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004586 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4587 /* Get source address */
4588 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004589 stcb,
4590 (sctp_route_t *)&net->ro,
4591 net,
4592 0,
4593 stcb->asoc.vrf_id);
Michael Tuexende0630d2017-11-03 22:04:01 +01004594 if (stcb->asoc.default_mtu > 0) {
4595 net->mtu = stcb->asoc.default_mtu;
4596 switch (net->ro._l_addr.sa.sa_family) {
4597#ifdef INET
4598 case AF_INET:
4599 net->mtu += SCTP_MIN_V4_OVERHEAD;
4600 break;
4601#endif
4602#ifdef INET6
4603 case AF_INET6:
4604 net->mtu += SCTP_MIN_OVERHEAD;
4605 break;
4606#endif
4607#if defined(__Userspace__)
4608 case AF_CONN:
4609 net->mtu += sizeof(struct sctphdr);
4610 break;
4611#endif
4612 default:
4613 break;
4614 }
4615#if defined(INET) || defined(INET6)
4616 if (net->port) {
4617 net->mtu += (uint32_t)sizeof(struct udphdr);
4618 }
4619#endif
4620 } else if (net->ro._s_addr != NULL) {
Michael Tuexend900e5f2017-06-23 10:52:43 +02004621 uint32_t imtu, rmtu, hcmtu;
4622
t00fcxen8b6ef092014-07-11 17:37:40 +00004623 net->src_addr_selected = 1;
4624 /* Now get the interface MTU */
4625 if (net->ro._s_addr->ifn_p != NULL) {
Michael Tuexenb29c2202022-02-18 17:32:29 +01004626 /*
4627 * XXX: Should we here just use
4628 * net->ro._s_addr->ifn_p->ifn_mtu
4629 */
4630 imtu = SCTP_GATHER_MTU_FROM_IFN_INFO(net->ro._s_addr->ifn_p->ifn_p,
4631 net->ro._s_addr->ifn_p->ifn_index);
Michael Tuexend900e5f2017-06-23 10:52:43 +02004632 } else {
4633 imtu = 0;
t00fcxen8b6ef092014-07-11 17:37:40 +00004634 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02004635#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004636 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 +02004637 hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
4638#else
Michael Tuexena0515422020-05-18 00:47:04 +02004639 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 +02004640 hcmtu = 0;
4641#endif
4642 net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004643#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004644#else
tuexendd729232011-11-01 23:04:43 +00004645 if (rmtu == 0) {
4646 /* Start things off to match mtu of interface please. */
4647 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004648 net->ro.ro_rt, net->mtu);
tuexendd729232011-11-01 23:04:43 +00004649 }
Michael Tuexena0515422020-05-18 00:47:04 +02004650#endif
t00fcxen8b6ef092014-07-11 17:37:40 +00004651 }
tuexen09631162012-04-18 11:23:02 +00004652 }
t00fcxen6d60eb02013-08-30 07:37:01 +00004653#endif
tuexene81177b2011-11-15 20:48:08 +00004654 if (net->mtu == 0) {
Michael Tuexende0630d2017-11-03 22:04:01 +01004655 if (stcb->asoc.default_mtu > 0) {
4656 net->mtu = stcb->asoc.default_mtu;
4657 switch (net->ro._l_addr.sa.sa_family) {
tuexene81177b2011-11-15 20:48:08 +00004658#ifdef INET
Michael Tuexende0630d2017-11-03 22:04:01 +01004659 case AF_INET:
4660 net->mtu += SCTP_MIN_V4_OVERHEAD;
4661 break;
tuexene81177b2011-11-15 20:48:08 +00004662#endif
4663#ifdef INET6
Michael Tuexende0630d2017-11-03 22:04:01 +01004664 case AF_INET6:
4665 net->mtu += SCTP_MIN_OVERHEAD;
4666 break;
tuexene81177b2011-11-15 20:48:08 +00004667#endif
tuexencc2b1422012-07-10 21:50:32 +00004668#if defined(__Userspace__)
Michael Tuexende0630d2017-11-03 22:04:01 +01004669 case AF_CONN:
4670 net->mtu += sizeof(struct sctphdr);
4671 break;
tuexencc2b1422012-07-10 21:50:32 +00004672#endif
Michael Tuexende0630d2017-11-03 22:04:01 +01004673 default:
4674 break;
4675 }
4676#if defined(INET) || defined(INET6)
4677 if (net->port) {
4678 net->mtu += (uint32_t)sizeof(struct udphdr);
4679 }
4680#endif
4681 } else {
4682 switch (newaddr->sa_family) {
4683#ifdef INET
4684 case AF_INET:
4685 net->mtu = SCTP_DEFAULT_MTU;
4686 break;
4687#endif
4688#ifdef INET6
4689 case AF_INET6:
4690 net->mtu = 1280;
4691 break;
4692#endif
4693#if defined(__Userspace__)
4694 case AF_CONN:
4695 net->mtu = 1280;
4696 break;
4697#endif
4698 default:
4699 break;
4700 }
tuexendd729232011-11-01 23:04:43 +00004701 }
tuexene81177b2011-11-15 20:48:08 +00004702 }
t00fcxen1ce83bb2014-07-11 07:17:36 +00004703#if defined(INET) || defined(INET6)
tuexene81177b2011-11-15 20:48:08 +00004704 if (net->port) {
4705 net->mtu -= (uint32_t)sizeof(struct udphdr);
4706 }
t00fcxen5ab37c82014-06-30 20:55:44 +00004707#endif
tuexene81177b2011-11-15 20:48:08 +00004708 if (from == SCTP_ALLOC_ASOC) {
4709 stcb->asoc.smallest_mtu = net->mtu;
4710 }
4711 if (stcb->asoc.smallest_mtu > net->mtu) {
Michael Tuexena40c76b2021-12-30 15:53:03 +01004712 sctp_pathmtu_adjustment(stcb, net->mtu, true);
tuexendd729232011-11-01 23:04:43 +00004713 }
4714#ifdef INET6
4715#ifdef SCTP_EMBEDDED_V6_SCOPE
4716 if (newaddr->sa_family == AF_INET6) {
4717 struct sockaddr_in6 *sin6;
4718
4719 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4720#ifdef SCTP_KAME
4721 (void)sa6_recoverscope(sin6);
4722#else
4723 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4724#endif /* SCTP_KAME */
4725 }
4726#endif /* SCTP_EMBEDDED_V6_SCOPE */
4727#endif
tuexendd729232011-11-01 23:04:43 +00004728
4729 /* JRS - Use the congestion control given in the CC module */
tuexen09631162012-04-18 11:23:02 +00004730 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
tuexendd729232011-11-01 23:04:43 +00004731 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4732
4733 /*
4734 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4735 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4736 */
4737 net->find_pseudo_cumack = 1;
4738 net->find_rtx_pseudo_cumack = 1;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004739#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004740 /* Choose an initial flowid. */
4741 net->flowid = stcb->asoc.my_vtag ^
4742 ntohs(stcb->rport) ^
4743 ntohs(stcb->sctp_ep->sctp_lport);
Michael Tuexen15f35f82016-06-07 09:19:50 +02004744 net->flowtype = M_HASHTYPE_OPAQUE_HASH;
tuexendd729232011-11-01 23:04:43 +00004745#endif
4746 if (netp) {
4747 *netp = net;
4748 }
4749 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004750#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004751 if (net->ro.ro_nh == NULL) {
4752#else
tuexendd729232011-11-01 23:04:43 +00004753 if (net->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004754#endif
tuexendd729232011-11-01 23:04:43 +00004755 /* Since we have no route put it at the back */
4756 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4757 } else if (netfirst == NULL) {
4758 /* We are the first one in the pool. */
4759 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004760#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004761 } else if (netfirst->ro.ro_nh == NULL) {
4762#else
tuexendd729232011-11-01 23:04:43 +00004763 } else if (netfirst->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004764#endif
tuexendd729232011-11-01 23:04:43 +00004765 /*
4766 * First one has NO route. Place this one ahead of the first
4767 * one.
4768 */
4769 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexen5be0c252020-06-13 00:53:56 +02004770#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004771 } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
4772#else
tuexendd729232011-11-01 23:04:43 +00004773 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
Michael Tuexena0515422020-05-18 00:47:04 +02004774#endif
tuexendd729232011-11-01 23:04:43 +00004775 /*
4776 * This one has a different interface than the one at the
4777 * top of the list. Place it ahead.
4778 */
4779 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004780 } else {
4781 /*
4782 * Ok we have the same interface as the first one. Move
4783 * forward until we find either a) one with a NULL route...
4784 * insert ahead of that b) one with a different ifp.. insert
4785 * after that. c) end of the list.. insert at the tail.
4786 */
4787 struct sctp_nets *netlook;
4788
4789 do {
4790 netlook = TAILQ_NEXT(netfirst, sctp_next);
4791 if (netlook == NULL) {
4792 /* End of the list */
4793 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4794 break;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004795#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004796 } else if (netlook->ro.ro_nh == NULL) {
4797#else
tuexendd729232011-11-01 23:04:43 +00004798 } else if (netlook->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004799#endif
tuexendd729232011-11-01 23:04:43 +00004800 /* next one has NO route */
4801 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4802 break;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004803#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004804 } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
tuexendd729232011-11-01 23:04:43 +00004805#else
Michael Tuexena0515422020-05-18 00:47:04 +02004806 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
tuexendd729232011-11-01 23:04:43 +00004807#endif
tuexendd729232011-11-01 23:04:43 +00004808 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
Michael Tuexena0515422020-05-18 00:47:04 +02004809 net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004810 break;
4811 }
tuexendd729232011-11-01 23:04:43 +00004812 /* Shift forward */
4813 netfirst = netlook;
tuexendd729232011-11-01 23:04:43 +00004814 } while (netlook != NULL);
4815 }
4816
4817 /* got to have a primary set */
4818 if (stcb->asoc.primary_destination == 0) {
4819 stcb->asoc.primary_destination = net;
Michael Tuexen5be0c252020-06-13 00:53:56 +02004820#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02004821 } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
4822 (net->ro.ro_nh) &&
4823#else
tuexendd729232011-11-01 23:04:43 +00004824 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
Michael Tuexena0515422020-05-18 00:47:04 +02004825 (net->ro.ro_rt) &&
4826#endif
4827 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
tuexendd729232011-11-01 23:04:43 +00004828 /* No route to current primary adopt new primary */
4829 stcb->asoc.primary_destination = net;
4830 }
4831 /* Validate primary is first */
4832 net = TAILQ_FIRST(&stcb->asoc.nets);
4833 if ((net != stcb->asoc.primary_destination) &&
4834 (stcb->asoc.primary_destination)) {
4835 /* first one on the list is NOT the primary
4836 * sctp_cmpaddr() is much more efficient if
4837 * the primary is the first on the list, make it
4838 * so.
4839 */
4840 TAILQ_REMOVE(&stcb->asoc.nets,
4841 stcb->asoc.primary_destination, sctp_next);
4842 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4843 stcb->asoc.primary_destination, sctp_next);
4844 }
4845 return (0);
4846}
4847
tuexendd729232011-11-01 23:04:43 +00004848static uint32_t
4849sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4850{
4851 uint32_t id;
4852 struct sctpasochead *head;
4853 struct sctp_tcb *lstcb;
4854
tuexendd729232011-11-01 23:04:43 +00004855 try_again:
4856 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4857 /* TSNH */
tuexendd729232011-11-01 23:04:43 +00004858 return (0);
4859 }
4860 /*
4861 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4862 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4863 */
4864 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4865 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4866 }
4867 id = inp->sctp_associd_counter;
4868 inp->sctp_associd_counter++;
4869 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4870 if (lstcb) {
4871 goto try_again;
4872 }
4873 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4874 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4875 stcb->asoc.in_asocid_hash = 1;
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01004876 return (id);
tuexendd729232011-11-01 23:04:43 +00004877}
4878
4879/*
4880 * allocate an association and add it to the endpoint. The caller must be
4881 * careful to add all additional addresses once they are know right away or
4882 * else the assoc will be may experience a blackout scenario.
4883 */
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004884static struct sctp_tcb *
4885sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4886 int *error, uint32_t override_tag, uint32_t initial_tsn,
4887 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
Michael Tuexen5be0c252020-06-13 00:53:56 +02004888#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004889 struct thread *p,
Michael Tuexeneb611572020-06-13 17:27:02 +02004890#elif defined(_WIN32) && !defined(__Userspace__)
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004891 PKTHREAD p,
tuexendd729232011-11-01 23:04:43 +00004892#else
4893#if defined(__Userspace__)
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004894 /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
tuexendd729232011-11-01 23:04:43 +00004895#endif
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004896 struct proc *p,
tuexendd729232011-11-01 23:04:43 +00004897#endif
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004898 int initialize_auth_params)
tuexendd729232011-11-01 23:04:43 +00004899{
4900 /* note the p argument is only valid in unbound sockets */
4901
4902 struct sctp_tcb *stcb;
4903 struct sctp_association *asoc;
4904 struct sctpasochead *head;
4905 uint16_t rport;
4906 int err;
4907
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004908 SCTP_INP_INFO_WLOCK_ASSERT();
4909 SCTP_INP_WLOCK_ASSERT(inp);
4910
tuexendd729232011-11-01 23:04:43 +00004911 /*
4912 * Assumption made here: Caller has done a
4913 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4914 * address does not exist already.
4915 */
4916 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4917 /* Hit max assoc, sorry no more */
4918 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4919 *error = ENOBUFS;
4920 return (NULL);
4921 }
4922 if (firstaddr == NULL) {
4923 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4924 *error = EINVAL;
4925 return (NULL);
4926 }
Michael Tuexen8dd0be72021-09-12 19:27:59 +02004927 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4928 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4929 *error = EINVAL;
4930 return (NULL);
4931 }
tuexendd729232011-11-01 23:04:43 +00004932 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4933 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4934 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4935 /*
4936 * If its in the TCP pool, its NOT allowed to create an
4937 * association. The parent listener needs to call
4938 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4939 * off, or connected one does this.. its an error.
4940 */
tuexendd729232011-11-01 23:04:43 +00004941 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4942 *error = EINVAL;
4943 return (NULL);
4944 }
4945 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4946 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4947 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4948 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
tuexendd729232011-11-01 23:04:43 +00004949 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4950 *error = EINVAL;
4951 return (NULL);
4952 }
4953 }
4954 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4955#ifdef SCTP_DEBUG
4956 if (firstaddr) {
4957 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4958 switch (firstaddr->sa_family) {
4959#ifdef INET
4960 case AF_INET:
4961 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4962 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4963 break;
4964#endif
4965#ifdef INET6
4966 case AF_INET6:
4967 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4968 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4969 break;
4970#endif
tuexencc2b1422012-07-10 21:50:32 +00004971#if defined(__Userspace__)
4972 case AF_CONN:
4973 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4974 ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
4975 break;
4976#endif
tuexendd729232011-11-01 23:04:43 +00004977 default:
4978 break;
4979 }
4980 } else {
4981 SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
4982 }
4983#endif /* SCTP_DEBUG */
4984 switch (firstaddr->sa_family) {
4985#ifdef INET
4986 case AF_INET:
4987 {
4988 struct sockaddr_in *sin;
4989
4990 sin = (struct sockaddr_in *)firstaddr;
4991 if ((ntohs(sin->sin_port) == 0) ||
4992 (sin->sin_addr.s_addr == INADDR_ANY) ||
4993 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
Michael Tuexend844a692020-09-29 11:38:11 +02004994 IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) ||
4995#if defined(__Userspace__)
Michael Tuexen8d6935d2022-05-15 01:51:24 +02004996 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) ||
4997 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexend844a692020-09-29 11:38:11 +02004998 (SCTP_IPV6_V6ONLY(inp) != 0)))) {
4999#else
Michael Tuexen8d6935d2022-05-15 01:51:24 +02005000 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexend844a692020-09-29 11:38:11 +02005001 (SCTP_IPV6_V6ONLY(inp) != 0))) {
5002#endif
tuexendd729232011-11-01 23:04:43 +00005003 /* Invalid address */
tuexendd729232011-11-01 23:04:43 +00005004 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5005 *error = EINVAL;
5006 return (NULL);
5007 }
5008 rport = sin->sin_port;
5009 break;
5010 }
5011#endif
5012#ifdef INET6
5013 case AF_INET6:
5014 {
5015 struct sockaddr_in6 *sin6;
5016
5017 sin6 = (struct sockaddr_in6 *)firstaddr;
5018 if ((ntohs(sin6->sin6_port) == 0) ||
5019 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
Michael Tuexend844a692020-09-29 11:38:11 +02005020 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) ||
5021 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) {
tuexendd729232011-11-01 23:04:43 +00005022 /* Invalid address */
tuexendd729232011-11-01 23:04:43 +00005023 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5024 *error = EINVAL;
5025 return (NULL);
5026 }
5027 rport = sin6->sin6_port;
5028 break;
5029 }
5030#endif
tuexencc2b1422012-07-10 21:50:32 +00005031#if defined(__Userspace__)
5032 case AF_CONN:
5033 {
5034 struct sockaddr_conn *sconn;
5035
5036 sconn = (struct sockaddr_conn *)firstaddr;
5037 if ((ntohs(sconn->sconn_port) == 0) ||
Michael Tuexend844a692020-09-29 11:38:11 +02005038 (sconn->sconn_addr == NULL) ||
5039 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) == 0)) {
tuexencc2b1422012-07-10 21:50:32 +00005040 /* Invalid address */
tuexencc2b1422012-07-10 21:50:32 +00005041 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5042 *error = EINVAL;
5043 return (NULL);
5044 }
5045 rport = sconn->sconn_port;
5046 break;
5047 }
5048#endif
tuexendd729232011-11-01 23:04:43 +00005049 default:
5050 /* not supported family type */
tuexendd729232011-11-01 23:04:43 +00005051 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5052 *error = EINVAL;
5053 return (NULL);
5054 }
tuexendd729232011-11-01 23:04:43 +00005055 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
5056 /*
5057 * If you have not performed a bind, then we need to do the
5058 * ephemeral bind for you.
5059 */
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005060 if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) {
tuexendd729232011-11-01 23:04:43 +00005061 /* bind error, probably perm */
5062 *error = err;
5063 return (NULL);
5064 }
5065 }
5066 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
5067 if (stcb == NULL) {
5068 /* out of memory? */
5069 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
5070 *error = ENOMEM;
5071 return (NULL);
5072 }
5073 SCTP_INCR_ASOC_COUNT();
5074
Michael Tuexend0853302017-07-19 17:01:31 +02005075 memset(stcb, 0, sizeof(*stcb));
tuexendd729232011-11-01 23:04:43 +00005076 asoc = &stcb->asoc;
5077
tuexendd729232011-11-01 23:04:43 +00005078 SCTP_TCB_LOCK_INIT(stcb);
tuexendd729232011-11-01 23:04:43 +00005079 stcb->rport = rport;
5080 /* setup back pointer's */
5081 stcb->sctp_ep = inp;
5082 stcb->sctp_socket = inp->sctp_socket;
Michael Tuexen965b19a2021-06-27 19:57:20 +02005083 if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) {
tuexendd729232011-11-01 23:04:43 +00005084 /* failed */
5085 SCTP_TCB_LOCK_DESTROY(stcb);
tuexendd729232011-11-01 23:04:43 +00005086 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5087 SCTP_DECR_ASOC_COUNT();
5088 *error = err;
5089 return (NULL);
5090 }
tuexendd729232011-11-01 23:04:43 +00005091 SCTP_TCB_LOCK(stcb);
5092
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01005093 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
tuexendd729232011-11-01 23:04:43 +00005094 /* now that my_vtag is set, add it to the hash */
5095 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
5096 /* put it in the bucket in the vtag hash of assoc's for the system */
5097 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
tuexendd729232011-11-01 23:04:43 +00005098
Michael Tuexen5cd6fe92020-10-06 13:14:37 +02005099 if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) {
tuexendd729232011-11-01 23:04:43 +00005100 /* failure.. memory error? */
5101 if (asoc->strmout) {
5102 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5103 asoc->strmout = NULL;
5104 }
5105 if (asoc->mapping_array) {
5106 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5107 asoc->mapping_array = NULL;
5108 }
5109 if (asoc->nr_mapping_array) {
5110 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5111 asoc->nr_mapping_array = NULL;
5112 }
5113 SCTP_DECR_ASOC_COUNT();
t00fcxen2c2ff922013-11-03 14:03:35 +00005114 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005115 SCTP_TCB_LOCK_DESTROY(stcb);
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005116 LIST_REMOVE(stcb, sctp_asocs);
tuexendd729232011-11-01 23:04:43 +00005117 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5118 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5119 SCTP_INP_WUNLOCK(inp);
5120 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
5121 *error = ENOBUFS;
5122 return (NULL);
5123 }
5124 /* Init all the timers */
5125 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
5126 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
5127 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
5128 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
5129 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
tuexendd729232011-11-01 23:04:43 +00005130 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5131
5132 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5133 /* now file the port under the hash as well */
5134 if (inp->sctp_tcbhash != NULL) {
5135 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5136 inp->sctp_hashmark)];
5137 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5138 }
Michael Tuexenbfb04f32019-07-14 14:42:17 +02005139 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
5140 sctp_initialize_auth_params(inp, stcb);
5141 }
t00fcxen8fca7ff2012-09-05 18:58:42 +00005142 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
tuexendd729232011-11-01 23:04:43 +00005143 return (stcb);
5144}
5145
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005146struct sctp_tcb *
5147sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
5148 int *error, uint32_t override_tag, uint32_t initial_tsn,
5149 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
5150#if defined(__FreeBSD__) && !defined(__Userspace__)
5151 struct thread *p,
5152#elif defined(_WIN32) && !defined(__Userspace__)
5153 PKTHREAD p,
5154#else
5155 struct proc *p,
5156#endif
5157 int initialize_auth_params)
5158{
5159 struct sctp_tcb *stcb;
5160
5161 SCTP_INP_INFO_WLOCK();
5162 SCTP_INP_WLOCK(inp);
5163 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
5164 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
5165 SCTP_INP_INFO_WUNLOCK();
5166 SCTP_INP_WUNLOCK(inp);
5167 return (stcb);
5168}
5169
5170struct sctp_tcb *
5171sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
5172 int *error, uint32_t override_tag, uint32_t initial_tsn,
5173 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
5174#if defined(__FreeBSD__) && !defined(__Userspace__)
5175 struct thread *p,
5176#elif defined(_WIN32) && !defined(__Userspace__)
5177 PKTHREAD p,
5178#else
5179 struct proc *p,
5180#endif
5181 int initialize_auth_params)
5182{
5183 struct sctp_tcb *stcb;
5184
5185 SCTP_INP_INFO_WLOCK();
5186 SCTP_INP_WLOCK(inp);
5187 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5188 SCTP_IS_LISTENING(inp)) {
5189 SCTP_INP_INFO_WUNLOCK();
5190 SCTP_INP_WUNLOCK(inp);
5191 *error = EINVAL;
5192 return (NULL);
5193 }
5194 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
5195 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
5196 SCTP_INP_INFO_WUNLOCK();
5197 if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
5198 inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
5199 soisconnecting(inp->sctp_socket);
5200 }
5201 SCTP_INP_WUNLOCK(inp);
5202 return (stcb);
5203}
5204
tuexendd729232011-11-01 23:04:43 +00005205void
5206sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5207{
Michael Tuexen21b55df2020-02-09 23:46:32 +01005208 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00005209 struct sctp_association *asoc;
5210
Michael Tuexen21b55df2020-02-09 23:46:32 +01005211 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00005212 asoc = &stcb->asoc;
5213 asoc->numnets--;
5214 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5215 if (net == asoc->primary_destination) {
5216 /* Reset primary */
5217 struct sctp_nets *lnet;
5218
5219 lnet = TAILQ_FIRST(&asoc->nets);
5220 /* Mobility adaptation
5221 Ideally, if deleted destination is the primary, it becomes
5222 a fast retransmission trigger by the subsequent SET PRIMARY.
5223 (by micchie)
5224 */
5225 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005226 SCTP_MOBILITY_BASE) ||
tuexendd729232011-11-01 23:04:43 +00005227 sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005228 SCTP_MOBILITY_FASTHANDOFF)) {
tuexendd729232011-11-01 23:04:43 +00005229 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5230 if (asoc->deleted_primary != NULL) {
5231 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5232 goto out;
5233 }
5234 asoc->deleted_primary = net;
5235 atomic_add_int(&net->ref_count, 1);
5236 memset(&net->lastsa, 0, sizeof(net->lastsa));
5237 memset(&net->lastsv, 0, sizeof(net->lastsv));
5238 sctp_mobility_feature_on(stcb->sctp_ep,
5239 SCTP_MOBILITY_PRIM_DELETED);
5240 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5241 stcb->sctp_ep, stcb, NULL);
5242 }
5243out:
5244 /* Try to find a confirmed primary */
5245 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5246 }
5247 if (net == asoc->last_data_chunk_from) {
5248 /* Reset primary */
5249 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5250 }
5251 if (net == asoc->last_control_chunk_from) {
5252 /* Clear net */
5253 asoc->last_control_chunk_from = NULL;
5254 }
Michael Tuexen6e00fe22021-02-21 17:31:55 +01005255 if (net == asoc->last_net_cmt_send_started) {
5256 /* Clear net */
5257 asoc->last_net_cmt_send_started = NULL;
5258 }
tuexendd729232011-11-01 23:04:43 +00005259 if (net == stcb->asoc.alternate) {
5260 sctp_free_remote_addr(stcb->asoc.alternate);
5261 stcb->asoc.alternate = NULL;
5262 }
Michael Tuexen21b55df2020-02-09 23:46:32 +01005263 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5264 SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
5265 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5266 SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
Michael Tuexenf52d3df2020-02-11 19:24:34 +01005267 net->dest_state |= SCTP_ADDR_BEING_DELETED;
tuexendd729232011-11-01 23:04:43 +00005268 sctp_free_remote_addr(net);
5269}
5270
5271/*
5272 * remove a remote endpoint address from an association, it will fail if the
5273 * address does not exist.
5274 */
5275int
5276sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5277{
5278 /*
5279 * Here we need to remove a remote address. This is quite simple, we
5280 * first find it in the list of address for the association
5281 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5282 * on that item. Note we do not allow it to be removed if there are
5283 * no other addresses.
5284 */
5285 struct sctp_association *asoc;
5286 struct sctp_nets *net, *nnet;
5287
5288 asoc = &stcb->asoc;
5289
5290 /* locate the address */
5291 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5292 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5293 continue;
5294 }
5295 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5296 remaddr)) {
5297 /* we found the guy */
5298 if (asoc->numnets < 2) {
5299 /* Must have at LEAST two remote addresses */
5300 return (-1);
5301 } else {
5302 sctp_remove_net(stcb, net);
5303 return (0);
5304 }
5305 }
5306 }
5307 /* not found. */
5308 return (-2);
5309}
5310
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005311static bool
5312sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, uint32_t now)
tuexendd729232011-11-01 23:04:43 +00005313{
5314 struct sctpvtaghead *chain;
5315 struct sctp_tagblock *twait_block;
tuexendd729232011-11-01 23:04:43 +00005316 int i;
5317
Michael Tuexen8dd0be72021-09-12 19:27:59 +02005318 SCTP_INP_INFO_LOCK_ASSERT();
tuexendd729232011-11-01 23:04:43 +00005319 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
t00fcxen14f8fbb2013-06-25 09:32:47 +00005320 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5321 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005322 if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) &&
5323 (twait_block->vtag_block[i].v_tag == tag) &&
5324 (twait_block->vtag_block[i].lport == lport) &&
t00fcxen14f8fbb2013-06-25 09:32:47 +00005325 (twait_block->vtag_block[i].rport == rport)) {
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005326 return (true);
t00fcxen14f8fbb2013-06-25 09:32:47 +00005327 }
tuexendd729232011-11-01 23:04:43 +00005328 }
5329 }
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005330 return (false);
tuexendd729232011-11-01 23:04:43 +00005331}
5332
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005333static void
5334sctp_set_vtag_block(struct sctp_timewait *vtag_block, uint32_t time,
5335 uint32_t tag, uint16_t lport, uint16_t rport)
5336{
5337 vtag_block->tv_sec_at_expire = time;
5338 vtag_block->v_tag = tag;
5339 vtag_block->lport = lport;
5340 vtag_block->rport = rport;
5341}
5342
5343static void
5344sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
tuexendd729232011-11-01 23:04:43 +00005345{
5346 struct sctpvtaghead *chain;
5347 struct sctp_tagblock *twait_block;
5348 struct timeval now;
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005349 uint32_t time;
5350 int i;
5351 bool set;
tuexendd729232011-11-01 23:04:43 +00005352
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005353 SCTP_INP_INFO_WLOCK_ASSERT();
tuexendd729232011-11-01 23:04:43 +00005354 (void)SCTP_GETTIME_TIMEVAL(&now);
Michael Tuexen64707062021-09-09 20:08:10 +02005355 time = (uint32_t)now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait);
tuexendd729232011-11-01 23:04:43 +00005356 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005357 set = false;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005358 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
tuexendd729232011-11-01 23:04:43 +00005359 /* Block(s) present, lets find space, and expire on the fly */
t00fcxen14f8fbb2013-06-25 09:32:47 +00005360 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005361 if ((twait_block->vtag_block[i].v_tag == 0) && !set) {
5362 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
5363 set = true;
5364 continue;
5365 }
5366 if ((twait_block->vtag_block[i].v_tag != 0) &&
5367 (twait_block->vtag_block[i].tv_sec_at_expire < (uint32_t)now.tv_sec)) {
5368 if (set) {
5369 /* Audit expires this guy */
5370 sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0);
5371 } else {
5372 /* Reuse it for the new tag */
5373 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
5374 set = true;
tuexendd729232011-11-01 23:04:43 +00005375 }
5376 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005377 }
5378 if (set) {
5379 /*
5380 * We only do up to the block where we can
5381 * place our tag for audits
5382 */
5383 break;
tuexendd729232011-11-01 23:04:43 +00005384 }
5385 }
5386 /* Need to add a new block to chain */
5387 if (!set) {
5388 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5389 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5390 if (twait_block == NULL) {
tuexendd729232011-11-01 23:04:43 +00005391 return;
5392 }
5393 memset(twait_block, 0, sizeof(struct sctp_tagblock));
5394 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005395 sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport);
tuexendd729232011-11-01 23:04:43 +00005396 }
5397}
5398
Michael Tuexene5001952016-04-17 19:25:27 +02005399void
5400sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
5401{
5402 struct sctp_tmit_chunk *chk, *nchk;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005403 struct sctp_queued_to_read *control, *ncontrol;
5404
5405 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
5406 TAILQ_REMOVE(rh, control, next_instrm);
5407 control->on_strm_q = 0;
5408 if (control->on_read_q == 0) {
5409 sctp_free_remote_addr(control->whoFrom);
5410 if (control->data) {
5411 sctp_m_freem(control->data);
5412 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02005413 }
5414 }
5415 /* Reassembly free? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005416 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5417 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02005418 if (chk->data) {
5419 sctp_m_freem(chk->data);
5420 chk->data = NULL;
5421 }
5422 if (chk->holds_key_ref)
5423 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5424 sctp_free_remote_addr(chk->whoTo);
5425 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5426 SCTP_DECR_CHK_COUNT();
5427 /*sa_ignore FREED_MEMORY*/
5428 }
5429 /*
5430 * We don't free the address here
5431 * since all the net's were freed
5432 * above.
5433 */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005434 if (control->on_read_q == 0) {
5435 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02005436 }
5437 }
5438}
tuexendd729232011-11-01 23:04:43 +00005439
tuexendd729232011-11-01 23:04:43 +00005440/*-
5441 * Free the association after un-hashing the remote port. This
5442 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5443 * expect that the input to this function IS a locked TCB.
5444 * It will return 0, if it did NOT destroy the association (instead
5445 * it unlocks it. It will return NON-zero if it either destroyed the
5446 * association OR the association is already destroyed.
5447 */
5448int
5449sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5450{
5451 int i;
5452 struct sctp_association *asoc;
5453 struct sctp_nets *net, *nnet;
5454 struct sctp_laddr *laddr, *naddr;
5455 struct sctp_tmit_chunk *chk, *nchk;
5456 struct sctp_asconf_addr *aparam, *naparam;
5457 struct sctp_asconf_ack *aack, *naack;
5458 struct sctp_stream_reset_list *strrst, *nstrrst;
5459 struct sctp_queued_to_read *sq, *nsq;
5460 struct sctp_stream_queue_pending *sp, *nsp;
5461 sctp_sharedkey_t *shared_key, *nshared_key;
5462 struct socket *so;
5463
5464 /* first, lets purge the entry from the hash table. */
Michael Tuexen5be0c252020-06-13 00:53:56 +02005465#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00005466 sctp_lock_assert(SCTP_INP_SO(inp));
5467#endif
Michael Tuexen05865332022-03-29 21:00:12 +02005468 SCTP_TCB_LOCK_ASSERT(stcb);
tuexendd729232011-11-01 23:04:43 +00005469
5470#ifdef SCTP_LOG_CLOSING
5471 sctp_log_closing(inp, stcb, 6);
5472#endif
5473 if (stcb->asoc.state == 0) {
5474#ifdef SCTP_LOG_CLOSING
5475 sctp_log_closing(inp, NULL, 7);
5476#endif
5477 /* there is no asoc, really TSNH :-0 */
5478 return (1);
5479 }
5480 if (stcb->asoc.alternate) {
5481 sctp_free_remote_addr(stcb->asoc.alternate);
5482 stcb->asoc.alternate = NULL;
5483 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02005484#if !(defined(__APPLE__) && !defined(__Userspace__))
5485 /* TEMP CODE */
tuexendd729232011-11-01 23:04:43 +00005486 if (stcb->freed_from_where == 0) {
5487 /* Only record the first place free happened from */
5488 stcb->freed_from_where = from_location;
5489 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02005490 /* TEMP CODE */
tuexendd729232011-11-01 23:04:43 +00005491#endif
5492
5493 asoc = &stcb->asoc;
5494 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5495 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5496 /* nothing around */
5497 so = NULL;
5498 else
5499 so = inp->sctp_socket;
5500
5501 /*
5502 * We used timer based freeing if a reader or writer is in the way.
5503 * So we first check if we are actually being called from a timer,
5504 * if so we abort early if a reader or writer is still in the way.
5505 */
5506 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5507 (from_inpcbfree == SCTP_NORMAL_PROC)) {
5508 /*
5509 * is it the timer driving us? if so are the reader/writers
5510 * gone?
5511 */
5512 if (stcb->asoc.refcnt) {
5513 /* nope, reader or writer in the way */
5514 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5515 /* no asoc destroyed */
5516 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 */
Michael Tuexen8d6935d2022-05-15 01:51:24 +02005571 if ((stcb->asoc.refcnt) ||
tuexendd729232011-11-01 23:04:43 +00005572 (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_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005587
5588#ifdef SCTP_LOG_CLOSING
5589 sctp_log_closing(inp, stcb, 9);
5590#endif
5591 /* no asoc destroyed */
5592 return (0);
5593 }
5594#ifdef SCTP_LOG_CLOSING
5595 sctp_log_closing(inp, stcb, 10);
5596#endif
5597 /* When I reach here, no others want
5598 * to kill the assoc yet.. and I own
5599 * the lock. Now its possible an abort
5600 * comes in when I do the lock exchange
5601 * below to grab all the locks to do
5602 * the final take out. to prevent this
5603 * we increment the count, which will
5604 * start a timer and blow out above thus
5605 * assuring us that we hold exclusive
5606 * killing of the asoc. Note that
5607 * after getting back the TCB lock
5608 * we will go ahead and increment the
5609 * counter back up and stop any timer
5610 * a passing stranger may have started :-S
5611 */
5612 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5613 atomic_add_int(&stcb->asoc.refcnt, 1);
5614
5615 SCTP_TCB_UNLOCK(stcb);
5616 SCTP_INP_INFO_WLOCK();
5617 SCTP_INP_WLOCK(inp);
5618 SCTP_TCB_LOCK(stcb);
5619 }
5620 /* Double check the GONE flag */
5621 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5622 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5623 /* nothing around */
5624 so = NULL;
5625
Michael Tuexen1f495442020-03-25 16:41:21 +01005626 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5627 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5628 /*
5629 * For TCP type we need special handling when we are
5630 * connected. We also include the peel'ed off ones to.
5631 */
5632 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5633 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5634 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5635 if (so) {
5636 SOCKBUF_LOCK(&so->so_rcv);
5637 so->so_state &= ~(SS_ISCONNECTING |
5638 SS_ISDISCONNECTING |
5639 SS_ISCONFIRMING |
5640 SS_ISCONNECTED);
5641 so->so_state |= SS_ISDISCONNECTED;
Michael Tuexen5be0c252020-06-13 00:53:56 +02005642#if defined(__APPLE__) && !defined(__Userspace__)
Michael Tuexen1f495442020-03-25 16:41:21 +01005643 socantrcvmore(so);
5644#else
5645 socantrcvmore_locked(so);
5646#endif
5647 socantsendmore(so);
5648 sctp_sowwakeup(inp, so);
5649 sctp_sorwakeup(inp, so);
5650 SCTP_SOWAKEUP(so);
5651 }
5652 }
5653 }
5654
tuexendd729232011-11-01 23:04:43 +00005655 /* Make it invalid too, that way if its
5656 * about to run it will abort and return.
5657 */
5658 /* re-increment the lock */
5659 if (from_inpcbfree == SCTP_NORMAL_PROC) {
Michael Tuexena106ab82021-11-26 14:03:15 +01005660 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00005661 }
5662 if (stcb->asoc.refcnt) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005663 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00005664 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5665 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5666 SCTP_INP_INFO_WUNLOCK();
5667 SCTP_INP_WUNLOCK(inp);
5668 }
5669 SCTP_TCB_UNLOCK(stcb);
5670 return (0);
5671 }
5672 asoc->state = 0;
5673 if (inp->sctp_tcbhash) {
5674 LIST_REMOVE(stcb, sctp_tcbhash);
5675 }
5676 if (stcb->asoc.in_asocid_hash) {
5677 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5678 }
Michael Tuexena8409b32021-09-02 01:50:45 +02005679 if (inp->sctp_socket == NULL) {
5680 stcb->sctp_socket = NULL;
5681 }
tuexendd729232011-11-01 23:04:43 +00005682 /* Now lets remove it from the list of ALL associations in the EP */
5683 LIST_REMOVE(stcb, sctp_tcblist);
5684 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5685 SCTP_INP_INCR_REF(inp);
5686 SCTP_INP_WUNLOCK(inp);
5687 }
5688 /* pull from vtag hash */
5689 LIST_REMOVE(stcb, sctp_asocs);
Michael Tuexen4ec13f72021-09-09 01:22:43 +02005690 sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport);
tuexendd729232011-11-01 23:04:43 +00005691
5692 /* Now restop the timers to be sure
5693 * this is paranoia at is finest!
5694 */
Michael Tuexen5f976392020-02-11 21:20:07 +01005695 sctp_stop_association_timers(stcb, true);
tuexendd729232011-11-01 23:04:43 +00005696
tuexendd729232011-11-01 23:04:43 +00005697 /*
5698 * The chunk lists and such SHOULD be empty but we check them just
5699 * in case.
5700 */
5701 /* anything on the wheel needs to be removed */
5702 for (i = 0; i < asoc->streamoutcnt; i++) {
5703 struct sctp_stream_out *outs;
5704
5705 outs = &asoc->strmout[i];
5706 /* now clean up any chunks here */
5707 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
Michael Tuexenc6ae6c62016-08-07 15:17:05 +02005708 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
tuexendd729232011-11-01 23:04:43 +00005709 TAILQ_REMOVE(&outs->outqueue, sp, next);
Michael Tuexen5e95fea2021-09-21 16:39:42 +02005710 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
tuexen30ca8172012-07-17 13:09:48 +00005711 sctp_free_spbufspace(stcb, asoc, sp);
tuexendd729232011-11-01 23:04:43 +00005712 if (sp->data) {
5713 if (so) {
5714 /* Still an open socket - report */
5715 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
tuexenda53ff02012-05-14 09:00:59 +00005716 0, (void *)sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005717 }
5718 if (sp->data) {
5719 sctp_m_freem(sp->data);
5720 sp->data = NULL;
5721 sp->tail_mbuf = NULL;
tuexen30ca8172012-07-17 13:09:48 +00005722 sp->length = 0;
tuexendd729232011-11-01 23:04:43 +00005723 }
5724 }
5725 if (sp->net) {
5726 sctp_free_remote_addr(sp->net);
5727 sp->net = NULL;
5728 }
tuexen30ca8172012-07-17 13:09:48 +00005729 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005730 }
5731 }
5732 /*sa_ignore FREED_MEMORY*/
5733 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5734 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5735 SCTP_FREE(strrst, SCTP_M_STRESET);
5736 }
5737 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5738 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5739 if (sq->data) {
5740 sctp_m_freem(sq->data);
5741 sq->data = NULL;
5742 }
5743 sctp_free_remote_addr(sq->whoFrom);
5744 sq->whoFrom = NULL;
5745 sq->stcb = NULL;
5746 /* Free the ctl entry */
Michael Tuexene5001952016-04-17 19:25:27 +02005747 sctp_free_a_readq(stcb, sq);
tuexendd729232011-11-01 23:04:43 +00005748 /*sa_ignore FREED_MEMORY*/
5749 }
5750 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5751 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5752 if (chk->data) {
5753 sctp_m_freem(chk->data);
5754 chk->data = NULL;
5755 }
5756 if (chk->holds_key_ref)
5757 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5758 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5759 SCTP_DECR_CHK_COUNT();
5760 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5761 asoc->free_chunk_cnt--;
5762 /*sa_ignore FREED_MEMORY*/
5763 }
5764 /* pending send queue SHOULD be empty */
5765 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005766 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5767 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen8fcc5142012-11-16 19:46:12 +00005768#ifdef INVARIANTS
5769 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005770 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen8fcc5142012-11-16 19:46:12 +00005771#endif
5772 }
tuexendd729232011-11-01 23:04:43 +00005773 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5774 if (chk->data) {
5775 if (so) {
5776 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005777 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5778 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005779 }
5780 if (chk->data) {
5781 sctp_m_freem(chk->data);
5782 chk->data = NULL;
5783 }
5784 }
5785 if (chk->holds_key_ref)
5786 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5787 if (chk->whoTo) {
5788 sctp_free_remote_addr(chk->whoTo);
5789 chk->whoTo = NULL;
5790 }
5791 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5792 SCTP_DECR_CHK_COUNT();
5793 /*sa_ignore FREED_MEMORY*/
5794 }
5795 /* sent queue SHOULD be empty */
5796 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
t00fcxen8fcc5142012-11-16 19:46:12 +00005797 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005798 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5799 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen9ad90772012-11-07 22:19:57 +00005800#ifdef INVARIANTS
5801 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005802 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen9ad90772012-11-07 22:19:57 +00005803#endif
5804 }
5805 }
tuexendd729232011-11-01 23:04:43 +00005806 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5807 if (chk->data) {
5808 if (so) {
5809 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005810 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5811 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005812 }
5813 if (chk->data) {
5814 sctp_m_freem(chk->data);
5815 chk->data = NULL;
5816 }
5817 }
5818 if (chk->holds_key_ref)
5819 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5820 sctp_free_remote_addr(chk->whoTo);
5821 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5822 SCTP_DECR_CHK_COUNT();
5823 /*sa_ignore FREED_MEMORY*/
5824 }
t00fcxen8fcc5142012-11-16 19:46:12 +00005825#ifdef INVARIANTS
5826 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
5827 if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
5828 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
5829 }
5830 }
5831#endif
tuexendd729232011-11-01 23:04:43 +00005832 /* control queue MAY not be empty */
5833 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5834 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5835 if (chk->data) {
5836 sctp_m_freem(chk->data);
5837 chk->data = NULL;
5838 }
5839 if (chk->holds_key_ref)
5840 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5841 sctp_free_remote_addr(chk->whoTo);
5842 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5843 SCTP_DECR_CHK_COUNT();
5844 /*sa_ignore FREED_MEMORY*/
5845 }
5846 /* ASCONF queue MAY not be empty */
5847 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5848 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5849 if (chk->data) {
5850 sctp_m_freem(chk->data);
5851 chk->data = NULL;
5852 }
5853 if (chk->holds_key_ref)
5854 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5855 sctp_free_remote_addr(chk->whoTo);
5856 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5857 SCTP_DECR_CHK_COUNT();
5858 /*sa_ignore FREED_MEMORY*/
5859 }
tuexendd729232011-11-01 23:04:43 +00005860 if (asoc->mapping_array) {
5861 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5862 asoc->mapping_array = NULL;
5863 }
5864 if (asoc->nr_mapping_array) {
5865 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5866 asoc->nr_mapping_array = NULL;
5867 }
5868 /* the stream outs */
5869 if (asoc->strmout) {
5870 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5871 asoc->strmout = NULL;
5872 }
5873 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5874 if (asoc->strmin) {
tuexendd729232011-11-01 23:04:43 +00005875 for (i = 0; i < asoc->streamincnt; i++) {
Michael Tuexene5001952016-04-17 19:25:27 +02005876 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
5877 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
tuexendd729232011-11-01 23:04:43 +00005878 }
5879 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5880 asoc->strmin = NULL;
5881 }
5882 asoc->streamincnt = 0;
5883 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5884#ifdef INVARIANTS
5885 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5886 panic("no net's left alloc'ed, or list points to itself");
5887 }
5888#endif
5889 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5890 sctp_free_remote_addr(net);
5891 }
5892 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5893 /*sa_ignore FREED_MEMORY*/
5894 sctp_remove_laddr(laddr);
5895 }
5896
5897 /* pending asconf (address) parameters */
5898 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5899 /*sa_ignore FREED_MEMORY*/
5900 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5901 SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5902 }
5903 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5904 /*sa_ignore FREED_MEMORY*/
5905 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5906 if (aack->data != NULL) {
5907 sctp_m_freem(aack->data);
5908 }
5909 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5910 }
5911 /* clean up auth stuff */
5912 if (asoc->local_hmacs)
5913 sctp_free_hmaclist(asoc->local_hmacs);
5914 if (asoc->peer_hmacs)
5915 sctp_free_hmaclist(asoc->peer_hmacs);
5916
5917 if (asoc->local_auth_chunks)
5918 sctp_free_chunklist(asoc->local_auth_chunks);
5919 if (asoc->peer_auth_chunks)
5920 sctp_free_chunklist(asoc->peer_auth_chunks);
5921
5922 sctp_free_authinfo(&asoc->authinfo);
5923
5924 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5925 LIST_REMOVE(shared_key, next);
5926 sctp_free_sharedkey(shared_key);
5927 /*sa_ignore FREED_MEMORY*/
5928 }
5929
5930 /* Insert new items here :> */
5931
5932 /* Get rid of LOCK */
t00fcxen2c2ff922013-11-03 14:03:35 +00005933 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005934 SCTP_TCB_LOCK_DESTROY(stcb);
tuexendd729232011-11-01 23:04:43 +00005935 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5936 SCTP_INP_INFO_WUNLOCK();
5937 SCTP_INP_RLOCK(inp);
5938 }
Michael Tuexen5be0c252020-06-13 00:53:56 +02005939#if defined(__APPLE__) && !defined(__Userspace__)
5940 /* TEMP CODE */
tuexendd729232011-11-01 23:04:43 +00005941 stcb->freed_from_where = from_location;
5942#endif
5943#ifdef SCTP_TRACK_FREED_ASOCS
5944 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5945 /* now clean up the tasoc itself */
5946 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5947 SCTP_DECR_ASOC_COUNT();
5948 } else {
5949 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5950 }
5951#else
5952 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5953 SCTP_DECR_ASOC_COUNT();
5954#endif
5955 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5956 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5957 /* If its NOT the inp_free calling us AND
5958 * sctp_close as been called, we
5959 * call back...
5960 */
5961 SCTP_INP_RUNLOCK(inp);
5962 /* This will start the kill timer (if we are
5963 * the last one) since we hold an increment yet. But
5964 * this is the only safe way to do this
5965 * since otherwise if the socket closes
5966 * at the same time we are here we might
5967 * collide in the cleanup.
5968 */
5969 sctp_inpcb_free(inp,
5970 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5971 SCTP_CALLED_DIRECTLY_NOCMPSET);
5972 SCTP_INP_DECR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00005973 } else {
5974 /* The socket is still open. */
5975 SCTP_INP_DECR_REF(inp);
Michael Tuexenc6c994c2020-05-18 21:50:12 +02005976 SCTP_INP_RUNLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00005977 }
5978 }
tuexendd729232011-11-01 23:04:43 +00005979 /* destroyed the asoc */
5980#ifdef SCTP_LOG_CLOSING
5981 sctp_log_closing(inp, NULL, 11);
5982#endif
5983 return (1);
5984}
5985
tuexendd729232011-11-01 23:04:43 +00005986/*
5987 * determine if a destination is "reachable" based upon the addresses bound
5988 * to the current endpoint (e.g. only v4 or v6 currently bound)
5989 */
5990/*
5991 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5992 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5993 * types bound as its endpoint
5994 */
5995int
5996sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5997{
5998 struct sctp_inpcb *inp;
5999 int answer;
6000
6001 /*
6002 * No locks here, the TCB, in all cases is already locked and an
6003 * assoc is up. There is either a INP lock by the caller applied (in
6004 * asconf case when deleting an address) or NOT in the HB case,
6005 * however if HB then the INP increment is up and the INP will not
6006 * be removed (on top of the fact that we have a TCB lock). So we
6007 * only want to read the sctp_flags, which is either bound-all or
6008 * not.. no protection needed since once an assoc is up you can't be
6009 * changing your binding.
6010 */
6011 inp = stcb->sctp_ep;
6012 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6013 /* if bound all, destination is not restricted */
6014 /*
6015 * RRS: Question during lock work: Is this correct? If you
6016 * are bound-all you still might need to obey the V4--V6
6017 * flags??? IMO this bound-all stuff needs to be removed!
6018 */
6019 return (1);
6020 }
6021 /* NOTE: all "scope" checks are done when local addresses are added */
6022 switch (destaddr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00006023#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00006024 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00006025 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
tuexendd729232011-11-01 23:04:43 +00006026 break;
tuexen310f1bc2012-07-15 11:20:56 +00006027#endif
6028#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006029 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00006030 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
tuexen46570542012-07-14 20:37:24 +00006031 break;
tuexen310f1bc2012-07-15 11:20:56 +00006032#endif
tuexen5d9b9742012-07-11 21:52:01 +00006033#if defined(__Userspace__)
6034 case AF_CONN:
6035 answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
tuexendd729232011-11-01 23:04:43 +00006036 break;
tuexen5d9b9742012-07-11 21:52:01 +00006037#endif
tuexendd729232011-11-01 23:04:43 +00006038 default:
6039 /* invalid family, so it's unreachable */
6040 answer = 0;
6041 break;
6042 }
6043 return (answer);
6044}
6045
6046/*
6047 * update the inp_vflags on an endpoint
6048 */
6049static void
6050sctp_update_ep_vflag(struct sctp_inpcb *inp)
6051{
6052 struct sctp_laddr *laddr;
6053
6054 /* first clear the flag */
tuexendd729232011-11-01 23:04:43 +00006055 inp->ip_inp.inp.inp_vflag = 0;
tuexendd729232011-11-01 23:04:43 +00006056 /* set the flag based on addresses on the ep list */
6057 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6058 if (laddr->ifa == NULL) {
6059 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02006060 __func__);
tuexendd729232011-11-01 23:04:43 +00006061 continue;
6062 }
6063
6064 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
6065 continue;
6066 }
6067 switch (laddr->ifa->address.sa.sa_family) {
6068#ifdef INET6
6069 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00006070 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
tuexendd729232011-11-01 23:04:43 +00006071 break;
6072#endif
6073#ifdef INET
6074 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00006075 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
tuexendd729232011-11-01 23:04:43 +00006076 break;
6077#endif
tuexen5d9b9742012-07-11 21:52:01 +00006078#if defined(__Userspace__)
6079 case AF_CONN:
6080 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6081 break;
6082#endif
tuexendd729232011-11-01 23:04:43 +00006083 default:
6084 break;
6085 }
6086 }
6087}
6088
6089/*
6090 * Add the address to the endpoint local address list There is nothing to be
6091 * done if we are bound to all addresses
6092 */
6093void
6094sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
6095{
6096 struct sctp_laddr *laddr;
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006097 struct sctp_tcb *stcb;
tuexen63fc0bb2011-12-27 12:24:52 +00006098 int fnd, error = 0;
tuexendd729232011-11-01 23:04:43 +00006099
6100 fnd = 0;
6101
6102 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6103 /* You are already bound to all. You have it already */
6104 return;
6105 }
6106#ifdef INET6
6107 if (ifa->address.sa.sa_family == AF_INET6) {
6108 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6109 /* Can't bind a non-useable addr. */
6110 return;
6111 }
6112 }
6113#endif
6114 /* first, is it already present? */
6115 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6116 if (laddr->ifa == ifa) {
6117 fnd = 1;
6118 break;
6119 }
6120 }
6121
6122 if (fnd == 0) {
6123 /* Not in the ep list */
6124 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
6125 if (error != 0)
6126 return;
6127 inp->laddr_count++;
6128 /* update inp_vflag flags */
6129 switch (ifa->address.sa.sa_family) {
6130#ifdef INET6
6131 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00006132 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
tuexendd729232011-11-01 23:04:43 +00006133 break;
6134#endif
tuexen310f1bc2012-07-15 11:20:56 +00006135#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006136 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00006137 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
tuexendd729232011-11-01 23:04:43 +00006138 break;
6139#endif
tuexen5d9b9742012-07-11 21:52:01 +00006140#if defined(__Userspace__)
6141 case AF_CONN:
6142 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6143 break;
6144#endif
tuexendd729232011-11-01 23:04:43 +00006145 default:
6146 break;
6147 }
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006148 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6149 sctp_add_local_addr_restricted(stcb, ifa);
6150 }
tuexendd729232011-11-01 23:04:43 +00006151 }
6152 return;
6153}
6154
tuexendd729232011-11-01 23:04:43 +00006155/*
6156 * select a new (hopefully reachable) destination net (should only be used
6157 * when we deleted an ep addr that is the only usable source address to reach
6158 * the destination net)
6159 */
6160static void
6161sctp_select_primary_destination(struct sctp_tcb *stcb)
6162{
6163 struct sctp_nets *net;
6164
6165 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6166 /* for now, we'll just pick the first reachable one we find */
6167 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6168 continue;
6169 if (sctp_destination_is_reachable(stcb,
6170 (struct sockaddr *)&net->ro._l_addr)) {
6171 /* found a reachable destination */
6172 stcb->asoc.primary_destination = net;
6173 }
6174 }
6175 /* I can't there from here! ...we're gonna die shortly... */
6176}
6177
tuexendd729232011-11-01 23:04:43 +00006178/*
Michael Tuexen24e6c732016-02-19 12:28:21 +01006179 * Delete the address from the endpoint local address list. There is nothing
tuexendd729232011-11-01 23:04:43 +00006180 * to be done if we are bound to all addresses
6181 */
6182void
6183sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6184{
6185 struct sctp_laddr *laddr;
6186 int fnd;
6187
6188 fnd = 0;
6189 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6190 /* You are already bound to all. You have it already */
6191 return;
6192 }
6193 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6194 if (laddr->ifa == ifa) {
6195 fnd = 1;
6196 break;
6197 }
6198 }
6199 if (fnd && (inp->laddr_count < 2)) {
6200 /* can't delete unless there are at LEAST 2 addresses */
6201 return;
6202 }
6203 if (fnd) {
6204 /*
6205 * clean up any use of this address go through our
6206 * associations and clear any last_used_address that match
6207 * this one for each assoc, see if a new primary_destination
6208 * is needed
6209 */
6210 struct sctp_tcb *stcb;
6211
6212 /* clean up "next_addr_touse" */
6213 if (inp->next_addr_touse == laddr)
6214 /* delete this address */
6215 inp->next_addr_touse = NULL;
6216
6217 /* clean up "last_used_address" */
6218 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6219 struct sctp_nets *net;
Michael Tuexen24e6c732016-02-19 12:28:21 +01006220
tuexendd729232011-11-01 23:04:43 +00006221 SCTP_TCB_LOCK(stcb);
6222 if (stcb->asoc.last_used_address == laddr)
6223 /* delete this address */
6224 stcb->asoc.last_used_address = NULL;
6225 /* Now spin through all the nets and purge any ref to laddr */
6226 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
Michael Tuexen24e6c732016-02-19 12:28:21 +01006227 if (net->ro._s_addr == laddr->ifa) {
tuexendd729232011-11-01 23:04:43 +00006228 /* Yep, purge src address selected */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006229#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexena0515422020-05-18 00:47:04 +02006230 RO_NHFREE(&net->ro);
6231#else
tuexendd729232011-11-01 23:04:43 +00006232 sctp_rtentry_t *rt;
6233
6234 /* delete this address if cached */
6235 rt = net->ro.ro_rt;
6236 if (rt != NULL) {
6237 RTFREE(rt);
6238 net->ro.ro_rt = NULL;
6239 }
Michael Tuexena0515422020-05-18 00:47:04 +02006240#endif
tuexendd729232011-11-01 23:04:43 +00006241 sctp_free_ifa(net->ro._s_addr);
6242 net->ro._s_addr = NULL;
6243 net->src_addr_selected = 0;
6244 }
6245 }
6246 SCTP_TCB_UNLOCK(stcb);
6247 } /* for each tcb */
6248 /* remove it from the ep list */
6249 sctp_remove_laddr(laddr);
6250 inp->laddr_count--;
6251 /* update inp_vflag flags */
6252 sctp_update_ep_vflag(inp);
6253 }
6254 return;
6255}
6256
6257/*
6258 * Add the address to the TCB local address restricted list.
6259 * This is a "pending" address list (eg. addresses waiting for an
6260 * ASCONF-ACK response) and cannot be used as a valid source address.
6261 */
6262void
6263sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6264{
tuexendd729232011-11-01 23:04:43 +00006265 struct sctp_laddr *laddr;
6266 struct sctpladdr *list;
6267
6268 /*
6269 * Assumes TCB is locked.. and possibly the INP. May need to
6270 * confirm/fix that if we need it and is not the case.
6271 */
6272 list = &stcb->asoc.sctp_restricted_addrs;
6273
tuexendd729232011-11-01 23:04:43 +00006274#ifdef INET6
6275 if (ifa->address.sa.sa_family == AF_INET6) {
6276 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6277 /* Can't bind a non-existent addr. */
6278 return;
6279 }
6280 }
6281#endif
6282 /* does the address already exist? */
6283 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6284 if (laddr->ifa == ifa) {
6285 return;
6286 }
6287 }
6288
6289 /* add to the list */
6290 (void)sctp_insert_laddr(list, ifa, 0);
6291 return;
6292}
6293
6294/*
tuexendd729232011-11-01 23:04:43 +00006295 * Remove a local address from the TCB local address restricted list
6296 */
6297void
6298sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6299{
6300 struct sctp_inpcb *inp;
6301 struct sctp_laddr *laddr;
6302
6303 /*
6304 * This is called by asconf work. It is assumed that a) The TCB is
6305 * locked and b) The INP is locked. This is true in as much as I can
6306 * trace through the entry asconf code where I did these locks.
6307 * Again, the ASCONF code is a bit different in that it does lock
6308 * the INP during its work often times. This must be since we don't
6309 * want other proc's looking up things while what they are looking
6310 * up is changing :-D
6311 */
6312
6313 inp = stcb->sctp_ep;
6314 /* if subset bound and don't allow ASCONF's, can't delete last */
6315 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6316 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6317 if (stcb->sctp_ep->laddr_count < 2) {
6318 /* can't delete last address */
6319 return;
6320 }
6321 }
6322 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6323 /* remove the address if it exists */
6324 if (laddr->ifa == NULL)
6325 continue;
6326 if (laddr->ifa == ifa) {
6327 sctp_remove_laddr(laddr);
6328 return;
6329 }
6330 }
6331
6332 /* address not found! */
6333 return;
6334}
6335
Michael Tuexen5be0c252020-06-13 00:53:56 +02006336#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006337/* sysctl */
6338static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6339static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006340#endif
tuexendd729232011-11-01 23:04:43 +00006341
Michael Tuexen5be0c252020-06-13 00:53:56 +02006342#if defined(__FreeBSD__) && !defined(__Userspace__)
6343#if defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexen63fc0bb2011-12-27 12:24:52 +00006344struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6345int *sctp_cpuarry = NULL;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006346
tuexendd729232011-11-01 23:04:43 +00006347void
6348sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6349{
6350 /* Queue a packet to a processor for the specified core */
6351 struct sctp_mcore_queue *qent;
6352 struct sctp_mcore_ctrl *wkq;
tuexen63fc0bb2011-12-27 12:24:52 +00006353 int need_wake = 0;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006354
tuexendd729232011-11-01 23:04:43 +00006355 if (sctp_mcore_workers == NULL) {
6356 /* Something went way bad during setup */
6357 sctp_input_with_port(m, off, 0);
6358 return;
6359 }
6360 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6361 (sizeof(struct sctp_mcore_queue)),
6362 SCTP_M_MCORE);
6363 if (qent == NULL) {
6364 /* This is trouble */
6365 sctp_input_with_port(m, off, 0);
6366 return;
6367 }
tuexendd729232011-11-01 23:04:43 +00006368 qent->vn = curvnet;
tuexendd729232011-11-01 23:04:43 +00006369 qent->m = m;
6370 qent->off = off;
6371 qent->v6 = 0;
6372 wkq = &sctp_mcore_workers[cpu_to_use];
6373 SCTP_MCORE_QLOCK(wkq);
6374
6375 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6376 if (wkq->running == 0) {
6377 need_wake = 1;
6378 }
6379 SCTP_MCORE_QUNLOCK(wkq);
tuexen63fc0bb2011-12-27 12:24:52 +00006380 if (need_wake) {
tuexendd729232011-11-01 23:04:43 +00006381 wakeup(&wkq->running);
6382 }
6383}
6384
6385static void
6386sctp_mcore_thread(void *arg)
6387{
6388
6389 struct sctp_mcore_ctrl *wkq;
6390 struct sctp_mcore_queue *qent;
6391
6392 wkq = (struct sctp_mcore_ctrl *)arg;
6393 struct mbuf *m;
6394 int off, v6;
6395
6396 /* Wait for first tickle */
6397 SCTP_MCORE_LOCK(wkq);
6398 wkq->running = 0;
6399 msleep(&wkq->running,
6400 &wkq->core_mtx,
6401 0, "wait for pkt", 0);
6402 SCTP_MCORE_UNLOCK(wkq);
6403
6404 /* Bind to our cpu */
6405 thread_lock(curthread);
6406 sched_bind(curthread, wkq->cpuid);
6407 thread_unlock(curthread);
6408
6409 /* Now lets start working */
6410 SCTP_MCORE_LOCK(wkq);
6411 /* Now grab lock and go */
tuexen9784e9a2011-12-18 13:04:23 +00006412 for (;;) {
tuexendd729232011-11-01 23:04:43 +00006413 SCTP_MCORE_QLOCK(wkq);
6414 skip_sleep:
6415 wkq->running = 1;
6416 qent = TAILQ_FIRST(&wkq->que);
6417 if (qent) {
6418 TAILQ_REMOVE(&wkq->que, qent, next);
6419 SCTP_MCORE_QUNLOCK(wkq);
tuexendd729232011-11-01 23:04:43 +00006420 CURVNET_SET(qent->vn);
tuexendd729232011-11-01 23:04:43 +00006421 m = qent->m;
6422 off = qent->off;
6423 v6 = qent->v6;
6424 SCTP_FREE(qent, SCTP_M_MCORE);
6425 if (v6 == 0) {
6426 sctp_input_with_port(m, off, 0);
6427 } else {
tuexencb5fe8d2012-05-04 09:50:27 +00006428 SCTP_PRINTF("V6 not yet supported\n");
tuexendd729232011-11-01 23:04:43 +00006429 sctp_m_freem(m);
6430 }
tuexendd729232011-11-01 23:04:43 +00006431 CURVNET_RESTORE();
tuexendd729232011-11-01 23:04:43 +00006432 SCTP_MCORE_QLOCK(wkq);
6433 }
6434 wkq->running = 0;
6435 if (!TAILQ_EMPTY(&wkq->que)) {
6436 goto skip_sleep;
6437 }
6438 SCTP_MCORE_QUNLOCK(wkq);
6439 msleep(&wkq->running,
6440 &wkq->core_mtx,
6441 0, "wait for pkt", 0);
tuexen63fc0bb2011-12-27 12:24:52 +00006442 }
tuexendd729232011-11-01 23:04:43 +00006443}
6444
6445static void
6446sctp_startup_mcore_threads(void)
6447{
6448 int i, cpu;
6449
6450 if (mp_ncpus == 1)
6451 return;
6452
6453 if (sctp_mcore_workers != NULL) {
6454 /* Already been here in some previous
6455 * vnet?
6456 */
6457 return;
6458 }
6459 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6460 ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6461 SCTP_M_MCORE);
6462 if (sctp_mcore_workers == NULL) {
6463 /* TSNH I hope */
6464 return;
6465 }
tuexen09631162012-04-18 11:23:02 +00006466 memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
tuexendd729232011-11-01 23:04:43 +00006467 sizeof(struct sctp_mcore_ctrl)));
6468 /* Init the structures */
tuexen63fc0bb2011-12-27 12:24:52 +00006469 for (i = 0; i<=mp_maxid; i++) {
tuexendd729232011-11-01 23:04:43 +00006470 TAILQ_INIT(&sctp_mcore_workers[i].que);
6471 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6472 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6473 sctp_mcore_workers[i].cpuid = i;
6474 }
6475 if (sctp_cpuarry == NULL) {
6476 SCTP_MALLOC(sctp_cpuarry, int *,
6477 (mp_ncpus * sizeof(int)),
6478 SCTP_M_MCORE);
tuexen63fc0bb2011-12-27 12:24:52 +00006479 i = 0;
tuexendd729232011-11-01 23:04:43 +00006480 CPU_FOREACH(cpu) {
6481 sctp_cpuarry[i] = cpu;
6482 i++;
6483 }
6484 }
tuexendd729232011-11-01 23:04:43 +00006485 /* Now start them all */
6486 CPU_FOREACH(cpu) {
tuexendd729232011-11-01 23:04:43 +00006487 (void)kproc_create(sctp_mcore_thread,
6488 (void *)&sctp_mcore_workers[cpu],
6489 &sctp_mcore_workers[cpu].thread_proc,
Michael Tuexenad4f4f02021-03-21 16:54:26 +01006490 0,
tuexendd729232011-11-01 23:04:43 +00006491 SCTP_KTHREAD_PAGES,
6492 SCTP_MCORE_NAME);
tuexendd729232011-11-01 23:04:43 +00006493 }
6494}
6495#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006496#endif
6497
6498#if defined(__FreeBSD__) && !defined(__Userspace__)
6499#if defined(SCTP_NOT_YET)
tuexendd729232011-11-01 23:04:43 +00006500static struct mbuf *
6501sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6502{
6503 struct ip *ip;
6504 struct sctphdr *sh;
6505 int offset;
6506 uint32_t flowid, tag;
tuexen76eb2092012-04-01 16:35:28 +00006507
tuexendd729232011-11-01 23:04:43 +00006508 /*
6509 * No flow id built by lower layers fix it so we
6510 * create one.
6511 */
6512 ip = mtod(m, struct ip *);
tuexen664967a2012-06-28 16:24:36 +00006513 offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
tuexendd729232011-11-01 23:04:43 +00006514 if (SCTP_BUF_LEN(m) < offset) {
tuexen664967a2012-06-28 16:24:36 +00006515 if ((m = m_pullup(m, offset)) == NULL) {
tuexendd729232011-11-01 23:04:43 +00006516 SCTP_STAT_INCR(sctps_hdrops);
6517 return (NULL);
6518 }
6519 ip = mtod(m, struct ip *);
6520 }
6521 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
tuexendd729232011-11-01 23:04:43 +00006522 tag = htonl(sh->v_tag);
6523 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6524 m->m_pkthdr.flowid = flowid;
Michael Tuexena5089952020-06-09 23:11:08 +02006525 /* FIX ME */
tuexendd729232011-11-01 23:04:43 +00006526 m->m_flags |= M_FLOWID;
6527 return (m);
6528}
tuexendd729232011-11-01 23:04:43 +00006529
Michael Tuexen5be0c252020-06-13 00:53:56 +02006530#endif
6531#endif
tuexendd729232011-11-01 23:04:43 +00006532void
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006533#if defined(__Userspace__)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006534sctp_pcb_init(int start_threads)
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006535#else
6536sctp_pcb_init(void)
6537#endif
tuexendd729232011-11-01 23:04:43 +00006538{
6539 /*
6540 * SCTP initialization for the PCB structures should be called by
Michael Tuexen34488e72016-05-03 22:11:59 +02006541 * the sctp_init() function.
tuexendd729232011-11-01 23:04:43 +00006542 */
6543 int i;
6544 struct timeval tv;
tuexen6019b072011-12-15 18:38:03 +00006545
tuexendd729232011-11-01 23:04:43 +00006546 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6547 /* error I was called twice */
6548 return;
6549 }
6550 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6551
Michael Tuexencb2878b2015-11-13 12:59:40 +01006552#if defined(SCTP_PROCESS_LEVEL_LOCKS)
Michael Tuexeneb611572020-06-13 17:27:02 +02006553#if !defined(_WIN32)
Michael Tuexencb2878b2015-11-13 12:59:40 +01006554 pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr));
Paul-Louis Ageneau9d6b99b2021-10-07 22:30:51 +02006555 pthread_rwlockattr_init(&SCTP_BASE_VAR(rwlock_attr));
Michael Tuexencb2878b2015-11-13 12:59:40 +01006556#ifdef INVARIANTS
6557 pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK);
6558#endif
6559#endif
6560#endif
tuexendd729232011-11-01 23:04:43 +00006561#if defined(SCTP_LOCAL_TRACE_BUF)
Michael Tuexeneb611572020-06-13 17:27:02 +02006562#if defined(_WIN32) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006563 if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02006564 memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006565 }
6566#else
Michael Tuexend0853302017-07-19 17:01:31 +02006567 memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006568#endif
6569#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006570#if defined(__FreeBSD__) && !defined(__Userspace__)
6571#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexendd729232011-11-01 23:04:43 +00006572 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6573 ((mp_maxid+1) * sizeof(struct sctpstat)),
6574 SCTP_M_MCORE);
6575#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006576#endif
tuexendd729232011-11-01 23:04:43 +00006577 (void)SCTP_GETTIME_TIMEVAL(&tv);
Michael Tuexen5be0c252020-06-13 00:53:56 +02006578#if defined(__FreeBSD__) && !defined(__Userspace__)
6579#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
Michael Tuexend0853302017-07-19 17:01:31 +02006580 memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1));
tuexendd729232011-11-01 23:04:43 +00006581 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6582 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6583#else
Michael Tuexend0853302017-07-19 17:01:31 +02006584 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
tuexendd729232011-11-01 23:04:43 +00006585 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6586 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6587#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006588#else
6589 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
6590 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6591 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6592#endif
tuexendd729232011-11-01 23:04:43 +00006593 /* init the empty list of (All) Endpoints */
6594 LIST_INIT(&SCTP_BASE_INFO(listhead));
Michael Tuexen5be0c252020-06-13 00:53:56 +02006595#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006596 LIST_INIT(&SCTP_BASE_INFO(inplisthead));
t00fcxenbc766ab2014-01-03 19:29:31 +00006597#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6598 SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead);
6599 SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init();
6600 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6601 SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6602 SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init();
6603 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6604#else
6605 SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead);
6606 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init();
6607 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6608 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6609 SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init();
6610 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6611#endif
6612#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6613 SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc;
6614 in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo));
6615#endif
tuexendd729232011-11-01 23:04:43 +00006616#endif
6617
tuexendd729232011-11-01 23:04:43 +00006618 /* init the hash table of endpoints */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006619#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006620 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6621 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6622 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
tuexendd729232011-11-01 23:04:43 +00006623#endif
6624 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6625 &SCTP_BASE_INFO(hashasocmark));
6626 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6627 &SCTP_BASE_INFO(hashmark));
6628 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6629 &SCTP_BASE_INFO(hashtcpmark));
6630 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
tuexendd729232011-11-01 23:04:43 +00006631 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6632 &SCTP_BASE_INFO(hashvrfmark));
6633
6634 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6635 &SCTP_BASE_INFO(vrf_ifn_hashmark));
6636 /* init the zones */
6637 /*
6638 * FIX ME: Should check for NULL returns, but if it does fail we are
6639 * doomed to panic anyways... add later maybe.
6640 */
6641 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6642 sizeof(struct sctp_inpcb), maxsockets);
6643
6644 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6645 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6646
6647 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6648 sizeof(struct sctp_laddr),
6649 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6650
6651 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6652 sizeof(struct sctp_nets),
6653 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6654
6655 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6656 sizeof(struct sctp_tmit_chunk),
6657 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6658
6659 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6660 sizeof(struct sctp_queued_to_read),
6661 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6662
6663 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6664 sizeof(struct sctp_stream_queue_pending),
6665 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6666
6667 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6668 sizeof(struct sctp_asconf),
6669 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6670
6671 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6672 sizeof(struct sctp_asconf_ack),
6673 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6674
tuexendd729232011-11-01 23:04:43 +00006675 /* Master Lock INIT for info structure */
tuexendd729232011-11-01 23:04:43 +00006676 SCTP_INP_INFO_LOCK_INIT();
6677 SCTP_STATLOG_INIT_LOCK();
6678
6679 SCTP_IPI_COUNT_INIT();
6680 SCTP_IPI_ADDR_INIT();
6681#ifdef SCTP_PACKET_LOGGING
6682 SCTP_IP_PKTLOG_INIT();
6683#endif
6684 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6685
6686 SCTP_WQ_ADDR_INIT();
6687 /* not sure if we need all the counts */
6688 SCTP_BASE_INFO(ipi_count_ep) = 0;
6689 /* assoc/tcb zone info */
6690 SCTP_BASE_INFO(ipi_count_asoc) = 0;
6691 /* local addrlist zone info */
6692 SCTP_BASE_INFO(ipi_count_laddr) = 0;
6693 /* remote addrlist zone info */
6694 SCTP_BASE_INFO(ipi_count_raddr) = 0;
6695 /* chunk info */
6696 SCTP_BASE_INFO(ipi_count_chunk) = 0;
6697
6698 /* socket queue zone info */
6699 SCTP_BASE_INFO(ipi_count_readq) = 0;
6700
6701 /* stream out queue cont */
6702 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6703
6704 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6705 SCTP_BASE_INFO(ipi_free_chunks) = 0;
6706
6707 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6708
6709 /* Init the TIMEWAIT list */
6710 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6711 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6712 }
tuexendd729232011-11-01 23:04:43 +00006713#if defined(SCTP_PROCESS_LEVEL_LOCKS)
Michael Tuexeneb611572020-06-13 17:27:02 +02006714#if defined(_WIN32)
tuexen6019b072011-12-15 18:38:03 +00006715 InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6716#else
tuexendd729232011-11-01 23:04:43 +00006717 (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6718#endif
tuexen6019b072011-12-15 18:38:03 +00006719#endif
tuexendd729232011-11-01 23:04:43 +00006720 sctp_startup_iterator();
6721
Michael Tuexen5be0c252020-06-13 00:53:56 +02006722#if defined(__FreeBSD__) && !defined(__Userspace__)
6723#if defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexendd729232011-11-01 23:04:43 +00006724 sctp_startup_mcore_threads();
6725#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006726#endif
tuexendd729232011-11-01 23:04:43 +00006727
tuexendd729232011-11-01 23:04:43 +00006728 /*
6729 * INIT the default VRF which for BSD is the only one, other O/S's
6730 * may have more. But initially they must start with one and then
6731 * add the VRF's as addresses are added.
6732 */
6733 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
Michael Tuexen5be0c252020-06-13 00:53:56 +02006734#if defined(__FreeBSD__) && !defined(__Userspace__) && defined(SCTP_NOT_YET)
tuexendd729232011-11-01 23:04:43 +00006735 if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
tuexencb5fe8d2012-05-04 09:50:27 +00006736 SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
tuexendd729232011-11-01 23:04:43 +00006737 }
6738#endif
6739#if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6740 /* allocate the lock for the callout/timer queue */
6741 SCTP_TIMERQ_LOCK_INIT();
6742 TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6743#endif
6744#if defined(__Userspace__)
Michael Tuexen2f6478e2017-03-02 09:57:46 +01006745 mbuf_initialize(NULL);
tuexendd729232011-11-01 23:04:43 +00006746 atomic_init();
tuexenc4178972012-07-15 20:40:54 +00006747#if defined(INET) || defined(INET6)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006748 if (start_threads)
6749 recv_thread_init();
tuexendd729232011-11-01 23:04:43 +00006750#endif
tuexenc4178972012-07-15 20:40:54 +00006751#endif
tuexendd729232011-11-01 23:04:43 +00006752}
6753
6754/*
6755 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6756 */
6757void
6758sctp_pcb_finish(void)
6759{
6760 struct sctp_vrflist *vrf_bucket;
6761 struct sctp_vrf *vrf, *nvrf;
6762 struct sctp_ifn *ifn, *nifn;
6763 struct sctp_ifa *ifa, *nifa;
6764 struct sctpvtaghead *chain;
6765 struct sctp_tagblock *twait_block, *prev_twait_block;
6766 struct sctp_laddr *wi, *nwi;
6767 int i;
t00fcxenc7932282013-11-16 14:57:41 +00006768 struct sctp_iterator *it, *nit;
Michael Tuexen20785d42015-11-13 12:48:35 +01006769
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006770 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6771 SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
6772 return;
6773 }
6774 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
Michael Tuexen5be0c252020-06-13 00:53:56 +02006775#if !(defined(__FreeBSD__) && !defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00006776 /* Notify the iterator to exit. */
6777 SCTP_IPI_ITERATOR_WQ_LOCK();
6778 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6779 sctp_wakeup_iterator();
6780 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6781#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006782#if defined(__APPLE__) && !defined(__Userspace__)
t00fcxenbc766ab2014-01-03 19:29:31 +00006783#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6784 in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo));
6785#endif
tuexendd729232011-11-01 23:04:43 +00006786 SCTP_IPI_ITERATOR_WQ_LOCK();
6787 do {
6788 msleep(&sctp_it_ctl.iterator_flags,
6789 sctp_it_ctl.ipi_iterator_wq_mtx,
6790 0, "waiting_for_work", 0);
6791 } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6792 thread_deallocate(sctp_it_ctl.thread_proc);
6793 SCTP_IPI_ITERATOR_WQ_UNLOCK();
tuexendd729232011-11-01 23:04:43 +00006794#endif
Michael Tuexeneb611572020-06-13 17:27:02 +02006795#if defined(_WIN32) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006796 if (sctp_it_ctl.iterator_thread_obj != NULL) {
6797 NTSTATUS status = STATUS_SUCCESS;
6798
6799 KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6800 status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6801 Executive,
6802 KernelMode,
6803 FALSE,
6804 NULL);
6805 ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6806 }
6807#endif
t00fcxen92a5ac42013-11-16 12:58:54 +00006808#if defined(__Userspace__)
Michael Tuexen3212d542020-03-16 12:30:04 +01006809 if (SCTP_BASE_VAR(iterator_thread_started)) {
Michael Tuexeneb611572020-06-13 17:27:02 +02006810#if defined(_WIN32)
t00fcxen92a5ac42013-11-16 12:58:54 +00006811 WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE);
6812 CloseHandle(sctp_it_ctl.thread_proc);
6813 sctp_it_ctl.thread_proc = NULL;
6814#else
6815 pthread_join(sctp_it_ctl.thread_proc, NULL);
6816 sctp_it_ctl.thread_proc = 0;
6817#endif
6818 }
6819#endif
6820#if defined(SCTP_PROCESS_LEVEL_LOCKS)
Michael Tuexeneb611572020-06-13 17:27:02 +02006821#if defined(_WIN32)
t00fcxen92a5ac42013-11-16 12:58:54 +00006822 DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup);
6823#else
6824 pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup);
Michael Tuexen89e15a72015-11-13 12:42:55 +01006825 pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr));
Paul-Louis Ageneau9d6b99b2021-10-07 22:30:51 +02006826 pthread_rwlockattr_destroy(&SCTP_BASE_VAR(rwlock_attr));
t00fcxen92a5ac42013-11-16 12:58:54 +00006827#endif
6828#endif
t00fcxen9232a652013-11-30 09:34:24 +00006829 /* In FreeBSD the iterator thread never exits
6830 * but we do clean up.
6831 * The only way FreeBSD reaches here is if we have VRF's
6832 * but we still add the ifdef to make it compile on old versions.
6833 */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006834#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006835retry:
6836#endif
t00fcxen9232a652013-11-30 09:34:24 +00006837 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen5be0c252020-06-13 00:53:56 +02006838#if defined(__FreeBSD__) && !defined(__Userspace__)
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006839 /*
6840 * sctp_iterator_worker() might be working on an it entry without
6841 * holding the lock. We won't find it on the list either and
6842 * continue and free/destroy it. While holding the lock, spin, to
6843 * avoid the race condition as sctp_iterator_worker() will have to
Michael Tuexen5ac79522020-06-06 23:32:08 +02006844 * wait to re-acquire the lock.
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006845 */
6846 if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
6847 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6848 SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
6849 "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
6850 DELAY(10);
6851 goto retry;
6852 }
6853#endif
t00fcxen9232a652013-11-30 09:34:24 +00006854 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
Michael Tuexen5be0c252020-06-13 00:53:56 +02006855#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen9232a652013-11-30 09:34:24 +00006856 if (it->vn != curvnet) {
6857 continue;
6858 }
6859#endif
6860 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6861 if (it->function_atend != NULL) {
6862 (*it->function_atend) (it->pointer, it->val);
6863 }
6864 SCTP_FREE(it,SCTP_M_ITER);
6865 }
6866 SCTP_IPI_ITERATOR_WQ_UNLOCK();
Michael Tuexen5be0c252020-06-13 00:53:56 +02006867#if defined(__FreeBSD__) && !defined(__Userspace__)
t00fcxen9232a652013-11-30 09:34:24 +00006868 SCTP_ITERATOR_LOCK();
6869 if ((sctp_it_ctl.cur_it) &&
6870 (sctp_it_ctl.cur_it->vn == curvnet)) {
6871 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6872 }
6873 SCTP_ITERATOR_UNLOCK();
6874#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006875#if !(defined(__FreeBSD__) && !defined(__Userspace__))
t00fcxen92a5ac42013-11-16 12:58:54 +00006876 SCTP_IPI_ITERATOR_WQ_DESTROY();
t00fcxen8dd95b82014-07-11 20:41:28 +00006877 SCTP_ITERATOR_LOCK_DESTROY();
t00fcxenc7932282013-11-16 14:57:41 +00006878#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006879 SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
tuexendd729232011-11-01 23:04:43 +00006880 SCTP_WQ_ADDR_LOCK();
6881 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6882 LIST_REMOVE(wi, sctp_nxt_addr);
6883 SCTP_DECR_LADDR_COUNT();
t00fcxen9892b7f2014-09-06 20:06:39 +00006884 if (wi->action == SCTP_DEL_IP_ADDRESS) {
6885 SCTP_FREE(wi->ifa, SCTP_M_IFA);
6886 }
tuexendd729232011-11-01 23:04:43 +00006887 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6888 }
6889 SCTP_WQ_ADDR_UNLOCK();
tuexen09631162012-04-18 11:23:02 +00006890
tuexendd729232011-11-01 23:04:43 +00006891 /*
6892 * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6893 * is destroyed first).
6894 */
Michael Tuexen2c5953e2020-07-17 17:25:19 +02006895 SCTP_IPI_ADDR_WLOCK();
tuexendd729232011-11-01 23:04:43 +00006896 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6897 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6898 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6899 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6900 /* free the ifa */
6901 LIST_REMOVE(ifa, next_bucket);
6902 LIST_REMOVE(ifa, next_ifa);
6903 SCTP_FREE(ifa, SCTP_M_IFA);
6904 }
6905 /* free the ifn */
6906 LIST_REMOVE(ifn, next_bucket);
6907 LIST_REMOVE(ifn, next_ifn);
6908 SCTP_FREE(ifn, SCTP_M_IFN);
6909 }
6910 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6911 /* free the vrf */
6912 LIST_REMOVE(vrf, next_vrf);
6913 SCTP_FREE(vrf, SCTP_M_VRF);
6914 }
Michael Tuexen2c5953e2020-07-17 17:25:19 +02006915 SCTP_IPI_ADDR_WUNLOCK();
tuexendd729232011-11-01 23:04:43 +00006916 /* free the vrf hashes */
6917 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6918 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
tuexendd729232011-11-01 23:04:43 +00006919
6920 /* free the TIMEWAIT list elements malloc'd in the function
6921 * sctp_add_vtag_to_timewait()...
6922 */
6923 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6924 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6925 if (!LIST_EMPTY(chain)) {
6926 prev_twait_block = NULL;
6927 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6928 if (prev_twait_block) {
6929 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6930 }
6931 prev_twait_block = twait_block;
6932 }
6933 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6934 }
6935 }
6936
6937 /* free the locks and mutexes */
Michael Tuexen5be0c252020-06-13 00:53:56 +02006938#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006939 SCTP_TIMERQ_LOCK_DESTROY();
6940#endif
6941#ifdef SCTP_PACKET_LOGGING
6942 SCTP_IP_PKTLOG_DESTROY();
6943#endif
6944 SCTP_IPI_ADDR_DESTROY();
Michael Tuexen5be0c252020-06-13 00:53:56 +02006945#if defined(__APPLE__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006946 SCTP_IPI_COUNT_DESTROY();
6947#endif
6948 SCTP_STATLOG_DESTROY();
tuexendd729232011-11-01 23:04:43 +00006949 SCTP_INP_INFO_LOCK_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006950
tuexendd729232011-11-01 23:04:43 +00006951 SCTP_WQ_ADDR_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006952
Michael Tuexen5be0c252020-06-13 00:53:56 +02006953#if defined(__APPLE__) && !defined(__Userspace__)
t00fcxenbc766ab2014-01-03 19:29:31 +00006954#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6955 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6956 lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp);
6957 lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6958#else
6959 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6960 lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp);
6961 lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6962#endif
tuexendd729232011-11-01 23:04:43 +00006963#endif
6964#if defined(__Userspace__)
6965 SCTP_TIMERQ_LOCK_DESTROY();
6966 SCTP_ZONE_DESTROY(zone_mbuf);
6967 SCTP_ZONE_DESTROY(zone_clust);
6968 SCTP_ZONE_DESTROY(zone_ext_refcnt);
6969#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006970 /* Get rid of other stuff too. */
6971 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6972 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6973 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6974 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6975 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6976 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6977
Michael Tuexeneb611572020-06-13 17:27:02 +02006978#if defined(_WIN32) || defined(__FreeBSD__) || defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00006979 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6980 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6981 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6982 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6983 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6984 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6985 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6986 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6987 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6988#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006989#if defined(__FreeBSD__) && !defined(__Userspace__)
6990#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexen09631162012-04-18 11:23:02 +00006991 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
tuexendd729232011-11-01 23:04:43 +00006992#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02006993#endif
tuexendd729232011-11-01 23:04:43 +00006994}
6995
tuexendd729232011-11-01 23:04:43 +00006996int
6997sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
tuexen664967a2012-06-28 16:24:36 +00006998 int offset, int limit,
6999 struct sockaddr *src, struct sockaddr *dst,
Michael Tuexen923f7352016-05-01 23:55:35 +02007000 struct sockaddr *altsa, uint16_t port)
tuexendd729232011-11-01 23:04:43 +00007001{
7002 /*
7003 * grub through the INIT pulling addresses and loading them to the
7004 * nets structure in the asoc. The from address in the mbuf should
7005 * also be loaded (if it is not already). This routine can be called
7006 * with either INIT or INIT-ACK's as long as the m points to the IP
7007 * packet and the offset points to the beginning of the parameters.
7008 */
tuexen63fc0bb2011-12-27 12:24:52 +00007009 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00007010 struct sctp_nets *net, *nnet, *net_tmp;
Michael Tuexen473225f2017-06-23 23:05:43 +02007011 struct sctp_paramhdr *phdr, param_buf;
tuexendd729232011-11-01 23:04:43 +00007012 struct sctp_tcb *stcb_tmp;
7013 uint16_t ptype, plen;
7014 struct sockaddr *sa;
tuexendd729232011-11-01 23:04:43 +00007015 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
7016 struct sctp_auth_random *p_random = NULL;
7017 uint16_t random_len = 0;
7018 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
7019 struct sctp_auth_hmac_algo *hmacs = NULL;
7020 uint16_t hmacs_len = 0;
tuexen63fc0bb2011-12-27 12:24:52 +00007021 uint8_t saw_asconf = 0;
7022 uint8_t saw_asconf_ack = 0;
tuexendd729232011-11-01 23:04:43 +00007023 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
7024 struct sctp_auth_chunk_list *chunks = NULL;
7025 uint16_t num_chunks = 0;
7026 sctp_key_t *new_key;
7027 uint32_t keylen;
7028 int got_random = 0, got_hmacs = 0, got_chklist = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007029 uint8_t peer_supports_ecn;
7030 uint8_t peer_supports_prsctp;
7031 uint8_t peer_supports_auth;
7032 uint8_t peer_supports_asconf;
7033 uint8_t peer_supports_asconf_ack;
7034 uint8_t peer_supports_reconfig;
7035 uint8_t peer_supports_nrsack;
7036 uint8_t peer_supports_pktdrop;
Michael Tuexene5001952016-04-17 19:25:27 +02007037 uint8_t peer_supports_idata;
tuexendd729232011-11-01 23:04:43 +00007038#ifdef INET
7039 struct sockaddr_in sin;
7040#endif
7041#ifdef INET6
7042 struct sockaddr_in6 sin6;
7043#endif
7044
7045 /* First get the destination address setup too. */
7046#ifdef INET
7047 memset(&sin, 0, sizeof(sin));
7048 sin.sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00007049#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00007050 sin.sin_len = sizeof(sin);
7051#endif
7052 sin.sin_port = stcb->rport;
7053#endif
7054#ifdef INET6
7055 memset(&sin6, 0, sizeof(sin6));
7056 sin6.sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00007057#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00007058 sin6.sin6_len = sizeof(struct sockaddr_in6);
7059#endif
7060 sin6.sin6_port = stcb->rport;
7061#endif
tuexen664967a2012-06-28 16:24:36 +00007062 if (altsa) {
7063 sa = altsa;
7064 } else {
7065 sa = src;
tuexendd729232011-11-01 23:04:43 +00007066 }
Michael Tuexene5001952016-04-17 19:25:27 +02007067 peer_supports_idata = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007068 peer_supports_ecn = 0;
7069 peer_supports_prsctp = 0;
7070 peer_supports_auth = 0;
7071 peer_supports_asconf = 0;
Michael Tuexen4544fa22020-10-06 13:35:42 +02007072 peer_supports_asconf_ack = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007073 peer_supports_reconfig = 0;
7074 peer_supports_nrsack = 0;
7075 peer_supports_pktdrop = 0;
tuexendd729232011-11-01 23:04:43 +00007076 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7077 /* mark all addresses that we have currently on the list */
7078 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
7079 }
7080 /* does the source address already exist? if so skip it */
tuexen63fc0bb2011-12-27 12:24:52 +00007081 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00007082 atomic_add_int(&stcb->asoc.refcnt, 1);
tuexen664967a2012-06-28 16:24:36 +00007083 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
Michael Tuexena106ab82021-11-26 14:03:15 +01007084 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00007085
7086 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
7087 /* we must add the source address */
7088 /* no scope set here since we have a tcb already. */
7089 switch (sa->sa_family) {
7090#ifdef INET
7091 case AF_INET:
t00fcxend0ad16b2013-02-09 18:34:24 +00007092 if (stcb->asoc.scope.ipv4_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007093 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
tuexendd729232011-11-01 23:04:43 +00007094 return (-1);
7095 }
7096 }
7097 break;
7098#endif
7099#ifdef INET6
7100 case AF_INET6:
t00fcxend0ad16b2013-02-09 18:34:24 +00007101 if (stcb->asoc.scope.ipv6_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007102 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
tuexendd729232011-11-01 23:04:43 +00007103 return (-2);
7104 }
7105 }
7106 break;
7107#endif
tuexen430ca6c2012-07-10 23:36:36 +00007108#if defined(__Userspace__)
7109 case AF_CONN:
t00fcxend0ad16b2013-02-09 18:34:24 +00007110 if (stcb->asoc.scope.conn_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007111 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007112 return (-2);
7113 }
tuexen430ca6c2012-07-10 23:36:36 +00007114 }
7115 break;
7116#endif
tuexendd729232011-11-01 23:04:43 +00007117 default:
7118 break;
7119 }
7120 } else {
7121 if (net_tmp != NULL && stcb_tmp == stcb) {
7122 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
7123 } else if (stcb_tmp != stcb) {
7124 /* It belongs to another association? */
7125 if (stcb_tmp)
7126 SCTP_TCB_UNLOCK(stcb_tmp);
7127 return (-3);
7128 }
7129 }
7130 if (stcb->asoc.state == 0) {
7131 /* the assoc was freed? */
7132 return (-4);
7133 }
tuexendd729232011-11-01 23:04:43 +00007134 /* now we must go through each of the params. */
Michael Tuexen473225f2017-06-23 23:05:43 +02007135 phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007136 while (phdr) {
7137 ptype = ntohs(phdr->param_type);
7138 plen = ntohs(phdr->param_length);
7139 /*
tuexencb5fe8d2012-05-04 09:50:27 +00007140 * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
tuexendd729232011-11-01 23:04:43 +00007141 * (int)plen);
7142 */
7143 if (offset + plen > limit) {
7144 break;
7145 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007146 if (plen < sizeof(struct sctp_paramhdr)) {
tuexendd729232011-11-01 23:04:43 +00007147 break;
7148 }
7149#ifdef INET
7150 if (ptype == SCTP_IPV4_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007151 if (stcb->asoc.scope.ipv4_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007152 struct sctp_ipv4addr_param *p4, p4_buf;
7153
7154 /* ok get the v4 address and check/add */
7155 phdr = sctp_get_next_param(m, offset,
7156 (struct sctp_paramhdr *)&p4_buf,
7157 sizeof(p4_buf));
7158 if (plen != sizeof(struct sctp_ipv4addr_param) ||
7159 phdr == NULL) {
7160 return (-5);
7161 }
7162 p4 = (struct sctp_ipv4addr_param *)phdr;
7163 sin.sin_addr.s_addr = p4->addr;
7164 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7165 /* Skip multi-cast addresses */
7166 goto next_param;
7167 }
7168 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
7169 (sin.sin_addr.s_addr == INADDR_ANY)) {
7170 goto next_param;
7171 }
7172 sa = (struct sockaddr *)&sin;
7173 inp = stcb->sctp_ep;
7174 atomic_add_int(&stcb->asoc.refcnt, 1);
7175 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007176 dst, stcb);
Michael Tuexena106ab82021-11-26 14:03:15 +01007177 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00007178
7179 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7180 inp == NULL) {
7181 /* we must add the source address */
7182 /*
7183 * no scope set since we have a tcb
7184 * already
7185 */
7186
7187 /*
7188 * we must validate the state again
7189 * here
7190 */
7191 add_it_now:
7192 if (stcb->asoc.state == 0) {
7193 /* the assoc was freed? */
7194 return (-7);
7195 }
Michael Tuexen923f7352016-05-01 23:55:35 +02007196 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
tuexendd729232011-11-01 23:04:43 +00007197 return (-8);
7198 }
7199 } else if (stcb_tmp == stcb) {
7200 if (stcb->asoc.state == 0) {
7201 /* the assoc was freed? */
7202 return (-10);
7203 }
7204 if (net != NULL) {
7205 /* clear flag */
7206 net->dest_state &=
7207 ~SCTP_ADDR_NOT_IN_ASSOC;
7208 }
7209 } else {
7210 /*
7211 * strange, address is in another
7212 * assoc? straighten out locks.
7213 */
7214 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007215 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007216 struct mbuf *op_err;
7217 char msg[SCTP_DIAG_INFO_LEN];
7218
tuexendd729232011-11-01 23:04:43 +00007219 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007220 SCTP_SNPRINTF(msg, sizeof(msg),
7221 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007222 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7223 msg);
tuexendd729232011-11-01 23:04:43 +00007224 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tuexen1ade45c2021-07-09 23:32:42 +02007225 stcb_tmp, op_err, false,
Michael Tüxenac142282015-07-28 00:42:03 +02007226 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007227 goto add_it_now;
7228 }
7229 SCTP_TCB_UNLOCK(stcb_tmp);
7230 }
7231
7232 if (stcb->asoc.state == 0) {
7233 /* the assoc was freed? */
7234 return (-12);
7235 }
7236 return (-13);
7237 }
7238 }
7239 } else
7240#endif
7241#ifdef INET6
7242 if (ptype == SCTP_IPV6_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007243 if (stcb->asoc.scope.ipv6_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007244 /* ok get the v6 address and check/add */
7245 struct sctp_ipv6addr_param *p6, p6_buf;
7246
7247 phdr = sctp_get_next_param(m, offset,
7248 (struct sctp_paramhdr *)&p6_buf,
7249 sizeof(p6_buf));
7250 if (plen != sizeof(struct sctp_ipv6addr_param) ||
7251 phdr == NULL) {
7252 return (-14);
7253 }
7254 p6 = (struct sctp_ipv6addr_param *)phdr;
7255 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7256 sizeof(p6->addr));
7257 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7258 /* Skip multi-cast addresses */
7259 goto next_param;
7260 }
7261 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7262 /* Link local make no sense without scope */
7263 goto next_param;
7264 }
7265 sa = (struct sockaddr *)&sin6;
7266 inp = stcb->sctp_ep;
7267 atomic_add_int(&stcb->asoc.refcnt, 1);
7268 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007269 dst, stcb);
Michael Tuexena106ab82021-11-26 14:03:15 +01007270 atomic_subtract_int(&stcb->asoc.refcnt, 1);
tuexendd729232011-11-01 23:04:43 +00007271 if (stcb_tmp == NULL &&
7272 (inp == stcb->sctp_ep || inp == NULL)) {
7273 /*
7274 * we must validate the state again
7275 * here
7276 */
7277 add_it_now6:
7278 if (stcb->asoc.state == 0) {
7279 /* the assoc was freed? */
7280 return (-16);
7281 }
7282 /*
7283 * we must add the address, no scope
7284 * set
7285 */
Michael Tuexen923f7352016-05-01 23:55:35 +02007286 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
tuexendd729232011-11-01 23:04:43 +00007287 return (-17);
7288 }
7289 } else if (stcb_tmp == stcb) {
7290 /*
7291 * we must validate the state again
7292 * here
7293 */
7294 if (stcb->asoc.state == 0) {
7295 /* the assoc was freed? */
7296 return (-19);
7297 }
7298 if (net != NULL) {
7299 /* clear flag */
7300 net->dest_state &=
7301 ~SCTP_ADDR_NOT_IN_ASSOC;
7302 }
7303 } else {
7304 /*
7305 * strange, address is in another
7306 * assoc? straighten out locks.
7307 */
Michael Tüxenac142282015-07-28 00:42:03 +02007308 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007309 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007310 struct mbuf *op_err;
7311 char msg[SCTP_DIAG_INFO_LEN];
7312
tuexendd729232011-11-01 23:04:43 +00007313 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007314 SCTP_SNPRINTF(msg, sizeof(msg),
7315 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007316 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7317 msg);
tuexendd729232011-11-01 23:04:43 +00007318 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tuexen1ade45c2021-07-09 23:32:42 +02007319 stcb_tmp, op_err, false,
Michael Tüxenac142282015-07-28 00:42:03 +02007320 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007321 goto add_it_now6;
7322 }
Michael Tüxenac142282015-07-28 00:42:03 +02007323 SCTP_TCB_UNLOCK(stcb_tmp);
7324 }
tuexendd729232011-11-01 23:04:43 +00007325 if (stcb->asoc.state == 0) {
7326 /* the assoc was freed? */
7327 return (-21);
7328 }
7329 return (-22);
7330 }
7331 }
7332 } else
7333#endif
7334 if (ptype == SCTP_ECN_CAPABLE) {
t00fcxenaac0f452014-08-12 12:51:45 +00007335 peer_supports_ecn = 1;
tuexendd729232011-11-01 23:04:43 +00007336 } else if (ptype == SCTP_ULP_ADAPTATION) {
7337 if (stcb->asoc.state != SCTP_STATE_OPEN) {
7338 struct sctp_adaptation_layer_indication ai, *aip;
7339
7340 phdr = sctp_get_next_param(m, offset,
7341 (struct sctp_paramhdr *)&ai, sizeof(ai));
7342 aip = (struct sctp_adaptation_layer_indication *)phdr;
7343 if (aip) {
7344 stcb->asoc.peers_adaptation = ntohl(aip->indication);
7345 stcb->asoc.adaptation_needed = 1;
7346 }
7347 }
7348 } else if (ptype == SCTP_SET_PRIM_ADDR) {
7349 struct sctp_asconf_addr_param lstore, *fee;
7350 int lptype;
7351 struct sockaddr *lsa = NULL;
7352#ifdef INET
7353 struct sctp_asconf_addrv4_param *fii;
7354#endif
7355
t00fcxenaac0f452014-08-12 12:51:45 +00007356 if (stcb->asoc.asconf_supported == 0) {
7357 return (-100);
7358 }
tuexendd729232011-11-01 23:04:43 +00007359 if (plen > sizeof(lstore)) {
7360 return (-23);
7361 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007362 if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
7363 return (-101);
7364 }
tuexendd729232011-11-01 23:04:43 +00007365 phdr = sctp_get_next_param(m, offset,
7366 (struct sctp_paramhdr *)&lstore,
Michael Tuexen473225f2017-06-23 23:05:43 +02007367 plen);
tuexendd729232011-11-01 23:04:43 +00007368 if (phdr == NULL) {
7369 return (-24);
7370 }
7371 fee = (struct sctp_asconf_addr_param *)phdr;
7372 lptype = ntohs(fee->addrp.ph.param_type);
7373 switch (lptype) {
7374#ifdef INET
7375 case SCTP_IPV4_ADDRESS:
7376 if (plen !=
7377 sizeof(struct sctp_asconf_addrv4_param)) {
7378 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7379 (int)sizeof(struct sctp_asconf_addrv4_param),
7380 plen);
7381 } else {
7382 fii = (struct sctp_asconf_addrv4_param *)fee;
7383 sin.sin_addr.s_addr = fii->addrp.addr;
7384 lsa = (struct sockaddr *)&sin;
7385 }
7386 break;
7387#endif
7388#ifdef INET6
7389 case SCTP_IPV6_ADDRESS:
7390 if (plen !=
7391 sizeof(struct sctp_asconf_addr_param)) {
7392 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7393 (int)sizeof(struct sctp_asconf_addr_param),
7394 plen);
7395 } else {
7396 memcpy(sin6.sin6_addr.s6_addr,
7397 fee->addrp.addr,
7398 sizeof(fee->addrp.addr));
7399 lsa = (struct sockaddr *)&sin6;
7400 }
7401 break;
7402#endif
7403 default:
7404 break;
7405 }
7406 if (lsa) {
7407 (void)sctp_set_primary_addr(stcb, sa, NULL);
7408 }
7409 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7410 stcb->asoc.peer_supports_nat = 1;
7411 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7412 /* Peer supports pr-sctp */
t00fcxenaac0f452014-08-12 12:51:45 +00007413 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007414 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7415 /* A supported extension chunk */
7416 struct sctp_supported_chunk_types_param *pr_supported;
7417 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7418 int num_ent, i;
7419
Michael Tuexen473225f2017-06-23 23:05:43 +02007420 if (plen > sizeof(local_store)) {
7421 return (-35);
7422 }
tuexendd729232011-11-01 23:04:43 +00007423 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02007424 (struct sctp_paramhdr *)&local_store, plen);
tuexendd729232011-11-01 23:04:43 +00007425 if (phdr == NULL) {
7426 return (-25);
7427 }
tuexendd729232011-11-01 23:04:43 +00007428 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7429 num_ent = plen - sizeof(struct sctp_paramhdr);
7430 for (i = 0; i < num_ent; i++) {
7431 switch (pr_supported->chunk_types[i]) {
7432 case SCTP_ASCONF:
t00fcxenaac0f452014-08-12 12:51:45 +00007433 peer_supports_asconf = 1;
t00fcxen10c96982014-12-17 20:37:01 +00007434 break;
tuexendd729232011-11-01 23:04:43 +00007435 case SCTP_ASCONF_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007436 peer_supports_asconf_ack = 1;
tuexendd729232011-11-01 23:04:43 +00007437 break;
7438 case SCTP_FORWARD_CUM_TSN:
t00fcxenaac0f452014-08-12 12:51:45 +00007439 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007440 break;
7441 case SCTP_PACKET_DROPPED:
t00fcxenaac0f452014-08-12 12:51:45 +00007442 peer_supports_pktdrop = 1;
tuexendd729232011-11-01 23:04:43 +00007443 break;
7444 case SCTP_NR_SELECTIVE_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007445 peer_supports_nrsack = 1;
tuexendd729232011-11-01 23:04:43 +00007446 break;
7447 case SCTP_STREAM_RESET:
t00fcxenaac0f452014-08-12 12:51:45 +00007448 peer_supports_reconfig = 1;
tuexendd729232011-11-01 23:04:43 +00007449 break;
7450 case SCTP_AUTHENTICATION:
t00fcxenaac0f452014-08-12 12:51:45 +00007451 peer_supports_auth = 1;
tuexendd729232011-11-01 23:04:43 +00007452 break;
Michael Tuexene5001952016-04-17 19:25:27 +02007453 case SCTP_IDATA:
7454 peer_supports_idata = 1;
7455 break;
tuexendd729232011-11-01 23:04:43 +00007456 default:
7457 /* one I have not learned yet */
7458 break;
tuexendd729232011-11-01 23:04:43 +00007459 }
7460 }
7461 } else if (ptype == SCTP_RANDOM) {
7462 if (plen > sizeof(random_store))
7463 break;
7464 if (got_random) {
7465 /* already processed a RANDOM */
7466 goto next_param;
7467 }
7468 phdr = sctp_get_next_param(m, offset,
7469 (struct sctp_paramhdr *)random_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007470 plen);
tuexendd729232011-11-01 23:04:43 +00007471 if (phdr == NULL)
7472 return (-26);
7473 p_random = (struct sctp_auth_random *)phdr;
7474 random_len = plen - sizeof(*p_random);
7475 /* enforce the random length */
7476 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7477 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7478 return (-27);
7479 }
7480 got_random = 1;
7481 } else if (ptype == SCTP_HMAC_LIST) {
t00fcxen05d0bb92014-09-16 14:25:27 +00007482 uint16_t num_hmacs;
7483 uint16_t i;
tuexendd729232011-11-01 23:04:43 +00007484
7485 if (plen > sizeof(hmacs_store))
7486 break;
7487 if (got_hmacs) {
7488 /* already processed a HMAC list */
7489 goto next_param;
7490 }
7491 phdr = sctp_get_next_param(m, offset,
7492 (struct sctp_paramhdr *)hmacs_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007493 plen);
tuexendd729232011-11-01 23:04:43 +00007494 if (phdr == NULL)
7495 return (-28);
7496 hmacs = (struct sctp_auth_hmac_algo *)phdr;
7497 hmacs_len = plen - sizeof(*hmacs);
7498 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7499 /* validate the hmac list */
7500 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7501 return (-29);
7502 }
7503 if (stcb->asoc.peer_hmacs != NULL)
7504 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7505 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7506 if (stcb->asoc.peer_hmacs != NULL) {
7507 for (i = 0; i < num_hmacs; i++) {
7508 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7509 ntohs(hmacs->hmac_ids[i]));
7510 }
7511 }
7512 got_hmacs = 1;
7513 } else if (ptype == SCTP_CHUNK_LIST) {
7514 int i;
7515
7516 if (plen > sizeof(chunks_store))
7517 break;
7518 if (got_chklist) {
7519 /* already processed a Chunks list */
7520 goto next_param;
7521 }
7522 phdr = sctp_get_next_param(m, offset,
7523 (struct sctp_paramhdr *)chunks_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007524 plen);
tuexendd729232011-11-01 23:04:43 +00007525 if (phdr == NULL)
7526 return (-30);
7527 chunks = (struct sctp_auth_chunk_list *)phdr;
7528 num_chunks = plen - sizeof(*chunks);
7529 if (stcb->asoc.peer_auth_chunks != NULL)
7530 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7531 else
7532 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7533 for (i = 0; i < num_chunks; i++) {
7534 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7535 stcb->asoc.peer_auth_chunks);
7536 /* record asconf/asconf-ack if listed */
7537 if (chunks->chunk_types[i] == SCTP_ASCONF)
7538 saw_asconf = 1;
7539 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7540 saw_asconf_ack = 1;
tuexendd729232011-11-01 23:04:43 +00007541 }
7542 got_chklist = 1;
7543 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7544 (ptype == SCTP_STATE_COOKIE) ||
7545 (ptype == SCTP_UNRECOG_PARAM) ||
7546 (ptype == SCTP_COOKIE_PRESERVE) ||
7547 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7548 (ptype == SCTP_ADD_IP_ADDRESS) ||
7549 (ptype == SCTP_DEL_IP_ADDRESS) ||
7550 (ptype == SCTP_ERROR_CAUSE_IND) ||
7551 (ptype == SCTP_SUCCESS_REPORT)) {
Michael Tuexen196fbbe2020-09-28 17:04:25 +02007552 /* don't care */
tuexendd729232011-11-01 23:04:43 +00007553 } else {
7554 if ((ptype & 0x8000) == 0x0000) {
7555 /*
7556 * must stop processing the rest of the
7557 * param's. Any report bits were handled
7558 * with the call to
7559 * sctp_arethere_unrecognized_parameters()
7560 * when the INIT or INIT-ACK was first seen.
7561 */
7562 break;
7563 }
7564 }
tuexen09631162012-04-18 11:23:02 +00007565
tuexendd729232011-11-01 23:04:43 +00007566 next_param:
7567 offset += SCTP_SIZE32(plen);
7568 if (offset >= limit) {
7569 break;
7570 }
Michael Tuexen473225f2017-06-23 23:05:43 +02007571 phdr = sctp_get_next_param(m, offset, &param_buf,
Michael Tuexen6e026692021-05-03 02:56:21 +02007572 sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007573 }
7574 /* Now check to see if we need to purge any addresses */
7575 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7576 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7577 SCTP_ADDR_NOT_IN_ASSOC) {
7578 /* This address has been removed from the asoc */
7579 /* remove and free it */
7580 stcb->asoc.numnets--;
7581 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
Michael Tuexen6e026692021-05-03 02:56:21 +02007582 if (net == stcb->asoc.alternate) {
7583 sctp_free_remote_addr(stcb->asoc.alternate);
7584 stcb->asoc.alternate = NULL;
7585 }
tuexendd729232011-11-01 23:04:43 +00007586 if (net == stcb->asoc.primary_destination) {
7587 stcb->asoc.primary_destination = NULL;
7588 sctp_select_primary_destination(stcb);
7589 }
Michael Tuexen6e026692021-05-03 02:56:21 +02007590 sctp_free_remote_addr(net);
tuexendd729232011-11-01 23:04:43 +00007591 }
7592 }
t00fcxenaac0f452014-08-12 12:51:45 +00007593 if ((stcb->asoc.ecn_supported == 1) &&
7594 (peer_supports_ecn == 0)) {
7595 stcb->asoc.ecn_supported = 0;
tuexendd729232011-11-01 23:04:43 +00007596 }
t00fcxenaac0f452014-08-12 12:51:45 +00007597 if ((stcb->asoc.prsctp_supported == 1) &&
7598 (peer_supports_prsctp == 0)) {
7599 stcb->asoc.prsctp_supported = 0;
7600 }
7601 if ((stcb->asoc.auth_supported == 1) &&
7602 ((peer_supports_auth == 0) ||
7603 (got_random == 0) || (got_hmacs == 0))) {
7604 stcb->asoc.auth_supported = 0;
7605 }
7606 if ((stcb->asoc.asconf_supported == 1) &&
7607 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
7608 (stcb->asoc.auth_supported == 0) ||
7609 (saw_asconf == 0) || (saw_asconf_ack == 0))) {
7610 stcb->asoc.asconf_supported = 0;
7611 }
7612 if ((stcb->asoc.reconfig_supported == 1) &&
7613 (peer_supports_reconfig == 0)) {
7614 stcb->asoc.reconfig_supported = 0;
7615 }
Michael Tuexene5001952016-04-17 19:25:27 +02007616 if ((stcb->asoc.idata_supported == 1) &&
7617 (peer_supports_idata == 0)) {
7618 stcb->asoc.idata_supported = 0;
7619 }
t00fcxenaac0f452014-08-12 12:51:45 +00007620 if ((stcb->asoc.nrsack_supported == 1) &&
7621 (peer_supports_nrsack == 0)) {
7622 stcb->asoc.nrsack_supported = 0;
7623 }
7624 if ((stcb->asoc.pktdrop_supported == 1) &&
Michael Tuexen8d6935d2022-05-15 01:51:24 +02007625 (peer_supports_pktdrop == 0)) {
t00fcxenaac0f452014-08-12 12:51:45 +00007626 stcb->asoc.pktdrop_supported = 0;
7627 }
7628 /* validate authentication required parameters */
7629 if ((peer_supports_auth == 0) && (got_chklist == 1)) {
tuexendd729232011-11-01 23:04:43 +00007630 /* peer does not support auth but sent a chunks list? */
7631 return (-31);
7632 }
t00fcxenaac0f452014-08-12 12:51:45 +00007633 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
tuexendd729232011-11-01 23:04:43 +00007634 /* peer supports asconf but not auth? */
7635 return (-32);
t00fcxenaac0f452014-08-12 12:51:45 +00007636 } else if ((peer_supports_asconf == 1) &&
7637 (peer_supports_auth == 1) &&
tuexendd729232011-11-01 23:04:43 +00007638 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7639 return (-33);
7640 }
7641 /* concatenate the full random key */
7642 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7643 if (chunks != NULL) {
7644 keylen += sizeof(*chunks) + num_chunks;
7645 }
7646 new_key = sctp_alloc_key(keylen);
7647 if (new_key != NULL) {
7648 /* copy in the RANDOM */
7649 if (p_random != NULL) {
7650 keylen = sizeof(*p_random) + random_len;
Michael Tuexend0853302017-07-19 17:01:31 +02007651 memcpy(new_key->key, p_random, keylen);
Michael Tuexen8789a6d2018-06-02 18:33:47 +02007652 } else {
7653 keylen = 0;
tuexendd729232011-11-01 23:04:43 +00007654 }
7655 /* append in the AUTH chunks */
7656 if (chunks != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007657 memcpy(new_key->key + keylen, chunks,
7658 sizeof(*chunks) + num_chunks);
tuexendd729232011-11-01 23:04:43 +00007659 keylen += sizeof(*chunks) + num_chunks;
7660 }
7661 /* append in the HMACs */
7662 if (hmacs != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007663 memcpy(new_key->key + keylen, hmacs,
7664 sizeof(*hmacs) + hmacs_len);
tuexendd729232011-11-01 23:04:43 +00007665 }
7666 } else {
7667 /* failed to get memory for the key */
7668 return (-34);
7669 }
7670 if (stcb->asoc.authinfo.peer_random != NULL)
7671 sctp_free_key(stcb->asoc.authinfo.peer_random);
7672 stcb->asoc.authinfo.peer_random = new_key;
7673 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7674 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7675
7676 return (0);
7677}
7678
7679int
7680sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7681 struct sctp_nets *net)
7682{
7683 /* make sure the requested primary address exists in the assoc */
7684 if (net == NULL && sa)
7685 net = sctp_findnet(stcb, sa);
7686
7687 if (net == NULL) {
7688 /* didn't find the requested primary address! */
7689 return (-1);
7690 } else {
7691 /* set the primary address */
7692 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7693 /* Must be confirmed, so queue to set */
7694 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7695 return (0);
7696 }
7697 stcb->asoc.primary_destination = net;
Michael Tuexen8d6935d2022-05-15 01:51:24 +02007698 if (((net->dest_state & SCTP_ADDR_PF) == 0) &&
7699 (stcb->asoc.alternate != NULL)) {
tuexendd729232011-11-01 23:04:43 +00007700 sctp_free_remote_addr(stcb->asoc.alternate);
7701 stcb->asoc.alternate = NULL;
7702 }
7703 net = TAILQ_FIRST(&stcb->asoc.nets);
7704 if (net != stcb->asoc.primary_destination) {
7705 /* first one on the list is NOT the primary
7706 * sctp_cmpaddr() is much more efficient if
7707 * the primary is the first on the list, make it
7708 * so.
7709 */
7710 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7711 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7712 }
7713 return (0);
7714 }
7715}
7716
Michael Tuexen4ec13f72021-09-09 01:22:43 +02007717bool
tuexen9784e9a2011-12-18 13:04:23 +00007718sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
tuexendd729232011-11-01 23:04:43 +00007719{
tuexendd729232011-11-01 23:04:43 +00007720 struct sctpasochead *head;
7721 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00007722
Michael Tuexen8dd0be72021-09-12 19:27:59 +02007723 SCTP_INP_INFO_LOCK_ASSERT();
7724
Michael Tuexen4ec13f72021-09-09 01:22:43 +02007725 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))];
tuexendd729232011-11-01 23:04:43 +00007726 LIST_FOREACH(stcb, head, sctp_asocs) {
7727 /* We choose not to lock anything here. TCB's can't be
7728 * removed since we have the read lock, so they can't
7729 * be freed on us, same thing for the INP. I may
7730 * be wrong with this assumption, but we will go
7731 * with it for now :-)
7732 */
7733 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7734 continue;
7735 }
7736 if (stcb->asoc.my_vtag == tag) {
7737 /* candidate */
7738 if (stcb->rport != rport) {
7739 continue;
7740 }
7741 if (stcb->sctp_ep->sctp_lport != lport) {
7742 continue;
7743 }
Michael Tuexen4ec13f72021-09-09 01:22:43 +02007744 /* The tag is currently used, so don't use it. */
Michael Tuexen8dd0be72021-09-12 19:27:59 +02007745 return (false);
tuexendd729232011-11-01 23:04:43 +00007746 }
7747 }
Michael Tuexen8dd0be72021-09-12 19:27:59 +02007748 return (!sctp_is_in_timewait(tag, lport, rport, (uint32_t)now->tv_sec));
tuexendd729232011-11-01 23:04:43 +00007749}
7750
tuexendd729232011-11-01 23:04:43 +00007751static void
tuexen9784e9a2011-12-18 13:04:23 +00007752sctp_drain_mbufs(struct sctp_tcb *stcb)
tuexendd729232011-11-01 23:04:43 +00007753{
7754 /*
7755 * We must hunt this association for MBUF's past the cumack (i.e.
7756 * out of order data that we can renege on).
7757 */
7758 struct sctp_association *asoc;
7759 struct sctp_tmit_chunk *chk, *nchk;
7760 uint32_t cumulative_tsn_p1;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007761 struct sctp_queued_to_read *control, *ncontrol;
tuexendd729232011-11-01 23:04:43 +00007762 int cnt, strmat;
7763 uint32_t gap, i;
tuexen63fc0bb2011-12-27 12:24:52 +00007764 int fnd = 0;
tuexendd729232011-11-01 23:04:43 +00007765
7766 /* We look for anything larger than the cum-ack + 1 */
7767
7768 asoc = &stcb->asoc;
7769 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7770 /* none we can reneg on. */
7771 return;
7772 }
7773 SCTP_STAT_INCR(sctps_protocol_drains_done);
7774 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7775 cnt = 0;
tuexendd729232011-11-01 23:04:43 +00007776 /* Ok that was fun, now we will drain all the inbound streams? */
7777 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007778 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007779#ifdef INVARIANTS
Michael Tuexen53579be2020-10-07 17:39:17 +02007780 if (control->on_strm_q != SCTP_ON_ORDERED) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007781 panic("Huh control: %p on_q: %d -- not ordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007782 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007783 }
7784#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007785 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
tuexendd729232011-11-01 23:04:43 +00007786 /* Yep it is above cum-ack */
7787 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007788 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7789 KASSERT(control->length > 0, ("control has zero length"));
7790 if (asoc->size_on_all_streams >= control->length) {
7791 asoc->size_on_all_streams -= control->length;
7792 } else {
7793#ifdef INVARIANTS
7794 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7795#else
7796 asoc->size_on_all_streams = 0;
7797#endif
7798 }
tuexendd729232011-11-01 23:04:43 +00007799 sctp_ucount_decr(asoc->cnt_on_all_streams);
7800 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007801 if (control->on_read_q) {
7802 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7803 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007804 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007805 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
7806 control->on_strm_q = 0;
7807 if (control->data) {
7808 sctp_m_freem(control->data);
7809 control->data = NULL;
tuexendd729232011-11-01 23:04:43 +00007810 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007811 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007812 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007813 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007814 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007815 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007816 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7817 if (asoc->size_on_reasm_queue >= chk->send_size) {
7818 asoc->size_on_reasm_queue -= chk->send_size;
7819 } else {
7820#ifdef INVARIANTS
7821 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7822#else
7823 asoc->size_on_reasm_queue = 0;
7824#endif
7825 }
Michael Tuexene5001952016-04-17 19:25:27 +02007826 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7827 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007828 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02007829 if (chk->data) {
7830 sctp_m_freem(chk->data);
7831 chk->data = NULL;
7832 }
7833 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7834 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007835 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02007836 }
7837 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007838 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007839#ifdef INVARIANTS
Michael Tuexen53579be2020-10-07 17:39:17 +02007840 if (control->on_strm_q != SCTP_ON_UNORDERED) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007841 panic("Huh control: %p on_q: %d -- not unordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007842 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007843 }
7844#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007845 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
Michael Tuexene5001952016-04-17 19:25:27 +02007846 /* Yep it is above cum-ack */
7847 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007848 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7849 KASSERT(control->length > 0, ("control has zero length"));
7850 if (asoc->size_on_all_streams >= control->length) {
7851 asoc->size_on_all_streams -= control->length;
7852 } else {
7853#ifdef INVARIANTS
7854 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7855#else
7856 asoc->size_on_all_streams = 0;
7857#endif
7858 }
Michael Tuexene5001952016-04-17 19:25:27 +02007859 sctp_ucount_decr(asoc->cnt_on_all_streams);
7860 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007861 if (control->on_read_q) {
7862 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7863 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007864 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007865 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
7866 control->on_strm_q = 0;
7867 if (control->data) {
7868 sctp_m_freem(control->data);
7869 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02007870 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007871 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007872 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007873 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007874 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007875 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007876 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7877 if (asoc->size_on_reasm_queue >= chk->send_size) {
7878 asoc->size_on_reasm_queue -= chk->send_size;
7879 } else {
7880#ifdef INVARIANTS
7881 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7882#else
7883 asoc->size_on_reasm_queue = 0;
7884#endif
7885 }
Michael Tuexene5001952016-04-17 19:25:27 +02007886 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7887 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007888 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02007889 if (chk->data) {
7890 sctp_m_freem(chk->data);
7891 chk->data = NULL;
7892 }
7893 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7894 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007895 sctp_free_a_readq(stcb, control);
tuexendd729232011-11-01 23:04:43 +00007896 }
7897 }
7898 }
7899 if (cnt) {
7900 /* We must back down to see what the new highest is */
7901 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
7902 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
7903 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
7904 asoc->highest_tsn_inside_map = i;
7905 fnd = 1;
7906 break;
7907 }
7908 }
7909 if (!fnd) {
7910 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
7911 }
7912
7913 /*
7914 * Question, should we go through the delivery queue? The only
7915 * reason things are on here is the app not reading OR a p-d-api up.
7916 * An attacker COULD send enough in to initiate the PD-API and then
7917 * send a bunch of stuff to other streams... these would wind up on
7918 * the delivery queue.. and then we would not get to them. But in
7919 * order to do this I then have to back-track and un-deliver
7920 * sequence numbers in streams.. el-yucko. I think for now we will
7921 * NOT look at the delivery queue and leave it to be something to
7922 * consider later. An alternative would be to abort the P-D-API with
7923 * a notification and then deliver the data.... Or another method
7924 * might be to keep track of how many times the situation occurs and
7925 * if we see a possible attack underway just abort the association.
7926 */
7927#ifdef SCTP_DEBUG
7928 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
7929#endif
7930 /*
7931 * Now do we need to find a new
7932 * asoc->highest_tsn_inside_map?
7933 */
7934 asoc->last_revoke_count = cnt;
Michael Tuexen61f66832020-02-09 23:15:19 +01007935 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
Michael Tuexen21b55df2020-02-09 23:46:32 +01007936 SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
tuexendd729232011-11-01 23:04:43 +00007937 /*sa_ignore NO_NULL_CHK*/
7938 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7939 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007940 }
7941 /*
7942 * Another issue, in un-setting the TSN's in the mapping array we
7943 * DID NOT adjust the highest_tsn marker. This will cause one of two
7944 * things to occur. It may cause us to do extra work in checking for
7945 * our mapping array movement. More importantly it may cause us to
7946 * SACK every datagram. This may not be a bad thing though since we
7947 * will recover once we get our cum-ack above and all this stuff we
7948 * dumped recovered.
7949 */
7950}
7951
7952void
Michael Tuexene19d1222022-08-03 17:35:14 +02007953sctp_drain(void)
tuexendd729232011-11-01 23:04:43 +00007954{
7955 /*
7956 * We must walk the PCB lists for ALL associations here. The system
7957 * is LOW on MBUF's and needs help. This is where reneging will
7958 * occur. We really hope this does NOT happen!
7959 */
Michael Tuexen5be0c252020-06-13 00:53:56 +02007960#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00007961 VNET_ITERATOR_DECL(vnet_iter);
7962#else
7963 struct sctp_inpcb *inp;
7964 struct sctp_tcb *stcb;
7965
7966 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7967 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7968 return;
7969 }
7970#endif
Michael Tuexen5be0c252020-06-13 00:53:56 +02007971#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00007972 VNET_LIST_RLOCK_NOSLEEP();
7973 VNET_FOREACH(vnet_iter) {
7974 CURVNET_SET(vnet_iter);
7975 struct sctp_inpcb *inp;
7976 struct sctp_tcb *stcb;
7977#endif
7978
Michael Tuexen5be0c252020-06-13 00:53:56 +02007979#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00007980 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7981 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7982#ifdef VIMAGE
7983 continue;
7984#else
7985 return;
tuexen09631162012-04-18 11:23:02 +00007986#endif
tuexendd729232011-11-01 23:04:43 +00007987 }
7988#endif
7989 SCTP_INP_INFO_RLOCK();
7990 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7991 /* For each endpoint */
7992 SCTP_INP_RLOCK(inp);
7993 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7994 /* For each association */
7995 SCTP_TCB_LOCK(stcb);
tuexen9784e9a2011-12-18 13:04:23 +00007996 sctp_drain_mbufs(stcb);
tuexendd729232011-11-01 23:04:43 +00007997 SCTP_TCB_UNLOCK(stcb);
7998 }
7999 SCTP_INP_RUNLOCK(inp);
8000 }
8001 SCTP_INP_INFO_RUNLOCK();
Michael Tuexen5be0c252020-06-13 00:53:56 +02008002#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00008003 CURVNET_RESTORE();
8004 }
8005 VNET_LIST_RUNLOCK_NOSLEEP();
8006#endif
8007}
8008
8009/*
8010 * start a new iterator
8011 * iterates through all endpoints and associations based on the pcb_state
8012 * flags and asoc_state. "af" (mandatory) is executed for all matching
8013 * assocs and "ef" (optional) is executed when the iterator completes.
8014 * "inpf" (optional) is executed for each new endpoint as it is being
8015 * iterated through. inpe (optional) is called when the inp completes
8016 * its way through all the stcbs.
8017 */
8018int
8019sctp_initiate_iterator(inp_func inpf,
8020 asoc_func af,
8021 inp_func inpe,
8022 uint32_t pcb_state,
8023 uint32_t pcb_features,
8024 uint32_t asoc_state,
8025 void *argp,
8026 uint32_t argi,
8027 end_func ef,
8028 struct sctp_inpcb *s_inp,
8029 uint8_t chunk_output_off)
8030{
8031 struct sctp_iterator *it = NULL;
8032
8033 if (af == NULL) {
8034 return (-1);
8035 }
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008036 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8037 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
8038 SCTP_BASE_VAR(sctp_pcb_initialized));
8039 return (-1);
8040 }
tuexendd729232011-11-01 23:04:43 +00008041 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
8042 SCTP_M_ITER);
8043 if (it == NULL) {
8044 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
Michael Tuexen287284c2020-01-12 14:34:21 +01008045 return (-1);
tuexendd729232011-11-01 23:04:43 +00008046 }
8047 memset(it, 0, sizeof(*it));
8048 it->function_assoc = af;
8049 it->function_inp = inpf;
8050 if (inpf)
8051 it->done_current_ep = 0;
8052 else
8053 it->done_current_ep = 1;
8054 it->function_atend = ef;
8055 it->pointer = argp;
8056 it->val = argi;
8057 it->pcb_flags = pcb_state;
8058 it->pcb_features = pcb_features;
8059 it->asoc_state = asoc_state;
8060 it->function_inp_end = inpe;
8061 it->no_chunk_output = chunk_output_off;
Michael Tuexen5be0c252020-06-13 00:53:56 +02008062#if defined(__FreeBSD__) && !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00008063 it->vn = curvnet;
tuexen09631162012-04-18 11:23:02 +00008064#endif
tuexendd729232011-11-01 23:04:43 +00008065 if (s_inp) {
8066 /* Assume lock is held here */
8067 it->inp = s_inp;
8068 SCTP_INP_INCR_REF(it->inp);
8069 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
8070 } else {
8071 SCTP_INP_INFO_RLOCK();
8072 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
8073 if (it->inp) {
8074 SCTP_INP_INCR_REF(it->inp);
8075 }
8076 SCTP_INP_INFO_RUNLOCK();
8077 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
tuexendd729232011-11-01 23:04:43 +00008078 }
8079 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008080 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8081 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8082 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
8083 SCTP_BASE_VAR(sctp_pcb_initialized), it);
8084 SCTP_FREE(it, SCTP_M_ITER);
8085 return (-1);
8086 }
tuexendd729232011-11-01 23:04:43 +00008087 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
8088 if (sctp_it_ctl.iterator_running == 0) {
8089 sctp_wakeup_iterator();
8090 }
8091 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8092 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
8093 return (0);
8094}
Michael Tuexen9b353c62022-06-04 11:20:39 +02008095
8096/*
8097 * Atomically add flags to the sctp_flags of an inp.
8098 * To be used when the write lock of the inp is not held.
8099 */
8100void
8101sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags)
8102{
8103 uint32_t old_flags, new_flags;
8104
8105 do {
8106 old_flags = inp->sctp_flags;
8107 new_flags = old_flags | flags;
8108 } while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0);
8109}