sm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. int sctp_chunk_iif(const struct sctp_chunk *);
  153. struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *,
  154. struct sctp_chunk *,
  155. gfp_t gfp);
  156. /* Prototypes for chunk-building functions. */
  157. struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
  158. const struct sctp_bind_addr *bp,
  159. gfp_t gfp, int vparam_len);
  160. struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
  161. const struct sctp_chunk *chunk,
  162. const gfp_t gfp, const int unkparam_len);
  163. struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
  164. const struct sctp_chunk *chunk);
  165. struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
  166. const struct sctp_chunk *chunk);
  167. struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
  168. const __u32 lowest_tsn,
  169. const struct sctp_chunk *chunk);
  170. struct sctp_chunk *sctp_make_idata(const struct sctp_association *asoc,
  171. __u8 flags, int paylen, gfp_t gfp);
  172. struct sctp_chunk *sctp_make_ifwdtsn(const struct sctp_association *asoc,
  173. __u32 new_cum_tsn, size_t nstreams,
  174. struct sctp_ifwdtsn_skip *skiplist);
  175. struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
  176. const struct sctp_sndrcvinfo *sinfo,
  177. int len, __u8 flags, gfp_t gfp);
  178. struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
  179. const __u32 lowest_tsn);
  180. struct sctp_chunk *sctp_make_sack(struct sctp_association *asoc);
  181. struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
  182. const struct sctp_chunk *chunk);
  183. struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
  184. const struct sctp_chunk *chunk);
  185. struct sctp_chunk *sctp_make_shutdown_complete(
  186. const struct sctp_association *asoc,
  187. const struct sctp_chunk *chunk);
  188. int sctp_init_cause(struct sctp_chunk *chunk, __be16 cause, size_t paylen);
  189. struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
  190. const struct sctp_chunk *chunk,
  191. const size_t hint);
  192. struct sctp_chunk *sctp_make_abort_no_data(const struct sctp_association *asoc,
  193. const struct sctp_chunk *chunk,
  194. __u32 tsn);
  195. struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
  196. struct msghdr *msg, size_t msg_len);
  197. struct sctp_chunk *sctp_make_abort_violation(
  198. const struct sctp_association *asoc,
  199. const struct sctp_chunk *chunk,
  200. const __u8 *payload,
  201. const size_t paylen);
  202. struct sctp_chunk *sctp_make_violation_paramlen(
  203. const struct sctp_association *asoc,
  204. const struct sctp_chunk *chunk,
  205. struct sctp_paramhdr *param);
  206. struct sctp_chunk *sctp_make_violation_max_retrans(
  207. const struct sctp_association *asoc,
  208. const struct sctp_chunk *chunk);
  209. struct sctp_chunk *sctp_make_new_encap_port(
  210. const struct sctp_association *asoc,
  211. const struct sctp_chunk *chunk);
  212. struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
  213. const struct sctp_transport *transport,
  214. __u32 probe_size);
  215. struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
  216. const struct sctp_chunk *chunk,
  217. const void *payload,
  218. const size_t paylen);
  219. struct sctp_chunk *sctp_make_pad(const struct sctp_association *asoc, int len);
  220. struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
  221. const struct sctp_chunk *chunk,
  222. __be16 cause_code, const void *payload,
  223. size_t paylen, size_t reserve_tail);
  224. struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
  225. union sctp_addr *laddr,
  226. struct sockaddr *addrs,
  227. int addrcnt, __be16 flags);
  228. struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
  229. union sctp_addr *addr);
  230. bool sctp_verify_asconf(const struct sctp_association *asoc,
  231. struct sctp_chunk *chunk, bool addr_param_needed,
  232. struct sctp_paramhdr **errp);
  233. struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
  234. struct sctp_chunk *asconf);
  235. int sctp_process_asconf_ack(struct sctp_association *asoc,
  236. struct sctp_chunk *asconf_ack);
  237. struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
  238. __u32 new_cum_tsn, size_t nstreams,
  239. struct sctp_fwdtsn_skip *skiplist);
  240. struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc,
  241. __u16 key_id);
  242. struct sctp_chunk *sctp_make_strreset_req(const struct sctp_association *asoc,
  243. __u16 stream_num, __be16 *stream_list,
  244. bool out, bool in);
  245. struct sctp_chunk *sctp_make_strreset_tsnreq(
  246. const struct sctp_association *asoc);
  247. struct sctp_chunk *sctp_make_strreset_addstrm(
  248. const struct sctp_association *asoc,
  249. __u16 out, __u16 in);
  250. struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
  251. __u32 result, __u32 sn);
  252. struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
  253. __u32 result, __u32 sn,
  254. __u32 sender_tsn,
  255. __u32 receiver_tsn);
  256. bool sctp_verify_reconf(const struct sctp_association *asoc,
  257. struct sctp_chunk *chunk,
  258. struct sctp_paramhdr **errp);
  259. void sctp_chunk_assign_tsn(struct sctp_chunk *chunk);
  260. void sctp_chunk_assign_ssn(struct sctp_chunk *chunk);
  261. /* Prototypes for stream-processing functions. */
  262. struct sctp_chunk *sctp_process_strreset_outreq(
  263. struct sctp_association *asoc,
  264. union sctp_params param,
  265. struct sctp_ulpevent **evp);
  266. struct sctp_chunk *sctp_process_strreset_inreq(
  267. struct sctp_association *asoc,
  268. union sctp_params param,
  269. struct sctp_ulpevent **evp);
  270. struct sctp_chunk *sctp_process_strreset_tsnreq(
  271. struct sctp_association *asoc,
  272. union sctp_params param,
  273. struct sctp_ulpevent **evp);
  274. struct sctp_chunk *sctp_process_strreset_addstrm_out(
  275. struct sctp_association *asoc,
  276. union sctp_params param,
  277. struct sctp_ulpevent **evp);
  278. struct sctp_chunk *sctp_process_strreset_addstrm_in(
  279. struct sctp_association *asoc,
  280. union sctp_params param,
  281. struct sctp_ulpevent **evp);
  282. struct sctp_chunk *sctp_process_strreset_resp(
  283. struct sctp_association *asoc,
  284. union sctp_params param,
  285. struct sctp_ulpevent **evp);
  286. /* Prototypes for statetable processing. */
  287. int sctp_do_sm(struct net *net, enum sctp_event_type event_type,
  288. union sctp_subtype subtype, enum sctp_state state,
  289. struct sctp_endpoint *ep, struct sctp_association *asoc,
  290. void *event_arg, gfp_t gfp);
  291. /* 2nd level prototypes */
  292. void sctp_generate_t3_rtx_event(struct timer_list *t);
  293. void sctp_generate_heartbeat_event(struct timer_list *t);
  294. void sctp_generate_reconf_event(struct timer_list *t);
  295. void sctp_generate_probe_event(struct timer_list *t);
  296. void sctp_generate_proto_unreach_event(struct timer_list *t);
  297. void sctp_ootb_pkt_free(struct sctp_packet *packet);
  298. struct sctp_association *sctp_unpack_cookie(
  299. const struct sctp_endpoint *ep,
  300. const struct sctp_association *asoc,
  301. struct sctp_chunk *chunk,
  302. gfp_t gfp, int *err,
  303. struct sctp_chunk **err_chk_p);
  304. /* 3rd level prototypes */
  305. __u32 sctp_generate_tag(const struct sctp_endpoint *ep);
  306. __u32 sctp_generate_tsn(const struct sctp_endpoint *ep);
  307. /* Extern declarations for major data structures. */
  308. extern sctp_timer_event_t *sctp_timer_events[SCTP_NUM_TIMEOUT_TYPES];
  309. /* Get the size of a DATA chunk payload. */
  310. static inline __u16 sctp_data_size(struct sctp_chunk *chunk)
  311. {
  312. __u16 size;
  313. size = ntohs(chunk->chunk_hdr->length);
  314. size -= sctp_datachk_len(&chunk->asoc->stream);
  315. return size;
  316. }
  317. /* Compare two TSNs */
  318. #define TSN_lt(a,b) \
  319. (typecheck(__u32, a) && \
  320. typecheck(__u32, b) && \
  321. ((__s32)((a) - (b)) < 0))
  322. #define TSN_lte(a,b) \
  323. (typecheck(__u32, a) && \
  324. typecheck(__u32, b) && \
  325. ((__s32)((a) - (b)) <= 0))
  326. /* Compare two MIDs */
  327. #define MID_lt(a, b) \
  328. (typecheck(__u32, a) && \
  329. typecheck(__u32, b) && \
  330. ((__s32)((a) - (b)) < 0))
  331. /* Compare two SSNs */
  332. #define SSN_lt(a,b) \
  333. (typecheck(__u16, a) && \
  334. typecheck(__u16, b) && \
  335. ((__s16)((a) - (b)) < 0))
  336. /* ADDIP 3.1.1 */
  337. #define ADDIP_SERIAL_gte(a,b) \
  338. (typecheck(__u32, a) && \
  339. typecheck(__u32, b) && \
  340. ((__s32)((b) - (a)) <= 0))
  341. /* Check VTAG of the packet matches the sender's own tag. */
  342. static inline int
  343. sctp_vtag_verify(const struct sctp_chunk *chunk,
  344. const struct sctp_association *asoc)
  345. {
  346. /* RFC 2960 Sec 8.5 When receiving an SCTP packet, the endpoint
  347. * MUST ensure that the value in the Verification Tag field of
  348. * the received SCTP packet matches its own Tag. If the received
  349. * Verification Tag value does not match the receiver's own
  350. * tag value, the receiver shall silently discard the packet...
  351. */
  352. if (ntohl(chunk->sctp_hdr->vtag) != asoc->c.my_vtag)
  353. return 0;
  354. chunk->transport->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
  355. return 1;
  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__ */