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 SCTP_INP_INFO_WLOCK_ASSERT();
1715 SCTP_INP_WLOCK_ASSERT(inp);
1716
1717 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
1718 /* only works with port reuse on */
1719 return (-1);
1720 }
1721 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
1722 return (0);
1723 }
1724 SCTP_INP_WUNLOCK(inp);
1725 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
1726 SCTP_BASE_INFO(hashmark))];
1727 /* Kick out all non-listeners to the TCP hash */
1728 LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
1729 if (tinp->sctp_lport != inp->sctp_lport) {
1730 continue;
1731 }
1732 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1733 continue;
1734 }
1735 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1736 continue;
1737 }
1738 if (SCTP_IS_LISTENING(tinp)) {
1739 continue;
1740 }
1741 SCTP_INP_WLOCK(tinp);
1742 LIST_REMOVE(tinp, sctp_hash);
1743 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
1744 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
1745 LIST_INSERT_HEAD(head, tinp, sctp_hash);
1746 SCTP_INP_WUNLOCK(tinp);
1747 }
1748 SCTP_INP_WLOCK(inp);
1749 /* Pull from where he was */
1750 LIST_REMOVE(inp, sctp_hash);
1751 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
1752 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
1753 LIST_INSERT_HEAD(head, inp, sctp_hash);
1754 return (0);
1755 }
1756
1757 struct sctp_inpcb *
sctp_pcb_findep(struct sockaddr * nam,int find_tcp_pool,int have_lock,uint32_t vrf_id)1758 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1759 uint32_t vrf_id)
1760 {
1761 /*
1762 * First we check the hash table to see if someone has this port
1763 * bound with just the port.
1764 */
1765 struct sctp_inpcb *inp;
1766 struct sctppcbhead *head;
1767 int lport;
1768 unsigned int i;
1769 #ifdef INET
1770 struct sockaddr_in *sin;
1771 #endif
1772 #ifdef INET6
1773 struct sockaddr_in6 *sin6;
1774 #endif
1775
1776 switch (nam->sa_family) {
1777 #ifdef INET
1778 case AF_INET:
1779 sin = (struct sockaddr_in *)nam;
1780 lport = sin->sin_port;
1781 break;
1782 #endif
1783 #ifdef INET6
1784 case AF_INET6:
1785 sin6 = (struct sockaddr_in6 *)nam;
1786 lport = sin6->sin6_port;
1787 break;
1788 #endif
1789 default:
1790 return (NULL);
1791 }
1792 /*
1793 * I could cheat here and just cast to one of the types but we will
1794 * do it right. It also provides the check against an Unsupported
1795 * type too.
1796 */
1797 /* Find the head of the ALLADDR chain */
1798 if (have_lock == 0) {
1799 SCTP_INP_INFO_RLOCK();
1800 }
1801 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1802 SCTP_BASE_INFO(hashmark))];
1803 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1804
1805 /*
1806 * If the TCP model exists it could be that the main listening
1807 * endpoint is gone but there still exists a connected socket for
1808 * this guy. If so we can return the first one that we find. This
1809 * may NOT be the correct one so the caller should be wary on the
1810 * returned INP. Currently the only caller that sets find_tcp_pool
1811 * is in bindx where we are verifying that a user CAN bind the
1812 * address. He either has bound it already, or someone else has, or
1813 * its open to bind, so this is good enough.
1814 */
1815 if (inp == NULL && find_tcp_pool) {
1816 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
1817 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
1818 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1819 if (inp) {
1820 break;
1821 }
1822 }
1823 }
1824 if (inp) {
1825 SCTP_INP_INCR_REF(inp);
1826 }
1827 if (have_lock == 0) {
1828 SCTP_INP_INFO_RUNLOCK();
1829 }
1830 return (inp);
1831 }
1832
1833 /*
1834 * Find an association for an endpoint with the pointer to whom you want to
1835 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1836 * need to change the *to to some other struct like a mbuf...
1837 */
1838 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)1839 sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
1840 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
1841 uint32_t vrf_id)
1842 {
1843 struct sctp_inpcb *inp = NULL;
1844 struct sctp_tcb *stcb;
1845
1846 SCTP_INP_INFO_RLOCK();
1847 if (find_tcp_pool) {
1848 if (inp_p != NULL) {
1849 stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
1850 vrf_id);
1851 } else {
1852 stcb = sctp_tcb_special_locate(&inp, from, to, netp,
1853 vrf_id);
1854 }
1855 if (stcb != NULL) {
1856 SCTP_INP_INFO_RUNLOCK();
1857 return (stcb);
1858 }
1859 }
1860 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1861 if (inp_p != NULL) {
1862 *inp_p = inp;
1863 }
1864 SCTP_INP_INFO_RUNLOCK();
1865 if (inp == NULL) {
1866 return (NULL);
1867 }
1868 /*
1869 * ok, we have an endpoint, now lets find the assoc for it (if any)
1870 * we now place the source address or from in the to of the find
1871 * endpoint call. Since in reality this chain is used from the
1872 * inbound packet side.
1873 */
1874 if (inp_p != NULL) {
1875 stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
1876 NULL);
1877 } else {
1878 stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
1879 NULL);
1880 }
1881 return (stcb);
1882 }
1883
1884 /*
1885 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1886 * find all addresses that the sender has specified in any address list. Each
1887 * address will be used to lookup the TCB and see if one exits.
1888 */
1889 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)1890 sctp_findassociation_special_addr(struct mbuf *m, int offset,
1891 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1892 struct sockaddr *dst)
1893 {
1894 struct sctp_paramhdr *phdr, param_buf;
1895 #if defined(INET) || defined(INET6)
1896 struct sctp_tcb *stcb;
1897 uint16_t ptype;
1898 #endif
1899 uint16_t plen;
1900 #ifdef INET
1901 struct sockaddr_in sin4;
1902 #endif
1903 #ifdef INET6
1904 struct sockaddr_in6 sin6;
1905 #endif
1906
1907 #ifdef INET
1908 memset(&sin4, 0, sizeof(sin4));
1909 sin4.sin_len = sizeof(sin4);
1910 sin4.sin_family = AF_INET;
1911 sin4.sin_port = sh->src_port;
1912 #endif
1913 #ifdef INET6
1914 memset(&sin6, 0, sizeof(sin6));
1915 sin6.sin6_len = sizeof(sin6);
1916 sin6.sin6_family = AF_INET6;
1917 sin6.sin6_port = sh->src_port;
1918 #endif
1919
1920 offset += sizeof(struct sctp_init_chunk);
1921
1922 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf));
1923 while (phdr != NULL) {
1924 /* now we must see if we want the parameter */
1925 #if defined(INET) || defined(INET6)
1926 ptype = ntohs(phdr->param_type);
1927 #endif
1928 plen = ntohs(phdr->param_length);
1929 if (plen == 0) {
1930 break;
1931 }
1932 #ifdef INET
1933 if (ptype == SCTP_IPV4_ADDRESS &&
1934 plen == sizeof(struct sctp_ipv4addr_param)) {
1935 /* Get the rest of the address */
1936 struct sctp_ipv4addr_param ip4_param, *p4;
1937
1938 phdr = sctp_get_next_param(m, offset,
1939 (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
1940 if (phdr == NULL) {
1941 return (NULL);
1942 }
1943 p4 = (struct sctp_ipv4addr_param *)phdr;
1944 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1945 /* look it up */
1946 stcb = sctp_findassociation_ep_addr(inp_p,
1947 (struct sockaddr *)&sin4, netp, dst, NULL);
1948 if (stcb != NULL) {
1949 return (stcb);
1950 }
1951 }
1952 #endif
1953 #ifdef INET6
1954 if (ptype == SCTP_IPV6_ADDRESS &&
1955 plen == sizeof(struct sctp_ipv6addr_param)) {
1956 /* Get the rest of the address */
1957 struct sctp_ipv6addr_param ip6_param, *p6;
1958
1959 phdr = sctp_get_next_param(m, offset,
1960 (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
1961 if (phdr == NULL) {
1962 return (NULL);
1963 }
1964 p6 = (struct sctp_ipv6addr_param *)phdr;
1965 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1966 /* look it up */
1967 stcb = sctp_findassociation_ep_addr(inp_p,
1968 (struct sockaddr *)&sin6, netp, dst, NULL);
1969 if (stcb != NULL) {
1970 return (stcb);
1971 }
1972 }
1973 #endif
1974 offset += SCTP_SIZE32(plen);
1975 phdr = sctp_get_next_param(m, offset, ¶m_buf,
1976 sizeof(param_buf));
1977 }
1978 return (NULL);
1979 }
1980
1981 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)1982 sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
1983 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1984 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
1985 {
1986 /*
1987 * Use my vtag to hash. If we find it we then verify the source addr
1988 * is in the assoc. If all goes well we save a bit on rec of a
1989 * packet.
1990 */
1991 struct sctpasochead *head;
1992 struct sctp_nets *net;
1993 struct sctp_tcb *stcb;
1994
1995 SCTP_INP_INFO_RLOCK();
1996 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
1997 SCTP_BASE_INFO(hashasocmark))];
1998 LIST_FOREACH(stcb, head, sctp_asocs) {
1999 SCTP_INP_RLOCK(stcb->sctp_ep);
2000 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2001 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2002 continue;
2003 }
2004 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2005 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2006 continue;
2007 }
2008 SCTP_TCB_LOCK(stcb);
2009 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2010 if (stcb->asoc.my_vtag == vtag) {
2011 /* candidate */
2012 if (stcb->rport != rport) {
2013 SCTP_TCB_UNLOCK(stcb);
2014 continue;
2015 }
2016 if (stcb->sctp_ep->sctp_lport != lport) {
2017 SCTP_TCB_UNLOCK(stcb);
2018 continue;
2019 }
2020 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2021 SCTP_TCB_UNLOCK(stcb);
2022 continue;
2023 }
2024 /* RRS:Need toaddr check here */
2025 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2026 /* Endpoint does not own this address */
2027 SCTP_TCB_UNLOCK(stcb);
2028 continue;
2029 }
2030 if (remote_tag) {
2031 /*
2032 * If we have both vtags that's all we match
2033 * on
2034 */
2035 if (stcb->asoc.peer_vtag == remote_tag) {
2036 /*
2037 * If both tags match we consider it
2038 * conclusive and check NO
2039 * source/destination addresses
2040 */
2041 goto conclusive;
2042 }
2043 }
2044 if (skip_src_check) {
2045 conclusive:
2046 if (from) {
2047 *netp = sctp_findnet(stcb, from);
2048 } else {
2049 *netp = NULL; /* unknown */
2050 }
2051 if (inp_p)
2052 *inp_p = stcb->sctp_ep;
2053 SCTP_INP_INFO_RUNLOCK();
2054 return (stcb);
2055 }
2056 net = sctp_findnet(stcb, from);
2057 if (net) {
2058 /* yep its him. */
2059 *netp = net;
2060 SCTP_STAT_INCR(sctps_vtagexpress);
2061 *inp_p = stcb->sctp_ep;
2062 SCTP_INP_INFO_RUNLOCK();
2063 return (stcb);
2064 } else {
2065 /*
2066 * not him, this should only happen in rare
2067 * cases so I peg it.
2068 */
2069 SCTP_STAT_INCR(sctps_vtagbogus);
2070 }
2071 }
2072 SCTP_TCB_UNLOCK(stcb);
2073 }
2074 SCTP_INP_INFO_RUNLOCK();
2075 return (NULL);
2076 }
2077
2078 /*
2079 * Find an association with the pointer to the inbound IP packet. This can be
2080 * a IPv4 or IPv6 packet.
2081 */
2082 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)2083 sctp_findassociation_addr(struct mbuf *m, int offset,
2084 struct sockaddr *src, struct sockaddr *dst,
2085 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2086 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2087 {
2088 struct sctp_tcb *stcb;
2089 struct sctp_inpcb *inp;
2090
2091 if (sh->v_tag) {
2092 /* we only go down this path if vtag is non-zero */
2093 stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
2094 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2095 if (stcb) {
2096 return (stcb);
2097 }
2098 }
2099
2100 if (inp_p) {
2101 stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
2102 1, vrf_id);
2103 inp = *inp_p;
2104 } else {
2105 stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
2106 1, vrf_id);
2107 }
2108 SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2109 if (stcb == NULL && inp) {
2110 /* Found a EP but not this address */
2111 if ((ch->chunk_type == SCTP_INITIATION) ||
2112 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2113 /*-
2114 * special hook, we do NOT return linp or an
2115 * association that is linked to an existing
2116 * association that is under the TCP pool (i.e. no
2117 * listener exists). The endpoint finding routine
2118 * will always find a listener before examining the
2119 * TCP pool.
2120 */
2121 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2122 if (inp_p) {
2123 *inp_p = NULL;
2124 }
2125 return (NULL);
2126 }
2127 stcb = sctp_findassociation_special_addr(m,
2128 offset, sh, &inp, netp, dst);
2129 if (inp_p != NULL) {
2130 *inp_p = inp;
2131 }
2132 }
2133 }
2134 SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2135 return (stcb);
2136 }
2137
2138 /*
2139 * lookup an association by an ASCONF lookup address.
2140 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2141 */
2142 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)2143 sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
2144 struct sockaddr *dst, struct sctphdr *sh,
2145 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2146 {
2147 struct sctp_tcb *stcb;
2148 union sctp_sockstore remote_store;
2149 struct sctp_paramhdr param_buf, *phdr;
2150 int ptype;
2151 int zero_address = 0;
2152 #ifdef INET
2153 struct sockaddr_in *sin;
2154 #endif
2155 #ifdef INET6
2156 struct sockaddr_in6 *sin6;
2157 #endif
2158
2159 memset(&remote_store, 0, sizeof(remote_store));
2160 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2161 ¶m_buf, sizeof(struct sctp_paramhdr));
2162 if (phdr == NULL) {
2163 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2164 __func__);
2165 return NULL;
2166 }
2167 ptype = (int)((uint32_t)ntohs(phdr->param_type));
2168 /* get the correlation address */
2169 switch (ptype) {
2170 #ifdef INET6
2171 case SCTP_IPV6_ADDRESS:
2172 {
2173 /* ipv6 address param */
2174 struct sctp_ipv6addr_param *p6, p6_buf;
2175
2176 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2177 return NULL;
2178 }
2179 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2180 offset + sizeof(struct sctp_asconf_chunk),
2181 &p6_buf.ph, sizeof(p6_buf));
2182 if (p6 == NULL) {
2183 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2184 __func__);
2185 return (NULL);
2186 }
2187 sin6 = &remote_store.sin6;
2188 sin6->sin6_family = AF_INET6;
2189 sin6->sin6_len = sizeof(*sin6);
2190 sin6->sin6_port = sh->src_port;
2191 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2192 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2193 zero_address = 1;
2194 break;
2195 }
2196 #endif
2197 #ifdef INET
2198 case SCTP_IPV4_ADDRESS:
2199 {
2200 /* ipv4 address param */
2201 struct sctp_ipv4addr_param *p4, p4_buf;
2202
2203 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2204 return NULL;
2205 }
2206 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2207 offset + sizeof(struct sctp_asconf_chunk),
2208 &p4_buf.ph, sizeof(p4_buf));
2209 if (p4 == NULL) {
2210 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2211 __func__);
2212 return (NULL);
2213 }
2214 sin = &remote_store.sin;
2215 sin->sin_family = AF_INET;
2216 sin->sin_len = sizeof(*sin);
2217 sin->sin_port = sh->src_port;
2218 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2219 if (sin->sin_addr.s_addr == INADDR_ANY)
2220 zero_address = 1;
2221 break;
2222 }
2223 #endif
2224 default:
2225 /* invalid address param type */
2226 return NULL;
2227 }
2228
2229 if (zero_address) {
2230 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
2231 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
2232 if (stcb != NULL) {
2233 SCTP_INP_DECR_REF(*inp_p);
2234 }
2235 } else {
2236 stcb = sctp_findassociation_ep_addr(inp_p,
2237 &remote_store.sa, netp,
2238 dst, NULL);
2239 }
2240 return (stcb);
2241 }
2242
2243 /*
2244 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2245 * addresses. This way if we don't get a bind we by default pick a ephemeral
2246 * port with all addresses bound.
2247 */
2248 int
sctp_inpcb_alloc(struct socket * so,uint32_t vrf_id)2249 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2250 {
2251 /*
2252 * we get called when a new endpoint starts up. We need to allocate
2253 * the sctp_inpcb structure from the zone and init it. Mark it as
2254 * unbound and find a port that we can use as an ephemeral with
2255 * INADDR_ANY. If the user binds later no problem we can then add in
2256 * the specific addresses. And setup the default parameters for the
2257 * EP.
2258 */
2259 int i, error;
2260 struct sctp_inpcb *inp;
2261 struct sctp_pcb *m;
2262 struct timeval time;
2263 sctp_sharedkey_t *null_key;
2264
2265 error = 0;
2266
2267 SCTP_INP_INFO_WLOCK();
2268 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2269 if (inp == NULL) {
2270 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2271 SCTP_INP_INFO_WUNLOCK();
2272 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2273 return (ENOBUFS);
2274 }
2275 /* zap it */
2276 memset(inp, 0, sizeof(*inp));
2277
2278 /* bump generations */
2279 /* setup socket pointers */
2280 inp->sctp_socket = so;
2281 inp->ip_inp.inp.inp_socket = so;
2282 inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
2283 #ifdef INET6
2284 if (INP_SOCKAF(so) == AF_INET6) {
2285 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2286 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2287 }
2288 if (MODULE_GLOBAL(ip6_v6only)) {
2289 inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
2290 }
2291 }
2292 #endif
2293 inp->sctp_associd_counter = 1;
2294 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2295 inp->sctp_frag_point = 0;
2296 inp->max_cwnd = 0;
2297 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2298 inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
2299 inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
2300 inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
2301 inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
2302 inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
2303 inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
2304 inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
2305 inp->idata_supported = 0;
2306 inp->rcv_edmid = SCTP_EDMID_NONE;
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 rw_init_flags(&inp->ip_inp.inp.inp_lock, "sctpinp",
2373 RW_RECURSE | RW_DUPOK);
2374 SCTP_INP_READ_LOCK_INIT(inp);
2375 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2376 /* lock the new ep */
2377 SCTP_INP_WLOCK(inp);
2378
2379 /* add it to the info area */
2380 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2381 SCTP_INP_INFO_WUNLOCK();
2382
2383 TAILQ_INIT(&inp->read_queue);
2384 LIST_INIT(&inp->sctp_addr_list);
2385
2386 LIST_INIT(&inp->sctp_asoc_list);
2387
2388 #ifdef SCTP_TRACK_FREED_ASOCS
2389 /* TEMP CODE */
2390 LIST_INIT(&inp->sctp_asoc_free_list);
2391 #endif
2392 /* Init the timer structure for signature change */
2393 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2394 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2395
2396 /* now init the actual endpoint default data */
2397 m = &inp->sctp_ep;
2398
2399 /* setup the base timeout information */
2400 m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */
2401 m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */
2402 m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2403 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2404 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2405 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2406 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2407 /* all max/min max are in ms */
2408 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2409 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2410 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2411 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2412 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2413 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2414 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2415 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2416 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2417 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2418 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2419 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2420 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2421
2422 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2423 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2424 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
2425 /* number of streams to pre-open on a association */
2426 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2427
2428 m->default_mtu = 0;
2429 /* Add adaptation cookie */
2430 m->adaptation_layer_indicator = 0;
2431 m->adaptation_layer_indicator_provided = 0;
2432
2433 /* seed random number generator */
2434 m->random_counter = 1;
2435 m->store_at = SCTP_SIGNATURE_SIZE;
2436 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2437 sctp_fill_random_store(m);
2438
2439 /* Minimum cookie size */
2440 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2441 sizeof(struct sctp_state_cookie);
2442 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2443
2444 /* Setup the initial secret */
2445 (void)SCTP_GETTIME_TIMEVAL(&time);
2446 m->time_of_secret_change = time.tv_sec;
2447
2448 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2449 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2450 }
2451 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2452
2453 /* How long is a cookie good for ? */
2454 m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2455 /*
2456 * Initialize authentication parameters
2457 */
2458 m->local_hmacs = sctp_default_supported_hmaclist();
2459 m->local_auth_chunks = sctp_alloc_chunklist();
2460 if (inp->asconf_supported) {
2461 sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
2462 sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
2463 }
2464 m->default_dscp = 0;
2465 #ifdef INET6
2466 m->default_flowlabel = 0;
2467 #endif
2468 m->port = 0; /* encapsulation disabled by default */
2469 LIST_INIT(&m->shared_keys);
2470 /* add default NULL key as key id 0 */
2471 null_key = sctp_alloc_sharedkey();
2472 sctp_insert_sharedkey(&m->shared_keys, null_key);
2473 SCTP_INP_WUNLOCK(inp);
2474 #ifdef SCTP_LOG_CLOSING
2475 sctp_log_closing(inp, NULL, 12);
2476 #endif
2477 return (error);
2478 }
2479
2480 void
sctp_move_pcb_and_assoc(struct sctp_inpcb * old_inp,struct sctp_inpcb * new_inp,struct sctp_tcb * stcb)2481 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2482 struct sctp_tcb *stcb)
2483 {
2484 struct sctp_nets *net;
2485 uint16_t lport, rport;
2486 struct sctppcbhead *head;
2487 struct sctp_laddr *laddr, *oladdr;
2488
2489 atomic_add_int(&stcb->asoc.refcnt, 1);
2490 SCTP_TCB_UNLOCK(stcb);
2491 SCTP_INP_INFO_WLOCK();
2492 SCTP_INP_WLOCK(old_inp);
2493 SCTP_INP_WLOCK(new_inp);
2494 SCTP_TCB_LOCK(stcb);
2495 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2496
2497 #ifdef INET6
2498 if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2499 new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS;
2500 if (old_inp->ip_inp.inp.in6p_outputopts) {
2501 new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT);
2502 }
2503 }
2504 #endif
2505 #if defined(INET) && defined(INET6)
2506 else
2507 #endif
2508 #ifdef INET
2509 {
2510 new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos;
2511 new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl;
2512 }
2513 #endif
2514 new_inp->sctp_ep.time_of_secret_change =
2515 old_inp->sctp_ep.time_of_secret_change;
2516 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2517 sizeof(old_inp->sctp_ep.secret_key));
2518 new_inp->sctp_ep.current_secret_number =
2519 old_inp->sctp_ep.current_secret_number;
2520 new_inp->sctp_ep.last_secret_number =
2521 old_inp->sctp_ep.last_secret_number;
2522 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2523
2524 /* make it so new data pours into the new socket */
2525 stcb->sctp_socket = new_inp->sctp_socket;
2526 stcb->sctp_ep = new_inp;
2527
2528 /* Copy the port across */
2529 lport = new_inp->sctp_lport = old_inp->sctp_lport;
2530 rport = stcb->rport;
2531 /* Pull the tcb from the old association */
2532 LIST_REMOVE(stcb, sctp_tcbhash);
2533 LIST_REMOVE(stcb, sctp_tcblist);
2534 if (stcb->asoc.in_asocid_hash) {
2535 LIST_REMOVE(stcb, sctp_tcbasocidhash);
2536 }
2537 /* Now insert the new_inp into the TCP connected hash */
2538 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2539
2540 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2541 /* Its safe to access */
2542 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2543
2544 /* Now move the tcb into the endpoint list */
2545 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2546 /*
2547 * Question, do we even need to worry about the ep-hash since we
2548 * only have one connection? Probably not :> so lets get rid of it
2549 * and not suck up any kernel memory in that.
2550 */
2551 if (stcb->asoc.in_asocid_hash) {
2552 struct sctpasochead *lhd;
2553
2554 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2555 new_inp->hashasocidmark)];
2556 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2557 }
2558 /* Ok. Let's restart timer. */
2559 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2560 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2561 stcb, net);
2562 }
2563
2564 SCTP_INP_INFO_WUNLOCK();
2565 if (new_inp->sctp_tcbhash != NULL) {
2566 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2567 new_inp->sctp_tcbhash = NULL;
2568 }
2569 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2570 /* Subset bound, so copy in the laddr list from the old_inp */
2571 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2572 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2573 if (laddr == NULL) {
2574 /*
2575 * Gak, what can we do? This assoc is really
2576 * HOSED. We probably should send an abort
2577 * here.
2578 */
2579 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2580 continue;
2581 }
2582 SCTP_INCR_LADDR_COUNT();
2583 memset(laddr, 0, sizeof(*laddr));
2584 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2585 laddr->ifa = oladdr->ifa;
2586 atomic_add_int(&laddr->ifa->refcount, 1);
2587 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2588 sctp_nxt_addr);
2589 new_inp->laddr_count++;
2590 if (oladdr == stcb->asoc.last_used_address) {
2591 stcb->asoc.last_used_address = laddr;
2592 }
2593 }
2594 }
2595 /* Now any running timers need to be adjusted. */
2596 if (stcb->asoc.dack_timer.ep == old_inp) {
2597 SCTP_INP_DECR_REF(old_inp);
2598 stcb->asoc.dack_timer.ep = new_inp;
2599 SCTP_INP_INCR_REF(new_inp);
2600 }
2601 if (stcb->asoc.asconf_timer.ep == old_inp) {
2602 SCTP_INP_DECR_REF(old_inp);
2603 stcb->asoc.asconf_timer.ep = new_inp;
2604 SCTP_INP_INCR_REF(new_inp);
2605 }
2606 if (stcb->asoc.strreset_timer.ep == old_inp) {
2607 SCTP_INP_DECR_REF(old_inp);
2608 stcb->asoc.strreset_timer.ep = new_inp;
2609 SCTP_INP_INCR_REF(new_inp);
2610 }
2611 if (stcb->asoc.shut_guard_timer.ep == old_inp) {
2612 SCTP_INP_DECR_REF(old_inp);
2613 stcb->asoc.shut_guard_timer.ep = new_inp;
2614 SCTP_INP_INCR_REF(new_inp);
2615 }
2616 if (stcb->asoc.autoclose_timer.ep == old_inp) {
2617 SCTP_INP_DECR_REF(old_inp);
2618 stcb->asoc.autoclose_timer.ep = new_inp;
2619 SCTP_INP_INCR_REF(new_inp);
2620 }
2621 if (stcb->asoc.delete_prim_timer.ep == old_inp) {
2622 SCTP_INP_DECR_REF(old_inp);
2623 stcb->asoc.delete_prim_timer.ep = new_inp;
2624 SCTP_INP_INCR_REF(new_inp);
2625 }
2626 /* now what about the nets? */
2627 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2628 if (net->pmtu_timer.ep == old_inp) {
2629 SCTP_INP_DECR_REF(old_inp);
2630 net->pmtu_timer.ep = new_inp;
2631 SCTP_INP_INCR_REF(new_inp);
2632 }
2633 if (net->hb_timer.ep == old_inp) {
2634 SCTP_INP_DECR_REF(old_inp);
2635 net->hb_timer.ep = new_inp;
2636 SCTP_INP_INCR_REF(new_inp);
2637 }
2638 if (net->rxt_timer.ep == old_inp) {
2639 SCTP_INP_DECR_REF(old_inp);
2640 net->rxt_timer.ep = new_inp;
2641 SCTP_INP_INCR_REF(new_inp);
2642 }
2643 }
2644 SCTP_INP_WUNLOCK(new_inp);
2645 SCTP_INP_WUNLOCK(old_inp);
2646 }
2647
2648 /*
2649 * insert an laddr entry with the given ifa for the desired list
2650 */
2651 static int
sctp_insert_laddr(struct sctpladdr * list,struct sctp_ifa * ifa,uint32_t act)2652 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
2653 {
2654 struct sctp_laddr *laddr;
2655
2656 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2657 if (laddr == NULL) {
2658 /* out of memory? */
2659 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2660 return (EINVAL);
2661 }
2662 SCTP_INCR_LADDR_COUNT();
2663 memset(laddr, 0, sizeof(*laddr));
2664 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2665 laddr->ifa = ifa;
2666 laddr->action = act;
2667 atomic_add_int(&ifa->refcount, 1);
2668 /* insert it */
2669 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
2670
2671 return (0);
2672 }
2673
2674 /*
2675 * Remove an laddr entry from the local address list (on an assoc)
2676 */
2677 static void
sctp_remove_laddr(struct sctp_laddr * laddr)2678 sctp_remove_laddr(struct sctp_laddr *laddr)
2679 {
2680
2681 /* remove from the list */
2682 LIST_REMOVE(laddr, sctp_nxt_addr);
2683 sctp_free_ifa(laddr->ifa);
2684 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
2685 SCTP_DECR_LADDR_COUNT();
2686 }
2687
2688 /*
2689 * Bind the socket, with the PCB and global info locks held. Note, if a
2690 * socket address is specified, the PCB lock may be dropped and re-acquired.
2691 *
2692 * sctp_ifap is used to bypass normal local address validation checks.
2693 */
2694 int
sctp_inpcb_bind_locked(struct sctp_inpcb * inp,struct sockaddr * addr,struct sctp_ifa * sctp_ifap,struct thread * td)2695 sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
2696 struct sctp_ifa *sctp_ifap, struct thread *td)
2697 {
2698 /* bind a ep to a socket address */
2699 struct sctppcbhead *head;
2700 struct sctp_inpcb *inp_tmp;
2701 struct inpcb *ip_inp;
2702 int port_reuse_active = 0;
2703 int bindall;
2704 uint16_t lport;
2705 int error;
2706 uint32_t vrf_id;
2707
2708 KASSERT(td != NULL, ("%s: null thread", __func__));
2709
2710 error = 0;
2711 lport = 0;
2712 bindall = 1;
2713 ip_inp = &inp->ip_inp.inp;
2714
2715 SCTP_INP_INFO_WLOCK_ASSERT();
2716 SCTP_INP_WLOCK_ASSERT(inp);
2717
2718 #ifdef SCTP_DEBUG
2719 if (addr) {
2720 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
2721 ntohs(((struct sockaddr_in *)addr)->sin_port));
2722 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
2723 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2724 }
2725 #endif
2726 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2727 error = EINVAL;
2728 /* already did a bind, subsequent binds NOT allowed ! */
2729 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2730 goto out;
2731 }
2732 if (addr != NULL) {
2733 switch (addr->sa_family) {
2734 #ifdef INET
2735 case AF_INET:
2736 {
2737 struct sockaddr_in *sin;
2738
2739 /* IPV6_V6ONLY socket? */
2740 if (SCTP_IPV6_V6ONLY(inp)) {
2741 error = EINVAL;
2742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2743 goto out;
2744 }
2745 if (addr->sa_len != sizeof(*sin)) {
2746 error = EINVAL;
2747 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2748 goto out;
2749 }
2750
2751 sin = (struct sockaddr_in *)addr;
2752 lport = sin->sin_port;
2753 /*
2754 * For LOOPBACK the prison_local_ip4() call
2755 * will transmute the ip address to the
2756 * proper value.
2757 */
2758 if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) {
2759 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2760 goto out;
2761 }
2762 if (sin->sin_addr.s_addr != INADDR_ANY) {
2763 bindall = 0;
2764 }
2765 break;
2766 }
2767 #endif
2768 #ifdef INET6
2769 case AF_INET6:
2770 {
2771 /*
2772 * Only for pure IPv6 Address. (No IPv4
2773 * Mapped!)
2774 */
2775 struct sockaddr_in6 *sin6;
2776
2777 sin6 = (struct sockaddr_in6 *)addr;
2778 if (addr->sa_len != sizeof(*sin6)) {
2779 error = EINVAL;
2780 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2781 goto out;
2782 }
2783 lport = sin6->sin6_port;
2784 /*
2785 * For LOOPBACK the prison_local_ip6() call
2786 * will transmute the ipv6 address to the
2787 * proper value.
2788 */
2789 if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr,
2790 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
2791 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2792 goto out;
2793 }
2794 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2795 bindall = 0;
2796 /* KAME hack: embed scopeid */
2797 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
2798 error = EINVAL;
2799 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2800 goto out;
2801 }
2802 }
2803 /* this must be cleared for ifa_ifwithaddr() */
2804 sin6->sin6_scope_id = 0;
2805 break;
2806 }
2807 #endif
2808 default:
2809 error = EAFNOSUPPORT;
2810 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2811 goto out;
2812 }
2813 }
2814 /* Setup a vrf_id to be the default for the non-bind-all case. */
2815 vrf_id = inp->def_vrf_id;
2816
2817 if (lport) {
2818 /*
2819 * Did the caller specify a port? if so we must see if an ep
2820 * already has this one bound.
2821 */
2822 /* got to be root to get at low ports */
2823 if (ntohs(lport) < IPPORT_RESERVED &&
2824 (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
2825 goto out;
2826 }
2827 SCTP_INP_INCR_REF(inp);
2828 SCTP_INP_WUNLOCK(inp);
2829 if (bindall) {
2830 vrf_id = inp->def_vrf_id;
2831 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2832 if (inp_tmp != NULL) {
2833 /*
2834 * lock guy returned and lower count note
2835 * that we are not bound so inp_tmp should
2836 * NEVER be inp. And it is this inp
2837 * (inp_tmp) that gets the reference bump,
2838 * so we must lower it.
2839 */
2840 SCTP_INP_DECR_REF(inp_tmp);
2841 /* unlock info */
2842 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2843 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2844 /*
2845 * Ok, must be one-2-one and
2846 * allowing port re-use
2847 */
2848 port_reuse_active = 1;
2849 goto continue_anyway;
2850 }
2851 SCTP_INP_WLOCK(inp);
2852 SCTP_INP_DECR_REF(inp);
2853 error = EADDRINUSE;
2854 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2855 goto out;
2856 }
2857 } else {
2858 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2859 if (inp_tmp != NULL) {
2860 /*
2861 * lock guy returned and lower count note
2862 * that we are not bound so inp_tmp should
2863 * NEVER be inp. And it is this inp
2864 * (inp_tmp) that gets the reference bump,
2865 * so we must lower it.
2866 */
2867 SCTP_INP_DECR_REF(inp_tmp);
2868 /* unlock info */
2869 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2870 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2871 /*
2872 * Ok, must be one-2-one and
2873 * allowing port re-use
2874 */
2875 port_reuse_active = 1;
2876 goto continue_anyway;
2877 }
2878 SCTP_INP_WLOCK(inp);
2879 SCTP_INP_DECR_REF(inp);
2880 error = EADDRINUSE;
2881 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2882 goto out;
2883 }
2884 }
2885 continue_anyway:
2886 SCTP_INP_WLOCK(inp);
2887 SCTP_INP_DECR_REF(inp);
2888 if (bindall) {
2889 /* verify that no lport is not used by a singleton */
2890 if ((port_reuse_active == 0) &&
2891 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
2892 /* Sorry someone already has this one bound */
2893 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2894 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2895 port_reuse_active = 1;
2896 } else {
2897 error = EADDRINUSE;
2898 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2899 goto out;
2900 }
2901 }
2902 }
2903 } else {
2904 uint16_t first, last, candidate;
2905 uint16_t count;
2906
2907 if (ip_inp->inp_flags & INP_HIGHPORT) {
2908 first = MODULE_GLOBAL(ipport_hifirstauto);
2909 last = MODULE_GLOBAL(ipport_hilastauto);
2910 } else if (ip_inp->inp_flags & INP_LOWPORT) {
2911 if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
2912 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2913 goto out;
2914 }
2915 first = MODULE_GLOBAL(ipport_lowfirstauto);
2916 last = MODULE_GLOBAL(ipport_lowlastauto);
2917 } else {
2918 first = MODULE_GLOBAL(ipport_firstauto);
2919 last = MODULE_GLOBAL(ipport_lastauto);
2920 }
2921 if (first > last) {
2922 uint16_t temp;
2923
2924 temp = first;
2925 first = last;
2926 last = temp;
2927 }
2928 count = last - first + 1; /* number of candidates */
2929 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
2930
2931 for (;;) {
2932 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
2933 lport = htons(candidate);
2934 break;
2935 }
2936 if (--count == 0) {
2937 error = EADDRINUSE;
2938 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2939 goto out;
2940 }
2941 if (candidate == last)
2942 candidate = first;
2943 else
2944 candidate = candidate + 1;
2945 }
2946 }
2947 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2948 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2949 /*
2950 * this really should not happen. The guy did a non-blocking
2951 * bind and then did a close at the same time.
2952 */
2953 error = EINVAL;
2954 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2955 goto out;
2956 }
2957 /* ok we look clear to give out this port, so lets setup the binding */
2958 if (bindall) {
2959 /* binding to all addresses, so just set in the proper flags */
2960 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2961 /* set the automatic addr changes from kernel flag */
2962 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
2963 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2964 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2965 } else {
2966 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2967 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2968 }
2969 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
2970 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
2971 } else {
2972 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
2973 }
2974 /*
2975 * set the automatic mobility_base from kernel flag (by
2976 * micchie)
2977 */
2978 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
2979 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
2980 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2981 } else {
2982 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
2983 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2984 }
2985 /*
2986 * set the automatic mobility_fasthandoff from kernel flag
2987 * (by micchie)
2988 */
2989 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
2990 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
2991 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2992 } else {
2993 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
2994 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2995 }
2996 } else {
2997 /*
2998 * bind specific, make sure flags is off and add a new
2999 * address structure to the sctp_addr_list inside the ep
3000 * structure.
3001 *
3002 * We will need to allocate one and insert it at the head.
3003 * The socketopt call can just insert new addresses in there
3004 * as well. It will also have to do the embed scope kame
3005 * hack too (before adding).
3006 */
3007 struct sctp_ifa *ifa;
3008 union sctp_sockstore store;
3009
3010 memset(&store, 0, sizeof(store));
3011 switch (addr->sa_family) {
3012 #ifdef INET
3013 case AF_INET:
3014 memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
3015 store.sin.sin_port = 0;
3016 break;
3017 #endif
3018 #ifdef INET6
3019 case AF_INET6:
3020 memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
3021 store.sin6.sin6_port = 0;
3022 break;
3023 #endif
3024 default:
3025 break;
3026 }
3027 /*
3028 * first find the interface with the bound address need to
3029 * zero out the port to find the address! yuck! can't do
3030 * this earlier since need port for sctp_pcb_findep()
3031 */
3032 if (sctp_ifap != NULL) {
3033 ifa = sctp_ifap;
3034 } else {
3035 /*
3036 * Note for BSD we hit here always other O/S's will
3037 * pass things in via the sctp_ifap argument.
3038 */
3039 ifa = sctp_find_ifa_by_addr(&store.sa,
3040 vrf_id, SCTP_ADDR_NOT_LOCKED);
3041 }
3042 if (ifa == NULL) {
3043 error = EADDRNOTAVAIL;
3044 /* Can't find an interface with that address */
3045 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3046 goto out;
3047 }
3048 #ifdef INET6
3049 if (addr->sa_family == AF_INET6) {
3050 /* GAK, more FIXME IFA lock? */
3051 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3052 /* Can't bind a non-existent addr. */
3053 error = EINVAL;
3054 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3055 goto out;
3056 }
3057 }
3058 #endif
3059 /* we're not bound all */
3060 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3061 /* allow bindx() to send ASCONF's for binding changes */
3062 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3063 /* clear automatic addr changes from kernel flag */
3064 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3065
3066 /* add this address to the endpoint list */
3067 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3068 if (error != 0)
3069 goto out;
3070 inp->laddr_count++;
3071 }
3072 /* find the bucket */
3073 if (port_reuse_active) {
3074 /* Put it into tcp 1-2-1 hash */
3075 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3076 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3077 } else {
3078 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3079 }
3080 /* put it in the bucket */
3081 LIST_INSERT_HEAD(head, inp, sctp_hash);
3082 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3083 (void *)head, ntohs(lport), port_reuse_active);
3084 /* set in the port */
3085 inp->sctp_lport = lport;
3086
3087 /* turn off just the unbound flag */
3088 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0,
3089 ("%s: inp %p is already bound", __func__, inp));
3090 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3091 out:
3092 return (error);
3093 }
3094
3095 int
sctp_inpcb_bind(struct socket * so,struct sockaddr * addr,struct sctp_ifa * sctp_ifap,struct thread * td)3096 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3097 struct sctp_ifa *sctp_ifap, struct thread *td)
3098 {
3099 struct sctp_inpcb *inp;
3100 int error;
3101
3102 inp = so->so_pcb;
3103 SCTP_INP_INFO_WLOCK();
3104 SCTP_INP_WLOCK(inp);
3105 error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td);
3106 SCTP_INP_WUNLOCK(inp);
3107 SCTP_INP_INFO_WUNLOCK();
3108 return (error);
3109 }
3110
3111 static void
sctp_iterator_inp_being_freed(struct sctp_inpcb * inp)3112 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3113 {
3114 struct sctp_iterator *it, *nit;
3115
3116 /*
3117 * We enter with the only the ITERATOR_LOCK in place and a write
3118 * lock on the inp_info stuff.
3119 */
3120 it = sctp_it_ctl.cur_it;
3121 if (it && (it->vn != curvnet)) {
3122 /* Its not looking at our VNET */
3123 return;
3124 }
3125 if (it && (it->inp == inp)) {
3126 /*
3127 * This is tricky and we hold the iterator lock, but when it
3128 * returns and gets the lock (when we release it) the
3129 * iterator will try to operate on inp. We need to stop that
3130 * from happening. But of course the iterator has a
3131 * reference on the stcb and inp. We can mark it and it will
3132 * stop.
3133 *
3134 * If its a single iterator situation, we set the end
3135 * iterator flag. Otherwise we set the iterator to go to the
3136 * next inp.
3137 *
3138 */
3139 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3140 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3141 } else {
3142 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3143 }
3144 }
3145 /*
3146 * Now go through and remove any single reference to our inp that
3147 * may be still pending on the list
3148 */
3149 SCTP_IPI_ITERATOR_WQ_LOCK();
3150 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3151 if (it->vn != curvnet) {
3152 continue;
3153 }
3154 if (it->inp == inp) {
3155 /* This one points to me is it inp specific? */
3156 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3157 /* Remove and free this one */
3158 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3159 it, sctp_nxt_itr);
3160 if (it->function_atend != NULL) {
3161 (*it->function_atend) (it->pointer, it->val);
3162 }
3163 SCTP_FREE(it, SCTP_M_ITER);
3164 } else {
3165 it->inp = LIST_NEXT(it->inp, sctp_list);
3166 if (it->inp) {
3167 SCTP_INP_INCR_REF(it->inp);
3168 }
3169 }
3170 /*
3171 * When its put in the refcnt is incremented so decr
3172 * it
3173 */
3174 SCTP_INP_DECR_REF(inp);
3175 }
3176 }
3177 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3178 }
3179
3180 /* release sctp_inpcb unbind the port */
3181 void
sctp_inpcb_free(struct sctp_inpcb * inp,int immediate,int from)3182 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3183 {
3184 /*
3185 * Here we free a endpoint. We must find it (if it is in the Hash
3186 * table) and remove it from there. Then we must also find it in the
3187 * overall list and remove it from there. After all removals are
3188 * complete then any timer has to be stopped. Then start the actual
3189 * freeing. a) Any local lists. b) Any associations. c) The hash of
3190 * all associations. d) finally the ep itself.
3191 */
3192 struct sctp_tcb *stcb, *nstcb;
3193 struct sctp_laddr *laddr, *nladdr;
3194 struct inpcb *ip_pcb;
3195 struct socket *so;
3196 int being_refed = 0;
3197 struct sctp_queued_to_read *sq, *nsq;
3198 int cnt;
3199 sctp_sharedkey_t *shared_key, *nshared_key;
3200
3201 #ifdef SCTP_LOG_CLOSING
3202 sctp_log_closing(inp, NULL, 0);
3203 #endif
3204 SCTP_ITERATOR_LOCK();
3205 /* mark any iterators on the list or being processed */
3206 sctp_iterator_inp_being_freed(inp);
3207 SCTP_ITERATOR_UNLOCK();
3208
3209 SCTP_ASOC_CREATE_LOCK(inp);
3210 SCTP_INP_INFO_WLOCK();
3211 SCTP_INP_WLOCK(inp);
3212 so = inp->sctp_socket;
3213 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0,
3214 ("%s: inp %p still has socket", __func__, inp));
3215 KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0,
3216 ("%s: double free of inp %p", __func__, inp));
3217 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3218 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3219 /* socket is gone, so no more wakeups allowed */
3220 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3221 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3222 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3223 }
3224 /* First time through we have the socket lock, after that no more. */
3225 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3226 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3227
3228 if (inp->control) {
3229 sctp_m_freem(inp->control);
3230 inp->control = NULL;
3231 }
3232 if (inp->pkt) {
3233 sctp_m_freem(inp->pkt);
3234 inp->pkt = NULL;
3235 }
3236 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3237 * here but I will be nice :> (i.e.
3238 * ip_pcb = ep;) */
3239 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3240 int cnt_in_sd;
3241
3242 cnt_in_sd = 0;
3243 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
3244 SCTP_TCB_LOCK(stcb);
3245 /* Disconnect the socket please. */
3246 stcb->sctp_socket = NULL;
3247 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
3248 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3249 /* Skip guys being freed */
3250 cnt_in_sd++;
3251 if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3252 /*
3253 * Special case - we did not start a
3254 * kill timer on the asoc due to it
3255 * was not closed. So go ahead and
3256 * start it now.
3257 */
3258 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
3259 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3260 }
3261 SCTP_TCB_UNLOCK(stcb);
3262 continue;
3263 }
3264 if (((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3265 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) &&
3266 (stcb->asoc.total_output_queue_size == 0)) {
3267 /*
3268 * If we have data in queue, we don't want
3269 * to just free since the app may have done,
3270 * send()/close or connect/send/close. And
3271 * it wants the data to get across first.
3272 */
3273 /* Just abandon things in the front states */
3274 if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_NOFORCE,
3275 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3276 cnt_in_sd++;
3277 }
3278 continue;
3279 }
3280 if ((stcb->asoc.size_on_reasm_queue > 0) ||
3281 (stcb->asoc.size_on_all_streams > 0) ||
3282 ((so != NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0))) {
3283 /* Left with Data unread */
3284 struct mbuf *op_err;
3285
3286 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3287 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3288 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3289 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3290 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3291 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3292 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3293 }
3294 if (sctp_free_assoc(inp, stcb,
3295 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3296 cnt_in_sd++;
3297 }
3298 continue;
3299 } else if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3300 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3301 (stcb->asoc.stream_queue_cnt == 0)) {
3302 if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, &stcb->asoc)) {
3303 goto abort_anyway;
3304 }
3305 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
3306 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3307 struct sctp_nets *netp;
3308
3309 /*
3310 * there is nothing queued to send,
3311 * so I send shutdown
3312 */
3313 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3314 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3315 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3316 }
3317 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
3318 sctp_stop_timers_for_shutdown(stcb);
3319 if (stcb->asoc.alternate) {
3320 netp = stcb->asoc.alternate;
3321 } else {
3322 netp = stcb->asoc.primary_destination;
3323 }
3324 sctp_send_shutdown(stcb, netp);
3325 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
3326 netp);
3327 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
3328 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3329 }
3330 } else {
3331 /* mark into shutdown pending */
3332 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
3333 if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, &stcb->asoc)) {
3334 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
3335 }
3336 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3337 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3338 (stcb->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3339 struct mbuf *op_err;
3340
3341 abort_anyway:
3342 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3343 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3344 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3345 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3346 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3347 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3348 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3349 }
3350 if (sctp_free_assoc(inp, stcb,
3351 SCTP_PCBFREE_NOFORCE,
3352 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3353 cnt_in_sd++;
3354 }
3355 continue;
3356 } else {
3357 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3358 }
3359 }
3360 cnt_in_sd++;
3361 SCTP_TCB_UNLOCK(stcb);
3362 }
3363 /* now is there some left in our SHUTDOWN state? */
3364 if (cnt_in_sd) {
3365 #ifdef SCTP_LOG_CLOSING
3366 sctp_log_closing(inp, NULL, 2);
3367 #endif
3368 inp->sctp_socket = NULL;
3369 SCTP_INP_WUNLOCK(inp);
3370 SCTP_ASOC_CREATE_UNLOCK(inp);
3371 SCTP_INP_INFO_WUNLOCK();
3372 return;
3373 }
3374 }
3375 inp->sctp_socket = NULL;
3376 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3377 /*
3378 * ok, this guy has been bound. It's port is somewhere in
3379 * the SCTP_BASE_INFO(hash table). Remove it!
3380 */
3381 LIST_REMOVE(inp, sctp_hash);
3382 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3383 }
3384
3385 /*
3386 * If there is a timer running to kill us, forget it, since it may
3387 * have a contest on the INP lock.. which would cause us to die ...
3388 */
3389 cnt = 0;
3390 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
3391 SCTP_TCB_LOCK(stcb);
3392 if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3393 /* Disconnect the socket please */
3394 stcb->sctp_socket = NULL;
3395 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
3396 }
3397 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3398 if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3399 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
3400 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3401 }
3402 cnt++;
3403 SCTP_TCB_UNLOCK(stcb);
3404 continue;
3405 }
3406 /* Free associations that are NOT killing us */
3407 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) &&
3408 ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3409 struct mbuf *op_err;
3410
3411 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3412 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3413 sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3414 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3415 } else if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3416 cnt++;
3417 SCTP_TCB_UNLOCK(stcb);
3418 continue;
3419 }
3420 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3421 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3422 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3423 }
3424 if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
3425 SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3426 cnt++;
3427 }
3428 }
3429 if (cnt) {
3430 /* Ok we have someone out there that will kill us */
3431 #ifdef SCTP_LOG_CLOSING
3432 sctp_log_closing(inp, NULL, 3);
3433 #endif
3434 SCTP_INP_WUNLOCK(inp);
3435 SCTP_ASOC_CREATE_UNLOCK(inp);
3436 SCTP_INP_INFO_WUNLOCK();
3437 return;
3438 }
3439 if (SCTP_INP_LOCK_CONTENDED(inp))
3440 being_refed++;
3441 if (SCTP_INP_READ_CONTENDED(inp))
3442 being_refed++;
3443 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
3444 being_refed++;
3445 /* NOTE: 0 refcount also means no timers are referencing us. */
3446 if ((inp->refcount) ||
3447 (being_refed) ||
3448 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3449 #ifdef SCTP_LOG_CLOSING
3450 sctp_log_closing(inp, NULL, 4);
3451 #endif
3452 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3453 SCTP_INP_WUNLOCK(inp);
3454 SCTP_ASOC_CREATE_UNLOCK(inp);
3455 SCTP_INP_INFO_WUNLOCK();
3456 return;
3457 }
3458 inp->sctp_ep.signature_change.type = 0;
3459 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3460 /*
3461 * Remove it from the list .. last thing we need a lock for.
3462 */
3463 LIST_REMOVE(inp, sctp_list);
3464 SCTP_INP_WUNLOCK(inp);
3465 SCTP_ASOC_CREATE_UNLOCK(inp);
3466 SCTP_INP_INFO_WUNLOCK();
3467
3468 #ifdef SCTP_LOG_CLOSING
3469 sctp_log_closing(inp, NULL, 5);
3470 #endif
3471 if ((inp->sctp_asocidhash) != NULL) {
3472 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
3473 inp->sctp_asocidhash = NULL;
3474 }
3475 /* sa_ignore FREED_MEMORY */
3476 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
3477 /* Its only abandoned if it had data left */
3478 if (sq->length)
3479 SCTP_STAT_INCR(sctps_left_abandon);
3480
3481 TAILQ_REMOVE(&inp->read_queue, sq, next);
3482 sctp_free_remote_addr(sq->whoFrom);
3483 if (so)
3484 SCTP_SB_DECR(&so->so_rcv, sq->length);
3485 if (sq->data) {
3486 sctp_m_freem(sq->data);
3487 sq->data = NULL;
3488 }
3489 /*
3490 * no need to free the net count, since at this point all
3491 * assoc's are gone.
3492 */
3493 sctp_free_a_readq(NULL, sq);
3494 }
3495 /* Now the sctp_pcb things */
3496 /*
3497 * free each asoc if it is not already closed/free. we can't use the
3498 * macro here since le_next will get freed as part of the
3499 * sctp_free_assoc() call.
3500 */
3501 if (ip_pcb->inp_options) {
3502 (void)sctp_m_free(ip_pcb->inp_options);
3503 ip_pcb->inp_options = 0;
3504 }
3505 #ifdef INET6
3506 if (ip_pcb->inp_vflag & INP_IPV6) {
3507 ip6_freepcbopts(ip_pcb->in6p_outputopts);
3508 }
3509 #endif /* INET6 */
3510 ip_pcb->inp_vflag = 0;
3511 /* free up authentication fields */
3512 if (inp->sctp_ep.local_auth_chunks != NULL)
3513 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3514 if (inp->sctp_ep.local_hmacs != NULL)
3515 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3516
3517 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
3518 LIST_REMOVE(shared_key, next);
3519 sctp_free_sharedkey(shared_key);
3520 /* sa_ignore FREED_MEMORY */
3521 }
3522
3523 /*
3524 * if we have an address list the following will free the list of
3525 * ifaddr's that are set into this ep. Again macro limitations here,
3526 * since the LIST_FOREACH could be a bad idea.
3527 */
3528 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3529 sctp_remove_laddr(laddr);
3530 }
3531
3532 #ifdef SCTP_TRACK_FREED_ASOCS
3533 /* TEMP CODE */
3534 LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_free_list, sctp_tcblist, nstcb) {
3535 LIST_REMOVE(stcb, sctp_tcblist);
3536 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
3537 SCTP_DECR_ASOC_COUNT();
3538 }
3539 /* *** END TEMP CODE *** */
3540 #endif
3541 /* Now lets see about freeing the EP hash table. */
3542 if (inp->sctp_tcbhash != NULL) {
3543 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3544 inp->sctp_tcbhash = NULL;
3545 }
3546 /* Now we must put the ep memory back into the zone pool */
3547 crfree(inp->ip_inp.inp.inp_cred);
3548 INP_LOCK_DESTROY(&inp->ip_inp.inp);
3549 SCTP_INP_LOCK_DESTROY(inp);
3550 SCTP_INP_READ_LOCK_DESTROY(inp);
3551 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3552 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3553 SCTP_DECR_EP_COUNT();
3554 }
3555
3556 struct sctp_nets *
sctp_findnet(struct sctp_tcb * stcb,struct sockaddr * addr)3557 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3558 {
3559 struct sctp_nets *net;
3560
3561 /* locate the address */
3562 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3563 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3564 return (net);
3565 }
3566 return (NULL);
3567 }
3568
3569 int
sctp_is_address_on_local_host(struct sockaddr * addr,uint32_t vrf_id)3570 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3571 {
3572 struct sctp_ifa *sctp_ifa;
3573
3574 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3575 if (sctp_ifa) {
3576 return (1);
3577 } else {
3578 return (0);
3579 }
3580 }
3581
3582 /*
3583 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3584 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3585 * stats of stuff.
3586 */
3587 int
sctp_add_remote_addr(struct sctp_tcb * stcb,struct sockaddr * newaddr,struct sctp_nets ** netp,uint16_t port,int set_scope,int from)3588 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3589 struct sctp_nets **netp, uint16_t port, int set_scope, int from)
3590 {
3591 /*
3592 * The following is redundant to the same lines in the
3593 * sctp_aloc_assoc() but is needed since others call the add address
3594 * function
3595 */
3596 struct sctp_nets *net, *netfirst;
3597 int addr_inscope;
3598
3599 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3600 from);
3601 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3602
3603 netfirst = sctp_findnet(stcb, newaddr);
3604 if (netfirst) {
3605 /*
3606 * Lie and return ok, we don't want to make the association
3607 * go away for this behavior. It will happen in the TCP
3608 * model in a connected socket. It does not reach the hash
3609 * table until after the association is built so it can't be
3610 * found. Mark as reachable, since the initial creation will
3611 * have been cleared and the NOT_IN_ASSOC flag will have
3612 * been added... and we don't want to end up removing it
3613 * back out.
3614 */
3615 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3616 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3617 SCTP_ADDR_UNCONFIRMED);
3618 } else {
3619 netfirst->dest_state = SCTP_ADDR_REACHABLE;
3620 }
3621
3622 return (0);
3623 }
3624 addr_inscope = 1;
3625 switch (newaddr->sa_family) {
3626 #ifdef INET
3627 case AF_INET:
3628 {
3629 struct sockaddr_in *sin;
3630
3631 sin = (struct sockaddr_in *)newaddr;
3632 if (sin->sin_addr.s_addr == 0) {
3633 /* Invalid address */
3634 return (-1);
3635 }
3636 /* zero out the zero area */
3637 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3638
3639 /* assure len is set */
3640 sin->sin_len = sizeof(struct sockaddr_in);
3641 if (set_scope) {
3642 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3643 stcb->asoc.scope.ipv4_local_scope = 1;
3644 }
3645 } else {
3646 /* Validate the address is in scope */
3647 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3648 (stcb->asoc.scope.ipv4_local_scope == 0)) {
3649 addr_inscope = 0;
3650 }
3651 }
3652 break;
3653 }
3654 #endif
3655 #ifdef INET6
3656 case AF_INET6:
3657 {
3658 struct sockaddr_in6 *sin6;
3659
3660 sin6 = (struct sockaddr_in6 *)newaddr;
3661 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3662 /* Invalid address */
3663 return (-1);
3664 }
3665 /* assure len is set */
3666 sin6->sin6_len = sizeof(struct sockaddr_in6);
3667 if (set_scope) {
3668 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3669 stcb->asoc.scope.loopback_scope = 1;
3670 stcb->asoc.scope.local_scope = 0;
3671 stcb->asoc.scope.ipv4_local_scope = 1;
3672 stcb->asoc.scope.site_scope = 1;
3673 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3674 /*
3675 * If the new destination is a
3676 * LINK_LOCAL we must have common
3677 * site scope. Don't set the local
3678 * scope since we may not share all
3679 * links, only loopback can do this.
3680 * Links on the local network would
3681 * also be on our private network
3682 * for v4 too.
3683 */
3684 stcb->asoc.scope.ipv4_local_scope = 1;
3685 stcb->asoc.scope.site_scope = 1;
3686 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3687 /*
3688 * If the new destination is
3689 * SITE_LOCAL then we must have site
3690 * scope in common.
3691 */
3692 stcb->asoc.scope.site_scope = 1;
3693 }
3694 } else {
3695 /* Validate the address is in scope */
3696 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3697 (stcb->asoc.scope.loopback_scope == 0)) {
3698 addr_inscope = 0;
3699 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3700 (stcb->asoc.scope.local_scope == 0)) {
3701 addr_inscope = 0;
3702 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3703 (stcb->asoc.scope.site_scope == 0)) {
3704 addr_inscope = 0;
3705 }
3706 }
3707 break;
3708 }
3709 #endif
3710 default:
3711 /* not supported family type */
3712 return (-1);
3713 }
3714 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3715 if (net == NULL) {
3716 return (-1);
3717 }
3718 SCTP_INCR_RADDR_COUNT();
3719 memset(net, 0, sizeof(struct sctp_nets));
3720 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3721 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3722 switch (newaddr->sa_family) {
3723 #ifdef INET
3724 case AF_INET:
3725 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3726 break;
3727 #endif
3728 #ifdef INET6
3729 case AF_INET6:
3730 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3731 break;
3732 #endif
3733 default:
3734 break;
3735 }
3736 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3737 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3738 stcb->asoc.scope.loopback_scope = 1;
3739 stcb->asoc.scope.ipv4_local_scope = 1;
3740 stcb->asoc.scope.local_scope = 0;
3741 stcb->asoc.scope.site_scope = 1;
3742 addr_inscope = 1;
3743 }
3744 net->failure_threshold = stcb->asoc.def_net_failure;
3745 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
3746 if (addr_inscope == 0) {
3747 net->dest_state = (SCTP_ADDR_REACHABLE |
3748 SCTP_ADDR_OUT_OF_SCOPE);
3749 } else {
3750 if (from == SCTP_ADDR_IS_CONFIRMED)
3751 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3752 net->dest_state = SCTP_ADDR_REACHABLE;
3753 else
3754 net->dest_state = SCTP_ADDR_REACHABLE |
3755 SCTP_ADDR_UNCONFIRMED;
3756 }
3757 /*
3758 * We set this to 0, the timer code knows that this means its an
3759 * initial value
3760 */
3761 net->rto_needed = 1;
3762 net->RTO = 0;
3763 net->RTO_measured = 0;
3764 stcb->asoc.numnets++;
3765 net->ref_count = 1;
3766 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
3767 net->port = port;
3768 net->dscp = stcb->asoc.default_dscp;
3769 #ifdef INET6
3770 net->flowlabel = stcb->asoc.default_flowlabel;
3771 #endif
3772 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
3773 net->dest_state |= SCTP_ADDR_NOHB;
3774 } else {
3775 net->dest_state &= ~SCTP_ADDR_NOHB;
3776 }
3777 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
3778 net->dest_state |= SCTP_ADDR_NO_PMTUD;
3779 } else {
3780 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
3781 }
3782 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
3783 /* Init the timer structure */
3784 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3785 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3786 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
3787
3788 /* Now generate a route for this guy */
3789 #ifdef INET6
3790 /* KAME hack: embed scopeid */
3791 if (newaddr->sa_family == AF_INET6) {
3792 struct sockaddr_in6 *sin6;
3793
3794 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3795 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
3796 sin6->sin6_scope_id = 0;
3797 }
3798 #endif
3799 SCTP_RTALLOC((sctp_route_t *)&net->ro,
3800 stcb->asoc.vrf_id,
3801 stcb->sctp_ep->fibnum);
3802
3803 net->src_addr_selected = 0;
3804 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3805 /* Get source address */
3806 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3807 stcb,
3808 (sctp_route_t *)&net->ro,
3809 net,
3810 0,
3811 stcb->asoc.vrf_id);
3812 if (stcb->asoc.default_mtu > 0) {
3813 net->mtu = stcb->asoc.default_mtu;
3814 switch (net->ro._l_addr.sa.sa_family) {
3815 #ifdef INET
3816 case AF_INET:
3817 net->mtu += SCTP_MIN_V4_OVERHEAD;
3818 break;
3819 #endif
3820 #ifdef INET6
3821 case AF_INET6:
3822 net->mtu += SCTP_MIN_OVERHEAD;
3823 break;
3824 #endif
3825 default:
3826 break;
3827 }
3828 #if defined(INET) || defined(INET6)
3829 if (net->port) {
3830 net->mtu += (uint32_t)sizeof(struct udphdr);
3831 }
3832 #endif
3833 } else if (net->ro._s_addr != NULL) {
3834 uint32_t imtu, rmtu, hcmtu;
3835
3836 net->src_addr_selected = 1;
3837 /* Now get the interface MTU */
3838 if (net->ro._s_addr->ifn_p != NULL) {
3839 /*
3840 * XXX: Should we here just use
3841 * net->ro._s_addr->ifn_p->ifn_mtu
3842 */
3843 imtu = SCTP_GATHER_MTU_FROM_IFN_INFO(net->ro._s_addr->ifn_p->ifn_p,
3844 net->ro._s_addr->ifn_p->ifn_index);
3845 } else {
3846 imtu = 0;
3847 }
3848 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh);
3849 hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
3850 net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
3851 }
3852 }
3853 if (net->mtu == 0) {
3854 if (stcb->asoc.default_mtu > 0) {
3855 net->mtu = stcb->asoc.default_mtu;
3856 switch (net->ro._l_addr.sa.sa_family) {
3857 #ifdef INET
3858 case AF_INET:
3859 net->mtu += SCTP_MIN_V4_OVERHEAD;
3860 break;
3861 #endif
3862 #ifdef INET6
3863 case AF_INET6:
3864 net->mtu += SCTP_MIN_OVERHEAD;
3865 break;
3866 #endif
3867 default:
3868 break;
3869 }
3870 #if defined(INET) || defined(INET6)
3871 if (net->port) {
3872 net->mtu += (uint32_t)sizeof(struct udphdr);
3873 }
3874 #endif
3875 } else {
3876 switch (newaddr->sa_family) {
3877 #ifdef INET
3878 case AF_INET:
3879 net->mtu = SCTP_DEFAULT_MTU;
3880 break;
3881 #endif
3882 #ifdef INET6
3883 case AF_INET6:
3884 net->mtu = 1280;
3885 break;
3886 #endif
3887 default:
3888 break;
3889 }
3890 }
3891 }
3892 #if defined(INET) || defined(INET6)
3893 if (net->port) {
3894 net->mtu -= (uint32_t)sizeof(struct udphdr);
3895 }
3896 #endif
3897 if (from == SCTP_ALLOC_ASOC) {
3898 stcb->asoc.smallest_mtu = net->mtu;
3899 }
3900 if (stcb->asoc.smallest_mtu > net->mtu) {
3901 sctp_pathmtu_adjustment(stcb, net->mtu, true);
3902 }
3903 #ifdef INET6
3904 if (newaddr->sa_family == AF_INET6) {
3905 struct sockaddr_in6 *sin6;
3906
3907 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3908 (void)sa6_recoverscope(sin6);
3909 }
3910 #endif
3911
3912 /* JRS - Use the congestion control given in the CC module */
3913 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
3914 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param) (stcb, net);
3915
3916 /*
3917 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3918 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3919 */
3920 net->find_pseudo_cumack = 1;
3921 net->find_rtx_pseudo_cumack = 1;
3922 /* Choose an initial flowid. */
3923 net->flowid = stcb->asoc.my_vtag ^
3924 ntohs(stcb->rport) ^
3925 ntohs(stcb->sctp_ep->sctp_lport);
3926 net->flowtype = M_HASHTYPE_OPAQUE_HASH;
3927 if (netp) {
3928 *netp = net;
3929 }
3930 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3931 if (net->ro.ro_nh == NULL) {
3932 /* Since we have no route put it at the back */
3933 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3934 } else if (netfirst == NULL) {
3935 /* We are the first one in the pool. */
3936 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3937 } else if (netfirst->ro.ro_nh == NULL) {
3938 /*
3939 * First one has NO route. Place this one ahead of the first
3940 * one.
3941 */
3942 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3943 } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
3944 /*
3945 * This one has a different interface than the one at the
3946 * top of the list. Place it ahead.
3947 */
3948 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3949 } else {
3950 /*
3951 * Ok we have the same interface as the first one. Move
3952 * forward until we find either a) one with a NULL route...
3953 * insert ahead of that b) one with a different ifp.. insert
3954 * after that. c) end of the list.. insert at the tail.
3955 */
3956 struct sctp_nets *netlook;
3957
3958 do {
3959 netlook = TAILQ_NEXT(netfirst, sctp_next);
3960 if (netlook == NULL) {
3961 /* End of the list */
3962 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3963 break;
3964 } else if (netlook->ro.ro_nh == NULL) {
3965 /* next one has NO route */
3966 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3967 break;
3968 } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
3969 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3970 net, sctp_next);
3971 break;
3972 }
3973 /* Shift forward */
3974 netfirst = netlook;
3975 } while (netlook != NULL);
3976 }
3977
3978 /* got to have a primary set */
3979 if (stcb->asoc.primary_destination == 0) {
3980 stcb->asoc.primary_destination = net;
3981 } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
3982 (net->ro.ro_nh) &&
3983 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3984 /* No route to current primary adopt new primary */
3985 stcb->asoc.primary_destination = net;
3986 }
3987 /* Validate primary is first */
3988 net = TAILQ_FIRST(&stcb->asoc.nets);
3989 if ((net != stcb->asoc.primary_destination) &&
3990 (stcb->asoc.primary_destination)) {
3991 /*
3992 * first one on the list is NOT the primary sctp_cmpaddr()
3993 * is much more efficient if the primary is the first on the
3994 * list, make it so.
3995 */
3996 TAILQ_REMOVE(&stcb->asoc.nets,
3997 stcb->asoc.primary_destination, sctp_next);
3998 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3999 stcb->asoc.primary_destination, sctp_next);
4000 }
4001 return (0);
4002 }
4003
4004 static uint32_t
sctp_aloc_a_assoc_id(struct sctp_inpcb * inp,struct sctp_tcb * stcb)4005 sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4006 {
4007 uint32_t id;
4008 struct sctpasochead *head;
4009 struct sctp_tcb *lstcb;
4010
4011 try_again:
4012 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4013 /* TSNH */
4014 return (0);
4015 }
4016 /*
4017 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4018 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4019 */
4020 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4021 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4022 }
4023 id = inp->sctp_associd_counter;
4024 inp->sctp_associd_counter++;
4025 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4026 if (lstcb) {
4027 goto try_again;
4028 }
4029 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4030 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4031 stcb->asoc.in_asocid_hash = 1;
4032 return (id);
4033 }
4034
4035 /*
4036 * allocate an association and add it to the endpoint. The caller must be
4037 * careful to add all additional addresses once they are know right away or
4038 * else the assoc will be may experience a blackout scenario.
4039 */
4040 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)4041 sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4042 int *error, uint32_t override_tag, uint32_t initial_tsn,
4043 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
4044 struct thread *p,
4045 int initialize_auth_params)
4046 {
4047 /* note the p argument is only valid in unbound sockets */
4048
4049 struct sctp_tcb *stcb;
4050 struct sctp_association *asoc;
4051 struct sctpasochead *head;
4052 uint16_t rport;
4053 int err;
4054
4055 SCTP_INP_INFO_WLOCK_ASSERT();
4056 SCTP_INP_WLOCK_ASSERT(inp);
4057
4058 /*
4059 * Assumption made here: Caller has done a
4060 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4061 * address does not exist already.
4062 */
4063 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4064 /* Hit max assoc, sorry no more */
4065 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4066 *error = ENOBUFS;
4067 return (NULL);
4068 }
4069 if (firstaddr == NULL) {
4070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4071 *error = EINVAL;
4072 return (NULL);
4073 }
4074 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4075 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4076 *error = EINVAL;
4077 return (NULL);
4078 }
4079 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4080 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4081 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4082 /*
4083 * If its in the TCP pool, its NOT allowed to create an
4084 * association. The parent listener needs to call
4085 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4086 * off, or connected one does this.. its an error.
4087 */
4088 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4089 *error = EINVAL;
4090 return (NULL);
4091 }
4092 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4093 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4094 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4095 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4096 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4097 *error = EINVAL;
4098 return (NULL);
4099 }
4100 }
4101 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4102 #ifdef SCTP_DEBUG
4103 if (firstaddr) {
4104 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4105 switch (firstaddr->sa_family) {
4106 #ifdef INET
4107 case AF_INET:
4108 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4109 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4110 break;
4111 #endif
4112 #ifdef INET6
4113 case AF_INET6:
4114 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4115 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4116 break;
4117 #endif
4118 default:
4119 break;
4120 }
4121 } else {
4122 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
4123 }
4124 #endif /* SCTP_DEBUG */
4125 switch (firstaddr->sa_family) {
4126 #ifdef INET
4127 case AF_INET:
4128 {
4129 struct sockaddr_in *sin;
4130
4131 sin = (struct sockaddr_in *)firstaddr;
4132 if ((ntohs(sin->sin_port) == 0) ||
4133 (sin->sin_addr.s_addr == INADDR_ANY) ||
4134 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
4135 IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) ||
4136 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
4137 (SCTP_IPV6_V6ONLY(inp) != 0))) {
4138 /* Invalid address */
4139 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4140 *error = EINVAL;
4141 return (NULL);
4142 }
4143 rport = sin->sin_port;
4144 break;
4145 }
4146 #endif
4147 #ifdef INET6
4148 case AF_INET6:
4149 {
4150 struct sockaddr_in6 *sin6;
4151
4152 sin6 = (struct sockaddr_in6 *)firstaddr;
4153 if ((ntohs(sin6->sin6_port) == 0) ||
4154 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4155 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) ||
4156 ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) {
4157 /* Invalid address */
4158 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4159 *error = EINVAL;
4160 return (NULL);
4161 }
4162 rport = sin6->sin6_port;
4163 break;
4164 }
4165 #endif
4166 default:
4167 /* not supported family type */
4168 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4169 *error = EINVAL;
4170 return (NULL);
4171 }
4172 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4173 /*
4174 * If you have not performed a bind, then we need to do the
4175 * ephemeral bind for you.
4176 */
4177 if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) {
4178 /* bind error, probably perm */
4179 *error = err;
4180 return (NULL);
4181 }
4182 }
4183 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4184 if (stcb == NULL) {
4185 /* out of memory? */
4186 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4187 *error = ENOMEM;
4188 return (NULL);
4189 }
4190 SCTP_INCR_ASOC_COUNT();
4191
4192 memset(stcb, 0, sizeof(*stcb));
4193 asoc = &stcb->asoc;
4194
4195 SCTP_TCB_LOCK_INIT(stcb);
4196 stcb->rport = rport;
4197 /* setup back pointer's */
4198 stcb->sctp_ep = inp;
4199 stcb->sctp_socket = inp->sctp_socket;
4200 if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) {
4201 /* failed */
4202 SCTP_TCB_LOCK_DESTROY(stcb);
4203 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4204 SCTP_DECR_ASOC_COUNT();
4205 *error = err;
4206 return (NULL);
4207 }
4208 SCTP_TCB_LOCK(stcb);
4209
4210 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4211 /* now that my_vtag is set, add it to the hash */
4212 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4213 /* put it in the bucket in the vtag hash of assoc's for the system */
4214 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4215
4216 if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) {
4217 /* failure.. memory error? */
4218 if (asoc->strmout) {
4219 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4220 asoc->strmout = NULL;
4221 }
4222 if (asoc->mapping_array) {
4223 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4224 asoc->mapping_array = NULL;
4225 }
4226 if (asoc->nr_mapping_array) {
4227 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4228 asoc->nr_mapping_array = NULL;
4229 }
4230 SCTP_DECR_ASOC_COUNT();
4231 SCTP_TCB_UNLOCK(stcb);
4232 SCTP_TCB_LOCK_DESTROY(stcb);
4233 LIST_REMOVE(stcb, sctp_asocs);
4234 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4235 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4236 SCTP_INP_WUNLOCK(inp);
4237 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4238 *error = ENOBUFS;
4239 return (NULL);
4240 }
4241 /* Init all the timers */
4242 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4243 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4244 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4245 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4246 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4247 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4248
4249 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4250 /* now file the port under the hash as well */
4251 if (inp->sctp_tcbhash != NULL) {
4252 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4253 inp->sctp_hashmark)];
4254 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4255 }
4256 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
4257 sctp_initialize_auth_params(inp, stcb);
4258 }
4259 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
4260 return (stcb);
4261 }
4262
4263 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)4264 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4265 int *error, uint32_t override_tag, uint32_t initial_tsn,
4266 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
4267 struct thread *p,
4268 int initialize_auth_params)
4269 {
4270 struct sctp_tcb *stcb;
4271
4272 SCTP_INP_INFO_WLOCK();
4273 SCTP_INP_WLOCK(inp);
4274 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
4275 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
4276 SCTP_INP_INFO_WUNLOCK();
4277 SCTP_INP_WUNLOCK(inp);
4278 return (stcb);
4279 }
4280
4281 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)4282 sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4283 int *error, uint32_t override_tag, uint32_t initial_tsn,
4284 uint32_t vrf_id, uint16_t o_streams, uint16_t port,
4285 struct thread *p,
4286 int initialize_auth_params)
4287 {
4288 struct sctp_tcb *stcb;
4289
4290 SCTP_INP_INFO_WLOCK();
4291 SCTP_INP_WLOCK(inp);
4292 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4293 SCTP_IS_LISTENING(inp)) {
4294 SCTP_INP_INFO_WUNLOCK();
4295 SCTP_INP_WUNLOCK(inp);
4296 *error = EINVAL;
4297 return (NULL);
4298 }
4299 stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
4300 initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
4301 SCTP_INP_INFO_WUNLOCK();
4302 if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4303 inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
4304 soisconnecting(inp->sctp_socket);
4305 }
4306 SCTP_INP_WUNLOCK(inp);
4307 return (stcb);
4308 }
4309
4310 void
sctp_remove_net(struct sctp_tcb * stcb,struct sctp_nets * net)4311 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4312 {
4313 struct sctp_inpcb *inp;
4314 struct sctp_association *asoc;
4315
4316 inp = stcb->sctp_ep;
4317 asoc = &stcb->asoc;
4318 asoc->numnets--;
4319 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4320 if (net == asoc->primary_destination) {
4321 /* Reset primary */
4322 struct sctp_nets *lnet;
4323
4324 lnet = TAILQ_FIRST(&asoc->nets);
4325 /*
4326 * Mobility adaptation Ideally, if deleted destination is
4327 * the primary, it becomes a fast retransmission trigger by
4328 * the subsequent SET PRIMARY. (by micchie)
4329 */
4330 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4331 SCTP_MOBILITY_BASE) ||
4332 sctp_is_mobility_feature_on(stcb->sctp_ep,
4333 SCTP_MOBILITY_FASTHANDOFF)) {
4334 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4335 if (asoc->deleted_primary != NULL) {
4336 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4337 goto out;
4338 }
4339 asoc->deleted_primary = net;
4340 atomic_add_int(&net->ref_count, 1);
4341 memset(&net->lastsa, 0, sizeof(net->lastsa));
4342 memset(&net->lastsv, 0, sizeof(net->lastsv));
4343 sctp_mobility_feature_on(stcb->sctp_ep,
4344 SCTP_MOBILITY_PRIM_DELETED);
4345 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4346 stcb->sctp_ep, stcb, NULL);
4347 }
4348 out:
4349 /* Try to find a confirmed primary */
4350 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4351 }
4352 if (net == asoc->last_data_chunk_from) {
4353 /* Reset primary */
4354 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4355 }
4356 if (net == asoc->last_control_chunk_from) {
4357 /* Clear net */
4358 asoc->last_control_chunk_from = NULL;
4359 }
4360 if (net == asoc->last_net_cmt_send_started) {
4361 /* Clear net */
4362 asoc->last_net_cmt_send_started = NULL;
4363 }
4364 if (net == stcb->asoc.alternate) {
4365 sctp_free_remote_addr(stcb->asoc.alternate);
4366 stcb->asoc.alternate = NULL;
4367 }
4368 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
4369 SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
4370 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
4371 SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
4372 net->dest_state |= SCTP_ADDR_BEING_DELETED;
4373 sctp_free_remote_addr(net);
4374 }
4375
4376 /*
4377 * remove a remote endpoint address from an association, it will fail if the
4378 * address does not exist.
4379 */
4380 int
sctp_del_remote_addr(struct sctp_tcb * stcb,struct sockaddr * remaddr)4381 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4382 {
4383 /*
4384 * Here we need to remove a remote address. This is quite simple, we
4385 * first find it in the list of address for the association
4386 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4387 * on that item. Note we do not allow it to be removed if there are
4388 * no other addresses.
4389 */
4390 struct sctp_association *asoc;
4391 struct sctp_nets *net, *nnet;
4392
4393 asoc = &stcb->asoc;
4394
4395 /* locate the address */
4396 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
4397 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4398 continue;
4399 }
4400 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4401 remaddr)) {
4402 /* we found the guy */
4403 if (asoc->numnets < 2) {
4404 /* Must have at LEAST two remote addresses */
4405 return (-1);
4406 } else {
4407 sctp_remove_net(stcb, net);
4408 return (0);
4409 }
4410 }
4411 }
4412 /* not found. */
4413 return (-2);
4414 }
4415
4416 static bool
sctp_is_in_timewait(uint32_t tag,uint16_t lport,uint16_t rport,time_t now)4417 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, time_t now)
4418 {
4419 struct sctpvtaghead *chain;
4420 struct sctp_tagblock *twait_block;
4421 int i;
4422
4423 SCTP_INP_INFO_LOCK_ASSERT();
4424 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4425 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4426 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4427 if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) &&
4428 (twait_block->vtag_block[i].v_tag == tag) &&
4429 (twait_block->vtag_block[i].lport == lport) &&
4430 (twait_block->vtag_block[i].rport == rport)) {
4431 return (true);
4432 }
4433 }
4434 }
4435 return (false);
4436 }
4437
4438 static void
sctp_set_vtag_block(struct sctp_timewait * vtag_block,time_t time,uint32_t tag,uint16_t lport,uint16_t rport)4439 sctp_set_vtag_block(struct sctp_timewait *vtag_block, time_t time,
4440 uint32_t tag, uint16_t lport, uint16_t rport)
4441 {
4442 vtag_block->tv_sec_at_expire = time;
4443 vtag_block->v_tag = tag;
4444 vtag_block->lport = lport;
4445 vtag_block->rport = rport;
4446 }
4447
4448 static void
sctp_add_vtag_to_timewait(uint32_t tag,uint16_t lport,uint16_t rport)4449 sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4450 {
4451 struct sctpvtaghead *chain;
4452 struct sctp_tagblock *twait_block;
4453 struct timeval now;
4454 time_t time;
4455 int i;
4456 bool set;
4457
4458 SCTP_INP_INFO_WLOCK_ASSERT();
4459 (void)SCTP_GETTIME_TIMEVAL(&now);
4460 time = now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait);
4461 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4462 set = false;
4463 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4464 /* Block(s) present, lets find space, and expire on the fly */
4465 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4466 if ((twait_block->vtag_block[i].v_tag == 0) && !set) {
4467 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
4468 set = true;
4469 continue;
4470 }
4471 if ((twait_block->vtag_block[i].v_tag != 0) &&
4472 (twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4473 if (set) {
4474 /* Audit expires this guy */
4475 sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0);
4476 } else {
4477 /* Reuse it for the new tag */
4478 sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
4479 set = true;
4480 }
4481 }
4482 }
4483 if (set) {
4484 /*
4485 * We only do up to the block where we can place our
4486 * tag for audits
4487 */
4488 break;
4489 }
4490 }
4491 /* Need to add a new block to chain */
4492 if (!set) {
4493 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4494 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4495 if (twait_block == NULL) {
4496 return;
4497 }
4498 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4499 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4500 sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport);
4501 }
4502 }
4503
4504 void
sctp_clean_up_stream(struct sctp_tcb * stcb,struct sctp_readhead * rh)4505 sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
4506 {
4507 struct sctp_tmit_chunk *chk, *nchk;
4508 struct sctp_queued_to_read *control, *ncontrol;
4509
4510 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
4511 TAILQ_REMOVE(rh, control, next_instrm);
4512 control->on_strm_q = 0;
4513 if (control->on_read_q == 0) {
4514 sctp_free_remote_addr(control->whoFrom);
4515 if (control->data) {
4516 sctp_m_freem(control->data);
4517 control->data = NULL;
4518 }
4519 }
4520 /* Reassembly free? */
4521 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
4522 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
4523 if (chk->data) {
4524 sctp_m_freem(chk->data);
4525 chk->data = NULL;
4526 }
4527 if (chk->holds_key_ref)
4528 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
4529 sctp_free_remote_addr(chk->whoTo);
4530 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4531 SCTP_DECR_CHK_COUNT();
4532 /* sa_ignore FREED_MEMORY */
4533 }
4534 /*
4535 * We don't free the address here since all the net's were
4536 * freed above.
4537 */
4538 if (control->on_read_q == 0) {
4539 sctp_free_a_readq(stcb, control);
4540 }
4541 }
4542 }
4543
4544 /*-
4545 * Free the association after un-hashing the remote port. This
4546 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4547 * expect that the input to this function IS a locked TCB.
4548 * It will return 0, if it did NOT destroy the association (instead
4549 * it unlocks it. It will return NON-zero if it either destroyed the
4550 * association OR the association is already destroyed.
4551 */
4552 int
sctp_free_assoc(struct sctp_inpcb * inp,struct sctp_tcb * stcb,int from_inpcbfree,int from_location)4553 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4554 {
4555 int i;
4556 struct sctp_association *asoc;
4557 struct sctp_nets *net, *nnet;
4558 struct sctp_laddr *laddr, *naddr;
4559 struct sctp_tmit_chunk *chk, *nchk;
4560 struct sctp_asconf_addr *aparam, *naparam;
4561 struct sctp_asconf_ack *aack, *naack;
4562 struct sctp_stream_reset_list *strrst, *nstrrst;
4563 struct sctp_queued_to_read *sq, *nsq;
4564 struct sctp_stream_queue_pending *sp, *nsp;
4565 sctp_sharedkey_t *shared_key, *nshared_key;
4566 struct socket *so;
4567
4568 /* first, lets purge the entry from the hash table. */
4569 SCTP_TCB_LOCK_ASSERT(stcb);
4570
4571 #ifdef SCTP_LOG_CLOSING
4572 sctp_log_closing(inp, stcb, 6);
4573 #endif
4574 if (stcb->asoc.state == 0) {
4575 #ifdef SCTP_LOG_CLOSING
4576 sctp_log_closing(inp, NULL, 7);
4577 #endif
4578 /* there is no asoc, really TSNH :-0 */
4579 return (1);
4580 }
4581 if (stcb->asoc.alternate) {
4582 sctp_free_remote_addr(stcb->asoc.alternate);
4583 stcb->asoc.alternate = NULL;
4584 }
4585 /* TEMP CODE */
4586 if (stcb->freed_from_where == 0) {
4587 /* Only record the first place free happened from */
4588 stcb->freed_from_where = from_location;
4589 }
4590 /* TEMP CODE */
4591
4592 asoc = &stcb->asoc;
4593 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4594 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4595 /* nothing around */
4596 so = NULL;
4597 else
4598 so = inp->sctp_socket;
4599
4600 /*
4601 * We used timer based freeing if a reader or writer is in the way.
4602 * So we first check if we are actually being called from a timer,
4603 * if so we abort early if a reader or writer is still in the way.
4604 */
4605 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4606 (from_inpcbfree == SCTP_NORMAL_PROC)) {
4607 /*
4608 * is it the timer driving us? if so are the reader/writers
4609 * gone?
4610 */
4611 if (stcb->asoc.refcnt) {
4612 /* nope, reader or writer in the way */
4613 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4614 /* no asoc destroyed */
4615 SCTP_TCB_UNLOCK(stcb);
4616 #ifdef SCTP_LOG_CLOSING
4617 sctp_log_closing(inp, stcb, 8);
4618 #endif
4619 return (0);
4620 }
4621 }
4622 /* Now clean up any other timers */
4623 sctp_stop_association_timers(stcb, false);
4624 /* Now the read queue needs to be cleaned up (only once) */
4625 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4626 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
4627 SCTP_INP_READ_LOCK(inp);
4628 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4629 if (sq->stcb == stcb) {
4630 sq->do_not_ref_stcb = 1;
4631 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4632 /*
4633 * If there is no end, there never will be
4634 * now.
4635 */
4636 if (sq->end_added == 0) {
4637 /* Held for PD-API, clear that. */
4638 sq->pdapi_aborted = 1;
4639 sq->held_length = 0;
4640 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4641 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
4642 stcb,
4643 SCTP_PARTIAL_DELIVERY_ABORTED,
4644 (void *)sq,
4645 SCTP_SO_LOCKED);
4646 }
4647 /* Add an end to wake them */
4648 sq->end_added = 1;
4649 }
4650 }
4651 }
4652 SCTP_INP_READ_UNLOCK(inp);
4653 if (stcb->block_entry) {
4654 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4655 stcb->block_entry->error = ECONNRESET;
4656 stcb->block_entry = NULL;
4657 }
4658 }
4659 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
4660 /*
4661 * Someone holds a reference OR the socket is unaccepted
4662 * yet.
4663 */
4664 if ((stcb->asoc.refcnt) ||
4665 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4666 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4667 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
4668 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4669 }
4670 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4671 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4672 /* nothing around */
4673 so = NULL;
4674 if (so) {
4675 /* Wake any reader/writers */
4676 sctp_sorwakeup(inp, so);
4677 sctp_sowwakeup(inp, so);
4678 }
4679 SCTP_TCB_UNLOCK(stcb);
4680
4681 #ifdef SCTP_LOG_CLOSING
4682 sctp_log_closing(inp, stcb, 9);
4683 #endif
4684 /* no asoc destroyed */
4685 return (0);
4686 }
4687 #ifdef SCTP_LOG_CLOSING
4688 sctp_log_closing(inp, stcb, 10);
4689 #endif
4690 /*
4691 * When I reach here, no others want to kill the assoc yet.. and I
4692 * own the lock. Now its possible an abort comes in when I do the
4693 * lock exchange below to grab all the locks to do the final take
4694 * out. to prevent this we increment the count, which will start a
4695 * timer and blow out above thus assuring us that we hold exclusive
4696 * killing of the asoc. Note that after getting back the TCB lock we
4697 * will go ahead and increment the counter back up and stop any
4698 * timer a passing stranger may have started :-S
4699 */
4700 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4701 atomic_add_int(&stcb->asoc.refcnt, 1);
4702
4703 SCTP_TCB_UNLOCK(stcb);
4704 SCTP_INP_INFO_WLOCK();
4705 SCTP_INP_WLOCK(inp);
4706 SCTP_TCB_LOCK(stcb);
4707 }
4708 /* Double check the GONE flag */
4709 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4710 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4711 /* nothing around */
4712 so = NULL;
4713
4714 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4715 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4716 /*
4717 * For TCP type we need special handling when we are
4718 * connected. We also include the peel'ed off ones to.
4719 */
4720 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4721 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4722 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4723 if (so) {
4724 SOCKBUF_LOCK(&so->so_rcv);
4725 so->so_state &= ~(SS_ISCONNECTING |
4726 SS_ISDISCONNECTING |
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_ZERO_CHECKSUM_ACCEPTABLE) {
6269 struct sctp_zero_checksum_acceptable zero_chksum,
6270 *zero_chksum_p;
6271
6272 phdr = sctp_get_next_param(m, offset,
6273 (struct sctp_paramhdr *)&zero_chksum,
6274 sizeof(struct sctp_zero_checksum_acceptable));
6275 if (phdr != NULL) {
6276 /*
6277 * Only send zero checksums if the upper
6278 * layer has enabled the support for the
6279 * same method as allowed by the peer.
6280 */
6281 zero_chksum_p = (struct sctp_zero_checksum_acceptable *)phdr;
6282 if ((ntohl(zero_chksum_p->edmid) != SCTP_EDMID_NONE) &&
6283 (ntohl(zero_chksum_p->edmid) == stcb->asoc.rcv_edmid)) {
6284 stcb->asoc.snd_edmid = stcb->asoc.rcv_edmid;
6285 }
6286 }
6287 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
6288 /* A supported extension chunk */
6289 struct sctp_supported_chunk_types_param *pr_supported;
6290 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
6291 int num_ent, i;
6292
6293 if (plen > sizeof(local_store)) {
6294 return (-35);
6295 }
6296 phdr = sctp_get_next_param(m, offset,
6297 (struct sctp_paramhdr *)&local_store, plen);
6298 if (phdr == NULL) {
6299 return (-25);
6300 }
6301 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
6302 num_ent = plen - sizeof(struct sctp_paramhdr);
6303 for (i = 0; i < num_ent; i++) {
6304 switch (pr_supported->chunk_types[i]) {
6305 case SCTP_ASCONF:
6306 peer_supports_asconf = 1;
6307 break;
6308 case SCTP_ASCONF_ACK:
6309 peer_supports_asconf_ack = 1;
6310 break;
6311 case SCTP_FORWARD_CUM_TSN:
6312 peer_supports_prsctp = 1;
6313 break;
6314 case SCTP_PACKET_DROPPED:
6315 peer_supports_pktdrop = 1;
6316 break;
6317 case SCTP_NR_SELECTIVE_ACK:
6318 peer_supports_nrsack = 1;
6319 break;
6320 case SCTP_STREAM_RESET:
6321 peer_supports_reconfig = 1;
6322 break;
6323 case SCTP_AUTHENTICATION:
6324 peer_supports_auth = 1;
6325 break;
6326 case SCTP_IDATA:
6327 peer_supports_idata = 1;
6328 break;
6329 default:
6330 /* one I have not learned yet */
6331 break;
6332 }
6333 }
6334 } else if (ptype == SCTP_RANDOM) {
6335 if (plen > sizeof(random_store))
6336 break;
6337 if (got_random) {
6338 /* already processed a RANDOM */
6339 goto next_param;
6340 }
6341 phdr = sctp_get_next_param(m, offset,
6342 (struct sctp_paramhdr *)random_store,
6343 plen);
6344 if (phdr == NULL)
6345 return (-26);
6346 p_random = (struct sctp_auth_random *)phdr;
6347 random_len = plen - sizeof(*p_random);
6348 /* enforce the random length */
6349 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
6350 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
6351 return (-27);
6352 }
6353 got_random = 1;
6354 } else if (ptype == SCTP_HMAC_LIST) {
6355 uint16_t num_hmacs;
6356 uint16_t i;
6357
6358 if (plen > sizeof(hmacs_store))
6359 break;
6360 if (got_hmacs) {
6361 /* already processed a HMAC list */
6362 goto next_param;
6363 }
6364 phdr = sctp_get_next_param(m, offset,
6365 (struct sctp_paramhdr *)hmacs_store,
6366 plen);
6367 if (phdr == NULL)
6368 return (-28);
6369 hmacs = (struct sctp_auth_hmac_algo *)phdr;
6370 hmacs_len = plen - sizeof(*hmacs);
6371 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
6372 /* validate the hmac list */
6373 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
6374 return (-29);
6375 }
6376 if (stcb->asoc.peer_hmacs != NULL)
6377 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
6378 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
6379 if (stcb->asoc.peer_hmacs != NULL) {
6380 for (i = 0; i < num_hmacs; i++) {
6381 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6382 ntohs(hmacs->hmac_ids[i]));
6383 }
6384 }
6385 got_hmacs = 1;
6386 } else if (ptype == SCTP_CHUNK_LIST) {
6387 int i;
6388
6389 if (plen > sizeof(chunks_store))
6390 break;
6391 if (got_chklist) {
6392 /* already processed a Chunks list */
6393 goto next_param;
6394 }
6395 phdr = sctp_get_next_param(m, offset,
6396 (struct sctp_paramhdr *)chunks_store,
6397 plen);
6398 if (phdr == NULL)
6399 return (-30);
6400 chunks = (struct sctp_auth_chunk_list *)phdr;
6401 num_chunks = plen - sizeof(*chunks);
6402 if (stcb->asoc.peer_auth_chunks != NULL)
6403 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6404 else
6405 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6406 for (i = 0; i < num_chunks; i++) {
6407 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
6408 stcb->asoc.peer_auth_chunks);
6409 /* record asconf/asconf-ack if listed */
6410 if (chunks->chunk_types[i] == SCTP_ASCONF)
6411 saw_asconf = 1;
6412 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6413 saw_asconf_ack = 1;
6414 }
6415 got_chklist = 1;
6416 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6417 (ptype == SCTP_STATE_COOKIE) ||
6418 (ptype == SCTP_UNRECOG_PARAM) ||
6419 (ptype == SCTP_COOKIE_PRESERVE) ||
6420 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6421 (ptype == SCTP_ADD_IP_ADDRESS) ||
6422 (ptype == SCTP_DEL_IP_ADDRESS) ||
6423 (ptype == SCTP_ERROR_CAUSE_IND) ||
6424 (ptype == SCTP_SUCCESS_REPORT)) {
6425 /* don't care */
6426 } else {
6427 if ((ptype & 0x8000) == 0x0000) {
6428 /*
6429 * must stop processing the rest of the
6430 * param's. Any report bits were handled
6431 * with the call to
6432 * sctp_arethere_unrecognized_parameters()
6433 * when the INIT or INIT-ACK was first seen.
6434 */
6435 break;
6436 }
6437 }
6438
6439 next_param:
6440 offset += SCTP_SIZE32(plen);
6441 if (offset >= limit) {
6442 break;
6443 }
6444 phdr = sctp_get_next_param(m, offset, ¶m_buf,
6445 sizeof(param_buf));
6446 }
6447 /* Now check to see if we need to purge any addresses */
6448 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
6449 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6450 SCTP_ADDR_NOT_IN_ASSOC) {
6451 /* This address has been removed from the asoc */
6452 /* remove and free it */
6453 stcb->asoc.numnets--;
6454 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6455 if (net == stcb->asoc.alternate) {
6456 sctp_free_remote_addr(stcb->asoc.alternate);
6457 stcb->asoc.alternate = NULL;
6458 }
6459 if (net == stcb->asoc.primary_destination) {
6460 stcb->asoc.primary_destination = NULL;
6461 sctp_select_primary_destination(stcb);
6462 }
6463 sctp_free_remote_addr(net);
6464 }
6465 }
6466 if ((stcb->asoc.ecn_supported == 1) &&
6467 (peer_supports_ecn == 0)) {
6468 stcb->asoc.ecn_supported = 0;
6469 }
6470 if ((stcb->asoc.prsctp_supported == 1) &&
6471 (peer_supports_prsctp == 0)) {
6472 stcb->asoc.prsctp_supported = 0;
6473 }
6474 if ((stcb->asoc.auth_supported == 1) &&
6475 ((peer_supports_auth == 0) ||
6476 (got_random == 0) || (got_hmacs == 0))) {
6477 stcb->asoc.auth_supported = 0;
6478 }
6479 if ((stcb->asoc.asconf_supported == 1) &&
6480 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
6481 (stcb->asoc.auth_supported == 0) ||
6482 (saw_asconf == 0) || (saw_asconf_ack == 0))) {
6483 stcb->asoc.asconf_supported = 0;
6484 }
6485 if ((stcb->asoc.reconfig_supported == 1) &&
6486 (peer_supports_reconfig == 0)) {
6487 stcb->asoc.reconfig_supported = 0;
6488 }
6489 if ((stcb->asoc.idata_supported == 1) &&
6490 (peer_supports_idata == 0)) {
6491 stcb->asoc.idata_supported = 0;
6492 }
6493 if ((stcb->asoc.nrsack_supported == 1) &&
6494 (peer_supports_nrsack == 0)) {
6495 stcb->asoc.nrsack_supported = 0;
6496 }
6497 if ((stcb->asoc.pktdrop_supported == 1) &&
6498 (peer_supports_pktdrop == 0)) {
6499 stcb->asoc.pktdrop_supported = 0;
6500 }
6501 /* validate authentication required parameters */
6502 if ((peer_supports_auth == 0) && (got_chklist == 1)) {
6503 /* peer does not support auth but sent a chunks list? */
6504 return (-31);
6505 }
6506 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
6507 /* peer supports asconf but not auth? */
6508 return (-32);
6509 } else if ((peer_supports_asconf == 1) &&
6510 (peer_supports_auth == 1) &&
6511 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6512 return (-33);
6513 }
6514 /* concatenate the full random key */
6515 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
6516 if (chunks != NULL) {
6517 keylen += sizeof(*chunks) + num_chunks;
6518 }
6519 new_key = sctp_alloc_key(keylen);
6520 if (new_key != NULL) {
6521 /* copy in the RANDOM */
6522 if (p_random != NULL) {
6523 keylen = sizeof(*p_random) + random_len;
6524 memcpy(new_key->key, p_random, keylen);
6525 } else {
6526 keylen = 0;
6527 }
6528 /* append in the AUTH chunks */
6529 if (chunks != NULL) {
6530 memcpy(new_key->key + keylen, chunks,
6531 sizeof(*chunks) + num_chunks);
6532 keylen += sizeof(*chunks) + num_chunks;
6533 }
6534 /* append in the HMACs */
6535 if (hmacs != NULL) {
6536 memcpy(new_key->key + keylen, hmacs,
6537 sizeof(*hmacs) + hmacs_len);
6538 }
6539 } else {
6540 /* failed to get memory for the key */
6541 return (-34);
6542 }
6543 if (stcb->asoc.authinfo.peer_random != NULL)
6544 sctp_free_key(stcb->asoc.authinfo.peer_random);
6545 stcb->asoc.authinfo.peer_random = new_key;
6546 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6547 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6548
6549 return (0);
6550 }
6551
6552 int
sctp_set_primary_addr(struct sctp_tcb * stcb,struct sockaddr * sa,struct sctp_nets * net)6553 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6554 struct sctp_nets *net)
6555 {
6556 /* make sure the requested primary address exists in the assoc */
6557 if (net == NULL && sa)
6558 net = sctp_findnet(stcb, sa);
6559
6560 if (net == NULL) {
6561 /* didn't find the requested primary address! */
6562 return (-1);
6563 } else {
6564 /* set the primary address */
6565 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6566 /* Must be confirmed, so queue to set */
6567 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6568 return (0);
6569 }
6570 stcb->asoc.primary_destination = net;
6571 if (((net->dest_state & SCTP_ADDR_PF) == 0) &&
6572 (stcb->asoc.alternate != NULL)) {
6573 sctp_free_remote_addr(stcb->asoc.alternate);
6574 stcb->asoc.alternate = NULL;
6575 }
6576 net = TAILQ_FIRST(&stcb->asoc.nets);
6577 if (net != stcb->asoc.primary_destination) {
6578 /*
6579 * first one on the list is NOT the primary
6580 * sctp_cmpaddr() is much more efficient if the
6581 * primary is the first on the list, make it so.
6582 */
6583 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6584 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6585 }
6586 return (0);
6587 }
6588 }
6589
6590 bool
sctp_is_vtag_good(uint32_t tag,uint16_t lport,uint16_t rport,struct timeval * now)6591 sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
6592 {
6593 struct sctpasochead *head;
6594 struct sctp_tcb *stcb;
6595
6596 SCTP_INP_INFO_LOCK_ASSERT();
6597
6598 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))];
6599 LIST_FOREACH(stcb, head, sctp_asocs) {
6600 /*
6601 * We choose not to lock anything here. TCB's can't be
6602 * removed since we have the read lock, so they can't be
6603 * freed on us, same thing for the INP. I may be wrong with
6604 * this assumption, but we will go with it for now :-)
6605 */
6606 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
6607 continue;
6608 }
6609 if (stcb->asoc.my_vtag == tag) {
6610 /* candidate */
6611 if (stcb->rport != rport) {
6612 continue;
6613 }
6614 if (stcb->sctp_ep->sctp_lport != lport) {
6615 continue;
6616 }
6617 /* The tag is currently used, so don't use it. */
6618 return (false);
6619 }
6620 }
6621 return (!sctp_is_in_timewait(tag, lport, rport, now->tv_sec));
6622 }
6623
6624 static void
sctp_drain_mbufs(struct sctp_tcb * stcb)6625 sctp_drain_mbufs(struct sctp_tcb *stcb)
6626 {
6627 /*
6628 * We must hunt this association for MBUF's past the cumack (i.e.
6629 * out of order data that we can renege on).
6630 */
6631 struct sctp_association *asoc;
6632 struct sctp_tmit_chunk *chk, *nchk;
6633 uint32_t cumulative_tsn_p1;
6634 struct sctp_queued_to_read *control, *ncontrol;
6635 int cnt, strmat;
6636 uint32_t gap, i;
6637 int fnd = 0;
6638
6639 /* We look for anything larger than the cum-ack + 1 */
6640
6641 asoc = &stcb->asoc;
6642 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6643 /* none we can reneg on. */
6644 return;
6645 }
6646 SCTP_STAT_INCR(sctps_protocol_drains_done);
6647 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6648 cnt = 0;
6649 /* Ok that was fun, now we will drain all the inbound streams? */
6650 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6651 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
6652 #ifdef INVARIANTS
6653 if (control->on_strm_q != SCTP_ON_ORDERED) {
6654 panic("Huh control: %p on_q: %d -- not ordered?",
6655 control, control->on_strm_q);
6656 }
6657 #endif
6658 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
6659 /* Yep it is above cum-ack */
6660 cnt++;
6661 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
6662 KASSERT(control->length > 0, ("control has zero length"));
6663 if (asoc->size_on_all_streams >= control->length) {
6664 asoc->size_on_all_streams -= control->length;
6665 } else {
6666 #ifdef INVARIANTS
6667 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
6668 #else
6669 asoc->size_on_all_streams = 0;
6670 #endif
6671 }
6672 sctp_ucount_decr(asoc->cnt_on_all_streams);
6673 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6674 if (control->on_read_q) {
6675 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
6676 control->on_read_q = 0;
6677 }
6678 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
6679 control->on_strm_q = 0;
6680 if (control->data) {
6681 sctp_m_freem(control->data);
6682 control->data = NULL;
6683 }
6684 sctp_free_remote_addr(control->whoFrom);
6685 /* Now its reasm? */
6686 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
6687 cnt++;
6688 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
6689 KASSERT(chk->send_size > 0, ("chunk has zero length"));
6690 if (asoc->size_on_reasm_queue >= chk->send_size) {
6691 asoc->size_on_reasm_queue -= chk->send_size;
6692 } else {
6693 #ifdef INVARIANTS
6694 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
6695 #else
6696 asoc->size_on_reasm_queue = 0;
6697 #endif
6698 }
6699 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6700 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6701 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
6702 if (chk->data) {
6703 sctp_m_freem(chk->data);
6704 chk->data = NULL;
6705 }
6706 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6707 }
6708 sctp_free_a_readq(stcb, control);
6709 }
6710 }
6711 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
6712 #ifdef INVARIANTS
6713 if (control->on_strm_q != SCTP_ON_UNORDERED) {
6714 panic("Huh control: %p on_q: %d -- not unordered?",
6715 control, control->on_strm_q);
6716 }
6717 #endif
6718 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
6719 /* Yep it is above cum-ack */
6720 cnt++;
6721 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
6722 KASSERT(control->length > 0, ("control has zero length"));
6723 if (asoc->size_on_all_streams >= control->length) {
6724 asoc->size_on_all_streams -= control->length;
6725 } else {
6726 #ifdef INVARIANTS
6727 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
6728 #else
6729 asoc->size_on_all_streams = 0;
6730 #endif
6731 }
6732 sctp_ucount_decr(asoc->cnt_on_all_streams);
6733 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6734 if (control->on_read_q) {
6735 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
6736 control->on_read_q = 0;
6737 }
6738 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
6739 control->on_strm_q = 0;
6740 if (control->data) {
6741 sctp_m_freem(control->data);
6742 control->data = NULL;
6743 }
6744 sctp_free_remote_addr(control->whoFrom);
6745 /* Now its reasm? */
6746 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
6747 cnt++;
6748 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
6749 KASSERT(chk->send_size > 0, ("chunk has zero length"));
6750 if (asoc->size_on_reasm_queue >= chk->send_size) {
6751 asoc->size_on_reasm_queue -= chk->send_size;
6752 } else {
6753 #ifdef INVARIANTS
6754 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
6755 #else
6756 asoc->size_on_reasm_queue = 0;
6757 #endif
6758 }
6759 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6760 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6761 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
6762 if (chk->data) {
6763 sctp_m_freem(chk->data);
6764 chk->data = NULL;
6765 }
6766 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6767 }
6768 sctp_free_a_readq(stcb, control);
6769 }
6770 }
6771 }
6772 if (cnt) {
6773 /* We must back down to see what the new highest is */
6774 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
6775 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
6776 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6777 asoc->highest_tsn_inside_map = i;
6778 fnd = 1;
6779 break;
6780 }
6781 }
6782 if (!fnd) {
6783 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
6784 }
6785
6786 /*
6787 * Question, should we go through the delivery queue? The
6788 * only reason things are on here is the app not reading OR
6789 * a p-d-api up. An attacker COULD send enough in to
6790 * initiate the PD-API and then send a bunch of stuff to
6791 * other streams... these would wind up on the delivery
6792 * queue.. and then we would not get to them. But in order
6793 * to do this I then have to back-track and un-deliver
6794 * sequence numbers in streams.. el-yucko. I think for now
6795 * we will NOT look at the delivery queue and leave it to be
6796 * something to consider later. An alternative would be to
6797 * abort the P-D-API with a notification and then deliver
6798 * the data.... Or another method might be to keep track of
6799 * how many times the situation occurs and if we see a
6800 * possible attack underway just abort the association.
6801 */
6802 #ifdef SCTP_DEBUG
6803 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6804 #endif
6805 /*
6806 * Now do we need to find a new
6807 * asoc->highest_tsn_inside_map?
6808 */
6809 asoc->last_revoke_count = cnt;
6810 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
6811 SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
6812 /* sa_ignore NO_NULL_CHK */
6813 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
6814 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6815 }
6816 /*
6817 * Another issue, in un-setting the TSN's in the mapping array we
6818 * DID NOT adjust the highest_tsn marker. This will cause one of
6819 * two things to occur. It may cause us to do extra work in checking
6820 * for our mapping array movement. More importantly it may cause us
6821 * to SACK every datagram. This may not be a bad thing though since
6822 * we will recover once we get our cum-ack above and all this stuff
6823 * we dumped recovered.
6824 */
6825 }
6826
6827 static void
sctp_drain(void)6828 sctp_drain(void)
6829 {
6830 struct epoch_tracker et;
6831
6832 VNET_ITERATOR_DECL(vnet_iter);
6833
6834 NET_EPOCH_ENTER(et);
6835 /*
6836 * We must walk the PCB lists for ALL associations here. The system
6837 * is LOW on MBUF's and needs help. This is where reneging will
6838 * occur. We really hope this does NOT happen!
6839 */
6840 VNET_LIST_RLOCK_NOSLEEP();
6841 VNET_FOREACH(vnet_iter) {
6842 CURVNET_SET(vnet_iter);
6843 struct sctp_inpcb *inp;
6844 struct sctp_tcb *stcb;
6845
6846 SCTP_STAT_INCR(sctps_protocol_drain_calls);
6847 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
6848 #ifdef VIMAGE
6849 continue;
6850 #else
6851 NET_EPOCH_EXIT(et);
6852 return;
6853 #endif
6854 }
6855 SCTP_INP_INFO_RLOCK();
6856 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
6857 /* For each endpoint */
6858 SCTP_INP_RLOCK(inp);
6859 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6860 /* For each association */
6861 SCTP_TCB_LOCK(stcb);
6862 sctp_drain_mbufs(stcb);
6863 SCTP_TCB_UNLOCK(stcb);
6864 }
6865 SCTP_INP_RUNLOCK(inp);
6866 }
6867 SCTP_INP_INFO_RUNLOCK();
6868 CURVNET_RESTORE();
6869 }
6870 VNET_LIST_RUNLOCK_NOSLEEP();
6871 NET_EPOCH_EXIT(et);
6872 }
6873
6874 EVENTHANDLER_DEFINE(vm_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT);
6875 EVENTHANDLER_DEFINE(mbuf_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT);
6876
6877 /*
6878 * start a new iterator
6879 * iterates through all endpoints and associations based on the pcb_state
6880 * flags and asoc_state. "af" (mandatory) is executed for all matching
6881 * assocs and "ef" (optional) is executed when the iterator completes.
6882 * "inpf" (optional) is executed for each new endpoint as it is being
6883 * iterated through. inpe (optional) is called when the inp completes
6884 * its way through all the stcbs.
6885 */
6886 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)6887 sctp_initiate_iterator(inp_func inpf,
6888 asoc_func af,
6889 inp_func inpe,
6890 uint32_t pcb_state,
6891 uint32_t pcb_features,
6892 uint32_t asoc_state,
6893 void *argp,
6894 uint32_t argi,
6895 end_func ef,
6896 struct sctp_inpcb *s_inp,
6897 uint8_t chunk_output_off)
6898 {
6899 struct sctp_iterator *it = NULL;
6900
6901 if (af == NULL) {
6902 return (-1);
6903 }
6904 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6905 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
6906 SCTP_BASE_VAR(sctp_pcb_initialized));
6907 return (-1);
6908 }
6909 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
6910 SCTP_M_ITER);
6911 if (it == NULL) {
6912 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
6913 return (-1);
6914 }
6915 memset(it, 0, sizeof(*it));
6916 it->function_assoc = af;
6917 it->function_inp = inpf;
6918 if (inpf)
6919 it->done_current_ep = 0;
6920 else
6921 it->done_current_ep = 1;
6922 it->function_atend = ef;
6923 it->pointer = argp;
6924 it->val = argi;
6925 it->pcb_flags = pcb_state;
6926 it->pcb_features = pcb_features;
6927 it->asoc_state = asoc_state;
6928 it->function_inp_end = inpe;
6929 it->no_chunk_output = chunk_output_off;
6930 it->vn = curvnet;
6931 if (s_inp) {
6932 /* Assume lock is held here */
6933 it->inp = s_inp;
6934 SCTP_INP_INCR_REF(it->inp);
6935 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
6936 } else {
6937 SCTP_INP_INFO_RLOCK();
6938 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
6939 if (it->inp) {
6940 SCTP_INP_INCR_REF(it->inp);
6941 }
6942 SCTP_INP_INFO_RUNLOCK();
6943 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
6944 }
6945 SCTP_IPI_ITERATOR_WQ_LOCK();
6946 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6947 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6948 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
6949 SCTP_BASE_VAR(sctp_pcb_initialized), it);
6950 SCTP_FREE(it, SCTP_M_ITER);
6951 return (-1);
6952 }
6953 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6954 if (sctp_it_ctl.iterator_running == 0) {
6955 sctp_wakeup_iterator();
6956 }
6957 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6958 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
6959 return (0);
6960 }
6961
6962 /*
6963 * Atomically add flags to the sctp_flags of an inp.
6964 * To be used when the write lock of the inp is not held.
6965 */
6966 void
sctp_pcb_add_flags(struct sctp_inpcb * inp,uint32_t flags)6967 sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags)
6968 {
6969 uint32_t old_flags, new_flags;
6970
6971 do {
6972 old_flags = inp->sctp_flags;
6973 new_flags = old_flags | flags;
6974 } while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0);
6975 }
6976