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