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>
69 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
70 __u8 type, __u8 flags, int paylen);
71 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
72 const struct sctp_association *asoc,
73 const struct sctp_chunk *init_chunk,
75 const __u8 *raw_addrs, int addrs_len);
76 static int sctp_process_param(struct sctp_association *asoc,
77 union sctp_params param,
78 const union sctp_addr *peer_addr,
81 /* What was the inbound interface for this chunk? */
82 int sctp_chunk_iif(const struct sctp_chunk *chunk)
87 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
89 iif = af->skb_iif(chunk->skb);
94 /* RFC 2960 3.3.2 Initiation (INIT) (1)
96 * Note 2: The ECN capable field is reserved for future use of
97 * Explicit Congestion Notification.
99 static const struct sctp_paramhdr ecap_param = {
100 SCTP_PARAM_ECN_CAPABLE,
101 __constant_htons(sizeof(struct sctp_paramhdr)),
103 static const struct sctp_paramhdr prsctp_param = {
104 SCTP_PARAM_FWD_TSN_SUPPORT,
105 __constant_htons(sizeof(struct sctp_paramhdr)),
108 /* A helper to initialize to initialize an op error inside a
109 * provided chunk, as most cause codes will be embedded inside an
112 void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
113 const void *payload, size_t paylen)
119 /* Cause code constants are now defined in network order. */
120 err.cause = cause_code;
121 len = sizeof(sctp_errhdr_t) + paylen;
123 err.length = htons(len);
125 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
126 sctp_addto_chunk(chunk, paylen, payload);
129 /* 3.3.2 Initiation (INIT) (1)
131 * This chunk is used to initiate a SCTP association between two
132 * endpoints. The format of the INIT chunk is shown below:
135 * 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
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | Type = 1 | Chunk Flags | Chunk Length |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * | Advertised Receiver Window Credit (a_rwnd) |
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * | Number of Outbound Streams | Number of Inbound Streams |
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148 * / Optional/Variable-Length Parameters /
150 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
153 * The INIT chunk contains the following parameters. Unless otherwise
154 * noted, each parameter MUST only be included once in the INIT chunk.
156 * Fixed Parameters Status
157 * ----------------------------------------------
158 * Initiate Tag Mandatory
159 * Advertised Receiver Window Credit Mandatory
160 * Number of Outbound Streams Mandatory
161 * Number of Inbound Streams Mandatory
162 * Initial TSN Mandatory
164 * Variable Parameters Status Type Value
165 * -------------------------------------------------------------
166 * IPv4 Address (Note 1) Optional 5
167 * IPv6 Address (Note 1) Optional 6
168 * Cookie Preservative Optional 9
169 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
170 * Host Name Address (Note 3) Optional 11
171 * Supported Address Types (Note 4) Optional 12
173 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
174 const struct sctp_bind_addr *bp,
175 gfp_t gfp, int vparam_len)
178 union sctp_params addrs;
180 struct sctp_chunk *retval = NULL;
181 int num_types, addrs_len = 0;
182 struct sctp_sock *sp;
183 sctp_supported_addrs_param_t sat;
185 sctp_adaptation_ind_param_t aiparam;
187 /* RFC 2960 3.3.2 Initiation (INIT) (1)
189 * Note 1: The INIT chunks can contain multiple addresses that
190 * can be IPv4 and/or IPv6 in any combination.
194 /* Convert the provided bind address list to raw format. */
195 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
197 init.init_tag = htonl(asoc->c.my_vtag);
198 init.a_rwnd = htonl(asoc->rwnd);
199 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
200 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
201 init.initial_tsn = htonl(asoc->c.initial_tsn);
203 /* How many address types are needed? */
204 sp = sctp_sk(asoc->base.sk);
205 num_types = sp->pf->supported_addrs(sp, types);
207 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
208 chunksize += sizeof(ecap_param);
209 if (sctp_prsctp_enable)
210 chunksize += sizeof(prsctp_param);
211 chunksize += sizeof(aiparam);
212 chunksize += vparam_len;
214 /* RFC 2960 3.3.2 Initiation (INIT) (1)
216 * Note 3: An INIT chunk MUST NOT contain more than one Host
217 * Name address parameter. Moreover, the sender of the INIT
218 * MUST NOT combine any other address types with the Host Name
219 * address in the INIT. The receiver of INIT MUST ignore any
220 * other address types if the Host Name address parameter is
221 * present in the received INIT chunk.
223 * PLEASE DO NOT FIXME [This version does not support Host Name.]
226 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
230 retval->subh.init_hdr =
231 sctp_addto_chunk(retval, sizeof(init), &init);
232 retval->param_hdr.v =
233 sctp_addto_chunk(retval, addrs_len, addrs.v);
235 /* RFC 2960 3.3.2 Initiation (INIT) (1)
237 * Note 4: This parameter, when present, specifies all the
238 * address types the sending endpoint can support. The absence
239 * of this parameter indicates that the sending endpoint can
240 * support any address type.
242 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
243 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
244 sctp_addto_chunk(retval, sizeof(sat), &sat);
245 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
247 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
248 if (sctp_prsctp_enable)
249 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
250 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
251 aiparam.param_hdr.length = htons(sizeof(aiparam));
252 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
253 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
259 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
260 const struct sctp_chunk *chunk,
261 gfp_t gfp, int unkparam_len)
263 sctp_inithdr_t initack;
264 struct sctp_chunk *retval;
265 union sctp_params addrs;
267 sctp_cookie_param_t *cookie;
270 sctp_adaptation_ind_param_t aiparam;
274 /* Note: there may be no addresses to embed. */
275 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
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,
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 /* Tell peer that we'll do PR-SCTP only if peer advertised. */
301 if (asoc->peer.prsctp_capable)
302 chunksize += sizeof(prsctp_param);
304 chunksize += sizeof(aiparam);
306 /* Now allocate and fill out the chunk. */
307 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
311 /* Per the advice in RFC 2960 6.4, send this reply to
312 * the source of the INIT packet.
314 retval->transport = chunk->transport;
315 retval->subh.init_hdr =
316 sctp_addto_chunk(retval, sizeof(initack), &initack);
317 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
318 sctp_addto_chunk(retval, cookie_len, cookie);
319 if (asoc->peer.ecn_capable)
320 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
321 if (asoc->peer.prsctp_capable)
322 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
324 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
325 aiparam.param_hdr.length = htons(sizeof(aiparam));
326 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
327 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
329 /* We need to remove the const qualifier at this point. */
330 retval->asoc = (struct sctp_association *) asoc;
332 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
334 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
335 * HEARTBEAT ACK, * etc.) to the same destination transport
336 * address from which it received the DATA or control chunk
337 * to which it is replying.
339 * [INIT ACK back to where the INIT came from.]
342 retval->transport = chunk->transport;
351 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
353 * This chunk is used only during the initialization of an association.
354 * It is sent by the initiator of an association to its peer to complete
355 * the initialization process. This chunk MUST precede any DATA chunk
356 * sent within the association, but MAY be bundled with one or more DATA
357 * chunks in the same packet.
360 * 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
361 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
362 * | Type = 10 |Chunk Flags | Length |
363 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
370 * Set to zero on transmit and ignored on receipt.
372 * Length: 16 bits (unsigned integer)
374 * Set to the size of the chunk in bytes, including the 4 bytes of
375 * the chunk header and the size of the Cookie.
377 * Cookie: variable size
379 * This field must contain the exact cookie received in the
380 * State Cookie parameter from the previous INIT ACK.
382 * An implementation SHOULD make the cookie as small as possible
383 * to insure interoperability.
385 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
386 const struct sctp_chunk *chunk)
388 struct sctp_chunk *retval;
392 cookie = asoc->peer.cookie;
393 cookie_len = asoc->peer.cookie_len;
395 /* Build a cookie echo chunk. */
396 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
399 retval->subh.cookie_hdr =
400 sctp_addto_chunk(retval, cookie_len, cookie);
402 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
404 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
405 * HEARTBEAT ACK, * etc.) to the same destination transport
406 * address from which it * received the DATA or control chunk
407 * to which it is replying.
409 * [COOKIE ECHO back to where the INIT ACK came from.]
412 retval->transport = chunk->transport;
418 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
420 * This chunk is used only during the initialization of an
421 * association. It is used to acknowledge the receipt of a COOKIE
422 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
423 * within the association, but MAY be bundled with one or more DATA
424 * chunks or SACK chunk in the same SCTP packet.
427 * 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
428 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
429 * | Type = 11 |Chunk Flags | Length = 4 |
430 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
432 * Chunk Flags: 8 bits
434 * Set to zero on transmit and ignored on receipt.
436 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
437 const struct sctp_chunk *chunk)
439 struct sctp_chunk *retval;
441 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
443 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
445 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
446 * HEARTBEAT ACK, * etc.) to the same destination transport
447 * address from which it * received the DATA or control chunk
448 * to which it is replying.
450 * [COOKIE ACK back to where the COOKIE ECHO came from.]
453 retval->transport = chunk->transport;
459 * Appendix A: Explicit Congestion Notification:
462 * RFC 2481 details a specific bit for a sender to send in the header of
463 * its next outbound TCP segment to indicate to its peer that it has
464 * reduced its congestion window. This is termed the CWR bit. For
465 * SCTP the same indication is made by including the CWR chunk.
466 * This chunk contains one data element, i.e. the TSN number that
467 * was sent in the ECNE chunk. This element represents the lowest
468 * TSN number in the datagram that was originally marked with the
472 * 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
473 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
474 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
475 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
476 * | Lowest TSN Number |
477 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
479 * Note: The CWR is considered a Control chunk.
481 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
482 const __u32 lowest_tsn,
483 const struct sctp_chunk *chunk)
485 struct sctp_chunk *retval;
488 cwr.lowest_tsn = htonl(lowest_tsn);
489 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
490 sizeof(sctp_cwrhdr_t));
495 retval->subh.ecn_cwr_hdr =
496 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
498 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
500 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
501 * HEARTBEAT ACK, * etc.) to the same destination transport
502 * address from which it * received the DATA or control chunk
503 * to which it is replying.
505 * [Report a reduced congestion window back to where the ECNE
509 retval->transport = chunk->transport;
515 /* Make an ECNE chunk. This is a congestion experienced report. */
516 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
517 const __u32 lowest_tsn)
519 struct sctp_chunk *retval;
522 ecne.lowest_tsn = htonl(lowest_tsn);
523 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
524 sizeof(sctp_ecnehdr_t));
527 retval->subh.ecne_hdr =
528 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
534 /* Make a DATA chunk for the given association from the provided
535 * parameters. However, do not populate the data payload.
537 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
538 const struct sctp_sndrcvinfo *sinfo,
539 int data_len, __u8 flags, __u16 ssn)
541 struct sctp_chunk *retval;
542 struct sctp_datahdr dp;
545 /* We assign the TSN as LATE as possible, not here when
546 * creating the chunk.
549 dp.stream = htons(sinfo->sinfo_stream);
550 dp.ppid = sinfo->sinfo_ppid;
552 /* Set the flags for an unordered send. */
553 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
554 flags |= SCTP_DATA_UNORDERED;
559 chunk_len = sizeof(dp) + data_len;
560 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
564 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
565 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
571 /* Create a selective ackowledgement (SACK) for the given
572 * association. This reports on which TSN's we've seen to date,
573 * including duplicates and gaps.
575 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
577 struct sctp_chunk *retval;
578 struct sctp_sackhdr sack;
581 __u16 num_gabs, num_dup_tsns;
582 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
584 ctsn = sctp_tsnmap_get_ctsn(map);
585 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
587 /* How much room is needed in the chunk? */
588 num_gabs = sctp_tsnmap_num_gabs(map);
589 num_dup_tsns = sctp_tsnmap_num_dups(map);
591 /* Initialize the SACK header. */
592 sack.cum_tsn_ack = htonl(ctsn);
593 sack.a_rwnd = htonl(asoc->a_rwnd);
594 sack.num_gap_ack_blocks = htons(num_gabs);
595 sack.num_dup_tsns = htons(num_dup_tsns);
598 + sizeof(struct sctp_gap_ack_block) * num_gabs
599 + sizeof(__u32) * num_dup_tsns;
601 /* Create the chunk. */
602 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
606 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
608 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
609 * HEARTBEAT ACK, etc.) to the same destination transport
610 * address from which it received the DATA or control chunk to
611 * which it is replying. This rule should also be followed if
612 * the endpoint is bundling DATA chunks together with the
615 * However, when acknowledging multiple DATA chunks received
616 * in packets from different source addresses in a single
617 * SACK, the SACK chunk may be transmitted to one of the
618 * destination transport addresses from which the DATA or
619 * control chunks being acknowledged were received.
621 * [BUG: We do not implement the following paragraph.
622 * Perhaps we should remember the last transport we used for a
623 * SACK and avoid that (if possible) if we have seen any
624 * duplicates. --piggy]
626 * When a receiver of a duplicate DATA chunk sends a SACK to a
627 * multi- homed endpoint it MAY be beneficial to vary the
628 * destination address and not use the source address of the
629 * DATA chunk. The reason being that receiving a duplicate
630 * from a multi-homed endpoint might indicate that the return
631 * path (as specified in the source address of the DATA chunk)
632 * for the SACK is broken.
634 * [Send to the address from which we last received a DATA chunk.]
636 retval->transport = asoc->peer.last_data_from;
638 retval->subh.sack_hdr =
639 sctp_addto_chunk(retval, sizeof(sack), &sack);
641 /* Add the gap ack block information. */
643 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
644 sctp_tsnmap_get_gabs(map));
646 /* Add the duplicate TSN information. */
648 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
649 sctp_tsnmap_get_dups(map));
655 /* Make a SHUTDOWN chunk. */
656 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
657 const struct sctp_chunk *chunk)
659 struct sctp_chunk *retval;
660 sctp_shutdownhdr_t shut;
663 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
664 shut.cum_tsn_ack = htonl(ctsn);
666 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
667 sizeof(sctp_shutdownhdr_t));
671 retval->subh.shutdown_hdr =
672 sctp_addto_chunk(retval, sizeof(shut), &shut);
675 retval->transport = chunk->transport;
680 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
681 const struct sctp_chunk *chunk)
683 struct sctp_chunk *retval;
685 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
687 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
689 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
690 * HEARTBEAT ACK, * etc.) to the same destination transport
691 * address from which it * received the DATA or control chunk
692 * to which it is replying.
694 * [ACK back to where the SHUTDOWN came from.]
697 retval->transport = chunk->transport;
702 struct sctp_chunk *sctp_make_shutdown_complete(
703 const struct sctp_association *asoc,
704 const struct sctp_chunk *chunk)
706 struct sctp_chunk *retval;
709 /* Set the T-bit if we have no association (vtag will be
712 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
714 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
716 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
718 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
719 * HEARTBEAT ACK, * etc.) to the same destination transport
720 * address from which it * received the DATA or control chunk
721 * to which it is replying.
723 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
727 retval->transport = chunk->transport;
732 /* Create an ABORT. Note that we set the T bit if we have no
733 * association, except when responding to an INIT (sctpimpguide 2.41).
735 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
736 const struct sctp_chunk *chunk,
739 struct sctp_chunk *retval;
742 /* Set the T-bit if we have no association and 'chunk' is not
743 * an INIT (vtag will be reflected).
746 if (chunk && chunk->chunk_hdr &&
747 chunk->chunk_hdr->type == SCTP_CID_INIT)
750 flags = SCTP_CHUNK_FLAG_T;
753 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
755 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
757 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
758 * HEARTBEAT ACK, * etc.) to the same destination transport
759 * address from which it * received the DATA or control chunk
760 * to which it is replying.
762 * [ABORT back to where the offender came from.]
765 retval->transport = chunk->transport;
770 /* Helper to create ABORT with a NO_USER_DATA error. */
771 struct sctp_chunk *sctp_make_abort_no_data(
772 const struct sctp_association *asoc,
773 const struct sctp_chunk *chunk, __u32 tsn)
775 struct sctp_chunk *retval;
778 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
784 /* Put the tsn back into network byte order. */
785 payload = htonl(tsn);
786 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
789 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
791 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
792 * HEARTBEAT ACK, * etc.) to the same destination transport
793 * address from which it * received the DATA or control chunk
794 * to which it is replying.
796 * [ABORT back to where the offender came from.]
799 retval->transport = chunk->transport;
805 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
806 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
807 const struct msghdr *msg,
810 struct sctp_chunk *retval;
811 void *payload = NULL;
814 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
819 /* Put the msg_iov together into payload. */
820 payload = kmalloc(paylen, GFP_KERNEL);
824 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
829 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
839 sctp_chunk_free(retval);
845 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
846 struct sctp_chunk *sctp_make_abort_violation(
847 const struct sctp_association *asoc,
848 const struct sctp_chunk *chunk,
852 struct sctp_chunk *retval;
853 struct sctp_paramhdr phdr;
855 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
856 + sizeof(sctp_chunkhdr_t));
860 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, payload, paylen);
862 phdr.type = htons(chunk->chunk_hdr->type);
863 phdr.length = chunk->chunk_hdr->length;
864 sctp_addto_chunk(retval, sizeof(sctp_paramhdr_t), &phdr);
870 /* Make a HEARTBEAT chunk. */
871 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
872 const struct sctp_transport *transport,
873 const void *payload, const size_t paylen)
875 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
881 /* Cast away the 'const', as this is just telling the chunk
882 * what transport it belongs to.
884 retval->transport = (struct sctp_transport *) transport;
885 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
891 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
892 const struct sctp_chunk *chunk,
893 const void *payload, const size_t paylen)
895 struct sctp_chunk *retval;
897 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
901 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
903 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
905 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
906 * HEARTBEAT ACK, * etc.) to the same destination transport
907 * address from which it * received the DATA or control chunk
908 * to which it is replying.
910 * [HBACK back to where the HEARTBEAT came from.]
913 retval->transport = chunk->transport;
919 /* Create an Operation Error chunk with the specified space reserved.
920 * This routine can be used for containing multiple causes in the chunk.
922 static struct sctp_chunk *sctp_make_op_error_space(
923 const struct sctp_association *asoc,
924 const struct sctp_chunk *chunk,
927 struct sctp_chunk *retval;
929 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
930 sizeof(sctp_errhdr_t) + size);
934 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
936 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
937 * HEARTBEAT ACK, etc.) to the same destination transport
938 * address from which it received the DATA or control chunk
939 * to which it is replying.
943 retval->transport = chunk->transport;
949 /* Create an Operation Error chunk. */
950 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
951 const struct sctp_chunk *chunk,
952 __be16 cause_code, const void *payload,
955 struct sctp_chunk *retval;
957 retval = sctp_make_op_error_space(asoc, chunk, paylen);
961 sctp_init_cause(retval, cause_code, payload, paylen);
967 /********************************************************************
968 * 2nd Level Abstractions
969 ********************************************************************/
971 /* Turn an skb into a chunk.
972 * FIXME: Eventually move the structure directly inside the skb->cb[].
974 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
975 const struct sctp_association *asoc,
978 struct sctp_chunk *retval;
980 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
986 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
989 INIT_LIST_HEAD(&retval->list);
991 retval->asoc = (struct sctp_association *)asoc;
995 retval->rtt_in_progress = 0;
997 retval->singleton = 1;
998 retval->end_of_packet = 0;
999 retval->ecn_ce_done = 0;
1000 retval->pdiscard = 0;
1002 /* sctpimpguide-05.txt Section 2.8.2
1003 * M1) Each time a new DATA chunk is transmitted
1004 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1005 * 'TSN.Missing.Report' count will be used to determine missing chunks
1006 * and when to fast retransmit.
1008 retval->tsn_missing_report = 0;
1009 retval->tsn_gap_acked = 0;
1010 retval->fast_retransmit = 0;
1012 /* If this is a fragmented message, track all fragments
1013 * of the message (for SEND_FAILED).
1017 /* Polish the bead hole. */
1018 INIT_LIST_HEAD(&retval->transmitted_list);
1019 INIT_LIST_HEAD(&retval->frag_list);
1020 SCTP_DBG_OBJCNT_INC(chunk);
1021 atomic_set(&retval->refcnt, 1);
1027 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1028 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1029 union sctp_addr *dest)
1031 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1032 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1035 /* Extract the source address from a chunk. */
1036 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1038 /* If we have a known transport, use that. */
1039 if (chunk->transport) {
1040 return &chunk->transport->ipaddr;
1042 /* Otherwise, extract it from the IP header. */
1043 return &chunk->source;
1047 /* Create a new chunk, setting the type and flags headers from the
1048 * arguments, reserving enough space for a 'paylen' byte payload.
1051 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1052 __u8 type, __u8 flags, int paylen)
1054 struct sctp_chunk *retval;
1055 sctp_chunkhdr_t *chunk_hdr;
1056 struct sk_buff *skb;
1059 /* No need to allocate LL here, as this is only a chunk. */
1060 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1065 /* Make room for the chunk header. */
1066 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1067 chunk_hdr->type = type;
1068 chunk_hdr->flags = flags;
1069 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1071 sk = asoc ? asoc->base.sk : NULL;
1072 retval = sctp_chunkify(skb, asoc, sk);
1078 retval->chunk_hdr = chunk_hdr;
1079 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1081 /* Set the skb to the belonging sock for accounting. */
1090 /* Release the memory occupied by a chunk. */
1091 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1093 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1094 dev_kfree_skb(chunk->skb);
1096 SCTP_DBG_OBJCNT_DEC(chunk);
1097 kmem_cache_free(sctp_chunk_cachep, chunk);
1100 /* Possibly, free the chunk. */
1101 void sctp_chunk_free(struct sctp_chunk *chunk)
1103 BUG_ON(!list_empty(&chunk->list));
1104 list_del_init(&chunk->transmitted_list);
1106 /* Release our reference on the message tracker. */
1108 sctp_datamsg_put(chunk->msg);
1110 sctp_chunk_put(chunk);
1113 /* Grab a reference to the chunk. */
1114 void sctp_chunk_hold(struct sctp_chunk *ch)
1116 atomic_inc(&ch->refcnt);
1119 /* Release a reference to the chunk. */
1120 void sctp_chunk_put(struct sctp_chunk *ch)
1122 if (atomic_dec_and_test(&ch->refcnt))
1123 sctp_chunk_destroy(ch);
1126 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1129 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1133 int chunklen = ntohs(chunk->chunk_hdr->length);
1134 int padlen = chunklen % 4;
1136 padding = skb_put(chunk->skb, padlen);
1137 target = skb_put(chunk->skb, len);
1139 memset(padding, 0, padlen);
1140 memcpy(target, data, len);
1142 /* Adjust the chunk length field. */
1143 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1144 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1149 /* Append bytes from user space to the end of a chunk. Will panic if
1150 * chunk is not big enough.
1151 * Returns a kernel err value.
1153 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1159 /* Make room in chunk for data. */
1160 target = skb_put(chunk->skb, len);
1162 /* Copy data (whole iovec) into chunk */
1163 if ((err = memcpy_fromiovecend(target, data, off, len)))
1166 /* Adjust the chunk length field. */
1167 chunk->chunk_hdr->length =
1168 htons(ntohs(chunk->chunk_hdr->length) + len);
1169 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1175 /* Helper function to assign a TSN if needed. This assumes that both
1176 * the data_hdr and association have already been assigned.
1178 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1186 /* This is the last possible instant to assign a SSN. */
1187 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1190 sid = ntohs(chunk->subh.data_hdr->stream);
1191 if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1192 ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
1194 ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
1197 chunk->subh.data_hdr->ssn = htons(ssn);
1201 /* Helper function to assign a TSN if needed. This assumes that both
1202 * the data_hdr and association have already been assigned.
1204 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1206 if (!chunk->has_tsn) {
1207 /* This is the last possible instant to
1210 chunk->subh.data_hdr->tsn =
1211 htonl(sctp_association_get_next_tsn(chunk->asoc));
1216 /* Create a CLOSED association to use with an incoming packet. */
1217 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1218 struct sctp_chunk *chunk,
1221 struct sctp_association *asoc;
1222 struct sk_buff *skb;
1226 /* Create the bare association. */
1227 scope = sctp_scope(sctp_source(chunk));
1228 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1233 /* Create an entry for the source address of the packet. */
1234 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1237 af->from_skb(&asoc->c.peer_addr, skb, 1);
1242 sctp_association_free(asoc);
1246 /* Build a cookie representing asoc.
1247 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1249 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1250 const struct sctp_association *asoc,
1251 const struct sctp_chunk *init_chunk,
1253 const __u8 *raw_addrs, int addrs_len)
1255 sctp_cookie_param_t *retval;
1256 struct sctp_signed_cookie *cookie;
1257 struct scatterlist sg;
1258 int headersize, bodysize;
1259 unsigned int keylen;
1262 /* Header size is static data prior to the actual cookie, including
1265 headersize = sizeof(sctp_paramhdr_t) +
1266 (sizeof(struct sctp_signed_cookie) -
1267 sizeof(struct sctp_cookie));
1268 bodysize = sizeof(struct sctp_cookie)
1269 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1271 /* Pad out the cookie to a multiple to make the signature
1272 * functions simpler to write.
1274 if (bodysize % SCTP_COOKIE_MULTIPLE)
1275 bodysize += SCTP_COOKIE_MULTIPLE
1276 - (bodysize % SCTP_COOKIE_MULTIPLE);
1277 *cookie_len = headersize + bodysize;
1279 /* Clear this memory since we are sending this data structure
1280 * out on the network.
1282 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1286 cookie = (struct sctp_signed_cookie *) retval->body;
1288 /* Set up the parameter header. */
1289 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1290 retval->p.length = htons(*cookie_len);
1292 /* Copy the cookie part of the association itself. */
1293 cookie->c = asoc->c;
1294 /* Save the raw address list length in the cookie. */
1295 cookie->c.raw_addr_list_len = addrs_len;
1297 /* Remember PR-SCTP capability. */
1298 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1300 /* Save adaptation indication in the cookie. */
1301 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1303 /* Set an expiration time for the cookie. */
1304 do_gettimeofday(&cookie->c.expiration);
1305 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1307 /* Copy the peer's init packet. */
1308 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1309 ntohs(init_chunk->chunk_hdr->length));
1311 /* Copy the raw local address list of the association. */
1312 memcpy((__u8 *)&cookie->c.peer_init[0] +
1313 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1315 if (sctp_sk(ep->base.sk)->hmac) {
1316 struct hash_desc desc;
1318 /* Sign the message. */
1319 sg.page = virt_to_page(&cookie->c);
1320 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1321 sg.length = bodysize;
1322 keylen = SCTP_SECRET_SIZE;
1323 key = (char *)ep->secret_key[ep->current_key];
1324 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1327 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1328 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1341 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1342 struct sctp_association *sctp_unpack_cookie(
1343 const struct sctp_endpoint *ep,
1344 const struct sctp_association *asoc,
1345 struct sctp_chunk *chunk, gfp_t gfp,
1346 int *error, struct sctp_chunk **errp)
1348 struct sctp_association *retval = NULL;
1349 struct sctp_signed_cookie *cookie;
1350 struct sctp_cookie *bear_cookie;
1351 int headersize, bodysize, fixed_size;
1352 __u8 *digest = ep->digest;
1353 struct scatterlist sg;
1354 unsigned int keylen, len;
1357 struct sk_buff *skb = chunk->skb;
1359 struct hash_desc desc;
1361 /* Header size is static data prior to the actual cookie, including
1364 headersize = sizeof(sctp_chunkhdr_t) +
1365 (sizeof(struct sctp_signed_cookie) -
1366 sizeof(struct sctp_cookie));
1367 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1368 fixed_size = headersize + sizeof(struct sctp_cookie);
1370 /* Verify that the chunk looks like it even has a cookie.
1371 * There must be enough room for our cookie and our peer's
1374 len = ntohs(chunk->chunk_hdr->length);
1375 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1378 /* Verify that the cookie has been padded out. */
1379 if (bodysize % SCTP_COOKIE_MULTIPLE)
1382 /* Process the cookie. */
1383 cookie = chunk->subh.cookie_hdr;
1384 bear_cookie = &cookie->c;
1386 if (!sctp_sk(ep->base.sk)->hmac)
1389 /* Check the signature. */
1390 keylen = SCTP_SECRET_SIZE;
1391 sg.page = virt_to_page(bear_cookie);
1392 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1393 sg.length = bodysize;
1394 key = (char *)ep->secret_key[ep->current_key];
1395 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1398 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1399 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1400 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1401 *error = -SCTP_IERROR_NOMEM;
1405 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1406 /* Try the previous key. */
1407 key = (char *)ep->secret_key[ep->last_key];
1408 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1409 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1410 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1411 *error = -SCTP_IERROR_NOMEM;
1415 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1416 /* Yikes! Still bad signature! */
1417 *error = -SCTP_IERROR_BAD_SIG;
1423 /* IG Section 2.35.2:
1424 * 3) Compare the port numbers and the verification tag contained
1425 * within the COOKIE ECHO chunk to the actual port numbers and the
1426 * verification tag within the SCTP common header of the received
1427 * packet. If these values do not match the packet MUST be silently
1430 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1431 *error = -SCTP_IERROR_BAD_TAG;
1435 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1436 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1437 *error = -SCTP_IERROR_BAD_PORTS;
1441 /* Check to see if the cookie is stale. If there is already
1442 * an association, there is no need to check cookie's expiration
1443 * for init collision case of lost COOKIE ACK.
1444 * If skb has been timestamped, then use the stamp, otherwise
1445 * use current time. This introduces a small possibility that
1446 * that a cookie may be considered expired, but his would only slow
1447 * down the new association establishment instead of every packet.
1449 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1450 skb_get_timestamp(skb, &tv);
1452 do_gettimeofday(&tv);
1454 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1456 * Section 3.3.10.3 Stale Cookie Error (3)
1460 * Stale Cookie Error: Indicates the receipt of a valid State
1461 * Cookie that has expired.
1463 len = ntohs(chunk->chunk_hdr->length);
1464 *errp = sctp_make_op_error_space(asoc, chunk, len);
1466 suseconds_t usecs = (tv.tv_sec -
1467 bear_cookie->expiration.tv_sec) * 1000000L +
1468 tv.tv_usec - bear_cookie->expiration.tv_usec;
1469 __be32 n = htonl(usecs);
1471 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1473 *error = -SCTP_IERROR_STALE_COOKIE;
1475 *error = -SCTP_IERROR_NOMEM;
1480 /* Make a new base association. */
1481 scope = sctp_scope(sctp_source(chunk));
1482 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1484 *error = -SCTP_IERROR_NOMEM;
1488 /* Set up our peer's port number. */
1489 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1491 /* Populate the association from the cookie. */
1492 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1494 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1496 *error = -SCTP_IERROR_NOMEM;
1500 /* Also, add the destination address. */
1501 if (list_empty(&retval->base.bind_addr.address_list)) {
1502 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
1506 retval->next_tsn = retval->c.initial_tsn;
1507 retval->ctsn_ack_point = retval->next_tsn - 1;
1508 retval->addip_serial = retval->c.initial_tsn;
1509 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1510 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1511 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1513 /* The INIT stuff will be done by the side effects. */
1518 sctp_association_free(retval);
1523 /* Yikes! The packet is either corrupt or deliberately
1526 *error = -SCTP_IERROR_MALFORMED;
1530 /********************************************************************
1531 * 3rd Level Abstractions
1532 ********************************************************************/
1534 struct __sctp_missing {
1537 } __attribute__((packed));
1540 * Report a missing mandatory parameter.
1542 static int sctp_process_missing_param(const struct sctp_association *asoc,
1543 sctp_param_t paramtype,
1544 struct sctp_chunk *chunk,
1545 struct sctp_chunk **errp)
1547 struct __sctp_missing report;
1550 len = WORD_ROUND(sizeof(report));
1552 /* Make an ERROR chunk, preparing enough room for
1553 * returning multiple unknown parameters.
1556 *errp = sctp_make_op_error_space(asoc, chunk, len);
1559 report.num_missing = htonl(1);
1560 report.type = paramtype;
1561 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1562 &report, sizeof(report));
1565 /* Stop processing this chunk. */
1569 /* Report an Invalid Mandatory Parameter. */
1570 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1571 struct sctp_chunk *chunk,
1572 struct sctp_chunk **errp)
1574 /* Invalid Mandatory Parameter Error has no payload. */
1577 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1580 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
1582 /* Stop processing this chunk. */
1586 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1587 struct sctp_paramhdr *param,
1588 const struct sctp_chunk *chunk,
1589 struct sctp_chunk **errp)
1591 char error[] = "The following parameter had invalid length:";
1592 size_t payload_len = WORD_ROUND(sizeof(error)) +
1593 sizeof(sctp_paramhdr_t);
1596 /* Create an error chunk and fill it in with our payload. */
1598 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1601 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION, error,
1603 sctp_addto_chunk(*errp, sizeof(sctp_paramhdr_t), param);
1610 /* Do not attempt to handle the HOST_NAME parm. However, do
1611 * send back an indicator to the peer.
1613 static int sctp_process_hn_param(const struct sctp_association *asoc,
1614 union sctp_params param,
1615 struct sctp_chunk *chunk,
1616 struct sctp_chunk **errp)
1618 __u16 len = ntohs(param.p->length);
1620 /* Make an ERROR chunk. */
1622 *errp = sctp_make_op_error_space(asoc, chunk, len);
1625 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
1628 /* Stop processing this chunk. */
1632 /* RFC 3.2.1 & the Implementers Guide 2.2.
1634 * The Parameter Types are encoded such that the
1635 * highest-order two bits specify the action that must be
1636 * taken if the processing endpoint does not recognize the
1639 * 00 - Stop processing this SCTP chunk and discard it,
1640 * do not process any further chunks within it.
1642 * 01 - Stop processing this SCTP chunk and discard it,
1643 * do not process any further chunks within it, and report
1644 * the unrecognized parameter in an 'Unrecognized
1645 * Parameter Type' (in either an ERROR or in the INIT ACK).
1647 * 10 - Skip this parameter and continue processing.
1649 * 11 - Skip this parameter and continue processing but
1650 * report the unrecognized parameter in an
1651 * 'Unrecognized Parameter Type' (in either an ERROR or in
1655 * 0 - discard the chunk
1656 * 1 - continue with the chunk
1658 static int sctp_process_unk_param(const struct sctp_association *asoc,
1659 union sctp_params param,
1660 struct sctp_chunk *chunk,
1661 struct sctp_chunk **errp)
1665 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1666 case SCTP_PARAM_ACTION_DISCARD:
1669 case SCTP_PARAM_ACTION_DISCARD_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)));
1684 case SCTP_PARAM_ACTION_SKIP:
1686 case SCTP_PARAM_ACTION_SKIP_ERR:
1687 /* Make an ERROR chunk, preparing enough room for
1688 * returning multiple unknown parameters.
1691 *errp = sctp_make_op_error_space(asoc, chunk,
1692 ntohs(chunk->chunk_hdr->length));
1695 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1697 WORD_ROUND(ntohs(param.p->length)));
1699 /* If there is no memory for generating the ERROR
1700 * report as specified, an ABORT will be triggered
1701 * to the peer and the association won't be
1715 /* Find unrecognized parameters in the chunk.
1717 * 0 - discard the chunk
1718 * 1 - continue with the chunk
1720 static int sctp_verify_param(const struct sctp_association *asoc,
1721 union sctp_params param,
1723 struct sctp_chunk *chunk,
1724 struct sctp_chunk **err_chunk)
1728 /* FIXME - This routine is not looking at each parameter per the
1729 * chunk type, i.e., unrecognized parameters should be further
1730 * identified based on the chunk id.
1733 switch (param.p->type) {
1734 case SCTP_PARAM_IPV4_ADDRESS:
1735 case SCTP_PARAM_IPV6_ADDRESS:
1736 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1737 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1738 case SCTP_PARAM_STATE_COOKIE:
1739 case SCTP_PARAM_HEARTBEAT_INFO:
1740 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1741 case SCTP_PARAM_ECN_CAPABLE:
1742 case SCTP_PARAM_ADAPTATION_LAYER_IND:
1745 case SCTP_PARAM_HOST_NAME_ADDRESS:
1746 /* Tell the peer, we won't support this param. */
1747 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1748 case SCTP_PARAM_FWD_TSN_SUPPORT:
1749 if (sctp_prsctp_enable)
1753 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1754 ntohs(param.p->type), cid);
1755 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1762 /* Verify the INIT packet before we process it. */
1763 int sctp_verify_init(const struct sctp_association *asoc,
1765 sctp_init_chunk_t *peer_init,
1766 struct sctp_chunk *chunk,
1767 struct sctp_chunk **errp)
1769 union sctp_params param;
1772 /* Verify stream values are non-zero. */
1773 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1774 (0 == peer_init->init_hdr.num_inbound_streams) ||
1775 (0 == peer_init->init_hdr.init_tag) ||
1776 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
1778 sctp_process_inv_mandatory(asoc, chunk, errp);
1782 /* Check for missing mandatory parameters. */
1783 sctp_walk_params(param, peer_init, init_hdr.params) {
1785 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1788 } /* for (loop through all parameters) */
1790 /* There is a possibility that a parameter length was bad and
1791 * in that case we would have stoped walking the parameters.
1792 * The current param.p would point at the bad one.
1793 * Current consensus on the mailing list is to generate a PROTOCOL
1794 * VIOLATION error. We build the ERROR chunk here and let the normal
1795 * error handling code build and send the packet.
1797 if (param.v < (void*)chunk->chunk_end - sizeof(sctp_paramhdr_t)) {
1798 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1802 /* The only missing mandatory param possible today is
1803 * the state cookie for an INIT-ACK chunk.
1805 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1806 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1811 /* Find unrecognized parameters. */
1813 sctp_walk_params(param, peer_init, init_hdr.params) {
1815 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1816 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1822 } /* for (loop through all parameters) */
1827 /* Unpack the parameters in an INIT packet into an association.
1828 * Returns 0 on failure, else success.
1829 * FIXME: This is an association method.
1831 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1832 const union sctp_addr *peer_addr,
1833 sctp_init_chunk_t *peer_init, gfp_t gfp)
1835 union sctp_params param;
1836 struct sctp_transport *transport;
1837 struct list_head *pos, *temp;
1840 /* We must include the address that the INIT packet came from.
1841 * This is the only address that matters for an INIT packet.
1842 * When processing a COOKIE ECHO, we retrieve the from address
1843 * of the INIT from the cookie.
1846 /* This implementation defaults to making the first transport
1847 * added as the primary transport. The source address seems to
1848 * be a a better choice than any of the embedded addresses.
1851 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
1855 /* Process the initialization parameters. */
1857 sctp_walk_params(param, peer_init, init_hdr.params) {
1859 if (!sctp_process_param(asoc, param, peer_addr, gfp))
1863 /* Walk list of transports, removing transports in the UNKNOWN state. */
1864 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1865 transport = list_entry(pos, struct sctp_transport, transports);
1866 if (transport->state == SCTP_UNKNOWN) {
1867 sctp_assoc_rm_peer(asoc, transport);
1871 /* The fixed INIT headers are always in network byte
1874 asoc->peer.i.init_tag =
1875 ntohl(peer_init->init_hdr.init_tag);
1876 asoc->peer.i.a_rwnd =
1877 ntohl(peer_init->init_hdr.a_rwnd);
1878 asoc->peer.i.num_outbound_streams =
1879 ntohs(peer_init->init_hdr.num_outbound_streams);
1880 asoc->peer.i.num_inbound_streams =
1881 ntohs(peer_init->init_hdr.num_inbound_streams);
1882 asoc->peer.i.initial_tsn =
1883 ntohl(peer_init->init_hdr.initial_tsn);
1885 /* Apply the upper bounds for output streams based on peer's
1886 * number of inbound streams.
1888 if (asoc->c.sinit_num_ostreams >
1889 ntohs(peer_init->init_hdr.num_inbound_streams)) {
1890 asoc->c.sinit_num_ostreams =
1891 ntohs(peer_init->init_hdr.num_inbound_streams);
1894 if (asoc->c.sinit_max_instreams >
1895 ntohs(peer_init->init_hdr.num_outbound_streams)) {
1896 asoc->c.sinit_max_instreams =
1897 ntohs(peer_init->init_hdr.num_outbound_streams);
1900 /* Copy Initiation tag from INIT to VT_peer in cookie. */
1901 asoc->c.peer_vtag = asoc->peer.i.init_tag;
1903 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
1904 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1906 /* Copy cookie in case we need to resend COOKIE-ECHO. */
1907 cookie = asoc->peer.cookie;
1909 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
1910 if (!asoc->peer.cookie)
1914 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1915 * high (for example, implementations MAY use the size of the receiver
1916 * advertised window).
1918 list_for_each(pos, &asoc->peer.transport_addr_list) {
1919 transport = list_entry(pos, struct sctp_transport, transports);
1920 transport->ssthresh = asoc->peer.i.a_rwnd;
1923 /* Set up the TSN tracking pieces. */
1924 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1925 asoc->peer.i.initial_tsn);
1927 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1929 * The stream sequence number in all the streams shall start
1930 * from 0 when the association is established. Also, when the
1931 * stream sequence number reaches the value 65535 the next
1932 * stream sequence number shall be set to 0.
1935 /* Allocate storage for the negotiated streams if it is not a temporary
1941 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1942 asoc->c.sinit_num_ostreams, gfp);
1946 error = sctp_assoc_set_id(asoc, gfp);
1951 /* ADDIP Section 4.1 ASCONF Chunk Procedures
1953 * When an endpoint has an ASCONF signaled change to be sent to the
1954 * remote endpoint it should do the following:
1956 * A2) A serial number should be assigned to the Chunk. The serial
1957 * number should be a monotonically increasing number. All serial
1958 * numbers are defined to be initialized at the start of the
1959 * association to the same value as the Initial TSN.
1961 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1965 /* Release the transport structures. */
1966 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1967 transport = list_entry(pos, struct sctp_transport, transports);
1969 sctp_transport_free(transport);
1972 asoc->peer.transport_count = 0;
1979 /* Update asoc with the option described in param.
1981 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1983 * asoc is the association to update.
1984 * param is the variable length parameter to use for update.
1985 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1986 * If the current packet is an INIT we want to minimize the amount of
1987 * work we do. In particular, we should not build transport
1988 * structures for the addresses.
1990 static int sctp_process_param(struct sctp_association *asoc,
1991 union sctp_params param,
1992 const union sctp_addr *peer_addr,
1995 union sctp_addr addr;
2003 /* We maintain all INIT parameters in network byte order all the
2004 * time. This allows us to not worry about whether the parameters
2005 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2007 switch (param.p->type) {
2008 case SCTP_PARAM_IPV6_ADDRESS:
2009 if (PF_INET6 != asoc->base.sk->sk_family)
2012 case SCTP_PARAM_IPV4_ADDRESS:
2013 af = sctp_get_af_specific(param_type2af(param.p->type));
2014 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2015 scope = sctp_scope(peer_addr);
2016 if (sctp_in_scope(&addr, scope))
2017 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2021 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2022 if (!sctp_cookie_preserve_enable)
2025 stale = ntohl(param.life->lifespan_increment);
2027 /* Suggested Cookie Life span increment's unit is msec,
2030 asoc->cookie_life.tv_sec += stale / 1000;
2031 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2034 case SCTP_PARAM_HOST_NAME_ADDRESS:
2035 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2038 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2039 /* Turn off the default values first so we'll know which
2040 * ones are really set by the peer.
2042 asoc->peer.ipv4_address = 0;
2043 asoc->peer.ipv6_address = 0;
2045 /* Cycle through address types; avoid divide by 0. */
2046 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2048 sat /= sizeof(__u16);
2050 for (i = 0; i < sat; ++i) {
2051 switch (param.sat->types[i]) {
2052 case SCTP_PARAM_IPV4_ADDRESS:
2053 asoc->peer.ipv4_address = 1;
2056 case SCTP_PARAM_IPV6_ADDRESS:
2057 asoc->peer.ipv6_address = 1;
2060 case SCTP_PARAM_HOST_NAME_ADDRESS:
2061 asoc->peer.hostname_address = 1;
2064 default: /* Just ignore anything else. */
2070 case SCTP_PARAM_STATE_COOKIE:
2071 asoc->peer.cookie_len =
2072 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2073 asoc->peer.cookie = param.cookie->body;
2076 case SCTP_PARAM_HEARTBEAT_INFO:
2077 /* Would be odd to receive, but it causes no problems. */
2080 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2081 /* Rejected during verify stage. */
2084 case SCTP_PARAM_ECN_CAPABLE:
2085 asoc->peer.ecn_capable = 1;
2088 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2089 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
2092 case SCTP_PARAM_FWD_TSN_SUPPORT:
2093 if (sctp_prsctp_enable) {
2094 asoc->peer.prsctp_capable = 1;
2099 /* Any unrecognized parameters should have been caught
2100 * and handled by sctp_verify_param() which should be
2101 * called prior to this routine. Simply log the error
2104 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2105 ntohs(param.p->type), asoc);
2112 /* Select a new verification tag. */
2113 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2115 /* I believe that this random number generator complies with RFC1750.
2116 * A tag of 0 is reserved for special cases (e.g. INIT).
2121 get_random_bytes(&x, sizeof(__u32));
2127 /* Select an initial TSN to send during startup. */
2128 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2132 get_random_bytes(&retval, sizeof(__u32));
2137 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2139 * 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
2140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2141 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2145 * | Address Parameter |
2146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2147 * | ASCONF Parameter #1 |
2148 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2152 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2153 * | ASCONF Parameter #N |
2154 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2156 * Address Parameter and other parameter will not be wrapped in this function
2158 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2159 union sctp_addr *addr,
2162 sctp_addiphdr_t asconf;
2163 struct sctp_chunk *retval;
2164 int length = sizeof(asconf) + vparam_len;
2165 union sctp_addr_param addrparam;
2167 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2169 addrlen = af->to_addr_param(addr, &addrparam);
2174 /* Create the chunk. */
2175 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2179 asconf.serial = htonl(asoc->addip_serial++);
2181 retval->subh.addip_hdr =
2182 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2183 retval->param_hdr.v =
2184 sctp_addto_chunk(retval, addrlen, &addrparam);
2190 * 3.2.1 Add IP Address
2192 * 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
2193 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2194 * | Type = 0xC001 | Length = Variable |
2195 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2196 * | ASCONF-Request Correlation ID |
2197 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2198 * | Address Parameter |
2199 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2201 * 3.2.2 Delete IP Address
2203 * 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
2204 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2205 * | Type = 0xC002 | Length = Variable |
2206 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2207 * | ASCONF-Request Correlation ID |
2208 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2209 * | Address Parameter |
2210 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2213 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2214 union sctp_addr *laddr,
2215 struct sockaddr *addrs,
2219 sctp_addip_param_t param;
2220 struct sctp_chunk *retval;
2221 union sctp_addr_param addr_param;
2222 union sctp_addr *addr;
2225 int paramlen = sizeof(param);
2226 int addr_param_len = 0;
2230 /* Get total length of all the address parameters. */
2232 for (i = 0; i < addrcnt; i++) {
2233 addr = (union sctp_addr *)addr_buf;
2234 af = sctp_get_af_specific(addr->v4.sin_family);
2235 addr_param_len = af->to_addr_param(addr, &addr_param);
2237 totallen += paramlen;
2238 totallen += addr_param_len;
2240 addr_buf += af->sockaddr_len;
2243 /* Create an asconf chunk with the required length. */
2244 retval = sctp_make_asconf(asoc, laddr, totallen);
2248 /* Add the address parameters to the asconf chunk. */
2250 for (i = 0; i < addrcnt; i++) {
2251 addr = (union sctp_addr *)addr_buf;
2252 af = sctp_get_af_specific(addr->v4.sin_family);
2253 addr_param_len = af->to_addr_param(addr, &addr_param);
2254 param.param_hdr.type = flags;
2255 param.param_hdr.length = htons(paramlen + addr_param_len);
2258 sctp_addto_chunk(retval, paramlen, ¶m);
2259 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2261 addr_buf += af->sockaddr_len;
2267 * 3.2.4 Set Primary IP Address
2269 * 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
2270 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2271 * | Type =0xC004 | Length = Variable |
2272 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2273 * | ASCONF-Request Correlation ID |
2274 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2275 * | Address Parameter |
2276 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2278 * Create an ASCONF chunk with Set Primary IP address parameter.
2280 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2281 union sctp_addr *addr)
2283 sctp_addip_param_t param;
2284 struct sctp_chunk *retval;
2285 int len = sizeof(param);
2286 union sctp_addr_param addrparam;
2288 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2290 addrlen = af->to_addr_param(addr, &addrparam);
2295 /* Create the chunk and make asconf header. */
2296 retval = sctp_make_asconf(asoc, addr, len);
2300 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2301 param.param_hdr.length = htons(len);
2304 sctp_addto_chunk(retval, sizeof(param), ¶m);
2305 sctp_addto_chunk(retval, addrlen, &addrparam);
2310 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2312 * 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
2313 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2314 * | Type = 0x80 | Chunk Flags | Chunk Length |
2315 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2317 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2318 * | ASCONF Parameter Response#1 |
2319 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2323 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2324 * | ASCONF Parameter Response#N |
2325 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2327 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2329 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2330 __u32 serial, int vparam_len)
2332 sctp_addiphdr_t asconf;
2333 struct sctp_chunk *retval;
2334 int length = sizeof(asconf) + vparam_len;
2336 /* Create the chunk. */
2337 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2341 asconf.serial = htonl(serial);
2343 retval->subh.addip_hdr =
2344 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2349 /* Add response parameters to an ASCONF_ACK chunk. */
2350 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2351 __be16 err_code, sctp_addip_param_t *asconf_param)
2353 sctp_addip_param_t ack_param;
2354 sctp_errhdr_t err_param;
2355 int asconf_param_len = 0;
2356 int err_param_len = 0;
2357 __be16 response_type;
2359 if (SCTP_ERROR_NO_ERROR == err_code) {
2360 response_type = SCTP_PARAM_SUCCESS_REPORT;
2362 response_type = SCTP_PARAM_ERR_CAUSE;
2363 err_param_len = sizeof(err_param);
2366 ntohs(asconf_param->param_hdr.length);
2369 /* Add Success Indication or Error Cause Indication parameter. */
2370 ack_param.param_hdr.type = response_type;
2371 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2374 ack_param.crr_id = crr_id;
2375 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2377 if (SCTP_ERROR_NO_ERROR == err_code)
2380 /* Add Error Cause parameter. */
2381 err_param.cause = err_code;
2382 err_param.length = htons(err_param_len + asconf_param_len);
2383 sctp_addto_chunk(chunk, err_param_len, &err_param);
2385 /* Add the failed TLV copied from ASCONF chunk. */
2387 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2390 /* Process a asconf parameter. */
2391 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2392 struct sctp_chunk *asconf,
2393 sctp_addip_param_t *asconf_param)
2395 struct sctp_transport *peer;
2397 union sctp_addr addr;
2398 struct list_head *pos;
2399 union sctp_addr_param *addr_param;
2401 addr_param = (union sctp_addr_param *)
2402 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2404 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2406 return SCTP_ERROR_INV_PARAM;
2408 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2409 switch (asconf_param->param_hdr.type) {
2410 case SCTP_PARAM_ADD_IP:
2411 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2412 * request and does not have the local resources to add this
2413 * new address to the association, it MUST return an Error
2414 * Cause TLV set to the new error code 'Operation Refused
2415 * Due to Resource Shortage'.
2418 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2420 return SCTP_ERROR_RSRC_LOW;
2422 /* Start the heartbeat timer. */
2423 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2424 sctp_transport_hold(peer);
2426 case SCTP_PARAM_DEL_IP:
2427 /* ADDIP 4.3 D7) If a request is received to delete the
2428 * last remaining IP address of a peer endpoint, the receiver
2429 * MUST send an Error Cause TLV with the error cause set to the
2430 * new error code 'Request to Delete Last Remaining IP Address'.
2432 pos = asoc->peer.transport_addr_list.next;
2433 if (pos->next == &asoc->peer.transport_addr_list)
2434 return SCTP_ERROR_DEL_LAST_IP;
2436 /* ADDIP 4.3 D8) If a request is received to delete an IP
2437 * address which is also the source address of the IP packet
2438 * which contained the ASCONF chunk, the receiver MUST reject
2439 * this request. To reject the request the receiver MUST send
2440 * an Error Cause TLV set to the new error code 'Request to
2441 * Delete Source IP Address'
2443 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2444 return SCTP_ERROR_DEL_SRC_IP;
2446 sctp_assoc_del_peer(asoc, &addr);
2448 case SCTP_PARAM_SET_PRIMARY:
2449 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2451 return SCTP_ERROR_INV_PARAM;
2453 sctp_assoc_set_primary(asoc, peer);
2456 return SCTP_ERROR_INV_PARAM;
2460 return SCTP_ERROR_NO_ERROR;
2463 /* Process an incoming ASCONF chunk with the next expected serial no. and
2464 * return an ASCONF_ACK chunk to be sent in response.
2466 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2467 struct sctp_chunk *asconf)
2469 sctp_addiphdr_t *hdr;
2470 union sctp_addr_param *addr_param;
2471 sctp_addip_param_t *asconf_param;
2472 struct sctp_chunk *asconf_ack;
2476 int chunk_len = asconf->skb->len;
2478 int all_param_pass = 1;
2480 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2481 serial = ntohl(hdr->serial);
2483 /* Skip the addiphdr and store a pointer to address parameter. */
2484 length = sizeof(sctp_addiphdr_t);
2485 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2486 chunk_len -= length;
2488 /* Skip the address parameter and store a pointer to the first
2491 length = ntohs(addr_param->v4.param_hdr.length);
2492 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2493 chunk_len -= length;
2495 /* create an ASCONF_ACK chunk.
2496 * Based on the definitions of parameters, we know that the size of
2497 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2500 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2504 /* Process the TLVs contained within the ASCONF chunk. */
2505 while (chunk_len > 0) {
2506 err_code = sctp_process_asconf_param(asoc, asconf,
2509 * If an error response is received for a TLV parameter,
2510 * all TLVs with no response before the failed TLV are
2511 * considered successful if not reported. All TLVs after
2512 * the failed response are considered unsuccessful unless
2513 * a specific success indication is present for the parameter.
2515 if (SCTP_ERROR_NO_ERROR != err_code)
2518 if (!all_param_pass)
2519 sctp_add_asconf_response(asconf_ack,
2520 asconf_param->crr_id, err_code,
2523 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2524 * an IP address sends an 'Out of Resource' in its response, it
2525 * MUST also fail any subsequent add or delete requests bundled
2528 if (SCTP_ERROR_RSRC_LOW == err_code)
2531 /* Move to the next ASCONF param. */
2532 length = ntohs(asconf_param->param_hdr.length);
2533 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2535 chunk_len -= length;
2539 asoc->peer.addip_serial++;
2541 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2542 * after freeing the reference to old asconf ack if any.
2545 if (asoc->addip_last_asconf_ack)
2546 sctp_chunk_free(asoc->addip_last_asconf_ack);
2548 sctp_chunk_hold(asconf_ack);
2549 asoc->addip_last_asconf_ack = asconf_ack;
2555 /* Process a asconf parameter that is successfully acked. */
2556 static int sctp_asconf_param_success(struct sctp_association *asoc,
2557 sctp_addip_param_t *asconf_param)
2560 union sctp_addr addr;
2561 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2562 union sctp_addr_param *addr_param;
2563 struct list_head *pos;
2564 struct sctp_transport *transport;
2565 struct sctp_sockaddr_entry *saddr;
2568 addr_param = (union sctp_addr_param *)
2569 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2571 /* We have checked the packet before, so we do not check again. */
2572 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2573 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
2575 switch (asconf_param->param_hdr.type) {
2576 case SCTP_PARAM_ADD_IP:
2577 sctp_local_bh_disable();
2578 sctp_write_lock(&asoc->base.addr_lock);
2579 list_for_each(pos, &bp->address_list) {
2580 saddr = list_entry(pos, struct sctp_sockaddr_entry, list);
2581 if (sctp_cmp_addr_exact(&saddr->a, &addr))
2582 saddr->use_as_src = 1;
2584 sctp_write_unlock(&asoc->base.addr_lock);
2585 sctp_local_bh_enable();
2587 case SCTP_PARAM_DEL_IP:
2588 sctp_local_bh_disable();
2589 sctp_write_lock(&asoc->base.addr_lock);
2590 retval = sctp_del_bind_addr(bp, &addr);
2591 sctp_write_unlock(&asoc->base.addr_lock);
2592 sctp_local_bh_enable();
2593 list_for_each(pos, &asoc->peer.transport_addr_list) {
2594 transport = list_entry(pos, struct sctp_transport,
2596 dst_release(transport->dst);
2597 sctp_transport_route(transport, NULL,
2598 sctp_sk(asoc->base.sk));
2608 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2609 * for the given asconf parameter. If there is no response for this parameter,
2610 * return the error code based on the third argument 'no_err'.
2612 * A7) If an error response is received for a TLV parameter, all TLVs with no
2613 * response before the failed TLV are considered successful if not reported.
2614 * All TLVs after the failed response are considered unsuccessful unless a
2615 * specific success indication is present for the parameter.
2617 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2618 sctp_addip_param_t *asconf_param,
2621 sctp_addip_param_t *asconf_ack_param;
2622 sctp_errhdr_t *err_param;
2624 int asconf_ack_len = asconf_ack->skb->len;
2628 err_code = SCTP_ERROR_NO_ERROR;
2630 err_code = SCTP_ERROR_REQ_REFUSED;
2632 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2633 * the first asconf_ack parameter.
2635 length = sizeof(sctp_addiphdr_t);
2636 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2638 asconf_ack_len -= length;
2640 while (asconf_ack_len > 0) {
2641 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2642 switch(asconf_ack_param->param_hdr.type) {
2643 case SCTP_PARAM_SUCCESS_REPORT:
2644 return SCTP_ERROR_NO_ERROR;
2645 case SCTP_PARAM_ERR_CAUSE:
2646 length = sizeof(sctp_addip_param_t);
2647 err_param = (sctp_errhdr_t *)
2648 ((void *)asconf_ack_param + length);
2649 asconf_ack_len -= length;
2650 if (asconf_ack_len > 0)
2651 return err_param->cause;
2653 return SCTP_ERROR_INV_PARAM;
2656 return SCTP_ERROR_INV_PARAM;
2660 length = ntohs(asconf_ack_param->param_hdr.length);
2661 asconf_ack_param = (sctp_addip_param_t *)
2662 ((void *)asconf_ack_param + length);
2663 asconf_ack_len -= length;
2669 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2670 int sctp_process_asconf_ack(struct sctp_association *asoc,
2671 struct sctp_chunk *asconf_ack)
2673 struct sctp_chunk *asconf = asoc->addip_last_asconf;
2674 union sctp_addr_param *addr_param;
2675 sctp_addip_param_t *asconf_param;
2677 int asconf_len = asconf->skb->len;
2678 int all_param_pass = 0;
2681 __be16 err_code = SCTP_ERROR_NO_ERROR;
2683 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2684 * a pointer to address parameter.
2686 length = sizeof(sctp_addip_chunk_t);
2687 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2688 asconf_len -= length;
2690 /* Skip the address parameter in the last asconf sent and store a
2691 * pointer to the first asconf paramter.
2693 length = ntohs(addr_param->v4.param_hdr.length);
2694 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2695 asconf_len -= length;
2698 * A8) If there is no response(s) to specific TLV parameter(s), and no
2699 * failures are indicated, then all request(s) are considered
2702 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2705 /* Process the TLVs contained in the last sent ASCONF chunk. */
2706 while (asconf_len > 0) {
2708 err_code = SCTP_ERROR_NO_ERROR;
2710 err_code = sctp_get_asconf_response(asconf_ack,
2713 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2718 case SCTP_ERROR_NO_ERROR:
2719 retval = sctp_asconf_param_success(asoc, asconf_param);
2722 case SCTP_ERROR_RSRC_LOW:
2726 case SCTP_ERROR_INV_PARAM:
2727 /* Disable sending this type of asconf parameter in
2730 asoc->peer.addip_disabled_mask |=
2731 asconf_param->param_hdr.type;
2734 case SCTP_ERROR_REQ_REFUSED:
2735 case SCTP_ERROR_DEL_LAST_IP:
2736 case SCTP_ERROR_DEL_SRC_IP:
2741 /* Skip the processed asconf parameter and move to the next
2744 length = ntohs(asconf_param->param_hdr.length);
2745 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2747 asconf_len -= length;
2750 /* Free the cached last sent asconf chunk. */
2751 sctp_chunk_free(asconf);
2752 asoc->addip_last_asconf = NULL;
2754 /* Send the next asconf chunk from the addip chunk queue. */
2755 if (!list_empty(&asoc->addip_chunk_list)) {
2756 struct list_head *entry = asoc->addip_chunk_list.next;
2757 asconf = list_entry(entry, struct sctp_chunk, list);
2759 list_del_init(entry);
2761 /* Hold the chunk until an ASCONF_ACK is received. */
2762 sctp_chunk_hold(asconf);
2763 if (sctp_primitive_ASCONF(asoc, asconf))
2764 sctp_chunk_free(asconf);
2766 asoc->addip_last_asconf = asconf;
2772 /* Make a FWD TSN chunk. */
2773 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2774 __u32 new_cum_tsn, size_t nstreams,
2775 struct sctp_fwdtsn_skip *skiplist)
2777 struct sctp_chunk *retval = NULL;
2778 struct sctp_fwdtsn_chunk *ftsn_chunk;
2779 struct sctp_fwdtsn_hdr ftsn_hdr;
2780 struct sctp_fwdtsn_skip skip;
2784 hint = (nstreams + 1) * sizeof(__u32);
2786 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2791 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2793 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2794 retval->subh.fwdtsn_hdr =
2795 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2797 for (i = 0; i < nstreams; i++) {
2798 skip.stream = skiplist[i].stream;
2799 skip.ssn = skiplist[i].ssn;
2800 sctp_addto_chunk(retval, sizeof(skip), &skip);