1 /* SCTP kernel 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 Intel Corp.
6  *
7  * This file is part of the SCTP kernel implementation
8  *
9  * This SCTP implementation is free software;
10  * you can redistribute it and/or modify it under the terms of
11  * the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This SCTP implementation is distributed in the hope that it
16  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
17  *                 ************************
18  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with GNU CC; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * Please send any bug reports or fixes you make to the
27  * email address(es):
28  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
29  *
30  * Or submit a bug report through the following website:
31  *    http://www.sf.net/projects/lksctp
32  *
33  * Written or modified by:
34  *   La Monte H.P. Yarroll <piggy@acm.org>
35  *   Karl Knutson          <karl@athena.chicago.il.us>
36  *   Randall Stewart       <randall@stewart.chicago.il.us>
37  *   Ken Morneau           <kmorneau@cisco.com>
38  *   Qiaobing Xie          <qxie1@motorola.com>
39  *   Xingang Guo           <xingang.guo@intel.com>
40  *   Sridhar Samudrala     <samudrala@us.ibm.com>
41  *   Daisy Chang           <daisyc@us.ibm.com>
42  *
43  * Any bugs reported given to us we will try to fix... any fixes shared will
44  * be incorporated into the next SCTP release.
45  */
46 
47 #ifndef __sctp_constants_h__
48 #define __sctp_constants_h__
49 
50 #include <linux/sctp.h>
51 #include <linux/ipv6.h> /* For ipv6hdr. */
52 #include <net/sctp/user.h>
53 #include <net/tcp_states.h>  /* For TCP states used in sctp_sock_state_t */
54 
55 /* Value used for stream negotiation. */
56 enum { SCTP_MAX_STREAM = 0xffff };
57 enum { SCTP_DEFAULT_OUTSTREAMS = 10 };
58 enum { SCTP_DEFAULT_INSTREAMS = SCTP_MAX_STREAM };
59 
60 /* Since CIDs are sparse, we need all four of the following
61  * symbols.  CIDs are dense through SCTP_CID_BASE_MAX.
62  */
63 #define SCTP_CID_BASE_MAX		SCTP_CID_SHUTDOWN_COMPLETE
64 
65 #define SCTP_NUM_BASE_CHUNK_TYPES	(SCTP_CID_BASE_MAX + 1)
66 
67 #define SCTP_NUM_ADDIP_CHUNK_TYPES	2
68 
69 #define SCTP_NUM_PRSCTP_CHUNK_TYPES	1
70 
71 #define SCTP_NUM_AUTH_CHUNK_TYPES	1
72 
73 #define SCTP_NUM_CHUNK_TYPES		(SCTP_NUM_BASE_CHUNK_TYPES + \
74 					 SCTP_NUM_ADDIP_CHUNK_TYPES +\
75 					 SCTP_NUM_PRSCTP_CHUNK_TYPES +\
76 					 SCTP_NUM_AUTH_CHUNK_TYPES)
77 
78 /* These are the different flavours of event.  */
79 typedef enum {
80 
81 	SCTP_EVENT_T_CHUNK = 1,
82 	SCTP_EVENT_T_TIMEOUT,
83 	SCTP_EVENT_T_OTHER,
84 	SCTP_EVENT_T_PRIMITIVE
85 
86 } sctp_event_t;
87 
88 /* As a convenience for the state machine, we append SCTP_EVENT_* and
89  * SCTP_ULP_* to the list of possible chunks.
90  */
91 
92 typedef enum {
93 	SCTP_EVENT_TIMEOUT_NONE = 0,
94 	SCTP_EVENT_TIMEOUT_T1_COOKIE,
95 	SCTP_EVENT_TIMEOUT_T1_INIT,
96 	SCTP_EVENT_TIMEOUT_T2_SHUTDOWN,
97 	SCTP_EVENT_TIMEOUT_T3_RTX,
98 	SCTP_EVENT_TIMEOUT_T4_RTO,
99 	SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD,
100 	SCTP_EVENT_TIMEOUT_HEARTBEAT,
101 	SCTP_EVENT_TIMEOUT_SACK,
102 	SCTP_EVENT_TIMEOUT_AUTOCLOSE,
103 } sctp_event_timeout_t;
104 
105 #define SCTP_EVENT_TIMEOUT_MAX		SCTP_EVENT_TIMEOUT_AUTOCLOSE
106 #define SCTP_NUM_TIMEOUT_TYPES		(SCTP_EVENT_TIMEOUT_MAX + 1)
107 
108 typedef enum {
109 	SCTP_EVENT_NO_PENDING_TSN = 0,
110 	SCTP_EVENT_ICMP_PROTO_UNREACH,
111 } sctp_event_other_t;
112 
113 #define SCTP_EVENT_OTHER_MAX		SCTP_EVENT_ICMP_PROTO_UNREACH
114 #define SCTP_NUM_OTHER_TYPES		(SCTP_EVENT_OTHER_MAX + 1)
115 
116 /* These are primitive requests from the ULP.  */
117 typedef enum {
118 	SCTP_PRIMITIVE_ASSOCIATE = 0,
119 	SCTP_PRIMITIVE_SHUTDOWN,
120 	SCTP_PRIMITIVE_ABORT,
121 	SCTP_PRIMITIVE_SEND,
122 	SCTP_PRIMITIVE_REQUESTHEARTBEAT,
123 	SCTP_PRIMITIVE_ASCONF,
124 } sctp_event_primitive_t;
125 
126 #define SCTP_EVENT_PRIMITIVE_MAX	SCTP_PRIMITIVE_ASCONF
127 #define SCTP_NUM_PRIMITIVE_TYPES	(SCTP_EVENT_PRIMITIVE_MAX + 1)
128 
129 /* We define here a utility type for manipulating subtypes.
130  * The subtype constructors all work like this:
131  *
132  * 	sctp_subtype_t foo = SCTP_ST_CHUNK(SCTP_CID_INIT);
133  */
134 
135 typedef union {
136 	sctp_cid_t chunk;
137 	sctp_event_timeout_t timeout;
138 	sctp_event_other_t other;
139 	sctp_event_primitive_t primitive;
140 } sctp_subtype_t;
141 
142 #define SCTP_SUBTYPE_CONSTRUCTOR(_name, _type, _elt) \
143 static inline sctp_subtype_t	\
144 SCTP_ST_## _name (_type _arg)		\
145 { sctp_subtype_t _retval; _retval._elt = _arg; return _retval; }
146 
147 SCTP_SUBTYPE_CONSTRUCTOR(CHUNK,		sctp_cid_t,		chunk)
148 SCTP_SUBTYPE_CONSTRUCTOR(TIMEOUT,	sctp_event_timeout_t,	timeout)
149 SCTP_SUBTYPE_CONSTRUCTOR(OTHER,		sctp_event_other_t,	other)
150 SCTP_SUBTYPE_CONSTRUCTOR(PRIMITIVE,	sctp_event_primitive_t,	primitive)
151 
152 
153 #define sctp_chunk_is_control(a) (a->chunk_hdr->type != SCTP_CID_DATA)
154 #define sctp_chunk_is_data(a) (a->chunk_hdr->type == SCTP_CID_DATA)
155 
156 /* Calculate the actual data size in a data chunk */
157 #define SCTP_DATA_SNDSIZE(c) ((int)((unsigned long)(c->chunk_end)\
158 		       		- (unsigned long)(c->chunk_hdr)\
159 				- sizeof(sctp_data_chunk_t)))
160 
161 /* Internal error codes */
162 typedef enum {
163 
164 	SCTP_IERROR_NO_ERROR	        = 0,
165 	SCTP_IERROR_BASE		= 1000,
166 	SCTP_IERROR_NO_COOKIE,
167 	SCTP_IERROR_BAD_SIG,
168 	SCTP_IERROR_STALE_COOKIE,
169 	SCTP_IERROR_NOMEM,
170 	SCTP_IERROR_MALFORMED,
171 	SCTP_IERROR_BAD_TAG,
172 	SCTP_IERROR_BIG_GAP,
173 	SCTP_IERROR_DUP_TSN,
174 	SCTP_IERROR_HIGH_TSN,
175 	SCTP_IERROR_IGNORE_TSN,
176 	SCTP_IERROR_NO_DATA,
177 	SCTP_IERROR_BAD_STREAM,
178 	SCTP_IERROR_BAD_PORTS,
179 	SCTP_IERROR_AUTH_BAD_HMAC,
180 	SCTP_IERROR_AUTH_BAD_KEYID,
181 	SCTP_IERROR_PROTO_VIOLATION,
182 	SCTP_IERROR_ERROR,
183 	SCTP_IERROR_ABORT,
184 } sctp_ierror_t;
185 
186 
187 
188 /* SCTP state defines for internal state machine */
189 typedef enum {
190 
191 	SCTP_STATE_EMPTY		= 0,
192 	SCTP_STATE_CLOSED		= 1,
193 	SCTP_STATE_COOKIE_WAIT		= 2,
194 	SCTP_STATE_COOKIE_ECHOED	= 3,
195 	SCTP_STATE_ESTABLISHED		= 4,
196 	SCTP_STATE_SHUTDOWN_PENDING	= 5,
197 	SCTP_STATE_SHUTDOWN_SENT	= 6,
198 	SCTP_STATE_SHUTDOWN_RECEIVED	= 7,
199 	SCTP_STATE_SHUTDOWN_ACK_SENT	= 8,
200 
201 } sctp_state_t;
202 
203 #define SCTP_STATE_MAX			SCTP_STATE_SHUTDOWN_ACK_SENT
204 #define SCTP_STATE_NUM_STATES		(SCTP_STATE_MAX + 1)
205 
206 /* These are values for sk->state.
207  * For a UDP-style SCTP socket, the states are defined as follows
208  * - A socket in SCTP_SS_CLOSED state indicates that it is not willing to
209  *   accept new associations, but it can initiate the creation of new ones.
210  * - A socket in SCTP_SS_LISTENING state indicates that it is willing to
211  *   accept new  associations and can initiate the creation of new ones.
212  * - A socket in SCTP_SS_ESTABLISHED state indicates that it is a peeled off
213  *   socket with one association.
214  * For a TCP-style SCTP socket, the states are defined as follows
215  * - A socket in SCTP_SS_CLOSED state indicates that it is not willing to
216  *   accept new associations, but it can initiate the creation of new ones.
217  * - A socket in SCTP_SS_LISTENING state indicates that it is willing to
218  *   accept new associations, but cannot initiate the creation of new ones.
219  * - A socket in SCTP_SS_ESTABLISHED state indicates that it has a single
220  *   association.
221  */
222 typedef enum {
223 	SCTP_SS_CLOSED         = TCP_CLOSE,
224 	SCTP_SS_LISTENING      = TCP_LISTEN,
225 	SCTP_SS_ESTABLISHING   = TCP_SYN_SENT,
226 	SCTP_SS_ESTABLISHED    = TCP_ESTABLISHED,
227 	SCTP_SS_CLOSING        = TCP_CLOSING,
228 } sctp_sock_state_t;
229 
230 /* These functions map various type to printable names.  */
231 const char *sctp_cname(const sctp_subtype_t);	/* chunk types */
232 const char *sctp_oname(const sctp_subtype_t);	/* other events */
233 const char *sctp_tname(const sctp_subtype_t);	/* timeouts */
234 const char *sctp_pname(const sctp_subtype_t);	/* primitives */
235 
236 /* This is a table of printable names of sctp_state_t's.  */
237 extern const char *const sctp_state_tbl[];
238 extern const char *const sctp_evttype_tbl[];
239 extern const char *const sctp_status_tbl[];
240 
241 /* Maximum chunk length considering padding requirements. */
242 enum { SCTP_MAX_CHUNK_LEN = ((1<<16) - sizeof(__u32)) };
243 
244 /* Encourage Cookie-Echo bundling by pre-fragmenting chunks a little
245  * harder (until reaching ESTABLISHED state).
246  */
247 enum { SCTP_ARBITRARY_COOKIE_ECHO_LEN = 200 };
248 
249 /* Guess at how big to make the TSN mapping array.
250  * We guarantee that we can handle at least this big a gap between the
251  * cumulative ACK and the highest TSN.  In practice, we can often
252  * handle up to twice this value.
253  *
254  * NEVER make this more than 32767 (2^15-1).  The Gap Ack Blocks in a
255  * SACK (see  section 3.3.4) are only 16 bits, so 2*SCTP_TSN_MAP_SIZE
256  * must be less than 65535 (2^16 - 1), or we will have overflow
257  * problems creating SACK's.
258  */
259 #define SCTP_TSN_MAP_INITIAL BITS_PER_LONG
260 #define SCTP_TSN_MAP_INCREMENT SCTP_TSN_MAP_INITIAL
261 #define SCTP_TSN_MAP_SIZE 4096
262 
263 /* We will not record more than this many duplicate TSNs between two
264  * SACKs.  The minimum PMTU is 576.  Remove all the headers and there
265  * is enough room for 131 duplicate reports.  Round down to the
266  * nearest power of 2.
267  */
268 enum { SCTP_MIN_PMTU = 576 };
269 enum { SCTP_MAX_DUP_TSNS = 16 };
270 enum { SCTP_MAX_GABS = 16 };
271 
272 /* Heartbeat interval - 30 secs */
273 #define SCTP_DEFAULT_TIMEOUT_HEARTBEAT	(30*1000)
274 
275 /* Delayed sack timer - 200ms */
276 #define SCTP_DEFAULT_TIMEOUT_SACK	(200)
277 
278 /* RTO.Initial              - 3  seconds
279  * RTO.Min                  - 1  second
280  * RTO.Max                  - 60 seconds
281  * RTO.Alpha                - 1/8
282  * RTO.Beta                 - 1/4
283  */
284 #define SCTP_RTO_INITIAL	(3 * 1000)
285 #define SCTP_RTO_MIN		(1 * 1000)
286 #define SCTP_RTO_MAX		(60 * 1000)
287 
288 #define SCTP_RTO_ALPHA          3   /* 1/8 when converted to right shifts. */
289 #define SCTP_RTO_BETA           2   /* 1/4 when converted to right shifts. */
290 
291 /* Maximum number of new data packets that can be sent in a burst.  */
292 #define SCTP_DEFAULT_MAX_BURST		4
293 
294 #define SCTP_CLOCK_GRANULARITY	1	/* 1 jiffy */
295 
296 #define SCTP_DEFAULT_COOKIE_LIFE	(60 * 1000) /* 60 seconds */
297 
298 #define SCTP_DEFAULT_MINWINDOW	1500	/* default minimum rwnd size */
299 #define SCTP_DEFAULT_MAXWINDOW	65535	/* default rwnd size */
300 #define SCTP_DEFAULT_RWND_SHIFT  4	/* by default, update on 1/16 of
301 					 * rcvbuf, which is 1/8 of initial
302 					 * window
303 					 */
304 #define SCTP_DEFAULT_MAXSEGMENT 1500	/* MTU size, this is the limit
305                                          * to which we will raise the P-MTU.
306 					 */
307 #define SCTP_DEFAULT_MINSEGMENT 512	/* MTU size ... if no mtu disc */
308 #define SCTP_HOW_MANY_SECRETS 2		/* How many secrets I keep */
309 #define SCTP_SECRET_SIZE 32		/* Number of octets in a 256 bits. */
310 
311 #define SCTP_SIGNATURE_SIZE 20	        /* size of a SLA-1 signature */
312 
313 #define SCTP_COOKIE_MULTIPLE 32 /* Pad out our cookie to make our hash
314 				 * functions simpler to write.
315 				 */
316 
317 #if defined (CONFIG_SCTP_HMAC_MD5)
318 #define SCTP_COOKIE_HMAC_ALG "hmac(md5)"
319 #elif defined (CONFIG_SCTP_HMAC_SHA1)
320 #define SCTP_COOKIE_HMAC_ALG "hmac(sha1)"
321 #else
322 #define SCTP_COOKIE_HMAC_ALG NULL
323 #endif
324 
325 /* These return values describe the success or failure of a number of
326  * routines which form the lower interface to SCTP_outqueue.
327  */
328 typedef enum {
329 	SCTP_XMIT_OK,
330 	SCTP_XMIT_PMTU_FULL,
331 	SCTP_XMIT_RWND_FULL,
332 	SCTP_XMIT_NAGLE_DELAY,
333 } sctp_xmit_t;
334 
335 /* These are the commands for manipulating transports.  */
336 typedef enum {
337 	SCTP_TRANSPORT_UP,
338 	SCTP_TRANSPORT_DOWN,
339 } sctp_transport_cmd_t;
340 
341 /* These are the address scopes defined mainly for IPv4 addresses
342  * based on draft of SCTP IPv4 scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
343  * These scopes are hopefully generic enough to be used on scoping both
344  * IPv4 and IPv6 addresses in SCTP.
345  * At this point, the IPv6 scopes will be mapped to these internal scopes
346  * as much as possible.
347  */
348 typedef enum {
349 	SCTP_SCOPE_GLOBAL,		/* IPv4 global addresses */
350 	SCTP_SCOPE_PRIVATE,		/* IPv4 private addresses */
351 	SCTP_SCOPE_LINK,		/* IPv4 link local address */
352 	SCTP_SCOPE_LOOPBACK,		/* IPv4 loopback address */
353 	SCTP_SCOPE_UNUSABLE,		/* IPv4 unusable addresses */
354 } sctp_scope_t;
355 
356 typedef enum {
357 	SCTP_SCOPE_POLICY_DISABLE,	/* Disable IPv4 address scoping */
358 	SCTP_SCOPE_POLICY_ENABLE,	/* Enable IPv4 address scoping */
359 	SCTP_SCOPE_POLICY_PRIVATE,	/* Follow draft but allow IPv4 private addresses */
360 	SCTP_SCOPE_POLICY_LINK,		/* Follow draft but allow IPv4 link local addresses */
361 } sctp_scope_policy_t;
362 
363 /* Based on IPv4 scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>,
364  * SCTP IPv4 unusable addresses: 0.0.0.0/8, 224.0.0.0/4, 198.18.0.0/24,
365  * 192.88.99.0/24.
366  * Also, RFC 8.4, non-unicast addresses are not considered valid SCTP
367  * addresses.
368  */
369 #define IS_IPV4_UNUSABLE_ADDRESS(a)	    \
370 	((htonl(INADDR_BROADCAST) == a) ||  \
371 	 ipv4_is_multicast(a) ||	    \
372 	 ipv4_is_zeronet(a) ||		    \
373 	 ipv4_is_test_198(a) ||		    \
374 	 ipv4_is_anycast_6to4(a))
375 
376 /* Flags used for the bind address copy functions.  */
377 #define SCTP_ADDR6_ALLOWED	0x00000001	/* IPv6 address is allowed by
378 						   local sock family */
379 #define SCTP_ADDR4_PEERSUPP	0x00000002	/* IPv4 address is supported by
380 						   peer */
381 #define SCTP_ADDR6_PEERSUPP	0x00000004	/* IPv6 address is supported by
382 						   peer */
383 
384 /* Reasons to retransmit. */
385 typedef enum {
386 	SCTP_RTXR_T3_RTX,
387 	SCTP_RTXR_FAST_RTX,
388 	SCTP_RTXR_PMTUD,
389 	SCTP_RTXR_T1_RTX,
390 } sctp_retransmit_reason_t;
391 
392 /* Reasons to lower cwnd. */
393 typedef enum {
394 	SCTP_LOWER_CWND_T3_RTX,
395 	SCTP_LOWER_CWND_FAST_RTX,
396 	SCTP_LOWER_CWND_ECNE,
397 	SCTP_LOWER_CWND_INACTIVE,
398 } sctp_lower_cwnd_t;
399 
400 
401 /* SCTP-AUTH Necessary constants */
402 
403 /* SCTP-AUTH, Section 3.3
404  *
405  *  The following Table 2 shows the currently defined values for HMAC
406  *  identifiers.
407  *
408  *  +-----------------+--------------------------+
409  *  | HMAC Identifier | Message Digest Algorithm |
410  *  +-----------------+--------------------------+
411  *  | 0               | Reserved                 |
412  *  | 1               | SHA-1 defined in [8]     |
413  *  | 2               | Reserved                 |
414  *  | 3               | SHA-256 defined in [8]   |
415  *  +-----------------+--------------------------+
416  */
417 enum {
418 	SCTP_AUTH_HMAC_ID_RESERVED_0,
419 	SCTP_AUTH_HMAC_ID_SHA1,
420 	SCTP_AUTH_HMAC_ID_RESERVED_2,
421 #if defined (CONFIG_CRYPTO_SHA256) || defined (CONFIG_CRYPTO_SHA256_MODULE)
422 	SCTP_AUTH_HMAC_ID_SHA256,
423 #endif
424 	__SCTP_AUTH_HMAC_MAX
425 };
426 
427 #define SCTP_AUTH_HMAC_ID_MAX	__SCTP_AUTH_HMAC_MAX - 1
428 #define SCTP_AUTH_NUM_HMACS 	__SCTP_AUTH_HMAC_MAX
429 #define SCTP_SHA1_SIG_SIZE 20
430 #define SCTP_SHA256_SIG_SIZE 32
431 
432 /*  SCTP-AUTH, Section 3.2
433  *     The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE and AUTH chunks
434  *     MUST NOT be listed in the CHUNKS parameter
435  */
436 #define SCTP_NUM_NOAUTH_CHUNKS	4
437 #define SCTP_AUTH_MAX_CHUNKS	(SCTP_NUM_CHUNK_TYPES - SCTP_NUM_NOAUTH_CHUNKS)
438 
439 /* SCTP-AUTH Section 6.1
440  * The RANDOM parameter MUST contain a 32 byte random number.
441  */
442 #define SCTP_AUTH_RANDOM_LENGTH 32
443 
444 #endif /* __sctp_constants_h__ */
445