blob: 7baabc0453425217c9b17f30352206b6f7548c3d [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/*
2 * Connection tracking protocol helper module for SCTP.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08003 *
4 * SCTP is defined in RFC 2960. References to various sections in this code
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08005 * are to this RFC.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08006 *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010 */
11
12#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013#include <linux/timer.h>
14#include <linux/netfilter.h>
15#include <linux/module.h>
16#include <linux/in.h>
17#include <linux/ip.h>
18#include <linux/sctp.h>
19#include <linux/string.h>
20#include <linux/seq_file.h>
Yasuyuki Kozakai40a839f2006-06-27 03:00:35 -070021#include <linux/spinlock.h>
22#include <linux/interrupt.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080023
24#include <net/netfilter/nf_conntrack.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010025#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010026#include <net/netfilter/nf_conntrack_ecache.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080028/* Protects conntrack->proto.sctp */
29static DEFINE_RWLOCK(sctp_lock);
30
31/* FIXME: Examine ipfilter's timeouts and conntrack transitions more
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080032 closely. They're more complex. --RR
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033
34 And so for me for SCTP :D -Kiran */
35
36static const char *sctp_conntrack_names[] = {
37 "NONE",
38 "CLOSED",
39 "COOKIE_WAIT",
40 "COOKIE_ECHOED",
41 "ESTABLISHED",
42 "SHUTDOWN_SENT",
43 "SHUTDOWN_RECD",
44 "SHUTDOWN_ACK_SENT",
45};
46
47#define SECS * HZ
48#define MINS * 60 SECS
49#define HOURS * 60 MINS
50#define DAYS * 24 HOURS
51
Brian Haley94aec082006-09-18 00:05:22 -070052static unsigned int nf_ct_sctp_timeout_closed __read_mostly = 10 SECS;
53static unsigned int nf_ct_sctp_timeout_cookie_wait __read_mostly = 3 SECS;
54static unsigned int nf_ct_sctp_timeout_cookie_echoed __read_mostly = 3 SECS;
55static unsigned int nf_ct_sctp_timeout_established __read_mostly = 5 DAYS;
56static unsigned int nf_ct_sctp_timeout_shutdown_sent __read_mostly = 300 SECS / 1000;
57static unsigned int nf_ct_sctp_timeout_shutdown_recd __read_mostly = 300 SECS / 1000;
58static unsigned int nf_ct_sctp_timeout_shutdown_ack_sent __read_mostly = 3 SECS;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080059
Patrick McHardybabbdb12006-01-09 17:48:09 -080060static unsigned int * sctp_timeouts[]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061= { NULL, /* SCTP_CONNTRACK_NONE */
62 &nf_ct_sctp_timeout_closed, /* SCTP_CONNTRACK_CLOSED */
63 &nf_ct_sctp_timeout_cookie_wait, /* SCTP_CONNTRACK_COOKIE_WAIT */
64 &nf_ct_sctp_timeout_cookie_echoed, /* SCTP_CONNTRACK_COOKIE_ECHOED */
65 &nf_ct_sctp_timeout_established, /* SCTP_CONNTRACK_ESTABLISHED */
66 &nf_ct_sctp_timeout_shutdown_sent, /* SCTP_CONNTRACK_SHUTDOWN_SENT */
67 &nf_ct_sctp_timeout_shutdown_recd, /* SCTP_CONNTRACK_SHUTDOWN_RECD */
68 &nf_ct_sctp_timeout_shutdown_ack_sent /* SCTP_CONNTRACK_SHUTDOWN_ACK_SENT */
69 };
70
71#define sNO SCTP_CONNTRACK_NONE
72#define sCL SCTP_CONNTRACK_CLOSED
73#define sCW SCTP_CONNTRACK_COOKIE_WAIT
74#define sCE SCTP_CONNTRACK_COOKIE_ECHOED
75#define sES SCTP_CONNTRACK_ESTABLISHED
76#define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
77#define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
78#define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
79#define sIV SCTP_CONNTRACK_MAX
80
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080081/*
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080082 These are the descriptions of the states:
83
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080084NOTE: These state names are tantalizingly similar to the states of an
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080085SCTP endpoint. But the interpretation of the states is a little different,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080086considering that these are the states of the connection and not of an end
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080087point. Please note the subtleties. -Kiran
88
89NONE - Nothing so far.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080090COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
91 an INIT_ACK chunk in the reply direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
93ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
94SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
95SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
96SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080097 to that of the SHUTDOWN chunk.
98CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
99 the SHUTDOWN chunk. Connection is closed.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800100*/
101
102/* TODO
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800103 - I have assumed that the first INIT is in the original direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104 This messes things when an INIT comes in the reply direction in CLOSED
105 state.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800106 - Check the error type in the reply dir before transitioning from
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107cookie echoed to closed.
108 - Sec 5.2.4 of RFC 2960
109 - Multi Homing support.
110*/
111
112/* SCTP conntrack state transitions */
Patrick McHardya5e73c22008-01-14 23:45:11 -0800113static const u8 sctp_conntracks[2][9][SCTP_CONNTRACK_MAX] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800114 {
115/* ORIGINAL */
116/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */
117/* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA},
118/* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},
119/* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
120/* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA},
121/* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA},
122/* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant have Stale cookie*/
123/* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA},/* 5.2.4 - Big TODO */
124/* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in orig dir */
125/* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL}
126 },
127 {
128/* REPLY */
129/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */
130/* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* INIT in sCL Big TODO */
131/* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},
132/* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
133/* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA},
134/* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA},
135/* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA},
136/* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in reply dir */
137/* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA},
138/* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL}
139 }
140};
141
142static int sctp_pkt_to_tuple(const struct sk_buff *skb,
143 unsigned int dataoff,
144 struct nf_conntrack_tuple *tuple)
145{
146 sctp_sctphdr_t _hdr, *hp;
147
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800148 /* Actually only need first 8 bytes. */
149 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
150 if (hp == NULL)
151 return 0;
152
153 tuple->src.u.sctp.port = hp->source;
154 tuple->dst.u.sctp.port = hp->dest;
155 return 1;
156}
157
158static int sctp_invert_tuple(struct nf_conntrack_tuple *tuple,
159 const struct nf_conntrack_tuple *orig)
160{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161 tuple->src.u.sctp.port = orig->dst.u.sctp.port;
162 tuple->dst.u.sctp.port = orig->src.u.sctp.port;
163 return 1;
164}
165
166/* Print out the per-protocol part of the tuple. */
167static int sctp_print_tuple(struct seq_file *s,
168 const struct nf_conntrack_tuple *tuple)
169{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800170 return seq_printf(s, "sport=%hu dport=%hu ",
171 ntohs(tuple->src.u.sctp.port),
172 ntohs(tuple->dst.u.sctp.port));
173}
174
175/* Print out the private part of the conntrack. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800176static int sctp_print_conntrack(struct seq_file *s, const struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800177{
178 enum sctp_conntrack state;
179
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180 read_lock_bh(&sctp_lock);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800181 state = ct->proto.sctp.state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800182 read_unlock_bh(&sctp_lock);
183
184 return seq_printf(s, "%s ", sctp_conntrack_names[state]);
185}
186
187#define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
Jan Engelhardte79ec502007-12-17 22:44:06 -0800188for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
189 (offset) < (skb)->len && \
190 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
191 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800192
193/* Some validity checks to make sure the chunks are fine */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800194static int do_basic_checks(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800195 const struct sk_buff *skb,
196 unsigned int dataoff,
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800197 unsigned long *map)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800198{
199 u_int32_t offset, count;
200 sctp_chunkhdr_t _sch, *sch;
201 int flag;
202
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203 flag = 0;
204
205 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700206 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207
Patrick McHardy5447d472008-01-14 23:45:48 -0800208 if (sch->type == SCTP_CID_INIT ||
209 sch->type == SCTP_CID_INIT_ACK ||
210 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211 flag = 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800212
Patrick McHardye17df682006-05-02 23:23:07 +0200213 /*
214 * Cookie Ack/Echo chunks not the first OR
215 * Init / Init Ack / Shutdown compl chunks not the only chunks
216 * OR zero-length.
217 */
Patrick McHardy5447d472008-01-14 23:45:48 -0800218 if (((sch->type == SCTP_CID_COOKIE_ACK ||
219 sch->type == SCTP_CID_COOKIE_ECHO ||
220 flag) &&
221 count != 0) || !sch->length) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700222 pr_debug("Basic checks failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223 return 1;
224 }
225
Patrick McHardy5447d472008-01-14 23:45:48 -0800226 if (map)
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800227 set_bit(sch->type, map);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228 }
229
Patrick McHardy0d537782007-07-07 22:39:38 -0700230 pr_debug("Basic checks passed\n");
Patrick McHardydd7271f2006-06-29 21:40:23 -0700231 return count == 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232}
233
Patrick McHardyefe9f682008-01-14 23:47:09 -0800234static int sctp_new_state(enum ip_conntrack_dir dir,
235 enum sctp_conntrack cur_state,
236 int chunk_type)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800237{
238 int i;
239
Patrick McHardy0d537782007-07-07 22:39:38 -0700240 pr_debug("Chunk type: %d\n", chunk_type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800241
242 switch (chunk_type) {
Patrick McHardy5447d472008-01-14 23:45:48 -0800243 case SCTP_CID_INIT:
244 pr_debug("SCTP_CID_INIT\n");
245 i = 0;
246 break;
247 case SCTP_CID_INIT_ACK:
248 pr_debug("SCTP_CID_INIT_ACK\n");
249 i = 1;
250 break;
251 case SCTP_CID_ABORT:
252 pr_debug("SCTP_CID_ABORT\n");
253 i = 2;
254 break;
255 case SCTP_CID_SHUTDOWN:
256 pr_debug("SCTP_CID_SHUTDOWN\n");
257 i = 3;
258 break;
259 case SCTP_CID_SHUTDOWN_ACK:
260 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
261 i = 4;
262 break;
263 case SCTP_CID_ERROR:
264 pr_debug("SCTP_CID_ERROR\n");
265 i = 5;
266 break;
267 case SCTP_CID_COOKIE_ECHO:
268 pr_debug("SCTP_CID_COOKIE_ECHO\n");
269 i = 6;
270 break;
271 case SCTP_CID_COOKIE_ACK:
272 pr_debug("SCTP_CID_COOKIE_ACK\n");
273 i = 7;
274 break;
275 case SCTP_CID_SHUTDOWN_COMPLETE:
276 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
277 i = 8;
278 break;
279 default:
280 /* Other chunks like DATA, SACK, HEARTBEAT and
281 its ACK do not cause a change in state */
282 pr_debug("Unknown chunk type, Will stay in %s\n",
283 sctp_conntrack_names[cur_state]);
284 return cur_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800285 }
286
Patrick McHardy0d537782007-07-07 22:39:38 -0700287 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
288 dir, sctp_conntrack_names[cur_state], chunk_type,
289 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800290
291 return sctp_conntracks[dir][i][cur_state];
292}
293
Patrick McHardyb37e9332008-01-14 23:46:52 -0800294/* Returns verdict for packet, or -NF_ACCEPT for invalid. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800295static int sctp_packet(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800296 const struct sk_buff *skb,
297 unsigned int dataoff,
298 enum ip_conntrack_info ctinfo,
299 int pf,
300 unsigned int hooknum)
301{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800302 enum sctp_conntrack new_state, old_state;
Patrick McHardy85288192008-01-14 23:46:37 -0800303 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304 sctp_sctphdr_t _sctph, *sh;
305 sctp_chunkhdr_t _sch, *sch;
306 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800307 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800308
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800309 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
310 if (sh == NULL)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800311 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800312
Patrick McHardy112f35c2008-01-14 23:46:20 -0800313 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800314 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800315
316 /* Check the verification tag (Sec 8.5) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800317 if (!test_bit(SCTP_CID_INIT, map) &&
318 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
319 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
320 !test_bit(SCTP_CID_ABORT, map) &&
321 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
Patrick McHardy85288192008-01-14 23:46:37 -0800322 sh->vtag != ct->proto.sctp.vtag[dir]) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700323 pr_debug("Verification tag check failed\n");
Patrick McHardyb37e9332008-01-14 23:46:52 -0800324 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800325 }
326
Patrick McHardyefe9f682008-01-14 23:47:09 -0800327 old_state = new_state = SCTP_CONNTRACK_MAX;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800328 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
329 write_lock_bh(&sctp_lock);
330
331 /* Special cases of Verification tag check (Sec 8.5.1) */
332 if (sch->type == SCTP_CID_INIT) {
333 /* Sec 8.5.1 (A) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800334 if (sh->vtag != 0)
335 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800336 } else if (sch->type == SCTP_CID_ABORT) {
337 /* Sec 8.5.1 (B) */
Patrick McHardy85288192008-01-14 23:46:37 -0800338 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
Patrick McHardyb37e9332008-01-14 23:46:52 -0800339 sh->vtag != ct->proto.sctp.vtag[!dir])
340 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800341 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
342 /* Sec 8.5.1 (C) */
Patrick McHardy85288192008-01-14 23:46:37 -0800343 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
344 sh->vtag != ct->proto.sctp.vtag[!dir] &&
Patrick McHardyb37e9332008-01-14 23:46:52 -0800345 (sch->flags & 1))
346 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800347 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
348 /* Sec 8.5.1 (D) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800349 if (sh->vtag != ct->proto.sctp.vtag[dir])
350 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800351 }
352
Patrick McHardyefe9f682008-01-14 23:47:09 -0800353 old_state = ct->proto.sctp.state;
354 new_state = sctp_new_state(dir, old_state, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800355
356 /* Invalid */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800357 if (new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700358 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
359 "conntrack=%u\n",
Patrick McHardyefe9f682008-01-14 23:47:09 -0800360 dir, sch->type, old_state);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800361 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800362 }
363
364 /* If it is an INIT or an INIT ACK note down the vtag */
Patrick McHardy5447d472008-01-14 23:45:48 -0800365 if (sch->type == SCTP_CID_INIT ||
366 sch->type == SCTP_CID_INIT_ACK) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800367 sctp_inithdr_t _inithdr, *ih;
368
369 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800370 sizeof(_inithdr), &_inithdr);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800371 if (ih == NULL)
372 goto out_unlock;
Patrick McHardy0d537782007-07-07 22:39:38 -0700373 pr_debug("Setting vtag %x for dir %d\n",
Patrick McHardy85288192008-01-14 23:46:37 -0800374 ih->init_tag, !dir);
375 ct->proto.sctp.vtag[!dir] = ih->init_tag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800376 }
377
Patrick McHardyefe9f682008-01-14 23:47:09 -0800378 ct->proto.sctp.state = new_state;
379 if (old_state != new_state)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800380 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
381 write_unlock_bh(&sctp_lock);
382 }
383
Patrick McHardyefe9f682008-01-14 23:47:09 -0800384 nf_ct_refresh_acct(ct, ctinfo, skb, *sctp_timeouts[new_state]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385
Patrick McHardyefe9f682008-01-14 23:47:09 -0800386 if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
Patrick McHardy85288192008-01-14 23:46:37 -0800387 dir == IP_CT_DIR_REPLY &&
Patrick McHardyefe9f682008-01-14 23:47:09 -0800388 new_state == SCTP_CONNTRACK_ESTABLISHED) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700389 pr_debug("Setting assured bit\n");
Patrick McHardy112f35c2008-01-14 23:46:20 -0800390 set_bit(IPS_ASSURED_BIT, &ct->status);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800391 nf_conntrack_event_cache(IPCT_STATUS, skb);
392 }
393
394 return NF_ACCEPT;
Patrick McHardyb37e9332008-01-14 23:46:52 -0800395
396out_unlock:
397 write_unlock_bh(&sctp_lock);
398out:
399 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800400}
401
402/* Called when a new connection for this protocol found. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800403static int sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404 unsigned int dataoff)
405{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800406 enum sctp_conntrack new_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800407 sctp_sctphdr_t _sctph, *sh;
408 sctp_chunkhdr_t _sch, *sch;
409 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800410 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800411
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800412 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
413 if (sh == NULL)
414 return 0;
415
Patrick McHardy112f35c2008-01-14 23:46:20 -0800416 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800417 return 0;
418
419 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800420 if (test_bit(SCTP_CID_ABORT, map) ||
421 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
422 test_bit(SCTP_CID_COOKIE_ACK, map))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800424
Patrick McHardyefe9f682008-01-14 23:47:09 -0800425 new_state = SCTP_CONNTRACK_MAX;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
427 /* Don't need lock here: this conntrack not in circulation yet */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800428 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
429 SCTP_CONNTRACK_NONE, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800430
431 /* Invalid: delete conntrack */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800432 if (new_state == SCTP_CONNTRACK_NONE ||
433 new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700434 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800435 return 0;
436 }
437
438 /* Copy the vtag into the state info */
439 if (sch->type == SCTP_CID_INIT) {
440 if (sh->vtag == 0) {
441 sctp_inithdr_t _inithdr, *ih;
442
443 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800444 sizeof(_inithdr), &_inithdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800445 if (ih == NULL)
446 return 0;
447
Patrick McHardy0d537782007-07-07 22:39:38 -0700448 pr_debug("Setting vtag %x for new conn\n",
449 ih->init_tag);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800450
Patrick McHardy112f35c2008-01-14 23:46:20 -0800451 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800452 ih->init_tag;
453 } else {
454 /* Sec 8.5.1 (A) */
455 return 0;
456 }
457 }
458 /* If it is a shutdown ack OOTB packet, we expect a return
459 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
460 else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700461 pr_debug("Setting vtag %x for new conn OOTB\n",
462 sh->vtag);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800463 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800464 }
465
Patrick McHardyefe9f682008-01-14 23:47:09 -0800466 ct->proto.sctp.state = new_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467 }
468
469 return 1;
470}
471
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800472#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100473static unsigned int sctp_sysctl_table_users;
474static struct ctl_table_header *sctp_sysctl_header;
475static struct ctl_table sctp_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800477 .procname = "nf_conntrack_sctp_timeout_closed",
478 .data = &nf_ct_sctp_timeout_closed,
479 .maxlen = sizeof(unsigned int),
480 .mode = 0644,
481 .proc_handler = &proc_dointvec_jiffies,
482 },
483 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800484 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
485 .data = &nf_ct_sctp_timeout_cookie_wait,
486 .maxlen = sizeof(unsigned int),
487 .mode = 0644,
488 .proc_handler = &proc_dointvec_jiffies,
489 },
490 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800491 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
492 .data = &nf_ct_sctp_timeout_cookie_echoed,
493 .maxlen = sizeof(unsigned int),
494 .mode = 0644,
495 .proc_handler = &proc_dointvec_jiffies,
496 },
497 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800498 .procname = "nf_conntrack_sctp_timeout_established",
499 .data = &nf_ct_sctp_timeout_established,
500 .maxlen = sizeof(unsigned int),
501 .mode = 0644,
502 .proc_handler = &proc_dointvec_jiffies,
503 },
504 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800505 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
506 .data = &nf_ct_sctp_timeout_shutdown_sent,
507 .maxlen = sizeof(unsigned int),
508 .mode = 0644,
509 .proc_handler = &proc_dointvec_jiffies,
510 },
511 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800512 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
513 .data = &nf_ct_sctp_timeout_shutdown_recd,
514 .maxlen = sizeof(unsigned int),
515 .mode = 0644,
516 .proc_handler = &proc_dointvec_jiffies,
517 },
518 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800519 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
520 .data = &nf_ct_sctp_timeout_shutdown_ack_sent,
521 .maxlen = sizeof(unsigned int),
522 .mode = 0644,
523 .proc_handler = &proc_dointvec_jiffies,
524 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800525 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100526 .ctl_name = 0
527 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800528};
Patrick McHardya999e682006-11-29 02:35:20 +0100529
530#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
531static struct ctl_table sctp_compat_sysctl_table[] = {
532 {
Patrick McHardya999e682006-11-29 02:35:20 +0100533 .procname = "ip_conntrack_sctp_timeout_closed",
534 .data = &nf_ct_sctp_timeout_closed,
535 .maxlen = sizeof(unsigned int),
536 .mode = 0644,
537 .proc_handler = &proc_dointvec_jiffies,
538 },
539 {
Patrick McHardya999e682006-11-29 02:35:20 +0100540 .procname = "ip_conntrack_sctp_timeout_cookie_wait",
541 .data = &nf_ct_sctp_timeout_cookie_wait,
542 .maxlen = sizeof(unsigned int),
543 .mode = 0644,
544 .proc_handler = &proc_dointvec_jiffies,
545 },
546 {
Patrick McHardya999e682006-11-29 02:35:20 +0100547 .procname = "ip_conntrack_sctp_timeout_cookie_echoed",
548 .data = &nf_ct_sctp_timeout_cookie_echoed,
549 .maxlen = sizeof(unsigned int),
550 .mode = 0644,
551 .proc_handler = &proc_dointvec_jiffies,
552 },
553 {
Patrick McHardya999e682006-11-29 02:35:20 +0100554 .procname = "ip_conntrack_sctp_timeout_established",
555 .data = &nf_ct_sctp_timeout_established,
556 .maxlen = sizeof(unsigned int),
557 .mode = 0644,
558 .proc_handler = &proc_dointvec_jiffies,
559 },
560 {
Patrick McHardya999e682006-11-29 02:35:20 +0100561 .procname = "ip_conntrack_sctp_timeout_shutdown_sent",
562 .data = &nf_ct_sctp_timeout_shutdown_sent,
563 .maxlen = sizeof(unsigned int),
564 .mode = 0644,
565 .proc_handler = &proc_dointvec_jiffies,
566 },
567 {
Patrick McHardya999e682006-11-29 02:35:20 +0100568 .procname = "ip_conntrack_sctp_timeout_shutdown_recd",
569 .data = &nf_ct_sctp_timeout_shutdown_recd,
570 .maxlen = sizeof(unsigned int),
571 .mode = 0644,
572 .proc_handler = &proc_dointvec_jiffies,
573 },
574 {
Patrick McHardya999e682006-11-29 02:35:20 +0100575 .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent",
576 .data = &nf_ct_sctp_timeout_shutdown_ack_sent,
577 .maxlen = sizeof(unsigned int),
578 .mode = 0644,
579 .proc_handler = &proc_dointvec_jiffies,
580 },
581 {
582 .ctl_name = 0
583 }
584};
585#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800586#endif
587
Patrick McHardy61075af2007-07-14 20:48:19 -0700588static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100589 .l3proto = PF_INET,
590 .l4proto = IPPROTO_SCTP,
591 .name = "sctp",
592 .pkt_to_tuple = sctp_pkt_to_tuple,
593 .invert_tuple = sctp_invert_tuple,
594 .print_tuple = sctp_print_tuple,
595 .print_conntrack = sctp_print_conntrack,
596 .packet = sctp_packet,
597 .new = sctp_new,
598 .me = THIS_MODULE,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800599#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
600 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
601 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
602 .nla_policy = nf_ct_port_nla_policy,
603#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100604#ifdef CONFIG_SYSCTL
605 .ctl_table_users = &sctp_sysctl_table_users,
606 .ctl_table_header = &sctp_sysctl_header,
607 .ctl_table = sctp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100608#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
609 .ctl_compat_table = sctp_compat_sysctl_table,
610#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100611#endif
612};
613
Patrick McHardy61075af2007-07-14 20:48:19 -0700614static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100615 .l3proto = PF_INET6,
616 .l4proto = IPPROTO_SCTP,
617 .name = "sctp",
618 .pkt_to_tuple = sctp_pkt_to_tuple,
619 .invert_tuple = sctp_invert_tuple,
620 .print_tuple = sctp_print_tuple,
621 .print_conntrack = sctp_print_conntrack,
622 .packet = sctp_packet,
623 .new = sctp_new,
624 .me = THIS_MODULE,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800625#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
626 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
627 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
628 .nla_policy = nf_ct_port_nla_policy,
629#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100630#ifdef CONFIG_SYSCTL
631 .ctl_table_users = &sctp_sysctl_table_users,
632 .ctl_table_header = &sctp_sysctl_header,
633 .ctl_table = sctp_sysctl_table,
634#endif
635};
636
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800637int __init nf_conntrack_proto_sctp_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800638{
639 int ret;
640
Martin Josefsson605dcad2006-11-29 02:35:06 +0100641 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_sctp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800642 if (ret) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100643 printk("nf_conntrack_l4proto_sctp4: protocol register failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800644 goto out;
645 }
Martin Josefsson605dcad2006-11-29 02:35:06 +0100646 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_sctp6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800647 if (ret) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100648 printk("nf_conntrack_l4proto_sctp6: protocol register failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800649 goto cleanup_sctp4;
650 }
651
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800652 return ret;
653
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800654 cleanup_sctp4:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100655 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800656 out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800657 return ret;
658}
659
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800660void __exit nf_conntrack_proto_sctp_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800661{
Martin Josefsson605dcad2006-11-29 02:35:06 +0100662 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp6);
663 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800664}
665
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800666module_init(nf_conntrack_proto_sctp_init);
667module_exit(nf_conntrack_proto_sctp_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800668
669MODULE_LICENSE("GPL");
670MODULE_AUTHOR("Kiran Kumar Immidi");
671MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP");
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800672MODULE_ALIAS("ip_conntrack_proto_sctp");