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