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