Pull fujitsu-v3 into release branch
[deliverable/linux.git] / net / sctp / sm_make_chunk.c
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
68 SCTP_STATIC
69 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
70 __u8 type, __u8 flags, int paylen);
71 static 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);
76 static int sctp_process_param(struct sctp_association *asoc,
77 union sctp_params param,
78 const union sctp_addr *peer_addr,
79 gfp_t gfp);
80
81 /* What was the inbound interface for this chunk? */
82 int sctp_chunk_iif(const struct sctp_chunk *chunk)
83 {
84 struct sctp_af *af;
85 int iif = 0;
86
87 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
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 */
99 static const struct sctp_paramhdr ecap_param = {
100 SCTP_PARAM_ECN_CAPABLE,
101 __constant_htons(sizeof(struct sctp_paramhdr)),
102 };
103 static 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 */
112 void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
113 size_t paylen)
114 {
115 sctp_errhdr_t err;
116 __u16 len;
117
118 /* Cause code constants are now defined in network order. */
119 err.cause = cause_code;
120 len = sizeof(sctp_errhdr_t) + paylen;
121 err.length = htons(len);
122 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
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 */
169 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
170 const struct sctp_bind_addr *bp,
171 gfp_t gfp, int vparam_len)
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;
180 __be16 types[2];
181 sctp_adaptation_ind_param_t aiparam;
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);
246 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
247 aiparam.param_hdr.length = htons(sizeof(aiparam));
248 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
249 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
250 nodata:
251 kfree(addrs.v);
252 return retval;
253 }
254
255 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
256 const struct sctp_chunk *chunk,
257 gfp_t gfp, int unkparam_len)
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;
266 sctp_adaptation_ind_param_t aiparam;
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
292 /* Tell peer that we'll do ECN only if peer advertised such cap. */
293 if (asoc->peer.ecn_capable)
294 chunksize += sizeof(ecap_param);
295
296 /* Tell peer that we'll do PR-SCTP only if peer advertised. */
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
320 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
321 aiparam.param_hdr.length = htons(sizeof(aiparam));
322 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
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
340 nomem_chunk:
341 kfree(cookie);
342 nomem_cookie:
343 kfree(addrs.v);
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 */
381 struct 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
410 nodata:
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 */
432 struct 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 */
477 struct 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
507 nodata:
508 return retval;
509 }
510
511 /* Make an ECNE chunk. This is a congestion experienced report. */
512 struct 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
526 nodata:
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 */
533 struct 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. */
549 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
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
563 nodata:
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 */
571 struct 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
647 nodata:
648 return retval;
649 }
650
651 /* Make a SHUTDOWN chunk. */
652 struct 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;
672 nodata:
673 return retval;
674 }
675
676 struct 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
698 struct 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
705 /* Set the T-bit if we have no association (vtag will be
706 * reflected)
707 */
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
725 return retval;
726 }
727
728 /* Create an ABORT. Note that we set the T bit if we have no
729 * association, except when responding to an INIT (sctpimpguide 2.41).
730 */
731 struct 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
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 }
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. */
767 struct 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;
772 __be32 payload;
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);
782 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
783 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
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
797 no_mem:
798 return retval;
799 }
800
801 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
802 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
803 const struct msghdr *msg,
804 size_t paylen)
805 {
806 struct sctp_chunk *retval;
807 void *payload = NULL;
808 int err;
809
810 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
811 if (!retval)
812 goto err_chunk;
813
814 if (paylen) {
815 /* Put the msg_iov together into payload. */
816 payload = kmalloc(paylen, GFP_KERNEL);
817 if (!payload)
818 goto err_payload;
819
820 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
821 if (err < 0)
822 goto err_copy;
823 }
824
825 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
826 sctp_addto_chunk(retval, paylen, payload);
827
828 if (paylen)
829 kfree(payload);
830
831 return retval;
832
833 err_copy:
834 kfree(payload);
835 err_payload:
836 sctp_chunk_free(retval);
837 retval = NULL;
838 err_chunk:
839 return retval;
840 }
841
842 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
843 struct sctp_chunk *sctp_make_abort_violation(
844 const struct sctp_association *asoc,
845 const struct sctp_chunk *chunk,
846 const __u8 *payload,
847 const size_t paylen)
848 {
849 struct sctp_chunk *retval;
850 struct sctp_paramhdr phdr;
851
852 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
853 + sizeof(sctp_paramhdr_t));
854 if (!retval)
855 goto end;
856
857 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
858 + sizeof(sctp_paramhdr_t));
859
860 phdr.type = htons(chunk->chunk_hdr->type);
861 phdr.length = chunk->chunk_hdr->length;
862 sctp_addto_chunk(retval, paylen, payload);
863 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
864
865 end:
866 return retval;
867 }
868
869 /* Make a HEARTBEAT chunk. */
870 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
871 const struct sctp_transport *transport,
872 const void *payload, const size_t paylen)
873 {
874 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
875 0, paylen);
876
877 if (!retval)
878 goto nodata;
879
880 /* Cast away the 'const', as this is just telling the chunk
881 * what transport it belongs to.
882 */
883 retval->transport = (struct sctp_transport *) transport;
884 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
885
886 nodata:
887 return retval;
888 }
889
890 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
891 const struct sctp_chunk *chunk,
892 const void *payload, const size_t paylen)
893 {
894 struct sctp_chunk *retval;
895
896 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
897 if (!retval)
898 goto nodata;
899
900 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
901
902 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
903 *
904 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
905 * HEARTBEAT ACK, * etc.) to the same destination transport
906 * address from which it * received the DATA or control chunk
907 * to which it is replying.
908 *
909 * [HBACK back to where the HEARTBEAT came from.]
910 */
911 if (chunk)
912 retval->transport = chunk->transport;
913
914 nodata:
915 return retval;
916 }
917
918 /* Create an Operation Error chunk with the specified space reserved.
919 * This routine can be used for containing multiple causes in the chunk.
920 */
921 static struct sctp_chunk *sctp_make_op_error_space(
922 const struct sctp_association *asoc,
923 const struct sctp_chunk *chunk,
924 size_t size)
925 {
926 struct sctp_chunk *retval;
927
928 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
929 sizeof(sctp_errhdr_t) + size);
930 if (!retval)
931 goto nodata;
932
933 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
934 *
935 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
936 * HEARTBEAT ACK, etc.) to the same destination transport
937 * address from which it received the DATA or control chunk
938 * to which it is replying.
939 *
940 */
941 if (chunk)
942 retval->transport = chunk->transport;
943
944 nodata:
945 return retval;
946 }
947
948 /* Create an Operation Error chunk. */
949 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
950 const struct sctp_chunk *chunk,
951 __be16 cause_code, const void *payload,
952 size_t paylen)
953 {
954 struct sctp_chunk *retval;
955
956 retval = sctp_make_op_error_space(asoc, chunk, paylen);
957 if (!retval)
958 goto nodata;
959
960 sctp_init_cause(retval, cause_code, paylen);
961 sctp_addto_chunk(retval, paylen, payload);
962
963 nodata:
964 return retval;
965 }
966
967 /********************************************************************
968 * 2nd Level Abstractions
969 ********************************************************************/
970
971 /* Turn an skb into a chunk.
972 * FIXME: Eventually move the structure directly inside the skb->cb[].
973 */
974 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
975 const struct sctp_association *asoc,
976 struct sock *sk)
977 {
978 struct sctp_chunk *retval;
979
980 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
981
982 if (!retval)
983 goto nodata;
984
985 if (!sk) {
986 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
987 }
988
989 INIT_LIST_HEAD(&retval->list);
990 retval->skb = skb;
991 retval->asoc = (struct sctp_association *)asoc;
992 retval->resent = 0;
993 retval->has_tsn = 0;
994 retval->has_ssn = 0;
995 retval->rtt_in_progress = 0;
996 retval->sent_at = 0;
997 retval->singleton = 1;
998 retval->end_of_packet = 0;
999 retval->ecn_ce_done = 0;
1000 retval->pdiscard = 0;
1001
1002 /* sctpimpguide-05.txt Section 2.8.2
1003 * M1) Each time a new DATA chunk is transmitted
1004 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1005 * 'TSN.Missing.Report' count will be used to determine missing chunks
1006 * and when to fast retransmit.
1007 */
1008 retval->tsn_missing_report = 0;
1009 retval->tsn_gap_acked = 0;
1010 retval->fast_retransmit = 0;
1011
1012 /* If this is a fragmented message, track all fragments
1013 * of the message (for SEND_FAILED).
1014 */
1015 retval->msg = NULL;
1016
1017 /* Polish the bead hole. */
1018 INIT_LIST_HEAD(&retval->transmitted_list);
1019 INIT_LIST_HEAD(&retval->frag_list);
1020 SCTP_DBG_OBJCNT_INC(chunk);
1021 atomic_set(&retval->refcnt, 1);
1022
1023 nodata:
1024 return retval;
1025 }
1026
1027 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1028 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1029 union sctp_addr *dest)
1030 {
1031 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1032 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1033 }
1034
1035 /* Extract the source address from a chunk. */
1036 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1037 {
1038 /* If we have a known transport, use that. */
1039 if (chunk->transport) {
1040 return &chunk->transport->ipaddr;
1041 } else {
1042 /* Otherwise, extract it from the IP header. */
1043 return &chunk->source;
1044 }
1045 }
1046
1047 /* Create a new chunk, setting the type and flags headers from the
1048 * arguments, reserving enough space for a 'paylen' byte payload.
1049 */
1050 SCTP_STATIC
1051 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1052 __u8 type, __u8 flags, int paylen)
1053 {
1054 struct sctp_chunk *retval;
1055 sctp_chunkhdr_t *chunk_hdr;
1056 struct sk_buff *skb;
1057 struct sock *sk;
1058
1059 /* No need to allocate LL here, as this is only a chunk. */
1060 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1061 GFP_ATOMIC);
1062 if (!skb)
1063 goto nodata;
1064
1065 /* Make room for the chunk header. */
1066 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1067 chunk_hdr->type = type;
1068 chunk_hdr->flags = flags;
1069 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1070
1071 sk = asoc ? asoc->base.sk : NULL;
1072 retval = sctp_chunkify(skb, asoc, sk);
1073 if (!retval) {
1074 kfree_skb(skb);
1075 goto nodata;
1076 }
1077
1078 retval->chunk_hdr = chunk_hdr;
1079 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1080
1081 /* Set the skb to the belonging sock for accounting. */
1082 skb->sk = sk;
1083
1084 return retval;
1085 nodata:
1086 return NULL;
1087 }
1088
1089
1090 /* Release the memory occupied by a chunk. */
1091 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1092 {
1093 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1094 dev_kfree_skb(chunk->skb);
1095
1096 SCTP_DBG_OBJCNT_DEC(chunk);
1097 kmem_cache_free(sctp_chunk_cachep, chunk);
1098 }
1099
1100 /* Possibly, free the chunk. */
1101 void sctp_chunk_free(struct sctp_chunk *chunk)
1102 {
1103 BUG_ON(!list_empty(&chunk->list));
1104 list_del_init(&chunk->transmitted_list);
1105
1106 /* Release our reference on the message tracker. */
1107 if (chunk->msg)
1108 sctp_datamsg_put(chunk->msg);
1109
1110 sctp_chunk_put(chunk);
1111 }
1112
1113 /* Grab a reference to the chunk. */
1114 void sctp_chunk_hold(struct sctp_chunk *ch)
1115 {
1116 atomic_inc(&ch->refcnt);
1117 }
1118
1119 /* Release a reference to the chunk. */
1120 void sctp_chunk_put(struct sctp_chunk *ch)
1121 {
1122 if (atomic_dec_and_test(&ch->refcnt))
1123 sctp_chunk_destroy(ch);
1124 }
1125
1126 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1127 * enough.
1128 */
1129 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1130 {
1131 void *target;
1132 void *padding;
1133 int chunklen = ntohs(chunk->chunk_hdr->length);
1134 int padlen = WORD_ROUND(chunklen) - chunklen;
1135
1136 padding = skb_put(chunk->skb, padlen);
1137 target = skb_put(chunk->skb, len);
1138
1139 memset(padding, 0, padlen);
1140 memcpy(target, data, len);
1141
1142 /* Adjust the chunk length field. */
1143 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1144 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1145
1146 return target;
1147 }
1148
1149 /* Append bytes to the end of a parameter. Will panic if chunk is not big
1150 * enough.
1151 */
1152 void *sctp_addto_param(struct sctp_chunk *chunk, int len, const void *data)
1153 {
1154 void *target;
1155 int chunklen = ntohs(chunk->chunk_hdr->length);
1156
1157 target = skb_put(chunk->skb, len);
1158
1159 memcpy(target, data, len);
1160
1161 /* Adjust the chunk length field. */
1162 chunk->chunk_hdr->length = htons(chunklen + len);
1163 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1164
1165 return target;
1166 }
1167
1168 /* Append bytes from user space to the end of a chunk. Will panic if
1169 * chunk is not big enough.
1170 * Returns a kernel err value.
1171 */
1172 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1173 struct iovec *data)
1174 {
1175 __u8 *target;
1176 int err = 0;
1177
1178 /* Make room in chunk for data. */
1179 target = skb_put(chunk->skb, len);
1180
1181 /* Copy data (whole iovec) into chunk */
1182 if ((err = memcpy_fromiovecend(target, data, off, len)))
1183 goto out;
1184
1185 /* Adjust the chunk length field. */
1186 chunk->chunk_hdr->length =
1187 htons(ntohs(chunk->chunk_hdr->length) + len);
1188 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1189
1190 out:
1191 return err;
1192 }
1193
1194 /* Helper function to assign a TSN if needed. This assumes that both
1195 * the data_hdr and association have already been assigned.
1196 */
1197 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1198 {
1199 struct sctp_datamsg *msg;
1200 struct sctp_chunk *lchunk;
1201 struct sctp_stream *stream;
1202 __u16 ssn;
1203 __u16 sid;
1204
1205 if (chunk->has_ssn)
1206 return;
1207
1208 /* All fragments will be on the same stream */
1209 sid = ntohs(chunk->subh.data_hdr->stream);
1210 stream = &chunk->asoc->ssnmap->out;
1211
1212 /* Now assign the sequence number to the entire message.
1213 * All fragments must have the same stream sequence number.
1214 */
1215 msg = chunk->msg;
1216 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1217 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1218 ssn = 0;
1219 } else {
1220 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1221 ssn = sctp_ssn_next(stream, sid);
1222 else
1223 ssn = sctp_ssn_peek(stream, sid);
1224 }
1225
1226 lchunk->subh.data_hdr->ssn = htons(ssn);
1227 lchunk->has_ssn = 1;
1228 }
1229 }
1230
1231 /* Helper function to assign a TSN if needed. This assumes that both
1232 * the data_hdr and association have already been assigned.
1233 */
1234 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1235 {
1236 if (!chunk->has_tsn) {
1237 /* This is the last possible instant to
1238 * assign a TSN.
1239 */
1240 chunk->subh.data_hdr->tsn =
1241 htonl(sctp_association_get_next_tsn(chunk->asoc));
1242 chunk->has_tsn = 1;
1243 }
1244 }
1245
1246 /* Create a CLOSED association to use with an incoming packet. */
1247 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1248 struct sctp_chunk *chunk,
1249 gfp_t gfp)
1250 {
1251 struct sctp_association *asoc;
1252 struct sk_buff *skb;
1253 sctp_scope_t scope;
1254 struct sctp_af *af;
1255
1256 /* Create the bare association. */
1257 scope = sctp_scope(sctp_source(chunk));
1258 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1259 if (!asoc)
1260 goto nodata;
1261 asoc->temp = 1;
1262 skb = chunk->skb;
1263 /* Create an entry for the source address of the packet. */
1264 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1265 if (unlikely(!af))
1266 goto fail;
1267 af->from_skb(&asoc->c.peer_addr, skb, 1);
1268 nodata:
1269 return asoc;
1270
1271 fail:
1272 sctp_association_free(asoc);
1273 return NULL;
1274 }
1275
1276 /* Build a cookie representing asoc.
1277 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1278 */
1279 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1280 const struct sctp_association *asoc,
1281 const struct sctp_chunk *init_chunk,
1282 int *cookie_len,
1283 const __u8 *raw_addrs, int addrs_len)
1284 {
1285 sctp_cookie_param_t *retval;
1286 struct sctp_signed_cookie *cookie;
1287 struct scatterlist sg;
1288 int headersize, bodysize;
1289 unsigned int keylen;
1290 char *key;
1291
1292 /* Header size is static data prior to the actual cookie, including
1293 * any padding.
1294 */
1295 headersize = sizeof(sctp_paramhdr_t) +
1296 (sizeof(struct sctp_signed_cookie) -
1297 sizeof(struct sctp_cookie));
1298 bodysize = sizeof(struct sctp_cookie)
1299 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1300
1301 /* Pad out the cookie to a multiple to make the signature
1302 * functions simpler to write.
1303 */
1304 if (bodysize % SCTP_COOKIE_MULTIPLE)
1305 bodysize += SCTP_COOKIE_MULTIPLE
1306 - (bodysize % SCTP_COOKIE_MULTIPLE);
1307 *cookie_len = headersize + bodysize;
1308
1309 /* Clear this memory since we are sending this data structure
1310 * out on the network.
1311 */
1312 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1313 if (!retval)
1314 goto nodata;
1315
1316 cookie = (struct sctp_signed_cookie *) retval->body;
1317
1318 /* Set up the parameter header. */
1319 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1320 retval->p.length = htons(*cookie_len);
1321
1322 /* Copy the cookie part of the association itself. */
1323 cookie->c = asoc->c;
1324 /* Save the raw address list length in the cookie. */
1325 cookie->c.raw_addr_list_len = addrs_len;
1326
1327 /* Remember PR-SCTP capability. */
1328 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1329
1330 /* Save adaptation indication in the cookie. */
1331 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1332
1333 /* Set an expiration time for the cookie. */
1334 do_gettimeofday(&cookie->c.expiration);
1335 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1336
1337 /* Copy the peer's init packet. */
1338 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1339 ntohs(init_chunk->chunk_hdr->length));
1340
1341 /* Copy the raw local address list of the association. */
1342 memcpy((__u8 *)&cookie->c.peer_init[0] +
1343 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1344
1345 if (sctp_sk(ep->base.sk)->hmac) {
1346 struct hash_desc desc;
1347
1348 /* Sign the message. */
1349 sg.page = virt_to_page(&cookie->c);
1350 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1351 sg.length = bodysize;
1352 keylen = SCTP_SECRET_SIZE;
1353 key = (char *)ep->secret_key[ep->current_key];
1354 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1355 desc.flags = 0;
1356
1357 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1358 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1359 goto free_cookie;
1360 }
1361
1362 return retval;
1363
1364 free_cookie:
1365 kfree(retval);
1366 nodata:
1367 *cookie_len = 0;
1368 return NULL;
1369 }
1370
1371 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1372 struct sctp_association *sctp_unpack_cookie(
1373 const struct sctp_endpoint *ep,
1374 const struct sctp_association *asoc,
1375 struct sctp_chunk *chunk, gfp_t gfp,
1376 int *error, struct sctp_chunk **errp)
1377 {
1378 struct sctp_association *retval = NULL;
1379 struct sctp_signed_cookie *cookie;
1380 struct sctp_cookie *bear_cookie;
1381 int headersize, bodysize, fixed_size;
1382 __u8 *digest = ep->digest;
1383 struct scatterlist sg;
1384 unsigned int keylen, len;
1385 char *key;
1386 sctp_scope_t scope;
1387 struct sk_buff *skb = chunk->skb;
1388 struct timeval tv;
1389 struct hash_desc desc;
1390
1391 /* Header size is static data prior to the actual cookie, including
1392 * any padding.
1393 */
1394 headersize = sizeof(sctp_chunkhdr_t) +
1395 (sizeof(struct sctp_signed_cookie) -
1396 sizeof(struct sctp_cookie));
1397 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1398 fixed_size = headersize + sizeof(struct sctp_cookie);
1399
1400 /* Verify that the chunk looks like it even has a cookie.
1401 * There must be enough room for our cookie and our peer's
1402 * INIT chunk.
1403 */
1404 len = ntohs(chunk->chunk_hdr->length);
1405 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1406 goto malformed;
1407
1408 /* Verify that the cookie has been padded out. */
1409 if (bodysize % SCTP_COOKIE_MULTIPLE)
1410 goto malformed;
1411
1412 /* Process the cookie. */
1413 cookie = chunk->subh.cookie_hdr;
1414 bear_cookie = &cookie->c;
1415
1416 if (!sctp_sk(ep->base.sk)->hmac)
1417 goto no_hmac;
1418
1419 /* Check the signature. */
1420 keylen = SCTP_SECRET_SIZE;
1421 sg.page = virt_to_page(bear_cookie);
1422 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1423 sg.length = bodysize;
1424 key = (char *)ep->secret_key[ep->current_key];
1425 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1426 desc.flags = 0;
1427
1428 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1429 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1430 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1431 *error = -SCTP_IERROR_NOMEM;
1432 goto fail;
1433 }
1434
1435 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1436 /* Try the previous key. */
1437 key = (char *)ep->secret_key[ep->last_key];
1438 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1439 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1440 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1441 *error = -SCTP_IERROR_NOMEM;
1442 goto fail;
1443 }
1444
1445 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1446 /* Yikes! Still bad signature! */
1447 *error = -SCTP_IERROR_BAD_SIG;
1448 goto fail;
1449 }
1450 }
1451
1452 no_hmac:
1453 /* IG Section 2.35.2:
1454 * 3) Compare the port numbers and the verification tag contained
1455 * within the COOKIE ECHO chunk to the actual port numbers and the
1456 * verification tag within the SCTP common header of the received
1457 * packet. If these values do not match the packet MUST be silently
1458 * discarded,
1459 */
1460 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1461 *error = -SCTP_IERROR_BAD_TAG;
1462 goto fail;
1463 }
1464
1465 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1466 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1467 *error = -SCTP_IERROR_BAD_PORTS;
1468 goto fail;
1469 }
1470
1471 /* Check to see if the cookie is stale. If there is already
1472 * an association, there is no need to check cookie's expiration
1473 * for init collision case of lost COOKIE ACK.
1474 * If skb has been timestamped, then use the stamp, otherwise
1475 * use current time. This introduces a small possibility that
1476 * that a cookie may be considered expired, but his would only slow
1477 * down the new association establishment instead of every packet.
1478 */
1479 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1480 skb_get_timestamp(skb, &tv);
1481 else
1482 do_gettimeofday(&tv);
1483
1484 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1485 /*
1486 * Section 3.3.10.3 Stale Cookie Error (3)
1487 *
1488 * Cause of error
1489 * ---------------
1490 * Stale Cookie Error: Indicates the receipt of a valid State
1491 * Cookie that has expired.
1492 */
1493 len = ntohs(chunk->chunk_hdr->length);
1494 *errp = sctp_make_op_error_space(asoc, chunk, len);
1495 if (*errp) {
1496 suseconds_t usecs = (tv.tv_sec -
1497 bear_cookie->expiration.tv_sec) * 1000000L +
1498 tv.tv_usec - bear_cookie->expiration.tv_usec;
1499 __be32 n = htonl(usecs);
1500
1501 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1502 sizeof(n));
1503 sctp_addto_chunk(*errp, sizeof(n), &n);
1504 *error = -SCTP_IERROR_STALE_COOKIE;
1505 } else
1506 *error = -SCTP_IERROR_NOMEM;
1507
1508 goto fail;
1509 }
1510
1511 /* Make a new base association. */
1512 scope = sctp_scope(sctp_source(chunk));
1513 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1514 if (!retval) {
1515 *error = -SCTP_IERROR_NOMEM;
1516 goto fail;
1517 }
1518
1519 /* Set up our peer's port number. */
1520 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1521
1522 /* Populate the association from the cookie. */
1523 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1524
1525 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1526 GFP_ATOMIC) < 0) {
1527 *error = -SCTP_IERROR_NOMEM;
1528 goto fail;
1529 }
1530
1531 /* Also, add the destination address. */
1532 if (list_empty(&retval->base.bind_addr.address_list)) {
1533 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
1534 GFP_ATOMIC);
1535 }
1536
1537 retval->next_tsn = retval->c.initial_tsn;
1538 retval->ctsn_ack_point = retval->next_tsn - 1;
1539 retval->addip_serial = retval->c.initial_tsn;
1540 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1541 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1542 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1543
1544 /* The INIT stuff will be done by the side effects. */
1545 return retval;
1546
1547 fail:
1548 if (retval)
1549 sctp_association_free(retval);
1550
1551 return NULL;
1552
1553 malformed:
1554 /* Yikes! The packet is either corrupt or deliberately
1555 * malformed.
1556 */
1557 *error = -SCTP_IERROR_MALFORMED;
1558 goto fail;
1559 }
1560
1561 /********************************************************************
1562 * 3rd Level Abstractions
1563 ********************************************************************/
1564
1565 struct __sctp_missing {
1566 __be32 num_missing;
1567 __be16 type;
1568 } __attribute__((packed));
1569
1570 /*
1571 * Report a missing mandatory parameter.
1572 */
1573 static int sctp_process_missing_param(const struct sctp_association *asoc,
1574 sctp_param_t paramtype,
1575 struct sctp_chunk *chunk,
1576 struct sctp_chunk **errp)
1577 {
1578 struct __sctp_missing report;
1579 __u16 len;
1580
1581 len = WORD_ROUND(sizeof(report));
1582
1583 /* Make an ERROR chunk, preparing enough room for
1584 * returning multiple unknown parameters.
1585 */
1586 if (!*errp)
1587 *errp = sctp_make_op_error_space(asoc, chunk, len);
1588
1589 if (*errp) {
1590 report.num_missing = htonl(1);
1591 report.type = paramtype;
1592 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1593 sizeof(report));
1594 sctp_addto_chunk(*errp, sizeof(report), &report);
1595 }
1596
1597 /* Stop processing this chunk. */
1598 return 0;
1599 }
1600
1601 /* Report an Invalid Mandatory Parameter. */
1602 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1603 struct sctp_chunk *chunk,
1604 struct sctp_chunk **errp)
1605 {
1606 /* Invalid Mandatory Parameter Error has no payload. */
1607
1608 if (!*errp)
1609 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1610
1611 if (*errp)
1612 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1613
1614 /* Stop processing this chunk. */
1615 return 0;
1616 }
1617
1618 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1619 struct sctp_paramhdr *param,
1620 const struct sctp_chunk *chunk,
1621 struct sctp_chunk **errp)
1622 {
1623 char error[] = "The following parameter had invalid length:";
1624 size_t payload_len = WORD_ROUND(sizeof(error)) +
1625 sizeof(sctp_paramhdr_t);
1626
1627
1628 /* Create an error chunk and fill it in with our payload. */
1629 if (!*errp)
1630 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1631
1632 if (*errp) {
1633 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION,
1634 sizeof(error) + sizeof(sctp_paramhdr_t));
1635 sctp_addto_chunk(*errp, sizeof(error), error);
1636 sctp_addto_param(*errp, sizeof(sctp_paramhdr_t), param);
1637 }
1638
1639 return 0;
1640 }
1641
1642
1643 /* Do not attempt to handle the HOST_NAME parm. However, do
1644 * send back an indicator to the peer.
1645 */
1646 static int sctp_process_hn_param(const struct sctp_association *asoc,
1647 union sctp_params param,
1648 struct sctp_chunk *chunk,
1649 struct sctp_chunk **errp)
1650 {
1651 __u16 len = ntohs(param.p->length);
1652
1653 /* Make an ERROR chunk. */
1654 if (!*errp)
1655 *errp = sctp_make_op_error_space(asoc, chunk, len);
1656
1657 if (*errp) {
1658 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1659 sctp_addto_chunk(*errp, len, param.v);
1660 }
1661
1662 /* Stop processing this chunk. */
1663 return 0;
1664 }
1665
1666 /* RFC 3.2.1 & the Implementers Guide 2.2.
1667 *
1668 * The Parameter Types are encoded such that the
1669 * highest-order two bits specify the action that must be
1670 * taken if the processing endpoint does not recognize the
1671 * Parameter Type.
1672 *
1673 * 00 - Stop processing this SCTP chunk and discard it,
1674 * do not process any further chunks within it.
1675 *
1676 * 01 - Stop processing this SCTP chunk and discard it,
1677 * do not process any further chunks within it, and report
1678 * the unrecognized parameter in an 'Unrecognized
1679 * Parameter Type' (in either an ERROR or in the INIT ACK).
1680 *
1681 * 10 - Skip this parameter and continue processing.
1682 *
1683 * 11 - Skip this parameter and continue processing but
1684 * report the unrecognized parameter in an
1685 * 'Unrecognized Parameter Type' (in either an ERROR or in
1686 * the INIT ACK).
1687 *
1688 * Return value:
1689 * 0 - discard the chunk
1690 * 1 - continue with the chunk
1691 */
1692 static int sctp_process_unk_param(const struct sctp_association *asoc,
1693 union sctp_params param,
1694 struct sctp_chunk *chunk,
1695 struct sctp_chunk **errp)
1696 {
1697 int retval = 1;
1698
1699 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1700 case SCTP_PARAM_ACTION_DISCARD:
1701 retval = 0;
1702 break;
1703 case SCTP_PARAM_ACTION_DISCARD_ERR:
1704 retval = 0;
1705 /* Make an ERROR chunk, preparing enough room for
1706 * returning multiple unknown parameters.
1707 */
1708 if (NULL == *errp)
1709 *errp = sctp_make_op_error_space(asoc, chunk,
1710 ntohs(chunk->chunk_hdr->length));
1711
1712 if (*errp) {
1713 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1714 WORD_ROUND(ntohs(param.p->length)));
1715 sctp_addto_chunk(*errp,
1716 WORD_ROUND(ntohs(param.p->length)),
1717 param.v);
1718 }
1719
1720 break;
1721 case SCTP_PARAM_ACTION_SKIP:
1722 break;
1723 case SCTP_PARAM_ACTION_SKIP_ERR:
1724 /* Make an ERROR chunk, preparing enough room for
1725 * returning multiple unknown parameters.
1726 */
1727 if (NULL == *errp)
1728 *errp = sctp_make_op_error_space(asoc, chunk,
1729 ntohs(chunk->chunk_hdr->length));
1730
1731 if (*errp) {
1732 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1733 WORD_ROUND(ntohs(param.p->length)));
1734 sctp_addto_chunk(*errp,
1735 WORD_ROUND(ntohs(param.p->length)),
1736 param.v);
1737 } else {
1738 /* If there is no memory for generating the ERROR
1739 * report as specified, an ABORT will be triggered
1740 * to the peer and the association won't be
1741 * established.
1742 */
1743 retval = 0;
1744 }
1745
1746 break;
1747 default:
1748 break;
1749 }
1750
1751 return retval;
1752 }
1753
1754 /* Find unrecognized parameters in the chunk.
1755 * Return values:
1756 * 0 - discard the chunk
1757 * 1 - continue with the chunk
1758 */
1759 static int sctp_verify_param(const struct sctp_association *asoc,
1760 union sctp_params param,
1761 sctp_cid_t cid,
1762 struct sctp_chunk *chunk,
1763 struct sctp_chunk **err_chunk)
1764 {
1765 int retval = 1;
1766
1767 /* FIXME - This routine is not looking at each parameter per the
1768 * chunk type, i.e., unrecognized parameters should be further
1769 * identified based on the chunk id.
1770 */
1771
1772 switch (param.p->type) {
1773 case SCTP_PARAM_IPV4_ADDRESS:
1774 case SCTP_PARAM_IPV6_ADDRESS:
1775 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1776 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1777 case SCTP_PARAM_STATE_COOKIE:
1778 case SCTP_PARAM_HEARTBEAT_INFO:
1779 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1780 case SCTP_PARAM_ECN_CAPABLE:
1781 case SCTP_PARAM_ADAPTATION_LAYER_IND:
1782 break;
1783
1784 case SCTP_PARAM_HOST_NAME_ADDRESS:
1785 /* Tell the peer, we won't support this param. */
1786 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1787 case SCTP_PARAM_FWD_TSN_SUPPORT:
1788 if (sctp_prsctp_enable)
1789 break;
1790 /* Fall Through */
1791 default:
1792 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1793 ntohs(param.p->type), cid);
1794 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1795
1796 break;
1797 }
1798 return retval;
1799 }
1800
1801 /* Verify the INIT packet before we process it. */
1802 int sctp_verify_init(const struct sctp_association *asoc,
1803 sctp_cid_t cid,
1804 sctp_init_chunk_t *peer_init,
1805 struct sctp_chunk *chunk,
1806 struct sctp_chunk **errp)
1807 {
1808 union sctp_params param;
1809 int has_cookie = 0;
1810
1811 /* Verify stream values are non-zero. */
1812 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1813 (0 == peer_init->init_hdr.num_inbound_streams) ||
1814 (0 == peer_init->init_hdr.init_tag) ||
1815 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
1816
1817 sctp_process_inv_mandatory(asoc, chunk, errp);
1818 return 0;
1819 }
1820
1821 /* Check for missing mandatory parameters. */
1822 sctp_walk_params(param, peer_init, init_hdr.params) {
1823
1824 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1825 has_cookie = 1;
1826
1827 } /* for (loop through all parameters) */
1828
1829 /* There is a possibility that a parameter length was bad and
1830 * in that case we would have stoped walking the parameters.
1831 * The current param.p would point at the bad one.
1832 * Current consensus on the mailing list is to generate a PROTOCOL
1833 * VIOLATION error. We build the ERROR chunk here and let the normal
1834 * error handling code build and send the packet.
1835 */
1836 if (param.v != (void*)chunk->chunk_end) {
1837 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1838 return 0;
1839 }
1840
1841 /* The only missing mandatory param possible today is
1842 * the state cookie for an INIT-ACK chunk.
1843 */
1844 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1845 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1846 chunk, errp);
1847 return 0;
1848 }
1849
1850 /* Find unrecognized parameters. */
1851
1852 sctp_walk_params(param, peer_init, init_hdr.params) {
1853
1854 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1855 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1856 return 0;
1857 else
1858 return 1;
1859 }
1860
1861 } /* for (loop through all parameters) */
1862
1863 return 1;
1864 }
1865
1866 /* Unpack the parameters in an INIT packet into an association.
1867 * Returns 0 on failure, else success.
1868 * FIXME: This is an association method.
1869 */
1870 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1871 const union sctp_addr *peer_addr,
1872 sctp_init_chunk_t *peer_init, gfp_t gfp)
1873 {
1874 union sctp_params param;
1875 struct sctp_transport *transport;
1876 struct list_head *pos, *temp;
1877 char *cookie;
1878
1879 /* We must include the address that the INIT packet came from.
1880 * This is the only address that matters for an INIT packet.
1881 * When processing a COOKIE ECHO, we retrieve the from address
1882 * of the INIT from the cookie.
1883 */
1884
1885 /* This implementation defaults to making the first transport
1886 * added as the primary transport. The source address seems to
1887 * be a a better choice than any of the embedded addresses.
1888 */
1889 if (peer_addr) {
1890 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
1891 goto nomem;
1892 }
1893
1894 /* Process the initialization parameters. */
1895
1896 sctp_walk_params(param, peer_init, init_hdr.params) {
1897
1898 if (!sctp_process_param(asoc, param, peer_addr, gfp))
1899 goto clean_up;
1900 }
1901
1902 /* Walk list of transports, removing transports in the UNKNOWN state. */
1903 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1904 transport = list_entry(pos, struct sctp_transport, transports);
1905 if (transport->state == SCTP_UNKNOWN) {
1906 sctp_assoc_rm_peer(asoc, transport);
1907 }
1908 }
1909
1910 /* The fixed INIT headers are always in network byte
1911 * order.
1912 */
1913 asoc->peer.i.init_tag =
1914 ntohl(peer_init->init_hdr.init_tag);
1915 asoc->peer.i.a_rwnd =
1916 ntohl(peer_init->init_hdr.a_rwnd);
1917 asoc->peer.i.num_outbound_streams =
1918 ntohs(peer_init->init_hdr.num_outbound_streams);
1919 asoc->peer.i.num_inbound_streams =
1920 ntohs(peer_init->init_hdr.num_inbound_streams);
1921 asoc->peer.i.initial_tsn =
1922 ntohl(peer_init->init_hdr.initial_tsn);
1923
1924 /* Apply the upper bounds for output streams based on peer's
1925 * number of inbound streams.
1926 */
1927 if (asoc->c.sinit_num_ostreams >
1928 ntohs(peer_init->init_hdr.num_inbound_streams)) {
1929 asoc->c.sinit_num_ostreams =
1930 ntohs(peer_init->init_hdr.num_inbound_streams);
1931 }
1932
1933 if (asoc->c.sinit_max_instreams >
1934 ntohs(peer_init->init_hdr.num_outbound_streams)) {
1935 asoc->c.sinit_max_instreams =
1936 ntohs(peer_init->init_hdr.num_outbound_streams);
1937 }
1938
1939 /* Copy Initiation tag from INIT to VT_peer in cookie. */
1940 asoc->c.peer_vtag = asoc->peer.i.init_tag;
1941
1942 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
1943 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1944
1945 /* Copy cookie in case we need to resend COOKIE-ECHO. */
1946 cookie = asoc->peer.cookie;
1947 if (cookie) {
1948 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
1949 if (!asoc->peer.cookie)
1950 goto clean_up;
1951 }
1952
1953 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1954 * high (for example, implementations MAY use the size of the receiver
1955 * advertised window).
1956 */
1957 list_for_each(pos, &asoc->peer.transport_addr_list) {
1958 transport = list_entry(pos, struct sctp_transport, transports);
1959 transport->ssthresh = asoc->peer.i.a_rwnd;
1960 }
1961
1962 /* Set up the TSN tracking pieces. */
1963 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1964 asoc->peer.i.initial_tsn);
1965
1966 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1967 *
1968 * The stream sequence number in all the streams shall start
1969 * from 0 when the association is established. Also, when the
1970 * stream sequence number reaches the value 65535 the next
1971 * stream sequence number shall be set to 0.
1972 */
1973
1974 /* Allocate storage for the negotiated streams if it is not a temporary
1975 * association.
1976 */
1977 if (!asoc->temp) {
1978 int error;
1979
1980 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1981 asoc->c.sinit_num_ostreams, gfp);
1982 if (!asoc->ssnmap)
1983 goto clean_up;
1984
1985 error = sctp_assoc_set_id(asoc, gfp);
1986 if (error)
1987 goto clean_up;
1988 }
1989
1990 /* ADDIP Section 4.1 ASCONF Chunk Procedures
1991 *
1992 * When an endpoint has an ASCONF signaled change to be sent to the
1993 * remote endpoint it should do the following:
1994 * ...
1995 * A2) A serial number should be assigned to the Chunk. The serial
1996 * number should be a monotonically increasing number. All serial
1997 * numbers are defined to be initialized at the start of the
1998 * association to the same value as the Initial TSN.
1999 */
2000 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2001 return 1;
2002
2003 clean_up:
2004 /* Release the transport structures. */
2005 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2006 transport = list_entry(pos, struct sctp_transport, transports);
2007 list_del_init(pos);
2008 sctp_transport_free(transport);
2009 }
2010
2011 asoc->peer.transport_count = 0;
2012
2013 nomem:
2014 return 0;
2015 }
2016
2017
2018 /* Update asoc with the option described in param.
2019 *
2020 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2021 *
2022 * asoc is the association to update.
2023 * param is the variable length parameter to use for update.
2024 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2025 * If the current packet is an INIT we want to minimize the amount of
2026 * work we do. In particular, we should not build transport
2027 * structures for the addresses.
2028 */
2029 static int sctp_process_param(struct sctp_association *asoc,
2030 union sctp_params param,
2031 const union sctp_addr *peer_addr,
2032 gfp_t gfp)
2033 {
2034 union sctp_addr addr;
2035 int i;
2036 __u16 sat;
2037 int retval = 1;
2038 sctp_scope_t scope;
2039 time_t stale;
2040 struct sctp_af *af;
2041
2042 /* We maintain all INIT parameters in network byte order all the
2043 * time. This allows us to not worry about whether the parameters
2044 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2045 */
2046 switch (param.p->type) {
2047 case SCTP_PARAM_IPV6_ADDRESS:
2048 if (PF_INET6 != asoc->base.sk->sk_family)
2049 break;
2050 /* Fall through. */
2051 case SCTP_PARAM_IPV4_ADDRESS:
2052 af = sctp_get_af_specific(param_type2af(param.p->type));
2053 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2054 scope = sctp_scope(peer_addr);
2055 if (sctp_in_scope(&addr, scope))
2056 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2057 return 0;
2058 break;
2059
2060 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2061 if (!sctp_cookie_preserve_enable)
2062 break;
2063
2064 stale = ntohl(param.life->lifespan_increment);
2065
2066 /* Suggested Cookie Life span increment's unit is msec,
2067 * (1/1000sec).
2068 */
2069 asoc->cookie_life.tv_sec += stale / 1000;
2070 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2071 break;
2072
2073 case SCTP_PARAM_HOST_NAME_ADDRESS:
2074 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2075 break;
2076
2077 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2078 /* Turn off the default values first so we'll know which
2079 * ones are really set by the peer.
2080 */
2081 asoc->peer.ipv4_address = 0;
2082 asoc->peer.ipv6_address = 0;
2083
2084 /* Cycle through address types; avoid divide by 0. */
2085 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2086 if (sat)
2087 sat /= sizeof(__u16);
2088
2089 for (i = 0; i < sat; ++i) {
2090 switch (param.sat->types[i]) {
2091 case SCTP_PARAM_IPV4_ADDRESS:
2092 asoc->peer.ipv4_address = 1;
2093 break;
2094
2095 case SCTP_PARAM_IPV6_ADDRESS:
2096 asoc->peer.ipv6_address = 1;
2097 break;
2098
2099 case SCTP_PARAM_HOST_NAME_ADDRESS:
2100 asoc->peer.hostname_address = 1;
2101 break;
2102
2103 default: /* Just ignore anything else. */
2104 break;
2105 }
2106 }
2107 break;
2108
2109 case SCTP_PARAM_STATE_COOKIE:
2110 asoc->peer.cookie_len =
2111 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2112 asoc->peer.cookie = param.cookie->body;
2113 break;
2114
2115 case SCTP_PARAM_HEARTBEAT_INFO:
2116 /* Would be odd to receive, but it causes no problems. */
2117 break;
2118
2119 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2120 /* Rejected during verify stage. */
2121 break;
2122
2123 case SCTP_PARAM_ECN_CAPABLE:
2124 asoc->peer.ecn_capable = 1;
2125 break;
2126
2127 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2128 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
2129 break;
2130
2131 case SCTP_PARAM_FWD_TSN_SUPPORT:
2132 if (sctp_prsctp_enable) {
2133 asoc->peer.prsctp_capable = 1;
2134 break;
2135 }
2136 /* Fall Through */
2137 default:
2138 /* Any unrecognized parameters should have been caught
2139 * and handled by sctp_verify_param() which should be
2140 * called prior to this routine. Simply log the error
2141 * here.
2142 */
2143 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2144 ntohs(param.p->type), asoc);
2145 break;
2146 }
2147
2148 return retval;
2149 }
2150
2151 /* Select a new verification tag. */
2152 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2153 {
2154 /* I believe that this random number generator complies with RFC1750.
2155 * A tag of 0 is reserved for special cases (e.g. INIT).
2156 */
2157 __u32 x;
2158
2159 do {
2160 get_random_bytes(&x, sizeof(__u32));
2161 } while (x == 0);
2162
2163 return x;
2164 }
2165
2166 /* Select an initial TSN to send during startup. */
2167 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2168 {
2169 __u32 retval;
2170
2171 get_random_bytes(&retval, sizeof(__u32));
2172 return retval;
2173 }
2174
2175 /*
2176 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2177 * 0 1 2 3
2178 * 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
2179 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2180 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2181 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2182 * | Serial Number |
2183 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2184 * | Address Parameter |
2185 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2186 * | ASCONF Parameter #1 |
2187 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2188 * \ \
2189 * / .... /
2190 * \ \
2191 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2192 * | ASCONF Parameter #N |
2193 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2194 *
2195 * Address Parameter and other parameter will not be wrapped in this function
2196 */
2197 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2198 union sctp_addr *addr,
2199 int vparam_len)
2200 {
2201 sctp_addiphdr_t asconf;
2202 struct sctp_chunk *retval;
2203 int length = sizeof(asconf) + vparam_len;
2204 union sctp_addr_param addrparam;
2205 int addrlen;
2206 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2207
2208 addrlen = af->to_addr_param(addr, &addrparam);
2209 if (!addrlen)
2210 return NULL;
2211 length += addrlen;
2212
2213 /* Create the chunk. */
2214 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2215 if (!retval)
2216 return NULL;
2217
2218 asconf.serial = htonl(asoc->addip_serial++);
2219
2220 retval->subh.addip_hdr =
2221 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2222 retval->param_hdr.v =
2223 sctp_addto_chunk(retval, addrlen, &addrparam);
2224
2225 return retval;
2226 }
2227
2228 /* ADDIP
2229 * 3.2.1 Add IP Address
2230 * 0 1 2 3
2231 * 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
2232 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2233 * | Type = 0xC001 | Length = Variable |
2234 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2235 * | ASCONF-Request Correlation ID |
2236 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2237 * | Address Parameter |
2238 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2239 *
2240 * 3.2.2 Delete IP Address
2241 * 0 1 2 3
2242 * 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
2243 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2244 * | Type = 0xC002 | Length = Variable |
2245 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2246 * | ASCONF-Request Correlation ID |
2247 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2248 * | Address Parameter |
2249 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2250 *
2251 */
2252 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2253 union sctp_addr *laddr,
2254 struct sockaddr *addrs,
2255 int addrcnt,
2256 __be16 flags)
2257 {
2258 sctp_addip_param_t param;
2259 struct sctp_chunk *retval;
2260 union sctp_addr_param addr_param;
2261 union sctp_addr *addr;
2262 void *addr_buf;
2263 struct sctp_af *af;
2264 int paramlen = sizeof(param);
2265 int addr_param_len = 0;
2266 int totallen = 0;
2267 int i;
2268
2269 /* Get total length of all the address parameters. */
2270 addr_buf = addrs;
2271 for (i = 0; i < addrcnt; i++) {
2272 addr = (union sctp_addr *)addr_buf;
2273 af = sctp_get_af_specific(addr->v4.sin_family);
2274 addr_param_len = af->to_addr_param(addr, &addr_param);
2275
2276 totallen += paramlen;
2277 totallen += addr_param_len;
2278
2279 addr_buf += af->sockaddr_len;
2280 }
2281
2282 /* Create an asconf chunk with the required length. */
2283 retval = sctp_make_asconf(asoc, laddr, totallen);
2284 if (!retval)
2285 return NULL;
2286
2287 /* Add the address parameters to the asconf chunk. */
2288 addr_buf = addrs;
2289 for (i = 0; i < addrcnt; i++) {
2290 addr = (union sctp_addr *)addr_buf;
2291 af = sctp_get_af_specific(addr->v4.sin_family);
2292 addr_param_len = af->to_addr_param(addr, &addr_param);
2293 param.param_hdr.type = flags;
2294 param.param_hdr.length = htons(paramlen + addr_param_len);
2295 param.crr_id = i;
2296
2297 sctp_addto_chunk(retval, paramlen, &param);
2298 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2299
2300 addr_buf += af->sockaddr_len;
2301 }
2302 return retval;
2303 }
2304
2305 /* ADDIP
2306 * 3.2.4 Set Primary IP Address
2307 * 0 1 2 3
2308 * 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
2309 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2310 * | Type =0xC004 | Length = Variable |
2311 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2312 * | ASCONF-Request Correlation ID |
2313 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2314 * | Address Parameter |
2315 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2316 *
2317 * Create an ASCONF chunk with Set Primary IP address parameter.
2318 */
2319 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2320 union sctp_addr *addr)
2321 {
2322 sctp_addip_param_t param;
2323 struct sctp_chunk *retval;
2324 int len = sizeof(param);
2325 union sctp_addr_param addrparam;
2326 int addrlen;
2327 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2328
2329 addrlen = af->to_addr_param(addr, &addrparam);
2330 if (!addrlen)
2331 return NULL;
2332 len += addrlen;
2333
2334 /* Create the chunk and make asconf header. */
2335 retval = sctp_make_asconf(asoc, addr, len);
2336 if (!retval)
2337 return NULL;
2338
2339 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2340 param.param_hdr.length = htons(len);
2341 param.crr_id = 0;
2342
2343 sctp_addto_chunk(retval, sizeof(param), &param);
2344 sctp_addto_chunk(retval, addrlen, &addrparam);
2345
2346 return retval;
2347 }
2348
2349 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2350 * 0 1 2 3
2351 * 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
2352 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2353 * | Type = 0x80 | Chunk Flags | Chunk Length |
2354 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2355 * | Serial Number |
2356 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2357 * | ASCONF Parameter Response#1 |
2358 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2359 * \ \
2360 * / .... /
2361 * \ \
2362 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2363 * | ASCONF Parameter Response#N |
2364 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2365 *
2366 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2367 */
2368 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2369 __u32 serial, int vparam_len)
2370 {
2371 sctp_addiphdr_t asconf;
2372 struct sctp_chunk *retval;
2373 int length = sizeof(asconf) + vparam_len;
2374
2375 /* Create the chunk. */
2376 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2377 if (!retval)
2378 return NULL;
2379
2380 asconf.serial = htonl(serial);
2381
2382 retval->subh.addip_hdr =
2383 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2384
2385 return retval;
2386 }
2387
2388 /* Add response parameters to an ASCONF_ACK chunk. */
2389 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2390 __be16 err_code, sctp_addip_param_t *asconf_param)
2391 {
2392 sctp_addip_param_t ack_param;
2393 sctp_errhdr_t err_param;
2394 int asconf_param_len = 0;
2395 int err_param_len = 0;
2396 __be16 response_type;
2397
2398 if (SCTP_ERROR_NO_ERROR == err_code) {
2399 response_type = SCTP_PARAM_SUCCESS_REPORT;
2400 } else {
2401 response_type = SCTP_PARAM_ERR_CAUSE;
2402 err_param_len = sizeof(err_param);
2403 if (asconf_param)
2404 asconf_param_len =
2405 ntohs(asconf_param->param_hdr.length);
2406 }
2407
2408 /* Add Success Indication or Error Cause Indication parameter. */
2409 ack_param.param_hdr.type = response_type;
2410 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2411 err_param_len +
2412 asconf_param_len);
2413 ack_param.crr_id = crr_id;
2414 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2415
2416 if (SCTP_ERROR_NO_ERROR == err_code)
2417 return;
2418
2419 /* Add Error Cause parameter. */
2420 err_param.cause = err_code;
2421 err_param.length = htons(err_param_len + asconf_param_len);
2422 sctp_addto_chunk(chunk, err_param_len, &err_param);
2423
2424 /* Add the failed TLV copied from ASCONF chunk. */
2425 if (asconf_param)
2426 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2427 }
2428
2429 /* Process a asconf parameter. */
2430 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2431 struct sctp_chunk *asconf,
2432 sctp_addip_param_t *asconf_param)
2433 {
2434 struct sctp_transport *peer;
2435 struct sctp_af *af;
2436 union sctp_addr addr;
2437 struct list_head *pos;
2438 union sctp_addr_param *addr_param;
2439
2440 addr_param = (union sctp_addr_param *)
2441 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2442
2443 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2444 if (unlikely(!af))
2445 return SCTP_ERROR_INV_PARAM;
2446
2447 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2448 switch (asconf_param->param_hdr.type) {
2449 case SCTP_PARAM_ADD_IP:
2450 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2451 * request and does not have the local resources to add this
2452 * new address to the association, it MUST return an Error
2453 * Cause TLV set to the new error code 'Operation Refused
2454 * Due to Resource Shortage'.
2455 */
2456
2457 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2458 if (!peer)
2459 return SCTP_ERROR_RSRC_LOW;
2460
2461 /* Start the heartbeat timer. */
2462 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2463 sctp_transport_hold(peer);
2464 break;
2465 case SCTP_PARAM_DEL_IP:
2466 /* ADDIP 4.3 D7) If a request is received to delete the
2467 * last remaining IP address of a peer endpoint, the receiver
2468 * MUST send an Error Cause TLV with the error cause set to the
2469 * new error code 'Request to Delete Last Remaining IP Address'.
2470 */
2471 pos = asoc->peer.transport_addr_list.next;
2472 if (pos->next == &asoc->peer.transport_addr_list)
2473 return SCTP_ERROR_DEL_LAST_IP;
2474
2475 /* ADDIP 4.3 D8) If a request is received to delete an IP
2476 * address which is also the source address of the IP packet
2477 * which contained the ASCONF chunk, the receiver MUST reject
2478 * this request. To reject the request the receiver MUST send
2479 * an Error Cause TLV set to the new error code 'Request to
2480 * Delete Source IP Address'
2481 */
2482 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2483 return SCTP_ERROR_DEL_SRC_IP;
2484
2485 sctp_assoc_del_peer(asoc, &addr);
2486 break;
2487 case SCTP_PARAM_SET_PRIMARY:
2488 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2489 if (!peer)
2490 return SCTP_ERROR_INV_PARAM;
2491
2492 sctp_assoc_set_primary(asoc, peer);
2493 break;
2494 default:
2495 return SCTP_ERROR_INV_PARAM;
2496 break;
2497 }
2498
2499 return SCTP_ERROR_NO_ERROR;
2500 }
2501
2502 /* Verify the ASCONF packet before we process it. */
2503 int sctp_verify_asconf(const struct sctp_association *asoc,
2504 struct sctp_paramhdr *param_hdr, void *chunk_end,
2505 struct sctp_paramhdr **errp) {
2506 sctp_addip_param_t *asconf_param;
2507 union sctp_params param;
2508 int length, plen;
2509
2510 param.v = (sctp_paramhdr_t *) param_hdr;
2511 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2512 length = ntohs(param.p->length);
2513 *errp = param.p;
2514
2515 if (param.v > chunk_end - length ||
2516 length < sizeof(sctp_paramhdr_t))
2517 return 0;
2518
2519 switch (param.p->type) {
2520 case SCTP_PARAM_ADD_IP:
2521 case SCTP_PARAM_DEL_IP:
2522 case SCTP_PARAM_SET_PRIMARY:
2523 asconf_param = (sctp_addip_param_t *)param.v;
2524 plen = ntohs(asconf_param->param_hdr.length);
2525 if (plen < sizeof(sctp_addip_param_t) +
2526 sizeof(sctp_paramhdr_t))
2527 return 0;
2528 break;
2529 case SCTP_PARAM_SUCCESS_REPORT:
2530 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2531 if (length != sizeof(sctp_addip_param_t))
2532 return 0;
2533
2534 break;
2535 default:
2536 break;
2537 }
2538
2539 param.v += WORD_ROUND(length);
2540 }
2541
2542 if (param.v != chunk_end)
2543 return 0;
2544
2545 return 1;
2546 }
2547
2548 /* Process an incoming ASCONF chunk with the next expected serial no. and
2549 * return an ASCONF_ACK chunk to be sent in response.
2550 */
2551 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2552 struct sctp_chunk *asconf)
2553 {
2554 sctp_addiphdr_t *hdr;
2555 union sctp_addr_param *addr_param;
2556 sctp_addip_param_t *asconf_param;
2557 struct sctp_chunk *asconf_ack;
2558
2559 __be16 err_code;
2560 int length = 0;
2561 int chunk_len = asconf->skb->len;
2562 __u32 serial;
2563 int all_param_pass = 1;
2564
2565 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2566 serial = ntohl(hdr->serial);
2567
2568 /* Skip the addiphdr and store a pointer to address parameter. */
2569 length = sizeof(sctp_addiphdr_t);
2570 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2571 chunk_len -= length;
2572
2573 /* Skip the address parameter and store a pointer to the first
2574 * asconf paramter.
2575 */
2576 length = ntohs(addr_param->v4.param_hdr.length);
2577 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2578 chunk_len -= length;
2579
2580 /* create an ASCONF_ACK chunk.
2581 * Based on the definitions of parameters, we know that the size of
2582 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2583 * paramters.
2584 */
2585 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2586 if (!asconf_ack)
2587 goto done;
2588
2589 /* Process the TLVs contained within the ASCONF chunk. */
2590 while (chunk_len > 0) {
2591 err_code = sctp_process_asconf_param(asoc, asconf,
2592 asconf_param);
2593 /* ADDIP 4.1 A7)
2594 * If an error response is received for a TLV parameter,
2595 * all TLVs with no response before the failed TLV are
2596 * considered successful if not reported. All TLVs after
2597 * the failed response are considered unsuccessful unless
2598 * a specific success indication is present for the parameter.
2599 */
2600 if (SCTP_ERROR_NO_ERROR != err_code)
2601 all_param_pass = 0;
2602
2603 if (!all_param_pass)
2604 sctp_add_asconf_response(asconf_ack,
2605 asconf_param->crr_id, err_code,
2606 asconf_param);
2607
2608 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2609 * an IP address sends an 'Out of Resource' in its response, it
2610 * MUST also fail any subsequent add or delete requests bundled
2611 * in the ASCONF.
2612 */
2613 if (SCTP_ERROR_RSRC_LOW == err_code)
2614 goto done;
2615
2616 /* Move to the next ASCONF param. */
2617 length = ntohs(asconf_param->param_hdr.length);
2618 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2619 length);
2620 chunk_len -= length;
2621 }
2622
2623 done:
2624 asoc->peer.addip_serial++;
2625
2626 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2627 * after freeing the reference to old asconf ack if any.
2628 */
2629 if (asconf_ack) {
2630 if (asoc->addip_last_asconf_ack)
2631 sctp_chunk_free(asoc->addip_last_asconf_ack);
2632
2633 sctp_chunk_hold(asconf_ack);
2634 asoc->addip_last_asconf_ack = asconf_ack;
2635 }
2636
2637 return asconf_ack;
2638 }
2639
2640 /* Process a asconf parameter that is successfully acked. */
2641 static int sctp_asconf_param_success(struct sctp_association *asoc,
2642 sctp_addip_param_t *asconf_param)
2643 {
2644 struct sctp_af *af;
2645 union sctp_addr addr;
2646 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2647 union sctp_addr_param *addr_param;
2648 struct list_head *pos;
2649 struct sctp_transport *transport;
2650 struct sctp_sockaddr_entry *saddr;
2651 int retval = 0;
2652
2653 addr_param = (union sctp_addr_param *)
2654 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2655
2656 /* We have checked the packet before, so we do not check again. */
2657 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2658 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
2659
2660 switch (asconf_param->param_hdr.type) {
2661 case SCTP_PARAM_ADD_IP:
2662 /* This is always done in BH context with a socket lock
2663 * held, so the list can not change.
2664 */
2665 list_for_each_entry(saddr, &bp->address_list, list) {
2666 if (sctp_cmp_addr_exact(&saddr->a, &addr))
2667 saddr->use_as_src = 1;
2668 }
2669 break;
2670 case SCTP_PARAM_DEL_IP:
2671 retval = sctp_del_bind_addr(bp, &addr, call_rcu_bh);
2672 list_for_each(pos, &asoc->peer.transport_addr_list) {
2673 transport = list_entry(pos, struct sctp_transport,
2674 transports);
2675 dst_release(transport->dst);
2676 sctp_transport_route(transport, NULL,
2677 sctp_sk(asoc->base.sk));
2678 }
2679 break;
2680 default:
2681 break;
2682 }
2683
2684 return retval;
2685 }
2686
2687 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2688 * for the given asconf parameter. If there is no response for this parameter,
2689 * return the error code based on the third argument 'no_err'.
2690 * ADDIP 4.1
2691 * A7) If an error response is received for a TLV parameter, all TLVs with no
2692 * response before the failed TLV are considered successful if not reported.
2693 * All TLVs after the failed response are considered unsuccessful unless a
2694 * specific success indication is present for the parameter.
2695 */
2696 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2697 sctp_addip_param_t *asconf_param,
2698 int no_err)
2699 {
2700 sctp_addip_param_t *asconf_ack_param;
2701 sctp_errhdr_t *err_param;
2702 int length;
2703 int asconf_ack_len = asconf_ack->skb->len;
2704 __be16 err_code;
2705
2706 if (no_err)
2707 err_code = SCTP_ERROR_NO_ERROR;
2708 else
2709 err_code = SCTP_ERROR_REQ_REFUSED;
2710
2711 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2712 * the first asconf_ack parameter.
2713 */
2714 length = sizeof(sctp_addiphdr_t);
2715 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2716 length);
2717 asconf_ack_len -= length;
2718
2719 while (asconf_ack_len > 0) {
2720 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2721 switch(asconf_ack_param->param_hdr.type) {
2722 case SCTP_PARAM_SUCCESS_REPORT:
2723 return SCTP_ERROR_NO_ERROR;
2724 case SCTP_PARAM_ERR_CAUSE:
2725 length = sizeof(sctp_addip_param_t);
2726 err_param = (sctp_errhdr_t *)
2727 ((void *)asconf_ack_param + length);
2728 asconf_ack_len -= length;
2729 if (asconf_ack_len > 0)
2730 return err_param->cause;
2731 else
2732 return SCTP_ERROR_INV_PARAM;
2733 break;
2734 default:
2735 return SCTP_ERROR_INV_PARAM;
2736 }
2737 }
2738
2739 length = ntohs(asconf_ack_param->param_hdr.length);
2740 asconf_ack_param = (sctp_addip_param_t *)
2741 ((void *)asconf_ack_param + length);
2742 asconf_ack_len -= length;
2743 }
2744
2745 return err_code;
2746 }
2747
2748 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2749 int sctp_process_asconf_ack(struct sctp_association *asoc,
2750 struct sctp_chunk *asconf_ack)
2751 {
2752 struct sctp_chunk *asconf = asoc->addip_last_asconf;
2753 union sctp_addr_param *addr_param;
2754 sctp_addip_param_t *asconf_param;
2755 int length = 0;
2756 int asconf_len = asconf->skb->len;
2757 int all_param_pass = 0;
2758 int no_err = 1;
2759 int retval = 0;
2760 __be16 err_code = SCTP_ERROR_NO_ERROR;
2761
2762 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2763 * a pointer to address parameter.
2764 */
2765 length = sizeof(sctp_addip_chunk_t);
2766 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2767 asconf_len -= length;
2768
2769 /* Skip the address parameter in the last asconf sent and store a
2770 * pointer to the first asconf paramter.
2771 */
2772 length = ntohs(addr_param->v4.param_hdr.length);
2773 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2774 asconf_len -= length;
2775
2776 /* ADDIP 4.1
2777 * A8) If there is no response(s) to specific TLV parameter(s), and no
2778 * failures are indicated, then all request(s) are considered
2779 * successful.
2780 */
2781 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2782 all_param_pass = 1;
2783
2784 /* Process the TLVs contained in the last sent ASCONF chunk. */
2785 while (asconf_len > 0) {
2786 if (all_param_pass)
2787 err_code = SCTP_ERROR_NO_ERROR;
2788 else {
2789 err_code = sctp_get_asconf_response(asconf_ack,
2790 asconf_param,
2791 no_err);
2792 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2793 no_err = 0;
2794 }
2795
2796 switch (err_code) {
2797 case SCTP_ERROR_NO_ERROR:
2798 retval = sctp_asconf_param_success(asoc, asconf_param);
2799 break;
2800
2801 case SCTP_ERROR_RSRC_LOW:
2802 retval = 1;
2803 break;
2804
2805 case SCTP_ERROR_INV_PARAM:
2806 /* Disable sending this type of asconf parameter in
2807 * future.
2808 */
2809 asoc->peer.addip_disabled_mask |=
2810 asconf_param->param_hdr.type;
2811 break;
2812
2813 case SCTP_ERROR_REQ_REFUSED:
2814 case SCTP_ERROR_DEL_LAST_IP:
2815 case SCTP_ERROR_DEL_SRC_IP:
2816 default:
2817 break;
2818 }
2819
2820 /* Skip the processed asconf parameter and move to the next
2821 * one.
2822 */
2823 length = ntohs(asconf_param->param_hdr.length);
2824 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2825 length);
2826 asconf_len -= length;
2827 }
2828
2829 /* Free the cached last sent asconf chunk. */
2830 sctp_chunk_free(asconf);
2831 asoc->addip_last_asconf = NULL;
2832
2833 /* Send the next asconf chunk from the addip chunk queue. */
2834 if (!list_empty(&asoc->addip_chunk_list)) {
2835 struct list_head *entry = asoc->addip_chunk_list.next;
2836 asconf = list_entry(entry, struct sctp_chunk, list);
2837
2838 list_del_init(entry);
2839
2840 /* Hold the chunk until an ASCONF_ACK is received. */
2841 sctp_chunk_hold(asconf);
2842 if (sctp_primitive_ASCONF(asoc, asconf))
2843 sctp_chunk_free(asconf);
2844 else
2845 asoc->addip_last_asconf = asconf;
2846 }
2847
2848 return retval;
2849 }
2850
2851 /* Make a FWD TSN chunk. */
2852 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2853 __u32 new_cum_tsn, size_t nstreams,
2854 struct sctp_fwdtsn_skip *skiplist)
2855 {
2856 struct sctp_chunk *retval = NULL;
2857 struct sctp_fwdtsn_chunk *ftsn_chunk;
2858 struct sctp_fwdtsn_hdr ftsn_hdr;
2859 struct sctp_fwdtsn_skip skip;
2860 size_t hint;
2861 int i;
2862
2863 hint = (nstreams + 1) * sizeof(__u32);
2864
2865 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2866
2867 if (!retval)
2868 return NULL;
2869
2870 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2871
2872 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2873 retval->subh.fwdtsn_hdr =
2874 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2875
2876 for (i = 0; i < nstreams; i++) {
2877 skip.stream = skiplist[i].stream;
2878 skip.ssn = skiplist[i].ssn;
2879 sctp_addto_chunk(retval, sizeof(skip), &skip);
2880 }
2881
2882 return retval;
2883 }
This page took 0.088565 seconds and 6 git commands to generate.