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