sm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. * These are definitions needed by the state machine.
  10. *
  11. * This SCTP 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. * This SCTP 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, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email addresses:
  29. * lksctp developers <linux-sctp@vger.kernel.org>
  30. *
  31. * Written or modified by:
  32. * La Monte H.P. Yarroll <piggy@acm.org>
  33. * Karl Knutson <karl@athena.chicago.il.us>
  34. * Xingang Guo <xingang.guo@intel.com>
  35. * Jon Grimm <jgrimm@us.ibm.com>
  36. * Dajiang Zhang <dajiang.zhang@nokia.com>
  37. * Sridhar Samudrala <sri@us.ibm.com>
  38. * Daisy Chang <daisyc@us.ibm.com>
  39. * Ardelle Fan <ardelle.fan@intel.com>
  40. * Kevin Gao <kevin.gao@intel.com>
  41. */
  42. #include <linux/types.h>
  43. #include <linux/compiler.h>
  44. #include <linux/slab.h>
  45. #include <linux/in.h>
  46. #include <net/sctp/command.h>
  47. #include <net/sctp/sctp.h>
  48. #ifndef __sctp_sm_h__
  49. #define __sctp_sm_h__
  50. /*
  51. * Possible values for the disposition are:
  52. */
  53. enum sctp_disposition {
  54. SCTP_DISPOSITION_DISCARD, /* No further processing. */
  55. SCTP_DISPOSITION_CONSUME, /* Process return values normally. */
  56. SCTP_DISPOSITION_NOMEM, /* We ran out of memory--recover. */
  57. SCTP_DISPOSITION_DELETE_TCB, /* Close the association. */
  58. SCTP_DISPOSITION_ABORT, /* Close the association NOW. */
  59. SCTP_DISPOSITION_VIOLATION, /* The peer is misbehaving. */
  60. SCTP_DISPOSITION_NOT_IMPL, /* This entry is not implemented. */
  61. SCTP_DISPOSITION_ERROR, /* This is plain old user error. */
  62. SCTP_DISPOSITION_BUG, /* This is a bug. */
  63. };
  64. typedef enum sctp_disposition (sctp_state_fn_t) (
  65. struct net *net,
  66. const struct sctp_endpoint *ep,
  67. const struct sctp_association *asoc,
  68. const union sctp_subtype type,
  69. void *arg,
  70. struct sctp_cmd_seq *commands);
  71. typedef void (sctp_timer_event_t) (unsigned long);
  72. struct sctp_sm_table_entry {
  73. sctp_state_fn_t *fn;
  74. const char *name;
  75. };
  76. /* A naming convention of "sctp_sf_xxx" applies to all the state functions
  77. * currently in use.
  78. */
  79. /* Prototypes for generic state functions. */
  80. sctp_state_fn_t sctp_sf_not_impl;
  81. sctp_state_fn_t sctp_sf_bug;
  82. /* Prototypes for gener timer state functions. */
  83. sctp_state_fn_t sctp_sf_timer_ignore;
  84. /* Prototypes for chunk state functions. */
  85. sctp_state_fn_t sctp_sf_do_9_1_abort;
  86. sctp_state_fn_t sctp_sf_cookie_wait_abort;
  87. sctp_state_fn_t sctp_sf_cookie_echoed_abort;
  88. sctp_state_fn_t sctp_sf_shutdown_pending_abort;
  89. sctp_state_fn_t sctp_sf_shutdown_sent_abort;
  90. sctp_state_fn_t sctp_sf_shutdown_ack_sent_abort;
  91. sctp_state_fn_t sctp_sf_do_5_1B_init;
  92. sctp_state_fn_t sctp_sf_do_5_1C_ack;
  93. sctp_state_fn_t sctp_sf_do_5_1D_ce;
  94. sctp_state_fn_t sctp_sf_do_5_1E_ca;
  95. sctp_state_fn_t sctp_sf_do_4_C;
  96. sctp_state_fn_t sctp_sf_eat_data_6_2;
  97. sctp_state_fn_t sctp_sf_eat_data_fast_4_4;
  98. sctp_state_fn_t sctp_sf_eat_sack_6_2;
  99. sctp_state_fn_t sctp_sf_operr_notify;
  100. sctp_state_fn_t sctp_sf_t1_init_timer_expire;
  101. sctp_state_fn_t sctp_sf_t1_cookie_timer_expire;
  102. sctp_state_fn_t sctp_sf_t2_timer_expire;
  103. sctp_state_fn_t sctp_sf_t4_timer_expire;
  104. sctp_state_fn_t sctp_sf_t5_timer_expire;
  105. sctp_state_fn_t sctp_sf_sendbeat_8_3;
  106. sctp_state_fn_t sctp_sf_beat_8_3;
  107. sctp_state_fn_t sctp_sf_backbeat_8_3;
  108. sctp_state_fn_t sctp_sf_do_9_2_final;
  109. sctp_state_fn_t sctp_sf_do_9_2_shutdown;
  110. sctp_state_fn_t sctp_sf_do_9_2_shut_ctsn;
  111. sctp_state_fn_t sctp_sf_do_ecn_cwr;
  112. sctp_state_fn_t sctp_sf_do_ecne;
  113. sctp_state_fn_t sctp_sf_ootb;
  114. sctp_state_fn_t sctp_sf_pdiscard;
  115. sctp_state_fn_t sctp_sf_violation;
  116. sctp_state_fn_t sctp_sf_discard_chunk;
  117. sctp_state_fn_t sctp_sf_do_5_2_1_siminit;
  118. sctp_state_fn_t sctp_sf_do_5_2_2_dupinit;
  119. sctp_state_fn_t sctp_sf_do_5_2_3_initack;
  120. sctp_state_fn_t sctp_sf_do_5_2_4_dupcook;
  121. sctp_state_fn_t sctp_sf_unk_chunk;
  122. sctp_state_fn_t sctp_sf_do_8_5_1_E_sa;
  123. sctp_state_fn_t sctp_sf_cookie_echoed_err;
  124. sctp_state_fn_t sctp_sf_do_asconf;
  125. sctp_state_fn_t sctp_sf_do_asconf_ack;
  126. sctp_state_fn_t sctp_sf_do_reconf;
  127. sctp_state_fn_t sctp_sf_do_9_2_reshutack;
  128. sctp_state_fn_t sctp_sf_eat_fwd_tsn;
  129. sctp_state_fn_t sctp_sf_eat_fwd_tsn_fast;
  130. sctp_state_fn_t sctp_sf_eat_auth;
  131. /* Prototypes for primitive event state functions. */
  132. sctp_state_fn_t sctp_sf_do_prm_asoc;
  133. sctp_state_fn_t sctp_sf_do_prm_send;
  134. sctp_state_fn_t sctp_sf_do_9_2_prm_shutdown;
  135. sctp_state_fn_t sctp_sf_cookie_wait_prm_shutdown;
  136. sctp_state_fn_t sctp_sf_cookie_echoed_prm_shutdown;
  137. sctp_state_fn_t sctp_sf_do_9_1_prm_abort;
  138. sctp_state_fn_t sctp_sf_cookie_wait_prm_abort;
  139. sctp_state_fn_t sctp_sf_cookie_echoed_prm_abort;
  140. sctp_state_fn_t sctp_sf_shutdown_pending_prm_abort;
  141. sctp_state_fn_t sctp_sf_shutdown_sent_prm_abort;
  142. sctp_state_fn_t sctp_sf_shutdown_ack_sent_prm_abort;
  143. sctp_state_fn_t sctp_sf_error_closed;
  144. sctp_state_fn_t sctp_sf_error_shutdown;
  145. sctp_state_fn_t sctp_sf_ignore_primitive;
  146. sctp_state_fn_t sctp_sf_do_prm_requestheartbeat;
  147. sctp_state_fn_t sctp_sf_do_prm_asconf;
  148. sctp_state_fn_t sctp_sf_do_prm_reconf;
  149. /* Prototypes for other event state functions. */
  150. sctp_state_fn_t sctp_sf_do_no_pending_tsn;
  151. sctp_state_fn_t sctp_sf_do_9_2_start_shutdown;
  152. sctp_state_fn_t sctp_sf_do_9_2_shutdown_ack;
  153. sctp_state_fn_t sctp_sf_ignore_other;
  154. sctp_state_fn_t sctp_sf_cookie_wait_icmp_abort;
  155. /* Prototypes for timeout event state functions. */
  156. sctp_state_fn_t sctp_sf_do_6_3_3_rtx;
  157. sctp_state_fn_t sctp_sf_send_reconf;
  158. sctp_state_fn_t sctp_sf_do_6_2_sack;
  159. sctp_state_fn_t sctp_sf_autoclose_timer_expire;
  160. /* Prototypes for utility support functions. */
  161. __u8 sctp_get_chunk_type(struct sctp_chunk *chunk);
  162. const struct sctp_sm_table_entry *sctp_sm_lookup_event(
  163. struct net *net,
  164. enum sctp_event event_type,
  165. enum sctp_state state,
  166. union sctp_subtype event_subtype);
  167. int sctp_chunk_iif(const struct sctp_chunk *);
  168. struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *,
  169. struct sctp_chunk *,
  170. gfp_t gfp);
  171. __u32 sctp_generate_verification_tag(void);
  172. void sctp_populate_tie_tags(__u8 *cookie, __u32 curTag, __u32 hisTag);
  173. /* Prototypes for chunk-building functions. */
  174. struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
  175. const struct sctp_bind_addr *bp,
  176. gfp_t gfp, int vparam_len);
  177. struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
  178. const struct sctp_chunk *chunk,
  179. const gfp_t gfp, const int unkparam_len);
  180. struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
  181. const struct sctp_chunk *chunk);
  182. struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
  183. const struct sctp_chunk *chunk);
  184. struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
  185. const __u32 lowest_tsn,
  186. const struct sctp_chunk *chunk);
  187. struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
  188. const struct sctp_sndrcvinfo *sinfo,
  189. int len, const __u8 flags,
  190. __u16 ssn, gfp_t gfp);
  191. struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
  192. const __u32 lowest_tsn);
  193. struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc);
  194. struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
  195. const struct sctp_chunk *chunk);
  196. struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
  197. const struct sctp_chunk *chunk);
  198. struct sctp_chunk *sctp_make_shutdown_complete(
  199. const struct sctp_association *asoc,
  200. const struct sctp_chunk *chunk);
  201. void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause, size_t paylen);
  202. struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
  203. const struct sctp_chunk *chunk,
  204. const size_t hint);
  205. struct sctp_chunk *sctp_make_abort_no_data(const struct sctp_association *asoc,
  206. const struct sctp_chunk *chunk,
  207. __u32 tsn);
  208. struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
  209. struct msghdr *msg, size_t msg_len);
  210. struct sctp_chunk *sctp_make_abort_violation(
  211. const struct sctp_association *asoc,
  212. const struct sctp_chunk *chunk,
  213. const __u8 *payload,
  214. const size_t paylen);
  215. struct sctp_chunk *sctp_make_violation_paramlen(
  216. const struct sctp_association *asoc,
  217. const struct sctp_chunk *chunk,
  218. struct sctp_paramhdr *param);
  219. struct sctp_chunk *sctp_make_violation_max_retrans(
  220. const struct sctp_association *asoc,
  221. const struct sctp_chunk *chunk);
  222. struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
  223. const struct sctp_transport *transport);
  224. struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
  225. const struct sctp_chunk *chunk,
  226. const void *payload,
  227. const size_t paylen);
  228. struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
  229. const struct sctp_chunk *chunk,
  230. __be16 cause_code, const void *payload,
  231. size_t paylen, size_t reserve_tail);
  232. struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
  233. union sctp_addr *laddr,
  234. struct sockaddr *addrs,
  235. int addrcnt, __be16 flags);
  236. struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
  237. union sctp_addr *addr);
  238. bool sctp_verify_asconf(const struct sctp_association *asoc,
  239. struct sctp_chunk *chunk, bool addr_param_needed,
  240. struct sctp_paramhdr **errp);
  241. struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
  242. struct sctp_chunk *asconf);
  243. int sctp_process_asconf_ack(struct sctp_association *asoc,
  244. struct sctp_chunk *asconf_ack);
  245. struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
  246. __u32 new_cum_tsn, size_t nstreams,
  247. struct sctp_fwdtsn_skip *skiplist);
  248. struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc);
  249. struct sctp_chunk *sctp_make_strreset_req(const struct sctp_association *asoc,
  250. __u16 stream_num, __be16 *stream_list,
  251. bool out, bool in);
  252. struct sctp_chunk *sctp_make_strreset_tsnreq(
  253. const struct sctp_association *asoc);
  254. struct sctp_chunk *sctp_make_strreset_addstrm(
  255. const struct sctp_association *asoc,
  256. __u16 out, __u16 in);
  257. struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
  258. __u32 result, __u32 sn);
  259. struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
  260. __u32 result, __u32 sn,
  261. __u32 sender_tsn,
  262. __u32 receiver_tsn);
  263. bool sctp_verify_reconf(const struct sctp_association *asoc,
  264. struct sctp_chunk *chunk,
  265. struct sctp_paramhdr **errp);
  266. void sctp_chunk_assign_tsn(struct sctp_chunk *chunk);
  267. void sctp_chunk_assign_ssn(struct sctp_chunk *chunk);
  268. /* Prototypes for stream-processing functions. */
  269. struct sctp_chunk *sctp_process_strreset_outreq(
  270. struct sctp_association *asoc,
  271. union sctp_params param,
  272. struct sctp_ulpevent **evp);
  273. struct sctp_chunk *sctp_process_strreset_inreq(
  274. struct sctp_association *asoc,
  275. union sctp_params param,
  276. struct sctp_ulpevent **evp);
  277. struct sctp_chunk *sctp_process_strreset_tsnreq(
  278. struct sctp_association *asoc,
  279. union sctp_params param,
  280. struct sctp_ulpevent **evp);
  281. struct sctp_chunk *sctp_process_strreset_addstrm_out(
  282. struct sctp_association *asoc,
  283. union sctp_params param,
  284. struct sctp_ulpevent **evp);
  285. struct sctp_chunk *sctp_process_strreset_addstrm_in(
  286. struct sctp_association *asoc,
  287. union sctp_params param,
  288. struct sctp_ulpevent **evp);
  289. struct sctp_chunk *sctp_process_strreset_resp(
  290. struct sctp_association *asoc,
  291. union sctp_params param,
  292. struct sctp_ulpevent **evp);
  293. /* Prototypes for statetable processing. */
  294. int sctp_do_sm(struct net *net, enum sctp_event event_type,
  295. union sctp_subtype subtype, enum sctp_state state,
  296. struct sctp_endpoint *ep, struct sctp_association *asoc,
  297. void *event_arg, gfp_t gfp);
  298. /* 2nd level prototypes */
  299. void sctp_generate_t3_rtx_event(unsigned long peer);
  300. void sctp_generate_heartbeat_event(unsigned long peer);
  301. void sctp_generate_reconf_event(unsigned long peer);
  302. void sctp_generate_proto_unreach_event(unsigned long peer);
  303. void sctp_ootb_pkt_free(struct sctp_packet *packet);
  304. struct sctp_association *sctp_unpack_cookie(
  305. const struct sctp_endpoint *ep,
  306. const struct sctp_association *asoc,
  307. struct sctp_chunk *chunk,
  308. gfp_t gfp, int *err,
  309. struct sctp_chunk **err_chk_p);
  310. /* 3rd level prototypes */
  311. __u32 sctp_generate_tag(const struct sctp_endpoint *ep);
  312. __u32 sctp_generate_tsn(const struct sctp_endpoint *ep);
  313. /* Extern declarations for major data structures. */
  314. extern sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES];
  315. /* Get the size of a DATA chunk payload. */
  316. static inline __u16 sctp_data_size(struct sctp_chunk *chunk)
  317. {
  318. __u16 size;
  319. size = ntohs(chunk->chunk_hdr->length);
  320. size -= sizeof(struct sctp_data_chunk);
  321. return size;
  322. }
  323. /* Compare two TSNs */
  324. #define TSN_lt(a,b) \
  325. (typecheck(__u32, a) && \
  326. typecheck(__u32, b) && \
  327. ((__s32)((a) - (b)) < 0))
  328. #define TSN_lte(a,b) \
  329. (typecheck(__u32, a) && \
  330. typecheck(__u32, b) && \
  331. ((__s32)((a) - (b)) <= 0))
  332. /* Compare two SSNs */
  333. #define SSN_lt(a,b) \
  334. (typecheck(__u16, a) && \
  335. typecheck(__u16, b) && \
  336. ((__s16)((a) - (b)) < 0))
  337. /* ADDIP 3.1.1 */
  338. #define ADDIP_SERIAL_gte(a,b) \
  339. (typecheck(__u32, a) && \
  340. typecheck(__u32, b) && \
  341. ((__s32)((b) - (a)) <= 0))
  342. /* Check VTAG of the packet matches the sender's own tag. */
  343. static inline int
  344. sctp_vtag_verify(const struct sctp_chunk *chunk,
  345. const struct sctp_association *asoc)
  346. {
  347. /* RFC 2960 Sec 8.5 When receiving an SCTP packet, the endpoint
  348. * MUST ensure that the value in the Verification Tag field of
  349. * the received SCTP packet matches its own Tag. If the received
  350. * Verification Tag value does not match the receiver's own
  351. * tag value, the receiver shall silently discard the packet...
  352. */
  353. if (ntohl(chunk->sctp_hdr->vtag) == asoc->c.my_vtag)
  354. return 1;
  355. return 0;
  356. }
  357. /* Check VTAG of the packet matches the sender's own tag and the T bit is
  358. * not set, OR its peer's tag and the T bit is set in the Chunk Flags.
  359. */
  360. static inline int
  361. sctp_vtag_verify_either(const struct sctp_chunk *chunk,
  362. const struct sctp_association *asoc)
  363. {
  364. /* RFC 2960 Section 8.5.1, sctpimpguide Section 2.41
  365. *
  366. * B) The receiver of a ABORT MUST accept the packet
  367. * if the Verification Tag field of the packet matches its own tag
  368. * and the T bit is not set
  369. * OR
  370. * it is set to its peer's tag and the T bit is set in the Chunk
  371. * Flags.
  372. * Otherwise, the receiver MUST silently discard the packet
  373. * and take no further action.
  374. *
  375. * C) The receiver of a SHUTDOWN COMPLETE shall accept the packet
  376. * if the Verification Tag field of the packet matches its own tag
  377. * and the T bit is not set
  378. * OR
  379. * it is set to its peer's tag and the T bit is set in the Chunk
  380. * Flags.
  381. * Otherwise, the receiver MUST silently discard the packet
  382. * and take no further action. An endpoint MUST ignore the
  383. * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
  384. */
  385. if ((!sctp_test_T_bit(chunk) &&
  386. (ntohl(chunk->sctp_hdr->vtag) == asoc->c.my_vtag)) ||
  387. (sctp_test_T_bit(chunk) && asoc->c.peer_vtag &&
  388. (ntohl(chunk->sctp_hdr->vtag) == asoc->c.peer_vtag))) {
  389. return 1;
  390. }
  391. return 0;
  392. }
  393. #endif /* __sctp_sm_h__ */