blob: f134da3aa588eba4fbbff7b76718e513c03ef07a [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
tuexendd729232011-11-01 23:04:43 +000035#ifdef __FreeBSD__
36#include <sys/cdefs.h>
Michael Tuexenedd369d2020-05-19 09:42:15 +020037__FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.c 361243 2020-05-19 07:23:35Z tuexen $");
tuexendd729232011-11-01 23:04:43 +000038#endif
39
40#include <netinet/sctp_os.h>
41#ifdef __FreeBSD__
42#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)
tuexen9784e9a2011-12-18 13:04:23 +000055#if !defined(__Userspace_os_Windows)
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
tuexendd729232011-11-01 23:04:43 +000066#if defined(__FreeBSD__)
67#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>
t00fcxen37058da2015-03-16 21:59:21 +000074#if !defined(__Userspace_os_Windows)
75#include <netdb.h>
76#endif
tuexen5154a002011-12-23 18:47:55 +000077#endif
tuexendd729232011-11-01 23:04:43 +000078
79#if defined(__APPLE__)
80#define APPLE_FILE_NO 4
81#endif
82
83#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
84VNET_DEFINE(struct sctp_base_info, system_base_info);
85#else
86struct sctp_base_info system_base_info;
87#endif
88
tuexendd729232011-11-01 23:04:43 +000089/* FIX: we don't handle multiple link local scopes */
90/* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
91#ifdef INET6
92int
93SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
94{
95#ifdef SCTP_EMBEDDED_V6_SCOPE
tuexene3851592012-06-01 08:31:45 +000096#if defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +000097 struct in6_addr tmp_a, tmp_b;
98
99 tmp_a = a->sin6_addr;
tuexen9a218b12012-08-04 21:17:58 +0000100#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +0000101 if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) {
tuexen9a218b12012-08-04 21:17:58 +0000102#else
103 if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +0000104#endif
tuexen63fc0bb2011-12-27 12:24:52 +0000105 return (0);
tuexendd729232011-11-01 23:04:43 +0000106 }
107 tmp_b = b->sin6_addr;
tuexen9a218b12012-08-04 21:17:58 +0000108#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +0000109 if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) {
tuexen9a218b12012-08-04 21:17:58 +0000110#else
111 if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +0000112#endif
tuexen63fc0bb2011-12-27 12:24:52 +0000113 return (0);
tuexendd729232011-11-01 23:04:43 +0000114 }
115 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
116#elif defined(SCTP_KAME)
117 struct sockaddr_in6 tmp_a, tmp_b;
118
119 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
120 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000121 return (0);
tuexendd729232011-11-01 23:04:43 +0000122 }
123 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
124 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000125 return (0);
tuexendd729232011-11-01 23:04:43 +0000126 }
127 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
128#else
129 struct in6_addr tmp_a, tmp_b;
130
131 tmp_a = a->sin6_addr;
132 if (in6_embedscope(&tmp_a, a) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000133 return (0);
tuexendd729232011-11-01 23:04:43 +0000134 }
135 tmp_b = b->sin6_addr;
136 if (in6_embedscope(&tmp_b, b) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000137 return (0);
tuexendd729232011-11-01 23:04:43 +0000138 }
139 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
140#endif
141#else
tuexen5154a002011-12-23 18:47:55 +0000142 return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr)));
tuexendd729232011-11-01 23:04:43 +0000143#endif /* SCTP_EMBEDDED_V6_SCOPE */
144}
145#endif
146
147void
148sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
149{
150 /*
151 * We really don't need to lock this, but I will just because it
152 * does not hurt.
153 */
tuexendd729232011-11-01 23:04:43 +0000154 SCTP_INP_INFO_RLOCK();
155 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
156 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
157 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
158 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
159 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
160 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
161 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
162 spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
tuexendd729232011-11-01 23:04:43 +0000163 SCTP_INP_INFO_RUNLOCK();
164}
165
t00fcxend0ad16b2013-02-09 18:34:24 +0000166/*-
tuexendd729232011-11-01 23:04:43 +0000167 * Addresses are added to VRF's (Virtual Router's). For BSD we
168 * have only the default VRF 0. We maintain a hash list of
169 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
170 * these has a list of addresses. When we add a new address
171 * to a VRF we lookup the ifn/ifn_index, if the ifn does
172 * not exist we create it and add it to the list of IFN's
173 * within the VRF. Once we have the sctp_ifn, we add the
174 * address to the list. So we look something like:
175 *
176 * hash-vrf-table
177 * vrf-> ifn-> ifn -> ifn
178 * vrf |
179 * ... +--ifa-> ifa -> ifa
180 * vrf
181 *
182 * We keep these separate lists since the SCTP subsystem will
183 * point to these from its source address selection nets structure.
184 * When an address is deleted it does not happen right away on
185 * the SCTP side, it gets scheduled. What we do when a
186 * delete happens is immediately remove the address from
187 * the master list and decrement the refcount. As our
188 * addip iterator works through and frees the src address
189 * selection pointing to the sctp_ifa, eventually the refcount
190 * will reach 0 and we will delete it. Note that it is assumed
191 * that any locking on system level ifn/ifa is done at the
192 * caller of these functions and these routines will only
193 * lock the SCTP structures as they add or delete things.
194 *
195 * Other notes on VRF concepts.
196 * - An endpoint can be in multiple VRF's
197 * - An association lives within a VRF and only one VRF.
198 * - Any incoming packet we can deduce the VRF for by
199 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
200 * - Any downward send call or connect call must supply the
201 * VRF via ancillary data or via some sort of set default
202 * VRF socket option call (again for BSD no brainer since
203 * the VRF is always 0).
204 * - An endpoint may add multiple VRF's to it.
205 * - Listening sockets can accept associations in any
206 * of the VRF's they are in but the assoc will end up
207 * in only one VRF (gotten from the packet or connect/send).
208 *
209 */
210
211struct sctp_vrf *
212sctp_allocate_vrf(int vrf_id)
213{
tuexen63fc0bb2011-12-27 12:24:52 +0000214 struct sctp_vrf *vrf = NULL;
tuexendd729232011-11-01 23:04:43 +0000215 struct sctp_vrflist *bucket;
216
217 /* First allocate the VRF structure */
218 vrf = sctp_find_vrf(vrf_id);
219 if (vrf) {
220 /* Already allocated */
221 return (vrf);
222 }
223 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
224 SCTP_M_VRF);
t00fcxen8dd95b82014-07-11 20:41:28 +0000225 if (vrf == NULL) {
226 /* No memory */
tuexendd729232011-11-01 23:04:43 +0000227#ifdef INVARIANTS
228 panic("No memory for VRF:%d", vrf_id);
229#endif
230 return (NULL);
231 }
232 /* setup the VRF */
233 memset(vrf, 0, sizeof(struct sctp_vrf));
234 vrf->vrf_id = vrf_id;
235 LIST_INIT(&vrf->ifnlist);
236 vrf->total_ifa_count = 0;
237 vrf->refcount = 0;
238 /* now also setup table ids */
239 SCTP_INIT_VRF_TABLEID(vrf);
240 /* Init the HASH of addresses */
241 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
242 &vrf->vrf_addr_hashmark);
243 if (vrf->vrf_addr_hash == NULL) {
t00fcxen8dd95b82014-07-11 20:41:28 +0000244 /* No memory */
tuexendd729232011-11-01 23:04:43 +0000245#ifdef INVARIANTS
246 panic("No memory for VRF:%d", vrf_id);
247#endif
248 SCTP_FREE(vrf, SCTP_M_VRF);
249 return (NULL);
250 }
251
252 /* Add it to the hash table */
253 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
254 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
255 atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
256 return (vrf);
257}
258
259
260struct sctp_ifn *
261sctp_find_ifn(void *ifn, uint32_t ifn_index)
262{
263 struct sctp_ifn *sctp_ifnp;
264 struct sctp_ifnlist *hash_ifn_head;
265
266 /* We assume the lock is held for the addresses
267 * if that's wrong problems could occur :-)
268 */
269 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
270 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
271 if (sctp_ifnp->ifn_index == ifn_index) {
tuexen63fc0bb2011-12-27 12:24:52 +0000272 return (sctp_ifnp);
tuexendd729232011-11-01 23:04:43 +0000273 }
274 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
tuexen63fc0bb2011-12-27 12:24:52 +0000275 return (sctp_ifnp);
tuexendd729232011-11-01 23:04:43 +0000276 }
277 }
tuexen63fc0bb2011-12-27 12:24:52 +0000278 return (NULL);
tuexendd729232011-11-01 23:04:43 +0000279}
280
281
tuexendd729232011-11-01 23:04:43 +0000282struct sctp_vrf *
283sctp_find_vrf(uint32_t vrf_id)
284{
285 struct sctp_vrflist *bucket;
286 struct sctp_vrf *liste;
t00fcxend0ad16b2013-02-09 18:34:24 +0000287
tuexendd729232011-11-01 23:04:43 +0000288 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
289 LIST_FOREACH(liste, bucket, next_vrf) {
290 if (vrf_id == liste->vrf_id) {
tuexen63fc0bb2011-12-27 12:24:52 +0000291 return (liste);
tuexendd729232011-11-01 23:04:43 +0000292 }
293 }
294 return (NULL);
295}
296
t00fcxend0ad16b2013-02-09 18:34:24 +0000297
tuexendd729232011-11-01 23:04:43 +0000298void
299sctp_free_vrf(struct sctp_vrf *vrf)
300{
301 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
302 if (vrf->vrf_addr_hash) {
303 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
304 vrf->vrf_addr_hash = NULL;
305 }
306 /* We zero'd the count */
307 LIST_REMOVE(vrf, next_vrf);
308 SCTP_FREE(vrf, SCTP_M_VRF);
309 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
310 }
311}
312
t00fcxend0ad16b2013-02-09 18:34:24 +0000313
tuexendd729232011-11-01 23:04:43 +0000314void
315sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
316{
317 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
318 /* We zero'd the count */
319 if (sctp_ifnp->vrf) {
320 sctp_free_vrf(sctp_ifnp->vrf);
321 }
322 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
323 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
324 }
325}
326
t00fcxend0ad16b2013-02-09 18:34:24 +0000327
tuexendd729232011-11-01 23:04:43 +0000328void
329sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
330{
331 struct sctp_ifn *sctp_ifnp;
332
333 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
334 if (sctp_ifnp != NULL) {
335 sctp_ifnp->ifn_mtu = mtu;
336 }
337}
338
339
340void
341sctp_free_ifa(struct sctp_ifa *sctp_ifap)
342{
343 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
344 /* We zero'd the count */
345 if (sctp_ifap->ifn_p) {
346 sctp_free_ifn(sctp_ifap->ifn_p);
347 }
348 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
349 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
350 }
351}
352
t00fcxend0ad16b2013-02-09 18:34:24 +0000353
tuexendd729232011-11-01 23:04:43 +0000354static void
355sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
356{
357 struct sctp_ifn *found;
358
359 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
360 if (found == NULL) {
361 /* Not in the list.. sorry */
362 return;
363 }
364 if (hold_addr_lock == 0)
365 SCTP_IPI_ADDR_WLOCK();
366 LIST_REMOVE(sctp_ifnp, next_bucket);
367 LIST_REMOVE(sctp_ifnp, next_ifn);
368 SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index,
369 sctp_ifnp->registered_af);
370 if (hold_addr_lock == 0)
371 SCTP_IPI_ADDR_WUNLOCK();
372 /* Take away the reference, and possibly free it */
373 sctp_free_ifn(sctp_ifnp);
374}
375
t00fcxend0ad16b2013-02-09 18:34:24 +0000376
tuexendd729232011-11-01 23:04:43 +0000377void
378sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
379 const char *if_name, uint32_t ifn_index)
380{
381 struct sctp_vrf *vrf;
t00fcxend0ad16b2013-02-09 18:34:24 +0000382 struct sctp_ifa *sctp_ifap;
383
tuexendd729232011-11-01 23:04:43 +0000384 SCTP_IPI_ADDR_RLOCK();
385 vrf = sctp_find_vrf(vrf_id);
386 if (vrf == NULL) {
387 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
388 goto out;
389
390 }
391 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
392 if (sctp_ifap == NULL) {
393 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
394 goto out;
tuexen0e813942011-11-18 09:18:32 +0000395 }
tuexendd729232011-11-01 23:04:43 +0000396 if (sctp_ifap->ifn_p == NULL) {
Michael Tuexen34488e72016-05-03 22:11:59 +0200397 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
tuexendd729232011-11-01 23:04:43 +0000398 goto out;
399 }
400 if (if_name) {
tuexen0e813942011-11-18 09:18:32 +0000401 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
tuexendd729232011-11-01 23:04:43 +0000402 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
tuexen0e813942011-11-18 09:18:32 +0000403 sctp_ifap->ifn_p->ifn_name, if_name);
tuexendd729232011-11-01 23:04:43 +0000404 goto out;
405 }
406 } else {
407 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
408 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
409 sctp_ifap->ifn_p->ifn_index, ifn_index);
410 goto out;
411 }
412 }
tuexen0e813942011-11-18 09:18:32 +0000413
tuexendd729232011-11-01 23:04:43 +0000414 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
415 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
416 out:
417 SCTP_IPI_ADDR_RUNLOCK();
418}
419
t00fcxend0ad16b2013-02-09 18:34:24 +0000420
tuexendd729232011-11-01 23:04:43 +0000421void
422sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
423 const char *if_name, uint32_t ifn_index)
424{
425 struct sctp_vrf *vrf;
t00fcxend0ad16b2013-02-09 18:34:24 +0000426 struct sctp_ifa *sctp_ifap;
427
tuexendd729232011-11-01 23:04:43 +0000428 SCTP_IPI_ADDR_RLOCK();
429 vrf = sctp_find_vrf(vrf_id);
430 if (vrf == NULL) {
431 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
432 goto out;
433
434 }
435 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
436 if (sctp_ifap == NULL) {
437 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
438 goto out;
tuexen0e813942011-11-18 09:18:32 +0000439 }
tuexendd729232011-11-01 23:04:43 +0000440 if (sctp_ifap->ifn_p == NULL) {
Michael Tuexen34488e72016-05-03 22:11:59 +0200441 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
tuexendd729232011-11-01 23:04:43 +0000442 goto out;
443 }
444 if (if_name) {
tuexen0e813942011-11-18 09:18:32 +0000445 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
tuexendd729232011-11-01 23:04:43 +0000446 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
tuexen0e813942011-11-18 09:18:32 +0000447 sctp_ifap->ifn_p->ifn_name, if_name);
tuexendd729232011-11-01 23:04:43 +0000448 goto out;
449 }
450 } else {
451 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
452 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
453 sctp_ifap->ifn_p->ifn_index, ifn_index);
454 goto out;
455 }
456 }
457
458 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
459 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
460 out:
461 SCTP_IPI_ADDR_RUNLOCK();
462}
463
t00fcxend0ad16b2013-02-09 18:34:24 +0000464
tuexendd729232011-11-01 23:04:43 +0000465/*-
466 * Add an ifa to an ifn.
467 * Register the interface as necessary.
468 * NOTE: ADDR write lock MUST be held.
469 */
470static void
471sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
472{
473 int ifa_af;
474
475 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
476 sctp_ifap->ifn_p = sctp_ifnp;
477 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
478 /* update address counts */
479 sctp_ifnp->ifa_count++;
480 ifa_af = sctp_ifap->address.sa.sa_family;
481 switch (ifa_af) {
482#ifdef INET
483 case AF_INET:
484 sctp_ifnp->num_v4++;
485 break;
486#endif
487#ifdef INET6
488 case AF_INET6:
489 sctp_ifnp->num_v6++;
490 break;
491#endif
492 default:
493 break;
494 }
495 if (sctp_ifnp->ifa_count == 1) {
496 /* register the new interface */
497 SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af);
498 sctp_ifnp->registered_af = ifa_af;
499 }
500}
501
t00fcxend0ad16b2013-02-09 18:34:24 +0000502
tuexendd729232011-11-01 23:04:43 +0000503/*-
504 * Remove an ifa from its ifn.
505 * If no more addresses exist, remove the ifn too. Otherwise, re-register
506 * the interface based on the remaining address families left.
507 * NOTE: ADDR write lock MUST be held.
508 */
509static void
510sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
511{
tuexendd729232011-11-01 23:04:43 +0000512 LIST_REMOVE(sctp_ifap, next_ifa);
513 if (sctp_ifap->ifn_p) {
514 /* update address counts */
515 sctp_ifap->ifn_p->ifa_count--;
516 switch (sctp_ifap->address.sa.sa_family) {
517#ifdef INET
518 case AF_INET:
519 sctp_ifap->ifn_p->num_v4--;
520 break;
521#endif
522#ifdef INET6
523 case AF_INET6:
524 sctp_ifap->ifn_p->num_v6--;
525 break;
526#endif
527 default:
528 break;
529 }
530
tuexendd729232011-11-01 23:04:43 +0000531 if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
532 /* remove the ifn, possibly freeing it */
533 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
534 } else {
535 /* re-register address family type, if needed */
536 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
537 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
tuexen63fc0bb2011-12-27 12:24:52 +0000538 SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6);
539 SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET);
tuexendd729232011-11-01 23:04:43 +0000540 sctp_ifap->ifn_p->registered_af = AF_INET;
541 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
542 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
tuexen63fc0bb2011-12-27 12:24:52 +0000543 SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET);
544 SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6);
tuexendd729232011-11-01 23:04:43 +0000545 sctp_ifap->ifn_p->registered_af = AF_INET6;
546 }
547 /* free the ifn refcount */
548 sctp_free_ifn(sctp_ifap->ifn_p);
549 }
550 sctp_ifap->ifn_p = NULL;
551 }
552}
553
t00fcxend0ad16b2013-02-09 18:34:24 +0000554
tuexendd729232011-11-01 23:04:43 +0000555struct sctp_ifa *
556sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
557 uint32_t ifn_type, const char *if_name, void *ifa,
558 struct sockaddr *addr, uint32_t ifa_flags,
559 int dynamic_add)
560{
561 struct sctp_vrf *vrf;
562 struct sctp_ifn *sctp_ifnp = NULL;
563 struct sctp_ifa *sctp_ifap = NULL;
564 struct sctp_ifalist *hash_addr_head;
565 struct sctp_ifnlist *hash_ifn_head;
566 uint32_t hash_of_addr;
567 int new_ifn_af = 0;
568
569#ifdef SCTP_DEBUG
570 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
571 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
572#endif
573 SCTP_IPI_ADDR_WLOCK();
574 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
575 if (sctp_ifnp) {
576 vrf = sctp_ifnp->vrf;
577 } else {
578 vrf = sctp_find_vrf(vrf_id);
579 if (vrf == NULL) {
580 vrf = sctp_allocate_vrf(vrf_id);
581 if (vrf == NULL) {
582 SCTP_IPI_ADDR_WUNLOCK();
583 return (NULL);
584 }
585 }
586 }
587 if (sctp_ifnp == NULL) {
588 /* build one and add it, can't hold lock
589 * until after malloc done though.
590 */
591 SCTP_IPI_ADDR_WUNLOCK();
592 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *,
593 sizeof(struct sctp_ifn), SCTP_M_IFN);
594 if (sctp_ifnp == NULL) {
595#ifdef INVARIANTS
596 panic("No memory for IFN");
597#endif
598 return (NULL);
599 }
600 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
601 sctp_ifnp->ifn_index = ifn_index;
602 sctp_ifnp->ifn_p = ifn;
603 sctp_ifnp->ifn_type = ifn_type;
604 sctp_ifnp->refcount = 0;
605 sctp_ifnp->vrf = vrf;
606 atomic_add_int(&vrf->refcount, 1);
607 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
608 if (if_name != NULL) {
Michael Tuexenedd369d2020-05-19 09:42:15 +0200609 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
tuexendd729232011-11-01 23:04:43 +0000610 } else {
Michael Tuexenedd369d2020-05-19 09:42:15 +0200611 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
tuexendd729232011-11-01 23:04:43 +0000612 }
613 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
614 LIST_INIT(&sctp_ifnp->ifalist);
615 SCTP_IPI_ADDR_WLOCK();
616 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
617 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
618 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
619 new_ifn_af = 1;
620 }
621 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
622 if (sctp_ifap) {
623 /* Hmm, it already exists? */
624 if ((sctp_ifap->ifn_p) &&
625 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
626 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
627 sctp_ifap->ifn_p->ifn_name, ifn_index,
t00fcxen8fca7ff2012-09-05 18:58:42 +0000628 (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000629 if (new_ifn_af) {
630 /* Remove the created one that we don't want */
631 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
632 }
633 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
634 /* easy to solve, just switch back to active */
635 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
636 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
637 sctp_ifap->ifn_p = sctp_ifnp;
638 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
639 }
640 exit_stage_left:
641 SCTP_IPI_ADDR_WUNLOCK();
642 return (sctp_ifap);
643 } else {
644 if (sctp_ifap->ifn_p) {
645 /*
646 * The last IFN gets the address, remove the
647 * old one
648 */
649 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +0000650 (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name,
tuexendd729232011-11-01 23:04:43 +0000651 sctp_ifap->ifn_p->ifn_index, if_name,
652 ifn_index);
653 /* remove the address from the old ifn */
654 sctp_remove_ifa_from_ifn(sctp_ifap);
655 /* move the address over to the new ifn */
656 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
t00fcxen8dd95b82014-07-11 20:41:28 +0000657 goto exit_stage_left;
tuexendd729232011-11-01 23:04:43 +0000658 } else {
659 /* repair ifnp which was NULL ? */
660 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
661 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +0000662 (void *)sctp_ifnp, (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000663 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
664 }
665 goto exit_stage_left;
666 }
667 }
668 SCTP_IPI_ADDR_WUNLOCK();
669 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
670 if (sctp_ifap == NULL) {
671#ifdef INVARIANTS
672 panic("No memory for IFA");
673#endif
674 return (NULL);
675 }
676 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
677 sctp_ifap->ifn_p = sctp_ifnp;
678 atomic_add_int(&sctp_ifnp->refcount, 1);
679 sctp_ifap->vrf_id = vrf_id;
680 sctp_ifap->ifa = ifa;
t00fcxena8a26f62012-09-12 19:24:54 +0000681#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +0000682 memcpy(&sctp_ifap->address, addr, addr->sa_len);
683#else
t00fcxend0ad16b2013-02-09 18:34:24 +0000684 switch (addr->sa_family) {
685#ifdef INET
686 case AF_INET:
tuexendd729232011-11-01 23:04:43 +0000687 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in));
t00fcxend0ad16b2013-02-09 18:34:24 +0000688 break;
689#endif
690#ifdef INET6
691 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +0000692 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6));
t00fcxend0ad16b2013-02-09 18:34:24 +0000693 break;
694#endif
695#if defined(__Userspace__)
696 case AF_CONN:
t00fcxen6df84312013-02-12 08:58:40 +0000697 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn));
t00fcxend0ad16b2013-02-09 18:34:24 +0000698 break;
699#endif
700 default:
701 /* TSNH */
702 break;
tuexendd729232011-11-01 23:04:43 +0000703 }
704#endif
705 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
706 sctp_ifap->flags = ifa_flags;
707 /* Set scope */
708 switch (sctp_ifap->address.sa.sa_family) {
709#ifdef INET
710 case AF_INET:
711 {
712 struct sockaddr_in *sin;
t00fcxend0ad16b2013-02-09 18:34:24 +0000713
t00fcxen05f07c02014-09-07 10:15:26 +0000714 sin = &sctp_ifap->address.sin;
tuexendd729232011-11-01 23:04:43 +0000715 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
716 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
717 sctp_ifap->src_is_loop = 1;
718 }
719 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
720 sctp_ifap->src_is_priv = 1;
721 }
722 sctp_ifnp->num_v4++;
723 if (new_ifn_af)
724 new_ifn_af = AF_INET;
725 break;
726 }
727#endif
728#ifdef INET6
729 case AF_INET6:
730 {
731 /* ok to use deprecated addresses? */
732 struct sockaddr_in6 *sin6;
t00fcxend0ad16b2013-02-09 18:34:24 +0000733
t00fcxen05f07c02014-09-07 10:15:26 +0000734 sin6 = &sctp_ifap->address.sin6;
tuexendd729232011-11-01 23:04:43 +0000735 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
736 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
737 sctp_ifap->src_is_loop = 1;
738 }
739 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
740 sctp_ifap->src_is_priv = 1;
741 }
742 sctp_ifnp->num_v6++;
743 if (new_ifn_af)
744 new_ifn_af = AF_INET6;
745 break;
746 }
747#endif
t00fcxend0ad16b2013-02-09 18:34:24 +0000748#if defined(__Userspace__)
749 case AF_CONN:
750 if (new_ifn_af)
751 new_ifn_af = AF_CONN;
752 break;
753#endif
tuexendd729232011-11-01 23:04:43 +0000754 default:
755 new_ifn_af = 0;
756 break;
757 }
758 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
759
760 if ((sctp_ifap->src_is_priv == 0) &&
761 (sctp_ifap->src_is_loop == 0)) {
762 sctp_ifap->src_is_glob = 1;
763 }
764 SCTP_IPI_ADDR_WLOCK();
765 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
766 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
767 sctp_ifap->refcount = 1;
768 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
769 sctp_ifnp->ifa_count++;
770 vrf->total_ifa_count++;
771 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
772 if (new_ifn_af) {
773 SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
774 sctp_ifnp->registered_af = new_ifn_af;
775 }
776 SCTP_IPI_ADDR_WUNLOCK();
777 if (dynamic_add) {
778 /* Bump up the refcount so that when the timer
779 * completes it will drop back down.
780 */
781 struct sctp_laddr *wi;
782
783 atomic_add_int(&sctp_ifap->refcount, 1);
784 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
785 if (wi == NULL) {
786 /*
787 * Gak, what can we do? We have lost an address
788 * change can you say HOSED?
789 */
790 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
791 /* Opps, must decrement the count */
792 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
793 if_name);
794 return (NULL);
795 }
796 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +0200797 memset(wi, 0, sizeof(*wi));
tuexendd729232011-11-01 23:04:43 +0000798 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
799 wi->ifa = sctp_ifap;
800 wi->action = SCTP_ADD_IP_ADDRESS;
tuexen09631162012-04-18 11:23:02 +0000801
tuexendd729232011-11-01 23:04:43 +0000802 SCTP_WQ_ADDR_LOCK();
803 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
tuexendd729232011-11-01 23:04:43 +0000804 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
805 (struct sctp_inpcb *)NULL,
806 (struct sctp_tcb *)NULL,
807 (struct sctp_nets *)NULL);
Michael Tuexen96939f12017-09-20 23:31:24 +0200808 SCTP_WQ_ADDR_UNLOCK();
tuexendd729232011-11-01 23:04:43 +0000809 } else {
810 /* it's ready for use */
811 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
812 }
813 return (sctp_ifap);
814}
815
816void
817sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
818 uint32_t ifn_index, const char *if_name)
819{
820 struct sctp_vrf *vrf;
821 struct sctp_ifa *sctp_ifap = NULL;
822
823 SCTP_IPI_ADDR_WLOCK();
824 vrf = sctp_find_vrf(vrf_id);
825 if (vrf == NULL) {
826 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
827 goto out_now;
828 }
829
830#ifdef SCTP_DEBUG
831 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
832 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
833#endif
834 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
835 if (sctp_ifap) {
836 /* Validate the delete */
837 if (sctp_ifap->ifn_p) {
tuexenb740ef62011-11-13 11:58:06 +0000838 int valid = 0;
tuexendd729232011-11-01 23:04:43 +0000839 /*-
840 * The name has priority over the ifn_index
Michael Tuexen3b4263d2020-06-06 19:52:50 +0200841 * if its given.
tuexendd729232011-11-01 23:04:43 +0000842 */
843 if (if_name) {
tuexenb740ef62011-11-13 11:58:06 +0000844 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
845 /* They match its a correct delete */
846 valid = 1;
tuexendd729232011-11-01 23:04:43 +0000847 }
848 }
849 if (!valid) {
850 /* last ditch check ifn_index */
851 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
852 valid = 1;
853 }
854 }
855 if (!valid) {
856 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
857 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
858 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
859 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
860 SCTP_IPI_ADDR_WUNLOCK();
861 return;
862 }
863 }
t00fcxen8fca7ff2012-09-05 18:58:42 +0000864 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000865 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
t00fcxen97887d62014-03-29 21:31:12 +0000866 /*
867 * We don't set the flag. This means that the structure will
868 * hang around in EP's that have bound specific to it until
869 * they close. This gives us TCP like behavior if someone
870 * removes an address (or for that matter adds it right back).
871 */
872 /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */
tuexendd729232011-11-01 23:04:43 +0000873 vrf->total_ifa_count--;
874 LIST_REMOVE(sctp_ifap, next_bucket);
875 sctp_remove_ifa_from_ifn(sctp_ifap);
876 }
877#ifdef SCTP_DEBUG
878 else {
879 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
880 ifn_index);
881 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
882 }
883#endif
884
885 out_now:
886 SCTP_IPI_ADDR_WUNLOCK();
887 if (sctp_ifap) {
888 struct sctp_laddr *wi;
889
890 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
891 if (wi == NULL) {
892 /*
893 * Gak, what can we do? We have lost an address
894 * change can you say HOSED?
895 */
896 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
897
898 /* Oops, must decrement the count */
899 sctp_free_ifa(sctp_ifap);
900 return;
901 }
902 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +0200903 memset(wi, 0, sizeof(*wi));
tuexendd729232011-11-01 23:04:43 +0000904 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
905 wi->ifa = sctp_ifap;
906 wi->action = SCTP_DEL_IP_ADDRESS;
907 SCTP_WQ_ADDR_LOCK();
908 /*
909 * Should this really be a tailq? As it is we will process the
910 * newest first :-0
911 */
912 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
tuexendd729232011-11-01 23:04:43 +0000913 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
914 (struct sctp_inpcb *)NULL,
915 (struct sctp_tcb *)NULL,
916 (struct sctp_nets *)NULL);
Michael Tuexen96939f12017-09-20 23:31:24 +0200917 SCTP_WQ_ADDR_UNLOCK();
tuexendd729232011-11-01 23:04:43 +0000918 }
919 return;
920}
921
922
t00fcxen60d0dea2013-02-10 20:04:03 +0000923static int
924sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
925{
t00fcxenaff14b62013-11-02 18:02:02 +0000926 int loopback_scope;
927#if defined(INET)
928 int ipv4_local_scope, ipv4_addr_legal;
929#endif
930#if defined(INET6)
931 int local_scope, site_scope, ipv6_addr_legal;
932#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000933#if defined(__Userspace__)
934 int conn_addr_legal;
935#endif
936 struct sctp_vrf *vrf;
937 struct sctp_ifn *sctp_ifn;
938 struct sctp_ifa *sctp_ifa;
939
940 loopback_scope = stcb->asoc.scope.loopback_scope;
t00fcxenaff14b62013-11-02 18:02:02 +0000941#if defined(INET)
t00fcxen60d0dea2013-02-10 20:04:03 +0000942 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
t00fcxenaff14b62013-11-02 18:02:02 +0000943 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
944#endif
945#if defined(INET6)
t00fcxen60d0dea2013-02-10 20:04:03 +0000946 local_scope = stcb->asoc.scope.local_scope;
947 site_scope = stcb->asoc.scope.site_scope;
t00fcxen60d0dea2013-02-10 20:04:03 +0000948 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
t00fcxenaff14b62013-11-02 18:02:02 +0000949#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000950#if defined(__Userspace__)
951 conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
952#endif
953
954 SCTP_IPI_ADDR_RLOCK();
955 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
956 if (vrf == NULL) {
957 /* no vrf, no addresses */
958 SCTP_IPI_ADDR_RUNLOCK();
959 return (0);
960 }
961
962 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
963 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
964 if ((loopback_scope == 0) &&
965 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
966 continue;
967 }
968 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
969 if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
970 (!sctp_is_addr_pending(stcb, sctp_ifa))) {
971 /* We allow pending addresses, where we
972 * have sent an asconf-add to be considered
973 * valid.
974 */
975 continue;
976 }
t00fcxenba8aa202013-11-16 15:37:17 +0000977 if (sctp_ifa->address.sa.sa_family != to->sa_family) {
978 continue;
979 }
t00fcxen60d0dea2013-02-10 20:04:03 +0000980 switch (sctp_ifa->address.sa.sa_family) {
981#ifdef INET
982 case AF_INET:
983 if (ipv4_addr_legal) {
984 struct sockaddr_in *sin, *rsin;
985
986 sin = &sctp_ifa->address.sin;
987 rsin = (struct sockaddr_in *)to;
988 if ((ipv4_local_scope == 0) &&
989 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
990 continue;
991 }
t00fcxen326aa4b2014-06-20 13:42:13 +0000992#if defined(__FreeBSD__)
993 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
994 &sin->sin_addr) != 0) {
995 continue;
996 }
997#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000998 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
999 SCTP_IPI_ADDR_RUNLOCK();
1000 return (1);
1001 }
1002 }
1003 break;
1004#endif
1005#ifdef INET6
1006 case AF_INET6:
1007 if (ipv6_addr_legal) {
1008 struct sockaddr_in6 *sin6, *rsin6;
1009#if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
1010 struct sockaddr_in6 lsa6;
1011#endif
1012 sin6 = &sctp_ifa->address.sin6;
1013 rsin6 = (struct sockaddr_in6 *)to;
t00fcxen326aa4b2014-06-20 13:42:13 +00001014#if defined(__FreeBSD__)
1015 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
1016 &sin6->sin6_addr) != 0) {
1017 continue;
1018 }
1019#endif
t00fcxen60d0dea2013-02-10 20:04:03 +00001020 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1021 if (local_scope == 0)
1022 continue;
1023#if defined(SCTP_EMBEDDED_V6_SCOPE)
1024 if (sin6->sin6_scope_id == 0) {
1025#ifdef SCTP_KAME
1026 if (sa6_recoverscope(sin6) != 0)
1027 continue;
1028#else
1029 lsa6 = *sin6;
1030 if (in6_recoverscope(&lsa6,
1031 &lsa6.sin6_addr,
1032 NULL))
1033 continue;
1034 sin6 = &lsa6;
1035#endif /* SCTP_KAME */
1036 }
1037#endif /* SCTP_EMBEDDED_V6_SCOPE */
1038 }
1039 if ((site_scope == 0) &&
1040 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1041 continue;
1042 }
1043 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1044 SCTP_IPI_ADDR_RUNLOCK();
1045 return (1);
1046 }
1047 }
1048 break;
1049#endif
1050#if defined(__Userspace__)
1051 case AF_CONN:
1052 if (conn_addr_legal) {
1053 struct sockaddr_conn *sconn, *rsconn;
1054
1055 sconn = &sctp_ifa->address.sconn;
1056 rsconn = (struct sockaddr_conn *)to;
1057 if (sconn->sconn_addr == rsconn->sconn_addr) {
1058 SCTP_IPI_ADDR_RUNLOCK();
1059 return (1);
1060 }
1061 }
1062 break;
1063#endif
1064 default:
1065 /* TSNH */
1066 break;
1067 }
1068 }
1069 }
1070 } else {
1071 struct sctp_laddr *laddr;
1072
1073 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1074 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1075 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1076 continue;
1077 }
1078 if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
1079 (!sctp_is_addr_pending(stcb, laddr->ifa))) {
1080 /* We allow pending addresses, where we
1081 * have sent an asconf-add to be considered
1082 * valid.
1083 */
1084 continue;
1085 }
1086 if (laddr->ifa->address.sa.sa_family != to->sa_family) {
1087 continue;
1088 }
1089 switch (to->sa_family) {
1090#ifdef INET
1091 case AF_INET:
1092 {
1093 struct sockaddr_in *sin, *rsin;
1094
t00fcxen05f07c02014-09-07 10:15:26 +00001095 sin = &laddr->ifa->address.sin;
t00fcxen60d0dea2013-02-10 20:04:03 +00001096 rsin = (struct sockaddr_in *)to;
1097 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1098 SCTP_IPI_ADDR_RUNLOCK();
1099 return (1);
1100 }
1101 break;
1102 }
1103#endif
1104#ifdef INET6
1105 case AF_INET6:
1106 {
1107 struct sockaddr_in6 *sin6, *rsin6;
1108
t00fcxen05f07c02014-09-07 10:15:26 +00001109 sin6 = &laddr->ifa->address.sin6;
t00fcxen60d0dea2013-02-10 20:04:03 +00001110 rsin6 = (struct sockaddr_in6 *)to;
1111 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1112 SCTP_IPI_ADDR_RUNLOCK();
1113 return (1);
1114 }
1115 break;
1116 }
1117
1118#endif
1119#if defined(__Userspace__)
1120 case AF_CONN:
1121 {
1122 struct sockaddr_conn *sconn, *rsconn;
1123
t00fcxen05f07c02014-09-07 10:15:26 +00001124 sconn = &laddr->ifa->address.sconn;
t00fcxen60d0dea2013-02-10 20:04:03 +00001125 rsconn = (struct sockaddr_conn *)to;
1126 if (sconn->sconn_addr == rsconn->sconn_addr) {
1127 SCTP_IPI_ADDR_RUNLOCK();
1128 return (1);
1129 }
1130 break;
1131 }
1132#endif
1133 default:
1134 /* TSNH */
1135 break;
1136 }
1137
1138 }
1139 }
1140 SCTP_IPI_ADDR_RUNLOCK();
1141 return (0);
1142}
1143
1144
tuexendd729232011-11-01 23:04:43 +00001145static struct sctp_tcb *
1146sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
1147 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
1148{
1149 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
1150 /*
1151 * If we support the TCP model, then we must now dig through to see
1152 * if we can find our endpoint in the list of tcp ep's.
1153 */
1154 uint16_t lport, rport;
1155 struct sctppcbhead *ephead;
1156 struct sctp_inpcb *inp;
1157 struct sctp_laddr *laddr;
1158 struct sctp_tcb *stcb;
1159 struct sctp_nets *net;
1160#ifdef SCTP_MVRF
1161 int fnd, i;
1162#endif
t00fcxend0ad16b2013-02-09 18:34:24 +00001163
tuexendd729232011-11-01 23:04:43 +00001164 if ((to == NULL) || (from == NULL)) {
1165 return (NULL);
1166 }
1167
1168 switch (to->sa_family) {
1169#ifdef INET
1170 case AF_INET:
1171 if (from->sa_family == AF_INET) {
1172 lport = ((struct sockaddr_in *)to)->sin_port;
1173 rport = ((struct sockaddr_in *)from)->sin_port;
1174 } else {
1175 return (NULL);
1176 }
1177 break;
1178#endif
1179#ifdef INET6
1180 case AF_INET6:
1181 if (from->sa_family == AF_INET6) {
1182 lport = ((struct sockaddr_in6 *)to)->sin6_port;
1183 rport = ((struct sockaddr_in6 *)from)->sin6_port;
1184 } else {
1185 return (NULL);
1186 }
1187 break;
1188#endif
tuexen5d9b9742012-07-11 21:52:01 +00001189#if defined(__Userspace__)
1190 case AF_CONN:
1191 if (from->sa_family == AF_CONN) {
1192 lport = ((struct sockaddr_conn *)to)->sconn_port;
1193 rport = ((struct sockaddr_conn *)from)->sconn_port;
1194 } else {
1195 return (NULL);
1196 }
1197 break;
1198#endif
tuexendd729232011-11-01 23:04:43 +00001199 default:
1200 return (NULL);
1201 }
1202 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
1203 /*
1204 * Ok now for each of the guys in this bucket we must look and see:
1205 * - Does the remote port match. - Does there single association's
1206 * addresses match this address (to). If so we update p_ep to point
1207 * to this ep and return the tcb from it.
1208 */
1209 LIST_FOREACH(inp, ephead, sctp_hash) {
1210 SCTP_INP_RLOCK(inp);
1211 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1212 SCTP_INP_RUNLOCK(inp);
1213 continue;
1214 }
1215 if (lport != inp->sctp_lport) {
1216 SCTP_INP_RUNLOCK(inp);
1217 continue;
1218 }
t00fcxen326aa4b2014-06-20 13:42:13 +00001219#if defined(__FreeBSD__)
1220 switch (to->sa_family) {
1221#ifdef INET
1222 case AF_INET:
1223 {
1224 struct sockaddr_in *sin;
1225
1226 sin = (struct sockaddr_in *)to;
1227 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1228 &sin->sin_addr) != 0) {
1229 SCTP_INP_RUNLOCK(inp);
1230 continue;
1231 }
1232 break;
1233 }
1234#endif
1235#ifdef INET6
1236 case AF_INET6:
1237 {
1238 struct sockaddr_in6 *sin6;
1239
1240 sin6 = (struct sockaddr_in6 *)to;
1241 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1242 &sin6->sin6_addr) != 0) {
1243 SCTP_INP_RUNLOCK(inp);
1244 continue;
1245 }
1246 break;
1247 }
1248#endif
1249 default:
1250 SCTP_INP_RUNLOCK(inp);
1251 continue;
1252 }
1253#endif
tuexendd729232011-11-01 23:04:43 +00001254#ifdef SCTP_MVRF
1255 fnd = 0;
tuexen9784e9a2011-12-18 13:04:23 +00001256 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00001257 if (inp->m_vrf_ids[i] == vrf_id) {
1258 fnd = 1;
1259 break;
1260 }
1261 }
1262 if (fnd == 0) {
1263 SCTP_INP_RUNLOCK(inp);
1264 continue;
1265 }
1266#else
1267 if (inp->def_vrf_id != vrf_id) {
1268 SCTP_INP_RUNLOCK(inp);
1269 continue;
1270 }
1271#endif
1272 /* check to see if the ep has one of the addresses */
1273 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1274 /* We are NOT bound all, so look further */
1275 int match = 0;
1276
1277 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1278
1279 if (laddr->ifa == NULL) {
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02001280 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__);
tuexendd729232011-11-01 23:04:43 +00001281 continue;
1282 }
1283 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1284 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1285 continue;
1286 }
1287 if (laddr->ifa->address.sa.sa_family ==
1288 to->sa_family) {
1289 /* see if it matches */
tuexendd729232011-11-01 23:04:43 +00001290#ifdef INET
1291 if (from->sa_family == AF_INET) {
1292 struct sockaddr_in *intf_addr, *sin;
1293
1294 intf_addr = &laddr->ifa->address.sin;
1295 sin = (struct sockaddr_in *)to;
1296 if (sin->sin_addr.s_addr ==
1297 intf_addr->sin_addr.s_addr) {
1298 match = 1;
1299 break;
1300 }
1301 }
1302#endif
1303#ifdef INET6
1304 if (from->sa_family == AF_INET6) {
1305 struct sockaddr_in6 *intf_addr6;
1306 struct sockaddr_in6 *sin6;
1307
1308 sin6 = (struct sockaddr_in6 *)
1309 to;
1310 intf_addr6 = &laddr->ifa->address.sin6;
1311
1312 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1313 intf_addr6)) {
1314 match = 1;
1315 break;
1316 }
1317 }
1318#endif
tuexen5d9b9742012-07-11 21:52:01 +00001319#if defined(__Userspace__)
1320 if (from->sa_family == AF_CONN) {
1321 struct sockaddr_conn *intf_addr, *sconn;
1322
1323 intf_addr = &laddr->ifa->address.sconn;
1324 sconn = (struct sockaddr_conn *)to;
1325 if (sconn->sconn_addr ==
1326 intf_addr->sconn_addr) {
1327 match = 1;
1328 break;
1329 }
1330 }
1331#endif
tuexendd729232011-11-01 23:04:43 +00001332 }
1333 }
1334 if (match == 0) {
1335 /* This endpoint does not have this address */
1336 SCTP_INP_RUNLOCK(inp);
1337 continue;
1338 }
1339 }
1340 /*
1341 * Ok if we hit here the ep has the address, does it hold
1342 * the tcb?
1343 */
t00fcxen60d0dea2013-02-10 20:04:03 +00001344 /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
tuexendd729232011-11-01 23:04:43 +00001345 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1346 if (stcb == NULL) {
1347 SCTP_INP_RUNLOCK(inp);
1348 continue;
1349 }
1350 SCTP_TCB_LOCK(stcb);
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 if (stcb->rport != rport) {
1357 /* remote port does not match. */
tuexen09631162012-04-18 11:23:02 +00001358 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001359 SCTP_INP_RUNLOCK(inp);
1360 continue;
1361 }
1362 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001363 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001364 SCTP_INP_RUNLOCK(inp);
1365 continue;
1366 }
t00fcxen60d0dea2013-02-10 20:04:03 +00001367 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1368 SCTP_TCB_UNLOCK(stcb);
1369 SCTP_INP_RUNLOCK(inp);
1370 continue;
1371 }
tuexendd729232011-11-01 23:04:43 +00001372 /* Does this TCB have a matching address? */
1373 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1374
1375 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1376 /* not the same family, can't be a match */
1377 continue;
1378 }
1379 switch (from->sa_family) {
1380#ifdef INET
1381 case AF_INET:
1382 {
1383 struct sockaddr_in *sin, *rsin;
1384
1385 sin = (struct sockaddr_in *)&net->ro._l_addr;
1386 rsin = (struct sockaddr_in *)from;
1387 if (sin->sin_addr.s_addr ==
1388 rsin->sin_addr.s_addr) {
1389 /* found it */
1390 if (netp != NULL) {
1391 *netp = net;
1392 }
1393 /* Update the endpoint pointer */
1394 *inp_p = inp;
1395 SCTP_INP_RUNLOCK(inp);
1396 return (stcb);
1397 }
1398 break;
1399 }
1400#endif
1401#ifdef INET6
1402 case AF_INET6:
1403 {
1404 struct sockaddr_in6 *sin6, *rsin6;
1405
1406 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1407 rsin6 = (struct sockaddr_in6 *)from;
1408 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1409 rsin6)) {
1410 /* found it */
1411 if (netp != NULL) {
1412 *netp = net;
1413 }
1414 /* Update the endpoint pointer */
1415 *inp_p = inp;
1416 SCTP_INP_RUNLOCK(inp);
1417 return (stcb);
1418 }
1419 break;
1420 }
1421#endif
tuexen5d9b9742012-07-11 21:52:01 +00001422#if defined(__Userspace__)
1423 case AF_CONN:
1424 {
1425 struct sockaddr_conn *sconn, *rsconn;
1426
1427 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1428 rsconn = (struct sockaddr_conn *)from;
1429 if (sconn->sconn_addr == rsconn->sconn_addr) {
1430 /* found it */
1431 if (netp != NULL) {
1432 *netp = net;
1433 }
1434 /* Update the endpoint pointer */
1435 *inp_p = inp;
1436 SCTP_INP_RUNLOCK(inp);
1437 return (stcb);
1438 }
1439 break;
1440 }
1441#endif
tuexendd729232011-11-01 23:04:43 +00001442 default:
1443 /* TSNH */
1444 break;
1445 }
1446 }
1447 SCTP_TCB_UNLOCK(stcb);
1448 SCTP_INP_RUNLOCK(inp);
1449 }
1450 return (NULL);
1451}
1452
t00fcxend0ad16b2013-02-09 18:34:24 +00001453
tuexendd729232011-11-01 23:04:43 +00001454/*
1455 * rules for use
1456 *
1457 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1458 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1459 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1460 * NULL.
1461 */
1462
1463struct sctp_tcb *
1464sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1465 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1466{
1467 struct sctpasochead *head;
1468 struct sctp_inpcb *inp;
tuexen63fc0bb2011-12-27 12:24:52 +00001469 struct sctp_tcb *stcb = NULL;
tuexendd729232011-11-01 23:04:43 +00001470 struct sctp_nets *net;
1471 uint16_t rport;
1472
1473 inp = *inp_p;
tuexen5d9b9742012-07-11 21:52:01 +00001474 switch (remote->sa_family) {
1475#ifdef INET
1476 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00001477 rport = (((struct sockaddr_in *)remote)->sin_port);
tuexen5d9b9742012-07-11 21:52:01 +00001478 break;
1479#endif
1480#ifdef INET6
1481 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00001482 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
tuexen5d9b9742012-07-11 21:52:01 +00001483 break;
1484#endif
1485#if defined(__Userspace__)
1486 case AF_CONN:
Taylor Brandstetter74670512018-03-27 10:56:58 -07001487 rport = (((struct sockaddr_conn *)remote)->sconn_port);
tuexen5d9b9742012-07-11 21:52:01 +00001488 break;
1489#endif
1490 default:
tuexendd729232011-11-01 23:04:43 +00001491 return (NULL);
1492 }
1493 if (locked_tcb) {
1494 /*
1495 * UN-lock so we can do proper locking here this occurs when
1496 * called from load_addresses_from_init.
1497 */
1498 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1499 SCTP_TCB_UNLOCK(locked_tcb);
1500 }
1501 SCTP_INP_INFO_RLOCK();
t00fcxenf2d781c2012-11-26 16:47:12 +00001502 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1503 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
tuexendd729232011-11-01 23:04:43 +00001504 /*-
1505 * Now either this guy is our listener or it's the
1506 * connector. If it is the one that issued the connect, then
1507 * it's only chance is to be the first TCB in the list. If
1508 * it is the acceptor, then do the special_lookup to hash
1509 * and find the real inp.
1510 */
Michael Tuexend900e5f2017-06-23 10:52:43 +02001511 if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) {
tuexendd729232011-11-01 23:04:43 +00001512 /* to is peer addr, from is my addr */
1513#ifndef SCTP_MVRF
1514 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1515 netp, inp->def_vrf_id);
1516 if ((stcb != NULL) && (locked_tcb == NULL)) {
1517 /* we have a locked tcb, lower refcount */
1518 SCTP_INP_DECR_REF(inp);
1519 }
1520 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1521 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1522 SCTP_TCB_LOCK(locked_tcb);
1523 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1524 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1525 }
1526#else
1527 /*-
1528 * MVRF is tricky, we must look in every VRF
1529 * the endpoint has.
1530 */
t00fcxen60d0dea2013-02-10 20:04:03 +00001531 int i;
tuexendd729232011-11-01 23:04:43 +00001532
t00fcxen60d0dea2013-02-10 20:04:03 +00001533 for (i = 0; i < inp->num_vrfs; i++) {
1534 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1535 netp, inp->m_vrf_ids[i]);
1536 if ((stcb != NULL) && (locked_tcb == NULL)) {
1537 /* we have a locked tcb, lower refcount */
1538 SCTP_INP_DECR_REF(inp);
1539 break;
1540 }
1541 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1542 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1543 SCTP_TCB_LOCK(locked_tcb);
1544 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1545 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1546 break;
tuexendd729232011-11-01 23:04:43 +00001547 }
1548 }
1549#endif
1550 SCTP_INP_INFO_RUNLOCK();
1551 return (stcb);
1552 } else {
1553 SCTP_INP_WLOCK(inp);
1554 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1555 goto null_return;
1556 }
1557 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1558 if (stcb == NULL) {
1559 goto null_return;
1560 }
1561 SCTP_TCB_LOCK(stcb);
1562
1563 if (stcb->rport != rport) {
1564 /* remote port does not match. */
1565 SCTP_TCB_UNLOCK(stcb);
1566 goto null_return;
1567 }
1568 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001569 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001570 goto null_return;
1571 }
1572 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1573 SCTP_TCB_UNLOCK(stcb);
1574 goto null_return;
1575 }
1576 /* now look at the list of remote addresses */
1577 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1578#ifdef INVARIANTS
1579 if (net == (TAILQ_NEXT(net, sctp_next))) {
1580 panic("Corrupt net list");
1581 }
1582#endif
1583 if (net->ro._l_addr.sa.sa_family !=
1584 remote->sa_family) {
1585 /* not the same family */
1586 continue;
1587 }
1588 switch (remote->sa_family) {
1589#ifdef INET
1590 case AF_INET:
1591 {
1592 struct sockaddr_in *sin, *rsin;
1593
1594 sin = (struct sockaddr_in *)
1595 &net->ro._l_addr;
1596 rsin = (struct sockaddr_in *)remote;
1597 if (sin->sin_addr.s_addr ==
1598 rsin->sin_addr.s_addr) {
1599 /* found it */
1600 if (netp != NULL) {
1601 *netp = net;
1602 }
1603 if (locked_tcb == NULL) {
1604 SCTP_INP_DECR_REF(inp);
1605 } else if (locked_tcb != stcb) {
1606 SCTP_TCB_LOCK(locked_tcb);
1607 }
1608 if (locked_tcb) {
1609 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1610 }
1611
1612 SCTP_INP_WUNLOCK(inp);
1613 SCTP_INP_INFO_RUNLOCK();
1614 return (stcb);
1615 }
1616 break;
1617 }
1618#endif
1619#ifdef INET6
1620 case AF_INET6:
1621 {
1622 struct sockaddr_in6 *sin6, *rsin6;
1623
1624 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1625 rsin6 = (struct sockaddr_in6 *)remote;
1626 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1627 rsin6)) {
1628 /* found it */
1629 if (netp != NULL) {
1630 *netp = net;
1631 }
1632 if (locked_tcb == NULL) {
1633 SCTP_INP_DECR_REF(inp);
1634 } else if (locked_tcb != stcb) {
1635 SCTP_TCB_LOCK(locked_tcb);
1636 }
1637 if (locked_tcb) {
1638 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1639 }
1640 SCTP_INP_WUNLOCK(inp);
1641 SCTP_INP_INFO_RUNLOCK();
1642 return (stcb);
1643 }
1644 break;
1645 }
1646#endif
tuexen5d9b9742012-07-11 21:52:01 +00001647#if defined(__Userspace__)
1648 case AF_CONN:
1649 {
1650 struct sockaddr_conn *sconn, *rsconn;
1651
1652 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1653 rsconn = (struct sockaddr_conn *)remote;
1654 if (sconn->sconn_addr == rsconn->sconn_addr) {
1655 /* found it */
1656 if (netp != NULL) {
1657 *netp = net;
1658 }
1659 if (locked_tcb == NULL) {
1660 SCTP_INP_DECR_REF(inp);
1661 } else if (locked_tcb != stcb) {
1662 SCTP_TCB_LOCK(locked_tcb);
1663 }
1664 if (locked_tcb) {
1665 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1666 }
1667 SCTP_INP_WUNLOCK(inp);
1668 SCTP_INP_INFO_RUNLOCK();
1669 return (stcb);
1670 }
1671 break;
1672 }
1673#endif
tuexendd729232011-11-01 23:04:43 +00001674 default:
1675 /* TSNH */
1676 break;
1677 }
1678 }
1679 SCTP_TCB_UNLOCK(stcb);
1680 }
1681 } else {
1682 SCTP_INP_WLOCK(inp);
1683 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1684 goto null_return;
1685 }
1686 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
t00fcxen1369cd42015-01-11 21:57:30 +00001687 inp->sctp_hashmark)];
tuexendd729232011-11-01 23:04:43 +00001688 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1689 if (stcb->rport != rport) {
1690 /* remote port does not match */
1691 continue;
1692 }
1693 SCTP_TCB_LOCK(stcb);
1694 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1695 SCTP_TCB_UNLOCK(stcb);
1696 continue;
1697 }
1698 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1699 SCTP_TCB_UNLOCK(stcb);
1700 continue;
1701 }
1702 /* now look at the list of remote addresses */
1703 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1704#ifdef INVARIANTS
1705 if (net == (TAILQ_NEXT(net, sctp_next))) {
1706 panic("Corrupt net list");
1707 }
1708#endif
1709 if (net->ro._l_addr.sa.sa_family !=
1710 remote->sa_family) {
1711 /* not the same family */
1712 continue;
1713 }
1714 switch (remote->sa_family) {
1715#ifdef INET
1716 case AF_INET:
1717 {
1718 struct sockaddr_in *sin, *rsin;
1719
1720 sin = (struct sockaddr_in *)
1721 &net->ro._l_addr;
1722 rsin = (struct sockaddr_in *)remote;
1723 if (sin->sin_addr.s_addr ==
1724 rsin->sin_addr.s_addr) {
1725 /* found it */
1726 if (netp != NULL) {
1727 *netp = net;
1728 }
1729 if (locked_tcb == NULL) {
1730 SCTP_INP_DECR_REF(inp);
1731 } else if (locked_tcb != stcb) {
1732 SCTP_TCB_LOCK(locked_tcb);
1733 }
1734 if (locked_tcb) {
1735 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1736 }
1737 SCTP_INP_WUNLOCK(inp);
1738 SCTP_INP_INFO_RUNLOCK();
1739 return (stcb);
1740 }
1741 break;
1742 }
1743#endif
1744#ifdef INET6
1745 case AF_INET6:
1746 {
1747 struct sockaddr_in6 *sin6, *rsin6;
1748
1749 sin6 = (struct sockaddr_in6 *)
1750 &net->ro._l_addr;
1751 rsin6 = (struct sockaddr_in6 *)remote;
1752 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1753 rsin6)) {
1754 /* found it */
1755 if (netp != NULL) {
1756 *netp = net;
1757 }
1758 if (locked_tcb == NULL) {
1759 SCTP_INP_DECR_REF(inp);
1760 } else if (locked_tcb != stcb) {
1761 SCTP_TCB_LOCK(locked_tcb);
1762 }
1763 if (locked_tcb) {
1764 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1765 }
1766 SCTP_INP_WUNLOCK(inp);
1767 SCTP_INP_INFO_RUNLOCK();
1768 return (stcb);
1769 }
1770 break;
1771 }
1772#endif
tuexen5d9b9742012-07-11 21:52:01 +00001773#if defined(__Userspace__)
1774 case AF_CONN:
1775 {
1776 struct sockaddr_conn *sconn, *rsconn;
1777
1778 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1779 rsconn = (struct sockaddr_conn *)remote;
1780 if (sconn->sconn_addr == rsconn->sconn_addr) {
1781 /* found it */
1782 if (netp != NULL) {
1783 *netp = net;
1784 }
1785 if (locked_tcb == NULL) {
1786 SCTP_INP_DECR_REF(inp);
1787 } else if (locked_tcb != stcb) {
1788 SCTP_TCB_LOCK(locked_tcb);
1789 }
1790 if (locked_tcb) {
1791 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1792 }
1793 SCTP_INP_WUNLOCK(inp);
1794 SCTP_INP_INFO_RUNLOCK();
1795 return (stcb);
1796 }
1797 break;
1798 }
1799#endif
tuexendd729232011-11-01 23:04:43 +00001800 default:
1801 /* TSNH */
1802 break;
1803 }
1804 }
1805 SCTP_TCB_UNLOCK(stcb);
1806 }
1807 }
1808null_return:
1809 /* clean up for returning null */
1810 if (locked_tcb) {
1811 SCTP_TCB_LOCK(locked_tcb);
1812 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1813 }
1814 SCTP_INP_WUNLOCK(inp);
1815 SCTP_INP_INFO_RUNLOCK();
1816 /* not found */
1817 return (NULL);
1818}
1819
t00fcxend0ad16b2013-02-09 18:34:24 +00001820
tuexendd729232011-11-01 23:04:43 +00001821/*
1822 * Find an association for a specific endpoint using the association id given
1823 * out in the COMM_UP notification
1824 */
tuexendd729232011-11-01 23:04:43 +00001825struct sctp_tcb *
1826sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1827{
1828 /*
1829 * Use my the assoc_id to find a endpoint
1830 */
1831 struct sctpasochead *head;
1832 struct sctp_tcb *stcb;
1833 uint32_t id;
t00fcxend0ad16b2013-02-09 18:34:24 +00001834
tuexendd729232011-11-01 23:04:43 +00001835 if (inp == NULL) {
1836 SCTP_PRINTF("TSNH ep_associd\n");
1837 return (NULL);
1838 }
1839 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1840 SCTP_PRINTF("TSNH ep_associd0\n");
1841 return (NULL);
1842 }
1843 id = (uint32_t)asoc_id;
1844 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1845 if (head == NULL) {
1846 /* invalid id TSNH */
1847 SCTP_PRINTF("TSNH ep_associd1\n");
1848 return (NULL);
1849 }
1850 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1851 if (stcb->asoc.assoc_id == id) {
1852 if (inp != stcb->sctp_ep) {
1853 /*
1854 * some other guy has the same id active (id
1855 * collision ??).
1856 */
1857 SCTP_PRINTF("TSNH ep_associd2\n");
1858 continue;
1859 }
1860 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1861 continue;
1862 }
1863 if (want_lock) {
1864 SCTP_TCB_LOCK(stcb);
1865 }
1866 return (stcb);
1867 }
1868 }
1869 return (NULL);
1870}
1871
1872
1873struct sctp_tcb *
1874sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1875{
t00fcxend0ad16b2013-02-09 18:34:24 +00001876 struct sctp_tcb *stcb;
1877
1878 SCTP_INP_RLOCK(inp);
1879 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1880 SCTP_INP_RUNLOCK(inp);
1881 return (stcb);
tuexendd729232011-11-01 23:04:43 +00001882}
1883
1884
t00fcxend0ad16b2013-02-09 18:34:24 +00001885/*
1886 * Endpoint probe expects that the INP_INFO is locked.
1887 */
tuexendd729232011-11-01 23:04:43 +00001888static struct sctp_inpcb *
1889sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1890 uint16_t lport, uint32_t vrf_id)
1891{
1892 struct sctp_inpcb *inp;
1893 struct sctp_laddr *laddr;
1894#ifdef INET
1895 struct sockaddr_in *sin;
1896#endif
1897#ifdef INET6
1898 struct sockaddr_in6 *sin6;
1899 struct sockaddr_in6 *intf_addr6;
1900#endif
tuexen09a676e2012-07-08 21:03:18 +00001901#if defined(__Userspace__)
1902 struct sockaddr_conn *sconn;
1903#endif
tuexendd729232011-11-01 23:04:43 +00001904#ifdef SCTP_MVRF
1905 int i;
1906#endif
1907 int fnd;
t00fcxend0ad16b2013-02-09 18:34:24 +00001908
tuexendd729232011-11-01 23:04:43 +00001909#ifdef INET
1910 sin = NULL;
1911#endif
1912#ifdef INET6
1913 sin6 = NULL;
1914#endif
t00fcxenb4c54ac2012-09-15 09:17:50 +00001915#if defined(__Userspace__)
1916 sconn = NULL;
1917#endif
tuexendd729232011-11-01 23:04:43 +00001918 switch (nam->sa_family) {
1919#ifdef INET
1920 case AF_INET:
1921 sin = (struct sockaddr_in *)nam;
1922 break;
1923#endif
1924#ifdef INET6
1925 case AF_INET6:
1926 sin6 = (struct sockaddr_in6 *)nam;
1927 break;
1928#endif
tuexen09a676e2012-07-08 21:03:18 +00001929#if defined(__Userspace__)
1930 case AF_CONN:
1931 sconn = (struct sockaddr_conn *)nam;
1932 break;
1933#endif
tuexendd729232011-11-01 23:04:43 +00001934 default:
1935 /* unsupported family */
1936 return (NULL);
1937 }
1938
1939 if (head == NULL)
1940 return (NULL);
tuexen09631162012-04-18 11:23:02 +00001941
tuexendd729232011-11-01 23:04:43 +00001942 LIST_FOREACH(inp, head, sctp_hash) {
1943 SCTP_INP_RLOCK(inp);
1944 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1945 SCTP_INP_RUNLOCK(inp);
1946 continue;
1947 }
1948 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1949 (inp->sctp_lport == lport)) {
1950 /* got it */
t00fcxen326aa4b2014-06-20 13:42:13 +00001951 switch (nam->sa_family) {
tuexendd729232011-11-01 23:04:43 +00001952#ifdef INET
t00fcxen326aa4b2014-06-20 13:42:13 +00001953 case AF_INET:
1954 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02001955 SCTP_IPV6_V6ONLY(inp)) {
t00fcxen326aa4b2014-06-20 13:42:13 +00001956 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1957 SCTP_INP_RUNLOCK(inp);
1958 continue;
1959 }
1960#if defined(__FreeBSD__)
1961 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1962 &sin->sin_addr) != 0) {
1963 SCTP_INP_RUNLOCK(inp);
1964 continue;
1965 }
1966#endif
1967 break;
tuexendd729232011-11-01 23:04:43 +00001968#endif
1969#ifdef INET6
t00fcxen326aa4b2014-06-20 13:42:13 +00001970 case AF_INET6:
1971 /* A V6 address and the endpoint is NOT bound V6 */
1972 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1973 SCTP_INP_RUNLOCK(inp);
1974 continue;
1975 }
1976#if defined(__FreeBSD__)
1977 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1978 &sin6->sin6_addr) != 0) {
1979 SCTP_INP_RUNLOCK(inp);
1980 continue;
1981 }
tuexendd729232011-11-01 23:04:43 +00001982#endif
t00fcxen326aa4b2014-06-20 13:42:13 +00001983 break;
1984#endif
1985 default:
1986 break;
1987 }
tuexendd729232011-11-01 23:04:43 +00001988 /* does a VRF id match? */
1989 fnd = 0;
1990#ifdef SCTP_MVRF
1991 for (i = 0; i < inp->num_vrfs; i++) {
1992 if (inp->m_vrf_ids[i] == vrf_id) {
1993 fnd = 1;
1994 break;
1995 }
1996 }
1997#else
1998 if (inp->def_vrf_id == vrf_id)
1999 fnd = 1;
2000#endif
2001
2002 SCTP_INP_RUNLOCK(inp);
2003 if (!fnd)
2004 continue;
2005 return (inp);
2006 }
2007 SCTP_INP_RUNLOCK(inp);
2008 }
2009 switch (nam->sa_family) {
2010#ifdef INET
2011 case AF_INET:
2012 if (sin->sin_addr.s_addr == INADDR_ANY) {
2013 /* Can't hunt for one that has no address specified */
2014 return (NULL);
2015 }
2016 break;
2017#endif
2018#ifdef INET6
2019 case AF_INET6:
tuexen63fc0bb2011-12-27 12:24:52 +00002020 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
tuexendd729232011-11-01 23:04:43 +00002021 /* Can't hunt for one that has no address specified */
2022 return (NULL);
2023 }
2024 break;
2025#endif
tuexen09a676e2012-07-08 21:03:18 +00002026#if defined(__Userspace__)
2027 case AF_CONN:
2028 if (sconn->sconn_addr == NULL) {
2029 return (NULL);
2030 }
2031 break;
2032#endif
tuexendd729232011-11-01 23:04:43 +00002033 default:
2034 break;
2035 }
2036 /*
2037 * ok, not bound to all so see if we can find a EP bound to this
2038 * address.
2039 */
2040 LIST_FOREACH(inp, head, sctp_hash) {
2041 SCTP_INP_RLOCK(inp);
2042 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2043 SCTP_INP_RUNLOCK(inp);
2044 continue;
2045 }
2046 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
2047 SCTP_INP_RUNLOCK(inp);
2048 continue;
2049 }
2050 /*
2051 * Ok this could be a likely candidate, look at all of its
2052 * addresses
2053 */
2054 if (inp->sctp_lport != lport) {
2055 SCTP_INP_RUNLOCK(inp);
2056 continue;
2057 }
2058 /* does a VRF id match? */
2059 fnd = 0;
2060#ifdef SCTP_MVRF
2061 for (i = 0; i < inp->num_vrfs; i++) {
2062 if (inp->m_vrf_ids[i] == vrf_id) {
2063 fnd = 1;
2064 break;
2065 }
2066 }
2067#else
2068 if (inp->def_vrf_id == vrf_id)
2069 fnd = 1;
2070
2071#endif
2072 if (!fnd) {
2073 SCTP_INP_RUNLOCK(inp);
2074 continue;
2075 }
2076 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2077 if (laddr->ifa == NULL) {
2078 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002079 __func__);
tuexendd729232011-11-01 23:04:43 +00002080 continue;
2081 }
2082 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
t00fcxen8fca7ff2012-09-05 18:58:42 +00002083 (void *)laddr->ifa);
tuexendd729232011-11-01 23:04:43 +00002084 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2085 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
2086 continue;
2087 }
2088 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
2089 /* possible, see if it matches */
2090 switch (nam->sa_family) {
2091#ifdef INET
2092 case AF_INET:
tuexen63fc0bb2011-12-27 12:24:52 +00002093#if defined(__APPLE__)
2094 if (sin == NULL) {
2095 /* TSNH */
2096 break;
2097 }
2098#endif
tuexendd729232011-11-01 23:04:43 +00002099 if (sin->sin_addr.s_addr ==
2100 laddr->ifa->address.sin.sin_addr.s_addr) {
2101 SCTP_INP_RUNLOCK(inp);
2102 return (inp);
2103 }
2104 break;
2105#endif
2106#ifdef INET6
2107 case AF_INET6:
2108 intf_addr6 = &laddr->ifa->address.sin6;
2109 if (SCTP6_ARE_ADDR_EQUAL(sin6,
2110 intf_addr6)) {
2111 SCTP_INP_RUNLOCK(inp);
2112 return (inp);
2113 }
2114 break;
2115#endif
tuexen09a676e2012-07-08 21:03:18 +00002116#if defined(__Userspace__)
2117 case AF_CONN:
2118 if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
2119 SCTP_INP_RUNLOCK(inp);
2120 return (inp);
2121 }
2122 break;
2123#endif
tuexendd729232011-11-01 23:04:43 +00002124 }
2125 }
2126 }
2127 SCTP_INP_RUNLOCK(inp);
2128 }
2129 return (NULL);
2130}
2131
2132
2133static struct sctp_inpcb *
2134sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
2135{
2136 struct sctppcbhead *head;
2137 struct sctp_inpcb *t_inp;
2138#ifdef SCTP_MVRF
2139 int i;
2140#endif
2141 int fnd;
2142
2143 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2144 SCTP_BASE_INFO(hashmark))];
2145 LIST_FOREACH(t_inp, head, sctp_hash) {
2146 if (t_inp->sctp_lport != lport) {
2147 continue;
2148 }
2149 /* is it in the VRF in question */
2150 fnd = 0;
2151#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00002152 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00002153 if (t_inp->m_vrf_ids[i] == vrf_id) {
2154 fnd = 1;
2155 break;
2156 }
2157 }
2158#else
2159 if (t_inp->def_vrf_id == vrf_id)
2160 fnd = 1;
2161#endif
2162 if (!fnd)
2163 continue;
2164
2165 /* This one is in use. */
2166 /* check the v6/v4 binding issue */
2167 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02002168 SCTP_IPV6_V6ONLY(t_inp)) {
tuexendd729232011-11-01 23:04:43 +00002169 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2170 /* collision in V6 space */
2171 return (t_inp);
2172 } else {
2173 /* inp is BOUND_V4 no conflict */
2174 continue;
2175 }
2176 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2177 /* t_inp is bound v4 and v6, conflict always */
2178 return (t_inp);
2179 } else {
2180 /* t_inp is bound only V4 */
2181 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02002182 SCTP_IPV6_V6ONLY(inp)) {
tuexendd729232011-11-01 23:04:43 +00002183 /* no conflict */
2184 continue;
2185 }
2186 /* else fall through to conflict */
2187 }
2188 return (t_inp);
2189 }
2190 return (NULL);
2191}
2192
2193
2194int
2195sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
2196{
2197 /* For 1-2-1 with port reuse */
2198 struct sctppcbhead *head;
t00fcxenc00f4bc2015-03-24 21:17:16 +00002199 struct sctp_inpcb *tinp, *ninp;
tuexendd729232011-11-01 23:04:43 +00002200
2201 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
2202 /* only works with port reuse on */
tuexen63fc0bb2011-12-27 12:24:52 +00002203 return (-1);
tuexendd729232011-11-01 23:04:43 +00002204 }
2205 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
2206 return (0);
2207 }
2208 SCTP_INP_RUNLOCK(inp);
t00fcxenc00f4bc2015-03-24 21:17:16 +00002209 SCTP_INP_INFO_WLOCK();
tuexendd729232011-11-01 23:04:43 +00002210 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
2211 SCTP_BASE_INFO(hashmark))];
2212 /* Kick out all non-listeners to the TCP hash */
t00fcxenc00f4bc2015-03-24 21:17:16 +00002213 LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
tuexendd729232011-11-01 23:04:43 +00002214 if (tinp->sctp_lport != inp->sctp_lport) {
2215 continue;
2216 }
2217 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2218 continue;
2219 }
2220 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2221 continue;
2222 }
Michael Tuexend900e5f2017-06-23 10:52:43 +02002223 if (SCTP_IS_LISTENING(tinp)) {
tuexendd729232011-11-01 23:04:43 +00002224 continue;
2225 }
2226 SCTP_INP_WLOCK(tinp);
2227 LIST_REMOVE(tinp, sctp_hash);
2228 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
2229 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
2230 LIST_INSERT_HEAD(head, tinp, sctp_hash);
2231 SCTP_INP_WUNLOCK(tinp);
2232 }
2233 SCTP_INP_WLOCK(inp);
2234 /* Pull from where he was */
2235 LIST_REMOVE(inp, sctp_hash);
2236 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
2237 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
2238 LIST_INSERT_HEAD(head, inp, sctp_hash);
2239 SCTP_INP_WUNLOCK(inp);
2240 SCTP_INP_RLOCK(inp);
t00fcxenc00f4bc2015-03-24 21:17:16 +00002241 SCTP_INP_INFO_WUNLOCK();
tuexendd729232011-11-01 23:04:43 +00002242 return (0);
2243}
2244
2245
2246struct sctp_inpcb *
2247sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
2248 uint32_t vrf_id)
2249{
2250 /*
2251 * First we check the hash table to see if someone has this port
2252 * bound with just the port.
2253 */
2254 struct sctp_inpcb *inp;
2255 struct sctppcbhead *head;
2256 int lport;
2257 unsigned int i;
2258#ifdef INET
2259 struct sockaddr_in *sin;
2260#endif
2261#ifdef INET6
2262 struct sockaddr_in6 *sin6;
2263#endif
tuexen09a676e2012-07-08 21:03:18 +00002264#if defined(__Userspace__)
2265 struct sockaddr_conn *sconn;
2266#endif
tuexendd729232011-11-01 23:04:43 +00002267
2268 switch (nam->sa_family) {
2269#ifdef INET
2270 case AF_INET:
2271 sin = (struct sockaddr_in *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002272 lport = sin->sin_port;
tuexendd729232011-11-01 23:04:43 +00002273 break;
2274#endif
2275#ifdef INET6
2276 case AF_INET6:
2277 sin6 = (struct sockaddr_in6 *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002278 lport = sin6->sin6_port;
tuexendd729232011-11-01 23:04:43 +00002279 break;
2280#endif
tuexen09a676e2012-07-08 21:03:18 +00002281#if defined(__Userspace__)
2282 case AF_CONN:
2283 sconn = (struct sockaddr_conn *)nam;
2284 lport = sconn->sconn_port;
2285 break;
2286#endif
tuexendd729232011-11-01 23:04:43 +00002287 default:
2288 return (NULL);
2289 }
2290 /*
2291 * I could cheat here and just cast to one of the types but we will
2292 * do it right. It also provides the check against an Unsupported
2293 * type too.
2294 */
2295 /* Find the head of the ALLADDR chain */
2296 if (have_lock == 0) {
2297 SCTP_INP_INFO_RLOCK();
2298 }
2299 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2300 SCTP_BASE_INFO(hashmark))];
2301 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2302
2303 /*
2304 * If the TCP model exists it could be that the main listening
2305 * endpoint is gone but there still exists a connected socket for this
2306 * guy. If so we can return the first one that we find. This may NOT
2307 * be the correct one so the caller should be wary on the returned INP.
2308 * Currently the only caller that sets find_tcp_pool is in bindx where
2309 * we are verifying that a user CAN bind the address. He either
2310 * has bound it already, or someone else has, or its open to bind,
2311 * so this is good enough.
2312 */
2313 if (inp == NULL && find_tcp_pool) {
2314 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
2315 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
2316 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2317 if (inp) {
2318 break;
2319 }
2320 }
2321 }
2322 if (inp) {
2323 SCTP_INP_INCR_REF(inp);
2324 }
2325 if (have_lock == 0) {
2326 SCTP_INP_INFO_RUNLOCK();
2327 }
2328 return (inp);
2329}
2330
t00fcxend0ad16b2013-02-09 18:34:24 +00002331
tuexendd729232011-11-01 23:04:43 +00002332/*
2333 * Find an association for an endpoint with the pointer to whom you want to
2334 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
2335 * need to change the *to to some other struct like a mbuf...
2336 */
2337struct sctp_tcb *
tuexen664967a2012-06-28 16:24:36 +00002338sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
tuexendd729232011-11-01 23:04:43 +00002339 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
2340 uint32_t vrf_id)
2341{
2342 struct sctp_inpcb *inp = NULL;
t00fcxen60d0dea2013-02-10 20:04:03 +00002343 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00002344
2345 SCTP_INP_INFO_RLOCK();
2346 if (find_tcp_pool) {
2347 if (inp_p != NULL) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002348 stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
2349 vrf_id);
tuexendd729232011-11-01 23:04:43 +00002350 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002351 stcb = sctp_tcb_special_locate(&inp, from, to, netp,
2352 vrf_id);
tuexendd729232011-11-01 23:04:43 +00002353 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002354 if (stcb != NULL) {
tuexendd729232011-11-01 23:04:43 +00002355 SCTP_INP_INFO_RUNLOCK();
t00fcxen60d0dea2013-02-10 20:04:03 +00002356 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002357 }
2358 }
2359 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
2360 if (inp_p != NULL) {
2361 *inp_p = inp;
2362 }
2363 SCTP_INP_INFO_RUNLOCK();
tuexendd729232011-11-01 23:04:43 +00002364 if (inp == NULL) {
2365 return (NULL);
2366 }
2367 /*
2368 * ok, we have an endpoint, now lets find the assoc for it (if any)
2369 * we now place the source address or from in the to of the find
2370 * endpoint call. Since in reality this chain is used from the
2371 * inbound packet side.
2372 */
2373 if (inp_p != NULL) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002374 stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
2375 NULL);
tuexendd729232011-11-01 23:04:43 +00002376 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002377 stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
2378 NULL);
tuexendd729232011-11-01 23:04:43 +00002379 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002380 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002381}
2382
2383
2384/*
2385 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
2386 * find all addresses that the sender has specified in any address list. Each
2387 * address will be used to lookup the TCB and see if one exits.
2388 */
2389static struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002390sctp_findassociation_special_addr(struct mbuf *m, int offset,
tuexendd729232011-11-01 23:04:43 +00002391 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
tuexen664967a2012-06-28 16:24:36 +00002392 struct sockaddr *dst)
tuexendd729232011-11-01 23:04:43 +00002393{
Michael Tuexen473225f2017-06-23 23:05:43 +02002394 struct sctp_paramhdr *phdr, param_buf;
t00fcxenaff14b62013-11-02 18:02:02 +00002395#if defined(INET) || defined(INET6)
t00fcxen6ebb6032013-11-02 18:03:31 +00002396 struct sctp_tcb *stcb;
t00fcxenaff14b62013-11-02 18:02:02 +00002397 uint16_t ptype;
2398#endif
2399 uint16_t plen;
tuexendd729232011-11-01 23:04:43 +00002400#ifdef INET
2401 struct sockaddr_in sin4;
2402#endif
2403#ifdef INET6
2404 struct sockaddr_in6 sin6;
2405#endif
2406
2407#ifdef INET
2408 memset(&sin4, 0, sizeof(sin4));
t00fcxen8fbaee32012-09-04 16:41:39 +00002409#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002410 sin4.sin_len = sizeof(sin4);
2411#endif
2412 sin4.sin_family = AF_INET;
2413 sin4.sin_port = sh->src_port;
2414#endif
2415#ifdef INET6
2416 memset(&sin6, 0, sizeof(sin6));
t00fcxen8fbaee32012-09-04 16:41:39 +00002417#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002418 sin6.sin6_len = sizeof(sin6);
2419#endif
2420 sin6.sin6_family = AF_INET6;
2421 sin6.sin6_port = sh->src_port;
2422#endif
2423
tuexendd729232011-11-01 23:04:43 +00002424 offset += sizeof(struct sctp_init_chunk);
2425
Michael Tuexen473225f2017-06-23 23:05:43 +02002426 phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00002427 while (phdr != NULL) {
2428 /* now we must see if we want the parameter */
t00fcxenaff14b62013-11-02 18:02:02 +00002429#if defined(INET) || defined(INET6)
tuexendd729232011-11-01 23:04:43 +00002430 ptype = ntohs(phdr->param_type);
t00fcxenaff14b62013-11-02 18:02:02 +00002431#endif
tuexendd729232011-11-01 23:04:43 +00002432 plen = ntohs(phdr->param_length);
2433 if (plen == 0) {
2434 break;
2435 }
2436#ifdef INET
2437 if (ptype == SCTP_IPV4_ADDRESS &&
2438 plen == sizeof(struct sctp_ipv4addr_param)) {
2439 /* Get the rest of the address */
Michael Tuexen473225f2017-06-23 23:05:43 +02002440 struct sctp_ipv4addr_param ip4_param, *p4;
tuexendd729232011-11-01 23:04:43 +00002441
2442 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02002443 (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
tuexendd729232011-11-01 23:04:43 +00002444 if (phdr == NULL) {
2445 return (NULL);
2446 }
2447 p4 = (struct sctp_ipv4addr_param *)phdr;
2448 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2449 /* look it up */
t00fcxen60d0dea2013-02-10 20:04:03 +00002450 stcb = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002451 (struct sockaddr *)&sin4, netp, dst, NULL);
t00fcxen60d0dea2013-02-10 20:04:03 +00002452 if (stcb != NULL) {
2453 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002454 }
2455 }
2456#endif
2457#ifdef INET6
2458 if (ptype == SCTP_IPV6_ADDRESS &&
2459 plen == sizeof(struct sctp_ipv6addr_param)) {
2460 /* Get the rest of the address */
Michael Tuexen473225f2017-06-23 23:05:43 +02002461 struct sctp_ipv6addr_param ip6_param, *p6;
tuexendd729232011-11-01 23:04:43 +00002462
2463 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02002464 (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
tuexendd729232011-11-01 23:04:43 +00002465 if (phdr == NULL) {
2466 return (NULL);
2467 }
2468 p6 = (struct sctp_ipv6addr_param *)phdr;
2469 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2470 /* look it up */
t00fcxen60d0dea2013-02-10 20:04:03 +00002471 stcb = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002472 (struct sockaddr *)&sin6, netp, dst, NULL);
t00fcxen60d0dea2013-02-10 20:04:03 +00002473 if (stcb != NULL) {
2474 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002475 }
2476 }
2477#endif
2478 offset += SCTP_SIZE32(plen);
Michael Tuexen473225f2017-06-23 23:05:43 +02002479 phdr = sctp_get_next_param(m, offset, &param_buf,
2480 sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00002481 }
2482 return (NULL);
2483}
2484
2485static struct sctp_tcb *
2486sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2487 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2488 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2489{
2490 /*
2491 * Use my vtag to hash. If we find it we then verify the source addr
2492 * is in the assoc. If all goes well we save a bit on rec of a
2493 * packet.
2494 */
2495 struct sctpasochead *head;
2496 struct sctp_nets *net;
2497 struct sctp_tcb *stcb;
tuexen9784e9a2011-12-18 13:04:23 +00002498#ifdef SCTP_MVRF
2499 unsigned int i;
2500#endif
tuexendd729232011-11-01 23:04:43 +00002501
tuexendd729232011-11-01 23:04:43 +00002502 SCTP_INP_INFO_RLOCK();
2503 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
t00fcxen0d664882015-01-11 21:47:36 +00002504 SCTP_BASE_INFO(hashasocmark))];
tuexendd729232011-11-01 23:04:43 +00002505 LIST_FOREACH(stcb, head, sctp_asocs) {
2506 SCTP_INP_RLOCK(stcb->sctp_ep);
2507 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2508 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2509 continue;
2510 }
tuexen9784e9a2011-12-18 13:04:23 +00002511#ifdef SCTP_MVRF
2512 for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) {
2513 if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) {
2514 break;
2515 }
2516 }
2517 if (i == stcb->sctp_ep->num_vrfs) {
2518 SCTP_INP_RUNLOCK(inp);
2519 continue;
2520 }
2521#else
2522 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2523 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2524 continue;
2525 }
2526#endif
tuexendd729232011-11-01 23:04:43 +00002527 SCTP_TCB_LOCK(stcb);
2528 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2529 if (stcb->asoc.my_vtag == vtag) {
2530 /* candidate */
2531 if (stcb->rport != rport) {
2532 SCTP_TCB_UNLOCK(stcb);
2533 continue;
2534 }
2535 if (stcb->sctp_ep->sctp_lport != lport) {
2536 SCTP_TCB_UNLOCK(stcb);
2537 continue;
2538 }
2539 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2540 SCTP_TCB_UNLOCK(stcb);
2541 continue;
2542 }
2543 /* RRS:Need toaddr check here */
2544 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2545 /* Endpoint does not own this address */
2546 SCTP_TCB_UNLOCK(stcb);
2547 continue;
2548 }
2549 if (remote_tag) {
2550 /* If we have both vtags that's all we match on */
2551 if (stcb->asoc.peer_vtag == remote_tag) {
2552 /* If both tags match we consider it conclusive
2553 * and check NO source/destination addresses
2554 */
2555 goto conclusive;
2556 }
2557 }
2558 if (skip_src_check) {
2559 conclusive:
2560 if (from) {
tuexen63fc0bb2011-12-27 12:24:52 +00002561 *netp = sctp_findnet(stcb, from);
tuexendd729232011-11-01 23:04:43 +00002562 } else {
2563 *netp = NULL; /* unknown */
2564 }
2565 if (inp_p)
2566 *inp_p = stcb->sctp_ep;
2567 SCTP_INP_INFO_RUNLOCK();
2568 return (stcb);
2569 }
2570 net = sctp_findnet(stcb, from);
2571 if (net) {
2572 /* yep its him. */
2573 *netp = net;
2574 SCTP_STAT_INCR(sctps_vtagexpress);
2575 *inp_p = stcb->sctp_ep;
2576 SCTP_INP_INFO_RUNLOCK();
2577 return (stcb);
2578 } else {
2579 /*
2580 * not him, this should only happen in rare
2581 * cases so I peg it.
2582 */
2583 SCTP_STAT_INCR(sctps_vtagbogus);
2584 }
2585 }
2586 SCTP_TCB_UNLOCK(stcb);
2587 }
2588 SCTP_INP_INFO_RUNLOCK();
2589 return (NULL);
2590}
2591
t00fcxend0ad16b2013-02-09 18:34:24 +00002592
tuexendd729232011-11-01 23:04:43 +00002593/*
2594 * Find an association with the pointer to the inbound IP packet. This can be
2595 * a IPv4 or IPv6 packet.
2596 */
2597struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002598sctp_findassociation_addr(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002599 struct sockaddr *src, struct sockaddr *dst,
tuexendd729232011-11-01 23:04:43 +00002600 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2601 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2602{
t00fcxen60d0dea2013-02-10 20:04:03 +00002603 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00002604 struct sctp_inpcb *inp;
2605
tuexendd729232011-11-01 23:04:43 +00002606 if (sh->v_tag) {
2607 /* we only go down this path if vtag is non-zero */
t00fcxen60d0dea2013-02-10 20:04:03 +00002608 stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
t00fcxenafd21192013-05-10 18:15:02 +00002609 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
t00fcxen60d0dea2013-02-10 20:04:03 +00002610 if (stcb) {
2611 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002612 }
2613 }
2614
tuexendd729232011-11-01 23:04:43 +00002615 if (inp_p) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002616 stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
Michael Tuexen7f202782016-01-28 17:09:30 +01002617 1, vrf_id);
tuexendd729232011-11-01 23:04:43 +00002618 inp = *inp_p;
2619 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002620 stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
Michael Tuexen7f202782016-01-28 17:09:30 +01002621 1, vrf_id);
tuexendd729232011-11-01 23:04:43 +00002622 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002623 SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2624 if (stcb == NULL && inp) {
tuexendd729232011-11-01 23:04:43 +00002625 /* Found a EP but not this address */
2626 if ((ch->chunk_type == SCTP_INITIATION) ||
2627 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2628 /*-
2629 * special hook, we do NOT return linp or an
2630 * association that is linked to an existing
2631 * association that is under the TCP pool (i.e. no
2632 * listener exists). The endpoint finding routine
2633 * will always find a listener before examining the
2634 * TCP pool.
2635 */
2636 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2637 if (inp_p) {
2638 *inp_p = NULL;
2639 }
2640 return (NULL);
2641 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002642 stcb = sctp_findassociation_special_addr(m,
tuexen664967a2012-06-28 16:24:36 +00002643 offset, sh, &inp, netp, dst);
tuexendd729232011-11-01 23:04:43 +00002644 if (inp_p != NULL) {
2645 *inp_p = inp;
2646 }
2647 }
2648 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002649 SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2650 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002651}
2652
2653/*
2654 * lookup an association by an ASCONF lookup address.
2655 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2656 */
2657struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002658sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002659 struct sockaddr *dst, struct sctphdr *sh,
2660 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
tuexendd729232011-11-01 23:04:43 +00002661{
2662 struct sctp_tcb *stcb;
t00fcxen05f07c02014-09-07 10:15:26 +00002663 union sctp_sockstore remote_store;
Michael Tuexen473225f2017-06-23 23:05:43 +02002664 struct sctp_paramhdr param_buf, *phdr;
tuexendd729232011-11-01 23:04:43 +00002665 int ptype;
2666 int zero_address = 0;
2667#ifdef INET
2668 struct sockaddr_in *sin;
2669#endif
2670#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00002671 struct sockaddr_in6 *sin6;
2672#endif
2673
tuexendd729232011-11-01 23:04:43 +00002674 memset(&remote_store, 0, sizeof(remote_store));
tuexendd729232011-11-01 23:04:43 +00002675 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002676 &param_buf, sizeof(struct sctp_paramhdr));
tuexendd729232011-11-01 23:04:43 +00002677 if (phdr == NULL) {
2678 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002679 __func__);
tuexendd729232011-11-01 23:04:43 +00002680 return NULL;
2681 }
2682 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2683 /* get the correlation address */
2684 switch (ptype) {
2685#ifdef INET6
2686 case SCTP_IPV6_ADDRESS:
2687 {
2688 /* ipv6 address param */
2689 struct sctp_ipv6addr_param *p6, p6_buf;
2690
2691 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2692 return NULL;
2693 }
2694 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2695 offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002696 &p6_buf.ph, sizeof(p6_buf));
tuexendd729232011-11-01 23:04:43 +00002697 if (p6 == NULL) {
2698 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002699 __func__);
tuexendd729232011-11-01 23:04:43 +00002700 return (NULL);
2701 }
t00fcxen05f07c02014-09-07 10:15:26 +00002702 sin6 = &remote_store.sin6;
tuexendd729232011-11-01 23:04:43 +00002703 sin6->sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00002704#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002705 sin6->sin6_len = sizeof(*sin6);
2706#endif
2707 sin6->sin6_port = sh->src_port;
2708 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2709 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2710 zero_address = 1;
2711 break;
2712 }
2713#endif
2714#ifdef INET
2715 case SCTP_IPV4_ADDRESS:
2716 {
2717 /* ipv4 address param */
2718 struct sctp_ipv4addr_param *p4, p4_buf;
2719
2720 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2721 return NULL;
2722 }
2723 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2724 offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002725 &p4_buf.ph, sizeof(p4_buf));
tuexendd729232011-11-01 23:04:43 +00002726 if (p4 == NULL) {
2727 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002728 __func__);
tuexendd729232011-11-01 23:04:43 +00002729 return (NULL);
2730 }
t00fcxen05f07c02014-09-07 10:15:26 +00002731 sin = &remote_store.sin;
tuexendd729232011-11-01 23:04:43 +00002732 sin->sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00002733#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002734 sin->sin_len = sizeof(*sin);
2735#endif
2736 sin->sin_port = sh->src_port;
2737 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2738 if (sin->sin_addr.s_addr == INADDR_ANY)
2739 zero_address = 1;
2740 break;
2741 }
2742#endif
2743 default:
2744 /* invalid address param type */
2745 return NULL;
2746 }
2747
2748 if (zero_address) {
tuexen664967a2012-06-28 16:24:36 +00002749 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
tuexendd729232011-11-01 23:04:43 +00002750 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
tuexenb53ec512012-08-05 10:55:12 +00002751 if (stcb != NULL) {
2752 SCTP_INP_DECR_REF(*inp_p);
2753 }
tuexendd729232011-11-01 23:04:43 +00002754 } else {
2755 stcb = sctp_findassociation_ep_addr(inp_p,
t00fcxen05f07c02014-09-07 10:15:26 +00002756 &remote_store.sa, netp,
tuexen664967a2012-06-28 16:24:36 +00002757 dst, NULL);
tuexendd729232011-11-01 23:04:43 +00002758 }
2759 return (stcb);
2760}
2761
2762
2763/*
2764 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2765 * addresses. This way if we don't get a bind we by default pick a ephemeral
2766 * port with all addresses bound.
2767 */
2768int
2769sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2770{
2771 /*
2772 * we get called when a new endpoint starts up. We need to allocate
2773 * the sctp_inpcb structure from the zone and init it. Mark it as
2774 * unbound and find a port that we can use as an ephemeral with
2775 * INADDR_ANY. If the user binds later no problem we can then add in
2776 * the specific addresses. And setup the default parameters for the
2777 * EP.
2778 */
2779 int i, error;
2780 struct sctp_inpcb *inp;
2781 struct sctp_pcb *m;
2782 struct timeval time;
2783 sctp_sharedkey_t *null_key;
2784
2785 error = 0;
2786
2787 SCTP_INP_INFO_WLOCK();
2788 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2789 if (inp == NULL) {
2790 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2791 SCTP_INP_INFO_WUNLOCK();
2792 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2793 return (ENOBUFS);
2794 }
2795 /* zap it */
Michael Tuexend0853302017-07-19 17:01:31 +02002796 memset(inp, 0, sizeof(*inp));
tuexendd729232011-11-01 23:04:43 +00002797
2798 /* bump generations */
2799#if defined(__APPLE__)
2800 inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
2801#endif
2802 /* setup socket pointers */
2803 inp->sctp_socket = so;
2804 inp->ip_inp.inp.inp_socket = so;
t00fcxen326aa4b2014-06-20 13:42:13 +00002805#if defined(__FreeBSD__)
2806 inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
2807#endif
tuexenc0bb6122012-01-15 14:21:21 +00002808#ifdef INET6
tuexen76eb2092012-04-01 16:35:28 +00002809#if !defined(__Userspace__) && !defined(__Windows__)
t00fcxenafd21192013-05-10 18:15:02 +00002810 if (INP_SOCKAF(so) == AF_INET6) {
2811 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2812 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2813 }
2814 if (MODULE_GLOBAL(ip6_v6only)) {
2815 inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
2816 }
tuexendd729232011-11-01 23:04:43 +00002817 }
2818#endif
tuexenc0bb6122012-01-15 14:21:21 +00002819#endif
tuexendd729232011-11-01 23:04:43 +00002820 inp->sctp_associd_counter = 1;
2821 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2822 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
t00fcxen3eda0f22015-03-10 20:10:45 +00002823 inp->max_cwnd = 0;
tuexendd729232011-11-01 23:04:43 +00002824 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
t00fcxen478b7722014-08-02 18:24:12 +00002825 inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
t00fcxen0e78cef2014-08-02 22:05:33 +00002826 inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
t00fcxenb3969a12014-08-12 13:19:21 +00002827 inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
t00fcxenaac0f452014-08-12 12:51:45 +00002828 inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
t00fcxenfa7e1a02014-08-04 20:41:36 +00002829 inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
t00fcxen3656cf92014-08-03 15:25:02 +00002830 inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
t00fcxen1cc0ba82014-08-03 20:32:08 +00002831 inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
Michael Tuexene5001952016-04-17 19:25:27 +02002832 inp->idata_supported = 0;
2833
t00fcxena51e4462015-06-17 15:53:23 +00002834#if defined(__FreeBSD__)
2835 inp->fibnum = so->so_fibnum;
2836#else
2837 inp->fibnum = 0;
2838#endif
tuexen09631162012-04-18 11:23:02 +00002839#if defined(__Userspace__)
tuexeneda9dbb2012-05-27 11:10:08 +00002840 inp->ulp_info = NULL;
tuexen09631162012-04-18 11:23:02 +00002841 inp->recv_callback = NULL;
2842 inp->send_callback = NULL;
2843 inp->send_sb_threshold = 0;
tuexen09631162012-04-18 11:23:02 +00002844#endif
tuexendd729232011-11-01 23:04:43 +00002845 /* init the small hash table we use to track asocid <-> tcb */
2846 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2847 if (inp->sctp_asocidhash == NULL) {
t00fcxen326aa4b2014-06-20 13:42:13 +00002848#if defined(__FreeBSD__)
2849 crfree(inp->ip_inp.inp.inp_cred);
2850#endif
tuexendd729232011-11-01 23:04:43 +00002851 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2852 SCTP_INP_INFO_WUNLOCK();
2853 return (ENOBUFS);
2854 }
tuexendd729232011-11-01 23:04:43 +00002855 SCTP_INCR_EP_COUNT();
tuexendd729232011-11-01 23:04:43 +00002856 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
tuexendd729232011-11-01 23:04:43 +00002857 SCTP_INP_INFO_WUNLOCK();
2858
2859 so->so_pcb = (caddr_t)inp;
2860
tuexen5fc22842012-02-11 13:32:19 +00002861#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00002862 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
2863 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
2864#else
2865 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2866#endif
2867 /* UDP style socket */
2868 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2869 SCTP_PCB_FLAGS_UNBOUND);
2870 /* Be sure it is NON-BLOCKING IO for UDP */
2871 /* SCTP_SET_SO_NBIO(so); */
2872 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2873 /* TCP style socket */
2874 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2875 SCTP_PCB_FLAGS_UNBOUND);
2876 /* Be sure we have blocking IO by default */
Michael Tuexenfe223572017-09-22 00:19:22 +02002877 SOCK_LOCK(so);
tuexendd729232011-11-01 23:04:43 +00002878 SCTP_CLEAR_SO_NBIO(so);
Michael Tuexenfe223572017-09-22 00:19:22 +02002879 SOCK_UNLOCK(so);
tuexendd729232011-11-01 23:04:43 +00002880 } else {
2881 /*
2882 * unsupported socket type (RAW, etc)- in case we missed it
2883 * in protosw
2884 */
2885 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2886 so->so_pcb = NULL;
t00fcxen326aa4b2014-06-20 13:42:13 +00002887#if defined(__FreeBSD__)
2888 crfree(inp->ip_inp.inp.inp_cred);
2889#endif
tuexendd729232011-11-01 23:04:43 +00002890 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2891 return (EOPNOTSUPP);
2892 }
2893 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2894 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2895 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2896 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2897 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2898 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2899 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2900 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2901 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2902 }
2903 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2904 &inp->sctp_hashmark);
2905 if (inp->sctp_tcbhash == NULL) {
2906 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2907 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2908 so->so_pcb = NULL;
t00fcxen326aa4b2014-06-20 13:42:13 +00002909#if defined(__FreeBSD__)
2910 crfree(inp->ip_inp.inp.inp_cred);
2911#endif
tuexendd729232011-11-01 23:04:43 +00002912 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2913 return (ENOBUFS);
2914 }
2915#ifdef SCTP_MVRF
2916 inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
2917 SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
2918 (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
2919 if (inp->m_vrf_ids == NULL) {
2920 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2921 so->so_pcb = NULL;
2922 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
t00fcxen326aa4b2014-06-20 13:42:13 +00002923#if defined(__FreeBSD__)
2924 crfree(inp->ip_inp.inp.inp_cred);
2925#endif
tuexendd729232011-11-01 23:04:43 +00002926 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2927 return (ENOBUFS);
2928 }
2929 inp->m_vrf_ids[0] = vrf_id;
2930 inp->num_vrfs = 1;
2931#endif
2932 inp->def_vrf_id = vrf_id;
2933
2934#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00002935#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
t00fcxenbc766ab2014-01-03 19:29:31 +00002936 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 +00002937 if (inp->ip_inp.inp.inpcb_mtx == NULL) {
t00fcxen8fca7ff2012-09-05 18:58:42 +00002938 SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so);
tuexendd729232011-11-01 23:04:43 +00002939#ifdef SCTP_MVRF
2940 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
2941#endif
2942 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2943 so->so_pcb = NULL;
2944 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
t00fcxenbc766ab2014-01-03 19:29:31 +00002945 SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock);
tuexendd729232011-11-01 23:04:43 +00002946 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
2947 return (ENOMEM);
2948 }
t00fcxenbc766ab2014-01-03 19:29:31 +00002949#elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2950 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 +00002951#else
t00fcxenbc766ab2014-01-03 19:29:31 +00002952 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 +00002953#endif
2954#endif
2955 SCTP_INP_INFO_WLOCK();
2956 SCTP_INP_LOCK_INIT(inp);
2957#if defined(__FreeBSD__)
2958 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2959#endif
2960 SCTP_INP_READ_INIT(inp);
2961 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2962 /* lock the new ep */
2963 SCTP_INP_WLOCK(inp);
2964
2965 /* add it to the info area */
2966 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2967#if defined(__APPLE__)
t00fcxenbc766ab2014-01-03 19:29:31 +00002968 inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo);
2969#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2970 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list);
2971#else
2972 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list);
2973#endif
tuexendd729232011-11-01 23:04:43 +00002974#endif
2975 SCTP_INP_INFO_WUNLOCK();
2976
2977 TAILQ_INIT(&inp->read_queue);
2978 LIST_INIT(&inp->sctp_addr_list);
2979
2980 LIST_INIT(&inp->sctp_asoc_list);
2981
2982#ifdef SCTP_TRACK_FREED_ASOCS
2983 /* TEMP CODE */
2984 LIST_INIT(&inp->sctp_asoc_free_list);
2985#endif
2986 /* Init the timer structure for signature change */
2987 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2988 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2989
2990 /* now init the actual endpoint default data */
2991 m = &inp->sctp_ep;
2992
2993 /* setup the base timeout information */
Michael Tuexen25935e82020-03-28 22:55:28 +01002994 m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */
2995 m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */
2996 m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2997 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2998 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2999 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
3000 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
tuexendd729232011-11-01 23:04:43 +00003001 /* all max/min max are in ms */
3002 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
3003 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
3004 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
3005 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
3006 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
tuexendd729232011-11-01 23:04:43 +00003007 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
3008 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
3009 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
3010 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
3011 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
3012 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
3013 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
3014 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
3015
3016 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
3017 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
t00fcxenc45180a2013-07-05 10:17:17 +00003018 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
tuexendd729232011-11-01 23:04:43 +00003019 /* number of streams to pre-open on a association */
3020 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
3021
Michael Tuexende0630d2017-11-03 22:04:01 +01003022 m->default_mtu = 0;
tuexendd729232011-11-01 23:04:43 +00003023 /* Add adaptation cookie */
t00fcxen36377202013-02-11 21:07:33 +00003024 m->adaptation_layer_indicator = 0;
3025 m->adaptation_layer_indicator_provided = 0;
tuexendd729232011-11-01 23:04:43 +00003026
3027 /* seed random number generator */
3028 m->random_counter = 1;
3029 m->store_at = SCTP_SIGNATURE_SIZE;
3030 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
3031 sctp_fill_random_store(m);
3032
3033 /* Minimum cookie size */
3034 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
3035 sizeof(struct sctp_state_cookie);
3036 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
3037
3038 /* Setup the initial secret */
3039 (void)SCTP_GETTIME_TIMEVAL(&time);
3040 m->time_of_secret_change = time.tv_sec;
3041
3042 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
3043 m->secret_key[0][i] = sctp_select_initial_TSN(m);
3044 }
3045 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
3046
3047 /* How long is a cookie good for ? */
Michael Tuexen25935e82020-03-28 22:55:28 +01003048 m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
tuexendd729232011-11-01 23:04:43 +00003049 /*
3050 * Initialize authentication parameters
3051 */
3052 m->local_hmacs = sctp_default_supported_hmaclist();
3053 m->local_auth_chunks = sctp_alloc_chunklist();
t00fcxenaac0f452014-08-12 12:51:45 +00003054 if (inp->asconf_supported) {
3055 sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
3056 sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
3057 }
tuexendd729232011-11-01 23:04:43 +00003058 m->default_dscp = 0;
3059#ifdef INET6
3060 m->default_flowlabel = 0;
3061#endif
tuexen9d7b2072011-11-20 16:35:17 +00003062 m->port = 0; /* encapsulation disabled by default */
tuexendd729232011-11-01 23:04:43 +00003063 LIST_INIT(&m->shared_keys);
3064 /* add default NULL key as key id 0 */
3065 null_key = sctp_alloc_sharedkey();
3066 sctp_insert_sharedkey(&m->shared_keys, null_key);
3067 SCTP_INP_WUNLOCK(inp);
3068#ifdef SCTP_LOG_CLOSING
3069 sctp_log_closing(inp, NULL, 12);
3070#endif
3071 return (error);
3072}
3073
3074
3075void
3076sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
3077 struct sctp_tcb *stcb)
3078{
3079 struct sctp_nets *net;
3080 uint16_t lport, rport;
3081 struct sctppcbhead *head;
3082 struct sctp_laddr *laddr, *oladdr;
3083
3084 atomic_add_int(&stcb->asoc.refcnt, 1);
3085 SCTP_TCB_UNLOCK(stcb);
3086 SCTP_INP_INFO_WLOCK();
3087 SCTP_INP_WLOCK(old_inp);
3088 SCTP_INP_WLOCK(new_inp);
3089 SCTP_TCB_LOCK(stcb);
3090 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3091
3092 new_inp->sctp_ep.time_of_secret_change =
3093 old_inp->sctp_ep.time_of_secret_change;
3094 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
3095 sizeof(old_inp->sctp_ep.secret_key));
3096 new_inp->sctp_ep.current_secret_number =
3097 old_inp->sctp_ep.current_secret_number;
3098 new_inp->sctp_ep.last_secret_number =
3099 old_inp->sctp_ep.last_secret_number;
3100 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
3101
3102 /* make it so new data pours into the new socket */
3103 stcb->sctp_socket = new_inp->sctp_socket;
3104 stcb->sctp_ep = new_inp;
3105
3106 /* Copy the port across */
3107 lport = new_inp->sctp_lport = old_inp->sctp_lport;
3108 rport = stcb->rport;
3109 /* Pull the tcb from the old association */
3110 LIST_REMOVE(stcb, sctp_tcbhash);
3111 LIST_REMOVE(stcb, sctp_tcblist);
3112 if (stcb->asoc.in_asocid_hash) {
3113 LIST_REMOVE(stcb, sctp_tcbasocidhash);
3114 }
3115 /* Now insert the new_inp into the TCP connected hash */
3116 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
3117
3118 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
3119 /* Its safe to access */
3120 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3121
3122 /* Now move the tcb into the endpoint list */
3123 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
3124 /*
3125 * Question, do we even need to worry about the ep-hash since we
3126 * only have one connection? Probably not :> so lets get rid of it
3127 * and not suck up any kernel memory in that.
3128 */
3129 if (stcb->asoc.in_asocid_hash) {
3130 struct sctpasochead *lhd;
3131 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
3132 new_inp->hashasocidmark)];
3133 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
3134 }
3135 /* Ok. Let's restart timer. */
3136 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3137 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
3138 stcb, net);
3139 }
3140
3141 SCTP_INP_INFO_WUNLOCK();
3142 if (new_inp->sctp_tcbhash != NULL) {
3143 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
3144 new_inp->sctp_tcbhash = NULL;
3145 }
3146 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3147 /* Subset bound, so copy in the laddr list from the old_inp */
3148 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3149 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3150 if (laddr == NULL) {
3151 /*
3152 * Gak, what can we do? This assoc is really
3153 * HOSED. We probably should send an abort
3154 * here.
3155 */
3156 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3157 continue;
3158 }
3159 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003160 memset(laddr, 0, sizeof(*laddr));
tuexendd729232011-11-01 23:04:43 +00003161 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3162 laddr->ifa = oladdr->ifa;
3163 atomic_add_int(&laddr->ifa->refcount, 1);
3164 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3165 sctp_nxt_addr);
3166 new_inp->laddr_count++;
3167 if (oladdr == stcb->asoc.last_used_address) {
3168 stcb->asoc.last_used_address = laddr;
3169 }
3170 }
3171 }
3172 /* Now any running timers need to be adjusted
3173 * since we really don't care if they are running
3174 * or not just blast in the new_inp into all of
3175 * them.
3176 */
3177
3178 stcb->asoc.dack_timer.ep = (void *)new_inp;
3179 stcb->asoc.asconf_timer.ep = (void *)new_inp;
3180 stcb->asoc.strreset_timer.ep = (void *)new_inp;
3181 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
3182 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
tuexendd729232011-11-01 23:04:43 +00003183 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
3184 /* now what about the nets? */
3185 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3186 net->pmtu_timer.ep = (void *)new_inp;
3187 net->hb_timer.ep = (void *)new_inp;
3188 net->rxt_timer.ep = (void *)new_inp;
3189 }
3190 SCTP_INP_WUNLOCK(new_inp);
3191 SCTP_INP_WUNLOCK(old_inp);
3192}
3193
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003194/*
3195 * insert an laddr entry with the given ifa for the desired list
3196 */
3197static int
3198sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
3199{
3200 struct sctp_laddr *laddr;
3201
3202 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3203 if (laddr == NULL) {
3204 /* out of memory? */
3205 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3206 return (EINVAL);
3207 }
3208 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003209 memset(laddr, 0, sizeof(*laddr));
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003210 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3211 laddr->ifa = ifa;
3212 laddr->action = act;
3213 atomic_add_int(&ifa->refcount, 1);
3214 /* insert it */
3215 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3216
3217 return (0);
3218}
3219
3220/*
3221 * Remove an laddr entry from the local address list (on an assoc)
3222 */
3223static void
3224sctp_remove_laddr(struct sctp_laddr *laddr)
3225{
3226
3227 /* remove from the list */
3228 LIST_REMOVE(laddr, sctp_nxt_addr);
3229 sctp_free_ifa(laddr->ifa);
3230 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
3231 SCTP_DECR_LADDR_COUNT();
3232}
tuexendd729232011-11-01 23:04:43 +00003233
3234#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
3235/*
3236 * Don't know why, but without this there is an unknown reference when
3237 * compiling NetBSD... hmm
3238 */
3239extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3240#endif
3241
3242
3243/* sctp_ifap is used to bypass normal local address validation checks */
3244int
3245#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3246sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
t00fcxenc24796c2014-05-06 15:39:13 +00003247 struct sctp_ifa *sctp_ifap, struct thread *p)
tuexendd729232011-11-01 23:04:43 +00003248#elif defined(__Windows__)
3249sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
t00fcxenc24796c2014-05-06 15:39:13 +00003250 struct sctp_ifa *sctp_ifap, PKTHREAD p)
tuexendd729232011-11-01 23:04:43 +00003251#else
3252sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
t00fcxenc24796c2014-05-06 15:39:13 +00003253 struct sctp_ifa *sctp_ifap, struct proc *p)
tuexendd729232011-11-01 23:04:43 +00003254#endif
3255{
3256 /* bind a ep to a socket address */
3257 struct sctppcbhead *head;
3258 struct sctp_inpcb *inp, *inp_tmp;
Michael Tuexen2e04f872019-08-05 13:56:43 +02003259#if defined(__FreeBSD__) || defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +00003260 struct inpcb *ip_inp;
t00fcxenaff14b62013-11-02 18:02:02 +00003261#endif
tuexendd729232011-11-01 23:04:43 +00003262 int port_reuse_active = 0;
3263 int bindall;
3264#ifdef SCTP_MVRF
3265 int i;
3266#endif
3267 uint16_t lport;
3268 int error;
3269 uint32_t vrf_id;
3270
3271 lport = 0;
tuexendd729232011-11-01 23:04:43 +00003272 bindall = 1;
3273 inp = (struct sctp_inpcb *)so->so_pcb;
Michael Tuexen2e04f872019-08-05 13:56:43 +02003274#if defined(__FreeBSD__) || defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +00003275 ip_inp = (struct inpcb *)so->so_pcb;
t00fcxenaff14b62013-11-02 18:02:02 +00003276#endif
tuexendd729232011-11-01 23:04:43 +00003277#ifdef SCTP_DEBUG
3278 if (addr) {
t00fcxen94dfd1e2012-12-27 08:14:10 +00003279 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
tuexendd729232011-11-01 23:04:43 +00003280 ntohs(((struct sockaddr_in *)addr)->sin_port));
t00fcxen94dfd1e2012-12-27 08:14:10 +00003281 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
tuexendd729232011-11-01 23:04:43 +00003282 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3283 }
3284#endif
3285 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3286 /* already did a bind, subsequent binds NOT allowed ! */
3287 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3288 return (EINVAL);
3289 }
3290#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3291#ifdef INVARIANTS
3292 if (p == NULL)
3293 panic("null proc/thread");
3294#endif
3295#endif
3296 if (addr != NULL) {
3297 switch (addr->sa_family) {
3298#ifdef INET
3299 case AF_INET:
3300 {
3301 struct sockaddr_in *sin;
3302
3303 /* IPV6_V6ONLY socket? */
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003304 if (SCTP_IPV6_V6ONLY(inp)) {
tuexendd729232011-11-01 23:04:43 +00003305 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3306 return (EINVAL);
3307 }
t00fcxena8a26f62012-09-12 19:24:54 +00003308#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003309 if (addr->sa_len != sizeof(*sin)) {
3310 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3311 return (EINVAL);
3312 }
3313#endif
3314
3315 sin = (struct sockaddr_in *)addr;
3316 lport = sin->sin_port;
3317#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
t00fcxenc24796c2014-05-06 15:39:13 +00003318 /*
3319 * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3320 * to the proper value.
3321 */
3322 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
3323 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3324 return (error);
3325 }
tuexendd729232011-11-01 23:04:43 +00003326#endif
3327 if (sin->sin_addr.s_addr != INADDR_ANY) {
3328 bindall = 0;
3329 }
3330 break;
3331 }
3332#endif
3333#ifdef INET6
3334 case AF_INET6:
3335 {
3336 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3337 struct sockaddr_in6 *sin6;
3338
3339 sin6 = (struct sockaddr_in6 *)addr;
3340
t00fcxena8a26f62012-09-12 19:24:54 +00003341#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003342 if (addr->sa_len != sizeof(*sin6)) {
3343 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3344 return (EINVAL);
3345 }
3346#endif
tuexendd729232011-11-01 23:04:43 +00003347 lport = sin6->sin6_port;
3348#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
t00fcxenc24796c2014-05-06 15:39:13 +00003349 /*
3350 * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
3351 * to the proper value.
3352 */
3353 if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003354 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
t00fcxenc24796c2014-05-06 15:39:13 +00003355 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3356 return (error);
3357 }
tuexendd729232011-11-01 23:04:43 +00003358#endif
3359 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3360 bindall = 0;
3361#ifdef SCTP_EMBEDDED_V6_SCOPE
3362 /* KAME hack: embed scopeid */
3363#if defined(SCTP_KAME)
3364 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3365 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3366 return (EINVAL);
3367 }
tuexene3851592012-06-01 08:31:45 +00003368#elif defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00003369#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
3370 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003371#else
tuexen9a218b12012-08-04 21:17:58 +00003372 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003373#endif
3374 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3375 return (EINVAL);
3376 }
3377#elif defined(__FreeBSD__)
3378 error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
3379 if (error != 0) {
3380 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3381 return (error);
3382 }
tuexendd729232011-11-01 23:04:43 +00003383#else
3384 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
3385 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3386 return (EINVAL);
3387 }
3388#endif
3389#endif /* SCTP_EMBEDDED_V6_SCOPE */
3390 }
3391#ifndef SCOPEDROUTING
3392 /* this must be cleared for ifa_ifwithaddr() */
3393 sin6->sin6_scope_id = 0;
3394#endif /* SCOPEDROUTING */
3395 break;
3396 }
3397#endif
tuexen09a676e2012-07-08 21:03:18 +00003398#if defined(__Userspace__)
3399 case AF_CONN:
3400 {
3401 struct sockaddr_conn *sconn;
t00fcxenefb47e72012-09-09 08:16:51 +00003402
t00fcxena8a26f62012-09-12 19:24:54 +00003403#ifdef HAVE_SA_LEN
tuexen09a676e2012-07-08 21:03:18 +00003404 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
3405 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3406 return (EINVAL);
3407 }
3408#endif
3409 sconn = (struct sockaddr_conn *)addr;
3410 lport = sconn->sconn_port;
3411 if (sconn->sconn_addr != NULL) {
3412 bindall = 0;
3413 }
3414 break;
3415 }
3416#endif
tuexendd729232011-11-01 23:04:43 +00003417 default:
3418 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
3419 return (EAFNOSUPPORT);
3420 }
3421 }
3422 SCTP_INP_INFO_WLOCK();
3423 SCTP_INP_WLOCK(inp);
3424 /* Setup a vrf_id to be the default for the non-bind-all case. */
t00fcxenc24796c2014-05-06 15:39:13 +00003425 vrf_id = inp->def_vrf_id;
tuexendd729232011-11-01 23:04:43 +00003426
3427 /* increase our count due to the unlock we do */
3428 SCTP_INP_INCR_REF(inp);
3429 if (lport) {
3430 /*
t00fcxenefb47e72012-09-09 08:16:51 +00003431 * Did the caller specify a port? if so we must see if an ep
tuexendd729232011-11-01 23:04:43 +00003432 * already has this one bound.
3433 */
3434 /* got to be root to get at low ports */
3435#if !defined(__Windows__)
3436 if (ntohs(lport) < IPPORT_RESERVED) {
Michael Tuexen4c4cbd92017-09-02 21:38:23 +02003437 if ((p != NULL) && ((error =
tuexendd729232011-11-01 23:04:43 +00003438#ifdef __FreeBSD__
3439#if __FreeBSD_version > 602000
3440 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3441#elif __FreeBSD_version >= 500000
3442 suser_cred(p->td_ucred, 0)
3443#else
3444 suser(p)
3445#endif
3446#elif defined(__APPLE__)
3447 suser(p->p_ucred, &p->p_acflag)
3448#elif defined(__Userspace__) /* must be true to use raw socket */
t00fcxenc24796c2014-05-06 15:39:13 +00003449 1
tuexendd729232011-11-01 23:04:43 +00003450#else
3451 suser(p, 0)
3452#endif
Michael Tuexen4c4cbd92017-09-02 21:38:23 +02003453 ) != 0)) {
tuexendd729232011-11-01 23:04:43 +00003454 SCTP_INP_DECR_REF(inp);
3455 SCTP_INP_WUNLOCK(inp);
3456 SCTP_INP_INFO_WUNLOCK();
3457 return (error);
3458 }
tuexendd729232011-11-01 23:04:43 +00003459 }
tuexendd729232011-11-01 23:04:43 +00003460#endif /* __Windows__ */
3461 SCTP_INP_WUNLOCK(inp);
3462 if (bindall) {
3463#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003464 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003465 vrf_id = inp->m_vrf_ids[i];
3466#else
3467 vrf_id = inp->def_vrf_id;
3468#endif
3469 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3470 if (inp_tmp != NULL) {
3471 /*
3472 * lock guy returned and lower count
3473 * note that we are not bound so
3474 * inp_tmp should NEVER be inp. And
3475 * it is this inp (inp_tmp) that gets
3476 * the reference bump, so we must
3477 * lower it.
3478 */
3479 SCTP_INP_DECR_REF(inp_tmp);
3480 /* unlock info */
3481 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3482 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3483 /* Ok, must be one-2-one and allowing port re-use */
3484 port_reuse_active = 1;
3485 goto continue_anyway;
3486 }
3487 SCTP_INP_DECR_REF(inp);
3488 SCTP_INP_INFO_WUNLOCK();
3489 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3490 return (EADDRINUSE);
3491 }
3492#ifdef SCTP_MVRF
3493 }
3494#endif
3495 } else {
3496 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3497 if (inp_tmp != NULL) {
3498 /*
3499 * lock guy returned and lower count note
3500 * that we are not bound so inp_tmp should
3501 * NEVER be inp. And it is this inp (inp_tmp)
3502 * that gets the reference bump, so we must
3503 * lower it.
3504 */
3505 SCTP_INP_DECR_REF(inp_tmp);
3506 /* unlock info */
3507 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3508 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3509 /* Ok, must be one-2-one and allowing port re-use */
3510 port_reuse_active = 1;
3511 goto continue_anyway;
3512 }
3513 SCTP_INP_DECR_REF(inp);
3514 SCTP_INP_INFO_WUNLOCK();
3515 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3516 return (EADDRINUSE);
3517 }
3518 }
3519 continue_anyway:
3520 SCTP_INP_WLOCK(inp);
3521 if (bindall) {
3522 /* verify that no lport is not used by a singleton */
t00fcxenefb47e72012-09-09 08:16:51 +00003523 if ((port_reuse_active == 0) &&
3524 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
tuexendd729232011-11-01 23:04:43 +00003525 /* Sorry someone already has this one bound */
3526 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3527 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3528 port_reuse_active = 1;
3529 } else {
3530 SCTP_INP_DECR_REF(inp);
3531 SCTP_INP_WUNLOCK(inp);
3532 SCTP_INP_INFO_WUNLOCK();
3533 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3534 return (EADDRINUSE);
3535 }
3536 }
3537 }
3538 } else {
3539 uint16_t first, last, candidate;
t00fcxenc24796c2014-05-06 15:39:13 +00003540 uint16_t count;
tuexendd729232011-11-01 23:04:43 +00003541 int done;
3542
3543#if defined(__Windows__)
3544 first = 1;
3545 last = 0xffff;
3546#else
3547#if defined(__Userspace__)
t00fcxenc24796c2014-05-06 15:39:13 +00003548 /* TODO ensure uid is 0, etc... */
tuexendd729232011-11-01 23:04:43 +00003549#elif defined(__FreeBSD__) || defined(__APPLE__)
t00fcxenc24796c2014-05-06 15:39:13 +00003550 if (ip_inp->inp_flags & INP_HIGHPORT) {
3551 first = MODULE_GLOBAL(ipport_hifirstauto);
3552 last = MODULE_GLOBAL(ipport_hilastauto);
3553 } else if (ip_inp->inp_flags & INP_LOWPORT) {
tuexendd729232011-11-01 23:04:43 +00003554 if (p && (error =
3555#ifdef __FreeBSD__
3556#if __FreeBSD_version > 602000
3557 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3558#elif __FreeBSD_version >= 500000
3559 suser_cred(p->td_ucred, 0)
3560#else
3561 suser(p)
3562#endif
3563#elif defined(__APPLE__)
3564 suser(p->p_ucred, &p->p_acflag)
3565#else
3566 suser(p, 0)
3567#endif
3568 )) {
3569 SCTP_INP_DECR_REF(inp);
3570 SCTP_INP_WUNLOCK(inp);
3571 SCTP_INP_INFO_WUNLOCK();
3572 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3573 return (error);
3574 }
t00fcxenc24796c2014-05-06 15:39:13 +00003575 first = MODULE_GLOBAL(ipport_lowfirstauto);
3576 last = MODULE_GLOBAL(ipport_lowlastauto);
3577 } else {
tuexendd729232011-11-01 23:04:43 +00003578#endif
t00fcxen8dd95b82014-07-11 20:41:28 +00003579 first = MODULE_GLOBAL(ipport_firstauto);
3580 last = MODULE_GLOBAL(ipport_lastauto);
tuexendd729232011-11-01 23:04:43 +00003581#if defined(__FreeBSD__) || defined(__APPLE__)
t00fcxenc24796c2014-05-06 15:39:13 +00003582 }
tuexendd729232011-11-01 23:04:43 +00003583#endif
3584#endif /* __Windows__ */
3585 if (first > last) {
3586 uint16_t temp;
3587
3588 temp = first;
3589 first = last;
3590 last = temp;
3591 }
3592 count = last - first + 1; /* number of candidates */
3593 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3594
3595 done = 0;
3596 while (!done) {
3597#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003598 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003599 if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
3600 break;
3601 }
3602 }
3603 if (i == inp->num_vrfs) {
3604 done = 1;
3605 }
3606#else
3607 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3608 done = 1;
3609 }
3610#endif
3611 if (!done) {
3612 if (--count == 0) {
3613 SCTP_INP_DECR_REF(inp);
3614 SCTP_INP_WUNLOCK(inp);
3615 SCTP_INP_INFO_WUNLOCK();
3616 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3617 return (EADDRINUSE);
3618 }
3619 if (candidate == last)
3620 candidate = first;
3621 else
3622 candidate = candidate + 1;
3623 }
3624 }
3625 lport = htons(candidate);
3626 }
3627 SCTP_INP_DECR_REF(inp);
3628 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3629 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3630 /*
3631 * this really should not happen. The guy did a non-blocking
3632 * bind and then did a close at the same time.
3633 */
3634 SCTP_INP_WUNLOCK(inp);
3635 SCTP_INP_INFO_WUNLOCK();
3636 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3637 return (EINVAL);
3638 }
3639 /* ok we look clear to give out this port, so lets setup the binding */
3640 if (bindall) {
3641 /* binding to all addresses, so just set in the proper flags */
3642 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3643 /* set the automatic addr changes from kernel flag */
3644 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3645 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3646 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3647 } else {
3648 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3649 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3650 }
3651 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3652 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3653 } else {
3654 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3655 }
3656 /* set the automatic mobility_base from kernel
3657 flag (by micchie)
3658 */
3659 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3660 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3661 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003662 } else {
tuexendd729232011-11-01 23:04:43 +00003663 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3664 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3665 }
3666 /* set the automatic mobility_fasthandoff from kernel
3667 flag (by micchie)
3668 */
3669 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3670 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3671 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003672 } else {
tuexendd729232011-11-01 23:04:43 +00003673 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3674 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3675 }
3676 } else {
3677 /*
3678 * bind specific, make sure flags is off and add a new
3679 * address structure to the sctp_addr_list inside the ep
3680 * structure.
3681 *
3682 * We will need to allocate one and insert it at the head. The
3683 * socketopt call can just insert new addresses in there as
3684 * well. It will also have to do the embed scope kame hack
3685 * too (before adding).
3686 */
3687 struct sctp_ifa *ifa;
t00fcxen05f07c02014-09-07 10:15:26 +00003688 union sctp_sockstore store;
tuexendd729232011-11-01 23:04:43 +00003689
t00fcxen05f07c02014-09-07 10:15:26 +00003690 memset(&store, 0, sizeof(store));
tuexendd729232011-11-01 23:04:43 +00003691 switch (addr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00003692#ifdef INET
tuexendd729232011-11-01 23:04:43 +00003693 case AF_INET:
t00fcxen05f07c02014-09-07 10:15:26 +00003694 memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
3695 store.sin.sin_port = 0;
tuexendd729232011-11-01 23:04:43 +00003696 break;
tuexen310f1bc2012-07-15 11:20:56 +00003697#endif
3698#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00003699 case AF_INET6:
t00fcxen05f07c02014-09-07 10:15:26 +00003700 memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
3701 store.sin6.sin6_port = 0;
tuexendd729232011-11-01 23:04:43 +00003702 break;
tuexen310f1bc2012-07-15 11:20:56 +00003703#endif
tuexencc2b1422012-07-10 21:50:32 +00003704#if defined(__Userspace__)
3705 case AF_CONN:
t00fcxen05f07c02014-09-07 10:15:26 +00003706 memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn));
3707 store.sconn.sconn_port = 0;
tuexencc2b1422012-07-10 21:50:32 +00003708 break;
tuexencc2b1422012-07-10 21:50:32 +00003709#endif
tuexendd729232011-11-01 23:04:43 +00003710 default:
3711 break;
3712 }
3713 /*
3714 * first find the interface with the bound address need to
3715 * zero out the port to find the address! yuck! can't do
3716 * this earlier since need port for sctp_pcb_findep()
3717 */
t00fcxenefb47e72012-09-09 08:16:51 +00003718 if (sctp_ifap != NULL) {
tuexendd729232011-11-01 23:04:43 +00003719 ifa = sctp_ifap;
t00fcxenefb47e72012-09-09 08:16:51 +00003720 } else {
tuexendd729232011-11-01 23:04:43 +00003721 /* Note for BSD we hit here always other
3722 * O/S's will pass things in via the
Michael Tuexen3b4263d2020-06-06 19:52:50 +02003723 * sctp_ifap argument.
tuexendd729232011-11-01 23:04:43 +00003724 */
t00fcxen05f07c02014-09-07 10:15:26 +00003725 ifa = sctp_find_ifa_by_addr(&store.sa,
tuexendd729232011-11-01 23:04:43 +00003726 vrf_id, SCTP_ADDR_NOT_LOCKED);
3727 }
3728 if (ifa == NULL) {
3729 /* Can't find an interface with that address */
3730 SCTP_INP_WUNLOCK(inp);
3731 SCTP_INP_INFO_WUNLOCK();
3732 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3733 return (EADDRNOTAVAIL);
3734 }
3735#ifdef INET6
3736 if (addr->sa_family == AF_INET6) {
3737 /* GAK, more FIXME IFA lock? */
3738 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3739 /* Can't bind a non-existent addr. */
3740 SCTP_INP_WUNLOCK(inp);
3741 SCTP_INP_INFO_WUNLOCK();
3742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3743 return (EINVAL);
3744 }
3745 }
3746#endif
3747 /* we're not bound all */
3748 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3749 /* allow bindx() to send ASCONF's for binding changes */
3750 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3751 /* clear automatic addr changes from kernel flag */
3752 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3753
3754 /* add this address to the endpoint list */
3755 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3756 if (error != 0) {
3757 SCTP_INP_WUNLOCK(inp);
3758 SCTP_INP_INFO_WUNLOCK();
3759 return (error);
3760 }
3761 inp->laddr_count++;
3762 }
3763 /* find the bucket */
tuexen98456cf2012-04-19 15:37:07 +00003764 if (port_reuse_active) {
tuexendd729232011-11-01 23:04:43 +00003765 /* Put it into tcp 1-2-1 hash */
3766 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3767 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
t00fcxenc24796c2014-05-06 15:39:13 +00003768 } else {
tuexendd729232011-11-01 23:04:43 +00003769 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3770 }
3771 /* put it in the bucket */
3772 LIST_INSERT_HEAD(head, inp, sctp_hash);
3773 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 +00003774 (void *)head, ntohs(lport), port_reuse_active);
tuexendd729232011-11-01 23:04:43 +00003775 /* set in the port */
3776 inp->sctp_lport = lport;
3777
3778 /* turn off just the unbound flag */
3779 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3780 SCTP_INP_WUNLOCK(inp);
3781 SCTP_INP_INFO_WUNLOCK();
3782 return (0);
3783}
3784
3785
3786static void
3787sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3788{
3789 struct sctp_iterator *it, *nit;
3790
3791 /*
3792 * We enter with the only the ITERATOR_LOCK in place and a write
3793 * lock on the inp_info stuff.
3794 */
3795 it = sctp_it_ctl.cur_it;
3796#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3797 if (it && (it->vn != curvnet)) {
3798 /* Its not looking at our VNET */
3799 return;
3800 }
3801#endif
3802 if (it && (it->inp == inp)) {
3803 /*
3804 * This is tricky and we hold the iterator lock,
3805 * but when it returns and gets the lock (when we
3806 * release it) the iterator will try to operate on
3807 * inp. We need to stop that from happening. But
3808 * of course the iterator has a reference on the
3809 * stcb and inp. We can mark it and it will stop.
3810 *
3811 * If its a single iterator situation, we
3812 * set the end iterator flag. Otherwise
3813 * we set the iterator to go to the next inp.
3814 *
3815 */
3816 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3817 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3818 } else {
3819 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3820 }
3821 }
3822 /* Now go through and remove any single reference to
3823 * our inp that may be still pending on the list
3824 */
3825 SCTP_IPI_ITERATOR_WQ_LOCK();
3826 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3827#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3828 if (it->vn != curvnet) {
3829 continue;
tuexen09631162012-04-18 11:23:02 +00003830 }
tuexendd729232011-11-01 23:04:43 +00003831#endif
3832 if (it->inp == inp) {
3833 /* This one points to me is it inp specific? */
3834 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3835 /* Remove and free this one */
3836 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3837 it, sctp_nxt_itr);
3838 if (it->function_atend != NULL) {
3839 (*it->function_atend) (it->pointer, it->val);
3840 }
3841 SCTP_FREE(it, SCTP_M_ITER);
3842 } else {
3843 it->inp = LIST_NEXT(it->inp, sctp_list);
3844 if (it->inp) {
3845 SCTP_INP_INCR_REF(it->inp);
3846 }
3847 }
3848 /* When its put in the refcnt is incremented so decr it */
3849 SCTP_INP_DECR_REF(inp);
3850 }
3851 }
3852 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3853}
3854
3855/* release sctp_inpcb unbind the port */
3856void
3857sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3858{
3859 /*
3860 * Here we free a endpoint. We must find it (if it is in the Hash
3861 * table) and remove it from there. Then we must also find it in the
3862 * overall list and remove it from there. After all removals are
3863 * complete then any timer has to be stopped. Then start the actual
3864 * freeing. a) Any local lists. b) Any associations. c) The hash of
3865 * all associations. d) finally the ep itself.
3866 */
tuexendd729232011-11-01 23:04:43 +00003867 struct sctp_tcb *asoc, *nasoc;
3868 struct sctp_laddr *laddr, *nladdr;
3869 struct inpcb *ip_pcb;
3870 struct socket *so;
tuexen63fc0bb2011-12-27 12:24:52 +00003871 int being_refed = 0;
tuexendd729232011-11-01 23:04:43 +00003872 struct sctp_queued_to_read *sq, *nsq;
Michael Tuexen3b4263d2020-06-06 19:52:50 +02003873#if !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00003874#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
3875 sctp_rtentry_t *rt;
3876#endif
3877#endif
3878 int cnt;
3879 sctp_sharedkey_t *shared_key, *nshared_key;
3880
3881
3882#if defined(__APPLE__)
3883 sctp_lock_assert(SCTP_INP_SO(inp));
3884#endif
3885#ifdef SCTP_LOG_CLOSING
3886 sctp_log_closing(inp, NULL, 0);
3887#endif
3888 SCTP_ITERATOR_LOCK();
3889 /* mark any iterators on the list or being processed */
3890 sctp_iterator_inp_being_freed(inp);
3891 SCTP_ITERATOR_UNLOCK();
3892 so = inp->sctp_socket;
3893 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3894 /* been here before.. eeks.. get out of here */
3895 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3896#ifdef SCTP_LOG_CLOSING
3897 sctp_log_closing(inp, NULL, 1);
3898#endif
3899 return;
3900 }
3901 SCTP_ASOC_CREATE_LOCK(inp);
3902 SCTP_INP_INFO_WLOCK();
3903
3904 SCTP_INP_WLOCK(inp);
3905 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3906 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3907 /* socket is gone, so no more wakeups allowed */
3908 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3909 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3910 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3911
3912 }
3913 /* First time through we have the socket lock, after that no more. */
3914 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
Michael Tuexen390beeb2020-02-04 15:05:45 +01003915 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
tuexendd729232011-11-01 23:04:43 +00003916
3917 if (inp->control) {
3918 sctp_m_freem(inp->control);
3919 inp->control = NULL;
3920 }
3921 if (inp->pkt) {
3922 sctp_m_freem(inp->pkt);
3923 inp->pkt = NULL;
3924 }
tuexendd729232011-11-01 23:04:43 +00003925 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3926 * here but I will be nice :> (i.e.
3927 * ip_pcb = ep;) */
3928 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3929 int cnt_in_sd;
3930
3931 cnt_in_sd = 0;
3932 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3933 SCTP_TCB_LOCK(asoc);
3934 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3935 /* Skip guys being freed */
3936 cnt_in_sd++;
3937 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3938 /*
3939 * Special case - we did not start a kill
3940 * timer on the asoc due to it was not
3941 * closed. So go ahead and start it now.
3942 */
Michael Tuexen348a36c2018-08-13 16:24:47 +02003943 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00003944 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3945 }
3946 SCTP_TCB_UNLOCK(asoc);
3947 continue;
3948 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02003949 if (((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3950 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
tuexendd729232011-11-01 23:04:43 +00003951 (asoc->asoc.total_output_queue_size == 0)) {
3952 /* If we have data in queue, we don't want to just
3953 * free since the app may have done, send()/close
3954 * or connect/send/close. And it wants the data
3955 * to get across first.
3956 */
3957 /* Just abandon things in the front states */
3958 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00003959 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
tuexendd729232011-11-01 23:04:43 +00003960 cnt_in_sd++;
3961 }
3962 continue;
3963 }
3964 /* Disconnect the socket please */
3965 asoc->sctp_socket = NULL;
Michael Tuexen348a36c2018-08-13 16:24:47 +02003966 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET);
tuexendd729232011-11-01 23:04:43 +00003967 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3968 (asoc->asoc.control_pdapi) ||
3969 (asoc->asoc.size_on_all_streams > 0) ||
tuexen63fc0bb2011-12-27 12:24:52 +00003970 (so && (so->so_rcv.sb_cc > 0))) {
tuexendd729232011-11-01 23:04:43 +00003971 /* Left with Data unread */
3972 struct mbuf *op_err;
3973
t00fcxen08f9ff92014-03-16 13:38:54 +00003974 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00003975 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
tuexendd729232011-11-01 23:04:43 +00003976 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3977 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen348a36c2018-08-13 16:24:47 +02003978 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3979 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00003980 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3981 }
3982 if (sctp_free_assoc(inp, asoc,
t00fcxen0057a6d2015-05-28 16:42:49 +00003983 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
tuexendd729232011-11-01 23:04:43 +00003984 cnt_in_sd++;
3985 }
3986 continue;
3987 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3988 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
t00fcxen28611aa2012-09-23 07:45:40 +00003989 (asoc->asoc.stream_queue_cnt == 0)) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02003990 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
tuexendd729232011-11-01 23:04:43 +00003991 goto abort_anyway;
3992 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02003993 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3994 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
tuexendd729232011-11-01 23:04:43 +00003995 struct sctp_nets *netp;
3996
t00fcxend0ad16b2013-02-09 18:34:24 +00003997 /*
3998 * there is nothing queued to send,
3999 * so I send shutdown
4000 */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004001 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4002 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004003 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4004 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004005 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
t00fcxend0ad16b2013-02-09 18:34:24 +00004006 sctp_stop_timers_for_shutdown(asoc);
tuexendd729232011-11-01 23:04:43 +00004007 if (asoc->asoc.alternate) {
4008 netp = asoc->asoc.alternate;
4009 } else {
4010 netp = asoc->asoc.primary_destination;
4011 }
tuexendd729232011-11-01 23:04:43 +00004012 sctp_send_shutdown(asoc, netp);
tuexendd729232011-11-01 23:04:43 +00004013 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
4014 netp);
Michael Tuexend07a5f22020-03-19 23:34:46 +01004015 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
tuexendd729232011-11-01 23:04:43 +00004016 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
4017 }
4018 } else {
4019 /* mark into shutdown pending */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004020 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
Michael Tuexend07a5f22020-03-19 23:34:46 +01004021 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02004022 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02004023 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT);
tuexendd729232011-11-01 23:04:43 +00004024 }
4025 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
4026 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
4027 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4028 struct mbuf *op_err;
4029 abort_anyway:
t00fcxen08f9ff92014-03-16 13:38:54 +00004030 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00004031 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
tuexendd729232011-11-01 23:04:43 +00004032 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4033 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen348a36c2018-08-13 16:24:47 +02004034 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4035 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004036 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4037 }
4038 if (sctp_free_assoc(inp, asoc,
4039 SCTP_PCBFREE_NOFORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00004040 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
tuexendd729232011-11-01 23:04:43 +00004041 cnt_in_sd++;
4042 }
4043 continue;
4044 } else {
4045 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
4046 }
4047 }
4048 cnt_in_sd++;
4049 SCTP_TCB_UNLOCK(asoc);
4050 }
4051 /* now is there some left in our SHUTDOWN state? */
4052 if (cnt_in_sd) {
4053#ifdef SCTP_LOG_CLOSING
4054 sctp_log_closing(inp, NULL, 2);
4055#endif
4056 inp->sctp_socket = NULL;
4057 SCTP_INP_WUNLOCK(inp);
4058 SCTP_ASOC_CREATE_UNLOCK(inp);
4059 SCTP_INP_INFO_WUNLOCK();
4060 return;
4061 }
4062 }
4063 inp->sctp_socket = NULL;
4064 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
4065 SCTP_PCB_FLAGS_UNBOUND) {
4066 /*
4067 * ok, this guy has been bound. It's port is
4068 * somewhere in the SCTP_BASE_INFO(hash table). Remove
4069 * it!
4070 */
4071 LIST_REMOVE(inp, sctp_hash);
4072 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
4073 }
4074
4075 /* If there is a timer running to kill us,
4076 * forget it, since it may have a contest
4077 * on the INP lock.. which would cause us
4078 * to die ...
4079 */
4080 cnt = 0;
4081 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
4082 SCTP_TCB_LOCK(asoc);
4083 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4084 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02004085 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
t00fcxen28611aa2012-09-23 07:45:40 +00004086 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
tuexendd729232011-11-01 23:04:43 +00004087 }
Michael Tuexen61f66832020-02-09 23:15:19 +01004088 cnt++;
tuexendd729232011-11-01 23:04:43 +00004089 SCTP_TCB_UNLOCK(asoc);
4090 continue;
4091 }
4092 /* Free associations that are NOT killing us */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004093 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
tuexendd729232011-11-01 23:04:43 +00004094 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
4095 struct mbuf *op_err;
t00fcxen28611aa2012-09-23 07:45:40 +00004096
t00fcxen08f9ff92014-03-16 13:38:54 +00004097 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00004098 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
tuexendd729232011-11-01 23:04:43 +00004099 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4100 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4101 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4102 cnt++;
4103 SCTP_TCB_UNLOCK(asoc);
4104 continue;
4105 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004106 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4107 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004108 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4109 }
t00fcxen0057a6d2015-05-28 16:42:49 +00004110 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE,
4111 SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
tuexendd729232011-11-01 23:04:43 +00004112 cnt++;
4113 }
4114 }
4115 if (cnt) {
4116 /* Ok we have someone out there that will kill us */
tuexendd729232011-11-01 23:04:43 +00004117#ifdef SCTP_LOG_CLOSING
4118 sctp_log_closing(inp, NULL, 3);
4119#endif
4120 SCTP_INP_WUNLOCK(inp);
4121 SCTP_ASOC_CREATE_UNLOCK(inp);
4122 SCTP_INP_INFO_WUNLOCK();
4123 return;
4124 }
4125 if (SCTP_INP_LOCK_CONTENDED(inp))
4126 being_refed++;
4127 if (SCTP_INP_READ_CONTENDED(inp))
4128 being_refed++;
tuexen63fc0bb2011-12-27 12:24:52 +00004129 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
tuexendd729232011-11-01 23:04:43 +00004130 being_refed++;
4131
tuexen63fc0bb2011-12-27 12:24:52 +00004132 if ((inp->refcount) ||
t00fcxen28611aa2012-09-23 07:45:40 +00004133 (being_refed) ||
4134 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
tuexendd729232011-11-01 23:04:43 +00004135#ifdef SCTP_LOG_CLOSING
4136 sctp_log_closing(inp, NULL, 4);
4137#endif
4138 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4139 SCTP_INP_WUNLOCK(inp);
4140 SCTP_ASOC_CREATE_UNLOCK(inp);
4141 SCTP_INP_INFO_WUNLOCK();
4142 return;
4143 }
4144 inp->sctp_ep.signature_change.type = 0;
4145 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4146 /* Remove it from the list .. last thing we need a
4147 * lock for.
4148 */
4149 LIST_REMOVE(inp, sctp_list);
4150 SCTP_INP_WUNLOCK(inp);
4151 SCTP_ASOC_CREATE_UNLOCK(inp);
4152 SCTP_INP_INFO_WUNLOCK();
4153 /* Now we release all locks. Since this INP
4154 * cannot be found anymore except possibly by the
4155 * kill timer that might be running. We call
4156 * the drain function here. It should hit the case
4157 * were it sees the ACTIVE flag cleared and exit
4158 * out freeing us to proceed and destroy everything.
4159 */
4160 if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
4161 (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
4162 } else {
4163 /* Probably un-needed */
4164 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4165 }
4166
4167#ifdef SCTP_LOG_CLOSING
4168 sctp_log_closing(inp, NULL, 5);
4169#endif
4170
Michael Tuexen3b4263d2020-06-06 19:52:50 +02004171#if !(defined(__Windows__) || defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004172#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4173 rt = ip_pcb->inp_route.ro_rt;
4174#endif
4175#endif
4176
tuexendd729232011-11-01 23:04:43 +00004177 if ((inp->sctp_asocidhash) != NULL) {
4178 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4179 inp->sctp_asocidhash = NULL;
4180 }
4181 /*sa_ignore FREED_MEMORY*/
4182 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4183 /* Its only abandoned if it had data left */
4184 if (sq->length)
4185 SCTP_STAT_INCR(sctps_left_abandon);
4186
4187 TAILQ_REMOVE(&inp->read_queue, sq, next);
4188 sctp_free_remote_addr(sq->whoFrom);
4189 if (so)
4190 so->so_rcv.sb_cc -= sq->length;
4191 if (sq->data) {
4192 sctp_m_freem(sq->data);
4193 sq->data = NULL;
4194 }
4195 /*
4196 * no need to free the net count, since at this point all
4197 * assoc's are gone.
4198 */
Michael Tuexene5001952016-04-17 19:25:27 +02004199 sctp_free_a_readq(NULL, sq);
tuexendd729232011-11-01 23:04:43 +00004200 }
4201 /* Now the sctp_pcb things */
4202 /*
4203 * free each asoc if it is not already closed/free. we can't use the
4204 * macro here since le_next will get freed as part of the
4205 * sctp_free_assoc() call.
4206 */
tuexendd729232011-11-01 23:04:43 +00004207 if (ip_pcb->inp_options) {
4208 (void)sctp_m_free(ip_pcb->inp_options);
4209 ip_pcb->inp_options = 0;
4210 }
tuexendd729232011-11-01 23:04:43 +00004211
Michael Tuexen3b4263d2020-06-06 19:52:50 +02004212#if !(defined(__Windows__) || defined(__Userspace__))
tuexendd729232011-11-01 23:04:43 +00004213#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4214 if (rt) {
4215 RTFREE(rt);
4216 ip_pcb->inp_route.ro_rt = 0;
4217 }
4218#endif
tuexen5fc22842012-02-11 13:32:19 +00004219#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00004220#ifdef INET
4221 if (ip_pcb->inp_moptions) {
4222 inp_freemoptions(ip_pcb->inp_moptions);
4223 ip_pcb->inp_moptions = 0;
4224 }
4225#endif
4226#endif
4227#endif
4228
4229#ifdef INET6
Michael Tuexen3b4263d2020-06-06 19:52:50 +02004230#if !(defined(__Windows__) || defined(__Userspace__))
t00fcxen4c76a912012-09-14 14:31:21 +00004231#if defined(__FreeBSD__) || defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +00004232 if (ip_pcb->inp_vflag & INP_IPV6) {
t00fcxen4c76a912012-09-14 14:31:21 +00004233#else
4234 if (inp->inp_vflag & INP_IPV6) {
tuexendd729232011-11-01 23:04:43 +00004235#endif
Michael Tuexen801a64b2019-08-05 15:33:37 +02004236 ip6_freepcbopts(ip_pcb->in6p_outputopts);
tuexendd729232011-11-01 23:04:43 +00004237 }
t00fcxen4c76a912012-09-14 14:31:21 +00004238#endif
tuexendd729232011-11-01 23:04:43 +00004239#endif /* INET6 */
4240#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
4241 inp->inp_vflag = 0;
4242#else
4243 ip_pcb->inp_vflag = 0;
4244#endif
4245 /* free up authentication fields */
4246 if (inp->sctp_ep.local_auth_chunks != NULL)
4247 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4248 if (inp->sctp_ep.local_hmacs != NULL)
4249 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4250
4251 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4252 LIST_REMOVE(shared_key, next);
4253 sctp_free_sharedkey(shared_key);
4254 /*sa_ignore FREED_MEMORY*/
4255 }
4256
4257#if defined(__APPLE__)
4258 inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
tuexen6d01b942012-05-04 18:15:17 +00004259 if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
tuexen49fdcc82012-05-04 10:13:12 +00004260#ifdef INVARIANTS
t00fcxen8fca7ff2012-09-05 18:58:42 +00004261 panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen6d01b942012-05-04 18:15:17 +00004262#else
t00fcxen8fca7ff2012-09-05 18:58:42 +00004263 SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen49fdcc82012-05-04 10:13:12 +00004264#endif
tuexen6d01b942012-05-04 18:15:17 +00004265 }
tuexendd729232011-11-01 23:04:43 +00004266 inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4267#endif
4268 /*
4269 * if we have an address list the following will free the list of
4270 * ifaddr's that are set into this ep. Again macro limitations here,
4271 * since the LIST_FOREACH could be a bad idea.
4272 */
4273 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4274 sctp_remove_laddr(laddr);
4275 }
4276
4277#ifdef SCTP_TRACK_FREED_ASOCS
4278 /* TEMP CODE */
4279 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
4280 LIST_REMOVE(asoc, sctp_tcblist);
4281 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
4282 SCTP_DECR_ASOC_COUNT();
4283 }
4284 /* *** END TEMP CODE ****/
4285#endif
4286#ifdef SCTP_MVRF
4287 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4288#endif
4289 /* Now lets see about freeing the EP hash table. */
4290 if (inp->sctp_tcbhash != NULL) {
4291 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4292 inp->sctp_tcbhash = NULL;
4293 }
4294 /* Now we must put the ep memory back into the zone pool */
4295#if defined(__FreeBSD__)
t00fcxen326aa4b2014-06-20 13:42:13 +00004296 crfree(inp->ip_inp.inp.inp_cred);
tuexendd729232011-11-01 23:04:43 +00004297 INP_LOCK_DESTROY(&inp->ip_inp.inp);
4298#endif
4299 SCTP_INP_LOCK_DESTROY(inp);
4300 SCTP_INP_READ_DESTROY(inp);
4301 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
4302#if !defined(__APPLE__)
4303 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4304 SCTP_DECR_EP_COUNT();
4305#else
4306 /* For Tiger, we will do this later... */
4307#endif
4308}
4309
4310
4311struct sctp_nets *
4312sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4313{
4314 struct sctp_nets *net;
4315 /* locate the address */
4316 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4317 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4318 return (net);
4319 }
4320 return (NULL);
4321}
4322
4323
4324int
4325sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4326{
tuexendd729232011-11-01 23:04:43 +00004327 struct sctp_ifa *sctp_ifa;
4328 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4329 if (sctp_ifa) {
4330 return (1);
4331 } else {
4332 return (0);
4333 }
tuexendd729232011-11-01 23:04:43 +00004334}
4335
4336/*
4337 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4338 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4339 * stats of stuff.
4340 */
4341int
4342sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
Michael Tuexen27574422016-04-30 16:35:29 +02004343 struct sctp_nets **netp, uint16_t port, int set_scope, int from)
tuexendd729232011-11-01 23:04:43 +00004344{
4345 /*
4346 * The following is redundant to the same lines in the
4347 * sctp_aloc_assoc() but is needed since others call the add
4348 * address function
4349 */
4350 struct sctp_nets *net, *netfirst;
4351 int addr_inscope;
4352
4353 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4354 from);
4355 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4356
4357 netfirst = sctp_findnet(stcb, newaddr);
4358 if (netfirst) {
4359 /*
4360 * Lie and return ok, we don't want to make the association
4361 * go away for this behavior. It will happen in the TCP
4362 * model in a connected socket. It does not reach the hash
4363 * table until after the association is built so it can't be
4364 * found. Mark as reachable, since the initial creation will
4365 * have been cleared and the NOT_IN_ASSOC flag will have
4366 * been added... and we don't want to end up removing it
4367 * back out.
4368 */
4369 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4370 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4371 SCTP_ADDR_UNCONFIRMED);
4372 } else {
4373 netfirst->dest_state = SCTP_ADDR_REACHABLE;
4374 }
4375
4376 return (0);
4377 }
4378 addr_inscope = 1;
4379 switch (newaddr->sa_family) {
4380#ifdef INET
4381 case AF_INET:
4382 {
4383 struct sockaddr_in *sin;
4384
4385 sin = (struct sockaddr_in *)newaddr;
4386 if (sin->sin_addr.s_addr == 0) {
4387 /* Invalid address */
4388 return (-1);
4389 }
Michael Tuexend0853302017-07-19 17:01:31 +02004390 /* zero out the zero area */
tuexendd729232011-11-01 23:04:43 +00004391 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4392
4393 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004394#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00004395 sin->sin_len = sizeof(struct sockaddr_in);
4396#endif
4397 if (set_scope) {
tuexendd729232011-11-01 23:04:43 +00004398 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004399 stcb->asoc.scope.ipv4_local_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004400 }
tuexendd729232011-11-01 23:04:43 +00004401 } else {
4402 /* Validate the address is in scope */
4403 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004404 (stcb->asoc.scope.ipv4_local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004405 addr_inscope = 0;
4406 }
4407 }
4408 break;
4409 }
4410#endif
4411#ifdef INET6
4412 case AF_INET6:
4413 {
4414 struct sockaddr_in6 *sin6;
4415
4416 sin6 = (struct sockaddr_in6 *)newaddr;
4417 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4418 /* Invalid address */
4419 return (-1);
4420 }
4421 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004422#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00004423 sin6->sin6_len = sizeof(struct sockaddr_in6);
4424#endif
4425 if (set_scope) {
4426 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004427 stcb->asoc.scope.loopback_scope = 1;
4428 stcb->asoc.scope.local_scope = 0;
4429 stcb->asoc.scope.ipv4_local_scope = 1;
4430 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004431 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4432 /*
4433 * If the new destination is a LINK_LOCAL we
4434 * must have common site scope. Don't set
4435 * the local scope since we may not share
4436 * all links, only loopback can do this.
4437 * Links on the local network would also be
4438 * on our private network for v4 too.
4439 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004440 stcb->asoc.scope.ipv4_local_scope = 1;
4441 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004442 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4443 /*
4444 * If the new destination is SITE_LOCAL then
4445 * we must have site scope in common.
4446 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004447 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004448 }
4449 } else {
4450 /* Validate the address is in scope */
4451 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004452 (stcb->asoc.scope.loopback_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004453 addr_inscope = 0;
4454 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004455 (stcb->asoc.scope.local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004456 addr_inscope = 0;
4457 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004458 (stcb->asoc.scope.site_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004459 addr_inscope = 0;
4460 }
4461 }
4462 break;
4463 }
4464#endif
tuexencc2b1422012-07-10 21:50:32 +00004465#if defined(__Userspace__)
4466 case AF_CONN:
4467 {
4468 struct sockaddr_conn *sconn;
4469
4470 sconn = (struct sockaddr_conn *)newaddr;
4471 if (sconn->sconn_addr == NULL) {
4472 /* Invalid address */
4473 return (-1);
4474 }
t00fcxen8fbaee32012-09-04 16:41:39 +00004475#ifdef HAVE_SCONN_LEN
tuexencc2b1422012-07-10 21:50:32 +00004476 sconn->sconn_len = sizeof(struct sockaddr_conn);
4477#endif
4478 break;
4479 }
4480#endif
tuexendd729232011-11-01 23:04:43 +00004481 default:
4482 /* not supported family type */
4483 return (-1);
4484 }
4485 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4486 if (net == NULL) {
4487 return (-1);
4488 }
4489 SCTP_INCR_RADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02004490 memset(net, 0, sizeof(struct sctp_nets));
tuexendd729232011-11-01 23:04:43 +00004491 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
t00fcxen907930b2012-09-12 19:43:22 +00004492#ifdef HAVE_SA_LEN
4493 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
4494#endif
tuexendd729232011-11-01 23:04:43 +00004495 switch (newaddr->sa_family) {
4496#ifdef INET
4497 case AF_INET:
t00fcxen907930b2012-09-12 19:43:22 +00004498#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004499 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4500#endif
4501 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4502 break;
4503#endif
4504#ifdef INET6
4505 case AF_INET6:
t00fcxen907930b2012-09-12 19:43:22 +00004506#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004507 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4508#endif
4509 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4510 break;
4511#endif
tuexencc2b1422012-07-10 21:50:32 +00004512#if defined(__Userspace__)
4513 case AF_CONN:
t00fcxen907930b2012-09-12 19:43:22 +00004514#ifndef HAVE_SA_LEN
tuexencc2b1422012-07-10 21:50:32 +00004515 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4516#endif
4517 ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4518 break;
4519#endif
tuexendd729232011-11-01 23:04:43 +00004520 default:
4521 break;
4522 }
4523 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4524 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004525 stcb->asoc.scope.loopback_scope = 1;
4526 stcb->asoc.scope.ipv4_local_scope = 1;
4527 stcb->asoc.scope.local_scope = 0;
4528 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004529 addr_inscope = 1;
4530 }
4531 net->failure_threshold = stcb->asoc.def_net_failure;
4532 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4533 if (addr_inscope == 0) {
4534 net->dest_state = (SCTP_ADDR_REACHABLE |
4535 SCTP_ADDR_OUT_OF_SCOPE);
4536 } else {
4537 if (from == SCTP_ADDR_IS_CONFIRMED)
4538 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4539 net->dest_state = SCTP_ADDR_REACHABLE;
4540 else
4541 net->dest_state = SCTP_ADDR_REACHABLE |
4542 SCTP_ADDR_UNCONFIRMED;
4543 }
4544 /* We set this to 0, the timer code knows that
4545 * this means its an initial value
4546 */
4547 net->rto_needed = 1;
t00fcxen8dd95b82014-07-11 20:41:28 +00004548 net->RTO = 0;
tuexendd729232011-11-01 23:04:43 +00004549 net->RTO_measured = 0;
4550 stcb->asoc.numnets++;
tuexen9d7b2072011-11-20 16:35:17 +00004551 net->ref_count = 1;
tuexendd729232011-11-01 23:04:43 +00004552 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
Michael Tuexen27574422016-04-30 16:35:29 +02004553 net->port = port;
tuexendd729232011-11-01 23:04:43 +00004554 net->dscp = stcb->asoc.default_dscp;
4555#ifdef INET6
4556 net->flowlabel = stcb->asoc.default_flowlabel;
4557#endif
4558 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4559 net->dest_state |= SCTP_ADDR_NOHB;
4560 } else {
4561 net->dest_state &= ~SCTP_ADDR_NOHB;
4562 }
4563 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4564 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4565 } else {
4566 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4567 }
4568 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4569 /* Init the timer structure */
4570 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4571 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4572 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4573
4574 /* Now generate a route for this guy */
4575#ifdef INET6
4576#ifdef SCTP_EMBEDDED_V6_SCOPE
4577 /* KAME hack: embed scopeid */
4578 if (newaddr->sa_family == AF_INET6) {
4579 struct sockaddr_in6 *sin6;
4580
4581 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
tuexene3851592012-06-01 08:31:45 +00004582#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00004583#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4584 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
tuexendd729232011-11-01 23:04:43 +00004585#else
tuexen9a218b12012-08-04 21:17:58 +00004586 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
tuexendd729232011-11-01 23:04:43 +00004587#endif
4588#elif defined(SCTP_KAME)
4589 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4590#else
4591 (void)in6_embedscope(&sin6->sin6_addr, sin6);
4592#endif
4593#ifndef SCOPEDROUTING
4594 sin6->sin6_scope_id = 0;
4595#endif
4596 }
4597#endif /* SCTP_EMBEDDED_V6_SCOPE */
4598#endif
t00fcxena51e4462015-06-17 15:53:23 +00004599 SCTP_RTALLOC((sctp_route_t *)&net->ro,
4600 stcb->asoc.vrf_id,
4601 stcb->sctp_ep->fibnum);
tuexendd729232011-11-01 23:04:43 +00004602
t00fcxen8b6ef092014-07-11 17:37:40 +00004603 net->src_addr_selected = 0;
Michael Tuexend900e5f2017-06-23 10:52:43 +02004604#if !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004605 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4606 /* Get source address */
4607 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004608 stcb,
4609 (sctp_route_t *)&net->ro,
4610 net,
4611 0,
4612 stcb->asoc.vrf_id);
Michael Tuexende0630d2017-11-03 22:04:01 +01004613 if (stcb->asoc.default_mtu > 0) {
4614 net->mtu = stcb->asoc.default_mtu;
4615 switch (net->ro._l_addr.sa.sa_family) {
4616#ifdef INET
4617 case AF_INET:
4618 net->mtu += SCTP_MIN_V4_OVERHEAD;
4619 break;
4620#endif
4621#ifdef INET6
4622 case AF_INET6:
4623 net->mtu += SCTP_MIN_OVERHEAD;
4624 break;
4625#endif
4626#if defined(__Userspace__)
4627 case AF_CONN:
4628 net->mtu += sizeof(struct sctphdr);
4629 break;
4630#endif
4631 default:
4632 break;
4633 }
4634#if defined(INET) || defined(INET6)
4635 if (net->port) {
4636 net->mtu += (uint32_t)sizeof(struct udphdr);
4637 }
4638#endif
4639 } else if (net->ro._s_addr != NULL) {
Michael Tuexend900e5f2017-06-23 10:52:43 +02004640 uint32_t imtu, rmtu, hcmtu;
4641
t00fcxen8b6ef092014-07-11 17:37:40 +00004642 net->src_addr_selected = 1;
4643 /* Now get the interface MTU */
4644 if (net->ro._s_addr->ifn_p != NULL) {
Michael Tuexend900e5f2017-06-23 10:52:43 +02004645 imtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
4646 } else {
4647 imtu = 0;
t00fcxen8b6ef092014-07-11 17:37:40 +00004648 }
Michael Tuexend900e5f2017-06-23 10:52:43 +02004649#if defined(__FreeBSD__)
Michael Tuexena0515422020-05-18 00:47:04 +02004650 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 +02004651 hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
4652#else
Michael Tuexena0515422020-05-18 00:47:04 +02004653 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 +02004654 hcmtu = 0;
4655#endif
4656 net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
Michael Tuexena0515422020-05-18 00:47:04 +02004657#if defined(__FreeBSD__)
4658#else
tuexendd729232011-11-01 23:04:43 +00004659 if (rmtu == 0) {
4660 /* Start things off to match mtu of interface please. */
4661 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004662 net->ro.ro_rt, net->mtu);
tuexendd729232011-11-01 23:04:43 +00004663 }
Michael Tuexena0515422020-05-18 00:47:04 +02004664#endif
t00fcxen8b6ef092014-07-11 17:37:40 +00004665 }
tuexen09631162012-04-18 11:23:02 +00004666 }
t00fcxen6d60eb02013-08-30 07:37:01 +00004667#endif
tuexene81177b2011-11-15 20:48:08 +00004668 if (net->mtu == 0) {
Michael Tuexende0630d2017-11-03 22:04:01 +01004669 if (stcb->asoc.default_mtu > 0) {
4670 net->mtu = stcb->asoc.default_mtu;
4671 switch (net->ro._l_addr.sa.sa_family) {
tuexene81177b2011-11-15 20:48:08 +00004672#ifdef INET
Michael Tuexende0630d2017-11-03 22:04:01 +01004673 case AF_INET:
4674 net->mtu += SCTP_MIN_V4_OVERHEAD;
4675 break;
tuexene81177b2011-11-15 20:48:08 +00004676#endif
4677#ifdef INET6
Michael Tuexende0630d2017-11-03 22:04:01 +01004678 case AF_INET6:
4679 net->mtu += SCTP_MIN_OVERHEAD;
4680 break;
tuexene81177b2011-11-15 20:48:08 +00004681#endif
tuexencc2b1422012-07-10 21:50:32 +00004682#if defined(__Userspace__)
Michael Tuexende0630d2017-11-03 22:04:01 +01004683 case AF_CONN:
4684 net->mtu += sizeof(struct sctphdr);
4685 break;
tuexencc2b1422012-07-10 21:50:32 +00004686#endif
Michael Tuexende0630d2017-11-03 22:04:01 +01004687 default:
4688 break;
4689 }
4690#if defined(INET) || defined(INET6)
4691 if (net->port) {
4692 net->mtu += (uint32_t)sizeof(struct udphdr);
4693 }
4694#endif
4695 } else {
4696 switch (newaddr->sa_family) {
4697#ifdef INET
4698 case AF_INET:
4699 net->mtu = SCTP_DEFAULT_MTU;
4700 break;
4701#endif
4702#ifdef INET6
4703 case AF_INET6:
4704 net->mtu = 1280;
4705 break;
4706#endif
4707#if defined(__Userspace__)
4708 case AF_CONN:
4709 net->mtu = 1280;
4710 break;
4711#endif
4712 default:
4713 break;
4714 }
tuexendd729232011-11-01 23:04:43 +00004715 }
tuexene81177b2011-11-15 20:48:08 +00004716 }
t00fcxen1ce83bb2014-07-11 07:17:36 +00004717#if defined(INET) || defined(INET6)
tuexene81177b2011-11-15 20:48:08 +00004718 if (net->port) {
4719 net->mtu -= (uint32_t)sizeof(struct udphdr);
4720 }
t00fcxen5ab37c82014-06-30 20:55:44 +00004721#endif
tuexene81177b2011-11-15 20:48:08 +00004722 if (from == SCTP_ALLOC_ASOC) {
4723 stcb->asoc.smallest_mtu = net->mtu;
4724 }
4725 if (stcb->asoc.smallest_mtu > net->mtu) {
Michael Tuexen923f7352016-05-01 23:55:35 +02004726 sctp_pathmtu_adjustment(stcb, net->mtu);
tuexendd729232011-11-01 23:04:43 +00004727 }
4728#ifdef INET6
4729#ifdef SCTP_EMBEDDED_V6_SCOPE
4730 if (newaddr->sa_family == AF_INET6) {
4731 struct sockaddr_in6 *sin6;
4732
4733 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4734#ifdef SCTP_KAME
4735 (void)sa6_recoverscope(sin6);
4736#else
4737 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4738#endif /* SCTP_KAME */
4739 }
4740#endif /* SCTP_EMBEDDED_V6_SCOPE */
4741#endif
tuexendd729232011-11-01 23:04:43 +00004742
4743 /* JRS - Use the congestion control given in the CC module */
tuexen09631162012-04-18 11:23:02 +00004744 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
tuexendd729232011-11-01 23:04:43 +00004745 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4746
4747 /*
4748 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4749 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4750 */
4751 net->find_pseudo_cumack = 1;
4752 net->find_rtx_pseudo_cumack = 1;
tuexendd729232011-11-01 23:04:43 +00004753#if defined(__FreeBSD__)
4754 /* Choose an initial flowid. */
4755 net->flowid = stcb->asoc.my_vtag ^
4756 ntohs(stcb->rport) ^
4757 ntohs(stcb->sctp_ep->sctp_lport);
Michael Tuexen15f35f82016-06-07 09:19:50 +02004758 net->flowtype = M_HASHTYPE_OPAQUE_HASH;
tuexendd729232011-11-01 23:04:43 +00004759#endif
4760 if (netp) {
4761 *netp = net;
4762 }
4763 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
Michael Tuexena0515422020-05-18 00:47:04 +02004764#if defined(__FreeBSD__)
4765 if (net->ro.ro_nh == NULL) {
4766#else
tuexendd729232011-11-01 23:04:43 +00004767 if (net->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004768#endif
tuexendd729232011-11-01 23:04:43 +00004769 /* Since we have no route put it at the back */
4770 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4771 } else if (netfirst == NULL) {
4772 /* We are the first one in the pool. */
4773 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexena0515422020-05-18 00:47:04 +02004774#if defined(__FreeBSD__)
4775 } else if (netfirst->ro.ro_nh == NULL) {
4776#else
tuexendd729232011-11-01 23:04:43 +00004777 } else if (netfirst->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004778#endif
tuexendd729232011-11-01 23:04:43 +00004779 /*
4780 * First one has NO route. Place this one ahead of the first
4781 * one.
4782 */
4783 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexena0515422020-05-18 00:47:04 +02004784#if defined(__FreeBSD__)
4785 } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
4786#else
tuexendd729232011-11-01 23:04:43 +00004787 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
Michael Tuexena0515422020-05-18 00:47:04 +02004788#endif
tuexendd729232011-11-01 23:04:43 +00004789 /*
4790 * This one has a different interface than the one at the
4791 * top of the list. Place it ahead.
4792 */
4793 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004794 } else {
4795 /*
4796 * Ok we have the same interface as the first one. Move
4797 * forward until we find either a) one with a NULL route...
4798 * insert ahead of that b) one with a different ifp.. insert
4799 * after that. c) end of the list.. insert at the tail.
4800 */
4801 struct sctp_nets *netlook;
4802
4803 do {
4804 netlook = TAILQ_NEXT(netfirst, sctp_next);
4805 if (netlook == NULL) {
4806 /* End of the list */
4807 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4808 break;
Michael Tuexena0515422020-05-18 00:47:04 +02004809#if defined(__FreeBSD__)
4810 } else if (netlook->ro.ro_nh == NULL) {
4811#else
tuexendd729232011-11-01 23:04:43 +00004812 } else if (netlook->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004813#endif
tuexendd729232011-11-01 23:04:43 +00004814 /* next one has NO route */
4815 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4816 break;
Michael Tuexena0515422020-05-18 00:47:04 +02004817#if defined(__FreeBSD__)
4818 } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
tuexendd729232011-11-01 23:04:43 +00004819#else
Michael Tuexena0515422020-05-18 00:47:04 +02004820 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
tuexendd729232011-11-01 23:04:43 +00004821#endif
tuexendd729232011-11-01 23:04:43 +00004822 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
Michael Tuexena0515422020-05-18 00:47:04 +02004823 net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004824 break;
4825 }
tuexendd729232011-11-01 23:04:43 +00004826 /* Shift forward */
4827 netfirst = netlook;
tuexendd729232011-11-01 23:04:43 +00004828 } while (netlook != NULL);
4829 }
4830
4831 /* got to have a primary set */
4832 if (stcb->asoc.primary_destination == 0) {
4833 stcb->asoc.primary_destination = net;
Michael Tuexena0515422020-05-18 00:47:04 +02004834#if defined(__FreeBSD__)
4835 } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
4836 (net->ro.ro_nh) &&
4837#else
tuexendd729232011-11-01 23:04:43 +00004838 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
Michael Tuexena0515422020-05-18 00:47:04 +02004839 (net->ro.ro_rt) &&
4840#endif
4841 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
tuexendd729232011-11-01 23:04:43 +00004842 /* No route to current primary adopt new primary */
4843 stcb->asoc.primary_destination = net;
4844 }
4845 /* Validate primary is first */
4846 net = TAILQ_FIRST(&stcb->asoc.nets);
4847 if ((net != stcb->asoc.primary_destination) &&
4848 (stcb->asoc.primary_destination)) {
4849 /* first one on the list is NOT the primary
4850 * sctp_cmpaddr() is much more efficient if
4851 * the primary is the first on the list, make it
4852 * so.
4853 */
4854 TAILQ_REMOVE(&stcb->asoc.nets,
4855 stcb->asoc.primary_destination, sctp_next);
4856 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4857 stcb->asoc.primary_destination, sctp_next);
4858 }
4859 return (0);
4860}
4861
4862
4863static uint32_t
4864sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4865{
4866 uint32_t id;
4867 struct sctpasochead *head;
4868 struct sctp_tcb *lstcb;
4869
tuexendd729232011-11-01 23:04:43 +00004870 try_again:
4871 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4872 /* TSNH */
tuexendd729232011-11-01 23:04:43 +00004873 return (0);
4874 }
4875 /*
4876 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4877 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4878 */
4879 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4880 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4881 }
4882 id = inp->sctp_associd_counter;
4883 inp->sctp_associd_counter++;
4884 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4885 if (lstcb) {
4886 goto try_again;
4887 }
4888 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4889 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4890 stcb->asoc.in_asocid_hash = 1;
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01004891 return (id);
tuexendd729232011-11-01 23:04:43 +00004892}
4893
4894/*
4895 * allocate an association and add it to the endpoint. The caller must be
4896 * careful to add all additional addresses once they are know right away or
4897 * else the assoc will be may experience a blackout scenario.
4898 */
4899struct sctp_tcb *
4900sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
Michael Tuexen49335e12015-12-06 17:31:45 +01004901 int *error, uint32_t override_tag, uint32_t vrf_id,
Michael Tuexen923f7352016-05-01 23:55:35 +02004902 uint16_t o_streams, uint16_t port,
tuexendd729232011-11-01 23:04:43 +00004903#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004904 struct thread *p,
tuexendd729232011-11-01 23:04:43 +00004905#elif defined(__Windows__)
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004906 PKTHREAD p,
tuexendd729232011-11-01 23:04:43 +00004907#else
4908#if defined(__Userspace__)
4909 /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
4910#endif
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004911 struct proc *p,
tuexendd729232011-11-01 23:04:43 +00004912#endif
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004913 int initialize_auth_params)
tuexendd729232011-11-01 23:04:43 +00004914{
4915 /* note the p argument is only valid in unbound sockets */
4916
4917 struct sctp_tcb *stcb;
4918 struct sctp_association *asoc;
4919 struct sctpasochead *head;
4920 uint16_t rport;
4921 int err;
4922
4923 /*
4924 * Assumption made here: Caller has done a
4925 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4926 * address does not exist already.
4927 */
4928 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4929 /* Hit max assoc, sorry no more */
4930 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4931 *error = ENOBUFS;
4932 return (NULL);
4933 }
4934 if (firstaddr == NULL) {
4935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4936 *error = EINVAL;
4937 return (NULL);
4938 }
4939 SCTP_INP_RLOCK(inp);
4940 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4941 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4942 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4943 /*
4944 * If its in the TCP pool, its NOT allowed to create an
4945 * association. The parent listener needs to call
4946 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4947 * off, or connected one does this.. its an error.
4948 */
4949 SCTP_INP_RUNLOCK(inp);
4950 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4951 *error = EINVAL;
4952 return (NULL);
4953 }
4954 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4955 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4956 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4957 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4958 SCTP_INP_RUNLOCK(inp);
4959 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4960 *error = EINVAL;
4961 return (NULL);
4962 }
4963 }
4964 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4965#ifdef SCTP_DEBUG
4966 if (firstaddr) {
4967 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4968 switch (firstaddr->sa_family) {
4969#ifdef INET
4970 case AF_INET:
4971 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4972 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4973 break;
4974#endif
4975#ifdef INET6
4976 case AF_INET6:
4977 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4978 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4979 break;
4980#endif
tuexencc2b1422012-07-10 21:50:32 +00004981#if defined(__Userspace__)
4982 case AF_CONN:
4983 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4984 ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
4985 break;
4986#endif
tuexendd729232011-11-01 23:04:43 +00004987 default:
4988 break;
4989 }
4990 } else {
4991 SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
4992 }
4993#endif /* SCTP_DEBUG */
4994 switch (firstaddr->sa_family) {
4995#ifdef INET
4996 case AF_INET:
4997 {
4998 struct sockaddr_in *sin;
4999
5000 sin = (struct sockaddr_in *)firstaddr;
5001 if ((ntohs(sin->sin_port) == 0) ||
5002 (sin->sin_addr.s_addr == INADDR_ANY) ||
5003 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
5004 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
5005 /* Invalid address */
5006 SCTP_INP_RUNLOCK(inp);
5007 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5008 *error = EINVAL;
5009 return (NULL);
5010 }
5011 rport = sin->sin_port;
5012 break;
5013 }
5014#endif
5015#ifdef INET6
5016 case AF_INET6:
5017 {
5018 struct sockaddr_in6 *sin6;
5019
5020 sin6 = (struct sockaddr_in6 *)firstaddr;
5021 if ((ntohs(sin6->sin6_port) == 0) ||
5022 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
5023 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
5024 /* Invalid address */
5025 SCTP_INP_RUNLOCK(inp);
5026 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5027 *error = EINVAL;
5028 return (NULL);
5029 }
5030 rport = sin6->sin6_port;
5031 break;
5032 }
5033#endif
tuexencc2b1422012-07-10 21:50:32 +00005034#if defined(__Userspace__)
5035 case AF_CONN:
5036 {
5037 struct sockaddr_conn *sconn;
5038
5039 sconn = (struct sockaddr_conn *)firstaddr;
5040 if ((ntohs(sconn->sconn_port) == 0) ||
5041 (sconn->sconn_addr == NULL)) {
5042 /* Invalid address */
5043 SCTP_INP_RUNLOCK(inp);
5044 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5045 *error = EINVAL;
5046 return (NULL);
5047 }
5048 rport = sconn->sconn_port;
5049 break;
5050 }
5051#endif
tuexendd729232011-11-01 23:04:43 +00005052 default:
5053 /* not supported family type */
5054 SCTP_INP_RUNLOCK(inp);
5055 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5056 *error = EINVAL;
5057 return (NULL);
5058 }
5059 SCTP_INP_RUNLOCK(inp);
5060 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
5061 /*
5062 * If you have not performed a bind, then we need to do the
5063 * ephemeral bind for you.
5064 */
Michael Tuexen3b4263d2020-06-06 19:52:50 +02005065 if ((err = sctp_inpcb_bind(inp->sctp_socket, NULL, NULL, p))) {
tuexendd729232011-11-01 23:04:43 +00005066 /* bind error, probably perm */
5067 *error = err;
5068 return (NULL);
5069 }
5070 }
5071 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
5072 if (stcb == NULL) {
5073 /* out of memory? */
5074 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
5075 *error = ENOMEM;
5076 return (NULL);
5077 }
5078 SCTP_INCR_ASOC_COUNT();
5079
Michael Tuexend0853302017-07-19 17:01:31 +02005080 memset(stcb, 0, sizeof(*stcb));
tuexendd729232011-11-01 23:04:43 +00005081 asoc = &stcb->asoc;
5082
tuexendd729232011-11-01 23:04:43 +00005083 SCTP_TCB_LOCK_INIT(stcb);
5084 SCTP_TCB_SEND_LOCK_INIT(stcb);
5085 stcb->rport = rport;
5086 /* setup back pointer's */
5087 stcb->sctp_ep = inp;
5088 stcb->sctp_socket = inp->sctp_socket;
Michael Tuexen49335e12015-12-06 17:31:45 +01005089 if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id, o_streams))) {
tuexendd729232011-11-01 23:04:43 +00005090 /* failed */
5091 SCTP_TCB_LOCK_DESTROY(stcb);
5092 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
tuexendd729232011-11-01 23:04:43 +00005093 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5094 SCTP_DECR_ASOC_COUNT();
5095 *error = err;
5096 return (NULL);
5097 }
5098 /* and the port */
5099 SCTP_INP_INFO_WLOCK();
5100 SCTP_INP_WLOCK(inp);
5101 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5102 /* inpcb freed while alloc going on */
5103 SCTP_TCB_LOCK_DESTROY(stcb);
5104 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
tuexendd729232011-11-01 23:04:43 +00005105 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5106 SCTP_INP_WUNLOCK(inp);
5107 SCTP_INP_INFO_WUNLOCK();
5108 SCTP_DECR_ASOC_COUNT();
5109 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5110 *error = EINVAL;
5111 return (NULL);
5112 }
5113 SCTP_TCB_LOCK(stcb);
5114
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01005115 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
tuexendd729232011-11-01 23:04:43 +00005116 /* now that my_vtag is set, add it to the hash */
5117 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
5118 /* put it in the bucket in the vtag hash of assoc's for the system */
5119 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
5120 SCTP_INP_INFO_WUNLOCK();
5121
Michael Tuexen923f7352016-05-01 23:55:35 +02005122 if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
tuexendd729232011-11-01 23:04:43 +00005123 /* failure.. memory error? */
5124 if (asoc->strmout) {
5125 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5126 asoc->strmout = NULL;
5127 }
5128 if (asoc->mapping_array) {
5129 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5130 asoc->mapping_array = NULL;
5131 }
5132 if (asoc->nr_mapping_array) {
5133 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5134 asoc->nr_mapping_array = NULL;
5135 }
5136 SCTP_DECR_ASOC_COUNT();
t00fcxen2c2ff922013-11-03 14:03:35 +00005137 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005138 SCTP_TCB_LOCK_DESTROY(stcb);
5139 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5140 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5141 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5142 SCTP_INP_WUNLOCK(inp);
5143 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
5144 *error = ENOBUFS;
5145 return (NULL);
5146 }
5147 /* Init all the timers */
5148 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
5149 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
5150 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
5151 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
5152 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
tuexendd729232011-11-01 23:04:43 +00005153 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5154
5155 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5156 /* now file the port under the hash as well */
5157 if (inp->sctp_tcbhash != NULL) {
5158 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5159 inp->sctp_hashmark)];
5160 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5161 }
Michael Tuexenbfb04f32019-07-14 14:42:17 +02005162 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
5163 sctp_initialize_auth_params(inp, stcb);
5164 }
tuexendd729232011-11-01 23:04:43 +00005165 SCTP_INP_WUNLOCK(inp);
t00fcxen8fca7ff2012-09-05 18:58:42 +00005166 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
tuexendd729232011-11-01 23:04:43 +00005167 return (stcb);
5168}
5169
5170
5171void
5172sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5173{
Michael Tuexen21b55df2020-02-09 23:46:32 +01005174 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00005175 struct sctp_association *asoc;
5176
Michael Tuexen21b55df2020-02-09 23:46:32 +01005177 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00005178 asoc = &stcb->asoc;
5179 asoc->numnets--;
5180 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5181 if (net == asoc->primary_destination) {
5182 /* Reset primary */
5183 struct sctp_nets *lnet;
5184
5185 lnet = TAILQ_FIRST(&asoc->nets);
5186 /* Mobility adaptation
5187 Ideally, if deleted destination is the primary, it becomes
5188 a fast retransmission trigger by the subsequent SET PRIMARY.
5189 (by micchie)
5190 */
5191 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005192 SCTP_MOBILITY_BASE) ||
tuexendd729232011-11-01 23:04:43 +00005193 sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005194 SCTP_MOBILITY_FASTHANDOFF)) {
tuexendd729232011-11-01 23:04:43 +00005195 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5196 if (asoc->deleted_primary != NULL) {
5197 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5198 goto out;
5199 }
5200 asoc->deleted_primary = net;
5201 atomic_add_int(&net->ref_count, 1);
5202 memset(&net->lastsa, 0, sizeof(net->lastsa));
5203 memset(&net->lastsv, 0, sizeof(net->lastsv));
5204 sctp_mobility_feature_on(stcb->sctp_ep,
5205 SCTP_MOBILITY_PRIM_DELETED);
5206 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5207 stcb->sctp_ep, stcb, NULL);
5208 }
5209out:
5210 /* Try to find a confirmed primary */
5211 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5212 }
5213 if (net == asoc->last_data_chunk_from) {
5214 /* Reset primary */
5215 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5216 }
5217 if (net == asoc->last_control_chunk_from) {
5218 /* Clear net */
5219 asoc->last_control_chunk_from = NULL;
5220 }
5221 if (net == stcb->asoc.alternate) {
5222 sctp_free_remote_addr(stcb->asoc.alternate);
5223 stcb->asoc.alternate = NULL;
5224 }
Michael Tuexen21b55df2020-02-09 23:46:32 +01005225 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5226 SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
5227 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5228 SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
Michael Tuexenf52d3df2020-02-11 19:24:34 +01005229 net->dest_state |= SCTP_ADDR_BEING_DELETED;
tuexendd729232011-11-01 23:04:43 +00005230 sctp_free_remote_addr(net);
5231}
5232
5233/*
5234 * remove a remote endpoint address from an association, it will fail if the
5235 * address does not exist.
5236 */
5237int
5238sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5239{
5240 /*
5241 * Here we need to remove a remote address. This is quite simple, we
5242 * first find it in the list of address for the association
5243 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5244 * on that item. Note we do not allow it to be removed if there are
5245 * no other addresses.
5246 */
5247 struct sctp_association *asoc;
5248 struct sctp_nets *net, *nnet;
5249
5250 asoc = &stcb->asoc;
5251
5252 /* locate the address */
5253 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5254 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5255 continue;
5256 }
5257 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5258 remaddr)) {
5259 /* we found the guy */
5260 if (asoc->numnets < 2) {
5261 /* Must have at LEAST two remote addresses */
5262 return (-1);
5263 } else {
5264 sctp_remove_net(stcb, net);
5265 return (0);
5266 }
5267 }
5268 }
5269 /* not found. */
5270 return (-2);
5271}
5272
5273void
5274sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5275{
5276 struct sctpvtaghead *chain;
5277 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005278 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005279 int i;
5280
5281 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
t00fcxen14f8fbb2013-06-25 09:32:47 +00005282 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5283 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5284 if ((twait_block->vtag_block[i].v_tag == tag) &&
5285 (twait_block->vtag_block[i].lport == lport) &&
5286 (twait_block->vtag_block[i].rport == rport)) {
5287 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5288 twait_block->vtag_block[i].v_tag = 0;
5289 twait_block->vtag_block[i].lport = 0;
5290 twait_block->vtag_block[i].rport = 0;
5291 found = 1;
tuexendd729232011-11-01 23:04:43 +00005292 break;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005293 }
tuexendd729232011-11-01 23:04:43 +00005294 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005295 if (found)
5296 break;
tuexendd729232011-11-01 23:04:43 +00005297 }
5298}
5299
5300int
5301sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5302{
5303 struct sctpvtaghead *chain;
5304 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005305 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005306 int i;
5307
5308 SCTP_INP_INFO_WLOCK();
5309 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
t00fcxen14f8fbb2013-06-25 09:32:47 +00005310 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5311 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5312 if ((twait_block->vtag_block[i].v_tag == tag) &&
5313 (twait_block->vtag_block[i].lport == lport) &&
5314 (twait_block->vtag_block[i].rport == rport)) {
5315 found = 1;
tuexendd729232011-11-01 23:04:43 +00005316 break;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005317 }
tuexendd729232011-11-01 23:04:43 +00005318 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005319 if (found)
5320 break;
tuexendd729232011-11-01 23:04:43 +00005321 }
5322 SCTP_INP_INFO_WUNLOCK();
tuexen63fc0bb2011-12-27 12:24:52 +00005323 return (found);
tuexendd729232011-11-01 23:04:43 +00005324}
5325
5326
5327void
5328sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
5329{
5330 struct sctpvtaghead *chain;
5331 struct sctp_tagblock *twait_block;
5332 struct timeval now;
5333 int set, i;
5334
5335 if (time == 0) {
5336 /* Its disabled */
5337 return;
5338 }
5339 (void)SCTP_GETTIME_TIMEVAL(&now);
5340 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5341 set = 0;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005342 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
tuexendd729232011-11-01 23:04:43 +00005343 /* Block(s) present, lets find space, and expire on the fly */
t00fcxen14f8fbb2013-06-25 09:32:47 +00005344 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5345 if ((twait_block->vtag_block[i].v_tag == 0) &&
5346 !set) {
5347 twait_block->vtag_block[i].tv_sec_at_expire =
5348 now.tv_sec + time;
5349 twait_block->vtag_block[i].v_tag = tag;
5350 twait_block->vtag_block[i].lport = lport;
5351 twait_block->vtag_block[i].rport = rport;
5352 set = 1;
5353 } else if ((twait_block->vtag_block[i].v_tag) &&
5354 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
5355 /* Audit expires this guy */
5356 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5357 twait_block->vtag_block[i].v_tag = 0;
5358 twait_block->vtag_block[i].lport = 0;
5359 twait_block->vtag_block[i].rport = 0;
5360 if (set == 0) {
5361 /* Reuse it for my new tag */
5362 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
tuexendd729232011-11-01 23:04:43 +00005363 twait_block->vtag_block[i].v_tag = tag;
5364 twait_block->vtag_block[i].lport = lport;
5365 twait_block->vtag_block[i].rport = rport;
5366 set = 1;
tuexendd729232011-11-01 23:04:43 +00005367 }
5368 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005369 }
5370 if (set) {
5371 /*
5372 * We only do up to the block where we can
5373 * place our tag for audits
5374 */
5375 break;
tuexendd729232011-11-01 23:04:43 +00005376 }
5377 }
5378 /* Need to add a new block to chain */
5379 if (!set) {
5380 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5381 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5382 if (twait_block == NULL) {
tuexendd729232011-11-01 23:04:43 +00005383 return;
5384 }
5385 memset(twait_block, 0, sizeof(struct sctp_tagblock));
5386 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
5387 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
5388 twait_block->vtag_block[0].v_tag = tag;
5389 twait_block->vtag_block[0].lport = lport;
5390 twait_block->vtag_block[0].rport = rport;
5391 }
5392}
5393
Michael Tuexene5001952016-04-17 19:25:27 +02005394void
5395sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
5396{
5397 struct sctp_tmit_chunk *chk, *nchk;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005398 struct sctp_queued_to_read *control, *ncontrol;
5399
5400 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
5401 TAILQ_REMOVE(rh, control, next_instrm);
5402 control->on_strm_q = 0;
5403 if (control->on_read_q == 0) {
5404 sctp_free_remote_addr(control->whoFrom);
5405 if (control->data) {
5406 sctp_m_freem(control->data);
5407 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02005408 }
5409 }
5410 /* Reassembly free? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005411 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5412 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02005413 if (chk->data) {
5414 sctp_m_freem(chk->data);
5415 chk->data = NULL;
5416 }
5417 if (chk->holds_key_ref)
5418 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5419 sctp_free_remote_addr(chk->whoTo);
5420 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5421 SCTP_DECR_CHK_COUNT();
5422 /*sa_ignore FREED_MEMORY*/
5423 }
5424 /*
5425 * We don't free the address here
5426 * since all the net's were freed
5427 * above.
5428 */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005429 if (control->on_read_q == 0) {
5430 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02005431 }
5432 }
5433}
tuexendd729232011-11-01 23:04:43 +00005434
tuexendd729232011-11-01 23:04:43 +00005435/*-
5436 * Free the association after un-hashing the remote port. This
5437 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5438 * expect that the input to this function IS a locked TCB.
5439 * It will return 0, if it did NOT destroy the association (instead
5440 * it unlocks it. It will return NON-zero if it either destroyed the
5441 * association OR the association is already destroyed.
5442 */
5443int
5444sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5445{
5446 int i;
5447 struct sctp_association *asoc;
5448 struct sctp_nets *net, *nnet;
5449 struct sctp_laddr *laddr, *naddr;
5450 struct sctp_tmit_chunk *chk, *nchk;
5451 struct sctp_asconf_addr *aparam, *naparam;
5452 struct sctp_asconf_ack *aack, *naack;
5453 struct sctp_stream_reset_list *strrst, *nstrrst;
5454 struct sctp_queued_to_read *sq, *nsq;
5455 struct sctp_stream_queue_pending *sp, *nsp;
5456 sctp_sharedkey_t *shared_key, *nshared_key;
5457 struct socket *so;
5458
5459 /* first, lets purge the entry from the hash table. */
5460#if defined(__APPLE__)
5461 sctp_lock_assert(SCTP_INP_SO(inp));
5462#endif
5463
5464#ifdef SCTP_LOG_CLOSING
5465 sctp_log_closing(inp, stcb, 6);
5466#endif
5467 if (stcb->asoc.state == 0) {
5468#ifdef SCTP_LOG_CLOSING
5469 sctp_log_closing(inp, NULL, 7);
5470#endif
5471 /* there is no asoc, really TSNH :-0 */
5472 return (1);
5473 }
5474 if (stcb->asoc.alternate) {
5475 sctp_free_remote_addr(stcb->asoc.alternate);
5476 stcb->asoc.alternate = NULL;
5477 }
5478#if !defined(__APPLE__) /* TEMP: moved to below */
5479 /* TEMP CODE */
5480 if (stcb->freed_from_where == 0) {
5481 /* Only record the first place free happened from */
5482 stcb->freed_from_where = from_location;
5483 }
5484 /* TEMP CODE */
5485#endif
5486
5487 asoc = &stcb->asoc;
5488 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5489 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5490 /* nothing around */
5491 so = NULL;
5492 else
5493 so = inp->sctp_socket;
5494
5495 /*
5496 * We used timer based freeing if a reader or writer is in the way.
5497 * So we first check if we are actually being called from a timer,
5498 * if so we abort early if a reader or writer is still in the way.
5499 */
5500 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5501 (from_inpcbfree == SCTP_NORMAL_PROC)) {
5502 /*
5503 * is it the timer driving us? if so are the reader/writers
5504 * gone?
5505 */
5506 if (stcb->asoc.refcnt) {
5507 /* nope, reader or writer in the way */
5508 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5509 /* no asoc destroyed */
5510 SCTP_TCB_UNLOCK(stcb);
5511#ifdef SCTP_LOG_CLOSING
5512 sctp_log_closing(inp, stcb, 8);
5513#endif
5514 return (0);
5515 }
5516 }
Michael Tuexen61f66832020-02-09 23:15:19 +01005517 /* Now clean up any other timers */
Michael Tuexen5f976392020-02-11 21:20:07 +01005518 sctp_stop_association_timers(stcb, false);
tuexendd729232011-11-01 23:04:43 +00005519 /* Now the read queue needs to be cleaned up (only once) */
5520 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005521 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
tuexendd729232011-11-01 23:04:43 +00005522 SCTP_INP_READ_LOCK(inp);
5523 TAILQ_FOREACH(sq, &inp->read_queue, next) {
5524 if (sq->stcb == stcb) {
5525 sq->do_not_ref_stcb = 1;
5526 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5527 /* If there is no end, there never
5528 * will be now.
5529 */
5530 if (sq->end_added == 0) {
5531 /* Held for PD-API clear that. */
5532 sq->pdapi_aborted = 1;
5533 sq->held_length = 0;
5534 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
5535 /*
5536 * Need to add a PD-API aborted indication.
5537 * Setting the control_pdapi assures that it will
5538 * be added right after this msg.
5539 */
5540 uint32_t strseq;
5541 stcb->asoc.control_pdapi = sq;
Michael Tuexen00657ac2016-12-07 21:53:26 +01005542 strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff);
tuexendd729232011-11-01 23:04:43 +00005543 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5544 stcb,
5545 SCTP_PARTIAL_DELIVERY_ABORTED,
5546 (void *)&strseq,
5547 SCTP_SO_LOCKED);
5548 stcb->asoc.control_pdapi = NULL;
5549 }
5550 }
5551 /* Add an end to wake them */
5552 sq->end_added = 1;
5553 }
5554 }
5555 SCTP_INP_READ_UNLOCK(inp);
5556 if (stcb->block_entry) {
5557 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
5558 stcb->block_entry->error = ECONNRESET;
5559 stcb->block_entry = NULL;
5560 }
5561 }
5562 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
5563 /* Someone holds a reference OR the socket is unaccepted yet.
5564 */
5565 if ((stcb->asoc.refcnt) ||
5566 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5567 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005568 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00005569 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5570 }
5571 SCTP_TCB_UNLOCK(stcb);
5572 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5573 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5574 /* nothing around */
5575 so = NULL;
5576 if (so) {
5577 /* Wake any reader/writers */
5578 sctp_sorwakeup(inp, so);
5579 sctp_sowwakeup(inp, so);
5580 }
5581
5582#ifdef SCTP_LOG_CLOSING
5583 sctp_log_closing(inp, stcb, 9);
5584#endif
5585 /* no asoc destroyed */
5586 return (0);
5587 }
5588#ifdef SCTP_LOG_CLOSING
5589 sctp_log_closing(inp, stcb, 10);
5590#endif
5591 /* When I reach here, no others want
5592 * to kill the assoc yet.. and I own
5593 * the lock. Now its possible an abort
5594 * comes in when I do the lock exchange
5595 * below to grab all the locks to do
5596 * the final take out. to prevent this
5597 * we increment the count, which will
5598 * start a timer and blow out above thus
5599 * assuring us that we hold exclusive
5600 * killing of the asoc. Note that
5601 * after getting back the TCB lock
5602 * we will go ahead and increment the
5603 * counter back up and stop any timer
5604 * a passing stranger may have started :-S
5605 */
5606 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5607 atomic_add_int(&stcb->asoc.refcnt, 1);
5608
5609 SCTP_TCB_UNLOCK(stcb);
5610 SCTP_INP_INFO_WLOCK();
5611 SCTP_INP_WLOCK(inp);
5612 SCTP_TCB_LOCK(stcb);
5613 }
5614 /* Double check the GONE flag */
5615 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5616 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5617 /* nothing around */
5618 so = NULL;
5619
Michael Tuexen1f495442020-03-25 16:41:21 +01005620 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5621 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5622 /*
5623 * For TCP type we need special handling when we are
5624 * connected. We also include the peel'ed off ones to.
5625 */
5626 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5627 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5628 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5629 if (so) {
5630 SOCKBUF_LOCK(&so->so_rcv);
5631 so->so_state &= ~(SS_ISCONNECTING |
5632 SS_ISDISCONNECTING |
5633 SS_ISCONFIRMING |
5634 SS_ISCONNECTED);
5635 so->so_state |= SS_ISDISCONNECTED;
5636#if defined(__APPLE__)
5637 socantrcvmore(so);
5638#else
5639 socantrcvmore_locked(so);
5640#endif
5641 socantsendmore(so);
5642 sctp_sowwakeup(inp, so);
5643 sctp_sorwakeup(inp, so);
5644 SCTP_SOWAKEUP(so);
5645 }
5646 }
5647 }
5648
tuexendd729232011-11-01 23:04:43 +00005649 /* Make it invalid too, that way if its
5650 * about to run it will abort and return.
5651 */
5652 /* re-increment the lock */
5653 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5654 atomic_add_int(&stcb->asoc.refcnt, -1);
5655 }
5656 if (stcb->asoc.refcnt) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005657 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00005658 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5659 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5660 SCTP_INP_INFO_WUNLOCK();
5661 SCTP_INP_WUNLOCK(inp);
5662 }
5663 SCTP_TCB_UNLOCK(stcb);
5664 return (0);
5665 }
5666 asoc->state = 0;
5667 if (inp->sctp_tcbhash) {
5668 LIST_REMOVE(stcb, sctp_tcbhash);
5669 }
5670 if (stcb->asoc.in_asocid_hash) {
5671 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5672 }
5673 /* Now lets remove it from the list of ALL associations in the EP */
5674 LIST_REMOVE(stcb, sctp_tcblist);
5675 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5676 SCTP_INP_INCR_REF(inp);
5677 SCTP_INP_WUNLOCK(inp);
5678 }
5679 /* pull from vtag hash */
5680 LIST_REMOVE(stcb, sctp_asocs);
5681 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
5682 inp->sctp_lport, stcb->rport);
5683
5684 /* Now restop the timers to be sure
5685 * this is paranoia at is finest!
5686 */
Michael Tuexen5f976392020-02-11 21:20:07 +01005687 sctp_stop_association_timers(stcb, true);
tuexendd729232011-11-01 23:04:43 +00005688
tuexendd729232011-11-01 23:04:43 +00005689 /*
5690 * The chunk lists and such SHOULD be empty but we check them just
5691 * in case.
5692 */
5693 /* anything on the wheel needs to be removed */
Michael Tuexenc3dd8e22019-03-26 14:09:18 +01005694 SCTP_TCB_SEND_LOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005695 for (i = 0; i < asoc->streamoutcnt; i++) {
5696 struct sctp_stream_out *outs;
5697
5698 outs = &asoc->strmout[i];
5699 /* now clean up any chunks here */
5700 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
Michael Tuexenc6ae6c62016-08-07 15:17:05 +02005701 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
tuexendd729232011-11-01 23:04:43 +00005702 TAILQ_REMOVE(&outs->outqueue, sp, next);
Michael Tuexenc3dd8e22019-03-26 14:09:18 +01005703 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
tuexen30ca8172012-07-17 13:09:48 +00005704 sctp_free_spbufspace(stcb, asoc, sp);
tuexendd729232011-11-01 23:04:43 +00005705 if (sp->data) {
5706 if (so) {
5707 /* Still an open socket - report */
5708 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
tuexenda53ff02012-05-14 09:00:59 +00005709 0, (void *)sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005710 }
5711 if (sp->data) {
5712 sctp_m_freem(sp->data);
5713 sp->data = NULL;
5714 sp->tail_mbuf = NULL;
tuexen30ca8172012-07-17 13:09:48 +00005715 sp->length = 0;
tuexendd729232011-11-01 23:04:43 +00005716 }
5717 }
5718 if (sp->net) {
5719 sctp_free_remote_addr(sp->net);
5720 sp->net = NULL;
5721 }
tuexen30ca8172012-07-17 13:09:48 +00005722 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005723 }
5724 }
Michael Tuexenc3dd8e22019-03-26 14:09:18 +01005725 SCTP_TCB_SEND_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005726 /*sa_ignore FREED_MEMORY*/
5727 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5728 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5729 SCTP_FREE(strrst, SCTP_M_STRESET);
5730 }
5731 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5732 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5733 if (sq->data) {
5734 sctp_m_freem(sq->data);
5735 sq->data = NULL;
5736 }
5737 sctp_free_remote_addr(sq->whoFrom);
5738 sq->whoFrom = NULL;
5739 sq->stcb = NULL;
5740 /* Free the ctl entry */
Michael Tuexene5001952016-04-17 19:25:27 +02005741 sctp_free_a_readq(stcb, sq);
tuexendd729232011-11-01 23:04:43 +00005742 /*sa_ignore FREED_MEMORY*/
5743 }
5744 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5745 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5746 if (chk->data) {
5747 sctp_m_freem(chk->data);
5748 chk->data = NULL;
5749 }
5750 if (chk->holds_key_ref)
5751 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5752 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5753 SCTP_DECR_CHK_COUNT();
5754 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5755 asoc->free_chunk_cnt--;
5756 /*sa_ignore FREED_MEMORY*/
5757 }
5758 /* pending send queue SHOULD be empty */
5759 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005760 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5761 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen8fcc5142012-11-16 19:46:12 +00005762#ifdef INVARIANTS
5763 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005764 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen8fcc5142012-11-16 19:46:12 +00005765#endif
5766 }
tuexendd729232011-11-01 23:04:43 +00005767 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5768 if (chk->data) {
5769 if (so) {
5770 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005771 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5772 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005773 }
5774 if (chk->data) {
5775 sctp_m_freem(chk->data);
5776 chk->data = NULL;
5777 }
5778 }
5779 if (chk->holds_key_ref)
5780 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5781 if (chk->whoTo) {
5782 sctp_free_remote_addr(chk->whoTo);
5783 chk->whoTo = NULL;
5784 }
5785 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5786 SCTP_DECR_CHK_COUNT();
5787 /*sa_ignore FREED_MEMORY*/
5788 }
5789 /* sent queue SHOULD be empty */
5790 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
t00fcxen8fcc5142012-11-16 19:46:12 +00005791 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005792 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5793 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen9ad90772012-11-07 22:19:57 +00005794#ifdef INVARIANTS
5795 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005796 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen9ad90772012-11-07 22:19:57 +00005797#endif
5798 }
5799 }
tuexendd729232011-11-01 23:04:43 +00005800 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5801 if (chk->data) {
5802 if (so) {
5803 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005804 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5805 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005806 }
5807 if (chk->data) {
5808 sctp_m_freem(chk->data);
5809 chk->data = NULL;
5810 }
5811 }
5812 if (chk->holds_key_ref)
5813 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5814 sctp_free_remote_addr(chk->whoTo);
5815 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5816 SCTP_DECR_CHK_COUNT();
5817 /*sa_ignore FREED_MEMORY*/
5818 }
t00fcxen8fcc5142012-11-16 19:46:12 +00005819#ifdef INVARIANTS
5820 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
5821 if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
5822 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
5823 }
5824 }
5825#endif
tuexendd729232011-11-01 23:04:43 +00005826 /* control queue MAY not be empty */
5827 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5828 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5829 if (chk->data) {
5830 sctp_m_freem(chk->data);
5831 chk->data = NULL;
5832 }
5833 if (chk->holds_key_ref)
5834 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5835 sctp_free_remote_addr(chk->whoTo);
5836 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5837 SCTP_DECR_CHK_COUNT();
5838 /*sa_ignore FREED_MEMORY*/
5839 }
5840 /* ASCONF queue MAY not be empty */
5841 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5842 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5843 if (chk->data) {
5844 sctp_m_freem(chk->data);
5845 chk->data = NULL;
5846 }
5847 if (chk->holds_key_ref)
5848 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5849 sctp_free_remote_addr(chk->whoTo);
5850 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5851 SCTP_DECR_CHK_COUNT();
5852 /*sa_ignore FREED_MEMORY*/
5853 }
tuexendd729232011-11-01 23:04:43 +00005854 if (asoc->mapping_array) {
5855 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5856 asoc->mapping_array = NULL;
5857 }
5858 if (asoc->nr_mapping_array) {
5859 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5860 asoc->nr_mapping_array = NULL;
5861 }
5862 /* the stream outs */
5863 if (asoc->strmout) {
5864 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5865 asoc->strmout = NULL;
5866 }
5867 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5868 if (asoc->strmin) {
tuexendd729232011-11-01 23:04:43 +00005869 for (i = 0; i < asoc->streamincnt; i++) {
Michael Tuexene5001952016-04-17 19:25:27 +02005870 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
5871 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
tuexendd729232011-11-01 23:04:43 +00005872 }
5873 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5874 asoc->strmin = NULL;
5875 }
5876 asoc->streamincnt = 0;
5877 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5878#ifdef INVARIANTS
5879 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5880 panic("no net's left alloc'ed, or list points to itself");
5881 }
5882#endif
5883 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5884 sctp_free_remote_addr(net);
5885 }
5886 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5887 /*sa_ignore FREED_MEMORY*/
5888 sctp_remove_laddr(laddr);
5889 }
5890
5891 /* pending asconf (address) parameters */
5892 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5893 /*sa_ignore FREED_MEMORY*/
5894 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5895 SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5896 }
5897 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5898 /*sa_ignore FREED_MEMORY*/
5899 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5900 if (aack->data != NULL) {
5901 sctp_m_freem(aack->data);
5902 }
5903 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5904 }
5905 /* clean up auth stuff */
5906 if (asoc->local_hmacs)
5907 sctp_free_hmaclist(asoc->local_hmacs);
5908 if (asoc->peer_hmacs)
5909 sctp_free_hmaclist(asoc->peer_hmacs);
5910
5911 if (asoc->local_auth_chunks)
5912 sctp_free_chunklist(asoc->local_auth_chunks);
5913 if (asoc->peer_auth_chunks)
5914 sctp_free_chunklist(asoc->peer_auth_chunks);
5915
5916 sctp_free_authinfo(&asoc->authinfo);
5917
5918 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5919 LIST_REMOVE(shared_key, next);
5920 sctp_free_sharedkey(shared_key);
5921 /*sa_ignore FREED_MEMORY*/
5922 }
5923
5924 /* Insert new items here :> */
5925
5926 /* Get rid of LOCK */
t00fcxen2c2ff922013-11-03 14:03:35 +00005927 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005928 SCTP_TCB_LOCK_DESTROY(stcb);
5929 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5930 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5931 SCTP_INP_INFO_WUNLOCK();
5932 SCTP_INP_RLOCK(inp);
5933 }
5934#if defined(__APPLE__) /* TEMP CODE */
5935 stcb->freed_from_where = from_location;
5936#endif
5937#ifdef SCTP_TRACK_FREED_ASOCS
5938 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5939 /* now clean up the tasoc itself */
5940 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5941 SCTP_DECR_ASOC_COUNT();
5942 } else {
5943 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5944 }
5945#else
5946 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5947 SCTP_DECR_ASOC_COUNT();
5948#endif
5949 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5950 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5951 /* If its NOT the inp_free calling us AND
5952 * sctp_close as been called, we
5953 * call back...
5954 */
5955 SCTP_INP_RUNLOCK(inp);
5956 /* This will start the kill timer (if we are
5957 * the last one) since we hold an increment yet. But
5958 * this is the only safe way to do this
5959 * since otherwise if the socket closes
5960 * at the same time we are here we might
5961 * collide in the cleanup.
5962 */
5963 sctp_inpcb_free(inp,
5964 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5965 SCTP_CALLED_DIRECTLY_NOCMPSET);
5966 SCTP_INP_DECR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00005967 } else {
5968 /* The socket is still open. */
5969 SCTP_INP_DECR_REF(inp);
Michael Tuexenc6c994c2020-05-18 21:50:12 +02005970 SCTP_INP_RUNLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00005971 }
5972 }
tuexendd729232011-11-01 23:04:43 +00005973 /* destroyed the asoc */
5974#ifdef SCTP_LOG_CLOSING
5975 sctp_log_closing(inp, NULL, 11);
5976#endif
5977 return (1);
5978}
5979
5980
5981
5982/*
5983 * determine if a destination is "reachable" based upon the addresses bound
5984 * to the current endpoint (e.g. only v4 or v6 currently bound)
5985 */
5986/*
5987 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5988 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5989 * types bound as its endpoint
5990 */
5991int
5992sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5993{
5994 struct sctp_inpcb *inp;
5995 int answer;
5996
5997 /*
5998 * No locks here, the TCB, in all cases is already locked and an
5999 * assoc is up. There is either a INP lock by the caller applied (in
6000 * asconf case when deleting an address) or NOT in the HB case,
6001 * however if HB then the INP increment is up and the INP will not
6002 * be removed (on top of the fact that we have a TCB lock). So we
6003 * only want to read the sctp_flags, which is either bound-all or
6004 * not.. no protection needed since once an assoc is up you can't be
6005 * changing your binding.
6006 */
6007 inp = stcb->sctp_ep;
6008 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6009 /* if bound all, destination is not restricted */
6010 /*
6011 * RRS: Question during lock work: Is this correct? If you
6012 * are bound-all you still might need to obey the V4--V6
6013 * flags??? IMO this bound-all stuff needs to be removed!
6014 */
6015 return (1);
6016 }
6017 /* NOTE: all "scope" checks are done when local addresses are added */
6018 switch (destaddr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00006019#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00006020 case AF_INET6:
6021#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6022 answer = inp->inp_vflag & INP_IPV6;
6023#else
6024 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
6025#endif
6026 break;
tuexen310f1bc2012-07-15 11:20:56 +00006027#endif
6028#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006029 case AF_INET:
6030#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6031 answer = inp->inp_vflag & INP_IPV4;
6032#else
6033 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
6034#endif
tuexen46570542012-07-14 20:37:24 +00006035 break;
tuexen310f1bc2012-07-15 11:20:56 +00006036#endif
tuexen5d9b9742012-07-11 21:52:01 +00006037#if defined(__Userspace__)
6038 case AF_CONN:
6039 answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
tuexendd729232011-11-01 23:04:43 +00006040 break;
tuexen5d9b9742012-07-11 21:52:01 +00006041#endif
tuexendd729232011-11-01 23:04:43 +00006042 default:
6043 /* invalid family, so it's unreachable */
6044 answer = 0;
6045 break;
6046 }
6047 return (answer);
6048}
6049
6050/*
6051 * update the inp_vflags on an endpoint
6052 */
6053static void
6054sctp_update_ep_vflag(struct sctp_inpcb *inp)
6055{
6056 struct sctp_laddr *laddr;
6057
6058 /* first clear the flag */
6059#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6060 inp->inp_vflag = 0;
6061#else
6062 inp->ip_inp.inp.inp_vflag = 0;
6063#endif
6064 /* set the flag based on addresses on the ep list */
6065 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6066 if (laddr->ifa == NULL) {
6067 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02006068 __func__);
tuexendd729232011-11-01 23:04:43 +00006069 continue;
6070 }
6071
6072 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
6073 continue;
6074 }
6075 switch (laddr->ifa->address.sa.sa_family) {
6076#ifdef INET6
6077 case AF_INET6:
6078#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6079 inp->inp_vflag |= INP_IPV6;
6080#else
6081 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6082#endif
6083 break;
6084#endif
6085#ifdef INET
6086 case AF_INET:
6087#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6088 inp->inp_vflag |= INP_IPV4;
6089#else
6090 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6091#endif
6092 break;
6093#endif
tuexen5d9b9742012-07-11 21:52:01 +00006094#if defined(__Userspace__)
6095 case AF_CONN:
6096 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6097 break;
6098#endif
tuexendd729232011-11-01 23:04:43 +00006099 default:
6100 break;
6101 }
6102 }
6103}
6104
6105/*
6106 * Add the address to the endpoint local address list There is nothing to be
6107 * done if we are bound to all addresses
6108 */
6109void
6110sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
6111{
6112 struct sctp_laddr *laddr;
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006113 struct sctp_tcb *stcb;
tuexen63fc0bb2011-12-27 12:24:52 +00006114 int fnd, error = 0;
tuexendd729232011-11-01 23:04:43 +00006115
6116 fnd = 0;
6117
6118 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6119 /* You are already bound to all. You have it already */
6120 return;
6121 }
6122#ifdef INET6
6123 if (ifa->address.sa.sa_family == AF_INET6) {
6124 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6125 /* Can't bind a non-useable addr. */
6126 return;
6127 }
6128 }
6129#endif
6130 /* first, is it already present? */
6131 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6132 if (laddr->ifa == ifa) {
6133 fnd = 1;
6134 break;
6135 }
6136 }
6137
6138 if (fnd == 0) {
6139 /* Not in the ep list */
6140 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
6141 if (error != 0)
6142 return;
6143 inp->laddr_count++;
6144 /* update inp_vflag flags */
6145 switch (ifa->address.sa.sa_family) {
6146#ifdef INET6
6147 case AF_INET6:
6148#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6149 inp->inp_vflag |= INP_IPV6;
6150#else
6151 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6152#endif
6153 break;
6154#endif
tuexen310f1bc2012-07-15 11:20:56 +00006155#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006156 case AF_INET:
6157#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6158 inp->inp_vflag |= INP_IPV4;
6159#else
6160 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6161#endif
6162 break;
6163#endif
tuexen5d9b9742012-07-11 21:52:01 +00006164#if defined(__Userspace__)
6165 case AF_CONN:
6166 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6167 break;
6168#endif
tuexendd729232011-11-01 23:04:43 +00006169 default:
6170 break;
6171 }
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006172 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6173 sctp_add_local_addr_restricted(stcb, ifa);
6174 }
tuexendd729232011-11-01 23:04:43 +00006175 }
6176 return;
6177}
6178
6179
6180/*
6181 * select a new (hopefully reachable) destination net (should only be used
6182 * when we deleted an ep addr that is the only usable source address to reach
6183 * the destination net)
6184 */
6185static void
6186sctp_select_primary_destination(struct sctp_tcb *stcb)
6187{
6188 struct sctp_nets *net;
6189
6190 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6191 /* for now, we'll just pick the first reachable one we find */
6192 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6193 continue;
6194 if (sctp_destination_is_reachable(stcb,
6195 (struct sockaddr *)&net->ro._l_addr)) {
6196 /* found a reachable destination */
6197 stcb->asoc.primary_destination = net;
6198 }
6199 }
6200 /* I can't there from here! ...we're gonna die shortly... */
6201}
6202
6203
6204/*
Michael Tuexen24e6c732016-02-19 12:28:21 +01006205 * Delete the address from the endpoint local address list. There is nothing
tuexendd729232011-11-01 23:04:43 +00006206 * to be done if we are bound to all addresses
6207 */
6208void
6209sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6210{
6211 struct sctp_laddr *laddr;
6212 int fnd;
6213
6214 fnd = 0;
6215 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6216 /* You are already bound to all. You have it already */
6217 return;
6218 }
6219 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6220 if (laddr->ifa == ifa) {
6221 fnd = 1;
6222 break;
6223 }
6224 }
6225 if (fnd && (inp->laddr_count < 2)) {
6226 /* can't delete unless there are at LEAST 2 addresses */
6227 return;
6228 }
6229 if (fnd) {
6230 /*
6231 * clean up any use of this address go through our
6232 * associations and clear any last_used_address that match
6233 * this one for each assoc, see if a new primary_destination
6234 * is needed
6235 */
6236 struct sctp_tcb *stcb;
6237
6238 /* clean up "next_addr_touse" */
6239 if (inp->next_addr_touse == laddr)
6240 /* delete this address */
6241 inp->next_addr_touse = NULL;
6242
6243 /* clean up "last_used_address" */
6244 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6245 struct sctp_nets *net;
Michael Tuexen24e6c732016-02-19 12:28:21 +01006246
tuexendd729232011-11-01 23:04:43 +00006247 SCTP_TCB_LOCK(stcb);
6248 if (stcb->asoc.last_used_address == laddr)
6249 /* delete this address */
6250 stcb->asoc.last_used_address = NULL;
6251 /* Now spin through all the nets and purge any ref to laddr */
6252 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
Michael Tuexen24e6c732016-02-19 12:28:21 +01006253 if (net->ro._s_addr == laddr->ifa) {
tuexendd729232011-11-01 23:04:43 +00006254 /* Yep, purge src address selected */
Michael Tuexena0515422020-05-18 00:47:04 +02006255#if defined(__FreeBSD__)
6256 RO_NHFREE(&net->ro);
6257#else
tuexendd729232011-11-01 23:04:43 +00006258 sctp_rtentry_t *rt;
6259
6260 /* delete this address if cached */
6261 rt = net->ro.ro_rt;
6262 if (rt != NULL) {
6263 RTFREE(rt);
6264 net->ro.ro_rt = NULL;
6265 }
Michael Tuexena0515422020-05-18 00:47:04 +02006266#endif
tuexendd729232011-11-01 23:04:43 +00006267 sctp_free_ifa(net->ro._s_addr);
6268 net->ro._s_addr = NULL;
6269 net->src_addr_selected = 0;
6270 }
6271 }
6272 SCTP_TCB_UNLOCK(stcb);
6273 } /* for each tcb */
6274 /* remove it from the ep list */
6275 sctp_remove_laddr(laddr);
6276 inp->laddr_count--;
6277 /* update inp_vflag flags */
6278 sctp_update_ep_vflag(inp);
6279 }
6280 return;
6281}
6282
6283/*
6284 * Add the address to the TCB local address restricted list.
6285 * This is a "pending" address list (eg. addresses waiting for an
6286 * ASCONF-ACK response) and cannot be used as a valid source address.
6287 */
6288void
6289sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6290{
tuexendd729232011-11-01 23:04:43 +00006291 struct sctp_laddr *laddr;
6292 struct sctpladdr *list;
6293
6294 /*
6295 * Assumes TCB is locked.. and possibly the INP. May need to
6296 * confirm/fix that if we need it and is not the case.
6297 */
6298 list = &stcb->asoc.sctp_restricted_addrs;
6299
tuexendd729232011-11-01 23:04:43 +00006300#ifdef INET6
6301 if (ifa->address.sa.sa_family == AF_INET6) {
6302 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6303 /* Can't bind a non-existent addr. */
6304 return;
6305 }
6306 }
6307#endif
6308 /* does the address already exist? */
6309 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6310 if (laddr->ifa == ifa) {
6311 return;
6312 }
6313 }
6314
6315 /* add to the list */
6316 (void)sctp_insert_laddr(list, ifa, 0);
6317 return;
6318}
6319
6320/*
tuexendd729232011-11-01 23:04:43 +00006321 * Remove a local address from the TCB local address restricted list
6322 */
6323void
6324sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6325{
6326 struct sctp_inpcb *inp;
6327 struct sctp_laddr *laddr;
6328
6329 /*
6330 * This is called by asconf work. It is assumed that a) The TCB is
6331 * locked and b) The INP is locked. This is true in as much as I can
6332 * trace through the entry asconf code where I did these locks.
6333 * Again, the ASCONF code is a bit different in that it does lock
6334 * the INP during its work often times. This must be since we don't
6335 * want other proc's looking up things while what they are looking
6336 * up is changing :-D
6337 */
6338
6339 inp = stcb->sctp_ep;
6340 /* if subset bound and don't allow ASCONF's, can't delete last */
6341 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6342 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6343 if (stcb->sctp_ep->laddr_count < 2) {
6344 /* can't delete last address */
6345 return;
6346 }
6347 }
6348 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6349 /* remove the address if it exists */
6350 if (laddr->ifa == NULL)
6351 continue;
6352 if (laddr->ifa == ifa) {
6353 sctp_remove_laddr(laddr);
6354 return;
6355 }
6356 }
6357
6358 /* address not found! */
6359 return;
6360}
6361
6362#if defined(__FreeBSD__)
6363/*
6364 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
6365 */
6366/* sysctl */
6367static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6368static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
6369#endif /* FreeBSD || APPLE */
6370
6371
6372
6373#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexen63fc0bb2011-12-27 12:24:52 +00006374struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6375int *sctp_cpuarry = NULL;
tuexendd729232011-11-01 23:04:43 +00006376void
6377sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6378{
6379 /* Queue a packet to a processor for the specified core */
6380 struct sctp_mcore_queue *qent;
6381 struct sctp_mcore_ctrl *wkq;
tuexen63fc0bb2011-12-27 12:24:52 +00006382 int need_wake = 0;
tuexendd729232011-11-01 23:04:43 +00006383 if (sctp_mcore_workers == NULL) {
6384 /* Something went way bad during setup */
6385 sctp_input_with_port(m, off, 0);
6386 return;
6387 }
6388 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6389 (sizeof(struct sctp_mcore_queue)),
6390 SCTP_M_MCORE);
6391 if (qent == NULL) {
6392 /* This is trouble */
6393 sctp_input_with_port(m, off, 0);
6394 return;
6395 }
6396#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6397 qent->vn = curvnet;
6398#endif
6399 qent->m = m;
6400 qent->off = off;
6401 qent->v6 = 0;
6402 wkq = &sctp_mcore_workers[cpu_to_use];
6403 SCTP_MCORE_QLOCK(wkq);
6404
6405 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6406 if (wkq->running == 0) {
6407 need_wake = 1;
6408 }
6409 SCTP_MCORE_QUNLOCK(wkq);
tuexen63fc0bb2011-12-27 12:24:52 +00006410 if (need_wake) {
tuexendd729232011-11-01 23:04:43 +00006411 wakeup(&wkq->running);
6412 }
6413}
6414
6415static void
6416sctp_mcore_thread(void *arg)
6417{
6418
6419 struct sctp_mcore_ctrl *wkq;
6420 struct sctp_mcore_queue *qent;
6421
6422 wkq = (struct sctp_mcore_ctrl *)arg;
6423 struct mbuf *m;
6424 int off, v6;
6425
6426 /* Wait for first tickle */
6427 SCTP_MCORE_LOCK(wkq);
6428 wkq->running = 0;
6429 msleep(&wkq->running,
6430 &wkq->core_mtx,
6431 0, "wait for pkt", 0);
6432 SCTP_MCORE_UNLOCK(wkq);
6433
6434 /* Bind to our cpu */
6435 thread_lock(curthread);
6436 sched_bind(curthread, wkq->cpuid);
6437 thread_unlock(curthread);
6438
6439 /* Now lets start working */
6440 SCTP_MCORE_LOCK(wkq);
6441 /* Now grab lock and go */
tuexen9784e9a2011-12-18 13:04:23 +00006442 for (;;) {
tuexendd729232011-11-01 23:04:43 +00006443 SCTP_MCORE_QLOCK(wkq);
6444 skip_sleep:
6445 wkq->running = 1;
6446 qent = TAILQ_FIRST(&wkq->que);
6447 if (qent) {
6448 TAILQ_REMOVE(&wkq->que, qent, next);
6449 SCTP_MCORE_QUNLOCK(wkq);
6450#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6451 CURVNET_SET(qent->vn);
6452#endif
6453 m = qent->m;
6454 off = qent->off;
6455 v6 = qent->v6;
6456 SCTP_FREE(qent, SCTP_M_MCORE);
6457 if (v6 == 0) {
6458 sctp_input_with_port(m, off, 0);
6459 } else {
tuexencb5fe8d2012-05-04 09:50:27 +00006460 SCTP_PRINTF("V6 not yet supported\n");
tuexendd729232011-11-01 23:04:43 +00006461 sctp_m_freem(m);
6462 }
6463#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6464 CURVNET_RESTORE();
6465#endif
6466 SCTP_MCORE_QLOCK(wkq);
6467 }
6468 wkq->running = 0;
6469 if (!TAILQ_EMPTY(&wkq->que)) {
6470 goto skip_sleep;
6471 }
6472 SCTP_MCORE_QUNLOCK(wkq);
6473 msleep(&wkq->running,
6474 &wkq->core_mtx,
6475 0, "wait for pkt", 0);
tuexen63fc0bb2011-12-27 12:24:52 +00006476 }
tuexendd729232011-11-01 23:04:43 +00006477}
6478
6479static void
6480sctp_startup_mcore_threads(void)
6481{
6482 int i, cpu;
6483
6484 if (mp_ncpus == 1)
6485 return;
6486
6487 if (sctp_mcore_workers != NULL) {
6488 /* Already been here in some previous
6489 * vnet?
6490 */
6491 return;
6492 }
6493 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6494 ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6495 SCTP_M_MCORE);
6496 if (sctp_mcore_workers == NULL) {
6497 /* TSNH I hope */
6498 return;
6499 }
tuexen09631162012-04-18 11:23:02 +00006500 memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
tuexendd729232011-11-01 23:04:43 +00006501 sizeof(struct sctp_mcore_ctrl)));
6502 /* Init the structures */
tuexen63fc0bb2011-12-27 12:24:52 +00006503 for (i = 0; i<=mp_maxid; i++) {
tuexendd729232011-11-01 23:04:43 +00006504 TAILQ_INIT(&sctp_mcore_workers[i].que);
6505 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6506 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6507 sctp_mcore_workers[i].cpuid = i;
6508 }
6509 if (sctp_cpuarry == NULL) {
6510 SCTP_MALLOC(sctp_cpuarry, int *,
6511 (mp_ncpus * sizeof(int)),
6512 SCTP_M_MCORE);
tuexen63fc0bb2011-12-27 12:24:52 +00006513 i = 0;
tuexendd729232011-11-01 23:04:43 +00006514 CPU_FOREACH(cpu) {
6515 sctp_cpuarry[i] = cpu;
6516 i++;
6517 }
6518 }
6519
6520 /* Now start them all */
6521 CPU_FOREACH(cpu) {
6522#if __FreeBSD_version <= 701000
6523 (void)kthread_create(sctp_mcore_thread,
6524 (void *)&sctp_mcore_workers[cpu],
6525 &sctp_mcore_workers[cpu].thread_proc,
6526 RFPROC,
6527 SCTP_KTHREAD_PAGES,
6528 SCTP_MCORE_NAME);
6529
6530#else
6531 (void)kproc_create(sctp_mcore_thread,
6532 (void *)&sctp_mcore_workers[cpu],
6533 &sctp_mcore_workers[cpu].thread_proc,
6534 RFPROC,
6535 SCTP_KTHREAD_PAGES,
6536 SCTP_MCORE_NAME);
6537#endif
tuexen09631162012-04-18 11:23:02 +00006538
tuexendd729232011-11-01 23:04:43 +00006539 }
6540}
6541#endif
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01006542#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000
tuexendd729232011-11-01 23:04:43 +00006543static struct mbuf *
6544sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6545{
6546 struct ip *ip;
6547 struct sctphdr *sh;
6548 int offset;
6549 uint32_t flowid, tag;
tuexen76eb2092012-04-01 16:35:28 +00006550
tuexendd729232011-11-01 23:04:43 +00006551 /*
6552 * No flow id built by lower layers fix it so we
6553 * create one.
6554 */
6555 ip = mtod(m, struct ip *);
tuexen664967a2012-06-28 16:24:36 +00006556 offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
tuexendd729232011-11-01 23:04:43 +00006557 if (SCTP_BUF_LEN(m) < offset) {
tuexen664967a2012-06-28 16:24:36 +00006558 if ((m = m_pullup(m, offset)) == NULL) {
tuexendd729232011-11-01 23:04:43 +00006559 SCTP_STAT_INCR(sctps_hdrops);
6560 return (NULL);
6561 }
6562 ip = mtod(m, struct ip *);
6563 }
6564 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
tuexendd729232011-11-01 23:04:43 +00006565 tag = htonl(sh->v_tag);
6566 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6567 m->m_pkthdr.flowid = flowid;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02006568/* FIX ME */
tuexendd729232011-11-01 23:04:43 +00006569 m->m_flags |= M_FLOWID;
6570 return (m);
6571}
6572#endif
6573
6574void
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006575#if defined(__Userspace__)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006576sctp_pcb_init(int start_threads)
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006577#else
6578sctp_pcb_init(void)
6579#endif
tuexendd729232011-11-01 23:04:43 +00006580{
6581 /*
6582 * SCTP initialization for the PCB structures should be called by
Michael Tuexen34488e72016-05-03 22:11:59 +02006583 * the sctp_init() function.
tuexendd729232011-11-01 23:04:43 +00006584 */
6585 int i;
6586 struct timeval tv;
tuexen6019b072011-12-15 18:38:03 +00006587
tuexendd729232011-11-01 23:04:43 +00006588 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6589 /* error I was called twice */
6590 return;
6591 }
6592 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6593
Michael Tuexencb2878b2015-11-13 12:59:40 +01006594#if defined(SCTP_PROCESS_LEVEL_LOCKS)
6595#if !defined(__Userspace_os_Windows)
6596 pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr));
6597#ifdef INVARIANTS
6598 pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK);
6599#endif
6600#endif
6601#endif
tuexendd729232011-11-01 23:04:43 +00006602#if defined(SCTP_LOCAL_TRACE_BUF)
6603#if defined(__Windows__)
6604 if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02006605 memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006606 }
6607#else
Michael Tuexend0853302017-07-19 17:01:31 +02006608 memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006609#endif
6610#endif
6611#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6612 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6613 ((mp_maxid+1) * sizeof(struct sctpstat)),
6614 SCTP_M_MCORE);
6615#endif
6616 (void)SCTP_GETTIME_TIMEVAL(&tv);
6617#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
Michael Tuexend0853302017-07-19 17:01:31 +02006618 memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1));
tuexendd729232011-11-01 23:04:43 +00006619 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6620 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6621#else
Michael Tuexend0853302017-07-19 17:01:31 +02006622 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
tuexendd729232011-11-01 23:04:43 +00006623 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6624 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6625#endif
6626 /* init the empty list of (All) Endpoints */
6627 LIST_INIT(&SCTP_BASE_INFO(listhead));
6628#if defined(__APPLE__)
6629 LIST_INIT(&SCTP_BASE_INFO(inplisthead));
t00fcxenbc766ab2014-01-03 19:29:31 +00006630#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6631 SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead);
6632 SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init();
6633 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6634 SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6635 SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init();
6636 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6637#else
6638 SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead);
6639 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init();
6640 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6641 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6642 SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init();
6643 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6644#endif
6645#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6646 SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc;
6647 in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo));
6648#endif
tuexendd729232011-11-01 23:04:43 +00006649#endif
6650
6651
6652 /* init the hash table of endpoints */
6653#if defined(__FreeBSD__)
6654#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
6655 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6656 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6657 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
6658#else
6659 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
6660 SCTP_BASE_SYSCTL(sctp_hashtblsize));
6661 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
6662 SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6663 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
6664 SCTP_BASE_SYSCTL(sctp_chunkscale));
6665#endif
6666#endif
6667 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6668 &SCTP_BASE_INFO(hashasocmark));
6669 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6670 &SCTP_BASE_INFO(hashmark));
6671 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6672 &SCTP_BASE_INFO(hashtcpmark));
6673 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
6674
6675
6676 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6677 &SCTP_BASE_INFO(hashvrfmark));
6678
6679 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6680 &SCTP_BASE_INFO(vrf_ifn_hashmark));
6681 /* init the zones */
6682 /*
6683 * FIX ME: Should check for NULL returns, but if it does fail we are
6684 * doomed to panic anyways... add later maybe.
6685 */
6686 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6687 sizeof(struct sctp_inpcb), maxsockets);
6688
6689 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6690 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6691
6692 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6693 sizeof(struct sctp_laddr),
6694 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6695
6696 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6697 sizeof(struct sctp_nets),
6698 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6699
6700 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6701 sizeof(struct sctp_tmit_chunk),
6702 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6703
6704 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6705 sizeof(struct sctp_queued_to_read),
6706 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6707
6708 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6709 sizeof(struct sctp_stream_queue_pending),
6710 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6711
6712 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6713 sizeof(struct sctp_asconf),
6714 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6715
6716 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6717 sizeof(struct sctp_asconf_ack),
6718 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6719
tuexen9784e9a2011-12-18 13:04:23 +00006720
tuexendd729232011-11-01 23:04:43 +00006721 /* Master Lock INIT for info structure */
tuexendd729232011-11-01 23:04:43 +00006722 SCTP_INP_INFO_LOCK_INIT();
6723 SCTP_STATLOG_INIT_LOCK();
6724
6725 SCTP_IPI_COUNT_INIT();
6726 SCTP_IPI_ADDR_INIT();
6727#ifdef SCTP_PACKET_LOGGING
6728 SCTP_IP_PKTLOG_INIT();
6729#endif
6730 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6731
6732 SCTP_WQ_ADDR_INIT();
6733 /* not sure if we need all the counts */
6734 SCTP_BASE_INFO(ipi_count_ep) = 0;
6735 /* assoc/tcb zone info */
6736 SCTP_BASE_INFO(ipi_count_asoc) = 0;
6737 /* local addrlist zone info */
6738 SCTP_BASE_INFO(ipi_count_laddr) = 0;
6739 /* remote addrlist zone info */
6740 SCTP_BASE_INFO(ipi_count_raddr) = 0;
6741 /* chunk info */
6742 SCTP_BASE_INFO(ipi_count_chunk) = 0;
6743
6744 /* socket queue zone info */
6745 SCTP_BASE_INFO(ipi_count_readq) = 0;
6746
6747 /* stream out queue cont */
6748 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6749
6750 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6751 SCTP_BASE_INFO(ipi_free_chunks) = 0;
6752
6753 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6754
6755 /* Init the TIMEWAIT list */
6756 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6757 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6758 }
tuexendd729232011-11-01 23:04:43 +00006759#if defined(SCTP_PROCESS_LEVEL_LOCKS)
tuexen5154a002011-12-23 18:47:55 +00006760#if defined(__Userspace_os_Windows)
tuexen6019b072011-12-15 18:38:03 +00006761 InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6762#else
tuexendd729232011-11-01 23:04:43 +00006763 (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6764#endif
tuexen6019b072011-12-15 18:38:03 +00006765#endif
tuexendd729232011-11-01 23:04:43 +00006766 sctp_startup_iterator();
6767
6768#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6769 sctp_startup_mcore_threads();
6770#endif
6771
tuexendd729232011-11-01 23:04:43 +00006772 /*
6773 * INIT the default VRF which for BSD is the only one, other O/S's
6774 * may have more. But initially they must start with one and then
6775 * add the VRF's as addresses are added.
6776 */
6777 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01006778#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000
tuexendd729232011-11-01 23:04:43 +00006779 if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
tuexencb5fe8d2012-05-04 09:50:27 +00006780 SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
tuexendd729232011-11-01 23:04:43 +00006781 }
6782#endif
6783#if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6784 /* allocate the lock for the callout/timer queue */
6785 SCTP_TIMERQ_LOCK_INIT();
6786 TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6787#endif
6788#if defined(__Userspace__)
Michael Tuexen2f6478e2017-03-02 09:57:46 +01006789 mbuf_initialize(NULL);
tuexendd729232011-11-01 23:04:43 +00006790 atomic_init();
tuexenc4178972012-07-15 20:40:54 +00006791#if defined(INET) || defined(INET6)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006792 if (start_threads)
6793 recv_thread_init();
tuexendd729232011-11-01 23:04:43 +00006794#endif
tuexenc4178972012-07-15 20:40:54 +00006795#endif
tuexendd729232011-11-01 23:04:43 +00006796}
6797
6798/*
6799 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6800 */
6801void
6802sctp_pcb_finish(void)
6803{
6804 struct sctp_vrflist *vrf_bucket;
6805 struct sctp_vrf *vrf, *nvrf;
6806 struct sctp_ifn *ifn, *nifn;
6807 struct sctp_ifa *ifa, *nifa;
6808 struct sctpvtaghead *chain;
6809 struct sctp_tagblock *twait_block, *prev_twait_block;
6810 struct sctp_laddr *wi, *nwi;
6811 int i;
t00fcxenc7932282013-11-16 14:57:41 +00006812 struct sctp_iterator *it, *nit;
Michael Tuexen20785d42015-11-13 12:48:35 +01006813
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006814 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6815 SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
6816 return;
6817 }
6818 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
t00fcxen9232a652013-11-30 09:34:24 +00006819#if !defined(__FreeBSD__)
tuexendd729232011-11-01 23:04:43 +00006820 /* Notify the iterator to exit. */
6821 SCTP_IPI_ITERATOR_WQ_LOCK();
6822 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6823 sctp_wakeup_iterator();
6824 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6825#endif
6826#if defined(__APPLE__)
t00fcxenbc766ab2014-01-03 19:29:31 +00006827#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6828 in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo));
6829#endif
tuexendd729232011-11-01 23:04:43 +00006830 SCTP_IPI_ITERATOR_WQ_LOCK();
6831 do {
6832 msleep(&sctp_it_ctl.iterator_flags,
6833 sctp_it_ctl.ipi_iterator_wq_mtx,
6834 0, "waiting_for_work", 0);
6835 } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6836 thread_deallocate(sctp_it_ctl.thread_proc);
6837 SCTP_IPI_ITERATOR_WQ_UNLOCK();
tuexendd729232011-11-01 23:04:43 +00006838#endif
6839#if defined(__Windows__)
6840 if (sctp_it_ctl.iterator_thread_obj != NULL) {
6841 NTSTATUS status = STATUS_SUCCESS;
6842
6843 KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6844 status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6845 Executive,
6846 KernelMode,
6847 FALSE,
6848 NULL);
6849 ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6850 }
6851#endif
t00fcxen92a5ac42013-11-16 12:58:54 +00006852#if defined(__Userspace__)
Michael Tuexen3212d542020-03-16 12:30:04 +01006853 if (SCTP_BASE_VAR(iterator_thread_started)) {
t00fcxen92a5ac42013-11-16 12:58:54 +00006854#if defined(__Userspace_os_Windows)
6855 WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE);
6856 CloseHandle(sctp_it_ctl.thread_proc);
6857 sctp_it_ctl.thread_proc = NULL;
6858#else
6859 pthread_join(sctp_it_ctl.thread_proc, NULL);
6860 sctp_it_ctl.thread_proc = 0;
6861#endif
6862 }
6863#endif
6864#if defined(SCTP_PROCESS_LEVEL_LOCKS)
6865#if defined(__Userspace_os_Windows)
6866 DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup);
6867#else
6868 pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup);
Michael Tuexen89e15a72015-11-13 12:42:55 +01006869 pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr));
t00fcxen92a5ac42013-11-16 12:58:54 +00006870#endif
6871#endif
t00fcxen9232a652013-11-30 09:34:24 +00006872 /* In FreeBSD the iterator thread never exits
6873 * but we do clean up.
6874 * The only way FreeBSD reaches here is if we have VRF's
6875 * but we still add the ifdef to make it compile on old versions.
6876 */
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006877#if defined(__FreeBSD__)
6878retry:
6879#endif
t00fcxen9232a652013-11-30 09:34:24 +00006880 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006881#if defined(__FreeBSD__)
6882 /*
6883 * sctp_iterator_worker() might be working on an it entry without
6884 * holding the lock. We won't find it on the list either and
6885 * continue and free/destroy it. While holding the lock, spin, to
6886 * avoid the race condition as sctp_iterator_worker() will have to
6887 * wait to re-aquire the lock.
6888 */
6889 if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
6890 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6891 SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
6892 "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
6893 DELAY(10);
6894 goto retry;
6895 }
6896#endif
t00fcxen9232a652013-11-30 09:34:24 +00006897 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
t00fcxen433c6e12013-11-30 09:45:11 +00006898#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
t00fcxen9232a652013-11-30 09:34:24 +00006899 if (it->vn != curvnet) {
6900 continue;
6901 }
6902#endif
6903 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6904 if (it->function_atend != NULL) {
6905 (*it->function_atend) (it->pointer, it->val);
6906 }
6907 SCTP_FREE(it,SCTP_M_ITER);
6908 }
6909 SCTP_IPI_ITERATOR_WQ_UNLOCK();
t00fcxen433c6e12013-11-30 09:45:11 +00006910#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
t00fcxen9232a652013-11-30 09:34:24 +00006911 SCTP_ITERATOR_LOCK();
6912 if ((sctp_it_ctl.cur_it) &&
6913 (sctp_it_ctl.cur_it->vn == curvnet)) {
6914 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6915 }
6916 SCTP_ITERATOR_UNLOCK();
6917#endif
t00fcxenc7932282013-11-16 14:57:41 +00006918#if !defined(__FreeBSD__)
t00fcxen92a5ac42013-11-16 12:58:54 +00006919 SCTP_IPI_ITERATOR_WQ_DESTROY();
t00fcxen8dd95b82014-07-11 20:41:28 +00006920 SCTP_ITERATOR_LOCK_DESTROY();
t00fcxenc7932282013-11-16 14:57:41 +00006921#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006922 SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
tuexendd729232011-11-01 23:04:43 +00006923 SCTP_WQ_ADDR_LOCK();
6924 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6925 LIST_REMOVE(wi, sctp_nxt_addr);
6926 SCTP_DECR_LADDR_COUNT();
t00fcxen9892b7f2014-09-06 20:06:39 +00006927 if (wi->action == SCTP_DEL_IP_ADDRESS) {
6928 SCTP_FREE(wi->ifa, SCTP_M_IFA);
6929 }
tuexendd729232011-11-01 23:04:43 +00006930 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6931 }
6932 SCTP_WQ_ADDR_UNLOCK();
tuexen09631162012-04-18 11:23:02 +00006933
tuexendd729232011-11-01 23:04:43 +00006934 /*
6935 * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6936 * is destroyed first).
6937 */
6938 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6939 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6940 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6941 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6942 /* free the ifa */
6943 LIST_REMOVE(ifa, next_bucket);
6944 LIST_REMOVE(ifa, next_ifa);
6945 SCTP_FREE(ifa, SCTP_M_IFA);
6946 }
6947 /* free the ifn */
6948 LIST_REMOVE(ifn, next_bucket);
6949 LIST_REMOVE(ifn, next_ifn);
6950 SCTP_FREE(ifn, SCTP_M_IFN);
6951 }
6952 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6953 /* free the vrf */
6954 LIST_REMOVE(vrf, next_vrf);
6955 SCTP_FREE(vrf, SCTP_M_VRF);
6956 }
6957 /* free the vrf hashes */
6958 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6959 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
tuexendd729232011-11-01 23:04:43 +00006960
6961 /* free the TIMEWAIT list elements malloc'd in the function
6962 * sctp_add_vtag_to_timewait()...
6963 */
6964 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6965 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6966 if (!LIST_EMPTY(chain)) {
6967 prev_twait_block = NULL;
6968 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6969 if (prev_twait_block) {
6970 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6971 }
6972 prev_twait_block = twait_block;
6973 }
6974 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6975 }
6976 }
6977
6978 /* free the locks and mutexes */
6979#if defined(__APPLE__)
6980 SCTP_TIMERQ_LOCK_DESTROY();
6981#endif
6982#ifdef SCTP_PACKET_LOGGING
6983 SCTP_IP_PKTLOG_DESTROY();
6984#endif
6985 SCTP_IPI_ADDR_DESTROY();
6986#if defined(__APPLE__)
6987 SCTP_IPI_COUNT_DESTROY();
6988#endif
6989 SCTP_STATLOG_DESTROY();
tuexendd729232011-11-01 23:04:43 +00006990 SCTP_INP_INFO_LOCK_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006991
tuexendd729232011-11-01 23:04:43 +00006992 SCTP_WQ_ADDR_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006993
tuexendd729232011-11-01 23:04:43 +00006994#if defined(__APPLE__)
t00fcxenbc766ab2014-01-03 19:29:31 +00006995#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6996 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6997 lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp);
6998 lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6999#else
7000 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
7001 lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp);
7002 lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
7003#endif
tuexendd729232011-11-01 23:04:43 +00007004#endif
7005#if defined(__Userspace__)
7006 SCTP_TIMERQ_LOCK_DESTROY();
7007 SCTP_ZONE_DESTROY(zone_mbuf);
7008 SCTP_ZONE_DESTROY(zone_clust);
7009 SCTP_ZONE_DESTROY(zone_ext_refcnt);
7010#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01007011 /* Get rid of other stuff too. */
7012 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
7013 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
7014 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
7015 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
7016 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
7017 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
7018
tuexendd729232011-11-01 23:04:43 +00007019#if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__)
7020 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
7021 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
7022 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
7023 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
7024 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
7025 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
7026 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
7027 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
7028 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
7029#endif
tuexendd729232011-11-01 23:04:43 +00007030#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexen09631162012-04-18 11:23:02 +00007031 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
tuexendd729232011-11-01 23:04:43 +00007032#endif
7033}
7034
7035
7036int
7037sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
tuexen664967a2012-06-28 16:24:36 +00007038 int offset, int limit,
7039 struct sockaddr *src, struct sockaddr *dst,
Michael Tuexen923f7352016-05-01 23:55:35 +02007040 struct sockaddr *altsa, uint16_t port)
tuexendd729232011-11-01 23:04:43 +00007041{
7042 /*
7043 * grub through the INIT pulling addresses and loading them to the
7044 * nets structure in the asoc. The from address in the mbuf should
7045 * also be loaded (if it is not already). This routine can be called
7046 * with either INIT or INIT-ACK's as long as the m points to the IP
7047 * packet and the offset points to the beginning of the parameters.
7048 */
tuexen63fc0bb2011-12-27 12:24:52 +00007049 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00007050 struct sctp_nets *net, *nnet, *net_tmp;
Michael Tuexen473225f2017-06-23 23:05:43 +02007051 struct sctp_paramhdr *phdr, param_buf;
tuexendd729232011-11-01 23:04:43 +00007052 struct sctp_tcb *stcb_tmp;
7053 uint16_t ptype, plen;
7054 struct sockaddr *sa;
tuexendd729232011-11-01 23:04:43 +00007055 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
7056 struct sctp_auth_random *p_random = NULL;
7057 uint16_t random_len = 0;
7058 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
7059 struct sctp_auth_hmac_algo *hmacs = NULL;
7060 uint16_t hmacs_len = 0;
tuexen63fc0bb2011-12-27 12:24:52 +00007061 uint8_t saw_asconf = 0;
7062 uint8_t saw_asconf_ack = 0;
tuexendd729232011-11-01 23:04:43 +00007063 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
7064 struct sctp_auth_chunk_list *chunks = NULL;
7065 uint16_t num_chunks = 0;
7066 sctp_key_t *new_key;
7067 uint32_t keylen;
7068 int got_random = 0, got_hmacs = 0, got_chklist = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007069 uint8_t peer_supports_ecn;
7070 uint8_t peer_supports_prsctp;
7071 uint8_t peer_supports_auth;
7072 uint8_t peer_supports_asconf;
7073 uint8_t peer_supports_asconf_ack;
7074 uint8_t peer_supports_reconfig;
7075 uint8_t peer_supports_nrsack;
7076 uint8_t peer_supports_pktdrop;
Michael Tuexene5001952016-04-17 19:25:27 +02007077 uint8_t peer_supports_idata;
tuexendd729232011-11-01 23:04:43 +00007078#ifdef INET
7079 struct sockaddr_in sin;
7080#endif
7081#ifdef INET6
7082 struct sockaddr_in6 sin6;
7083#endif
7084
7085 /* First get the destination address setup too. */
7086#ifdef INET
7087 memset(&sin, 0, sizeof(sin));
7088 sin.sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00007089#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00007090 sin.sin_len = sizeof(sin);
7091#endif
7092 sin.sin_port = stcb->rport;
7093#endif
7094#ifdef INET6
7095 memset(&sin6, 0, sizeof(sin6));
7096 sin6.sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00007097#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00007098 sin6.sin6_len = sizeof(struct sockaddr_in6);
7099#endif
7100 sin6.sin6_port = stcb->rport;
7101#endif
tuexen664967a2012-06-28 16:24:36 +00007102 if (altsa) {
7103 sa = altsa;
7104 } else {
7105 sa = src;
tuexendd729232011-11-01 23:04:43 +00007106 }
Michael Tuexene5001952016-04-17 19:25:27 +02007107 peer_supports_idata = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007108 peer_supports_ecn = 0;
7109 peer_supports_prsctp = 0;
7110 peer_supports_auth = 0;
7111 peer_supports_asconf = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007112 peer_supports_reconfig = 0;
7113 peer_supports_nrsack = 0;
7114 peer_supports_pktdrop = 0;
tuexendd729232011-11-01 23:04:43 +00007115 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7116 /* mark all addresses that we have currently on the list */
7117 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
7118 }
7119 /* does the source address already exist? if so skip it */
tuexen63fc0bb2011-12-27 12:24:52 +00007120 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00007121 atomic_add_int(&stcb->asoc.refcnt, 1);
tuexen664967a2012-06-28 16:24:36 +00007122 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007123 atomic_add_int(&stcb->asoc.refcnt, -1);
7124
7125 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
7126 /* we must add the source address */
7127 /* no scope set here since we have a tcb already. */
7128 switch (sa->sa_family) {
7129#ifdef INET
7130 case AF_INET:
t00fcxend0ad16b2013-02-09 18:34:24 +00007131 if (stcb->asoc.scope.ipv4_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007132 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
tuexendd729232011-11-01 23:04:43 +00007133 return (-1);
7134 }
7135 }
7136 break;
7137#endif
7138#ifdef INET6
7139 case AF_INET6:
t00fcxend0ad16b2013-02-09 18:34:24 +00007140 if (stcb->asoc.scope.ipv6_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007141 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
tuexendd729232011-11-01 23:04:43 +00007142 return (-2);
7143 }
7144 }
7145 break;
7146#endif
tuexen430ca6c2012-07-10 23:36:36 +00007147#if defined(__Userspace__)
7148 case AF_CONN:
t00fcxend0ad16b2013-02-09 18:34:24 +00007149 if (stcb->asoc.scope.conn_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007150 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007151 return (-2);
7152 }
tuexen430ca6c2012-07-10 23:36:36 +00007153 }
7154 break;
7155#endif
tuexendd729232011-11-01 23:04:43 +00007156 default:
7157 break;
7158 }
7159 } else {
7160 if (net_tmp != NULL && stcb_tmp == stcb) {
7161 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
7162 } else if (stcb_tmp != stcb) {
7163 /* It belongs to another association? */
7164 if (stcb_tmp)
7165 SCTP_TCB_UNLOCK(stcb_tmp);
7166 return (-3);
7167 }
7168 }
7169 if (stcb->asoc.state == 0) {
7170 /* the assoc was freed? */
7171 return (-4);
7172 }
tuexendd729232011-11-01 23:04:43 +00007173 /* now we must go through each of the params. */
Michael Tuexen473225f2017-06-23 23:05:43 +02007174 phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007175 while (phdr) {
7176 ptype = ntohs(phdr->param_type);
7177 plen = ntohs(phdr->param_length);
7178 /*
tuexencb5fe8d2012-05-04 09:50:27 +00007179 * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
tuexendd729232011-11-01 23:04:43 +00007180 * (int)plen);
7181 */
7182 if (offset + plen > limit) {
7183 break;
7184 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007185 if (plen < sizeof(struct sctp_paramhdr)) {
tuexendd729232011-11-01 23:04:43 +00007186 break;
7187 }
7188#ifdef INET
7189 if (ptype == SCTP_IPV4_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007190 if (stcb->asoc.scope.ipv4_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007191 struct sctp_ipv4addr_param *p4, p4_buf;
7192
7193 /* ok get the v4 address and check/add */
7194 phdr = sctp_get_next_param(m, offset,
7195 (struct sctp_paramhdr *)&p4_buf,
7196 sizeof(p4_buf));
7197 if (plen != sizeof(struct sctp_ipv4addr_param) ||
7198 phdr == NULL) {
7199 return (-5);
7200 }
7201 p4 = (struct sctp_ipv4addr_param *)phdr;
7202 sin.sin_addr.s_addr = p4->addr;
7203 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7204 /* Skip multi-cast addresses */
7205 goto next_param;
7206 }
7207 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
7208 (sin.sin_addr.s_addr == INADDR_ANY)) {
7209 goto next_param;
7210 }
7211 sa = (struct sockaddr *)&sin;
7212 inp = stcb->sctp_ep;
7213 atomic_add_int(&stcb->asoc.refcnt, 1);
7214 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007215 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007216 atomic_add_int(&stcb->asoc.refcnt, -1);
7217
7218 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7219 inp == NULL) {
7220 /* we must add the source address */
7221 /*
7222 * no scope set since we have a tcb
7223 * already
7224 */
7225
7226 /*
7227 * we must validate the state again
7228 * here
7229 */
7230 add_it_now:
7231 if (stcb->asoc.state == 0) {
7232 /* the assoc was freed? */
7233 return (-7);
7234 }
Michael Tuexen923f7352016-05-01 23:55:35 +02007235 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
tuexendd729232011-11-01 23:04:43 +00007236 return (-8);
7237 }
7238 } else if (stcb_tmp == stcb) {
7239 if (stcb->asoc.state == 0) {
7240 /* the assoc was freed? */
7241 return (-10);
7242 }
7243 if (net != NULL) {
7244 /* clear flag */
7245 net->dest_state &=
7246 ~SCTP_ADDR_NOT_IN_ASSOC;
7247 }
7248 } else {
7249 /*
7250 * strange, address is in another
7251 * assoc? straighten out locks.
7252 */
7253 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007254 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007255 struct mbuf *op_err;
7256 char msg[SCTP_DIAG_INFO_LEN];
7257
tuexendd729232011-11-01 23:04:43 +00007258 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007259 SCTP_SNPRINTF(msg, sizeof(msg),
7260 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007261 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7262 msg);
tuexendd729232011-11-01 23:04:43 +00007263 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tüxenac142282015-07-28 00:42:03 +02007264 stcb_tmp, op_err,
7265 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007266 goto add_it_now;
7267 }
7268 SCTP_TCB_UNLOCK(stcb_tmp);
7269 }
7270
7271 if (stcb->asoc.state == 0) {
7272 /* the assoc was freed? */
7273 return (-12);
7274 }
7275 return (-13);
7276 }
7277 }
7278 } else
7279#endif
7280#ifdef INET6
7281 if (ptype == SCTP_IPV6_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007282 if (stcb->asoc.scope.ipv6_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007283 /* ok get the v6 address and check/add */
7284 struct sctp_ipv6addr_param *p6, p6_buf;
7285
7286 phdr = sctp_get_next_param(m, offset,
7287 (struct sctp_paramhdr *)&p6_buf,
7288 sizeof(p6_buf));
7289 if (plen != sizeof(struct sctp_ipv6addr_param) ||
7290 phdr == NULL) {
7291 return (-14);
7292 }
7293 p6 = (struct sctp_ipv6addr_param *)phdr;
7294 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7295 sizeof(p6->addr));
7296 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7297 /* Skip multi-cast addresses */
7298 goto next_param;
7299 }
7300 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7301 /* Link local make no sense without scope */
7302 goto next_param;
7303 }
7304 sa = (struct sockaddr *)&sin6;
7305 inp = stcb->sctp_ep;
7306 atomic_add_int(&stcb->asoc.refcnt, 1);
7307 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007308 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007309 atomic_add_int(&stcb->asoc.refcnt, -1);
7310 if (stcb_tmp == NULL &&
7311 (inp == stcb->sctp_ep || inp == NULL)) {
7312 /*
7313 * we must validate the state again
7314 * here
7315 */
7316 add_it_now6:
7317 if (stcb->asoc.state == 0) {
7318 /* the assoc was freed? */
7319 return (-16);
7320 }
7321 /*
7322 * we must add the address, no scope
7323 * set
7324 */
Michael Tuexen923f7352016-05-01 23:55:35 +02007325 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
tuexendd729232011-11-01 23:04:43 +00007326 return (-17);
7327 }
7328 } else if (stcb_tmp == stcb) {
7329 /*
7330 * we must validate the state again
7331 * here
7332 */
7333 if (stcb->asoc.state == 0) {
7334 /* the assoc was freed? */
7335 return (-19);
7336 }
7337 if (net != NULL) {
7338 /* clear flag */
7339 net->dest_state &=
7340 ~SCTP_ADDR_NOT_IN_ASSOC;
7341 }
7342 } else {
7343 /*
7344 * strange, address is in another
7345 * assoc? straighten out locks.
7346 */
Michael Tüxenac142282015-07-28 00:42:03 +02007347 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007348 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007349 struct mbuf *op_err;
7350 char msg[SCTP_DIAG_INFO_LEN];
7351
tuexendd729232011-11-01 23:04:43 +00007352 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007353 SCTP_SNPRINTF(msg, sizeof(msg),
7354 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007355 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7356 msg);
tuexendd729232011-11-01 23:04:43 +00007357 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tüxenac142282015-07-28 00:42:03 +02007358 stcb_tmp, op_err,
7359 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007360 goto add_it_now6;
7361 }
Michael Tüxenac142282015-07-28 00:42:03 +02007362 SCTP_TCB_UNLOCK(stcb_tmp);
7363 }
tuexendd729232011-11-01 23:04:43 +00007364 if (stcb->asoc.state == 0) {
7365 /* the assoc was freed? */
7366 return (-21);
7367 }
7368 return (-22);
7369 }
7370 }
7371 } else
7372#endif
7373 if (ptype == SCTP_ECN_CAPABLE) {
t00fcxenaac0f452014-08-12 12:51:45 +00007374 peer_supports_ecn = 1;
tuexendd729232011-11-01 23:04:43 +00007375 } else if (ptype == SCTP_ULP_ADAPTATION) {
7376 if (stcb->asoc.state != SCTP_STATE_OPEN) {
7377 struct sctp_adaptation_layer_indication ai, *aip;
7378
7379 phdr = sctp_get_next_param(m, offset,
7380 (struct sctp_paramhdr *)&ai, sizeof(ai));
7381 aip = (struct sctp_adaptation_layer_indication *)phdr;
7382 if (aip) {
7383 stcb->asoc.peers_adaptation = ntohl(aip->indication);
7384 stcb->asoc.adaptation_needed = 1;
7385 }
7386 }
7387 } else if (ptype == SCTP_SET_PRIM_ADDR) {
7388 struct sctp_asconf_addr_param lstore, *fee;
7389 int lptype;
7390 struct sockaddr *lsa = NULL;
7391#ifdef INET
7392 struct sctp_asconf_addrv4_param *fii;
7393#endif
7394
t00fcxenaac0f452014-08-12 12:51:45 +00007395 if (stcb->asoc.asconf_supported == 0) {
7396 return (-100);
7397 }
tuexendd729232011-11-01 23:04:43 +00007398 if (plen > sizeof(lstore)) {
7399 return (-23);
7400 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007401 if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
7402 return (-101);
7403 }
tuexendd729232011-11-01 23:04:43 +00007404 phdr = sctp_get_next_param(m, offset,
7405 (struct sctp_paramhdr *)&lstore,
Michael Tuexen473225f2017-06-23 23:05:43 +02007406 plen);
tuexendd729232011-11-01 23:04:43 +00007407 if (phdr == NULL) {
7408 return (-24);
7409 }
7410 fee = (struct sctp_asconf_addr_param *)phdr;
7411 lptype = ntohs(fee->addrp.ph.param_type);
7412 switch (lptype) {
7413#ifdef INET
7414 case SCTP_IPV4_ADDRESS:
7415 if (plen !=
7416 sizeof(struct sctp_asconf_addrv4_param)) {
7417 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7418 (int)sizeof(struct sctp_asconf_addrv4_param),
7419 plen);
7420 } else {
7421 fii = (struct sctp_asconf_addrv4_param *)fee;
7422 sin.sin_addr.s_addr = fii->addrp.addr;
7423 lsa = (struct sockaddr *)&sin;
7424 }
7425 break;
7426#endif
7427#ifdef INET6
7428 case SCTP_IPV6_ADDRESS:
7429 if (plen !=
7430 sizeof(struct sctp_asconf_addr_param)) {
7431 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7432 (int)sizeof(struct sctp_asconf_addr_param),
7433 plen);
7434 } else {
7435 memcpy(sin6.sin6_addr.s6_addr,
7436 fee->addrp.addr,
7437 sizeof(fee->addrp.addr));
7438 lsa = (struct sockaddr *)&sin6;
7439 }
7440 break;
7441#endif
7442 default:
7443 break;
7444 }
7445 if (lsa) {
7446 (void)sctp_set_primary_addr(stcb, sa, NULL);
7447 }
7448 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7449 stcb->asoc.peer_supports_nat = 1;
7450 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7451 /* Peer supports pr-sctp */
t00fcxenaac0f452014-08-12 12:51:45 +00007452 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007453 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7454 /* A supported extension chunk */
7455 struct sctp_supported_chunk_types_param *pr_supported;
7456 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7457 int num_ent, i;
7458
Michael Tuexen473225f2017-06-23 23:05:43 +02007459 if (plen > sizeof(local_store)) {
7460 return (-35);
7461 }
tuexendd729232011-11-01 23:04:43 +00007462 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02007463 (struct sctp_paramhdr *)&local_store, plen);
tuexendd729232011-11-01 23:04:43 +00007464 if (phdr == NULL) {
7465 return (-25);
7466 }
tuexendd729232011-11-01 23:04:43 +00007467 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7468 num_ent = plen - sizeof(struct sctp_paramhdr);
7469 for (i = 0; i < num_ent; i++) {
7470 switch (pr_supported->chunk_types[i]) {
7471 case SCTP_ASCONF:
t00fcxenaac0f452014-08-12 12:51:45 +00007472 peer_supports_asconf = 1;
t00fcxen10c96982014-12-17 20:37:01 +00007473 break;
tuexendd729232011-11-01 23:04:43 +00007474 case SCTP_ASCONF_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007475 peer_supports_asconf_ack = 1;
tuexendd729232011-11-01 23:04:43 +00007476 break;
7477 case SCTP_FORWARD_CUM_TSN:
t00fcxenaac0f452014-08-12 12:51:45 +00007478 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007479 break;
7480 case SCTP_PACKET_DROPPED:
t00fcxenaac0f452014-08-12 12:51:45 +00007481 peer_supports_pktdrop = 1;
tuexendd729232011-11-01 23:04:43 +00007482 break;
7483 case SCTP_NR_SELECTIVE_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007484 peer_supports_nrsack = 1;
tuexendd729232011-11-01 23:04:43 +00007485 break;
7486 case SCTP_STREAM_RESET:
t00fcxenaac0f452014-08-12 12:51:45 +00007487 peer_supports_reconfig = 1;
tuexendd729232011-11-01 23:04:43 +00007488 break;
7489 case SCTP_AUTHENTICATION:
t00fcxenaac0f452014-08-12 12:51:45 +00007490 peer_supports_auth = 1;
tuexendd729232011-11-01 23:04:43 +00007491 break;
Michael Tuexene5001952016-04-17 19:25:27 +02007492 case SCTP_IDATA:
7493 peer_supports_idata = 1;
7494 break;
tuexendd729232011-11-01 23:04:43 +00007495 default:
7496 /* one I have not learned yet */
7497 break;
7498
7499 }
7500 }
7501 } else if (ptype == SCTP_RANDOM) {
7502 if (plen > sizeof(random_store))
7503 break;
7504 if (got_random) {
7505 /* already processed a RANDOM */
7506 goto next_param;
7507 }
7508 phdr = sctp_get_next_param(m, offset,
7509 (struct sctp_paramhdr *)random_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007510 plen);
tuexendd729232011-11-01 23:04:43 +00007511 if (phdr == NULL)
7512 return (-26);
7513 p_random = (struct sctp_auth_random *)phdr;
7514 random_len = plen - sizeof(*p_random);
7515 /* enforce the random length */
7516 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7517 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7518 return (-27);
7519 }
7520 got_random = 1;
7521 } else if (ptype == SCTP_HMAC_LIST) {
t00fcxen05d0bb92014-09-16 14:25:27 +00007522 uint16_t num_hmacs;
7523 uint16_t i;
tuexendd729232011-11-01 23:04:43 +00007524
7525 if (plen > sizeof(hmacs_store))
7526 break;
7527 if (got_hmacs) {
7528 /* already processed a HMAC list */
7529 goto next_param;
7530 }
7531 phdr = sctp_get_next_param(m, offset,
7532 (struct sctp_paramhdr *)hmacs_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007533 plen);
tuexendd729232011-11-01 23:04:43 +00007534 if (phdr == NULL)
7535 return (-28);
7536 hmacs = (struct sctp_auth_hmac_algo *)phdr;
7537 hmacs_len = plen - sizeof(*hmacs);
7538 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7539 /* validate the hmac list */
7540 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7541 return (-29);
7542 }
7543 if (stcb->asoc.peer_hmacs != NULL)
7544 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7545 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7546 if (stcb->asoc.peer_hmacs != NULL) {
7547 for (i = 0; i < num_hmacs; i++) {
7548 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7549 ntohs(hmacs->hmac_ids[i]));
7550 }
7551 }
7552 got_hmacs = 1;
7553 } else if (ptype == SCTP_CHUNK_LIST) {
7554 int i;
7555
7556 if (plen > sizeof(chunks_store))
7557 break;
7558 if (got_chklist) {
7559 /* already processed a Chunks list */
7560 goto next_param;
7561 }
7562 phdr = sctp_get_next_param(m, offset,
7563 (struct sctp_paramhdr *)chunks_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007564 plen);
tuexendd729232011-11-01 23:04:43 +00007565 if (phdr == NULL)
7566 return (-30);
7567 chunks = (struct sctp_auth_chunk_list *)phdr;
7568 num_chunks = plen - sizeof(*chunks);
7569 if (stcb->asoc.peer_auth_chunks != NULL)
7570 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7571 else
7572 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7573 for (i = 0; i < num_chunks; i++) {
7574 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7575 stcb->asoc.peer_auth_chunks);
7576 /* record asconf/asconf-ack if listed */
7577 if (chunks->chunk_types[i] == SCTP_ASCONF)
7578 saw_asconf = 1;
7579 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7580 saw_asconf_ack = 1;
7581
7582 }
7583 got_chklist = 1;
7584 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7585 (ptype == SCTP_STATE_COOKIE) ||
7586 (ptype == SCTP_UNRECOG_PARAM) ||
7587 (ptype == SCTP_COOKIE_PRESERVE) ||
7588 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7589 (ptype == SCTP_ADD_IP_ADDRESS) ||
7590 (ptype == SCTP_DEL_IP_ADDRESS) ||
7591 (ptype == SCTP_ERROR_CAUSE_IND) ||
7592 (ptype == SCTP_SUCCESS_REPORT)) {
7593 /* don't care */ ;
7594 } else {
7595 if ((ptype & 0x8000) == 0x0000) {
7596 /*
7597 * must stop processing the rest of the
7598 * param's. Any report bits were handled
7599 * with the call to
7600 * sctp_arethere_unrecognized_parameters()
7601 * when the INIT or INIT-ACK was first seen.
7602 */
7603 break;
7604 }
7605 }
tuexen09631162012-04-18 11:23:02 +00007606
tuexendd729232011-11-01 23:04:43 +00007607 next_param:
7608 offset += SCTP_SIZE32(plen);
7609 if (offset >= limit) {
7610 break;
7611 }
Michael Tuexen473225f2017-06-23 23:05:43 +02007612 phdr = sctp_get_next_param(m, offset, &param_buf,
7613 sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007614 }
7615 /* Now check to see if we need to purge any addresses */
7616 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7617 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7618 SCTP_ADDR_NOT_IN_ASSOC) {
7619 /* This address has been removed from the asoc */
7620 /* remove and free it */
7621 stcb->asoc.numnets--;
7622 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
7623 sctp_free_remote_addr(net);
7624 if (net == stcb->asoc.primary_destination) {
7625 stcb->asoc.primary_destination = NULL;
7626 sctp_select_primary_destination(stcb);
7627 }
7628 }
7629 }
t00fcxenaac0f452014-08-12 12:51:45 +00007630 if ((stcb->asoc.ecn_supported == 1) &&
7631 (peer_supports_ecn == 0)) {
7632 stcb->asoc.ecn_supported = 0;
tuexendd729232011-11-01 23:04:43 +00007633 }
t00fcxenaac0f452014-08-12 12:51:45 +00007634 if ((stcb->asoc.prsctp_supported == 1) &&
7635 (peer_supports_prsctp == 0)) {
7636 stcb->asoc.prsctp_supported = 0;
7637 }
7638 if ((stcb->asoc.auth_supported == 1) &&
7639 ((peer_supports_auth == 0) ||
7640 (got_random == 0) || (got_hmacs == 0))) {
7641 stcb->asoc.auth_supported = 0;
7642 }
7643 if ((stcb->asoc.asconf_supported == 1) &&
7644 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
7645 (stcb->asoc.auth_supported == 0) ||
7646 (saw_asconf == 0) || (saw_asconf_ack == 0))) {
7647 stcb->asoc.asconf_supported = 0;
7648 }
7649 if ((stcb->asoc.reconfig_supported == 1) &&
7650 (peer_supports_reconfig == 0)) {
7651 stcb->asoc.reconfig_supported = 0;
7652 }
Michael Tuexene5001952016-04-17 19:25:27 +02007653 if ((stcb->asoc.idata_supported == 1) &&
7654 (peer_supports_idata == 0)) {
7655 stcb->asoc.idata_supported = 0;
7656 }
t00fcxenaac0f452014-08-12 12:51:45 +00007657 if ((stcb->asoc.nrsack_supported == 1) &&
7658 (peer_supports_nrsack == 0)) {
7659 stcb->asoc.nrsack_supported = 0;
7660 }
7661 if ((stcb->asoc.pktdrop_supported == 1) &&
7662 (peer_supports_pktdrop == 0)){
7663 stcb->asoc.pktdrop_supported = 0;
7664 }
7665 /* validate authentication required parameters */
7666 if ((peer_supports_auth == 0) && (got_chklist == 1)) {
tuexendd729232011-11-01 23:04:43 +00007667 /* peer does not support auth but sent a chunks list? */
7668 return (-31);
7669 }
t00fcxenaac0f452014-08-12 12:51:45 +00007670 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
tuexendd729232011-11-01 23:04:43 +00007671 /* peer supports asconf but not auth? */
7672 return (-32);
t00fcxenaac0f452014-08-12 12:51:45 +00007673 } else if ((peer_supports_asconf == 1) &&
7674 (peer_supports_auth == 1) &&
tuexendd729232011-11-01 23:04:43 +00007675 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7676 return (-33);
7677 }
7678 /* concatenate the full random key */
7679 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7680 if (chunks != NULL) {
7681 keylen += sizeof(*chunks) + num_chunks;
7682 }
7683 new_key = sctp_alloc_key(keylen);
7684 if (new_key != NULL) {
7685 /* copy in the RANDOM */
7686 if (p_random != NULL) {
7687 keylen = sizeof(*p_random) + random_len;
Michael Tuexend0853302017-07-19 17:01:31 +02007688 memcpy(new_key->key, p_random, keylen);
Michael Tuexen8789a6d2018-06-02 18:33:47 +02007689 } else {
7690 keylen = 0;
tuexendd729232011-11-01 23:04:43 +00007691 }
7692 /* append in the AUTH chunks */
7693 if (chunks != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007694 memcpy(new_key->key + keylen, chunks,
7695 sizeof(*chunks) + num_chunks);
tuexendd729232011-11-01 23:04:43 +00007696 keylen += sizeof(*chunks) + num_chunks;
7697 }
7698 /* append in the HMACs */
7699 if (hmacs != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007700 memcpy(new_key->key + keylen, hmacs,
7701 sizeof(*hmacs) + hmacs_len);
tuexendd729232011-11-01 23:04:43 +00007702 }
7703 } else {
7704 /* failed to get memory for the key */
7705 return (-34);
7706 }
7707 if (stcb->asoc.authinfo.peer_random != NULL)
7708 sctp_free_key(stcb->asoc.authinfo.peer_random);
7709 stcb->asoc.authinfo.peer_random = new_key;
7710 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7711 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7712
7713 return (0);
7714}
7715
7716int
7717sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7718 struct sctp_nets *net)
7719{
7720 /* make sure the requested primary address exists in the assoc */
7721 if (net == NULL && sa)
7722 net = sctp_findnet(stcb, sa);
7723
7724 if (net == NULL) {
7725 /* didn't find the requested primary address! */
7726 return (-1);
7727 } else {
7728 /* set the primary address */
7729 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7730 /* Must be confirmed, so queue to set */
7731 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7732 return (0);
7733 }
7734 stcb->asoc.primary_destination = net;
7735 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
7736 sctp_free_remote_addr(stcb->asoc.alternate);
7737 stcb->asoc.alternate = NULL;
7738 }
7739 net = TAILQ_FIRST(&stcb->asoc.nets);
7740 if (net != stcb->asoc.primary_destination) {
7741 /* first one on the list is NOT the primary
7742 * sctp_cmpaddr() is much more efficient if
7743 * the primary is the first on the list, make it
7744 * so.
7745 */
7746 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7747 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7748 }
7749 return (0);
7750 }
7751}
7752
7753int
tuexen9784e9a2011-12-18 13:04:23 +00007754sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
tuexendd729232011-11-01 23:04:43 +00007755{
7756 /*
7757 * This function serves two purposes. It will see if a TAG can be
7758 * re-used and return 1 for yes it is ok and 0 for don't use that
7759 * tag. A secondary function it will do is purge out old tags that
7760 * can be removed.
7761 */
7762 struct sctpvtaghead *chain;
7763 struct sctp_tagblock *twait_block;
7764 struct sctpasochead *head;
7765 struct sctp_tcb *stcb;
7766 int i;
7767
7768 SCTP_INP_INFO_RLOCK();
7769 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
7770 SCTP_BASE_INFO(hashasocmark))];
tuexendd729232011-11-01 23:04:43 +00007771 LIST_FOREACH(stcb, head, sctp_asocs) {
7772 /* We choose not to lock anything here. TCB's can't be
7773 * removed since we have the read lock, so they can't
7774 * be freed on us, same thing for the INP. I may
7775 * be wrong with this assumption, but we will go
7776 * with it for now :-)
7777 */
7778 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7779 continue;
7780 }
7781 if (stcb->asoc.my_vtag == tag) {
7782 /* candidate */
7783 if (stcb->rport != rport) {
7784 continue;
7785 }
7786 if (stcb->sctp_ep->sctp_lport != lport) {
7787 continue;
7788 }
7789 /* Its a used tag set */
7790 SCTP_INP_INFO_RUNLOCK();
7791 return (0);
7792 }
7793 }
tuexendd729232011-11-01 23:04:43 +00007794 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
7795 /* Now what about timed wait ? */
t00fcxen14f8fbb2013-06-25 09:32:47 +00007796 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
tuexendd729232011-11-01 23:04:43 +00007797 /*
7798 * Block(s) are present, lets see if we have this tag in the
7799 * list
7800 */
t00fcxen14f8fbb2013-06-25 09:32:47 +00007801 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
7802 if (twait_block->vtag_block[i].v_tag == 0) {
7803 /* not used */
7804 continue;
7805 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
7806 now->tv_sec) {
7807 /* Audit expires this guy */
7808 twait_block->vtag_block[i].tv_sec_at_expire = 0;
7809 twait_block->vtag_block[i].v_tag = 0;
7810 twait_block->vtag_block[i].lport = 0;
7811 twait_block->vtag_block[i].rport = 0;
7812 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
7813 (twait_block->vtag_block[i].lport == lport) &&
7814 (twait_block->vtag_block[i].rport == rport)) {
7815 /* Bad tag, sorry :< */
7816 SCTP_INP_INFO_RUNLOCK();
7817 return (0);
tuexendd729232011-11-01 23:04:43 +00007818 }
7819 }
7820 }
7821 SCTP_INP_INFO_RUNLOCK();
7822 return (1);
7823}
7824
tuexendd729232011-11-01 23:04:43 +00007825static void
tuexen9784e9a2011-12-18 13:04:23 +00007826sctp_drain_mbufs(struct sctp_tcb *stcb)
tuexendd729232011-11-01 23:04:43 +00007827{
7828 /*
7829 * We must hunt this association for MBUF's past the cumack (i.e.
7830 * out of order data that we can renege on).
7831 */
7832 struct sctp_association *asoc;
7833 struct sctp_tmit_chunk *chk, *nchk;
7834 uint32_t cumulative_tsn_p1;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007835 struct sctp_queued_to_read *control, *ncontrol;
tuexendd729232011-11-01 23:04:43 +00007836 int cnt, strmat;
7837 uint32_t gap, i;
tuexen63fc0bb2011-12-27 12:24:52 +00007838 int fnd = 0;
tuexendd729232011-11-01 23:04:43 +00007839
7840 /* We look for anything larger than the cum-ack + 1 */
7841
7842 asoc = &stcb->asoc;
7843 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7844 /* none we can reneg on. */
7845 return;
7846 }
7847 SCTP_STAT_INCR(sctps_protocol_drains_done);
7848 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7849 cnt = 0;
tuexendd729232011-11-01 23:04:43 +00007850 /* Ok that was fun, now we will drain all the inbound streams? */
7851 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007852 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007853#ifdef INVARIANTS
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007854 if (control->on_strm_q != SCTP_ON_ORDERED ) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007855 panic("Huh control: %p on_q: %d -- not ordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007856 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007857 }
7858#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007859 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
tuexendd729232011-11-01 23:04:43 +00007860 /* Yep it is above cum-ack */
7861 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007862 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7863 KASSERT(control->length > 0, ("control has zero length"));
7864 if (asoc->size_on_all_streams >= control->length) {
7865 asoc->size_on_all_streams -= control->length;
7866 } else {
7867#ifdef INVARIANTS
7868 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7869#else
7870 asoc->size_on_all_streams = 0;
7871#endif
7872 }
tuexendd729232011-11-01 23:04:43 +00007873 sctp_ucount_decr(asoc->cnt_on_all_streams);
7874 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007875 if (control->on_read_q) {
7876 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7877 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007878 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007879 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
7880 control->on_strm_q = 0;
7881 if (control->data) {
7882 sctp_m_freem(control->data);
7883 control->data = NULL;
tuexendd729232011-11-01 23:04:43 +00007884 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007885 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007886 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007887 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007888 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007889 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007890 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7891 if (asoc->size_on_reasm_queue >= chk->send_size) {
7892 asoc->size_on_reasm_queue -= chk->send_size;
7893 } else {
7894#ifdef INVARIANTS
7895 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7896#else
7897 asoc->size_on_reasm_queue = 0;
7898#endif
7899 }
Michael Tuexene5001952016-04-17 19:25:27 +02007900 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7901 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007902 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02007903 if (chk->data) {
7904 sctp_m_freem(chk->data);
7905 chk->data = NULL;
7906 }
7907 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7908 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007909 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02007910 }
7911 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007912 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007913#ifdef INVARIANTS
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007914 if (control->on_strm_q != SCTP_ON_UNORDERED ) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007915 panic("Huh control: %p on_q: %d -- not unordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007916 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007917 }
7918#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007919 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
Michael Tuexene5001952016-04-17 19:25:27 +02007920 /* Yep it is above cum-ack */
7921 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007922 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7923 KASSERT(control->length > 0, ("control has zero length"));
7924 if (asoc->size_on_all_streams >= control->length) {
7925 asoc->size_on_all_streams -= control->length;
7926 } else {
7927#ifdef INVARIANTS
7928 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7929#else
7930 asoc->size_on_all_streams = 0;
7931#endif
7932 }
Michael Tuexene5001952016-04-17 19:25:27 +02007933 sctp_ucount_decr(asoc->cnt_on_all_streams);
7934 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007935 if (control->on_read_q) {
7936 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7937 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007938 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007939 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
7940 control->on_strm_q = 0;
7941 if (control->data) {
7942 sctp_m_freem(control->data);
7943 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02007944 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007945 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007946 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007947 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007948 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007949 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007950 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7951 if (asoc->size_on_reasm_queue >= chk->send_size) {
7952 asoc->size_on_reasm_queue -= chk->send_size;
7953 } else {
7954#ifdef INVARIANTS
7955 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7956#else
7957 asoc->size_on_reasm_queue = 0;
7958#endif
7959 }
Michael Tuexene5001952016-04-17 19:25:27 +02007960 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7961 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007962 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02007963 if (chk->data) {
7964 sctp_m_freem(chk->data);
7965 chk->data = NULL;
7966 }
7967 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7968 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007969 sctp_free_a_readq(stcb, control);
tuexendd729232011-11-01 23:04:43 +00007970 }
7971 }
7972 }
7973 if (cnt) {
7974 /* We must back down to see what the new highest is */
7975 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
7976 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
7977 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
7978 asoc->highest_tsn_inside_map = i;
7979 fnd = 1;
7980 break;
7981 }
7982 }
7983 if (!fnd) {
7984 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
7985 }
7986
7987 /*
7988 * Question, should we go through the delivery queue? The only
7989 * reason things are on here is the app not reading OR a p-d-api up.
7990 * An attacker COULD send enough in to initiate the PD-API and then
7991 * send a bunch of stuff to other streams... these would wind up on
7992 * the delivery queue.. and then we would not get to them. But in
7993 * order to do this I then have to back-track and un-deliver
7994 * sequence numbers in streams.. el-yucko. I think for now we will
7995 * NOT look at the delivery queue and leave it to be something to
7996 * consider later. An alternative would be to abort the P-D-API with
7997 * a notification and then deliver the data.... Or another method
7998 * might be to keep track of how many times the situation occurs and
7999 * if we see a possible attack underway just abort the association.
8000 */
8001#ifdef SCTP_DEBUG
8002 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
8003#endif
8004 /*
8005 * Now do we need to find a new
8006 * asoc->highest_tsn_inside_map?
8007 */
8008 asoc->last_revoke_count = cnt;
Michael Tuexen61f66832020-02-09 23:15:19 +01008009 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
Michael Tuexen21b55df2020-02-09 23:46:32 +01008010 SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
tuexendd729232011-11-01 23:04:43 +00008011 /*sa_ignore NO_NULL_CHK*/
8012 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
8013 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00008014 }
8015 /*
8016 * Another issue, in un-setting the TSN's in the mapping array we
8017 * DID NOT adjust the highest_tsn marker. This will cause one of two
8018 * things to occur. It may cause us to do extra work in checking for
8019 * our mapping array movement. More importantly it may cause us to
8020 * SACK every datagram. This may not be a bad thing though since we
8021 * will recover once we get our cum-ack above and all this stuff we
8022 * dumped recovered.
8023 */
8024}
8025
8026void
8027sctp_drain()
8028{
8029 /*
8030 * We must walk the PCB lists for ALL associations here. The system
8031 * is LOW on MBUF's and needs help. This is where reneging will
8032 * occur. We really hope this does NOT happen!
8033 */
8034#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8035 VNET_ITERATOR_DECL(vnet_iter);
8036#else
8037 struct sctp_inpcb *inp;
8038 struct sctp_tcb *stcb;
8039
8040 SCTP_STAT_INCR(sctps_protocol_drain_calls);
8041 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
8042 return;
8043 }
8044#endif
8045#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8046 VNET_LIST_RLOCK_NOSLEEP();
8047 VNET_FOREACH(vnet_iter) {
8048 CURVNET_SET(vnet_iter);
8049 struct sctp_inpcb *inp;
8050 struct sctp_tcb *stcb;
8051#endif
8052
8053#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8054 SCTP_STAT_INCR(sctps_protocol_drain_calls);
8055 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
8056#ifdef VIMAGE
8057 continue;
8058#else
8059 return;
tuexen09631162012-04-18 11:23:02 +00008060#endif
tuexendd729232011-11-01 23:04:43 +00008061 }
8062#endif
8063 SCTP_INP_INFO_RLOCK();
8064 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
8065 /* For each endpoint */
8066 SCTP_INP_RLOCK(inp);
8067 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
8068 /* For each association */
8069 SCTP_TCB_LOCK(stcb);
tuexen9784e9a2011-12-18 13:04:23 +00008070 sctp_drain_mbufs(stcb);
tuexendd729232011-11-01 23:04:43 +00008071 SCTP_TCB_UNLOCK(stcb);
8072 }
8073 SCTP_INP_RUNLOCK(inp);
8074 }
8075 SCTP_INP_INFO_RUNLOCK();
8076#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8077 CURVNET_RESTORE();
8078 }
8079 VNET_LIST_RUNLOCK_NOSLEEP();
8080#endif
8081}
8082
8083/*
8084 * start a new iterator
8085 * iterates through all endpoints and associations based on the pcb_state
8086 * flags and asoc_state. "af" (mandatory) is executed for all matching
8087 * assocs and "ef" (optional) is executed when the iterator completes.
8088 * "inpf" (optional) is executed for each new endpoint as it is being
8089 * iterated through. inpe (optional) is called when the inp completes
8090 * its way through all the stcbs.
8091 */
8092int
8093sctp_initiate_iterator(inp_func inpf,
8094 asoc_func af,
8095 inp_func inpe,
8096 uint32_t pcb_state,
8097 uint32_t pcb_features,
8098 uint32_t asoc_state,
8099 void *argp,
8100 uint32_t argi,
8101 end_func ef,
8102 struct sctp_inpcb *s_inp,
8103 uint8_t chunk_output_off)
8104{
8105 struct sctp_iterator *it = NULL;
8106
8107 if (af == NULL) {
8108 return (-1);
8109 }
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008110 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8111 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
8112 SCTP_BASE_VAR(sctp_pcb_initialized));
8113 return (-1);
8114 }
tuexendd729232011-11-01 23:04:43 +00008115 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
8116 SCTP_M_ITER);
8117 if (it == NULL) {
8118 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
Michael Tuexen287284c2020-01-12 14:34:21 +01008119 return (-1);
tuexendd729232011-11-01 23:04:43 +00008120 }
8121 memset(it, 0, sizeof(*it));
8122 it->function_assoc = af;
8123 it->function_inp = inpf;
8124 if (inpf)
8125 it->done_current_ep = 0;
8126 else
8127 it->done_current_ep = 1;
8128 it->function_atend = ef;
8129 it->pointer = argp;
8130 it->val = argi;
8131 it->pcb_flags = pcb_state;
8132 it->pcb_features = pcb_features;
8133 it->asoc_state = asoc_state;
8134 it->function_inp_end = inpe;
8135 it->no_chunk_output = chunk_output_off;
8136#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8137 it->vn = curvnet;
tuexen09631162012-04-18 11:23:02 +00008138#endif
tuexendd729232011-11-01 23:04:43 +00008139 if (s_inp) {
8140 /* Assume lock is held here */
8141 it->inp = s_inp;
8142 SCTP_INP_INCR_REF(it->inp);
8143 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
8144 } else {
8145 SCTP_INP_INFO_RLOCK();
8146 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
8147 if (it->inp) {
8148 SCTP_INP_INCR_REF(it->inp);
8149 }
8150 SCTP_INP_INFO_RUNLOCK();
8151 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
8152
8153 }
8154 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008155 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8156 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8157 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
8158 SCTP_BASE_VAR(sctp_pcb_initialized), it);
8159 SCTP_FREE(it, SCTP_M_ITER);
8160 return (-1);
8161 }
tuexendd729232011-11-01 23:04:43 +00008162 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
8163 if (sctp_it_ctl.iterator_running == 0) {
8164 sctp_wakeup_iterator();
8165 }
8166 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8167 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
8168 return (0);
8169}