sm.h 16 KB

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