blob: cde52902b82bb6af86a344530c55b33f2b1c2378 [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>
tuexend85b3d92012-08-04 20:45:45 +000035__FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.c 239041 2012-08-04 20:40:36Z 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,
624 sctp_ifap);
625 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",
646 sctp_ifap, sctp_ifap->ifn_p->ifn_name,
647 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",
658 sctp_ifnp, sctp_ifap);
659 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;
tuexen6019b072011-12-15 18:38:03 +0000677#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
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 }
839 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", sctp_ifap);
840 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, ",
1950 laddr->ifa);
1951 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));
tuexen6019b072011-12-15 18:38:03 +00002271#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
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));
tuexenda6f8ba2012-01-08 11:33:02 +00002279#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
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 }
2497 SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
2498 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 }
2523 SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
2524 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;
tuexenda6f8ba2012-01-08 11:33:02 +00002578#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
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;
tuexen6019b072011-12-15 18:38:03 +00002607#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
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);
tuexencb5fe8d2012-05-04 09:50:27 +00002625 /*SCTP_PRINTF("findassociation_ep_asconf: zero lookup address finds stcb 0x%x\n", (uint32_t)stcb);*/
tuexendd729232011-11-01 23:04:43 +00002626 } else {
2627 stcb = sctp_findassociation_ep_addr(inp_p,
2628 (struct sockaddr *)&remote_store, netp,
tuexen664967a2012-06-28 16:24:36 +00002629 dst, NULL);
tuexendd729232011-11-01 23:04:43 +00002630 }
2631 return (stcb);
2632}
2633
2634
2635/*
2636 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2637 * addresses. This way if we don't get a bind we by default pick a ephemeral
2638 * port with all addresses bound.
2639 */
2640int
2641sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2642{
2643 /*
2644 * we get called when a new endpoint starts up. We need to allocate
2645 * the sctp_inpcb structure from the zone and init it. Mark it as
2646 * unbound and find a port that we can use as an ephemeral with
2647 * INADDR_ANY. If the user binds later no problem we can then add in
2648 * the specific addresses. And setup the default parameters for the
2649 * EP.
2650 */
2651 int i, error;
2652 struct sctp_inpcb *inp;
2653 struct sctp_pcb *m;
2654 struct timeval time;
2655 sctp_sharedkey_t *null_key;
2656
2657 error = 0;
2658
2659 SCTP_INP_INFO_WLOCK();
2660 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2661 if (inp == NULL) {
2662 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2663 SCTP_INP_INFO_WUNLOCK();
2664 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2665 return (ENOBUFS);
2666 }
2667 /* zap it */
2668 bzero(inp, sizeof(*inp));
2669
2670 /* bump generations */
2671#if defined(__APPLE__)
2672 inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
2673#endif
2674 /* setup socket pointers */
2675 inp->sctp_socket = so;
2676 inp->ip_inp.inp.inp_socket = so;
tuexenc0bb6122012-01-15 14:21:21 +00002677#ifdef INET6
tuexen76eb2092012-04-01 16:35:28 +00002678#if !defined(__Userspace__) && !defined(__Windows__)
tuexendd729232011-11-01 23:04:43 +00002679 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2680 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2681 }
2682#endif
tuexenc0bb6122012-01-15 14:21:21 +00002683#endif
tuexendd729232011-11-01 23:04:43 +00002684 inp->sctp_associd_counter = 1;
2685 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2686 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2687 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2688 inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable);
tuexen09631162012-04-18 11:23:02 +00002689#if defined(__Userspace__)
tuexeneda9dbb2012-05-27 11:10:08 +00002690 inp->ulp_info = NULL;
tuexen09631162012-04-18 11:23:02 +00002691 inp->recv_callback = NULL;
2692 inp->send_callback = NULL;
2693 inp->send_sb_threshold = 0;
tuexen09631162012-04-18 11:23:02 +00002694#endif
tuexendd729232011-11-01 23:04:43 +00002695 /* init the small hash table we use to track asocid <-> tcb */
2696 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2697 if (inp->sctp_asocidhash == NULL) {
2698 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2699 SCTP_INP_INFO_WUNLOCK();
2700 return (ENOBUFS);
2701 }
2702#ifdef IPSEC
2703#if !(defined(__APPLE__))
2704 {
2705 struct inpcbpolicy *pcb_sp = NULL;
2706
2707 error = ipsec_init_policy(so, &pcb_sp);
2708 /* Arrange to share the policy */
2709 inp->ip_inp.inp.inp_sp = pcb_sp;
2710 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2711 }
2712#else
2713 /* not sure what to do for openbsd here */
2714 error = 0;
2715#endif
2716 if (error != 0) {
2717 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2718 SCTP_INP_INFO_WUNLOCK();
2719 return error;
2720 }
2721#endif /* IPSEC */
2722 SCTP_INCR_EP_COUNT();
tuexendd729232011-11-01 23:04:43 +00002723 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
tuexendd729232011-11-01 23:04:43 +00002724 SCTP_INP_INFO_WUNLOCK();
2725
2726 so->so_pcb = (caddr_t)inp;
2727
tuexen5fc22842012-02-11 13:32:19 +00002728#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00002729 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
2730 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
2731#else
2732 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2733#endif
2734 /* UDP style socket */
2735 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2736 SCTP_PCB_FLAGS_UNBOUND);
2737 /* Be sure it is NON-BLOCKING IO for UDP */
2738 /* SCTP_SET_SO_NBIO(so); */
2739 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2740 /* TCP style socket */
2741 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2742 SCTP_PCB_FLAGS_UNBOUND);
2743 /* Be sure we have blocking IO by default */
2744 SCTP_CLEAR_SO_NBIO(so);
2745#if defined(__Panda__)
2746 } else if (SCTP_SO_TYPE(so) == SOCK_FASTSEQPACKET) {
2747 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2748 SCTP_PCB_FLAGS_UNBOUND);
2749 sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE);
2750 } else if (SCTP_SO_TYPE(so) == SOCK_FASTSTREAM) {
2751 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2752 SCTP_PCB_FLAGS_UNBOUND);
2753 sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE);
2754#endif
2755 } else {
2756 /*
2757 * unsupported socket type (RAW, etc)- in case we missed it
2758 * in protosw
2759 */
2760 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2761 so->so_pcb = NULL;
2762 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2763 return (EOPNOTSUPP);
2764 }
2765 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2766 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2767 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2768 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2769 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2770 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2771 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2772 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2773 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2774 }
2775 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2776 &inp->sctp_hashmark);
2777 if (inp->sctp_tcbhash == NULL) {
2778 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2779 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2780 so->so_pcb = NULL;
2781 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2782 return (ENOBUFS);
2783 }
2784#ifdef SCTP_MVRF
2785 inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
2786 SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
2787 (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
2788 if (inp->m_vrf_ids == NULL) {
2789 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2790 so->so_pcb = NULL;
2791 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2792 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2793 return (ENOBUFS);
2794 }
2795 inp->m_vrf_ids[0] = vrf_id;
2796 inp->num_vrfs = 1;
2797#endif
2798 inp->def_vrf_id = vrf_id;
2799
2800#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00002801#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
tuexendd729232011-11-01 23:04:43 +00002802 inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(mtx_grp), SCTP_BASE_INFO(mtx_attr));
2803 if (inp->ip_inp.inp.inpcb_mtx == NULL) {
2804 SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", so);
2805#ifdef SCTP_MVRF
2806 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
2807#endif
2808 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2809 so->so_pcb = NULL;
2810 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2811 SCTP_UNLOCK_EXC(SCTP_BASE_INFO(ipi_ep_mtx));
2812 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
2813 return (ENOMEM);
2814 }
tuexen9a218b12012-08-04 21:17:58 +00002815#else
2816 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 +00002817#endif
2818#endif
2819 SCTP_INP_INFO_WLOCK();
2820 SCTP_INP_LOCK_INIT(inp);
2821#if defined(__FreeBSD__)
2822 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2823#endif
2824 SCTP_INP_READ_INIT(inp);
2825 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2826 /* lock the new ep */
2827 SCTP_INP_WLOCK(inp);
2828
2829 /* add it to the info area */
2830 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2831#if defined(__APPLE__)
2832 LIST_INSERT_HEAD(&SCTP_BASE_INFO(inplisthead), &inp->ip_inp.inp, inp_list);
2833#endif
2834 SCTP_INP_INFO_WUNLOCK();
2835
2836 TAILQ_INIT(&inp->read_queue);
2837 LIST_INIT(&inp->sctp_addr_list);
2838
2839 LIST_INIT(&inp->sctp_asoc_list);
2840
2841#ifdef SCTP_TRACK_FREED_ASOCS
2842 /* TEMP CODE */
2843 LIST_INIT(&inp->sctp_asoc_free_list);
2844#endif
2845 /* Init the timer structure for signature change */
2846 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2847 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2848
2849 /* now init the actual endpoint default data */
2850 m = &inp->sctp_ep;
2851
2852 /* setup the base timeout information */
2853 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
2854 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
2855 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2856 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2857 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2858 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2859 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2860 /* all max/min max are in ms */
2861 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2862 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2863 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2864 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2865 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2866
2867 m->max_open_streams_intome = MAX_SCTP_STREAMS;
2868
2869 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2870 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2871 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2872 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2873 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2874 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2875 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2876 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2877
2878 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2879 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2880 /* number of streams to pre-open on a association */
2881 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2882
2883 /* Add adaptation cookie */
2884 m->adaptation_layer_indicator = 0x504C5253;
2885
2886 /* seed random number generator */
2887 m->random_counter = 1;
2888 m->store_at = SCTP_SIGNATURE_SIZE;
2889 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2890 sctp_fill_random_store(m);
2891
2892 /* Minimum cookie size */
2893 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2894 sizeof(struct sctp_state_cookie);
2895 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2896
2897 /* Setup the initial secret */
2898 (void)SCTP_GETTIME_TIMEVAL(&time);
2899 m->time_of_secret_change = time.tv_sec;
2900
2901 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2902 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2903 }
2904 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2905
2906 /* How long is a cookie good for ? */
2907 m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2908 /*
2909 * Initialize authentication parameters
2910 */
2911 m->local_hmacs = sctp_default_supported_hmaclist();
2912 m->local_auth_chunks = sctp_alloc_chunklist();
2913 m->default_dscp = 0;
2914#ifdef INET6
2915 m->default_flowlabel = 0;
2916#endif
tuexen9d7b2072011-11-20 16:35:17 +00002917 m->port = 0; /* encapsulation disabled by default */
tuexendd729232011-11-01 23:04:43 +00002918 sctp_auth_set_default_chunks(m->local_auth_chunks);
2919 LIST_INIT(&m->shared_keys);
2920 /* add default NULL key as key id 0 */
2921 null_key = sctp_alloc_sharedkey();
2922 sctp_insert_sharedkey(&m->shared_keys, null_key);
2923 SCTP_INP_WUNLOCK(inp);
2924#ifdef SCTP_LOG_CLOSING
2925 sctp_log_closing(inp, NULL, 12);
2926#endif
2927 return (error);
2928}
2929
2930
2931void
2932sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2933 struct sctp_tcb *stcb)
2934{
2935 struct sctp_nets *net;
2936 uint16_t lport, rport;
2937 struct sctppcbhead *head;
2938 struct sctp_laddr *laddr, *oladdr;
2939
2940 atomic_add_int(&stcb->asoc.refcnt, 1);
2941 SCTP_TCB_UNLOCK(stcb);
2942 SCTP_INP_INFO_WLOCK();
2943 SCTP_INP_WLOCK(old_inp);
2944 SCTP_INP_WLOCK(new_inp);
2945 SCTP_TCB_LOCK(stcb);
2946 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2947
2948 new_inp->sctp_ep.time_of_secret_change =
2949 old_inp->sctp_ep.time_of_secret_change;
2950 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2951 sizeof(old_inp->sctp_ep.secret_key));
2952 new_inp->sctp_ep.current_secret_number =
2953 old_inp->sctp_ep.current_secret_number;
2954 new_inp->sctp_ep.last_secret_number =
2955 old_inp->sctp_ep.last_secret_number;
2956 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2957
2958 /* make it so new data pours into the new socket */
2959 stcb->sctp_socket = new_inp->sctp_socket;
2960 stcb->sctp_ep = new_inp;
2961
2962 /* Copy the port across */
2963 lport = new_inp->sctp_lport = old_inp->sctp_lport;
2964 rport = stcb->rport;
2965 /* Pull the tcb from the old association */
2966 LIST_REMOVE(stcb, sctp_tcbhash);
2967 LIST_REMOVE(stcb, sctp_tcblist);
2968 if (stcb->asoc.in_asocid_hash) {
2969 LIST_REMOVE(stcb, sctp_tcbasocidhash);
2970 }
2971 /* Now insert the new_inp into the TCP connected hash */
2972 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2973
2974 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2975 /* Its safe to access */
2976 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2977
2978 /* Now move the tcb into the endpoint list */
2979 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2980 /*
2981 * Question, do we even need to worry about the ep-hash since we
2982 * only have one connection? Probably not :> so lets get rid of it
2983 * and not suck up any kernel memory in that.
2984 */
2985 if (stcb->asoc.in_asocid_hash) {
2986 struct sctpasochead *lhd;
2987 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2988 new_inp->hashasocidmark)];
2989 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2990 }
2991 /* Ok. Let's restart timer. */
2992 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2993 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2994 stcb, net);
2995 }
2996
2997 SCTP_INP_INFO_WUNLOCK();
2998 if (new_inp->sctp_tcbhash != NULL) {
2999 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
3000 new_inp->sctp_tcbhash = NULL;
3001 }
3002 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3003 /* Subset bound, so copy in the laddr list from the old_inp */
3004 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3005 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3006 if (laddr == NULL) {
3007 /*
3008 * Gak, what can we do? This assoc is really
3009 * HOSED. We probably should send an abort
3010 * here.
3011 */
3012 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3013 continue;
3014 }
3015 SCTP_INCR_LADDR_COUNT();
3016 bzero(laddr, sizeof(*laddr));
3017 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3018 laddr->ifa = oladdr->ifa;
3019 atomic_add_int(&laddr->ifa->refcount, 1);
3020 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3021 sctp_nxt_addr);
3022 new_inp->laddr_count++;
3023 if (oladdr == stcb->asoc.last_used_address) {
3024 stcb->asoc.last_used_address = laddr;
3025 }
3026 }
3027 }
3028 /* Now any running timers need to be adjusted
3029 * since we really don't care if they are running
3030 * or not just blast in the new_inp into all of
3031 * them.
3032 */
3033
3034 stcb->asoc.dack_timer.ep = (void *)new_inp;
3035 stcb->asoc.asconf_timer.ep = (void *)new_inp;
3036 stcb->asoc.strreset_timer.ep = (void *)new_inp;
3037 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
3038 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
3039 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
3040 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
3041 /* now what about the nets? */
3042 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3043 net->pmtu_timer.ep = (void *)new_inp;
3044 net->hb_timer.ep = (void *)new_inp;
3045 net->rxt_timer.ep = (void *)new_inp;
3046 }
3047 SCTP_INP_WUNLOCK(new_inp);
3048 SCTP_INP_WUNLOCK(old_inp);
3049}
3050
3051
3052#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
3053/*
3054 * Don't know why, but without this there is an unknown reference when
3055 * compiling NetBSD... hmm
3056 */
3057extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3058#endif
3059
3060
3061/* sctp_ifap is used to bypass normal local address validation checks */
3062int
3063#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3064sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3065 struct sctp_ifa *sctp_ifap, struct thread *p)
3066#elif defined(__Windows__)
3067sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3068 struct sctp_ifa *sctp_ifap, PKTHREAD p)
3069#else
3070sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3071 struct sctp_ifa *sctp_ifap, struct proc *p)
3072#endif
3073{
3074 /* bind a ep to a socket address */
3075 struct sctppcbhead *head;
3076 struct sctp_inpcb *inp, *inp_tmp;
3077 struct inpcb *ip_inp;
3078 int port_reuse_active = 0;
3079 int bindall;
3080#ifdef SCTP_MVRF
3081 int i;
3082#endif
3083 uint16_t lport;
3084 int error;
3085 uint32_t vrf_id;
3086
3087 lport = 0;
3088 error = 0;
3089 bindall = 1;
3090 inp = (struct sctp_inpcb *)so->so_pcb;
3091 ip_inp = (struct inpcb *)so->so_pcb;
3092#ifdef SCTP_DEBUG
3093 if (addr) {
3094 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
3095 ntohs(((struct sockaddr_in *)addr)->sin_port));
3096 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
3097 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3098 }
3099#endif
3100 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3101 /* already did a bind, subsequent binds NOT allowed ! */
3102 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3103 return (EINVAL);
3104 }
3105#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
3106#ifdef INVARIANTS
3107 if (p == NULL)
3108 panic("null proc/thread");
3109#endif
3110#endif
3111 if (addr != NULL) {
3112 switch (addr->sa_family) {
3113#ifdef INET
3114 case AF_INET:
3115 {
3116 struct sockaddr_in *sin;
3117
3118 /* IPV6_V6ONLY socket? */
3119 if (SCTP_IPV6_V6ONLY(ip_inp)) {
3120 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3121 return (EINVAL);
3122 }
tuexen6019b072011-12-15 18:38:03 +00003123#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00003124 if (addr->sa_len != sizeof(*sin)) {
3125 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3126 return (EINVAL);
3127 }
3128#endif
3129
3130 sin = (struct sockaddr_in *)addr;
3131 lport = sin->sin_port;
3132#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
3133 /*
3134 * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3135 * to the proper value.
3136 */
3137 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
3138 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3139 return (error);
3140 }
3141#endif
3142 if (sin->sin_addr.s_addr != INADDR_ANY) {
3143 bindall = 0;
3144 }
3145 break;
3146 }
3147#endif
3148#ifdef INET6
3149 case AF_INET6:
3150 {
3151 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3152 struct sockaddr_in6 *sin6;
3153
3154 sin6 = (struct sockaddr_in6 *)addr;
3155
tuexenda6f8ba2012-01-08 11:33:02 +00003156#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00003157 if (addr->sa_len != sizeof(*sin6)) {
3158 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3159 return (EINVAL);
3160 }
3161#endif
3162
3163 lport = sin6->sin6_port;
3164#if defined(__FreeBSD__) && __FreeBSD_version >= 800000
3165
3166 /*
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 }
3175#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;
3219#if !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
3220 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
3221 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3222 return (EINVAL);
3223 }
3224#endif
3225 sconn = (struct sockaddr_conn *)addr;
3226 lport = sconn->sconn_port;
3227 if (sconn->sconn_addr != NULL) {
3228 bindall = 0;
3229 }
3230 break;
3231 }
3232#endif
tuexendd729232011-11-01 23:04:43 +00003233 default:
3234 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
3235 return (EAFNOSUPPORT);
3236 }
3237 }
3238 SCTP_INP_INFO_WLOCK();
3239 SCTP_INP_WLOCK(inp);
3240 /* Setup a vrf_id to be the default for the non-bind-all case. */
3241 vrf_id = inp->def_vrf_id;
3242
3243 /* increase our count due to the unlock we do */
3244 SCTP_INP_INCR_REF(inp);
3245 if (lport) {
3246 /*
3247 * Did the caller specify a port? if so we must see if a ep
3248 * already has this one bound.
3249 */
3250 /* got to be root to get at low ports */
3251#if !defined(__Windows__)
3252 if (ntohs(lport) < IPPORT_RESERVED) {
3253 if (p && (error =
3254#ifdef __FreeBSD__
3255#if __FreeBSD_version > 602000
3256 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3257#elif __FreeBSD_version >= 500000
3258 suser_cred(p->td_ucred, 0)
3259#else
3260 suser(p)
3261#endif
3262#elif defined(__APPLE__)
3263 suser(p->p_ucred, &p->p_acflag)
3264#elif defined(__Userspace__) /* must be true to use raw socket */
3265 1
3266#else
3267 suser(p, 0)
3268#endif
3269 )) {
3270 SCTP_INP_DECR_REF(inp);
3271 SCTP_INP_WUNLOCK(inp);
3272 SCTP_INP_INFO_WUNLOCK();
3273 return (error);
3274 }
3275#if defined(__Panda__)
3276 if (!SCTP_IS_PRIVILEDGED(so)) {
3277 SCTP_INP_DECR_REF(inp);
3278 SCTP_INP_WUNLOCK(inp);
3279 SCTP_INP_INFO_WUNLOCK();
3280 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EACCES);
3281 return (EACCES);
3282 }
3283#endif
3284 }
3285#if !defined(__Panda__) && !defined(__Userspace__)
3286 if (p == NULL) {
3287 SCTP_INP_DECR_REF(inp);
3288 SCTP_INP_WUNLOCK(inp);
3289 SCTP_INP_INFO_WUNLOCK();
3290 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3291 return (error);
3292 }
3293#endif
3294#endif /* __Windows__ */
3295 SCTP_INP_WUNLOCK(inp);
3296 if (bindall) {
3297#ifdef SCTP_MVRF
tuexen63fc0bb2011-12-27 12:24:52 +00003298 for (i = 0; i < inp->num_vrfs; i++) {
tuexendd729232011-11-01 23:04:43 +00003299 vrf_id = inp->m_vrf_ids[i];
3300#else
3301 vrf_id = inp->def_vrf_id;
3302#endif
3303 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3304 if (inp_tmp != NULL) {
3305 /*
3306 * lock guy returned and lower count
3307 * note that we are not bound so
3308 * inp_tmp should NEVER be inp. And
3309 * it is this inp (inp_tmp) that gets
3310 * the reference bump, so we must
3311 * lower it.
3312 */
3313 SCTP_INP_DECR_REF(inp_tmp);
3314 /* unlock info */
3315 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3316 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3317 /* Ok, must be one-2-one and allowing port re-use */
3318 port_reuse_active = 1;
3319 goto continue_anyway;
3320 }
3321 SCTP_INP_DECR_REF(inp);
3322 SCTP_INP_INFO_WUNLOCK();
3323 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3324 return (EADDRINUSE);
3325 }
3326#ifdef SCTP_MVRF
3327 }
3328#endif
3329 } else {
3330 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3331 if (inp_tmp != NULL) {
3332 /*
3333 * lock guy returned and lower count note
3334 * that we are not bound so inp_tmp should
3335 * NEVER be inp. And it is this inp (inp_tmp)
3336 * that gets the reference bump, so we must
3337 * lower it.
3338 */
3339 SCTP_INP_DECR_REF(inp_tmp);
3340 /* unlock info */
3341 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3342 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3343 /* Ok, must be one-2-one and allowing port re-use */
3344 port_reuse_active = 1;
3345 goto continue_anyway;
3346 }
3347 SCTP_INP_DECR_REF(inp);
3348 SCTP_INP_INFO_WUNLOCK();
3349 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3350 return (EADDRINUSE);
3351 }
3352 }
3353 continue_anyway:
3354 SCTP_INP_WLOCK(inp);
3355 if (bindall) {
3356 /* verify that no lport is not used by a singleton */
3357 if ((port_reuse_active == 0) &&
3358 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))
3359 ) {
3360 /* 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);
3497 }
3498 else {
3499 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3500 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3501 }
3502 /* set the automatic mobility_fasthandoff from kernel
3503 flag (by micchie)
3504 */
3505 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3506 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3507 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3508 }
3509 else {
3510 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3511 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3512 }
3513 } else {
3514 /*
3515 * bind specific, make sure flags is off and add a new
3516 * address structure to the sctp_addr_list inside the ep
3517 * structure.
3518 *
3519 * We will need to allocate one and insert it at the head. The
3520 * socketopt call can just insert new addresses in there as
3521 * well. It will also have to do the embed scope kame hack
3522 * too (before adding).
3523 */
3524 struct sctp_ifa *ifa;
3525 struct sockaddr_storage store_sa;
3526
3527 memset(&store_sa, 0, sizeof(store_sa));
3528 switch (addr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00003529#ifdef INET
tuexendd729232011-11-01 23:04:43 +00003530 case AF_INET:
3531 {
3532 struct sockaddr_in *sin;
3533
3534 sin = (struct sockaddr_in *)&store_sa;
3535 memcpy(sin, addr, sizeof(struct sockaddr_in));
3536 sin->sin_port = 0;
3537 break;
3538 }
tuexen310f1bc2012-07-15 11:20:56 +00003539#endif
3540#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00003541 case AF_INET6:
3542 {
3543 struct sockaddr_in6 *sin6;
3544
3545 sin6 = (struct sockaddr_in6 *)&store_sa;
3546 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
3547 sin6->sin6_port = 0;
3548 break;
3549 }
tuexen310f1bc2012-07-15 11:20:56 +00003550#endif
tuexencc2b1422012-07-10 21:50:32 +00003551#if defined(__Userspace__)
3552 case AF_CONN:
3553 {
3554 struct sockaddr_conn *sconn;
3555
3556 sconn = (struct sockaddr_conn *)&store_sa;
3557 memcpy(sconn, addr, sizeof(struct sockaddr_conn));
3558 sconn->sconn_port = 0;
3559 break;
3560 }
3561#endif
tuexendd729232011-11-01 23:04:43 +00003562 default:
3563 break;
3564 }
3565 /*
3566 * first find the interface with the bound address need to
3567 * zero out the port to find the address! yuck! can't do
3568 * this earlier since need port for sctp_pcb_findep()
3569 */
3570 if (sctp_ifap != NULL)
3571 ifa = sctp_ifap;
3572 else {
3573 /* Note for BSD we hit here always other
3574 * O/S's will pass things in via the
3575 * sctp_ifap argument (Panda).
3576 */
3577 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
3578 vrf_id, SCTP_ADDR_NOT_LOCKED);
3579 }
3580 if (ifa == NULL) {
3581 /* Can't find an interface with that address */
3582 SCTP_INP_WUNLOCK(inp);
3583 SCTP_INP_INFO_WUNLOCK();
3584 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3585 return (EADDRNOTAVAIL);
3586 }
3587#ifdef INET6
3588 if (addr->sa_family == AF_INET6) {
3589 /* GAK, more FIXME IFA lock? */
3590 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3591 /* Can't bind a non-existent addr. */
3592 SCTP_INP_WUNLOCK(inp);
3593 SCTP_INP_INFO_WUNLOCK();
3594 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3595 return (EINVAL);
3596 }
3597 }
3598#endif
3599 /* we're not bound all */
3600 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3601 /* allow bindx() to send ASCONF's for binding changes */
3602 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3603 /* clear automatic addr changes from kernel flag */
3604 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3605
3606 /* add this address to the endpoint list */
3607 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3608 if (error != 0) {
3609 SCTP_INP_WUNLOCK(inp);
3610 SCTP_INP_INFO_WUNLOCK();
3611 return (error);
3612 }
3613 inp->laddr_count++;
3614 }
3615 /* find the bucket */
tuexen98456cf2012-04-19 15:37:07 +00003616 if (port_reuse_active) {
tuexendd729232011-11-01 23:04:43 +00003617 /* Put it into tcp 1-2-1 hash */
3618 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3619 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3620 } else {
3621 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3622 }
3623 /* put it in the bucket */
3624 LIST_INSERT_HEAD(head, inp, sctp_hash);
3625 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3626 head, ntohs(lport), port_reuse_active);
3627 /* set in the port */
3628 inp->sctp_lport = lport;
3629
3630 /* turn off just the unbound flag */
3631 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3632 SCTP_INP_WUNLOCK(inp);
3633 SCTP_INP_INFO_WUNLOCK();
3634 return (0);
3635}
3636
3637
3638static void
3639sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3640{
3641 struct sctp_iterator *it, *nit;
3642
3643 /*
3644 * We enter with the only the ITERATOR_LOCK in place and a write
3645 * lock on the inp_info stuff.
3646 */
3647 it = sctp_it_ctl.cur_it;
3648#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3649 if (it && (it->vn != curvnet)) {
3650 /* Its not looking at our VNET */
3651 return;
3652 }
3653#endif
3654 if (it && (it->inp == inp)) {
3655 /*
3656 * This is tricky and we hold the iterator lock,
3657 * but when it returns and gets the lock (when we
3658 * release it) the iterator will try to operate on
3659 * inp. We need to stop that from happening. But
3660 * of course the iterator has a reference on the
3661 * stcb and inp. We can mark it and it will stop.
3662 *
3663 * If its a single iterator situation, we
3664 * set the end iterator flag. Otherwise
3665 * we set the iterator to go to the next inp.
3666 *
3667 */
3668 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3669 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3670 } else {
3671 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3672 }
3673 }
3674 /* Now go through and remove any single reference to
3675 * our inp that may be still pending on the list
3676 */
3677 SCTP_IPI_ITERATOR_WQ_LOCK();
3678 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3679#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
3680 if (it->vn != curvnet) {
3681 continue;
tuexen09631162012-04-18 11:23:02 +00003682 }
tuexendd729232011-11-01 23:04:43 +00003683#endif
3684 if (it->inp == inp) {
3685 /* This one points to me is it inp specific? */
3686 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3687 /* Remove and free this one */
3688 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3689 it, sctp_nxt_itr);
3690 if (it->function_atend != NULL) {
3691 (*it->function_atend) (it->pointer, it->val);
3692 }
3693 SCTP_FREE(it, SCTP_M_ITER);
3694 } else {
3695 it->inp = LIST_NEXT(it->inp, sctp_list);
3696 if (it->inp) {
3697 SCTP_INP_INCR_REF(it->inp);
3698 }
3699 }
3700 /* When its put in the refcnt is incremented so decr it */
3701 SCTP_INP_DECR_REF(inp);
3702 }
3703 }
3704 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3705}
3706
3707/* release sctp_inpcb unbind the port */
3708void
3709sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3710{
3711 /*
3712 * Here we free a endpoint. We must find it (if it is in the Hash
3713 * table) and remove it from there. Then we must also find it in the
3714 * overall list and remove it from there. After all removals are
3715 * complete then any timer has to be stopped. Then start the actual
3716 * freeing. a) Any local lists. b) Any associations. c) The hash of
3717 * all associations. d) finally the ep itself.
3718 */
tuexendd729232011-11-01 23:04:43 +00003719 struct sctp_tcb *asoc, *nasoc;
3720 struct sctp_laddr *laddr, *nladdr;
3721 struct inpcb *ip_pcb;
3722 struct socket *so;
tuexen63fc0bb2011-12-27 12:24:52 +00003723 int being_refed = 0;
tuexendd729232011-11-01 23:04:43 +00003724 struct sctp_queued_to_read *sq, *nsq;
3725#if !defined(__Panda__) && !defined(__Userspace__)
3726#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
3727 sctp_rtentry_t *rt;
3728#endif
3729#endif
3730 int cnt;
3731 sctp_sharedkey_t *shared_key, *nshared_key;
3732
3733
3734#if defined(__APPLE__)
3735 sctp_lock_assert(SCTP_INP_SO(inp));
3736#endif
3737#ifdef SCTP_LOG_CLOSING
3738 sctp_log_closing(inp, NULL, 0);
3739#endif
3740 SCTP_ITERATOR_LOCK();
3741 /* mark any iterators on the list or being processed */
3742 sctp_iterator_inp_being_freed(inp);
3743 SCTP_ITERATOR_UNLOCK();
3744 so = inp->sctp_socket;
3745 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3746 /* been here before.. eeks.. get out of here */
3747 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3748#ifdef SCTP_LOG_CLOSING
3749 sctp_log_closing(inp, NULL, 1);
3750#endif
3751 return;
3752 }
3753 SCTP_ASOC_CREATE_LOCK(inp);
3754 SCTP_INP_INFO_WLOCK();
3755
3756 SCTP_INP_WLOCK(inp);
3757 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3758 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3759 /* socket is gone, so no more wakeups allowed */
3760 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3761 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3762 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3763
3764 }
3765 /* First time through we have the socket lock, after that no more. */
3766 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
tuexen63fc0bb2011-12-27 12:24:52 +00003767 SCTP_FROM_SCTP_PCB+SCTP_LOC_1);
tuexendd729232011-11-01 23:04:43 +00003768
3769 if (inp->control) {
3770 sctp_m_freem(inp->control);
3771 inp->control = NULL;
3772 }
3773 if (inp->pkt) {
3774 sctp_m_freem(inp->pkt);
3775 inp->pkt = NULL;
3776 }
tuexendd729232011-11-01 23:04:43 +00003777 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3778 * here but I will be nice :> (i.e.
3779 * ip_pcb = ep;) */
3780 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3781 int cnt_in_sd;
3782
3783 cnt_in_sd = 0;
3784 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3785 SCTP_TCB_LOCK(asoc);
3786 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3787 /* Skip guys being freed */
3788 cnt_in_sd++;
3789 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3790 /*
3791 * Special case - we did not start a kill
3792 * timer on the asoc due to it was not
3793 * closed. So go ahead and start it now.
3794 */
3795 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3796 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3797 }
3798 SCTP_TCB_UNLOCK(asoc);
3799 continue;
3800 }
3801 if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3802 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
3803 (asoc->asoc.total_output_queue_size == 0)) {
3804 /* If we have data in queue, we don't want to just
3805 * free since the app may have done, send()/close
3806 * or connect/send/close. And it wants the data
3807 * to get across first.
3808 */
3809 /* Just abandon things in the front states */
3810 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3811 SCTP_FROM_SCTP_PCB+SCTP_LOC_2) == 0) {
3812 cnt_in_sd++;
3813 }
3814 continue;
3815 }
3816 /* Disconnect the socket please */
3817 asoc->sctp_socket = NULL;
3818 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3819 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3820 (asoc->asoc.control_pdapi) ||
3821 (asoc->asoc.size_on_all_streams > 0) ||
tuexen63fc0bb2011-12-27 12:24:52 +00003822 (so && (so->so_rcv.sb_cc > 0))) {
tuexendd729232011-11-01 23:04:43 +00003823 /* Left with Data unread */
3824 struct mbuf *op_err;
3825
3826 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3827 0, M_DONTWAIT, 1, MT_DATA);
3828 if (op_err) {
3829 /* Fill in the user initiated abort */
3830 struct sctp_paramhdr *ph;
3831 uint32_t *ippp;
3832
3833 SCTP_BUF_LEN(op_err) =
3834 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
3835 ph = mtod(op_err,
3836 struct sctp_paramhdr *);
3837 ph->param_type = htons(
3838 SCTP_CAUSE_USER_INITIATED_ABT);
3839 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3840 ippp = (uint32_t *) (ph + 1);
3841 *ippp = htonl(SCTP_FROM_SCTP_PCB+SCTP_LOC_3);
3842 }
3843 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_3;
tuexendd729232011-11-01 23:04:43 +00003844 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3845 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3846 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3847 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3848 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3849 }
3850 if (sctp_free_assoc(inp, asoc,
3851 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_4) == 0) {
3852 cnt_in_sd++;
3853 }
3854 continue;
3855 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3856 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3857 (asoc->asoc.stream_queue_cnt == 0)
3858 ) {
3859 if (asoc->asoc.locked_on_sending) {
3860 goto abort_anyway;
3861 }
3862 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3863 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3864 struct sctp_nets *netp;
3865
3866 if (asoc->asoc.alternate) {
3867 netp = asoc->asoc.alternate;
3868 } else {
3869 netp = asoc->asoc.primary_destination;
3870 }
3871 /*
3872 * there is nothing queued to send,
3873 * so I send shutdown
3874 */
3875 sctp_send_shutdown(asoc, netp);
3876 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3877 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3878 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3879 }
3880 SCTP_SET_STATE(&asoc->asoc,SCTP_STATE_SHUTDOWN_SENT);
3881 SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3882 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3883 netp);
3884 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3885 asoc->asoc.primary_destination);
3886 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3887 }
3888 } else {
3889 /* mark into shutdown pending */
3890 struct sctp_stream_queue_pending *sp;
3891
3892 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3893 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3894 asoc->asoc.primary_destination);
3895 if (asoc->asoc.locked_on_sending) {
3896 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3897 sctp_streamhead);
3898 if (sp == NULL) {
3899 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
3900 asoc->asoc.locked_on_sending,
3901 asoc->asoc.locked_on_sending->stream_no);
3902 } else {
3903 if ((sp->length == 0) && (sp->msg_is_complete == 0))
3904 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3905 }
3906 }
3907 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3908 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3909 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3910 struct mbuf *op_err;
3911 abort_anyway:
3912 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3913 0, M_DONTWAIT, 1, MT_DATA);
3914 if (op_err) {
3915 /* Fill in the user initiated abort */
3916 struct sctp_paramhdr *ph;
3917 uint32_t *ippp;
3918 SCTP_BUF_LEN(op_err) =
3919 (sizeof(struct sctp_paramhdr) +
3920 sizeof(uint32_t));
3921 ph = mtod(op_err,
3922 struct sctp_paramhdr *);
3923 ph->param_type = htons(
3924 SCTP_CAUSE_USER_INITIATED_ABT);
3925 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3926 ippp = (uint32_t *) (ph + 1);
3927 *ippp = htonl(SCTP_FROM_SCTP_PCB+SCTP_LOC_5);
3928 }
3929 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_5;
tuexendd729232011-11-01 23:04:43 +00003930 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3931 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3932 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3933 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3934 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3935 }
3936 if (sctp_free_assoc(inp, asoc,
3937 SCTP_PCBFREE_NOFORCE,
3938 SCTP_FROM_SCTP_PCB+SCTP_LOC_6) == 0) {
3939 cnt_in_sd++;
3940 }
3941 continue;
3942 } else {
3943 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3944 }
3945 }
3946 cnt_in_sd++;
3947 SCTP_TCB_UNLOCK(asoc);
3948 }
3949 /* now is there some left in our SHUTDOWN state? */
3950 if (cnt_in_sd) {
3951#ifdef SCTP_LOG_CLOSING
3952 sctp_log_closing(inp, NULL, 2);
3953#endif
3954 inp->sctp_socket = NULL;
3955 SCTP_INP_WUNLOCK(inp);
3956 SCTP_ASOC_CREATE_UNLOCK(inp);
3957 SCTP_INP_INFO_WUNLOCK();
3958 return;
3959 }
3960 }
3961 inp->sctp_socket = NULL;
3962 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3963 SCTP_PCB_FLAGS_UNBOUND) {
3964 /*
3965 * ok, this guy has been bound. It's port is
3966 * somewhere in the SCTP_BASE_INFO(hash table). Remove
3967 * it!
3968 */
3969 LIST_REMOVE(inp, sctp_hash);
3970 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3971 }
3972
3973 /* If there is a timer running to kill us,
3974 * forget it, since it may have a contest
3975 * on the INP lock.. which would cause us
3976 * to die ...
3977 */
3978 cnt = 0;
3979 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3980 SCTP_TCB_LOCK(asoc);
3981 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3982 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3983 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3984 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3985 }
3986 cnt++;
3987 SCTP_TCB_UNLOCK(asoc);
3988 continue;
3989 }
3990 /* Free associations that are NOT killing us */
3991 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3992 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3993 struct mbuf *op_err;
3994 uint32_t *ippp;
3995 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3996 0, M_DONTWAIT, 1, MT_DATA);
3997 if (op_err) {
3998 /* Fill in the user initiated abort */
3999 struct sctp_paramhdr *ph;
4000
4001 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
4002 sizeof(uint32_t));
4003 ph = mtod(op_err, struct sctp_paramhdr *);
4004 ph->param_type = htons(
4005 SCTP_CAUSE_USER_INITIATED_ABT);
4006 ph->param_length = htons(SCTP_BUF_LEN(op_err));
4007 ippp = (uint32_t *) (ph + 1);
4008 *ippp = htonl(SCTP_FROM_SCTP_PCB+SCTP_LOC_7);
4009
4010 }
4011 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_7;
tuexendd729232011-11-01 23:04:43 +00004012 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4013 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4014 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4015 cnt++;
4016 SCTP_TCB_UNLOCK(asoc);
4017 continue;
4018 }
4019 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
4020 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4021 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4022 }
4023 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_8) == 0) {
4024 cnt++;
4025 }
4026 }
4027 if (cnt) {
4028 /* Ok we have someone out there that will kill us */
4029 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4030#ifdef SCTP_LOG_CLOSING
4031 sctp_log_closing(inp, NULL, 3);
4032#endif
4033 SCTP_INP_WUNLOCK(inp);
4034 SCTP_ASOC_CREATE_UNLOCK(inp);
4035 SCTP_INP_INFO_WUNLOCK();
4036 return;
4037 }
4038 if (SCTP_INP_LOCK_CONTENDED(inp))
4039 being_refed++;
4040 if (SCTP_INP_READ_CONTENDED(inp))
4041 being_refed++;
tuexen63fc0bb2011-12-27 12:24:52 +00004042 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
tuexendd729232011-11-01 23:04:43 +00004043 being_refed++;
4044
tuexen63fc0bb2011-12-27 12:24:52 +00004045 if ((inp->refcount) ||
tuexendd729232011-11-01 23:04:43 +00004046 (being_refed) ||
4047 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
4048 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4049#ifdef SCTP_LOG_CLOSING
4050 sctp_log_closing(inp, NULL, 4);
4051#endif
4052 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4053 SCTP_INP_WUNLOCK(inp);
4054 SCTP_ASOC_CREATE_UNLOCK(inp);
4055 SCTP_INP_INFO_WUNLOCK();
4056 return;
4057 }
4058 inp->sctp_ep.signature_change.type = 0;
4059 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4060 /* Remove it from the list .. last thing we need a
4061 * lock for.
4062 */
4063 LIST_REMOVE(inp, sctp_list);
4064 SCTP_INP_WUNLOCK(inp);
4065 SCTP_ASOC_CREATE_UNLOCK(inp);
4066 SCTP_INP_INFO_WUNLOCK();
4067 /* Now we release all locks. Since this INP
4068 * cannot be found anymore except possibly by the
4069 * kill timer that might be running. We call
4070 * the drain function here. It should hit the case
4071 * were it sees the ACTIVE flag cleared and exit
4072 * out freeing us to proceed and destroy everything.
4073 */
4074 if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
4075 (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
4076 } else {
4077 /* Probably un-needed */
4078 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
4079 }
4080
4081#ifdef SCTP_LOG_CLOSING
4082 sctp_log_closing(inp, NULL, 5);
4083#endif
4084
4085#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4086#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4087 rt = ip_pcb->inp_route.ro_rt;
4088#endif
4089#endif
4090
4091#if defined(__Panda__)
4092 if (inp->pak_to_read) {
4093 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_timer.timer);
4094 SCTP_RELEASE_PKT(inp->pak_to_read);
4095 inp->pak_to_read = NULL;
4096 }
4097 if (inp->pak_to_read_sendq) {
4098 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_sendq_timer.timer);
4099 SCTP_RELEASE_PKT(inp->pak_to_read_sendq);
4100 inp->pak_to_read_sendq = NULL;
4101 }
4102#endif
4103 if ((inp->sctp_asocidhash) != NULL) {
4104 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4105 inp->sctp_asocidhash = NULL;
4106 }
4107 /*sa_ignore FREED_MEMORY*/
4108 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4109 /* Its only abandoned if it had data left */
4110 if (sq->length)
4111 SCTP_STAT_INCR(sctps_left_abandon);
4112
4113 TAILQ_REMOVE(&inp->read_queue, sq, next);
4114 sctp_free_remote_addr(sq->whoFrom);
4115 if (so)
4116 so->so_rcv.sb_cc -= sq->length;
4117 if (sq->data) {
4118 sctp_m_freem(sq->data);
4119 sq->data = NULL;
4120 }
4121 /*
4122 * no need to free the net count, since at this point all
4123 * assoc's are gone.
4124 */
4125 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
4126 SCTP_DECR_READQ_COUNT();
4127 }
4128 /* Now the sctp_pcb things */
4129 /*
4130 * free each asoc if it is not already closed/free. we can't use the
4131 * macro here since le_next will get freed as part of the
4132 * sctp_free_assoc() call.
4133 */
tuexendd729232011-11-01 23:04:43 +00004134 if (so) {
4135#ifdef IPSEC
4136 ipsec_delete_pcbpolicy(ip_pcb);
4137#endif /* IPSEC */
4138
4139 /* Unlocks not needed since the socket is gone now */
4140 }
4141#ifndef __Panda__
4142 if (ip_pcb->inp_options) {
4143 (void)sctp_m_free(ip_pcb->inp_options);
4144 ip_pcb->inp_options = 0;
4145 }
4146#endif
4147
4148#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4149#if !defined(__FreeBSD__) || __FreeBSD_version < 500000
4150 if (rt) {
4151 RTFREE(rt);
4152 ip_pcb->inp_route.ro_rt = 0;
4153 }
4154#endif
tuexen5fc22842012-02-11 13:32:19 +00004155#if defined(__FreeBSD__) && __FreeBSD_version < 803000
tuexendd729232011-11-01 23:04:43 +00004156#ifdef INET
4157 if (ip_pcb->inp_moptions) {
4158 inp_freemoptions(ip_pcb->inp_moptions);
4159 ip_pcb->inp_moptions = 0;
4160 }
4161#endif
4162#endif
4163#endif
4164
4165#ifdef INET6
4166#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
4167 if (inp->inp_vflag & INP_IPV6) {
4168#else
4169 if (ip_pcb->inp_vflag & INP_IPV6) {
4170#endif
4171 struct in6pcb *in6p;
4172
4173 in6p = (struct in6pcb *)inp;
4174#if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__))
4175 ip6_freepcbopts(in6p->in6p_outputopts);
4176#endif
4177 }
4178#endif /* INET6 */
4179#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
4180 inp->inp_vflag = 0;
4181#else
4182 ip_pcb->inp_vflag = 0;
4183#endif
4184 /* free up authentication fields */
4185 if (inp->sctp_ep.local_auth_chunks != NULL)
4186 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4187 if (inp->sctp_ep.local_hmacs != NULL)
4188 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4189
4190 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4191 LIST_REMOVE(shared_key, next);
4192 sctp_free_sharedkey(shared_key);
4193 /*sa_ignore FREED_MEMORY*/
4194 }
4195
4196#if defined(__APPLE__)
4197 inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
tuexen6d01b942012-05-04 18:15:17 +00004198 if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
tuexen49fdcc82012-05-04 10:13:12 +00004199#ifdef INVARIANTS
tuexendd729232011-11-01 23:04:43 +00004200 panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", inp);
tuexen6d01b942012-05-04 18:15:17 +00004201#else
4202 SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", inp);
tuexen49fdcc82012-05-04 10:13:12 +00004203#endif
tuexen6d01b942012-05-04 18:15:17 +00004204 }
tuexendd729232011-11-01 23:04:43 +00004205 inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4206#endif
4207 /*
4208 * if we have an address list the following will free the list of
4209 * ifaddr's that are set into this ep. Again macro limitations here,
4210 * since the LIST_FOREACH could be a bad idea.
4211 */
4212 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4213 sctp_remove_laddr(laddr);
4214 }
4215
4216#ifdef SCTP_TRACK_FREED_ASOCS
4217 /* TEMP CODE */
4218 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
4219 LIST_REMOVE(asoc, sctp_tcblist);
4220 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
4221 SCTP_DECR_ASOC_COUNT();
4222 }
4223 /* *** END TEMP CODE ****/
4224#endif
4225#ifdef SCTP_MVRF
4226 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4227#endif
4228 /* Now lets see about freeing the EP hash table. */
4229 if (inp->sctp_tcbhash != NULL) {
4230 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4231 inp->sctp_tcbhash = NULL;
4232 }
4233 /* Now we must put the ep memory back into the zone pool */
4234#if defined(__FreeBSD__)
4235 INP_LOCK_DESTROY(&inp->ip_inp.inp);
4236#endif
4237 SCTP_INP_LOCK_DESTROY(inp);
4238 SCTP_INP_READ_DESTROY(inp);
4239 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
4240#if !defined(__APPLE__)
4241 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4242 SCTP_DECR_EP_COUNT();
4243#else
4244 /* For Tiger, we will do this later... */
4245#endif
4246}
4247
4248
4249struct sctp_nets *
4250sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4251{
4252 struct sctp_nets *net;
4253 /* locate the address */
4254 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4255 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4256 return (net);
4257 }
4258 return (NULL);
4259}
4260
4261
4262int
4263sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4264{
4265#ifdef __Panda__
4266 return (0);
4267#else
4268 struct sctp_ifa *sctp_ifa;
4269 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4270 if (sctp_ifa) {
4271 return (1);
4272 } else {
4273 return (0);
4274 }
4275#endif
4276}
4277
4278/*
4279 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4280 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4281 * stats of stuff.
4282 */
4283int
4284sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
4285 struct sctp_nets **netp, int set_scope, int from)
4286{
4287 /*
4288 * The following is redundant to the same lines in the
4289 * sctp_aloc_assoc() but is needed since others call the add
4290 * address function
4291 */
4292 struct sctp_nets *net, *netfirst;
4293 int addr_inscope;
4294
4295 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4296 from);
4297 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4298
4299 netfirst = sctp_findnet(stcb, newaddr);
4300 if (netfirst) {
4301 /*
4302 * Lie and return ok, we don't want to make the association
4303 * go away for this behavior. It will happen in the TCP
4304 * model in a connected socket. It does not reach the hash
4305 * table until after the association is built so it can't be
4306 * found. Mark as reachable, since the initial creation will
4307 * have been cleared and the NOT_IN_ASSOC flag will have
4308 * been added... and we don't want to end up removing it
4309 * back out.
4310 */
4311 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4312 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4313 SCTP_ADDR_UNCONFIRMED);
4314 } else {
4315 netfirst->dest_state = SCTP_ADDR_REACHABLE;
4316 }
4317
4318 return (0);
4319 }
4320 addr_inscope = 1;
4321 switch (newaddr->sa_family) {
4322#ifdef INET
4323 case AF_INET:
4324 {
4325 struct sockaddr_in *sin;
4326
4327 sin = (struct sockaddr_in *)newaddr;
4328 if (sin->sin_addr.s_addr == 0) {
4329 /* Invalid address */
4330 return (-1);
4331 }
4332 /* zero out the bzero area */
4333 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4334
4335 /* assure len is set */
tuexen6019b072011-12-15 18:38:03 +00004336#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00004337 sin->sin_len = sizeof(struct sockaddr_in);
4338#endif
4339 if (set_scope) {
4340#ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
4341 stcb->ipv4_local_scope = 1;
4342#else
4343 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
4344 stcb->asoc.ipv4_local_scope = 1;
4345 }
4346#endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
4347 } else {
4348 /* Validate the address is in scope */
4349 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
4350 (stcb->asoc.ipv4_local_scope == 0)) {
4351 addr_inscope = 0;
4352 }
4353 }
4354 break;
4355 }
4356#endif
4357#ifdef INET6
4358 case AF_INET6:
4359 {
4360 struct sockaddr_in6 *sin6;
4361
4362 sin6 = (struct sockaddr_in6 *)newaddr;
4363 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4364 /* Invalid address */
4365 return (-1);
4366 }
4367 /* assure len is set */
tuexenda6f8ba2012-01-08 11:33:02 +00004368#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00004369 sin6->sin6_len = sizeof(struct sockaddr_in6);
4370#endif
4371 if (set_scope) {
4372 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
4373 stcb->asoc.loopback_scope = 1;
4374 stcb->asoc.local_scope = 0;
4375 stcb->asoc.ipv4_local_scope = 1;
4376 stcb->asoc.site_scope = 1;
4377 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4378 /*
4379 * If the new destination is a LINK_LOCAL we
4380 * must have common site scope. Don't set
4381 * the local scope since we may not share
4382 * all links, only loopback can do this.
4383 * Links on the local network would also be
4384 * on our private network for v4 too.
4385 */
4386 stcb->asoc.ipv4_local_scope = 1;
4387 stcb->asoc.site_scope = 1;
4388 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4389 /*
4390 * If the new destination is SITE_LOCAL then
4391 * we must have site scope in common.
4392 */
4393 stcb->asoc.site_scope = 1;
4394 }
4395 } else {
4396 /* Validate the address is in scope */
4397 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
4398 (stcb->asoc.loopback_scope == 0)) {
4399 addr_inscope = 0;
4400 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
4401 (stcb->asoc.local_scope == 0)) {
4402 addr_inscope = 0;
4403 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
4404 (stcb->asoc.site_scope == 0)) {
4405 addr_inscope = 0;
4406 }
4407 }
4408 break;
4409 }
4410#endif
tuexencc2b1422012-07-10 21:50:32 +00004411#if defined(__Userspace__)
4412 case AF_CONN:
4413 {
4414 struct sockaddr_conn *sconn;
4415
4416 sconn = (struct sockaddr_conn *)newaddr;
4417 if (sconn->sconn_addr == NULL) {
4418 /* Invalid address */
4419 return (-1);
4420 }
4421#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
4422 sconn->sconn_len = sizeof(struct sockaddr_conn);
4423#endif
4424 break;
4425 }
4426#endif
tuexendd729232011-11-01 23:04:43 +00004427 default:
4428 /* not supported family type */
4429 return (-1);
4430 }
4431 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4432 if (net == NULL) {
4433 return (-1);
4434 }
4435 SCTP_INCR_RADDR_COUNT();
4436 bzero(net, sizeof(struct sctp_nets));
4437 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
tuexen6019b072011-12-15 18:38:03 +00004438#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00004439 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
4440#endif
4441 switch (newaddr->sa_family) {
4442#ifdef INET
4443 case AF_INET:
tuexen9784e9a2011-12-18 13:04:23 +00004444#if defined(__Windows__) || defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00004445 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4446#endif
4447 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4448 break;
4449#endif
4450#ifdef INET6
4451 case AF_INET6:
tuexenda6f8ba2012-01-08 11:33:02 +00004452#if defined(__Windows__) || defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00004453 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4454#endif
4455 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4456 break;
4457#endif
tuexencc2b1422012-07-10 21:50:32 +00004458#if defined(__Userspace__)
4459 case AF_CONN:
4460#if defined(__Windows__) || defined(__Userspace_os_Linux) || defined(__Userspace_os_Windows)
4461 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4462#endif
4463 ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4464 break;
4465#endif
tuexendd729232011-11-01 23:04:43 +00004466 default:
4467 break;
4468 }
4469 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4470 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
4471 stcb->asoc.loopback_scope = 1;
4472 stcb->asoc.ipv4_local_scope = 1;
4473 stcb->asoc.local_scope = 0;
4474 stcb->asoc.site_scope = 1;
4475 addr_inscope = 1;
4476 }
4477 net->failure_threshold = stcb->asoc.def_net_failure;
4478 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4479 if (addr_inscope == 0) {
4480 net->dest_state = (SCTP_ADDR_REACHABLE |
4481 SCTP_ADDR_OUT_OF_SCOPE);
4482 } else {
4483 if (from == SCTP_ADDR_IS_CONFIRMED)
4484 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4485 net->dest_state = SCTP_ADDR_REACHABLE;
4486 else
4487 net->dest_state = SCTP_ADDR_REACHABLE |
4488 SCTP_ADDR_UNCONFIRMED;
4489 }
4490 /* We set this to 0, the timer code knows that
4491 * this means its an initial value
4492 */
4493 net->rto_needed = 1;
4494 net->RTO = 0;
4495 net->RTO_measured = 0;
4496 stcb->asoc.numnets++;
tuexen9d7b2072011-11-20 16:35:17 +00004497 net->ref_count = 1;
tuexendd729232011-11-01 23:04:43 +00004498 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
tuexen9d7b2072011-11-20 16:35:17 +00004499 net->port = stcb->asoc.port;
tuexendd729232011-11-01 23:04:43 +00004500 net->dscp = stcb->asoc.default_dscp;
4501#ifdef INET6
4502 net->flowlabel = stcb->asoc.default_flowlabel;
4503#endif
4504 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4505 net->dest_state |= SCTP_ADDR_NOHB;
4506 } else {
4507 net->dest_state &= ~SCTP_ADDR_NOHB;
4508 }
4509 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4510 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4511 } else {
4512 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4513 }
4514 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4515 /* Init the timer structure */
4516 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4517 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4518 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4519
4520 /* Now generate a route for this guy */
4521#ifdef INET6
4522#ifdef SCTP_EMBEDDED_V6_SCOPE
4523 /* KAME hack: embed scopeid */
4524 if (newaddr->sa_family == AF_INET6) {
4525 struct sockaddr_in6 *sin6;
4526
4527 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
tuexene3851592012-06-01 08:31:45 +00004528#if defined(__APPLE__)
tuexen9a218b12012-08-04 21:17:58 +00004529#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4530 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
tuexendd729232011-11-01 23:04:43 +00004531#else
tuexen9a218b12012-08-04 21:17:58 +00004532 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
tuexendd729232011-11-01 23:04:43 +00004533#endif
4534#elif defined(SCTP_KAME)
4535 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4536#else
4537 (void)in6_embedscope(&sin6->sin6_addr, sin6);
4538#endif
4539#ifndef SCOPEDROUTING
4540 sin6->sin6_scope_id = 0;
4541#endif
4542 }
4543#endif /* SCTP_EMBEDDED_V6_SCOPE */
4544#endif
4545 SCTP_RTALLOC((sctp_route_t *)&net->ro, stcb->asoc.vrf_id);
4546
4547 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4548 /* Get source address */
4549 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
4550 stcb,
4551 (sctp_route_t *)&net->ro,
4552 net,
4553 0,
4554 stcb->asoc.vrf_id);
4555 /* Now get the interface MTU */
4556 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
4557 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
tuexendd729232011-11-01 23:04:43 +00004558 }
tuexene81177b2011-11-15 20:48:08 +00004559 if (net->mtu > 0) {
tuexendd729232011-11-01 23:04:43 +00004560 uint32_t rmtu;
4561
4562 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
4563 if (rmtu == 0) {
4564 /* Start things off to match mtu of interface please. */
4565 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
4566 net->ro.ro_rt, net->mtu);
4567 } else {
4568 /* we take the route mtu over the interface, since
4569 * the route may be leading out the loopback, or
4570 * a different interface.
4571 */
4572 net->mtu = rmtu;
4573 }
4574 }
tuexen09631162012-04-18 11:23:02 +00004575 }
tuexene81177b2011-11-15 20:48:08 +00004576 if (net->mtu == 0) {
4577 switch (newaddr->sa_family) {
4578#ifdef INET
4579 case AF_INET:
4580 net->mtu = SCTP_DEFAULT_MTU;
4581 break;
4582#endif
4583#ifdef INET6
4584 case AF_INET6:
4585 net->mtu = 1280;
4586 break;
4587#endif
tuexencc2b1422012-07-10 21:50:32 +00004588#if defined(__Userspace__)
4589 case AF_CONN:
4590 net->mtu = 1280;
4591 break;
4592#endif
tuexene81177b2011-11-15 20:48:08 +00004593 default:
4594 break;
tuexendd729232011-11-01 23:04:43 +00004595 }
tuexene81177b2011-11-15 20:48:08 +00004596 }
4597 if (net->port) {
4598 net->mtu -= (uint32_t)sizeof(struct udphdr);
4599 }
4600 if (from == SCTP_ALLOC_ASOC) {
4601 stcb->asoc.smallest_mtu = net->mtu;
4602 }
4603 if (stcb->asoc.smallest_mtu > net->mtu) {
4604 stcb->asoc.smallest_mtu = net->mtu;
tuexendd729232011-11-01 23:04:43 +00004605 }
4606#ifdef INET6
4607#ifdef SCTP_EMBEDDED_V6_SCOPE
4608 if (newaddr->sa_family == AF_INET6) {
4609 struct sockaddr_in6 *sin6;
4610
4611 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4612#ifdef SCTP_KAME
4613 (void)sa6_recoverscope(sin6);
4614#else
4615 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4616#endif /* SCTP_KAME */
4617 }
4618#endif /* SCTP_EMBEDDED_V6_SCOPE */
4619#endif
tuexendd729232011-11-01 23:04:43 +00004620
4621 /* JRS - Use the congestion control given in the CC module */
tuexen09631162012-04-18 11:23:02 +00004622 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
tuexendd729232011-11-01 23:04:43 +00004623 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4624
4625 /*
4626 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4627 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4628 */
4629 net->find_pseudo_cumack = 1;
4630 net->find_rtx_pseudo_cumack = 1;
4631 net->src_addr_selected = 0;
4632#if defined(__FreeBSD__)
4633 /* Choose an initial flowid. */
4634 net->flowid = stcb->asoc.my_vtag ^
4635 ntohs(stcb->rport) ^
4636 ntohs(stcb->sctp_ep->sctp_lport);
4637#ifdef INVARIANTS
4638 net->flowidset = 1;
4639#endif
4640#endif
4641 if (netp) {
4642 *netp = net;
4643 }
4644 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4645 if (net->ro.ro_rt == NULL) {
4646 /* Since we have no route put it at the back */
4647 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4648 } else if (netfirst == NULL) {
4649 /* We are the first one in the pool. */
4650 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4651 } else if (netfirst->ro.ro_rt == NULL) {
4652 /*
4653 * First one has NO route. Place this one ahead of the first
4654 * one.
4655 */
4656 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4657#ifndef __Panda__
4658 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4659 /*
4660 * This one has a different interface than the one at the
4661 * top of the list. Place it ahead.
4662 */
4663 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4664#endif
4665 } else {
4666 /*
4667 * Ok we have the same interface as the first one. Move
4668 * forward until we find either a) one with a NULL route...
4669 * insert ahead of that b) one with a different ifp.. insert
4670 * after that. c) end of the list.. insert at the tail.
4671 */
4672 struct sctp_nets *netlook;
4673
4674 do {
4675 netlook = TAILQ_NEXT(netfirst, sctp_next);
4676 if (netlook == NULL) {
4677 /* End of the list */
4678 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4679 break;
4680 } else if (netlook->ro.ro_rt == NULL) {
4681 /* next one has NO route */
4682 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4683 break;
4684 }
4685#ifndef __Panda__
4686 else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp)
4687#else
4688 else
4689#endif
4690 {
4691 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4692 net, sctp_next);
4693 break;
4694 }
4695#ifndef __Panda__
4696 /* Shift forward */
4697 netfirst = netlook;
4698#endif
4699 } while (netlook != NULL);
4700 }
4701
4702 /* got to have a primary set */
4703 if (stcb->asoc.primary_destination == 0) {
4704 stcb->asoc.primary_destination = net;
4705 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4706 (net->ro.ro_rt) &&
4707 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4708 /* No route to current primary adopt new primary */
4709 stcb->asoc.primary_destination = net;
4710 }
4711 /* Validate primary is first */
4712 net = TAILQ_FIRST(&stcb->asoc.nets);
4713 if ((net != stcb->asoc.primary_destination) &&
4714 (stcb->asoc.primary_destination)) {
4715 /* first one on the list is NOT the primary
4716 * sctp_cmpaddr() is much more efficient if
4717 * the primary is the first on the list, make it
4718 * so.
4719 */
4720 TAILQ_REMOVE(&stcb->asoc.nets,
4721 stcb->asoc.primary_destination, sctp_next);
4722 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4723 stcb->asoc.primary_destination, sctp_next);
4724 }
4725 return (0);
4726}
4727
4728
4729static uint32_t
4730sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4731{
4732 uint32_t id;
4733 struct sctpasochead *head;
4734 struct sctp_tcb *lstcb;
4735
4736 SCTP_INP_WLOCK(inp);
4737 try_again:
4738 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4739 /* TSNH */
4740 SCTP_INP_WUNLOCK(inp);
4741 return (0);
4742 }
4743 /*
4744 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4745 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4746 */
4747 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4748 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4749 }
4750 id = inp->sctp_associd_counter;
4751 inp->sctp_associd_counter++;
4752 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4753 if (lstcb) {
4754 goto try_again;
4755 }
4756 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4757 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4758 stcb->asoc.in_asocid_hash = 1;
4759 SCTP_INP_WUNLOCK(inp);
4760 return id;
4761}
4762
4763/*
4764 * allocate an association and add it to the endpoint. The caller must be
4765 * careful to add all additional addresses once they are know right away or
4766 * else the assoc will be may experience a blackout scenario.
4767 */
4768struct sctp_tcb *
4769sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4770 int *error, uint32_t override_tag, uint32_t vrf_id,
4771#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
4772 struct thread *p
4773#elif defined(__Windows__)
4774 PKTHREAD p
4775#else
4776#if defined(__Userspace__)
4777 /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
4778#endif
4779 struct proc *p
4780#endif
4781)
4782{
4783 /* note the p argument is only valid in unbound sockets */
4784
4785 struct sctp_tcb *stcb;
4786 struct sctp_association *asoc;
4787 struct sctpasochead *head;
4788 uint16_t rport;
4789 int err;
4790
4791 /*
4792 * Assumption made here: Caller has done a
4793 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4794 * address does not exist already.
4795 */
4796 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4797 /* Hit max assoc, sorry no more */
4798 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4799 *error = ENOBUFS;
4800 return (NULL);
4801 }
4802 if (firstaddr == NULL) {
4803 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4804 *error = EINVAL;
4805 return (NULL);
4806 }
4807 SCTP_INP_RLOCK(inp);
4808 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4809 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4810 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4811 /*
4812 * If its in the TCP pool, its NOT allowed to create an
4813 * association. The parent listener needs to call
4814 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4815 * off, or connected one does this.. its an error.
4816 */
4817 SCTP_INP_RUNLOCK(inp);
4818 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4819 *error = EINVAL;
4820 return (NULL);
4821 }
4822 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4823 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4824 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4825 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4826 SCTP_INP_RUNLOCK(inp);
4827 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4828 *error = EINVAL;
4829 return (NULL);
4830 }
4831 }
4832 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4833#ifdef SCTP_DEBUG
4834 if (firstaddr) {
4835 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4836 switch (firstaddr->sa_family) {
4837#ifdef INET
4838 case AF_INET:
4839 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4840 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4841 break;
4842#endif
4843#ifdef INET6
4844 case AF_INET6:
4845 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4846 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4847 break;
4848#endif
tuexencc2b1422012-07-10 21:50:32 +00004849#if defined(__Userspace__)
4850 case AF_CONN:
4851 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4852 ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
4853 break;
4854#endif
tuexendd729232011-11-01 23:04:43 +00004855 default:
4856 break;
4857 }
4858 } else {
4859 SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
4860 }
4861#endif /* SCTP_DEBUG */
4862 switch (firstaddr->sa_family) {
4863#ifdef INET
4864 case AF_INET:
4865 {
4866 struct sockaddr_in *sin;
4867
4868 sin = (struct sockaddr_in *)firstaddr;
4869 if ((ntohs(sin->sin_port) == 0) ||
4870 (sin->sin_addr.s_addr == INADDR_ANY) ||
4871 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
4872 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
4873 /* Invalid address */
4874 SCTP_INP_RUNLOCK(inp);
4875 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4876 *error = EINVAL;
4877 return (NULL);
4878 }
4879 rport = sin->sin_port;
4880 break;
4881 }
4882#endif
4883#ifdef INET6
4884 case AF_INET6:
4885 {
4886 struct sockaddr_in6 *sin6;
4887
4888 sin6 = (struct sockaddr_in6 *)firstaddr;
4889 if ((ntohs(sin6->sin6_port) == 0) ||
4890 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4891 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4892 /* Invalid address */
4893 SCTP_INP_RUNLOCK(inp);
4894 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4895 *error = EINVAL;
4896 return (NULL);
4897 }
4898 rport = sin6->sin6_port;
4899 break;
4900 }
4901#endif
tuexencc2b1422012-07-10 21:50:32 +00004902#if defined(__Userspace__)
4903 case AF_CONN:
4904 {
4905 struct sockaddr_conn *sconn;
4906
4907 sconn = (struct sockaddr_conn *)firstaddr;
4908 if ((ntohs(sconn->sconn_port) == 0) ||
4909 (sconn->sconn_addr == NULL)) {
4910 /* Invalid address */
4911 SCTP_INP_RUNLOCK(inp);
4912 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4913 *error = EINVAL;
4914 return (NULL);
4915 }
4916 rport = sconn->sconn_port;
4917 break;
4918 }
4919#endif
tuexendd729232011-11-01 23:04:43 +00004920 default:
4921 /* not supported family type */
4922 SCTP_INP_RUNLOCK(inp);
4923 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4924 *error = EINVAL;
4925 return (NULL);
4926 }
4927 SCTP_INP_RUNLOCK(inp);
4928 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4929 /*
4930 * If you have not performed a bind, then we need to do the
4931 * ephemeral bind for you.
4932 */
4933 if ((err = sctp_inpcb_bind(inp->sctp_socket,
4934 (struct sockaddr *)NULL,
4935 (struct sctp_ifa *)NULL,
4936#ifndef __Panda__
4937 p
4938#else
4939 (struct proc *)NULL
4940#endif
4941 ))) {
4942 /* bind error, probably perm */
4943 *error = err;
4944 return (NULL);
4945 }
4946 }
4947 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4948 if (stcb == NULL) {
4949 /* out of memory? */
4950 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4951 *error = ENOMEM;
4952 return (NULL);
4953 }
4954 SCTP_INCR_ASOC_COUNT();
4955
4956 bzero(stcb, sizeof(*stcb));
4957 asoc = &stcb->asoc;
4958
4959 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4960 SCTP_TCB_LOCK_INIT(stcb);
4961 SCTP_TCB_SEND_LOCK_INIT(stcb);
4962 stcb->rport = rport;
4963 /* setup back pointer's */
4964 stcb->sctp_ep = inp;
4965 stcb->sctp_socket = inp->sctp_socket;
4966 if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) {
4967 /* failed */
4968 SCTP_TCB_LOCK_DESTROY(stcb);
4969 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4970 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4971 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4972 SCTP_DECR_ASOC_COUNT();
4973 *error = err;
4974 return (NULL);
4975 }
4976 /* and the port */
4977 SCTP_INP_INFO_WLOCK();
4978 SCTP_INP_WLOCK(inp);
4979 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4980 /* inpcb freed while alloc going on */
4981 SCTP_TCB_LOCK_DESTROY(stcb);
4982 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4983 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4984 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4985 SCTP_INP_WUNLOCK(inp);
4986 SCTP_INP_INFO_WUNLOCK();
4987 SCTP_DECR_ASOC_COUNT();
4988 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4989 *error = EINVAL;
4990 return (NULL);
4991 }
4992 SCTP_TCB_LOCK(stcb);
4993
4994 /* now that my_vtag is set, add it to the hash */
4995 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4996 /* put it in the bucket in the vtag hash of assoc's for the system */
4997 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4998 SCTP_INP_INFO_WUNLOCK();
4999
5000 if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
5001 /* failure.. memory error? */
5002 if (asoc->strmout) {
5003 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5004 asoc->strmout = NULL;
5005 }
5006 if (asoc->mapping_array) {
5007 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5008 asoc->mapping_array = NULL;
5009 }
5010 if (asoc->nr_mapping_array) {
5011 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5012 asoc->nr_mapping_array = NULL;
5013 }
5014 SCTP_DECR_ASOC_COUNT();
5015 SCTP_TCB_LOCK_DESTROY(stcb);
5016 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5017 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5018 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5019 SCTP_INP_WUNLOCK(inp);
5020 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
5021 *error = ENOBUFS;
5022 return (NULL);
5023 }
5024 /* Init all the timers */
5025 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
5026 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
5027 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
5028 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
5029 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
5030 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
5031 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5032
5033 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5034 /* now file the port under the hash as well */
5035 if (inp->sctp_tcbhash != NULL) {
5036 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5037 inp->sctp_hashmark)];
5038 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5039 }
5040 SCTP_INP_WUNLOCK(inp);
5041 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
5042 return (stcb);
5043}
5044
5045
5046void
5047sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5048{
5049 struct sctp_association *asoc;
5050
5051 asoc = &stcb->asoc;
5052 asoc->numnets--;
5053 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5054 if (net == asoc->primary_destination) {
5055 /* Reset primary */
5056 struct sctp_nets *lnet;
5057
5058 lnet = TAILQ_FIRST(&asoc->nets);
5059 /* Mobility adaptation
5060 Ideally, if deleted destination is the primary, it becomes
5061 a fast retransmission trigger by the subsequent SET PRIMARY.
5062 (by micchie)
5063 */
5064 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
5065 SCTP_MOBILITY_BASE) ||
5066 sctp_is_mobility_feature_on(stcb->sctp_ep,
5067 SCTP_MOBILITY_FASTHANDOFF)) {
5068 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5069 if (asoc->deleted_primary != NULL) {
5070 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5071 goto out;
5072 }
5073 asoc->deleted_primary = net;
5074 atomic_add_int(&net->ref_count, 1);
5075 memset(&net->lastsa, 0, sizeof(net->lastsa));
5076 memset(&net->lastsv, 0, sizeof(net->lastsv));
5077 sctp_mobility_feature_on(stcb->sctp_ep,
5078 SCTP_MOBILITY_PRIM_DELETED);
5079 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5080 stcb->sctp_ep, stcb, NULL);
5081 }
5082out:
5083 /* Try to find a confirmed primary */
5084 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5085 }
5086 if (net == asoc->last_data_chunk_from) {
5087 /* Reset primary */
5088 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5089 }
5090 if (net == asoc->last_control_chunk_from) {
5091 /* Clear net */
5092 asoc->last_control_chunk_from = NULL;
5093 }
5094 if (net == stcb->asoc.alternate) {
5095 sctp_free_remote_addr(stcb->asoc.alternate);
5096 stcb->asoc.alternate = NULL;
5097 }
5098 sctp_free_remote_addr(net);
5099}
5100
5101/*
5102 * remove a remote endpoint address from an association, it will fail if the
5103 * address does not exist.
5104 */
5105int
5106sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5107{
5108 /*
5109 * Here we need to remove a remote address. This is quite simple, we
5110 * first find it in the list of address for the association
5111 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5112 * on that item. Note we do not allow it to be removed if there are
5113 * no other addresses.
5114 */
5115 struct sctp_association *asoc;
5116 struct sctp_nets *net, *nnet;
5117
5118 asoc = &stcb->asoc;
5119
5120 /* locate the address */
5121 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5122 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5123 continue;
5124 }
5125 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5126 remaddr)) {
5127 /* we found the guy */
5128 if (asoc->numnets < 2) {
5129 /* Must have at LEAST two remote addresses */
5130 return (-1);
5131 } else {
5132 sctp_remove_net(stcb, net);
5133 return (0);
5134 }
5135 }
5136 }
5137 /* not found. */
5138 return (-2);
5139}
5140
5141void
5142sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5143{
5144 struct sctpvtaghead *chain;
5145 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005146 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005147 int i;
5148
5149 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5150 if (!LIST_EMPTY(chain)) {
5151 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5152 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5153 if ((twait_block->vtag_block[i].v_tag == tag) &&
5154 (twait_block->vtag_block[i].lport == lport) &&
5155 (twait_block->vtag_block[i].rport == rport)) {
5156 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5157 twait_block->vtag_block[i].v_tag = 0;
5158 twait_block->vtag_block[i].lport = 0;
5159 twait_block->vtag_block[i].rport = 0;
5160 found = 1;
5161 break;
5162 }
5163 }
5164 if (found)
5165 break;
5166 }
5167 }
5168}
5169
5170int
5171sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5172{
5173 struct sctpvtaghead *chain;
5174 struct sctp_tagblock *twait_block;
tuexen63fc0bb2011-12-27 12:24:52 +00005175 int found = 0;
tuexendd729232011-11-01 23:04:43 +00005176 int i;
5177
5178 SCTP_INP_INFO_WLOCK();
5179 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5180 if (!LIST_EMPTY(chain)) {
5181 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5182 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5183 if ((twait_block->vtag_block[i].v_tag == tag) &&
5184 (twait_block->vtag_block[i].lport == lport) &&
5185 (twait_block->vtag_block[i].rport == rport)) {
5186 found = 1;
5187 break;
5188 }
5189 }
5190 if (found)
5191 break;
5192 }
5193 }
5194 SCTP_INP_INFO_WUNLOCK();
tuexen63fc0bb2011-12-27 12:24:52 +00005195 return (found);
tuexendd729232011-11-01 23:04:43 +00005196}
5197
5198
5199void
5200sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
5201{
5202 struct sctpvtaghead *chain;
5203 struct sctp_tagblock *twait_block;
5204 struct timeval now;
5205 int set, i;
5206
5207 if (time == 0) {
5208 /* Its disabled */
5209 return;
5210 }
5211 (void)SCTP_GETTIME_TIMEVAL(&now);
5212 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5213 set = 0;
5214 if (!LIST_EMPTY(chain)) {
5215 /* Block(s) present, lets find space, and expire on the fly */
5216 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5217 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5218 if ((twait_block->vtag_block[i].v_tag == 0) &&
5219 !set) {
5220 twait_block->vtag_block[i].tv_sec_at_expire =
5221 now.tv_sec + time;
5222 twait_block->vtag_block[i].v_tag = tag;
5223 twait_block->vtag_block[i].lport = lport;
5224 twait_block->vtag_block[i].rport = rport;
5225 set = 1;
5226 } else if ((twait_block->vtag_block[i].v_tag) &&
5227 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
5228 /* Audit expires this guy */
5229 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5230 twait_block->vtag_block[i].v_tag = 0;
5231 twait_block->vtag_block[i].lport = 0;
5232 twait_block->vtag_block[i].rport = 0;
5233 if (set == 0) {
5234 /* Reuse it for my new tag */
5235 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
5236 twait_block->vtag_block[i].v_tag = tag;
5237 twait_block->vtag_block[i].lport = lport;
5238 twait_block->vtag_block[i].rport = rport;
5239 set = 1;
5240 }
5241 }
5242 }
5243 if (set) {
5244 /*
5245 * We only do up to the block where we can
5246 * place our tag for audits
5247 */
5248 break;
5249 }
5250 }
5251 }
5252 /* Need to add a new block to chain */
5253 if (!set) {
5254 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5255 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5256 if (twait_block == NULL) {
5257#ifdef INVARIANTS
5258 panic("Can not alloc tagblock");
5259#endif
5260 return;
5261 }
5262 memset(twait_block, 0, sizeof(struct sctp_tagblock));
5263 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
5264 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
5265 twait_block->vtag_block[0].v_tag = tag;
5266 twait_block->vtag_block[0].lport = lport;
5267 twait_block->vtag_block[0].rport = rport;
5268 }
5269}
5270
5271
5272#ifdef __Panda__
5273void panda_wakeup_socket(struct socket *so);
5274#endif
5275
5276/*-
5277 * Free the association after un-hashing the remote port. This
5278 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5279 * expect that the input to this function IS a locked TCB.
5280 * It will return 0, if it did NOT destroy the association (instead
5281 * it unlocks it. It will return NON-zero if it either destroyed the
5282 * association OR the association is already destroyed.
5283 */
5284int
5285sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5286{
5287 int i;
5288 struct sctp_association *asoc;
5289 struct sctp_nets *net, *nnet;
5290 struct sctp_laddr *laddr, *naddr;
5291 struct sctp_tmit_chunk *chk, *nchk;
5292 struct sctp_asconf_addr *aparam, *naparam;
5293 struct sctp_asconf_ack *aack, *naack;
5294 struct sctp_stream_reset_list *strrst, *nstrrst;
5295 struct sctp_queued_to_read *sq, *nsq;
5296 struct sctp_stream_queue_pending *sp, *nsp;
5297 sctp_sharedkey_t *shared_key, *nshared_key;
5298 struct socket *so;
5299
5300 /* first, lets purge the entry from the hash table. */
5301#if defined(__APPLE__)
5302 sctp_lock_assert(SCTP_INP_SO(inp));
5303#endif
5304
5305#ifdef SCTP_LOG_CLOSING
5306 sctp_log_closing(inp, stcb, 6);
5307#endif
5308 if (stcb->asoc.state == 0) {
5309#ifdef SCTP_LOG_CLOSING
5310 sctp_log_closing(inp, NULL, 7);
5311#endif
5312 /* there is no asoc, really TSNH :-0 */
5313 return (1);
5314 }
5315 if (stcb->asoc.alternate) {
5316 sctp_free_remote_addr(stcb->asoc.alternate);
5317 stcb->asoc.alternate = NULL;
5318 }
5319#if !defined(__APPLE__) /* TEMP: moved to below */
5320 /* TEMP CODE */
5321 if (stcb->freed_from_where == 0) {
5322 /* Only record the first place free happened from */
5323 stcb->freed_from_where = from_location;
5324 }
5325 /* TEMP CODE */
5326#endif
5327
5328 asoc = &stcb->asoc;
5329 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5330 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5331 /* nothing around */
5332 so = NULL;
5333 else
5334 so = inp->sctp_socket;
5335
5336 /*
5337 * We used timer based freeing if a reader or writer is in the way.
5338 * So we first check if we are actually being called from a timer,
5339 * if so we abort early if a reader or writer is still in the way.
5340 */
5341 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5342 (from_inpcbfree == SCTP_NORMAL_PROC)) {
5343 /*
5344 * is it the timer driving us? if so are the reader/writers
5345 * gone?
5346 */
5347 if (stcb->asoc.refcnt) {
5348 /* nope, reader or writer in the way */
5349 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5350 /* no asoc destroyed */
5351 SCTP_TCB_UNLOCK(stcb);
5352#ifdef SCTP_LOG_CLOSING
5353 sctp_log_closing(inp, stcb, 8);
5354#endif
5355 return (0);
5356 }
5357 }
5358 /* now clean up any other timers */
5359 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
5360 asoc->dack_timer.self = NULL;
5361 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
5362 /*-
5363 * For stream reset we don't blast this unless
5364 * it is a str-reset timer, it might be the
5365 * free-asoc timer which we DON'T want to
5366 * disturb.
5367 */
5368 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
5369 asoc->strreset_timer.self = NULL;
5370 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
5371 asoc->asconf_timer.self = NULL;
5372 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
5373 asoc->autoclose_timer.self = NULL;
5374 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
5375 asoc->shut_guard_timer.self = NULL;
5376 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
5377 asoc->delayed_event_timer.self = NULL;
5378 /* Mobility adaptation */
5379 (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
5380 asoc->delete_prim_timer.self = NULL;
5381 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5382 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
5383 net->rxt_timer.self = NULL;
5384 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
5385 net->pmtu_timer.self = NULL;
5386 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
5387 net->hb_timer.self = NULL;
5388 }
5389 /* Now the read queue needs to be cleaned up (only once) */
5390 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
5391 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
5392 SCTP_INP_READ_LOCK(inp);
5393 TAILQ_FOREACH(sq, &inp->read_queue, next) {
5394 if (sq->stcb == stcb) {
5395 sq->do_not_ref_stcb = 1;
5396 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5397 /* If there is no end, there never
5398 * will be now.
5399 */
5400 if (sq->end_added == 0) {
5401 /* Held for PD-API clear that. */
5402 sq->pdapi_aborted = 1;
5403 sq->held_length = 0;
5404 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
5405 /*
5406 * Need to add a PD-API aborted indication.
5407 * Setting the control_pdapi assures that it will
5408 * be added right after this msg.
5409 */
5410 uint32_t strseq;
5411 stcb->asoc.control_pdapi = sq;
5412 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
5413 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5414 stcb,
5415 SCTP_PARTIAL_DELIVERY_ABORTED,
5416 (void *)&strseq,
5417 SCTP_SO_LOCKED);
5418 stcb->asoc.control_pdapi = NULL;
5419 }
5420 }
5421 /* Add an end to wake them */
5422 sq->end_added = 1;
5423 }
5424 }
5425 SCTP_INP_READ_UNLOCK(inp);
5426 if (stcb->block_entry) {
5427 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
5428 stcb->block_entry->error = ECONNRESET;
5429 stcb->block_entry = NULL;
5430 }
5431 }
5432 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
5433 /* Someone holds a reference OR the socket is unaccepted yet.
5434 */
5435 if ((stcb->asoc.refcnt) ||
5436 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5437 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
5438 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
5439 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5440 }
5441 SCTP_TCB_UNLOCK(stcb);
5442 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5443 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5444 /* nothing around */
5445 so = NULL;
5446 if (so) {
5447 /* Wake any reader/writers */
5448 sctp_sorwakeup(inp, so);
5449 sctp_sowwakeup(inp, so);
5450 }
5451
5452#ifdef SCTP_LOG_CLOSING
5453 sctp_log_closing(inp, stcb, 9);
5454#endif
5455 /* no asoc destroyed */
5456 return (0);
5457 }
5458#ifdef SCTP_LOG_CLOSING
5459 sctp_log_closing(inp, stcb, 10);
5460#endif
5461 /* When I reach here, no others want
5462 * to kill the assoc yet.. and I own
5463 * the lock. Now its possible an abort
5464 * comes in when I do the lock exchange
5465 * below to grab all the locks to do
5466 * the final take out. to prevent this
5467 * we increment the count, which will
5468 * start a timer and blow out above thus
5469 * assuring us that we hold exclusive
5470 * killing of the asoc. Note that
5471 * after getting back the TCB lock
5472 * we will go ahead and increment the
5473 * counter back up and stop any timer
5474 * a passing stranger may have started :-S
5475 */
5476 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5477 atomic_add_int(&stcb->asoc.refcnt, 1);
5478
5479 SCTP_TCB_UNLOCK(stcb);
5480 SCTP_INP_INFO_WLOCK();
5481 SCTP_INP_WLOCK(inp);
5482 SCTP_TCB_LOCK(stcb);
5483 }
5484 /* Double check the GONE flag */
5485 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5486 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5487 /* nothing around */
5488 so = NULL;
5489
5490 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5491 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5492 /*
5493 * For TCP type we need special handling when we are
5494 * connected. We also include the peel'ed off ones to.
5495 */
5496 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5497 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5498 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5499 if (so) {
5500 SOCK_LOCK(so);
5501 if (so->so_rcv.sb_cc == 0) {
5502 so->so_state &= ~(SS_ISCONNECTING |
5503 SS_ISDISCONNECTING |
5504 SS_ISCONFIRMING |
5505 SS_ISCONNECTED);
5506 }
5507#if defined(__APPLE__)
5508 socantrcvmore(so);
5509#else
5510 socantrcvmore_locked(so);
5511#endif
5512 sctp_sowwakeup(inp, so);
5513 sctp_sorwakeup(inp, so);
5514 SCTP_SOWAKEUP(so);
5515 }
5516 }
5517 }
5518
5519 /* Make it invalid too, that way if its
5520 * about to run it will abort and return.
5521 */
5522 /* re-increment the lock */
5523 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5524 atomic_add_int(&stcb->asoc.refcnt, -1);
5525 }
5526 if (stcb->asoc.refcnt) {
5527 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
5528 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5529 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5530 SCTP_INP_INFO_WUNLOCK();
5531 SCTP_INP_WUNLOCK(inp);
5532 }
5533 SCTP_TCB_UNLOCK(stcb);
5534 return (0);
5535 }
5536 asoc->state = 0;
5537 if (inp->sctp_tcbhash) {
5538 LIST_REMOVE(stcb, sctp_tcbhash);
5539 }
5540 if (stcb->asoc.in_asocid_hash) {
5541 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5542 }
5543 /* Now lets remove it from the list of ALL associations in the EP */
5544 LIST_REMOVE(stcb, sctp_tcblist);
5545 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5546 SCTP_INP_INCR_REF(inp);
5547 SCTP_INP_WUNLOCK(inp);
5548 }
5549 /* pull from vtag hash */
5550 LIST_REMOVE(stcb, sctp_asocs);
5551 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
5552 inp->sctp_lport, stcb->rport);
5553
5554 /* Now restop the timers to be sure
5555 * this is paranoia at is finest!
5556 */
5557 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
5558 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
5559 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
5560 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
5561 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
5562 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
5563 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
5564 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5565 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
5566 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
5567 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
5568 }
5569
5570 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
5571 /*
5572 * The chunk lists and such SHOULD be empty but we check them just
5573 * in case.
5574 */
5575 /* anything on the wheel needs to be removed */
5576 for (i = 0; i < asoc->streamoutcnt; i++) {
5577 struct sctp_stream_out *outs;
5578
5579 outs = &asoc->strmout[i];
5580 /* now clean up any chunks here */
5581 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
5582 TAILQ_REMOVE(&outs->outqueue, sp, next);
tuexen30ca8172012-07-17 13:09:48 +00005583 sctp_free_spbufspace(stcb, asoc, sp);
tuexendd729232011-11-01 23:04:43 +00005584 if (sp->data) {
5585 if (so) {
5586 /* Still an open socket - report */
5587 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
tuexenda53ff02012-05-14 09:00:59 +00005588 0, (void *)sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005589 }
5590 if (sp->data) {
5591 sctp_m_freem(sp->data);
5592 sp->data = NULL;
5593 sp->tail_mbuf = NULL;
tuexen30ca8172012-07-17 13:09:48 +00005594 sp->length = 0;
tuexendd729232011-11-01 23:04:43 +00005595 }
5596 }
5597 if (sp->net) {
5598 sctp_free_remote_addr(sp->net);
5599 sp->net = NULL;
5600 }
tuexen30ca8172012-07-17 13:09:48 +00005601 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005602 }
5603 }
5604 /*sa_ignore FREED_MEMORY*/
5605 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5606 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5607 SCTP_FREE(strrst, SCTP_M_STRESET);
5608 }
5609 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5610 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5611 if (sq->data) {
5612 sctp_m_freem(sq->data);
5613 sq->data = NULL;
5614 }
5615 sctp_free_remote_addr(sq->whoFrom);
5616 sq->whoFrom = NULL;
5617 sq->stcb = NULL;
5618 /* Free the ctl entry */
5619 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
5620 SCTP_DECR_READQ_COUNT();
5621 /*sa_ignore FREED_MEMORY*/
5622 }
5623 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5624 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5625 if (chk->data) {
5626 sctp_m_freem(chk->data);
5627 chk->data = NULL;
5628 }
5629 if (chk->holds_key_ref)
5630 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5631 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5632 SCTP_DECR_CHK_COUNT();
5633 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5634 asoc->free_chunk_cnt--;
5635 /*sa_ignore FREED_MEMORY*/
5636 }
5637 /* pending send queue SHOULD be empty */
5638 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5639 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5640 if (chk->data) {
5641 if (so) {
5642 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005643 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5644 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005645 }
5646 if (chk->data) {
5647 sctp_m_freem(chk->data);
5648 chk->data = NULL;
5649 }
5650 }
5651 if (chk->holds_key_ref)
5652 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5653 if (chk->whoTo) {
5654 sctp_free_remote_addr(chk->whoTo);
5655 chk->whoTo = NULL;
5656 }
5657 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5658 SCTP_DECR_CHK_COUNT();
5659 /*sa_ignore FREED_MEMORY*/
5660 }
5661 /* sent queue SHOULD be empty */
5662 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
5663 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5664 if (chk->data) {
5665 if (so) {
5666 /* Still a socket? */
tuexenda53ff02012-05-14 09:00:59 +00005667 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5668 0, chk, SCTP_SO_LOCKED);
tuexendd729232011-11-01 23:04:43 +00005669 }
5670 if (chk->data) {
5671 sctp_m_freem(chk->data);
5672 chk->data = NULL;
5673 }
5674 }
5675 if (chk->holds_key_ref)
5676 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5677 sctp_free_remote_addr(chk->whoTo);
5678 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5679 SCTP_DECR_CHK_COUNT();
5680 /*sa_ignore FREED_MEMORY*/
5681 }
5682 /* control queue MAY not be empty */
5683 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5684 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5685 if (chk->data) {
5686 sctp_m_freem(chk->data);
5687 chk->data = NULL;
5688 }
5689 if (chk->holds_key_ref)
5690 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5691 sctp_free_remote_addr(chk->whoTo);
5692 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5693 SCTP_DECR_CHK_COUNT();
5694 /*sa_ignore FREED_MEMORY*/
5695 }
5696 /* ASCONF queue MAY not be empty */
5697 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5698 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5699 if (chk->data) {
5700 sctp_m_freem(chk->data);
5701 chk->data = NULL;
5702 }
5703 if (chk->holds_key_ref)
5704 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5705 sctp_free_remote_addr(chk->whoTo);
5706 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5707 SCTP_DECR_CHK_COUNT();
5708 /*sa_ignore FREED_MEMORY*/
5709 }
5710 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5711 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5712 if (chk->data) {
5713 sctp_m_freem(chk->data);
5714 chk->data = NULL;
5715 }
5716 if (chk->holds_key_ref)
5717 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5718 sctp_free_remote_addr(chk->whoTo);
5719 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5720 SCTP_DECR_CHK_COUNT();
5721 /*sa_ignore FREED_MEMORY*/
5722 }
5723
5724 if (asoc->mapping_array) {
5725 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5726 asoc->mapping_array = NULL;
5727 }
5728 if (asoc->nr_mapping_array) {
5729 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5730 asoc->nr_mapping_array = NULL;
5731 }
5732 /* the stream outs */
5733 if (asoc->strmout) {
5734 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5735 asoc->strmout = NULL;
5736 }
5737 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5738 if (asoc->strmin) {
5739 struct sctp_queued_to_read *ctl, *nctl;
5740
5741 for (i = 0; i < asoc->streamincnt; i++) {
5742 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
5743 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
5744 sctp_free_remote_addr(ctl->whoFrom);
5745 if (ctl->data) {
5746 sctp_m_freem(ctl->data);
5747 ctl->data = NULL;
5748 }
5749 /*
5750 * We don't free the address here
5751 * since all the net's were freed
5752 * above.
5753 */
5754 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
5755 SCTP_DECR_READQ_COUNT();
5756 }
5757 }
5758 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5759 asoc->strmin = NULL;
5760 }
5761 asoc->streamincnt = 0;
5762 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5763#ifdef INVARIANTS
5764 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5765 panic("no net's left alloc'ed, or list points to itself");
5766 }
5767#endif
5768 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5769 sctp_free_remote_addr(net);
5770 }
5771 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5772 /*sa_ignore FREED_MEMORY*/
5773 sctp_remove_laddr(laddr);
5774 }
5775
5776 /* pending asconf (address) parameters */
5777 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5778 /*sa_ignore FREED_MEMORY*/
5779 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5780 SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5781 }
5782 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5783 /*sa_ignore FREED_MEMORY*/
5784 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5785 if (aack->data != NULL) {
5786 sctp_m_freem(aack->data);
5787 }
5788 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5789 }
5790 /* clean up auth stuff */
5791 if (asoc->local_hmacs)
5792 sctp_free_hmaclist(asoc->local_hmacs);
5793 if (asoc->peer_hmacs)
5794 sctp_free_hmaclist(asoc->peer_hmacs);
5795
5796 if (asoc->local_auth_chunks)
5797 sctp_free_chunklist(asoc->local_auth_chunks);
5798 if (asoc->peer_auth_chunks)
5799 sctp_free_chunklist(asoc->peer_auth_chunks);
5800
5801 sctp_free_authinfo(&asoc->authinfo);
5802
5803 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5804 LIST_REMOVE(shared_key, next);
5805 sctp_free_sharedkey(shared_key);
5806 /*sa_ignore FREED_MEMORY*/
5807 }
5808
5809 /* Insert new items here :> */
5810
5811 /* Get rid of LOCK */
5812 SCTP_TCB_LOCK_DESTROY(stcb);
5813 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5814 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5815 SCTP_INP_INFO_WUNLOCK();
5816 SCTP_INP_RLOCK(inp);
5817 }
5818#if defined(__APPLE__) /* TEMP CODE */
5819 stcb->freed_from_where = from_location;
5820#endif
5821#ifdef SCTP_TRACK_FREED_ASOCS
5822 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5823 /* now clean up the tasoc itself */
5824 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5825 SCTP_DECR_ASOC_COUNT();
5826 } else {
5827 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5828 }
5829#else
5830 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5831 SCTP_DECR_ASOC_COUNT();
5832#endif
5833 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5834 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5835 /* If its NOT the inp_free calling us AND
5836 * sctp_close as been called, we
5837 * call back...
5838 */
5839 SCTP_INP_RUNLOCK(inp);
5840 /* This will start the kill timer (if we are
5841 * the last one) since we hold an increment yet. But
5842 * this is the only safe way to do this
5843 * since otherwise if the socket closes
5844 * at the same time we are here we might
5845 * collide in the cleanup.
5846 */
5847 sctp_inpcb_free(inp,
5848 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5849 SCTP_CALLED_DIRECTLY_NOCMPSET);
5850 SCTP_INP_DECR_REF(inp);
5851 goto out_of;
5852 } else {
5853 /* The socket is still open. */
5854 SCTP_INP_DECR_REF(inp);
5855 }
5856 }
5857 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5858 SCTP_INP_RUNLOCK(inp);
5859 }
5860 out_of:
5861 /* destroyed the asoc */
5862#ifdef SCTP_LOG_CLOSING
5863 sctp_log_closing(inp, NULL, 11);
5864#endif
5865 return (1);
5866}
5867
5868
5869
5870/*
5871 * determine if a destination is "reachable" based upon the addresses bound
5872 * to the current endpoint (e.g. only v4 or v6 currently bound)
5873 */
5874/*
5875 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5876 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5877 * types bound as its endpoint
5878 */
5879int
5880sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5881{
5882 struct sctp_inpcb *inp;
5883 int answer;
5884
5885 /*
5886 * No locks here, the TCB, in all cases is already locked and an
5887 * assoc is up. There is either a INP lock by the caller applied (in
5888 * asconf case when deleting an address) or NOT in the HB case,
5889 * however if HB then the INP increment is up and the INP will not
5890 * be removed (on top of the fact that we have a TCB lock). So we
5891 * only want to read the sctp_flags, which is either bound-all or
5892 * not.. no protection needed since once an assoc is up you can't be
5893 * changing your binding.
5894 */
5895 inp = stcb->sctp_ep;
5896 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5897 /* if bound all, destination is not restricted */
5898 /*
5899 * RRS: Question during lock work: Is this correct? If you
5900 * are bound-all you still might need to obey the V4--V6
5901 * flags??? IMO this bound-all stuff needs to be removed!
5902 */
5903 return (1);
5904 }
5905 /* NOTE: all "scope" checks are done when local addresses are added */
5906 switch (destaddr->sa_family) {
tuexen310f1bc2012-07-15 11:20:56 +00005907#ifdef INET6
tuexendd729232011-11-01 23:04:43 +00005908 case AF_INET6:
5909#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5910 answer = inp->inp_vflag & INP_IPV6;
5911#else
5912 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5913#endif
5914 break;
tuexen310f1bc2012-07-15 11:20:56 +00005915#endif
5916#ifdef INET
tuexendd729232011-11-01 23:04:43 +00005917 case AF_INET:
5918#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5919 answer = inp->inp_vflag & INP_IPV4;
5920#else
5921 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5922#endif
tuexen46570542012-07-14 20:37:24 +00005923 break;
tuexen310f1bc2012-07-15 11:20:56 +00005924#endif
tuexen5d9b9742012-07-11 21:52:01 +00005925#if defined(__Userspace__)
5926 case AF_CONN:
5927 answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
tuexendd729232011-11-01 23:04:43 +00005928 break;
tuexen5d9b9742012-07-11 21:52:01 +00005929#endif
tuexendd729232011-11-01 23:04:43 +00005930 default:
5931 /* invalid family, so it's unreachable */
5932 answer = 0;
5933 break;
5934 }
5935 return (answer);
5936}
5937
5938/*
5939 * update the inp_vflags on an endpoint
5940 */
5941static void
5942sctp_update_ep_vflag(struct sctp_inpcb *inp)
5943{
5944 struct sctp_laddr *laddr;
5945
5946 /* first clear the flag */
5947#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5948 inp->inp_vflag = 0;
5949#else
5950 inp->ip_inp.inp.inp_vflag = 0;
5951#endif
5952 /* set the flag based on addresses on the ep list */
5953 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5954 if (laddr->ifa == NULL) {
5955 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
5956 __FUNCTION__);
5957 continue;
5958 }
5959
5960 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5961 continue;
5962 }
5963 switch (laddr->ifa->address.sa.sa_family) {
5964#ifdef INET6
5965 case AF_INET6:
5966#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5967 inp->inp_vflag |= INP_IPV6;
5968#else
5969 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5970#endif
5971 break;
5972#endif
5973#ifdef INET
5974 case AF_INET:
5975#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
5976 inp->inp_vflag |= INP_IPV4;
5977#else
5978 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5979#endif
5980 break;
5981#endif
tuexen5d9b9742012-07-11 21:52:01 +00005982#if defined(__Userspace__)
5983 case AF_CONN:
5984 inp->ip_inp.inp.inp_vflag |= INP_CONN;
5985 break;
5986#endif
tuexendd729232011-11-01 23:04:43 +00005987 default:
5988 break;
5989 }
5990 }
5991}
5992
5993/*
5994 * Add the address to the endpoint local address list There is nothing to be
5995 * done if we are bound to all addresses
5996 */
5997void
5998sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5999{
6000 struct sctp_laddr *laddr;
tuexen63fc0bb2011-12-27 12:24:52 +00006001 int fnd, error = 0;
tuexendd729232011-11-01 23:04:43 +00006002
6003 fnd = 0;
6004
6005 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6006 /* You are already bound to all. You have it already */
6007 return;
6008 }
6009#ifdef INET6
6010 if (ifa->address.sa.sa_family == AF_INET6) {
6011 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6012 /* Can't bind a non-useable addr. */
6013 return;
6014 }
6015 }
6016#endif
6017 /* first, is it already present? */
6018 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6019 if (laddr->ifa == ifa) {
6020 fnd = 1;
6021 break;
6022 }
6023 }
6024
6025 if (fnd == 0) {
6026 /* Not in the ep list */
6027 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
6028 if (error != 0)
6029 return;
6030 inp->laddr_count++;
6031 /* update inp_vflag flags */
6032 switch (ifa->address.sa.sa_family) {
6033#ifdef INET6
6034 case AF_INET6:
6035#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6036 inp->inp_vflag |= INP_IPV6;
6037#else
6038 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6039#endif
6040 break;
6041#endif
tuexen310f1bc2012-07-15 11:20:56 +00006042#ifdef INET
tuexendd729232011-11-01 23:04:43 +00006043 case AF_INET:
6044#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
6045 inp->inp_vflag |= INP_IPV4;
6046#else
6047 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6048#endif
6049 break;
6050#endif
tuexen5d9b9742012-07-11 21:52:01 +00006051#if defined(__Userspace__)
6052 case AF_CONN:
6053 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6054 break;
6055#endif
tuexendd729232011-11-01 23:04:43 +00006056 default:
6057 break;
6058 }
6059 }
6060 return;
6061}
6062
6063
6064/*
6065 * select a new (hopefully reachable) destination net (should only be used
6066 * when we deleted an ep addr that is the only usable source address to reach
6067 * the destination net)
6068 */
6069static void
6070sctp_select_primary_destination(struct sctp_tcb *stcb)
6071{
6072 struct sctp_nets *net;
6073
6074 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6075 /* for now, we'll just pick the first reachable one we find */
6076 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6077 continue;
6078 if (sctp_destination_is_reachable(stcb,
6079 (struct sockaddr *)&net->ro._l_addr)) {
6080 /* found a reachable destination */
6081 stcb->asoc.primary_destination = net;
6082 }
6083 }
6084 /* I can't there from here! ...we're gonna die shortly... */
6085}
6086
6087
6088/*
6089 * Delete the address from the endpoint local address list There is nothing
6090 * to be done if we are bound to all addresses
6091 */
6092void
6093sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6094{
6095 struct sctp_laddr *laddr;
6096 int fnd;
6097
6098 fnd = 0;
6099 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6100 /* You are already bound to all. You have it already */
6101 return;
6102 }
6103 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6104 if (laddr->ifa == ifa) {
6105 fnd = 1;
6106 break;
6107 }
6108 }
6109 if (fnd && (inp->laddr_count < 2)) {
6110 /* can't delete unless there are at LEAST 2 addresses */
6111 return;
6112 }
6113 if (fnd) {
6114 /*
6115 * clean up any use of this address go through our
6116 * associations and clear any last_used_address that match
6117 * this one for each assoc, see if a new primary_destination
6118 * is needed
6119 */
6120 struct sctp_tcb *stcb;
6121
6122 /* clean up "next_addr_touse" */
6123 if (inp->next_addr_touse == laddr)
6124 /* delete this address */
6125 inp->next_addr_touse = NULL;
6126
6127 /* clean up "last_used_address" */
6128 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6129 struct sctp_nets *net;
6130 SCTP_TCB_LOCK(stcb);
6131 if (stcb->asoc.last_used_address == laddr)
6132 /* delete this address */
6133 stcb->asoc.last_used_address = NULL;
6134 /* Now spin through all the nets and purge any ref to laddr */
6135 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6136 if (net->ro._s_addr &&
6137 (net->ro._s_addr->ifa == laddr->ifa)) {
6138 /* Yep, purge src address selected */
6139 sctp_rtentry_t *rt;
6140
6141 /* delete this address if cached */
6142 rt = net->ro.ro_rt;
6143 if (rt != NULL) {
6144 RTFREE(rt);
6145 net->ro.ro_rt = NULL;
6146 }
6147 sctp_free_ifa(net->ro._s_addr);
6148 net->ro._s_addr = NULL;
6149 net->src_addr_selected = 0;
6150 }
6151 }
6152 SCTP_TCB_UNLOCK(stcb);
6153 } /* for each tcb */
6154 /* remove it from the ep list */
6155 sctp_remove_laddr(laddr);
6156 inp->laddr_count--;
6157 /* update inp_vflag flags */
6158 sctp_update_ep_vflag(inp);
6159 }
6160 return;
6161}
6162
6163/*
6164 * Add the address to the TCB local address restricted list.
6165 * This is a "pending" address list (eg. addresses waiting for an
6166 * ASCONF-ACK response) and cannot be used as a valid source address.
6167 */
6168void
6169sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6170{
tuexendd729232011-11-01 23:04:43 +00006171 struct sctp_laddr *laddr;
6172 struct sctpladdr *list;
6173
6174 /*
6175 * Assumes TCB is locked.. and possibly the INP. May need to
6176 * confirm/fix that if we need it and is not the case.
6177 */
6178 list = &stcb->asoc.sctp_restricted_addrs;
6179
tuexendd729232011-11-01 23:04:43 +00006180#ifdef INET6
6181 if (ifa->address.sa.sa_family == AF_INET6) {
6182 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6183 /* Can't bind a non-existent addr. */
6184 return;
6185 }
6186 }
6187#endif
6188 /* does the address already exist? */
6189 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6190 if (laddr->ifa == ifa) {
6191 return;
6192 }
6193 }
6194
6195 /* add to the list */
6196 (void)sctp_insert_laddr(list, ifa, 0);
6197 return;
6198}
6199
6200/*
6201 * insert an laddr entry with the given ifa for the desired list
6202 */
6203int
6204sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
6205{
6206 struct sctp_laddr *laddr;
6207
6208 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
6209 if (laddr == NULL) {
6210 /* out of memory? */
6211 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
6212 return (EINVAL);
6213 }
6214 SCTP_INCR_LADDR_COUNT();
6215 bzero(laddr, sizeof(*laddr));
6216 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
6217 laddr->ifa = ifa;
6218 laddr->action = act;
6219 atomic_add_int(&ifa->refcount, 1);
6220 /* insert it */
6221 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
6222
6223 return (0);
6224}
6225
6226/*
6227 * Remove an laddr entry from the local address list (on an assoc)
6228 */
6229void
6230sctp_remove_laddr(struct sctp_laddr *laddr)
6231{
6232
6233 /* remove from the list */
6234 LIST_REMOVE(laddr, sctp_nxt_addr);
6235 sctp_free_ifa(laddr->ifa);
6236 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
6237 SCTP_DECR_LADDR_COUNT();
6238}
6239
6240/*
6241 * Remove a local address from the TCB local address restricted list
6242 */
6243void
6244sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6245{
6246 struct sctp_inpcb *inp;
6247 struct sctp_laddr *laddr;
6248
6249 /*
6250 * This is called by asconf work. It is assumed that a) The TCB is
6251 * locked and b) The INP is locked. This is true in as much as I can
6252 * trace through the entry asconf code where I did these locks.
6253 * Again, the ASCONF code is a bit different in that it does lock
6254 * the INP during its work often times. This must be since we don't
6255 * want other proc's looking up things while what they are looking
6256 * up is changing :-D
6257 */
6258
6259 inp = stcb->sctp_ep;
6260 /* if subset bound and don't allow ASCONF's, can't delete last */
6261 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6262 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6263 if (stcb->sctp_ep->laddr_count < 2) {
6264 /* can't delete last address */
6265 return;
6266 }
6267 }
6268 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6269 /* remove the address if it exists */
6270 if (laddr->ifa == NULL)
6271 continue;
6272 if (laddr->ifa == ifa) {
6273 sctp_remove_laddr(laddr);
6274 return;
6275 }
6276 }
6277
6278 /* address not found! */
6279 return;
6280}
6281
6282#if defined(__FreeBSD__)
6283/*
6284 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
6285 */
6286/* sysctl */
6287static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6288static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
6289#endif /* FreeBSD || APPLE */
6290
6291
6292
6293#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
tuexen63fc0bb2011-12-27 12:24:52 +00006294struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6295int *sctp_cpuarry = NULL;
tuexendd729232011-11-01 23:04:43 +00006296void
6297sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6298{
6299 /* Queue a packet to a processor for the specified core */
6300 struct sctp_mcore_queue *qent;
6301 struct sctp_mcore_ctrl *wkq;
tuexen63fc0bb2011-12-27 12:24:52 +00006302 int need_wake = 0;
tuexendd729232011-11-01 23:04:43 +00006303 if (sctp_mcore_workers == NULL) {
6304 /* Something went way bad during setup */
6305 sctp_input_with_port(m, off, 0);
6306 return;
6307 }
6308 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6309 (sizeof(struct sctp_mcore_queue)),
6310 SCTP_M_MCORE);
6311 if (qent == NULL) {
6312 /* This is trouble */
6313 sctp_input_with_port(m, off, 0);
6314 return;
6315 }
6316#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6317 qent->vn = curvnet;
6318#endif
6319 qent->m = m;
6320 qent->off = off;
6321 qent->v6 = 0;
6322 wkq = &sctp_mcore_workers[cpu_to_use];
6323 SCTP_MCORE_QLOCK(wkq);
6324
6325 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6326 if (wkq->running == 0) {
6327 need_wake = 1;
6328 }
6329 SCTP_MCORE_QUNLOCK(wkq);
tuexen63fc0bb2011-12-27 12:24:52 +00006330 if (need_wake) {
tuexendd729232011-11-01 23:04:43 +00006331 wakeup(&wkq->running);
6332 }
6333}
6334
6335static void
6336sctp_mcore_thread(void *arg)
6337{
6338
6339 struct sctp_mcore_ctrl *wkq;
6340 struct sctp_mcore_queue *qent;
6341
6342 wkq = (struct sctp_mcore_ctrl *)arg;
6343 struct mbuf *m;
6344 int off, v6;
6345
6346 /* Wait for first tickle */
6347 SCTP_MCORE_LOCK(wkq);
6348 wkq->running = 0;
6349 msleep(&wkq->running,
6350 &wkq->core_mtx,
6351 0, "wait for pkt", 0);
6352 SCTP_MCORE_UNLOCK(wkq);
6353
6354 /* Bind to our cpu */
6355 thread_lock(curthread);
6356 sched_bind(curthread, wkq->cpuid);
6357 thread_unlock(curthread);
6358
6359 /* Now lets start working */
6360 SCTP_MCORE_LOCK(wkq);
6361 /* Now grab lock and go */
tuexen9784e9a2011-12-18 13:04:23 +00006362 for (;;) {
tuexendd729232011-11-01 23:04:43 +00006363 SCTP_MCORE_QLOCK(wkq);
6364 skip_sleep:
6365 wkq->running = 1;
6366 qent = TAILQ_FIRST(&wkq->que);
6367 if (qent) {
6368 TAILQ_REMOVE(&wkq->que, qent, next);
6369 SCTP_MCORE_QUNLOCK(wkq);
6370#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6371 CURVNET_SET(qent->vn);
6372#endif
6373 m = qent->m;
6374 off = qent->off;
6375 v6 = qent->v6;
6376 SCTP_FREE(qent, SCTP_M_MCORE);
6377 if (v6 == 0) {
6378 sctp_input_with_port(m, off, 0);
6379 } else {
tuexencb5fe8d2012-05-04 09:50:27 +00006380 SCTP_PRINTF("V6 not yet supported\n");
tuexendd729232011-11-01 23:04:43 +00006381 sctp_m_freem(m);
6382 }
6383#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
6384 CURVNET_RESTORE();
6385#endif
6386 SCTP_MCORE_QLOCK(wkq);
6387 }
6388 wkq->running = 0;
6389 if (!TAILQ_EMPTY(&wkq->que)) {
6390 goto skip_sleep;
6391 }
6392 SCTP_MCORE_QUNLOCK(wkq);
6393 msleep(&wkq->running,
6394 &wkq->core_mtx,
6395 0, "wait for pkt", 0);
tuexen63fc0bb2011-12-27 12:24:52 +00006396 }
tuexendd729232011-11-01 23:04:43 +00006397}
6398
6399static void
6400sctp_startup_mcore_threads(void)
6401{
6402 int i, cpu;
6403
6404 if (mp_ncpus == 1)
6405 return;
6406
6407 if (sctp_mcore_workers != NULL) {
6408 /* Already been here in some previous
6409 * vnet?
6410 */
6411 return;
6412 }
6413 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6414 ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6415 SCTP_M_MCORE);
6416 if (sctp_mcore_workers == NULL) {
6417 /* TSNH I hope */
6418 return;
6419 }
tuexen09631162012-04-18 11:23:02 +00006420 memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
tuexendd729232011-11-01 23:04:43 +00006421 sizeof(struct sctp_mcore_ctrl)));
6422 /* Init the structures */
tuexen63fc0bb2011-12-27 12:24:52 +00006423 for (i = 0; i<=mp_maxid; i++) {
tuexendd729232011-11-01 23:04:43 +00006424 TAILQ_INIT(&sctp_mcore_workers[i].que);
6425 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6426 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6427 sctp_mcore_workers[i].cpuid = i;
6428 }
6429 if (sctp_cpuarry == NULL) {
6430 SCTP_MALLOC(sctp_cpuarry, int *,
6431 (mp_ncpus * sizeof(int)),
6432 SCTP_M_MCORE);
tuexen63fc0bb2011-12-27 12:24:52 +00006433 i = 0;
tuexendd729232011-11-01 23:04:43 +00006434 CPU_FOREACH(cpu) {
6435 sctp_cpuarry[i] = cpu;
6436 i++;
6437 }
6438 }
6439
6440 /* Now start them all */
6441 CPU_FOREACH(cpu) {
6442#if __FreeBSD_version <= 701000
6443 (void)kthread_create(sctp_mcore_thread,
6444 (void *)&sctp_mcore_workers[cpu],
6445 &sctp_mcore_workers[cpu].thread_proc,
6446 RFPROC,
6447 SCTP_KTHREAD_PAGES,
6448 SCTP_MCORE_NAME);
6449
6450#else
6451 (void)kproc_create(sctp_mcore_thread,
6452 (void *)&sctp_mcore_workers[cpu],
6453 &sctp_mcore_workers[cpu].thread_proc,
6454 RFPROC,
6455 SCTP_KTHREAD_PAGES,
6456 SCTP_MCORE_NAME);
6457#endif
tuexen09631162012-04-18 11:23:02 +00006458
tuexendd729232011-11-01 23:04:43 +00006459 }
6460}
6461#endif
tuexen76eb2092012-04-01 16:35:28 +00006462#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000
tuexendd729232011-11-01 23:04:43 +00006463static struct mbuf *
6464sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6465{
6466 struct ip *ip;
6467 struct sctphdr *sh;
6468 int offset;
6469 uint32_t flowid, tag;
tuexen76eb2092012-04-01 16:35:28 +00006470
tuexendd729232011-11-01 23:04:43 +00006471 /*
6472 * No flow id built by lower layers fix it so we
6473 * create one.
6474 */
6475 ip = mtod(m, struct ip *);
tuexen664967a2012-06-28 16:24:36 +00006476 offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
tuexendd729232011-11-01 23:04:43 +00006477 if (SCTP_BUF_LEN(m) < offset) {
tuexen664967a2012-06-28 16:24:36 +00006478 if ((m = m_pullup(m, offset)) == NULL) {
tuexendd729232011-11-01 23:04:43 +00006479 SCTP_STAT_INCR(sctps_hdrops);
6480 return (NULL);
6481 }
6482 ip = mtod(m, struct ip *);
6483 }
6484 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
tuexendd729232011-11-01 23:04:43 +00006485 tag = htonl(sh->v_tag);
6486 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6487 m->m_pkthdr.flowid = flowid;
6488 m->m_flags |= M_FLOWID;
6489 return (m);
6490}
6491#endif
6492
6493void
6494sctp_pcb_init()
6495{
6496 /*
6497 * SCTP initialization for the PCB structures should be called by
6498 * the sctp_init() funciton.
6499 */
6500 int i;
6501 struct timeval tv;
tuexen6019b072011-12-15 18:38:03 +00006502
tuexendd729232011-11-01 23:04:43 +00006503 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6504 /* error I was called twice */
6505 return;
6506 }
6507 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6508
6509#if defined(SCTP_LOCAL_TRACE_BUF)
6510#if defined(__Windows__)
6511 if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
6512 bzero(SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
6513 }
6514#else
6515 bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
6516#endif
6517#endif
6518#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6519 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6520 ((mp_maxid+1) * sizeof(struct sctpstat)),
6521 SCTP_M_MCORE);
6522#endif
6523 (void)SCTP_GETTIME_TIMEVAL(&tv);
6524#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6525 bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid+1)));
6526 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6527 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6528#else
6529 bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
6530 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6531 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6532#endif
6533 /* init the empty list of (All) Endpoints */
6534 LIST_INIT(&SCTP_BASE_INFO(listhead));
6535#if defined(__APPLE__)
6536 LIST_INIT(&SCTP_BASE_INFO(inplisthead));
6537#endif
6538
6539
6540 /* init the hash table of endpoints */
6541#if defined(__FreeBSD__)
6542#if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
6543 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6544 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6545 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
6546#else
6547 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
6548 SCTP_BASE_SYSCTL(sctp_hashtblsize));
6549 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
6550 SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6551 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
6552 SCTP_BASE_SYSCTL(sctp_chunkscale));
6553#endif
6554#endif
6555 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6556 &SCTP_BASE_INFO(hashasocmark));
6557 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6558 &SCTP_BASE_INFO(hashmark));
6559 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6560 &SCTP_BASE_INFO(hashtcpmark));
6561 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
6562
6563
6564 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6565 &SCTP_BASE_INFO(hashvrfmark));
6566
6567 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6568 &SCTP_BASE_INFO(vrf_ifn_hashmark));
6569 /* init the zones */
6570 /*
6571 * FIX ME: Should check for NULL returns, but if it does fail we are
6572 * doomed to panic anyways... add later maybe.
6573 */
6574 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6575 sizeof(struct sctp_inpcb), maxsockets);
6576
6577 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6578 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6579
6580 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6581 sizeof(struct sctp_laddr),
6582 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6583
6584 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6585 sizeof(struct sctp_nets),
6586 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6587
6588 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6589 sizeof(struct sctp_tmit_chunk),
6590 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6591
6592 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6593 sizeof(struct sctp_queued_to_read),
6594 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6595
6596 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6597 sizeof(struct sctp_stream_queue_pending),
6598 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6599
6600 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6601 sizeof(struct sctp_asconf),
6602 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6603
6604 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6605 sizeof(struct sctp_asconf_ack),
6606 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6607
tuexen9784e9a2011-12-18 13:04:23 +00006608
tuexendd729232011-11-01 23:04:43 +00006609 /* Master Lock INIT for info structure */
6610#if defined(__APPLE__)
6611 /* allocate the lock group attribute for SCTP PCB mutexes */
6612 SCTP_BASE_INFO(mtx_grp_attr) = lck_grp_attr_alloc_init();
6613 lck_grp_attr_setdefault(SCTP_BASE_INFO(mtx_grp_attr));
6614 /* allocate the lock group for SCTP PCB mutexes */
6615 SCTP_BASE_INFO(mtx_grp) = lck_grp_alloc_init("sctppcb",
6616 SCTP_BASE_INFO(mtx_grp_attr));
6617 /* allocate the lock attribute for SCTP PCB mutexes */
6618 SCTP_BASE_INFO(mtx_attr) = lck_attr_alloc_init();
6619 lck_attr_setdefault(SCTP_BASE_INFO(mtx_attr));
tuexen9784e9a2011-12-18 13:04:23 +00006620#endif /* __APPLE__ */
tuexendd729232011-11-01 23:04:43 +00006621 SCTP_INP_INFO_LOCK_INIT();
6622 SCTP_STATLOG_INIT_LOCK();
6623
6624 SCTP_IPI_COUNT_INIT();
6625 SCTP_IPI_ADDR_INIT();
6626#ifdef SCTP_PACKET_LOGGING
6627 SCTP_IP_PKTLOG_INIT();
6628#endif
6629 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6630
6631 SCTP_WQ_ADDR_INIT();
6632 /* not sure if we need all the counts */
6633 SCTP_BASE_INFO(ipi_count_ep) = 0;
6634 /* assoc/tcb zone info */
6635 SCTP_BASE_INFO(ipi_count_asoc) = 0;
6636 /* local addrlist zone info */
6637 SCTP_BASE_INFO(ipi_count_laddr) = 0;
6638 /* remote addrlist zone info */
6639 SCTP_BASE_INFO(ipi_count_raddr) = 0;
6640 /* chunk info */
6641 SCTP_BASE_INFO(ipi_count_chunk) = 0;
6642
6643 /* socket queue zone info */
6644 SCTP_BASE_INFO(ipi_count_readq) = 0;
6645
6646 /* stream out queue cont */
6647 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6648
6649 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6650 SCTP_BASE_INFO(ipi_free_chunks) = 0;
6651
6652 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6653
6654 /* Init the TIMEWAIT list */
6655 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6656 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6657 }
6658
6659#if defined(SCTP_PROCESS_LEVEL_LOCKS)
tuexen5154a002011-12-23 18:47:55 +00006660#if defined(__Userspace_os_Windows)
tuexen6019b072011-12-15 18:38:03 +00006661 InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6662#else
tuexendd729232011-11-01 23:04:43 +00006663 (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6664#endif
tuexen6019b072011-12-15 18:38:03 +00006665#endif
tuexendd729232011-11-01 23:04:43 +00006666 sctp_startup_iterator();
6667
6668#if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6669 sctp_startup_mcore_threads();
6670#endif
6671
6672#ifndef __Panda__
6673 /*
6674 * INIT the default VRF which for BSD is the only one, other O/S's
6675 * may have more. But initially they must start with one and then
6676 * add the VRF's as addresses are added.
6677 */
6678 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
6679#endif
tuexen76eb2092012-04-01 16:35:28 +00006680#if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000
tuexendd729232011-11-01 23:04:43 +00006681 if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
tuexencb5fe8d2012-05-04 09:50:27 +00006682 SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
tuexendd729232011-11-01 23:04:43 +00006683 }
6684#endif
6685#if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6686 /* allocate the lock for the callout/timer queue */
6687 SCTP_TIMERQ_LOCK_INIT();
6688 TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6689#endif
6690#if defined(__Userspace__)
6691 mbuf_init(NULL);
6692 atomic_init();
tuexenc4178972012-07-15 20:40:54 +00006693#if defined(INET) || defined(INET6)
tuexen852c7962012-04-28 16:39:32 +00006694 recv_thread_init();
tuexendd729232011-11-01 23:04:43 +00006695#endif
tuexenc4178972012-07-15 20:40:54 +00006696#endif
tuexendd729232011-11-01 23:04:43 +00006697}
6698
6699/*
6700 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6701 */
6702void
6703sctp_pcb_finish(void)
6704{
6705 struct sctp_vrflist *vrf_bucket;
6706 struct sctp_vrf *vrf, *nvrf;
6707 struct sctp_ifn *ifn, *nifn;
6708 struct sctp_ifa *ifa, *nifa;
6709 struct sctpvtaghead *chain;
6710 struct sctp_tagblock *twait_block, *prev_twait_block;
6711 struct sctp_laddr *wi, *nwi;
6712 int i;
6713
6714#if defined(__FreeBSD__)
6715 /* Free BSD the it thread never exits
6716 * but we do clean up. The only way
6717 * freebsd reaches here if we have VRF's
6718 * but we still add the ifdef to make it
6719 * compile on old versions.
6720 */
6721 {
6722 struct sctp_iterator *it, *nit;
6723
6724 SCTP_IPI_ITERATOR_WQ_LOCK();
6725 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
6726 if (it->vn != curvnet) {
6727 continue;
tuexen09631162012-04-18 11:23:02 +00006728 }
tuexendd729232011-11-01 23:04:43 +00006729 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6730 if (it->function_atend != NULL) {
6731 (*it->function_atend) (it->pointer, it->val);
6732 }
6733 SCTP_FREE(it,SCTP_M_ITER);
6734 }
6735 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6736 SCTP_ITERATOR_LOCK();
6737 if ((sctp_it_ctl.cur_it) &&
6738 (sctp_it_ctl.cur_it->vn == curvnet)) {
6739 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6740 }
6741 SCTP_ITERATOR_UNLOCK();
6742 }
6743#else
6744 /* Notify the iterator to exit. */
6745 SCTP_IPI_ITERATOR_WQ_LOCK();
6746 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6747 sctp_wakeup_iterator();
6748 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6749#endif
6750#if defined(__APPLE__)
6751 SCTP_IPI_ITERATOR_WQ_LOCK();
6752 do {
6753 msleep(&sctp_it_ctl.iterator_flags,
6754 sctp_it_ctl.ipi_iterator_wq_mtx,
6755 0, "waiting_for_work", 0);
6756 } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6757 thread_deallocate(sctp_it_ctl.thread_proc);
6758 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6759 SCTP_IPI_ITERATOR_WQ_DESTROY();
6760 SCTP_ITERATOR_LOCK_DESTROY();
6761#endif
6762#if defined(__Windows__)
6763 if (sctp_it_ctl.iterator_thread_obj != NULL) {
6764 NTSTATUS status = STATUS_SUCCESS;
6765
6766 KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6767 status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6768 Executive,
6769 KernelMode,
6770 FALSE,
6771 NULL);
6772 ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6773 }
6774#endif
6775
6776 SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer));
6777 SCTP_WQ_ADDR_LOCK();
6778 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6779 LIST_REMOVE(wi, sctp_nxt_addr);
6780 SCTP_DECR_LADDR_COUNT();
6781 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6782 }
6783 SCTP_WQ_ADDR_UNLOCK();
tuexen09631162012-04-18 11:23:02 +00006784
tuexendd729232011-11-01 23:04:43 +00006785 /*
6786 * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6787 * is destroyed first).
6788 */
6789 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6790 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6791 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6792 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6793 /* free the ifa */
6794 LIST_REMOVE(ifa, next_bucket);
6795 LIST_REMOVE(ifa, next_ifa);
6796 SCTP_FREE(ifa, SCTP_M_IFA);
6797 }
6798 /* free the ifn */
6799 LIST_REMOVE(ifn, next_bucket);
6800 LIST_REMOVE(ifn, next_ifn);
6801 SCTP_FREE(ifn, SCTP_M_IFN);
6802 }
6803 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6804 /* free the vrf */
6805 LIST_REMOVE(vrf, next_vrf);
6806 SCTP_FREE(vrf, SCTP_M_VRF);
6807 }
6808 /* free the vrf hashes */
6809 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6810 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
tuexen9784e9a2011-12-18 13:04:23 +00006811#if defined(__Userspace__) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00006812 /* free memory allocated by getifaddrs call */
6813 freeifaddrs(g_interfaces);
6814#endif
6815
6816 /* free the TIMEWAIT list elements malloc'd in the function
6817 * sctp_add_vtag_to_timewait()...
6818 */
6819 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6820 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6821 if (!LIST_EMPTY(chain)) {
6822 prev_twait_block = NULL;
6823 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6824 if (prev_twait_block) {
6825 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6826 }
6827 prev_twait_block = twait_block;
6828 }
6829 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6830 }
6831 }
6832
6833 /* free the locks and mutexes */
6834#if defined(__APPLE__)
6835 SCTP_TIMERQ_LOCK_DESTROY();
6836#endif
6837#ifdef SCTP_PACKET_LOGGING
6838 SCTP_IP_PKTLOG_DESTROY();
6839#endif
6840 SCTP_IPI_ADDR_DESTROY();
6841#if defined(__APPLE__)
6842 SCTP_IPI_COUNT_DESTROY();
6843#endif
6844 SCTP_STATLOG_DESTROY();
6845#if !defined(__Userspace__)
6846 SCTP_INP_INFO_LOCK_DESTROY();
6847#endif
tuexen09631162012-04-18 11:23:02 +00006848
tuexendd729232011-11-01 23:04:43 +00006849 SCTP_WQ_ADDR_DESTROY();
tuexen09631162012-04-18 11:23:02 +00006850
tuexendd729232011-11-01 23:04:43 +00006851#if defined(__APPLE__)
6852 lck_grp_attr_free(SCTP_BASE_INFO(mtx_grp_attr));
6853 lck_grp_free(SCTP_BASE_INFO(mtx_grp));
6854 lck_attr_free(SCTP_BASE_INFO(mtx_attr));
6855#endif
6856#if defined(__Userspace__)
6857 SCTP_TIMERQ_LOCK_DESTROY();
6858 SCTP_ZONE_DESTROY(zone_mbuf);
6859 SCTP_ZONE_DESTROY(zone_clust);
6860 SCTP_ZONE_DESTROY(zone_ext_refcnt);
6861#endif
6862#if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__)
6863 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6864 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6865 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6866 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6867 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6868 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6869 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6870 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6871 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6872#endif
6873 /* Get rid of other stuff to */
6874 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6875 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6876 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6877 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6878 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6879 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6880#if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
tuexen09631162012-04-18 11:23:02 +00006881 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
tuexendd729232011-11-01 23:04:43 +00006882#endif
6883}
6884
6885
6886int
6887sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
tuexen664967a2012-06-28 16:24:36 +00006888 int offset, int limit,
6889 struct sockaddr *src, struct sockaddr *dst,
tuexen9784e9a2011-12-18 13:04:23 +00006890 struct sockaddr *altsa)
tuexendd729232011-11-01 23:04:43 +00006891{
6892 /*
6893 * grub through the INIT pulling addresses and loading them to the
6894 * nets structure in the asoc. The from address in the mbuf should
6895 * also be loaded (if it is not already). This routine can be called
6896 * with either INIT or INIT-ACK's as long as the m points to the IP
6897 * packet and the offset points to the beginning of the parameters.
6898 */
tuexen63fc0bb2011-12-27 12:24:52 +00006899 struct sctp_inpcb *inp;
tuexendd729232011-11-01 23:04:43 +00006900 struct sctp_nets *net, *nnet, *net_tmp;
tuexendd729232011-11-01 23:04:43 +00006901 struct sctp_paramhdr *phdr, parm_buf;
6902 struct sctp_tcb *stcb_tmp;
6903 uint16_t ptype, plen;
6904 struct sockaddr *sa;
tuexendd729232011-11-01 23:04:43 +00006905 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
6906 struct sctp_auth_random *p_random = NULL;
6907 uint16_t random_len = 0;
6908 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
6909 struct sctp_auth_hmac_algo *hmacs = NULL;
6910 uint16_t hmacs_len = 0;
tuexen63fc0bb2011-12-27 12:24:52 +00006911 uint8_t saw_asconf = 0;
6912 uint8_t saw_asconf_ack = 0;
tuexendd729232011-11-01 23:04:43 +00006913 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
6914 struct sctp_auth_chunk_list *chunks = NULL;
6915 uint16_t num_chunks = 0;
6916 sctp_key_t *new_key;
6917 uint32_t keylen;
6918 int got_random = 0, got_hmacs = 0, got_chklist = 0;
6919 uint8_t ecn_allowed;
6920#ifdef INET
6921 struct sockaddr_in sin;
6922#endif
6923#ifdef INET6
6924 struct sockaddr_in6 sin6;
6925#endif
6926
6927 /* First get the destination address setup too. */
6928#ifdef INET
6929 memset(&sin, 0, sizeof(sin));
6930 sin.sin_family = AF_INET;
tuexen6019b072011-12-15 18:38:03 +00006931#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00006932 sin.sin_len = sizeof(sin);
6933#endif
6934 sin.sin_port = stcb->rport;
6935#endif
6936#ifdef INET6
6937 memset(&sin6, 0, sizeof(sin6));
6938 sin6.sin6_family = AF_INET6;
tuexenda6f8ba2012-01-08 11:33:02 +00006939#if !defined(__Windows__) && !defined(__Userspace_os_Linux) && !defined(__Userspace_os_Windows)
tuexendd729232011-11-01 23:04:43 +00006940 sin6.sin6_len = sizeof(struct sockaddr_in6);
6941#endif
6942 sin6.sin6_port = stcb->rport;
6943#endif
tuexen664967a2012-06-28 16:24:36 +00006944 if (altsa) {
6945 sa = altsa;
6946 } else {
6947 sa = src;
tuexendd729232011-11-01 23:04:43 +00006948 }
6949 /* Turn off ECN until we get through all params */
6950 ecn_allowed = 0;
6951 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6952 /* mark all addresses that we have currently on the list */
6953 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
6954 }
6955 /* does the source address already exist? if so skip it */
tuexen63fc0bb2011-12-27 12:24:52 +00006956 inp = stcb->sctp_ep;
tuexendd729232011-11-01 23:04:43 +00006957 atomic_add_int(&stcb->asoc.refcnt, 1);
tuexen664967a2012-06-28 16:24:36 +00006958 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
tuexendd729232011-11-01 23:04:43 +00006959 atomic_add_int(&stcb->asoc.refcnt, -1);
6960
6961 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
6962 /* we must add the source address */
6963 /* no scope set here since we have a tcb already. */
6964 switch (sa->sa_family) {
6965#ifdef INET
6966 case AF_INET:
6967 if (stcb->asoc.ipv4_addr_legal) {
6968 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
6969 return (-1);
6970 }
6971 }
6972 break;
6973#endif
6974#ifdef INET6
6975 case AF_INET6:
6976 if (stcb->asoc.ipv6_addr_legal) {
6977 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6978 return (-2);
6979 }
6980 }
6981 break;
6982#endif
tuexen430ca6c2012-07-10 23:36:36 +00006983#if defined(__Userspace__)
6984 case AF_CONN:
6985 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6986 return (-2);
6987 }
6988 break;
6989#endif
tuexendd729232011-11-01 23:04:43 +00006990 default:
6991 break;
6992 }
6993 } else {
6994 if (net_tmp != NULL && stcb_tmp == stcb) {
6995 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
6996 } else if (stcb_tmp != stcb) {
6997 /* It belongs to another association? */
6998 if (stcb_tmp)
6999 SCTP_TCB_UNLOCK(stcb_tmp);
7000 return (-3);
7001 }
7002 }
7003 if (stcb->asoc.state == 0) {
7004 /* the assoc was freed? */
7005 return (-4);
7006 }
7007 /*
7008 * peer must explicitly turn this on. This may have been initialized
7009 * to be "on" in order to allow local addr changes while INIT's are
7010 * in flight.
7011 */
7012 stcb->asoc.peer_supports_asconf = 0;
7013 /* now we must go through each of the params. */
7014 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
7015 while (phdr) {
7016 ptype = ntohs(phdr->param_type);
7017 plen = ntohs(phdr->param_length);
7018 /*
tuexencb5fe8d2012-05-04 09:50:27 +00007019 * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
tuexendd729232011-11-01 23:04:43 +00007020 * (int)plen);
7021 */
7022 if (offset + plen > limit) {
7023 break;
7024 }
7025 if (plen == 0) {
7026 break;
7027 }
7028#ifdef INET
7029 if (ptype == SCTP_IPV4_ADDRESS) {
7030 if (stcb->asoc.ipv4_addr_legal) {
7031 struct sctp_ipv4addr_param *p4, p4_buf;
7032
7033 /* ok get the v4 address and check/add */
7034 phdr = sctp_get_next_param(m, offset,
7035 (struct sctp_paramhdr *)&p4_buf,
7036 sizeof(p4_buf));
7037 if (plen != sizeof(struct sctp_ipv4addr_param) ||
7038 phdr == NULL) {
7039 return (-5);
7040 }
7041 p4 = (struct sctp_ipv4addr_param *)phdr;
7042 sin.sin_addr.s_addr = p4->addr;
7043 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7044 /* Skip multi-cast addresses */
7045 goto next_param;
7046 }
7047 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
7048 (sin.sin_addr.s_addr == INADDR_ANY)) {
7049 goto next_param;
7050 }
7051 sa = (struct sockaddr *)&sin;
7052 inp = stcb->sctp_ep;
7053 atomic_add_int(&stcb->asoc.refcnt, 1);
7054 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007055 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007056 atomic_add_int(&stcb->asoc.refcnt, -1);
7057
7058 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7059 inp == NULL) {
7060 /* we must add the source address */
7061 /*
7062 * no scope set since we have a tcb
7063 * already
7064 */
7065
7066 /*
7067 * we must validate the state again
7068 * here
7069 */
7070 add_it_now:
7071 if (stcb->asoc.state == 0) {
7072 /* the assoc was freed? */
7073 return (-7);
7074 }
7075 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
7076 return (-8);
7077 }
7078 } else if (stcb_tmp == stcb) {
7079 if (stcb->asoc.state == 0) {
7080 /* the assoc was freed? */
7081 return (-10);
7082 }
7083 if (net != NULL) {
7084 /* clear flag */
7085 net->dest_state &=
7086 ~SCTP_ADDR_NOT_IN_ASSOC;
7087 }
7088 } else {
7089 /*
7090 * strange, address is in another
7091 * assoc? straighten out locks.
7092 */
7093 if (stcb_tmp) {
7094 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
7095 /* in setup state we abort this guy */
7096 sctp_abort_an_association(stcb_tmp->sctp_ep,
tuexenda53ff02012-05-14 09:00:59 +00007097 stcb_tmp, NULL, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007098 goto add_it_now;
7099 }
7100 SCTP_TCB_UNLOCK(stcb_tmp);
7101 }
7102
7103 if (stcb->asoc.state == 0) {
7104 /* the assoc was freed? */
7105 return (-12);
7106 }
7107 return (-13);
7108 }
7109 }
7110 } else
7111#endif
7112#ifdef INET6
7113 if (ptype == SCTP_IPV6_ADDRESS) {
7114 if (stcb->asoc.ipv6_addr_legal) {
7115 /* ok get the v6 address and check/add */
7116 struct sctp_ipv6addr_param *p6, p6_buf;
7117
7118 phdr = sctp_get_next_param(m, offset,
7119 (struct sctp_paramhdr *)&p6_buf,
7120 sizeof(p6_buf));
7121 if (plen != sizeof(struct sctp_ipv6addr_param) ||
7122 phdr == NULL) {
7123 return (-14);
7124 }
7125 p6 = (struct sctp_ipv6addr_param *)phdr;
7126 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7127 sizeof(p6->addr));
7128 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7129 /* Skip multi-cast addresses */
7130 goto next_param;
7131 }
7132 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7133 /* Link local make no sense without scope */
7134 goto next_param;
7135 }
7136 sa = (struct sockaddr *)&sin6;
7137 inp = stcb->sctp_ep;
7138 atomic_add_int(&stcb->asoc.refcnt, 1);
7139 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
tuexen664967a2012-06-28 16:24:36 +00007140 dst, stcb);
tuexendd729232011-11-01 23:04:43 +00007141 atomic_add_int(&stcb->asoc.refcnt, -1);
7142 if (stcb_tmp == NULL &&
7143 (inp == stcb->sctp_ep || inp == NULL)) {
7144 /*
7145 * we must validate the state again
7146 * here
7147 */
7148 add_it_now6:
7149 if (stcb->asoc.state == 0) {
7150 /* the assoc was freed? */
7151 return (-16);
7152 }
7153 /*
7154 * we must add the address, no scope
7155 * set
7156 */
7157 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
7158 return (-17);
7159 }
7160 } else if (stcb_tmp == stcb) {
7161 /*
7162 * we must validate the state again
7163 * here
7164 */
7165 if (stcb->asoc.state == 0) {
7166 /* the assoc was freed? */
7167 return (-19);
7168 }
7169 if (net != NULL) {
7170 /* clear flag */
7171 net->dest_state &=
7172 ~SCTP_ADDR_NOT_IN_ASSOC;
7173 }
7174 } else {
7175 /*
7176 * strange, address is in another
7177 * assoc? straighten out locks.
7178 */
7179 if (stcb_tmp)
7180 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
7181 /* in setup state we abort this guy */
7182 sctp_abort_an_association(stcb_tmp->sctp_ep,
tuexenda53ff02012-05-14 09:00:59 +00007183 stcb_tmp, NULL, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007184 goto add_it_now6;
7185 }
7186 SCTP_TCB_UNLOCK(stcb_tmp);
7187
7188 if (stcb->asoc.state == 0) {
7189 /* the assoc was freed? */
7190 return (-21);
7191 }
7192 return (-22);
7193 }
7194 }
7195 } else
7196#endif
7197 if (ptype == SCTP_ECN_CAPABLE) {
7198 ecn_allowed = 1;
7199 } else if (ptype == SCTP_ULP_ADAPTATION) {
7200 if (stcb->asoc.state != SCTP_STATE_OPEN) {
7201 struct sctp_adaptation_layer_indication ai, *aip;
7202
7203 phdr = sctp_get_next_param(m, offset,
7204 (struct sctp_paramhdr *)&ai, sizeof(ai));
7205 aip = (struct sctp_adaptation_layer_indication *)phdr;
7206 if (aip) {
7207 stcb->asoc.peers_adaptation = ntohl(aip->indication);
7208 stcb->asoc.adaptation_needed = 1;
7209 }
7210 }
7211 } else if (ptype == SCTP_SET_PRIM_ADDR) {
7212 struct sctp_asconf_addr_param lstore, *fee;
7213 int lptype;
7214 struct sockaddr *lsa = NULL;
7215#ifdef INET
7216 struct sctp_asconf_addrv4_param *fii;
7217#endif
7218
7219 stcb->asoc.peer_supports_asconf = 1;
7220 if (plen > sizeof(lstore)) {
7221 return (-23);
7222 }
7223 phdr = sctp_get_next_param(m, offset,
7224 (struct sctp_paramhdr *)&lstore,
7225 min(plen,sizeof(lstore)));
7226 if (phdr == NULL) {
7227 return (-24);
7228 }
7229 fee = (struct sctp_asconf_addr_param *)phdr;
7230 lptype = ntohs(fee->addrp.ph.param_type);
7231 switch (lptype) {
7232#ifdef INET
7233 case SCTP_IPV4_ADDRESS:
7234 if (plen !=
7235 sizeof(struct sctp_asconf_addrv4_param)) {
7236 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7237 (int)sizeof(struct sctp_asconf_addrv4_param),
7238 plen);
7239 } else {
7240 fii = (struct sctp_asconf_addrv4_param *)fee;
7241 sin.sin_addr.s_addr = fii->addrp.addr;
7242 lsa = (struct sockaddr *)&sin;
7243 }
7244 break;
7245#endif
7246#ifdef INET6
7247 case SCTP_IPV6_ADDRESS:
7248 if (plen !=
7249 sizeof(struct sctp_asconf_addr_param)) {
7250 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7251 (int)sizeof(struct sctp_asconf_addr_param),
7252 plen);
7253 } else {
7254 memcpy(sin6.sin6_addr.s6_addr,
7255 fee->addrp.addr,
7256 sizeof(fee->addrp.addr));
7257 lsa = (struct sockaddr *)&sin6;
7258 }
7259 break;
7260#endif
7261 default:
7262 break;
7263 }
7264 if (lsa) {
7265 (void)sctp_set_primary_addr(stcb, sa, NULL);
7266 }
7267 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7268 stcb->asoc.peer_supports_nat = 1;
7269 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7270 /* Peer supports pr-sctp */
7271 stcb->asoc.peer_supports_prsctp = 1;
7272 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7273 /* A supported extension chunk */
7274 struct sctp_supported_chunk_types_param *pr_supported;
7275 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7276 int num_ent, i;
7277
7278 phdr = sctp_get_next_param(m, offset,
7279 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store),plen));
7280 if (phdr == NULL) {
7281 return (-25);
7282 }
7283 stcb->asoc.peer_supports_asconf = 0;
7284 stcb->asoc.peer_supports_prsctp = 0;
7285 stcb->asoc.peer_supports_pktdrop = 0;
7286 stcb->asoc.peer_supports_strreset = 0;
7287 stcb->asoc.peer_supports_nr_sack = 0;
7288 stcb->asoc.peer_supports_auth = 0;
7289 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7290 num_ent = plen - sizeof(struct sctp_paramhdr);
7291 for (i = 0; i < num_ent; i++) {
7292 switch (pr_supported->chunk_types[i]) {
7293 case SCTP_ASCONF:
7294 case SCTP_ASCONF_ACK:
7295 stcb->asoc.peer_supports_asconf = 1;
7296 break;
7297 case SCTP_FORWARD_CUM_TSN:
7298 stcb->asoc.peer_supports_prsctp = 1;
7299 break;
7300 case SCTP_PACKET_DROPPED:
7301 stcb->asoc.peer_supports_pktdrop = 1;
7302 break;
7303 case SCTP_NR_SELECTIVE_ACK:
7304 stcb->asoc.peer_supports_nr_sack = 1;
7305 break;
7306 case SCTP_STREAM_RESET:
7307 stcb->asoc.peer_supports_strreset = 1;
7308 break;
7309 case SCTP_AUTHENTICATION:
7310 stcb->asoc.peer_supports_auth = 1;
7311 break;
7312 default:
7313 /* one I have not learned yet */
7314 break;
7315
7316 }
7317 }
7318 } else if (ptype == SCTP_RANDOM) {
7319 if (plen > sizeof(random_store))
7320 break;
7321 if (got_random) {
7322 /* already processed a RANDOM */
7323 goto next_param;
7324 }
7325 phdr = sctp_get_next_param(m, offset,
7326 (struct sctp_paramhdr *)random_store,
7327 min(sizeof(random_store),plen));
7328 if (phdr == NULL)
7329 return (-26);
7330 p_random = (struct sctp_auth_random *)phdr;
7331 random_len = plen - sizeof(*p_random);
7332 /* enforce the random length */
7333 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7334 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7335 return (-27);
7336 }
7337 got_random = 1;
7338 } else if (ptype == SCTP_HMAC_LIST) {
7339 int num_hmacs;
7340 int i;
7341
7342 if (plen > sizeof(hmacs_store))
7343 break;
7344 if (got_hmacs) {
7345 /* already processed a HMAC list */
7346 goto next_param;
7347 }
7348 phdr = sctp_get_next_param(m, offset,
7349 (struct sctp_paramhdr *)hmacs_store,
7350 min(plen,sizeof(hmacs_store)));
7351 if (phdr == NULL)
7352 return (-28);
7353 hmacs = (struct sctp_auth_hmac_algo *)phdr;
7354 hmacs_len = plen - sizeof(*hmacs);
7355 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7356 /* validate the hmac list */
7357 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7358 return (-29);
7359 }
7360 if (stcb->asoc.peer_hmacs != NULL)
7361 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7362 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7363 if (stcb->asoc.peer_hmacs != NULL) {
7364 for (i = 0; i < num_hmacs; i++) {
7365 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7366 ntohs(hmacs->hmac_ids[i]));
7367 }
7368 }
7369 got_hmacs = 1;
7370 } else if (ptype == SCTP_CHUNK_LIST) {
7371 int i;
7372
7373 if (plen > sizeof(chunks_store))
7374 break;
7375 if (got_chklist) {
7376 /* already processed a Chunks list */
7377 goto next_param;
7378 }
7379 phdr = sctp_get_next_param(m, offset,
7380 (struct sctp_paramhdr *)chunks_store,
7381 min(plen,sizeof(chunks_store)));
7382 if (phdr == NULL)
7383 return (-30);
7384 chunks = (struct sctp_auth_chunk_list *)phdr;
7385 num_chunks = plen - sizeof(*chunks);
7386 if (stcb->asoc.peer_auth_chunks != NULL)
7387 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7388 else
7389 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7390 for (i = 0; i < num_chunks; i++) {
7391 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7392 stcb->asoc.peer_auth_chunks);
7393 /* record asconf/asconf-ack if listed */
7394 if (chunks->chunk_types[i] == SCTP_ASCONF)
7395 saw_asconf = 1;
7396 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7397 saw_asconf_ack = 1;
7398
7399 }
7400 got_chklist = 1;
7401 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7402 (ptype == SCTP_STATE_COOKIE) ||
7403 (ptype == SCTP_UNRECOG_PARAM) ||
7404 (ptype == SCTP_COOKIE_PRESERVE) ||
7405 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7406 (ptype == SCTP_ADD_IP_ADDRESS) ||
7407 (ptype == SCTP_DEL_IP_ADDRESS) ||
7408 (ptype == SCTP_ERROR_CAUSE_IND) ||
7409 (ptype == SCTP_SUCCESS_REPORT)) {
7410 /* don't care */ ;
7411 } else {
7412 if ((ptype & 0x8000) == 0x0000) {
7413 /*
7414 * must stop processing the rest of the
7415 * param's. Any report bits were handled
7416 * with the call to
7417 * sctp_arethere_unrecognized_parameters()
7418 * when the INIT or INIT-ACK was first seen.
7419 */
7420 break;
7421 }
7422 }
tuexen09631162012-04-18 11:23:02 +00007423
tuexendd729232011-11-01 23:04:43 +00007424 next_param:
7425 offset += SCTP_SIZE32(plen);
7426 if (offset >= limit) {
7427 break;
7428 }
7429 phdr = sctp_get_next_param(m, offset, &parm_buf,
7430 sizeof(parm_buf));
7431 }
7432 /* Now check to see if we need to purge any addresses */
7433 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7434 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7435 SCTP_ADDR_NOT_IN_ASSOC) {
7436 /* This address has been removed from the asoc */
7437 /* remove and free it */
7438 stcb->asoc.numnets--;
7439 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
7440 sctp_free_remote_addr(net);
7441 if (net == stcb->asoc.primary_destination) {
7442 stcb->asoc.primary_destination = NULL;
7443 sctp_select_primary_destination(stcb);
7444 }
7445 }
7446 }
7447 if (ecn_allowed == 0) {
7448 stcb->asoc.ecn_allowed = 0;
7449 }
7450 /* validate authentication required parameters */
7451 if (got_random && got_hmacs) {
7452 stcb->asoc.peer_supports_auth = 1;
7453 } else {
7454 stcb->asoc.peer_supports_auth = 0;
7455 }
7456 if (!stcb->asoc.peer_supports_auth && got_chklist) {
7457 /* peer does not support auth but sent a chunks list? */
7458 return (-31);
7459 }
7460 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
7461 !stcb->asoc.peer_supports_auth) {
7462 /* peer supports asconf but not auth? */
7463 return (-32);
7464 } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
7465 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7466 return (-33);
7467 }
7468 /* concatenate the full random key */
7469 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7470 if (chunks != NULL) {
7471 keylen += sizeof(*chunks) + num_chunks;
7472 }
7473 new_key = sctp_alloc_key(keylen);
7474 if (new_key != NULL) {
7475 /* copy in the RANDOM */
7476 if (p_random != NULL) {
7477 keylen = sizeof(*p_random) + random_len;
7478 bcopy(p_random, new_key->key, keylen);
7479 }
7480 /* append in the AUTH chunks */
7481 if (chunks != NULL) {
7482 bcopy(chunks, new_key->key + keylen,
7483 sizeof(*chunks) + num_chunks);
7484 keylen += sizeof(*chunks) + num_chunks;
7485 }
7486 /* append in the HMACs */
7487 if (hmacs != NULL) {
7488 bcopy(hmacs, new_key->key + keylen,
7489 sizeof(*hmacs) + hmacs_len);
7490 }
7491 } else {
7492 /* failed to get memory for the key */
7493 return (-34);
7494 }
7495 if (stcb->asoc.authinfo.peer_random != NULL)
7496 sctp_free_key(stcb->asoc.authinfo.peer_random);
7497 stcb->asoc.authinfo.peer_random = new_key;
7498 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7499 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7500
7501 return (0);
7502}
7503
7504int
7505sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7506 struct sctp_nets *net)
7507{
7508 /* make sure the requested primary address exists in the assoc */
7509 if (net == NULL && sa)
7510 net = sctp_findnet(stcb, sa);
7511
7512 if (net == NULL) {
7513 /* didn't find the requested primary address! */
7514 return (-1);
7515 } else {
7516 /* set the primary address */
7517 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7518 /* Must be confirmed, so queue to set */
7519 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7520 return (0);
7521 }
7522 stcb->asoc.primary_destination = net;
7523 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
7524 sctp_free_remote_addr(stcb->asoc.alternate);
7525 stcb->asoc.alternate = NULL;
7526 }
7527 net = TAILQ_FIRST(&stcb->asoc.nets);
7528 if (net != stcb->asoc.primary_destination) {
7529 /* first one on the list is NOT the primary
7530 * sctp_cmpaddr() is much more efficient if
7531 * the primary is the first on the list, make it
7532 * so.
7533 */
7534 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7535 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7536 }
7537 return (0);
7538 }
7539}
7540
7541int
tuexen9784e9a2011-12-18 13:04:23 +00007542sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
tuexendd729232011-11-01 23:04:43 +00007543{
7544 /*
7545 * This function serves two purposes. It will see if a TAG can be
7546 * re-used and return 1 for yes it is ok and 0 for don't use that
7547 * tag. A secondary function it will do is purge out old tags that
7548 * can be removed.
7549 */
7550 struct sctpvtaghead *chain;
7551 struct sctp_tagblock *twait_block;
7552 struct sctpasochead *head;
7553 struct sctp_tcb *stcb;
7554 int i;
7555
7556 SCTP_INP_INFO_RLOCK();
7557 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
7558 SCTP_BASE_INFO(hashasocmark))];
7559 if (head == NULL) {
7560 /* invalid vtag */
7561 goto skip_vtag_check;
7562 }
7563 LIST_FOREACH(stcb, head, sctp_asocs) {
7564 /* We choose not to lock anything here. TCB's can't be
7565 * removed since we have the read lock, so they can't
7566 * be freed on us, same thing for the INP. I may
7567 * be wrong with this assumption, but we will go
7568 * with it for now :-)
7569 */
7570 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7571 continue;
7572 }
7573 if (stcb->asoc.my_vtag == tag) {
7574 /* candidate */
7575 if (stcb->rport != rport) {
7576 continue;
7577 }
7578 if (stcb->sctp_ep->sctp_lport != lport) {
7579 continue;
7580 }
7581 /* Its a used tag set */
7582 SCTP_INP_INFO_RUNLOCK();
7583 return (0);
7584 }
7585 }
7586skip_vtag_check:
7587
7588 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
7589 /* Now what about timed wait ? */
7590 if (!LIST_EMPTY(chain)) {
7591 /*
7592 * Block(s) are present, lets see if we have this tag in the
7593 * list
7594 */
7595 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
7596 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
7597 if (twait_block->vtag_block[i].v_tag == 0) {
7598 /* not used */
7599 continue;
7600 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
7601 now->tv_sec) {
7602 /* Audit expires this guy */
7603 twait_block->vtag_block[i].tv_sec_at_expire = 0;
7604 twait_block->vtag_block[i].v_tag = 0;
7605 twait_block->vtag_block[i].lport = 0;
7606 twait_block->vtag_block[i].rport = 0;
7607 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
7608 (twait_block->vtag_block[i].lport == lport) &&
7609 (twait_block->vtag_block[i].rport == rport)) {
7610 /* Bad tag, sorry :< */
7611 SCTP_INP_INFO_RUNLOCK();
7612 return (0);
7613 }
7614 }
7615 }
7616 }
7617 SCTP_INP_INFO_RUNLOCK();
7618 return (1);
7619}
7620
tuexendd729232011-11-01 23:04:43 +00007621static void
tuexen9784e9a2011-12-18 13:04:23 +00007622sctp_drain_mbufs(struct sctp_tcb *stcb)
tuexendd729232011-11-01 23:04:43 +00007623{
7624 /*
7625 * We must hunt this association for MBUF's past the cumack (i.e.
7626 * out of order data that we can renege on).
7627 */
7628 struct sctp_association *asoc;
7629 struct sctp_tmit_chunk *chk, *nchk;
7630 uint32_t cumulative_tsn_p1;
7631 struct sctp_queued_to_read *ctl, *nctl;
7632 int cnt, strmat;
7633 uint32_t gap, i;
tuexen63fc0bb2011-12-27 12:24:52 +00007634 int fnd = 0;
tuexendd729232011-11-01 23:04:43 +00007635
7636 /* We look for anything larger than the cum-ack + 1 */
7637
7638 asoc = &stcb->asoc;
7639 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7640 /* none we can reneg on. */
7641 return;
7642 }
7643 SCTP_STAT_INCR(sctps_protocol_drains_done);
7644 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7645 cnt = 0;
7646 /* First look in the re-assembly queue */
7647 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
7648 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) {
7649 /* Yep it is above cum-ack */
7650 cnt++;
7651 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn);
7652 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
7653 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7654 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7655 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
7656 if (chk->data) {
7657 sctp_m_freem(chk->data);
7658 chk->data = NULL;
7659 }
7660 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7661 }
7662 }
7663 /* Ok that was fun, now we will drain all the inbound streams? */
7664 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
7665 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) {
7666 if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) {
7667 /* Yep it is above cum-ack */
7668 cnt++;
7669 SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn);
7670 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
7671 sctp_ucount_decr(asoc->cnt_on_all_streams);
7672 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7673 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next);
7674 if (ctl->data) {
7675 sctp_m_freem(ctl->data);
7676 ctl->data = NULL;
7677 }
7678 sctp_free_remote_addr(ctl->whoFrom);
7679 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
7680 SCTP_DECR_READQ_COUNT();
7681 }
7682 }
7683 }
7684 if (cnt) {
7685 /* We must back down to see what the new highest is */
7686 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
7687 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
7688 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
7689 asoc->highest_tsn_inside_map = i;
7690 fnd = 1;
7691 break;
7692 }
7693 }
7694 if (!fnd) {
7695 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
7696 }
7697
7698 /*
7699 * Question, should we go through the delivery queue? The only
7700 * reason things are on here is the app not reading OR a p-d-api up.
7701 * An attacker COULD send enough in to initiate the PD-API and then
7702 * send a bunch of stuff to other streams... these would wind up on
7703 * the delivery queue.. and then we would not get to them. But in
7704 * order to do this I then have to back-track and un-deliver
7705 * sequence numbers in streams.. el-yucko. I think for now we will
7706 * NOT look at the delivery queue and leave it to be something to
7707 * consider later. An alternative would be to abort the P-D-API with
7708 * a notification and then deliver the data.... Or another method
7709 * might be to keep track of how many times the situation occurs and
7710 * if we see a possible attack underway just abort the association.
7711 */
7712#ifdef SCTP_DEBUG
7713 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
7714#endif
7715 /*
7716 * Now do we need to find a new
7717 * asoc->highest_tsn_inside_map?
7718 */
7719 asoc->last_revoke_count = cnt;
7720 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
7721 /*sa_ignore NO_NULL_CHK*/
7722 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7723 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
tuexendd729232011-11-01 23:04:43 +00007724 }
7725 /*
7726 * Another issue, in un-setting the TSN's in the mapping array we
7727 * DID NOT adjust the highest_tsn marker. This will cause one of two
7728 * things to occur. It may cause us to do extra work in checking for
7729 * our mapping array movement. More importantly it may cause us to
7730 * SACK every datagram. This may not be a bad thing though since we
7731 * will recover once we get our cum-ack above and all this stuff we
7732 * dumped recovered.
7733 */
7734}
7735
7736void
7737sctp_drain()
7738{
7739 /*
7740 * We must walk the PCB lists for ALL associations here. The system
7741 * is LOW on MBUF's and needs help. This is where reneging will
7742 * occur. We really hope this does NOT happen!
7743 */
7744#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7745 VNET_ITERATOR_DECL(vnet_iter);
7746#else
7747 struct sctp_inpcb *inp;
7748 struct sctp_tcb *stcb;
7749
7750 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7751 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7752 return;
7753 }
7754#endif
7755#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7756 VNET_LIST_RLOCK_NOSLEEP();
7757 VNET_FOREACH(vnet_iter) {
7758 CURVNET_SET(vnet_iter);
7759 struct sctp_inpcb *inp;
7760 struct sctp_tcb *stcb;
7761#endif
7762
7763#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7764 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7765 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7766#ifdef VIMAGE
7767 continue;
7768#else
7769 return;
tuexen09631162012-04-18 11:23:02 +00007770#endif
tuexendd729232011-11-01 23:04:43 +00007771 }
7772#endif
7773 SCTP_INP_INFO_RLOCK();
7774 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7775 /* For each endpoint */
7776 SCTP_INP_RLOCK(inp);
7777 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7778 /* For each association */
7779 SCTP_TCB_LOCK(stcb);
tuexen9784e9a2011-12-18 13:04:23 +00007780 sctp_drain_mbufs(stcb);
tuexendd729232011-11-01 23:04:43 +00007781 SCTP_TCB_UNLOCK(stcb);
7782 }
7783 SCTP_INP_RUNLOCK(inp);
7784 }
7785 SCTP_INP_INFO_RUNLOCK();
7786#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7787 CURVNET_RESTORE();
7788 }
7789 VNET_LIST_RUNLOCK_NOSLEEP();
7790#endif
7791}
7792
7793/*
7794 * start a new iterator
7795 * iterates through all endpoints and associations based on the pcb_state
7796 * flags and asoc_state. "af" (mandatory) is executed for all matching
7797 * assocs and "ef" (optional) is executed when the iterator completes.
7798 * "inpf" (optional) is executed for each new endpoint as it is being
7799 * iterated through. inpe (optional) is called when the inp completes
7800 * its way through all the stcbs.
7801 */
7802int
7803sctp_initiate_iterator(inp_func inpf,
7804 asoc_func af,
7805 inp_func inpe,
7806 uint32_t pcb_state,
7807 uint32_t pcb_features,
7808 uint32_t asoc_state,
7809 void *argp,
7810 uint32_t argi,
7811 end_func ef,
7812 struct sctp_inpcb *s_inp,
7813 uint8_t chunk_output_off)
7814{
7815 struct sctp_iterator *it = NULL;
7816
7817 if (af == NULL) {
7818 return (-1);
7819 }
7820 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
7821 SCTP_M_ITER);
7822 if (it == NULL) {
7823 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
7824 return (ENOMEM);
7825 }
7826 memset(it, 0, sizeof(*it));
7827 it->function_assoc = af;
7828 it->function_inp = inpf;
7829 if (inpf)
7830 it->done_current_ep = 0;
7831 else
7832 it->done_current_ep = 1;
7833 it->function_atend = ef;
7834 it->pointer = argp;
7835 it->val = argi;
7836 it->pcb_flags = pcb_state;
7837 it->pcb_features = pcb_features;
7838 it->asoc_state = asoc_state;
7839 it->function_inp_end = inpe;
7840 it->no_chunk_output = chunk_output_off;
7841#if defined(__FreeBSD__) && __FreeBSD_version >= 801000
7842 it->vn = curvnet;
tuexen09631162012-04-18 11:23:02 +00007843#endif
tuexendd729232011-11-01 23:04:43 +00007844 if (s_inp) {
7845 /* Assume lock is held here */
7846 it->inp = s_inp;
7847 SCTP_INP_INCR_REF(it->inp);
7848 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
7849 } else {
7850 SCTP_INP_INFO_RLOCK();
7851 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
7852 if (it->inp) {
7853 SCTP_INP_INCR_REF(it->inp);
7854 }
7855 SCTP_INP_INFO_RUNLOCK();
7856 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
7857
7858 }
7859 SCTP_IPI_ITERATOR_WQ_LOCK();
7860
7861 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
7862 if (sctp_it_ctl.iterator_running == 0) {
7863 sctp_wakeup_iterator();
7864 }
7865 SCTP_IPI_ITERATOR_WQ_UNLOCK();
7866 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
7867 return (0);
7868}