blob: f9a08370dbb31f3998ea719c0271a44d5bc1d7d9 [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
Patrick McHardyc88130b2008-01-31 04:42:11 -080028/* Protects ct->proto.sctp */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080029static 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
Patrick McHardy86c0bf42008-01-14 23:48:17 -080052static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] __read_mostly = {
53 [SCTP_CONNTRACK_CLOSED] = 10 SECS,
54 [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
55 [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
56 [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS,
57 [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000,
58 [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000,
59 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
60};
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061
62#define sNO SCTP_CONNTRACK_NONE
63#define sCL SCTP_CONNTRACK_CLOSED
64#define sCW SCTP_CONNTRACK_COOKIE_WAIT
65#define sCE SCTP_CONNTRACK_COOKIE_ECHOED
66#define sES SCTP_CONNTRACK_ESTABLISHED
67#define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
68#define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
69#define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
70#define sIV SCTP_CONNTRACK_MAX
71
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080072/*
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080073 These are the descriptions of the states:
74
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080075NOTE: These state names are tantalizingly similar to the states of an
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080076SCTP endpoint. But the interpretation of the states is a little different,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080077considering that these are the states of the connection and not of an end
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080078point. Please note the subtleties. -Kiran
79
80NONE - Nothing so far.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080081COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
82 an INIT_ACK chunk in the reply direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
84ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
85SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
86SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
87SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080088 to that of the SHUTDOWN chunk.
89CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
90 the SHUTDOWN chunk. Connection is closed.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091*/
92
93/* TODO
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080094 - I have assumed that the first INIT is in the original direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 This messes things when an INIT comes in the reply direction in CLOSED
96 state.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080097 - Check the error type in the reply dir before transitioning from
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080098cookie echoed to closed.
99 - Sec 5.2.4 of RFC 2960
100 - Multi Homing support.
101*/
102
103/* SCTP conntrack state transitions */
Patrick McHardya5e73c22008-01-14 23:45:11 -0800104static const u8 sctp_conntracks[2][9][SCTP_CONNTRACK_MAX] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800105 {
106/* ORIGINAL */
107/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */
108/* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA},
109/* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},
110/* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
111/* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA},
112/* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA},
113/* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant have Stale cookie*/
114/* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA},/* 5.2.4 - Big TODO */
115/* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in orig dir */
116/* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL}
117 },
118 {
119/* REPLY */
120/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */
121/* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* INIT in sCL Big TODO */
122/* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},
123/* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
124/* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA},
125/* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA},
126/* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA},
127/* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in reply dir */
128/* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA},
129/* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL}
130 }
131};
132
133static int sctp_pkt_to_tuple(const struct sk_buff *skb,
134 unsigned int dataoff,
135 struct nf_conntrack_tuple *tuple)
136{
137 sctp_sctphdr_t _hdr, *hp;
138
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800139 /* Actually only need first 8 bytes. */
140 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
141 if (hp == NULL)
142 return 0;
143
144 tuple->src.u.sctp.port = hp->source;
145 tuple->dst.u.sctp.port = hp->dest;
146 return 1;
147}
148
149static int sctp_invert_tuple(struct nf_conntrack_tuple *tuple,
150 const struct nf_conntrack_tuple *orig)
151{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800152 tuple->src.u.sctp.port = orig->dst.u.sctp.port;
153 tuple->dst.u.sctp.port = orig->src.u.sctp.port;
154 return 1;
155}
156
157/* Print out the per-protocol part of the tuple. */
158static int sctp_print_tuple(struct seq_file *s,
159 const struct nf_conntrack_tuple *tuple)
160{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161 return seq_printf(s, "sport=%hu dport=%hu ",
162 ntohs(tuple->src.u.sctp.port),
163 ntohs(tuple->dst.u.sctp.port));
164}
165
166/* Print out the private part of the conntrack. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800167static int sctp_print_conntrack(struct seq_file *s, const struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800168{
169 enum sctp_conntrack state;
170
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171 read_lock_bh(&sctp_lock);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800172 state = ct->proto.sctp.state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800173 read_unlock_bh(&sctp_lock);
174
175 return seq_printf(s, "%s ", sctp_conntrack_names[state]);
176}
177
178#define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
Jan Engelhardte79ec502007-12-17 22:44:06 -0800179for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
180 (offset) < (skb)->len && \
181 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
182 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183
184/* Some validity checks to make sure the chunks are fine */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800185static int do_basic_checks(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186 const struct sk_buff *skb,
187 unsigned int dataoff,
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800188 unsigned long *map)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189{
190 u_int32_t offset, count;
191 sctp_chunkhdr_t _sch, *sch;
192 int flag;
193
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194 flag = 0;
195
196 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700197 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800198
Patrick McHardy5447d472008-01-14 23:45:48 -0800199 if (sch->type == SCTP_CID_INIT ||
200 sch->type == SCTP_CID_INIT_ACK ||
201 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800202 flag = 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203
Patrick McHardye17df682006-05-02 23:23:07 +0200204 /*
205 * Cookie Ack/Echo chunks not the first OR
206 * Init / Init Ack / Shutdown compl chunks not the only chunks
207 * OR zero-length.
208 */
Patrick McHardy5447d472008-01-14 23:45:48 -0800209 if (((sch->type == SCTP_CID_COOKIE_ACK ||
210 sch->type == SCTP_CID_COOKIE_ECHO ||
211 flag) &&
212 count != 0) || !sch->length) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700213 pr_debug("Basic checks failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214 return 1;
215 }
216
Patrick McHardy5447d472008-01-14 23:45:48 -0800217 if (map)
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800218 set_bit(sch->type, map);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219 }
220
Patrick McHardy0d537782007-07-07 22:39:38 -0700221 pr_debug("Basic checks passed\n");
Patrick McHardydd7271f2006-06-29 21:40:23 -0700222 return count == 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223}
224
Patrick McHardyefe9f682008-01-14 23:47:09 -0800225static int sctp_new_state(enum ip_conntrack_dir dir,
226 enum sctp_conntrack cur_state,
227 int chunk_type)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228{
229 int i;
230
Patrick McHardy0d537782007-07-07 22:39:38 -0700231 pr_debug("Chunk type: %d\n", chunk_type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800232
233 switch (chunk_type) {
Patrick McHardy5447d472008-01-14 23:45:48 -0800234 case SCTP_CID_INIT:
235 pr_debug("SCTP_CID_INIT\n");
236 i = 0;
237 break;
238 case SCTP_CID_INIT_ACK:
239 pr_debug("SCTP_CID_INIT_ACK\n");
240 i = 1;
241 break;
242 case SCTP_CID_ABORT:
243 pr_debug("SCTP_CID_ABORT\n");
244 i = 2;
245 break;
246 case SCTP_CID_SHUTDOWN:
247 pr_debug("SCTP_CID_SHUTDOWN\n");
248 i = 3;
249 break;
250 case SCTP_CID_SHUTDOWN_ACK:
251 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
252 i = 4;
253 break;
254 case SCTP_CID_ERROR:
255 pr_debug("SCTP_CID_ERROR\n");
256 i = 5;
257 break;
258 case SCTP_CID_COOKIE_ECHO:
259 pr_debug("SCTP_CID_COOKIE_ECHO\n");
260 i = 6;
261 break;
262 case SCTP_CID_COOKIE_ACK:
263 pr_debug("SCTP_CID_COOKIE_ACK\n");
264 i = 7;
265 break;
266 case SCTP_CID_SHUTDOWN_COMPLETE:
267 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
268 i = 8;
269 break;
270 default:
271 /* Other chunks like DATA, SACK, HEARTBEAT and
272 its ACK do not cause a change in state */
273 pr_debug("Unknown chunk type, Will stay in %s\n",
274 sctp_conntrack_names[cur_state]);
275 return cur_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800276 }
277
Patrick McHardy0d537782007-07-07 22:39:38 -0700278 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
279 dir, sctp_conntrack_names[cur_state], chunk_type,
280 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800281
282 return sctp_conntracks[dir][i][cur_state];
283}
284
Patrick McHardyb37e9332008-01-14 23:46:52 -0800285/* Returns verdict for packet, or -NF_ACCEPT for invalid. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800286static int sctp_packet(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800287 const struct sk_buff *skb,
288 unsigned int dataoff,
289 enum ip_conntrack_info ctinfo,
290 int pf,
291 unsigned int hooknum)
292{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800293 enum sctp_conntrack new_state, old_state;
Patrick McHardy85288192008-01-14 23:46:37 -0800294 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800295 sctp_sctphdr_t _sctph, *sh;
296 sctp_chunkhdr_t _sch, *sch;
297 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800298 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800299
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
301 if (sh == NULL)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800302 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800303
Patrick McHardy112f35c2008-01-14 23:46:20 -0800304 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800305 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800306
307 /* Check the verification tag (Sec 8.5) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800308 if (!test_bit(SCTP_CID_INIT, map) &&
309 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
310 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
311 !test_bit(SCTP_CID_ABORT, map) &&
312 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
Patrick McHardy85288192008-01-14 23:46:37 -0800313 sh->vtag != ct->proto.sctp.vtag[dir]) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700314 pr_debug("Verification tag check failed\n");
Patrick McHardyb37e9332008-01-14 23:46:52 -0800315 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800316 }
317
Patrick McHardyefe9f682008-01-14 23:47:09 -0800318 old_state = new_state = SCTP_CONNTRACK_MAX;
Patrick McHardy4a648302008-01-14 23:47:25 -0800319 write_lock_bh(&sctp_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800320 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800321 /* Special cases of Verification tag check (Sec 8.5.1) */
322 if (sch->type == SCTP_CID_INIT) {
323 /* Sec 8.5.1 (A) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800324 if (sh->vtag != 0)
325 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800326 } else if (sch->type == SCTP_CID_ABORT) {
327 /* Sec 8.5.1 (B) */
Patrick McHardy85288192008-01-14 23:46:37 -0800328 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
Patrick McHardyb37e9332008-01-14 23:46:52 -0800329 sh->vtag != ct->proto.sctp.vtag[!dir])
330 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800331 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
332 /* Sec 8.5.1 (C) */
Patrick McHardy85288192008-01-14 23:46:37 -0800333 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
334 sh->vtag != ct->proto.sctp.vtag[!dir] &&
Patrick McHardy9b1c2cf2008-01-14 23:48:02 -0800335 sch->flags & SCTP_CHUNK_FLAG_T)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800336 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800337 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
338 /* Sec 8.5.1 (D) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800339 if (sh->vtag != ct->proto.sctp.vtag[dir])
340 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800341 }
342
Patrick McHardyefe9f682008-01-14 23:47:09 -0800343 old_state = ct->proto.sctp.state;
344 new_state = sctp_new_state(dir, old_state, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800345
346 /* Invalid */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800347 if (new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700348 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
349 "conntrack=%u\n",
Patrick McHardyefe9f682008-01-14 23:47:09 -0800350 dir, sch->type, old_state);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800351 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800352 }
353
354 /* If it is an INIT or an INIT ACK note down the vtag */
Patrick McHardy5447d472008-01-14 23:45:48 -0800355 if (sch->type == SCTP_CID_INIT ||
356 sch->type == SCTP_CID_INIT_ACK) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800357 sctp_inithdr_t _inithdr, *ih;
358
359 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800360 sizeof(_inithdr), &_inithdr);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800361 if (ih == NULL)
362 goto out_unlock;
Patrick McHardy0d537782007-07-07 22:39:38 -0700363 pr_debug("Setting vtag %x for dir %d\n",
Patrick McHardy85288192008-01-14 23:46:37 -0800364 ih->init_tag, !dir);
365 ct->proto.sctp.vtag[!dir] = ih->init_tag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800366 }
367
Patrick McHardyefe9f682008-01-14 23:47:09 -0800368 ct->proto.sctp.state = new_state;
369 if (old_state != new_state)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800370 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800371 }
Patrick McHardy4a648302008-01-14 23:47:25 -0800372 write_unlock_bh(&sctp_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800373
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800374 nf_ct_refresh_acct(ct, ctinfo, skb, sctp_timeouts[new_state]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800375
Patrick McHardyefe9f682008-01-14 23:47:09 -0800376 if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
Patrick McHardy85288192008-01-14 23:46:37 -0800377 dir == IP_CT_DIR_REPLY &&
Patrick McHardyefe9f682008-01-14 23:47:09 -0800378 new_state == SCTP_CONNTRACK_ESTABLISHED) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700379 pr_debug("Setting assured bit\n");
Patrick McHardy112f35c2008-01-14 23:46:20 -0800380 set_bit(IPS_ASSURED_BIT, &ct->status);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800381 nf_conntrack_event_cache(IPCT_STATUS, skb);
382 }
383
384 return NF_ACCEPT;
Patrick McHardyb37e9332008-01-14 23:46:52 -0800385
386out_unlock:
387 write_unlock_bh(&sctp_lock);
388out:
389 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800390}
391
392/* Called when a new connection for this protocol found. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800393static int sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800394 unsigned int dataoff)
395{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800396 enum sctp_conntrack new_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800397 sctp_sctphdr_t _sctph, *sh;
398 sctp_chunkhdr_t _sch, *sch;
399 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800400 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800402 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
403 if (sh == NULL)
404 return 0;
405
Patrick McHardy112f35c2008-01-14 23:46:20 -0800406 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800407 return 0;
408
409 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800410 if (test_bit(SCTP_CID_ABORT, map) ||
411 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
412 test_bit(SCTP_CID_COOKIE_ACK, map))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800413 return 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414
Patrick McHardyefe9f682008-01-14 23:47:09 -0800415 new_state = SCTP_CONNTRACK_MAX;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800416 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
417 /* Don't need lock here: this conntrack not in circulation yet */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800418 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
419 SCTP_CONNTRACK_NONE, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420
421 /* Invalid: delete conntrack */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800422 if (new_state == SCTP_CONNTRACK_NONE ||
423 new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700424 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800425 return 0;
426 }
427
428 /* Copy the vtag into the state info */
429 if (sch->type == SCTP_CID_INIT) {
430 if (sh->vtag == 0) {
431 sctp_inithdr_t _inithdr, *ih;
432
433 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800434 sizeof(_inithdr), &_inithdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800435 if (ih == NULL)
436 return 0;
437
Patrick McHardy0d537782007-07-07 22:39:38 -0700438 pr_debug("Setting vtag %x for new conn\n",
439 ih->init_tag);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800440
Patrick McHardy112f35c2008-01-14 23:46:20 -0800441 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800442 ih->init_tag;
443 } else {
444 /* Sec 8.5.1 (A) */
445 return 0;
446 }
447 }
448 /* If it is a shutdown ack OOTB packet, we expect a return
449 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
450 else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700451 pr_debug("Setting vtag %x for new conn OOTB\n",
452 sh->vtag);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800453 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800454 }
455
Patrick McHardyefe9f682008-01-14 23:47:09 -0800456 ct->proto.sctp.state = new_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800457 }
458
459 return 1;
460}
461
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100463static unsigned int sctp_sysctl_table_users;
464static struct ctl_table_header *sctp_sysctl_header;
465static struct ctl_table sctp_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800466 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800467 .procname = "nf_conntrack_sctp_timeout_closed",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800468 .data = &sctp_timeouts[SCTP_CONNTRACK_CLOSED],
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800469 .maxlen = sizeof(unsigned int),
470 .mode = 0644,
471 .proc_handler = &proc_dointvec_jiffies,
472 },
473 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800474 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800475 .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_WAIT],
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 .maxlen = sizeof(unsigned int),
477 .mode = 0644,
478 .proc_handler = &proc_dointvec_jiffies,
479 },
480 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800481 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800482 .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_ECHOED],
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800483 .maxlen = sizeof(unsigned int),
484 .mode = 0644,
485 .proc_handler = &proc_dointvec_jiffies,
486 },
487 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800488 .procname = "nf_conntrack_sctp_timeout_established",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800489 .data = &sctp_timeouts[SCTP_CONNTRACK_ESTABLISHED],
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800490 .maxlen = sizeof(unsigned int),
491 .mode = 0644,
492 .proc_handler = &proc_dointvec_jiffies,
493 },
494 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800495 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800496 .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT],
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800497 .maxlen = sizeof(unsigned int),
498 .mode = 0644,
499 .proc_handler = &proc_dointvec_jiffies,
500 },
501 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800502 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800503 .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD],
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800504 .maxlen = sizeof(unsigned int),
505 .mode = 0644,
506 .proc_handler = &proc_dointvec_jiffies,
507 },
508 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800509 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800510 .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT],
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800511 .maxlen = sizeof(unsigned int),
512 .mode = 0644,
513 .proc_handler = &proc_dointvec_jiffies,
514 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800515 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100516 .ctl_name = 0
517 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800518};
Patrick McHardya999e682006-11-29 02:35:20 +0100519
520#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
521static struct ctl_table sctp_compat_sysctl_table[] = {
522 {
Patrick McHardya999e682006-11-29 02:35:20 +0100523 .procname = "ip_conntrack_sctp_timeout_closed",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800524 .data = &sctp_timeouts[SCTP_CONNTRACK_CLOSED],
Patrick McHardya999e682006-11-29 02:35:20 +0100525 .maxlen = sizeof(unsigned int),
526 .mode = 0644,
527 .proc_handler = &proc_dointvec_jiffies,
528 },
529 {
Patrick McHardya999e682006-11-29 02:35:20 +0100530 .procname = "ip_conntrack_sctp_timeout_cookie_wait",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800531 .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_WAIT],
Patrick McHardya999e682006-11-29 02:35:20 +0100532 .maxlen = sizeof(unsigned int),
533 .mode = 0644,
534 .proc_handler = &proc_dointvec_jiffies,
535 },
536 {
Patrick McHardya999e682006-11-29 02:35:20 +0100537 .procname = "ip_conntrack_sctp_timeout_cookie_echoed",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800538 .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_ECHOED],
Patrick McHardya999e682006-11-29 02:35:20 +0100539 .maxlen = sizeof(unsigned int),
540 .mode = 0644,
541 .proc_handler = &proc_dointvec_jiffies,
542 },
543 {
Patrick McHardya999e682006-11-29 02:35:20 +0100544 .procname = "ip_conntrack_sctp_timeout_established",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800545 .data = &sctp_timeouts[SCTP_CONNTRACK_ESTABLISHED],
Patrick McHardya999e682006-11-29 02:35:20 +0100546 .maxlen = sizeof(unsigned int),
547 .mode = 0644,
548 .proc_handler = &proc_dointvec_jiffies,
549 },
550 {
Patrick McHardya999e682006-11-29 02:35:20 +0100551 .procname = "ip_conntrack_sctp_timeout_shutdown_sent",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800552 .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT],
Patrick McHardya999e682006-11-29 02:35:20 +0100553 .maxlen = sizeof(unsigned int),
554 .mode = 0644,
555 .proc_handler = &proc_dointvec_jiffies,
556 },
557 {
Patrick McHardya999e682006-11-29 02:35:20 +0100558 .procname = "ip_conntrack_sctp_timeout_shutdown_recd",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800559 .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD],
Patrick McHardya999e682006-11-29 02:35:20 +0100560 .maxlen = sizeof(unsigned int),
561 .mode = 0644,
562 .proc_handler = &proc_dointvec_jiffies,
563 },
564 {
Patrick McHardya999e682006-11-29 02:35:20 +0100565 .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent",
Patrick McHardy86c0bf42008-01-14 23:48:17 -0800566 .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT],
Patrick McHardya999e682006-11-29 02:35:20 +0100567 .maxlen = sizeof(unsigned int),
568 .mode = 0644,
569 .proc_handler = &proc_dointvec_jiffies,
570 },
571 {
572 .ctl_name = 0
573 }
574};
575#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800576#endif
577
Patrick McHardy61075af2007-07-14 20:48:19 -0700578static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100579 .l3proto = PF_INET,
580 .l4proto = IPPROTO_SCTP,
581 .name = "sctp",
582 .pkt_to_tuple = sctp_pkt_to_tuple,
583 .invert_tuple = sctp_invert_tuple,
584 .print_tuple = sctp_print_tuple,
585 .print_conntrack = sctp_print_conntrack,
586 .packet = sctp_packet,
587 .new = sctp_new,
588 .me = THIS_MODULE,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800589#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
590 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
591 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
592 .nla_policy = nf_ct_port_nla_policy,
593#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100594#ifdef CONFIG_SYSCTL
595 .ctl_table_users = &sctp_sysctl_table_users,
596 .ctl_table_header = &sctp_sysctl_header,
597 .ctl_table = sctp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100598#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
599 .ctl_compat_table = sctp_compat_sysctl_table,
600#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100601#endif
602};
603
Patrick McHardy61075af2007-07-14 20:48:19 -0700604static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100605 .l3proto = PF_INET6,
606 .l4proto = IPPROTO_SCTP,
607 .name = "sctp",
608 .pkt_to_tuple = sctp_pkt_to_tuple,
609 .invert_tuple = sctp_invert_tuple,
610 .print_tuple = sctp_print_tuple,
611 .print_conntrack = sctp_print_conntrack,
612 .packet = sctp_packet,
613 .new = sctp_new,
614 .me = THIS_MODULE,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800615#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
616 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
617 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
618 .nla_policy = nf_ct_port_nla_policy,
619#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100620#ifdef CONFIG_SYSCTL
621 .ctl_table_users = &sctp_sysctl_table_users,
622 .ctl_table_header = &sctp_sysctl_header,
623 .ctl_table = sctp_sysctl_table,
624#endif
625};
626
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800627static int __init nf_conntrack_proto_sctp_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800628{
629 int ret;
630
Martin Josefsson605dcad2006-11-29 02:35:06 +0100631 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_sctp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800632 if (ret) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100633 printk("nf_conntrack_l4proto_sctp4: protocol register failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800634 goto out;
635 }
Martin Josefsson605dcad2006-11-29 02:35:06 +0100636 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_sctp6);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800637 if (ret) {
Martin Josefsson605dcad2006-11-29 02:35:06 +0100638 printk("nf_conntrack_l4proto_sctp6: protocol register failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800639 goto cleanup_sctp4;
640 }
641
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800642 return ret;
643
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800644 cleanup_sctp4:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100645 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800646 out:
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800647 return ret;
648}
649
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800650static void __exit nf_conntrack_proto_sctp_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800651{
Martin Josefsson605dcad2006-11-29 02:35:06 +0100652 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp6);
653 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800654}
655
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800656module_init(nf_conntrack_proto_sctp_init);
657module_exit(nf_conntrack_proto_sctp_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800658
659MODULE_LICENSE("GPL");
660MODULE_AUTHOR("Kiran Kumar Immidi");
661MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP");
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800662MODULE_ALIAS("ip_conntrack_proto_sctp");