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