ia64/linux-2.6.18-xen.hg

view net/sctp/sm_make_chunk.c @ 871:9cbcc9008446

xen/x86: don't initialize cpu_data[]'s apicid field on generic code

Afaict, this is not only redundant with the intialization done in
drivers/xen/core/smpboot.c, but actually results - at least for
secondary CPUs - in the Xen-specific value written to be later
overwritten with whatever the generic code determines (with no
guarantee that the two values are identical).

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