xref: /linux/net/sctp/sm_make_chunk.c (revision 52ffeaa358d1f3d91aa87a26ad90d77769cff74b)
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 	enum sctp_scope scope;
1737 	ktime_t kt;
1738 
1739 	/* Header size is static data prior to the actual cookie, including
1740 	 * any padding.
1741 	 */
1742 	headersize = sizeof(struct sctp_chunkhdr) +
1743 		     (sizeof(struct sctp_signed_cookie) -
1744 		      sizeof(struct sctp_cookie));
1745 	bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1746 	fixed_size = headersize + sizeof(struct sctp_cookie);
1747 
1748 	/* Verify that the chunk looks like it even has a cookie.
1749 	 * There must be enough room for our cookie and our peer's
1750 	 * INIT chunk.
1751 	 */
1752 	len = ntohs(chunk->chunk_hdr->length);
1753 	if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1754 		goto malformed;
1755 
1756 	/* Verify that the cookie has been padded out. */
1757 	if (bodysize % SCTP_COOKIE_MULTIPLE)
1758 		goto malformed;
1759 
1760 	/* Process the cookie.  */
1761 	cookie = chunk->subh.cookie_hdr;
1762 	bear_cookie = &cookie->c;
1763 
1764 	ch = (struct sctp_chunkhdr *)(bear_cookie + 1);
1765 	if (ch->type != SCTP_CID_INIT)
1766 		goto malformed;
1767 	chlen = ntohs(ch->length);
1768 	if (chlen < sizeof(struct sctp_init_chunk))
1769 		goto malformed;
1770 	if (chlen > len - fixed_size)
1771 		goto malformed;
1772 	if (bear_cookie->raw_addr_list_len > len - fixed_size - chlen)
1773 		goto malformed;
1774 
1775 	/* Verify the cookie's MAC, if cookie authentication is enabled. */
1776 	if (sctp_sk(ep->base.sk)->cookie_auth_enable) {
1777 		u8 mac[SHA256_DIGEST_SIZE];
1778 
1779 		hmac_sha256(&ep->cookie_auth_key, (const u8 *)bear_cookie,
1780 			    bodysize, mac);
1781 		static_assert(sizeof(cookie->mac) == sizeof(mac));
1782 		if (crypto_memneq(mac, cookie->mac, sizeof(mac))) {
1783 			*error = -SCTP_IERROR_BAD_SIG;
1784 			goto fail;
1785 		}
1786 	}
1787 
1788 	/* IG Section 2.35.2:
1789 	 *  3) Compare the port numbers and the verification tag contained
1790 	 *     within the COOKIE ECHO chunk to the actual port numbers and the
1791 	 *     verification tag within the SCTP common header of the received
1792 	 *     packet. If these values do not match the packet MUST be silently
1793 	 *     discarded,
1794 	 */
1795 	if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1796 		*error = -SCTP_IERROR_BAD_TAG;
1797 		goto fail;
1798 	}
1799 
1800 	if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1801 	    ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1802 		*error = -SCTP_IERROR_BAD_PORTS;
1803 		goto fail;
1804 	}
1805 
1806 	/* Check to see if the cookie is stale.  RFC 9260 Section 5.2.4
1807 	 * exempts an expired cookie only when both Verification Tags match
1808 	 * the current association.
1809 	 * If skb has been timestamped, then use the stamp, otherwise
1810 	 * use current time.  This introduces a small possibility that
1811 	 * a cookie may be considered expired, but this would only slow
1812 	 * down the new association establishment instead of every packet.
1813 	 */
1814 	if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1815 		kt = skb_get_ktime(skb);
1816 	else
1817 		kt = ktime_get_real();
1818 
1819 	if ((!asoc ||
1820 	     asoc->c.my_vtag != bear_cookie->my_vtag ||
1821 	     asoc->c.peer_vtag != bear_cookie->peer_vtag) &&
1822 	    ktime_before(bear_cookie->expiration, kt)) {
1823 		suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
1824 		__be32 n = htonl(usecs);
1825 
1826 		/*
1827 		 * Section 3.3.10.3 Stale Cookie Error (3)
1828 		 *
1829 		 * Cause of error
1830 		 * ---------------
1831 		 * Stale Cookie Error:  Indicates the receipt of a valid State
1832 		 * Cookie that has expired.
1833 		 */
1834 		*errp = sctp_make_op_error(asoc, chunk,
1835 					   SCTP_ERROR_STALE_COOKIE, &n,
1836 					   sizeof(n), 0);
1837 		if (*errp)
1838 			*error = -SCTP_IERROR_STALE_COOKIE;
1839 		else
1840 			*error = -SCTP_IERROR_NOMEM;
1841 
1842 		goto fail;
1843 	}
1844 
1845 	/* Make a new base association.  */
1846 	scope = sctp_scope(sctp_source(chunk));
1847 	retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1848 	if (!retval) {
1849 		*error = -SCTP_IERROR_NOMEM;
1850 		goto fail;
1851 	}
1852 
1853 	/* Set up our peer's port number.  */
1854 	retval->peer.port = ntohs(chunk->sctp_hdr->source);
1855 
1856 	if (!sctp_auth_verify_cookie_params(ep, bear_cookie))
1857 		goto malformed;
1858 
1859 	/* Populate the association from the cookie.  */
1860 	memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1861 
1862 	if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1863 						 GFP_ATOMIC) < 0) {
1864 		*error = -SCTP_IERROR_NOMEM;
1865 		goto fail;
1866 	}
1867 
1868 	/* Also, add the destination address. */
1869 	if (list_empty(&retval->base.bind_addr.address_list)) {
1870 		sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1871 				   sizeof(chunk->dest), SCTP_ADDR_SRC,
1872 				   GFP_ATOMIC);
1873 	}
1874 
1875 	retval->next_tsn = retval->c.initial_tsn;
1876 	retval->ctsn_ack_point = retval->next_tsn - 1;
1877 	retval->addip_serial = retval->c.initial_tsn;
1878 	retval->strreset_outseq = retval->c.initial_tsn;
1879 	retval->adv_peer_ack_point = retval->ctsn_ack_point;
1880 	retval->peer.prsctp_capable = retval->c.prsctp_capable;
1881 	retval->peer.adaptation_ind = retval->c.adaptation_ind;
1882 
1883 	/* The INIT stuff will be done by the side effects.  */
1884 	return retval;
1885 
1886 fail:
1887 	if (retval)
1888 		sctp_association_free(retval);
1889 
1890 	return NULL;
1891 
1892 malformed:
1893 	/* Yikes!  The packet is either corrupt or deliberately
1894 	 * malformed.
1895 	 */
1896 	*error = -SCTP_IERROR_MALFORMED;
1897 	goto fail;
1898 }
1899 
1900 /********************************************************************
1901  * 3rd Level Abstractions
1902  ********************************************************************/
1903 
1904 struct __sctp_missing {
1905 	__be32 num_missing;
1906 	__be16 type;
1907 }  __packed;
1908 
1909 /*
1910  * Report a missing mandatory parameter.
1911  */
1912 static int sctp_process_missing_param(const struct sctp_association *asoc,
1913 				      enum sctp_param paramtype,
1914 				      struct sctp_chunk *chunk,
1915 				      struct sctp_chunk **errp)
1916 {
1917 	struct __sctp_missing report;
1918 	__u16 len;
1919 
1920 	len = SCTP_PAD4(sizeof(report));
1921 
1922 	/* Make an ERROR chunk, preparing enough room for
1923 	 * returning multiple unknown parameters.
1924 	 */
1925 	if (!*errp)
1926 		*errp = sctp_make_op_error_space(asoc, chunk, len);
1927 
1928 	if (*errp) {
1929 		report.num_missing = htonl(1);
1930 		report.type = paramtype;
1931 		sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1932 				sizeof(report));
1933 		sctp_addto_chunk(*errp, sizeof(report), &report);
1934 	}
1935 
1936 	/* Stop processing this chunk. */
1937 	return 0;
1938 }
1939 
1940 /* Report an Invalid Mandatory Parameter.  */
1941 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1942 				      struct sctp_chunk *chunk,
1943 				      struct sctp_chunk **errp)
1944 {
1945 	/* Invalid Mandatory Parameter Error has no payload. */
1946 
1947 	if (!*errp)
1948 		*errp = sctp_make_op_error_space(asoc, chunk, 0);
1949 
1950 	if (*errp)
1951 		sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1952 
1953 	/* Stop processing this chunk. */
1954 	return 0;
1955 }
1956 
1957 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1958 					struct sctp_paramhdr *param,
1959 					const struct sctp_chunk *chunk,
1960 					struct sctp_chunk **errp)
1961 {
1962 	/* This is a fatal error.  Any accumulated non-fatal errors are
1963 	 * not reported.
1964 	 */
1965 	if (*errp)
1966 		sctp_chunk_free(*errp);
1967 
1968 	/* Create an error chunk and fill it in with our payload. */
1969 	*errp = sctp_make_violation_paramlen(asoc, chunk, param);
1970 
1971 	return 0;
1972 }
1973 
1974 
1975 /* Do not attempt to handle the HOST_NAME parm.  However, do
1976  * send back an indicator to the peer.
1977  */
1978 static int sctp_process_hn_param(const struct sctp_association *asoc,
1979 				 union sctp_params param,
1980 				 struct sctp_chunk *chunk,
1981 				 struct sctp_chunk **errp)
1982 {
1983 	__u16 len = ntohs(param.p->length);
1984 
1985 	/* Processing of the HOST_NAME parameter will generate an
1986 	 * ABORT.  If we've accumulated any non-fatal errors, they
1987 	 * would be unrecognized parameters and we should not include
1988 	 * them in the ABORT.
1989 	 */
1990 	if (*errp)
1991 		sctp_chunk_free(*errp);
1992 
1993 	*errp = sctp_make_op_error(asoc, chunk, SCTP_ERROR_DNS_FAILED,
1994 				   param.v, len, 0);
1995 
1996 	/* Stop processing this chunk. */
1997 	return 0;
1998 }
1999 
2000 static int sctp_verify_ext_param(struct net *net,
2001 				 const struct sctp_endpoint *ep,
2002 				 union sctp_params param)
2003 {
2004 	__u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2005 	int have_asconf = 0;
2006 	int have_auth = 0;
2007 	int i;
2008 
2009 	for (i = 0; i < num_ext; i++) {
2010 		switch (param.ext->chunks[i]) {
2011 		case SCTP_CID_AUTH:
2012 			have_auth = 1;
2013 			break;
2014 		case SCTP_CID_ASCONF:
2015 		case SCTP_CID_ASCONF_ACK:
2016 			have_asconf = 1;
2017 			break;
2018 		}
2019 	}
2020 
2021 	/* ADD-IP Security: The draft requires us to ABORT or ignore the
2022 	 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not.  Do this
2023 	 * only if ADD-IP is turned on and we are not backward-compatible
2024 	 * mode.
2025 	 */
2026 	if (net->sctp.addip_noauth)
2027 		return 1;
2028 
2029 	if (ep->asconf_enable && !have_auth && have_asconf)
2030 		return 0;
2031 
2032 	return 1;
2033 }
2034 
2035 static void sctp_process_ext_param(struct sctp_association *asoc,
2036 				   union sctp_params param)
2037 {
2038 	__u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2039 	int i;
2040 
2041 	for (i = 0; i < num_ext; i++) {
2042 		switch (param.ext->chunks[i]) {
2043 		case SCTP_CID_RECONF:
2044 			if (asoc->ep->reconf_enable)
2045 				asoc->peer.reconf_capable = 1;
2046 			break;
2047 		case SCTP_CID_FWD_TSN:
2048 			if (asoc->ep->prsctp_enable)
2049 				asoc->peer.prsctp_capable = 1;
2050 			break;
2051 		case SCTP_CID_AUTH:
2052 			/* if the peer reports AUTH, assume that he
2053 			 * supports AUTH.
2054 			 */
2055 			if (asoc->ep->auth_enable)
2056 				asoc->peer.auth_capable = 1;
2057 			break;
2058 		case SCTP_CID_ASCONF:
2059 		case SCTP_CID_ASCONF_ACK:
2060 			if (asoc->ep->asconf_enable)
2061 				asoc->peer.asconf_capable = 1;
2062 			break;
2063 		case SCTP_CID_I_DATA:
2064 			if (asoc->ep->intl_enable)
2065 				asoc->peer.intl_capable = 1;
2066 			break;
2067 		default:
2068 			break;
2069 		}
2070 	}
2071 }
2072 
2073 /* RFC 3.2.1 & the Implementers Guide 2.2.
2074  *
2075  * The Parameter Types are encoded such that the
2076  * highest-order two bits specify the action that must be
2077  * taken if the processing endpoint does not recognize the
2078  * Parameter Type.
2079  *
2080  * 00 - Stop processing this parameter; do not process any further
2081  * 	parameters within this chunk
2082  *
2083  * 01 - Stop processing this parameter, do not process any further
2084  *	parameters within this chunk, and report the unrecognized
2085  *	parameter in an 'Unrecognized Parameter' ERROR chunk.
2086  *
2087  * 10 - Skip this parameter and continue processing.
2088  *
2089  * 11 - Skip this parameter and continue processing but
2090  *	report the unrecognized parameter in an
2091  *	'Unrecognized Parameter' ERROR chunk.
2092  *
2093  * Return value:
2094  * 	SCTP_IERROR_NO_ERROR - continue with the chunk
2095  * 	SCTP_IERROR_ERROR    - stop and report an error.
2096  * 	SCTP_IERROR_NOMEME   - out of memory.
2097  */
2098 static enum sctp_ierror sctp_process_unk_param(
2099 					const struct sctp_association *asoc,
2100 					union sctp_params param,
2101 					struct sctp_chunk *chunk,
2102 					struct sctp_chunk **errp)
2103 {
2104 	int retval = SCTP_IERROR_NO_ERROR;
2105 
2106 	switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2107 	case SCTP_PARAM_ACTION_DISCARD:
2108 		retval =  SCTP_IERROR_ERROR;
2109 		break;
2110 	case SCTP_PARAM_ACTION_SKIP:
2111 		break;
2112 	case SCTP_PARAM_ACTION_DISCARD_ERR:
2113 		retval =  SCTP_IERROR_ERROR;
2114 		fallthrough;
2115 	case SCTP_PARAM_ACTION_SKIP_ERR:
2116 		/* Make an ERROR chunk, preparing enough room for
2117 		 * returning multiple unknown parameters.
2118 		 */
2119 		if (!*errp) {
2120 			*errp = sctp_make_op_error_limited(asoc, chunk);
2121 			if (!*errp) {
2122 				/* If there is no memory for generating the
2123 				 * ERROR report as specified, an ABORT will be
2124 				 * triggered to the peer and the association
2125 				 * won't be established.
2126 				 */
2127 				retval = SCTP_IERROR_NOMEM;
2128 				break;
2129 			}
2130 		}
2131 
2132 		if (!sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2133 				     ntohs(param.p->length)))
2134 			sctp_addto_chunk(*errp, ntohs(param.p->length),
2135 					 param.v);
2136 		break;
2137 	default:
2138 		break;
2139 	}
2140 
2141 	return retval;
2142 }
2143 
2144 /* Verify variable length parameters
2145  * Return values:
2146  * 	SCTP_IERROR_ABORT - trigger an ABORT
2147  * 	SCTP_IERROR_NOMEM - out of memory (abort)
2148  *	SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2149  * 	SCTP_IERROR_NO_ERROR - continue with the chunk
2150  */
2151 static enum sctp_ierror sctp_verify_param(struct net *net,
2152 					  const struct sctp_endpoint *ep,
2153 					  const struct sctp_association *asoc,
2154 					  union sctp_params param,
2155 					  enum sctp_cid cid,
2156 					  struct sctp_chunk *chunk,
2157 					  struct sctp_chunk **err_chunk)
2158 {
2159 	struct sctp_hmac_algo_param *hmacs;
2160 	int retval = SCTP_IERROR_NO_ERROR;
2161 	__u16 n_elt, id = 0;
2162 	int i;
2163 
2164 	/* FIXME - This routine is not looking at each parameter per the
2165 	 * chunk type, i.e., unrecognized parameters should be further
2166 	 * identified based on the chunk id.
2167 	 */
2168 
2169 	switch (param.p->type) {
2170 	case SCTP_PARAM_IPV4_ADDRESS:
2171 	case SCTP_PARAM_IPV6_ADDRESS:
2172 	case SCTP_PARAM_COOKIE_PRESERVATIVE:
2173 	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2174 	case SCTP_PARAM_STATE_COOKIE:
2175 	case SCTP_PARAM_HEARTBEAT_INFO:
2176 	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2177 	case SCTP_PARAM_ECN_CAPABLE:
2178 		break;
2179 	case SCTP_PARAM_ADAPTATION_LAYER_IND:
2180 		if (ntohs(param.p->length) != sizeof(*param.aind)) {
2181 			sctp_process_inv_paramlength(asoc, param.p,
2182 						     chunk, err_chunk);
2183 			retval = SCTP_IERROR_ABORT;
2184 		}
2185 		break;
2186 
2187 	case SCTP_PARAM_SUPPORTED_EXT:
2188 		if (!sctp_verify_ext_param(net, ep, param))
2189 			return SCTP_IERROR_ABORT;
2190 		break;
2191 
2192 	case SCTP_PARAM_SET_PRIMARY:
2193 		if (!ep->asconf_enable)
2194 			goto unhandled;
2195 
2196 		if (ntohs(param.p->length) < sizeof(struct sctp_addip_param) +
2197 					     sizeof(struct sctp_paramhdr)) {
2198 			sctp_process_inv_paramlength(asoc, param.p,
2199 						     chunk, err_chunk);
2200 			retval = SCTP_IERROR_ABORT;
2201 		}
2202 		break;
2203 
2204 	case SCTP_PARAM_HOST_NAME_ADDRESS:
2205 		/* This param has been Deprecated, send ABORT.  */
2206 		sctp_process_hn_param(asoc, param, chunk, err_chunk);
2207 		retval = SCTP_IERROR_ABORT;
2208 		break;
2209 
2210 	case SCTP_PARAM_FWD_TSN_SUPPORT:
2211 		if (ep->prsctp_enable)
2212 			break;
2213 		goto unhandled;
2214 
2215 	case SCTP_PARAM_RANDOM:
2216 		if (!ep->auth_enable)
2217 			goto unhandled;
2218 
2219 		/* SCTP-AUTH: Secion 6.1
2220 		 * If the random number is not 32 byte long the association
2221 		 * MUST be aborted.  The ABORT chunk SHOULD contain the error
2222 		 * cause 'Protocol Violation'.
2223 		 */
2224 		if (SCTP_AUTH_RANDOM_LENGTH != ntohs(param.p->length) -
2225 					       sizeof(struct sctp_paramhdr)) {
2226 			sctp_process_inv_paramlength(asoc, param.p,
2227 						     chunk, err_chunk);
2228 			retval = SCTP_IERROR_ABORT;
2229 		}
2230 		break;
2231 
2232 	case SCTP_PARAM_CHUNKS:
2233 		if (!ep->auth_enable)
2234 			goto unhandled;
2235 
2236 		/* SCTP-AUTH: Section 3.2
2237 		 * The CHUNKS parameter MUST be included once in the INIT or
2238 		 *  INIT-ACK chunk if the sender wants to receive authenticated
2239 		 *  chunks.  Its maximum length is 260 bytes.
2240 		 */
2241 		if (260 < ntohs(param.p->length)) {
2242 			sctp_process_inv_paramlength(asoc, param.p,
2243 						     chunk, err_chunk);
2244 			retval = SCTP_IERROR_ABORT;
2245 		}
2246 		break;
2247 
2248 	case SCTP_PARAM_HMAC_ALGO:
2249 		if (!ep->auth_enable)
2250 			goto unhandled;
2251 
2252 		hmacs = (struct sctp_hmac_algo_param *)param.p;
2253 		n_elt = (ntohs(param.p->length) -
2254 			 sizeof(struct sctp_paramhdr)) >> 1;
2255 
2256 		/* SCTP-AUTH: Section 6.1
2257 		 * The HMAC algorithm based on SHA-1 MUST be supported and
2258 		 * included in the HMAC-ALGO parameter.
2259 		 */
2260 		for (i = 0; i < n_elt; i++) {
2261 			id = ntohs(hmacs->hmac_ids[i]);
2262 
2263 			if (id == SCTP_AUTH_HMAC_ID_SHA1)
2264 				break;
2265 		}
2266 
2267 		if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2268 			sctp_process_inv_paramlength(asoc, param.p, chunk,
2269 						     err_chunk);
2270 			retval = SCTP_IERROR_ABORT;
2271 		}
2272 		break;
2273 unhandled:
2274 	default:
2275 		pr_debug("%s: unrecognized param:%d for chunk:%d\n",
2276 			 __func__, ntohs(param.p->type), cid);
2277 
2278 		retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2279 		break;
2280 	}
2281 	return retval;
2282 }
2283 
2284 /* Verify the INIT packet before we process it.  */
2285 int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
2286 		     const struct sctp_association *asoc, enum sctp_cid cid,
2287 		     struct sctp_init_chunk *peer_init,
2288 		     struct sctp_chunk *chunk, struct sctp_chunk **errp)
2289 {
2290 	union sctp_params param;
2291 	bool has_cookie = false;
2292 	int result;
2293 
2294 	/* Check for missing mandatory parameters. Note: Initial TSN is
2295 	 * also mandatory, but is not checked here since the valid range
2296 	 * is 0..2**32-1. RFC4960, section 3.3.3.
2297 	 */
2298 	if (peer_init->init_hdr.num_outbound_streams == 0 ||
2299 	    peer_init->init_hdr.num_inbound_streams == 0 ||
2300 	    peer_init->init_hdr.init_tag == 0 ||
2301 	    ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
2302 		return sctp_process_inv_mandatory(asoc, chunk, errp);
2303 
2304 	sctp_walk_params(param, peer_init) {
2305 		if (param.p->type == SCTP_PARAM_STATE_COOKIE)
2306 			has_cookie = true;
2307 	}
2308 
2309 	/* There is a possibility that a parameter length was bad and
2310 	 * in that case we would have stoped walking the parameters.
2311 	 * The current param.p would point at the bad one.
2312 	 * Current consensus on the mailing list is to generate a PROTOCOL
2313 	 * VIOLATION error.  We build the ERROR chunk here and let the normal
2314 	 * error handling code build and send the packet.
2315 	 */
2316 	if (param.v != (void *)peer_init +
2317 		       SCTP_PAD4(ntohs(peer_init->chunk_hdr.length)))
2318 		return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2319 
2320 	/* The only missing mandatory param possible today is
2321 	 * the state cookie for an INIT-ACK chunk.
2322 	 */
2323 	if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2324 		return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2325 						  chunk, errp);
2326 
2327 	/* Verify all the variable length parameters */
2328 	sctp_walk_params(param, peer_init) {
2329 		result = sctp_verify_param(net, ep, asoc, param, cid,
2330 					   chunk, errp);
2331 		switch (result) {
2332 		case SCTP_IERROR_ABORT:
2333 		case SCTP_IERROR_NOMEM:
2334 			return 0;
2335 		case SCTP_IERROR_ERROR:
2336 			return 1;
2337 		case SCTP_IERROR_NO_ERROR:
2338 		default:
2339 			break;
2340 		}
2341 
2342 	} /* for (loop through all parameters) */
2343 
2344 	return 1;
2345 }
2346 
2347 /* Unpack the parameters in an INIT packet into an association.
2348  * Returns 0 on failure, else success.
2349  * FIXME:  This is an association method.
2350  */
2351 int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
2352 		      const union sctp_addr *peer_addr,
2353 		      struct sctp_init_chunk *peer_init, gfp_t gfp)
2354 {
2355 	struct sctp_transport *transport;
2356 	struct list_head *pos, *temp;
2357 	union sctp_params param;
2358 	union sctp_addr addr;
2359 	struct sctp_af *af;
2360 	int src_match = 0;
2361 
2362 	/* We must include the address that the INIT packet came from.
2363 	 * This is the only address that matters for an INIT packet.
2364 	 * When processing a COOKIE ECHO, we retrieve the from address
2365 	 * of the INIT from the cookie.
2366 	 */
2367 
2368 	/* This implementation defaults to making the first transport
2369 	 * added as the primary transport.  The source address seems to
2370 	 * be a better choice than any of the embedded addresses.
2371 	 */
2372 	asoc->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
2373 	if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2374 		goto nomem;
2375 
2376 	if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
2377 		src_match = 1;
2378 
2379 	/* Process the initialization parameters.  */
2380 	sctp_walk_params(param, peer_init) {
2381 		if (!src_match &&
2382 		    (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2383 		     param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2384 			af = sctp_get_af_specific(param_type2af(param.p->type));
2385 			if (!af->from_addr_param(&addr, param.addr,
2386 						 chunk->sctp_hdr->source, 0))
2387 				continue;
2388 			if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
2389 				src_match = 1;
2390 		}
2391 
2392 		if (!sctp_process_param(asoc, param, peer_addr, gfp))
2393 			goto clean_up;
2394 	}
2395 
2396 	/* source address of chunk may not match any valid address */
2397 	if (!src_match)
2398 		goto clean_up;
2399 
2400 	/* AUTH: After processing the parameters, make sure that we
2401 	 * have all the required info to potentially do authentications.
2402 	 */
2403 	if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2404 					!asoc->peer.peer_hmacs))
2405 		asoc->peer.auth_capable = 0;
2406 
2407 	/* In a non-backward compatible mode, if the peer claims
2408 	 * support for ADD-IP but not AUTH,  the ADD-IP spec states
2409 	 * that we MUST ABORT the association. Section 6.  The section
2410 	 * also give us an option to silently ignore the packet, which
2411 	 * is what we'll do here.
2412 	 */
2413 	if (!asoc->base.net->sctp.addip_noauth &&
2414 	    (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2415 		asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2416 						  SCTP_PARAM_DEL_IP |
2417 						  SCTP_PARAM_SET_PRIMARY);
2418 		asoc->peer.asconf_capable = 0;
2419 		goto clean_up;
2420 	}
2421 
2422 	/* Walk list of transports, removing transports in the UNKNOWN state. */
2423 	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2424 		transport = list_entry(pos, struct sctp_transport, transports);
2425 		if (transport->state == SCTP_UNKNOWN) {
2426 			sctp_assoc_rm_peer(asoc, transport);
2427 		}
2428 	}
2429 
2430 	/* The fixed INIT headers are always in network byte
2431 	 * order.
2432 	 */
2433 	asoc->peer.i.init_tag =
2434 		ntohl(peer_init->init_hdr.init_tag);
2435 	asoc->peer.i.a_rwnd =
2436 		ntohl(peer_init->init_hdr.a_rwnd);
2437 	asoc->peer.i.num_outbound_streams =
2438 		ntohs(peer_init->init_hdr.num_outbound_streams);
2439 	asoc->peer.i.num_inbound_streams =
2440 		ntohs(peer_init->init_hdr.num_inbound_streams);
2441 	asoc->peer.i.initial_tsn =
2442 		ntohl(peer_init->init_hdr.initial_tsn);
2443 
2444 	asoc->strreset_inseq = asoc->peer.i.initial_tsn;
2445 
2446 	/* Apply the upper bounds for output streams based on peer's
2447 	 * number of inbound streams.
2448 	 */
2449 	if (asoc->c.sinit_num_ostreams  >
2450 	    ntohs(peer_init->init_hdr.num_inbound_streams)) {
2451 		asoc->c.sinit_num_ostreams =
2452 			ntohs(peer_init->init_hdr.num_inbound_streams);
2453 	}
2454 
2455 	if (asoc->c.sinit_max_instreams >
2456 	    ntohs(peer_init->init_hdr.num_outbound_streams)) {
2457 		asoc->c.sinit_max_instreams =
2458 			ntohs(peer_init->init_hdr.num_outbound_streams);
2459 	}
2460 
2461 	/* Copy Initiation tag from INIT to VT_peer in cookie.   */
2462 	asoc->c.peer_vtag = asoc->peer.i.init_tag;
2463 
2464 	/* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
2465 	asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2466 
2467 	/* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2468 	 * high (for example, implementations MAY use the size of the receiver
2469 	 * advertised window).
2470 	 */
2471 	list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2472 			transports) {
2473 		transport->ssthresh = asoc->peer.i.a_rwnd;
2474 	}
2475 
2476 	/* Set up the TSN tracking pieces.  */
2477 	if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2478 				asoc->peer.i.initial_tsn, gfp))
2479 		goto clean_up;
2480 
2481 	/* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2482 	 *
2483 	 * The stream sequence number in all the streams shall start
2484 	 * from 0 when the association is established.  Also, when the
2485 	 * stream sequence number reaches the value 65535 the next
2486 	 * stream sequence number shall be set to 0.
2487 	 */
2488 
2489 	if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams,
2490 			     asoc->c.sinit_max_instreams, gfp))
2491 		goto clean_up;
2492 
2493 	/* Update frag_point when stream_interleave may get changed. */
2494 	sctp_assoc_update_frag_point(asoc);
2495 
2496 	if (!asoc->temp && sctp_assoc_set_id(asoc, gfp))
2497 		goto clean_up;
2498 
2499 	/* ADDIP Section 4.1 ASCONF Chunk Procedures
2500 	 *
2501 	 * When an endpoint has an ASCONF signaled change to be sent to the
2502 	 * remote endpoint it should do the following:
2503 	 * ...
2504 	 * A2) A serial number should be assigned to the Chunk. The serial
2505 	 * number should be a monotonically increasing number. All serial
2506 	 * numbers are defined to be initialized at the start of the
2507 	 * association to the same value as the Initial TSN.
2508 	 */
2509 	asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2510 	return 1;
2511 
2512 clean_up:
2513 	/* Release the transport structures. */
2514 	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2515 		transport = list_entry(pos, struct sctp_transport, transports);
2516 		if (transport->state != SCTP_ACTIVE)
2517 			sctp_assoc_rm_peer(asoc, transport);
2518 	}
2519 
2520 nomem:
2521 	return 0;
2522 }
2523 
2524 
2525 /* Update asoc with the option described in param.
2526  *
2527  * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2528  *
2529  * asoc is the association to update.
2530  * param is the variable length parameter to use for update.
2531  * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2532  * If the current packet is an INIT we want to minimize the amount of
2533  * work we do.  In particular, we should not build transport
2534  * structures for the addresses.
2535  */
2536 static int sctp_process_param(struct sctp_association *asoc,
2537 			      union sctp_params param,
2538 			      const union sctp_addr *peer_addr,
2539 			      gfp_t gfp)
2540 {
2541 	struct sctp_endpoint *ep = asoc->ep;
2542 	union sctp_addr_param *addr_param;
2543 	struct net *net = asoc->base.net;
2544 	struct sctp_transport *t;
2545 	enum sctp_scope scope;
2546 	union sctp_addr addr;
2547 	struct sctp_af *af;
2548 	int retval = 1, i;
2549 	u32 stale;
2550 	__u16 sat;
2551 
2552 	/* We maintain all INIT parameters in network byte order all the
2553 	 * time.  This allows us to not worry about whether the parameters
2554 	 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2555 	 */
2556 	switch (param.p->type) {
2557 	case SCTP_PARAM_IPV6_ADDRESS:
2558 		if (PF_INET6 != asoc->base.sk->sk_family)
2559 			break;
2560 		goto do_addr_param;
2561 
2562 	case SCTP_PARAM_IPV4_ADDRESS:
2563 		/* v4 addresses are not allowed on v6-only socket */
2564 		if (ipv6_only_sock(asoc->base.sk))
2565 			break;
2566 do_addr_param:
2567 		af = sctp_get_af_specific(param_type2af(param.p->type));
2568 		if (!af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0))
2569 			break;
2570 		scope = sctp_scope(peer_addr);
2571 		if (sctp_in_scope(net, &addr, scope))
2572 			if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2573 				return 0;
2574 		break;
2575 
2576 	case SCTP_PARAM_COOKIE_PRESERVATIVE:
2577 		if (!net->sctp.cookie_preserve_enable)
2578 			break;
2579 
2580 		stale = ntohl(param.life->lifespan_increment);
2581 
2582 		/* Suggested Cookie Life span increment's unit is msec,
2583 		 * (1/1000sec).
2584 		 */
2585 		asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
2586 		break;
2587 
2588 	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2589 		/* Turn off the default values first so we'll know which
2590 		 * ones are really set by the peer.
2591 		 */
2592 		asoc->peer.ipv4_address = 0;
2593 		asoc->peer.ipv6_address = 0;
2594 
2595 		/* Assume that peer supports the address family
2596 		 * by which it sends a packet.
2597 		 */
2598 		if (peer_addr->sa.sa_family == AF_INET6)
2599 			asoc->peer.ipv6_address = 1;
2600 		else if (peer_addr->sa.sa_family == AF_INET)
2601 			asoc->peer.ipv4_address = 1;
2602 
2603 		/* Cycle through address types; avoid divide by 0. */
2604 		sat = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2605 		if (sat)
2606 			sat /= sizeof(__u16);
2607 
2608 		for (i = 0; i < sat; ++i) {
2609 			switch (param.sat->types[i]) {
2610 			case SCTP_PARAM_IPV4_ADDRESS:
2611 				asoc->peer.ipv4_address = 1;
2612 				break;
2613 
2614 			case SCTP_PARAM_IPV6_ADDRESS:
2615 				if (PF_INET6 == asoc->base.sk->sk_family)
2616 					asoc->peer.ipv6_address = 1;
2617 				break;
2618 
2619 			default: /* Just ignore anything else.  */
2620 				break;
2621 			}
2622 		}
2623 		break;
2624 
2625 	case SCTP_PARAM_STATE_COOKIE:
2626 		asoc->peer.cookie_len =
2627 			ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2628 		kfree(asoc->peer.cookie);
2629 		asoc->peer.cookie = kmemdup(param.cookie->body, asoc->peer.cookie_len, gfp);
2630 		if (!asoc->peer.cookie)
2631 			retval = 0;
2632 		break;
2633 
2634 	case SCTP_PARAM_HEARTBEAT_INFO:
2635 		/* Would be odd to receive, but it causes no problems. */
2636 		break;
2637 
2638 	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2639 		/* Rejected during verify stage. */
2640 		break;
2641 
2642 	case SCTP_PARAM_ECN_CAPABLE:
2643 		if (asoc->ep->ecn_enable) {
2644 			asoc->peer.ecn_capable = 1;
2645 			break;
2646 		}
2647 		/* Fall Through */
2648 		goto fall_through;
2649 
2650 
2651 	case SCTP_PARAM_ADAPTATION_LAYER_IND:
2652 		asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
2653 		break;
2654 
2655 	case SCTP_PARAM_SET_PRIMARY:
2656 		if (!ep->asconf_enable)
2657 			goto fall_through;
2658 
2659 		addr_param = param.v + sizeof(struct sctp_addip_param);
2660 		if (ntohs(addr_param->p.length) >
2661 		    ntohs(param.p->length) - sizeof(struct sctp_addip_param))
2662 			break;
2663 
2664 		af = sctp_get_af_specific(param_type2af(addr_param->p.type));
2665 		if (!af)
2666 			break;
2667 
2668 		if (!af->from_addr_param(&addr, addr_param,
2669 					 htons(asoc->peer.port), 0))
2670 			break;
2671 
2672 		if (!af->addr_valid(&addr, NULL, NULL))
2673 			break;
2674 
2675 		t = sctp_assoc_lookup_paddr(asoc, &addr);
2676 		if (!t)
2677 			break;
2678 
2679 		sctp_assoc_set_primary(asoc, t);
2680 		break;
2681 
2682 	case SCTP_PARAM_SUPPORTED_EXT:
2683 		sctp_process_ext_param(asoc, param);
2684 		break;
2685 
2686 	case SCTP_PARAM_FWD_TSN_SUPPORT:
2687 		if (asoc->ep->prsctp_enable) {
2688 			asoc->peer.prsctp_capable = 1;
2689 			break;
2690 		}
2691 		/* Fall Through */
2692 		goto fall_through;
2693 
2694 	case SCTP_PARAM_RANDOM:
2695 		if (!ep->auth_enable)
2696 			goto fall_through;
2697 
2698 		/* Save peer's random parameter */
2699 		kfree(asoc->peer.peer_random);
2700 		asoc->peer.peer_random = kmemdup(param.p,
2701 					    ntohs(param.p->length), gfp);
2702 		if (!asoc->peer.peer_random) {
2703 			retval = 0;
2704 			break;
2705 		}
2706 		break;
2707 
2708 	case SCTP_PARAM_HMAC_ALGO:
2709 		if (!ep->auth_enable)
2710 			goto fall_through;
2711 
2712 		/* Save peer's HMAC list */
2713 		kfree(asoc->peer.peer_hmacs);
2714 		asoc->peer.peer_hmacs = kmemdup(param.p,
2715 					    ntohs(param.p->length), gfp);
2716 		if (!asoc->peer.peer_hmacs) {
2717 			retval = 0;
2718 			break;
2719 		}
2720 
2721 		/* Set the default HMAC the peer requested*/
2722 		sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2723 		break;
2724 
2725 	case SCTP_PARAM_CHUNKS:
2726 		if (!ep->auth_enable)
2727 			goto fall_through;
2728 
2729 		kfree(asoc->peer.peer_chunks);
2730 		asoc->peer.peer_chunks = kmemdup(param.p,
2731 					    ntohs(param.p->length), gfp);
2732 		if (!asoc->peer.peer_chunks)
2733 			retval = 0;
2734 		break;
2735 fall_through:
2736 	default:
2737 		/* Any unrecognized parameters should have been caught
2738 		 * and handled by sctp_verify_param() which should be
2739 		 * called prior to this routine.  Simply log the error
2740 		 * here.
2741 		 */
2742 		pr_debug("%s: ignoring param:%d for association:%p.\n",
2743 			 __func__, ntohs(param.p->type), asoc);
2744 		break;
2745 	}
2746 
2747 	return retval;
2748 }
2749 
2750 /* Select a new verification tag.  */
2751 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2752 {
2753 	/* I believe that this random number generator complies with RFC1750.
2754 	 * A tag of 0 is reserved for special cases (e.g. INIT).
2755 	 */
2756 	__u32 x;
2757 
2758 	do {
2759 		x = get_random_u32();
2760 	} while (x == 0);
2761 
2762 	return x;
2763 }
2764 
2765 /* Select an initial TSN to send during startup.  */
2766 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2767 {
2768 	__u32 retval;
2769 
2770 	retval = get_random_u32();
2771 	return retval;
2772 }
2773 
2774 /*
2775  * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2776  *      0                   1                   2                   3
2777  *      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
2778  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2779  *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
2780  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2781  *     |                       Serial Number                           |
2782  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2783  *     |                    Address Parameter                          |
2784  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2785  *     |                     ASCONF Parameter #1                       |
2786  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2787  *     \                                                               \
2788  *     /                             ....                              /
2789  *     \                                                               \
2790  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2791  *     |                     ASCONF Parameter #N                       |
2792  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2793  *
2794  * Address Parameter and other parameter will not be wrapped in this function
2795  */
2796 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2797 					   union sctp_addr *addr,
2798 					   int vparam_len)
2799 {
2800 	struct sctp_addiphdr asconf;
2801 	struct sctp_chunk *retval;
2802 	int length = sizeof(asconf) + vparam_len;
2803 	union sctp_addr_param addrparam;
2804 	int addrlen;
2805 	struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2806 
2807 	addrlen = af->to_addr_param(addr, &addrparam);
2808 	if (!addrlen)
2809 		return NULL;
2810 	length += addrlen;
2811 
2812 	/* Create the chunk.  */
2813 	retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
2814 				   GFP_ATOMIC);
2815 	if (!retval)
2816 		return NULL;
2817 
2818 	asconf.serial = htonl(asoc->addip_serial++);
2819 
2820 	retval->subh.addip_hdr =
2821 		sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2822 	retval->param_hdr.v =
2823 		sctp_addto_chunk(retval, addrlen, &addrparam);
2824 
2825 	return retval;
2826 }
2827 
2828 /* ADDIP
2829  * 3.2.1 Add IP Address
2830  * 	0                   1                   2                   3
2831  * 	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
2832  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2833  *     |        Type = 0xC001          |    Length = Variable          |
2834  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2835  *     |               ASCONF-Request Correlation ID                   |
2836  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2837  *     |                       Address Parameter                       |
2838  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2839  *
2840  * 3.2.2 Delete IP Address
2841  * 	0                   1                   2                   3
2842  * 	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
2843  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2844  *     |        Type = 0xC002          |    Length = Variable          |
2845  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2846  *     |               ASCONF-Request Correlation ID                   |
2847  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2848  *     |                       Address Parameter                       |
2849  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2850  *
2851  */
2852 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2853 					      union sctp_addr *laddr,
2854 					      struct sockaddr *addrs,
2855 					      int addrcnt, __be16 flags)
2856 {
2857 	union sctp_addr_param addr_param;
2858 	struct sctp_addip_param	param;
2859 	int paramlen = sizeof(param);
2860 	struct sctp_chunk *retval;
2861 	int addr_param_len = 0;
2862 	union sctp_addr *addr;
2863 	int totallen = 0, i;
2864 	int del_pickup = 0;
2865 	struct sctp_af *af;
2866 	void *addr_buf;
2867 
2868 	/* Get total length of all the address parameters. */
2869 	addr_buf = addrs;
2870 	for (i = 0; i < addrcnt; i++) {
2871 		addr = addr_buf;
2872 		af = sctp_get_af_specific(addr->v4.sin_family);
2873 		addr_param_len = af->to_addr_param(addr, &addr_param);
2874 
2875 		totallen += paramlen;
2876 		totallen += addr_param_len;
2877 
2878 		addr_buf += af->sockaddr_len;
2879 		if (asoc->asconf_addr_del_pending && !del_pickup) {
2880 			/* reuse the parameter length from the same scope one */
2881 			totallen += paramlen;
2882 			totallen += addr_param_len;
2883 			del_pickup = 1;
2884 
2885 			pr_debug("%s: picked same-scope del_pending addr, "
2886 				 "totallen for all addresses is %d\n",
2887 				 __func__, totallen);
2888 		}
2889 	}
2890 
2891 	/* Create an asconf chunk with the required length. */
2892 	retval = sctp_make_asconf(asoc, laddr, totallen);
2893 	if (!retval)
2894 		return NULL;
2895 
2896 	/* Add the address parameters to the asconf chunk. */
2897 	addr_buf = addrs;
2898 	for (i = 0; i < addrcnt; i++) {
2899 		addr = addr_buf;
2900 		af = sctp_get_af_specific(addr->v4.sin_family);
2901 		addr_param_len = af->to_addr_param(addr, &addr_param);
2902 		param.param_hdr.type = flags;
2903 		param.param_hdr.length = htons(paramlen + addr_param_len);
2904 		param.crr_id = htonl(i);
2905 
2906 		sctp_addto_chunk(retval, paramlen, &param);
2907 		sctp_addto_chunk(retval, addr_param_len, &addr_param);
2908 
2909 		addr_buf += af->sockaddr_len;
2910 	}
2911 	if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
2912 		addr = asoc->asconf_addr_del_pending;
2913 		af = sctp_get_af_specific(addr->v4.sin_family);
2914 		addr_param_len = af->to_addr_param(addr, &addr_param);
2915 		param.param_hdr.type = SCTP_PARAM_DEL_IP;
2916 		param.param_hdr.length = htons(paramlen + addr_param_len);
2917 		param.crr_id = htonl(i);
2918 
2919 		sctp_addto_chunk(retval, paramlen, &param);
2920 		sctp_addto_chunk(retval, addr_param_len, &addr_param);
2921 	}
2922 	return retval;
2923 }
2924 
2925 /* ADDIP
2926  * 3.2.4 Set Primary IP Address
2927  *	0                   1                   2                   3
2928  *	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
2929  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2930  *     |        Type =0xC004           |    Length = Variable          |
2931  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2932  *     |               ASCONF-Request Correlation ID                   |
2933  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2934  *     |                       Address Parameter                       |
2935  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2936  *
2937  * Create an ASCONF chunk with Set Primary IP address parameter.
2938  */
2939 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2940 					     union sctp_addr *addr)
2941 {
2942 	struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2943 	union sctp_addr_param addrparam;
2944 	struct sctp_addip_param	param;
2945 	struct sctp_chunk *retval;
2946 	int len = sizeof(param);
2947 	int addrlen;
2948 
2949 	addrlen = af->to_addr_param(addr, &addrparam);
2950 	if (!addrlen)
2951 		return NULL;
2952 	len += addrlen;
2953 
2954 	/* Create the chunk and make asconf header. */
2955 	retval = sctp_make_asconf(asoc, addr, len);
2956 	if (!retval)
2957 		return NULL;
2958 
2959 	param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2960 	param.param_hdr.length = htons(len);
2961 	param.crr_id = 0;
2962 
2963 	sctp_addto_chunk(retval, sizeof(param), &param);
2964 	sctp_addto_chunk(retval, addrlen, &addrparam);
2965 
2966 	return retval;
2967 }
2968 
2969 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2970  *      0                   1                   2                   3
2971  *      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
2972  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2973  *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
2974  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2975  *     |                       Serial Number                           |
2976  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2977  *     |                 ASCONF Parameter Response#1                   |
2978  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2979  *     \                                                               \
2980  *     /                             ....                              /
2981  *     \                                                               \
2982  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2983  *     |                 ASCONF Parameter Response#N                   |
2984  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2985  *
2986  * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2987  */
2988 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2989 					       __u32 serial, int vparam_len)
2990 {
2991 	struct sctp_addiphdr asconf;
2992 	struct sctp_chunk *retval;
2993 	int length = sizeof(asconf) + vparam_len;
2994 
2995 	/* Create the chunk.  */
2996 	retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
2997 				   GFP_ATOMIC);
2998 	if (!retval)
2999 		return NULL;
3000 
3001 	asconf.serial = htonl(serial);
3002 
3003 	retval->subh.addip_hdr =
3004 		sctp_addto_chunk(retval, sizeof(asconf), &asconf);
3005 
3006 	return retval;
3007 }
3008 
3009 /* Add response parameters to an ASCONF_ACK chunk. */
3010 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
3011 				     __be16 err_code,
3012 				     struct sctp_addip_param *asconf_param)
3013 {
3014 	struct sctp_addip_param ack_param;
3015 	struct sctp_errhdr err_param;
3016 	int asconf_param_len = 0;
3017 	int err_param_len = 0;
3018 	__be16 response_type;
3019 
3020 	if (SCTP_ERROR_NO_ERROR == err_code) {
3021 		response_type = SCTP_PARAM_SUCCESS_REPORT;
3022 	} else {
3023 		response_type = SCTP_PARAM_ERR_CAUSE;
3024 		err_param_len = sizeof(err_param);
3025 		if (asconf_param)
3026 			asconf_param_len =
3027 				 ntohs(asconf_param->param_hdr.length);
3028 	}
3029 
3030 	/* Add Success Indication or Error Cause Indication parameter. */
3031 	ack_param.param_hdr.type = response_type;
3032 	ack_param.param_hdr.length = htons(sizeof(ack_param) +
3033 					   err_param_len +
3034 					   asconf_param_len);
3035 	ack_param.crr_id = crr_id;
3036 	sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
3037 
3038 	if (SCTP_ERROR_NO_ERROR == err_code)
3039 		return;
3040 
3041 	/* Add Error Cause parameter. */
3042 	err_param.cause = err_code;
3043 	err_param.length = htons(err_param_len + asconf_param_len);
3044 	sctp_addto_chunk(chunk, err_param_len, &err_param);
3045 
3046 	/* Add the failed TLV copied from ASCONF chunk. */
3047 	if (asconf_param)
3048 		sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
3049 }
3050 
3051 /* Process a asconf parameter. */
3052 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
3053 					struct sctp_chunk *asconf,
3054 					struct sctp_addip_param *asconf_param)
3055 {
3056 	union sctp_addr_param *addr_param;
3057 	struct sctp_transport *peer;
3058 	union sctp_addr	addr;
3059 	struct sctp_af *af;
3060 
3061 	if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
3062 	    asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
3063 	    asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
3064 		return SCTP_ERROR_UNKNOWN_PARAM;
3065 
3066 	addr_param = (void *)asconf_param + sizeof(*asconf_param);
3067 	if (ntohs(addr_param->p.length) >
3068 	    ntohs(asconf_param->param_hdr.length) - sizeof(*asconf_param))
3069 		return SCTP_ERROR_PROTO_VIOLATION;
3070 
3071 	switch (addr_param->p.type) {
3072 	case SCTP_PARAM_IPV6_ADDRESS:
3073 		if (!asoc->peer.ipv6_address)
3074 			return SCTP_ERROR_DNS_FAILED;
3075 		break;
3076 	case SCTP_PARAM_IPV4_ADDRESS:
3077 		if (!asoc->peer.ipv4_address)
3078 			return SCTP_ERROR_DNS_FAILED;
3079 		break;
3080 	default:
3081 		return SCTP_ERROR_DNS_FAILED;
3082 	}
3083 
3084 	af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3085 	if (unlikely(!af))
3086 		return SCTP_ERROR_DNS_FAILED;
3087 
3088 	if (!af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0))
3089 		return SCTP_ERROR_DNS_FAILED;
3090 
3091 	/* ADDIP 4.2.1  This parameter MUST NOT contain a broadcast
3092 	 * or multicast address.
3093 	 * (note: wildcard is permitted and requires special handling so
3094 	 *  make sure we check for that)
3095 	 */
3096 	if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
3097 		return SCTP_ERROR_DNS_FAILED;
3098 
3099 	switch (asconf_param->param_hdr.type) {
3100 	case SCTP_PARAM_ADD_IP:
3101 		/* Section 4.2.1:
3102 		 * If the address 0.0.0.0 or ::0 is provided, the source
3103 		 * address of the packet MUST be added.
3104 		 */
3105 		if (af->is_any(&addr))
3106 			memcpy(&addr, &asconf->source, sizeof(addr));
3107 
3108 		if (security_sctp_bind_connect(asoc->ep->base.sk,
3109 					       SCTP_PARAM_ADD_IP,
3110 					       (struct sockaddr *)&addr,
3111 					       af->sockaddr_len))
3112 			return SCTP_ERROR_REQ_REFUSED;
3113 
3114 		/* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
3115 		 * request and does not have the local resources to add this
3116 		 * new address to the association, it MUST return an Error
3117 		 * Cause TLV set to the new error code 'Operation Refused
3118 		 * Due to Resource Shortage'.
3119 		 */
3120 
3121 		peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
3122 		if (!peer)
3123 			return SCTP_ERROR_RSRC_LOW;
3124 
3125 		/* Start the heartbeat timer. */
3126 		sctp_transport_reset_hb_timer(peer);
3127 		asoc->new_transport = peer;
3128 		break;
3129 	case SCTP_PARAM_DEL_IP:
3130 		/* ADDIP 4.3 D7) If a request is received to delete the
3131 		 * last remaining IP address of a peer endpoint, the receiver
3132 		 * MUST send an Error Cause TLV with the error cause set to the
3133 		 * new error code 'Request to Delete Last Remaining IP Address'.
3134 		 */
3135 		if (asoc->peer.transport_count == 1)
3136 			return SCTP_ERROR_DEL_LAST_IP;
3137 
3138 		/* ADDIP 4.3 D8) If a request is received to delete an IP
3139 		 * address which is also the source address of the IP packet
3140 		 * which contained the ASCONF chunk, the receiver MUST reject
3141 		 * this request. To reject the request the receiver MUST send
3142 		 * an Error Cause TLV set to the new error code 'Request to
3143 		 * Delete Source IP Address'
3144 		 */
3145 		if (sctp_cmp_addr_exact(&asconf->source, &addr))
3146 			return SCTP_ERROR_DEL_SRC_IP;
3147 
3148 		/* Section 4.2.2
3149 		 * If the address 0.0.0.0 or ::0 is provided, all
3150 		 * addresses of the peer except	the source address of the
3151 		 * packet MUST be deleted.
3152 		 */
3153 		if (af->is_any(&addr)) {
3154 			sctp_assoc_set_primary(asoc, asconf->transport);
3155 			sctp_assoc_del_nonprimary_peers(asoc,
3156 							asconf->transport);
3157 			return SCTP_ERROR_NO_ERROR;
3158 		}
3159 
3160 		/* If the address is not part of the association, the
3161 		 * ASCONF-ACK with Error Cause Indication Parameter
3162 		 * which including cause of Unresolvable Address should
3163 		 * be sent.
3164 		 */
3165 		peer = sctp_assoc_lookup_paddr(asoc, &addr);
3166 		if (!peer)
3167 			return SCTP_ERROR_DNS_FAILED;
3168 
3169 		/* Don't free asconf->transport; a later wildcard DEL-IP
3170 		 * parameter reuses it.
3171 		 */
3172 		if (peer == asconf->transport)
3173 			return SCTP_ERROR_REQ_REFUSED;
3174 
3175 		sctp_assoc_rm_peer(asoc, peer);
3176 		break;
3177 	case SCTP_PARAM_SET_PRIMARY:
3178 		/* ADDIP Section 4.2.4
3179 		 * If the address 0.0.0.0 or ::0 is provided, the receiver
3180 		 * MAY mark the source address of the packet as its
3181 		 * primary.
3182 		 */
3183 		if (af->is_any(&addr))
3184 			memcpy(&addr, sctp_source(asconf), sizeof(addr));
3185 
3186 		if (security_sctp_bind_connect(asoc->ep->base.sk,
3187 					       SCTP_PARAM_SET_PRIMARY,
3188 					       (struct sockaddr *)&addr,
3189 					       af->sockaddr_len))
3190 			return SCTP_ERROR_REQ_REFUSED;
3191 
3192 		peer = sctp_assoc_lookup_paddr(asoc, &addr);
3193 		if (!peer)
3194 			return SCTP_ERROR_DNS_FAILED;
3195 
3196 		sctp_assoc_set_primary(asoc, peer);
3197 		break;
3198 	}
3199 
3200 	return SCTP_ERROR_NO_ERROR;
3201 }
3202 
3203 /* Verify the ASCONF packet before we process it. */
3204 bool sctp_verify_asconf(const struct sctp_association *asoc,
3205 			struct sctp_chunk *chunk, bool addr_param_needed,
3206 			struct sctp_paramhdr **errp)
3207 {
3208 	struct sctp_addip_chunk *addip;
3209 	bool addr_param_seen = false;
3210 	union sctp_params param;
3211 
3212 	addip = (struct sctp_addip_chunk *)chunk->chunk_hdr;
3213 	sctp_walk_params(param, addip) {
3214 		size_t length = ntohs(param.p->length);
3215 
3216 		*errp = param.p;
3217 		switch (param.p->type) {
3218 		case SCTP_PARAM_ERR_CAUSE:
3219 			if (length < sizeof(struct sctp_addip_param) +
3220 				     sizeof(struct sctp_errhdr))
3221 				return false;
3222 			break;
3223 		case SCTP_PARAM_IPV4_ADDRESS:
3224 			if (length != sizeof(struct sctp_ipv4addr_param))
3225 				return false;
3226 			/* ensure there is only one addr param and it's in the
3227 			 * beginning of addip_hdr params, or we reject it.
3228 			 */
3229 			if (param.v != (addip + 1))
3230 				return false;
3231 			addr_param_seen = true;
3232 			break;
3233 		case SCTP_PARAM_IPV6_ADDRESS:
3234 			if (length != sizeof(struct sctp_ipv6addr_param))
3235 				return false;
3236 			if (param.v != (addip + 1))
3237 				return false;
3238 			addr_param_seen = true;
3239 			break;
3240 		case SCTP_PARAM_ADD_IP:
3241 		case SCTP_PARAM_DEL_IP:
3242 		case SCTP_PARAM_SET_PRIMARY:
3243 			/* In ASCONF chunks, these need to be first. */
3244 			if (addr_param_needed && !addr_param_seen)
3245 				return false;
3246 			length = ntohs(param.addip->param_hdr.length);
3247 			if (length < sizeof(struct sctp_addip_param) +
3248 				     sizeof(**errp))
3249 				return false;
3250 			break;
3251 		case SCTP_PARAM_SUCCESS_REPORT:
3252 		case SCTP_PARAM_ADAPTATION_LAYER_IND:
3253 			if (length != sizeof(struct sctp_addip_param))
3254 				return false;
3255 			break;
3256 		default:
3257 			/* This is unknown to us, reject! */
3258 			return false;
3259 		}
3260 	}
3261 
3262 	/* Remaining sanity checks. */
3263 	if (addr_param_needed && !addr_param_seen)
3264 		return false;
3265 	if (!addr_param_needed && addr_param_seen)
3266 		return false;
3267 	if (param.v != chunk->chunk_end)
3268 		return false;
3269 
3270 	return true;
3271 }
3272 
3273 /* Process an incoming ASCONF chunk with the next expected serial no. and
3274  * return an ASCONF_ACK chunk to be sent in response.
3275  */
3276 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3277 				       struct sctp_chunk *asconf)
3278 {
3279 	union sctp_addr_param *addr_param;
3280 	struct sctp_addip_chunk *addip;
3281 	struct sctp_chunk *asconf_ack;
3282 	bool all_param_pass = true;
3283 	struct sctp_addiphdr *hdr;
3284 	int length = 0, chunk_len;
3285 	union sctp_params param;
3286 	__be16 err_code;
3287 	__u32 serial;
3288 
3289 	addip = (struct sctp_addip_chunk *)asconf->chunk_hdr;
3290 	chunk_len = ntohs(asconf->chunk_hdr->length) -
3291 		    sizeof(struct sctp_chunkhdr);
3292 	hdr = (struct sctp_addiphdr *)asconf->skb->data;
3293 	serial = ntohl(hdr->serial);
3294 
3295 	/* Skip the addiphdr and store a pointer to address parameter.  */
3296 	length = sizeof(*hdr);
3297 	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3298 	chunk_len -= length;
3299 
3300 	/* Skip the address parameter and store a pointer to the first
3301 	 * asconf parameter.
3302 	 */
3303 	length = ntohs(addr_param->p.length);
3304 	chunk_len -= length;
3305 
3306 	/* create an ASCONF_ACK chunk.
3307 	 * Based on the definitions of parameters, we know that the size of
3308 	 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
3309 	 * parameters.
3310 	 */
3311 	asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
3312 	if (!asconf_ack)
3313 		goto done;
3314 
3315 	/* Process the TLVs contained within the ASCONF chunk. */
3316 	sctp_walk_params(param, addip) {
3317 		/* Skip preceding address parameters. */
3318 		if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
3319 		    param.p->type == SCTP_PARAM_IPV6_ADDRESS)
3320 			continue;
3321 
3322 		err_code = sctp_process_asconf_param(asoc, asconf,
3323 						     param.addip);
3324 		/* ADDIP 4.1 A7)
3325 		 * If an error response is received for a TLV parameter,
3326 		 * all TLVs with no response before the failed TLV are
3327 		 * considered successful if not reported.  All TLVs after
3328 		 * the failed response are considered unsuccessful unless
3329 		 * a specific success indication is present for the parameter.
3330 		 */
3331 		if (err_code != SCTP_ERROR_NO_ERROR)
3332 			all_param_pass = false;
3333 		if (!all_param_pass)
3334 			sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
3335 						 err_code, param.addip);
3336 
3337 		/* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3338 		 * an IP address sends an 'Out of Resource' in its response, it
3339 		 * MUST also fail any subsequent add or delete requests bundled
3340 		 * in the ASCONF.
3341 		 */
3342 		if (err_code == SCTP_ERROR_RSRC_LOW)
3343 			goto done;
3344 	}
3345 done:
3346 	/* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3347 	 * after freeing the reference to old asconf ack if any.
3348 	 */
3349 	if (asconf_ack) {
3350 		asoc->peer.addip_serial++;
3351 		sctp_chunk_hold(asconf_ack);
3352 		list_add_tail(&asconf_ack->transmitted_list,
3353 			      &asoc->asconf_ack_list);
3354 	}
3355 
3356 	return asconf_ack;
3357 }
3358 
3359 /* Process a asconf parameter that is successfully acked. */
3360 static void sctp_asconf_param_success(struct sctp_association *asoc,
3361 				      struct sctp_addip_param *asconf_param)
3362 {
3363 	struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3364 	union sctp_addr_param *addr_param;
3365 	struct sctp_sockaddr_entry *saddr;
3366 	struct sctp_transport *transport;
3367 	union sctp_addr	addr;
3368 	struct sctp_af *af;
3369 
3370 	addr_param = (void *)asconf_param + sizeof(*asconf_param);
3371 
3372 	/* We have checked the packet before, so we do not check again.	*/
3373 	af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3374 	if (!af->from_addr_param(&addr, addr_param, htons(bp->port), 0))
3375 		return;
3376 
3377 	switch (asconf_param->param_hdr.type) {
3378 	case SCTP_PARAM_ADD_IP:
3379 		/* This is always done in BH context with a socket lock
3380 		 * held, so the list can not change.
3381 		 */
3382 		local_bh_disable();
3383 		list_for_each_entry(saddr, &bp->address_list, list) {
3384 			if (sctp_cmp_addr_exact(&saddr->a, &addr))
3385 				saddr->state = SCTP_ADDR_SRC;
3386 		}
3387 		local_bh_enable();
3388 		list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3389 				transports) {
3390 			sctp_transport_dst_release(transport);
3391 		}
3392 		break;
3393 	case SCTP_PARAM_DEL_IP:
3394 		local_bh_disable();
3395 		sctp_del_bind_addr(bp, &addr);
3396 		if (asoc->asconf_addr_del_pending != NULL &&
3397 		    sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
3398 			kfree(asoc->asconf_addr_del_pending);
3399 			asoc->asconf_addr_del_pending = NULL;
3400 		}
3401 		local_bh_enable();
3402 		list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3403 				transports) {
3404 			sctp_transport_dst_release(transport);
3405 		}
3406 		break;
3407 	default:
3408 		break;
3409 	}
3410 }
3411 
3412 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3413  * for the given asconf parameter.  If there is no response for this parameter,
3414  * return the error code based on the third argument 'no_err'.
3415  * ADDIP 4.1
3416  * A7) If an error response is received for a TLV parameter, all TLVs with no
3417  * response before the failed TLV are considered successful if not reported.
3418  * All TLVs after the failed response are considered unsuccessful unless a
3419  * specific success indication is present for the parameter.
3420  */
3421 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3422 				       struct sctp_addip_param *asconf_param,
3423 				       int no_err)
3424 {
3425 	struct sctp_addip_param	*asconf_ack_param;
3426 	struct sctp_errhdr *err_param;
3427 	int asconf_ack_len;
3428 	__be16 err_code;
3429 	int length;
3430 
3431 	if (no_err)
3432 		err_code = SCTP_ERROR_NO_ERROR;
3433 	else
3434 		err_code = SCTP_ERROR_REQ_REFUSED;
3435 
3436 	asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3437 			 sizeof(struct sctp_chunkhdr);
3438 
3439 	/* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3440 	 * the first asconf_ack parameter.
3441 	 */
3442 	length = sizeof(struct sctp_addiphdr);
3443 	asconf_ack_param = (struct sctp_addip_param *)(asconf_ack->skb->data +
3444 						       length);
3445 	asconf_ack_len -= length;
3446 
3447 	while (asconf_ack_len > 0) {
3448 		if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3449 			switch (asconf_ack_param->param_hdr.type) {
3450 			case SCTP_PARAM_SUCCESS_REPORT:
3451 				return SCTP_ERROR_NO_ERROR;
3452 			case SCTP_PARAM_ERR_CAUSE:
3453 				length = sizeof(*asconf_ack_param);
3454 				err_param = (void *)asconf_ack_param + length;
3455 				return err_param->cause;
3456 			default:
3457 				return SCTP_ERROR_INV_PARAM;
3458 			}
3459 		}
3460 
3461 		length = ntohs(asconf_ack_param->param_hdr.length);
3462 		asconf_ack_param = (void *)asconf_ack_param + SCTP_PAD4(length);
3463 		asconf_ack_len -= SCTP_PAD4(length);
3464 	}
3465 
3466 	return err_code;
3467 }
3468 
3469 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3470 int sctp_process_asconf_ack(struct sctp_association *asoc,
3471 			    struct sctp_chunk *asconf_ack)
3472 {
3473 	struct sctp_chunk *asconf = asoc->addip_last_asconf;
3474 	struct sctp_addip_param *asconf_param;
3475 	__be16 err_code = SCTP_ERROR_NO_ERROR;
3476 	union sctp_addr_param *addr_param;
3477 	int asconf_len = asconf->skb->len;
3478 	int all_param_pass = 0;
3479 	int length = 0;
3480 	int no_err = 1;
3481 	int retval = 0;
3482 
3483 	/* Skip the chunkhdr and addiphdr from the last asconf sent and store
3484 	 * a pointer to address parameter.
3485 	 */
3486 	length = sizeof(struct sctp_addip_chunk);
3487 	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3488 	asconf_len -= length;
3489 
3490 	/* Skip the address parameter in the last asconf sent and store a
3491 	 * pointer to the first asconf parameter.
3492 	 */
3493 	length = ntohs(addr_param->p.length);
3494 	asconf_param = (void *)addr_param + length;
3495 	asconf_len -= length;
3496 
3497 	/* ADDIP 4.1
3498 	 * A8) If there is no response(s) to specific TLV parameter(s), and no
3499 	 * failures are indicated, then all request(s) are considered
3500 	 * successful.
3501 	 */
3502 	if (asconf_ack->skb->len == sizeof(struct sctp_addiphdr))
3503 		all_param_pass = 1;
3504 
3505 	/* Process the TLVs contained in the last sent ASCONF chunk. */
3506 	while (asconf_len > 0) {
3507 		if (all_param_pass)
3508 			err_code = SCTP_ERROR_NO_ERROR;
3509 		else {
3510 			err_code = sctp_get_asconf_response(asconf_ack,
3511 							    asconf_param,
3512 							    no_err);
3513 			if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3514 				no_err = 0;
3515 		}
3516 
3517 		switch (err_code) {
3518 		case SCTP_ERROR_NO_ERROR:
3519 			sctp_asconf_param_success(asoc, asconf_param);
3520 			break;
3521 
3522 		case SCTP_ERROR_RSRC_LOW:
3523 			retval = 1;
3524 			break;
3525 
3526 		case SCTP_ERROR_UNKNOWN_PARAM:
3527 			/* Disable sending this type of asconf parameter in
3528 			 * future.
3529 			 */
3530 			asoc->peer.addip_disabled_mask |=
3531 				asconf_param->param_hdr.type;
3532 			break;
3533 
3534 		case SCTP_ERROR_REQ_REFUSED:
3535 		case SCTP_ERROR_DEL_LAST_IP:
3536 		case SCTP_ERROR_DEL_SRC_IP:
3537 		default:
3538 			 break;
3539 		}
3540 
3541 		/* Skip the processed asconf parameter and move to the next
3542 		 * one.
3543 		 */
3544 		length = ntohs(asconf_param->param_hdr.length);
3545 		asconf_param = (void *)asconf_param + length;
3546 		asconf_len -= length;
3547 	}
3548 
3549 	if (no_err && asoc->src_out_of_asoc_ok) {
3550 		asoc->src_out_of_asoc_ok = 0;
3551 		sctp_transport_immediate_rtx(asoc->peer.primary_path);
3552 	}
3553 
3554 	/* Free the cached last sent asconf chunk. */
3555 	list_del_init(&asconf->transmitted_list);
3556 	sctp_chunk_free(asconf);
3557 	asoc->addip_last_asconf = NULL;
3558 
3559 	return retval;
3560 }
3561 
3562 /* Make a FWD TSN chunk. */
3563 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3564 				    __u32 new_cum_tsn, size_t nstreams,
3565 				    struct sctp_fwdtsn_skip *skiplist)
3566 {
3567 	struct sctp_chunk *retval = NULL;
3568 	struct sctp_fwdtsn_hdr ftsn_hdr;
3569 	struct sctp_fwdtsn_skip skip;
3570 	size_t hint;
3571 	int i;
3572 
3573 	hint = (nstreams + 1) * sizeof(__u32);
3574 
3575 	retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
3576 
3577 	if (!retval)
3578 		return NULL;
3579 
3580 	ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3581 	retval->subh.fwdtsn_hdr =
3582 		sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3583 
3584 	for (i = 0; i < nstreams; i++) {
3585 		skip.stream = skiplist[i].stream;
3586 		skip.ssn = skiplist[i].ssn;
3587 		sctp_addto_chunk(retval, sizeof(skip), &skip);
3588 	}
3589 
3590 	return retval;
3591 }
3592 
3593 struct sctp_chunk *sctp_make_ifwdtsn(const struct sctp_association *asoc,
3594 				     __u32 new_cum_tsn, size_t nstreams,
3595 				     struct sctp_ifwdtsn_skip *skiplist)
3596 {
3597 	struct sctp_chunk *retval = NULL;
3598 	struct sctp_ifwdtsn_hdr ftsn_hdr;
3599 	size_t hint;
3600 
3601 	hint = (nstreams + 1) * sizeof(__u32);
3602 
3603 	retval = sctp_make_control(asoc, SCTP_CID_I_FWD_TSN, 0, hint,
3604 				   GFP_ATOMIC);
3605 	if (!retval)
3606 		return NULL;
3607 
3608 	ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3609 	retval->subh.ifwdtsn_hdr =
3610 		sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3611 
3612 	sctp_addto_chunk(retval, nstreams * sizeof(skiplist[0]), skiplist);
3613 
3614 	return retval;
3615 }
3616 
3617 /* RE-CONFIG 3.1 (RE-CONFIG chunk)
3618  *   0                   1                   2                   3
3619  *   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
3620  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3621  *  | Type = 130    |  Chunk Flags  |      Chunk Length             |
3622  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3623  *  \                                                               \
3624  *  /                  Re-configuration Parameter                   /
3625  *  \                                                               \
3626  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3627  *  \                                                               \
3628  *  /             Re-configuration Parameter (optional)             /
3629  *  \                                                               \
3630  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3631  */
3632 static struct sctp_chunk *sctp_make_reconf(const struct sctp_association *asoc,
3633 					   int length)
3634 {
3635 	struct sctp_reconf_chunk *reconf;
3636 	struct sctp_chunk *retval;
3637 
3638 	retval = sctp_make_control(asoc, SCTP_CID_RECONF, 0, length,
3639 				   GFP_ATOMIC);
3640 	if (!retval)
3641 		return NULL;
3642 
3643 	reconf = (struct sctp_reconf_chunk *)retval->chunk_hdr;
3644 	retval->param_hdr.v = (u8 *)(reconf + 1);
3645 
3646 	return retval;
3647 }
3648 
3649 /* RE-CONFIG 4.1 (STREAM OUT RESET)
3650  *   0                   1                   2                   3
3651  *   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
3652  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3653  *  |     Parameter Type = 13       | Parameter Length = 16 + 2 * N |
3654  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3655  *  |           Re-configuration Request Sequence Number            |
3656  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3657  *  |           Re-configuration Response Sequence Number           |
3658  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3659  *  |                Sender's Last Assigned TSN                     |
3660  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3661  *  |  Stream Number 1 (optional)   |    Stream Number 2 (optional) |
3662  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3663  *  /                            ......                             /
3664  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3665  *  |  Stream Number N-1 (optional) |    Stream Number N (optional) |
3666  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3667  *
3668  * RE-CONFIG 4.2 (STREAM IN RESET)
3669  *   0                   1                   2                   3
3670  *   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
3671  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3672  *  |     Parameter Type = 14       |  Parameter Length = 8 + 2 * N |
3673  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3674  *  |          Re-configuration Request Sequence Number             |
3675  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3676  *  |  Stream Number 1 (optional)   |    Stream Number 2 (optional) |
3677  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3678  *  /                            ......                             /
3679  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3680  *  |  Stream Number N-1 (optional) |    Stream Number N (optional) |
3681  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3682  */
3683 struct sctp_chunk *sctp_make_strreset_req(
3684 					const struct sctp_association *asoc,
3685 					__u16 stream_num, __be16 *stream_list,
3686 					bool out, bool in)
3687 {
3688 	__u16 stream_len = stream_num * sizeof(__u16);
3689 	struct sctp_strreset_outreq outreq;
3690 	struct sctp_strreset_inreq inreq;
3691 	struct sctp_chunk *retval;
3692 	__u16 outlen, inlen;
3693 
3694 	outlen = (sizeof(outreq) + stream_len) * out;
3695 	inlen = (sizeof(inreq) + stream_len) * in;
3696 
3697 	retval = sctp_make_reconf(asoc, SCTP_PAD4(outlen) + SCTP_PAD4(inlen));
3698 	if (!retval)
3699 		return NULL;
3700 
3701 	if (outlen) {
3702 		outreq.param_hdr.type = SCTP_PARAM_RESET_OUT_REQUEST;
3703 		outreq.param_hdr.length = htons(outlen);
3704 		outreq.request_seq = htonl(asoc->strreset_outseq);
3705 		outreq.response_seq = htonl(asoc->strreset_inseq - 1);
3706 		outreq.send_reset_at_tsn = htonl(asoc->next_tsn - 1);
3707 
3708 		sctp_addto_chunk(retval, sizeof(outreq), &outreq);
3709 
3710 		if (stream_len)
3711 			sctp_addto_chunk(retval, stream_len, stream_list);
3712 	}
3713 
3714 	if (inlen) {
3715 		inreq.param_hdr.type = SCTP_PARAM_RESET_IN_REQUEST;
3716 		inreq.param_hdr.length = htons(inlen);
3717 		inreq.request_seq = htonl(asoc->strreset_outseq + out);
3718 
3719 		sctp_addto_chunk(retval, sizeof(inreq), &inreq);
3720 
3721 		if (stream_len)
3722 			sctp_addto_chunk(retval, stream_len, stream_list);
3723 	}
3724 
3725 	return retval;
3726 }
3727 
3728 /* RE-CONFIG 4.3 (SSN/TSN RESET ALL)
3729  *   0                   1                   2                   3
3730  *   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
3731  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3732  *  |     Parameter Type = 15       |      Parameter Length = 8     |
3733  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3734  *  |         Re-configuration Request Sequence Number              |
3735  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3736  */
3737 struct sctp_chunk *sctp_make_strreset_tsnreq(
3738 					const struct sctp_association *asoc)
3739 {
3740 	struct sctp_strreset_tsnreq tsnreq;
3741 	__u16 length = sizeof(tsnreq);
3742 	struct sctp_chunk *retval;
3743 
3744 	retval = sctp_make_reconf(asoc, length);
3745 	if (!retval)
3746 		return NULL;
3747 
3748 	tsnreq.param_hdr.type = SCTP_PARAM_RESET_TSN_REQUEST;
3749 	tsnreq.param_hdr.length = htons(length);
3750 	tsnreq.request_seq = htonl(asoc->strreset_outseq);
3751 
3752 	sctp_addto_chunk(retval, sizeof(tsnreq), &tsnreq);
3753 
3754 	return retval;
3755 }
3756 
3757 /* RE-CONFIG 4.5/4.6 (ADD STREAM)
3758  *   0                   1                   2                   3
3759  *   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
3760  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3761  *  |     Parameter Type = 17       |      Parameter Length = 12    |
3762  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3763  *  |          Re-configuration Request Sequence Number             |
3764  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3765  *  |      Number of new streams    |         Reserved              |
3766  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3767  */
3768 struct sctp_chunk *sctp_make_strreset_addstrm(
3769 					const struct sctp_association *asoc,
3770 					__u16 out, __u16 in)
3771 {
3772 	struct sctp_strreset_addstrm addstrm;
3773 	__u16 size = sizeof(addstrm);
3774 	struct sctp_chunk *retval;
3775 
3776 	retval = sctp_make_reconf(asoc, (!!out + !!in) * size);
3777 	if (!retval)
3778 		return NULL;
3779 
3780 	if (out) {
3781 		addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_OUT_STREAMS;
3782 		addstrm.param_hdr.length = htons(size);
3783 		addstrm.number_of_streams = htons(out);
3784 		addstrm.request_seq = htonl(asoc->strreset_outseq);
3785 		addstrm.reserved = 0;
3786 
3787 		sctp_addto_chunk(retval, size, &addstrm);
3788 	}
3789 
3790 	if (in) {
3791 		addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_IN_STREAMS;
3792 		addstrm.param_hdr.length = htons(size);
3793 		addstrm.number_of_streams = htons(in);
3794 		addstrm.request_seq = htonl(asoc->strreset_outseq + !!out);
3795 		addstrm.reserved = 0;
3796 
3797 		sctp_addto_chunk(retval, size, &addstrm);
3798 	}
3799 
3800 	return retval;
3801 }
3802 
3803 /* RE-CONFIG 4.4 (RESP)
3804  *   0                   1                   2                   3
3805  *   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
3806  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3807  *  |     Parameter Type = 16       |      Parameter Length         |
3808  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3809  *  |         Re-configuration Response Sequence Number             |
3810  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3811  *  |                            Result                             |
3812  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3813  */
3814 struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
3815 					   __u32 result, __u32 sn)
3816 {
3817 	struct sctp_strreset_resp resp;
3818 	__u16 length = sizeof(resp);
3819 	struct sctp_chunk *retval;
3820 
3821 	retval = sctp_make_reconf(asoc, length);
3822 	if (!retval)
3823 		return NULL;
3824 
3825 	resp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3826 	resp.param_hdr.length = htons(length);
3827 	resp.response_seq = htonl(sn);
3828 	resp.result = htonl(result);
3829 
3830 	sctp_addto_chunk(retval, sizeof(resp), &resp);
3831 
3832 	return retval;
3833 }
3834 
3835 /* RE-CONFIG 4.4 OPTIONAL (TSNRESP)
3836  *   0                   1                   2                   3
3837  *   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
3838  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3839  *  |     Parameter Type = 16       |      Parameter Length         |
3840  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3841  *  |         Re-configuration Response Sequence Number             |
3842  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3843  *  |                            Result                             |
3844  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3845  *  |                   Sender's Next TSN (optional)                |
3846  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3847  *  |                  Receiver's Next TSN (optional)               |
3848  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3849  */
3850 struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
3851 					      __u32 result, __u32 sn,
3852 					      __u32 sender_tsn,
3853 					      __u32 receiver_tsn)
3854 {
3855 	struct sctp_strreset_resptsn tsnresp;
3856 	__u16 length = sizeof(tsnresp);
3857 	struct sctp_chunk *retval;
3858 
3859 	retval = sctp_make_reconf(asoc, length);
3860 	if (!retval)
3861 		return NULL;
3862 
3863 	tsnresp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3864 	tsnresp.param_hdr.length = htons(length);
3865 
3866 	tsnresp.response_seq = htonl(sn);
3867 	tsnresp.result = htonl(result);
3868 	tsnresp.senders_next_tsn = htonl(sender_tsn);
3869 	tsnresp.receivers_next_tsn = htonl(receiver_tsn);
3870 
3871 	sctp_addto_chunk(retval, sizeof(tsnresp), &tsnresp);
3872 
3873 	return retval;
3874 }
3875 
3876 bool sctp_verify_reconf(const struct sctp_association *asoc,
3877 			struct sctp_chunk *chunk,
3878 			struct sctp_paramhdr **errp)
3879 {
3880 	struct sctp_reconf_chunk *hdr;
3881 	union sctp_params param;
3882 	__be16 last = 0;
3883 	__u16 cnt = 0;
3884 
3885 	hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3886 	sctp_walk_params(param, hdr) {
3887 		__u16 length = ntohs(param.p->length);
3888 
3889 		*errp = param.p;
3890 		if (cnt++ > 2)
3891 			return false;
3892 		switch (param.p->type) {
3893 		case SCTP_PARAM_RESET_OUT_REQUEST:
3894 			if (length < sizeof(struct sctp_strreset_outreq) ||
3895 			    (last && last != SCTP_PARAM_RESET_RESPONSE &&
3896 			     last != SCTP_PARAM_RESET_IN_REQUEST))
3897 				return false;
3898 			break;
3899 		case SCTP_PARAM_RESET_IN_REQUEST:
3900 			if (length < sizeof(struct sctp_strreset_inreq) ||
3901 			    (last && last != SCTP_PARAM_RESET_OUT_REQUEST))
3902 				return false;
3903 			break;
3904 		case SCTP_PARAM_RESET_RESPONSE:
3905 			if ((length != sizeof(struct sctp_strreset_resp) &&
3906 			     length != sizeof(struct sctp_strreset_resptsn)) ||
3907 			    (last && last != SCTP_PARAM_RESET_RESPONSE &&
3908 			     last != SCTP_PARAM_RESET_OUT_REQUEST))
3909 				return false;
3910 			break;
3911 		case SCTP_PARAM_RESET_TSN_REQUEST:
3912 			if (length !=
3913 			    sizeof(struct sctp_strreset_tsnreq) || last)
3914 				return false;
3915 			break;
3916 		case SCTP_PARAM_RESET_ADD_IN_STREAMS:
3917 			if (length != sizeof(struct sctp_strreset_addstrm) ||
3918 			    (last && last != SCTP_PARAM_RESET_ADD_OUT_STREAMS))
3919 				return false;
3920 			break;
3921 		case SCTP_PARAM_RESET_ADD_OUT_STREAMS:
3922 			if (length != sizeof(struct sctp_strreset_addstrm) ||
3923 			    (last && last != SCTP_PARAM_RESET_ADD_IN_STREAMS))
3924 				return false;
3925 			break;
3926 		default:
3927 			return false;
3928 		}
3929 
3930 		last = param.p->type;
3931 	}
3932 
3933 	return true;
3934 }
3935