blob: c746d61f83edb562f17b64ba63566bc0f12d8b2b [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/* FIXME: Examine ipfilter's timeouts and conntrack transitions more
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080029 closely. They're more complex. --RR
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030
31 And so for me for SCTP :D -Kiran */
32
Jan Engelhardt12c33aa2008-04-14 11:15:54 +020033static const char *const sctp_conntrack_names[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034 "NONE",
35 "CLOSED",
36 "COOKIE_WAIT",
37 "COOKIE_ECHOED",
38 "ESTABLISHED",
39 "SHUTDOWN_SENT",
40 "SHUTDOWN_RECD",
41 "SHUTDOWN_ACK_SENT",
42};
43
44#define SECS * HZ
45#define MINS * 60 SECS
46#define HOURS * 60 MINS
47#define DAYS * 24 HOURS
48
Patrick McHardy86c0bf42008-01-14 23:48:17 -080049static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] __read_mostly = {
50 [SCTP_CONNTRACK_CLOSED] = 10 SECS,
51 [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
52 [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
53 [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS,
54 [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000,
55 [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000,
56 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
57};
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058
59#define sNO SCTP_CONNTRACK_NONE
60#define sCL SCTP_CONNTRACK_CLOSED
61#define sCW SCTP_CONNTRACK_COOKIE_WAIT
62#define sCE SCTP_CONNTRACK_COOKIE_ECHOED
63#define sES SCTP_CONNTRACK_ESTABLISHED
64#define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
65#define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
66#define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
67#define sIV SCTP_CONNTRACK_MAX
68
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080069/*
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080070 These are the descriptions of the states:
71
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080072NOTE: These state names are tantalizingly similar to the states of an
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080073SCTP endpoint. But the interpretation of the states is a little different,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080074considering that these are the states of the connection and not of an end
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080075point. Please note the subtleties. -Kiran
76
77NONE - Nothing so far.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080078COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
79 an INIT_ACK chunk in the reply direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080080COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
81ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
82SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
83SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
84SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080085 to that of the SHUTDOWN chunk.
86CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
87 the SHUTDOWN chunk. Connection is closed.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080088*/
89
90/* TODO
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080091 - I have assumed that the first INIT is in the original direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092 This messes things when an INIT comes in the reply direction in CLOSED
93 state.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080094 - Check the error type in the reply dir before transitioning from
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095cookie echoed to closed.
96 - Sec 5.2.4 of RFC 2960
97 - Multi Homing support.
98*/
99
100/* SCTP conntrack state transitions */
Patrick McHardya5e73c22008-01-14 23:45:11 -0800101static const u8 sctp_conntracks[2][9][SCTP_CONNTRACK_MAX] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102 {
103/* ORIGINAL */
104/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */
105/* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA},
106/* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},
107/* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
108/* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA},
109/* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA},
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300110/* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Can't have Stale cookie*/
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800111/* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA},/* 5.2.4 - Big TODO */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300112/* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Can't come in orig dir */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800113/* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL}
114 },
115 {
116/* REPLY */
117/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */
118/* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* INIT in sCL Big TODO */
119/* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},
120/* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
121/* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA},
122/* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA},
123/* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA},
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300124/* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Can't come in reply dir */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800125/* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA},
126/* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL}
127 }
128};
129
Gao feng49d485a32012-05-28 21:04:18 +0000130static int sctp_net_id __read_mostly;
131struct sctp_net {
132 struct nf_proto_net pn;
133 unsigned int timeouts[SCTP_CONNTRACK_MAX];
134};
135
136static inline struct sctp_net *sctp_pernet(struct net *net)
137{
138 return net_generic(net, sctp_net_id);
139}
140
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200141static bool sctp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
142 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800143{
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200144 const struct sctphdr *hp;
145 struct sctphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147 /* Actually only need first 8 bytes. */
148 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
149 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200150 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151
152 tuple->src.u.sctp.port = hp->source;
153 tuple->dst.u.sctp.port = hp->dest;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200154 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800155}
156
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200157static bool sctp_invert_tuple(struct nf_conntrack_tuple *tuple,
158 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800159{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160 tuple->src.u.sctp.port = orig->dst.u.sctp.port;
161 tuple->dst.u.sctp.port = orig->src.u.sctp.port;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200162 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163}
164
165/* Print out the per-protocol part of the tuple. */
166static int sctp_print_tuple(struct seq_file *s,
167 const struct nf_conntrack_tuple *tuple)
168{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169 return seq_printf(s, "sport=%hu dport=%hu ",
170 ntohs(tuple->src.u.sctp.port),
171 ntohs(tuple->dst.u.sctp.port));
172}
173
174/* Print out the private part of the conntrack. */
Patrick McHardy440f0d52009-06-10 14:32:47 +0200175static int sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176{
177 enum sctp_conntrack state;
178
Patrick McHardy440f0d52009-06-10 14:32:47 +0200179 spin_lock_bh(&ct->lock);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800180 state = ct->proto.sctp.state;
Patrick McHardy440f0d52009-06-10 14:32:47 +0200181 spin_unlock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800182
183 return seq_printf(s, "%s ", sctp_conntrack_names[state]);
184}
185
186#define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
Jan Engelhardte79ec502007-12-17 22:44:06 -0800187for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
188 (offset) < (skb)->len && \
189 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
190 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191
192/* Some validity checks to make sure the chunks are fine */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800193static int do_basic_checks(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194 const struct sk_buff *skb,
195 unsigned int dataoff,
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800196 unsigned long *map)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197{
198 u_int32_t offset, count;
199 sctp_chunkhdr_t _sch, *sch;
200 int flag;
201
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800202 flag = 0;
203
204 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700205 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800206
Patrick McHardy5447d472008-01-14 23:45:48 -0800207 if (sch->type == SCTP_CID_INIT ||
208 sch->type == SCTP_CID_INIT_ACK ||
209 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800210 flag = 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211
Patrick McHardye17df682006-05-02 23:23:07 +0200212 /*
213 * Cookie Ack/Echo chunks not the first OR
214 * Init / Init Ack / Shutdown compl chunks not the only chunks
215 * OR zero-length.
216 */
Patrick McHardy5447d472008-01-14 23:45:48 -0800217 if (((sch->type == SCTP_CID_COOKIE_ACK ||
218 sch->type == SCTP_CID_COOKIE_ECHO ||
219 flag) &&
220 count != 0) || !sch->length) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700221 pr_debug("Basic checks failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800222 return 1;
223 }
224
Patrick McHardy5447d472008-01-14 23:45:48 -0800225 if (map)
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800226 set_bit(sch->type, map);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800227 }
228
Patrick McHardy0d537782007-07-07 22:39:38 -0700229 pr_debug("Basic checks passed\n");
Patrick McHardydd7271f2006-06-29 21:40:23 -0700230 return count == 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800231}
232
Patrick McHardyefe9f682008-01-14 23:47:09 -0800233static int sctp_new_state(enum ip_conntrack_dir dir,
234 enum sctp_conntrack cur_state,
235 int chunk_type)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800236{
237 int i;
238
Patrick McHardy0d537782007-07-07 22:39:38 -0700239 pr_debug("Chunk type: %d\n", chunk_type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800240
241 switch (chunk_type) {
Patrick McHardy5447d472008-01-14 23:45:48 -0800242 case SCTP_CID_INIT:
243 pr_debug("SCTP_CID_INIT\n");
244 i = 0;
245 break;
246 case SCTP_CID_INIT_ACK:
247 pr_debug("SCTP_CID_INIT_ACK\n");
248 i = 1;
249 break;
250 case SCTP_CID_ABORT:
251 pr_debug("SCTP_CID_ABORT\n");
252 i = 2;
253 break;
254 case SCTP_CID_SHUTDOWN:
255 pr_debug("SCTP_CID_SHUTDOWN\n");
256 i = 3;
257 break;
258 case SCTP_CID_SHUTDOWN_ACK:
259 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
260 i = 4;
261 break;
262 case SCTP_CID_ERROR:
263 pr_debug("SCTP_CID_ERROR\n");
264 i = 5;
265 break;
266 case SCTP_CID_COOKIE_ECHO:
267 pr_debug("SCTP_CID_COOKIE_ECHO\n");
268 i = 6;
269 break;
270 case SCTP_CID_COOKIE_ACK:
271 pr_debug("SCTP_CID_COOKIE_ACK\n");
272 i = 7;
273 break;
274 case SCTP_CID_SHUTDOWN_COMPLETE:
275 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
276 i = 8;
277 break;
278 default:
279 /* Other chunks like DATA, SACK, HEARTBEAT and
280 its ACK do not cause a change in state */
281 pr_debug("Unknown chunk type, Will stay in %s\n",
282 sctp_conntrack_names[cur_state]);
283 return cur_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800284 }
285
Patrick McHardy0d537782007-07-07 22:39:38 -0700286 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
287 dir, sctp_conntrack_names[cur_state], chunk_type,
288 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800289
290 return sctp_conntracks[dir][i][cur_state];
291}
292
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100293static unsigned int *sctp_get_timeouts(struct net *net)
294{
Gao feng49d485a32012-05-28 21:04:18 +0000295 return sctp_pernet(net)->timeouts;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100296}
297
Patrick McHardyb37e9332008-01-14 23:46:52 -0800298/* Returns verdict for packet, or -NF_ACCEPT for invalid. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800299static int sctp_packet(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300 const struct sk_buff *skb,
301 unsigned int dataoff,
302 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200303 u_int8_t pf,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100304 unsigned int hooknum,
305 unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800306{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800307 enum sctp_conntrack new_state, old_state;
Patrick McHardy85288192008-01-14 23:46:37 -0800308 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200309 const struct sctphdr *sh;
310 struct sctphdr _sctph;
311 const struct sctp_chunkhdr *sch;
312 struct sctp_chunkhdr _sch;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800314 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800315
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800316 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
317 if (sh == NULL)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800318 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800319
Patrick McHardy112f35c2008-01-14 23:46:20 -0800320 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800321 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800322
323 /* Check the verification tag (Sec 8.5) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800324 if (!test_bit(SCTP_CID_INIT, map) &&
325 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
326 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
327 !test_bit(SCTP_CID_ABORT, map) &&
328 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
Patrick McHardy85288192008-01-14 23:46:37 -0800329 sh->vtag != ct->proto.sctp.vtag[dir]) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700330 pr_debug("Verification tag check failed\n");
Patrick McHardyb37e9332008-01-14 23:46:52 -0800331 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800332 }
333
Patrick McHardy328bd892008-11-24 13:44:55 +0100334 old_state = new_state = SCTP_CONNTRACK_NONE;
Patrick McHardy440f0d52009-06-10 14:32:47 +0200335 spin_lock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800336 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800337 /* Special cases of Verification tag check (Sec 8.5.1) */
338 if (sch->type == SCTP_CID_INIT) {
339 /* Sec 8.5.1 (A) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800340 if (sh->vtag != 0)
341 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800342 } else if (sch->type == SCTP_CID_ABORT) {
343 /* Sec 8.5.1 (B) */
Patrick McHardy85288192008-01-14 23:46:37 -0800344 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
Patrick McHardyb37e9332008-01-14 23:46:52 -0800345 sh->vtag != ct->proto.sctp.vtag[!dir])
346 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800347 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
348 /* Sec 8.5.1 (C) */
Patrick McHardy85288192008-01-14 23:46:37 -0800349 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
350 sh->vtag != ct->proto.sctp.vtag[!dir] &&
Patrick McHardy9b1c2cf2008-01-14 23:48:02 -0800351 sch->flags & SCTP_CHUNK_FLAG_T)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800352 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800353 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
354 /* Sec 8.5.1 (D) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800355 if (sh->vtag != ct->proto.sctp.vtag[dir])
356 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800357 }
358
Patrick McHardyefe9f682008-01-14 23:47:09 -0800359 old_state = ct->proto.sctp.state;
360 new_state = sctp_new_state(dir, old_state, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800361
362 /* Invalid */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800363 if (new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700364 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
365 "conntrack=%u\n",
Patrick McHardyefe9f682008-01-14 23:47:09 -0800366 dir, sch->type, old_state);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800367 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800368 }
369
370 /* If it is an INIT or an INIT ACK note down the vtag */
Patrick McHardy5447d472008-01-14 23:45:48 -0800371 if (sch->type == SCTP_CID_INIT ||
372 sch->type == SCTP_CID_INIT_ACK) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800373 sctp_inithdr_t _inithdr, *ih;
374
375 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800376 sizeof(_inithdr), &_inithdr);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800377 if (ih == NULL)
378 goto out_unlock;
Patrick McHardy0d537782007-07-07 22:39:38 -0700379 pr_debug("Setting vtag %x for dir %d\n",
Patrick McHardy85288192008-01-14 23:46:37 -0800380 ih->init_tag, !dir);
381 ct->proto.sctp.vtag[!dir] = ih->init_tag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800382 }
383
Patrick McHardyefe9f682008-01-14 23:47:09 -0800384 ct->proto.sctp.state = new_state;
385 if (old_state != new_state)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200386 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800387 }
Patrick McHardy440f0d52009-06-10 14:32:47 +0200388 spin_unlock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800389
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100390 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800391
Patrick McHardyefe9f682008-01-14 23:47:09 -0800392 if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
Patrick McHardy85288192008-01-14 23:46:37 -0800393 dir == IP_CT_DIR_REPLY &&
Patrick McHardyefe9f682008-01-14 23:47:09 -0800394 new_state == SCTP_CONNTRACK_ESTABLISHED) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700395 pr_debug("Setting assured bit\n");
Patrick McHardy112f35c2008-01-14 23:46:20 -0800396 set_bit(IPS_ASSURED_BIT, &ct->status);
Patrick McHardy858b31332010-02-03 13:48:53 +0100397 nf_conntrack_event_cache(IPCT_ASSURED, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398 }
399
400 return NF_ACCEPT;
Patrick McHardyb37e9332008-01-14 23:46:52 -0800401
402out_unlock:
Patrick McHardy440f0d52009-06-10 14:32:47 +0200403 spin_unlock_bh(&ct->lock);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800404out:
405 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406}
407
408/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200409static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100410 unsigned int dataoff, unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800411{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800412 enum sctp_conntrack new_state;
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200413 const struct sctphdr *sh;
414 struct sctphdr _sctph;
415 const struct sctp_chunkhdr *sch;
416 struct sctp_chunkhdr _sch;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800417 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800418 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800419
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
421 if (sh == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200422 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423
Patrick McHardy112f35c2008-01-14 23:46:20 -0800424 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200425 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426
427 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800428 if (test_bit(SCTP_CID_ABORT, map) ||
429 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
430 test_bit(SCTP_CID_COOKIE_ACK, map))
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200431 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800432
Changli Gaoe5fc9e72010-11-12 17:33:17 +0100433 memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
Patrick McHardyefe9f682008-01-14 23:47:09 -0800434 new_state = SCTP_CONNTRACK_MAX;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800435 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
436 /* Don't need lock here: this conntrack not in circulation yet */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800437 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
438 SCTP_CONNTRACK_NONE, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800439
440 /* Invalid: delete conntrack */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800441 if (new_state == SCTP_CONNTRACK_NONE ||
442 new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700443 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200444 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800445 }
446
447 /* Copy the vtag into the state info */
448 if (sch->type == SCTP_CID_INIT) {
449 if (sh->vtag == 0) {
450 sctp_inithdr_t _inithdr, *ih;
451
452 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800453 sizeof(_inithdr), &_inithdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800454 if (ih == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200455 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456
Patrick McHardy0d537782007-07-07 22:39:38 -0700457 pr_debug("Setting vtag %x for new conn\n",
458 ih->init_tag);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459
Patrick McHardy112f35c2008-01-14 23:46:20 -0800460 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800461 ih->init_tag;
462 } else {
463 /* Sec 8.5.1 (A) */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200464 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800465 }
466 }
467 /* If it is a shutdown ack OOTB packet, we expect a return
468 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
469 else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700470 pr_debug("Setting vtag %x for new conn OOTB\n",
471 sh->vtag);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800472 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800473 }
474
Patrick McHardyefe9f682008-01-14 23:47:09 -0800475 ct->proto.sctp.state = new_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 }
477
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200478 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800479}
480
Igor Maravićc0cd1152011-12-12 02:58:24 +0000481#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700482
483#include <linux/netfilter/nfnetlink.h>
484#include <linux/netfilter/nfnetlink_conntrack.h>
485
486static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
Patrick McHardy440f0d52009-06-10 14:32:47 +0200487 struct nf_conn *ct)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700488{
489 struct nlattr *nest_parms;
490
Patrick McHardy440f0d52009-06-10 14:32:47 +0200491 spin_lock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700492 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
493 if (!nest_parms)
494 goto nla_put_failure;
495
David S. Miller5e8d1eb2012-04-01 18:51:07 -0400496 if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
497 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
498 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
499 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
500 ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
501 goto nla_put_failure;
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700502
Patrick McHardy440f0d52009-06-10 14:32:47 +0200503 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700504
505 nla_nest_end(skb, nest_parms);
506
507 return 0;
508
509nla_put_failure:
Patrick McHardy440f0d52009-06-10 14:32:47 +0200510 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700511 return -1;
512}
513
514static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
515 [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
516 [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
517 [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
518};
519
520static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
521{
522 struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
523 struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
524 int err;
525
526 /* updates may not contain the internal protocol info, skip parsing */
527 if (!attr)
528 return 0;
529
530 err = nla_parse_nested(tb,
531 CTA_PROTOINFO_SCTP_MAX,
532 attr,
533 sctp_nla_policy);
534 if (err < 0)
535 return err;
536
537 if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
538 !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
539 !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
540 return -EINVAL;
541
Patrick McHardy440f0d52009-06-10 14:32:47 +0200542 spin_lock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700543 ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
544 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
Patrick McHardy5547cd02008-07-21 10:03:49 -0700545 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700546 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Patrick McHardy5547cd02008-07-21 10:03:49 -0700547 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
Patrick McHardy440f0d52009-06-10 14:32:47 +0200548 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700549
550 return 0;
551}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100552
553static int sctp_nlattr_size(void)
554{
555 return nla_total_size(0) /* CTA_PROTOINFO_SCTP */
556 + nla_policy_len(sctp_nla_policy, CTA_PROTOINFO_SCTP_MAX + 1);
557}
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700558#endif
559
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100560#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
561
562#include <linux/netfilter/nfnetlink.h>
563#include <linux/netfilter/nfnetlink_cttimeout.h>
564
Gao feng8264deb2012-05-28 21:04:23 +0000565static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
566 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100567{
568 unsigned int *timeouts = data;
Gao feng8264deb2012-05-28 21:04:23 +0000569 struct sctp_net *sn = sctp_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100570 int i;
571
572 /* set default SCTP timeouts. */
573 for (i=0; i<SCTP_CONNTRACK_MAX; i++)
Gao feng8264deb2012-05-28 21:04:23 +0000574 timeouts[i] = sn->timeouts[i];
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100575
576 /* there's a 1:1 mapping between attributes and protocol states. */
577 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
578 if (tb[i]) {
579 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
580 }
581 }
582 return 0;
583}
584
585static int
586sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
587{
588 const unsigned int *timeouts = data;
589 int i;
590
David S. Miller5e8d1eb2012-04-01 18:51:07 -0400591 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
592 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
593 goto nla_put_failure;
594 }
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100595 return 0;
596
597nla_put_failure:
598 return -ENOSPC;
599}
600
601static const struct nla_policy
602sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
603 [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
604 [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
605 [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
606 [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
607 [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
608 [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
609 [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
610};
611#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
612
613
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800614#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100615static struct ctl_table sctp_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800616 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800617 .procname = "nf_conntrack_sctp_timeout_closed",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800618 .maxlen = sizeof(unsigned int),
619 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800620 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800621 },
622 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800623 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800624 .maxlen = sizeof(unsigned int),
625 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800626 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800627 },
628 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800629 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800630 .maxlen = sizeof(unsigned int),
631 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800632 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800633 },
634 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800635 .procname = "nf_conntrack_sctp_timeout_established",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800636 .maxlen = sizeof(unsigned int),
637 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800638 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800639 },
640 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800641 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800642 .maxlen = sizeof(unsigned int),
643 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800644 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800645 },
646 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800647 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800648 .maxlen = sizeof(unsigned int),
649 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800650 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800651 },
652 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800654 .maxlen = sizeof(unsigned int),
655 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800656 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800657 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800658 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800659};
Patrick McHardya999e682006-11-29 02:35:20 +0100660
661#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
662static struct ctl_table sctp_compat_sysctl_table[] = {
663 {
Patrick McHardya999e682006-11-29 02:35:20 +0100664 .procname = "ip_conntrack_sctp_timeout_closed",
Patrick McHardya999e682006-11-29 02:35:20 +0100665 .maxlen = sizeof(unsigned int),
666 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800667 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100668 },
669 {
Patrick McHardya999e682006-11-29 02:35:20 +0100670 .procname = "ip_conntrack_sctp_timeout_cookie_wait",
Patrick McHardya999e682006-11-29 02:35:20 +0100671 .maxlen = sizeof(unsigned int),
672 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800673 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100674 },
675 {
Patrick McHardya999e682006-11-29 02:35:20 +0100676 .procname = "ip_conntrack_sctp_timeout_cookie_echoed",
Patrick McHardya999e682006-11-29 02:35:20 +0100677 .maxlen = sizeof(unsigned int),
678 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800679 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100680 },
681 {
Patrick McHardya999e682006-11-29 02:35:20 +0100682 .procname = "ip_conntrack_sctp_timeout_established",
Patrick McHardya999e682006-11-29 02:35:20 +0100683 .maxlen = sizeof(unsigned int),
684 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800685 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100686 },
687 {
Patrick McHardya999e682006-11-29 02:35:20 +0100688 .procname = "ip_conntrack_sctp_timeout_shutdown_sent",
Patrick McHardya999e682006-11-29 02:35:20 +0100689 .maxlen = sizeof(unsigned int),
690 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800691 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100692 },
693 {
Patrick McHardya999e682006-11-29 02:35:20 +0100694 .procname = "ip_conntrack_sctp_timeout_shutdown_recd",
Patrick McHardya999e682006-11-29 02:35:20 +0100695 .maxlen = sizeof(unsigned int),
696 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800697 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100698 },
699 {
Patrick McHardya999e682006-11-29 02:35:20 +0100700 .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent",
Patrick McHardya999e682006-11-29 02:35:20 +0100701 .maxlen = sizeof(unsigned int),
702 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800703 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100704 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800705 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100706};
707#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800708#endif
709
Gao fengf42c4182012-06-21 04:36:46 +0000710static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
711 struct sctp_net *sn)
Gao feng49d485a32012-05-28 21:04:18 +0000712{
713#ifdef CONFIG_SYSCTL
Gao feng49d485a32012-05-28 21:04:18 +0000714 if (pn->ctl_table)
715 return 0;
716
717 pn->ctl_table = kmemdup(sctp_sysctl_table,
718 sizeof(sctp_sysctl_table),
719 GFP_KERNEL);
720 if (!pn->ctl_table)
721 return -ENOMEM;
722
723 pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
724 pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
725 pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
726 pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
727 pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
728 pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
729 pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
730#endif
731 return 0;
732}
733
Gao fengf42c4182012-06-21 04:36:46 +0000734static int sctp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
735 struct sctp_net *sn)
Gao feng49d485a32012-05-28 21:04:18 +0000736{
737#ifdef CONFIG_SYSCTL
738#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
Gao feng49d485a32012-05-28 21:04:18 +0000739 pn->ctl_compat_table = kmemdup(sctp_compat_sysctl_table,
740 sizeof(sctp_compat_sysctl_table),
741 GFP_KERNEL);
742 if (!pn->ctl_compat_table)
743 return -ENOMEM;
744
745 pn->ctl_compat_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
746 pn->ctl_compat_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
747 pn->ctl_compat_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
748 pn->ctl_compat_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
749 pn->ctl_compat_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
750 pn->ctl_compat_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
751 pn->ctl_compat_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
752#endif
753#endif
754 return 0;
755}
756
Gao fengf42c4182012-06-21 04:36:46 +0000757static int sctp_init_net(struct net *net, u_int16_t proto)
Gao feng49d485a32012-05-28 21:04:18 +0000758{
759 int ret;
760 struct sctp_net *sn = sctp_pernet(net);
Gao fengf42c4182012-06-21 04:36:46 +0000761 struct nf_proto_net *pn = &sn->pn;
Gao feng49d485a32012-05-28 21:04:18 +0000762
Gao fengf42c4182012-06-21 04:36:46 +0000763 if (!pn->users) {
764 int i;
Gao feng49d485a32012-05-28 21:04:18 +0000765
Gao fengf42c4182012-06-21 04:36:46 +0000766 for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
767 sn->timeouts[i] = sctp_timeouts[i];
Gao feng49d485a32012-05-28 21:04:18 +0000768 }
Gao fengf42c4182012-06-21 04:36:46 +0000769
770 if (proto == AF_INET) {
771 ret = sctp_kmemdup_compat_sysctl_table(pn, sn);
772 if (ret < 0)
773 return ret;
774
775 ret = sctp_kmemdup_sysctl_table(pn, sn);
776 if (ret < 0)
777 nf_ct_kfree_compat_sysctl_table(pn);
778 } else
779 ret = sctp_kmemdup_sysctl_table(pn, sn);
780
Gao feng49d485a32012-05-28 21:04:18 +0000781 return ret;
782}
783
Patrick McHardy61075af2007-07-14 20:48:19 -0700784static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100785 .l3proto = PF_INET,
786 .l4proto = IPPROTO_SCTP,
787 .name = "sctp",
788 .pkt_to_tuple = sctp_pkt_to_tuple,
789 .invert_tuple = sctp_invert_tuple,
790 .print_tuple = sctp_print_tuple,
791 .print_conntrack = sctp_print_conntrack,
792 .packet = sctp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100793 .get_timeouts = sctp_get_timeouts,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100794 .new = sctp_new,
795 .me = THIS_MODULE,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000796#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700797 .to_nlattr = sctp_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100798 .nlattr_size = sctp_nlattr_size,
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700799 .from_nlattr = nlattr_to_sctp,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800800 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100801 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800802 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
803 .nla_policy = nf_ct_port_nla_policy,
804#endif
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100805#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
806 .ctnl_timeout = {
807 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
808 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
809 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
810 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
811 .nla_policy = sctp_timeout_nla_policy,
812 },
813#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng49d485a32012-05-28 21:04:18 +0000814 .net_id = &sctp_net_id,
Gao fengf42c4182012-06-21 04:36:46 +0000815 .init_net = sctp_init_net,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100816};
817
Patrick McHardy61075af2007-07-14 20:48:19 -0700818static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100819 .l3proto = PF_INET6,
820 .l4proto = IPPROTO_SCTP,
821 .name = "sctp",
822 .pkt_to_tuple = sctp_pkt_to_tuple,
823 .invert_tuple = sctp_invert_tuple,
824 .print_tuple = sctp_print_tuple,
825 .print_conntrack = sctp_print_conntrack,
826 .packet = sctp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100827 .get_timeouts = sctp_get_timeouts,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100828 .new = sctp_new,
829 .me = THIS_MODULE,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000830#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700831 .to_nlattr = sctp_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100832 .nlattr_size = sctp_nlattr_size,
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700833 .from_nlattr = nlattr_to_sctp,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800834 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100835 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800836 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
837 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100838#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
839 .ctnl_timeout = {
840 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
841 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
842 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
843 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
844 .nla_policy = sctp_timeout_nla_policy,
845 },
846#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800847#endif
Gao feng49d485a32012-05-28 21:04:18 +0000848 .net_id = &sctp_net_id,
Gao fengf42c4182012-06-21 04:36:46 +0000849 .init_net = sctp_init_net,
Gao feng49d485a32012-05-28 21:04:18 +0000850};
851
852static int sctp_net_init(struct net *net)
853{
854 int ret = 0;
855
856 ret = nf_conntrack_l4proto_register(net,
857 &nf_conntrack_l4proto_sctp4);
858 if (ret < 0) {
859 pr_err("nf_conntrack_l4proto_sctp4 :protocol register failed.\n");
860 goto out;
861 }
862 ret = nf_conntrack_l4proto_register(net,
863 &nf_conntrack_l4proto_sctp6);
864 if (ret < 0) {
865 pr_err("nf_conntrack_l4proto_sctp6 :protocol register failed.\n");
866 goto cleanup_sctp4;
867 }
868 return 0;
869
870cleanup_sctp4:
871 nf_conntrack_l4proto_unregister(net,
872 &nf_conntrack_l4proto_sctp4);
873out:
874 return ret;
875}
876
877static void sctp_net_exit(struct net *net)
878{
879 nf_conntrack_l4proto_unregister(net,
880 &nf_conntrack_l4proto_sctp6);
881 nf_conntrack_l4proto_unregister(net,
882 &nf_conntrack_l4proto_sctp4);
883}
884
885static struct pernet_operations sctp_net_ops = {
886 .init = sctp_net_init,
887 .exit = sctp_net_exit,
888 .id = &sctp_net_id,
889 .size = sizeof(struct sctp_net),
Patrick McHardy933a41e2006-11-29 02:35:18 +0100890};
891
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800892static int __init nf_conntrack_proto_sctp_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800893{
Gao feng49d485a32012-05-28 21:04:18 +0000894 return register_pernet_subsys(&sctp_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800895}
896
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800897static void __exit nf_conntrack_proto_sctp_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800898{
Gao feng49d485a32012-05-28 21:04:18 +0000899 unregister_pernet_subsys(&sctp_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800900}
901
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800902module_init(nf_conntrack_proto_sctp_init);
903module_exit(nf_conntrack_proto_sctp_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800904
905MODULE_LICENSE("GPL");
906MODULE_AUTHOR("Kiran Kumar Immidi");
907MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP");
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800908MODULE_ALIAS("ip_conntrack_proto_sctp");