xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp_common.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/stream.h>
30 #include <sys/strsubr.h>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/kmem.h>
34 #include <sys/socket.h>
35 #include <sys/random.h>
36 #include <sys/tsol/tndb.h>
37 #include <sys/tsol/tnet.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/ip6.h>
41 #include <netinet/sctp.h>
42 
43 #include <inet/common.h>
44 #include <inet/ip.h>
45 #include <inet/ip6.h>
46 #include <inet/ip_ire.h>
47 #include <inet/mib2.h>
48 #include <inet/nd.h>
49 #include <inet/optcom.h>
50 #include <inet/sctp_ip.h>
51 #include <inet/ipclassifier.h>
52 
53 #include "sctp_impl.h"
54 #include "sctp_addr.h"
55 #include "sctp_asconf.h"
56 
57 static struct kmem_cache *sctp_kmem_faddr_cache;
58 static void sctp_init_faddr(sctp_t *, sctp_faddr_t *, in6_addr_t *, mblk_t *);
59 
60 /* Set the source address.  Refer to comments in sctp_get_ire(). */
61 void
62 sctp_set_saddr(sctp_t *sctp, sctp_faddr_t *fp)
63 {
64 	boolean_t v6 = !fp->isv4;
65 	boolean_t addr_set;
66 
67 	fp->saddr = sctp_get_valid_addr(sctp, v6, &addr_set);
68 	/*
69 	 * If there is no source address avaialble, mark this peer address
70 	 * as unreachable for now.  When the heartbeat timer fires, it will
71 	 * call sctp_get_ire() to re-check if there is any source address
72 	 * available.
73 	 */
74 	if (!addr_set)
75 		fp->state = SCTP_FADDRS_UNREACH;
76 }
77 
78 /*
79  * Call this function to update the cached IRE of a peer addr fp.
80  */
81 void
82 sctp_get_ire(sctp_t *sctp, sctp_faddr_t *fp)
83 {
84 	ire_t		*ire;
85 	ipaddr_t	addr4;
86 	in6_addr_t	laddr;
87 	sctp_saddr_ipif_t *sp;
88 	int		hdrlen;
89 	ts_label_t	*tsl;
90 	sctp_stack_t	*sctps = sctp->sctp_sctps;
91 	ip_stack_t	*ipst = sctps->sctps_netstack->netstack_ip;
92 
93 	/* Remove the previous cache IRE */
94 	if ((ire = fp->ire) != NULL) {
95 		IRE_REFRELE_NOTR(ire);
96 		fp->ire = NULL;
97 	}
98 
99 	/*
100 	 * If this addr is not reachable, mark it as unconfirmed for now, the
101 	 * state will be changed back to unreachable later in this function
102 	 * if it is still the case.
103 	 */
104 	if (fp->state == SCTP_FADDRS_UNREACH) {
105 		fp->state = SCTP_FADDRS_UNCONFIRMED;
106 	}
107 
108 	tsl = crgetlabel(CONN_CRED(sctp->sctp_connp));
109 
110 	if (fp->isv4) {
111 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
112 		ire = ire_cache_lookup(addr4, sctp->sctp_zoneid, tsl, ipst);
113 		if (ire != NULL)
114 			IN6_IPADDR_TO_V4MAPPED(ire->ire_src_addr, &laddr);
115 	} else {
116 		ire = ire_cache_lookup_v6(&fp->faddr, sctp->sctp_zoneid, tsl,
117 		    ipst);
118 		if (ire != NULL)
119 			laddr = ire->ire_src_addr_v6;
120 	}
121 
122 	if (ire == NULL) {
123 		dprint(3, ("ire2faddr: no ire for %x:%x:%x:%x\n",
124 		    SCTP_PRINTADDR(fp->faddr)));
125 		/*
126 		 * It is tempting to just leave the src addr
127 		 * unspecified and let IP figure it out, but we
128 		 * *cannot* do this, since IP may choose a src addr
129 		 * that is not part of this association... unless
130 		 * this sctp has bound to all addrs.  So if the ire
131 		 * lookup fails, try to find one in our src addr
132 		 * list, unless the sctp has bound to all addrs, in
133 		 * which case we change the src addr to unspec.
134 		 *
135 		 * Note that if this is a v6 endpoint but it does
136 		 * not have any v4 address at this point (e.g. may
137 		 * have been  deleted), sctp_get_valid_addr() will
138 		 * return mapped INADDR_ANY.  In this case, this
139 		 * address should be marked not reachable so that
140 		 * it won't be used to send data.
141 		 */
142 		sctp_set_saddr(sctp, fp);
143 		if (fp->state == SCTP_FADDRS_UNREACH)
144 			return;
145 		goto check_current;
146 	}
147 
148 	/* Make sure the laddr is part of this association */
149 	if ((sp = sctp_saddr_lookup(sctp, &ire->ire_ipif->ipif_v6lcl_addr,
150 	    0)) != NULL && !sp->saddr_ipif_dontsrc) {
151 		if (sp->saddr_ipif_unconfirmed == 1)
152 			sp->saddr_ipif_unconfirmed = 0;
153 		fp->saddr = laddr;
154 	} else {
155 		dprint(2, ("ire2faddr: src addr is not part of assc\n"));
156 
157 		/*
158 		 * Set the src to the first saddr and hope for the best.
159 		 * Note that we will still do the ire caching below.
160 		 * Otherwise, whenever we send a packet, we need to do
161 		 * the ire lookup again and still may not get the correct
162 		 * source address.  Note that this case should very seldomly
163 		 * happen.  One scenario this can happen is an app
164 		 * explicitly bind() to an address.  But that address is
165 		 * not the preferred source address to send to the peer.
166 		 */
167 		sctp_set_saddr(sctp, fp);
168 		if (fp->state == SCTP_FADDRS_UNREACH) {
169 			IRE_REFRELE(ire);
170 			return;
171 		}
172 	}
173 
174 	/*
175 	 * Note that ire_cache_lookup_*() returns an ire with the tracing
176 	 * bits enabled.  This requires the thread holding the ire also
177 	 * do the IRE_REFRELE().  Thus we need to do IRE_REFHOLD_NOTR()
178 	 * and then IRE_REFRELE() the ire here to make the tracing bits
179 	 * work.
180 	 */
181 	IRE_REFHOLD_NOTR(ire);
182 	IRE_REFRELE(ire);
183 
184 	/* Cache the IRE */
185 	fp->ire = ire;
186 	if (fp->ire->ire_type == IRE_LOOPBACK && !sctp->sctp_loopback)
187 		sctp->sctp_loopback = 1;
188 
189 	/*
190 	 * Pull out RTO information for this faddr and use it if we don't
191 	 * have any yet.
192 	 */
193 	if (fp->srtt == -1 && ire->ire_uinfo.iulp_rtt != 0) {
194 		/* The cached value is in ms. */
195 		fp->srtt = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt);
196 		fp->rttvar = MSEC_TO_TICK(ire->ire_uinfo.iulp_rtt_sd);
197 		fp->rto = 3 * fp->srtt;
198 
199 		/* Bound the RTO by configured min and max values */
200 		if (fp->rto < sctp->sctp_rto_min) {
201 			fp->rto = sctp->sctp_rto_min;
202 		}
203 		if (fp->rto > sctp->sctp_rto_max) {
204 			fp->rto = sctp->sctp_rto_max;
205 		}
206 		SCTP_MAX_RTO(sctp, fp);
207 	}
208 
209 	/*
210 	 * Record the MTU for this faddr. If the MTU for this faddr has
211 	 * changed, check if the assc MTU will also change.
212 	 */
213 	if (fp->isv4) {
214 		hdrlen = sctp->sctp_hdr_len;
215 	} else {
216 		hdrlen = sctp->sctp_hdr6_len;
217 	}
218 	if ((fp->sfa_pmss + hdrlen) != ire->ire_max_frag) {
219 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
220 		fp->sfa_pmss = (ire->ire_max_frag - hdrlen) & ~(SCTP_ALIGN - 1);
221 		if (fp->cwnd < (fp->sfa_pmss * 2)) {
222 			SET_CWND(fp, fp->sfa_pmss,
223 			    sctps->sctps_slow_start_initial);
224 		}
225 	}
226 
227 check_current:
228 	if (fp == sctp->sctp_current)
229 		sctp_set_faddr_current(sctp, fp);
230 }
231 
232 void
233 sctp_update_ire(sctp_t *sctp)
234 {
235 	ire_t		*ire;
236 	sctp_faddr_t	*fp;
237 	sctp_stack_t	*sctps = sctp->sctp_sctps;
238 
239 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
240 		if ((ire = fp->ire) == NULL)
241 			continue;
242 		mutex_enter(&ire->ire_lock);
243 
244 		/*
245 		 * If the cached IRE is going away, there is no point to
246 		 * update it.
247 		 */
248 		if (ire->ire_marks & IRE_MARK_CONDEMNED) {
249 			mutex_exit(&ire->ire_lock);
250 			IRE_REFRELE_NOTR(ire);
251 			fp->ire = NULL;
252 			continue;
253 		}
254 
255 		/*
256 		 * Only record the PMTU for this faddr if we actually have
257 		 * done discovery. This prevents initialized default from
258 		 * clobbering any real info that IP may have.
259 		 */
260 		if (fp->pmtu_discovered) {
261 			if (fp->isv4) {
262 				ire->ire_max_frag = fp->sfa_pmss +
263 				    sctp->sctp_hdr_len;
264 			} else {
265 				ire->ire_max_frag = fp->sfa_pmss +
266 				    sctp->sctp_hdr6_len;
267 			}
268 		}
269 
270 		if (sctps->sctps_rtt_updates != 0 &&
271 		    fp->rtt_updates >= sctps->sctps_rtt_updates) {
272 			/*
273 			 * If there is no old cached values, initialize them
274 			 * conservatively.  Set them to be (1.5 * new value).
275 			 * This code copied from ip_ire_advise().  The cached
276 			 * value is in ms.
277 			 */
278 			if (ire->ire_uinfo.iulp_rtt != 0) {
279 				ire->ire_uinfo.iulp_rtt =
280 				    (ire->ire_uinfo.iulp_rtt +
281 				    TICK_TO_MSEC(fp->srtt)) >> 1;
282 			} else {
283 				ire->ire_uinfo.iulp_rtt =
284 				    TICK_TO_MSEC(fp->srtt + (fp->srtt >> 1));
285 			}
286 			if (ire->ire_uinfo.iulp_rtt_sd != 0) {
287 				ire->ire_uinfo.iulp_rtt_sd =
288 				    (ire->ire_uinfo.iulp_rtt_sd +
289 				    TICK_TO_MSEC(fp->rttvar)) >> 1;
290 			} else {
291 				ire->ire_uinfo.iulp_rtt_sd =
292 				    TICK_TO_MSEC(fp->rttvar +
293 				    (fp->rttvar >> 1));
294 			}
295 			fp->rtt_updates = 0;
296 		}
297 		mutex_exit(&ire->ire_lock);
298 	}
299 }
300 
301 /*
302  * The sender must set the total length in the IP header.
303  * If sendto == NULL, the current will be used.
304  */
305 mblk_t *
306 sctp_make_mp(sctp_t *sctp, sctp_faddr_t *sendto, int trailer)
307 {
308 	mblk_t *mp;
309 	size_t ipsctplen;
310 	int isv4;
311 	sctp_faddr_t *fp;
312 	sctp_stack_t *sctps = sctp->sctp_sctps;
313 	boolean_t src_changed = B_FALSE;
314 
315 	ASSERT(sctp->sctp_current != NULL || sendto != NULL);
316 	if (sendto == NULL) {
317 		fp = sctp->sctp_current;
318 	} else {
319 		fp = sendto;
320 	}
321 	isv4 = fp->isv4;
322 
323 	/* Try to look for another IRE again. */
324 	if (fp->ire == NULL) {
325 		sctp_get_ire(sctp, fp);
326 		/*
327 		 * Although we still may not get an IRE, the source address
328 		 * may be changed in sctp_get_ire().  Set src_changed to
329 		 * true so that the source address is copied again.
330 		 */
331 		src_changed = B_TRUE;
332 	}
333 
334 	/* There is no suitable source address to use, return. */
335 	if (fp->state == SCTP_FADDRS_UNREACH)
336 		return (NULL);
337 	ASSERT(!SCTP_IS_ADDR_UNSPEC(fp->isv4, fp->saddr));
338 
339 	if (isv4) {
340 		ipsctplen = sctp->sctp_hdr_len;
341 	} else {
342 		ipsctplen = sctp->sctp_hdr6_len;
343 	}
344 
345 	mp = allocb_cred(ipsctplen + sctps->sctps_wroff_xtra + trailer,
346 	    CONN_CRED(sctp->sctp_connp), sctp->sctp_cpid);
347 	if (mp == NULL) {
348 		ip1dbg(("sctp_make_mp: error making mp..\n"));
349 		return (NULL);
350 	}
351 	mp->b_rptr += sctps->sctps_wroff_xtra;
352 	mp->b_wptr = mp->b_rptr + ipsctplen;
353 
354 	ASSERT(OK_32PTR(mp->b_wptr));
355 
356 	if (isv4) {
357 		ipha_t *iph = (ipha_t *)mp->b_rptr;
358 
359 		bcopy(sctp->sctp_iphc, mp->b_rptr, ipsctplen);
360 		if (fp != sctp->sctp_current || src_changed) {
361 			/* Fix the source and destination addresses. */
362 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
363 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr, iph->ipha_src);
364 		}
365 		/* set or clear the don't fragment bit */
366 		if (fp->df) {
367 			iph->ipha_fragment_offset_and_flags = htons(IPH_DF);
368 		} else {
369 			iph->ipha_fragment_offset_and_flags = 0;
370 		}
371 	} else {
372 		bcopy(sctp->sctp_iphc6, mp->b_rptr, ipsctplen);
373 		if (fp != sctp->sctp_current || src_changed) {
374 			/* Fix the source and destination addresses. */
375 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
376 			((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
377 		}
378 	}
379 	ASSERT(sctp->sctp_connp != NULL);
380 
381 	/*
382 	 * IP will not free this IRE if it is condemned.  SCTP needs to
383 	 * free it.
384 	 */
385 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
386 		IRE_REFRELE_NOTR(fp->ire);
387 		fp->ire = NULL;
388 	}
389 	/* Stash the conn and ire ptr info. for IP */
390 	SCTP_STASH_IPINFO(mp, fp->ire);
391 
392 	return (mp);
393 }
394 
395 /*
396  * Notify upper layers about preferred write offset, write size.
397  */
398 void
399 sctp_set_ulp_prop(sctp_t *sctp)
400 {
401 	int hdrlen;
402 	struct sock_proto_props sopp;
403 
404 	sctp_stack_t *sctps = sctp->sctp_sctps;
405 
406 	if (sctp->sctp_current->isv4) {
407 		hdrlen = sctp->sctp_hdr_len;
408 	} else {
409 		hdrlen = sctp->sctp_hdr6_len;
410 	}
411 	ASSERT(sctp->sctp_ulpd);
412 
413 	ASSERT(sctp->sctp_current->sfa_pmss == sctp->sctp_mss);
414 	bzero(&sopp, sizeof (sopp));
415 	sopp.sopp_flags = SOCKOPT_MAXBLK|SOCKOPT_WROFF;
416 	sopp.sopp_wroff = sctps->sctps_wroff_xtra + hdrlen +
417 	    sizeof (sctp_data_hdr_t);
418 	sopp.sopp_maxblk = sctp->sctp_mss - sizeof (sctp_data_hdr_t);
419 	sctp->sctp_ulp_prop(sctp->sctp_ulpd, &sopp);
420 }
421 
422 void
423 sctp_set_iplen(sctp_t *sctp, mblk_t *mp)
424 {
425 	uint16_t	sum = 0;
426 	ipha_t		*iph;
427 	ip6_t		*ip6h;
428 	mblk_t		*pmp = mp;
429 	boolean_t	isv4;
430 
431 	isv4 = (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
432 	for (; pmp; pmp = pmp->b_cont)
433 		sum += pmp->b_wptr - pmp->b_rptr;
434 
435 	if (isv4) {
436 		iph = (ipha_t *)mp->b_rptr;
437 		iph->ipha_length = htons(sum);
438 	} else {
439 		ip6h = (ip6_t *)mp->b_rptr;
440 		/*
441 		 * If an ip6i_t is present, the real IPv6 header
442 		 * immediately follows.
443 		 */
444 		if (ip6h->ip6_nxt == IPPROTO_RAW)
445 			ip6h = (ip6_t *)&ip6h[1];
446 		ip6h->ip6_plen = htons(sum - ((char *)&sctp->sctp_ip6h[1] -
447 		    sctp->sctp_iphc6));
448 	}
449 }
450 
451 int
452 sctp_compare_faddrsets(sctp_faddr_t *a1, sctp_faddr_t *a2)
453 {
454 	int na1 = 0;
455 	int overlap = 0;
456 	int equal = 1;
457 	int onematch;
458 	sctp_faddr_t *fp1, *fp2;
459 
460 	for (fp1 = a1; fp1; fp1 = fp1->next) {
461 		onematch = 0;
462 		for (fp2 = a2; fp2; fp2 = fp2->next) {
463 			if (IN6_ARE_ADDR_EQUAL(&fp1->faddr, &fp2->faddr)) {
464 				overlap++;
465 				onematch = 1;
466 				break;
467 			}
468 			if (!onematch) {
469 				equal = 0;
470 			}
471 		}
472 		na1++;
473 	}
474 
475 	if (equal) {
476 		return (SCTP_ADDR_EQUAL);
477 	}
478 	if (overlap == na1) {
479 		return (SCTP_ADDR_SUBSET);
480 	}
481 	if (overlap) {
482 		return (SCTP_ADDR_OVERLAP);
483 	}
484 	return (SCTP_ADDR_DISJOINT);
485 }
486 
487 /*
488  * Returns 0 on success, ENOMEM on memory allocation failure, EHOSTUNREACH
489  * if the connection credentials fail remote host accreditation or
490  * if the new destination does not support the previously established
491  * connection security label. If sleep is true, this function should
492  * never fail for a memory allocation failure. The boolean parameter
493  * "first" decides whether the newly created faddr structure should be
494  * added at the beginning of the list or at the end.
495  *
496  * Note: caller must hold conn fanout lock.
497  */
498 int
499 sctp_add_faddr(sctp_t *sctp, in6_addr_t *addr, int sleep, boolean_t first)
500 {
501 	sctp_faddr_t	*faddr;
502 	mblk_t		*timer_mp;
503 	int		err;
504 
505 	if (is_system_labeled()) {
506 		cred_t *effective_cred;
507 
508 		/*
509 		 * Verify the destination is allowed to receive packets
510 		 * at the security label of the connection we are initiating.
511 		 *
512 		 * tsol_check_dest() will create a new effective cred for
513 		 * this connection with a modified label or label flags only
514 		 * if there are changes from the original cred.
515 		 *
516 		 * conn_effective_cred may be non-NULL if a previous
517 		 * faddr was already added or if this is a server
518 		 * accepting a connection on a multi-label port.
519 		 *
520 		 * Accept whatever label we get if this is the first
521 		 * destination address for this connection. The security
522 		 * label and label flags must match any previuous settings
523 		 * for all subsequent destination addresses.
524 		 */
525 		if (IN6_IS_ADDR_V4MAPPED(addr)) {
526 			uint32_t dst;
527 			IN6_V4MAPPED_TO_IPADDR(addr, dst);
528 			err = tsol_check_dest(CONN_CRED(sctp->sctp_connp),
529 			    &dst, IPV4_VERSION, sctp->sctp_mac_mode,
530 			    &effective_cred);
531 		} else {
532 			err = tsol_check_dest(CONN_CRED(sctp->sctp_connp),
533 			    addr, IPV6_VERSION, sctp->sctp_mac_mode,
534 			    &effective_cred);
535 		}
536 		if (err != 0)
537 			return (err);
538 		if (sctp->sctp_faddrs == NULL &&
539 		    sctp->sctp_connp->conn_effective_cred == NULL) {
540 			sctp->sctp_connp->conn_effective_cred = effective_cred;
541 		} else if (effective_cred != NULL) {
542 			crfree(effective_cred);
543 			return (EHOSTUNREACH);
544 		}
545 	}
546 
547 	if ((faddr = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep)) == NULL)
548 		return (ENOMEM);
549 	timer_mp = sctp_timer_alloc((sctp), sctp_rexmit_timer, sleep);
550 	if (timer_mp == NULL) {
551 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
552 		return (ENOMEM);
553 	}
554 	((sctpt_t *)(timer_mp->b_rptr))->sctpt_faddr = faddr;
555 
556 	sctp_init_faddr(sctp, faddr, addr, timer_mp);
557 
558 	/* Check for subnet broadcast. */
559 	if (faddr->ire != NULL && faddr->ire->ire_type & IRE_BROADCAST) {
560 		IRE_REFRELE_NOTR(faddr->ire);
561 		sctp_timer_free(timer_mp);
562 		faddr->timer_mp = NULL;
563 		kmem_cache_free(sctp_kmem_faddr_cache, faddr);
564 		return (EADDRNOTAVAIL);
565 	}
566 	ASSERT(faddr->next == NULL);
567 
568 	if (sctp->sctp_faddrs == NULL) {
569 		ASSERT(sctp->sctp_lastfaddr == NULL);
570 		/* only element on list; first and last are same */
571 		sctp->sctp_faddrs = sctp->sctp_lastfaddr = faddr;
572 	} else if (first) {
573 		ASSERT(sctp->sctp_lastfaddr != NULL);
574 		faddr->next = sctp->sctp_faddrs;
575 		sctp->sctp_faddrs = faddr;
576 	} else {
577 		sctp->sctp_lastfaddr->next = faddr;
578 		sctp->sctp_lastfaddr = faddr;
579 	}
580 	sctp->sctp_nfaddrs++;
581 
582 	return (0);
583 }
584 
585 sctp_faddr_t *
586 sctp_lookup_faddr(sctp_t *sctp, in6_addr_t *addr)
587 {
588 	sctp_faddr_t *fp;
589 
590 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
591 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr))
592 			break;
593 	}
594 
595 	return (fp);
596 }
597 
598 sctp_faddr_t *
599 sctp_lookup_faddr_nosctp(sctp_faddr_t *fp, in6_addr_t *addr)
600 {
601 	for (; fp; fp = fp->next) {
602 		if (IN6_ARE_ADDR_EQUAL(&fp->faddr, addr)) {
603 			break;
604 		}
605 	}
606 
607 	return (fp);
608 }
609 
610 /*
611  * To change the currently used peer address to the specified one.
612  */
613 void
614 sctp_set_faddr_current(sctp_t *sctp, sctp_faddr_t *fp)
615 {
616 	/* Now setup the composite header. */
617 	if (fp->isv4) {
618 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
619 		    sctp->sctp_ipha->ipha_dst);
620 		IN6_V4MAPPED_TO_IPADDR(&fp->saddr, sctp->sctp_ipha->ipha_src);
621 		/* update don't fragment bit */
622 		if (fp->df) {
623 			sctp->sctp_ipha->ipha_fragment_offset_and_flags =
624 			    htons(IPH_DF);
625 		} else {
626 			sctp->sctp_ipha->ipha_fragment_offset_and_flags = 0;
627 		}
628 	} else {
629 		sctp->sctp_ip6h->ip6_dst = fp->faddr;
630 		sctp->sctp_ip6h->ip6_src = fp->saddr;
631 	}
632 
633 	sctp->sctp_current = fp;
634 	sctp->sctp_mss = fp->sfa_pmss;
635 
636 	/* Update the uppper layer for the change. */
637 	if (!SCTP_IS_DETACHED(sctp))
638 		sctp_set_ulp_prop(sctp);
639 }
640 
641 void
642 sctp_redo_faddr_srcs(sctp_t *sctp)
643 {
644 	sctp_faddr_t *fp;
645 
646 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
647 		sctp_get_ire(sctp, fp);
648 	}
649 }
650 
651 void
652 sctp_faddr_alive(sctp_t *sctp, sctp_faddr_t *fp)
653 {
654 	int64_t now = lbolt64;
655 
656 	fp->strikes = 0;
657 	sctp->sctp_strikes = 0;
658 	fp->lastactive = now;
659 	fp->hb_expiry = now + SET_HB_INTVL(fp);
660 	fp->hb_pending = B_FALSE;
661 	if (fp->state != SCTP_FADDRS_ALIVE) {
662 		fp->state = SCTP_FADDRS_ALIVE;
663 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_AVAILABLE, 0);
664 		/* Should have a full IRE now */
665 		sctp_get_ire(sctp, fp);
666 
667 		/*
668 		 * If this is the primary, switch back to it now.  And
669 		 * we probably want to reset the source addr used to reach
670 		 * it.
671 		 */
672 		if (fp == sctp->sctp_primary) {
673 			ASSERT(fp->state != SCTP_FADDRS_UNREACH);
674 			sctp_set_faddr_current(sctp, fp);
675 			return;
676 		}
677 	}
678 }
679 
680 int
681 sctp_is_a_faddr_clean(sctp_t *sctp)
682 {
683 	sctp_faddr_t *fp;
684 
685 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
686 		if (fp->state == SCTP_FADDRS_ALIVE && fp->strikes == 0) {
687 			return (1);
688 		}
689 	}
690 
691 	return (0);
692 }
693 
694 /*
695  * Returns 0 if there is at leave one other active faddr, -1 if there
696  * are none. If there are none left, faddr_dead() will start killing the
697  * association.
698  * If the downed faddr was the current faddr, a new current faddr
699  * will be chosen.
700  */
701 int
702 sctp_faddr_dead(sctp_t *sctp, sctp_faddr_t *fp, int newstate)
703 {
704 	sctp_faddr_t *ofp;
705 	sctp_stack_t *sctps = sctp->sctp_sctps;
706 
707 	if (fp->state == SCTP_FADDRS_ALIVE) {
708 		sctp_intf_event(sctp, fp->faddr, SCTP_ADDR_UNREACHABLE, 0);
709 	}
710 	fp->state = newstate;
711 
712 	dprint(1, ("sctp_faddr_dead: %x:%x:%x:%x down (state=%d)\n",
713 	    SCTP_PRINTADDR(fp->faddr), newstate));
714 
715 	if (fp == sctp->sctp_current) {
716 		/* Current faddr down; need to switch it */
717 		sctp->sctp_current = NULL;
718 	}
719 
720 	/* Find next alive faddr */
721 	ofp = fp;
722 	for (fp = fp->next; fp != NULL; fp = fp->next) {
723 		if (fp->state == SCTP_FADDRS_ALIVE) {
724 			break;
725 		}
726 	}
727 
728 	if (fp == NULL) {
729 		/* Continue from beginning of list */
730 		for (fp = sctp->sctp_faddrs; fp != ofp; fp = fp->next) {
731 			if (fp->state == SCTP_FADDRS_ALIVE) {
732 				break;
733 			}
734 		}
735 	}
736 
737 	/*
738 	 * Find a new fp, so if the current faddr is dead, use the new fp
739 	 * as the current one.
740 	 */
741 	if (fp != ofp) {
742 		if (sctp->sctp_current == NULL) {
743 			dprint(1, ("sctp_faddr_dead: failover->%x:%x:%x:%x\n",
744 			    SCTP_PRINTADDR(fp->faddr)));
745 			/*
746 			 * Note that we don't need to reset the source addr
747 			 * of the new fp.
748 			 */
749 			sctp_set_faddr_current(sctp, fp);
750 		}
751 		return (0);
752 	}
753 
754 
755 	/* All faddrs are down; kill the association */
756 	dprint(1, ("sctp_faddr_dead: all faddrs down, killing assoc\n"));
757 	BUMP_MIB(&sctps->sctps_mib, sctpAborted);
758 	sctp_assoc_event(sctp, sctp->sctp_state < SCTPS_ESTABLISHED ?
759 	    SCTP_CANT_STR_ASSOC : SCTP_COMM_LOST, 0, NULL);
760 	sctp_clean_death(sctp, sctp->sctp_client_errno ?
761 	    sctp->sctp_client_errno : ETIMEDOUT);
762 
763 	return (-1);
764 }
765 
766 sctp_faddr_t *
767 sctp_rotate_faddr(sctp_t *sctp, sctp_faddr_t *ofp)
768 {
769 	sctp_faddr_t *nfp = NULL;
770 
771 	if (ofp == NULL) {
772 		ofp = sctp->sctp_current;
773 	}
774 
775 	/* Find the next live one */
776 	for (nfp = ofp->next; nfp != NULL; nfp = nfp->next) {
777 		if (nfp->state == SCTP_FADDRS_ALIVE) {
778 			break;
779 		}
780 	}
781 
782 	if (nfp == NULL) {
783 		/* Continue from beginning of list */
784 		for (nfp = sctp->sctp_faddrs; nfp != ofp; nfp = nfp->next) {
785 			if (nfp->state == SCTP_FADDRS_ALIVE) {
786 				break;
787 			}
788 		}
789 	}
790 
791 	/*
792 	 * nfp could only be NULL if all faddrs are down, and when
793 	 * this happens, faddr_dead() should have killed the
794 	 * association. Hence this assertion...
795 	 */
796 	ASSERT(nfp != NULL);
797 	return (nfp);
798 }
799 
800 void
801 sctp_unlink_faddr(sctp_t *sctp, sctp_faddr_t *fp)
802 {
803 	sctp_faddr_t *fpp;
804 
805 	if (!sctp->sctp_faddrs) {
806 		return;
807 	}
808 
809 	if (fp->timer_mp != NULL) {
810 		sctp_timer_free(fp->timer_mp);
811 		fp->timer_mp = NULL;
812 		fp->timer_running = 0;
813 	}
814 	if (fp->rc_timer_mp != NULL) {
815 		sctp_timer_free(fp->rc_timer_mp);
816 		fp->rc_timer_mp = NULL;
817 		fp->rc_timer_running = 0;
818 	}
819 	if (fp->ire != NULL) {
820 		IRE_REFRELE_NOTR(fp->ire);
821 		fp->ire = NULL;
822 	}
823 
824 	if (fp == sctp->sctp_faddrs) {
825 		goto gotit;
826 	}
827 
828 	for (fpp = sctp->sctp_faddrs; fpp->next != fp; fpp = fpp->next)
829 		;
830 
831 gotit:
832 	ASSERT(sctp->sctp_conn_tfp != NULL);
833 	mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
834 	if (fp == sctp->sctp_faddrs) {
835 		sctp->sctp_faddrs = fp->next;
836 	} else {
837 		fpp->next = fp->next;
838 	}
839 	mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
840 	/* XXX faddr2ire? */
841 	kmem_cache_free(sctp_kmem_faddr_cache, fp);
842 	sctp->sctp_nfaddrs--;
843 }
844 
845 void
846 sctp_zap_faddrs(sctp_t *sctp, int caller_holds_lock)
847 {
848 	sctp_faddr_t *fp, *fpn;
849 
850 	if (sctp->sctp_faddrs == NULL) {
851 		ASSERT(sctp->sctp_lastfaddr == NULL);
852 		return;
853 	}
854 
855 	ASSERT(sctp->sctp_lastfaddr != NULL);
856 	sctp->sctp_lastfaddr = NULL;
857 	sctp->sctp_current = NULL;
858 	sctp->sctp_primary = NULL;
859 
860 	sctp_free_faddr_timers(sctp);
861 
862 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
863 		/* in conn fanout; need to hold lock */
864 		mutex_enter(&sctp->sctp_conn_tfp->tf_lock);
865 	}
866 
867 	for (fp = sctp->sctp_faddrs; fp; fp = fpn) {
868 		fpn = fp->next;
869 		if (fp->ire != NULL)
870 			IRE_REFRELE_NOTR(fp->ire);
871 		kmem_cache_free(sctp_kmem_faddr_cache, fp);
872 		sctp->sctp_nfaddrs--;
873 	}
874 
875 	sctp->sctp_faddrs = NULL;
876 	ASSERT(sctp->sctp_nfaddrs == 0);
877 	if (sctp->sctp_conn_tfp != NULL && !caller_holds_lock) {
878 		mutex_exit(&sctp->sctp_conn_tfp->tf_lock);
879 	}
880 
881 }
882 
883 void
884 sctp_zap_addrs(sctp_t *sctp)
885 {
886 	sctp_zap_faddrs(sctp, 0);
887 	sctp_free_saddrs(sctp);
888 }
889 
890 /*
891  * Initialize the IPv4 header. Loses any record of any IP options.
892  */
893 int
894 sctp_header_init_ipv4(sctp_t *sctp, int sleep)
895 {
896 	sctp_hdr_t	*sctph;
897 	sctp_stack_t	*sctps = sctp->sctp_sctps;
898 
899 	/*
900 	 * This is a simple initialization. If there's
901 	 * already a template, it should never be too small,
902 	 * so reuse it.  Otherwise, allocate space for the new one.
903 	 */
904 	if (sctp->sctp_iphc != NULL) {
905 		ASSERT(sctp->sctp_iphc_len >= SCTP_MAX_COMBINED_HEADER_LENGTH);
906 		bzero(sctp->sctp_iphc, sctp->sctp_iphc_len);
907 	} else {
908 		sctp->sctp_iphc_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
909 		sctp->sctp_iphc = kmem_zalloc(sctp->sctp_iphc_len, sleep);
910 		if (sctp->sctp_iphc == NULL) {
911 			sctp->sctp_iphc_len = 0;
912 			return (ENOMEM);
913 		}
914 	}
915 
916 	sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc;
917 
918 	sctp->sctp_hdr_len = sizeof (ipha_t) + sizeof (sctp_hdr_t);
919 	sctp->sctp_ip_hdr_len = sizeof (ipha_t);
920 	sctp->sctp_ipha->ipha_length = htons(sizeof (ipha_t) +
921 	    sizeof (sctp_hdr_t));
922 	sctp->sctp_ipha->ipha_version_and_hdr_length =
923 	    (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS;
924 
925 	/*
926 	 * These two fields should be zero, and are already set above.
927 	 *
928 	 * sctp->sctp_ipha->ipha_ident,
929 	 * sctp->sctp_ipha->ipha_fragment_offset_and_flags.
930 	 */
931 
932 	sctp->sctp_ipha->ipha_ttl = sctps->sctps_ipv4_ttl;
933 	sctp->sctp_ipha->ipha_protocol = IPPROTO_SCTP;
934 
935 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc + sizeof (ipha_t));
936 	sctp->sctp_sctph = sctph;
937 
938 	return (0);
939 }
940 
941 /*
942  * Update sctp_sticky_hdrs based on sctp_sticky_ipp.
943  * The headers include ip6i_t (if needed), ip6_t, any sticky extension
944  * headers, and the maximum size sctp header (to avoid reallocation
945  * on the fly for additional sctp options).
946  * Returns failure if can't allocate memory.
947  */
948 int
949 sctp_build_hdrs(sctp_t *sctp)
950 {
951 	char		*hdrs;
952 	uint_t		hdrs_len;
953 	ip6i_t		*ip6i;
954 	char		buf[SCTP_MAX_HDR_LENGTH];
955 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
956 	in6_addr_t	src;
957 	in6_addr_t	dst;
958 	sctp_stack_t	*sctps = sctp->sctp_sctps;
959 
960 	/*
961 	 * save the existing sctp header and source/dest IP addresses
962 	 */
963 	bcopy(sctp->sctp_sctph6, buf, sizeof (sctp_hdr_t));
964 	src = sctp->sctp_ip6h->ip6_src;
965 	dst = sctp->sctp_ip6h->ip6_dst;
966 	hdrs_len = ip_total_hdrs_len_v6(ipp) + SCTP_MAX_HDR_LENGTH;
967 	ASSERT(hdrs_len != 0);
968 	if (hdrs_len > sctp->sctp_iphc6_len) {
969 		/* Need to reallocate */
970 		hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP);
971 		if (hdrs == NULL)
972 			return (ENOMEM);
973 
974 		if (sctp->sctp_iphc6_len != 0)
975 			kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
976 		sctp->sctp_iphc6 = hdrs;
977 		sctp->sctp_iphc6_len = hdrs_len;
978 	}
979 	ip_build_hdrs_v6((uchar_t *)sctp->sctp_iphc6,
980 	    hdrs_len - SCTP_MAX_HDR_LENGTH, ipp, IPPROTO_SCTP);
981 
982 	/* Set header fields not in ipp */
983 	if (ipp->ipp_fields & IPPF_HAS_IP6I) {
984 		ip6i = (ip6i_t *)sctp->sctp_iphc6;
985 		sctp->sctp_ip6h = (ip6_t *)&ip6i[1];
986 	} else {
987 		sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
988 	}
989 	/*
990 	 * sctp->sctp_ip_hdr_len will include ip6i_t if there is one.
991 	 */
992 	sctp->sctp_ip_hdr6_len = hdrs_len - SCTP_MAX_HDR_LENGTH;
993 	sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 +
994 	    sctp->sctp_ip_hdr6_len);
995 	sctp->sctp_hdr6_len = sctp->sctp_ip_hdr6_len + sizeof (sctp_hdr_t);
996 
997 	bcopy(buf, sctp->sctp_sctph6, sizeof (sctp_hdr_t));
998 
999 	sctp->sctp_ip6h->ip6_src = src;
1000 	sctp->sctp_ip6h->ip6_dst = dst;
1001 	/*
1002 	 * If the hoplimit was not set by ip_build_hdrs_v6(), we need to
1003 	 * set it to the default value for SCTP.
1004 	 */
1005 	if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS))
1006 		sctp->sctp_ip6h->ip6_hops = sctps->sctps_ipv6_hoplimit;
1007 	/*
1008 	 * If we're setting extension headers after a connection
1009 	 * has been established, and if we have a routing header
1010 	 * among the extension headers, call ip_massage_options_v6 to
1011 	 * manipulate the routing header/ip6_dst set the checksum
1012 	 * difference in the sctp header template.
1013 	 * (This happens in sctp_connect_ipv6 if the routing header
1014 	 * is set prior to the connect.)
1015 	 */
1016 
1017 	if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
1018 	    (sctp->sctp_sticky_ipp.ipp_fields & IPPF_RTHDR)) {
1019 		ip6_rthdr_t *rth;
1020 
1021 		rth = ip_find_rthdr_v6(sctp->sctp_ip6h,
1022 		    (uint8_t *)sctp->sctp_sctph6);
1023 		if (rth != NULL) {
1024 			(void) ip_massage_options_v6(sctp->sctp_ip6h, rth,
1025 			    sctps->sctps_netstack);
1026 		}
1027 	}
1028 	return (0);
1029 }
1030 
1031 /*
1032  * Initialize the IPv6 header. Loses any record of any IPv6 extension headers.
1033  */
1034 int
1035 sctp_header_init_ipv6(sctp_t *sctp, int sleep)
1036 {
1037 	sctp_hdr_t	*sctph;
1038 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1039 
1040 	/*
1041 	 * This is a simple initialization. If there's
1042 	 * already a template, it should never be too small,
1043 	 * so reuse it. Otherwise, allocate space for the new one.
1044 	 * Ensure that there is enough space to "downgrade" the sctp_t
1045 	 * to an IPv4 sctp_t. This requires having space for a full load
1046 	 * of IPv4 options
1047 	 */
1048 	if (sctp->sctp_iphc6 != NULL) {
1049 		ASSERT(sctp->sctp_iphc6_len >=
1050 		    SCTP_MAX_COMBINED_HEADER_LENGTH);
1051 		bzero(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
1052 	} else {
1053 		sctp->sctp_iphc6_len = SCTP_MAX_COMBINED_HEADER_LENGTH;
1054 		sctp->sctp_iphc6 = kmem_zalloc(sctp->sctp_iphc_len, sleep);
1055 		if (sctp->sctp_iphc6 == NULL) {
1056 			sctp->sctp_iphc6_len = 0;
1057 			return (ENOMEM);
1058 		}
1059 	}
1060 	sctp->sctp_hdr6_len = IPV6_HDR_LEN + sizeof (sctp_hdr_t);
1061 	sctp->sctp_ip_hdr6_len = IPV6_HDR_LEN;
1062 	sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6;
1063 
1064 	/* Initialize the header template */
1065 
1066 	sctp->sctp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
1067 	sctp->sctp_ip6h->ip6_plen = ntohs(sizeof (sctp_hdr_t));
1068 	sctp->sctp_ip6h->ip6_nxt = IPPROTO_SCTP;
1069 	sctp->sctp_ip6h->ip6_hops = sctps->sctps_ipv6_hoplimit;
1070 
1071 	sctph = (sctp_hdr_t *)(sctp->sctp_iphc6 + IPV6_HDR_LEN);
1072 	sctp->sctp_sctph6 = sctph;
1073 
1074 	return (0);
1075 }
1076 
1077 static int
1078 sctp_v4_label(sctp_t *sctp)
1079 {
1080 	uchar_t optbuf[IP_MAX_OPT_LENGTH];
1081 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
1082 	int added;
1083 
1084 	if (tsol_compute_label(cr, sctp->sctp_ipha->ipha_dst, optbuf,
1085 	    sctp->sctp_sctps->sctps_netstack->netstack_ip) != 0)
1086 		return (EACCES);
1087 
1088 	added = tsol_remove_secopt(sctp->sctp_ipha, sctp->sctp_hdr_len);
1089 	if (added == -1)
1090 		return (EACCES);
1091 	sctp->sctp_hdr_len += added;
1092 	sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph + added);
1093 	sctp->sctp_ip_hdr_len += added;
1094 	if ((sctp->sctp_v4label_len = optbuf[IPOPT_OLEN]) != 0) {
1095 		sctp->sctp_v4label_len = (sctp->sctp_v4label_len + 3) & ~3;
1096 		added = tsol_prepend_option(optbuf, sctp->sctp_ipha,
1097 		    sctp->sctp_hdr_len);
1098 		if (added == -1)
1099 			return (EACCES);
1100 		sctp->sctp_hdr_len += added;
1101 		sctp->sctp_sctph = (sctp_hdr_t *)((uchar_t *)sctp->sctp_sctph +
1102 		    added);
1103 		sctp->sctp_ip_hdr_len += added;
1104 	}
1105 	return (0);
1106 }
1107 
1108 static int
1109 sctp_v6_label(sctp_t *sctp)
1110 {
1111 	uchar_t optbuf[TSOL_MAX_IPV6_OPTION];
1112 	const cred_t *cr = CONN_CRED(sctp->sctp_connp);
1113 
1114 	if (tsol_compute_label_v6(cr, &sctp->sctp_ip6h->ip6_dst, optbuf,
1115 	    sctp->sctp_sctps->sctps_netstack->netstack_ip) != 0)
1116 		return (EACCES);
1117 	if (tsol_update_sticky(&sctp->sctp_sticky_ipp, &sctp->sctp_v6label_len,
1118 	    optbuf) != 0)
1119 		return (EACCES);
1120 	if (sctp_build_hdrs(sctp) != 0)
1121 		return (EACCES);
1122 	return (0);
1123 }
1124 
1125 /*
1126  * XXX implement more sophisticated logic
1127  */
1128 int
1129 sctp_set_hdraddrs(sctp_t *sctp)
1130 {
1131 	sctp_faddr_t *fp;
1132 	int gotv4 = 0;
1133 	int gotv6 = 0;
1134 
1135 	ASSERT(sctp->sctp_faddrs != NULL);
1136 	ASSERT(sctp->sctp_nsaddrs > 0);
1137 
1138 	/* Set up using the primary first */
1139 	if (IN6_IS_ADDR_V4MAPPED(&sctp->sctp_primary->faddr)) {
1140 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->faddr,
1141 		    sctp->sctp_ipha->ipha_dst);
1142 		/* saddr may be unspec; make_mp() will handle this */
1143 		IN6_V4MAPPED_TO_IPADDR(&sctp->sctp_primary->saddr,
1144 		    sctp->sctp_ipha->ipha_src);
1145 		if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
1146 			gotv4 = 1;
1147 			if (sctp->sctp_ipversion == IPV4_VERSION) {
1148 				goto copyports;
1149 			}
1150 		}
1151 	} else {
1152 		sctp->sctp_ip6h->ip6_dst = sctp->sctp_primary->faddr;
1153 		/* saddr may be unspec; make_mp() will handle this */
1154 		sctp->sctp_ip6h->ip6_src = sctp->sctp_primary->saddr;
1155 		if (!is_system_labeled() || sctp_v6_label(sctp) == 0)
1156 			gotv6 = 1;
1157 	}
1158 
1159 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
1160 		if (!gotv4 && IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
1161 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr,
1162 			    sctp->sctp_ipha->ipha_dst);
1163 			/* copy in the faddr_t's saddr */
1164 			IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
1165 			    sctp->sctp_ipha->ipha_src);
1166 			if (!is_system_labeled() || sctp_v4_label(sctp) == 0) {
1167 				gotv4 = 1;
1168 				if (sctp->sctp_ipversion == IPV4_VERSION ||
1169 				    gotv6) {
1170 					break;
1171 				}
1172 			}
1173 		} else if (!gotv6 && !IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
1174 			sctp->sctp_ip6h->ip6_dst = fp->faddr;
1175 			/* copy in the faddr_t's saddr */
1176 			sctp->sctp_ip6h->ip6_src = fp->saddr;
1177 			if (!is_system_labeled() || sctp_v6_label(sctp) == 0) {
1178 				gotv6 = 1;
1179 				if (gotv4)
1180 					break;
1181 			}
1182 		}
1183 	}
1184 
1185 copyports:
1186 	if (!gotv4 && !gotv6)
1187 		return (EACCES);
1188 
1189 	/* copy in the ports for good measure */
1190 	sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
1191 	sctp->sctp_sctph->sh_dport = sctp->sctp_fport;
1192 
1193 	sctp->sctp_sctph6->sh_sport = sctp->sctp_lport;
1194 	sctp->sctp_sctph6->sh_dport = sctp->sctp_fport;
1195 	return (0);
1196 }
1197 
1198 /*
1199  * got_errchunk is set B_TRUE only if called from validate_init_params(), when
1200  * an ERROR chunk is already prepended the size of which needs updating for
1201  * additional unrecognized parameters. Other callers either prepend the ERROR
1202  * chunk with the correct size after calling this function, or they are calling
1203  * to add an invalid parameter to an INIT_ACK chunk, in that case no ERROR chunk
1204  * exists, the CAUSE blocks go into the INIT_ACK directly.
1205  *
1206  * *errmp will be non-NULL both when adding an additional CAUSE block to an
1207  * existing prepended COOKIE ERROR chunk (processing params of an INIT_ACK),
1208  * and when adding unrecognized parameters after the first, to an INIT_ACK
1209  * (processing params of an INIT chunk).
1210  */
1211 void
1212 sctp_add_unrec_parm(sctp_parm_hdr_t *uph, mblk_t **errmp,
1213     boolean_t got_errchunk)
1214 {
1215 	mblk_t *mp;
1216 	sctp_parm_hdr_t *ph;
1217 	size_t len;
1218 	int pad;
1219 	sctp_chunk_hdr_t *ecp;
1220 
1221 	len = sizeof (*ph) + ntohs(uph->sph_len);
1222 	if ((pad = len % SCTP_ALIGN) != 0) {
1223 		pad = SCTP_ALIGN - pad;
1224 		len += pad;
1225 	}
1226 	mp = allocb(len, BPRI_MED);
1227 	if (mp == NULL) {
1228 		return;
1229 	}
1230 
1231 	ph = (sctp_parm_hdr_t *)(mp->b_rptr);
1232 	ph->sph_type = htons(PARM_UNRECOGNIZED);
1233 	ph->sph_len = htons(len - pad);
1234 
1235 	/* copy in the unrecognized parameter */
1236 	bcopy(uph, ph + 1, ntohs(uph->sph_len));
1237 
1238 	if (pad != 0)
1239 		bzero((mp->b_rptr + len - pad), pad);
1240 
1241 	mp->b_wptr = mp->b_rptr + len;
1242 	if (*errmp != NULL) {
1243 		/*
1244 		 * Update total length if an ERROR chunk, then link
1245 		 * this CAUSE block to the possible chain of CAUSE
1246 		 * blocks attached to the ERROR chunk or INIT_ACK
1247 		 * being created.
1248 		 */
1249 		if (got_errchunk) {
1250 			/* ERROR chunk already prepended */
1251 			ecp = (sctp_chunk_hdr_t *)((*errmp)->b_rptr);
1252 			ecp->sch_len = htons(ntohs(ecp->sch_len) + len);
1253 		}
1254 		linkb(*errmp, mp);
1255 	} else {
1256 		*errmp = mp;
1257 	}
1258 }
1259 
1260 /*
1261  * o Bounds checking
1262  * o Updates remaining
1263  * o Checks alignment
1264  */
1265 sctp_parm_hdr_t *
1266 sctp_next_parm(sctp_parm_hdr_t *current, ssize_t *remaining)
1267 {
1268 	int pad;
1269 	uint16_t len;
1270 
1271 	len = ntohs(current->sph_len);
1272 	*remaining -= len;
1273 	if (*remaining < sizeof (*current) || len < sizeof (*current)) {
1274 		return (NULL);
1275 	}
1276 	if ((pad = len & (SCTP_ALIGN - 1)) != 0) {
1277 		pad = SCTP_ALIGN - pad;
1278 		*remaining -= pad;
1279 	}
1280 	/*LINTED pointer cast may result in improper alignment*/
1281 	current = (sctp_parm_hdr_t *)((char *)current + len + pad);
1282 	return (current);
1283 }
1284 
1285 /*
1286  * Sets the address parameters given in the INIT chunk into sctp's
1287  * faddrs; if psctp is non-NULL, copies psctp's saddrs. If there are
1288  * no address parameters in the INIT chunk, a single faddr is created
1289  * from the ip hdr at the beginning of pkt.
1290  * If there already are existing addresses hanging from sctp, merge
1291  * them in, if the old info contains addresses which are not present
1292  * in this new info, get rid of them, and clean the pointers if there's
1293  * messages which have this as their target address.
1294  *
1295  * We also re-adjust the source address list here since the list may
1296  * contain more than what is actually part of the association. If
1297  * we get here from sctp_send_cookie_echo(), we are on the active
1298  * side and psctp will be NULL and ich will be the INIT-ACK chunk.
1299  * If we get here from sctp_accept_comm(), ich will be the INIT chunk
1300  * and psctp will the listening endpoint.
1301  *
1302  * INIT processing: When processing the INIT we inherit the src address
1303  * list from the listener. For a loopback or linklocal association, we
1304  * delete the list and just take the address from the IP header (since
1305  * that's how we created the INIT-ACK). Additionally, for loopback we
1306  * ignore the address params in the INIT. For determining which address
1307  * types were sent in the INIT-ACK we follow the same logic as in
1308  * creating the INIT-ACK. We delete addresses of the type that are not
1309  * supported by the peer.
1310  *
1311  * INIT-ACK processing: When processing the INIT-ACK since we had not
1312  * included addr params for loopback or linklocal addresses when creating
1313  * the INIT, we just use the address from the IP header. Further, for
1314  * loopback we ignore the addr param list. We mark addresses of the
1315  * type not supported by the peer as unconfirmed.
1316  *
1317  * In case of INIT processing we look for supported address types in the
1318  * supported address param, if present. In both cases the address type in
1319  * the IP header is supported as well as types for addresses in the param
1320  * list, if any.
1321  *
1322  * Once we have the supported address types sctp_check_saddr() runs through
1323  * the source address list and deletes or marks as unconfirmed address of
1324  * types not supported by the peer.
1325  *
1326  * Returns 0 on success, sys errno on failure
1327  */
1328 int
1329 sctp_get_addrparams(sctp_t *sctp, sctp_t *psctp, mblk_t *pkt,
1330     sctp_chunk_hdr_t *ich, uint_t *sctp_options)
1331 {
1332 	sctp_init_chunk_t	*init;
1333 	ipha_t			*iph;
1334 	ip6_t			*ip6h;
1335 	in6_addr_t		hdrsaddr[1];
1336 	in6_addr_t		hdrdaddr[1];
1337 	sctp_parm_hdr_t		*ph;
1338 	ssize_t			remaining;
1339 	int			isv4;
1340 	int			err;
1341 	sctp_faddr_t		*fp;
1342 	int			supp_af = 0;
1343 	boolean_t		check_saddr = B_TRUE;
1344 	in6_addr_t		curaddr;
1345 	sctp_stack_t		*sctps = sctp->sctp_sctps;
1346 
1347 	if (sctp_options != NULL)
1348 		*sctp_options = 0;
1349 
1350 	/* extract the address from the IP header */
1351 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1352 	if (isv4) {
1353 		iph = (ipha_t *)pkt->b_rptr;
1354 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdrsaddr);
1355 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_dst, hdrdaddr);
1356 		supp_af |= PARM_SUPP_V4;
1357 	} else {
1358 		ip6h = (ip6_t *)pkt->b_rptr;
1359 		hdrsaddr[0] = ip6h->ip6_src;
1360 		hdrdaddr[0] = ip6h->ip6_dst;
1361 		supp_af |= PARM_SUPP_V6;
1362 	}
1363 
1364 	/*
1365 	 * Unfortunately, we can't delay this because adding an faddr
1366 	 * looks for the presence of the source address (from the ire
1367 	 * for the faddr) in the source address list. We could have
1368 	 * delayed this if, say, this was a loopback/linklocal connection.
1369 	 * Now, we just end up nuking this list and taking the addr from
1370 	 * the IP header for loopback/linklocal.
1371 	 */
1372 	if (psctp != NULL && psctp->sctp_nsaddrs > 0) {
1373 		ASSERT(sctp->sctp_nsaddrs == 0);
1374 
1375 		err = sctp_dup_saddrs(psctp, sctp, KM_NOSLEEP);
1376 		if (err != 0)
1377 			return (err);
1378 	}
1379 	/*
1380 	 * We will add the faddr before parsing the address list as this
1381 	 * might be a loopback connection and we would not have to
1382 	 * go through the list.
1383 	 *
1384 	 * Make sure the header's addr is in the list
1385 	 */
1386 	fp = sctp_lookup_faddr(sctp, hdrsaddr);
1387 	if (fp == NULL) {
1388 		/* not included; add it now */
1389 		err = sctp_add_faddr(sctp, hdrsaddr, KM_NOSLEEP, B_TRUE);
1390 		if (err != 0)
1391 			return (err);
1392 
1393 		/* sctp_faddrs will be the hdr addr */
1394 		fp = sctp->sctp_faddrs;
1395 	}
1396 	/* make the header addr the primary */
1397 
1398 	if (cl_sctp_assoc_change != NULL && psctp == NULL)
1399 		curaddr = sctp->sctp_current->faddr;
1400 
1401 	sctp->sctp_primary = fp;
1402 	sctp->sctp_current = fp;
1403 	sctp->sctp_mss = fp->sfa_pmss;
1404 
1405 	/* For loopback connections & linklocal get address from the header */
1406 	if (sctp->sctp_loopback || sctp->sctp_linklocal) {
1407 		if (sctp->sctp_nsaddrs != 0)
1408 			sctp_free_saddrs(sctp);
1409 		if ((err = sctp_saddr_add_addr(sctp, hdrdaddr, 0)) != 0)
1410 			return (err);
1411 		/* For loopback ignore address list */
1412 		if (sctp->sctp_loopback)
1413 			return (0);
1414 		check_saddr = B_FALSE;
1415 	}
1416 
1417 	/* Walk the params in the INIT [ACK], pulling out addr params */
1418 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
1419 	    sizeof (sctp_init_chunk_t);
1420 	if (remaining < sizeof (*ph)) {
1421 		if (check_saddr) {
1422 			sctp_check_saddr(sctp, supp_af, psctp == NULL ?
1423 			    B_FALSE : B_TRUE, hdrdaddr);
1424 		}
1425 		ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1426 		return (0);
1427 	}
1428 
1429 	init = (sctp_init_chunk_t *)(ich + 1);
1430 	ph = (sctp_parm_hdr_t *)(init + 1);
1431 
1432 	/* params will have already been byteordered when validating */
1433 	while (ph != NULL) {
1434 		if (ph->sph_type == htons(PARM_SUPP_ADDRS)) {
1435 			int		plen;
1436 			uint16_t	*p;
1437 			uint16_t	addrtype;
1438 
1439 			ASSERT(psctp != NULL);
1440 			plen = ntohs(ph->sph_len);
1441 			p = (uint16_t *)(ph + 1);
1442 			while (plen > 0) {
1443 				addrtype = ntohs(*p);
1444 				switch (addrtype) {
1445 					case PARM_ADDR6:
1446 						supp_af |= PARM_SUPP_V6;
1447 						break;
1448 					case PARM_ADDR4:
1449 						supp_af |= PARM_SUPP_V4;
1450 						break;
1451 					default:
1452 						break;
1453 				}
1454 				p++;
1455 				plen -= sizeof (*p);
1456 			}
1457 		} else if (ph->sph_type == htons(PARM_ADDR4)) {
1458 			if (remaining >= PARM_ADDR4_LEN) {
1459 				in6_addr_t addr;
1460 				ipaddr_t ta;
1461 
1462 				supp_af |= PARM_SUPP_V4;
1463 				/*
1464 				 * Screen out broad/multicasts & loopback.
1465 				 * If the endpoint only accepts v6 address,
1466 				 * go to the next one.
1467 				 *
1468 				 * Subnet broadcast check is done in
1469 				 * sctp_add_faddr().  If the address is
1470 				 * a broadcast address, it won't be added.
1471 				 */
1472 				bcopy(ph + 1, &ta, sizeof (ta));
1473 				if (ta == 0 ||
1474 				    ta == INADDR_BROADCAST ||
1475 				    ta == htonl(INADDR_LOOPBACK) ||
1476 				    CLASSD(ta) ||
1477 				    sctp->sctp_connp->conn_ipv6_v6only) {
1478 					goto next;
1479 				}
1480 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
1481 				    (ph + 1), &addr);
1482 
1483 				/* Check for duplicate. */
1484 				if (sctp_lookup_faddr(sctp, &addr) != NULL)
1485 					goto next;
1486 
1487 				/* OK, add it to the faddr set */
1488 				err = sctp_add_faddr(sctp, &addr, KM_NOSLEEP,
1489 				    B_FALSE);
1490 				/* Something is wrong...  Try the next one. */
1491 				if (err != 0)
1492 					goto next;
1493 			}
1494 		} else if (ph->sph_type == htons(PARM_ADDR6) &&
1495 		    sctp->sctp_family == AF_INET6) {
1496 			/* An v4 socket should not take v6 addresses. */
1497 			if (remaining >= PARM_ADDR6_LEN) {
1498 				in6_addr_t *addr6;
1499 
1500 				supp_af |= PARM_SUPP_V6;
1501 				addr6 = (in6_addr_t *)(ph + 1);
1502 				/*
1503 				 * Screen out link locals, mcast, loopback
1504 				 * and bogus v6 address.
1505 				 */
1506 				if (IN6_IS_ADDR_LINKLOCAL(addr6) ||
1507 				    IN6_IS_ADDR_MULTICAST(addr6) ||
1508 				    IN6_IS_ADDR_LOOPBACK(addr6) ||
1509 				    IN6_IS_ADDR_V4MAPPED(addr6)) {
1510 					goto next;
1511 				}
1512 				/* Check for duplicate. */
1513 				if (sctp_lookup_faddr(sctp, addr6) != NULL)
1514 					goto next;
1515 
1516 				err = sctp_add_faddr(sctp,
1517 				    (in6_addr_t *)(ph + 1), KM_NOSLEEP,
1518 				    B_FALSE);
1519 				/* Something is wrong...  Try the next one. */
1520 				if (err != 0)
1521 					goto next;
1522 			}
1523 		} else if (ph->sph_type == htons(PARM_FORWARD_TSN)) {
1524 			if (sctp_options != NULL)
1525 				*sctp_options |= SCTP_PRSCTP_OPTION;
1526 		} /* else; skip */
1527 
1528 next:
1529 		ph = sctp_next_parm(ph, &remaining);
1530 	}
1531 	if (check_saddr) {
1532 		sctp_check_saddr(sctp, supp_af, psctp == NULL ? B_FALSE :
1533 		    B_TRUE, hdrdaddr);
1534 	}
1535 	ASSERT(sctp_saddr_lookup(sctp, hdrdaddr, 0) != NULL);
1536 	/*
1537 	 * We have the right address list now, update clustering's
1538 	 * knowledge because when we sent the INIT we had just added
1539 	 * the address the INIT was sent to.
1540 	 */
1541 	if (psctp == NULL && cl_sctp_assoc_change != NULL) {
1542 		uchar_t	*alist;
1543 		size_t	asize;
1544 		uchar_t	*dlist;
1545 		size_t	dsize;
1546 
1547 		asize = sizeof (in6_addr_t) * sctp->sctp_nfaddrs;
1548 		alist = kmem_alloc(asize, KM_NOSLEEP);
1549 		if (alist == NULL) {
1550 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1551 			return (ENOMEM);
1552 		}
1553 		/*
1554 		 * Just include the address the INIT was sent to in the
1555 		 * delete list and send the entire faddr list. We could
1556 		 * do it differently (i.e include all the addresses in the
1557 		 * add list even if it contains the original address OR
1558 		 * remove the original address from the add list etc.), but
1559 		 * this seems reasonable enough.
1560 		 */
1561 		dsize = sizeof (in6_addr_t);
1562 		dlist = kmem_alloc(dsize, KM_NOSLEEP);
1563 		if (dlist == NULL) {
1564 			kmem_free(alist, asize);
1565 			SCTP_KSTAT(sctps, sctp_cl_assoc_change);
1566 			return (ENOMEM);
1567 		}
1568 		bcopy(&curaddr, dlist, sizeof (curaddr));
1569 		sctp_get_faddr_list(sctp, alist, asize);
1570 		(*cl_sctp_assoc_change)(sctp->sctp_family, alist, asize,
1571 		    sctp->sctp_nfaddrs, dlist, dsize, 1, SCTP_CL_PADDR,
1572 		    (cl_sctp_handle_t)sctp);
1573 		/* alist and dlist will be freed by the clustering module */
1574 	}
1575 	return (0);
1576 }
1577 
1578 /*
1579  * Returns 0 if the check failed and the restart should be refused,
1580  * 1 if the check succeeded.
1581  */
1582 int
1583 sctp_secure_restart_check(mblk_t *pkt, sctp_chunk_hdr_t *ich, uint32_t ports,
1584     int sleep, sctp_stack_t *sctps)
1585 {
1586 	sctp_faddr_t *fp, *fphead = NULL;
1587 	sctp_parm_hdr_t *ph;
1588 	ssize_t remaining;
1589 	int isv4;
1590 	ipha_t *iph;
1591 	ip6_t *ip6h;
1592 	in6_addr_t hdraddr[1];
1593 	int retval = 0;
1594 	sctp_tf_t *tf;
1595 	sctp_t *sctp;
1596 	int compres;
1597 	sctp_init_chunk_t *init;
1598 	int nadded = 0;
1599 
1600 	/* extract the address from the IP header */
1601 	isv4 = (IPH_HDR_VERSION(pkt->b_rptr) == IPV4_VERSION);
1602 	if (isv4) {
1603 		iph = (ipha_t *)pkt->b_rptr;
1604 		IN6_IPADDR_TO_V4MAPPED(iph->ipha_src, hdraddr);
1605 	} else {
1606 		ip6h = (ip6_t *)pkt->b_rptr;
1607 		hdraddr[0] = ip6h->ip6_src;
1608 	}
1609 
1610 	/* Walk the params in the INIT [ACK], pulling out addr params */
1611 	remaining = ntohs(ich->sch_len) - sizeof (*ich) -
1612 	    sizeof (sctp_init_chunk_t);
1613 	if (remaining < sizeof (*ph)) {
1614 		/* no parameters; restart OK */
1615 		return (1);
1616 	}
1617 	init = (sctp_init_chunk_t *)(ich + 1);
1618 	ph = (sctp_parm_hdr_t *)(init + 1);
1619 
1620 	while (ph != NULL) {
1621 		sctp_faddr_t *fpa = NULL;
1622 
1623 		/* params will have already been byteordered when validating */
1624 		if (ph->sph_type == htons(PARM_ADDR4)) {
1625 			if (remaining >= PARM_ADDR4_LEN) {
1626 				in6_addr_t addr;
1627 				IN6_INADDR_TO_V4MAPPED((struct in_addr *)
1628 				    (ph + 1), &addr);
1629 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
1630 				    sleep);
1631 				if (fpa == NULL) {
1632 					goto done;
1633 				}
1634 				bzero(fpa, sizeof (*fpa));
1635 				fpa->faddr = addr;
1636 				fpa->next = NULL;
1637 			}
1638 		} else if (ph->sph_type == htons(PARM_ADDR6)) {
1639 			if (remaining >= PARM_ADDR6_LEN) {
1640 				fpa = kmem_cache_alloc(sctp_kmem_faddr_cache,
1641 				    sleep);
1642 				if (fpa == NULL) {
1643 					goto done;
1644 				}
1645 				bzero(fpa, sizeof (*fpa));
1646 				bcopy(ph + 1, &fpa->faddr,
1647 				    sizeof (fpa->faddr));
1648 				fpa->next = NULL;
1649 			}
1650 		}
1651 		/* link in the new addr, if it was an addr param */
1652 		if (fpa != NULL) {
1653 			if (fphead == NULL) {
1654 				fphead = fpa;
1655 			} else {
1656 				fpa->next = fphead;
1657 				fphead = fpa;
1658 			}
1659 		}
1660 
1661 		ph = sctp_next_parm(ph, &remaining);
1662 	}
1663 
1664 	if (fphead == NULL) {
1665 		/* no addr parameters; restart OK */
1666 		return (1);
1667 	}
1668 
1669 	/*
1670 	 * got at least one; make sure the header's addr is
1671 	 * in the list
1672 	 */
1673 	fp = sctp_lookup_faddr_nosctp(fphead, hdraddr);
1674 	if (fp == NULL) {
1675 		/* not included; add it now */
1676 		fp = kmem_cache_alloc(sctp_kmem_faddr_cache, sleep);
1677 		if (fp == NULL) {
1678 			goto done;
1679 		}
1680 		bzero(fp, sizeof (*fp));
1681 		fp->faddr = *hdraddr;
1682 		fp->next = fphead;
1683 		fphead = fp;
1684 	}
1685 
1686 	/*
1687 	 * Now, we can finally do the check: For each sctp instance
1688 	 * on the hash line for ports, compare its faddr set against
1689 	 * the new one. If the new one is a strict subset of any
1690 	 * existing sctp's faddrs, the restart is OK. However, if there
1691 	 * is an overlap, this could be an attack, so return failure.
1692 	 * If all sctp's faddrs are disjoint, this is a legitimate new
1693 	 * association.
1694 	 */
1695 	tf = &(sctps->sctps_conn_fanout[SCTP_CONN_HASH(sctps, ports)]);
1696 	mutex_enter(&tf->tf_lock);
1697 
1698 	for (sctp = tf->tf_sctp; sctp; sctp = sctp->sctp_conn_hash_next) {
1699 		if (ports != sctp->sctp_ports) {
1700 			continue;
1701 		}
1702 		compres = sctp_compare_faddrsets(fphead, sctp->sctp_faddrs);
1703 		if (compres <= SCTP_ADDR_SUBSET) {
1704 			retval = 1;
1705 			mutex_exit(&tf->tf_lock);
1706 			goto done;
1707 		}
1708 		if (compres == SCTP_ADDR_OVERLAP) {
1709 			dprint(1,
1710 			    ("new assoc from %x:%x:%x:%x overlaps with %p\n",
1711 			    SCTP_PRINTADDR(*hdraddr), (void *)sctp));
1712 			/*
1713 			 * While we still hold the lock, we need to
1714 			 * figure out which addresses have been
1715 			 * added so we can include them in the abort
1716 			 * we will send back. Since these faddrs will
1717 			 * never be used, we overload the rto field
1718 			 * here, setting it to 0 if the address was
1719 			 * not added, 1 if it was added.
1720 			 */
1721 			for (fp = fphead; fp; fp = fp->next) {
1722 				if (sctp_lookup_faddr(sctp, &fp->faddr)) {
1723 					fp->rto = 0;
1724 				} else {
1725 					fp->rto = 1;
1726 					nadded++;
1727 				}
1728 			}
1729 			mutex_exit(&tf->tf_lock);
1730 			goto done;
1731 		}
1732 	}
1733 	mutex_exit(&tf->tf_lock);
1734 
1735 	/* All faddrs are disjoint; legit new association */
1736 	retval = 1;
1737 
1738 done:
1739 	/* If are attempted adds, send back an abort listing the addrs */
1740 	if (nadded > 0) {
1741 		void *dtail;
1742 		size_t dlen;
1743 
1744 		dtail = kmem_alloc(PARM_ADDR6_LEN * nadded, KM_NOSLEEP);
1745 		if (dtail == NULL) {
1746 			goto cleanup;
1747 		}
1748 
1749 		ph = dtail;
1750 		dlen = 0;
1751 		for (fp = fphead; fp; fp = fp->next) {
1752 			if (fp->rto == 0) {
1753 				continue;
1754 			}
1755 			if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
1756 				ipaddr_t addr4;
1757 
1758 				ph->sph_type = htons(PARM_ADDR4);
1759 				ph->sph_len = htons(PARM_ADDR4_LEN);
1760 				IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
1761 				ph++;
1762 				bcopy(&addr4, ph, sizeof (addr4));
1763 				ph = (sctp_parm_hdr_t *)
1764 				    ((char *)ph + sizeof (addr4));
1765 				dlen += PARM_ADDR4_LEN;
1766 			} else {
1767 				ph->sph_type = htons(PARM_ADDR6);
1768 				ph->sph_len = htons(PARM_ADDR6_LEN);
1769 				ph++;
1770 				bcopy(&fp->faddr, ph, sizeof (fp->faddr));
1771 				ph = (sctp_parm_hdr_t *)
1772 				    ((char *)ph + sizeof (fp->faddr));
1773 				dlen += PARM_ADDR6_LEN;
1774 			}
1775 		}
1776 
1777 		/* Send off the abort */
1778 		sctp_send_abort(sctp, sctp_init2vtag(ich),
1779 		    SCTP_ERR_RESTART_NEW_ADDRS, dtail, dlen, pkt, 0, B_TRUE);
1780 
1781 		kmem_free(dtail, PARM_ADDR6_LEN * nadded);
1782 	}
1783 
1784 cleanup:
1785 	/* Clean up */
1786 	if (fphead) {
1787 		sctp_faddr_t *fpn;
1788 		for (fp = fphead; fp; fp = fpn) {
1789 			fpn = fp->next;
1790 			kmem_cache_free(sctp_kmem_faddr_cache, fp);
1791 		}
1792 	}
1793 
1794 	return (retval);
1795 }
1796 
1797 /*
1798  * Reset any state related to transmitted chunks.
1799  */
1800 void
1801 sctp_congest_reset(sctp_t *sctp)
1802 {
1803 	sctp_faddr_t	*fp;
1804 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1805 	mblk_t		*mp;
1806 
1807 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
1808 		fp->ssthresh = sctps->sctps_initial_mtu;
1809 		SET_CWND(fp, fp->sfa_pmss, sctps->sctps_slow_start_initial);
1810 		fp->suna = 0;
1811 		fp->pba = 0;
1812 	}
1813 	/*
1814 	 * Clean up the transmit list as well since we have reset accounting
1815 	 * on all the fps. Send event upstream, if required.
1816 	 */
1817 	while ((mp = sctp->sctp_xmit_head) != NULL) {
1818 		sctp->sctp_xmit_head = mp->b_next;
1819 		mp->b_next = NULL;
1820 		if (sctp->sctp_xmit_head != NULL)
1821 			sctp->sctp_xmit_head->b_prev = NULL;
1822 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
1823 	}
1824 	sctp->sctp_xmit_head = NULL;
1825 	sctp->sctp_xmit_tail = NULL;
1826 	sctp->sctp_xmit_unacked = NULL;
1827 
1828 	sctp->sctp_unacked = 0;
1829 	/*
1830 	 * Any control message as well. We will clean-up this list as well.
1831 	 * This contains any pending ASCONF request that we have queued/sent.
1832 	 * If we do get an ACK we will just drop it. However, given that
1833 	 * we are restarting chances are we aren't going to get any.
1834 	 */
1835 	if (sctp->sctp_cxmit_list != NULL)
1836 		sctp_asconf_free_cxmit(sctp, NULL);
1837 	sctp->sctp_cxmit_list = NULL;
1838 	sctp->sctp_cchunk_pend = 0;
1839 
1840 	sctp->sctp_rexmitting = B_FALSE;
1841 	sctp->sctp_rxt_nxttsn = 0;
1842 	sctp->sctp_rxt_maxtsn = 0;
1843 
1844 	sctp->sctp_zero_win_probe = B_FALSE;
1845 }
1846 
1847 static void
1848 sctp_init_faddr(sctp_t *sctp, sctp_faddr_t *fp, in6_addr_t *addr,
1849     mblk_t *timer_mp)
1850 {
1851 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1852 
1853 	bcopy(addr, &fp->faddr, sizeof (*addr));
1854 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
1855 		fp->isv4 = 1;
1856 		/* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */
1857 		fp->sfa_pmss =
1858 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr_len) &
1859 		    ~(SCTP_ALIGN - 1);
1860 	} else {
1861 		fp->isv4 = 0;
1862 		fp->sfa_pmss =
1863 		    (sctps->sctps_initial_mtu - sctp->sctp_hdr6_len) &
1864 		    ~(SCTP_ALIGN - 1);
1865 	}
1866 	fp->cwnd = sctps->sctps_slow_start_initial * fp->sfa_pmss;
1867 	fp->rto = MIN(sctp->sctp_rto_initial, sctp->sctp_init_rto_max);
1868 	SCTP_MAX_RTO(sctp, fp);
1869 	fp->srtt = -1;
1870 	fp->rtt_updates = 0;
1871 	fp->strikes = 0;
1872 	fp->max_retr = sctp->sctp_pp_max_rxt;
1873 	/* Mark it as not confirmed. */
1874 	fp->state = SCTP_FADDRS_UNCONFIRMED;
1875 	fp->hb_interval = sctp->sctp_hb_interval;
1876 	fp->ssthresh = sctps->sctps_initial_ssthresh;
1877 	fp->suna = 0;
1878 	fp->pba = 0;
1879 	fp->acked = 0;
1880 	fp->lastactive = lbolt64;
1881 	fp->timer_mp = timer_mp;
1882 	fp->hb_pending = B_FALSE;
1883 	fp->hb_enabled = B_TRUE;
1884 	fp->df = 1;
1885 	fp->pmtu_discovered = 0;
1886 	fp->next = NULL;
1887 	fp->ire = NULL;
1888 	fp->T3expire = 0;
1889 	(void) random_get_pseudo_bytes((uint8_t *)&fp->hb_secret,
1890 	    sizeof (fp->hb_secret));
1891 	fp->hb_expiry = lbolt64;
1892 	fp->rxt_unacked = 0;
1893 
1894 	sctp_get_ire(sctp, fp);
1895 }
1896 
1897 /*ARGSUSED*/
1898 static int
1899 faddr_constructor(void *buf, void *arg, int flags)
1900 {
1901 	sctp_faddr_t *fp = buf;
1902 
1903 	fp->timer_mp = NULL;
1904 	fp->timer_running = 0;
1905 
1906 	fp->rc_timer_mp = NULL;
1907 	fp->rc_timer_running = 0;
1908 
1909 	return (0);
1910 }
1911 
1912 /*ARGSUSED*/
1913 static void
1914 faddr_destructor(void *buf, void *arg)
1915 {
1916 	sctp_faddr_t *fp = buf;
1917 
1918 	ASSERT(fp->timer_mp == NULL);
1919 	ASSERT(fp->timer_running == 0);
1920 
1921 	ASSERT(fp->rc_timer_mp == NULL);
1922 	ASSERT(fp->rc_timer_running == 0);
1923 }
1924 
1925 void
1926 sctp_faddr_init(void)
1927 {
1928 	sctp_kmem_faddr_cache = kmem_cache_create("sctp_faddr_cache",
1929 	    sizeof (sctp_faddr_t), 0, faddr_constructor, faddr_destructor,
1930 	    NULL, NULL, NULL, 0);
1931 }
1932 
1933 void
1934 sctp_faddr_fini(void)
1935 {
1936 	kmem_cache_destroy(sctp_kmem_faddr_cache);
1937 }
1938