blob: 1314d33f6bcf4c59e0394569a055392cc9a57335 [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 *
Patrick McHardyf229f6c2013-04-06 15:24:29 +02004 * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com>
5 * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net>
6 *
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08007 * SCTP is defined in RFC 2960. References to various sections in this code
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08008 * are to this RFC.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08009 *
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013 */
14
15#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080016#include <linux/timer.h>
17#include <linux/netfilter.h>
18#include <linux/module.h>
19#include <linux/in.h>
20#include <linux/ip.h>
21#include <linux/sctp.h>
22#include <linux/string.h>
23#include <linux/seq_file.h>
Yasuyuki Kozakai40a839f2006-06-27 03:00:35 -070024#include <linux/spinlock.h>
25#include <linux/interrupt.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080026
27#include <net/netfilter/nf_conntrack.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010028#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010029#include <net/netfilter/nf_conntrack_ecache.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031/* 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
Jan Engelhardt12c33aa2008-04-14 11:15:54 +020036static const char *const sctp_conntrack_names[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080037 "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},
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300113/* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Can't have Stale cookie*/
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800114/* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA},/* 5.2.4 - Big TODO */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300115/* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Can't come in orig dir */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800116/* 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},
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300127/* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Can't come in reply dir */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800128/* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA},
129/* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL}
130 }
131};
132
Gao feng49d485a32012-05-28 21:04:18 +0000133static int sctp_net_id __read_mostly;
134struct sctp_net {
135 struct nf_proto_net pn;
136 unsigned int timeouts[SCTP_CONNTRACK_MAX];
137};
138
139static inline struct sctp_net *sctp_pernet(struct net *net)
140{
141 return net_generic(net, sctp_net_id);
142}
143
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200144static bool sctp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
145 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146{
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200147 const struct sctphdr *hp;
148 struct sctphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800150 /* Actually only need first 8 bytes. */
151 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
152 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200153 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800154
155 tuple->src.u.sctp.port = hp->source;
156 tuple->dst.u.sctp.port = hp->dest;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200157 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158}
159
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200160static bool sctp_invert_tuple(struct nf_conntrack_tuple *tuple,
161 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163 tuple->src.u.sctp.port = orig->dst.u.sctp.port;
164 tuple->dst.u.sctp.port = orig->src.u.sctp.port;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200165 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166}
167
168/* Print out the per-protocol part of the tuple. */
169static int sctp_print_tuple(struct seq_file *s,
170 const struct nf_conntrack_tuple *tuple)
171{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800172 return seq_printf(s, "sport=%hu dport=%hu ",
173 ntohs(tuple->src.u.sctp.port),
174 ntohs(tuple->dst.u.sctp.port));
175}
176
177/* Print out the private part of the conntrack. */
Patrick McHardy440f0d52009-06-10 14:32:47 +0200178static int sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800179{
180 enum sctp_conntrack state;
181
Patrick McHardy440f0d52009-06-10 14:32:47 +0200182 spin_lock_bh(&ct->lock);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800183 state = ct->proto.sctp.state;
Patrick McHardy440f0d52009-06-10 14:32:47 +0200184 spin_unlock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185
186 return seq_printf(s, "%s ", sctp_conntrack_names[state]);
187}
188
189#define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
Jan Engelhardte79ec502007-12-17 22:44:06 -0800190for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
191 (offset) < (skb)->len && \
192 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
193 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194
195/* Some validity checks to make sure the chunks are fine */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800196static int do_basic_checks(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197 const struct sk_buff *skb,
198 unsigned int dataoff,
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800199 unsigned long *map)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800200{
201 u_int32_t offset, count;
202 sctp_chunkhdr_t _sch, *sch;
203 int flag;
204
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205 flag = 0;
206
207 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700208 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209
Patrick McHardy5447d472008-01-14 23:45:48 -0800210 if (sch->type == SCTP_CID_INIT ||
211 sch->type == SCTP_CID_INIT_ACK ||
212 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213 flag = 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214
Patrick McHardye17df682006-05-02 23:23:07 +0200215 /*
216 * Cookie Ack/Echo chunks not the first OR
217 * Init / Init Ack / Shutdown compl chunks not the only chunks
218 * OR zero-length.
219 */
Patrick McHardy5447d472008-01-14 23:45:48 -0800220 if (((sch->type == SCTP_CID_COOKIE_ACK ||
221 sch->type == SCTP_CID_COOKIE_ECHO ||
222 flag) &&
223 count != 0) || !sch->length) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700224 pr_debug("Basic checks failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800225 return 1;
226 }
227
Patrick McHardy5447d472008-01-14 23:45:48 -0800228 if (map)
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800229 set_bit(sch->type, map);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230 }
231
Patrick McHardy0d537782007-07-07 22:39:38 -0700232 pr_debug("Basic checks passed\n");
Patrick McHardydd7271f2006-06-29 21:40:23 -0700233 return count == 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800234}
235
Patrick McHardyefe9f682008-01-14 23:47:09 -0800236static int sctp_new_state(enum ip_conntrack_dir dir,
237 enum sctp_conntrack cur_state,
238 int chunk_type)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239{
240 int i;
241
Patrick McHardy0d537782007-07-07 22:39:38 -0700242 pr_debug("Chunk type: %d\n", chunk_type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800243
244 switch (chunk_type) {
Patrick McHardy5447d472008-01-14 23:45:48 -0800245 case SCTP_CID_INIT:
246 pr_debug("SCTP_CID_INIT\n");
247 i = 0;
248 break;
249 case SCTP_CID_INIT_ACK:
250 pr_debug("SCTP_CID_INIT_ACK\n");
251 i = 1;
252 break;
253 case SCTP_CID_ABORT:
254 pr_debug("SCTP_CID_ABORT\n");
255 i = 2;
256 break;
257 case SCTP_CID_SHUTDOWN:
258 pr_debug("SCTP_CID_SHUTDOWN\n");
259 i = 3;
260 break;
261 case SCTP_CID_SHUTDOWN_ACK:
262 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
263 i = 4;
264 break;
265 case SCTP_CID_ERROR:
266 pr_debug("SCTP_CID_ERROR\n");
267 i = 5;
268 break;
269 case SCTP_CID_COOKIE_ECHO:
270 pr_debug("SCTP_CID_COOKIE_ECHO\n");
271 i = 6;
272 break;
273 case SCTP_CID_COOKIE_ACK:
274 pr_debug("SCTP_CID_COOKIE_ACK\n");
275 i = 7;
276 break;
277 case SCTP_CID_SHUTDOWN_COMPLETE:
278 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
279 i = 8;
280 break;
281 default:
282 /* Other chunks like DATA, SACK, HEARTBEAT and
283 its ACK do not cause a change in state */
284 pr_debug("Unknown chunk type, Will stay in %s\n",
285 sctp_conntrack_names[cur_state]);
286 return cur_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800287 }
288
Patrick McHardy0d537782007-07-07 22:39:38 -0700289 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
290 dir, sctp_conntrack_names[cur_state], chunk_type,
291 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292
293 return sctp_conntracks[dir][i][cur_state];
294}
295
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100296static unsigned int *sctp_get_timeouts(struct net *net)
297{
Gao feng49d485a32012-05-28 21:04:18 +0000298 return sctp_pernet(net)->timeouts;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100299}
300
Patrick McHardyb37e9332008-01-14 23:46:52 -0800301/* Returns verdict for packet, or -NF_ACCEPT for invalid. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800302static int sctp_packet(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800303 const struct sk_buff *skb,
304 unsigned int dataoff,
305 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200306 u_int8_t pf,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100307 unsigned int hooknum,
308 unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800309{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800310 enum sctp_conntrack new_state, old_state;
Patrick McHardy85288192008-01-14 23:46:37 -0800311 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200312 const struct sctphdr *sh;
313 struct sctphdr _sctph;
314 const struct sctp_chunkhdr *sch;
315 struct sctp_chunkhdr _sch;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800316 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800317 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800318
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800319 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
320 if (sh == NULL)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800321 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800322
Patrick McHardy112f35c2008-01-14 23:46:20 -0800323 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800324 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800325
326 /* Check the verification tag (Sec 8.5) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800327 if (!test_bit(SCTP_CID_INIT, map) &&
328 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
329 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
330 !test_bit(SCTP_CID_ABORT, map) &&
331 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
Patrick McHardy85288192008-01-14 23:46:37 -0800332 sh->vtag != ct->proto.sctp.vtag[dir]) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700333 pr_debug("Verification tag check failed\n");
Patrick McHardyb37e9332008-01-14 23:46:52 -0800334 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800335 }
336
Patrick McHardy328bd892008-11-24 13:44:55 +0100337 old_state = new_state = SCTP_CONNTRACK_NONE;
Patrick McHardy440f0d52009-06-10 14:32:47 +0200338 spin_lock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800339 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800340 /* Special cases of Verification tag check (Sec 8.5.1) */
341 if (sch->type == SCTP_CID_INIT) {
342 /* Sec 8.5.1 (A) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800343 if (sh->vtag != 0)
344 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800345 } else if (sch->type == SCTP_CID_ABORT) {
346 /* Sec 8.5.1 (B) */
Patrick McHardy85288192008-01-14 23:46:37 -0800347 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
Patrick McHardyb37e9332008-01-14 23:46:52 -0800348 sh->vtag != ct->proto.sctp.vtag[!dir])
349 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800350 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
351 /* Sec 8.5.1 (C) */
Patrick McHardy85288192008-01-14 23:46:37 -0800352 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
353 sh->vtag != ct->proto.sctp.vtag[!dir] &&
Patrick McHardy9b1c2cf2008-01-14 23:48:02 -0800354 sch->flags & SCTP_CHUNK_FLAG_T)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800355 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800356 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
357 /* Sec 8.5.1 (D) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800358 if (sh->vtag != ct->proto.sctp.vtag[dir])
359 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800360 }
361
Patrick McHardyefe9f682008-01-14 23:47:09 -0800362 old_state = ct->proto.sctp.state;
363 new_state = sctp_new_state(dir, old_state, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800364
365 /* Invalid */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800366 if (new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700367 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
368 "conntrack=%u\n",
Patrick McHardyefe9f682008-01-14 23:47:09 -0800369 dir, sch->type, old_state);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800370 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800371 }
372
373 /* If it is an INIT or an INIT ACK note down the vtag */
Patrick McHardy5447d472008-01-14 23:45:48 -0800374 if (sch->type == SCTP_CID_INIT ||
375 sch->type == SCTP_CID_INIT_ACK) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800376 sctp_inithdr_t _inithdr, *ih;
377
378 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800379 sizeof(_inithdr), &_inithdr);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800380 if (ih == NULL)
381 goto out_unlock;
Patrick McHardy0d537782007-07-07 22:39:38 -0700382 pr_debug("Setting vtag %x for dir %d\n",
Patrick McHardy85288192008-01-14 23:46:37 -0800383 ih->init_tag, !dir);
384 ct->proto.sctp.vtag[!dir] = ih->init_tag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800385 }
386
Patrick McHardyefe9f682008-01-14 23:47:09 -0800387 ct->proto.sctp.state = new_state;
388 if (old_state != new_state)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200389 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800390 }
Patrick McHardy440f0d52009-06-10 14:32:47 +0200391 spin_unlock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100393 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800394
Patrick McHardyefe9f682008-01-14 23:47:09 -0800395 if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
Patrick McHardy85288192008-01-14 23:46:37 -0800396 dir == IP_CT_DIR_REPLY &&
Patrick McHardyefe9f682008-01-14 23:47:09 -0800397 new_state == SCTP_CONNTRACK_ESTABLISHED) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700398 pr_debug("Setting assured bit\n");
Patrick McHardy112f35c2008-01-14 23:46:20 -0800399 set_bit(IPS_ASSURED_BIT, &ct->status);
Patrick McHardy858b31332010-02-03 13:48:53 +0100400 nf_conntrack_event_cache(IPCT_ASSURED, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401 }
402
403 return NF_ACCEPT;
Patrick McHardyb37e9332008-01-14 23:46:52 -0800404
405out_unlock:
Patrick McHardy440f0d52009-06-10 14:32:47 +0200406 spin_unlock_bh(&ct->lock);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800407out:
408 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409}
410
411/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200412static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100413 unsigned int dataoff, unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800414{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800415 enum sctp_conntrack new_state;
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200416 const struct sctphdr *sh;
417 struct sctphdr _sctph;
418 const struct sctp_chunkhdr *sch;
419 struct sctp_chunkhdr _sch;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800420 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800421 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800422
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
424 if (sh == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200425 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800426
Patrick McHardy112f35c2008-01-14 23:46:20 -0800427 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200428 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800429
430 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800431 if (test_bit(SCTP_CID_ABORT, map) ||
432 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
433 test_bit(SCTP_CID_COOKIE_ACK, map))
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200434 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800435
Changli Gaoe5fc9e72010-11-12 17:33:17 +0100436 memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
Patrick McHardyefe9f682008-01-14 23:47:09 -0800437 new_state = SCTP_CONNTRACK_MAX;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800438 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
439 /* Don't need lock here: this conntrack not in circulation yet */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800440 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
441 SCTP_CONNTRACK_NONE, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800442
443 /* Invalid: delete conntrack */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800444 if (new_state == SCTP_CONNTRACK_NONE ||
445 new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700446 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200447 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800448 }
449
450 /* Copy the vtag into the state info */
451 if (sch->type == SCTP_CID_INIT) {
452 if (sh->vtag == 0) {
453 sctp_inithdr_t _inithdr, *ih;
454
455 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800456 sizeof(_inithdr), &_inithdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800457 if (ih == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200458 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459
Patrick McHardy0d537782007-07-07 22:39:38 -0700460 pr_debug("Setting vtag %x for new conn\n",
461 ih->init_tag);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462
Patrick McHardy112f35c2008-01-14 23:46:20 -0800463 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800464 ih->init_tag;
465 } else {
466 /* Sec 8.5.1 (A) */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200467 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468 }
469 }
470 /* If it is a shutdown ack OOTB packet, we expect a return
471 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
472 else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700473 pr_debug("Setting vtag %x for new conn OOTB\n",
474 sh->vtag);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800475 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800476 }
477
Patrick McHardyefe9f682008-01-14 23:47:09 -0800478 ct->proto.sctp.state = new_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800479 }
480
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200481 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482}
483
Igor Maravićc0cd1152011-12-12 02:58:24 +0000484#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700485
486#include <linux/netfilter/nfnetlink.h>
487#include <linux/netfilter/nfnetlink_conntrack.h>
488
489static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
Patrick McHardy440f0d52009-06-10 14:32:47 +0200490 struct nf_conn *ct)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700491{
492 struct nlattr *nest_parms;
493
Patrick McHardy440f0d52009-06-10 14:32:47 +0200494 spin_lock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700495 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
496 if (!nest_parms)
497 goto nla_put_failure;
498
David S. Miller5e8d1eb2012-04-01 18:51:07 -0400499 if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
500 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
501 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
502 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
503 ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
504 goto nla_put_failure;
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700505
Patrick McHardy440f0d52009-06-10 14:32:47 +0200506 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700507
508 nla_nest_end(skb, nest_parms);
509
510 return 0;
511
512nla_put_failure:
Patrick McHardy440f0d52009-06-10 14:32:47 +0200513 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700514 return -1;
515}
516
517static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
518 [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
519 [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
520 [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
521};
522
523static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
524{
525 struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
526 struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
527 int err;
528
529 /* updates may not contain the internal protocol info, skip parsing */
530 if (!attr)
531 return 0;
532
533 err = nla_parse_nested(tb,
534 CTA_PROTOINFO_SCTP_MAX,
535 attr,
536 sctp_nla_policy);
537 if (err < 0)
538 return err;
539
540 if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
541 !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
542 !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
543 return -EINVAL;
544
Patrick McHardy440f0d52009-06-10 14:32:47 +0200545 spin_lock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700546 ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
547 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
Patrick McHardy5547cd02008-07-21 10:03:49 -0700548 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700549 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Patrick McHardy5547cd02008-07-21 10:03:49 -0700550 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
Patrick McHardy440f0d52009-06-10 14:32:47 +0200551 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700552
553 return 0;
554}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100555
556static int sctp_nlattr_size(void)
557{
558 return nla_total_size(0) /* CTA_PROTOINFO_SCTP */
559 + nla_policy_len(sctp_nla_policy, CTA_PROTOINFO_SCTP_MAX + 1);
560}
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700561#endif
562
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100563#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
564
565#include <linux/netfilter/nfnetlink.h>
566#include <linux/netfilter/nfnetlink_cttimeout.h>
567
Gao feng8264deb2012-05-28 21:04:23 +0000568static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
569 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100570{
571 unsigned int *timeouts = data;
Gao feng8264deb2012-05-28 21:04:23 +0000572 struct sctp_net *sn = sctp_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100573 int i;
574
575 /* set default SCTP timeouts. */
576 for (i=0; i<SCTP_CONNTRACK_MAX; i++)
Gao feng8264deb2012-05-28 21:04:23 +0000577 timeouts[i] = sn->timeouts[i];
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100578
579 /* there's a 1:1 mapping between attributes and protocol states. */
580 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
581 if (tb[i]) {
582 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
583 }
584 }
585 return 0;
586}
587
588static int
589sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
590{
591 const unsigned int *timeouts = data;
592 int i;
593
David S. Miller5e8d1eb2012-04-01 18:51:07 -0400594 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
595 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
596 goto nla_put_failure;
597 }
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100598 return 0;
599
600nla_put_failure:
601 return -ENOSPC;
602}
603
604static const struct nla_policy
605sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
606 [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
607 [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
608 [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
609 [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
610 [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
611 [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
612 [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
613};
614#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
615
616
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800617#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100618static struct ctl_table sctp_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800619 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800620 .procname = "nf_conntrack_sctp_timeout_closed",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800621 .maxlen = sizeof(unsigned int),
622 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800623 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800624 },
625 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800626 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800627 .maxlen = sizeof(unsigned int),
628 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800629 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800630 },
631 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800632 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800633 .maxlen = sizeof(unsigned int),
634 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800635 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800636 },
637 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800638 .procname = "nf_conntrack_sctp_timeout_established",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800639 .maxlen = sizeof(unsigned int),
640 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800641 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800642 },
643 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800644 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800645 .maxlen = sizeof(unsigned int),
646 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800647 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800648 },
649 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800650 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800651 .maxlen = sizeof(unsigned int),
652 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800653 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800654 },
655 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800656 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800657 .maxlen = sizeof(unsigned int),
658 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800659 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800660 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800661 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800662};
Patrick McHardya999e682006-11-29 02:35:20 +0100663
664#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
665static struct ctl_table sctp_compat_sysctl_table[] = {
666 {
Patrick McHardya999e682006-11-29 02:35:20 +0100667 .procname = "ip_conntrack_sctp_timeout_closed",
Patrick McHardya999e682006-11-29 02:35:20 +0100668 .maxlen = sizeof(unsigned int),
669 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800670 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100671 },
672 {
Patrick McHardya999e682006-11-29 02:35:20 +0100673 .procname = "ip_conntrack_sctp_timeout_cookie_wait",
Patrick McHardya999e682006-11-29 02:35:20 +0100674 .maxlen = sizeof(unsigned int),
675 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800676 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100677 },
678 {
Patrick McHardya999e682006-11-29 02:35:20 +0100679 .procname = "ip_conntrack_sctp_timeout_cookie_echoed",
Patrick McHardya999e682006-11-29 02:35:20 +0100680 .maxlen = sizeof(unsigned int),
681 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800682 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100683 },
684 {
Patrick McHardya999e682006-11-29 02:35:20 +0100685 .procname = "ip_conntrack_sctp_timeout_established",
Patrick McHardya999e682006-11-29 02:35:20 +0100686 .maxlen = sizeof(unsigned int),
687 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800688 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100689 },
690 {
Patrick McHardya999e682006-11-29 02:35:20 +0100691 .procname = "ip_conntrack_sctp_timeout_shutdown_sent",
Patrick McHardya999e682006-11-29 02:35:20 +0100692 .maxlen = sizeof(unsigned int),
693 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800694 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100695 },
696 {
Patrick McHardya999e682006-11-29 02:35:20 +0100697 .procname = "ip_conntrack_sctp_timeout_shutdown_recd",
Patrick McHardya999e682006-11-29 02:35:20 +0100698 .maxlen = sizeof(unsigned int),
699 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800700 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100701 },
702 {
Patrick McHardya999e682006-11-29 02:35:20 +0100703 .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent",
Patrick McHardya999e682006-11-29 02:35:20 +0100704 .maxlen = sizeof(unsigned int),
705 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800706 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100707 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800708 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100709};
710#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800711#endif
712
Gao fengf42c4182012-06-21 04:36:46 +0000713static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
714 struct sctp_net *sn)
Gao feng49d485a32012-05-28 21:04:18 +0000715{
716#ifdef CONFIG_SYSCTL
Gao feng49d485a32012-05-28 21:04:18 +0000717 if (pn->ctl_table)
718 return 0;
719
720 pn->ctl_table = kmemdup(sctp_sysctl_table,
721 sizeof(sctp_sysctl_table),
722 GFP_KERNEL);
723 if (!pn->ctl_table)
724 return -ENOMEM;
725
726 pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
727 pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
728 pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
729 pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
730 pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
731 pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
732 pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
733#endif
734 return 0;
735}
736
Gao fengf42c4182012-06-21 04:36:46 +0000737static int sctp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
738 struct sctp_net *sn)
Gao feng49d485a32012-05-28 21:04:18 +0000739{
740#ifdef CONFIG_SYSCTL
741#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
Gao feng49d485a32012-05-28 21:04:18 +0000742 pn->ctl_compat_table = kmemdup(sctp_compat_sysctl_table,
743 sizeof(sctp_compat_sysctl_table),
744 GFP_KERNEL);
745 if (!pn->ctl_compat_table)
746 return -ENOMEM;
747
748 pn->ctl_compat_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
749 pn->ctl_compat_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
750 pn->ctl_compat_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
751 pn->ctl_compat_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
752 pn->ctl_compat_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
753 pn->ctl_compat_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
754 pn->ctl_compat_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
755#endif
756#endif
757 return 0;
758}
759
Gao fengf42c4182012-06-21 04:36:46 +0000760static int sctp_init_net(struct net *net, u_int16_t proto)
Gao feng49d485a32012-05-28 21:04:18 +0000761{
762 int ret;
763 struct sctp_net *sn = sctp_pernet(net);
Gao fengf42c4182012-06-21 04:36:46 +0000764 struct nf_proto_net *pn = &sn->pn;
Gao feng49d485a32012-05-28 21:04:18 +0000765
Gao fengf42c4182012-06-21 04:36:46 +0000766 if (!pn->users) {
767 int i;
Gao feng49d485a32012-05-28 21:04:18 +0000768
Gao fengf42c4182012-06-21 04:36:46 +0000769 for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
770 sn->timeouts[i] = sctp_timeouts[i];
Gao feng49d485a32012-05-28 21:04:18 +0000771 }
Gao fengf42c4182012-06-21 04:36:46 +0000772
773 if (proto == AF_INET) {
774 ret = sctp_kmemdup_compat_sysctl_table(pn, sn);
775 if (ret < 0)
776 return ret;
777
778 ret = sctp_kmemdup_sysctl_table(pn, sn);
779 if (ret < 0)
780 nf_ct_kfree_compat_sysctl_table(pn);
781 } else
782 ret = sctp_kmemdup_sysctl_table(pn, sn);
783
Gao feng49d485a32012-05-28 21:04:18 +0000784 return ret;
785}
786
Patrick McHardy61075af2007-07-14 20:48:19 -0700787static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100788 .l3proto = PF_INET,
789 .l4proto = IPPROTO_SCTP,
790 .name = "sctp",
791 .pkt_to_tuple = sctp_pkt_to_tuple,
792 .invert_tuple = sctp_invert_tuple,
793 .print_tuple = sctp_print_tuple,
794 .print_conntrack = sctp_print_conntrack,
795 .packet = sctp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100796 .get_timeouts = sctp_get_timeouts,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100797 .new = sctp_new,
798 .me = THIS_MODULE,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000799#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700800 .to_nlattr = sctp_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100801 .nlattr_size = sctp_nlattr_size,
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700802 .from_nlattr = nlattr_to_sctp,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800803 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100804 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800805 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
806 .nla_policy = nf_ct_port_nla_policy,
807#endif
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100808#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
809 .ctnl_timeout = {
810 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
811 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
812 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
813 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
814 .nla_policy = sctp_timeout_nla_policy,
815 },
816#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng49d485a32012-05-28 21:04:18 +0000817 .net_id = &sctp_net_id,
Gao fengf42c4182012-06-21 04:36:46 +0000818 .init_net = sctp_init_net,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100819};
820
Patrick McHardy61075af2007-07-14 20:48:19 -0700821static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100822 .l3proto = PF_INET6,
823 .l4proto = IPPROTO_SCTP,
824 .name = "sctp",
825 .pkt_to_tuple = sctp_pkt_to_tuple,
826 .invert_tuple = sctp_invert_tuple,
827 .print_tuple = sctp_print_tuple,
828 .print_conntrack = sctp_print_conntrack,
829 .packet = sctp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100830 .get_timeouts = sctp_get_timeouts,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100831 .new = sctp_new,
832 .me = THIS_MODULE,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000833#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700834 .to_nlattr = sctp_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100835 .nlattr_size = sctp_nlattr_size,
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700836 .from_nlattr = nlattr_to_sctp,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800837 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100838 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800839 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
840 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100841#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
842 .ctnl_timeout = {
843 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
844 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
845 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
846 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
847 .nla_policy = sctp_timeout_nla_policy,
848 },
849#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800850#endif
Gao feng49d485a32012-05-28 21:04:18 +0000851 .net_id = &sctp_net_id,
Gao fengf42c4182012-06-21 04:36:46 +0000852 .init_net = sctp_init_net,
Gao feng49d485a32012-05-28 21:04:18 +0000853};
854
855static int sctp_net_init(struct net *net)
856{
857 int ret = 0;
858
Gao fengc296bb42013-01-23 12:51:10 +0100859 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000860 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100861 pr_err("nf_conntrack_sctp4: pernet registration failed.\n");
Gao feng49d485a32012-05-28 21:04:18 +0000862 goto out;
863 }
Gao fengc296bb42013-01-23 12:51:10 +0100864 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp6);
Gao feng49d485a32012-05-28 21:04:18 +0000865 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100866 pr_err("nf_conntrack_sctp6: pernet registration failed.\n");
Gao feng49d485a32012-05-28 21:04:18 +0000867 goto cleanup_sctp4;
868 }
869 return 0;
870
871cleanup_sctp4:
Gao fengc296bb42013-01-23 12:51:10 +0100872 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000873out:
874 return ret;
875}
876
877static void sctp_net_exit(struct net *net)
878{
Gao fengc296bb42013-01-23 12:51:10 +0100879 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp6);
880 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000881}
882
883static struct pernet_operations sctp_net_ops = {
884 .init = sctp_net_init,
885 .exit = sctp_net_exit,
886 .id = &sctp_net_id,
887 .size = sizeof(struct sctp_net),
Patrick McHardy933a41e2006-11-29 02:35:18 +0100888};
889
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800890static int __init nf_conntrack_proto_sctp_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800891{
Gao fengc296bb42013-01-23 12:51:10 +0100892 int ret;
893
Gao feng0d98da52013-03-07 17:20:46 +0000894 ret = register_pernet_subsys(&sctp_net_ops);
895 if (ret < 0)
896 goto out_pernet;
897
Gao fengc296bb42013-01-23 12:51:10 +0100898 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp4);
899 if (ret < 0)
900 goto out_sctp4;
901
902 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp6);
903 if (ret < 0)
904 goto out_sctp6;
905
Gao fengc296bb42013-01-23 12:51:10 +0100906 return 0;
Gao fengc296bb42013-01-23 12:51:10 +0100907out_sctp6:
908 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
909out_sctp4:
Gao feng0d98da52013-03-07 17:20:46 +0000910 unregister_pernet_subsys(&sctp_net_ops);
911out_pernet:
Gao fengc296bb42013-01-23 12:51:10 +0100912 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800913}
914
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800915static void __exit nf_conntrack_proto_sctp_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800916{
Gao fengc296bb42013-01-23 12:51:10 +0100917 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp6);
918 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000919 unregister_pernet_subsys(&sctp_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800920}
921
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800922module_init(nf_conntrack_proto_sctp_init);
923module_exit(nf_conntrack_proto_sctp_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800924
925MODULE_LICENSE("GPL");
926MODULE_AUTHOR("Kiran Kumar Immidi");
927MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP");
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800928MODULE_ALIAS("ip_conntrack_proto_sctp");