sm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <[email protected]>
  15. *
  16. * Written or modified by:
  17. * La Monte H.P. Yarroll <[email protected]>
  18. * Karl Knutson <[email protected]>
  19. * Xingang Guo <[email protected]>
  20. * Jon Grimm <[email protected]>
  21. * Dajiang Zhang <[email protected]>
  22. * Sridhar Samudrala <[email protected]>
  23. * Daisy Chang <[email protected]>
  24. * Ardelle Fan <[email protected]>
  25. * Kevin Gao <[email protected]>
  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. __u8 sctp_get_chunk_type(struct sctp_chunk *chunk);
  148. const struct sctp_sm_table_entry *sctp_sm_lookup_event(
  149. struct net *net,
  150. enum sctp_event_type event_type,
  151. enum sctp_state state,
  152. union sctp_subtype event_subtype);
  153. int sctp_chunk_iif(const struct sctp_chunk *);
  154. struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *,
  155. struct sctp_chunk *,
  156. gfp_t gfp);
  157. __u32 sctp_generate_verification_tag(void);
  158. void sctp_populate_tie_tags(__u8 *cookie, __u32 curTag, __u32 hisTag);
  159. /* Prototypes for chunk-building functions. */
  160. struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
  161. const struct sctp_bind_addr *bp,
  162. gfp_t gfp, int vparam_len);
  163. struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
  164. const struct sctp_chunk *chunk,
  165. const gfp_t gfp, const int unkparam_len);
  166. struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
  167. const struct sctp_chunk *chunk);
  168. struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
  169. const struct sctp_chunk *chunk);
  170. struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
  171. const __u32 lowest_tsn,
  172. const struct sctp_chunk *chunk);
  173. struct sctp_chunk *sctp_make_idata(const struct sctp_association *asoc,
  174. __u8 flags, int paylen, gfp_t gfp);
  175. struct sctp_chunk *sctp_make_ifwdtsn(const struct sctp_association *asoc,
  176. __u32 new_cum_tsn, size_t nstreams,
  177. struct sctp_ifwdtsn_skip *skiplist);
  178. struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
  179. const struct sctp_sndrcvinfo *sinfo,
  180. int len, __u8 flags, gfp_t gfp);
  181. struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
  182. const __u32 lowest_tsn);
  183. struct sctp_chunk *sctp_make_sack(struct sctp_association *asoc);
  184. struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
  185. const struct sctp_chunk *chunk);
  186. struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
  187. const struct sctp_chunk *chunk);
  188. struct sctp_chunk *sctp_make_shutdown_complete(
  189. const struct sctp_association *asoc,
  190. const struct sctp_chunk *chunk);
  191. int sctp_init_cause(struct sctp_chunk *chunk, __be16 cause, size_t paylen);
  192. struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
  193. const struct sctp_chunk *chunk,
  194. const size_t hint);
  195. struct sctp_chunk *sctp_make_abort_no_data(const struct sctp_association *asoc,
  196. const struct sctp_chunk *chunk,
  197. __u32 tsn);
  198. struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
  199. struct msghdr *msg, size_t msg_len);
  200. struct sctp_chunk *sctp_make_abort_violation(
  201. const struct sctp_association *asoc,
  202. const struct sctp_chunk *chunk,
  203. const __u8 *payload,
  204. const size_t paylen);
  205. struct sctp_chunk *sctp_make_violation_paramlen(
  206. const struct sctp_association *asoc,
  207. const struct sctp_chunk *chunk,
  208. struct sctp_paramhdr *param);
  209. struct sctp_chunk *sctp_make_violation_max_retrans(
  210. const struct sctp_association *asoc,
  211. const struct sctp_chunk *chunk);
  212. struct sctp_chunk *sctp_make_new_encap_port(
  213. const struct sctp_association *asoc,
  214. const struct sctp_chunk *chunk);
  215. struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
  216. const struct sctp_transport *transport,
  217. __u32 probe_size);
  218. struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
  219. const struct sctp_chunk *chunk,
  220. const void *payload,
  221. const size_t paylen);
  222. struct sctp_chunk *sctp_make_pad(const struct sctp_association *asoc, int len);
  223. struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
  224. const struct sctp_chunk *chunk,
  225. __be16 cause_code, const void *payload,
  226. size_t paylen, size_t reserve_tail);
  227. struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
  228. union sctp_addr *laddr,
  229. struct sockaddr *addrs,
  230. int addrcnt, __be16 flags);
  231. struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
  232. union sctp_addr *addr);
  233. bool sctp_verify_asconf(const struct sctp_association *asoc,
  234. struct sctp_chunk *chunk, bool addr_param_needed,
  235. struct sctp_paramhdr **errp);
  236. struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
  237. struct sctp_chunk *asconf);
  238. int sctp_process_asconf_ack(struct sctp_association *asoc,
  239. struct sctp_chunk *asconf_ack);
  240. struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
  241. __u32 new_cum_tsn, size_t nstreams,
  242. struct sctp_fwdtsn_skip *skiplist);
  243. struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc,
  244. __u16 key_id);
  245. struct sctp_chunk *sctp_make_strreset_req(const struct sctp_association *asoc,
  246. __u16 stream_num, __be16 *stream_list,
  247. bool out, bool in);
  248. struct sctp_chunk *sctp_make_strreset_tsnreq(
  249. const struct sctp_association *asoc);
  250. struct sctp_chunk *sctp_make_strreset_addstrm(
  251. const struct sctp_association *asoc,
  252. __u16 out, __u16 in);
  253. struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
  254. __u32 result, __u32 sn);
  255. struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
  256. __u32 result, __u32 sn,
  257. __u32 sender_tsn,
  258. __u32 receiver_tsn);
  259. bool sctp_verify_reconf(const struct sctp_association *asoc,
  260. struct sctp_chunk *chunk,
  261. struct sctp_paramhdr **errp);
  262. void sctp_chunk_assign_tsn(struct sctp_chunk *chunk);
  263. void sctp_chunk_assign_ssn(struct sctp_chunk *chunk);
  264. /* Prototypes for stream-processing functions. */
  265. struct sctp_chunk *sctp_process_strreset_outreq(
  266. struct sctp_association *asoc,
  267. union sctp_params param,
  268. struct sctp_ulpevent **evp);
  269. struct sctp_chunk *sctp_process_strreset_inreq(
  270. struct sctp_association *asoc,
  271. union sctp_params param,
  272. struct sctp_ulpevent **evp);
  273. struct sctp_chunk *sctp_process_strreset_tsnreq(
  274. struct sctp_association *asoc,
  275. union sctp_params param,
  276. struct sctp_ulpevent **evp);
  277. struct sctp_chunk *sctp_process_strreset_addstrm_out(
  278. struct sctp_association *asoc,
  279. union sctp_params param,
  280. struct sctp_ulpevent **evp);
  281. struct sctp_chunk *sctp_process_strreset_addstrm_in(
  282. struct sctp_association *asoc,
  283. union sctp_params param,
  284. struct sctp_ulpevent **evp);
  285. struct sctp_chunk *sctp_process_strreset_resp(
  286. struct sctp_association *asoc,
  287. union sctp_params param,
  288. struct sctp_ulpevent **evp);
  289. /* Prototypes for statetable processing. */
  290. int sctp_do_sm(struct net *net, enum sctp_event_type event_type,
  291. union sctp_subtype subtype, enum sctp_state state,
  292. struct sctp_endpoint *ep, struct sctp_association *asoc,
  293. void *event_arg, gfp_t gfp);
  294. /* 2nd level prototypes */
  295. void sctp_generate_t3_rtx_event(struct timer_list *t);
  296. void sctp_generate_heartbeat_event(struct timer_list *t);
  297. void sctp_generate_reconf_event(struct timer_list *t);
  298. void sctp_generate_probe_event(struct timer_list *t);
  299. void sctp_generate_proto_unreach_event(struct timer_list *t);
  300. void sctp_ootb_pkt_free(struct sctp_packet *packet);
  301. struct sctp_association *sctp_unpack_cookie(
  302. const struct sctp_endpoint *ep,
  303. const struct sctp_association *asoc,
  304. struct sctp_chunk *chunk,
  305. gfp_t gfp, int *err,
  306. struct sctp_chunk **err_chk_p);
  307. /* 3rd level prototypes */
  308. __u32 sctp_generate_tag(const struct sctp_endpoint *ep);
  309. __u32 sctp_generate_tsn(const struct sctp_endpoint *ep);
  310. /* Extern declarations for major data structures. */
  311. extern sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES];
  312. /* Get the size of a DATA chunk payload. */
  313. static inline __u16 sctp_data_size(struct sctp_chunk *chunk)
  314. {
  315. __u16 size;
  316. size = ntohs(chunk->chunk_hdr->length);
  317. size -= sctp_datachk_len(&chunk->asoc->stream);
  318. return size;
  319. }
  320. /* Compare two TSNs */
  321. #define TSN_lt(a,b) \
  322. (typecheck(__u32, a) && \
  323. typecheck(__u32, b) && \
  324. ((__s32)((a) - (b)) < 0))
  325. #define TSN_lte(a,b) \
  326. (typecheck(__u32, a) && \
  327. typecheck(__u32, b) && \
  328. ((__s32)((a) - (b)) <= 0))
  329. /* Compare two MIDs */
  330. #define MID_lt(a, b) \
  331. (typecheck(__u32, a) && \
  332. typecheck(__u32, b) && \
  333. ((__s32)((a) - (b)) < 0))
  334. /* Compare two SSNs */
  335. #define SSN_lt(a,b) \
  336. (typecheck(__u16, a) && \
  337. typecheck(__u16, b) && \
  338. ((__s16)((a) - (b)) < 0))
  339. /* ADDIP 3.1.1 */
  340. #define ADDIP_SERIAL_gte(a,b) \
  341. (typecheck(__u32, a) && \
  342. typecheck(__u32, b) && \
  343. ((__s32)((b) - (a)) <= 0))
  344. /* Check VTAG of the packet matches the sender's own tag. */
  345. static inline int
  346. sctp_vtag_verify(const struct sctp_chunk *chunk,
  347. const struct sctp_association *asoc)
  348. {
  349. /* RFC 2960 Sec 8.5 When receiving an SCTP packet, the endpoint
  350. * MUST ensure that the value in the Verification Tag field of
  351. * the received SCTP packet matches its own Tag. If the received
  352. * Verification Tag value does not match the receiver's own
  353. * tag value, the receiver shall silently discard the packet...
  354. */
  355. if (ntohl(chunk->sctp_hdr->vtag) != asoc->c.my_vtag)
  356. return 0;
  357. chunk->transport->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
  358. return 1;
  359. }
  360. /* Check VTAG of the packet matches the sender's own tag and the T bit is
  361. * not set, OR its peer's tag and the T bit is set in the Chunk Flags.
  362. */
  363. static inline int
  364. sctp_vtag_verify_either(const struct sctp_chunk *chunk,
  365. const struct sctp_association *asoc)
  366. {
  367. /* RFC 2960 Section 8.5.1, sctpimpguide Section 2.41
  368. *
  369. * B) The receiver of a ABORT MUST accept the packet
  370. * if the Verification Tag field of the packet matches its own tag
  371. * and the T bit is not set
  372. * OR
  373. * it is set to its peer's tag and the T bit is set in the Chunk
  374. * Flags.
  375. * Otherwise, the receiver MUST silently discard the packet
  376. * and take no further action.
  377. *
  378. * C) The receiver of a SHUTDOWN COMPLETE shall accept the packet
  379. * if the Verification Tag field of the packet matches its own tag
  380. * and the T bit is not set
  381. * OR
  382. * it is set to its peer's tag and the T bit is set in the Chunk
  383. * Flags.
  384. * Otherwise, the receiver MUST silently discard the packet
  385. * and take no further action. An endpoint MUST ignore the
  386. * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
  387. */
  388. if ((!sctp_test_T_bit(chunk) &&
  389. (ntohl(chunk->sctp_hdr->vtag) == asoc->c.my_vtag)) ||
  390. (sctp_test_T_bit(chunk) && asoc->c.peer_vtag &&
  391. (ntohl(chunk->sctp_hdr->vtag) == asoc->c.peer_vtag))) {
  392. return 1;
  393. }
  394. return 0;
  395. }
  396. #endif /* __sctp_sm_h__ */