nf_conntrack_proto_sctp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * Connection tracking protocol helper module for SCTP.
  3. *
  4. * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com>
  5. * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net>
  6. *
  7. * SCTP is defined in RFC 2960. References to various sections in this code
  8. * are to this RFC.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/timer.h>
  16. #include <linux/netfilter.h>
  17. #include <linux/module.h>
  18. #include <linux/in.h>
  19. #include <linux/ip.h>
  20. #include <linux/sctp.h>
  21. #include <linux/string.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <net/netfilter/nf_conntrack.h>
  26. #include <net/netfilter/nf_conntrack_l4proto.h>
  27. #include <net/netfilter/nf_conntrack_ecache.h>
  28. /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
  29. closely. They're more complex. --RR
  30. And so for me for SCTP :D -Kiran */
  31. static const char *const sctp_conntrack_names[] = {
  32. "NONE",
  33. "CLOSED",
  34. "COOKIE_WAIT",
  35. "COOKIE_ECHOED",
  36. "ESTABLISHED",
  37. "SHUTDOWN_SENT",
  38. "SHUTDOWN_RECD",
  39. "SHUTDOWN_ACK_SENT",
  40. "HEARTBEAT_SENT",
  41. "HEARTBEAT_ACKED",
  42. };
  43. #define SECS * HZ
  44. #define MINS * 60 SECS
  45. #define HOURS * 60 MINS
  46. #define DAYS * 24 HOURS
  47. static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] __read_mostly = {
  48. [SCTP_CONNTRACK_CLOSED] = 10 SECS,
  49. [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
  50. [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
  51. [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS,
  52. [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000,
  53. [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000,
  54. [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
  55. [SCTP_CONNTRACK_HEARTBEAT_SENT] = 30 SECS,
  56. [SCTP_CONNTRACK_HEARTBEAT_ACKED] = 210 SECS,
  57. };
  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 */ {sCW, sCW, 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, sCL, 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. static int sctp_net_id __read_mostly;
  134. struct sctp_net {
  135. struct nf_proto_net pn;
  136. unsigned int timeouts[SCTP_CONNTRACK_MAX];
  137. };
  138. static inline struct sctp_net *sctp_pernet(struct net *net)
  139. {
  140. return net_generic(net, sctp_net_id);
  141. }
  142. static bool sctp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
  143. struct net *net, struct nf_conntrack_tuple *tuple)
  144. {
  145. const struct sctphdr *hp;
  146. struct sctphdr _hdr;
  147. /* Actually only need first 8 bytes. */
  148. hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
  149. if (hp == NULL)
  150. return false;
  151. tuple->src.u.sctp.port = hp->source;
  152. tuple->dst.u.sctp.port = hp->dest;
  153. return true;
  154. }
  155. static bool sctp_invert_tuple(struct nf_conntrack_tuple *tuple,
  156. const struct nf_conntrack_tuple *orig)
  157. {
  158. tuple->src.u.sctp.port = orig->dst.u.sctp.port;
  159. tuple->dst.u.sctp.port = orig->src.u.sctp.port;
  160. return true;
  161. }
  162. /* Print out the per-protocol part of the tuple. */
  163. static void sctp_print_tuple(struct seq_file *s,
  164. const struct nf_conntrack_tuple *tuple)
  165. {
  166. seq_printf(s, "sport=%hu dport=%hu ",
  167. ntohs(tuple->src.u.sctp.port),
  168. ntohs(tuple->dst.u.sctp.port));
  169. }
  170. /* Print out the private part of the conntrack. */
  171. static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
  172. {
  173. enum sctp_conntrack state;
  174. spin_lock_bh(&ct->lock);
  175. state = ct->proto.sctp.state;
  176. spin_unlock_bh(&ct->lock);
  177. seq_printf(s, "%s ", sctp_conntrack_names[state]);
  178. }
  179. #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
  180. for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
  181. (offset) < (skb)->len && \
  182. ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
  183. (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
  184. /* Some validity checks to make sure the chunks are fine */
  185. static int do_basic_checks(struct nf_conn *ct,
  186. const struct sk_buff *skb,
  187. unsigned int dataoff,
  188. unsigned long *map)
  189. {
  190. u_int32_t offset, count;
  191. sctp_chunkhdr_t _sch, *sch;
  192. int flag;
  193. flag = 0;
  194. for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
  195. pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
  196. if (sch->type == SCTP_CID_INIT ||
  197. sch->type == SCTP_CID_INIT_ACK ||
  198. sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
  199. flag = 1;
  200. /*
  201. * Cookie Ack/Echo chunks not the first OR
  202. * Init / Init Ack / Shutdown compl chunks not the only chunks
  203. * OR zero-length.
  204. */
  205. if (((sch->type == SCTP_CID_COOKIE_ACK ||
  206. sch->type == SCTP_CID_COOKIE_ECHO ||
  207. flag) &&
  208. count != 0) || !sch->length) {
  209. pr_debug("Basic checks failed\n");
  210. return 1;
  211. }
  212. if (map)
  213. set_bit(sch->type, map);
  214. }
  215. pr_debug("Basic checks passed\n");
  216. return count == 0;
  217. }
  218. static int sctp_new_state(enum ip_conntrack_dir dir,
  219. enum sctp_conntrack cur_state,
  220. int chunk_type)
  221. {
  222. int i;
  223. pr_debug("Chunk type: %d\n", chunk_type);
  224. switch (chunk_type) {
  225. case SCTP_CID_INIT:
  226. pr_debug("SCTP_CID_INIT\n");
  227. i = 0;
  228. break;
  229. case SCTP_CID_INIT_ACK:
  230. pr_debug("SCTP_CID_INIT_ACK\n");
  231. i = 1;
  232. break;
  233. case SCTP_CID_ABORT:
  234. pr_debug("SCTP_CID_ABORT\n");
  235. i = 2;
  236. break;
  237. case SCTP_CID_SHUTDOWN:
  238. pr_debug("SCTP_CID_SHUTDOWN\n");
  239. i = 3;
  240. break;
  241. case SCTP_CID_SHUTDOWN_ACK:
  242. pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
  243. i = 4;
  244. break;
  245. case SCTP_CID_ERROR:
  246. pr_debug("SCTP_CID_ERROR\n");
  247. i = 5;
  248. break;
  249. case SCTP_CID_COOKIE_ECHO:
  250. pr_debug("SCTP_CID_COOKIE_ECHO\n");
  251. i = 6;
  252. break;
  253. case SCTP_CID_COOKIE_ACK:
  254. pr_debug("SCTP_CID_COOKIE_ACK\n");
  255. i = 7;
  256. break;
  257. case SCTP_CID_SHUTDOWN_COMPLETE:
  258. pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
  259. i = 8;
  260. break;
  261. case SCTP_CID_HEARTBEAT:
  262. pr_debug("SCTP_CID_HEARTBEAT");
  263. i = 9;
  264. break;
  265. case SCTP_CID_HEARTBEAT_ACK:
  266. pr_debug("SCTP_CID_HEARTBEAT_ACK");
  267. i = 10;
  268. break;
  269. default:
  270. /* Other chunks like DATA or SACK do not change the state */
  271. pr_debug("Unknown chunk type, Will stay in %s\n",
  272. sctp_conntrack_names[cur_state]);
  273. return cur_state;
  274. }
  275. pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
  276. dir, sctp_conntrack_names[cur_state], chunk_type,
  277. sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
  278. return sctp_conntracks[dir][i][cur_state];
  279. }
  280. static unsigned int *sctp_get_timeouts(struct net *net)
  281. {
  282. return sctp_pernet(net)->timeouts;
  283. }
  284. /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
  285. static int sctp_packet(struct nf_conn *ct,
  286. const struct sk_buff *skb,
  287. unsigned int dataoff,
  288. enum ip_conntrack_info ctinfo,
  289. u_int8_t pf,
  290. unsigned int hooknum,
  291. unsigned int *timeouts)
  292. {
  293. enum sctp_conntrack new_state, old_state;
  294. enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
  295. const struct sctphdr *sh;
  296. struct sctphdr _sctph;
  297. const struct sctp_chunkhdr *sch;
  298. struct sctp_chunkhdr _sch;
  299. u_int32_t offset, count;
  300. unsigned long map[256 / sizeof(unsigned long)] = { 0 };
  301. sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
  302. if (sh == NULL)
  303. goto out;
  304. if (do_basic_checks(ct, skb, dataoff, map) != 0)
  305. goto out;
  306. /* Check the verification tag (Sec 8.5) */
  307. if (!test_bit(SCTP_CID_INIT, map) &&
  308. !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
  309. !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
  310. !test_bit(SCTP_CID_ABORT, map) &&
  311. !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
  312. !test_bit(SCTP_CID_HEARTBEAT, map) &&
  313. !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
  314. sh->vtag != ct->proto.sctp.vtag[dir]) {
  315. pr_debug("Verification tag check failed\n");
  316. goto out;
  317. }
  318. old_state = new_state = SCTP_CONNTRACK_NONE;
  319. spin_lock_bh(&ct->lock);
  320. for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
  321. /* Special cases of Verification tag check (Sec 8.5.1) */
  322. if (sch->type == SCTP_CID_INIT) {
  323. /* Sec 8.5.1 (A) */
  324. if (sh->vtag != 0)
  325. goto out_unlock;
  326. } else if (sch->type == SCTP_CID_ABORT) {
  327. /* Sec 8.5.1 (B) */
  328. if (sh->vtag != ct->proto.sctp.vtag[dir] &&
  329. sh->vtag != ct->proto.sctp.vtag[!dir])
  330. goto out_unlock;
  331. } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
  332. /* Sec 8.5.1 (C) */
  333. if (sh->vtag != ct->proto.sctp.vtag[dir] &&
  334. sh->vtag != ct->proto.sctp.vtag[!dir] &&
  335. sch->flags & SCTP_CHUNK_FLAG_T)
  336. goto out_unlock;
  337. } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
  338. /* Sec 8.5.1 (D) */
  339. if (sh->vtag != ct->proto.sctp.vtag[dir])
  340. goto out_unlock;
  341. } else if (sch->type == SCTP_CID_HEARTBEAT ||
  342. sch->type == SCTP_CID_HEARTBEAT_ACK) {
  343. if (ct->proto.sctp.vtag[dir] == 0) {
  344. pr_debug("Setting vtag %x for dir %d\n",
  345. sh->vtag, dir);
  346. ct->proto.sctp.vtag[dir] = sh->vtag;
  347. } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
  348. pr_debug("Verification tag check failed\n");
  349. goto out_unlock;
  350. }
  351. }
  352. old_state = ct->proto.sctp.state;
  353. new_state = sctp_new_state(dir, old_state, sch->type);
  354. /* Invalid */
  355. if (new_state == SCTP_CONNTRACK_MAX) {
  356. pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
  357. "conntrack=%u\n",
  358. dir, sch->type, old_state);
  359. goto out_unlock;
  360. }
  361. /* If it is an INIT or an INIT ACK note down the vtag */
  362. if (sch->type == SCTP_CID_INIT ||
  363. sch->type == SCTP_CID_INIT_ACK) {
  364. sctp_inithdr_t _inithdr, *ih;
  365. ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
  366. sizeof(_inithdr), &_inithdr);
  367. if (ih == NULL)
  368. goto out_unlock;
  369. pr_debug("Setting vtag %x for dir %d\n",
  370. ih->init_tag, !dir);
  371. ct->proto.sctp.vtag[!dir] = ih->init_tag;
  372. }
  373. ct->proto.sctp.state = new_state;
  374. if (old_state != new_state)
  375. nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
  376. }
  377. spin_unlock_bh(&ct->lock);
  378. nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
  379. if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
  380. dir == IP_CT_DIR_REPLY &&
  381. new_state == SCTP_CONNTRACK_ESTABLISHED) {
  382. pr_debug("Setting assured bit\n");
  383. set_bit(IPS_ASSURED_BIT, &ct->status);
  384. nf_conntrack_event_cache(IPCT_ASSURED, ct);
  385. }
  386. return NF_ACCEPT;
  387. out_unlock:
  388. spin_unlock_bh(&ct->lock);
  389. out:
  390. return -NF_ACCEPT;
  391. }
  392. /* Called when a new connection for this protocol found. */
  393. static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
  394. unsigned int dataoff, unsigned int *timeouts)
  395. {
  396. enum sctp_conntrack new_state;
  397. const struct sctphdr *sh;
  398. struct sctphdr _sctph;
  399. const struct sctp_chunkhdr *sch;
  400. struct sctp_chunkhdr _sch;
  401. u_int32_t offset, count;
  402. unsigned long map[256 / sizeof(unsigned long)] = { 0 };
  403. sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
  404. if (sh == NULL)
  405. return false;
  406. if (do_basic_checks(ct, skb, dataoff, map) != 0)
  407. return false;
  408. /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
  409. if (test_bit(SCTP_CID_ABORT, map) ||
  410. test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
  411. test_bit(SCTP_CID_COOKIE_ACK, map))
  412. return false;
  413. memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
  414. new_state = SCTP_CONNTRACK_MAX;
  415. for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
  416. /* Don't need lock here: this conntrack not in circulation yet */
  417. new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
  418. SCTP_CONNTRACK_NONE, sch->type);
  419. /* Invalid: delete conntrack */
  420. if (new_state == SCTP_CONNTRACK_NONE ||
  421. new_state == SCTP_CONNTRACK_MAX) {
  422. pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
  423. return false;
  424. }
  425. /* Copy the vtag into the state info */
  426. if (sch->type == SCTP_CID_INIT) {
  427. if (sh->vtag == 0) {
  428. sctp_inithdr_t _inithdr, *ih;
  429. ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
  430. sizeof(_inithdr), &_inithdr);
  431. if (ih == NULL)
  432. return false;
  433. pr_debug("Setting vtag %x for new conn\n",
  434. ih->init_tag);
  435. ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
  436. ih->init_tag;
  437. } else {
  438. /* Sec 8.5.1 (A) */
  439. return false;
  440. }
  441. } else if (sch->type == SCTP_CID_HEARTBEAT) {
  442. pr_debug("Setting vtag %x for secondary conntrack\n",
  443. sh->vtag);
  444. ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
  445. }
  446. /* If it is a shutdown ack OOTB packet, we expect a return
  447. shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
  448. else {
  449. pr_debug("Setting vtag %x for new conn OOTB\n",
  450. sh->vtag);
  451. ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
  452. }
  453. ct->proto.sctp.state = new_state;
  454. }
  455. return true;
  456. }
  457. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  458. #include <linux/netfilter/nfnetlink.h>
  459. #include <linux/netfilter/nfnetlink_conntrack.h>
  460. static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
  461. struct nf_conn *ct)
  462. {
  463. struct nlattr *nest_parms;
  464. spin_lock_bh(&ct->lock);
  465. nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
  466. if (!nest_parms)
  467. goto nla_put_failure;
  468. if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
  469. nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
  470. ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
  471. nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
  472. ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
  473. goto nla_put_failure;
  474. spin_unlock_bh(&ct->lock);
  475. nla_nest_end(skb, nest_parms);
  476. return 0;
  477. nla_put_failure:
  478. spin_unlock_bh(&ct->lock);
  479. return -1;
  480. }
  481. static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
  482. [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
  483. [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
  484. [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
  485. };
  486. static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
  487. {
  488. struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
  489. struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
  490. int err;
  491. /* updates may not contain the internal protocol info, skip parsing */
  492. if (!attr)
  493. return 0;
  494. err = nla_parse_nested(tb,
  495. CTA_PROTOINFO_SCTP_MAX,
  496. attr,
  497. sctp_nla_policy);
  498. if (err < 0)
  499. return err;
  500. if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
  501. !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
  502. !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
  503. return -EINVAL;
  504. spin_lock_bh(&ct->lock);
  505. ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
  506. ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
  507. nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
  508. ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
  509. nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
  510. spin_unlock_bh(&ct->lock);
  511. return 0;
  512. }
  513. static int sctp_nlattr_size(void)
  514. {
  515. return nla_total_size(0) /* CTA_PROTOINFO_SCTP */
  516. + nla_policy_len(sctp_nla_policy, CTA_PROTOINFO_SCTP_MAX + 1);
  517. }
  518. #endif
  519. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  520. #include <linux/netfilter/nfnetlink.h>
  521. #include <linux/netfilter/nfnetlink_cttimeout.h>
  522. static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
  523. struct net *net, void *data)
  524. {
  525. unsigned int *timeouts = data;
  526. struct sctp_net *sn = sctp_pernet(net);
  527. int i;
  528. /* set default SCTP timeouts. */
  529. for (i=0; i<SCTP_CONNTRACK_MAX; i++)
  530. timeouts[i] = sn->timeouts[i];
  531. /* there's a 1:1 mapping between attributes and protocol states. */
  532. for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
  533. if (tb[i]) {
  534. timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
  535. }
  536. }
  537. return 0;
  538. }
  539. static int
  540. sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  541. {
  542. const unsigned int *timeouts = data;
  543. int i;
  544. for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
  545. if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
  546. goto nla_put_failure;
  547. }
  548. return 0;
  549. nla_put_failure:
  550. return -ENOSPC;
  551. }
  552. static const struct nla_policy
  553. sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
  554. [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
  555. [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
  556. [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
  557. [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
  558. [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
  559. [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
  560. [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
  561. [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT] = { .type = NLA_U32 },
  562. [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED] = { .type = NLA_U32 },
  563. };
  564. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  565. #ifdef CONFIG_SYSCTL
  566. static struct ctl_table sctp_sysctl_table[] = {
  567. {
  568. .procname = "nf_conntrack_sctp_timeout_closed",
  569. .maxlen = sizeof(unsigned int),
  570. .mode = 0644,
  571. .proc_handler = proc_dointvec_jiffies,
  572. },
  573. {
  574. .procname = "nf_conntrack_sctp_timeout_cookie_wait",
  575. .maxlen = sizeof(unsigned int),
  576. .mode = 0644,
  577. .proc_handler = proc_dointvec_jiffies,
  578. },
  579. {
  580. .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
  581. .maxlen = sizeof(unsigned int),
  582. .mode = 0644,
  583. .proc_handler = proc_dointvec_jiffies,
  584. },
  585. {
  586. .procname = "nf_conntrack_sctp_timeout_established",
  587. .maxlen = sizeof(unsigned int),
  588. .mode = 0644,
  589. .proc_handler = proc_dointvec_jiffies,
  590. },
  591. {
  592. .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
  593. .maxlen = sizeof(unsigned int),
  594. .mode = 0644,
  595. .proc_handler = proc_dointvec_jiffies,
  596. },
  597. {
  598. .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
  599. .maxlen = sizeof(unsigned int),
  600. .mode = 0644,
  601. .proc_handler = proc_dointvec_jiffies,
  602. },
  603. {
  604. .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
  605. .maxlen = sizeof(unsigned int),
  606. .mode = 0644,
  607. .proc_handler = proc_dointvec_jiffies,
  608. },
  609. {
  610. .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
  611. .maxlen = sizeof(unsigned int),
  612. .mode = 0644,
  613. .proc_handler = proc_dointvec_jiffies,
  614. },
  615. {
  616. .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
  617. .maxlen = sizeof(unsigned int),
  618. .mode = 0644,
  619. .proc_handler = proc_dointvec_jiffies,
  620. },
  621. { }
  622. };
  623. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  624. static struct ctl_table sctp_compat_sysctl_table[] = {
  625. {
  626. .procname = "ip_conntrack_sctp_timeout_closed",
  627. .maxlen = sizeof(unsigned int),
  628. .mode = 0644,
  629. .proc_handler = proc_dointvec_jiffies,
  630. },
  631. {
  632. .procname = "ip_conntrack_sctp_timeout_cookie_wait",
  633. .maxlen = sizeof(unsigned int),
  634. .mode = 0644,
  635. .proc_handler = proc_dointvec_jiffies,
  636. },
  637. {
  638. .procname = "ip_conntrack_sctp_timeout_cookie_echoed",
  639. .maxlen = sizeof(unsigned int),
  640. .mode = 0644,
  641. .proc_handler = proc_dointvec_jiffies,
  642. },
  643. {
  644. .procname = "ip_conntrack_sctp_timeout_established",
  645. .maxlen = sizeof(unsigned int),
  646. .mode = 0644,
  647. .proc_handler = proc_dointvec_jiffies,
  648. },
  649. {
  650. .procname = "ip_conntrack_sctp_timeout_shutdown_sent",
  651. .maxlen = sizeof(unsigned int),
  652. .mode = 0644,
  653. .proc_handler = proc_dointvec_jiffies,
  654. },
  655. {
  656. .procname = "ip_conntrack_sctp_timeout_shutdown_recd",
  657. .maxlen = sizeof(unsigned int),
  658. .mode = 0644,
  659. .proc_handler = proc_dointvec_jiffies,
  660. },
  661. {
  662. .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent",
  663. .maxlen = sizeof(unsigned int),
  664. .mode = 0644,
  665. .proc_handler = proc_dointvec_jiffies,
  666. },
  667. { }
  668. };
  669. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  670. #endif
  671. static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
  672. struct sctp_net *sn)
  673. {
  674. #ifdef CONFIG_SYSCTL
  675. if (pn->ctl_table)
  676. return 0;
  677. pn->ctl_table = kmemdup(sctp_sysctl_table,
  678. sizeof(sctp_sysctl_table),
  679. GFP_KERNEL);
  680. if (!pn->ctl_table)
  681. return -ENOMEM;
  682. pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
  683. pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
  684. pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
  685. pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
  686. pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
  687. pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
  688. pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
  689. pn->ctl_table[7].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_SENT];
  690. pn->ctl_table[8].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_ACKED];
  691. #endif
  692. return 0;
  693. }
  694. static int sctp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
  695. struct sctp_net *sn)
  696. {
  697. #ifdef CONFIG_SYSCTL
  698. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  699. pn->ctl_compat_table = kmemdup(sctp_compat_sysctl_table,
  700. sizeof(sctp_compat_sysctl_table),
  701. GFP_KERNEL);
  702. if (!pn->ctl_compat_table)
  703. return -ENOMEM;
  704. pn->ctl_compat_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
  705. pn->ctl_compat_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
  706. pn->ctl_compat_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
  707. pn->ctl_compat_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
  708. pn->ctl_compat_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
  709. pn->ctl_compat_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
  710. pn->ctl_compat_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
  711. #endif
  712. #endif
  713. return 0;
  714. }
  715. static int sctp_init_net(struct net *net, u_int16_t proto)
  716. {
  717. int ret;
  718. struct sctp_net *sn = sctp_pernet(net);
  719. struct nf_proto_net *pn = &sn->pn;
  720. if (!pn->users) {
  721. int i;
  722. for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
  723. sn->timeouts[i] = sctp_timeouts[i];
  724. }
  725. if (proto == AF_INET) {
  726. ret = sctp_kmemdup_compat_sysctl_table(pn, sn);
  727. if (ret < 0)
  728. return ret;
  729. ret = sctp_kmemdup_sysctl_table(pn, sn);
  730. if (ret < 0)
  731. nf_ct_kfree_compat_sysctl_table(pn);
  732. } else
  733. ret = sctp_kmemdup_sysctl_table(pn, sn);
  734. return ret;
  735. }
  736. static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = {
  737. .l3proto = PF_INET,
  738. .l4proto = IPPROTO_SCTP,
  739. .name = "sctp",
  740. .pkt_to_tuple = sctp_pkt_to_tuple,
  741. .invert_tuple = sctp_invert_tuple,
  742. .print_tuple = sctp_print_tuple,
  743. .print_conntrack = sctp_print_conntrack,
  744. .packet = sctp_packet,
  745. .get_timeouts = sctp_get_timeouts,
  746. .new = sctp_new,
  747. .me = THIS_MODULE,
  748. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  749. .to_nlattr = sctp_to_nlattr,
  750. .nlattr_size = sctp_nlattr_size,
  751. .from_nlattr = nlattr_to_sctp,
  752. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  753. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  754. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  755. .nla_policy = nf_ct_port_nla_policy,
  756. #endif
  757. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  758. .ctnl_timeout = {
  759. .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
  760. .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
  761. .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
  762. .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
  763. .nla_policy = sctp_timeout_nla_policy,
  764. },
  765. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  766. .net_id = &sctp_net_id,
  767. .init_net = sctp_init_net,
  768. };
  769. static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = {
  770. .l3proto = PF_INET6,
  771. .l4proto = IPPROTO_SCTP,
  772. .name = "sctp",
  773. .pkt_to_tuple = sctp_pkt_to_tuple,
  774. .invert_tuple = sctp_invert_tuple,
  775. .print_tuple = sctp_print_tuple,
  776. .print_conntrack = sctp_print_conntrack,
  777. .packet = sctp_packet,
  778. .get_timeouts = sctp_get_timeouts,
  779. .new = sctp_new,
  780. .me = THIS_MODULE,
  781. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  782. .to_nlattr = sctp_to_nlattr,
  783. .nlattr_size = sctp_nlattr_size,
  784. .from_nlattr = nlattr_to_sctp,
  785. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  786. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  787. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  788. .nla_policy = nf_ct_port_nla_policy,
  789. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  790. .ctnl_timeout = {
  791. .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
  792. .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
  793. .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
  794. .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
  795. .nla_policy = sctp_timeout_nla_policy,
  796. },
  797. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  798. #endif
  799. .net_id = &sctp_net_id,
  800. .init_net = sctp_init_net,
  801. };
  802. static int sctp_net_init(struct net *net)
  803. {
  804. int ret = 0;
  805. ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp4);
  806. if (ret < 0) {
  807. pr_err("nf_conntrack_sctp4: pernet registration failed.\n");
  808. goto out;
  809. }
  810. ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp6);
  811. if (ret < 0) {
  812. pr_err("nf_conntrack_sctp6: pernet registration failed.\n");
  813. goto cleanup_sctp4;
  814. }
  815. return 0;
  816. cleanup_sctp4:
  817. nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4);
  818. out:
  819. return ret;
  820. }
  821. static void sctp_net_exit(struct net *net)
  822. {
  823. nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp6);
  824. nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4);
  825. }
  826. static struct pernet_operations sctp_net_ops = {
  827. .init = sctp_net_init,
  828. .exit = sctp_net_exit,
  829. .id = &sctp_net_id,
  830. .size = sizeof(struct sctp_net),
  831. };
  832. static int __init nf_conntrack_proto_sctp_init(void)
  833. {
  834. int ret;
  835. ret = register_pernet_subsys(&sctp_net_ops);
  836. if (ret < 0)
  837. goto out_pernet;
  838. ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp4);
  839. if (ret < 0)
  840. goto out_sctp4;
  841. ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp6);
  842. if (ret < 0)
  843. goto out_sctp6;
  844. return 0;
  845. out_sctp6:
  846. nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
  847. out_sctp4:
  848. unregister_pernet_subsys(&sctp_net_ops);
  849. out_pernet:
  850. return ret;
  851. }
  852. static void __exit nf_conntrack_proto_sctp_fini(void)
  853. {
  854. nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp6);
  855. nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
  856. unregister_pernet_subsys(&sctp_net_ops);
  857. }
  858. module_init(nf_conntrack_proto_sctp_init);
  859. module_exit(nf_conntrack_proto_sctp_fini);
  860. MODULE_LICENSE("GPL");
  861. MODULE_AUTHOR("Kiran Kumar Immidi");
  862. MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP");
  863. MODULE_ALIAS("ip_conntrack_proto_sctp");