]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_pcb.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[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                         inp->sctp_socket = NULL;
3279                         return;
3280                 }
3281         }
3282         inp->sctp_socket = NULL;
3283         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3284             SCTP_PCB_FLAGS_UNBOUND) {
3285                 /*
3286                  * ok, this guy has been bound. It's port is somewhere in
3287                  * the SCTP_BASE_INFO(hash table). Remove it!
3288                  */
3289                 LIST_REMOVE(inp, sctp_hash);
3290                 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3291         }
3292         /*
3293          * If there is a timer running to kill us, forget it, since it may
3294          * have a contest on the INP lock.. which would cause us to die ...
3295          */
3296         cnt = 0;
3297         for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
3298             asoc = nasoc) {
3299                 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3300                 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3301                         cnt++;
3302                         continue;
3303                 }
3304                 /* Free associations that are NOT killing us */
3305                 SCTP_TCB_LOCK(asoc);
3306                 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3307                     ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3308                         struct mbuf *op_err;
3309                         uint32_t *ippp;
3310
3311                         op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3312                             0, M_DONTWAIT, 1, MT_DATA);
3313                         if (op_err) {
3314                                 /* Fill in the user initiated abort */
3315                                 struct sctp_paramhdr *ph;
3316
3317                                 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
3318                                     sizeof(uint32_t));
3319                                 ph = mtod(op_err, struct sctp_paramhdr *);
3320                                 ph->param_type = htons(
3321                                     SCTP_CAUSE_USER_INITIATED_ABT);
3322                                 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3323                                 ippp = (uint32_t *) (ph + 1);
3324                                 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
3325
3326                         }
3327                         asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3328 #if defined(SCTP_PANIC_ON_ABORT)
3329                         panic("inpcb_free does an abort");
3330 #endif
3331                         sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3332                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3333                 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3334                         cnt++;
3335                         SCTP_TCB_UNLOCK(asoc);
3336                         continue;
3337                 }
3338                 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3339                     (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3340                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3341                 }
3342                 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3343                         cnt++;
3344                 }
3345         }
3346         if (cnt) {
3347                 /* Ok we have someone out there that will kill us */
3348                 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3349                 SCTP_INP_WUNLOCK(inp);
3350                 SCTP_ASOC_CREATE_UNLOCK(inp);
3351                 SCTP_INP_INFO_WUNLOCK();
3352                 SCTP_ITERATOR_UNLOCK();
3353 #ifdef SCTP_LOG_CLOSING
3354                 sctp_log_closing(inp, NULL, 3);
3355 #endif
3356                 return;
3357         }
3358         if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3359                 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3360                 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3361                 SCTP_INP_WUNLOCK(inp);
3362                 SCTP_ASOC_CREATE_UNLOCK(inp);
3363                 SCTP_INP_INFO_WUNLOCK();
3364                 SCTP_ITERATOR_UNLOCK();
3365 #ifdef SCTP_LOG_CLOSING
3366                 sctp_log_closing(inp, NULL, 4);
3367 #endif
3368                 return;
3369         }
3370         (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3371         inp->sctp_ep.signature_change.type = 0;
3372         inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3373
3374 #ifdef SCTP_LOG_CLOSING
3375         sctp_log_closing(inp, NULL, 5);
3376 #endif
3377
3378         (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3379         inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
3380         /* Clear the read queue */
3381         /* sa_ignore FREED_MEMORY */
3382         while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
3383                 /* Its only abandoned if it had data left */
3384                 if (sq->length)
3385                         SCTP_STAT_INCR(sctps_left_abandon);
3386
3387                 TAILQ_REMOVE(&inp->read_queue, sq, next);
3388                 sctp_free_remote_addr(sq->whoFrom);
3389                 if (so)
3390                         so->so_rcv.sb_cc -= sq->length;
3391                 if (sq->data) {
3392                         sctp_m_freem(sq->data);
3393                         sq->data = NULL;
3394                 }
3395                 /*
3396                  * no need to free the net count, since at this point all
3397                  * assoc's are gone.
3398                  */
3399                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
3400                 SCTP_DECR_READQ_COUNT();
3401         }
3402         /* Now the sctp_pcb things */
3403         /*
3404          * free each asoc if it is not already closed/free. we can't use the
3405          * macro here since le_next will get freed as part of the
3406          * sctp_free_assoc() call.
3407          */
3408         cnt = 0;
3409         if (so) {
3410 #ifdef IPSEC
3411                 ipsec4_delete_pcbpolicy(ip_pcb);
3412 #endif                          /* IPSEC */
3413
3414                 /* Unlocks not needed since the socket is gone now */
3415         }
3416         if (ip_pcb->inp_options) {
3417                 (void)sctp_m_free(ip_pcb->inp_options);
3418                 ip_pcb->inp_options = 0;
3419         }
3420         if (ip_pcb->inp_moptions) {
3421                 inp_freemoptions(ip_pcb->inp_moptions);
3422                 ip_pcb->inp_moptions = 0;
3423         }
3424 #ifdef INET6
3425         if (ip_pcb->inp_vflag & INP_IPV6) {
3426                 struct in6pcb *in6p;
3427
3428                 in6p = (struct in6pcb *)inp;
3429                 ip6_freepcbopts(in6p->in6p_outputopts);
3430         }
3431 #endif                          /* INET6 */
3432         ip_pcb->inp_vflag = 0;
3433         /* free up authentication fields */
3434         if (inp->sctp_ep.local_auth_chunks != NULL)
3435                 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3436         if (inp->sctp_ep.local_hmacs != NULL)
3437                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3438
3439         shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3440         while (shared_key) {
3441                 LIST_REMOVE(shared_key, next);
3442                 sctp_free_sharedkey(shared_key);
3443                 /* sa_ignore FREED_MEMORY */
3444                 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3445         }
3446
3447         inp_save = LIST_NEXT(inp, sctp_list);
3448         LIST_REMOVE(inp, sctp_list);
3449
3450         /* fix any iterators only after out of the list */
3451         sctp_iterator_inp_being_freed(inp, inp_save);
3452         /*
3453          * if we have an address list the following will free the list of
3454          * ifaddr's that are set into this ep. Again macro limitations here,
3455          * since the LIST_FOREACH could be a bad idea.
3456          */
3457         for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
3458             laddr = nladdr) {
3459                 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
3460                 sctp_remove_laddr(laddr);
3461         }
3462
3463 #ifdef SCTP_TRACK_FREED_ASOCS
3464         /* TEMP CODE */
3465         for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
3466             asoc = nasoc) {
3467                 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3468                 LIST_REMOVE(asoc, sctp_tcblist);
3469                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
3470                 SCTP_DECR_ASOC_COUNT();
3471         }
3472         /* *** END TEMP CODE *** */
3473 #endif
3474         /* Now lets see about freeing the EP hash table. */
3475         if (inp->sctp_tcbhash != NULL) {
3476                 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3477                 inp->sctp_tcbhash = NULL;
3478         }
3479         /* Now we must put the ep memory back into the zone pool */
3480         INP_LOCK_DESTROY(&inp->ip_inp.inp);
3481         SCTP_INP_LOCK_DESTROY(inp);
3482         SCTP_INP_READ_DESTROY(inp);
3483         SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3484         SCTP_INP_INFO_WUNLOCK();
3485         SCTP_ITERATOR_UNLOCK();
3486         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3487         SCTP_DECR_EP_COUNT();
3488 }
3489
3490
3491 struct sctp_nets *
3492 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3493 {
3494         struct sctp_nets *net;
3495
3496         /* locate the address */
3497         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3498                 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3499                         return (net);
3500         }
3501         return (NULL);
3502 }
3503
3504
3505 int
3506 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3507 {
3508         struct sctp_ifa *sctp_ifa;
3509
3510         sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3511         if (sctp_ifa) {
3512                 return (1);
3513         } else {
3514                 return (0);
3515         }
3516 }
3517
3518 /*
3519  * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3520  * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3521  * stats of stuff.
3522  */
3523 int
3524 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3525     int set_scope, int from)
3526 {
3527         /*
3528          * The following is redundant to the same lines in the
3529          * sctp_aloc_assoc() but is needed since other's call the add
3530          * address function
3531          */
3532         struct sctp_nets *net, *netfirst;
3533         int addr_inscope;
3534
3535         SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3536             from);
3537         SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3538
3539         netfirst = sctp_findnet(stcb, newaddr);
3540         if (netfirst) {
3541                 /*
3542                  * Lie and return ok, we don't want to make the association
3543                  * go away for this behavior. It will happen in the TCP
3544                  * model in a connected socket. It does not reach the hash
3545                  * table until after the association is built so it can't be
3546                  * found. Mark as reachable, since the initial creation will
3547                  * have been cleared and the NOT_IN_ASSOC flag will have
3548                  * been added... and we don't want to end up removing it
3549                  * back out.
3550                  */
3551                 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3552                         netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3553                             SCTP_ADDR_UNCONFIRMED);
3554                 } else {
3555                         netfirst->dest_state = SCTP_ADDR_REACHABLE;
3556                 }
3557
3558                 return (0);
3559         }
3560         addr_inscope = 1;
3561         if (newaddr->sa_family == AF_INET) {
3562                 struct sockaddr_in *sin;
3563
3564                 sin = (struct sockaddr_in *)newaddr;
3565                 if (sin->sin_addr.s_addr == 0) {
3566                         /* Invalid address */
3567                         return (-1);
3568                 }
3569                 /* zero out the bzero area */
3570                 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3571
3572                 /* assure len is set */
3573                 sin->sin_len = sizeof(struct sockaddr_in);
3574                 if (set_scope) {
3575 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3576                         stcb->ipv4_local_scope = 1;
3577 #else
3578                         if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3579                                 stcb->asoc.ipv4_local_scope = 1;
3580                         }
3581 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3582                 } else {
3583                         /* Validate the address is in scope */
3584                         if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3585                             (stcb->asoc.ipv4_local_scope == 0)) {
3586                                 addr_inscope = 0;
3587                         }
3588                 }
3589 #ifdef INET6
3590         } else if (newaddr->sa_family == AF_INET6) {
3591                 struct sockaddr_in6 *sin6;
3592
3593                 sin6 = (struct sockaddr_in6 *)newaddr;
3594                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3595                         /* Invalid address */
3596                         return (-1);
3597                 }
3598                 /* assure len is set */
3599                 sin6->sin6_len = sizeof(struct sockaddr_in6);
3600                 if (set_scope) {
3601                         if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3602                                 stcb->asoc.loopback_scope = 1;
3603                                 stcb->asoc.local_scope = 0;
3604                                 stcb->asoc.ipv4_local_scope = 1;
3605                                 stcb->asoc.site_scope = 1;
3606                         } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3607                                 /*
3608                                  * If the new destination is a LINK_LOCAL we
3609                                  * must have common site scope. Don't set
3610                                  * the local scope since we may not share
3611                                  * all links, only loopback can do this.
3612                                  * Links on the local network would also be
3613                                  * on our private network for v4 too.
3614                                  */
3615                                 stcb->asoc.ipv4_local_scope = 1;
3616                                 stcb->asoc.site_scope = 1;
3617                         } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3618                                 /*
3619                                  * If the new destination is SITE_LOCAL then
3620                                  * we must have site scope in common.
3621                                  */
3622                                 stcb->asoc.site_scope = 1;
3623                         }
3624                 } else {
3625                         /* Validate the address is in scope */
3626                         if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3627                             (stcb->asoc.loopback_scope == 0)) {
3628                                 addr_inscope = 0;
3629                         } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3630                             (stcb->asoc.local_scope == 0)) {
3631                                 addr_inscope = 0;
3632                         } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3633                             (stcb->asoc.site_scope == 0)) {
3634                                 addr_inscope = 0;
3635                         }
3636                 }
3637 #endif
3638         } else {
3639                 /* not supported family type */
3640                 return (-1);
3641         }
3642         net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3643         if (net == NULL) {
3644                 return (-1);
3645         }
3646         SCTP_INCR_RADDR_COUNT();
3647         bzero(net, sizeof(*net));
3648         (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3649         memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3650         if (newaddr->sa_family == AF_INET) {
3651                 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3652         } else if (newaddr->sa_family == AF_INET6) {
3653                 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3654         }
3655         net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3656         if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3657                 stcb->asoc.loopback_scope = 1;
3658                 stcb->asoc.ipv4_local_scope = 1;
3659                 stcb->asoc.local_scope = 0;
3660                 stcb->asoc.site_scope = 1;
3661                 addr_inscope = 1;
3662         }
3663         net->failure_threshold = stcb->asoc.def_net_failure;
3664         if (addr_inscope == 0) {
3665                 net->dest_state = (SCTP_ADDR_REACHABLE |
3666                     SCTP_ADDR_OUT_OF_SCOPE);
3667         } else {
3668                 if (from == SCTP_ADDR_IS_CONFIRMED)
3669                         /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3670                         net->dest_state = SCTP_ADDR_REACHABLE;
3671                 else
3672                         net->dest_state = SCTP_ADDR_REACHABLE |
3673                             SCTP_ADDR_UNCONFIRMED;
3674         }
3675         /*
3676          * We set this to 0, the timer code knows that this means its an
3677          * initial value
3678          */
3679         net->RTO = 0;
3680         net->RTO_measured = 0;
3681         stcb->asoc.numnets++;
3682         *(&net->ref_count) = 1;
3683         net->tos_flowlabel = 0;
3684         if (SCTP_BASE_SYSCTL(sctp_udp_tunneling_for_client_enable)) {
3685                 net->port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3686         } else {
3687                 net->port = 0;
3688         }
3689 #ifdef INET
3690         if (newaddr->sa_family == AF_INET)
3691                 net->tos_flowlabel = stcb->asoc.default_tos;
3692 #endif
3693 #ifdef INET6
3694         if (newaddr->sa_family == AF_INET6)
3695                 net->tos_flowlabel = stcb->asoc.default_flowlabel;
3696 #endif
3697         /* Init the timer structure */
3698         SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3699         SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
3700         SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3701
3702         /* Now generate a route for this guy */
3703 #ifdef INET6
3704         /* KAME hack: embed scopeid */
3705         if (newaddr->sa_family == AF_INET6) {
3706                 struct sockaddr_in6 *sin6;
3707
3708                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3709                 (void)sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
3710                 sin6->sin6_scope_id = 0;
3711         }
3712 #endif
3713         SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
3714
3715         if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3716                 /* Get source address */
3717                 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3718                     stcb,
3719                     (sctp_route_t *) & net->ro,
3720                     net,
3721                     0,
3722                     stcb->asoc.vrf_id);
3723                 /* Now get the interface MTU */
3724                 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
3725                         net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
3726                 } else {
3727                         net->mtu = 0;
3728                 }
3729 #ifdef SCTP_PRINT_FOR_B_AND_M
3730                 SCTP_PRINTF("We have found an interface mtu of %d\n", net->mtu);
3731 #endif
3732                 if (net->mtu == 0) {
3733                         /* Huh ?? */
3734                         net->mtu = SCTP_DEFAULT_MTU;
3735                 } else {
3736                         uint32_t rmtu;
3737
3738                         rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
3739 #ifdef SCTP_PRINT_FOR_B_AND_M
3740                         SCTP_PRINTF("The route mtu is %d\n", rmtu);
3741 #endif
3742                         if (rmtu == 0) {
3743                                 /*
3744                                  * Start things off to match mtu of
3745                                  * interface please.
3746                                  */
3747                                 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
3748                                     net->ro.ro_rt, net->mtu);
3749                         } else {
3750                                 /*
3751                                  * we take the route mtu over the interface,
3752                                  * since the route may be leading out the
3753                                  * loopback, or a different interface.
3754                                  */
3755                                 net->mtu = rmtu;
3756                         }
3757                 }
3758                 if (from == SCTP_ALLOC_ASOC) {
3759 #ifdef SCTP_PRINT_FOR_B_AND_M
3760                         SCTP_PRINTF("New assoc sets mtu to :%d\n", net->mtu);
3761 #endif
3762                         stcb->asoc.smallest_mtu = net->mtu;
3763                 }
3764         } else {
3765                 net->mtu = stcb->asoc.smallest_mtu;
3766         }
3767 #ifdef INET6
3768         if (newaddr->sa_family == AF_INET6) {
3769                 struct sockaddr_in6 *sin6;
3770
3771                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3772                 (void)sa6_recoverscope(sin6);
3773         }
3774 #endif
3775         if (net->port) {
3776                 net->mtu -= sizeof(struct udphdr);
3777         }
3778         if (stcb->asoc.smallest_mtu > net->mtu) {
3779 #ifdef SCTP_PRINT_FOR_B_AND_M
3780                 SCTP_PRINTF("new address mtu:%d smaller than smallest:%d\n",
3781                     net->mtu, stcb->asoc.smallest_mtu);
3782 #endif
3783                 stcb->asoc.smallest_mtu = net->mtu;
3784         }
3785         /* JRS - Use the congestion control given in the CC module */
3786         stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3787
3788         /*
3789          * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3790          * of assoc (2005/06/27, iyengar@cis.udel.edu)
3791          */
3792         net->find_pseudo_cumack = 1;
3793         net->find_rtx_pseudo_cumack = 1;
3794         net->src_addr_selected = 0;
3795         netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3796         if (net->ro.ro_rt == NULL) {
3797                 /* Since we have no route put it at the back */
3798                 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3799         } else if (netfirst == NULL) {
3800                 /* We are the first one in the pool. */
3801                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3802         } else if (netfirst->ro.ro_rt == NULL) {
3803                 /*
3804                  * First one has NO route. Place this one ahead of the first
3805                  * one.
3806                  */
3807                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3808         } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3809                 /*
3810                  * This one has a different interface than the one at the
3811                  * top of the list. Place it ahead.
3812                  */
3813                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3814         } else {
3815                 /*
3816                  * Ok we have the same interface as the first one. Move
3817                  * forward until we find either a) one with a NULL route...
3818                  * insert ahead of that b) one with a different ifp.. insert
3819                  * after that. c) end of the list.. insert at the tail.
3820                  */
3821                 struct sctp_nets *netlook;
3822
3823                 do {
3824                         netlook = TAILQ_NEXT(netfirst, sctp_next);
3825                         if (netlook == NULL) {
3826                                 /* End of the list */
3827                                 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3828                                 break;
3829                         } else if (netlook->ro.ro_rt == NULL) {
3830                                 /* next one has NO route */
3831                                 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3832                                 break;
3833                         } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
3834                                 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3835                                     net, sctp_next);
3836                                 break;
3837                         }
3838                         /* Shift forward */
3839                         netfirst = netlook;
3840                 } while (netlook != NULL);
3841         }
3842
3843         /* got to have a primary set */
3844         if (stcb->asoc.primary_destination == 0) {
3845                 stcb->asoc.primary_destination = net;
3846         } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3847                     (net->ro.ro_rt) &&
3848             ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3849                 /* No route to current primary adopt new primary */
3850                 stcb->asoc.primary_destination = net;
3851         }
3852         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3853             net);
3854         /* Validate primary is first */
3855         net = TAILQ_FIRST(&stcb->asoc.nets);
3856         if ((net != stcb->asoc.primary_destination) &&
3857             (stcb->asoc.primary_destination)) {
3858                 /*
3859                  * first one on the list is NOT the primary sctp_cmpaddr()
3860                  * is much more efficent if the primary is the first on the
3861                  * list, make it so.
3862                  */
3863                 TAILQ_REMOVE(&stcb->asoc.nets,
3864                     stcb->asoc.primary_destination, sctp_next);
3865                 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3866                     stcb->asoc.primary_destination, sctp_next);
3867         }
3868         return (0);
3869 }
3870
3871
3872 /*
3873  * allocate an association and add it to the endpoint. The caller must be
3874  * careful to add all additional addresses once they are know right away or
3875  * else the assoc will be may experience a blackout scenario.
3876  */
3877 struct sctp_tcb *
3878 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3879     int for_a_init, int *error, uint32_t override_tag, uint32_t vrf_id,
3880     struct thread *p
3881 )
3882 {
3883         /* note the p argument is only valid in unbound sockets */
3884
3885         struct sctp_tcb *stcb;
3886         struct sctp_association *asoc;
3887         struct sctpasochead *head;
3888         uint16_t rport;
3889         int err;
3890
3891         /*
3892          * Assumption made here: Caller has done a
3893          * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3894          * address does not exist already.
3895          */
3896         if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
3897                 /* Hit max assoc, sorry no more */
3898                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
3899                 *error = ENOBUFS;
3900                 return (NULL);
3901         }
3902         if (firstaddr == NULL) {
3903                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3904                 *error = EINVAL;
3905                 return (NULL);
3906         }
3907         SCTP_INP_RLOCK(inp);
3908         if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
3909             ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
3910             (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3911                 /*
3912                  * If its in the TCP pool, its NOT allowed to create an
3913                  * association. The parent listener needs to call
3914                  * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3915                  * off, or connected one does this.. its an error.
3916                  */
3917                 SCTP_INP_RUNLOCK(inp);
3918                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3919                 *error = EINVAL;
3920                 return (NULL);
3921         }
3922         SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
3923 #ifdef SCTP_DEBUG
3924         if (firstaddr) {
3925                 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
3926                 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
3927                     ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3928         } else {
3929                 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
3930         }
3931 #endif                          /* SCTP_DEBUG */
3932         if (firstaddr->sa_family == AF_INET) {
3933                 struct sockaddr_in *sin;
3934
3935                 sin = (struct sockaddr_in *)firstaddr;
3936                 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3937                         /* Invalid address */
3938                         SCTP_INP_RUNLOCK(inp);
3939                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3940                         *error = EINVAL;
3941                         return (NULL);
3942                 }
3943                 rport = sin->sin_port;
3944         } else if (firstaddr->sa_family == AF_INET6) {
3945                 struct sockaddr_in6 *sin6;
3946
3947                 sin6 = (struct sockaddr_in6 *)firstaddr;
3948                 if ((sin6->sin6_port == 0) ||
3949                     (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3950                         /* Invalid address */
3951                         SCTP_INP_RUNLOCK(inp);
3952                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3953                         *error = EINVAL;
3954                         return (NULL);
3955                 }
3956                 rport = sin6->sin6_port;
3957         } else {
3958                 /* not supported family type */
3959                 SCTP_INP_RUNLOCK(inp);
3960                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3961                 *error = EINVAL;
3962                 return (NULL);
3963         }
3964         SCTP_INP_RUNLOCK(inp);
3965         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3966                 /*
3967                  * If you have not performed a bind, then we need to do the
3968                  * ephemerial bind for you.
3969                  */
3970                 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3971                     (struct sockaddr *)NULL,
3972                     (struct sctp_ifa *)NULL,
3973                     p
3974                     ))) {
3975                         /* bind error, probably perm */
3976                         *error = err;
3977                         return (NULL);
3978                 }
3979         }
3980         stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
3981         if (stcb == NULL) {
3982                 /* out of memory? */
3983                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
3984                 *error = ENOMEM;
3985                 return (NULL);
3986         }
3987         SCTP_INCR_ASOC_COUNT();
3988
3989         bzero(stcb, sizeof(*stcb));
3990         asoc = &stcb->asoc;
3991         SCTP_TCB_LOCK_INIT(stcb);
3992         SCTP_TCB_SEND_LOCK_INIT(stcb);
3993         /* setup back pointer's */
3994         stcb->sctp_ep = inp;
3995         stcb->sctp_socket = inp->sctp_socket;
3996         if ((err = sctp_init_asoc(inp, stcb, for_a_init, override_tag, vrf_id))) {
3997                 /* failed */
3998                 SCTP_TCB_LOCK_DESTROY(stcb);
3999                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4000                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4001                 SCTP_DECR_ASOC_COUNT();
4002                 *error = err;
4003                 return (NULL);
4004         }
4005         /* and the port */
4006         stcb->rport = rport;
4007         SCTP_INP_INFO_WLOCK();
4008         SCTP_INP_WLOCK(inp);
4009         if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4010                 /* inpcb freed while alloc going on */
4011                 SCTP_TCB_LOCK_DESTROY(stcb);
4012                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4013                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4014                 SCTP_INP_WUNLOCK(inp);
4015                 SCTP_INP_INFO_WUNLOCK();
4016                 SCTP_DECR_ASOC_COUNT();
4017                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4018                 *error = EINVAL;
4019                 return (NULL);
4020         }
4021         SCTP_TCB_LOCK(stcb);
4022
4023         /* now that my_vtag is set, add it to the hash */
4024         head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
4025             SCTP_BASE_INFO(hashasocmark))];
4026         /* put it in the bucket in the vtag hash of assoc's for the system */
4027         LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4028         sctp_delete_from_timewait(stcb->asoc.my_vtag);
4029
4030         SCTP_INP_INFO_WUNLOCK();
4031
4032         if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4033                 /* failure.. memory error? */
4034                 if (asoc->strmout) {
4035                         SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4036                         asoc->strmout = NULL;
4037                 }
4038                 if (asoc->mapping_array) {
4039                         SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4040                         asoc->mapping_array = NULL;
4041                 }
4042                 SCTP_DECR_ASOC_COUNT();
4043                 SCTP_TCB_LOCK_DESTROY(stcb);
4044                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4045                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4046                 SCTP_INP_WUNLOCK(inp);
4047                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4048                 *error = ENOBUFS;
4049                 return (NULL);
4050         }
4051         /* Init all the timers */
4052         SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
4053         SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4054         SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4055         SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4056         SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4057         SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4058         SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
4059         SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4060
4061         LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4062         /* now file the port under the hash as well */
4063         if (inp->sctp_tcbhash != NULL) {
4064                 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4065                     inp->sctp_hashmark)];
4066                 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4067         }
4068         SCTP_INP_WUNLOCK(inp);
4069         SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
4070         return (stcb);
4071 }
4072
4073
4074 void
4075 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4076 {
4077         struct sctp_association *asoc;
4078
4079         asoc = &stcb->asoc;
4080         asoc->numnets--;
4081         TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4082         if (net == asoc->primary_destination) {
4083                 /* Reset primary */
4084                 struct sctp_nets *lnet;
4085
4086                 lnet = TAILQ_FIRST(&asoc->nets);
4087                 /*
4088                  * Mobility adaptation Ideally, if deleted destination is
4089                  * the primary, it becomes a fast retransmission trigger by
4090                  * the subsequent SET PRIMARY. (by micchie)
4091                  */
4092                 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4093                     SCTP_MOBILITY_BASE) ||
4094                     sctp_is_mobility_feature_on(stcb->sctp_ep,
4095                     SCTP_MOBILITY_FASTHANDOFF)) {
4096                         SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4097                         if (asoc->deleted_primary != NULL) {
4098                                 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4099                                 goto out;
4100                         }
4101                         asoc->deleted_primary = net;
4102                         atomic_add_int(&net->ref_count, 1);
4103                         memset(&net->lastsa, 0, sizeof(net->lastsa));
4104                         memset(&net->lastsv, 0, sizeof(net->lastsv));
4105                         sctp_mobility_feature_on(stcb->sctp_ep,
4106                             SCTP_MOBILITY_PRIM_DELETED);
4107                         sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4108                             stcb->sctp_ep, stcb, NULL);
4109                 }
4110 out:
4111                 /* Try to find a confirmed primary */
4112                 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4113         }
4114         if (net == asoc->last_data_chunk_from) {
4115                 /* Reset primary */
4116                 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4117         }
4118         if (net == asoc->last_control_chunk_from) {
4119                 /* Clear net */
4120                 asoc->last_control_chunk_from = NULL;
4121         }
4122         sctp_free_remote_addr(net);
4123 }
4124
4125 /*
4126  * remove a remote endpoint address from an association, it will fail if the
4127  * address does not exist.
4128  */
4129 int
4130 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4131 {
4132         /*
4133          * Here we need to remove a remote address. This is quite simple, we
4134          * first find it in the list of address for the association
4135          * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4136          * on that item. Note we do not allow it to be removed if there are
4137          * no other addresses.
4138          */
4139         struct sctp_association *asoc;
4140         struct sctp_nets *net, *net_tmp;
4141
4142         asoc = &stcb->asoc;
4143
4144         /* locate the address */
4145         for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
4146                 net_tmp = TAILQ_NEXT(net, sctp_next);
4147                 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4148                         continue;
4149                 }
4150                 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4151                     remaddr)) {
4152                         /* we found the guy */
4153                         if (asoc->numnets < 2) {
4154                                 /* Must have at LEAST two remote addresses */
4155                                 return (-1);
4156                         } else {
4157                                 sctp_remove_net(stcb, net);
4158                                 return (0);
4159                         }
4160                 }
4161         }
4162         /* not found. */
4163         return (-2);
4164 }
4165
4166 void
4167 sctp_delete_from_timewait(uint32_t tag)
4168 {
4169         struct sctpvtaghead *chain;
4170         struct sctp_tagblock *twait_block;
4171         int found = 0;
4172         int i;
4173
4174         chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4175         if (!SCTP_LIST_EMPTY(chain)) {
4176                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4177                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4178                                 if (twait_block->vtag_block[i].v_tag == tag) {
4179                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
4180                                         twait_block->vtag_block[i].v_tag = 0;
4181                                         found = 1;
4182                                         break;
4183                                 }
4184                         }
4185                         if (found)
4186                                 break;
4187                 }
4188         }
4189 }
4190
4191 int
4192 sctp_is_in_timewait(uint32_t tag)
4193 {
4194         struct sctpvtaghead *chain;
4195         struct sctp_tagblock *twait_block;
4196         int found = 0;
4197         int i;
4198
4199         chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4200         if (!SCTP_LIST_EMPTY(chain)) {
4201                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4202                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4203                                 if (twait_block->vtag_block[i].v_tag == tag) {
4204                                         found = 1;
4205                                         break;
4206                                 }
4207                         }
4208                         if (found)
4209                                 break;
4210                 }
4211         }
4212         return (found);
4213 }
4214
4215
4216 void
4217 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time)
4218 {
4219         struct sctpvtaghead *chain;
4220         struct sctp_tagblock *twait_block;
4221         struct timeval now;
4222         int set, i;
4223
4224         (void)SCTP_GETTIME_TIMEVAL(&now);
4225         chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4226         set = 0;
4227         if (!SCTP_LIST_EMPTY(chain)) {
4228                 /* Block(s) present, lets find space, and expire on the fly */
4229                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4230                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4231                                 if ((twait_block->vtag_block[i].v_tag == 0) &&
4232                                     !set) {
4233                                         twait_block->vtag_block[i].tv_sec_at_expire =
4234                                             now.tv_sec + time;
4235                                         twait_block->vtag_block[i].v_tag = tag;
4236                                         set = 1;
4237                                 } else if ((twait_block->vtag_block[i].v_tag) &&
4238                                     ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4239                                         /* Audit expires this guy */
4240                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
4241                                         twait_block->vtag_block[i].v_tag = 0;
4242                                         if (set == 0) {
4243                                                 /* Reuse it for my new tag */
4244                                                 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4245                                                 twait_block->vtag_block[0].v_tag = tag;
4246                                                 set = 1;
4247                                         }
4248                                 }
4249                         }
4250                         if (set) {
4251                                 /*
4252                                  * We only do up to the block where we can
4253                                  * place our tag for audits
4254                                  */
4255                                 break;
4256                         }
4257                 }
4258         }
4259         /* Need to add a new block to chain */
4260         if (!set) {
4261                 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4262                     sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4263                 if (twait_block == NULL) {
4264                         return;
4265                 }
4266                 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4267                 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4268                 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4269                 twait_block->vtag_block[0].v_tag = tag;
4270         }
4271 }
4272
4273
4274 static void
4275 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4276 {
4277         struct sctp_iterator *it;
4278
4279         /*
4280          * Unlock the tcb lock we do this so we avoid a dead lock scenario
4281          * where the iterator is waiting on the TCB lock and the TCB lock is
4282          * waiting on the iterator lock.
4283          */
4284         it = stcb->asoc.stcb_starting_point_for_iterator;
4285         if (it == NULL) {
4286                 return;
4287         }
4288         if (it->inp != stcb->sctp_ep) {
4289                 /* hmm, focused on the wrong one? */
4290                 return;
4291         }
4292         if (it->stcb != stcb) {
4293                 return;
4294         }
4295         it->stcb = LIST_NEXT(stcb, sctp_tcblist);
4296         if (it->stcb == NULL) {
4297                 /* done with all asoc's in this assoc */
4298                 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
4299                         it->inp = NULL;
4300                 } else {
4301                         it->inp = LIST_NEXT(inp, sctp_list);
4302                 }
4303         }
4304 }
4305
4306
4307 /*-
4308  * Free the association after un-hashing the remote port. This
4309  * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4310  * expect that the input to this function IS a locked TCB.
4311  * It will return 0, if it did NOT destroy the association (instead
4312  * it unlocks it. It will return NON-zero if it either destroyed the
4313  * association OR the association is already destroyed.
4314  */
4315 int
4316 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4317 {
4318         int i;
4319         struct sctp_association *asoc;
4320         struct sctp_nets *net, *prev;
4321         struct sctp_laddr *laddr;
4322         struct sctp_tmit_chunk *chk;
4323         struct sctp_asconf_addr *aparam;
4324         struct sctp_asconf_ack *aack;
4325         struct sctp_stream_reset_list *liste;
4326         struct sctp_queued_to_read *sq;
4327         struct sctp_stream_queue_pending *sp;
4328         sctp_sharedkey_t *shared_key;
4329         struct socket *so;
4330         int ccnt = 0;
4331         int cnt = 0;
4332
4333         /* first, lets purge the entry from the hash table. */
4334
4335 #ifdef SCTP_LOG_CLOSING
4336         sctp_log_closing(inp, stcb, 6);
4337 #endif
4338         if (stcb->asoc.state == 0) {
4339 #ifdef SCTP_LOG_CLOSING
4340                 sctp_log_closing(inp, NULL, 7);
4341 #endif
4342                 /* there is no asoc, really TSNH :-0 */
4343                 return (1);
4344         }
4345         /* TEMP CODE */
4346         if (stcb->freed_from_where == 0) {
4347                 /* Only record the first place free happened from */
4348                 stcb->freed_from_where = from_location;
4349         }
4350         /* TEMP CODE */
4351
4352         asoc = &stcb->asoc;
4353         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4354             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4355                 /* nothing around */
4356                 so = NULL;
4357         else
4358                 so = inp->sctp_socket;
4359
4360         /*
4361          * We used timer based freeing if a reader or writer is in the way.
4362          * So we first check if we are actually being called from a timer,
4363          * if so we abort early if a reader or writer is still in the way.
4364          */
4365         if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4366             (from_inpcbfree == SCTP_NORMAL_PROC)) {
4367                 /*
4368                  * is it the timer driving us? if so are the reader/writers
4369                  * gone?
4370                  */
4371                 if (stcb->asoc.refcnt) {
4372                         /* nope, reader or writer in the way */
4373                         sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4374                         /* no asoc destroyed */
4375                         SCTP_TCB_UNLOCK(stcb);
4376 #ifdef SCTP_LOG_CLOSING
4377                         sctp_log_closing(inp, stcb, 8);
4378 #endif
4379                         return (0);
4380                 }
4381         }
4382         /* now clean up any other timers */
4383         (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4384         asoc->hb_timer.self = NULL;
4385         (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4386         asoc->dack_timer.self = NULL;
4387         (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4388         /*-
4389          * For stream reset we don't blast this unless
4390          * it is a str-reset timer, it might be the
4391          * free-asoc timer which we DON'T want to
4392          * disturb.
4393          */
4394         if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4395                 asoc->strreset_timer.self = NULL;
4396         (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4397         asoc->asconf_timer.self = NULL;
4398         (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4399         asoc->autoclose_timer.self = NULL;
4400         (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4401         asoc->shut_guard_timer.self = NULL;
4402         (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4403         asoc->delayed_event_timer.self = NULL;
4404         /* Mobility adaptation */
4405         (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4406         asoc->delete_prim_timer.self = NULL;
4407         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4408                 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4409                 net->fr_timer.self = NULL;
4410                 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4411                 net->rxt_timer.self = NULL;
4412                 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4413                 net->pmtu_timer.self = NULL;
4414         }
4415         /* Now the read queue needs to be cleaned up (only once) */
4416         cnt = 0;
4417         if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4418                 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4419                 SCTP_INP_READ_LOCK(inp);
4420                 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4421                         if (sq->stcb == stcb) {
4422                                 sq->do_not_ref_stcb = 1;
4423                                 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4424                                 /*
4425                                  * If there is no end, there never will be
4426                                  * now.
4427                                  */
4428                                 if (sq->end_added == 0) {
4429                                         /* Held for PD-API clear that. */
4430                                         sq->pdapi_aborted = 1;
4431                                         sq->held_length = 0;
4432                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4433                                                 /*
4434                                                  * Need to add a PD-API
4435                                                  * aborted indication.
4436                                                  * Setting the control_pdapi
4437                                                  * assures that it will be
4438                                                  * added right after this
4439                                                  * msg.
4440                                                  */
4441                                                 uint32_t strseq;
4442
4443                                                 stcb->asoc.control_pdapi = sq;
4444                                                 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4445                                                 sctp_notify_partial_delivery_indication(stcb,
4446                                                     SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq);
4447                                                 stcb->asoc.control_pdapi = NULL;
4448                                         }
4449                                 }
4450                                 /* Add an end to wake them */
4451                                 sq->end_added = 1;
4452                                 cnt++;
4453                         }
4454                 }
4455                 SCTP_INP_READ_UNLOCK(inp);
4456                 if (stcb->block_entry) {
4457                         cnt++;
4458                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4459                         stcb->block_entry->error = ECONNRESET;
4460                         stcb->block_entry = NULL;
4461                 }
4462         }
4463         if (stcb->asoc.refcnt) {
4464                 /*
4465                  * reader or writer in the way, we have hopefully given him
4466                  * something to chew on above.
4467                  */
4468                 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4469                 SCTP_TCB_UNLOCK(stcb);
4470                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4471                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4472                         /* nothing around */
4473                         so = NULL;
4474                 if (so) {
4475                         /* Wake any reader/writers */
4476                         sctp_sorwakeup(inp, so);
4477                         sctp_sowwakeup(inp, so);
4478                 }
4479 #ifdef SCTP_LOG_CLOSING
4480                 sctp_log_closing(inp, stcb, 9);
4481 #endif
4482                 /* no asoc destroyed */
4483                 return (0);
4484         }
4485 #ifdef SCTP_LOG_CLOSING
4486         sctp_log_closing(inp, stcb, 10);
4487 #endif
4488         /*
4489          * When I reach here, no others want to kill the assoc yet.. and I
4490          * own the lock. Now its possible an abort comes in when I do the
4491          * lock exchange below to grab all the locks to do the final take
4492          * out. to prevent this we increment the count, which will start a
4493          * timer and blow out above thus assuring us that we hold exclusive
4494          * killing of the asoc. Note that after getting back the TCB lock we
4495          * will go ahead and increment the counter back up and stop any
4496          * timer a passing stranger may have started :-S
4497          */
4498         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4499                 atomic_add_int(&stcb->asoc.refcnt, 1);
4500
4501                 SCTP_TCB_UNLOCK(stcb);
4502
4503                 SCTP_ITERATOR_LOCK();
4504                 SCTP_INP_INFO_WLOCK();
4505                 SCTP_INP_WLOCK(inp);
4506                 SCTP_TCB_LOCK(stcb);
4507         }
4508         /* Double check the GONE flag */
4509         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4510             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4511                 /* nothing around */
4512                 so = NULL;
4513
4514         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4515             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4516                 /*
4517                  * For TCP type we need special handling when we are
4518                  * connected. We also include the peel'ed off ones to.
4519                  */
4520                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4521                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4522                         inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4523                         if (so) {
4524                                 SOCK_LOCK(so);
4525                                 if (so->so_rcv.sb_cc == 0) {
4526                                         so->so_state &= ~(SS_ISCONNECTING |
4527                                             SS_ISDISCONNECTING |
4528                                             SS_ISCONFIRMING |
4529                                             SS_ISCONNECTED);
4530                                 }
4531                                 SOCK_UNLOCK(so);
4532                                 socantrcvmore(so);
4533                                 sctp_sowwakeup(inp, so);
4534                                 sctp_sorwakeup(inp, so);
4535                                 SCTP_SOWAKEUP(so);
4536                         }
4537                 }
4538         }
4539         /*
4540          * Make it invalid too, that way if its about to run it will abort
4541          * and return.
4542          */
4543         sctp_iterator_asoc_being_freed(inp, stcb);
4544         /* re-increment the lock */
4545         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4546                 atomic_add_int(&stcb->asoc.refcnt, -1);
4547         }
4548         asoc->state = 0;
4549         if (inp->sctp_tcbhash) {
4550                 LIST_REMOVE(stcb, sctp_tcbhash);
4551         }
4552         if (stcb->asoc.in_restart_hash) {
4553                 LIST_REMOVE(stcb, sctp_tcbrestarhash);
4554         }
4555         /* Now lets remove it from the list of ALL associations in the EP */
4556         LIST_REMOVE(stcb, sctp_tcblist);
4557         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4558                 SCTP_INP_INCR_REF(inp);
4559                 SCTP_INP_WUNLOCK(inp);
4560                 SCTP_ITERATOR_UNLOCK();
4561         }
4562         /* pull from vtag hash */
4563         LIST_REMOVE(stcb, sctp_asocs);
4564         sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_TIME_WAIT);
4565
4566         /*
4567          * Now restop the timers to be sure - this is paranoia at is finest!
4568          */
4569         (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4570         (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4571         (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4572         (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4573         (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4574         (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4575         (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4576         (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4577         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4578                 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4579                 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4580                 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4581         }
4582
4583         asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4584         prev = NULL;
4585         /*
4586          * The chunk lists and such SHOULD be empty but we check them just
4587          * in case.
4588          */
4589         /* anything on the wheel needs to be removed */
4590         for (i = 0; i < asoc->streamoutcnt; i++) {
4591                 struct sctp_stream_out *outs;
4592
4593                 outs = &asoc->strmout[i];
4594                 /* now clean up any chunks here */
4595                 sp = TAILQ_FIRST(&outs->outqueue);
4596                 while (sp) {
4597                         TAILQ_REMOVE(&outs->outqueue, sp, next);
4598                         if (sp->data) {
4599                                 sctp_m_freem(sp->data);
4600                                 sp->data = NULL;
4601                                 sp->tail_mbuf = NULL;
4602                         }
4603                         sctp_free_remote_addr(sp->net);
4604                         sctp_free_spbufspace(stcb, asoc, sp);
4605                         /* Free the zone stuff  */
4606                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), sp);
4607                         SCTP_DECR_STRMOQ_COUNT();
4608                         /* sa_ignore FREED_MEMORY */
4609                         sp = TAILQ_FIRST(&outs->outqueue);
4610                 }
4611         }
4612
4613         /* sa_ignore FREED_MEMORY */
4614         while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
4615                 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
4616                 SCTP_FREE(liste, SCTP_M_STRESET);
4617         }
4618
4619         sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4620         while (sq) {
4621                 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
4622                 if (sq->data) {
4623                         sctp_m_freem(sq->data);
4624                         sq->data = NULL;
4625                 }
4626                 sctp_free_remote_addr(sq->whoFrom);
4627                 sq->whoFrom = NULL;
4628                 sq->stcb = NULL;
4629                 /* Free the ctl entry */
4630                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
4631                 SCTP_DECR_READQ_COUNT();
4632                 /* sa_ignore FREED_MEMORY */
4633                 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4634         }
4635
4636         chk = TAILQ_FIRST(&asoc->free_chunks);
4637         while (chk) {
4638                 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
4639                 if (chk->data) {
4640                         sctp_m_freem(chk->data);
4641                         chk->data = NULL;
4642                 }
4643                 ccnt++;
4644                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4645                 SCTP_DECR_CHK_COUNT();
4646                 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
4647                 asoc->free_chunk_cnt--;
4648                 /* sa_ignore FREED_MEMORY */
4649                 chk = TAILQ_FIRST(&asoc->free_chunks);
4650         }
4651         /* pending send queue SHOULD be empty */
4652         if (!TAILQ_EMPTY(&asoc->send_queue)) {
4653                 chk = TAILQ_FIRST(&asoc->send_queue);
4654                 while (chk) {
4655                         TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4656                         if (chk->data) {
4657                                 sctp_m_freem(chk->data);
4658                                 chk->data = NULL;
4659                         }
4660                         ccnt++;
4661                         sctp_free_remote_addr(chk->whoTo);
4662                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4663                         SCTP_DECR_CHK_COUNT();
4664                         /* sa_ignore FREED_MEMORY */
4665                         chk = TAILQ_FIRST(&asoc->send_queue);
4666                 }
4667         }
4668 /*
4669   if(ccnt) {
4670   printf("Freed %d from send_queue\n", ccnt);
4671   ccnt = 0;
4672   }
4673 */
4674         /* sent queue SHOULD be empty */
4675         if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4676                 chk = TAILQ_FIRST(&asoc->sent_queue);
4677                 while (chk) {
4678                         TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4679                         if (chk->data) {
4680                                 sctp_m_freem(chk->data);
4681                                 chk->data = NULL;
4682                         }
4683                         ccnt++;
4684                         sctp_free_remote_addr(chk->whoTo);
4685                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4686                         SCTP_DECR_CHK_COUNT();
4687                         /* sa_ignore FREED_MEMORY */
4688                         chk = TAILQ_FIRST(&asoc->sent_queue);
4689                 }
4690         }
4691 /*
4692   if(ccnt) {
4693   printf("Freed %d from sent_queue\n", ccnt);
4694   ccnt = 0;
4695   }
4696 */
4697         /* control queue MAY not be empty */
4698         if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
4699                 chk = TAILQ_FIRST(&asoc->control_send_queue);
4700                 while (chk) {
4701                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4702                         if (chk->data) {
4703                                 sctp_m_freem(chk->data);
4704                                 chk->data = NULL;
4705                         }
4706                         ccnt++;
4707                         sctp_free_remote_addr(chk->whoTo);
4708                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4709                         SCTP_DECR_CHK_COUNT();
4710                         /* sa_ignore FREED_MEMORY */
4711                         chk = TAILQ_FIRST(&asoc->control_send_queue);
4712                 }
4713         }
4714 /*
4715   if(ccnt) {
4716   printf("Freed %d from ctrl_queue\n", ccnt);
4717   ccnt = 0;
4718   }
4719 */
4720
4721         /* ASCONF queue MAY not be empty */
4722         if (!TAILQ_EMPTY(&asoc->asconf_send_queue)) {
4723                 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
4724                 while (chk) {
4725                         TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
4726                         if (chk->data) {
4727                                 sctp_m_freem(chk->data);
4728                                 chk->data = NULL;
4729                         }
4730                         ccnt++;
4731                         sctp_free_remote_addr(chk->whoTo);
4732                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4733                         SCTP_DECR_CHK_COUNT();
4734                         /* sa_ignore FREED_MEMORY */
4735                         chk = TAILQ_FIRST(&asoc->asconf_send_queue);
4736                 }
4737         }
4738 /*
4739   if(ccnt) {
4740   printf("Freed %d from asconf_queue\n", ccnt);
4741   ccnt = 0;
4742   }
4743 */
4744         if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
4745                 chk = TAILQ_FIRST(&asoc->reasmqueue);
4746                 while (chk) {
4747                         TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4748                         if (chk->data) {
4749                                 sctp_m_freem(chk->data);
4750                                 chk->data = NULL;
4751                         }
4752                         sctp_free_remote_addr(chk->whoTo);
4753                         ccnt++;
4754                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4755                         SCTP_DECR_CHK_COUNT();
4756                         /* sa_ignore FREED_MEMORY */
4757                         chk = TAILQ_FIRST(&asoc->reasmqueue);
4758                 }
4759         }
4760 /*
4761   if(ccnt) {
4762   printf("Freed %d from reasm_queue\n", ccnt);
4763   ccnt = 0;
4764   }
4765 */
4766         if (asoc->mapping_array) {
4767                 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4768                 asoc->mapping_array = NULL;
4769         }
4770         /* the stream outs */
4771         if (asoc->strmout) {
4772                 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4773                 asoc->strmout = NULL;
4774         }
4775         asoc->streamoutcnt = 0;
4776         if (asoc->strmin) {
4777                 struct sctp_queued_to_read *ctl;
4778
4779                 for (i = 0; i < asoc->streamincnt; i++) {
4780                         if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
4781                                 /* We have somethings on the streamin queue */
4782                                 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4783                                 while (ctl) {
4784                                         TAILQ_REMOVE(&asoc->strmin[i].inqueue,
4785                                             ctl, next);
4786                                         sctp_free_remote_addr(ctl->whoFrom);
4787                                         if (ctl->data) {
4788                                                 sctp_m_freem(ctl->data);
4789                                                 ctl->data = NULL;
4790                                         }
4791                                         /*
4792                                          * We don't free the address here
4793                                          * since all the net's were freed
4794                                          * above.
4795                                          */
4796                                         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
4797                                         SCTP_DECR_READQ_COUNT();
4798                                         ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4799                                 }
4800                         }
4801                 }
4802                 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
4803                 asoc->strmin = NULL;
4804         }
4805         asoc->streamincnt = 0;
4806         while (!TAILQ_EMPTY(&asoc->nets)) {
4807                 /* sa_ignore FREED_MEMORY */
4808                 net = TAILQ_FIRST(&asoc->nets);
4809                 /* pull from list */
4810                 if ((SCTP_BASE_INFO(ipi_count_raddr) == 0) || (prev == net)) {
4811 #ifdef INVARIANTS
4812                         panic("no net's left alloc'ed, or list points to itself");
4813 #endif
4814                         break;
4815                 }
4816                 prev = net;
4817                 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4818                 sctp_free_remote_addr(net);
4819         }
4820
4821         while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
4822                 /* sa_ignore FREED_MEMORY */
4823                 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
4824                 sctp_remove_laddr(laddr);
4825         }
4826
4827         /* pending asconf (address) parameters */
4828         while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
4829                 /* sa_ignore FREED_MEMORY */
4830                 aparam = TAILQ_FIRST(&asoc->asconf_queue);
4831                 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
4832                 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
4833         }
4834         while (!TAILQ_EMPTY(&asoc->asconf_ack_sent)) {
4835                 /* sa_ignore FREED_MEMORY */
4836                 aack = TAILQ_FIRST(&asoc->asconf_ack_sent);
4837                 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
4838                 if (aack->data != NULL) {
4839                         sctp_m_freem(aack->data);
4840                 }
4841                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
4842         }
4843         /* clean up auth stuff */
4844         if (asoc->local_hmacs)
4845                 sctp_free_hmaclist(asoc->local_hmacs);
4846         if (asoc->peer_hmacs)
4847                 sctp_free_hmaclist(asoc->peer_hmacs);
4848
4849         if (asoc->local_auth_chunks)
4850                 sctp_free_chunklist(asoc->local_auth_chunks);
4851         if (asoc->peer_auth_chunks)
4852                 sctp_free_chunklist(asoc->peer_auth_chunks);
4853
4854         sctp_free_authinfo(&asoc->authinfo);
4855
4856         shared_key = LIST_FIRST(&asoc->shared_keys);
4857         while (shared_key) {
4858                 LIST_REMOVE(shared_key, next);
4859                 sctp_free_sharedkey(shared_key);
4860                 /* sa_ignore FREED_MEMORY */
4861                 shared_key = LIST_FIRST(&asoc->shared_keys);
4862         }
4863
4864         /* Insert new items here :> */
4865
4866         /* Get rid of LOCK */
4867         SCTP_TCB_LOCK_DESTROY(stcb);
4868         SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4869         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4870                 SCTP_INP_INFO_WUNLOCK();
4871                 SCTP_INP_RLOCK(inp);
4872         }
4873 #ifdef SCTP_TRACK_FREED_ASOCS
4874         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4875                 /* now clean up the tasoc itself */
4876                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4877                 SCTP_DECR_ASOC_COUNT();
4878         } else {
4879                 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
4880         }
4881 #else
4882         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4883         SCTP_DECR_ASOC_COUNT();
4884 #endif
4885         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4886                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4887                         /*
4888                          * If its NOT the inp_free calling us AND sctp_close
4889                          * as been called, we call back...
4890                          */
4891                         SCTP_INP_RUNLOCK(inp);
4892                         /*
4893                          * This will start the kill timer (if we are the
4894                          * lastone) since we hold an increment yet. But this
4895                          * is the only safe way to do this since otherwise
4896                          * if the socket closes at the same time we are here
4897                          * we might collide in the cleanup.
4898                          */
4899                         sctp_inpcb_free(inp,
4900                             SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
4901                             SCTP_CALLED_DIRECTLY_NOCMPSET);
4902                         SCTP_INP_DECR_REF(inp);
4903                         goto out_of;
4904                 } else {
4905                         /* The socket is still open. */
4906                         SCTP_INP_DECR_REF(inp);
4907                 }
4908         }
4909         if (from_inpcbfree == SCTP_NORMAL_PROC) {
4910                 SCTP_INP_RUNLOCK(inp);
4911         }
4912 out_of:
4913         /* destroyed the asoc */
4914 #ifdef SCTP_LOG_CLOSING
4915         sctp_log_closing(inp, NULL, 11);
4916 #endif
4917         return (1);
4918 }
4919
4920
4921
4922 /*
4923  * determine if a destination is "reachable" based upon the addresses bound
4924  * to the current endpoint (e.g. only v4 or v6 currently bound)
4925  */
4926 /*
4927  * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4928  * assoc level v4/v6 flags, as the assoc *may* not have the same address
4929  * types bound as its endpoint
4930  */
4931 int
4932 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4933 {
4934         struct sctp_inpcb *inp;
4935         int answer;
4936
4937         /*
4938          * No locks here, the TCB, in all cases is already locked and an
4939          * assoc is up. There is either a INP lock by the caller applied (in
4940          * asconf case when deleting an address) or NOT in the HB case,
4941          * however if HB then the INP increment is up and the INP will not
4942          * be removed (on top of the fact that we have a TCB lock). So we
4943          * only want to read the sctp_flags, which is either bound-all or
4944          * not.. no protection needed since once an assoc is up you can't be
4945          * changing your binding.
4946          */
4947         inp = stcb->sctp_ep;
4948         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4949                 /* if bound all, destination is not restricted */
4950                 /*
4951                  * RRS: Question during lock work: Is this correct? If you
4952                  * are bound-all you still might need to obey the V4--V6
4953                  * flags??? IMO this bound-all stuff needs to be removed!
4954                  */
4955                 return (1);
4956         }
4957         /* NOTE: all "scope" checks are done when local addresses are added */
4958         if (destaddr->sa_family == AF_INET6) {
4959                 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4960         } else if (destaddr->sa_family == AF_INET) {
4961                 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4962         } else {
4963                 /* invalid family, so it's unreachable */
4964                 answer = 0;
4965         }
4966         return (answer);
4967 }
4968
4969 /*
4970  * update the inp_vflags on an endpoint
4971  */
4972 static void
4973 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4974 {
4975         struct sctp_laddr *laddr;
4976
4977         /* first clear the flag */
4978         inp->ip_inp.inp.inp_vflag = 0;
4979         /* set the flag based on addresses on the ep list */
4980         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4981                 if (laddr->ifa == NULL) {
4982                         SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
4983                             __FUNCTION__);
4984                         continue;
4985                 }
4986                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4987                         continue;
4988                 }
4989                 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4990                         inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4991                 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4992                         inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4993                 }
4994         }
4995 }
4996
4997 /*
4998  * Add the address to the endpoint local address list There is nothing to be
4999  * done if we are bound to all addresses
5000  */
5001 void
5002 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5003 {
5004         struct sctp_laddr *laddr;
5005         int fnd, error = 0;
5006
5007         fnd = 0;
5008
5009         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5010                 /* You are already bound to all. You have it already */
5011                 return;
5012         }
5013         if (ifa->address.sa.sa_family == AF_INET6) {
5014                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5015                         /* Can't bind a non-useable addr. */
5016                         return;
5017                 }
5018         }
5019         /* first, is it already present? */
5020         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5021                 if (laddr->ifa == ifa) {
5022                         fnd = 1;
5023                         break;
5024                 }
5025         }
5026
5027         if (fnd == 0) {
5028                 /* Not in the ep list */
5029                 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5030                 if (error != 0)
5031                         return;
5032                 inp->laddr_count++;
5033                 /* update inp_vflag flags */
5034                 if (ifa->address.sa.sa_family == AF_INET6) {
5035                         inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5036                 } else if (ifa->address.sa.sa_family == AF_INET) {
5037                         inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5038                 }
5039         }
5040         return;
5041 }
5042
5043
5044 /*
5045  * select a new (hopefully reachable) destination net (should only be used
5046  * when we deleted an ep addr that is the only usable source address to reach
5047  * the destination net)
5048  */
5049 static void
5050 sctp_select_primary_destination(struct sctp_tcb *stcb)
5051 {
5052         struct sctp_nets *net;
5053
5054         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5055                 /* for now, we'll just pick the first reachable one we find */
5056                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5057                         continue;
5058                 if (sctp_destination_is_reachable(stcb,
5059                     (struct sockaddr *)&net->ro._l_addr)) {
5060                         /* found a reachable destination */
5061                         stcb->asoc.primary_destination = net;
5062                 }
5063         }
5064         /* I can't there from here! ...we're gonna die shortly... */
5065 }
5066
5067
5068 /*
5069  * Delete the address from the endpoint local address list There is nothing
5070  * to be done if we are bound to all addresses
5071  */
5072 void
5073 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5074 {
5075         struct sctp_laddr *laddr;
5076         int fnd;
5077
5078         fnd = 0;
5079         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5080                 /* You are already bound to all. You have it already */
5081                 return;
5082         }
5083         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5084                 if (laddr->ifa == ifa) {
5085                         fnd = 1;
5086                         break;
5087                 }
5088         }
5089         if (fnd && (inp->laddr_count < 2)) {
5090                 /* can't delete unless there are at LEAST 2 addresses */
5091                 return;
5092         }
5093         if (fnd) {
5094                 /*
5095                  * clean up any use of this address go through our
5096                  * associations and clear any last_used_address that match
5097                  * this one for each assoc, see if a new primary_destination
5098                  * is needed
5099                  */
5100                 struct sctp_tcb *stcb;
5101
5102                 /* clean up "next_addr_touse" */
5103                 if (inp->next_addr_touse == laddr)
5104                         /* delete this address */
5105                         inp->next_addr_touse = NULL;
5106
5107                 /* clean up "last_used_address" */
5108                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5109                         struct sctp_nets *net;
5110
5111                         SCTP_TCB_LOCK(stcb);
5112                         if (stcb->asoc.last_used_address == laddr)
5113                                 /* delete this address */
5114                                 stcb->asoc.last_used_address = NULL;
5115                         /*
5116                          * Now spin through all the nets and purge any ref
5117                          * to laddr
5118                          */
5119                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5120                                 if (net->ro._s_addr &&
5121                                     (net->ro._s_addr->ifa == laddr->ifa)) {
5122                                         /* Yep, purge src address selected */
5123                                         sctp_rtentry_t *rt;
5124
5125                                         /* delete this address if cached */
5126                                         rt = net->ro.ro_rt;
5127                                         if (rt != NULL) {
5128                                                 RTFREE(rt);
5129                                                 net->ro.ro_rt = NULL;
5130                                         }
5131                                         sctp_free_ifa(net->ro._s_addr);
5132                                         net->ro._s_addr = NULL;
5133                                         net->src_addr_selected = 0;
5134                                 }
5135                         }
5136                         SCTP_TCB_UNLOCK(stcb);
5137                 }               /* for each tcb */
5138                 /* remove it from the ep list */
5139                 sctp_remove_laddr(laddr);
5140                 inp->laddr_count--;
5141                 /* update inp_vflag flags */
5142                 sctp_update_ep_vflag(inp);
5143         }
5144         return;
5145 }
5146
5147 /*
5148  * Add the address to the TCB local address restricted list.
5149  * This is a "pending" address list (eg. addresses waiting for an
5150  * ASCONF-ACK response) and cannot be used as a valid source address.
5151  */
5152 void
5153 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5154 {
5155         struct sctp_inpcb *inp;
5156         struct sctp_laddr *laddr;
5157         struct sctpladdr *list;
5158
5159         /*
5160          * Assumes TCB is locked.. and possibly the INP. May need to
5161          * confirm/fix that if we need it and is not the case.
5162          */
5163         list = &stcb->asoc.sctp_restricted_addrs;
5164
5165         inp = stcb->sctp_ep;
5166         if (ifa->address.sa.sa_family == AF_INET6) {
5167                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5168                         /* Can't bind a non-existent addr. */
5169                         return;
5170                 }
5171         }
5172         /* does the address already exist? */
5173         LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5174                 if (laddr->ifa == ifa) {
5175                         return;
5176                 }
5177         }
5178
5179         /* add to the list */
5180         (void)sctp_insert_laddr(list, ifa, 0);
5181         return;
5182 }
5183
5184 /*
5185  * insert an laddr entry with the given ifa for the desired list
5186  */
5187 int
5188 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5189 {
5190         struct sctp_laddr *laddr;
5191
5192         laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
5193         if (laddr == NULL) {
5194                 /* out of memory? */
5195                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5196                 return (EINVAL);
5197         }
5198         SCTP_INCR_LADDR_COUNT();
5199         bzero(laddr, sizeof(*laddr));
5200         (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5201         laddr->ifa = ifa;
5202         laddr->action = act;
5203         atomic_add_int(&ifa->refcount, 1);
5204         /* insert it */
5205         LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5206
5207         return (0);
5208 }
5209
5210 /*
5211  * Remove an laddr entry from the local address list (on an assoc)
5212  */
5213 void
5214 sctp_remove_laddr(struct sctp_laddr *laddr)
5215 {
5216
5217         /* remove from the list */
5218         LIST_REMOVE(laddr, sctp_nxt_addr);
5219         sctp_free_ifa(laddr->ifa);
5220         SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
5221         SCTP_DECR_LADDR_COUNT();
5222 }
5223
5224 /*
5225  * Remove a local address from the TCB local address restricted list
5226  */
5227 void
5228 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5229 {
5230         struct sctp_inpcb *inp;
5231         struct sctp_laddr *laddr;
5232
5233         /*
5234          * This is called by asconf work. It is assumed that a) The TCB is
5235          * locked and b) The INP is locked. This is true in as much as I can
5236          * trace through the entry asconf code where I did these locks.
5237          * Again, the ASCONF code is a bit different in that it does lock
5238          * the INP during its work often times. This must be since we don't
5239          * want other proc's looking up things while what they are looking
5240          * up is changing :-D
5241          */
5242
5243         inp = stcb->sctp_ep;
5244         /* if subset bound and don't allow ASCONF's, can't delete last */
5245         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5246             sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5247                 if (stcb->sctp_ep->laddr_count < 2) {
5248                         /* can't delete last address */
5249                         return;
5250                 }
5251         }
5252         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5253                 /* remove the address if it exists */
5254                 if (laddr->ifa == NULL)
5255                         continue;
5256                 if (laddr->ifa == ifa) {
5257                         sctp_remove_laddr(laddr);
5258                         return;
5259                 }
5260         }
5261
5262         /* address not found! */
5263         return;
5264 }
5265
5266 /*
5267  * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5268  */
5269 /* sysctl */
5270 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5271 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5272
5273 void
5274 sctp_pcb_init()
5275 {
5276         /*
5277          * SCTP initialization for the PCB structures should be called by
5278          * the sctp_init() funciton.
5279          */
5280         int i;
5281         struct timeval tv;
5282
5283         if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5284                 /* error I was called twice */
5285                 return;
5286         }
5287         SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5288
5289 #if defined(SCTP_LOCAL_TRACE_BUF)
5290         bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
5291 #endif
5292         (void)SCTP_GETTIME_TIMEVAL(&tv);
5293         SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t) tv.tv_sec;
5294         SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t) tv.tv_usec;
5295         /* init the empty list of (All) Endpoints */
5296         LIST_INIT(&SCTP_BASE_INFO(listhead));
5297
5298         /* init the iterator head */
5299         TAILQ_INIT(&SCTP_BASE_INFO(iteratorhead));
5300
5301         /* init the hash table of endpoints */
5302         TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5303         TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5304         TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5305         SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5306             &SCTP_BASE_INFO(hashasocmark));
5307         SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5308             &SCTP_BASE_INFO(hashmark));
5309         SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5310             &SCTP_BASE_INFO(hashtcpmark));
5311         SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5312
5313         /* init the small hash table we use to track restarted asoc's */
5314         SCTP_BASE_INFO(sctp_restarthash) = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
5315             &SCTP_BASE_INFO(hashrestartmark));
5316
5317
5318         SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5319             &SCTP_BASE_INFO(hashvrfmark));
5320
5321         SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5322             &SCTP_BASE_INFO(vrf_ifn_hashmark));
5323
5324         /* init the zones */
5325         /*
5326          * FIX ME: Should check for NULL returns, but if it does fail we are
5327          * doomed to panic anyways... add later maybe.
5328          */
5329         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5330             sizeof(struct sctp_inpcb), maxsockets);
5331
5332         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5333             sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5334
5335         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5336             sizeof(struct sctp_laddr),
5337             (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5338
5339         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5340             sizeof(struct sctp_nets),
5341             (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5342
5343         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5344             sizeof(struct sctp_tmit_chunk),
5345             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5346
5347         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5348             sizeof(struct sctp_queued_to_read),
5349             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5350
5351         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5352             sizeof(struct sctp_stream_queue_pending),
5353             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5354
5355         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5356             sizeof(struct sctp_asconf),
5357             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5358
5359         SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
5360             sizeof(struct sctp_asconf_ack),
5361             (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5362
5363
5364         /* Master Lock INIT for info structure */
5365         SCTP_INP_INFO_LOCK_INIT();
5366         SCTP_STATLOG_INIT_LOCK();
5367         SCTP_ITERATOR_LOCK_INIT();
5368
5369         SCTP_IPI_COUNT_INIT();
5370         SCTP_IPI_ADDR_INIT();
5371         SCTP_IPI_ITERATOR_WQ_INIT();
5372 #ifdef SCTP_PACKET_LOGGING
5373         SCTP_IP_PKTLOG_INIT();
5374 #endif
5375         LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5376
5377         /* not sure if we need all the counts */
5378         SCTP_BASE_INFO(ipi_count_ep) = 0;
5379         /* assoc/tcb zone info */
5380         SCTP_BASE_INFO(ipi_count_asoc) = 0;
5381         /* local addrlist zone info */
5382         SCTP_BASE_INFO(ipi_count_laddr) = 0;
5383         /* remote addrlist zone info */
5384         SCTP_BASE_INFO(ipi_count_raddr) = 0;
5385         /* chunk info */
5386         SCTP_BASE_INFO(ipi_count_chunk) = 0;
5387
5388         /* socket queue zone info */
5389         SCTP_BASE_INFO(ipi_count_readq) = 0;
5390
5391         /* stream out queue cont */
5392         SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5393
5394         SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5395         SCTP_BASE_INFO(ipi_free_chunks) = 0;
5396
5397         SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5398
5399         /* Init the TIMEWAIT list */
5400         for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
5401                 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait[i]));
5402         }
5403
5404 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5405         SCTP_BASE_INFO(iterator_running) = 0;
5406         SCTP_BASE_INFO(threads_must_exit) = 0;
5407         sctp_startup_iterator();
5408 #endif
5409
5410         /*
5411          * INIT the default VRF which for BSD is the only one, other O/S's
5412          * may have more. But initially they must start with one and then
5413          * add the VRF's as addresses are added.
5414          */
5415         sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5416
5417 }
5418
5419 /*
5420  * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5421  */
5422 void
5423 sctp_pcb_finish(void)
5424 {
5425         struct sctp_vrflist *vrf_bucket;
5426         struct sctp_vrf *vrf;
5427         struct sctp_ifn *ifn;
5428         struct sctp_ifa *ifa;
5429         struct sctpvtaghead *chain;
5430         struct sctp_tagblock *twait_block, *prev_twait_block;
5431         int i;
5432
5433         /*
5434          * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
5435          * destroyed first).
5436          */
5437         vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
5438         vrf = LIST_FIRST(vrf_bucket);
5439         while (vrf) {
5440                 ifn = LIST_FIRST(&vrf->ifnlist);
5441                 while (ifn) {
5442                         ifa = LIST_FIRST(&ifn->ifalist);
5443                         while (ifa) {
5444                                 /* free the ifa */
5445                                 LIST_REMOVE(ifa, next_bucket);
5446                                 LIST_REMOVE(ifa, next_ifa);
5447                                 SCTP_FREE(ifa, SCTP_M_IFA);
5448                                 ifa = LIST_FIRST(&ifn->ifalist);
5449                         }
5450                         /* free the ifn */
5451                         LIST_REMOVE(ifn, next_bucket);
5452                         LIST_REMOVE(ifn, next_ifn);
5453                         SCTP_FREE(ifn, SCTP_M_IFN);
5454                         ifn = LIST_FIRST(&vrf->ifnlist);
5455                 }
5456                 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
5457                 /* free the vrf */
5458                 LIST_REMOVE(vrf, next_vrf);
5459                 SCTP_FREE(vrf, SCTP_M_VRF);
5460                 vrf = LIST_FIRST(vrf_bucket);
5461         }
5462         /* free the vrf hashes */
5463         SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
5464         SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
5465
5466         /*
5467          * free the TIMEWAIT list elements malloc'd in the function
5468          * sctp_add_vtag_to_timewait()...
5469          */
5470         for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
5471                 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
5472                 if (!SCTP_LIST_EMPTY(chain)) {
5473                         prev_twait_block = NULL;
5474                         LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5475                                 if (prev_twait_block) {
5476                                         SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5477                                 }
5478                                 prev_twait_block = twait_block;
5479                         }
5480                         SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5481                 }
5482         }
5483
5484         /* free the locks and mutexes */
5485 #ifdef SCTP_PACKET_LOGGING
5486         SCTP_IP_PKTLOG_DESTROY();
5487
5488 #endif
5489         SCTP_IPI_ITERATOR_WQ_DESTROY();
5490         SCTP_IPI_ADDR_DESTROY();
5491         SCTP_ITERATOR_LOCK_DESTROY();
5492         SCTP_STATLOG_DESTROY();
5493         SCTP_INP_INFO_LOCK_DESTROY();
5494
5495         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
5496         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
5497         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
5498         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
5499         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
5500         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
5501         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
5502         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
5503         SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
5504         /* Get rid of other stuff to */
5505         if (SCTP_BASE_INFO(sctp_asochash) != NULL)
5506                 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
5507         if (SCTP_BASE_INFO(sctp_ephash) != NULL)
5508                 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
5509         if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
5510                 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
5511         if (SCTP_BASE_INFO(sctp_restarthash) != NULL)
5512                 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_restarthash), SCTP_BASE_INFO(hashrestartmark));
5513
5514 }
5515
5516
5517 int
5518 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
5519     int iphlen, int offset, int limit, struct sctphdr *sh,
5520     struct sockaddr *altsa)
5521 {
5522         /*
5523          * grub through the INIT pulling addresses and loading them to the
5524          * nets structure in the asoc. The from address in the mbuf should
5525          * also be loaded (if it is not already). This routine can be called
5526          * with either INIT or INIT-ACK's as long as the m points to the IP
5527          * packet and the offset points to the beginning of the parameters.
5528          */
5529         struct sctp_inpcb *inp, *l_inp;
5530         struct sctp_nets *net, *net_tmp;
5531         struct ip *iph;
5532         struct sctp_paramhdr *phdr, parm_buf;
5533         struct sctp_tcb *stcb_tmp;
5534         uint16_t ptype, plen;
5535         struct sockaddr *sa;
5536         struct sockaddr_storage dest_store;
5537         struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
5538         struct sockaddr_in sin;
5539         struct sockaddr_in6 sin6;
5540         uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
5541         struct sctp_auth_random *p_random = NULL;
5542         uint16_t random_len = 0;
5543         uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
5544         struct sctp_auth_hmac_algo *hmacs = NULL;
5545         uint16_t hmacs_len = 0;
5546         uint8_t saw_asconf = 0;
5547         uint8_t saw_asconf_ack = 0;
5548         uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
5549         struct sctp_auth_chunk_list *chunks = NULL;
5550         uint16_t num_chunks = 0;
5551         sctp_key_t *new_key;
5552         uint32_t keylen;
5553         int got_random = 0, got_hmacs = 0, got_chklist = 0;
5554
5555         /* First get the destination address setup too. */
5556         memset(&sin, 0, sizeof(sin));
5557         memset(&sin6, 0, sizeof(sin6));
5558
5559         sin.sin_family = AF_INET;
5560         sin.sin_len = sizeof(sin);
5561         sin.sin_port = stcb->rport;
5562
5563         sin6.sin6_family = AF_INET6;
5564         sin6.sin6_len = sizeof(struct sockaddr_in6);
5565         sin6.sin6_port = stcb->rport;
5566         if (altsa == NULL) {
5567                 iph = mtod(m, struct ip *);
5568                 switch (iph->ip_v) {
5569                 case IPVERSION:
5570                         {
5571                                 /* its IPv4 */
5572                                 struct sockaddr_in *sin_2;
5573
5574                                 sin_2 = (struct sockaddr_in *)(local_sa);
5575                                 memset(sin_2, 0, sizeof(sin));
5576                                 sin_2->sin_family = AF_INET;
5577                                 sin_2->sin_len = sizeof(sin);
5578                                 sin_2->sin_port = sh->dest_port;
5579                                 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
5580                                 sin.sin_addr = iph->ip_src;
5581                                 sa = (struct sockaddr *)&sin;
5582                                 break;
5583                         }
5584 #ifdef INET6
5585                 case IPV6_VERSION >> 4:
5586                         {
5587                                 /* its IPv6 */
5588                                 struct ip6_hdr *ip6;
5589                                 struct sockaddr_in6 *sin6_2;
5590
5591                                 ip6 = mtod(m, struct ip6_hdr *);
5592                                 sin6_2 = (struct sockaddr_in6 *)(local_sa);
5593                                 memset(sin6_2, 0, sizeof(sin6));
5594                                 sin6_2->sin6_family = AF_INET6;
5595                                 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
5596                                 sin6_2->sin6_port = sh->dest_port;
5597                                 sin6.sin6_addr = ip6->ip6_src;
5598                                 sa = (struct sockaddr *)&sin6;
5599                                 break;
5600                         }
5601 #endif
5602                 default:
5603                         sa = NULL;
5604                         break;
5605                 }
5606         } else {
5607                 /*
5608                  * For cookies we use the src address NOT from the packet
5609                  * but from the original INIT
5610                  */
5611                 sa = altsa;
5612         }
5613         /* Turn off ECN until we get through all params */
5614         stcb->asoc.ecn_allowed = 0;
5615         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5616                 /* mark all addresses that we have currently on the list */
5617                 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
5618         }
5619         /* does the source address already exist? if so skip it */
5620         l_inp = inp = stcb->sctp_ep;
5621
5622         atomic_add_int(&stcb->asoc.refcnt, 1);
5623         stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
5624         atomic_add_int(&stcb->asoc.refcnt, -1);
5625
5626         if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
5627                 /* we must add the source address */
5628                 /* no scope set here since we have a tcb already. */
5629                 if ((sa->sa_family == AF_INET) &&
5630                     (stcb->asoc.ipv4_addr_legal)) {
5631                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
5632                                 return (-1);
5633                         }
5634                 } else if ((sa->sa_family == AF_INET6) &&
5635                     (stcb->asoc.ipv6_addr_legal)) {
5636                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
5637                                 return (-2);
5638                         }
5639                 }
5640         } else {
5641                 if (net_tmp != NULL && stcb_tmp == stcb) {
5642                         net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
5643                 } else if (stcb_tmp != stcb) {
5644                         /* It belongs to another association? */
5645                         if (stcb_tmp)
5646                                 SCTP_TCB_UNLOCK(stcb_tmp);
5647                         return (-3);
5648                 }
5649         }
5650         if (stcb->asoc.state == 0) {
5651                 /* the assoc was freed? */
5652                 return (-4);
5653         }
5654         /*
5655          * peer must explicitly turn this on. This may have been initialized
5656          * to be "on" in order to allow local addr changes while INIT's are
5657          * in flight.
5658          */
5659         stcb->asoc.peer_supports_asconf = 0;
5660         /* now we must go through each of the params. */
5661         phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
5662         while (phdr) {
5663                 ptype = ntohs(phdr->param_type);
5664                 plen = ntohs(phdr->param_length);
5665                 /*
5666                  * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
5667                  * (int)plen);
5668                  */
5669                 if (offset + plen > limit) {
5670                         break;
5671                 }
5672                 if (plen == 0) {
5673                         break;
5674                 }
5675                 if (ptype == SCTP_IPV4_ADDRESS) {
5676                         if (stcb->asoc.ipv4_addr_legal) {
5677                                 struct sctp_ipv4addr_param *p4, p4_buf;
5678
5679                                 /* ok get the v4 address and check/add */
5680                                 phdr = sctp_get_next_param(m, offset,
5681                                     (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5682                                 if (plen != sizeof(struct sctp_ipv4addr_param) ||
5683                                     phdr == NULL) {
5684                                         return (-5);
5685                                 }
5686                                 p4 = (struct sctp_ipv4addr_param *)phdr;
5687                                 sin.sin_addr.s_addr = p4->addr;
5688                                 if (IN_MULTICAST(sin.sin_addr.s_addr)) {
5689                                         /* Skip multi-cast addresses */
5690                                         goto next_param;
5691                                 }
5692                                 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
5693                                     (sin.sin_addr.s_addr == INADDR_ANY)) {
5694                                         goto next_param;
5695                                 }
5696                                 sa = (struct sockaddr *)&sin;
5697                                 inp = stcb->sctp_ep;
5698                                 atomic_add_int(&stcb->asoc.refcnt, 1);
5699                                 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5700                                     local_sa, stcb);
5701                                 atomic_add_int(&stcb->asoc.refcnt, -1);
5702
5703                                 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
5704                                     inp == NULL) {
5705                                         /* we must add the source address */
5706                                         /*
5707                                          * no scope set since we have a tcb
5708                                          * already
5709                                          */
5710
5711                                         /*
5712                                          * we must validate the state again
5713                                          * here
5714                                          */
5715                         add_it_now:
5716                                         if (stcb->asoc.state == 0) {
5717                                                 /* the assoc was freed? */
5718                                                 return (-7);
5719                                         }
5720                                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
5721                                                 return (-8);
5722                                         }
5723                                 } else if (stcb_tmp == stcb) {
5724                                         if (stcb->asoc.state == 0) {
5725                                                 /* the assoc was freed? */
5726                                                 return (-10);
5727                                         }
5728                                         if (net != NULL) {
5729                                                 /* clear flag */
5730                                                 net->dest_state &=
5731                                                     ~SCTP_ADDR_NOT_IN_ASSOC;
5732                                         }
5733                                 } else {
5734                                         /*
5735                                          * strange, address is in another
5736                                          * assoc? straighten out locks.
5737                                          */
5738                                         if (stcb_tmp) {
5739                                                 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
5740                                                         /*
5741                                                          * in setup state we
5742                                                          * abort this guy
5743                                                          */
5744                                                         sctp_abort_an_association(stcb_tmp->sctp_ep,
5745                                                             stcb_tmp, 1, NULL, 0);
5746                                                         goto add_it_now;
5747                                                 }
5748                                                 SCTP_TCB_UNLOCK(stcb_tmp);
5749                                         }
5750                                         if (stcb->asoc.state == 0) {
5751                                                 /* the assoc was freed? */
5752                                                 return (-12);
5753                                         }
5754                                         return (-13);
5755                                 }
5756                         }
5757                 } else if (ptype == SCTP_IPV6_ADDRESS) {
5758                         if (stcb->asoc.ipv6_addr_legal) {
5759                                 /* ok get the v6 address and check/add */
5760                                 struct sctp_ipv6addr_param *p6, p6_buf;
5761
5762                                 phdr = sctp_get_next_param(m, offset,
5763                                     (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5764                                 if (plen != sizeof(struct sctp_ipv6addr_param) ||
5765                                     phdr == NULL) {
5766                                         return (-14);
5767                                 }
5768                                 p6 = (struct sctp_ipv6addr_param *)phdr;
5769                                 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5770                                     sizeof(p6->addr));
5771                                 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
5772                                         /* Skip multi-cast addresses */
5773                                         goto next_param;
5774                                 }
5775                                 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
5776                                         /*
5777                                          * Link local make no sense without
5778                                          * scope
5779                                          */
5780                                         goto next_param;
5781                                 }
5782                                 sa = (struct sockaddr *)&sin6;
5783                                 inp = stcb->sctp_ep;
5784                                 atomic_add_int(&stcb->asoc.refcnt, 1);
5785                                 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5786                                     local_sa, stcb);
5787                                 atomic_add_int(&stcb->asoc.refcnt, -1);
5788                                 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
5789                                     inp == NULL)) {
5790                                         /*
5791                                          * we must validate the state again
5792                                          * here
5793                                          */
5794                         add_it_now6:
5795                                         if (stcb->asoc.state == 0) {
5796                                                 /* the assoc was freed? */
5797                                                 return (-16);
5798                                         }
5799                                         /*
5800                                          * we must add the address, no scope
5801                                          * set
5802                                          */
5803                                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
5804                                                 return (-17);
5805                                         }
5806                                 } else if (stcb_tmp == stcb) {
5807                                         /*
5808                                          * we must validate the state again
5809                                          * here
5810                                          */
5811                                         if (stcb->asoc.state == 0) {
5812                                                 /* the assoc was freed? */
5813                                                 return (-19);
5814                                         }
5815                                         if (net != NULL) {
5816                                                 /* clear flag */
5817                                                 net->dest_state &=
5818                                                     ~SCTP_ADDR_NOT_IN_ASSOC;
5819                                         }
5820                                 } else {
5821                                         /*
5822                                          * strange, address is in another
5823                                          * assoc? straighten out locks.
5824                                          */
5825                                         if (stcb_tmp)
5826                                                 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
5827                                                         /*
5828                                                          * in setup state we
5829                                                          * abort this guy
5830                                                          */
5831                                                         sctp_abort_an_association(stcb_tmp->sctp_ep,
5832                                                             stcb_tmp, 1, NULL, 0);
5833                                                         goto add_it_now6;
5834                                                 }
5835                                         SCTP_TCB_UNLOCK(stcb_tmp);
5836
5837                                         if (stcb->asoc.state == 0) {
5838                                                 /* the assoc was freed? */
5839                                                 return (-21);
5840                                         }
5841                                         return (-22);
5842                                 }
5843                         }
5844                 } else if (ptype == SCTP_ECN_CAPABLE) {
5845                         stcb->asoc.ecn_allowed = 1;
5846                 } else if (ptype == SCTP_ULP_ADAPTATION) {
5847                         if (stcb->asoc.state != SCTP_STATE_OPEN) {
5848                                 struct sctp_adaptation_layer_indication ai,
5849                                                                 *aip;
5850
5851                                 phdr = sctp_get_next_param(m, offset,
5852                                     (struct sctp_paramhdr *)&ai, sizeof(ai));
5853                                 aip = (struct sctp_adaptation_layer_indication *)phdr;
5854                                 if (aip) {
5855                                         stcb->asoc.peers_adaptation = ntohl(aip->indication);
5856                                         stcb->asoc.adaptation_needed = 1;
5857                                 }
5858                         }
5859                 } else if (ptype == SCTP_SET_PRIM_ADDR) {
5860                         struct sctp_asconf_addr_param lstore, *fee;
5861                         struct sctp_asconf_addrv4_param *fii;
5862                         int lptype;
5863                         struct sockaddr *lsa = NULL;
5864
5865                         stcb->asoc.peer_supports_asconf = 1;
5866                         if (plen > sizeof(lstore)) {
5867                                 return (-23);
5868                         }
5869                         phdr = sctp_get_next_param(m, offset,
5870                             (struct sctp_paramhdr *)&lstore, min(plen, sizeof(lstore)));
5871                         if (phdr == NULL) {
5872                                 return (-24);
5873                         }
5874                         fee = (struct sctp_asconf_addr_param *)phdr;
5875                         lptype = ntohs(fee->addrp.ph.param_type);
5876                         if (lptype == SCTP_IPV4_ADDRESS) {
5877                                 if (plen !=
5878                                     sizeof(struct sctp_asconf_addrv4_param)) {
5879                                         SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
5880                                             (int)sizeof(struct sctp_asconf_addrv4_param),
5881                                             plen);
5882                                 } else {
5883                                         fii = (struct sctp_asconf_addrv4_param *)fee;
5884                                         sin.sin_addr.s_addr = fii->addrp.addr;
5885                                         lsa = (struct sockaddr *)&sin;
5886                                 }
5887                         } else if (lptype == SCTP_IPV6_ADDRESS) {
5888                                 if (plen !=
5889                                     sizeof(struct sctp_asconf_addr_param)) {
5890                                         SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
5891                                             (int)sizeof(struct sctp_asconf_addr_param),
5892                                             plen);
5893                                 } else {
5894                                         memcpy(sin6.sin6_addr.s6_addr,
5895                                             fee->addrp.addr,
5896                                             sizeof(fee->addrp.addr));
5897                                         lsa = (struct sockaddr *)&sin6;
5898                                 }
5899                         }
5900                         if (lsa) {
5901                                 (void)sctp_set_primary_addr(stcb, sa, NULL);
5902                         }
5903                 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
5904                         /* Peer supports pr-sctp */
5905                         stcb->asoc.peer_supports_prsctp = 1;
5906                 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
5907                         /* A supported extension chunk */
5908                         struct sctp_supported_chunk_types_param *pr_supported;
5909                         uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
5910                         int num_ent, i;
5911
5912                         phdr = sctp_get_next_param(m, offset,
5913                             (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
5914                         if (phdr == NULL) {
5915                                 return (-25);
5916                         }
5917                         stcb->asoc.peer_supports_asconf = 0;
5918                         stcb->asoc.peer_supports_prsctp = 0;
5919                         stcb->asoc.peer_supports_pktdrop = 0;
5920                         stcb->asoc.peer_supports_strreset = 0;
5921                         stcb->asoc.peer_supports_auth = 0;
5922                         pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
5923                         num_ent = plen - sizeof(struct sctp_paramhdr);
5924                         for (i = 0; i < num_ent; i++) {
5925                                 switch (pr_supported->chunk_types[i]) {
5926                                 case SCTP_ASCONF:
5927                                 case SCTP_ASCONF_ACK:
5928                                         stcb->asoc.peer_supports_asconf = 1;
5929                                         break;
5930                                 case SCTP_FORWARD_CUM_TSN:
5931                                         stcb->asoc.peer_supports_prsctp = 1;
5932                                         break;
5933                                 case SCTP_PACKET_DROPPED:
5934                                         stcb->asoc.peer_supports_pktdrop = 1;
5935                                         break;
5936                                 case SCTP_STREAM_RESET:
5937                                         stcb->asoc.peer_supports_strreset = 1;
5938                                         break;
5939                                 case SCTP_AUTHENTICATION:
5940                                         stcb->asoc.peer_supports_auth = 1;
5941                                         break;
5942                                 default:
5943                                         /* one I have not learned yet */
5944                                         break;
5945
5946                                 }
5947                         }
5948                 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
5949                         /* Peer supports ECN-nonce */
5950                         stcb->asoc.peer_supports_ecn_nonce = 1;
5951                         stcb->asoc.ecn_nonce_allowed = 1;
5952                 } else if (ptype == SCTP_RANDOM) {
5953                         if (plen > sizeof(random_store))
5954                                 break;
5955                         if (got_random) {
5956                                 /* already processed a RANDOM */
5957                                 goto next_param;
5958                         }
5959                         phdr = sctp_get_next_param(m, offset,
5960                             (struct sctp_paramhdr *)random_store,
5961                             min(sizeof(random_store), plen));
5962                         if (phdr == NULL)
5963                                 return (-26);
5964                         p_random = (struct sctp_auth_random *)phdr;
5965                         random_len = plen - sizeof(*p_random);
5966                         /* enforce the random length */
5967                         if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
5968                                 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
5969                                 return (-27);
5970                         }
5971                         got_random = 1;
5972                 } else if (ptype == SCTP_HMAC_LIST) {
5973                         int num_hmacs;
5974                         int i;
5975
5976                         if (plen > sizeof(hmacs_store))
5977                                 break;
5978                         if (got_hmacs) {
5979                                 /* already processed a HMAC list */
5980                                 goto next_param;
5981                         }
5982                         phdr = sctp_get_next_param(m, offset,
5983                             (struct sctp_paramhdr *)hmacs_store,
5984                             min(plen, sizeof(hmacs_store)));
5985                         if (phdr == NULL)
5986                                 return (-28);
5987                         hmacs = (struct sctp_auth_hmac_algo *)phdr;
5988                         hmacs_len = plen - sizeof(*hmacs);
5989                         num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
5990                         /* validate the hmac list */
5991                         if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
5992                                 return (-29);
5993                         }
5994                         if (stcb->asoc.peer_hmacs != NULL)
5995                                 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
5996                         stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
5997                         if (stcb->asoc.peer_hmacs != NULL) {
5998                                 for (i = 0; i < num_hmacs; i++) {
5999                                         (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6000                                             ntohs(hmacs->hmac_ids[i]));
6001                                 }
6002                         }
6003                         got_hmacs = 1;
6004                 } else if (ptype == SCTP_CHUNK_LIST) {
6005                         int i;
6006
6007                         if (plen > sizeof(chunks_store))
6008                                 break;
6009                         if (got_chklist) {
6010                                 /* already processed a Chunks list */
6011                                 goto next_param;
6012                         }
6013                         phdr = sctp_get_next_param(m, offset,
6014                             (struct sctp_paramhdr *)chunks_store,
6015                             min(plen, sizeof(chunks_store)));
6016                         if (phdr == NULL)
6017                                 return (-30);
6018                         chunks = (struct sctp_auth_chunk_list *)phdr;
6019                         num_chunks = plen - sizeof(*chunks);
6020                         if (stcb->asoc.peer_auth_chunks != NULL)
6021                                 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6022                         else
6023                                 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6024                         for (i = 0; i < num_chunks; i++) {
6025                                 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
6026                                     stcb->asoc.peer_auth_chunks);
6027                                 /* record asconf/asconf-ack if listed */
6028                                 if (chunks->chunk_types[i] == SCTP_ASCONF)
6029                                         saw_asconf = 1;
6030                                 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6031                                         saw_asconf_ack = 1;
6032
6033                         }
6034                         got_chklist = 1;
6035                 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6036                             (ptype == SCTP_STATE_COOKIE) ||
6037                             (ptype == SCTP_UNRECOG_PARAM) ||
6038                             (ptype == SCTP_COOKIE_PRESERVE) ||
6039                             (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6040                             (ptype == SCTP_ADD_IP_ADDRESS) ||
6041                             (ptype == SCTP_DEL_IP_ADDRESS) ||
6042                             (ptype == SCTP_ERROR_CAUSE_IND) ||
6043                     (ptype == SCTP_SUCCESS_REPORT)) {
6044                          /* don't care */ ;
6045                 } else {
6046                         if ((ptype & 0x8000) == 0x0000) {
6047                                 /*
6048                                  * must stop processing the rest of the
6049                                  * param's. Any report bits were handled
6050                                  * with the call to
6051                                  * sctp_arethere_unrecognized_parameters()
6052                                  * when the INIT or INIT-ACK was first seen.
6053                                  */
6054                                 break;
6055                         }
6056                 }
6057 next_param:
6058                 offset += SCTP_SIZE32(plen);
6059                 if (offset >= limit) {
6060                         break;
6061                 }
6062                 phdr = sctp_get_next_param(m, offset, &parm_buf,
6063                     sizeof(parm_buf));
6064         }
6065         /* Now check to see if we need to purge any addresses */
6066         for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
6067                 net_tmp = TAILQ_NEXT(net, sctp_next);
6068                 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6069                     SCTP_ADDR_NOT_IN_ASSOC) {
6070                         /* This address has been removed from the asoc */
6071                         /* remove and free it */
6072                         stcb->asoc.numnets--;
6073                         TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6074                         sctp_free_remote_addr(net);
6075                         if (net == stcb->asoc.primary_destination) {
6076                                 stcb->asoc.primary_destination = NULL;
6077                                 sctp_select_primary_destination(stcb);
6078                         }
6079                 }
6080         }
6081         /* validate authentication required parameters */
6082         if (got_random && got_hmacs) {
6083                 stcb->asoc.peer_supports_auth = 1;
6084         } else {
6085                 stcb->asoc.peer_supports_auth = 0;
6086         }
6087         if (!stcb->asoc.peer_supports_auth && got_chklist) {
6088                 /* peer does not support auth but sent a chunks list? */
6089                 return (-31);
6090         }
6091         if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
6092             !stcb->asoc.peer_supports_auth) {
6093                 /* peer supports asconf but not auth? */
6094                 return (-32);
6095         } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
6096             ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6097                 return (-33);
6098         }
6099         /* concatenate the full random key */
6100 #ifdef SCTP_AUTH_DRAFT_04
6101         keylen = random_len;
6102         new_key = sctp_alloc_key(keylen);
6103         if (new_key != NULL) {
6104                 /* copy in the RANDOM */
6105                 if (p_random != NULL)
6106                         bcopy(p_random->random_data, new_key->key, random_len);
6107         }
6108 #else
6109         keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
6110             sizeof(*hmacs) + hmacs_len;
6111         new_key = sctp_alloc_key(keylen);
6112         if (new_key != NULL) {
6113                 /* copy in the RANDOM */
6114                 if (p_random != NULL) {
6115                         keylen = sizeof(*p_random) + random_len;
6116                         bcopy(p_random, new_key->key, keylen);
6117                 }
6118                 /* append in the AUTH chunks */
6119                 if (chunks != NULL) {
6120                         bcopy(chunks, new_key->key + keylen,
6121                             sizeof(*chunks) + num_chunks);
6122                         keylen += sizeof(*chunks) + num_chunks;
6123                 }
6124                 /* append in the HMACs */
6125                 if (hmacs != NULL) {
6126                         bcopy(hmacs, new_key->key + keylen,
6127                             sizeof(*hmacs) + hmacs_len);
6128                 }
6129         }
6130 #endif
6131         else {
6132                 /* failed to get memory for the key */
6133                 return (-34);
6134         }
6135         if (stcb->asoc.authinfo.peer_random != NULL)
6136                 sctp_free_key(stcb->asoc.authinfo.peer_random);
6137         stcb->asoc.authinfo.peer_random = new_key;
6138 #ifdef SCTP_AUTH_DRAFT_04
6139         /* don't include the chunks and hmacs for draft -04 */
6140         stcb->asoc.authinfo.peer_random->keylen = random_len;
6141 #endif
6142         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6143         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6144
6145         return (0);
6146 }
6147
6148 int
6149 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6150     struct sctp_nets *net)
6151 {
6152         /* make sure the requested primary address exists in the assoc */
6153         if (net == NULL && sa)
6154                 net = sctp_findnet(stcb, sa);
6155
6156         if (net == NULL) {
6157                 /* didn't find the requested primary address! */
6158                 return (-1);
6159         } else {
6160                 /* set the primary address */
6161                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6162                         /* Must be confirmed, so queue to set */
6163                         net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6164                         return (0);
6165                 }
6166                 stcb->asoc.primary_destination = net;
6167                 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
6168                 net = TAILQ_FIRST(&stcb->asoc.nets);
6169                 if (net != stcb->asoc.primary_destination) {
6170                         /*
6171                          * first one on the list is NOT the primary
6172                          * sctp_cmpaddr() is much more efficent if the
6173                          * primary is the first on the list, make it so.
6174                          */
6175                         TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6176                         TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6177                 }
6178                 return (0);
6179         }
6180 }
6181
6182 int
6183 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now, int save_in_twait)
6184 {
6185         /*
6186          * This function serves two purposes. It will see if a TAG can be
6187          * re-used and return 1 for yes it is ok and 0 for don't use that
6188          * tag. A secondary function it will do is purge out old tags that
6189          * can be removed.
6190          */
6191         struct sctpasochead *head;
6192         struct sctpvtaghead *chain;
6193         struct sctp_tagblock *twait_block;
6194         struct sctp_tcb *stcb;
6195         int i;
6196
6197         SCTP_INP_INFO_WLOCK();
6198         chain = &SCTP_BASE_INFO(vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE))];
6199         /* First is the vtag in use ? */
6200
6201         head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
6202             SCTP_BASE_INFO(hashasocmark))];
6203         if (head == NULL) {
6204                 goto check_restart;
6205         }
6206         LIST_FOREACH(stcb, head, sctp_asocs) {
6207
6208                 if (stcb->asoc.my_vtag == tag) {
6209                         /*
6210                          * We should remove this if and return 0 always if
6211                          * we want vtags unique across all endpoints. For
6212                          * now within a endpoint is ok.
6213                          */
6214                         if (inp == stcb->sctp_ep) {
6215                                 /* bad tag, in use */
6216                                 SCTP_INP_INFO_WUNLOCK();
6217                                 return (0);
6218                         }
6219                 }
6220         }
6221 check_restart:
6222         /* Now lets check the restart hash */
6223         head = &SCTP_BASE_INFO(sctp_restarthash)[SCTP_PCBHASH_ASOC(tag,
6224             SCTP_BASE_INFO(hashrestartmark))];
6225         if (head == NULL) {
6226                 goto check_time_wait;
6227         }
6228         LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
6229                 if (stcb->asoc.assoc_id == tag) {
6230                         /* candidate */
6231                         if (inp == stcb->sctp_ep) {
6232                                 /* bad tag, in use */
6233                                 SCTP_INP_INFO_WUNLOCK();
6234                                 return (0);
6235                         }
6236                 }
6237         }
6238 check_time_wait:
6239         /* Now what about timed wait ? */
6240         if (!SCTP_LIST_EMPTY(chain)) {
6241                 /*
6242                  * Block(s) are present, lets see if we have this tag in the
6243                  * list
6244                  */
6245                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6246                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6247                                 if (twait_block->vtag_block[i].v_tag == 0) {
6248                                         /* not used */
6249                                         continue;
6250                                 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6251                                     now->tv_sec) {
6252                                         /* Audit expires this guy */
6253                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
6254                                         twait_block->vtag_block[i].v_tag = 0;
6255                                 } else if (twait_block->vtag_block[i].v_tag ==
6256                                     tag) {
6257                                         /* Bad tag, sorry :< */
6258                                         SCTP_INP_INFO_WUNLOCK();
6259                                         return (0);
6260                                 }
6261                         }
6262                 }
6263         }
6264         /*-
6265          * Not found, ok to use the tag, add it to the time wait hash
6266          * as well this will prevent two sucessive cookies from getting
6267          * the same tag or two inits sent quickly on multi-processors.
6268          * We only keep the tag for the life of a cookie and when we
6269          * add this tag to the assoc hash we need to purge it from
6270          * the t-wait hash.
6271          */
6272         if (save_in_twait)
6273                 sctp_add_vtag_to_timewait(tag, TICKS_TO_SEC(inp->sctp_ep.def_cookie_life));
6274         SCTP_INP_INFO_WUNLOCK();
6275         return (1);
6276 }
6277
6278
6279 static sctp_assoc_t reneged_asoc_ids[256];
6280 static uint8_t reneged_at = 0;
6281
6282
6283 static void
6284 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
6285 {
6286         /*
6287          * We must hunt this association for MBUF's past the cumack (i.e.
6288          * out of order data that we can renege on).
6289          */
6290         struct sctp_association *asoc;
6291         struct sctp_tmit_chunk *chk, *nchk;
6292         uint32_t cumulative_tsn_p1, tsn;
6293         struct sctp_queued_to_read *ctl, *nctl;
6294         int cnt, strmat, gap;
6295
6296         /* We look for anything larger than the cum-ack + 1 */
6297
6298         SCTP_STAT_INCR(sctps_protocol_drain_calls);
6299         if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
6300                 return;
6301         }
6302         asoc = &stcb->asoc;
6303         if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6304                 /* none we can reneg on. */
6305                 return;
6306         }
6307         SCTP_STAT_INCR(sctps_protocol_drains_done);
6308         cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6309         cnt = 0;
6310         /* First look in the re-assembly queue */
6311         chk = TAILQ_FIRST(&asoc->reasmqueue);
6312         while (chk) {
6313                 /* Get the next one */
6314                 nchk = TAILQ_NEXT(chk, sctp_next);
6315                 if (compare_with_wrap(chk->rec.data.TSN_seq,
6316                     cumulative_tsn_p1, MAX_TSN)) {
6317                         /* Yep it is above cum-ack */
6318                         cnt++;
6319                         tsn = chk->rec.data.TSN_seq;
6320                         if (tsn >= asoc->mapping_array_base_tsn) {
6321                                 gap = tsn - asoc->mapping_array_base_tsn;
6322                         } else {
6323                                 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6324                                     tsn + 1;
6325                         }
6326                         asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6327                         sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6328                         SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6329                         TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6330                         if (chk->data) {
6331                                 sctp_m_freem(chk->data);
6332                                 chk->data = NULL;
6333                         }
6334                         sctp_free_a_chunk(stcb, chk);
6335                 }
6336                 chk = nchk;
6337         }
6338         /* Ok that was fun, now we will drain all the inbound streams? */
6339         for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6340                 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
6341                 while (ctl) {
6342                         nctl = TAILQ_NEXT(ctl, next);
6343                         if (compare_with_wrap(ctl->sinfo_tsn,
6344                             cumulative_tsn_p1, MAX_TSN)) {
6345                                 /* Yep it is above cum-ack */
6346                                 cnt++;
6347                                 tsn = ctl->sinfo_tsn;
6348                                 if (tsn >= asoc->mapping_array_base_tsn) {
6349                                         gap = tsn -
6350                                             asoc->mapping_array_base_tsn;
6351                                 } else {
6352                                         gap = (MAX_TSN -
6353                                             asoc->mapping_array_base_tsn) +
6354                                             tsn + 1;
6355                                 }
6356                                 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6357                                 sctp_ucount_decr(asoc->cnt_on_all_streams);
6358
6359                                 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
6360                                     gap);
6361                                 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
6362                                     ctl, next);
6363                                 if (ctl->data) {
6364                                         sctp_m_freem(ctl->data);
6365                                         ctl->data = NULL;
6366                                 }
6367                                 sctp_free_remote_addr(ctl->whoFrom);
6368                                 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
6369                                 SCTP_DECR_READQ_COUNT();
6370                         }
6371                         ctl = nctl;
6372                 }
6373         }
6374         /*
6375          * Question, should we go through the delivery queue? The only
6376          * reason things are on here is the app not reading OR a p-d-api up.
6377          * An attacker COULD send enough in to initiate the PD-API and then
6378          * send a bunch of stuff to other streams... these would wind up on
6379          * the delivery queue.. and then we would not get to them. But in
6380          * order to do this I then have to back-track and un-deliver
6381          * sequence numbers in streams.. el-yucko. I think for now we will
6382          * NOT look at the delivery queue and leave it to be something to
6383          * consider later. An alternative would be to abort the P-D-API with
6384          * a notification and then deliver the data.... Or another method
6385          * might be to keep track of how many times the situation occurs and
6386          * if we see a possible attack underway just abort the association.
6387          */
6388 #ifdef SCTP_DEBUG
6389         if (cnt) {
6390                 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6391         }
6392 #endif
6393         if (cnt) {
6394                 /*
6395                  * Now do we need to find a new
6396                  * asoc->highest_tsn_inside_map?
6397                  */
6398                 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
6399                         gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
6400                 } else {
6401                         gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6402                             asoc->highest_tsn_inside_map + 1;
6403                 }
6404                 if (gap >= (asoc->mapping_array_size << 3)) {
6405                         /*
6406                          * Something bad happened or cum-ack and high were
6407                          * behind the base, but if so earlier checks should
6408                          * have found NO data... wierd... we will start at
6409                          * end of mapping array.
6410                          */
6411                         SCTP_PRINTF("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
6412                             (int)gap,
6413                             (int)(asoc->mapping_array_size << 3),
6414                             (int)asoc->highest_tsn_inside_map);
6415                         gap = asoc->mapping_array_size << 3;
6416                 }
6417                 while (gap > 0) {
6418                         if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6419                                 /* found the new highest */
6420                                 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
6421                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6422                                         sctp_log_map(0, 8, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6423                                 }
6424                                 break;
6425                         }
6426                         gap--;
6427                 }
6428                 if (gap == 0) {
6429                         /* Nothing left in map */
6430                         memset(asoc->mapping_array, 0, asoc->mapping_array_size);
6431                         asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
6432                         asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
6433                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6434                                 sctp_log_map(0, 9, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6435                         }
6436                 }
6437                 asoc->last_revoke_count = cnt;
6438                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
6439                 /* sa_ignore NO_NULL_CHK */
6440                 sctp_send_sack(stcb);
6441                 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6442                 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
6443                 reneged_at++;
6444         }
6445         /*
6446          * Another issue, in un-setting the TSN's in the mapping array we
6447          * DID NOT adjust the higest_tsn marker.  This will cause one of two
6448          * things to occur. It may cause us to do extra work in checking for
6449          * our mapping array movement. More importantly it may cause us to
6450          * SACK every datagram. This may not be a bad thing though since we
6451          * will recover once we get our cum-ack above and all this stuff we
6452          * dumped recovered.
6453          */
6454 }
6455
6456 void
6457 sctp_drain()
6458 {
6459         /*
6460          * We must walk the PCB lists for ALL associations here. The system
6461          * is LOW on MBUF's and needs help. This is where reneging will
6462          * occur. We really hope this does NOT happen!
6463          */
6464         struct sctp_inpcb *inp;
6465         struct sctp_tcb *stcb;
6466
6467         SCTP_INP_INFO_RLOCK();
6468         LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
6469                 /* For each endpoint */
6470                 SCTP_INP_RLOCK(inp);
6471                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6472                         /* For each association */
6473                         SCTP_TCB_LOCK(stcb);
6474                         sctp_drain_mbufs(inp, stcb);
6475                         SCTP_TCB_UNLOCK(stcb);
6476                 }
6477                 SCTP_INP_RUNLOCK(inp);
6478         }
6479         SCTP_INP_INFO_RUNLOCK();
6480 }
6481
6482 /*
6483  * start a new iterator
6484  * iterates through all endpoints and associations based on the pcb_state
6485  * flags and asoc_state.  "af" (mandatory) is executed for all matching
6486  * assocs and "ef" (optional) is executed when the iterator completes.
6487  * "inpf" (optional) is executed for each new endpoint as it is being
6488  * iterated through. inpe (optional) is called when the inp completes
6489  * its way through all the stcbs.
6490  */
6491 int
6492 sctp_initiate_iterator(inp_func inpf,
6493     asoc_func af,
6494     inp_func inpe,
6495     uint32_t pcb_state,
6496     uint32_t pcb_features,
6497     uint32_t asoc_state,
6498     void *argp,
6499     uint32_t argi,
6500     end_func ef,
6501     struct sctp_inpcb *s_inp,
6502     uint8_t chunk_output_off)
6503 {
6504         struct sctp_iterator *it = NULL;
6505
6506         if (af == NULL) {
6507                 return (-1);
6508         }
6509         SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
6510             SCTP_M_ITER);
6511         if (it == NULL) {
6512                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
6513                 return (ENOMEM);
6514         }
6515         memset(it, 0, sizeof(*it));
6516         it->function_assoc = af;
6517         it->function_inp = inpf;
6518         if (inpf)
6519                 it->done_current_ep = 0;
6520         else
6521                 it->done_current_ep = 1;
6522         it->function_atend = ef;
6523         it->pointer = argp;
6524         it->val = argi;
6525         it->pcb_flags = pcb_state;
6526         it->pcb_features = pcb_features;
6527         it->asoc_state = asoc_state;
6528         it->function_inp_end = inpe;
6529         it->no_chunk_output = chunk_output_off;
6530         if (s_inp) {
6531                 it->inp = s_inp;
6532                 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
6533         } else {
6534                 SCTP_INP_INFO_RLOCK();
6535                 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
6536
6537                 SCTP_INP_INFO_RUNLOCK();
6538                 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
6539
6540         }
6541         SCTP_IPI_ITERATOR_WQ_LOCK();
6542         if (it->inp) {
6543                 SCTP_INP_INCR_REF(it->inp);
6544         }
6545         TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(iteratorhead), it, sctp_nxt_itr);
6546 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
6547         if (SCTP_BASE_INFO(iterator_running) == 0) {
6548                 sctp_wakeup_iterator();
6549         }
6550         SCTP_IPI_ITERATOR_WQ_UNLOCK();
6551 #else
6552         if (it->inp)
6553                 SCTP_INP_DECR_REF(it->inp);
6554         SCTP_IPI_ITERATOR_WQ_UNLOCK();
6555         /* Init the timer */
6556         SCTP_OS_TIMER_INIT(&it->tmr.timer);
6557         /* add to the list of all iterators */
6558         sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,
6559             NULL, NULL);
6560 #endif
6561         /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
6562         return (0);
6563 }