xref: /linux/net/sctp/sm_make_chunk.c (revision 9802123ceca818aa69284c615114ca55b9cc94a1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3  * (C) Copyright IBM Corp. 2001, 2004
4  * Copyright (c) 1999-2000 Cisco, Inc.
5  * Copyright (c) 1999-2001 Motorola, Inc.
6  * Copyright (c) 2001-2002 Intel Corp.
7  *
8  * This file is part of the SCTP kernel implementation
9  *
10  * These functions work with the state functions in sctp_sm_statefuns.c
11  * to implement the state operations.  These functions implement the
12  * steps which require modifying existing data structures.
13  *
14  * Please send any bug reports or fixes you make to the
15  * email address(es):
16  *    lksctp developers <linux-sctp@vger.kernel.org>
17  *
18  * Written or modified by:
19  *    La Monte H.P. Yarroll <piggy@acm.org>
20  *    Karl Knutson          <karl@athena.chicago.il.us>
21  *    C. Robin              <chris@hundredacre.ac.uk>
22  *    Jon Grimm             <jgrimm@us.ibm.com>
23  *    Xingang Guo           <xingang.guo@intel.com>
24  *    Dajiang Zhang	    <dajiang.zhang@nokia.com>
25  *    Sridhar Samudrala	    <sri@us.ibm.com>
26  *    Daisy Chang	    <daisyc@us.ibm.com>
27  *    Ardelle Fan	    <ardelle.fan@intel.com>
28  *    Kevin Gao             <kevin.gao@intel.com>
29  */
30 
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 
33 #include <crypto/utils.h>
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/ip.h>
37 #include <linux/ipv6.h>
38 #include <linux/net.h>
39 #include <linux/inet.h>
40 #include <linux/scatterlist.h>
41 #include <linux/slab.h>
42 #include <net/sock.h>
43 
44 #include <linux/skbuff.h>
45 #include <linux/random.h>	/* for get_random_bytes */
46 #include <net/sctp/sctp.h>
47 #include <net/sctp/sm.h>
48 
49 static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
50 					    __u8 type, __u8 flags, int paylen,
51 					    gfp_t gfp);
52 static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
53 					 __u8 flags, int paylen, gfp_t gfp);
54 static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
55 					   __u8 type, __u8 flags, int paylen,
56 					   gfp_t gfp);
57 static struct sctp_cookie_param *sctp_pack_cookie(
58 					const struct sctp_endpoint *ep,
59 					const struct sctp_association *asoc,
60 					const struct sctp_chunk *init_chunk,
61 					int *cookie_len,
62 					const __u8 *raw_addrs, int addrs_len);
63 static int sctp_process_param(struct sctp_association *asoc,
64 			      union sctp_params param,
65 			      const union sctp_addr *peer_addr,
66 			      gfp_t gfp);
67 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
68 			      const void *data);
69 
70 /* Control chunk destructor */
71 static void sctp_control_release_owner(struct sk_buff *skb)
72 {
73 	struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
74 
75 	if (chunk->shkey) {
76 		struct sctp_shared_key *shkey = chunk->shkey;
77 		struct sctp_association *asoc = chunk->asoc;
78 
79 		/* refcnt == 2 and !list_empty mean after this release, it's
80 		 * not being used anywhere, and it's time to notify userland
81 		 * that this shkey can be freed if it's been deactivated.
82 		 */
83 		if (shkey->deactivated && !list_empty(&shkey->key_list) &&
84 		    refcount_read(&shkey->refcnt) == 2) {
85 			struct sctp_ulpevent *ev;
86 
87 			ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
88 							SCTP_AUTH_FREE_KEY,
89 							GFP_KERNEL);
90 			if (ev)
91 				asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
92 		}
93 		sctp_auth_shkey_release(chunk->shkey);
94 	}
95 }
96 
97 void sctp_control_set_owner_w(struct sctp_chunk *chunk)
98 {
99 	struct sctp_association *asoc = chunk->asoc;
100 	struct sk_buff *skb = chunk->skb;
101 
102 	/* TODO: properly account for control chunks.
103 	 * To do it right we'll need:
104 	 *  1) endpoint if association isn't known.
105 	 *  2) proper memory accounting.
106 	 *
107 	 *  For now don't do anything for now.
108 	 */
109 	if (chunk->auth) {
110 		if (!chunk->shkey)
111 			chunk->shkey = asoc->shkey;
112 		sctp_auth_shkey_hold(chunk->shkey);
113 	}
114 	skb->sk = asoc ? asoc->base.sk : NULL;
115 	skb_shinfo(skb)->destructor_arg = chunk;
116 	skb->destructor = sctp_control_release_owner;
117 }
118 
119 /* RFC 2960 3.3.2 Initiation (INIT) (1)
120  *
121  * Note 2: The ECN capable field is reserved for future use of
122  * Explicit Congestion Notification.
123  */
124 static const struct sctp_paramhdr ecap_param = {
125 	SCTP_PARAM_ECN_CAPABLE,
126 	cpu_to_be16(sizeof(struct sctp_paramhdr)),
127 };
128 static const struct sctp_paramhdr prsctp_param = {
129 	SCTP_PARAM_FWD_TSN_SUPPORT,
130 	cpu_to_be16(sizeof(struct sctp_paramhdr)),
131 };
132 
133 /* A helper to initialize an op error inside a provided chunk, as most
134  * cause codes will be embedded inside an abort chunk.
135  */
136 int sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
137 		    size_t paylen)
138 {
139 	struct sctp_errhdr err;
140 	__u16 len;
141 
142 	/* Cause code constants are now defined in network order.  */
143 	err.cause = cause_code;
144 	len = sizeof(err) + paylen;
145 	err.length = htons(len);
146 
147 	if (skb_tailroom(chunk->skb) < len)
148 		return -ENOSPC;
149 
150 	chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(err), &err);
151 
152 	return 0;
153 }
154 
155 /* 3.3.2 Initiation (INIT) (1)
156  *
157  * This chunk is used to initiate a SCTP association between two
158  * endpoints. The format of the INIT chunk is shown below:
159  *
160  *     0                   1                   2                   3
161  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
162  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163  *    |   Type = 1    |  Chunk Flags  |      Chunk Length             |
164  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165  *    |                         Initiate Tag                          |
166  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167  *    |           Advertised Receiver Window Credit (a_rwnd)          |
168  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169  *    |  Number of Outbound Streams   |  Number of Inbound Streams    |
170  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
171  *    |                          Initial TSN                          |
172  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
173  *    \                                                               \
174  *    /              Optional/Variable-Length Parameters              /
175  *    \                                                               \
176  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
177  *
178  *
179  * The INIT chunk contains the following parameters. Unless otherwise
180  * noted, each parameter MUST only be included once in the INIT chunk.
181  *
182  * Fixed Parameters                     Status
183  * ----------------------------------------------
184  * Initiate Tag                        Mandatory
185  * Advertised Receiver Window Credit   Mandatory
186  * Number of Outbound Streams          Mandatory
187  * Number of Inbound Streams           Mandatory
188  * Initial TSN                         Mandatory
189  *
190  * Variable Parameters                  Status     Type Value
191  * -------------------------------------------------------------
192  * IPv4 Address (Note 1)               Optional    5
193  * IPv6 Address (Note 1)               Optional    6
194  * Cookie Preservative                 Optional    9
195  * Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
196  * Host Name Address (Note 3)          Optional    11
197  * Supported Address Types (Note 4)    Optional    12
198  */
199 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
200 				  const struct sctp_bind_addr *bp,
201 				  gfp_t gfp, int vparam_len)
202 {
203 	struct sctp_supported_ext_param ext_param;
204 	struct sctp_adaptation_ind_param aiparam;
205 	struct sctp_paramhdr *auth_chunks = NULL;
206 	struct sctp_paramhdr *auth_hmacs = NULL;
207 	struct sctp_supported_addrs_param sat;
208 	struct sctp_endpoint *ep = asoc->ep;
209 	struct sctp_chunk *retval = NULL;
210 	int num_types, addrs_len = 0;
211 	struct sctp_inithdr init;
212 	union sctp_params addrs;
213 	struct sctp_sock *sp;
214 	__u8 extensions[5];
215 	size_t chunksize;
216 	__be16 types[2];
217 	int num_ext = 0;
218 
219 	/* RFC 2960 3.3.2 Initiation (INIT) (1)
220 	 *
221 	 * Note 1: The INIT chunks can contain multiple addresses that
222 	 * can be IPv4 and/or IPv6 in any combination.
223 	 */
224 
225 	/* Convert the provided bind address list to raw format. */
226 	addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
227 
228 	init.init_tag		   = htonl(asoc->c.my_vtag);
229 	init.a_rwnd		   = htonl(asoc->rwnd);
230 	init.num_outbound_streams  = htons(asoc->c.sinit_num_ostreams);
231 	init.num_inbound_streams   = htons(asoc->c.sinit_max_instreams);
232 	init.initial_tsn	   = htonl(asoc->c.initial_tsn);
233 
234 	/* How many address types are needed? */
235 	sp = sctp_sk(asoc->base.sk);
236 	num_types = sp->pf->supported_addrs(sp, types);
237 
238 	chunksize = sizeof(init) + addrs_len;
239 	chunksize += SCTP_PAD4(SCTP_SAT_LEN(num_types));
240 
241 	if (asoc->ep->ecn_enable)
242 		chunksize += sizeof(ecap_param);
243 
244 	if (asoc->ep->prsctp_enable)
245 		chunksize += sizeof(prsctp_param);
246 
247 	/* ADDIP: Section 4.2.7:
248 	 *  An implementation supporting this extension [ADDIP] MUST list
249 	 *  the ASCONF,the ASCONF-ACK, and the AUTH  chunks in its INIT and
250 	 *  INIT-ACK parameters.
251 	 */
252 	if (asoc->ep->asconf_enable) {
253 		extensions[num_ext] = SCTP_CID_ASCONF;
254 		extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
255 		num_ext += 2;
256 	}
257 
258 	if (asoc->ep->reconf_enable) {
259 		extensions[num_ext] = SCTP_CID_RECONF;
260 		num_ext += 1;
261 	}
262 
263 	if (sp->adaptation_ind)
264 		chunksize += sizeof(aiparam);
265 
266 	if (asoc->ep->intl_enable) {
267 		extensions[num_ext] = SCTP_CID_I_DATA;
268 		num_ext += 1;
269 	}
270 
271 	chunksize += vparam_len;
272 
273 	/* Account for AUTH related parameters */
274 	if (ep->auth_enable) {
275 		/* Add random parameter length*/
276 		chunksize += sizeof(asoc->c.auth_random);
277 
278 		/* Add HMACS parameter length if any were defined */
279 		auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
280 		if (auth_hmacs->length)
281 			chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
282 		else
283 			auth_hmacs = NULL;
284 
285 		/* Add CHUNKS parameter length */
286 		auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
287 		if (auth_chunks->length)
288 			chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
289 		else
290 			auth_chunks = NULL;
291 
292 		extensions[num_ext] = SCTP_CID_AUTH;
293 		num_ext += 1;
294 	}
295 
296 	/* If we have any extensions to report, account for that */
297 	if (num_ext)
298 		chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
299 
300 	/* RFC 2960 3.3.2 Initiation (INIT) (1)
301 	 *
302 	 * Note 3: An INIT chunk MUST NOT contain more than one Host
303 	 * Name address parameter. Moreover, the sender of the INIT
304 	 * MUST NOT combine any other address types with the Host Name
305 	 * address in the INIT. The receiver of INIT MUST ignore any
306 	 * other address types if the Host Name address parameter is
307 	 * present in the received INIT chunk.
308 	 *
309 	 * PLEASE DO NOT FIXME [This version does not support Host Name.]
310 	 */
311 
312 	retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
313 	if (!retval)
314 		goto nodata;
315 
316 	retval->subh.init_hdr =
317 		sctp_addto_chunk(retval, sizeof(init), &init);
318 	retval->param_hdr.v =
319 		sctp_addto_chunk(retval, addrs_len, addrs.v);
320 
321 	/* RFC 2960 3.3.2 Initiation (INIT) (1)
322 	 *
323 	 * Note 4: This parameter, when present, specifies all the
324 	 * address types the sending endpoint can support. The absence
325 	 * of this parameter indicates that the sending endpoint can
326 	 * support any address type.
327 	 */
328 	sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
329 	sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
330 	sctp_addto_chunk(retval, sizeof(sat), &sat);
331 	sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
332 
333 	if (asoc->ep->ecn_enable)
334 		sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
335 
336 	/* Add the supported extensions parameter.  Be nice and add this
337 	 * fist before addiding the parameters for the extensions themselves
338 	 */
339 	if (num_ext) {
340 		ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
341 		ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
342 		sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
343 		sctp_addto_param(retval, num_ext, extensions);
344 	}
345 
346 	if (asoc->ep->prsctp_enable)
347 		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
348 
349 	if (sp->adaptation_ind) {
350 		aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
351 		aiparam.param_hdr.length = htons(sizeof(aiparam));
352 		aiparam.adaptation_ind = htonl(sp->adaptation_ind);
353 		sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
354 	}
355 
356 	/* Add SCTP-AUTH chunks to the parameter list */
357 	if (ep->auth_enable) {
358 		sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
359 				 asoc->c.auth_random);
360 		if (auth_hmacs)
361 			sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
362 					auth_hmacs);
363 		if (auth_chunks)
364 			sctp_addto_chunk(retval, ntohs(auth_chunks->length),
365 					auth_chunks);
366 	}
367 nodata:
368 	kfree(addrs.v);
369 	return retval;
370 }
371 
372 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
373 				      const struct sctp_chunk *chunk,
374 				      gfp_t gfp, int unkparam_len)
375 {
376 	struct sctp_supported_ext_param ext_param;
377 	struct sctp_adaptation_ind_param aiparam;
378 	struct sctp_paramhdr *auth_chunks = NULL;
379 	struct sctp_paramhdr *auth_random = NULL;
380 	struct sctp_paramhdr *auth_hmacs = NULL;
381 	struct sctp_chunk *retval = NULL;
382 	struct sctp_cookie_param *cookie;
383 	struct sctp_inithdr initack;
384 	union sctp_params addrs;
385 	struct sctp_sock *sp;
386 	__u8 extensions[5];
387 	size_t chunksize;
388 	int num_ext = 0;
389 	int cookie_len;
390 	int addrs_len;
391 
392 	/* Note: there may be no addresses to embed. */
393 	addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
394 
395 	initack.init_tag	        = htonl(asoc->c.my_vtag);
396 	initack.a_rwnd			= htonl(asoc->rwnd);
397 	initack.num_outbound_streams	= htons(asoc->c.sinit_num_ostreams);
398 	initack.num_inbound_streams	= htons(asoc->c.sinit_max_instreams);
399 	initack.initial_tsn		= htonl(asoc->c.initial_tsn);
400 
401 	/* FIXME:  We really ought to build the cookie right
402 	 * into the packet instead of allocating more fresh memory.
403 	 */
404 	cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
405 				  addrs.v, addrs_len);
406 	if (!cookie)
407 		goto nomem_cookie;
408 
409 	/* Calculate the total size of allocation, include the reserved
410 	 * space for reporting unknown parameters if it is specified.
411 	 */
412 	sp = sctp_sk(asoc->base.sk);
413 	chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
414 
415 	/* Tell peer that we'll do ECN only if peer advertised such cap.  */
416 	if (asoc->peer.ecn_capable)
417 		chunksize += sizeof(ecap_param);
418 
419 	if (asoc->peer.prsctp_capable)
420 		chunksize += sizeof(prsctp_param);
421 
422 	if (asoc->peer.asconf_capable) {
423 		extensions[num_ext] = SCTP_CID_ASCONF;
424 		extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
425 		num_ext += 2;
426 	}
427 
428 	if (asoc->peer.reconf_capable) {
429 		extensions[num_ext] = SCTP_CID_RECONF;
430 		num_ext += 1;
431 	}
432 
433 	if (sp->adaptation_ind)
434 		chunksize += sizeof(aiparam);
435 
436 	if (asoc->peer.intl_capable) {
437 		extensions[num_ext] = SCTP_CID_I_DATA;
438 		num_ext += 1;
439 	}
440 
441 	if (asoc->peer.auth_capable) {
442 		auth_random = (struct sctp_paramhdr *)asoc->c.auth_random;
443 		chunksize += ntohs(auth_random->length);
444 
445 		auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
446 		if (auth_hmacs->length)
447 			chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
448 		else
449 			auth_hmacs = NULL;
450 
451 		auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
452 		if (auth_chunks->length)
453 			chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
454 		else
455 			auth_chunks = NULL;
456 
457 		extensions[num_ext] = SCTP_CID_AUTH;
458 		num_ext += 1;
459 	}
460 
461 	if (num_ext)
462 		chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
463 
464 	/* Now allocate and fill out the chunk.  */
465 	retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
466 	if (!retval)
467 		goto nomem_chunk;
468 
469 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
470 	 *
471 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
472 	 * HEARTBEAT ACK, * etc.) to the same destination transport
473 	 * address from which it received the DATA or control chunk
474 	 * to which it is replying.
475 	 *
476 	 * [INIT ACK back to where the INIT came from.]
477 	 */
478 	if (chunk->transport)
479 		retval->transport =
480 			sctp_assoc_lookup_paddr(asoc,
481 						&chunk->transport->ipaddr);
482 
483 	retval->subh.init_hdr =
484 		sctp_addto_chunk(retval, sizeof(initack), &initack);
485 	retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
486 	sctp_addto_chunk(retval, cookie_len, cookie);
487 	if (asoc->peer.ecn_capable)
488 		sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
489 	if (num_ext) {
490 		ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
491 		ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
492 		sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
493 		sctp_addto_param(retval, num_ext, extensions);
494 	}
495 	if (asoc->peer.prsctp_capable)
496 		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
497 
498 	if (sp->adaptation_ind) {
499 		aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
500 		aiparam.param_hdr.length = htons(sizeof(aiparam));
501 		aiparam.adaptation_ind = htonl(sp->adaptation_ind);
502 		sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
503 	}
504 
505 	if (asoc->peer.auth_capable) {
506 		sctp_addto_chunk(retval, ntohs(auth_random->length),
507 				 auth_random);
508 		if (auth_hmacs)
509 			sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
510 					auth_hmacs);
511 		if (auth_chunks)
512 			sctp_addto_chunk(retval, ntohs(auth_chunks->length),
513 					auth_chunks);
514 	}
515 
516 	/* We need to remove the const qualifier at this point.  */
517 	retval->asoc = (struct sctp_association *) asoc;
518 
519 nomem_chunk:
520 	kfree(cookie);
521 nomem_cookie:
522 	kfree(addrs.v);
523 	return retval;
524 }
525 
526 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
527  *
528  * This chunk is used only during the initialization of an association.
529  * It is sent by the initiator of an association to its peer to complete
530  * the initialization process. This chunk MUST precede any DATA chunk
531  * sent within the association, but MAY be bundled with one or more DATA
532  * chunks in the same packet.
533  *
534  *      0                   1                   2                   3
535  *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
536  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
537  *     |   Type = 10   |Chunk  Flags   |         Length                |
538  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
539  *     /                     Cookie                                    /
540  *     \                                                               \
541  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
542  *
543  * Chunk Flags: 8 bit
544  *
545  *   Set to zero on transmit and ignored on receipt.
546  *
547  * Length: 16 bits (unsigned integer)
548  *
549  *   Set to the size of the chunk in bytes, including the 4 bytes of
550  *   the chunk header and the size of the Cookie.
551  *
552  * Cookie: variable size
553  *
554  *   This field must contain the exact cookie received in the
555  *   State Cookie parameter from the previous INIT ACK.
556  *
557  *   An implementation SHOULD make the cookie as small as possible
558  *   to insure interoperability.
559  */
560 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
561 					 const struct sctp_chunk *chunk)
562 {
563 	struct sctp_chunk *retval;
564 	int cookie_len;
565 	void *cookie;
566 
567 	cookie = asoc->peer.cookie;
568 	cookie_len = asoc->peer.cookie_len;
569 
570 	/* Build a cookie echo chunk.  */
571 	retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
572 				   cookie_len, GFP_ATOMIC);
573 	if (!retval)
574 		goto nodata;
575 	retval->subh.cookie_hdr =
576 		sctp_addto_chunk(retval, cookie_len, cookie);
577 
578 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
579 	 *
580 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
581 	 * HEARTBEAT ACK, * etc.) to the same destination transport
582 	 * address from which it * received the DATA or control chunk
583 	 * to which it is replying.
584 	 *
585 	 * [COOKIE ECHO back to where the INIT ACK came from.]
586 	 */
587 	if (chunk)
588 		retval->transport = chunk->transport;
589 
590 nodata:
591 	return retval;
592 }
593 
594 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
595  *
596  * This chunk is used only during the initialization of an
597  * association.  It is used to acknowledge the receipt of a COOKIE
598  * ECHO chunk.  This chunk MUST precede any DATA or SACK chunk sent
599  * within the association, but MAY be bundled with one or more DATA
600  * chunks or SACK chunk in the same SCTP packet.
601  *
602  *      0                   1                   2                   3
603  *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
604  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605  *     |   Type = 11   |Chunk  Flags   |     Length = 4                |
606  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
607  *
608  * Chunk Flags: 8 bits
609  *
610  *   Set to zero on transmit and ignored on receipt.
611  */
612 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
613 					const struct sctp_chunk *chunk)
614 {
615 	struct sctp_chunk *retval;
616 
617 	retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
618 
619 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
620 	 *
621 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
622 	 * HEARTBEAT ACK, * etc.) to the same destination transport
623 	 * address from which it * received the DATA or control chunk
624 	 * to which it is replying.
625 	 *
626 	 * [COOKIE ACK back to where the COOKIE ECHO came from.]
627 	 */
628 	if (retval && chunk && chunk->transport)
629 		retval->transport =
630 			sctp_assoc_lookup_paddr(asoc,
631 						&chunk->transport->ipaddr);
632 
633 	return retval;
634 }
635 
636 /*
637  *  Appendix A: Explicit Congestion Notification:
638  *  CWR:
639  *
640  *  RFC 2481 details a specific bit for a sender to send in the header of
641  *  its next outbound TCP segment to indicate to its peer that it has
642  *  reduced its congestion window.  This is termed the CWR bit.  For
643  *  SCTP the same indication is made by including the CWR chunk.
644  *  This chunk contains one data element, i.e. the TSN number that
645  *  was sent in the ECNE chunk.  This element represents the lowest
646  *  TSN number in the datagram that was originally marked with the
647  *  CE bit.
648  *
649  *     0                   1                   2                   3
650  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
651  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652  *    | Chunk Type=13 | Flags=00000000|    Chunk Length = 8           |
653  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
654  *    |                      Lowest TSN Number                        |
655  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656  *
657  *     Note: The CWR is considered a Control chunk.
658  */
659 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
660 				 const __u32 lowest_tsn,
661 				 const struct sctp_chunk *chunk)
662 {
663 	struct sctp_chunk *retval;
664 	struct sctp_cwrhdr cwr;
665 
666 	cwr.lowest_tsn = htonl(lowest_tsn);
667 	retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
668 				   sizeof(cwr), GFP_ATOMIC);
669 
670 	if (!retval)
671 		goto nodata;
672 
673 	retval->subh.ecn_cwr_hdr =
674 		sctp_addto_chunk(retval, sizeof(cwr), &cwr);
675 
676 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
677 	 *
678 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
679 	 * HEARTBEAT ACK, * etc.) to the same destination transport
680 	 * address from which it * received the DATA or control chunk
681 	 * to which it is replying.
682 	 *
683 	 * [Report a reduced congestion window back to where the ECNE
684 	 * came from.]
685 	 */
686 	if (chunk)
687 		retval->transport = chunk->transport;
688 
689 nodata:
690 	return retval;
691 }
692 
693 /* Make an ECNE chunk.  This is a congestion experienced report.  */
694 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
695 				  const __u32 lowest_tsn)
696 {
697 	struct sctp_chunk *retval;
698 	struct sctp_ecnehdr ecne;
699 
700 	ecne.lowest_tsn = htonl(lowest_tsn);
701 	retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
702 				   sizeof(ecne), GFP_ATOMIC);
703 	if (!retval)
704 		goto nodata;
705 	retval->subh.ecne_hdr =
706 		sctp_addto_chunk(retval, sizeof(ecne), &ecne);
707 
708 nodata:
709 	return retval;
710 }
711 
712 /* Make a DATA chunk for the given association from the provided
713  * parameters.  However, do not populate the data payload.
714  */
715 struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
716 					    const struct sctp_sndrcvinfo *sinfo,
717 					    int len, __u8 flags, gfp_t gfp)
718 {
719 	struct sctp_chunk *retval;
720 	struct sctp_datahdr dp;
721 
722 	/* We assign the TSN as LATE as possible, not here when
723 	 * creating the chunk.
724 	 */
725 	memset(&dp, 0, sizeof(dp));
726 	dp.ppid = sinfo->sinfo_ppid;
727 	dp.stream = htons(sinfo->sinfo_stream);
728 
729 	/* Set the flags for an unordered send.  */
730 	if (sinfo->sinfo_flags & SCTP_UNORDERED)
731 		flags |= SCTP_DATA_UNORDERED;
732 
733 	retval = sctp_make_data(asoc, flags, sizeof(dp) + len, gfp);
734 	if (!retval)
735 		return NULL;
736 
737 	retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
738 	memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
739 
740 	return retval;
741 }
742 
743 /* Create a selective ackowledgement (SACK) for the given
744  * association.  This reports on which TSN's we've seen to date,
745  * including duplicates and gaps.
746  */
747 struct sctp_chunk *sctp_make_sack(struct sctp_association *asoc)
748 {
749 	struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
750 	struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
751 	__u16 num_gabs, num_dup_tsns;
752 	struct sctp_transport *trans;
753 	struct sctp_chunk *retval;
754 	struct sctp_sackhdr sack;
755 	__u32 ctsn;
756 	int len;
757 
758 	memset(gabs, 0, sizeof(gabs));
759 	ctsn = sctp_tsnmap_get_ctsn(map);
760 
761 	pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
762 
763 	/* How much room is needed in the chunk? */
764 	num_gabs = sctp_tsnmap_num_gabs(map, gabs);
765 	num_dup_tsns = sctp_tsnmap_num_dups(map);
766 
767 	/* Initialize the SACK header.  */
768 	sack.cum_tsn_ack	    = htonl(ctsn);
769 	sack.a_rwnd 		    = htonl(asoc->a_rwnd);
770 	sack.num_gap_ack_blocks     = htons(num_gabs);
771 	sack.num_dup_tsns           = htons(num_dup_tsns);
772 
773 	len = sizeof(sack)
774 		+ sizeof(struct sctp_gap_ack_block) * num_gabs
775 		+ sizeof(__u32) * num_dup_tsns;
776 
777 	/* Create the chunk.  */
778 	retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
779 	if (!retval)
780 		goto nodata;
781 
782 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
783 	 *
784 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
785 	 * HEARTBEAT ACK, etc.) to the same destination transport
786 	 * address from which it received the DATA or control chunk to
787 	 * which it is replying.  This rule should also be followed if
788 	 * the endpoint is bundling DATA chunks together with the
789 	 * reply chunk.
790 	 *
791 	 * However, when acknowledging multiple DATA chunks received
792 	 * in packets from different source addresses in a single
793 	 * SACK, the SACK chunk may be transmitted to one of the
794 	 * destination transport addresses from which the DATA or
795 	 * control chunks being acknowledged were received.
796 	 *
797 	 * [BUG:  We do not implement the following paragraph.
798 	 * Perhaps we should remember the last transport we used for a
799 	 * SACK and avoid that (if possible) if we have seen any
800 	 * duplicates. --piggy]
801 	 *
802 	 * When a receiver of a duplicate DATA chunk sends a SACK to a
803 	 * multi- homed endpoint it MAY be beneficial to vary the
804 	 * destination address and not use the source address of the
805 	 * DATA chunk.  The reason being that receiving a duplicate
806 	 * from a multi-homed endpoint might indicate that the return
807 	 * path (as specified in the source address of the DATA chunk)
808 	 * for the SACK is broken.
809 	 *
810 	 * [Send to the address from which we last received a DATA chunk.]
811 	 */
812 	retval->transport = asoc->peer.last_data_from;
813 
814 	retval->subh.sack_hdr =
815 		sctp_addto_chunk(retval, sizeof(sack), &sack);
816 
817 	/* Add the gap ack block information.   */
818 	if (num_gabs)
819 		sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
820 				 gabs);
821 
822 	/* Add the duplicate TSN information.  */
823 	if (num_dup_tsns) {
824 		asoc->stats.idupchunks += num_dup_tsns;
825 		sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
826 				 sctp_tsnmap_get_dups(map));
827 	}
828 	/* Once we have a sack generated, check to see what our sack
829 	 * generation is, if its 0, reset the transports to 0, and reset
830 	 * the association generation to 1
831 	 *
832 	 * The idea is that zero is never used as a valid generation for the
833 	 * association so no transport will match after a wrap event like this,
834 	 * Until the next sack
835 	 */
836 	if (++asoc->peer.sack_generation == 0) {
837 		list_for_each_entry(trans, &asoc->peer.transport_addr_list,
838 				    transports)
839 			trans->sack_generation = 0;
840 		asoc->peer.sack_generation = 1;
841 	}
842 nodata:
843 	return retval;
844 }
845 
846 /* Make a SHUTDOWN chunk. */
847 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
848 				      const struct sctp_chunk *chunk)
849 {
850 	struct sctp_shutdownhdr shut;
851 	struct sctp_chunk *retval;
852 	__u32 ctsn;
853 
854 	ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
855 	shut.cum_tsn_ack = htonl(ctsn);
856 
857 	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
858 				   sizeof(shut), GFP_ATOMIC);
859 	if (!retval)
860 		goto nodata;
861 
862 	retval->subh.shutdown_hdr =
863 		sctp_addto_chunk(retval, sizeof(shut), &shut);
864 
865 	if (chunk)
866 		retval->transport = chunk->transport;
867 nodata:
868 	return retval;
869 }
870 
871 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
872 					  const struct sctp_chunk *chunk)
873 {
874 	struct sctp_chunk *retval;
875 
876 	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
877 				   GFP_ATOMIC);
878 
879 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
880 	 *
881 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
882 	 * HEARTBEAT ACK, * etc.) to the same destination transport
883 	 * address from which it * received the DATA or control chunk
884 	 * to which it is replying.
885 	 *
886 	 * [ACK back to where the SHUTDOWN came from.]
887 	 */
888 	if (retval && chunk)
889 		retval->transport = chunk->transport;
890 
891 	return retval;
892 }
893 
894 struct sctp_chunk *sctp_make_shutdown_complete(
895 					const struct sctp_association *asoc,
896 					const struct sctp_chunk *chunk)
897 {
898 	struct sctp_chunk *retval;
899 	__u8 flags = 0;
900 
901 	/* Set the T-bit if we have no association (vtag will be
902 	 * reflected)
903 	 */
904 	flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
905 
906 	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
907 				   0, GFP_ATOMIC);
908 
909 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
910 	 *
911 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
912 	 * HEARTBEAT ACK, * etc.) to the same destination transport
913 	 * address from which it * received the DATA or control chunk
914 	 * to which it is replying.
915 	 *
916 	 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
917 	 * came from.]
918 	 */
919 	if (retval && chunk)
920 		retval->transport = chunk->transport;
921 
922 	return retval;
923 }
924 
925 /* Create an ABORT.  Note that we set the T bit if we have no
926  * association, except when responding to an INIT (sctpimpguide 2.41).
927  */
928 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
929 				   const struct sctp_chunk *chunk,
930 				   const size_t hint)
931 {
932 	struct sctp_chunk *retval;
933 	__u8 flags = 0;
934 
935 	/* Set the T-bit if we have no association and 'chunk' is not
936 	 * an INIT (vtag will be reflected).
937 	 */
938 	if (!asoc) {
939 		if (chunk && chunk->chunk_hdr &&
940 		    chunk->chunk_hdr->type == SCTP_CID_INIT)
941 			flags = 0;
942 		else
943 			flags = SCTP_CHUNK_FLAG_T;
944 	}
945 
946 	retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
947 				   GFP_ATOMIC);
948 
949 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
950 	 *
951 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
952 	 * HEARTBEAT ACK, * etc.) to the same destination transport
953 	 * address from which it * received the DATA or control chunk
954 	 * to which it is replying.
955 	 *
956 	 * [ABORT back to where the offender came from.]
957 	 */
958 	if (retval && chunk)
959 		retval->transport = chunk->transport;
960 
961 	return retval;
962 }
963 
964 /* Helper to create ABORT with a NO_USER_DATA error.  */
965 struct sctp_chunk *sctp_make_abort_no_data(
966 					const struct sctp_association *asoc,
967 					const struct sctp_chunk *chunk,
968 					__u32 tsn)
969 {
970 	struct sctp_chunk *retval;
971 	__be32 payload;
972 
973 	retval = sctp_make_abort(asoc, chunk,
974 				 sizeof(struct sctp_errhdr) + sizeof(tsn));
975 
976 	if (!retval)
977 		goto no_mem;
978 
979 	/* Put the tsn back into network byte order.  */
980 	payload = htonl(tsn);
981 	sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
982 	sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
983 
984 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
985 	 *
986 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
987 	 * HEARTBEAT ACK, * etc.) to the same destination transport
988 	 * address from which it * received the DATA or control chunk
989 	 * to which it is replying.
990 	 *
991 	 * [ABORT back to where the offender came from.]
992 	 */
993 	if (chunk)
994 		retval->transport = chunk->transport;
995 
996 no_mem:
997 	return retval;
998 }
999 
1000 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error.  */
1001 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
1002 					struct msghdr *msg,
1003 					size_t paylen)
1004 {
1005 	struct sctp_chunk *retval;
1006 	void *payload = NULL;
1007 	int err;
1008 
1009 	retval = sctp_make_abort(asoc, NULL,
1010 				 sizeof(struct sctp_errhdr) + paylen);
1011 	if (!retval)
1012 		goto err_chunk;
1013 
1014 	if (paylen) {
1015 		/* Put the msg_iov together into payload.  */
1016 		payload = kmalloc(paylen, GFP_KERNEL);
1017 		if (!payload)
1018 			goto err_payload;
1019 
1020 		err = memcpy_from_msg(payload, msg, paylen);
1021 		if (err < 0)
1022 			goto err_copy;
1023 	}
1024 
1025 	sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
1026 	sctp_addto_chunk(retval, paylen, payload);
1027 
1028 	if (paylen)
1029 		kfree(payload);
1030 
1031 	return retval;
1032 
1033 err_copy:
1034 	kfree(payload);
1035 err_payload:
1036 	sctp_chunk_free(retval);
1037 	retval = NULL;
1038 err_chunk:
1039 	return retval;
1040 }
1041 
1042 /* Append bytes to the end of a parameter.  Will panic if chunk is not big
1043  * enough.
1044  */
1045 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1046 			      const void *data)
1047 {
1048 	int chunklen = ntohs(chunk->chunk_hdr->length);
1049 	void *target;
1050 
1051 	target = skb_put(chunk->skb, len);
1052 
1053 	if (data)
1054 		memcpy(target, data, len);
1055 	else
1056 		memset(target, 0, len);
1057 
1058 	/* Adjust the chunk length field.  */
1059 	chunk->chunk_hdr->length = htons(chunklen + len);
1060 	chunk->chunk_end = skb_tail_pointer(chunk->skb);
1061 
1062 	return target;
1063 }
1064 
1065 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1066 struct sctp_chunk *sctp_make_abort_violation(
1067 					const struct sctp_association *asoc,
1068 					const struct sctp_chunk *chunk,
1069 					const __u8 *payload,
1070 					const size_t paylen)
1071 {
1072 	struct sctp_chunk  *retval;
1073 	struct sctp_paramhdr phdr;
1074 
1075 	retval = sctp_make_abort(asoc, chunk, sizeof(struct sctp_errhdr) +
1076 					      paylen + sizeof(phdr));
1077 	if (!retval)
1078 		goto end;
1079 
1080 	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen +
1081 							    sizeof(phdr));
1082 
1083 	phdr.type = htons(chunk->chunk_hdr->type);
1084 	phdr.length = chunk->chunk_hdr->length;
1085 	sctp_addto_chunk(retval, paylen, payload);
1086 	sctp_addto_param(retval, sizeof(phdr), &phdr);
1087 
1088 end:
1089 	return retval;
1090 }
1091 
1092 struct sctp_chunk *sctp_make_violation_paramlen(
1093 					const struct sctp_association *asoc,
1094 					const struct sctp_chunk *chunk,
1095 					struct sctp_paramhdr *param)
1096 {
1097 	static const char error[] = "The following parameter had invalid length:";
1098 	size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr) +
1099 			     sizeof(*param);
1100 	struct sctp_chunk *retval;
1101 
1102 	retval = sctp_make_abort(asoc, chunk, payload_len);
1103 	if (!retval)
1104 		goto nodata;
1105 
1106 	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1107 			sizeof(error) + sizeof(*param));
1108 	sctp_addto_chunk(retval, sizeof(error), error);
1109 	sctp_addto_param(retval, sizeof(*param), param);
1110 
1111 nodata:
1112 	return retval;
1113 }
1114 
1115 struct sctp_chunk *sctp_make_violation_max_retrans(
1116 					const struct sctp_association *asoc,
1117 					const struct sctp_chunk *chunk)
1118 {
1119 	static const char error[] = "Association exceeded its max_retrans count";
1120 	size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr);
1121 	struct sctp_chunk *retval;
1122 
1123 	retval = sctp_make_abort(asoc, chunk, payload_len);
1124 	if (!retval)
1125 		goto nodata;
1126 
1127 	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
1128 	sctp_addto_chunk(retval, sizeof(error), error);
1129 
1130 nodata:
1131 	return retval;
1132 }
1133 
1134 struct sctp_chunk *sctp_make_new_encap_port(const struct sctp_association *asoc,
1135 					    const struct sctp_chunk *chunk)
1136 {
1137 	struct sctp_new_encap_port_hdr nep;
1138 	struct sctp_chunk *retval;
1139 
1140 	retval = sctp_make_abort(asoc, chunk,
1141 				 sizeof(struct sctp_errhdr) + sizeof(nep));
1142 	if (!retval)
1143 		goto nodata;
1144 
1145 	sctp_init_cause(retval, SCTP_ERROR_NEW_ENCAP_PORT, sizeof(nep));
1146 	nep.cur_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
1147 	nep.new_port = chunk->transport->encap_port;
1148 	sctp_addto_chunk(retval, sizeof(nep), &nep);
1149 
1150 nodata:
1151 	return retval;
1152 }
1153 
1154 /* Make a HEARTBEAT chunk.  */
1155 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1156 				       const struct sctp_transport *transport,
1157 				       __u32 probe_size)
1158 {
1159 	struct sctp_sender_hb_info hbinfo = {};
1160 	struct sctp_chunk *retval;
1161 
1162 	retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
1163 				   sizeof(hbinfo), GFP_ATOMIC);
1164 
1165 	if (!retval)
1166 		goto nodata;
1167 
1168 	hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
1169 	hbinfo.param_hdr.length = htons(sizeof(hbinfo));
1170 	hbinfo.daddr = transport->ipaddr;
1171 	hbinfo.sent_at = jiffies;
1172 	hbinfo.hb_nonce = transport->hb_nonce;
1173 	hbinfo.probe_size = probe_size;
1174 
1175 	/* Cast away the 'const', as this is just telling the chunk
1176 	 * what transport it belongs to.
1177 	 */
1178 	retval->transport = (struct sctp_transport *) transport;
1179 	retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
1180 						&hbinfo);
1181 	retval->pmtu_probe = !!probe_size;
1182 
1183 nodata:
1184 	return retval;
1185 }
1186 
1187 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1188 					   const struct sctp_chunk *chunk,
1189 					   const void *payload,
1190 					   const size_t paylen)
1191 {
1192 	struct sctp_chunk *retval;
1193 
1194 	retval  = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen,
1195 				    GFP_ATOMIC);
1196 	if (!retval)
1197 		goto nodata;
1198 
1199 	retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1200 
1201 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
1202 	 *
1203 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1204 	 * HEARTBEAT ACK, * etc.) to the same destination transport
1205 	 * address from which it * received the DATA or control chunk
1206 	 * to which it is replying.
1207 	 *
1208 	 * [HBACK back to where the HEARTBEAT came from.]
1209 	 */
1210 	if (chunk)
1211 		retval->transport = chunk->transport;
1212 
1213 nodata:
1214 	return retval;
1215 }
1216 
1217 /* RFC4820 3. Padding Chunk (PAD)
1218  *  0                   1                   2                   3
1219  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1220  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1221  * | Type = 0x84   |   Flags=0     |             Length            |
1222  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1223  * |                                                               |
1224  * \                         Padding Data                          /
1225  * /                                                               \
1226  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1227  */
1228 struct sctp_chunk *sctp_make_pad(const struct sctp_association *asoc, int len)
1229 {
1230 	struct sctp_chunk *retval;
1231 
1232 	retval = sctp_make_control(asoc, SCTP_CID_PAD, 0, len, GFP_ATOMIC);
1233 	if (!retval)
1234 		return NULL;
1235 
1236 	skb_put_zero(retval->skb, len);
1237 	retval->chunk_hdr->length = htons(ntohs(retval->chunk_hdr->length) + len);
1238 	retval->chunk_end = skb_tail_pointer(retval->skb);
1239 
1240 	return retval;
1241 }
1242 
1243 /* Create an Operation Error chunk with the specified space reserved.
1244  * This routine can be used for containing multiple causes in the chunk.
1245  */
1246 static struct sctp_chunk *sctp_make_op_error_space(
1247 					const struct sctp_association *asoc,
1248 					const struct sctp_chunk *chunk,
1249 					size_t size)
1250 {
1251 	struct sctp_chunk *retval;
1252 
1253 	retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
1254 				   sizeof(struct sctp_errhdr) + size,
1255 				   GFP_ATOMIC);
1256 	if (!retval)
1257 		goto nodata;
1258 
1259 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
1260 	 *
1261 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1262 	 * HEARTBEAT ACK, etc.) to the same destination transport
1263 	 * address from which it received the DATA or control chunk
1264 	 * to which it is replying.
1265 	 *
1266 	 */
1267 	if (chunk)
1268 		retval->transport = chunk->transport;
1269 
1270 nodata:
1271 	return retval;
1272 }
1273 
1274 /* Create an Operation Error chunk of a fixed size, specifically,
1275  * min(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT) - overheads.
1276  * This is a helper function to allocate an error chunk for those
1277  * invalid parameter codes in which we may not want to report all the
1278  * errors, if the incoming chunk is large. If it can't fit in a single
1279  * packet, we ignore it.
1280  */
1281 static inline struct sctp_chunk *sctp_make_op_error_limited(
1282 					const struct sctp_association *asoc,
1283 					const struct sctp_chunk *chunk)
1284 {
1285 	size_t size = SCTP_DEFAULT_MAXSEGMENT;
1286 	struct sctp_sock *sp = NULL;
1287 
1288 	if (asoc) {
1289 		size = min_t(size_t, size, asoc->pathmtu);
1290 		sp = sctp_sk(asoc->base.sk);
1291 	}
1292 
1293 	size = sctp_mtu_payload(sp, size, sizeof(struct sctp_errhdr));
1294 
1295 	return sctp_make_op_error_space(asoc, chunk, size);
1296 }
1297 
1298 /* Create an Operation Error chunk.  */
1299 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1300 				      const struct sctp_chunk *chunk,
1301 				      __be16 cause_code, const void *payload,
1302 				      size_t paylen, size_t reserve_tail)
1303 {
1304 	struct sctp_chunk *retval;
1305 
1306 	retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
1307 	if (!retval)
1308 		goto nodata;
1309 
1310 	sctp_init_cause(retval, cause_code, paylen + reserve_tail);
1311 	sctp_addto_chunk(retval, paylen, payload);
1312 	if (reserve_tail)
1313 		sctp_addto_param(retval, reserve_tail, NULL);
1314 
1315 nodata:
1316 	return retval;
1317 }
1318 
1319 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc,
1320 				  __u16 key_id)
1321 {
1322 	struct sctp_authhdr auth_hdr;
1323 	const struct sctp_hmac *hmac_desc;
1324 	struct sctp_chunk *retval;
1325 
1326 	/* Get the first hmac that the peer told us to use */
1327 	hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1328 	if (unlikely(!hmac_desc))
1329 		return NULL;
1330 
1331 	retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
1332 				   hmac_desc->hmac_len + sizeof(auth_hdr),
1333 				   GFP_ATOMIC);
1334 	if (!retval)
1335 		return NULL;
1336 
1337 	auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1338 	auth_hdr.shkey_id = htons(key_id);
1339 
1340 	retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(auth_hdr),
1341 						 &auth_hdr);
1342 
1343 	skb_put_zero(retval->skb, hmac_desc->hmac_len);
1344 
1345 	/* Adjust the chunk header to include the empty MAC */
1346 	retval->chunk_hdr->length =
1347 		htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1348 	retval->chunk_end = skb_tail_pointer(retval->skb);
1349 
1350 	return retval;
1351 }
1352 
1353 
1354 /********************************************************************
1355  * 2nd Level Abstractions
1356  ********************************************************************/
1357 
1358 /* Turn an skb into a chunk.
1359  * FIXME: Eventually move the structure directly inside the skb->cb[].
1360  *
1361  * sctpimpguide-05.txt Section 2.8.2
1362  * M1) Each time a new DATA chunk is transmitted
1363  * set the 'TSN.Missing.Report' count for that TSN to 0. The
1364  * 'TSN.Missing.Report' count will be used to determine missing chunks
1365  * and when to fast retransmit.
1366  *
1367  */
1368 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1369 				 const struct sctp_association *asoc,
1370 				 struct sock *sk, gfp_t gfp)
1371 {
1372 	struct sctp_chunk *retval;
1373 
1374 	retval = kmem_cache_zalloc(sctp_chunk_cachep, gfp);
1375 
1376 	if (!retval)
1377 		goto nodata;
1378 	if (!sk)
1379 		pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
1380 
1381 	INIT_LIST_HEAD(&retval->list);
1382 	retval->skb		= skb;
1383 	retval->asoc		= (struct sctp_association *)asoc;
1384 	retval->singleton	= 1;
1385 
1386 	retval->fast_retransmit = SCTP_CAN_FRTX;
1387 
1388 	/* Polish the bead hole.  */
1389 	INIT_LIST_HEAD(&retval->transmitted_list);
1390 	INIT_LIST_HEAD(&retval->frag_list);
1391 	SCTP_DBG_OBJCNT_INC(chunk);
1392 	refcount_set(&retval->refcnt, 1);
1393 
1394 nodata:
1395 	return retval;
1396 }
1397 
1398 /* Set chunk->source and dest based on the IP header in chunk->skb.  */
1399 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1400 		     union sctp_addr *dest)
1401 {
1402 	memcpy(&chunk->source, src, sizeof(union sctp_addr));
1403 	memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1404 }
1405 
1406 /* Extract the source address from a chunk.  */
1407 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1408 {
1409 	/* If we have a known transport, use that.  */
1410 	if (chunk->transport) {
1411 		return &chunk->transport->ipaddr;
1412 	} else {
1413 		/* Otherwise, extract it from the IP header.  */
1414 		return &chunk->source;
1415 	}
1416 }
1417 
1418 /* Create a new chunk, setting the type and flags headers from the
1419  * arguments, reserving enough space for a 'paylen' byte payload.
1420  */
1421 static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
1422 					   __u8 type, __u8 flags, int paylen,
1423 					   gfp_t gfp)
1424 {
1425 	struct sctp_chunkhdr *chunk_hdr;
1426 	struct sctp_chunk *retval;
1427 	struct sk_buff *skb;
1428 	struct sock *sk;
1429 	int chunklen;
1430 
1431 	chunklen = SCTP_PAD4(sizeof(*chunk_hdr) + paylen);
1432 	if (chunklen > SCTP_MAX_CHUNK_LEN)
1433 		goto nodata;
1434 
1435 	/* No need to allocate LL here, as this is only a chunk. */
1436 	skb = alloc_skb(chunklen, gfp);
1437 	if (!skb)
1438 		goto nodata;
1439 
1440 	/* Make room for the chunk header.  */
1441 	chunk_hdr = (struct sctp_chunkhdr *)skb_put(skb, sizeof(*chunk_hdr));
1442 	chunk_hdr->type	  = type;
1443 	chunk_hdr->flags  = flags;
1444 	chunk_hdr->length = htons(sizeof(*chunk_hdr));
1445 
1446 	sk = asoc ? asoc->base.sk : NULL;
1447 	retval = sctp_chunkify(skb, asoc, sk, gfp);
1448 	if (!retval) {
1449 		kfree_skb(skb);
1450 		goto nodata;
1451 	}
1452 
1453 	retval->chunk_hdr = chunk_hdr;
1454 	retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(*chunk_hdr);
1455 
1456 	/* Determine if the chunk needs to be authenticated */
1457 	if (sctp_auth_send_cid(type, asoc))
1458 		retval->auth = 1;
1459 
1460 	return retval;
1461 nodata:
1462 	return NULL;
1463 }
1464 
1465 static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
1466 					 __u8 flags, int paylen, gfp_t gfp)
1467 {
1468 	return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen, gfp);
1469 }
1470 
1471 struct sctp_chunk *sctp_make_idata(const struct sctp_association *asoc,
1472 				   __u8 flags, int paylen, gfp_t gfp)
1473 {
1474 	return _sctp_make_chunk(asoc, SCTP_CID_I_DATA, flags, paylen, gfp);
1475 }
1476 
1477 static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
1478 					    __u8 type, __u8 flags, int paylen,
1479 					    gfp_t gfp)
1480 {
1481 	struct sctp_chunk *chunk;
1482 
1483 	chunk = _sctp_make_chunk(asoc, type, flags, paylen, gfp);
1484 	if (chunk)
1485 		sctp_control_set_owner_w(chunk);
1486 
1487 	return chunk;
1488 }
1489 
1490 /* Release the memory occupied by a chunk.  */
1491 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1492 {
1493 	BUG_ON(!list_empty(&chunk->list));
1494 	list_del_init(&chunk->transmitted_list);
1495 
1496 	consume_skb(chunk->skb);
1497 	consume_skb(chunk->auth_chunk);
1498 
1499 	SCTP_DBG_OBJCNT_DEC(chunk);
1500 	kmem_cache_free(sctp_chunk_cachep, chunk);
1501 }
1502 
1503 /* Possibly, free the chunk.  */
1504 void sctp_chunk_free(struct sctp_chunk *chunk)
1505 {
1506 	/* Release our reference on the message tracker. */
1507 	if (chunk->msg)
1508 		sctp_datamsg_put(chunk->msg);
1509 
1510 	sctp_chunk_put(chunk);
1511 }
1512 
1513 /* Grab a reference to the chunk. */
1514 void sctp_chunk_hold(struct sctp_chunk *ch)
1515 {
1516 	refcount_inc(&ch->refcnt);
1517 }
1518 
1519 /* Release a reference to the chunk. */
1520 void sctp_chunk_put(struct sctp_chunk *ch)
1521 {
1522 	if (refcount_dec_and_test(&ch->refcnt))
1523 		sctp_chunk_destroy(ch);
1524 }
1525 
1526 /* Append bytes to the end of a chunk.  Will panic if chunk is not big
1527  * enough.
1528  */
1529 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1530 {
1531 	int chunklen = ntohs(chunk->chunk_hdr->length);
1532 	int padlen = SCTP_PAD4(chunklen) - chunklen;
1533 	void *target;
1534 
1535 	skb_put_zero(chunk->skb, padlen);
1536 	target = skb_put_data(chunk->skb, data, len);
1537 
1538 	/* Adjust the chunk length field.  */
1539 	chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1540 	chunk->chunk_end = skb_tail_pointer(chunk->skb);
1541 
1542 	return target;
1543 }
1544 
1545 /* Append bytes from user space to the end of a chunk.  Will panic if
1546  * chunk is not big enough.
1547  * Returns a kernel err value.
1548  */
1549 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
1550 			  struct iov_iter *from)
1551 {
1552 	void *target;
1553 
1554 	/* Make room in chunk for data.  */
1555 	target = skb_put(chunk->skb, len);
1556 
1557 	/* Copy data (whole iovec) into chunk */
1558 	if (!copy_from_iter_full(target, len, from))
1559 		return -EFAULT;
1560 
1561 	/* Adjust the chunk length field.  */
1562 	chunk->chunk_hdr->length =
1563 		htons(ntohs(chunk->chunk_hdr->length) + len);
1564 	chunk->chunk_end = skb_tail_pointer(chunk->skb);
1565 
1566 	return 0;
1567 }
1568 
1569 /* Helper function to assign a TSN if needed.  This assumes that both
1570  * the data_hdr and association have already been assigned.
1571  */
1572 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1573 {
1574 	struct sctp_stream *stream;
1575 	struct sctp_chunk *lchunk;
1576 	struct sctp_datamsg *msg;
1577 	__u16 ssn, sid;
1578 
1579 	if (chunk->has_ssn)
1580 		return;
1581 
1582 	/* All fragments will be on the same stream */
1583 	sid = ntohs(chunk->subh.data_hdr->stream);
1584 	stream = &chunk->asoc->stream;
1585 
1586 	/* Now assign the sequence number to the entire message.
1587 	 * All fragments must have the same stream sequence number.
1588 	 */
1589 	msg = chunk->msg;
1590 	list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1591 		if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1592 			ssn = 0;
1593 		} else {
1594 			if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1595 				ssn = sctp_ssn_next(stream, out, sid);
1596 			else
1597 				ssn = sctp_ssn_peek(stream, out, sid);
1598 		}
1599 
1600 		lchunk->subh.data_hdr->ssn = htons(ssn);
1601 		lchunk->has_ssn = 1;
1602 	}
1603 }
1604 
1605 /* Helper function to assign a TSN if needed.  This assumes that both
1606  * the data_hdr and association have already been assigned.
1607  */
1608 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1609 {
1610 	if (!chunk->has_tsn) {
1611 		/* This is the last possible instant to
1612 		 * assign a TSN.
1613 		 */
1614 		chunk->subh.data_hdr->tsn =
1615 			htonl(sctp_association_get_next_tsn(chunk->asoc));
1616 		chunk->has_tsn = 1;
1617 	}
1618 }
1619 
1620 /* Create a CLOSED association to use with an incoming packet.  */
1621 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1622 					     struct sctp_chunk *chunk,
1623 					     gfp_t gfp)
1624 {
1625 	struct sctp_association *asoc;
1626 	enum sctp_scope scope;
1627 	struct sk_buff *skb;
1628 
1629 	/* Create the bare association.  */
1630 	scope = sctp_scope(sctp_source(chunk));
1631 	asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1632 	if (!asoc)
1633 		goto nodata;
1634 	asoc->temp = 1;
1635 	skb = chunk->skb;
1636 	/* Create an entry for the source address of the packet.  */
1637 	SCTP_INPUT_CB(skb)->af->from_skb(&asoc->c.peer_addr, skb, 1);
1638 
1639 nodata:
1640 	return asoc;
1641 }
1642 
1643 /* Build a cookie representing asoc.
1644  * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1645  */
1646 static struct sctp_cookie_param *sctp_pack_cookie(
1647 					const struct sctp_endpoint *ep,
1648 					const struct sctp_association *asoc,
1649 					const struct sctp_chunk *init_chunk,
1650 					int *cookie_len, const __u8 *raw_addrs,
1651 					int addrs_len)
1652 {
1653 	struct sctp_signed_cookie *cookie;
1654 	struct sctp_cookie_param *retval;
1655 	int headersize, bodysize;
1656 
1657 	/* Header size is static data prior to the actual cookie, including
1658 	 * any padding.
1659 	 */
1660 	headersize = sizeof(struct sctp_paramhdr) +
1661 		     (sizeof(struct sctp_signed_cookie) -
1662 		      sizeof(struct sctp_cookie));
1663 	bodysize = sizeof(struct sctp_cookie)
1664 		+ ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1665 
1666 	/* Pad out the cookie to a multiple to make the signature
1667 	 * functions simpler to write.
1668 	 */
1669 	if (bodysize % SCTP_COOKIE_MULTIPLE)
1670 		bodysize += SCTP_COOKIE_MULTIPLE
1671 			- (bodysize % SCTP_COOKIE_MULTIPLE);
1672 	*cookie_len = headersize + bodysize;
1673 
1674 	/* Clear this memory since we are sending this data structure
1675 	 * out on the network.
1676 	 */
1677 	retval = kzalloc(*cookie_len, GFP_ATOMIC);
1678 	if (!retval) {
1679 		*cookie_len = 0;
1680 		return NULL;
1681 	}
1682 
1683 	cookie = (struct sctp_signed_cookie *) retval->body;
1684 
1685 	/* Set up the parameter header.  */
1686 	retval->p.type = SCTP_PARAM_STATE_COOKIE;
1687 	retval->p.length = htons(*cookie_len);
1688 
1689 	/* Copy the cookie part of the association itself.  */
1690 	cookie->c = asoc->c;
1691 	/* Save the raw address list length in the cookie. */
1692 	cookie->c.raw_addr_list_len = addrs_len;
1693 
1694 	/* Remember PR-SCTP capability. */
1695 	cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1696 
1697 	/* Save adaptation indication in the cookie. */
1698 	cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1699 
1700 	/* Set an expiration time for the cookie.  */
1701 	cookie->c.expiration = ktime_add(asoc->cookie_life,
1702 					 ktime_get_real());
1703 
1704 	/* Copy the peer's init packet.  */
1705 	memcpy(cookie + 1, init_chunk->chunk_hdr,
1706 	       ntohs(init_chunk->chunk_hdr->length));
1707 
1708 	/* Copy the raw local address list of the association. */
1709 	memcpy((__u8 *)(cookie + 1) +
1710 	       ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1711 
1712 	/* Sign the cookie, if cookie authentication is enabled. */
1713 	if (sctp_sk(ep->base.sk)->cookie_auth_enable) {
1714 		static_assert(sizeof(cookie->mac) == SHA256_DIGEST_SIZE);
1715 		hmac_sha256(&ep->cookie_auth_key, (const u8 *)&cookie->c,
1716 			    bodysize, cookie->mac);
1717 	}
1718 
1719 	return retval;
1720 }
1721 
1722 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association.  */
1723 struct sctp_association *sctp_unpack_cookie(
1724 					const struct sctp_endpoint *ep,
1725 					const struct sctp_association *asoc,
1726 					struct sctp_chunk *chunk, gfp_t gfp,
1727 					int *error, struct sctp_chunk **errp)
1728 {
1729 	struct sctp_association *retval = NULL;
1730 	int headersize, bodysize, fixed_size;
1731 	struct sctp_signed_cookie *cookie;
1732 	struct sk_buff *skb = chunk->skb;
1733 	struct sctp_cookie *bear_cookie;
1734 	struct sctp_chunkhdr *ch;
1735 	unsigned int len, chlen;
1736 	union sctp_addr paddr;
1737 	enum sctp_scope scope;
1738 	struct sctp_af *af;
1739 	ktime_t kt;
1740 
1741 	/* Header size is static data prior to the actual cookie, including
1742 	 * any padding.
1743 	 */
1744 	headersize = sizeof(struct sctp_chunkhdr) +
1745 		     (sizeof(struct sctp_signed_cookie) -
1746 		      sizeof(struct sctp_cookie));
1747 	bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1748 	fixed_size = headersize + sizeof(struct sctp_cookie);
1749 
1750 	/* Verify that the chunk looks like it even has a cookie.
1751 	 * There must be enough room for our cookie and our peer's
1752 	 * INIT chunk.
1753 	 */
1754 	len = ntohs(chunk->chunk_hdr->length);
1755 	if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1756 		goto malformed;
1757 
1758 	/* Verify that the cookie has been padded out. */
1759 	if (bodysize % SCTP_COOKIE_MULTIPLE)
1760 		goto malformed;
1761 
1762 	/* Process the cookie.  */
1763 	cookie = chunk->subh.cookie_hdr;
1764 	bear_cookie = &cookie->c;
1765 
1766 	ch = (struct sctp_chunkhdr *)(bear_cookie + 1);
1767 	if (ch->type != SCTP_CID_INIT)
1768 		goto malformed;
1769 	chlen = ntohs(ch->length);
1770 	if (chlen < sizeof(struct sctp_init_chunk))
1771 		goto malformed;
1772 	if (chlen > len - fixed_size)
1773 		goto malformed;
1774 	if (bear_cookie->raw_addr_list_len > len - fixed_size - chlen)
1775 		goto malformed;
1776 
1777 	/* Verify the cookie's MAC, if cookie authentication is enabled. */
1778 	if (sctp_sk(ep->base.sk)->cookie_auth_enable) {
1779 		u8 mac[SHA256_DIGEST_SIZE];
1780 
1781 		hmac_sha256(&ep->cookie_auth_key, (const u8 *)bear_cookie,
1782 			    bodysize, mac);
1783 		static_assert(sizeof(cookie->mac) == sizeof(mac));
1784 		if (crypto_memneq(mac, cookie->mac, sizeof(mac))) {
1785 			*error = -SCTP_IERROR_BAD_SIG;
1786 			goto fail;
1787 		}
1788 	}
1789 
1790 	/* IG Section 2.35.2:
1791 	 *  3) Compare the port numbers and the verification tag contained
1792 	 *     within the COOKIE ECHO chunk to the actual port numbers and the
1793 	 *     verification tag within the SCTP common header of the received
1794 	 *     packet. If these values do not match the packet MUST be silently
1795 	 *     discarded,
1796 	 */
1797 	if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1798 		*error = -SCTP_IERROR_BAD_TAG;
1799 		goto fail;
1800 	}
1801 
1802 	if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1803 	    ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1804 		*error = -SCTP_IERROR_BAD_PORTS;
1805 		goto fail;
1806 	}
1807 
1808 	/* Check to see if the cookie is stale.  RFC 9260 Section 5.2.4
1809 	 * exempts an expired cookie only when both Verification Tags match
1810 	 * the current association.
1811 	 * If skb has been timestamped, then use the stamp, otherwise
1812 	 * use current time.  This introduces a small possibility that
1813 	 * a cookie may be considered expired, but this would only slow
1814 	 * down the new association establishment instead of every packet.
1815 	 */
1816 	if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1817 		kt = skb_get_ktime(skb);
1818 	else
1819 		kt = ktime_get_real();
1820 
1821 	if ((!asoc ||
1822 	     asoc->c.my_vtag != bear_cookie->my_vtag ||
1823 	     asoc->c.peer_vtag != bear_cookie->peer_vtag) &&
1824 	    ktime_before(bear_cookie->expiration, kt)) {
1825 		suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
1826 		__be32 n = htonl(usecs);
1827 
1828 		/*
1829 		 * Section 3.3.10.3 Stale Cookie Error (3)
1830 		 *
1831 		 * Cause of error
1832 		 * ---------------
1833 		 * Stale Cookie Error:  Indicates the receipt of a valid State
1834 		 * Cookie that has expired.
1835 		 */
1836 		*errp = sctp_make_op_error(asoc, chunk,
1837 					   SCTP_ERROR_STALE_COOKIE, &n,
1838 					   sizeof(n), 0);
1839 		if (*errp)
1840 			*error = -SCTP_IERROR_STALE_COOKIE;
1841 		else
1842 			*error = -SCTP_IERROR_NOMEM;
1843 
1844 		goto fail;
1845 	}
1846 
1847 	paddr = bear_cookie->peer_addr;
1848 	af = sctp_get_af_specific(paddr.sa.sa_family);
1849 	if (!af || !af->addr_valid(&paddr, sctp_sk(ep->base.sk), NULL))
1850 		goto malformed;
1851 
1852 	/* Make a new base association.  */
1853 	scope = sctp_scope(sctp_source(chunk));
1854 	retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1855 	if (!retval) {
1856 		*error = -SCTP_IERROR_NOMEM;
1857 		goto fail;
1858 	}
1859 
1860 	/* Set up our peer's port number.  */
1861 	retval->peer.port = ntohs(chunk->sctp_hdr->source);
1862 
1863 	if (!sctp_auth_verify_cookie_params(ep, bear_cookie))
1864 		goto malformed;
1865 
1866 	/* Populate the association from the cookie.  */
1867 	memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1868 	retval->c.peer_addr = paddr;
1869 
1870 	if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1871 						 GFP_ATOMIC) < 0) {
1872 		*error = -SCTP_IERROR_NOMEM;
1873 		goto fail;
1874 	}
1875 
1876 	/* Also, add the destination address. */
1877 	if (list_empty(&retval->base.bind_addr.address_list)) {
1878 		sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1879 				   sizeof(chunk->dest), SCTP_ADDR_SRC,
1880 				   GFP_ATOMIC);
1881 	}
1882 
1883 	retval->next_tsn = retval->c.initial_tsn;
1884 	retval->ctsn_ack_point = retval->next_tsn - 1;
1885 	retval->addip_serial = retval->c.initial_tsn;
1886 	retval->strreset_outseq = retval->c.initial_tsn;
1887 	retval->adv_peer_ack_point = retval->ctsn_ack_point;
1888 	retval->peer.prsctp_capable = retval->c.prsctp_capable;
1889 	retval->peer.adaptation_ind = retval->c.adaptation_ind;
1890 
1891 	/* The INIT stuff will be done by the side effects.  */
1892 	return retval;
1893 
1894 fail:
1895 	if (retval)
1896 		sctp_association_free(retval);
1897 
1898 	return NULL;
1899 
1900 malformed:
1901 	/* Yikes!  The packet is either corrupt or deliberately
1902 	 * malformed.
1903 	 */
1904 	*error = -SCTP_IERROR_MALFORMED;
1905 	goto fail;
1906 }
1907 
1908 /********************************************************************
1909  * 3rd Level Abstractions
1910  ********************************************************************/
1911 
1912 struct __sctp_missing {
1913 	__be32 num_missing;
1914 	__be16 type;
1915 }  __packed;
1916 
1917 /*
1918  * Report a missing mandatory parameter.
1919  */
1920 static int sctp_process_missing_param(const struct sctp_association *asoc,
1921 				      enum sctp_param paramtype,
1922 				      struct sctp_chunk *chunk,
1923 				      struct sctp_chunk **errp)
1924 {
1925 	struct __sctp_missing report;
1926 	__u16 len;
1927 
1928 	len = SCTP_PAD4(sizeof(report));
1929 
1930 	/* Make an ERROR chunk, preparing enough room for
1931 	 * returning multiple unknown parameters.
1932 	 */
1933 	if (!*errp)
1934 		*errp = sctp_make_op_error_space(asoc, chunk, len);
1935 
1936 	if (*errp) {
1937 		report.num_missing = htonl(1);
1938 		report.type = paramtype;
1939 		sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1940 				sizeof(report));
1941 		sctp_addto_chunk(*errp, sizeof(report), &report);
1942 	}
1943 
1944 	/* Stop processing this chunk. */
1945 	return 0;
1946 }
1947 
1948 /* Report an Invalid Mandatory Parameter.  */
1949 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1950 				      struct sctp_chunk *chunk,
1951 				      struct sctp_chunk **errp)
1952 {
1953 	/* Invalid Mandatory Parameter Error has no payload. */
1954 
1955 	if (!*errp)
1956 		*errp = sctp_make_op_error_space(asoc, chunk, 0);
1957 
1958 	if (*errp)
1959 		sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1960 
1961 	/* Stop processing this chunk. */
1962 	return 0;
1963 }
1964 
1965 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1966 					struct sctp_paramhdr *param,
1967 					const struct sctp_chunk *chunk,
1968 					struct sctp_chunk **errp)
1969 {
1970 	/* This is a fatal error.  Any accumulated non-fatal errors are
1971 	 * not reported.
1972 	 */
1973 	if (*errp)
1974 		sctp_chunk_free(*errp);
1975 
1976 	/* Create an error chunk and fill it in with our payload. */
1977 	*errp = sctp_make_violation_paramlen(asoc, chunk, param);
1978 
1979 	return 0;
1980 }
1981 
1982 
1983 /* Do not attempt to handle the HOST_NAME parm.  However, do
1984  * send back an indicator to the peer.
1985  */
1986 static int sctp_process_hn_param(const struct sctp_association *asoc,
1987 				 union sctp_params param,
1988 				 struct sctp_chunk *chunk,
1989 				 struct sctp_chunk **errp)
1990 {
1991 	__u16 len = ntohs(param.p->length);
1992 
1993 	/* Processing of the HOST_NAME parameter will generate an
1994 	 * ABORT.  If we've accumulated any non-fatal errors, they
1995 	 * would be unrecognized parameters and we should not include
1996 	 * them in the ABORT.
1997 	 */
1998 	if (*errp)
1999 		sctp_chunk_free(*errp);
2000 
2001 	*errp = sctp_make_op_error(asoc, chunk, SCTP_ERROR_DNS_FAILED,
2002 				   param.v, len, 0);
2003 
2004 	/* Stop processing this chunk. */
2005 	return 0;
2006 }
2007 
2008 static int sctp_verify_ext_param(struct net *net,
2009 				 const struct sctp_endpoint *ep,
2010 				 union sctp_params param)
2011 {
2012 	__u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2013 	int have_asconf = 0;
2014 	int have_auth = 0;
2015 	int i;
2016 
2017 	for (i = 0; i < num_ext; i++) {
2018 		switch (param.ext->chunks[i]) {
2019 		case SCTP_CID_AUTH:
2020 			have_auth = 1;
2021 			break;
2022 		case SCTP_CID_ASCONF:
2023 		case SCTP_CID_ASCONF_ACK:
2024 			have_asconf = 1;
2025 			break;
2026 		}
2027 	}
2028 
2029 	/* ADD-IP Security: The draft requires us to ABORT or ignore the
2030 	 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not.  Do this
2031 	 * only if ADD-IP is turned on and we are not backward-compatible
2032 	 * mode.
2033 	 */
2034 	if (net->sctp.addip_noauth)
2035 		return 1;
2036 
2037 	if (ep->asconf_enable && !have_auth && have_asconf)
2038 		return 0;
2039 
2040 	return 1;
2041 }
2042 
2043 static void sctp_process_ext_param(struct sctp_association *asoc,
2044 				   union sctp_params param)
2045 {
2046 	__u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2047 	int i;
2048 
2049 	for (i = 0; i < num_ext; i++) {
2050 		switch (param.ext->chunks[i]) {
2051 		case SCTP_CID_RECONF:
2052 			if (asoc->ep->reconf_enable)
2053 				asoc->peer.reconf_capable = 1;
2054 			break;
2055 		case SCTP_CID_FWD_TSN:
2056 			if (asoc->ep->prsctp_enable)
2057 				asoc->peer.prsctp_capable = 1;
2058 			break;
2059 		case SCTP_CID_AUTH:
2060 			/* if the peer reports AUTH, assume that he
2061 			 * supports AUTH.
2062 			 */
2063 			if (asoc->ep->auth_enable)
2064 				asoc->peer.auth_capable = 1;
2065 			break;
2066 		case SCTP_CID_ASCONF:
2067 		case SCTP_CID_ASCONF_ACK:
2068 			if (asoc->ep->asconf_enable)
2069 				asoc->peer.asconf_capable = 1;
2070 			break;
2071 		case SCTP_CID_I_DATA:
2072 			if (asoc->ep->intl_enable)
2073 				asoc->peer.intl_capable = 1;
2074 			break;
2075 		default:
2076 			break;
2077 		}
2078 	}
2079 }
2080 
2081 /* RFC 3.2.1 & the Implementers Guide 2.2.
2082  *
2083  * The Parameter Types are encoded such that the
2084  * highest-order two bits specify the action that must be
2085  * taken if the processing endpoint does not recognize the
2086  * Parameter Type.
2087  *
2088  * 00 - Stop processing this parameter; do not process any further
2089  * 	parameters within this chunk
2090  *
2091  * 01 - Stop processing this parameter, do not process any further
2092  *	parameters within this chunk, and report the unrecognized
2093  *	parameter in an 'Unrecognized Parameter' ERROR chunk.
2094  *
2095  * 10 - Skip this parameter and continue processing.
2096  *
2097  * 11 - Skip this parameter and continue processing but
2098  *	report the unrecognized parameter in an
2099  *	'Unrecognized Parameter' ERROR chunk.
2100  *
2101  * Return value:
2102  * 	SCTP_IERROR_NO_ERROR - continue with the chunk
2103  * 	SCTP_IERROR_ERROR    - stop and report an error.
2104  * 	SCTP_IERROR_NOMEME   - out of memory.
2105  */
2106 static enum sctp_ierror sctp_process_unk_param(
2107 					const struct sctp_association *asoc,
2108 					union sctp_params param,
2109 					struct sctp_chunk *chunk,
2110 					struct sctp_chunk **errp)
2111 {
2112 	int retval = SCTP_IERROR_NO_ERROR;
2113 
2114 	switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2115 	case SCTP_PARAM_ACTION_DISCARD:
2116 		retval =  SCTP_IERROR_ERROR;
2117 		break;
2118 	case SCTP_PARAM_ACTION_SKIP:
2119 		break;
2120 	case SCTP_PARAM_ACTION_DISCARD_ERR:
2121 		retval =  SCTP_IERROR_ERROR;
2122 		fallthrough;
2123 	case SCTP_PARAM_ACTION_SKIP_ERR:
2124 		/* Make an ERROR chunk, preparing enough room for
2125 		 * returning multiple unknown parameters.
2126 		 */
2127 		if (!*errp) {
2128 			*errp = sctp_make_op_error_limited(asoc, chunk);
2129 			if (!*errp) {
2130 				/* If there is no memory for generating the
2131 				 * ERROR report as specified, an ABORT will be
2132 				 * triggered to the peer and the association
2133 				 * won't be established.
2134 				 */
2135 				retval = SCTP_IERROR_NOMEM;
2136 				break;
2137 			}
2138 		}
2139 
2140 		if (!sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2141 				     ntohs(param.p->length)))
2142 			sctp_addto_chunk(*errp, ntohs(param.p->length),
2143 					 param.v);
2144 		break;
2145 	default:
2146 		break;
2147 	}
2148 
2149 	return retval;
2150 }
2151 
2152 /* Verify variable length parameters
2153  * Return values:
2154  * 	SCTP_IERROR_ABORT - trigger an ABORT
2155  * 	SCTP_IERROR_NOMEM - out of memory (abort)
2156  *	SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2157  * 	SCTP_IERROR_NO_ERROR - continue with the chunk
2158  */
2159 static enum sctp_ierror sctp_verify_param(struct net *net,
2160 					  const struct sctp_endpoint *ep,
2161 					  const struct sctp_association *asoc,
2162 					  union sctp_params param,
2163 					  enum sctp_cid cid,
2164 					  struct sctp_chunk *chunk,
2165 					  struct sctp_chunk **err_chunk)
2166 {
2167 	struct sctp_hmac_algo_param *hmacs;
2168 	int retval = SCTP_IERROR_NO_ERROR;
2169 	__u16 n_elt, id = 0;
2170 	int i;
2171 
2172 	/* FIXME - This routine is not looking at each parameter per the
2173 	 * chunk type, i.e., unrecognized parameters should be further
2174 	 * identified based on the chunk id.
2175 	 */
2176 
2177 	switch (param.p->type) {
2178 	case SCTP_PARAM_IPV4_ADDRESS:
2179 	case SCTP_PARAM_IPV6_ADDRESS:
2180 	case SCTP_PARAM_COOKIE_PRESERVATIVE:
2181 	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2182 	case SCTP_PARAM_STATE_COOKIE:
2183 	case SCTP_PARAM_HEARTBEAT_INFO:
2184 	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2185 	case SCTP_PARAM_ECN_CAPABLE:
2186 		break;
2187 	case SCTP_PARAM_ADAPTATION_LAYER_IND:
2188 		if (ntohs(param.p->length) != sizeof(*param.aind)) {
2189 			sctp_process_inv_paramlength(asoc, param.p,
2190 						     chunk, err_chunk);
2191 			retval = SCTP_IERROR_ABORT;
2192 		}
2193 		break;
2194 
2195 	case SCTP_PARAM_SUPPORTED_EXT:
2196 		if (!sctp_verify_ext_param(net, ep, param))
2197 			return SCTP_IERROR_ABORT;
2198 		break;
2199 
2200 	case SCTP_PARAM_SET_PRIMARY:
2201 		if (!ep->asconf_enable)
2202 			goto unhandled;
2203 
2204 		if (ntohs(param.p->length) < sizeof(struct sctp_addip_param) +
2205 					     sizeof(struct sctp_paramhdr)) {
2206 			sctp_process_inv_paramlength(asoc, param.p,
2207 						     chunk, err_chunk);
2208 			retval = SCTP_IERROR_ABORT;
2209 		}
2210 		break;
2211 
2212 	case SCTP_PARAM_HOST_NAME_ADDRESS:
2213 		/* This param has been Deprecated, send ABORT.  */
2214 		sctp_process_hn_param(asoc, param, chunk, err_chunk);
2215 		retval = SCTP_IERROR_ABORT;
2216 		break;
2217 
2218 	case SCTP_PARAM_FWD_TSN_SUPPORT:
2219 		if (ep->prsctp_enable)
2220 			break;
2221 		goto unhandled;
2222 
2223 	case SCTP_PARAM_RANDOM:
2224 		if (!ep->auth_enable)
2225 			goto unhandled;
2226 
2227 		/* SCTP-AUTH: Secion 6.1
2228 		 * If the random number is not 32 byte long the association
2229 		 * MUST be aborted.  The ABORT chunk SHOULD contain the error
2230 		 * cause 'Protocol Violation'.
2231 		 */
2232 		if (SCTP_AUTH_RANDOM_LENGTH != ntohs(param.p->length) -
2233 					       sizeof(struct sctp_paramhdr)) {
2234 			sctp_process_inv_paramlength(asoc, param.p,
2235 						     chunk, err_chunk);
2236 			retval = SCTP_IERROR_ABORT;
2237 		}
2238 		break;
2239 
2240 	case SCTP_PARAM_CHUNKS:
2241 		if (!ep->auth_enable)
2242 			goto unhandled;
2243 
2244 		/* SCTP-AUTH: Section 3.2
2245 		 * The CHUNKS parameter MUST be included once in the INIT or
2246 		 *  INIT-ACK chunk if the sender wants to receive authenticated
2247 		 *  chunks.  Its maximum length is 260 bytes.
2248 		 */
2249 		if (260 < ntohs(param.p->length)) {
2250 			sctp_process_inv_paramlength(asoc, param.p,
2251 						     chunk, err_chunk);
2252 			retval = SCTP_IERROR_ABORT;
2253 		}
2254 		break;
2255 
2256 	case SCTP_PARAM_HMAC_ALGO:
2257 		if (!ep->auth_enable)
2258 			goto unhandled;
2259 
2260 		hmacs = (struct sctp_hmac_algo_param *)param.p;
2261 		n_elt = (ntohs(param.p->length) -
2262 			 sizeof(struct sctp_paramhdr)) >> 1;
2263 
2264 		/* SCTP-AUTH: Section 6.1
2265 		 * The HMAC algorithm based on SHA-1 MUST be supported and
2266 		 * included in the HMAC-ALGO parameter.
2267 		 */
2268 		for (i = 0; i < n_elt; i++) {
2269 			id = ntohs(hmacs->hmac_ids[i]);
2270 
2271 			if (id == SCTP_AUTH_HMAC_ID_SHA1)
2272 				break;
2273 		}
2274 
2275 		if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2276 			sctp_process_inv_paramlength(asoc, param.p, chunk,
2277 						     err_chunk);
2278 			retval = SCTP_IERROR_ABORT;
2279 		}
2280 		break;
2281 unhandled:
2282 	default:
2283 		pr_debug("%s: unrecognized param:%d for chunk:%d\n",
2284 			 __func__, ntohs(param.p->type), cid);
2285 
2286 		retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2287 		break;
2288 	}
2289 	return retval;
2290 }
2291 
2292 /* Verify the INIT packet before we process it.  */
2293 int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
2294 		     const struct sctp_association *asoc, enum sctp_cid cid,
2295 		     struct sctp_init_chunk *peer_init,
2296 		     struct sctp_chunk *chunk, struct sctp_chunk **errp)
2297 {
2298 	union sctp_params param;
2299 	bool has_cookie = false;
2300 	int result;
2301 
2302 	/* Check for missing mandatory parameters. Note: Initial TSN is
2303 	 * also mandatory, but is not checked here since the valid range
2304 	 * is 0..2**32-1. RFC4960, section 3.3.3.
2305 	 */
2306 	if (peer_init->init_hdr.num_outbound_streams == 0 ||
2307 	    peer_init->init_hdr.num_inbound_streams == 0 ||
2308 	    peer_init->init_hdr.init_tag == 0 ||
2309 	    ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
2310 		return sctp_process_inv_mandatory(asoc, chunk, errp);
2311 
2312 	sctp_walk_params(param, peer_init) {
2313 		if (param.p->type == SCTP_PARAM_STATE_COOKIE)
2314 			has_cookie = true;
2315 	}
2316 
2317 	/* There is a possibility that a parameter length was bad and
2318 	 * in that case we would have stoped walking the parameters.
2319 	 * The current param.p would point at the bad one.
2320 	 * Current consensus on the mailing list is to generate a PROTOCOL
2321 	 * VIOLATION error.  We build the ERROR chunk here and let the normal
2322 	 * error handling code build and send the packet.
2323 	 */
2324 	if (param.v != (void *)peer_init +
2325 		       SCTP_PAD4(ntohs(peer_init->chunk_hdr.length)))
2326 		return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2327 
2328 	/* The only missing mandatory param possible today is
2329 	 * the state cookie for an INIT-ACK chunk.
2330 	 */
2331 	if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2332 		return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2333 						  chunk, errp);
2334 
2335 	/* Verify all the variable length parameters */
2336 	sctp_walk_params(param, peer_init) {
2337 		result = sctp_verify_param(net, ep, asoc, param, cid,
2338 					   chunk, errp);
2339 		switch (result) {
2340 		case SCTP_IERROR_ABORT:
2341 		case SCTP_IERROR_NOMEM:
2342 			return 0;
2343 		case SCTP_IERROR_ERROR:
2344 			return 1;
2345 		case SCTP_IERROR_NO_ERROR:
2346 		default:
2347 			break;
2348 		}
2349 
2350 	} /* for (loop through all parameters) */
2351 
2352 	return 1;
2353 }
2354 
2355 /* Unpack the parameters in an INIT packet into an association.
2356  * Returns 0 on failure, else success.
2357  * FIXME:  This is an association method.
2358  */
2359 int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
2360 		      const union sctp_addr *peer_addr,
2361 		      struct sctp_init_chunk *peer_init, gfp_t gfp)
2362 {
2363 	struct sctp_transport *transport;
2364 	struct list_head *pos, *temp;
2365 	union sctp_params param;
2366 	union sctp_addr addr;
2367 	struct sctp_af *af;
2368 	int src_match = 0;
2369 
2370 	/* We must include the address that the INIT packet came from.
2371 	 * This is the only address that matters for an INIT packet.
2372 	 * When processing a COOKIE ECHO, we retrieve the from address
2373 	 * of the INIT from the cookie.
2374 	 */
2375 
2376 	/* This implementation defaults to making the first transport
2377 	 * added as the primary transport.  The source address seems to
2378 	 * be a better choice than any of the embedded addresses.
2379 	 */
2380 	asoc->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
2381 	if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2382 		goto nomem;
2383 
2384 	if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
2385 		src_match = 1;
2386 
2387 	/* Process the initialization parameters.  */
2388 	sctp_walk_params(param, peer_init) {
2389 		if (!src_match &&
2390 		    (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2391 		     param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2392 			af = sctp_get_af_specific(param_type2af(param.p->type));
2393 			if (!af || !af->from_addr_param(&addr, param.addr,
2394 							chunk->sctp_hdr->source, 0))
2395 				continue;
2396 			if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
2397 				src_match = 1;
2398 		}
2399 
2400 		if (!sctp_process_param(asoc, param, peer_addr, gfp))
2401 			goto clean_up;
2402 	}
2403 
2404 	/* source address of chunk may not match any valid address */
2405 	if (!src_match)
2406 		goto clean_up;
2407 
2408 	/* AUTH: After processing the parameters, make sure that we
2409 	 * have all the required info to potentially do authentications.
2410 	 */
2411 	if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2412 					!asoc->peer.peer_hmacs))
2413 		asoc->peer.auth_capable = 0;
2414 
2415 	/* In a non-backward compatible mode, if the peer claims
2416 	 * support for ADD-IP but not AUTH,  the ADD-IP spec states
2417 	 * that we MUST ABORT the association. Section 6.  The section
2418 	 * also give us an option to silently ignore the packet, which
2419 	 * is what we'll do here.
2420 	 */
2421 	if (!asoc->base.net->sctp.addip_noauth &&
2422 	    (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2423 		asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2424 						  SCTP_PARAM_DEL_IP |
2425 						  SCTP_PARAM_SET_PRIMARY);
2426 		asoc->peer.asconf_capable = 0;
2427 		goto clean_up;
2428 	}
2429 
2430 	/* Walk list of transports, removing transports in the UNKNOWN state. */
2431 	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2432 		transport = list_entry(pos, struct sctp_transport, transports);
2433 		if (transport->state == SCTP_UNKNOWN) {
2434 			sctp_assoc_rm_peer(asoc, transport);
2435 		}
2436 	}
2437 
2438 	/* The fixed INIT headers are always in network byte
2439 	 * order.
2440 	 */
2441 	asoc->peer.i.init_tag =
2442 		ntohl(peer_init->init_hdr.init_tag);
2443 	asoc->peer.i.a_rwnd =
2444 		ntohl(peer_init->init_hdr.a_rwnd);
2445 	asoc->peer.i.num_outbound_streams =
2446 		ntohs(peer_init->init_hdr.num_outbound_streams);
2447 	asoc->peer.i.num_inbound_streams =
2448 		ntohs(peer_init->init_hdr.num_inbound_streams);
2449 	asoc->peer.i.initial_tsn =
2450 		ntohl(peer_init->init_hdr.initial_tsn);
2451 
2452 	asoc->strreset_inseq = asoc->peer.i.initial_tsn;
2453 
2454 	/* Apply the upper bounds for output streams based on peer's
2455 	 * number of inbound streams.
2456 	 */
2457 	if (asoc->c.sinit_num_ostreams  >
2458 	    ntohs(peer_init->init_hdr.num_inbound_streams)) {
2459 		asoc->c.sinit_num_ostreams =
2460 			ntohs(peer_init->init_hdr.num_inbound_streams);
2461 	}
2462 
2463 	if (asoc->c.sinit_max_instreams >
2464 	    ntohs(peer_init->init_hdr.num_outbound_streams)) {
2465 		asoc->c.sinit_max_instreams =
2466 			ntohs(peer_init->init_hdr.num_outbound_streams);
2467 	}
2468 
2469 	/* Copy Initiation tag from INIT to VT_peer in cookie.   */
2470 	asoc->c.peer_vtag = asoc->peer.i.init_tag;
2471 
2472 	/* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
2473 	asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2474 
2475 	/* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2476 	 * high (for example, implementations MAY use the size of the receiver
2477 	 * advertised window).
2478 	 */
2479 	list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2480 			transports) {
2481 		transport->ssthresh = asoc->peer.i.a_rwnd;
2482 	}
2483 
2484 	/* Set up the TSN tracking pieces.  */
2485 	if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2486 				asoc->peer.i.initial_tsn, gfp))
2487 		goto clean_up;
2488 
2489 	/* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2490 	 *
2491 	 * The stream sequence number in all the streams shall start
2492 	 * from 0 when the association is established.  Also, when the
2493 	 * stream sequence number reaches the value 65535 the next
2494 	 * stream sequence number shall be set to 0.
2495 	 */
2496 
2497 	if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams,
2498 			     asoc->c.sinit_max_instreams, gfp))
2499 		goto clean_up;
2500 
2501 	/* Update frag_point when stream_interleave may get changed. */
2502 	sctp_assoc_update_frag_point(asoc);
2503 
2504 	if (!asoc->temp && sctp_assoc_set_id(asoc, gfp))
2505 		goto clean_up;
2506 
2507 	/* ADDIP Section 4.1 ASCONF Chunk Procedures
2508 	 *
2509 	 * When an endpoint has an ASCONF signaled change to be sent to the
2510 	 * remote endpoint it should do the following:
2511 	 * ...
2512 	 * A2) A serial number should be assigned to the Chunk. The serial
2513 	 * number should be a monotonically increasing number. All serial
2514 	 * numbers are defined to be initialized at the start of the
2515 	 * association to the same value as the Initial TSN.
2516 	 */
2517 	asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2518 	return 1;
2519 
2520 clean_up:
2521 	/* Release the transport structures. */
2522 	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2523 		transport = list_entry(pos, struct sctp_transport, transports);
2524 		if (transport->state != SCTP_ACTIVE)
2525 			sctp_assoc_rm_peer(asoc, transport);
2526 	}
2527 
2528 nomem:
2529 	return 0;
2530 }
2531 
2532 
2533 /* Update asoc with the option described in param.
2534  *
2535  * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2536  *
2537  * asoc is the association to update.
2538  * param is the variable length parameter to use for update.
2539  * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2540  * If the current packet is an INIT we want to minimize the amount of
2541  * work we do.  In particular, we should not build transport
2542  * structures for the addresses.
2543  */
2544 static int sctp_process_param(struct sctp_association *asoc,
2545 			      union sctp_params param,
2546 			      const union sctp_addr *peer_addr,
2547 			      gfp_t gfp)
2548 {
2549 	struct sctp_endpoint *ep = asoc->ep;
2550 	union sctp_addr_param *addr_param;
2551 	struct net *net = asoc->base.net;
2552 	struct sctp_transport *t;
2553 	enum sctp_scope scope;
2554 	union sctp_addr addr;
2555 	struct sctp_af *af;
2556 	int retval = 1, i;
2557 	u32 stale;
2558 	__u16 sat;
2559 
2560 	/* We maintain all INIT parameters in network byte order all the
2561 	 * time.  This allows us to not worry about whether the parameters
2562 	 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2563 	 */
2564 	switch (param.p->type) {
2565 	case SCTP_PARAM_IPV6_ADDRESS:
2566 	case SCTP_PARAM_IPV4_ADDRESS:
2567 		af = sctp_get_af_specific(param_type2af(param.p->type));
2568 		if (!af || !af->from_addr_param(&addr, param.addr,
2569 						htons(asoc->peer.port), 0) ||
2570 		    !af->addr_valid(&addr, sctp_sk(asoc->base.sk), NULL))
2571 			break;
2572 		scope = sctp_scope(peer_addr);
2573 		if (sctp_in_scope(net, &addr, scope))
2574 			if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2575 				return 0;
2576 		break;
2577 
2578 	case SCTP_PARAM_COOKIE_PRESERVATIVE:
2579 		if (!net->sctp.cookie_preserve_enable)
2580 			break;
2581 
2582 		stale = ntohl(param.life->lifespan_increment);
2583 
2584 		/* Suggested Cookie Life span increment's unit is msec,
2585 		 * (1/1000sec).
2586 		 */
2587 		asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
2588 		break;
2589 
2590 	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2591 		/* Turn off the default values first so we'll know which
2592 		 * ones are really set by the peer.
2593 		 */
2594 		asoc->peer.ipv4_address = 0;
2595 		asoc->peer.ipv6_address = 0;
2596 
2597 		/* Assume that peer supports the address family
2598 		 * by which it sends a packet.
2599 		 */
2600 		if (peer_addr->sa.sa_family == AF_INET6)
2601 			asoc->peer.ipv6_address = 1;
2602 		else if (peer_addr->sa.sa_family == AF_INET)
2603 			asoc->peer.ipv4_address = 1;
2604 
2605 		/* Cycle through address types; avoid divide by 0. */
2606 		sat = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2607 		if (sat)
2608 			sat /= sizeof(__u16);
2609 
2610 		for (i = 0; i < sat; ++i) {
2611 			switch (param.sat->types[i]) {
2612 			case SCTP_PARAM_IPV4_ADDRESS:
2613 				asoc->peer.ipv4_address = 1;
2614 				break;
2615 
2616 			case SCTP_PARAM_IPV6_ADDRESS:
2617 				if (PF_INET6 == asoc->base.sk->sk_family)
2618 					asoc->peer.ipv6_address = 1;
2619 				break;
2620 
2621 			default: /* Just ignore anything else.  */
2622 				break;
2623 			}
2624 		}
2625 		break;
2626 
2627 	case SCTP_PARAM_STATE_COOKIE:
2628 		asoc->peer.cookie_len =
2629 			ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2630 		kfree(asoc->peer.cookie);
2631 		asoc->peer.cookie = kmemdup(param.cookie->body, asoc->peer.cookie_len, gfp);
2632 		if (!asoc->peer.cookie)
2633 			retval = 0;
2634 		break;
2635 
2636 	case SCTP_PARAM_HEARTBEAT_INFO:
2637 		/* Would be odd to receive, but it causes no problems. */
2638 		break;
2639 
2640 	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2641 		/* Rejected during verify stage. */
2642 		break;
2643 
2644 	case SCTP_PARAM_ECN_CAPABLE:
2645 		if (asoc->ep->ecn_enable) {
2646 			asoc->peer.ecn_capable = 1;
2647 			break;
2648 		}
2649 		/* Fall Through */
2650 		goto fall_through;
2651 
2652 
2653 	case SCTP_PARAM_ADAPTATION_LAYER_IND:
2654 		asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
2655 		break;
2656 
2657 	case SCTP_PARAM_SET_PRIMARY:
2658 		if (!ep->asconf_enable)
2659 			goto fall_through;
2660 
2661 		addr_param = param.v + sizeof(struct sctp_addip_param);
2662 		if (ntohs(addr_param->p.length) >
2663 		    ntohs(param.p->length) - sizeof(struct sctp_addip_param))
2664 			break;
2665 
2666 		af = sctp_get_af_specific(param_type2af(addr_param->p.type));
2667 		if (!af)
2668 			break;
2669 
2670 		if (!af->from_addr_param(&addr, addr_param,
2671 					 htons(asoc->peer.port), 0))
2672 			break;
2673 
2674 		if (!af->addr_valid(&addr, NULL, NULL))
2675 			break;
2676 
2677 		t = sctp_assoc_lookup_paddr(asoc, &addr);
2678 		if (!t)
2679 			break;
2680 
2681 		sctp_assoc_set_primary(asoc, t);
2682 		break;
2683 
2684 	case SCTP_PARAM_SUPPORTED_EXT:
2685 		sctp_process_ext_param(asoc, param);
2686 		break;
2687 
2688 	case SCTP_PARAM_FWD_TSN_SUPPORT:
2689 		if (asoc->ep->prsctp_enable) {
2690 			asoc->peer.prsctp_capable = 1;
2691 			break;
2692 		}
2693 		/* Fall Through */
2694 		goto fall_through;
2695 
2696 	case SCTP_PARAM_RANDOM:
2697 		if (!ep->auth_enable)
2698 			goto fall_through;
2699 
2700 		/* Save peer's random parameter */
2701 		kfree(asoc->peer.peer_random);
2702 		asoc->peer.peer_random = kmemdup(param.p,
2703 					    ntohs(param.p->length), gfp);
2704 		if (!asoc->peer.peer_random) {
2705 			retval = 0;
2706 			break;
2707 		}
2708 		break;
2709 
2710 	case SCTP_PARAM_HMAC_ALGO:
2711 		if (!ep->auth_enable)
2712 			goto fall_through;
2713 
2714 		/* Save peer's HMAC list */
2715 		kfree(asoc->peer.peer_hmacs);
2716 		asoc->peer.peer_hmacs = kmemdup(param.p,
2717 					    ntohs(param.p->length), gfp);
2718 		if (!asoc->peer.peer_hmacs) {
2719 			retval = 0;
2720 			break;
2721 		}
2722 
2723 		/* Set the default HMAC the peer requested*/
2724 		sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2725 		break;
2726 
2727 	case SCTP_PARAM_CHUNKS:
2728 		if (!ep->auth_enable)
2729 			goto fall_through;
2730 
2731 		kfree(asoc->peer.peer_chunks);
2732 		asoc->peer.peer_chunks = kmemdup(param.p,
2733 					    ntohs(param.p->length), gfp);
2734 		if (!asoc->peer.peer_chunks)
2735 			retval = 0;
2736 		break;
2737 fall_through:
2738 	default:
2739 		/* Any unrecognized parameters should have been caught
2740 		 * and handled by sctp_verify_param() which should be
2741 		 * called prior to this routine.  Simply log the error
2742 		 * here.
2743 		 */
2744 		pr_debug("%s: ignoring param:%d for association:%p.\n",
2745 			 __func__, ntohs(param.p->type), asoc);
2746 		break;
2747 	}
2748 
2749 	return retval;
2750 }
2751 
2752 /* Select a new verification tag.  */
2753 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2754 {
2755 	/* I believe that this random number generator complies with RFC1750.
2756 	 * A tag of 0 is reserved for special cases (e.g. INIT).
2757 	 */
2758 	__u32 x;
2759 
2760 	do {
2761 		x = get_random_u32();
2762 	} while (x == 0);
2763 
2764 	return x;
2765 }
2766 
2767 /* Select an initial TSN to send during startup.  */
2768 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2769 {
2770 	__u32 retval;
2771 
2772 	retval = get_random_u32();
2773 	return retval;
2774 }
2775 
2776 /*
2777  * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2778  *      0                   1                   2                   3
2779  *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2780  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2781  *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
2782  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2783  *     |                       Serial Number                           |
2784  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2785  *     |                    Address Parameter                          |
2786  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2787  *     |                     ASCONF Parameter #1                       |
2788  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2789  *     \                                                               \
2790  *     /                             ....                              /
2791  *     \                                                               \
2792  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2793  *     |                     ASCONF Parameter #N                       |
2794  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2795  *
2796  * Address Parameter and other parameter will not be wrapped in this function
2797  */
2798 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2799 					   union sctp_addr *addr,
2800 					   int vparam_len)
2801 {
2802 	struct sctp_addiphdr asconf;
2803 	struct sctp_chunk *retval;
2804 	int length = sizeof(asconf) + vparam_len;
2805 	union sctp_addr_param addrparam;
2806 	int addrlen;
2807 	struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2808 
2809 	addrlen = af->to_addr_param(addr, &addrparam);
2810 	if (!addrlen)
2811 		return NULL;
2812 	length += addrlen;
2813 
2814 	/* Create the chunk.  */
2815 	retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
2816 				   GFP_ATOMIC);
2817 	if (!retval)
2818 		return NULL;
2819 
2820 	asconf.serial = htonl(asoc->addip_serial++);
2821 
2822 	retval->subh.addip_hdr =
2823 		sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2824 	retval->param_hdr.v =
2825 		sctp_addto_chunk(retval, addrlen, &addrparam);
2826 
2827 	return retval;
2828 }
2829 
2830 /* ADDIP
2831  * 3.2.1 Add IP Address
2832  * 	0                   1                   2                   3
2833  * 	0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2834  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2835  *     |        Type = 0xC001          |    Length = Variable          |
2836  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2837  *     |               ASCONF-Request Correlation ID                   |
2838  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2839  *     |                       Address Parameter                       |
2840  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2841  *
2842  * 3.2.2 Delete IP Address
2843  * 	0                   1                   2                   3
2844  * 	0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2845  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2846  *     |        Type = 0xC002          |    Length = Variable          |
2847  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2848  *     |               ASCONF-Request Correlation ID                   |
2849  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2850  *     |                       Address Parameter                       |
2851  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2852  *
2853  */
2854 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2855 					      union sctp_addr *laddr,
2856 					      struct sockaddr *addrs,
2857 					      int addrcnt, __be16 flags)
2858 {
2859 	union sctp_addr_param addr_param;
2860 	struct sctp_addip_param	param;
2861 	int paramlen = sizeof(param);
2862 	struct sctp_chunk *retval;
2863 	int addr_param_len = 0;
2864 	union sctp_addr *addr;
2865 	int totallen = 0, i;
2866 	int del_pickup = 0;
2867 	struct sctp_af *af;
2868 	void *addr_buf;
2869 
2870 	/* Get total length of all the address parameters. */
2871 	addr_buf = addrs;
2872 	for (i = 0; i < addrcnt; i++) {
2873 		addr = addr_buf;
2874 		af = sctp_get_af_specific(addr->v4.sin_family);
2875 		addr_param_len = af->to_addr_param(addr, &addr_param);
2876 
2877 		totallen += paramlen;
2878 		totallen += addr_param_len;
2879 
2880 		addr_buf += af->sockaddr_len;
2881 		if (asoc->asconf_addr_del_pending && !del_pickup) {
2882 			/* reuse the parameter length from the same scope one */
2883 			totallen += paramlen;
2884 			totallen += addr_param_len;
2885 			del_pickup = 1;
2886 
2887 			pr_debug("%s: picked same-scope del_pending addr, "
2888 				 "totallen for all addresses is %d\n",
2889 				 __func__, totallen);
2890 		}
2891 	}
2892 
2893 	/* Create an asconf chunk with the required length. */
2894 	retval = sctp_make_asconf(asoc, laddr, totallen);
2895 	if (!retval)
2896 		return NULL;
2897 
2898 	/* Add the address parameters to the asconf chunk. */
2899 	addr_buf = addrs;
2900 	for (i = 0; i < addrcnt; i++) {
2901 		addr = addr_buf;
2902 		af = sctp_get_af_specific(addr->v4.sin_family);
2903 		addr_param_len = af->to_addr_param(addr, &addr_param);
2904 		param.param_hdr.type = flags;
2905 		param.param_hdr.length = htons(paramlen + addr_param_len);
2906 		param.crr_id = htonl(i);
2907 
2908 		sctp_addto_chunk(retval, paramlen, &param);
2909 		sctp_addto_chunk(retval, addr_param_len, &addr_param);
2910 
2911 		addr_buf += af->sockaddr_len;
2912 	}
2913 	if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
2914 		addr = asoc->asconf_addr_del_pending;
2915 		af = sctp_get_af_specific(addr->v4.sin_family);
2916 		addr_param_len = af->to_addr_param(addr, &addr_param);
2917 		param.param_hdr.type = SCTP_PARAM_DEL_IP;
2918 		param.param_hdr.length = htons(paramlen + addr_param_len);
2919 		param.crr_id = htonl(i);
2920 
2921 		sctp_addto_chunk(retval, paramlen, &param);
2922 		sctp_addto_chunk(retval, addr_param_len, &addr_param);
2923 	}
2924 	return retval;
2925 }
2926 
2927 /* ADDIP
2928  * 3.2.4 Set Primary IP Address
2929  *	0                   1                   2                   3
2930  *	0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2931  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2932  *     |        Type =0xC004           |    Length = Variable          |
2933  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2934  *     |               ASCONF-Request Correlation ID                   |
2935  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2936  *     |                       Address Parameter                       |
2937  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2938  *
2939  * Create an ASCONF chunk with Set Primary IP address parameter.
2940  */
2941 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2942 					     union sctp_addr *addr)
2943 {
2944 	struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2945 	union sctp_addr_param addrparam;
2946 	struct sctp_addip_param	param;
2947 	struct sctp_chunk *retval;
2948 	int len = sizeof(param);
2949 	int addrlen;
2950 
2951 	addrlen = af->to_addr_param(addr, &addrparam);
2952 	if (!addrlen)
2953 		return NULL;
2954 	len += addrlen;
2955 
2956 	/* Create the chunk and make asconf header. */
2957 	retval = sctp_make_asconf(asoc, addr, len);
2958 	if (!retval)
2959 		return NULL;
2960 
2961 	param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2962 	param.param_hdr.length = htons(len);
2963 	param.crr_id = 0;
2964 
2965 	sctp_addto_chunk(retval, sizeof(param), &param);
2966 	sctp_addto_chunk(retval, addrlen, &addrparam);
2967 
2968 	return retval;
2969 }
2970 
2971 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2972  *      0                   1                   2                   3
2973  *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2974  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2975  *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
2976  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2977  *     |                       Serial Number                           |
2978  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2979  *     |                 ASCONF Parameter Response#1                   |
2980  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2981  *     \                                                               \
2982  *     /                             ....                              /
2983  *     \                                                               \
2984  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2985  *     |                 ASCONF Parameter Response#N                   |
2986  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2987  *
2988  * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2989  */
2990 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2991 					       __u32 serial, int vparam_len)
2992 {
2993 	struct sctp_addiphdr asconf;
2994 	struct sctp_chunk *retval;
2995 	int length = sizeof(asconf) + vparam_len;
2996 
2997 	/* Create the chunk.  */
2998 	retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
2999 				   GFP_ATOMIC);
3000 	if (!retval)
3001 		return NULL;
3002 
3003 	asconf.serial = htonl(serial);
3004 
3005 	retval->subh.addip_hdr =
3006 		sctp_addto_chunk(retval, sizeof(asconf), &asconf);
3007 
3008 	return retval;
3009 }
3010 
3011 /* Add response parameters to an ASCONF_ACK chunk. */
3012 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
3013 				     __be16 err_code,
3014 				     struct sctp_addip_param *asconf_param)
3015 {
3016 	struct sctp_addip_param ack_param;
3017 	struct sctp_errhdr err_param;
3018 	int asconf_param_len = 0;
3019 	int err_param_len = 0;
3020 	__be16 response_type;
3021 
3022 	if (SCTP_ERROR_NO_ERROR == err_code) {
3023 		response_type = SCTP_PARAM_SUCCESS_REPORT;
3024 	} else {
3025 		response_type = SCTP_PARAM_ERR_CAUSE;
3026 		err_param_len = sizeof(err_param);
3027 		if (asconf_param)
3028 			asconf_param_len =
3029 				 ntohs(asconf_param->param_hdr.length);
3030 	}
3031 
3032 	/* Add Success Indication or Error Cause Indication parameter. */
3033 	ack_param.param_hdr.type = response_type;
3034 	ack_param.param_hdr.length = htons(sizeof(ack_param) +
3035 					   err_param_len +
3036 					   asconf_param_len);
3037 	ack_param.crr_id = crr_id;
3038 	sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
3039 
3040 	if (SCTP_ERROR_NO_ERROR == err_code)
3041 		return;
3042 
3043 	/* Add Error Cause parameter. */
3044 	err_param.cause = err_code;
3045 	err_param.length = htons(err_param_len + asconf_param_len);
3046 	sctp_addto_chunk(chunk, err_param_len, &err_param);
3047 
3048 	/* Add the failed TLV copied from ASCONF chunk. */
3049 	if (asconf_param)
3050 		sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
3051 }
3052 
3053 /* Process a asconf parameter. */
3054 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
3055 					struct sctp_chunk *asconf,
3056 					struct sctp_addip_param *asconf_param)
3057 {
3058 	union sctp_addr_param *addr_param;
3059 	struct sctp_transport *peer;
3060 	union sctp_addr	addr;
3061 	struct sctp_af *af;
3062 
3063 	if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
3064 	    asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
3065 	    asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
3066 		return SCTP_ERROR_UNKNOWN_PARAM;
3067 
3068 	addr_param = (void *)asconf_param + sizeof(*asconf_param);
3069 	if (ntohs(addr_param->p.length) >
3070 	    ntohs(asconf_param->param_hdr.length) - sizeof(*asconf_param))
3071 		return SCTP_ERROR_PROTO_VIOLATION;
3072 
3073 	switch (addr_param->p.type) {
3074 	case SCTP_PARAM_IPV6_ADDRESS:
3075 		if (!asoc->peer.ipv6_address)
3076 			return SCTP_ERROR_DNS_FAILED;
3077 		break;
3078 	case SCTP_PARAM_IPV4_ADDRESS:
3079 		if (!asoc->peer.ipv4_address)
3080 			return SCTP_ERROR_DNS_FAILED;
3081 		break;
3082 	default:
3083 		return SCTP_ERROR_DNS_FAILED;
3084 	}
3085 
3086 	af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3087 	if (unlikely(!af))
3088 		return SCTP_ERROR_DNS_FAILED;
3089 
3090 	if (!af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0))
3091 		return SCTP_ERROR_DNS_FAILED;
3092 
3093 	/* ADDIP 4.2.1  This parameter MUST NOT contain a broadcast
3094 	 * or multicast address.
3095 	 * (note: wildcard is permitted and requires special handling so
3096 	 *  make sure we check for that)
3097 	 */
3098 	if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
3099 		return SCTP_ERROR_DNS_FAILED;
3100 
3101 	switch (asconf_param->param_hdr.type) {
3102 	case SCTP_PARAM_ADD_IP:
3103 		/* Section 4.2.1:
3104 		 * If the address 0.0.0.0 or ::0 is provided, the source
3105 		 * address of the packet MUST be added.
3106 		 */
3107 		if (af->is_any(&addr))
3108 			memcpy(&addr, &asconf->source, sizeof(addr));
3109 
3110 		if (security_sctp_bind_connect(asoc->ep->base.sk,
3111 					       SCTP_PARAM_ADD_IP,
3112 					       (struct sockaddr *)&addr,
3113 					       af->sockaddr_len))
3114 			return SCTP_ERROR_REQ_REFUSED;
3115 
3116 		/* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
3117 		 * request and does not have the local resources to add this
3118 		 * new address to the association, it MUST return an Error
3119 		 * Cause TLV set to the new error code 'Operation Refused
3120 		 * Due to Resource Shortage'.
3121 		 */
3122 
3123 		peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
3124 		if (!peer)
3125 			return SCTP_ERROR_RSRC_LOW;
3126 
3127 		/* Start the heartbeat timer. */
3128 		sctp_transport_reset_hb_timer(peer);
3129 		asoc->new_transport = peer;
3130 		break;
3131 	case SCTP_PARAM_DEL_IP:
3132 		/* ADDIP 4.3 D7) If a request is received to delete the
3133 		 * last remaining IP address of a peer endpoint, the receiver
3134 		 * MUST send an Error Cause TLV with the error cause set to the
3135 		 * new error code 'Request to Delete Last Remaining IP Address'.
3136 		 */
3137 		if (asoc->peer.transport_count == 1)
3138 			return SCTP_ERROR_DEL_LAST_IP;
3139 
3140 		/* ADDIP 4.3 D8) If a request is received to delete an IP
3141 		 * address which is also the source address of the IP packet
3142 		 * which contained the ASCONF chunk, the receiver MUST reject
3143 		 * this request. To reject the request the receiver MUST send
3144 		 * an Error Cause TLV set to the new error code 'Request to
3145 		 * Delete Source IP Address'
3146 		 */
3147 		if (sctp_cmp_addr_exact(&asconf->source, &addr))
3148 			return SCTP_ERROR_DEL_SRC_IP;
3149 
3150 		/* Section 4.2.2
3151 		 * If the address 0.0.0.0 or ::0 is provided, all
3152 		 * addresses of the peer except	the source address of the
3153 		 * packet MUST be deleted.
3154 		 */
3155 		if (af->is_any(&addr)) {
3156 			sctp_assoc_set_primary(asoc, asconf->transport);
3157 			sctp_assoc_del_nonprimary_peers(asoc,
3158 							asconf->transport);
3159 			return SCTP_ERROR_NO_ERROR;
3160 		}
3161 
3162 		/* If the address is not part of the association, the
3163 		 * ASCONF-ACK with Error Cause Indication Parameter
3164 		 * which including cause of Unresolvable Address should
3165 		 * be sent.
3166 		 */
3167 		peer = sctp_assoc_lookup_paddr(asoc, &addr);
3168 		if (!peer)
3169 			return SCTP_ERROR_DNS_FAILED;
3170 
3171 		/* Don't free asconf->transport; a later wildcard DEL-IP
3172 		 * parameter reuses it.
3173 		 */
3174 		if (peer == asconf->transport)
3175 			return SCTP_ERROR_REQ_REFUSED;
3176 
3177 		sctp_assoc_rm_peer(asoc, peer);
3178 		break;
3179 	case SCTP_PARAM_SET_PRIMARY:
3180 		/* ADDIP Section 4.2.4
3181 		 * If the address 0.0.0.0 or ::0 is provided, the receiver
3182 		 * MAY mark the source address of the packet as its
3183 		 * primary.
3184 		 */
3185 		if (af->is_any(&addr))
3186 			memcpy(&addr, sctp_source(asconf), sizeof(addr));
3187 
3188 		if (security_sctp_bind_connect(asoc->ep->base.sk,
3189 					       SCTP_PARAM_SET_PRIMARY,
3190 					       (struct sockaddr *)&addr,
3191 					       af->sockaddr_len))
3192 			return SCTP_ERROR_REQ_REFUSED;
3193 
3194 		peer = sctp_assoc_lookup_paddr(asoc, &addr);
3195 		if (!peer)
3196 			return SCTP_ERROR_DNS_FAILED;
3197 
3198 		sctp_assoc_set_primary(asoc, peer);
3199 		break;
3200 	}
3201 
3202 	return SCTP_ERROR_NO_ERROR;
3203 }
3204 
3205 /* Verify the ASCONF packet before we process it. */
3206 bool sctp_verify_asconf(const struct sctp_association *asoc,
3207 			struct sctp_chunk *chunk, bool addr_param_needed,
3208 			struct sctp_paramhdr **errp)
3209 {
3210 	struct sctp_addip_chunk *addip;
3211 	bool addr_param_seen = false;
3212 	union sctp_params param;
3213 
3214 	addip = (struct sctp_addip_chunk *)chunk->chunk_hdr;
3215 	sctp_walk_params(param, addip) {
3216 		size_t length = ntohs(param.p->length);
3217 
3218 		*errp = param.p;
3219 		switch (param.p->type) {
3220 		case SCTP_PARAM_ERR_CAUSE:
3221 			if (length < sizeof(struct sctp_addip_param) +
3222 				     sizeof(struct sctp_errhdr))
3223 				return false;
3224 			break;
3225 		case SCTP_PARAM_IPV4_ADDRESS:
3226 			if (length != sizeof(struct sctp_ipv4addr_param))
3227 				return false;
3228 			/* ensure there is only one addr param and it's in the
3229 			 * beginning of addip_hdr params, or we reject it.
3230 			 */
3231 			if (param.v != (addip + 1))
3232 				return false;
3233 			addr_param_seen = true;
3234 			break;
3235 		case SCTP_PARAM_IPV6_ADDRESS:
3236 			if (length != sizeof(struct sctp_ipv6addr_param))
3237 				return false;
3238 			if (param.v != (addip + 1))
3239 				return false;
3240 			addr_param_seen = true;
3241 			break;
3242 		case SCTP_PARAM_ADD_IP:
3243 		case SCTP_PARAM_DEL_IP:
3244 		case SCTP_PARAM_SET_PRIMARY:
3245 			/* In ASCONF chunks, these need to be first. */
3246 			if (addr_param_needed && !addr_param_seen)
3247 				return false;
3248 			length = ntohs(param.addip->param_hdr.length);
3249 			if (length < sizeof(struct sctp_addip_param) +
3250 				     sizeof(**errp))
3251 				return false;
3252 			break;
3253 		case SCTP_PARAM_SUCCESS_REPORT:
3254 		case SCTP_PARAM_ADAPTATION_LAYER_IND:
3255 			if (length != sizeof(struct sctp_addip_param))
3256 				return false;
3257 			break;
3258 		default:
3259 			/* This is unknown to us, reject! */
3260 			return false;
3261 		}
3262 	}
3263 
3264 	/* Remaining sanity checks. */
3265 	if (addr_param_needed && !addr_param_seen)
3266 		return false;
3267 	if (!addr_param_needed && addr_param_seen)
3268 		return false;
3269 	if (param.v != chunk->chunk_end)
3270 		return false;
3271 
3272 	return true;
3273 }
3274 
3275 /* Process an incoming ASCONF chunk with the next expected serial no. and
3276  * return an ASCONF_ACK chunk to be sent in response.
3277  */
3278 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3279 				       struct sctp_chunk *asconf)
3280 {
3281 	union sctp_addr_param *addr_param;
3282 	struct sctp_addip_chunk *addip;
3283 	struct sctp_chunk *asconf_ack;
3284 	bool all_param_pass = true;
3285 	struct sctp_addiphdr *hdr;
3286 	int length = 0, chunk_len;
3287 	union sctp_params param;
3288 	__be16 err_code;
3289 	__u32 serial;
3290 
3291 	addip = (struct sctp_addip_chunk *)asconf->chunk_hdr;
3292 	chunk_len = ntohs(asconf->chunk_hdr->length) -
3293 		    sizeof(struct sctp_chunkhdr);
3294 	hdr = (struct sctp_addiphdr *)asconf->skb->data;
3295 	serial = ntohl(hdr->serial);
3296 
3297 	/* Skip the addiphdr and store a pointer to address parameter.  */
3298 	length = sizeof(*hdr);
3299 	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3300 	chunk_len -= length;
3301 
3302 	/* Skip the address parameter and store a pointer to the first
3303 	 * asconf parameter.
3304 	 */
3305 	length = ntohs(addr_param->p.length);
3306 	chunk_len -= length;
3307 
3308 	/* create an ASCONF_ACK chunk.
3309 	 * Based on the definitions of parameters, we know that the size of
3310 	 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
3311 	 * parameters.
3312 	 */
3313 	asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
3314 	if (!asconf_ack)
3315 		goto done;
3316 
3317 	/* Process the TLVs contained within the ASCONF chunk. */
3318 	sctp_walk_params(param, addip) {
3319 		/* Skip preceding address parameters. */
3320 		if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
3321 		    param.p->type == SCTP_PARAM_IPV6_ADDRESS)
3322 			continue;
3323 
3324 		err_code = sctp_process_asconf_param(asoc, asconf,
3325 						     param.addip);
3326 		/* ADDIP 4.1 A7)
3327 		 * If an error response is received for a TLV parameter,
3328 		 * all TLVs with no response before the failed TLV are
3329 		 * considered successful if not reported.  All TLVs after
3330 		 * the failed response are considered unsuccessful unless
3331 		 * a specific success indication is present for the parameter.
3332 		 */
3333 		if (err_code != SCTP_ERROR_NO_ERROR)
3334 			all_param_pass = false;
3335 		if (!all_param_pass)
3336 			sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
3337 						 err_code, param.addip);
3338 
3339 		/* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3340 		 * an IP address sends an 'Out of Resource' in its response, it
3341 		 * MUST also fail any subsequent add or delete requests bundled
3342 		 * in the ASCONF.
3343 		 */
3344 		if (err_code == SCTP_ERROR_RSRC_LOW)
3345 			goto done;
3346 	}
3347 done:
3348 	/* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3349 	 * after freeing the reference to old asconf ack if any.
3350 	 */
3351 	if (asconf_ack) {
3352 		asoc->peer.addip_serial++;
3353 		sctp_chunk_hold(asconf_ack);
3354 		list_add_tail(&asconf_ack->transmitted_list,
3355 			      &asoc->asconf_ack_list);
3356 	}
3357 
3358 	return asconf_ack;
3359 }
3360 
3361 /* Process a asconf parameter that is successfully acked. */
3362 static void sctp_asconf_param_success(struct sctp_association *asoc,
3363 				      struct sctp_addip_param *asconf_param)
3364 {
3365 	struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3366 	union sctp_addr_param *addr_param;
3367 	struct sctp_sockaddr_entry *saddr;
3368 	struct sctp_transport *transport;
3369 	union sctp_addr	addr;
3370 	struct sctp_af *af;
3371 
3372 	addr_param = (void *)asconf_param + sizeof(*asconf_param);
3373 
3374 	/* We have checked the packet before, so we do not check again.	*/
3375 	af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3376 	if (!af->from_addr_param(&addr, addr_param, htons(bp->port), 0))
3377 		return;
3378 
3379 	switch (asconf_param->param_hdr.type) {
3380 	case SCTP_PARAM_ADD_IP:
3381 		/* This is always done in BH context with a socket lock
3382 		 * held, so the list can not change.
3383 		 */
3384 		local_bh_disable();
3385 		list_for_each_entry(saddr, &bp->address_list, list) {
3386 			if (sctp_cmp_addr_exact(&saddr->a, &addr))
3387 				saddr->state = SCTP_ADDR_SRC;
3388 		}
3389 		local_bh_enable();
3390 		list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3391 				transports) {
3392 			sctp_transport_dst_release(transport);
3393 		}
3394 		break;
3395 	case SCTP_PARAM_DEL_IP:
3396 		local_bh_disable();
3397 		sctp_del_bind_addr(bp, &addr);
3398 		if (asoc->asconf_addr_del_pending != NULL &&
3399 		    sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
3400 			kfree(asoc->asconf_addr_del_pending);
3401 			asoc->asconf_addr_del_pending = NULL;
3402 		}
3403 		local_bh_enable();
3404 		list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3405 				transports) {
3406 			sctp_transport_dst_release(transport);
3407 		}
3408 		break;
3409 	default:
3410 		break;
3411 	}
3412 }
3413 
3414 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3415  * for the given asconf parameter.  If there is no response for this parameter,
3416  * return the error code based on the third argument 'no_err'.
3417  * ADDIP 4.1
3418  * A7) If an error response is received for a TLV parameter, all TLVs with no
3419  * response before the failed TLV are considered successful if not reported.
3420  * All TLVs after the failed response are considered unsuccessful unless a
3421  * specific success indication is present for the parameter.
3422  */
3423 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3424 				       struct sctp_addip_param *asconf_param,
3425 				       int no_err)
3426 {
3427 	struct sctp_addip_param	*asconf_ack_param;
3428 	struct sctp_errhdr *err_param;
3429 	int asconf_ack_len;
3430 	__be16 err_code;
3431 	int length;
3432 
3433 	if (no_err)
3434 		err_code = SCTP_ERROR_NO_ERROR;
3435 	else
3436 		err_code = SCTP_ERROR_REQ_REFUSED;
3437 
3438 	asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3439 			 sizeof(struct sctp_chunkhdr);
3440 
3441 	/* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3442 	 * the first asconf_ack parameter.
3443 	 */
3444 	length = sizeof(struct sctp_addiphdr);
3445 	asconf_ack_param = (struct sctp_addip_param *)(asconf_ack->skb->data +
3446 						       length);
3447 	asconf_ack_len -= length;
3448 
3449 	while (asconf_ack_len > 0) {
3450 		if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3451 			switch (asconf_ack_param->param_hdr.type) {
3452 			case SCTP_PARAM_SUCCESS_REPORT:
3453 				return SCTP_ERROR_NO_ERROR;
3454 			case SCTP_PARAM_ERR_CAUSE:
3455 				length = sizeof(*asconf_ack_param);
3456 				err_param = (void *)asconf_ack_param + length;
3457 				return err_param->cause;
3458 			default:
3459 				return SCTP_ERROR_INV_PARAM;
3460 			}
3461 		}
3462 
3463 		length = ntohs(asconf_ack_param->param_hdr.length);
3464 		asconf_ack_param = (void *)asconf_ack_param + SCTP_PAD4(length);
3465 		asconf_ack_len -= SCTP_PAD4(length);
3466 	}
3467 
3468 	return err_code;
3469 }
3470 
3471 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3472 int sctp_process_asconf_ack(struct sctp_association *asoc,
3473 			    struct sctp_chunk *asconf_ack)
3474 {
3475 	struct sctp_chunk *asconf = asoc->addip_last_asconf;
3476 	struct sctp_addip_param *asconf_param;
3477 	__be16 err_code = SCTP_ERROR_NO_ERROR;
3478 	union sctp_addr_param *addr_param;
3479 	int asconf_len = asconf->skb->len;
3480 	int all_param_pass = 0;
3481 	int length = 0;
3482 	int no_err = 1;
3483 	int retval = 0;
3484 
3485 	/* Skip the chunkhdr and addiphdr from the last asconf sent and store
3486 	 * a pointer to address parameter.
3487 	 */
3488 	length = sizeof(struct sctp_addip_chunk);
3489 	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3490 	asconf_len -= length;
3491 
3492 	/* Skip the address parameter in the last asconf sent and store a
3493 	 * pointer to the first asconf parameter.
3494 	 */
3495 	length = ntohs(addr_param->p.length);
3496 	asconf_param = (void *)addr_param + length;
3497 	asconf_len -= length;
3498 
3499 	/* ADDIP 4.1
3500 	 * A8) If there is no response(s) to specific TLV parameter(s), and no
3501 	 * failures are indicated, then all request(s) are considered
3502 	 * successful.
3503 	 */
3504 	if (asconf_ack->skb->len == sizeof(struct sctp_addiphdr))
3505 		all_param_pass = 1;
3506 
3507 	/* Process the TLVs contained in the last sent ASCONF chunk. */
3508 	while (asconf_len > 0) {
3509 		if (all_param_pass)
3510 			err_code = SCTP_ERROR_NO_ERROR;
3511 		else {
3512 			err_code = sctp_get_asconf_response(asconf_ack,
3513 							    asconf_param,
3514 							    no_err);
3515 			if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3516 				no_err = 0;
3517 		}
3518 
3519 		switch (err_code) {
3520 		case SCTP_ERROR_NO_ERROR:
3521 			sctp_asconf_param_success(asoc, asconf_param);
3522 			break;
3523 
3524 		case SCTP_ERROR_RSRC_LOW:
3525 			retval = 1;
3526 			break;
3527 
3528 		case SCTP_ERROR_UNKNOWN_PARAM:
3529 			/* Disable sending this type of asconf parameter in
3530 			 * future.
3531 			 */
3532 			asoc->peer.addip_disabled_mask |=
3533 				asconf_param->param_hdr.type;
3534 			break;
3535 
3536 		case SCTP_ERROR_REQ_REFUSED:
3537 		case SCTP_ERROR_DEL_LAST_IP:
3538 		case SCTP_ERROR_DEL_SRC_IP:
3539 		default:
3540 			 break;
3541 		}
3542 
3543 		/* Skip the processed asconf parameter and move to the next
3544 		 * one.
3545 		 */
3546 		length = ntohs(asconf_param->param_hdr.length);
3547 		asconf_param = (void *)asconf_param + length;
3548 		asconf_len -= length;
3549 	}
3550 
3551 	if (no_err && asoc->src_out_of_asoc_ok) {
3552 		asoc->src_out_of_asoc_ok = 0;
3553 		sctp_transport_immediate_rtx(asoc->peer.primary_path);
3554 	}
3555 
3556 	/* Free the cached last sent asconf chunk. */
3557 	list_del_init(&asconf->transmitted_list);
3558 	sctp_chunk_free(asconf);
3559 	asoc->addip_last_asconf = NULL;
3560 
3561 	return retval;
3562 }
3563 
3564 /* Make a FWD TSN chunk. */
3565 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3566 				    __u32 new_cum_tsn, size_t nstreams,
3567 				    struct sctp_fwdtsn_skip *skiplist)
3568 {
3569 	struct sctp_chunk *retval = NULL;
3570 	struct sctp_fwdtsn_hdr ftsn_hdr;
3571 	struct sctp_fwdtsn_skip skip;
3572 	size_t hint;
3573 	int i;
3574 
3575 	hint = (nstreams + 1) * sizeof(__u32);
3576 
3577 	retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
3578 
3579 	if (!retval)
3580 		return NULL;
3581 
3582 	ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3583 	retval->subh.fwdtsn_hdr =
3584 		sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3585 
3586 	for (i = 0; i < nstreams; i++) {
3587 		skip.stream = skiplist[i].stream;
3588 		skip.ssn = skiplist[i].ssn;
3589 		sctp_addto_chunk(retval, sizeof(skip), &skip);
3590 	}
3591 
3592 	return retval;
3593 }
3594 
3595 struct sctp_chunk *sctp_make_ifwdtsn(const struct sctp_association *asoc,
3596 				     __u32 new_cum_tsn, size_t nstreams,
3597 				     struct sctp_ifwdtsn_skip *skiplist)
3598 {
3599 	struct sctp_chunk *retval = NULL;
3600 	struct sctp_ifwdtsn_hdr ftsn_hdr;
3601 	size_t hint;
3602 
3603 	hint = (nstreams + 1) * sizeof(__u32);
3604 
3605 	retval = sctp_make_control(asoc, SCTP_CID_I_FWD_TSN, 0, hint,
3606 				   GFP_ATOMIC);
3607 	if (!retval)
3608 		return NULL;
3609 
3610 	ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3611 	retval->subh.ifwdtsn_hdr =
3612 		sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3613 
3614 	sctp_addto_chunk(retval, nstreams * sizeof(skiplist[0]), skiplist);
3615 
3616 	return retval;
3617 }
3618 
3619 /* RE-CONFIG 3.1 (RE-CONFIG chunk)
3620  *   0                   1                   2                   3
3621  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3622  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3623  *  | Type = 130    |  Chunk Flags  |      Chunk Length             |
3624  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3625  *  \                                                               \
3626  *  /                  Re-configuration Parameter                   /
3627  *  \                                                               \
3628  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3629  *  \                                                               \
3630  *  /             Re-configuration Parameter (optional)             /
3631  *  \                                                               \
3632  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3633  */
3634 static struct sctp_chunk *sctp_make_reconf(const struct sctp_association *asoc,
3635 					   int length)
3636 {
3637 	struct sctp_reconf_chunk *reconf;
3638 	struct sctp_chunk *retval;
3639 
3640 	retval = sctp_make_control(asoc, SCTP_CID_RECONF, 0, length,
3641 				   GFP_ATOMIC);
3642 	if (!retval)
3643 		return NULL;
3644 
3645 	reconf = (struct sctp_reconf_chunk *)retval->chunk_hdr;
3646 	retval->param_hdr.v = (u8 *)(reconf + 1);
3647 
3648 	return retval;
3649 }
3650 
3651 /* RE-CONFIG 4.1 (STREAM OUT RESET)
3652  *   0                   1                   2                   3
3653  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3654  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3655  *  |     Parameter Type = 13       | Parameter Length = 16 + 2 * N |
3656  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3657  *  |           Re-configuration Request Sequence Number            |
3658  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3659  *  |           Re-configuration Response Sequence Number           |
3660  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3661  *  |                Sender's Last Assigned TSN                     |
3662  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3663  *  |  Stream Number 1 (optional)   |    Stream Number 2 (optional) |
3664  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3665  *  /                            ......                             /
3666  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3667  *  |  Stream Number N-1 (optional) |    Stream Number N (optional) |
3668  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3669  *
3670  * RE-CONFIG 4.2 (STREAM IN RESET)
3671  *   0                   1                   2                   3
3672  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3673  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3674  *  |     Parameter Type = 14       |  Parameter Length = 8 + 2 * N |
3675  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3676  *  |          Re-configuration Request Sequence Number             |
3677  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3678  *  |  Stream Number 1 (optional)   |    Stream Number 2 (optional) |
3679  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3680  *  /                            ......                             /
3681  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3682  *  |  Stream Number N-1 (optional) |    Stream Number N (optional) |
3683  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3684  */
3685 struct sctp_chunk *sctp_make_strreset_req(
3686 					const struct sctp_association *asoc,
3687 					__u16 stream_num, __be16 *stream_list,
3688 					bool out, bool in)
3689 {
3690 	__u16 stream_len = stream_num * sizeof(__u16);
3691 	struct sctp_strreset_outreq outreq;
3692 	struct sctp_strreset_inreq inreq;
3693 	struct sctp_chunk *retval;
3694 	__u16 outlen, inlen;
3695 
3696 	outlen = (sizeof(outreq) + stream_len) * out;
3697 	inlen = (sizeof(inreq) + stream_len) * in;
3698 
3699 	retval = sctp_make_reconf(asoc, SCTP_PAD4(outlen) + SCTP_PAD4(inlen));
3700 	if (!retval)
3701 		return NULL;
3702 
3703 	if (outlen) {
3704 		outreq.param_hdr.type = SCTP_PARAM_RESET_OUT_REQUEST;
3705 		outreq.param_hdr.length = htons(outlen);
3706 		outreq.request_seq = htonl(asoc->strreset_outseq);
3707 		outreq.response_seq = htonl(asoc->strreset_inseq - 1);
3708 		outreq.send_reset_at_tsn = htonl(asoc->next_tsn - 1);
3709 
3710 		sctp_addto_chunk(retval, sizeof(outreq), &outreq);
3711 
3712 		if (stream_len)
3713 			sctp_addto_chunk(retval, stream_len, stream_list);
3714 	}
3715 
3716 	if (inlen) {
3717 		inreq.param_hdr.type = SCTP_PARAM_RESET_IN_REQUEST;
3718 		inreq.param_hdr.length = htons(inlen);
3719 		inreq.request_seq = htonl(asoc->strreset_outseq + out);
3720 
3721 		sctp_addto_chunk(retval, sizeof(inreq), &inreq);
3722 
3723 		if (stream_len)
3724 			sctp_addto_chunk(retval, stream_len, stream_list);
3725 	}
3726 
3727 	return retval;
3728 }
3729 
3730 /* RE-CONFIG 4.3 (SSN/TSN RESET ALL)
3731  *   0                   1                   2                   3
3732  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3733  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3734  *  |     Parameter Type = 15       |      Parameter Length = 8     |
3735  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3736  *  |         Re-configuration Request Sequence Number              |
3737  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3738  */
3739 struct sctp_chunk *sctp_make_strreset_tsnreq(
3740 					const struct sctp_association *asoc)
3741 {
3742 	struct sctp_strreset_tsnreq tsnreq;
3743 	__u16 length = sizeof(tsnreq);
3744 	struct sctp_chunk *retval;
3745 
3746 	retval = sctp_make_reconf(asoc, length);
3747 	if (!retval)
3748 		return NULL;
3749 
3750 	tsnreq.param_hdr.type = SCTP_PARAM_RESET_TSN_REQUEST;
3751 	tsnreq.param_hdr.length = htons(length);
3752 	tsnreq.request_seq = htonl(asoc->strreset_outseq);
3753 
3754 	sctp_addto_chunk(retval, sizeof(tsnreq), &tsnreq);
3755 
3756 	return retval;
3757 }
3758 
3759 /* RE-CONFIG 4.5/4.6 (ADD STREAM)
3760  *   0                   1                   2                   3
3761  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3762  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3763  *  |     Parameter Type = 17       |      Parameter Length = 12    |
3764  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3765  *  |          Re-configuration Request Sequence Number             |
3766  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3767  *  |      Number of new streams    |         Reserved              |
3768  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3769  */
3770 struct sctp_chunk *sctp_make_strreset_addstrm(
3771 					const struct sctp_association *asoc,
3772 					__u16 out, __u16 in)
3773 {
3774 	struct sctp_strreset_addstrm addstrm;
3775 	__u16 size = sizeof(addstrm);
3776 	struct sctp_chunk *retval;
3777 
3778 	retval = sctp_make_reconf(asoc, (!!out + !!in) * size);
3779 	if (!retval)
3780 		return NULL;
3781 
3782 	if (out) {
3783 		addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_OUT_STREAMS;
3784 		addstrm.param_hdr.length = htons(size);
3785 		addstrm.number_of_streams = htons(out);
3786 		addstrm.request_seq = htonl(asoc->strreset_outseq);
3787 		addstrm.reserved = 0;
3788 
3789 		sctp_addto_chunk(retval, size, &addstrm);
3790 	}
3791 
3792 	if (in) {
3793 		addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_IN_STREAMS;
3794 		addstrm.param_hdr.length = htons(size);
3795 		addstrm.number_of_streams = htons(in);
3796 		addstrm.request_seq = htonl(asoc->strreset_outseq + !!out);
3797 		addstrm.reserved = 0;
3798 
3799 		sctp_addto_chunk(retval, size, &addstrm);
3800 	}
3801 
3802 	return retval;
3803 }
3804 
3805 /* RE-CONFIG 4.4 (RESP)
3806  *   0                   1                   2                   3
3807  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3808  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3809  *  |     Parameter Type = 16       |      Parameter Length         |
3810  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3811  *  |         Re-configuration Response Sequence Number             |
3812  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3813  *  |                            Result                             |
3814  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3815  */
3816 struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
3817 					   __u32 result, __u32 sn)
3818 {
3819 	struct sctp_strreset_resp resp;
3820 	__u16 length = sizeof(resp);
3821 	struct sctp_chunk *retval;
3822 
3823 	retval = sctp_make_reconf(asoc, length);
3824 	if (!retval)
3825 		return NULL;
3826 
3827 	resp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3828 	resp.param_hdr.length = htons(length);
3829 	resp.response_seq = htonl(sn);
3830 	resp.result = htonl(result);
3831 
3832 	sctp_addto_chunk(retval, sizeof(resp), &resp);
3833 
3834 	return retval;
3835 }
3836 
3837 /* RE-CONFIG 4.4 OPTIONAL (TSNRESP)
3838  *   0                   1                   2                   3
3839  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
3840  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3841  *  |     Parameter Type = 16       |      Parameter Length         |
3842  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3843  *  |         Re-configuration Response Sequence Number             |
3844  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3845  *  |                            Result                             |
3846  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3847  *  |                   Sender's Next TSN (optional)                |
3848  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3849  *  |                  Receiver's Next TSN (optional)               |
3850  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3851  */
3852 struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
3853 					      __u32 result, __u32 sn,
3854 					      __u32 sender_tsn,
3855 					      __u32 receiver_tsn)
3856 {
3857 	struct sctp_strreset_resptsn tsnresp;
3858 	__u16 length = sizeof(tsnresp);
3859 	struct sctp_chunk *retval;
3860 
3861 	retval = sctp_make_reconf(asoc, length);
3862 	if (!retval)
3863 		return NULL;
3864 
3865 	tsnresp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3866 	tsnresp.param_hdr.length = htons(length);
3867 
3868 	tsnresp.response_seq = htonl(sn);
3869 	tsnresp.result = htonl(result);
3870 	tsnresp.senders_next_tsn = htonl(sender_tsn);
3871 	tsnresp.receivers_next_tsn = htonl(receiver_tsn);
3872 
3873 	sctp_addto_chunk(retval, sizeof(tsnresp), &tsnresp);
3874 
3875 	return retval;
3876 }
3877 
3878 bool sctp_verify_reconf(const struct sctp_association *asoc,
3879 			struct sctp_chunk *chunk,
3880 			struct sctp_paramhdr **errp)
3881 {
3882 	struct sctp_reconf_chunk *hdr;
3883 	union sctp_params param;
3884 	__be16 last = 0;
3885 	__u16 cnt = 0;
3886 
3887 	hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3888 	sctp_walk_params(param, hdr) {
3889 		__u16 length = ntohs(param.p->length);
3890 
3891 		*errp = param.p;
3892 		if (cnt++ > 2)
3893 			return false;
3894 		switch (param.p->type) {
3895 		case SCTP_PARAM_RESET_OUT_REQUEST:
3896 			if (length < sizeof(struct sctp_strreset_outreq) ||
3897 			    (last && last != SCTP_PARAM_RESET_RESPONSE &&
3898 			     last != SCTP_PARAM_RESET_IN_REQUEST))
3899 				return false;
3900 			break;
3901 		case SCTP_PARAM_RESET_IN_REQUEST:
3902 			if (length < sizeof(struct sctp_strreset_inreq) ||
3903 			    (last && last != SCTP_PARAM_RESET_OUT_REQUEST))
3904 				return false;
3905 			break;
3906 		case SCTP_PARAM_RESET_RESPONSE:
3907 			if ((length != sizeof(struct sctp_strreset_resp) &&
3908 			     length != sizeof(struct sctp_strreset_resptsn)) ||
3909 			    (last && last != SCTP_PARAM_RESET_RESPONSE &&
3910 			     last != SCTP_PARAM_RESET_OUT_REQUEST))
3911 				return false;
3912 			break;
3913 		case SCTP_PARAM_RESET_TSN_REQUEST:
3914 			if (length !=
3915 			    sizeof(struct sctp_strreset_tsnreq) || last)
3916 				return false;
3917 			break;
3918 		case SCTP_PARAM_RESET_ADD_IN_STREAMS:
3919 			if (length != sizeof(struct sctp_strreset_addstrm) ||
3920 			    (last && last != SCTP_PARAM_RESET_ADD_OUT_STREAMS))
3921 				return false;
3922 			break;
3923 		case SCTP_PARAM_RESET_ADD_OUT_STREAMS:
3924 			if (length != sizeof(struct sctp_strreset_addstrm) ||
3925 			    (last && last != SCTP_PARAM_RESET_ADD_IN_STREAMS))
3926 				return false;
3927 			break;
3928 		default:
3929 			return false;
3930 		}
3931 
3932 		last = param.p->type;
3933 	}
3934 
3935 	return true;
3936 }
3937