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