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