blob: 5fa47088ec467019eb1889f497df8e88556f6f11 [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
841 * if its given. We do this especially for
842 * panda who might recycle indexes fast.
843 */
844 if (if_name) {
tuexenb740ef62011-11-13 11:58:06 +0000845 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
846 /* They match its a correct delete */
847 valid = 1;
tuexendd729232011-11-01 23:04:43 +0000848 }
849 }
850 if (!valid) {
851 /* last ditch check ifn_index */
852 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
853 valid = 1;
854 }
855 }
856 if (!valid) {
857 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
858 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
859 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
860 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
861 SCTP_IPI_ADDR_WUNLOCK();
862 return;
863 }
864 }
t00fcxen8fca7ff2012-09-05 18:58:42 +0000865 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000866 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
t00fcxen97887d62014-03-29 21:31:12 +0000867 /*
868 * We don't set the flag. This means that the structure will
869 * hang around in EP's that have bound specific to it until
870 * they close. This gives us TCP like behavior if someone
871 * removes an address (or for that matter adds it right back).
872 */
873 /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */
tuexendd729232011-11-01 23:04:43 +0000874 vrf->total_ifa_count--;
875 LIST_REMOVE(sctp_ifap, next_bucket);
876 sctp_remove_ifa_from_ifn(sctp_ifap);
877 }
878#ifdef SCTP_DEBUG
879 else {
880 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
881 ifn_index);
882 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
883 }
884#endif
885
886 out_now:
887 SCTP_IPI_ADDR_WUNLOCK();
888 if (sctp_ifap) {
889 struct sctp_laddr *wi;
890
891 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
892 if (wi == NULL) {
893 /*
894 * Gak, what can we do? We have lost an address
895 * change can you say HOSED?
896 */
897 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
898
899 /* Oops, must decrement the count */
900 sctp_free_ifa(sctp_ifap);
901 return;
902 }
903 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +0200904 memset(wi, 0, sizeof(*wi));
tuexendd729232011-11-01 23:04:43 +0000905 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
906 wi->ifa = sctp_ifap;
907 wi->action = SCTP_DEL_IP_ADDRESS;
908 SCTP_WQ_ADDR_LOCK();
909 /*
910 * Should this really be a tailq? As it is we will process the
911 * newest first :-0
912 */
913 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
tuexendd729232011-11-01 23:04:43 +0000914 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
915 (struct sctp_inpcb *)NULL,
916 (struct sctp_tcb *)NULL,
917 (struct sctp_nets *)NULL);
Michael Tuexen96939f12017-09-20 23:31:24 +0200918 SCTP_WQ_ADDR_UNLOCK();
tuexendd729232011-11-01 23:04:43 +0000919 }
920 return;
921}
922
923
t00fcxen60d0dea2013-02-10 20:04:03 +0000924static int
925sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
926{
t00fcxenaff14b62013-11-02 18:02:02 +0000927 int loopback_scope;
928#if defined(INET)
929 int ipv4_local_scope, ipv4_addr_legal;
930#endif
931#if defined(INET6)
932 int local_scope, site_scope, ipv6_addr_legal;
933#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000934#if defined(__Userspace__)
935 int conn_addr_legal;
936#endif
937 struct sctp_vrf *vrf;
938 struct sctp_ifn *sctp_ifn;
939 struct sctp_ifa *sctp_ifa;
940
941 loopback_scope = stcb->asoc.scope.loopback_scope;
t00fcxenaff14b62013-11-02 18:02:02 +0000942#if defined(INET)
t00fcxen60d0dea2013-02-10 20:04:03 +0000943 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
t00fcxenaff14b62013-11-02 18:02:02 +0000944 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
945#endif
946#if defined(INET6)
t00fcxen60d0dea2013-02-10 20:04:03 +0000947 local_scope = stcb->asoc.scope.local_scope;
948 site_scope = stcb->asoc.scope.site_scope;
t00fcxen60d0dea2013-02-10 20:04:03 +0000949 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
t00fcxenaff14b62013-11-02 18:02:02 +0000950#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000951#if defined(__Userspace__)
952 conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
953#endif
954
955 SCTP_IPI_ADDR_RLOCK();
956 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
957 if (vrf == NULL) {
958 /* no vrf, no addresses */
959 SCTP_IPI_ADDR_RUNLOCK();
960 return (0);
961 }
962
963 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
964 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
965 if ((loopback_scope == 0) &&
966 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
967 continue;
968 }
969 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
970 if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
971 (!sctp_is_addr_pending(stcb, sctp_ifa))) {
972 /* We allow pending addresses, where we
973 * have sent an asconf-add to be considered
974 * valid.
975 */
976 continue;
977 }
t00fcxenba8aa202013-11-16 15:37:17 +0000978 if (sctp_ifa->address.sa.sa_family != to->sa_family) {
979 continue;
980 }
t00fcxen60d0dea2013-02-10 20:04:03 +0000981 switch (sctp_ifa->address.sa.sa_family) {
982#ifdef INET
983 case AF_INET:
984 if (ipv4_addr_legal) {
985 struct sockaddr_in *sin, *rsin;
986
987 sin = &sctp_ifa->address.sin;
988 rsin = (struct sockaddr_in *)to;
989 if ((ipv4_local_scope == 0) &&
990 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
991 continue;
992 }
t00fcxen326aa4b2014-06-20 13:42:13 +0000993#if defined(__FreeBSD__)
994 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
995 &sin->sin_addr) != 0) {
996 continue;
997 }
998#endif
t00fcxen60d0dea2013-02-10 20:04:03 +0000999 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1000 SCTP_IPI_ADDR_RUNLOCK();
1001 return (1);
1002 }
1003 }
1004 break;
1005#endif
1006#ifdef INET6
1007 case AF_INET6:
1008 if (ipv6_addr_legal) {
1009 struct sockaddr_in6 *sin6, *rsin6;
1010#if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
1011 struct sockaddr_in6 lsa6;
1012#endif
1013 sin6 = &sctp_ifa->address.sin6;
1014 rsin6 = (struct sockaddr_in6 *)to;
t00fcxen326aa4b2014-06-20 13:42:13 +00001015#if defined(__FreeBSD__)
1016 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
1017 &sin6->sin6_addr) != 0) {
1018 continue;
1019 }
1020#endif
t00fcxen60d0dea2013-02-10 20:04:03 +00001021 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1022 if (local_scope == 0)
1023 continue;
1024#if defined(SCTP_EMBEDDED_V6_SCOPE)
1025 if (sin6->sin6_scope_id == 0) {
1026#ifdef SCTP_KAME
1027 if (sa6_recoverscope(sin6) != 0)
1028 continue;
1029#else
1030 lsa6 = *sin6;
1031 if (in6_recoverscope(&lsa6,
1032 &lsa6.sin6_addr,
1033 NULL))
1034 continue;
1035 sin6 = &lsa6;
1036#endif /* SCTP_KAME */
1037 }
1038#endif /* SCTP_EMBEDDED_V6_SCOPE */
1039 }
1040 if ((site_scope == 0) &&
1041 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1042 continue;
1043 }
1044 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1045 SCTP_IPI_ADDR_RUNLOCK();
1046 return (1);
1047 }
1048 }
1049 break;
1050#endif
1051#if defined(__Userspace__)
1052 case AF_CONN:
1053 if (conn_addr_legal) {
1054 struct sockaddr_conn *sconn, *rsconn;
1055
1056 sconn = &sctp_ifa->address.sconn;
1057 rsconn = (struct sockaddr_conn *)to;
1058 if (sconn->sconn_addr == rsconn->sconn_addr) {
1059 SCTP_IPI_ADDR_RUNLOCK();
1060 return (1);
1061 }
1062 }
1063 break;
1064#endif
1065 default:
1066 /* TSNH */
1067 break;
1068 }
1069 }
1070 }
1071 } else {
1072 struct sctp_laddr *laddr;
1073
1074 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1075 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1076 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1077 continue;
1078 }
1079 if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
1080 (!sctp_is_addr_pending(stcb, laddr->ifa))) {
1081 /* We allow pending addresses, where we
1082 * have sent an asconf-add to be considered
1083 * valid.
1084 */
1085 continue;
1086 }
1087 if (laddr->ifa->address.sa.sa_family != to->sa_family) {
1088 continue;
1089 }
1090 switch (to->sa_family) {
1091#ifdef INET
1092 case AF_INET:
1093 {
1094 struct sockaddr_in *sin, *rsin;
1095
t00fcxen05f07c02014-09-07 10:15:26 +00001096 sin = &laddr->ifa->address.sin;
t00fcxen60d0dea2013-02-10 20:04:03 +00001097 rsin = (struct sockaddr_in *)to;
1098 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1099 SCTP_IPI_ADDR_RUNLOCK();
1100 return (1);
1101 }
1102 break;
1103 }
1104#endif
1105#ifdef INET6
1106 case AF_INET6:
1107 {
1108 struct sockaddr_in6 *sin6, *rsin6;
1109
t00fcxen05f07c02014-09-07 10:15:26 +00001110 sin6 = &laddr->ifa->address.sin6;
t00fcxen60d0dea2013-02-10 20:04:03 +00001111 rsin6 = (struct sockaddr_in6 *)to;
1112 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1113 SCTP_IPI_ADDR_RUNLOCK();
1114 return (1);
1115 }
1116 break;
1117 }
1118
1119#endif
1120#if defined(__Userspace__)
1121 case AF_CONN:
1122 {
1123 struct sockaddr_conn *sconn, *rsconn;
1124
t00fcxen05f07c02014-09-07 10:15:26 +00001125 sconn = &laddr->ifa->address.sconn;
t00fcxen60d0dea2013-02-10 20:04:03 +00001126 rsconn = (struct sockaddr_conn *)to;
1127 if (sconn->sconn_addr == rsconn->sconn_addr) {
1128 SCTP_IPI_ADDR_RUNLOCK();
1129 return (1);
1130 }
1131 break;
1132 }
1133#endif
1134 default:
1135 /* TSNH */
1136 break;
1137 }
1138
1139 }
1140 }
1141 SCTP_IPI_ADDR_RUNLOCK();
1142 return (0);
1143}
1144
1145
tuexendd729232011-11-01 23:04:43 +00001146static struct sctp_tcb *
1147sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
1148 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
1149{
1150 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
1151 /*
1152 * If we support the TCP model, then we must now dig through to see
1153 * if we can find our endpoint in the list of tcp ep's.
1154 */
1155 uint16_t lport, rport;
1156 struct sctppcbhead *ephead;
1157 struct sctp_inpcb *inp;
1158 struct sctp_laddr *laddr;
1159 struct sctp_tcb *stcb;
1160 struct sctp_nets *net;
1161#ifdef SCTP_MVRF
1162 int fnd, i;
1163#endif
t00fcxend0ad16b2013-02-09 18:34:24 +00001164
tuexendd729232011-11-01 23:04:43 +00001165 if ((to == NULL) || (from == NULL)) {
1166 return (NULL);
1167 }
1168
1169 switch (to->sa_family) {
1170#ifdef INET
1171 case AF_INET:
1172 if (from->sa_family == AF_INET) {
1173 lport = ((struct sockaddr_in *)to)->sin_port;
1174 rport = ((struct sockaddr_in *)from)->sin_port;
1175 } else {
1176 return (NULL);
1177 }
1178 break;
1179#endif
1180#ifdef INET6
1181 case AF_INET6:
1182 if (from->sa_family == AF_INET6) {
1183 lport = ((struct sockaddr_in6 *)to)->sin6_port;
1184 rport = ((struct sockaddr_in6 *)from)->sin6_port;
1185 } else {
1186 return (NULL);
1187 }
1188 break;
1189#endif
tuexen5d9b9742012-07-11 21:52:01 +00001190#if defined(__Userspace__)
1191 case AF_CONN:
1192 if (from->sa_family == AF_CONN) {
1193 lport = ((struct sockaddr_conn *)to)->sconn_port;
1194 rport = ((struct sockaddr_conn *)from)->sconn_port;
1195 } else {
1196 return (NULL);
1197 }
1198 break;
1199#endif
tuexendd729232011-11-01 23:04:43 +00001200 default:
1201 return (NULL);
1202 }
1203 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
1204 /*
1205 * Ok now for each of the guys in this bucket we must look and see:
1206 * - Does the remote port match. - Does there single association's
1207 * addresses match this address (to). If so we update p_ep to point
1208 * to this ep and return the tcb from it.
1209 */
1210 LIST_FOREACH(inp, ephead, sctp_hash) {
1211 SCTP_INP_RLOCK(inp);
1212 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1213 SCTP_INP_RUNLOCK(inp);
1214 continue;
1215 }
1216 if (lport != inp->sctp_lport) {
1217 SCTP_INP_RUNLOCK(inp);
1218 continue;
1219 }
t00fcxen326aa4b2014-06-20 13:42:13 +00001220#if defined(__FreeBSD__)
1221 switch (to->sa_family) {
1222#ifdef INET
1223 case AF_INET:
1224 {
1225 struct sockaddr_in *sin;
1226
1227 sin = (struct sockaddr_in *)to;
1228 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1229 &sin->sin_addr) != 0) {
1230 SCTP_INP_RUNLOCK(inp);
1231 continue;
1232 }
1233 break;
1234 }
1235#endif
1236#ifdef INET6
1237 case AF_INET6:
1238 {
1239 struct sockaddr_in6 *sin6;
1240
1241 sin6 = (struct sockaddr_in6 *)to;
1242 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1243 &sin6->sin6_addr) != 0) {
1244 SCTP_INP_RUNLOCK(inp);
1245 continue;
1246 }
1247 break;
1248 }
1249#endif
1250 default:
1251 SCTP_INP_RUNLOCK(inp);
1252 continue;
1253 }
1254#endif
tuexendd729232011-11-01 23:04:43 +00001255#ifdef SCTP_MVRF
1256 fnd = 0;
tuexen9784e9a2011-12-18 13:04:23 +00001257 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00001258 if (inp->m_vrf_ids[i] == vrf_id) {
1259 fnd = 1;
1260 break;
1261 }
1262 }
1263 if (fnd == 0) {
1264 SCTP_INP_RUNLOCK(inp);
1265 continue;
1266 }
1267#else
1268 if (inp->def_vrf_id != vrf_id) {
1269 SCTP_INP_RUNLOCK(inp);
1270 continue;
1271 }
1272#endif
1273 /* check to see if the ep has one of the addresses */
1274 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1275 /* We are NOT bound all, so look further */
1276 int match = 0;
1277
1278 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1279
1280 if (laddr->ifa == NULL) {
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02001281 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__);
tuexendd729232011-11-01 23:04:43 +00001282 continue;
1283 }
1284 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1285 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1286 continue;
1287 }
1288 if (laddr->ifa->address.sa.sa_family ==
1289 to->sa_family) {
1290 /* see if it matches */
tuexendd729232011-11-01 23:04:43 +00001291#ifdef INET
1292 if (from->sa_family == AF_INET) {
1293 struct sockaddr_in *intf_addr, *sin;
1294
1295 intf_addr = &laddr->ifa->address.sin;
1296 sin = (struct sockaddr_in *)to;
1297 if (sin->sin_addr.s_addr ==
1298 intf_addr->sin_addr.s_addr) {
1299 match = 1;
1300 break;
1301 }
1302 }
1303#endif
1304#ifdef INET6
1305 if (from->sa_family == AF_INET6) {
1306 struct sockaddr_in6 *intf_addr6;
1307 struct sockaddr_in6 *sin6;
1308
1309 sin6 = (struct sockaddr_in6 *)
1310 to;
1311 intf_addr6 = &laddr->ifa->address.sin6;
1312
1313 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1314 intf_addr6)) {
1315 match = 1;
1316 break;
1317 }
1318 }
1319#endif
tuexen5d9b9742012-07-11 21:52:01 +00001320#if defined(__Userspace__)
1321 if (from->sa_family == AF_CONN) {
1322 struct sockaddr_conn *intf_addr, *sconn;
1323
1324 intf_addr = &laddr->ifa->address.sconn;
1325 sconn = (struct sockaddr_conn *)to;
1326 if (sconn->sconn_addr ==
1327 intf_addr->sconn_addr) {
1328 match = 1;
1329 break;
1330 }
1331 }
1332#endif
tuexendd729232011-11-01 23:04:43 +00001333 }
1334 }
1335 if (match == 0) {
1336 /* This endpoint does not have this address */
1337 SCTP_INP_RUNLOCK(inp);
1338 continue;
1339 }
1340 }
1341 /*
1342 * Ok if we hit here the ep has the address, does it hold
1343 * the tcb?
1344 */
t00fcxen60d0dea2013-02-10 20:04:03 +00001345 /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
tuexendd729232011-11-01 23:04:43 +00001346 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1347 if (stcb == NULL) {
1348 SCTP_INP_RUNLOCK(inp);
1349 continue;
1350 }
1351 SCTP_TCB_LOCK(stcb);
t00fcxen60d0dea2013-02-10 20:04:03 +00001352 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1353 SCTP_TCB_UNLOCK(stcb);
1354 SCTP_INP_RUNLOCK(inp);
1355 continue;
1356 }
tuexendd729232011-11-01 23:04:43 +00001357 if (stcb->rport != rport) {
1358 /* remote port does not match. */
tuexen09631162012-04-18 11:23:02 +00001359 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001360 SCTP_INP_RUNLOCK(inp);
1361 continue;
1362 }
1363 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001364 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001365 SCTP_INP_RUNLOCK(inp);
1366 continue;
1367 }
t00fcxen60d0dea2013-02-10 20:04:03 +00001368 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1369 SCTP_TCB_UNLOCK(stcb);
1370 SCTP_INP_RUNLOCK(inp);
1371 continue;
1372 }
tuexendd729232011-11-01 23:04:43 +00001373 /* Does this TCB have a matching address? */
1374 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1375
1376 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1377 /* not the same family, can't be a match */
1378 continue;
1379 }
1380 switch (from->sa_family) {
1381#ifdef INET
1382 case AF_INET:
1383 {
1384 struct sockaddr_in *sin, *rsin;
1385
1386 sin = (struct sockaddr_in *)&net->ro._l_addr;
1387 rsin = (struct sockaddr_in *)from;
1388 if (sin->sin_addr.s_addr ==
1389 rsin->sin_addr.s_addr) {
1390 /* found it */
1391 if (netp != NULL) {
1392 *netp = net;
1393 }
1394 /* Update the endpoint pointer */
1395 *inp_p = inp;
1396 SCTP_INP_RUNLOCK(inp);
1397 return (stcb);
1398 }
1399 break;
1400 }
1401#endif
1402#ifdef INET6
1403 case AF_INET6:
1404 {
1405 struct sockaddr_in6 *sin6, *rsin6;
1406
1407 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1408 rsin6 = (struct sockaddr_in6 *)from;
1409 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1410 rsin6)) {
1411 /* found it */
1412 if (netp != NULL) {
1413 *netp = net;
1414 }
1415 /* Update the endpoint pointer */
1416 *inp_p = inp;
1417 SCTP_INP_RUNLOCK(inp);
1418 return (stcb);
1419 }
1420 break;
1421 }
1422#endif
tuexen5d9b9742012-07-11 21:52:01 +00001423#if defined(__Userspace__)
1424 case AF_CONN:
1425 {
1426 struct sockaddr_conn *sconn, *rsconn;
1427
1428 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1429 rsconn = (struct sockaddr_conn *)from;
1430 if (sconn->sconn_addr == rsconn->sconn_addr) {
1431 /* found it */
1432 if (netp != NULL) {
1433 *netp = net;
1434 }
1435 /* Update the endpoint pointer */
1436 *inp_p = inp;
1437 SCTP_INP_RUNLOCK(inp);
1438 return (stcb);
1439 }
1440 break;
1441 }
1442#endif
tuexendd729232011-11-01 23:04:43 +00001443 default:
1444 /* TSNH */
1445 break;
1446 }
1447 }
1448 SCTP_TCB_UNLOCK(stcb);
1449 SCTP_INP_RUNLOCK(inp);
1450 }
1451 return (NULL);
1452}
1453
t00fcxend0ad16b2013-02-09 18:34:24 +00001454
tuexendd729232011-11-01 23:04:43 +00001455/*
1456 * rules for use
1457 *
1458 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1459 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1460 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1461 * NULL.
1462 */
1463
1464struct sctp_tcb *
1465sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1466 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1467{
1468 struct sctpasochead *head;
1469 struct sctp_inpcb *inp;
tuexen63fc0bb2011-12-27 12:24:52 +00001470 struct sctp_tcb *stcb = NULL;
tuexendd729232011-11-01 23:04:43 +00001471 struct sctp_nets *net;
1472 uint16_t rport;
1473
1474 inp = *inp_p;
tuexen5d9b9742012-07-11 21:52:01 +00001475 switch (remote->sa_family) {
1476#ifdef INET
1477 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00001478 rport = (((struct sockaddr_in *)remote)->sin_port);
tuexen5d9b9742012-07-11 21:52:01 +00001479 break;
1480#endif
1481#ifdef INET6
1482 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00001483 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
tuexen5d9b9742012-07-11 21:52:01 +00001484 break;
1485#endif
1486#if defined(__Userspace__)
1487 case AF_CONN:
Taylor Brandstetter74670512018-03-27 10:56:58 -07001488 rport = (((struct sockaddr_conn *)remote)->sconn_port);
tuexen5d9b9742012-07-11 21:52:01 +00001489 break;
1490#endif
1491 default:
tuexendd729232011-11-01 23:04:43 +00001492 return (NULL);
1493 }
1494 if (locked_tcb) {
1495 /*
1496 * UN-lock so we can do proper locking here this occurs when
1497 * called from load_addresses_from_init.
1498 */
1499 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1500 SCTP_TCB_UNLOCK(locked_tcb);
1501 }
1502 SCTP_INP_INFO_RLOCK();
t00fcxenf2d781c2012-11-26 16:47:12 +00001503 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1504 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
tuexendd729232011-11-01 23:04:43 +00001505 /*-
1506 * Now either this guy is our listener or it's the
1507 * connector. If it is the one that issued the connect, then
1508 * it's only chance is to be the first TCB in the list. If
1509 * it is the acceptor, then do the special_lookup to hash
1510 * and find the real inp.
1511 */
Michael Tuexend900e5f2017-06-23 10:52:43 +02001512 if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) {
tuexendd729232011-11-01 23:04:43 +00001513 /* to is peer addr, from is my addr */
1514#ifndef SCTP_MVRF
1515 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1516 netp, inp->def_vrf_id);
1517 if ((stcb != NULL) && (locked_tcb == NULL)) {
1518 /* we have a locked tcb, lower refcount */
1519 SCTP_INP_DECR_REF(inp);
1520 }
1521 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1522 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1523 SCTP_TCB_LOCK(locked_tcb);
1524 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1525 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1526 }
1527#else
1528 /*-
1529 * MVRF is tricky, we must look in every VRF
1530 * the endpoint has.
1531 */
t00fcxen60d0dea2013-02-10 20:04:03 +00001532 int i;
tuexendd729232011-11-01 23:04:43 +00001533
t00fcxen60d0dea2013-02-10 20:04:03 +00001534 for (i = 0; i < inp->num_vrfs; i++) {
1535 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1536 netp, inp->m_vrf_ids[i]);
1537 if ((stcb != NULL) && (locked_tcb == NULL)) {
1538 /* we have a locked tcb, lower refcount */
1539 SCTP_INP_DECR_REF(inp);
1540 break;
1541 }
1542 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1543 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1544 SCTP_TCB_LOCK(locked_tcb);
1545 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1546 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1547 break;
tuexendd729232011-11-01 23:04:43 +00001548 }
1549 }
1550#endif
1551 SCTP_INP_INFO_RUNLOCK();
1552 return (stcb);
1553 } else {
1554 SCTP_INP_WLOCK(inp);
1555 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1556 goto null_return;
1557 }
1558 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1559 if (stcb == NULL) {
1560 goto null_return;
1561 }
1562 SCTP_TCB_LOCK(stcb);
1563
1564 if (stcb->rport != rport) {
1565 /* remote port does not match. */
1566 SCTP_TCB_UNLOCK(stcb);
1567 goto null_return;
1568 }
1569 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001570 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001571 goto null_return;
1572 }
1573 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1574 SCTP_TCB_UNLOCK(stcb);
1575 goto null_return;
1576 }
1577 /* now look at the list of remote addresses */
1578 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1579#ifdef INVARIANTS
1580 if (net == (TAILQ_NEXT(net, sctp_next))) {
1581 panic("Corrupt net list");
1582 }
1583#endif
1584 if (net->ro._l_addr.sa.sa_family !=
1585 remote->sa_family) {
1586 /* not the same family */
1587 continue;
1588 }
1589 switch (remote->sa_family) {
1590#ifdef INET
1591 case AF_INET:
1592 {
1593 struct sockaddr_in *sin, *rsin;
1594
1595 sin = (struct sockaddr_in *)
1596 &net->ro._l_addr;
1597 rsin = (struct sockaddr_in *)remote;
1598 if (sin->sin_addr.s_addr ==
1599 rsin->sin_addr.s_addr) {
1600 /* found it */
1601 if (netp != NULL) {
1602 *netp = net;
1603 }
1604 if (locked_tcb == NULL) {
1605 SCTP_INP_DECR_REF(inp);
1606 } else if (locked_tcb != stcb) {
1607 SCTP_TCB_LOCK(locked_tcb);
1608 }
1609 if (locked_tcb) {
1610 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1611 }
1612
1613 SCTP_INP_WUNLOCK(inp);
1614 SCTP_INP_INFO_RUNLOCK();
1615 return (stcb);
1616 }
1617 break;
1618 }
1619#endif
1620#ifdef INET6
1621 case AF_INET6:
1622 {
1623 struct sockaddr_in6 *sin6, *rsin6;
1624
1625 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1626 rsin6 = (struct sockaddr_in6 *)remote;
1627 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1628 rsin6)) {
1629 /* found it */
1630 if (netp != NULL) {
1631 *netp = net;
1632 }
1633 if (locked_tcb == NULL) {
1634 SCTP_INP_DECR_REF(inp);
1635 } else if (locked_tcb != stcb) {
1636 SCTP_TCB_LOCK(locked_tcb);
1637 }
1638 if (locked_tcb) {
1639 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1640 }
1641 SCTP_INP_WUNLOCK(inp);
1642 SCTP_INP_INFO_RUNLOCK();
1643 return (stcb);
1644 }
1645 break;
1646 }
1647#endif
tuexen5d9b9742012-07-11 21:52:01 +00001648#if defined(__Userspace__)
1649 case AF_CONN:
1650 {
1651 struct sockaddr_conn *sconn, *rsconn;
1652
1653 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1654 rsconn = (struct sockaddr_conn *)remote;
1655 if (sconn->sconn_addr == rsconn->sconn_addr) {
1656 /* found it */
1657 if (netp != NULL) {
1658 *netp = net;
1659 }
1660 if (locked_tcb == NULL) {
1661 SCTP_INP_DECR_REF(inp);
1662 } else if (locked_tcb != stcb) {
1663 SCTP_TCB_LOCK(locked_tcb);
1664 }
1665 if (locked_tcb) {
1666 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1667 }
1668 SCTP_INP_WUNLOCK(inp);
1669 SCTP_INP_INFO_RUNLOCK();
1670 return (stcb);
1671 }
1672 break;
1673 }
1674#endif
tuexendd729232011-11-01 23:04:43 +00001675 default:
1676 /* TSNH */
1677 break;
1678 }
1679 }
1680 SCTP_TCB_UNLOCK(stcb);
1681 }
1682 } else {
1683 SCTP_INP_WLOCK(inp);
1684 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1685 goto null_return;
1686 }
1687 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
t00fcxen1369cd42015-01-11 21:57:30 +00001688 inp->sctp_hashmark)];
tuexendd729232011-11-01 23:04:43 +00001689 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1690 if (stcb->rport != rport) {
1691 /* remote port does not match */
1692 continue;
1693 }
1694 SCTP_TCB_LOCK(stcb);
1695 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1696 SCTP_TCB_UNLOCK(stcb);
1697 continue;
1698 }
1699 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1700 SCTP_TCB_UNLOCK(stcb);
1701 continue;
1702 }
1703 /* now look at the list of remote addresses */
1704 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1705#ifdef INVARIANTS
1706 if (net == (TAILQ_NEXT(net, sctp_next))) {
1707 panic("Corrupt net list");
1708 }
1709#endif
1710 if (net->ro._l_addr.sa.sa_family !=
1711 remote->sa_family) {
1712 /* not the same family */
1713 continue;
1714 }
1715 switch (remote->sa_family) {
1716#ifdef INET
1717 case AF_INET:
1718 {
1719 struct sockaddr_in *sin, *rsin;
1720
1721 sin = (struct sockaddr_in *)
1722 &net->ro._l_addr;
1723 rsin = (struct sockaddr_in *)remote;
1724 if (sin->sin_addr.s_addr ==
1725 rsin->sin_addr.s_addr) {
1726 /* found it */
1727 if (netp != NULL) {
1728 *netp = net;
1729 }
1730 if (locked_tcb == NULL) {
1731 SCTP_INP_DECR_REF(inp);
1732 } else if (locked_tcb != stcb) {
1733 SCTP_TCB_LOCK(locked_tcb);
1734 }
1735 if (locked_tcb) {
1736 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1737 }
1738 SCTP_INP_WUNLOCK(inp);
1739 SCTP_INP_INFO_RUNLOCK();
1740 return (stcb);
1741 }
1742 break;
1743 }
1744#endif
1745#ifdef INET6
1746 case AF_INET6:
1747 {
1748 struct sockaddr_in6 *sin6, *rsin6;
1749
1750 sin6 = (struct sockaddr_in6 *)
1751 &net->ro._l_addr;
1752 rsin6 = (struct sockaddr_in6 *)remote;
1753 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1754 rsin6)) {
1755 /* found it */
1756 if (netp != NULL) {
1757 *netp = net;
1758 }
1759 if (locked_tcb == NULL) {
1760 SCTP_INP_DECR_REF(inp);
1761 } else if (locked_tcb != stcb) {
1762 SCTP_TCB_LOCK(locked_tcb);
1763 }
1764 if (locked_tcb) {
1765 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1766 }
1767 SCTP_INP_WUNLOCK(inp);
1768 SCTP_INP_INFO_RUNLOCK();
1769 return (stcb);
1770 }
1771 break;
1772 }
1773#endif
tuexen5d9b9742012-07-11 21:52:01 +00001774#if defined(__Userspace__)
1775 case AF_CONN:
1776 {
1777 struct sockaddr_conn *sconn, *rsconn;
1778
1779 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1780 rsconn = (struct sockaddr_conn *)remote;
1781 if (sconn->sconn_addr == rsconn->sconn_addr) {
1782 /* found it */
1783 if (netp != NULL) {
1784 *netp = net;
1785 }
1786 if (locked_tcb == NULL) {
1787 SCTP_INP_DECR_REF(inp);
1788 } else if (locked_tcb != stcb) {
1789 SCTP_TCB_LOCK(locked_tcb);
1790 }
1791 if (locked_tcb) {
1792 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1793 }
1794 SCTP_INP_WUNLOCK(inp);
1795 SCTP_INP_INFO_RUNLOCK();
1796 return (stcb);
1797 }
1798 break;
1799 }
1800#endif
tuexendd729232011-11-01 23:04:43 +00001801 default:
1802 /* TSNH */
1803 break;
1804 }
1805 }
1806 SCTP_TCB_UNLOCK(stcb);
1807 }
1808 }
1809null_return:
1810 /* clean up for returning null */
1811 if (locked_tcb) {
1812 SCTP_TCB_LOCK(locked_tcb);
1813 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1814 }
1815 SCTP_INP_WUNLOCK(inp);
1816 SCTP_INP_INFO_RUNLOCK();
1817 /* not found */
1818 return (NULL);
1819}
1820
t00fcxend0ad16b2013-02-09 18:34:24 +00001821
tuexendd729232011-11-01 23:04:43 +00001822/*
1823 * Find an association for a specific endpoint using the association id given
1824 * out in the COMM_UP notification
1825 */
tuexendd729232011-11-01 23:04:43 +00001826struct sctp_tcb *
1827sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1828{
1829 /*
1830 * Use my the assoc_id to find a endpoint
1831 */
1832 struct sctpasochead *head;
1833 struct sctp_tcb *stcb;
1834 uint32_t id;
t00fcxend0ad16b2013-02-09 18:34:24 +00001835
tuexendd729232011-11-01 23:04:43 +00001836 if (inp == NULL) {
1837 SCTP_PRINTF("TSNH ep_associd\n");
1838 return (NULL);
1839 }
1840 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1841 SCTP_PRINTF("TSNH ep_associd0\n");
1842 return (NULL);
1843 }
1844 id = (uint32_t)asoc_id;
1845 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1846 if (head == NULL) {
1847 /* invalid id TSNH */
1848 SCTP_PRINTF("TSNH ep_associd1\n");
1849 return (NULL);
1850 }
1851 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1852 if (stcb->asoc.assoc_id == id) {
1853 if (inp != stcb->sctp_ep) {
1854 /*
1855 * some other guy has the same id active (id
1856 * collision ??).
1857 */
1858 SCTP_PRINTF("TSNH ep_associd2\n");
1859 continue;
1860 }
1861 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1862 continue;
1863 }
1864 if (want_lock) {
1865 SCTP_TCB_LOCK(stcb);
1866 }
1867 return (stcb);
1868 }
1869 }
1870 return (NULL);
1871}
1872
1873
1874struct sctp_tcb *
1875sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1876{
t00fcxend0ad16b2013-02-09 18:34:24 +00001877 struct sctp_tcb *stcb;
1878
1879 SCTP_INP_RLOCK(inp);
1880 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1881 SCTP_INP_RUNLOCK(inp);
1882 return (stcb);
tuexendd729232011-11-01 23:04:43 +00001883}
1884
1885
t00fcxend0ad16b2013-02-09 18:34:24 +00001886/*
1887 * Endpoint probe expects that the INP_INFO is locked.
1888 */
tuexendd729232011-11-01 23:04:43 +00001889static struct sctp_inpcb *
1890sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1891 uint16_t lport, uint32_t vrf_id)
1892{
1893 struct sctp_inpcb *inp;
1894 struct sctp_laddr *laddr;
1895#ifdef INET
1896 struct sockaddr_in *sin;
1897#endif
1898#ifdef INET6
1899 struct sockaddr_in6 *sin6;
1900 struct sockaddr_in6 *intf_addr6;
1901#endif
tuexen09a676e2012-07-08 21:03:18 +00001902#if defined(__Userspace__)
1903 struct sockaddr_conn *sconn;
1904#endif
tuexendd729232011-11-01 23:04:43 +00001905#ifdef SCTP_MVRF
1906 int i;
1907#endif
1908 int fnd;
t00fcxend0ad16b2013-02-09 18:34:24 +00001909
tuexendd729232011-11-01 23:04:43 +00001910#ifdef INET
1911 sin = NULL;
1912#endif
1913#ifdef INET6
1914 sin6 = NULL;
1915#endif
t00fcxenb4c54ac2012-09-15 09:17:50 +00001916#if defined(__Userspace__)
1917 sconn = NULL;
1918#endif
tuexendd729232011-11-01 23:04:43 +00001919 switch (nam->sa_family) {
1920#ifdef INET
1921 case AF_INET:
1922 sin = (struct sockaddr_in *)nam;
1923 break;
1924#endif
1925#ifdef INET6
1926 case AF_INET6:
1927 sin6 = (struct sockaddr_in6 *)nam;
1928 break;
1929#endif
tuexen09a676e2012-07-08 21:03:18 +00001930#if defined(__Userspace__)
1931 case AF_CONN:
1932 sconn = (struct sockaddr_conn *)nam;
1933 break;
1934#endif
tuexendd729232011-11-01 23:04:43 +00001935 default:
1936 /* unsupported family */
1937 return (NULL);
1938 }
1939
1940 if (head == NULL)
1941 return (NULL);
tuexen09631162012-04-18 11:23:02 +00001942
tuexendd729232011-11-01 23:04:43 +00001943 LIST_FOREACH(inp, head, sctp_hash) {
1944 SCTP_INP_RLOCK(inp);
1945 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1946 SCTP_INP_RUNLOCK(inp);
1947 continue;
1948 }
1949 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1950 (inp->sctp_lport == lport)) {
1951 /* got it */
t00fcxen326aa4b2014-06-20 13:42:13 +00001952 switch (nam->sa_family) {
tuexendd729232011-11-01 23:04:43 +00001953#ifdef INET
t00fcxen326aa4b2014-06-20 13:42:13 +00001954 case AF_INET:
1955 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02001956 SCTP_IPV6_V6ONLY(inp)) {
t00fcxen326aa4b2014-06-20 13:42:13 +00001957 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1958 SCTP_INP_RUNLOCK(inp);
1959 continue;
1960 }
1961#if defined(__FreeBSD__)
1962 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1963 &sin->sin_addr) != 0) {
1964 SCTP_INP_RUNLOCK(inp);
1965 continue;
1966 }
1967#endif
1968 break;
tuexendd729232011-11-01 23:04:43 +00001969#endif
1970#ifdef INET6
t00fcxen326aa4b2014-06-20 13:42:13 +00001971 case AF_INET6:
1972 /* A V6 address and the endpoint is NOT bound V6 */
1973 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1974 SCTP_INP_RUNLOCK(inp);
1975 continue;
1976 }
1977#if defined(__FreeBSD__)
1978 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1979 &sin6->sin6_addr) != 0) {
1980 SCTP_INP_RUNLOCK(inp);
1981 continue;
1982 }
tuexendd729232011-11-01 23:04:43 +00001983#endif
t00fcxen326aa4b2014-06-20 13:42:13 +00001984 break;
1985#endif
1986 default:
1987 break;
1988 }
tuexendd729232011-11-01 23:04:43 +00001989 /* does a VRF id match? */
1990 fnd = 0;
1991#ifdef SCTP_MVRF
1992 for (i = 0; i < inp->num_vrfs; i++) {
1993 if (inp->m_vrf_ids[i] == vrf_id) {
1994 fnd = 1;
1995 break;
1996 }
1997 }
1998#else
1999 if (inp->def_vrf_id == vrf_id)
2000 fnd = 1;
2001#endif
2002
2003 SCTP_INP_RUNLOCK(inp);
2004 if (!fnd)
2005 continue;
2006 return (inp);
2007 }
2008 SCTP_INP_RUNLOCK(inp);
2009 }
2010 switch (nam->sa_family) {
2011#ifdef INET
2012 case AF_INET:
2013 if (sin->sin_addr.s_addr == INADDR_ANY) {
2014 /* Can't hunt for one that has no address specified */
2015 return (NULL);
2016 }
2017 break;
2018#endif
2019#ifdef INET6
2020 case AF_INET6:
tuexen63fc0bb2011-12-27 12:24:52 +00002021 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
tuexendd729232011-11-01 23:04:43 +00002022 /* Can't hunt for one that has no address specified */
2023 return (NULL);
2024 }
2025 break;
2026#endif
tuexen09a676e2012-07-08 21:03:18 +00002027#if defined(__Userspace__)
2028 case AF_CONN:
2029 if (sconn->sconn_addr == NULL) {
2030 return (NULL);
2031 }
2032 break;
2033#endif
tuexendd729232011-11-01 23:04:43 +00002034 default:
2035 break;
2036 }
2037 /*
2038 * ok, not bound to all so see if we can find a EP bound to this
2039 * address.
2040 */
2041 LIST_FOREACH(inp, head, sctp_hash) {
2042 SCTP_INP_RLOCK(inp);
2043 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2044 SCTP_INP_RUNLOCK(inp);
2045 continue;
2046 }
2047 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
2048 SCTP_INP_RUNLOCK(inp);
2049 continue;
2050 }
2051 /*
2052 * Ok this could be a likely candidate, look at all of its
2053 * addresses
2054 */
2055 if (inp->sctp_lport != lport) {
2056 SCTP_INP_RUNLOCK(inp);
2057 continue;
2058 }
2059 /* does a VRF id match? */
2060 fnd = 0;
2061#ifdef SCTP_MVRF
2062 for (i = 0; i < inp->num_vrfs; i++) {
2063 if (inp->m_vrf_ids[i] == vrf_id) {
2064 fnd = 1;
2065 break;
2066 }
2067 }
2068#else
2069 if (inp->def_vrf_id == vrf_id)
2070 fnd = 1;
2071
2072#endif
2073 if (!fnd) {
2074 SCTP_INP_RUNLOCK(inp);
2075 continue;
2076 }
2077 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2078 if (laddr->ifa == NULL) {
2079 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002080 __func__);
tuexendd729232011-11-01 23:04:43 +00002081 continue;
2082 }
2083 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
t00fcxen8fca7ff2012-09-05 18:58:42 +00002084 (void *)laddr->ifa);
tuexendd729232011-11-01 23:04:43 +00002085 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2086 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
2087 continue;
2088 }
2089 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
2090 /* possible, see if it matches */
2091 switch (nam->sa_family) {
2092#ifdef INET
2093 case AF_INET:
tuexen63fc0bb2011-12-27 12:24:52 +00002094#if defined(__APPLE__)
2095 if (sin == NULL) {
2096 /* TSNH */
2097 break;
2098 }
2099#endif
tuexendd729232011-11-01 23:04:43 +00002100 if (sin->sin_addr.s_addr ==
2101 laddr->ifa->address.sin.sin_addr.s_addr) {
2102 SCTP_INP_RUNLOCK(inp);
2103 return (inp);
2104 }
2105 break;
2106#endif
2107#ifdef INET6
2108 case AF_INET6:
2109 intf_addr6 = &laddr->ifa->address.sin6;
2110 if (SCTP6_ARE_ADDR_EQUAL(sin6,
2111 intf_addr6)) {
2112 SCTP_INP_RUNLOCK(inp);
2113 return (inp);
2114 }
2115 break;
2116#endif
tuexen09a676e2012-07-08 21:03:18 +00002117#if defined(__Userspace__)
2118 case AF_CONN:
2119 if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
2120 SCTP_INP_RUNLOCK(inp);
2121 return (inp);
2122 }
2123 break;
2124#endif
tuexendd729232011-11-01 23:04:43 +00002125 }
2126 }
2127 }
2128 SCTP_INP_RUNLOCK(inp);
2129 }
2130 return (NULL);
2131}
2132
2133
2134static struct sctp_inpcb *
2135sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
2136{
2137 struct sctppcbhead *head;
2138 struct sctp_inpcb *t_inp;
2139#ifdef SCTP_MVRF
2140 int i;
2141#endif
2142 int fnd;
2143
2144 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2145 SCTP_BASE_INFO(hashmark))];
2146 LIST_FOREACH(t_inp, head, sctp_hash) {
2147 if (t_inp->sctp_lport != lport) {
2148 continue;
2149 }
2150 /* is it in the VRF in question */
2151 fnd = 0;
2152#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00002153 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00002154 if (t_inp->m_vrf_ids[i] == vrf_id) {
2155 fnd = 1;
2156 break;
2157 }
2158 }
2159#else
2160 if (t_inp->def_vrf_id == vrf_id)
2161 fnd = 1;
2162#endif
2163 if (!fnd)
2164 continue;
2165
2166 /* This one is in use. */
2167 /* check the v6/v4 binding issue */
2168 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02002169 SCTP_IPV6_V6ONLY(t_inp)) {
tuexendd729232011-11-01 23:04:43 +00002170 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2171 /* collision in V6 space */
2172 return (t_inp);
2173 } else {
2174 /* inp is BOUND_V4 no conflict */
2175 continue;
2176 }
2177 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2178 /* t_inp is bound v4 and v6, conflict always */
2179 return (t_inp);
2180 } else {
2181 /* t_inp is bound only V4 */
2182 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
Michael Tuexen72e5cec2019-08-05 13:21:02 +02002183 SCTP_IPV6_V6ONLY(inp)) {
tuexendd729232011-11-01 23:04:43 +00002184 /* no conflict */
2185 continue;
2186 }
2187 /* else fall through to conflict */
2188 }
2189 return (t_inp);
2190 }
2191 return (NULL);
2192}
2193
2194
2195int
2196sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
2197{
2198 /* For 1-2-1 with port reuse */
2199 struct sctppcbhead *head;
t00fcxenc00f4bc2015-03-24 21:17:16 +00002200 struct sctp_inpcb *tinp, *ninp;
tuexendd729232011-11-01 23:04:43 +00002201
2202 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
2203 /* only works with port reuse on */
tuexen63fc0bb2011-12-27 12:24:52 +00002204 return (-1);
tuexendd729232011-11-01 23:04:43 +00002205 }
2206 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
2207 return (0);
2208 }
2209 SCTP_INP_RUNLOCK(inp);
t00fcxenc00f4bc2015-03-24 21:17:16 +00002210 SCTP_INP_INFO_WLOCK();
tuexendd729232011-11-01 23:04:43 +00002211 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
2212 SCTP_BASE_INFO(hashmark))];
2213 /* Kick out all non-listeners to the TCP hash */
t00fcxenc00f4bc2015-03-24 21:17:16 +00002214 LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
tuexendd729232011-11-01 23:04:43 +00002215 if (tinp->sctp_lport != inp->sctp_lport) {
2216 continue;
2217 }
2218 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2219 continue;
2220 }
2221 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2222 continue;
2223 }
Michael Tuexend900e5f2017-06-23 10:52:43 +02002224 if (SCTP_IS_LISTENING(tinp)) {
tuexendd729232011-11-01 23:04:43 +00002225 continue;
2226 }
2227 SCTP_INP_WLOCK(tinp);
2228 LIST_REMOVE(tinp, sctp_hash);
2229 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
2230 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
2231 LIST_INSERT_HEAD(head, tinp, sctp_hash);
2232 SCTP_INP_WUNLOCK(tinp);
2233 }
2234 SCTP_INP_WLOCK(inp);
2235 /* Pull from where he was */
2236 LIST_REMOVE(inp, sctp_hash);
2237 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
2238 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
2239 LIST_INSERT_HEAD(head, inp, sctp_hash);
2240 SCTP_INP_WUNLOCK(inp);
2241 SCTP_INP_RLOCK(inp);
t00fcxenc00f4bc2015-03-24 21:17:16 +00002242 SCTP_INP_INFO_WUNLOCK();
tuexendd729232011-11-01 23:04:43 +00002243 return (0);
2244}
2245
2246
2247struct sctp_inpcb *
2248sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
2249 uint32_t vrf_id)
2250{
2251 /*
2252 * First we check the hash table to see if someone has this port
2253 * bound with just the port.
2254 */
2255 struct sctp_inpcb *inp;
2256 struct sctppcbhead *head;
2257 int lport;
2258 unsigned int i;
2259#ifdef INET
2260 struct sockaddr_in *sin;
2261#endif
2262#ifdef INET6
2263 struct sockaddr_in6 *sin6;
2264#endif
tuexen09a676e2012-07-08 21:03:18 +00002265#if defined(__Userspace__)
2266 struct sockaddr_conn *sconn;
2267#endif
tuexendd729232011-11-01 23:04:43 +00002268
2269 switch (nam->sa_family) {
2270#ifdef INET
2271 case AF_INET:
2272 sin = (struct sockaddr_in *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002273 lport = sin->sin_port;
tuexendd729232011-11-01 23:04:43 +00002274 break;
2275#endif
2276#ifdef INET6
2277 case AF_INET6:
2278 sin6 = (struct sockaddr_in6 *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002279 lport = sin6->sin6_port;
tuexendd729232011-11-01 23:04:43 +00002280 break;
2281#endif
tuexen09a676e2012-07-08 21:03:18 +00002282#if defined(__Userspace__)
2283 case AF_CONN:
2284 sconn = (struct sockaddr_conn *)nam;
2285 lport = sconn->sconn_port;
2286 break;
2287#endif
tuexendd729232011-11-01 23:04:43 +00002288 default:
2289 return (NULL);
2290 }
2291 /*
2292 * I could cheat here and just cast to one of the types but we will
2293 * do it right. It also provides the check against an Unsupported
2294 * type too.
2295 */
2296 /* Find the head of the ALLADDR chain */
2297 if (have_lock == 0) {
2298 SCTP_INP_INFO_RLOCK();
2299 }
2300 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2301 SCTP_BASE_INFO(hashmark))];
2302 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2303
2304 /*
2305 * If the TCP model exists it could be that the main listening
2306 * endpoint is gone but there still exists a connected socket for this
2307 * guy. If so we can return the first one that we find. This may NOT
2308 * be the correct one so the caller should be wary on the returned INP.
2309 * Currently the only caller that sets find_tcp_pool is in bindx where
2310 * we are verifying that a user CAN bind the address. He either
2311 * has bound it already, or someone else has, or its open to bind,
2312 * so this is good enough.
2313 */
2314 if (inp == NULL && find_tcp_pool) {
2315 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
2316 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
2317 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2318 if (inp) {
2319 break;
2320 }
2321 }
2322 }
2323 if (inp) {
2324 SCTP_INP_INCR_REF(inp);
2325 }
2326 if (have_lock == 0) {
2327 SCTP_INP_INFO_RUNLOCK();
2328 }
2329 return (inp);
2330}
2331
t00fcxend0ad16b2013-02-09 18:34:24 +00002332
tuexendd729232011-11-01 23:04:43 +00002333/*
2334 * Find an association for an endpoint with the pointer to whom you want to
2335 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
2336 * need to change the *to to some other struct like a mbuf...
2337 */
2338struct sctp_tcb *
tuexen664967a2012-06-28 16:24:36 +00002339sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
tuexendd729232011-11-01 23:04:43 +00002340 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
2341 uint32_t vrf_id)
2342{
2343 struct sctp_inpcb *inp = NULL;
t00fcxen60d0dea2013-02-10 20:04:03 +00002344 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00002345
2346 SCTP_INP_INFO_RLOCK();
2347 if (find_tcp_pool) {
2348 if (inp_p != NULL) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002349 stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
2350 vrf_id);
tuexendd729232011-11-01 23:04:43 +00002351 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002352 stcb = sctp_tcb_special_locate(&inp, from, to, netp,
2353 vrf_id);
tuexendd729232011-11-01 23:04:43 +00002354 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002355 if (stcb != NULL) {
tuexendd729232011-11-01 23:04:43 +00002356 SCTP_INP_INFO_RUNLOCK();
t00fcxen60d0dea2013-02-10 20:04:03 +00002357 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002358 }
2359 }
2360 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
2361 if (inp_p != NULL) {
2362 *inp_p = inp;
2363 }
2364 SCTP_INP_INFO_RUNLOCK();
tuexendd729232011-11-01 23:04:43 +00002365 if (inp == NULL) {
2366 return (NULL);
2367 }
2368 /*
2369 * ok, we have an endpoint, now lets find the assoc for it (if any)
2370 * we now place the source address or from in the to of the find
2371 * endpoint call. Since in reality this chain is used from the
2372 * inbound packet side.
2373 */
2374 if (inp_p != NULL) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002375 stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
2376 NULL);
tuexendd729232011-11-01 23:04:43 +00002377 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002378 stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
2379 NULL);
tuexendd729232011-11-01 23:04:43 +00002380 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002381 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002382}
2383
2384
2385/*
2386 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
2387 * find all addresses that the sender has specified in any address list. Each
2388 * address will be used to lookup the TCB and see if one exits.
2389 */
2390static struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002391sctp_findassociation_special_addr(struct mbuf *m, int offset,
tuexendd729232011-11-01 23:04:43 +00002392 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
tuexen664967a2012-06-28 16:24:36 +00002393 struct sockaddr *dst)
tuexendd729232011-11-01 23:04:43 +00002394{
Michael Tuexen473225f2017-06-23 23:05:43 +02002395 struct sctp_paramhdr *phdr, param_buf;
t00fcxenaff14b62013-11-02 18:02:02 +00002396#if defined(INET) || defined(INET6)
t00fcxen6ebb6032013-11-02 18:03:31 +00002397 struct sctp_tcb *stcb;
t00fcxenaff14b62013-11-02 18:02:02 +00002398 uint16_t ptype;
2399#endif
2400 uint16_t plen;
tuexendd729232011-11-01 23:04:43 +00002401#ifdef INET
2402 struct sockaddr_in sin4;
2403#endif
2404#ifdef INET6
2405 struct sockaddr_in6 sin6;
2406#endif
2407
2408#ifdef INET
2409 memset(&sin4, 0, sizeof(sin4));
t00fcxen8fbaee32012-09-04 16:41:39 +00002410#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002411 sin4.sin_len = sizeof(sin4);
2412#endif
2413 sin4.sin_family = AF_INET;
2414 sin4.sin_port = sh->src_port;
2415#endif
2416#ifdef INET6
2417 memset(&sin6, 0, sizeof(sin6));
t00fcxen8fbaee32012-09-04 16:41:39 +00002418#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002419 sin6.sin6_len = sizeof(sin6);
2420#endif
2421 sin6.sin6_family = AF_INET6;
2422 sin6.sin6_port = sh->src_port;
2423#endif
2424
tuexendd729232011-11-01 23:04:43 +00002425 offset += sizeof(struct sctp_init_chunk);
2426
Michael Tuexen473225f2017-06-23 23:05:43 +02002427 phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00002428 while (phdr != NULL) {
2429 /* now we must see if we want the parameter */
t00fcxenaff14b62013-11-02 18:02:02 +00002430#if defined(INET) || defined(INET6)
tuexendd729232011-11-01 23:04:43 +00002431 ptype = ntohs(phdr->param_type);
t00fcxenaff14b62013-11-02 18:02:02 +00002432#endif
tuexendd729232011-11-01 23:04:43 +00002433 plen = ntohs(phdr->param_length);
2434 if (plen == 0) {
2435 break;
2436 }
2437#ifdef INET
2438 if (ptype == SCTP_IPV4_ADDRESS &&
2439 plen == sizeof(struct sctp_ipv4addr_param)) {
2440 /* Get the rest of the address */
Michael Tuexen473225f2017-06-23 23:05:43 +02002441 struct sctp_ipv4addr_param ip4_param, *p4;
tuexendd729232011-11-01 23:04:43 +00002442
2443 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02002444 (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
tuexendd729232011-11-01 23:04:43 +00002445 if (phdr == NULL) {
2446 return (NULL);
2447 }
2448 p4 = (struct sctp_ipv4addr_param *)phdr;
2449 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2450 /* look it up */
t00fcxen60d0dea2013-02-10 20:04:03 +00002451 stcb = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002452 (struct sockaddr *)&sin4, netp, dst, NULL);
t00fcxen60d0dea2013-02-10 20:04:03 +00002453 if (stcb != NULL) {
2454 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002455 }
2456 }
2457#endif
2458#ifdef INET6
2459 if (ptype == SCTP_IPV6_ADDRESS &&
2460 plen == sizeof(struct sctp_ipv6addr_param)) {
2461 /* Get the rest of the address */
Michael Tuexen473225f2017-06-23 23:05:43 +02002462 struct sctp_ipv6addr_param ip6_param, *p6;
tuexendd729232011-11-01 23:04:43 +00002463
2464 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02002465 (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
tuexendd729232011-11-01 23:04:43 +00002466 if (phdr == NULL) {
2467 return (NULL);
2468 }
2469 p6 = (struct sctp_ipv6addr_param *)phdr;
2470 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2471 /* look it up */
t00fcxen60d0dea2013-02-10 20:04:03 +00002472 stcb = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002473 (struct sockaddr *)&sin6, netp, dst, NULL);
t00fcxen60d0dea2013-02-10 20:04:03 +00002474 if (stcb != NULL) {
2475 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002476 }
2477 }
2478#endif
2479 offset += SCTP_SIZE32(plen);
Michael Tuexen473225f2017-06-23 23:05:43 +02002480 phdr = sctp_get_next_param(m, offset, &param_buf,
2481 sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00002482 }
2483 return (NULL);
2484}
2485
2486static struct sctp_tcb *
2487sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2488 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2489 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2490{
2491 /*
2492 * Use my vtag to hash. If we find it we then verify the source addr
2493 * is in the assoc. If all goes well we save a bit on rec of a
2494 * packet.
2495 */
2496 struct sctpasochead *head;
2497 struct sctp_nets *net;
2498 struct sctp_tcb *stcb;
tuexen9784e9a2011-12-18 13:04:23 +00002499#ifdef SCTP_MVRF
2500 unsigned int i;
2501#endif
tuexendd729232011-11-01 23:04:43 +00002502
tuexendd729232011-11-01 23:04:43 +00002503 SCTP_INP_INFO_RLOCK();
2504 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
t00fcxen0d664882015-01-11 21:47:36 +00002505 SCTP_BASE_INFO(hashasocmark))];
tuexendd729232011-11-01 23:04:43 +00002506 LIST_FOREACH(stcb, head, sctp_asocs) {
2507 SCTP_INP_RLOCK(stcb->sctp_ep);
2508 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2509 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2510 continue;
2511 }
tuexen9784e9a2011-12-18 13:04:23 +00002512#ifdef SCTP_MVRF
2513 for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) {
2514 if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) {
2515 break;
2516 }
2517 }
2518 if (i == stcb->sctp_ep->num_vrfs) {
2519 SCTP_INP_RUNLOCK(inp);
2520 continue;
2521 }
2522#else
2523 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2524 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2525 continue;
2526 }
2527#endif
tuexendd729232011-11-01 23:04:43 +00002528 SCTP_TCB_LOCK(stcb);
2529 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2530 if (stcb->asoc.my_vtag == vtag) {
2531 /* candidate */
2532 if (stcb->rport != rport) {
2533 SCTP_TCB_UNLOCK(stcb);
2534 continue;
2535 }
2536 if (stcb->sctp_ep->sctp_lport != lport) {
2537 SCTP_TCB_UNLOCK(stcb);
2538 continue;
2539 }
2540 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2541 SCTP_TCB_UNLOCK(stcb);
2542 continue;
2543 }
2544 /* RRS:Need toaddr check here */
2545 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2546 /* Endpoint does not own this address */
2547 SCTP_TCB_UNLOCK(stcb);
2548 continue;
2549 }
2550 if (remote_tag) {
2551 /* If we have both vtags that's all we match on */
2552 if (stcb->asoc.peer_vtag == remote_tag) {
2553 /* If both tags match we consider it conclusive
2554 * and check NO source/destination addresses
2555 */
2556 goto conclusive;
2557 }
2558 }
2559 if (skip_src_check) {
2560 conclusive:
2561 if (from) {
tuexen63fc0bb2011-12-27 12:24:52 +00002562 *netp = sctp_findnet(stcb, from);
tuexendd729232011-11-01 23:04:43 +00002563 } else {
2564 *netp = NULL; /* unknown */
2565 }
2566 if (inp_p)
2567 *inp_p = stcb->sctp_ep;
2568 SCTP_INP_INFO_RUNLOCK();
2569 return (stcb);
2570 }
2571 net = sctp_findnet(stcb, from);
2572 if (net) {
2573 /* yep its him. */
2574 *netp = net;
2575 SCTP_STAT_INCR(sctps_vtagexpress);
2576 *inp_p = stcb->sctp_ep;
2577 SCTP_INP_INFO_RUNLOCK();
2578 return (stcb);
2579 } else {
2580 /*
2581 * not him, this should only happen in rare
2582 * cases so I peg it.
2583 */
2584 SCTP_STAT_INCR(sctps_vtagbogus);
2585 }
2586 }
2587 SCTP_TCB_UNLOCK(stcb);
2588 }
2589 SCTP_INP_INFO_RUNLOCK();
2590 return (NULL);
2591}
2592
t00fcxend0ad16b2013-02-09 18:34:24 +00002593
tuexendd729232011-11-01 23:04:43 +00002594/*
2595 * Find an association with the pointer to the inbound IP packet. This can be
2596 * a IPv4 or IPv6 packet.
2597 */
2598struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002599sctp_findassociation_addr(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002600 struct sockaddr *src, struct sockaddr *dst,
tuexendd729232011-11-01 23:04:43 +00002601 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2602 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2603{
t00fcxen60d0dea2013-02-10 20:04:03 +00002604 struct sctp_tcb *stcb;
tuexendd729232011-11-01 23:04:43 +00002605 struct sctp_inpcb *inp;
2606
tuexendd729232011-11-01 23:04:43 +00002607 if (sh->v_tag) {
2608 /* we only go down this path if vtag is non-zero */
t00fcxen60d0dea2013-02-10 20:04:03 +00002609 stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
t00fcxenafd21192013-05-10 18:15:02 +00002610 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
t00fcxen60d0dea2013-02-10 20:04:03 +00002611 if (stcb) {
2612 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002613 }
2614 }
2615
tuexendd729232011-11-01 23:04:43 +00002616 if (inp_p) {
t00fcxen60d0dea2013-02-10 20:04:03 +00002617 stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
Michael Tuexen7f202782016-01-28 17:09:30 +01002618 1, vrf_id);
tuexendd729232011-11-01 23:04:43 +00002619 inp = *inp_p;
2620 } else {
t00fcxen60d0dea2013-02-10 20:04:03 +00002621 stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
Michael Tuexen7f202782016-01-28 17:09:30 +01002622 1, vrf_id);
tuexendd729232011-11-01 23:04:43 +00002623 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002624 SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2625 if (stcb == NULL && inp) {
tuexendd729232011-11-01 23:04:43 +00002626 /* Found a EP but not this address */
2627 if ((ch->chunk_type == SCTP_INITIATION) ||
2628 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2629 /*-
2630 * special hook, we do NOT return linp or an
2631 * association that is linked to an existing
2632 * association that is under the TCP pool (i.e. no
2633 * listener exists). The endpoint finding routine
2634 * will always find a listener before examining the
2635 * TCP pool.
2636 */
2637 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2638 if (inp_p) {
2639 *inp_p = NULL;
2640 }
2641 return (NULL);
2642 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002643 stcb = sctp_findassociation_special_addr(m,
tuexen664967a2012-06-28 16:24:36 +00002644 offset, sh, &inp, netp, dst);
tuexendd729232011-11-01 23:04:43 +00002645 if (inp_p != NULL) {
2646 *inp_p = inp;
2647 }
2648 }
2649 }
t00fcxen60d0dea2013-02-10 20:04:03 +00002650 SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2651 return (stcb);
tuexendd729232011-11-01 23:04:43 +00002652}
2653
2654/*
2655 * lookup an association by an ASCONF lookup address.
2656 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2657 */
2658struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002659sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002660 struct sockaddr *dst, struct sctphdr *sh,
2661 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
tuexendd729232011-11-01 23:04:43 +00002662{
2663 struct sctp_tcb *stcb;
t00fcxen05f07c02014-09-07 10:15:26 +00002664 union sctp_sockstore remote_store;
Michael Tuexen473225f2017-06-23 23:05:43 +02002665 struct sctp_paramhdr param_buf, *phdr;
tuexendd729232011-11-01 23:04:43 +00002666 int ptype;
2667 int zero_address = 0;
2668#ifdef INET
2669 struct sockaddr_in *sin;
2670#endif
2671#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00002672 struct sockaddr_in6 *sin6;
2673#endif
2674
tuexendd729232011-11-01 23:04:43 +00002675 memset(&remote_store, 0, sizeof(remote_store));
tuexendd729232011-11-01 23:04:43 +00002676 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002677 &param_buf, sizeof(struct sctp_paramhdr));
tuexendd729232011-11-01 23:04:43 +00002678 if (phdr == NULL) {
2679 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002680 __func__);
tuexendd729232011-11-01 23:04:43 +00002681 return NULL;
2682 }
2683 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2684 /* get the correlation address */
2685 switch (ptype) {
2686#ifdef INET6
2687 case SCTP_IPV6_ADDRESS:
2688 {
2689 /* ipv6 address param */
2690 struct sctp_ipv6addr_param *p6, p6_buf;
2691
2692 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2693 return NULL;
2694 }
2695 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2696 offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002697 &p6_buf.ph, sizeof(p6_buf));
tuexendd729232011-11-01 23:04:43 +00002698 if (p6 == NULL) {
2699 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002700 __func__);
tuexendd729232011-11-01 23:04:43 +00002701 return (NULL);
2702 }
t00fcxen05f07c02014-09-07 10:15:26 +00002703 sin6 = &remote_store.sin6;
tuexendd729232011-11-01 23:04:43 +00002704 sin6->sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00002705#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002706 sin6->sin6_len = sizeof(*sin6);
2707#endif
2708 sin6->sin6_port = sh->src_port;
2709 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2710 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2711 zero_address = 1;
2712 break;
2713 }
2714#endif
2715#ifdef INET
2716 case SCTP_IPV4_ADDRESS:
2717 {
2718 /* ipv4 address param */
2719 struct sctp_ipv4addr_param *p4, p4_buf;
2720
2721 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2722 return NULL;
2723 }
2724 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2725 offset + sizeof(struct sctp_asconf_chunk),
Michael Tuexen473225f2017-06-23 23:05:43 +02002726 &p4_buf.ph, sizeof(p4_buf));
tuexendd729232011-11-01 23:04:43 +00002727 if (p4 == NULL) {
2728 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02002729 __func__);
tuexendd729232011-11-01 23:04:43 +00002730 return (NULL);
2731 }
t00fcxen05f07c02014-09-07 10:15:26 +00002732 sin = &remote_store.sin;
tuexendd729232011-11-01 23:04:43 +00002733 sin->sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00002734#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002735 sin->sin_len = sizeof(*sin);
2736#endif
2737 sin->sin_port = sh->src_port;
2738 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2739 if (sin->sin_addr.s_addr == INADDR_ANY)
2740 zero_address = 1;
2741 break;
2742 }
2743#endif
2744 default:
2745 /* invalid address param type */
2746 return NULL;
2747 }
2748
2749 if (zero_address) {
tuexen664967a2012-06-28 16:24:36 +00002750 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
tuexendd729232011-11-01 23:04:43 +00002751 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
tuexenb53ec512012-08-05 10:55:12 +00002752 if (stcb != NULL) {
2753 SCTP_INP_DECR_REF(*inp_p);
2754 }
tuexendd729232011-11-01 23:04:43 +00002755 } else {
2756 stcb = sctp_findassociation_ep_addr(inp_p,
t00fcxen05f07c02014-09-07 10:15:26 +00002757 &remote_store.sa, netp,
tuexen664967a2012-06-28 16:24:36 +00002758 dst, NULL);
tuexendd729232011-11-01 23:04:43 +00002759 }
2760 return (stcb);
2761}
2762
2763
2764/*
2765 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2766 * addresses. This way if we don't get a bind we by default pick a ephemeral
2767 * port with all addresses bound.
2768 */
2769int
2770sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2771{
2772 /*
2773 * we get called when a new endpoint starts up. We need to allocate
2774 * the sctp_inpcb structure from the zone and init it. Mark it as
2775 * unbound and find a port that we can use as an ephemeral with
2776 * INADDR_ANY. If the user binds later no problem we can then add in
2777 * the specific addresses. And setup the default parameters for the
2778 * EP.
2779 */
2780 int i, error;
2781 struct sctp_inpcb *inp;
2782 struct sctp_pcb *m;
2783 struct timeval time;
2784 sctp_sharedkey_t *null_key;
2785
2786 error = 0;
2787
2788 SCTP_INP_INFO_WLOCK();
2789 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2790 if (inp == NULL) {
2791 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2792 SCTP_INP_INFO_WUNLOCK();
2793 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2794 return (ENOBUFS);
2795 }
2796 /* zap it */
Michael Tuexend0853302017-07-19 17:01:31 +02002797 memset(inp, 0, sizeof(*inp));
tuexendd729232011-11-01 23:04:43 +00002798
2799 /* bump generations */
2800#if defined(__APPLE__)
2801 inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
2802#endif
2803 /* setup socket pointers */
2804 inp->sctp_socket = so;
2805 inp->ip_inp.inp.inp_socket = so;
t00fcxen326aa4b2014-06-20 13:42:13 +00002806#if defined(__FreeBSD__)
2807 inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
2808#endif
tuexenc0bb6122012-01-15 14:21:21 +00002809#ifdef INET6
tuexen76eb2092012-04-01 16:35:28 +00002810#if !defined(__Userspace__) && !defined(__Windows__)
t00fcxenafd21192013-05-10 18:15:02 +00002811 if (INP_SOCKAF(so) == AF_INET6) {
2812 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2813 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2814 }
2815 if (MODULE_GLOBAL(ip6_v6only)) {
2816 inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
2817 }
tuexendd729232011-11-01 23:04:43 +00002818 }
2819#endif
tuexenc0bb6122012-01-15 14:21:21 +00002820#endif
tuexendd729232011-11-01 23:04:43 +00002821 inp->sctp_associd_counter = 1;
2822 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2823 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
t00fcxen3eda0f22015-03-10 20:10:45 +00002824 inp->max_cwnd = 0;
tuexendd729232011-11-01 23:04:43 +00002825 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
t00fcxen478b7722014-08-02 18:24:12 +00002826 inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
t00fcxen0e78cef2014-08-02 22:05:33 +00002827 inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
t00fcxenb3969a12014-08-12 13:19:21 +00002828 inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
t00fcxenaac0f452014-08-12 12:51:45 +00002829 inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
t00fcxenfa7e1a02014-08-04 20:41:36 +00002830 inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
t00fcxen3656cf92014-08-03 15:25:02 +00002831 inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
t00fcxen1cc0ba82014-08-03 20:32:08 +00002832 inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
Michael Tuexene5001952016-04-17 19:25:27 +02002833 inp->idata_supported = 0;
2834
t00fcxena51e4462015-06-17 15:53:23 +00002835#if defined(__FreeBSD__)
2836 inp->fibnum = so->so_fibnum;
2837#else
2838 inp->fibnum = 0;
2839#endif
tuexen09631162012-04-18 11:23:02 +00002840#if defined(__Userspace__)
tuexeneda9dbb2012-05-27 11:10:08 +00002841 inp->ulp_info = NULL;
tuexen09631162012-04-18 11:23:02 +00002842 inp->recv_callback = NULL;
2843 inp->send_callback = NULL;
2844 inp->send_sb_threshold = 0;
tuexen09631162012-04-18 11:23:02 +00002845#endif
tuexendd729232011-11-01 23:04:43 +00002846 /* init the small hash table we use to track asocid <-> tcb */
2847 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2848 if (inp->sctp_asocidhash == NULL) {
t00fcxen326aa4b2014-06-20 13:42:13 +00002849#if defined(__FreeBSD__)
2850 crfree(inp->ip_inp.inp.inp_cred);
2851#endif
tuexendd729232011-11-01 23:04:43 +00002852 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2853 SCTP_INP_INFO_WUNLOCK();
2854 return (ENOBUFS);
2855 }
tuexendd729232011-11-01 23:04:43 +00002856 SCTP_INCR_EP_COUNT();
tuexendd729232011-11-01 23:04:43 +00002857 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
tuexendd729232011-11-01 23:04:43 +00002858 SCTP_INP_INFO_WUNLOCK();
2859
2860 so->so_pcb = (caddr_t)inp;
2861
tuexen5fc22842012-02-11 13:32:19 +00002862#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00002863 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
2864 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
2865#else
2866 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2867#endif
2868 /* UDP style socket */
2869 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2870 SCTP_PCB_FLAGS_UNBOUND);
2871 /* Be sure it is NON-BLOCKING IO for UDP */
2872 /* SCTP_SET_SO_NBIO(so); */
2873 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2874 /* TCP style socket */
2875 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2876 SCTP_PCB_FLAGS_UNBOUND);
2877 /* Be sure we have blocking IO by default */
Michael Tuexenfe223572017-09-22 00:19:22 +02002878 SOCK_LOCK(so);
tuexendd729232011-11-01 23:04:43 +00002879 SCTP_CLEAR_SO_NBIO(so);
Michael Tuexenfe223572017-09-22 00:19:22 +02002880 SOCK_UNLOCK(so);
tuexendd729232011-11-01 23:04:43 +00002881#if defined(__Panda__)
2882 } else if (SCTP_SO_TYPE(so) == SOCK_FASTSEQPACKET) {
2883 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2884 SCTP_PCB_FLAGS_UNBOUND);
tuexendd729232011-11-01 23:04:43 +00002885 } else if (SCTP_SO_TYPE(so) == SOCK_FASTSTREAM) {
2886 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2887 SCTP_PCB_FLAGS_UNBOUND);
tuexendd729232011-11-01 23:04:43 +00002888#endif
2889 } else {
2890 /*
2891 * unsupported socket type (RAW, etc)- in case we missed it
2892 * in protosw
2893 */
2894 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2895 so->so_pcb = NULL;
t00fcxen326aa4b2014-06-20 13:42:13 +00002896#if defined(__FreeBSD__)
2897 crfree(inp->ip_inp.inp.inp_cred);
2898#endif
tuexendd729232011-11-01 23:04:43 +00002899 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2900 return (EOPNOTSUPP);
2901 }
2902 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2903 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2904 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2905 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2906 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2907 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2908 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2909 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2910 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2911 }
2912 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2913 &inp->sctp_hashmark);
2914 if (inp->sctp_tcbhash == NULL) {
2915 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2916 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2917 so->so_pcb = NULL;
t00fcxen326aa4b2014-06-20 13:42:13 +00002918#if defined(__FreeBSD__)
2919 crfree(inp->ip_inp.inp.inp_cred);
2920#endif
tuexendd729232011-11-01 23:04:43 +00002921 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2922 return (ENOBUFS);
2923 }
2924#ifdef SCTP_MVRF
2925 inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
2926 SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
2927 (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
2928 if (inp->m_vrf_ids == NULL) {
2929 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2930 so->so_pcb = NULL;
2931 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
t00fcxen326aa4b2014-06-20 13:42:13 +00002932#if defined(__FreeBSD__)
2933 crfree(inp->ip_inp.inp.inp_cred);
2934#endif
tuexendd729232011-11-01 23:04:43 +00002935 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2936 return (ENOBUFS);
2937 }
2938 inp->m_vrf_ids[0] = vrf_id;
2939 inp->num_vrfs = 1;
2940#endif
2941 inp->def_vrf_id = vrf_id;
2942
2943#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00002944#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
t00fcxenbc766ab2014-01-03 19:29:31 +00002945 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 +00002946 if (inp->ip_inp.inp.inpcb_mtx == NULL) {
t00fcxen8fca7ff2012-09-05 18:58:42 +00002947 SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so);
tuexendd729232011-11-01 23:04:43 +00002948#ifdef SCTP_MVRF
2949 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
2950#endif
2951 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2952 so->so_pcb = NULL;
2953 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
t00fcxenbc766ab2014-01-03 19:29:31 +00002954 SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock);
tuexendd729232011-11-01 23:04:43 +00002955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
2956 return (ENOMEM);
2957 }
t00fcxenbc766ab2014-01-03 19:29:31 +00002958#elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2959 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 +00002960#else
t00fcxenbc766ab2014-01-03 19:29:31 +00002961 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 +00002962#endif
2963#endif
2964 SCTP_INP_INFO_WLOCK();
2965 SCTP_INP_LOCK_INIT(inp);
2966#if defined(__FreeBSD__)
2967 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2968#endif
2969 SCTP_INP_READ_INIT(inp);
2970 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2971 /* lock the new ep */
2972 SCTP_INP_WLOCK(inp);
2973
2974 /* add it to the info area */
2975 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2976#if defined(__APPLE__)
t00fcxenbc766ab2014-01-03 19:29:31 +00002977 inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo);
2978#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2979 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list);
2980#else
2981 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list);
2982#endif
tuexendd729232011-11-01 23:04:43 +00002983#endif
2984 SCTP_INP_INFO_WUNLOCK();
2985
2986 TAILQ_INIT(&inp->read_queue);
2987 LIST_INIT(&inp->sctp_addr_list);
2988
2989 LIST_INIT(&inp->sctp_asoc_list);
2990
2991#ifdef SCTP_TRACK_FREED_ASOCS
2992 /* TEMP CODE */
2993 LIST_INIT(&inp->sctp_asoc_free_list);
2994#endif
2995 /* Init the timer structure for signature change */
2996 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2997 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2998
2999 /* now init the actual endpoint default data */
3000 m = &inp->sctp_ep;
3001
3002 /* setup the base timeout information */
Michael Tuexen25935e82020-03-28 22:55:28 +01003003 m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */
3004 m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */
3005 m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
3006 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
3007 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
3008 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
3009 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
tuexendd729232011-11-01 23:04:43 +00003010 /* all max/min max are in ms */
3011 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
3012 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
3013 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
3014 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
3015 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
tuexendd729232011-11-01 23:04:43 +00003016 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
3017 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
3018 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
3019 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
3020 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
3021 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
3022 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
3023 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
3024
3025 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
3026 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
t00fcxenc45180a2013-07-05 10:17:17 +00003027 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
tuexendd729232011-11-01 23:04:43 +00003028 /* number of streams to pre-open on a association */
3029 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
3030
Michael Tuexende0630d2017-11-03 22:04:01 +01003031 m->default_mtu = 0;
tuexendd729232011-11-01 23:04:43 +00003032 /* Add adaptation cookie */
t00fcxen36377202013-02-11 21:07:33 +00003033 m->adaptation_layer_indicator = 0;
3034 m->adaptation_layer_indicator_provided = 0;
tuexendd729232011-11-01 23:04:43 +00003035
3036 /* seed random number generator */
3037 m->random_counter = 1;
3038 m->store_at = SCTP_SIGNATURE_SIZE;
3039 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
3040 sctp_fill_random_store(m);
3041
3042 /* Minimum cookie size */
3043 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
3044 sizeof(struct sctp_state_cookie);
3045 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
3046
3047 /* Setup the initial secret */
3048 (void)SCTP_GETTIME_TIMEVAL(&time);
3049 m->time_of_secret_change = time.tv_sec;
3050
3051 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
3052 m->secret_key[0][i] = sctp_select_initial_TSN(m);
3053 }
3054 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
3055
3056 /* How long is a cookie good for ? */
Michael Tuexen25935e82020-03-28 22:55:28 +01003057 m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
tuexendd729232011-11-01 23:04:43 +00003058 /*
3059 * Initialize authentication parameters
3060 */
3061 m->local_hmacs = sctp_default_supported_hmaclist();
3062 m->local_auth_chunks = sctp_alloc_chunklist();
t00fcxenaac0f452014-08-12 12:51:45 +00003063 if (inp->asconf_supported) {
3064 sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
3065 sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
3066 }
tuexendd729232011-11-01 23:04:43 +00003067 m->default_dscp = 0;
3068#ifdef INET6
3069 m->default_flowlabel = 0;
3070#endif
tuexen9d7b2072011-11-20 16:35:17 +00003071 m->port = 0; /* encapsulation disabled by default */
tuexendd729232011-11-01 23:04:43 +00003072 LIST_INIT(&m->shared_keys);
3073 /* add default NULL key as key id 0 */
3074 null_key = sctp_alloc_sharedkey();
3075 sctp_insert_sharedkey(&m->shared_keys, null_key);
3076 SCTP_INP_WUNLOCK(inp);
3077#ifdef SCTP_LOG_CLOSING
3078 sctp_log_closing(inp, NULL, 12);
3079#endif
3080 return (error);
3081}
3082
3083
3084void
3085sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
3086 struct sctp_tcb *stcb)
3087{
3088 struct sctp_nets *net;
3089 uint16_t lport, rport;
3090 struct sctppcbhead *head;
3091 struct sctp_laddr *laddr, *oladdr;
3092
3093 atomic_add_int(&stcb->asoc.refcnt, 1);
3094 SCTP_TCB_UNLOCK(stcb);
3095 SCTP_INP_INFO_WLOCK();
3096 SCTP_INP_WLOCK(old_inp);
3097 SCTP_INP_WLOCK(new_inp);
3098 SCTP_TCB_LOCK(stcb);
3099 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3100
3101 new_inp->sctp_ep.time_of_secret_change =
3102 old_inp->sctp_ep.time_of_secret_change;
3103 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
3104 sizeof(old_inp->sctp_ep.secret_key));
3105 new_inp->sctp_ep.current_secret_number =
3106 old_inp->sctp_ep.current_secret_number;
3107 new_inp->sctp_ep.last_secret_number =
3108 old_inp->sctp_ep.last_secret_number;
3109 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
3110
3111 /* make it so new data pours into the new socket */
3112 stcb->sctp_socket = new_inp->sctp_socket;
3113 stcb->sctp_ep = new_inp;
3114
3115 /* Copy the port across */
3116 lport = new_inp->sctp_lport = old_inp->sctp_lport;
3117 rport = stcb->rport;
3118 /* Pull the tcb from the old association */
3119 LIST_REMOVE(stcb, sctp_tcbhash);
3120 LIST_REMOVE(stcb, sctp_tcblist);
3121 if (stcb->asoc.in_asocid_hash) {
3122 LIST_REMOVE(stcb, sctp_tcbasocidhash);
3123 }
3124 /* Now insert the new_inp into the TCP connected hash */
3125 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
3126
3127 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
3128 /* Its safe to access */
3129 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3130
3131 /* Now move the tcb into the endpoint list */
3132 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
3133 /*
3134 * Question, do we even need to worry about the ep-hash since we
3135 * only have one connection? Probably not :> so lets get rid of it
3136 * and not suck up any kernel memory in that.
3137 */
3138 if (stcb->asoc.in_asocid_hash) {
3139 struct sctpasochead *lhd;
3140 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
3141 new_inp->hashasocidmark)];
3142 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
3143 }
3144 /* Ok. Let's restart timer. */
3145 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3146 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
3147 stcb, net);
3148 }
3149
3150 SCTP_INP_INFO_WUNLOCK();
3151 if (new_inp->sctp_tcbhash != NULL) {
3152 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
3153 new_inp->sctp_tcbhash = NULL;
3154 }
3155 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3156 /* Subset bound, so copy in the laddr list from the old_inp */
3157 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3158 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3159 if (laddr == NULL) {
3160 /*
3161 * Gak, what can we do? This assoc is really
3162 * HOSED. We probably should send an abort
3163 * here.
3164 */
3165 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3166 continue;
3167 }
3168 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003169 memset(laddr, 0, sizeof(*laddr));
tuexendd729232011-11-01 23:04:43 +00003170 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3171 laddr->ifa = oladdr->ifa;
3172 atomic_add_int(&laddr->ifa->refcount, 1);
3173 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3174 sctp_nxt_addr);
3175 new_inp->laddr_count++;
3176 if (oladdr == stcb->asoc.last_used_address) {
3177 stcb->asoc.last_used_address = laddr;
3178 }
3179 }
3180 }
3181 /* Now any running timers need to be adjusted
3182 * since we really don't care if they are running
3183 * or not just blast in the new_inp into all of
3184 * them.
3185 */
3186
3187 stcb->asoc.dack_timer.ep = (void *)new_inp;
3188 stcb->asoc.asconf_timer.ep = (void *)new_inp;
3189 stcb->asoc.strreset_timer.ep = (void *)new_inp;
3190 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
3191 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
tuexendd729232011-11-01 23:04:43 +00003192 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
3193 /* now what about the nets? */
3194 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3195 net->pmtu_timer.ep = (void *)new_inp;
3196 net->hb_timer.ep = (void *)new_inp;
3197 net->rxt_timer.ep = (void *)new_inp;
3198 }
3199 SCTP_INP_WUNLOCK(new_inp);
3200 SCTP_INP_WUNLOCK(old_inp);
3201}
3202
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003203/*
3204 * insert an laddr entry with the given ifa for the desired list
3205 */
3206static int
3207sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
3208{
3209 struct sctp_laddr *laddr;
3210
3211 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3212 if (laddr == NULL) {
3213 /* out of memory? */
3214 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3215 return (EINVAL);
3216 }
3217 SCTP_INCR_LADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02003218 memset(laddr, 0, sizeof(*laddr));
Michael Tuexen71a83fb2016-02-16 21:52:58 +01003219 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3220 laddr->ifa = ifa;
3221 laddr->action = act;
3222 atomic_add_int(&ifa->refcount, 1);
3223 /* insert it */
3224 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3225
3226 return (0);
3227}
3228
3229/*
3230 * Remove an laddr entry from the local address list (on an assoc)
3231 */
3232static void
3233sctp_remove_laddr(struct sctp_laddr *laddr)
3234{
3235
3236 /* remove from the list */
3237 LIST_REMOVE(laddr, sctp_nxt_addr);
3238 sctp_free_ifa(laddr->ifa);
3239 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
3240 SCTP_DECR_LADDR_COUNT();
3241}
tuexendd729232011-11-01 23:04:43 +00003242
3243#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
3244/*
3245 * Don't know why, but without this there is an unknown reference when
3246 * compiling NetBSD... hmm
3247 */
3248extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3249#endif
3250
3251
3252/* sctp_ifap is used to bypass normal local address validation checks */
3253int
3254#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3255sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
t00fcxenc24796c2014-05-06 15:39:13 +00003256 struct sctp_ifa *sctp_ifap, struct thread *p)
tuexendd729232011-11-01 23:04:43 +00003257#elif defined(__Windows__)
3258sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
t00fcxenc24796c2014-05-06 15:39:13 +00003259 struct sctp_ifa *sctp_ifap, PKTHREAD p)
tuexendd729232011-11-01 23:04:43 +00003260#else
3261sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
t00fcxenc24796c2014-05-06 15:39:13 +00003262 struct sctp_ifa *sctp_ifap, struct proc *p)
tuexendd729232011-11-01 23:04:43 +00003263#endif
3264{
3265 /* bind a ep to a socket address */
3266 struct sctppcbhead *head;
3267 struct sctp_inpcb *inp, *inp_tmp;
Michael Tuexen2e04f872019-08-05 13:56:43 +02003268#if defined(__FreeBSD__) || defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +00003269 struct inpcb *ip_inp;
t00fcxenaff14b62013-11-02 18:02:02 +00003270#endif
tuexendd729232011-11-01 23:04:43 +00003271 int port_reuse_active = 0;
3272 int bindall;
3273#ifdef SCTP_MVRF
3274 int i;
3275#endif
3276 uint16_t lport;
3277 int error;
3278 uint32_t vrf_id;
3279
3280 lport = 0;
tuexendd729232011-11-01 23:04:43 +00003281 bindall = 1;
3282 inp = (struct sctp_inpcb *)so->so_pcb;
Michael Tuexen2e04f872019-08-05 13:56:43 +02003283#if defined(__FreeBSD__) || defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +00003284 ip_inp = (struct inpcb *)so->so_pcb;
t00fcxenaff14b62013-11-02 18:02:02 +00003285#endif
tuexendd729232011-11-01 23:04:43 +00003286#ifdef SCTP_DEBUG
3287 if (addr) {
t00fcxen94dfd1e2012-12-27 08:14:10 +00003288 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
tuexendd729232011-11-01 23:04:43 +00003289 ntohs(((struct sockaddr_in *)addr)->sin_port));
t00fcxen94dfd1e2012-12-27 08:14:10 +00003290 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
tuexendd729232011-11-01 23:04:43 +00003291 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3292 }
3293#endif
3294 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3295 /* already did a bind, subsequent binds NOT allowed ! */
3296 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3297 return (EINVAL);
3298 }
3299#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3300#ifdef INVARIANTS
3301 if (p == NULL)
3302 panic("null proc/thread");
3303#endif
3304#endif
3305 if (addr != NULL) {
3306 switch (addr->sa_family) {
3307#ifdef INET
3308 case AF_INET:
3309 {
3310 struct sockaddr_in *sin;
3311
3312 /* IPV6_V6ONLY socket? */
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003313 if (SCTP_IPV6_V6ONLY(inp)) {
tuexendd729232011-11-01 23:04:43 +00003314 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3315 return (EINVAL);
3316 }
t00fcxena8a26f62012-09-12 19:24:54 +00003317#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003318 if (addr->sa_len != sizeof(*sin)) {
3319 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3320 return (EINVAL);
3321 }
3322#endif
3323
3324 sin = (struct sockaddr_in *)addr;
3325 lport = sin->sin_port;
3326#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
t00fcxenc24796c2014-05-06 15:39:13 +00003327 /*
3328 * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3329 * to the proper value.
3330 */
3331 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
3332 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3333 return (error);
3334 }
tuexendd729232011-11-01 23:04:43 +00003335#endif
3336 if (sin->sin_addr.s_addr != INADDR_ANY) {
3337 bindall = 0;
3338 }
3339 break;
3340 }
3341#endif
3342#ifdef INET6
3343 case AF_INET6:
3344 {
3345 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3346 struct sockaddr_in6 *sin6;
3347
3348 sin6 = (struct sockaddr_in6 *)addr;
3349
t00fcxena8a26f62012-09-12 19:24:54 +00003350#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003351 if (addr->sa_len != sizeof(*sin6)) {
3352 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3353 return (EINVAL);
3354 }
3355#endif
tuexendd729232011-11-01 23:04:43 +00003356 lport = sin6->sin6_port;
3357#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
t00fcxenc24796c2014-05-06 15:39:13 +00003358 /*
3359 * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
3360 * to the proper value.
3361 */
3362 if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
Michael Tuexen72e5cec2019-08-05 13:21:02 +02003363 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
t00fcxenc24796c2014-05-06 15:39:13 +00003364 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3365 return (error);
3366 }
tuexendd729232011-11-01 23:04:43 +00003367#endif
3368 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3369 bindall = 0;
3370#ifdef SCTP_EMBEDDED_V6_SCOPE
3371 /* KAME hack: embed scopeid */
3372#if defined(SCTP_KAME)
3373 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3374 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3375 return (EINVAL);
3376 }
tuexene3851592012-06-01 08:31:45 +00003377#elif defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00003378#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
3379 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003380#else
tuexen9a218b12012-08-04 21:17:58 +00003381 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003382#endif
3383 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3384 return (EINVAL);
3385 }
3386#elif defined(__FreeBSD__)
3387 error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
3388 if (error != 0) {
3389 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3390 return (error);
3391 }
tuexendd729232011-11-01 23:04:43 +00003392#else
3393 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
3394 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3395 return (EINVAL);
3396 }
3397#endif
3398#endif /* SCTP_EMBEDDED_V6_SCOPE */
3399 }
3400#ifndef SCOPEDROUTING
3401 /* this must be cleared for ifa_ifwithaddr() */
3402 sin6->sin6_scope_id = 0;
3403#endif /* SCOPEDROUTING */
3404 break;
3405 }
3406#endif
tuexen09a676e2012-07-08 21:03:18 +00003407#if defined(__Userspace__)
3408 case AF_CONN:
3409 {
3410 struct sockaddr_conn *sconn;
t00fcxenefb47e72012-09-09 08:16:51 +00003411
t00fcxena8a26f62012-09-12 19:24:54 +00003412#ifdef HAVE_SA_LEN
tuexen09a676e2012-07-08 21:03:18 +00003413 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
3414 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3415 return (EINVAL);
3416 }
3417#endif
3418 sconn = (struct sockaddr_conn *)addr;
3419 lport = sconn->sconn_port;
3420 if (sconn->sconn_addr != NULL) {
3421 bindall = 0;
3422 }
3423 break;
3424 }
3425#endif
tuexendd729232011-11-01 23:04:43 +00003426 default:
3427 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
3428 return (EAFNOSUPPORT);
3429 }
3430 }
3431 SCTP_INP_INFO_WLOCK();
3432 SCTP_INP_WLOCK(inp);
3433 /* Setup a vrf_id to be the default for the non-bind-all case. */
t00fcxenc24796c2014-05-06 15:39:13 +00003434 vrf_id = inp->def_vrf_id;
tuexendd729232011-11-01 23:04:43 +00003435
3436 /* increase our count due to the unlock we do */
3437 SCTP_INP_INCR_REF(inp);
3438 if (lport) {
3439 /*
t00fcxenefb47e72012-09-09 08:16:51 +00003440 * Did the caller specify a port? if so we must see if an ep
tuexendd729232011-11-01 23:04:43 +00003441 * already has this one bound.
3442 */
3443 /* got to be root to get at low ports */
3444#if !defined(__Windows__)
3445 if (ntohs(lport) < IPPORT_RESERVED) {
Michael Tuexen4c4cbd92017-09-02 21:38:23 +02003446 if ((p != NULL) && ((error =
tuexendd729232011-11-01 23:04:43 +00003447#ifdef __FreeBSD__
3448#if __FreeBSD_version > 602000
3449 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3450#elif __FreeBSD_version >= 500000
3451 suser_cred(p->td_ucred, 0)
3452#else
3453 suser(p)
3454#endif
3455#elif defined(__APPLE__)
3456 suser(p->p_ucred, &p->p_acflag)
3457#elif defined(__Userspace__) /* must be true to use raw socket */
t00fcxenc24796c2014-05-06 15:39:13 +00003458 1
tuexendd729232011-11-01 23:04:43 +00003459#else
3460 suser(p, 0)
3461#endif
Michael Tuexen4c4cbd92017-09-02 21:38:23 +02003462 ) != 0)) {
tuexendd729232011-11-01 23:04:43 +00003463 SCTP_INP_DECR_REF(inp);
3464 SCTP_INP_WUNLOCK(inp);
3465 SCTP_INP_INFO_WUNLOCK();
3466 return (error);
3467 }
3468#if defined(__Panda__)
3469 if (!SCTP_IS_PRIVILEDGED(so)) {
3470 SCTP_INP_DECR_REF(inp);
3471 SCTP_INP_WUNLOCK(inp);
3472 SCTP_INP_INFO_WUNLOCK();
3473 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EACCES);
3474 return (EACCES);
3475 }
3476#endif
3477 }
tuexendd729232011-11-01 23:04:43 +00003478#endif /* __Windows__ */
3479 SCTP_INP_WUNLOCK(inp);
3480 if (bindall) {
3481#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003482 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003483 vrf_id = inp->m_vrf_ids[i];
3484#else
3485 vrf_id = inp->def_vrf_id;
3486#endif
3487 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3488 if (inp_tmp != NULL) {
3489 /*
3490 * lock guy returned and lower count
3491 * note that we are not bound so
3492 * inp_tmp should NEVER be inp. And
3493 * it is this inp (inp_tmp) that gets
3494 * the reference bump, so we must
3495 * lower it.
3496 */
3497 SCTP_INP_DECR_REF(inp_tmp);
3498 /* unlock info */
3499 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3500 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3501 /* Ok, must be one-2-one and allowing port re-use */
3502 port_reuse_active = 1;
3503 goto continue_anyway;
3504 }
3505 SCTP_INP_DECR_REF(inp);
3506 SCTP_INP_INFO_WUNLOCK();
3507 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3508 return (EADDRINUSE);
3509 }
3510#ifdef SCTP_MVRF
3511 }
3512#endif
3513 } else {
3514 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3515 if (inp_tmp != NULL) {
3516 /*
3517 * lock guy returned and lower count note
3518 * that we are not bound so inp_tmp should
3519 * NEVER be inp. And it is this inp (inp_tmp)
3520 * that gets the reference bump, so we must
3521 * lower it.
3522 */
3523 SCTP_INP_DECR_REF(inp_tmp);
3524 /* unlock info */
3525 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3526 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3527 /* Ok, must be one-2-one and allowing port re-use */
3528 port_reuse_active = 1;
3529 goto continue_anyway;
3530 }
3531 SCTP_INP_DECR_REF(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 continue_anyway:
3538 SCTP_INP_WLOCK(inp);
3539 if (bindall) {
3540 /* verify that no lport is not used by a singleton */
t00fcxenefb47e72012-09-09 08:16:51 +00003541 if ((port_reuse_active == 0) &&
3542 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
tuexendd729232011-11-01 23:04:43 +00003543 /* Sorry someone already has this one bound */
3544 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3545 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3546 port_reuse_active = 1;
3547 } else {
3548 SCTP_INP_DECR_REF(inp);
3549 SCTP_INP_WUNLOCK(inp);
3550 SCTP_INP_INFO_WUNLOCK();
3551 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3552 return (EADDRINUSE);
3553 }
3554 }
3555 }
3556 } else {
3557 uint16_t first, last, candidate;
t00fcxenc24796c2014-05-06 15:39:13 +00003558 uint16_t count;
tuexendd729232011-11-01 23:04:43 +00003559 int done;
3560
3561#if defined(__Windows__)
3562 first = 1;
3563 last = 0xffff;
3564#else
3565#if defined(__Userspace__)
t00fcxenc24796c2014-05-06 15:39:13 +00003566 /* TODO ensure uid is 0, etc... */
tuexendd729232011-11-01 23:04:43 +00003567#elif defined(__FreeBSD__) || defined(__APPLE__)
t00fcxenc24796c2014-05-06 15:39:13 +00003568 if (ip_inp->inp_flags & INP_HIGHPORT) {
3569 first = MODULE_GLOBAL(ipport_hifirstauto);
3570 last = MODULE_GLOBAL(ipport_hilastauto);
3571 } else if (ip_inp->inp_flags & INP_LOWPORT) {
tuexendd729232011-11-01 23:04:43 +00003572 if (p && (error =
3573#ifdef __FreeBSD__
3574#if __FreeBSD_version > 602000
3575 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3576#elif __FreeBSD_version >= 500000
3577 suser_cred(p->td_ucred, 0)
3578#else
3579 suser(p)
3580#endif
3581#elif defined(__APPLE__)
3582 suser(p->p_ucred, &p->p_acflag)
3583#else
3584 suser(p, 0)
3585#endif
3586 )) {
3587 SCTP_INP_DECR_REF(inp);
3588 SCTP_INP_WUNLOCK(inp);
3589 SCTP_INP_INFO_WUNLOCK();
3590 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3591 return (error);
3592 }
t00fcxenc24796c2014-05-06 15:39:13 +00003593 first = MODULE_GLOBAL(ipport_lowfirstauto);
3594 last = MODULE_GLOBAL(ipport_lowlastauto);
3595 } else {
tuexendd729232011-11-01 23:04:43 +00003596#endif
t00fcxen8dd95b82014-07-11 20:41:28 +00003597 first = MODULE_GLOBAL(ipport_firstauto);
3598 last = MODULE_GLOBAL(ipport_lastauto);
tuexendd729232011-11-01 23:04:43 +00003599#if defined(__FreeBSD__) || defined(__APPLE__)
t00fcxenc24796c2014-05-06 15:39:13 +00003600 }
tuexendd729232011-11-01 23:04:43 +00003601#endif
3602#endif /* __Windows__ */
3603 if (first > last) {
3604 uint16_t temp;
3605
3606 temp = first;
3607 first = last;
3608 last = temp;
3609 }
3610 count = last - first + 1; /* number of candidates */
3611 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3612
3613 done = 0;
3614 while (!done) {
3615#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003616 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003617 if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
3618 break;
3619 }
3620 }
3621 if (i == inp->num_vrfs) {
3622 done = 1;
3623 }
3624#else
3625 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3626 done = 1;
3627 }
3628#endif
3629 if (!done) {
3630 if (--count == 0) {
3631 SCTP_INP_DECR_REF(inp);
3632 SCTP_INP_WUNLOCK(inp);
3633 SCTP_INP_INFO_WUNLOCK();
3634 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3635 return (EADDRINUSE);
3636 }
3637 if (candidate == last)
3638 candidate = first;
3639 else
3640 candidate = candidate + 1;
3641 }
3642 }
3643 lport = htons(candidate);
3644 }
3645 SCTP_INP_DECR_REF(inp);
3646 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3647 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3648 /*
3649 * this really should not happen. The guy did a non-blocking
3650 * bind and then did a close at the same time.
3651 */
3652 SCTP_INP_WUNLOCK(inp);
3653 SCTP_INP_INFO_WUNLOCK();
3654 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3655 return (EINVAL);
3656 }
3657 /* ok we look clear to give out this port, so lets setup the binding */
3658 if (bindall) {
3659 /* binding to all addresses, so just set in the proper flags */
3660 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3661 /* set the automatic addr changes from kernel flag */
3662 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3663 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3664 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3665 } else {
3666 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3667 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3668 }
3669 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3670 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3671 } else {
3672 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3673 }
3674 /* set the automatic mobility_base from kernel
3675 flag (by micchie)
3676 */
3677 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3678 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3679 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003680 } else {
tuexendd729232011-11-01 23:04:43 +00003681 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3682 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3683 }
3684 /* set the automatic mobility_fasthandoff from kernel
3685 flag (by micchie)
3686 */
3687 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3688 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3689 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003690 } else {
tuexendd729232011-11-01 23:04:43 +00003691 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3692 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3693 }
3694 } else {
3695 /*
3696 * bind specific, make sure flags is off and add a new
3697 * address structure to the sctp_addr_list inside the ep
3698 * structure.
3699 *
3700 * We will need to allocate one and insert it at the head. The
3701 * socketopt call can just insert new addresses in there as
3702 * well. It will also have to do the embed scope kame hack
3703 * too (before adding).
3704 */
3705 struct sctp_ifa *ifa;
t00fcxen05f07c02014-09-07 10:15:26 +00003706 union sctp_sockstore store;
tuexendd729232011-11-01 23:04:43 +00003707
t00fcxen05f07c02014-09-07 10:15:26 +00003708 memset(&store, 0, sizeof(store));
tuexendd729232011-11-01 23:04:43 +00003709 switch (addr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00003710#ifdef INET
tuexendd729232011-11-01 23:04:43 +00003711 case AF_INET:
t00fcxen05f07c02014-09-07 10:15:26 +00003712 memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
3713 store.sin.sin_port = 0;
tuexendd729232011-11-01 23:04:43 +00003714 break;
tuexen310f1bc2012-07-15 11:20:56 +00003715#endif
3716#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00003717 case AF_INET6:
t00fcxen05f07c02014-09-07 10:15:26 +00003718 memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
3719 store.sin6.sin6_port = 0;
tuexendd729232011-11-01 23:04:43 +00003720 break;
tuexen310f1bc2012-07-15 11:20:56 +00003721#endif
tuexencc2b1422012-07-10 21:50:32 +00003722#if defined(__Userspace__)
3723 case AF_CONN:
t00fcxen05f07c02014-09-07 10:15:26 +00003724 memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn));
3725 store.sconn.sconn_port = 0;
tuexencc2b1422012-07-10 21:50:32 +00003726 break;
tuexencc2b1422012-07-10 21:50:32 +00003727#endif
tuexendd729232011-11-01 23:04:43 +00003728 default:
3729 break;
3730 }
3731 /*
3732 * first find the interface with the bound address need to
3733 * zero out the port to find the address! yuck! can't do
3734 * this earlier since need port for sctp_pcb_findep()
3735 */
t00fcxenefb47e72012-09-09 08:16:51 +00003736 if (sctp_ifap != NULL) {
tuexendd729232011-11-01 23:04:43 +00003737 ifa = sctp_ifap;
t00fcxenefb47e72012-09-09 08:16:51 +00003738 } else {
tuexendd729232011-11-01 23:04:43 +00003739 /* Note for BSD we hit here always other
3740 * O/S's will pass things in via the
3741 * sctp_ifap argument (Panda).
3742 */
t00fcxen05f07c02014-09-07 10:15:26 +00003743 ifa = sctp_find_ifa_by_addr(&store.sa,
tuexendd729232011-11-01 23:04:43 +00003744 vrf_id, SCTP_ADDR_NOT_LOCKED);
3745 }
3746 if (ifa == NULL) {
3747 /* Can't find an interface with that address */
3748 SCTP_INP_WUNLOCK(inp);
3749 SCTP_INP_INFO_WUNLOCK();
3750 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3751 return (EADDRNOTAVAIL);
3752 }
3753#ifdef INET6
3754 if (addr->sa_family == AF_INET6) {
3755 /* GAK, more FIXME IFA lock? */
3756 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3757 /* Can't bind a non-existent addr. */
3758 SCTP_INP_WUNLOCK(inp);
3759 SCTP_INP_INFO_WUNLOCK();
3760 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3761 return (EINVAL);
3762 }
3763 }
3764#endif
3765 /* we're not bound all */
3766 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3767 /* allow bindx() to send ASCONF's for binding changes */
3768 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3769 /* clear automatic addr changes from kernel flag */
3770 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3771
3772 /* add this address to the endpoint list */
3773 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3774 if (error != 0) {
3775 SCTP_INP_WUNLOCK(inp);
3776 SCTP_INP_INFO_WUNLOCK();
3777 return (error);
3778 }
3779 inp->laddr_count++;
3780 }
3781 /* find the bucket */
tuexen98456cf2012-04-19 15:37:07 +00003782 if (port_reuse_active) {
tuexendd729232011-11-01 23:04:43 +00003783 /* Put it into tcp 1-2-1 hash */
3784 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3785 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
t00fcxenc24796c2014-05-06 15:39:13 +00003786 } else {
tuexendd729232011-11-01 23:04:43 +00003787 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3788 }
3789 /* put it in the bucket */
3790 LIST_INSERT_HEAD(head, inp, sctp_hash);
3791 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 +00003792 (void *)head, ntohs(lport), port_reuse_active);
tuexendd729232011-11-01 23:04:43 +00003793 /* set in the port */
3794 inp->sctp_lport = lport;
3795
3796 /* turn off just the unbound flag */
3797 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3798 SCTP_INP_WUNLOCK(inp);
3799 SCTP_INP_INFO_WUNLOCK();
3800 return (0);
3801}
3802
3803
3804static void
3805sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3806{
3807 struct sctp_iterator *it, *nit;
3808
3809 /*
3810 * We enter with the only the ITERATOR_LOCK in place and a write
3811 * lock on the inp_info stuff.
3812 */
3813 it = sctp_it_ctl.cur_it;
3814#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3815 if (it && (it->vn != curvnet)) {
3816 /* Its not looking at our VNET */
3817 return;
3818 }
3819#endif
3820 if (it && (it->inp == inp)) {
3821 /*
3822 * This is tricky and we hold the iterator lock,
3823 * but when it returns and gets the lock (when we
3824 * release it) the iterator will try to operate on
3825 * inp. We need to stop that from happening. But
3826 * of course the iterator has a reference on the
3827 * stcb and inp. We can mark it and it will stop.
3828 *
3829 * If its a single iterator situation, we
3830 * set the end iterator flag. Otherwise
3831 * we set the iterator to go to the next inp.
3832 *
3833 */
3834 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3835 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3836 } else {
3837 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3838 }
3839 }
3840 /* Now go through and remove any single reference to
3841 * our inp that may be still pending on the list
3842 */
3843 SCTP_IPI_ITERATOR_WQ_LOCK();
3844 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3845#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3846 if (it->vn != curvnet) {
3847 continue;
tuexen09631162012-04-18 11:23:02 +00003848 }
tuexendd729232011-11-01 23:04:43 +00003849#endif
3850 if (it->inp == inp) {
3851 /* This one points to me is it inp specific? */
3852 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3853 /* Remove and free this one */
3854 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3855 it, sctp_nxt_itr);
3856 if (it->function_atend != NULL) {
3857 (*it->function_atend) (it->pointer, it->val);
3858 }
3859 SCTP_FREE(it, SCTP_M_ITER);
3860 } else {
3861 it->inp = LIST_NEXT(it->inp, sctp_list);
3862 if (it->inp) {
3863 SCTP_INP_INCR_REF(it->inp);
3864 }
3865 }
3866 /* When its put in the refcnt is incremented so decr it */
3867 SCTP_INP_DECR_REF(inp);
3868 }
3869 }
3870 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3871}
3872
3873/* release sctp_inpcb unbind the port */
3874void
3875sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3876{
3877 /*
3878 * Here we free a endpoint. We must find it (if it is in the Hash
3879 * table) and remove it from there. Then we must also find it in the
3880 * overall list and remove it from there. After all removals are
3881 * complete then any timer has to be stopped. Then start the actual
3882 * freeing. a) Any local lists. b) Any associations. c) The hash of
3883 * all associations. d) finally the ep itself.
3884 */
tuexendd729232011-11-01 23:04:43 +00003885 struct sctp_tcb *asoc, *nasoc;
3886 struct sctp_laddr *laddr, *nladdr;
3887 struct inpcb *ip_pcb;
3888 struct socket *so;
tuexen63fc0bb2011-12-27 12:24:52 +00003889 int being_refed = 0;
tuexendd729232011-11-01 23:04:43 +00003890 struct sctp_queued_to_read *sq, *nsq;
3891#if !defined(__Panda__) && !defined(__Userspace__)
3892#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
3893 sctp_rtentry_t *rt;
3894#endif
3895#endif
3896 int cnt;
3897 sctp_sharedkey_t *shared_key, *nshared_key;
3898
3899
3900#if defined(__APPLE__)
3901 sctp_lock_assert(SCTP_INP_SO(inp));
3902#endif
3903#ifdef SCTP_LOG_CLOSING
3904 sctp_log_closing(inp, NULL, 0);
3905#endif
3906 SCTP_ITERATOR_LOCK();
3907 /* mark any iterators on the list or being processed */
3908 sctp_iterator_inp_being_freed(inp);
3909 SCTP_ITERATOR_UNLOCK();
3910 so = inp->sctp_socket;
3911 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3912 /* been here before.. eeks.. get out of here */
3913 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3914#ifdef SCTP_LOG_CLOSING
3915 sctp_log_closing(inp, NULL, 1);
3916#endif
3917 return;
3918 }
3919 SCTP_ASOC_CREATE_LOCK(inp);
3920 SCTP_INP_INFO_WLOCK();
3921
3922 SCTP_INP_WLOCK(inp);
3923 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3924 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3925 /* socket is gone, so no more wakeups allowed */
3926 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3927 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3928 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3929
3930 }
3931 /* First time through we have the socket lock, after that no more. */
3932 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
Michael Tuexen390beeb2020-02-04 15:05:45 +01003933 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
tuexendd729232011-11-01 23:04:43 +00003934
3935 if (inp->control) {
3936 sctp_m_freem(inp->control);
3937 inp->control = NULL;
3938 }
3939 if (inp->pkt) {
3940 sctp_m_freem(inp->pkt);
3941 inp->pkt = NULL;
3942 }
tuexendd729232011-11-01 23:04:43 +00003943 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3944 * here but I will be nice :> (i.e.
3945 * ip_pcb = ep;) */
3946 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3947 int cnt_in_sd;
3948
3949 cnt_in_sd = 0;
3950 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3951 SCTP_TCB_LOCK(asoc);
3952 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3953 /* Skip guys being freed */
3954 cnt_in_sd++;
3955 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3956 /*
3957 * Special case - we did not start a kill
3958 * timer on the asoc due to it was not
3959 * closed. So go ahead and start it now.
3960 */
Michael Tuexen348a36c2018-08-13 16:24:47 +02003961 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00003962 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3963 }
3964 SCTP_TCB_UNLOCK(asoc);
3965 continue;
3966 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02003967 if (((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3968 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
tuexendd729232011-11-01 23:04:43 +00003969 (asoc->asoc.total_output_queue_size == 0)) {
3970 /* If we have data in queue, we don't want to just
3971 * free since the app may have done, send()/close
3972 * or connect/send/close. And it wants the data
3973 * to get across first.
3974 */
3975 /* Just abandon things in the front states */
3976 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00003977 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
tuexendd729232011-11-01 23:04:43 +00003978 cnt_in_sd++;
3979 }
3980 continue;
3981 }
3982 /* Disconnect the socket please */
3983 asoc->sctp_socket = NULL;
Michael Tuexen348a36c2018-08-13 16:24:47 +02003984 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET);
tuexendd729232011-11-01 23:04:43 +00003985 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3986 (asoc->asoc.control_pdapi) ||
3987 (asoc->asoc.size_on_all_streams > 0) ||
tuexen63fc0bb2011-12-27 12:24:52 +00003988 (so && (so->so_rcv.sb_cc > 0))) {
tuexendd729232011-11-01 23:04:43 +00003989 /* Left with Data unread */
3990 struct mbuf *op_err;
3991
t00fcxen08f9ff92014-03-16 13:38:54 +00003992 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00003993 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
tuexendd729232011-11-01 23:04:43 +00003994 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3995 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen348a36c2018-08-13 16:24:47 +02003996 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3997 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00003998 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3999 }
4000 if (sctp_free_assoc(inp, asoc,
t00fcxen0057a6d2015-05-28 16:42:49 +00004001 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
tuexendd729232011-11-01 23:04:43 +00004002 cnt_in_sd++;
4003 }
4004 continue;
4005 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
4006 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
t00fcxen28611aa2012-09-23 07:45:40 +00004007 (asoc->asoc.stream_queue_cnt == 0)) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02004008 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
tuexendd729232011-11-01 23:04:43 +00004009 goto abort_anyway;
4010 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004011 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4012 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
tuexendd729232011-11-01 23:04:43 +00004013 struct sctp_nets *netp;
4014
t00fcxend0ad16b2013-02-09 18:34:24 +00004015 /*
4016 * there is nothing queued to send,
4017 * so I send shutdown
4018 */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004019 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4020 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004021 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4022 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004023 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
t00fcxend0ad16b2013-02-09 18:34:24 +00004024 sctp_stop_timers_for_shutdown(asoc);
tuexendd729232011-11-01 23:04:43 +00004025 if (asoc->asoc.alternate) {
4026 netp = asoc->asoc.alternate;
4027 } else {
4028 netp = asoc->asoc.primary_destination;
4029 }
tuexendd729232011-11-01 23:04:43 +00004030 sctp_send_shutdown(asoc, netp);
tuexendd729232011-11-01 23:04:43 +00004031 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
4032 netp);
Michael Tuexend07a5f22020-03-19 23:34:46 +01004033 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
tuexendd729232011-11-01 23:04:43 +00004034 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
4035 }
4036 } else {
4037 /* mark into shutdown pending */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004038 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
Michael Tuexend07a5f22020-03-19 23:34:46 +01004039 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02004040 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02004041 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT);
tuexendd729232011-11-01 23:04:43 +00004042 }
4043 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
4044 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
4045 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4046 struct mbuf *op_err;
4047 abort_anyway:
t00fcxen08f9ff92014-03-16 13:38:54 +00004048 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00004049 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
tuexendd729232011-11-01 23:04:43 +00004050 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4051 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
Michael Tuexen348a36c2018-08-13 16:24:47 +02004052 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4053 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004054 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4055 }
4056 if (sctp_free_assoc(inp, asoc,
4057 SCTP_PCBFREE_NOFORCE,
t00fcxen0057a6d2015-05-28 16:42:49 +00004058 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
tuexendd729232011-11-01 23:04:43 +00004059 cnt_in_sd++;
4060 }
4061 continue;
4062 } else {
4063 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
4064 }
4065 }
4066 cnt_in_sd++;
4067 SCTP_TCB_UNLOCK(asoc);
4068 }
4069 /* now is there some left in our SHUTDOWN state? */
4070 if (cnt_in_sd) {
4071#ifdef SCTP_LOG_CLOSING
4072 sctp_log_closing(inp, NULL, 2);
4073#endif
4074 inp->sctp_socket = NULL;
4075 SCTP_INP_WUNLOCK(inp);
4076 SCTP_ASOC_CREATE_UNLOCK(inp);
4077 SCTP_INP_INFO_WUNLOCK();
4078 return;
4079 }
4080 }
4081 inp->sctp_socket = NULL;
4082 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
4083 SCTP_PCB_FLAGS_UNBOUND) {
4084 /*
4085 * ok, this guy has been bound. It's port is
4086 * somewhere in the SCTP_BASE_INFO(hash table). Remove
4087 * it!
4088 */
4089 LIST_REMOVE(inp, sctp_hash);
4090 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
4091 }
4092
4093 /* If there is a timer running to kill us,
4094 * forget it, since it may have a contest
4095 * on the INP lock.. which would cause us
4096 * to die ...
4097 */
4098 cnt = 0;
4099 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
4100 SCTP_TCB_LOCK(asoc);
4101 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4102 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02004103 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
t00fcxen28611aa2012-09-23 07:45:40 +00004104 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
tuexendd729232011-11-01 23:04:43 +00004105 }
Michael Tuexen61f66832020-02-09 23:15:19 +01004106 cnt++;
tuexendd729232011-11-01 23:04:43 +00004107 SCTP_TCB_UNLOCK(asoc);
4108 continue;
4109 }
4110 /* Free associations that are NOT killing us */
Michael Tuexen348a36c2018-08-13 16:24:47 +02004111 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
tuexendd729232011-11-01 23:04:43 +00004112 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
4113 struct mbuf *op_err;
t00fcxen28611aa2012-09-23 07:45:40 +00004114
t00fcxen08f9ff92014-03-16 13:38:54 +00004115 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
t00fcxen0057a6d2015-05-28 16:42:49 +00004116 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
tuexendd729232011-11-01 23:04:43 +00004117 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4118 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4119 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4120 cnt++;
4121 SCTP_TCB_UNLOCK(asoc);
4122 continue;
4123 }
Michael Tuexen348a36c2018-08-13 16:24:47 +02004124 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4125 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
tuexendd729232011-11-01 23:04:43 +00004126 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4127 }
t00fcxen0057a6d2015-05-28 16:42:49 +00004128 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE,
4129 SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
tuexendd729232011-11-01 23:04:43 +00004130 cnt++;
4131 }
4132 }
4133 if (cnt) {
4134 /* Ok we have someone out there that will kill us */
tuexendd729232011-11-01 23:04:43 +00004135#ifdef SCTP_LOG_CLOSING
4136 sctp_log_closing(inp, NULL, 3);
4137#endif
4138 SCTP_INP_WUNLOCK(inp);
4139 SCTP_ASOC_CREATE_UNLOCK(inp);
4140 SCTP_INP_INFO_WUNLOCK();
4141 return;
4142 }
4143 if (SCTP_INP_LOCK_CONTENDED(inp))
4144 being_refed++;
4145 if (SCTP_INP_READ_CONTENDED(inp))
4146 being_refed++;
tuexen63fc0bb2011-12-27 12:24:52 +00004147 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
tuexendd729232011-11-01 23:04:43 +00004148 being_refed++;
4149
tuexen63fc0bb2011-12-27 12:24:52 +00004150 if ((inp->refcount) ||
t00fcxen28611aa2012-09-23 07:45:40 +00004151 (being_refed) ||
4152 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
tuexendd729232011-11-01 23:04:43 +00004153#ifdef SCTP_LOG_CLOSING
4154 sctp_log_closing(inp, NULL, 4);
4155#endif
4156 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4157 SCTP_INP_WUNLOCK(inp);
4158 SCTP_ASOC_CREATE_UNLOCK(inp);
4159 SCTP_INP_INFO_WUNLOCK();
4160 return;
4161 }
4162 inp->sctp_ep.signature_change.type = 0;
4163 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4164 /* Remove it from the list .. last thing we need a
4165 * lock for.
4166 */
4167 LIST_REMOVE(inp, sctp_list);
4168 SCTP_INP_WUNLOCK(inp);
4169 SCTP_ASOC_CREATE_UNLOCK(inp);
4170 SCTP_INP_INFO_WUNLOCK();
4171 /* Now we release all locks. Since this INP
4172 * cannot be found anymore except possibly by the
4173 * kill timer that might be running. We call
4174 * the drain function here. It should hit the case
4175 * were it sees the ACTIVE flag cleared and exit
4176 * out freeing us to proceed and destroy everything.
4177 */
4178 if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
4179 (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
4180 } else {
4181 /* Probably un-needed */
4182 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4183 }
4184
4185#ifdef SCTP_LOG_CLOSING
4186 sctp_log_closing(inp, NULL, 5);
4187#endif
4188
4189#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4190#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4191 rt = ip_pcb->inp_route.ro_rt;
4192#endif
4193#endif
4194
tuexendd729232011-11-01 23:04:43 +00004195 if ((inp->sctp_asocidhash) != NULL) {
4196 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4197 inp->sctp_asocidhash = NULL;
4198 }
4199 /*sa_ignore FREED_MEMORY*/
4200 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4201 /* Its only abandoned if it had data left */
4202 if (sq->length)
4203 SCTP_STAT_INCR(sctps_left_abandon);
4204
4205 TAILQ_REMOVE(&inp->read_queue, sq, next);
4206 sctp_free_remote_addr(sq->whoFrom);
4207 if (so)
4208 so->so_rcv.sb_cc -= sq->length;
4209 if (sq->data) {
4210 sctp_m_freem(sq->data);
4211 sq->data = NULL;
4212 }
4213 /*
4214 * no need to free the net count, since at this point all
4215 * assoc's are gone.
4216 */
Michael Tuexene5001952016-04-17 19:25:27 +02004217 sctp_free_a_readq(NULL, sq);
tuexendd729232011-11-01 23:04:43 +00004218 }
4219 /* Now the sctp_pcb things */
4220 /*
4221 * free each asoc if it is not already closed/free. we can't use the
4222 * macro here since le_next will get freed as part of the
4223 * sctp_free_assoc() call.
4224 */
tuexendd729232011-11-01 23:04:43 +00004225#ifndef __Panda__
4226 if (ip_pcb->inp_options) {
4227 (void)sctp_m_free(ip_pcb->inp_options);
4228 ip_pcb->inp_options = 0;
4229 }
4230#endif
4231
4232#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4233#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4234 if (rt) {
4235 RTFREE(rt);
4236 ip_pcb->inp_route.ro_rt = 0;
4237 }
4238#endif
tuexen5fc22842012-02-11 13:32:19 +00004239#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00004240#ifdef INET
4241 if (ip_pcb->inp_moptions) {
4242 inp_freemoptions(ip_pcb->inp_moptions);
4243 ip_pcb->inp_moptions = 0;
4244 }
4245#endif
4246#endif
4247#endif
4248
4249#ifdef INET6
t00fcxen4c76a912012-09-14 14:31:21 +00004250#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4251#if defined(__FreeBSD__) || defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +00004252 if (ip_pcb->inp_vflag & INP_IPV6) {
t00fcxen4c76a912012-09-14 14:31:21 +00004253#else
4254 if (inp->inp_vflag & INP_IPV6) {
tuexendd729232011-11-01 23:04:43 +00004255#endif
Michael Tuexen801a64b2019-08-05 15:33:37 +02004256 ip6_freepcbopts(ip_pcb->in6p_outputopts);
tuexendd729232011-11-01 23:04:43 +00004257 }
t00fcxen4c76a912012-09-14 14:31:21 +00004258#endif
tuexendd729232011-11-01 23:04:43 +00004259#endif /* INET6 */
4260#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
4261 inp->inp_vflag = 0;
4262#else
4263 ip_pcb->inp_vflag = 0;
4264#endif
4265 /* free up authentication fields */
4266 if (inp->sctp_ep.local_auth_chunks != NULL)
4267 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4268 if (inp->sctp_ep.local_hmacs != NULL)
4269 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4270
4271 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4272 LIST_REMOVE(shared_key, next);
4273 sctp_free_sharedkey(shared_key);
4274 /*sa_ignore FREED_MEMORY*/
4275 }
4276
4277#if defined(__APPLE__)
4278 inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
tuexen6d01b942012-05-04 18:15:17 +00004279 if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
tuexen49fdcc82012-05-04 10:13:12 +00004280#ifdef INVARIANTS
t00fcxen8fca7ff2012-09-05 18:58:42 +00004281 panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen6d01b942012-05-04 18:15:17 +00004282#else
t00fcxen8fca7ff2012-09-05 18:58:42 +00004283 SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen49fdcc82012-05-04 10:13:12 +00004284#endif
tuexen6d01b942012-05-04 18:15:17 +00004285 }
tuexendd729232011-11-01 23:04:43 +00004286 inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4287#endif
4288 /*
4289 * if we have an address list the following will free the list of
4290 * ifaddr's that are set into this ep. Again macro limitations here,
4291 * since the LIST_FOREACH could be a bad idea.
4292 */
4293 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4294 sctp_remove_laddr(laddr);
4295 }
4296
4297#ifdef SCTP_TRACK_FREED_ASOCS
4298 /* TEMP CODE */
4299 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
4300 LIST_REMOVE(asoc, sctp_tcblist);
4301 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
4302 SCTP_DECR_ASOC_COUNT();
4303 }
4304 /* *** END TEMP CODE ****/
4305#endif
4306#ifdef SCTP_MVRF
4307 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4308#endif
4309 /* Now lets see about freeing the EP hash table. */
4310 if (inp->sctp_tcbhash != NULL) {
4311 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4312 inp->sctp_tcbhash = NULL;
4313 }
4314 /* Now we must put the ep memory back into the zone pool */
4315#if defined(__FreeBSD__)
t00fcxen326aa4b2014-06-20 13:42:13 +00004316 crfree(inp->ip_inp.inp.inp_cred);
tuexendd729232011-11-01 23:04:43 +00004317 INP_LOCK_DESTROY(&inp->ip_inp.inp);
4318#endif
4319 SCTP_INP_LOCK_DESTROY(inp);
4320 SCTP_INP_READ_DESTROY(inp);
4321 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
4322#if !defined(__APPLE__)
4323 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4324 SCTP_DECR_EP_COUNT();
4325#else
4326 /* For Tiger, we will do this later... */
4327#endif
4328}
4329
4330
4331struct sctp_nets *
4332sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4333{
4334 struct sctp_nets *net;
4335 /* locate the address */
4336 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4337 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4338 return (net);
4339 }
4340 return (NULL);
4341}
4342
4343
4344int
4345sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4346{
4347#ifdef __Panda__
4348 return (0);
4349#else
4350 struct sctp_ifa *sctp_ifa;
4351 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4352 if (sctp_ifa) {
4353 return (1);
4354 } else {
4355 return (0);
4356 }
4357#endif
4358}
4359
4360/*
4361 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4362 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4363 * stats of stuff.
4364 */
4365int
4366sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
Michael Tuexen27574422016-04-30 16:35:29 +02004367 struct sctp_nets **netp, uint16_t port, int set_scope, int from)
tuexendd729232011-11-01 23:04:43 +00004368{
4369 /*
4370 * The following is redundant to the same lines in the
4371 * sctp_aloc_assoc() but is needed since others call the add
4372 * address function
4373 */
4374 struct sctp_nets *net, *netfirst;
4375 int addr_inscope;
4376
4377 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4378 from);
4379 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4380
4381 netfirst = sctp_findnet(stcb, newaddr);
4382 if (netfirst) {
4383 /*
4384 * Lie and return ok, we don't want to make the association
4385 * go away for this behavior. It will happen in the TCP
4386 * model in a connected socket. It does not reach the hash
4387 * table until after the association is built so it can't be
4388 * found. Mark as reachable, since the initial creation will
4389 * have been cleared and the NOT_IN_ASSOC flag will have
4390 * been added... and we don't want to end up removing it
4391 * back out.
4392 */
4393 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4394 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4395 SCTP_ADDR_UNCONFIRMED);
4396 } else {
4397 netfirst->dest_state = SCTP_ADDR_REACHABLE;
4398 }
4399
4400 return (0);
4401 }
4402 addr_inscope = 1;
4403 switch (newaddr->sa_family) {
4404#ifdef INET
4405 case AF_INET:
4406 {
4407 struct sockaddr_in *sin;
4408
4409 sin = (struct sockaddr_in *)newaddr;
4410 if (sin->sin_addr.s_addr == 0) {
4411 /* Invalid address */
4412 return (-1);
4413 }
Michael Tuexend0853302017-07-19 17:01:31 +02004414 /* zero out the zero area */
tuexendd729232011-11-01 23:04:43 +00004415 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4416
4417 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004418#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00004419 sin->sin_len = sizeof(struct sockaddr_in);
4420#endif
4421 if (set_scope) {
tuexendd729232011-11-01 23:04:43 +00004422 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004423 stcb->asoc.scope.ipv4_local_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004424 }
tuexendd729232011-11-01 23:04:43 +00004425 } else {
4426 /* Validate the address is in scope */
4427 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004428 (stcb->asoc.scope.ipv4_local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004429 addr_inscope = 0;
4430 }
4431 }
4432 break;
4433 }
4434#endif
4435#ifdef INET6
4436 case AF_INET6:
4437 {
4438 struct sockaddr_in6 *sin6;
4439
4440 sin6 = (struct sockaddr_in6 *)newaddr;
4441 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4442 /* Invalid address */
4443 return (-1);
4444 }
4445 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004446#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00004447 sin6->sin6_len = sizeof(struct sockaddr_in6);
4448#endif
4449 if (set_scope) {
4450 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004451 stcb->asoc.scope.loopback_scope = 1;
4452 stcb->asoc.scope.local_scope = 0;
4453 stcb->asoc.scope.ipv4_local_scope = 1;
4454 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004455 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4456 /*
4457 * If the new destination is a LINK_LOCAL we
4458 * must have common site scope. Don't set
4459 * the local scope since we may not share
4460 * all links, only loopback can do this.
4461 * Links on the local network would also be
4462 * on our private network for v4 too.
4463 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004464 stcb->asoc.scope.ipv4_local_scope = 1;
4465 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004466 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4467 /*
4468 * If the new destination is SITE_LOCAL then
4469 * we must have site scope in common.
4470 */
t00fcxend0ad16b2013-02-09 18:34:24 +00004471 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004472 }
4473 } else {
4474 /* Validate the address is in scope */
4475 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004476 (stcb->asoc.scope.loopback_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004477 addr_inscope = 0;
4478 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004479 (stcb->asoc.scope.local_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004480 addr_inscope = 0;
4481 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
t00fcxend0ad16b2013-02-09 18:34:24 +00004482 (stcb->asoc.scope.site_scope == 0)) {
tuexendd729232011-11-01 23:04:43 +00004483 addr_inscope = 0;
4484 }
4485 }
4486 break;
4487 }
4488#endif
tuexencc2b1422012-07-10 21:50:32 +00004489#if defined(__Userspace__)
4490 case AF_CONN:
4491 {
4492 struct sockaddr_conn *sconn;
4493
4494 sconn = (struct sockaddr_conn *)newaddr;
4495 if (sconn->sconn_addr == NULL) {
4496 /* Invalid address */
4497 return (-1);
4498 }
t00fcxen8fbaee32012-09-04 16:41:39 +00004499#ifdef HAVE_SCONN_LEN
tuexencc2b1422012-07-10 21:50:32 +00004500 sconn->sconn_len = sizeof(struct sockaddr_conn);
4501#endif
4502 break;
4503 }
4504#endif
tuexendd729232011-11-01 23:04:43 +00004505 default:
4506 /* not supported family type */
4507 return (-1);
4508 }
4509 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4510 if (net == NULL) {
4511 return (-1);
4512 }
4513 SCTP_INCR_RADDR_COUNT();
Michael Tuexend0853302017-07-19 17:01:31 +02004514 memset(net, 0, sizeof(struct sctp_nets));
tuexendd729232011-11-01 23:04:43 +00004515 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
t00fcxen907930b2012-09-12 19:43:22 +00004516#ifdef HAVE_SA_LEN
4517 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
4518#endif
tuexendd729232011-11-01 23:04:43 +00004519 switch (newaddr->sa_family) {
4520#ifdef INET
4521 case AF_INET:
t00fcxen907930b2012-09-12 19:43:22 +00004522#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004523 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4524#endif
4525 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4526 break;
4527#endif
4528#ifdef INET6
4529 case AF_INET6:
t00fcxen907930b2012-09-12 19:43:22 +00004530#ifndef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00004531 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4532#endif
4533 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4534 break;
4535#endif
tuexencc2b1422012-07-10 21:50:32 +00004536#if defined(__Userspace__)
4537 case AF_CONN:
t00fcxen907930b2012-09-12 19:43:22 +00004538#ifndef HAVE_SA_LEN
tuexencc2b1422012-07-10 21:50:32 +00004539 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4540#endif
4541 ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4542 break;
4543#endif
tuexendd729232011-11-01 23:04:43 +00004544 default:
4545 break;
4546 }
4547 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4548 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
t00fcxend0ad16b2013-02-09 18:34:24 +00004549 stcb->asoc.scope.loopback_scope = 1;
4550 stcb->asoc.scope.ipv4_local_scope = 1;
4551 stcb->asoc.scope.local_scope = 0;
4552 stcb->asoc.scope.site_scope = 1;
tuexendd729232011-11-01 23:04:43 +00004553 addr_inscope = 1;
4554 }
4555 net->failure_threshold = stcb->asoc.def_net_failure;
4556 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4557 if (addr_inscope == 0) {
4558 net->dest_state = (SCTP_ADDR_REACHABLE |
4559 SCTP_ADDR_OUT_OF_SCOPE);
4560 } else {
4561 if (from == SCTP_ADDR_IS_CONFIRMED)
4562 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4563 net->dest_state = SCTP_ADDR_REACHABLE;
4564 else
4565 net->dest_state = SCTP_ADDR_REACHABLE |
4566 SCTP_ADDR_UNCONFIRMED;
4567 }
4568 /* We set this to 0, the timer code knows that
4569 * this means its an initial value
4570 */
4571 net->rto_needed = 1;
t00fcxen8dd95b82014-07-11 20:41:28 +00004572 net->RTO = 0;
tuexendd729232011-11-01 23:04:43 +00004573 net->RTO_measured = 0;
4574 stcb->asoc.numnets++;
tuexen9d7b2072011-11-20 16:35:17 +00004575 net->ref_count = 1;
tuexendd729232011-11-01 23:04:43 +00004576 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
Michael Tuexen27574422016-04-30 16:35:29 +02004577 net->port = port;
tuexendd729232011-11-01 23:04:43 +00004578 net->dscp = stcb->asoc.default_dscp;
4579#ifdef INET6
4580 net->flowlabel = stcb->asoc.default_flowlabel;
4581#endif
4582 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4583 net->dest_state |= SCTP_ADDR_NOHB;
4584 } else {
4585 net->dest_state &= ~SCTP_ADDR_NOHB;
4586 }
4587 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4588 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4589 } else {
4590 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4591 }
4592 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4593 /* Init the timer structure */
4594 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4595 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4596 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4597
4598 /* Now generate a route for this guy */
4599#ifdef INET6
4600#ifdef SCTP_EMBEDDED_V6_SCOPE
4601 /* KAME hack: embed scopeid */
4602 if (newaddr->sa_family == AF_INET6) {
4603 struct sockaddr_in6 *sin6;
4604
4605 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
tuexene3851592012-06-01 08:31:45 +00004606#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00004607#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4608 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
tuexendd729232011-11-01 23:04:43 +00004609#else
tuexen9a218b12012-08-04 21:17:58 +00004610 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
tuexendd729232011-11-01 23:04:43 +00004611#endif
4612#elif defined(SCTP_KAME)
4613 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4614#else
4615 (void)in6_embedscope(&sin6->sin6_addr, sin6);
4616#endif
4617#ifndef SCOPEDROUTING
4618 sin6->sin6_scope_id = 0;
4619#endif
4620 }
4621#endif /* SCTP_EMBEDDED_V6_SCOPE */
4622#endif
t00fcxena51e4462015-06-17 15:53:23 +00004623 SCTP_RTALLOC((sctp_route_t *)&net->ro,
4624 stcb->asoc.vrf_id,
4625 stcb->sctp_ep->fibnum);
tuexendd729232011-11-01 23:04:43 +00004626
t00fcxen8b6ef092014-07-11 17:37:40 +00004627 net->src_addr_selected = 0;
Michael Tuexend900e5f2017-06-23 10:52:43 +02004628#if !defined(__Userspace__)
tuexendd729232011-11-01 23:04:43 +00004629 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4630 /* Get source address */
4631 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004632 stcb,
4633 (sctp_route_t *)&net->ro,
4634 net,
4635 0,
4636 stcb->asoc.vrf_id);
Michael Tuexende0630d2017-11-03 22:04:01 +01004637 if (stcb->asoc.default_mtu > 0) {
4638 net->mtu = stcb->asoc.default_mtu;
4639 switch (net->ro._l_addr.sa.sa_family) {
4640#ifdef INET
4641 case AF_INET:
4642 net->mtu += SCTP_MIN_V4_OVERHEAD;
4643 break;
4644#endif
4645#ifdef INET6
4646 case AF_INET6:
4647 net->mtu += SCTP_MIN_OVERHEAD;
4648 break;
4649#endif
4650#if defined(__Userspace__)
4651 case AF_CONN:
4652 net->mtu += sizeof(struct sctphdr);
4653 break;
4654#endif
4655 default:
4656 break;
4657 }
4658#if defined(INET) || defined(INET6)
4659 if (net->port) {
4660 net->mtu += (uint32_t)sizeof(struct udphdr);
4661 }
4662#endif
4663 } else if (net->ro._s_addr != NULL) {
Michael Tuexend900e5f2017-06-23 10:52:43 +02004664 uint32_t imtu, rmtu, hcmtu;
4665
t00fcxen8b6ef092014-07-11 17:37:40 +00004666 net->src_addr_selected = 1;
4667 /* Now get the interface MTU */
4668 if (net->ro._s_addr->ifn_p != NULL) {
Michael Tuexend900e5f2017-06-23 10:52:43 +02004669 imtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
4670 } else {
4671 imtu = 0;
t00fcxen8b6ef092014-07-11 17:37:40 +00004672 }
Michael Tuexend900e5f2017-06-23 10:52:43 +02004673#if defined(__FreeBSD__)
Michael Tuexena0515422020-05-18 00:47:04 +02004674 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 +02004675 hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
4676#else
Michael Tuexena0515422020-05-18 00:47:04 +02004677 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 +02004678 hcmtu = 0;
4679#endif
4680 net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
Michael Tuexena0515422020-05-18 00:47:04 +02004681#if defined(__FreeBSD__)
4682#else
tuexendd729232011-11-01 23:04:43 +00004683 if (rmtu == 0) {
4684 /* Start things off to match mtu of interface please. */
4685 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
Michael Tuexend900e5f2017-06-23 10:52:43 +02004686 net->ro.ro_rt, net->mtu);
tuexendd729232011-11-01 23:04:43 +00004687 }
Michael Tuexena0515422020-05-18 00:47:04 +02004688#endif
t00fcxen8b6ef092014-07-11 17:37:40 +00004689 }
tuexen09631162012-04-18 11:23:02 +00004690 }
t00fcxen6d60eb02013-08-30 07:37:01 +00004691#endif
tuexene81177b2011-11-15 20:48:08 +00004692 if (net->mtu == 0) {
Michael Tuexende0630d2017-11-03 22:04:01 +01004693 if (stcb->asoc.default_mtu > 0) {
4694 net->mtu = stcb->asoc.default_mtu;
4695 switch (net->ro._l_addr.sa.sa_family) {
tuexene81177b2011-11-15 20:48:08 +00004696#ifdef INET
Michael Tuexende0630d2017-11-03 22:04:01 +01004697 case AF_INET:
4698 net->mtu += SCTP_MIN_V4_OVERHEAD;
4699 break;
tuexene81177b2011-11-15 20:48:08 +00004700#endif
4701#ifdef INET6
Michael Tuexende0630d2017-11-03 22:04:01 +01004702 case AF_INET6:
4703 net->mtu += SCTP_MIN_OVERHEAD;
4704 break;
tuexene81177b2011-11-15 20:48:08 +00004705#endif
tuexencc2b1422012-07-10 21:50:32 +00004706#if defined(__Userspace__)
Michael Tuexende0630d2017-11-03 22:04:01 +01004707 case AF_CONN:
4708 net->mtu += sizeof(struct sctphdr);
4709 break;
tuexencc2b1422012-07-10 21:50:32 +00004710#endif
Michael Tuexende0630d2017-11-03 22:04:01 +01004711 default:
4712 break;
4713 }
4714#if defined(INET) || defined(INET6)
4715 if (net->port) {
4716 net->mtu += (uint32_t)sizeof(struct udphdr);
4717 }
4718#endif
4719 } else {
4720 switch (newaddr->sa_family) {
4721#ifdef INET
4722 case AF_INET:
4723 net->mtu = SCTP_DEFAULT_MTU;
4724 break;
4725#endif
4726#ifdef INET6
4727 case AF_INET6:
4728 net->mtu = 1280;
4729 break;
4730#endif
4731#if defined(__Userspace__)
4732 case AF_CONN:
4733 net->mtu = 1280;
4734 break;
4735#endif
4736 default:
4737 break;
4738 }
tuexendd729232011-11-01 23:04:43 +00004739 }
tuexene81177b2011-11-15 20:48:08 +00004740 }
t00fcxen1ce83bb2014-07-11 07:17:36 +00004741#if defined(INET) || defined(INET6)
tuexene81177b2011-11-15 20:48:08 +00004742 if (net->port) {
4743 net->mtu -= (uint32_t)sizeof(struct udphdr);
4744 }
t00fcxen5ab37c82014-06-30 20:55:44 +00004745#endif
tuexene81177b2011-11-15 20:48:08 +00004746 if (from == SCTP_ALLOC_ASOC) {
4747 stcb->asoc.smallest_mtu = net->mtu;
4748 }
4749 if (stcb->asoc.smallest_mtu > net->mtu) {
Michael Tuexen923f7352016-05-01 23:55:35 +02004750 sctp_pathmtu_adjustment(stcb, net->mtu);
tuexendd729232011-11-01 23:04:43 +00004751 }
4752#ifdef INET6
4753#ifdef SCTP_EMBEDDED_V6_SCOPE
4754 if (newaddr->sa_family == AF_INET6) {
4755 struct sockaddr_in6 *sin6;
4756
4757 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4758#ifdef SCTP_KAME
4759 (void)sa6_recoverscope(sin6);
4760#else
4761 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4762#endif /* SCTP_KAME */
4763 }
4764#endif /* SCTP_EMBEDDED_V6_SCOPE */
4765#endif
tuexendd729232011-11-01 23:04:43 +00004766
4767 /* JRS - Use the congestion control given in the CC module */
tuexen09631162012-04-18 11:23:02 +00004768 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
tuexendd729232011-11-01 23:04:43 +00004769 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4770
4771 /*
4772 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4773 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4774 */
4775 net->find_pseudo_cumack = 1;
4776 net->find_rtx_pseudo_cumack = 1;
tuexendd729232011-11-01 23:04:43 +00004777#if defined(__FreeBSD__)
4778 /* Choose an initial flowid. */
4779 net->flowid = stcb->asoc.my_vtag ^
4780 ntohs(stcb->rport) ^
4781 ntohs(stcb->sctp_ep->sctp_lport);
Michael Tuexen15f35f82016-06-07 09:19:50 +02004782 net->flowtype = M_HASHTYPE_OPAQUE_HASH;
tuexendd729232011-11-01 23:04:43 +00004783#endif
4784 if (netp) {
4785 *netp = net;
4786 }
4787 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
Michael Tuexena0515422020-05-18 00:47:04 +02004788#if defined(__FreeBSD__)
4789 if (net->ro.ro_nh == NULL) {
4790#else
tuexendd729232011-11-01 23:04:43 +00004791 if (net->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004792#endif
tuexendd729232011-11-01 23:04:43 +00004793 /* Since we have no route put it at the back */
4794 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4795 } else if (netfirst == NULL) {
4796 /* We are the first one in the pool. */
4797 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexena0515422020-05-18 00:47:04 +02004798#if defined(__FreeBSD__)
4799 } else if (netfirst->ro.ro_nh == NULL) {
4800#else
tuexendd729232011-11-01 23:04:43 +00004801 } else if (netfirst->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004802#endif
tuexendd729232011-11-01 23:04:43 +00004803 /*
4804 * First one has NO route. Place this one ahead of the first
4805 * one.
4806 */
4807 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
Michael Tuexena0515422020-05-18 00:47:04 +02004808#if defined(__FreeBSD__)
4809 } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
4810#else
tuexendd729232011-11-01 23:04:43 +00004811 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
Michael Tuexena0515422020-05-18 00:47:04 +02004812#endif
tuexendd729232011-11-01 23:04:43 +00004813 /*
4814 * This one has a different interface than the one at the
4815 * top of the list. Place it ahead.
4816 */
4817 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004818 } else {
4819 /*
4820 * Ok we have the same interface as the first one. Move
4821 * forward until we find either a) one with a NULL route...
4822 * insert ahead of that b) one with a different ifp.. insert
4823 * after that. c) end of the list.. insert at the tail.
4824 */
4825 struct sctp_nets *netlook;
4826
4827 do {
4828 netlook = TAILQ_NEXT(netfirst, sctp_next);
4829 if (netlook == NULL) {
4830 /* End of the list */
4831 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4832 break;
Michael Tuexena0515422020-05-18 00:47:04 +02004833#if defined(__FreeBSD__)
4834 } else if (netlook->ro.ro_nh == NULL) {
4835#else
tuexendd729232011-11-01 23:04:43 +00004836 } else if (netlook->ro.ro_rt == NULL) {
Michael Tuexena0515422020-05-18 00:47:04 +02004837#endif
tuexendd729232011-11-01 23:04:43 +00004838 /* next one has NO route */
4839 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4840 break;
Michael Tuexena0515422020-05-18 00:47:04 +02004841#if defined(__FreeBSD__)
4842 } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
tuexendd729232011-11-01 23:04:43 +00004843#else
Michael Tuexena0515422020-05-18 00:47:04 +02004844 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
tuexendd729232011-11-01 23:04:43 +00004845#endif
tuexendd729232011-11-01 23:04:43 +00004846 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
Michael Tuexena0515422020-05-18 00:47:04 +02004847 net, sctp_next);
tuexendd729232011-11-01 23:04:43 +00004848 break;
4849 }
tuexendd729232011-11-01 23:04:43 +00004850 /* Shift forward */
4851 netfirst = netlook;
tuexendd729232011-11-01 23:04:43 +00004852 } while (netlook != NULL);
4853 }
4854
4855 /* got to have a primary set */
4856 if (stcb->asoc.primary_destination == 0) {
4857 stcb->asoc.primary_destination = net;
Michael Tuexena0515422020-05-18 00:47:04 +02004858#if defined(__FreeBSD__)
4859 } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
4860 (net->ro.ro_nh) &&
4861#else
tuexendd729232011-11-01 23:04:43 +00004862 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
Michael Tuexena0515422020-05-18 00:47:04 +02004863 (net->ro.ro_rt) &&
4864#endif
4865 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
tuexendd729232011-11-01 23:04:43 +00004866 /* No route to current primary adopt new primary */
4867 stcb->asoc.primary_destination = net;
4868 }
4869 /* Validate primary is first */
4870 net = TAILQ_FIRST(&stcb->asoc.nets);
4871 if ((net != stcb->asoc.primary_destination) &&
4872 (stcb->asoc.primary_destination)) {
4873 /* first one on the list is NOT the primary
4874 * sctp_cmpaddr() is much more efficient if
4875 * the primary is the first on the list, make it
4876 * so.
4877 */
4878 TAILQ_REMOVE(&stcb->asoc.nets,
4879 stcb->asoc.primary_destination, sctp_next);
4880 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4881 stcb->asoc.primary_destination, sctp_next);
4882 }
4883 return (0);
4884}
4885
4886
4887static uint32_t
4888sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4889{
4890 uint32_t id;
4891 struct sctpasochead *head;
4892 struct sctp_tcb *lstcb;
4893
tuexendd729232011-11-01 23:04:43 +00004894 try_again:
4895 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4896 /* TSNH */
tuexendd729232011-11-01 23:04:43 +00004897 return (0);
4898 }
4899 /*
4900 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4901 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4902 */
4903 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4904 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4905 }
4906 id = inp->sctp_associd_counter;
4907 inp->sctp_associd_counter++;
4908 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4909 if (lstcb) {
4910 goto try_again;
4911 }
4912 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4913 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4914 stcb->asoc.in_asocid_hash = 1;
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01004915 return (id);
tuexendd729232011-11-01 23:04:43 +00004916}
4917
4918/*
4919 * allocate an association and add it to the endpoint. The caller must be
4920 * careful to add all additional addresses once they are know right away or
4921 * else the assoc will be may experience a blackout scenario.
4922 */
4923struct sctp_tcb *
4924sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
Michael Tuexen49335e12015-12-06 17:31:45 +01004925 int *error, uint32_t override_tag, uint32_t vrf_id,
Michael Tuexen923f7352016-05-01 23:55:35 +02004926 uint16_t o_streams, uint16_t port,
tuexendd729232011-11-01 23:04:43 +00004927#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004928 struct thread *p,
tuexendd729232011-11-01 23:04:43 +00004929#elif defined(__Windows__)
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004930 PKTHREAD p,
tuexendd729232011-11-01 23:04:43 +00004931#else
4932#if defined(__Userspace__)
4933 /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
4934#endif
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004935 struct proc *p,
tuexendd729232011-11-01 23:04:43 +00004936#endif
Michael Tuexenbfb04f32019-07-14 14:42:17 +02004937 int initialize_auth_params)
tuexendd729232011-11-01 23:04:43 +00004938{
4939 /* note the p argument is only valid in unbound sockets */
4940
4941 struct sctp_tcb *stcb;
4942 struct sctp_association *asoc;
4943 struct sctpasochead *head;
4944 uint16_t rport;
4945 int err;
4946
4947 /*
4948 * Assumption made here: Caller has done a
4949 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4950 * address does not exist already.
4951 */
4952 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4953 /* Hit max assoc, sorry no more */
4954 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4955 *error = ENOBUFS;
4956 return (NULL);
4957 }
4958 if (firstaddr == NULL) {
4959 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4960 *error = EINVAL;
4961 return (NULL);
4962 }
4963 SCTP_INP_RLOCK(inp);
4964 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4965 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4966 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4967 /*
4968 * If its in the TCP pool, its NOT allowed to create an
4969 * association. The parent listener needs to call
4970 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4971 * off, or connected one does this.. its an error.
4972 */
4973 SCTP_INP_RUNLOCK(inp);
4974 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4975 *error = EINVAL;
4976 return (NULL);
4977 }
4978 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4979 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4980 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4981 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4982 SCTP_INP_RUNLOCK(inp);
4983 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4984 *error = EINVAL;
4985 return (NULL);
4986 }
4987 }
4988 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4989#ifdef SCTP_DEBUG
4990 if (firstaddr) {
4991 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4992 switch (firstaddr->sa_family) {
4993#ifdef INET
4994 case AF_INET:
4995 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4996 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4997 break;
4998#endif
4999#ifdef INET6
5000 case AF_INET6:
5001 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
5002 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
5003 break;
5004#endif
tuexencc2b1422012-07-10 21:50:32 +00005005#if defined(__Userspace__)
5006 case AF_CONN:
5007 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
5008 ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
5009 break;
5010#endif
tuexendd729232011-11-01 23:04:43 +00005011 default:
5012 break;
5013 }
5014 } else {
5015 SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
5016 }
5017#endif /* SCTP_DEBUG */
5018 switch (firstaddr->sa_family) {
5019#ifdef INET
5020 case AF_INET:
5021 {
5022 struct sockaddr_in *sin;
5023
5024 sin = (struct sockaddr_in *)firstaddr;
5025 if ((ntohs(sin->sin_port) == 0) ||
5026 (sin->sin_addr.s_addr == INADDR_ANY) ||
5027 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
5028 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
5029 /* Invalid address */
5030 SCTP_INP_RUNLOCK(inp);
5031 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5032 *error = EINVAL;
5033 return (NULL);
5034 }
5035 rport = sin->sin_port;
5036 break;
5037 }
5038#endif
5039#ifdef INET6
5040 case AF_INET6:
5041 {
5042 struct sockaddr_in6 *sin6;
5043
5044 sin6 = (struct sockaddr_in6 *)firstaddr;
5045 if ((ntohs(sin6->sin6_port) == 0) ||
5046 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
5047 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
5048 /* Invalid address */
5049 SCTP_INP_RUNLOCK(inp);
5050 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5051 *error = EINVAL;
5052 return (NULL);
5053 }
5054 rport = sin6->sin6_port;
5055 break;
5056 }
5057#endif
tuexencc2b1422012-07-10 21:50:32 +00005058#if defined(__Userspace__)
5059 case AF_CONN:
5060 {
5061 struct sockaddr_conn *sconn;
5062
5063 sconn = (struct sockaddr_conn *)firstaddr;
5064 if ((ntohs(sconn->sconn_port) == 0) ||
5065 (sconn->sconn_addr == NULL)) {
5066 /* Invalid address */
5067 SCTP_INP_RUNLOCK(inp);
5068 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5069 *error = EINVAL;
5070 return (NULL);
5071 }
5072 rport = sconn->sconn_port;
5073 break;
5074 }
5075#endif
tuexendd729232011-11-01 23:04:43 +00005076 default:
5077 /* not supported family type */
5078 SCTP_INP_RUNLOCK(inp);
5079 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5080 *error = EINVAL;
5081 return (NULL);
5082 }
5083 SCTP_INP_RUNLOCK(inp);
5084 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
5085 /*
5086 * If you have not performed a bind, then we need to do the
5087 * ephemeral bind for you.
5088 */
5089 if ((err = sctp_inpcb_bind(inp->sctp_socket,
5090 (struct sockaddr *)NULL,
5091 (struct sctp_ifa *)NULL,
5092#ifndef __Panda__
5093 p
5094#else
5095 (struct proc *)NULL
5096#endif
5097 ))) {
5098 /* bind error, probably perm */
5099 *error = err;
5100 return (NULL);
5101 }
5102 }
5103 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
5104 if (stcb == NULL) {
5105 /* out of memory? */
5106 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
5107 *error = ENOMEM;
5108 return (NULL);
5109 }
5110 SCTP_INCR_ASOC_COUNT();
5111
Michael Tuexend0853302017-07-19 17:01:31 +02005112 memset(stcb, 0, sizeof(*stcb));
tuexendd729232011-11-01 23:04:43 +00005113 asoc = &stcb->asoc;
5114
tuexendd729232011-11-01 23:04:43 +00005115 SCTP_TCB_LOCK_INIT(stcb);
5116 SCTP_TCB_SEND_LOCK_INIT(stcb);
5117 stcb->rport = rport;
5118 /* setup back pointer's */
5119 stcb->sctp_ep = inp;
5120 stcb->sctp_socket = inp->sctp_socket;
Michael Tuexen49335e12015-12-06 17:31:45 +01005121 if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id, o_streams))) {
tuexendd729232011-11-01 23:04:43 +00005122 /* failed */
5123 SCTP_TCB_LOCK_DESTROY(stcb);
5124 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
tuexendd729232011-11-01 23:04:43 +00005125 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5126 SCTP_DECR_ASOC_COUNT();
5127 *error = err;
5128 return (NULL);
5129 }
5130 /* and the port */
5131 SCTP_INP_INFO_WLOCK();
5132 SCTP_INP_WLOCK(inp);
5133 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5134 /* inpcb freed while alloc going on */
5135 SCTP_TCB_LOCK_DESTROY(stcb);
5136 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
tuexendd729232011-11-01 23:04:43 +00005137 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5138 SCTP_INP_WUNLOCK(inp);
5139 SCTP_INP_INFO_WUNLOCK();
5140 SCTP_DECR_ASOC_COUNT();
5141 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5142 *error = EINVAL;
5143 return (NULL);
5144 }
5145 SCTP_TCB_LOCK(stcb);
5146
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01005147 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
tuexendd729232011-11-01 23:04:43 +00005148 /* now that my_vtag is set, add it to the hash */
5149 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
5150 /* put it in the bucket in the vtag hash of assoc's for the system */
5151 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
5152 SCTP_INP_INFO_WUNLOCK();
5153
Michael Tuexen923f7352016-05-01 23:55:35 +02005154 if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
tuexendd729232011-11-01 23:04:43 +00005155 /* failure.. memory error? */
5156 if (asoc->strmout) {
5157 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5158 asoc->strmout = NULL;
5159 }
5160 if (asoc->mapping_array) {
5161 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5162 asoc->mapping_array = NULL;
5163 }
5164 if (asoc->nr_mapping_array) {
5165 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5166 asoc->nr_mapping_array = NULL;
5167 }
5168 SCTP_DECR_ASOC_COUNT();
t00fcxen2c2ff922013-11-03 14:03:35 +00005169 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005170 SCTP_TCB_LOCK_DESTROY(stcb);
5171 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5172 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5173 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5174 SCTP_INP_WUNLOCK(inp);
5175 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
5176 *error = ENOBUFS;
5177 return (NULL);
5178 }
5179 /* Init all the timers */
5180 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
5181 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
5182 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
5183 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
5184 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
tuexendd729232011-11-01 23:04:43 +00005185 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5186
5187 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5188 /* now file the port under the hash as well */
5189 if (inp->sctp_tcbhash != NULL) {
5190 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5191 inp->sctp_hashmark)];
5192 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5193 }
Michael Tuexenbfb04f32019-07-14 14:42:17 +02005194 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
5195 sctp_initialize_auth_params(inp, stcb);
5196 }
tuexendd729232011-11-01 23:04:43 +00005197 SCTP_INP_WUNLOCK(inp);
t00fcxen8fca7ff2012-09-05 18:58:42 +00005198 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
tuexendd729232011-11-01 23:04:43 +00005199 return (stcb);
5200}
5201
5202
5203void
5204sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5205{
Michael Tuexen21b55df2020-02-09 23:46:32 +01005206 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00005207 struct sctp_association *asoc;
5208
Michael Tuexen21b55df2020-02-09 23:46:32 +01005209 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00005210 asoc = &stcb->asoc;
5211 asoc->numnets--;
5212 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5213 if (net == asoc->primary_destination) {
5214 /* Reset primary */
5215 struct sctp_nets *lnet;
5216
5217 lnet = TAILQ_FIRST(&asoc->nets);
5218 /* Mobility adaptation
5219 Ideally, if deleted destination is the primary, it becomes
5220 a fast retransmission trigger by the subsequent SET PRIMARY.
5221 (by micchie)
5222 */
5223 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005224 SCTP_MOBILITY_BASE) ||
tuexendd729232011-11-01 23:04:43 +00005225 sctp_is_mobility_feature_on(stcb->sctp_ep,
t00fcxen8dd95b82014-07-11 20:41:28 +00005226 SCTP_MOBILITY_FASTHANDOFF)) {
tuexendd729232011-11-01 23:04:43 +00005227 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5228 if (asoc->deleted_primary != NULL) {
5229 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5230 goto out;
5231 }
5232 asoc->deleted_primary = net;
5233 atomic_add_int(&net->ref_count, 1);
5234 memset(&net->lastsa, 0, sizeof(net->lastsa));
5235 memset(&net->lastsv, 0, sizeof(net->lastsv));
5236 sctp_mobility_feature_on(stcb->sctp_ep,
5237 SCTP_MOBILITY_PRIM_DELETED);
5238 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5239 stcb->sctp_ep, stcb, NULL);
5240 }
5241out:
5242 /* Try to find a confirmed primary */
5243 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5244 }
5245 if (net == asoc->last_data_chunk_from) {
5246 /* Reset primary */
5247 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5248 }
5249 if (net == asoc->last_control_chunk_from) {
5250 /* Clear net */
5251 asoc->last_control_chunk_from = NULL;
5252 }
5253 if (net == stcb->asoc.alternate) {
5254 sctp_free_remote_addr(stcb->asoc.alternate);
5255 stcb->asoc.alternate = NULL;
5256 }
Michael Tuexen21b55df2020-02-09 23:46:32 +01005257 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5258 SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
5259 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5260 SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
Michael Tuexenf52d3df2020-02-11 19:24:34 +01005261 net->dest_state |= SCTP_ADDR_BEING_DELETED;
tuexendd729232011-11-01 23:04:43 +00005262 sctp_free_remote_addr(net);
5263}
5264
5265/*
5266 * remove a remote endpoint address from an association, it will fail if the
5267 * address does not exist.
5268 */
5269int
5270sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5271{
5272 /*
5273 * Here we need to remove a remote address. This is quite simple, we
5274 * first find it in the list of address for the association
5275 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5276 * on that item. Note we do not allow it to be removed if there are
5277 * no other addresses.
5278 */
5279 struct sctp_association *asoc;
5280 struct sctp_nets *net, *nnet;
5281
5282 asoc = &stcb->asoc;
5283
5284 /* locate the address */
5285 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5286 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5287 continue;
5288 }
5289 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5290 remaddr)) {
5291 /* we found the guy */
5292 if (asoc->numnets < 2) {
5293 /* Must have at LEAST two remote addresses */
5294 return (-1);
5295 } else {
5296 sctp_remove_net(stcb, net);
5297 return (0);
5298 }
5299 }
5300 }
5301 /* not found. */
5302 return (-2);
5303}
5304
5305void
5306sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5307{
5308 struct sctpvtaghead *chain;
5309 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005310 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005311 int i;
5312
5313 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
t00fcxen14f8fbb2013-06-25 09:32:47 +00005314 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5315 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5316 if ((twait_block->vtag_block[i].v_tag == tag) &&
5317 (twait_block->vtag_block[i].lport == lport) &&
5318 (twait_block->vtag_block[i].rport == rport)) {
5319 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5320 twait_block->vtag_block[i].v_tag = 0;
5321 twait_block->vtag_block[i].lport = 0;
5322 twait_block->vtag_block[i].rport = 0;
5323 found = 1;
tuexendd729232011-11-01 23:04:43 +00005324 break;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005325 }
tuexendd729232011-11-01 23:04:43 +00005326 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005327 if (found)
5328 break;
tuexendd729232011-11-01 23:04:43 +00005329 }
5330}
5331
5332int
5333sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5334{
5335 struct sctpvtaghead *chain;
5336 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005337 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005338 int i;
5339
5340 SCTP_INP_INFO_WLOCK();
5341 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
t00fcxen14f8fbb2013-06-25 09:32:47 +00005342 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5343 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5344 if ((twait_block->vtag_block[i].v_tag == tag) &&
5345 (twait_block->vtag_block[i].lport == lport) &&
5346 (twait_block->vtag_block[i].rport == rport)) {
5347 found = 1;
tuexendd729232011-11-01 23:04:43 +00005348 break;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005349 }
tuexendd729232011-11-01 23:04:43 +00005350 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005351 if (found)
5352 break;
tuexendd729232011-11-01 23:04:43 +00005353 }
5354 SCTP_INP_INFO_WUNLOCK();
tuexen63fc0bb2011-12-27 12:24:52 +00005355 return (found);
tuexendd729232011-11-01 23:04:43 +00005356}
5357
5358
5359void
5360sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
5361{
5362 struct sctpvtaghead *chain;
5363 struct sctp_tagblock *twait_block;
5364 struct timeval now;
5365 int set, i;
5366
5367 if (time == 0) {
5368 /* Its disabled */
5369 return;
5370 }
5371 (void)SCTP_GETTIME_TIMEVAL(&now);
5372 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5373 set = 0;
t00fcxen14f8fbb2013-06-25 09:32:47 +00005374 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
tuexendd729232011-11-01 23:04:43 +00005375 /* Block(s) present, lets find space, and expire on the fly */
t00fcxen14f8fbb2013-06-25 09:32:47 +00005376 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5377 if ((twait_block->vtag_block[i].v_tag == 0) &&
5378 !set) {
5379 twait_block->vtag_block[i].tv_sec_at_expire =
5380 now.tv_sec + time;
5381 twait_block->vtag_block[i].v_tag = tag;
5382 twait_block->vtag_block[i].lport = lport;
5383 twait_block->vtag_block[i].rport = rport;
5384 set = 1;
5385 } else if ((twait_block->vtag_block[i].v_tag) &&
5386 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
5387 /* Audit expires this guy */
5388 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5389 twait_block->vtag_block[i].v_tag = 0;
5390 twait_block->vtag_block[i].lport = 0;
5391 twait_block->vtag_block[i].rport = 0;
5392 if (set == 0) {
5393 /* Reuse it for my new tag */
5394 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
tuexendd729232011-11-01 23:04:43 +00005395 twait_block->vtag_block[i].v_tag = tag;
5396 twait_block->vtag_block[i].lport = lport;
5397 twait_block->vtag_block[i].rport = rport;
5398 set = 1;
tuexendd729232011-11-01 23:04:43 +00005399 }
5400 }
t00fcxen14f8fbb2013-06-25 09:32:47 +00005401 }
5402 if (set) {
5403 /*
5404 * We only do up to the block where we can
5405 * place our tag for audits
5406 */
5407 break;
tuexendd729232011-11-01 23:04:43 +00005408 }
5409 }
5410 /* Need to add a new block to chain */
5411 if (!set) {
5412 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5413 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5414 if (twait_block == NULL) {
tuexendd729232011-11-01 23:04:43 +00005415 return;
5416 }
5417 memset(twait_block, 0, sizeof(struct sctp_tagblock));
5418 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
5419 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
5420 twait_block->vtag_block[0].v_tag = tag;
5421 twait_block->vtag_block[0].lport = lport;
5422 twait_block->vtag_block[0].rport = rport;
5423 }
5424}
5425
Michael Tuexene5001952016-04-17 19:25:27 +02005426void
5427sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
5428{
5429 struct sctp_tmit_chunk *chk, *nchk;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005430 struct sctp_queued_to_read *control, *ncontrol;
5431
5432 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
5433 TAILQ_REMOVE(rh, control, next_instrm);
5434 control->on_strm_q = 0;
5435 if (control->on_read_q == 0) {
5436 sctp_free_remote_addr(control->whoFrom);
5437 if (control->data) {
5438 sctp_m_freem(control->data);
5439 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02005440 }
5441 }
5442 /* Reassembly free? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005443 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5444 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02005445 if (chk->data) {
5446 sctp_m_freem(chk->data);
5447 chk->data = NULL;
5448 }
5449 if (chk->holds_key_ref)
5450 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5451 sctp_free_remote_addr(chk->whoTo);
5452 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5453 SCTP_DECR_CHK_COUNT();
5454 /*sa_ignore FREED_MEMORY*/
5455 }
5456 /*
5457 * We don't free the address here
5458 * since all the net's were freed
5459 * above.
5460 */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02005461 if (control->on_read_q == 0) {
5462 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02005463 }
5464 }
5465}
tuexendd729232011-11-01 23:04:43 +00005466
5467#ifdef __Panda__
5468void panda_wakeup_socket(struct socket *so);
5469#endif
5470
5471/*-
5472 * Free the association after un-hashing the remote port. This
5473 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5474 * expect that the input to this function IS a locked TCB.
5475 * It will return 0, if it did NOT destroy the association (instead
5476 * it unlocks it. It will return NON-zero if it either destroyed the
5477 * association OR the association is already destroyed.
5478 */
5479int
5480sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5481{
5482 int i;
5483 struct sctp_association *asoc;
5484 struct sctp_nets *net, *nnet;
5485 struct sctp_laddr *laddr, *naddr;
5486 struct sctp_tmit_chunk *chk, *nchk;
5487 struct sctp_asconf_addr *aparam, *naparam;
5488 struct sctp_asconf_ack *aack, *naack;
5489 struct sctp_stream_reset_list *strrst, *nstrrst;
5490 struct sctp_queued_to_read *sq, *nsq;
5491 struct sctp_stream_queue_pending *sp, *nsp;
5492 sctp_sharedkey_t *shared_key, *nshared_key;
5493 struct socket *so;
5494
5495 /* first, lets purge the entry from the hash table. */
5496#if defined(__APPLE__)
5497 sctp_lock_assert(SCTP_INP_SO(inp));
5498#endif
5499
5500#ifdef SCTP_LOG_CLOSING
5501 sctp_log_closing(inp, stcb, 6);
5502#endif
5503 if (stcb->asoc.state == 0) {
5504#ifdef SCTP_LOG_CLOSING
5505 sctp_log_closing(inp, NULL, 7);
5506#endif
5507 /* there is no asoc, really TSNH :-0 */
5508 return (1);
5509 }
5510 if (stcb->asoc.alternate) {
5511 sctp_free_remote_addr(stcb->asoc.alternate);
5512 stcb->asoc.alternate = NULL;
5513 }
5514#if !defined(__APPLE__) /* TEMP: moved to below */
5515 /* TEMP CODE */
5516 if (stcb->freed_from_where == 0) {
5517 /* Only record the first place free happened from */
5518 stcb->freed_from_where = from_location;
5519 }
5520 /* TEMP CODE */
5521#endif
5522
5523 asoc = &stcb->asoc;
5524 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5525 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5526 /* nothing around */
5527 so = NULL;
5528 else
5529 so = inp->sctp_socket;
5530
5531 /*
5532 * We used timer based freeing if a reader or writer is in the way.
5533 * So we first check if we are actually being called from a timer,
5534 * if so we abort early if a reader or writer is still in the way.
5535 */
5536 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5537 (from_inpcbfree == SCTP_NORMAL_PROC)) {
5538 /*
5539 * is it the timer driving us? if so are the reader/writers
5540 * gone?
5541 */
5542 if (stcb->asoc.refcnt) {
5543 /* nope, reader or writer in the way */
5544 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5545 /* no asoc destroyed */
5546 SCTP_TCB_UNLOCK(stcb);
5547#ifdef SCTP_LOG_CLOSING
5548 sctp_log_closing(inp, stcb, 8);
5549#endif
5550 return (0);
5551 }
5552 }
Michael Tuexen61f66832020-02-09 23:15:19 +01005553 /* Now clean up any other timers */
Michael Tuexen5f976392020-02-11 21:20:07 +01005554 sctp_stop_association_timers(stcb, false);
tuexendd729232011-11-01 23:04:43 +00005555 /* Now the read queue needs to be cleaned up (only once) */
5556 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005557 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
tuexendd729232011-11-01 23:04:43 +00005558 SCTP_INP_READ_LOCK(inp);
5559 TAILQ_FOREACH(sq, &inp->read_queue, next) {
5560 if (sq->stcb == stcb) {
5561 sq->do_not_ref_stcb = 1;
5562 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5563 /* If there is no end, there never
5564 * will be now.
5565 */
5566 if (sq->end_added == 0) {
5567 /* Held for PD-API clear that. */
5568 sq->pdapi_aborted = 1;
5569 sq->held_length = 0;
5570 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
5571 /*
5572 * Need to add a PD-API aborted indication.
5573 * Setting the control_pdapi assures that it will
5574 * be added right after this msg.
5575 */
5576 uint32_t strseq;
5577 stcb->asoc.control_pdapi = sq;
Michael Tuexen00657ac2016-12-07 21:53:26 +01005578 strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff);
tuexendd729232011-11-01 23:04:43 +00005579 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5580 stcb,
5581 SCTP_PARTIAL_DELIVERY_ABORTED,
5582 (void *)&strseq,
5583 SCTP_SO_LOCKED);
5584 stcb->asoc.control_pdapi = NULL;
5585 }
5586 }
5587 /* Add an end to wake them */
5588 sq->end_added = 1;
5589 }
5590 }
5591 SCTP_INP_READ_UNLOCK(inp);
5592 if (stcb->block_entry) {
5593 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
5594 stcb->block_entry->error = ECONNRESET;
5595 stcb->block_entry = NULL;
5596 }
5597 }
5598 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
5599 /* Someone holds a reference OR the socket is unaccepted yet.
5600 */
5601 if ((stcb->asoc.refcnt) ||
5602 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5603 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005604 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00005605 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5606 }
5607 SCTP_TCB_UNLOCK(stcb);
5608 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5609 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5610 /* nothing around */
5611 so = NULL;
5612 if (so) {
5613 /* Wake any reader/writers */
5614 sctp_sorwakeup(inp, so);
5615 sctp_sowwakeup(inp, so);
5616 }
5617
5618#ifdef SCTP_LOG_CLOSING
5619 sctp_log_closing(inp, stcb, 9);
5620#endif
5621 /* no asoc destroyed */
5622 return (0);
5623 }
5624#ifdef SCTP_LOG_CLOSING
5625 sctp_log_closing(inp, stcb, 10);
5626#endif
5627 /* When I reach here, no others want
5628 * to kill the assoc yet.. and I own
5629 * the lock. Now its possible an abort
5630 * comes in when I do the lock exchange
5631 * below to grab all the locks to do
5632 * the final take out. to prevent this
5633 * we increment the count, which will
5634 * start a timer and blow out above thus
5635 * assuring us that we hold exclusive
5636 * killing of the asoc. Note that
5637 * after getting back the TCB lock
5638 * we will go ahead and increment the
5639 * counter back up and stop any timer
5640 * a passing stranger may have started :-S
5641 */
5642 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5643 atomic_add_int(&stcb->asoc.refcnt, 1);
5644
5645 SCTP_TCB_UNLOCK(stcb);
5646 SCTP_INP_INFO_WLOCK();
5647 SCTP_INP_WLOCK(inp);
5648 SCTP_TCB_LOCK(stcb);
5649 }
5650 /* Double check the GONE flag */
5651 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5652 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5653 /* nothing around */
5654 so = NULL;
5655
Michael Tuexen1f495442020-03-25 16:41:21 +01005656 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5657 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5658 /*
5659 * For TCP type we need special handling when we are
5660 * connected. We also include the peel'ed off ones to.
5661 */
5662 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5663 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5664 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5665 if (so) {
5666 SOCKBUF_LOCK(&so->so_rcv);
5667 so->so_state &= ~(SS_ISCONNECTING |
5668 SS_ISDISCONNECTING |
5669 SS_ISCONFIRMING |
5670 SS_ISCONNECTED);
5671 so->so_state |= SS_ISDISCONNECTED;
5672#if defined(__APPLE__)
5673 socantrcvmore(so);
5674#else
5675 socantrcvmore_locked(so);
5676#endif
5677 socantsendmore(so);
5678 sctp_sowwakeup(inp, so);
5679 sctp_sorwakeup(inp, so);
5680 SCTP_SOWAKEUP(so);
5681 }
5682 }
5683 }
5684
tuexendd729232011-11-01 23:04:43 +00005685 /* Make it invalid too, that way if its
5686 * about to run it will abort and return.
5687 */
5688 /* re-increment the lock */
5689 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5690 atomic_add_int(&stcb->asoc.refcnt, -1);
5691 }
5692 if (stcb->asoc.refcnt) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02005693 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
tuexendd729232011-11-01 23:04:43 +00005694 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5695 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5696 SCTP_INP_INFO_WUNLOCK();
5697 SCTP_INP_WUNLOCK(inp);
5698 }
5699 SCTP_TCB_UNLOCK(stcb);
5700 return (0);
5701 }
5702 asoc->state = 0;
5703 if (inp->sctp_tcbhash) {
5704 LIST_REMOVE(stcb, sctp_tcbhash);
5705 }
5706 if (stcb->asoc.in_asocid_hash) {
5707 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5708 }
5709 /* Now lets remove it from the list of ALL associations in the EP */
5710 LIST_REMOVE(stcb, sctp_tcblist);
5711 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5712 SCTP_INP_INCR_REF(inp);
5713 SCTP_INP_WUNLOCK(inp);
5714 }
5715 /* pull from vtag hash */
5716 LIST_REMOVE(stcb, sctp_asocs);
5717 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
5718 inp->sctp_lport, stcb->rport);
5719
5720 /* Now restop the timers to be sure
5721 * this is paranoia at is finest!
5722 */
Michael Tuexen5f976392020-02-11 21:20:07 +01005723 sctp_stop_association_timers(stcb, true);
tuexendd729232011-11-01 23:04:43 +00005724
tuexendd729232011-11-01 23:04:43 +00005725 /*
5726 * The chunk lists and such SHOULD be empty but we check them just
5727 * in case.
5728 */
5729 /* anything on the wheel needs to be removed */
Michael Tuexenc3dd8e22019-03-26 14:09:18 +01005730 SCTP_TCB_SEND_LOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005731 for (i = 0; i < asoc->streamoutcnt; i++) {
5732 struct sctp_stream_out *outs;
5733
5734 outs = &asoc->strmout[i];
5735 /* now clean up any chunks here */
5736 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
Michael Tuexenc6ae6c62016-08-07 15:17:05 +02005737 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
tuexendd729232011-11-01 23:04:43 +00005738 TAILQ_REMOVE(&outs->outqueue, sp, next);
Michael Tuexenc3dd8e22019-03-26 14:09:18 +01005739 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
tuexen30ca8172012-07-17 13:09:48 +00005740 sctp_free_spbufspace(stcb, asoc, sp);
tuexendd729232011-11-01 23:04:43 +00005741 if (sp->data) {
5742 if (so) {
5743 /* Still an open socket - report */
5744 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
tuexenda53ff02012-05-14 09:00:59 +00005745 0, (void *)sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005746 }
5747 if (sp->data) {
5748 sctp_m_freem(sp->data);
5749 sp->data = NULL;
5750 sp->tail_mbuf = NULL;
tuexen30ca8172012-07-17 13:09:48 +00005751 sp->length = 0;
tuexendd729232011-11-01 23:04:43 +00005752 }
5753 }
5754 if (sp->net) {
5755 sctp_free_remote_addr(sp->net);
5756 sp->net = NULL;
5757 }
tuexen30ca8172012-07-17 13:09:48 +00005758 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005759 }
5760 }
Michael Tuexenc3dd8e22019-03-26 14:09:18 +01005761 SCTP_TCB_SEND_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005762 /*sa_ignore FREED_MEMORY*/
5763 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5764 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5765 SCTP_FREE(strrst, SCTP_M_STRESET);
5766 }
5767 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5768 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5769 if (sq->data) {
5770 sctp_m_freem(sq->data);
5771 sq->data = NULL;
5772 }
5773 sctp_free_remote_addr(sq->whoFrom);
5774 sq->whoFrom = NULL;
5775 sq->stcb = NULL;
5776 /* Free the ctl entry */
Michael Tuexene5001952016-04-17 19:25:27 +02005777 sctp_free_a_readq(stcb, sq);
tuexendd729232011-11-01 23:04:43 +00005778 /*sa_ignore FREED_MEMORY*/
5779 }
5780 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5781 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5782 if (chk->data) {
5783 sctp_m_freem(chk->data);
5784 chk->data = NULL;
5785 }
5786 if (chk->holds_key_ref)
5787 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5788 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5789 SCTP_DECR_CHK_COUNT();
5790 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5791 asoc->free_chunk_cnt--;
5792 /*sa_ignore FREED_MEMORY*/
5793 }
5794 /* pending send queue SHOULD be empty */
5795 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005796 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5797 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen8fcc5142012-11-16 19:46:12 +00005798#ifdef INVARIANTS
5799 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005800 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen8fcc5142012-11-16 19:46:12 +00005801#endif
5802 }
tuexendd729232011-11-01 23:04:43 +00005803 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5804 if (chk->data) {
5805 if (so) {
5806 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005807 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5808 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005809 }
5810 if (chk->data) {
5811 sctp_m_freem(chk->data);
5812 chk->data = NULL;
5813 }
5814 }
5815 if (chk->holds_key_ref)
5816 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5817 if (chk->whoTo) {
5818 sctp_free_remote_addr(chk->whoTo);
5819 chk->whoTo = NULL;
5820 }
5821 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5822 SCTP_DECR_CHK_COUNT();
5823 /*sa_ignore FREED_MEMORY*/
5824 }
5825 /* sent queue SHOULD be empty */
5826 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
t00fcxen8fcc5142012-11-16 19:46:12 +00005827 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005828 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5829 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
t00fcxen9ad90772012-11-07 22:19:57 +00005830#ifdef INVARIANTS
5831 } else {
Michael Tuexen00657ac2016-12-07 21:53:26 +01005832 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
t00fcxen9ad90772012-11-07 22:19:57 +00005833#endif
5834 }
5835 }
tuexendd729232011-11-01 23:04:43 +00005836 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5837 if (chk->data) {
5838 if (so) {
5839 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005840 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5841 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005842 }
5843 if (chk->data) {
5844 sctp_m_freem(chk->data);
5845 chk->data = NULL;
5846 }
5847 }
5848 if (chk->holds_key_ref)
5849 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5850 sctp_free_remote_addr(chk->whoTo);
5851 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5852 SCTP_DECR_CHK_COUNT();
5853 /*sa_ignore FREED_MEMORY*/
5854 }
t00fcxen8fcc5142012-11-16 19:46:12 +00005855#ifdef INVARIANTS
5856 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
5857 if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
5858 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
5859 }
5860 }
5861#endif
tuexendd729232011-11-01 23:04:43 +00005862 /* control queue MAY not be empty */
5863 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5864 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5865 if (chk->data) {
5866 sctp_m_freem(chk->data);
5867 chk->data = NULL;
5868 }
5869 if (chk->holds_key_ref)
5870 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5871 sctp_free_remote_addr(chk->whoTo);
5872 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5873 SCTP_DECR_CHK_COUNT();
5874 /*sa_ignore FREED_MEMORY*/
5875 }
5876 /* ASCONF queue MAY not be empty */
5877 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5878 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5879 if (chk->data) {
5880 sctp_m_freem(chk->data);
5881 chk->data = NULL;
5882 }
5883 if (chk->holds_key_ref)
5884 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5885 sctp_free_remote_addr(chk->whoTo);
5886 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5887 SCTP_DECR_CHK_COUNT();
5888 /*sa_ignore FREED_MEMORY*/
5889 }
tuexendd729232011-11-01 23:04:43 +00005890 if (asoc->mapping_array) {
5891 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5892 asoc->mapping_array = NULL;
5893 }
5894 if (asoc->nr_mapping_array) {
5895 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5896 asoc->nr_mapping_array = NULL;
5897 }
5898 /* the stream outs */
5899 if (asoc->strmout) {
5900 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5901 asoc->strmout = NULL;
5902 }
5903 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5904 if (asoc->strmin) {
tuexendd729232011-11-01 23:04:43 +00005905 for (i = 0; i < asoc->streamincnt; i++) {
Michael Tuexene5001952016-04-17 19:25:27 +02005906 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
5907 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
tuexendd729232011-11-01 23:04:43 +00005908 }
5909 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5910 asoc->strmin = NULL;
5911 }
5912 asoc->streamincnt = 0;
5913 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5914#ifdef INVARIANTS
5915 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5916 panic("no net's left alloc'ed, or list points to itself");
5917 }
5918#endif
5919 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5920 sctp_free_remote_addr(net);
5921 }
5922 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5923 /*sa_ignore FREED_MEMORY*/
5924 sctp_remove_laddr(laddr);
5925 }
5926
5927 /* pending asconf (address) parameters */
5928 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5929 /*sa_ignore FREED_MEMORY*/
5930 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5931 SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5932 }
5933 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5934 /*sa_ignore FREED_MEMORY*/
5935 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5936 if (aack->data != NULL) {
5937 sctp_m_freem(aack->data);
5938 }
5939 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5940 }
5941 /* clean up auth stuff */
5942 if (asoc->local_hmacs)
5943 sctp_free_hmaclist(asoc->local_hmacs);
5944 if (asoc->peer_hmacs)
5945 sctp_free_hmaclist(asoc->peer_hmacs);
5946
5947 if (asoc->local_auth_chunks)
5948 sctp_free_chunklist(asoc->local_auth_chunks);
5949 if (asoc->peer_auth_chunks)
5950 sctp_free_chunklist(asoc->peer_auth_chunks);
5951
5952 sctp_free_authinfo(&asoc->authinfo);
5953
5954 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5955 LIST_REMOVE(shared_key, next);
5956 sctp_free_sharedkey(shared_key);
5957 /*sa_ignore FREED_MEMORY*/
5958 }
5959
5960 /* Insert new items here :> */
5961
5962 /* Get rid of LOCK */
t00fcxen2c2ff922013-11-03 14:03:35 +00005963 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00005964 SCTP_TCB_LOCK_DESTROY(stcb);
5965 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5966 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5967 SCTP_INP_INFO_WUNLOCK();
5968 SCTP_INP_RLOCK(inp);
5969 }
5970#if defined(__APPLE__) /* TEMP CODE */
5971 stcb->freed_from_where = from_location;
5972#endif
5973#ifdef SCTP_TRACK_FREED_ASOCS
5974 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5975 /* now clean up the tasoc itself */
5976 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5977 SCTP_DECR_ASOC_COUNT();
5978 } else {
5979 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5980 }
5981#else
5982 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5983 SCTP_DECR_ASOC_COUNT();
5984#endif
5985 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5986 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5987 /* If its NOT the inp_free calling us AND
5988 * sctp_close as been called, we
5989 * call back...
5990 */
5991 SCTP_INP_RUNLOCK(inp);
5992 /* This will start the kill timer (if we are
5993 * the last one) since we hold an increment yet. But
5994 * this is the only safe way to do this
5995 * since otherwise if the socket closes
5996 * at the same time we are here we might
5997 * collide in the cleanup.
5998 */
5999 sctp_inpcb_free(inp,
6000 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
6001 SCTP_CALLED_DIRECTLY_NOCMPSET);
6002 SCTP_INP_DECR_REF(inp);
tuexendd729232011-11-01 23:04:43 +00006003 } else {
6004 /* The socket is still open. */
6005 SCTP_INP_DECR_REF(inp);
Michael Tuexenc6c994c2020-05-18 21:50:12 +02006006 SCTP_INP_RUNLOCK(inp);
tuexendd729232011-11-01 23:04:43 +00006007 }
6008 }
tuexendd729232011-11-01 23:04:43 +00006009 /* destroyed the asoc */
6010#ifdef SCTP_LOG_CLOSING
6011 sctp_log_closing(inp, NULL, 11);
6012#endif
6013 return (1);
6014}
6015
6016
6017
6018/*
6019 * determine if a destination is "reachable" based upon the addresses bound
6020 * to the current endpoint (e.g. only v4 or v6 currently bound)
6021 */
6022/*
6023 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
6024 * assoc level v4/v6 flags, as the assoc *may* not have the same address
6025 * types bound as its endpoint
6026 */
6027int
6028sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
6029{
6030 struct sctp_inpcb *inp;
6031 int answer;
6032
6033 /*
6034 * No locks here, the TCB, in all cases is already locked and an
6035 * assoc is up. There is either a INP lock by the caller applied (in
6036 * asconf case when deleting an address) or NOT in the HB case,
6037 * however if HB then the INP increment is up and the INP will not
6038 * be removed (on top of the fact that we have a TCB lock). So we
6039 * only want to read the sctp_flags, which is either bound-all or
6040 * not.. no protection needed since once an assoc is up you can't be
6041 * changing your binding.
6042 */
6043 inp = stcb->sctp_ep;
6044 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6045 /* if bound all, destination is not restricted */
6046 /*
6047 * RRS: Question during lock work: Is this correct? If you
6048 * are bound-all you still might need to obey the V4--V6
6049 * flags??? IMO this bound-all stuff needs to be removed!
6050 */
6051 return (1);
6052 }
6053 /* NOTE: all "scope" checks are done when local addresses are added */
6054 switch (destaddr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00006055#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00006056 case AF_INET6:
6057#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6058 answer = inp->inp_vflag & INP_IPV6;
6059#else
6060 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
6061#endif
6062 break;
tuexen310f1bc2012-07-15 11:20:56 +00006063#endif
6064#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006065 case AF_INET:
6066#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6067 answer = inp->inp_vflag & INP_IPV4;
6068#else
6069 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
6070#endif
tuexen46570542012-07-14 20:37:24 +00006071 break;
tuexen310f1bc2012-07-15 11:20:56 +00006072#endif
tuexen5d9b9742012-07-11 21:52:01 +00006073#if defined(__Userspace__)
6074 case AF_CONN:
6075 answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
tuexendd729232011-11-01 23:04:43 +00006076 break;
tuexen5d9b9742012-07-11 21:52:01 +00006077#endif
tuexendd729232011-11-01 23:04:43 +00006078 default:
6079 /* invalid family, so it's unreachable */
6080 answer = 0;
6081 break;
6082 }
6083 return (answer);
6084}
6085
6086/*
6087 * update the inp_vflags on an endpoint
6088 */
6089static void
6090sctp_update_ep_vflag(struct sctp_inpcb *inp)
6091{
6092 struct sctp_laddr *laddr;
6093
6094 /* first clear the flag */
6095#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6096 inp->inp_vflag = 0;
6097#else
6098 inp->ip_inp.inp.inp_vflag = 0;
6099#endif
6100 /* set the flag based on addresses on the ep list */
6101 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6102 if (laddr->ifa == NULL) {
6103 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
Saúl Ibarra Corretgé8b04b412015-09-26 09:53:06 +02006104 __func__);
tuexendd729232011-11-01 23:04:43 +00006105 continue;
6106 }
6107
6108 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
6109 continue;
6110 }
6111 switch (laddr->ifa->address.sa.sa_family) {
6112#ifdef INET6
6113 case AF_INET6:
6114#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6115 inp->inp_vflag |= INP_IPV6;
6116#else
6117 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6118#endif
6119 break;
6120#endif
6121#ifdef INET
6122 case AF_INET:
6123#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6124 inp->inp_vflag |= INP_IPV4;
6125#else
6126 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6127#endif
6128 break;
6129#endif
tuexen5d9b9742012-07-11 21:52:01 +00006130#if defined(__Userspace__)
6131 case AF_CONN:
6132 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6133 break;
6134#endif
tuexendd729232011-11-01 23:04:43 +00006135 default:
6136 break;
6137 }
6138 }
6139}
6140
6141/*
6142 * Add the address to the endpoint local address list There is nothing to be
6143 * done if we are bound to all addresses
6144 */
6145void
6146sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
6147{
6148 struct sctp_laddr *laddr;
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006149 struct sctp_tcb *stcb;
tuexen63fc0bb2011-12-27 12:24:52 +00006150 int fnd, error = 0;
tuexendd729232011-11-01 23:04:43 +00006151
6152 fnd = 0;
6153
6154 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6155 /* You are already bound to all. You have it already */
6156 return;
6157 }
6158#ifdef INET6
6159 if (ifa->address.sa.sa_family == AF_INET6) {
6160 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6161 /* Can't bind a non-useable addr. */
6162 return;
6163 }
6164 }
6165#endif
6166 /* first, is it already present? */
6167 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6168 if (laddr->ifa == ifa) {
6169 fnd = 1;
6170 break;
6171 }
6172 }
6173
6174 if (fnd == 0) {
6175 /* Not in the ep list */
6176 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
6177 if (error != 0)
6178 return;
6179 inp->laddr_count++;
6180 /* update inp_vflag flags */
6181 switch (ifa->address.sa.sa_family) {
6182#ifdef INET6
6183 case AF_INET6:
6184#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6185 inp->inp_vflag |= INP_IPV6;
6186#else
6187 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6188#endif
6189 break;
6190#endif
tuexen310f1bc2012-07-15 11:20:56 +00006191#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006192 case AF_INET:
6193#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6194 inp->inp_vflag |= INP_IPV4;
6195#else
6196 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6197#endif
6198 break;
6199#endif
tuexen5d9b9742012-07-11 21:52:01 +00006200#if defined(__Userspace__)
6201 case AF_CONN:
6202 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6203 break;
6204#endif
tuexendd729232011-11-01 23:04:43 +00006205 default:
6206 break;
6207 }
Michael Tuexend75dc0c2016-03-28 21:38:43 +02006208 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6209 sctp_add_local_addr_restricted(stcb, ifa);
6210 }
tuexendd729232011-11-01 23:04:43 +00006211 }
6212 return;
6213}
6214
6215
6216/*
6217 * select a new (hopefully reachable) destination net (should only be used
6218 * when we deleted an ep addr that is the only usable source address to reach
6219 * the destination net)
6220 */
6221static void
6222sctp_select_primary_destination(struct sctp_tcb *stcb)
6223{
6224 struct sctp_nets *net;
6225
6226 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6227 /* for now, we'll just pick the first reachable one we find */
6228 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6229 continue;
6230 if (sctp_destination_is_reachable(stcb,
6231 (struct sockaddr *)&net->ro._l_addr)) {
6232 /* found a reachable destination */
6233 stcb->asoc.primary_destination = net;
6234 }
6235 }
6236 /* I can't there from here! ...we're gonna die shortly... */
6237}
6238
6239
6240/*
Michael Tuexen24e6c732016-02-19 12:28:21 +01006241 * Delete the address from the endpoint local address list. There is nothing
tuexendd729232011-11-01 23:04:43 +00006242 * to be done if we are bound to all addresses
6243 */
6244void
6245sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6246{
6247 struct sctp_laddr *laddr;
6248 int fnd;
6249
6250 fnd = 0;
6251 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6252 /* You are already bound to all. You have it already */
6253 return;
6254 }
6255 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6256 if (laddr->ifa == ifa) {
6257 fnd = 1;
6258 break;
6259 }
6260 }
6261 if (fnd && (inp->laddr_count < 2)) {
6262 /* can't delete unless there are at LEAST 2 addresses */
6263 return;
6264 }
6265 if (fnd) {
6266 /*
6267 * clean up any use of this address go through our
6268 * associations and clear any last_used_address that match
6269 * this one for each assoc, see if a new primary_destination
6270 * is needed
6271 */
6272 struct sctp_tcb *stcb;
6273
6274 /* clean up "next_addr_touse" */
6275 if (inp->next_addr_touse == laddr)
6276 /* delete this address */
6277 inp->next_addr_touse = NULL;
6278
6279 /* clean up "last_used_address" */
6280 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6281 struct sctp_nets *net;
Michael Tuexen24e6c732016-02-19 12:28:21 +01006282
tuexendd729232011-11-01 23:04:43 +00006283 SCTP_TCB_LOCK(stcb);
6284 if (stcb->asoc.last_used_address == laddr)
6285 /* delete this address */
6286 stcb->asoc.last_used_address = NULL;
6287 /* Now spin through all the nets and purge any ref to laddr */
6288 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
Michael Tuexen24e6c732016-02-19 12:28:21 +01006289 if (net->ro._s_addr == laddr->ifa) {
tuexendd729232011-11-01 23:04:43 +00006290 /* Yep, purge src address selected */
Michael Tuexena0515422020-05-18 00:47:04 +02006291#if defined(__FreeBSD__)
6292 RO_NHFREE(&net->ro);
6293#else
tuexendd729232011-11-01 23:04:43 +00006294 sctp_rtentry_t *rt;
6295
6296 /* delete this address if cached */
6297 rt = net->ro.ro_rt;
6298 if (rt != NULL) {
6299 RTFREE(rt);
6300 net->ro.ro_rt = NULL;
6301 }
Michael Tuexena0515422020-05-18 00:47:04 +02006302#endif
tuexendd729232011-11-01 23:04:43 +00006303 sctp_free_ifa(net->ro._s_addr);
6304 net->ro._s_addr = NULL;
6305 net->src_addr_selected = 0;
6306 }
6307 }
6308 SCTP_TCB_UNLOCK(stcb);
6309 } /* for each tcb */
6310 /* remove it from the ep list */
6311 sctp_remove_laddr(laddr);
6312 inp->laddr_count--;
6313 /* update inp_vflag flags */
6314 sctp_update_ep_vflag(inp);
6315 }
6316 return;
6317}
6318
6319/*
6320 * Add the address to the TCB local address restricted list.
6321 * This is a "pending" address list (eg. addresses waiting for an
6322 * ASCONF-ACK response) and cannot be used as a valid source address.
6323 */
6324void
6325sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6326{
tuexendd729232011-11-01 23:04:43 +00006327 struct sctp_laddr *laddr;
6328 struct sctpladdr *list;
6329
6330 /*
6331 * Assumes TCB is locked.. and possibly the INP. May need to
6332 * confirm/fix that if we need it and is not the case.
6333 */
6334 list = &stcb->asoc.sctp_restricted_addrs;
6335
tuexendd729232011-11-01 23:04:43 +00006336#ifdef INET6
6337 if (ifa->address.sa.sa_family == AF_INET6) {
6338 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6339 /* Can't bind a non-existent addr. */
6340 return;
6341 }
6342 }
6343#endif
6344 /* does the address already exist? */
6345 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6346 if (laddr->ifa == ifa) {
6347 return;
6348 }
6349 }
6350
6351 /* add to the list */
6352 (void)sctp_insert_laddr(list, ifa, 0);
6353 return;
6354}
6355
6356/*
tuexendd729232011-11-01 23:04:43 +00006357 * Remove a local address from the TCB local address restricted list
6358 */
6359void
6360sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6361{
6362 struct sctp_inpcb *inp;
6363 struct sctp_laddr *laddr;
6364
6365 /*
6366 * This is called by asconf work. It is assumed that a) The TCB is
6367 * locked and b) The INP is locked. This is true in as much as I can
6368 * trace through the entry asconf code where I did these locks.
6369 * Again, the ASCONF code is a bit different in that it does lock
6370 * the INP during its work often times. This must be since we don't
6371 * want other proc's looking up things while what they are looking
6372 * up is changing :-D
6373 */
6374
6375 inp = stcb->sctp_ep;
6376 /* if subset bound and don't allow ASCONF's, can't delete last */
6377 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6378 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6379 if (stcb->sctp_ep->laddr_count < 2) {
6380 /* can't delete last address */
6381 return;
6382 }
6383 }
6384 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6385 /* remove the address if it exists */
6386 if (laddr->ifa == NULL)
6387 continue;
6388 if (laddr->ifa == ifa) {
6389 sctp_remove_laddr(laddr);
6390 return;
6391 }
6392 }
6393
6394 /* address not found! */
6395 return;
6396}
6397
6398#if defined(__FreeBSD__)
6399/*
6400 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
6401 */
6402/* sysctl */
6403static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6404static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
6405#endif /* FreeBSD || APPLE */
6406
6407
6408
6409#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexen63fc0bb2011-12-27 12:24:52 +00006410struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6411int *sctp_cpuarry = NULL;
tuexendd729232011-11-01 23:04:43 +00006412void
6413sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6414{
6415 /* Queue a packet to a processor for the specified core */
6416 struct sctp_mcore_queue *qent;
6417 struct sctp_mcore_ctrl *wkq;
tuexen63fc0bb2011-12-27 12:24:52 +00006418 int need_wake = 0;
tuexendd729232011-11-01 23:04:43 +00006419 if (sctp_mcore_workers == NULL) {
6420 /* Something went way bad during setup */
6421 sctp_input_with_port(m, off, 0);
6422 return;
6423 }
6424 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6425 (sizeof(struct sctp_mcore_queue)),
6426 SCTP_M_MCORE);
6427 if (qent == NULL) {
6428 /* This is trouble */
6429 sctp_input_with_port(m, off, 0);
6430 return;
6431 }
6432#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6433 qent->vn = curvnet;
6434#endif
6435 qent->m = m;
6436 qent->off = off;
6437 qent->v6 = 0;
6438 wkq = &sctp_mcore_workers[cpu_to_use];
6439 SCTP_MCORE_QLOCK(wkq);
6440
6441 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6442 if (wkq->running == 0) {
6443 need_wake = 1;
6444 }
6445 SCTP_MCORE_QUNLOCK(wkq);
tuexen63fc0bb2011-12-27 12:24:52 +00006446 if (need_wake) {
tuexendd729232011-11-01 23:04:43 +00006447 wakeup(&wkq->running);
6448 }
6449}
6450
6451static void
6452sctp_mcore_thread(void *arg)
6453{
6454
6455 struct sctp_mcore_ctrl *wkq;
6456 struct sctp_mcore_queue *qent;
6457
6458 wkq = (struct sctp_mcore_ctrl *)arg;
6459 struct mbuf *m;
6460 int off, v6;
6461
6462 /* Wait for first tickle */
6463 SCTP_MCORE_LOCK(wkq);
6464 wkq->running = 0;
6465 msleep(&wkq->running,
6466 &wkq->core_mtx,
6467 0, "wait for pkt", 0);
6468 SCTP_MCORE_UNLOCK(wkq);
6469
6470 /* Bind to our cpu */
6471 thread_lock(curthread);
6472 sched_bind(curthread, wkq->cpuid);
6473 thread_unlock(curthread);
6474
6475 /* Now lets start working */
6476 SCTP_MCORE_LOCK(wkq);
6477 /* Now grab lock and go */
tuexen9784e9a2011-12-18 13:04:23 +00006478 for (;;) {
tuexendd729232011-11-01 23:04:43 +00006479 SCTP_MCORE_QLOCK(wkq);
6480 skip_sleep:
6481 wkq->running = 1;
6482 qent = TAILQ_FIRST(&wkq->que);
6483 if (qent) {
6484 TAILQ_REMOVE(&wkq->que, qent, next);
6485 SCTP_MCORE_QUNLOCK(wkq);
6486#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6487 CURVNET_SET(qent->vn);
6488#endif
6489 m = qent->m;
6490 off = qent->off;
6491 v6 = qent->v6;
6492 SCTP_FREE(qent, SCTP_M_MCORE);
6493 if (v6 == 0) {
6494 sctp_input_with_port(m, off, 0);
6495 } else {
tuexencb5fe8d2012-05-04 09:50:27 +00006496 SCTP_PRINTF("V6 not yet supported\n");
tuexendd729232011-11-01 23:04:43 +00006497 sctp_m_freem(m);
6498 }
6499#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6500 CURVNET_RESTORE();
6501#endif
6502 SCTP_MCORE_QLOCK(wkq);
6503 }
6504 wkq->running = 0;
6505 if (!TAILQ_EMPTY(&wkq->que)) {
6506 goto skip_sleep;
6507 }
6508 SCTP_MCORE_QUNLOCK(wkq);
6509 msleep(&wkq->running,
6510 &wkq->core_mtx,
6511 0, "wait for pkt", 0);
tuexen63fc0bb2011-12-27 12:24:52 +00006512 }
tuexendd729232011-11-01 23:04:43 +00006513}
6514
6515static void
6516sctp_startup_mcore_threads(void)
6517{
6518 int i, cpu;
6519
6520 if (mp_ncpus == 1)
6521 return;
6522
6523 if (sctp_mcore_workers != NULL) {
6524 /* Already been here in some previous
6525 * vnet?
6526 */
6527 return;
6528 }
6529 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6530 ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6531 SCTP_M_MCORE);
6532 if (sctp_mcore_workers == NULL) {
6533 /* TSNH I hope */
6534 return;
6535 }
tuexen09631162012-04-18 11:23:02 +00006536 memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
tuexendd729232011-11-01 23:04:43 +00006537 sizeof(struct sctp_mcore_ctrl)));
6538 /* Init the structures */
tuexen63fc0bb2011-12-27 12:24:52 +00006539 for (i = 0; i<=mp_maxid; i++) {
tuexendd729232011-11-01 23:04:43 +00006540 TAILQ_INIT(&sctp_mcore_workers[i].que);
6541 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6542 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6543 sctp_mcore_workers[i].cpuid = i;
6544 }
6545 if (sctp_cpuarry == NULL) {
6546 SCTP_MALLOC(sctp_cpuarry, int *,
6547 (mp_ncpus * sizeof(int)),
6548 SCTP_M_MCORE);
tuexen63fc0bb2011-12-27 12:24:52 +00006549 i = 0;
tuexendd729232011-11-01 23:04:43 +00006550 CPU_FOREACH(cpu) {
6551 sctp_cpuarry[i] = cpu;
6552 i++;
6553 }
6554 }
6555
6556 /* Now start them all */
6557 CPU_FOREACH(cpu) {
6558#if __FreeBSD_version <= 701000
6559 (void)kthread_create(sctp_mcore_thread,
6560 (void *)&sctp_mcore_workers[cpu],
6561 &sctp_mcore_workers[cpu].thread_proc,
6562 RFPROC,
6563 SCTP_KTHREAD_PAGES,
6564 SCTP_MCORE_NAME);
6565
6566#else
6567 (void)kproc_create(sctp_mcore_thread,
6568 (void *)&sctp_mcore_workers[cpu],
6569 &sctp_mcore_workers[cpu].thread_proc,
6570 RFPROC,
6571 SCTP_KTHREAD_PAGES,
6572 SCTP_MCORE_NAME);
6573#endif
tuexen09631162012-04-18 11:23:02 +00006574
tuexendd729232011-11-01 23:04:43 +00006575 }
6576}
6577#endif
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01006578#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000
tuexendd729232011-11-01 23:04:43 +00006579static struct mbuf *
6580sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6581{
6582 struct ip *ip;
6583 struct sctphdr *sh;
6584 int offset;
6585 uint32_t flowid, tag;
tuexen76eb2092012-04-01 16:35:28 +00006586
tuexendd729232011-11-01 23:04:43 +00006587 /*
6588 * No flow id built by lower layers fix it so we
6589 * create one.
6590 */
6591 ip = mtod(m, struct ip *);
tuexen664967a2012-06-28 16:24:36 +00006592 offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
tuexendd729232011-11-01 23:04:43 +00006593 if (SCTP_BUF_LEN(m) < offset) {
tuexen664967a2012-06-28 16:24:36 +00006594 if ((m = m_pullup(m, offset)) == NULL) {
tuexendd729232011-11-01 23:04:43 +00006595 SCTP_STAT_INCR(sctps_hdrops);
6596 return (NULL);
6597 }
6598 ip = mtod(m, struct ip *);
6599 }
6600 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
tuexendd729232011-11-01 23:04:43 +00006601 tag = htonl(sh->v_tag);
6602 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6603 m->m_pkthdr.flowid = flowid;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02006604/* FIX ME */
tuexendd729232011-11-01 23:04:43 +00006605 m->m_flags |= M_FLOWID;
6606 return (m);
6607}
6608#endif
6609
6610void
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006611#if defined(__Userspace__)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006612sctp_pcb_init(int start_threads)
Michael Tuexen843cc3b2019-08-31 15:25:03 +02006613#else
6614sctp_pcb_init(void)
6615#endif
tuexendd729232011-11-01 23:04:43 +00006616{
6617 /*
6618 * SCTP initialization for the PCB structures should be called by
Michael Tuexen34488e72016-05-03 22:11:59 +02006619 * the sctp_init() function.
tuexendd729232011-11-01 23:04:43 +00006620 */
6621 int i;
6622 struct timeval tv;
tuexen6019b072011-12-15 18:38:03 +00006623
tuexendd729232011-11-01 23:04:43 +00006624 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6625 /* error I was called twice */
6626 return;
6627 }
6628 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6629
Michael Tuexencb2878b2015-11-13 12:59:40 +01006630#if defined(SCTP_PROCESS_LEVEL_LOCKS)
6631#if !defined(__Userspace_os_Windows)
6632 pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr));
6633#ifdef INVARIANTS
6634 pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK);
6635#endif
6636#endif
6637#endif
tuexendd729232011-11-01 23:04:43 +00006638#if defined(SCTP_LOCAL_TRACE_BUF)
6639#if defined(__Windows__)
6640 if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02006641 memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006642 }
6643#else
Michael Tuexend0853302017-07-19 17:01:31 +02006644 memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
tuexendd729232011-11-01 23:04:43 +00006645#endif
6646#endif
6647#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6648 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6649 ((mp_maxid+1) * sizeof(struct sctpstat)),
6650 SCTP_M_MCORE);
6651#endif
6652 (void)SCTP_GETTIME_TIMEVAL(&tv);
6653#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
Michael Tuexend0853302017-07-19 17:01:31 +02006654 memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1));
tuexendd729232011-11-01 23:04:43 +00006655 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6656 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6657#else
Michael Tuexend0853302017-07-19 17:01:31 +02006658 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
tuexendd729232011-11-01 23:04:43 +00006659 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6660 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6661#endif
6662 /* init the empty list of (All) Endpoints */
6663 LIST_INIT(&SCTP_BASE_INFO(listhead));
6664#if defined(__APPLE__)
6665 LIST_INIT(&SCTP_BASE_INFO(inplisthead));
t00fcxenbc766ab2014-01-03 19:29:31 +00006666#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6667 SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead);
6668 SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init();
6669 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6670 SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6671 SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init();
6672 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6673#else
6674 SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead);
6675 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init();
6676 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6677 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6678 SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init();
6679 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6680#endif
6681#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6682 SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc;
6683 in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo));
6684#endif
tuexendd729232011-11-01 23:04:43 +00006685#endif
6686
6687
6688 /* init the hash table of endpoints */
6689#if defined(__FreeBSD__)
6690#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
6691 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6692 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6693 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
6694#else
6695 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
6696 SCTP_BASE_SYSCTL(sctp_hashtblsize));
6697 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
6698 SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6699 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
6700 SCTP_BASE_SYSCTL(sctp_chunkscale));
6701#endif
6702#endif
6703 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6704 &SCTP_BASE_INFO(hashasocmark));
6705 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6706 &SCTP_BASE_INFO(hashmark));
6707 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6708 &SCTP_BASE_INFO(hashtcpmark));
6709 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
6710
6711
6712 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6713 &SCTP_BASE_INFO(hashvrfmark));
6714
6715 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6716 &SCTP_BASE_INFO(vrf_ifn_hashmark));
6717 /* init the zones */
6718 /*
6719 * FIX ME: Should check for NULL returns, but if it does fail we are
6720 * doomed to panic anyways... add later maybe.
6721 */
6722 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6723 sizeof(struct sctp_inpcb), maxsockets);
6724
6725 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6726 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6727
6728 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6729 sizeof(struct sctp_laddr),
6730 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6731
6732 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6733 sizeof(struct sctp_nets),
6734 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6735
6736 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6737 sizeof(struct sctp_tmit_chunk),
6738 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6739
6740 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6741 sizeof(struct sctp_queued_to_read),
6742 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6743
6744 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6745 sizeof(struct sctp_stream_queue_pending),
6746 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6747
6748 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6749 sizeof(struct sctp_asconf),
6750 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6751
6752 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6753 sizeof(struct sctp_asconf_ack),
6754 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6755
tuexen9784e9a2011-12-18 13:04:23 +00006756
tuexendd729232011-11-01 23:04:43 +00006757 /* Master Lock INIT for info structure */
tuexendd729232011-11-01 23:04:43 +00006758 SCTP_INP_INFO_LOCK_INIT();
6759 SCTP_STATLOG_INIT_LOCK();
6760
6761 SCTP_IPI_COUNT_INIT();
6762 SCTP_IPI_ADDR_INIT();
6763#ifdef SCTP_PACKET_LOGGING
6764 SCTP_IP_PKTLOG_INIT();
6765#endif
6766 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6767
6768 SCTP_WQ_ADDR_INIT();
6769 /* not sure if we need all the counts */
6770 SCTP_BASE_INFO(ipi_count_ep) = 0;
6771 /* assoc/tcb zone info */
6772 SCTP_BASE_INFO(ipi_count_asoc) = 0;
6773 /* local addrlist zone info */
6774 SCTP_BASE_INFO(ipi_count_laddr) = 0;
6775 /* remote addrlist zone info */
6776 SCTP_BASE_INFO(ipi_count_raddr) = 0;
6777 /* chunk info */
6778 SCTP_BASE_INFO(ipi_count_chunk) = 0;
6779
6780 /* socket queue zone info */
6781 SCTP_BASE_INFO(ipi_count_readq) = 0;
6782
6783 /* stream out queue cont */
6784 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6785
6786 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6787 SCTP_BASE_INFO(ipi_free_chunks) = 0;
6788
6789 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6790
6791 /* Init the TIMEWAIT list */
6792 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6793 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6794 }
tuexendd729232011-11-01 23:04:43 +00006795#if defined(SCTP_PROCESS_LEVEL_LOCKS)
tuexen5154a002011-12-23 18:47:55 +00006796#if defined(__Userspace_os_Windows)
tuexen6019b072011-12-15 18:38:03 +00006797 InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6798#else
tuexendd729232011-11-01 23:04:43 +00006799 (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6800#endif
tuexen6019b072011-12-15 18:38:03 +00006801#endif
tuexendd729232011-11-01 23:04:43 +00006802 sctp_startup_iterator();
6803
6804#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6805 sctp_startup_mcore_threads();
6806#endif
6807
6808#ifndef __Panda__
6809 /*
6810 * INIT the default VRF which for BSD is the only one, other O/S's
6811 * may have more. But initially they must start with one and then
6812 * add the VRF's as addresses are added.
6813 */
6814 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
6815#endif
Michael Tuexenbe8e0eb2019-03-24 00:11:02 +01006816#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000
tuexendd729232011-11-01 23:04:43 +00006817 if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
tuexencb5fe8d2012-05-04 09:50:27 +00006818 SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
tuexendd729232011-11-01 23:04:43 +00006819 }
6820#endif
6821#if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6822 /* allocate the lock for the callout/timer queue */
6823 SCTP_TIMERQ_LOCK_INIT();
6824 TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6825#endif
6826#if defined(__Userspace__)
Michael Tuexen2f6478e2017-03-02 09:57:46 +01006827 mbuf_initialize(NULL);
tuexendd729232011-11-01 23:04:43 +00006828 atomic_init();
tuexenc4178972012-07-15 20:40:54 +00006829#if defined(INET) || defined(INET6)
José Luis Millánf6aa4b02019-08-12 22:54:25 +02006830 if (start_threads)
6831 recv_thread_init();
tuexendd729232011-11-01 23:04:43 +00006832#endif
tuexenc4178972012-07-15 20:40:54 +00006833#endif
tuexendd729232011-11-01 23:04:43 +00006834}
6835
6836/*
6837 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6838 */
6839void
6840sctp_pcb_finish(void)
6841{
6842 struct sctp_vrflist *vrf_bucket;
6843 struct sctp_vrf *vrf, *nvrf;
6844 struct sctp_ifn *ifn, *nifn;
6845 struct sctp_ifa *ifa, *nifa;
6846 struct sctpvtaghead *chain;
6847 struct sctp_tagblock *twait_block, *prev_twait_block;
6848 struct sctp_laddr *wi, *nwi;
6849 int i;
t00fcxenc7932282013-11-16 14:57:41 +00006850 struct sctp_iterator *it, *nit;
Michael Tuexen20785d42015-11-13 12:48:35 +01006851
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006852 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6853 SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
6854 return;
6855 }
6856 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
t00fcxen9232a652013-11-30 09:34:24 +00006857#if !defined(__FreeBSD__)
tuexendd729232011-11-01 23:04:43 +00006858 /* Notify the iterator to exit. */
6859 SCTP_IPI_ITERATOR_WQ_LOCK();
6860 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6861 sctp_wakeup_iterator();
6862 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6863#endif
6864#if defined(__APPLE__)
t00fcxenbc766ab2014-01-03 19:29:31 +00006865#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6866 in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo));
6867#endif
tuexendd729232011-11-01 23:04:43 +00006868 SCTP_IPI_ITERATOR_WQ_LOCK();
6869 do {
6870 msleep(&sctp_it_ctl.iterator_flags,
6871 sctp_it_ctl.ipi_iterator_wq_mtx,
6872 0, "waiting_for_work", 0);
6873 } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6874 thread_deallocate(sctp_it_ctl.thread_proc);
6875 SCTP_IPI_ITERATOR_WQ_UNLOCK();
tuexendd729232011-11-01 23:04:43 +00006876#endif
6877#if defined(__Windows__)
6878 if (sctp_it_ctl.iterator_thread_obj != NULL) {
6879 NTSTATUS status = STATUS_SUCCESS;
6880
6881 KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6882 status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6883 Executive,
6884 KernelMode,
6885 FALSE,
6886 NULL);
6887 ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6888 }
6889#endif
t00fcxen92a5ac42013-11-16 12:58:54 +00006890#if defined(__Userspace__)
Michael Tuexen3212d542020-03-16 12:30:04 +01006891 if (SCTP_BASE_VAR(iterator_thread_started)) {
t00fcxen92a5ac42013-11-16 12:58:54 +00006892#if defined(__Userspace_os_Windows)
6893 WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE);
6894 CloseHandle(sctp_it_ctl.thread_proc);
6895 sctp_it_ctl.thread_proc = NULL;
6896#else
6897 pthread_join(sctp_it_ctl.thread_proc, NULL);
6898 sctp_it_ctl.thread_proc = 0;
6899#endif
6900 }
6901#endif
6902#if defined(SCTP_PROCESS_LEVEL_LOCKS)
6903#if defined(__Userspace_os_Windows)
6904 DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup);
6905#else
6906 pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup);
Michael Tuexen89e15a72015-11-13 12:42:55 +01006907 pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr));
t00fcxen92a5ac42013-11-16 12:58:54 +00006908#endif
6909#endif
t00fcxen9232a652013-11-30 09:34:24 +00006910 /* In FreeBSD the iterator thread never exits
6911 * but we do clean up.
6912 * The only way FreeBSD reaches here is if we have VRF's
6913 * but we still add the ifdef to make it compile on old versions.
6914 */
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006915#if defined(__FreeBSD__)
6916retry:
6917#endif
t00fcxen9232a652013-11-30 09:34:24 +00006918 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006919#if defined(__FreeBSD__)
6920 /*
6921 * sctp_iterator_worker() might be working on an it entry without
6922 * holding the lock. We won't find it on the list either and
6923 * continue and free/destroy it. While holding the lock, spin, to
6924 * avoid the race condition as sctp_iterator_worker() will have to
6925 * wait to re-aquire the lock.
6926 */
6927 if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
6928 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6929 SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
6930 "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
6931 DELAY(10);
6932 goto retry;
6933 }
6934#endif
t00fcxen9232a652013-11-30 09:34:24 +00006935 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
t00fcxen433c6e12013-11-30 09:45:11 +00006936#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
t00fcxen9232a652013-11-30 09:34:24 +00006937 if (it->vn != curvnet) {
6938 continue;
6939 }
6940#endif
6941 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6942 if (it->function_atend != NULL) {
6943 (*it->function_atend) (it->pointer, it->val);
6944 }
6945 SCTP_FREE(it,SCTP_M_ITER);
6946 }
6947 SCTP_IPI_ITERATOR_WQ_UNLOCK();
t00fcxen433c6e12013-11-30 09:45:11 +00006948#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
t00fcxen9232a652013-11-30 09:34:24 +00006949 SCTP_ITERATOR_LOCK();
6950 if ((sctp_it_ctl.cur_it) &&
6951 (sctp_it_ctl.cur_it->vn == curvnet)) {
6952 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6953 }
6954 SCTP_ITERATOR_UNLOCK();
6955#endif
t00fcxenc7932282013-11-16 14:57:41 +00006956#if !defined(__FreeBSD__)
t00fcxen92a5ac42013-11-16 12:58:54 +00006957 SCTP_IPI_ITERATOR_WQ_DESTROY();
t00fcxen8dd95b82014-07-11 20:41:28 +00006958 SCTP_ITERATOR_LOCK_DESTROY();
t00fcxenc7932282013-11-16 14:57:41 +00006959#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01006960 SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
tuexendd729232011-11-01 23:04:43 +00006961 SCTP_WQ_ADDR_LOCK();
6962 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6963 LIST_REMOVE(wi, sctp_nxt_addr);
6964 SCTP_DECR_LADDR_COUNT();
t00fcxen9892b7f2014-09-06 20:06:39 +00006965 if (wi->action == SCTP_DEL_IP_ADDRESS) {
6966 SCTP_FREE(wi->ifa, SCTP_M_IFA);
6967 }
tuexendd729232011-11-01 23:04:43 +00006968 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6969 }
6970 SCTP_WQ_ADDR_UNLOCK();
tuexen09631162012-04-18 11:23:02 +00006971
tuexendd729232011-11-01 23:04:43 +00006972 /*
6973 * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6974 * is destroyed first).
6975 */
6976 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6977 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6978 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6979 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6980 /* free the ifa */
6981 LIST_REMOVE(ifa, next_bucket);
6982 LIST_REMOVE(ifa, next_ifa);
6983 SCTP_FREE(ifa, SCTP_M_IFA);
6984 }
6985 /* free the ifn */
6986 LIST_REMOVE(ifn, next_bucket);
6987 LIST_REMOVE(ifn, next_ifn);
6988 SCTP_FREE(ifn, SCTP_M_IFN);
6989 }
6990 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6991 /* free the vrf */
6992 LIST_REMOVE(vrf, next_vrf);
6993 SCTP_FREE(vrf, SCTP_M_VRF);
6994 }
6995 /* free the vrf hashes */
6996 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6997 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
tuexendd729232011-11-01 23:04:43 +00006998
6999 /* free the TIMEWAIT list elements malloc'd in the function
7000 * sctp_add_vtag_to_timewait()...
7001 */
7002 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
7003 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
7004 if (!LIST_EMPTY(chain)) {
7005 prev_twait_block = NULL;
7006 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
7007 if (prev_twait_block) {
7008 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
7009 }
7010 prev_twait_block = twait_block;
7011 }
7012 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
7013 }
7014 }
7015
7016 /* free the locks and mutexes */
7017#if defined(__APPLE__)
7018 SCTP_TIMERQ_LOCK_DESTROY();
7019#endif
7020#ifdef SCTP_PACKET_LOGGING
7021 SCTP_IP_PKTLOG_DESTROY();
7022#endif
7023 SCTP_IPI_ADDR_DESTROY();
7024#if defined(__APPLE__)
7025 SCTP_IPI_COUNT_DESTROY();
7026#endif
7027 SCTP_STATLOG_DESTROY();
tuexendd729232011-11-01 23:04:43 +00007028 SCTP_INP_INFO_LOCK_DESTROY();
tuexen09631162012-04-18 11:23:02 +00007029
tuexendd729232011-11-01 23:04:43 +00007030 SCTP_WQ_ADDR_DESTROY();
tuexen09631162012-04-18 11:23:02 +00007031
tuexendd729232011-11-01 23:04:43 +00007032#if defined(__APPLE__)
t00fcxenbc766ab2014-01-03 19:29:31 +00007033#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
7034 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
7035 lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp);
7036 lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr);
7037#else
7038 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
7039 lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp);
7040 lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
7041#endif
tuexendd729232011-11-01 23:04:43 +00007042#endif
7043#if defined(__Userspace__)
7044 SCTP_TIMERQ_LOCK_DESTROY();
7045 SCTP_ZONE_DESTROY(zone_mbuf);
7046 SCTP_ZONE_DESTROY(zone_clust);
7047 SCTP_ZONE_DESTROY(zone_ext_refcnt);
7048#endif
Michael Tuexen71a83fb2016-02-16 21:52:58 +01007049 /* Get rid of other stuff too. */
7050 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
7051 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
7052 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
7053 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
7054 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
7055 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
7056
tuexendd729232011-11-01 23:04:43 +00007057#if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__)
7058 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
7059 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
7060 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
7061 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
7062 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
7063 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
7064 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
7065 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
7066 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
7067#endif
tuexendd729232011-11-01 23:04:43 +00007068#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexen09631162012-04-18 11:23:02 +00007069 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
tuexendd729232011-11-01 23:04:43 +00007070#endif
7071}
7072
7073
7074int
7075sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
tuexen664967a2012-06-28 16:24:36 +00007076 int offset, int limit,
7077 struct sockaddr *src, struct sockaddr *dst,
Michael Tuexen923f7352016-05-01 23:55:35 +02007078 struct sockaddr *altsa, uint16_t port)
tuexendd729232011-11-01 23:04:43 +00007079{
7080 /*
7081 * grub through the INIT pulling addresses and loading them to the
7082 * nets structure in the asoc. The from address in the mbuf should
7083 * also be loaded (if it is not already). This routine can be called
7084 * with either INIT or INIT-ACK's as long as the m points to the IP
7085 * packet and the offset points to the beginning of the parameters.
7086 */
tuexen63fc0bb2011-12-27 12:24:52 +00007087 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00007088 struct sctp_nets *net, *nnet, *net_tmp;
Michael Tuexen473225f2017-06-23 23:05:43 +02007089 struct sctp_paramhdr *phdr, param_buf;
tuexendd729232011-11-01 23:04:43 +00007090 struct sctp_tcb *stcb_tmp;
7091 uint16_t ptype, plen;
7092 struct sockaddr *sa;
tuexendd729232011-11-01 23:04:43 +00007093 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
7094 struct sctp_auth_random *p_random = NULL;
7095 uint16_t random_len = 0;
7096 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
7097 struct sctp_auth_hmac_algo *hmacs = NULL;
7098 uint16_t hmacs_len = 0;
tuexen63fc0bb2011-12-27 12:24:52 +00007099 uint8_t saw_asconf = 0;
7100 uint8_t saw_asconf_ack = 0;
tuexendd729232011-11-01 23:04:43 +00007101 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
7102 struct sctp_auth_chunk_list *chunks = NULL;
7103 uint16_t num_chunks = 0;
7104 sctp_key_t *new_key;
7105 uint32_t keylen;
7106 int got_random = 0, got_hmacs = 0, got_chklist = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007107 uint8_t peer_supports_ecn;
7108 uint8_t peer_supports_prsctp;
7109 uint8_t peer_supports_auth;
7110 uint8_t peer_supports_asconf;
7111 uint8_t peer_supports_asconf_ack;
7112 uint8_t peer_supports_reconfig;
7113 uint8_t peer_supports_nrsack;
7114 uint8_t peer_supports_pktdrop;
Michael Tuexene5001952016-04-17 19:25:27 +02007115 uint8_t peer_supports_idata;
tuexendd729232011-11-01 23:04:43 +00007116#ifdef INET
7117 struct sockaddr_in sin;
7118#endif
7119#ifdef INET6
7120 struct sockaddr_in6 sin6;
7121#endif
7122
7123 /* First get the destination address setup too. */
7124#ifdef INET
7125 memset(&sin, 0, sizeof(sin));
7126 sin.sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00007127#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00007128 sin.sin_len = sizeof(sin);
7129#endif
7130 sin.sin_port = stcb->rport;
7131#endif
7132#ifdef INET6
7133 memset(&sin6, 0, sizeof(sin6));
7134 sin6.sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00007135#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00007136 sin6.sin6_len = sizeof(struct sockaddr_in6);
7137#endif
7138 sin6.sin6_port = stcb->rport;
7139#endif
tuexen664967a2012-06-28 16:24:36 +00007140 if (altsa) {
7141 sa = altsa;
7142 } else {
7143 sa = src;
tuexendd729232011-11-01 23:04:43 +00007144 }
Michael Tuexene5001952016-04-17 19:25:27 +02007145 peer_supports_idata = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007146 peer_supports_ecn = 0;
7147 peer_supports_prsctp = 0;
7148 peer_supports_auth = 0;
7149 peer_supports_asconf = 0;
t00fcxenaac0f452014-08-12 12:51:45 +00007150 peer_supports_reconfig = 0;
7151 peer_supports_nrsack = 0;
7152 peer_supports_pktdrop = 0;
tuexendd729232011-11-01 23:04:43 +00007153 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7154 /* mark all addresses that we have currently on the list */
7155 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
7156 }
7157 /* does the source address already exist? if so skip it */
tuexen63fc0bb2011-12-27 12:24:52 +00007158 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00007159 atomic_add_int(&stcb->asoc.refcnt, 1);
tuexen664967a2012-06-28 16:24:36 +00007160 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007161 atomic_add_int(&stcb->asoc.refcnt, -1);
7162
7163 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
7164 /* we must add the source address */
7165 /* no scope set here since we have a tcb already. */
7166 switch (sa->sa_family) {
7167#ifdef INET
7168 case AF_INET:
t00fcxend0ad16b2013-02-09 18:34:24 +00007169 if (stcb->asoc.scope.ipv4_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007170 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
tuexendd729232011-11-01 23:04:43 +00007171 return (-1);
7172 }
7173 }
7174 break;
7175#endif
7176#ifdef INET6
7177 case AF_INET6:
t00fcxend0ad16b2013-02-09 18:34:24 +00007178 if (stcb->asoc.scope.ipv6_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007179 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
tuexendd729232011-11-01 23:04:43 +00007180 return (-2);
7181 }
7182 }
7183 break;
7184#endif
tuexen430ca6c2012-07-10 23:36:36 +00007185#if defined(__Userspace__)
7186 case AF_CONN:
t00fcxend0ad16b2013-02-09 18:34:24 +00007187 if (stcb->asoc.scope.conn_addr_legal) {
Michael Tuexen923f7352016-05-01 23:55:35 +02007188 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007189 return (-2);
7190 }
tuexen430ca6c2012-07-10 23:36:36 +00007191 }
7192 break;
7193#endif
tuexendd729232011-11-01 23:04:43 +00007194 default:
7195 break;
7196 }
7197 } else {
7198 if (net_tmp != NULL && stcb_tmp == stcb) {
7199 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
7200 } else if (stcb_tmp != stcb) {
7201 /* It belongs to another association? */
7202 if (stcb_tmp)
7203 SCTP_TCB_UNLOCK(stcb_tmp);
7204 return (-3);
7205 }
7206 }
7207 if (stcb->asoc.state == 0) {
7208 /* the assoc was freed? */
7209 return (-4);
7210 }
tuexendd729232011-11-01 23:04:43 +00007211 /* now we must go through each of the params. */
Michael Tuexen473225f2017-06-23 23:05:43 +02007212 phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007213 while (phdr) {
7214 ptype = ntohs(phdr->param_type);
7215 plen = ntohs(phdr->param_length);
7216 /*
tuexencb5fe8d2012-05-04 09:50:27 +00007217 * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
tuexendd729232011-11-01 23:04:43 +00007218 * (int)plen);
7219 */
7220 if (offset + plen > limit) {
7221 break;
7222 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007223 if (plen < sizeof(struct sctp_paramhdr)) {
tuexendd729232011-11-01 23:04:43 +00007224 break;
7225 }
7226#ifdef INET
7227 if (ptype == SCTP_IPV4_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007228 if (stcb->asoc.scope.ipv4_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007229 struct sctp_ipv4addr_param *p4, p4_buf;
7230
7231 /* ok get the v4 address and check/add */
7232 phdr = sctp_get_next_param(m, offset,
7233 (struct sctp_paramhdr *)&p4_buf,
7234 sizeof(p4_buf));
7235 if (plen != sizeof(struct sctp_ipv4addr_param) ||
7236 phdr == NULL) {
7237 return (-5);
7238 }
7239 p4 = (struct sctp_ipv4addr_param *)phdr;
7240 sin.sin_addr.s_addr = p4->addr;
7241 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7242 /* Skip multi-cast addresses */
7243 goto next_param;
7244 }
7245 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
7246 (sin.sin_addr.s_addr == INADDR_ANY)) {
7247 goto next_param;
7248 }
7249 sa = (struct sockaddr *)&sin;
7250 inp = stcb->sctp_ep;
7251 atomic_add_int(&stcb->asoc.refcnt, 1);
7252 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007253 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007254 atomic_add_int(&stcb->asoc.refcnt, -1);
7255
7256 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7257 inp == NULL) {
7258 /* we must add the source address */
7259 /*
7260 * no scope set since we have a tcb
7261 * already
7262 */
7263
7264 /*
7265 * we must validate the state again
7266 * here
7267 */
7268 add_it_now:
7269 if (stcb->asoc.state == 0) {
7270 /* the assoc was freed? */
7271 return (-7);
7272 }
Michael Tuexen923f7352016-05-01 23:55:35 +02007273 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
tuexendd729232011-11-01 23:04:43 +00007274 return (-8);
7275 }
7276 } else if (stcb_tmp == stcb) {
7277 if (stcb->asoc.state == 0) {
7278 /* the assoc was freed? */
7279 return (-10);
7280 }
7281 if (net != NULL) {
7282 /* clear flag */
7283 net->dest_state &=
7284 ~SCTP_ADDR_NOT_IN_ASSOC;
7285 }
7286 } else {
7287 /*
7288 * strange, address is in another
7289 * assoc? straighten out locks.
7290 */
7291 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007292 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007293 struct mbuf *op_err;
7294 char msg[SCTP_DIAG_INFO_LEN];
7295
tuexendd729232011-11-01 23:04:43 +00007296 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007297 SCTP_SNPRINTF(msg, sizeof(msg),
7298 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007299 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7300 msg);
tuexendd729232011-11-01 23:04:43 +00007301 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tüxenac142282015-07-28 00:42:03 +02007302 stcb_tmp, op_err,
7303 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007304 goto add_it_now;
7305 }
7306 SCTP_TCB_UNLOCK(stcb_tmp);
7307 }
7308
7309 if (stcb->asoc.state == 0) {
7310 /* the assoc was freed? */
7311 return (-12);
7312 }
7313 return (-13);
7314 }
7315 }
7316 } else
7317#endif
7318#ifdef INET6
7319 if (ptype == SCTP_IPV6_ADDRESS) {
t00fcxend0ad16b2013-02-09 18:34:24 +00007320 if (stcb->asoc.scope.ipv6_addr_legal) {
tuexendd729232011-11-01 23:04:43 +00007321 /* ok get the v6 address and check/add */
7322 struct sctp_ipv6addr_param *p6, p6_buf;
7323
7324 phdr = sctp_get_next_param(m, offset,
7325 (struct sctp_paramhdr *)&p6_buf,
7326 sizeof(p6_buf));
7327 if (plen != sizeof(struct sctp_ipv6addr_param) ||
7328 phdr == NULL) {
7329 return (-14);
7330 }
7331 p6 = (struct sctp_ipv6addr_param *)phdr;
7332 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7333 sizeof(p6->addr));
7334 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7335 /* Skip multi-cast addresses */
7336 goto next_param;
7337 }
7338 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7339 /* Link local make no sense without scope */
7340 goto next_param;
7341 }
7342 sa = (struct sockaddr *)&sin6;
7343 inp = stcb->sctp_ep;
7344 atomic_add_int(&stcb->asoc.refcnt, 1);
7345 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007346 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007347 atomic_add_int(&stcb->asoc.refcnt, -1);
7348 if (stcb_tmp == NULL &&
7349 (inp == stcb->sctp_ep || inp == NULL)) {
7350 /*
7351 * we must validate the state again
7352 * here
7353 */
7354 add_it_now6:
7355 if (stcb->asoc.state == 0) {
7356 /* the assoc was freed? */
7357 return (-16);
7358 }
7359 /*
7360 * we must add the address, no scope
7361 * set
7362 */
Michael Tuexen923f7352016-05-01 23:55:35 +02007363 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
tuexendd729232011-11-01 23:04:43 +00007364 return (-17);
7365 }
7366 } else if (stcb_tmp == stcb) {
7367 /*
7368 * we must validate the state again
7369 * here
7370 */
7371 if (stcb->asoc.state == 0) {
7372 /* the assoc was freed? */
7373 return (-19);
7374 }
7375 if (net != NULL) {
7376 /* clear flag */
7377 net->dest_state &=
7378 ~SCTP_ADDR_NOT_IN_ASSOC;
7379 }
7380 } else {
7381 /*
7382 * strange, address is in another
7383 * assoc? straighten out locks.
7384 */
Michael Tüxenac142282015-07-28 00:42:03 +02007385 if (stcb_tmp) {
Michael Tuexen348a36c2018-08-13 16:24:47 +02007386 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
Michael Tüxenac142282015-07-28 00:42:03 +02007387 struct mbuf *op_err;
7388 char msg[SCTP_DIAG_INFO_LEN];
7389
tuexendd729232011-11-01 23:04:43 +00007390 /* in setup state we abort this guy */
Michael Tuexenedd369d2020-05-19 09:42:15 +02007391 SCTP_SNPRINTF(msg, sizeof(msg),
7392 "%s:%d at %s", __FILE__, __LINE__, __func__);
Michael Tüxenac142282015-07-28 00:42:03 +02007393 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7394 msg);
tuexendd729232011-11-01 23:04:43 +00007395 sctp_abort_an_association(stcb_tmp->sctp_ep,
Michael Tüxenac142282015-07-28 00:42:03 +02007396 stcb_tmp, op_err,
7397 SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007398 goto add_it_now6;
7399 }
Michael Tüxenac142282015-07-28 00:42:03 +02007400 SCTP_TCB_UNLOCK(stcb_tmp);
7401 }
tuexendd729232011-11-01 23:04:43 +00007402 if (stcb->asoc.state == 0) {
7403 /* the assoc was freed? */
7404 return (-21);
7405 }
7406 return (-22);
7407 }
7408 }
7409 } else
7410#endif
7411 if (ptype == SCTP_ECN_CAPABLE) {
t00fcxenaac0f452014-08-12 12:51:45 +00007412 peer_supports_ecn = 1;
tuexendd729232011-11-01 23:04:43 +00007413 } else if (ptype == SCTP_ULP_ADAPTATION) {
7414 if (stcb->asoc.state != SCTP_STATE_OPEN) {
7415 struct sctp_adaptation_layer_indication ai, *aip;
7416
7417 phdr = sctp_get_next_param(m, offset,
7418 (struct sctp_paramhdr *)&ai, sizeof(ai));
7419 aip = (struct sctp_adaptation_layer_indication *)phdr;
7420 if (aip) {
7421 stcb->asoc.peers_adaptation = ntohl(aip->indication);
7422 stcb->asoc.adaptation_needed = 1;
7423 }
7424 }
7425 } else if (ptype == SCTP_SET_PRIM_ADDR) {
7426 struct sctp_asconf_addr_param lstore, *fee;
7427 int lptype;
7428 struct sockaddr *lsa = NULL;
7429#ifdef INET
7430 struct sctp_asconf_addrv4_param *fii;
7431#endif
7432
t00fcxenaac0f452014-08-12 12:51:45 +00007433 if (stcb->asoc.asconf_supported == 0) {
7434 return (-100);
7435 }
tuexendd729232011-11-01 23:04:43 +00007436 if (plen > sizeof(lstore)) {
7437 return (-23);
7438 }
Michael Tuexen790a7a22019-12-20 17:02:02 +01007439 if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
7440 return (-101);
7441 }
tuexendd729232011-11-01 23:04:43 +00007442 phdr = sctp_get_next_param(m, offset,
7443 (struct sctp_paramhdr *)&lstore,
Michael Tuexen473225f2017-06-23 23:05:43 +02007444 plen);
tuexendd729232011-11-01 23:04:43 +00007445 if (phdr == NULL) {
7446 return (-24);
7447 }
7448 fee = (struct sctp_asconf_addr_param *)phdr;
7449 lptype = ntohs(fee->addrp.ph.param_type);
7450 switch (lptype) {
7451#ifdef INET
7452 case SCTP_IPV4_ADDRESS:
7453 if (plen !=
7454 sizeof(struct sctp_asconf_addrv4_param)) {
7455 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7456 (int)sizeof(struct sctp_asconf_addrv4_param),
7457 plen);
7458 } else {
7459 fii = (struct sctp_asconf_addrv4_param *)fee;
7460 sin.sin_addr.s_addr = fii->addrp.addr;
7461 lsa = (struct sockaddr *)&sin;
7462 }
7463 break;
7464#endif
7465#ifdef INET6
7466 case SCTP_IPV6_ADDRESS:
7467 if (plen !=
7468 sizeof(struct sctp_asconf_addr_param)) {
7469 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7470 (int)sizeof(struct sctp_asconf_addr_param),
7471 plen);
7472 } else {
7473 memcpy(sin6.sin6_addr.s6_addr,
7474 fee->addrp.addr,
7475 sizeof(fee->addrp.addr));
7476 lsa = (struct sockaddr *)&sin6;
7477 }
7478 break;
7479#endif
7480 default:
7481 break;
7482 }
7483 if (lsa) {
7484 (void)sctp_set_primary_addr(stcb, sa, NULL);
7485 }
7486 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7487 stcb->asoc.peer_supports_nat = 1;
7488 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7489 /* Peer supports pr-sctp */
t00fcxenaac0f452014-08-12 12:51:45 +00007490 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007491 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7492 /* A supported extension chunk */
7493 struct sctp_supported_chunk_types_param *pr_supported;
7494 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7495 int num_ent, i;
7496
Michael Tuexen473225f2017-06-23 23:05:43 +02007497 if (plen > sizeof(local_store)) {
7498 return (-35);
7499 }
tuexendd729232011-11-01 23:04:43 +00007500 phdr = sctp_get_next_param(m, offset,
Michael Tuexen473225f2017-06-23 23:05:43 +02007501 (struct sctp_paramhdr *)&local_store, plen);
tuexendd729232011-11-01 23:04:43 +00007502 if (phdr == NULL) {
7503 return (-25);
7504 }
tuexendd729232011-11-01 23:04:43 +00007505 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7506 num_ent = plen - sizeof(struct sctp_paramhdr);
7507 for (i = 0; i < num_ent; i++) {
7508 switch (pr_supported->chunk_types[i]) {
7509 case SCTP_ASCONF:
t00fcxenaac0f452014-08-12 12:51:45 +00007510 peer_supports_asconf = 1;
t00fcxen10c96982014-12-17 20:37:01 +00007511 break;
tuexendd729232011-11-01 23:04:43 +00007512 case SCTP_ASCONF_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007513 peer_supports_asconf_ack = 1;
tuexendd729232011-11-01 23:04:43 +00007514 break;
7515 case SCTP_FORWARD_CUM_TSN:
t00fcxenaac0f452014-08-12 12:51:45 +00007516 peer_supports_prsctp = 1;
tuexendd729232011-11-01 23:04:43 +00007517 break;
7518 case SCTP_PACKET_DROPPED:
t00fcxenaac0f452014-08-12 12:51:45 +00007519 peer_supports_pktdrop = 1;
tuexendd729232011-11-01 23:04:43 +00007520 break;
7521 case SCTP_NR_SELECTIVE_ACK:
t00fcxenaac0f452014-08-12 12:51:45 +00007522 peer_supports_nrsack = 1;
tuexendd729232011-11-01 23:04:43 +00007523 break;
7524 case SCTP_STREAM_RESET:
t00fcxenaac0f452014-08-12 12:51:45 +00007525 peer_supports_reconfig = 1;
tuexendd729232011-11-01 23:04:43 +00007526 break;
7527 case SCTP_AUTHENTICATION:
t00fcxenaac0f452014-08-12 12:51:45 +00007528 peer_supports_auth = 1;
tuexendd729232011-11-01 23:04:43 +00007529 break;
Michael Tuexene5001952016-04-17 19:25:27 +02007530 case SCTP_IDATA:
7531 peer_supports_idata = 1;
7532 break;
tuexendd729232011-11-01 23:04:43 +00007533 default:
7534 /* one I have not learned yet */
7535 break;
7536
7537 }
7538 }
7539 } else if (ptype == SCTP_RANDOM) {
7540 if (plen > sizeof(random_store))
7541 break;
7542 if (got_random) {
7543 /* already processed a RANDOM */
7544 goto next_param;
7545 }
7546 phdr = sctp_get_next_param(m, offset,
7547 (struct sctp_paramhdr *)random_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007548 plen);
tuexendd729232011-11-01 23:04:43 +00007549 if (phdr == NULL)
7550 return (-26);
7551 p_random = (struct sctp_auth_random *)phdr;
7552 random_len = plen - sizeof(*p_random);
7553 /* enforce the random length */
7554 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7555 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7556 return (-27);
7557 }
7558 got_random = 1;
7559 } else if (ptype == SCTP_HMAC_LIST) {
t00fcxen05d0bb92014-09-16 14:25:27 +00007560 uint16_t num_hmacs;
7561 uint16_t i;
tuexendd729232011-11-01 23:04:43 +00007562
7563 if (plen > sizeof(hmacs_store))
7564 break;
7565 if (got_hmacs) {
7566 /* already processed a HMAC list */
7567 goto next_param;
7568 }
7569 phdr = sctp_get_next_param(m, offset,
7570 (struct sctp_paramhdr *)hmacs_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007571 plen);
tuexendd729232011-11-01 23:04:43 +00007572 if (phdr == NULL)
7573 return (-28);
7574 hmacs = (struct sctp_auth_hmac_algo *)phdr;
7575 hmacs_len = plen - sizeof(*hmacs);
7576 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7577 /* validate the hmac list */
7578 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7579 return (-29);
7580 }
7581 if (stcb->asoc.peer_hmacs != NULL)
7582 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7583 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7584 if (stcb->asoc.peer_hmacs != NULL) {
7585 for (i = 0; i < num_hmacs; i++) {
7586 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7587 ntohs(hmacs->hmac_ids[i]));
7588 }
7589 }
7590 got_hmacs = 1;
7591 } else if (ptype == SCTP_CHUNK_LIST) {
7592 int i;
7593
7594 if (plen > sizeof(chunks_store))
7595 break;
7596 if (got_chklist) {
7597 /* already processed a Chunks list */
7598 goto next_param;
7599 }
7600 phdr = sctp_get_next_param(m, offset,
7601 (struct sctp_paramhdr *)chunks_store,
Michael Tuexen473225f2017-06-23 23:05:43 +02007602 plen);
tuexendd729232011-11-01 23:04:43 +00007603 if (phdr == NULL)
7604 return (-30);
7605 chunks = (struct sctp_auth_chunk_list *)phdr;
7606 num_chunks = plen - sizeof(*chunks);
7607 if (stcb->asoc.peer_auth_chunks != NULL)
7608 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7609 else
7610 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7611 for (i = 0; i < num_chunks; i++) {
7612 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7613 stcb->asoc.peer_auth_chunks);
7614 /* record asconf/asconf-ack if listed */
7615 if (chunks->chunk_types[i] == SCTP_ASCONF)
7616 saw_asconf = 1;
7617 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7618 saw_asconf_ack = 1;
7619
7620 }
7621 got_chklist = 1;
7622 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7623 (ptype == SCTP_STATE_COOKIE) ||
7624 (ptype == SCTP_UNRECOG_PARAM) ||
7625 (ptype == SCTP_COOKIE_PRESERVE) ||
7626 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7627 (ptype == SCTP_ADD_IP_ADDRESS) ||
7628 (ptype == SCTP_DEL_IP_ADDRESS) ||
7629 (ptype == SCTP_ERROR_CAUSE_IND) ||
7630 (ptype == SCTP_SUCCESS_REPORT)) {
7631 /* don't care */ ;
7632 } else {
7633 if ((ptype & 0x8000) == 0x0000) {
7634 /*
7635 * must stop processing the rest of the
7636 * param's. Any report bits were handled
7637 * with the call to
7638 * sctp_arethere_unrecognized_parameters()
7639 * when the INIT or INIT-ACK was first seen.
7640 */
7641 break;
7642 }
7643 }
tuexen09631162012-04-18 11:23:02 +00007644
tuexendd729232011-11-01 23:04:43 +00007645 next_param:
7646 offset += SCTP_SIZE32(plen);
7647 if (offset >= limit) {
7648 break;
7649 }
Michael Tuexen473225f2017-06-23 23:05:43 +02007650 phdr = sctp_get_next_param(m, offset, &param_buf,
7651 sizeof(param_buf));
tuexendd729232011-11-01 23:04:43 +00007652 }
7653 /* Now check to see if we need to purge any addresses */
7654 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7655 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7656 SCTP_ADDR_NOT_IN_ASSOC) {
7657 /* This address has been removed from the asoc */
7658 /* remove and free it */
7659 stcb->asoc.numnets--;
7660 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
7661 sctp_free_remote_addr(net);
7662 if (net == stcb->asoc.primary_destination) {
7663 stcb->asoc.primary_destination = NULL;
7664 sctp_select_primary_destination(stcb);
7665 }
7666 }
7667 }
t00fcxenaac0f452014-08-12 12:51:45 +00007668 if ((stcb->asoc.ecn_supported == 1) &&
7669 (peer_supports_ecn == 0)) {
7670 stcb->asoc.ecn_supported = 0;
tuexendd729232011-11-01 23:04:43 +00007671 }
t00fcxenaac0f452014-08-12 12:51:45 +00007672 if ((stcb->asoc.prsctp_supported == 1) &&
7673 (peer_supports_prsctp == 0)) {
7674 stcb->asoc.prsctp_supported = 0;
7675 }
7676 if ((stcb->asoc.auth_supported == 1) &&
7677 ((peer_supports_auth == 0) ||
7678 (got_random == 0) || (got_hmacs == 0))) {
7679 stcb->asoc.auth_supported = 0;
7680 }
7681 if ((stcb->asoc.asconf_supported == 1) &&
7682 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
7683 (stcb->asoc.auth_supported == 0) ||
7684 (saw_asconf == 0) || (saw_asconf_ack == 0))) {
7685 stcb->asoc.asconf_supported = 0;
7686 }
7687 if ((stcb->asoc.reconfig_supported == 1) &&
7688 (peer_supports_reconfig == 0)) {
7689 stcb->asoc.reconfig_supported = 0;
7690 }
Michael Tuexene5001952016-04-17 19:25:27 +02007691 if ((stcb->asoc.idata_supported == 1) &&
7692 (peer_supports_idata == 0)) {
7693 stcb->asoc.idata_supported = 0;
7694 }
t00fcxenaac0f452014-08-12 12:51:45 +00007695 if ((stcb->asoc.nrsack_supported == 1) &&
7696 (peer_supports_nrsack == 0)) {
7697 stcb->asoc.nrsack_supported = 0;
7698 }
7699 if ((stcb->asoc.pktdrop_supported == 1) &&
7700 (peer_supports_pktdrop == 0)){
7701 stcb->asoc.pktdrop_supported = 0;
7702 }
7703 /* validate authentication required parameters */
7704 if ((peer_supports_auth == 0) && (got_chklist == 1)) {
tuexendd729232011-11-01 23:04:43 +00007705 /* peer does not support auth but sent a chunks list? */
7706 return (-31);
7707 }
t00fcxenaac0f452014-08-12 12:51:45 +00007708 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
tuexendd729232011-11-01 23:04:43 +00007709 /* peer supports asconf but not auth? */
7710 return (-32);
t00fcxenaac0f452014-08-12 12:51:45 +00007711 } else if ((peer_supports_asconf == 1) &&
7712 (peer_supports_auth == 1) &&
tuexendd729232011-11-01 23:04:43 +00007713 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7714 return (-33);
7715 }
7716 /* concatenate the full random key */
7717 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7718 if (chunks != NULL) {
7719 keylen += sizeof(*chunks) + num_chunks;
7720 }
7721 new_key = sctp_alloc_key(keylen);
7722 if (new_key != NULL) {
7723 /* copy in the RANDOM */
7724 if (p_random != NULL) {
7725 keylen = sizeof(*p_random) + random_len;
Michael Tuexend0853302017-07-19 17:01:31 +02007726 memcpy(new_key->key, p_random, keylen);
Michael Tuexen8789a6d2018-06-02 18:33:47 +02007727 } else {
7728 keylen = 0;
tuexendd729232011-11-01 23:04:43 +00007729 }
7730 /* append in the AUTH chunks */
7731 if (chunks != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007732 memcpy(new_key->key + keylen, chunks,
7733 sizeof(*chunks) + num_chunks);
tuexendd729232011-11-01 23:04:43 +00007734 keylen += sizeof(*chunks) + num_chunks;
7735 }
7736 /* append in the HMACs */
7737 if (hmacs != NULL) {
Michael Tuexend0853302017-07-19 17:01:31 +02007738 memcpy(new_key->key + keylen, hmacs,
7739 sizeof(*hmacs) + hmacs_len);
tuexendd729232011-11-01 23:04:43 +00007740 }
7741 } else {
7742 /* failed to get memory for the key */
7743 return (-34);
7744 }
7745 if (stcb->asoc.authinfo.peer_random != NULL)
7746 sctp_free_key(stcb->asoc.authinfo.peer_random);
7747 stcb->asoc.authinfo.peer_random = new_key;
7748 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7749 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7750
7751 return (0);
7752}
7753
7754int
7755sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7756 struct sctp_nets *net)
7757{
7758 /* make sure the requested primary address exists in the assoc */
7759 if (net == NULL && sa)
7760 net = sctp_findnet(stcb, sa);
7761
7762 if (net == NULL) {
7763 /* didn't find the requested primary address! */
7764 return (-1);
7765 } else {
7766 /* set the primary address */
7767 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7768 /* Must be confirmed, so queue to set */
7769 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7770 return (0);
7771 }
7772 stcb->asoc.primary_destination = net;
7773 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
7774 sctp_free_remote_addr(stcb->asoc.alternate);
7775 stcb->asoc.alternate = NULL;
7776 }
7777 net = TAILQ_FIRST(&stcb->asoc.nets);
7778 if (net != stcb->asoc.primary_destination) {
7779 /* first one on the list is NOT the primary
7780 * sctp_cmpaddr() is much more efficient if
7781 * the primary is the first on the list, make it
7782 * so.
7783 */
7784 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7785 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7786 }
7787 return (0);
7788 }
7789}
7790
7791int
tuexen9784e9a2011-12-18 13:04:23 +00007792sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
tuexendd729232011-11-01 23:04:43 +00007793{
7794 /*
7795 * This function serves two purposes. It will see if a TAG can be
7796 * re-used and return 1 for yes it is ok and 0 for don't use that
7797 * tag. A secondary function it will do is purge out old tags that
7798 * can be removed.
7799 */
7800 struct sctpvtaghead *chain;
7801 struct sctp_tagblock *twait_block;
7802 struct sctpasochead *head;
7803 struct sctp_tcb *stcb;
7804 int i;
7805
7806 SCTP_INP_INFO_RLOCK();
7807 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
7808 SCTP_BASE_INFO(hashasocmark))];
tuexendd729232011-11-01 23:04:43 +00007809 LIST_FOREACH(stcb, head, sctp_asocs) {
7810 /* We choose not to lock anything here. TCB's can't be
7811 * removed since we have the read lock, so they can't
7812 * be freed on us, same thing for the INP. I may
7813 * be wrong with this assumption, but we will go
7814 * with it for now :-)
7815 */
7816 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7817 continue;
7818 }
7819 if (stcb->asoc.my_vtag == tag) {
7820 /* candidate */
7821 if (stcb->rport != rport) {
7822 continue;
7823 }
7824 if (stcb->sctp_ep->sctp_lport != lport) {
7825 continue;
7826 }
7827 /* Its a used tag set */
7828 SCTP_INP_INFO_RUNLOCK();
7829 return (0);
7830 }
7831 }
tuexendd729232011-11-01 23:04:43 +00007832 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
7833 /* Now what about timed wait ? */
t00fcxen14f8fbb2013-06-25 09:32:47 +00007834 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
tuexendd729232011-11-01 23:04:43 +00007835 /*
7836 * Block(s) are present, lets see if we have this tag in the
7837 * list
7838 */
t00fcxen14f8fbb2013-06-25 09:32:47 +00007839 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
7840 if (twait_block->vtag_block[i].v_tag == 0) {
7841 /* not used */
7842 continue;
7843 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
7844 now->tv_sec) {
7845 /* Audit expires this guy */
7846 twait_block->vtag_block[i].tv_sec_at_expire = 0;
7847 twait_block->vtag_block[i].v_tag = 0;
7848 twait_block->vtag_block[i].lport = 0;
7849 twait_block->vtag_block[i].rport = 0;
7850 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
7851 (twait_block->vtag_block[i].lport == lport) &&
7852 (twait_block->vtag_block[i].rport == rport)) {
7853 /* Bad tag, sorry :< */
7854 SCTP_INP_INFO_RUNLOCK();
7855 return (0);
tuexendd729232011-11-01 23:04:43 +00007856 }
7857 }
7858 }
7859 SCTP_INP_INFO_RUNLOCK();
7860 return (1);
7861}
7862
tuexendd729232011-11-01 23:04:43 +00007863static void
tuexen9784e9a2011-12-18 13:04:23 +00007864sctp_drain_mbufs(struct sctp_tcb *stcb)
tuexendd729232011-11-01 23:04:43 +00007865{
7866 /*
7867 * We must hunt this association for MBUF's past the cumack (i.e.
7868 * out of order data that we can renege on).
7869 */
7870 struct sctp_association *asoc;
7871 struct sctp_tmit_chunk *chk, *nchk;
7872 uint32_t cumulative_tsn_p1;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007873 struct sctp_queued_to_read *control, *ncontrol;
tuexendd729232011-11-01 23:04:43 +00007874 int cnt, strmat;
7875 uint32_t gap, i;
tuexen63fc0bb2011-12-27 12:24:52 +00007876 int fnd = 0;
tuexendd729232011-11-01 23:04:43 +00007877
7878 /* We look for anything larger than the cum-ack + 1 */
7879
7880 asoc = &stcb->asoc;
7881 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7882 /* none we can reneg on. */
7883 return;
7884 }
7885 SCTP_STAT_INCR(sctps_protocol_drains_done);
7886 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7887 cnt = 0;
tuexendd729232011-11-01 23:04:43 +00007888 /* Ok that was fun, now we will drain all the inbound streams? */
7889 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007890 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007891#ifdef INVARIANTS
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007892 if (control->on_strm_q != SCTP_ON_ORDERED ) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007893 panic("Huh control: %p on_q: %d -- not ordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007894 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007895 }
7896#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007897 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
tuexendd729232011-11-01 23:04:43 +00007898 /* Yep it is above cum-ack */
7899 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007900 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7901 KASSERT(control->length > 0, ("control has zero length"));
7902 if (asoc->size_on_all_streams >= control->length) {
7903 asoc->size_on_all_streams -= control->length;
7904 } else {
7905#ifdef INVARIANTS
7906 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7907#else
7908 asoc->size_on_all_streams = 0;
7909#endif
7910 }
tuexendd729232011-11-01 23:04:43 +00007911 sctp_ucount_decr(asoc->cnt_on_all_streams);
7912 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007913 if (control->on_read_q) {
7914 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7915 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007916 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007917 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
7918 control->on_strm_q = 0;
7919 if (control->data) {
7920 sctp_m_freem(control->data);
7921 control->data = NULL;
tuexendd729232011-11-01 23:04:43 +00007922 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007923 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007924 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007925 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007926 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007927 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007928 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7929 if (asoc->size_on_reasm_queue >= chk->send_size) {
7930 asoc->size_on_reasm_queue -= chk->send_size;
7931 } else {
7932#ifdef INVARIANTS
7933 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7934#else
7935 asoc->size_on_reasm_queue = 0;
7936#endif
7937 }
Michael Tuexene5001952016-04-17 19:25:27 +02007938 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7939 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007940 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02007941 if (chk->data) {
7942 sctp_m_freem(chk->data);
7943 chk->data = NULL;
7944 }
7945 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7946 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007947 sctp_free_a_readq(stcb, control);
Michael Tuexene5001952016-04-17 19:25:27 +02007948 }
7949 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007950 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007951#ifdef INVARIANTS
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007952 if (control->on_strm_q != SCTP_ON_UNORDERED ) {
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007953 panic("Huh control: %p on_q: %d -- not unordered?",
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007954 control, control->on_strm_q);
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007955 }
7956#endif
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007957 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
Michael Tuexene5001952016-04-17 19:25:27 +02007958 /* Yep it is above cum-ack */
7959 cnt++;
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007960 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7961 KASSERT(control->length > 0, ("control has zero length"));
7962 if (asoc->size_on_all_streams >= control->length) {
7963 asoc->size_on_all_streams -= control->length;
7964 } else {
7965#ifdef INVARIANTS
7966 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7967#else
7968 asoc->size_on_all_streams = 0;
7969#endif
7970 }
Michael Tuexene5001952016-04-17 19:25:27 +02007971 sctp_ucount_decr(asoc->cnt_on_all_streams);
7972 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007973 if (control->on_read_q) {
7974 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7975 control->on_read_q = 0;
Michael Tuexenfdcf7902016-08-06 14:39:31 +02007976 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007977 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
7978 control->on_strm_q = 0;
7979 if (control->data) {
7980 sctp_m_freem(control->data);
7981 control->data = NULL;
Michael Tuexene5001952016-04-17 19:25:27 +02007982 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007983 sctp_free_remote_addr(control->whoFrom);
Michael Tuexene5001952016-04-17 19:25:27 +02007984 /* Now its reasm? */
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007985 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
Michael Tuexene5001952016-04-17 19:25:27 +02007986 cnt++;
Michael Tuexen00657ac2016-12-07 21:53:26 +01007987 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02007988 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7989 if (asoc->size_on_reasm_queue >= chk->send_size) {
7990 asoc->size_on_reasm_queue -= chk->send_size;
7991 } else {
7992#ifdef INVARIANTS
7993 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7994#else
7995 asoc->size_on_reasm_queue = 0;
7996#endif
7997 }
Michael Tuexene5001952016-04-17 19:25:27 +02007998 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7999 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02008000 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
Michael Tuexene5001952016-04-17 19:25:27 +02008001 if (chk->data) {
8002 sctp_m_freem(chk->data);
8003 chk->data = NULL;
8004 }
8005 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
8006 }
Michael Tuexenbe5e3e72017-07-19 14:44:48 +02008007 sctp_free_a_readq(stcb, control);
tuexendd729232011-11-01 23:04:43 +00008008 }
8009 }
8010 }
8011 if (cnt) {
8012 /* We must back down to see what the new highest is */
8013 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
8014 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
8015 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
8016 asoc->highest_tsn_inside_map = i;
8017 fnd = 1;
8018 break;
8019 }
8020 }
8021 if (!fnd) {
8022 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
8023 }
8024
8025 /*
8026 * Question, should we go through the delivery queue? The only
8027 * reason things are on here is the app not reading OR a p-d-api up.
8028 * An attacker COULD send enough in to initiate the PD-API and then
8029 * send a bunch of stuff to other streams... these would wind up on
8030 * the delivery queue.. and then we would not get to them. But in
8031 * order to do this I then have to back-track and un-deliver
8032 * sequence numbers in streams.. el-yucko. I think for now we will
8033 * NOT look at the delivery queue and leave it to be something to
8034 * consider later. An alternative would be to abort the P-D-API with
8035 * a notification and then deliver the data.... Or another method
8036 * might be to keep track of how many times the situation occurs and
8037 * if we see a possible attack underway just abort the association.
8038 */
8039#ifdef SCTP_DEBUG
8040 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
8041#endif
8042 /*
8043 * Now do we need to find a new
8044 * asoc->highest_tsn_inside_map?
8045 */
8046 asoc->last_revoke_count = cnt;
Michael Tuexen61f66832020-02-09 23:15:19 +01008047 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
Michael Tuexen21b55df2020-02-09 23:46:32 +01008048 SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
tuexendd729232011-11-01 23:04:43 +00008049 /*sa_ignore NO_NULL_CHK*/
8050 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
8051 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00008052 }
8053 /*
8054 * Another issue, in un-setting the TSN's in the mapping array we
8055 * DID NOT adjust the highest_tsn marker. This will cause one of two
8056 * things to occur. It may cause us to do extra work in checking for
8057 * our mapping array movement. More importantly it may cause us to
8058 * SACK every datagram. This may not be a bad thing though since we
8059 * will recover once we get our cum-ack above and all this stuff we
8060 * dumped recovered.
8061 */
8062}
8063
8064void
8065sctp_drain()
8066{
8067 /*
8068 * We must walk the PCB lists for ALL associations here. The system
8069 * is LOW on MBUF's and needs help. This is where reneging will
8070 * occur. We really hope this does NOT happen!
8071 */
8072#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8073 VNET_ITERATOR_DECL(vnet_iter);
8074#else
8075 struct sctp_inpcb *inp;
8076 struct sctp_tcb *stcb;
8077
8078 SCTP_STAT_INCR(sctps_protocol_drain_calls);
8079 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
8080 return;
8081 }
8082#endif
8083#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8084 VNET_LIST_RLOCK_NOSLEEP();
8085 VNET_FOREACH(vnet_iter) {
8086 CURVNET_SET(vnet_iter);
8087 struct sctp_inpcb *inp;
8088 struct sctp_tcb *stcb;
8089#endif
8090
8091#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8092 SCTP_STAT_INCR(sctps_protocol_drain_calls);
8093 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
8094#ifdef VIMAGE
8095 continue;
8096#else
8097 return;
tuexen09631162012-04-18 11:23:02 +00008098#endif
tuexendd729232011-11-01 23:04:43 +00008099 }
8100#endif
8101 SCTP_INP_INFO_RLOCK();
8102 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
8103 /* For each endpoint */
8104 SCTP_INP_RLOCK(inp);
8105 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
8106 /* For each association */
8107 SCTP_TCB_LOCK(stcb);
tuexen9784e9a2011-12-18 13:04:23 +00008108 sctp_drain_mbufs(stcb);
tuexendd729232011-11-01 23:04:43 +00008109 SCTP_TCB_UNLOCK(stcb);
8110 }
8111 SCTP_INP_RUNLOCK(inp);
8112 }
8113 SCTP_INP_INFO_RUNLOCK();
8114#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8115 CURVNET_RESTORE();
8116 }
8117 VNET_LIST_RUNLOCK_NOSLEEP();
8118#endif
8119}
8120
8121/*
8122 * start a new iterator
8123 * iterates through all endpoints and associations based on the pcb_state
8124 * flags and asoc_state. "af" (mandatory) is executed for all matching
8125 * assocs and "ef" (optional) is executed when the iterator completes.
8126 * "inpf" (optional) is executed for each new endpoint as it is being
8127 * iterated through. inpe (optional) is called when the inp completes
8128 * its way through all the stcbs.
8129 */
8130int
8131sctp_initiate_iterator(inp_func inpf,
8132 asoc_func af,
8133 inp_func inpe,
8134 uint32_t pcb_state,
8135 uint32_t pcb_features,
8136 uint32_t asoc_state,
8137 void *argp,
8138 uint32_t argi,
8139 end_func ef,
8140 struct sctp_inpcb *s_inp,
8141 uint8_t chunk_output_off)
8142{
8143 struct sctp_iterator *it = NULL;
8144
8145 if (af == NULL) {
8146 return (-1);
8147 }
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008148 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8149 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
8150 SCTP_BASE_VAR(sctp_pcb_initialized));
8151 return (-1);
8152 }
tuexendd729232011-11-01 23:04:43 +00008153 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
8154 SCTP_M_ITER);
8155 if (it == NULL) {
8156 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
Michael Tuexen287284c2020-01-12 14:34:21 +01008157 return (-1);
tuexendd729232011-11-01 23:04:43 +00008158 }
8159 memset(it, 0, sizeof(*it));
8160 it->function_assoc = af;
8161 it->function_inp = inpf;
8162 if (inpf)
8163 it->done_current_ep = 0;
8164 else
8165 it->done_current_ep = 1;
8166 it->function_atend = ef;
8167 it->pointer = argp;
8168 it->val = argi;
8169 it->pcb_flags = pcb_state;
8170 it->pcb_features = pcb_features;
8171 it->asoc_state = asoc_state;
8172 it->function_inp_end = inpe;
8173 it->no_chunk_output = chunk_output_off;
8174#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
8175 it->vn = curvnet;
tuexen09631162012-04-18 11:23:02 +00008176#endif
tuexendd729232011-11-01 23:04:43 +00008177 if (s_inp) {
8178 /* Assume lock is held here */
8179 it->inp = s_inp;
8180 SCTP_INP_INCR_REF(it->inp);
8181 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
8182 } else {
8183 SCTP_INP_INFO_RLOCK();
8184 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
8185 if (it->inp) {
8186 SCTP_INP_INCR_REF(it->inp);
8187 }
8188 SCTP_INP_INFO_RUNLOCK();
8189 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
8190
8191 }
8192 SCTP_IPI_ITERATOR_WQ_LOCK();
Michael Tuexen71a83fb2016-02-16 21:52:58 +01008193 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8194 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8195 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
8196 SCTP_BASE_VAR(sctp_pcb_initialized), it);
8197 SCTP_FREE(it, SCTP_M_ITER);
8198 return (-1);
8199 }
tuexendd729232011-11-01 23:04:43 +00008200 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
8201 if (sctp_it_ctl.iterator_running == 0) {
8202 sctp_wakeup_iterator();
8203 }
8204 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8205 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
8206 return (0);
8207}