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