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