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