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