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