nf_conntrack_proto_sctp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Connection tracking protocol helper module for SCTP.
  4. *
  5. * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com>
  6. * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net>
  7. *
  8. * SCTP is defined in RFC 2960. References to various sections in this code
  9. * are to this RFC.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/timer.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/in.h>
  15. #include <linux/ip.h>
  16. #include <linux/sctp.h>
  17. #include <linux/string.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/interrupt.h>
  21. #include <net/sctp/checksum.h>
  22. #include <net/netfilter/nf_log.h>
  23. #include <net/netfilter/nf_conntrack.h>
  24. #include <net/netfilter/nf_conntrack_l4proto.h>
  25. #include <net/netfilter/nf_conntrack_ecache.h>
  26. #include <net/netfilter/nf_conntrack_timeout.h>
  27. /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
  28. closely. They're more complex. --RR
  29. And so for me for SCTP :D -Kiran */
  30. static const char *const sctp_conntrack_names[] = {
  31. "NONE",
  32. "CLOSED",
  33. "COOKIE_WAIT",
  34. "COOKIE_ECHOED",
  35. "ESTABLISHED",
  36. "SHUTDOWN_SENT",
  37. "SHUTDOWN_RECD",
  38. "SHUTDOWN_ACK_SENT",
  39. "HEARTBEAT_SENT",
  40. "HEARTBEAT_ACKED",
  41. };
  42. #define SECS * HZ
  43. #define MINS * 60 SECS
  44. #define HOURS * 60 MINS
  45. #define DAYS * 24 HOURS
  46. static const unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] = {
  47. [SCTP_CONNTRACK_CLOSED] = 10 SECS,
  48. [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
  49. [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
  50. [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS,
  51. [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000,
  52. [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000,
  53. [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
  54. [SCTP_CONNTRACK_HEARTBEAT_SENT] = 30 SECS,
  55. [SCTP_CONNTRACK_HEARTBEAT_ACKED] = 210 SECS,
  56. };
  57. #define SCTP_FLAG_HEARTBEAT_VTAG_FAILED 1
  58. #define sNO SCTP_CONNTRACK_NONE
  59. #define sCL SCTP_CONNTRACK_CLOSED
  60. #define sCW SCTP_CONNTRACK_COOKIE_WAIT
  61. #define sCE SCTP_CONNTRACK_COOKIE_ECHOED
  62. #define sES SCTP_CONNTRACK_ESTABLISHED
  63. #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
  64. #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
  65. #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
  66. #define sHS SCTP_CONNTRACK_HEARTBEAT_SENT
  67. #define sHA SCTP_CONNTRACK_HEARTBEAT_ACKED
  68. #define sIV SCTP_CONNTRACK_MAX
  69. /*
  70. These are the descriptions of the states:
  71. NOTE: These state names are tantalizingly similar to the states of an
  72. SCTP endpoint. But the interpretation of the states is a little different,
  73. considering that these are the states of the connection and not of an end
  74. point. Please note the subtleties. -Kiran
  75. NONE - Nothing so far.
  76. COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
  77. an INIT_ACK chunk in the reply direction.
  78. COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
  79. ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
  80. SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
  81. SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
  82. SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
  83. to that of the SHUTDOWN chunk.
  84. CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
  85. the SHUTDOWN chunk. Connection is closed.
  86. HEARTBEAT_SENT - We have seen a HEARTBEAT in a new flow.
  87. HEARTBEAT_ACKED - We have seen a HEARTBEAT-ACK in the direction opposite to
  88. that of the HEARTBEAT chunk. Secondary connection is
  89. established.
  90. */
  91. /* TODO
  92. - I have assumed that the first INIT is in the original direction.
  93. This messes things when an INIT comes in the reply direction in CLOSED
  94. state.
  95. - Check the error type in the reply dir before transitioning from
  96. cookie echoed to closed.
  97. - Sec 5.2.4 of RFC 2960
  98. - Full Multi Homing support.
  99. */
  100. /* SCTP conntrack state transitions */
  101. static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {
  102. {
  103. /* ORIGINAL */
  104. /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
  105. /* init */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA},
  106. /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},
  107. /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
  108. /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS},
  109. /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA, sHA},
  110. /* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't have Stale cookie*/
  111. /* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* 5.2.4 - Big TODO */
  112. /* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't come in orig dir */
  113. /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL, sHA},
  114. /* heartbeat */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
  115. /* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}
  116. },
  117. {
  118. /* REPLY */
  119. /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
  120. /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */
  121. /* init_ack */ {sIV, sCW, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},
  122. /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL},
  123. /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR},
  124. /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA},
  125. /* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV, sHA},
  126. /* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* Can't come in reply dir */
  127. /* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV, sHA},
  128. /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV, sHA},
  129. /* heartbeat */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
  130. /* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHA, sHA}
  131. }
  132. };
  133. #ifdef CONFIG_NF_CONNTRACK_PROCFS
  134. /* Print out the private part of the conntrack. */
  135. static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
  136. {
  137. seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
  138. }
  139. #endif
  140. #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
  141. for ((offset) = (dataoff) + sizeof(struct sctphdr), (count) = 0; \
  142. (offset) < (skb)->len && \
  143. ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
  144. (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
  145. /* Some validity checks to make sure the chunks are fine */
  146. static int do_basic_checks(struct nf_conn *ct,
  147. const struct sk_buff *skb,
  148. unsigned int dataoff,
  149. unsigned long *map)
  150. {
  151. u_int32_t offset, count;
  152. struct sctp_chunkhdr _sch, *sch;
  153. int flag;
  154. flag = 0;
  155. for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
  156. pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
  157. if (sch->type == SCTP_CID_INIT ||
  158. sch->type == SCTP_CID_INIT_ACK ||
  159. sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
  160. flag = 1;
  161. /*
  162. * Cookie Ack/Echo chunks not the first OR
  163. * Init / Init Ack / Shutdown compl chunks not the only chunks
  164. * OR zero-length.
  165. */
  166. if (((sch->type == SCTP_CID_COOKIE_ACK ||
  167. sch->type == SCTP_CID_COOKIE_ECHO ||
  168. flag) &&
  169. count != 0) || !sch->length) {
  170. pr_debug("Basic checks failed\n");
  171. return 1;
  172. }
  173. if (map)
  174. set_bit(sch->type, map);
  175. }
  176. pr_debug("Basic checks passed\n");
  177. return count == 0;
  178. }
  179. static int sctp_new_state(enum ip_conntrack_dir dir,
  180. enum sctp_conntrack cur_state,
  181. int chunk_type)
  182. {
  183. int i;
  184. pr_debug("Chunk type: %d\n", chunk_type);
  185. switch (chunk_type) {
  186. case SCTP_CID_INIT:
  187. pr_debug("SCTP_CID_INIT\n");
  188. i = 0;
  189. break;
  190. case SCTP_CID_INIT_ACK:
  191. pr_debug("SCTP_CID_INIT_ACK\n");
  192. i = 1;
  193. break;
  194. case SCTP_CID_ABORT:
  195. pr_debug("SCTP_CID_ABORT\n");
  196. i = 2;
  197. break;
  198. case SCTP_CID_SHUTDOWN:
  199. pr_debug("SCTP_CID_SHUTDOWN\n");
  200. i = 3;
  201. break;
  202. case SCTP_CID_SHUTDOWN_ACK:
  203. pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
  204. i = 4;
  205. break;
  206. case SCTP_CID_ERROR:
  207. pr_debug("SCTP_CID_ERROR\n");
  208. i = 5;
  209. break;
  210. case SCTP_CID_COOKIE_ECHO:
  211. pr_debug("SCTP_CID_COOKIE_ECHO\n");
  212. i = 6;
  213. break;
  214. case SCTP_CID_COOKIE_ACK:
  215. pr_debug("SCTP_CID_COOKIE_ACK\n");
  216. i = 7;
  217. break;
  218. case SCTP_CID_SHUTDOWN_COMPLETE:
  219. pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
  220. i = 8;
  221. break;
  222. case SCTP_CID_HEARTBEAT:
  223. pr_debug("SCTP_CID_HEARTBEAT");
  224. i = 9;
  225. break;
  226. case SCTP_CID_HEARTBEAT_ACK:
  227. pr_debug("SCTP_CID_HEARTBEAT_ACK");
  228. i = 10;
  229. break;
  230. default:
  231. /* Other chunks like DATA or SACK do not change the state */
  232. pr_debug("Unknown chunk type, Will stay in %s\n",
  233. sctp_conntrack_names[cur_state]);
  234. return cur_state;
  235. }
  236. pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
  237. dir, sctp_conntrack_names[cur_state], chunk_type,
  238. sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
  239. return sctp_conntracks[dir][i][cur_state];
  240. }
  241. /* Don't need lock here: this conntrack not in circulation yet */
  242. static noinline bool
  243. sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
  244. const struct sctphdr *sh, unsigned int dataoff)
  245. {
  246. enum sctp_conntrack new_state;
  247. const struct sctp_chunkhdr *sch;
  248. struct sctp_chunkhdr _sch;
  249. u32 offset, count;
  250. memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
  251. new_state = SCTP_CONNTRACK_MAX;
  252. for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) {
  253. new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
  254. SCTP_CONNTRACK_NONE, sch->type);
  255. /* Invalid: delete conntrack */
  256. if (new_state == SCTP_CONNTRACK_NONE ||
  257. new_state == SCTP_CONNTRACK_MAX) {
  258. pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
  259. return false;
  260. }
  261. /* Copy the vtag into the state info */
  262. if (sch->type == SCTP_CID_INIT) {
  263. struct sctp_inithdr _inithdr, *ih;
  264. /* Sec 8.5.1 (A) */
  265. if (sh->vtag)
  266. return false;
  267. ih = skb_header_pointer(skb, offset + sizeof(_sch),
  268. sizeof(_inithdr), &_inithdr);
  269. if (!ih)
  270. return false;
  271. pr_debug("Setting vtag %x for new conn\n",
  272. ih->init_tag);
  273. ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = ih->init_tag;
  274. } else if (sch->type == SCTP_CID_HEARTBEAT) {
  275. pr_debug("Setting vtag %x for secondary conntrack\n",
  276. sh->vtag);
  277. ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
  278. } else {
  279. /* If it is a shutdown ack OOTB packet, we expect a return
  280. shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
  281. pr_debug("Setting vtag %x for new conn OOTB\n",
  282. sh->vtag);
  283. ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
  284. }
  285. ct->proto.sctp.state = SCTP_CONNTRACK_NONE;
  286. }
  287. return true;
  288. }
  289. static bool sctp_error(struct sk_buff *skb,
  290. unsigned int dataoff,
  291. const struct nf_hook_state *state)
  292. {
  293. const struct sctphdr *sh;
  294. const char *logmsg;
  295. if (skb->len < dataoff + sizeof(struct sctphdr)) {
  296. logmsg = "nf_ct_sctp: short packet ";
  297. goto out_invalid;
  298. }
  299. if (state->hook == NF_INET_PRE_ROUTING &&
  300. state->net->ct.sysctl_checksum &&
  301. skb->ip_summed == CHECKSUM_NONE) {
  302. if (skb_ensure_writable(skb, dataoff + sizeof(*sh))) {
  303. logmsg = "nf_ct_sctp: failed to read header ";
  304. goto out_invalid;
  305. }
  306. sh = (const struct sctphdr *)(skb->data + dataoff);
  307. if (sh->checksum != sctp_compute_cksum(skb, dataoff)) {
  308. logmsg = "nf_ct_sctp: bad CRC ";
  309. goto out_invalid;
  310. }
  311. skb->ip_summed = CHECKSUM_UNNECESSARY;
  312. }
  313. return false;
  314. out_invalid:
  315. nf_l4proto_log_invalid(skb, state->net, state->pf, IPPROTO_SCTP, "%s", logmsg);
  316. return true;
  317. }
  318. /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
  319. int nf_conntrack_sctp_packet(struct nf_conn *ct,
  320. struct sk_buff *skb,
  321. unsigned int dataoff,
  322. enum ip_conntrack_info ctinfo,
  323. const struct nf_hook_state *state)
  324. {
  325. enum sctp_conntrack new_state, old_state;
  326. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  327. const struct sctphdr *sh;
  328. struct sctphdr _sctph;
  329. const struct sctp_chunkhdr *sch;
  330. struct sctp_chunkhdr _sch;
  331. u_int32_t offset, count;
  332. unsigned int *timeouts;
  333. unsigned long map[256 / sizeof(unsigned long)] = { 0 };
  334. bool ignore = false;
  335. if (sctp_error(skb, dataoff, state))
  336. return -NF_ACCEPT;
  337. sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
  338. if (sh == NULL)
  339. goto out;
  340. if (do_basic_checks(ct, skb, dataoff, map) != 0)
  341. goto out;
  342. if (!nf_ct_is_confirmed(ct)) {
  343. /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
  344. if (test_bit(SCTP_CID_ABORT, map) ||
  345. test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
  346. test_bit(SCTP_CID_COOKIE_ACK, map))
  347. return -NF_ACCEPT;
  348. if (!sctp_new(ct, skb, sh, dataoff))
  349. return -NF_ACCEPT;
  350. }
  351. /* Check the verification tag (Sec 8.5) */
  352. if (!test_bit(SCTP_CID_INIT, map) &&
  353. !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
  354. !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
  355. !test_bit(SCTP_CID_ABORT, map) &&
  356. !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
  357. !test_bit(SCTP_CID_HEARTBEAT, map) &&
  358. !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
  359. sh->vtag != ct->proto.sctp.vtag[dir]) {
  360. pr_debug("Verification tag check failed\n");
  361. goto out;
  362. }
  363. old_state = new_state = SCTP_CONNTRACK_NONE;
  364. spin_lock_bh(&ct->lock);
  365. for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
  366. /* Special cases of Verification tag check (Sec 8.5.1) */
  367. if (sch->type == SCTP_CID_INIT) {
  368. /* Sec 8.5.1 (A) */
  369. if (sh->vtag != 0)
  370. goto out_unlock;
  371. } else if (sch->type == SCTP_CID_ABORT) {
  372. /* Sec 8.5.1 (B) */
  373. if (sh->vtag != ct->proto.sctp.vtag[dir] &&
  374. sh->vtag != ct->proto.sctp.vtag[!dir])
  375. goto out_unlock;
  376. } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
  377. /* Sec 8.5.1 (C) */
  378. if (sh->vtag != ct->proto.sctp.vtag[dir] &&
  379. sh->vtag != ct->proto.sctp.vtag[!dir] &&
  380. sch->flags & SCTP_CHUNK_FLAG_T)
  381. goto out_unlock;
  382. } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
  383. /* Sec 8.5.1 (D) */
  384. if (sh->vtag != ct->proto.sctp.vtag[dir])
  385. goto out_unlock;
  386. } else if (sch->type == SCTP_CID_HEARTBEAT) {
  387. if (ct->proto.sctp.vtag[dir] == 0) {
  388. pr_debug("Setting %d vtag %x for dir %d\n", sch->type, sh->vtag, dir);
  389. ct->proto.sctp.vtag[dir] = sh->vtag;
  390. } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
  391. if (test_bit(SCTP_CID_DATA, map) || ignore)
  392. goto out_unlock;
  393. ct->proto.sctp.flags |= SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
  394. ct->proto.sctp.last_dir = dir;
  395. ignore = true;
  396. continue;
  397. } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
  398. ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
  399. }
  400. } else if (sch->type == SCTP_CID_HEARTBEAT_ACK) {
  401. if (ct->proto.sctp.vtag[dir] == 0) {
  402. pr_debug("Setting vtag %x for dir %d\n",
  403. sh->vtag, dir);
  404. ct->proto.sctp.vtag[dir] = sh->vtag;
  405. } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
  406. if (test_bit(SCTP_CID_DATA, map) || ignore)
  407. goto out_unlock;
  408. if ((ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) == 0 ||
  409. ct->proto.sctp.last_dir == dir)
  410. goto out_unlock;
  411. ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
  412. ct->proto.sctp.vtag[dir] = sh->vtag;
  413. ct->proto.sctp.vtag[!dir] = 0;
  414. } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
  415. ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
  416. }
  417. }
  418. old_state = ct->proto.sctp.state;
  419. new_state = sctp_new_state(dir, old_state, sch->type);
  420. /* Invalid */
  421. if (new_state == SCTP_CONNTRACK_MAX) {
  422. pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
  423. "conntrack=%u\n",
  424. dir, sch->type, old_state);
  425. goto out_unlock;
  426. }
  427. /* If it is an INIT or an INIT ACK note down the vtag */
  428. if (sch->type == SCTP_CID_INIT ||
  429. sch->type == SCTP_CID_INIT_ACK) {
  430. struct sctp_inithdr _inithdr, *ih;
  431. ih = skb_header_pointer(skb, offset + sizeof(_sch),
  432. sizeof(_inithdr), &_inithdr);
  433. if (ih == NULL)
  434. goto out_unlock;
  435. pr_debug("Setting vtag %x for dir %d\n",
  436. ih->init_tag, !dir);
  437. ct->proto.sctp.vtag[!dir] = ih->init_tag;
  438. /* don't renew timeout on init retransmit so
  439. * port reuse by client or NAT middlebox cannot
  440. * keep entry alive indefinitely (incl. nat info).
  441. */
  442. if (new_state == SCTP_CONNTRACK_CLOSED &&
  443. old_state == SCTP_CONNTRACK_CLOSED &&
  444. nf_ct_is_confirmed(ct))
  445. ignore = true;
  446. }
  447. ct->proto.sctp.state = new_state;
  448. if (old_state != new_state)
  449. nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
  450. }
  451. spin_unlock_bh(&ct->lock);
  452. /* allow but do not refresh timeout */
  453. if (ignore)
  454. return NF_ACCEPT;
  455. timeouts = nf_ct_timeout_lookup(ct);
  456. if (!timeouts)
  457. timeouts = nf_sctp_pernet(nf_ct_net(ct))->timeouts;
  458. nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
  459. if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
  460. dir == IP_CT_DIR_REPLY &&
  461. new_state == SCTP_CONNTRACK_ESTABLISHED) {
  462. pr_debug("Setting assured bit\n");
  463. set_bit(IPS_ASSURED_BIT, &ct->status);
  464. nf_conntrack_event_cache(IPCT_ASSURED, ct);
  465. }
  466. return NF_ACCEPT;
  467. out_unlock:
  468. spin_unlock_bh(&ct->lock);
  469. out:
  470. return -NF_ACCEPT;
  471. }
  472. static bool sctp_can_early_drop(const struct nf_conn *ct)
  473. {
  474. switch (ct->proto.sctp.state) {
  475. case SCTP_CONNTRACK_SHUTDOWN_SENT:
  476. case SCTP_CONNTRACK_SHUTDOWN_RECD:
  477. case SCTP_CONNTRACK_SHUTDOWN_ACK_SENT:
  478. return true;
  479. default:
  480. break;
  481. }
  482. return false;
  483. }
  484. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  485. #include <linux/netfilter/nfnetlink.h>
  486. #include <linux/netfilter/nfnetlink_conntrack.h>
  487. static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
  488. struct nf_conn *ct)
  489. {
  490. struct nlattr *nest_parms;
  491. spin_lock_bh(&ct->lock);
  492. nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP);
  493. if (!nest_parms)
  494. goto nla_put_failure;
  495. if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
  496. nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
  497. ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
  498. nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
  499. ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
  500. goto nla_put_failure;
  501. spin_unlock_bh(&ct->lock);
  502. nla_nest_end(skb, nest_parms);
  503. return 0;
  504. nla_put_failure:
  505. spin_unlock_bh(&ct->lock);
  506. return -1;
  507. }
  508. static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
  509. [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
  510. [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
  511. [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
  512. };
  513. #define SCTP_NLATTR_SIZE ( \
  514. NLA_ALIGN(NLA_HDRLEN + 1) + \
  515. NLA_ALIGN(NLA_HDRLEN + 4) + \
  516. NLA_ALIGN(NLA_HDRLEN + 4))
  517. static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
  518. {
  519. struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
  520. struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
  521. int err;
  522. /* updates may not contain the internal protocol info, skip parsing */
  523. if (!attr)
  524. return 0;
  525. err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_SCTP_MAX, attr,
  526. sctp_nla_policy, NULL);
  527. if (err < 0)
  528. return err;
  529. if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
  530. !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
  531. !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
  532. return -EINVAL;
  533. spin_lock_bh(&ct->lock);
  534. ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
  535. ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
  536. nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
  537. ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
  538. nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
  539. spin_unlock_bh(&ct->lock);
  540. return 0;
  541. }
  542. #endif
  543. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  544. #include <linux/netfilter/nfnetlink.h>
  545. #include <linux/netfilter/nfnetlink_cttimeout.h>
  546. static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
  547. struct net *net, void *data)
  548. {
  549. unsigned int *timeouts = data;
  550. struct nf_sctp_net *sn = nf_sctp_pernet(net);
  551. int i;
  552. if (!timeouts)
  553. timeouts = sn->timeouts;
  554. /* set default SCTP timeouts. */
  555. for (i=0; i<SCTP_CONNTRACK_MAX; i++)
  556. timeouts[i] = sn->timeouts[i];
  557. /* there's a 1:1 mapping between attributes and protocol states. */
  558. for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
  559. if (tb[i]) {
  560. timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
  561. }
  562. }
  563. timeouts[CTA_TIMEOUT_SCTP_UNSPEC] = timeouts[CTA_TIMEOUT_SCTP_CLOSED];
  564. return 0;
  565. }
  566. static int
  567. sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  568. {
  569. const unsigned int *timeouts = data;
  570. int i;
  571. for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
  572. if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
  573. goto nla_put_failure;
  574. }
  575. return 0;
  576. nla_put_failure:
  577. return -ENOSPC;
  578. }
  579. static const struct nla_policy
  580. sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
  581. [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
  582. [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
  583. [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
  584. [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
  585. [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
  586. [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
  587. [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
  588. [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT] = { .type = NLA_U32 },
  589. [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED] = { .type = NLA_U32 },
  590. };
  591. #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
  592. void nf_conntrack_sctp_init_net(struct net *net)
  593. {
  594. struct nf_sctp_net *sn = nf_sctp_pernet(net);
  595. int i;
  596. for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
  597. sn->timeouts[i] = sctp_timeouts[i];
  598. /* timeouts[0] is unused, init it so ->timeouts[0] contains
  599. * 'new' timeout, like udp or icmp.
  600. */
  601. sn->timeouts[0] = sctp_timeouts[SCTP_CONNTRACK_CLOSED];
  602. }
  603. const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp = {
  604. .l4proto = IPPROTO_SCTP,
  605. #ifdef CONFIG_NF_CONNTRACK_PROCFS
  606. .print_conntrack = sctp_print_conntrack,
  607. #endif
  608. .can_early_drop = sctp_can_early_drop,
  609. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  610. .nlattr_size = SCTP_NLATTR_SIZE,
  611. .to_nlattr = sctp_to_nlattr,
  612. .from_nlattr = nlattr_to_sctp,
  613. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  614. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  615. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  616. .nla_policy = nf_ct_port_nla_policy,
  617. #endif
  618. #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
  619. .ctnl_timeout = {
  620. .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
  621. .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
  622. .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
  623. .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
  624. .nla_policy = sctp_timeout_nla_policy,
  625. },
  626. #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
  627. };