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