]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/netinet/sctp_pcb.c
MFC r228653:
[FreeBSD/stable/8.git] / sys / netinet / sctp_pcb.c
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2011, by Randall Stewart. 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 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                 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2073                         SCTP_INP_RUNLOCK(stcb->sctp_ep);
2074                         continue;
2075                 }
2076                 SCTP_TCB_LOCK(stcb);
2077                 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2078                 if (stcb->asoc.my_vtag == vtag) {
2079                         /* candidate */
2080                         if (stcb->rport != rport) {
2081                                 SCTP_TCB_UNLOCK(stcb);
2082                                 continue;
2083                         }
2084                         if (stcb->sctp_ep->sctp_lport != lport) {
2085                                 SCTP_TCB_UNLOCK(stcb);
2086                                 continue;
2087                         }
2088                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2089                                 SCTP_TCB_UNLOCK(stcb);
2090                                 continue;
2091                         }
2092                         /* RRS:Need toaddr check here */
2093                         if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2094                                 /* Endpoint does not own this address */
2095                                 SCTP_TCB_UNLOCK(stcb);
2096                                 continue;
2097                         }
2098                         if (remote_tag) {
2099                                 /*
2100                                  * If we have both vtags that's all we match
2101                                  * on
2102                                  */
2103                                 if (stcb->asoc.peer_vtag == remote_tag) {
2104                                         /*
2105                                          * If both tags match we consider it
2106                                          * conclusive and check NO
2107                                          * source/destination addresses
2108                                          */
2109                                         goto conclusive;
2110                                 }
2111                         }
2112                         if (skip_src_check) {
2113                 conclusive:
2114                                 if (from) {
2115                                         net = sctp_findnet(stcb, from);
2116                                 } else {
2117                                         *netp = NULL;   /* unknown */
2118                                 }
2119                                 if (inp_p)
2120                                         *inp_p = stcb->sctp_ep;
2121                                 SCTP_INP_INFO_RUNLOCK();
2122                                 return (stcb);
2123                         }
2124                         net = sctp_findnet(stcb, from);
2125                         if (net) {
2126                                 /* yep its him. */
2127                                 *netp = net;
2128                                 SCTP_STAT_INCR(sctps_vtagexpress);
2129                                 *inp_p = stcb->sctp_ep;
2130                                 SCTP_INP_INFO_RUNLOCK();
2131                                 return (stcb);
2132                         } else {
2133                                 /*
2134                                  * not him, this should only happen in rare
2135                                  * cases so I peg it.
2136                                  */
2137                                 SCTP_STAT_INCR(sctps_vtagbogus);
2138                         }
2139                 }
2140                 SCTP_TCB_UNLOCK(stcb);
2141         }
2142         SCTP_INP_INFO_RUNLOCK();
2143         return (NULL);
2144 }
2145
2146 /*
2147  * Find an association with the pointer to the inbound IP packet. This can be
2148  * a IPv4 or IPv6 packet.
2149  */
2150 struct sctp_tcb *
2151 sctp_findassociation_addr(struct mbuf *m, int offset,
2152     struct sctphdr *sh, struct sctp_chunkhdr *ch,
2153     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2154 {
2155         int find_tcp_pool;
2156         struct ip *iph;
2157         struct sctp_tcb *retval;
2158         struct sockaddr_storage to_store, from_store;
2159         struct sockaddr *to = (struct sockaddr *)&to_store;
2160         struct sockaddr *from = (struct sockaddr *)&from_store;
2161         struct sctp_inpcb *inp;
2162
2163         iph = mtod(m, struct ip *);
2164         switch (iph->ip_v) {
2165 #ifdef INET
2166         case IPVERSION:
2167                 {
2168                         /* its IPv4 */
2169                         struct sockaddr_in *from4;
2170
2171                         from4 = (struct sockaddr_in *)&from_store;
2172                         bzero(from4, sizeof(*from4));
2173                         from4->sin_family = AF_INET;
2174                         from4->sin_len = sizeof(struct sockaddr_in);
2175                         from4->sin_addr.s_addr = iph->ip_src.s_addr;
2176                         from4->sin_port = sh->src_port;
2177                         break;
2178                 }
2179 #endif
2180 #ifdef INET6
2181         case IPV6_VERSION >> 4:
2182                 {
2183                         /* its IPv6 */
2184                         struct ip6_hdr *ip6;
2185                         struct sockaddr_in6 *from6;
2186
2187                         ip6 = mtod(m, struct ip6_hdr *);
2188                         from6 = (struct sockaddr_in6 *)&from_store;
2189                         bzero(from6, sizeof(*from6));
2190                         from6->sin6_family = AF_INET6;
2191                         from6->sin6_len = sizeof(struct sockaddr_in6);
2192                         from6->sin6_addr = ip6->ip6_src;
2193                         from6->sin6_port = sh->src_port;
2194                         /* Get the scopes in properly to the sin6 addr's */
2195                         /* we probably don't need these operations */
2196                         (void)sa6_recoverscope(from6);
2197                         sa6_embedscope(from6, MODULE_GLOBAL(ip6_use_defzone));
2198                         break;
2199                 }
2200 #endif
2201         default:
2202                 /* Currently not supported. */
2203                 return (NULL);
2204         }
2205
2206
2207         switch (iph->ip_v) {
2208 #ifdef INET
2209         case IPVERSION:
2210                 {
2211                         /* its IPv4 */
2212                         struct sockaddr_in *to4;
2213
2214                         to4 = (struct sockaddr_in *)&to_store;
2215                         bzero(to4, sizeof(*to4));
2216                         to4->sin_family = AF_INET;
2217                         to4->sin_len = sizeof(struct sockaddr_in);
2218                         to4->sin_addr.s_addr = iph->ip_dst.s_addr;
2219                         to4->sin_port = sh->dest_port;
2220                         break;
2221                 }
2222 #endif
2223 #ifdef INET6
2224         case IPV6_VERSION >> 4:
2225                 {
2226                         /* its IPv6 */
2227                         struct ip6_hdr *ip6;
2228                         struct sockaddr_in6 *to6;
2229
2230                         ip6 = mtod(m, struct ip6_hdr *);
2231                         to6 = (struct sockaddr_in6 *)&to_store;
2232                         bzero(to6, sizeof(*to6));
2233                         to6->sin6_family = AF_INET6;
2234                         to6->sin6_len = sizeof(struct sockaddr_in6);
2235                         to6->sin6_addr = ip6->ip6_dst;
2236                         to6->sin6_port = sh->dest_port;
2237                         /* Get the scopes in properly to the sin6 addr's */
2238                         /* we probably don't need these operations */
2239                         (void)sa6_recoverscope(to6);
2240                         sa6_embedscope(to6, MODULE_GLOBAL(ip6_use_defzone));
2241                         break;
2242                 }
2243 #endif
2244         default:
2245                 /* TSNH */
2246                 break;
2247         }
2248         if (sh->v_tag) {
2249                 /* we only go down this path if vtag is non-zero */
2250                 retval = sctp_findassoc_by_vtag(from, to, ntohl(sh->v_tag),
2251                     inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2252                 if (retval) {
2253                         return (retval);
2254                 }
2255         }
2256         find_tcp_pool = 0;
2257         if ((ch->chunk_type != SCTP_INITIATION) &&
2258             (ch->chunk_type != SCTP_INITIATION_ACK) &&
2259             (ch->chunk_type != SCTP_COOKIE_ACK) &&
2260             (ch->chunk_type != SCTP_COOKIE_ECHO)) {
2261                 /* Other chunk types go to the tcp pool. */
2262                 find_tcp_pool = 1;
2263         }
2264         if (inp_p) {
2265                 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
2266                     find_tcp_pool, vrf_id);
2267                 inp = *inp_p;
2268         } else {
2269                 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
2270                     find_tcp_pool, vrf_id);
2271         }
2272         SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
2273         if (retval == NULL && inp) {
2274                 /* Found a EP but not this address */
2275                 if ((ch->chunk_type == SCTP_INITIATION) ||
2276                     (ch->chunk_type == SCTP_INITIATION_ACK)) {
2277                         /*-
2278                          * special hook, we do NOT return linp or an
2279                          * association that is linked to an existing
2280                          * association that is under the TCP pool (i.e. no
2281                          * listener exists). The endpoint finding routine
2282                          * will always find a listener before examining the
2283                          * TCP pool.
2284                          */
2285                         if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2286                                 if (inp_p) {
2287                                         *inp_p = NULL;
2288                                 }
2289                                 return (NULL);
2290                         }
2291                         retval = sctp_findassociation_special_addr(m,
2292                             offset, sh, &inp, netp, to);
2293                         if (inp_p != NULL) {
2294                                 *inp_p = inp;
2295                         }
2296                 }
2297         }
2298         SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
2299         return (retval);
2300 }
2301
2302 /*
2303  * lookup an association by an ASCONF lookup address.
2304  * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2305  */
2306 struct sctp_tcb *
2307 sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
2308     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2309 {
2310         struct sctp_tcb *stcb;
2311         struct sockaddr_storage local_store, remote_store;
2312         struct sockaddr *to;
2313         struct ip *iph;
2314         struct sctp_paramhdr parm_buf, *phdr;
2315         int ptype;
2316         int zero_address = 0;
2317
2318 #ifdef INET
2319         struct sockaddr_in *sin;
2320
2321 #endif
2322 #ifdef INET6
2323         struct ip6_hdr *ip6;
2324         struct sockaddr_in6 *sin6;
2325
2326 #endif
2327
2328         memset(&local_store, 0, sizeof(local_store));
2329         memset(&remote_store, 0, sizeof(remote_store));
2330         to = (struct sockaddr *)&local_store;
2331         /* First get the destination address setup too. */
2332         iph = mtod(m, struct ip *);
2333         switch (iph->ip_v) {
2334 #ifdef INET
2335         case IPVERSION:
2336                 /* its IPv4 */
2337                 sin = (struct sockaddr_in *)&local_store;
2338                 sin->sin_family = AF_INET;
2339                 sin->sin_len = sizeof(*sin);
2340                 sin->sin_port = sh->dest_port;
2341                 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
2342                 break;
2343 #endif
2344 #ifdef INET6
2345         case IPV6_VERSION >> 4:
2346                 /* its IPv6 */
2347                 ip6 = mtod(m, struct ip6_hdr *);
2348                 sin6 = (struct sockaddr_in6 *)&local_store;
2349                 sin6->sin6_family = AF_INET6;
2350                 sin6->sin6_len = sizeof(*sin6);
2351                 sin6->sin6_port = sh->dest_port;
2352                 sin6->sin6_addr = ip6->ip6_dst;
2353                 break;
2354 #endif
2355         default:
2356                 return NULL;
2357         }
2358
2359         phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2360             &parm_buf, sizeof(struct sctp_paramhdr));
2361         if (phdr == NULL) {
2362                 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2363                     __FUNCTION__);
2364                 return NULL;
2365         }
2366         ptype = (int)((uint32_t) ntohs(phdr->param_type));
2367         /* get the correlation address */
2368         switch (ptype) {
2369 #ifdef INET6
2370         case SCTP_IPV6_ADDRESS:
2371                 {
2372                         /* ipv6 address param */
2373                         struct sctp_ipv6addr_param *p6, p6_buf;
2374
2375                         if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2376                                 return NULL;
2377                         }
2378                         p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2379                             offset + sizeof(struct sctp_asconf_chunk),
2380                             &p6_buf.ph, sizeof(*p6));
2381                         if (p6 == NULL) {
2382                                 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2383                                     __FUNCTION__);
2384                                 return (NULL);
2385                         }
2386                         sin6 = (struct sockaddr_in6 *)&remote_store;
2387                         sin6->sin6_family = AF_INET6;
2388                         sin6->sin6_len = sizeof(*sin6);
2389                         sin6->sin6_port = sh->src_port;
2390                         memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2391                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2392                                 zero_address = 1;
2393                         break;
2394                 }
2395 #endif
2396 #ifdef INET
2397         case SCTP_IPV4_ADDRESS:
2398                 {
2399                         /* ipv4 address param */
2400                         struct sctp_ipv4addr_param *p4, p4_buf;
2401
2402                         if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2403                                 return NULL;
2404                         }
2405                         p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2406                             offset + sizeof(struct sctp_asconf_chunk),
2407                             &p4_buf.ph, sizeof(*p4));
2408                         if (p4 == NULL) {
2409                                 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2410                                     __FUNCTION__);
2411                                 return (NULL);
2412                         }
2413                         sin = (struct sockaddr_in *)&remote_store;
2414                         sin->sin_family = AF_INET;
2415                         sin->sin_len = sizeof(*sin);
2416                         sin->sin_port = sh->src_port;
2417                         memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2418                         if (sin->sin_addr.s_addr == INADDR_ANY)
2419                                 zero_address = 1;
2420                         break;
2421                 }
2422 #endif
2423         default:
2424                 /* invalid address param type */
2425                 return NULL;
2426         }
2427
2428         if (zero_address) {
2429                 stcb = sctp_findassoc_by_vtag(NULL, to, ntohl(sh->v_tag), inp_p,
2430                     netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
2431                 /*
2432                  * printf("findassociation_ep_asconf: zero lookup address
2433                  * finds stcb 0x%x\n", (uint32_t)stcb);
2434                  */
2435         } else {
2436                 stcb = sctp_findassociation_ep_addr(inp_p,
2437                     (struct sockaddr *)&remote_store, netp,
2438                     to, NULL);
2439         }
2440         return (stcb);
2441 }
2442
2443
2444 /*
2445  * allocate a sctp_inpcb and setup a temporary binding to a port/all
2446  * addresses. This way if we don't get a bind we by default pick a ephemeral
2447  * port with all addresses bound.
2448  */
2449 int
2450 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2451 {
2452         /*
2453          * we get called when a new endpoint starts up. We need to allocate
2454          * the sctp_inpcb structure from the zone and init it. Mark it as
2455          * unbound and find a port that we can use as an ephemeral with
2456          * INADDR_ANY. If the user binds later no problem we can then add in
2457          * the specific addresses. And setup the default parameters for the
2458          * EP.
2459          */
2460         int i, error;
2461         struct sctp_inpcb *inp;
2462         struct sctp_pcb *m;
2463         struct timeval time;
2464         sctp_sharedkey_t *null_key;
2465
2466         error = 0;
2467
2468         SCTP_INP_INFO_WLOCK();
2469         inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2470         if (inp == NULL) {
2471                 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2472                 SCTP_INP_INFO_WUNLOCK();
2473                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2474                 return (ENOBUFS);
2475         }
2476         /* zap it */
2477         bzero(inp, sizeof(*inp));
2478
2479         /* bump generations */
2480         /* setup socket pointers */
2481         inp->sctp_socket = so;
2482         inp->ip_inp.inp.inp_socket = so;
2483 #ifdef INET6
2484         if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2485                 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2486         }
2487 #endif
2488         inp->sctp_associd_counter = 1;
2489         inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2490         inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2491         inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2492         inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable);
2493         /* init the small hash table we use to track asocid <-> tcb */
2494         inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2495         if (inp->sctp_asocidhash == NULL) {
2496                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2497                 SCTP_INP_INFO_WUNLOCK();
2498                 return (ENOBUFS);
2499         }
2500 #ifdef IPSEC
2501         {
2502                 struct inpcbpolicy *pcb_sp = NULL;
2503
2504                 error = ipsec_init_policy(so, &pcb_sp);
2505                 /* Arrange to share the policy */
2506                 inp->ip_inp.inp.inp_sp = pcb_sp;
2507                 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2508         }
2509         if (error != 0) {
2510                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2511                 SCTP_INP_INFO_WUNLOCK();
2512                 return error;
2513         }
2514 #endif                          /* IPSEC */
2515         SCTP_INCR_EP_COUNT();
2516         inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
2517         SCTP_INP_INFO_WUNLOCK();
2518
2519         so->so_pcb = (caddr_t)inp;
2520
2521         if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2522                 /* UDP style socket */
2523                 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2524                     SCTP_PCB_FLAGS_UNBOUND);
2525                 /* Be sure it is NON-BLOCKING IO for UDP */
2526                 /* SCTP_SET_SO_NBIO(so); */
2527         } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2528                 /* TCP style socket */
2529                 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2530                     SCTP_PCB_FLAGS_UNBOUND);
2531                 /* Be sure we have blocking IO by default */
2532                 SCTP_CLEAR_SO_NBIO(so);
2533         } else {
2534                 /*
2535                  * unsupported socket type (RAW, etc)- in case we missed it
2536                  * in protosw
2537                  */
2538                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2539                 so->so_pcb = NULL;
2540                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2541                 return (EOPNOTSUPP);
2542         }
2543         if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2544                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2545                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2546         } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2547                 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2548                 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2549         } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2550                 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2551                 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2552         }
2553         inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2554             &inp->sctp_hashmark);
2555         if (inp->sctp_tcbhash == NULL) {
2556                 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2557                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2558                 so->so_pcb = NULL;
2559                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2560                 return (ENOBUFS);
2561         }
2562         inp->def_vrf_id = vrf_id;
2563
2564         SCTP_INP_INFO_WLOCK();
2565         SCTP_INP_LOCK_INIT(inp);
2566         INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2567         SCTP_INP_READ_INIT(inp);
2568         SCTP_ASOC_CREATE_LOCK_INIT(inp);
2569         /* lock the new ep */
2570         SCTP_INP_WLOCK(inp);
2571
2572         /* add it to the info area */
2573         LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2574         SCTP_INP_INFO_WUNLOCK();
2575
2576         TAILQ_INIT(&inp->read_queue);
2577         LIST_INIT(&inp->sctp_addr_list);
2578
2579         LIST_INIT(&inp->sctp_asoc_list);
2580
2581 #ifdef SCTP_TRACK_FREED_ASOCS
2582         /* TEMP CODE */
2583         LIST_INIT(&inp->sctp_asoc_free_list);
2584 #endif
2585         /* Init the timer structure for signature change */
2586         SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2587         inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2588
2589         /* now init the actual endpoint default data */
2590         m = &inp->sctp_ep;
2591
2592         /* setup the base timeout information */
2593         m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC);    /* needed ? */
2594         m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC);    /* needed ? */
2595         m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2596         m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2597         m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2598         m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2599         m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2600         /* all max/min max are in ms */
2601         m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2602         m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2603         m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2604         m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2605         m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2606
2607         m->max_open_streams_intome = MAX_SCTP_STREAMS;
2608
2609         m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2610         m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2611         m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2612         m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2613         m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2614         m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2615         m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2616         m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2617
2618         m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2619         m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2620         /* number of streams to pre-open on a association */
2621         m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2622
2623         /* Add adaptation cookie */
2624         m->adaptation_layer_indicator = 0x504C5253;
2625
2626         /* seed random number generator */
2627         m->random_counter = 1;
2628         m->store_at = SCTP_SIGNATURE_SIZE;
2629         SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2630         sctp_fill_random_store(m);
2631
2632         /* Minimum cookie size */
2633         m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2634             sizeof(struct sctp_state_cookie);
2635         m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2636
2637         /* Setup the initial secret */
2638         (void)SCTP_GETTIME_TIMEVAL(&time);
2639         m->time_of_secret_change = time.tv_sec;
2640
2641         for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2642                 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2643         }
2644         sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2645
2646         /* How long is a cookie good for ? */
2647         m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2648         /*
2649          * Initialize authentication parameters
2650          */
2651         m->local_hmacs = sctp_default_supported_hmaclist();
2652         m->local_auth_chunks = sctp_alloc_chunklist();
2653         m->default_dscp = 0;
2654 #ifdef INET6
2655         m->default_flowlabel = 0;
2656 #endif
2657         m->port = 0;            /* encapsulation disabled by default */
2658         sctp_auth_set_default_chunks(m->local_auth_chunks);
2659         LIST_INIT(&m->shared_keys);
2660         /* add default NULL key as key id 0 */
2661         null_key = sctp_alloc_sharedkey();
2662         sctp_insert_sharedkey(&m->shared_keys, null_key);
2663         SCTP_INP_WUNLOCK(inp);
2664 #ifdef SCTP_LOG_CLOSING
2665         sctp_log_closing(inp, NULL, 12);
2666 #endif
2667         return (error);
2668 }
2669
2670
2671 void
2672 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2673     struct sctp_tcb *stcb)
2674 {
2675         struct sctp_nets *net;
2676         uint16_t lport, rport;
2677         struct sctppcbhead *head;
2678         struct sctp_laddr *laddr, *oladdr;
2679
2680         atomic_add_int(&stcb->asoc.refcnt, 1);
2681         SCTP_TCB_UNLOCK(stcb);
2682         SCTP_INP_INFO_WLOCK();
2683         SCTP_INP_WLOCK(old_inp);
2684         SCTP_INP_WLOCK(new_inp);
2685         SCTP_TCB_LOCK(stcb);
2686         atomic_subtract_int(&stcb->asoc.refcnt, 1);
2687
2688         new_inp->sctp_ep.time_of_secret_change =
2689             old_inp->sctp_ep.time_of_secret_change;
2690         memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2691             sizeof(old_inp->sctp_ep.secret_key));
2692         new_inp->sctp_ep.current_secret_number =
2693             old_inp->sctp_ep.current_secret_number;
2694         new_inp->sctp_ep.last_secret_number =
2695             old_inp->sctp_ep.last_secret_number;
2696         new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2697
2698         /* make it so new data pours into the new socket */
2699         stcb->sctp_socket = new_inp->sctp_socket;
2700         stcb->sctp_ep = new_inp;
2701
2702         /* Copy the port across */
2703         lport = new_inp->sctp_lport = old_inp->sctp_lport;
2704         rport = stcb->rport;
2705         /* Pull the tcb from the old association */
2706         LIST_REMOVE(stcb, sctp_tcbhash);
2707         LIST_REMOVE(stcb, sctp_tcblist);
2708         if (stcb->asoc.in_asocid_hash) {
2709                 LIST_REMOVE(stcb, sctp_tcbasocidhash);
2710         }
2711         /* Now insert the new_inp into the TCP connected hash */
2712         head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2713
2714         LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2715         /* Its safe to access */
2716         new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2717
2718         /* Now move the tcb into the endpoint list */
2719         LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2720         /*
2721          * Question, do we even need to worry about the ep-hash since we
2722          * only have one connection? Probably not :> so lets get rid of it
2723          * and not suck up any kernel memory in that.
2724          */
2725         if (stcb->asoc.in_asocid_hash) {
2726                 struct sctpasochead *lhd;
2727
2728                 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2729                     new_inp->hashasocidmark)];
2730                 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2731         }
2732         /* Ok. Let's restart timer. */
2733         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2734                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2735                     stcb, net);
2736         }
2737
2738         SCTP_INP_INFO_WUNLOCK();
2739         if (new_inp->sctp_tcbhash != NULL) {
2740                 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2741                 new_inp->sctp_tcbhash = NULL;
2742         }
2743         if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2744                 /* Subset bound, so copy in the laddr list from the old_inp */
2745                 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2746                         laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2747                         if (laddr == NULL) {
2748                                 /*
2749                                  * Gak, what can we do? This assoc is really
2750                                  * HOSED. We probably should send an abort
2751                                  * here.
2752                                  */
2753                                 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2754                                 continue;
2755                         }
2756                         SCTP_INCR_LADDR_COUNT();
2757                         bzero(laddr, sizeof(*laddr));
2758                         (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2759                         laddr->ifa = oladdr->ifa;
2760                         atomic_add_int(&laddr->ifa->refcount, 1);
2761                         LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2762                             sctp_nxt_addr);
2763                         new_inp->laddr_count++;
2764                         if (oladdr == stcb->asoc.last_used_address) {
2765                                 stcb->asoc.last_used_address = laddr;
2766                         }
2767                 }
2768         }
2769         /*
2770          * Now any running timers need to be adjusted since we really don't
2771          * care if they are running or not just blast in the new_inp into
2772          * all of them.
2773          */
2774
2775         stcb->asoc.dack_timer.ep = (void *)new_inp;
2776         stcb->asoc.asconf_timer.ep = (void *)new_inp;
2777         stcb->asoc.strreset_timer.ep = (void *)new_inp;
2778         stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
2779         stcb->asoc.autoclose_timer.ep = (void *)new_inp;
2780         stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
2781         stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
2782         /* now what about the nets? */
2783         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2784                 net->pmtu_timer.ep = (void *)new_inp;
2785                 net->hb_timer.ep = (void *)new_inp;
2786                 net->rxt_timer.ep = (void *)new_inp;
2787         }
2788         SCTP_INP_WUNLOCK(new_inp);
2789         SCTP_INP_WUNLOCK(old_inp);
2790 }
2791
2792
2793
2794
2795 /* sctp_ifap is used to bypass normal local address validation checks */
2796 int
2797 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
2798     struct sctp_ifa *sctp_ifap, struct thread *p)
2799 {
2800         /* bind a ep to a socket address */
2801         struct sctppcbhead *head;
2802         struct sctp_inpcb *inp, *inp_tmp;
2803         struct inpcb *ip_inp;
2804         int port_reuse_active = 0;
2805         int bindall;
2806         uint16_t lport;
2807         int error;
2808         uint32_t vrf_id;
2809
2810         lport = 0;
2811         error = 0;
2812         bindall = 1;
2813         inp = (struct sctp_inpcb *)so->so_pcb;
2814         ip_inp = (struct inpcb *)so->so_pcb;
2815 #ifdef SCTP_DEBUG
2816         if (addr) {
2817                 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
2818                     ntohs(((struct sockaddr_in *)addr)->sin_port));
2819                 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
2820                 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2821         }
2822 #endif
2823         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2824                 /* already did a bind, subsequent binds NOT allowed ! */
2825                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2826                 return (EINVAL);
2827         }
2828 #ifdef INVARIANTS
2829         if (p == NULL)
2830                 panic("null proc/thread");
2831 #endif
2832         if (addr != NULL) {
2833                 switch (addr->sa_family) {
2834 #ifdef INET
2835                 case AF_INET:
2836                         {
2837                                 struct sockaddr_in *sin;
2838
2839                                 /* IPV6_V6ONLY socket? */
2840                                 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2841                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2842                                         return (EINVAL);
2843                                 }
2844                                 if (addr->sa_len != sizeof(*sin)) {
2845                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2846                                         return (EINVAL);
2847                                 }
2848                                 sin = (struct sockaddr_in *)addr;
2849                                 lport = sin->sin_port;
2850                                 /*
2851                                  * For LOOPBACK the prison_local_ip4() call
2852                                  * will transmute the ip address to the
2853                                  * proper value.
2854                                  */
2855                                 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
2856                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2857                                         return (error);
2858                                 }
2859                                 if (sin->sin_addr.s_addr != INADDR_ANY) {
2860                                         bindall = 0;
2861                                 }
2862                                 break;
2863                         }
2864 #endif
2865 #ifdef INET6
2866                 case AF_INET6:
2867                         {
2868                                 /*
2869                                  * Only for pure IPv6 Address. (No IPv4
2870                                  * Mapped!)
2871                                  */
2872                                 struct sockaddr_in6 *sin6;
2873
2874                                 sin6 = (struct sockaddr_in6 *)addr;
2875
2876                                 if (addr->sa_len != sizeof(*sin6)) {
2877                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2878                                         return (EINVAL);
2879                                 }
2880                                 lport = sin6->sin6_port;
2881
2882                                 /*
2883                                  * For LOOPBACK the prison_local_ip6() call
2884                                  * will transmute the ipv6 address to the
2885                                  * proper value.
2886                                  */
2887                                 if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
2888                                     (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
2889                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2890                                         return (error);
2891                                 }
2892                                 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2893                                         bindall = 0;
2894                                         /* KAME hack: embed scopeid */
2895                                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
2896                                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2897                                                 return (EINVAL);
2898                                         }
2899                                 }
2900                                 /* this must be cleared for ifa_ifwithaddr() */
2901                                 sin6->sin6_scope_id = 0;
2902                                 break;
2903                         }
2904 #endif
2905                 default:
2906                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
2907                         return (EAFNOSUPPORT);
2908                 }
2909         }
2910         SCTP_INP_INFO_WLOCK();
2911         SCTP_INP_WLOCK(inp);
2912         /* Setup a vrf_id to be the default for the non-bind-all case. */
2913         vrf_id = inp->def_vrf_id;
2914
2915         /* increase our count due to the unlock we do */
2916         SCTP_INP_INCR_REF(inp);
2917         if (lport) {
2918                 /*
2919                  * Did the caller specify a port? if so we must see if a ep
2920                  * already has this one bound.
2921                  */
2922                 /* got to be root to get at low ports */
2923                 if (ntohs(lport) < IPPORT_RESERVED) {
2924                         if (p && (error =
2925                             priv_check(p, PRIV_NETINET_RESERVEDPORT)
2926                             )) {
2927                                 SCTP_INP_DECR_REF(inp);
2928                                 SCTP_INP_WUNLOCK(inp);
2929                                 SCTP_INP_INFO_WUNLOCK();
2930                                 return (error);
2931                         }
2932                 }
2933                 if (p == NULL) {
2934                         SCTP_INP_DECR_REF(inp);
2935                         SCTP_INP_WUNLOCK(inp);
2936                         SCTP_INP_INFO_WUNLOCK();
2937                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2938                         return (error);
2939                 }
2940                 SCTP_INP_WUNLOCK(inp);
2941                 if (bindall) {
2942                         vrf_id = inp->def_vrf_id;
2943                         inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2944                         if (inp_tmp != NULL) {
2945                                 /*
2946                                  * lock guy returned and lower count note
2947                                  * that we are not bound so inp_tmp should
2948                                  * NEVER be inp. And it is this inp
2949                                  * (inp_tmp) that gets the reference bump,
2950                                  * so we must lower it.
2951                                  */
2952                                 SCTP_INP_DECR_REF(inp_tmp);
2953                                 /* unlock info */
2954                                 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2955                                     (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2956                                         /*
2957                                          * Ok, must be one-2-one and
2958                                          * allowing port re-use
2959                                          */
2960                                         port_reuse_active = 1;
2961                                         goto continue_anyway;
2962                                 }
2963                                 SCTP_INP_DECR_REF(inp);
2964                                 SCTP_INP_INFO_WUNLOCK();
2965                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2966                                 return (EADDRINUSE);
2967                         }
2968                 } else {
2969                         inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2970                         if (inp_tmp != NULL) {
2971                                 /*
2972                                  * lock guy returned and lower count note
2973                                  * that we are not bound so inp_tmp should
2974                                  * NEVER be inp. And it is this inp
2975                                  * (inp_tmp) that gets the reference bump,
2976                                  * so we must lower it.
2977                                  */
2978                                 SCTP_INP_DECR_REF(inp_tmp);
2979                                 /* unlock info */
2980                                 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2981                                     (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2982                                         /*
2983                                          * Ok, must be one-2-one and
2984                                          * allowing port re-use
2985                                          */
2986                                         port_reuse_active = 1;
2987                                         goto continue_anyway;
2988                                 }
2989                                 SCTP_INP_DECR_REF(inp);
2990                                 SCTP_INP_INFO_WUNLOCK();
2991                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2992                                 return (EADDRINUSE);
2993                         }
2994                 }
2995 continue_anyway:
2996                 SCTP_INP_WLOCK(inp);
2997                 if (bindall) {
2998                         /* verify that no lport is not used by a singleton */
2999                         if ((port_reuse_active == 0) &&
3000                             (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))
3001                             ) {
3002                                 /* Sorry someone already has this one bound */
3003                                 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3004                                     (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3005                                         port_reuse_active = 1;
3006                                 } else {
3007                                         SCTP_INP_DECR_REF(inp);
3008                                         SCTP_INP_WUNLOCK(inp);
3009                                         SCTP_INP_INFO_WUNLOCK();
3010                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3011                                         return (EADDRINUSE);
3012                                 }
3013                         }
3014                 }
3015         } else {
3016                 uint16_t first, last, candidate;
3017                 uint16_t count;
3018                 int done;
3019
3020                 if (ip_inp->inp_flags & INP_HIGHPORT) {
3021                         first = MODULE_GLOBAL(ipport_hifirstauto);
3022                         last = MODULE_GLOBAL(ipport_hilastauto);
3023                 } else if (ip_inp->inp_flags & INP_LOWPORT) {
3024                         if (p && (error =
3025                             priv_check(p, PRIV_NETINET_RESERVEDPORT)
3026                             )) {
3027                                 SCTP_INP_DECR_REF(inp);
3028                                 SCTP_INP_WUNLOCK(inp);
3029                                 SCTP_INP_INFO_WUNLOCK();
3030                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3031                                 return (error);
3032                         }
3033                         first = MODULE_GLOBAL(ipport_lowfirstauto);
3034                         last = MODULE_GLOBAL(ipport_lowlastauto);
3035                 } else {
3036                         first = MODULE_GLOBAL(ipport_firstauto);
3037                         last = MODULE_GLOBAL(ipport_lastauto);
3038                 }
3039                 if (first > last) {
3040                         uint16_t temp;
3041
3042                         temp = first;
3043                         first = last;
3044                         last = temp;
3045                 }
3046                 count = last - first + 1;       /* number of candidates */
3047                 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3048
3049                 done = 0;
3050                 while (!done) {
3051                         if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3052                                 done = 1;
3053                         }
3054                         if (!done) {
3055                                 if (--count == 0) {
3056                                         SCTP_INP_DECR_REF(inp);
3057                                         SCTP_INP_WUNLOCK(inp);
3058                                         SCTP_INP_INFO_WUNLOCK();
3059                                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3060                                         return (EADDRINUSE);
3061                                 }
3062                                 if (candidate == last)
3063                                         candidate = first;
3064                                 else
3065                                         candidate = candidate + 1;
3066                         }
3067                 }
3068                 lport = htons(candidate);
3069         }
3070         SCTP_INP_DECR_REF(inp);
3071         if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3072             SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3073                 /*
3074                  * this really should not happen. The guy did a non-blocking
3075                  * bind and then did a close at the same time.
3076                  */
3077                 SCTP_INP_WUNLOCK(inp);
3078                 SCTP_INP_INFO_WUNLOCK();
3079                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3080                 return (EINVAL);
3081         }
3082         /* ok we look clear to give out this port, so lets setup the binding */
3083         if (bindall) {
3084                 /* binding to all addresses, so just set in the proper flags */
3085                 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3086                 /* set the automatic addr changes from kernel flag */
3087                 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3088                         sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3089                         sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3090                 } else {
3091                         sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3092                         sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3093                 }
3094                 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3095                         sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3096                 } else {
3097                         sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3098                 }
3099                 /*
3100                  * set the automatic mobility_base from kernel flag (by
3101                  * micchie)
3102                  */
3103                 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3104                         sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3105                         sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3106                 } else {
3107                         sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3108                         sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3109                 }
3110                 /*
3111                  * set the automatic mobility_fasthandoff from kernel flag
3112                  * (by micchie)
3113                  */
3114                 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3115                         sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3116                         sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3117                 } else {
3118                         sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3119                         sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3120                 }
3121         } else {
3122                 /*
3123                  * bind specific, make sure flags is off and add a new
3124                  * address structure to the sctp_addr_list inside the ep
3125                  * structure.
3126                  * 
3127                  * We will need to allocate one and insert it at the head. The
3128                  * socketopt call can just insert new addresses in there as
3129                  * well. It will also have to do the embed scope kame hack
3130                  * too (before adding).
3131                  */
3132                 struct sctp_ifa *ifa;
3133                 struct sockaddr_storage store_sa;
3134
3135                 memset(&store_sa, 0, sizeof(store_sa));
3136                 switch (addr->sa_family) {
3137                 case AF_INET:
3138                         {
3139                                 struct sockaddr_in *sin;
3140
3141                                 sin = (struct sockaddr_in *)&store_sa;
3142                                 memcpy(sin, addr, sizeof(struct sockaddr_in));
3143                                 sin->sin_port = 0;
3144                                 break;
3145                         }
3146                 case AF_INET6:
3147                         {
3148                                 struct sockaddr_in6 *sin6;
3149
3150                                 sin6 = (struct sockaddr_in6 *)&store_sa;
3151                                 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
3152                                 sin6->sin6_port = 0;
3153                                 break;
3154                         }
3155                 default:
3156                         break;
3157                 }
3158                 /*
3159                  * first find the interface with the bound address need to
3160                  * zero out the port to find the address! yuck! can't do
3161                  * this earlier since need port for sctp_pcb_findep()
3162                  */
3163                 if (sctp_ifap != NULL)
3164                         ifa = sctp_ifap;
3165                 else {
3166                         /*
3167                          * Note for BSD we hit here always other O/S's will
3168                          * pass things in via the sctp_ifap argument
3169                          * (Panda).
3170                          */
3171                         ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
3172                             vrf_id, SCTP_ADDR_NOT_LOCKED);
3173                 }
3174                 if (ifa == NULL) {
3175                         /* Can't find an interface with that address */
3176                         SCTP_INP_WUNLOCK(inp);
3177                         SCTP_INP_INFO_WUNLOCK();
3178                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3179                         return (EADDRNOTAVAIL);
3180                 }
3181 #ifdef INET6
3182                 if (addr->sa_family == AF_INET6) {
3183                         /* GAK, more FIXME IFA lock? */
3184                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3185                                 /* Can't bind a non-existent addr. */
3186                                 SCTP_INP_WUNLOCK(inp);
3187                                 SCTP_INP_INFO_WUNLOCK();
3188                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3189                                 return (EINVAL);
3190                         }
3191                 }
3192 #endif
3193                 /* we're not bound all */
3194                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3195                 /* allow bindx() to send ASCONF's for binding changes */
3196                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3197                 /* clear automatic addr changes from kernel flag */
3198                 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3199
3200                 /* add this address to the endpoint list */
3201                 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3202                 if (error != 0) {
3203                         SCTP_INP_WUNLOCK(inp);
3204                         SCTP_INP_INFO_WUNLOCK();
3205                         return (error);
3206                 }
3207                 inp->laddr_count++;
3208         }
3209         /* find the bucket */
3210         if (port_reuse_active) {
3211                 /* Put it into tcp 1-2-1 hash */
3212                 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3213                 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3214         } else {
3215                 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3216         }
3217         /* put it in the bucket */
3218         LIST_INSERT_HEAD(head, inp, sctp_hash);
3219         SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3220             head, ntohs(lport), port_reuse_active);
3221         /* set in the port */
3222         inp->sctp_lport = lport;
3223
3224         /* turn off just the unbound flag */
3225         inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3226         SCTP_INP_WUNLOCK(inp);
3227         SCTP_INP_INFO_WUNLOCK();
3228         return (0);
3229 }
3230
3231
3232 static void
3233 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3234 {
3235         struct sctp_iterator *it, *nit;
3236
3237         /*
3238          * We enter with the only the ITERATOR_LOCK in place and a write
3239          * lock on the inp_info stuff.
3240          */
3241         it = sctp_it_ctl.cur_it;
3242         if (it && (it->vn != curvnet)) {
3243                 /* Its not looking at our VNET */
3244                 return;
3245         }
3246         if (it && (it->inp == inp)) {
3247                 /*
3248                  * This is tricky and we hold the iterator lock, but when it
3249                  * returns and gets the lock (when we release it) the
3250                  * iterator will try to operate on inp. We need to stop that
3251                  * from happening. But of course the iterator has a
3252                  * reference on the stcb and inp. We can mark it and it will
3253                  * stop.
3254                  * 
3255                  * If its a single iterator situation, we set the end iterator
3256                  * flag. Otherwise we set the iterator to go to the next
3257                  * inp.
3258                  * 
3259                  */
3260                 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3261                         sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3262                 } else {
3263                         sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3264                 }
3265         }
3266         /*
3267          * Now go through and remove any single reference to our inp that
3268          * may be still pending on the list
3269          */
3270         SCTP_IPI_ITERATOR_WQ_LOCK();
3271         TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3272                 if (it->vn != curvnet) {
3273                         continue;
3274                 }
3275                 if (it->inp == inp) {
3276                         /* This one points to me is it inp specific? */
3277                         if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3278                                 /* Remove and free this one */
3279                                 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3280                                     it, sctp_nxt_itr);
3281                                 if (it->function_atend != NULL) {
3282                                         (*it->function_atend) (it->pointer, it->val);
3283                                 }
3284                                 SCTP_FREE(it, SCTP_M_ITER);
3285                         } else {
3286                                 it->inp = LIST_NEXT(it->inp, sctp_list);
3287                                 if (it->inp) {
3288                                         SCTP_INP_INCR_REF(it->inp);
3289                                 }
3290                         }
3291                         /*
3292                          * When its put in the refcnt is incremented so decr
3293                          * it
3294                          */
3295                         SCTP_INP_DECR_REF(inp);
3296                 }
3297         }
3298         SCTP_IPI_ITERATOR_WQ_UNLOCK();
3299 }
3300
3301 /* release sctp_inpcb unbind the port */
3302 void
3303 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3304 {
3305         /*
3306          * Here we free a endpoint. We must find it (if it is in the Hash
3307          * table) and remove it from there. Then we must also find it in the
3308          * overall list and remove it from there. After all removals are
3309          * complete then any timer has to be stopped. Then start the actual
3310          * freeing. a) Any local lists. b) Any associations. c) The hash of
3311          * all associations. d) finally the ep itself.
3312          */
3313         struct sctp_pcb *m;
3314         struct sctp_tcb *asoc, *nasoc;
3315         struct sctp_laddr *laddr, *nladdr;
3316         struct inpcb *ip_pcb;
3317         struct socket *so;
3318         int being_refed = 0;
3319         struct sctp_queued_to_read *sq, *nsq;
3320         int cnt;
3321         sctp_sharedkey_t *shared_key, *nshared_key;
3322
3323
3324 #ifdef SCTP_LOG_CLOSING
3325         sctp_log_closing(inp, NULL, 0);
3326 #endif
3327         SCTP_ITERATOR_LOCK();
3328         /* mark any iterators on the list or being processed */
3329         sctp_iterator_inp_being_freed(inp);
3330         SCTP_ITERATOR_UNLOCK();
3331         so = inp->sctp_socket;
3332         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3333                 /* been here before.. eeks.. get out of here */
3334                 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3335 #ifdef SCTP_LOG_CLOSING
3336                 sctp_log_closing(inp, NULL, 1);
3337 #endif
3338                 return;
3339         }
3340         SCTP_ASOC_CREATE_LOCK(inp);
3341         SCTP_INP_INFO_WLOCK();
3342
3343         SCTP_INP_WLOCK(inp);
3344         if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3345                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3346                 /* socket is gone, so no more wakeups allowed */
3347                 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3348                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3349                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3350
3351         }
3352         /* First time through we have the socket lock, after that no more. */
3353         sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3354             SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3355
3356         if (inp->control) {
3357                 sctp_m_freem(inp->control);
3358                 inp->control = NULL;
3359         }
3360         if (inp->pkt) {
3361                 sctp_m_freem(inp->pkt);
3362                 inp->pkt = NULL;
3363         }
3364         m = &inp->sctp_ep;
3365         ip_pcb = &inp->ip_inp.inp;      /* we could just cast the main pointer
3366                                          * here but I will be nice :> (i.e.
3367                                          * ip_pcb = ep;) */
3368         if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3369                 int cnt_in_sd;
3370
3371                 cnt_in_sd = 0;
3372                 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3373                         SCTP_TCB_LOCK(asoc);
3374                         if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3375                                 /* Skip guys being freed */
3376                                 cnt_in_sd++;
3377                                 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3378                                         /*
3379                                          * Special case - we did not start a
3380                                          * kill timer on the asoc due to it
3381                                          * was not closed. So go ahead and
3382                                          * start it now.
3383                                          */
3384                                         asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3385                                         sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3386                                 }
3387                                 SCTP_TCB_UNLOCK(asoc);
3388                                 continue;
3389                         }
3390                         if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3391                             (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
3392                             (asoc->asoc.total_output_queue_size == 0)) {
3393                                 /*
3394                                  * If we have data in queue, we don't want
3395                                  * to just free since the app may have done,
3396                                  * send()/close or connect/send/close. And
3397                                  * it wants the data to get across first.
3398                                  */
3399                                 /* Just abandon things in the front states */
3400                                 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3401                                     SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3402                                         cnt_in_sd++;
3403                                 }
3404                                 continue;
3405                         }
3406                         /* Disconnect the socket please */
3407                         asoc->sctp_socket = NULL;
3408                         asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3409                         if ((asoc->asoc.size_on_reasm_queue > 0) ||
3410                             (asoc->asoc.control_pdapi) ||
3411                             (asoc->asoc.size_on_all_streams > 0) ||
3412                             (so && (so->so_rcv.sb_cc > 0))
3413                             ) {
3414                                 /* Left with Data unread */
3415                                 struct mbuf *op_err;
3416
3417                                 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3418                                     0, M_DONTWAIT, 1, MT_DATA);
3419                                 if (op_err) {
3420                                         /* Fill in the user initiated abort */
3421                                         struct sctp_paramhdr *ph;
3422                                         uint32_t *ippp;
3423
3424                                         SCTP_BUF_LEN(op_err) =
3425                                             sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
3426                                         ph = mtod(op_err,
3427                                             struct sctp_paramhdr *);
3428                                         ph->param_type = htons(
3429                                             SCTP_CAUSE_USER_INITIATED_ABT);
3430                                         ph->param_length = htons(SCTP_BUF_LEN(op_err));
3431                                         ippp = (uint32_t *) (ph + 1);
3432                                         *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
3433                                 }
3434                                 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3435 #if defined(SCTP_PANIC_ON_ABORT)
3436                                 panic("inpcb_free does an abort");
3437 #endif
3438                                 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3439                                 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3440                                 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3441                                     (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3442                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3443                                 }
3444                                 if (sctp_free_assoc(inp, asoc,
3445                                     SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3446                                         cnt_in_sd++;
3447                                 }
3448                                 continue;
3449                         } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3450                                     TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3451                                     (asoc->asoc.stream_queue_cnt == 0)
3452                             ) {
3453                                 if (asoc->asoc.locked_on_sending) {
3454                                         goto abort_anyway;
3455                                 }
3456                                 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3457                                     (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3458                                         struct sctp_nets *netp;
3459
3460                                         if (asoc->asoc.alternate) {
3461                                                 netp = asoc->asoc.alternate;
3462                                         } else {
3463                                                 netp = asoc->asoc.primary_destination;
3464                                         }
3465                                         /*
3466                                          * there is nothing queued to send,
3467                                          * so I send shutdown
3468                                          */
3469                                         sctp_send_shutdown(asoc, netp);
3470                                         if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3471                                             (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3472                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3473                                         }
3474                                         SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT);
3475                                         SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3476                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3477                                             netp);
3478                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3479                                             asoc->asoc.primary_destination);
3480                                         sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3481                                 }
3482                         } else {
3483                                 /* mark into shutdown pending */
3484                                 struct sctp_stream_queue_pending *sp;
3485
3486                                 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3487                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3488                                     asoc->asoc.primary_destination);
3489                                 if (asoc->asoc.locked_on_sending) {
3490                                         sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3491                                             sctp_streamhead);
3492                                         if (sp == NULL) {
3493                                                 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
3494                                                     asoc->asoc.locked_on_sending,
3495                                                     asoc->asoc.locked_on_sending->stream_no);
3496                                         } else {
3497                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
3498                                                         asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3499                                         }
3500                                 }
3501                                 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3502                                     TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3503                                     (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3504                                         struct mbuf *op_err;
3505
3506                         abort_anyway:
3507                                         op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3508                                             0, M_DONTWAIT, 1, MT_DATA);
3509                                         if (op_err) {
3510                                                 /*
3511                                                  * Fill in the user
3512                                                  * initiated abort
3513                                                  */
3514                                                 struct sctp_paramhdr *ph;
3515                                                 uint32_t *ippp;
3516
3517                                                 SCTP_BUF_LEN(op_err) =
3518                                                     (sizeof(struct sctp_paramhdr) +
3519                                                     sizeof(uint32_t));
3520                                                 ph = mtod(op_err,
3521                                                     struct sctp_paramhdr *);
3522                                                 ph->param_type = htons(
3523                                                     SCTP_CAUSE_USER_INITIATED_ABT);
3524                                                 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3525                                                 ippp = (uint32_t *) (ph + 1);
3526                                                 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
3527                                         }
3528                                         asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3529 #if defined(SCTP_PANIC_ON_ABORT)
3530                                         panic("inpcb_free does an abort");
3531 #endif
3532
3533                                         sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3534                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3535                                         if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3536                                             (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3537                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3538                                         }
3539                                         if (sctp_free_assoc(inp, asoc,
3540                                             SCTP_PCBFREE_NOFORCE,
3541                                             SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3542                                                 cnt_in_sd++;
3543                                         }
3544                                         continue;
3545                                 } else {
3546                                         sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3547                                 }
3548                         }
3549                         cnt_in_sd++;
3550                         SCTP_TCB_UNLOCK(asoc);
3551                 }
3552                 /* now is there some left in our SHUTDOWN state? */
3553                 if (cnt_in_sd) {
3554 #ifdef SCTP_LOG_CLOSING
3555                         sctp_log_closing(inp, NULL, 2);
3556 #endif
3557                         inp->sctp_socket = NULL;
3558                         SCTP_INP_WUNLOCK(inp);
3559                         SCTP_ASOC_CREATE_UNLOCK(inp);
3560                         SCTP_INP_INFO_WUNLOCK();
3561                         return;
3562                 }
3563         }
3564         inp->sctp_socket = NULL;
3565         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3566             SCTP_PCB_FLAGS_UNBOUND) {
3567                 /*
3568                  * ok, this guy has been bound. It's port is somewhere in
3569                  * the SCTP_BASE_INFO(hash table). Remove it!
3570                  */
3571                 LIST_REMOVE(inp, sctp_hash);
3572                 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3573         }
3574         /*
3575          * If there is a timer running to kill us, forget it, since it may
3576          * have a contest on the INP lock.. which would cause us to die ...
3577          */
3578         cnt = 0;
3579         LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3580                 SCTP_TCB_LOCK(asoc);
3581                 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3582                         if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3583                                 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3584                                 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3585                         }
3586                         cnt++;
3587                         SCTP_TCB_UNLOCK(asoc);
3588                         continue;
3589                 }
3590                 /* Free associations that are NOT killing us */
3591                 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3592                     ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3593                         struct mbuf *op_err;
3594                         uint32_t *ippp;
3595
3596                         op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3597                             0, M_DONTWAIT, 1, MT_DATA);
3598                         if (op_err) {
3599                                 /* Fill in the user initiated abort */
3600                                 struct sctp_paramhdr *ph;
3601
3602                                 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
3603                                     sizeof(uint32_t));
3604                                 ph = mtod(op_err, struct sctp_paramhdr *);
3605                                 ph->param_type = htons(
3606                                     SCTP_CAUSE_USER_INITIATED_ABT);
3607                                 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3608                                 ippp = (uint32_t *) (ph + 1);
3609                                 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
3610
3611                         }
3612                         asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3613 #if defined(SCTP_PANIC_ON_ABORT)
3614                         panic("inpcb_free does an abort");
3615 #endif
3616                         sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3617                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3618                 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3619                         cnt++;
3620                         SCTP_TCB_UNLOCK(asoc);
3621                         continue;
3622                 }
3623                 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3624                     (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3625                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3626                 }
3627                 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3628                         cnt++;
3629                 }
3630         }
3631         if (cnt) {
3632                 /* Ok we have someone out there that will kill us */
3633                 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3634 #ifdef SCTP_LOG_CLOSING
3635                 sctp_log_closing(inp, NULL, 3);
3636 #endif
3637                 SCTP_INP_WUNLOCK(inp);
3638                 SCTP_ASOC_CREATE_UNLOCK(inp);
3639                 SCTP_INP_INFO_WUNLOCK();
3640                 return;
3641         }
3642         if (SCTP_INP_LOCK_CONTENDED(inp))
3643                 being_refed++;
3644         if (SCTP_INP_READ_CONTENDED(inp))
3645                 being_refed++;
3646         if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
3647                 being_refed++;
3648
3649         if ((inp->refcount) ||
3650             (being_refed) ||
3651             (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3652                 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3653 #ifdef SCTP_LOG_CLOSING
3654                 sctp_log_closing(inp, NULL, 4);
3655 #endif
3656                 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3657                 SCTP_INP_WUNLOCK(inp);
3658                 SCTP_ASOC_CREATE_UNLOCK(inp);
3659                 SCTP_INP_INFO_WUNLOCK();
3660                 return;
3661         }
3662         inp->sctp_ep.signature_change.type = 0;
3663         inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3664         /*
3665          * Remove it from the list .. last thing we need a lock for.
3666          */
3667         LIST_REMOVE(inp, sctp_list);
3668         SCTP_INP_WUNLOCK(inp);
3669         SCTP_ASOC_CREATE_UNLOCK(inp);
3670         SCTP_INP_INFO_WUNLOCK();
3671         /*
3672          * Now we release all locks. Since this INP cannot be found anymore
3673          * except possibly by the kill timer that might be running. We call
3674          * the drain function here. It should hit the case were it sees the
3675          * ACTIVE flag cleared and exit out freeing us to proceed and
3676          * destroy everything.
3677          */
3678         if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
3679                 (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
3680         } else {
3681                 /* Probably un-needed */
3682                 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3683         }
3684
3685 #ifdef SCTP_LOG_CLOSING
3686         sctp_log_closing(inp, NULL, 5);
3687 #endif
3688
3689
3690         if ((inp->sctp_asocidhash) != NULL) {
3691                 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
3692                 inp->sctp_asocidhash = NULL;
3693         }
3694         /* sa_ignore FREED_MEMORY */
3695         TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
3696                 /* Its only abandoned if it had data left */
3697                 if (sq->length)
3698                         SCTP_STAT_INCR(sctps_left_abandon);
3699
3700                 TAILQ_REMOVE(&inp->read_queue, sq, next);
3701                 sctp_free_remote_addr(sq->whoFrom);
3702                 if (so)
3703                         so->so_rcv.sb_cc -= sq->length;
3704                 if (sq->data) {
3705                         sctp_m_freem(sq->data);
3706                         sq->data = NULL;
3707                 }
3708                 /*
3709                  * no need to free the net count, since at this point all
3710                  * assoc's are gone.
3711                  */
3712                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
3713                 SCTP_DECR_READQ_COUNT();
3714         }
3715         /* Now the sctp_pcb things */
3716         /*
3717          * free each asoc if it is not already closed/free. we can't use the
3718          * macro here since le_next will get freed as part of the
3719          * sctp_free_assoc() call.
3720          */
3721         cnt = 0;
3722         if (so) {
3723 #ifdef IPSEC
3724                 ipsec_delete_pcbpolicy(ip_pcb);
3725 #endif                          /* IPSEC */
3726
3727                 /* Unlocks not needed since the socket is gone now */
3728         }
3729         if (ip_pcb->inp_options) {
3730                 (void)sctp_m_free(ip_pcb->inp_options);
3731                 ip_pcb->inp_options = 0;
3732         }
3733 #ifdef INET6
3734         if (ip_pcb->inp_vflag & INP_IPV6) {
3735                 struct in6pcb *in6p;
3736
3737                 in6p = (struct in6pcb *)inp;
3738                 ip6_freepcbopts(in6p->in6p_outputopts);
3739         }
3740 #endif                          /* INET6 */
3741         ip_pcb->inp_vflag = 0;
3742         /* free up authentication fields */
3743         if (inp->sctp_ep.local_auth_chunks != NULL)
3744                 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3745         if (inp->sctp_ep.local_hmacs != NULL)
3746                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3747
3748         LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
3749                 LIST_REMOVE(shared_key, next);
3750                 sctp_free_sharedkey(shared_key);
3751                 /* sa_ignore FREED_MEMORY */
3752         }
3753
3754         /*
3755          * if we have an address list the following will free the list of
3756          * ifaddr's that are set into this ep. Again macro limitations here,
3757          * since the LIST_FOREACH could be a bad idea.
3758          */
3759         LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3760                 sctp_remove_laddr(laddr);
3761         }
3762
3763 #ifdef SCTP_TRACK_FREED_ASOCS
3764         /* TEMP CODE */
3765         LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
3766                 LIST_REMOVE(asoc, sctp_tcblist);
3767                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
3768                 SCTP_DECR_ASOC_COUNT();
3769         }
3770         /* *** END TEMP CODE *** */
3771 #endif
3772         /* Now lets see about freeing the EP hash table. */
3773         if (inp->sctp_tcbhash != NULL) {
3774                 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3775                 inp->sctp_tcbhash = NULL;
3776         }
3777         /* Now we must put the ep memory back into the zone pool */
3778         INP_LOCK_DESTROY(&inp->ip_inp.inp);
3779         SCTP_INP_LOCK_DESTROY(inp);
3780         SCTP_INP_READ_DESTROY(inp);
3781         SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3782         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3783         SCTP_DECR_EP_COUNT();
3784 }
3785
3786
3787 struct sctp_nets *
3788 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3789 {
3790         struct sctp_nets *net;
3791
3792         /* locate the address */
3793         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3794                 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3795                         return (net);
3796         }
3797         return (NULL);
3798 }
3799
3800
3801 int
3802 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3803 {
3804         struct sctp_ifa *sctp_ifa;
3805
3806         sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3807         if (sctp_ifa) {
3808                 return (1);
3809         } else {
3810                 return (0);
3811         }
3812 }
3813
3814 /*
3815  * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3816  * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3817  * stats of stuff.
3818  */
3819 int
3820 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3821     struct sctp_nets **netp, int set_scope, int from)
3822 {
3823         /*
3824          * The following is redundant to the same lines in the
3825          * sctp_aloc_assoc() but is needed since others call the add address
3826          * function
3827          */
3828         struct sctp_nets *net, *netfirst;
3829         int addr_inscope;
3830
3831         SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3832             from);
3833         SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3834
3835         netfirst = sctp_findnet(stcb, newaddr);
3836         if (netfirst) {
3837                 /*
3838                  * Lie and return ok, we don't want to make the association
3839                  * go away for this behavior. It will happen in the TCP
3840                  * model in a connected socket. It does not reach the hash
3841                  * table until after the association is built so it can't be
3842                  * found. Mark as reachable, since the initial creation will
3843                  * have been cleared and the NOT_IN_ASSOC flag will have
3844                  * been added... and we don't want to end up removing it
3845                  * back out.
3846                  */
3847                 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3848                         netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3849                             SCTP_ADDR_UNCONFIRMED);
3850                 } else {
3851                         netfirst->dest_state = SCTP_ADDR_REACHABLE;
3852                 }
3853
3854                 return (0);
3855         }
3856         addr_inscope = 1;
3857         switch (newaddr->sa_family) {
3858 #ifdef INET
3859         case AF_INET:
3860                 {
3861                         struct sockaddr_in *sin;
3862
3863                         sin = (struct sockaddr_in *)newaddr;
3864                         if (sin->sin_addr.s_addr == 0) {
3865                                 /* Invalid address */
3866                                 return (-1);
3867                         }
3868                         /* zero out the bzero area */
3869                         memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3870
3871                         /* assure len is set */
3872                         sin->sin_len = sizeof(struct sockaddr_in);
3873                         if (set_scope) {
3874 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3875                                 stcb->ipv4_local_scope = 1;
3876 #else
3877                                 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3878                                         stcb->asoc.ipv4_local_scope = 1;
3879                                 }
3880 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3881                         } else {
3882                                 /* Validate the address is in scope */
3883                                 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3884                                     (stcb->asoc.ipv4_local_scope == 0)) {
3885                                         addr_inscope = 0;
3886                                 }
3887                         }
3888                         break;
3889                 }
3890 #endif
3891 #ifdef INET6
3892         case AF_INET6:
3893                 {
3894                         struct sockaddr_in6 *sin6;
3895
3896                         sin6 = (struct sockaddr_in6 *)newaddr;
3897                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3898                                 /* Invalid address */
3899                                 return (-1);
3900                         }
3901                         /* assure len is set */
3902                         sin6->sin6_len = sizeof(struct sockaddr_in6);
3903                         if (set_scope) {
3904                                 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3905                                         stcb->asoc.loopback_scope = 1;
3906                                         stcb->asoc.local_scope = 0;
3907                                         stcb->asoc.ipv4_local_scope = 1;
3908                                         stcb->asoc.site_scope = 1;
3909                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3910                                         /*
3911                                          * If the new destination is a
3912                                          * LINK_LOCAL we must have common
3913                                          * site scope. Don't set the local
3914                                          * scope since we may not share all
3915                                          * links, only loopback can do this.
3916                                          * Links on the local network would
3917                                          * also be on our private network
3918                                          * for v4 too.
3919                                          */
3920                                         stcb->asoc.ipv4_local_scope = 1;
3921                                         stcb->asoc.site_scope = 1;
3922                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3923                                         /*
3924                                          * If the new destination is
3925                                          * SITE_LOCAL then we must have site
3926                                          * scope in common.
3927                                          */
3928                                         stcb->asoc.site_scope = 1;
3929                                 }
3930                         } else {
3931                                 /* Validate the address is in scope */
3932                                 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3933                                     (stcb->asoc.loopback_scope == 0)) {
3934                                         addr_inscope = 0;
3935                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3936                                     (stcb->asoc.local_scope == 0)) {
3937                                         addr_inscope = 0;
3938                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3939                                     (stcb->asoc.site_scope == 0)) {
3940                                         addr_inscope = 0;
3941                                 }
3942                         }
3943                         break;
3944                 }
3945 #endif
3946         default:
3947                 /* not supported family type */
3948                 return (-1);
3949         }
3950         net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3951         if (net == NULL) {
3952                 return (-1);
3953         }
3954         SCTP_INCR_RADDR_COUNT();
3955         bzero(net, sizeof(struct sctp_nets));
3956         (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3957         memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3958         switch (newaddr->sa_family) {
3959 #ifdef INET
3960         case AF_INET:
3961                 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3962                 break;
3963 #endif
3964 #ifdef INET6
3965         case AF_INET6:
3966                 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3967                 break;
3968 #endif
3969         default:
3970                 break;
3971         }
3972         net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3973         if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3974                 stcb->asoc.loopback_scope = 1;
3975                 stcb->asoc.ipv4_local_scope = 1;
3976                 stcb->asoc.local_scope = 0;
3977                 stcb->asoc.site_scope = 1;
3978                 addr_inscope = 1;
3979         }
3980         net->failure_threshold = stcb->asoc.def_net_failure;
3981         net->pf_threshold = stcb->asoc.def_net_pf_threshold;
3982         if (addr_inscope == 0) {
3983                 net->dest_state = (SCTP_ADDR_REACHABLE |
3984                     SCTP_ADDR_OUT_OF_SCOPE);
3985         } else {
3986                 if (from == SCTP_ADDR_IS_CONFIRMED)
3987                         /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3988                         net->dest_state = SCTP_ADDR_REACHABLE;
3989                 else
3990                         net->dest_state = SCTP_ADDR_REACHABLE |
3991                             SCTP_ADDR_UNCONFIRMED;
3992         }
3993         /*
3994          * We set this to 0, the timer code knows that this means its an
3995          * initial value
3996          */
3997         net->rto_needed = 1;
3998         net->RTO = 0;
3999         net->RTO_measured = 0;
4000         stcb->asoc.numnets++;
4001         net->ref_count = 1;
4002         net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
4003         net->port = stcb->asoc.port;
4004         net->dscp = stcb->asoc.default_dscp;
4005 #ifdef INET6
4006         net->flowlabel = stcb->asoc.default_flowlabel;
4007 #endif
4008         if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4009                 net->dest_state |= SCTP_ADDR_NOHB;
4010         } else {
4011                 net->dest_state &= ~SCTP_ADDR_NOHB;
4012         }
4013         if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4014                 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4015         } else {
4016                 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4017         }
4018         net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4019         /* Init the timer structure */
4020         SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4021         SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4022         SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4023
4024         /* Now generate a route for this guy */
4025 #ifdef INET6
4026         /* KAME hack: embed scopeid */
4027         if (newaddr->sa_family == AF_INET6) {
4028                 struct sockaddr_in6 *sin6;
4029
4030                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4031                 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4032                 sin6->sin6_scope_id = 0;
4033         }
4034 #endif
4035         SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
4036
4037         if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4038                 /* Get source address */
4039                 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
4040                     stcb,
4041                     (sctp_route_t *) & net->ro,
4042                     net,
4043                     0,
4044                     stcb->asoc.vrf_id);
4045                 /* Now get the interface MTU */
4046                 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
4047                         net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
4048                 }
4049                 if (net->mtu > 0) {
4050                         uint32_t rmtu;
4051
4052                         rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
4053                         if (rmtu == 0) {
4054                                 /*
4055                                  * Start things off to match mtu of
4056                                  * interface please.
4057                                  */
4058                                 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
4059                                     net->ro.ro_rt, net->mtu);
4060                         } else {
4061                                 /*
4062                                  * we take the route mtu over the interface,
4063                                  * since the route may be leading out the
4064                                  * loopback, or a different interface.
4065                                  */
4066                                 net->mtu = rmtu;
4067                         }
4068                 }
4069         }
4070         if (net->mtu == 0) {
4071                 switch (newaddr->sa_family) {
4072 #ifdef INET
4073                 case AF_INET:
4074                         net->mtu = SCTP_DEFAULT_MTU;
4075                         break;
4076 #endif
4077 #ifdef INET6
4078                 case AF_INET6:
4079                         net->mtu = 1280;
4080                         break;
4081 #endif
4082                 default:
4083                         break;
4084                 }
4085         }
4086         if (net->port) {
4087                 net->mtu -= (uint32_t) sizeof(struct udphdr);
4088         }
4089         if (from == SCTP_ALLOC_ASOC) {
4090                 stcb->asoc.smallest_mtu = net->mtu;
4091         }
4092         if (stcb->asoc.smallest_mtu > net->mtu) {
4093                 stcb->asoc.smallest_mtu = net->mtu;
4094         }
4095 #ifdef INET6
4096         if (newaddr->sa_family == AF_INET6) {
4097                 struct sockaddr_in6 *sin6;
4098
4099                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4100                 (void)sa6_recoverscope(sin6);
4101         }
4102 #endif
4103
4104         /* JRS - Use the congestion control given in the CC module */
4105         if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
4106                 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param) (stcb, net);
4107
4108         /*
4109          * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4110          * of assoc (2005/06/27, iyengar@cis.udel.edu)
4111          */
4112         net->find_pseudo_cumack = 1;
4113         net->find_rtx_pseudo_cumack = 1;
4114         net->src_addr_selected = 0;
4115         /* Choose an initial flowid. */
4116         net->flowid = stcb->asoc.my_vtag ^
4117             ntohs(stcb->rport) ^
4118             ntohs(stcb->sctp_ep->sctp_lport);
4119 #ifdef INVARIANTS
4120         net->flowidset = 1;
4121 #endif
4122         if (netp) {
4123                 *netp = net;
4124         }
4125         netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4126         if (net->ro.ro_rt == NULL) {
4127                 /* Since we have no route put it at the back */
4128                 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4129         } else if (netfirst == NULL) {
4130                 /* We are the first one in the pool. */
4131                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4132         } else if (netfirst->ro.ro_rt == NULL) {
4133                 /*
4134                  * First one has NO route. Place this one ahead of the first
4135                  * one.
4136                  */
4137                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4138         } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4139                 /*
4140                  * This one has a different interface than the one at the
4141                  * top of the list. Place it ahead.
4142                  */
4143                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4144         } else {
4145                 /*
4146                  * Ok we have the same interface as the first one. Move
4147                  * forward until we find either a) one with a NULL route...
4148                  * insert ahead of that b) one with a different ifp.. insert
4149                  * after that. c) end of the list.. insert at the tail.
4150                  */
4151                 struct sctp_nets *netlook;
4152
4153                 do {
4154                         netlook = TAILQ_NEXT(netfirst, sctp_next);
4155                         if (netlook == NULL) {
4156                                 /* End of the list */
4157                                 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4158                                 break;
4159                         } else if (netlook->ro.ro_rt == NULL) {
4160                                 /* next one has NO route */
4161                                 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4162                                 break;
4163                         } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
4164                                 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4165                                     net, sctp_next);
4166                                 break;
4167                         }
4168                         /* Shift forward */
4169                         netfirst = netlook;
4170                 } while (netlook != NULL);
4171         }
4172
4173         /* got to have a primary set */
4174         if (stcb->asoc.primary_destination == 0) {
4175                 stcb->asoc.primary_destination = net;
4176         } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4177                     (net->ro.ro_rt) &&
4178             ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4179                 /* No route to current primary adopt new primary */
4180                 stcb->asoc.primary_destination = net;
4181         }
4182         /* Validate primary is first */
4183         net = TAILQ_FIRST(&stcb->asoc.nets);
4184         if ((net != stcb->asoc.primary_destination) &&
4185             (stcb->asoc.primary_destination)) {
4186                 /*
4187                  * first one on the list is NOT the primary sctp_cmpaddr()
4188                  * is much more efficient if the primary is the first on the
4189                  * list, make it so.
4190                  */
4191                 TAILQ_REMOVE(&stcb->asoc.nets,
4192                     stcb->asoc.primary_destination, sctp_next);
4193                 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4194                     stcb->asoc.primary_destination, sctp_next);
4195         }
4196         return (0);
4197 }
4198
4199
4200 static uint32_t
4201 sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4202 {
4203         uint32_t id;
4204         struct sctpasochead *head;
4205         struct sctp_tcb *lstcb;
4206
4207         SCTP_INP_WLOCK(inp);
4208 try_again:
4209         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4210                 /* TSNH */
4211                 SCTP_INP_WUNLOCK(inp);
4212                 return (0);
4213         }
4214         /*
4215          * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4216          * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4217          */
4218         if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4219                 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4220         }
4221         id = inp->sctp_associd_counter;
4222         inp->sctp_associd_counter++;
4223         lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t) id, 0);
4224         if (lstcb) {
4225                 goto try_again;
4226         }
4227         head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4228         LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4229         stcb->asoc.in_asocid_hash = 1;
4230         SCTP_INP_WUNLOCK(inp);
4231         return id;
4232 }
4233
4234 /*
4235  * allocate an association and add it to the endpoint. The caller must be
4236  * careful to add all additional addresses once they are know right away or
4237  * else the assoc will be may experience a blackout scenario.
4238  */
4239 struct sctp_tcb *
4240 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4241     int *error, uint32_t override_tag, uint32_t vrf_id,
4242     struct thread *p
4243 )
4244 {
4245         /* note the p argument is only valid in unbound sockets */
4246
4247         struct sctp_tcb *stcb;
4248         struct sctp_association *asoc;
4249         struct sctpasochead *head;
4250         uint16_t rport;
4251         int err;
4252
4253         /*
4254          * Assumption made here: Caller has done a
4255          * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4256          * address does not exist already.
4257          */
4258         if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4259                 /* Hit max assoc, sorry no more */
4260                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4261                 *error = ENOBUFS;
4262                 return (NULL);
4263         }
4264         if (firstaddr == NULL) {
4265                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4266                 *error = EINVAL;
4267                 return (NULL);
4268         }
4269         SCTP_INP_RLOCK(inp);
4270         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4271             ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4272             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4273                 /*
4274                  * If its in the TCP pool, its NOT allowed to create an
4275                  * association. The parent listener needs to call
4276                  * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4277                  * off, or connected one does this.. its an error.
4278                  */
4279                 SCTP_INP_RUNLOCK(inp);
4280                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4281                 *error = EINVAL;
4282                 return (NULL);
4283         }
4284         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4285             (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4286                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4287                     (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4288                         SCTP_INP_RUNLOCK(inp);
4289                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4290                         *error = EINVAL;
4291                         return (NULL);
4292                 }
4293         }
4294         SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4295 #ifdef SCTP_DEBUG
4296         if (firstaddr) {
4297                 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4298                 switch (firstaddr->sa_family) {
4299 #ifdef INET
4300                 case AF_INET:
4301                         SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4302                             ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4303                         break;
4304 #endif
4305 #ifdef INET6
4306                 case AF_INET6:
4307                         SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4308                             ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4309                         break;
4310 #endif
4311                 default:
4312                         break;
4313                 }
4314         } else {
4315                 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
4316         }
4317 #endif                          /* SCTP_DEBUG */
4318         switch (firstaddr->sa_family) {
4319 #ifdef INET
4320         case AF_INET:
4321                 {
4322                         struct sockaddr_in *sin;
4323
4324                         sin = (struct sockaddr_in *)firstaddr;
4325                         if ((ntohs(sin->sin_port) == 0) ||
4326                             (sin->sin_addr.s_addr == INADDR_ANY) ||
4327                             (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
4328                             IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
4329                                 /* Invalid address */
4330                                 SCTP_INP_RUNLOCK(inp);
4331                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4332                                 *error = EINVAL;
4333                                 return (NULL);
4334                         }
4335                         rport = sin->sin_port;
4336                         break;
4337                 }
4338 #endif
4339 #ifdef INET6
4340         case AF_INET6:
4341                 {
4342                         struct sockaddr_in6 *sin6;
4343
4344                         sin6 = (struct sockaddr_in6 *)firstaddr;
4345                         if ((ntohs(sin6->sin6_port) == 0) ||
4346                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4347                             IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4348                                 /* Invalid address */
4349                                 SCTP_INP_RUNLOCK(inp);
4350                                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4351                                 *error = EINVAL;
4352                                 return (NULL);
4353                         }
4354                         rport = sin6->sin6_port;
4355                         break;
4356                 }
4357 #endif
4358         default:
4359                 /* not supported family type */
4360                 SCTP_INP_RUNLOCK(inp);
4361                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4362                 *error = EINVAL;
4363                 return (NULL);
4364         }
4365         SCTP_INP_RUNLOCK(inp);
4366         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4367                 /*
4368                  * If you have not performed a bind, then we need to do the
4369                  * ephemeral bind for you.
4370                  */
4371                 if ((err = sctp_inpcb_bind(inp->sctp_socket,
4372                     (struct sockaddr *)NULL,
4373                     (struct sctp_ifa *)NULL,
4374                     p
4375                     ))) {
4376                         /* bind error, probably perm */
4377                         *error = err;
4378                         return (NULL);
4379                 }
4380         }
4381         stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4382         if (stcb == NULL) {
4383                 /* out of memory? */
4384                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4385                 *error = ENOMEM;
4386                 return (NULL);
4387         }
4388         SCTP_INCR_ASOC_COUNT();
4389
4390         bzero(stcb, sizeof(*stcb));
4391         asoc = &stcb->asoc;
4392
4393         asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4394         SCTP_TCB_LOCK_INIT(stcb);
4395         SCTP_TCB_SEND_LOCK_INIT(stcb);
4396         stcb->rport = rport;
4397         /* setup back pointer's */
4398         stcb->sctp_ep = inp;
4399         stcb->sctp_socket = inp->sctp_socket;
4400         if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) {
4401                 /* failed */
4402                 SCTP_TCB_LOCK_DESTROY(stcb);
4403                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4404                 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4405                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4406                 SCTP_DECR_ASOC_COUNT();
4407                 *error = err;
4408                 return (NULL);
4409         }
4410         /* and the port */
4411         SCTP_INP_INFO_WLOCK();
4412         SCTP_INP_WLOCK(inp);
4413         if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4414                 /* inpcb freed while alloc going on */
4415                 SCTP_TCB_LOCK_DESTROY(stcb);
4416                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4417                 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4418                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4419                 SCTP_INP_WUNLOCK(inp);
4420                 SCTP_INP_INFO_WUNLOCK();
4421                 SCTP_DECR_ASOC_COUNT();
4422                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4423                 *error = EINVAL;
4424                 return (NULL);
4425         }
4426         SCTP_TCB_LOCK(stcb);
4427
4428         /* now that my_vtag is set, add it to the hash */
4429         head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4430         /* put it in the bucket in the vtag hash of assoc's for the system */
4431         LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4432         SCTP_INP_INFO_WUNLOCK();
4433
4434         if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4435                 /* failure.. memory error? */
4436                 if (asoc->strmout) {
4437                         SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4438                         asoc->strmout = NULL;
4439                 }
4440                 if (asoc->mapping_array) {
4441                         SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4442                         asoc->mapping_array = NULL;
4443                 }
4444                 if (asoc->nr_mapping_array) {
4445                         SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4446                         asoc->nr_mapping_array = NULL;
4447                 }
4448                 SCTP_DECR_ASOC_COUNT();
4449                 SCTP_TCB_LOCK_DESTROY(stcb);
4450                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4451                 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4452                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4453                 SCTP_INP_WUNLOCK(inp);
4454                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4455                 *error = ENOBUFS;
4456                 return (NULL);
4457         }
4458         /* Init all the timers */
4459         SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4460         SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4461         SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4462         SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4463         SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4464         SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
4465         SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4466
4467         LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4468         /* now file the port under the hash as well */
4469         if (inp->sctp_tcbhash != NULL) {
4470                 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4471                     inp->sctp_hashmark)];
4472                 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4473         }
4474         SCTP_INP_WUNLOCK(inp);
4475         SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
4476         return (stcb);
4477 }
4478
4479
4480 void
4481 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4482 {
4483         struct sctp_association *asoc;
4484
4485         asoc = &stcb->asoc;
4486         asoc->numnets--;
4487         TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4488         if (net == asoc->primary_destination) {
4489                 /* Reset primary */
4490                 struct sctp_nets *lnet;
4491
4492                 lnet = TAILQ_FIRST(&asoc->nets);
4493                 /*
4494                  * Mobility adaptation Ideally, if deleted destination is
4495                  * the primary, it becomes a fast retransmission trigger by
4496                  * the subsequent SET PRIMARY. (by micchie)
4497                  */
4498                 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4499                     SCTP_MOBILITY_BASE) ||
4500                     sctp_is_mobility_feature_on(stcb->sctp_ep,
4501                     SCTP_MOBILITY_FASTHANDOFF)) {
4502                         SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4503                         if (asoc->deleted_primary != NULL) {
4504                                 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4505                                 goto out;
4506                         }
4507                         asoc->deleted_primary = net;
4508                         atomic_add_int(&net->ref_count, 1);
4509                         memset(&net->lastsa, 0, sizeof(net->lastsa));
4510                         memset(&net->lastsv, 0, sizeof(net->lastsv));
4511                         sctp_mobility_feature_on(stcb->sctp_ep,
4512                             SCTP_MOBILITY_PRIM_DELETED);
4513                         sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4514                             stcb->sctp_ep, stcb, NULL);
4515                 }
4516 out:
4517                 /* Try to find a confirmed primary */
4518                 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4519         }
4520         if (net == asoc->last_data_chunk_from) {
4521                 /* Reset primary */
4522                 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4523         }
4524         if (net == asoc->last_control_chunk_from) {
4525                 /* Clear net */
4526                 asoc->last_control_chunk_from = NULL;
4527         }
4528         if (net == stcb->asoc.alternate) {
4529                 sctp_free_remote_addr(stcb->asoc.alternate);
4530                 stcb->asoc.alternate = NULL;
4531         }
4532         sctp_free_remote_addr(net);
4533 }
4534
4535 /*
4536  * remove a remote endpoint address from an association, it will fail if the
4537  * address does not exist.
4538  */
4539 int
4540 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4541 {
4542         /*
4543          * Here we need to remove a remote address. This is quite simple, we
4544          * first find it in the list of address for the association
4545          * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4546          * on that item. Note we do not allow it to be removed if there are
4547          * no other addresses.
4548          */
4549         struct sctp_association *asoc;
4550         struct sctp_nets *net, *nnet;
4551
4552         asoc = &stcb->asoc;
4553
4554         /* locate the address */
4555         TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
4556                 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4557                         continue;
4558                 }
4559                 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4560                     remaddr)) {
4561                         /* we found the guy */
4562                         if (asoc->numnets < 2) {
4563                                 /* Must have at LEAST two remote addresses */
4564                                 return (-1);
4565                         } else {
4566                                 sctp_remove_net(stcb, net);
4567                                 return (0);
4568                         }
4569                 }
4570         }
4571         /* not found. */
4572         return (-2);
4573 }
4574
4575 void
4576 sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4577 {
4578         struct sctpvtaghead *chain;
4579         struct sctp_tagblock *twait_block;
4580         int found = 0;
4581         int i;
4582
4583         chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4584         if (!LIST_EMPTY(chain)) {
4585                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4586                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4587                                 if ((twait_block->vtag_block[i].v_tag == tag) &&
4588                                     (twait_block->vtag_block[i].lport == lport) &&
4589                                     (twait_block->vtag_block[i].rport == rport)) {
4590                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
4591                                         twait_block->vtag_block[i].v_tag = 0;
4592                                         twait_block->vtag_block[i].lport = 0;
4593                                         twait_block->vtag_block[i].rport = 0;
4594                                         found = 1;
4595                                         break;
4596                                 }
4597                         }
4598                         if (found)
4599                                 break;
4600                 }
4601         }
4602 }
4603
4604 int
4605 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4606 {
4607         struct sctpvtaghead *chain;
4608         struct sctp_tagblock *twait_block;
4609         int found = 0;
4610         int i;
4611
4612         SCTP_INP_INFO_WLOCK();
4613         chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4614         if (!LIST_EMPTY(chain)) {
4615                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4616                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4617                                 if ((twait_block->vtag_block[i].v_tag == tag) &&
4618                                     (twait_block->vtag_block[i].lport == lport) &&
4619                                     (twait_block->vtag_block[i].rport == rport)) {
4620                                         found = 1;
4621                                         break;
4622                                 }
4623                         }
4624                         if (found)
4625                                 break;
4626                 }
4627         }
4628         SCTP_INP_INFO_WUNLOCK();
4629         return (found);
4630 }
4631
4632
4633 void
4634 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
4635 {
4636         struct sctpvtaghead *chain;
4637         struct sctp_tagblock *twait_block;
4638         struct timeval now;
4639         int set, i;
4640
4641         if (time == 0) {
4642                 /* Its disabled */
4643                 return;
4644         }
4645         (void)SCTP_GETTIME_TIMEVAL(&now);
4646         chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4647         set = 0;
4648         if (!LIST_EMPTY(chain)) {
4649                 /* Block(s) present, lets find space, and expire on the fly */
4650                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4651                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4652                                 if ((twait_block->vtag_block[i].v_tag == 0) &&
4653                                     !set) {
4654                                         twait_block->vtag_block[i].tv_sec_at_expire =
4655                                             now.tv_sec + time;
4656                                         twait_block->vtag_block[i].v_tag = tag;
4657                                         twait_block->vtag_block[i].lport = lport;
4658                                         twait_block->vtag_block[i].rport = rport;
4659                                         set = 1;
4660                                 } else if ((twait_block->vtag_block[i].v_tag) &&
4661                                     ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4662                                         /* Audit expires this guy */
4663                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
4664                                         twait_block->vtag_block[i].v_tag = 0;
4665                                         twait_block->vtag_block[i].lport = 0;
4666                                         twait_block->vtag_block[i].rport = 0;
4667                                         if (set == 0) {
4668                                                 /* Reuse it for my new tag */
4669                                                 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
4670                                                 twait_block->vtag_block[i].v_tag = tag;
4671                                                 twait_block->vtag_block[i].lport = lport;
4672                                                 twait_block->vtag_block[i].rport = rport;
4673                                                 set = 1;
4674                                         }
4675                                 }
4676                         }
4677                         if (set) {
4678                                 /*
4679                                  * We only do up to the block where we can
4680                                  * place our tag for audits
4681                                  */
4682                                 break;
4683                         }
4684                 }
4685         }
4686         /* Need to add a new block to chain */
4687         if (!set) {
4688                 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4689                     sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4690                 if (twait_block == NULL) {
4691 #ifdef INVARIANTS
4692                         panic("Can not alloc tagblock");
4693 #endif
4694                         return;
4695                 }
4696                 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4697                 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4698                 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4699                 twait_block->vtag_block[0].v_tag = tag;
4700                 twait_block->vtag_block[0].lport = lport;
4701                 twait_block->vtag_block[0].rport = rport;
4702         }
4703 }
4704
4705
4706
4707 /*-
4708  * Free the association after un-hashing the remote port. This
4709  * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4710  * expect that the input to this function IS a locked TCB.
4711  * It will return 0, if it did NOT destroy the association (instead
4712  * it unlocks it. It will return NON-zero if it either destroyed the
4713  * association OR the association is already destroyed.
4714  */
4715 int
4716 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4717 {
4718         int i;
4719         struct sctp_association *asoc;
4720         struct sctp_nets *net, *nnet;
4721         struct sctp_laddr *laddr, *naddr;
4722         struct sctp_tmit_chunk *chk, *nchk;
4723         struct sctp_asconf_addr *aparam, *naparam;
4724         struct sctp_asconf_ack *aack, *naack;
4725         struct sctp_stream_reset_list *strrst, *nstrrst;
4726         struct sctp_queued_to_read *sq, *nsq;
4727         struct sctp_stream_queue_pending *sp, *nsp;
4728         sctp_sharedkey_t *shared_key, *nshared_key;
4729         struct socket *so;
4730
4731         /* first, lets purge the entry from the hash table. */
4732
4733 #ifdef SCTP_LOG_CLOSING
4734         sctp_log_closing(inp, stcb, 6);
4735 #endif
4736         if (stcb->asoc.state == 0) {
4737 #ifdef SCTP_LOG_CLOSING
4738                 sctp_log_closing(inp, NULL, 7);
4739 #endif
4740                 /* there is no asoc, really TSNH :-0 */
4741                 return (1);
4742         }
4743         if (stcb->asoc.alternate) {
4744                 sctp_free_remote_addr(stcb->asoc.alternate);
4745                 stcb->asoc.alternate = NULL;
4746         }
4747         /* TEMP CODE */
4748         if (stcb->freed_from_where == 0) {
4749                 /* Only record the first place free happened from */
4750                 stcb->freed_from_where = from_location;
4751         }
4752         /* TEMP CODE */
4753
4754         asoc = &stcb->asoc;
4755         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4756             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4757                 /* nothing around */
4758                 so = NULL;
4759         else
4760                 so = inp->sctp_socket;
4761
4762         /*
4763          * We used timer based freeing if a reader or writer is in the way.
4764          * So we first check if we are actually being called from a timer,
4765          * if so we abort early if a reader or writer is still in the way.
4766          */
4767         if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4768             (from_inpcbfree == SCTP_NORMAL_PROC)) {
4769                 /*
4770                  * is it the timer driving us? if so are the reader/writers
4771                  * gone?
4772                  */
4773                 if (stcb->asoc.refcnt) {
4774                         /* nope, reader or writer in the way */
4775                         sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4776                         /* no asoc destroyed */
4777                         SCTP_TCB_UNLOCK(stcb);
4778 #ifdef SCTP_LOG_CLOSING
4779                         sctp_log_closing(inp, stcb, 8);
4780 #endif
4781                         return (0);
4782                 }
4783         }
4784         /* now clean up any other timers */
4785         (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4786         asoc->dack_timer.self = NULL;
4787         (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4788         /*-
4789          * For stream reset we don't blast this unless
4790          * it is a str-reset timer, it might be the
4791          * free-asoc timer which we DON'T want to
4792          * disturb.
4793          */
4794         if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4795                 asoc->strreset_timer.self = NULL;
4796         (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4797         asoc->asconf_timer.self = NULL;
4798         (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4799         asoc->autoclose_timer.self = NULL;
4800         (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4801         asoc->shut_guard_timer.self = NULL;
4802         (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4803         asoc->delayed_event_timer.self = NULL;
4804         /* Mobility adaptation */
4805         (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4806         asoc->delete_prim_timer.self = NULL;
4807         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4808                 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4809                 net->rxt_timer.self = NULL;
4810                 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4811                 net->pmtu_timer.self = NULL;
4812                 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
4813                 net->hb_timer.self = NULL;
4814         }
4815         /* Now the read queue needs to be cleaned up (only once) */
4816         if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4817                 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4818                 SCTP_INP_READ_LOCK(inp);
4819                 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4820                         if (sq->stcb == stcb) {
4821                                 sq->do_not_ref_stcb = 1;
4822                                 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4823                                 /*
4824                                  * If there is no end, there never will be
4825                                  * now.
4826                                  */
4827                                 if (sq->end_added == 0) {
4828                                         /* Held for PD-API clear that. */
4829                                         sq->pdapi_aborted = 1;
4830                                         sq->held_length = 0;
4831                                         if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4832                                                 /*
4833                                                  * Need to add a PD-API
4834                                                  * aborted indication.
4835                                                  * Setting the control_pdapi
4836                                                  * assures that it will be
4837                                                  * added right after this
4838                                                  * msg.
4839                                                  */
4840                                                 uint32_t strseq;
4841
4842                                                 stcb->asoc.control_pdapi = sq;
4843                                                 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4844                                                 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
4845                                                     stcb,
4846                                                     SCTP_PARTIAL_DELIVERY_ABORTED,
4847                                                     (void *)&strseq,
4848                                                     SCTP_SO_LOCKED);
4849                                                 stcb->asoc.control_pdapi = NULL;
4850                                         }
4851                                 }
4852                                 /* Add an end to wake them */
4853                                 sq->end_added = 1;
4854                         }
4855                 }
4856                 SCTP_INP_READ_UNLOCK(inp);
4857                 if (stcb->block_entry) {
4858                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4859                         stcb->block_entry->error = ECONNRESET;
4860                         stcb->block_entry = NULL;
4861                 }
4862         }
4863         if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
4864                 /*
4865                  * Someone holds a reference OR the socket is unaccepted
4866                  * yet.
4867                  */
4868                 if ((stcb->asoc.refcnt) ||
4869                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4870                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4871                         stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4872                         sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4873                 }
4874                 SCTP_TCB_UNLOCK(stcb);
4875                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4876                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4877                         /* nothing around */
4878                         so = NULL;
4879                 if (so) {
4880                         /* Wake any reader/writers */
4881                         sctp_sorwakeup(inp, so);
4882                         sctp_sowwakeup(inp, so);
4883                 }
4884 #ifdef SCTP_LOG_CLOSING
4885                 sctp_log_closing(inp, stcb, 9);
4886 #endif
4887                 /* no asoc destroyed */
4888                 return (0);
4889         }
4890 #ifdef SCTP_LOG_CLOSING
4891         sctp_log_closing(inp, stcb, 10);
4892 #endif
4893         /*
4894          * When I reach here, no others want to kill the assoc yet.. and I
4895          * own the lock. Now its possible an abort comes in when I do the
4896          * lock exchange below to grab all the locks to do the final take
4897          * out. to prevent this we increment the count, which will start a
4898          * timer and blow out above thus assuring us that we hold exclusive
4899          * killing of the asoc. Note that after getting back the TCB lock we
4900          * will go ahead and increment the counter back up and stop any
4901          * timer a passing stranger may have started :-S
4902          */
4903         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4904                 atomic_add_int(&stcb->asoc.refcnt, 1);
4905
4906                 SCTP_TCB_UNLOCK(stcb);
4907                 SCTP_INP_INFO_WLOCK();
4908                 SCTP_INP_WLOCK(inp);
4909                 SCTP_TCB_LOCK(stcb);
4910         }
4911         /* Double check the GONE flag */
4912         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4913             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4914                 /* nothing around */
4915                 so = NULL;
4916
4917         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4918             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4919                 /*
4920                  * For TCP type we need special handling when we are
4921                  * connected. We also include the peel'ed off ones to.
4922                  */
4923                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4924                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4925                         inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4926                         if (so) {
4927                                 SOCK_LOCK(so);
4928                                 if (so->so_rcv.sb_cc == 0) {
4929                                         so->so_state &= ~(SS_ISCONNECTING |
4930                                             SS_ISDISCONNECTING |
4931                                             SS_ISCONFIRMING |
4932                                             SS_ISCONNECTED);
4933                                 }
4934                                 socantrcvmore_locked(so);
4935                                 sctp_sowwakeup(inp, so);
4936                                 sctp_sorwakeup(inp, so);
4937                                 SCTP_SOWAKEUP(so);
4938                         }
4939                 }
4940         }
4941         /*
4942          * Make it invalid too, that way if its about to run it will abort
4943          * and return.
4944          */
4945         /* re-increment the lock */
4946         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4947                 atomic_add_int(&stcb->asoc.refcnt, -1);
4948         }
4949         if (stcb->asoc.refcnt) {
4950                 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4951                 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4952                 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4953                         SCTP_INP_INFO_WUNLOCK();
4954                         SCTP_INP_WUNLOCK(inp);
4955                 }
4956                 SCTP_TCB_UNLOCK(stcb);
4957                 return (0);
4958         }
4959         asoc->state = 0;
4960         if (inp->sctp_tcbhash) {
4961                 LIST_REMOVE(stcb, sctp_tcbhash);
4962         }
4963         if (stcb->asoc.in_asocid_hash) {
4964                 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4965         }
4966         /* Now lets remove it from the list of ALL associations in the EP */
4967         LIST_REMOVE(stcb, sctp_tcblist);
4968         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4969                 SCTP_INP_INCR_REF(inp);
4970                 SCTP_INP_WUNLOCK(inp);
4971         }
4972         /* pull from vtag hash */
4973         LIST_REMOVE(stcb, sctp_asocs);
4974         sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
4975             inp->sctp_lport, stcb->rport);
4976
4977         /*
4978          * Now restop the timers to be sure this is paranoia at is finest!
4979          */
4980         (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4981         (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4982         (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4983         (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4984         (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4985         (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4986         (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4987         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4988                 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4989                 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4990                 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
4991         }
4992
4993         asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4994         /*
4995          * The chunk lists and such SHOULD be empty but we check them just
4996          * in case.
4997          */
4998         /* anything on the wheel needs to be removed */
4999         for (i = 0; i < asoc->streamoutcnt; i++) {
5000                 struct sctp_stream_out *outs;
5001
5002                 outs = &asoc->strmout[i];
5003                 /* now clean up any chunks here */
5004                 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
5005                         TAILQ_REMOVE(&outs->outqueue, sp, next);
5006                         if (sp->data) {
5007                                 if (so) {
5008                                         /* Still an open socket - report */
5009                                         sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
5010                                             SCTP_NOTIFY_DATAGRAM_UNSENT,
5011                                             (void *)sp, SCTP_SO_LOCKED);
5012                                 }
5013                                 if (sp->data) {
5014                                         sctp_m_freem(sp->data);
5015                                         sp->data = NULL;
5016                                         sp->tail_mbuf = NULL;
5017                                 }
5018                         }
5019                         if (sp->net) {
5020                                 sctp_free_remote_addr(sp->net);
5021                                 sp->net = NULL;
5022                         }
5023                         sctp_free_spbufspace(stcb, asoc, sp);
5024                         if (sp->holds_key_ref)
5025                                 sctp_auth_key_release(stcb, sp->auth_keyid, SCTP_SO_LOCKED);
5026                         /* Free the zone stuff  */
5027                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), sp);
5028                         SCTP_DECR_STRMOQ_COUNT();
5029                         /* sa_ignore FREED_MEMORY */
5030                 }
5031         }
5032         /* sa_ignore FREED_MEMORY */
5033         TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5034                 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5035                 SCTP_FREE(strrst, SCTP_M_STRESET);
5036         }
5037         TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5038                 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5039                 if (sq->data) {
5040                         sctp_m_freem(sq->data);
5041                         sq->data = NULL;
5042                 }
5043                 sctp_free_remote_addr(sq->whoFrom);
5044                 sq->whoFrom = NULL;
5045                 sq->stcb = NULL;
5046                 /* Free the ctl entry */
5047                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
5048                 SCTP_DECR_READQ_COUNT();
5049                 /* sa_ignore FREED_MEMORY */
5050         }
5051         TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5052                 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5053                 if (chk->data) {
5054                         sctp_m_freem(chk->data);
5055                         chk->data = NULL;
5056                 }
5057                 if (chk->holds_key_ref)
5058                         sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5059                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5060                 SCTP_DECR_CHK_COUNT();
5061                 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5062                 asoc->free_chunk_cnt--;
5063                 /* sa_ignore FREED_MEMORY */
5064         }
5065         /* pending send queue SHOULD be empty */
5066         TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5067                 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5068                 if (chk->data) {
5069                         if (so) {
5070                                 /* Still a socket? */
5071                                 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5072                                     SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_LOCKED);
5073                         }
5074                         if (chk->data) {
5075                                 sctp_m_freem(chk->data);
5076                                 chk->data = NULL;
5077                         }
5078                 }
5079                 if (chk->holds_key_ref)
5080                         sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5081                 if (chk->whoTo) {
5082                         sctp_free_remote_addr(chk->whoTo);
5083                         chk->whoTo = NULL;
5084                 }
5085                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5086                 SCTP_DECR_CHK_COUNT();
5087                 /* sa_ignore FREED_MEMORY */
5088         }
5089         /* sent queue SHOULD be empty */
5090         TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
5091                 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5092                 if (chk->data) {
5093                         if (so) {
5094                                 /* Still a socket? */
5095                                 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5096                                     SCTP_NOTIFY_DATAGRAM_SENT, chk, SCTP_SO_LOCKED);
5097                         }
5098                         if (chk->data) {
5099                                 sctp_m_freem(chk->data);
5100                                 chk->data = NULL;
5101                         }
5102                 }
5103                 if (chk->holds_key_ref)
5104                         sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5105                 sctp_free_remote_addr(chk->whoTo);
5106                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5107                 SCTP_DECR_CHK_COUNT();
5108                 /* sa_ignore FREED_MEMORY */
5109         }
5110         /* control queue MAY not be empty */
5111         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5112                 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5113                 if (chk->data) {
5114                         sctp_m_freem(chk->data);
5115                         chk->data = NULL;
5116                 }
5117                 if (chk->holds_key_ref)
5118                         sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5119                 sctp_free_remote_addr(chk->whoTo);
5120                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5121                 SCTP_DECR_CHK_COUNT();
5122                 /* sa_ignore FREED_MEMORY */
5123         }
5124         /* ASCONF queue MAY not be empty */
5125         TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5126                 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5127                 if (chk->data) {
5128                         sctp_m_freem(chk->data);
5129                         chk->data = NULL;
5130                 }
5131                 if (chk->holds_key_ref)
5132                         sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5133                 sctp_free_remote_addr(chk->whoTo);
5134                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5135                 SCTP_DECR_CHK_COUNT();
5136                 /* sa_ignore FREED_MEMORY */
5137         }
5138         TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5139                 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5140                 if (chk->data) {
5141                         sctp_m_freem(chk->data);
5142                         chk->data = NULL;
5143                 }
5144                 if (chk->holds_key_ref)
5145                         sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5146                 sctp_free_remote_addr(chk->whoTo);
5147                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5148                 SCTP_DECR_CHK_COUNT();
5149                 /* sa_ignore FREED_MEMORY */
5150         }
5151
5152         if (asoc->mapping_array) {
5153                 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5154                 asoc->mapping_array = NULL;
5155         }
5156         if (asoc->nr_mapping_array) {
5157                 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5158                 asoc->nr_mapping_array = NULL;
5159         }
5160         /* the stream outs */
5161         if (asoc->strmout) {
5162                 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5163                 asoc->strmout = NULL;
5164         }
5165         asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5166         if (asoc->strmin) {
5167                 struct sctp_queued_to_read *ctl, *nctl;
5168
5169                 for (i = 0; i < asoc->streamincnt; i++) {
5170                         TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
5171                                 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
5172                                 sctp_free_remote_addr(ctl->whoFrom);
5173                                 if (ctl->data) {
5174                                         sctp_m_freem(ctl->data);
5175                                         ctl->data = NULL;
5176                                 }
5177                                 /*
5178                                  * We don't free the address here since all
5179                                  * the net's were freed above.
5180                                  */
5181                                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
5182                                 SCTP_DECR_READQ_COUNT();
5183                         }
5184                 }
5185                 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5186                 asoc->strmin = NULL;
5187         }
5188         asoc->streamincnt = 0;
5189         TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5190 #ifdef INVARIANTS
5191                 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5192                         panic("no net's left alloc'ed, or list points to itself");
5193                 }
5194 #endif
5195                 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5196                 sctp_free_remote_addr(net);
5197         }
5198         LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5199                 /* sa_ignore FREED_MEMORY */
5200                 sctp_remove_laddr(laddr);
5201         }
5202
5203         /* pending asconf (address) parameters */
5204         TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5205                 /* sa_ignore FREED_MEMORY */
5206                 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5207                 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
5208         }
5209         TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5210                 /* sa_ignore FREED_MEMORY */
5211                 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5212                 if (aack->data != NULL) {
5213                         sctp_m_freem(aack->data);
5214                 }
5215                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5216         }
5217         /* clean up auth stuff */
5218         if (asoc->local_hmacs)
5219                 sctp_free_hmaclist(asoc->local_hmacs);
5220         if (asoc->peer_hmacs)
5221                 sctp_free_hmaclist(asoc->peer_hmacs);
5222
5223         if (asoc->local_auth_chunks)
5224                 sctp_free_chunklist(asoc->local_auth_chunks);
5225         if (asoc->peer_auth_chunks)
5226                 sctp_free_chunklist(asoc->peer_auth_chunks);
5227
5228         sctp_free_authinfo(&asoc->authinfo);
5229
5230         LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5231                 LIST_REMOVE(shared_key, next);
5232                 sctp_free_sharedkey(shared_key);
5233                 /* sa_ignore FREED_MEMORY */
5234         }
5235
5236         /* Insert new items here :> */
5237
5238         /* Get rid of LOCK */
5239         SCTP_TCB_LOCK_DESTROY(stcb);
5240         SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5241         if (from_inpcbfree == SCTP_NORMAL_PROC) {
5242                 SCTP_INP_INFO_WUNLOCK();
5243                 SCTP_INP_RLOCK(inp);
5244         }
5245 #ifdef SCTP_TRACK_FREED_ASOCS
5246         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5247                 /* now clean up the tasoc itself */
5248                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5249                 SCTP_DECR_ASOC_COUNT();
5250         } else {
5251                 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5252         }
5253 #else
5254         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5255         SCTP_DECR_ASOC_COUNT();
5256 #endif
5257         if (from_inpcbfree == SCTP_NORMAL_PROC) {
5258                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5259                         /*
5260                          * If its NOT the inp_free calling us AND sctp_close
5261                          * as been called, we call back...
5262                          */
5263                         SCTP_INP_RUNLOCK(inp);
5264                         /*
5265                          * This will start the kill timer (if we are the
5266                          * last one) since we hold an increment yet. But
5267                          * this is the only safe way to do this since
5268                          * otherwise if the socket closes at the same time
5269                          * we are here we might collide in the cleanup.
5270                          */
5271                         sctp_inpcb_free(inp,
5272                             SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5273                             SCTP_CALLED_DIRECTLY_NOCMPSET);
5274                         SCTP_INP_DECR_REF(inp);
5275                         goto out_of;
5276                 } else {
5277                         /* The socket is still open. */
5278                         SCTP_INP_DECR_REF(inp);
5279                 }
5280         }
5281         if (from_inpcbfree == SCTP_NORMAL_PROC) {
5282                 SCTP_INP_RUNLOCK(inp);
5283         }
5284 out_of:
5285         /* destroyed the asoc */
5286 #ifdef SCTP_LOG_CLOSING
5287         sctp_log_closing(inp, NULL, 11);
5288 #endif
5289         return (1);
5290 }
5291
5292
5293
5294 /*
5295  * determine if a destination is "reachable" based upon the addresses bound
5296  * to the current endpoint (e.g. only v4 or v6 currently bound)
5297  */
5298 /*
5299  * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5300  * assoc level v4/v6 flags, as the assoc *may* not have the same address
5301  * types bound as its endpoint
5302  */
5303 int
5304 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5305 {
5306         struct sctp_inpcb *inp;
5307         int answer;
5308
5309         /*
5310          * No locks here, the TCB, in all cases is already locked and an
5311          * assoc is up. There is either a INP lock by the caller applied (in
5312          * asconf case when deleting an address) or NOT in the HB case,
5313          * however if HB then the INP increment is up and the INP will not
5314          * be removed (on top of the fact that we have a TCB lock). So we
5315          * only want to read the sctp_flags, which is either bound-all or
5316          * not.. no protection needed since once an assoc is up you can't be
5317          * changing your binding.
5318          */
5319         inp = stcb->sctp_ep;
5320         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5321                 /* if bound all, destination is not restricted */
5322                 /*
5323                  * RRS: Question during lock work: Is this correct? If you
5324                  * are bound-all you still might need to obey the V4--V6
5325                  * flags??? IMO this bound-all stuff needs to be removed!
5326                  */
5327                 return (1);
5328         }
5329         /* NOTE: all "scope" checks are done when local addresses are added */
5330         switch (destaddr->sa_family) {
5331         case AF_INET6:
5332                 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5333                 break;
5334         case AF_INET:
5335                 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5336                 break;
5337         default:
5338                 /* invalid family, so it's unreachable */
5339                 answer = 0;
5340                 break;
5341         }
5342         return (answer);
5343 }
5344
5345 /*
5346  * update the inp_vflags on an endpoint
5347  */
5348 static void
5349 sctp_update_ep_vflag(struct sctp_inpcb *inp)
5350 {
5351         struct sctp_laddr *laddr;
5352
5353         /* first clear the flag */
5354         inp->ip_inp.inp.inp_vflag = 0;
5355         /* set the flag based on addresses on the ep list */
5356         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5357                 if (laddr->ifa == NULL) {
5358                         SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
5359                             __FUNCTION__);
5360                         continue;
5361                 }
5362                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5363                         continue;
5364                 }
5365                 switch (laddr->ifa->address.sa.sa_family) {
5366 #ifdef INET6
5367                 case AF_INET6:
5368                         inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5369                         break;
5370 #endif
5371 #ifdef INET
5372                 case AF_INET:
5373                         inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5374                         break;
5375 #endif
5376                 default:
5377                         break;
5378                 }
5379         }
5380 }
5381
5382 /*
5383  * Add the address to the endpoint local address list There is nothing to be
5384  * done if we are bound to all addresses
5385  */
5386 void
5387 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5388 {
5389         struct sctp_laddr *laddr;
5390         int fnd, error = 0;
5391
5392         fnd = 0;
5393
5394         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5395                 /* You are already bound to all. You have it already */
5396                 return;
5397         }
5398 #ifdef INET6
5399         if (ifa->address.sa.sa_family == AF_INET6) {
5400                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5401                         /* Can't bind a non-useable addr. */
5402                         return;
5403                 }
5404         }
5405 #endif
5406         /* first, is it already present? */
5407         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5408                 if (laddr->ifa == ifa) {
5409                         fnd = 1;
5410                         break;
5411                 }
5412         }
5413
5414         if (fnd == 0) {
5415                 /* Not in the ep list */
5416                 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5417                 if (error != 0)
5418                         return;
5419                 inp->laddr_count++;
5420                 /* update inp_vflag flags */
5421                 switch (ifa->address.sa.sa_family) {
5422 #ifdef INET6
5423                 case AF_INET6:
5424                         inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5425                         break;
5426 #endif
5427 #ifdef INET6
5428                 case AF_INET:
5429                         inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5430                         break;
5431 #endif
5432                 default:
5433                         break;
5434                 }
5435         }
5436         return;
5437 }
5438
5439
5440 /*
5441  * select a new (hopefully reachable) destination net (should only be used
5442  * when we deleted an ep addr that is the only usable source address to reach
5443  * the destination net)
5444  */
5445 static void
5446 sctp_select_primary_destination(struct sctp_tcb *stcb)
5447 {
5448         struct sctp_nets *net;
5449
5450         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5451                 /* for now, we'll just pick the first reachable one we find */
5452                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5453                         continue;
5454                 if (sctp_destination_is_reachable(stcb,
5455                     (struct sockaddr *)&net->ro._l_addr)) {
5456                         /* found a reachable destination */
5457                         stcb->asoc.primary_destination = net;
5458                 }
5459         }
5460         /* I can't there from here! ...we're gonna die shortly... */
5461 }
5462
5463
5464 /*
5465  * Delete the address from the endpoint local address list There is nothing
5466  * to be done if we are bound to all addresses
5467  */
5468 void
5469 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5470 {
5471         struct sctp_laddr *laddr;
5472         int fnd;
5473
5474         fnd = 0;
5475         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5476                 /* You are already bound to all. You have it already */
5477                 return;
5478         }
5479         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5480                 if (laddr->ifa == ifa) {
5481                         fnd = 1;
5482                         break;
5483                 }
5484         }
5485         if (fnd && (inp->laddr_count < 2)) {
5486                 /* can't delete unless there are at LEAST 2 addresses */
5487                 return;
5488         }
5489         if (fnd) {
5490                 /*
5491                  * clean up any use of this address go through our
5492                  * associations and clear any last_used_address that match
5493                  * this one for each assoc, see if a new primary_destination
5494                  * is needed
5495                  */
5496                 struct sctp_tcb *stcb;
5497
5498                 /* clean up "next_addr_touse" */
5499                 if (inp->next_addr_touse == laddr)
5500                         /* delete this address */
5501                         inp->next_addr_touse = NULL;
5502
5503                 /* clean up "last_used_address" */
5504                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5505                         struct sctp_nets *net;
5506
5507                         SCTP_TCB_LOCK(stcb);
5508                         if (stcb->asoc.last_used_address == laddr)
5509                                 /* delete this address */
5510                                 stcb->asoc.last_used_address = NULL;
5511                         /*
5512                          * Now spin through all the nets and purge any ref
5513                          * to laddr
5514                          */
5515                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5516                                 if (net->ro._s_addr &&
5517                                     (net->ro._s_addr->ifa == laddr->ifa)) {
5518                                         /* Yep, purge src address selected */
5519                                         sctp_rtentry_t *rt;
5520
5521                                         /* delete this address if cached */
5522                                         rt = net->ro.ro_rt;
5523                                         if (rt != NULL) {
5524                                                 RTFREE(rt);
5525                                                 net->ro.ro_rt = NULL;
5526                                         }
5527                                         sctp_free_ifa(net->ro._s_addr);
5528                                         net->ro._s_addr = NULL;
5529                                         net->src_addr_selected = 0;
5530                                 }
5531                         }
5532                         SCTP_TCB_UNLOCK(stcb);
5533                 }               /* for each tcb */
5534                 /* remove it from the ep list */
5535                 sctp_remove_laddr(laddr);
5536                 inp->laddr_count--;
5537                 /* update inp_vflag flags */
5538                 sctp_update_ep_vflag(inp);
5539         }
5540         return;
5541 }
5542
5543 /*
5544  * Add the address to the TCB local address restricted list.
5545  * This is a "pending" address list (eg. addresses waiting for an
5546  * ASCONF-ACK response) and cannot be used as a valid source address.
5547  */
5548 void
5549 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5550 {
5551         struct sctp_inpcb *inp;
5552         struct sctp_laddr *laddr;
5553         struct sctpladdr *list;
5554
5555         /*
5556          * Assumes TCB is locked.. and possibly the INP. May need to
5557          * confirm/fix that if we need it and is not the case.
5558          */
5559         list = &stcb->asoc.sctp_restricted_addrs;
5560
5561         inp = stcb->sctp_ep;
5562 #ifdef INET6
5563         if (ifa->address.sa.sa_family == AF_INET6) {
5564                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5565                         /* Can't bind a non-existent addr. */
5566                         return;
5567                 }
5568         }
5569 #endif
5570         /* does the address already exist? */
5571         LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5572                 if (laddr->ifa == ifa) {
5573                         return;
5574                 }
5575         }
5576
5577         /* add to the list */
5578         (void)sctp_insert_laddr(list, ifa, 0);
5579         return;
5580 }
5581
5582 /*
5583  * insert an laddr entry with the given ifa for the desired list
5584  */
5585 int
5586 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5587 {
5588         struct sctp_laddr *laddr;
5589
5590         laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
5591         if (laddr == NULL) {
5592                 /* out of memory? */
5593                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5594                 return (EINVAL);
5595         }
5596         SCTP_INCR_LADDR_COUNT();
5597         bzero(laddr, sizeof(*laddr));
5598         (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5599         laddr->ifa = ifa;
5600         laddr->action = act;
5601         atomic_add_int(&ifa->refcount, 1);
5602         /* insert it */
5603         LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5604
5605         return (0);
5606 }
5607
5608 /*
5609  * Remove an laddr entry from the local address list (on an assoc)
5610  */
5611 void
5612 sctp_remove_laddr(struct sctp_laddr *laddr)
5613 {
5614
5615         /* remove from the list */
5616         LIST_REMOVE(laddr, sctp_nxt_addr);
5617         sctp_free_ifa(laddr->ifa);
5618         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
5619         SCTP_DECR_LADDR_COUNT();
5620 }
5621
5622 /*
5623  * Remove a local address from the TCB local address restricted list
5624  */
5625 void
5626 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5627 {
5628         struct sctp_inpcb *inp;
5629         struct sctp_laddr *laddr;
5630
5631         /*
5632          * This is called by asconf work. It is assumed that a) The TCB is
5633          * locked and b) The INP is locked. This is true in as much as I can
5634          * trace through the entry asconf code where I did these locks.
5635          * Again, the ASCONF code is a bit different in that it does lock
5636          * the INP during its work often times. This must be since we don't
5637          * want other proc's looking up things while what they are looking
5638          * up is changing :-D
5639          */
5640
5641         inp = stcb->sctp_ep;
5642         /* if subset bound and don't allow ASCONF's, can't delete last */
5643         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5644             sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5645                 if (stcb->sctp_ep->laddr_count < 2) {
5646                         /* can't delete last address */
5647                         return;
5648                 }
5649         }
5650         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5651                 /* remove the address if it exists */
5652                 if (laddr->ifa == NULL)
5653                         continue;
5654                 if (laddr->ifa == ifa) {
5655                         sctp_remove_laddr(laddr);
5656                         return;
5657                 }
5658         }
5659
5660         /* address not found! */
5661         return;
5662 }
5663
5664 /*
5665  * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5666  */
5667 /* sysctl */
5668 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5669 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5670
5671
5672
5673 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5674 struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
5675 int *sctp_cpuarry = NULL;
5676 void
5677 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
5678 {
5679         /* Queue a packet to a processor for the specified core */
5680         struct sctp_mcore_queue *qent;
5681         struct sctp_mcore_ctrl *wkq;
5682         int need_wake = 0;
5683
5684         if (sctp_mcore_workers == NULL) {
5685                 /* Something went way bad during setup */
5686                 sctp_input_with_port(m, off, 0);
5687                 return;
5688         }
5689         SCTP_MALLOC(qent, struct sctp_mcore_queue *,
5690             (sizeof(struct sctp_mcore_queue)),
5691             SCTP_M_MCORE);
5692         if (qent == NULL) {
5693                 /* This is trouble  */
5694                 sctp_input_with_port(m, off, 0);
5695                 return;
5696         }
5697         qent->vn = curvnet;
5698         qent->m = m;
5699         qent->off = off;
5700         qent->v6 = 0;
5701         wkq = &sctp_mcore_workers[cpu_to_use];
5702         SCTP_MCORE_QLOCK(wkq);
5703
5704         TAILQ_INSERT_TAIL(&wkq->que, qent, next);
5705         if (wkq->running == 0) {
5706                 need_wake = 1;
5707         }
5708         SCTP_MCORE_QUNLOCK(wkq);
5709         if (need_wake) {
5710                 wakeup(&wkq->running);
5711         }
5712 }
5713
5714 static void
5715 sctp_mcore_thread(void *arg)
5716 {
5717
5718         struct sctp_mcore_ctrl *wkq;
5719         struct sctp_mcore_queue *qent;
5720
5721         wkq = (struct sctp_mcore_ctrl *)arg;
5722         struct mbuf *m;
5723         int off, v6;
5724
5725         /* Wait for first tickle */
5726         SCTP_MCORE_LOCK(wkq);
5727         wkq->running = 0;
5728         msleep(&wkq->running,
5729             &wkq->core_mtx,
5730             0, "wait for pkt", 0);
5731         SCTP_MCORE_UNLOCK(wkq);
5732
5733         /* Bind to our cpu */
5734         thread_lock(curthread);
5735         sched_bind(curthread, wkq->cpuid);
5736         thread_unlock(curthread);
5737
5738         /* Now lets start working */
5739         SCTP_MCORE_LOCK(wkq);
5740         /* Now grab lock and go */
5741         for (;;) {
5742                 SCTP_MCORE_QLOCK(wkq);
5743 skip_sleep:
5744                 wkq->running = 1;
5745                 qent = TAILQ_FIRST(&wkq->que);
5746                 if (qent) {
5747                         TAILQ_REMOVE(&wkq->que, qent, next);
5748                         SCTP_MCORE_QUNLOCK(wkq);
5749                         CURVNET_SET(qent->vn);
5750                         m = qent->m;
5751                         off = qent->off;
5752                         v6 = qent->v6;
5753                         SCTP_FREE(qent, SCTP_M_MCORE);
5754                         if (v6 == 0) {
5755                                 sctp_input_with_port(m, off, 0);
5756                         } else {
5757                                 printf("V6 not yet supported\n");
5758                                 sctp_m_freem(m);
5759                         }
5760                         CURVNET_RESTORE();
5761                         SCTP_MCORE_QLOCK(wkq);
5762                 }
5763                 wkq->running = 0;
5764                 if (!TAILQ_EMPTY(&wkq->que)) {
5765                         goto skip_sleep;
5766                 }
5767                 SCTP_MCORE_QUNLOCK(wkq);
5768                 msleep(&wkq->running,
5769                     &wkq->core_mtx,
5770                     0, "wait for pkt", 0);
5771         };
5772 }
5773
5774 static void
5775 sctp_startup_mcore_threads(void)
5776 {
5777         int i, cpu;
5778
5779         if (mp_ncpus == 1)
5780                 return;
5781
5782         if (sctp_mcore_workers != NULL) {
5783                 /*
5784                  * Already been here in some previous vnet?
5785                  */
5786                 return;
5787         }
5788         SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
5789             ((mp_maxid + 1) * sizeof(struct sctp_mcore_ctrl)),
5790             SCTP_M_MCORE);
5791         if (sctp_mcore_workers == NULL) {
5792                 /* TSNH I hope */
5793                 return;
5794         }
5795         memset(sctp_mcore_workers, 0, ((mp_maxid + 1) *
5796             sizeof(struct sctp_mcore_ctrl)));
5797         /* Init the structures */
5798         for (i = 0; i <= mp_maxid; i++) {
5799                 TAILQ_INIT(&sctp_mcore_workers[i].que);
5800                 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
5801                 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
5802                 sctp_mcore_workers[i].cpuid = i;
5803         }
5804         if (sctp_cpuarry == NULL) {
5805                 SCTP_MALLOC(sctp_cpuarry, int *,
5806                     (mp_ncpus * sizeof(int)),
5807                     SCTP_M_MCORE);
5808                 i = 0;
5809                 CPU_FOREACH(cpu) {
5810                         sctp_cpuarry[i] = cpu;
5811                         i++;
5812                 }
5813         }
5814         /* Now start them all */
5815         CPU_FOREACH(cpu) {
5816                 (void)kproc_create(sctp_mcore_thread,
5817                     (void *)&sctp_mcore_workers[cpu],
5818                     &sctp_mcore_workers[cpu].thread_proc,
5819                     RFPROC,
5820                     SCTP_KTHREAD_PAGES,
5821                     SCTP_MCORE_NAME);
5822
5823         }
5824 }
5825
5826 #endif
5827
5828 void
5829 sctp_pcb_init()
5830 {
5831         /*
5832          * SCTP initialization for the PCB structures should be called by
5833          * the sctp_init() funciton.
5834          */
5835         int i;
5836         struct timeval tv;
5837
5838         if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5839                 /* error I was called twice */
5840                 return;
5841         }
5842         SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5843
5844 #if defined(SCTP_LOCAL_TRACE_BUF)
5845         bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
5846 #endif
5847 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5848         SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
5849             ((mp_maxid + 1) * sizeof(struct sctpstat)),
5850             SCTP_M_MCORE);
5851 #endif
5852         (void)SCTP_GETTIME_TIMEVAL(&tv);
5853 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5854         bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid + 1)));
5855         SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t) tv.tv_sec;
5856         SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t) tv.tv_usec;
5857 #else
5858         bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
5859         SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t) tv.tv_sec;
5860         SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t) tv.tv_usec;
5861 #endif
5862         /* init the empty list of (All) Endpoints */
5863         LIST_INIT(&SCTP_BASE_INFO(listhead));
5864
5865
5866         /* init the hash table of endpoints */
5867         TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5868         TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5869         TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5870         SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5871             &SCTP_BASE_INFO(hashasocmark));
5872         SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5873             &SCTP_BASE_INFO(hashmark));
5874         SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5875             &SCTP_BASE_INFO(hashtcpmark));
5876         SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5877
5878
5879         SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5880             &SCTP_BASE_INFO(hashvrfmark));
5881
5882         SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5883             &SCTP_BASE_INFO(vrf_ifn_hashmark));
5884         /* init the zones */
5885         /*
5886          * FIX ME: Should check for NULL returns, but if it does fail we are
5887          * doomed to panic anyways... add later maybe.
5888          */
5889         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5890             sizeof(struct sctp_inpcb), maxsockets);
5891
5892         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5893             sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5894
5895         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5896             sizeof(struct sctp_laddr),
5897             (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5898
5899         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5900             sizeof(struct sctp_nets),
5901             (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5902
5903         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5904             sizeof(struct sctp_tmit_chunk),
5905             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5906
5907         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5908             sizeof(struct sctp_queued_to_read),
5909             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5910
5911         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5912             sizeof(struct sctp_stream_queue_pending),
5913             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5914
5915         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5916             sizeof(struct sctp_asconf),
5917             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5918
5919         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
5920             sizeof(struct sctp_asconf_ack),
5921             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5922
5923
5924         /* Master Lock INIT for info structure */
5925         SCTP_INP_INFO_LOCK_INIT();
5926         SCTP_STATLOG_INIT_LOCK();
5927
5928         SCTP_IPI_COUNT_INIT();
5929         SCTP_IPI_ADDR_INIT();
5930 #ifdef SCTP_PACKET_LOGGING
5931         SCTP_IP_PKTLOG_INIT();
5932 #endif
5933         LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5934
5935         SCTP_WQ_ADDR_INIT();
5936         /* not sure if we need all the counts */
5937         SCTP_BASE_INFO(ipi_count_ep) = 0;
5938         /* assoc/tcb zone info */
5939         SCTP_BASE_INFO(ipi_count_asoc) = 0;
5940         /* local addrlist zone info */
5941         SCTP_BASE_INFO(ipi_count_laddr) = 0;
5942         /* remote addrlist zone info */
5943         SCTP_BASE_INFO(ipi_count_raddr) = 0;
5944         /* chunk info */
5945         SCTP_BASE_INFO(ipi_count_chunk) = 0;
5946
5947         /* socket queue zone info */
5948         SCTP_BASE_INFO(ipi_count_readq) = 0;
5949
5950         /* stream out queue cont */
5951         SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5952
5953         SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5954         SCTP_BASE_INFO(ipi_free_chunks) = 0;
5955
5956         SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5957
5958         /* Init the TIMEWAIT list */
5959         for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
5960                 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
5961         }
5962
5963         sctp_startup_iterator();
5964
5965 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5966         sctp_startup_mcore_threads();
5967 #endif
5968
5969         /*
5970          * INIT the default VRF which for BSD is the only one, other O/S's
5971          * may have more. But initially they must start with one and then
5972          * add the VRF's as addresses are added.
5973          */
5974         sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5975 }
5976
5977 /*
5978  * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5979  */
5980 void
5981 sctp_pcb_finish(void)
5982 {
5983         struct sctp_vrflist *vrf_bucket;
5984         struct sctp_vrf *vrf, *nvrf;
5985         struct sctp_ifn *ifn, *nifn;
5986         struct sctp_ifa *ifa, *nifa;
5987         struct sctpvtaghead *chain;
5988         struct sctp_tagblock *twait_block, *prev_twait_block;
5989         struct sctp_laddr *wi, *nwi;
5990         int i;
5991
5992         /*
5993          * Free BSD the it thread never exits but we do clean up. The only
5994          * way freebsd reaches here if we have VRF's but we still add the
5995          * ifdef to make it compile on old versions.
5996          */
5997         {
5998                 struct sctp_iterator *it, *nit;
5999
6000                 SCTP_IPI_ITERATOR_WQ_LOCK();
6001                 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
6002                         if (it->vn != curvnet) {
6003                                 continue;
6004                         }
6005                         TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6006                         if (it->function_atend != NULL) {
6007                                 (*it->function_atend) (it->pointer, it->val);
6008                         }
6009                         SCTP_FREE(it, SCTP_M_ITER);
6010                 }
6011                 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6012                 SCTP_ITERATOR_LOCK();
6013                 if ((sctp_it_ctl.cur_it) &&
6014                     (sctp_it_ctl.cur_it->vn == curvnet)) {
6015                         sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6016                 }
6017                 SCTP_ITERATOR_UNLOCK();
6018         }
6019
6020         SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer));
6021         SCTP_WQ_ADDR_LOCK();
6022         LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6023                 LIST_REMOVE(wi, sctp_nxt_addr);
6024                 SCTP_DECR_LADDR_COUNT();
6025                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6026         }
6027         SCTP_WQ_ADDR_UNLOCK();
6028
6029         /*
6030          * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
6031          * destroyed first).
6032          */
6033         vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6034         LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6035                 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6036                         LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6037                                 /* free the ifa */
6038                                 LIST_REMOVE(ifa, next_bucket);
6039                                 LIST_REMOVE(ifa, next_ifa);
6040                                 SCTP_FREE(ifa, SCTP_M_IFA);
6041                         }
6042                         /* free the ifn */
6043                         LIST_REMOVE(ifn, next_bucket);
6044                         LIST_REMOVE(ifn, next_ifn);
6045                         SCTP_FREE(ifn, SCTP_M_IFN);
6046                 }
6047                 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6048                 /* free the vrf */
6049                 LIST_REMOVE(vrf, next_vrf);
6050                 SCTP_FREE(vrf, SCTP_M_VRF);
6051         }
6052         /* free the vrf hashes */
6053         SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6054         SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
6055
6056         /*
6057          * free the TIMEWAIT list elements malloc'd in the function
6058          * sctp_add_vtag_to_timewait()...
6059          */
6060         for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6061                 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6062                 if (!LIST_EMPTY(chain)) {
6063                         prev_twait_block = NULL;
6064                         LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6065                                 if (prev_twait_block) {
6066                                         SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6067                                 }
6068                                 prev_twait_block = twait_block;
6069                         }
6070                         SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6071                 }
6072         }
6073
6074         /* free the locks and mutexes */
6075 #ifdef SCTP_PACKET_LOGGING
6076         SCTP_IP_PKTLOG_DESTROY();
6077 #endif
6078         SCTP_IPI_ADDR_DESTROY();
6079         SCTP_STATLOG_DESTROY();
6080         SCTP_INP_INFO_LOCK_DESTROY();
6081
6082         SCTP_WQ_ADDR_DESTROY();
6083
6084         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6085         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6086         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6087         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6088         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6089         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6090         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6091         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6092         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6093         /* Get rid of other stuff to */
6094         if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6095                 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6096         if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6097                 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6098         if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6099                 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6100 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6101         SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
6102 #endif
6103 }
6104
6105
6106 int
6107 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
6108     int offset, int limit, struct sctphdr *sh,
6109     struct sockaddr *altsa)
6110 {
6111         /*
6112          * grub through the INIT pulling addresses and loading them to the
6113          * nets structure in the asoc. The from address in the mbuf should
6114          * also be loaded (if it is not already). This routine can be called
6115          * with either INIT or INIT-ACK's as long as the m points to the IP
6116          * packet and the offset points to the beginning of the parameters.
6117          */
6118         struct sctp_inpcb *inp, *l_inp;
6119         struct sctp_nets *net, *nnet, *net_tmp;
6120         struct ip *iph;
6121         struct sctp_paramhdr *phdr, parm_buf;
6122         struct sctp_tcb *stcb_tmp;
6123         uint16_t ptype, plen;
6124         struct sockaddr *sa;
6125         struct sockaddr_storage dest_store;
6126         struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
6127         uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
6128         struct sctp_auth_random *p_random = NULL;
6129         uint16_t random_len = 0;
6130         uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
6131         struct sctp_auth_hmac_algo *hmacs = NULL;
6132         uint16_t hmacs_len = 0;
6133         uint8_t saw_asconf = 0;
6134         uint8_t saw_asconf_ack = 0;
6135         uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
6136         struct sctp_auth_chunk_list *chunks = NULL;
6137         uint16_t num_chunks = 0;
6138         sctp_key_t *new_key;
6139         uint32_t keylen;
6140         int got_random = 0, got_hmacs = 0, got_chklist = 0;
6141         uint8_t ecn_allowed;
6142
6143 #ifdef INET
6144         struct sockaddr_in sin;
6145
6146 #endif
6147 #ifdef INET6
6148         struct sockaddr_in6 sin6;
6149
6150 #endif
6151
6152         /* First get the destination address setup too. */
6153 #ifdef INET
6154         memset(&sin, 0, sizeof(sin));
6155         sin.sin_family = AF_INET;
6156         sin.sin_len = sizeof(sin);
6157         sin.sin_port = stcb->rport;
6158 #endif
6159 #ifdef INET6
6160         memset(&sin6, 0, sizeof(sin6));
6161         sin6.sin6_family = AF_INET6;
6162         sin6.sin6_len = sizeof(struct sockaddr_in6);
6163         sin6.sin6_port = stcb->rport;
6164 #endif
6165         iph = mtod(m, struct ip *);
6166         switch (iph->ip_v) {
6167 #ifdef INET
6168         case IPVERSION:
6169                 {
6170                         /* its IPv4 */
6171                         struct sockaddr_in *sin_2;
6172
6173                         sin_2 = (struct sockaddr_in *)(local_sa);
6174                         memset(sin_2, 0, sizeof(sin));
6175                         sin_2->sin_family = AF_INET;
6176                         sin_2->sin_len = sizeof(sin);
6177                         sin_2->sin_port = sh->dest_port;
6178                         sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
6179                         if (altsa) {
6180                                 /*
6181                                  * For cookies we use the src address NOT
6182                                  * from the packet but from the original
6183                                  * INIT.
6184                                  */
6185                                 sa = altsa;
6186                         } else {
6187                                 sin.sin_addr = iph->ip_src;
6188                                 sa = (struct sockaddr *)&sin;
6189                         }
6190                         break;
6191                 }
6192 #endif
6193 #ifdef INET6
6194         case IPV6_VERSION >> 4:
6195                 {
6196                         /* its IPv6 */
6197                         struct ip6_hdr *ip6;
6198                         struct sockaddr_in6 *sin6_2;
6199
6200                         ip6 = mtod(m, struct ip6_hdr *);
6201                         sin6_2 = (struct sockaddr_in6 *)(local_sa);
6202                         memset(sin6_2, 0, sizeof(sin6));
6203                         sin6_2->sin6_family = AF_INET6;
6204                         sin6_2->sin6_len = sizeof(struct sockaddr_in6);
6205                         sin6_2->sin6_port = sh->dest_port;
6206                         sin6_2->sin6_addr = ip6->ip6_dst;
6207                         if (altsa) {
6208                                 /*
6209                                  * For cookies we use the src address NOT
6210                                  * from the packet but from the original
6211                                  * INIT.
6212                                  */
6213                                 sa = altsa;
6214                         } else {
6215                                 sin6.sin6_addr = ip6->ip6_src;
6216                                 sa = (struct sockaddr *)&sin6;
6217                         }
6218                         break;
6219                 }
6220 #endif
6221         default:
6222                 return (-1);
6223                 break;
6224         }
6225         /* Turn off ECN until we get through all params */
6226         ecn_allowed = 0;
6227         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6228                 /* mark all addresses that we have currently on the list */
6229                 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
6230         }
6231         /* does the source address already exist? if so skip it */
6232         l_inp = inp = stcb->sctp_ep;
6233
6234         atomic_add_int(&stcb->asoc.refcnt, 1);
6235         stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
6236         atomic_add_int(&stcb->asoc.refcnt, -1);
6237
6238         if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
6239                 /* we must add the source address */
6240                 /* no scope set here since we have a tcb already. */
6241                 switch (sa->sa_family) {
6242 #ifdef INET
6243                 case AF_INET:
6244                         if (stcb->asoc.ipv4_addr_legal) {
6245                                 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
6246                                         return (-1);
6247                                 }
6248                         }
6249                         break;
6250 #endif
6251 #ifdef INET6
6252                 case AF_INET6:
6253                         if (stcb->asoc.ipv6_addr_legal) {
6254                                 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6255                                         return (-2);
6256                                 }
6257                         }
6258                         break;
6259 #endif
6260                 default:
6261                         break;
6262                 }
6263         } else {
6264                 if (net_tmp != NULL && stcb_tmp == stcb) {
6265                         net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
6266                 } else if (stcb_tmp != stcb) {
6267                         /* It belongs to another association? */
6268                         if (stcb_tmp)
6269                                 SCTP_TCB_UNLOCK(stcb_tmp);
6270                         return (-3);
6271                 }
6272         }
6273         if (stcb->asoc.state == 0) {
6274                 /* the assoc was freed? */
6275                 return (-4);
6276         }
6277         /*
6278          * peer must explicitly turn this on. This may have been initialized
6279          * to be "on" in order to allow local addr changes while INIT's are
6280          * in flight.
6281          */
6282         stcb->asoc.peer_supports_asconf = 0;
6283         /* now we must go through each of the params. */
6284         phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
6285         while (phdr) {
6286                 ptype = ntohs(phdr->param_type);
6287                 plen = ntohs(phdr->param_length);
6288                 /*
6289                  * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
6290                  * (int)plen);
6291                  */
6292                 if (offset + plen > limit) {
6293                         break;
6294                 }
6295                 if (plen == 0) {
6296                         break;
6297                 }
6298 #ifdef INET
6299                 if (ptype == SCTP_IPV4_ADDRESS) {
6300                         if (stcb->asoc.ipv4_addr_legal) {
6301                                 struct sctp_ipv4addr_param *p4, p4_buf;
6302
6303                                 /* ok get the v4 address and check/add */
6304                                 phdr = sctp_get_next_param(m, offset,
6305                                     (struct sctp_paramhdr *)&p4_buf,
6306                                     sizeof(p4_buf));
6307                                 if (plen != sizeof(struct sctp_ipv4addr_param) ||
6308                                     phdr == NULL) {
6309                                         return (-5);
6310                                 }
6311                                 p4 = (struct sctp_ipv4addr_param *)phdr;
6312                                 sin.sin_addr.s_addr = p4->addr;
6313                                 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
6314                                         /* Skip multi-cast addresses */
6315                                         goto next_param;
6316                                 }
6317                                 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
6318                                     (sin.sin_addr.s_addr == INADDR_ANY)) {
6319                                         goto next_param;
6320                                 }
6321                                 sa = (struct sockaddr *)&sin;
6322                                 inp = stcb->sctp_ep;
6323                                 atomic_add_int(&stcb->asoc.refcnt, 1);
6324                                 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6325                                     local_sa, stcb);
6326                                 atomic_add_int(&stcb->asoc.refcnt, -1);
6327
6328                                 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
6329                                     inp == NULL) {
6330                                         /* we must add the source address */
6331                                         /*
6332                                          * no scope set since we have a tcb
6333                                          * already
6334                                          */
6335
6336                                         /*
6337                                          * we must validate the state again
6338                                          * here
6339                                          */
6340                         add_it_now:
6341                                         if (stcb->asoc.state == 0) {
6342                                                 /* the assoc was freed? */
6343                                                 return (-7);
6344                                         }
6345                                         if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
6346                                                 return (-8);
6347                                         }
6348                                 } else if (stcb_tmp == stcb) {
6349                                         if (stcb->asoc.state == 0) {
6350                                                 /* the assoc was freed? */
6351                                                 return (-10);
6352                                         }
6353                                         if (net != NULL) {
6354                                                 /* clear flag */
6355                                                 net->dest_state &=
6356                                                     ~SCTP_ADDR_NOT_IN_ASSOC;
6357                                         }
6358                                 } else {
6359                                         /*
6360                                          * strange, address is in another
6361                                          * assoc? straighten out locks.
6362                                          */
6363                                         if (stcb_tmp) {
6364                                                 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6365                                                         /*
6366                                                          * in setup state we
6367                                                          * abort this guy
6368                                                          */
6369                                                         sctp_abort_an_association(stcb_tmp->sctp_ep,
6370                                                             stcb_tmp, 1, NULL, 0);
6371                                                         goto add_it_now;
6372                                                 }
6373                                                 SCTP_TCB_UNLOCK(stcb_tmp);
6374                                         }
6375                                         if (stcb->asoc.state == 0) {
6376                                                 /* the assoc was freed? */
6377                                                 return (-12);
6378                                         }
6379                                         return (-13);
6380                                 }
6381                         }
6382                 } else
6383 #endif
6384 #ifdef INET6
6385                 if (ptype == SCTP_IPV6_ADDRESS) {
6386                         if (stcb->asoc.ipv6_addr_legal) {
6387                                 /* ok get the v6 address and check/add */
6388                                 struct sctp_ipv6addr_param *p6, p6_buf;
6389
6390                                 phdr = sctp_get_next_param(m, offset,
6391                                     (struct sctp_paramhdr *)&p6_buf,
6392                                     sizeof(p6_buf));
6393                                 if (plen != sizeof(struct sctp_ipv6addr_param) ||
6394                                     phdr == NULL) {
6395                                         return (-14);
6396                                 }
6397                                 p6 = (struct sctp_ipv6addr_param *)phdr;
6398                                 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6399                                     sizeof(p6->addr));
6400                                 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
6401                                         /* Skip multi-cast addresses */
6402                                         goto next_param;
6403                                 }
6404                                 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
6405                                         /*
6406                                          * Link local make no sense without
6407                                          * scope
6408                                          */
6409                                         goto next_param;
6410                                 }
6411                                 sa = (struct sockaddr *)&sin6;
6412                                 inp = stcb->sctp_ep;
6413                                 atomic_add_int(&stcb->asoc.refcnt, 1);
6414                                 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6415                                     local_sa, stcb);
6416                                 atomic_add_int(&stcb->asoc.refcnt, -1);
6417                                 if (stcb_tmp == NULL &&
6418                                     (inp == stcb->sctp_ep || inp == NULL)) {
6419                                         /*
6420                                          * we must validate the state again
6421                                          * here
6422                                          */
6423                         add_it_now6:
6424                                         if (stcb->asoc.state == 0) {
6425                                                 /* the assoc was freed? */
6426                                                 return (-16);
6427                                         }
6428                                         /*
6429                                          * we must add the address, no scope
6430                                          * set
6431                                          */
6432                                         if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
6433                                                 return (-17);
6434                                         }
6435                                 } else if (stcb_tmp == stcb) {
6436                                         /*
6437                                          * we must validate the state again
6438                                          * here
6439                                          */
6440                                         if (stcb->asoc.state == 0) {
6441                                                 /* the assoc was freed? */
6442                                                 return (-19);
6443                                         }
6444                                         if (net != NULL) {
6445                                                 /* clear flag */
6446                                                 net->dest_state &=
6447                                                     ~SCTP_ADDR_NOT_IN_ASSOC;
6448                                         }
6449                                 } else {
6450                                         /*
6451                                          * strange, address is in another
6452                                          * assoc? straighten out locks.
6453                                          */
6454                                         if (stcb_tmp)
6455                                                 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6456                                                         /*
6457                                                          * in setup state we
6458                                                          * abort this guy
6459                                                          */
6460                                                         sctp_abort_an_association(stcb_tmp->sctp_ep,
6461                                                             stcb_tmp, 1, NULL, 0);
6462                                                         goto add_it_now6;
6463                                                 }
6464                                         SCTP_TCB_UNLOCK(stcb_tmp);
6465
6466                                         if (stcb->asoc.state == 0) {
6467                                                 /* the assoc was freed? */
6468                                                 return (-21);
6469                                         }
6470                                         return (-22);
6471                                 }
6472                         }
6473                 } else
6474 #endif
6475                 if (ptype == SCTP_ECN_CAPABLE) {
6476                         ecn_allowed = 1;
6477                 } else if (ptype == SCTP_ULP_ADAPTATION) {
6478                         if (stcb->asoc.state != SCTP_STATE_OPEN) {
6479                                 struct sctp_adaptation_layer_indication ai,
6480                                                                 *aip;
6481
6482                                 phdr = sctp_get_next_param(m, offset,
6483                                     (struct sctp_paramhdr *)&ai, sizeof(ai));
6484                                 aip = (struct sctp_adaptation_layer_indication *)phdr;
6485                                 if (aip) {
6486                                         stcb->asoc.peers_adaptation = ntohl(aip->indication);
6487                                         stcb->asoc.adaptation_needed = 1;
6488                                 }
6489                         }
6490                 } else if (ptype == SCTP_SET_PRIM_ADDR) {
6491                         struct sctp_asconf_addr_param lstore, *fee;
6492                         int lptype;
6493                         struct sockaddr *lsa = NULL;
6494
6495 #ifdef INET
6496                         struct sctp_asconf_addrv4_param *fii;
6497
6498 #endif
6499
6500                         stcb->asoc.peer_supports_asconf = 1;
6501                         if (plen > sizeof(lstore)) {
6502                                 return (-23);
6503                         }
6504                         phdr = sctp_get_next_param(m, offset,
6505                             (struct sctp_paramhdr *)&lstore,
6506                             min(plen, sizeof(lstore)));
6507                         if (phdr == NULL) {
6508                                 return (-24);
6509                         }
6510                         fee = (struct sctp_asconf_addr_param *)phdr;
6511                         lptype = ntohs(fee->addrp.ph.param_type);
6512                         switch (lptype) {
6513 #ifdef INET
6514                         case SCTP_IPV4_ADDRESS:
6515                                 if (plen !=
6516                                     sizeof(struct sctp_asconf_addrv4_param)) {
6517                                         SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
6518                                             (int)sizeof(struct sctp_asconf_addrv4_param),
6519                                             plen);
6520                                 } else {
6521                                         fii = (struct sctp_asconf_addrv4_param *)fee;
6522                                         sin.sin_addr.s_addr = fii->addrp.addr;
6523                                         lsa = (struct sockaddr *)&sin;
6524                                 }
6525                                 break;
6526 #endif
6527 #ifdef INET6
6528                         case SCTP_IPV6_ADDRESS:
6529                                 if (plen !=
6530                                     sizeof(struct sctp_asconf_addr_param)) {
6531                                         SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
6532                                             (int)sizeof(struct sctp_asconf_addr_param),
6533                                             plen);
6534                                 } else {
6535                                         memcpy(sin6.sin6_addr.s6_addr,
6536                                             fee->addrp.addr,
6537                                             sizeof(fee->addrp.addr));
6538                                         lsa = (struct sockaddr *)&sin6;
6539                                 }
6540                                 break;
6541 #endif
6542                         default:
6543                                 break;
6544                         }
6545                         if (lsa) {
6546                                 (void)sctp_set_primary_addr(stcb, sa, NULL);
6547                         }
6548                 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
6549                         stcb->asoc.peer_supports_nat = 1;
6550                 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
6551                         /* Peer supports pr-sctp */
6552                         stcb->asoc.peer_supports_prsctp = 1;
6553                 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
6554                         /* A supported extension chunk */
6555                         struct sctp_supported_chunk_types_param *pr_supported;
6556                         uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
6557                         int num_ent, i;
6558
6559                         phdr = sctp_get_next_param(m, offset,
6560                             (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
6561                         if (phdr == NULL) {
6562                                 return (-25);
6563                         }
6564                         stcb->asoc.peer_supports_asconf = 0;
6565                         stcb->asoc.peer_supports_prsctp = 0;
6566                         stcb->asoc.peer_supports_pktdrop = 0;
6567                         stcb->asoc.peer_supports_strreset = 0;
6568                         stcb->asoc.peer_supports_nr_sack = 0;
6569                         stcb->asoc.peer_supports_auth = 0;
6570                         pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
6571                         num_ent = plen - sizeof(struct sctp_paramhdr);
6572                         for (i = 0; i < num_ent; i++) {
6573                                 switch (pr_supported->chunk_types[i]) {
6574                                 case SCTP_ASCONF:
6575                                 case SCTP_ASCONF_ACK:
6576                                         stcb->asoc.peer_supports_asconf = 1;
6577                                         break;
6578                                 case SCTP_FORWARD_CUM_TSN:
6579                                         stcb->asoc.peer_supports_prsctp = 1;
6580                                         break;
6581                                 case SCTP_PACKET_DROPPED:
6582                                         stcb->asoc.peer_supports_pktdrop = 1;
6583                                         break;
6584                                 case SCTP_NR_SELECTIVE_ACK:
6585                                         stcb->asoc.peer_supports_nr_sack = 1;
6586                                         break;
6587                                 case SCTP_STREAM_RESET:
6588                                         stcb->asoc.peer_supports_strreset = 1;
6589                                         break;
6590                                 case SCTP_AUTHENTICATION:
6591                                         stcb->asoc.peer_supports_auth = 1;
6592                                         break;
6593                                 default:
6594                                         /* one I have not learned yet */
6595                                         break;
6596
6597                                 }
6598                         }
6599                 } else if (ptype == SCTP_RANDOM) {
6600                         if (plen > sizeof(random_store))
6601                                 break;
6602                         if (got_random) {
6603                                 /* already processed a RANDOM */
6604                                 goto next_param;
6605                         }
6606                         phdr = sctp_get_next_param(m, offset,
6607                             (struct sctp_paramhdr *)random_store,
6608                             min(sizeof(random_store), plen));
6609                         if (phdr == NULL)
6610                                 return (-26);
6611                         p_random = (struct sctp_auth_random *)phdr;
6612                         random_len = plen - sizeof(*p_random);
6613                         /* enforce the random length */
6614                         if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
6615                                 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
6616                                 return (-27);
6617                         }
6618                         got_random = 1;
6619                 } else if (ptype == SCTP_HMAC_LIST) {
6620                         int num_hmacs;
6621                         int i;
6622
6623                         if (plen > sizeof(hmacs_store))
6624                                 break;
6625                         if (got_hmacs) {
6626                                 /* already processed a HMAC list */
6627                                 goto next_param;
6628                         }
6629                         phdr = sctp_get_next_param(m, offset,
6630                             (struct sctp_paramhdr *)hmacs_store,
6631                             min(plen, sizeof(hmacs_store)));
6632                         if (phdr == NULL)
6633                                 return (-28);
6634                         hmacs = (struct sctp_auth_hmac_algo *)phdr;
6635                         hmacs_len = plen - sizeof(*hmacs);
6636                         num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
6637                         /* validate the hmac list */
6638                         if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
6639                                 return (-29);
6640                         }
6641                         if (stcb->asoc.peer_hmacs != NULL)
6642                                 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
6643                         stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
6644                         if (stcb->asoc.peer_hmacs != NULL) {
6645                                 for (i = 0; i < num_hmacs; i++) {
6646                                         (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6647                                             ntohs(hmacs->hmac_ids[i]));
6648                                 }
6649                         }
6650                         got_hmacs = 1;
6651                 } else if (ptype == SCTP_CHUNK_LIST) {
6652                         int i;
6653
6654                         if (plen > sizeof(chunks_store))
6655                                 break;
6656                         if (got_chklist) {
6657                                 /* already processed a Chunks list */
6658                                 goto next_param;
6659                         }
6660                         phdr = sctp_get_next_param(m, offset,
6661                             (struct sctp_paramhdr *)chunks_store,
6662                             min(plen, sizeof(chunks_store)));
6663                         if (phdr == NULL)
6664                                 return (-30);
6665                         chunks = (struct sctp_auth_chunk_list *)phdr;
6666                         num_chunks = plen - sizeof(*chunks);
6667                         if (stcb->asoc.peer_auth_chunks != NULL)
6668                                 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6669                         else
6670                                 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6671                         for (i = 0; i < num_chunks; i++) {
6672                                 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
6673                                     stcb->asoc.peer_auth_chunks);
6674                                 /* record asconf/asconf-ack if listed */
6675                                 if (chunks->chunk_types[i] == SCTP_ASCONF)
6676                                         saw_asconf = 1;
6677                                 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6678                                         saw_asconf_ack = 1;
6679
6680                         }
6681                         got_chklist = 1;
6682                 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6683                             (ptype == SCTP_STATE_COOKIE) ||
6684                             (ptype == SCTP_UNRECOG_PARAM) ||
6685                             (ptype == SCTP_COOKIE_PRESERVE) ||
6686                             (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6687                             (ptype == SCTP_ADD_IP_ADDRESS) ||
6688                             (ptype == SCTP_DEL_IP_ADDRESS) ||
6689                             (ptype == SCTP_ERROR_CAUSE_IND) ||
6690                     (ptype == SCTP_SUCCESS_REPORT)) {
6691                          /* don't care */ ;
6692                 } else {
6693                         if ((ptype & 0x8000) == 0x0000) {
6694                                 /*
6695                                  * must stop processing the rest of the
6696                                  * param's. Any report bits were handled
6697                                  * with the call to
6698                                  * sctp_arethere_unrecognized_parameters()
6699                                  * when the INIT or INIT-ACK was first seen.
6700                                  */
6701                                 break;
6702                         }
6703                 }
6704
6705 next_param:
6706                 offset += SCTP_SIZE32(plen);
6707                 if (offset >= limit) {
6708                         break;
6709                 }
6710                 phdr = sctp_get_next_param(m, offset, &parm_buf,
6711                     sizeof(parm_buf));
6712         }
6713         /* Now check to see if we need to purge any addresses */
6714         TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
6715                 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6716                     SCTP_ADDR_NOT_IN_ASSOC) {
6717                         /* This address has been removed from the asoc */
6718                         /* remove and free it */
6719                         stcb->asoc.numnets--;
6720                         TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6721                         sctp_free_remote_addr(net);
6722                         if (net == stcb->asoc.primary_destination) {
6723                                 stcb->asoc.primary_destination = NULL;
6724                                 sctp_select_primary_destination(stcb);
6725                         }
6726                 }
6727         }
6728         if (ecn_allowed == 0) {
6729                 stcb->asoc.ecn_allowed = 0;
6730         }
6731         /* validate authentication required parameters */
6732         if (got_random && got_hmacs) {
6733                 stcb->asoc.peer_supports_auth = 1;
6734         } else {
6735                 stcb->asoc.peer_supports_auth = 0;
6736         }
6737         if (!stcb->asoc.peer_supports_auth && got_chklist) {
6738                 /* peer does not support auth but sent a chunks list? */
6739                 return (-31);
6740         }
6741         if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
6742             !stcb->asoc.peer_supports_auth) {
6743                 /* peer supports asconf but not auth? */
6744                 return (-32);
6745         } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
6746             ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6747                 return (-33);
6748         }
6749         /* concatenate the full random key */
6750         keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
6751         if (chunks != NULL) {
6752                 keylen += sizeof(*chunks) + num_chunks;
6753         }
6754         new_key = sctp_alloc_key(keylen);
6755         if (new_key != NULL) {
6756                 /* copy in the RANDOM */
6757                 if (p_random != NULL) {
6758                         keylen = sizeof(*p_random) + random_len;
6759                         bcopy(p_random, new_key->key, keylen);
6760                 }
6761                 /* append in the AUTH chunks */
6762                 if (chunks != NULL) {
6763                         bcopy(chunks, new_key->key + keylen,
6764                             sizeof(*chunks) + num_chunks);
6765                         keylen += sizeof(*chunks) + num_chunks;
6766                 }
6767                 /* append in the HMACs */
6768                 if (hmacs != NULL) {
6769                         bcopy(hmacs, new_key->key + keylen,
6770                             sizeof(*hmacs) + hmacs_len);
6771                 }
6772         } else {
6773                 /* failed to get memory for the key */
6774                 return (-34);
6775         }
6776         if (stcb->asoc.authinfo.peer_random != NULL)
6777                 sctp_free_key(stcb->asoc.authinfo.peer_random);
6778         stcb->asoc.authinfo.peer_random = new_key;
6779         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6780         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6781
6782         return (0);
6783 }
6784
6785 int
6786 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6787     struct sctp_nets *net)
6788 {
6789         /* make sure the requested primary address exists in the assoc */
6790         if (net == NULL && sa)
6791                 net = sctp_findnet(stcb, sa);
6792
6793         if (net == NULL) {
6794                 /* didn't find the requested primary address! */
6795                 return (-1);
6796         } else {
6797                 /* set the primary address */
6798                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6799                         /* Must be confirmed, so queue to set */
6800                         net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6801                         return (0);
6802                 }
6803                 stcb->asoc.primary_destination = net;
6804                 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
6805                         sctp_free_remote_addr(stcb->asoc.alternate);
6806                         stcb->asoc.alternate = NULL;
6807                 }
6808                 net = TAILQ_FIRST(&stcb->asoc.nets);
6809                 if (net != stcb->asoc.primary_destination) {
6810                         /*
6811                          * first one on the list is NOT the primary
6812                          * sctp_cmpaddr() is much more efficient if the
6813                          * primary is the first on the list, make it so.
6814                          */
6815                         TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6816                         TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6817                 }
6818                 return (0);
6819         }
6820 }
6821
6822 int
6823 sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
6824 {
6825         /*
6826          * This function serves two purposes. It will see if a TAG can be
6827          * re-used and return 1 for yes it is ok and 0 for don't use that
6828          * tag. A secondary function it will do is purge out old tags that
6829          * can be removed.
6830          */
6831         struct sctpvtaghead *chain;
6832         struct sctp_tagblock *twait_block;
6833         struct sctpasochead *head;
6834         struct sctp_tcb *stcb;
6835         int i;
6836
6837         SCTP_INP_INFO_RLOCK();
6838         head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
6839             SCTP_BASE_INFO(hashasocmark))];
6840         if (head == NULL) {
6841                 /* invalid vtag */
6842                 goto skip_vtag_check;
6843         }
6844         LIST_FOREACH(stcb, head, sctp_asocs) {
6845                 /*
6846                  * We choose not to lock anything here. TCB's can't be
6847                  * removed since we have the read lock, so they can't be
6848                  * freed on us, same thing for the INP. I may be wrong with
6849                  * this assumption, but we will go with it for now :-)
6850                  */
6851                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
6852                         continue;
6853                 }
6854                 if (stcb->asoc.my_vtag == tag) {
6855                         /* candidate */
6856                         if (stcb->rport != rport) {
6857                                 continue;
6858                         }
6859                         if (stcb->sctp_ep->sctp_lport != lport) {
6860                                 continue;
6861                         }
6862                         /* Its a used tag set */
6863                         SCTP_INP_INFO_RUNLOCK();
6864                         return (0);
6865                 }
6866         }
6867 skip_vtag_check:
6868
6869         chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
6870         /* Now what about timed wait ? */
6871         if (!LIST_EMPTY(chain)) {
6872                 /*
6873                  * Block(s) are present, lets see if we have this tag in the
6874                  * list
6875                  */
6876                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6877                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6878                                 if (twait_block->vtag_block[i].v_tag == 0) {
6879                                         /* not used */
6880                                         continue;
6881                                 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6882                                     now->tv_sec) {
6883                                         /* Audit expires this guy */
6884                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
6885                                         twait_block->vtag_block[i].v_tag = 0;
6886                                         twait_block->vtag_block[i].lport = 0;
6887                                         twait_block->vtag_block[i].rport = 0;
6888                                 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
6889                                             (twait_block->vtag_block[i].lport == lport) &&
6890                                     (twait_block->vtag_block[i].rport == rport)) {
6891                                         /* Bad tag, sorry :< */
6892                                         SCTP_INP_INFO_RUNLOCK();
6893                                         return (0);
6894                                 }
6895                         }
6896                 }
6897         }
6898         SCTP_INP_INFO_RUNLOCK();
6899         return (1);
6900 }
6901
6902 static void
6903 sctp_drain_mbufs(struct sctp_tcb *stcb)
6904 {
6905         /*
6906          * We must hunt this association for MBUF's past the cumack (i.e.
6907          * out of order data that we can renege on).
6908          */
6909         struct sctp_association *asoc;
6910         struct sctp_tmit_chunk *chk, *nchk;
6911         uint32_t cumulative_tsn_p1;
6912         struct sctp_queued_to_read *ctl, *nctl;
6913         int cnt, strmat;
6914         uint32_t gap, i;
6915         int fnd = 0;
6916
6917         /* We look for anything larger than the cum-ack + 1 */
6918
6919         asoc = &stcb->asoc;
6920         if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6921                 /* none we can reneg on. */
6922                 return;
6923         }
6924         SCTP_STAT_INCR(sctps_protocol_drains_done);
6925         cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6926         cnt = 0;
6927         /* First look in the re-assembly queue */
6928         TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
6929                 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) {
6930                         /* Yep it is above cum-ack */
6931                         cnt++;
6932                         SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn);
6933                         asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6934                         sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6935                         SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6936                         TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6937                         if (chk->data) {
6938                                 sctp_m_freem(chk->data);
6939                                 chk->data = NULL;
6940                         }
6941                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6942                 }
6943         }
6944         /* Ok that was fun, now we will drain all the inbound streams? */
6945         for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6946                 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) {
6947                         if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) {
6948                                 /* Yep it is above cum-ack */
6949                                 cnt++;
6950                                 SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn);
6951                                 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6952                                 sctp_ucount_decr(asoc->cnt_on_all_streams);
6953                                 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6954                                 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next);
6955                                 if (ctl->data) {
6956                                         sctp_m_freem(ctl->data);
6957                                         ctl->data = NULL;
6958                                 }
6959                                 sctp_free_remote_addr(ctl->whoFrom);
6960                                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
6961                                 SCTP_DECR_READQ_COUNT();
6962                         }
6963                 }
6964         }
6965         if (cnt) {
6966                 /* We must back down to see what the new highest is */
6967                 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
6968                         SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
6969                         if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6970                                 asoc->highest_tsn_inside_map = i;
6971                                 fnd = 1;
6972                                 break;
6973                         }
6974                 }
6975                 if (!fnd) {
6976                         asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
6977                 }
6978                 /*
6979                  * Question, should we go through the delivery queue? The
6980                  * only reason things are on here is the app not reading OR
6981                  * a p-d-api up. An attacker COULD send enough in to
6982                  * initiate the PD-API and then send a bunch of stuff to
6983                  * other streams... these would wind up on the delivery
6984                  * queue.. and then we would not get to them. But in order
6985                  * to do this I then have to back-track and un-deliver
6986                  * sequence numbers in streams.. el-yucko. I think for now
6987                  * we will NOT look at the delivery queue and leave it to be
6988                  * something to consider later. An alternative would be to
6989                  * abort the P-D-API with a notification and then deliver
6990                  * the data.... Or another method might be to keep track of
6991                  * how many times the situation occurs and if we see a
6992                  * possible attack underway just abort the association.
6993                  */
6994 #ifdef SCTP_DEBUG
6995                 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6996 #endif
6997                 /*
6998                  * Now do we need to find a new
6999                  * asoc->highest_tsn_inside_map?
7000                  */
7001                 asoc->last_revoke_count = cnt;
7002                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
7003                 /* sa_ignore NO_NULL_CHK */
7004                 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7005                 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
7006         }
7007         /*
7008          * Another issue, in un-setting the TSN's in the mapping array we
7009          * DID NOT adjust the highest_tsn marker.  This will cause one of
7010          * two things to occur. It may cause us to do extra work in checking
7011          * for our mapping array movement. More importantly it may cause us
7012          * to SACK every datagram. This may not be a bad thing though since
7013          * we will recover once we get our cum-ack above and all this stuff
7014          * we dumped recovered.
7015          */
7016 }
7017
7018 void
7019 sctp_drain()
7020 {
7021         /*
7022          * We must walk the PCB lists for ALL associations here. The system
7023          * is LOW on MBUF's and needs help. This is where reneging will
7024          * occur. We really hope this does NOT happen!
7025          */
7026         VNET_ITERATOR_DECL(vnet_iter);
7027         VNET_LIST_RLOCK_NOSLEEP();
7028         VNET_FOREACH(vnet_iter) {
7029                 CURVNET_SET(vnet_iter);
7030                 struct sctp_inpcb *inp;
7031                 struct sctp_tcb *stcb;
7032
7033                 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7034                 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7035 #ifdef VIMAGE
7036                         continue;
7037 #else
7038                         return;
7039 #endif
7040                 }
7041                 SCTP_INP_INFO_RLOCK();
7042                 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7043                         /* For each endpoint */
7044                         SCTP_INP_RLOCK(inp);
7045                         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7046                                 /* For each association */
7047                                 SCTP_TCB_LOCK(stcb);
7048                                 sctp_drain_mbufs(stcb);
7049                                 SCTP_TCB_UNLOCK(stcb);
7050                         }
7051                         SCTP_INP_RUNLOCK(inp);
7052                 }
7053                 SCTP_INP_INFO_RUNLOCK();
7054                 CURVNET_RESTORE();
7055         }
7056         VNET_LIST_RUNLOCK_NOSLEEP();
7057 }
7058
7059 /*
7060  * start a new iterator
7061  * iterates through all endpoints and associations based on the pcb_state
7062  * flags and asoc_state.  "af" (mandatory) is executed for all matching
7063  * assocs and "ef" (optional) is executed when the iterator completes.
7064  * "inpf" (optional) is executed for each new endpoint as it is being
7065  * iterated through. inpe (optional) is called when the inp completes
7066  * its way through all the stcbs.
7067  */
7068 int
7069 sctp_initiate_iterator(inp_func inpf,
7070     asoc_func af,
7071     inp_func inpe,
7072     uint32_t pcb_state,
7073     uint32_t pcb_features,
7074     uint32_t asoc_state,
7075     void *argp,
7076     uint32_t argi,
7077     end_func ef,
7078     struct sctp_inpcb *s_inp,
7079     uint8_t chunk_output_off)
7080 {
7081         struct sctp_iterator *it = NULL;
7082
7083         if (af == NULL) {
7084                 return (-1);
7085         }
7086         SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
7087             SCTP_M_ITER);
7088         if (it == NULL) {
7089                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
7090                 return (ENOMEM);
7091         }
7092         memset(it, 0, sizeof(*it));
7093         it->function_assoc = af;
7094         it->function_inp = inpf;
7095         if (inpf)
7096                 it->done_current_ep = 0;
7097         else
7098                 it->done_current_ep = 1;
7099         it->function_atend = ef;
7100         it->pointer = argp;
7101         it->val = argi;
7102         it->pcb_flags = pcb_state;
7103         it->pcb_features = pcb_features;
7104         it->asoc_state = asoc_state;
7105         it->function_inp_end = inpe;
7106         it->no_chunk_output = chunk_output_off;
7107         it->vn = curvnet;
7108         if (s_inp) {
7109                 /* Assume lock is held here */
7110                 it->inp = s_inp;
7111                 SCTP_INP_INCR_REF(it->inp);
7112                 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
7113         } else {
7114                 SCTP_INP_INFO_RLOCK();
7115                 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
7116                 if (it->inp) {
7117                         SCTP_INP_INCR_REF(it->inp);
7118                 }
7119                 SCTP_INP_INFO_RUNLOCK();
7120                 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
7121
7122         }
7123         SCTP_IPI_ITERATOR_WQ_LOCK();
7124
7125         TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
7126         if (sctp_it_ctl.iterator_running == 0) {
7127                 sctp_wakeup_iterator();
7128         }
7129         SCTP_IPI_ITERATOR_WQ_UNLOCK();
7130         /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
7131         return (0);
7132 }