1 /* SCTP kernel reference Implementation
2  * (C) Copyright IBM Corp. 2001, 2003
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001-2002 Intel Corp.
6  *
7  * This file is part of the SCTP kernel reference Implementation
8  *
9  * These functions work with the state functions in sctp_sm_statefuns.c
10  * to implement the state operations.  These functions implement the
11  * steps which require modifying existing data structures.
12  *
13  * The SCTP reference implementation is free software;
14  * you can redistribute it and/or modify it under the terms of
15  * the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * The SCTP reference implementation is distributed in the hope that it
20  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21  *                 ************************
22  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  * See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with GNU CC; see the file COPYING.  If not, write to
27  * the Free Software Foundation, 59 Temple Place - Suite 330,
28  * Boston, MA 02111-1307, USA.
29  *
30  * Please send any bug reports or fixes you make to the
31  * email address(es):
32  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
33  *
34  * Or submit a bug report through the following website:
35  *    http://www.sf.net/projects/lksctp
36  *
37  * Written or modified by:
38  *    La Monte H.P. Yarroll <piggy@acm.org>
39  *    Karl Knutson          <karl@athena.chicago.il.us>
40  *    C. Robin              <chris@hundredacre.ac.uk>
41  *    Jon Grimm             <jgrimm@us.ibm.com>
42  *    Xingang Guo           <xingang.guo@intel.com>
43  *    Dajiang Zhang	    <dajiang.zhang@nokia.com>
44  *    Sridhar Samudrala	    <sri@us.ibm.com>
45  *    Daisy Chang	    <daisyc@us.ibm.com>
46  *    Ardelle Fan	    <ardelle.fan@intel.com>
47  *    Kevin Gao             <kevin.gao@intel.com>
48  *
49  * Any bugs reported given to us we will try to fix... any fixes shared will
50  * be incorporated into the next SCTP release.
51  */
52 
53 #include <linux/types.h>
54 #include <linux/kernel.h>
55 #include <linux/ip.h>
56 #include <linux/ipv6.h>
57 #include <linux/net.h>
58 #include <linux/inet.h>
59 #include <asm/scatterlist.h>
60 #include <linux/crypto.h>
61 #include <net/sock.h>
62 
63 #include <linux/skbuff.h>
64 #include <linux/random.h>	/* for get_random_bytes */
65 #include <net/sctp/sctp.h>
66 #include <net/sctp/sm.h>
67 
68 extern kmem_cache_t *sctp_chunk_cachep;
69 
70 SCTP_STATIC
71 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
72 				   __u8 type, __u8 flags, int paylen);
73 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
74 					const struct sctp_association *asoc,
75 					const struct sctp_chunk *init_chunk,
76 					int *cookie_len,
77 					const __u8 *raw_addrs, int addrs_len);
78 static int sctp_process_param(struct sctp_association *asoc,
79 			      union sctp_params param,
80 			      const union sctp_addr *peer_addr,
81 			      int gfp);
82 
83 /* What was the inbound interface for this chunk? */
sctp_chunk_iif(const struct sctp_chunk * chunk)84 int sctp_chunk_iif(const struct sctp_chunk *chunk)
85 {
86 	struct sctp_af *af;
87 	int iif = 0;
88 
89 	af = sctp_get_af_specific(ipver2af(chunk->skb->nh.iph->version));
90 	if (af)
91 		iif = af->skb_iif(chunk->skb);
92 
93 	return iif;
94 }
95 
96 /* RFC 2960 3.3.2 Initiation (INIT) (1)
97  *
98  * Note 2: The ECN capable field is reserved for future use of
99  * Explicit Congestion Notification.
100  */
101 static const struct sctp_paramhdr ecap_param = {
102 	SCTP_PARAM_ECN_CAPABLE,
103 	__constant_htons(sizeof(struct sctp_paramhdr)),
104 };
105 static const struct sctp_paramhdr prsctp_param = {
106 	SCTP_PARAM_FWD_TSN_SUPPORT,
107 	__constant_htons(sizeof(struct sctp_paramhdr)),
108 };
109 
110 /* A helper to initialize to initialize an op error inside a
111  * provided chunk, as most cause codes will be embedded inside an
112  * abort chunk.
113  */
sctp_init_cause(struct sctp_chunk * chunk,__u16 cause_code,const void * payload,size_t paylen)114 void  sctp_init_cause(struct sctp_chunk *chunk, __u16 cause_code,
115 		      const void *payload, size_t paylen)
116 {
117 	sctp_errhdr_t err;
118 	int padlen;
119 	__u16 len;
120 
121         /* Cause code constants are now defined in network order.  */
122 	err.cause = cause_code;
123 	len = sizeof(sctp_errhdr_t) + paylen;
124 	padlen = len % 4;
125 	err.length  = htons(len);
126 	len += padlen;
127 	sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
128 	chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
129 }
130 
131 /* 3.3.2 Initiation (INIT) (1)
132  *
133  * This chunk is used to initiate a SCTP association between two
134  * endpoints. The format of the INIT chunk is shown below:
135  *
136  *     0                   1                   2                   3
137  *     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
138  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139  *    |   Type = 1    |  Chunk Flags  |      Chunk Length             |
140  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141  *    |                         Initiate Tag                          |
142  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143  *    |           Advertised Receiver Window Credit (a_rwnd)          |
144  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145  *    |  Number of Outbound Streams   |  Number of Inbound Streams    |
146  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147  *    |                          Initial TSN                          |
148  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149  *    \                                                               \
150  *    /              Optional/Variable-Length Parameters              /
151  *    \                                                               \
152  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
153  *
154  *
155  * The INIT chunk contains the following parameters. Unless otherwise
156  * noted, each parameter MUST only be included once in the INIT chunk.
157  *
158  * Fixed Parameters                     Status
159  * ----------------------------------------------
160  * Initiate Tag                        Mandatory
161  * Advertised Receiver Window Credit   Mandatory
162  * Number of Outbound Streams          Mandatory
163  * Number of Inbound Streams           Mandatory
164  * Initial TSN                         Mandatory
165  *
166  * Variable Parameters                  Status     Type Value
167  * -------------------------------------------------------------
168  * IPv4 Address (Note 1)               Optional    5
169  * IPv6 Address (Note 1)               Optional    6
170  * Cookie Preservative                 Optional    9
171  * Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
172  * Host Name Address (Note 3)          Optional    11
173  * Supported Address Types (Note 4)    Optional    12
174  */
sctp_make_init(const struct sctp_association * asoc,const struct sctp_bind_addr * bp,int gfp,int vparam_len)175 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
176 			     const struct sctp_bind_addr *bp,
177 			     int gfp, int vparam_len)
178 {
179 	sctp_inithdr_t init;
180 	union sctp_params addrs;
181 	size_t chunksize;
182 	struct sctp_chunk *retval = NULL;
183 	int num_types, addrs_len = 0;
184 	struct sctp_opt *sp;
185 	sctp_supported_addrs_param_t sat;
186 	__u16 types[2];
187 	sctp_adaption_ind_param_t aiparam;
188 
189 	/* RFC 2960 3.3.2 Initiation (INIT) (1)
190 	 *
191 	 * Note 1: The INIT chunks can contain multiple addresses that
192 	 * can be IPv4 and/or IPv6 in any combination.
193 	 */
194 	retval = NULL;
195 
196 	/* Convert the provided bind address list to raw format. */
197 	addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
198 
199 	init.init_tag		   = htonl(asoc->c.my_vtag);
200 	init.a_rwnd		   = htonl(asoc->rwnd);
201 	init.num_outbound_streams  = htons(asoc->c.sinit_num_ostreams);
202 	init.num_inbound_streams   = htons(asoc->c.sinit_max_instreams);
203 	init.initial_tsn	   = htonl(asoc->c.initial_tsn);
204 
205 	/* How many address types are needed? */
206 	sp = sctp_sk(asoc->base.sk);
207 	num_types = sp->pf->supported_addrs(sp, types);
208 
209 	chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
210 	chunksize += sizeof(ecap_param);
211 	if (sctp_prsctp_enable)
212 		chunksize += sizeof(prsctp_param);
213 	chunksize += sizeof(aiparam);
214 	chunksize += vparam_len;
215 
216 	/* RFC 2960 3.3.2 Initiation (INIT) (1)
217 	 *
218 	 * Note 3: An INIT chunk MUST NOT contain more than one Host
219 	 * Name address parameter. Moreover, the sender of the INIT
220 	 * MUST NOT combine any other address types with the Host Name
221 	 * address in the INIT. The receiver of INIT MUST ignore any
222 	 * other address types if the Host Name address parameter is
223 	 * present in the received INIT chunk.
224 	 *
225 	 * PLEASE DO NOT FIXME [This version does not support Host Name.]
226 	 */
227 
228 	retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
229 	if (!retval)
230 		goto nodata;
231 
232 	retval->subh.init_hdr =
233 		sctp_addto_chunk(retval, sizeof(init), &init);
234 	retval->param_hdr.v =
235 		sctp_addto_chunk(retval, addrs_len, addrs.v);
236 
237 	/* RFC 2960 3.3.2 Initiation (INIT) (1)
238 	 *
239 	 * Note 4: This parameter, when present, specifies all the
240 	 * address types the sending endpoint can support. The absence
241 	 * of this parameter indicates that the sending endpoint can
242 	 * support any address type.
243 	 */
244 	sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
245 	sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
246 	sctp_addto_chunk(retval, sizeof(sat), &sat);
247 	sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
248 
249 	sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
250 	if (sctp_prsctp_enable)
251 		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
252 	aiparam.param_hdr.type = SCTP_PARAM_ADAPTION_LAYER_IND;
253 	aiparam.param_hdr.length = htons(sizeof(aiparam));
254 	aiparam.adaption_ind = htonl(sp->adaption_ind);
255 	sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
256 nodata:
257 	if (addrs.v)
258 		kfree(addrs.v);
259 	return retval;
260 }
261 
sctp_make_init_ack(const struct sctp_association * asoc,const struct sctp_chunk * chunk,int gfp,int unkparam_len)262 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
263 				 const struct sctp_chunk *chunk,
264 				 int gfp, int unkparam_len)
265 {
266 	sctp_inithdr_t initack;
267 	struct sctp_chunk *retval;
268 	union sctp_params addrs;
269 	int addrs_len;
270 	sctp_cookie_param_t *cookie;
271 	int cookie_len;
272 	size_t chunksize;
273 	sctp_adaption_ind_param_t aiparam;
274 
275 	retval = NULL;
276 
277 	/* Note: there may be no addresses to embed. */
278 	addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
279 
280 	initack.init_tag	        = htonl(asoc->c.my_vtag);
281 	initack.a_rwnd			= htonl(asoc->rwnd);
282 	initack.num_outbound_streams	= htons(asoc->c.sinit_num_ostreams);
283 	initack.num_inbound_streams	= htons(asoc->c.sinit_max_instreams);
284 	initack.initial_tsn		= htonl(asoc->c.initial_tsn);
285 
286 	/* FIXME:  We really ought to build the cookie right
287 	 * into the packet instead of allocating more fresh memory.
288 	 */
289 	cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
290 				  addrs.v, addrs_len);
291 	if (!cookie)
292 		goto nomem_cookie;
293 
294 	/* Calculate the total size of allocation, include the reserved
295 	 * space for reporting unknown parameters if it is specified.
296 	 */
297 	chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
298 
299         /* Tell peer that we'll do ECN only if peer advertised such cap.  */
300 	if (asoc->peer.ecn_capable)
301 		chunksize += sizeof(ecap_param);
302 
303         /* Tell peer that we'll do PR-SCTP only if peer advertised.  */
304 	if (asoc->peer.prsctp_capable)
305 		chunksize += sizeof(prsctp_param);
306 
307 	chunksize += sizeof(aiparam);
308 
309 	/* Now allocate and fill out the chunk.  */
310 	retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
311 	if (!retval)
312 		goto nomem_chunk;
313 
314 	/* Per the advice in RFC 2960 6.4, send this reply to
315 	 * the source of the INIT packet.
316 	 */
317 	retval->transport = chunk->transport;
318 	retval->subh.init_hdr =
319 		sctp_addto_chunk(retval, sizeof(initack), &initack);
320 	retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
321 	sctp_addto_chunk(retval, cookie_len, cookie);
322 	if (asoc->peer.ecn_capable)
323 		sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
324 	if (asoc->peer.prsctp_capable)
325 		sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
326 
327 	aiparam.param_hdr.type = SCTP_PARAM_ADAPTION_LAYER_IND;
328 	aiparam.param_hdr.length = htons(sizeof(aiparam));
329 	aiparam.adaption_ind = htonl(sctp_sk(asoc->base.sk)->adaption_ind);
330 	sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
331 
332 	/* We need to remove the const qualifier at this point.  */
333 	retval->asoc = (struct sctp_association *) asoc;
334 
335 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
336 	 *
337 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
338 	 * HEARTBEAT ACK, * etc.) to the same destination transport
339 	 * address from which it received the DATA or control chunk
340 	 * to which it is replying.
341 	 *
342 	 * [INIT ACK back to where the INIT came from.]
343 	 */
344 	if (chunk)
345 		retval->transport = chunk->transport;
346 
347 nomem_chunk:
348 	kfree(cookie);
349 nomem_cookie:
350 	if (addrs.v)
351 		kfree(addrs.v);
352 	return retval;
353 }
354 
355 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
356  *
357  * This chunk is used only during the initialization of an association.
358  * It is sent by the initiator of an association to its peer to complete
359  * the initialization process. This chunk MUST precede any DATA chunk
360  * sent within the association, but MAY be bundled with one or more DATA
361  * chunks in the same packet.
362  *
363  *      0                   1                   2                   3
364  *      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
365  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366  *     |   Type = 10   |Chunk  Flags   |         Length                |
367  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
368  *     /                     Cookie                                    /
369  *     \                                                               \
370  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371  *
372  * Chunk Flags: 8 bit
373  *
374  *   Set to zero on transmit and ignored on receipt.
375  *
376  * Length: 16 bits (unsigned integer)
377  *
378  *   Set to the size of the chunk in bytes, including the 4 bytes of
379  *   the chunk header and the size of the Cookie.
380  *
381  * Cookie: variable size
382  *
383  *   This field must contain the exact cookie received in the
384  *   State Cookie parameter from the previous INIT ACK.
385  *
386  *   An implementation SHOULD make the cookie as small as possible
387  *   to insure interoperability.
388  */
sctp_make_cookie_echo(const struct sctp_association * asoc,const struct sctp_chunk * chunk)389 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
390 				    const struct sctp_chunk *chunk)
391 {
392 	struct sctp_chunk *retval;
393 	void *cookie;
394 	int cookie_len;
395 
396 	cookie = asoc->peer.cookie;
397 	cookie_len = asoc->peer.cookie_len;
398 
399 	/* Build a cookie echo chunk.  */
400 	retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
401 	if (!retval)
402 		goto nodata;
403 	retval->subh.cookie_hdr =
404 		sctp_addto_chunk(retval, cookie_len, cookie);
405 
406 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
407 	 *
408 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
409 	 * HEARTBEAT ACK, * etc.) to the same destination transport
410 	 * address from which it * received the DATA or control chunk
411 	 * to which it is replying.
412 	 *
413 	 * [COOKIE ECHO back to where the INIT ACK came from.]
414 	 */
415 	if (chunk)
416 		retval->transport = chunk->transport;
417 
418 nodata:
419 	return retval;
420 }
421 
422 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
423  *
424  * This chunk is used only during the initialization of an
425  * association.  It is used to acknowledge the receipt of a COOKIE
426  * ECHO chunk.  This chunk MUST precede any DATA or SACK chunk sent
427  * within the association, but MAY be bundled with one or more DATA
428  * chunks or SACK chunk in the same SCTP packet.
429  *
430  *      0                   1                   2                   3
431  *      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
432  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433  *     |   Type = 11   |Chunk  Flags   |     Length = 4                |
434  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
435  *
436  * Chunk Flags: 8 bits
437  *
438  *   Set to zero on transmit and ignored on receipt.
439  */
sctp_make_cookie_ack(const struct sctp_association * asoc,const struct sctp_chunk * chunk)440 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
441 				   const struct sctp_chunk *chunk)
442 {
443 	struct sctp_chunk *retval;
444 
445 	retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
446 
447 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
448 	 *
449 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
450 	 * HEARTBEAT ACK, * etc.) to the same destination transport
451 	 * address from which it * received the DATA or control chunk
452 	 * to which it is replying.
453 	 *
454 	 * [COOKIE ACK back to where the COOKIE ECHO came from.]
455 	 */
456 	if (retval && chunk)
457 		retval->transport = chunk->transport;
458 
459 	return retval;
460 }
461 
462 /*
463  *  Appendix A: Explicit Congestion Notification:
464  *  CWR:
465  *
466  *  RFC 2481 details a specific bit for a sender to send in the header of
467  *  its next outbound TCP segment to indicate to its peer that it has
468  *  reduced its congestion window.  This is termed the CWR bit.  For
469  *  SCTP the same indication is made by including the CWR chunk.
470  *  This chunk contains one data element, i.e. the TSN number that
471  *  was sent in the ECNE chunk.  This element represents the lowest
472  *  TSN number in the datagram that was originally marked with the
473  *  CE bit.
474  *
475  *     0                   1                   2                   3
476  *     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
477  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
478  *    | Chunk Type=13 | Flags=00000000|    Chunk Length = 8           |
479  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
480  *    |                      Lowest TSN Number                        |
481  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
482  *
483  *     Note: The CWR is considered a Control chunk.
484  */
sctp_make_cwr(const struct sctp_association * asoc,const __u32 lowest_tsn,const struct sctp_chunk * chunk)485 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
486 			    const __u32 lowest_tsn,
487 			    const struct sctp_chunk *chunk)
488 {
489 	struct sctp_chunk *retval;
490 	sctp_cwrhdr_t cwr;
491 
492 	cwr.lowest_tsn = htonl(lowest_tsn);
493 	retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
494 				 sizeof(sctp_cwrhdr_t));
495 
496 	if (!retval)
497 		goto nodata;
498 
499 	retval->subh.ecn_cwr_hdr =
500 		sctp_addto_chunk(retval, sizeof(cwr), &cwr);
501 
502 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
503 	 *
504 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
505 	 * HEARTBEAT ACK, * etc.) to the same destination transport
506 	 * address from which it * received the DATA or control chunk
507 	 * to which it is replying.
508 	 *
509 	 * [Report a reduced congestion window back to where the ECNE
510 	 * came from.]
511 	 */
512 	if (chunk)
513 		retval->transport = chunk->transport;
514 
515 nodata:
516 	return retval;
517 }
518 
519 /* Make an ECNE chunk.  This is a congestion experienced report.  */
sctp_make_ecne(const struct sctp_association * asoc,const __u32 lowest_tsn)520 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
521 			     const __u32 lowest_tsn)
522 {
523 	struct sctp_chunk *retval;
524 	sctp_ecnehdr_t ecne;
525 
526 	ecne.lowest_tsn = htonl(lowest_tsn);
527 	retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
528 				 sizeof(sctp_ecnehdr_t));
529 	if (!retval)
530 		goto nodata;
531 	retval->subh.ecne_hdr =
532 		sctp_addto_chunk(retval, sizeof(ecne), &ecne);
533 
534 nodata:
535 	return retval;
536 }
537 
538 /* Make a DATA chunk for the given association from the provided
539  * parameters.  However, do not populate the data payload.
540  */
sctp_make_datafrag_empty(struct sctp_association * asoc,const struct sctp_sndrcvinfo * sinfo,int data_len,__u8 flags,__u16 ssn)541 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
542 				       const struct sctp_sndrcvinfo *sinfo,
543 				       int data_len, __u8 flags, __u16 ssn)
544 {
545 	struct sctp_chunk *retval;
546 	struct sctp_datahdr dp;
547 	int chunk_len;
548 
549 	/* We assign the TSN as LATE as possible, not here when
550 	 * creating the chunk.
551 	 */
552 	dp.tsn = 0;
553 	dp.stream = htons(sinfo->sinfo_stream);
554 	dp.ppid   = sinfo->sinfo_ppid;
555 
556 	/* Set the flags for an unordered send.  */
557 	if (sinfo->sinfo_flags & MSG_UNORDERED) {
558 		flags |= SCTP_DATA_UNORDERED;
559 		dp.ssn = 0;
560 	} else
561 		dp.ssn = htons(ssn);
562 
563 	chunk_len = sizeof(dp) + data_len;
564 	retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
565 	if (!retval)
566 		goto nodata;
567 
568 	retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
569 	memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
570 
571 nodata:
572 	return retval;
573 }
574 
575 /* Create a selective ackowledgement (SACK) for the given
576  * association.  This reports on which TSN's we've seen to date,
577  * including duplicates and gaps.
578  */
sctp_make_sack(const struct sctp_association * asoc)579 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
580 {
581 	struct sctp_chunk *retval;
582 	struct sctp_sackhdr sack;
583 	int len;
584 	__u32 ctsn;
585 	__u16 num_gabs, num_dup_tsns;
586 	struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
587 
588 	ctsn = sctp_tsnmap_get_ctsn(map);
589 	SCTP_DEBUG_PRINTK("sackCTSNAck sent:  0x%x.\n", ctsn);
590 
591 	/* How much room is needed in the chunk? */
592 	num_gabs = sctp_tsnmap_num_gabs(map);
593 	num_dup_tsns = sctp_tsnmap_num_dups(map);
594 
595 	/* Initialize the SACK header.  */
596 	sack.cum_tsn_ack	    = htonl(ctsn);
597 	sack.a_rwnd 		    = htonl(asoc->a_rwnd);
598 	sack.num_gap_ack_blocks     = htons(num_gabs);
599 	sack.num_dup_tsns           = htons(num_dup_tsns);
600 
601 	len = sizeof(sack)
602 		+ sizeof(struct sctp_gap_ack_block) * num_gabs
603 		+ sizeof(__u32) * num_dup_tsns;
604 
605 	/* Create the chunk.  */
606 	retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
607 	if (!retval)
608 		goto nodata;
609 
610 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
611 	 *
612 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
613 	 * HEARTBEAT ACK, etc.) to the same destination transport
614 	 * address from which it received the DATA or control chunk to
615 	 * which it is replying.  This rule should also be followed if
616 	 * the endpoint is bundling DATA chunks together with the
617 	 * reply chunk.
618 	 *
619 	 * However, when acknowledging multiple DATA chunks received
620 	 * in packets from different source addresses in a single
621 	 * SACK, the SACK chunk may be transmitted to one of the
622 	 * destination transport addresses from which the DATA or
623 	 * control chunks being acknowledged were received.
624 	 *
625 	 * [BUG:  We do not implement the following paragraph.
626 	 * Perhaps we should remember the last transport we used for a
627 	 * SACK and avoid that (if possible) if we have seen any
628 	 * duplicates. --piggy]
629 	 *
630 	 * When a receiver of a duplicate DATA chunk sends a SACK to a
631 	 * multi- homed endpoint it MAY be beneficial to vary the
632 	 * destination address and not use the source address of the
633 	 * DATA chunk.  The reason being that receiving a duplicate
634 	 * from a multi-homed endpoint might indicate that the return
635 	 * path (as specified in the source address of the DATA chunk)
636 	 * for the SACK is broken.
637 	 *
638 	 * [Send to the address from which we last received a DATA chunk.]
639 	 */
640 	retval->transport = asoc->peer.last_data_from;
641 
642 	retval->subh.sack_hdr =
643 		sctp_addto_chunk(retval, sizeof(sack), &sack);
644 
645 	/* Add the gap ack block information.   */
646 	if (num_gabs)
647 		sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
648 				 sctp_tsnmap_get_gabs(map));
649 
650 	/* Add the duplicate TSN information.  */
651 	if (num_dup_tsns)
652 		sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
653 				 sctp_tsnmap_get_dups(map));
654 
655 nodata:
656 	return retval;
657 }
658 
659 /* Make a SHUTDOWN chunk. */
sctp_make_shutdown(const struct sctp_association * asoc,const struct sctp_chunk * chunk)660 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
661 				      const struct sctp_chunk *chunk)
662 {
663 	struct sctp_chunk *retval;
664 	sctp_shutdownhdr_t shut;
665 	__u32 ctsn;
666 
667 	ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
668 	shut.cum_tsn_ack = htonl(ctsn);
669 
670 	retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
671 				 sizeof(sctp_shutdownhdr_t));
672 	if (!retval)
673 		goto nodata;
674 
675 	retval->subh.shutdown_hdr =
676 		sctp_addto_chunk(retval, sizeof(shut), &shut);
677 
678 	if (chunk)
679 		retval->transport = chunk->transport;
680 nodata:
681 	return retval;
682 }
683 
sctp_make_shutdown_ack(const struct sctp_association * asoc,const struct sctp_chunk * chunk)684 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
685 				     const struct sctp_chunk *chunk)
686 {
687 	struct sctp_chunk *retval;
688 
689 	retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
690 
691 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
692 	 *
693 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
694 	 * HEARTBEAT ACK, * etc.) to the same destination transport
695 	 * address from which it * received the DATA or control chunk
696 	 * to which it is replying.
697 	 *
698 	 * [ACK back to where the SHUTDOWN came from.]
699 	 */
700 	if (retval && chunk)
701 		retval->transport = chunk->transport;
702 
703 	return retval;
704 }
705 
sctp_make_shutdown_complete(const struct sctp_association * asoc,const struct sctp_chunk * chunk)706 struct sctp_chunk *sctp_make_shutdown_complete(
707 	const struct sctp_association *asoc,
708 	const struct sctp_chunk *chunk)
709 {
710 	struct sctp_chunk *retval;
711 	__u8 flags = 0;
712 
713 	/* Maybe set the T-bit if we have no association. */
714 	flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
715 
716 	retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
717 
718 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
719 	 *
720 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
721 	 * HEARTBEAT ACK, * etc.) to the same destination transport
722 	 * address from which it * received the DATA or control chunk
723 	 * to which it is replying.
724 	 *
725 	 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
726 	 * came from.]
727 	 */
728 	if (retval && chunk)
729 		retval->transport = chunk->transport;
730 
731         return retval;
732 }
733 
734 /* Create an ABORT.  Note that we set the T bit if we have no
735  * association.
736  */
sctp_make_abort(const struct sctp_association * asoc,const struct sctp_chunk * chunk,const size_t hint)737 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
738 			      const struct sctp_chunk *chunk,
739 			      const size_t hint)
740 {
741 	struct sctp_chunk *retval;
742 	__u8 flags = 0;
743 
744 	/* Maybe set the T-bit if we have no association.  */
745 	flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
746 
747 	retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
748 
749 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
750 	 *
751 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
752 	 * HEARTBEAT ACK, * etc.) to the same destination transport
753 	 * address from which it * received the DATA or control chunk
754 	 * to which it is replying.
755 	 *
756 	 * [ABORT back to where the offender came from.]
757 	 */
758 	if (retval && chunk)
759 		retval->transport = chunk->transport;
760 
761 	return retval;
762 }
763 
764 /* Helper to create ABORT with a NO_USER_DATA error.  */
sctp_make_abort_no_data(const struct sctp_association * asoc,const struct sctp_chunk * chunk,__u32 tsn)765 struct sctp_chunk *sctp_make_abort_no_data(
766 	const struct sctp_association *asoc,
767 	const struct sctp_chunk *chunk, __u32 tsn)
768 {
769 	struct sctp_chunk *retval;
770 	__u32 payload;
771 
772 	retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
773 				 + sizeof(tsn));
774 
775 	if (!retval)
776 		goto no_mem;
777 
778 	/* Put the tsn back into network byte order.  */
779 	payload = htonl(tsn);
780 	sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
781 			sizeof(payload));
782 
783 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
784 	 *
785 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
786 	 * HEARTBEAT ACK, * etc.) to the same destination transport
787 	 * address from which it * received the DATA or control chunk
788 	 * to which it is replying.
789 	 *
790 	 * [ABORT back to where the offender came from.]
791 	 */
792 	if (chunk)
793 		retval->transport = chunk->transport;
794 
795 no_mem:
796 	return retval;
797 }
798 
799 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error.  */
sctp_make_abort_user(const struct sctp_association * asoc,const struct msghdr * msg,size_t paylen)800 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
801 					const struct msghdr *msg,
802 					size_t paylen)
803 {
804 	struct sctp_chunk *retval;
805 	void *payload = NULL;
806 	int err;
807 
808 	retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
809 	if (!retval)
810 		goto err_chunk;
811 
812 	if (paylen) {
813 		/* Put the msg_iov together into payload.  */
814 		payload = kmalloc(paylen, GFP_KERNEL);
815 		if (!payload)
816 			goto err_payload;
817 
818 		err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
819 		if (err < 0)
820 			goto err_copy;
821 	}
822 
823 	sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
824 
825 	if (paylen)
826 		kfree(payload);
827 
828 	return retval;
829 
830 err_copy:
831 	kfree(payload);
832 err_payload:
833 	sctp_chunk_free(retval);
834 	retval = NULL;
835 err_chunk:
836 	return retval;
837 }
838 
839 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
sctp_make_abort_violation(const struct sctp_association * asoc,const struct sctp_chunk * chunk,const __u8 * payload,const size_t paylen)840 struct sctp_chunk *sctp_make_abort_violation(
841 	const struct sctp_association *asoc,
842 	const struct sctp_chunk *chunk,
843 	const __u8   *payload,
844 	const size_t paylen)
845 {
846 	struct sctp_chunk  *retval;
847 	struct sctp_paramhdr phdr;
848 
849 	retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
850 					+ sizeof(sctp_chunkhdr_t));
851 	if (!retval)
852 		goto end;
853 
854 	sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, payload, paylen);
855 
856 	phdr.type = htons(chunk->chunk_hdr->type);
857 	phdr.length = chunk->chunk_hdr->length;
858 	sctp_addto_chunk(retval, sizeof(sctp_paramhdr_t), &phdr);
859 
860 end:
861 	return retval;
862 }
863 
864 /* Make a HEARTBEAT chunk.  */
sctp_make_heartbeat(const struct sctp_association * asoc,const struct sctp_transport * transport,const void * payload,const size_t paylen)865 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
866 				  const struct sctp_transport *transport,
867 				  const void *payload, const size_t paylen)
868 {
869 	struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
870 						    0, paylen);
871 
872 	if (!retval)
873 		goto nodata;
874 
875 	/* Cast away the 'const', as this is just telling the chunk
876 	 * what transport it belongs to.
877 	 */
878 	retval->transport = (struct sctp_transport *) transport;
879 	retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
880 
881 nodata:
882 	return retval;
883 }
884 
sctp_make_heartbeat_ack(const struct sctp_association * asoc,const struct sctp_chunk * chunk,const void * payload,const size_t paylen)885 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
886 				      const struct sctp_chunk *chunk,
887 				      const void *payload, const size_t paylen)
888 {
889 	struct sctp_chunk *retval;
890 
891 	retval  = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
892 	if (!retval)
893 		goto nodata;
894 
895 	retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
896 
897 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
898 	 *
899 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
900 	 * HEARTBEAT ACK, * etc.) to the same destination transport
901 	 * address from which it * received the DATA or control chunk
902 	 * to which it is replying.
903 	 *
904 	 * [HBACK back to where the HEARTBEAT came from.]
905 	 */
906 	if (chunk)
907 		retval->transport = chunk->transport;
908 
909 nodata:
910 	return retval;
911 }
912 
913 /* Create an Operation Error chunk with the specified space reserved.
914  * This routine can be used for containing multiple causes in the chunk.
915  */
sctp_make_op_error_space(const struct sctp_association * asoc,const struct sctp_chunk * chunk,size_t size)916 static struct sctp_chunk *sctp_make_op_error_space(
917 	const struct sctp_association *asoc,
918 	const struct sctp_chunk *chunk,
919 	size_t size)
920 {
921 	struct sctp_chunk *retval;
922 
923 	retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
924 				 sizeof(sctp_errhdr_t) + size);
925 	if (!retval)
926 		goto nodata;
927 
928 	/* RFC 2960 6.4 Multi-homed SCTP Endpoints
929 	 *
930 	 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
931 	 * HEARTBEAT ACK, etc.) to the same destination transport
932 	 * address from which it received the DATA or control chunk
933 	 * to which it is replying.
934 	 *
935 	 */
936 	if (chunk)
937 		retval->transport = chunk->transport;
938 
939 nodata:
940 	return retval;
941 }
942 
943 /* Create an Operation Error chunk.  */
sctp_make_op_error(const struct sctp_association * asoc,const struct sctp_chunk * chunk,__u16 cause_code,const void * payload,size_t paylen)944 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
945 				 const struct sctp_chunk *chunk,
946 				 __u16 cause_code, const void *payload,
947 				 size_t paylen)
948 {
949 	struct sctp_chunk *retval;
950 
951 	retval = sctp_make_op_error_space(asoc, chunk, paylen);
952 	if (!retval)
953 		goto nodata;
954 
955 	sctp_init_cause(retval, cause_code, payload, paylen);
956 
957 nodata:
958 	return retval;
959 }
960 
961 /********************************************************************
962  * 2nd Level Abstractions
963  ********************************************************************/
964 
965 /* Turn an skb into a chunk.
966  * FIXME: Eventually move the structure directly inside the skb->cb[].
967  */
sctp_chunkify(struct sk_buff * skb,const struct sctp_association * asoc,struct sock * sk)968 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
969 			    const struct sctp_association *asoc,
970 			    struct sock *sk)
971 {
972 	struct sctp_chunk *retval;
973 
974 	retval = kmem_cache_alloc(sctp_chunk_cachep, SLAB_ATOMIC);
975 
976 	if (!retval)
977 		goto nodata;
978 	memset(retval, 0, sizeof(struct sctp_chunk));
979 
980 	if (!sk) {
981 		SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
982 	}
983 
984 	retval->skb		= skb;
985 	retval->asoc		= (struct sctp_association *)asoc;
986 	retval->resent  	= 0;
987 	retval->has_tsn		= 0;
988 	retval->has_ssn         = 0;
989 	retval->rtt_in_progress	= 0;
990 	retval->sent_at		= 0;
991 	retval->singleton	= 1;
992 	retval->end_of_packet	= 0;
993 	retval->ecn_ce_done	= 0;
994 	retval->pdiscard	= 0;
995 
996 	/* sctpimpguide-05.txt Section 2.8.2
997 	 * M1) Each time a new DATA chunk is transmitted
998 	 * set the 'TSN.Missing.Report' count for that TSN to 0. The
999 	 * 'TSN.Missing.Report' count will be used to determine missing chunks
1000 	 * and when to fast retransmit.
1001 	 */
1002 	retval->tsn_missing_report = 0;
1003 	retval->tsn_gap_acked = 0;
1004 	retval->fast_retransmit = 0;
1005 
1006 	/* If this is a fragmented message, track all fragments
1007 	 * of the message (for SEND_FAILED).
1008 	 */
1009 	retval->msg = NULL;
1010 
1011 	/* Polish the bead hole.  */
1012 	INIT_LIST_HEAD(&retval->transmitted_list);
1013 	INIT_LIST_HEAD(&retval->frag_list);
1014 	SCTP_DBG_OBJCNT_INC(chunk);
1015 	atomic_set(&retval->refcnt, 1);
1016 
1017 nodata:
1018 	return retval;
1019 }
1020 
1021 /* Set chunk->source and dest based on the IP header in chunk->skb.  */
sctp_init_addrs(struct sctp_chunk * chunk,union sctp_addr * src,union sctp_addr * dest)1022 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1023 		     union sctp_addr *dest)
1024 {
1025 	memcpy(&chunk->source, src, sizeof(union sctp_addr));
1026 	memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1027 }
1028 
1029 /* Extract the source address from a chunk.  */
sctp_source(const struct sctp_chunk * chunk)1030 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1031 {
1032 	/* If we have a known transport, use that.  */
1033 	if (chunk->transport) {
1034 		return &chunk->transport->ipaddr;
1035 	} else {
1036 		/* Otherwise, extract it from the IP header.  */
1037 		return &chunk->source;
1038 	}
1039 }
1040 
1041 /* Create a new chunk, setting the type and flags headers from the
1042  * arguments, reserving enough space for a 'paylen' byte payload.
1043  */
1044 SCTP_STATIC
sctp_make_chunk(const struct sctp_association * asoc,__u8 type,__u8 flags,int paylen)1045 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1046 				   __u8 type, __u8 flags, int paylen)
1047 {
1048 	struct sctp_chunk *retval;
1049 	sctp_chunkhdr_t *chunk_hdr;
1050 	struct sk_buff *skb;
1051 	struct sock *sk;
1052 
1053 	/* No need to allocate LL here, as this is only a chunk. */
1054 	skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1055 			GFP_ATOMIC);
1056 	if (!skb)
1057 		goto nodata;
1058 
1059 	/* Make room for the chunk header.  */
1060 	chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1061 	chunk_hdr->type	  = type;
1062 	chunk_hdr->flags  = flags;
1063 	chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1064 
1065 	sk = asoc ? asoc->base.sk : NULL;
1066 	retval = sctp_chunkify(skb, asoc, sk);
1067 	if (!retval) {
1068 		kfree_skb(skb);
1069 		goto nodata;
1070 	}
1071 
1072 	retval->chunk_hdr = chunk_hdr;
1073 	retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1074 
1075 	/* Set the skb to the belonging sock for accounting.  */
1076 	skb->sk = sk;
1077 
1078 	return retval;
1079 nodata:
1080 	return NULL;
1081 }
1082 
1083 
1084 /* Release the memory occupied by a chunk.  */
sctp_chunk_destroy(struct sctp_chunk * chunk)1085 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1086 {
1087 	/* Free the chunk skb data and the SCTP_chunk stub itself. */
1088 	dev_kfree_skb(chunk->skb);
1089 
1090 	SCTP_DBG_OBJCNT_DEC(chunk);
1091 	kmem_cache_free(sctp_chunk_cachep, chunk);
1092 }
1093 
1094 /* Possibly, free the chunk.  */
sctp_chunk_free(struct sctp_chunk * chunk)1095 void sctp_chunk_free(struct sctp_chunk *chunk)
1096 {
1097 	/* Make sure that we are not on any list.  */
1098 	skb_unlink((struct sk_buff *) chunk);
1099 	list_del_init(&chunk->transmitted_list);
1100 
1101 	/* Release our reference on the message tracker. */
1102 	if (chunk->msg)
1103 		sctp_datamsg_put(chunk->msg);
1104 
1105 	sctp_chunk_put(chunk);
1106 }
1107 
1108 /* Grab a reference to the chunk. */
sctp_chunk_hold(struct sctp_chunk * ch)1109 void sctp_chunk_hold(struct sctp_chunk *ch)
1110 {
1111 	atomic_inc(&ch->refcnt);
1112 }
1113 
1114 /* Release a reference to the chunk. */
sctp_chunk_put(struct sctp_chunk * ch)1115 void sctp_chunk_put(struct sctp_chunk *ch)
1116 {
1117 	if (atomic_dec_and_test(&ch->refcnt))
1118 		sctp_chunk_destroy(ch);
1119 }
1120 
1121 /* Append bytes to the end of a chunk.  Will panic if chunk is not big
1122  * enough.
1123  */
sctp_addto_chunk(struct sctp_chunk * chunk,int len,const void * data)1124 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1125 {
1126 	void *target;
1127 	void *padding;
1128 	int chunklen = ntohs(chunk->chunk_hdr->length);
1129 	int padlen = chunklen % 4;
1130 
1131 	padding = skb_put(chunk->skb, padlen);
1132 	target = skb_put(chunk->skb, len);
1133 
1134 	memset(padding, 0, padlen);
1135 	memcpy(target, data, len);
1136 
1137 	/* Adjust the chunk length field.  */
1138 	chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1139 	chunk->chunk_end = chunk->skb->tail;
1140 
1141 	return target;
1142 }
1143 
1144 /* Append bytes from user space to the end of a chunk.  Will panic if
1145  * chunk is not big enough.
1146  * Returns a kernel err value.
1147  */
sctp_user_addto_chunk(struct sctp_chunk * chunk,int off,int len,struct iovec * data)1148 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1149 			  struct iovec *data)
1150 {
1151 	__u8 *target;
1152 	int err = 0;
1153 
1154 	/* Make room in chunk for data.  */
1155 	target = skb_put(chunk->skb, len);
1156 
1157 	/* Copy data (whole iovec) into chunk */
1158 	if ((err = memcpy_fromiovecend(target, data, off, len)))
1159 		goto out;
1160 
1161 	/* Adjust the chunk length field.  */
1162 	chunk->chunk_hdr->length =
1163 		htons(ntohs(chunk->chunk_hdr->length) + len);
1164 	chunk->chunk_end = chunk->skb->tail;
1165 
1166 out:
1167 	return err;
1168 }
1169 
1170 /* Helper function to assign a TSN if needed.  This assumes that both
1171  * the data_hdr and association have already been assigned.
1172  */
sctp_chunk_assign_ssn(struct sctp_chunk * chunk)1173 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1174 {
1175 	__u16 ssn;
1176 	__u16 sid;
1177 
1178 	if (chunk->has_ssn)
1179 		return;
1180 
1181 	/* This is the last possible instant to assign a SSN. */
1182 	if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1183 		ssn = 0;
1184 	} else {
1185 		sid = htons(chunk->subh.data_hdr->stream);
1186 		if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1187 			ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
1188 		else
1189 			ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
1190 		ssn = htons(ssn);
1191 	}
1192 
1193 	chunk->subh.data_hdr->ssn = ssn;
1194 	chunk->has_ssn = 1;
1195 }
1196 
1197 /* Helper function to assign a TSN if needed.  This assumes that both
1198  * the data_hdr and association have already been assigned.
1199  */
sctp_chunk_assign_tsn(struct sctp_chunk * chunk)1200 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1201 {
1202 	if (!chunk->has_tsn) {
1203 		/* This is the last possible instant to
1204 		 * assign a TSN.
1205 		 */
1206 		chunk->subh.data_hdr->tsn =
1207 			htonl(sctp_association_get_next_tsn(chunk->asoc));
1208 		chunk->has_tsn = 1;
1209 	}
1210 }
1211 
1212 /* Create a CLOSED association to use with an incoming packet.  */
sctp_make_temp_asoc(const struct sctp_endpoint * ep,struct sctp_chunk * chunk,int gfp)1213 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1214 					struct sctp_chunk *chunk, int gfp)
1215 {
1216 	struct sctp_association *asoc;
1217 	struct sk_buff *skb;
1218 	sctp_scope_t scope;
1219 	struct sctp_af *af;
1220 
1221 	/* Create the bare association.  */
1222 	scope = sctp_scope(sctp_source(chunk));
1223 	asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1224 	if (!asoc)
1225 		goto nodata;
1226 	asoc->temp = 1;
1227 	skb = chunk->skb;
1228 	/* Create an entry for the source address of the packet.  */
1229 	af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
1230 	if (unlikely(!af))
1231 		goto fail;
1232 	af->from_skb(&asoc->c.peer_addr, skb, 1);
1233 nodata:
1234 	return asoc;
1235 
1236 fail:
1237 	sctp_association_free(asoc);
1238 	return NULL;
1239 }
1240 
1241 /* Build a cookie representing asoc.
1242  * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1243  */
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)1244 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1245 				      const struct sctp_association *asoc,
1246 				      const struct sctp_chunk *init_chunk,
1247 				      int *cookie_len,
1248 				      const __u8 *raw_addrs, int addrs_len)
1249 {
1250 	sctp_cookie_param_t *retval;
1251 	struct sctp_signed_cookie *cookie;
1252 	struct scatterlist sg;
1253 	int headersize, bodysize;
1254 	unsigned int keylen;
1255 	char *key;
1256 
1257 	headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
1258 	bodysize = sizeof(struct sctp_cookie)
1259 		+ ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1260 
1261 	/* Pad out the cookie to a multiple to make the signature
1262 	 * functions simpler to write.
1263 	 */
1264 	if (bodysize % SCTP_COOKIE_MULTIPLE)
1265 		bodysize += SCTP_COOKIE_MULTIPLE
1266 			- (bodysize % SCTP_COOKIE_MULTIPLE);
1267 	*cookie_len = headersize + bodysize;
1268 
1269 	retval = (sctp_cookie_param_t *)kmalloc(*cookie_len, GFP_ATOMIC);
1270 
1271 	if (!retval) {
1272 		*cookie_len = 0;
1273 		goto nodata;
1274 	}
1275 
1276 	/* Clear this memory since we are sending this data structure
1277 	 * out on the network.
1278 	 */
1279 	memset(retval, 0x00, *cookie_len);
1280 	cookie = (struct sctp_signed_cookie *) retval->body;
1281 
1282 	/* Set up the parameter header.  */
1283 	retval->p.type = SCTP_PARAM_STATE_COOKIE;
1284 	retval->p.length = htons(*cookie_len);
1285 
1286 	/* Copy the cookie part of the association itself.  */
1287 	cookie->c = asoc->c;
1288 	/* Save the raw address list length in the cookie. */
1289 	cookie->c.raw_addr_list_len = addrs_len;
1290 
1291 	/* Remember PR-SCTP capability. */
1292 	cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1293 
1294 	/* Save adaption indication in the cookie. */
1295 	cookie->c.adaption_ind = asoc->peer.adaption_ind;
1296 
1297 	/* Set an expiration time for the cookie.  */
1298 	do_gettimeofday(&cookie->c.expiration);
1299 	TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1300 
1301 	/* Copy the peer's init packet.  */
1302 	memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1303 	       ntohs(init_chunk->chunk_hdr->length));
1304 
1305 	/* Copy the raw local address list of the association. */
1306 	memcpy((__u8 *)&cookie->c.peer_init[0] +
1307 	       ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1308 
1309   	if (sctp_sk(ep->base.sk)->hmac) {
1310 		/* Sign the message.  */
1311 		sg.page = virt_to_page(&cookie->c);
1312 		sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1313 		sg.length = bodysize;
1314 		keylen = SCTP_SECRET_SIZE;
1315 		key = (char *)ep->secret_key[ep->current_key];
1316 
1317 		sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1318 				 &sg, 1, cookie->signature);
1319 	}
1320 
1321 nodata:
1322 	return retval;
1323 }
1324 
1325 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association.  */
sctp_unpack_cookie(const struct sctp_endpoint * ep,const struct sctp_association * asoc,struct sctp_chunk * chunk,int gfp,int * error,struct sctp_chunk ** errp)1326 struct sctp_association *sctp_unpack_cookie(
1327 	const struct sctp_endpoint *ep,
1328 	const struct sctp_association *asoc,
1329 	struct sctp_chunk *chunk, int gfp,
1330 	int *error, struct sctp_chunk **errp)
1331 {
1332 	struct sctp_association *retval = NULL;
1333 	struct sctp_signed_cookie *cookie;
1334 	struct sctp_cookie *bear_cookie;
1335 	int headersize, bodysize, fixed_size;
1336 	__u8 digest[SCTP_SIGNATURE_SIZE];
1337 	struct scatterlist sg;
1338 	unsigned int keylen, len;
1339 	char *key;
1340 	sctp_scope_t scope;
1341 	struct sk_buff *skb = chunk->skb;
1342 
1343 	headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
1344 	bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1345 	fixed_size = headersize + sizeof(struct sctp_cookie);
1346 
1347 	/* Verify that the chunk looks like it even has a cookie.
1348 	 * There must be enough room for our cookie and our peer's
1349 	 * INIT chunk.
1350 	 */
1351 	len = ntohs(chunk->chunk_hdr->length);
1352 	if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1353 		goto malformed;
1354 
1355 	/* Verify that the cookie has been padded out. */
1356 	if (bodysize % SCTP_COOKIE_MULTIPLE)
1357 		goto malformed;
1358 
1359 	/* Process the cookie.  */
1360 	cookie = chunk->subh.cookie_hdr;
1361 	bear_cookie = &cookie->c;
1362 
1363 	if (!sctp_sk(ep->base.sk)->hmac)
1364 		goto no_hmac;
1365 
1366 	/* Check the signature.  */
1367 	keylen = SCTP_SECRET_SIZE;
1368 	sg.page = virt_to_page(bear_cookie);
1369 	sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1370 	sg.length = bodysize;
1371 	key = (char *)ep->secret_key[ep->current_key];
1372 
1373 	memset(digest, 0x00, sizeof(digest));
1374 	sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen, &sg,
1375 			 1, digest);
1376 
1377 	if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1378 		/* Try the previous key. */
1379 		key = (char *)ep->secret_key[ep->last_key];
1380 		memset(digest, 0x00, sizeof(digest));
1381 		sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1382 				 &sg, 1, digest);
1383 
1384 		if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1385 			/* Yikes!  Still bad signature! */
1386 			*error = -SCTP_IERROR_BAD_SIG;
1387 			goto fail;
1388 		}
1389 	}
1390 
1391 no_hmac:
1392 	/* IG Section 2.35.2:
1393 	 *  3) Compare the port numbers and the verification tag contained
1394 	 *     within the COOKIE ECHO chunk to the actual port numbers and the
1395 	 *     verification tag within the SCTP common header of the received
1396 	 *     packet. If these values do not match the packet MUST be silently
1397 	 *     discarded,
1398 	 */
1399 	if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1400 		*error = -SCTP_IERROR_BAD_TAG;
1401 		goto fail;
1402 	}
1403 
1404 	if (ntohs(chunk->sctp_hdr->source) != bear_cookie->peer_addr.v4.sin_port ||
1405 	    ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1406 		*error = -SCTP_IERROR_BAD_PORTS;
1407 		goto fail;
1408 	}
1409 
1410 	/* Check to see if the cookie is stale.  If there is already
1411 	 * an association, there is no need to check cookie's expiration
1412 	 * for init collision case of lost COOKIE ACK.
1413 	 */
1414 	if (!asoc && tv_lt(bear_cookie->expiration, skb->stamp)) {
1415 		__u16 len;
1416 		/*
1417 		 * Section 3.3.10.3 Stale Cookie Error (3)
1418 		 *
1419 		 * Cause of error
1420 		 * ---------------
1421 		 * Stale Cookie Error:  Indicates the receipt of a valid State
1422 		 * Cookie that has expired.
1423 		 */
1424 		len = ntohs(chunk->chunk_hdr->length);
1425 		*errp = sctp_make_op_error_space(asoc, chunk, len);
1426 		if (*errp) {
1427 			suseconds_t usecs = (skb->stamp.tv_sec -
1428 				bear_cookie->expiration.tv_sec) * 1000000L +
1429 				skb->stamp.tv_usec -
1430 				bear_cookie->expiration.tv_usec;
1431 
1432 			usecs = htonl(usecs);
1433 			sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1434 					&usecs, sizeof(usecs));
1435 			*error = -SCTP_IERROR_STALE_COOKIE;
1436 		} else
1437 			*error = -SCTP_IERROR_NOMEM;
1438 
1439 		goto fail;
1440 	}
1441 
1442 	/* Make a new base association.  */
1443 	scope = sctp_scope(sctp_source(chunk));
1444 	retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1445 	if (!retval) {
1446 		*error = -SCTP_IERROR_NOMEM;
1447 		goto fail;
1448 	}
1449 
1450 	/* Set up our peer's port number.  */
1451 	retval->peer.port = ntohs(chunk->sctp_hdr->source);
1452 
1453 	/* Populate the association from the cookie.  */
1454 	memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1455 
1456 	if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1457 						 GFP_ATOMIC) < 0) {
1458 		*error = -SCTP_IERROR_NOMEM;
1459 		goto fail;
1460 	}
1461 
1462 	/* Also, add the destination address. */
1463 	if (list_empty(&retval->base.bind_addr.address_list)) {
1464 		sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1465 				   GFP_ATOMIC);
1466 	}
1467 
1468 	retval->next_tsn = retval->c.initial_tsn;
1469 	retval->ctsn_ack_point = retval->next_tsn - 1;
1470 	retval->addip_serial = retval->c.initial_tsn;
1471 	retval->adv_peer_ack_point = retval->ctsn_ack_point;
1472 	retval->peer.prsctp_capable = retval->c.prsctp_capable;
1473 	retval->peer.adaption_ind = retval->c.adaption_ind;
1474 
1475 	/* The INIT stuff will be done by the side effects.  */
1476 	return retval;
1477 
1478 fail:
1479 	if (retval)
1480 		sctp_association_free(retval);
1481 
1482 	return NULL;
1483 
1484 malformed:
1485 	/* Yikes!  The packet is either corrupt or deliberately
1486 	 * malformed.
1487 	 */
1488 	*error = -SCTP_IERROR_MALFORMED;
1489 	goto fail;
1490 }
1491 
1492 /********************************************************************
1493  * 3rd Level Abstractions
1494  ********************************************************************/
1495 
1496 struct __sctp_missing {
1497 	__u32 num_missing;
1498 	__u16 type;
1499 }  __attribute__((packed));;
1500 
1501 /*
1502  * Report a missing mandatory parameter.
1503  */
sctp_process_missing_param(const struct sctp_association * asoc,sctp_param_t paramtype,struct sctp_chunk * chunk,struct sctp_chunk ** errp)1504 static int sctp_process_missing_param(const struct sctp_association *asoc,
1505 				      sctp_param_t paramtype,
1506 				      struct sctp_chunk *chunk,
1507 				      struct sctp_chunk **errp)
1508 {
1509 	struct __sctp_missing report;
1510 	__u16 len;
1511 
1512 	len = WORD_ROUND(sizeof(report));
1513 
1514 	/* Make an ERROR chunk, preparing enough room for
1515 	 * returning multiple unknown parameters.
1516 	 */
1517 	if (!*errp)
1518 		*errp = sctp_make_op_error_space(asoc, chunk, len);
1519 
1520 	if (*errp) {
1521 		report.num_missing = htonl(1);
1522 		report.type = paramtype;
1523 		sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM,
1524 				&report, sizeof(report));
1525 	}
1526 
1527 	/* Stop processing this chunk. */
1528 	return 0;
1529 }
1530 
1531 /* Report an Invalid Mandatory Parameter.  */
sctp_process_inv_mandatory(const struct sctp_association * asoc,struct sctp_chunk * chunk,struct sctp_chunk ** errp)1532 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1533 				      struct sctp_chunk *chunk,
1534 				      struct sctp_chunk **errp)
1535 {
1536 	/* Invalid Mandatory Parameter Error has no payload. */
1537 
1538 	if (!*errp)
1539 		*errp = sctp_make_op_error_space(asoc, chunk, 0);
1540 
1541 	if (*errp)
1542 		sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
1543 
1544 	/* Stop processing this chunk. */
1545 	return 0;
1546 }
1547 
sctp_process_inv_paramlength(const struct sctp_association * asoc,struct sctp_paramhdr * param,const struct sctp_chunk * chunk,struct sctp_chunk ** errp)1548 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1549 					struct sctp_paramhdr *param,
1550 					const struct sctp_chunk *chunk,
1551 					struct sctp_chunk **errp)
1552 {
1553 	char		error[] = "The following parameter had invalid length:";
1554 	size_t		payload_len = WORD_ROUND(sizeof(error)) +
1555 						sizeof(sctp_paramhdr_t);
1556 
1557 
1558 	/* Create an error chunk and fill it in with our payload. */
1559 	if (!*errp)
1560 		*errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1561 
1562 	if (*errp) {
1563 		sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION, error,
1564 				sizeof(error));
1565 		sctp_addto_chunk(*errp, sizeof(sctp_paramhdr_t), param);
1566 	}
1567 
1568 	return 0;
1569 }
1570 
1571 
1572 /* Do not attempt to handle the HOST_NAME parm.  However, do
1573  * send back an indicator to the peer.
1574  */
sctp_process_hn_param(const struct sctp_association * asoc,union sctp_params param,struct sctp_chunk * chunk,struct sctp_chunk ** errp)1575 static int sctp_process_hn_param(const struct sctp_association *asoc,
1576 				 union sctp_params param,
1577 				 struct sctp_chunk *chunk,
1578 				 struct sctp_chunk **errp)
1579 {
1580 	__u16 len = ntohs(param.p->length);
1581 
1582 	/* Make an ERROR chunk. */
1583 	if (!*errp)
1584 		*errp = sctp_make_op_error_space(asoc, chunk, len);
1585 
1586 	if (*errp)
1587 		sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
1588 				param.v, len);
1589 
1590 	/* Stop processing this chunk. */
1591 	return 0;
1592 }
1593 
1594 /* RFC 3.2.1 & the Implementers Guide 2.2.
1595  *
1596  * The Parameter Types are encoded such that the
1597  * highest-order two bits specify the action that must be
1598  * taken if the processing endpoint does not recognize the
1599  * Parameter Type.
1600  *
1601  * 00 - Stop processing this SCTP chunk and discard it,
1602  *	do not process any further chunks within it.
1603  *
1604  * 01 - Stop processing this SCTP chunk and discard it,
1605  *	do not process any further chunks within it, and report
1606  *	the unrecognized parameter in an 'Unrecognized
1607  *	Parameter Type' (in either an ERROR or in the INIT ACK).
1608  *
1609  * 10 - Skip this parameter and continue processing.
1610  *
1611  * 11 - Skip this parameter and continue processing but
1612  *	report the unrecognized parameter in an
1613  *	'Unrecognized Parameter Type' (in either an ERROR or in
1614  *	the INIT ACK).
1615  *
1616  * Return value:
1617  * 	0 - discard the chunk
1618  * 	1 - continue with the chunk
1619  */
sctp_process_unk_param(const struct sctp_association * asoc,union sctp_params param,struct sctp_chunk * chunk,struct sctp_chunk ** errp)1620 static int sctp_process_unk_param(const struct sctp_association *asoc,
1621 				  union sctp_params param,
1622 				  struct sctp_chunk *chunk,
1623 				  struct sctp_chunk **errp)
1624 {
1625 	int retval = 1;
1626 
1627 	switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1628 	case SCTP_PARAM_ACTION_DISCARD:
1629 		retval =  0;
1630 		break;
1631 	case SCTP_PARAM_ACTION_DISCARD_ERR:
1632 		retval =  0;
1633 		/* Make an ERROR chunk, preparing enough room for
1634 		 * returning multiple unknown parameters.
1635 		 */
1636 		if (NULL == *errp)
1637 			*errp = sctp_make_op_error_space(asoc, chunk,
1638 					ntohs(chunk->chunk_hdr->length));
1639 
1640 		if (*errp)
1641 			sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1642 					param.v,
1643 					WORD_ROUND(ntohs(param.p->length)));
1644 
1645 		break;
1646 	case SCTP_PARAM_ACTION_SKIP:
1647 		break;
1648 	case SCTP_PARAM_ACTION_SKIP_ERR:
1649 		/* Make an ERROR chunk, preparing enough room for
1650 		 * returning multiple unknown parameters.
1651 		 */
1652 		if (NULL == *errp)
1653 			*errp = sctp_make_op_error_space(asoc, chunk,
1654 					ntohs(chunk->chunk_hdr->length));
1655 
1656 		if (*errp) {
1657 			sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1658 					param.v,
1659 					WORD_ROUND(ntohs(param.p->length)));
1660 		} else {
1661 			/* If there is no memory for generating the ERROR
1662 			 * report as specified, an ABORT will be triggered
1663 			 * to the peer and the association won't be
1664 			 * established.
1665 			 */
1666 			retval = 0;
1667 		}
1668 
1669 		break;
1670 	default:
1671 		break;
1672 	}
1673 
1674 	return retval;
1675 }
1676 
1677 /* Find unrecognized parameters in the chunk.
1678  * Return values:
1679  * 	0 - discard the chunk
1680  * 	1 - continue with the chunk
1681  */
sctp_verify_param(const struct sctp_association * asoc,union sctp_params param,sctp_cid_t cid,struct sctp_chunk * chunk,struct sctp_chunk ** err_chunk)1682 static int sctp_verify_param(const struct sctp_association *asoc,
1683 			     union sctp_params param,
1684 			     sctp_cid_t cid,
1685 			     struct sctp_chunk *chunk,
1686 			     struct sctp_chunk **err_chunk)
1687 {
1688 	int retval = 1;
1689 
1690 	/* FIXME - This routine is not looking at each parameter per the
1691 	 * chunk type, i.e., unrecognized parameters should be further
1692 	 * identified based on the chunk id.
1693 	 */
1694 
1695 	switch (param.p->type) {
1696 	case SCTP_PARAM_IPV4_ADDRESS:
1697 	case SCTP_PARAM_IPV6_ADDRESS:
1698 	case SCTP_PARAM_COOKIE_PRESERVATIVE:
1699 	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1700 	case SCTP_PARAM_STATE_COOKIE:
1701 	case SCTP_PARAM_HEARTBEAT_INFO:
1702 	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1703 	case SCTP_PARAM_ECN_CAPABLE:
1704 	case SCTP_PARAM_ADAPTION_LAYER_IND:
1705 		break;
1706 
1707 	case SCTP_PARAM_HOST_NAME_ADDRESS:
1708 		/* Tell the peer, we won't support this param.  */
1709 		return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1710 	case SCTP_PARAM_FWD_TSN_SUPPORT:
1711 		if (sctp_prsctp_enable)
1712 			break;
1713 		/* Fall Through */
1714 	default:
1715 		SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1716 				ntohs(param.p->type), cid);
1717 		return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1718 
1719 		break;
1720 	}
1721 	return retval;
1722 }
1723 
1724 /* Verify the INIT packet before we process it.  */
sctp_verify_init(const struct sctp_association * asoc,sctp_cid_t cid,sctp_init_chunk_t * peer_init,struct sctp_chunk * chunk,struct sctp_chunk ** errp)1725 int sctp_verify_init(const struct sctp_association *asoc,
1726 		     sctp_cid_t cid,
1727 		     sctp_init_chunk_t *peer_init,
1728 		     struct sctp_chunk *chunk,
1729 		     struct sctp_chunk **errp)
1730 {
1731 	union sctp_params param;
1732 	int has_cookie = 0;
1733 
1734 	/* Verify stream values are non-zero. */
1735 	if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1736 	    (0 == peer_init->init_hdr.num_inbound_streams)) {
1737 
1738 		sctp_process_inv_mandatory(asoc, chunk, errp);
1739 		return 0;
1740 	}
1741 
1742 	/* Check for missing mandatory parameters.  */
1743 	sctp_walk_params(param, peer_init, init_hdr.params) {
1744 
1745 		if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1746 			has_cookie = 1;
1747 
1748 	} /* for (loop through all parameters) */
1749 
1750 	/* There is a possibility that a parameter length was bad and
1751 	 * in that case we would have stoped walking the parameters.
1752 	 * The current param.p would point at the bad one.
1753 	 * Current consensus on the mailing list is to generate a PROTOCOL
1754 	 * VIOLATION error.  We build the ERROR chunk here and let the normal
1755 	 * error handling code build and send the packet.
1756 	 */
1757 	if (param.v < (void*)chunk->chunk_end - sizeof(sctp_paramhdr_t)) {
1758 		sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1759 		return 0;
1760 	}
1761 
1762 	/* The only missing mandatory param possible today is
1763 	 * the state cookie for an INIT-ACK chunk.
1764 	 */
1765 	if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1766 		sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1767 					   chunk, errp);
1768 		return 0;
1769 	}
1770 
1771 	/* Find unrecognized parameters. */
1772 
1773 	sctp_walk_params(param, peer_init, init_hdr.params) {
1774 
1775 		if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1776 			if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1777 				return 0;
1778 			else
1779 				return 1;
1780 		}
1781 
1782 	} /* for (loop through all parameters) */
1783 
1784 	return 1;
1785 }
1786 
1787 /* Unpack the parameters in an INIT packet into an association.
1788  * Returns 0 on failure, else success.
1789  * FIXME:  This is an association method.
1790  */
sctp_process_init(struct sctp_association * asoc,sctp_cid_t cid,const union sctp_addr * peer_addr,sctp_init_chunk_t * peer_init,int gfp)1791 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1792 		      const union sctp_addr *peer_addr,
1793 		      sctp_init_chunk_t *peer_init, int gfp)
1794 {
1795 	union sctp_params param;
1796 	struct sctp_transport *transport;
1797 	struct list_head *pos, *temp;
1798 	char *cookie;
1799 
1800 	/* We must include the address that the INIT packet came from.
1801 	 * This is the only address that matters for an INIT packet.
1802 	 * When processing a COOKIE ECHO, we retrieve the from address
1803 	 * of the INIT from the cookie.
1804 	 */
1805 
1806 	/* This implementation defaults to making the first transport
1807 	 * added as the primary transport.  The source address seems to
1808 	 * be a a better choice than any of the embedded addresses.
1809 	 */
1810 	if (peer_addr)
1811 		if(!sctp_assoc_add_peer(asoc, peer_addr, gfp))
1812 			goto nomem;
1813 
1814 	/* Process the initialization parameters.  */
1815 
1816 	sctp_walk_params(param, peer_init, init_hdr.params) {
1817 
1818 		if (!sctp_process_param(asoc, param, peer_addr, gfp))
1819                         goto clean_up;
1820 	}
1821 
1822 	/* The fixed INIT headers are always in network byte
1823 	 * order.
1824 	 */
1825 	asoc->peer.i.init_tag =
1826 		ntohl(peer_init->init_hdr.init_tag);
1827 	asoc->peer.i.a_rwnd =
1828 		ntohl(peer_init->init_hdr.a_rwnd);
1829 	asoc->peer.i.num_outbound_streams =
1830 		ntohs(peer_init->init_hdr.num_outbound_streams);
1831 	asoc->peer.i.num_inbound_streams =
1832 		ntohs(peer_init->init_hdr.num_inbound_streams);
1833 	asoc->peer.i.initial_tsn =
1834 		ntohl(peer_init->init_hdr.initial_tsn);
1835 
1836 	/* Apply the upper bounds for output streams based on peer's
1837 	 * number of inbound streams.
1838 	 */
1839 	if (asoc->c.sinit_num_ostreams  >
1840 	    ntohs(peer_init->init_hdr.num_inbound_streams)) {
1841 		asoc->c.sinit_num_ostreams =
1842 			ntohs(peer_init->init_hdr.num_inbound_streams);
1843 	}
1844 
1845 	if (asoc->c.sinit_max_instreams >
1846 	    ntohs(peer_init->init_hdr.num_outbound_streams)) {
1847 		asoc->c.sinit_max_instreams =
1848 			ntohs(peer_init->init_hdr.num_outbound_streams);
1849 	}
1850 
1851 	/* Copy Initiation tag from INIT to VT_peer in cookie.   */
1852 	asoc->c.peer_vtag = asoc->peer.i.init_tag;
1853 
1854 	/* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
1855 	asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1856 
1857 	/* Copy cookie in case we need to resend COOKIE-ECHO. */
1858 	cookie = asoc->peer.cookie;
1859 	if (cookie) {
1860 		asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, gfp);
1861 		if (!asoc->peer.cookie)
1862 			goto clean_up;
1863 		memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
1864 	}
1865 
1866 	/* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1867 	 * high (for example, implementations MAY use the size of the receiver
1868 	 * advertised window).
1869 	 */
1870 	list_for_each(pos, &asoc->peer.transport_addr_list) {
1871 		transport = list_entry(pos, struct sctp_transport, transports);
1872 		transport->ssthresh = asoc->peer.i.a_rwnd;
1873 	}
1874 
1875 	/* Set up the TSN tracking pieces.  */
1876 	sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1877 			 asoc->peer.i.initial_tsn);
1878 
1879 	/* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1880 	 *
1881 	 * The stream sequence number in all the streams shall start
1882 	 * from 0 when the association is established.  Also, when the
1883 	 * stream sequence number reaches the value 65535 the next
1884 	 * stream sequence number shall be set to 0.
1885 	 */
1886 
1887 	/* Allocate storage for the negotiated streams if it is not a temporary 	 * association.
1888 	 */
1889 	if (!asoc->temp) {
1890 		asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1891 					       asoc->c.sinit_num_ostreams, gfp);
1892 		if (!asoc->ssnmap)
1893 			goto nomem_ssnmap;
1894 	}
1895 
1896 	/* ADDIP Section 4.1 ASCONF Chunk Procedures
1897 	 *
1898 	 * When an endpoint has an ASCONF signaled change to be sent to the
1899 	 * remote endpoint it should do the following:
1900 	 * ...
1901 	 * A2) A serial number should be assigned to the Chunk. The serial
1902 	 * number should be a monotonically increasing number. All serial
1903 	 * numbers are defined to be initialized at the start of the
1904 	 * association to the same value as the Initial TSN.
1905 	 */
1906 	asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1907 	return 1;
1908 
1909 nomem_ssnmap:
1910 clean_up:
1911 	/* Release the transport structures. */
1912 	list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1913 		transport = list_entry(pos, struct sctp_transport, transports);
1914 		list_del_init(pos);
1915 		sctp_transport_free(transport);
1916 	}
1917 nomem:
1918 	return 0;
1919 }
1920 
1921 
1922 /* Update asoc with the option described in param.
1923  *
1924  * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1925  *
1926  * asoc is the association to update.
1927  * param is the variable length parameter to use for update.
1928  * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1929  * If the current packet is an INIT we want to minimize the amount of
1930  * work we do.  In particular, we should not build transport
1931  * structures for the addresses.
1932  */
sctp_process_param(struct sctp_association * asoc,union sctp_params param,const union sctp_addr * peer_addr,int gfp)1933 static int sctp_process_param(struct sctp_association *asoc,
1934 			      union sctp_params param,
1935 			      const union sctp_addr *peer_addr,
1936 			      int gfp)
1937 {
1938 	union sctp_addr addr;
1939 	int i;
1940 	__u16 sat;
1941 	int retval = 1;
1942 	sctp_scope_t scope;
1943 	time_t stale;
1944 	struct sctp_af *af;
1945 
1946 	/* We maintain all INIT parameters in network byte order all the
1947 	 * time.  This allows us to not worry about whether the parameters
1948 	 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
1949 	 */
1950 	switch (param.p->type) {
1951 	case SCTP_PARAM_IPV6_ADDRESS:
1952 		if (PF_INET6 != asoc->base.sk->family)
1953 			break;
1954 		/* Fall through. */
1955 	case SCTP_PARAM_IPV4_ADDRESS:
1956 		af = sctp_get_af_specific(param_type2af(param.p->type));
1957 		af->from_addr_param(&addr, param.addr, asoc->peer.port, 0);
1958 		scope = sctp_scope(peer_addr);
1959 		if (sctp_in_scope(&addr, scope))
1960 			if (!sctp_assoc_add_peer(asoc, &addr, gfp))
1961 				return 0;
1962 		break;
1963 
1964 	case SCTP_PARAM_COOKIE_PRESERVATIVE:
1965 		if (!sctp_cookie_preserve_enable)
1966 			break;
1967 
1968 		stale = ntohl(param.life->lifespan_increment);
1969 
1970 		/* Suggested Cookie Life span increment's unit is msec,
1971 		 * (1/1000sec).
1972 		 */
1973 		asoc->cookie_life.tv_sec += stale / 1000;
1974 		asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
1975 		break;
1976 
1977 	case SCTP_PARAM_HOST_NAME_ADDRESS:
1978 		SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
1979 		break;
1980 
1981 	case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1982 		/* Turn off the default values first so we'll know which
1983 		 * ones are really set by the peer.
1984 		 */
1985 		asoc->peer.ipv4_address = 0;
1986 		asoc->peer.ipv6_address = 0;
1987 
1988 		/* Cycle through address types; avoid divide by 0. */
1989 		sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1990 		if (sat)
1991 			sat /= sizeof(__u16);
1992 
1993 		for (i = 0; i < sat; ++i) {
1994 			switch (param.sat->types[i]) {
1995 			case SCTP_PARAM_IPV4_ADDRESS:
1996 				asoc->peer.ipv4_address = 1;
1997 				break;
1998 
1999 			case SCTP_PARAM_IPV6_ADDRESS:
2000 				asoc->peer.ipv6_address = 1;
2001 				break;
2002 
2003 			case SCTP_PARAM_HOST_NAME_ADDRESS:
2004 				asoc->peer.hostname_address = 1;
2005 				break;
2006 
2007 			default: /* Just ignore anything else.  */
2008 				break;
2009 			};
2010 		}
2011 		break;
2012 
2013 	case SCTP_PARAM_STATE_COOKIE:
2014 		asoc->peer.cookie_len =
2015 			ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2016 		asoc->peer.cookie = param.cookie->body;
2017 		break;
2018 
2019 	case SCTP_PARAM_HEARTBEAT_INFO:
2020 		/* Would be odd to receive, but it causes no problems. */
2021 		break;
2022 
2023 	case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2024 		/* Rejected during verify stage. */
2025 		break;
2026 
2027 	case SCTP_PARAM_ECN_CAPABLE:
2028 		asoc->peer.ecn_capable = 1;
2029 		break;
2030 
2031 	case SCTP_PARAM_ADAPTION_LAYER_IND:
2032 		asoc->peer.adaption_ind = param.aind->adaption_ind;
2033 		break;
2034 
2035 	case SCTP_PARAM_FWD_TSN_SUPPORT:
2036 		if (sctp_prsctp_enable) {
2037 			asoc->peer.prsctp_capable = 1;
2038 			break;
2039 		}
2040 		/* Fall Through */
2041 	default:
2042 		/* Any unrecognized parameters should have been caught
2043 		 * and handled by sctp_verify_param() which should be
2044 		 * called prior to this routine.  Simply log the error
2045 		 * here.
2046 		 */
2047 		SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2048 				  ntohs(param.p->type), asoc);
2049 		break;
2050 	};
2051 
2052 	return retval;
2053 }
2054 
2055 /* Select a new verification tag.  */
sctp_generate_tag(const struct sctp_endpoint * ep)2056 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2057 {
2058 	/* I believe that this random number generator complies with RFC1750.
2059 	 * A tag of 0 is reserved for special cases (e.g. INIT).
2060 	 */
2061 	__u32 x;
2062 
2063 	do {
2064 		get_random_bytes(&x, sizeof(__u32));
2065 	} while (x == 0);
2066 
2067 	return x;
2068 }
2069 
2070 /* Select an initial TSN to send during startup.  */
sctp_generate_tsn(const struct sctp_endpoint * ep)2071 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2072 {
2073 	__u32 retval;
2074 
2075 	get_random_bytes(&retval, sizeof(__u32));
2076 	return retval;
2077 }
2078 
2079 /*
2080  * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2081  *      0                   1                   2                   3
2082  *      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
2083  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2084  *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
2085  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2086  *     |                       Serial Number                           |
2087  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2088  *     |                    Address Parameter                          |
2089  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2090  *     |                     ASCONF Parameter #1                       |
2091  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2092  *     \                                                               \
2093  *     /                             ....                              /
2094  *     \                                                               \
2095  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2096  *     |                     ASCONF Parameter #N                       |
2097  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2098  *
2099  * Address Parameter and other parameter will not be wrapped in this function
2100  */
sctp_make_asconf(struct sctp_association * asoc,union sctp_addr * addr,int vparam_len)2101 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2102 					   union sctp_addr *addr,
2103 					   int vparam_len)
2104 {
2105 	sctp_addiphdr_t asconf;
2106 	struct sctp_chunk *retval;
2107 	int length = sizeof(asconf) + vparam_len;
2108 	union sctp_addr_param addrparam;
2109 	int addrlen;
2110 	struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2111 
2112 	addrlen = af->to_addr_param(addr, &addrparam);
2113 	if (!addrlen)
2114 		return NULL;
2115 	length += addrlen;
2116 
2117 	/* Create the chunk.  */
2118 	retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2119 	if (!retval)
2120 		return NULL;
2121 
2122 	asconf.serial = htonl(asoc->addip_serial++);
2123 
2124 	retval->subh.addip_hdr =
2125 		sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2126 	retval->param_hdr.v =
2127 		sctp_addto_chunk(retval, addrlen, &addrparam);
2128 
2129 	return retval;
2130 }
2131 
2132 /* ADDIP
2133  * 3.2.1 Add IP Address
2134  * 	0                   1                   2                   3
2135  * 	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
2136  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2137  *     |        Type = 0xC001          |    Length = Variable          |
2138  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2139  *     |               ASCONF-Request Correlation ID                   |
2140  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2141  *     |                       Address Parameter                       |
2142  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2143  *
2144  * 3.2.2 Delete IP Address
2145  * 	0                   1                   2                   3
2146  * 	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
2147  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2148  *     |        Type = 0xC002          |    Length = Variable          |
2149  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2150  *     |               ASCONF-Request Correlation ID                   |
2151  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2152  *     |                       Address Parameter                       |
2153  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2154  *
2155  */
sctp_make_asconf_update_ip(struct sctp_association * asoc,union sctp_addr * laddr,struct sockaddr * addrs,int addrcnt,__u16 flags)2156 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2157 					      union sctp_addr	      *laddr,
2158 					      struct sockaddr	      *addrs,
2159 					      int		      addrcnt,
2160 					      __u16		      flags)
2161 {
2162 	sctp_addip_param_t	param;
2163 	struct sctp_chunk	*retval;
2164 	union sctp_addr_param	addr_param;
2165 	union sctp_addr		*addr;
2166 	void			*addr_buf;
2167 	struct sctp_af		*af;
2168 	int			paramlen = sizeof(param);
2169 	int			addr_param_len = 0;
2170 	int 			totallen = 0;
2171 	int 			i;
2172 
2173 	/* Get total length of all the address parameters. */
2174 	addr_buf = addrs;
2175 	for (i = 0; i < addrcnt; i++) {
2176 		addr = (union sctp_addr *)addr_buf;
2177 		af = sctp_get_af_specific(addr->v4.sin_family);
2178 		addr_param_len = af->to_addr_param(addr, &addr_param);
2179 
2180 		totallen += paramlen;
2181 		totallen += addr_param_len;
2182 
2183 		addr_buf += af->sockaddr_len;
2184 	}
2185 
2186 	/* Create an asconf chunk with the required length. */
2187 	retval = sctp_make_asconf(asoc, laddr, totallen);
2188 	if (!retval)
2189 		return NULL;
2190 
2191 	/* Add the address parameters to the asconf chunk. */
2192 	addr_buf = addrs;
2193 	for (i = 0; i < addrcnt; i++) {
2194 		addr = (union sctp_addr *)addr_buf;
2195 		af = sctp_get_af_specific(addr->v4.sin_family);
2196 		addr_param_len = af->to_addr_param(addr, &addr_param);
2197 		param.param_hdr.type = flags;
2198 		param.param_hdr.length = htons(paramlen + addr_param_len);
2199 		param.crr_id = i;
2200 
2201 		sctp_addto_chunk(retval, paramlen, &param);
2202 		sctp_addto_chunk(retval, addr_param_len, &addr_param);
2203 
2204 		addr_buf += af->sockaddr_len;
2205 	}
2206 	return retval;
2207 }
2208 
2209 /* ADDIP
2210  * 3.2.4 Set Primary IP Address
2211  *	0                   1                   2                   3
2212  *	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
2213  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2214  *     |        Type =0xC004           |    Length = Variable          |
2215  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2216  *     |               ASCONF-Request Correlation ID                   |
2217  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2218  *     |                       Address Parameter                       |
2219  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2220  *
2221  * Create an ASCONF chunk with Set Primary IP address parameter.
2222  */
sctp_make_asconf_set_prim(struct sctp_association * asoc,union sctp_addr * addr)2223 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2224 					     union sctp_addr *addr)
2225 {
2226 	sctp_addip_param_t	param;
2227 	struct sctp_chunk 	*retval;
2228 	int 			len = sizeof(param);
2229 	union sctp_addr_param	addrparam;
2230 	int			addrlen;
2231 	struct sctp_af		*af = sctp_get_af_specific(addr->v4.sin_family);
2232 
2233 	addrlen = af->to_addr_param(addr, &addrparam);
2234 	if (!addrlen)
2235 		return NULL;
2236 	len += addrlen;
2237 
2238 	/* Create the chunk and make asconf header. */
2239 	retval = sctp_make_asconf(asoc, addr, len);
2240 	if (!retval)
2241 		return NULL;
2242 
2243 	param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2244 	param.param_hdr.length = htons(len);
2245 	param.crr_id = 0;
2246 
2247 	sctp_addto_chunk(retval, sizeof(param), &param);
2248 	sctp_addto_chunk(retval, addrlen, &addrparam);
2249 
2250 	return retval;
2251 }
2252 
2253 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2254  *      0                   1                   2                   3
2255  *      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
2256  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2257  *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
2258  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2259  *     |                       Serial Number                           |
2260  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2261  *     |                 ASCONF Parameter Response#1                   |
2262  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2263  *     \                                                               \
2264  *     /                             ....                              /
2265  *     \                                                               \
2266  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2267  *     |                 ASCONF Parameter Response#N                   |
2268  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2269  *
2270  * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2271  */
sctp_make_asconf_ack(const struct sctp_association * asoc,__u32 serial,int vparam_len)2272 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2273 					       __u32 serial, int vparam_len)
2274 {
2275 	sctp_addiphdr_t		asconf;
2276 	struct sctp_chunk	*retval;
2277 	int			length = sizeof(asconf) + vparam_len;
2278 
2279 	/* Create the chunk.  */
2280 	retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2281 	if (!retval)
2282 		return NULL;
2283 
2284 	asconf.serial = htonl(serial);
2285 
2286 	retval->subh.addip_hdr =
2287 		sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2288 
2289 	return retval;
2290 }
2291 
2292 /* Add response parameters to an ASCONF_ACK chunk. */
sctp_add_asconf_response(struct sctp_chunk * chunk,__u32 crr_id,__u16 err_code,sctp_addip_param_t * asconf_param)2293 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __u32 crr_id,
2294 			      __u16 err_code, sctp_addip_param_t *asconf_param)
2295 {
2296 	sctp_addip_param_t 	ack_param;
2297 	sctp_errhdr_t		err_param;
2298 	int			asconf_param_len = 0;
2299 	int			err_param_len = 0;
2300 	__u16			response_type;
2301 
2302 	if (SCTP_ERROR_NO_ERROR == err_code) {
2303 		response_type = SCTP_PARAM_SUCCESS_REPORT;
2304 	} else {
2305 		response_type = SCTP_PARAM_ERR_CAUSE;
2306 		err_param_len = sizeof(err_param);
2307 		if (asconf_param)
2308 			asconf_param_len =
2309 				 ntohs(asconf_param->param_hdr.length);
2310 	}
2311 
2312 	/* Add Success Indication or Error Cause Indication parameter. */
2313 	ack_param.param_hdr.type = response_type;
2314 	ack_param.param_hdr.length = htons(sizeof(ack_param) +
2315 					   err_param_len +
2316 					   asconf_param_len);
2317 	ack_param.crr_id = crr_id;
2318 	sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2319 
2320 	if (SCTP_ERROR_NO_ERROR == err_code)
2321 		return;
2322 
2323 	/* Add Error Cause parameter. */
2324 	err_param.cause = err_code;
2325 	err_param.length = htons(err_param_len + asconf_param_len);
2326 	sctp_addto_chunk(chunk, err_param_len, &err_param);
2327 
2328 	/* Add the failed TLV copied from ASCONF chunk. */
2329 	if (asconf_param)
2330 		sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2331 }
2332 
2333 /* Process a asconf parameter. */
sctp_process_asconf_param(struct sctp_association * asoc,struct sctp_chunk * asconf,sctp_addip_param_t * asconf_param)2334 static __u16 sctp_process_asconf_param(struct sctp_association *asoc,
2335 				       struct sctp_chunk *asconf,
2336 				       sctp_addip_param_t *asconf_param)
2337 {
2338 	struct sctp_transport *peer;
2339 	struct sctp_af *af;
2340 	union sctp_addr	addr;
2341 	struct list_head *pos;
2342 	union sctp_addr_param *addr_param;
2343 
2344 	addr_param = (union sctp_addr_param *)
2345 			((void *)asconf_param + sizeof(sctp_addip_param_t));
2346 
2347 	af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2348 	if (unlikely(!af))
2349 		return SCTP_ERROR_INV_PARAM;
2350 
2351 	af->from_addr_param(&addr, addr_param, asoc->peer.port, 0);
2352 	switch (asconf_param->param_hdr.type) {
2353 	case SCTP_PARAM_ADD_IP:
2354 		/* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2355 	 	 * request and does not have the local resources to add this
2356 	 	 * new address to the association, it MUST return an Error
2357 	 	 * Cause TLV set to the new error code 'Operation Refused
2358 	 	 * Due to Resource Shortage'.
2359 	 	 */
2360 
2361 		peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC);
2362 		if (!peer)
2363 			return SCTP_ERROR_RSRC_LOW;
2364 
2365 		/* Start the heartbeat timer. */
2366 		if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2367 			sctp_transport_hold(peer);
2368 		break;
2369 	case SCTP_PARAM_DEL_IP:
2370 		/* ADDIP 4.3 D7) If a request is received to delete the
2371 	 	 * last remaining IP address of a peer endpoint, the receiver
2372 	 	 * MUST send an Error Cause TLV with the error cause set to the
2373 	 	 * new error code 'Request to Delete Last Remaining IP Address'.
2374 	 	 */
2375 		pos = asoc->peer.transport_addr_list.next;
2376 		if (pos->next == &asoc->peer.transport_addr_list)
2377 			return SCTP_ERROR_DEL_LAST_IP;
2378 
2379 		/* ADDIP 4.3 D8) If a request is received to delete an IP
2380 		 * address which is also the source address of the IP packet
2381 		 * which contained the ASCONF chunk, the receiver MUST reject
2382 		 * this request. To reject the request the receiver MUST send
2383 		 * an Error Cause TLV set to the new error code 'Request to
2384 		 * Delete Source IP Address'
2385 		 */
2386 		if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2387 			return SCTP_ERROR_DEL_SRC_IP;
2388 
2389 		sctp_assoc_del_peer(asoc, &addr);
2390 		break;
2391 	case SCTP_PARAM_SET_PRIMARY:
2392 		peer = sctp_assoc_lookup_paddr(asoc, &addr);
2393 		if (!peer)
2394 			return SCTP_ERROR_INV_PARAM;
2395 
2396 		sctp_assoc_set_primary(asoc, peer);
2397 		break;
2398 	default:
2399 		return SCTP_ERROR_INV_PARAM;
2400 		break;
2401 	}
2402 
2403 	return SCTP_ERROR_NO_ERROR;
2404 }
2405 
2406 /* Process an incoming ASCONF chunk with the next expected serial no. and
2407  * return an ASCONF_ACK chunk to be sent in response.
2408  */
sctp_process_asconf(struct sctp_association * asoc,struct sctp_chunk * asconf)2409 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2410 				       struct sctp_chunk *asconf)
2411 {
2412 	sctp_addiphdr_t		*hdr;
2413 	union sctp_addr_param	*addr_param;
2414 	sctp_addip_param_t	*asconf_param;
2415 	struct sctp_chunk	*asconf_ack;
2416 
2417 	__u16	err_code;
2418 	int	length = 0;
2419 	int	chunk_len = asconf->skb->len;
2420 	__u32	serial;
2421 	int	all_param_pass = 1;
2422 
2423 	hdr = (sctp_addiphdr_t *)asconf->skb->data;
2424 	serial = ntohl(hdr->serial);
2425 
2426 	/* Skip the addiphdr and store a pointer to address parameter.  */
2427 	length = sizeof(sctp_addiphdr_t);
2428 	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2429 	chunk_len -= length;
2430 
2431 	/* Skip the address parameter and store a pointer to the first
2432 	 * asconf paramter.
2433 	 */
2434 	length = ntohs(addr_param->v4.param_hdr.length);
2435 	asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2436 	chunk_len -= length;
2437 
2438 	/* create an ASCONF_ACK chunk.
2439 	 * Based on the definitions of parameters, we know that the size of
2440 	 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2441 	 * paramters.
2442 	 */
2443 	asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2444 	if (!asconf_ack)
2445 		goto done;
2446 
2447 	/* Process the TLVs contained within the ASCONF chunk. */
2448 	while (chunk_len > 0) {
2449 		err_code = sctp_process_asconf_param(asoc, asconf,
2450 						     asconf_param);
2451 		/* ADDIP 4.1 A7)
2452 		 * If an error response is received for a TLV parameter,
2453 		 * all TLVs with no response before the failed TLV are
2454 		 * considered successful if not reported.  All TLVs after
2455 		 * the failed response are considered unsuccessful unless
2456 		 * a specific success indication is present for the parameter.
2457 		 */
2458 		if (SCTP_ERROR_NO_ERROR != err_code)
2459 			all_param_pass = 0;
2460 
2461 		if (!all_param_pass)
2462 			sctp_add_asconf_response(asconf_ack,
2463 						 asconf_param->crr_id, err_code,
2464 						 asconf_param);
2465 
2466 		/* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2467 		 * an IP address sends an 'Out of Resource' in its response, it
2468 		 * MUST also fail any subsequent add or delete requests bundled
2469 		 * in the ASCONF.
2470 		 */
2471 		if (SCTP_ERROR_RSRC_LOW == err_code)
2472 			goto done;
2473 
2474 		/* Move to the next ASCONF param. */
2475 		length = ntohs(asconf_param->param_hdr.length);
2476 		asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2477 						      length);
2478 		chunk_len -= length;
2479 	}
2480 
2481 done:
2482 	asoc->peer.addip_serial++;
2483 
2484 	/* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2485 	 * after freeing the reference to old asconf ack if any.
2486 	 */
2487 	if (asconf_ack) {
2488 		if (asoc->addip_last_asconf_ack)
2489 			sctp_chunk_free(asoc->addip_last_asconf_ack);
2490 
2491 		sctp_chunk_hold(asconf_ack);
2492 		asoc->addip_last_asconf_ack = asconf_ack;
2493 	}
2494 
2495 	return asconf_ack;
2496 }
2497 
2498 /* Process a asconf parameter that is successfully acked. */
sctp_asconf_param_success(struct sctp_association * asoc,sctp_addip_param_t * asconf_param)2499 static int sctp_asconf_param_success(struct sctp_association *asoc,
2500 				     sctp_addip_param_t *asconf_param)
2501 {
2502 	struct sctp_af *af;
2503 	union sctp_addr	addr;
2504 	struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2505 	union sctp_addr_param *addr_param;
2506 	struct list_head *pos;
2507 	struct sctp_transport *transport;
2508 	int retval = 0;
2509 
2510 	addr_param = (union sctp_addr_param *)
2511 			((void *)asconf_param + sizeof(sctp_addip_param_t));
2512 
2513 	/* We have checked the packet before, so we do not check again.	*/
2514 	af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2515 	af->from_addr_param(&addr, addr_param, bp->port, 0);
2516 
2517 	switch (asconf_param->param_hdr.type) {
2518 	case SCTP_PARAM_ADD_IP:
2519 		sctp_local_bh_disable();
2520 		sctp_write_lock(&asoc->base.addr_lock);
2521 		retval = sctp_add_bind_addr(bp, &addr, GFP_ATOMIC);
2522 		sctp_write_unlock(&asoc->base.addr_lock);
2523 		sctp_local_bh_enable();
2524 		break;
2525 	case SCTP_PARAM_DEL_IP:
2526 		sctp_local_bh_disable();
2527 		sctp_write_lock(&asoc->base.addr_lock);
2528 		retval = sctp_del_bind_addr(bp, &addr);
2529 		sctp_write_unlock(&asoc->base.addr_lock);
2530 		sctp_local_bh_enable();
2531 		list_for_each(pos, &asoc->peer.transport_addr_list) {
2532 			transport = list_entry(pos, struct sctp_transport,
2533 						 transports);
2534 			sctp_transport_route(transport, NULL,
2535 					     sctp_sk(asoc->base.sk));
2536 		}
2537 		break;
2538 	default:
2539 		break;
2540 	}
2541 
2542 	return retval;
2543 }
2544 
2545 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2546  * for the given asconf parameter.  If there is no response for this parameter,
2547  * return the error code based on the third argument 'no_err'.
2548  * ADDIP 4.1
2549  * A7) If an error response is received for a TLV parameter, all TLVs with no
2550  * response before the failed TLV are considered successful if not reported.
2551  * All TLVs after the failed response are considered unsuccessful unless a
2552  * specific success indication is present for the parameter.
2553  */
sctp_get_asconf_response(struct sctp_chunk * asconf_ack,sctp_addip_param_t * asconf_param,int no_err)2554 static __u16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2555 				      sctp_addip_param_t *asconf_param,
2556 				      int no_err)
2557 {
2558 	sctp_addip_param_t	*asconf_ack_param;
2559 	sctp_errhdr_t		*err_param;
2560 	int			length;
2561 	int			asconf_ack_len = asconf_ack->skb->len;
2562 	__u16			err_code;
2563 
2564 	if (no_err)
2565 		err_code = SCTP_ERROR_NO_ERROR;
2566 	else
2567 		err_code = SCTP_ERROR_REQ_REFUSED;
2568 
2569 	/* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2570 	 * the first asconf_ack parameter.
2571 	 */
2572 	length = sizeof(sctp_addiphdr_t);
2573 	asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2574 						  length);
2575 	asconf_ack_len -= length;
2576 
2577 	while (asconf_ack_len > 0) {
2578 		if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2579 			switch(asconf_ack_param->param_hdr.type) {
2580 			case SCTP_PARAM_SUCCESS_REPORT:
2581 				return SCTP_ERROR_NO_ERROR;
2582 			case SCTP_PARAM_ERR_CAUSE:
2583 				length = sizeof(sctp_addip_param_t);
2584 				err_param = (sctp_errhdr_t *)
2585 					   ((void *)asconf_ack_param + length);
2586 				asconf_ack_len -= length;
2587 				if (asconf_ack_len > 0)
2588 					return err_param->cause;
2589 				else
2590 					return SCTP_ERROR_INV_PARAM;
2591 				break;
2592 			default:
2593 				return SCTP_ERROR_INV_PARAM;
2594 			}
2595 		}
2596 
2597 		length = ntohs(asconf_ack_param->param_hdr.length);
2598 		asconf_ack_param = (sctp_addip_param_t *)
2599 					((void *)asconf_ack_param + length);
2600 		asconf_ack_len -= length;
2601 	}
2602 
2603 	return err_code;
2604 }
2605 
2606 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
sctp_process_asconf_ack(struct sctp_association * asoc,struct sctp_chunk * asconf_ack)2607 int sctp_process_asconf_ack(struct sctp_association *asoc,
2608 			    struct sctp_chunk *asconf_ack)
2609 {
2610 	struct sctp_chunk	*asconf = asoc->addip_last_asconf;
2611 	union sctp_addr_param	*addr_param;
2612 	sctp_addip_param_t	*asconf_param;
2613 	int	length = 0;
2614 	int	asconf_len = asconf->skb->len;
2615 	int	all_param_pass = 0;
2616 	int	no_err = 1;
2617 	int	retval = 0;
2618 	__u16	err_code = SCTP_ERROR_NO_ERROR;
2619 
2620 	/* Skip the chunkhdr and addiphdr from the last asconf sent and store
2621 	 * a pointer to address parameter.
2622 	 */
2623 	length = sizeof(sctp_addip_chunk_t);
2624 	addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2625 	asconf_len -= length;
2626 
2627 	/* Skip the address parameter in the last asconf sent and store a
2628 	 * pointer to the first asconf paramter.
2629 	 */
2630 	length = ntohs(addr_param->v4.param_hdr.length);
2631 	asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2632 	asconf_len -= length;
2633 
2634 	/* ADDIP 4.1
2635 	 * A8) If there is no response(s) to specific TLV parameter(s), and no
2636 	 * failures are indicated, then all request(s) are considered
2637 	 * successful.
2638 	 */
2639 	if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2640 		all_param_pass = 1;
2641 
2642 	/* Process the TLVs contained in the last sent ASCONF chunk. */
2643 	while (asconf_len > 0) {
2644 		if (all_param_pass)
2645 			err_code = SCTP_ERROR_NO_ERROR;
2646 		else {
2647 			err_code = sctp_get_asconf_response(asconf_ack,
2648 							    asconf_param,
2649 							    no_err);
2650 			if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2651 				no_err = 0;
2652 		}
2653 
2654 		switch (err_code) {
2655 		case SCTP_ERROR_NO_ERROR:
2656 			retval = sctp_asconf_param_success(asoc, asconf_param);
2657 			break;
2658 
2659 		case SCTP_ERROR_RSRC_LOW:
2660 			retval = 1;
2661 			break;
2662 
2663 		case SCTP_ERROR_INV_PARAM:
2664 			/* Disable sending this type of asconf parameter in
2665 			 * future.
2666 			 */
2667 			asoc->peer.addip_disabled_mask |=
2668 				asconf_param->param_hdr.type;
2669 			break;
2670 
2671 		case SCTP_ERROR_REQ_REFUSED:
2672 		case SCTP_ERROR_DEL_LAST_IP:
2673 		case SCTP_ERROR_DEL_SRC_IP:
2674 		default:
2675 			 break;
2676 		}
2677 
2678 		/* Skip the processed asconf parameter and move to the next
2679 		 * one.
2680 	 	 */
2681 		length = ntohs(asconf_param->param_hdr.length);
2682 		asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2683 						      length);
2684 		asconf_len -= length;
2685 	}
2686 
2687 	/* Free the cached last sent asconf chunk. */
2688 	sctp_chunk_free(asconf);
2689 	asoc->addip_last_asconf = NULL;
2690 
2691 	/* Send the next asconf chunk from the addip chunk queue. */
2692 	asconf = (struct sctp_chunk *)__skb_dequeue(&asoc->addip_chunks);
2693 	if (asconf) {
2694 		/* Hold the chunk until an ASCONF_ACK is received. */
2695 		sctp_chunk_hold(asconf);
2696 		if (sctp_primitive_ASCONF(asoc, asconf))
2697 			sctp_chunk_free(asconf);
2698 		else
2699 			asoc->addip_last_asconf = asconf;
2700 	}
2701 
2702 	return retval;
2703 }
2704 
2705 /* Make a FWD TSN chunk. */
sctp_make_fwdtsn(const struct sctp_association * asoc,__u32 new_cum_tsn,size_t nstreams,struct sctp_fwdtsn_skip * skiplist)2706 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2707 				    __u32 new_cum_tsn, size_t nstreams,
2708 				    struct sctp_fwdtsn_skip *skiplist)
2709 {
2710 	struct sctp_chunk *retval = NULL;
2711 	struct sctp_fwdtsn_chunk *ftsn_chunk;
2712 	struct sctp_fwdtsn_hdr ftsn_hdr;
2713 	struct sctp_fwdtsn_skip skip;
2714 	size_t hint;
2715 	int i;
2716 
2717 	hint = (nstreams + 1) * sizeof(__u32);
2718 
2719 	/* Maybe set the T-bit if we have no association.  */
2720 	retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2721 
2722 	if (!retval)
2723 		return NULL;
2724 
2725 	ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2726 
2727 	ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2728 	retval->subh.fwdtsn_hdr =
2729 		sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2730 
2731 	for (i = 0; i < nstreams; i++) {
2732 		skip.stream = skiplist[i].stream;
2733 		skip.ssn = skiplist[i].ssn;
2734 		sctp_addto_chunk(retval, sizeof(skip), &skip);
2735 	}
2736 
2737 	return retval;
2738 }
2739