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