xref: /netbsd/sys/netinet/sctp_pcb.c (revision 86d79a4c)
1 /* $KAME: sctp_pcb.c,v 1.39 2005/06/16 18:29:25 jinmei Exp $ */
2 /* $NetBSD: sctp_pcb.c,v 1.26 2022/10/15 21:53:21 andvar Exp $ */
3 
4 /*
5  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Cisco Systems, Inc.
19  * 4. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sctp_pcb.c,v 1.26 2022/10/15 21:53:21 andvar Exp $");
37 
38 #ifdef _KERNEL_OPT
39 #include "opt_inet.h"
40 #include "opt_ipsec.h"
41 #include "opt_sctp.h"
42 #endif /* _KERNEL_OPT */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/proc.h>
53 #include <sys/kauth.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
56 #include <sys/callout.h>
57 
58 #include <machine/limits.h>
59 #include <machine/cpu.h>
60 
61 #include <net/if.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70 
71 #ifdef INET6
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
75 #include <netinet6/in6_pcb.h>
76 #endif /* INET6 */
77 
78 #ifdef IPSEC
79 #include <netipsec/ipsec.h>
80 #include <netipsec/key.h>
81 #endif /* IPSEC */
82 
83 #include <netinet/sctp_var.h>
84 #include <netinet/sctp_pcb.h>
85 #include <netinet/sctputil.h>
86 #include <netinet/sctp.h>
87 #include <netinet/sctp_header.h>
88 #include <netinet/sctp_asconf.h>
89 #include <netinet/sctp_output.h>
90 #include <netinet/sctp_timer.h>
91 
92 #ifndef SCTP_PCBHASHSIZE
93 /* default number of association hash buckets in each endpoint */
94 #define SCTP_PCBHASHSIZE 256
95 #endif
96 
97 #ifdef SCTP_DEBUG
98 u_int32_t sctp_debug_on = SCTP_DEBUG_ALL;
99 #endif /* SCTP_DEBUG */
100 
101 u_int32_t sctp_pegs[SCTP_NUMBER_OF_PEGS];
102 
103 int sctp_pcbtblsize = SCTP_PCBHASHSIZE;
104 
105 struct sctp_epinfo sctppcbinfo;
106 
107 /* FIX: we don't handle multiple link local scopes */
108 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
109 int
SCTP6_ARE_ADDR_EQUAL(const struct in6_addr * a,const struct in6_addr * b)110 SCTP6_ARE_ADDR_EQUAL(const struct in6_addr *a, const struct in6_addr *b)
111 {
112 	struct in6_addr tmp_a, tmp_b;
113 	/* use a copy of a and b */
114 	tmp_a = *a;
115 	tmp_b = *b;
116 	in6_clearscope(&tmp_a);
117 	in6_clearscope(&tmp_b);
118 	return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
119 }
120 
121 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
122 
123 #ifndef xyzzy
124 void sctp_validate_no_locks(void);
125 
126 void
SCTP_INP_RLOCK(struct sctp_inpcb * inp)127 SCTP_INP_RLOCK(struct sctp_inpcb *inp)
128 {
129 	struct sctp_tcb *stcb;
130 	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
131 		if (mtx_owned(&(stcb)->tcb_mtx))
132 			panic("I own TCB lock?");
133 	}
134         if (mtx_owned(&(inp)->inp_mtx))
135 		panic("INP Recursive Lock-R");
136         mtx_lock(&(inp)->inp_mtx);
137 }
138 
139 void
SCTP_INP_WLOCK(struct sctp_inpcb * inp)140 SCTP_INP_WLOCK(struct sctp_inpcb *inp)
141 {
142 	SCTP_INP_RLOCK(inp);
143 }
144 
145 void
SCTP_INP_INFO_RLOCK()146 SCTP_INP_INFO_RLOCK()
147 {
148 	struct sctp_inpcb *inp;
149 	struct sctp_tcb *stcb;
150 	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
151 		if (mtx_owned(&(inp)->inp_mtx))
152 			panic("info-lock and own inp lock?");
153 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
154 			if (mtx_owned(&(stcb)->tcb_mtx))
155 				panic("Info lock and own a tcb lock?");
156 		}
157 	}
158 	if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
159 		panic("INP INFO Recursive Lock-R");
160 	mtx_lock(&sctppcbinfo.ipi_ep_mtx);
161 }
162 
163 void
SCTP_INP_INFO_WLOCK()164 SCTP_INP_INFO_WLOCK()
165 {
166 	SCTP_INP_INFO_RLOCK();
167 }
168 
169 
sctp_validate_no_locks()170 void sctp_validate_no_locks()
171 {
172 	struct sctp_inpcb *inp;
173 	struct sctp_tcb *stcb;
174 
175 	if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
176 		panic("INP INFO lock is owned?");
177 
178 	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
179 		if (mtx_owned(&(inp)->inp_mtx))
180 			panic("You own an INP lock?");
181 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
182 			if (mtx_owned(&(stcb)->tcb_mtx))
183 				panic("You own a TCB lock?");
184 		}
185 	}
186 }
187 
188 #endif
189 #endif
190 
191 void
sctp_fill_pcbinfo(struct sctp_pcbinfo * spcb)192 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
193 {
194 	/* We really don't need
195 	 * to lock this, but I will
196 	 * just because it does not hurt.
197 	 */
198 	SCTP_INP_INFO_RLOCK();
199 	spcb->ep_count = sctppcbinfo.ipi_count_ep;
200 	spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
201 	spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
202 	spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
203 	spcb->chk_count = sctppcbinfo.ipi_count_chunk;
204 	spcb->sockq_count = sctppcbinfo.ipi_count_sockq;
205 	spcb->mbuf_track = sctppcbinfo.mbuf_track;
206 	SCTP_INP_INFO_RUNLOCK();
207 }
208 
209 
210 /*
211  * Notes on locks for FreeBSD 5 and up. All association
212  * lookups that have a definte ep, the INP structure is
213  * assumed to be locked for reading. If we need to go
214  * find the INP (usually when a **inp is passed) then
215  * we must lock the INFO structure first and if needed
216  * lock the INP too. Note that if we lock it we must
217  *
218  */
219 
220 
221 /*
222  * Given a endpoint, look and find in its association list any association
223  * with the "to" address given. This can be a "from" address, too, for
224  * inbound packets. For outbound packets it is a true "to" address.
225  */
226 static struct sctp_tcb *
sctp_tcb_special_locate(struct sctp_inpcb ** inp_p,struct sockaddr * from,struct sockaddr * to,struct sctp_nets ** netp)227 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
228 			struct sockaddr *to, struct sctp_nets **netp)
229 {
230 	/**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
231 
232 	/*
233 	 * Note for this module care must be taken when observing what to is
234 	 * for. In most of the rest of the code the TO field represents my
235 	 * peer and the FROM field represents my address. For this module it
236 	 * is reversed of that.
237 	 */
238 	/*
239 	 * If we support the TCP model, then we must now dig through to
240 	 * see if we can find our endpoint in the list of tcp ep's.
241 	 */
242 	uint16_t lport, rport;
243 	struct sctppcbhead *ephead;
244 	struct sctp_inpcb *inp;
245 	struct sctp_laddr *laddr;
246 	struct sctp_tcb *stcb;
247 	struct sctp_nets *net;
248 
249 	if ((to == NULL) || (from == NULL)) {
250 		return (NULL);
251 	}
252 
253 	if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
254 		lport = ((struct sockaddr_in *)to)->sin_port;
255 		rport = ((struct sockaddr_in *)from)->sin_port;
256 	} else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
257 		lport = ((struct sockaddr_in6 *)to)->sin6_port;
258 		rport = ((struct sockaddr_in6 *)from)->sin6_port;
259 	} else {
260 		return NULL;
261 	}
262 	ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
263 						     (lport + rport), sctppcbinfo.hashtcpmark)];
264 	/*
265 	 * Ok now for each of the guys in this bucket we must look
266 	 * and see:
267 	 *  - Does the remote port match.
268 	 *  - Does there single association's addresses match this
269 	 *    address (to).
270 	 * If so we update p_ep to point to this ep and return the
271 	 * tcb from it.
272 	 */
273 	LIST_FOREACH(inp, ephead, sctp_hash) {
274 		if (lport != inp->sctp_lport) {
275 			continue;
276 		}
277 		SCTP_INP_RLOCK(inp);
278 		/* check to see if the ep has one of the addresses */
279 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
280 			/* We are NOT bound all, so look further */
281 			int match = 0;
282 
283 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
284 				if (laddr->ifa == NULL) {
285 #ifdef SCTP_DEBUG
286 					if (sctp_debug_on & SCTP_DEBUG_PCB1) {
287 						printf("An ounce of prevention is worth a pound of cure\n");
288 					}
289 #endif
290 					continue;
291 				}
292 				if (laddr->ifa->ifa_addr == NULL) {
293 #ifdef SCTP_DEBUG
294 					if (sctp_debug_on & SCTP_DEBUG_PCB1) {
295 						printf("ifa with a NULL address\n");
296 					}
297 #endif
298 					continue;
299 				}
300 				if (laddr->ifa->ifa_addr->sa_family ==
301 				    to->sa_family) {
302 					/* see if it matches */
303 					struct sockaddr_in *intf_addr, *sin;
304 					intf_addr = (struct sockaddr_in *)
305 						laddr->ifa->ifa_addr;
306 					sin = (struct sockaddr_in *)to;
307 					if (from->sa_family == AF_INET) {
308 						if (sin->sin_addr.s_addr ==
309 						    intf_addr->sin_addr.s_addr) {
310 							match = 1;
311 							SCTP_INP_RUNLOCK(inp);
312 							break;
313 						}
314 					} else {
315 						struct sockaddr_in6 *intf_addr6;
316 						struct sockaddr_in6 *sin6;
317 						sin6 = (struct sockaddr_in6 *)
318 							to;
319 						intf_addr6 = (struct sockaddr_in6 *)
320 							laddr->ifa->ifa_addr;
321 
322 						if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
323 									 &intf_addr6->sin6_addr)) {
324 							match = 1;
325 							SCTP_INP_RUNLOCK(inp);
326 							break;
327 						}
328 					}
329 				}
330 			}
331 			if (match == 0) {
332 				/* This endpoint does not have this address */
333 				SCTP_INP_RUNLOCK(inp);
334 				continue;
335 			}
336 		}
337 		/*
338 		 * Ok if we hit here the ep has the address, does it hold the
339 		 * tcb?
340 		 */
341 
342 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
343 		if (stcb == NULL) {
344 			SCTP_INP_RUNLOCK(inp);
345 			continue;
346 		}
347 		SCTP_TCB_LOCK(stcb);
348 		if (stcb->rport != rport) {
349 			/* remote port does not match. */
350 			SCTP_TCB_UNLOCK(stcb);
351 			SCTP_INP_RUNLOCK(inp);
352 			continue;
353 		}
354 		/* Does this TCB have a matching address? */
355 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
356 			if (sctp_cmpaddr(from, rtcache_getdst(&net->ro))) {
357 				/* found it */
358 				if (netp != NULL) {
359 					*netp = net;
360 				}
361 				/* Update the endpoint pointer */
362 				*inp_p = inp;
363 				SCTP_INP_RUNLOCK(inp);
364 				return (stcb);
365 			}
366 		}
367 		SCTP_TCB_UNLOCK(stcb);
368 
369 		SCTP_INP_RUNLOCK(inp);
370 	}
371 	return (NULL);
372 }
373 
374 struct sctp_tcb *
sctp_findassociation_ep_asconf(struct mbuf * m,int iphlen,int offset,struct sctphdr * sh,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp)375 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
376     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
377 {
378 	struct sctp_tcb *stcb;
379 	struct sockaddr_in *sin;
380 	struct sockaddr_in6 *sin6;
381 	struct sockaddr_storage local_store, remote_store;
382 	struct ip *iph;
383 	struct sctp_paramhdr parm_buf, *phdr;
384 	int ptype;
385 
386 	memset(&local_store, 0, sizeof(local_store));
387 	memset(&remote_store, 0, sizeof(remote_store));
388 
389 	/* First get the destination address setup too. */
390 	iph = mtod(m, struct ip *);
391 	if (iph->ip_v == IPVERSION) {
392 		/* its IPv4 */
393 		sin = (struct sockaddr_in *)&local_store;
394 		sin->sin_family = AF_INET;
395 		sin->sin_len = sizeof(*sin);
396 		sin->sin_port = sh->dest_port;
397 		sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
398 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
399 		/* its IPv6 */
400 		struct ip6_hdr *ip6;
401 		ip6 = mtod(m, struct ip6_hdr *);
402 		sin6 = (struct sockaddr_in6 *)&local_store;
403 		sin6->sin6_family = AF_INET6;
404 		sin6->sin6_len = sizeof(*sin6);
405 		sin6->sin6_port = sh->dest_port;
406 		sin6->sin6_addr = ip6->ip6_dst;
407 	} else {
408 		return NULL;
409 	}
410 
411 	phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
412 	    &parm_buf, sizeof(struct sctp_paramhdr));
413 	if (phdr == NULL) {
414 #ifdef SCTP_DEBUG
415 		if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
416 			printf("sctp_process_control: failed to get asconf lookup addr\n");
417 		}
418 #endif /* SCTP_DEBUG */
419 		return NULL;
420 	}
421 	ptype = (int)((u_int)ntohs(phdr->param_type));
422 	/* get the correlation address */
423 	if (ptype == SCTP_IPV6_ADDRESS) {
424 		/* ipv6 address param */
425 		struct sctp_ipv6addr_param *p6, p6_buf;
426 		if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
427 			return NULL;
428 		}
429 
430 		p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
431 		    offset + sizeof(struct sctp_asconf_chunk),
432 		    &p6_buf.ph, sizeof(*p6));
433 		if (p6 == NULL) {
434 #ifdef SCTP_DEBUG
435 			if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
436 				printf("sctp_process_control: failed to get asconf v6 lookup addr\n");
437 			}
438 #endif /* SCTP_DEBUG */
439 			return (NULL);
440 		}
441 		sin6 = (struct sockaddr_in6 *)&remote_store;
442 		sin6->sin6_family = AF_INET6;
443 		sin6->sin6_len = sizeof(*sin6);
444 		sin6->sin6_port = sh->src_port;
445 		memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
446 	} else if (ptype == SCTP_IPV4_ADDRESS) {
447 		/* ipv4 address param */
448 		struct sctp_ipv4addr_param *p4, p4_buf;
449 		if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
450 			return NULL;
451 		}
452 
453 		p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
454 		    offset + sizeof(struct sctp_asconf_chunk),
455 		    &p4_buf.ph, sizeof(*p4));
456 		if (p4 == NULL) {
457 #ifdef SCTP_DEBUG
458 			if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
459 				printf("sctp_process_control: failed to get asconf v4 lookup addr\n");
460 			}
461 #endif /* SCTP_DEBUG */
462 			return (NULL);
463 		}
464 		sin = (struct sockaddr_in *)&remote_store;
465 		sin->sin_family = AF_INET;
466 		sin->sin_len = sizeof(*sin);
467 		sin->sin_port = sh->src_port;
468 		memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
469 	} else {
470 		/* invalid address param type */
471 		return NULL;
472 	}
473 
474 	stcb = sctp_findassociation_ep_addr(inp_p,
475 	    (struct sockaddr *)&remote_store, netp,
476 	    (struct sockaddr *)&local_store, NULL);
477 	return (stcb);
478 }
479 
480 struct sctp_tcb *
sctp_findassociation_ep_addr(struct sctp_inpcb ** inp_p,struct sockaddr * remote,struct sctp_nets ** netp,struct sockaddr * local,struct sctp_tcb * locked_tcb)481 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
482     struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
483 {
484 	struct sctpasochead *head;
485 	struct sctp_inpcb *inp;
486 	struct sctp_tcb *stcb;
487 	struct sctp_nets *net;
488 	uint16_t rport;
489 
490 	inp = *inp_p;
491 	if (remote->sa_family == AF_INET) {
492 		rport = (((struct sockaddr_in *)remote)->sin_port);
493 	} else if (remote->sa_family == AF_INET6) {
494 		rport = (((struct sockaddr_in6 *)remote)->sin6_port);
495 	} else {
496 		return (NULL);
497 	}
498 	if (locked_tcb) {
499 		/* UN-lock so we can do proper locking here
500 		 * this occurs when called from load_addresses_from_init.
501 		 */
502 		SCTP_TCB_UNLOCK(locked_tcb);
503 	}
504 	SCTP_INP_INFO_RLOCK();
505 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
506 		/*
507 		 * Now either this guy is our listner or it's the connector.
508 		 * If it is the one that issued the connect, then it's only
509 		 * chance is to be the first TCB in the list. If it is the
510 		 * acceptor, then do the special_lookup to hash and find the
511 		 * real inp.
512 		 */
513 		if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
514 			/* to is peer addr, from is my addr */
515 			stcb = sctp_tcb_special_locate(inp_p, remote, local,
516 						       netp);
517 			if ((stcb != NULL) && (locked_tcb == NULL)){
518 				/* we have a locked tcb, lower refcount */
519 				SCTP_INP_WLOCK(inp);
520 				SCTP_INP_DECR_REF(inp);
521 				SCTP_INP_WUNLOCK(inp);
522 			}
523 			if (locked_tcb != NULL) {
524 				SCTP_INP_RLOCK(locked_tcb->sctp_ep);
525 				SCTP_TCB_LOCK(locked_tcb);
526 				SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
527 				if (stcb != NULL) {
528 					SCTP_TCB_UNLOCK(stcb);
529 				}
530 			}
531 			SCTP_INP_INFO_RUNLOCK();
532 			return (stcb);
533 		} else {
534 			SCTP_INP_WLOCK(inp);
535 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
536 			if (stcb == NULL) {
537 				goto null_return;
538 			}
539 			SCTP_TCB_LOCK(stcb);
540 			if (stcb->rport != rport) {
541 				/* remote port does not match. */
542 				SCTP_TCB_UNLOCK(stcb);
543 				goto null_return;
544 			}
545 			/* now look at the list of remote addresses */
546 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
547 				if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
548 					/* found it */
549 					if (netp != NULL) {
550 						*netp = net;
551 					}
552 					if (locked_tcb == NULL) {
553 						SCTP_INP_DECR_REF(inp);
554 					}
555 					SCTP_INP_WUNLOCK(inp);
556 					SCTP_INP_INFO_RUNLOCK();
557 					return (stcb);
558 				}
559 			}
560 			SCTP_TCB_UNLOCK(stcb);
561 		}
562 	} else {
563 		SCTP_INP_WLOCK(inp);
564 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
565 							       inp->sctp_hashmark)];
566 		if (head == NULL) {
567 			goto null_return;
568 		}
569 		LIST_FOREACH(stcb, head, sctp_tcbhash) {
570 			if (stcb->rport != rport) {
571 				/* remote port does not match */
572 				continue;
573 			}
574 			/* now look at the list of remote addresses */
575 			SCTP_TCB_LOCK(stcb);
576 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
577 				if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
578 					/* found it */
579 					if (netp != NULL) {
580 						*netp = net;
581 					}
582 					if (locked_tcb == NULL) {
583 						SCTP_INP_DECR_REF(inp);
584 					}
585 					SCTP_INP_WUNLOCK(inp);
586 					SCTP_INP_INFO_RUNLOCK();
587 					return (stcb);
588 				}
589 			}
590 			SCTP_TCB_UNLOCK(stcb);
591 		}
592 	}
593  null_return:
594 	/* clean up for returning null */
595 	if (locked_tcb){
596 		if (locked_tcb->sctp_ep != inp) {
597 			SCTP_INP_RLOCK(locked_tcb->sctp_ep);
598 			SCTP_TCB_LOCK(locked_tcb);
599 			SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
600 		} else {
601 			SCTP_TCB_LOCK(locked_tcb);
602 		}
603 	}
604 	SCTP_INP_WUNLOCK(inp);
605 	SCTP_INP_INFO_RUNLOCK();
606 	/* not found */
607 	return (NULL);
608 }
609 
610 /*
611  * Find an association for a specific endpoint using the association id
612  * given out in the COMM_UP notification
613  */
614 struct sctp_tcb *
sctp_findassociation_ep_asocid(struct sctp_inpcb * inp,vaddr_t asoc_id)615 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, vaddr_t asoc_id)
616 {
617 	/*
618 	 * Use my the assoc_id to find a endpoint
619 	 */
620 	struct sctpasochead *head;
621 	struct sctp_tcb *stcb;
622 	u_int32_t vtag;
623 
624 	if (asoc_id == 0 || inp == NULL) {
625 		return (NULL);
626 	}
627 	SCTP_INP_INFO_RLOCK();
628 	vtag = (u_int32_t)asoc_id;
629 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
630 	    sctppcbinfo.hashasocmark)];
631 	if (head == NULL) {
632 		/* invalid vtag */
633 		SCTP_INP_INFO_RUNLOCK();
634 		return (NULL);
635 	}
636 	LIST_FOREACH(stcb, head, sctp_asocs) {
637 		SCTP_INP_RLOCK(stcb->sctp_ep);
638 		SCTP_TCB_LOCK(stcb);
639 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
640 		if (stcb->asoc.my_vtag == vtag) {
641 			/* candidate */
642 			if (inp != stcb->sctp_ep) {
643 				/* some other guy has the
644 				 * same vtag active (vtag collision).
645 				 */
646 				sctp_pegs[SCTP_VTAG_BOGUS]++;
647 				SCTP_TCB_UNLOCK(stcb);
648 				continue;
649 			}
650 			sctp_pegs[SCTP_VTAG_EXPR]++;
651 			SCTP_INP_INFO_RUNLOCK();
652 			return (stcb);
653 		}
654 		SCTP_TCB_UNLOCK(stcb);
655 	}
656 	SCTP_INP_INFO_RUNLOCK();
657 	return (NULL);
658 }
659 
660 static struct sctp_inpcb *
sctp_endpoint_probe(struct sockaddr * nam,struct sctppcbhead * head,uint16_t lport)661 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
662 		    uint16_t lport)
663 {
664 	struct sctp_inpcb *inp;
665 	struct sockaddr_in *sin;
666 	struct sockaddr_in6 *sin6;
667 	struct sctp_laddr *laddr;
668 
669 	/* Endpoing probe expects
670 	 * that the INP_INFO is locked.
671 	 */
672 	if (nam->sa_family == AF_INET) {
673 		sin = (struct sockaddr_in *)nam;
674 		sin6 = NULL;
675 	} else if (nam->sa_family == AF_INET6) {
676 		sin6 = (struct sockaddr_in6 *)nam;
677 		sin = NULL;
678 	} else {
679 		/* unsupported family */
680 		return (NULL);
681 	}
682 	if (head == NULL)
683 		return (NULL);
684 
685 	LIST_FOREACH(inp, head, sctp_hash) {
686 		SCTP_INP_RLOCK(inp);
687 
688 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
689 		    (inp->sctp_lport == lport)) {
690 			/* got it */
691 			if ((nam->sa_family == AF_INET) &&
692 			    (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
693 			    (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
694 				) {
695 				/* IPv4 on a IPv6 socket with ONLY IPv6 set */
696 				SCTP_INP_RUNLOCK(inp);
697 				continue;
698 			}
699 			/* A V6 address and the endpoint is NOT bound V6 */
700 			if (nam->sa_family == AF_INET6 &&
701 			   (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
702 				SCTP_INP_RUNLOCK(inp);
703 				continue;
704 			}
705 			SCTP_INP_RUNLOCK(inp);
706 			return (inp);
707 		}
708 		SCTP_INP_RUNLOCK(inp);
709 	}
710 
711 	if ((nam->sa_family == AF_INET) &&
712 	    (sin->sin_addr.s_addr == INADDR_ANY)) {
713 		/* Can't hunt for one that has no address specified */
714 		return (NULL);
715 	} else if ((nam->sa_family == AF_INET6) &&
716 		   (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
717 		/* Can't hunt for one that has no address specified */
718 		return (NULL);
719 	}
720 	/*
721 	 * ok, not bound to all so see if we can find a EP bound to this
722 	 * address.
723 	 */
724 #ifdef SCTP_DEBUG
725 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
726 		printf("Ok, there is NO bound-all available for port:%x\n", ntohs(lport));
727 	}
728 #endif
729 	LIST_FOREACH(inp, head, sctp_hash) {
730 		SCTP_INP_RLOCK(inp);
731 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
732 			SCTP_INP_RUNLOCK(inp);
733 			continue;
734 		}
735 		/*
736 		 * Ok this could be a likely candidate, look at all of
737 		 * its addresses
738 		 */
739 		if (inp->sctp_lport != lport) {
740 			SCTP_INP_RUNLOCK(inp);
741 			continue;
742 		}
743 #ifdef SCTP_DEBUG
744 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
745 			printf("Ok, found matching local port\n");
746 		}
747 #endif
748 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
749 			if (laddr->ifa == NULL) {
750 #ifdef SCTP_DEBUG
751 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
752 					printf("An ounce of prevention is worth a pound of cure\n");
753 				}
754 #endif
755 				continue;
756 			}
757 #ifdef SCTP_DEBUG
758 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
759 				printf("Ok laddr->ifa:%p is possible, ",
760 				    laddr->ifa);
761 			}
762 #endif
763 			if (laddr->ifa->ifa_addr == NULL) {
764 #ifdef SCTP_DEBUG
765 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
766 					printf("Huh IFA as an ifa_addr=NULL, ");
767 				}
768 #endif
769 				continue;
770 			}
771 #ifdef SCTP_DEBUG
772 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
773 				printf("Ok laddr->ifa:%p is possible, ",
774 				    laddr->ifa->ifa_addr);
775 				sctp_print_address(laddr->ifa->ifa_addr);
776 				printf("looking for ");
777 				sctp_print_address(nam);
778 			}
779 #endif
780 			if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
781 				/* possible, see if it matches */
782 				struct sockaddr_in *intf_addr;
783 				intf_addr = (struct sockaddr_in *)
784 				    laddr->ifa->ifa_addr;
785 				if (nam->sa_family == AF_INET) {
786 					if (sin->sin_addr.s_addr ==
787 					    intf_addr->sin_addr.s_addr) {
788 #ifdef SCTP_DEBUG
789 						if (sctp_debug_on & SCTP_DEBUG_PCB1) {
790 							printf("YES, return ep:%p\n", inp);
791 						}
792 #endif
793 						SCTP_INP_RUNLOCK(inp);
794 						return (inp);
795 					}
796 				} else if (nam->sa_family == AF_INET6) {
797 					struct sockaddr_in6 *intf_addr6;
798 					intf_addr6 = (struct sockaddr_in6 *)
799 					    laddr->ifa->ifa_addr;
800 					if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
801 				 	    &intf_addr6->sin6_addr)) {
802 #ifdef SCTP_DEBUG
803 						if (sctp_debug_on & SCTP_DEBUG_PCB1) {
804 							printf("YES, return ep:%p\n", inp);
805 						}
806 #endif
807 						SCTP_INP_RUNLOCK(inp);
808 						return (inp);
809 					}
810 				}
811 			}
812 			SCTP_INP_RUNLOCK(inp);
813 		}
814 	}
815 #ifdef SCTP_DEBUG
816 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
817 		printf("NO, Falls out to NULL\n");
818 	}
819 #endif
820 	return (NULL);
821 }
822 
823 
824 struct sctp_inpcb *
sctp_pcb_findep(struct sockaddr * nam,int find_tcp_pool,int have_lock)825 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
826 {
827 	/*
828 	 * First we check the hash table to see if someone has this port
829 	 * bound with just the port.
830 	 */
831 	struct sctp_inpcb *inp;
832 	struct sctppcbhead *head;
833 	int lport;
834 #ifdef SCTP_DEBUG
835 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
836 		printf("Looking for endpoint %d :",
837 		       ntohs(((struct sockaddr_in *)nam)->sin_port));
838 		sctp_print_address(nam);
839 	}
840 #endif
841 	if (nam->sa_family == AF_INET) {
842 		lport = ((struct sockaddr_in *)nam)->sin_port;
843 	} else if (nam->sa_family == AF_INET6) {
844 		lport = ((struct sockaddr_in6 *)nam)->sin6_port;
845 	} else {
846 		/* unsupported family */
847 		return (NULL);
848 	}
849 	/*
850 	 * I could cheat here and just cast to one of the types but we will
851 	 * do it right. It also provides the check against an Unsupported
852 	 * type too.
853 	 */
854 	/* Find the head of the ALLADDR chain */
855 	if (have_lock == 0) {
856 		SCTP_INP_INFO_RLOCK();
857 	}
858 	head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
859 							     sctppcbinfo.hashmark)];
860 #ifdef SCTP_DEBUG
861 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
862 		printf("Main hash to lookup at head:%p\n", head);
863 	}
864 #endif
865  	inp = sctp_endpoint_probe(nam, head, lport);
866 
867 	/*
868 	 * If the TCP model exists it could be that the main listening
869 	 * endpoint is gone but there exists a connected socket for this
870 	 * guy yet. If so we can return the first one that we find. This
871 	 * may NOT be the correct one but the sctp_findassociation_ep_addr
872 	 * has further code to look at all TCP models.
873 	 */
874 	if (inp == NULL && find_tcp_pool) {
875 		unsigned int i;
876 #ifdef SCTP_DEBUG
877 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
878 			printf("EP was NULL and TCP model is supported\n");
879 		}
880 #endif
881 		for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
882 			/*
883 			 * This is real gross, but we do NOT have a remote
884 			 * port at this point depending on who is calling. We
885 			 * must therefore look for ANY one that matches our
886 			 * local port :/
887 			 */
888 			head = &sctppcbinfo.sctp_tcpephash[i];
889 			if (LIST_FIRST(head)) {
890 				inp = sctp_endpoint_probe(nam, head, lport);
891 				if (inp) {
892 					/* Found one */
893 					break;
894 				}
895 			}
896 		}
897 	}
898 #ifdef SCTP_DEBUG
899 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
900 		printf("EP to return is %p\n", inp);
901 	}
902 #endif
903 	if (have_lock == 0) {
904 		if (inp) {
905 			SCTP_INP_WLOCK(inp);
906 			SCTP_INP_INCR_REF(inp);
907 			SCTP_INP_WUNLOCK(inp);
908 		}
909 		SCTP_INP_INFO_RUNLOCK();
910 	} else {
911 		if (inp) {
912 			SCTP_INP_WLOCK(inp);
913 			SCTP_INP_INCR_REF(inp);
914 			SCTP_INP_WUNLOCK(inp);
915 		}
916 	}
917 	return (inp);
918 }
919 
920 /*
921  * Find an association for an endpoint with the pointer to whom you want
922  * to send to and the endpoint pointer. The address can be IPv4 or IPv6.
923  * We may need to change the *to to some other struct like a mbuf...
924  */
925 struct sctp_tcb *
sctp_findassociation_addr_sa(struct sockaddr * to,struct sockaddr * from,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,int find_tcp_pool)926 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
927     struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
928 {
929 	struct sctp_inpcb *inp;
930 	struct sctp_tcb *retval;
931 
932 	SCTP_INP_INFO_RLOCK();
933 	if (find_tcp_pool) {
934 		if (inp_p != NULL) {
935 			retval = sctp_tcb_special_locate(inp_p, from, to, netp);
936 		} else {
937 			retval = sctp_tcb_special_locate(&inp, from, to, netp);
938 		}
939 		if (retval != NULL) {
940 			SCTP_INP_INFO_RUNLOCK();
941 			return (retval);
942 		}
943 	}
944 	inp = sctp_pcb_findep(to, 0, 1);
945 	if (inp_p != NULL) {
946 		*inp_p = inp;
947 	}
948 	SCTP_INP_INFO_RUNLOCK();
949 
950 	if (inp == NULL) {
951 		return (NULL);
952 	}
953 
954 	/*
955 	 * ok, we have an endpoint, now lets find the assoc for it (if any)
956 	 * we now place the source address or from in the to of the find
957 	 * endpoint call. Since in reality this chain is used from the
958 	 * inbound packet side.
959 	 */
960 	if (inp_p != NULL) {
961 		return (sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL));
962 	} else {
963 		return (sctp_findassociation_ep_addr(&inp, from, netp, to, NULL));
964 	}
965 }
966 
967 
968 /*
969  * This routine will grub through the mbuf that is a INIT or INIT-ACK and
970  * find all addresses that the sender has specified in any address list.
971  * Each address will be used to lookup the TCB and see if one exits.
972  */
973 static struct sctp_tcb *
sctp_findassociation_special_addr(struct mbuf * m,int iphlen,int offset,struct sctphdr * sh,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,struct sockaddr * dest)974 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
975     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
976     struct sockaddr *dest)
977 {
978 	struct sockaddr_in sin4;
979 	struct sockaddr_in6 sin6;
980 	struct sctp_paramhdr *phdr, parm_buf;
981 	struct sctp_tcb *retval;
982 	u_int32_t ptype, plen;
983 
984 	memset(&sin4, 0, sizeof(sin4));
985 	memset(&sin6, 0, sizeof(sin6));
986 	sin4.sin_len = sizeof(sin4);
987 	sin4.sin_family = AF_INET;
988 	sin4.sin_port = sh->src_port;
989 	sin6.sin6_len = sizeof(sin6);
990 	sin6.sin6_family = AF_INET6;
991 	sin6.sin6_port = sh->src_port;
992 
993 	retval = NULL;
994 	offset += sizeof(struct sctp_init_chunk);
995 
996 	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
997 	while (phdr != NULL) {
998 		/* now we must see if we want the parameter */
999 		ptype = ntohs(phdr->param_type);
1000 		plen = ntohs(phdr->param_length);
1001 		if (plen == 0) {
1002 #ifdef SCTP_DEBUG
1003 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1004 				printf("sctp_findassociation_special_addr: Impossible length in parameter\n");
1005 			}
1006 #endif /* SCTP_DEBUG */
1007 			break;
1008 		}
1009 		if (ptype == SCTP_IPV4_ADDRESS &&
1010 		    plen == sizeof(struct sctp_ipv4addr_param)) {
1011 			/* Get the rest of the address */
1012 			struct sctp_ipv4addr_param ip4_parm, *p4;
1013 
1014 			phdr = sctp_get_next_param(m, offset,
1015 			    (struct sctp_paramhdr *)&ip4_parm, plen);
1016 			if (phdr == NULL) {
1017 				return (NULL);
1018 			}
1019 			p4 = (struct sctp_ipv4addr_param *)phdr;
1020 			memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1021 			/* look it up */
1022 			retval = sctp_findassociation_ep_addr(inp_p,
1023 			    (struct sockaddr *)&sin4, netp, dest, NULL);
1024 			if (retval != NULL) {
1025 				return (retval);
1026 			}
1027 		} else if (ptype == SCTP_IPV6_ADDRESS &&
1028 		    plen == sizeof(struct sctp_ipv6addr_param)) {
1029 			/* Get the rest of the address */
1030 			struct sctp_ipv6addr_param ip6_parm, *p6;
1031 
1032 			phdr = sctp_get_next_param(m, offset,
1033 			    (struct sctp_paramhdr *)&ip6_parm, plen);
1034 			if (phdr == NULL) {
1035 				return (NULL);
1036 			}
1037 			p6 = (struct sctp_ipv6addr_param *)phdr;
1038 			memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1039 			/* look it up */
1040 			retval = sctp_findassociation_ep_addr(inp_p,
1041 			    (struct sockaddr *)&sin6, netp, dest, NULL);
1042 			if (retval != NULL) {
1043 				return (retval);
1044 			}
1045 		}
1046 		offset += SCTP_SIZE32(plen);
1047 		phdr = sctp_get_next_param(m, offset, &parm_buf,
1048 		    sizeof(parm_buf));
1049 	}
1050 	return (NULL);
1051 }
1052 
1053 static struct sctp_tcb *
sctp_findassoc_by_vtag(struct sockaddr * from,uint32_t vtag,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,uint16_t rport,uint16_t lport)1054 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1055     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1056     uint16_t lport)
1057 {
1058 	/*
1059 	 * Use my vtag to hash. If we find it we then verify the source addr
1060 	 * is in the assoc. If all goes well we save a bit on rec of a packet.
1061 	 */
1062 	struct sctpasochead *head;
1063 	struct sctp_nets *net;
1064 	struct sctp_tcb *stcb;
1065 
1066 	SCTP_INP_INFO_RLOCK();
1067 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1068 	    sctppcbinfo.hashasocmark)];
1069 	if (head == NULL) {
1070 		/* invalid vtag */
1071 		SCTP_INP_INFO_RUNLOCK();
1072 		return (NULL);
1073 	}
1074 	LIST_FOREACH(stcb, head, sctp_asocs) {
1075 		SCTP_INP_RLOCK(stcb->sctp_ep);
1076 		SCTP_TCB_LOCK(stcb);
1077 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
1078 		if (stcb->asoc.my_vtag == vtag) {
1079 			/* candidate */
1080 			if (stcb->rport != rport) {
1081 				/*
1082 				 * we could remove this if vtags are unique
1083 				 * across the system.
1084 				 */
1085 				SCTP_TCB_UNLOCK(stcb);
1086 				continue;
1087 			}
1088 			if (stcb->sctp_ep->sctp_lport != lport) {
1089 				/*
1090 				 * we could remove this if vtags are unique
1091 				 * across the system.
1092 				 */
1093 				SCTP_TCB_UNLOCK(stcb);
1094 				continue;
1095 			}
1096 			net = sctp_findnet(stcb, from);
1097 			if (net) {
1098 				/* yep its him. */
1099 				*netp = net;
1100 				sctp_pegs[SCTP_VTAG_EXPR]++;
1101 				*inp_p = stcb->sctp_ep;
1102 				SCTP_INP_INFO_RUNLOCK();
1103 				return (stcb);
1104 			} else {
1105  				/* not him, this should only
1106  				 * happen in rare cases so
1107  				 * I peg it.
1108   				 */
1109  				sctp_pegs[SCTP_VTAG_BOGUS]++;
1110 			}
1111 		}
1112 		SCTP_TCB_UNLOCK(stcb);
1113 	}
1114 	SCTP_INP_INFO_RUNLOCK();
1115 	return (NULL);
1116 }
1117 
1118 /*
1119  * Find an association with the pointer to the inbound IP packet. This
1120  * can be a IPv4 or IPv6 packet.
1121  */
1122 struct sctp_tcb *
sctp_findassociation_addr(struct mbuf * m,int iphlen,int offset,struct sctphdr * sh,struct sctp_chunkhdr * ch,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp)1123 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1124     struct sctphdr *sh, struct sctp_chunkhdr *ch,
1125     struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1126 {
1127 	int find_tcp_pool;
1128 	struct ip *iph;
1129 	struct sctp_tcb *retval;
1130 	struct sockaddr_storage to_store, from_store;
1131 	struct sockaddr *to = (struct sockaddr *)&to_store;
1132 	struct sockaddr *from = (struct sockaddr *)&from_store;
1133 	struct sctp_inpcb *inp;
1134 
1135 
1136 	iph = mtod(m, struct ip *);
1137 	if (iph->ip_v == IPVERSION) {
1138 		/* its IPv4 */
1139 		struct sockaddr_in *to4, *from4;
1140 
1141 		to4 = (struct sockaddr_in *)&to_store;
1142 		from4 = (struct sockaddr_in *)&from_store;
1143 		memset(to4, 0, sizeof(*to4));
1144 		memset(from4, 0, sizeof(*from4));
1145 		from4->sin_family = to4->sin_family = AF_INET;
1146 		from4->sin_len = to4->sin_len = sizeof(struct sockaddr_in);
1147 		from4->sin_addr.s_addr  = iph->ip_src.s_addr;
1148 		to4->sin_addr.s_addr = iph->ip_dst.s_addr ;
1149 		from4->sin_port = sh->src_port;
1150 		to4->sin_port = sh->dest_port;
1151 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1152 		/* its IPv6 */
1153 		struct ip6_hdr *ip6;
1154 		struct sockaddr_in6 *to6, *from6;
1155 
1156 		ip6 = mtod(m, struct ip6_hdr *);
1157 		to6 = (struct sockaddr_in6 *)&to_store;
1158 		from6 = (struct sockaddr_in6 *)&from_store;
1159 		memset(to6, 0, sizeof(*to6));
1160 		memset(from6, 0, sizeof(*from6));
1161 		from6->sin6_family = to6->sin6_family = AF_INET6;
1162 		from6->sin6_len = to6->sin6_len = sizeof(struct sockaddr_in6);
1163 		from6->sin6_addr = ip6->ip6_src;
1164 		to6->sin6_addr = ip6->ip6_dst;
1165 		from6->sin6_port = sh->src_port;
1166 		to6->sin6_port = sh->dest_port;
1167 		/* Get the scopes in properly to the sin6 addr's */
1168 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
1169 		/* We probably don't need this operation (jinmei@kame) */
1170 		(void)in6_recoverscope(to6, &to6->sin6_addr, NULL);
1171 		(void)in6_embedscope(&to6->sin6_addr, to6, NULL, NULL);
1172 
1173 		(void)in6_recoverscope(from6, &from6->sin6_addr, NULL);
1174 		(void)in6_embedscope(&from6->sin6_addr, from6, NULL, NULL);
1175 #endif
1176 	} else {
1177 		/* Currently not supported. */
1178 		return (NULL);
1179 	}
1180 #ifdef SCTP_DEBUG
1181 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1182 		printf("Looking for port %d address :",
1183 		       ntohs(((struct sockaddr_in *)to)->sin_port));
1184 		sctp_print_address(to);
1185 		printf("From for port %d address :",
1186 		       ntohs(((struct sockaddr_in *)from)->sin_port));
1187 		sctp_print_address(from);
1188 	}
1189 #endif
1190 
1191 	if (sh->v_tag) {
1192 		/* we only go down this path if vtag is non-zero */
1193 		retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1194 		    inp_p, netp, sh->src_port, sh->dest_port);
1195 		if (retval) {
1196 			return (retval);
1197 		}
1198 	}
1199 	find_tcp_pool = 0;
1200 	if ((ch->chunk_type != SCTP_INITIATION) &&
1201 	    (ch->chunk_type != SCTP_INITIATION_ACK) &&
1202 	    (ch->chunk_type != SCTP_COOKIE_ACK) &&
1203 	    (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1204 		/* Other chunk types go to the tcp pool. */
1205 		find_tcp_pool = 1;
1206 	}
1207 	if (inp_p) {
1208 		retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1209 		    find_tcp_pool);
1210 		inp = *inp_p;
1211 	} else {
1212 		retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1213 		    find_tcp_pool);
1214 	}
1215 #ifdef SCTP_DEBUG
1216 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1217 		printf("retval:%p inp:%p\n", retval, inp);
1218 	}
1219 #endif
1220 	if (retval == NULL && inp) {
1221 		/* Found a EP but not this address */
1222 #ifdef SCTP_DEBUG
1223 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1224 			printf("Found endpoint %p but no asoc - ep state:%x\n",
1225 			    inp, inp->sctp_flags);
1226 		}
1227 #endif
1228 		if ((ch->chunk_type == SCTP_INITIATION) ||
1229 		    (ch->chunk_type == SCTP_INITIATION_ACK)) {
1230 			/*
1231 			 * special hook, we do NOT return linp or an
1232 			 * association that is linked to an existing
1233 			 * association that is under the TCP pool (i.e. no
1234 			 * listener exists). The endpoint finding routine
1235 			 * will always find a listner before examining the
1236 			 * TCP pool.
1237 			 */
1238 			if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1239 #ifdef SCTP_DEBUG
1240 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1241 					printf("Gak, its in the TCP pool... return NULL");
1242 				}
1243 #endif
1244 				if (inp_p) {
1245 					*inp_p = NULL;
1246 				}
1247 				return (NULL);
1248 			}
1249 #ifdef SCTP_DEBUG
1250 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1251 				printf("Now doing SPECIAL find\n");
1252 			}
1253 #endif
1254 			retval = sctp_findassociation_special_addr(m, iphlen,
1255 			    offset, sh, inp_p, netp, to);
1256 		}
1257 	}
1258 #ifdef SCTP_DEBUG
1259 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1260 	    printf("retval is %p\n", retval);
1261 	}
1262 #endif
1263 	return (retval);
1264 }
1265 
1266 extern int sctp_max_burst_default;
1267 
1268 extern unsigned int sctp_delayed_sack_time_default;
1269 extern unsigned int sctp_heartbeat_interval_default;
1270 extern unsigned int sctp_pmtu_raise_time_default;
1271 extern unsigned int sctp_shutdown_guard_time_default;
1272 extern unsigned int sctp_secret_lifetime_default;
1273 
1274 extern unsigned int sctp_rto_max_default;
1275 extern unsigned int sctp_rto_min_default;
1276 extern unsigned int sctp_rto_initial_default;
1277 extern unsigned int sctp_init_rto_max_default;
1278 extern unsigned int sctp_valid_cookie_life_default;
1279 extern unsigned int sctp_init_rtx_max_default;
1280 extern unsigned int sctp_assoc_rtx_max_default;
1281 extern unsigned int sctp_path_rtx_max_default;
1282 extern unsigned int sctp_nr_outgoing_streams_default;
1283 
1284 /*
1285  * allocate a sctp_inpcb and setup a temporary binding to a port/all
1286  * addresses. This way if we don't get a bind we by default pick a ephemeral
1287  * port with all addresses bound.
1288  */
1289 int
sctp_inpcb_alloc(struct socket * so)1290 sctp_inpcb_alloc(struct socket *so)
1291 {
1292 	/*
1293 	 * we get called when a new endpoint starts up. We need to allocate
1294 	 * the sctp_inpcb structure from the zone and init it. Mark it as
1295 	 * unbound and find a port that we can use as an ephemeral with
1296 	 * INADDR_ANY. If the user binds later no problem we can then add
1297 	 * in the specific addresses. And setup the default parameters for
1298 	 * the EP.
1299 	 */
1300 	int i, error;
1301 	struct sctp_inpcb *inp;
1302 #ifdef DEBUG
1303 	struct sctp_inpcb *n_inp;
1304 #endif
1305 #ifdef IPSEC
1306 	struct inpcbpolicy *pcb_sp = NULL;
1307 #endif
1308 	struct sctp_pcb *m;
1309 	struct timeval time;
1310 
1311 	error = 0;
1312 
1313         /* Hack alert:
1314 	 *
1315 	 * This code audits the entire INP list to see if
1316 	 * any ep's that are in the GONE state are now
1317 	 * all free. This should not happen really since when
1318 	 * the last association if freed we should end up deleting
1319 	 * the inpcb. This code including the locks should
1320 	 * be taken out ... since the last set of fixes I
1321 	 * have not seen the "Found a GONE on list" has not
1322 	 * came out. But i am paranoid and we will leave this
1323 	 * in at the cost of efficiency on allocation of PCB's.
1324 	 * Probably we should move this to the invariant
1325 	 * compile options
1326 	 */
1327 #ifdef DEBUG
1328 	SCTP_INP_INFO_RLOCK();
1329 	inp = LIST_FIRST(&sctppcbinfo.listhead);
1330 	while (inp) {
1331 		n_inp = LIST_NEXT(inp, sctp_list);
1332 		if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1333 			if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
1334 				/* finish the job now */
1335 				printf("Found a GONE on list\n");
1336 				SCTP_INP_INFO_RUNLOCK();
1337 				sctp_inpcb_free(inp, 1);
1338 				SCTP_INP_INFO_RLOCK();
1339 			}
1340 		}
1341 		inp = n_inp;
1342 	}
1343 	SCTP_INP_INFO_RUNLOCK();
1344 #endif /* DEBUG */
1345 
1346 	SCTP_INP_INFO_WLOCK();
1347 	inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
1348 	if (inp == NULL) {
1349 		printf("Out of SCTP-INPCB structures - no resources\n");
1350 		SCTP_INP_INFO_WUNLOCK();
1351 		return (ENOBUFS);
1352 	}
1353 
1354 	/* zap it */
1355 	memset(inp, 0, sizeof(*inp));
1356 
1357 	/* setup socket pointers */
1358 	inp->sctp_socket = so;
1359 
1360 	/* setup inpcb socket too */
1361 	inp->ip_inp.inp.inp_socket = so;
1362 	inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1363 #ifdef IPSEC
1364 	if (ipsec_enabled) {
1365 		error = ipsec_init_pcbpolicy(so, &pcb_sp);
1366 		if (error != 0) {
1367 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1368 			SCTP_INP_INFO_WUNLOCK();
1369 			return error;
1370 		}
1371 		/* Arrange to share the policy */
1372 		inp->ip_inp.inp.inp_sp = pcb_sp;
1373 		pcb_sp->sp_inph = (struct inpcb_hdr *)inp;
1374 	}
1375 #endif /* IPSEC */
1376 	sctppcbinfo.ipi_count_ep++;
1377 	inp->inp_ip_ttl = ip_defttl;
1378 	inp->inp_ip_tos = 0;
1379 
1380 	so->so_pcb = (void *)inp;
1381 
1382 	if ((so->so_type == SOCK_DGRAM) ||
1383 	    (so->so_type == SOCK_SEQPACKET)) {
1384 		/* UDP style socket */
1385 		inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1386 		    SCTP_PCB_FLAGS_UNBOUND);
1387 		inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1388 		/* Be sure it is NON-BLOCKING IO for UDP */
1389 		/*so->so_state |= SS_NBIO;*/
1390 	} else if (so->so_type == SOCK_STREAM) {
1391 		/* TCP style socket */
1392 		inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1393 		    SCTP_PCB_FLAGS_UNBOUND);
1394 		inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1395 		/* Be sure we have blocking IO bu default */
1396 		so->so_state &= ~SS_NBIO;
1397 	} else {
1398 		/*
1399 		 * unsupported socket type (RAW, etc)- in case we missed
1400 		 * it in protosw
1401 		 */
1402 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1403 		SCTP_INP_INFO_WUNLOCK();
1404 		return (EOPNOTSUPP);
1405 	}
1406 	inp->sctp_tcbhash = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_hash);
1407 	if (inp->sctp_tcbhash == NULL) {
1408 		printf("Out of SCTP-INPCB->hashinit - no resources\n");
1409 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1410 		SCTP_INP_INFO_WUNLOCK();
1411 		return (ENOBUFS);
1412 	} else {
1413 		for (i = 0; i < sctp_pcbtblsize; i++)
1414 			LIST_INIT(&inp->sctp_tcbhash[i]);
1415 		for (i = 1; i < sctp_pcbtblsize; i <<= 1)
1416 			continue;
1417 		inp->sctp_hashmark = i - 1;
1418 	}
1419         /* LOCK init's */
1420 	SCTP_INP_LOCK_INIT(inp);
1421 	SCTP_ASOC_CREATE_LOCK_INIT(inp);
1422 	/* lock the new ep */
1423 	SCTP_INP_WLOCK(inp);
1424 
1425 	/* add it to the info area */
1426 	LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1427 	SCTP_INP_INFO_WUNLOCK();
1428 
1429 	LIST_INIT(&inp->sctp_addr_list);
1430 	LIST_INIT(&inp->sctp_asoc_list);
1431 	TAILQ_INIT(&inp->sctp_queue_list);
1432 	/* Init the timer structure for signature change */
1433 	callout_init(&inp->sctp_ep.signature_change.timer, 0);
1434 	inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1435 
1436 	/* now init the actual endpoint default data */
1437 	m = &inp->sctp_ep;
1438 
1439 	/* setup the base timeout information */
1440 	m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1441 	m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1442 	m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1443 	m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_heartbeat_interval_default; /* this is in MSEC */
1444 	m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1445 	m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1446 	m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1447 	/* all max/min max are in ms */
1448 	m->sctp_maxrto = sctp_rto_max_default;
1449 	m->sctp_minrto = sctp_rto_min_default;
1450 	m->initial_rto = sctp_rto_initial_default;
1451 	m->initial_init_rto_max = sctp_init_rto_max_default;
1452 
1453 	m->max_open_streams_intome = MAX_SCTP_STREAMS;
1454 
1455 	m->max_init_times = sctp_init_rtx_max_default;
1456 	m->max_send_times = sctp_assoc_rtx_max_default;
1457 	m->def_net_failure = sctp_path_rtx_max_default;
1458 	m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1459 	m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1460 	m->max_burst = sctp_max_burst_default;
1461 	/* number of streams to pre-open on a association */
1462 	m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1463 
1464 	/* Add adaption cookie */
1465 	m->adaption_layer_indicator = 0x504C5253;
1466 
1467 	/* Minimum cookie size */
1468 	m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1469 		sizeof(struct sctp_state_cookie);
1470 	m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1471 
1472 	/* Setup the initial secret */
1473 	SCTP_GETTIME_TIMEVAL(&time);
1474 	m->time_of_secret_change = time.tv_sec;
1475 
1476 	for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1477 		m->secret_key[0][i] = sctp_select_initial_TSN(m);
1478 	}
1479 	sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1480 
1481 	/* How long is a cookie good for ? */
1482 	m->def_cookie_life = sctp_valid_cookie_life_default;
1483 	SCTP_INP_WUNLOCK(inp);
1484 	return (error);
1485 }
1486 
1487 
1488 void
sctp_move_pcb_and_assoc(struct sctp_inpcb * old_inp,struct sctp_inpcb * new_inp,struct sctp_tcb * stcb)1489 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1490     struct sctp_tcb *stcb)
1491 {
1492 	uint16_t lport, rport;
1493 	struct sctppcbhead *head;
1494 	struct sctp_laddr *laddr, *oladdr;
1495 
1496 	SCTP_TCB_UNLOCK(stcb);
1497 	SCTP_INP_INFO_WLOCK();
1498 	SCTP_INP_WLOCK(old_inp);
1499 	SCTP_INP_WLOCK(new_inp);
1500 	SCTP_TCB_LOCK(stcb);
1501 
1502 	new_inp->sctp_ep.time_of_secret_change =
1503 	    old_inp->sctp_ep.time_of_secret_change;
1504 	memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1505 	    sizeof(old_inp->sctp_ep.secret_key));
1506 	new_inp->sctp_ep.current_secret_number =
1507 	    old_inp->sctp_ep.current_secret_number;
1508 	new_inp->sctp_ep.last_secret_number =
1509 	    old_inp->sctp_ep.last_secret_number;
1510 	new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1511 
1512 	/* Copy the port across */
1513 	lport = new_inp->sctp_lport = old_inp->sctp_lport;
1514 	rport = stcb->rport;
1515 	/* Pull the tcb from the old association */
1516 	LIST_REMOVE(stcb, sctp_tcbhash);
1517 	LIST_REMOVE(stcb, sctp_tcblist);
1518 
1519 	/* Now insert the new_inp into the TCP connected hash */
1520 	head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1521 	    sctppcbinfo.hashtcpmark)];
1522 
1523 	LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1524 
1525 	/* Now move the tcb into the endpoint list */
1526 	LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1527 	/*
1528 	 * Question, do we even need to worry about the ep-hash since
1529 	 * we only have one connection? Probably not :> so lets
1530 	 * get rid of it and not suck up any kernel memory in that.
1531 	 */
1532 	SCTP_INP_INFO_WUNLOCK();
1533 	stcb->sctp_socket = new_inp->sctp_socket;
1534 	stcb->sctp_ep = new_inp;
1535 	if (new_inp->sctp_tcbhash != NULL) {
1536 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash,
1537 			      new_inp->sctp_tcbhash);
1538 		new_inp->sctp_tcbhash = NULL;
1539 	}
1540 	if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1541 		/* Subset bound, so copy in the laddr list from the old_inp */
1542 		LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1543 			laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
1544 			    sctppcbinfo.ipi_zone_laddr);
1545 			if (laddr == NULL) {
1546 				/*
1547 				 * Gak, what can we do? This assoc is really
1548 				 * HOSED. We probably should send an abort
1549 				 * here.
1550 				 */
1551 #ifdef SCTP_DEBUG
1552 				if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1553 					printf("Association hosed in TCP model, out of laddr memory\n");
1554 				}
1555 #endif /* SCTP_DEBUG */
1556 				continue;
1557 			}
1558 			sctppcbinfo.ipi_count_laddr++;
1559 			sctppcbinfo.ipi_gencnt_laddr++;
1560 			memset(laddr, 0, sizeof(*laddr));
1561 			laddr->ifa = oladdr->ifa;
1562 			LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1563 			    sctp_nxt_addr);
1564 			new_inp->laddr_count++;
1565 		}
1566 	}
1567 	SCTP_INP_WUNLOCK(new_inp);
1568 	SCTP_INP_WUNLOCK(old_inp);
1569 }
1570 
1571 static int
sctp_isport_inuse(struct sctp_inpcb * inp,uint16_t lport)1572 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
1573 {
1574 	struct sctppcbhead *head;
1575 	struct sctp_inpcb *t_inp;
1576 
1577 	head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1578 	    sctppcbinfo.hashmark)];
1579 	LIST_FOREACH(t_inp, head, sctp_hash) {
1580 		if (t_inp->sctp_lport != lport) {
1581 			continue;
1582 		}
1583 		/* This one is in use. */
1584 		/* check the v6/v4 binding issue */
1585 		if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1586 		    (((struct in6pcb *)t_inp)->in6p_flags & IN6P_IPV6_V6ONLY)) {
1587 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1588 				/* collision in V6 space */
1589 				return (1);
1590 			} else {
1591 				/* inp is BOUND_V4 no conflict */
1592 				continue;
1593 			}
1594 		} else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1595 			/* t_inp is bound v4 and v6, conflict always */
1596 			return (1);
1597 		} else {
1598 			/* t_inp is bound only V4 */
1599 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1600 			    (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1601 				) {
1602 				/* no conflict */
1603 				continue;
1604 			}
1605 			/* else fall through to conflict */
1606 		}
1607 		return (1);
1608 	}
1609 	return (0);
1610 }
1611 
1612 int
sctp_inpcb_bind(struct socket * so,struct sockaddr * addr,struct lwp * l)1613 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct lwp *l)
1614 {
1615 	/* bind a ep to a socket address */
1616 	struct sctppcbhead *head;
1617 	struct sctp_inpcb *inp, *inp_tmp;
1618 	int bindall;
1619 	uint16_t lport;
1620 	int error;
1621 
1622 	lport = 0;
1623 	error = 0;
1624 	bindall = 1;
1625 	inp = (struct sctp_inpcb *)so->so_pcb;
1626 #ifdef SCTP_DEBUG
1627 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1628 		if (addr) {
1629 			printf("Bind called port:%d\n",
1630 			       ntohs(((struct sockaddr_in *)addr)->sin_port));
1631 			printf("Addr :");
1632 			sctp_print_address(addr);
1633 		}
1634 	}
1635 #endif /* SCTP_DEBUG */
1636 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1637 		/* already did a bind, subsequent binds NOT allowed ! */
1638 		return (EINVAL);
1639 	}
1640 
1641 	if (addr != NULL) {
1642 		if (addr->sa_family == AF_INET) {
1643 			struct sockaddr_in *sin;
1644 
1645 			/* IPV6_V6ONLY socket? */
1646 			if (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY) {
1647 				return (EINVAL);
1648 			}
1649 
1650 			if (addr->sa_len != sizeof(*sin))
1651 				return (EINVAL);
1652 
1653 			sin = (struct sockaddr_in *)addr;
1654 			lport = sin->sin_port;
1655 
1656 			if (sin->sin_addr.s_addr != INADDR_ANY) {
1657 				bindall = 0;
1658 			}
1659 #ifdef IPSEC
1660 			inp->ip_inp.inp.inp_af = AF_INET;
1661 #endif
1662 		} else if (addr->sa_family == AF_INET6) {
1663 			/* Only for pure IPv6 Address. (No IPv4 Mapped!) */
1664 			struct sockaddr_in6 *sin6;
1665 
1666 			sin6 = (struct sockaddr_in6 *)addr;
1667 
1668 			if (addr->sa_len != sizeof(*sin6))
1669 				return (EINVAL);
1670 
1671 			lport = sin6->sin6_port;
1672 			if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1673 				bindall = 0;
1674 				/* KAME hack: embed scopeid */
1675 				error = sa6_embedscope(sin6, ip6_use_defzone);
1676 				if (error != 0)
1677 					return (error);
1678 			}
1679 #ifndef SCOPEDROUTING
1680 			/* this must be cleared for ifa_ifwithaddr() */
1681 			sin6->sin6_scope_id = 0;
1682 #endif /* SCOPEDROUTING */
1683 #ifdef IPSEC
1684 			inp->ip_inp.inp.inp_af = AF_INET6;
1685 #endif
1686 		} else {
1687 			return (EAFNOSUPPORT);
1688 		}
1689 #ifdef IPSEC
1690 		if (ipsec_enabled) {
1691 			inp->ip_inp.inp.inp_socket = so;
1692 			error = ipsec_init_pcbpolicy(so, &inp->ip_inp.inp.inp_sp);
1693 			if (error != 0)
1694 				return (error);
1695 			inp->ip_inp.inp.inp_sp->sp_inph = (struct inpcb_hdr *)inp;
1696 		}
1697 #endif
1698 	}
1699 	SCTP_INP_INFO_WLOCK();
1700 #ifdef SCTP_DEBUG
1701 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1702 		printf("sctp_inpcb_bind: after SCTP_INP_INFO_WLOCK\n");
1703 	}
1704 #endif /* SCTP_DEBUG */
1705 	SCTP_INP_WLOCK(inp);
1706 	/* increase our count due to the unlock we do */
1707 	SCTP_INP_INCR_REF(inp);
1708 	if (lport) {
1709 		enum kauth_network_req req;
1710 		/*
1711 		 * Did the caller specify a port? if so we must see if a
1712 		 * ep already has this one bound.
1713 		 */
1714 		if (ntohs(lport) < IPPORT_RESERVED)
1715 			req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
1716 		else
1717 			req = KAUTH_REQ_NETWORK_BIND_PORT;
1718 
1719 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
1720 		    req, so, addr, NULL);
1721 		if (error) {
1722 			SCTP_INP_DECR_REF(inp);
1723 			SCTP_INP_WUNLOCK(inp);
1724 			SCTP_INP_INFO_WUNLOCK();
1725 			return (EACCES);
1726 		}
1727 		SCTP_INP_WUNLOCK(inp);
1728 		inp_tmp = sctp_pcb_findep(addr, 0, 1);
1729 		if (inp_tmp != NULL) {
1730 			/* lock guy returned and lower count
1731 			 * note that we are not bound so inp_tmp
1732 			 * should NEVER be inp. And it is this
1733 			 * inp (inp_tmp) that gets the reference
1734 			 * bump, so we must lower it.
1735 			 */
1736 			SCTP_INP_WLOCK(inp_tmp);
1737 			SCTP_INP_DECR_REF(inp_tmp);
1738 			SCTP_INP_WUNLOCK(inp_tmp);
1739 
1740 			/* unlock info */
1741 			SCTP_INP_INFO_WUNLOCK();
1742 			return EADDRINUSE;
1743 		}
1744 		SCTP_INP_WLOCK(inp);
1745 		if (bindall) {
1746 			/* verify that no lport is not used by a singleton */
1747 			if (sctp_isport_inuse(inp, lport)) {
1748 				/* Sorry someone already has this one bound */
1749 				SCTP_INP_DECR_REF(inp);
1750 				SCTP_INP_WUNLOCK(inp);
1751 				SCTP_INP_INFO_WUNLOCK();
1752 				return EADDRINUSE;
1753 			}
1754 		}
1755 	} else {
1756 		/*
1757 		 * get any port but lets make sure no one has any address
1758 		 * with this port bound
1759 		 */
1760 
1761 		/*
1762 		 * setup the inp to the top (I could use the union but this
1763 		 * is just as easy
1764 		 */
1765 		uint32_t port_guess;
1766 		uint16_t port_attempt;
1767 		int not_done=1;
1768 
1769 		while (not_done) {
1770 			port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
1771 			port_attempt = (port_guess &  0x0000ffff);
1772 			if (port_attempt == 0) {
1773 				goto next_half;
1774 			}
1775 			if (port_attempt < IPPORT_RESERVED) {
1776 				port_attempt += IPPORT_RESERVED;
1777 			}
1778 
1779 			if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1780 				/* got a port we can use */
1781 				not_done = 0;
1782 				continue;
1783 			}
1784 			/* try upper half */
1785 		next_half:
1786 			port_attempt = ((port_guess >> 16) &  0x0000ffff);
1787 			if (port_attempt == 0) {
1788 				goto last_try;
1789 			}
1790 			if (port_attempt < IPPORT_RESERVED) {
1791 				port_attempt += IPPORT_RESERVED;
1792 			}
1793 			if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1794 				/* got a port we can use */
1795 				not_done = 0;
1796 				continue;
1797 			}
1798 			/* try two half's added together */
1799 		last_try:
1800 			port_attempt = (((port_guess >> 16) &  0x0000ffff) + (port_guess & 0x0000ffff));
1801 			if (port_attempt == 0) {
1802 				/* get a new random number */
1803 				continue;
1804 			}
1805 			if (port_attempt < IPPORT_RESERVED) {
1806 				port_attempt += IPPORT_RESERVED;
1807 			}
1808 			if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1809 				/* got a port we can use */
1810 				not_done = 0;
1811 				continue;
1812 			}
1813 		}
1814 		/* we don't get out of the loop until we have a port */
1815 		lport = htons(port_attempt);
1816 	}
1817 	SCTP_INP_DECR_REF(inp);
1818 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
1819 		/* this really should not happen. The guy
1820 		 * did a non-blocking bind and then did a close
1821 		 * at the same time.
1822 		 */
1823 		SCTP_INP_WUNLOCK(inp);
1824 		SCTP_INP_INFO_WUNLOCK();
1825 		return (EINVAL);
1826 	}
1827 	/* ok we look clear to give out this port, so lets setup the binding */
1828 	if (bindall) {
1829 		/* binding to all addresses, so just set in the proper flags */
1830 		inp->sctp_flags |= (SCTP_PCB_FLAGS_BOUNDALL |
1831 		    SCTP_PCB_FLAGS_DO_ASCONF);
1832 		/* set the automatic addr changes from kernel flag */
1833 		if (sctp_auto_asconf == 0) {
1834 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
1835 		} else {
1836 			inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
1837 		}
1838 	} else {
1839 		/*
1840 		 * bind specific, make sure flags is off and add a new address
1841 		 * structure to the sctp_addr_list inside the ep structure.
1842 		 *
1843 		 * We will need to allocate one and insert it at the head.
1844 		 * The socketopt call can just insert new addresses in there
1845 		 * as well. It will also have to do the embed scope kame hack
1846 		 * too (before adding).
1847 		 */
1848 		struct ifaddr *ifa;
1849 		struct sockaddr_storage store_sa;
1850 
1851 		memset(&store_sa, 0, sizeof(store_sa));
1852 		if (addr->sa_family == AF_INET) {
1853 			struct sockaddr_in *sin;
1854 
1855 			sin = (struct sockaddr_in *)&store_sa;
1856 			memcpy(sin, addr, sizeof(struct sockaddr_in));
1857 			sin->sin_port = 0;
1858 		} else if (addr->sa_family == AF_INET6) {
1859 			struct sockaddr_in6 *sin6;
1860 
1861 			sin6 = (struct sockaddr_in6 *)&store_sa;
1862 			memcpy(sin6, addr, sizeof(struct sockaddr_in6));
1863 			sin6->sin6_port = 0;
1864 		}
1865 		/*
1866 		 * first find the interface with the bound address
1867 		 * need to zero out the port to find the address! yuck!
1868 		 * can't do this earlier since need port for sctp_pcb_findep()
1869 		 */
1870 		ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
1871 		if (ifa == NULL) {
1872 			/* Can't find an interface with that address */
1873 			SCTP_INP_WUNLOCK(inp);
1874 			SCTP_INP_INFO_WUNLOCK();
1875 			return (EADDRNOTAVAIL);
1876 		}
1877 		if (addr->sa_family == AF_INET6) {
1878 			struct in6_ifaddr *ifa6;
1879 			ifa6 = (struct in6_ifaddr *)ifa;
1880 			/*
1881 			 * allow binding of deprecated addresses as per
1882 			 * RFC 2462 and ipng discussion
1883 			 */
1884 			if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
1885 			    IN6_IFF_ANYCAST |
1886 			    IN6_IFF_NOTREADY)) {
1887 				/* Can't bind a non-existent addr. */
1888 				SCTP_INP_WUNLOCK(inp);
1889 				SCTP_INP_INFO_WUNLOCK();
1890 				return (EINVAL);
1891 			}
1892 		}
1893 		/* we're not bound all */
1894 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
1895 #if 0 /* use sysctl now */
1896 		/* don't allow automatic addr changes from kernel */
1897 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
1898 #endif
1899 		/* set the automatic addr changes from kernel flag */
1900 		if (sctp_auto_asconf == 0) {
1901 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
1902 		} else {
1903 			inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
1904 		}
1905 		/* allow bindx() to send ASCONF's for binding changes */
1906 		inp->sctp_flags |= SCTP_PCB_FLAGS_DO_ASCONF;
1907 		/* add this address to the endpoint list */
1908 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
1909 		if (error != 0) {
1910 			SCTP_INP_WUNLOCK(inp);
1911 			SCTP_INP_INFO_WUNLOCK();
1912 			return (error);
1913 		}
1914 		inp->laddr_count++;
1915 	}
1916 	/* find the bucket */
1917 	head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1918 	    sctppcbinfo.hashmark)];
1919 	/* put it in the bucket */
1920 	LIST_INSERT_HEAD(head, inp, sctp_hash);
1921 #ifdef SCTP_DEBUG
1922 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1923 		printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
1924 	}
1925 #endif
1926 	/* set in the port */
1927 	inp->sctp_lport = lport;
1928 
1929 	/* turn off just the unbound flag */
1930 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1931 	SCTP_INP_WUNLOCK(inp);
1932 	SCTP_INP_INFO_WUNLOCK();
1933 	return (0);
1934 }
1935 
1936 
1937 static void
sctp_iterator_inp_being_freed(struct sctp_inpcb * inp,struct sctp_inpcb * inp_next)1938 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
1939 {
1940 	struct sctp_iterator *it;
1941 	/* We enter with the only the ITERATOR_LOCK in place and
1942 	 * A write lock on the inp_info stuff.
1943 	 */
1944 
1945 	/* Go through all iterators, we must do this since
1946 	 * it is possible that some iterator does NOT have
1947 	 * the lock, but is waiting for it. And the one that
1948 	 * had the lock has either moved in the last iteration
1949 	 * or we just cleared it above. We need to find all
1950 	 * of those guys. The list of iterators should never
1951 	 * be very big though.
1952 	 */
1953  	LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
1954 		if (it == inp->inp_starting_point_for_iterator)
1955 			/* skip this guy, he's special */
1956 			continue;
1957  		if (it->inp == inp) {
1958 			/* This is tricky and we DON'T lock the iterator.
1959 			 * Reason is he's running but waiting for me since
1960 			 * inp->inp_starting_point_for_iterator has the lock
1961 			 * on me (the guy above we skipped). This tells us
1962 			 * its is not running but waiting for inp->inp_starting_point_for_iterator
1963 			 * to be released by the guy that does have our INP in a lock.
1964 			 */
1965 			if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1966 				it->inp = NULL;
1967 				it->stcb = NULL;
1968 			} else {
1969 				/* set him up to do the next guy not me */
1970 				it->inp = inp_next;
1971 				it->stcb = NULL;
1972 			}
1973 		}
1974 	}
1975 	it = inp->inp_starting_point_for_iterator;
1976 	if (it) {
1977 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1978 			it->inp = NULL;
1979 		} else {
1980 			it->inp = inp_next;
1981 		}
1982 		it->stcb = NULL;
1983 	}
1984 }
1985 
1986 /* release sctp_inpcb unbind the port */
1987 void
sctp_inpcb_free(struct sctp_inpcb * inp,int immediate)1988 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate)
1989 {
1990 	/*
1991 	 * Here we free a endpoint. We must find it (if it is in the Hash
1992 	 * table) and remove it from there. Then we must also find it in
1993 	 * the overall list and remove it from there. After all removals are
1994 	 * complete then any timer has to be stopped. Then start the actual
1995 	 * freeing.
1996 	 * a) Any local lists.
1997 	 * b) Any associations.
1998 	 * c) The hash of all associations.
1999 	 * d) finally the ep itself.
2000 	 */
2001 	struct sctp_inpcb *inp_save;
2002 	struct sctp_tcb *asoc, *nasoc;
2003 	struct sctp_laddr *laddr, *nladdr;
2004 	struct inpcb *ip_pcb;
2005 	struct socket *so;
2006 	struct sctp_socket_q_list *sq;
2007 	int s, cnt;
2008 	struct rtentry *rt;
2009 
2010 	s = splsoftnet();
2011 	SCTP_ASOC_CREATE_LOCK(inp);
2012 	SCTP_INP_WLOCK(inp);
2013 
2014 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2015 		/* been here before */
2016 		splx(s);
2017 		printf("Endpoint was all gone (dup free)?\n");
2018 		SCTP_INP_WUNLOCK(inp);
2019 		SCTP_ASOC_CREATE_UNLOCK(inp);
2020 		return;
2021 	}
2022 	sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2023 
2024 	if (inp->control) {
2025 		sctp_m_freem(inp->control);
2026 		inp->control = NULL;
2027 	}
2028 	if (inp->pkt) {
2029 		sctp_m_freem(inp->pkt);
2030 		inp->pkt = NULL;
2031 	}
2032 	so = inp->sctp_socket;
2033 	ip_pcb = &inp->ip_inp.inp; /* we could just cast the main
2034 				   * pointer here but I will
2035 				   * be nice :> (i.e. ip_pcb = ep;)
2036 				   */
2037 
2038 	if (immediate == 0) {
2039 		int cnt_in_sd;
2040 		cnt_in_sd = 0;
2041 		for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2042 		     asoc = nasoc) {
2043 			nasoc = LIST_NEXT(asoc, sctp_tcblist);
2044 			if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2045 			    (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2046 				/* Just abandon things in the front states */
2047 				SCTP_TCB_LOCK(asoc);
2048 				SCTP_INP_WUNLOCK(inp);
2049 				sctp_free_assoc(inp, asoc);
2050 				SCTP_INP_WLOCK(inp);
2051 				continue;
2052 			} else {
2053 				asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2054 			}
2055 			if ((asoc->asoc.size_on_delivery_queue  > 0) ||
2056 			    (asoc->asoc.size_on_reasm_queue > 0) ||
2057 			    (asoc->asoc.size_on_all_streams > 0) ||
2058 			    (so && (so->so_rcv.sb_cc > 0))
2059 				) {
2060 				/* Left with Data unread */
2061 				struct mbuf *op_err;
2062 				MGET(op_err, M_DONTWAIT, MT_DATA);
2063 				if (op_err) {
2064 					/* Fill in the user initiated abort */
2065 					struct sctp_paramhdr *ph;
2066 					op_err->m_len =
2067 					    sizeof(struct sctp_paramhdr);
2068 					ph = mtod(op_err,
2069 					    struct sctp_paramhdr *);
2070 					ph->param_type = htons(
2071 					    SCTP_CAUSE_USER_INITIATED_ABT);
2072 					ph->param_length = htons(op_err->m_len);
2073 				}
2074 				SCTP_TCB_LOCK(asoc);
2075 				sctp_send_abort_tcb(asoc, op_err);
2076 
2077 				SCTP_INP_WUNLOCK(inp);
2078 				sctp_free_assoc(inp, asoc);
2079 				SCTP_INP_WLOCK(inp);
2080 				continue;
2081 			} else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2082 			    TAILQ_EMPTY(&asoc->asoc.sent_queue)) {
2083 				if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2084 				    (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2085 					/* there is nothing queued to send, so I send shutdown */
2086 					SCTP_TCB_LOCK(asoc);
2087 					sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2088 					asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2089 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2090 							 asoc->asoc.primary_destination);
2091 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2092 							 asoc->asoc.primary_destination);
2093 					sctp_chunk_output(inp, asoc, 1);
2094 					SCTP_TCB_UNLOCK(asoc);
2095 				}
2096 			} else {
2097 				/* mark into shutdown pending */
2098 				asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2099 			}
2100 			cnt_in_sd++;
2101 		}
2102 		/* now is there some left in our SHUTDOWN state? */
2103 		if (cnt_in_sd) {
2104 			inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE;
2105 			splx(s);
2106 			SCTP_INP_WUNLOCK(inp);
2107 			SCTP_ASOC_CREATE_UNLOCK(inp);
2108 			return;
2109 		}
2110 	}
2111 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
2112 	if (inp->refcount) {
2113 		sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2114 		SCTP_INP_WUNLOCK(inp);
2115 		SCTP_ASOC_CREATE_UNLOCK(inp);
2116 		return;
2117 	}
2118 #endif
2119 	inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2120 
2121 	/* XXX */
2122 	rt = rtcache_validate(&ip_pcb->inp_route);
2123 	rtcache_unref(rt, &ip_pcb->inp_route);
2124 
2125 	callout_stop(&inp->sctp_ep.signature_change.timer);
2126 	callout_destroy(&inp->sctp_ep.signature_change.timer);
2127 
2128 	if (so) {
2129 	/* First take care of socket level things */
2130 #ifdef IPSEC
2131 		if (ipsec_enabled)
2132 			ipsec_delete_pcbpolicy(ip_pcb);
2133 #endif /*IPSEC*/
2134 		so->so_pcb = 0;
2135 	}
2136 
2137 	if (ip_pcb->inp_options) {
2138 		(void)m_free(ip_pcb->inp_options);
2139 		ip_pcb->inp_options = 0;
2140 	}
2141 	rtcache_free(&ip_pcb->inp_route);
2142 	if (ip_pcb->inp_moptions) {
2143 		ip_freemoptions(ip_pcb->inp_moptions);
2144 		ip_pcb->inp_moptions = 0;
2145 	}
2146 	inp->inp_vflag = 0;
2147 
2148 	/* Now the sctp_pcb things */
2149 	/*
2150 	 * free each asoc if it is not already closed/free. we can't use
2151 	 * the macro here since le_next will get freed as part of the
2152 	 * sctp_free_assoc() call.
2153 	 */
2154 	cnt = 0;
2155 	for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2156 	     asoc = nasoc) {
2157 		nasoc = LIST_NEXT(asoc, sctp_tcblist);
2158 		SCTP_TCB_LOCK(asoc);
2159 		if (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) {
2160 			struct mbuf *op_err;
2161 			MGET(op_err, M_DONTWAIT, MT_DATA);
2162 			if (op_err) {
2163 				/* Fill in the user initiated abort */
2164 				struct sctp_paramhdr *ph;
2165 				op_err->m_len = sizeof(struct sctp_paramhdr);
2166 				ph = mtod(op_err, struct sctp_paramhdr *);
2167 				ph->param_type = htons(
2168 				    SCTP_CAUSE_USER_INITIATED_ABT);
2169 				ph->param_length = htons(op_err->m_len);
2170 			}
2171 			sctp_send_abort_tcb(asoc, op_err);
2172 		}
2173 		cnt++;
2174 		/*
2175 		 * sctp_free_assoc() will call sctp_inpcb_free(),
2176 		 * if SCTP_PCB_FLAGS_SOCKET_GONE set.
2177 		 * So, we clear it before sctp_free_assoc() making sure
2178 		 * no double sctp_inpcb_free().
2179 		 */
2180 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_SOCKET_GONE;
2181 		SCTP_INP_WUNLOCK(inp);
2182 		sctp_free_assoc(inp, asoc);
2183 		SCTP_INP_WLOCK(inp);
2184 	}
2185 	while ((sq = TAILQ_FIRST(&inp->sctp_queue_list)) != NULL) {
2186 		TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
2187 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
2188 		sctppcbinfo.ipi_count_sockq--;
2189 		sctppcbinfo.ipi_gencnt_sockq++;
2190 	}
2191 	inp->sctp_socket = 0;
2192 	/* Now first we remove ourselves from the overall list of all EP's */
2193 
2194 	/* Unlock inp first, need correct order */
2195 	SCTP_INP_WUNLOCK(inp);
2196 	/* now iterator lock */
2197 	SCTP_ITERATOR_LOCK();
2198 	/* now info lock */
2199 	SCTP_INP_INFO_WLOCK();
2200 	/* now reget the inp lock */
2201 	SCTP_INP_WLOCK(inp);
2202 
2203 	inp_save = LIST_NEXT(inp, sctp_list);
2204 	LIST_REMOVE(inp, sctp_list);
2205 	/*
2206 	 * Now the question comes as to if this EP was ever bound at all.
2207 	 * If it was, then we must pull it out of the EP hash list.
2208 	 */
2209 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2210 	    SCTP_PCB_FLAGS_UNBOUND) {
2211 		/*
2212 		 * ok, this guy has been bound. It's port is somewhere
2213 		 * in the sctppcbinfo hash table. Remove it!
2214 		 */
2215 		LIST_REMOVE(inp, sctp_hash);
2216 	}
2217         /* fix any iterators only after out of the list */
2218 	sctp_iterator_inp_being_freed(inp, inp_save);
2219 	SCTP_ITERATOR_UNLOCK();
2220 	/*
2221 	 * if we have an address list the following will free the list of
2222 	 * ifaddr's that are set into this ep. Again macro limitations here,
2223 	 * since the LIST_FOREACH could be a bad idea.
2224 	 */
2225 	for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2226 	     laddr = nladdr) {
2227 		nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2228 		LIST_REMOVE(laddr, sctp_nxt_addr);
2229 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
2230 		sctppcbinfo.ipi_gencnt_laddr++;
2231 		sctppcbinfo.ipi_count_laddr--;
2232 	}
2233 
2234 	/* Now lets see about freeing the EP hash table. */
2235 	if (inp->sctp_tcbhash != NULL) {
2236 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash, inp->sctp_tcbhash);
2237 		inp->sctp_tcbhash = NULL;
2238 	}
2239 	SCTP_INP_WUNLOCK(inp);
2240 	SCTP_ASOC_CREATE_UNLOCK(inp);
2241 	SCTP_INP_LOCK_DESTROY(inp);
2242 	SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2243 
2244 	/* Now we must put the ep memory back into the zone pool */
2245 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2246 	sctppcbinfo.ipi_count_ep--;
2247 
2248 	SCTP_INP_INFO_WUNLOCK();
2249 	splx(s);
2250 
2251 	sofree(so);
2252 	mutex_enter(softnet_lock);
2253 }
2254 
2255 
2256 struct sctp_nets *
sctp_findnet(struct sctp_tcb * stcb,struct sockaddr * addr)2257 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2258 {
2259 	struct sctp_nets *net;
2260 
2261 	/* use the peer's/remote port for lookup if unspecified */
2262 #if 0 /* why do we need to check the port for a nets list on an assoc? */
2263 	if (stcb->rport != sin->sin_port) {
2264 		/* we cheat and just a sin for this test */
2265 		return (NULL);
2266 	}
2267 #endif
2268 	/* locate the address */
2269 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2270 		if (sctp_cmpaddr(addr, rtcache_getdst(&net->ro)))
2271 			return (net);
2272 	}
2273 	return (NULL);
2274 }
2275 
2276 
2277 /*
2278  * add's a remote endpoint address, done with the INIT/INIT-ACK
2279  * as well as when a ASCONF arrives that adds it. It will also
2280  * initialize all the cwnd stats of stuff.
2281  */
2282 int
sctp_is_address_on_local_host(struct sockaddr * addr)2283 sctp_is_address_on_local_host(struct sockaddr *addr)
2284 {
2285 	struct ifnet *ifn;
2286 	struct ifaddr *ifa;
2287 	int s;
2288 
2289 	s = pserialize_read_enter();
2290 	IFNET_READER_FOREACH(ifn) {
2291 		IFADDR_READER_FOREACH(ifa, ifn) {
2292 			if (addr->sa_family == ifa->ifa_addr->sa_family) {
2293 				/* same family */
2294 				if (addr->sa_family == AF_INET) {
2295 					struct sockaddr_in *sin, *sin_c;
2296 					sin = (struct sockaddr_in *)addr;
2297 					sin_c = (struct sockaddr_in *)
2298 					    ifa->ifa_addr;
2299 					if (sin->sin_addr.s_addr ==
2300 					    sin_c->sin_addr.s_addr) {
2301 						/* we are on the same machine */
2302 						pserialize_read_exit(s);
2303 						return (1);
2304 					}
2305 				} else if (addr->sa_family == AF_INET6) {
2306 					struct sockaddr_in6 *sin6, *sin_c6;
2307 					sin6 = (struct sockaddr_in6 *)addr;
2308 					sin_c6 = (struct sockaddr_in6 *)
2309 					    ifa->ifa_addr;
2310 					if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2311 					    &sin_c6->sin6_addr)) {
2312 						/* we are on the same machine */
2313 						pserialize_read_exit(s);
2314 						return (1);
2315 					}
2316 				}
2317 			}
2318 		}
2319 	}
2320 	pserialize_read_exit(s);
2321 
2322 	return (0);
2323 }
2324 
2325 int
sctp_add_remote_addr(struct sctp_tcb * stcb,struct sockaddr * newaddr,int set_scope,int from)2326 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2327     int set_scope, int from)
2328 {
2329 	/*
2330 	 * The following is redundant to the same lines in the
2331 	 * sctp_aloc_assoc() but is needed since other's call the add
2332 	 * address function
2333 	 */
2334 	struct sctp_nets *net, *netfirst;
2335 	struct rtentry *rt, *netfirst_rt;
2336 	int addr_inscope;
2337 
2338 #ifdef SCTP_DEBUG
2339 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2340 		printf("Adding an address (from:%d) to the peer: ", from);
2341 		sctp_print_address(newaddr);
2342 	}
2343 #endif
2344 	netfirst = sctp_findnet(stcb, newaddr);
2345 	if (netfirst) {
2346 		/*
2347 		 * Lie and return ok, we don't want to make the association
2348 		 * go away for this behavior. It will happen in the TCP model
2349 		 * in a connected socket. It does not reach the hash table
2350 		 * until after the association is built so it can't be found.
2351 		 * Mark as reachable, since the initial creation will have
2352 		 * been cleared and the NOT_IN_ASSOC flag will have been
2353 		 * added... and we don't want to end up removing it back out.
2354 		 */
2355 		if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2356 			netfirst->dest_state = (SCTP_ADDR_REACHABLE|
2357 			    SCTP_ADDR_UNCONFIRMED);
2358 		} else {
2359 			netfirst->dest_state = SCTP_ADDR_REACHABLE;
2360 		}
2361 
2362 		return (0);
2363 	}
2364 	addr_inscope = 1;
2365 	if (newaddr->sa_family == AF_INET) {
2366 		struct sockaddr_in *sin;
2367 		sin = (struct sockaddr_in *)newaddr;
2368 		if (sin->sin_addr.s_addr == 0) {
2369 			/* Invalid address */
2370 			return (-1);
2371 		}
2372 		/* zero out the bzero area */
2373 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2374 
2375 		/* assure len is set */
2376 		sin->sin_len = sizeof(struct sockaddr_in);
2377 		if (set_scope) {
2378 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2379 			stcb->ipv4_local_scope = 1;
2380 #else
2381 			if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2382 				stcb->asoc.ipv4_local_scope = 1;
2383 			}
2384 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2385 
2386 			if (sctp_is_address_on_local_host(newaddr)) {
2387 				stcb->asoc.loopback_scope = 1;
2388 				stcb->asoc.ipv4_local_scope = 1;
2389 				stcb->asoc.local_scope = 1;
2390 				stcb->asoc.site_scope = 1;
2391 			}
2392 		} else {
2393 			if (from == 8) {
2394 				/* From connectx */
2395 				if (sctp_is_address_on_local_host(newaddr)) {
2396 					stcb->asoc.loopback_scope = 1;
2397 					stcb->asoc.ipv4_local_scope = 1;
2398 					stcb->asoc.local_scope = 1;
2399 					stcb->asoc.site_scope = 1;
2400 				}
2401 			}
2402 			/* Validate the address is in scope */
2403 			if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2404 			    (stcb->asoc.ipv4_local_scope == 0)) {
2405 				addr_inscope = 0;
2406 			}
2407 		}
2408 	} else if (newaddr->sa_family == AF_INET6) {
2409 		struct sockaddr_in6 *sin6;
2410 		sin6 = (struct sockaddr_in6 *)newaddr;
2411 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2412 			/* Invalid address */
2413 			return (-1);
2414 		}
2415 		/* assure len is set */
2416 		sin6->sin6_len = sizeof(struct sockaddr_in6);
2417 		if (set_scope) {
2418 			if (sctp_is_address_on_local_host(newaddr)) {
2419 				stcb->asoc.loopback_scope = 1;
2420 				stcb->asoc.local_scope = 1;
2421 				stcb->asoc.ipv4_local_scope = 1;
2422 				stcb->asoc.site_scope = 1;
2423 			} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2424 				/*
2425 				 * If the new destination is a LINK_LOCAL
2426 				 * we must have common site scope. Don't set
2427 				 * the local scope since we may not share all
2428 				 * links, only loopback can do this.
2429  				 * Links on the local network would also
2430  				 * be on our private network for v4 too.
2431 				 */
2432  				stcb->asoc.ipv4_local_scope = 1;
2433 				stcb->asoc.site_scope = 1;
2434 			} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2435 				/*
2436 				 * If the new destination is SITE_LOCAL
2437 				 * then we must have site scope in common.
2438 				 */
2439 				stcb->asoc.site_scope = 1;
2440 			}
2441 		} else {
2442 			if (from == 8) {
2443 				/* From connectx */
2444 				if (sctp_is_address_on_local_host(newaddr)) {
2445 					stcb->asoc.loopback_scope = 1;
2446 					stcb->asoc.ipv4_local_scope = 1;
2447 					stcb->asoc.local_scope = 1;
2448 					stcb->asoc.site_scope = 1;
2449 				}
2450 			}
2451 			/* Validate the address is in scope */
2452 			if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2453 			    (stcb->asoc.loopback_scope == 0)) {
2454 				addr_inscope = 0;
2455 			} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2456 				   (stcb->asoc.local_scope == 0)) {
2457 				addr_inscope = 0;
2458 			} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2459 				   (stcb->asoc.site_scope == 0)) {
2460 				addr_inscope = 0;
2461 			}
2462 		}
2463 	} else {
2464 		/* not supported family type */
2465 		return (-1);
2466 	}
2467 	net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
2468 	if (net == NULL) {
2469 		return (-1);
2470 	}
2471 	sctppcbinfo.ipi_count_raddr++;
2472 	sctppcbinfo.ipi_gencnt_raddr++;
2473 	memset(net, 0, sizeof(*net));
2474 	if (newaddr->sa_family == AF_INET) {
2475 		((struct sockaddr_in *)newaddr)->sin_port = stcb->rport;
2476 	} else if (newaddr->sa_family == AF_INET6) {
2477 		((struct sockaddr_in6 *)newaddr)->sin6_port = stcb->rport;
2478 	}
2479 	net->addr_is_local = sctp_is_address_on_local_host(newaddr);
2480 	net->failure_threshold = stcb->asoc.def_net_failure;
2481 	if (addr_inscope == 0) {
2482 #ifdef SCTP_DEBUG
2483 		if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2484 			printf("Adding an address which is OUT OF SCOPE\n");
2485 		}
2486 #endif /* SCTP_DEBUG */
2487 		net->dest_state = (SCTP_ADDR_REACHABLE |
2488 		    SCTP_ADDR_OUT_OF_SCOPE);
2489 	} else {
2490 		if (from == 8)
2491 			/* 8 is passed by connect_x */
2492 			net->dest_state = SCTP_ADDR_REACHABLE;
2493 		else
2494 			net->dest_state = SCTP_ADDR_REACHABLE |
2495 			    SCTP_ADDR_UNCONFIRMED;
2496 	}
2497 	net->RTO = stcb->asoc.initial_rto;
2498 	stcb->asoc.numnets++;
2499 	net->ref_count = 1;
2500 
2501 	/* Init the timer structure */
2502 	callout_init(&net->rxt_timer.timer, 0);
2503 	callout_init(&net->pmtu_timer.timer, 0);
2504 
2505 	/* Now generate a route for this guy */
2506 	/* KAME hack: embed scope zone ID */
2507 	if (newaddr->sa_family == AF_INET6) {
2508 		struct sockaddr_in6 *sin6;
2509 		sin6 = (struct sockaddr_in6 *)newaddr;
2510 		if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2511 			return (-1);
2512 	}
2513 	rt = rtcache_lookup(&net->ro, newaddr);
2514 	if (rt) {
2515 		net->mtu = rt->rt_ifp->if_mtu;
2516 		if (from == 1) {
2517 			stcb->asoc.smallest_mtu = net->mtu;
2518 		}
2519 		/* start things off to match mtu of interface please. */
2520 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
2521 	} else {
2522 		net->mtu = stcb->asoc.smallest_mtu;
2523 	}
2524 #ifdef SCTP_DEBUG
2525 	printf("After lookup\n");
2526 #endif
2527 	if (stcb->asoc.smallest_mtu > net->mtu) {
2528 		stcb->asoc.smallest_mtu = net->mtu;
2529 	}
2530 	/* We take the max of the burst limit times a MTU or the INITIAL_CWND.
2531 	 * We then limit this to 4 MTU's of sending.
2532 	 */
2533  	net->cwnd = uimin((net->mtu * 4), uimax((stcb->asoc.max_burst * net->mtu), SCTP_INITIAL_CWND));
2534 
2535 	/* we always get at LEAST 2 MTU's */
2536 	if (net->cwnd < (2 * net->mtu)) {
2537 		net->cwnd = 2 * net->mtu;
2538 	}
2539 
2540 	net->ssthresh = stcb->asoc.peers_rwnd;
2541 
2542 	net->src_addr_selected = 0;
2543 	netfirst = TAILQ_FIRST(&stcb->asoc.nets);
2544 	if (rt == NULL) {
2545 		/* Since we have no route put it at the back */
2546 		TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
2547 	} else if (netfirst == NULL) {
2548 		/* We are the first one in the pool. */
2549 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2550 	} else if ((netfirst_rt = rtcache_validate(&netfirst->ro)) == NULL) {
2551 		/*
2552 		 * First one has NO route. Place this one ahead of the
2553 		 * first one.
2554 		 */
2555 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2556 	} else if (rt->rt_ifp != netfirst_rt->rt_ifp) {
2557 		rtcache_unref(netfirst_rt, &netfirst->ro);
2558 		/*
2559 		 * This one has a different interface than the one at the
2560 		 * top of the list. Place it ahead.
2561 		 */
2562 		TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2563 	} else {
2564 		/*
2565 		 * Ok we have the same interface as the first one. Move
2566 		 * forward until we find either
2567 		 *   a) one with a NULL route... insert ahead of that
2568 		 *   b) one with a different ifp.. insert after that.
2569 		 *   c) end of the list.. insert at the tail.
2570 		 */
2571 		struct sctp_nets *netlook;
2572 		struct rtentry *netlook_rt;
2573 		do {
2574 			netlook = TAILQ_NEXT(netfirst, sctp_next);
2575 			if (netlook == NULL) {
2576 				/* End of the list */
2577 				TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
2578 				    sctp_next);
2579 				break;
2580 			} else if ((netlook_rt = rtcache_validate(&netlook->ro)) == NULL) {
2581 				/* next one has NO route */
2582 				TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
2583 				break;
2584 			} else if (netlook_rt->rt_ifp != rt->rt_ifp) {
2585 				rtcache_unref(netlook_rt, &netlook->ro);
2586 				TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
2587 				    net, sctp_next);
2588 				break;
2589 			}
2590 			rtcache_unref(netlook_rt, &netlook->ro);
2591 			/* Shift forward */
2592 			netfirst = netlook;
2593 		} while (netlook != NULL);
2594 		rtcache_unref(netfirst_rt, &netfirst->ro);
2595 	}
2596 	/* got to have a primary set */
2597 	if (stcb->asoc.primary_destination == 0) {
2598 		stcb->asoc.primary_destination = net;
2599 	} else if (!rtcache_validate(&stcb->asoc.primary_destination->ro)) {
2600 		/* No route to current primary adopt new primary */
2601 		stcb->asoc.primary_destination = net;
2602 	}
2603 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
2604 	    net);
2605 
2606 	return (0);
2607 }
2608 
2609 
2610 /*
2611  * allocate an association and add it to the endpoint. The caller must
2612  * be careful to add all additional addresses once they are know right
2613  * away or else the assoc will be may experience a blackout scenario.
2614  */
2615 struct sctp_tcb *
sctp_aloc_assoc(struct sctp_inpcb * inp,struct sockaddr * firstaddr,int for_a_init,int * error,uint32_t override_tag)2616 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
2617     int for_a_init, int *error,  uint32_t override_tag)
2618 {
2619 	struct sctp_tcb *stcb;
2620 	struct sctp_association *asoc;
2621 	struct sctpasochead *head;
2622 	uint16_t rport;
2623 	int err;
2624 
2625 	/*
2626 	 * Assumption made here:
2627 	 *  Caller has done a sctp_findassociation_ep_addr(ep, addr's);
2628 	 *  to make sure the address does not exist already.
2629 	 */
2630 	if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
2631 		/* Hit max assoc, sorry no more */
2632 		*error = ENOBUFS;
2633 		return (NULL);
2634 	}
2635 	SCTP_INP_RLOCK(inp);
2636 	if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2637 		/*
2638 		 * If its in the TCP pool, its NOT allowed to create an
2639 		 * association. The parent listener needs to call
2640 		 * sctp_aloc_assoc.. or the one-2-many socket. If a
2641 		 * peeled off, or connected one does this.. its an error.
2642 		 */
2643 		SCTP_INP_RUNLOCK(inp);
2644 		*error = EINVAL;
2645 		return (NULL);
2646  	}
2647 
2648 #ifdef SCTP_DEBUG
2649 	if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2650 		printf("Allocate an association for peer:");
2651 		if (firstaddr)
2652 			sctp_print_address(firstaddr);
2653 		else
2654 			printf("None\n");
2655 		printf("Port:%d\n",
2656 		       ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
2657 	}
2658 #endif /* SCTP_DEBUG */
2659 	if (firstaddr->sa_family == AF_INET) {
2660 		struct sockaddr_in *sin;
2661 		sin = (struct sockaddr_in *)firstaddr;
2662 		if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
2663 			/* Invalid address */
2664 #ifdef SCTP_DEBUG
2665 			if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2666 				printf("peer address invalid\n");
2667 			}
2668 #endif
2669 			SCTP_INP_RUNLOCK(inp);
2670 			*error = EINVAL;
2671 			return (NULL);
2672 		}
2673 		rport = sin->sin_port;
2674 	} else if (firstaddr->sa_family == AF_INET6) {
2675 		struct sockaddr_in6 *sin6;
2676 		sin6 = (struct sockaddr_in6 *)firstaddr;
2677 		if ((sin6->sin6_port == 0) ||
2678 		    (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
2679 			/* Invalid address */
2680 #ifdef SCTP_DEBUG
2681 			if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2682 				printf("peer address invalid\n");
2683 			}
2684 #endif
2685 			SCTP_INP_RUNLOCK(inp);
2686 			*error = EINVAL;
2687 			return (NULL);
2688 		}
2689 		rport = sin6->sin6_port;
2690 	} else {
2691 		/* not supported family type */
2692 #ifdef SCTP_DEBUG
2693 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2694 			printf("BAD family %d\n", firstaddr->sa_family);
2695 		}
2696 #endif
2697 		SCTP_INP_RUNLOCK(inp);
2698 		*error = EINVAL;
2699 		return (NULL);
2700 	}
2701 	SCTP_INP_RUNLOCK(inp);
2702 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
2703 		/*
2704 		 * If you have not performed a bind, then we need to do
2705 		 * the ephemerial bind for you.
2706 		 */
2707 #ifdef SCTP_DEBUG
2708 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2709 			printf("Doing implicit BIND\n");
2710 		}
2711 #endif
2712 
2713 		if ((err = sctp_inpcb_bind(inp->sctp_socket,
2714 		    (struct sockaddr *)NULL, (struct lwp *)NULL))){
2715 			/* bind error, probably perm */
2716 #ifdef SCTP_DEBUG
2717 			if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2718 				printf("BIND FAILS ret:%d\n", err);
2719 			}
2720 #endif
2721 
2722 			*error = err;
2723 			return (NULL);
2724 		}
2725 	}
2726 	stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
2727 	if (stcb == NULL) {
2728 		/* out of memory? */
2729 #ifdef SCTP_DEBUG
2730 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2731 			printf("aloc_assoc: no assoc mem left, stcb=NULL\n");
2732 		}
2733 #endif
2734 		*error = ENOMEM;
2735 		return (NULL);
2736 	}
2737 	sctppcbinfo.ipi_count_asoc++;
2738 	sctppcbinfo.ipi_gencnt_asoc++;
2739 
2740 	memset(stcb, 0, sizeof(*stcb));
2741 	asoc = &stcb->asoc;
2742 	SCTP_TCB_LOCK_INIT(stcb);
2743 	/* setup back pointers */
2744 #ifdef SCTP_DEBUG
2745 	printf("Before back pointers\n");
2746 #endif
2747 	stcb->sctp_ep = inp;
2748 	stcb->sctp_socket = inp->sctp_socket;
2749 	if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
2750 		/* failed */
2751 		SCTP_TCB_LOCK_DESTROY (stcb);
2752 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2753 		sctppcbinfo.ipi_count_asoc--;
2754 #ifdef SCTP_DEBUG
2755 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2756 			printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
2757 		}
2758 #endif
2759 		*error = err;
2760 		return (NULL);
2761 	}
2762 	/* and the port */
2763 	stcb->rport = rport;
2764 	SCTP_INP_INFO_WLOCK();
2765 	SCTP_INP_WLOCK(inp);
2766 	if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2767 		/* inpcb freed while alloc going on */
2768 		SCTP_TCB_LOCK_DESTROY (stcb);
2769 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2770 		SCTP_INP_WUNLOCK(inp);
2771 		SCTP_INP_INFO_WUNLOCK();
2772 		sctppcbinfo.ipi_count_asoc--;
2773 #ifdef SCTP_DEBUG
2774 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2775 			printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
2776 		}
2777 #endif
2778 		*error = EINVAL;
2779 		return (NULL);
2780 	}
2781 	SCTP_TCB_LOCK(stcb);
2782 
2783 	/* now that my_vtag is set, add it to the  hash */
2784 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2785 	     sctppcbinfo.hashasocmark)];
2786 	/* put it in the bucket in the vtag hash of assoc's for the system */
2787 	LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2788 	SCTP_INP_INFO_WUNLOCK();
2789 
2790 
2791 	if ((err = sctp_add_remote_addr(stcb, firstaddr, 1, 1))) {
2792 		/* failure.. memory error? */
2793 		if (asoc->strmout)
2794 			free(asoc->strmout, M_PCB);
2795 		if (asoc->mapping_array)
2796 			free(asoc->mapping_array, M_PCB);
2797 
2798 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2799 		sctppcbinfo.ipi_count_asoc--;
2800 #ifdef SCTP_DEBUG
2801 		if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2802 			printf("aloc_assoc: couldn't add remote addr!\n");
2803 		}
2804 #endif
2805 		SCTP_TCB_LOCK_DESTROY (stcb);
2806 		*error = ENOBUFS;
2807 		return (NULL);
2808 	}
2809 	/* Init all the timers */
2810 	callout_init(&asoc->hb_timer.timer, 0);
2811 	callout_init(&asoc->dack_timer.timer, 0);
2812 	callout_init(&asoc->asconf_timer.timer, 0);
2813 	callout_init(&asoc->shut_guard_timer.timer, 0);
2814 	callout_init(&asoc->autoclose_timer.timer, 0);
2815 	callout_init(&asoc->delayed_event_timer.timer, 0);
2816 	LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
2817 	/* now file the port under the hash as well */
2818 #ifdef SCTP_DEBUG
2819 	printf("Before hashing %ld size %d\n",
2820 		inp->sctp_hashmark, sctp_pcbtblsize);
2821 #endif
2822 	if (inp->sctp_tcbhash != NULL) {
2823 		head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
2824 		   inp->sctp_hashmark)];
2825 		LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
2826 	}
2827 #ifdef SCTP_DEBUG
2828 	printf("After hashing\n");
2829 #endif
2830 	SCTP_INP_WUNLOCK(inp);
2831 #ifdef SCTP_DEBUG
2832 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2833 		printf("Association %p now allocated\n", stcb);
2834 	}
2835 #endif
2836 	return (stcb);
2837 }
2838 
2839 void
sctp_free_remote_addr(struct sctp_nets * net)2840 sctp_free_remote_addr(struct sctp_nets *net)
2841 {
2842 	if (net == NULL)
2843 		return;
2844 	net->ref_count--;
2845 	if (net->ref_count <= 0) {
2846 		/* stop timer if running */
2847 		callout_stop(&net->rxt_timer.timer);
2848 		callout_stop(&net->pmtu_timer.timer);
2849 		callout_destroy(&net->rxt_timer.timer);
2850 		callout_destroy(&net->pmtu_timer.timer);
2851 		net->dest_state = SCTP_ADDR_NOT_REACHABLE;
2852 		rtcache_free(&net->ro);
2853 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
2854 		sctppcbinfo.ipi_count_raddr--;
2855 	}
2856 }
2857 
2858 /*
2859  * remove a remote endpoint address from an association, it
2860  * will fail if the address does not exist.
2861  */
2862 int
sctp_del_remote_addr(struct sctp_tcb * stcb,struct sockaddr * remaddr)2863 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
2864 {
2865 	/*
2866 	 * Here we need to remove a remote address. This is quite simple, we
2867 	 * first find it in the list of address for the association
2868 	 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE on
2869 	 * that item.
2870 	 * Note we do not allow it to be removed if there are no other
2871 	 * addresses.
2872 	 */
2873 	struct sctp_association *asoc;
2874 	struct sctp_nets *net, *net_tmp;
2875 	asoc = &stcb->asoc;
2876 	if (asoc->numnets < 2) {
2877 		/* Must have at LEAST two remote addresses */
2878 		return (-1);
2879 	}
2880 	/* locate the address */
2881 	for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
2882 		net_tmp = TAILQ_NEXT(net, sctp_next);
2883 		if (rtcache_getdst(&net->ro)->sa_family != remaddr->sa_family) {
2884 			continue;
2885 		}
2886 		if (sctp_cmpaddr(rtcache_getdst(&net->ro), remaddr)) {
2887 			/* we found the guy */
2888 			asoc->numnets--;
2889 			TAILQ_REMOVE(&asoc->nets, net, sctp_next);
2890 			sctp_free_remote_addr(net);
2891 			if (net == asoc->primary_destination) {
2892 				/* Reset primary */
2893 				struct sctp_nets *lnet;
2894 				lnet = TAILQ_FIRST(&asoc->nets);
2895 				/* Try to find a confirmed primary */
2896 				asoc->primary_destination =
2897 				    sctp_find_alternate_net(stcb, lnet);
2898 			}
2899 			if (net == asoc->last_data_chunk_from) {
2900 				/* Reset primary */
2901 				asoc->last_data_chunk_from =
2902 				    TAILQ_FIRST(&asoc->nets);
2903 			}
2904 			if (net == asoc->last_control_chunk_from) {
2905 				/* Reset primary */
2906 				asoc->last_control_chunk_from =
2907 				    TAILQ_FIRST(&asoc->nets);
2908 			}
2909 			if (net == asoc->asconf_last_sent_to) {
2910 				/* Reset primary */
2911 				asoc->asconf_last_sent_to =
2912 				    TAILQ_FIRST(&asoc->nets);
2913 			}
2914 			return (0);
2915 		}
2916 	}
2917 	/* not found. */
2918 	return (-2);
2919 }
2920 
2921 
2922 static void
sctp_add_vtag_to_timewait(struct sctp_inpcb * inp,u_int32_t tag)2923 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, u_int32_t tag)
2924 {
2925 	struct sctpvtaghead *chain;
2926 	struct sctp_tagblock *twait_block;
2927 	struct timeval now;
2928 	int set, i;
2929 	SCTP_GETTIME_TIMEVAL(&now);
2930 	chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
2931 	set = 0;
2932 	if (!LIST_EMPTY(chain)) {
2933 		/* Block(s) present, lets find space, and expire on the fly */
2934 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
2935 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
2936 				if ((twait_block->vtag_block[i].v_tag == 0) &&
2937 				    !set) {
2938 					twait_block->vtag_block[0].tv_sec_at_expire =
2939 					    now.tv_sec + SCTP_TIME_WAIT;
2940 					twait_block->vtag_block[0].v_tag = tag;
2941 					set = 1;
2942 				} else if ((twait_block->vtag_block[i].v_tag) &&
2943 				    ((long)twait_block->vtag_block[i].tv_sec_at_expire >
2944 				    now.tv_sec)) {
2945 					/* Audit expires this guy */
2946 					twait_block->vtag_block[i].tv_sec_at_expire = 0;
2947 					twait_block->vtag_block[i].v_tag = 0;
2948 					if (set == 0) {
2949 						/* Reuse it for my new tag */
2950 						twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
2951 						twait_block->vtag_block[0].v_tag = tag;
2952 						set = 1;
2953 					}
2954 				}
2955 			}
2956 			if (set) {
2957 				/*
2958 				 * We only do up to the block where we can
2959 				 * place our tag for audits
2960 				 */
2961 				break;
2962 			}
2963 		}
2964 	}
2965 	/* Need to add a new block to chain */
2966 	if (!set) {
2967 		twait_block = malloc(sizeof(struct sctp_tagblock), M_PCB, M_NOWAIT);
2968 		if (twait_block == NULL) {
2969 			return;
2970 		}
2971 		memset(twait_block, 0, sizeof(struct sctp_timewait));
2972 		LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
2973 		twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
2974 		    SCTP_TIME_WAIT;
2975 		twait_block->vtag_block[0].v_tag = tag;
2976 	}
2977 }
2978 
2979 
2980 static void
sctp_iterator_asoc_being_freed(struct sctp_inpcb * inp,struct sctp_tcb * stcb)2981 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2982 {
2983 	struct sctp_iterator *it;
2984 
2985 
2986 
2987 	/* Unlock the tcb lock we do this so
2988 	 * we avoid a dead lock scenario where
2989 	 * the iterator is waiting on the TCB lock
2990 	 * and the TCB lock is waiting on the iterator
2991 	 * lock.
2992 	 */
2993 	SCTP_ITERATOR_LOCK();
2994 	SCTP_INP_INFO_WLOCK();
2995 	SCTP_INP_WLOCK(inp);
2996 	SCTP_TCB_LOCK(stcb);
2997 
2998 	it = stcb->asoc.stcb_starting_point_for_iterator;
2999 	if (it == NULL) {
3000 		return;
3001 	}
3002 	if (it->inp != stcb->sctp_ep) {
3003 		/* hm, focused on the wrong one? */
3004 		return;
3005 	}
3006 	if (it->stcb != stcb) {
3007 		return;
3008 	}
3009 	it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3010 	if (it->stcb == NULL) {
3011 		/* done with all asoc's in this assoc */
3012 		if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3013 			it->inp = NULL;
3014 		} else {
3015 
3016 			it->inp = LIST_NEXT(inp, sctp_list);
3017 		}
3018 	}
3019 }
3020 
3021 /*
3022  * Free the association after un-hashing the remote port.
3023  */
3024 void
sctp_free_assoc(struct sctp_inpcb * inp,struct sctp_tcb * stcb)3025 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3026 {
3027 	struct sctp_association *asoc;
3028 	struct sctp_nets *net, *prev;
3029 	struct sctp_laddr *laddr;
3030 	struct sctp_tmit_chunk *chk;
3031 	struct sctp_asconf_addr *aparam;
3032 	struct sctp_socket_q_list *sq;
3033 	int s;
3034 
3035 	/* first, lets purge the entry from the hash table. */
3036 	s = splsoftnet();
3037 	if (stcb->asoc.state == 0) {
3038 		printf("Freeing already free association:%p - huh??\n",
3039 		    stcb);
3040 		splx(s);
3041 		return;
3042 	}
3043 	asoc = &stcb->asoc;
3044 	asoc->state = 0;
3045 	/* now clean up any other timers */
3046 	callout_stop(&asoc->hb_timer.timer);
3047 	callout_destroy(&asoc->hb_timer.timer);
3048 	callout_stop(&asoc->dack_timer.timer);
3049 	callout_destroy(&asoc->dack_timer.timer);
3050 	callout_stop(&asoc->asconf_timer.timer);
3051 	callout_destroy(&asoc->asconf_timer.timer);
3052 	callout_stop(&asoc->shut_guard_timer.timer);
3053 	callout_destroy(&asoc->shut_guard_timer.timer);
3054 	callout_stop(&asoc->autoclose_timer.timer);
3055 	callout_destroy(&asoc->autoclose_timer.timer);
3056 	callout_stop(&asoc->delayed_event_timer.timer);
3057 	callout_destroy(&asoc->delayed_event_timer.timer);
3058 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3059 		callout_stop(&net->rxt_timer.timer);
3060 		callout_stop(&net->pmtu_timer.timer);
3061 		callout_destroy(&net->rxt_timer.timer);
3062 		callout_destroy(&net->pmtu_timer.timer);
3063 	}
3064 
3065 	/* Iterator asoc being freed we send an
3066 	 * unlocked TCB. It returns with INP_INFO
3067 	 * and INP write locked and the TCB locked
3068 	 * too and of course the iterator lock
3069 	 * in place as well..
3070 	 */
3071 	SCTP_TCB_UNLOCK(stcb);
3072 	sctp_iterator_asoc_being_freed(inp, stcb);
3073 
3074 	/* Null all of my entry's on the socket q */
3075 	TAILQ_FOREACH(sq, &inp->sctp_queue_list, next_sq) {
3076 		if (sq->tcb == stcb) {
3077 			sq->tcb = NULL;
3078 		}
3079 	}
3080 
3081 	if (inp->sctp_tcb_at_block == (void *)stcb) {
3082 		inp->error_on_block = ECONNRESET;
3083 	}
3084 
3085 	if (inp->sctp_tcbhash) {
3086 		LIST_REMOVE(stcb, sctp_tcbhash);
3087 	}
3088 	/* Now lets remove it from the list of ALL associations in the EP */
3089 	LIST_REMOVE(stcb, sctp_tcblist);
3090 	SCTP_INP_WUNLOCK(inp);
3091 	SCTP_ITERATOR_UNLOCK();
3092 
3093 
3094 	/* pull from vtag hash */
3095 	LIST_REMOVE(stcb, sctp_asocs);
3096 
3097 	/*
3098 	 * Now before we can free the assoc, we must  remove all of the
3099 	 * networks and any other allocated space.. i.e. add removes here
3100 	 * before the SCTP_ZONE_FREE() of the tasoc entry.
3101 	 */
3102 
3103 	sctp_add_vtag_to_timewait(inp, asoc->my_vtag);
3104 	SCTP_INP_INFO_WUNLOCK();
3105 	prev = NULL;
3106 	while (!TAILQ_EMPTY(&asoc->nets)) {
3107 		net = TAILQ_FIRST(&asoc->nets);
3108 		/* pull from list */
3109 		if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3110 			break;
3111 		}
3112 		prev = net;
3113 		TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3114 		rtcache_free(&net->ro);
3115 		/* free it */
3116 		net->ref_count = 0;
3117 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
3118 		sctppcbinfo.ipi_count_raddr--;
3119 	}
3120 	/*
3121 	 * The chunk lists and such SHOULD be empty but we check them
3122 	 * just in case.
3123 	 */
3124 	/* anything on the wheel needs to be removed */
3125 	while (!TAILQ_EMPTY(&asoc->out_wheel)) {
3126 		struct sctp_stream_out *outs;
3127 		outs = TAILQ_FIRST(&asoc->out_wheel);
3128 		TAILQ_REMOVE(&asoc->out_wheel, outs, next_spoke);
3129 		/* now clean up any chunks here */
3130 		chk = TAILQ_FIRST(&outs->outqueue);
3131 		while (chk) {
3132 			TAILQ_REMOVE(&outs->outqueue, chk, sctp_next);
3133 			if (chk->data) {
3134 				sctp_m_freem(chk->data);
3135 				chk->data = NULL;
3136 			}
3137 			chk->whoTo = NULL;
3138 			chk->asoc = NULL;
3139 			/* Free the chunk */
3140 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3141 			sctppcbinfo.ipi_count_chunk--;
3142 			sctppcbinfo.ipi_gencnt_chunk++;
3143 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3144 				panic("Chunk count is negative");
3145 			}
3146 			chk = TAILQ_FIRST(&outs->outqueue);
3147 		}
3148 		outs = TAILQ_FIRST(&asoc->out_wheel);
3149 	}
3150 
3151 	if (asoc->pending_reply) {
3152 		free(asoc->pending_reply, M_PCB);
3153 		asoc->pending_reply = NULL;
3154 	}
3155 	chk = TAILQ_FIRST(&asoc->pending_reply_queue);
3156 	while (chk) {
3157 		TAILQ_REMOVE(&asoc->pending_reply_queue, chk, sctp_next);
3158 		if (chk->data) {
3159 			sctp_m_freem(chk->data);
3160 			chk->data = NULL;
3161 		}
3162 		chk->whoTo = NULL;
3163 		chk->asoc = NULL;
3164 		/* Free the chunk */
3165 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3166 		sctppcbinfo.ipi_count_chunk--;
3167 		sctppcbinfo.ipi_gencnt_chunk++;
3168 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3169 			panic("Chunk count is negative");
3170 		}
3171 		chk = TAILQ_FIRST(&asoc->pending_reply_queue);
3172 	}
3173 	/* pending send queue SHOULD be empty */
3174 	if (!TAILQ_EMPTY(&asoc->send_queue)) {
3175 		chk = TAILQ_FIRST(&asoc->send_queue);
3176 		while (chk) {
3177 			TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3178 			if (chk->data) {
3179 				sctp_m_freem(chk->data);
3180 				chk->data = NULL;
3181 			}
3182 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3183 			sctppcbinfo.ipi_count_chunk--;
3184 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3185 				panic("Chunk count is negative");
3186 			}
3187 			sctppcbinfo.ipi_gencnt_chunk++;
3188 			chk = TAILQ_FIRST(&asoc->send_queue);
3189 		}
3190 	}
3191 	/* sent queue SHOULD be empty */
3192 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3193 		chk = TAILQ_FIRST(&asoc->sent_queue);
3194 		while (chk) {
3195 			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3196 			if (chk->data) {
3197 				sctp_m_freem(chk->data);
3198 				chk->data = NULL;
3199 			}
3200 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3201 			sctppcbinfo.ipi_count_chunk--;
3202 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3203 				panic("Chunk count is negative");
3204 			}
3205 			sctppcbinfo.ipi_gencnt_chunk++;
3206 			chk = TAILQ_FIRST(&asoc->sent_queue);
3207 		}
3208 	}
3209 	/* control queue MAY not be empty */
3210 	if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3211 		chk = TAILQ_FIRST(&asoc->control_send_queue);
3212 		while (chk) {
3213 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3214 			if (chk->data) {
3215 				sctp_m_freem(chk->data);
3216 				chk->data = NULL;
3217 			}
3218 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3219 			sctppcbinfo.ipi_count_chunk--;
3220 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3221 				panic("Chunk count is negative");
3222 			}
3223 			sctppcbinfo.ipi_gencnt_chunk++;
3224 			chk = TAILQ_FIRST(&asoc->control_send_queue);
3225 		}
3226 	}
3227 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3228 		chk = TAILQ_FIRST(&asoc->reasmqueue);
3229 		while (chk) {
3230 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3231 			if (chk->data) {
3232 				sctp_m_freem(chk->data);
3233 				chk->data = NULL;
3234 			}
3235 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3236 			sctppcbinfo.ipi_count_chunk--;
3237 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3238 				panic("Chunk count is negative");
3239 			}
3240 			sctppcbinfo.ipi_gencnt_chunk++;
3241 			chk = TAILQ_FIRST(&asoc->reasmqueue);
3242 		}
3243 	}
3244 	if (!TAILQ_EMPTY(&asoc->delivery_queue)) {
3245 		chk = TAILQ_FIRST(&asoc->delivery_queue);
3246 		while (chk) {
3247 			TAILQ_REMOVE(&asoc->delivery_queue, chk, sctp_next);
3248 			if (chk->data) {
3249 				sctp_m_freem(chk->data);
3250 				chk->data = NULL;
3251 			}
3252 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3253 			sctppcbinfo.ipi_count_chunk--;
3254 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3255 				panic("Chunk count is negative");
3256 			}
3257 			sctppcbinfo.ipi_gencnt_chunk++;
3258 			chk = TAILQ_FIRST(&asoc->delivery_queue);
3259 		}
3260 	}
3261 	if (asoc->mapping_array) {
3262 		free(asoc->mapping_array, M_PCB);
3263 		asoc->mapping_array = NULL;
3264 	}
3265 
3266 	/* the stream outs */
3267 	if (asoc->strmout) {
3268 		free(asoc->strmout, M_PCB);
3269 		asoc->strmout = NULL;
3270 	}
3271 	asoc->streamoutcnt = 0;
3272 	if (asoc->strmin) {
3273 		int i;
3274 		for (i = 0; i < asoc->streamincnt; i++) {
3275 			if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3276 				/* We have somethings on the streamin queue */
3277 				chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3278 				while (chk) {
3279 					TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3280 					    chk, sctp_next);
3281 					if (chk->data) {
3282 						sctp_m_freem(chk->data);
3283 						chk->data = NULL;
3284 					}
3285 					SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk,
3286 					    chk);
3287 					sctppcbinfo.ipi_count_chunk--;
3288 					if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3289 						panic("Chunk count is negative");
3290 					}
3291 					sctppcbinfo.ipi_gencnt_chunk++;
3292 					chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3293 				}
3294 			}
3295 		}
3296 		free(asoc->strmin, M_PCB);
3297 		asoc->strmin = NULL;
3298 	}
3299 	asoc->streamincnt = 0;
3300 	/* local addresses, if any */
3301 	while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
3302 		laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
3303 		LIST_REMOVE(laddr, sctp_nxt_addr);
3304 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3305 		sctppcbinfo.ipi_count_laddr--;
3306 	}
3307 	/* pending asconf (address) parameters */
3308 	while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3309 		aparam = TAILQ_FIRST(&asoc->asconf_queue);
3310 		TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3311 		free(aparam, M_PCB);
3312 	}
3313 	if (asoc->last_asconf_ack_sent != NULL) {
3314 		sctp_m_freem(asoc->last_asconf_ack_sent);
3315 		asoc->last_asconf_ack_sent = NULL;
3316 	}
3317 	/* Insert new items here :> */
3318 
3319 	/* Get rid of LOCK */
3320 	SCTP_TCB_LOCK_DESTROY(stcb);
3321 
3322 	/* now clean up the tasoc itself */
3323 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3324 	sctppcbinfo.ipi_count_asoc--;
3325 	if ((inp->sctp_socket->so_snd.sb_cc) ||
3326 	    (inp->sctp_socket->so_snd.sb_mbcnt)) {
3327 		/* This will happen when a abort is done */
3328 		inp->sctp_socket->so_snd.sb_cc = 0;
3329 		inp->sctp_socket->so_snd.sb_mbcnt = 0;
3330 	}
3331 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3332 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
3333 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3334 				/*
3335 				 * For the base fd, that is NOT in TCP pool we
3336 				 * turn off the connected flag. This allows
3337 				 * non-listening endpoints to connect/shutdown/
3338 				 * connect.
3339 				 */
3340 				inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3341 				soisdisconnected(inp->sctp_socket);
3342 			}
3343 			/*
3344 			 * For those that are in the TCP pool we just leave
3345 			 * so it cannot be used. When they close the fd we
3346 			 * will free it all.
3347 			 */
3348 		}
3349 	}
3350 	if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3351 		sctp_inpcb_free(inp, 0);
3352 	}
3353 	splx(s);
3354 }
3355 
3356 
3357 /*
3358  * determine if a destination is "reachable" based upon the addresses
3359  * bound to the current endpoint (e.g. only v4 or v6 currently bound)
3360  */
3361 /*
3362  * FIX: if we allow assoc-level bindx(), then this needs to be fixed
3363  * to use assoc level v4/v6 flags, as the assoc *may* not have the
3364  * same address types bound as its endpoint
3365  */
3366 int
sctp_destination_is_reachable(struct sctp_tcb * stcb,const struct sockaddr * destaddr)3367 sctp_destination_is_reachable(struct sctp_tcb *stcb, const struct sockaddr *destaddr)
3368 {
3369 	struct sctp_inpcb *inp;
3370 	int answer;
3371 
3372 	/* No locks here, the TCB, in all cases is already
3373 	 * locked and an assoc is up. There is either a
3374 	 * INP lock by the caller applied (in asconf case when
3375 	 * deleting an address) or NOT in the HB case, however
3376 	 * if HB then the INP increment is up and the INP
3377 	 * will not be removed (on top of the fact that
3378 	 * we have a TCB lock). So we only want to
3379 	 * read the sctp_flags, which is either bound-all
3380 	 * or not.. no protection needed since once an
3381 	 * assoc is up you can't be changing your binding.
3382 	 */
3383 	inp = stcb->sctp_ep;
3384 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3385 		/* if bound all, destination is not restricted */
3386 		/* RRS: Question during lock work: Is this
3387 		 * correct? If you are bound-all you still
3388 		 * might need to obey the V4--V6 flags???
3389 		 * IMO this bound-all stuff needs to be removed!
3390 		 */
3391 		return (1);
3392 	}
3393 	/* NOTE: all "scope" checks are done when local addresses are added */
3394 	if (destaddr->sa_family == AF_INET6) {
3395 		answer = inp->inp_vflag & INP_IPV6;
3396 	} else if (destaddr->sa_family == AF_INET) {
3397 		answer = inp->inp_vflag & INP_IPV4;
3398 	} else {
3399 		/* invalid family, so it's unreachable */
3400 		answer = 0;
3401 	}
3402 	return (answer);
3403 }
3404 
3405 /*
3406  * update the inp_vflags on an endpoint
3407  */
3408 static void
sctp_update_ep_vflag(struct sctp_inpcb * inp)3409 sctp_update_ep_vflag(struct sctp_inpcb *inp) {
3410 	struct sctp_laddr *laddr;
3411 
3412 	/* first clear the flag */
3413 	inp->inp_vflag = 0;
3414 
3415 	/* set the flag based on addresses on the ep list */
3416 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3417 		if (laddr->ifa == NULL) {
3418 #ifdef SCTP_DEBUG
3419 			if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3420 				printf("An ounce of prevention is worth a pound of cure\n");
3421 			}
3422 #endif /* SCTP_DEBUG */
3423 			continue;
3424 		}
3425 		if (laddr->ifa->ifa_addr) {
3426 			continue;
3427 		}
3428 		if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
3429 			inp->inp_vflag |= INP_IPV6;
3430 		} else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3431 			inp->inp_vflag |= INP_IPV4;
3432 		}
3433 	}
3434 }
3435 
3436 /*
3437  * Add the address to the endpoint local address list
3438  * There is nothing to be done if we are bound to all addresses
3439  */
3440 int
sctp_add_local_addr_ep(struct sctp_inpcb * inp,struct ifaddr * ifa)3441 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3442 {
3443 	struct sctp_laddr *laddr;
3444 	int fnd, error;
3445 	fnd = 0;
3446 
3447 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3448 		/* You are already bound to all. You have it already */
3449 		return (0);
3450 	}
3451 	if (ifa->ifa_addr->sa_family == AF_INET6) {
3452 		struct in6_ifaddr *ifa6;
3453 		ifa6 = (struct in6_ifaddr *)ifa;
3454 		if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3455 		    IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
3456 			/* Can't bind a non-existent addr. */
3457 			return (-1);
3458 	}
3459 	/* first, is it already present? */
3460 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3461 		if (laddr->ifa == ifa) {
3462 			fnd = 1;
3463 			break;
3464 		}
3465 	}
3466 
3467 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
3468 		/* Not bound to all */
3469 		error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
3470 		if (error != 0)
3471 			return (error);
3472 		inp->laddr_count++;
3473 		/* update inp_vflag flags */
3474 		if (ifa->ifa_addr->sa_family == AF_INET6) {
3475 			inp->inp_vflag |= INP_IPV6;
3476 		} else if (ifa->ifa_addr->sa_family == AF_INET) {
3477 			inp->inp_vflag |= INP_IPV4;
3478 		}
3479 	}
3480 	return (0);
3481 }
3482 
3483 
3484 /*
3485  * select a new (hopefully reachable) destination net
3486  * (should only be used when we deleted an ep addr that is the
3487  * only usable source address to reach the destination net)
3488  */
3489 static void
sctp_select_primary_destination(struct sctp_tcb * stcb)3490 sctp_select_primary_destination(struct sctp_tcb *stcb)
3491 {
3492 	struct sctp_nets *net;
3493 
3494 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3495 		/* for now, we'll just pick the first reachable one we find */
3496 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
3497 			continue;
3498 		if (sctp_destination_is_reachable(stcb,
3499 			rtcache_getdst(&net->ro))) {
3500 			/* found a reachable destination */
3501 			stcb->asoc.primary_destination = net;
3502 		}
3503 	}
3504 	/* I can't there from here! ...we're gonna die shortly... */
3505 }
3506 
3507 
3508 /*
3509  * Delete the address from the endpoint local address list
3510  * There is nothing to be done if we are bound to all addresses
3511  */
3512 int
sctp_del_local_addr_ep(struct sctp_inpcb * inp,struct ifaddr * ifa)3513 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3514 {
3515 	struct sctp_laddr *laddr;
3516 	int fnd;
3517 	fnd = 0;
3518 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3519 		/* You are already bound to all. You have it already */
3520 		return (EINVAL);
3521 	}
3522 
3523 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3524 		if (laddr->ifa == ifa) {
3525 			fnd = 1;
3526 			break;
3527 		}
3528 	}
3529 	if (fnd && (inp->laddr_count < 2)) {
3530 		/* can't delete unless there are at LEAST 2 addresses */
3531 		return (-1);
3532 	}
3533 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
3534 		/*
3535 		 * clean up any use of this address
3536 		 * go through our associations and clear any
3537 		 *  last_used_address that match this one
3538 		 * for each assoc, see if a new primary_destination is needed
3539 		 */
3540 		struct sctp_tcb *stcb;
3541 
3542 		/* clean up "next_addr_touse" */
3543 		if (inp->next_addr_touse == laddr)
3544 			/* delete this address */
3545 			inp->next_addr_touse = NULL;
3546 
3547 		/* clean up "last_used_address" */
3548 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3549 			if (stcb->asoc.last_used_address == laddr)
3550 				/* delete this address */
3551 				stcb->asoc.last_used_address = NULL;
3552 		} /* for each tcb */
3553 
3554 		/* remove it from the ep list */
3555 		sctp_remove_laddr(laddr);
3556 		inp->laddr_count--;
3557 		/* update inp_vflag flags */
3558 		sctp_update_ep_vflag(inp);
3559 		/* select a new primary destination if needed */
3560 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3561 			/* presume caller (sctp_asconf.c) already owns INP lock */
3562 			SCTP_TCB_LOCK(stcb);
3563 			if (sctp_destination_is_reachable(stcb,
3564 			    rtcache_getdst(&stcb->asoc.primary_destination->ro)) == 0) {
3565 				sctp_select_primary_destination(stcb);
3566 			}
3567 			SCTP_TCB_UNLOCK(stcb);
3568 		} /* for each tcb */
3569 	}
3570 	return (0);
3571 }
3572 
3573 /*
3574  * Add the addr to the TCB local address list
3575  * For the BOUNDALL or dynamic case, this is a "pending" address list
3576  * (eg. addresses waiting for an ASCONF-ACK response)
3577  * For the subset binding, static case, this is a "valid" address list
3578  */
3579 int
sctp_add_local_addr_assoc(struct sctp_tcb * stcb,struct ifaddr * ifa)3580 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3581 {
3582 	struct sctp_laddr *laddr;
3583 	int error;
3584 
3585 	/* Assumes TCP is locked.. and possiblye
3586 	 * the INP. May need to confirm/fix that if
3587 	 * we need it and is not the case.
3588 	 */
3589 	if (ifa->ifa_addr->sa_family == AF_INET6) {
3590 		struct in6_ifaddr *ifa6;
3591 		ifa6 = (struct in6_ifaddr *)ifa;
3592 		if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3593 		    /* IN6_IFF_DEPRECATED | */
3594 		    IN6_IFF_ANYCAST |
3595 		    IN6_IFF_NOTREADY))
3596 			/* Can't bind a non-existent addr. */
3597 			return (-1);
3598 	}
3599 	/* does the address already exist? */
3600 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3601 		if (laddr->ifa == ifa) {
3602 			return (-1);
3603 		}
3604 	}
3605 
3606 	/* add to the list */
3607 	error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
3608 	if (error != 0)
3609 		return (error);
3610 	return (0);
3611 }
3612 
3613 /*
3614  * insert an laddr entry with the given ifa for the desired list
3615  */
3616 int
sctp_insert_laddr(struct sctpladdr * list,struct ifaddr * ifa)3617 sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa) {
3618 	struct sctp_laddr *laddr;
3619 	int s;
3620 
3621 	s = splsoftnet();
3622 
3623 	laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
3624 	if (laddr == NULL) {
3625 		/* out of memory? */
3626 		splx(s);
3627 		return (EINVAL);
3628 	}
3629 	sctppcbinfo.ipi_count_laddr++;
3630 	sctppcbinfo.ipi_gencnt_laddr++;
3631 	memset(laddr, 0, sizeof(*laddr));
3632 	laddr->ifa = ifa;
3633 	/* insert it */
3634 	LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3635 
3636 	splx(s);
3637 	return (0);
3638 }
3639 
3640 /*
3641  * Remove an laddr entry from the local address list (on an assoc)
3642  */
3643 void
sctp_remove_laddr(struct sctp_laddr * laddr)3644 sctp_remove_laddr(struct sctp_laddr *laddr)
3645 {
3646 	int s;
3647 	s = splsoftnet();
3648 	/* remove from the list */
3649 	LIST_REMOVE(laddr, sctp_nxt_addr);
3650 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3651 	sctppcbinfo.ipi_count_laddr--;
3652 	sctppcbinfo.ipi_gencnt_laddr++;
3653 
3654 	splx(s);
3655 }
3656 
3657 /*
3658  * Remove an address from the TCB local address list
3659  */
3660 int
sctp_del_local_addr_assoc(struct sctp_tcb * stcb,struct ifaddr * ifa)3661 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3662 {
3663 	struct sctp_inpcb *inp;
3664 	struct sctp_laddr *laddr;
3665 
3666 	/* This is called by asconf work. It is assumed that
3667 	 * a) The TCB is locked
3668 	 * and
3669 	 * b) The INP is locked.
3670 	 * This is true in as much as I can trace through
3671 	 * the entry asconf code where I did these locks.
3672 	 * Again, the ASCONF code is a bit different in
3673 	 * that it does lock the INP during its work often
3674 	 * times. This must be since we don't want other
3675 	 * proc's looking up things while what they are
3676 	 * looking up is changing :-D
3677 	 */
3678 
3679 	inp = stcb->sctp_ep;
3680 	/* if subset bound and don't allow ASCONF's, can't delete last */
3681 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
3682 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
3683 		if (stcb->asoc.numnets < 2) {
3684 			/* can't delete last address */
3685 			return (-1);
3686 		}
3687 	}
3688 
3689 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3690 		/* remove the address if it exists */
3691 		if (laddr->ifa == NULL)
3692 			continue;
3693 		if (laddr->ifa == ifa) {
3694 			sctp_remove_laddr(laddr);
3695 			return (0);
3696 		}
3697 	}
3698 
3699 	/* address not found! */
3700 	return (-1);
3701 }
3702 
3703 /*
3704  * Remove an address from the TCB local address list
3705  * lookup using a sockaddr addr
3706  */
3707 int
sctp_del_local_addr_assoc_sa(struct sctp_tcb * stcb,struct sockaddr * sa)3708 sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
3709 {
3710 	struct sctp_inpcb *inp;
3711 	struct sctp_laddr *laddr;
3712 	struct sockaddr *l_sa;
3713 
3714         /*
3715          * This function I find does not seem to have a caller.
3716 	 * As such we NEED TO DELETE this code. If we do
3717 	 * find a caller, the caller MUST have locked the TCB
3718 	 * at the least and probably the INP as well.
3719          */
3720 	inp = stcb->sctp_ep;
3721 	/* if subset bound and don't allow ASCONF's, can't delete last */
3722 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
3723 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
3724 		if (stcb->asoc.numnets < 2) {
3725 			/* can't delete last address */
3726 			return (-1);
3727 		}
3728 	}
3729 
3730 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3731 		/* make sure the address exists */
3732 		if (laddr->ifa == NULL)
3733 			continue;
3734 		if (laddr->ifa->ifa_addr == NULL)
3735 			continue;
3736 
3737 		l_sa = laddr->ifa->ifa_addr;
3738 		if (l_sa->sa_family == AF_INET6) {
3739 			/* IPv6 address */
3740 			struct sockaddr_in6 *sin1, *sin2;
3741 			sin1 = (struct sockaddr_in6 *)l_sa;
3742 			sin2 = (struct sockaddr_in6 *)sa;
3743 			if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
3744 			    sizeof(struct in6_addr)) == 0) {
3745 				/* matched */
3746 				sctp_remove_laddr(laddr);
3747 				return (0);
3748 			}
3749 		} else if (l_sa->sa_family == AF_INET) {
3750 			/* IPv4 address */
3751 			struct sockaddr_in *sin1, *sin2;
3752 			sin1 = (struct sockaddr_in *)l_sa;
3753 			sin2 = (struct sockaddr_in *)sa;
3754 			if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
3755 				/* matched */
3756 				sctp_remove_laddr(laddr);
3757 				return (0);
3758 			}
3759 		} else {
3760 			/* invalid family */
3761 			return (-1);
3762 		}
3763 	} /* end foreach */
3764 	/* address not found! */
3765 	return (-1);
3766 }
3767 
3768 static char sctp_pcb_initialized = 0;
3769 
3770 #if defined(__FreeBSD__) || defined(__APPLE__)
3771 /* sysctl */
3772 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
3773 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
3774 
3775 #endif /* FreeBSD || APPLE */
3776 
3777 #ifndef SCTP_TCBHASHSIZE
3778 #define SCTP_TCBHASHSIZE 1024
3779 #endif
3780 
3781 #ifndef SCTP_CHUNKQUEUE_SCALE
3782 #define SCTP_CHUNKQUEUE_SCALE 10
3783 #endif
3784 
3785 void
sctp_pcb_init(void)3786 sctp_pcb_init(void)
3787 {
3788 	/*
3789 	 * SCTP initialization for the PCB structures
3790 	 * should be called by the sctp_init() function.
3791 	 */
3792 	int i;
3793 	int hashtblsize = SCTP_TCBHASHSIZE;
3794 
3795 #if defined(__FreeBSD__) || defined(__APPLE__)
3796 	int sctp_chunkscale = SCTP_CHUNKQUEUE_SCALE;
3797 #endif
3798 
3799 	if (sctp_pcb_initialized != 0) {
3800 		/* error I was called twice */
3801 		return;
3802 	}
3803 	sctp_pcb_initialized = 1;
3804 
3805 	/* Init all peg counts */
3806 	for (i = 0; i < SCTP_NUMBER_OF_PEGS; i++) {
3807 		sctp_pegs[i] = 0;
3808 	}
3809 
3810 	/* init the empty list of (All) Endpoints */
3811 	LIST_INIT(&sctppcbinfo.listhead);
3812 
3813 	/* init the iterator head */
3814 	LIST_INIT(&sctppcbinfo.iteratorhead);
3815 
3816 	/* init the hash table of endpoints */
3817 #if defined(__FreeBSD__)
3818 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
3819 	TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &hashtblsize);
3820 	TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
3821 	TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
3822 #else
3823 	TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
3824 	    hashtblsize);
3825 	TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
3826 	    sctp_pcbtblsize);
3827 	TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
3828 	    sctp_chunkscale);
3829 #endif
3830 #endif
3831 
3832 	sctppcbinfo.sctp_asochash = hashinit((hashtblsize * 31), HASH_LIST,
3833 			M_WAITOK, &sctppcbinfo.hashasocmark);
3834 
3835 	sctppcbinfo.sctp_ephash = hashinit(hashtblsize, HASH_LIST,
3836 			M_WAITOK, &sctppcbinfo.hashmark);
3837 
3838 	sctppcbinfo.sctp_tcpephash = hashinit(hashtblsize, HASH_LIST,
3839 			M_WAITOK, &sctppcbinfo.hashtcpmark);
3840 
3841 	sctppcbinfo.hashtblsize = hashtblsize;
3842 
3843 	/* init the zones */
3844 	/*
3845 	 * FIX ME: Should check for NULL returns, but if it does fail we
3846 	 * are doomed to panic anyways... add later maybe.
3847 	 */
3848 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
3849 	    sizeof(struct sctp_inpcb), maxsockets);
3850 
3851 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
3852 	    sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
3853 
3854 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
3855 	    sizeof(struct sctp_laddr),
3856 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
3857 
3858 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
3859 	    sizeof(struct sctp_nets),
3860 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address));
3861 
3862 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
3863 	    sizeof(struct sctp_tmit_chunk),
3864 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address *
3865 	    sctp_chunkscale));
3866 
3867 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_sockq, "sctp_sockq",
3868 	    sizeof(struct sctp_socket_q_list),
3869 	    (sctp_max_number_of_assoc * sctp_scale_up_for_address *
3870 	    sctp_chunkscale));
3871 
3872 	SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_hash, "sctp_hash",
3873 		       sizeof(void *) * sctp_pcbtblsize, maxsockets);
3874 
3875         /* Master Lock INIT for info structure */
3876 	SCTP_INP_INFO_LOCK_INIT();
3877 	SCTP_ITERATOR_LOCK_INIT();
3878 	/* not sure if we need all the counts */
3879 	sctppcbinfo.ipi_count_ep = 0;
3880 	sctppcbinfo.ipi_gencnt_ep = 0;
3881 	/* assoc/tcb zone info */
3882 	sctppcbinfo.ipi_count_asoc = 0;
3883 	sctppcbinfo.ipi_gencnt_asoc = 0;
3884 	/* local addrlist zone info */
3885 	sctppcbinfo.ipi_count_laddr = 0;
3886 	sctppcbinfo.ipi_gencnt_laddr = 0;
3887 	/* remote addrlist zone info */
3888 	sctppcbinfo.ipi_count_raddr = 0;
3889 	sctppcbinfo.ipi_gencnt_raddr = 0;
3890 	/* chunk info */
3891 	sctppcbinfo.ipi_count_chunk = 0;
3892 	sctppcbinfo.ipi_gencnt_chunk = 0;
3893 
3894 	/* socket queue zone info */
3895 	sctppcbinfo.ipi_count_sockq = 0;
3896 	sctppcbinfo.ipi_gencnt_sockq = 0;
3897 
3898 	/* mbuf tracker */
3899 	sctppcbinfo.mbuf_track = 0;
3900 	/* port stuff */
3901 	sctppcbinfo.lastlow = anonportmin;
3902 
3903 	/* Init the TIMEWAIT list */
3904 	for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
3905 		LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
3906 	}
3907 
3908 #if defined(_SCTP_NEEDS_CALLOUT_) && !defined(__APPLE__)
3909 	TAILQ_INIT(&sctppcbinfo.callqueue);
3910 #endif
3911 
3912 }
3913 
3914 int
sctp_load_addresses_from_init(struct sctp_tcb * stcb,struct mbuf * m,int iphlen,int offset,int limit,struct sctphdr * sh,struct sockaddr * altsa)3915 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
3916     int iphlen, int offset, int limit, struct sctphdr *sh,
3917     struct sockaddr *altsa)
3918 {
3919 	/*
3920 	 * grub through the INIT pulling addresses and
3921 	 * loading them to the nets structure in the asoc.
3922 	 * The from address in the mbuf should also be loaded
3923 	 * (if it is not already). This routine can be called
3924 	 * with either INIT or INIT-ACK's as long as the
3925 	 * m points to the IP packet and the offset points
3926 	 * to the beginning of the parameters.
3927 	 */
3928 	struct sctp_inpcb *inp, *l_inp;
3929 	struct sctp_nets *net, *net_tmp;
3930 	struct ip *iph;
3931 	struct sctp_paramhdr *phdr, parm_buf;
3932 	struct sctp_tcb *stcb_tmp;
3933 	u_int16_t ptype, plen;
3934 	struct sockaddr *sa;
3935 	struct sockaddr_storage dest_store;
3936 	struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
3937 	struct sockaddr_in sin;
3938 	struct sockaddr_in6 sin6;
3939 
3940 	/* First get the destination address setup too. */
3941 	memset(&sin, 0, sizeof(sin));
3942 	memset(&sin6, 0, sizeof(sin6));
3943 
3944 	sin.sin_family = AF_INET;
3945 	sin.sin_len = sizeof(sin);
3946 	sin.sin_port = stcb->rport;
3947 
3948 	sin6.sin6_family = AF_INET6;
3949 	sin6.sin6_len = sizeof(struct sockaddr_in6);
3950 	sin6.sin6_port = stcb->rport;
3951 	if (altsa == NULL) {
3952 		iph = mtod(m, struct ip *);
3953 		if (iph->ip_v == IPVERSION) {
3954 			/* its IPv4 */
3955 			struct sockaddr_in *sin_2;
3956 			sin_2 = (struct sockaddr_in *)(local_sa);
3957 			memset(sin_2, 0, sizeof(sin));
3958 			sin_2->sin_family = AF_INET;
3959 			sin_2->sin_len = sizeof(sin);
3960 			sin_2->sin_port = sh->dest_port;
3961 			sin_2->sin_addr.s_addr = iph->ip_dst.s_addr ;
3962 			sin.sin_addr = iph->ip_src;
3963 			sa = (struct sockaddr *)&sin;
3964 		} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3965 			/* its IPv6 */
3966 			struct ip6_hdr *ip6;
3967 			struct sockaddr_in6 *sin6_2;
3968 
3969 			ip6 = mtod(m, struct ip6_hdr *);
3970 			sin6_2 = (struct sockaddr_in6 *)(local_sa);
3971 			memset(sin6_2, 0, sizeof(sin6));
3972 			sin6_2->sin6_family = AF_INET6;
3973 			sin6_2->sin6_len = sizeof(struct sockaddr_in6);
3974 			sin6_2->sin6_port = sh->dest_port;
3975 			sin6.sin6_addr = ip6->ip6_src;
3976 			sa = (struct sockaddr *)&sin6;
3977 		} else {
3978 			sa = NULL;
3979 		}
3980 	} else {
3981 		/*
3982 		 * For cookies we use the src address NOT from the packet
3983 		 * but from the original INIT
3984 		 */
3985 		sa = altsa;
3986 	}
3987 	/* Turn off ECN until we get through all params */
3988 	stcb->asoc.ecn_allowed = 0;
3989 
3990 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3991 		/* mark all addresses that we have currently on the list */
3992 		net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
3993 	}
3994 	/* does the source address already exist? if so skip it */
3995 	l_inp = inp = stcb->sctp_ep;
3996 	stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
3997 	if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
3998 		/* we must add the source address */
3999 		/* no scope set here since we have a tcb already. */
4000 		if ((sa->sa_family == AF_INET) &&
4001 		    (stcb->asoc.ipv4_addr_legal)) {
4002 			if (sctp_add_remote_addr(stcb, sa, 0, 2)) {
4003 				return (-1);
4004 			}
4005 		} else if ((sa->sa_family == AF_INET6) &&
4006 		    (stcb->asoc.ipv6_addr_legal)) {
4007 			if (sctp_add_remote_addr(stcb, sa, 0, 3)) {
4008 				return (-1);
4009 			}
4010 		}
4011 	} else {
4012 		if (net_tmp != NULL && stcb_tmp == stcb) {
4013 			net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4014 		} else if (stcb_tmp != stcb) {
4015 			/* It belongs to another association? */
4016 			return (-1);
4017 		}
4018 	}
4019 	/* since a unlock occurred we must check the
4020 	 * TCB's state and the pcb's gone flags.
4021 	 */
4022 	if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4023 		/* the user freed the ep */
4024 		return (-1);
4025 	}
4026 	if (stcb->asoc.state == 0) {
4027 		/* the assoc was freed? */
4028 		return (-1);
4029 	}
4030 
4031 	/* now we must go through each of the params. */
4032 	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4033 	while (phdr) {
4034 		ptype = ntohs(phdr->param_type);
4035 		plen = ntohs(phdr->param_length);
4036 		/*printf("ptype => %d, plen => %d\n", ptype, plen);*/
4037 		if (offset + plen > limit) {
4038 			break;
4039 		}
4040 		if (plen == 0) {
4041 			break;
4042 		}
4043 		if ((ptype == SCTP_IPV4_ADDRESS) &&
4044 		    (stcb->asoc.ipv4_addr_legal)) {
4045 			struct sctp_ipv4addr_param *p4, p4_buf;
4046 			/* ok get the v4 address and check/add */
4047 			phdr = sctp_get_next_param(m, offset,
4048 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4049 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
4050 			    phdr == NULL) {
4051 				return (-1);
4052 			}
4053 			p4 = (struct sctp_ipv4addr_param *)phdr;
4054 			sin.sin_addr.s_addr = p4->addr;
4055 			sa = (struct sockaddr *)&sin;
4056 			inp = stcb->sctp_ep;
4057 			stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4058 			    local_sa, stcb);
4059 
4060 			if ((stcb_tmp== NULL && inp == stcb->sctp_ep) ||
4061 			    inp == NULL) {
4062 				/* we must add the source address */
4063 				/* no scope set since we have a tcb already */
4064 
4065 				/* we must validate the state again here */
4066 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4067 					/* the user freed the ep */
4068 					return (-1);
4069 				}
4070 				if (stcb->asoc.state == 0) {
4071 					/* the assoc was freed? */
4072 					return (-1);
4073 				}
4074 				if (sctp_add_remote_addr(stcb, sa, 0, 4)) {
4075 					return (-1);
4076 				}
4077 			} else if (stcb_tmp == stcb) {
4078 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4079 					/* the user freed the ep */
4080 					return (-1);
4081 				}
4082 				if (stcb->asoc.state == 0) {
4083 					/* the assoc was freed? */
4084 					return (-1);
4085 				}
4086 				if (net != NULL) {
4087 					/* clear flag */
4088 					net->dest_state &=
4089 					    ~SCTP_ADDR_NOT_IN_ASSOC;
4090 				}
4091 			} else {
4092 				/* strange, address is in another assoc?
4093 				 * straighten out locks.
4094 				 */
4095 				SCTP_TCB_UNLOCK(stcb_tmp);
4096 				SCTP_INP_RLOCK(inp);
4097 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4098 					/* the user freed the ep */
4099 					SCTP_INP_RUNLOCK(l_inp);
4100 					return (-1);
4101 				}
4102 				if (stcb->asoc.state == 0) {
4103 					/* the assoc was freed? */
4104 					SCTP_INP_RUNLOCK(l_inp);
4105 					return (-1);
4106 				}
4107 				SCTP_TCB_LOCK(stcb);
4108 				SCTP_INP_RUNLOCK(stcb->sctp_ep);
4109 				return (-1);
4110 			}
4111 		} else if ((ptype == SCTP_IPV6_ADDRESS) &&
4112 		    (stcb->asoc.ipv6_addr_legal)) {
4113 			/* ok get the v6 address and check/add */
4114 			struct sctp_ipv6addr_param *p6, p6_buf;
4115 			phdr = sctp_get_next_param(m, offset,
4116 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4117 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
4118 			    phdr == NULL) {
4119 				return (-1);
4120 			}
4121 			p6 = (struct sctp_ipv6addr_param *)phdr;
4122 			memcpy((void *)&sin6.sin6_addr, p6->addr,
4123 			    sizeof(p6->addr));
4124 			sa = (struct sockaddr *)&sin6;
4125 			inp = stcb->sctp_ep;
4126 			stcb_tmp= sctp_findassociation_ep_addr(&inp, sa, &net,
4127 			    local_sa, stcb);
4128 			if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4129 			    inp == NULL)) {
4130 				/* we must validate the state again here */
4131 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4132 					/* the user freed the ep */
4133 					return (-1);
4134 				}
4135 				if (stcb->asoc.state == 0) {
4136 					/* the assoc was freed? */
4137 					return (-1);
4138 				}
4139 				/* we must add the address, no scope set */
4140 				if (sctp_add_remote_addr(stcb, sa, 0, 5)) {
4141 					return (-1);
4142 				}
4143 			} else if (stcb_tmp == stcb) {
4144 				/* we must validate the state again here */
4145 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4146 					/* the user freed the ep */
4147 					return (-1);
4148 				}
4149 				if (stcb->asoc.state == 0) {
4150 					/* the assoc was freed? */
4151 					return (-1);
4152 				}
4153 				if (net != NULL) {
4154 					/* clear flag */
4155 					net->dest_state &=
4156 					    ~SCTP_ADDR_NOT_IN_ASSOC;
4157 				}
4158 			} else {
4159 				/* strange, address is in another assoc?
4160 				 * straighten out locks.
4161 				 */
4162 				SCTP_TCB_UNLOCK(stcb_tmp);
4163 				SCTP_INP_RLOCK(l_inp);
4164 				/* we must validate the state again here */
4165 				if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4166 					/* the user freed the ep */
4167 					SCTP_INP_RUNLOCK(l_inp);
4168 					return (-1);
4169 				}
4170 				if (stcb->asoc.state == 0) {
4171 					/* the assoc was freed? */
4172 					SCTP_INP_RUNLOCK(l_inp);
4173 					return (-1);
4174 				}
4175 				SCTP_TCB_LOCK(stcb);
4176 				SCTP_INP_RUNLOCK(l_inp);
4177 				return (-1);
4178 			}
4179 		} else if (ptype == SCTP_ECN_CAPABLE) {
4180 			stcb->asoc.ecn_allowed = 1;
4181 		} else if (ptype == SCTP_ULP_ADAPTION) {
4182 			if (stcb->asoc.state != SCTP_STATE_OPEN) {
4183 				struct sctp_adaption_layer_indication ai, *aip;
4184 
4185 				phdr = sctp_get_next_param(m, offset,
4186 							   (struct sctp_paramhdr *)&ai, sizeof(ai));
4187 				aip = (struct sctp_adaption_layer_indication *)phdr;
4188 				sctp_ulp_notify(SCTP_NOTIFY_ADAPTION_INDICATION,
4189 						stcb, ntohl(aip->indication), NULL);
4190 			}
4191 		} else if (ptype == SCTP_SET_PRIM_ADDR) {
4192 			struct sctp_asconf_addr_param lstore, *fee;
4193 			struct sctp_asconf_addrv4_param *fii;
4194 			int lptype;
4195 			struct sockaddr *lsa = NULL;
4196 
4197 			stcb->asoc.peer_supports_asconf = 1;
4198 			stcb->asoc.peer_supports_asconf_setprim = 1;
4199 			if (plen > sizeof(lstore)) {
4200 				return (-1);
4201 			}
4202 			phdr = sctp_get_next_param(m, offset,
4203     			    (struct sctp_paramhdr *)&lstore, plen);
4204 			if (phdr == NULL) {
4205 				return (-1);
4206 			}
4207 
4208 			fee  = (struct sctp_asconf_addr_param *)phdr;
4209 			lptype = ntohs(fee->addrp.ph.param_type);
4210 			if (lptype == SCTP_IPV4_ADDRESS) {
4211 				if (plen !=
4212 				    sizeof(struct sctp_asconf_addrv4_param)) {
4213 					printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4214 				           (int)sizeof(struct sctp_asconf_addrv4_param),
4215 				           plen);
4216 				} else {
4217 					fii = (struct sctp_asconf_addrv4_param *)fee;
4218 					sin.sin_addr.s_addr = fii->addrp.addr;
4219 					lsa = (struct sockaddr *)&sin;
4220 				}
4221 			} else if (lptype == SCTP_IPV6_ADDRESS) {
4222 				if (plen !=
4223 				    sizeof(struct sctp_asconf_addr_param)) {
4224 					printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4225 				           (int)sizeof(struct sctp_asconf_addr_param),
4226 				           plen);
4227 				} else {
4228 					memcpy(sin6.sin6_addr.s6_addr,
4229 					    fee->addrp.addr,
4230 					    sizeof(fee->addrp.addr));
4231 					lsa = (struct sockaddr *)&sin6;
4232 				}
4233 			}
4234 			if (lsa) {
4235 				sctp_set_primary_addr(stcb, sa, NULL);
4236 			}
4237 
4238 		} else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4239 			/* Peer supports pr-sctp */
4240 			stcb->asoc.peer_supports_prsctp = 1;
4241 		} else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4242 			/* A supported extension chunk */
4243 			struct sctp_supported_chunk_types_param *pr_supported;
4244 			uint8_t local_store[128];
4245 			int num_ent, i;
4246 
4247 			phdr = sctp_get_next_param(m, offset,
4248     			    (struct sctp_paramhdr *)&local_store, plen);
4249 			if (phdr == NULL) {
4250 				return (-1);
4251 			}
4252 			stcb->asoc.peer_supports_asconf = 0;
4253 			stcb->asoc.peer_supports_asconf_setprim = 0;
4254 			stcb->asoc.peer_supports_prsctp = 0;
4255 			stcb->asoc.peer_supports_pktdrop = 0;
4256 			stcb->asoc.peer_supports_strreset = 0;
4257 			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4258 			num_ent = plen - sizeof(struct sctp_paramhdr);
4259 			for (i=0; i<num_ent; i++) {
4260 				switch (pr_supported->chunk_types[i]) {
4261 				case SCTP_ASCONF:
4262 					stcb->asoc.peer_supports_asconf = 1;
4263 					stcb->asoc.peer_supports_asconf_setprim = 1;
4264 					break;
4265 				case SCTP_ASCONF_ACK:
4266 					stcb->asoc.peer_supports_asconf = 1;
4267 					stcb->asoc.peer_supports_asconf_setprim = 1;
4268 					break;
4269 				case SCTP_FORWARD_CUM_TSN:
4270 					stcb->asoc.peer_supports_prsctp = 1;
4271 					break;
4272 				case SCTP_PACKET_DROPPED:
4273 					stcb->asoc.peer_supports_pktdrop = 1;
4274 					break;
4275 				case SCTP_STREAM_RESET:
4276 					stcb->asoc.peer_supports_strreset = 1;
4277 					break;
4278 				default:
4279 					/* one I have not learned yet */
4280 					break;
4281 
4282 				}
4283 			}
4284 		} else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4285 			/* Peer supports ECN-nonce */
4286 			stcb->asoc.peer_supports_ecn_nonce = 1;
4287 			stcb->asoc.ecn_nonce_allowed = 1;
4288 		} else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4289 			   (ptype == SCTP_STATE_COOKIE) ||
4290 			   (ptype == SCTP_UNRECOG_PARAM) ||
4291 			   (ptype == SCTP_COOKIE_PRESERVE) ||
4292 			   (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4293 			   (ptype == SCTP_ADD_IP_ADDRESS) ||
4294 			   (ptype == SCTP_DEL_IP_ADDRESS) ||
4295 			   (ptype == SCTP_ERROR_CAUSE_IND) ||
4296 			   (ptype == SCTP_SUCCESS_REPORT)) {
4297 			/* don't care */;
4298 		} else {
4299 			if ((ptype & 0x8000) == 0x0000) {
4300 				/* must stop processing the rest of
4301 				 * the param's. Any report bits were
4302 				 * handled with the call to sctp_arethere_unrecognized_parameters()
4303 				 * when the INIT or INIT-ACK was first seen.
4304 				 */
4305 				break;
4306 			}
4307 		}
4308 		offset += SCTP_SIZE32(plen);
4309 		if (offset >= limit) {
4310 			break;
4311 		}
4312 		phdr = sctp_get_next_param(m, offset, &parm_buf,
4313 		    sizeof(parm_buf));
4314 	}
4315 	/* Now check to see if we need to purge any addresses */
4316 	for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
4317 		net_tmp = TAILQ_NEXT(net, sctp_next);
4318 		if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
4319 		    SCTP_ADDR_NOT_IN_ASSOC) {
4320 			/* This address has been removed from the asoc */
4321 			/* remove and free it */
4322 			stcb->asoc.numnets--;
4323 			TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
4324 			sctp_free_remote_addr(net);
4325 			if (net == stcb->asoc.primary_destination) {
4326 				stcb->asoc.primary_destination = NULL;
4327 				sctp_select_primary_destination(stcb);
4328 			}
4329 		}
4330 	}
4331 	return (0);
4332 }
4333 
4334 int
sctp_set_primary_addr(struct sctp_tcb * stcb,struct sockaddr * sa,struct sctp_nets * net)4335 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
4336     struct sctp_nets *net)
4337 {
4338 	/* make sure the requested primary address exists in the assoc */
4339 	if (net == NULL && sa)
4340 		net = sctp_findnet(stcb, sa);
4341 
4342 	if (net == NULL) {
4343 		/* didn't find the requested primary address! */
4344 		return (-1);
4345 	} else {
4346 		/* set the primary address */
4347 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
4348 			/* Must be confirmed */
4349 			return (-1);
4350 		}
4351 		stcb->asoc.primary_destination = net;
4352 		net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
4353 		return (0);
4354 	}
4355 }
4356 
4357 
4358 int
sctp_is_vtag_good(struct sctp_inpcb * inp,u_int32_t tag,struct timeval * now)4359 sctp_is_vtag_good(struct sctp_inpcb *inp, u_int32_t tag, struct timeval *now)
4360 {
4361 	/*
4362 	 * This function serves two purposes. It will see if a TAG can be
4363 	 * re-used and return 1 for yes it is ok and 0 for don't use that
4364 	 * tag.
4365 	 * A secondary function it will do is purge out old tags that can
4366 	 * be removed.
4367 	 */
4368 	struct sctpasochead *head;
4369 	struct sctpvtaghead *chain;
4370 	struct sctp_tagblock *twait_block;
4371 	struct sctp_tcb *stcb;
4372 
4373 	int i;
4374 	SCTP_INP_INFO_WLOCK();
4375 	chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4376 	/* First is the vtag in use ? */
4377 
4378 	head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
4379 	    sctppcbinfo.hashasocmark)];
4380 	if (head == NULL) {
4381 		SCTP_INP_INFO_WUNLOCK();
4382 		return (0);
4383 	}
4384 	LIST_FOREACH(stcb, head, sctp_asocs) {
4385 		if (stcb->asoc.my_vtag == tag) {
4386 			/* We should remove this if and
4387 			 * return 0 always if we want vtags
4388 			 * unique across all endpoints. For
4389 			 * now within a endpoint is ok.
4390 			 */
4391  			if (inp == stcb->sctp_ep) {
4392 				/* bad tag, in use */
4393 				SCTP_INP_INFO_WUNLOCK();
4394 				return (0);
4395 			}
4396 		}
4397 	}
4398 	if (!LIST_EMPTY(chain)) {
4399 		/*
4400 		 * Block(s) are present, lets see if we have this tag in
4401 		 * the list
4402 		 */
4403 		LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4404 			for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4405 				if (twait_block->vtag_block[i].v_tag == 0) {
4406 					/* not used */
4407 					continue;
4408 				} else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
4409 				    now->tv_sec) {
4410 					/* Audit expires this guy */
4411 					twait_block->vtag_block[i].tv_sec_at_expire = 0;
4412 					twait_block->vtag_block[i].v_tag = 0;
4413 				} else if (twait_block->vtag_block[i].v_tag ==
4414 				    tag) {
4415 					/* Bad tag, sorry :< */
4416 					SCTP_INP_INFO_WUNLOCK();
4417 					return (0);
4418 				}
4419 			}
4420 		}
4421 	}
4422 	/* Not found, ok to use the tag */
4423 	SCTP_INP_INFO_WUNLOCK();
4424 	return (1);
4425 }
4426 
4427 
4428 /*
4429  * Delete the address from the endpoint local address list
4430  * Lookup using a sockaddr address (ie. not an ifaddr)
4431  */
4432 int
sctp_del_local_addr_ep_sa(struct sctp_inpcb * inp,struct sockaddr * sa)4433 sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
4434 {
4435 	struct sctp_laddr *laddr;
4436 	struct sockaddr *l_sa;
4437 	int found = 0;
4438 	/* Here is another function I cannot find a
4439 	 * caller for. As such we SHOULD delete it
4440 	 * if we have no users. If we find a user that
4441 	 * user MUST have the INP locked.
4442 	 *
4443 	 */
4444 
4445 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4446 		/* You are already bound to all. You have it already */
4447 		return (EINVAL);
4448 	}
4449 
4450 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4451 		/* make sure the address exists */
4452 		if (laddr->ifa == NULL)
4453 			continue;
4454 		if (laddr->ifa->ifa_addr == NULL)
4455 			continue;
4456 
4457 		l_sa = laddr->ifa->ifa_addr;
4458 		if (l_sa->sa_family == AF_INET6) {
4459 			/* IPv6 address */
4460 			struct sockaddr_in6 *sin1, *sin2;
4461 			sin1 = (struct sockaddr_in6 *)l_sa;
4462 			sin2 = (struct sockaddr_in6 *)sa;
4463 			if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4464 			    sizeof(struct in6_addr)) == 0) {
4465 				/* matched */
4466 				found = 1;
4467 				break;
4468 			}
4469 		} else if (l_sa->sa_family == AF_INET) {
4470 			/* IPv4 address */
4471 			struct sockaddr_in *sin1, *sin2;
4472 			sin1 = (struct sockaddr_in *)l_sa;
4473 			sin2 = (struct sockaddr_in *)sa;
4474 			if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4475 				/* matched */
4476 				found = 1;
4477 				break;
4478 			}
4479 		} else {
4480 			/* invalid family */
4481 			return (-1);
4482 		}
4483 	}
4484 
4485 	if (found && inp->laddr_count < 2) {
4486 		/* can't delete unless there are at LEAST 2 addresses */
4487 		return (-1);
4488 	}
4489 
4490 	if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4491 		/*
4492 		 * remove it from the ep list, this should NOT be
4493 		 * done until its really gone from the interface list and
4494 		 * we won't be receiving more of these. Probably right
4495 		 * away. If we do allow a removal of an address from
4496 		 * an association (sub-set bind) than this should NOT
4497 		 * be called until the all ASCONF come back from this
4498 		 * association.
4499 		 */
4500 		sctp_remove_laddr(laddr);
4501 		return (0);
4502 	} else {
4503 		return (-1);
4504 	}
4505 }
4506 
4507 static void
sctp_drain_mbufs(struct sctp_inpcb * inp,struct sctp_tcb * stcb)4508 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4509 {
4510 	/*
4511 	 * We must hunt this association for MBUF's past the cumack
4512 	 * (i.e. out of order data that we can renege on).
4513 	 */
4514 	struct sctp_association *asoc;
4515 	struct sctp_tmit_chunk *chk, *nchk;
4516 	u_int32_t cumulative_tsn_p1, tsn;
4517 	int cnt, strmat, gap;
4518 	/* We look for anything larger than the cum-ack + 1 */
4519 
4520 	asoc = &stcb->asoc;
4521 	cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
4522 	cnt = 0;
4523 	/* First look in the re-assembly queue */
4524 	chk = TAILQ_FIRST(&asoc->reasmqueue);
4525 	while (chk) {
4526 		/* Get the next one */
4527 		nchk = TAILQ_NEXT(chk, sctp_next);
4528 		if (compare_with_wrap(chk->rec.data.TSN_seq,
4529 		    cumulative_tsn_p1, MAX_TSN)) {
4530 			/* Yep it is above cum-ack */
4531 			cnt++;
4532 			tsn = chk->rec.data.TSN_seq;
4533 			if (tsn >= asoc->mapping_array_base_tsn) {
4534 				gap  = tsn - asoc->mapping_array_base_tsn;
4535 			} else {
4536 				gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
4537 				    tsn + 1;
4538 			}
4539 			asoc->size_on_reasm_queue -= chk->send_size;
4540 			asoc->cnt_on_reasm_queue--;
4541 			SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
4542 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4543 			if (chk->data) {
4544 				sctp_m_freem(chk->data);
4545 				chk->data = NULL;
4546 			}
4547 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4548 			sctppcbinfo.ipi_count_chunk--;
4549 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4550 				panic("Chunk count is negative");
4551 			}
4552 			sctppcbinfo.ipi_gencnt_chunk++;
4553 		}
4554 		chk = nchk;
4555 	}
4556 	/* Ok that was fun, now we will drain all the inbound streams? */
4557 	for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
4558 		chk = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
4559 		while (chk) {
4560 			nchk = TAILQ_NEXT(chk, sctp_next);
4561 			if (compare_with_wrap(chk->rec.data.TSN_seq,
4562 			    cumulative_tsn_p1, MAX_TSN)) {
4563 				/* Yep it is above cum-ack */
4564 				cnt++;
4565 				tsn = chk->rec.data.TSN_seq;
4566 				if (tsn >= asoc->mapping_array_base_tsn) {
4567 					gap = tsn -
4568 					    asoc->mapping_array_base_tsn;
4569 				} else {
4570 					gap = (MAX_TSN -
4571 					    asoc->mapping_array_base_tsn) +
4572 					    tsn + 1;
4573 				}
4574 				asoc->size_on_all_streams -= chk->send_size;
4575 				asoc->cnt_on_all_streams--;
4576 
4577 				SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
4578 				    gap);
4579 				TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
4580 				    chk, sctp_next);
4581 				if (chk->data) {
4582 					sctp_m_freem(chk->data);
4583 					chk->data = NULL;
4584 				}
4585 				SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4586 				sctppcbinfo.ipi_count_chunk--;
4587 				if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4588 					panic("Chunk count is negative");
4589 				}
4590 				sctppcbinfo.ipi_gencnt_chunk++;
4591 			}
4592 			chk = nchk;
4593 		}
4594 	}
4595 	/*
4596 	 * Question, should we go through the delivery queue?
4597 	 * The only reason things are on here is the app not reading OR a
4598 	 * p-d-api up. An attacker COULD send enough in to initiate the
4599 	 * PD-API and then send a bunch of stuff to other streams... these
4600 	 * would wind up on the delivery queue.. and then we would not get
4601 	 * to them. But in order to do this I then have to back-track and
4602 	 * un-deliver sequence numbers in streams.. el-yucko. I think for
4603 	 * now we will NOT look at the delivery queue and leave it to be
4604 	 * something to consider later. An alternative would be to abort
4605 	 * the P-D-API with a notification and then deliver the data....
4606 	 * Or another method might be to keep track of how many times the
4607 	 * situation occurs and if we see a possible attack underway just
4608 	 * abort the association.
4609 	 */
4610 #ifdef SCTP_DEBUG
4611 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
4612 		if (cnt) {
4613 			printf("Freed %d chunks from reneg harvest\n", cnt);
4614 		}
4615 	}
4616 #endif /* SCTP_DEBUG */
4617 
4618 	/*
4619 	 * Another issue, in un-setting the TSN's in the mapping array we
4620 	 * DID NOT adjust the highest_tsn marker.  This will cause one of
4621 	 * two things to occur. It may cause us to do extra work in checking
4622 	 * for our mapping array movement. More importantly it may cause us
4623 	 * to SACK every datagram. This may not be a bad thing though since
4624 	 * we will recover once we get our cum-ack above and all this stuff
4625 	 * we dumped recovered.
4626 	 */
4627 }
4628 
4629 void
sctp_drain(void)4630 sctp_drain(void)
4631 {
4632 	/*
4633 	 * We must walk the PCB lists for ALL associations here. The system
4634 	 * is LOW on MBUF's and needs help. This is where reneging will
4635 	 * occur. We really hope this does NOT happen!
4636 	 */
4637 	struct sctp_inpcb *inp;
4638 	struct sctp_tcb *stcb;
4639 
4640 	SCTP_INP_INFO_RLOCK();
4641 	LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
4642 		/* For each endpoint */
4643 		SCTP_INP_RLOCK(inp);
4644 		LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4645 			/* For each association */
4646 			SCTP_TCB_LOCK(stcb);
4647 			sctp_drain_mbufs(inp, stcb);
4648 			SCTP_TCB_UNLOCK(stcb);
4649 		}
4650 		SCTP_INP_RUNLOCK(inp);
4651 	}
4652 	SCTP_INP_INFO_RUNLOCK();
4653 }
4654 
4655 int
sctp_add_to_socket_q(struct sctp_inpcb * inp,struct sctp_tcb * stcb)4656 sctp_add_to_socket_q(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4657 {
4658 	struct sctp_socket_q_list *sq;
4659 
4660 	/* write lock on INP assumed */
4661 	if ((inp == NULL) || (stcb == NULL)) {
4662 		/* I am paranoid */
4663 		return (0);
4664 	}
4665 	sq = (struct sctp_socket_q_list *)SCTP_ZONE_GET(
4666 	    sctppcbinfo.ipi_zone_sockq);
4667 	if (sq == NULL) {
4668 		/* out of sq structs */
4669 		return (0);
4670 	}
4671 	sctppcbinfo.ipi_count_sockq++;
4672 	sctppcbinfo.ipi_gencnt_sockq++;
4673 	if (stcb)
4674 		stcb->asoc.cnt_msg_on_sb++;
4675 	sq->tcb = stcb;
4676 	TAILQ_INSERT_TAIL(&inp->sctp_queue_list, sq, next_sq);
4677 	return (1);
4678 }
4679 
4680 
4681 struct sctp_tcb *
sctp_remove_from_socket_q(struct sctp_inpcb * inp)4682 sctp_remove_from_socket_q(struct sctp_inpcb *inp)
4683 {
4684 	struct sctp_tcb *stcb = NULL;
4685 	struct sctp_socket_q_list *sq;
4686 
4687 	/* W-Lock on INP assumed held */
4688 	sq = TAILQ_FIRST(&inp->sctp_queue_list);
4689 	if (sq == NULL)
4690 		return (NULL);
4691 
4692 	stcb = sq->tcb;
4693 	TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
4694 	SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
4695 	sctppcbinfo.ipi_count_sockq--;
4696 	sctppcbinfo.ipi_gencnt_sockq++;
4697 	if (stcb) {
4698 		stcb->asoc.cnt_msg_on_sb--;
4699 	}
4700 	return (stcb);
4701 }
4702 
4703 int
sctp_initiate_iterator(asoc_func af,uint32_t pcb_state,uint32_t asoc_state,void * argp,uint32_t argi,end_func ef,struct sctp_inpcb * s_inp)4704 sctp_initiate_iterator(asoc_func af, uint32_t pcb_state, uint32_t asoc_state,
4705 		       void *argp, uint32_t argi, end_func ef,
4706 		       struct sctp_inpcb *s_inp)
4707 {
4708 	struct sctp_iterator *it=NULL;
4709 	int s;
4710 	if (af == NULL) {
4711 		return (-1);
4712 	}
4713 	it = malloc(sizeof(struct sctp_iterator), M_PCB, M_WAITOK);
4714 	if (it == NULL) {
4715 		return (ENOMEM);
4716 	}
4717 	memset(it, 0, sizeof(*it));
4718 	it->function_toapply = af;
4719 	it->function_atend = ef;
4720 	it->pointer = argp;
4721 	it->val = argi;
4722 	it->pcb_flags = pcb_state;
4723 	it->asoc_state = asoc_state;
4724 	if (s_inp) {
4725 		it->inp = s_inp;
4726 		it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
4727 	} else {
4728 		SCTP_INP_INFO_RLOCK();
4729 		it->inp = LIST_FIRST(&sctppcbinfo.listhead);
4730 		SCTP_INP_INFO_RUNLOCK();
4731 		it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
4732 
4733 	}
4734 	/* Init the timer */
4735 	callout_init(&it->tmr.timer, 0);
4736 	/* add to the list of all iterators */
4737 	SCTP_INP_INFO_WLOCK();
4738 	LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
4739 	SCTP_INP_INFO_WUNLOCK();
4740 	s = splsoftnet();
4741 	sctp_iterator_timer(it);
4742 	splx(s);
4743 	return (0);
4744 }
4745 
4746 
4747