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