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