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,
118 /* Cause code constants are now defined in network order. */
119 err.cause = cause_code;
120 len = sizeof(sctp_errhdr_t) + paylen;
121 err.length = htons(len);
122 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
125 /* 3.3.2 Initiation (INIT) (1)
127 * This chunk is used to initiate a SCTP association between two
128 * endpoints. The format of the INIT chunk is shown below:
131 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | Type = 1 | Chunk Flags | Chunk Length |
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | Advertised Receiver Window Credit (a_rwnd) |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | Number of Outbound Streams | Number of Inbound Streams |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 * / Optional/Variable-Length Parameters /
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 * The INIT chunk contains the following parameters. Unless otherwise
150 * noted, each parameter MUST only be included once in the INIT chunk.
152 * Fixed Parameters Status
153 * ----------------------------------------------
154 * Initiate Tag Mandatory
155 * Advertised Receiver Window Credit Mandatory
156 * Number of Outbound Streams Mandatory
157 * Number of Inbound Streams Mandatory
158 * Initial TSN Mandatory
160 * Variable Parameters Status Type Value
161 * -------------------------------------------------------------
162 * IPv4 Address (Note 1) Optional 5
163 * IPv6 Address (Note 1) Optional 6
164 * Cookie Preservative Optional 9
165 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
166 * Host Name Address (Note 3) Optional 11
167 * Supported Address Types (Note 4) Optional 12
169 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
170 const struct sctp_bind_addr *bp,
171 gfp_t gfp, int vparam_len)
174 union sctp_params addrs;
176 struct sctp_chunk *retval = NULL;
177 int num_types, addrs_len = 0;
178 struct sctp_sock *sp;
179 sctp_supported_addrs_param_t sat;
181 sctp_adaptation_ind_param_t aiparam;
183 /* RFC 2960 3.3.2 Initiation (INIT) (1)
185 * Note 1: The INIT chunks can contain multiple addresses that
186 * can be IPv4 and/or IPv6 in any combination.
190 /* Convert the provided bind address list to raw format. */
191 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
193 init.init_tag = htonl(asoc->c.my_vtag);
194 init.a_rwnd = htonl(asoc->rwnd);
195 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
196 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
197 init.initial_tsn = htonl(asoc->c.initial_tsn);
199 /* How many address types are needed? */
200 sp = sctp_sk(asoc->base.sk);
201 num_types = sp->pf->supported_addrs(sp, types);
203 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
204 chunksize += sizeof(ecap_param);
205 if (sctp_prsctp_enable)
206 chunksize += sizeof(prsctp_param);
207 chunksize += sizeof(aiparam);
208 chunksize += vparam_len;
210 /* RFC 2960 3.3.2 Initiation (INIT) (1)
212 * Note 3: An INIT chunk MUST NOT contain more than one Host
213 * Name address parameter. Moreover, the sender of the INIT
214 * MUST NOT combine any other address types with the Host Name
215 * address in the INIT. The receiver of INIT MUST ignore any
216 * other address types if the Host Name address parameter is
217 * present in the received INIT chunk.
219 * PLEASE DO NOT FIXME [This version does not support Host Name.]
222 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
226 retval->subh.init_hdr =
227 sctp_addto_chunk(retval, sizeof(init), &init);
228 retval->param_hdr.v =
229 sctp_addto_chunk(retval, addrs_len, addrs.v);
231 /* RFC 2960 3.3.2 Initiation (INIT) (1)
233 * Note 4: This parameter, when present, specifies all the
234 * address types the sending endpoint can support. The absence
235 * of this parameter indicates that the sending endpoint can
236 * support any address type.
238 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
239 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
240 sctp_addto_chunk(retval, sizeof(sat), &sat);
241 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
243 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
244 if (sctp_prsctp_enable)
245 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
246 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
247 aiparam.param_hdr.length = htons(sizeof(aiparam));
248 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
249 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
255 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
256 const struct sctp_chunk *chunk,
257 gfp_t gfp, int unkparam_len)
259 sctp_inithdr_t initack;
260 struct sctp_chunk *retval;
261 union sctp_params addrs;
263 sctp_cookie_param_t *cookie;
266 sctp_adaptation_ind_param_t aiparam;
270 /* Note: there may be no addresses to embed. */
271 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
273 initack.init_tag = htonl(asoc->c.my_vtag);
274 initack.a_rwnd = htonl(asoc->rwnd);
275 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
276 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
277 initack.initial_tsn = htonl(asoc->c.initial_tsn);
279 /* FIXME: We really ought to build the cookie right
280 * into the packet instead of allocating more fresh memory.
282 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
287 /* Calculate the total size of allocation, include the reserved
288 * space for reporting unknown parameters if it is specified.
290 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
292 /* Tell peer that we'll do ECN only if peer advertised such cap. */
293 if (asoc->peer.ecn_capable)
294 chunksize += sizeof(ecap_param);
296 /* Tell peer that we'll do PR-SCTP only if peer advertised. */
297 if (asoc->peer.prsctp_capable)
298 chunksize += sizeof(prsctp_param);
300 chunksize += sizeof(aiparam);
302 /* Now allocate and fill out the chunk. */
303 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
307 /* Per the advice in RFC 2960 6.4, send this reply to
308 * the source of the INIT packet.
310 retval->transport = chunk->transport;
311 retval->subh.init_hdr =
312 sctp_addto_chunk(retval, sizeof(initack), &initack);
313 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
314 sctp_addto_chunk(retval, cookie_len, cookie);
315 if (asoc->peer.ecn_capable)
316 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
317 if (asoc->peer.prsctp_capable)
318 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
320 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
321 aiparam.param_hdr.length = htons(sizeof(aiparam));
322 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
323 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
325 /* We need to remove the const qualifier at this point. */
326 retval->asoc = (struct sctp_association *) asoc;
328 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
330 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
331 * HEARTBEAT ACK, * etc.) to the same destination transport
332 * address from which it received the DATA or control chunk
333 * to which it is replying.
335 * [INIT ACK back to where the INIT came from.]
338 retval->transport = chunk->transport;
347 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
349 * This chunk is used only during the initialization of an association.
350 * It is sent by the initiator of an association to its peer to complete
351 * the initialization process. This chunk MUST precede any DATA chunk
352 * sent within the association, but MAY be bundled with one or more DATA
353 * chunks in the same packet.
356 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
357 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
358 * | Type = 10 |Chunk Flags | Length |
359 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
362 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366 * Set to zero on transmit and ignored on receipt.
368 * Length: 16 bits (unsigned integer)
370 * Set to the size of the chunk in bytes, including the 4 bytes of
371 * the chunk header and the size of the Cookie.
373 * Cookie: variable size
375 * This field must contain the exact cookie received in the
376 * State Cookie parameter from the previous INIT ACK.
378 * An implementation SHOULD make the cookie as small as possible
379 * to insure interoperability.
381 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
382 const struct sctp_chunk *chunk)
384 struct sctp_chunk *retval;
388 cookie = asoc->peer.cookie;
389 cookie_len = asoc->peer.cookie_len;
391 /* Build a cookie echo chunk. */
392 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
395 retval->subh.cookie_hdr =
396 sctp_addto_chunk(retval, cookie_len, cookie);
398 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
400 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
401 * HEARTBEAT ACK, * etc.) to the same destination transport
402 * address from which it * received the DATA or control chunk
403 * to which it is replying.
405 * [COOKIE ECHO back to where the INIT ACK came from.]
408 retval->transport = chunk->transport;
414 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
416 * This chunk is used only during the initialization of an
417 * association. It is used to acknowledge the receipt of a COOKIE
418 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
419 * within the association, but MAY be bundled with one or more DATA
420 * chunks or SACK chunk in the same SCTP packet.
423 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
424 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
425 * | Type = 11 |Chunk Flags | Length = 4 |
426 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
428 * Chunk Flags: 8 bits
430 * Set to zero on transmit and ignored on receipt.
432 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
433 const struct sctp_chunk *chunk)
435 struct sctp_chunk *retval;
437 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
439 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
441 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
442 * HEARTBEAT ACK, * etc.) to the same destination transport
443 * address from which it * received the DATA or control chunk
444 * to which it is replying.
446 * [COOKIE ACK back to where the COOKIE ECHO came from.]
449 retval->transport = chunk->transport;
455 * Appendix A: Explicit Congestion Notification:
458 * RFC 2481 details a specific bit for a sender to send in the header of
459 * its next outbound TCP segment to indicate to its peer that it has
460 * reduced its congestion window. This is termed the CWR bit. For
461 * SCTP the same indication is made by including the CWR chunk.
462 * This chunk contains one data element, i.e. the TSN number that
463 * was sent in the ECNE chunk. This element represents the lowest
464 * TSN number in the datagram that was originally marked with the
468 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
469 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
470 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
471 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
472 * | Lowest TSN Number |
473 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
475 * Note: The CWR is considered a Control chunk.
477 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
478 const __u32 lowest_tsn,
479 const struct sctp_chunk *chunk)
481 struct sctp_chunk *retval;
484 cwr.lowest_tsn = htonl(lowest_tsn);
485 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
486 sizeof(sctp_cwrhdr_t));
491 retval->subh.ecn_cwr_hdr =
492 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
494 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
496 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
497 * HEARTBEAT ACK, * etc.) to the same destination transport
498 * address from which it * received the DATA or control chunk
499 * to which it is replying.
501 * [Report a reduced congestion window back to where the ECNE
505 retval->transport = chunk->transport;
511 /* Make an ECNE chunk. This is a congestion experienced report. */
512 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
513 const __u32 lowest_tsn)
515 struct sctp_chunk *retval;
518 ecne.lowest_tsn = htonl(lowest_tsn);
519 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
520 sizeof(sctp_ecnehdr_t));
523 retval->subh.ecne_hdr =
524 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
530 /* Make a DATA chunk for the given association from the provided
531 * parameters. However, do not populate the data payload.
533 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
534 const struct sctp_sndrcvinfo *sinfo,
535 int data_len, __u8 flags, __u16 ssn)
537 struct sctp_chunk *retval;
538 struct sctp_datahdr dp;
541 /* We assign the TSN as LATE as possible, not here when
542 * creating the chunk.
545 dp.stream = htons(sinfo->sinfo_stream);
546 dp.ppid = sinfo->sinfo_ppid;
548 /* Set the flags for an unordered send. */
549 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
550 flags |= SCTP_DATA_UNORDERED;
555 chunk_len = sizeof(dp) + data_len;
556 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
560 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
561 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
567 /* Create a selective ackowledgement (SACK) for the given
568 * association. This reports on which TSN's we've seen to date,
569 * including duplicates and gaps.
571 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
573 struct sctp_chunk *retval;
574 struct sctp_sackhdr sack;
577 __u16 num_gabs, num_dup_tsns;
578 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
580 ctsn = sctp_tsnmap_get_ctsn(map);
581 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
583 /* How much room is needed in the chunk? */
584 num_gabs = sctp_tsnmap_num_gabs(map);
585 num_dup_tsns = sctp_tsnmap_num_dups(map);
587 /* Initialize the SACK header. */
588 sack.cum_tsn_ack = htonl(ctsn);
589 sack.a_rwnd = htonl(asoc->a_rwnd);
590 sack.num_gap_ack_blocks = htons(num_gabs);
591 sack.num_dup_tsns = htons(num_dup_tsns);
594 + sizeof(struct sctp_gap_ack_block) * num_gabs
595 + sizeof(__u32) * num_dup_tsns;
597 /* Create the chunk. */
598 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
602 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
604 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
605 * HEARTBEAT ACK, etc.) to the same destination transport
606 * address from which it received the DATA or control chunk to
607 * which it is replying. This rule should also be followed if
608 * the endpoint is bundling DATA chunks together with the
611 * However, when acknowledging multiple DATA chunks received
612 * in packets from different source addresses in a single
613 * SACK, the SACK chunk may be transmitted to one of the
614 * destination transport addresses from which the DATA or
615 * control chunks being acknowledged were received.
617 * [BUG: We do not implement the following paragraph.
618 * Perhaps we should remember the last transport we used for a
619 * SACK and avoid that (if possible) if we have seen any
620 * duplicates. --piggy]
622 * When a receiver of a duplicate DATA chunk sends a SACK to a
623 * multi- homed endpoint it MAY be beneficial to vary the
624 * destination address and not use the source address of the
625 * DATA chunk. The reason being that receiving a duplicate
626 * from a multi-homed endpoint might indicate that the return
627 * path (as specified in the source address of the DATA chunk)
628 * for the SACK is broken.
630 * [Send to the address from which we last received a DATA chunk.]
632 retval->transport = asoc->peer.last_data_from;
634 retval->subh.sack_hdr =
635 sctp_addto_chunk(retval, sizeof(sack), &sack);
637 /* Add the gap ack block information. */
639 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
640 sctp_tsnmap_get_gabs(map));
642 /* Add the duplicate TSN information. */
644 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
645 sctp_tsnmap_get_dups(map));
651 /* Make a SHUTDOWN chunk. */
652 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
653 const struct sctp_chunk *chunk)
655 struct sctp_chunk *retval;
656 sctp_shutdownhdr_t shut;
659 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
660 shut.cum_tsn_ack = htonl(ctsn);
662 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
663 sizeof(sctp_shutdownhdr_t));
667 retval->subh.shutdown_hdr =
668 sctp_addto_chunk(retval, sizeof(shut), &shut);
671 retval->transport = chunk->transport;
676 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
677 const struct sctp_chunk *chunk)
679 struct sctp_chunk *retval;
681 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
683 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
685 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
686 * HEARTBEAT ACK, * etc.) to the same destination transport
687 * address from which it * received the DATA or control chunk
688 * to which it is replying.
690 * [ACK back to where the SHUTDOWN came from.]
693 retval->transport = chunk->transport;
698 struct sctp_chunk *sctp_make_shutdown_complete(
699 const struct sctp_association *asoc,
700 const struct sctp_chunk *chunk)
702 struct sctp_chunk *retval;
705 /* Set the T-bit if we have no association (vtag will be
708 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
710 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
712 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
714 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
715 * HEARTBEAT ACK, * etc.) to the same destination transport
716 * address from which it * received the DATA or control chunk
717 * to which it is replying.
719 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
723 retval->transport = chunk->transport;
728 /* Create an ABORT. Note that we set the T bit if we have no
729 * association, except when responding to an INIT (sctpimpguide 2.41).
731 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
732 const struct sctp_chunk *chunk,
735 struct sctp_chunk *retval;
738 /* Set the T-bit if we have no association and 'chunk' is not
739 * an INIT (vtag will be reflected).
742 if (chunk && chunk->chunk_hdr &&
743 chunk->chunk_hdr->type == SCTP_CID_INIT)
746 flags = SCTP_CHUNK_FLAG_T;
749 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
751 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
753 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
754 * HEARTBEAT ACK, * etc.) to the same destination transport
755 * address from which it * received the DATA or control chunk
756 * to which it is replying.
758 * [ABORT back to where the offender came from.]
761 retval->transport = chunk->transport;
766 /* Helper to create ABORT with a NO_USER_DATA error. */
767 struct sctp_chunk *sctp_make_abort_no_data(
768 const struct sctp_association *asoc,
769 const struct sctp_chunk *chunk, __u32 tsn)
771 struct sctp_chunk *retval;
774 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
780 /* Put the tsn back into network byte order. */
781 payload = htonl(tsn);
782 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
783 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
785 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
787 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
788 * HEARTBEAT ACK, * etc.) to the same destination transport
789 * address from which it * received the DATA or control chunk
790 * to which it is replying.
792 * [ABORT back to where the offender came from.]
795 retval->transport = chunk->transport;
801 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
802 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
803 const struct msghdr *msg,
806 struct sctp_chunk *retval;
807 void *payload = NULL;
810 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
815 /* Put the msg_iov together into payload. */
816 payload = kmalloc(paylen, GFP_KERNEL);
820 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
825 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
826 sctp_addto_chunk(retval, paylen, payload);
836 sctp_chunk_free(retval);
842 /* Append bytes to the end of a parameter. Will panic if chunk is not big
845 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
849 int chunklen = ntohs(chunk->chunk_hdr->length);
851 target = skb_put(chunk->skb, len);
853 memcpy(target, data, len);
855 /* Adjust the chunk length field. */
856 chunk->chunk_hdr->length = htons(chunklen + len);
857 chunk->chunk_end = skb_tail_pointer(chunk->skb);
862 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
863 struct sctp_chunk *sctp_make_abort_violation(
864 const struct sctp_association *asoc,
865 const struct sctp_chunk *chunk,
869 struct sctp_chunk *retval;
870 struct sctp_paramhdr phdr;
872 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
873 + sizeof(sctp_paramhdr_t));
877 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
878 + sizeof(sctp_paramhdr_t));
880 phdr.type = htons(chunk->chunk_hdr->type);
881 phdr.length = chunk->chunk_hdr->length;
882 sctp_addto_chunk(retval, paylen, payload);
883 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
889 /* Make a HEARTBEAT chunk. */
890 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
891 const struct sctp_transport *transport,
892 const void *payload, const size_t paylen)
894 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
900 /* Cast away the 'const', as this is just telling the chunk
901 * what transport it belongs to.
903 retval->transport = (struct sctp_transport *) transport;
904 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
910 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
911 const struct sctp_chunk *chunk,
912 const void *payload, const size_t paylen)
914 struct sctp_chunk *retval;
916 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
920 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
922 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
924 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
925 * HEARTBEAT ACK, * etc.) to the same destination transport
926 * address from which it * received the DATA or control chunk
927 * to which it is replying.
929 * [HBACK back to where the HEARTBEAT came from.]
932 retval->transport = chunk->transport;
938 /* Create an Operation Error chunk with the specified space reserved.
939 * This routine can be used for containing multiple causes in the chunk.
941 static struct sctp_chunk *sctp_make_op_error_space(
942 const struct sctp_association *asoc,
943 const struct sctp_chunk *chunk,
946 struct sctp_chunk *retval;
948 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
949 sizeof(sctp_errhdr_t) + size);
953 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
955 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
956 * HEARTBEAT ACK, etc.) to the same destination transport
957 * address from which it received the DATA or control chunk
958 * to which it is replying.
962 retval->transport = chunk->transport;
968 /* Create an Operation Error chunk. */
969 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
970 const struct sctp_chunk *chunk,
971 __be16 cause_code, const void *payload,
974 struct sctp_chunk *retval;
976 retval = sctp_make_op_error_space(asoc, chunk, paylen);
980 sctp_init_cause(retval, cause_code, paylen);
981 sctp_addto_chunk(retval, paylen, payload);
987 /********************************************************************
988 * 2nd Level Abstractions
989 ********************************************************************/
991 /* Turn an skb into a chunk.
992 * FIXME: Eventually move the structure directly inside the skb->cb[].
994 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
995 const struct sctp_association *asoc,
998 struct sctp_chunk *retval;
1000 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
1006 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1009 INIT_LIST_HEAD(&retval->list);
1011 retval->asoc = (struct sctp_association *)asoc;
1013 retval->has_tsn = 0;
1014 retval->has_ssn = 0;
1015 retval->rtt_in_progress = 0;
1016 retval->sent_at = 0;
1017 retval->singleton = 1;
1018 retval->end_of_packet = 0;
1019 retval->ecn_ce_done = 0;
1020 retval->pdiscard = 0;
1022 /* sctpimpguide-05.txt Section 2.8.2
1023 * M1) Each time a new DATA chunk is transmitted
1024 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1025 * 'TSN.Missing.Report' count will be used to determine missing chunks
1026 * and when to fast retransmit.
1028 retval->tsn_missing_report = 0;
1029 retval->tsn_gap_acked = 0;
1030 retval->fast_retransmit = 0;
1032 /* If this is a fragmented message, track all fragments
1033 * of the message (for SEND_FAILED).
1037 /* Polish the bead hole. */
1038 INIT_LIST_HEAD(&retval->transmitted_list);
1039 INIT_LIST_HEAD(&retval->frag_list);
1040 SCTP_DBG_OBJCNT_INC(chunk);
1041 atomic_set(&retval->refcnt, 1);
1047 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1048 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1049 union sctp_addr *dest)
1051 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1052 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1055 /* Extract the source address from a chunk. */
1056 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1058 /* If we have a known transport, use that. */
1059 if (chunk->transport) {
1060 return &chunk->transport->ipaddr;
1062 /* Otherwise, extract it from the IP header. */
1063 return &chunk->source;
1067 /* Create a new chunk, setting the type and flags headers from the
1068 * arguments, reserving enough space for a 'paylen' byte payload.
1071 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1072 __u8 type, __u8 flags, int paylen)
1074 struct sctp_chunk *retval;
1075 sctp_chunkhdr_t *chunk_hdr;
1076 struct sk_buff *skb;
1079 /* No need to allocate LL here, as this is only a chunk. */
1080 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1085 /* Make room for the chunk header. */
1086 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1087 chunk_hdr->type = type;
1088 chunk_hdr->flags = flags;
1089 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1091 sk = asoc ? asoc->base.sk : NULL;
1092 retval = sctp_chunkify(skb, asoc, sk);
1098 retval->chunk_hdr = chunk_hdr;
1099 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1101 /* Set the skb to the belonging sock for accounting. */
1110 /* Release the memory occupied by a chunk. */
1111 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1113 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1114 dev_kfree_skb(chunk->skb);
1116 SCTP_DBG_OBJCNT_DEC(chunk);
1117 kmem_cache_free(sctp_chunk_cachep, chunk);
1120 /* Possibly, free the chunk. */
1121 void sctp_chunk_free(struct sctp_chunk *chunk)
1123 BUG_ON(!list_empty(&chunk->list));
1124 list_del_init(&chunk->transmitted_list);
1126 /* Release our reference on the message tracker. */
1128 sctp_datamsg_put(chunk->msg);
1130 sctp_chunk_put(chunk);
1133 /* Grab a reference to the chunk. */
1134 void sctp_chunk_hold(struct sctp_chunk *ch)
1136 atomic_inc(&ch->refcnt);
1139 /* Release a reference to the chunk. */
1140 void sctp_chunk_put(struct sctp_chunk *ch)
1142 if (atomic_dec_and_test(&ch->refcnt))
1143 sctp_chunk_destroy(ch);
1146 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1149 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1153 int chunklen = ntohs(chunk->chunk_hdr->length);
1154 int padlen = WORD_ROUND(chunklen) - chunklen;
1156 padding = skb_put(chunk->skb, padlen);
1157 target = skb_put(chunk->skb, len);
1159 memset(padding, 0, padlen);
1160 memcpy(target, data, len);
1162 /* Adjust the chunk length field. */
1163 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1164 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1169 /* Append bytes from user space to the end of a chunk. Will panic if
1170 * chunk is not big enough.
1171 * Returns a kernel err value.
1173 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1179 /* Make room in chunk for data. */
1180 target = skb_put(chunk->skb, len);
1182 /* Copy data (whole iovec) into chunk */
1183 if ((err = memcpy_fromiovecend(target, data, off, len)))
1186 /* Adjust the chunk length field. */
1187 chunk->chunk_hdr->length =
1188 htons(ntohs(chunk->chunk_hdr->length) + len);
1189 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1195 /* Helper function to assign a TSN if needed. This assumes that both
1196 * the data_hdr and association have already been assigned.
1198 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1200 struct sctp_datamsg *msg;
1201 struct sctp_chunk *lchunk;
1202 struct sctp_stream *stream;
1209 /* All fragments will be on the same stream */
1210 sid = ntohs(chunk->subh.data_hdr->stream);
1211 stream = &chunk->asoc->ssnmap->out;
1213 /* Now assign the sequence number to the entire message.
1214 * All fragments must have the same stream sequence number.
1217 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1218 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1221 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1222 ssn = sctp_ssn_next(stream, sid);
1224 ssn = sctp_ssn_peek(stream, sid);
1227 lchunk->subh.data_hdr->ssn = htons(ssn);
1228 lchunk->has_ssn = 1;
1232 /* Helper function to assign a TSN if needed. This assumes that both
1233 * the data_hdr and association have already been assigned.
1235 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1237 if (!chunk->has_tsn) {
1238 /* This is the last possible instant to
1241 chunk->subh.data_hdr->tsn =
1242 htonl(sctp_association_get_next_tsn(chunk->asoc));
1247 /* Create a CLOSED association to use with an incoming packet. */
1248 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1249 struct sctp_chunk *chunk,
1252 struct sctp_association *asoc;
1253 struct sk_buff *skb;
1257 /* Create the bare association. */
1258 scope = sctp_scope(sctp_source(chunk));
1259 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1264 /* Create an entry for the source address of the packet. */
1265 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1268 af->from_skb(&asoc->c.peer_addr, skb, 1);
1273 sctp_association_free(asoc);
1277 /* Build a cookie representing asoc.
1278 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1280 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1281 const struct sctp_association *asoc,
1282 const struct sctp_chunk *init_chunk,
1284 const __u8 *raw_addrs, int addrs_len)
1286 sctp_cookie_param_t *retval;
1287 struct sctp_signed_cookie *cookie;
1288 struct scatterlist sg;
1289 int headersize, bodysize;
1290 unsigned int keylen;
1293 /* Header size is static data prior to the actual cookie, including
1296 headersize = sizeof(sctp_paramhdr_t) +
1297 (sizeof(struct sctp_signed_cookie) -
1298 sizeof(struct sctp_cookie));
1299 bodysize = sizeof(struct sctp_cookie)
1300 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1302 /* Pad out the cookie to a multiple to make the signature
1303 * functions simpler to write.
1305 if (bodysize % SCTP_COOKIE_MULTIPLE)
1306 bodysize += SCTP_COOKIE_MULTIPLE
1307 - (bodysize % SCTP_COOKIE_MULTIPLE);
1308 *cookie_len = headersize + bodysize;
1310 /* Clear this memory since we are sending this data structure
1311 * out on the network.
1313 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1317 cookie = (struct sctp_signed_cookie *) retval->body;
1319 /* Set up the parameter header. */
1320 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1321 retval->p.length = htons(*cookie_len);
1323 /* Copy the cookie part of the association itself. */
1324 cookie->c = asoc->c;
1325 /* Save the raw address list length in the cookie. */
1326 cookie->c.raw_addr_list_len = addrs_len;
1328 /* Remember PR-SCTP capability. */
1329 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1331 /* Save adaptation indication in the cookie. */
1332 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1334 /* Set an expiration time for the cookie. */
1335 do_gettimeofday(&cookie->c.expiration);
1336 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1338 /* Copy the peer's init packet. */
1339 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1340 ntohs(init_chunk->chunk_hdr->length));
1342 /* Copy the raw local address list of the association. */
1343 memcpy((__u8 *)&cookie->c.peer_init[0] +
1344 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1346 if (sctp_sk(ep->base.sk)->hmac) {
1347 struct hash_desc desc;
1349 /* Sign the message. */
1350 sg.page = virt_to_page(&cookie->c);
1351 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1352 sg.length = bodysize;
1353 keylen = SCTP_SECRET_SIZE;
1354 key = (char *)ep->secret_key[ep->current_key];
1355 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1358 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1359 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1372 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1373 struct sctp_association *sctp_unpack_cookie(
1374 const struct sctp_endpoint *ep,
1375 const struct sctp_association *asoc,
1376 struct sctp_chunk *chunk, gfp_t gfp,
1377 int *error, struct sctp_chunk **errp)
1379 struct sctp_association *retval = NULL;
1380 struct sctp_signed_cookie *cookie;
1381 struct sctp_cookie *bear_cookie;
1382 int headersize, bodysize, fixed_size;
1383 __u8 *digest = ep->digest;
1384 struct scatterlist sg;
1385 unsigned int keylen, len;
1388 struct sk_buff *skb = chunk->skb;
1390 struct hash_desc desc;
1392 /* Header size is static data prior to the actual cookie, including
1395 headersize = sizeof(sctp_chunkhdr_t) +
1396 (sizeof(struct sctp_signed_cookie) -
1397 sizeof(struct sctp_cookie));
1398 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1399 fixed_size = headersize + sizeof(struct sctp_cookie);
1401 /* Verify that the chunk looks like it even has a cookie.
1402 * There must be enough room for our cookie and our peer's
1405 len = ntohs(chunk->chunk_hdr->length);
1406 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1409 /* Verify that the cookie has been padded out. */
1410 if (bodysize % SCTP_COOKIE_MULTIPLE)
1413 /* Process the cookie. */
1414 cookie = chunk->subh.cookie_hdr;
1415 bear_cookie = &cookie->c;
1417 if (!sctp_sk(ep->base.sk)->hmac)
1420 /* Check the signature. */
1421 keylen = SCTP_SECRET_SIZE;
1422 sg.page = virt_to_page(bear_cookie);
1423 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1424 sg.length = bodysize;
1425 key = (char *)ep->secret_key[ep->current_key];
1426 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1429 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1430 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1431 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1432 *error = -SCTP_IERROR_NOMEM;
1436 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1437 /* Try the previous key. */
1438 key = (char *)ep->secret_key[ep->last_key];
1439 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1440 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1441 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1442 *error = -SCTP_IERROR_NOMEM;
1446 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1447 /* Yikes! Still bad signature! */
1448 *error = -SCTP_IERROR_BAD_SIG;
1454 /* IG Section 2.35.2:
1455 * 3) Compare the port numbers and the verification tag contained
1456 * within the COOKIE ECHO chunk to the actual port numbers and the
1457 * verification tag within the SCTP common header of the received
1458 * packet. If these values do not match the packet MUST be silently
1461 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1462 *error = -SCTP_IERROR_BAD_TAG;
1466 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1467 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1468 *error = -SCTP_IERROR_BAD_PORTS;
1472 /* Check to see if the cookie is stale. If there is already
1473 * an association, there is no need to check cookie's expiration
1474 * for init collision case of lost COOKIE ACK.
1475 * If skb has been timestamped, then use the stamp, otherwise
1476 * use current time. This introduces a small possibility that
1477 * that a cookie may be considered expired, but his would only slow
1478 * down the new association establishment instead of every packet.
1480 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1481 skb_get_timestamp(skb, &tv);
1483 do_gettimeofday(&tv);
1485 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1487 * Section 3.3.10.3 Stale Cookie Error (3)
1491 * Stale Cookie Error: Indicates the receipt of a valid State
1492 * Cookie that has expired.
1494 len = ntohs(chunk->chunk_hdr->length);
1495 *errp = sctp_make_op_error_space(asoc, chunk, len);
1497 suseconds_t usecs = (tv.tv_sec -
1498 bear_cookie->expiration.tv_sec) * 1000000L +
1499 tv.tv_usec - bear_cookie->expiration.tv_usec;
1500 __be32 n = htonl(usecs);
1502 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1504 sctp_addto_chunk(*errp, sizeof(n), &n);
1505 *error = -SCTP_IERROR_STALE_COOKIE;
1507 *error = -SCTP_IERROR_NOMEM;
1512 /* Make a new base association. */
1513 scope = sctp_scope(sctp_source(chunk));
1514 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1516 *error = -SCTP_IERROR_NOMEM;
1520 /* Set up our peer's port number. */
1521 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1523 /* Populate the association from the cookie. */
1524 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1526 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1528 *error = -SCTP_IERROR_NOMEM;
1532 /* Also, add the destination address. */
1533 if (list_empty(&retval->base.bind_addr.address_list)) {
1534 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
1538 retval->next_tsn = retval->c.initial_tsn;
1539 retval->ctsn_ack_point = retval->next_tsn - 1;
1540 retval->addip_serial = retval->c.initial_tsn;
1541 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1542 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1543 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1545 /* The INIT stuff will be done by the side effects. */
1550 sctp_association_free(retval);
1555 /* Yikes! The packet is either corrupt or deliberately
1558 *error = -SCTP_IERROR_MALFORMED;
1562 /********************************************************************
1563 * 3rd Level Abstractions
1564 ********************************************************************/
1566 struct __sctp_missing {
1569 } __attribute__((packed));
1572 * Report a missing mandatory parameter.
1574 static int sctp_process_missing_param(const struct sctp_association *asoc,
1575 sctp_param_t paramtype,
1576 struct sctp_chunk *chunk,
1577 struct sctp_chunk **errp)
1579 struct __sctp_missing report;
1582 len = WORD_ROUND(sizeof(report));
1584 /* Make an ERROR chunk, preparing enough room for
1585 * returning multiple unknown parameters.
1588 *errp = sctp_make_op_error_space(asoc, chunk, len);
1591 report.num_missing = htonl(1);
1592 report.type = paramtype;
1593 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1595 sctp_addto_chunk(*errp, sizeof(report), &report);
1598 /* Stop processing this chunk. */
1602 /* Report an Invalid Mandatory Parameter. */
1603 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1604 struct sctp_chunk *chunk,
1605 struct sctp_chunk **errp)
1607 /* Invalid Mandatory Parameter Error has no payload. */
1610 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1613 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1615 /* Stop processing this chunk. */
1619 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1620 struct sctp_paramhdr *param,
1621 const struct sctp_chunk *chunk,
1622 struct sctp_chunk **errp)
1624 char error[] = "The following parameter had invalid length:";
1625 size_t payload_len = WORD_ROUND(sizeof(error)) +
1626 sizeof(sctp_paramhdr_t);
1629 /* Create an error chunk and fill it in with our payload. */
1631 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1634 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION,
1635 sizeof(error) + sizeof(sctp_paramhdr_t));
1636 sctp_addto_chunk(*errp, sizeof(error), error);
1637 sctp_addto_param(*errp, sizeof(sctp_paramhdr_t), param);
1644 /* Do not attempt to handle the HOST_NAME parm. However, do
1645 * send back an indicator to the peer.
1647 static int sctp_process_hn_param(const struct sctp_association *asoc,
1648 union sctp_params param,
1649 struct sctp_chunk *chunk,
1650 struct sctp_chunk **errp)
1652 __u16 len = ntohs(param.p->length);
1654 /* Make an ERROR chunk. */
1656 *errp = sctp_make_op_error_space(asoc, chunk, len);
1659 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1660 sctp_addto_chunk(*errp, len, param.v);
1663 /* Stop processing this chunk. */
1667 /* RFC 3.2.1 & the Implementers Guide 2.2.
1669 * The Parameter Types are encoded such that the
1670 * highest-order two bits specify the action that must be
1671 * taken if the processing endpoint does not recognize the
1674 * 00 - Stop processing this SCTP chunk and discard it,
1675 * do not process any further chunks within it.
1677 * 01 - Stop processing this SCTP chunk and discard it,
1678 * do not process any further chunks within it, and report
1679 * the unrecognized parameter in an 'Unrecognized
1680 * Parameter Type' (in either an ERROR or in the INIT ACK).
1682 * 10 - Skip this parameter and continue processing.
1684 * 11 - Skip this parameter and continue processing but
1685 * report the unrecognized parameter in an
1686 * 'Unrecognized Parameter Type' (in either an ERROR or in
1690 * 0 - discard the chunk
1691 * 1 - continue with the chunk
1693 static int sctp_process_unk_param(const struct sctp_association *asoc,
1694 union sctp_params param,
1695 struct sctp_chunk *chunk,
1696 struct sctp_chunk **errp)
1700 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1701 case SCTP_PARAM_ACTION_DISCARD:
1704 case SCTP_PARAM_ACTION_DISCARD_ERR:
1706 /* Make an ERROR chunk, preparing enough room for
1707 * returning multiple unknown parameters.
1710 *errp = sctp_make_op_error_space(asoc, chunk,
1711 ntohs(chunk->chunk_hdr->length));
1714 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1715 WORD_ROUND(ntohs(param.p->length)));
1716 sctp_addto_chunk(*errp,
1717 WORD_ROUND(ntohs(param.p->length)),
1722 case SCTP_PARAM_ACTION_SKIP:
1724 case SCTP_PARAM_ACTION_SKIP_ERR:
1725 /* Make an ERROR chunk, preparing enough room for
1726 * returning multiple unknown parameters.
1729 *errp = sctp_make_op_error_space(asoc, chunk,
1730 ntohs(chunk->chunk_hdr->length));
1733 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1734 WORD_ROUND(ntohs(param.p->length)));
1735 sctp_addto_chunk(*errp,
1736 WORD_ROUND(ntohs(param.p->length)),
1739 /* If there is no memory for generating the ERROR
1740 * report as specified, an ABORT will be triggered
1741 * to the peer and the association won't be
1755 /* Find unrecognized parameters in the chunk.
1757 * 0 - discard the chunk
1758 * 1 - continue with the chunk
1760 static int sctp_verify_param(const struct sctp_association *asoc,
1761 union sctp_params param,
1763 struct sctp_chunk *chunk,
1764 struct sctp_chunk **err_chunk)
1768 /* FIXME - This routine is not looking at each parameter per the
1769 * chunk type, i.e., unrecognized parameters should be further
1770 * identified based on the chunk id.
1773 switch (param.p->type) {
1774 case SCTP_PARAM_IPV4_ADDRESS:
1775 case SCTP_PARAM_IPV6_ADDRESS:
1776 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1777 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1778 case SCTP_PARAM_STATE_COOKIE:
1779 case SCTP_PARAM_HEARTBEAT_INFO:
1780 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1781 case SCTP_PARAM_ECN_CAPABLE:
1782 case SCTP_PARAM_ADAPTATION_LAYER_IND:
1785 case SCTP_PARAM_HOST_NAME_ADDRESS:
1786 /* Tell the peer, we won't support this param. */
1787 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1788 case SCTP_PARAM_FWD_TSN_SUPPORT:
1789 if (sctp_prsctp_enable)
1793 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1794 ntohs(param.p->type), cid);
1795 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1802 /* Verify the INIT packet before we process it. */
1803 int sctp_verify_init(const struct sctp_association *asoc,
1805 sctp_init_chunk_t *peer_init,
1806 struct sctp_chunk *chunk,
1807 struct sctp_chunk **errp)
1809 union sctp_params param;
1812 /* Verify stream values are non-zero. */
1813 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1814 (0 == peer_init->init_hdr.num_inbound_streams) ||
1815 (0 == peer_init->init_hdr.init_tag) ||
1816 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
1818 sctp_process_inv_mandatory(asoc, chunk, errp);
1822 /* Check for missing mandatory parameters. */
1823 sctp_walk_params(param, peer_init, init_hdr.params) {
1825 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1828 } /* for (loop through all parameters) */
1830 /* There is a possibility that a parameter length was bad and
1831 * in that case we would have stoped walking the parameters.
1832 * The current param.p would point at the bad one.
1833 * Current consensus on the mailing list is to generate a PROTOCOL
1834 * VIOLATION error. We build the ERROR chunk here and let the normal
1835 * error handling code build and send the packet.
1837 if (param.v != (void*)chunk->chunk_end) {
1838 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1842 /* The only missing mandatory param possible today is
1843 * the state cookie for an INIT-ACK chunk.
1845 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1846 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1851 /* Find unrecognized parameters. */
1853 sctp_walk_params(param, peer_init, init_hdr.params) {
1855 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1856 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1862 } /* for (loop through all parameters) */
1867 /* Unpack the parameters in an INIT packet into an association.
1868 * Returns 0 on failure, else success.
1869 * FIXME: This is an association method.
1871 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1872 const union sctp_addr *peer_addr,
1873 sctp_init_chunk_t *peer_init, gfp_t gfp)
1875 union sctp_params param;
1876 struct sctp_transport *transport;
1877 struct list_head *pos, *temp;
1880 /* We must include the address that the INIT packet came from.
1881 * This is the only address that matters for an INIT packet.
1882 * When processing a COOKIE ECHO, we retrieve the from address
1883 * of the INIT from the cookie.
1886 /* This implementation defaults to making the first transport
1887 * added as the primary transport. The source address seems to
1888 * be a a better choice than any of the embedded addresses.
1891 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
1895 /* Process the initialization parameters. */
1897 sctp_walk_params(param, peer_init, init_hdr.params) {
1899 if (!sctp_process_param(asoc, param, peer_addr, gfp))
1903 /* Walk list of transports, removing transports in the UNKNOWN state. */
1904 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1905 transport = list_entry(pos, struct sctp_transport, transports);
1906 if (transport->state == SCTP_UNKNOWN) {
1907 sctp_assoc_rm_peer(asoc, transport);
1911 /* The fixed INIT headers are always in network byte
1914 asoc->peer.i.init_tag =
1915 ntohl(peer_init->init_hdr.init_tag);
1916 asoc->peer.i.a_rwnd =
1917 ntohl(peer_init->init_hdr.a_rwnd);
1918 asoc->peer.i.num_outbound_streams =
1919 ntohs(peer_init->init_hdr.num_outbound_streams);
1920 asoc->peer.i.num_inbound_streams =
1921 ntohs(peer_init->init_hdr.num_inbound_streams);
1922 asoc->peer.i.initial_tsn =
1923 ntohl(peer_init->init_hdr.initial_tsn);
1925 /* Apply the upper bounds for output streams based on peer's
1926 * number of inbound streams.
1928 if (asoc->c.sinit_num_ostreams >
1929 ntohs(peer_init->init_hdr.num_inbound_streams)) {
1930 asoc->c.sinit_num_ostreams =
1931 ntohs(peer_init->init_hdr.num_inbound_streams);
1934 if (asoc->c.sinit_max_instreams >
1935 ntohs(peer_init->init_hdr.num_outbound_streams)) {
1936 asoc->c.sinit_max_instreams =
1937 ntohs(peer_init->init_hdr.num_outbound_streams);
1940 /* Copy Initiation tag from INIT to VT_peer in cookie. */
1941 asoc->c.peer_vtag = asoc->peer.i.init_tag;
1943 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
1944 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1946 /* Copy cookie in case we need to resend COOKIE-ECHO. */
1947 cookie = asoc->peer.cookie;
1949 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
1950 if (!asoc->peer.cookie)
1954 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1955 * high (for example, implementations MAY use the size of the receiver
1956 * advertised window).
1958 list_for_each(pos, &asoc->peer.transport_addr_list) {
1959 transport = list_entry(pos, struct sctp_transport, transports);
1960 transport->ssthresh = asoc->peer.i.a_rwnd;
1963 /* Set up the TSN tracking pieces. */
1964 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1965 asoc->peer.i.initial_tsn);
1967 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1969 * The stream sequence number in all the streams shall start
1970 * from 0 when the association is established. Also, when the
1971 * stream sequence number reaches the value 65535 the next
1972 * stream sequence number shall be set to 0.
1975 /* Allocate storage for the negotiated streams if it is not a temporary
1981 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1982 asoc->c.sinit_num_ostreams, gfp);
1986 error = sctp_assoc_set_id(asoc, gfp);
1991 /* ADDIP Section 4.1 ASCONF Chunk Procedures
1993 * When an endpoint has an ASCONF signaled change to be sent to the
1994 * remote endpoint it should do the following:
1996 * A2) A serial number should be assigned to the Chunk. The serial
1997 * number should be a monotonically increasing number. All serial
1998 * numbers are defined to be initialized at the start of the
1999 * association to the same value as the Initial TSN.
2001 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2005 /* Release the transport structures. */
2006 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2007 transport = list_entry(pos, struct sctp_transport, transports);
2009 sctp_transport_free(transport);
2012 asoc->peer.transport_count = 0;
2019 /* Update asoc with the option described in param.
2021 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2023 * asoc is the association to update.
2024 * param is the variable length parameter to use for update.
2025 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2026 * If the current packet is an INIT we want to minimize the amount of
2027 * work we do. In particular, we should not build transport
2028 * structures for the addresses.
2030 static int sctp_process_param(struct sctp_association *asoc,
2031 union sctp_params param,
2032 const union sctp_addr *peer_addr,
2035 union sctp_addr addr;
2043 /* We maintain all INIT parameters in network byte order all the
2044 * time. This allows us to not worry about whether the parameters
2045 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2047 switch (param.p->type) {
2048 case SCTP_PARAM_IPV6_ADDRESS:
2049 if (PF_INET6 != asoc->base.sk->sk_family)
2052 case SCTP_PARAM_IPV4_ADDRESS:
2053 af = sctp_get_af_specific(param_type2af(param.p->type));
2054 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2055 scope = sctp_scope(peer_addr);
2056 if (sctp_in_scope(&addr, scope))
2057 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2061 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2062 if (!sctp_cookie_preserve_enable)
2065 stale = ntohl(param.life->lifespan_increment);
2067 /* Suggested Cookie Life span increment's unit is msec,
2070 asoc->cookie_life.tv_sec += stale / 1000;
2071 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2074 case SCTP_PARAM_HOST_NAME_ADDRESS:
2075 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2078 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2079 /* Turn off the default values first so we'll know which
2080 * ones are really set by the peer.
2082 asoc->peer.ipv4_address = 0;
2083 asoc->peer.ipv6_address = 0;
2085 /* Cycle through address types; avoid divide by 0. */
2086 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2088 sat /= sizeof(__u16);
2090 for (i = 0; i < sat; ++i) {
2091 switch (param.sat->types[i]) {
2092 case SCTP_PARAM_IPV4_ADDRESS:
2093 asoc->peer.ipv4_address = 1;
2096 case SCTP_PARAM_IPV6_ADDRESS:
2097 asoc->peer.ipv6_address = 1;
2100 case SCTP_PARAM_HOST_NAME_ADDRESS:
2101 asoc->peer.hostname_address = 1;
2104 default: /* Just ignore anything else. */
2110 case SCTP_PARAM_STATE_COOKIE:
2111 asoc->peer.cookie_len =
2112 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2113 asoc->peer.cookie = param.cookie->body;
2116 case SCTP_PARAM_HEARTBEAT_INFO:
2117 /* Would be odd to receive, but it causes no problems. */
2120 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2121 /* Rejected during verify stage. */
2124 case SCTP_PARAM_ECN_CAPABLE:
2125 asoc->peer.ecn_capable = 1;
2128 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2129 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
2132 case SCTP_PARAM_FWD_TSN_SUPPORT:
2133 if (sctp_prsctp_enable) {
2134 asoc->peer.prsctp_capable = 1;
2139 /* Any unrecognized parameters should have been caught
2140 * and handled by sctp_verify_param() which should be
2141 * called prior to this routine. Simply log the error
2144 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2145 ntohs(param.p->type), asoc);
2152 /* Select a new verification tag. */
2153 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2155 /* I believe that this random number generator complies with RFC1750.
2156 * A tag of 0 is reserved for special cases (e.g. INIT).
2161 get_random_bytes(&x, sizeof(__u32));
2167 /* Select an initial TSN to send during startup. */
2168 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2172 get_random_bytes(&retval, sizeof(__u32));
2177 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2179 * 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
2180 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2181 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2182 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2184 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2185 * | Address Parameter |
2186 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2187 * | ASCONF Parameter #1 |
2188 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2192 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2193 * | ASCONF Parameter #N |
2194 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2196 * Address Parameter and other parameter will not be wrapped in this function
2198 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2199 union sctp_addr *addr,
2202 sctp_addiphdr_t asconf;
2203 struct sctp_chunk *retval;
2204 int length = sizeof(asconf) + vparam_len;
2205 union sctp_addr_param addrparam;
2207 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2209 addrlen = af->to_addr_param(addr, &addrparam);
2214 /* Create the chunk. */
2215 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2219 asconf.serial = htonl(asoc->addip_serial++);
2221 retval->subh.addip_hdr =
2222 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2223 retval->param_hdr.v =
2224 sctp_addto_chunk(retval, addrlen, &addrparam);
2230 * 3.2.1 Add IP Address
2232 * 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
2233 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2234 * | Type = 0xC001 | Length = Variable |
2235 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2236 * | ASCONF-Request Correlation ID |
2237 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2238 * | Address Parameter |
2239 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2241 * 3.2.2 Delete IP Address
2243 * 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
2244 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2245 * | Type = 0xC002 | Length = Variable |
2246 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2247 * | ASCONF-Request Correlation ID |
2248 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2249 * | Address Parameter |
2250 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2253 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2254 union sctp_addr *laddr,
2255 struct sockaddr *addrs,
2259 sctp_addip_param_t param;
2260 struct sctp_chunk *retval;
2261 union sctp_addr_param addr_param;
2262 union sctp_addr *addr;
2265 int paramlen = sizeof(param);
2266 int addr_param_len = 0;
2270 /* Get total length of all the address parameters. */
2272 for (i = 0; i < addrcnt; i++) {
2273 addr = (union sctp_addr *)addr_buf;
2274 af = sctp_get_af_specific(addr->v4.sin_family);
2275 addr_param_len = af->to_addr_param(addr, &addr_param);
2277 totallen += paramlen;
2278 totallen += addr_param_len;
2280 addr_buf += af->sockaddr_len;
2283 /* Create an asconf chunk with the required length. */
2284 retval = sctp_make_asconf(asoc, laddr, totallen);
2288 /* Add the address parameters to the asconf chunk. */
2290 for (i = 0; i < addrcnt; i++) {
2291 addr = (union sctp_addr *)addr_buf;
2292 af = sctp_get_af_specific(addr->v4.sin_family);
2293 addr_param_len = af->to_addr_param(addr, &addr_param);
2294 param.param_hdr.type = flags;
2295 param.param_hdr.length = htons(paramlen + addr_param_len);
2298 sctp_addto_chunk(retval, paramlen, ¶m);
2299 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2301 addr_buf += af->sockaddr_len;
2307 * 3.2.4 Set Primary IP Address
2309 * 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
2310 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2311 * | Type =0xC004 | Length = Variable |
2312 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2313 * | ASCONF-Request Correlation ID |
2314 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2315 * | Address Parameter |
2316 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2318 * Create an ASCONF chunk with Set Primary IP address parameter.
2320 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2321 union sctp_addr *addr)
2323 sctp_addip_param_t param;
2324 struct sctp_chunk *retval;
2325 int len = sizeof(param);
2326 union sctp_addr_param addrparam;
2328 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2330 addrlen = af->to_addr_param(addr, &addrparam);
2335 /* Create the chunk and make asconf header. */
2336 retval = sctp_make_asconf(asoc, addr, len);
2340 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2341 param.param_hdr.length = htons(len);
2344 sctp_addto_chunk(retval, sizeof(param), ¶m);
2345 sctp_addto_chunk(retval, addrlen, &addrparam);
2350 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2352 * 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
2353 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2354 * | Type = 0x80 | Chunk Flags | Chunk Length |
2355 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2357 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2358 * | ASCONF Parameter Response#1 |
2359 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2363 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2364 * | ASCONF Parameter Response#N |
2365 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2367 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2369 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2370 __u32 serial, int vparam_len)
2372 sctp_addiphdr_t asconf;
2373 struct sctp_chunk *retval;
2374 int length = sizeof(asconf) + vparam_len;
2376 /* Create the chunk. */
2377 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2381 asconf.serial = htonl(serial);
2383 retval->subh.addip_hdr =
2384 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2389 /* Add response parameters to an ASCONF_ACK chunk. */
2390 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2391 __be16 err_code, sctp_addip_param_t *asconf_param)
2393 sctp_addip_param_t ack_param;
2394 sctp_errhdr_t err_param;
2395 int asconf_param_len = 0;
2396 int err_param_len = 0;
2397 __be16 response_type;
2399 if (SCTP_ERROR_NO_ERROR == err_code) {
2400 response_type = SCTP_PARAM_SUCCESS_REPORT;
2402 response_type = SCTP_PARAM_ERR_CAUSE;
2403 err_param_len = sizeof(err_param);
2406 ntohs(asconf_param->param_hdr.length);
2409 /* Add Success Indication or Error Cause Indication parameter. */
2410 ack_param.param_hdr.type = response_type;
2411 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2414 ack_param.crr_id = crr_id;
2415 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2417 if (SCTP_ERROR_NO_ERROR == err_code)
2420 /* Add Error Cause parameter. */
2421 err_param.cause = err_code;
2422 err_param.length = htons(err_param_len + asconf_param_len);
2423 sctp_addto_chunk(chunk, err_param_len, &err_param);
2425 /* Add the failed TLV copied from ASCONF chunk. */
2427 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2430 /* Process a asconf parameter. */
2431 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2432 struct sctp_chunk *asconf,
2433 sctp_addip_param_t *asconf_param)
2435 struct sctp_transport *peer;
2437 union sctp_addr addr;
2438 struct list_head *pos;
2439 union sctp_addr_param *addr_param;
2441 addr_param = (union sctp_addr_param *)
2442 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2444 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2446 return SCTP_ERROR_INV_PARAM;
2448 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2449 switch (asconf_param->param_hdr.type) {
2450 case SCTP_PARAM_ADD_IP:
2451 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2452 * request and does not have the local resources to add this
2453 * new address to the association, it MUST return an Error
2454 * Cause TLV set to the new error code 'Operation Refused
2455 * Due to Resource Shortage'.
2458 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2460 return SCTP_ERROR_RSRC_LOW;
2462 /* Start the heartbeat timer. */
2463 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2464 sctp_transport_hold(peer);
2466 case SCTP_PARAM_DEL_IP:
2467 /* ADDIP 4.3 D7) If a request is received to delete the
2468 * last remaining IP address of a peer endpoint, the receiver
2469 * MUST send an Error Cause TLV with the error cause set to the
2470 * new error code 'Request to Delete Last Remaining IP Address'.
2472 pos = asoc->peer.transport_addr_list.next;
2473 if (pos->next == &asoc->peer.transport_addr_list)
2474 return SCTP_ERROR_DEL_LAST_IP;
2476 /* ADDIP 4.3 D8) If a request is received to delete an IP
2477 * address which is also the source address of the IP packet
2478 * which contained the ASCONF chunk, the receiver MUST reject
2479 * this request. To reject the request the receiver MUST send
2480 * an Error Cause TLV set to the new error code 'Request to
2481 * Delete Source IP Address'
2483 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2484 return SCTP_ERROR_DEL_SRC_IP;
2486 sctp_assoc_del_peer(asoc, &addr);
2488 case SCTP_PARAM_SET_PRIMARY:
2489 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2491 return SCTP_ERROR_INV_PARAM;
2493 sctp_assoc_set_primary(asoc, peer);
2496 return SCTP_ERROR_INV_PARAM;
2500 return SCTP_ERROR_NO_ERROR;
2503 /* Verify the ASCONF packet before we process it. */
2504 int sctp_verify_asconf(const struct sctp_association *asoc,
2505 struct sctp_paramhdr *param_hdr, void *chunk_end,
2506 struct sctp_paramhdr **errp) {
2507 sctp_addip_param_t *asconf_param;
2508 union sctp_params param;
2511 param.v = (sctp_paramhdr_t *) param_hdr;
2512 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2513 length = ntohs(param.p->length);
2516 if (param.v > chunk_end - length ||
2517 length < sizeof(sctp_paramhdr_t))
2520 switch (param.p->type) {
2521 case SCTP_PARAM_ADD_IP:
2522 case SCTP_PARAM_DEL_IP:
2523 case SCTP_PARAM_SET_PRIMARY:
2524 asconf_param = (sctp_addip_param_t *)param.v;
2525 plen = ntohs(asconf_param->param_hdr.length);
2526 if (plen < sizeof(sctp_addip_param_t) +
2527 sizeof(sctp_paramhdr_t))
2530 case SCTP_PARAM_SUCCESS_REPORT:
2531 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2532 if (length != sizeof(sctp_addip_param_t))
2540 param.v += WORD_ROUND(length);
2543 if (param.v != chunk_end)
2549 /* Process an incoming ASCONF chunk with the next expected serial no. and
2550 * return an ASCONF_ACK chunk to be sent in response.
2552 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2553 struct sctp_chunk *asconf)
2555 sctp_addiphdr_t *hdr;
2556 union sctp_addr_param *addr_param;
2557 sctp_addip_param_t *asconf_param;
2558 struct sctp_chunk *asconf_ack;
2562 int chunk_len = asconf->skb->len;
2564 int all_param_pass = 1;
2566 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2567 serial = ntohl(hdr->serial);
2569 /* Skip the addiphdr and store a pointer to address parameter. */
2570 length = sizeof(sctp_addiphdr_t);
2571 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2572 chunk_len -= length;
2574 /* Skip the address parameter and store a pointer to the first
2577 length = ntohs(addr_param->v4.param_hdr.length);
2578 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2579 chunk_len -= length;
2581 /* create an ASCONF_ACK chunk.
2582 * Based on the definitions of parameters, we know that the size of
2583 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2586 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2590 /* Process the TLVs contained within the ASCONF chunk. */
2591 while (chunk_len > 0) {
2592 err_code = sctp_process_asconf_param(asoc, asconf,
2595 * If an error response is received for a TLV parameter,
2596 * all TLVs with no response before the failed TLV are
2597 * considered successful if not reported. All TLVs after
2598 * the failed response are considered unsuccessful unless
2599 * a specific success indication is present for the parameter.
2601 if (SCTP_ERROR_NO_ERROR != err_code)
2604 if (!all_param_pass)
2605 sctp_add_asconf_response(asconf_ack,
2606 asconf_param->crr_id, err_code,
2609 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2610 * an IP address sends an 'Out of Resource' in its response, it
2611 * MUST also fail any subsequent add or delete requests bundled
2614 if (SCTP_ERROR_RSRC_LOW == err_code)
2617 /* Move to the next ASCONF param. */
2618 length = ntohs(asconf_param->param_hdr.length);
2619 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2621 chunk_len -= length;
2625 asoc->peer.addip_serial++;
2627 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2628 * after freeing the reference to old asconf ack if any.
2631 if (asoc->addip_last_asconf_ack)
2632 sctp_chunk_free(asoc->addip_last_asconf_ack);
2634 sctp_chunk_hold(asconf_ack);
2635 asoc->addip_last_asconf_ack = asconf_ack;
2641 /* Process a asconf parameter that is successfully acked. */
2642 static int sctp_asconf_param_success(struct sctp_association *asoc,
2643 sctp_addip_param_t *asconf_param)
2646 union sctp_addr addr;
2647 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2648 union sctp_addr_param *addr_param;
2649 struct list_head *pos;
2650 struct sctp_transport *transport;
2651 struct sctp_sockaddr_entry *saddr;
2654 addr_param = (union sctp_addr_param *)
2655 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2657 /* We have checked the packet before, so we do not check again. */
2658 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2659 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
2661 switch (asconf_param->param_hdr.type) {
2662 case SCTP_PARAM_ADD_IP:
2663 /* This is always done in BH context with a socket lock
2664 * held, so the list can not change.
2666 list_for_each_entry(saddr, &bp->address_list, list) {
2667 if (sctp_cmp_addr_exact(&saddr->a, &addr))
2668 saddr->use_as_src = 1;
2671 case SCTP_PARAM_DEL_IP:
2672 retval = sctp_del_bind_addr(bp, &addr, call_rcu_bh);
2673 list_for_each(pos, &asoc->peer.transport_addr_list) {
2674 transport = list_entry(pos, struct sctp_transport,
2676 dst_release(transport->dst);
2677 sctp_transport_route(transport, NULL,
2678 sctp_sk(asoc->base.sk));
2688 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2689 * for the given asconf parameter. If there is no response for this parameter,
2690 * return the error code based on the third argument 'no_err'.
2692 * A7) If an error response is received for a TLV parameter, all TLVs with no
2693 * response before the failed TLV are considered successful if not reported.
2694 * All TLVs after the failed response are considered unsuccessful unless a
2695 * specific success indication is present for the parameter.
2697 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2698 sctp_addip_param_t *asconf_param,
2701 sctp_addip_param_t *asconf_ack_param;
2702 sctp_errhdr_t *err_param;
2704 int asconf_ack_len = asconf_ack->skb->len;
2708 err_code = SCTP_ERROR_NO_ERROR;
2710 err_code = SCTP_ERROR_REQ_REFUSED;
2712 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2713 * the first asconf_ack parameter.
2715 length = sizeof(sctp_addiphdr_t);
2716 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2718 asconf_ack_len -= length;
2720 while (asconf_ack_len > 0) {
2721 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2722 switch(asconf_ack_param->param_hdr.type) {
2723 case SCTP_PARAM_SUCCESS_REPORT:
2724 return SCTP_ERROR_NO_ERROR;
2725 case SCTP_PARAM_ERR_CAUSE:
2726 length = sizeof(sctp_addip_param_t);
2727 err_param = (sctp_errhdr_t *)
2728 ((void *)asconf_ack_param + length);
2729 asconf_ack_len -= length;
2730 if (asconf_ack_len > 0)
2731 return err_param->cause;
2733 return SCTP_ERROR_INV_PARAM;
2736 return SCTP_ERROR_INV_PARAM;
2740 length = ntohs(asconf_ack_param->param_hdr.length);
2741 asconf_ack_param = (sctp_addip_param_t *)
2742 ((void *)asconf_ack_param + length);
2743 asconf_ack_len -= length;
2749 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2750 int sctp_process_asconf_ack(struct sctp_association *asoc,
2751 struct sctp_chunk *asconf_ack)
2753 struct sctp_chunk *asconf = asoc->addip_last_asconf;
2754 union sctp_addr_param *addr_param;
2755 sctp_addip_param_t *asconf_param;
2757 int asconf_len = asconf->skb->len;
2758 int all_param_pass = 0;
2761 __be16 err_code = SCTP_ERROR_NO_ERROR;
2763 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2764 * a pointer to address parameter.
2766 length = sizeof(sctp_addip_chunk_t);
2767 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2768 asconf_len -= length;
2770 /* Skip the address parameter in the last asconf sent and store a
2771 * pointer to the first asconf paramter.
2773 length = ntohs(addr_param->v4.param_hdr.length);
2774 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2775 asconf_len -= length;
2778 * A8) If there is no response(s) to specific TLV parameter(s), and no
2779 * failures are indicated, then all request(s) are considered
2782 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2785 /* Process the TLVs contained in the last sent ASCONF chunk. */
2786 while (asconf_len > 0) {
2788 err_code = SCTP_ERROR_NO_ERROR;
2790 err_code = sctp_get_asconf_response(asconf_ack,
2793 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2798 case SCTP_ERROR_NO_ERROR:
2799 retval = sctp_asconf_param_success(asoc, asconf_param);
2802 case SCTP_ERROR_RSRC_LOW:
2806 case SCTP_ERROR_INV_PARAM:
2807 /* Disable sending this type of asconf parameter in
2810 asoc->peer.addip_disabled_mask |=
2811 asconf_param->param_hdr.type;
2814 case SCTP_ERROR_REQ_REFUSED:
2815 case SCTP_ERROR_DEL_LAST_IP:
2816 case SCTP_ERROR_DEL_SRC_IP:
2821 /* Skip the processed asconf parameter and move to the next
2824 length = ntohs(asconf_param->param_hdr.length);
2825 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2827 asconf_len -= length;
2830 /* Free the cached last sent asconf chunk. */
2831 sctp_chunk_free(asconf);
2832 asoc->addip_last_asconf = NULL;
2834 /* Send the next asconf chunk from the addip chunk queue. */
2835 if (!list_empty(&asoc->addip_chunk_list)) {
2836 struct list_head *entry = asoc->addip_chunk_list.next;
2837 asconf = list_entry(entry, struct sctp_chunk, list);
2839 list_del_init(entry);
2841 /* Hold the chunk until an ASCONF_ACK is received. */
2842 sctp_chunk_hold(asconf);
2843 if (sctp_primitive_ASCONF(asoc, asconf))
2844 sctp_chunk_free(asconf);
2846 asoc->addip_last_asconf = asconf;
2852 /* Make a FWD TSN chunk. */
2853 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2854 __u32 new_cum_tsn, size_t nstreams,
2855 struct sctp_fwdtsn_skip *skiplist)
2857 struct sctp_chunk *retval = NULL;
2858 struct sctp_fwdtsn_chunk *ftsn_chunk;
2859 struct sctp_fwdtsn_hdr ftsn_hdr;
2860 struct sctp_fwdtsn_skip skip;
2864 hint = (nstreams + 1) * sizeof(__u32);
2866 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2871 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2873 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2874 retval->subh.fwdtsn_hdr =
2875 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2877 for (i = 0; i < nstreams; i++) {
2878 skip.stream = skiplist[i].stream;
2879 skip.ssn = skiplist[i].ssn;
2880 sctp_addto_chunk(retval, sizeof(skip), &skip);