Prep-work patch for congestion-control
[vpp.git] / src / vnet / sctp / sctp_packet.h
1 /*
2  * Copyright (c) 2017 SUSE LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef included_vnet_sctp_packet_h
16 #define included_vnet_sctp_packet_h
17
18 #include <stdbool.h>
19
20 #include <vnet/ip/ip4_packet.h>
21 #include <vnet/ip/ip6_packet.h>
22
23 /*
24  * As per RFC 4960
25  * https://tools.ietf.org/html/rfc4960
26  */
27
28 /*
29  * 0                   1                   2                   3
30  * 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
31  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32  * |     Source Port Number        |     Destination Port Number   |
33  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  * |                      Verification Tag                         |
35  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36  * |                           Checksum                            |
37  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38  */
39 typedef struct
40 {
41   /*
42    * This is the SCTP sender's port number. It can be used by the
43    * receiver in combination with the source IP address, the SCTP
44    * destination port, and possibly the destination IP address to
45    * identify the association to which this packet belongs.
46    * The port number 0 MUST NOT be used.
47    */
48   u16 src_port;
49
50   /*
51    * This is the SCTP port number to which this packet is destined.
52    * The receiving host will use this port number to de-multiplex the
53    * SCTP packet to the correct receiving endpoint/application.
54    * The port number 0 MUST NOT be used.
55    */
56   u16 dst_port;
57
58   /*
59    * The receiver of this packet uses the Verification Tag to validate
60    * the sender of this SCTP packet.  On transmit, the value of this
61    * Verification Tag MUST be set to the value of the Initiate Tag
62    * received from the peer endpoint during the association
63    * initialization, with the following exceptions:
64    * - A packet containing an INIT chunk MUST have a zero Verification
65    *   Tag.
66    * - A packet containing a SHUTDOWN COMPLETE chunk with the T bit
67    *   set MUST have the Verification Tag copied from the packet with
68    *   the SHUTDOWN ACK chunk.
69    * - A packet containing an ABORT chunk may have the verification tag
70    *   copied from the packet that caused the ABORT to be sent.
71    * An INIT chunk MUST be the only chunk in the SCTP packet carrying it.
72    */
73   u32 verification_tag;
74
75   /*
76    * This field contains the checksum of this SCTP packet.
77    * SCTP uses the CRC32c algorithm.
78    */
79   u32 checksum;
80
81 } sctp_header_t;
82
83 always_inline void
84 vnet_set_sctp_src_port (sctp_header_t * h, u16 src_port)
85 {
86   h->src_port = clib_host_to_net_u16 (src_port);
87 }
88
89 always_inline u16
90 vnet_get_sctp_src_port (sctp_header_t * h)
91 {
92   return (clib_net_to_host_u16 (h->src_port));
93 }
94
95 always_inline void
96 vnet_set_sctp_dst_port (sctp_header_t * h, u16 dst_port)
97 {
98   h->dst_port = clib_host_to_net_u16 (dst_port);
99 }
100
101 always_inline u16
102 vnet_get_sctp_dst_port (sctp_header_t * h)
103 {
104   return (clib_net_to_host_u16 (h->dst_port));
105 }
106
107 always_inline void
108 vnet_set_sctp_verification_tag (sctp_header_t * h, u32 verification_tag)
109 {
110   h->verification_tag = clib_host_to_net_u32 (verification_tag);
111 }
112
113 always_inline u32
114 vnet_get_sctp_verification_tag (sctp_header_t * h)
115 {
116   return (clib_net_to_host_u32 (h->verification_tag));
117 }
118
119 always_inline void
120 vnet_set_sctp_checksum (sctp_header_t * h, u32 checksum)
121 {
122   h->checksum = clib_host_to_net_u32 (checksum);
123 }
124
125 always_inline u32
126 vnet_get_sctp_checksum (sctp_header_t * h)
127 {
128   return (clib_net_to_host_u32 (h->checksum));
129 }
130
131 /*
132  * Multiple chunks can be bundled into one SCTP packet up to the MTU
133  * size, except for the INIT, INIT ACK, and SHUTDOWN COMPLETE chunks.
134  * These chunks MUST NOT be bundled with any other chunk in a packet.
135  *
136  *
137  * 0                   1                   2                   3
138  * 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
139  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140  * |                        Common Header                          |
141  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142  * |                          Chunk #1                             |
143  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144  * |                           ...                                 |
145  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146  * |                          Chunk #n                             |
147  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148  */
149
150 typedef enum
151 {
152   DATA = 0,
153   INIT,
154   INIT_ACK,
155   SACK,
156   HEARTBEAT,
157   HEARTBEAT_ACK,
158   ABORT,
159   SHUTDOWN,
160   SHUTDOWN_ACK,
161   OPERATION_ERROR,
162   COOKIE_ECHO,
163   COOKIE_ACK,
164   ECNE,
165   CWR,
166   SHUTDOWN_COMPLETE
167 } sctp_chunk_type;
168
169 /*
170  * 0                   1                   2                   3
171  * 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
172  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
173  * |   Chunk Type  | Chunk  Flags  |        Chunk Length           |
174  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
175  */
176 typedef struct
177 {
178   /*
179    * This field identifies the type of information contained in the
180    * Chunk Value field. It takes a value from 0 to 254.
181    * The value of 255 is reserved for future use as an extension field.
182    *
183    * The values of Chunk Types are defined as follows:
184    * ID Value    Chunk Type
185    * -----       ----------
186    *  0          - Payload Data (DATA)
187    *  1          - Initiation (INIT)
188    *  2          - Initiation Acknowledgement (INIT ACK)
189    *  3          - Selective Acknowledgement (SACK)
190    *  4          - Heartbeat Request (HEARTBEAT)
191    *  5          - Heartbeat Acknowledgement (HEARTBEAT ACK)
192    *  6          - Abort (ABORT)
193    *  7          - Shutdown (SHUTDOWN)
194    *  8          - Shutdown Acknowledgement (SHUTDOWN ACK)
195    *  9          - Operation Error (ERROR)
196    *  10         - State Cookie (COOKIE ECHO)
197    *  11         - Cookie Acknowledgement (COOKIE ACK)
198    *  12         - Reserved for Explicit Congestion Notification Echo (ECNE)
199    *  13         - Reserved for Congestion Window Reduced (CWR)
200    *  14         - Shutdown Complete (SHUTDOWN COMPLETE)
201    *  15 to 62   - available
202    *  63         - reserved for IETF-defined Chunk Extensions
203    *  64 to 126  - available
204    *  127        - reserved for IETF-defined Chunk Extensions
205    *  128 to 190 - available
206    *  191        - reserved for IETF-defined Chunk Extensions
207    *  192 to 254 - available
208    *  255        - reserved for IETF-defined Chunk Extensions
209    *
210    *  Chunk Types are encoded such that the highest-order 2 bits specify
211    *  the action that must be taken if the processing endpoint does not
212    *  recognize the Chunk Type.
213    *  00 -  Stop processing this SCTP packet and discard it, do not
214    *  process any further chunks within it.
215    *  01 -  Stop processing this SCTP packet and discard it, do not
216    *  process any further chunks within it, and report the
217    *  unrecognized chunk in an 'Unrecognized Chunk Type'.
218    *  10 -  Skip this chunk and continue processing.
219    *  11 -  Skip this chunk and continue processing, but report in an
220    *  ERROR chunk using the 'Unrecognized Chunk Type' cause of error.
221    *
222    *  Note: The ECNE and CWR chunk types are reserved for future use of
223    *  Explicit Congestion Notification (ECN);
224    */
225   //u8 type;
226
227   /*
228    * The usage of these bits depends on the Chunk type as given by the
229    * Chunk Type field.  Unless otherwise specified, they are set to 0 on
230    * transmit and are ignored on receipt.
231    */
232   //u8 flags;
233
234   /*
235    * This value represents the size of the chunk in bytes, including
236    * the Chunk Type, Chunk Flags, Chunk Length, and Chunk Value fields.
237    * Therefore, if the Chunk Value field is zero-length, the Length
238    * field will be set to 4.
239    * The Chunk Length field does not count any chunk padding.
240    * Chunks (including Type, Length, and Value fields) are padded out
241    * by the sender with all zero bytes to be a multiple of 4 bytes
242    * long. This padding MUST NOT be more than 3 bytes in total. The
243    * Chunk Length value does not include terminating padding of the
244    * chunk. However, it does include padding of any variable-length
245    * parameter except the last parameter in the chunk. The receiver
246    * MUST ignore the padding.
247    *
248    * Note: A robust implementation should accept the chunk whether or
249    * not the final padding has been included in the Chunk Length.
250    */
251   //u16 length;
252
253   u32 params;
254
255 } sctp_chunks_common_hdr_t;
256
257 typedef struct
258 {
259   sctp_header_t hdr;
260   sctp_chunks_common_hdr_t common_hdr;
261
262 } sctp_full_hdr_t;
263
264 #define CHUNK_TYPE_MASK 0xFF000000
265 #define CHUNK_TYPE_SHIFT 24
266
267 #define CHUNK_FLAGS_MASK 0x00FF0000
268 #define CHUNK_FLAGS_SHIFT 16
269
270 #define CHUNK_LENGTH_MASK 0x0000FFFF
271 #define CHUNK_LENGTH_SHIFT 0
272
273 always_inline void
274 vnet_sctp_common_hdr_params_host_to_net (sctp_chunks_common_hdr_t * h)
275 {
276   h->params = clib_host_to_net_u32 (h->params);
277 }
278
279 always_inline void
280 vnet_sctp_common_hdr_params_net_to_host (sctp_chunks_common_hdr_t * h)
281 {
282   h->params = clib_net_to_host_u32 (h->params);
283 }
284
285 always_inline void
286 vnet_sctp_set_chunk_type (sctp_chunks_common_hdr_t * h, sctp_chunk_type t)
287 {
288   h->params &= ~(CHUNK_TYPE_MASK);
289   h->params |= (t << CHUNK_TYPE_SHIFT) & CHUNK_TYPE_MASK;
290 }
291
292 always_inline u8
293 vnet_sctp_get_chunk_type (sctp_chunks_common_hdr_t * h)
294 {
295   return ((h->params & CHUNK_TYPE_MASK) >> CHUNK_TYPE_SHIFT);
296 }
297
298 always_inline void
299 vnet_sctp_set_chunk_length (sctp_chunks_common_hdr_t * h, u16 length)
300 {
301   h->params &= ~(CHUNK_LENGTH_MASK);
302   h->params |= (length << CHUNK_LENGTH_SHIFT) & CHUNK_LENGTH_MASK;
303 }
304
305 always_inline u16
306 vnet_sctp_get_chunk_length (sctp_chunks_common_hdr_t * h)
307 {
308   return ((h->params & CHUNK_LENGTH_MASK) >> CHUNK_LENGTH_SHIFT);
309 }
310
311 /*
312  * Payload chunk
313  *
314  * 0                   1                   2                   3
315  * 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
316  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
317  * |   Type = 0    | Reserved|U|B|E|    Length                     |
318  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
319  * |                              TSN                              |
320  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
321  * |      Stream Identifier S      |   Stream Sequence Number n    |
322  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
323  * |                  Payload Protocol Identifier                  |
324  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
325  * \                                                               \
326  * /                 User Data (seq n of Stream S)                 /
327  * \                                                               \
328  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
329  */
330 typedef struct
331 {
332   sctp_header_t sctp_hdr;
333   /*
334    * Type (8 bits): 0
335    * Flags (8 bits):
336    * -- Reserved (5 bits): all 0s
337    * -- U bit
338    * -- B bit
339    * -- E bit
340    * Length (16 bits): This field indicates the length of the DATA chunk in
341    * bytes from the beginning of the type field to the end of the User Data
342    * field excluding any padding.
343    * A DATA chunk with one byte of user data will have Length set to 17
344    * (indicating 17 bytes). A DATA chunk with a User Data field of length L
345    * will have the Length field set to (16 + L) (indicating 16+L bytes) where
346    * L MUST be greater than 0.
347    */
348
349   /*
350    * Fragment Description Table:
351    *
352    *    B E                  Description
353    * ============================================================
354    * |  1 0 | First piece of a fragmented user message          |
355    * +----------------------------------------------------------+
356    * |  0 0 | Middle piece of a fragmented user message         |
357    * +----------------------------------------------------------+
358    * |  0 1 | Last piece of a fragmented user message           |
359    * +----------------------------------------------------------+
360    * |  1 1 | Unfragmented message                              |
361    * ============================================================
362    */
363   sctp_chunks_common_hdr_t chunk_hdr;
364
365   /*
366    * This value represents the TSN for this DATA chunk.
367    * The valid range of TSN is from 0 to 4294967295 (2**32 - 1).
368    * TSN wraps back to 0 after reaching 4294967295.
369    */
370   u32 tsn;
371
372   /*
373    * Identifies the stream to which the following user data belongs.
374    */
375   u16 stream_id;
376
377   /*
378    * This value represents the Stream Sequence Number of the following user data
379    * within the stream S. Valid range is 0 to 65535.
380    * When a user message is fragmented by SCTP for transport, the same Stream
381    * Sequence Number MUST be carried in each of the fragments of the message.
382    */
383   u16 stream_seq;
384
385   /*
386    * This value represents an application (or upper layer) specified protocol
387    * identifier. This value is passed to SCTP by its upper layer and sent to its
388    * peer. This identifier is not used by SCTP but can be used by certain network
389    * entities, as well as by the peer application, to identify the type of
390    * information being carried in this DATA chunk. This field must be sent even
391    * in fragmented DATA chunks (to make sure it is available for agents in the
392    * middle of the network).  Note that this field is NOT touched by an SCTP
393    * implementation; therefore, its byte order is NOT necessarily big endian.
394    * The upper layer is responsible for any byte order conversions to this field.
395    * The value 0 indicates that no application identifier is specified by the
396    * upper layer for this payload data.
397    */
398   u32 payload_id;
399
400   /*
401    * This is the payload user data. The implementation MUST pad the end of the
402    * data to a 4-byte boundary with all-zero bytes. Any padding MUST NOT be
403    * included in the Length field. A sender MUST never add more than 3 bytes of
404    * padding.
405    */
406   u32 data[];
407
408 } sctp_payload_data_chunk_t;
409
410 always_inline void
411 vnet_sctp_set_ebit (sctp_payload_data_chunk_t * p, u8 enable)
412 {
413   //p->chunk_hdr.flags = clib_host_to_net_u16 (enable);
414 }
415
416 always_inline u8
417 vnet_sctp_get_ebit (sctp_payload_data_chunk_t * p)
418 {
419   //return (clib_net_to_host_u16 (p->chunk_hdr.flags));
420   return 0;
421 }
422
423 always_inline void
424 vnet_sctp_set_bbit (sctp_payload_data_chunk_t * p, u8 enable)
425 {
426   //p->chunk_hdr.flags = clib_host_to_net_u16 (enable << 1);
427 }
428
429 always_inline u8
430 vnet_sctp_get_bbit (sctp_payload_data_chunk_t * p)
431 {
432   //return (clib_net_to_host_u16 (p->chunk_hdr.flags >> 1));
433   return 0;
434 }
435
436 always_inline void
437 vnet_sctp_set_ubit (sctp_payload_data_chunk_t * p, u8 enable)
438 {
439   //p->chunk_hdr.flags = clib_host_to_net_u16 (enable << 2);
440 }
441
442 always_inline u8
443 vnet_sctp_get_ubit (sctp_payload_data_chunk_t * p)
444 {
445   //return (clib_net_to_host_u16 (p->chunk_hdr.flags >> 2));
446   return 0;
447 }
448
449 always_inline void
450 vnet_sctp_set_tsn (sctp_payload_data_chunk_t * p, u32 tsn)
451 {
452   p->tsn = clib_host_to_net_u32 (tsn);
453 }
454
455 always_inline u32
456 vnet_sctp_get_tsn (sctp_payload_data_chunk_t * p)
457 {
458   return (clib_net_to_host_u32 (p->tsn));
459 }
460
461 always_inline void
462 vnet_sctp_set_stream_id (sctp_payload_data_chunk_t * p, u16 stream_id)
463 {
464   p->stream_id = clib_host_to_net_u16 (stream_id);
465 }
466
467 always_inline u16
468 vnet_sctp_get_stream_id (sctp_payload_data_chunk_t * p)
469 {
470   return (clib_net_to_host_u16 (p->stream_id));
471 }
472
473 always_inline void
474 vnet_sctp_set_stream_seq (sctp_payload_data_chunk_t * p, u16 stream_seq)
475 {
476   p->stream_seq = clib_host_to_net_u16 (stream_seq);
477 }
478
479 always_inline u16
480 vnet_sctp_get_stream_seq (sctp_payload_data_chunk_t * p)
481 {
482   return (clib_net_to_host_u16 (p->stream_seq));
483 }
484
485 always_inline void
486 vnet_sctp_set_payload_id (sctp_payload_data_chunk_t * p, u32 payload_id)
487 {
488   p->payload_id = clib_host_to_net_u32 (payload_id);
489 }
490
491 always_inline u32
492 vnet_sctp_get_payload_id (sctp_payload_data_chunk_t * p)
493 {
494   return (clib_net_to_host_u32 (p->payload_id));
495 }
496
497 always_inline u16
498 vnet_sctp_calculate_padding (u16 base_length)
499 {
500   if (base_length % 4 == 0)
501     return 0;
502
503   return (4 - base_length % 4);
504 }
505
506 #define DEFAULT_A_RWND 1480
507 #define INBOUND_STREAMS_COUNT 1
508 #define OUTBOUND_STREAMS_COUNT 1
509
510 /*
511  * INIT chunk
512  *
513  * This chunk is used to initiate an SCTP association between two
514  * endpoints.
515  *
516  * 0                   1                   2                   3
517  * 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
518  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
519  * |   Type = 1    |  Chunk Flags  |      Chunk Length             |
520  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
521  * |                         Initiate Tag                          |
522  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
523  * |           Advertised Receiver Window Credit (a_rwnd)          |
524  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
525  * |  Number of Outbound Streams   |  Number of Inbound Streams    |
526  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
527  * |                          Initial TSN                          |
528  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
529  * \                                                               \
530  * /              Optional/Variable-Length Parameters              /
531  * \                                                               \
532  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
533  *
534  * The INIT chunk contains the following parameters. Unless otherwise
535  * noted, each parameter MUST only be included once in the INIT chunk.
536  *
537  * Fixed Parameters                     Status
538  * ----------------------------------------------
539  *  Initiate Tag                        Mandatory
540  *  Advertised Receiver Window Credit   Mandatory
541  *  Number of Outbound Streams          Mandatory
542  *  Number of Inbound Streams           Mandatory
543  *  Initial TSN                         Mandatory
544  *
545  * Variable Parameters                  Status     Type Value
546  * -------------------------------------------------------------
547  *  IPv4 Address (Note 1)               Optional    5
548  *  IPv6 Address (Note 1)               Optional    6
549  *  Cookie Preservative                 Optional    9
550  *  Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
551  *  Host Name Address (Note 3)          Optional    11
552  *  Supported Address Types (Note 4)    Optional    12
553  *
554  * Note 1: The INIT chunks can contain multiple addresses that can be
555  * IPv4 and/or IPv6 in any combination.
556  *
557  * Note 2: The ECN Capable field is reserved for future use of Explicit
558  * Congestion Notification.
559  *
560  * Note 3: An INIT chunk MUST NOT contain more than one Host Name Address
561  * parameter. Moreover, the sender of the INIT MUST NOT combine any other
562  * address types with the Host Name Address in the INIT. The receiver of
563  * INIT MUST ignore any other address types if the Host Name Address parameter
564  * is present in the received INIT chunk.
565  *
566  * Note 4: This parameter, when present, specifies all the address types the
567  * sending endpoint can support.  The absence of this parameter indicates that
568  * the sending endpoint can support any address type.
569  *
570  * IMPLEMENTATION NOTE: If an INIT chunk is received with known parameters that
571  * are not optional parameters of the INIT chunk, then the receiver SHOULD
572  * process the INIT chunk and send back an INIT ACK. The receiver of the INIT
573  * chunk MAY bundle an ERROR chunk with the COOKIE ACK chunk later.
574  * However, restrictive implementations MAY send back an ABORT chunk in response
575  * to the INIT chunk. The Chunk Flags field in INIT is reserved, and all bits
576  * in it should be set to 0 by the sender and ignored by the receiver.
577  * The sequence of parameters within an INIT can be processed in any order.
578  */
579 typedef struct
580 {
581   sctp_header_t sctp_hdr;
582   sctp_chunks_common_hdr_t chunk_hdr;
583
584   /*
585    * The receiver of the INIT (the responding end) records the value of
586    * the Initiate Tag parameter.
587    * This value MUST be placed into the Verification Tag field of every
588    * SCTP packet that the receiver of the INIT transmits within this association.
589    * The Initiate Tag is allowed to have any value except 0.
590    *
591    * If the value of the Initiate Tag in a received INIT chunk is found
592    * to be 0, the receiver MUST treat it as an error and close the
593    * association by transmitting an ABORT.
594    *
595    * The value of the INIT TAG is recommended to be random for security
596    * reasons. A good method is described in https://tools.ietf.org/html/rfc4086
597    */
598   u32 initiate_tag;
599
600   /*
601    * This value represents the dedicated buffer space, in number of bytes,
602    * the sender of the INIT has reserved in association with this window.
603    * During the life of the association, this buffer space SHOULD NOT be
604    * lessened (i.e., dedicated buffers taken away from this association);
605    * however, an endpoint MAY change the value of a_rwnd it sends in SACK
606    * chunks.
607    */
608   u32 a_rwnd;
609
610   /*
611    * Defines the number of outbound streams the sender of this INIT chunk
612    * wishes to create in this association.
613    * The value of 0 MUST NOT be used.
614    *
615    * Note: A receiver of an INIT with the OS value set to 0 SHOULD abort
616    * the association.
617    */
618   u16 outbound_streams_count;
619
620   /*
621    * Defines the maximum number of streams the sender of this INIT
622    * chunk allows the peer end to create in this association.
623    * The value 0 MUST NOT be used.
624    *
625    * Note: There is no negotiation of the actual number of streams but
626    * instead the two endpoints will use the min(requested, offered).
627    *
628    * Note: A receiver of an INIT with the MIS value of 0 SHOULD abort
629    * the association.
630    */
631   u16 inboud_streams_count;
632
633   /*
634    * Defines the initial TSN that the sender will use.
635    * The valid range is from 0 to 4294967295.
636    * This field MAY be set to the value of the Initiate Tag field.
637    */
638   u32 initial_tsn;
639
640   /* The following field allows to have multiple optional fields which are:
641    * - sctp_ipv4_address
642    * - sctp_ipv6_address
643    * - sctp_cookie_preservative
644    * - sctp_hostname_address
645    * - sctp_supported_address_types
646    */
647   u32 optional_fields[];
648
649 } sctp_init_chunk_t;
650
651 /*
652  * INIT ACK chunk
653  *
654  * The INIT ACK chunk is used to acknowledge the initiation of an SCTP
655  * association. The parameter part of INIT ACK is formatted similarly to the
656  * INIT chunk.
657  *
658  * It uses two extra variable parameters:
659  * - the State Cookie and
660  * - the Unrecognized Parameter:
661  */
662 /*
663  * 0                   1                   2                   3
664  * 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
665  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
666  * |   Type = 2    |  Chunk Flags  |      Chunk Length             |
667  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
668  * |                         Initiate Tag                          |
669  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
670  * |              Advertised Receiver Window Credit                |
671  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
672  * |  Number of Outbound Streams   |  Number of Inbound Streams    |
673  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
674  * |                          Initial TSN                          |
675  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
676  * \                                                               \
677  * /              Optional/Variable-Length Parameters              /
678  * \                                                               \
679  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
680  */
681 typedef sctp_init_chunk_t sctp_init_ack_chunk_t;
682
683 /*
684  * 0                   1                   2                   3
685  * 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
686  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
687  * |          Parameter Type       |       Parameter Length        |
688  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
689  * \                                                               \
690  * /                       Parameter Value                         /
691  * \                                                               \
692  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693  */
694 typedef struct
695 {
696   u16 type;
697   u16 length;
698
699 } sctp_opt_params_hdr_t;
700
701 typedef struct
702 {
703   sctp_opt_params_hdr_t param_hdr;
704
705   u64 mac;                      /* RFC 2104 */
706   u32 creation_time;
707   u32 cookie_lifespan;
708
709 } sctp_state_cookie_param_t;
710
711 /*
712  *  This chunk is used only during the initialization of an association.
713  *  It is sent by the initiator of an association to its peer to complete
714  *  the initialization process.  This chunk MUST precede any DATA chunk
715  *  sent within the association, but MAY be bundled with one or more DATA
716  *  chunks in the same packet.
717  *
718  *  0                   1                   2                   3
719  *  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
720  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
721  *  |   Type = 10   |Chunk  Flags   |         Length                |
722  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
723  *  /                     Cookie                                    /
724  *  \                                                               \
725  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
726  */
727 typedef struct
728 {
729   sctp_header_t sctp_hdr;
730   sctp_chunks_common_hdr_t chunk_hdr;
731
732   sctp_state_cookie_param_t cookie;
733
734 } sctp_cookie_echo_chunk_t;
735
736
737 /*
738  * 0                   1                   2                   3
739  * 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
740  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
741  * |   Type = 11   |Chunk  Flags   |     Length = 4                |
742  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
743  */
744 typedef struct
745 {
746   sctp_header_t sctp_hdr;
747   sctp_chunks_common_hdr_t chunk_hdr;
748
749 } sctp_cookie_ack_chunk_t;
750
751 /*
752  * 0                   1                   2                   3
753  * 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
754  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
755  * |   Type = 14   |Chunk  Flags   |     Length = 4                |
756  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
757  */
758 typedef struct
759 {
760   sctp_header_t sctp_hdr;
761   sctp_chunks_common_hdr_t chunk_hdr;
762
763 } sctp_shutdown_complete_chunk_t;
764
765 /* OPTIONAL or VARIABLE-LENGTH parameters for INIT */
766 #define SCTP_IPV4_ADDRESS_TYPE  5
767 #define SCTP_IPV4_ADDRESS_TYPE_LENGTH 8
768 #define SCTP_IPV6_ADDRESS_TYPE  6
769 #define SCTP_IPV6_ADDRESS_TYPE_LENGTH 20
770 #define SCTP_STATE_COOKIE_TYPE          7
771 #define SCTP_UNRECOGNIZED_TYPE  8
772 #define SCTP_COOKIE_PRESERVATIVE_TYPE   9
773 #define SCTP_COOKIE_PRESERVATIVE_TYPE_LENGTH    8
774 #define SCTP_HOSTNAME_ADDRESS_TYPE      11
775 #define SCTP_SUPPORTED_ADDRESS_TYPES    12
776
777 /*
778  * 0                   1                   2                   3
779  * 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
780  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
781  * |        Type = 5               |      Length = 8               |
782  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
783  * |                        IPv4 Address                           |
784  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
785  */
786 typedef struct
787 {
788   sctp_opt_params_hdr_t param_hdr;
789
790   /*
791    * Contains an IPv4 address of the sending endpoint.
792    * It is binary encoded.
793    */
794   ip4_address_t address;
795
796 } sctp_ipv4_addr_param_t;
797
798 always_inline void
799 vnet_sctp_set_ipv4_address (sctp_ipv4_addr_param_t * a, ip4_address_t address)
800 {
801   a->param_hdr.type = clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
802   a->param_hdr.length = clib_host_to_net_u16 (8);
803   a->address.as_u32 = clib_host_to_net_u32 (address.as_u32);
804 }
805
806 always_inline u32
807 vnet_sctp_get_ipv4_address (sctp_ipv4_addr_param_t * a)
808 {
809   return (clib_net_to_host_u32 (a->address.as_u32));
810 }
811
812 /*
813  * 0                   1                   2                   3
814  * 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
815  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
816  * |            Type = 6           |          Length = 20          |
817  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
818  * |                                                               |
819  * |                         IPv6 Address                          |
820  * |                                                               |
821  * |                                                               |
822  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
823  */
824 typedef struct
825 {
826   sctp_opt_params_hdr_t param_hdr;
827
828   /*
829    * Contains an IPv6 address of the sending endpoint.
830    * It is binary encoded.
831    */
832   ip6_address_t address;
833
834 } sctp_ipv6_addr_param_t;
835
836 always_inline void
837 vnet_sctp_set_ipv6_address (sctp_ipv6_addr_param_t * a, ip6_address_t address)
838 {
839   a->param_hdr.type = clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
840   a->param_hdr.length = clib_host_to_net_u16 (20);
841   a->address.as_u64[0] = clib_host_to_net_u64 (address.as_u64[0]);
842   a->address.as_u64[1] = clib_host_to_net_u64 (address.as_u64[1]);
843 }
844
845 always_inline ip6_address_t
846 vnet_sctp_get_ipv6_address (sctp_ipv6_addr_param_t * a)
847 {
848   ip6_address_t ip6_address;
849
850   ip6_address.as_u64[0] = clib_net_to_host_u64 (a->address.as_u64[0]);
851   ip6_address.as_u64[1] = clib_net_to_host_u64 (a->address.as_u64[1]);
852
853   return ip6_address;
854 }
855
856 /*
857  * The sender of the INIT shall use this parameter to suggest to the
858  * receiver of the INIT for a longer life-span of the State Cookie.
859  */
860 /*
861  * 0                   1                   2                   3
862  * 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
863  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
864  * |          Type = 9             |          Length = 8           |
865  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
866  * |         Suggested Cookie Life-Span Increment (msec.)          |
867  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
868  */
869 typedef struct
870 {
871   sctp_opt_params_hdr_t param_hdr;
872
873   /*
874    * This parameter indicates to the receiver how much increment in
875    * milliseconds the sender wishes the receiver to add to its default
876    * cookie life-span.
877    *
878    * This optional parameter should be added to the INIT chunk by the
879    * sender when it reattempts establishing an association with a peer
880    * to which its previous attempt of establishing the association
881    * failed due to a stale cookie operation error. The receiver MAY
882    * choose to ignore the suggested cookie life-span increase for its
883    * own security reasons.
884    */
885   u32 life_span_inc;
886
887 } sctp_cookie_preservative_param_t;
888
889 always_inline void
890 vnet_sctp_set_cookie_preservative (sctp_cookie_preservative_param_t * c,
891                                    u32 life_span_inc)
892 {
893   c->param_hdr.type = clib_host_to_net_u16 (SCTP_COOKIE_PRESERVATIVE_TYPE);
894   c->param_hdr.length = clib_host_to_net_u16 (8);
895   c->life_span_inc = clib_host_to_net_u32 (life_span_inc);
896 }
897
898 always_inline u32
899 vnet_sctp_get_cookie_preservative (sctp_cookie_preservative_param_t * c)
900 {
901   return (clib_net_to_host_u32 (c->life_span_inc));
902 }
903
904 #define FQDN_MAX_LENGTH 256
905
906 /*
907  * The sender of INIT uses this parameter to pass its Host Name (in
908  * place of its IP addresses) to its peer.
909  * The peer is responsible for resolving the name.
910  * Using this parameter might make it more likely for the association to work
911  * across a NAT box.
912  */
913 /*
914  * 0                   1                   2                   3
915  * 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
916  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
917  * |          Type = 11            |          Length               |
918  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
919  * /                          Host Name                            /
920  * \                                                               \
921  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
922  */
923 typedef struct
924 {
925   sctp_opt_params_hdr_t param_hdr;
926
927
928   /*
929    * This field contains a host name in "host name syntax" per RFC 1123
930    * Section 2.1
931    *
932    * Note: At least one null terminator is included in the Host Name
933    * string and must be included in the length.
934    */
935   char hostname[FQDN_MAX_LENGTH];
936
937 } sctp_hostname_param_t;
938
939 always_inline void
940 vnet_sctp_set_hostname_address (sctp_hostname_param_t * h, char *hostname)
941 {
942   h->param_hdr.length = FQDN_MAX_LENGTH;
943   h->param_hdr.type = clib_host_to_net_u16 (SCTP_HOSTNAME_ADDRESS_TYPE);
944   memset (h->hostname, '0', FQDN_MAX_LENGTH);
945   memcpy (h->hostname, hostname, FQDN_MAX_LENGTH);
946 }
947
948 #define MAX_SUPPORTED_ADDRESS_TYPES     3
949
950 /*
951  * The sender of INIT uses this parameter to list all the address types
952  * it can support.
953  */
954 /*
955  * 0                   1                   2                   3
956  * 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
957  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
958  * |          Type = 12            |          Length               |
959  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
960  * |        Address Type #1        |        Address Type #2        |
961  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
962  * |                            ......                             |
963  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+-+-+-+-++-+-+-+
964  */
965 typedef struct
966 {
967   sctp_opt_params_hdr_t param_hdr;
968
969   u16 address_type[MAX_SUPPORTED_ADDRESS_TYPES];
970
971 } sctp_supported_addr_types_param_t;
972
973 always_inline void
974 vnet_sctp_set_supported_address_types (sctp_supported_addr_types_param_t * s)
975 {
976   s->param_hdr.type = clib_host_to_net_u16 (SCTP_SUPPORTED_ADDRESS_TYPES);
977   s->param_hdr.length = 4 /* base = type + length */  +
978     MAX_SUPPORTED_ADDRESS_TYPES * 4;    /* each address type is 4 bytes */
979
980   s->address_type[0] = clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
981   s->address_type[1] = clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
982   s->address_type[2] = clib_host_to_net_u16 (SCTP_HOSTNAME_ADDRESS_TYPE);
983 }
984
985 /*
986  * Error cause codes to be used for the sctp_error_cause.cause_code field
987  */
988 #define INVALID_STREAM_IDENTIFIER       1
989 #define MISSING_MANDATORY_PARAMETER     2
990 #define STALE_COOKIE_ERROR              3
991 #define OUT_OF_RESOURCE                 4
992 #define UNRESOLVABLE_ADDRESS            5
993 #define UNRECOGNIZED_CHUNK_TYPE         6
994 #define INVALID_MANDATORY_PARAMETER     7
995 #define UNRECOGNIZED_PARAMETER          8
996 #define NO_USER_DATA                    9
997 #define COOKIE_RECEIVED_WHILE_SHUTTING_DOWN     10
998 #define RESTART_OF_ASSOCIATION_WITH_NEW_ADDR    11
999 #define USER_INITIATED_ABORT            12
1000 #define PROTOCOL_VIOLATION              13
1001
1002 always_inline void
1003 vnet_sctp_set_state_cookie (sctp_state_cookie_param_t * s)
1004 {
1005   s->param_hdr.type = clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
1006
1007   /* TODO: length & value to be populated */
1008 }
1009
1010 typedef struct
1011 {
1012   sctp_opt_params_hdr_t param_hdr;
1013
1014   u32 value[];
1015
1016 } sctp_unrecognized_param_t;
1017
1018 always_inline void
1019 vnet_sctp_set_unrecognized_param (sctp_unrecognized_param_t * u)
1020 {
1021   u->param_hdr.type = clib_host_to_net_u16 (UNRECOGNIZED_PARAMETER);
1022
1023   /* TODO: length & value to be populated */
1024 }
1025
1026 /*
1027  * Selective ACK (SACK) chunk
1028  *
1029  * This chunk is sent to the peer endpoint to acknowledge received DATA
1030  * chunks and to inform the peer endpoint of gaps in the received
1031  * subsequences of DATA chunks as represented by their TSNs.
1032  */
1033 /*
1034  * 0                   1                   2                   3
1035  * 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
1036  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1037  * |   Type = 3    |Chunk  Flags   |      Chunk Length             |
1038  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1039  * |                      Cumulative TSN Ack                       |
1040  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1041  * |          Advertised Receiver Window Credit (a_rwnd)           |
1042  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1043  * | Number of Gap Ack Blocks = N  |  Number of Duplicate TSNs = X |
1044  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1045  * |  Gap Ack Block #1 Start       |   Gap Ack Block #1 End        |
1046  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1047  * /                                                               /
1048  * \                              ...                              \
1049  * /                                                               /
1050  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1051  * |   Gap Ack Block #N Start      |  Gap Ack Block #N End         |
1052  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1053  * |                       Duplicate TSN 1                         |
1054  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1055  * /                                                               /
1056  * \                              ...                              \
1057  * /                                                               /
1058  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1059  * |                       Duplicate TSN X                         |
1060  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1061  */
1062 typedef struct
1063 {
1064   sctp_header_t sctp_hdr;
1065   sctp_chunks_common_hdr_t chunk_hdr;
1066   /*
1067    * This parameter contains the TSN of the last DATA chunk received in
1068    * sequence before a gap.  In the case where no DATA chunk has been
1069    * received, this value is set to the peer's Initial TSN minus one.
1070    */
1071   u32 cumulative_tsn_ack;
1072
1073   /*
1074    * This field indicates the updated receive buffer space in bytes of
1075    * the sender of this SACK.
1076    */
1077   u32 a_rwnd;
1078
1079   /*
1080    * Indicates the number of Gap Ack Blocks included in this SACK.
1081    */
1082   u16 gap_ack_blocks_count;
1083
1084   /*
1085    * This field contains the number of duplicate TSNs the endpoint has
1086    * received.  Each duplicate TSN is listed following the Gap Ack Block
1087    * list.
1088    */
1089   u16 duplicate_tsn_count;
1090
1091   /*
1092    * Indicates the Start offset TSN for this Gap Ack Block. To calculate
1093    * the actual TSN number the Cumulative TSN Ack is added to this offset
1094    * number. This calculated TSN identifies the first TSN in this Gap Ack
1095    * Block that has been received.
1096    */
1097   u16 *gap_ack_block_start;
1098
1099   /*
1100    * Indicates the End offset TSN for this Gap Ack Block. To calculate
1101    * the actual TSN number, the Cumulative TSN Ack is added to this offset
1102    * number. This calculated TSN identifies the TSN of the last DATA chunk
1103    * received in this Gap Ack Block.
1104    */
1105   u16 *gap_ack_block_end;
1106
1107   /*
1108    * Indicates the number of times a TSN was received in duplicate since
1109    * the last SACK was sent. Every time a receiver gets a duplicate TSN
1110    * (before sending the SACK), it adds it to the list of duplicates.
1111    * The duplicate count is reinitialized to zero after sending each SACK.
1112    */
1113   u32 duplicate_tsn;
1114
1115 } sctp_selective_ack_chunk_t;
1116
1117 always_inline void
1118 vnet_sctp_set_cumulative_tsn_ack (sctp_selective_ack_chunk_t * s,
1119                                   u32 cumulative_tsn_ack)
1120 {
1121   vnet_sctp_set_chunk_type (&s->chunk_hdr, SACK);
1122   s->cumulative_tsn_ack = clib_host_to_net_u32 (cumulative_tsn_ack);
1123 }
1124
1125 always_inline u32
1126 vnet_sctp_get_cumulative_tsn_ack (sctp_selective_ack_chunk_t * s)
1127 {
1128   return clib_net_to_host_u32 (s->cumulative_tsn_ack);
1129 }
1130
1131 always_inline void
1132 vnet_sctp_set_arwnd (sctp_selective_ack_chunk_t * s, u32 a_rwnd)
1133 {
1134   vnet_sctp_set_chunk_type (&s->chunk_hdr, SACK);
1135   s->a_rwnd = clib_host_to_net_u32 (a_rwnd);
1136 }
1137
1138 always_inline u32
1139 vnet_sctp_get_arwnd (sctp_selective_ack_chunk_t * s)
1140 {
1141   return clib_net_to_host_u32 (s->a_rwnd);
1142 }
1143
1144 always_inline void
1145 vnet_sctp_set_gap_ack_blocks_count (sctp_selective_ack_chunk_t * s,
1146                                     u16 gap_ack_blocks_count)
1147 {
1148   vnet_sctp_set_chunk_type (&s->chunk_hdr, SACK);
1149   s->gap_ack_blocks_count = clib_host_to_net_u16 (gap_ack_blocks_count);
1150
1151   if (s->gap_ack_block_start == NULL)
1152     s->gap_ack_block_start =
1153       clib_mem_alloc (sizeof (u16) * gap_ack_blocks_count);
1154   if (s->gap_ack_block_end == NULL)
1155     s->gap_ack_block_end =
1156       clib_mem_alloc (sizeof (u16) * gap_ack_blocks_count);
1157 }
1158
1159 always_inline u16
1160 vnet_sctp_get_gap_ack_blocks_count (sctp_selective_ack_chunk_t * s)
1161 {
1162   return clib_net_to_host_u32 (s->gap_ack_blocks_count);
1163 }
1164
1165 always_inline void
1166 vnet_sctp_set_duplicate_tsn_count (sctp_selective_ack_chunk_t * s,
1167                                    u16 duplicate_tsn_count)
1168 {
1169   vnet_sctp_set_chunk_type (&s->chunk_hdr, SACK);
1170   s->duplicate_tsn_count = clib_host_to_net_u16 (duplicate_tsn_count);
1171 }
1172
1173 always_inline u16
1174 vnet_sctp_get_duplicate_tsn_count (sctp_selective_ack_chunk_t * s)
1175 {
1176   return clib_net_to_host_u16 (s->duplicate_tsn_count);
1177 }
1178
1179 /*
1180  * Heartbeat Info
1181  *
1182  * 0                   1                   2                   3
1183  * 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
1184  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1185  * |    Heartbeat Info Type=1      |         HB Info Length        |
1186  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1187  * /                  Sender-Specific Heartbeat Info               /
1188  * \                                                               \
1189  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1190  */
1191 typedef struct
1192 {
1193   sctp_opt_params_hdr_t param_hdr;
1194
1195   /*
1196    * The Sender-Specific Heartbeat Info field should normally include
1197    * information about the sender's current time when this HEARTBEAT
1198    * chunk is sent and the destination transport address to which this
1199    * HEARTBEAT is sent.
1200    * This information is simply reflected back by the receiver in the
1201    * HEARTBEAT ACK message.
1202    *
1203    * Note also that the HEARTBEAT message is both for reachability
1204    * checking and for path verification.
1205    * When a HEARTBEAT chunk is being used for path verification purposes,
1206    * it MUST hold a 64-bit random nonce.
1207    */
1208   u64 hb_info;
1209
1210 } sctp_hb_info_param_t;
1211
1212 always_inline void
1213 vnet_sctp_set_heartbeat_info (sctp_hb_info_param_t * h, u64 hb_info,
1214                               u16 hb_info_length)
1215 {
1216   h->hb_info = clib_host_to_net_u16 (1);
1217   h->param_hdr.length = clib_host_to_net_u16 (hb_info_length);
1218   h->hb_info = clib_host_to_net_u64 (hb_info);
1219 }
1220
1221 /*
1222  * Heartbeat Request
1223  *
1224  * An endpoint should send this chunk to its peer endpoint to probe the
1225  * reachability of a particular destination transport address defined in
1226  * the present association.
1227  * The parameter field contains the Heartbeat Information, which is a
1228  * variable-length opaque data structure understood only by the sender.
1229  */
1230 /*
1231  * 0                   1                   2                   3
1232  * 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
1233  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1234  * |   Type = 4    | Chunk  Flags  |      Heartbeat Length         |
1235  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1236  * \                                                               \
1237  * /            Heartbeat Information TLV (Variable-Length)        /
1238  * \                                                               \
1239  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1240  */
1241 typedef struct
1242 {
1243   sctp_header_t sctp_hdr;
1244   sctp_chunks_common_hdr_t chunk_hdr;
1245   sctp_hb_info_param_t hb_info;
1246
1247 } sctp_hb_req_chunk_t;
1248
1249 always_inline void
1250 vnet_sctp_set_hb_request_info (sctp_hb_req_chunk_t * h,
1251                                sctp_hb_info_param_t * hb_info)
1252 {
1253   vnet_sctp_set_chunk_type (&h->chunk_hdr, HEARTBEAT);
1254   memcpy (&h->hb_info, hb_info, sizeof (h->hb_info));
1255 }
1256
1257 /*
1258  * Heartbeat Acknowledgement
1259  *
1260  * An endpoint should send this chunk to its peer endpoint as a response
1261  * to a HEARTBEAT chunk.
1262  * A HEARTBEAT ACK is always sent to the source IP address of the IP datagram
1263  * containing the HEARTBEAT chunk to which this ack is responding.
1264  */
1265 /*
1266  *
1267  * 0                   1                   2                   3
1268  * 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
1269  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1270  * |   Type = 5    | Chunk  Flags  |    Heartbeat Ack Length       |
1271  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1272  * \                                                               \
1273  * /            Heartbeat Information TLV (Variable-Length)        /
1274  * \                                                               \
1275  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1276  */
1277 typedef sctp_hb_req_chunk_t sctp_hb_ack_chunk_t;
1278
1279 always_inline void
1280 vnet_sctp_set_hb_ack_info (sctp_hb_ack_chunk_t * h,
1281                            sctp_hb_info_param_t * hb_info)
1282 {
1283   vnet_sctp_set_chunk_type (&h->chunk_hdr, HEARTBEAT_ACK);
1284   memcpy (&h->hb_info, hb_info, sizeof (h->hb_info));
1285 }
1286
1287 /*
1288  * Error cause
1289  */
1290 /*
1291  * 0                   1                   2                   3
1292  * 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
1293  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1294  * |           Cause Code          |       Cause Length            |
1295  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1296  * /                    Cause-Specific Information                 /
1297  * \                                                               \
1298  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1299 */
1300 typedef struct
1301 {
1302
1303   sctp_opt_params_hdr_t param_hdr;
1304   u64 cause_info;
1305
1306 } sctp_err_cause_param_t;
1307
1308 /*
1309  * Abort Association (ABORT)
1310  *
1311  * The ABORT chunk is sent to the peer of an association to close the
1312  * association.  The ABORT chunk may contain Cause Parameters to inform
1313  * the receiver about the reason of the abort.  DATA chunks MUST NOT be
1314  * bundled with ABORT.  Control chunks (except for INIT, INIT ACK, and
1315  * SHUTDOWN COMPLETE) MAY be bundled with an ABORT, but they MUST be
1316  * placed before the ABORT in the SCTP packet or they will be ignored by
1317  * the receiver.
1318  *
1319  * If an endpoint receives an ABORT with a format error or no TCB is
1320  * found, it MUST silently discard it.  Moreover, under any
1321  * circumstances, an endpoint that receives an ABORT MUST NOT respond to
1322  * that ABORT by sending an ABORT of its own.
1323  */
1324 /*
1325  * 0                   1                   2                   3
1326  * 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
1327  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1328  * |   Type = 6    |Reserved     |T|           Length              |
1329  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1330  * \                                                               \
1331  * /                   zero or more Error Causes                   /
1332  * \                                                               \
1333  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1334  */
1335 typedef struct
1336 {
1337   sctp_header_t sctp_hdr;
1338   sctp_chunks_common_hdr_t chunk_hdr;
1339   sctp_err_cause_param_t err_causes[];
1340
1341 } sctp_abort_chunk_t;
1342
1343 always_inline void
1344 vnet_sctp_set_tbit (sctp_abort_chunk_t * a)
1345 {
1346   vnet_sctp_set_chunk_type (&a->chunk_hdr, ABORT);
1347   // a->chunk_hdr.flags = clib_host_to_net_u16 (1);
1348 }
1349
1350 always_inline void
1351 vnet_sctp_unset_tbit (sctp_abort_chunk_t * a)
1352 {
1353   vnet_sctp_set_chunk_type (&a->chunk_hdr, ABORT);
1354   // a->chunk_hdr.flags = clib_host_to_net_u16 (0);
1355 }
1356
1357 /*
1358  * Shutdown Association (SHUTDOWN)
1359  *
1360  * An endpoint in an association MUST use this chunk to initiate a
1361  * graceful close of the association with its peer.
1362  */
1363 /*
1364  * 0                   1                   2                   3
1365  * 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
1366  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1367  * |   Type = 7    | Chunk  Flags  |      Length = 8               |
1368  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1369  * |                      Cumulative TSN Ack                       |
1370  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1371  */
1372 typedef struct
1373 {
1374   sctp_header_t sctp_hdr;
1375   sctp_chunks_common_hdr_t chunk_hdr;
1376   /*
1377    * This parameter contains the TSN of the last chunk received in
1378    * sequence before any gaps.
1379    *
1380    * Note: Since the SHUTDOWN message does not contain Gap Ack Blocks,
1381    * it cannot be used to acknowledge TSNs received out of order. In a
1382    * SACK, lack of Gap Ack Blocks that were previously included
1383    * indicates that the data receiver reneged on the associated DATA
1384    * chunks. Since SHUTDOWN does not contain Gap Ack Blocks, the
1385    * receiver of the SHUTDOWN shouldn't interpret the lack of a Gap Ack
1386    * Block as a renege.
1387    */
1388   u32 cumulative_tsn_ack;
1389
1390 } sctp_shutdown_association_chunk_t;
1391
1392 always_inline void
1393 vnet_sctp_set_tsn_last_received_chunk (sctp_shutdown_association_chunk_t * s,
1394                                        u32 tsn_last_chunk)
1395 {
1396   vnet_sctp_set_chunk_type (&s->chunk_hdr, SHUTDOWN);
1397   s->cumulative_tsn_ack = clib_host_to_net_u32 (tsn_last_chunk);
1398 }
1399
1400 /*
1401  * Shutdown Acknowledgement (SHUTDOWN ACK)
1402  *
1403  * This chunk MUST be used to acknowledge the receipt of the SHUTDOWN
1404  * chunk at the completion of the shutdown process.
1405  */
1406 /*
1407  * 0                   1                   2                   3
1408  * 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
1409  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1410  * |   Type = 8    |Chunk  Flags   |      Length = 4               |
1411  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1412  */
1413 typedef struct
1414 {
1415   sctp_header_t sctp_hdr;
1416   sctp_chunks_common_hdr_t chunk_hdr;
1417 } sctp_shutdown_ack_chunk_t;
1418
1419 always_inline void
1420 vnet_sctp_fill_shutdown_ack (sctp_shutdown_ack_chunk_t * s)
1421 {
1422   vnet_sctp_set_chunk_type (&s->chunk_hdr, SHUTDOWN_ACK);
1423   vnet_sctp_set_chunk_length (&s->chunk_hdr, 4);
1424 }
1425
1426 #endif /* included_vnet_sctp_packet_h */
1427
1428 /*
1429  * fd.io coding-style-patch-verification: ON
1430  *
1431  * Local Variables:
1432  * eval: (c-set-style "gnu")
1433  * End:
1434  */