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.
7 * This file is part of the SCTP kernel reference Implementation
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.
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)
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.
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.
30 * Please send any bug reports or fixes you make to the
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
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>
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
53 #include <linux/types.h>
54 #include <linux/kernel.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>
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;
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,
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,
83 /* What was the inbound interface for this chunk? */
84 int sctp_chunk_iif(const struct sctp_chunk *chunk)
89 af = sctp_get_af_specific(ipver2af(chunk->skb->nh.iph->version));
91 iif = af->skb_iif(chunk->skb);
96 /* RFC 2960 3.3.2 Initiation (INIT) (1)
98 * Note 2: The ECN capable field is reserved for future use of
99 * Explicit Congestion Notification.
101 static const struct sctp_paramhdr ecap_param = {
102 SCTP_PARAM_ECN_CAPABLE,
103 __constant_htons(sizeof(struct sctp_paramhdr)),
105 static const struct sctp_paramhdr prsctp_param = {
106 SCTP_PARAM_FWD_TSN_SUPPORT,
107 __constant_htons(sizeof(struct sctp_paramhdr)),
110 /* A helper to initialize to initialize an op error inside a
111 * provided chunk, as most cause codes will be embedded inside an
114 void sctp_init_cause(struct sctp_chunk *chunk, __u16 cause_code,
115 const void *payload, size_t paylen)
121 /* Cause code constants are now defined in network order. */
122 err.cause = cause_code;
123 len = sizeof(sctp_errhdr_t) + paylen;
125 err.length = htons(len);
127 sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
128 chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
131 /* 3.3.2 Initiation (INIT) (1)
133 * This chunk is used to initiate a SCTP association between two
134 * endpoints. The format of the INIT chunk is shown below:
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * | Advertised Receiver Window Credit (a_rwnd) |
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * | Number of Outbound Streams | Number of Inbound Streams |
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 * / Optional/Variable-Length Parameters /
152 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
155 * The INIT chunk contains the following parameters. Unless otherwise
156 * noted, each parameter MUST only be included once in the INIT chunk.
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
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
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)
180 union sctp_params addrs;
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;
187 sctp_adaption_ind_param_t aiparam;
189 /* RFC 2960 3.3.2 Initiation (INIT) (1)
191 * Note 1: The INIT chunks can contain multiple addresses that
192 * can be IPv4 and/or IPv6 in any combination.
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)
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.
225 * PLEASE DO NOT FIXME [This version does not support Host Name.]
228 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
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)
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.
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);
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)
265 sctp_inithdr_t initack;
266 struct sctp_chunk *retval;
267 union sctp_params addrs;
269 sctp_cookie_param_t *cookie;
272 sctp_adaption_ind_param_t aiparam;
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.
288 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
293 /* Calculate the total size of allocation, include the reserved
294 * space for reporting unknown parameters if it is specified.
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);
313 /* Per the advice in RFC 2960 6.4, send this reply to
314 * the source of the INIT packet.
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
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.
341 * [INIT ACK back to where the INIT came from.]
344 retval->transport = chunk->transport;
353 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
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.
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
372 * Set to zero on transmit and ignored on receipt.
374 * Length: 16 bits (unsigned integer)
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.
379 * Cookie: variable size
381 * This field must contain the exact cookie received in the
382 * State Cookie parameter from the previous INIT ACK.
384 * An implementation SHOULD make the cookie as small as possible
385 * to insure interoperability.
387 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
388 const struct sctp_chunk *chunk)
390 struct sctp_chunk *retval;
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);
401 retval->subh.cookie_hdr =
402 sctp_addto_chunk(retval, cookie_len, cookie);
404 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
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.
411 * [COOKIE ECHO back to where the INIT ACK came from.]
414 retval->transport = chunk->transport;
420 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
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.
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
434 * Chunk Flags: 8 bits
436 * Set to zero on transmit and ignored on receipt.
438 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
439 const struct sctp_chunk *chunk)
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
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.
452 * [COOKIE ACK back to where the COOKIE ECHO came from.]
455 retval->transport = chunk->transport;
461 * Appendix A: Explicit Congestion Notification:
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
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
481 * Note: The CWR is considered a Control chunk.
483 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
484 const __u32 lowest_tsn,
485 const struct sctp_chunk *chunk)
487 struct sctp_chunk *retval;
490 cwr.lowest_tsn = htonl(lowest_tsn);
491 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
492 sizeof(sctp_cwrhdr_t));
497 retval->subh.ecn_cwr_hdr =
498 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
500 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
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.
507 * [Report a reduced congestion window back to where the ECNE
511 retval->transport = chunk->transport;
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)
521 struct sctp_chunk *retval;
524 ecne.lowest_tsn = htonl(lowest_tsn);
525 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
526 sizeof(sctp_ecnehdr_t));
529 retval->subh.ecne_hdr =
530 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
536 /* Make a DATA chunk for the given association from the provided
537 * parameters. However, do not populate the data payload.
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)
543 struct sctp_chunk *retval;
544 struct sctp_datahdr dp;
547 /* We assign the TSN as LATE as possible, not here when
548 * creating the chunk.
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;
561 chunk_len = sizeof(dp) + data_len;
562 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
566 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
567 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
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.
577 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
579 struct sctp_chunk *retval;
580 struct sctp_sackhdr sack;
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);
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);
608 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
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
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.
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]
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.
636 * [Send to the address from which we last received a DATA chunk.]
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. */
645 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
646 sctp_tsnmap_get_gabs(map));
648 /* Add the duplicate TSN information. */
650 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
651 sctp_tsnmap_get_dups(map));
657 /* Make a SHUTDOWN chunk. */
658 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
659 const struct sctp_chunk *chunk)
661 struct sctp_chunk *retval;
662 sctp_shutdownhdr_t shut;
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));
673 retval->subh.shutdown_hdr =
674 sctp_addto_chunk(retval, sizeof(shut), &shut);
677 retval->transport = chunk->transport;
682 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
683 const struct sctp_chunk *chunk)
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
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.
696 * [ACK back to where the SHUTDOWN came from.]
699 retval->transport = chunk->transport;
704 struct sctp_chunk *sctp_make_shutdown_complete(
705 const struct sctp_association *asoc,
706 const struct sctp_chunk *chunk)
708 struct sctp_chunk *retval;
711 /* Set the T-bit if we have no association (vtag will be
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
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.
725 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
729 retval->transport = chunk->transport;
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).
737 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
738 const struct sctp_chunk *chunk,
741 struct sctp_chunk *retval;
744 /* Set the T-bit if we have no association and 'chunk' is not
745 * an INIT (vtag will be reflected).
748 if (chunk && chunk->chunk_hdr &&
749 chunk->chunk_hdr->type == SCTP_CID_INIT)
752 flags = SCTP_CHUNK_FLAG_T;
755 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
757 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
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.
764 * [ABORT back to where the offender came from.]
767 retval->transport = chunk->transport;
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)
777 struct sctp_chunk *retval;
780 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
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,
791 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
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.
798 * [ABORT back to where the offender came from.]
801 retval->transport = chunk->transport;
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 sctp_chunk *chunk,
810 const struct msghdr *msg)
812 struct sctp_chunk *retval;
813 void *payload = NULL, *payoff;
815 struct iovec *iov = NULL;
820 iovlen = msg->msg_iovlen;
821 paylen = get_user_iov_size(iov, iovlen);
824 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen);
829 /* Put the msg_iov together into payload. */
830 payload = kmalloc(paylen, GFP_ATOMIC);
835 for (; iovlen > 0; --iovlen) {
836 if (copy_from_user(payoff, iov->iov_base,iov->iov_len))
838 payoff += iov->iov_len;
843 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
853 sctp_chunk_free(retval);
859 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
860 struct sctp_chunk *sctp_make_abort_violation(
861 const struct sctp_association *asoc,
862 const struct sctp_chunk *chunk,
866 struct sctp_chunk *retval;
867 struct sctp_paramhdr phdr;
869 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
870 + sizeof(sctp_chunkhdr_t));
874 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, payload, paylen);
876 phdr.type = htons(chunk->chunk_hdr->type);
877 phdr.length = chunk->chunk_hdr->length;
878 sctp_addto_chunk(retval, sizeof(sctp_paramhdr_t), &phdr);
884 /* Make a HEARTBEAT chunk. */
885 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
886 const struct sctp_transport *transport,
887 const void *payload, const size_t paylen)
889 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
895 /* Cast away the 'const', as this is just telling the chunk
896 * what transport it belongs to.
898 retval->transport = (struct sctp_transport *) transport;
899 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
905 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
906 const struct sctp_chunk *chunk,
907 const void *payload, const size_t paylen)
909 struct sctp_chunk *retval;
911 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
915 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
917 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
919 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
920 * HEARTBEAT ACK, * etc.) to the same destination transport
921 * address from which it * received the DATA or control chunk
922 * to which it is replying.
924 * [HBACK back to where the HEARTBEAT came from.]
927 retval->transport = chunk->transport;
933 /* Create an Operation Error chunk with the specified space reserved.
934 * This routine can be used for containing multiple causes in the chunk.
936 static struct sctp_chunk *sctp_make_op_error_space(
937 const struct sctp_association *asoc,
938 const struct sctp_chunk *chunk,
941 struct sctp_chunk *retval;
943 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
944 sizeof(sctp_errhdr_t) + size);
948 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
950 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
951 * HEARTBEAT ACK, etc.) to the same destination transport
952 * address from which it received the DATA or control chunk
953 * to which it is replying.
957 retval->transport = chunk->transport;
963 /* Create an Operation Error chunk. */
964 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
965 const struct sctp_chunk *chunk,
966 __u16 cause_code, const void *payload,
969 struct sctp_chunk *retval;
971 retval = sctp_make_op_error_space(asoc, chunk, paylen);
975 sctp_init_cause(retval, cause_code, payload, paylen);
981 /********************************************************************
982 * 2nd Level Abstractions
983 ********************************************************************/
985 /* Turn an skb into a chunk.
986 * FIXME: Eventually move the structure directly inside the skb->cb[].
988 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
989 const struct sctp_association *asoc,
992 struct sctp_chunk *retval;
994 retval = kmem_cache_alloc(sctp_chunk_cachep, SLAB_ATOMIC);
998 memset(retval, 0, sizeof(struct sctp_chunk));
1001 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1004 INIT_LIST_HEAD(&retval->list);
1006 retval->asoc = (struct sctp_association *)asoc;
1008 retval->has_tsn = 0;
1009 retval->has_ssn = 0;
1010 retval->rtt_in_progress = 0;
1011 retval->sent_at = 0;
1012 retval->singleton = 1;
1013 retval->end_of_packet = 0;
1014 retval->ecn_ce_done = 0;
1015 retval->pdiscard = 0;
1017 /* sctpimpguide-05.txt Section 2.8.2
1018 * M1) Each time a new DATA chunk is transmitted
1019 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1020 * 'TSN.Missing.Report' count will be used to determine missing chunks
1021 * and when to fast retransmit.
1023 retval->tsn_missing_report = 0;
1024 retval->tsn_gap_acked = 0;
1025 retval->fast_retransmit = 0;
1027 /* If this is a fragmented message, track all fragments
1028 * of the message (for SEND_FAILED).
1032 /* Polish the bead hole. */
1033 INIT_LIST_HEAD(&retval->transmitted_list);
1034 INIT_LIST_HEAD(&retval->frag_list);
1035 SCTP_DBG_OBJCNT_INC(chunk);
1036 atomic_set(&retval->refcnt, 1);
1042 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1043 void sctp_init_addrs(struct sctp_chunk *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 struct sctp_chunk *chunk)
1053 /* If we have a known transport, use that. */
1054 if (chunk->transport) {
1055 return &chunk->transport->ipaddr;
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.
1066 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1067 __u8 type, __u8 flags, int paylen)
1069 struct sctp_chunk *retval;
1070 sctp_chunkhdr_t *chunk_hdr;
1071 struct sk_buff *skb;
1074 /* No need to allocate LL here, as this is only a chunk. */
1075 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1080 /* Make room for the chunk header. */
1081 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1082 chunk_hdr->type = type;
1083 chunk_hdr->flags = flags;
1084 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1086 sk = asoc ? asoc->base.sk : NULL;
1087 retval = sctp_chunkify(skb, asoc, sk);
1093 retval->chunk_hdr = chunk_hdr;
1094 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1096 /* Set the skb to the belonging sock for accounting. */
1105 /* Release the memory occupied by a chunk. */
1106 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1108 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1109 dev_kfree_skb(chunk->skb);
1111 SCTP_DBG_OBJCNT_DEC(chunk);
1112 kmem_cache_free(sctp_chunk_cachep, chunk);
1115 /* Possibly, free the chunk. */
1116 void sctp_chunk_free(struct sctp_chunk *chunk)
1118 BUG_ON(!list_empty(&chunk->list));
1119 list_del_init(&chunk->transmitted_list);
1121 /* Release our reference on the message tracker. */
1123 sctp_datamsg_put(chunk->msg);
1125 sctp_chunk_put(chunk);
1128 /* Grab a reference to the chunk. */
1129 void sctp_chunk_hold(struct sctp_chunk *ch)
1131 atomic_inc(&ch->refcnt);
1134 /* Release a reference to the chunk. */
1135 void sctp_chunk_put(struct sctp_chunk *ch)
1137 if (atomic_dec_and_test(&ch->refcnt))
1138 sctp_chunk_destroy(ch);
1141 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1144 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1148 int chunklen = ntohs(chunk->chunk_hdr->length);
1149 int padlen = chunklen % 4;
1151 padding = skb_put(chunk->skb, padlen);
1152 target = skb_put(chunk->skb, len);
1154 memset(padding, 0, padlen);
1155 memcpy(target, data, len);
1157 /* Adjust the chunk length field. */
1158 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1159 chunk->chunk_end = chunk->skb->tail;
1164 /* Append bytes from user space to the end of a chunk. Will panic if
1165 * chunk is not big enough.
1166 * Returns a kernel err value.
1168 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1174 /* Make room in chunk for data. */
1175 target = skb_put(chunk->skb, len);
1177 /* Copy data (whole iovec) into chunk */
1178 if ((err = memcpy_fromiovecend(target, data, off, len)))
1181 /* Adjust the chunk length field. */
1182 chunk->chunk_hdr->length =
1183 htons(ntohs(chunk->chunk_hdr->length) + len);
1184 chunk->chunk_end = chunk->skb->tail;
1190 /* Helper function to assign a TSN if needed. This assumes that both
1191 * the data_hdr and association have already been assigned.
1193 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1201 /* This is the last possible instant to assign a SSN. */
1202 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1205 sid = htons(chunk->subh.data_hdr->stream);
1206 if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1207 ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
1209 ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
1213 chunk->subh.data_hdr->ssn = ssn;
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(struct sctp_chunk *chunk)
1222 if (!chunk->has_tsn) {
1223 /* This is the last possible instant to
1226 chunk->subh.data_hdr->tsn =
1227 htonl(sctp_association_get_next_tsn(chunk->asoc));
1232 /* Create a CLOSED association to use with an incoming packet. */
1233 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1234 struct sctp_chunk *chunk,
1237 struct sctp_association *asoc;
1238 struct sk_buff *skb;
1242 /* Create the bare association. */
1243 scope = sctp_scope(sctp_source(chunk));
1244 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1249 /* Create an entry for the source address of the packet. */
1250 af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
1253 af->from_skb(&asoc->c.peer_addr, skb, 1);
1258 sctp_association_free(asoc);
1262 /* Build a cookie representing asoc.
1263 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1265 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1266 const struct sctp_association *asoc,
1267 const struct sctp_chunk *init_chunk,
1269 const __u8 *raw_addrs, int addrs_len)
1271 sctp_cookie_param_t *retval;
1272 struct sctp_signed_cookie *cookie;
1273 struct scatterlist sg;
1274 int headersize, bodysize;
1275 unsigned int keylen;
1278 headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
1279 bodysize = sizeof(struct sctp_cookie)
1280 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1282 /* Pad out the cookie to a multiple to make the signature
1283 * functions simpler to write.
1285 if (bodysize % SCTP_COOKIE_MULTIPLE)
1286 bodysize += SCTP_COOKIE_MULTIPLE
1287 - (bodysize % SCTP_COOKIE_MULTIPLE);
1288 *cookie_len = headersize + bodysize;
1290 retval = kmalloc(*cookie_len, GFP_ATOMIC);
1297 /* Clear this memory since we are sending this data structure
1298 * out on the network.
1300 memset(retval, 0x00, *cookie_len);
1301 cookie = (struct sctp_signed_cookie *) retval->body;
1303 /* Set up the parameter header. */
1304 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1305 retval->p.length = htons(*cookie_len);
1307 /* Copy the cookie part of the association itself. */
1308 cookie->c = asoc->c;
1309 /* Save the raw address list length in the cookie. */
1310 cookie->c.raw_addr_list_len = addrs_len;
1312 /* Remember PR-SCTP capability. */
1313 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1315 /* Save adaption indication in the cookie. */
1316 cookie->c.adaption_ind = asoc->peer.adaption_ind;
1318 /* Set an expiration time for the cookie. */
1319 do_gettimeofday(&cookie->c.expiration);
1320 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1322 /* Copy the peer's init packet. */
1323 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1324 ntohs(init_chunk->chunk_hdr->length));
1326 /* Copy the raw local address list of the association. */
1327 memcpy((__u8 *)&cookie->c.peer_init[0] +
1328 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1330 if (sctp_sk(ep->base.sk)->hmac) {
1331 /* Sign the message. */
1332 sg.page = virt_to_page(&cookie->c);
1333 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1334 sg.length = bodysize;
1335 keylen = SCTP_SECRET_SIZE;
1336 key = (char *)ep->secret_key[ep->current_key];
1338 sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1339 &sg, 1, cookie->signature);
1346 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1347 struct sctp_association *sctp_unpack_cookie(
1348 const struct sctp_endpoint *ep,
1349 const struct sctp_association *asoc,
1350 struct sctp_chunk *chunk, gfp_t gfp,
1351 int *error, struct sctp_chunk **errp)
1353 struct sctp_association *retval = NULL;
1354 struct sctp_signed_cookie *cookie;
1355 struct sctp_cookie *bear_cookie;
1356 int headersize, bodysize, fixed_size;
1357 __u8 digest[SCTP_SIGNATURE_SIZE];
1358 struct scatterlist sg;
1359 unsigned int keylen, len;
1362 struct sk_buff *skb = chunk->skb;
1365 headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
1366 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1367 fixed_size = headersize + sizeof(struct sctp_cookie);
1369 /* Verify that the chunk looks like it even has a cookie.
1370 * There must be enough room for our cookie and our peer's
1373 len = ntohs(chunk->chunk_hdr->length);
1374 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1377 /* Verify that the cookie has been padded out. */
1378 if (bodysize % SCTP_COOKIE_MULTIPLE)
1381 /* Process the cookie. */
1382 cookie = chunk->subh.cookie_hdr;
1383 bear_cookie = &cookie->c;
1385 if (!sctp_sk(ep->base.sk)->hmac)
1388 /* Check the signature. */
1389 keylen = SCTP_SECRET_SIZE;
1390 sg.page = virt_to_page(bear_cookie);
1391 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1392 sg.length = bodysize;
1393 key = (char *)ep->secret_key[ep->current_key];
1395 memset(digest, 0x00, sizeof(digest));
1396 sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen, &sg,
1399 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1400 /* Try the previous key. */
1401 key = (char *)ep->secret_key[ep->last_key];
1402 memset(digest, 0x00, sizeof(digest));
1403 sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1406 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1407 /* Yikes! Still bad signature! */
1408 *error = -SCTP_IERROR_BAD_SIG;
1414 /* IG Section 2.35.2:
1415 * 3) Compare the port numbers and the verification tag contained
1416 * within the COOKIE ECHO chunk to the actual port numbers and the
1417 * verification tag within the SCTP common header of the received
1418 * packet. If these values do not match the packet MUST be silently
1421 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1422 *error = -SCTP_IERROR_BAD_TAG;
1426 if (ntohs(chunk->sctp_hdr->source) != bear_cookie->peer_addr.v4.sin_port ||
1427 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1428 *error = -SCTP_IERROR_BAD_PORTS;
1432 /* Check to see if the cookie is stale. If there is already
1433 * an association, there is no need to check cookie's expiration
1434 * for init collision case of lost COOKIE ACK.
1436 skb_get_timestamp(skb, &tv);
1437 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1440 * Section 3.3.10.3 Stale Cookie Error (3)
1444 * Stale Cookie Error: Indicates the receipt of a valid State
1445 * Cookie that has expired.
1447 len = ntohs(chunk->chunk_hdr->length);
1448 *errp = sctp_make_op_error_space(asoc, chunk, len);
1450 suseconds_t usecs = (tv.tv_sec -
1451 bear_cookie->expiration.tv_sec) * 1000000L +
1452 tv.tv_usec - bear_cookie->expiration.tv_usec;
1454 usecs = htonl(usecs);
1455 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1456 &usecs, sizeof(usecs));
1457 *error = -SCTP_IERROR_STALE_COOKIE;
1459 *error = -SCTP_IERROR_NOMEM;
1464 /* Make a new base association. */
1465 scope = sctp_scope(sctp_source(chunk));
1466 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1468 *error = -SCTP_IERROR_NOMEM;
1472 /* Set up our peer's port number. */
1473 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1475 /* Populate the association from the cookie. */
1476 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1478 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1480 *error = -SCTP_IERROR_NOMEM;
1484 /* Also, add the destination address. */
1485 if (list_empty(&retval->base.bind_addr.address_list)) {
1486 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1490 retval->next_tsn = retval->c.initial_tsn;
1491 retval->ctsn_ack_point = retval->next_tsn - 1;
1492 retval->addip_serial = retval->c.initial_tsn;
1493 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1494 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1495 retval->peer.adaption_ind = retval->c.adaption_ind;
1497 /* The INIT stuff will be done by the side effects. */
1502 sctp_association_free(retval);
1507 /* Yikes! The packet is either corrupt or deliberately
1510 *error = -SCTP_IERROR_MALFORMED;
1514 /********************************************************************
1515 * 3rd Level Abstractions
1516 ********************************************************************/
1518 struct __sctp_missing {
1521 } __attribute__((packed));
1524 * Report a missing mandatory parameter.
1526 static int sctp_process_missing_param(const struct sctp_association *asoc,
1527 sctp_param_t paramtype,
1528 struct sctp_chunk *chunk,
1529 struct sctp_chunk **errp)
1531 struct __sctp_missing report;
1534 len = WORD_ROUND(sizeof(report));
1536 /* Make an ERROR chunk, preparing enough room for
1537 * returning multiple unknown parameters.
1540 *errp = sctp_make_op_error_space(asoc, chunk, len);
1543 report.num_missing = htonl(1);
1544 report.type = paramtype;
1545 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM,
1546 &report, sizeof(report));
1549 /* Stop processing this chunk. */
1553 /* Report an Invalid Mandatory Parameter. */
1554 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1555 struct sctp_chunk *chunk,
1556 struct sctp_chunk **errp)
1558 /* Invalid Mandatory Parameter Error has no payload. */
1561 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1564 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
1566 /* Stop processing this chunk. */
1570 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1571 struct sctp_paramhdr *param,
1572 const struct sctp_chunk *chunk,
1573 struct sctp_chunk **errp)
1575 char error[] = "The following parameter had invalid length:";
1576 size_t payload_len = WORD_ROUND(sizeof(error)) +
1577 sizeof(sctp_paramhdr_t);
1580 /* Create an error chunk and fill it in with our payload. */
1582 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1585 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION, error,
1587 sctp_addto_chunk(*errp, sizeof(sctp_paramhdr_t), param);
1594 /* Do not attempt to handle the HOST_NAME parm. However, do
1595 * send back an indicator to the peer.
1597 static int sctp_process_hn_param(const struct sctp_association *asoc,
1598 union sctp_params param,
1599 struct sctp_chunk *chunk,
1600 struct sctp_chunk **errp)
1602 __u16 len = ntohs(param.p->length);
1604 /* Make an ERROR chunk. */
1606 *errp = sctp_make_op_error_space(asoc, chunk, len);
1609 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
1612 /* Stop processing this chunk. */
1616 /* RFC 3.2.1 & the Implementers Guide 2.2.
1618 * The Parameter Types are encoded such that the
1619 * highest-order two bits specify the action that must be
1620 * taken if the processing endpoint does not recognize the
1623 * 00 - Stop processing this SCTP chunk and discard it,
1624 * do not process any further chunks within it.
1626 * 01 - Stop processing this SCTP chunk and discard it,
1627 * do not process any further chunks within it, and report
1628 * the unrecognized parameter in an 'Unrecognized
1629 * Parameter Type' (in either an ERROR or in the INIT ACK).
1631 * 10 - Skip this parameter and continue processing.
1633 * 11 - Skip this parameter and continue processing but
1634 * report the unrecognized parameter in an
1635 * 'Unrecognized Parameter Type' (in either an ERROR or in
1639 * 0 - discard the chunk
1640 * 1 - continue with the chunk
1642 static int sctp_process_unk_param(const struct sctp_association *asoc,
1643 union sctp_params param,
1644 struct sctp_chunk *chunk,
1645 struct sctp_chunk **errp)
1649 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1650 case SCTP_PARAM_ACTION_DISCARD:
1653 case SCTP_PARAM_ACTION_DISCARD_ERR:
1655 /* Make an ERROR chunk, preparing enough room for
1656 * returning multiple unknown parameters.
1659 *errp = sctp_make_op_error_space(asoc, chunk,
1660 ntohs(chunk->chunk_hdr->length));
1663 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1665 WORD_ROUND(ntohs(param.p->length)));
1668 case SCTP_PARAM_ACTION_SKIP:
1670 case SCTP_PARAM_ACTION_SKIP_ERR:
1671 /* Make an ERROR chunk, preparing enough room for
1672 * returning multiple unknown parameters.
1675 *errp = sctp_make_op_error_space(asoc, chunk,
1676 ntohs(chunk->chunk_hdr->length));
1679 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1681 WORD_ROUND(ntohs(param.p->length)));
1683 /* If there is no memory for generating the ERROR
1684 * report as specified, an ABORT will be triggered
1685 * to the peer and the association won't be
1699 /* Find unrecognized parameters in the chunk.
1701 * 0 - discard the chunk
1702 * 1 - continue with the chunk
1704 static int sctp_verify_param(const struct sctp_association *asoc,
1705 union sctp_params param,
1707 struct sctp_chunk *chunk,
1708 struct sctp_chunk **err_chunk)
1712 /* FIXME - This routine is not looking at each parameter per the
1713 * chunk type, i.e., unrecognized parameters should be further
1714 * identified based on the chunk id.
1717 switch (param.p->type) {
1718 case SCTP_PARAM_IPV4_ADDRESS:
1719 case SCTP_PARAM_IPV6_ADDRESS:
1720 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1721 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1722 case SCTP_PARAM_STATE_COOKIE:
1723 case SCTP_PARAM_HEARTBEAT_INFO:
1724 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1725 case SCTP_PARAM_ECN_CAPABLE:
1726 case SCTP_PARAM_ADAPTION_LAYER_IND:
1729 case SCTP_PARAM_HOST_NAME_ADDRESS:
1730 /* Tell the peer, we won't support this param. */
1731 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1732 case SCTP_PARAM_FWD_TSN_SUPPORT:
1733 if (sctp_prsctp_enable)
1737 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1738 ntohs(param.p->type), cid);
1739 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1746 /* Verify the INIT packet before we process it. */
1747 int sctp_verify_init(const struct sctp_association *asoc,
1749 sctp_init_chunk_t *peer_init,
1750 struct sctp_chunk *chunk,
1751 struct sctp_chunk **errp)
1753 union sctp_params param;
1756 /* Verify stream values are non-zero. */
1757 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1758 (0 == peer_init->init_hdr.num_inbound_streams)) {
1760 sctp_process_inv_mandatory(asoc, chunk, errp);
1764 /* Check for missing mandatory parameters. */
1765 sctp_walk_params(param, peer_init, init_hdr.params) {
1767 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1770 } /* for (loop through all parameters) */
1772 /* There is a possibility that a parameter length was bad and
1773 * in that case we would have stoped walking the parameters.
1774 * The current param.p would point at the bad one.
1775 * Current consensus on the mailing list is to generate a PROTOCOL
1776 * VIOLATION error. We build the ERROR chunk here and let the normal
1777 * error handling code build and send the packet.
1779 if (param.v < (void*)chunk->chunk_end - sizeof(sctp_paramhdr_t)) {
1780 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1784 /* The only missing mandatory param possible today is
1785 * the state cookie for an INIT-ACK chunk.
1787 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1788 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1793 /* Find unrecognized parameters. */
1795 sctp_walk_params(param, peer_init, init_hdr.params) {
1797 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1798 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1804 } /* for (loop through all parameters) */
1809 /* Unpack the parameters in an INIT packet into an association.
1810 * Returns 0 on failure, else success.
1811 * FIXME: This is an association method.
1813 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1814 const union sctp_addr *peer_addr,
1815 sctp_init_chunk_t *peer_init, gfp_t gfp)
1817 union sctp_params param;
1818 struct sctp_transport *transport;
1819 struct list_head *pos, *temp;
1822 /* We must include the address that the INIT packet came from.
1823 * This is the only address that matters for an INIT packet.
1824 * When processing a COOKIE ECHO, we retrieve the from address
1825 * of the INIT from the cookie.
1828 /* This implementation defaults to making the first transport
1829 * added as the primary transport. The source address seems to
1830 * be a a better choice than any of the embedded addresses.
1833 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
1836 /* Process the initialization parameters. */
1838 sctp_walk_params(param, peer_init, init_hdr.params) {
1840 if (!sctp_process_param(asoc, param, peer_addr, gfp))
1844 /* Walk list of transports, removing transports in the UNKNOWN state. */
1845 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1846 transport = list_entry(pos, struct sctp_transport, transports);
1847 if (transport->state == SCTP_UNKNOWN) {
1848 sctp_assoc_rm_peer(asoc, transport);
1852 /* The fixed INIT headers are always in network byte
1855 asoc->peer.i.init_tag =
1856 ntohl(peer_init->init_hdr.init_tag);
1857 asoc->peer.i.a_rwnd =
1858 ntohl(peer_init->init_hdr.a_rwnd);
1859 asoc->peer.i.num_outbound_streams =
1860 ntohs(peer_init->init_hdr.num_outbound_streams);
1861 asoc->peer.i.num_inbound_streams =
1862 ntohs(peer_init->init_hdr.num_inbound_streams);
1863 asoc->peer.i.initial_tsn =
1864 ntohl(peer_init->init_hdr.initial_tsn);
1866 /* Apply the upper bounds for output streams based on peer's
1867 * number of inbound streams.
1869 if (asoc->c.sinit_num_ostreams >
1870 ntohs(peer_init->init_hdr.num_inbound_streams)) {
1871 asoc->c.sinit_num_ostreams =
1872 ntohs(peer_init->init_hdr.num_inbound_streams);
1875 if (asoc->c.sinit_max_instreams >
1876 ntohs(peer_init->init_hdr.num_outbound_streams)) {
1877 asoc->c.sinit_max_instreams =
1878 ntohs(peer_init->init_hdr.num_outbound_streams);
1881 /* Copy Initiation tag from INIT to VT_peer in cookie. */
1882 asoc->c.peer_vtag = asoc->peer.i.init_tag;
1884 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
1885 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1887 /* Copy cookie in case we need to resend COOKIE-ECHO. */
1888 cookie = asoc->peer.cookie;
1890 asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, gfp);
1891 if (!asoc->peer.cookie)
1893 memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
1896 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1897 * high (for example, implementations MAY use the size of the receiver
1898 * advertised window).
1900 list_for_each(pos, &asoc->peer.transport_addr_list) {
1901 transport = list_entry(pos, struct sctp_transport, transports);
1902 transport->ssthresh = asoc->peer.i.a_rwnd;
1905 /* Set up the TSN tracking pieces. */
1906 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1907 asoc->peer.i.initial_tsn);
1909 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1911 * The stream sequence number in all the streams shall start
1912 * from 0 when the association is established. Also, when the
1913 * stream sequence number reaches the value 65535 the next
1914 * stream sequence number shall be set to 0.
1917 /* Allocate storage for the negotiated streams if it is not a temporary
1924 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1925 asoc->c.sinit_num_ostreams, gfp);
1930 if (unlikely(!idr_pre_get(&sctp_assocs_id, gfp)))
1932 spin_lock_bh(&sctp_assocs_id_lock);
1933 error = idr_get_new_above(&sctp_assocs_id, (void *)asoc, 1,
1935 spin_unlock_bh(&sctp_assocs_id_lock);
1936 if (error == -EAGAIN)
1941 asoc->assoc_id = (sctp_assoc_t) assoc_id;
1944 /* ADDIP Section 4.1 ASCONF Chunk Procedures
1946 * When an endpoint has an ASCONF signaled change to be sent to the
1947 * remote endpoint it should do the following:
1949 * A2) A serial number should be assigned to the Chunk. The serial
1950 * number should be a monotonically increasing number. All serial
1951 * numbers are defined to be initialized at the start of the
1952 * association to the same value as the Initial TSN.
1954 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1958 /* Release the transport structures. */
1959 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1960 transport = list_entry(pos, struct sctp_transport, transports);
1962 sctp_transport_free(transport);
1965 asoc->peer.transport_count = 0;
1972 /* Update asoc with the option described in param.
1974 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1976 * asoc is the association to update.
1977 * param is the variable length parameter to use for update.
1978 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1979 * If the current packet is an INIT we want to minimize the amount of
1980 * work we do. In particular, we should not build transport
1981 * structures for the addresses.
1983 static int sctp_process_param(struct sctp_association *asoc,
1984 union sctp_params param,
1985 const union sctp_addr *peer_addr,
1988 union sctp_addr addr;
1996 /* We maintain all INIT parameters in network byte order all the
1997 * time. This allows us to not worry about whether the parameters
1998 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2000 switch (param.p->type) {
2001 case SCTP_PARAM_IPV6_ADDRESS:
2002 if (PF_INET6 != asoc->base.sk->sk_family)
2005 case SCTP_PARAM_IPV4_ADDRESS:
2006 af = sctp_get_af_specific(param_type2af(param.p->type));
2007 af->from_addr_param(&addr, param.addr, asoc->peer.port, 0);
2008 scope = sctp_scope(peer_addr);
2009 if (sctp_in_scope(&addr, scope))
2010 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_ACTIVE))
2014 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2015 if (!sctp_cookie_preserve_enable)
2018 stale = ntohl(param.life->lifespan_increment);
2020 /* Suggested Cookie Life span increment's unit is msec,
2023 asoc->cookie_life.tv_sec += stale / 1000;
2024 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2027 case SCTP_PARAM_HOST_NAME_ADDRESS:
2028 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2031 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2032 /* Turn off the default values first so we'll know which
2033 * ones are really set by the peer.
2035 asoc->peer.ipv4_address = 0;
2036 asoc->peer.ipv6_address = 0;
2038 /* Cycle through address types; avoid divide by 0. */
2039 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2041 sat /= sizeof(__u16);
2043 for (i = 0; i < sat; ++i) {
2044 switch (param.sat->types[i]) {
2045 case SCTP_PARAM_IPV4_ADDRESS:
2046 asoc->peer.ipv4_address = 1;
2049 case SCTP_PARAM_IPV6_ADDRESS:
2050 asoc->peer.ipv6_address = 1;
2053 case SCTP_PARAM_HOST_NAME_ADDRESS:
2054 asoc->peer.hostname_address = 1;
2057 default: /* Just ignore anything else. */
2063 case SCTP_PARAM_STATE_COOKIE:
2064 asoc->peer.cookie_len =
2065 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2066 asoc->peer.cookie = param.cookie->body;
2069 case SCTP_PARAM_HEARTBEAT_INFO:
2070 /* Would be odd to receive, but it causes no problems. */
2073 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2074 /* Rejected during verify stage. */
2077 case SCTP_PARAM_ECN_CAPABLE:
2078 asoc->peer.ecn_capable = 1;
2081 case SCTP_PARAM_ADAPTION_LAYER_IND:
2082 asoc->peer.adaption_ind = param.aind->adaption_ind;
2085 case SCTP_PARAM_FWD_TSN_SUPPORT:
2086 if (sctp_prsctp_enable) {
2087 asoc->peer.prsctp_capable = 1;
2092 /* Any unrecognized parameters should have been caught
2093 * and handled by sctp_verify_param() which should be
2094 * called prior to this routine. Simply log the error
2097 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2098 ntohs(param.p->type), asoc);
2105 /* Select a new verification tag. */
2106 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2108 /* I believe that this random number generator complies with RFC1750.
2109 * A tag of 0 is reserved for special cases (e.g. INIT).
2114 get_random_bytes(&x, sizeof(__u32));
2120 /* Select an initial TSN to send during startup. */
2121 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2125 get_random_bytes(&retval, sizeof(__u32));
2130 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2132 * 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
2133 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2134 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2138 * | Address Parameter |
2139 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2140 * | ASCONF Parameter #1 |
2141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2146 * | ASCONF Parameter #N |
2147 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2149 * Address Parameter and other parameter will not be wrapped in this function
2151 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2152 union sctp_addr *addr,
2155 sctp_addiphdr_t asconf;
2156 struct sctp_chunk *retval;
2157 int length = sizeof(asconf) + vparam_len;
2158 union sctp_addr_param addrparam;
2160 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2162 addrlen = af->to_addr_param(addr, &addrparam);
2167 /* Create the chunk. */
2168 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2172 asconf.serial = htonl(asoc->addip_serial++);
2174 retval->subh.addip_hdr =
2175 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2176 retval->param_hdr.v =
2177 sctp_addto_chunk(retval, addrlen, &addrparam);
2183 * 3.2.1 Add IP Address
2185 * 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
2186 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2187 * | Type = 0xC001 | Length = Variable |
2188 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2189 * | ASCONF-Request Correlation ID |
2190 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2191 * | Address Parameter |
2192 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2194 * 3.2.2 Delete IP Address
2196 * 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
2197 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2198 * | Type = 0xC002 | Length = Variable |
2199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2200 * | ASCONF-Request Correlation ID |
2201 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2202 * | Address Parameter |
2203 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2206 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2207 union sctp_addr *laddr,
2208 struct sockaddr *addrs,
2212 sctp_addip_param_t param;
2213 struct sctp_chunk *retval;
2214 union sctp_addr_param addr_param;
2215 union sctp_addr *addr;
2218 int paramlen = sizeof(param);
2219 int addr_param_len = 0;
2223 /* Get total length of all the address parameters. */
2225 for (i = 0; i < addrcnt; i++) {
2226 addr = (union sctp_addr *)addr_buf;
2227 af = sctp_get_af_specific(addr->v4.sin_family);
2228 addr_param_len = af->to_addr_param(addr, &addr_param);
2230 totallen += paramlen;
2231 totallen += addr_param_len;
2233 addr_buf += af->sockaddr_len;
2236 /* Create an asconf chunk with the required length. */
2237 retval = sctp_make_asconf(asoc, laddr, totallen);
2241 /* Add the address parameters to the asconf chunk. */
2243 for (i = 0; i < addrcnt; i++) {
2244 addr = (union sctp_addr *)addr_buf;
2245 af = sctp_get_af_specific(addr->v4.sin_family);
2246 addr_param_len = af->to_addr_param(addr, &addr_param);
2247 param.param_hdr.type = flags;
2248 param.param_hdr.length = htons(paramlen + addr_param_len);
2251 sctp_addto_chunk(retval, paramlen, ¶m);
2252 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2254 addr_buf += af->sockaddr_len;
2260 * 3.2.4 Set Primary IP Address
2262 * 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
2263 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2264 * | Type =0xC004 | Length = Variable |
2265 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2266 * | ASCONF-Request Correlation ID |
2267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2268 * | Address Parameter |
2269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2271 * Create an ASCONF chunk with Set Primary IP address parameter.
2273 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2274 union sctp_addr *addr)
2276 sctp_addip_param_t param;
2277 struct sctp_chunk *retval;
2278 int len = sizeof(param);
2279 union sctp_addr_param addrparam;
2281 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2283 addrlen = af->to_addr_param(addr, &addrparam);
2288 /* Create the chunk and make asconf header. */
2289 retval = sctp_make_asconf(asoc, addr, len);
2293 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2294 param.param_hdr.length = htons(len);
2297 sctp_addto_chunk(retval, sizeof(param), ¶m);
2298 sctp_addto_chunk(retval, addrlen, &addrparam);
2303 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2305 * 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
2306 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2307 * | Type = 0x80 | Chunk Flags | Chunk Length |
2308 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2310 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2311 * | ASCONF Parameter Response#1 |
2312 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2316 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2317 * | ASCONF Parameter Response#N |
2318 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2320 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2322 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2323 __u32 serial, int vparam_len)
2325 sctp_addiphdr_t asconf;
2326 struct sctp_chunk *retval;
2327 int length = sizeof(asconf) + vparam_len;
2329 /* Create the chunk. */
2330 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2334 asconf.serial = htonl(serial);
2336 retval->subh.addip_hdr =
2337 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2342 /* Add response parameters to an ASCONF_ACK chunk. */
2343 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __u32 crr_id,
2344 __u16 err_code, sctp_addip_param_t *asconf_param)
2346 sctp_addip_param_t ack_param;
2347 sctp_errhdr_t err_param;
2348 int asconf_param_len = 0;
2349 int err_param_len = 0;
2350 __u16 response_type;
2352 if (SCTP_ERROR_NO_ERROR == err_code) {
2353 response_type = SCTP_PARAM_SUCCESS_REPORT;
2355 response_type = SCTP_PARAM_ERR_CAUSE;
2356 err_param_len = sizeof(err_param);
2359 ntohs(asconf_param->param_hdr.length);
2362 /* Add Success Indication or Error Cause Indication parameter. */
2363 ack_param.param_hdr.type = response_type;
2364 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2367 ack_param.crr_id = crr_id;
2368 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2370 if (SCTP_ERROR_NO_ERROR == err_code)
2373 /* Add Error Cause parameter. */
2374 err_param.cause = err_code;
2375 err_param.length = htons(err_param_len + asconf_param_len);
2376 sctp_addto_chunk(chunk, err_param_len, &err_param);
2378 /* Add the failed TLV copied from ASCONF chunk. */
2380 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2383 /* Process a asconf parameter. */
2384 static __u16 sctp_process_asconf_param(struct sctp_association *asoc,
2385 struct sctp_chunk *asconf,
2386 sctp_addip_param_t *asconf_param)
2388 struct sctp_transport *peer;
2390 union sctp_addr addr;
2391 struct list_head *pos;
2392 union sctp_addr_param *addr_param;
2394 addr_param = (union sctp_addr_param *)
2395 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2397 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2399 return SCTP_ERROR_INV_PARAM;
2401 af->from_addr_param(&addr, addr_param, asoc->peer.port, 0);
2402 switch (asconf_param->param_hdr.type) {
2403 case SCTP_PARAM_ADD_IP:
2404 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2405 * request and does not have the local resources to add this
2406 * new address to the association, it MUST return an Error
2407 * Cause TLV set to the new error code 'Operation Refused
2408 * Due to Resource Shortage'.
2411 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_ACTIVE);
2413 return SCTP_ERROR_RSRC_LOW;
2415 /* Start the heartbeat timer. */
2416 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2417 sctp_transport_hold(peer);
2419 case SCTP_PARAM_DEL_IP:
2420 /* ADDIP 4.3 D7) If a request is received to delete the
2421 * last remaining IP address of a peer endpoint, the receiver
2422 * MUST send an Error Cause TLV with the error cause set to the
2423 * new error code 'Request to Delete Last Remaining IP Address'.
2425 pos = asoc->peer.transport_addr_list.next;
2426 if (pos->next == &asoc->peer.transport_addr_list)
2427 return SCTP_ERROR_DEL_LAST_IP;
2429 /* ADDIP 4.3 D8) If a request is received to delete an IP
2430 * address which is also the source address of the IP packet
2431 * which contained the ASCONF chunk, the receiver MUST reject
2432 * this request. To reject the request the receiver MUST send
2433 * an Error Cause TLV set to the new error code 'Request to
2434 * Delete Source IP Address'
2436 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2437 return SCTP_ERROR_DEL_SRC_IP;
2439 sctp_assoc_del_peer(asoc, &addr);
2441 case SCTP_PARAM_SET_PRIMARY:
2442 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2444 return SCTP_ERROR_INV_PARAM;
2446 sctp_assoc_set_primary(asoc, peer);
2449 return SCTP_ERROR_INV_PARAM;
2453 return SCTP_ERROR_NO_ERROR;
2456 /* Process an incoming ASCONF chunk with the next expected serial no. and
2457 * return an ASCONF_ACK chunk to be sent in response.
2459 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2460 struct sctp_chunk *asconf)
2462 sctp_addiphdr_t *hdr;
2463 union sctp_addr_param *addr_param;
2464 sctp_addip_param_t *asconf_param;
2465 struct sctp_chunk *asconf_ack;
2469 int chunk_len = asconf->skb->len;
2471 int all_param_pass = 1;
2473 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2474 serial = ntohl(hdr->serial);
2476 /* Skip the addiphdr and store a pointer to address parameter. */
2477 length = sizeof(sctp_addiphdr_t);
2478 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2479 chunk_len -= length;
2481 /* Skip the address parameter and store a pointer to the first
2484 length = ntohs(addr_param->v4.param_hdr.length);
2485 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2486 chunk_len -= length;
2488 /* create an ASCONF_ACK chunk.
2489 * Based on the definitions of parameters, we know that the size of
2490 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2493 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2497 /* Process the TLVs contained within the ASCONF chunk. */
2498 while (chunk_len > 0) {
2499 err_code = sctp_process_asconf_param(asoc, asconf,
2502 * If an error response is received for a TLV parameter,
2503 * all TLVs with no response before the failed TLV are
2504 * considered successful if not reported. All TLVs after
2505 * the failed response are considered unsuccessful unless
2506 * a specific success indication is present for the parameter.
2508 if (SCTP_ERROR_NO_ERROR != err_code)
2511 if (!all_param_pass)
2512 sctp_add_asconf_response(asconf_ack,
2513 asconf_param->crr_id, err_code,
2516 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2517 * an IP address sends an 'Out of Resource' in its response, it
2518 * MUST also fail any subsequent add or delete requests bundled
2521 if (SCTP_ERROR_RSRC_LOW == err_code)
2524 /* Move to the next ASCONF param. */
2525 length = ntohs(asconf_param->param_hdr.length);
2526 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2528 chunk_len -= length;
2532 asoc->peer.addip_serial++;
2534 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2535 * after freeing the reference to old asconf ack if any.
2538 if (asoc->addip_last_asconf_ack)
2539 sctp_chunk_free(asoc->addip_last_asconf_ack);
2541 sctp_chunk_hold(asconf_ack);
2542 asoc->addip_last_asconf_ack = asconf_ack;
2548 /* Process a asconf parameter that is successfully acked. */
2549 static int sctp_asconf_param_success(struct sctp_association *asoc,
2550 sctp_addip_param_t *asconf_param)
2553 union sctp_addr addr;
2554 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2555 union sctp_addr_param *addr_param;
2556 struct list_head *pos;
2557 struct sctp_transport *transport;
2560 addr_param = (union sctp_addr_param *)
2561 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2563 /* We have checked the packet before, so we do not check again. */
2564 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2565 af->from_addr_param(&addr, addr_param, bp->port, 0);
2567 switch (asconf_param->param_hdr.type) {
2568 case SCTP_PARAM_ADD_IP:
2569 sctp_local_bh_disable();
2570 sctp_write_lock(&asoc->base.addr_lock);
2571 retval = sctp_add_bind_addr(bp, &addr, GFP_ATOMIC);
2572 sctp_write_unlock(&asoc->base.addr_lock);
2573 sctp_local_bh_enable();
2575 case SCTP_PARAM_DEL_IP:
2576 sctp_local_bh_disable();
2577 sctp_write_lock(&asoc->base.addr_lock);
2578 retval = sctp_del_bind_addr(bp, &addr);
2579 sctp_write_unlock(&asoc->base.addr_lock);
2580 sctp_local_bh_enable();
2581 list_for_each(pos, &asoc->peer.transport_addr_list) {
2582 transport = list_entry(pos, struct sctp_transport,
2584 sctp_transport_route(transport, NULL,
2585 sctp_sk(asoc->base.sk));
2595 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2596 * for the given asconf parameter. If there is no response for this parameter,
2597 * return the error code based on the third argument 'no_err'.
2599 * A7) If an error response is received for a TLV parameter, all TLVs with no
2600 * response before the failed TLV are considered successful if not reported.
2601 * All TLVs after the failed response are considered unsuccessful unless a
2602 * specific success indication is present for the parameter.
2604 static __u16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2605 sctp_addip_param_t *asconf_param,
2608 sctp_addip_param_t *asconf_ack_param;
2609 sctp_errhdr_t *err_param;
2611 int asconf_ack_len = asconf_ack->skb->len;
2615 err_code = SCTP_ERROR_NO_ERROR;
2617 err_code = SCTP_ERROR_REQ_REFUSED;
2619 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2620 * the first asconf_ack parameter.
2622 length = sizeof(sctp_addiphdr_t);
2623 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2625 asconf_ack_len -= length;
2627 while (asconf_ack_len > 0) {
2628 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2629 switch(asconf_ack_param->param_hdr.type) {
2630 case SCTP_PARAM_SUCCESS_REPORT:
2631 return SCTP_ERROR_NO_ERROR;
2632 case SCTP_PARAM_ERR_CAUSE:
2633 length = sizeof(sctp_addip_param_t);
2634 err_param = (sctp_errhdr_t *)
2635 ((void *)asconf_ack_param + length);
2636 asconf_ack_len -= length;
2637 if (asconf_ack_len > 0)
2638 return err_param->cause;
2640 return SCTP_ERROR_INV_PARAM;
2643 return SCTP_ERROR_INV_PARAM;
2647 length = ntohs(asconf_ack_param->param_hdr.length);
2648 asconf_ack_param = (sctp_addip_param_t *)
2649 ((void *)asconf_ack_param + length);
2650 asconf_ack_len -= length;
2656 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2657 int sctp_process_asconf_ack(struct sctp_association *asoc,
2658 struct sctp_chunk *asconf_ack)
2660 struct sctp_chunk *asconf = asoc->addip_last_asconf;
2661 union sctp_addr_param *addr_param;
2662 sctp_addip_param_t *asconf_param;
2664 int asconf_len = asconf->skb->len;
2665 int all_param_pass = 0;
2668 __u16 err_code = SCTP_ERROR_NO_ERROR;
2670 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2671 * a pointer to address parameter.
2673 length = sizeof(sctp_addip_chunk_t);
2674 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2675 asconf_len -= length;
2677 /* Skip the address parameter in the last asconf sent and store a
2678 * pointer to the first asconf paramter.
2680 length = ntohs(addr_param->v4.param_hdr.length);
2681 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2682 asconf_len -= length;
2685 * A8) If there is no response(s) to specific TLV parameter(s), and no
2686 * failures are indicated, then all request(s) are considered
2689 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2692 /* Process the TLVs contained in the last sent ASCONF chunk. */
2693 while (asconf_len > 0) {
2695 err_code = SCTP_ERROR_NO_ERROR;
2697 err_code = sctp_get_asconf_response(asconf_ack,
2700 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2705 case SCTP_ERROR_NO_ERROR:
2706 retval = sctp_asconf_param_success(asoc, asconf_param);
2709 case SCTP_ERROR_RSRC_LOW:
2713 case SCTP_ERROR_INV_PARAM:
2714 /* Disable sending this type of asconf parameter in
2717 asoc->peer.addip_disabled_mask |=
2718 asconf_param->param_hdr.type;
2721 case SCTP_ERROR_REQ_REFUSED:
2722 case SCTP_ERROR_DEL_LAST_IP:
2723 case SCTP_ERROR_DEL_SRC_IP:
2728 /* Skip the processed asconf parameter and move to the next
2731 length = ntohs(asconf_param->param_hdr.length);
2732 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2734 asconf_len -= length;
2737 /* Free the cached last sent asconf chunk. */
2738 sctp_chunk_free(asconf);
2739 asoc->addip_last_asconf = NULL;
2741 /* Send the next asconf chunk from the addip chunk queue. */
2742 if (!list_empty(&asoc->addip_chunk_list)) {
2743 struct list_head *entry = asoc->addip_chunk_list.next;
2744 asconf = list_entry(entry, struct sctp_chunk, list);
2746 list_del_init(entry);
2748 /* Hold the chunk until an ASCONF_ACK is received. */
2749 sctp_chunk_hold(asconf);
2750 if (sctp_primitive_ASCONF(asoc, asconf))
2751 sctp_chunk_free(asconf);
2753 asoc->addip_last_asconf = asconf;
2759 /* Make a FWD TSN chunk. */
2760 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2761 __u32 new_cum_tsn, size_t nstreams,
2762 struct sctp_fwdtsn_skip *skiplist)
2764 struct sctp_chunk *retval = NULL;
2765 struct sctp_fwdtsn_chunk *ftsn_chunk;
2766 struct sctp_fwdtsn_hdr ftsn_hdr;
2767 struct sctp_fwdtsn_skip skip;
2771 hint = (nstreams + 1) * sizeof(__u32);
2773 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2778 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2780 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2781 retval->subh.fwdtsn_hdr =
2782 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2784 for (i = 0; i < nstreams; i++) {
2785 skip.stream = skiplist[i].stream;
2786 skip.ssn = skiplist[i].ssn;
2787 sctp_addto_chunk(retval, sizeof(skip), &skip);