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 Intel Corp.
6  *
7  * This file is part of the SCTP kernel reference Implementation
8  *
9  * These are definitions needed by the state machine.
10  *
11  * The SCTP reference implementation is free software;
12  * you can redistribute it and/or modify it under the terms of
13  * the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * The SCTP reference implementation is distributed in the hope that it
18  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19  *                 ************************
20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  * See the GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with GNU CC; see the file COPYING.  If not, write to
25  * the Free Software Foundation, 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  *
28  * Please send any bug reports or fixes you make to the
29  * email addresses:
30  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
31  *
32  * Or submit a bug report through the following website:
33  *    http://www.sf.net/projects/lksctp
34  *
35  * Written or modified by:
36  *    La Monte H.P. Yarroll <piggy@acm.org>
37  *    Karl Knutson <karl@athena.chicago.il.us>
38  *    Xingang Guo <xingang.guo@intel.com>
39  *    Jon Grimm <jgrimm@us.ibm.com>
40  *    Dajiang Zhang <dajiang.zhang@nokia.com>
41  *    Sridhar Samudrala <sri@us.ibm.com>
42  *    Daisy Chang <daisyc@us.ibm.com>
43  *    Ardelle Fan <ardelle.fan@intel.com>
44  *    Kevin Gao <kevin.gao@intel.com>
45  *
46  * Any bugs reported given to us we will try to fix... any fixes shared will
47  * be incorporated into the next SCTP release.
48  */
49 
50 #include <linux/types.h>
51 #include <linux/compiler.h>
52 #include <linux/slab.h>
53 #include <linux/in.h>
54 #include <net/sctp/command.h>
55 #include <net/sctp/sctp.h>
56 
57 #ifndef __sctp_sm_h__
58 #define __sctp_sm_h__
59 
60 /*
61  * Possible values for the disposition are:
62  */
63 typedef enum {
64 	SCTP_DISPOSITION_DISCARD,	 /* No further processing.  */
65 	SCTP_DISPOSITION_CONSUME,	 /* Process return values normally.  */
66 	SCTP_DISPOSITION_NOMEM,		 /* We ran out of memory--recover.  */
67 	SCTP_DISPOSITION_DELETE_TCB,	 /* Close the association.  */
68 	SCTP_DISPOSITION_ABORT,		 /* Close the association NOW.  */
69 	SCTP_DISPOSITION_VIOLATION,	 /* The peer is misbehaving.  */
70 	SCTP_DISPOSITION_NOT_IMPL,	 /* This entry is not implemented.  */
71 	SCTP_DISPOSITION_ERROR,		 /* This is plain old user error.  */
72 	SCTP_DISPOSITION_BUG,		 /* This is a bug.  */
73 } sctp_disposition_t;
74 
75 typedef struct {
76 	int name;
77 	int action;
78 } sctp_sm_command_t;
79 
80 typedef sctp_disposition_t (sctp_state_fn_t) (const struct sctp_endpoint *,
81 					      const struct sctp_association *,
82 					      const sctp_subtype_t type,
83 					      void *arg,
84 					      sctp_cmd_seq_t *);
85 typedef void (sctp_timer_event_t) (unsigned long);
86 typedef struct {
87 	sctp_state_fn_t *fn;
88 	const char *name;
89 } sctp_sm_table_entry_t;
90 
91 /* A naming convention of "sctp_sf_xxx" applies to all the state functions
92  * currently in use.
93  */
94 
95 /* Prototypes for generic state functions. */
96 sctp_state_fn_t sctp_sf_not_impl;
97 sctp_state_fn_t sctp_sf_bug;
98 
99 /* Prototypes for gener timer state functions. */
100 sctp_state_fn_t sctp_sf_timer_ignore;
101 
102 /* Prototypes for chunk state functions. */
103 sctp_state_fn_t sctp_sf_do_9_1_abort;
104 sctp_state_fn_t sctp_sf_cookie_wait_abort;
105 sctp_state_fn_t sctp_sf_cookie_echoed_abort;
106 sctp_state_fn_t sctp_sf_shutdown_pending_abort;
107 sctp_state_fn_t sctp_sf_shutdown_sent_abort;
108 sctp_state_fn_t sctp_sf_shutdown_ack_sent_abort;
109 sctp_state_fn_t sctp_sf_do_5_1B_init;
110 sctp_state_fn_t sctp_sf_do_5_1C_ack;
111 sctp_state_fn_t sctp_sf_do_5_1D_ce;
112 sctp_state_fn_t sctp_sf_do_5_1E_ca;
113 sctp_state_fn_t sctp_sf_do_4_C;
114 sctp_state_fn_t sctp_sf_eat_data_6_2;
115 sctp_state_fn_t sctp_sf_eat_data_fast_4_4;
116 sctp_state_fn_t sctp_sf_eat_sack_6_2;
117 sctp_state_fn_t sctp_sf_tabort_8_4_8;
118 sctp_state_fn_t sctp_sf_operr_notify;
119 sctp_state_fn_t sctp_sf_t1_timer_expire;
120 sctp_state_fn_t sctp_sf_t2_timer_expire;
121 sctp_state_fn_t sctp_sf_t4_timer_expire;
122 sctp_state_fn_t sctp_sf_t5_timer_expire;
123 sctp_state_fn_t sctp_sf_sendbeat_8_3;
124 sctp_state_fn_t sctp_sf_beat_8_3;
125 sctp_state_fn_t sctp_sf_backbeat_8_3;
126 sctp_state_fn_t sctp_sf_do_9_2_final;
127 sctp_state_fn_t sctp_sf_do_9_2_shutdown;
128 sctp_state_fn_t sctp_sf_do_ecn_cwr;
129 sctp_state_fn_t sctp_sf_do_ecne;
130 sctp_state_fn_t sctp_sf_ootb;
131 sctp_state_fn_t sctp_sf_pdiscard;
132 sctp_state_fn_t sctp_sf_violation;
133 sctp_state_fn_t sctp_sf_violation_chunklen;
134 sctp_state_fn_t sctp_sf_discard_chunk;
135 sctp_state_fn_t sctp_sf_do_5_2_1_siminit;
136 sctp_state_fn_t sctp_sf_do_5_2_2_dupinit;
137 sctp_state_fn_t sctp_sf_do_5_2_4_dupcook;
138 sctp_state_fn_t sctp_sf_unk_chunk;
139 sctp_state_fn_t sctp_sf_do_8_5_1_E_sa;
140 sctp_state_fn_t sctp_sf_cookie_echoed_err;
141 sctp_state_fn_t sctp_sf_do_asconf;
142 sctp_state_fn_t sctp_sf_do_asconf_ack;
143 sctp_state_fn_t sctp_sf_do_9_2_reshutack;
144 sctp_state_fn_t sctp_sf_eat_fwd_tsn;
145 sctp_state_fn_t sctp_sf_eat_fwd_tsn_fast;
146 
147 /* Prototypes for primitive event state functions.  */
148 sctp_state_fn_t sctp_sf_do_prm_asoc;
149 sctp_state_fn_t sctp_sf_do_prm_send;
150 sctp_state_fn_t sctp_sf_do_9_2_prm_shutdown;
151 sctp_state_fn_t sctp_sf_cookie_wait_prm_shutdown;
152 sctp_state_fn_t sctp_sf_cookie_echoed_prm_shutdown;
153 sctp_state_fn_t sctp_sf_do_9_1_prm_abort;
154 sctp_state_fn_t sctp_sf_cookie_wait_prm_abort;
155 sctp_state_fn_t sctp_sf_cookie_echoed_prm_abort;
156 sctp_state_fn_t sctp_sf_shutdown_pending_prm_abort;
157 sctp_state_fn_t sctp_sf_shutdown_sent_prm_abort;
158 sctp_state_fn_t sctp_sf_shutdown_ack_sent_prm_abort;
159 sctp_state_fn_t sctp_sf_error_closed;
160 sctp_state_fn_t sctp_sf_error_shutdown;
161 sctp_state_fn_t sctp_sf_ignore_primitive;
162 sctp_state_fn_t sctp_sf_do_prm_requestheartbeat;
163 sctp_state_fn_t sctp_sf_do_prm_asconf;
164 
165 /* Prototypes for other event state functions.  */
166 sctp_state_fn_t sctp_sf_do_9_2_start_shutdown;
167 sctp_state_fn_t sctp_sf_do_9_2_shutdown_ack;
168 sctp_state_fn_t sctp_sf_ignore_other;
169 sctp_state_fn_t sctp_sf_cookie_wait_icmp_abort;
170 
171 /* Prototypes for timeout event state functions.  */
172 sctp_state_fn_t sctp_sf_do_6_3_3_rtx;
173 sctp_state_fn_t sctp_sf_do_6_2_sack;
174 sctp_state_fn_t sctp_sf_autoclose_timer_expire;
175 
176 /* Prototypes for utility support functions.  */
177 __u8 sctp_get_chunk_type(struct sctp_chunk *chunk);
178 const sctp_sm_table_entry_t *sctp_sm_lookup_event(sctp_event_t,
179 					    sctp_state_t,
180 					    sctp_subtype_t);
181 int sctp_chunk_iif(const struct sctp_chunk *);
182 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *,
183 					     struct sctp_chunk *,
184 					     int gfp);
185 __u32 sctp_generate_verification_tag(void);
186 void sctp_populate_tie_tags(__u8 *cookie, __u32 curTag, __u32 hisTag);
187 
188 /* Prototypes for chunk-building functions.  */
189 struct sctp_chunk *sctp_make_init(const struct sctp_association *,
190 			     const struct sctp_bind_addr *,
191 			     int gfp, int vparam_len);
192 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *,
193 				 const struct sctp_chunk *,
194 				 const int gfp,
195 				 const int unkparam_len);
196 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *,
197 				    const struct sctp_chunk *);
198 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *,
199 				   const struct sctp_chunk *);
200 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *,
201 				 const __u32 lowest_tsn,
202 				 const struct sctp_chunk *);
203 struct sctp_chunk * sctp_make_datafrag_empty(struct sctp_association *,
204 					const struct sctp_sndrcvinfo *sinfo,
205 					int len, const __u8 flags,
206 					__u16 ssn);
207 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *,
208 				  const __u32);
209 struct sctp_chunk *sctp_make_sack(const struct sctp_association *);
210 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
211 				      const struct sctp_chunk *chunk);
212 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
213 					  const struct sctp_chunk *);
214 struct sctp_chunk *sctp_make_shutdown_complete(const struct sctp_association *,
215 					  const struct sctp_chunk *);
216 void sctp_init_cause(struct sctp_chunk *, __u16 cause, const void *, size_t);
217 struct sctp_chunk *sctp_make_abort(const struct sctp_association *,
218 			      const struct sctp_chunk *,
219 			      const size_t hint);
220 struct sctp_chunk *sctp_make_abort_no_data(const struct sctp_association *,
221 				      const struct sctp_chunk *,
222 				      __u32 tsn);
223 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *,
224 					const struct msghdr *, size_t msg_len);
225 struct sctp_chunk *sctp_make_abort_violation(const struct sctp_association *,
226 				   const struct sctp_chunk *,
227 				   const __u8 *,
228 				   const size_t );
229 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *,
230 				  const struct sctp_transport *,
231 				  const void *payload,
232 				  const size_t paylen);
233 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *,
234 				      const struct sctp_chunk *,
235 				      const void *payload,
236 				      const size_t paylen);
237 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *,
238 				 const struct sctp_chunk *chunk,
239 				 __u16 cause_code,
240 				 const void *payload,
241 				 size_t paylen);
242 
243 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *,
244 					      union sctp_addr *,
245 					      struct sockaddr *,
246 					      int, __u16);
247 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
248 					     union sctp_addr *addr);
249 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
250 				       struct sctp_chunk *asconf);
251 int sctp_process_asconf_ack(struct sctp_association *asoc,
252 			    struct sctp_chunk *asconf_ack);
253 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
254 				    __u32 new_cum_tsn, size_t nstreams,
255 				    struct sctp_fwdtsn_skip *skiplist);
256 
257 void sctp_chunk_assign_tsn(struct sctp_chunk *);
258 void sctp_chunk_assign_ssn(struct sctp_chunk *);
259 
260 void sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands, __u16 error);
261 
262 /* Prototypes for statetable processing. */
263 
264 int sctp_do_sm(sctp_event_t event_type, sctp_subtype_t subtype,
265 	       sctp_state_t state,
266                struct sctp_endpoint *,
267                struct sctp_association *asoc,
268                void *event_arg,
269                int gfp);
270 
271 /* 2nd level prototypes */
272 void sctp_generate_t3_rtx_event(unsigned long peer);
273 void sctp_generate_heartbeat_event(unsigned long peer);
274 
275 void sctp_ootb_pkt_free(struct sctp_packet *);
276 
277 struct sctp_association *sctp_unpack_cookie(const struct sctp_endpoint *,
278 				       const struct sctp_association *,
279 				       struct sctp_chunk *, int gfp, int *err,
280 				       struct sctp_chunk **err_chk_p);
281 int sctp_addip_addr_config(struct sctp_association *, sctp_param_t,
282 			   struct sockaddr_storage*, int);
283 
284 /* 3rd level prototypes */
285 __u32 sctp_generate_tag(const struct sctp_endpoint *);
286 __u32 sctp_generate_tsn(const struct sctp_endpoint *);
287 
288 /* Extern declarations for major data structures.  */
289 extern sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES];
290 
291 
292 /* Get the size of a DATA chunk payload. */
sctp_data_size(struct sctp_chunk * chunk)293 static inline __u16 sctp_data_size(struct sctp_chunk *chunk)
294 {
295 	__u16 size;
296 
297 	size = ntohs(chunk->chunk_hdr->length);
298 	size -= sizeof(sctp_data_chunk_t);
299 
300 	return size;
301 }
302 
303 /* Compare two TSNs */
304 
305 /* RFC 1982 - Serial Number Arithmetic
306  *
307  * 2. Comparison
308  *  Then, s1 is said to be equal to s2 if and only if i1 is equal to i2,
309  *  in all other cases, s1 is not equal to s2.
310  *
311  * s1 is said to be less than s2 if, and only if, s1 is not equal to s2,
312  * and
313  *
314  *      (i1 < i2 and i2 - i1 < 2^(SERIAL_BITS - 1)) or
315  *      (i1 > i2 and i1 - i2 > 2^(SERIAL_BITS - 1))
316  *
317  * s1 is said to be greater than s2 if, and only if, s1 is not equal to
318  * s2, and
319  *
320  *      (i1 < i2 and i2 - i1 > 2^(SERIAL_BITS - 1)) or
321  *      (i1 > i2 and i1 - i2 < 2^(SERIAL_BITS - 1))
322  */
323 
324 /*
325  * RFC 2960
326  *  1.6 Serial Number Arithmetic
327  *
328  * Comparisons and arithmetic on TSNs in this document SHOULD use Serial
329  * Number Arithmetic as defined in [RFC1982] where SERIAL_BITS = 32.
330  */
331 
332 enum {
333 	TSN_SIGN_BIT = (1<<31)
334 };
335 
TSN_lt(__u32 s,__u32 t)336 static inline int TSN_lt(__u32 s, __u32 t)
337 {
338 	return (((s) - (t)) & TSN_SIGN_BIT);
339 }
340 
TSN_lte(__u32 s,__u32 t)341 static inline int TSN_lte(__u32 s, __u32 t)
342 {
343 	return (((s) == (t)) || (((s) - (t)) & TSN_SIGN_BIT));
344 }
345 
346 /* Compare two SSNs */
347 
348 /*
349  * RFC 2960
350  *  1.6 Serial Number Arithmetic
351  *
352  * Comparisons and arithmetic on Stream Sequence Numbers in this document
353  * SHOULD use Serial Number Arithmetic as defined in [RFC1982] where
354  * SERIAL_BITS = 16.
355  */
356 enum {
357 	SSN_SIGN_BIT = (1<<15)
358 };
359 
SSN_lt(__u16 s,__u16 t)360 static inline int SSN_lt(__u16 s, __u16 t)
361 {
362 	return (((s) - (t)) & SSN_SIGN_BIT);
363 }
364 
SSN_lte(__u16 s,__u16 t)365 static inline int SSN_lte(__u16 s, __u16 t)
366 {
367 	return (((s) == (t)) || (((s) - (t)) & SSN_SIGN_BIT));
368 }
369 
370 /*
371  * ADDIP 3.1.1
372  * The valid range of Serial Number is from 0 to 4294967295 (2**32 - 1). Serial
373  * Numbers wrap back to 0 after reaching 4294967295.
374  */
375 enum {
376 	ADDIP_SERIAL_SIGN_BIT = (1<<31)
377 };
378 
ADDIP_SERIAL_gte(__u16 s,__u16 t)379 static inline int ADDIP_SERIAL_gte(__u16 s, __u16 t)
380 {
381 	return (((s) == (t)) || (((t) - (s)) & ADDIP_SERIAL_SIGN_BIT));
382 }
383 
384 
385 /* Run sctp_add_cmd() generating a BUG() if there is a failure.  */
sctp_add_cmd_sf(sctp_cmd_seq_t * seq,sctp_verb_t verb,sctp_arg_t obj)386 static inline void sctp_add_cmd_sf(sctp_cmd_seq_t *seq, sctp_verb_t verb, sctp_arg_t obj)
387 {
388 	if (unlikely(!sctp_add_cmd(seq, verb, obj)))
389 		BUG();
390 }
391 
392 /* Check VTAG of the packet matches the sender's own tag. */
393 static inline int
sctp_vtag_verify(const struct sctp_chunk * chunk,const struct sctp_association * asoc)394 sctp_vtag_verify(const struct sctp_chunk *chunk,
395 		 const struct sctp_association *asoc)
396 {
397 	/* RFC 2960 Sec 8.5 When receiving an SCTP packet, the endpoint
398 	 * MUST ensure that the value in the Verification Tag field of
399 	 * the received SCTP packet matches its own Tag. If the received
400 	 * Verification Tag value does not match the receiver's own
401 	 * tag value, the receiver shall silently discard the packet...
402 	 */
403         if (ntohl(chunk->sctp_hdr->vtag) == asoc->c.my_vtag)
404                 return 1;
405 
406 	return 0;
407 }
408 
409 /* Check VTAG of the packet matches the sender's own tag OR its peer's
410  * tag and the T bit is set in the Chunk Flags.
411  */
412 static inline int
sctp_vtag_verify_either(const struct sctp_chunk * chunk,const struct sctp_association * asoc)413 sctp_vtag_verify_either(const struct sctp_chunk *chunk,
414 			const struct sctp_association *asoc)
415 {
416         /* RFC 2960 Section 8.5.1, sctpimpguide-06 Section 2.13.2
417 	 *
418 	 * B) The receiver of a ABORT shall accept the packet if the
419 	 * Verification Tag field of the packet matches its own tag OR it
420 	 * is set to its peer's tag and the T bit is set in the Chunk
421 	 * Flags. Otherwise, the receiver MUST silently discard the packet
422 	 * and take no further action.
423 	 *
424 	 * (C) The receiver of a SHUTDOWN COMPLETE shall accept the
425 	 * packet if the Verification Tag field of the packet
426 	 * matches its own tag OR it is set to its peer's tag and
427 	 * the T bit is set in the Chunk Flags.  Otherwise, the
428 	 * receiver MUST silently discard the packet and take no
429 	 * further action....
430 	 *
431 	 */
432         if ((ntohl(chunk->sctp_hdr->vtag) == asoc->c.my_vtag) ||
433 	    (sctp_test_T_bit(chunk) && (ntohl(chunk->sctp_hdr->vtag)
434 	    == asoc->c.peer_vtag))) {
435                 return 1;
436 	}
437 
438 	return 0;
439 }
440 
441 #endif /* __sctp_sm_h__ */
442