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