blob: 50c53265e8529f2c4657bc58c34ffde3dd1bf066 [file] [log] [blame]
tuexendd729232011-11-01 23:04:43 +00001/*-
2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
tuexen194eae12012-05-23 12:03:48 +00003 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
tuexendd729232011-11-01 23:04:43 +00005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * a) Redistributions of source code must retain the above copyright notice,
tuexen9784e9a2011-12-18 13:04:23 +000010 * this list of conditions and the following disclaimer.
tuexendd729232011-11-01 23:04:43 +000011 *
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
tuexen9784e9a2011-12-18 13:04:23 +000014 * the documentation and/or other materials provided with the distribution.
tuexendd729232011-11-01 23:04:43 +000015 *
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
tuexendd729232011-11-01 23:04:43 +000033#ifdef __FreeBSD__
34#include <sys/cdefs.h>
t00fcxenefb47e72012-09-09 08:16:51 +000035__FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.c 240263 2012-09-09 08:14:04Z tuexen $");
tuexendd729232011-11-01 23:04:43 +000036#endif
37
38#include <netinet/sctp_os.h>
39#ifdef __FreeBSD__
40#include <sys/proc.h>
41#endif
42#include <netinet/sctp_var.h>
43#include <netinet/sctp_sysctl.h>
44#include <netinet/sctp_pcb.h>
45#include <netinet/sctputil.h>
46#include <netinet/sctp.h>
47#include <netinet/sctp_header.h>
48#include <netinet/sctp_asconf.h>
49#include <netinet/sctp_output.h>
50#include <netinet/sctp_timer.h>
51#include <netinet/sctp_bsd_addr.h>
tuexen5fc22842012-02-11 13:32:19 +000052#if defined(__FreeBSD__) && __FreeBSD_version >= 803000
tuexendd729232011-11-01 23:04:43 +000053#include <netinet/sctp_dtrace_define.h>
tuexen63fc0bb2011-12-27 12:24:52 +000054#endif
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
tuexendd729232011-11-01 23:04:43 +000058#ifdef INET6
tuexen5154a002011-12-23 18:47:55 +000059#if defined(__Userspace__)
60#include "user_ip6_var.h"
61#else
tuexendd729232011-11-01 23:04:43 +000062#include <netinet6/ip6_var.h>
63#endif
tuexen5154a002011-12-23 18:47:55 +000064#endif
tuexendd729232011-11-01 23:04:43 +000065#if defined(__FreeBSD__)
66#include <sys/sched.h>
67#include <sys/smp.h>
tuexenc0bb6122012-01-15 14:21:21 +000068#include <sys/unistd.h>
tuexendd729232011-11-01 23:04:43 +000069#endif
tuexen5154a002011-12-23 18:47:55 +000070#if defined(__Userspace__)
tuexenc0bb6122012-01-15 14:21:21 +000071#if !defined(__Userspace_os_Windows)
72#include <sys/unistd.h>
73#endif
tuexen5154a002011-12-23 18:47:55 +000074#include <user_socketvar.h>
75#endif
tuexendd729232011-11-01 23:04:43 +000076
77#if defined(__APPLE__)
78#define APPLE_FILE_NO 4
79#endif
80
81#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
82VNET_DEFINE(struct sctp_base_info, system_base_info);
83#else
84struct sctp_base_info system_base_info;
85#endif
86
87#if defined(__Userspace__)
88struct ifaddrs *g_interfaces;
89#endif
90/* FIX: we don't handle multiple link local scopes */
91/* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
92#ifdef INET6
93int
94SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
95{
96#ifdef SCTP_EMBEDDED_V6_SCOPE
tuexene3851592012-06-01 08:31:45 +000097#if defined(__APPLE__)
tuexendd729232011-11-01 23:04:43 +000098 struct in6_addr tmp_a, tmp_b;
99
100 tmp_a = a->sin6_addr;
tuexen9a218b12012-08-04 21:17:58 +0000101#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +0000102 if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) {
tuexen9a218b12012-08-04 21:17:58 +0000103#else
104 if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +0000105#endif
tuexen63fc0bb2011-12-27 12:24:52 +0000106 return (0);
tuexendd729232011-11-01 23:04:43 +0000107 }
108 tmp_b = b->sin6_addr;
tuexen9a218b12012-08-04 21:17:58 +0000109#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +0000110 if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) {
tuexen9a218b12012-08-04 21:17:58 +0000111#else
112 if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +0000113#endif
tuexen63fc0bb2011-12-27 12:24:52 +0000114 return (0);
tuexendd729232011-11-01 23:04:43 +0000115 }
116 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
117#elif defined(SCTP_KAME)
118 struct sockaddr_in6 tmp_a, tmp_b;
119
120 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
121 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000122 return (0);
tuexendd729232011-11-01 23:04:43 +0000123 }
124 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
125 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000126 return (0);
tuexendd729232011-11-01 23:04:43 +0000127 }
128 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
129#else
130 struct in6_addr tmp_a, tmp_b;
131
132 tmp_a = a->sin6_addr;
133 if (in6_embedscope(&tmp_a, a) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000134 return (0);
tuexendd729232011-11-01 23:04:43 +0000135 }
136 tmp_b = b->sin6_addr;
137 if (in6_embedscope(&tmp_b, b) != 0) {
tuexen63fc0bb2011-12-27 12:24:52 +0000138 return (0);
tuexendd729232011-11-01 23:04:43 +0000139 }
140 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
141#endif
142#else
tuexen5154a002011-12-23 18:47:55 +0000143 return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr)));
tuexendd729232011-11-01 23:04:43 +0000144#endif /* SCTP_EMBEDDED_V6_SCOPE */
145}
146#endif
147
148void
149sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
150{
151 /*
152 * We really don't need to lock this, but I will just because it
153 * does not hurt.
154 */
155#if defined(__APPLE__)
156 /*
157 * On Tiger the caller MUST do all necessary locking.
158 */
159#endif
160 SCTP_INP_INFO_RLOCK();
161 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
162 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
163 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
164 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
165 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
166 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
167 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
168 spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
169
170 SCTP_INP_INFO_RUNLOCK();
171}
172
173/*
174 * Addresses are added to VRF's (Virtual Router's). For BSD we
175 * have only the default VRF 0. We maintain a hash list of
176 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
177 * these has a list of addresses. When we add a new address
178 * to a VRF we lookup the ifn/ifn_index, if the ifn does
179 * not exist we create it and add it to the list of IFN's
180 * within the VRF. Once we have the sctp_ifn, we add the
181 * address to the list. So we look something like:
182 *
183 * hash-vrf-table
184 * vrf-> ifn-> ifn -> ifn
185 * vrf |
186 * ... +--ifa-> ifa -> ifa
187 * vrf
188 *
189 * We keep these separate lists since the SCTP subsystem will
190 * point to these from its source address selection nets structure.
191 * When an address is deleted it does not happen right away on
192 * the SCTP side, it gets scheduled. What we do when a
193 * delete happens is immediately remove the address from
194 * the master list and decrement the refcount. As our
195 * addip iterator works through and frees the src address
196 * selection pointing to the sctp_ifa, eventually the refcount
197 * will reach 0 and we will delete it. Note that it is assumed
198 * that any locking on system level ifn/ifa is done at the
199 * caller of these functions and these routines will only
200 * lock the SCTP structures as they add or delete things.
201 *
202 * Other notes on VRF concepts.
203 * - An endpoint can be in multiple VRF's
204 * - An association lives within a VRF and only one VRF.
205 * - Any incoming packet we can deduce the VRF for by
206 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
207 * - Any downward send call or connect call must supply the
208 * VRF via ancillary data or via some sort of set default
209 * VRF socket option call (again for BSD no brainer since
210 * the VRF is always 0).
211 * - An endpoint may add multiple VRF's to it.
212 * - Listening sockets can accept associations in any
213 * of the VRF's they are in but the assoc will end up
214 * in only one VRF (gotten from the packet or connect/send).
215 *
216 */
217
218struct sctp_vrf *
219sctp_allocate_vrf(int vrf_id)
220{
tuexen63fc0bb2011-12-27 12:24:52 +0000221 struct sctp_vrf *vrf = NULL;
tuexendd729232011-11-01 23:04:43 +0000222 struct sctp_vrflist *bucket;
223
224 /* First allocate the VRF structure */
225 vrf = sctp_find_vrf(vrf_id);
226 if (vrf) {
227 /* Already allocated */
228 return (vrf);
229 }
230 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
231 SCTP_M_VRF);
232 if (vrf == NULL) {
233 /* No memory */
234#ifdef INVARIANTS
235 panic("No memory for VRF:%d", vrf_id);
236#endif
237 return (NULL);
238 }
239 /* setup the VRF */
240 memset(vrf, 0, sizeof(struct sctp_vrf));
241 vrf->vrf_id = vrf_id;
242 LIST_INIT(&vrf->ifnlist);
243 vrf->total_ifa_count = 0;
244 vrf->refcount = 0;
245 /* now also setup table ids */
246 SCTP_INIT_VRF_TABLEID(vrf);
247 /* Init the HASH of addresses */
248 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
249 &vrf->vrf_addr_hashmark);
250 if (vrf->vrf_addr_hash == NULL) {
251 /* No memory */
252#ifdef INVARIANTS
253 panic("No memory for VRF:%d", vrf_id);
254#endif
255 SCTP_FREE(vrf, SCTP_M_VRF);
256 return (NULL);
257 }
258
259 /* Add it to the hash table */
260 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
261 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
262 atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
263 return (vrf);
264}
265
266
267struct sctp_ifn *
268sctp_find_ifn(void *ifn, uint32_t ifn_index)
269{
270 struct sctp_ifn *sctp_ifnp;
271 struct sctp_ifnlist *hash_ifn_head;
272
273 /* We assume the lock is held for the addresses
274 * if that's wrong problems could occur :-)
275 */
276 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
277 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
278 if (sctp_ifnp->ifn_index == ifn_index) {
tuexen63fc0bb2011-12-27 12:24:52 +0000279 return (sctp_ifnp);
tuexendd729232011-11-01 23:04:43 +0000280 }
281 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
tuexen63fc0bb2011-12-27 12:24:52 +0000282 return (sctp_ifnp);
tuexendd729232011-11-01 23:04:43 +0000283 }
284 }
tuexen63fc0bb2011-12-27 12:24:52 +0000285 return (NULL);
tuexendd729232011-11-01 23:04:43 +0000286}
287
288
289
290struct sctp_vrf *
291sctp_find_vrf(uint32_t vrf_id)
292{
293 struct sctp_vrflist *bucket;
294 struct sctp_vrf *liste;
295 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
296 LIST_FOREACH(liste, bucket, next_vrf) {
297 if (vrf_id == liste->vrf_id) {
tuexen63fc0bb2011-12-27 12:24:52 +0000298 return (liste);
tuexendd729232011-11-01 23:04:43 +0000299 }
300 }
301 return (NULL);
302}
303
304void
305sctp_free_vrf(struct sctp_vrf *vrf)
306{
307 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
308 if (vrf->vrf_addr_hash) {
309 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
310 vrf->vrf_addr_hash = NULL;
311 }
312 /* We zero'd the count */
313 LIST_REMOVE(vrf, next_vrf);
314 SCTP_FREE(vrf, SCTP_M_VRF);
315 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
316 }
317}
318
319void
320sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
321{
322 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
323 /* We zero'd the count */
324 if (sctp_ifnp->vrf) {
325 sctp_free_vrf(sctp_ifnp->vrf);
326 }
327 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
328 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
329 }
330}
331
332void
333sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
334{
335 struct sctp_ifn *sctp_ifnp;
336
337 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
338 if (sctp_ifnp != NULL) {
339 sctp_ifnp->ifn_mtu = mtu;
340 }
341}
342
343
344void
345sctp_free_ifa(struct sctp_ifa *sctp_ifap)
346{
347 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
348 /* We zero'd the count */
349 if (sctp_ifap->ifn_p) {
350 sctp_free_ifn(sctp_ifap->ifn_p);
351 }
352 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
353 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
354 }
355}
356
357static void
358sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
359{
360 struct sctp_ifn *found;
361
362 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
363 if (found == NULL) {
364 /* Not in the list.. sorry */
365 return;
366 }
367 if (hold_addr_lock == 0)
368 SCTP_IPI_ADDR_WLOCK();
369 LIST_REMOVE(sctp_ifnp, next_bucket);
370 LIST_REMOVE(sctp_ifnp, next_ifn);
371 SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index,
372 sctp_ifnp->registered_af);
373 if (hold_addr_lock == 0)
374 SCTP_IPI_ADDR_WUNLOCK();
375 /* Take away the reference, and possibly free it */
376 sctp_free_ifn(sctp_ifnp);
377}
378
379void
380sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
381 const char *if_name, uint32_t ifn_index)
382{
383 struct sctp_vrf *vrf;
384 struct sctp_ifa *sctp_ifap = NULL;
385 SCTP_IPI_ADDR_RLOCK();
386 vrf = sctp_find_vrf(vrf_id);
387 if (vrf == NULL) {
388 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
389 goto out;
390
391 }
392 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
393 if (sctp_ifap == NULL) {
394 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
395 goto out;
tuexen0e813942011-11-18 09:18:32 +0000396 }
tuexendd729232011-11-01 23:04:43 +0000397 if (sctp_ifap->ifn_p == NULL) {
398 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
399 goto out;
400 }
401 if (if_name) {
tuexen0e813942011-11-18 09:18:32 +0000402 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
tuexendd729232011-11-01 23:04:43 +0000403 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
tuexen0e813942011-11-18 09:18:32 +0000404 sctp_ifap->ifn_p->ifn_name, if_name);
tuexendd729232011-11-01 23:04:43 +0000405 goto out;
406 }
407 } else {
408 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
409 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
410 sctp_ifap->ifn_p->ifn_index, ifn_index);
411 goto out;
412 }
413 }
tuexen0e813942011-11-18 09:18:32 +0000414
tuexendd729232011-11-01 23:04:43 +0000415 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
416 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
417 out:
418 SCTP_IPI_ADDR_RUNLOCK();
419}
420
421void
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;
426 struct sctp_ifa *sctp_ifap = NULL;
427 SCTP_IPI_ADDR_RLOCK();
428 vrf = sctp_find_vrf(vrf_id);
429 if (vrf == NULL) {
430 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
431 goto out;
432
433 }
434 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
435 if (sctp_ifap == NULL) {
436 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
437 goto out;
tuexen0e813942011-11-18 09:18:32 +0000438 }
tuexendd729232011-11-01 23:04:43 +0000439 if (sctp_ifap->ifn_p == NULL) {
440 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
441 goto out;
442 }
443 if (if_name) {
tuexen0e813942011-11-18 09:18:32 +0000444 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
tuexendd729232011-11-01 23:04:43 +0000445 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
tuexen0e813942011-11-18 09:18:32 +0000446 sctp_ifap->ifn_p->ifn_name, if_name);
tuexendd729232011-11-01 23:04:43 +0000447 goto out;
448 }
449 } else {
450 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
451 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
452 sctp_ifap->ifn_p->ifn_index, ifn_index);
453 goto out;
454 }
455 }
456
457 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
458 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
459 out:
460 SCTP_IPI_ADDR_RUNLOCK();
461}
462
463/*-
464 * Add an ifa to an ifn.
465 * Register the interface as necessary.
466 * NOTE: ADDR write lock MUST be held.
467 */
468static void
469sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
470{
471 int ifa_af;
472
473 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
474 sctp_ifap->ifn_p = sctp_ifnp;
475 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
476 /* update address counts */
477 sctp_ifnp->ifa_count++;
478 ifa_af = sctp_ifap->address.sa.sa_family;
479 switch (ifa_af) {
480#ifdef INET
481 case AF_INET:
482 sctp_ifnp->num_v4++;
483 break;
484#endif
485#ifdef INET6
486 case AF_INET6:
487 sctp_ifnp->num_v6++;
488 break;
489#endif
490 default:
491 break;
492 }
493 if (sctp_ifnp->ifa_count == 1) {
494 /* register the new interface */
495 SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af);
496 sctp_ifnp->registered_af = ifa_af;
497 }
498}
499
500/*-
501 * Remove an ifa from its ifn.
502 * If no more addresses exist, remove the ifn too. Otherwise, re-register
503 * the interface based on the remaining address families left.
504 * NOTE: ADDR write lock MUST be held.
505 */
506static void
507sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
508{
tuexendd729232011-11-01 23:04:43 +0000509 LIST_REMOVE(sctp_ifap, next_ifa);
510 if (sctp_ifap->ifn_p) {
511 /* update address counts */
512 sctp_ifap->ifn_p->ifa_count--;
513 switch (sctp_ifap->address.sa.sa_family) {
514#ifdef INET
515 case AF_INET:
516 sctp_ifap->ifn_p->num_v4--;
517 break;
518#endif
519#ifdef INET6
520 case AF_INET6:
521 sctp_ifap->ifn_p->num_v6--;
522 break;
523#endif
524 default:
525 break;
526 }
527
tuexendd729232011-11-01 23:04:43 +0000528 if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
529 /* remove the ifn, possibly freeing it */
530 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
531 } else {
532 /* re-register address family type, if needed */
533 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
534 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
tuexen63fc0bb2011-12-27 12:24:52 +0000535 SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6);
536 SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET);
tuexendd729232011-11-01 23:04:43 +0000537 sctp_ifap->ifn_p->registered_af = AF_INET;
538 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
539 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
tuexen63fc0bb2011-12-27 12:24:52 +0000540 SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET);
541 SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6);
tuexendd729232011-11-01 23:04:43 +0000542 sctp_ifap->ifn_p->registered_af = AF_INET6;
543 }
544 /* free the ifn refcount */
545 sctp_free_ifn(sctp_ifap->ifn_p);
546 }
547 sctp_ifap->ifn_p = NULL;
548 }
549}
550
551struct sctp_ifa *
552sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
553 uint32_t ifn_type, const char *if_name, void *ifa,
554 struct sockaddr *addr, uint32_t ifa_flags,
555 int dynamic_add)
556{
557 struct sctp_vrf *vrf;
558 struct sctp_ifn *sctp_ifnp = NULL;
559 struct sctp_ifa *sctp_ifap = NULL;
560 struct sctp_ifalist *hash_addr_head;
561 struct sctp_ifnlist *hash_ifn_head;
562 uint32_t hash_of_addr;
563 int new_ifn_af = 0;
564
565#ifdef SCTP_DEBUG
566 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
567 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
568#endif
569 SCTP_IPI_ADDR_WLOCK();
570 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
571 if (sctp_ifnp) {
572 vrf = sctp_ifnp->vrf;
573 } else {
574 vrf = sctp_find_vrf(vrf_id);
575 if (vrf == NULL) {
576 vrf = sctp_allocate_vrf(vrf_id);
577 if (vrf == NULL) {
578 SCTP_IPI_ADDR_WUNLOCK();
579 return (NULL);
580 }
581 }
582 }
583 if (sctp_ifnp == NULL) {
584 /* build one and add it, can't hold lock
585 * until after malloc done though.
586 */
587 SCTP_IPI_ADDR_WUNLOCK();
588 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *,
589 sizeof(struct sctp_ifn), SCTP_M_IFN);
590 if (sctp_ifnp == NULL) {
591#ifdef INVARIANTS
592 panic("No memory for IFN");
593#endif
594 return (NULL);
595 }
596 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
597 sctp_ifnp->ifn_index = ifn_index;
598 sctp_ifnp->ifn_p = ifn;
599 sctp_ifnp->ifn_type = ifn_type;
600 sctp_ifnp->refcount = 0;
601 sctp_ifnp->vrf = vrf;
602 atomic_add_int(&vrf->refcount, 1);
603 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
604 if (if_name != NULL) {
tuexenb740ef62011-11-13 11:58:06 +0000605 snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
tuexendd729232011-11-01 23:04:43 +0000606 } else {
tuexenb740ef62011-11-13 11:58:06 +0000607 snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
tuexendd729232011-11-01 23:04:43 +0000608 }
609 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
610 LIST_INIT(&sctp_ifnp->ifalist);
611 SCTP_IPI_ADDR_WLOCK();
612 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
613 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
614 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
615 new_ifn_af = 1;
616 }
617 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
618 if (sctp_ifap) {
619 /* Hmm, it already exists? */
620 if ((sctp_ifap->ifn_p) &&
621 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
622 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
623 sctp_ifap->ifn_p->ifn_name, ifn_index,
t00fcxen8fca7ff2012-09-05 18:58:42 +0000624 (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000625 if (new_ifn_af) {
626 /* Remove the created one that we don't want */
627 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
628 }
629 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
630 /* easy to solve, just switch back to active */
631 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
632 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
633 sctp_ifap->ifn_p = sctp_ifnp;
634 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
635 }
636 exit_stage_left:
637 SCTP_IPI_ADDR_WUNLOCK();
638 return (sctp_ifap);
639 } else {
640 if (sctp_ifap->ifn_p) {
641 /*
642 * The last IFN gets the address, remove the
643 * old one
644 */
645 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +0000646 (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name,
tuexendd729232011-11-01 23:04:43 +0000647 sctp_ifap->ifn_p->ifn_index, if_name,
648 ifn_index);
649 /* remove the address from the old ifn */
650 sctp_remove_ifa_from_ifn(sctp_ifap);
651 /* move the address over to the new ifn */
652 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
653 goto exit_stage_left;
654 } else {
655 /* repair ifnp which was NULL ? */
656 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
657 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +0000658 (void *)sctp_ifnp, (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000659 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
660 }
661 goto exit_stage_left;
662 }
663 }
664 SCTP_IPI_ADDR_WUNLOCK();
665 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
666 if (sctp_ifap == NULL) {
667#ifdef INVARIANTS
668 panic("No memory for IFA");
669#endif
670 return (NULL);
671 }
672 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
673 sctp_ifap->ifn_p = sctp_ifnp;
674 atomic_add_int(&sctp_ifnp->refcount, 1);
675 sctp_ifap->vrf_id = vrf_id;
676 sctp_ifap->ifa = ifa;
t00fcxena8a26f62012-09-12 19:24:54 +0000677#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +0000678 memcpy(&sctp_ifap->address, addr, addr->sa_len);
679#else
680 if (addr->sa_family == AF_INET) {
681 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in));
682 } else {
683 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6));
684 }
685#endif
686 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
687 sctp_ifap->flags = ifa_flags;
688 /* Set scope */
689 switch (sctp_ifap->address.sa.sa_family) {
690#ifdef INET
691 case AF_INET:
692 {
693 struct sockaddr_in *sin;
694 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
695 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
696 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
697 sctp_ifap->src_is_loop = 1;
698 }
699 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
700 sctp_ifap->src_is_priv = 1;
701 }
702 sctp_ifnp->num_v4++;
703 if (new_ifn_af)
704 new_ifn_af = AF_INET;
705 break;
706 }
707#endif
708#ifdef INET6
709 case AF_INET6:
710 {
711 /* ok to use deprecated addresses? */
712 struct sockaddr_in6 *sin6;
713 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
714 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
715 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
716 sctp_ifap->src_is_loop = 1;
717 }
718 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
719 sctp_ifap->src_is_priv = 1;
720 }
721 sctp_ifnp->num_v6++;
722 if (new_ifn_af)
723 new_ifn_af = AF_INET6;
724 break;
725 }
726#endif
727 default:
728 new_ifn_af = 0;
729 break;
730 }
731 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
732
733 if ((sctp_ifap->src_is_priv == 0) &&
734 (sctp_ifap->src_is_loop == 0)) {
735 sctp_ifap->src_is_glob = 1;
736 }
737 SCTP_IPI_ADDR_WLOCK();
738 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
739 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
740 sctp_ifap->refcount = 1;
741 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
742 sctp_ifnp->ifa_count++;
743 vrf->total_ifa_count++;
744 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
745 if (new_ifn_af) {
746 SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
747 sctp_ifnp->registered_af = new_ifn_af;
748 }
749 SCTP_IPI_ADDR_WUNLOCK();
750 if (dynamic_add) {
751 /* Bump up the refcount so that when the timer
752 * completes it will drop back down.
753 */
754 struct sctp_laddr *wi;
755
756 atomic_add_int(&sctp_ifap->refcount, 1);
757 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
758 if (wi == NULL) {
759 /*
760 * Gak, what can we do? We have lost an address
761 * change can you say HOSED?
762 */
763 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
764 /* Opps, must decrement the count */
765 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
766 if_name);
767 return (NULL);
768 }
769 SCTP_INCR_LADDR_COUNT();
770 bzero(wi, sizeof(*wi));
771 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
772 wi->ifa = sctp_ifap;
773 wi->action = SCTP_ADD_IP_ADDRESS;
tuexen09631162012-04-18 11:23:02 +0000774
tuexendd729232011-11-01 23:04:43 +0000775 SCTP_WQ_ADDR_LOCK();
776 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
777 SCTP_WQ_ADDR_UNLOCK();
778
779 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
780 (struct sctp_inpcb *)NULL,
781 (struct sctp_tcb *)NULL,
782 (struct sctp_nets *)NULL);
783 } else {
784 /* it's ready for use */
785 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
786 }
787 return (sctp_ifap);
788}
789
790void
791sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
792 uint32_t ifn_index, const char *if_name)
793{
794 struct sctp_vrf *vrf;
795 struct sctp_ifa *sctp_ifap = NULL;
796
797 SCTP_IPI_ADDR_WLOCK();
798 vrf = sctp_find_vrf(vrf_id);
799 if (vrf == NULL) {
800 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
801 goto out_now;
802 }
803
804#ifdef SCTP_DEBUG
805 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
806 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
807#endif
808 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
809 if (sctp_ifap) {
810 /* Validate the delete */
811 if (sctp_ifap->ifn_p) {
tuexenb740ef62011-11-13 11:58:06 +0000812 int valid = 0;
tuexendd729232011-11-01 23:04:43 +0000813 /*-
814 * The name has priority over the ifn_index
815 * if its given. We do this especially for
816 * panda who might recycle indexes fast.
817 */
818 if (if_name) {
tuexenb740ef62011-11-13 11:58:06 +0000819 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
820 /* They match its a correct delete */
821 valid = 1;
tuexendd729232011-11-01 23:04:43 +0000822 }
823 }
824 if (!valid) {
825 /* last ditch check ifn_index */
826 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
827 valid = 1;
828 }
829 }
830 if (!valid) {
831 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
832 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
833 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
834 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
835 SCTP_IPI_ADDR_WUNLOCK();
836 return;
837 }
838 }
t00fcxen8fca7ff2012-09-05 18:58:42 +0000839 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
tuexendd729232011-11-01 23:04:43 +0000840 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
841 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
842 vrf->total_ifa_count--;
843 LIST_REMOVE(sctp_ifap, next_bucket);
844 sctp_remove_ifa_from_ifn(sctp_ifap);
845 }
846#ifdef SCTP_DEBUG
847 else {
848 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
849 ifn_index);
850 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
851 }
852#endif
853
854 out_now:
855 SCTP_IPI_ADDR_WUNLOCK();
856 if (sctp_ifap) {
857 struct sctp_laddr *wi;
858
859 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
860 if (wi == NULL) {
861 /*
862 * Gak, what can we do? We have lost an address
863 * change can you say HOSED?
864 */
865 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
866
867 /* Oops, must decrement the count */
868 sctp_free_ifa(sctp_ifap);
869 return;
870 }
871 SCTP_INCR_LADDR_COUNT();
872 bzero(wi, sizeof(*wi));
873 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
874 wi->ifa = sctp_ifap;
875 wi->action = SCTP_DEL_IP_ADDRESS;
876 SCTP_WQ_ADDR_LOCK();
877 /*
878 * Should this really be a tailq? As it is we will process the
879 * newest first :-0
880 */
881 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
882 SCTP_WQ_ADDR_UNLOCK();
883
884 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
885 (struct sctp_inpcb *)NULL,
886 (struct sctp_tcb *)NULL,
887 (struct sctp_nets *)NULL);
888 }
889 return;
890}
891
892
893static struct sctp_tcb *
894sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
895 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
896{
897 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
898 /*
899 * If we support the TCP model, then we must now dig through to see
900 * if we can find our endpoint in the list of tcp ep's.
901 */
902 uint16_t lport, rport;
903 struct sctppcbhead *ephead;
904 struct sctp_inpcb *inp;
905 struct sctp_laddr *laddr;
906 struct sctp_tcb *stcb;
907 struct sctp_nets *net;
908#ifdef SCTP_MVRF
909 int fnd, i;
910#endif
911 if ((to == NULL) || (from == NULL)) {
912 return (NULL);
913 }
914
915 switch (to->sa_family) {
916#ifdef INET
917 case AF_INET:
918 if (from->sa_family == AF_INET) {
919 lport = ((struct sockaddr_in *)to)->sin_port;
920 rport = ((struct sockaddr_in *)from)->sin_port;
921 } else {
922 return (NULL);
923 }
924 break;
925#endif
926#ifdef INET6
927 case AF_INET6:
928 if (from->sa_family == AF_INET6) {
929 lport = ((struct sockaddr_in6 *)to)->sin6_port;
930 rport = ((struct sockaddr_in6 *)from)->sin6_port;
931 } else {
932 return (NULL);
933 }
934 break;
935#endif
tuexen5d9b9742012-07-11 21:52:01 +0000936#if defined(__Userspace__)
937 case AF_CONN:
938 if (from->sa_family == AF_CONN) {
939 lport = ((struct sockaddr_conn *)to)->sconn_port;
940 rport = ((struct sockaddr_conn *)from)->sconn_port;
941 } else {
942 return (NULL);
943 }
944 break;
945#endif
tuexendd729232011-11-01 23:04:43 +0000946 default:
947 return (NULL);
948 }
949 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
950 /*
951 * Ok now for each of the guys in this bucket we must look and see:
952 * - Does the remote port match. - Does there single association's
953 * addresses match this address (to). If so we update p_ep to point
954 * to this ep and return the tcb from it.
955 */
956 LIST_FOREACH(inp, ephead, sctp_hash) {
957 SCTP_INP_RLOCK(inp);
958 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
959 SCTP_INP_RUNLOCK(inp);
960 continue;
961 }
962 if (lport != inp->sctp_lport) {
963 SCTP_INP_RUNLOCK(inp);
964 continue;
965 }
966#ifdef SCTP_MVRF
967 fnd = 0;
tuexen9784e9a2011-12-18 13:04:23 +0000968 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +0000969 if (inp->m_vrf_ids[i] == vrf_id) {
970 fnd = 1;
971 break;
972 }
973 }
974 if (fnd == 0) {
975 SCTP_INP_RUNLOCK(inp);
976 continue;
977 }
978#else
979 if (inp->def_vrf_id != vrf_id) {
980 SCTP_INP_RUNLOCK(inp);
981 continue;
982 }
983#endif
984 /* check to see if the ep has one of the addresses */
985 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
986 /* We are NOT bound all, so look further */
987 int match = 0;
988
989 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
990
991 if (laddr->ifa == NULL) {
992 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
993 continue;
994 }
995 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
996 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
997 continue;
998 }
999 if (laddr->ifa->address.sa.sa_family ==
1000 to->sa_family) {
1001 /* see if it matches */
1002
1003#ifdef INET
1004 if (from->sa_family == AF_INET) {
1005 struct sockaddr_in *intf_addr, *sin;
1006
1007 intf_addr = &laddr->ifa->address.sin;
1008 sin = (struct sockaddr_in *)to;
1009 if (sin->sin_addr.s_addr ==
1010 intf_addr->sin_addr.s_addr) {
1011 match = 1;
1012 break;
1013 }
1014 }
1015#endif
1016#ifdef INET6
1017 if (from->sa_family == AF_INET6) {
1018 struct sockaddr_in6 *intf_addr6;
1019 struct sockaddr_in6 *sin6;
1020
1021 sin6 = (struct sockaddr_in6 *)
1022 to;
1023 intf_addr6 = &laddr->ifa->address.sin6;
1024
1025 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1026 intf_addr6)) {
1027 match = 1;
1028 break;
1029 }
1030 }
1031#endif
tuexen5d9b9742012-07-11 21:52:01 +00001032#if defined(__Userspace__)
1033 if (from->sa_family == AF_CONN) {
1034 struct sockaddr_conn *intf_addr, *sconn;
1035
1036 intf_addr = &laddr->ifa->address.sconn;
1037 sconn = (struct sockaddr_conn *)to;
1038 if (sconn->sconn_addr ==
1039 intf_addr->sconn_addr) {
1040 match = 1;
1041 break;
1042 }
1043 }
1044#endif
tuexendd729232011-11-01 23:04:43 +00001045 }
1046 }
1047 if (match == 0) {
1048 /* This endpoint does not have this address */
1049 SCTP_INP_RUNLOCK(inp);
1050 continue;
1051 }
1052 }
1053 /*
1054 * Ok if we hit here the ep has the address, does it hold
1055 * the tcb?
1056 */
1057
1058 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1059 if (stcb == NULL) {
1060 SCTP_INP_RUNLOCK(inp);
1061 continue;
1062 }
1063 SCTP_TCB_LOCK(stcb);
1064 if (stcb->rport != rport) {
1065 /* remote port does not match. */
tuexen09631162012-04-18 11:23:02 +00001066 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001067 SCTP_INP_RUNLOCK(inp);
1068 continue;
1069 }
1070 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001071 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001072 SCTP_INP_RUNLOCK(inp);
1073 continue;
1074 }
1075 /* Does this TCB have a matching address? */
1076 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1077
1078 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1079 /* not the same family, can't be a match */
1080 continue;
1081 }
1082 switch (from->sa_family) {
1083#ifdef INET
1084 case AF_INET:
1085 {
1086 struct sockaddr_in *sin, *rsin;
1087
1088 sin = (struct sockaddr_in *)&net->ro._l_addr;
1089 rsin = (struct sockaddr_in *)from;
1090 if (sin->sin_addr.s_addr ==
1091 rsin->sin_addr.s_addr) {
1092 /* found it */
1093 if (netp != NULL) {
1094 *netp = net;
1095 }
1096 /* Update the endpoint pointer */
1097 *inp_p = inp;
1098 SCTP_INP_RUNLOCK(inp);
1099 return (stcb);
1100 }
1101 break;
1102 }
1103#endif
1104#ifdef INET6
1105 case AF_INET6:
1106 {
1107 struct sockaddr_in6 *sin6, *rsin6;
1108
1109 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1110 rsin6 = (struct sockaddr_in6 *)from;
1111 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1112 rsin6)) {
1113 /* found it */
1114 if (netp != NULL) {
1115 *netp = net;
1116 }
1117 /* Update the endpoint pointer */
1118 *inp_p = inp;
1119 SCTP_INP_RUNLOCK(inp);
1120 return (stcb);
1121 }
1122 break;
1123 }
1124#endif
tuexen5d9b9742012-07-11 21:52:01 +00001125#if defined(__Userspace__)
1126 case AF_CONN:
1127 {
1128 struct sockaddr_conn *sconn, *rsconn;
1129
1130 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1131 rsconn = (struct sockaddr_conn *)from;
1132 if (sconn->sconn_addr == rsconn->sconn_addr) {
1133 /* found it */
1134 if (netp != NULL) {
1135 *netp = net;
1136 }
1137 /* Update the endpoint pointer */
1138 *inp_p = inp;
1139 SCTP_INP_RUNLOCK(inp);
1140 return (stcb);
1141 }
1142 break;
1143 }
1144#endif
tuexendd729232011-11-01 23:04:43 +00001145 default:
1146 /* TSNH */
1147 break;
1148 }
1149 }
1150 SCTP_TCB_UNLOCK(stcb);
1151 SCTP_INP_RUNLOCK(inp);
1152 }
1153 return (NULL);
1154}
1155
1156static int
1157sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
1158{
1159 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1160 int ipv4_addr_legal, ipv6_addr_legal;
1161 struct sctp_vrf *vrf;
1162 struct sctp_ifn *sctp_ifn;
1163 struct sctp_ifa *sctp_ifa;
1164
1165 loopback_scope = stcb->asoc.loopback_scope;
1166 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1167 local_scope = stcb->asoc.local_scope;
1168 site_scope = stcb->asoc.site_scope;
1169 ipv4_addr_legal = ipv6_addr_legal = 0;
1170 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1171 ipv6_addr_legal = 1;
1172 if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) {
1173 ipv4_addr_legal = 1;
1174 }
1175 } else {
1176 ipv4_addr_legal = 1;
1177 }
1178
1179 SCTP_IPI_ADDR_RLOCK();
1180 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
1181 if (vrf == NULL) {
1182 /* no vrf, no addresses */
1183 SCTP_IPI_ADDR_RUNLOCK();
1184 return (0);
1185 }
1186
1187 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
tuexencc2b1422012-07-10 21:50:32 +00001188#if defined(__Userspace__)
1189 if (to->sa_family == AF_CONN) {
1190 SCTP_IPI_ADDR_RUNLOCK();
1191 return (1);
1192 }
1193#endif
tuexendd729232011-11-01 23:04:43 +00001194 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1195 if ((loopback_scope == 0) &&
1196 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1197 continue;
1198 }
1199 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1200 if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
1201 (!sctp_is_addr_pending(stcb, sctp_ifa))) {
1202 /* We allow pending addresses, where we
1203 * have sent an asconf-add to be considered
1204 * valid.
1205 */
1206 continue;
1207 }
1208 switch (sctp_ifa->address.sa.sa_family) {
1209#ifdef INET
1210 case AF_INET:
1211 if (ipv4_addr_legal) {
1212 struct sockaddr_in *sin, *rsin;
1213
1214 sin = &sctp_ifa->address.sin;
1215 rsin = (struct sockaddr_in *)to;
1216 if ((ipv4_local_scope == 0) &&
1217 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1218 continue;
1219 }
1220 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1221 SCTP_IPI_ADDR_RUNLOCK();
1222 return (1);
1223 }
1224 }
1225 break;
1226#endif
1227#ifdef INET6
1228 case AF_INET6:
1229 if (ipv6_addr_legal) {
1230 struct sockaddr_in6 *sin6, *rsin6;
1231#if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
1232 struct sockaddr_in6 lsa6;
1233#endif
1234 sin6 = &sctp_ifa->address.sin6;
1235 rsin6 = (struct sockaddr_in6 *)to;
tuexendd729232011-11-01 23:04:43 +00001236 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1237 if (local_scope == 0)
1238 continue;
1239#if defined(SCTP_EMBEDDED_V6_SCOPE)
1240 if (sin6->sin6_scope_id == 0) {
1241#ifdef SCTP_KAME
1242 if (sa6_recoverscope(sin6) != 0)
1243 continue;
1244#else
1245 lsa6 = *sin6;
1246 if (in6_recoverscope(&lsa6,
1247 &lsa6.sin6_addr,
1248 NULL))
1249 continue;
1250 sin6 = &lsa6;
1251#endif /* SCTP_KAME */
1252 }
1253#endif /* SCTP_EMBEDDED_V6_SCOPE */
1254 }
1255 if ((site_scope == 0) &&
1256 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1257 continue;
1258 }
tuexendd729232011-11-01 23:04:43 +00001259 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1260 SCTP_IPI_ADDR_RUNLOCK();
1261 return (1);
1262 }
1263 }
1264 break;
1265#endif
1266 default:
1267 /* TSNH */
1268 break;
1269 }
1270 }
1271 }
1272 } else {
1273 struct sctp_laddr *laddr;
1274
1275 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1276 if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
tuexen09631162012-04-18 11:23:02 +00001277 (!sctp_is_addr_pending(stcb, laddr->ifa))) {
tuexendd729232011-11-01 23:04:43 +00001278 /* We allow pending addresses, where we
1279 * have sent an asconf-add to be considered
1280 * valid.
1281 */
1282 continue;
1283 }
1284 if (laddr->ifa->address.sa.sa_family != to->sa_family) {
1285 continue;
1286 }
1287 switch (to->sa_family) {
1288#ifdef INET
1289 case AF_INET:
1290 {
1291 struct sockaddr_in *sin, *rsin;
1292
1293 sin = (struct sockaddr_in *)&laddr->ifa->address.sin;
1294 rsin = (struct sockaddr_in *)to;
1295 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1296 SCTP_IPI_ADDR_RUNLOCK();
1297 return (1);
1298 }
1299 break;
1300 }
1301#endif
1302#ifdef INET6
1303 case AF_INET6:
1304 {
1305 struct sockaddr_in6 *sin6, *rsin6;
1306
1307 sin6 = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1308 rsin6 = (struct sockaddr_in6 *)to;
1309 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1310 SCTP_IPI_ADDR_RUNLOCK();
1311 return (1);
1312 }
1313 break;
1314 }
tuexen09631162012-04-18 11:23:02 +00001315
tuexendd729232011-11-01 23:04:43 +00001316#endif
tuexencc2b1422012-07-10 21:50:32 +00001317#if defined(__Userspace__)
1318 case AF_CONN:
1319 {
1320 struct sockaddr_conn *sconn, *rsconn;
1321
1322 sconn = (struct sockaddr_conn *)&laddr->ifa->address.sconn;
1323 rsconn = (struct sockaddr_conn *)to;
1324 if (sconn->sconn_addr == rsconn->sconn_addr) {
1325 SCTP_IPI_ADDR_RUNLOCK();
1326 return (1);
1327 }
1328 break;
1329 }
1330#endif
tuexendd729232011-11-01 23:04:43 +00001331 default:
1332 /* TSNH */
1333 break;
1334 }
1335
1336 }
1337 }
1338 SCTP_IPI_ADDR_RUNLOCK();
1339 return (0);
1340}
1341
1342/*
1343 * rules for use
1344 *
1345 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1346 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1347 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1348 * NULL.
1349 */
1350
1351struct sctp_tcb *
1352sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1353 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1354{
1355 struct sctpasochead *head;
1356 struct sctp_inpcb *inp;
tuexen63fc0bb2011-12-27 12:24:52 +00001357 struct sctp_tcb *stcb = NULL;
tuexendd729232011-11-01 23:04:43 +00001358 struct sctp_nets *net;
1359 uint16_t rport;
1360
1361 inp = *inp_p;
tuexen5d9b9742012-07-11 21:52:01 +00001362 switch (remote->sa_family) {
1363#ifdef INET
1364 case AF_INET:
tuexendd729232011-11-01 23:04:43 +00001365 rport = (((struct sockaddr_in *)remote)->sin_port);
tuexen5d9b9742012-07-11 21:52:01 +00001366 break;
1367#endif
1368#ifdef INET6
1369 case AF_INET6:
tuexendd729232011-11-01 23:04:43 +00001370 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
tuexen5d9b9742012-07-11 21:52:01 +00001371 break;
1372#endif
1373#if defined(__Userspace__)
1374 case AF_CONN:
1375 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1376 break;
1377#endif
1378 default:
tuexendd729232011-11-01 23:04:43 +00001379 return (NULL);
1380 }
1381 if (locked_tcb) {
1382 /*
1383 * UN-lock so we can do proper locking here this occurs when
1384 * called from load_addresses_from_init.
1385 */
1386 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1387 SCTP_TCB_UNLOCK(locked_tcb);
1388 }
1389 SCTP_INP_INFO_RLOCK();
1390 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1391 /*-
1392 * Now either this guy is our listener or it's the
1393 * connector. If it is the one that issued the connect, then
1394 * it's only chance is to be the first TCB in the list. If
1395 * it is the acceptor, then do the special_lookup to hash
1396 * and find the real inp.
1397 */
1398 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
1399 /* to is peer addr, from is my addr */
1400#ifndef SCTP_MVRF
1401 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1402 netp, inp->def_vrf_id);
1403 if ((stcb != NULL) && (locked_tcb == NULL)) {
1404 /* we have a locked tcb, lower refcount */
1405 SCTP_INP_DECR_REF(inp);
1406 }
1407 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1408 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1409 SCTP_TCB_LOCK(locked_tcb);
1410 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1411 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1412 }
1413#else
1414 /*-
1415 * MVRF is tricky, we must look in every VRF
1416 * the endpoint has.
1417 */
1418 {
1419 int i;
1420
tuexen63fc0bb2011-12-27 12:24:52 +00001421 for (i = 0; i<inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00001422 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1423 netp, inp->m_vrf_ids[i]);
1424 if ((stcb != NULL) && (locked_tcb == NULL)) {
1425 /* we have a locked tcb, lower refcount */
1426 SCTP_INP_DECR_REF(inp);
1427 break;
1428 }
1429 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1430 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1431 SCTP_TCB_LOCK(locked_tcb);
1432 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1433 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1434 break;
1435 }
1436 }
1437 }
1438#endif
1439 SCTP_INP_INFO_RUNLOCK();
1440 return (stcb);
1441 } else {
1442 SCTP_INP_WLOCK(inp);
1443 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1444 goto null_return;
1445 }
1446 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1447 if (stcb == NULL) {
1448 goto null_return;
1449 }
1450 SCTP_TCB_LOCK(stcb);
1451
1452 if (stcb->rport != rport) {
1453 /* remote port does not match. */
1454 SCTP_TCB_UNLOCK(stcb);
1455 goto null_return;
1456 }
1457 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
tuexen09631162012-04-18 11:23:02 +00001458 SCTP_TCB_UNLOCK(stcb);
tuexendd729232011-11-01 23:04:43 +00001459 goto null_return;
1460 }
1461 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1462 SCTP_TCB_UNLOCK(stcb);
1463 goto null_return;
1464 }
1465 /* now look at the list of remote addresses */
1466 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1467#ifdef INVARIANTS
1468 if (net == (TAILQ_NEXT(net, sctp_next))) {
1469 panic("Corrupt net list");
1470 }
1471#endif
1472 if (net->ro._l_addr.sa.sa_family !=
1473 remote->sa_family) {
1474 /* not the same family */
1475 continue;
1476 }
1477 switch (remote->sa_family) {
1478#ifdef INET
1479 case AF_INET:
1480 {
1481 struct sockaddr_in *sin, *rsin;
1482
1483 sin = (struct sockaddr_in *)
1484 &net->ro._l_addr;
1485 rsin = (struct sockaddr_in *)remote;
1486 if (sin->sin_addr.s_addr ==
1487 rsin->sin_addr.s_addr) {
1488 /* found it */
1489 if (netp != NULL) {
1490 *netp = net;
1491 }
1492 if (locked_tcb == NULL) {
1493 SCTP_INP_DECR_REF(inp);
1494 } else if (locked_tcb != stcb) {
1495 SCTP_TCB_LOCK(locked_tcb);
1496 }
1497 if (locked_tcb) {
1498 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1499 }
1500
1501 SCTP_INP_WUNLOCK(inp);
1502 SCTP_INP_INFO_RUNLOCK();
1503 return (stcb);
1504 }
1505 break;
1506 }
1507#endif
1508#ifdef INET6
1509 case AF_INET6:
1510 {
1511 struct sockaddr_in6 *sin6, *rsin6;
1512
1513 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1514 rsin6 = (struct sockaddr_in6 *)remote;
1515 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1516 rsin6)) {
1517 /* found it */
1518 if (netp != NULL) {
1519 *netp = net;
1520 }
1521 if (locked_tcb == NULL) {
1522 SCTP_INP_DECR_REF(inp);
1523 } else if (locked_tcb != stcb) {
1524 SCTP_TCB_LOCK(locked_tcb);
1525 }
1526 if (locked_tcb) {
1527 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1528 }
1529 SCTP_INP_WUNLOCK(inp);
1530 SCTP_INP_INFO_RUNLOCK();
1531 return (stcb);
1532 }
1533 break;
1534 }
1535#endif
tuexen5d9b9742012-07-11 21:52:01 +00001536#if defined(__Userspace__)
1537 case AF_CONN:
1538 {
1539 struct sockaddr_conn *sconn, *rsconn;
1540
1541 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1542 rsconn = (struct sockaddr_conn *)remote;
1543 if (sconn->sconn_addr == rsconn->sconn_addr) {
1544 /* found it */
1545 if (netp != NULL) {
1546 *netp = net;
1547 }
1548 if (locked_tcb == NULL) {
1549 SCTP_INP_DECR_REF(inp);
1550 } else if (locked_tcb != stcb) {
1551 SCTP_TCB_LOCK(locked_tcb);
1552 }
1553 if (locked_tcb) {
1554 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1555 }
1556 SCTP_INP_WUNLOCK(inp);
1557 SCTP_INP_INFO_RUNLOCK();
1558 return (stcb);
1559 }
1560 break;
1561 }
1562#endif
tuexendd729232011-11-01 23:04:43 +00001563 default:
1564 /* TSNH */
1565 break;
1566 }
1567 }
1568 SCTP_TCB_UNLOCK(stcb);
1569 }
1570 } else {
1571 SCTP_INP_WLOCK(inp);
1572 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1573 goto null_return;
1574 }
1575 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1576 inp->sctp_hashmark)];
1577 if (head == NULL) {
1578 goto null_return;
1579 }
1580 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1581 if (stcb->rport != rport) {
1582 /* remote port does not match */
1583 continue;
1584 }
1585 SCTP_TCB_LOCK(stcb);
1586 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1587 SCTP_TCB_UNLOCK(stcb);
1588 continue;
1589 }
1590 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1591 SCTP_TCB_UNLOCK(stcb);
1592 continue;
1593 }
1594 /* now look at the list of remote addresses */
1595 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1596#ifdef INVARIANTS
1597 if (net == (TAILQ_NEXT(net, sctp_next))) {
1598 panic("Corrupt net list");
1599 }
1600#endif
1601 if (net->ro._l_addr.sa.sa_family !=
1602 remote->sa_family) {
1603 /* not the same family */
1604 continue;
1605 }
1606 switch (remote->sa_family) {
1607#ifdef INET
1608 case AF_INET:
1609 {
1610 struct sockaddr_in *sin, *rsin;
1611
1612 sin = (struct sockaddr_in *)
1613 &net->ro._l_addr;
1614 rsin = (struct sockaddr_in *)remote;
1615 if (sin->sin_addr.s_addr ==
1616 rsin->sin_addr.s_addr) {
1617 /* found it */
1618 if (netp != NULL) {
1619 *netp = net;
1620 }
1621 if (locked_tcb == NULL) {
1622 SCTP_INP_DECR_REF(inp);
1623 } else if (locked_tcb != stcb) {
1624 SCTP_TCB_LOCK(locked_tcb);
1625 }
1626 if (locked_tcb) {
1627 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1628 }
1629 SCTP_INP_WUNLOCK(inp);
1630 SCTP_INP_INFO_RUNLOCK();
1631 return (stcb);
1632 }
1633 break;
1634 }
1635#endif
1636#ifdef INET6
1637 case AF_INET6:
1638 {
1639 struct sockaddr_in6 *sin6, *rsin6;
1640
1641 sin6 = (struct sockaddr_in6 *)
1642 &net->ro._l_addr;
1643 rsin6 = (struct sockaddr_in6 *)remote;
1644 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1645 rsin6)) {
1646 /* found it */
1647 if (netp != NULL) {
1648 *netp = net;
1649 }
1650 if (locked_tcb == NULL) {
1651 SCTP_INP_DECR_REF(inp);
1652 } else if (locked_tcb != stcb) {
1653 SCTP_TCB_LOCK(locked_tcb);
1654 }
1655 if (locked_tcb) {
1656 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1657 }
1658 SCTP_INP_WUNLOCK(inp);
1659 SCTP_INP_INFO_RUNLOCK();
1660 return (stcb);
1661 }
1662 break;
1663 }
1664#endif
tuexen5d9b9742012-07-11 21:52:01 +00001665#if defined(__Userspace__)
1666 case AF_CONN:
1667 {
1668 struct sockaddr_conn *sconn, *rsconn;
1669
1670 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1671 rsconn = (struct sockaddr_conn *)remote;
1672 if (sconn->sconn_addr == rsconn->sconn_addr) {
1673 /* found it */
1674 if (netp != NULL) {
1675 *netp = net;
1676 }
1677 if (locked_tcb == NULL) {
1678 SCTP_INP_DECR_REF(inp);
1679 } else if (locked_tcb != stcb) {
1680 SCTP_TCB_LOCK(locked_tcb);
1681 }
1682 if (locked_tcb) {
1683 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1684 }
1685 SCTP_INP_WUNLOCK(inp);
1686 SCTP_INP_INFO_RUNLOCK();
1687 return (stcb);
1688 }
1689 break;
1690 }
1691#endif
tuexendd729232011-11-01 23:04:43 +00001692 default:
1693 /* TSNH */
1694 break;
1695 }
1696 }
1697 SCTP_TCB_UNLOCK(stcb);
1698 }
1699 }
1700null_return:
1701 /* clean up for returning null */
1702 if (locked_tcb) {
1703 SCTP_TCB_LOCK(locked_tcb);
1704 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1705 }
1706 SCTP_INP_WUNLOCK(inp);
1707 SCTP_INP_INFO_RUNLOCK();
1708 /* not found */
1709 return (NULL);
1710}
1711
1712/*
1713 * Find an association for a specific endpoint using the association id given
1714 * out in the COMM_UP notification
1715 */
1716
1717struct sctp_tcb *
1718sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1719{
1720 /*
1721 * Use my the assoc_id to find a endpoint
1722 */
1723 struct sctpasochead *head;
1724 struct sctp_tcb *stcb;
1725 uint32_t id;
1726 if (inp == NULL) {
1727 SCTP_PRINTF("TSNH ep_associd\n");
1728 return (NULL);
1729 }
1730 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1731 SCTP_PRINTF("TSNH ep_associd0\n");
1732 return (NULL);
1733 }
1734 id = (uint32_t)asoc_id;
1735 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1736 if (head == NULL) {
1737 /* invalid id TSNH */
1738 SCTP_PRINTF("TSNH ep_associd1\n");
1739 return (NULL);
1740 }
1741 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1742 if (stcb->asoc.assoc_id == id) {
1743 if (inp != stcb->sctp_ep) {
1744 /*
1745 * some other guy has the same id active (id
1746 * collision ??).
1747 */
1748 SCTP_PRINTF("TSNH ep_associd2\n");
1749 continue;
1750 }
1751 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1752 continue;
1753 }
1754 if (want_lock) {
1755 SCTP_TCB_LOCK(stcb);
1756 }
1757 return (stcb);
1758 }
1759 }
1760 return (NULL);
1761}
1762
1763
1764struct sctp_tcb *
1765sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1766{
1767 struct sctp_tcb *stcb;
1768 SCTP_INP_RLOCK(inp);
1769 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1770 SCTP_INP_RUNLOCK(inp);
1771 return (stcb);
1772}
1773
1774
1775static struct sctp_inpcb *
1776sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1777 uint16_t lport, uint32_t vrf_id)
1778{
1779 struct sctp_inpcb *inp;
1780 struct sctp_laddr *laddr;
1781#ifdef INET
1782 struct sockaddr_in *sin;
1783#endif
1784#ifdef INET6
1785 struct sockaddr_in6 *sin6;
1786 struct sockaddr_in6 *intf_addr6;
1787#endif
tuexen09a676e2012-07-08 21:03:18 +00001788#if defined(__Userspace__)
1789 struct sockaddr_conn *sconn;
1790#endif
tuexendd729232011-11-01 23:04:43 +00001791
1792#ifdef SCTP_MVRF
1793 int i;
1794#endif
1795 int fnd;
1796 /*
1797 * Endpoint probe expects that the INP_INFO is locked.
1798 */
1799#ifdef INET
1800 sin = NULL;
1801#endif
1802#ifdef INET6
1803 sin6 = NULL;
1804#endif
1805 switch (nam->sa_family) {
1806#ifdef INET
1807 case AF_INET:
1808 sin = (struct sockaddr_in *)nam;
1809 break;
1810#endif
1811#ifdef INET6
1812 case AF_INET6:
1813 sin6 = (struct sockaddr_in6 *)nam;
1814 break;
1815#endif
tuexen09a676e2012-07-08 21:03:18 +00001816#if defined(__Userspace__)
1817 case AF_CONN:
1818 sconn = (struct sockaddr_conn *)nam;
1819 break;
1820#endif
tuexendd729232011-11-01 23:04:43 +00001821 default:
1822 /* unsupported family */
1823 return (NULL);
1824 }
1825
1826 if (head == NULL)
1827 return (NULL);
tuexen09631162012-04-18 11:23:02 +00001828
tuexendd729232011-11-01 23:04:43 +00001829 LIST_FOREACH(inp, head, sctp_hash) {
1830 SCTP_INP_RLOCK(inp);
1831 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1832 SCTP_INP_RUNLOCK(inp);
1833 continue;
1834 }
1835 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1836 (inp->sctp_lport == lport)) {
1837 /* got it */
1838#ifdef INET
1839 if ((nam->sa_family == AF_INET) &&
1840 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1841 SCTP_IPV6_V6ONLY(inp)) {
1842 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1843 SCTP_INP_RUNLOCK(inp);
1844 continue;
1845 }
1846#endif
1847#ifdef INET6
1848 /* A V6 address and the endpoint is NOT bound V6 */
1849 if (nam->sa_family == AF_INET6 &&
1850 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1851 SCTP_INP_RUNLOCK(inp);
1852 continue;
1853 }
1854#endif
1855 /* does a VRF id match? */
1856 fnd = 0;
1857#ifdef SCTP_MVRF
1858 for (i = 0; i < inp->num_vrfs; i++) {
1859 if (inp->m_vrf_ids[i] == vrf_id) {
1860 fnd = 1;
1861 break;
1862 }
1863 }
1864#else
1865 if (inp->def_vrf_id == vrf_id)
1866 fnd = 1;
1867#endif
1868
1869 SCTP_INP_RUNLOCK(inp);
1870 if (!fnd)
1871 continue;
1872 return (inp);
1873 }
1874 SCTP_INP_RUNLOCK(inp);
1875 }
1876 switch (nam->sa_family) {
1877#ifdef INET
1878 case AF_INET:
1879 if (sin->sin_addr.s_addr == INADDR_ANY) {
1880 /* Can't hunt for one that has no address specified */
1881 return (NULL);
1882 }
1883 break;
1884#endif
1885#ifdef INET6
1886 case AF_INET6:
tuexen63fc0bb2011-12-27 12:24:52 +00001887 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
tuexendd729232011-11-01 23:04:43 +00001888 /* Can't hunt for one that has no address specified */
1889 return (NULL);
1890 }
1891 break;
1892#endif
tuexen09a676e2012-07-08 21:03:18 +00001893#if defined(__Userspace__)
1894 case AF_CONN:
1895 if (sconn->sconn_addr == NULL) {
1896 return (NULL);
1897 }
1898 break;
1899#endif
tuexendd729232011-11-01 23:04:43 +00001900 default:
1901 break;
1902 }
1903 /*
1904 * ok, not bound to all so see if we can find a EP bound to this
1905 * address.
1906 */
1907 LIST_FOREACH(inp, head, sctp_hash) {
1908 SCTP_INP_RLOCK(inp);
1909 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1910 SCTP_INP_RUNLOCK(inp);
1911 continue;
1912 }
1913 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1914 SCTP_INP_RUNLOCK(inp);
1915 continue;
1916 }
1917 /*
1918 * Ok this could be a likely candidate, look at all of its
1919 * addresses
1920 */
1921 if (inp->sctp_lport != lport) {
1922 SCTP_INP_RUNLOCK(inp);
1923 continue;
1924 }
1925 /* does a VRF id match? */
1926 fnd = 0;
1927#ifdef SCTP_MVRF
1928 for (i = 0; i < inp->num_vrfs; i++) {
1929 if (inp->m_vrf_ids[i] == vrf_id) {
1930 fnd = 1;
1931 break;
1932 }
1933 }
1934#else
1935 if (inp->def_vrf_id == vrf_id)
1936 fnd = 1;
1937
1938#endif
1939 if (!fnd) {
1940 SCTP_INP_RUNLOCK(inp);
1941 continue;
1942 }
1943 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1944 if (laddr->ifa == NULL) {
1945 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1946 __FUNCTION__);
1947 continue;
1948 }
1949 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
t00fcxen8fca7ff2012-09-05 18:58:42 +00001950 (void *)laddr->ifa);
tuexendd729232011-11-01 23:04:43 +00001951 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1952 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1953 continue;
1954 }
1955 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1956 /* possible, see if it matches */
1957 switch (nam->sa_family) {
1958#ifdef INET
1959 case AF_INET:
tuexen63fc0bb2011-12-27 12:24:52 +00001960#if defined(__APPLE__)
1961 if (sin == NULL) {
1962 /* TSNH */
1963 break;
1964 }
1965#endif
tuexendd729232011-11-01 23:04:43 +00001966 if (sin->sin_addr.s_addr ==
1967 laddr->ifa->address.sin.sin_addr.s_addr) {
1968 SCTP_INP_RUNLOCK(inp);
1969 return (inp);
1970 }
1971 break;
1972#endif
1973#ifdef INET6
1974 case AF_INET6:
1975 intf_addr6 = &laddr->ifa->address.sin6;
1976 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1977 intf_addr6)) {
1978 SCTP_INP_RUNLOCK(inp);
1979 return (inp);
1980 }
1981 break;
1982#endif
tuexen09a676e2012-07-08 21:03:18 +00001983#if defined(__Userspace__)
1984 case AF_CONN:
1985 if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
1986 SCTP_INP_RUNLOCK(inp);
1987 return (inp);
1988 }
1989 break;
1990#endif
tuexendd729232011-11-01 23:04:43 +00001991 }
1992 }
1993 }
1994 SCTP_INP_RUNLOCK(inp);
1995 }
1996 return (NULL);
1997}
1998
1999
2000static struct sctp_inpcb *
2001sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
2002{
2003 struct sctppcbhead *head;
2004 struct sctp_inpcb *t_inp;
2005#ifdef SCTP_MVRF
2006 int i;
2007#endif
2008 int fnd;
2009
2010 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2011 SCTP_BASE_INFO(hashmark))];
2012 LIST_FOREACH(t_inp, head, sctp_hash) {
2013 if (t_inp->sctp_lport != lport) {
2014 continue;
2015 }
2016 /* is it in the VRF in question */
2017 fnd = 0;
2018#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00002019 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00002020 if (t_inp->m_vrf_ids[i] == vrf_id) {
2021 fnd = 1;
2022 break;
2023 }
2024 }
2025#else
2026 if (t_inp->def_vrf_id == vrf_id)
2027 fnd = 1;
2028#endif
2029 if (!fnd)
2030 continue;
2031
2032 /* This one is in use. */
2033 /* check the v6/v4 binding issue */
2034 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2035 SCTP_IPV6_V6ONLY(t_inp)) {
2036 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2037 /* collision in V6 space */
2038 return (t_inp);
2039 } else {
2040 /* inp is BOUND_V4 no conflict */
2041 continue;
2042 }
2043 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2044 /* t_inp is bound v4 and v6, conflict always */
2045 return (t_inp);
2046 } else {
2047 /* t_inp is bound only V4 */
2048 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2049 SCTP_IPV6_V6ONLY(inp)) {
2050 /* no conflict */
2051 continue;
2052 }
2053 /* else fall through to conflict */
2054 }
2055 return (t_inp);
2056 }
2057 return (NULL);
2058}
2059
2060
2061int
2062sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
2063{
2064 /* For 1-2-1 with port reuse */
2065 struct sctppcbhead *head;
2066 struct sctp_inpcb *tinp;
2067
2068 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
2069 /* only works with port reuse on */
tuexen63fc0bb2011-12-27 12:24:52 +00002070 return (-1);
tuexendd729232011-11-01 23:04:43 +00002071 }
2072 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
2073 return (0);
2074 }
2075 SCTP_INP_RUNLOCK(inp);
2076 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
2077 SCTP_BASE_INFO(hashmark))];
2078 /* Kick out all non-listeners to the TCP hash */
2079 LIST_FOREACH(tinp, head, sctp_hash) {
2080 if (tinp->sctp_lport != inp->sctp_lport) {
2081 continue;
2082 }
2083 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2084 continue;
2085 }
2086 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2087 continue;
2088 }
2089 if (tinp->sctp_socket->so_qlimit) {
2090 continue;
2091 }
2092 SCTP_INP_WLOCK(tinp);
2093 LIST_REMOVE(tinp, sctp_hash);
2094 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
2095 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
2096 LIST_INSERT_HEAD(head, tinp, sctp_hash);
2097 SCTP_INP_WUNLOCK(tinp);
2098 }
2099 SCTP_INP_WLOCK(inp);
2100 /* Pull from where he was */
2101 LIST_REMOVE(inp, sctp_hash);
2102 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
2103 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
2104 LIST_INSERT_HEAD(head, inp, sctp_hash);
2105 SCTP_INP_WUNLOCK(inp);
2106 SCTP_INP_RLOCK(inp);
2107 return (0);
2108}
2109
2110
2111struct sctp_inpcb *
2112sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
2113 uint32_t vrf_id)
2114{
2115 /*
2116 * First we check the hash table to see if someone has this port
2117 * bound with just the port.
2118 */
2119 struct sctp_inpcb *inp;
2120 struct sctppcbhead *head;
2121 int lport;
2122 unsigned int i;
2123#ifdef INET
2124 struct sockaddr_in *sin;
2125#endif
2126#ifdef INET6
2127 struct sockaddr_in6 *sin6;
2128#endif
tuexen09a676e2012-07-08 21:03:18 +00002129#if defined(__Userspace__)
2130 struct sockaddr_conn *sconn;
2131#endif
tuexendd729232011-11-01 23:04:43 +00002132
2133 switch (nam->sa_family) {
2134#ifdef INET
2135 case AF_INET:
2136 sin = (struct sockaddr_in *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002137 lport = sin->sin_port;
tuexendd729232011-11-01 23:04:43 +00002138 break;
2139#endif
2140#ifdef INET6
2141 case AF_INET6:
2142 sin6 = (struct sockaddr_in6 *)nam;
tuexen63fc0bb2011-12-27 12:24:52 +00002143 lport = sin6->sin6_port;
tuexendd729232011-11-01 23:04:43 +00002144 break;
2145#endif
tuexen09a676e2012-07-08 21:03:18 +00002146#if defined(__Userspace__)
2147 case AF_CONN:
2148 sconn = (struct sockaddr_conn *)nam;
2149 lport = sconn->sconn_port;
2150 break;
2151#endif
tuexendd729232011-11-01 23:04:43 +00002152 default:
2153 return (NULL);
2154 }
2155 /*
2156 * I could cheat here and just cast to one of the types but we will
2157 * do it right. It also provides the check against an Unsupported
2158 * type too.
2159 */
2160 /* Find the head of the ALLADDR chain */
2161 if (have_lock == 0) {
2162 SCTP_INP_INFO_RLOCK();
2163 }
2164 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2165 SCTP_BASE_INFO(hashmark))];
2166 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2167
2168 /*
2169 * If the TCP model exists it could be that the main listening
2170 * endpoint is gone but there still exists a connected socket for this
2171 * guy. If so we can return the first one that we find. This may NOT
2172 * be the correct one so the caller should be wary on the returned INP.
2173 * Currently the only caller that sets find_tcp_pool is in bindx where
2174 * we are verifying that a user CAN bind the address. He either
2175 * has bound it already, or someone else has, or its open to bind,
2176 * so this is good enough.
2177 */
2178 if (inp == NULL && find_tcp_pool) {
2179 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
2180 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
2181 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2182 if (inp) {
2183 break;
2184 }
2185 }
2186 }
2187 if (inp) {
2188 SCTP_INP_INCR_REF(inp);
2189 }
2190 if (have_lock == 0) {
2191 SCTP_INP_INFO_RUNLOCK();
2192 }
2193 return (inp);
2194}
2195
2196/*
2197 * Find an association for an endpoint with the pointer to whom you want to
2198 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
2199 * need to change the *to to some other struct like a mbuf...
2200 */
2201struct sctp_tcb *
tuexen664967a2012-06-28 16:24:36 +00002202sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
tuexendd729232011-11-01 23:04:43 +00002203 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
2204 uint32_t vrf_id)
2205{
2206 struct sctp_inpcb *inp = NULL;
2207 struct sctp_tcb *retval;
2208
2209 SCTP_INP_INFO_RLOCK();
2210 if (find_tcp_pool) {
2211 if (inp_p != NULL) {
2212 retval = sctp_tcb_special_locate(inp_p, from, to, netp,
2213 vrf_id);
2214 } else {
2215 retval = sctp_tcb_special_locate(&inp, from, to, netp,
2216 vrf_id);
2217 }
2218 if (retval != NULL) {
2219 SCTP_INP_INFO_RUNLOCK();
2220 return (retval);
2221 }
2222 }
2223 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
2224 if (inp_p != NULL) {
2225 *inp_p = inp;
2226 }
2227 SCTP_INP_INFO_RUNLOCK();
2228
2229 if (inp == NULL) {
2230 return (NULL);
2231 }
2232 /*
2233 * ok, we have an endpoint, now lets find the assoc for it (if any)
2234 * we now place the source address or from in the to of the find
2235 * endpoint call. Since in reality this chain is used from the
2236 * inbound packet side.
2237 */
2238 if (inp_p != NULL) {
2239 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to,
2240 NULL);
2241 } else {
2242 retval = sctp_findassociation_ep_addr(&inp, from, netp, to,
2243 NULL);
2244 }
2245 return retval;
2246}
2247
2248
2249/*
2250 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
2251 * find all addresses that the sender has specified in any address list. Each
2252 * address will be used to lookup the TCB and see if one exits.
2253 */
2254static struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002255sctp_findassociation_special_addr(struct mbuf *m, int offset,
tuexendd729232011-11-01 23:04:43 +00002256 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
tuexen664967a2012-06-28 16:24:36 +00002257 struct sockaddr *dst)
tuexendd729232011-11-01 23:04:43 +00002258{
2259 struct sctp_paramhdr *phdr, parm_buf;
2260 struct sctp_tcb *retval;
2261 uint32_t ptype, plen;
2262#ifdef INET
2263 struct sockaddr_in sin4;
2264#endif
2265#ifdef INET6
2266 struct sockaddr_in6 sin6;
2267#endif
2268
2269#ifdef INET
2270 memset(&sin4, 0, sizeof(sin4));
t00fcxen8fbaee32012-09-04 16:41:39 +00002271#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002272 sin4.sin_len = sizeof(sin4);
2273#endif
2274 sin4.sin_family = AF_INET;
2275 sin4.sin_port = sh->src_port;
2276#endif
2277#ifdef INET6
2278 memset(&sin6, 0, sizeof(sin6));
t00fcxen8fbaee32012-09-04 16:41:39 +00002279#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002280 sin6.sin6_len = sizeof(sin6);
2281#endif
2282 sin6.sin6_family = AF_INET6;
2283 sin6.sin6_port = sh->src_port;
2284#endif
2285
2286 retval = NULL;
2287 offset += sizeof(struct sctp_init_chunk);
2288
2289 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
2290 while (phdr != NULL) {
2291 /* now we must see if we want the parameter */
2292 ptype = ntohs(phdr->param_type);
2293 plen = ntohs(phdr->param_length);
2294 if (plen == 0) {
2295 break;
2296 }
2297#ifdef INET
2298 if (ptype == SCTP_IPV4_ADDRESS &&
2299 plen == sizeof(struct sctp_ipv4addr_param)) {
2300 /* Get the rest of the address */
2301 struct sctp_ipv4addr_param ip4_parm, *p4;
2302
2303 phdr = sctp_get_next_param(m, offset,
2304 (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
2305 if (phdr == NULL) {
2306 return (NULL);
2307 }
2308 p4 = (struct sctp_ipv4addr_param *)phdr;
2309 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2310 /* look it up */
2311 retval = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002312 (struct sockaddr *)&sin4, netp, dst, NULL);
tuexendd729232011-11-01 23:04:43 +00002313 if (retval != NULL) {
2314 return (retval);
2315 }
2316 }
2317#endif
2318#ifdef INET6
2319 if (ptype == SCTP_IPV6_ADDRESS &&
2320 plen == sizeof(struct sctp_ipv6addr_param)) {
2321 /* Get the rest of the address */
2322 struct sctp_ipv6addr_param ip6_parm, *p6;
2323
2324 phdr = sctp_get_next_param(m, offset,
2325 (struct sctp_paramhdr *)&ip6_parm, min(plen,sizeof(ip6_parm)));
2326 if (phdr == NULL) {
2327 return (NULL);
2328 }
2329 p6 = (struct sctp_ipv6addr_param *)phdr;
2330 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2331 /* look it up */
2332 retval = sctp_findassociation_ep_addr(inp_p,
tuexen664967a2012-06-28 16:24:36 +00002333 (struct sockaddr *)&sin6, netp, dst, NULL);
tuexendd729232011-11-01 23:04:43 +00002334 if (retval != NULL) {
2335 return (retval);
2336 }
2337 }
2338#endif
2339 offset += SCTP_SIZE32(plen);
2340 phdr = sctp_get_next_param(m, offset, &parm_buf,
2341 sizeof(parm_buf));
2342 }
2343 return (NULL);
2344}
2345
2346static struct sctp_tcb *
2347sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2348 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2349 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2350{
2351 /*
2352 * Use my vtag to hash. If we find it we then verify the source addr
2353 * is in the assoc. If all goes well we save a bit on rec of a
2354 * packet.
2355 */
2356 struct sctpasochead *head;
2357 struct sctp_nets *net;
2358 struct sctp_tcb *stcb;
tuexen9784e9a2011-12-18 13:04:23 +00002359#ifdef SCTP_MVRF
2360 unsigned int i;
2361#endif
tuexendd729232011-11-01 23:04:43 +00002362
tuexendd729232011-11-01 23:04:43 +00002363 SCTP_INP_INFO_RLOCK();
2364 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
2365 SCTP_BASE_INFO(hashasocmark))];
2366 if (head == NULL) {
2367 /* invalid vtag */
2368 SCTP_INP_INFO_RUNLOCK();
2369 return (NULL);
2370 }
2371 LIST_FOREACH(stcb, head, sctp_asocs) {
2372 SCTP_INP_RLOCK(stcb->sctp_ep);
2373 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2374 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2375 continue;
2376 }
tuexen9784e9a2011-12-18 13:04:23 +00002377#ifdef SCTP_MVRF
2378 for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) {
2379 if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) {
2380 break;
2381 }
2382 }
2383 if (i == stcb->sctp_ep->num_vrfs) {
2384 SCTP_INP_RUNLOCK(inp);
2385 continue;
2386 }
2387#else
2388 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2389 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2390 continue;
2391 }
2392#endif
tuexendd729232011-11-01 23:04:43 +00002393 SCTP_TCB_LOCK(stcb);
2394 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2395 if (stcb->asoc.my_vtag == vtag) {
2396 /* candidate */
2397 if (stcb->rport != rport) {
2398 SCTP_TCB_UNLOCK(stcb);
2399 continue;
2400 }
2401 if (stcb->sctp_ep->sctp_lport != lport) {
2402 SCTP_TCB_UNLOCK(stcb);
2403 continue;
2404 }
2405 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2406 SCTP_TCB_UNLOCK(stcb);
2407 continue;
2408 }
2409 /* RRS:Need toaddr check here */
2410 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2411 /* Endpoint does not own this address */
2412 SCTP_TCB_UNLOCK(stcb);
2413 continue;
2414 }
2415 if (remote_tag) {
2416 /* If we have both vtags that's all we match on */
2417 if (stcb->asoc.peer_vtag == remote_tag) {
2418 /* If both tags match we consider it conclusive
2419 * and check NO source/destination addresses
2420 */
2421 goto conclusive;
2422 }
2423 }
2424 if (skip_src_check) {
2425 conclusive:
2426 if (from) {
tuexen63fc0bb2011-12-27 12:24:52 +00002427 *netp = sctp_findnet(stcb, from);
tuexendd729232011-11-01 23:04:43 +00002428 } else {
2429 *netp = NULL; /* unknown */
2430 }
2431 if (inp_p)
2432 *inp_p = stcb->sctp_ep;
2433 SCTP_INP_INFO_RUNLOCK();
2434 return (stcb);
2435 }
2436 net = sctp_findnet(stcb, from);
2437 if (net) {
2438 /* yep its him. */
2439 *netp = net;
2440 SCTP_STAT_INCR(sctps_vtagexpress);
2441 *inp_p = stcb->sctp_ep;
2442 SCTP_INP_INFO_RUNLOCK();
2443 return (stcb);
2444 } else {
2445 /*
2446 * not him, this should only happen in rare
2447 * cases so I peg it.
2448 */
2449 SCTP_STAT_INCR(sctps_vtagbogus);
2450 }
2451 }
2452 SCTP_TCB_UNLOCK(stcb);
2453 }
2454 SCTP_INP_INFO_RUNLOCK();
2455 return (NULL);
2456}
2457
2458/*
2459 * Find an association with the pointer to the inbound IP packet. This can be
2460 * a IPv4 or IPv6 packet.
2461 */
2462struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002463sctp_findassociation_addr(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002464 struct sockaddr *src, struct sockaddr *dst,
tuexendd729232011-11-01 23:04:43 +00002465 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2466 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2467{
2468 int find_tcp_pool;
tuexendd729232011-11-01 23:04:43 +00002469 struct sctp_tcb *retval;
tuexendd729232011-11-01 23:04:43 +00002470 struct sctp_inpcb *inp;
2471
tuexendd729232011-11-01 23:04:43 +00002472 if (sh->v_tag) {
2473 /* we only go down this path if vtag is non-zero */
tuexen664967a2012-06-28 16:24:36 +00002474 retval = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
tuexendd729232011-11-01 23:04:43 +00002475 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2476 if (retval) {
2477 return (retval);
2478 }
2479 }
2480
2481 find_tcp_pool = 0;
2482 if ((ch->chunk_type != SCTP_INITIATION) &&
2483 (ch->chunk_type != SCTP_INITIATION_ACK) &&
2484 (ch->chunk_type != SCTP_COOKIE_ACK) &&
2485 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
2486 /* Other chunk types go to the tcp pool. */
2487 find_tcp_pool = 1;
2488 }
2489 if (inp_p) {
tuexen664967a2012-06-28 16:24:36 +00002490 retval = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
tuexendd729232011-11-01 23:04:43 +00002491 find_tcp_pool, vrf_id);
2492 inp = *inp_p;
2493 } else {
tuexen664967a2012-06-28 16:24:36 +00002494 retval = sctp_findassociation_addr_sa(src, dst, &inp, netp,
tuexendd729232011-11-01 23:04:43 +00002495 find_tcp_pool, vrf_id);
2496 }
t00fcxen8fca7ff2012-09-05 18:58:42 +00002497 SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", (void *)retval, (void *)inp);
tuexendd729232011-11-01 23:04:43 +00002498 if (retval == NULL && inp) {
2499 /* Found a EP but not this address */
2500 if ((ch->chunk_type == SCTP_INITIATION) ||
2501 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2502 /*-
2503 * special hook, we do NOT return linp or an
2504 * association that is linked to an existing
2505 * association that is under the TCP pool (i.e. no
2506 * listener exists). The endpoint finding routine
2507 * will always find a listener before examining the
2508 * TCP pool.
2509 */
2510 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2511 if (inp_p) {
2512 *inp_p = NULL;
2513 }
2514 return (NULL);
2515 }
tuexen9784e9a2011-12-18 13:04:23 +00002516 retval = sctp_findassociation_special_addr(m,
tuexen664967a2012-06-28 16:24:36 +00002517 offset, sh, &inp, netp, dst);
tuexendd729232011-11-01 23:04:43 +00002518 if (inp_p != NULL) {
2519 *inp_p = inp;
2520 }
2521 }
2522 }
t00fcxen8fca7ff2012-09-05 18:58:42 +00002523 SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", (void *)retval);
tuexendd729232011-11-01 23:04:43 +00002524 return (retval);
2525}
2526
2527/*
2528 * lookup an association by an ASCONF lookup address.
2529 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2530 */
2531struct sctp_tcb *
tuexen9784e9a2011-12-18 13:04:23 +00002532sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
tuexen664967a2012-06-28 16:24:36 +00002533 struct sockaddr *dst, struct sctphdr *sh,
2534 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
tuexendd729232011-11-01 23:04:43 +00002535{
2536 struct sctp_tcb *stcb;
tuexen664967a2012-06-28 16:24:36 +00002537 struct sockaddr_storage remote_store;
tuexendd729232011-11-01 23:04:43 +00002538 struct sctp_paramhdr parm_buf, *phdr;
2539 int ptype;
2540 int zero_address = 0;
2541#ifdef INET
2542 struct sockaddr_in *sin;
2543#endif
2544#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00002545 struct sockaddr_in6 *sin6;
2546#endif
2547
tuexendd729232011-11-01 23:04:43 +00002548 memset(&remote_store, 0, sizeof(remote_store));
tuexendd729232011-11-01 23:04:43 +00002549 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2550 &parm_buf, sizeof(struct sctp_paramhdr));
2551 if (phdr == NULL) {
2552 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2553 __FUNCTION__);
2554 return NULL;
2555 }
2556 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2557 /* get the correlation address */
2558 switch (ptype) {
2559#ifdef INET6
2560 case SCTP_IPV6_ADDRESS:
2561 {
2562 /* ipv6 address param */
2563 struct sctp_ipv6addr_param *p6, p6_buf;
2564
2565 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2566 return NULL;
2567 }
2568 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2569 offset + sizeof(struct sctp_asconf_chunk),
2570 &p6_buf.ph, sizeof(*p6));
2571 if (p6 == NULL) {
2572 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2573 __FUNCTION__);
2574 return (NULL);
2575 }
2576 sin6 = (struct sockaddr_in6 *)&remote_store;
2577 sin6->sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00002578#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00002579 sin6->sin6_len = sizeof(*sin6);
2580#endif
2581 sin6->sin6_port = sh->src_port;
2582 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2583 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2584 zero_address = 1;
2585 break;
2586 }
2587#endif
2588#ifdef INET
2589 case SCTP_IPV4_ADDRESS:
2590 {
2591 /* ipv4 address param */
2592 struct sctp_ipv4addr_param *p4, p4_buf;
2593
2594 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2595 return NULL;
2596 }
2597 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2598 offset + sizeof(struct sctp_asconf_chunk),
2599 &p4_buf.ph, sizeof(*p4));
2600 if (p4 == NULL) {
2601 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2602 __FUNCTION__);
2603 return (NULL);
2604 }
2605 sin = (struct sockaddr_in *)&remote_store;
2606 sin->sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00002607#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00002608 sin->sin_len = sizeof(*sin);
2609#endif
2610 sin->sin_port = sh->src_port;
2611 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2612 if (sin->sin_addr.s_addr == INADDR_ANY)
2613 zero_address = 1;
2614 break;
2615 }
2616#endif
2617 default:
2618 /* invalid address param type */
2619 return NULL;
2620 }
2621
2622 if (zero_address) {
tuexen664967a2012-06-28 16:24:36 +00002623 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
tuexendd729232011-11-01 23:04:43 +00002624 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
tuexenb53ec512012-08-05 10:55:12 +00002625 if (stcb != NULL) {
2626 SCTP_INP_DECR_REF(*inp_p);
2627 }
tuexendd729232011-11-01 23:04:43 +00002628 } else {
2629 stcb = sctp_findassociation_ep_addr(inp_p,
2630 (struct sockaddr *)&remote_store, netp,
tuexen664967a2012-06-28 16:24:36 +00002631 dst, NULL);
tuexendd729232011-11-01 23:04:43 +00002632 }
2633 return (stcb);
2634}
2635
2636
2637/*
2638 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2639 * addresses. This way if we don't get a bind we by default pick a ephemeral
2640 * port with all addresses bound.
2641 */
2642int
2643sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2644{
2645 /*
2646 * we get called when a new endpoint starts up. We need to allocate
2647 * the sctp_inpcb structure from the zone and init it. Mark it as
2648 * unbound and find a port that we can use as an ephemeral with
2649 * INADDR_ANY. If the user binds later no problem we can then add in
2650 * the specific addresses. And setup the default parameters for the
2651 * EP.
2652 */
2653 int i, error;
2654 struct sctp_inpcb *inp;
2655 struct sctp_pcb *m;
2656 struct timeval time;
2657 sctp_sharedkey_t *null_key;
2658
2659 error = 0;
2660
2661 SCTP_INP_INFO_WLOCK();
2662 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2663 if (inp == NULL) {
2664 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2665 SCTP_INP_INFO_WUNLOCK();
2666 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2667 return (ENOBUFS);
2668 }
2669 /* zap it */
2670 bzero(inp, sizeof(*inp));
2671
2672 /* bump generations */
2673#if defined(__APPLE__)
2674 inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
2675#endif
2676 /* setup socket pointers */
2677 inp->sctp_socket = so;
2678 inp->ip_inp.inp.inp_socket = so;
tuexenc0bb6122012-01-15 14:21:21 +00002679#ifdef INET6
tuexen76eb2092012-04-01 16:35:28 +00002680#if !defined(__Userspace__) && !defined(__Windows__)
tuexendd729232011-11-01 23:04:43 +00002681 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2682 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2683 }
2684#endif
tuexenc0bb6122012-01-15 14:21:21 +00002685#endif
tuexendd729232011-11-01 23:04:43 +00002686 inp->sctp_associd_counter = 1;
2687 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2688 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2689 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2690 inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable);
tuexen09631162012-04-18 11:23:02 +00002691#if defined(__Userspace__)
tuexeneda9dbb2012-05-27 11:10:08 +00002692 inp->ulp_info = NULL;
tuexen09631162012-04-18 11:23:02 +00002693 inp->recv_callback = NULL;
2694 inp->send_callback = NULL;
2695 inp->send_sb_threshold = 0;
tuexen09631162012-04-18 11:23:02 +00002696#endif
tuexendd729232011-11-01 23:04:43 +00002697 /* init the small hash table we use to track asocid <-> tcb */
2698 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2699 if (inp->sctp_asocidhash == NULL) {
2700 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2701 SCTP_INP_INFO_WUNLOCK();
2702 return (ENOBUFS);
2703 }
2704#ifdef IPSEC
2705#if !(defined(__APPLE__))
2706 {
2707 struct inpcbpolicy *pcb_sp = NULL;
2708
2709 error = ipsec_init_policy(so, &pcb_sp);
2710 /* Arrange to share the policy */
2711 inp->ip_inp.inp.inp_sp = pcb_sp;
2712 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2713 }
2714#else
2715 /* not sure what to do for openbsd here */
2716 error = 0;
2717#endif
2718 if (error != 0) {
2719 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2720 SCTP_INP_INFO_WUNLOCK();
2721 return error;
2722 }
2723#endif /* IPSEC */
2724 SCTP_INCR_EP_COUNT();
tuexendd729232011-11-01 23:04:43 +00002725 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
tuexendd729232011-11-01 23:04:43 +00002726 SCTP_INP_INFO_WUNLOCK();
2727
2728 so->so_pcb = (caddr_t)inp;
2729
tuexen5fc22842012-02-11 13:32:19 +00002730#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00002731 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
2732 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
2733#else
2734 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2735#endif
2736 /* UDP style socket */
2737 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2738 SCTP_PCB_FLAGS_UNBOUND);
2739 /* Be sure it is NON-BLOCKING IO for UDP */
2740 /* SCTP_SET_SO_NBIO(so); */
2741 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2742 /* TCP style socket */
2743 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2744 SCTP_PCB_FLAGS_UNBOUND);
2745 /* Be sure we have blocking IO by default */
2746 SCTP_CLEAR_SO_NBIO(so);
2747#if defined(__Panda__)
2748 } else if (SCTP_SO_TYPE(so) == SOCK_FASTSEQPACKET) {
2749 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2750 SCTP_PCB_FLAGS_UNBOUND);
2751 sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE);
2752 } else if (SCTP_SO_TYPE(so) == SOCK_FASTSTREAM) {
2753 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2754 SCTP_PCB_FLAGS_UNBOUND);
2755 sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE);
2756#endif
2757 } else {
2758 /*
2759 * unsupported socket type (RAW, etc)- in case we missed it
2760 * in protosw
2761 */
2762 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2763 so->so_pcb = NULL;
2764 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2765 return (EOPNOTSUPP);
2766 }
2767 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2768 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2769 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2770 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2771 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2772 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2773 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2774 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2775 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2776 }
2777 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2778 &inp->sctp_hashmark);
2779 if (inp->sctp_tcbhash == NULL) {
2780 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2781 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2782 so->so_pcb = NULL;
2783 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2784 return (ENOBUFS);
2785 }
2786#ifdef SCTP_MVRF
2787 inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
2788 SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
2789 (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
2790 if (inp->m_vrf_ids == NULL) {
2791 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2792 so->so_pcb = NULL;
2793 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2794 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2795 return (ENOBUFS);
2796 }
2797 inp->m_vrf_ids[0] = vrf_id;
2798 inp->num_vrfs = 1;
2799#endif
2800 inp->def_vrf_id = vrf_id;
2801
2802#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00002803#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +00002804 inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(mtx_grp), SCTP_BASE_INFO(mtx_attr));
2805 if (inp->ip_inp.inp.inpcb_mtx == NULL) {
t00fcxen8fca7ff2012-09-05 18:58:42 +00002806 SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so);
tuexendd729232011-11-01 23:04:43 +00002807#ifdef SCTP_MVRF
2808 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
2809#endif
2810 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2811 so->so_pcb = NULL;
2812 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2813 SCTP_UNLOCK_EXC(SCTP_BASE_INFO(ipi_ep_mtx));
2814 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
2815 return (ENOMEM);
2816 }
tuexen9a218b12012-08-04 21:17:58 +00002817#else
2818 lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(mtx_grp), SCTP_BASE_INFO(mtx_attr));
tuexendd729232011-11-01 23:04:43 +00002819#endif
2820#endif
2821 SCTP_INP_INFO_WLOCK();
2822 SCTP_INP_LOCK_INIT(inp);
2823#if defined(__FreeBSD__)
2824 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2825#endif
2826 SCTP_INP_READ_INIT(inp);
2827 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2828 /* lock the new ep */
2829 SCTP_INP_WLOCK(inp);
2830
2831 /* add it to the info area */
2832 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2833#if defined(__APPLE__)
2834 LIST_INSERT_HEAD(&SCTP_BASE_INFO(inplisthead), &inp->ip_inp.inp, inp_list);
2835#endif
2836 SCTP_INP_INFO_WUNLOCK();
2837
2838 TAILQ_INIT(&inp->read_queue);
2839 LIST_INIT(&inp->sctp_addr_list);
2840
2841 LIST_INIT(&inp->sctp_asoc_list);
2842
2843#ifdef SCTP_TRACK_FREED_ASOCS
2844 /* TEMP CODE */
2845 LIST_INIT(&inp->sctp_asoc_free_list);
2846#endif
2847 /* Init the timer structure for signature change */
2848 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2849 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2850
2851 /* now init the actual endpoint default data */
2852 m = &inp->sctp_ep;
2853
2854 /* setup the base timeout information */
2855 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
2856 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
2857 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2858 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2859 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2860 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2861 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2862 /* all max/min max are in ms */
2863 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2864 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2865 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2866 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2867 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2868
2869 m->max_open_streams_intome = MAX_SCTP_STREAMS;
2870
2871 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2872 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2873 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2874 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2875 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2876 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2877 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2878 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2879
2880 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2881 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2882 /* number of streams to pre-open on a association */
2883 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2884
2885 /* Add adaptation cookie */
2886 m->adaptation_layer_indicator = 0x504C5253;
2887
2888 /* seed random number generator */
2889 m->random_counter = 1;
2890 m->store_at = SCTP_SIGNATURE_SIZE;
2891 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2892 sctp_fill_random_store(m);
2893
2894 /* Minimum cookie size */
2895 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2896 sizeof(struct sctp_state_cookie);
2897 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2898
2899 /* Setup the initial secret */
2900 (void)SCTP_GETTIME_TIMEVAL(&time);
2901 m->time_of_secret_change = time.tv_sec;
2902
2903 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2904 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2905 }
2906 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2907
2908 /* How long is a cookie good for ? */
2909 m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2910 /*
2911 * Initialize authentication parameters
2912 */
2913 m->local_hmacs = sctp_default_supported_hmaclist();
2914 m->local_auth_chunks = sctp_alloc_chunklist();
2915 m->default_dscp = 0;
2916#ifdef INET6
2917 m->default_flowlabel = 0;
2918#endif
tuexen9d7b2072011-11-20 16:35:17 +00002919 m->port = 0; /* encapsulation disabled by default */
tuexendd729232011-11-01 23:04:43 +00002920 sctp_auth_set_default_chunks(m->local_auth_chunks);
2921 LIST_INIT(&m->shared_keys);
2922 /* add default NULL key as key id 0 */
2923 null_key = sctp_alloc_sharedkey();
2924 sctp_insert_sharedkey(&m->shared_keys, null_key);
2925 SCTP_INP_WUNLOCK(inp);
2926#ifdef SCTP_LOG_CLOSING
2927 sctp_log_closing(inp, NULL, 12);
2928#endif
2929 return (error);
2930}
2931
2932
2933void
2934sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2935 struct sctp_tcb *stcb)
2936{
2937 struct sctp_nets *net;
2938 uint16_t lport, rport;
2939 struct sctppcbhead *head;
2940 struct sctp_laddr *laddr, *oladdr;
2941
2942 atomic_add_int(&stcb->asoc.refcnt, 1);
2943 SCTP_TCB_UNLOCK(stcb);
2944 SCTP_INP_INFO_WLOCK();
2945 SCTP_INP_WLOCK(old_inp);
2946 SCTP_INP_WLOCK(new_inp);
2947 SCTP_TCB_LOCK(stcb);
2948 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2949
2950 new_inp->sctp_ep.time_of_secret_change =
2951 old_inp->sctp_ep.time_of_secret_change;
2952 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2953 sizeof(old_inp->sctp_ep.secret_key));
2954 new_inp->sctp_ep.current_secret_number =
2955 old_inp->sctp_ep.current_secret_number;
2956 new_inp->sctp_ep.last_secret_number =
2957 old_inp->sctp_ep.last_secret_number;
2958 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2959
2960 /* make it so new data pours into the new socket */
2961 stcb->sctp_socket = new_inp->sctp_socket;
2962 stcb->sctp_ep = new_inp;
2963
2964 /* Copy the port across */
2965 lport = new_inp->sctp_lport = old_inp->sctp_lport;
2966 rport = stcb->rport;
2967 /* Pull the tcb from the old association */
2968 LIST_REMOVE(stcb, sctp_tcbhash);
2969 LIST_REMOVE(stcb, sctp_tcblist);
2970 if (stcb->asoc.in_asocid_hash) {
2971 LIST_REMOVE(stcb, sctp_tcbasocidhash);
2972 }
2973 /* Now insert the new_inp into the TCP connected hash */
2974 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2975
2976 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2977 /* Its safe to access */
2978 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2979
2980 /* Now move the tcb into the endpoint list */
2981 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2982 /*
2983 * Question, do we even need to worry about the ep-hash since we
2984 * only have one connection? Probably not :> so lets get rid of it
2985 * and not suck up any kernel memory in that.
2986 */
2987 if (stcb->asoc.in_asocid_hash) {
2988 struct sctpasochead *lhd;
2989 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2990 new_inp->hashasocidmark)];
2991 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2992 }
2993 /* Ok. Let's restart timer. */
2994 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2995 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2996 stcb, net);
2997 }
2998
2999 SCTP_INP_INFO_WUNLOCK();
3000 if (new_inp->sctp_tcbhash != NULL) {
3001 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
3002 new_inp->sctp_tcbhash = NULL;
3003 }
3004 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3005 /* Subset bound, so copy in the laddr list from the old_inp */
3006 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3007 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3008 if (laddr == NULL) {
3009 /*
3010 * Gak, what can we do? This assoc is really
3011 * HOSED. We probably should send an abort
3012 * here.
3013 */
3014 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3015 continue;
3016 }
3017 SCTP_INCR_LADDR_COUNT();
3018 bzero(laddr, sizeof(*laddr));
3019 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3020 laddr->ifa = oladdr->ifa;
3021 atomic_add_int(&laddr->ifa->refcount, 1);
3022 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3023 sctp_nxt_addr);
3024 new_inp->laddr_count++;
3025 if (oladdr == stcb->asoc.last_used_address) {
3026 stcb->asoc.last_used_address = laddr;
3027 }
3028 }
3029 }
3030 /* Now any running timers need to be adjusted
3031 * since we really don't care if they are running
3032 * or not just blast in the new_inp into all of
3033 * them.
3034 */
3035
3036 stcb->asoc.dack_timer.ep = (void *)new_inp;
3037 stcb->asoc.asconf_timer.ep = (void *)new_inp;
3038 stcb->asoc.strreset_timer.ep = (void *)new_inp;
3039 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
3040 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
3041 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
3042 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
3043 /* now what about the nets? */
3044 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3045 net->pmtu_timer.ep = (void *)new_inp;
3046 net->hb_timer.ep = (void *)new_inp;
3047 net->rxt_timer.ep = (void *)new_inp;
3048 }
3049 SCTP_INP_WUNLOCK(new_inp);
3050 SCTP_INP_WUNLOCK(old_inp);
3051}
3052
3053
3054#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
3055/*
3056 * Don't know why, but without this there is an unknown reference when
3057 * compiling NetBSD... hmm
3058 */
3059extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3060#endif
3061
3062
3063/* sctp_ifap is used to bypass normal local address validation checks */
3064int
3065#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3066sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3067 struct sctp_ifa *sctp_ifap, struct thread *p)
3068#elif defined(__Windows__)
3069sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3070 struct sctp_ifa *sctp_ifap, PKTHREAD p)
3071#else
3072sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3073 struct sctp_ifa *sctp_ifap, struct proc *p)
3074#endif
3075{
3076 /* bind a ep to a socket address */
3077 struct sctppcbhead *head;
3078 struct sctp_inpcb *inp, *inp_tmp;
3079 struct inpcb *ip_inp;
3080 int port_reuse_active = 0;
3081 int bindall;
3082#ifdef SCTP_MVRF
3083 int i;
3084#endif
3085 uint16_t lport;
3086 int error;
3087 uint32_t vrf_id;
3088
3089 lport = 0;
3090 error = 0;
3091 bindall = 1;
3092 inp = (struct sctp_inpcb *)so->so_pcb;
3093 ip_inp = (struct inpcb *)so->so_pcb;
3094#ifdef SCTP_DEBUG
3095 if (addr) {
3096 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
3097 ntohs(((struct sockaddr_in *)addr)->sin_port));
3098 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
3099 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3100 }
3101#endif
3102 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3103 /* already did a bind, subsequent binds NOT allowed ! */
3104 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3105 return (EINVAL);
3106 }
3107#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3108#ifdef INVARIANTS
3109 if (p == NULL)
3110 panic("null proc/thread");
3111#endif
3112#endif
3113 if (addr != NULL) {
3114 switch (addr->sa_family) {
3115#ifdef INET
3116 case AF_INET:
3117 {
3118 struct sockaddr_in *sin;
3119
3120 /* IPV6_V6ONLY socket? */
3121 if (SCTP_IPV6_V6ONLY(ip_inp)) {
3122 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3123 return (EINVAL);
3124 }
t00fcxena8a26f62012-09-12 19:24:54 +00003125#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003126 if (addr->sa_len != sizeof(*sin)) {
3127 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3128 return (EINVAL);
3129 }
3130#endif
3131
3132 sin = (struct sockaddr_in *)addr;
3133 lport = sin->sin_port;
3134#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
t00fcxenefb47e72012-09-09 08:16:51 +00003135 /*
3136 * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3137 * to the proper value.
3138 */
3139 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
3140 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3141 return (error);
3142 }
tuexendd729232011-11-01 23:04:43 +00003143#endif
3144 if (sin->sin_addr.s_addr != INADDR_ANY) {
3145 bindall = 0;
3146 }
3147 break;
3148 }
3149#endif
3150#ifdef INET6
3151 case AF_INET6:
3152 {
3153 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3154 struct sockaddr_in6 *sin6;
3155
3156 sin6 = (struct sockaddr_in6 *)addr;
3157
t00fcxena8a26f62012-09-12 19:24:54 +00003158#ifdef HAVE_SA_LEN
tuexendd729232011-11-01 23:04:43 +00003159 if (addr->sa_len != sizeof(*sin6)) {
3160 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3161 return (EINVAL);
3162 }
3163#endif
tuexendd729232011-11-01 23:04:43 +00003164 lport = sin6->sin6_port;
3165#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
t00fcxenefb47e72012-09-09 08:16:51 +00003166 /*
3167 * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
3168 * to the proper value.
3169 */
3170 if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
3171 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
3172 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3173 return (error);
3174 }
tuexendd729232011-11-01 23:04:43 +00003175#endif
3176 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3177 bindall = 0;
3178#ifdef SCTP_EMBEDDED_V6_SCOPE
3179 /* KAME hack: embed scopeid */
3180#if defined(SCTP_KAME)
3181 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3182 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3183 return (EINVAL);
3184 }
tuexene3851592012-06-01 08:31:45 +00003185#elif defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00003186#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
3187 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003188#else
tuexen9a218b12012-08-04 21:17:58 +00003189 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
tuexendd729232011-11-01 23:04:43 +00003190#endif
3191 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3192 return (EINVAL);
3193 }
3194#elif defined(__FreeBSD__)
3195 error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
3196 if (error != 0) {
3197 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3198 return (error);
3199 }
tuexendd729232011-11-01 23:04:43 +00003200#else
3201 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
3202 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3203 return (EINVAL);
3204 }
3205#endif
3206#endif /* SCTP_EMBEDDED_V6_SCOPE */
3207 }
3208#ifndef SCOPEDROUTING
3209 /* this must be cleared for ifa_ifwithaddr() */
3210 sin6->sin6_scope_id = 0;
3211#endif /* SCOPEDROUTING */
3212 break;
3213 }
3214#endif
tuexen09a676e2012-07-08 21:03:18 +00003215#if defined(__Userspace__)
3216 case AF_CONN:
3217 {
3218 struct sockaddr_conn *sconn;
t00fcxenefb47e72012-09-09 08:16:51 +00003219
t00fcxena8a26f62012-09-12 19:24:54 +00003220#ifdef HAVE_SA_LEN
tuexen09a676e2012-07-08 21:03:18 +00003221 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
3222 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3223 return (EINVAL);
3224 }
3225#endif
3226 sconn = (struct sockaddr_conn *)addr;
3227 lport = sconn->sconn_port;
3228 if (sconn->sconn_addr != NULL) {
3229 bindall = 0;
3230 }
3231 break;
3232 }
3233#endif
tuexendd729232011-11-01 23:04:43 +00003234 default:
3235 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
3236 return (EAFNOSUPPORT);
3237 }
3238 }
3239 SCTP_INP_INFO_WLOCK();
3240 SCTP_INP_WLOCK(inp);
3241 /* Setup a vrf_id to be the default for the non-bind-all case. */
3242 vrf_id = inp->def_vrf_id;
3243
3244 /* increase our count due to the unlock we do */
3245 SCTP_INP_INCR_REF(inp);
3246 if (lport) {
3247 /*
t00fcxenefb47e72012-09-09 08:16:51 +00003248 * Did the caller specify a port? if so we must see if an ep
tuexendd729232011-11-01 23:04:43 +00003249 * already has this one bound.
3250 */
3251 /* got to be root to get at low ports */
3252#if !defined(__Windows__)
3253 if (ntohs(lport) < IPPORT_RESERVED) {
3254 if (p && (error =
3255#ifdef __FreeBSD__
3256#if __FreeBSD_version > 602000
3257 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3258#elif __FreeBSD_version >= 500000
3259 suser_cred(p->td_ucred, 0)
3260#else
3261 suser(p)
3262#endif
3263#elif defined(__APPLE__)
3264 suser(p->p_ucred, &p->p_acflag)
3265#elif defined(__Userspace__) /* must be true to use raw socket */
3266 1
3267#else
3268 suser(p, 0)
3269#endif
3270 )) {
3271 SCTP_INP_DECR_REF(inp);
3272 SCTP_INP_WUNLOCK(inp);
3273 SCTP_INP_INFO_WUNLOCK();
3274 return (error);
3275 }
3276#if defined(__Panda__)
3277 if (!SCTP_IS_PRIVILEDGED(so)) {
3278 SCTP_INP_DECR_REF(inp);
3279 SCTP_INP_WUNLOCK(inp);
3280 SCTP_INP_INFO_WUNLOCK();
3281 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EACCES);
3282 return (EACCES);
3283 }
3284#endif
3285 }
3286#if !defined(__Panda__) && !defined(__Userspace__)
3287 if (p == NULL) {
3288 SCTP_INP_DECR_REF(inp);
3289 SCTP_INP_WUNLOCK(inp);
3290 SCTP_INP_INFO_WUNLOCK();
3291 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3292 return (error);
3293 }
3294#endif
3295#endif /* __Windows__ */
3296 SCTP_INP_WUNLOCK(inp);
3297 if (bindall) {
3298#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003299 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003300 vrf_id = inp->m_vrf_ids[i];
3301#else
3302 vrf_id = inp->def_vrf_id;
3303#endif
3304 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3305 if (inp_tmp != NULL) {
3306 /*
3307 * lock guy returned and lower count
3308 * note that we are not bound so
3309 * inp_tmp should NEVER be inp. And
3310 * it is this inp (inp_tmp) that gets
3311 * the reference bump, so we must
3312 * lower it.
3313 */
3314 SCTP_INP_DECR_REF(inp_tmp);
3315 /* unlock info */
3316 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3317 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3318 /* Ok, must be one-2-one and allowing port re-use */
3319 port_reuse_active = 1;
3320 goto continue_anyway;
3321 }
3322 SCTP_INP_DECR_REF(inp);
3323 SCTP_INP_INFO_WUNLOCK();
3324 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3325 return (EADDRINUSE);
3326 }
3327#ifdef SCTP_MVRF
3328 }
3329#endif
3330 } else {
3331 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3332 if (inp_tmp != NULL) {
3333 /*
3334 * lock guy returned and lower count note
3335 * that we are not bound so inp_tmp should
3336 * NEVER be inp. And it is this inp (inp_tmp)
3337 * that gets the reference bump, so we must
3338 * lower it.
3339 */
3340 SCTP_INP_DECR_REF(inp_tmp);
3341 /* unlock info */
3342 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3343 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3344 /* Ok, must be one-2-one and allowing port re-use */
3345 port_reuse_active = 1;
3346 goto continue_anyway;
3347 }
3348 SCTP_INP_DECR_REF(inp);
3349 SCTP_INP_INFO_WUNLOCK();
3350 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3351 return (EADDRINUSE);
3352 }
3353 }
3354 continue_anyway:
3355 SCTP_INP_WLOCK(inp);
3356 if (bindall) {
3357 /* verify that no lport is not used by a singleton */
t00fcxenefb47e72012-09-09 08:16:51 +00003358 if ((port_reuse_active == 0) &&
3359 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
tuexendd729232011-11-01 23:04:43 +00003360 /* Sorry someone already has this one bound */
3361 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3362 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3363 port_reuse_active = 1;
3364 } else {
3365 SCTP_INP_DECR_REF(inp);
3366 SCTP_INP_WUNLOCK(inp);
3367 SCTP_INP_INFO_WUNLOCK();
3368 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3369 return (EADDRINUSE);
3370 }
3371 }
3372 }
3373 } else {
3374 uint16_t first, last, candidate;
3375 uint16_t count;
3376 int done;
3377
3378#if defined(__Windows__)
3379 first = 1;
3380 last = 0xffff;
3381#else
3382#if defined(__Userspace__)
3383 /* TODO ensure uid is 0, etc... */
3384#elif defined(__FreeBSD__) || defined(__APPLE__)
3385 if (ip_inp->inp_flags & INP_HIGHPORT) {
3386 first = MODULE_GLOBAL(ipport_hifirstauto);
3387 last = MODULE_GLOBAL(ipport_hilastauto);
3388 } else if (ip_inp->inp_flags & INP_LOWPORT) {
3389 if (p && (error =
3390#ifdef __FreeBSD__
3391#if __FreeBSD_version > 602000
3392 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3393#elif __FreeBSD_version >= 500000
3394 suser_cred(p->td_ucred, 0)
3395#else
3396 suser(p)
3397#endif
3398#elif defined(__APPLE__)
3399 suser(p->p_ucred, &p->p_acflag)
3400#else
3401 suser(p, 0)
3402#endif
3403 )) {
3404 SCTP_INP_DECR_REF(inp);
3405 SCTP_INP_WUNLOCK(inp);
3406 SCTP_INP_INFO_WUNLOCK();
3407 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3408 return (error);
3409 }
3410 first = MODULE_GLOBAL(ipport_lowfirstauto);
3411 last = MODULE_GLOBAL(ipport_lowlastauto);
3412 } else {
3413#endif
3414 first = MODULE_GLOBAL(ipport_firstauto);
3415 last = MODULE_GLOBAL(ipport_lastauto);
3416#if defined(__FreeBSD__) || defined(__APPLE__)
3417 }
3418#endif
3419#endif /* __Windows__ */
3420 if (first > last) {
3421 uint16_t temp;
3422
3423 temp = first;
3424 first = last;
3425 last = temp;
3426 }
3427 count = last - first + 1; /* number of candidates */
3428 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3429
3430 done = 0;
3431 while (!done) {
3432#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003433 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003434 if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
3435 break;
3436 }
3437 }
3438 if (i == inp->num_vrfs) {
3439 done = 1;
3440 }
3441#else
3442 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3443 done = 1;
3444 }
3445#endif
3446 if (!done) {
3447 if (--count == 0) {
3448 SCTP_INP_DECR_REF(inp);
3449 SCTP_INP_WUNLOCK(inp);
3450 SCTP_INP_INFO_WUNLOCK();
3451 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3452 return (EADDRINUSE);
3453 }
3454 if (candidate == last)
3455 candidate = first;
3456 else
3457 candidate = candidate + 1;
3458 }
3459 }
3460 lport = htons(candidate);
3461 }
3462 SCTP_INP_DECR_REF(inp);
3463 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3464 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3465 /*
3466 * this really should not happen. The guy did a non-blocking
3467 * bind and then did a close at the same time.
3468 */
3469 SCTP_INP_WUNLOCK(inp);
3470 SCTP_INP_INFO_WUNLOCK();
3471 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3472 return (EINVAL);
3473 }
3474 /* ok we look clear to give out this port, so lets setup the binding */
3475 if (bindall) {
3476 /* binding to all addresses, so just set in the proper flags */
3477 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3478 /* set the automatic addr changes from kernel flag */
3479 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3480 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3481 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3482 } else {
3483 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3484 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3485 }
3486 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3487 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3488 } else {
3489 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3490 }
3491 /* set the automatic mobility_base from kernel
3492 flag (by micchie)
3493 */
3494 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3495 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3496 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003497 } else {
tuexendd729232011-11-01 23:04:43 +00003498 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3499 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3500 }
3501 /* set the automatic mobility_fasthandoff from kernel
3502 flag (by micchie)
3503 */
3504 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3505 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3506 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
t00fcxenefb47e72012-09-09 08:16:51 +00003507 } else {
tuexendd729232011-11-01 23:04:43 +00003508 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3509 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3510 }
3511 } else {
3512 /*
3513 * bind specific, make sure flags is off and add a new
3514 * address structure to the sctp_addr_list inside the ep
3515 * structure.
3516 *
3517 * We will need to allocate one and insert it at the head. The
3518 * socketopt call can just insert new addresses in there as
3519 * well. It will also have to do the embed scope kame hack
3520 * too (before adding).
3521 */
3522 struct sctp_ifa *ifa;
3523 struct sockaddr_storage store_sa;
3524
3525 memset(&store_sa, 0, sizeof(store_sa));
3526 switch (addr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00003527#ifdef INET
tuexendd729232011-11-01 23:04:43 +00003528 case AF_INET:
3529 {
3530 struct sockaddr_in *sin;
3531
3532 sin = (struct sockaddr_in *)&store_sa;
3533 memcpy(sin, addr, sizeof(struct sockaddr_in));
3534 sin->sin_port = 0;
3535 break;
3536 }
tuexen310f1bc2012-07-15 11:20:56 +00003537#endif
3538#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00003539 case AF_INET6:
3540 {
3541 struct sockaddr_in6 *sin6;
3542
3543 sin6 = (struct sockaddr_in6 *)&store_sa;
3544 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
3545 sin6->sin6_port = 0;
3546 break;
3547 }
tuexen310f1bc2012-07-15 11:20:56 +00003548#endif
tuexencc2b1422012-07-10 21:50:32 +00003549#if defined(__Userspace__)
3550 case AF_CONN:
3551 {
3552 struct sockaddr_conn *sconn;
3553
3554 sconn = (struct sockaddr_conn *)&store_sa;
3555 memcpy(sconn, addr, sizeof(struct sockaddr_conn));
3556 sconn->sconn_port = 0;
3557 break;
3558 }
3559#endif
tuexendd729232011-11-01 23:04:43 +00003560 default:
3561 break;
3562 }
3563 /*
3564 * first find the interface with the bound address need to
3565 * zero out the port to find the address! yuck! can't do
3566 * this earlier since need port for sctp_pcb_findep()
3567 */
t00fcxenefb47e72012-09-09 08:16:51 +00003568 if (sctp_ifap != NULL) {
tuexendd729232011-11-01 23:04:43 +00003569 ifa = sctp_ifap;
t00fcxenefb47e72012-09-09 08:16:51 +00003570 } else {
tuexendd729232011-11-01 23:04:43 +00003571 /* Note for BSD we hit here always other
3572 * O/S's will pass things in via the
3573 * sctp_ifap argument (Panda).
3574 */
3575 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
3576 vrf_id, SCTP_ADDR_NOT_LOCKED);
3577 }
3578 if (ifa == NULL) {
3579 /* Can't find an interface with that address */
3580 SCTP_INP_WUNLOCK(inp);
3581 SCTP_INP_INFO_WUNLOCK();
3582 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3583 return (EADDRNOTAVAIL);
3584 }
3585#ifdef INET6
3586 if (addr->sa_family == AF_INET6) {
3587 /* GAK, more FIXME IFA lock? */
3588 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3589 /* Can't bind a non-existent addr. */
3590 SCTP_INP_WUNLOCK(inp);
3591 SCTP_INP_INFO_WUNLOCK();
3592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3593 return (EINVAL);
3594 }
3595 }
3596#endif
3597 /* we're not bound all */
3598 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3599 /* allow bindx() to send ASCONF's for binding changes */
3600 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3601 /* clear automatic addr changes from kernel flag */
3602 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3603
3604 /* add this address to the endpoint list */
3605 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3606 if (error != 0) {
3607 SCTP_INP_WUNLOCK(inp);
3608 SCTP_INP_INFO_WUNLOCK();
3609 return (error);
3610 }
3611 inp->laddr_count++;
3612 }
3613 /* find the bucket */
tuexen98456cf2012-04-19 15:37:07 +00003614 if (port_reuse_active) {
tuexendd729232011-11-01 23:04:43 +00003615 /* Put it into tcp 1-2-1 hash */
3616 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3617 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3618 } else {
3619 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3620 }
3621 /* put it in the bucket */
3622 LIST_INSERT_HEAD(head, inp, sctp_hash);
3623 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 +00003624 (void *)head, ntohs(lport), port_reuse_active);
tuexendd729232011-11-01 23:04:43 +00003625 /* set in the port */
3626 inp->sctp_lport = lport;
3627
3628 /* turn off just the unbound flag */
3629 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3630 SCTP_INP_WUNLOCK(inp);
3631 SCTP_INP_INFO_WUNLOCK();
3632 return (0);
3633}
3634
3635
3636static void
3637sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3638{
3639 struct sctp_iterator *it, *nit;
3640
3641 /*
3642 * We enter with the only the ITERATOR_LOCK in place and a write
3643 * lock on the inp_info stuff.
3644 */
3645 it = sctp_it_ctl.cur_it;
3646#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3647 if (it && (it->vn != curvnet)) {
3648 /* Its not looking at our VNET */
3649 return;
3650 }
3651#endif
3652 if (it && (it->inp == inp)) {
3653 /*
3654 * This is tricky and we hold the iterator lock,
3655 * but when it returns and gets the lock (when we
3656 * release it) the iterator will try to operate on
3657 * inp. We need to stop that from happening. But
3658 * of course the iterator has a reference on the
3659 * stcb and inp. We can mark it and it will stop.
3660 *
3661 * If its a single iterator situation, we
3662 * set the end iterator flag. Otherwise
3663 * we set the iterator to go to the next inp.
3664 *
3665 */
3666 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3667 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3668 } else {
3669 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3670 }
3671 }
3672 /* Now go through and remove any single reference to
3673 * our inp that may be still pending on the list
3674 */
3675 SCTP_IPI_ITERATOR_WQ_LOCK();
3676 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3677#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3678 if (it->vn != curvnet) {
3679 continue;
tuexen09631162012-04-18 11:23:02 +00003680 }
tuexendd729232011-11-01 23:04:43 +00003681#endif
3682 if (it->inp == inp) {
3683 /* This one points to me is it inp specific? */
3684 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3685 /* Remove and free this one */
3686 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3687 it, sctp_nxt_itr);
3688 if (it->function_atend != NULL) {
3689 (*it->function_atend) (it->pointer, it->val);
3690 }
3691 SCTP_FREE(it, SCTP_M_ITER);
3692 } else {
3693 it->inp = LIST_NEXT(it->inp, sctp_list);
3694 if (it->inp) {
3695 SCTP_INP_INCR_REF(it->inp);
3696 }
3697 }
3698 /* When its put in the refcnt is incremented so decr it */
3699 SCTP_INP_DECR_REF(inp);
3700 }
3701 }
3702 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3703}
3704
3705/* release sctp_inpcb unbind the port */
3706void
3707sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3708{
3709 /*
3710 * Here we free a endpoint. We must find it (if it is in the Hash
3711 * table) and remove it from there. Then we must also find it in the
3712 * overall list and remove it from there. After all removals are
3713 * complete then any timer has to be stopped. Then start the actual
3714 * freeing. a) Any local lists. b) Any associations. c) The hash of
3715 * all associations. d) finally the ep itself.
3716 */
tuexendd729232011-11-01 23:04:43 +00003717 struct sctp_tcb *asoc, *nasoc;
3718 struct sctp_laddr *laddr, *nladdr;
3719 struct inpcb *ip_pcb;
3720 struct socket *so;
tuexen63fc0bb2011-12-27 12:24:52 +00003721 int being_refed = 0;
tuexendd729232011-11-01 23:04:43 +00003722 struct sctp_queued_to_read *sq, *nsq;
3723#if !defined(__Panda__) && !defined(__Userspace__)
3724#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
3725 sctp_rtentry_t *rt;
3726#endif
3727#endif
3728 int cnt;
3729 sctp_sharedkey_t *shared_key, *nshared_key;
3730
3731
3732#if defined(__APPLE__)
3733 sctp_lock_assert(SCTP_INP_SO(inp));
3734#endif
3735#ifdef SCTP_LOG_CLOSING
3736 sctp_log_closing(inp, NULL, 0);
3737#endif
3738 SCTP_ITERATOR_LOCK();
3739 /* mark any iterators on the list or being processed */
3740 sctp_iterator_inp_being_freed(inp);
3741 SCTP_ITERATOR_UNLOCK();
3742 so = inp->sctp_socket;
3743 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3744 /* been here before.. eeks.. get out of here */
3745 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3746#ifdef SCTP_LOG_CLOSING
3747 sctp_log_closing(inp, NULL, 1);
3748#endif
3749 return;
3750 }
3751 SCTP_ASOC_CREATE_LOCK(inp);
3752 SCTP_INP_INFO_WLOCK();
3753
3754 SCTP_INP_WLOCK(inp);
3755 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3756 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3757 /* socket is gone, so no more wakeups allowed */
3758 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3759 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3760 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3761
3762 }
3763 /* First time through we have the socket lock, after that no more. */
3764 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
tuexen63fc0bb2011-12-27 12:24:52 +00003765 SCTP_FROM_SCTP_PCB+SCTP_LOC_1);
tuexendd729232011-11-01 23:04:43 +00003766
3767 if (inp->control) {
3768 sctp_m_freem(inp->control);
3769 inp->control = NULL;
3770 }
3771 if (inp->pkt) {
3772 sctp_m_freem(inp->pkt);
3773 inp->pkt = NULL;
3774 }
tuexendd729232011-11-01 23:04:43 +00003775 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3776 * here but I will be nice :> (i.e.
3777 * ip_pcb = ep;) */
3778 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3779 int cnt_in_sd;
3780
3781 cnt_in_sd = 0;
3782 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3783 SCTP_TCB_LOCK(asoc);
3784 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3785 /* Skip guys being freed */
3786 cnt_in_sd++;
3787 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3788 /*
3789 * Special case - we did not start a kill
3790 * timer on the asoc due to it was not
3791 * closed. So go ahead and start it now.
3792 */
3793 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3794 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3795 }
3796 SCTP_TCB_UNLOCK(asoc);
3797 continue;
3798 }
3799 if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3800 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
3801 (asoc->asoc.total_output_queue_size == 0)) {
3802 /* If we have data in queue, we don't want to just
3803 * free since the app may have done, send()/close
3804 * or connect/send/close. And it wants the data
3805 * to get across first.
3806 */
3807 /* Just abandon things in the front states */
3808 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3809 SCTP_FROM_SCTP_PCB+SCTP_LOC_2) == 0) {
3810 cnt_in_sd++;
3811 }
3812 continue;
3813 }
3814 /* Disconnect the socket please */
3815 asoc->sctp_socket = NULL;
3816 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3817 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3818 (asoc->asoc.control_pdapi) ||
3819 (asoc->asoc.size_on_all_streams > 0) ||
tuexen63fc0bb2011-12-27 12:24:52 +00003820 (so && (so->so_rcv.sb_cc > 0))) {
tuexendd729232011-11-01 23:04:43 +00003821 /* Left with Data unread */
3822 struct mbuf *op_err;
3823
3824 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3825 0, M_DONTWAIT, 1, MT_DATA);
3826 if (op_err) {
3827 /* Fill in the user initiated abort */
3828 struct sctp_paramhdr *ph;
3829 uint32_t *ippp;
3830
3831 SCTP_BUF_LEN(op_err) =
3832 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
3833 ph = mtod(op_err,
3834 struct sctp_paramhdr *);
3835 ph->param_type = htons(
3836 SCTP_CAUSE_USER_INITIATED_ABT);
3837 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3838 ippp = (uint32_t *) (ph + 1);
3839 *ippp = htonl(SCTP_FROM_SCTP_PCB+SCTP_LOC_3);
3840 }
3841 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_3;
tuexendd729232011-11-01 23:04:43 +00003842 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3843 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3844 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3845 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3846 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3847 }
3848 if (sctp_free_assoc(inp, asoc,
3849 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_4) == 0) {
3850 cnt_in_sd++;
3851 }
3852 continue;
3853 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3854 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3855 (asoc->asoc.stream_queue_cnt == 0)
3856 ) {
3857 if (asoc->asoc.locked_on_sending) {
3858 goto abort_anyway;
3859 }
3860 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3861 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3862 struct sctp_nets *netp;
3863
3864 if (asoc->asoc.alternate) {
3865 netp = asoc->asoc.alternate;
3866 } else {
3867 netp = asoc->asoc.primary_destination;
3868 }
3869 /*
3870 * there is nothing queued to send,
3871 * so I send shutdown
3872 */
3873 sctp_send_shutdown(asoc, netp);
3874 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3875 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3876 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3877 }
3878 SCTP_SET_STATE(&asoc->asoc,SCTP_STATE_SHUTDOWN_SENT);
3879 SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3880 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3881 netp);
3882 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3883 asoc->asoc.primary_destination);
3884 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3885 }
3886 } else {
3887 /* mark into shutdown pending */
3888 struct sctp_stream_queue_pending *sp;
3889
3890 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3891 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3892 asoc->asoc.primary_destination);
3893 if (asoc->asoc.locked_on_sending) {
3894 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3895 sctp_streamhead);
3896 if (sp == NULL) {
3897 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
t00fcxen8fca7ff2012-09-05 18:58:42 +00003898 (void *)asoc->asoc.locked_on_sending,
tuexendd729232011-11-01 23:04:43 +00003899 asoc->asoc.locked_on_sending->stream_no);
3900 } else {
3901 if ((sp->length == 0) && (sp->msg_is_complete == 0))
3902 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3903 }
3904 }
3905 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3906 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3907 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3908 struct mbuf *op_err;
3909 abort_anyway:
3910 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3911 0, M_DONTWAIT, 1, MT_DATA);
3912 if (op_err) {
3913 /* Fill in the user initiated abort */
3914 struct sctp_paramhdr *ph;
3915 uint32_t *ippp;
3916 SCTP_BUF_LEN(op_err) =
3917 (sizeof(struct sctp_paramhdr) +
3918 sizeof(uint32_t));
3919 ph = mtod(op_err,
3920 struct sctp_paramhdr *);
3921 ph->param_type = htons(
3922 SCTP_CAUSE_USER_INITIATED_ABT);
3923 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3924 ippp = (uint32_t *) (ph + 1);
3925 *ippp = htonl(SCTP_FROM_SCTP_PCB+SCTP_LOC_5);
3926 }
3927 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_5;
tuexendd729232011-11-01 23:04:43 +00003928 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3929 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3930 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3931 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3932 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3933 }
3934 if (sctp_free_assoc(inp, asoc,
3935 SCTP_PCBFREE_NOFORCE,
3936 SCTP_FROM_SCTP_PCB+SCTP_LOC_6) == 0) {
3937 cnt_in_sd++;
3938 }
3939 continue;
3940 } else {
3941 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3942 }
3943 }
3944 cnt_in_sd++;
3945 SCTP_TCB_UNLOCK(asoc);
3946 }
3947 /* now is there some left in our SHUTDOWN state? */
3948 if (cnt_in_sd) {
3949#ifdef SCTP_LOG_CLOSING
3950 sctp_log_closing(inp, NULL, 2);
3951#endif
3952 inp->sctp_socket = NULL;
3953 SCTP_INP_WUNLOCK(inp);
3954 SCTP_ASOC_CREATE_UNLOCK(inp);
3955 SCTP_INP_INFO_WUNLOCK();
3956 return;
3957 }
3958 }
3959 inp->sctp_socket = NULL;
3960 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3961 SCTP_PCB_FLAGS_UNBOUND) {
3962 /*
3963 * ok, this guy has been bound. It's port is
3964 * somewhere in the SCTP_BASE_INFO(hash table). Remove
3965 * it!
3966 */
3967 LIST_REMOVE(inp, sctp_hash);
3968 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3969 }
3970
3971 /* If there is a timer running to kill us,
3972 * forget it, since it may have a contest
3973 * on the INP lock.. which would cause us
3974 * to die ...
3975 */
3976 cnt = 0;
3977 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3978 SCTP_TCB_LOCK(asoc);
3979 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3980 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3981 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3982 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3983 }
3984 cnt++;
3985 SCTP_TCB_UNLOCK(asoc);
3986 continue;
3987 }
3988 /* Free associations that are NOT killing us */
3989 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3990 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3991 struct mbuf *op_err;
3992 uint32_t *ippp;
3993 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3994 0, M_DONTWAIT, 1, MT_DATA);
3995 if (op_err) {
3996 /* Fill in the user initiated abort */
3997 struct sctp_paramhdr *ph;
3998
3999 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
4000 sizeof(uint32_t));
4001 ph = mtod(op_err, struct sctp_paramhdr *);
4002 ph->param_type = htons(
4003 SCTP_CAUSE_USER_INITIATED_ABT);
4004 ph->param_length = htons(SCTP_BUF_LEN(op_err));
4005 ippp = (uint32_t *) (ph + 1);
4006 *ippp = htonl(SCTP_FROM_SCTP_PCB+SCTP_LOC_7);
4007
4008 }
4009 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_7;
tuexendd729232011-11-01 23:04:43 +00004010 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4011 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4012 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4013 cnt++;
4014 SCTP_TCB_UNLOCK(asoc);
4015 continue;
4016 }
4017 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
4018 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4019 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4020 }
4021 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_8) == 0) {
4022 cnt++;
4023 }
4024 }
4025 if (cnt) {
4026 /* Ok we have someone out there that will kill us */
4027 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4028#ifdef SCTP_LOG_CLOSING
4029 sctp_log_closing(inp, NULL, 3);
4030#endif
4031 SCTP_INP_WUNLOCK(inp);
4032 SCTP_ASOC_CREATE_UNLOCK(inp);
4033 SCTP_INP_INFO_WUNLOCK();
4034 return;
4035 }
4036 if (SCTP_INP_LOCK_CONTENDED(inp))
4037 being_refed++;
4038 if (SCTP_INP_READ_CONTENDED(inp))
4039 being_refed++;
tuexen63fc0bb2011-12-27 12:24:52 +00004040 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
tuexendd729232011-11-01 23:04:43 +00004041 being_refed++;
4042
tuexen63fc0bb2011-12-27 12:24:52 +00004043 if ((inp->refcount) ||
tuexendd729232011-11-01 23:04:43 +00004044 (being_refed) ||
4045 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
4046 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4047#ifdef SCTP_LOG_CLOSING
4048 sctp_log_closing(inp, NULL, 4);
4049#endif
4050 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4051 SCTP_INP_WUNLOCK(inp);
4052 SCTP_ASOC_CREATE_UNLOCK(inp);
4053 SCTP_INP_INFO_WUNLOCK();
4054 return;
4055 }
4056 inp->sctp_ep.signature_change.type = 0;
4057 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4058 /* Remove it from the list .. last thing we need a
4059 * lock for.
4060 */
4061 LIST_REMOVE(inp, sctp_list);
4062 SCTP_INP_WUNLOCK(inp);
4063 SCTP_ASOC_CREATE_UNLOCK(inp);
4064 SCTP_INP_INFO_WUNLOCK();
4065 /* Now we release all locks. Since this INP
4066 * cannot be found anymore except possibly by the
4067 * kill timer that might be running. We call
4068 * the drain function here. It should hit the case
4069 * were it sees the ACTIVE flag cleared and exit
4070 * out freeing us to proceed and destroy everything.
4071 */
4072 if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
4073 (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
4074 } else {
4075 /* Probably un-needed */
4076 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4077 }
4078
4079#ifdef SCTP_LOG_CLOSING
4080 sctp_log_closing(inp, NULL, 5);
4081#endif
4082
4083#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4084#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4085 rt = ip_pcb->inp_route.ro_rt;
4086#endif
4087#endif
4088
4089#if defined(__Panda__)
4090 if (inp->pak_to_read) {
4091 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_timer.timer);
4092 SCTP_RELEASE_PKT(inp->pak_to_read);
4093 inp->pak_to_read = NULL;
4094 }
4095 if (inp->pak_to_read_sendq) {
4096 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_sendq_timer.timer);
4097 SCTP_RELEASE_PKT(inp->pak_to_read_sendq);
4098 inp->pak_to_read_sendq = NULL;
4099 }
4100#endif
4101 if ((inp->sctp_asocidhash) != NULL) {
4102 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4103 inp->sctp_asocidhash = NULL;
4104 }
4105 /*sa_ignore FREED_MEMORY*/
4106 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4107 /* Its only abandoned if it had data left */
4108 if (sq->length)
4109 SCTP_STAT_INCR(sctps_left_abandon);
4110
4111 TAILQ_REMOVE(&inp->read_queue, sq, next);
4112 sctp_free_remote_addr(sq->whoFrom);
4113 if (so)
4114 so->so_rcv.sb_cc -= sq->length;
4115 if (sq->data) {
4116 sctp_m_freem(sq->data);
4117 sq->data = NULL;
4118 }
4119 /*
4120 * no need to free the net count, since at this point all
4121 * assoc's are gone.
4122 */
4123 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
4124 SCTP_DECR_READQ_COUNT();
4125 }
4126 /* Now the sctp_pcb things */
4127 /*
4128 * free each asoc if it is not already closed/free. we can't use the
4129 * macro here since le_next will get freed as part of the
4130 * sctp_free_assoc() call.
4131 */
tuexendd729232011-11-01 23:04:43 +00004132 if (so) {
4133#ifdef IPSEC
4134 ipsec_delete_pcbpolicy(ip_pcb);
4135#endif /* IPSEC */
4136
4137 /* Unlocks not needed since the socket is gone now */
4138 }
4139#ifndef __Panda__
4140 if (ip_pcb->inp_options) {
4141 (void)sctp_m_free(ip_pcb->inp_options);
4142 ip_pcb->inp_options = 0;
4143 }
4144#endif
4145
4146#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4147#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4148 if (rt) {
4149 RTFREE(rt);
4150 ip_pcb->inp_route.ro_rt = 0;
4151 }
4152#endif
tuexen5fc22842012-02-11 13:32:19 +00004153#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00004154#ifdef INET
4155 if (ip_pcb->inp_moptions) {
4156 inp_freemoptions(ip_pcb->inp_moptions);
4157 ip_pcb->inp_moptions = 0;
4158 }
4159#endif
4160#endif
4161#endif
4162
4163#ifdef INET6
4164#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
4165 if (inp->inp_vflag & INP_IPV6) {
4166#else
4167 if (ip_pcb->inp_vflag & INP_IPV6) {
4168#endif
4169 struct in6pcb *in6p;
4170
4171 in6p = (struct in6pcb *)inp;
4172#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4173 ip6_freepcbopts(in6p->in6p_outputopts);
4174#endif
4175 }
4176#endif /* INET6 */
4177#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
4178 inp->inp_vflag = 0;
4179#else
4180 ip_pcb->inp_vflag = 0;
4181#endif
4182 /* free up authentication fields */
4183 if (inp->sctp_ep.local_auth_chunks != NULL)
4184 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4185 if (inp->sctp_ep.local_hmacs != NULL)
4186 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4187
4188 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4189 LIST_REMOVE(shared_key, next);
4190 sctp_free_sharedkey(shared_key);
4191 /*sa_ignore FREED_MEMORY*/
4192 }
4193
4194#if defined(__APPLE__)
4195 inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
tuexen6d01b942012-05-04 18:15:17 +00004196 if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
tuexen49fdcc82012-05-04 10:13:12 +00004197#ifdef INVARIANTS
t00fcxen8fca7ff2012-09-05 18:58:42 +00004198 panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen6d01b942012-05-04 18:15:17 +00004199#else
t00fcxen8fca7ff2012-09-05 18:58:42 +00004200 SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
tuexen49fdcc82012-05-04 10:13:12 +00004201#endif
tuexen6d01b942012-05-04 18:15:17 +00004202 }
tuexendd729232011-11-01 23:04:43 +00004203 inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4204#endif
4205 /*
4206 * if we have an address list the following will free the list of
4207 * ifaddr's that are set into this ep. Again macro limitations here,
4208 * since the LIST_FOREACH could be a bad idea.
4209 */
4210 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4211 sctp_remove_laddr(laddr);
4212 }
4213
4214#ifdef SCTP_TRACK_FREED_ASOCS
4215 /* TEMP CODE */
4216 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
4217 LIST_REMOVE(asoc, sctp_tcblist);
4218 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
4219 SCTP_DECR_ASOC_COUNT();
4220 }
4221 /* *** END TEMP CODE ****/
4222#endif
4223#ifdef SCTP_MVRF
4224 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4225#endif
4226 /* Now lets see about freeing the EP hash table. */
4227 if (inp->sctp_tcbhash != NULL) {
4228 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4229 inp->sctp_tcbhash = NULL;
4230 }
4231 /* Now we must put the ep memory back into the zone pool */
4232#if defined(__FreeBSD__)
4233 INP_LOCK_DESTROY(&inp->ip_inp.inp);
4234#endif
4235 SCTP_INP_LOCK_DESTROY(inp);
4236 SCTP_INP_READ_DESTROY(inp);
4237 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
4238#if !defined(__APPLE__)
4239 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4240 SCTP_DECR_EP_COUNT();
4241#else
4242 /* For Tiger, we will do this later... */
4243#endif
4244}
4245
4246
4247struct sctp_nets *
4248sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4249{
4250 struct sctp_nets *net;
4251 /* locate the address */
4252 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4253 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4254 return (net);
4255 }
4256 return (NULL);
4257}
4258
4259
4260int
4261sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4262{
4263#ifdef __Panda__
4264 return (0);
4265#else
4266 struct sctp_ifa *sctp_ifa;
4267 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4268 if (sctp_ifa) {
4269 return (1);
4270 } else {
4271 return (0);
4272 }
4273#endif
4274}
4275
4276/*
4277 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4278 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4279 * stats of stuff.
4280 */
4281int
4282sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
4283 struct sctp_nets **netp, int set_scope, int from)
4284{
4285 /*
4286 * The following is redundant to the same lines in the
4287 * sctp_aloc_assoc() but is needed since others call the add
4288 * address function
4289 */
4290 struct sctp_nets *net, *netfirst;
4291 int addr_inscope;
4292
4293 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4294 from);
4295 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4296
4297 netfirst = sctp_findnet(stcb, newaddr);
4298 if (netfirst) {
4299 /*
4300 * Lie and return ok, we don't want to make the association
4301 * go away for this behavior. It will happen in the TCP
4302 * model in a connected socket. It does not reach the hash
4303 * table until after the association is built so it can't be
4304 * found. Mark as reachable, since the initial creation will
4305 * have been cleared and the NOT_IN_ASSOC flag will have
4306 * been added... and we don't want to end up removing it
4307 * back out.
4308 */
4309 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4310 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4311 SCTP_ADDR_UNCONFIRMED);
4312 } else {
4313 netfirst->dest_state = SCTP_ADDR_REACHABLE;
4314 }
4315
4316 return (0);
4317 }
4318 addr_inscope = 1;
4319 switch (newaddr->sa_family) {
4320#ifdef INET
4321 case AF_INET:
4322 {
4323 struct sockaddr_in *sin;
4324
4325 sin = (struct sockaddr_in *)newaddr;
4326 if (sin->sin_addr.s_addr == 0) {
4327 /* Invalid address */
4328 return (-1);
4329 }
4330 /* zero out the bzero area */
4331 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4332
4333 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004334#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00004335 sin->sin_len = sizeof(struct sockaddr_in);
4336#endif
4337 if (set_scope) {
4338#ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
4339 stcb->ipv4_local_scope = 1;
4340#else
4341 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
4342 stcb->asoc.ipv4_local_scope = 1;
4343 }
4344#endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
4345 } else {
4346 /* Validate the address is in scope */
4347 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
4348 (stcb->asoc.ipv4_local_scope == 0)) {
4349 addr_inscope = 0;
4350 }
4351 }
4352 break;
4353 }
4354#endif
4355#ifdef INET6
4356 case AF_INET6:
4357 {
4358 struct sockaddr_in6 *sin6;
4359
4360 sin6 = (struct sockaddr_in6 *)newaddr;
4361 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4362 /* Invalid address */
4363 return (-1);
4364 }
4365 /* assure len is set */
t00fcxen8fbaee32012-09-04 16:41:39 +00004366#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00004367 sin6->sin6_len = sizeof(struct sockaddr_in6);
4368#endif
4369 if (set_scope) {
4370 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
4371 stcb->asoc.loopback_scope = 1;
4372 stcb->asoc.local_scope = 0;
4373 stcb->asoc.ipv4_local_scope = 1;
4374 stcb->asoc.site_scope = 1;
4375 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4376 /*
4377 * If the new destination is a LINK_LOCAL we
4378 * must have common site scope. Don't set
4379 * the local scope since we may not share
4380 * all links, only loopback can do this.
4381 * Links on the local network would also be
4382 * on our private network for v4 too.
4383 */
4384 stcb->asoc.ipv4_local_scope = 1;
4385 stcb->asoc.site_scope = 1;
4386 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4387 /*
4388 * If the new destination is SITE_LOCAL then
4389 * we must have site scope in common.
4390 */
4391 stcb->asoc.site_scope = 1;
4392 }
4393 } else {
4394 /* Validate the address is in scope */
4395 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
4396 (stcb->asoc.loopback_scope == 0)) {
4397 addr_inscope = 0;
4398 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
4399 (stcb->asoc.local_scope == 0)) {
4400 addr_inscope = 0;
4401 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
4402 (stcb->asoc.site_scope == 0)) {
4403 addr_inscope = 0;
4404 }
4405 }
4406 break;
4407 }
4408#endif
tuexencc2b1422012-07-10 21:50:32 +00004409#if defined(__Userspace__)
4410 case AF_CONN:
4411 {
4412 struct sockaddr_conn *sconn;
4413
4414 sconn = (struct sockaddr_conn *)newaddr;
4415 if (sconn->sconn_addr == NULL) {
4416 /* Invalid address */
4417 return (-1);
4418 }
t00fcxen8fbaee32012-09-04 16:41:39 +00004419#ifdef HAVE_SCONN_LEN
tuexencc2b1422012-07-10 21:50:32 +00004420 sconn->sconn_len = sizeof(struct sockaddr_conn);
4421#endif
4422 break;
4423 }
4424#endif
tuexendd729232011-11-01 23:04:43 +00004425 default:
4426 /* not supported family type */
4427 return (-1);
4428 }
4429 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4430 if (net == NULL) {
4431 return (-1);
4432 }
4433 SCTP_INCR_RADDR_COUNT();
4434 bzero(net, sizeof(struct sctp_nets));
4435 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
tuexendd729232011-11-01 23:04:43 +00004436 switch (newaddr->sa_family) {
4437#ifdef INET
4438 case AF_INET:
tuexen9784e9a2011-12-18 13:04:23 +00004439#if defined(__Windows__) || defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00004440 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4441#endif
4442 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4443 break;
4444#endif
4445#ifdef INET6
4446 case AF_INET6:
tuexenda6f8ba2012-01-08 11:33:02 +00004447#if defined(__Windows__) || defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00004448 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4449#endif
4450 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4451 break;
4452#endif
tuexencc2b1422012-07-10 21:50:32 +00004453#if defined(__Userspace__)
4454 case AF_CONN:
4455#if defined(__Windows__) || defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
4456 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4457#endif
4458 ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4459 break;
4460#endif
tuexendd729232011-11-01 23:04:43 +00004461 default:
4462 break;
4463 }
4464 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4465 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
4466 stcb->asoc.loopback_scope = 1;
4467 stcb->asoc.ipv4_local_scope = 1;
4468 stcb->asoc.local_scope = 0;
4469 stcb->asoc.site_scope = 1;
4470 addr_inscope = 1;
4471 }
4472 net->failure_threshold = stcb->asoc.def_net_failure;
4473 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4474 if (addr_inscope == 0) {
4475 net->dest_state = (SCTP_ADDR_REACHABLE |
4476 SCTP_ADDR_OUT_OF_SCOPE);
4477 } else {
4478 if (from == SCTP_ADDR_IS_CONFIRMED)
4479 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4480 net->dest_state = SCTP_ADDR_REACHABLE;
4481 else
4482 net->dest_state = SCTP_ADDR_REACHABLE |
4483 SCTP_ADDR_UNCONFIRMED;
4484 }
4485 /* We set this to 0, the timer code knows that
4486 * this means its an initial value
4487 */
4488 net->rto_needed = 1;
4489 net->RTO = 0;
4490 net->RTO_measured = 0;
4491 stcb->asoc.numnets++;
tuexen9d7b2072011-11-20 16:35:17 +00004492 net->ref_count = 1;
tuexendd729232011-11-01 23:04:43 +00004493 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
tuexen9d7b2072011-11-20 16:35:17 +00004494 net->port = stcb->asoc.port;
tuexendd729232011-11-01 23:04:43 +00004495 net->dscp = stcb->asoc.default_dscp;
4496#ifdef INET6
4497 net->flowlabel = stcb->asoc.default_flowlabel;
4498#endif
4499 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4500 net->dest_state |= SCTP_ADDR_NOHB;
4501 } else {
4502 net->dest_state &= ~SCTP_ADDR_NOHB;
4503 }
4504 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4505 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4506 } else {
4507 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4508 }
4509 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4510 /* Init the timer structure */
4511 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4512 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4513 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4514
4515 /* Now generate a route for this guy */
4516#ifdef INET6
4517#ifdef SCTP_EMBEDDED_V6_SCOPE
4518 /* KAME hack: embed scopeid */
4519 if (newaddr->sa_family == AF_INET6) {
4520 struct sockaddr_in6 *sin6;
4521
4522 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
tuexene3851592012-06-01 08:31:45 +00004523#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00004524#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4525 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
tuexendd729232011-11-01 23:04:43 +00004526#else
tuexen9a218b12012-08-04 21:17:58 +00004527 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
tuexendd729232011-11-01 23:04:43 +00004528#endif
4529#elif defined(SCTP_KAME)
4530 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4531#else
4532 (void)in6_embedscope(&sin6->sin6_addr, sin6);
4533#endif
4534#ifndef SCOPEDROUTING
4535 sin6->sin6_scope_id = 0;
4536#endif
4537 }
4538#endif /* SCTP_EMBEDDED_V6_SCOPE */
4539#endif
4540 SCTP_RTALLOC((sctp_route_t *)&net->ro, stcb->asoc.vrf_id);
4541
4542 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4543 /* Get source address */
4544 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
4545 stcb,
4546 (sctp_route_t *)&net->ro,
4547 net,
4548 0,
4549 stcb->asoc.vrf_id);
4550 /* Now get the interface MTU */
4551 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
4552 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
tuexendd729232011-11-01 23:04:43 +00004553 }
tuexene81177b2011-11-15 20:48:08 +00004554 if (net->mtu > 0) {
tuexendd729232011-11-01 23:04:43 +00004555 uint32_t rmtu;
4556
4557 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
4558 if (rmtu == 0) {
4559 /* Start things off to match mtu of interface please. */
4560 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
4561 net->ro.ro_rt, net->mtu);
4562 } else {
4563 /* we take the route mtu over the interface, since
4564 * the route may be leading out the loopback, or
4565 * a different interface.
4566 */
4567 net->mtu = rmtu;
4568 }
4569 }
tuexen09631162012-04-18 11:23:02 +00004570 }
tuexene81177b2011-11-15 20:48:08 +00004571 if (net->mtu == 0) {
4572 switch (newaddr->sa_family) {
4573#ifdef INET
4574 case AF_INET:
4575 net->mtu = SCTP_DEFAULT_MTU;
4576 break;
4577#endif
4578#ifdef INET6
4579 case AF_INET6:
4580 net->mtu = 1280;
4581 break;
4582#endif
tuexencc2b1422012-07-10 21:50:32 +00004583#if defined(__Userspace__)
4584 case AF_CONN:
4585 net->mtu = 1280;
4586 break;
4587#endif
tuexene81177b2011-11-15 20:48:08 +00004588 default:
4589 break;
tuexendd729232011-11-01 23:04:43 +00004590 }
tuexene81177b2011-11-15 20:48:08 +00004591 }
4592 if (net->port) {
4593 net->mtu -= (uint32_t)sizeof(struct udphdr);
4594 }
4595 if (from == SCTP_ALLOC_ASOC) {
4596 stcb->asoc.smallest_mtu = net->mtu;
4597 }
4598 if (stcb->asoc.smallest_mtu > net->mtu) {
4599 stcb->asoc.smallest_mtu = net->mtu;
tuexendd729232011-11-01 23:04:43 +00004600 }
4601#ifdef INET6
4602#ifdef SCTP_EMBEDDED_V6_SCOPE
4603 if (newaddr->sa_family == AF_INET6) {
4604 struct sockaddr_in6 *sin6;
4605
4606 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4607#ifdef SCTP_KAME
4608 (void)sa6_recoverscope(sin6);
4609#else
4610 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4611#endif /* SCTP_KAME */
4612 }
4613#endif /* SCTP_EMBEDDED_V6_SCOPE */
4614#endif
tuexendd729232011-11-01 23:04:43 +00004615
4616 /* JRS - Use the congestion control given in the CC module */
tuexen09631162012-04-18 11:23:02 +00004617 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
tuexendd729232011-11-01 23:04:43 +00004618 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4619
4620 /*
4621 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4622 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4623 */
4624 net->find_pseudo_cumack = 1;
4625 net->find_rtx_pseudo_cumack = 1;
4626 net->src_addr_selected = 0;
4627#if defined(__FreeBSD__)
4628 /* Choose an initial flowid. */
4629 net->flowid = stcb->asoc.my_vtag ^
4630 ntohs(stcb->rport) ^
4631 ntohs(stcb->sctp_ep->sctp_lport);
4632#ifdef INVARIANTS
4633 net->flowidset = 1;
4634#endif
4635#endif
4636 if (netp) {
4637 *netp = net;
4638 }
4639 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4640 if (net->ro.ro_rt == NULL) {
4641 /* Since we have no route put it at the back */
4642 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4643 } else if (netfirst == NULL) {
4644 /* We are the first one in the pool. */
4645 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4646 } else if (netfirst->ro.ro_rt == NULL) {
4647 /*
4648 * First one has NO route. Place this one ahead of the first
4649 * one.
4650 */
4651 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4652#ifndef __Panda__
4653 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4654 /*
4655 * This one has a different interface than the one at the
4656 * top of the list. Place it ahead.
4657 */
4658 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4659#endif
4660 } else {
4661 /*
4662 * Ok we have the same interface as the first one. Move
4663 * forward until we find either a) one with a NULL route...
4664 * insert ahead of that b) one with a different ifp.. insert
4665 * after that. c) end of the list.. insert at the tail.
4666 */
4667 struct sctp_nets *netlook;
4668
4669 do {
4670 netlook = TAILQ_NEXT(netfirst, sctp_next);
4671 if (netlook == NULL) {
4672 /* End of the list */
4673 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4674 break;
4675 } else if (netlook->ro.ro_rt == NULL) {
4676 /* next one has NO route */
4677 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4678 break;
4679 }
4680#ifndef __Panda__
4681 else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp)
4682#else
4683 else
4684#endif
4685 {
4686 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4687 net, sctp_next);
4688 break;
4689 }
4690#ifndef __Panda__
4691 /* Shift forward */
4692 netfirst = netlook;
4693#endif
4694 } while (netlook != NULL);
4695 }
4696
4697 /* got to have a primary set */
4698 if (stcb->asoc.primary_destination == 0) {
4699 stcb->asoc.primary_destination = net;
4700 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4701 (net->ro.ro_rt) &&
4702 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4703 /* No route to current primary adopt new primary */
4704 stcb->asoc.primary_destination = net;
4705 }
4706 /* Validate primary is first */
4707 net = TAILQ_FIRST(&stcb->asoc.nets);
4708 if ((net != stcb->asoc.primary_destination) &&
4709 (stcb->asoc.primary_destination)) {
4710 /* first one on the list is NOT the primary
4711 * sctp_cmpaddr() is much more efficient if
4712 * the primary is the first on the list, make it
4713 * so.
4714 */
4715 TAILQ_REMOVE(&stcb->asoc.nets,
4716 stcb->asoc.primary_destination, sctp_next);
4717 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4718 stcb->asoc.primary_destination, sctp_next);
4719 }
4720 return (0);
4721}
4722
4723
4724static uint32_t
4725sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4726{
4727 uint32_t id;
4728 struct sctpasochead *head;
4729 struct sctp_tcb *lstcb;
4730
4731 SCTP_INP_WLOCK(inp);
4732 try_again:
4733 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4734 /* TSNH */
4735 SCTP_INP_WUNLOCK(inp);
4736 return (0);
4737 }
4738 /*
4739 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4740 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4741 */
4742 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4743 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4744 }
4745 id = inp->sctp_associd_counter;
4746 inp->sctp_associd_counter++;
4747 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4748 if (lstcb) {
4749 goto try_again;
4750 }
4751 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4752 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4753 stcb->asoc.in_asocid_hash = 1;
4754 SCTP_INP_WUNLOCK(inp);
4755 return id;
4756}
4757
4758/*
4759 * allocate an association and add it to the endpoint. The caller must be
4760 * careful to add all additional addresses once they are know right away or
4761 * else the assoc will be may experience a blackout scenario.
4762 */
4763struct sctp_tcb *
4764sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4765 int *error, uint32_t override_tag, uint32_t vrf_id,
4766#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
4767 struct thread *p
4768#elif defined(__Windows__)
4769 PKTHREAD p
4770#else
4771#if defined(__Userspace__)
4772 /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
4773#endif
4774 struct proc *p
4775#endif
4776)
4777{
4778 /* note the p argument is only valid in unbound sockets */
4779
4780 struct sctp_tcb *stcb;
4781 struct sctp_association *asoc;
4782 struct sctpasochead *head;
4783 uint16_t rport;
4784 int err;
4785
4786 /*
4787 * Assumption made here: Caller has done a
4788 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4789 * address does not exist already.
4790 */
4791 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4792 /* Hit max assoc, sorry no more */
4793 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4794 *error = ENOBUFS;
4795 return (NULL);
4796 }
4797 if (firstaddr == NULL) {
4798 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4799 *error = EINVAL;
4800 return (NULL);
4801 }
4802 SCTP_INP_RLOCK(inp);
4803 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4804 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4805 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4806 /*
4807 * If its in the TCP pool, its NOT allowed to create an
4808 * association. The parent listener needs to call
4809 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4810 * off, or connected one does this.. its an error.
4811 */
4812 SCTP_INP_RUNLOCK(inp);
4813 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4814 *error = EINVAL;
4815 return (NULL);
4816 }
4817 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4818 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4819 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4820 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4821 SCTP_INP_RUNLOCK(inp);
4822 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4823 *error = EINVAL;
4824 return (NULL);
4825 }
4826 }
4827 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4828#ifdef SCTP_DEBUG
4829 if (firstaddr) {
4830 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4831 switch (firstaddr->sa_family) {
4832#ifdef INET
4833 case AF_INET:
4834 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4835 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4836 break;
4837#endif
4838#ifdef INET6
4839 case AF_INET6:
4840 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4841 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4842 break;
4843#endif
tuexencc2b1422012-07-10 21:50:32 +00004844#if defined(__Userspace__)
4845 case AF_CONN:
4846 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4847 ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
4848 break;
4849#endif
tuexendd729232011-11-01 23:04:43 +00004850 default:
4851 break;
4852 }
4853 } else {
4854 SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
4855 }
4856#endif /* SCTP_DEBUG */
4857 switch (firstaddr->sa_family) {
4858#ifdef INET
4859 case AF_INET:
4860 {
4861 struct sockaddr_in *sin;
4862
4863 sin = (struct sockaddr_in *)firstaddr;
4864 if ((ntohs(sin->sin_port) == 0) ||
4865 (sin->sin_addr.s_addr == INADDR_ANY) ||
4866 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
4867 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
4868 /* Invalid address */
4869 SCTP_INP_RUNLOCK(inp);
4870 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4871 *error = EINVAL;
4872 return (NULL);
4873 }
4874 rport = sin->sin_port;
4875 break;
4876 }
4877#endif
4878#ifdef INET6
4879 case AF_INET6:
4880 {
4881 struct sockaddr_in6 *sin6;
4882
4883 sin6 = (struct sockaddr_in6 *)firstaddr;
4884 if ((ntohs(sin6->sin6_port) == 0) ||
4885 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4886 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4887 /* Invalid address */
4888 SCTP_INP_RUNLOCK(inp);
4889 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4890 *error = EINVAL;
4891 return (NULL);
4892 }
4893 rport = sin6->sin6_port;
4894 break;
4895 }
4896#endif
tuexencc2b1422012-07-10 21:50:32 +00004897#if defined(__Userspace__)
4898 case AF_CONN:
4899 {
4900 struct sockaddr_conn *sconn;
4901
4902 sconn = (struct sockaddr_conn *)firstaddr;
4903 if ((ntohs(sconn->sconn_port) == 0) ||
4904 (sconn->sconn_addr == NULL)) {
4905 /* Invalid address */
4906 SCTP_INP_RUNLOCK(inp);
4907 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4908 *error = EINVAL;
4909 return (NULL);
4910 }
4911 rport = sconn->sconn_port;
4912 break;
4913 }
4914#endif
tuexendd729232011-11-01 23:04:43 +00004915 default:
4916 /* not supported family type */
4917 SCTP_INP_RUNLOCK(inp);
4918 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4919 *error = EINVAL;
4920 return (NULL);
4921 }
4922 SCTP_INP_RUNLOCK(inp);
4923 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4924 /*
4925 * If you have not performed a bind, then we need to do the
4926 * ephemeral bind for you.
4927 */
4928 if ((err = sctp_inpcb_bind(inp->sctp_socket,
4929 (struct sockaddr *)NULL,
4930 (struct sctp_ifa *)NULL,
4931#ifndef __Panda__
4932 p
4933#else
4934 (struct proc *)NULL
4935#endif
4936 ))) {
4937 /* bind error, probably perm */
4938 *error = err;
4939 return (NULL);
4940 }
4941 }
4942 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4943 if (stcb == NULL) {
4944 /* out of memory? */
4945 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4946 *error = ENOMEM;
4947 return (NULL);
4948 }
4949 SCTP_INCR_ASOC_COUNT();
4950
4951 bzero(stcb, sizeof(*stcb));
4952 asoc = &stcb->asoc;
4953
4954 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4955 SCTP_TCB_LOCK_INIT(stcb);
4956 SCTP_TCB_SEND_LOCK_INIT(stcb);
4957 stcb->rport = rport;
4958 /* setup back pointer's */
4959 stcb->sctp_ep = inp;
4960 stcb->sctp_socket = inp->sctp_socket;
4961 if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) {
4962 /* failed */
4963 SCTP_TCB_LOCK_DESTROY(stcb);
4964 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4965 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4966 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4967 SCTP_DECR_ASOC_COUNT();
4968 *error = err;
4969 return (NULL);
4970 }
4971 /* and the port */
4972 SCTP_INP_INFO_WLOCK();
4973 SCTP_INP_WLOCK(inp);
4974 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4975 /* inpcb freed while alloc going on */
4976 SCTP_TCB_LOCK_DESTROY(stcb);
4977 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4978 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4979 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4980 SCTP_INP_WUNLOCK(inp);
4981 SCTP_INP_INFO_WUNLOCK();
4982 SCTP_DECR_ASOC_COUNT();
4983 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4984 *error = EINVAL;
4985 return (NULL);
4986 }
4987 SCTP_TCB_LOCK(stcb);
4988
4989 /* now that my_vtag is set, add it to the hash */
4990 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4991 /* put it in the bucket in the vtag hash of assoc's for the system */
4992 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4993 SCTP_INP_INFO_WUNLOCK();
4994
4995 if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4996 /* failure.. memory error? */
4997 if (asoc->strmout) {
4998 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4999 asoc->strmout = NULL;
5000 }
5001 if (asoc->mapping_array) {
5002 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5003 asoc->mapping_array = NULL;
5004 }
5005 if (asoc->nr_mapping_array) {
5006 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5007 asoc->nr_mapping_array = NULL;
5008 }
5009 SCTP_DECR_ASOC_COUNT();
5010 SCTP_TCB_LOCK_DESTROY(stcb);
5011 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5012 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5013 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5014 SCTP_INP_WUNLOCK(inp);
5015 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
5016 *error = ENOBUFS;
5017 return (NULL);
5018 }
5019 /* Init all the timers */
5020 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
5021 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
5022 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
5023 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
5024 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
5025 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
5026 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5027
5028 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5029 /* now file the port under the hash as well */
5030 if (inp->sctp_tcbhash != NULL) {
5031 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5032 inp->sctp_hashmark)];
5033 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5034 }
5035 SCTP_INP_WUNLOCK(inp);
t00fcxen8fca7ff2012-09-05 18:58:42 +00005036 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
tuexendd729232011-11-01 23:04:43 +00005037 return (stcb);
5038}
5039
5040
5041void
5042sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5043{
5044 struct sctp_association *asoc;
5045
5046 asoc = &stcb->asoc;
5047 asoc->numnets--;
5048 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5049 if (net == asoc->primary_destination) {
5050 /* Reset primary */
5051 struct sctp_nets *lnet;
5052
5053 lnet = TAILQ_FIRST(&asoc->nets);
5054 /* Mobility adaptation
5055 Ideally, if deleted destination is the primary, it becomes
5056 a fast retransmission trigger by the subsequent SET PRIMARY.
5057 (by micchie)
5058 */
5059 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
5060 SCTP_MOBILITY_BASE) ||
5061 sctp_is_mobility_feature_on(stcb->sctp_ep,
5062 SCTP_MOBILITY_FASTHANDOFF)) {
5063 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5064 if (asoc->deleted_primary != NULL) {
5065 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5066 goto out;
5067 }
5068 asoc->deleted_primary = net;
5069 atomic_add_int(&net->ref_count, 1);
5070 memset(&net->lastsa, 0, sizeof(net->lastsa));
5071 memset(&net->lastsv, 0, sizeof(net->lastsv));
5072 sctp_mobility_feature_on(stcb->sctp_ep,
5073 SCTP_MOBILITY_PRIM_DELETED);
5074 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5075 stcb->sctp_ep, stcb, NULL);
5076 }
5077out:
5078 /* Try to find a confirmed primary */
5079 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5080 }
5081 if (net == asoc->last_data_chunk_from) {
5082 /* Reset primary */
5083 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5084 }
5085 if (net == asoc->last_control_chunk_from) {
5086 /* Clear net */
5087 asoc->last_control_chunk_from = NULL;
5088 }
5089 if (net == stcb->asoc.alternate) {
5090 sctp_free_remote_addr(stcb->asoc.alternate);
5091 stcb->asoc.alternate = NULL;
5092 }
5093 sctp_free_remote_addr(net);
5094}
5095
5096/*
5097 * remove a remote endpoint address from an association, it will fail if the
5098 * address does not exist.
5099 */
5100int
5101sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5102{
5103 /*
5104 * Here we need to remove a remote address. This is quite simple, we
5105 * first find it in the list of address for the association
5106 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5107 * on that item. Note we do not allow it to be removed if there are
5108 * no other addresses.
5109 */
5110 struct sctp_association *asoc;
5111 struct sctp_nets *net, *nnet;
5112
5113 asoc = &stcb->asoc;
5114
5115 /* locate the address */
5116 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5117 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5118 continue;
5119 }
5120 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5121 remaddr)) {
5122 /* we found the guy */
5123 if (asoc->numnets < 2) {
5124 /* Must have at LEAST two remote addresses */
5125 return (-1);
5126 } else {
5127 sctp_remove_net(stcb, net);
5128 return (0);
5129 }
5130 }
5131 }
5132 /* not found. */
5133 return (-2);
5134}
5135
5136void
5137sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5138{
5139 struct sctpvtaghead *chain;
5140 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005141 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005142 int i;
5143
5144 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5145 if (!LIST_EMPTY(chain)) {
5146 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5147 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5148 if ((twait_block->vtag_block[i].v_tag == tag) &&
5149 (twait_block->vtag_block[i].lport == lport) &&
5150 (twait_block->vtag_block[i].rport == rport)) {
5151 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5152 twait_block->vtag_block[i].v_tag = 0;
5153 twait_block->vtag_block[i].lport = 0;
5154 twait_block->vtag_block[i].rport = 0;
5155 found = 1;
5156 break;
5157 }
5158 }
5159 if (found)
5160 break;
5161 }
5162 }
5163}
5164
5165int
5166sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5167{
5168 struct sctpvtaghead *chain;
5169 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005170 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005171 int i;
5172
5173 SCTP_INP_INFO_WLOCK();
5174 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5175 if (!LIST_EMPTY(chain)) {
5176 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5177 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5178 if ((twait_block->vtag_block[i].v_tag == tag) &&
5179 (twait_block->vtag_block[i].lport == lport) &&
5180 (twait_block->vtag_block[i].rport == rport)) {
5181 found = 1;
5182 break;
5183 }
5184 }
5185 if (found)
5186 break;
5187 }
5188 }
5189 SCTP_INP_INFO_WUNLOCK();
tuexen63fc0bb2011-12-27 12:24:52 +00005190 return (found);
tuexendd729232011-11-01 23:04:43 +00005191}
5192
5193
5194void
5195sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
5196{
5197 struct sctpvtaghead *chain;
5198 struct sctp_tagblock *twait_block;
5199 struct timeval now;
5200 int set, i;
5201
5202 if (time == 0) {
5203 /* Its disabled */
5204 return;
5205 }
5206 (void)SCTP_GETTIME_TIMEVAL(&now);
5207 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5208 set = 0;
5209 if (!LIST_EMPTY(chain)) {
5210 /* Block(s) present, lets find space, and expire on the fly */
5211 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5212 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5213 if ((twait_block->vtag_block[i].v_tag == 0) &&
5214 !set) {
5215 twait_block->vtag_block[i].tv_sec_at_expire =
5216 now.tv_sec + time;
5217 twait_block->vtag_block[i].v_tag = tag;
5218 twait_block->vtag_block[i].lport = lport;
5219 twait_block->vtag_block[i].rport = rport;
5220 set = 1;
5221 } else if ((twait_block->vtag_block[i].v_tag) &&
5222 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
5223 /* Audit expires this guy */
5224 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5225 twait_block->vtag_block[i].v_tag = 0;
5226 twait_block->vtag_block[i].lport = 0;
5227 twait_block->vtag_block[i].rport = 0;
5228 if (set == 0) {
5229 /* Reuse it for my new tag */
5230 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
5231 twait_block->vtag_block[i].v_tag = tag;
5232 twait_block->vtag_block[i].lport = lport;
5233 twait_block->vtag_block[i].rport = rport;
5234 set = 1;
5235 }
5236 }
5237 }
5238 if (set) {
5239 /*
5240 * We only do up to the block where we can
5241 * place our tag for audits
5242 */
5243 break;
5244 }
5245 }
5246 }
5247 /* Need to add a new block to chain */
5248 if (!set) {
5249 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5250 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5251 if (twait_block == NULL) {
5252#ifdef INVARIANTS
5253 panic("Can not alloc tagblock");
5254#endif
5255 return;
5256 }
5257 memset(twait_block, 0, sizeof(struct sctp_tagblock));
5258 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
5259 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
5260 twait_block->vtag_block[0].v_tag = tag;
5261 twait_block->vtag_block[0].lport = lport;
5262 twait_block->vtag_block[0].rport = rport;
5263 }
5264}
5265
5266
5267#ifdef __Panda__
5268void panda_wakeup_socket(struct socket *so);
5269#endif
5270
5271/*-
5272 * Free the association after un-hashing the remote port. This
5273 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5274 * expect that the input to this function IS a locked TCB.
5275 * It will return 0, if it did NOT destroy the association (instead
5276 * it unlocks it. It will return NON-zero if it either destroyed the
5277 * association OR the association is already destroyed.
5278 */
5279int
5280sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5281{
5282 int i;
5283 struct sctp_association *asoc;
5284 struct sctp_nets *net, *nnet;
5285 struct sctp_laddr *laddr, *naddr;
5286 struct sctp_tmit_chunk *chk, *nchk;
5287 struct sctp_asconf_addr *aparam, *naparam;
5288 struct sctp_asconf_ack *aack, *naack;
5289 struct sctp_stream_reset_list *strrst, *nstrrst;
5290 struct sctp_queued_to_read *sq, *nsq;
5291 struct sctp_stream_queue_pending *sp, *nsp;
5292 sctp_sharedkey_t *shared_key, *nshared_key;
5293 struct socket *so;
5294
5295 /* first, lets purge the entry from the hash table. */
5296#if defined(__APPLE__)
5297 sctp_lock_assert(SCTP_INP_SO(inp));
5298#endif
5299
5300#ifdef SCTP_LOG_CLOSING
5301 sctp_log_closing(inp, stcb, 6);
5302#endif
5303 if (stcb->asoc.state == 0) {
5304#ifdef SCTP_LOG_CLOSING
5305 sctp_log_closing(inp, NULL, 7);
5306#endif
5307 /* there is no asoc, really TSNH :-0 */
5308 return (1);
5309 }
5310 if (stcb->asoc.alternate) {
5311 sctp_free_remote_addr(stcb->asoc.alternate);
5312 stcb->asoc.alternate = NULL;
5313 }
5314#if !defined(__APPLE__) /* TEMP: moved to below */
5315 /* TEMP CODE */
5316 if (stcb->freed_from_where == 0) {
5317 /* Only record the first place free happened from */
5318 stcb->freed_from_where = from_location;
5319 }
5320 /* TEMP CODE */
5321#endif
5322
5323 asoc = &stcb->asoc;
5324 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5325 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5326 /* nothing around */
5327 so = NULL;
5328 else
5329 so = inp->sctp_socket;
5330
5331 /*
5332 * We used timer based freeing if a reader or writer is in the way.
5333 * So we first check if we are actually being called from a timer,
5334 * if so we abort early if a reader or writer is still in the way.
5335 */
5336 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5337 (from_inpcbfree == SCTP_NORMAL_PROC)) {
5338 /*
5339 * is it the timer driving us? if so are the reader/writers
5340 * gone?
5341 */
5342 if (stcb->asoc.refcnt) {
5343 /* nope, reader or writer in the way */
5344 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5345 /* no asoc destroyed */
5346 SCTP_TCB_UNLOCK(stcb);
5347#ifdef SCTP_LOG_CLOSING
5348 sctp_log_closing(inp, stcb, 8);
5349#endif
5350 return (0);
5351 }
5352 }
5353 /* now clean up any other timers */
5354 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
5355 asoc->dack_timer.self = NULL;
5356 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
5357 /*-
5358 * For stream reset we don't blast this unless
5359 * it is a str-reset timer, it might be the
5360 * free-asoc timer which we DON'T want to
5361 * disturb.
5362 */
5363 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
5364 asoc->strreset_timer.self = NULL;
5365 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
5366 asoc->asconf_timer.self = NULL;
5367 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
5368 asoc->autoclose_timer.self = NULL;
5369 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
5370 asoc->shut_guard_timer.self = NULL;
5371 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
5372 asoc->delayed_event_timer.self = NULL;
5373 /* Mobility adaptation */
5374 (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
5375 asoc->delete_prim_timer.self = NULL;
5376 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5377 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
5378 net->rxt_timer.self = NULL;
5379 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
5380 net->pmtu_timer.self = NULL;
5381 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
5382 net->hb_timer.self = NULL;
5383 }
5384 /* Now the read queue needs to be cleaned up (only once) */
5385 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
5386 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
5387 SCTP_INP_READ_LOCK(inp);
5388 TAILQ_FOREACH(sq, &inp->read_queue, next) {
5389 if (sq->stcb == stcb) {
5390 sq->do_not_ref_stcb = 1;
5391 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5392 /* If there is no end, there never
5393 * will be now.
5394 */
5395 if (sq->end_added == 0) {
5396 /* Held for PD-API clear that. */
5397 sq->pdapi_aborted = 1;
5398 sq->held_length = 0;
5399 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
5400 /*
5401 * Need to add a PD-API aborted indication.
5402 * Setting the control_pdapi assures that it will
5403 * be added right after this msg.
5404 */
5405 uint32_t strseq;
5406 stcb->asoc.control_pdapi = sq;
5407 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
5408 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5409 stcb,
5410 SCTP_PARTIAL_DELIVERY_ABORTED,
5411 (void *)&strseq,
5412 SCTP_SO_LOCKED);
5413 stcb->asoc.control_pdapi = NULL;
5414 }
5415 }
5416 /* Add an end to wake them */
5417 sq->end_added = 1;
5418 }
5419 }
5420 SCTP_INP_READ_UNLOCK(inp);
5421 if (stcb->block_entry) {
5422 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
5423 stcb->block_entry->error = ECONNRESET;
5424 stcb->block_entry = NULL;
5425 }
5426 }
5427 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
5428 /* Someone holds a reference OR the socket is unaccepted yet.
5429 */
5430 if ((stcb->asoc.refcnt) ||
5431 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5432 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
5433 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
5434 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5435 }
5436 SCTP_TCB_UNLOCK(stcb);
5437 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5438 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5439 /* nothing around */
5440 so = NULL;
5441 if (so) {
5442 /* Wake any reader/writers */
5443 sctp_sorwakeup(inp, so);
5444 sctp_sowwakeup(inp, so);
5445 }
5446
5447#ifdef SCTP_LOG_CLOSING
5448 sctp_log_closing(inp, stcb, 9);
5449#endif
5450 /* no asoc destroyed */
5451 return (0);
5452 }
5453#ifdef SCTP_LOG_CLOSING
5454 sctp_log_closing(inp, stcb, 10);
5455#endif
5456 /* When I reach here, no others want
5457 * to kill the assoc yet.. and I own
5458 * the lock. Now its possible an abort
5459 * comes in when I do the lock exchange
5460 * below to grab all the locks to do
5461 * the final take out. to prevent this
5462 * we increment the count, which will
5463 * start a timer and blow out above thus
5464 * assuring us that we hold exclusive
5465 * killing of the asoc. Note that
5466 * after getting back the TCB lock
5467 * we will go ahead and increment the
5468 * counter back up and stop any timer
5469 * a passing stranger may have started :-S
5470 */
5471 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5472 atomic_add_int(&stcb->asoc.refcnt, 1);
5473
5474 SCTP_TCB_UNLOCK(stcb);
5475 SCTP_INP_INFO_WLOCK();
5476 SCTP_INP_WLOCK(inp);
5477 SCTP_TCB_LOCK(stcb);
5478 }
5479 /* Double check the GONE flag */
5480 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5481 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5482 /* nothing around */
5483 so = NULL;
5484
5485 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5486 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5487 /*
5488 * For TCP type we need special handling when we are
5489 * connected. We also include the peel'ed off ones to.
5490 */
5491 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5492 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5493 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5494 if (so) {
5495 SOCK_LOCK(so);
5496 if (so->so_rcv.sb_cc == 0) {
5497 so->so_state &= ~(SS_ISCONNECTING |
5498 SS_ISDISCONNECTING |
5499 SS_ISCONFIRMING |
5500 SS_ISCONNECTED);
5501 }
5502#if defined(__APPLE__)
5503 socantrcvmore(so);
5504#else
5505 socantrcvmore_locked(so);
5506#endif
5507 sctp_sowwakeup(inp, so);
5508 sctp_sorwakeup(inp, so);
5509 SCTP_SOWAKEUP(so);
5510 }
5511 }
5512 }
5513
5514 /* Make it invalid too, that way if its
5515 * about to run it will abort and return.
5516 */
5517 /* re-increment the lock */
5518 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5519 atomic_add_int(&stcb->asoc.refcnt, -1);
5520 }
5521 if (stcb->asoc.refcnt) {
5522 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
5523 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5524 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5525 SCTP_INP_INFO_WUNLOCK();
5526 SCTP_INP_WUNLOCK(inp);
5527 }
5528 SCTP_TCB_UNLOCK(stcb);
5529 return (0);
5530 }
5531 asoc->state = 0;
5532 if (inp->sctp_tcbhash) {
5533 LIST_REMOVE(stcb, sctp_tcbhash);
5534 }
5535 if (stcb->asoc.in_asocid_hash) {
5536 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5537 }
5538 /* Now lets remove it from the list of ALL associations in the EP */
5539 LIST_REMOVE(stcb, sctp_tcblist);
5540 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5541 SCTP_INP_INCR_REF(inp);
5542 SCTP_INP_WUNLOCK(inp);
5543 }
5544 /* pull from vtag hash */
5545 LIST_REMOVE(stcb, sctp_asocs);
5546 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
5547 inp->sctp_lport, stcb->rport);
5548
5549 /* Now restop the timers to be sure
5550 * this is paranoia at is finest!
5551 */
5552 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
5553 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
5554 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
5555 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
5556 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
5557 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
5558 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
5559 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5560 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
5561 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
5562 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
5563 }
5564
5565 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
5566 /*
5567 * The chunk lists and such SHOULD be empty but we check them just
5568 * in case.
5569 */
5570 /* anything on the wheel needs to be removed */
5571 for (i = 0; i < asoc->streamoutcnt; i++) {
5572 struct sctp_stream_out *outs;
5573
5574 outs = &asoc->strmout[i];
5575 /* now clean up any chunks here */
5576 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
5577 TAILQ_REMOVE(&outs->outqueue, sp, next);
tuexen30ca8172012-07-17 13:09:48 +00005578 sctp_free_spbufspace(stcb, asoc, sp);
tuexendd729232011-11-01 23:04:43 +00005579 if (sp->data) {
5580 if (so) {
5581 /* Still an open socket - report */
5582 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
tuexenda53ff02012-05-14 09:00:59 +00005583 0, (void *)sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005584 }
5585 if (sp->data) {
5586 sctp_m_freem(sp->data);
5587 sp->data = NULL;
5588 sp->tail_mbuf = NULL;
tuexen30ca8172012-07-17 13:09:48 +00005589 sp->length = 0;
tuexendd729232011-11-01 23:04:43 +00005590 }
5591 }
5592 if (sp->net) {
5593 sctp_free_remote_addr(sp->net);
5594 sp->net = NULL;
5595 }
tuexen30ca8172012-07-17 13:09:48 +00005596 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005597 }
5598 }
5599 /*sa_ignore FREED_MEMORY*/
5600 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5601 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5602 SCTP_FREE(strrst, SCTP_M_STRESET);
5603 }
5604 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5605 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5606 if (sq->data) {
5607 sctp_m_freem(sq->data);
5608 sq->data = NULL;
5609 }
5610 sctp_free_remote_addr(sq->whoFrom);
5611 sq->whoFrom = NULL;
5612 sq->stcb = NULL;
5613 /* Free the ctl entry */
5614 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
5615 SCTP_DECR_READQ_COUNT();
5616 /*sa_ignore FREED_MEMORY*/
5617 }
5618 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5619 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5620 if (chk->data) {
5621 sctp_m_freem(chk->data);
5622 chk->data = NULL;
5623 }
5624 if (chk->holds_key_ref)
5625 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5626 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5627 SCTP_DECR_CHK_COUNT();
5628 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5629 asoc->free_chunk_cnt--;
5630 /*sa_ignore FREED_MEMORY*/
5631 }
5632 /* pending send queue SHOULD be empty */
5633 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5634 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5635 if (chk->data) {
5636 if (so) {
5637 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005638 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5639 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005640 }
5641 if (chk->data) {
5642 sctp_m_freem(chk->data);
5643 chk->data = NULL;
5644 }
5645 }
5646 if (chk->holds_key_ref)
5647 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5648 if (chk->whoTo) {
5649 sctp_free_remote_addr(chk->whoTo);
5650 chk->whoTo = NULL;
5651 }
5652 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5653 SCTP_DECR_CHK_COUNT();
5654 /*sa_ignore FREED_MEMORY*/
5655 }
5656 /* sent queue SHOULD be empty */
5657 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
5658 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5659 if (chk->data) {
5660 if (so) {
5661 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005662 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5663 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005664 }
5665 if (chk->data) {
5666 sctp_m_freem(chk->data);
5667 chk->data = NULL;
5668 }
5669 }
5670 if (chk->holds_key_ref)
5671 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5672 sctp_free_remote_addr(chk->whoTo);
5673 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5674 SCTP_DECR_CHK_COUNT();
5675 /*sa_ignore FREED_MEMORY*/
5676 }
5677 /* control queue MAY not be empty */
5678 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5679 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5680 if (chk->data) {
5681 sctp_m_freem(chk->data);
5682 chk->data = NULL;
5683 }
5684 if (chk->holds_key_ref)
5685 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5686 sctp_free_remote_addr(chk->whoTo);
5687 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5688 SCTP_DECR_CHK_COUNT();
5689 /*sa_ignore FREED_MEMORY*/
5690 }
5691 /* ASCONF queue MAY not be empty */
5692 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5693 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5694 if (chk->data) {
5695 sctp_m_freem(chk->data);
5696 chk->data = NULL;
5697 }
5698 if (chk->holds_key_ref)
5699 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5700 sctp_free_remote_addr(chk->whoTo);
5701 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5702 SCTP_DECR_CHK_COUNT();
5703 /*sa_ignore FREED_MEMORY*/
5704 }
5705 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5706 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5707 if (chk->data) {
5708 sctp_m_freem(chk->data);
5709 chk->data = NULL;
5710 }
5711 if (chk->holds_key_ref)
5712 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5713 sctp_free_remote_addr(chk->whoTo);
5714 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5715 SCTP_DECR_CHK_COUNT();
5716 /*sa_ignore FREED_MEMORY*/
5717 }
5718
5719 if (asoc->mapping_array) {
5720 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5721 asoc->mapping_array = NULL;
5722 }
5723 if (asoc->nr_mapping_array) {
5724 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5725 asoc->nr_mapping_array = NULL;
5726 }
5727 /* the stream outs */
5728 if (asoc->strmout) {
5729 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5730 asoc->strmout = NULL;
5731 }
5732 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5733 if (asoc->strmin) {
5734 struct sctp_queued_to_read *ctl, *nctl;
5735
5736 for (i = 0; i < asoc->streamincnt; i++) {
5737 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
5738 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
5739 sctp_free_remote_addr(ctl->whoFrom);
5740 if (ctl->data) {
5741 sctp_m_freem(ctl->data);
5742 ctl->data = NULL;
5743 }
5744 /*
5745 * We don't free the address here
5746 * since all the net's were freed
5747 * above.
5748 */
5749 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
5750 SCTP_DECR_READQ_COUNT();
5751 }
5752 }
5753 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5754 asoc->strmin = NULL;
5755 }
5756 asoc->streamincnt = 0;
5757 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5758#ifdef INVARIANTS
5759 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5760 panic("no net's left alloc'ed, or list points to itself");
5761 }
5762#endif
5763 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5764 sctp_free_remote_addr(net);
5765 }
5766 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5767 /*sa_ignore FREED_MEMORY*/
5768 sctp_remove_laddr(laddr);
5769 }
5770
5771 /* pending asconf (address) parameters */
5772 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5773 /*sa_ignore FREED_MEMORY*/
5774 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5775 SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5776 }
5777 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5778 /*sa_ignore FREED_MEMORY*/
5779 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5780 if (aack->data != NULL) {
5781 sctp_m_freem(aack->data);
5782 }
5783 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5784 }
5785 /* clean up auth stuff */
5786 if (asoc->local_hmacs)
5787 sctp_free_hmaclist(asoc->local_hmacs);
5788 if (asoc->peer_hmacs)
5789 sctp_free_hmaclist(asoc->peer_hmacs);
5790
5791 if (asoc->local_auth_chunks)
5792 sctp_free_chunklist(asoc->local_auth_chunks);
5793 if (asoc->peer_auth_chunks)
5794 sctp_free_chunklist(asoc->peer_auth_chunks);
5795
5796 sctp_free_authinfo(&asoc->authinfo);
5797
5798 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5799 LIST_REMOVE(shared_key, next);
5800 sctp_free_sharedkey(shared_key);
5801 /*sa_ignore FREED_MEMORY*/
5802 }
5803
5804 /* Insert new items here :> */
5805
5806 /* Get rid of LOCK */
5807 SCTP_TCB_LOCK_DESTROY(stcb);
5808 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5809 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5810 SCTP_INP_INFO_WUNLOCK();
5811 SCTP_INP_RLOCK(inp);
5812 }
5813#if defined(__APPLE__) /* TEMP CODE */
5814 stcb->freed_from_where = from_location;
5815#endif
5816#ifdef SCTP_TRACK_FREED_ASOCS
5817 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5818 /* now clean up the tasoc itself */
5819 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5820 SCTP_DECR_ASOC_COUNT();
5821 } else {
5822 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5823 }
5824#else
5825 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5826 SCTP_DECR_ASOC_COUNT();
5827#endif
5828 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5829 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5830 /* If its NOT the inp_free calling us AND
5831 * sctp_close as been called, we
5832 * call back...
5833 */
5834 SCTP_INP_RUNLOCK(inp);
5835 /* This will start the kill timer (if we are
5836 * the last one) since we hold an increment yet. But
5837 * this is the only safe way to do this
5838 * since otherwise if the socket closes
5839 * at the same time we are here we might
5840 * collide in the cleanup.
5841 */
5842 sctp_inpcb_free(inp,
5843 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5844 SCTP_CALLED_DIRECTLY_NOCMPSET);
5845 SCTP_INP_DECR_REF(inp);
5846 goto out_of;
5847 } else {
5848 /* The socket is still open. */
5849 SCTP_INP_DECR_REF(inp);
5850 }
5851 }
5852 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5853 SCTP_INP_RUNLOCK(inp);
5854 }
5855 out_of:
5856 /* destroyed the asoc */
5857#ifdef SCTP_LOG_CLOSING
5858 sctp_log_closing(inp, NULL, 11);
5859#endif
5860 return (1);
5861}
5862
5863
5864
5865/*
5866 * determine if a destination is "reachable" based upon the addresses bound
5867 * to the current endpoint (e.g. only v4 or v6 currently bound)
5868 */
5869/*
5870 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5871 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5872 * types bound as its endpoint
5873 */
5874int
5875sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5876{
5877 struct sctp_inpcb *inp;
5878 int answer;
5879
5880 /*
5881 * No locks here, the TCB, in all cases is already locked and an
5882 * assoc is up. There is either a INP lock by the caller applied (in
5883 * asconf case when deleting an address) or NOT in the HB case,
5884 * however if HB then the INP increment is up and the INP will not
5885 * be removed (on top of the fact that we have a TCB lock). So we
5886 * only want to read the sctp_flags, which is either bound-all or
5887 * not.. no protection needed since once an assoc is up you can't be
5888 * changing your binding.
5889 */
5890 inp = stcb->sctp_ep;
5891 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5892 /* if bound all, destination is not restricted */
5893 /*
5894 * RRS: Question during lock work: Is this correct? If you
5895 * are bound-all you still might need to obey the V4--V6
5896 * flags??? IMO this bound-all stuff needs to be removed!
5897 */
5898 return (1);
5899 }
5900 /* NOTE: all "scope" checks are done when local addresses are added */
5901 switch (destaddr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00005902#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00005903 case AF_INET6:
5904#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5905 answer = inp->inp_vflag & INP_IPV6;
5906#else
5907 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5908#endif
5909 break;
tuexen310f1bc2012-07-15 11:20:56 +00005910#endif
5911#ifdef INET
tuexendd729232011-11-01 23:04:43 +00005912 case AF_INET:
5913#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5914 answer = inp->inp_vflag & INP_IPV4;
5915#else
5916 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5917#endif
tuexen46570542012-07-14 20:37:24 +00005918 break;
tuexen310f1bc2012-07-15 11:20:56 +00005919#endif
tuexen5d9b9742012-07-11 21:52:01 +00005920#if defined(__Userspace__)
5921 case AF_CONN:
5922 answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
tuexendd729232011-11-01 23:04:43 +00005923 break;
tuexen5d9b9742012-07-11 21:52:01 +00005924#endif
tuexendd729232011-11-01 23:04:43 +00005925 default:
5926 /* invalid family, so it's unreachable */
5927 answer = 0;
5928 break;
5929 }
5930 return (answer);
5931}
5932
5933/*
5934 * update the inp_vflags on an endpoint
5935 */
5936static void
5937sctp_update_ep_vflag(struct sctp_inpcb *inp)
5938{
5939 struct sctp_laddr *laddr;
5940
5941 /* first clear the flag */
5942#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5943 inp->inp_vflag = 0;
5944#else
5945 inp->ip_inp.inp.inp_vflag = 0;
5946#endif
5947 /* set the flag based on addresses on the ep list */
5948 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5949 if (laddr->ifa == NULL) {
5950 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
5951 __FUNCTION__);
5952 continue;
5953 }
5954
5955 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5956 continue;
5957 }
5958 switch (laddr->ifa->address.sa.sa_family) {
5959#ifdef INET6
5960 case AF_INET6:
5961#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5962 inp->inp_vflag |= INP_IPV6;
5963#else
5964 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5965#endif
5966 break;
5967#endif
5968#ifdef INET
5969 case AF_INET:
5970#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5971 inp->inp_vflag |= INP_IPV4;
5972#else
5973 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5974#endif
5975 break;
5976#endif
tuexen5d9b9742012-07-11 21:52:01 +00005977#if defined(__Userspace__)
5978 case AF_CONN:
5979 inp->ip_inp.inp.inp_vflag |= INP_CONN;
5980 break;
5981#endif
tuexendd729232011-11-01 23:04:43 +00005982 default:
5983 break;
5984 }
5985 }
5986}
5987
5988/*
5989 * Add the address to the endpoint local address list There is nothing to be
5990 * done if we are bound to all addresses
5991 */
5992void
5993sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5994{
5995 struct sctp_laddr *laddr;
tuexen63fc0bb2011-12-27 12:24:52 +00005996 int fnd, error = 0;
tuexendd729232011-11-01 23:04:43 +00005997
5998 fnd = 0;
5999
6000 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6001 /* You are already bound to all. You have it already */
6002 return;
6003 }
6004#ifdef INET6
6005 if (ifa->address.sa.sa_family == AF_INET6) {
6006 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6007 /* Can't bind a non-useable addr. */
6008 return;
6009 }
6010 }
6011#endif
6012 /* first, is it already present? */
6013 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6014 if (laddr->ifa == ifa) {
6015 fnd = 1;
6016 break;
6017 }
6018 }
6019
6020 if (fnd == 0) {
6021 /* Not in the ep list */
6022 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
6023 if (error != 0)
6024 return;
6025 inp->laddr_count++;
6026 /* update inp_vflag flags */
6027 switch (ifa->address.sa.sa_family) {
6028#ifdef INET6
6029 case AF_INET6:
6030#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6031 inp->inp_vflag |= INP_IPV6;
6032#else
6033 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6034#endif
6035 break;
6036#endif
tuexen310f1bc2012-07-15 11:20:56 +00006037#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006038 case AF_INET:
6039#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6040 inp->inp_vflag |= INP_IPV4;
6041#else
6042 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6043#endif
6044 break;
6045#endif
tuexen5d9b9742012-07-11 21:52:01 +00006046#if defined(__Userspace__)
6047 case AF_CONN:
6048 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6049 break;
6050#endif
tuexendd729232011-11-01 23:04:43 +00006051 default:
6052 break;
6053 }
6054 }
6055 return;
6056}
6057
6058
6059/*
6060 * select a new (hopefully reachable) destination net (should only be used
6061 * when we deleted an ep addr that is the only usable source address to reach
6062 * the destination net)
6063 */
6064static void
6065sctp_select_primary_destination(struct sctp_tcb *stcb)
6066{
6067 struct sctp_nets *net;
6068
6069 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6070 /* for now, we'll just pick the first reachable one we find */
6071 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6072 continue;
6073 if (sctp_destination_is_reachable(stcb,
6074 (struct sockaddr *)&net->ro._l_addr)) {
6075 /* found a reachable destination */
6076 stcb->asoc.primary_destination = net;
6077 }
6078 }
6079 /* I can't there from here! ...we're gonna die shortly... */
6080}
6081
6082
6083/*
6084 * Delete the address from the endpoint local address list There is nothing
6085 * to be done if we are bound to all addresses
6086 */
6087void
6088sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6089{
6090 struct sctp_laddr *laddr;
6091 int fnd;
6092
6093 fnd = 0;
6094 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6095 /* You are already bound to all. You have it already */
6096 return;
6097 }
6098 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6099 if (laddr->ifa == ifa) {
6100 fnd = 1;
6101 break;
6102 }
6103 }
6104 if (fnd && (inp->laddr_count < 2)) {
6105 /* can't delete unless there are at LEAST 2 addresses */
6106 return;
6107 }
6108 if (fnd) {
6109 /*
6110 * clean up any use of this address go through our
6111 * associations and clear any last_used_address that match
6112 * this one for each assoc, see if a new primary_destination
6113 * is needed
6114 */
6115 struct sctp_tcb *stcb;
6116
6117 /* clean up "next_addr_touse" */
6118 if (inp->next_addr_touse == laddr)
6119 /* delete this address */
6120 inp->next_addr_touse = NULL;
6121
6122 /* clean up "last_used_address" */
6123 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6124 struct sctp_nets *net;
6125 SCTP_TCB_LOCK(stcb);
6126 if (stcb->asoc.last_used_address == laddr)
6127 /* delete this address */
6128 stcb->asoc.last_used_address = NULL;
6129 /* Now spin through all the nets and purge any ref to laddr */
6130 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6131 if (net->ro._s_addr &&
6132 (net->ro._s_addr->ifa == laddr->ifa)) {
6133 /* Yep, purge src address selected */
6134 sctp_rtentry_t *rt;
6135
6136 /* delete this address if cached */
6137 rt = net->ro.ro_rt;
6138 if (rt != NULL) {
6139 RTFREE(rt);
6140 net->ro.ro_rt = NULL;
6141 }
6142 sctp_free_ifa(net->ro._s_addr);
6143 net->ro._s_addr = NULL;
6144 net->src_addr_selected = 0;
6145 }
6146 }
6147 SCTP_TCB_UNLOCK(stcb);
6148 } /* for each tcb */
6149 /* remove it from the ep list */
6150 sctp_remove_laddr(laddr);
6151 inp->laddr_count--;
6152 /* update inp_vflag flags */
6153 sctp_update_ep_vflag(inp);
6154 }
6155 return;
6156}
6157
6158/*
6159 * Add the address to the TCB local address restricted list.
6160 * This is a "pending" address list (eg. addresses waiting for an
6161 * ASCONF-ACK response) and cannot be used as a valid source address.
6162 */
6163void
6164sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6165{
tuexendd729232011-11-01 23:04:43 +00006166 struct sctp_laddr *laddr;
6167 struct sctpladdr *list;
6168
6169 /*
6170 * Assumes TCB is locked.. and possibly the INP. May need to
6171 * confirm/fix that if we need it and is not the case.
6172 */
6173 list = &stcb->asoc.sctp_restricted_addrs;
6174
tuexendd729232011-11-01 23:04:43 +00006175#ifdef INET6
6176 if (ifa->address.sa.sa_family == AF_INET6) {
6177 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6178 /* Can't bind a non-existent addr. */
6179 return;
6180 }
6181 }
6182#endif
6183 /* does the address already exist? */
6184 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6185 if (laddr->ifa == ifa) {
6186 return;
6187 }
6188 }
6189
6190 /* add to the list */
6191 (void)sctp_insert_laddr(list, ifa, 0);
6192 return;
6193}
6194
6195/*
6196 * insert an laddr entry with the given ifa for the desired list
6197 */
6198int
6199sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
6200{
6201 struct sctp_laddr *laddr;
6202
6203 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6204 if (laddr == NULL) {
6205 /* out of memory? */
6206 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
6207 return (EINVAL);
6208 }
6209 SCTP_INCR_LADDR_COUNT();
6210 bzero(laddr, sizeof(*laddr));
6211 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
6212 laddr->ifa = ifa;
6213 laddr->action = act;
6214 atomic_add_int(&ifa->refcount, 1);
6215 /* insert it */
6216 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
6217
6218 return (0);
6219}
6220
6221/*
6222 * Remove an laddr entry from the local address list (on an assoc)
6223 */
6224void
6225sctp_remove_laddr(struct sctp_laddr *laddr)
6226{
6227
6228 /* remove from the list */
6229 LIST_REMOVE(laddr, sctp_nxt_addr);
6230 sctp_free_ifa(laddr->ifa);
6231 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
6232 SCTP_DECR_LADDR_COUNT();
6233}
6234
6235/*
6236 * Remove a local address from the TCB local address restricted list
6237 */
6238void
6239sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6240{
6241 struct sctp_inpcb *inp;
6242 struct sctp_laddr *laddr;
6243
6244 /*
6245 * This is called by asconf work. It is assumed that a) The TCB is
6246 * locked and b) The INP is locked. This is true in as much as I can
6247 * trace through the entry asconf code where I did these locks.
6248 * Again, the ASCONF code is a bit different in that it does lock
6249 * the INP during its work often times. This must be since we don't
6250 * want other proc's looking up things while what they are looking
6251 * up is changing :-D
6252 */
6253
6254 inp = stcb->sctp_ep;
6255 /* if subset bound and don't allow ASCONF's, can't delete last */
6256 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6257 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6258 if (stcb->sctp_ep->laddr_count < 2) {
6259 /* can't delete last address */
6260 return;
6261 }
6262 }
6263 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6264 /* remove the address if it exists */
6265 if (laddr->ifa == NULL)
6266 continue;
6267 if (laddr->ifa == ifa) {
6268 sctp_remove_laddr(laddr);
6269 return;
6270 }
6271 }
6272
6273 /* address not found! */
6274 return;
6275}
6276
6277#if defined(__FreeBSD__)
6278/*
6279 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
6280 */
6281/* sysctl */
6282static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6283static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
6284#endif /* FreeBSD || APPLE */
6285
6286
6287
6288#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexen63fc0bb2011-12-27 12:24:52 +00006289struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6290int *sctp_cpuarry = NULL;
tuexendd729232011-11-01 23:04:43 +00006291void
6292sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6293{
6294 /* Queue a packet to a processor for the specified core */
6295 struct sctp_mcore_queue *qent;
6296 struct sctp_mcore_ctrl *wkq;
tuexen63fc0bb2011-12-27 12:24:52 +00006297 int need_wake = 0;
tuexendd729232011-11-01 23:04:43 +00006298 if (sctp_mcore_workers == NULL) {
6299 /* Something went way bad during setup */
6300 sctp_input_with_port(m, off, 0);
6301 return;
6302 }
6303 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6304 (sizeof(struct sctp_mcore_queue)),
6305 SCTP_M_MCORE);
6306 if (qent == NULL) {
6307 /* This is trouble */
6308 sctp_input_with_port(m, off, 0);
6309 return;
6310 }
6311#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6312 qent->vn = curvnet;
6313#endif
6314 qent->m = m;
6315 qent->off = off;
6316 qent->v6 = 0;
6317 wkq = &sctp_mcore_workers[cpu_to_use];
6318 SCTP_MCORE_QLOCK(wkq);
6319
6320 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6321 if (wkq->running == 0) {
6322 need_wake = 1;
6323 }
6324 SCTP_MCORE_QUNLOCK(wkq);
tuexen63fc0bb2011-12-27 12:24:52 +00006325 if (need_wake) {
tuexendd729232011-11-01 23:04:43 +00006326 wakeup(&wkq->running);
6327 }
6328}
6329
6330static void
6331sctp_mcore_thread(void *arg)
6332{
6333
6334 struct sctp_mcore_ctrl *wkq;
6335 struct sctp_mcore_queue *qent;
6336
6337 wkq = (struct sctp_mcore_ctrl *)arg;
6338 struct mbuf *m;
6339 int off, v6;
6340
6341 /* Wait for first tickle */
6342 SCTP_MCORE_LOCK(wkq);
6343 wkq->running = 0;
6344 msleep(&wkq->running,
6345 &wkq->core_mtx,
6346 0, "wait for pkt", 0);
6347 SCTP_MCORE_UNLOCK(wkq);
6348
6349 /* Bind to our cpu */
6350 thread_lock(curthread);
6351 sched_bind(curthread, wkq->cpuid);
6352 thread_unlock(curthread);
6353
6354 /* Now lets start working */
6355 SCTP_MCORE_LOCK(wkq);
6356 /* Now grab lock and go */
tuexen9784e9a2011-12-18 13:04:23 +00006357 for (;;) {
tuexendd729232011-11-01 23:04:43 +00006358 SCTP_MCORE_QLOCK(wkq);
6359 skip_sleep:
6360 wkq->running = 1;
6361 qent = TAILQ_FIRST(&wkq->que);
6362 if (qent) {
6363 TAILQ_REMOVE(&wkq->que, qent, next);
6364 SCTP_MCORE_QUNLOCK(wkq);
6365#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6366 CURVNET_SET(qent->vn);
6367#endif
6368 m = qent->m;
6369 off = qent->off;
6370 v6 = qent->v6;
6371 SCTP_FREE(qent, SCTP_M_MCORE);
6372 if (v6 == 0) {
6373 sctp_input_with_port(m, off, 0);
6374 } else {
tuexencb5fe8d2012-05-04 09:50:27 +00006375 SCTP_PRINTF("V6 not yet supported\n");
tuexendd729232011-11-01 23:04:43 +00006376 sctp_m_freem(m);
6377 }
6378#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6379 CURVNET_RESTORE();
6380#endif
6381 SCTP_MCORE_QLOCK(wkq);
6382 }
6383 wkq->running = 0;
6384 if (!TAILQ_EMPTY(&wkq->que)) {
6385 goto skip_sleep;
6386 }
6387 SCTP_MCORE_QUNLOCK(wkq);
6388 msleep(&wkq->running,
6389 &wkq->core_mtx,
6390 0, "wait for pkt", 0);
tuexen63fc0bb2011-12-27 12:24:52 +00006391 }
tuexendd729232011-11-01 23:04:43 +00006392}
6393
6394static void
6395sctp_startup_mcore_threads(void)
6396{
6397 int i, cpu;
6398
6399 if (mp_ncpus == 1)
6400 return;
6401
6402 if (sctp_mcore_workers != NULL) {
6403 /* Already been here in some previous
6404 * vnet?
6405 */
6406 return;
6407 }
6408 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6409 ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6410 SCTP_M_MCORE);
6411 if (sctp_mcore_workers == NULL) {
6412 /* TSNH I hope */
6413 return;
6414 }
tuexen09631162012-04-18 11:23:02 +00006415 memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
tuexendd729232011-11-01 23:04:43 +00006416 sizeof(struct sctp_mcore_ctrl)));
6417 /* Init the structures */
tuexen63fc0bb2011-12-27 12:24:52 +00006418 for (i = 0; i<=mp_maxid; i++) {
tuexendd729232011-11-01 23:04:43 +00006419 TAILQ_INIT(&sctp_mcore_workers[i].que);
6420 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6421 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6422 sctp_mcore_workers[i].cpuid = i;
6423 }
6424 if (sctp_cpuarry == NULL) {
6425 SCTP_MALLOC(sctp_cpuarry, int *,
6426 (mp_ncpus * sizeof(int)),
6427 SCTP_M_MCORE);
tuexen63fc0bb2011-12-27 12:24:52 +00006428 i = 0;
tuexendd729232011-11-01 23:04:43 +00006429 CPU_FOREACH(cpu) {
6430 sctp_cpuarry[i] = cpu;
6431 i++;
6432 }
6433 }
6434
6435 /* Now start them all */
6436 CPU_FOREACH(cpu) {
6437#if __FreeBSD_version <= 701000
6438 (void)kthread_create(sctp_mcore_thread,
6439 (void *)&sctp_mcore_workers[cpu],
6440 &sctp_mcore_workers[cpu].thread_proc,
6441 RFPROC,
6442 SCTP_KTHREAD_PAGES,
6443 SCTP_MCORE_NAME);
6444
6445#else
6446 (void)kproc_create(sctp_mcore_thread,
6447 (void *)&sctp_mcore_workers[cpu],
6448 &sctp_mcore_workers[cpu].thread_proc,
6449 RFPROC,
6450 SCTP_KTHREAD_PAGES,
6451 SCTP_MCORE_NAME);
6452#endif
tuexen09631162012-04-18 11:23:02 +00006453
tuexendd729232011-11-01 23:04:43 +00006454 }
6455}
6456#endif
tuexen76eb2092012-04-01 16:35:28 +00006457#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000
tuexendd729232011-11-01 23:04:43 +00006458static struct mbuf *
6459sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6460{
6461 struct ip *ip;
6462 struct sctphdr *sh;
6463 int offset;
6464 uint32_t flowid, tag;
tuexen76eb2092012-04-01 16:35:28 +00006465
tuexendd729232011-11-01 23:04:43 +00006466 /*
6467 * No flow id built by lower layers fix it so we
6468 * create one.
6469 */
6470 ip = mtod(m, struct ip *);
tuexen664967a2012-06-28 16:24:36 +00006471 offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
tuexendd729232011-11-01 23:04:43 +00006472 if (SCTP_BUF_LEN(m) < offset) {
tuexen664967a2012-06-28 16:24:36 +00006473 if ((m = m_pullup(m, offset)) == NULL) {
tuexendd729232011-11-01 23:04:43 +00006474 SCTP_STAT_INCR(sctps_hdrops);
6475 return (NULL);
6476 }
6477 ip = mtod(m, struct ip *);
6478 }
6479 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
tuexendd729232011-11-01 23:04:43 +00006480 tag = htonl(sh->v_tag);
6481 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6482 m->m_pkthdr.flowid = flowid;
6483 m->m_flags |= M_FLOWID;
6484 return (m);
6485}
6486#endif
6487
6488void
6489sctp_pcb_init()
6490{
6491 /*
6492 * SCTP initialization for the PCB structures should be called by
6493 * the sctp_init() funciton.
6494 */
6495 int i;
6496 struct timeval tv;
tuexen6019b072011-12-15 18:38:03 +00006497
tuexendd729232011-11-01 23:04:43 +00006498 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6499 /* error I was called twice */
6500 return;
6501 }
6502 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6503
6504#if defined(SCTP_LOCAL_TRACE_BUF)
6505#if defined(__Windows__)
6506 if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
6507 bzero(SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
6508 }
6509#else
6510 bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
6511#endif
6512#endif
6513#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6514 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6515 ((mp_maxid+1) * sizeof(struct sctpstat)),
6516 SCTP_M_MCORE);
6517#endif
6518 (void)SCTP_GETTIME_TIMEVAL(&tv);
6519#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6520 bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid+1)));
6521 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6522 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6523#else
6524 bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
6525 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6526 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6527#endif
6528 /* init the empty list of (All) Endpoints */
6529 LIST_INIT(&SCTP_BASE_INFO(listhead));
6530#if defined(__APPLE__)
6531 LIST_INIT(&SCTP_BASE_INFO(inplisthead));
6532#endif
6533
6534
6535 /* init the hash table of endpoints */
6536#if defined(__FreeBSD__)
6537#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
6538 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6539 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6540 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
6541#else
6542 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
6543 SCTP_BASE_SYSCTL(sctp_hashtblsize));
6544 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
6545 SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6546 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
6547 SCTP_BASE_SYSCTL(sctp_chunkscale));
6548#endif
6549#endif
6550 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6551 &SCTP_BASE_INFO(hashasocmark));
6552 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6553 &SCTP_BASE_INFO(hashmark));
6554 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6555 &SCTP_BASE_INFO(hashtcpmark));
6556 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
6557
6558
6559 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6560 &SCTP_BASE_INFO(hashvrfmark));
6561
6562 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6563 &SCTP_BASE_INFO(vrf_ifn_hashmark));
6564 /* init the zones */
6565 /*
6566 * FIX ME: Should check for NULL returns, but if it does fail we are
6567 * doomed to panic anyways... add later maybe.
6568 */
6569 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6570 sizeof(struct sctp_inpcb), maxsockets);
6571
6572 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6573 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6574
6575 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6576 sizeof(struct sctp_laddr),
6577 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6578
6579 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6580 sizeof(struct sctp_nets),
6581 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6582
6583 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6584 sizeof(struct sctp_tmit_chunk),
6585 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6586
6587 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6588 sizeof(struct sctp_queued_to_read),
6589 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6590
6591 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6592 sizeof(struct sctp_stream_queue_pending),
6593 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6594
6595 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6596 sizeof(struct sctp_asconf),
6597 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6598
6599 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6600 sizeof(struct sctp_asconf_ack),
6601 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6602
tuexen9784e9a2011-12-18 13:04:23 +00006603
tuexendd729232011-11-01 23:04:43 +00006604 /* Master Lock INIT for info structure */
6605#if defined(__APPLE__)
6606 /* allocate the lock group attribute for SCTP PCB mutexes */
6607 SCTP_BASE_INFO(mtx_grp_attr) = lck_grp_attr_alloc_init();
6608 lck_grp_attr_setdefault(SCTP_BASE_INFO(mtx_grp_attr));
6609 /* allocate the lock group for SCTP PCB mutexes */
6610 SCTP_BASE_INFO(mtx_grp) = lck_grp_alloc_init("sctppcb",
6611 SCTP_BASE_INFO(mtx_grp_attr));
6612 /* allocate the lock attribute for SCTP PCB mutexes */
6613 SCTP_BASE_INFO(mtx_attr) = lck_attr_alloc_init();
6614 lck_attr_setdefault(SCTP_BASE_INFO(mtx_attr));
tuexen9784e9a2011-12-18 13:04:23 +00006615#endif /* __APPLE__ */
tuexendd729232011-11-01 23:04:43 +00006616 SCTP_INP_INFO_LOCK_INIT();
6617 SCTP_STATLOG_INIT_LOCK();
6618
6619 SCTP_IPI_COUNT_INIT();
6620 SCTP_IPI_ADDR_INIT();
6621#ifdef SCTP_PACKET_LOGGING
6622 SCTP_IP_PKTLOG_INIT();
6623#endif
6624 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6625
6626 SCTP_WQ_ADDR_INIT();
6627 /* not sure if we need all the counts */
6628 SCTP_BASE_INFO(ipi_count_ep) = 0;
6629 /* assoc/tcb zone info */
6630 SCTP_BASE_INFO(ipi_count_asoc) = 0;
6631 /* local addrlist zone info */
6632 SCTP_BASE_INFO(ipi_count_laddr) = 0;
6633 /* remote addrlist zone info */
6634 SCTP_BASE_INFO(ipi_count_raddr) = 0;
6635 /* chunk info */
6636 SCTP_BASE_INFO(ipi_count_chunk) = 0;
6637
6638 /* socket queue zone info */
6639 SCTP_BASE_INFO(ipi_count_readq) = 0;
6640
6641 /* stream out queue cont */
6642 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6643
6644 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6645 SCTP_BASE_INFO(ipi_free_chunks) = 0;
6646
6647 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6648
6649 /* Init the TIMEWAIT list */
6650 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6651 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6652 }
6653
6654#if defined(SCTP_PROCESS_LEVEL_LOCKS)
tuexen5154a002011-12-23 18:47:55 +00006655#if defined(__Userspace_os_Windows)
tuexen6019b072011-12-15 18:38:03 +00006656 InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6657#else
tuexendd729232011-11-01 23:04:43 +00006658 (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6659#endif
tuexen6019b072011-12-15 18:38:03 +00006660#endif
tuexendd729232011-11-01 23:04:43 +00006661 sctp_startup_iterator();
6662
6663#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6664 sctp_startup_mcore_threads();
6665#endif
6666
6667#ifndef __Panda__
6668 /*
6669 * INIT the default VRF which for BSD is the only one, other O/S's
6670 * may have more. But initially they must start with one and then
6671 * add the VRF's as addresses are added.
6672 */
6673 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
6674#endif
tuexen76eb2092012-04-01 16:35:28 +00006675#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000
tuexendd729232011-11-01 23:04:43 +00006676 if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
tuexencb5fe8d2012-05-04 09:50:27 +00006677 SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
tuexendd729232011-11-01 23:04:43 +00006678 }
6679#endif
6680#if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6681 /* allocate the lock for the callout/timer queue */
6682 SCTP_TIMERQ_LOCK_INIT();
6683 TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6684#endif
6685#if defined(__Userspace__)
6686 mbuf_init(NULL);
6687 atomic_init();
tuexenc4178972012-07-15 20:40:54 +00006688#if defined(INET) || defined(INET6)
tuexen852c7962012-04-28 16:39:32 +00006689 recv_thread_init();
tuexendd729232011-11-01 23:04:43 +00006690#endif
tuexenc4178972012-07-15 20:40:54 +00006691#endif
tuexendd729232011-11-01 23:04:43 +00006692}
6693
6694/*
6695 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6696 */
6697void
6698sctp_pcb_finish(void)
6699{
6700 struct sctp_vrflist *vrf_bucket;
6701 struct sctp_vrf *vrf, *nvrf;
6702 struct sctp_ifn *ifn, *nifn;
6703 struct sctp_ifa *ifa, *nifa;
6704 struct sctpvtaghead *chain;
6705 struct sctp_tagblock *twait_block, *prev_twait_block;
6706 struct sctp_laddr *wi, *nwi;
6707 int i;
6708
6709#if defined(__FreeBSD__)
6710 /* Free BSD the it thread never exits
6711 * but we do clean up. The only way
6712 * freebsd reaches here if we have VRF's
6713 * but we still add the ifdef to make it
6714 * compile on old versions.
6715 */
6716 {
6717 struct sctp_iterator *it, *nit;
6718
6719 SCTP_IPI_ITERATOR_WQ_LOCK();
6720 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
6721 if (it->vn != curvnet) {
6722 continue;
tuexen09631162012-04-18 11:23:02 +00006723 }
tuexendd729232011-11-01 23:04:43 +00006724 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6725 if (it->function_atend != NULL) {
6726 (*it->function_atend) (it->pointer, it->val);
6727 }
6728 SCTP_FREE(it,SCTP_M_ITER);
6729 }
6730 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6731 SCTP_ITERATOR_LOCK();
6732 if ((sctp_it_ctl.cur_it) &&
6733 (sctp_it_ctl.cur_it->vn == curvnet)) {
6734 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6735 }
6736 SCTP_ITERATOR_UNLOCK();
6737 }
6738#else
6739 /* Notify the iterator to exit. */
6740 SCTP_IPI_ITERATOR_WQ_LOCK();
6741 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6742 sctp_wakeup_iterator();
6743 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6744#endif
6745#if defined(__APPLE__)
6746 SCTP_IPI_ITERATOR_WQ_LOCK();
6747 do {
6748 msleep(&sctp_it_ctl.iterator_flags,
6749 sctp_it_ctl.ipi_iterator_wq_mtx,
6750 0, "waiting_for_work", 0);
6751 } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6752 thread_deallocate(sctp_it_ctl.thread_proc);
6753 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6754 SCTP_IPI_ITERATOR_WQ_DESTROY();
6755 SCTP_ITERATOR_LOCK_DESTROY();
6756#endif
6757#if defined(__Windows__)
6758 if (sctp_it_ctl.iterator_thread_obj != NULL) {
6759 NTSTATUS status = STATUS_SUCCESS;
6760
6761 KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6762 status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6763 Executive,
6764 KernelMode,
6765 FALSE,
6766 NULL);
6767 ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6768 }
6769#endif
6770
6771 SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer));
6772 SCTP_WQ_ADDR_LOCK();
6773 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6774 LIST_REMOVE(wi, sctp_nxt_addr);
6775 SCTP_DECR_LADDR_COUNT();
6776 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6777 }
6778 SCTP_WQ_ADDR_UNLOCK();
tuexen09631162012-04-18 11:23:02 +00006779
tuexendd729232011-11-01 23:04:43 +00006780 /*
6781 * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6782 * is destroyed first).
6783 */
6784 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6785 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6786 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6787 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6788 /* free the ifa */
6789 LIST_REMOVE(ifa, next_bucket);
6790 LIST_REMOVE(ifa, next_ifa);
6791 SCTP_FREE(ifa, SCTP_M_IFA);
6792 }
6793 /* free the ifn */
6794 LIST_REMOVE(ifn, next_bucket);
6795 LIST_REMOVE(ifn, next_ifn);
6796 SCTP_FREE(ifn, SCTP_M_IFN);
6797 }
6798 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6799 /* free the vrf */
6800 LIST_REMOVE(vrf, next_vrf);
6801 SCTP_FREE(vrf, SCTP_M_VRF);
6802 }
6803 /* free the vrf hashes */
6804 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6805 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
tuexen9784e9a2011-12-18 13:04:23 +00006806#if defined(__Userspace__) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00006807 /* free memory allocated by getifaddrs call */
6808 freeifaddrs(g_interfaces);
6809#endif
6810
6811 /* free the TIMEWAIT list elements malloc'd in the function
6812 * sctp_add_vtag_to_timewait()...
6813 */
6814 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6815 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6816 if (!LIST_EMPTY(chain)) {
6817 prev_twait_block = NULL;
6818 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6819 if (prev_twait_block) {
6820 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6821 }
6822 prev_twait_block = twait_block;
6823 }
6824 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6825 }
6826 }
6827
6828 /* free the locks and mutexes */
6829#if defined(__APPLE__)
6830 SCTP_TIMERQ_LOCK_DESTROY();
6831#endif
6832#ifdef SCTP_PACKET_LOGGING
6833 SCTP_IP_PKTLOG_DESTROY();
6834#endif
6835 SCTP_IPI_ADDR_DESTROY();
6836#if defined(__APPLE__)
6837 SCTP_IPI_COUNT_DESTROY();
6838#endif
6839 SCTP_STATLOG_DESTROY();
6840#if !defined(__Userspace__)
6841 SCTP_INP_INFO_LOCK_DESTROY();
6842#endif
tuexen09631162012-04-18 11:23:02 +00006843
tuexendd729232011-11-01 23:04:43 +00006844 SCTP_WQ_ADDR_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006845
tuexendd729232011-11-01 23:04:43 +00006846#if defined(__APPLE__)
6847 lck_grp_attr_free(SCTP_BASE_INFO(mtx_grp_attr));
6848 lck_grp_free(SCTP_BASE_INFO(mtx_grp));
6849 lck_attr_free(SCTP_BASE_INFO(mtx_attr));
6850#endif
6851#if defined(__Userspace__)
6852 SCTP_TIMERQ_LOCK_DESTROY();
6853 SCTP_ZONE_DESTROY(zone_mbuf);
6854 SCTP_ZONE_DESTROY(zone_clust);
6855 SCTP_ZONE_DESTROY(zone_ext_refcnt);
6856#endif
6857#if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__)
6858 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6859 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6860 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6861 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6862 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6863 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6864 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6865 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6866 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6867#endif
6868 /* Get rid of other stuff to */
6869 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6870 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6871 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6872 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6873 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6874 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6875#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexen09631162012-04-18 11:23:02 +00006876 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
tuexendd729232011-11-01 23:04:43 +00006877#endif
6878}
6879
6880
6881int
6882sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
tuexen664967a2012-06-28 16:24:36 +00006883 int offset, int limit,
6884 struct sockaddr *src, struct sockaddr *dst,
tuexen9784e9a2011-12-18 13:04:23 +00006885 struct sockaddr *altsa)
tuexendd729232011-11-01 23:04:43 +00006886{
6887 /*
6888 * grub through the INIT pulling addresses and loading them to the
6889 * nets structure in the asoc. The from address in the mbuf should
6890 * also be loaded (if it is not already). This routine can be called
6891 * with either INIT or INIT-ACK's as long as the m points to the IP
6892 * packet and the offset points to the beginning of the parameters.
6893 */
tuexen63fc0bb2011-12-27 12:24:52 +00006894 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00006895 struct sctp_nets *net, *nnet, *net_tmp;
tuexendd729232011-11-01 23:04:43 +00006896 struct sctp_paramhdr *phdr, parm_buf;
6897 struct sctp_tcb *stcb_tmp;
6898 uint16_t ptype, plen;
6899 struct sockaddr *sa;
tuexendd729232011-11-01 23:04:43 +00006900 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
6901 struct sctp_auth_random *p_random = NULL;
6902 uint16_t random_len = 0;
6903 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
6904 struct sctp_auth_hmac_algo *hmacs = NULL;
6905 uint16_t hmacs_len = 0;
tuexen63fc0bb2011-12-27 12:24:52 +00006906 uint8_t saw_asconf = 0;
6907 uint8_t saw_asconf_ack = 0;
tuexendd729232011-11-01 23:04:43 +00006908 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
6909 struct sctp_auth_chunk_list *chunks = NULL;
6910 uint16_t num_chunks = 0;
6911 sctp_key_t *new_key;
6912 uint32_t keylen;
6913 int got_random = 0, got_hmacs = 0, got_chklist = 0;
6914 uint8_t ecn_allowed;
6915#ifdef INET
6916 struct sockaddr_in sin;
6917#endif
6918#ifdef INET6
6919 struct sockaddr_in6 sin6;
6920#endif
6921
6922 /* First get the destination address setup too. */
6923#ifdef INET
6924 memset(&sin, 0, sizeof(sin));
6925 sin.sin_family = AF_INET;
t00fcxen8fbaee32012-09-04 16:41:39 +00006926#ifdef HAVE_SIN_LEN
tuexendd729232011-11-01 23:04:43 +00006927 sin.sin_len = sizeof(sin);
6928#endif
6929 sin.sin_port = stcb->rport;
6930#endif
6931#ifdef INET6
6932 memset(&sin6, 0, sizeof(sin6));
6933 sin6.sin6_family = AF_INET6;
t00fcxen8fbaee32012-09-04 16:41:39 +00006934#ifdef HAVE_SIN6_LEN
tuexendd729232011-11-01 23:04:43 +00006935 sin6.sin6_len = sizeof(struct sockaddr_in6);
6936#endif
6937 sin6.sin6_port = stcb->rport;
6938#endif
tuexen664967a2012-06-28 16:24:36 +00006939 if (altsa) {
6940 sa = altsa;
6941 } else {
6942 sa = src;
tuexendd729232011-11-01 23:04:43 +00006943 }
6944 /* Turn off ECN until we get through all params */
6945 ecn_allowed = 0;
6946 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6947 /* mark all addresses that we have currently on the list */
6948 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
6949 }
6950 /* does the source address already exist? if so skip it */
tuexen63fc0bb2011-12-27 12:24:52 +00006951 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00006952 atomic_add_int(&stcb->asoc.refcnt, 1);
tuexen664967a2012-06-28 16:24:36 +00006953 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
tuexendd729232011-11-01 23:04:43 +00006954 atomic_add_int(&stcb->asoc.refcnt, -1);
6955
6956 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
6957 /* we must add the source address */
6958 /* no scope set here since we have a tcb already. */
6959 switch (sa->sa_family) {
6960#ifdef INET
6961 case AF_INET:
6962 if (stcb->asoc.ipv4_addr_legal) {
6963 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
6964 return (-1);
6965 }
6966 }
6967 break;
6968#endif
6969#ifdef INET6
6970 case AF_INET6:
6971 if (stcb->asoc.ipv6_addr_legal) {
6972 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6973 return (-2);
6974 }
6975 }
6976 break;
6977#endif
tuexen430ca6c2012-07-10 23:36:36 +00006978#if defined(__Userspace__)
6979 case AF_CONN:
6980 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6981 return (-2);
6982 }
6983 break;
6984#endif
tuexendd729232011-11-01 23:04:43 +00006985 default:
6986 break;
6987 }
6988 } else {
6989 if (net_tmp != NULL && stcb_tmp == stcb) {
6990 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
6991 } else if (stcb_tmp != stcb) {
6992 /* It belongs to another association? */
6993 if (stcb_tmp)
6994 SCTP_TCB_UNLOCK(stcb_tmp);
6995 return (-3);
6996 }
6997 }
6998 if (stcb->asoc.state == 0) {
6999 /* the assoc was freed? */
7000 return (-4);
7001 }
7002 /*
7003 * peer must explicitly turn this on. This may have been initialized
7004 * to be "on" in order to allow local addr changes while INIT's are
7005 * in flight.
7006 */
7007 stcb->asoc.peer_supports_asconf = 0;
7008 /* now we must go through each of the params. */
7009 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
7010 while (phdr) {
7011 ptype = ntohs(phdr->param_type);
7012 plen = ntohs(phdr->param_length);
7013 /*
tuexencb5fe8d2012-05-04 09:50:27 +00007014 * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
tuexendd729232011-11-01 23:04:43 +00007015 * (int)plen);
7016 */
7017 if (offset + plen > limit) {
7018 break;
7019 }
7020 if (plen == 0) {
7021 break;
7022 }
7023#ifdef INET
7024 if (ptype == SCTP_IPV4_ADDRESS) {
7025 if (stcb->asoc.ipv4_addr_legal) {
7026 struct sctp_ipv4addr_param *p4, p4_buf;
7027
7028 /* ok get the v4 address and check/add */
7029 phdr = sctp_get_next_param(m, offset,
7030 (struct sctp_paramhdr *)&p4_buf,
7031 sizeof(p4_buf));
7032 if (plen != sizeof(struct sctp_ipv4addr_param) ||
7033 phdr == NULL) {
7034 return (-5);
7035 }
7036 p4 = (struct sctp_ipv4addr_param *)phdr;
7037 sin.sin_addr.s_addr = p4->addr;
7038 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7039 /* Skip multi-cast addresses */
7040 goto next_param;
7041 }
7042 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
7043 (sin.sin_addr.s_addr == INADDR_ANY)) {
7044 goto next_param;
7045 }
7046 sa = (struct sockaddr *)&sin;
7047 inp = stcb->sctp_ep;
7048 atomic_add_int(&stcb->asoc.refcnt, 1);
7049 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007050 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007051 atomic_add_int(&stcb->asoc.refcnt, -1);
7052
7053 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7054 inp == NULL) {
7055 /* we must add the source address */
7056 /*
7057 * no scope set since we have a tcb
7058 * already
7059 */
7060
7061 /*
7062 * we must validate the state again
7063 * here
7064 */
7065 add_it_now:
7066 if (stcb->asoc.state == 0) {
7067 /* the assoc was freed? */
7068 return (-7);
7069 }
7070 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
7071 return (-8);
7072 }
7073 } else if (stcb_tmp == stcb) {
7074 if (stcb->asoc.state == 0) {
7075 /* the assoc was freed? */
7076 return (-10);
7077 }
7078 if (net != NULL) {
7079 /* clear flag */
7080 net->dest_state &=
7081 ~SCTP_ADDR_NOT_IN_ASSOC;
7082 }
7083 } else {
7084 /*
7085 * strange, address is in another
7086 * assoc? straighten out locks.
7087 */
7088 if (stcb_tmp) {
7089 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
7090 /* in setup state we abort this guy */
7091 sctp_abort_an_association(stcb_tmp->sctp_ep,
tuexenda53ff02012-05-14 09:00:59 +00007092 stcb_tmp, NULL, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007093 goto add_it_now;
7094 }
7095 SCTP_TCB_UNLOCK(stcb_tmp);
7096 }
7097
7098 if (stcb->asoc.state == 0) {
7099 /* the assoc was freed? */
7100 return (-12);
7101 }
7102 return (-13);
7103 }
7104 }
7105 } else
7106#endif
7107#ifdef INET6
7108 if (ptype == SCTP_IPV6_ADDRESS) {
7109 if (stcb->asoc.ipv6_addr_legal) {
7110 /* ok get the v6 address and check/add */
7111 struct sctp_ipv6addr_param *p6, p6_buf;
7112
7113 phdr = sctp_get_next_param(m, offset,
7114 (struct sctp_paramhdr *)&p6_buf,
7115 sizeof(p6_buf));
7116 if (plen != sizeof(struct sctp_ipv6addr_param) ||
7117 phdr == NULL) {
7118 return (-14);
7119 }
7120 p6 = (struct sctp_ipv6addr_param *)phdr;
7121 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7122 sizeof(p6->addr));
7123 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7124 /* Skip multi-cast addresses */
7125 goto next_param;
7126 }
7127 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7128 /* Link local make no sense without scope */
7129 goto next_param;
7130 }
7131 sa = (struct sockaddr *)&sin6;
7132 inp = stcb->sctp_ep;
7133 atomic_add_int(&stcb->asoc.refcnt, 1);
7134 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007135 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007136 atomic_add_int(&stcb->asoc.refcnt, -1);
7137 if (stcb_tmp == NULL &&
7138 (inp == stcb->sctp_ep || inp == NULL)) {
7139 /*
7140 * we must validate the state again
7141 * here
7142 */
7143 add_it_now6:
7144 if (stcb->asoc.state == 0) {
7145 /* the assoc was freed? */
7146 return (-16);
7147 }
7148 /*
7149 * we must add the address, no scope
7150 * set
7151 */
7152 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
7153 return (-17);
7154 }
7155 } else if (stcb_tmp == stcb) {
7156 /*
7157 * we must validate the state again
7158 * here
7159 */
7160 if (stcb->asoc.state == 0) {
7161 /* the assoc was freed? */
7162 return (-19);
7163 }
7164 if (net != NULL) {
7165 /* clear flag */
7166 net->dest_state &=
7167 ~SCTP_ADDR_NOT_IN_ASSOC;
7168 }
7169 } else {
7170 /*
7171 * strange, address is in another
7172 * assoc? straighten out locks.
7173 */
7174 if (stcb_tmp)
7175 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
7176 /* in setup state we abort this guy */
7177 sctp_abort_an_association(stcb_tmp->sctp_ep,
tuexenda53ff02012-05-14 09:00:59 +00007178 stcb_tmp, NULL, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007179 goto add_it_now6;
7180 }
7181 SCTP_TCB_UNLOCK(stcb_tmp);
7182
7183 if (stcb->asoc.state == 0) {
7184 /* the assoc was freed? */
7185 return (-21);
7186 }
7187 return (-22);
7188 }
7189 }
7190 } else
7191#endif
7192 if (ptype == SCTP_ECN_CAPABLE) {
7193 ecn_allowed = 1;
7194 } else if (ptype == SCTP_ULP_ADAPTATION) {
7195 if (stcb->asoc.state != SCTP_STATE_OPEN) {
7196 struct sctp_adaptation_layer_indication ai, *aip;
7197
7198 phdr = sctp_get_next_param(m, offset,
7199 (struct sctp_paramhdr *)&ai, sizeof(ai));
7200 aip = (struct sctp_adaptation_layer_indication *)phdr;
7201 if (aip) {
7202 stcb->asoc.peers_adaptation = ntohl(aip->indication);
7203 stcb->asoc.adaptation_needed = 1;
7204 }
7205 }
7206 } else if (ptype == SCTP_SET_PRIM_ADDR) {
7207 struct sctp_asconf_addr_param lstore, *fee;
7208 int lptype;
7209 struct sockaddr *lsa = NULL;
7210#ifdef INET
7211 struct sctp_asconf_addrv4_param *fii;
7212#endif
7213
7214 stcb->asoc.peer_supports_asconf = 1;
7215 if (plen > sizeof(lstore)) {
7216 return (-23);
7217 }
7218 phdr = sctp_get_next_param(m, offset,
7219 (struct sctp_paramhdr *)&lstore,
7220 min(plen,sizeof(lstore)));
7221 if (phdr == NULL) {
7222 return (-24);
7223 }
7224 fee = (struct sctp_asconf_addr_param *)phdr;
7225 lptype = ntohs(fee->addrp.ph.param_type);
7226 switch (lptype) {
7227#ifdef INET
7228 case SCTP_IPV4_ADDRESS:
7229 if (plen !=
7230 sizeof(struct sctp_asconf_addrv4_param)) {
7231 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7232 (int)sizeof(struct sctp_asconf_addrv4_param),
7233 plen);
7234 } else {
7235 fii = (struct sctp_asconf_addrv4_param *)fee;
7236 sin.sin_addr.s_addr = fii->addrp.addr;
7237 lsa = (struct sockaddr *)&sin;
7238 }
7239 break;
7240#endif
7241#ifdef INET6
7242 case SCTP_IPV6_ADDRESS:
7243 if (plen !=
7244 sizeof(struct sctp_asconf_addr_param)) {
7245 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7246 (int)sizeof(struct sctp_asconf_addr_param),
7247 plen);
7248 } else {
7249 memcpy(sin6.sin6_addr.s6_addr,
7250 fee->addrp.addr,
7251 sizeof(fee->addrp.addr));
7252 lsa = (struct sockaddr *)&sin6;
7253 }
7254 break;
7255#endif
7256 default:
7257 break;
7258 }
7259 if (lsa) {
7260 (void)sctp_set_primary_addr(stcb, sa, NULL);
7261 }
7262 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7263 stcb->asoc.peer_supports_nat = 1;
7264 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7265 /* Peer supports pr-sctp */
7266 stcb->asoc.peer_supports_prsctp = 1;
7267 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7268 /* A supported extension chunk */
7269 struct sctp_supported_chunk_types_param *pr_supported;
7270 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7271 int num_ent, i;
7272
7273 phdr = sctp_get_next_param(m, offset,
7274 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store),plen));
7275 if (phdr == NULL) {
7276 return (-25);
7277 }
7278 stcb->asoc.peer_supports_asconf = 0;
7279 stcb->asoc.peer_supports_prsctp = 0;
7280 stcb->asoc.peer_supports_pktdrop = 0;
7281 stcb->asoc.peer_supports_strreset = 0;
7282 stcb->asoc.peer_supports_nr_sack = 0;
7283 stcb->asoc.peer_supports_auth = 0;
7284 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7285 num_ent = plen - sizeof(struct sctp_paramhdr);
7286 for (i = 0; i < num_ent; i++) {
7287 switch (pr_supported->chunk_types[i]) {
7288 case SCTP_ASCONF:
7289 case SCTP_ASCONF_ACK:
7290 stcb->asoc.peer_supports_asconf = 1;
7291 break;
7292 case SCTP_FORWARD_CUM_TSN:
7293 stcb->asoc.peer_supports_prsctp = 1;
7294 break;
7295 case SCTP_PACKET_DROPPED:
7296 stcb->asoc.peer_supports_pktdrop = 1;
7297 break;
7298 case SCTP_NR_SELECTIVE_ACK:
7299 stcb->asoc.peer_supports_nr_sack = 1;
7300 break;
7301 case SCTP_STREAM_RESET:
7302 stcb->asoc.peer_supports_strreset = 1;
7303 break;
7304 case SCTP_AUTHENTICATION:
7305 stcb->asoc.peer_supports_auth = 1;
7306 break;
7307 default:
7308 /* one I have not learned yet */
7309 break;
7310
7311 }
7312 }
7313 } else if (ptype == SCTP_RANDOM) {
7314 if (plen > sizeof(random_store))
7315 break;
7316 if (got_random) {
7317 /* already processed a RANDOM */
7318 goto next_param;
7319 }
7320 phdr = sctp_get_next_param(m, offset,
7321 (struct sctp_paramhdr *)random_store,
7322 min(sizeof(random_store),plen));
7323 if (phdr == NULL)
7324 return (-26);
7325 p_random = (struct sctp_auth_random *)phdr;
7326 random_len = plen - sizeof(*p_random);
7327 /* enforce the random length */
7328 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7329 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7330 return (-27);
7331 }
7332 got_random = 1;
7333 } else if (ptype == SCTP_HMAC_LIST) {
7334 int num_hmacs;
7335 int i;
7336
7337 if (plen > sizeof(hmacs_store))
7338 break;
7339 if (got_hmacs) {
7340 /* already processed a HMAC list */
7341 goto next_param;
7342 }
7343 phdr = sctp_get_next_param(m, offset,
7344 (struct sctp_paramhdr *)hmacs_store,
7345 min(plen,sizeof(hmacs_store)));
7346 if (phdr == NULL)
7347 return (-28);
7348 hmacs = (struct sctp_auth_hmac_algo *)phdr;
7349 hmacs_len = plen - sizeof(*hmacs);
7350 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7351 /* validate the hmac list */
7352 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7353 return (-29);
7354 }
7355 if (stcb->asoc.peer_hmacs != NULL)
7356 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7357 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7358 if (stcb->asoc.peer_hmacs != NULL) {
7359 for (i = 0; i < num_hmacs; i++) {
7360 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7361 ntohs(hmacs->hmac_ids[i]));
7362 }
7363 }
7364 got_hmacs = 1;
7365 } else if (ptype == SCTP_CHUNK_LIST) {
7366 int i;
7367
7368 if (plen > sizeof(chunks_store))
7369 break;
7370 if (got_chklist) {
7371 /* already processed a Chunks list */
7372 goto next_param;
7373 }
7374 phdr = sctp_get_next_param(m, offset,
7375 (struct sctp_paramhdr *)chunks_store,
7376 min(plen,sizeof(chunks_store)));
7377 if (phdr == NULL)
7378 return (-30);
7379 chunks = (struct sctp_auth_chunk_list *)phdr;
7380 num_chunks = plen - sizeof(*chunks);
7381 if (stcb->asoc.peer_auth_chunks != NULL)
7382 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7383 else
7384 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7385 for (i = 0; i < num_chunks; i++) {
7386 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7387 stcb->asoc.peer_auth_chunks);
7388 /* record asconf/asconf-ack if listed */
7389 if (chunks->chunk_types[i] == SCTP_ASCONF)
7390 saw_asconf = 1;
7391 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7392 saw_asconf_ack = 1;
7393
7394 }
7395 got_chklist = 1;
7396 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7397 (ptype == SCTP_STATE_COOKIE) ||
7398 (ptype == SCTP_UNRECOG_PARAM) ||
7399 (ptype == SCTP_COOKIE_PRESERVE) ||
7400 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7401 (ptype == SCTP_ADD_IP_ADDRESS) ||
7402 (ptype == SCTP_DEL_IP_ADDRESS) ||
7403 (ptype == SCTP_ERROR_CAUSE_IND) ||
7404 (ptype == SCTP_SUCCESS_REPORT)) {
7405 /* don't care */ ;
7406 } else {
7407 if ((ptype & 0x8000) == 0x0000) {
7408 /*
7409 * must stop processing the rest of the
7410 * param's. Any report bits were handled
7411 * with the call to
7412 * sctp_arethere_unrecognized_parameters()
7413 * when the INIT or INIT-ACK was first seen.
7414 */
7415 break;
7416 }
7417 }
tuexen09631162012-04-18 11:23:02 +00007418
tuexendd729232011-11-01 23:04:43 +00007419 next_param:
7420 offset += SCTP_SIZE32(plen);
7421 if (offset >= limit) {
7422 break;
7423 }
7424 phdr = sctp_get_next_param(m, offset, &parm_buf,
7425 sizeof(parm_buf));
7426 }
7427 /* Now check to see if we need to purge any addresses */
7428 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7429 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7430 SCTP_ADDR_NOT_IN_ASSOC) {
7431 /* This address has been removed from the asoc */
7432 /* remove and free it */
7433 stcb->asoc.numnets--;
7434 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
7435 sctp_free_remote_addr(net);
7436 if (net == stcb->asoc.primary_destination) {
7437 stcb->asoc.primary_destination = NULL;
7438 sctp_select_primary_destination(stcb);
7439 }
7440 }
7441 }
7442 if (ecn_allowed == 0) {
7443 stcb->asoc.ecn_allowed = 0;
7444 }
7445 /* validate authentication required parameters */
7446 if (got_random && got_hmacs) {
7447 stcb->asoc.peer_supports_auth = 1;
7448 } else {
7449 stcb->asoc.peer_supports_auth = 0;
7450 }
7451 if (!stcb->asoc.peer_supports_auth && got_chklist) {
7452 /* peer does not support auth but sent a chunks list? */
7453 return (-31);
7454 }
7455 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
7456 !stcb->asoc.peer_supports_auth) {
7457 /* peer supports asconf but not auth? */
7458 return (-32);
7459 } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
7460 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7461 return (-33);
7462 }
7463 /* concatenate the full random key */
7464 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7465 if (chunks != NULL) {
7466 keylen += sizeof(*chunks) + num_chunks;
7467 }
7468 new_key = sctp_alloc_key(keylen);
7469 if (new_key != NULL) {
7470 /* copy in the RANDOM */
7471 if (p_random != NULL) {
7472 keylen = sizeof(*p_random) + random_len;
7473 bcopy(p_random, new_key->key, keylen);
7474 }
7475 /* append in the AUTH chunks */
7476 if (chunks != NULL) {
7477 bcopy(chunks, new_key->key + keylen,
7478 sizeof(*chunks) + num_chunks);
7479 keylen += sizeof(*chunks) + num_chunks;
7480 }
7481 /* append in the HMACs */
7482 if (hmacs != NULL) {
7483 bcopy(hmacs, new_key->key + keylen,
7484 sizeof(*hmacs) + hmacs_len);
7485 }
7486 } else {
7487 /* failed to get memory for the key */
7488 return (-34);
7489 }
7490 if (stcb->asoc.authinfo.peer_random != NULL)
7491 sctp_free_key(stcb->asoc.authinfo.peer_random);
7492 stcb->asoc.authinfo.peer_random = new_key;
7493 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7494 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7495
7496 return (0);
7497}
7498
7499int
7500sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7501 struct sctp_nets *net)
7502{
7503 /* make sure the requested primary address exists in the assoc */
7504 if (net == NULL && sa)
7505 net = sctp_findnet(stcb, sa);
7506
7507 if (net == NULL) {
7508 /* didn't find the requested primary address! */
7509 return (-1);
7510 } else {
7511 /* set the primary address */
7512 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7513 /* Must be confirmed, so queue to set */
7514 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7515 return (0);
7516 }
7517 stcb->asoc.primary_destination = net;
7518 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
7519 sctp_free_remote_addr(stcb->asoc.alternate);
7520 stcb->asoc.alternate = NULL;
7521 }
7522 net = TAILQ_FIRST(&stcb->asoc.nets);
7523 if (net != stcb->asoc.primary_destination) {
7524 /* first one on the list is NOT the primary
7525 * sctp_cmpaddr() is much more efficient if
7526 * the primary is the first on the list, make it
7527 * so.
7528 */
7529 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7530 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7531 }
7532 return (0);
7533 }
7534}
7535
7536int
tuexen9784e9a2011-12-18 13:04:23 +00007537sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
tuexendd729232011-11-01 23:04:43 +00007538{
7539 /*
7540 * This function serves two purposes. It will see if a TAG can be
7541 * re-used and return 1 for yes it is ok and 0 for don't use that
7542 * tag. A secondary function it will do is purge out old tags that
7543 * can be removed.
7544 */
7545 struct sctpvtaghead *chain;
7546 struct sctp_tagblock *twait_block;
7547 struct sctpasochead *head;
7548 struct sctp_tcb *stcb;
7549 int i;
7550
7551 SCTP_INP_INFO_RLOCK();
7552 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
7553 SCTP_BASE_INFO(hashasocmark))];
7554 if (head == NULL) {
7555 /* invalid vtag */
7556 goto skip_vtag_check;
7557 }
7558 LIST_FOREACH(stcb, head, sctp_asocs) {
7559 /* We choose not to lock anything here. TCB's can't be
7560 * removed since we have the read lock, so they can't
7561 * be freed on us, same thing for the INP. I may
7562 * be wrong with this assumption, but we will go
7563 * with it for now :-)
7564 */
7565 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7566 continue;
7567 }
7568 if (stcb->asoc.my_vtag == tag) {
7569 /* candidate */
7570 if (stcb->rport != rport) {
7571 continue;
7572 }
7573 if (stcb->sctp_ep->sctp_lport != lport) {
7574 continue;
7575 }
7576 /* Its a used tag set */
7577 SCTP_INP_INFO_RUNLOCK();
7578 return (0);
7579 }
7580 }
7581skip_vtag_check:
7582
7583 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
7584 /* Now what about timed wait ? */
7585 if (!LIST_EMPTY(chain)) {
7586 /*
7587 * Block(s) are present, lets see if we have this tag in the
7588 * list
7589 */
7590 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
7591 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
7592 if (twait_block->vtag_block[i].v_tag == 0) {
7593 /* not used */
7594 continue;
7595 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
7596 now->tv_sec) {
7597 /* Audit expires this guy */
7598 twait_block->vtag_block[i].tv_sec_at_expire = 0;
7599 twait_block->vtag_block[i].v_tag = 0;
7600 twait_block->vtag_block[i].lport = 0;
7601 twait_block->vtag_block[i].rport = 0;
7602 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
7603 (twait_block->vtag_block[i].lport == lport) &&
7604 (twait_block->vtag_block[i].rport == rport)) {
7605 /* Bad tag, sorry :< */
7606 SCTP_INP_INFO_RUNLOCK();
7607 return (0);
7608 }
7609 }
7610 }
7611 }
7612 SCTP_INP_INFO_RUNLOCK();
7613 return (1);
7614}
7615
tuexendd729232011-11-01 23:04:43 +00007616static void
tuexen9784e9a2011-12-18 13:04:23 +00007617sctp_drain_mbufs(struct sctp_tcb *stcb)
tuexendd729232011-11-01 23:04:43 +00007618{
7619 /*
7620 * We must hunt this association for MBUF's past the cumack (i.e.
7621 * out of order data that we can renege on).
7622 */
7623 struct sctp_association *asoc;
7624 struct sctp_tmit_chunk *chk, *nchk;
7625 uint32_t cumulative_tsn_p1;
7626 struct sctp_queued_to_read *ctl, *nctl;
7627 int cnt, strmat;
7628 uint32_t gap, i;
tuexen63fc0bb2011-12-27 12:24:52 +00007629 int fnd = 0;
tuexendd729232011-11-01 23:04:43 +00007630
7631 /* We look for anything larger than the cum-ack + 1 */
7632
7633 asoc = &stcb->asoc;
7634 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7635 /* none we can reneg on. */
7636 return;
7637 }
7638 SCTP_STAT_INCR(sctps_protocol_drains_done);
7639 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7640 cnt = 0;
7641 /* First look in the re-assembly queue */
7642 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
7643 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) {
7644 /* Yep it is above cum-ack */
7645 cnt++;
7646 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn);
7647 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
7648 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7649 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7650 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
7651 if (chk->data) {
7652 sctp_m_freem(chk->data);
7653 chk->data = NULL;
7654 }
7655 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7656 }
7657 }
7658 /* Ok that was fun, now we will drain all the inbound streams? */
7659 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
7660 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) {
7661 if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) {
7662 /* Yep it is above cum-ack */
7663 cnt++;
7664 SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn);
7665 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
7666 sctp_ucount_decr(asoc->cnt_on_all_streams);
7667 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7668 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next);
7669 if (ctl->data) {
7670 sctp_m_freem(ctl->data);
7671 ctl->data = NULL;
7672 }
7673 sctp_free_remote_addr(ctl->whoFrom);
7674 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
7675 SCTP_DECR_READQ_COUNT();
7676 }
7677 }
7678 }
7679 if (cnt) {
7680 /* We must back down to see what the new highest is */
7681 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
7682 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
7683 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
7684 asoc->highest_tsn_inside_map = i;
7685 fnd = 1;
7686 break;
7687 }
7688 }
7689 if (!fnd) {
7690 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
7691 }
7692
7693 /*
7694 * Question, should we go through the delivery queue? The only
7695 * reason things are on here is the app not reading OR a p-d-api up.
7696 * An attacker COULD send enough in to initiate the PD-API and then
7697 * send a bunch of stuff to other streams... these would wind up on
7698 * the delivery queue.. and then we would not get to them. But in
7699 * order to do this I then have to back-track and un-deliver
7700 * sequence numbers in streams.. el-yucko. I think for now we will
7701 * NOT look at the delivery queue and leave it to be something to
7702 * consider later. An alternative would be to abort the P-D-API with
7703 * a notification and then deliver the data.... Or another method
7704 * might be to keep track of how many times the situation occurs and
7705 * if we see a possible attack underway just abort the association.
7706 */
7707#ifdef SCTP_DEBUG
7708 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
7709#endif
7710 /*
7711 * Now do we need to find a new
7712 * asoc->highest_tsn_inside_map?
7713 */
7714 asoc->last_revoke_count = cnt;
7715 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
7716 /*sa_ignore NO_NULL_CHK*/
7717 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7718 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007719 }
7720 /*
7721 * Another issue, in un-setting the TSN's in the mapping array we
7722 * DID NOT adjust the highest_tsn marker. This will cause one of two
7723 * things to occur. It may cause us to do extra work in checking for
7724 * our mapping array movement. More importantly it may cause us to
7725 * SACK every datagram. This may not be a bad thing though since we
7726 * will recover once we get our cum-ack above and all this stuff we
7727 * dumped recovered.
7728 */
7729}
7730
7731void
7732sctp_drain()
7733{
7734 /*
7735 * We must walk the PCB lists for ALL associations here. The system
7736 * is LOW on MBUF's and needs help. This is where reneging will
7737 * occur. We really hope this does NOT happen!
7738 */
7739#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7740 VNET_ITERATOR_DECL(vnet_iter);
7741#else
7742 struct sctp_inpcb *inp;
7743 struct sctp_tcb *stcb;
7744
7745 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7746 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7747 return;
7748 }
7749#endif
7750#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7751 VNET_LIST_RLOCK_NOSLEEP();
7752 VNET_FOREACH(vnet_iter) {
7753 CURVNET_SET(vnet_iter);
7754 struct sctp_inpcb *inp;
7755 struct sctp_tcb *stcb;
7756#endif
7757
7758#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7759 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7760 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7761#ifdef VIMAGE
7762 continue;
7763#else
7764 return;
tuexen09631162012-04-18 11:23:02 +00007765#endif
tuexendd729232011-11-01 23:04:43 +00007766 }
7767#endif
7768 SCTP_INP_INFO_RLOCK();
7769 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7770 /* For each endpoint */
7771 SCTP_INP_RLOCK(inp);
7772 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7773 /* For each association */
7774 SCTP_TCB_LOCK(stcb);
tuexen9784e9a2011-12-18 13:04:23 +00007775 sctp_drain_mbufs(stcb);
tuexendd729232011-11-01 23:04:43 +00007776 SCTP_TCB_UNLOCK(stcb);
7777 }
7778 SCTP_INP_RUNLOCK(inp);
7779 }
7780 SCTP_INP_INFO_RUNLOCK();
7781#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7782 CURVNET_RESTORE();
7783 }
7784 VNET_LIST_RUNLOCK_NOSLEEP();
7785#endif
7786}
7787
7788/*
7789 * start a new iterator
7790 * iterates through all endpoints and associations based on the pcb_state
7791 * flags and asoc_state. "af" (mandatory) is executed for all matching
7792 * assocs and "ef" (optional) is executed when the iterator completes.
7793 * "inpf" (optional) is executed for each new endpoint as it is being
7794 * iterated through. inpe (optional) is called when the inp completes
7795 * its way through all the stcbs.
7796 */
7797int
7798sctp_initiate_iterator(inp_func inpf,
7799 asoc_func af,
7800 inp_func inpe,
7801 uint32_t pcb_state,
7802 uint32_t pcb_features,
7803 uint32_t asoc_state,
7804 void *argp,
7805 uint32_t argi,
7806 end_func ef,
7807 struct sctp_inpcb *s_inp,
7808 uint8_t chunk_output_off)
7809{
7810 struct sctp_iterator *it = NULL;
7811
7812 if (af == NULL) {
7813 return (-1);
7814 }
7815 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
7816 SCTP_M_ITER);
7817 if (it == NULL) {
7818 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
7819 return (ENOMEM);
7820 }
7821 memset(it, 0, sizeof(*it));
7822 it->function_assoc = af;
7823 it->function_inp = inpf;
7824 if (inpf)
7825 it->done_current_ep = 0;
7826 else
7827 it->done_current_ep = 1;
7828 it->function_atend = ef;
7829 it->pointer = argp;
7830 it->val = argi;
7831 it->pcb_flags = pcb_state;
7832 it->pcb_features = pcb_features;
7833 it->asoc_state = asoc_state;
7834 it->function_inp_end = inpe;
7835 it->no_chunk_output = chunk_output_off;
7836#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7837 it->vn = curvnet;
tuexen09631162012-04-18 11:23:02 +00007838#endif
tuexendd729232011-11-01 23:04:43 +00007839 if (s_inp) {
7840 /* Assume lock is held here */
7841 it->inp = s_inp;
7842 SCTP_INP_INCR_REF(it->inp);
7843 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
7844 } else {
7845 SCTP_INP_INFO_RLOCK();
7846 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
7847 if (it->inp) {
7848 SCTP_INP_INCR_REF(it->inp);
7849 }
7850 SCTP_INP_INFO_RUNLOCK();
7851 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
7852
7853 }
7854 SCTP_IPI_ITERATOR_WQ_LOCK();
7855
7856 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
7857 if (sctp_it_ctl.iterator_running == 0) {
7858 sctp_wakeup_iterator();
7859 }
7860 SCTP_IPI_ITERATOR_WQ_UNLOCK();
7861 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
7862 return (0);
7863}