sm_make_chunk.c 103 KB
Newer Older
1
/* SCTP kernel implementation
Linus Torvalds's avatar
Linus Torvalds committed
2
3
4
5
6
 * (C) Copyright IBM Corp. 2001, 2004
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 2001-2002 Intel Corp.
 *
7
 * This file is part of the SCTP kernel implementation
Linus Torvalds's avatar
Linus Torvalds committed
8
9
10
11
12
 *
 * These functions work with the state functions in sctp_sm_statefuns.c
 * to implement the state operations.  These functions implement the
 * steps which require modifying existing data structures.
 *
13
 * This SCTP implementation is free software;
Linus Torvalds's avatar
Linus Torvalds committed
14
15
16
17
18
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
19
 * This SCTP implementation is distributed in the hope that it
Linus Torvalds's avatar
Linus Torvalds committed
20
21
22
23
24
25
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
26
27
 * along with GNU CC; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
Linus Torvalds's avatar
Linus Torvalds committed
28
29
30
 *
 * Please send any bug reports or fixes you make to the
 * email address(es):
31
 *    lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds's avatar
Linus Torvalds committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 *
 * Written or modified by:
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson          <karl@athena.chicago.il.us>
 *    C. Robin              <chris@hundredacre.ac.uk>
 *    Jon Grimm             <jgrimm@us.ibm.com>
 *    Xingang Guo           <xingang.guo@intel.com>
 *    Dajiang Zhang	    <dajiang.zhang@nokia.com>
 *    Sridhar Samudrala	    <sri@us.ibm.com>
 *    Daisy Chang	    <daisyc@us.ibm.com>
 *    Ardelle Fan	    <ardelle.fan@intel.com>
 *    Kevin Gao             <kevin.gao@intel.com>
 */

46
47
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Herbert Xu's avatar
Herbert Xu committed
48
#include <crypto/hash.h>
Linus Torvalds's avatar
Linus Torvalds committed
49
50
51
52
53
54
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/net.h>
#include <linux/inet.h>
Christian Borntraeger's avatar
Christian Borntraeger committed
55
#include <linux/scatterlist.h>
56
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
57
58
59
60
61
62
63
#include <net/sock.h>

#include <linux/skbuff.h>
#include <linux/random.h>	/* for get_random_bytes */
#include <net/sctp/sctp.h>
#include <net/sctp/sm.h>

64
static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
65
66
					    __u8 type, __u8 flags, int paylen,
					    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
71
					   __u8 type, __u8 flags, int paylen,
					   gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
72
73
74
75
76
77
78
79
static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
					const struct sctp_association *asoc,
					const struct sctp_chunk *init_chunk,
					int *cookie_len,
					const __u8 *raw_addrs, int addrs_len);
static int sctp_process_param(struct sctp_association *asoc,
			      union sctp_params param,
			      const union sctp_addr *peer_addr,
80
			      gfp_t gfp);
81
82
static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
			      const void *data);
83
84
static void  *sctp_addto_chunk_fixed(struct sctp_chunk *, int len,
				     const void *data);
Linus Torvalds's avatar
Linus Torvalds committed
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Control chunk destructor */
static void sctp_control_release_owner(struct sk_buff *skb)
{
	/*TODO: do memory release */
}

static void sctp_control_set_owner_w(struct sctp_chunk *chunk)
{
	struct sctp_association *asoc = chunk->asoc;
	struct sk_buff *skb = chunk->skb;

	/* TODO: properly account for control chunks.
	 * To do it right we'll need:
	 *  1) endpoint if association isn't known.
	 *  2) proper memory accounting.
	 *
	 *  For now don't do anything for now.
	 */
	skb->sk = asoc ? asoc->base.sk : NULL;
	skb->destructor = sctp_control_release_owner;
}

Linus Torvalds's avatar
Linus Torvalds committed
108
109
110
111
112
113
/* What was the inbound interface for this chunk? */
int sctp_chunk_iif(const struct sctp_chunk *chunk)
{
	struct sctp_af *af;
	int iif = 0;

114
	af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds's avatar
Linus Torvalds committed
115
116
117
118
119
120
121
122
123
124
125
126
127
	if (af)
		iif = af->skb_iif(chunk->skb);

	return iif;
}

/* RFC 2960 3.3.2 Initiation (INIT) (1)
 *
 * Note 2: The ECN capable field is reserved for future use of
 * Explicit Congestion Notification.
 */
static const struct sctp_paramhdr ecap_param = {
	SCTP_PARAM_ECN_CAPABLE,
128
	cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds's avatar
Linus Torvalds committed
129
130
131
};
static const struct sctp_paramhdr prsctp_param = {
	SCTP_PARAM_FWD_TSN_SUPPORT,
132
	cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds's avatar
Linus Torvalds committed
133
134
};

135
/* A helper to initialize an op error inside a
Linus Torvalds's avatar
Linus Torvalds committed
136
137
138
 * provided chunk, as most cause codes will be embedded inside an
 * abort chunk.
 */
Al Viro's avatar
Al Viro committed
139
void  sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
140
		      size_t paylen)
Linus Torvalds's avatar
Linus Torvalds committed
141
142
143
144
{
	sctp_errhdr_t err;
	__u16 len;

145
	/* Cause code constants are now defined in network order.  */
Linus Torvalds's avatar
Linus Torvalds committed
146
147
148
	err.cause = cause_code;
	len = sizeof(sctp_errhdr_t) + paylen;
	err.length  = htons(len);
149
	chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
Linus Torvalds's avatar
Linus Torvalds committed
150
151
}

152
153
154
155
156
/* A helper to initialize an op error inside a
 * provided chunk, as most cause codes will be embedded inside an
 * abort chunk.  Differs from sctp_init_cause in that it won't oops
 * if there isn't enough space in the op error chunk
 */
157
static int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
158
159
160
161
162
163
164
165
166
167
		      size_t paylen)
{
	sctp_errhdr_t err;
	__u16 len;

	/* Cause code constants are now defined in network order.  */
	err.cause = cause_code;
	len = sizeof(sctp_errhdr_t) + paylen;
	err.length  = htons(len);

168
	if (skb_tailroom(chunk->skb) < len)
169
170
171
172
173
174
		return -ENOSPC;
	chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk,
						     sizeof(sctp_errhdr_t),
						     &err);
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* 3.3.2 Initiation (INIT) (1)
 *
 * This chunk is used to initiate a SCTP association between two
 * endpoints. The format of the INIT chunk is shown below:
 *
 *     0                   1                   2                   3
 *     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
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |   Type = 1    |  Chunk Flags  |      Chunk Length             |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                         Initiate Tag                          |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |           Advertised Receiver Window Credit (a_rwnd)          |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |  Number of Outbound Streams   |  Number of Inbound Streams    |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                          Initial TSN                          |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    \                                                               \
 *    /              Optional/Variable-Length Parameters              /
 *    \                                                               \
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *
 * The INIT chunk contains the following parameters. Unless otherwise
 * noted, each parameter MUST only be included once in the INIT chunk.
 *
 * Fixed Parameters                     Status
 * ----------------------------------------------
 * Initiate Tag                        Mandatory
 * Advertised Receiver Window Credit   Mandatory
 * Number of Outbound Streams          Mandatory
 * Number of Inbound Streams           Mandatory
 * Initial TSN                         Mandatory
 *
 * Variable Parameters                  Status     Type Value
 * -------------------------------------------------------------
 * IPv4 Address (Note 1)               Optional    5
 * IPv6 Address (Note 1)               Optional    6
 * Cookie Preservative                 Optional    9
 * Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
 * Host Name Address (Note 3)          Optional    11
 * Supported Address Types (Note 4)    Optional    12
 */
struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
			     const struct sctp_bind_addr *bp,
221
			     gfp_t gfp, int vparam_len)
Linus Torvalds's avatar
Linus Torvalds committed
222
{
223
	struct net *net = sock_net(asoc->base.sk);
224
	struct sctp_endpoint *ep = asoc->ep;
Linus Torvalds's avatar
Linus Torvalds committed
225
226
227
228
229
230
231
	sctp_inithdr_t init;
	union sctp_params addrs;
	size_t chunksize;
	struct sctp_chunk *retval = NULL;
	int num_types, addrs_len = 0;
	struct sctp_sock *sp;
	sctp_supported_addrs_param_t sat;
232
	__be16 types[2];
233
	sctp_adaptation_ind_param_t aiparam;
234
235
236
	sctp_supported_ext_param_t ext_param;
	int num_ext = 0;
	__u8 extensions[3];
237
238
	sctp_paramhdr_t *auth_chunks = NULL,
			*auth_hmacs = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

	/* RFC 2960 3.3.2 Initiation (INIT) (1)
	 *
	 * Note 1: The INIT chunks can contain multiple addresses that
	 * can be IPv4 and/or IPv6 in any combination.
	 */
	retval = NULL;

	/* Convert the provided bind address list to raw format. */
	addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);

	init.init_tag		   = htonl(asoc->c.my_vtag);
	init.a_rwnd		   = htonl(asoc->rwnd);
	init.num_outbound_streams  = htons(asoc->c.sinit_num_ostreams);
	init.num_inbound_streams   = htons(asoc->c.sinit_max_instreams);
	init.initial_tsn	   = htonl(asoc->c.initial_tsn);

	/* How many address types are needed? */
	sp = sctp_sk(asoc->base.sk);
	num_types = sp->pf->supported_addrs(sp, types);

260
261
	chunksize = sizeof(init) + addrs_len;
	chunksize += WORD_ROUND(SCTP_SAT_LEN(num_types));
Linus Torvalds's avatar
Linus Torvalds committed
262
	chunksize += sizeof(ecap_param);
263

264
	if (asoc->prsctp_enable)
265
266
		chunksize += sizeof(prsctp_param);

267
268
269
270
271
	/* ADDIP: Section 4.2.7:
	 *  An implementation supporting this extension [ADDIP] MUST list
	 *  the ASCONF,the ASCONF-ACK, and the AUTH  chunks in its INIT and
	 *  INIT-ACK parameters.
	 */
272
	if (net->sctp.addip_enable) {
273
274
275
276
277
		extensions[num_ext] = SCTP_CID_ASCONF;
		extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
		num_ext += 2;
	}

278
279
280
	if (sp->adaptation_ind)
		chunksize += sizeof(aiparam);

Linus Torvalds's avatar
Linus Torvalds committed
281
282
	chunksize += vparam_len;

283
	/* Account for AUTH related parameters */
284
	if (ep->auth_enable) {
285
286
287
288
289
290
		/* Add random parameter length*/
		chunksize += sizeof(asoc->c.auth_random);

		/* Add HMACS parameter length if any were defined */
		auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
		if (auth_hmacs->length)
291
			chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
292
293
294
295
296
297
		else
			auth_hmacs = NULL;

		/* Add CHUNKS parameter length */
		auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
		if (auth_chunks->length)
298
			chunksize += WORD_ROUND(ntohs(auth_chunks->length));
299
		else
300
			auth_chunks = NULL;
301
302
303
304
305

		extensions[num_ext] = SCTP_CID_AUTH;
		num_ext += 1;
	}

306
307
	/* If we have any extensions to report, account for that */
	if (num_ext)
308
309
		chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
					num_ext);
310

Linus Torvalds's avatar
Linus Torvalds committed
311
312
313
314
315
316
317
318
319
320
321
322
	/* RFC 2960 3.3.2 Initiation (INIT) (1)
	 *
	 * Note 3: An INIT chunk MUST NOT contain more than one Host
	 * Name address parameter. Moreover, the sender of the INIT
	 * MUST NOT combine any other address types with the Host Name
	 * address in the INIT. The receiver of INIT MUST ignore any
	 * other address types if the Host Name address parameter is
	 * present in the received INIT chunk.
	 *
	 * PLEASE DO NOT FIXME [This version does not support Host Name.]
	 */

323
	retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
Linus Torvalds's avatar
Linus Torvalds committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
	if (!retval)
		goto nodata;

	retval->subh.init_hdr =
		sctp_addto_chunk(retval, sizeof(init), &init);
	retval->param_hdr.v =
		sctp_addto_chunk(retval, addrs_len, addrs.v);

	/* RFC 2960 3.3.2 Initiation (INIT) (1)
	 *
	 * Note 4: This parameter, when present, specifies all the
	 * address types the sending endpoint can support. The absence
	 * of this parameter indicates that the sending endpoint can
	 * support any address type.
	 */
	sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
	sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
	sctp_addto_chunk(retval, sizeof(sat), &sat);
	sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);

	sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
345

Joe Perches's avatar
Joe Perches committed
346
	/* Add the supported extensions parameter.  Be nice and add this
347
348
349
350
351
352
353
354
	 * fist before addiding the parameters for the extensions themselves
	 */
	if (num_ext) {
		ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
		ext_param.param_hdr.length =
			    htons(sizeof(sctp_supported_ext_param_t) + num_ext);
		sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
				&ext_param);
355
		sctp_addto_param(retval, num_ext, extensions);
356
357
	}

358
	if (asoc->prsctp_enable)
Linus Torvalds's avatar
Linus Torvalds committed
359
		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
360

361
362
363
364
365
366
	if (sp->adaptation_ind) {
		aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
		aiparam.param_hdr.length = htons(sizeof(aiparam));
		aiparam.adaptation_ind = htonl(sp->adaptation_ind);
		sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
	}
367

368
	/* Add SCTP-AUTH chunks to the parameter list */
369
	if (ep->auth_enable) {
370
371
372
373
374
375
376
377
378
		sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
				 asoc->c.auth_random);
		if (auth_hmacs)
			sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
					auth_hmacs);
		if (auth_chunks)
			sctp_addto_chunk(retval, ntohs(auth_chunks->length),
					auth_chunks);
	}
Linus Torvalds's avatar
Linus Torvalds committed
379
nodata:
Jesper Juhl's avatar
Jesper Juhl committed
380
	kfree(addrs.v);
Linus Torvalds's avatar
Linus Torvalds committed
381
382
383
384
385
	return retval;
}

struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
				 const struct sctp_chunk *chunk,
386
				 gfp_t gfp, int unkparam_len)
Linus Torvalds's avatar
Linus Torvalds committed
387
388
389
390
{
	sctp_inithdr_t initack;
	struct sctp_chunk *retval;
	union sctp_params addrs;
391
	struct sctp_sock *sp;
Linus Torvalds's avatar
Linus Torvalds committed
392
393
394
395
	int addrs_len;
	sctp_cookie_param_t *cookie;
	int cookie_len;
	size_t chunksize;
396
	sctp_adaptation_ind_param_t aiparam;
397
398
399
	sctp_supported_ext_param_t ext_param;
	int num_ext = 0;
	__u8 extensions[3];
400
401
402
	sctp_paramhdr_t *auth_chunks = NULL,
			*auth_hmacs = NULL,
			*auth_random = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

	retval = NULL;

	/* Note: there may be no addresses to embed. */
	addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);

	initack.init_tag	        = htonl(asoc->c.my_vtag);
	initack.a_rwnd			= htonl(asoc->rwnd);
	initack.num_outbound_streams	= htons(asoc->c.sinit_num_ostreams);
	initack.num_inbound_streams	= htons(asoc->c.sinit_max_instreams);
	initack.initial_tsn		= htonl(asoc->c.initial_tsn);

	/* FIXME:  We really ought to build the cookie right
	 * into the packet instead of allocating more fresh memory.
	 */
	cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
				  addrs.v, addrs_len);
	if (!cookie)
		goto nomem_cookie;

	/* Calculate the total size of allocation, include the reserved
	 * space for reporting unknown parameters if it is specified.
	 */
426
	sp = sctp_sk(asoc->base.sk);
Linus Torvalds's avatar
Linus Torvalds committed
427
428
	chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;

429
	/* Tell peer that we'll do ECN only if peer advertised such cap.  */
Linus Torvalds's avatar
Linus Torvalds committed
430
431
432
	if (asoc->peer.ecn_capable)
		chunksize += sizeof(ecap_param);

433
	if (asoc->peer.prsctp_capable)
434
435
		chunksize += sizeof(prsctp_param);

436
	if (asoc->peer.asconf_capable) {
437
438
439
440
441
		extensions[num_ext] = SCTP_CID_ASCONF;
		extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
		num_ext += 2;
	}

442
443
	if (sp->adaptation_ind)
		chunksize += sizeof(aiparam);
Linus Torvalds's avatar
Linus Torvalds committed
444

445
446
447
448
449
450
	if (asoc->peer.auth_capable) {
		auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
		chunksize += ntohs(auth_random->length);

		auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
		if (auth_hmacs->length)
451
			chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
452
453
454
455
456
		else
			auth_hmacs = NULL;

		auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
		if (auth_chunks->length)
457
			chunksize += WORD_ROUND(ntohs(auth_chunks->length));
458
459
460
461
462
463
464
		else
			auth_chunks = NULL;

		extensions[num_ext] = SCTP_CID_AUTH;
		num_ext += 1;
	}

465
	if (num_ext)
466
467
		chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
					num_ext);
468

Linus Torvalds's avatar
Linus Torvalds committed
469
	/* Now allocate and fill out the chunk.  */
470
	retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
Linus Torvalds's avatar
Linus Torvalds committed
471
472
473
	if (!retval)
		goto nomem_chunk;

474
475
476
477
478
479
480
481
	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [INIT ACK back to where the INIT came from.]
Linus Torvalds's avatar
Linus Torvalds committed
482
483
	 */
	retval->transport = chunk->transport;
484

Linus Torvalds's avatar
Linus Torvalds committed
485
486
487
488
489
490
	retval->subh.init_hdr =
		sctp_addto_chunk(retval, sizeof(initack), &initack);
	retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
	sctp_addto_chunk(retval, cookie_len, cookie);
	if (asoc->peer.ecn_capable)
		sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
491
492
493
494
495
496
	if (num_ext) {
		ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
		ext_param.param_hdr.length =
			    htons(sizeof(sctp_supported_ext_param_t) + num_ext);
		sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
				 &ext_param);
497
		sctp_addto_param(retval, num_ext, extensions);
498
	}
Linus Torvalds's avatar
Linus Torvalds committed
499
500
501
	if (asoc->peer.prsctp_capable)
		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);

502
503
504
505
506
507
	if (sp->adaptation_ind) {
		aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
		aiparam.param_hdr.length = htons(sizeof(aiparam));
		aiparam.adaptation_ind = htonl(sp->adaptation_ind);
		sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
	}
Linus Torvalds's avatar
Linus Torvalds committed
508

509
510
511
512
513
514
515
516
517
518
519
	if (asoc->peer.auth_capable) {
		sctp_addto_chunk(retval, ntohs(auth_random->length),
				 auth_random);
		if (auth_hmacs)
			sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
					auth_hmacs);
		if (auth_chunks)
			sctp_addto_chunk(retval, ntohs(auth_chunks->length),
					auth_chunks);
	}

Linus Torvalds's avatar
Linus Torvalds committed
520
521
522
523
524
525
	/* We need to remove the const qualifier at this point.  */
	retval->asoc = (struct sctp_association *) asoc;

nomem_chunk:
	kfree(cookie);
nomem_cookie:
Jesper Juhl's avatar
Jesper Juhl committed
526
	kfree(addrs.v);
Linus Torvalds's avatar
Linus Torvalds committed
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
	return retval;
}

/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
 *
 * This chunk is used only during the initialization of an association.
 * It is sent by the initiator of an association to its peer to complete
 * the initialization process. This chunk MUST precede any DATA chunk
 * sent within the association, but MAY be bundled with one or more DATA
 * chunks in the same packet.
 *
 *      0                   1                   2                   3
 *      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
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |   Type = 10   |Chunk  Flags   |         Length                |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     /                     Cookie                                    /
 *     \                                                               \
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * Chunk Flags: 8 bit
 *
 *   Set to zero on transmit and ignored on receipt.
 *
 * Length: 16 bits (unsigned integer)
 *
 *   Set to the size of the chunk in bytes, including the 4 bytes of
 *   the chunk header and the size of the Cookie.
 *
 * Cookie: variable size
 *
 *   This field must contain the exact cookie received in the
 *   State Cookie parameter from the previous INIT ACK.
 *
 *   An implementation SHOULD make the cookie as small as possible
 *   to insure interoperability.
 */
struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
				    const struct sctp_chunk *chunk)
{
	struct sctp_chunk *retval;
	void *cookie;
	int cookie_len;

	cookie = asoc->peer.cookie;
	cookie_len = asoc->peer.cookie_len;

	/* Build a cookie echo chunk.  */
575
576
	retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
				   cookie_len, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
	if (!retval)
		goto nodata;
	retval->subh.cookie_hdr =
		sctp_addto_chunk(retval, cookie_len, cookie);

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [COOKIE ECHO back to where the INIT ACK came from.]
	 */
	if (chunk)
		retval->transport = chunk->transport;

nodata:
	return retval;
}

/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
 *
 * This chunk is used only during the initialization of an
 * association.  It is used to acknowledge the receipt of a COOKIE
 * ECHO chunk.  This chunk MUST precede any DATA or SACK chunk sent
 * within the association, but MAY be bundled with one or more DATA
 * chunks or SACK chunk in the same SCTP packet.
 *
 *      0                   1                   2                   3
 *      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
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |   Type = 11   |Chunk  Flags   |     Length = 4                |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * Chunk Flags: 8 bits
 *
 *   Set to zero on transmit and ignored on receipt.
 */
struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
				   const struct sctp_chunk *chunk)
{
	struct sctp_chunk *retval;

621
	retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [COOKIE ACK back to where the COOKIE ECHO came from.]
	 */
	if (retval && chunk)
		retval->transport = chunk->transport;

	return retval;
}

/*
 *  Appendix A: Explicit Congestion Notification:
 *  CWR:
 *
 *  RFC 2481 details a specific bit for a sender to send in the header of
 *  its next outbound TCP segment to indicate to its peer that it has
 *  reduced its congestion window.  This is termed the CWR bit.  For
 *  SCTP the same indication is made by including the CWR chunk.
 *  This chunk contains one data element, i.e. the TSN number that
 *  was sent in the ECNE chunk.  This element represents the lowest
 *  TSN number in the datagram that was originally marked with the
 *  CE bit.
 *
 *     0                   1                   2                   3
 *     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
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    | Chunk Type=13 | Flags=00000000|    Chunk Length = 8           |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    |                      Lowest TSN Number                        |
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 *     Note: The CWR is considered a Control chunk.
 */
struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
			    const __u32 lowest_tsn,
			    const struct sctp_chunk *chunk)
{
	struct sctp_chunk *retval;
	sctp_cwrhdr_t cwr;

	cwr.lowest_tsn = htonl(lowest_tsn);
669
	retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
670
				   sizeof(sctp_cwrhdr_t), GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702

	if (!retval)
		goto nodata;

	retval->subh.ecn_cwr_hdr =
		sctp_addto_chunk(retval, sizeof(cwr), &cwr);

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [Report a reduced congestion window back to where the ECNE
	 * came from.]
	 */
	if (chunk)
		retval->transport = chunk->transport;

nodata:
	return retval;
}

/* Make an ECNE chunk.  This is a congestion experienced report.  */
struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
			     const __u32 lowest_tsn)
{
	struct sctp_chunk *retval;
	sctp_ecnehdr_t ecne;

	ecne.lowest_tsn = htonl(lowest_tsn);
703
	retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
704
				   sizeof(sctp_ecnehdr_t), GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
705
706
707
708
709
710
711
712
713
	if (!retval)
		goto nodata;
	retval->subh.ecne_hdr =
		sctp_addto_chunk(retval, sizeof(ecne), &ecne);

nodata:
	return retval;
}

Xin Long's avatar
Xin Long committed
714
715
716
717
718
719
720
721
722
723
724
static void sctp_set_prsctp_policy(struct sctp_chunk *chunk,
				   const struct sctp_sndrcvinfo *sinfo)
{
	if (!chunk->asoc->prsctp_enable)
		return;

	if (SCTP_PR_TTL_ENABLED(sinfo->sinfo_flags))
		chunk->prsctp_param =
			jiffies + msecs_to_jiffies(sinfo->sinfo_timetolive);
}

Linus Torvalds's avatar
Linus Torvalds committed
725
726
727
728
729
/* Make a DATA chunk for the given association from the provided
 * parameters.  However, do not populate the data payload.
 */
struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
				       const struct sctp_sndrcvinfo *sinfo,
730
731
				       int data_len, __u8 flags, __u16 ssn,
				       gfp_t gfp)
Linus Torvalds's avatar
Linus Torvalds committed
732
733
734
735
736
737
738
739
740
741
742
743
744
{
	struct sctp_chunk *retval;
	struct sctp_datahdr dp;
	int chunk_len;

	/* We assign the TSN as LATE as possible, not here when
	 * creating the chunk.
	 */
	dp.tsn = 0;
	dp.stream = htons(sinfo->sinfo_stream);
	dp.ppid   = sinfo->sinfo_ppid;

	/* Set the flags for an unordered send.  */
745
	if (sinfo->sinfo_flags & SCTP_UNORDERED) {
Linus Torvalds's avatar
Linus Torvalds committed
746
747
748
749
750
751
		flags |= SCTP_DATA_UNORDERED;
		dp.ssn = 0;
	} else
		dp.ssn = htons(ssn);

	chunk_len = sizeof(dp) + data_len;
752
	retval = sctp_make_data(asoc, flags, chunk_len, gfp);
Linus Torvalds's avatar
Linus Torvalds committed
753
754
755
756
757
	if (!retval)
		goto nodata;

	retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
	memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
Xin Long's avatar
Xin Long committed
758
	sctp_set_prsctp_policy(retval, sinfo);
Linus Torvalds's avatar
Linus Torvalds committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774

nodata:
	return retval;
}

/* Create a selective ackowledgement (SACK) for the given
 * association.  This reports on which TSN's we've seen to date,
 * including duplicates and gaps.
 */
struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
{
	struct sctp_chunk *retval;
	struct sctp_sackhdr sack;
	int len;
	__u32 ctsn;
	__u16 num_gabs, num_dup_tsns;
775
	struct sctp_association *aptr = (struct sctp_association *)asoc;
Linus Torvalds's avatar
Linus Torvalds committed
776
	struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
777
	struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
778
	struct sctp_transport *trans;
Linus Torvalds's avatar
Linus Torvalds committed
779

780
	memset(gabs, 0, sizeof(gabs));
Linus Torvalds's avatar
Linus Torvalds committed
781
	ctsn = sctp_tsnmap_get_ctsn(map);
782
783

	pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
Linus Torvalds's avatar
Linus Torvalds committed
784
785

	/* How much room is needed in the chunk? */
786
	num_gabs = sctp_tsnmap_num_gabs(map, gabs);
Linus Torvalds's avatar
Linus Torvalds committed
787
788
789
790
791
792
793
794
795
796
797
798
799
	num_dup_tsns = sctp_tsnmap_num_dups(map);

	/* Initialize the SACK header.  */
	sack.cum_tsn_ack	    = htonl(ctsn);
	sack.a_rwnd 		    = htonl(asoc->a_rwnd);
	sack.num_gap_ack_blocks     = htons(num_gabs);
	sack.num_dup_tsns           = htons(num_dup_tsns);

	len = sizeof(sack)
		+ sizeof(struct sctp_gap_ack_block) * num_gabs
		+ sizeof(__u32) * num_dup_tsns;

	/* Create the chunk.  */
800
	retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
	if (!retval)
		goto nodata;

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, etc.) to the same destination transport
	 * address from which it received the DATA or control chunk to
	 * which it is replying.  This rule should also be followed if
	 * the endpoint is bundling DATA chunks together with the
	 * reply chunk.
	 *
	 * However, when acknowledging multiple DATA chunks received
	 * in packets from different source addresses in a single
	 * SACK, the SACK chunk may be transmitted to one of the
	 * destination transport addresses from which the DATA or
	 * control chunks being acknowledged were received.
	 *
	 * [BUG:  We do not implement the following paragraph.
	 * Perhaps we should remember the last transport we used for a
	 * SACK and avoid that (if possible) if we have seen any
	 * duplicates. --piggy]
	 *
	 * When a receiver of a duplicate DATA chunk sends a SACK to a
	 * multi- homed endpoint it MAY be beneficial to vary the
	 * destination address and not use the source address of the
	 * DATA chunk.  The reason being that receiving a duplicate
	 * from a multi-homed endpoint might indicate that the return
	 * path (as specified in the source address of the DATA chunk)
	 * for the SACK is broken.
	 *
	 * [Send to the address from which we last received a DATA chunk.]
	 */
	retval->transport = asoc->peer.last_data_from;

	retval->subh.sack_hdr =
		sctp_addto_chunk(retval, sizeof(sack), &sack);

	/* Add the gap ack block information.   */
	if (num_gabs)
		sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
842
				 gabs);
Linus Torvalds's avatar
Linus Torvalds committed
843
844

	/* Add the duplicate TSN information.  */
845
846
	if (num_dup_tsns) {
		aptr->stats.idupchunks += num_dup_tsns;
Linus Torvalds's avatar
Linus Torvalds committed
847
848
		sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
				 sctp_tsnmap_get_dups(map));
849
	}
850
851
852
853
854
855
856
857
858
859
860
861
862
863
	/* Once we have a sack generated, check to see what our sack
	 * generation is, if its 0, reset the transports to 0, and reset
	 * the association generation to 1
	 *
	 * The idea is that zero is never used as a valid generation for the
	 * association so no transport will match after a wrap event like this,
	 * Until the next sack
	 */
	if (++aptr->peer.sack_generation == 0) {
		list_for_each_entry(trans, &asoc->peer.transport_addr_list,
				    transports)
			trans->sack_generation = 0;
		aptr->peer.sack_generation = 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
nodata:
	return retval;
}

/* Make a SHUTDOWN chunk. */
struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
				      const struct sctp_chunk *chunk)
{
	struct sctp_chunk *retval;
	sctp_shutdownhdr_t shut;
	__u32 ctsn;

	ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
	shut.cum_tsn_ack = htonl(ctsn);

879
	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
880
				   sizeof(sctp_shutdownhdr_t), GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
	if (!retval)
		goto nodata;

	retval->subh.shutdown_hdr =
		sctp_addto_chunk(retval, sizeof(shut), &shut);

	if (chunk)
		retval->transport = chunk->transport;
nodata:
	return retval;
}

struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
				     const struct sctp_chunk *chunk)
{
	struct sctp_chunk *retval;

898
899
	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
				   GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [ACK back to where the SHUTDOWN came from.]
	 */
	if (retval && chunk)
		retval->transport = chunk->transport;

	return retval;
}

struct sctp_chunk *sctp_make_shutdown_complete(
	const struct sctp_association *asoc,
	const struct sctp_chunk *chunk)
{
	struct sctp_chunk *retval;
	__u8 flags = 0;

923
924
925
	/* Set the T-bit if we have no association (vtag will be
	 * reflected)
	 */
Linus Torvalds's avatar
Linus Torvalds committed
926
927
	flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;

928
929
	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
				   0, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
930
931
932
933
934
935
936
937
938
939
940
941
942
943

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
	 * came from.]
	 */
	if (retval && chunk)
		retval->transport = chunk->transport;

944
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
945
946
947
}

/* Create an ABORT.  Note that we set the T bit if we have no
948
 * association, except when responding to an INIT (sctpimpguide 2.41).
Linus Torvalds's avatar
Linus Torvalds committed
949
950
951
952
953
954
955
956
 */
struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
			      const struct sctp_chunk *chunk,
			      const size_t hint)
{
	struct sctp_chunk *retval;
	__u8 flags = 0;

957
958
959
960
961
962
963
964
965
966
	/* Set the T-bit if we have no association and 'chunk' is not
	 * an INIT (vtag will be reflected).
	 */
	if (!asoc) {
		if (chunk && chunk->chunk_hdr &&
		    chunk->chunk_hdr->type == SCTP_CID_INIT)
			flags = 0;
		else
			flags = SCTP_CHUNK_FLAG_T;
	}
Linus Torvalds's avatar
Linus Torvalds committed
967

968
969
	retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
				   GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [ABORT back to where the offender came from.]
	 */
	if (retval && chunk)
		retval->transport = chunk->transport;

	return retval;
}

/* Helper to create ABORT with a NO_USER_DATA error.  */
struct sctp_chunk *sctp_make_abort_no_data(
	const struct sctp_association *asoc,
	const struct sctp_chunk *chunk, __u32 tsn)
{
	struct sctp_chunk *retval;
992
	__be32 payload;
Linus Torvalds's avatar
Linus Torvalds committed
993
994
995
996
997
998
999
1000
1001

	retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
				 + sizeof(tsn));

	if (!retval)
		goto no_mem;

	/* Put the tsn back into network byte order.  */
	payload = htonl(tsn);
1002
1003
	sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
	sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
Linus Torvalds's avatar
Linus Torvalds committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [ABORT back to where the offender came from.]
	 */
	if (chunk)
		retval->transport = chunk->transport;

no_mem:
	return retval;
}

/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error.  */
struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
Al Viro's avatar
Al Viro committed
1023
					struct msghdr *msg,
1024
					size_t paylen)
Linus Torvalds's avatar
Linus Torvalds committed
1025
1026
{
	struct sctp_chunk *retval;
1027
1028
	void *payload = NULL;
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
1029

1030
	retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
Linus Torvalds's avatar
Linus Torvalds committed
1031
1032
1033
1034
1035
	if (!retval)
		goto err_chunk;

	if (paylen) {
		/* Put the msg_iov together into payload.  */
1036
		payload = kmalloc(paylen, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1037
1038
		if (!payload)
			goto err_payload;
1039

Al Viro's avatar
Al Viro committed
1040
		err = memcpy_from_msg(payload, msg, paylen);
1041
1042
		if (err < 0)
			goto err_copy;
Linus Torvalds's avatar
Linus Torvalds committed
1043
1044
	}

1045
1046
	sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
	sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds's avatar
Linus Torvalds committed
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061

	if (paylen)
		kfree(payload);

	return retval;

err_copy:
	kfree(payload);
err_payload:
	sctp_chunk_free(retval);
	retval = NULL;
err_chunk:
	return retval;
}

1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
/* Append bytes to the end of a parameter.  Will panic if chunk is not big
 * enough.
 */
static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
			      const void *data)
{
	void *target;
	int chunklen = ntohs(chunk->chunk_hdr->length);

	target = skb_put(chunk->skb, len);

1073
1074
1075
1076
	if (data)
		memcpy(target, data, len);
	else
		memset(target, 0, len);
1077
1078
1079
1080
1081
1082
1083
1084

	/* Adjust the chunk length field.  */
	chunk->chunk_hdr->length = htons(chunklen + len);
	chunk->chunk_end = skb_tail_pointer(chunk->skb);

	return target;
}

1085
/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
Linus Torvalds's avatar
Linus Torvalds committed
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
struct sctp_chunk *sctp_make_abort_violation(
	const struct sctp_association *asoc,
	const struct sctp_chunk *chunk,
	const __u8   *payload,
	const size_t paylen)
{
	struct sctp_chunk  *retval;
	struct sctp_paramhdr phdr;

	retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
1096
					+ sizeof(sctp_paramhdr_t));
Linus Torvalds's avatar
Linus Torvalds committed
1097
1098
1099
	if (!retval)
		goto end;

1100
1101
	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
					+ sizeof(sctp_paramhdr_t));
Linus Torvalds's avatar
Linus Torvalds committed
1102
1103
1104

	phdr.type = htons(chunk->chunk_hdr->type);
	phdr.length = chunk->chunk_hdr->length;
1105
1106
	sctp_addto_chunk(retval, paylen, payload);
	sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
Linus Torvalds's avatar
Linus Torvalds committed
1107
1108
1109
1110
1111

end:
	return retval;
}

1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
struct sctp_chunk *sctp_make_violation_paramlen(
	const struct sctp_association *asoc,
	const struct sctp_chunk *chunk,
	struct sctp_paramhdr *param)
{
	struct sctp_chunk *retval;
	static const char error[] = "The following parameter had invalid length:";
	size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
				sizeof(sctp_paramhdr_t);

	retval = sctp_make_abort(asoc, chunk, payload_len);
	if (!retval)
		goto nodata;

	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
			sizeof(error) + sizeof(sctp_paramhdr_t));
	sctp_addto_chunk(retval, sizeof(error), error);
	sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);

nodata:
	return retval;
}

1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
struct sctp_chunk *sctp_make_violation_max_retrans(
	const struct sctp_association *asoc,
	const struct sctp_chunk *chunk)
{
	struct sctp_chunk *retval;
	static const char error[] = "Association exceeded its max_retans count";
	size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t);

	retval = sctp_make_abort(asoc, chunk, payload_len);
	if (!retval)
		goto nodata;

	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
	sctp_addto_chunk(retval, sizeof(error), error);

nodata:
	return retval;
}

Linus Torvalds's avatar
Linus Torvalds committed
1154
1155
/* Make a HEARTBEAT chunk.  */
struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1156
				  const struct sctp_transport *transport)
Linus Torvalds's avatar
Linus Torvalds committed
1157
{
1158
1159
1160
	struct sctp_chunk *retval;
	sctp_sender_hb_info_t hbinfo;

1161
1162
	retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
				   sizeof(hbinfo), GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
1163
1164
1165
1166

	if (!retval)
		goto nodata;

1167
1168
1169
1170
1171
1172
	hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
	hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
	hbinfo.daddr = transport->ipaddr;
	hbinfo.sent_at = jiffies;
	hbinfo.hb_nonce = transport->hb_nonce;

Linus Torvalds's avatar
Linus Torvalds committed
1173
1174
1175
1176
	/* Cast away the 'const', as this is just telling the chunk
	 * what transport it belongs to.
	 */
	retval->transport = (struct sctp_transport *) transport;
1177
1178
	retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
						&hbinfo);
Linus Torvalds's avatar
Linus Torvalds committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189

nodata:
	return retval;
}

struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
				      const struct sctp_chunk *chunk,
				      const void *payload, const size_t paylen)
{
	struct sctp_chunk *retval;

1190
1191
	retval  = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen,
				    GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
	if (!retval)
		goto nodata;

	retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, * etc.) to the same destination transport
	 * address from which it * received the DATA or control chunk
	 * to which it is replying.
	 *
	 * [HBACK back to where the HEARTBEAT came from.]
	 */
	if (chunk)
		retval->transport = chunk->transport;

nodata:
	return retval;
}

/* Create an Operation Error chunk with the specified space reserved.
 * This routine can be used for containing multiple causes in the chunk.
 */
static struct sctp_chunk *sctp_make_op_error_space(
	const struct sctp_association *asoc,
	const struct sctp_chunk *chunk,
	size_t size)
{
	struct sctp_chunk *retval;

1223
	retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
1224
				   sizeof(sctp_errhdr_t) + size, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
	if (!retval)
		goto nodata;

	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
	 *
	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
	 * HEARTBEAT ACK, etc.) to the same destination transport
	 * address from which it received the DATA or control chunk
	 * to which it is replying.
	 *
	 */
	if (chunk)
		retval->transport = chunk->transport;

nodata:
	return retval;
}

1243
1244
1245
1246
/* Create an Operation Error chunk of a fixed size,
 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
 * This is a helper function to allocate an error chunk for
 * for those invalid parameter codes in which we may not want
1247
 * to report all the errors, if the incoming chunk is large
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
 */
static inline struct sctp_chunk *sctp_make_op_error_fixed(
	const struct sctp_association *asoc,
	const struct sctp_chunk *chunk)
{
	size_t size = asoc ? asoc->pathmtu : 0;

	if (!size)
		size = SCTP_DEFAULT_MAXSEGMENT;

	return sctp_make_op_error_space(asoc, chunk, size);
}

Linus Torvalds's avatar
Linus Torvalds committed
1261
1262
1263
/* Create an Operation Error chunk.  */
struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
				 const struct sctp_chunk *chunk,
1264
				 __be16 cause_code, const void *payload,
1265
				 size_t paylen, size_t reserve_tail)
Linus Torvalds's avatar
Linus Torvalds committed
1266
1267
1268
{
	struct sctp_chunk *retval;

1269
	retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
Linus Torvalds's avatar
Linus Torvalds committed
1270
1271
1272
	if (!retval)
		goto nodata;

1273
	sctp_init_cause(retval, cause_code, paylen + reserve_tail);
1274
	sctp_addto_chunk(retval, paylen, payload);
1275
1276
	if (reserve_tail)
		sctp_addto_param(retval, reserve_tail, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1277
1278
1279
1280
1281

nodata:
	return retval;
}

1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
{
	struct sctp_chunk *retval;
	struct sctp_hmac *hmac_desc;
	struct sctp_authhdr auth_hdr;
	__u8 *hmac;

	/* Get the first hmac that the peer told us to use */
	hmac_desc = sctp_auth_asoc_get_hmac(asoc);
	if (unlikely(!hmac_desc))
		return NULL;

1294
	retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
1295
1296
			hmac_desc->hmac_len + sizeof(sctp_authhdr_t),
			GFP_ATOMIC);
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
	if (!retval)
		return NULL;

	auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
	auth_hdr.shkey_id = htons(asoc->active_key_id);

	retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
						&auth_hdr);

	hmac = skb_put(retval->skb, hmac_desc->hmac_len);
	memset(hmac, 0, hmac_desc->hmac_len);

	/* Adjust the chunk header to include the empty MAC */
	retval->chunk_hdr->length =
		htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
	retval->chunk_end = skb_tail_pointer(retval->skb);

	return retval;
}


Linus Torvalds's avatar
Linus Torvalds committed
1318
1319
1320
1321
1322
1323
/********************************************************************
 * 2nd Level Abstractions
 ********************************************************************/

/* Turn an skb into a chunk.
 * FIXME: Eventually move the structure directly inside the skb->cb[].
1324
1325
1326
1327
1328
1329
1330
 *
 * sctpimpguide-05.txt Section 2.8.2
 * M1) Each time a new DATA chunk is transmitted
 * set the 'TSN.Missing.Report' count for that TSN to 0. The
 * 'TSN.Missing.Report' count will be used to determine missing chunks
 * and when to fast retransmit.
 *
Linus Torvalds's avatar
Linus Torvalds committed
1331
1332
1333
 */
struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
			    const struct sctp_association *asoc,
1334
			    struct sock *sk, gfp_t gfp)
Linus Torvalds's avatar
Linus Torvalds committed
1335
1336
1337
{
	struct sctp_chunk *retval;

1338
	retval = kmem_cache_zalloc(sctp_chunk_cachep, gfp);
Linus Torvalds's avatar
Linus Torvalds committed
1339
1340
1341

	if (!retval)
		goto nodata;
1342
1343
	if (!sk)
		pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
Linus Torvalds's avatar
Linus Torvalds committed
1344

1345
	INIT_LIST_HEAD(&retval->list);
Linus Torvalds's avatar
Linus Torvalds committed
1346
1347
1348
1349
	retval->skb		= skb;
	retval->asoc		= (struct sctp_association *)asoc;
	retval->singleton	= 1;

1350
	retval->fast_retransmit = SCTP_CAN_FRTX;
Linus Torvalds's avatar
Linus Torvalds committed
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365

	/* Polish the bead hole.  */
	INIT_LIST_HEAD(&retval->transmitted_list);
	INIT_LIST_HEAD(&retval->frag_list);
	SCTP_DBG_OBJCNT_INC(chunk);
	atomic_set(&retval->refcnt, 1);

nodata:
	return retval;
}

/* Set chunk->source and dest based on the IP header in chunk->skb.  */
void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
		     union sctp_addr *dest)
{
1366
	memcpy(&chunk->source, src, sizeof(union sctp_addr));
1367
	memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
Linus Torvalds's avatar
Linus Torvalds committed
1368
1369
1370
1371
1372
1373
1374
}

/* Extract the source address from a chunk.  */
const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
{
	/* If we have a known transport, use that.  */
	if (chunk->transport) {
1375
		return &chunk->transport->ipaddr;
Linus Torvalds's avatar
Linus Torvalds committed
1376
1377
	} else {
		/* Otherwise, extract it from the IP header.  */
1378
		return &chunk->source;
Linus Torvalds's avatar
Linus Torvalds committed
1379
1380
1381
1382
1383
1384
	}
}

/* Create a new chunk, setting the type and flags headers from the
 * arguments, reserving enough space for a 'paylen' byte payload.
 */
1385
static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
1386
1387
					    __u8 type, __u8 flags, int paylen,
					    gfp_t gfp)
Linus Torvalds's avatar
Linus Torvalds committed
1388
1389
1390
1391
1392
1393
1394
{
	struct sctp_chunk *retval;
	sctp_chunkhdr_t *chunk_hdr;
	struct sk_buff *skb;
	struct sock *sk;

	/* No need to allocate LL here, as this is only a chunk. */
1395
	skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen), gfp);
Linus Torvalds's avatar
Linus Torvalds committed
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
	if (!skb)
		goto nodata;

	/* Make room for the chunk header.  */
	chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
	chunk_hdr->type	  = type;
	chunk_hdr->flags  = flags;
	chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));

	sk = asoc ? asoc->base.sk : NULL;
1406
	retval = sctp_chunkify(skb, asoc, sk, gfp);
Linus Torvalds's avatar
Linus Torvalds committed
1407
1408
1409
1410
1411
1412
1413
1414
	if (!retval) {
		kfree_skb(skb);
		goto nodata;
	}

	retval->chunk_hdr = chunk_hdr;
	retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);

1415
1416
1417
1418
	/* Determine if the chunk needs to be authenticated */
	if (sctp_auth_send_cid(type, asoc))
		retval->auth = 1;

Linus Torvalds's avatar
Linus Torvalds committed
1419
1420
1421
1422
1423
	return retval;
nodata:
	return NULL;
}

1424
static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
1425
					 __u8 flags, int paylen, gfp_t gfp)
1426
{
1427
	return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen, gfp);
1428
1429
1430
}

static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
1431
1432
					    __u8 type, __u8 flags, int paylen,
					    gfp_t gfp)
1433
{
1434
	struct sctp_chunk *chunk;
1435

1436
	chunk = _sctp_make_chunk(asoc, type, flags, paylen, gfp);
1437
1438
1439
1440
1441
	if (chunk)
		sctp_control_set_owner_w(chunk);

	return chunk;
}
Linus Torvalds's avatar
Linus Torvalds committed
1442
1443
1444
1445

/* Release the memory occupied by a chunk.  */
static void sctp_chunk_destroy(struct sctp_chunk *chunk)
{
1446
1447
1448
	BUG_ON(!list_empty(&chunk->list));
	list_del_init(&chunk->transmitted_list);

1449
1450
	consume_skb(chunk->skb);
	consume_skb(chunk->auth_chunk);
Linus Torvalds's avatar
Linus Torvalds committed
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486

	SCTP_DBG_OBJCNT_DEC(chunk);
	kmem_cache_free(sctp_chunk_cachep, chunk);
}

/* Possibly, free the chunk.  */
void sctp_chunk_free(struct sctp_chunk *chunk)
{
	/* Release our reference on the message tracker. */
	if (chunk->msg)
		sctp_datamsg_put(chunk->msg);

	sctp_chunk_put(chunk);
}

/* Grab a reference to the chunk. */
void sctp_chunk_hold(struct sctp_chunk *ch)
{
	atomic_inc(&ch->refcnt);
}

/* Release a reference to the chunk. */
void sctp_chunk_put(struct sctp_chunk *ch)
{
	if (atomic_dec_and_test(&ch->refcnt))
		sctp_chunk_destroy(ch);
}

/* Append bytes to the end of a chunk.  Will panic if chunk is not big
 * enough.
 */
void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
{
	void *target;
	void *padding;
	int chunklen = ntohs(chunk->chunk_hdr->length);
1487
	int padlen = WORD_ROUND(chunklen) - chunklen;
Linus Torvalds's avatar
Linus Torvalds committed
1488
1489
1490
1491
1492
1493
1494
1495
1496

	padding = skb_put(chunk->skb, padlen);
	target = skb_put(chunk->skb, len);

	memset(padding, 0, padlen);
	memcpy(target, data, len);

	/* Adjust the chunk length field.  */
	chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1497
	chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds's avatar
Linus Torvalds committed
1498
1499
1500
1501

	return target;
}

1502
1503
1504
/* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
 * space in the chunk
 */
1505
1506
static void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
				    int len, const void *data)
1507
{
1508
	if (skb_tailroom(chunk->skb) >= len)
1509
1510
1511
1512
1513
		return sctp_addto_chunk(chunk, len, data);
	else
		return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
1514
1515
1516
1517
/* Append bytes from user space to the end of a chunk.  Will panic if
 * chunk is not big enough.
 * Returns a kernel err value.
 */
1518
1519
int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
			  struct iov_iter *from)
Linus Torvalds's avatar
Linus Torvalds committed
1520
{
1521
1522
	void *target;
	ssize_t copied;
Linus Torvalds's avatar
Linus Torvalds committed
1523
1524
1525
1526
1527

	/* Make room in chunk for data.  */
	target = skb_put(chunk->skb, len);

	/* Copy data (whole iovec) into chunk */
1528
1529
1530
	copied = copy_from_iter(target, len, from);
	if (copied != len)
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
1531
1532
1533
1534

	/* Adjust the chunk length field.  */
	chunk->chunk_hdr->length =
		htons(ntohs(chunk->chunk_hdr->length) + len);
1535
	chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds's avatar
Linus Torvalds committed
1536

1537
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1538
1539
1540
1541
1542
1543
1544
}

/* Helper function to assign a TSN if needed.  This assumes that both
 * the data_hdr and association have already been assigned.
 */
void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
{
1545
1546
1547
	struct sctp_datamsg *msg;
	struct sctp_chunk *lchunk;
	struct sctp_stream *stream;
Linus Torvalds's avatar
Linus Torvalds committed
1548
1549
1550
1551
1552
1553
	__u16 ssn;
	__u16 sid;

	if (chunk->has_ssn)
		return;

1554
1555
1556
	/* All fragments will be on the same stream */
	sid = ntohs(chunk->subh.data_hdr->stream);
	stream = &chunk->asoc->ssnmap->out;
Linus Torvalds's avatar
Linus Torvalds committed
1557

1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
	/* Now assign the sequence number to the entire message.
	 * All fragments must have the same stream sequence number.
	 */
	msg = chunk->msg;
	list_for_each_entry(lchunk, &msg->chunks, frag_list) {
		if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
			ssn = 0;
		} else {
			if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
				ssn = sctp_ssn_next(stream, sid);
			else
				ssn = sctp_ssn_peek(stream, sid);
		}

		lchunk->subh.data_hdr->ssn = htons(ssn);
		lchunk->has_ssn = 1;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
}

/* Helper function to assign a TSN if needed.  This assumes that both
 * the data_hdr and association have already been assigned.
 */
void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
{
	if (!chunk->has_tsn) {
		/* This is the last possible instant to
		 * assign a TSN.
		 */
		chunk->subh.data_hdr->tsn =
			htonl(sctp_association_get_next_tsn(chunk->asoc));
		chunk->has_tsn = 1;
	}
}

/* Create a CLOSED association to use with an incoming packet.  */
struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
1594
					struct sctp_chunk *chunk,
1595
					gfp_t gfp)
Linus Torvalds's avatar
Linus Torvalds committed
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
{
	struct sctp_association *asoc;
	struct sk_buff *skb;
	sctp_scope_t scope;
	struct sctp_af *af;

	/* Create the bare association.  */
	scope = sctp_scope(sctp_source(chunk));
	asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
	if (!asoc)
		goto nodata;
	asoc->temp = 1;
	skb = chunk->skb;
	/* Create an entry for the source address of the packet.  */
1610
	af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
Linus Torvalds's avatar
Linus Torvalds committed
1611
1612
	if (unlikely(!af))
		goto fail;
1613
	af->from_skb(&asoc->c.peer_addr, skb, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
nodata:
	return asoc;

fail:
	sctp_association_free(asoc);
	return NULL;
}

/* Build a cookie representing asoc.
 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
 */
static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
				      const struct sctp_association *asoc,
				      const struct sctp_chunk *init_chunk,
				      int *cookie_len,
				      const __u8 *raw_addrs, int addrs_len)
{
	sctp_cookie_param_t *retval;
	struct sctp_signed_cookie *cookie;
	int headersize, bodysize;