sm_make_chunk.c 102.3 KB
Newer Older
1
/* SCTP kernel implementation
L
Linus Torvalds 已提交
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
L
Linus Torvalds 已提交
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;
L
Linus Torvalds 已提交
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
L
Linus Torvalds 已提交
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/>.
L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
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

L
Linus Torvalds 已提交
48 49 50 51 52 53
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/net.h>
#include <linux/inet.h>
C
Christian Borntraeger 已提交
54
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
55
#include <linux/crypto.h>
56
#include <linux/slab.h>
L
Linus Torvalds 已提交
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 65 66 67 68 69
static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
					    __u8 type, __u8 flags, int paylen);
static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
					 __u8 flags, int paylen);
static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
					   __u8 type, __u8 flags, int paylen);
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77
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,
A
Al Viro 已提交
78
			      gfp_t gfp);
79 80
static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
			      const void *data);
81 82
static void  *sctp_addto_chunk_fixed(struct sctp_chunk *, int len,
				     const void *data);
L
Linus Torvalds 已提交
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/* 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;
}

L
Linus Torvalds 已提交
106 107 108 109 110 111
/* What was the inbound interface for this chunk? */
int sctp_chunk_iif(const struct sctp_chunk *chunk)
{
	struct sctp_af *af;
	int iif = 0;

112
	af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
	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,
126
	cpu_to_be16(sizeof(struct sctp_paramhdr)),
L
Linus Torvalds 已提交
127 128 129
};
static const struct sctp_paramhdr prsctp_param = {
	SCTP_PARAM_FWD_TSN_SUPPORT,
130
	cpu_to_be16(sizeof(struct sctp_paramhdr)),
L
Linus Torvalds 已提交
131 132
};

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

143
	/* Cause code constants are now defined in network order.  */
L
Linus Torvalds 已提交
144 145 146
	err.cause = cause_code;
	len = sizeof(sctp_errhdr_t) + paylen;
	err.length  = htons(len);
147
	chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
L
Linus Torvalds 已提交
148 149
}

150 151 152 153 154
/* 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
 */
155
static int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
156 157 158 159 160 161 162 163 164 165
		      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);

166
	if (skb_tailroom(chunk->skb) < len)
167 168 169 170 171 172
		return -ENOSPC;
	chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk,
						     sizeof(sctp_errhdr_t),
						     &err);
	return 0;
}
L
Linus Torvalds 已提交
173 174 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
/* 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,
A
Al Viro 已提交
219
			     gfp_t gfp, int vparam_len)
L
Linus Torvalds 已提交
220
{
221
	struct net *net = sock_net(asoc->base.sk);
222
	struct sctp_endpoint *ep = asoc->ep;
L
Linus Torvalds 已提交
223 224 225 226 227 228 229
	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;
A
Al Viro 已提交
230
	__be16 types[2];
231
	sctp_adaptation_ind_param_t aiparam;
232 233 234
	sctp_supported_ext_param_t ext_param;
	int num_ext = 0;
	__u8 extensions[3];
235 236
	sctp_paramhdr_t *auth_chunks = NULL,
			*auth_hmacs = NULL;
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

	/* 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);

258 259
	chunksize = sizeof(init) + addrs_len;
	chunksize += WORD_ROUND(SCTP_SAT_LEN(num_types));
L
Linus Torvalds 已提交
260
	chunksize += sizeof(ecap_param);
261

262
	if (net->sctp.prsctp_enable)
263 264
		chunksize += sizeof(prsctp_param);

265 266 267 268 269
	/* 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.
	 */
270
	if (net->sctp.addip_enable) {
271 272 273 274 275
		extensions[num_ext] = SCTP_CID_ASCONF;
		extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
		num_ext += 2;
	}

276 277 278
	if (sp->adaptation_ind)
		chunksize += sizeof(aiparam);

L
Linus Torvalds 已提交
279 280
	chunksize += vparam_len;

281
	/* Account for AUTH related parameters */
282
	if (ep->auth_enable) {
283 284 285 286 287 288
		/* 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)
289
			chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
290 291 292 293 294 295
		else
			auth_hmacs = NULL;

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

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

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

L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317 318 319 320
	/* 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.]
	 */

321
	retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize);
L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	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);
343

J
Joe Perches 已提交
344
	/* Add the supported extensions parameter.  Be nice and add this
345 346 347 348 349 350 351 352
	 * 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);
353
		sctp_addto_param(retval, num_ext, extensions);
354 355
	}

356
	if (net->sctp.prsctp_enable)
L
Linus Torvalds 已提交
357
		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
358

359 360 361 362 363 364
	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);
	}
365

366
	/* Add SCTP-AUTH chunks to the parameter list */
367
	if (ep->auth_enable) {
368 369 370 371 372 373 374 375 376
		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);
	}
L
Linus Torvalds 已提交
377
nodata:
J
Jesper Juhl 已提交
378
	kfree(addrs.v);
L
Linus Torvalds 已提交
379 380 381 382 383
	return retval;
}

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

	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.
	 */
424
	sp = sctp_sk(asoc->base.sk);
L
Linus Torvalds 已提交
425 426
	chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;

427
	/* Tell peer that we'll do ECN only if peer advertised such cap.  */
L
Linus Torvalds 已提交
428 429 430
	if (asoc->peer.ecn_capable)
		chunksize += sizeof(ecap_param);

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

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

440 441
	if (sp->adaptation_ind)
		chunksize += sizeof(aiparam);
L
Linus Torvalds 已提交
442

443 444 445 446 447 448
	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)
449
			chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
450 451 452 453 454
		else
			auth_hmacs = NULL;

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

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

463
	if (num_ext)
464 465
		chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
					num_ext);
466

L
Linus Torvalds 已提交
467
	/* Now allocate and fill out the chunk.  */
468
	retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
L
Linus Torvalds 已提交
469 470 471
	if (!retval)
		goto nomem_chunk;

472 473 474 475 476 477 478 479
	/* 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.]
L
Linus Torvalds 已提交
480 481
	 */
	retval->transport = chunk->transport;
482

L
Linus Torvalds 已提交
483 484 485 486 487 488
	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);
489 490 491 492 493 494
	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);
495
		sctp_addto_param(retval, num_ext, extensions);
496
	}
L
Linus Torvalds 已提交
497 498 499
	if (asoc->peer.prsctp_capable)
		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);

500 501 502 503 504 505
	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);
	}
L
Linus Torvalds 已提交
506

507 508 509 510 511 512 513 514 515 516 517
	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);
	}

L
Linus Torvalds 已提交
518 519 520 521 522 523
	/* We need to remove the const qualifier at this point.  */
	retval->asoc = (struct sctp_association *) asoc;

nomem_chunk:
	kfree(cookie);
nomem_cookie:
J
Jesper Juhl 已提交
524
	kfree(addrs.v);
L
Linus Torvalds 已提交
525 526 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
	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.  */
573
	retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
L
Linus Torvalds 已提交
574 575 576 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
	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;

618
	retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
L
Linus Torvalds 已提交
619 620 621 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

	/* 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);
666 667
	retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
				   sizeof(sctp_cwrhdr_t));
L
Linus Torvalds 已提交
668 669 670 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

	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);
700 701
	retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
				   sizeof(sctp_ecnehdr_t));
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	if (!retval)
		goto nodata;
	retval->subh.ecne_hdr =
		sctp_addto_chunk(retval, sizeof(ecne), &ecne);

nodata:
	return retval;
}

/* 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,
				       int data_len, __u8 flags, __u16 ssn)
{
	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.  */
730
	if (sinfo->sinfo_flags & SCTP_UNORDERED) {
L
Linus Torvalds 已提交
731 732 733 734 735 736
		flags |= SCTP_DATA_UNORDERED;
		dp.ssn = 0;
	} else
		dp.ssn = htons(ssn);

	chunk_len = sizeof(dp) + data_len;
737
	retval = sctp_make_data(asoc, flags, chunk_len);
L
Linus Torvalds 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	if (!retval)
		goto nodata;

	retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
	memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));

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;
759
	struct sctp_association *aptr = (struct sctp_association *)asoc;
L
Linus Torvalds 已提交
760
	struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
761
	struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
762
	struct sctp_transport *trans;
L
Linus Torvalds 已提交
763

764
	memset(gabs, 0, sizeof(gabs));
L
Linus Torvalds 已提交
765
	ctsn = sctp_tsnmap_get_ctsn(map);
766 767

	pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
L
Linus Torvalds 已提交
768 769

	/* How much room is needed in the chunk? */
770
	num_gabs = sctp_tsnmap_num_gabs(map, gabs);
L
Linus Torvalds 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783
	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.  */
784
	retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len);
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 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
	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,
826
				 gabs);
L
Linus Torvalds 已提交
827 828

	/* Add the duplicate TSN information.  */
829 830
	if (num_dup_tsns) {
		aptr->stats.idupchunks += num_dup_tsns;
L
Linus Torvalds 已提交
831 832
		sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
				 sctp_tsnmap_get_dups(map));
833
	}
834 835 836 837 838 839 840 841 842 843 844 845 846 847
	/* 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;
	}
L
Linus Torvalds 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
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);

863 864
	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
				   sizeof(sctp_shutdownhdr_t));
L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
	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;

882
	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

	/* 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;

906 907 908
	/* Set the T-bit if we have no association (vtag will be
	 * reflected)
	 */
L
Linus Torvalds 已提交
909 910
	flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;

911
	retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925

	/* 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;

926
	return retval;
L
Linus Torvalds 已提交
927 928 929
}

/* Create an ABORT.  Note that we set the T bit if we have no
930
 * association, except when responding to an INIT (sctpimpguide 2.41).
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938
 */
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;

939 940 941 942 943 944 945 946 947 948
	/* 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;
	}
L
Linus Torvalds 已提交
949

950
	retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint);
L
Linus Torvalds 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

	/* 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;
A
Al Viro 已提交
973
	__be32 payload;
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982

	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);
983 984
	sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
	sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

	/* 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,
A
Al Viro 已提交
1004
					struct msghdr *msg,
1005
					size_t paylen)
L
Linus Torvalds 已提交
1006 1007
{
	struct sctp_chunk *retval;
1008 1009
	void *payload = NULL;
	int err;
L
Linus Torvalds 已提交
1010

1011
	retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016
	if (!retval)
		goto err_chunk;

	if (paylen) {
		/* Put the msg_iov together into payload.  */
1017
		payload = kmalloc(paylen, GFP_KERNEL);
L
Linus Torvalds 已提交
1018 1019
		if (!payload)
			goto err_payload;
1020

A
Al Viro 已提交
1021
		err = memcpy_from_msg(payload, msg, paylen);
1022 1023
		if (err < 0)
			goto err_copy;
L
Linus Torvalds 已提交
1024 1025
	}

1026 1027
	sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
	sctp_addto_chunk(retval, paylen, payload);
L
Linus Torvalds 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

	if (paylen)
		kfree(payload);

	return retval;

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

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
/* 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);

1054 1055 1056 1057
	if (data)
		memcpy(target, data, len);
	else
		memset(target, 0, len);
1058 1059 1060 1061 1062 1063 1064 1065

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

	return target;
}

1066
/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
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
1077
					+ sizeof(sctp_paramhdr_t));
L
Linus Torvalds 已提交
1078 1079 1080
	if (!retval)
		goto end;

1081 1082
	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
					+ sizeof(sctp_paramhdr_t));
L
Linus Torvalds 已提交
1083 1084 1085

	phdr.type = htons(chunk->chunk_hdr->type);
	phdr.length = chunk->chunk_hdr->length;
1086 1087
	sctp_addto_chunk(retval, paylen, payload);
	sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092

end:
	return retval;
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
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;
}

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_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;
}

L
Linus Torvalds 已提交
1135 1136
/* Make a HEARTBEAT chunk.  */
struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1137
				  const struct sctp_transport *transport)
L
Linus Torvalds 已提交
1138
{
1139 1140 1141
	struct sctp_chunk *retval;
	sctp_sender_hb_info_t hbinfo;

1142
	retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0, sizeof(hbinfo));
L
Linus Torvalds 已提交
1143 1144 1145 1146

	if (!retval)
		goto nodata;

1147 1148 1149 1150 1151 1152
	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;

L
Linus Torvalds 已提交
1153 1154 1155 1156
	/* Cast away the 'const', as this is just telling the chunk
	 * what transport it belongs to.
	 */
	retval->transport = (struct sctp_transport *) transport;
1157 1158
	retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
						&hbinfo);
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

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;

1170
	retval  = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
L
Linus Torvalds 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	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;

1202 1203
	retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
				   sizeof(sctp_errhdr_t) + size);
L
Linus Torvalds 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	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;
}

1222 1223 1224 1225
/* 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
1226
 * to report all the errors, if the incoming chunk is large
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
 */
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);
}

L
Linus Torvalds 已提交
1240 1241 1242
/* Create an Operation Error chunk.  */
struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
				 const struct sctp_chunk *chunk,
1243
				 __be16 cause_code, const void *payload,
1244
				 size_t paylen, size_t reserve_tail)
L
Linus Torvalds 已提交
1245 1246 1247
{
	struct sctp_chunk *retval;

1248
	retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
L
Linus Torvalds 已提交
1249 1250 1251
	if (!retval)
		goto nodata;

1252
	sctp_init_cause(retval, cause_code, paylen + reserve_tail);
1253
	sctp_addto_chunk(retval, paylen, payload);
1254 1255
	if (reserve_tail)
		sctp_addto_param(retval, reserve_tail, NULL);
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260

nodata:
	return retval;
}

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
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;

1273
	retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
			hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
	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;
}


L
Linus Torvalds 已提交
1296 1297 1298 1299 1300 1301
/********************************************************************
 * 2nd Level Abstractions
 ********************************************************************/

/* Turn an skb into a chunk.
 * FIXME: Eventually move the structure directly inside the skb->cb[].
1302 1303 1304 1305 1306 1307 1308
 *
 * 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.
 *
L
Linus Torvalds 已提交
1309 1310 1311 1312 1313 1314 1315
 */
struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
			    const struct sctp_association *asoc,
			    struct sock *sk)
{
	struct sctp_chunk *retval;

1316
	retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
L
Linus Torvalds 已提交
1317 1318 1319

	if (!retval)
		goto nodata;
1320 1321
	if (!sk)
		pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
L
Linus Torvalds 已提交
1322

1323
	INIT_LIST_HEAD(&retval->list);
L
Linus Torvalds 已提交
1324 1325 1326 1327
	retval->skb		= skb;
	retval->asoc		= (struct sctp_association *)asoc;
	retval->singleton	= 1;

1328
	retval->fast_retransmit = SCTP_CAN_FRTX;
L
Linus Torvalds 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343

	/* 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)
{
1344
	memcpy(&chunk->source, src, sizeof(union sctp_addr));
1345
	memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350 1351 1352
}

/* 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) {
1353
		return &chunk->transport->ipaddr;
L
Linus Torvalds 已提交
1354 1355
	} else {
		/* Otherwise, extract it from the IP header.  */
1356
		return &chunk->source;
L
Linus Torvalds 已提交
1357 1358 1359 1360 1361 1362
	}
}

/* Create a new chunk, setting the type and flags headers from the
 * arguments, reserving enough space for a 'paylen' byte payload.
 */
1363 1364
static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
					    __u8 type, __u8 flags, int paylen)
L
Linus Torvalds 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
{
	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. */
	skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
			GFP_ATOMIC);
	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;
	retval = sctp_chunkify(skb, asoc, sk);
	if (!retval) {
		kfree_skb(skb);
		goto nodata;
	}

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

1393 1394 1395 1396
	/* Determine if the chunk needs to be authenticated */
	if (sctp_auth_send_cid(type, asoc))
		retval->auth = 1;

L
Linus Torvalds 已提交
1397 1398 1399 1400 1401
	return retval;
nodata:
	return NULL;
}

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
					 __u8 flags, int paylen)
{
	return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen);
}

static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
					    __u8 type, __u8 flags, int paylen)
{
	struct sctp_chunk *chunk = _sctp_make_chunk(asoc, type, flags, paylen);

	if (chunk)
		sctp_control_set_owner_w(chunk);

	return chunk;
}
L
Linus Torvalds 已提交
1418 1419 1420 1421

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

1425 1426
	consume_skb(chunk->skb);
	consume_skb(chunk->auth_chunk);
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

	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);
1463
	int padlen = WORD_ROUND(chunklen) - chunklen;
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472

	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);
1473
	chunk->chunk_end = skb_tail_pointer(chunk->skb);
L
Linus Torvalds 已提交
1474 1475 1476 1477

	return target;
}

1478 1479 1480
/* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
 * space in the chunk
 */
1481 1482
static void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
				    int len, const void *data)
1483
{
1484
	if (skb_tailroom(chunk->skb) >= len)
1485 1486 1487 1488 1489
		return sctp_addto_chunk(chunk, len, data);
	else
		return NULL;
}

L
Linus Torvalds 已提交
1490 1491 1492 1493
/* Append bytes from user space to the end of a chunk.  Will panic if
 * chunk is not big enough.
 * Returns a kernel err value.
 */
1494 1495
int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
			  struct iov_iter *from)
L
Linus Torvalds 已提交
1496
{
1497 1498
	void *target;
	ssize_t copied;
L
Linus Torvalds 已提交
1499 1500 1501 1502 1503

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

	/* Copy data (whole iovec) into chunk */
1504 1505 1506
	copied = copy_from_iter(target, len, from);
	if (copied != len)
		return -EFAULT;
L
Linus Torvalds 已提交
1507 1508 1509 1510

	/* Adjust the chunk length field.  */
	chunk->chunk_hdr->length =
		htons(ntohs(chunk->chunk_hdr->length) + len);
1511
	chunk->chunk_end = skb_tail_pointer(chunk->skb);
L
Linus Torvalds 已提交
1512

1513
	return 0;
L
Linus Torvalds 已提交
1514 1515 1516 1517 1518 1519 1520
}

/* 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)
{
1521 1522 1523
	struct sctp_datamsg *msg;
	struct sctp_chunk *lchunk;
	struct sctp_stream *stream;
L
Linus Torvalds 已提交
1524 1525 1526 1527 1528 1529
	__u16 ssn;
	__u16 sid;

	if (chunk->has_ssn)
		return;

1530 1531 1532
	/* All fragments will be on the same stream */
	sid = ntohs(chunk->subh.data_hdr->stream);
	stream = &chunk->asoc->ssnmap->out;
L
Linus Torvalds 已提交
1533

1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
	/* 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;
	}
L
Linus Torvalds 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
}

/* 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,
A
Alexey Dobriyan 已提交
1570
					struct sctp_chunk *chunk,
A
Al Viro 已提交
1571
					gfp_t gfp)
L
Linus Torvalds 已提交
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
{
	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.  */
1586
	af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
L
Linus Torvalds 已提交
1587 1588
	if (unlikely(!af))
		goto fail;
1589
	af->from_skb(&asoc->c.peer_addr, skb, 1);
L
Linus Torvalds 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
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;
	struct scatterlist sg;
	int headersize, bodysize;

1612 1613 1614
	/* Header size is static data prior to the actual cookie, including
	 * any padding.
	 */
1615 1616
	headersize = sizeof(sctp_paramhdr_t) +
		     (sizeof(struct sctp_signed_cookie) -
1617
		      sizeof(struct sctp_cookie));
L
Linus Torvalds 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
	bodysize = sizeof(struct sctp_cookie)
		+ ntohs(init_chunk->chunk_hdr->length) + addrs_len;

	/* Pad out the cookie to a multiple to make the signature
	 * functions simpler to write.
	 */
	if (bodysize % SCTP_COOKIE_MULTIPLE)
		bodysize += SCTP_COOKIE_MULTIPLE
			- (bodysize % SCTP_COOKIE_MULTIPLE);
	*cookie_len = headersize + bodysize;

	/* Clear this memory since we are sending this data structure
	 * out on the network.
	 */
1632 1633 1634 1635
	retval = kzalloc(*cookie_len, GFP_ATOMIC);
	if (!retval)
		goto nodata;

L
Linus Torvalds 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
	cookie = (struct sctp_signed_cookie *) retval->body;

	/* Set up the parameter header.  */
	retval->p.type = SCTP_PARAM_STATE_COOKIE;
	retval->p.length = htons(*cookie_len);

	/* Copy the cookie part of the association itself.  */
	cookie->c = asoc->c;
	/* Save the raw address list length in the cookie. */
	cookie->c.raw_addr_list_len = addrs_len;

	/* Remember PR-SCTP capability. */
	cookie->c.prsctp_capable = asoc->peer.prsctp_capable;

1650 1651
	/* Save adaptation indication in the cookie. */
	cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
L
Linus Torvalds 已提交
1652 1653

	/* Set an expiration time for the cookie.  */
1654 1655
	cookie->c.expiration = ktime_add(asoc->cookie_life,
					 ktime_get());
L
Linus Torvalds 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664

	/* Copy the peer's init packet.  */
	memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
	       ntohs(init_chunk->chunk_hdr->length));

	/* Copy the raw local address list of the association. */
	memcpy((__u8 *)&cookie->c.peer_init[0] +
	       ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);

1665
	if (sctp_sk(ep->base.sk)->hmac) {
1666 1667
		struct hash_desc desc;

L
Linus Torvalds 已提交
1668
		/* Sign the message.  */
1669
		sg_init_one(&sg, &cookie->c, bodysize);
1670 1671
		desc.tfm = sctp_sk(ep->base.sk)->hmac;
		desc.flags = 0;
L
Linus Torvalds 已提交
1672

1673 1674
		if (crypto_hash_setkey(desc.tfm, ep->secret_key,
				       sizeof(ep->secret_key)) ||
1675 1676
		    crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
			goto free_cookie;
L
Linus Torvalds 已提交
1677 1678 1679
	}

	return retval;
1680 1681 1682 1683 1684 1685

free_cookie:
	kfree(retval);
nodata:
	*cookie_len = 0;
	return NULL;
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690 1691
}

/* Unpack the cookie from COOKIE ECHO chunk, recreating the association.  */
struct sctp_association *sctp_unpack_cookie(
	const struct sctp_endpoint *ep,
	const struct sctp_association *asoc,
A
Al Viro 已提交
1692
	struct sctp_chunk *chunk, gfp_t gfp,
L
Linus Torvalds 已提交
1693 1694 1695 1696 1697 1698
	int *error, struct sctp_chunk **errp)
{
	struct sctp_association *retval = NULL;
	struct sctp_signed_cookie *cookie;
	struct sctp_cookie *bear_cookie;
	int headersize, bodysize, fixed_size;
1699
	__u8 *digest = ep->digest;
L
Linus Torvalds 已提交
1700
	struct scatterlist sg;
1701
	unsigned int len;
L
Linus Torvalds 已提交
1702 1703
	sctp_scope_t scope;
	struct sk_buff *skb = chunk->skb;
1704
	ktime_t kt;
1705
	struct hash_desc desc;
L
Linus Torvalds 已提交
1706

1707 1708 1709 1710
	/* Header size is static data prior to the actual cookie, including
	 * any padding.
	 */
	headersize = sizeof(sctp_chunkhdr_t) +
1711
		     (sizeof(struct sctp_signed_cookie) -
1712
		      sizeof(struct sctp_cookie));
L
Linus Torvalds 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
	bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
	fixed_size = headersize + sizeof(struct sctp_cookie);

	/* Verify that the chunk looks like it even has a cookie.
	 * There must be enough room for our cookie and our peer's
	 * INIT chunk.
	 */
	len = ntohs(chunk->chunk_hdr->length);
	if (len < fixed_size + sizeof(struct sctp_chunkhdr))
		goto malformed;

	/* Verify that the cookie has been padded out. */
	if (bodysize % SCTP_COOKIE_MULTIPLE)
		goto malformed;

	/* Process the cookie.  */
	cookie = chunk->subh.cookie_hdr;
	bear_cookie = &cookie->c;

	if (!sctp_sk(ep->base.sk)->hmac)
		goto no_hmac;

	/* Check the signature.  */
1736
	sg_init_one(&sg, bear_cookie, bodysize);
1737 1738
	desc.tfm = sctp_sk(ep->base.sk)->hmac;
	desc.flags = 0;
L
Linus Torvalds 已提交
1739

A
Al Viro 已提交
1740
	memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1741 1742
	if (crypto_hash_setkey(desc.tfm, ep->secret_key,
			       sizeof(ep->secret_key)) ||
1743 1744 1745 1746
	    crypto_hash_digest(&desc, &sg, bodysize, digest)) {
		*error = -SCTP_IERROR_NOMEM;
		goto fail;
	}
L
Linus Torvalds 已提交
1747 1748

	if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1749 1750
		*error = -SCTP_IERROR_BAD_SIG;
		goto fail;
L
Linus Torvalds 已提交
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
	}

no_hmac:
	/* IG Section 2.35.2:
	 *  3) Compare the port numbers and the verification tag contained
	 *     within the COOKIE ECHO chunk to the actual port numbers and the
	 *     verification tag within the SCTP common header of the received
	 *     packet. If these values do not match the packet MUST be silently
	 *     discarded,
	 */
	if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
		*error = -SCTP_IERROR_BAD_TAG;
		goto fail;
	}

1766
	if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
L
Linus Torvalds 已提交
1767 1768 1769 1770 1771 1772 1773 1774
	    ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
		*error = -SCTP_IERROR_BAD_PORTS;
		goto fail;
	}

	/* Check to see if the cookie is stale.  If there is already
	 * an association, there is no need to check cookie's expiration
	 * for init collision case of lost COOKIE ACK.
1775 1776 1777 1778
	 * If skb has been timestamped, then use the stamp, otherwise
	 * use current time.  This introduces a small possibility that
	 * that a cookie may be considered expired, but his would only slow
	 * down the new association establishment instead of every packet.
L
Linus Torvalds 已提交
1779
	 */
1780
	if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1781
		kt = skb_get_ktime(skb);
1782
	else
1783
		kt = ktime_get();
1784

1785
	if (!asoc && ktime_before(bear_cookie->expiration, kt)) {
L
Linus Torvalds 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
		/*
		 * Section 3.3.10.3 Stale Cookie Error (3)
		 *
		 * Cause of error
		 * ---------------
		 * Stale Cookie Error:  Indicates the receipt of a valid State
		 * Cookie that has expired.
		 */
		len = ntohs(chunk->chunk_hdr->length);
		*errp = sctp_make_op_error_space(asoc, chunk, len);
		if (*errp) {
1797
			suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
1798
			__be32 n = htonl(usecs);
L
Linus Torvalds 已提交
1799 1800

			sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1801 1802
					sizeof(n));
			sctp_addto_chunk(*errp, sizeof(n), &n);
L
Linus Torvalds 已提交
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
			*error = -SCTP_IERROR_STALE_COOKIE;
		} else
			*error = -SCTP_IERROR_NOMEM;

		goto fail;
	}

	/* Make a new base association.  */
	scope = sctp_scope(sctp_source(chunk));
	retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
	if (!retval) {
		*error = -SCTP_IERROR_NOMEM;
		goto fail;
	}

	/* Set up our peer's port number.  */
	retval->peer.port = ntohs(chunk->sctp_hdr->source);

	/* Populate the association from the cookie.  */
	memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));

	if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
						 GFP_ATOMIC) < 0) {
		*error = -SCTP_IERROR_NOMEM;
		goto fail;
	}

	/* Also, add the destination address. */
	if (list_empty(&retval->base.bind_addr.address_list)) {
1832 1833
		sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
				SCTP_ADDR_SRC, GFP_ATOMIC);
L
Linus Torvalds 已提交
1834 1835 1836 1837 1838 1839 1840
	}

	retval->next_tsn = retval->c.initial_tsn;
	retval->ctsn_ack_point = retval->next_tsn - 1;
	retval->addip_serial = retval->c.initial_tsn;
	retval->adv_peer_ack_point = retval->ctsn_ack_point;
	retval->peer.prsctp_capable = retval->c.prsctp_capable;
1841
	retval->peer.adaptation_ind = retval->c.adaptation_ind;
L
Linus Torvalds 已提交
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864

	/* The INIT stuff will be done by the side effects.  */
	return retval;

fail:
	if (retval)
		sctp_association_free(retval);

	return NULL;

malformed:
	/* Yikes!  The packet is either corrupt or deliberately
	 * malformed.
	 */
	*error = -SCTP_IERROR_MALFORMED;
	goto fail;
}

/********************************************************************
 * 3rd Level Abstractions
 ********************************************************************/

struct __sctp_missing {
A
Al Viro 已提交
1865 1866
	__be32 num_missing;
	__be16 type;
E
Eric Dumazet 已提交
1867
}  __packed;
L
Linus Torvalds 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

/*
 * Report a missing mandatory parameter.
 */
static int sctp_process_missing_param(const struct sctp_association *asoc,
				      sctp_param_t paramtype,
				      struct sctp_chunk *chunk,
				      struct sctp_chunk **errp)
{
	struct __sctp_missing report;
	__u16 len;

	len = WORD_ROUND(sizeof(report));

	/* Make an ERROR chunk, preparing enough room for
	 * returning multiple unknown parameters.
	 */
	if (!*errp)
		*errp = sctp_make_op_error_space(asoc, chunk, len);

	if (*errp) {
		report.num_missing = htonl(1);
		report.type = paramtype;
1891
		sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1892 1893
				sizeof(report));
		sctp_addto_chunk(*errp, sizeof(report), &report);
L
Linus Torvalds 已提交
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
	}

	/* Stop processing this chunk. */
	return 0;
}

/* Report an Invalid Mandatory Parameter.  */
static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
				      struct sctp_chunk *chunk,
				      struct sctp_chunk **errp)
{
	/* Invalid Mandatory Parameter Error has no payload. */

	if (!*errp)
		*errp = sctp_make_op_error_space(asoc, chunk, 0);

	if (*errp)
1911
		sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
L
Linus Torvalds 已提交
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921

	/* Stop processing this chunk. */
	return 0;
}

static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
					struct sctp_paramhdr *param,
					const struct sctp_chunk *chunk,
					struct sctp_chunk **errp)
{
1922 1923 1924 1925 1926 1927
	/* This is a fatal error.  Any accumulated non-fatal errors are
	 * not reported.
	 */
	if (*errp)
		sctp_chunk_free(*errp);

L
Linus Torvalds 已提交
1928
	/* Create an error chunk and fill it in with our payload. */
1929
	*errp = sctp_make_violation_paramlen(asoc, chunk, param);
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944

	return 0;
}


/* Do not attempt to handle the HOST_NAME parm.  However, do
 * send back an indicator to the peer.
 */
static int sctp_process_hn_param(const struct sctp_association *asoc,
				 union sctp_params param,
				 struct sctp_chunk *chunk,
				 struct sctp_chunk **errp)
{
	__u16 len = ntohs(param.p->length);

1945 1946 1947 1948 1949 1950 1951 1952 1953
	/* Processing of the HOST_NAME parameter will generate an
	 * ABORT.  If we've accumulated any non-fatal errors, they
	 * would be unrecognized parameters and we should not include
	 * them in the ABORT.
	 */
	if (*errp)
		sctp_chunk_free(*errp);

	*errp = sctp_make_op_error_space(asoc, chunk, len);
L
Linus Torvalds 已提交
1954

1955 1956 1957 1958
	if (*errp) {
		sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
		sctp_addto_chunk(*errp, len, param.v);
	}
L
Linus Torvalds 已提交
1959 1960 1961 1962 1963

	/* Stop processing this chunk. */
	return 0;
}

1964
static int sctp_verify_ext_param(struct net *net, union sctp_params param)
1965 1966 1967 1968 1969 1970 1971 1972
{
	__u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
	int have_auth = 0;
	int have_asconf = 0;
	int i;

	for (i = 0; i < num_ext; i++) {
		switch (param.ext->chunks[i]) {
1973 1974 1975 1976 1977 1978 1979
		case SCTP_CID_AUTH:
			have_auth = 1;
			break;
		case SCTP_CID_ASCONF:
		case SCTP_CID_ASCONF_ACK:
			have_asconf = 1;
			break;
1980 1981 1982 1983 1984 1985 1986 1987
		}
	}

	/* ADD-IP Security: The draft requires us to ABORT or ignore the
	 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not.  Do this
	 * only if ADD-IP is turned on and we are not backward-compatible
	 * mode.
	 */
1988
	if (net->sctp.addip_noauth)
1989 1990
		return 1;

1991
	if (net->sctp.addip_enable && !have_auth && have_asconf)
1992 1993 1994 1995 1996
		return 0;

	return 1;
}

1997 1998 1999
static void sctp_process_ext_param(struct sctp_association *asoc,
				    union sctp_params param)
{
2000
	struct net *net = sock_net(asoc->base.sk);
2001 2002 2003 2004 2005
	__u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
	int i;

	for (i = 0; i < num_ext; i++) {
		switch (param.ext->chunks[i]) {
2006 2007
		case SCTP_CID_FWD_TSN:
			if (net->sctp.prsctp_enable && !asoc->peer.prsctp_capable)
2008
				    asoc->peer.prsctp_capable = 1;
2009 2010 2011 2012 2013
			break;
		case SCTP_CID_AUTH:
			/* if the peer reports AUTH, assume that he
			 * supports AUTH.
			 */
2014
			if (asoc->ep->auth_enable)
2015 2016 2017 2018 2019 2020 2021 2022 2023
				asoc->peer.auth_capable = 1;
			break;
		case SCTP_CID_ASCONF:
		case SCTP_CID_ASCONF_ACK:
			if (net->sctp.addip_enable)
				asoc->peer.asconf_capable = 1;
			break;
		default:
			break;
2024 2025 2026 2027
		}
	}
}

L
Linus Torvalds 已提交
2028 2029 2030 2031 2032 2033 2034
/* RFC 3.2.1 & the Implementers Guide 2.2.
 *
 * The Parameter Types are encoded such that the
 * highest-order two bits specify the action that must be
 * taken if the processing endpoint does not recognize the
 * Parameter Type.
 *
2035 2036
 * 00 - Stop processing this parameter; do not process any further
 * 	parameters within this chunk
L
Linus Torvalds 已提交
2037
 *
2038 2039 2040
 * 01 - Stop processing this parameter, do not process any further
 *	parameters within this chunk, and report the unrecognized
 *	parameter in an 'Unrecognized Parameter' ERROR chunk.
L
Linus Torvalds 已提交
2041 2042 2043 2044 2045
 *
 * 10 - Skip this parameter and continue processing.
 *
 * 11 - Skip this parameter and continue processing but
 *	report the unrecognized parameter in an
2046
 *	'Unrecognized Parameter' ERROR chunk.
L
Linus Torvalds 已提交
2047 2048
 *
 * Return value:
2049 2050 2051
 * 	SCTP_IERROR_NO_ERROR - continue with the chunk
 * 	SCTP_IERROR_ERROR    - stop and report an error.
 * 	SCTP_IERROR_NOMEME   - out of memory.
L
Linus Torvalds 已提交
2052
 */
2053 2054 2055 2056
static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
					    union sctp_params param,
					    struct sctp_chunk *chunk,
					    struct sctp_chunk **errp)
L
Linus Torvalds 已提交
2057
{
2058
	int retval = SCTP_IERROR_NO_ERROR;
L
Linus Torvalds 已提交
2059 2060 2061

	switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
	case SCTP_PARAM_ACTION_DISCARD:
2062
		retval =  SCTP_IERROR_ERROR;
L
Linus Torvalds 已提交
2063 2064 2065
		break;
	case SCTP_PARAM_ACTION_SKIP:
		break;
2066 2067 2068
	case SCTP_PARAM_ACTION_DISCARD_ERR:
		retval =  SCTP_IERROR_ERROR;
		/* Fall through */
L
Linus Torvalds 已提交
2069 2070 2071 2072 2073
	case SCTP_PARAM_ACTION_SKIP_ERR:
		/* Make an ERROR chunk, preparing enough room for
		 * returning multiple unknown parameters.
		 */
		if (NULL == *errp)
2074
			*errp = sctp_make_op_error_fixed(asoc, chunk);
L
Linus Torvalds 已提交
2075 2076

		if (*errp) {
2077 2078 2079 2080 2081
			if (!sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
					WORD_ROUND(ntohs(param.p->length))))
				sctp_addto_chunk_fixed(*errp,
						WORD_ROUND(ntohs(param.p->length)),
						param.v);
L
Linus Torvalds 已提交
2082 2083 2084 2085 2086 2087
		} else {
			/* If there is no memory for generating the ERROR
			 * report as specified, an ABORT will be triggered
			 * to the peer and the association won't be
			 * established.
			 */
2088
			retval = SCTP_IERROR_NOMEM;
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097
		}
		break;
	default:
		break;
	}

	return retval;
}

2098
/* Verify variable length parameters
L
Linus Torvalds 已提交
2099
 * Return values:
2100 2101 2102 2103
 * 	SCTP_IERROR_ABORT - trigger an ABORT
 * 	SCTP_IERROR_NOMEM - out of memory (abort)
 *	SCTP_IERROR_ERROR - stop processing, trigger an ERROR
 * 	SCTP_IERROR_NO_ERROR - continue with the chunk
L
Linus Torvalds 已提交
2104
 */
2105
static sctp_ierror_t sctp_verify_param(struct net *net,
2106
					const struct sctp_endpoint *ep,
2107
					const struct sctp_association *asoc,
2108 2109 2110 2111
					union sctp_params param,
					sctp_cid_t cid,
					struct sctp_chunk *chunk,
					struct sctp_chunk **err_chunk)
L
Linus Torvalds 已提交
2112
{
2113
	struct sctp_hmac_algo_param *hmacs;
2114
	int retval = SCTP_IERROR_NO_ERROR;
2115 2116
	__u16 n_elt, id = 0;
	int i;
L
Linus Torvalds 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131

	/* FIXME - This routine is not looking at each parameter per the
	 * chunk type, i.e., unrecognized parameters should be further
	 * identified based on the chunk id.
	 */

	switch (param.p->type) {
	case SCTP_PARAM_IPV4_ADDRESS:
	case SCTP_PARAM_IPV6_ADDRESS:
	case SCTP_PARAM_COOKIE_PRESERVATIVE:
	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
	case SCTP_PARAM_STATE_COOKIE:
	case SCTP_PARAM_HEARTBEAT_INFO:
	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
	case SCTP_PARAM_ECN_CAPABLE:
2132
	case SCTP_PARAM_ADAPTATION_LAYER_IND:
2133 2134
		break;

2135
	case SCTP_PARAM_SUPPORTED_EXT:
2136
		if (!sctp_verify_ext_param(net, param))
2137
			return SCTP_IERROR_ABORT;
L
Linus Torvalds 已提交
2138 2139
		break;

2140
	case SCTP_PARAM_SET_PRIMARY:
2141
		if (net->sctp.addip_enable)
2142 2143 2144
			break;
		goto fallthrough;

L
Linus Torvalds 已提交
2145 2146
	case SCTP_PARAM_HOST_NAME_ADDRESS:
		/* Tell the peer, we won't support this param.  */
2147 2148 2149
		sctp_process_hn_param(asoc, param, chunk, err_chunk);
		retval = SCTP_IERROR_ABORT;
		break;
2150

L
Linus Torvalds 已提交
2151
	case SCTP_PARAM_FWD_TSN_SUPPORT:
2152
		if (net->sctp.prsctp_enable)
L
Linus Torvalds 已提交
2153
			break;
2154 2155 2156
		goto fallthrough;

	case SCTP_PARAM_RANDOM:
2157
		if (!ep->auth_enable)
2158 2159 2160 2161 2162 2163 2164 2165
			goto fallthrough;

		/* SCTP-AUTH: Secion 6.1
		 * If the random number is not 32 byte long the association
		 * MUST be aborted.  The ABORT chunk SHOULD contain the error
		 * cause 'Protocol Violation'.
		 */
		if (SCTP_AUTH_RANDOM_LENGTH !=
2166 2167
			ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
			sctp_process_inv_paramlength(asoc, param.p,
2168
							chunk, err_chunk);
2169 2170
			retval = SCTP_IERROR_ABORT;
		}
2171 2172 2173
		break;

	case SCTP_PARAM_CHUNKS:
2174
		if (!ep->auth_enable)
2175 2176 2177 2178 2179 2180 2181
			goto fallthrough;

		/* SCTP-AUTH: Section 3.2
		 * The CHUNKS parameter MUST be included once in the INIT or
		 *  INIT-ACK chunk if the sender wants to receive authenticated
		 *  chunks.  Its maximum length is 260 bytes.
		 */
2182 2183 2184 2185 2186
		if (260 < ntohs(param.p->length)) {
			sctp_process_inv_paramlength(asoc, param.p,
						     chunk, err_chunk);
			retval = SCTP_IERROR_ABORT;
		}
2187 2188 2189
		break;

	case SCTP_PARAM_HMAC_ALGO:
2190
		if (!ep->auth_enable)
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
			goto fallthrough;

		hmacs = (struct sctp_hmac_algo_param *)param.p;
		n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;

		/* SCTP-AUTH: Section 6.1
		 * The HMAC algorithm based on SHA-1 MUST be supported and
		 * included in the HMAC-ALGO parameter.
		 */
		for (i = 0; i < n_elt; i++) {
			id = ntohs(hmacs->hmac_ids[i]);

			if (id == SCTP_AUTH_HMAC_ID_SHA1)
				break;
		}

		if (id != SCTP_AUTH_HMAC_ID_SHA1) {
			sctp_process_inv_paramlength(asoc, param.p, chunk,
						     err_chunk);
			retval = SCTP_IERROR_ABORT;
		}
		break;
2213
fallthrough:
L
Linus Torvalds 已提交
2214
	default:
2215 2216 2217
		pr_debug("%s: unrecognized param:%d for chunk:%d\n",
			 __func__, ntohs(param.p->type), cid);

2218
		retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
L
Linus Torvalds 已提交
2219 2220 2221 2222 2223 2224
		break;
	}
	return retval;
}

/* Verify the INIT packet before we process it.  */
2225 2226 2227
int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
		     const struct sctp_association *asoc, sctp_cid_t cid,
		     sctp_init_chunk_t *peer_init, struct sctp_chunk *chunk,
L
Linus Torvalds 已提交
2228 2229 2230
		     struct sctp_chunk **errp)
{
	union sctp_params param;
2231
	bool has_cookie = false;
2232
	int result;
L
Linus Torvalds 已提交
2233

2234 2235 2236 2237 2238 2239 2240 2241
	/* Check for missing mandatory parameters. Note: Initial TSN is
	 * also mandatory, but is not checked here since the valid range
	 * is 0..2**32-1. RFC4960, section 3.3.3.
	 */
	if (peer_init->init_hdr.num_outbound_streams == 0 ||
	    peer_init->init_hdr.num_inbound_streams == 0 ||
	    peer_init->init_hdr.init_tag == 0 ||
	    ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
2242
		return sctp_process_inv_mandatory(asoc, chunk, errp);
L
Linus Torvalds 已提交
2243 2244

	sctp_walk_params(param, peer_init, init_hdr.params) {
2245 2246 2247
		if (param.p->type == SCTP_PARAM_STATE_COOKIE)
			has_cookie = true;
	}
L
Linus Torvalds 已提交
2248 2249 2250 2251 2252 2253 2254 2255

	/* There is a possibility that a parameter length was bad and
	 * in that case we would have stoped walking the parameters.
	 * The current param.p would point at the bad one.
	 * Current consensus on the mailing list is to generate a PROTOCOL
	 * VIOLATION error.  We build the ERROR chunk here and let the normal
	 * error handling code build and send the packet.
	 */
2256
	if (param.v != (void *)chunk->chunk_end)
2257
		return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
L
Linus Torvalds 已提交
2258 2259 2260 2261

	/* The only missing mandatory param possible today is
	 * the state cookie for an INIT-ACK chunk.
	 */
2262 2263 2264
	if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
		return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
						  chunk, errp);
L
Linus Torvalds 已提交
2265

2266
	/* Verify all the variable length parameters */
L
Linus Torvalds 已提交
2267
	sctp_walk_params(param, peer_init, init_hdr.params) {
2268 2269
		result = sctp_verify_param(net, ep, asoc, param, cid,
					   chunk, errp);
2270
		switch (result) {
2271 2272 2273 2274 2275 2276 2277 2278
		case SCTP_IERROR_ABORT:
		case SCTP_IERROR_NOMEM:
			return 0;
		case SCTP_IERROR_ERROR:
			return 1;
		case SCTP_IERROR_NO_ERROR:
		default:
			break;
L
Linus Torvalds 已提交
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
		}

	} /* for (loop through all parameters) */

	return 1;
}

/* Unpack the parameters in an INIT packet into an association.
 * Returns 0 on failure, else success.
 * FIXME:  This is an association method.
 */
2290
int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
L
Linus Torvalds 已提交
2291
		      const union sctp_addr *peer_addr,
A
Al Viro 已提交
2292
		      sctp_init_chunk_t *peer_init, gfp_t gfp)
L
Linus Torvalds 已提交
2293
{
2294
	struct net *net = sock_net(asoc->base.sk);
L
Linus Torvalds 已提交
2295 2296 2297
	union sctp_params param;
	struct sctp_transport *transport;
	struct list_head *pos, *temp;
2298 2299
	struct sctp_af *af;
	union sctp_addr addr;
L
Linus Torvalds 已提交
2300
	char *cookie;
2301
	int src_match = 0;
L
Linus Torvalds 已提交
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312

	/* We must include the address that the INIT packet came from.
	 * This is the only address that matters for an INIT packet.
	 * When processing a COOKIE ECHO, we retrieve the from address
	 * of the INIT from the cookie.
	 */

	/* This implementation defaults to making the first transport
	 * added as the primary transport.  The source address seems to
	 * be a a better choice than any of the embedded addresses.
	 */
2313
	if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2314 2315 2316 2317
		goto nomem;

	if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
		src_match = 1;
L
Linus Torvalds 已提交
2318 2319 2320

	/* Process the initialization parameters.  */
	sctp_walk_params(param, peer_init, init_hdr.params) {
2321 2322 2323 2324 2325 2326 2327 2328
		if (!src_match && (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
		    param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
			af = sctp_get_af_specific(param_type2af(param.p->type));
			af->from_addr_param(&addr, param.addr,
					    chunk->sctp_hdr->source, 0);
			if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
				src_match = 1;
		}
L
Linus Torvalds 已提交
2329 2330

		if (!sctp_process_param(asoc, param, peer_addr, gfp))
2331
			goto clean_up;
L
Linus Torvalds 已提交
2332 2333
	}

2334 2335 2336 2337
	/* source address of chunk may not match any valid address */
	if (!src_match)
		goto clean_up;

2338 2339 2340 2341 2342 2343 2344
	/* AUTH: After processing the parameters, make sure that we
	 * have all the required info to potentially do authentications.
	 */
	if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
					!asoc->peer.peer_hmacs))
		asoc->peer.auth_capable = 0;

2345 2346 2347 2348 2349
	/* In a non-backward compatible mode, if the peer claims
	 * support for ADD-IP but not AUTH,  the ADD-IP spec states
	 * that we MUST ABORT the association. Section 6.  The section
	 * also give us an option to silently ignore the packet, which
	 * is what we'll do here.
2350
	 */
2351
	if (!net->sctp.addip_noauth &&
2352
	     (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2353 2354 2355
		asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
						  SCTP_PARAM_DEL_IP |
						  SCTP_PARAM_SET_PRIMARY);
2356
		asoc->peer.asconf_capable = 0;
2357
		goto clean_up;
2358 2359
	}

F
Frank Filz 已提交
2360 2361 2362 2363 2364 2365 2366 2367
	/* Walk list of transports, removing transports in the UNKNOWN state. */
	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
		transport = list_entry(pos, struct sctp_transport, transports);
		if (transport->state == SCTP_UNKNOWN) {
			sctp_assoc_rm_peer(asoc, transport);
		}
	}

L
Linus Torvalds 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
	/* The fixed INIT headers are always in network byte
	 * order.
	 */
	asoc->peer.i.init_tag =
		ntohl(peer_init->init_hdr.init_tag);
	asoc->peer.i.a_rwnd =
		ntohl(peer_init->init_hdr.a_rwnd);
	asoc->peer.i.num_outbound_streams =
		ntohs(peer_init->init_hdr.num_outbound_streams);
	asoc->peer.i.num_inbound_streams =
		ntohs(peer_init->init_hdr.num_inbound_streams);
	asoc->peer.i.initial_tsn =
		ntohl(peer_init->init_hdr.initial_tsn);

	/* Apply the upper bounds for output streams based on peer's
	 * number of inbound streams.
	 */
	if (asoc->c.sinit_num_ostreams  >
	    ntohs(peer_init->init_hdr.num_inbound_streams)) {
		asoc->c.sinit_num_ostreams =
			ntohs(peer_init->init_hdr.num_inbound_streams);
	}

	if (asoc->c.sinit_max_instreams >
	    ntohs(peer_init->init_hdr.num_outbound_streams)) {
		asoc->c.sinit_max_instreams =
			ntohs(peer_init->init_hdr.num_outbound_streams);
	}

	/* Copy Initiation tag from INIT to VT_peer in cookie.   */
	asoc->c.peer_vtag = asoc->peer.i.init_tag;

	/* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
	asoc->peer.rwnd = asoc->peer.i.a_rwnd;

	/* Copy cookie in case we need to resend COOKIE-ECHO. */
	cookie = asoc->peer.cookie;
	if (cookie) {
2406
		asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
L
Linus Torvalds 已提交
2407 2408 2409 2410 2411 2412 2413 2414
		if (!asoc->peer.cookie)
			goto clean_up;
	}

	/* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
	 * high (for example, implementations MAY use the size of the receiver
	 * advertised window).
	 */
2415 2416
	list_for_each_entry(transport, &asoc->peer.transport_addr_list,
			transports) {
L
Linus Torvalds 已提交
2417 2418 2419 2420
		transport->ssthresh = asoc->peer.i.a_rwnd;
	}

	/* Set up the TSN tracking pieces.  */
2421 2422 2423
	if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
				asoc->peer.i.initial_tsn, gfp))
		goto clean_up;
L
Linus Torvalds 已提交
2424 2425 2426 2427 2428 2429 2430 2431 2432

	/* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
	 *
	 * The stream sequence number in all the streams shall start
	 * from 0 when the association is established.  Also, when the
	 * stream sequence number reaches the value 65535 the next
	 * stream sequence number shall be set to 0.
	 */

F
Frank Filz 已提交
2433
	/* Allocate storage for the negotiated streams if it is not a temporary
2434
	 * association.
L
Linus Torvalds 已提交
2435 2436 2437 2438 2439 2440 2441 2442 2443
	 */
	if (!asoc->temp) {
		int error;

		asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
					       asoc->c.sinit_num_ostreams, gfp);
		if (!asoc->ssnmap)
			goto clean_up;

2444 2445
		error = sctp_assoc_set_id(asoc, gfp);
		if (error)
L
Linus Torvalds 已提交
2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
			goto clean_up;
	}

	/* ADDIP Section 4.1 ASCONF Chunk Procedures
	 *
	 * When an endpoint has an ASCONF signaled change to be sent to the
	 * remote endpoint it should do the following:
	 * ...
	 * A2) A serial number should be assigned to the Chunk. The serial
	 * number should be a monotonically increasing number. All serial
	 * numbers are defined to be initialized at the start of the
	 * association to the same value as the Initial TSN.
	 */
	asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
	return 1;

clean_up:
	/* Release the transport structures. */
	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
		transport = list_entry(pos, struct sctp_transport, transports);
2466 2467
		if (transport->state != SCTP_ACTIVE)
			sctp_assoc_rm_peer(asoc, transport);
L
Linus Torvalds 已提交
2468
	}
F
Frank Filz 已提交
2469

L
Linus Torvalds 已提交
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
nomem:
	return 0;
}


/* Update asoc with the option described in param.
 *
 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
 *
 * asoc is the association to update.
 * param is the variable length parameter to use for update.
 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
 * If the current packet is an INIT we want to minimize the amount of
 * work we do.  In particular, we should not build transport
 * structures for the addresses.
 */
static int sctp_process_param(struct sctp_association *asoc,
			      union sctp_params param,
			      const union sctp_addr *peer_addr,
A
Al Viro 已提交
2489
			      gfp_t gfp)
L
Linus Torvalds 已提交
2490
{
2491
	struct net *net = sock_net(asoc->base.sk);
L
Linus Torvalds 已提交
2492 2493 2494 2495 2496 2497 2498
	union sctp_addr addr;
	int i;
	__u16 sat;
	int retval = 1;
	sctp_scope_t scope;
	time_t stale;
	struct sctp_af *af;
2499 2500
	union sctp_addr_param *addr_param;
	struct sctp_transport *t;
2501
	struct sctp_endpoint *ep = asoc->ep;
L
Linus Torvalds 已提交
2502 2503 2504 2505 2506 2507 2508 2509 2510

	/* We maintain all INIT parameters in network byte order all the
	 * time.  This allows us to not worry about whether the parameters
	 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
	 */
	switch (param.p->type) {
	case SCTP_PARAM_IPV6_ADDRESS:
		if (PF_INET6 != asoc->base.sk->sk_family)
			break;
2511 2512
		goto do_addr_param;

L
Linus Torvalds 已提交
2513
	case SCTP_PARAM_IPV4_ADDRESS:
2514 2515 2516 2517
		/* v4 addresses are not allowed on v6-only socket */
		if (ipv6_only_sock(asoc->base.sk))
			break;
do_addr_param:
L
Linus Torvalds 已提交
2518
		af = sctp_get_af_specific(param_type2af(param.p->type));
2519
		af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
L
Linus Torvalds 已提交
2520
		scope = sctp_scope(peer_addr);
2521
		if (sctp_in_scope(net, &addr, scope))
2522
			if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
L
Linus Torvalds 已提交
2523 2524 2525 2526
				return 0;
		break;

	case SCTP_PARAM_COOKIE_PRESERVATIVE:
2527
		if (!net->sctp.cookie_preserve_enable)
L
Linus Torvalds 已提交
2528 2529 2530 2531 2532 2533 2534
			break;

		stale = ntohl(param.life->lifespan_increment);

		/* Suggested Cookie Life span increment's unit is msec,
		 * (1/1000sec).
		 */
2535
		asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
L
Linus Torvalds 已提交
2536 2537 2538
		break;

	case SCTP_PARAM_HOST_NAME_ADDRESS:
2539
		pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
L
Linus Torvalds 已提交
2540 2541 2542 2543 2544 2545 2546 2547 2548
		break;

	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
		/* Turn off the default values first so we'll know which
		 * ones are really set by the peer.
		 */
		asoc->peer.ipv4_address = 0;
		asoc->peer.ipv6_address = 0;

2549 2550 2551 2552 2553 2554 2555 2556
		/* Assume that peer supports the address family
		 * by which it sends a packet.
		 */
		if (peer_addr->sa.sa_family == AF_INET6)
			asoc->peer.ipv6_address = 1;
		else if (peer_addr->sa.sa_family == AF_INET)
			asoc->peer.ipv4_address = 1;

L
Linus Torvalds 已提交
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
		/* Cycle through address types; avoid divide by 0. */
		sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
		if (sat)
			sat /= sizeof(__u16);

		for (i = 0; i < sat; ++i) {
			switch (param.sat->types[i]) {
			case SCTP_PARAM_IPV4_ADDRESS:
				asoc->peer.ipv4_address = 1;
				break;

			case SCTP_PARAM_IPV6_ADDRESS:
2569 2570
				if (PF_INET6 == asoc->base.sk->sk_family)
					asoc->peer.ipv6_address = 1;
L
Linus Torvalds 已提交
2571 2572 2573 2574 2575 2576 2577 2578
				break;

			case SCTP_PARAM_HOST_NAME_ADDRESS:
				asoc->peer.hostname_address = 1;
				break;

			default: /* Just ignore anything else.  */
				break;
2579
			}
L
Linus Torvalds 已提交
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
		}
		break;

	case SCTP_PARAM_STATE_COOKIE:
		asoc->peer.cookie_len =
			ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
		asoc->peer.cookie = param.cookie->body;
		break;

	case SCTP_PARAM_HEARTBEAT_INFO:
		/* Would be odd to receive, but it causes no problems. */
		break;

	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
		/* Rejected during verify stage. */
		break;

	case SCTP_PARAM_ECN_CAPABLE:
		asoc->peer.ecn_capable = 1;
		break;

2601
	case SCTP_PARAM_ADAPTATION_LAYER_IND:
2602
		asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
L
Linus Torvalds 已提交
2603 2604
		break;

2605
	case SCTP_PARAM_SET_PRIMARY:
2606
		if (!net->sctp.addip_enable)
2607 2608
			goto fall_through;

2609 2610
		addr_param = param.v + sizeof(sctp_addip_param_t);

2611
		af = sctp_get_af_specific(param_type2af(addr_param->p.type));
2612 2613 2614
		if (af == NULL)
			break;

2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
		af->from_addr_param(&addr, addr_param,
				    htons(asoc->peer.port), 0);

		/* if the address is invalid, we can't process it.
		 * XXX: see spec for what to do.
		 */
		if (!af->addr_valid(&addr, NULL, NULL))
			break;

		t = sctp_assoc_lookup_paddr(asoc, &addr);
		if (!t)
			break;

		sctp_assoc_set_primary(asoc, t);
		break;

2631 2632 2633 2634
	case SCTP_PARAM_SUPPORTED_EXT:
		sctp_process_ext_param(asoc, param);
		break;

L
Linus Torvalds 已提交
2635
	case SCTP_PARAM_FWD_TSN_SUPPORT:
2636
		if (net->sctp.prsctp_enable) {
L
Linus Torvalds 已提交
2637 2638 2639
			asoc->peer.prsctp_capable = 1;
			break;
		}
2640
		/* Fall Through */
2641 2642 2643
		goto fall_through;

	case SCTP_PARAM_RANDOM:
2644
		if (!ep->auth_enable)
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
			goto fall_through;

		/* Save peer's random parameter */
		asoc->peer.peer_random = kmemdup(param.p,
					    ntohs(param.p->length), gfp);
		if (!asoc->peer.peer_random) {
			retval = 0;
			break;
		}
		break;

	case SCTP_PARAM_HMAC_ALGO:
2657
		if (!ep->auth_enable)
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
			goto fall_through;

		/* Save peer's HMAC list */
		asoc->peer.peer_hmacs = kmemdup(param.p,
					    ntohs(param.p->length), gfp);
		if (!asoc->peer.peer_hmacs) {
			retval = 0;
			break;
		}

		/* Set the default HMAC the peer requested*/
		sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
		break;

	case SCTP_PARAM_CHUNKS:
2673
		if (!ep->auth_enable)
2674 2675 2676 2677 2678 2679 2680 2681
			goto fall_through;

		asoc->peer.peer_chunks = kmemdup(param.p,
					    ntohs(param.p->length), gfp);
		if (!asoc->peer.peer_chunks)
			retval = 0;
		break;
fall_through:
L
Linus Torvalds 已提交
2682 2683 2684 2685 2686 2687
	default:
		/* Any unrecognized parameters should have been caught
		 * and handled by sctp_verify_param() which should be
		 * called prior to this routine.  Simply log the error
		 * here.
		 */
2688 2689
		pr_debug("%s: ignoring param:%d for association:%p.\n",
			 __func__, ntohs(param.p->type), asoc);
L
Linus Torvalds 已提交
2690
		break;
2691
	}
L
Linus Torvalds 已提交
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739

	return retval;
}

/* Select a new verification tag.  */
__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
{
	/* I believe that this random number generator complies with RFC1750.
	 * A tag of 0 is reserved for special cases (e.g. INIT).
	 */
	__u32 x;

	do {
		get_random_bytes(&x, sizeof(__u32));
	} while (x == 0);

	return x;
}

/* Select an initial TSN to send during startup.  */
__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
{
	__u32 retval;

	get_random_bytes(&retval, sizeof(__u32));
	return retval;
}

/*
 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
 *      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 = 0xC1   |  Chunk Flags  |      Chunk Length             |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Serial Number                           |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                    Address Parameter                          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                     ASCONF Parameter #1                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     \                                                               \
 *     /                             ....                              /
 *     \                                                               \
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                     ASCONF Parameter #N                       |
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
2740
 * Address Parameter and other parameter will not be wrapped in this function
L
Linus Torvalds 已提交
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
 */
static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
					   union sctp_addr *addr,
					   int vparam_len)
{
	sctp_addiphdr_t asconf;
	struct sctp_chunk *retval;
	int length = sizeof(asconf) + vparam_len;
	union sctp_addr_param addrparam;
	int addrlen;
	struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);

	addrlen = af->to_addr_param(addr, &addrparam);
	if (!addrlen)
		return NULL;
	length += addrlen;

	/* Create the chunk.  */
2759
	retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length);
L
Linus Torvalds 已提交
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
	if (!retval)
		return NULL;

	asconf.serial = htonl(asoc->addip_serial++);

	retval->subh.addip_hdr =
		sctp_addto_chunk(retval, sizeof(asconf), &asconf);
	retval->param_hdr.v =
		sctp_addto_chunk(retval, addrlen, &addrparam);

	return retval;
}

/* ADDIP
 * 3.2.1 Add IP Address
 * 	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 = 0xC001          |    Length = Variable          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |               ASCONF-Request Correlation ID                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Address Parameter                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * 3.2.2 Delete IP Address
 * 	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 = 0xC002          |    Length = Variable          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |               ASCONF-Request Correlation ID                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Address Parameter                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */
struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
					      union sctp_addr	      *laddr,
					      struct sockaddr	      *addrs,
					      int		      addrcnt,
2801
					      __be16		      flags)
L
Linus Torvalds 已提交
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
{
	sctp_addip_param_t	param;
	struct sctp_chunk	*retval;
	union sctp_addr_param	addr_param;
	union sctp_addr		*addr;
	void			*addr_buf;
	struct sctp_af		*af;
	int			paramlen = sizeof(param);
	int			addr_param_len = 0;
	int 			totallen = 0;
	int 			i;
2813
	int			del_pickup = 0;
L
Linus Torvalds 已提交
2814 2815 2816 2817

	/* Get total length of all the address parameters. */
	addr_buf = addrs;
	for (i = 0; i < addrcnt; i++) {
J
Joe Perches 已提交
2818
		addr = addr_buf;
L
Linus Torvalds 已提交
2819 2820 2821 2822 2823 2824 2825
		af = sctp_get_af_specific(addr->v4.sin_family);
		addr_param_len = af->to_addr_param(addr, &addr_param);

		totallen += paramlen;
		totallen += addr_param_len;

		addr_buf += af->sockaddr_len;
2826 2827 2828 2829 2830
		if (asoc->asconf_addr_del_pending && !del_pickup) {
			/* reuse the parameter length from the same scope one */
			totallen += paramlen;
			totallen += addr_param_len;
			del_pickup = 1;
2831 2832 2833 2834

			pr_debug("%s: picked same-scope del_pending addr, "
				 "totallen for all addresses is %d\n",
				 __func__, totallen);
2835
		}
L
Linus Torvalds 已提交
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
	}

	/* Create an asconf chunk with the required length. */
	retval = sctp_make_asconf(asoc, laddr, totallen);
	if (!retval)
		return NULL;

	/* Add the address parameters to the asconf chunk. */
	addr_buf = addrs;
	for (i = 0; i < addrcnt; i++) {
J
Joe Perches 已提交
2846
		addr = addr_buf;
L
Linus Torvalds 已提交
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
		af = sctp_get_af_specific(addr->v4.sin_family);
		addr_param_len = af->to_addr_param(addr, &addr_param);
		param.param_hdr.type = flags;
		param.param_hdr.length = htons(paramlen + addr_param_len);
		param.crr_id = i;

		sctp_addto_chunk(retval, paramlen, &param);
		sctp_addto_chunk(retval, addr_param_len, &addr_param);

		addr_buf += af->sockaddr_len;
	}
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
	if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
		addr = asoc->asconf_addr_del_pending;
		af = sctp_get_af_specific(addr->v4.sin_family);
		addr_param_len = af->to_addr_param(addr, &addr_param);
		param.param_hdr.type = SCTP_PARAM_DEL_IP;
		param.param_hdr.length = htons(paramlen + addr_param_len);
		param.crr_id = i;

		sctp_addto_chunk(retval, paramlen, &param);
		sctp_addto_chunk(retval, addr_param_len, &addr_param);
	}
L
Linus Torvalds 已提交
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
	return retval;
}

/* ADDIP
 * 3.2.4 Set Primary IP Address
 *	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 =0xC004           |    Length = Variable          |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |               ASCONF-Request Correlation ID                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Address Parameter                       |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
2884
 * Create an ASCONF chunk with Set Primary IP address parameter.
L
Linus Torvalds 已提交
2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
 */
struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
					     union sctp_addr *addr)
{
	sctp_addip_param_t	param;
	struct sctp_chunk 	*retval;
	int 			len = sizeof(param);
	union sctp_addr_param	addrparam;
	int			addrlen;
	struct sctp_af		*af = sctp_get_af_specific(addr->v4.sin_family);

	addrlen = af->to_addr_param(addr, &addrparam);
	if (!addrlen)
		return NULL;
	len += addrlen;

	/* Create the chunk and make asconf header. */
	retval = sctp_make_asconf(asoc, addr, len);
	if (!retval)
		return NULL;

	param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
	param.param_hdr.length = htons(len);
	param.crr_id = 0;

	sctp_addto_chunk(retval, sizeof(param), &param);
	sctp_addto_chunk(retval, addrlen, &addrparam);

	return retval;
}

/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
 *      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 = 0x80   |  Chunk Flags  |      Chunk Length             |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                       Serial Number                           |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                 ASCONF Parameter Response#1                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     \                                                               \
 *     /                             ....                              /
 *     \                                                               \
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                 ASCONF Parameter Response#N                   |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
2933
 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
L
Linus Torvalds 已提交
2934 2935 2936 2937 2938 2939 2940 2941 2942
 */
static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
					       __u32 serial, int vparam_len)
{
	sctp_addiphdr_t		asconf;
	struct sctp_chunk	*retval;
	int			length = sizeof(asconf) + vparam_len;

	/* Create the chunk.  */
2943
	retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length);
L
Linus Torvalds 已提交
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
	if (!retval)
		return NULL;

	asconf.serial = htonl(serial);

	retval->subh.addip_hdr =
		sctp_addto_chunk(retval, sizeof(asconf), &asconf);

	return retval;
}

/* Add response parameters to an ASCONF_ACK chunk. */
A
Al Viro 已提交
2956
static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2957
			      __be16 err_code, sctp_addip_param_t *asconf_param)
L
Linus Torvalds 已提交
2958 2959 2960 2961 2962
{
	sctp_addip_param_t 	ack_param;
	sctp_errhdr_t		err_param;
	int			asconf_param_len = 0;
	int			err_param_len = 0;
2963
	__be16			response_type;
L
Linus Torvalds 已提交
2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974

	if (SCTP_ERROR_NO_ERROR == err_code) {
		response_type = SCTP_PARAM_SUCCESS_REPORT;
	} else {
		response_type = SCTP_PARAM_ERR_CAUSE;
		err_param_len = sizeof(err_param);
		if (asconf_param)
			asconf_param_len =
				 ntohs(asconf_param->param_hdr.length);
	}

2975
	/* Add Success Indication or Error Cause Indication parameter. */
L
Linus Torvalds 已提交
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
	ack_param.param_hdr.type = response_type;
	ack_param.param_hdr.length = htons(sizeof(ack_param) +
					   err_param_len +
					   asconf_param_len);
	ack_param.crr_id = crr_id;
	sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);

	if (SCTP_ERROR_NO_ERROR == err_code)
		return;

	/* Add Error Cause parameter. */
	err_param.cause = err_code;
	err_param.length = htons(err_param_len + asconf_param_len);
	sctp_addto_chunk(chunk, err_param_len, &err_param);

	/* Add the failed TLV copied from ASCONF chunk. */
	if (asconf_param)
		sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
}

/* Process a asconf parameter. */
2997
static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
L
Linus Torvalds 已提交
2998 2999 3000 3001 3002 3003 3004
				       struct sctp_chunk *asconf,
				       sctp_addip_param_t *asconf_param)
{
	struct sctp_transport *peer;
	struct sctp_af *af;
	union sctp_addr	addr;
	union sctp_addr_param *addr_param;
3005

J
Joe Perches 已提交
3006
	addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
3007

3008 3009 3010 3011 3012
	if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
	    asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
	    asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
		return SCTP_ERROR_UNKNOWN_PARAM;

3013
	switch (addr_param->p.type) {
3014 3015
	case SCTP_PARAM_IPV6_ADDRESS:
		if (!asoc->peer.ipv6_address)
3016
			return SCTP_ERROR_DNS_FAILED;
3017 3018 3019
		break;
	case SCTP_PARAM_IPV4_ADDRESS:
		if (!asoc->peer.ipv4_address)
3020
			return SCTP_ERROR_DNS_FAILED;
3021 3022
		break;
	default:
3023
		return SCTP_ERROR_DNS_FAILED;
3024 3025
	}

3026
	af = sctp_get_af_specific(param_type2af(addr_param->p.type));
L
Linus Torvalds 已提交
3027
	if (unlikely(!af))
3028
		return SCTP_ERROR_DNS_FAILED;
L
Linus Torvalds 已提交
3029

3030
	af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
3031 3032 3033 3034 3035 3036 3037

	/* ADDIP 4.2.1  This parameter MUST NOT contain a broadcast
	 * or multicast address.
	 * (note: wildcard is permitted and requires special handling so
	 *  make sure we check for that)
	 */
	if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
3038
		return SCTP_ERROR_DNS_FAILED;
3039

L
Linus Torvalds 已提交
3040 3041
	switch (asconf_param->param_hdr.type) {
	case SCTP_PARAM_ADD_IP:
3042 3043 3044 3045 3046 3047 3048
		/* Section 4.2.1:
		 * If the address 0.0.0.0 or ::0 is provided, the source
		 * address of the packet MUST be added.
		 */
		if (af->is_any(&addr))
			memcpy(&addr, &asconf->source, sizeof(addr));

L
Linus Torvalds 已提交
3049
		/* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
3050 3051 3052 3053 3054
		 * request and does not have the local resources to add this
		 * new address to the association, it MUST return an Error
		 * Cause TLV set to the new error code 'Operation Refused
		 * Due to Resource Shortage'.
		 */
L
Linus Torvalds 已提交
3055

3056
		peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
L
Linus Torvalds 已提交
3057 3058 3059 3060 3061 3062
		if (!peer)
			return SCTP_ERROR_RSRC_LOW;

		/* Start the heartbeat timer. */
		if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
			sctp_transport_hold(peer);
3063
		asoc->new_transport = peer;
L
Linus Torvalds 已提交
3064 3065 3066
		break;
	case SCTP_PARAM_DEL_IP:
		/* ADDIP 4.3 D7) If a request is received to delete the
3067 3068 3069 3070
		 * last remaining IP address of a peer endpoint, the receiver
		 * MUST send an Error Cause TLV with the error cause set to the
		 * new error code 'Request to Delete Last Remaining IP Address'.
		 */
3071
		if (asoc->peer.transport_count == 1)
L
Linus Torvalds 已提交
3072 3073 3074 3075 3076 3077 3078 3079 3080
			return SCTP_ERROR_DEL_LAST_IP;

		/* ADDIP 4.3 D8) If a request is received to delete an IP
		 * address which is also the source address of the IP packet
		 * which contained the ASCONF chunk, the receiver MUST reject
		 * this request. To reject the request the receiver MUST send
		 * an Error Cause TLV set to the new error code 'Request to
		 * Delete Source IP Address'
		 */
3081
		if (sctp_cmp_addr_exact(&asconf->source, &addr))
L
Linus Torvalds 已提交
3082 3083
			return SCTP_ERROR_DEL_SRC_IP;

3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094
		/* Section 4.2.2
		 * If the address 0.0.0.0 or ::0 is provided, all
		 * addresses of the peer except	the source address of the
		 * packet MUST be deleted.
		 */
		if (af->is_any(&addr)) {
			sctp_assoc_set_primary(asoc, asconf->transport);
			sctp_assoc_del_nonprimary_peers(asoc,
							asconf->transport);
		} else
			sctp_assoc_del_peer(asoc, &addr);
L
Linus Torvalds 已提交
3095 3096
		break;
	case SCTP_PARAM_SET_PRIMARY:
3097 3098 3099 3100 3101 3102 3103 3104
		/* ADDIP Section 4.2.4
		 * If the address 0.0.0.0 or ::0 is provided, the receiver
		 * MAY mark the source address of the packet as its
		 * primary.
		 */
		if (af->is_any(&addr))
			memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));

3105
		peer = sctp_assoc_lookup_paddr(asoc, &addr);
L
Linus Torvalds 已提交
3106
		if (!peer)
3107
			return SCTP_ERROR_DNS_FAILED;
L
Linus Torvalds 已提交
3108 3109 3110 3111 3112 3113 3114 3115

		sctp_assoc_set_primary(asoc, peer);
		break;
	}

	return SCTP_ERROR_NO_ERROR;
}

3116 3117 3118 3119 3120 3121
/* Verify the ASCONF packet before we process it. */
bool sctp_verify_asconf(const struct sctp_association *asoc,
			struct sctp_chunk *chunk, bool addr_param_needed,
			struct sctp_paramhdr **errp)
{
	sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) chunk->chunk_hdr;
3122
	union sctp_params param;
3123
	bool addr_param_seen = false;
3124

3125 3126
	sctp_walk_params(param, addip, addip_hdr.params) {
		size_t length = ntohs(param.p->length);
3127

3128
		*errp = param.p;
3129
		switch (param.p->type) {
3130 3131 3132 3133 3134
		case SCTP_PARAM_ERR_CAUSE:
			break;
		case SCTP_PARAM_IPV4_ADDRESS:
			if (length != sizeof(sctp_ipv4addr_param_t))
				return false;
3135 3136 3137 3138 3139
			/* ensure there is only one addr param and it's in the
			 * beginning of addip_hdr params, or we reject it.
			 */
			if (param.v != addip->addip_hdr.params)
				return false;
3140 3141 3142 3143 3144
			addr_param_seen = true;
			break;
		case SCTP_PARAM_IPV6_ADDRESS:
			if (length != sizeof(sctp_ipv6addr_param_t))
				return false;
3145 3146
			if (param.v != addip->addip_hdr.params)
				return false;
3147 3148
			addr_param_seen = true;
			break;
3149 3150 3151
		case SCTP_PARAM_ADD_IP:
		case SCTP_PARAM_DEL_IP:
		case SCTP_PARAM_SET_PRIMARY:
3152 3153 3154 3155 3156 3157 3158
			/* In ASCONF chunks, these need to be first. */
			if (addr_param_needed && !addr_param_seen)
				return false;
			length = ntohs(param.addip->param_hdr.length);
			if (length < sizeof(sctp_addip_param_t) +
				     sizeof(sctp_paramhdr_t))
				return false;
3159 3160 3161 3162
			break;
		case SCTP_PARAM_SUCCESS_REPORT:
		case SCTP_PARAM_ADAPTATION_LAYER_IND:
			if (length != sizeof(sctp_addip_param_t))
3163
				return false;
3164 3165
			break;
		default:
3166 3167
			/* This is unkown to us, reject! */
			return false;
3168 3169 3170
		}
	}

3171 3172 3173 3174 3175 3176 3177
	/* Remaining sanity checks. */
	if (addr_param_needed && !addr_param_seen)
		return false;
	if (!addr_param_needed && addr_param_seen)
		return false;
	if (param.v != chunk->chunk_end)
		return false;
3178

3179
	return true;
3180 3181
}

3182
/* Process an incoming ASCONF chunk with the next expected serial no. and
L
Linus Torvalds 已提交
3183 3184 3185 3186 3187
 * return an ASCONF_ACK chunk to be sent in response.
 */
struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
				       struct sctp_chunk *asconf)
{
3188 3189 3190
	sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) asconf->chunk_hdr;
	bool all_param_pass = true;
	union sctp_params param;
L
Linus Torvalds 已提交
3191 3192 3193 3194
	sctp_addiphdr_t		*hdr;
	union sctp_addr_param	*addr_param;
	sctp_addip_param_t	*asconf_param;
	struct sctp_chunk	*asconf_ack;
3195
	__be16	err_code;
L
Linus Torvalds 已提交
3196
	int	length = 0;
3197
	int	chunk_len;
L
Linus Torvalds 已提交
3198 3199
	__u32	serial;

3200
	chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
L
Linus Torvalds 已提交
3201 3202 3203
	hdr = (sctp_addiphdr_t *)asconf->skb->data;
	serial = ntohl(hdr->serial);

3204
	/* Skip the addiphdr and store a pointer to address parameter.  */
L
Linus Torvalds 已提交
3205 3206 3207 3208 3209
	length = sizeof(sctp_addiphdr_t);
	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
	chunk_len -= length;

	/* Skip the address parameter and store a pointer to the first
J
Joe Perches 已提交
3210
	 * asconf parameter.
3211
	 */
3212
	length = ntohs(addr_param->p.length);
J
Joe Perches 已提交
3213
	asconf_param = (void *)addr_param + length;
L
Linus Torvalds 已提交
3214 3215
	chunk_len -= length;

3216
	/* create an ASCONF_ACK chunk.
L
Linus Torvalds 已提交
3217
	 * Based on the definitions of parameters, we know that the size of
3218
	 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
J
Joe Perches 已提交
3219
	 * parameters.
L
Linus Torvalds 已提交
3220
	 */
3221
	asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
L
Linus Torvalds 已提交
3222 3223 3224 3225
	if (!asconf_ack)
		goto done;

	/* Process the TLVs contained within the ASCONF chunk. */
3226 3227 3228 3229 3230 3231
	sctp_walk_params(param, addip, addip_hdr.params) {
		/* Skip preceeding address parameters. */
		if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
		    param.p->type == SCTP_PARAM_IPV6_ADDRESS)
			continue;

L
Linus Torvalds 已提交
3232
		err_code = sctp_process_asconf_param(asoc, asconf,
3233
						     param.addip);
L
Linus Torvalds 已提交
3234 3235 3236 3237 3238 3239 3240
		/* ADDIP 4.1 A7)
		 * If an error response is received for a TLV parameter,
		 * all TLVs with no response before the failed TLV are
		 * considered successful if not reported.  All TLVs after
		 * the failed response are considered unsuccessful unless
		 * a specific success indication is present for the parameter.
		 */
3241 3242
		if (err_code != SCTP_ERROR_NO_ERROR)
			all_param_pass = false;
L
Linus Torvalds 已提交
3243
		if (!all_param_pass)
3244 3245
			sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
						 err_code, param.addip);
L
Linus Torvalds 已提交
3246 3247 3248 3249

		/* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
		 * an IP address sends an 'Out of Resource' in its response, it
		 * MUST also fail any subsequent add or delete requests bundled
3250
		 * in the ASCONF.
L
Linus Torvalds 已提交
3251
		 */
3252
		if (err_code == SCTP_ERROR_RSRC_LOW)
L
Linus Torvalds 已提交
3253 3254 3255 3256 3257 3258
			goto done;
	}
done:
	asoc->peer.addip_serial++;

	/* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3259
	 * after freeing the reference to old asconf ack if any.
L
Linus Torvalds 已提交
3260 3261 3262
	 */
	if (asconf_ack) {
		sctp_chunk_hold(asconf_ack);
3263 3264
		list_add_tail(&asconf_ack->transmitted_list,
			      &asoc->asconf_ack_list);
L
Linus Torvalds 已提交
3265 3266 3267 3268 3269 3270
	}

	return asconf_ack;
}

/* Process a asconf parameter that is successfully acked. */
3271
static void sctp_asconf_param_success(struct sctp_association *asoc,
L
Linus Torvalds 已提交
3272 3273 3274 3275 3276 3277 3278
				     sctp_addip_param_t *asconf_param)
{
	struct sctp_af *af;
	union sctp_addr	addr;
	struct sctp_bind_addr *bp = &asoc->base.bind_addr;
	union sctp_addr_param *addr_param;
	struct sctp_transport *transport;
3279
	struct sctp_sockaddr_entry *saddr;
L
Linus Torvalds 已提交
3280

J
Joe Perches 已提交
3281
	addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
L
Linus Torvalds 已提交
3282 3283

	/* We have checked the packet before, so we do not check again.	*/
3284
	af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3285
	af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
L
Linus Torvalds 已提交
3286 3287 3288

	switch (asconf_param->param_hdr.type) {
	case SCTP_PARAM_ADD_IP:
3289 3290 3291
		/* This is always done in BH context with a socket lock
		 * held, so the list can not change.
		 */
3292
		local_bh_disable();
3293
		list_for_each_entry(saddr, &bp->address_list, list) {
3294
			if (sctp_cmp_addr_exact(&saddr->a, &addr))
3295
				saddr->state = SCTP_ADDR_SRC;
3296
		}
3297
		local_bh_enable();
3298 3299 3300
		list_for_each_entry(transport, &asoc->peer.transport_addr_list,
				transports) {
			dst_release(transport->dst);
3301
			transport->dst = NULL;
3302
		}
L
Linus Torvalds 已提交
3303 3304
		break;
	case SCTP_PARAM_DEL_IP:
3305
		local_bh_disable();
3306
		sctp_del_bind_addr(bp, &addr);
3307 3308 3309 3310 3311
		if (asoc->asconf_addr_del_pending != NULL &&
		    sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
			kfree(asoc->asconf_addr_del_pending);
			asoc->asconf_addr_del_pending = NULL;
		}
3312
		local_bh_enable();
3313 3314
		list_for_each_entry(transport, &asoc->peer.transport_addr_list,
				transports) {
3315
			dst_release(transport->dst);
3316
			transport->dst = NULL;
L
Linus Torvalds 已提交
3317 3318 3319 3320 3321 3322 3323 3324 3325
		}
		break;
	default:
		break;
	}
}

/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
 * for the given asconf parameter.  If there is no response for this parameter,
3326
 * return the error code based on the third argument 'no_err'.
L
Linus Torvalds 已提交
3327 3328 3329 3330 3331 3332
 * ADDIP 4.1
 * A7) If an error response is received for a TLV parameter, all TLVs with no
 * response before the failed TLV are considered successful if not reported.
 * All TLVs after the failed response are considered unsuccessful unless a
 * specific success indication is present for the parameter.
 */
3333
static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
L
Linus Torvalds 已提交
3334 3335 3336 3337 3338 3339
				      sctp_addip_param_t *asconf_param,
				      int no_err)
{
	sctp_addip_param_t	*asconf_ack_param;
	sctp_errhdr_t		*err_param;
	int			length;
3340
	int			asconf_ack_len;
3341
	__be16			err_code;
L
Linus Torvalds 已提交
3342 3343 3344 3345 3346 3347

	if (no_err)
		err_code = SCTP_ERROR_NO_ERROR;
	else
		err_code = SCTP_ERROR_REQ_REFUSED;

3348 3349 3350
	asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
			     sizeof(sctp_chunkhdr_t);

L
Linus Torvalds 已提交
3351 3352
	/* Skip the addiphdr from the asconf_ack chunk and store a pointer to
	 * the first asconf_ack parameter.
3353
	 */
L
Linus Torvalds 已提交
3354 3355 3356 3357 3358 3359 3360
	length = sizeof(sctp_addiphdr_t);
	asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
						  length);
	asconf_ack_len -= length;

	while (asconf_ack_len > 0) {
		if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3361
			switch (asconf_ack_param->param_hdr.type) {
L
Linus Torvalds 已提交
3362 3363 3364 3365
			case SCTP_PARAM_SUCCESS_REPORT:
				return SCTP_ERROR_NO_ERROR;
			case SCTP_PARAM_ERR_CAUSE:
				length = sizeof(sctp_addip_param_t);
J
Joe Perches 已提交
3366
				err_param = (void *)asconf_ack_param + length;
L
Linus Torvalds 已提交
3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378
				asconf_ack_len -= length;
				if (asconf_ack_len > 0)
					return err_param->cause;
				else
					return SCTP_ERROR_INV_PARAM;
				break;
			default:
				return SCTP_ERROR_INV_PARAM;
			}
		}

		length = ntohs(asconf_ack_param->param_hdr.length);
J
Joe Perches 已提交
3379
		asconf_ack_param = (void *)asconf_ack_param + length;
L
Linus Torvalds 已提交
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
		asconf_ack_len -= length;
	}

	return err_code;
}

/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
int sctp_process_asconf_ack(struct sctp_association *asoc,
			    struct sctp_chunk *asconf_ack)
{
	struct sctp_chunk	*asconf = asoc->addip_last_asconf;
	union sctp_addr_param	*addr_param;
	sctp_addip_param_t	*asconf_param;
	int	length = 0;
	int	asconf_len = asconf->skb->len;
	int	all_param_pass = 0;
	int	no_err = 1;
	int	retval = 0;
3398
	__be16	err_code = SCTP_ERROR_NO_ERROR;
L
Linus Torvalds 已提交
3399 3400 3401

	/* Skip the chunkhdr and addiphdr from the last asconf sent and store
	 * a pointer to address parameter.
3402
	 */
L
Linus Torvalds 已提交
3403 3404 3405 3406 3407
	length = sizeof(sctp_addip_chunk_t);
	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
	asconf_len -= length;

	/* Skip the address parameter in the last asconf sent and store a
J
Joe Perches 已提交
3408
	 * pointer to the first asconf parameter.
3409
	 */
3410
	length = ntohs(addr_param->p.length);
J
Joe Perches 已提交
3411
	asconf_param = (void *)addr_param + length;
L
Linus Torvalds 已提交
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
	asconf_len -= length;

	/* ADDIP 4.1
	 * A8) If there is no response(s) to specific TLV parameter(s), and no
	 * failures are indicated, then all request(s) are considered
	 * successful.
	 */
	if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
		all_param_pass = 1;

	/* Process the TLVs contained in the last sent ASCONF chunk. */
	while (asconf_len > 0) {
		if (all_param_pass)
			err_code = SCTP_ERROR_NO_ERROR;
		else {
			err_code = sctp_get_asconf_response(asconf_ack,
							    asconf_param,
							    no_err);
			if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
				no_err = 0;
		}

		switch (err_code) {
		case SCTP_ERROR_NO_ERROR:
3436
			sctp_asconf_param_success(asoc, asconf_param);
L
Linus Torvalds 已提交
3437 3438 3439 3440 3441 3442
			break;

		case SCTP_ERROR_RSRC_LOW:
			retval = 1;
			break;

3443
		case SCTP_ERROR_UNKNOWN_PARAM:
L
Linus Torvalds 已提交
3444 3445
			/* Disable sending this type of asconf parameter in
			 * future.
3446
			 */
L
Linus Torvalds 已提交
3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459
			asoc->peer.addip_disabled_mask |=
				asconf_param->param_hdr.type;
			break;

		case SCTP_ERROR_REQ_REFUSED:
		case SCTP_ERROR_DEL_LAST_IP:
		case SCTP_ERROR_DEL_SRC_IP:
		default:
			 break;
		}

		/* Skip the processed asconf parameter and move to the next
		 * one.
3460
		 */
L
Linus Torvalds 已提交
3461
		length = ntohs(asconf_param->param_hdr.length);
J
Joe Perches 已提交
3462
		asconf_param = (void *)asconf_param + length;
L
Linus Torvalds 已提交
3463 3464 3465
		asconf_len -= length;
	}

3466
	if (no_err && asoc->src_out_of_asoc_ok) {
3467
		asoc->src_out_of_asoc_ok = 0;
3468 3469
		sctp_transport_immediate_rtx(asoc->peer.primary_path);
	}
3470

L
Linus Torvalds 已提交
3471
	/* Free the cached last sent asconf chunk. */
3472
	list_del_init(&asconf->transmitted_list);
L
Linus Torvalds 已提交
3473 3474 3475 3476 3477 3478
	sctp_chunk_free(asconf);
	asoc->addip_last_asconf = NULL;

	return retval;
}

3479
/* Make a FWD TSN chunk. */
L
Linus Torvalds 已提交
3480 3481 3482 3483 3484
struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
				    __u32 new_cum_tsn, size_t nstreams,
				    struct sctp_fwdtsn_skip *skiplist)
{
	struct sctp_chunk *retval = NULL;
3485
	struct sctp_fwdtsn_hdr ftsn_hdr;
L
Linus Torvalds 已提交
3486 3487 3488 3489 3490 3491
	struct sctp_fwdtsn_skip skip;
	size_t hint;
	int i;

	hint = (nstreams + 1) * sizeof(__u32);

3492
	retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint);
L
Linus Torvalds 已提交
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508

	if (!retval)
		return NULL;

	ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
	retval->subh.fwdtsn_hdr =
		sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);

	for (i = 0; i < nstreams; i++) {
		skip.stream = skiplist[i].stream;
		skip.ssn = skiplist[i].ssn;
		sctp_addto_chunk(retval, sizeof(skip), &skip);
	}

	return retval;
}