Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / net / sctp / sm_make_chunk.c
blob0681874c23a98881a0888daaed32e7f301c49f6b
1 /* SCTP kernel reference Implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001-2002 Intel Corp.
5 * Copyright (c) 2001-2002 International Business Machines Corp.
7 * This file is part of the SCTP kernel reference Implementation
9 * This file includes part of the implementation of the add-IP extension,
10 * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
11 * for the SCTP kernel reference Implementation.
13 * These functions work with the state functions in sctp_sm_statefuns.c
14 * to implement the state operations. These functions implement the
15 * steps which require modifying existing data structures.
17 * The SCTP reference implementation is free software;
18 * you can redistribute it and/or modify it under the terms of
19 * the GNU General Public License as published by
20 * the Free Software Foundation; either version 2, or (at your option)
21 * any later version.
23 * The SCTP reference implementation is distributed in the hope that it
24 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * ************************
26 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 * See the GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with GNU CC; see the file COPYING. If not, write to
31 * the Free Software Foundation, 59 Temple Place - Suite 330,
32 * Boston, MA 02111-1307, USA.
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
36 * lksctp developers <lksctp-developers@lists.sourceforge.net>
38 * Or submit a bug report through the following website:
39 * http://www.sf.net/projects/lksctp
41 * Written or modified by:
42 * La Monte H.P. Yarroll <piggy@acm.org>
43 * Karl Knutson <karl@athena.chicago.il.us>
44 * C. Robin <chris@hundredacre.ac.uk>
45 * Jon Grimm <jgrimm@us.ibm.com>
46 * Xingang Guo <xingang.guo@intel.com>
47 * Dajiang Zhang <dajiang.zhang@nokia.com>
48 * Sridhar Samudrala <sri@us.ibm.com>
49 * Daisy Chang <daisyc@us.ibm.com>
50 * Ardelle Fan <ardelle.fan@intel.com>
52 * Any bugs reported given to us we will try to fix... any fixes shared will
53 * be incorporated into the next SCTP release.
56 #include <linux/types.h>
57 #include <linux/kernel.h>
58 #include <linux/ip.h>
59 #include <linux/ipv6.h>
60 #include <linux/net.h>
61 #include <linux/inet.h>
62 #include <net/sock.h>
64 #include <linux/skbuff.h>
65 #include <linux/random.h> /* for get_random_bytes */
66 #include <net/sctp/sctp.h>
67 #include <net/sctp/sm.h>
69 /* RFC 2960 3.3.2 Initiation (INIT) (1)
71 * Note 4: This parameter, when present, specifies all the
72 * address types the sending endpoint can support. The absence
73 * of this parameter indicates that the sending endpoint can
74 * support any address type.
76 static const sctp_supported_addrs_param_t sat_param = {
78 SCTP_PARAM_SUPPORTED_ADDRESS_TYPES,
79 __constant_htons(SCTP_SAT_LEN),
83 /* gcc 3.2 doesn't allow initialization of zero-length arrays. So the above
84 * structure is split and the address types array is initialized using a
85 * fixed length array.
87 static const __u16 sat_addr_types[2] = {
88 SCTP_PARAM_IPV4_ADDRESS,
89 SCTP_V6(SCTP_PARAM_IPV6_ADDRESS,)
90 };
92 /* RFC 2960 3.3.2 Initiation (INIT) (1)
94 * Note 2: The ECN capable field is reserved for future use of
95 * Explicit Congestion Notification.
97 static const sctp_ecn_capable_param_t ecap_param = {
99 SCTP_PARAM_ECN_CAPABLE,
100 __constant_htons(sizeof(sctp_ecn_capable_param_t)),
104 /* A helper to initilize to initilize an op error inside a
105 * provided chunk, as most cause codes will be embedded inside an
106 * abort chunk.
108 void sctp_init_cause(sctp_chunk_t *chunk, __u16 cause_code,
109 const void *payload, size_t paylen)
111 sctp_errhdr_t err;
112 int padlen;
113 __u16 len;
115 /* Cause code constants are now defined in network order. */
116 err.cause = cause_code;
117 len = sizeof(sctp_errhdr_t) + paylen;
118 padlen = len % 4;
119 len += padlen;
120 err.length = htons(len);
121 sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
122 chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
125 /* 3.3.2 Initiation (INIT) (1)
127 * This chunk is used to initiate a SCTP association between two
128 * endpoints. The format of the INIT chunk is shown below:
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 * The INIT chunk contains the following parameters. Unless otherwise
150 * noted, each parameter MUST only be included once in the INIT chunk.
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
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
169 sctp_chunk_t *sctp_make_init(const sctp_association_t *asoc,
170 const sctp_bind_addr_t *bp,
171 int priority, int vparam_len)
173 sctp_inithdr_t init;
174 union sctp_params addrs;
175 size_t chunksize;
176 sctp_chunk_t *retval = NULL;
177 int addrs_len = 0;
179 /* RFC 2960 3.3.2 Initiation (INIT) (1)
181 * Note 1: The INIT chunks can contain multiple addresses that
182 * can be IPv4 and/or IPv6 in any combination.
184 retval = NULL;
185 addrs.v = NULL;
187 /* Convert the provided bind address list to raw format */
188 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, priority);
189 if (!addrs.v)
190 goto nodata;
192 init.init_tag = htonl(asoc->c.my_vtag);
193 init.a_rwnd = htonl(asoc->rwnd);
194 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
195 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
196 init.initial_tsn = htonl(asoc->c.initial_tsn);
198 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN;
199 chunksize += sizeof(ecap_param);
200 chunksize += vparam_len;
202 /* RFC 2960 3.3.2 Initiation (INIT) (1)
204 * Note 3: An INIT chunk MUST NOT contain more than one Host
205 * Name address parameter. Moreover, the sender of the INIT
206 * MUST NOT combine any other address types with the Host Name
207 * address in the INIT. The receiver of INIT MUST ignore any
208 * other address types if the Host Name address parameter is
209 * present in the received INIT chunk.
211 * PLEASE DO NOT FIXME [This version does not support Host Name.]
214 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
215 if (!retval)
216 goto nodata;
218 retval->subh.init_hdr =
219 sctp_addto_chunk(retval, sizeof(init), &init);
220 retval->param_hdr.v =
221 sctp_addto_chunk(retval, addrs_len, addrs.v);
223 sctp_addto_chunk(retval, sizeof(sctp_paramhdr_t), &sat_param);
224 sctp_addto_chunk(retval, sizeof(sat_addr_types), sat_addr_types);
226 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
228 nodata:
229 if (addrs.v)
230 kfree(addrs.v);
231 return retval;
234 sctp_chunk_t *sctp_make_init_ack(const sctp_association_t *asoc,
235 const sctp_chunk_t *chunk,
236 int priority, int unkparam_len)
238 sctp_inithdr_t initack;
239 sctp_chunk_t *retval;
240 union sctp_params addrs;
241 int addrs_len;
242 sctp_cookie_param_t *cookie;
243 int cookie_len;
244 size_t chunksize;
245 int error;
246 sctp_scope_t scope;
247 sctp_bind_addr_t *bp = NULL;
248 int flags;
250 retval = NULL;
252 /* Build up the bind address list for the association based on
253 * info from the local endpoint and the remote peer.
255 bp = sctp_bind_addr_new(priority);
256 if (!bp)
257 goto nomem_bindaddr;
259 /* Look for supported address types parameter and then build
260 * our address list based on that.
262 scope = sctp_scope(&asoc->peer.active_path->ipaddr);
263 flags = (PF_INET6 == asoc->base.sk->family) ? SCTP_ADDR6_ALLOWED : 0;
264 if (asoc->peer.ipv4_address)
265 flags |= SCTP_ADDR4_PEERSUPP;
266 if (asoc->peer.ipv6_address)
267 flags |= SCTP_ADDR6_PEERSUPP;
268 error = sctp_bind_addr_copy(bp, &asoc->ep->base.bind_addr,
269 scope, priority, flags);
270 if (error)
271 goto nomem_copyaddr;
273 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, priority);
274 if (!addrs.v)
275 goto nomem_rawaddr;
277 initack.init_tag = htonl(asoc->c.my_vtag);
278 initack.a_rwnd = htonl(asoc->rwnd);
279 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
280 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
281 initack.initial_tsn = htonl(asoc->c.initial_tsn);
283 /* FIXME: We really ought to build the cookie right
284 * into the packet instead of allocating more fresh memory.
286 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
287 addrs.v, addrs_len);
288 if (!cookie)
289 goto nomem_cookie;
291 /* Calculate the total size of allocation, include the reserved
292 * space for reporting unknown parameters if it is specified.
294 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
296 /* Tell peer that we'll do ECN only if peer advertised such cap. */
297 if (asoc->peer.ecn_capable)
298 chunksize += sizeof(ecap_param);
300 /* Now allocate and fill out the chunk. */
301 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
302 if (!retval)
303 goto nomem_chunk;
305 /* Per the advice in RFC 2960 6.4, send this reply to
306 * the source of the INIT packet.
308 retval->transport = chunk->transport;
309 retval->subh.init_hdr =
310 sctp_addto_chunk(retval, sizeof(initack), &initack);
311 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
312 sctp_addto_chunk(retval, cookie_len, cookie);
313 if (asoc->peer.ecn_capable)
314 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
316 /* We need to remove the const qualifier at this point. */
317 retval->asoc = (sctp_association_t *) asoc;
319 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
321 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
322 * HEARTBEAT ACK, * etc.) to the same destination transport
323 * address from which it received the DATA or control chunk
324 * to which it is replying.
326 * [INIT ACK back to where the INIT came from.]
328 if (chunk)
329 retval->transport = chunk->transport;
331 nomem_chunk:
332 kfree(cookie);
333 nomem_cookie:
334 kfree(addrs.v);
335 nomem_rawaddr:
336 nomem_copyaddr:
337 sctp_bind_addr_free(bp);
338 nomem_bindaddr:
339 return retval;
342 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
344 * This chunk is used only during the initialization of an association.
345 * It is sent by the initiator of an association to its peer to complete
346 * the initialization process. This chunk MUST precede any DATA chunk
347 * sent within the association, but MAY be bundled with one or more DATA
348 * chunks in the same packet.
350 * 0 1 2 3
351 * 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
352 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
353 * | Type = 10 |Chunk Flags | Length |
354 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
355 * / Cookie /
356 * \ \
357 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
359 * Chunk Flags: 8 bit
361 * Set to zero on transmit and ignored on receipt.
363 * Length: 16 bits (unsigned integer)
365 * Set to the size of the chunk in bytes, including the 4 bytes of
366 * the chunk header and the size of the Cookie.
368 * Cookie: variable size
370 * This field must contain the exact cookie received in the
371 * State Cookie parameter from the previous INIT ACK.
373 * An implementation SHOULD make the cookie as small as possible
374 * to insure interoperability.
376 sctp_chunk_t *sctp_make_cookie_echo(const sctp_association_t *asoc,
377 const sctp_chunk_t *chunk)
379 sctp_chunk_t *retval;
380 void *cookie;
381 int cookie_len;
383 cookie = asoc->peer.cookie;
384 cookie_len = asoc->peer.cookie_len;
386 /* Build a cookie echo chunk. */
387 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
388 if (!retval)
389 goto nodata;
390 retval->subh.cookie_hdr =
391 sctp_addto_chunk(retval, cookie_len, cookie);
393 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
395 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
396 * HEARTBEAT ACK, * etc.) to the same destination transport
397 * address from which it * received the DATA or control chunk
398 * to which it is replying.
400 * [COOKIE ECHO back to where the INIT ACK came from.]
402 if (chunk)
403 retval->transport = chunk->transport;
405 nodata:
406 return retval;
409 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
411 * This chunk is used only during the initialization of an
412 * association. It is used to acknowledge the receipt of a COOKIE
413 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
414 * within the association, but MAY be bundled with one or more DATA
415 * chunks or SACK chunk in the same SCTP packet.
417 * 0 1 2 3
418 * 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
419 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
420 * | Type = 11 |Chunk Flags | Length = 4 |
421 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
423 * Chunk Flags: 8 bits
425 * Set to zero on transmit and ignored on receipt.
427 sctp_chunk_t *sctp_make_cookie_ack(const sctp_association_t *asoc,
428 const sctp_chunk_t *chunk)
430 sctp_chunk_t *retval;
432 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
434 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
436 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
437 * HEARTBEAT ACK, * etc.) to the same destination transport
438 * address from which it * received the DATA or control chunk
439 * to which it is replying.
441 * [COOKIE ACK back to where the COOKIE ECHO came from.]
443 if (retval && chunk)
444 retval->transport = chunk->transport;
446 return retval;
450 * Appendix A: Explicit Congestion Notification:
451 * CWR:
453 * RFC 2481 details a specific bit for a sender to send in the header of
454 * its next outbound TCP segment to indicate to its peer that it has
455 * reduced its congestion window. This is termed the CWR bit. For
456 * SCTP the same indication is made by including the CWR chunk.
457 * This chunk contains one data element, i.e. the TSN number that
458 * was sent in the ECNE chunk. This element represents the lowest
459 * TSN number in the datagram that was originally marked with the
460 * CE bit.
462 * 0 1 2 3
463 * 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
464 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
465 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
466 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
467 * | Lowest TSN Number |
468 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
470 * Note: The CWR is considered a Control chunk.
472 sctp_chunk_t *sctp_make_cwr(const sctp_association_t *asoc,
473 const __u32 lowest_tsn,
474 const sctp_chunk_t *chunk)
476 sctp_chunk_t *retval;
477 sctp_cwrhdr_t cwr;
479 cwr.lowest_tsn = htonl(lowest_tsn);
480 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
481 sizeof(sctp_cwrhdr_t));
483 if (!retval)
484 goto nodata;
486 retval->subh.ecn_cwr_hdr =
487 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
489 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
491 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
492 * HEARTBEAT ACK, * etc.) to the same destination transport
493 * address from which it * received the DATA or control chunk
494 * to which it is replying.
496 * [Report a reduced congestion window back to where the ECNE
497 * came from.]
499 if (chunk)
500 retval->transport = chunk->transport;
502 nodata:
503 return retval;
506 /* Make an ECNE chunk. This is a congestion experienced report. */
507 sctp_chunk_t *sctp_make_ecne(const sctp_association_t *asoc,
508 const __u32 lowest_tsn)
510 sctp_chunk_t *retval;
511 sctp_ecnehdr_t ecne;
513 ecne.lowest_tsn = htonl(lowest_tsn);
514 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
515 sizeof(sctp_ecnehdr_t));
516 if (!retval)
517 goto nodata;
518 retval->subh.ecne_hdr =
519 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
521 nodata:
522 return retval;
525 /* Make a DATA chunk for the given association from the provided
526 * parameters. However, do not populate the data payload.
528 sctp_chunk_t *sctp_make_datafrag_empty(sctp_association_t *asoc,
529 const struct sctp_sndrcvinfo *sinfo,
530 int data_len, __u8 flags, __u16 ssn)
532 sctp_chunk_t *retval;
533 sctp_datahdr_t dp;
534 int chunk_len;
536 /* We assign the TSN as LATE as possible, not here when
537 * creating the chunk.
539 dp.tsn= 1000000; /* This marker is a debugging aid. */
540 dp.stream = htons(sinfo->sinfo_stream);
541 dp.ppid = htonl(sinfo->sinfo_ppid);
542 dp.ssn = htons(ssn);
544 /* Set the flags for an unordered send. */
545 if (sinfo->sinfo_flags & MSG_UNORDERED)
546 flags |= SCTP_DATA_UNORDERED;
548 chunk_len = sizeof(dp) + data_len;
549 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
550 if (!retval)
551 goto nodata;
553 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
554 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
556 nodata:
557 return retval;
560 /* Make a DATA chunk for the given association. Populate the data
561 * payload.
563 sctp_chunk_t *sctp_make_datafrag(sctp_association_t *asoc,
564 const struct sctp_sndrcvinfo *sinfo,
565 int data_len, const __u8 *data,
566 __u8 flags, __u16 ssn)
568 sctp_chunk_t *retval;
570 retval = sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, ssn);
571 if (retval)
572 sctp_addto_chunk(retval, data_len, data);
574 return retval;
577 /* Make a DATA chunk for the given association to ride on stream id
578 * 'stream', with a payload id of 'payload', and a body of 'data'.
580 sctp_chunk_t *sctp_make_data(sctp_association_t *asoc,
581 const struct sctp_sndrcvinfo *sinfo,
582 int data_len, const __u8 *data)
584 sctp_chunk_t *retval = NULL;
586 retval = sctp_make_data_empty(asoc, sinfo, data_len);
587 if (retval)
588 sctp_addto_chunk(retval, data_len, data);
589 return retval;
592 /* Make a DATA chunk for the given association to ride on stream id
593 * 'stream', with a payload id of 'payload', and a body big enough to
594 * hold 'data_len' octets of data. We use this version when we need
595 * to build the message AFTER allocating memory.
597 sctp_chunk_t *sctp_make_data_empty(sctp_association_t *asoc,
598 const struct sctp_sndrcvinfo *sinfo,
599 int data_len)
601 __u16 ssn;
602 __u8 flags = SCTP_DATA_NOT_FRAG;
604 /* Sockets API Extensions for SCTP 5.2.2
605 * MSG_UNORDERED - This flag requests the un-ordered delivery of the
606 * message. If this flag is clear, the datagram is considered an
607 * ordered send and a new ssn is generated. The flags field is set
608 * in the inner routine - sctp_make_datafrag_empty().
610 if (sinfo->sinfo_flags & MSG_UNORDERED) {
611 ssn = 0;
612 } else {
613 ssn = __sctp_association_get_next_ssn(asoc,
614 sinfo->sinfo_stream);
617 return sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, ssn);
620 /* Create a selective ackowledgement (SACK) for the given
621 * association. This reports on which TSN's we've seen to date,
622 * including duplicates and gaps.
624 sctp_chunk_t *sctp_make_sack(const sctp_association_t *asoc)
626 sctp_chunk_t *retval;
627 sctp_sackhdr_t sack;
628 sctp_gap_ack_block_t gab;
629 int length;
630 __u32 ctsn;
631 sctp_tsnmap_iter_t iter;
632 __u16 num_gabs;
633 __u16 num_dup_tsns = asoc->peer.next_dup_tsn;
634 const sctp_tsnmap_t *map = &asoc->peer.tsn_map;
636 ctsn = sctp_tsnmap_get_ctsn(map);
637 SCTP_DEBUG_PRINTK("make_sack: sackCTSNAck sent is 0x%x.\n",
638 ctsn);
640 /* Count the number of Gap Ack Blocks. */
641 sctp_tsnmap_iter_init(map, &iter);
642 for (num_gabs = 0;
643 sctp_tsnmap_next_gap_ack(map, &iter, &gab.start, &gab.end);
644 num_gabs++) {
645 /* Do nothing. */
648 /* Initialize the SACK header. */
649 sack.cum_tsn_ack = htonl(ctsn);
650 sack.a_rwnd = htonl(asoc->rwnd);
651 sack.num_gap_ack_blocks = htons(num_gabs);
652 sack.num_dup_tsns = htons(num_dup_tsns);
654 length = sizeof(sack)
655 + sizeof(sctp_gap_ack_block_t) * num_gabs
656 + sizeof(sctp_dup_tsn_t) * num_dup_tsns;
658 /* Create the chunk. */
659 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, length);
660 if (!retval)
661 goto nodata;
663 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
665 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
666 * HEARTBEAT ACK, etc.) to the same destination transport
667 * address from which it received the DATA or control chunk to
668 * which it is replying. This rule should also be followed if
669 * the endpoint is bundling DATA chunks together with the
670 * reply chunk.
672 * However, when acknowledging multiple DATA chunks received
673 * in packets from different source addresses in a single
674 * SACK, the SACK chunk may be transmitted to one of the
675 * destination transport addresses from which the DATA or
676 * control chunks being acknowledged were received.
678 * [BUG: We do not implement the following paragraph.
679 * Perhaps we should remember the last transport we used for a
680 * SACK and avoid that (if possible) if we have seen any
681 * duplicates. --piggy]
683 * When a receiver of a duplicate DATA chunk sends a SACK to a
684 * multi- homed endpoint it MAY be beneficial to vary the
685 * destination address and not use the source address of the
686 * DATA chunk. The reason being that receiving a duplicate
687 * from a multi-homed endpoint might indicate that the return
688 * path (as specified in the source address of the DATA chunk)
689 * for the SACK is broken.
691 * [Send to the address from which we last received a DATA chunk.]
693 retval->transport = asoc->peer.last_data_from;
695 retval->subh.sack_hdr =
696 sctp_addto_chunk(retval, sizeof(sack), &sack);
698 /* Put the Gap Ack Blocks into the chunk. */
699 sctp_tsnmap_iter_init(map, &iter);
700 while(sctp_tsnmap_next_gap_ack(map, &iter, &gab.start, &gab.end)) {
701 gab.start = htons(gab.start);
702 gab.end = htons(gab.end);
703 sctp_addto_chunk(retval,
704 sizeof(sctp_gap_ack_block_t),
705 &gab);
708 /* Register the duplicates. */
709 sctp_addto_chunk(retval,
710 sizeof(sctp_dup_tsn_t) * num_dup_tsns,
711 &asoc->peer.dup_tsns);
713 nodata:
714 return retval;
717 sctp_chunk_t *sctp_make_shutdown(const sctp_association_t *asoc)
719 sctp_chunk_t *retval;
720 sctp_shutdownhdr_t shut;
721 __u32 ctsn;
723 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
724 shut.cum_tsn_ack = htonl(ctsn);
726 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
727 sizeof(sctp_shutdownhdr_t));
728 if (!retval)
729 goto nodata;
731 retval->subh.shutdown_hdr =
732 sctp_addto_chunk(retval, sizeof(shut), &shut);
734 nodata:
735 return retval;
738 sctp_chunk_t *sctp_make_shutdown_ack(const sctp_association_t *asoc,
739 const sctp_chunk_t *chunk)
741 sctp_chunk_t *retval;
743 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
745 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
747 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
748 * HEARTBEAT ACK, * etc.) to the same destination transport
749 * address from which it * received the DATA or control chunk
750 * to which it is replying.
752 * [ACK back to where the SHUTDOWN came from.]
754 if (retval && chunk)
755 retval->transport = chunk->transport;
757 return retval;
760 sctp_chunk_t *sctp_make_shutdown_complete(const sctp_association_t *asoc,
761 const sctp_chunk_t *chunk)
763 sctp_chunk_t *retval;
764 __u8 flags = 0;
766 /* Maybe set the T-bit if we have no association. */
767 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
769 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
771 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
773 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
774 * HEARTBEAT ACK, * etc.) to the same destination transport
775 * address from which it * received the DATA or control chunk
776 * to which it is replying.
778 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
779 * came from.]
781 if (retval && chunk)
782 retval->transport = chunk->transport;
784 return retval;
787 /* Create an ABORT. Note that we set the T bit if we have no
788 * association.
790 sctp_chunk_t *sctp_make_abort(const sctp_association_t *asoc,
791 const sctp_chunk_t *chunk,
792 const size_t hint)
794 sctp_chunk_t *retval;
795 __u8 flags = 0;
797 /* Maybe set the T-bit if we have no association. */
798 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
800 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
802 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
804 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
805 * HEARTBEAT ACK, * etc.) to the same destination transport
806 * address from which it * received the DATA or control chunk
807 * to which it is replying.
809 * [ABORT back to where the offender came from.]
811 if (retval && chunk)
812 retval->transport = chunk->transport;
814 return retval;
817 /* Helper to create ABORT with a NO_USER_DATA error. */
818 sctp_chunk_t *sctp_make_abort_no_data(const sctp_association_t *asoc,
819 const sctp_chunk_t *chunk, __u32 tsn)
821 sctp_chunk_t *retval;
822 __u32 payload;
824 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
825 + sizeof(tsn));
827 if (!retval)
828 goto no_mem;
830 /* Put the tsn back into network byte order. */
831 payload = htonl(tsn);
832 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
833 sizeof(payload));
835 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
837 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
838 * HEARTBEAT ACK, * etc.) to the same destination transport
839 * address from which it * received the DATA or control chunk
840 * to which it is replying.
842 * [ABORT back to where the offender came from.]
844 if (chunk)
845 retval->transport = chunk->transport;
847 no_mem:
848 return retval;
851 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
852 sctp_chunk_t *sctp_make_abort_user(const sctp_association_t *asoc,
853 const sctp_chunk_t *chunk,
854 const struct msghdr *msg)
856 sctp_chunk_t *retval;
857 void *payload = NULL, *payoff;
858 size_t paylen;
859 struct iovec *iov = msg->msg_iov;
860 int iovlen = msg->msg_iovlen;
862 paylen = get_user_iov_size(iov, iovlen);
863 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen);
864 if (!retval)
865 goto err_chunk;
867 if (paylen) {
868 /* Put the msg_iov together into payload. */
869 payload = kmalloc(paylen, GFP_ATOMIC);
870 if (!payload)
871 goto err_payload;
872 payoff = payload;
874 for (; iovlen > 0; --iovlen) {
875 if (copy_from_user(payoff, iov->iov_base, iov->iov_len))
876 goto err_copy;
877 payoff += iov->iov_len;
878 iov++;
882 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
884 if (paylen)
885 kfree(payload);
887 return retval;
889 err_copy:
890 kfree(payload);
891 err_payload:
892 sctp_free_chunk(retval);
893 retval = NULL;
894 err_chunk:
895 return retval;
898 /* Make a HEARTBEAT chunk. */
899 sctp_chunk_t *sctp_make_heartbeat(const sctp_association_t *asoc,
900 const sctp_transport_t *transport,
901 const void *payload, const size_t paylen)
903 sctp_chunk_t *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
904 0, paylen);
906 if (!retval)
907 goto nodata;
909 /* Cast away the 'const', as this is just telling the chunk
910 * what transport it belongs to.
912 retval->transport = (sctp_transport_t *) transport;
913 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
915 nodata:
916 return retval;
919 sctp_chunk_t *sctp_make_heartbeat_ack(const sctp_association_t *asoc,
920 const sctp_chunk_t *chunk,
921 const void *payload, const size_t paylen)
923 sctp_chunk_t *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK,
924 0, paylen);
926 if (!retval)
927 goto nodata;
928 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
930 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
932 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
933 * HEARTBEAT ACK, * etc.) to the same destination transport
934 * address from which it * received the DATA or control chunk
935 * to which it is replying.
937 * [HBACK back to where the HEARTBEAT came from.]
939 if (chunk)
940 retval->transport = chunk->transport;
942 nodata:
943 return retval;
946 /* Create an Operation Error chunk with the specified space reserved.
947 * This routine can be used for containing multiple causes in the chunk.
949 sctp_chunk_t *sctp_make_op_error_space(const sctp_association_t *asoc,
950 const sctp_chunk_t *chunk,
951 size_t size)
953 sctp_chunk_t *retval;
955 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
956 sizeof(sctp_errhdr_t) + size);
957 if (!retval)
958 goto nodata;
960 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
962 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
963 * HEARTBEAT ACK, etc.) to the same destination transport
964 * address from which it received the DATA or control chunk
965 * to which it is replying.
968 if (chunk)
969 retval->transport = chunk->transport;
971 nodata:
972 return retval;
975 /* Create an Operation Error chunk. */
976 sctp_chunk_t *sctp_make_op_error(const sctp_association_t *asoc,
977 const sctp_chunk_t *chunk,
978 __u16 cause_code, const void *payload,
979 size_t paylen)
981 sctp_chunk_t *retval = sctp_make_op_error_space(asoc, chunk, paylen);
983 if (!retval)
984 goto nodata;
986 sctp_init_cause(retval, cause_code, payload, paylen);
988 nodata:
989 return retval;
992 /********************************************************************
993 * 2nd Level Abstractions
994 ********************************************************************/
996 /* Turn an skb into a chunk.
997 * FIXME: Eventually move the structure directly inside the skb->cb[].
999 sctp_chunk_t *sctp_chunkify(struct sk_buff *skb, const sctp_association_t *asoc,
1000 struct sock *sk)
1002 sctp_chunk_t *retval = t_new(sctp_chunk_t, GFP_ATOMIC);
1004 if (!retval)
1005 goto nodata;
1006 memset(retval, 0, sizeof(sctp_chunk_t));
1008 if (!sk) {
1009 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1012 retval->skb = skb;
1013 retval->asoc = (sctp_association_t *) asoc;
1014 retval->num_times_sent = 0;
1015 retval->has_tsn = 0;
1016 retval->rtt_in_progress = 0;
1017 retval->sent_at = jiffies;
1018 retval->singleton = 1;
1019 retval->end_of_packet = 0;
1020 retval->ecn_ce_done = 0;
1021 retval->pdiscard = 0;
1023 /* sctpimpguide-05.txt Section 2.8.2
1024 * M1) Each time a new DATA chunk is transmitted
1025 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1026 * 'TSN.Missing.Report' count will be used to determine missing chunks
1027 * and when to fast retransmit.
1029 retval->tsn_missing_report = 0;
1030 retval->tsn_gap_acked = 0;
1031 retval->fast_retransmit = 0;
1033 /* Polish the bead hole. */
1034 INIT_LIST_HEAD(&retval->transmitted_list);
1035 INIT_LIST_HEAD(&retval->frag_list);
1036 SCTP_DBG_OBJCNT_INC(chunk);
1038 nodata:
1039 return retval;
1042 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1043 void sctp_init_addrs(sctp_chunk_t *chunk, union sctp_addr *src,
1044 union sctp_addr *dest)
1046 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1047 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1050 /* Extract the source address from a chunk. */
1051 const union sctp_addr *sctp_source(const sctp_chunk_t *chunk)
1053 /* If we have a known transport, use that. */
1054 if (chunk->transport) {
1055 return &chunk->transport->ipaddr;
1056 } else {
1057 /* Otherwise, extract it from the IP header. */
1058 return &chunk->source;
1062 /* Create a new chunk, setting the type and flags headers from the
1063 * arguments, reserving enough space for a 'paylen' byte payload.
1065 sctp_chunk_t *sctp_make_chunk(const sctp_association_t *asoc,
1066 __u8 type, __u8 flags, int paylen)
1068 sctp_chunk_t *retval;
1069 sctp_chunkhdr_t *chunk_hdr;
1070 struct sk_buff *skb;
1071 struct sock *sk;
1073 skb = dev_alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen));
1074 if (!skb)
1075 goto nodata;
1077 /* Make room for the chunk header. */
1078 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1079 skb_pull(skb, sizeof(sctp_chunkhdr_t));
1081 chunk_hdr->type = type;
1082 chunk_hdr->flags = flags;
1083 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1085 /* Move the data pointer back up to the start of the chunk. */
1086 skb_push(skb, sizeof(sctp_chunkhdr_t));
1088 sk = asoc ? asoc->base.sk : NULL;
1089 retval = sctp_chunkify(skb, asoc, sk);
1090 if (!retval) {
1091 dev_kfree_skb(skb);
1092 goto nodata;
1095 retval->chunk_hdr = chunk_hdr;
1096 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(sctp_chunkhdr_t);
1098 /* Set the skb to the belonging sock for accounting. */
1099 skb->sk = sk;
1101 return retval;
1103 nodata:
1104 return NULL;
1107 /* Release the memory occupied by a chunk. */
1108 void sctp_free_chunk(sctp_chunk_t *chunk)
1110 /* Make sure that we are not on any list. */
1111 skb_unlink((struct sk_buff *) chunk);
1112 list_del(&chunk->transmitted_list);
1114 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1115 dev_kfree_skb(chunk->skb);
1117 kfree(chunk);
1118 SCTP_DBG_OBJCNT_DEC(chunk);
1121 /* Do a deep copy of a chunk. */
1122 sctp_chunk_t *sctp_copy_chunk(sctp_chunk_t *chunk, const int priority)
1124 sctp_chunk_t *retval;
1125 long offset;
1127 retval = t_new(sctp_chunk_t, priority);
1128 if (!retval)
1129 goto nodata;
1131 /* Do the shallow copy. */
1132 *retval = *chunk;
1134 /* Make sure that the copy does NOT think it is on any lists. */
1135 retval->next = NULL;
1136 retval->prev = NULL;
1137 retval->list = NULL;
1138 INIT_LIST_HEAD(&retval->transmitted_list);
1139 INIT_LIST_HEAD(&retval->frag_list);
1141 /* Now we copy the deep structure. */
1142 retval->skb = skb_copy(chunk->skb, priority);
1143 if (!retval->skb) {
1144 kfree(retval);
1145 goto nodata;
1148 /* Move the copy headers to point into the new skb. */
1149 offset = ((__u8 *)retval->skb->head)
1150 - ((__u8 *)chunk->skb->head);
1152 if (retval->param_hdr.v)
1153 retval->param_hdr.v += offset;
1154 if (retval->subh.v)
1155 retval->subh.v += offset;
1156 if (retval->chunk_end)
1157 ((__u8 *) retval->chunk_end) += offset;
1158 if (retval->chunk_hdr)
1159 ((__u8 *) retval->chunk_hdr) += offset;
1160 if (retval->sctp_hdr)
1161 ((__u8 *) retval->sctp_hdr) += offset;
1162 SCTP_DBG_OBJCNT_INC(chunk);
1163 return retval;
1165 nodata:
1166 return NULL;
1169 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1170 * enough.
1172 void *sctp_addto_chunk(sctp_chunk_t *chunk, int len, const void *data)
1174 void *target;
1175 void *padding;
1176 int chunklen = ntohs(chunk->chunk_hdr->length);
1177 int padlen = chunklen % 4;
1179 padding = skb_put(chunk->skb, padlen);
1180 target = skb_put(chunk->skb, len);
1182 memset(padding, 0, padlen);
1183 memcpy(target, data, len);
1185 /* Adjust the chunk length field. */
1186 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1187 chunk->chunk_end = chunk->skb->tail;
1189 return target;
1192 /* Append bytes from user space to the end of a chunk. Will panic if
1193 * chunk is not big enough.
1194 * Returns a kernel err value.
1196 int sctp_user_addto_chunk(sctp_chunk_t *chunk, int len, struct iovec *data)
1198 __u8 *target;
1199 int err = 0;
1201 /* Make room in chunk for data. */
1202 target = skb_put(chunk->skb, len);
1204 /* Copy data (whole iovec) into chunk */
1205 if ((err = memcpy_fromiovec(target, data, len)))
1206 goto out;
1208 /* Adjust the chunk length field. */
1209 chunk->chunk_hdr->length =
1210 htons(ntohs(chunk->chunk_hdr->length) + len);
1211 chunk->chunk_end = chunk->skb->tail;
1213 out:
1214 return err;
1217 /* Helper function to assign a TSN if needed. This assumes that both
1218 * the data_hdr and association have already been assigned.
1220 void sctp_chunk_assign_tsn(sctp_chunk_t *chunk)
1222 if (!chunk->has_tsn) {
1223 /* This is the last possible instant to
1224 * assign a TSN.
1226 chunk->subh.data_hdr->tsn =
1227 htonl(__sctp_association_get_next_tsn(chunk->asoc));
1228 chunk->has_tsn = 1;
1232 /* Create a CLOSED association to use with an incoming packet. */
1233 sctp_association_t *sctp_make_temp_asoc(const sctp_endpoint_t *ep,
1234 sctp_chunk_t *chunk,
1235 int priority)
1237 sctp_association_t *asoc;
1238 sctp_scope_t scope;
1240 /* Create the bare association. */
1241 scope = sctp_scope(sctp_source(chunk));
1242 asoc = sctp_association_new(ep, ep->base.sk, scope, priority);
1243 if (!asoc)
1244 goto nodata;
1246 /* Create an entry for the source address of the packet. */
1247 switch (chunk->skb->nh.iph->version) {
1248 case 4:
1249 asoc->c.peer_addr.v4.sin_family = AF_INET;
1250 asoc->c.peer_addr.v4.sin_port = ntohs(chunk->sctp_hdr->source);
1251 asoc->c.peer_addr.v4.sin_addr.s_addr =
1252 chunk->skb->nh.iph->saddr;
1253 break;
1255 case 6:
1256 asoc->c.peer_addr.v6.sin6_family = AF_INET6;
1257 asoc->c.peer_addr.v6.sin6_port
1258 = ntohs(chunk->sctp_hdr->source);
1259 asoc->c.peer_addr.v6.sin6_flowinfo = 0; /* BUG BUG BUG */
1260 asoc->c.peer_addr.v6.sin6_addr = chunk->skb->nh.ipv6h->saddr;
1261 asoc->c.peer_addr.v6.sin6_scope_id = 0; /* BUG BUG BUG */
1262 break;
1264 default:
1265 /* Yikes! I never heard of this kind of address. */
1266 goto fail;
1269 nodata:
1270 return asoc;
1272 fail:
1273 sctp_association_free(asoc);
1274 return NULL;
1277 /* Build a cookie representing asoc.
1278 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1280 sctp_cookie_param_t *sctp_pack_cookie(const sctp_endpoint_t *ep,
1281 const sctp_association_t *asoc,
1282 const sctp_chunk_t *init_chunk,
1283 int *cookie_len,
1284 const __u8 *raw_addrs, int addrs_len)
1286 sctp_cookie_param_t *retval;
1287 sctp_signed_cookie_t *cookie;
1288 int headersize, bodysize;
1290 headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
1291 bodysize = sizeof(sctp_cookie_t)
1292 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1294 /* Pad out the cookie to a multiple to make the signature
1295 * functions simpler to write.
1297 if (bodysize % SCTP_COOKIE_MULTIPLE)
1298 bodysize += SCTP_COOKIE_MULTIPLE
1299 - (bodysize % SCTP_COOKIE_MULTIPLE);
1300 *cookie_len = headersize + bodysize;
1302 retval = (sctp_cookie_param_t *)
1303 kmalloc(*cookie_len, GFP_ATOMIC);
1304 if (!retval) {
1305 *cookie_len = 0;
1306 goto nodata;
1309 /* Clear this memory since we are sending this data structure
1310 * out on the network.
1312 memset(retval, 0x00, *cookie_len);
1313 cookie = (sctp_signed_cookie_t *) retval->body;
1315 /* Set up the parameter header. */
1316 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1317 retval->p.length = htons(*cookie_len);
1319 /* Copy the cookie part of the association itself. */
1320 cookie->c = asoc->c;
1321 /* Save the raw address list length in the cookie. */
1322 cookie->c.raw_addr_list_len = addrs_len;
1324 /* Set an expiration time for the cookie. */
1325 do_gettimeofday(&cookie->c.expiration);
1326 tv_add(&asoc->cookie_life, &cookie->c.expiration);
1328 /* Copy the peer's init packet. */
1329 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1330 ntohs(init_chunk->chunk_hdr->length));
1332 /* Copy the raw local address list of the association. */
1333 memcpy((__u8 *)&cookie->c.peer_init[0] +
1334 ntohs(init_chunk->chunk_hdr->length), raw_addrs,
1335 addrs_len);
1337 /* Sign the message. */
1338 sctp_hash_digest(ep->secret_key[ep->current_key], SCTP_SECRET_SIZE,
1339 (__u8 *) &cookie->c, bodysize, cookie->signature);
1341 nodata:
1342 return retval;
1345 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1346 sctp_association_t *sctp_unpack_cookie(const sctp_endpoint_t *ep,
1347 const sctp_association_t *asoc,
1348 sctp_chunk_t *chunk, int priority,
1349 int *error, sctp_chunk_t **err_chk_p)
1351 sctp_association_t *retval = NULL;
1352 sctp_signed_cookie_t *cookie;
1353 sctp_cookie_t *bear_cookie;
1354 int headersize, bodysize;
1355 int fixed_size, var_size1, var_size2, var_size3;
1356 __u8 digest_buf[SCTP_SIGNATURE_SIZE];
1357 int secret;
1358 sctp_scope_t scope;
1359 __u8 *raw_addr_list;
1361 headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
1362 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1363 fixed_size = headersize + sizeof(sctp_cookie_t);
1365 /* Verify that the chunk looks like it even has a cookie.
1366 * There must be enough room for our cookie and our peer's
1367 * INIT chunk.
1369 if (ntohs(chunk->chunk_hdr->length) <
1370 (fixed_size + sizeof(sctp_chunkhdr_t)))
1371 goto malformed;
1373 /* Verify that the cookie has been padded out. */
1374 if (bodysize % SCTP_COOKIE_MULTIPLE)
1375 goto malformed;
1377 /* Process the cookie. */
1378 cookie = chunk->subh.cookie_hdr;
1379 bear_cookie = &cookie->c;
1380 var_size1 = ntohs(chunk->chunk_hdr->length) - fixed_size;
1381 var_size2 = ntohs(bear_cookie->peer_init->chunk_hdr.length);
1382 var_size3 = bear_cookie->raw_addr_list_len;
1384 /* Check the signature. */
1385 secret = ep->current_key;
1386 sctp_hash_digest(ep->secret_key[secret], SCTP_SECRET_SIZE,
1387 (__u8 *) bear_cookie, bodysize,
1388 digest_buf);
1389 if (memcmp(digest_buf, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1390 /* Try the previous key. */
1391 secret = ep->last_key;
1392 sctp_hash_digest(ep->secret_key[secret], SCTP_SECRET_SIZE,
1393 (__u8 *) bear_cookie, bodysize, digest_buf);
1394 if (memcmp(digest_buf, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1395 /* Yikes! Still bad signature! */
1396 *error = -SCTP_IERROR_BAD_SIG;
1397 goto fail;
1401 /* Check to see if the cookie is stale. If there is already
1402 * an association, there is no need to check cookie's expiration
1403 * for init collision case of lost COOKIE ACK.
1405 if (!asoc && tv_lt(bear_cookie->expiration, chunk->skb->stamp)) {
1407 * Section 3.3.10.3 Stale Cookie Error (3)
1409 * Cause of error
1410 * ---------------
1411 * Stale Cookie Error: Indicates the receipt of a valid State
1412 * Cookie that has expired.
1414 *err_chk_p = sctp_make_op_error_space(asoc, chunk,
1415 ntohs(chunk->chunk_hdr->length));
1416 if (*err_chk_p) {
1417 suseconds_t usecs = (chunk->skb->stamp.tv_sec -
1418 bear_cookie->expiration.tv_sec) * 1000000L +
1419 chunk->skb->stamp.tv_usec -
1420 bear_cookie->expiration.tv_usec;
1422 usecs = htonl(usecs);
1423 sctp_init_cause(*err_chk_p, SCTP_ERROR_STALE_COOKIE,
1424 &usecs, sizeof(usecs));
1425 *error = -SCTP_IERROR_STALE_COOKIE;
1426 } else
1427 *error = -SCTP_IERROR_NOMEM;
1429 goto fail;
1432 /* Make a new base association. */
1433 scope = sctp_scope(sctp_source(chunk));
1434 retval = sctp_association_new(ep, ep->base.sk, scope, priority);
1435 if (!retval) {
1436 *error = -SCTP_IERROR_NOMEM;
1437 goto fail;
1440 /* Set up our peer's port number. */
1441 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1443 /* Populate the association from the cookie. */
1444 retval->c = *bear_cookie;
1446 /* Build the bind address list based on the cookie. */
1447 raw_addr_list = (__u8 *) bear_cookie +
1448 sizeof(sctp_cookie_t) + var_size2;
1449 if (sctp_raw_to_bind_addrs(&retval->base.bind_addr, raw_addr_list,
1450 var_size3, retval->base.bind_addr.port,
1451 priority)) {
1452 *error = -SCTP_IERROR_NOMEM;
1453 goto fail;
1456 retval->next_tsn = retval->c.initial_tsn;
1457 retval->ctsn_ack_point = retval->next_tsn - 1;
1459 /* The INIT stuff will be done by the side effects. */
1460 return retval;
1462 fail:
1463 if (retval)
1464 sctp_association_free(retval);
1466 return NULL;
1468 malformed:
1469 /* Yikes! The packet is either corrupt or deliberately
1470 * malformed.
1472 *error = -SCTP_IERROR_MALFORMED;
1473 goto fail;
1476 /********************************************************************
1477 * 3rd Level Abstractions
1478 ********************************************************************/
1480 /* Do not attempt to handle the HOST_NAME parm. However, do
1481 * send back an indicator to the peer.
1483 static int sctp_process_hn_param(const sctp_association_t *asoc,
1484 union sctp_params param,
1485 sctp_chunk_t *chunk,
1486 sctp_chunk_t **err_chk_p)
1488 __u16 len = ntohs(param.p->length);
1490 /* Make an ERROR chunk, preparing enough room for
1491 * returning multiple unknown parameters.
1493 if (!*err_chk_p)
1494 *err_chk_p = sctp_make_op_error_space(asoc, chunk, len);
1496 if (*err_chk_p)
1497 sctp_init_cause(*err_chk_p, SCTP_ERROR_DNS_FAILED,
1498 param.v, len);
1500 /* Stop processing this chunk. */
1501 return 0;
1504 /* RFC 3.2.1 & the Implementers Guide 2.2.
1506 * The Parameter Types are encoded such that the
1507 * highest-order two bits specify the action that must be
1508 * taken if the processing endpoint does not recognize the
1509 * Parameter Type.
1511 * 00 - Stop processing this SCTP chunk and discard it,
1512 * do not process any further chunks within it.
1514 * 01 - Stop processing this SCTP chunk and discard it,
1515 * do not process any further chunks within it, and report
1516 * the unrecognized parameter in an 'Unrecognized
1517 * Parameter Type' (in either an ERROR or in the INIT ACK).
1519 * 10 - Skip this parameter and continue processing.
1521 * 11 - Skip this parameter and continue processing but
1522 * report the unrecognized parameter in an
1523 * 'Unrecognized Parameter Type' (in either an ERROR or in
1524 * the INIT ACK).
1526 * Return value:
1527 * 0 - discard the chunk
1528 * 1 - continue with the chunk
1530 static int sctp_process_unk_param(const sctp_association_t *asoc,
1531 union sctp_params param,
1532 sctp_chunk_t *chunk,
1533 sctp_chunk_t **err_chk_p)
1535 int retval = 1;
1537 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1538 case SCTP_PARAM_ACTION_DISCARD:
1539 retval = 0;
1540 break;
1541 case SCTP_PARAM_ACTION_DISCARD_ERR:
1542 retval = 0;
1543 /* Make an ERROR chunk, preparing enough room for
1544 * returning multiple unknown parameters.
1546 if (NULL == *err_chk_p)
1547 *err_chk_p = sctp_make_op_error_space(asoc, chunk,
1548 ntohs(chunk->chunk_hdr->length));
1550 if (*err_chk_p)
1551 sctp_init_cause(*err_chk_p, SCTP_ERROR_UNKNOWN_PARAM,
1552 param.v,
1553 WORD_ROUND(ntohs(param.p->length)));
1555 break;
1556 case SCTP_PARAM_ACTION_SKIP:
1557 break;
1558 case SCTP_PARAM_ACTION_SKIP_ERR:
1559 /* Make an ERROR chunk, preparing enough room for
1560 * returning multiple unknown parameters.
1562 if (NULL == *err_chk_p)
1563 *err_chk_p = sctp_make_op_error_space(asoc, chunk,
1564 ntohs(chunk->chunk_hdr->length));
1566 if (*err_chk_p) {
1567 sctp_init_cause(*err_chk_p, SCTP_ERROR_UNKNOWN_PARAM,
1568 param.v,
1569 WORD_ROUND(ntohs(param.p->length)));
1570 } else {
1571 /* If there is no memory for generating the ERROR
1572 * report as specified, an ABORT will be triggered
1573 * to the peer and the association won't be established.
1575 retval = 0;
1578 break;
1579 default:
1580 break;
1583 return retval;
1586 /* Find unrecognized parameters in the chunk.
1587 * Return values:
1588 * 0 - discard the chunk
1589 * 1 - continue with the chunk
1591 static int sctp_verify_param(const sctp_association_t *asoc,
1592 union sctp_params param,
1593 sctp_cid_t cid,
1594 sctp_chunk_t *chunk,
1595 sctp_chunk_t **err_chunk)
1597 int retval = 1;
1599 /* FIXME - This routine is not looking at each parameter per the
1600 * chunk type, i.e., unrecognized parameters should be further
1601 * identified based on the chunk id.
1604 switch (param.p->type) {
1605 case SCTP_PARAM_IPV4_ADDRESS:
1606 case SCTP_PARAM_IPV6_ADDRESS:
1607 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1608 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1609 case SCTP_PARAM_STATE_COOKIE:
1610 case SCTP_PARAM_HEARTBEAT_INFO:
1611 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1612 case SCTP_PARAM_ECN_CAPABLE:
1613 break;
1615 case SCTP_PARAM_HOST_NAME_ADDRESS:
1616 /* Tell the peer, we won't support this param. */
1617 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1618 default:
1619 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1620 ntohs(param.p->type), cid);
1621 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1623 break;
1625 return retval;
1628 /* Verify the INIT packet before we process it. */
1629 int sctp_verify_init(const sctp_association_t *asoc,
1630 sctp_cid_t cid,
1631 sctp_init_chunk_t *peer_init,
1632 sctp_chunk_t *chunk,
1633 sctp_chunk_t **err_chk_p)
1635 union sctp_params param;
1637 /* FIXME - Verify the fixed fields of the INIT chunk. Also, verify
1638 * the mandatory parameters somewhere here and generate either the
1639 * "Missing mandatory parameter" error or the "Invalid mandatory
1640 * parameter" error.
1643 /* Find unrecognized parameters. */
1645 sctp_walk_params(param, peer_init, init_hdr.params) {
1647 if (!sctp_verify_param(asoc, param, cid, chunk, err_chk_p))
1648 return 0;
1650 } /* for (loop through all parameters) */
1652 return 1;
1655 /* Unpack the parameters in an INIT packet into an association.
1656 * Returns 0 on failure, else success.
1658 int sctp_process_init(sctp_association_t *asoc, sctp_cid_t cid,
1659 const union sctp_addr *peer_addr,
1660 sctp_init_chunk_t *peer_init,
1661 int priority)
1663 union sctp_params param;
1664 sctp_transport_t *transport;
1665 struct list_head *pos, *temp;
1666 char *cookie;
1668 /* We must include the address that the INIT packet came from.
1669 * This is the only address that matters for an INIT packet.
1670 * When processing a COOKIE ECHO, we retrieve the from address
1671 * of the INIT from the cookie.
1674 /* This implementation defaults to making the first transport
1675 * added as the primary transport. The source address seems to
1676 * be a a better choice than any of the embedded addresses.
1678 if (peer_addr)
1679 if(!sctp_assoc_add_peer(asoc, peer_addr, priority))
1680 goto nomem;
1682 /* Process the initialization parameters. */
1684 sctp_walk_params(param, peer_init, init_hdr.params) {
1686 if (!sctp_process_param(asoc, param, peer_addr, priority))
1687 goto clean_up;
1690 /* The fixed INIT headers are always in network byte
1691 * order.
1693 asoc->peer.i.init_tag =
1694 ntohl(peer_init->init_hdr.init_tag);
1695 asoc->peer.i.a_rwnd =
1696 ntohl(peer_init->init_hdr.a_rwnd);
1697 asoc->peer.i.num_outbound_streams =
1698 ntohs(peer_init->init_hdr.num_outbound_streams);
1699 asoc->peer.i.num_inbound_streams =
1700 ntohs(peer_init->init_hdr.num_inbound_streams);
1701 asoc->peer.i.initial_tsn =
1702 ntohl(peer_init->init_hdr.initial_tsn);
1704 /* Apply the upper bounds for output streams based on peer's
1705 * number of inbound streams.
1707 if (asoc->c.sinit_num_ostreams >
1708 ntohs(peer_init->init_hdr.num_inbound_streams)) {
1709 asoc->c.sinit_num_ostreams =
1710 ntohs(peer_init->init_hdr.num_inbound_streams);
1713 /* Copy Initiation tag from INIT to VT_peer in cookie. */
1714 asoc->c.peer_vtag = asoc->peer.i.init_tag;
1716 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
1717 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1719 /* Copy cookie in case we need to resend COOKIE-ECHO. */
1720 cookie = asoc->peer.cookie;
1721 if (cookie) {
1722 asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, priority);
1723 if (!asoc->peer.cookie)
1724 goto clean_up;
1725 memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
1728 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1729 * high (for example, implementations MAY use the size of the receiver
1730 * advertised window).
1732 list_for_each(pos, &asoc->peer.transport_addr_list) {
1733 transport = list_entry(pos, sctp_transport_t, transports);
1734 transport->ssthresh = asoc->peer.i.a_rwnd;
1737 /* Set up the TSN tracking pieces. */
1738 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1739 asoc->peer.i.initial_tsn);
1741 /* ADDIP Section 4.1 ASCONF Chunk Procedures
1743 * When an endpoint has an ASCONF signaled change to be sent to the
1744 * remote endpoint it should do the following:
1745 * ...
1746 * A2) A serial number should be assigned to the Chunk. The serial
1747 * number should be a monotonically increasing number. All serial
1748 * numbers are defined to be initialized at the start of the
1749 * association to the same value as the Initial TSN.
1751 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1752 return 1;
1754 clean_up:
1755 /* Release the transport structures. */
1756 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1757 transport = list_entry(pos, sctp_transport_t, transports);
1758 list_del(pos);
1759 sctp_transport_free(transport);
1761 nomem:
1762 return 0;
1766 /* Update asoc with the option described in param.
1768 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1770 * asoc is the association to update.
1771 * param is the variable length parameter to use for update.
1772 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1773 * If the current packet is an INIT we want to minimize the amount of
1774 * work we do. In particular, we should not build transport
1775 * structures for the addresses.
1777 int sctp_process_param(sctp_association_t *asoc, union sctp_params param,
1778 const union sctp_addr *peer_addr, int priority)
1780 union sctp_addr addr;
1781 int i;
1782 __u16 sat;
1783 int retval = 1;
1784 sctp_scope_t scope;
1785 time_t stale;
1787 /* We maintain all INIT parameters in network byte order all the
1788 * time. This allows us to not worry about whether the parameters
1789 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
1791 switch (param.p->type) {
1792 case SCTP_PARAM_IPV6_ADDRESS:
1793 if( PF_INET6 != asoc->base.sk->family)
1794 break;
1795 /* Fall through. */
1796 case SCTP_PARAM_IPV4_ADDRESS:
1797 sctp_param2sockaddr(&addr, param.addr, asoc->peer.port);
1798 scope = sctp_scope(peer_addr);
1799 if (sctp_in_scope(&addr, scope))
1800 if (!sctp_assoc_add_peer(asoc, &addr, priority))
1801 return 0;
1802 break;
1804 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1805 if (!sctp_proto.cookie_preserve_enable)
1806 break;
1808 stale = ntohl(param.life->lifespan_increment);
1810 /* Suggested Cookie Life span increment's unit is msec,
1811 * (1/1000sec).
1813 asoc->cookie_life.tv_sec += stale / 1000;
1814 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
1815 break;
1817 case SCTP_PARAM_HOST_NAME_ADDRESS:
1818 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
1819 break;
1821 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1822 /* Turn off the default values first so we'll know which
1823 * ones are really set by the peer.
1825 asoc->peer.ipv4_address = 0;
1826 asoc->peer.ipv6_address = 0;
1828 /* Cycle through address types; avoid divide by 0. */
1829 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1830 if (sat)
1831 sat /= sizeof(__u16);
1833 for (i = 0; i < sat; ++i) {
1834 switch (param.sat->types[i]) {
1835 case SCTP_PARAM_IPV4_ADDRESS:
1836 asoc->peer.ipv4_address = 1;
1837 break;
1839 case SCTP_PARAM_IPV6_ADDRESS:
1840 asoc->peer.ipv6_address = 1;
1841 break;
1843 case SCTP_PARAM_HOST_NAME_ADDRESS:
1844 asoc->peer.hostname_address = 1;
1845 break;
1847 default: /* Just ignore anything else. */
1848 break;
1851 break;
1853 case SCTP_PARAM_STATE_COOKIE:
1854 asoc->peer.cookie_len =
1855 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1856 asoc->peer.cookie = param.cookie->body;
1857 break;
1859 case SCTP_PARAM_HEARTBEAT_INFO:
1860 /* Would be odd to receive, but it causes no problems. */
1861 break;
1863 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1864 /* Rejected during verify stage. */
1865 break;
1867 case SCTP_PARAM_ECN_CAPABLE:
1868 asoc->peer.ecn_capable = 1;
1869 break;
1871 default:
1872 /* Any unrecognized parameters should have been caught
1873 * and handled by sctp_verify_param() which should be
1874 * called prior to this routine. Simply log the error
1875 * here.
1877 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
1878 ntohs(param.p->type), asoc);
1879 break;
1882 return retval;
1885 /* Select a new verification tag. */
1886 __u32 sctp_generate_tag(const sctp_endpoint_t *ep)
1888 /* I believe that this random number generator complies with RFC1750.
1889 * A tag of 0 is reserved for special cases (e.g. INIT).
1891 __u32 x;
1893 do {
1894 get_random_bytes(&x, sizeof(__u32));
1895 } while (x == 0);
1897 return x;
1900 /* Select an initial TSN to send during startup. */
1901 __u32 sctp_generate_tsn(const sctp_endpoint_t *ep)
1903 __u32 retval;
1905 get_random_bytes(&retval, sizeof(__u32));
1906 return retval;
1909 /********************************************************************
1910 * 4th Level Abstractions
1911 ********************************************************************/
1913 /* Convert from an SCTP IP parameter to a union sctp_addr. */
1914 void sctp_param2sockaddr(union sctp_addr *addr, sctp_addr_param_t *param,
1915 __u16 port)
1917 switch(param->v4.param_hdr.type) {
1918 case SCTP_PARAM_IPV4_ADDRESS:
1919 addr->v4.sin_family = AF_INET;
1920 addr->v4.sin_port = port;
1921 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
1922 break;
1924 case SCTP_PARAM_IPV6_ADDRESS:
1925 addr->v6.sin6_family = AF_INET6;
1926 addr->v6.sin6_port = port;
1927 addr->v6.sin6_flowinfo = 0; /* BUG */
1928 addr->v6.sin6_addr = param->v6.addr;
1929 addr->v6.sin6_scope_id = 0; /* BUG */
1930 break;
1932 default:
1933 SCTP_DEBUG_PRINTK("Illegal address type %d\n",
1934 ntohs(param->v4.param_hdr.type));
1935 break;
1939 /* Convert an IP address in an SCTP param into a sockaddr_in. */
1940 /* Returns true if a valid conversion was possible. */
1941 int sctp_addr2sockaddr(union sctp_params p, union sctp_addr *sa)
1943 switch (p.p->type) {
1944 case SCTP_PARAM_IPV4_ADDRESS:
1945 sa->v4.sin_addr = *((struct in_addr *)&p.v4->addr);
1946 sa->v4.sin_family = AF_INET;
1947 break;
1949 case SCTP_PARAM_IPV6_ADDRESS:
1950 *((struct in6_addr *)&sa->v4.sin_addr)
1951 = p.v6->addr;
1952 sa->v4.sin_family = AF_INET6;
1953 break;
1955 default:
1956 return 0;
1959 return 1;
1962 /* Convert a sockaddr_in to an IP address in an SCTP param.
1963 * Returns len if a valid conversion was possible.
1965 int sockaddr2sctp_addr(const union sctp_addr *sa, sctp_addr_param_t *p)
1967 int len = 0;
1969 switch (sa->v4.sin_family) {
1970 case AF_INET:
1971 p->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
1972 p->v4.param_hdr.length = ntohs(sizeof(sctp_ipv4addr_param_t));
1973 len = sizeof(sctp_ipv4addr_param_t);
1974 p->v4.addr.s_addr = sa->v4.sin_addr.s_addr;
1975 break;
1977 case AF_INET6:
1978 p->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
1979 p->v6.param_hdr.length = ntohs(sizeof(sctp_ipv6addr_param_t));
1980 len = sizeof(sctp_ipv6addr_param_t);
1981 p->v6.addr = *(&sa->v6.sin6_addr);
1982 break;
1984 default:
1985 printk(KERN_WARNING "sockaddr2sctp_addr: Illegal family %d.\n",
1986 sa->v4.sin_family);
1987 return 0;
1990 return len;