blob: 9578a7c371ef2ce04f0a7b10c694b2533e29bd3a [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",
Michal Kubečekd7ee3512015-07-17 16:17:56 +020045 "HEARTBEAT_SENT",
46 "HEARTBEAT_ACKED",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047};
48
49#define SECS * HZ
50#define MINS * 60 SECS
51#define HOURS * 60 MINS
52#define DAYS * 24 HOURS
53
Patrick McHardy86c0bf42008-01-14 23:48:17 -080054static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] __read_mostly = {
55 [SCTP_CONNTRACK_CLOSED] = 10 SECS,
56 [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS,
57 [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS,
58 [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS,
59 [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000,
60 [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000,
61 [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS,
Michal Kubečekd7ee3512015-07-17 16:17:56 +020062 [SCTP_CONNTRACK_HEARTBEAT_SENT] = 30 SECS,
63 [SCTP_CONNTRACK_HEARTBEAT_ACKED] = 210 SECS,
Patrick McHardy86c0bf42008-01-14 23:48:17 -080064};
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080065
66#define sNO SCTP_CONNTRACK_NONE
67#define sCL SCTP_CONNTRACK_CLOSED
68#define sCW SCTP_CONNTRACK_COOKIE_WAIT
69#define sCE SCTP_CONNTRACK_COOKIE_ECHOED
70#define sES SCTP_CONNTRACK_ESTABLISHED
71#define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
72#define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
73#define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
Michal Kubečekd7ee3512015-07-17 16:17:56 +020074#define sHS SCTP_CONNTRACK_HEARTBEAT_SENT
75#define sHA SCTP_CONNTRACK_HEARTBEAT_ACKED
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080076#define sIV SCTP_CONNTRACK_MAX
77
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080078/*
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080079 These are the descriptions of the states:
80
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080081NOTE: These state names are tantalizingly similar to the states of an
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080082SCTP endpoint. But the interpretation of the states is a little different,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080083considering that these are the states of the connection and not of an end
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080084point. Please note the subtleties. -Kiran
85
86NONE - Nothing so far.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080087COOKIE WAIT - We have seen an INIT chunk in the original direction, or also
88 an INIT_ACK chunk in the reply direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080089COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction.
90ESTABLISHED - We have seen a COOKIE_ACK in the reply direction.
91SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction.
92SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin.
93SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080094 to that of the SHUTDOWN chunk.
95CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
96 the SHUTDOWN chunk. Connection is closed.
Michal Kubečekd7ee3512015-07-17 16:17:56 +020097HEARTBEAT_SENT - We have seen a HEARTBEAT in a new flow.
98HEARTBEAT_ACKED - We have seen a HEARTBEAT-ACK in the direction opposite to
99 that of the HEARTBEAT chunk. Secondary connection is
100 established.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800101*/
102
103/* TODO
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800104 - I have assumed that the first INIT is in the original direction.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800105 This messes things when an INIT comes in the reply direction in CLOSED
106 state.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800107 - Check the error type in the reply dir before transitioning from
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800108cookie echoed to closed.
109 - Sec 5.2.4 of RFC 2960
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200110 - Full Multi Homing support.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800111*/
112
113/* SCTP conntrack state transitions */
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200114static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800115 {
116/* ORIGINAL */
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200117/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
118/* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA},
119/* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},
120/* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
121/* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS},
122/* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA, sHA},
123/* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't have Stale cookie*/
124/* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* 5.2.4 - Big TODO */
125/* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't come in orig dir */
126/* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL, sHA},
127/* heartbeat */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
128/* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800129 },
130 {
131/* REPLY */
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200132/* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
133/* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */
134/* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},
135/* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL},
136/* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR},
137/* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA},
138/* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV, sHA},
139/* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* Can't come in reply dir */
140/* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV, sHA},
141/* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV, sHA},
142/* heartbeat */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
143/* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHA, sHA}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800144 }
145};
146
Gao feng49d485a32012-05-28 21:04:18 +0000147static int sctp_net_id __read_mostly;
148struct sctp_net {
149 struct nf_proto_net pn;
150 unsigned int timeouts[SCTP_CONNTRACK_MAX];
151};
152
153static inline struct sctp_net *sctp_pernet(struct net *net)
154{
155 return net_generic(net, sctp_net_id);
156}
157
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200158static bool sctp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500159 struct net *net, struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160{
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200161 const struct sctphdr *hp;
162 struct sctphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164 /* Actually only need first 8 bytes. */
165 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
166 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200167 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800168
169 tuple->src.u.sctp.port = hp->source;
170 tuple->dst.u.sctp.port = hp->dest;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200171 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800172}
173
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200174static bool sctp_invert_tuple(struct nf_conntrack_tuple *tuple,
175 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800177 tuple->src.u.sctp.port = orig->dst.u.sctp.port;
178 tuple->dst.u.sctp.port = orig->src.u.sctp.port;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200179 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180}
181
182/* Print out the per-protocol part of the tuple. */
Joe Perches824f1fb2014-09-29 16:08:22 -0700183static void sctp_print_tuple(struct seq_file *s,
184 const struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185{
Joe Perches824f1fb2014-09-29 16:08:22 -0700186 seq_printf(s, "sport=%hu dport=%hu ",
187 ntohs(tuple->src.u.sctp.port),
188 ntohs(tuple->dst.u.sctp.port));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189}
190
191/* Print out the private part of the conntrack. */
Steven Rostedt (Red Hat)37246a52014-10-27 16:02:47 -0400192static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800193{
194 enum sctp_conntrack state;
195
Patrick McHardy440f0d52009-06-10 14:32:47 +0200196 spin_lock_bh(&ct->lock);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800197 state = ct->proto.sctp.state;
Patrick McHardy440f0d52009-06-10 14:32:47 +0200198 spin_unlock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800199
Steven Rostedt (Red Hat)37246a52014-10-27 16:02:47 -0400200 seq_printf(s, "%s ", sctp_conntrack_names[state]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201}
202
203#define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
Jan Engelhardte79ec502007-12-17 22:44:06 -0800204for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
205 (offset) < (skb)->len && \
206 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
207 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800208
209/* Some validity checks to make sure the chunks are fine */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800210static int do_basic_checks(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211 const struct sk_buff *skb,
212 unsigned int dataoff,
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800213 unsigned long *map)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214{
215 u_int32_t offset, count;
216 sctp_chunkhdr_t _sch, *sch;
217 int flag;
218
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800219 flag = 0;
220
221 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700222 pr_debug("Chunk Num: %d Type: %d\n", count, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223
Patrick McHardy5447d472008-01-14 23:45:48 -0800224 if (sch->type == SCTP_CID_INIT ||
225 sch->type == SCTP_CID_INIT_ACK ||
226 sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800227 flag = 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228
Patrick McHardye17df682006-05-02 23:23:07 +0200229 /*
230 * Cookie Ack/Echo chunks not the first OR
231 * Init / Init Ack / Shutdown compl chunks not the only chunks
232 * OR zero-length.
233 */
Patrick McHardy5447d472008-01-14 23:45:48 -0800234 if (((sch->type == SCTP_CID_COOKIE_ACK ||
235 sch->type == SCTP_CID_COOKIE_ECHO ||
236 flag) &&
237 count != 0) || !sch->length) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700238 pr_debug("Basic checks failed\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800239 return 1;
240 }
241
Patrick McHardy5447d472008-01-14 23:45:48 -0800242 if (map)
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800243 set_bit(sch->type, map);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800244 }
245
Patrick McHardy0d537782007-07-07 22:39:38 -0700246 pr_debug("Basic checks passed\n");
Patrick McHardydd7271f2006-06-29 21:40:23 -0700247 return count == 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800248}
249
Patrick McHardyefe9f682008-01-14 23:47:09 -0800250static int sctp_new_state(enum ip_conntrack_dir dir,
251 enum sctp_conntrack cur_state,
252 int chunk_type)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800253{
254 int i;
255
Patrick McHardy0d537782007-07-07 22:39:38 -0700256 pr_debug("Chunk type: %d\n", chunk_type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257
258 switch (chunk_type) {
Patrick McHardy5447d472008-01-14 23:45:48 -0800259 case SCTP_CID_INIT:
260 pr_debug("SCTP_CID_INIT\n");
261 i = 0;
262 break;
263 case SCTP_CID_INIT_ACK:
264 pr_debug("SCTP_CID_INIT_ACK\n");
265 i = 1;
266 break;
267 case SCTP_CID_ABORT:
268 pr_debug("SCTP_CID_ABORT\n");
269 i = 2;
270 break;
271 case SCTP_CID_SHUTDOWN:
272 pr_debug("SCTP_CID_SHUTDOWN\n");
273 i = 3;
274 break;
275 case SCTP_CID_SHUTDOWN_ACK:
276 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
277 i = 4;
278 break;
279 case SCTP_CID_ERROR:
280 pr_debug("SCTP_CID_ERROR\n");
281 i = 5;
282 break;
283 case SCTP_CID_COOKIE_ECHO:
284 pr_debug("SCTP_CID_COOKIE_ECHO\n");
285 i = 6;
286 break;
287 case SCTP_CID_COOKIE_ACK:
288 pr_debug("SCTP_CID_COOKIE_ACK\n");
289 i = 7;
290 break;
291 case SCTP_CID_SHUTDOWN_COMPLETE:
292 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
293 i = 8;
294 break;
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200295 case SCTP_CID_HEARTBEAT:
296 pr_debug("SCTP_CID_HEARTBEAT");
297 i = 9;
298 break;
299 case SCTP_CID_HEARTBEAT_ACK:
300 pr_debug("SCTP_CID_HEARTBEAT_ACK");
301 i = 10;
302 break;
Patrick McHardy5447d472008-01-14 23:45:48 -0800303 default:
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200304 /* Other chunks like DATA or SACK do not change the state */
Patrick McHardy5447d472008-01-14 23:45:48 -0800305 pr_debug("Unknown chunk type, Will stay in %s\n",
306 sctp_conntrack_names[cur_state]);
307 return cur_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800308 }
309
Patrick McHardy0d537782007-07-07 22:39:38 -0700310 pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
311 dir, sctp_conntrack_names[cur_state], chunk_type,
312 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800313
314 return sctp_conntracks[dir][i][cur_state];
315}
316
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100317static unsigned int *sctp_get_timeouts(struct net *net)
318{
Gao feng49d485a32012-05-28 21:04:18 +0000319 return sctp_pernet(net)->timeouts;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100320}
321
Patrick McHardyb37e9332008-01-14 23:46:52 -0800322/* Returns verdict for packet, or -NF_ACCEPT for invalid. */
Patrick McHardy112f35c2008-01-14 23:46:20 -0800323static int sctp_packet(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800324 const struct sk_buff *skb,
325 unsigned int dataoff,
326 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200327 u_int8_t pf,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100328 unsigned int hooknum,
329 unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800330{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800331 enum sctp_conntrack new_state, old_state;
Patrick McHardy85288192008-01-14 23:46:37 -0800332 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200333 const struct sctphdr *sh;
334 struct sctphdr _sctph;
335 const struct sctp_chunkhdr *sch;
336 struct sctp_chunkhdr _sch;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800337 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800338 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800339
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800340 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
341 if (sh == NULL)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800342 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800343
Patrick McHardy112f35c2008-01-14 23:46:20 -0800344 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800345 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346
347 /* Check the verification tag (Sec 8.5) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800348 if (!test_bit(SCTP_CID_INIT, map) &&
349 !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
350 !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
351 !test_bit(SCTP_CID_ABORT, map) &&
352 !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200353 !test_bit(SCTP_CID_HEARTBEAT, map) &&
354 !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
Patrick McHardy85288192008-01-14 23:46:37 -0800355 sh->vtag != ct->proto.sctp.vtag[dir]) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700356 pr_debug("Verification tag check failed\n");
Patrick McHardyb37e9332008-01-14 23:46:52 -0800357 goto out;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800358 }
359
Patrick McHardy328bd892008-11-24 13:44:55 +0100360 old_state = new_state = SCTP_CONNTRACK_NONE;
Patrick McHardy440f0d52009-06-10 14:32:47 +0200361 spin_lock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800362 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800363 /* Special cases of Verification tag check (Sec 8.5.1) */
364 if (sch->type == SCTP_CID_INIT) {
365 /* Sec 8.5.1 (A) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800366 if (sh->vtag != 0)
367 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800368 } else if (sch->type == SCTP_CID_ABORT) {
369 /* Sec 8.5.1 (B) */
Patrick McHardy85288192008-01-14 23:46:37 -0800370 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
Patrick McHardyb37e9332008-01-14 23:46:52 -0800371 sh->vtag != ct->proto.sctp.vtag[!dir])
372 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800373 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
374 /* Sec 8.5.1 (C) */
Patrick McHardy85288192008-01-14 23:46:37 -0800375 if (sh->vtag != ct->proto.sctp.vtag[dir] &&
376 sh->vtag != ct->proto.sctp.vtag[!dir] &&
Patrick McHardy9b1c2cf2008-01-14 23:48:02 -0800377 sch->flags & SCTP_CHUNK_FLAG_T)
Patrick McHardyb37e9332008-01-14 23:46:52 -0800378 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800379 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
380 /* Sec 8.5.1 (D) */
Patrick McHardyb37e9332008-01-14 23:46:52 -0800381 if (sh->vtag != ct->proto.sctp.vtag[dir])
382 goto out_unlock;
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200383 } else if (sch->type == SCTP_CID_HEARTBEAT ||
384 sch->type == SCTP_CID_HEARTBEAT_ACK) {
385 if (ct->proto.sctp.vtag[dir] == 0) {
386 pr_debug("Setting vtag %x for dir %d\n",
387 sh->vtag, dir);
388 ct->proto.sctp.vtag[dir] = sh->vtag;
389 } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
390 pr_debug("Verification tag check failed\n");
391 goto out_unlock;
392 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800393 }
394
Patrick McHardyefe9f682008-01-14 23:47:09 -0800395 old_state = ct->proto.sctp.state;
396 new_state = sctp_new_state(dir, old_state, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800397
398 /* Invalid */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800399 if (new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700400 pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
401 "conntrack=%u\n",
Patrick McHardyefe9f682008-01-14 23:47:09 -0800402 dir, sch->type, old_state);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800403 goto out_unlock;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404 }
405
406 /* If it is an INIT or an INIT ACK note down the vtag */
Patrick McHardy5447d472008-01-14 23:45:48 -0800407 if (sch->type == SCTP_CID_INIT ||
408 sch->type == SCTP_CID_INIT_ACK) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409 sctp_inithdr_t _inithdr, *ih;
410
411 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800412 sizeof(_inithdr), &_inithdr);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800413 if (ih == NULL)
414 goto out_unlock;
Patrick McHardy0d537782007-07-07 22:39:38 -0700415 pr_debug("Setting vtag %x for dir %d\n",
Patrick McHardy85288192008-01-14 23:46:37 -0800416 ih->init_tag, !dir);
417 ct->proto.sctp.vtag[!dir] = ih->init_tag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800418 }
419
Patrick McHardyefe9f682008-01-14 23:47:09 -0800420 ct->proto.sctp.state = new_state;
421 if (old_state != new_state)
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200422 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800423 }
Patrick McHardy440f0d52009-06-10 14:32:47 +0200424 spin_unlock_bh(&ct->lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800425
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100426 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800427
Patrick McHardyefe9f682008-01-14 23:47:09 -0800428 if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
Patrick McHardy85288192008-01-14 23:46:37 -0800429 dir == IP_CT_DIR_REPLY &&
Patrick McHardyefe9f682008-01-14 23:47:09 -0800430 new_state == SCTP_CONNTRACK_ESTABLISHED) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700431 pr_debug("Setting assured bit\n");
Patrick McHardy112f35c2008-01-14 23:46:20 -0800432 set_bit(IPS_ASSURED_BIT, &ct->status);
Patrick McHardy858b31332010-02-03 13:48:53 +0100433 nf_conntrack_event_cache(IPCT_ASSURED, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800434 }
435
436 return NF_ACCEPT;
Patrick McHardyb37e9332008-01-14 23:46:52 -0800437
438out_unlock:
Patrick McHardy440f0d52009-06-10 14:32:47 +0200439 spin_unlock_bh(&ct->lock);
Patrick McHardyb37e9332008-01-14 23:46:52 -0800440out:
441 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800442}
443
444/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200445static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100446 unsigned int dataoff, unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800447{
Patrick McHardyefe9f682008-01-14 23:47:09 -0800448 enum sctp_conntrack new_state;
Jan Engelhardt12c33aa2008-04-14 11:15:54 +0200449 const struct sctphdr *sh;
450 struct sctphdr _sctph;
451 const struct sctp_chunkhdr *sch;
452 struct sctp_chunkhdr _sch;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800453 u_int32_t offset, count;
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800454 unsigned long map[256 / sizeof(unsigned long)] = { 0 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800455
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456 sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
457 if (sh == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200458 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459
Patrick McHardy112f35c2008-01-14 23:46:20 -0800460 if (do_basic_checks(ct, skb, dataoff, map) != 0)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200461 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800462
463 /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
Patrick McHardy35c6d3c2008-01-14 23:46:05 -0800464 if (test_bit(SCTP_CID_ABORT, map) ||
465 test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
466 test_bit(SCTP_CID_COOKIE_ACK, map))
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200467 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800468
Changli Gaoe5fc9e72010-11-12 17:33:17 +0100469 memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
Patrick McHardyefe9f682008-01-14 23:47:09 -0800470 new_state = SCTP_CONNTRACK_MAX;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800471 for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
472 /* Don't need lock here: this conntrack not in circulation yet */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800473 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
474 SCTP_CONNTRACK_NONE, sch->type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800475
476 /* Invalid: delete conntrack */
Patrick McHardyefe9f682008-01-14 23:47:09 -0800477 if (new_state == SCTP_CONNTRACK_NONE ||
478 new_state == SCTP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700479 pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200480 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800481 }
482
483 /* Copy the vtag into the state info */
484 if (sch->type == SCTP_CID_INIT) {
485 if (sh->vtag == 0) {
486 sctp_inithdr_t _inithdr, *ih;
487
488 ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t),
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800489 sizeof(_inithdr), &_inithdr);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800490 if (ih == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200491 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800492
Patrick McHardy0d537782007-07-07 22:39:38 -0700493 pr_debug("Setting vtag %x for new conn\n",
494 ih->init_tag);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800495
Patrick McHardy112f35c2008-01-14 23:46:20 -0800496 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800497 ih->init_tag;
498 } else {
499 /* Sec 8.5.1 (A) */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200500 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800501 }
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200502 } else if (sch->type == SCTP_CID_HEARTBEAT) {
503 pr_debug("Setting vtag %x for secondary conntrack\n",
504 sh->vtag);
505 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800506 }
507 /* If it is a shutdown ack OOTB packet, we expect a return
508 shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
509 else {
Patrick McHardy0d537782007-07-07 22:39:38 -0700510 pr_debug("Setting vtag %x for new conn OOTB\n",
511 sh->vtag);
Patrick McHardy112f35c2008-01-14 23:46:20 -0800512 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800513 }
514
Patrick McHardyefe9f682008-01-14 23:47:09 -0800515 ct->proto.sctp.state = new_state;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800516 }
517
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200518 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800519}
520
Igor Maravićc0cd1152011-12-12 02:58:24 +0000521#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700522
523#include <linux/netfilter/nfnetlink.h>
524#include <linux/netfilter/nfnetlink_conntrack.h>
525
526static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
Patrick McHardy440f0d52009-06-10 14:32:47 +0200527 struct nf_conn *ct)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700528{
529 struct nlattr *nest_parms;
530
Patrick McHardy440f0d52009-06-10 14:32:47 +0200531 spin_lock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700532 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
533 if (!nest_parms)
534 goto nla_put_failure;
535
David S. Miller5e8d1eb2012-04-01 18:51:07 -0400536 if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
537 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
538 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
539 nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
540 ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
541 goto nla_put_failure;
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700542
Patrick McHardy440f0d52009-06-10 14:32:47 +0200543 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700544
545 nla_nest_end(skb, nest_parms);
546
547 return 0;
548
549nla_put_failure:
Patrick McHardy440f0d52009-06-10 14:32:47 +0200550 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700551 return -1;
552}
553
554static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
555 [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 },
556 [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 },
557 [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 },
558};
559
560static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
561{
562 struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
563 struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
564 int err;
565
566 /* updates may not contain the internal protocol info, skip parsing */
567 if (!attr)
568 return 0;
569
570 err = nla_parse_nested(tb,
571 CTA_PROTOINFO_SCTP_MAX,
572 attr,
573 sctp_nla_policy);
574 if (err < 0)
575 return err;
576
577 if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
578 !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
579 !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
580 return -EINVAL;
581
Patrick McHardy440f0d52009-06-10 14:32:47 +0200582 spin_lock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700583 ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
584 ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
Patrick McHardy5547cd02008-07-21 10:03:49 -0700585 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700586 ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
Patrick McHardy5547cd02008-07-21 10:03:49 -0700587 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
Patrick McHardy440f0d52009-06-10 14:32:47 +0200588 spin_unlock_bh(&ct->lock);
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700589
590 return 0;
591}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100592
593static int sctp_nlattr_size(void)
594{
595 return nla_total_size(0) /* CTA_PROTOINFO_SCTP */
596 + nla_policy_len(sctp_nla_policy, CTA_PROTOINFO_SCTP_MAX + 1);
597}
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700598#endif
599
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100600#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
601
602#include <linux/netfilter/nfnetlink.h>
603#include <linux/netfilter/nfnetlink_cttimeout.h>
604
Gao feng8264deb2012-05-28 21:04:23 +0000605static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
606 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100607{
608 unsigned int *timeouts = data;
Gao feng8264deb2012-05-28 21:04:23 +0000609 struct sctp_net *sn = sctp_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100610 int i;
611
612 /* set default SCTP timeouts. */
613 for (i=0; i<SCTP_CONNTRACK_MAX; i++)
Gao feng8264deb2012-05-28 21:04:23 +0000614 timeouts[i] = sn->timeouts[i];
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100615
616 /* there's a 1:1 mapping between attributes and protocol states. */
617 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
618 if (tb[i]) {
619 timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
620 }
621 }
622 return 0;
623}
624
625static int
626sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
627{
628 const unsigned int *timeouts = data;
629 int i;
630
David S. Miller5e8d1eb2012-04-01 18:51:07 -0400631 for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
632 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
633 goto nla_put_failure;
634 }
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100635 return 0;
636
637nla_put_failure:
638 return -ENOSPC;
639}
640
641static const struct nla_policy
642sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
643 [CTA_TIMEOUT_SCTP_CLOSED] = { .type = NLA_U32 },
644 [CTA_TIMEOUT_SCTP_COOKIE_WAIT] = { .type = NLA_U32 },
645 [CTA_TIMEOUT_SCTP_COOKIE_ECHOED] = { .type = NLA_U32 },
646 [CTA_TIMEOUT_SCTP_ESTABLISHED] = { .type = NLA_U32 },
647 [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT] = { .type = NLA_U32 },
648 [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD] = { .type = NLA_U32 },
649 [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = { .type = NLA_U32 },
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200650 [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT] = { .type = NLA_U32 },
651 [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED] = { .type = NLA_U32 },
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100652};
653#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
654
655
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800656#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100657static struct ctl_table sctp_sysctl_table[] = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800658 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800659 .procname = "nf_conntrack_sctp_timeout_closed",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800660 .maxlen = sizeof(unsigned int),
661 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800662 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800663 },
664 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800665 .procname = "nf_conntrack_sctp_timeout_cookie_wait",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800666 .maxlen = sizeof(unsigned int),
667 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800668 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800669 },
670 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800671 .procname = "nf_conntrack_sctp_timeout_cookie_echoed",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800672 .maxlen = sizeof(unsigned int),
673 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800674 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800675 },
676 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800677 .procname = "nf_conntrack_sctp_timeout_established",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800678 .maxlen = sizeof(unsigned int),
679 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800680 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800681 },
682 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800683 .procname = "nf_conntrack_sctp_timeout_shutdown_sent",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800684 .maxlen = sizeof(unsigned int),
685 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800686 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800687 },
688 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800689 .procname = "nf_conntrack_sctp_timeout_shutdown_recd",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800690 .maxlen = sizeof(unsigned int),
691 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800692 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800693 },
694 {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800695 .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800696 .maxlen = sizeof(unsigned int),
697 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800698 .proc_handler = proc_dointvec_jiffies,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800699 },
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200700 {
701 .procname = "nf_conntrack_sctp_timeout_heartbeat_sent",
702 .maxlen = sizeof(unsigned int),
703 .mode = 0644,
704 .proc_handler = proc_dointvec_jiffies,
705 },
706 {
707 .procname = "nf_conntrack_sctp_timeout_heartbeat_acked",
708 .maxlen = sizeof(unsigned int),
709 .mode = 0644,
710 .proc_handler = proc_dointvec_jiffies,
711 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800712 { }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800713};
Patrick McHardya999e682006-11-29 02:35:20 +0100714
715#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
716static struct ctl_table sctp_compat_sysctl_table[] = {
717 {
Patrick McHardya999e682006-11-29 02:35:20 +0100718 .procname = "ip_conntrack_sctp_timeout_closed",
Patrick McHardya999e682006-11-29 02:35:20 +0100719 .maxlen = sizeof(unsigned int),
720 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800721 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100722 },
723 {
Patrick McHardya999e682006-11-29 02:35:20 +0100724 .procname = "ip_conntrack_sctp_timeout_cookie_wait",
Patrick McHardya999e682006-11-29 02:35:20 +0100725 .maxlen = sizeof(unsigned int),
726 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800727 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100728 },
729 {
Patrick McHardya999e682006-11-29 02:35:20 +0100730 .procname = "ip_conntrack_sctp_timeout_cookie_echoed",
Patrick McHardya999e682006-11-29 02:35:20 +0100731 .maxlen = sizeof(unsigned int),
732 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800733 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100734 },
735 {
Patrick McHardya999e682006-11-29 02:35:20 +0100736 .procname = "ip_conntrack_sctp_timeout_established",
Patrick McHardya999e682006-11-29 02:35:20 +0100737 .maxlen = sizeof(unsigned int),
738 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800739 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100740 },
741 {
Patrick McHardya999e682006-11-29 02:35:20 +0100742 .procname = "ip_conntrack_sctp_timeout_shutdown_sent",
Patrick McHardya999e682006-11-29 02:35:20 +0100743 .maxlen = sizeof(unsigned int),
744 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800745 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100746 },
747 {
Patrick McHardya999e682006-11-29 02:35:20 +0100748 .procname = "ip_conntrack_sctp_timeout_shutdown_recd",
Patrick McHardya999e682006-11-29 02:35:20 +0100749 .maxlen = sizeof(unsigned int),
750 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800751 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100752 },
753 {
Patrick McHardya999e682006-11-29 02:35:20 +0100754 .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent",
Patrick McHardya999e682006-11-29 02:35:20 +0100755 .maxlen = sizeof(unsigned int),
756 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800757 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100758 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800759 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100760};
761#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800762#endif
763
Gao fengf42c4182012-06-21 04:36:46 +0000764static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
765 struct sctp_net *sn)
Gao feng49d485a32012-05-28 21:04:18 +0000766{
767#ifdef CONFIG_SYSCTL
Gao feng49d485a32012-05-28 21:04:18 +0000768 if (pn->ctl_table)
769 return 0;
770
771 pn->ctl_table = kmemdup(sctp_sysctl_table,
772 sizeof(sctp_sysctl_table),
773 GFP_KERNEL);
774 if (!pn->ctl_table)
775 return -ENOMEM;
776
777 pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
778 pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
779 pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
780 pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
781 pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
782 pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
783 pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
Michal Kubečekd7ee3512015-07-17 16:17:56 +0200784 pn->ctl_table[7].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_SENT];
785 pn->ctl_table[8].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_ACKED];
Gao feng49d485a32012-05-28 21:04:18 +0000786#endif
787 return 0;
788}
789
Gao fengf42c4182012-06-21 04:36:46 +0000790static int sctp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
791 struct sctp_net *sn)
Gao feng49d485a32012-05-28 21:04:18 +0000792{
793#ifdef CONFIG_SYSCTL
794#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
Gao feng49d485a32012-05-28 21:04:18 +0000795 pn->ctl_compat_table = kmemdup(sctp_compat_sysctl_table,
796 sizeof(sctp_compat_sysctl_table),
797 GFP_KERNEL);
798 if (!pn->ctl_compat_table)
799 return -ENOMEM;
800
801 pn->ctl_compat_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
802 pn->ctl_compat_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
803 pn->ctl_compat_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
804 pn->ctl_compat_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
805 pn->ctl_compat_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
806 pn->ctl_compat_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
807 pn->ctl_compat_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
808#endif
809#endif
810 return 0;
811}
812
Gao fengf42c4182012-06-21 04:36:46 +0000813static int sctp_init_net(struct net *net, u_int16_t proto)
Gao feng49d485a32012-05-28 21:04:18 +0000814{
815 int ret;
816 struct sctp_net *sn = sctp_pernet(net);
Gao fengf42c4182012-06-21 04:36:46 +0000817 struct nf_proto_net *pn = &sn->pn;
Gao feng49d485a32012-05-28 21:04:18 +0000818
Gao fengf42c4182012-06-21 04:36:46 +0000819 if (!pn->users) {
820 int i;
Gao feng49d485a32012-05-28 21:04:18 +0000821
Gao fengf42c4182012-06-21 04:36:46 +0000822 for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
823 sn->timeouts[i] = sctp_timeouts[i];
Gao feng49d485a32012-05-28 21:04:18 +0000824 }
Gao fengf42c4182012-06-21 04:36:46 +0000825
826 if (proto == AF_INET) {
827 ret = sctp_kmemdup_compat_sysctl_table(pn, sn);
828 if (ret < 0)
829 return ret;
830
831 ret = sctp_kmemdup_sysctl_table(pn, sn);
832 if (ret < 0)
833 nf_ct_kfree_compat_sysctl_table(pn);
834 } else
835 ret = sctp_kmemdup_sysctl_table(pn, sn);
836
Gao feng49d485a32012-05-28 21:04:18 +0000837 return ret;
838}
839
Patrick McHardy61075af2007-07-14 20:48:19 -0700840static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100841 .l3proto = PF_INET,
842 .l4proto = IPPROTO_SCTP,
843 .name = "sctp",
844 .pkt_to_tuple = sctp_pkt_to_tuple,
845 .invert_tuple = sctp_invert_tuple,
846 .print_tuple = sctp_print_tuple,
847 .print_conntrack = sctp_print_conntrack,
848 .packet = sctp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100849 .get_timeouts = sctp_get_timeouts,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100850 .new = sctp_new,
851 .me = THIS_MODULE,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000852#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700853 .to_nlattr = sctp_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100854 .nlattr_size = sctp_nlattr_size,
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700855 .from_nlattr = nlattr_to_sctp,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800856 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100857 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800858 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
859 .nla_policy = nf_ct_port_nla_policy,
860#endif
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100861#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
862 .ctnl_timeout = {
863 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
864 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
865 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
866 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
867 .nla_policy = sctp_timeout_nla_policy,
868 },
869#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng49d485a32012-05-28 21:04:18 +0000870 .net_id = &sctp_net_id,
Gao fengf42c4182012-06-21 04:36:46 +0000871 .init_net = sctp_init_net,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100872};
873
Patrick McHardy61075af2007-07-14 20:48:19 -0700874static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100875 .l3proto = PF_INET6,
876 .l4proto = IPPROTO_SCTP,
877 .name = "sctp",
878 .pkt_to_tuple = sctp_pkt_to_tuple,
879 .invert_tuple = sctp_invert_tuple,
880 .print_tuple = sctp_print_tuple,
881 .print_conntrack = sctp_print_conntrack,
882 .packet = sctp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100883 .get_timeouts = sctp_get_timeouts,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100884 .new = sctp_new,
885 .me = THIS_MODULE,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000886#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700887 .to_nlattr = sctp_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100888 .nlattr_size = sctp_nlattr_size,
Pablo Neira Ayusoa2588602008-06-09 15:56:39 -0700889 .from_nlattr = nlattr_to_sctp,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800890 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100891 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800892 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
893 .nla_policy = nf_ct_port_nla_policy,
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100894#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
895 .ctnl_timeout = {
896 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
897 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
898 .nlattr_max = CTA_TIMEOUT_SCTP_MAX,
899 .obj_size = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
900 .nla_policy = sctp_timeout_nla_policy,
901 },
902#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Pablo Neira Ayusoc7212e92007-12-17 22:29:02 -0800903#endif
Gao feng49d485a32012-05-28 21:04:18 +0000904 .net_id = &sctp_net_id,
Gao fengf42c4182012-06-21 04:36:46 +0000905 .init_net = sctp_init_net,
Gao feng49d485a32012-05-28 21:04:18 +0000906};
907
908static int sctp_net_init(struct net *net)
909{
910 int ret = 0;
911
Gao fengc296bb42013-01-23 12:51:10 +0100912 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000913 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100914 pr_err("nf_conntrack_sctp4: pernet registration failed.\n");
Gao feng49d485a32012-05-28 21:04:18 +0000915 goto out;
916 }
Gao fengc296bb42013-01-23 12:51:10 +0100917 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_sctp6);
Gao feng49d485a32012-05-28 21:04:18 +0000918 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100919 pr_err("nf_conntrack_sctp6: pernet registration failed.\n");
Gao feng49d485a32012-05-28 21:04:18 +0000920 goto cleanup_sctp4;
921 }
922 return 0;
923
924cleanup_sctp4:
Gao fengc296bb42013-01-23 12:51:10 +0100925 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000926out:
927 return ret;
928}
929
930static void sctp_net_exit(struct net *net)
931{
Gao fengc296bb42013-01-23 12:51:10 +0100932 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp6);
933 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000934}
935
936static struct pernet_operations sctp_net_ops = {
937 .init = sctp_net_init,
938 .exit = sctp_net_exit,
939 .id = &sctp_net_id,
940 .size = sizeof(struct sctp_net),
Patrick McHardy933a41e2006-11-29 02:35:18 +0100941};
942
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800943static int __init nf_conntrack_proto_sctp_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800944{
Gao fengc296bb42013-01-23 12:51:10 +0100945 int ret;
946
Gao feng0d98da52013-03-07 17:20:46 +0000947 ret = register_pernet_subsys(&sctp_net_ops);
948 if (ret < 0)
949 goto out_pernet;
950
Gao fengc296bb42013-01-23 12:51:10 +0100951 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp4);
952 if (ret < 0)
953 goto out_sctp4;
954
955 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_sctp6);
956 if (ret < 0)
957 goto out_sctp6;
958
Gao fengc296bb42013-01-23 12:51:10 +0100959 return 0;
Gao fengc296bb42013-01-23 12:51:10 +0100960out_sctp6:
961 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
962out_sctp4:
Gao feng0d98da52013-03-07 17:20:46 +0000963 unregister_pernet_subsys(&sctp_net_ops);
964out_pernet:
Gao fengc296bb42013-01-23 12:51:10 +0100965 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800966}
967
Stephen Hemminger2f0d2f12008-01-31 04:08:10 -0800968static void __exit nf_conntrack_proto_sctp_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800969{
Gao fengc296bb42013-01-23 12:51:10 +0100970 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp6);
971 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_sctp4);
Gao feng49d485a32012-05-28 21:04:18 +0000972 unregister_pernet_subsys(&sctp_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800973}
974
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800975module_init(nf_conntrack_proto_sctp_init);
976module_exit(nf_conntrack_proto_sctp_fini);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800977
978MODULE_LICENSE("GPL");
979MODULE_AUTHOR("Kiran Kumar Immidi");
980MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP");
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800981MODULE_ALIAS("ip_conntrack_proto_sctp");