blob: 1c8206e6560ac691159b6a8a9e9cd3b675b047e2 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 */
8
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08009#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010#include <linux/timer.h>
11#include <linux/netfilter.h>
12#include <linux/module.h>
13#include <linux/in.h>
14#include <linux/tcp.h>
15#include <linux/spinlock.h>
16#include <linux/skbuff.h>
17#include <linux/ipv6.h>
18#include <net/ip6_checksum.h>
19
20#include <net/tcp.h>
21
22#include <linux/netfilter.h>
23#include <linux/netfilter_ipv4.h>
24#include <linux/netfilter_ipv6.h>
25#include <net/netfilter/nf_conntrack.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010026#include <net/netfilter/nf_conntrack_l4proto.h>
Martin Josefssonf6180122006-11-29 02:35:01 +010027#include <net/netfilter/nf_conntrack_ecache.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080028
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080029/* Protects conntrack->proto.tcp */
30static DEFINE_RWLOCK(tcp_lock);
31
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080032/* "Be conservative in what you do,
33 be liberal in what you accept from others."
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034 If it's non-zero, we mark only out of window RST segments as INVALID. */
Patrick McHardy3aef0fd2007-02-12 11:16:58 -080035static int nf_ct_tcp_be_liberal __read_mostly = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036
Patrick McHardya09113c2007-02-07 15:05:33 -080037/* If it is set to zero, we disable picking up already established
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080038 connections. */
Patrick McHardy3aef0fd2007-02-12 11:16:58 -080039static int nf_ct_tcp_loose __read_mostly = 1;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080041/* Max number of the retransmitted packets without receiving an (acceptable)
42 ACK from the destination. If this number is reached, a shorter timer
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043 will be started. */
Patrick McHardy3aef0fd2007-02-12 11:16:58 -080044static int nf_ct_tcp_max_retrans __read_mostly = 3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045
46 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
47 closely. They're more complex. --RR */
48
49static const char *tcp_conntrack_names[] = {
50 "NONE",
51 "SYN_SENT",
52 "SYN_RECV",
53 "ESTABLISHED",
54 "FIN_WAIT",
55 "CLOSE_WAIT",
56 "LAST_ACK",
57 "TIME_WAIT",
58 "CLOSE",
59 "LISTEN"
60};
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080061
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080062#define SECS * HZ
63#define MINS * 60 SECS
64#define HOURS * 60 MINS
65#define DAYS * 24 HOURS
66
Patrick McHardy933a41e2006-11-29 02:35:18 +010067static unsigned int nf_ct_tcp_timeout_syn_sent __read_mostly = 2 MINS;
68static unsigned int nf_ct_tcp_timeout_syn_recv __read_mostly = 60 SECS;
69static unsigned int nf_ct_tcp_timeout_established __read_mostly = 5 DAYS;
70static unsigned int nf_ct_tcp_timeout_fin_wait __read_mostly = 2 MINS;
71static unsigned int nf_ct_tcp_timeout_close_wait __read_mostly = 60 SECS;
72static unsigned int nf_ct_tcp_timeout_last_ack __read_mostly = 30 SECS;
73static unsigned int nf_ct_tcp_timeout_time_wait __read_mostly = 2 MINS;
74static unsigned int nf_ct_tcp_timeout_close __read_mostly = 10 SECS;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080075
76/* RFC1122 says the R2 limit should be at least 100 seconds.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080077 Linux uses 15 packets as limit, which corresponds
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080078 to ~13-30min depending on RTO. */
Patrick McHardy933a41e2006-11-29 02:35:18 +010079static unsigned int nf_ct_tcp_timeout_max_retrans __read_mostly = 5 MINS;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080080
Patrick McHardy933a41e2006-11-29 02:35:18 +010081static unsigned int * tcp_timeouts[] = {
82 NULL, /* TCP_CONNTRACK_NONE */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083 &nf_ct_tcp_timeout_syn_sent, /* TCP_CONNTRACK_SYN_SENT, */
84 &nf_ct_tcp_timeout_syn_recv, /* TCP_CONNTRACK_SYN_RECV, */
85 &nf_ct_tcp_timeout_established, /* TCP_CONNTRACK_ESTABLISHED, */
86 &nf_ct_tcp_timeout_fin_wait, /* TCP_CONNTRACK_FIN_WAIT, */
87 &nf_ct_tcp_timeout_close_wait, /* TCP_CONNTRACK_CLOSE_WAIT, */
88 &nf_ct_tcp_timeout_last_ack, /* TCP_CONNTRACK_LAST_ACK, */
89 &nf_ct_tcp_timeout_time_wait, /* TCP_CONNTRACK_TIME_WAIT, */
90 &nf_ct_tcp_timeout_close, /* TCP_CONNTRACK_CLOSE, */
91 NULL, /* TCP_CONNTRACK_LISTEN */
92 };
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080093
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094#define sNO TCP_CONNTRACK_NONE
95#define sSS TCP_CONNTRACK_SYN_SENT
96#define sSR TCP_CONNTRACK_SYN_RECV
97#define sES TCP_CONNTRACK_ESTABLISHED
98#define sFW TCP_CONNTRACK_FIN_WAIT
99#define sCW TCP_CONNTRACK_CLOSE_WAIT
100#define sLA TCP_CONNTRACK_LAST_ACK
101#define sTW TCP_CONNTRACK_TIME_WAIT
102#define sCL TCP_CONNTRACK_CLOSE
103#define sLI TCP_CONNTRACK_LISTEN
104#define sIV TCP_CONNTRACK_MAX
105#define sIG TCP_CONNTRACK_IGNORE
106
107/* What TCP flags are set from RST/SYN/FIN/ACK. */
108enum tcp_bit_set {
109 TCP_SYN_SET,
110 TCP_SYNACK_SET,
111 TCP_FIN_SET,
112 TCP_ACK_SET,
113 TCP_RST_SET,
114 TCP_NONE_SET,
115};
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800116
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800117/*
118 * The TCP state transition table needs a few words...
119 *
120 * We are the man in the middle. All the packets go through us
121 * but might get lost in transit to the destination.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800122 * It is assumed that the destinations can't receive segments
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123 * we haven't seen.
124 *
125 * The checked segment is in window, but our windows are *not*
126 * equivalent with the ones of the sender/receiver. We always
127 * try to guess the state of the current sender.
128 *
129 * The meaning of the states are:
130 *
131 * NONE: initial state
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800132 * SYN_SENT: SYN-only packet seen
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800133 * SYN_RECV: SYN-ACK packet seen
134 * ESTABLISHED: ACK packet seen
135 * FIN_WAIT: FIN packet seen
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800136 * CLOSE_WAIT: ACK seen (after FIN)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800137 * LAST_ACK: FIN seen (after FIN)
138 * TIME_WAIT: last ACK seen
139 * CLOSE: closed connection
140 *
141 * LISTEN state is not used.
142 *
143 * Packets marked as IGNORED (sIG):
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800144 * if they may be either invalid or valid
145 * and the receiver may send back a connection
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146 * closing RST or a SYN/ACK.
147 *
148 * Packets marked as INVALID (sIV):
149 * if they are invalid
150 * or we do not support the request (simultaneous open)
151 */
152static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
153 {
154/* ORIGINAL */
155/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
156/*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
157/*
158 * sNO -> sSS Initialize a new connection
159 * sSS -> sSS Retransmitted SYN
160 * sSR -> sIG Late retransmitted SYN?
161 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800162 * are errors. Receiver will reply with RST
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163 * and close the connection.
164 * Or we are not in sync and hold a dead connection.
165 * sFW -> sIG
166 * sCW -> sIG
167 * sLA -> sIG
168 * sTW -> sSS Reopened connection (RFC 1122).
169 * sCL -> sSS
170 */
171/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
172/*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
173/*
174 * A SYN/ACK from the client is always invalid:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800175 * - either it tries to set up a simultaneous open, which is
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176 * not supported;
177 * - or the firewall has just been inserted between the two hosts
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800178 * during the session set-up. The SYN will be retransmitted
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800179 * by the true client (or it'll time out).
180 */
181/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
182/*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
183/*
184 * sNO -> sIV Too late and no reason to do anything...
185 * sSS -> sIV Client migth not send FIN in this state:
186 * we enforce waiting for a SYN/ACK reply first.
187 * sSR -> sFW Close started.
188 * sES -> sFW
189 * sFW -> sLA FIN seen in both directions, waiting for
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800190 * the last ACK.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191 * Migth be a retransmitted FIN as well...
192 * sCW -> sLA
193 * sLA -> sLA Retransmitted FIN. Remain in the same state.
194 * sTW -> sTW
195 * sCL -> sCL
196 */
197/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
198/*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
199/*
200 * sNO -> sES Assumed.
201 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
202 * sSR -> sES Established state is reached.
203 * sES -> sES :-)
204 * sFW -> sCW Normal close request answered by ACK.
205 * sCW -> sCW
206 * sLA -> sTW Last ACK detected.
207 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
208 * sCL -> sCL
209 */
210/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
211/*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
212/*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
213 },
214 {
215/* REPLY */
216/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
217/*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
218/*
219 * sNO -> sIV Never reached.
220 * sSS -> sIV Simultaneous open, not supported
221 * sSR -> sIV Simultaneous open, not supported.
222 * sES -> sIV Server may not initiate a connection.
223 * sFW -> sIV
224 * sCW -> sIV
225 * sLA -> sIV
226 * sTW -> sIV Reopened connection, but server may not do it.
227 * sCL -> sIV
228 */
229/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
230/*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
231/*
232 * sSS -> sSR Standard open.
233 * sSR -> sSR Retransmitted SYN/ACK.
234 * sES -> sIG Late retransmitted SYN/ACK?
235 * sFW -> sIG Might be SYN/ACK answering ignored SYN
236 * sCW -> sIG
237 * sLA -> sIG
238 * sTW -> sIG
239 * sCL -> sIG
240 */
241/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
242/*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
243/*
244 * sSS -> sIV Server might not send FIN in this state.
245 * sSR -> sFW Close started.
246 * sES -> sFW
247 * sFW -> sLA FIN seen in both directions.
248 * sCW -> sLA
249 * sLA -> sLA Retransmitted FIN.
250 * sTW -> sTW
251 * sCL -> sCL
252 */
253/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
Jozsef Kadlecsik73f30602005-12-01 14:28:58 -0800254/*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800255/*
Jozsef Kadlecsik73f30602005-12-01 14:28:58 -0800256 * sSS -> sIG Might be a half-open connection.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257 * sSR -> sSR Might answer late resent SYN.
258 * sES -> sES :-)
259 * sFW -> sCW Normal close request answered by ACK.
260 * sCW -> sCW
261 * sLA -> sTW Last ACK detected.
262 * sTW -> sTW Retransmitted last ACK.
263 * sCL -> sCL
264 */
265/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
266/*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
267/*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800268 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800269};
270
271static int tcp_pkt_to_tuple(const struct sk_buff *skb,
272 unsigned int dataoff,
273 struct nf_conntrack_tuple *tuple)
274{
275 struct tcphdr _hdr, *hp;
276
277 /* Actually only need first 8 bytes. */
278 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
279 if (hp == NULL)
280 return 0;
281
282 tuple->src.u.tcp.port = hp->source;
283 tuple->dst.u.tcp.port = hp->dest;
284
285 return 1;
286}
287
288static int tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
289 const struct nf_conntrack_tuple *orig)
290{
291 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
292 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
293 return 1;
294}
295
296/* Print out the per-protocol part of the tuple. */
297static int tcp_print_tuple(struct seq_file *s,
298 const struct nf_conntrack_tuple *tuple)
299{
300 return seq_printf(s, "sport=%hu dport=%hu ",
301 ntohs(tuple->src.u.tcp.port),
302 ntohs(tuple->dst.u.tcp.port));
303}
304
305/* Print out the private part of the conntrack. */
306static int tcp_print_conntrack(struct seq_file *s,
307 const struct nf_conn *conntrack)
308{
309 enum tcp_conntrack state;
310
311 read_lock_bh(&tcp_lock);
312 state = conntrack->proto.tcp.state;
313 read_unlock_bh(&tcp_lock);
314
315 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
316}
317
318static unsigned int get_conntrack_index(const struct tcphdr *tcph)
319{
320 if (tcph->rst) return TCP_RST_SET;
321 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
322 else if (tcph->fin) return TCP_FIN_SET;
323 else if (tcph->ack) return TCP_ACK_SET;
324 else return TCP_NONE_SET;
325}
326
327/* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
328 in IP Filter' by Guido van Rooij.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800329
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800330 http://www.nluug.nl/events/sane2000/papers.html
331 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800332
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800333 The boundaries and the conditions are changed according to RFC793:
334 the packet must intersect the window (i.e. segments may be
335 after the right or before the left edge) and thus receivers may ACK
336 segments after the right edge of the window.
337
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800338 td_maxend = max(sack + max(win,1)) seen in reply packets
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800339 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
340 td_maxwin += seq + len - sender.td_maxend
341 if seq + len > sender.td_maxend
342 td_end = max(seq + len) seen in sent packets
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800343
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800344 I. Upper bound for valid data: seq <= sender.td_maxend
345 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
346 III. Upper bound for valid ack: sack <= receiver.td_end
347 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
348
349 where sack is the highest right edge of sack block found in the packet.
350
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800351 The upper bound limit for a valid ack is not ignored -
352 we doesn't have to deal with fragments.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800353*/
354
355static inline __u32 segment_seq_plus_len(__u32 seq,
356 size_t len,
357 unsigned int dataoff,
358 struct tcphdr *tcph)
359{
360 /* XXX Should I use payload length field in IP/IPv6 header ?
361 * - YK */
362 return (seq + len - dataoff - tcph->doff*4
363 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
364}
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800365
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800366/* Fixme: what about big packets? */
367#define MAXACKWINCONST 66000
368#define MAXACKWINDOW(sender) \
369 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
370 : MAXACKWINCONST)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800371
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800372/*
373 * Simplified tcp_parse_options routine from tcp_input.c
374 */
375static void tcp_options(const struct sk_buff *skb,
376 unsigned int dataoff,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800377 struct tcphdr *tcph,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800378 struct ip_ct_tcp_state *state)
379{
380 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
381 unsigned char *ptr;
382 int length = (tcph->doff*4) - sizeof(struct tcphdr);
383
384 if (!length)
385 return;
386
387 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
388 length, buff);
389 BUG_ON(ptr == NULL);
390
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800391 state->td_scale =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392 state->flags = 0;
393
394 while (length > 0) {
395 int opcode=*ptr++;
396 int opsize;
397
398 switch (opcode) {
399 case TCPOPT_EOL:
400 return;
401 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
402 length--;
403 continue;
404 default:
405 opsize=*ptr++;
406 if (opsize < 2) /* "silly options" */
407 return;
408 if (opsize > length)
409 break; /* don't parse partial options */
410
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800411 if (opcode == TCPOPT_SACK_PERM
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800412 && opsize == TCPOLEN_SACK_PERM)
413 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
414 else if (opcode == TCPOPT_WINDOW
415 && opsize == TCPOLEN_WINDOW) {
416 state->td_scale = *(u_int8_t *)ptr;
417
418 if (state->td_scale > 14) {
419 /* See RFC1323 */
420 state->td_scale = 14;
421 }
422 state->flags |=
423 IP_CT_TCP_FLAG_WINDOW_SCALE;
424 }
425 ptr += opsize - 2;
426 length -= opsize;
427 }
428 }
429}
430
431static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
432 struct tcphdr *tcph, __u32 *sack)
433{
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800434 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800435 unsigned char *ptr;
436 int length = (tcph->doff*4) - sizeof(struct tcphdr);
437 __u32 tmp;
438
439 if (!length)
440 return;
441
442 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
443 length, buff);
444 BUG_ON(ptr == NULL);
445
446 /* Fast path for timestamp-only option */
447 if (length == TCPOLEN_TSTAMP_ALIGNED*4
YOSHIFUJI Hideaki8f05ce92007-03-07 14:21:00 +0900448 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
449 | (TCPOPT_NOP << 16)
450 | (TCPOPT_TIMESTAMP << 8)
451 | TCPOLEN_TIMESTAMP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800452 return;
453
454 while (length > 0) {
455 int opcode = *ptr++;
456 int opsize, i;
457
458 switch (opcode) {
459 case TCPOPT_EOL:
460 return;
461 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
462 length--;
463 continue;
464 default:
465 opsize = *ptr++;
466 if (opsize < 2) /* "silly options" */
467 return;
468 if (opsize > length)
469 break; /* don't parse partial options */
470
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800471 if (opcode == TCPOPT_SACK
472 && opsize >= (TCPOLEN_SACK_BASE
473 + TCPOLEN_SACK_PERBLOCK)
474 && !((opsize - TCPOLEN_SACK_BASE)
475 % TCPOLEN_SACK_PERBLOCK)) {
476 for (i = 0;
477 i < (opsize - TCPOLEN_SACK_BASE);
478 i += TCPOLEN_SACK_PERBLOCK) {
479 tmp = ntohl(*((__be32 *)(ptr+i)+1));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480
481 if (after(tmp, *sack))
482 *sack = tmp;
483 }
484 return;
485 }
486 ptr += opsize - 2;
487 length -= opsize;
488 }
489 }
490}
491
Patrick McHardy0d537782007-07-07 22:39:38 -0700492static int tcp_in_window(struct nf_conn *ct,
493 struct ip_ct_tcp *state,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800494 enum ip_conntrack_dir dir,
495 unsigned int index,
496 const struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800497 unsigned int dataoff,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800498 struct tcphdr *tcph,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800499 int pf)
500{
501 struct ip_ct_tcp_state *sender = &state->seen[dir];
502 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
Patrick McHardy0d537782007-07-07 22:39:38 -0700503 struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800504 __u32 seq, ack, sack, end, win, swin;
505 int res;
506
507 /*
508 * Get the required data from the packet.
509 */
510 seq = ntohl(tcph->seq);
511 ack = sack = ntohl(tcph->ack_seq);
512 win = ntohs(tcph->window);
513 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
514
515 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
516 tcp_sack(skb, dataoff, tcph, &sack);
517
Patrick McHardy0d537782007-07-07 22:39:38 -0700518 pr_debug("tcp_in_window: START\n");
519 pr_debug("tcp_in_window: ");
520 NF_CT_DUMP_TUPLE(tuple);
521 pr_debug("seq=%u ack=%u sack=%u win=%u end=%u\n",
522 seq, ack, sack, win, end);
523 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
524 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
525 sender->td_end, sender->td_maxend, sender->td_maxwin,
526 sender->td_scale,
527 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
528 receiver->td_scale);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800529
530 if (sender->td_end == 0) {
531 /*
532 * Initialize sender data.
533 */
534 if (tcph->syn && tcph->ack) {
535 /*
536 * Outgoing SYN-ACK in reply to a SYN.
537 */
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800538 sender->td_end =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800539 sender->td_maxend = end;
540 sender->td_maxwin = (win == 0 ? 1 : win);
541
542 tcp_options(skb, dataoff, tcph, sender);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800543 /*
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800544 * RFC 1323:
545 * Both sides must send the Window Scale option
546 * to enable window scaling in either direction.
547 */
548 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
549 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800550 sender->td_scale =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800551 receiver->td_scale = 0;
552 } else {
553 /*
554 * We are in the middle of a connection,
555 * its history is lost for us.
556 * Let's try to use the data from the packet.
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800557 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800558 sender->td_end = end;
559 sender->td_maxwin = (win == 0 ? 1 : win);
560 sender->td_maxend = end + sender->td_maxwin;
561 }
562 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
563 && dir == IP_CT_DIR_ORIGINAL)
564 || (state->state == TCP_CONNTRACK_SYN_RECV
565 && dir == IP_CT_DIR_REPLY))
566 && after(end, sender->td_end)) {
567 /*
568 * RFC 793: "if a TCP is reinitialized ... then it need
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800569 * not wait at all; it must only be sure to use sequence
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800570 * numbers larger than those recently used."
571 */
572 sender->td_end =
573 sender->td_maxend = end;
574 sender->td_maxwin = (win == 0 ? 1 : win);
575
576 tcp_options(skb, dataoff, tcph, sender);
577 }
578
579 if (!(tcph->ack)) {
580 /*
581 * If there is no ACK, just pretend it was set and OK.
582 */
583 ack = sack = receiver->td_end;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800584 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
585 (TCP_FLAG_ACK|TCP_FLAG_RST))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800586 && (ack == 0)) {
587 /*
588 * Broken TCP stacks, that set ACK in RST packets as well
589 * with zero ack value.
590 */
591 ack = sack = receiver->td_end;
592 }
593
594 if (seq == end
595 && (!tcph->rst
596 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
597 /*
598 * Packets contains no data: we assume it is valid
599 * and check the ack value only.
600 * However RST segments are always validated by their
601 * SEQ number, except when seq == 0 (reset sent answering
602 * SYN.
603 */
604 seq = end = sender->td_end;
605
Patrick McHardy0d537782007-07-07 22:39:38 -0700606 pr_debug("tcp_in_window: ");
607 NF_CT_DUMP_TUPLE(tuple);
608 pr_debug("seq=%u ack=%u sack =%u win=%u end=%u\n",
609 seq, ack, sack, win, end);
610 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
611 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
612 sender->td_end, sender->td_maxend, sender->td_maxwin,
613 sender->td_scale,
614 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
615 receiver->td_scale);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800616
Patrick McHardy0d537782007-07-07 22:39:38 -0700617 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
618 before(seq, sender->td_maxend + 1),
619 after(end, sender->td_end - receiver->td_maxwin - 1),
620 before(sack, receiver->td_end + 1),
621 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800622
Patrick McHardya09113c2007-02-07 15:05:33 -0800623 if (before(seq, sender->td_maxend + 1) &&
624 after(end, sender->td_end - receiver->td_maxwin - 1) &&
625 before(sack, receiver->td_end + 1) &&
626 after(ack, receiver->td_end - MAXACKWINDOW(sender))) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800627 /*
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800628 * Take into account window scaling (RFC 1323).
629 */
630 if (!tcph->syn)
631 win <<= sender->td_scale;
632
633 /*
634 * Update sender data.
635 */
636 swin = win + (sack - ack);
637 if (sender->td_maxwin < swin)
638 sender->td_maxwin = swin;
639 if (after(end, sender->td_end))
640 sender->td_end = end;
641 /*
642 * Update receiver data.
643 */
644 if (after(end, sender->td_maxend))
645 receiver->td_maxwin += end - sender->td_maxend;
646 if (after(sack + win, receiver->td_maxend - 1)) {
647 receiver->td_maxend = sack + win;
648 if (win == 0)
649 receiver->td_maxend++;
650 }
651
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800652 /*
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800653 * Check retransmissions.
654 */
655 if (index == TCP_ACK_SET) {
656 if (state->last_dir == dir
657 && state->last_seq == seq
658 && state->last_ack == ack
George Hansperc1fe3ca2006-09-20 12:03:23 -0700659 && state->last_end == end
660 && state->last_win == win)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800661 state->retrans++;
662 else {
663 state->last_dir = dir;
664 state->last_seq = seq;
665 state->last_ack = ack;
666 state->last_end = end;
George Hansperc1fe3ca2006-09-20 12:03:23 -0700667 state->last_win = win;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800668 state->retrans = 0;
669 }
670 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800671 res = 1;
672 } else {
Patrick McHardya09113c2007-02-07 15:05:33 -0800673 res = 0;
674 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
675 nf_ct_tcp_be_liberal)
676 res = 1;
677 if (!res && LOG_INVALID(IPPROTO_TCP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800678 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
679 "nf_ct_tcp: %s ",
680 before(seq, sender->td_maxend + 1) ?
681 after(end, sender->td_end - receiver->td_maxwin - 1) ?
682 before(sack, receiver->td_end + 1) ?
683 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
684 : "ACK is under the lower bound (possible overly delayed ACK)"
685 : "ACK is over the upper bound (ACKed data not seen yet)"
686 : "SEQ is under the lower bound (already ACKed data retransmitted)"
687 : "SEQ is over the upper bound (over the window of the receiver)");
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800688 }
689
Patrick McHardy0d537782007-07-07 22:39:38 -0700690 pr_debug("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
691 "receiver end=%u maxend=%u maxwin=%u\n",
692 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
693 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800694
695 return res;
696}
697
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800698#ifdef CONFIG_NF_NAT_NEEDED
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800699/* Update sender->td_end after NAT successfully mangled the packet */
700/* Caller must linearize skb at tcp header. */
701void nf_conntrack_tcp_update(struct sk_buff *skb,
702 unsigned int dataoff,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800703 struct nf_conn *conntrack,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800704 int dir)
705{
706 struct tcphdr *tcph = (void *)skb->data + dataoff;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800707 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
708 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
Patrick McHardy0d537782007-07-07 22:39:38 -0700709 __u32 end;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800710
711 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph);
712
713 write_lock_bh(&tcp_lock);
714 /*
715 * We have to worry for the ack in the reply packet only...
716 */
717 if (after(end, conntrack->proto.tcp.seen[dir].td_end))
718 conntrack->proto.tcp.seen[dir].td_end = end;
719 conntrack->proto.tcp.last_end = end;
720 write_unlock_bh(&tcp_lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700721 pr_debug("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
722 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
723 sender->td_end, sender->td_maxend, sender->td_maxwin,
724 sender->td_scale,
725 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
726 receiver->td_scale);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800727}
Patrick McHardy13b18332006-12-02 22:11:25 -0800728EXPORT_SYMBOL_GPL(nf_conntrack_tcp_update);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800729#endif
730
731#define TH_FIN 0x01
732#define TH_SYN 0x02
733#define TH_RST 0x04
734#define TH_PUSH 0x08
735#define TH_ACK 0x10
736#define TH_URG 0x20
737#define TH_ECE 0x40
738#define TH_CWR 0x80
739
Willy Tarreau5c8ce7c2007-03-14 16:44:53 -0700740/* table of valid flag combinations - PUSH, ECE and CWR are always valid */
741static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG) + 1] =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800742{
743 [TH_SYN] = 1,
Patrick McHardyd3ab4292007-03-04 15:57:46 -0800744 [TH_SYN|TH_URG] = 1,
Patrick McHardyd3ab4292007-03-04 15:57:46 -0800745 [TH_SYN|TH_ACK] = 1,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800746 [TH_RST] = 1,
747 [TH_RST|TH_ACK] = 1,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800748 [TH_FIN|TH_ACK] = 1,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800749 [TH_FIN|TH_ACK|TH_URG] = 1,
Willy Tarreau5c8ce7c2007-03-14 16:44:53 -0700750 [TH_ACK] = 1,
751 [TH_ACK|TH_URG] = 1,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800752};
753
754/* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
755static int tcp_error(struct sk_buff *skb,
756 unsigned int dataoff,
757 enum ip_conntrack_info *ctinfo,
758 int pf,
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700759 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800760{
761 struct tcphdr _tcph, *th;
762 unsigned int tcplen = skb->len - dataoff;
763 u_int8_t tcpflags;
764
765 /* Smaller that minimal TCP header? */
766 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
767 if (th == NULL) {
768 if (LOG_INVALID(IPPROTO_TCP))
769 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
770 "nf_ct_tcp: short packet ");
771 return -NF_ACCEPT;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800772 }
773
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800774 /* Not whole TCP header or malformed packet */
775 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
776 if (LOG_INVALID(IPPROTO_TCP))
777 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
778 "nf_ct_tcp: truncated/malformed packet ");
779 return -NF_ACCEPT;
780 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800781
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800782 /* Checksum invalid? Ignore.
783 * We skip checking packets on the outgoing path
Patrick McHardy84fa7932006-08-29 16:44:56 -0700784 * because the checksum is assumed to be correct.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800785 */
786 /* FIXME: Source route IP option packets --RR */
Patrick McHardy39a27a32006-05-29 18:23:54 -0700787 if (nf_conntrack_checksum &&
788 ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) ||
789 (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700790 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800791 if (LOG_INVALID(IPPROTO_TCP))
792 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
793 "nf_ct_tcp: bad TCP checksum ");
794 return -NF_ACCEPT;
795 }
796
797 /* Check TCP flags. */
Willy Tarreau5c8ce7c2007-03-14 16:44:53 -0700798 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR|TH_PUSH));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800799 if (!tcp_valid_flags[tcpflags]) {
800 if (LOG_INVALID(IPPROTO_TCP))
801 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
802 "nf_ct_tcp: invalid TCP flag combination ");
803 return -NF_ACCEPT;
804 }
805
806 return NF_ACCEPT;
807}
808
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800809/* Returns verdict for packet, or -1 for invalid. */
810static int tcp_packet(struct nf_conn *conntrack,
811 const struct sk_buff *skb,
812 unsigned int dataoff,
813 enum ip_conntrack_info ctinfo,
814 int pf,
815 unsigned int hooknum)
816{
Patrick McHardy0d537782007-07-07 22:39:38 -0700817 struct nf_conntrack_tuple *tuple;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800818 enum tcp_conntrack new_state, old_state;
819 enum ip_conntrack_dir dir;
820 struct tcphdr *th, _tcph;
821 unsigned long timeout;
822 unsigned int index;
823
824 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
825 BUG_ON(th == NULL);
826
827 write_lock_bh(&tcp_lock);
828 old_state = conntrack->proto.tcp.state;
829 dir = CTINFO2DIR(ctinfo);
830 index = get_conntrack_index(th);
831 new_state = tcp_conntracks[dir][index][old_state];
Patrick McHardy0d537782007-07-07 22:39:38 -0700832 tuple = &conntrack->tuplehash[dir].tuple;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800833
834 switch (new_state) {
835 case TCP_CONNTRACK_IGNORE:
Jozsef Kadlecsik73f30602005-12-01 14:28:58 -0800836 /* Ignored packets:
837 *
838 * a) SYN in ORIGINAL
839 * b) SYN/ACK in REPLY
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800840 * c) ACK in reply direction after initial SYN in original.
Jozsef Kadlecsik73f30602005-12-01 14:28:58 -0800841 */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800842 if (index == TCP_SYNACK_SET
843 && conntrack->proto.tcp.last_index == TCP_SYN_SET
844 && conntrack->proto.tcp.last_dir != dir
845 && ntohl(th->ack_seq) ==
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800846 conntrack->proto.tcp.last_end) {
847 /* This SYN/ACK acknowledges a SYN that we earlier
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800848 * ignored as invalid. This means that the client and
849 * the server are both in sync, while the firewall is
850 * not. We kill this session and block the SYN/ACK so
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800851 * that the client cannot but retransmit its SYN and
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800852 * thus initiate a clean new session.
853 */
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800854 write_unlock_bh(&tcp_lock);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800855 if (LOG_INVALID(IPPROTO_TCP))
856 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
857 "nf_ct_tcp: killing out of sync session ");
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800858 if (del_timer(&conntrack->timeout))
859 conntrack->timeout.function((unsigned long)
860 conntrack);
861 return -NF_DROP;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800862 }
863 conntrack->proto.tcp.last_index = index;
864 conntrack->proto.tcp.last_dir = dir;
865 conntrack->proto.tcp.last_seq = ntohl(th->seq);
866 conntrack->proto.tcp.last_end =
867 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
868
869 write_unlock_bh(&tcp_lock);
870 if (LOG_INVALID(IPPROTO_TCP))
871 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
872 "nf_ct_tcp: invalid packed ignored ");
873 return NF_ACCEPT;
874 case TCP_CONNTRACK_MAX:
875 /* Invalid packet */
Patrick McHardy0d537782007-07-07 22:39:38 -0700876 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
877 dir, get_conntrack_index(th), old_state);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800878 write_unlock_bh(&tcp_lock);
879 if (LOG_INVALID(IPPROTO_TCP))
880 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
881 "nf_ct_tcp: invalid state ");
882 return -NF_ACCEPT;
883 case TCP_CONNTRACK_SYN_SENT:
884 if (old_state < TCP_CONNTRACK_TIME_WAIT)
885 break;
886 if ((conntrack->proto.tcp.seen[dir].flags &
887 IP_CT_TCP_FLAG_CLOSE_INIT)
888 || after(ntohl(th->seq),
889 conntrack->proto.tcp.seen[dir].td_end)) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800890 /* Attempt to reopen a closed connection.
891 * Delete this connection and look up again. */
892 write_unlock_bh(&tcp_lock);
893 if (del_timer(&conntrack->timeout))
894 conntrack->timeout.function((unsigned long)
895 conntrack);
896 return -NF_REPEAT;
KOVACS Krisztian3746a2b2005-11-14 15:23:01 -0800897 } else {
898 write_unlock_bh(&tcp_lock);
899 if (LOG_INVALID(IPPROTO_TCP))
900 nf_log_packet(pf, 0, skb, NULL, NULL,
901 NULL, "nf_ct_tcp: invalid SYN");
902 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800903 }
904 case TCP_CONNTRACK_CLOSE:
905 if (index == TCP_RST_SET
Jozsef Kadlecsik73f30602005-12-01 14:28:58 -0800906 && ((test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800907 && conntrack->proto.tcp.last_index == TCP_SYN_SET)
908 || (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
909 && conntrack->proto.tcp.last_index == TCP_ACK_SET))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800910 && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
Adrian Bunk93b1fae2006-01-10 00:13:33 +0100911 /* RST sent to invalid SYN or ACK we had let through
Jozsef Kadlecsik73f30602005-12-01 14:28:58 -0800912 * at a) and c) above:
913 *
914 * a) SYN was in window then
915 * c) we hold a half-open connection.
916 *
917 * Delete our connection entry.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800918 * We skip window checking, because packet might ACK
Jozsef Kadlecsik73f30602005-12-01 14:28:58 -0800919 * segments we ignored. */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800920 goto in_window;
921 }
Adrian Bunk93b1fae2006-01-10 00:13:33 +0100922 /* Just fall through */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800923 default:
924 /* Keep compilers happy. */
925 break;
926 }
927
Patrick McHardy0d537782007-07-07 22:39:38 -0700928 if (!tcp_in_window(conntrack, &conntrack->proto.tcp, dir, index,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800929 skb, dataoff, th, pf)) {
930 write_unlock_bh(&tcp_lock);
931 return -NF_ACCEPT;
932 }
933 in_window:
934 /* From now on we have got in-window packets */
935 conntrack->proto.tcp.last_index = index;
936
Patrick McHardy0d537782007-07-07 22:39:38 -0700937 pr_debug("tcp_conntracks: ");
938 NF_CT_DUMP_TUPLE(tuple);
939 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
940 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
941 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
942 old_state, new_state);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800943
944 conntrack->proto.tcp.state = new_state;
945 if (old_state != new_state
946 && (new_state == TCP_CONNTRACK_FIN_WAIT
947 || new_state == TCP_CONNTRACK_CLOSE))
948 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
949 timeout = conntrack->proto.tcp.retrans >= nf_ct_tcp_max_retrans
950 && *tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans
951 ? nf_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
952 write_unlock_bh(&tcp_lock);
953
954 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
955 if (new_state != old_state)
956 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
957
958 if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
959 /* If only reply is a RST, we can consider ourselves not to
960 have an established connection: this is a fairly common
961 problem case, so we can delete the conntrack
962 immediately. --RR */
963 if (th->rst) {
964 if (del_timer(&conntrack->timeout))
965 conntrack->timeout.function((unsigned long)
966 conntrack);
967 return NF_ACCEPT;
968 }
969 } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
970 && (old_state == TCP_CONNTRACK_SYN_RECV
971 || old_state == TCP_CONNTRACK_ESTABLISHED)
972 && new_state == TCP_CONNTRACK_ESTABLISHED) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800973 /* Set ASSURED if we see see valid ack in ESTABLISHED
974 after SYN_RECV or a valid answer for a picked up
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800975 connection. */
976 set_bit(IPS_ASSURED_BIT, &conntrack->status);
977 nf_conntrack_event_cache(IPCT_STATUS, skb);
978 }
979 nf_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
980
981 return NF_ACCEPT;
982}
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800983
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800984/* Called when a new connection for this protocol found. */
985static int tcp_new(struct nf_conn *conntrack,
986 const struct sk_buff *skb,
987 unsigned int dataoff)
988{
989 enum tcp_conntrack new_state;
990 struct tcphdr *th, _tcph;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800991 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
992 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800993
994 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
995 BUG_ON(th == NULL);
996
997 /* Don't need lock here: this conntrack not in circulation yet */
998 new_state
999 = tcp_conntracks[0][get_conntrack_index(th)]
1000 [TCP_CONNTRACK_NONE];
1001
1002 /* Invalid: delete conntrack */
1003 if (new_state >= TCP_CONNTRACK_MAX) {
Patrick McHardy0d537782007-07-07 22:39:38 -07001004 pr_debug("nf_ct_tcp: invalid new deleting.\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001005 return 0;
1006 }
1007
1008 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1009 /* SYN packet */
1010 conntrack->proto.tcp.seen[0].td_end =
1011 segment_seq_plus_len(ntohl(th->seq), skb->len,
1012 dataoff, th);
1013 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1014 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1015 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1016 conntrack->proto.tcp.seen[0].td_maxend =
1017 conntrack->proto.tcp.seen[0].td_end;
1018
1019 tcp_options(skb, dataoff, th, &conntrack->proto.tcp.seen[0]);
1020 conntrack->proto.tcp.seen[1].flags = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001021 } else if (nf_ct_tcp_loose == 0) {
1022 /* Don't try to pick up connections. */
1023 return 0;
1024 } else {
1025 /*
1026 * We are in the middle of a connection,
1027 * its history is lost for us.
1028 * Let's try to use the data from the packet.
1029 */
1030 conntrack->proto.tcp.seen[0].td_end =
1031 segment_seq_plus_len(ntohl(th->seq), skb->len,
1032 dataoff, th);
1033 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1034 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1035 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1036 conntrack->proto.tcp.seen[0].td_maxend =
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001037 conntrack->proto.tcp.seen[0].td_end +
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001038 conntrack->proto.tcp.seen[0].td_maxwin;
1039 conntrack->proto.tcp.seen[0].td_scale = 0;
1040
Patrick McHardya09113c2007-02-07 15:05:33 -08001041 /* We assume SACK and liberal window checking to handle
1042 * window scaling */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001043 conntrack->proto.tcp.seen[0].flags =
Patrick McHardya09113c2007-02-07 15:05:33 -08001044 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1045 IP_CT_TCP_FLAG_BE_LIBERAL;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001046 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001047
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001048 conntrack->proto.tcp.seen[1].td_end = 0;
1049 conntrack->proto.tcp.seen[1].td_maxend = 0;
1050 conntrack->proto.tcp.seen[1].td_maxwin = 1;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001051 conntrack->proto.tcp.seen[1].td_scale = 0;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001052
1053 /* tcp_packet will set them */
1054 conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1055 conntrack->proto.tcp.last_index = TCP_NONE_SET;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001056
Patrick McHardy0d537782007-07-07 22:39:38 -07001057 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1058 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1059 sender->td_end, sender->td_maxend, sender->td_maxwin,
1060 sender->td_scale,
1061 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1062 receiver->td_scale);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001063 return 1;
1064}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001065
Patrick McHardye281db5c2007-03-04 15:57:25 -08001066#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001067
1068#include <linux/netfilter/nfnetlink.h>
1069#include <linux/netfilter/nfnetlink_conntrack.h>
1070
1071static int tcp_to_nfattr(struct sk_buff *skb, struct nfattr *nfa,
1072 const struct nf_conn *ct)
1073{
1074 struct nfattr *nest_parms;
Pablo Neira Ayusoc8e20782007-03-14 16:45:19 -07001075 struct nf_ct_tcp_flags tmp = {};
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001076
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001077 read_lock_bh(&tcp_lock);
1078 nest_parms = NFA_NEST(skb, CTA_PROTOINFO_TCP);
1079 NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t),
1080 &ct->proto.tcp.state);
Pablo Neira Ayusoc8e20782007-03-14 16:45:19 -07001081
1082 NFA_PUT(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL, sizeof(u_int8_t),
1083 &ct->proto.tcp.seen[0].td_scale);
1084
1085 NFA_PUT(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY, sizeof(u_int8_t),
1086 &ct->proto.tcp.seen[1].td_scale);
1087
1088 tmp.flags = ct->proto.tcp.seen[0].flags;
1089 NFA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1090 sizeof(struct nf_ct_tcp_flags), &tmp);
1091
1092 tmp.flags = ct->proto.tcp.seen[1].flags;
1093 NFA_PUT(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1094 sizeof(struct nf_ct_tcp_flags), &tmp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001095 read_unlock_bh(&tcp_lock);
1096
1097 NFA_NEST_END(skb, nest_parms);
1098
1099 return 0;
1100
1101nfattr_failure:
1102 read_unlock_bh(&tcp_lock);
1103 return -1;
1104}
1105
1106static const size_t cta_min_tcp[CTA_PROTOINFO_TCP_MAX] = {
Pablo Neira Ayusoc8e20782007-03-14 16:45:19 -07001107 [CTA_PROTOINFO_TCP_STATE-1] = sizeof(u_int8_t),
1108 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1] = sizeof(u_int8_t),
1109 [CTA_PROTOINFO_TCP_WSCALE_REPLY-1] = sizeof(u_int8_t),
1110 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1] = sizeof(struct nf_ct_tcp_flags),
1111 [CTA_PROTOINFO_TCP_FLAGS_REPLY-1] = sizeof(struct nf_ct_tcp_flags)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001112};
1113
1114static int nfattr_to_tcp(struct nfattr *cda[], struct nf_conn *ct)
1115{
1116 struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1];
1117 struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];
1118
1119 /* updates could not contain anything about the private
1120 * protocol info, in that case skip the parsing */
1121 if (!attr)
1122 return 0;
1123
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001124 nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001125
1126 if (nfattr_bad_size(tb, CTA_PROTOINFO_TCP_MAX, cta_min_tcp))
1127 return -EINVAL;
1128
1129 if (!tb[CTA_PROTOINFO_TCP_STATE-1])
1130 return -EINVAL;
1131
1132 write_lock_bh(&tcp_lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001133 ct->proto.tcp.state =
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001134 *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
Pablo Neira Ayusoc8e20782007-03-14 16:45:19 -07001135
1136 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1]) {
1137 struct nf_ct_tcp_flags *attr =
1138 NFA_DATA(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL-1]);
1139 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1140 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1141 }
1142
1143 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY-1]) {
1144 struct nf_ct_tcp_flags *attr =
1145 NFA_DATA(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY-1]);
1146 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1147 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1148 }
1149
1150 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1] &&
1151 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY-1] &&
1152 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1153 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1154 ct->proto.tcp.seen[0].td_scale = *(u_int8_t *)
1155 NFA_DATA(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL-1]);
1156 ct->proto.tcp.seen[1].td_scale = *(u_int8_t *)
1157 NFA_DATA(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY-1]);
1158 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001159 write_unlock_bh(&tcp_lock);
1160
1161 return 0;
1162}
1163#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +01001164
1165#ifdef CONFIG_SYSCTL
1166static unsigned int tcp_sysctl_table_users;
1167static struct ctl_table_header *tcp_sysctl_header;
1168static struct ctl_table tcp_sysctl_table[] = {
1169 {
1170 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1171 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1172 .data = &nf_ct_tcp_timeout_syn_sent,
1173 .maxlen = sizeof(unsigned int),
1174 .mode = 0644,
1175 .proc_handler = &proc_dointvec_jiffies,
1176 },
1177 {
1178 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1179 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1180 .data = &nf_ct_tcp_timeout_syn_recv,
1181 .maxlen = sizeof(unsigned int),
1182 .mode = 0644,
1183 .proc_handler = &proc_dointvec_jiffies,
1184 },
1185 {
1186 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1187 .procname = "nf_conntrack_tcp_timeout_established",
1188 .data = &nf_ct_tcp_timeout_established,
1189 .maxlen = sizeof(unsigned int),
1190 .mode = 0644,
1191 .proc_handler = &proc_dointvec_jiffies,
1192 },
1193 {
1194 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1195 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1196 .data = &nf_ct_tcp_timeout_fin_wait,
1197 .maxlen = sizeof(unsigned int),
1198 .mode = 0644,
1199 .proc_handler = &proc_dointvec_jiffies,
1200 },
1201 {
1202 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1203 .procname = "nf_conntrack_tcp_timeout_close_wait",
1204 .data = &nf_ct_tcp_timeout_close_wait,
1205 .maxlen = sizeof(unsigned int),
1206 .mode = 0644,
1207 .proc_handler = &proc_dointvec_jiffies,
1208 },
1209 {
1210 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1211 .procname = "nf_conntrack_tcp_timeout_last_ack",
1212 .data = &nf_ct_tcp_timeout_last_ack,
1213 .maxlen = sizeof(unsigned int),
1214 .mode = 0644,
1215 .proc_handler = &proc_dointvec_jiffies,
1216 },
1217 {
1218 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1219 .procname = "nf_conntrack_tcp_timeout_time_wait",
1220 .data = &nf_ct_tcp_timeout_time_wait,
1221 .maxlen = sizeof(unsigned int),
1222 .mode = 0644,
1223 .proc_handler = &proc_dointvec_jiffies,
1224 },
1225 {
1226 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1227 .procname = "nf_conntrack_tcp_timeout_close",
1228 .data = &nf_ct_tcp_timeout_close,
1229 .maxlen = sizeof(unsigned int),
1230 .mode = 0644,
1231 .proc_handler = &proc_dointvec_jiffies,
1232 },
1233 {
1234 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1235 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1236 .data = &nf_ct_tcp_timeout_max_retrans,
1237 .maxlen = sizeof(unsigned int),
1238 .mode = 0644,
1239 .proc_handler = &proc_dointvec_jiffies,
1240 },
1241 {
1242 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
1243 .procname = "nf_conntrack_tcp_loose",
1244 .data = &nf_ct_tcp_loose,
1245 .maxlen = sizeof(unsigned int),
1246 .mode = 0644,
1247 .proc_handler = &proc_dointvec,
1248 },
1249 {
1250 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
1251 .procname = "nf_conntrack_tcp_be_liberal",
1252 .data = &nf_ct_tcp_be_liberal,
1253 .maxlen = sizeof(unsigned int),
1254 .mode = 0644,
1255 .proc_handler = &proc_dointvec,
1256 },
1257 {
1258 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
1259 .procname = "nf_conntrack_tcp_max_retrans",
1260 .data = &nf_ct_tcp_max_retrans,
1261 .maxlen = sizeof(unsigned int),
1262 .mode = 0644,
1263 .proc_handler = &proc_dointvec,
1264 },
1265 {
1266 .ctl_name = 0
1267 }
1268};
Patrick McHardya999e682006-11-29 02:35:20 +01001269
1270#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1271static struct ctl_table tcp_compat_sysctl_table[] = {
1272 {
1273 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1274 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1275 .data = &nf_ct_tcp_timeout_syn_sent,
1276 .maxlen = sizeof(unsigned int),
1277 .mode = 0644,
1278 .proc_handler = &proc_dointvec_jiffies,
1279 },
1280 {
1281 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1282 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1283 .data = &nf_ct_tcp_timeout_syn_recv,
1284 .maxlen = sizeof(unsigned int),
1285 .mode = 0644,
1286 .proc_handler = &proc_dointvec_jiffies,
1287 },
1288 {
1289 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1290 .procname = "ip_conntrack_tcp_timeout_established",
1291 .data = &nf_ct_tcp_timeout_established,
1292 .maxlen = sizeof(unsigned int),
1293 .mode = 0644,
1294 .proc_handler = &proc_dointvec_jiffies,
1295 },
1296 {
1297 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1298 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1299 .data = &nf_ct_tcp_timeout_fin_wait,
1300 .maxlen = sizeof(unsigned int),
1301 .mode = 0644,
1302 .proc_handler = &proc_dointvec_jiffies,
1303 },
1304 {
1305 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1306 .procname = "ip_conntrack_tcp_timeout_close_wait",
1307 .data = &nf_ct_tcp_timeout_close_wait,
1308 .maxlen = sizeof(unsigned int),
1309 .mode = 0644,
1310 .proc_handler = &proc_dointvec_jiffies,
1311 },
1312 {
1313 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1314 .procname = "ip_conntrack_tcp_timeout_last_ack",
1315 .data = &nf_ct_tcp_timeout_last_ack,
1316 .maxlen = sizeof(unsigned int),
1317 .mode = 0644,
1318 .proc_handler = &proc_dointvec_jiffies,
1319 },
1320 {
1321 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1322 .procname = "ip_conntrack_tcp_timeout_time_wait",
1323 .data = &nf_ct_tcp_timeout_time_wait,
1324 .maxlen = sizeof(unsigned int),
1325 .mode = 0644,
1326 .proc_handler = &proc_dointvec_jiffies,
1327 },
1328 {
1329 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1330 .procname = "ip_conntrack_tcp_timeout_close",
1331 .data = &nf_ct_tcp_timeout_close,
1332 .maxlen = sizeof(unsigned int),
1333 .mode = 0644,
1334 .proc_handler = &proc_dointvec_jiffies,
1335 },
1336 {
1337 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1338 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1339 .data = &nf_ct_tcp_timeout_max_retrans,
1340 .maxlen = sizeof(unsigned int),
1341 .mode = 0644,
1342 .proc_handler = &proc_dointvec_jiffies,
1343 },
1344 {
1345 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
1346 .procname = "ip_conntrack_tcp_loose",
1347 .data = &nf_ct_tcp_loose,
1348 .maxlen = sizeof(unsigned int),
1349 .mode = 0644,
1350 .proc_handler = &proc_dointvec,
1351 },
1352 {
1353 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
1354 .procname = "ip_conntrack_tcp_be_liberal",
1355 .data = &nf_ct_tcp_be_liberal,
1356 .maxlen = sizeof(unsigned int),
1357 .mode = 0644,
1358 .proc_handler = &proc_dointvec,
1359 },
1360 {
1361 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
1362 .procname = "ip_conntrack_tcp_max_retrans",
1363 .data = &nf_ct_tcp_max_retrans,
1364 .maxlen = sizeof(unsigned int),
1365 .mode = 0644,
1366 .proc_handler = &proc_dointvec,
1367 },
1368 {
1369 .ctl_name = 0
1370 }
1371};
1372#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +01001373#endif /* CONFIG_SYSCTL */
1374
Martin Josefsson605dcad2006-11-29 02:35:06 +01001375struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001376{
1377 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001378 .l4proto = IPPROTO_TCP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001379 .name = "tcp",
1380 .pkt_to_tuple = tcp_pkt_to_tuple,
1381 .invert_tuple = tcp_invert_tuple,
1382 .print_tuple = tcp_print_tuple,
1383 .print_conntrack = tcp_print_conntrack,
1384 .packet = tcp_packet,
1385 .new = tcp_new,
Patrick McHardy96f6bf82006-04-06 14:19:24 -07001386 .error = tcp_error,
Patrick McHardye281db5c2007-03-04 15:57:25 -08001387#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001388 .to_nfattr = tcp_to_nfattr,
1389 .from_nfattr = nfattr_to_tcp,
1390 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1391 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1392#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +01001393#ifdef CONFIG_SYSCTL
1394 .ctl_table_users = &tcp_sysctl_table_users,
1395 .ctl_table_header = &tcp_sysctl_header,
1396 .ctl_table = tcp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +01001397#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1398 .ctl_compat_table = tcp_compat_sysctl_table,
1399#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +01001400#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001401};
Patrick McHardy13b18332006-12-02 22:11:25 -08001402EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001403
Martin Josefsson605dcad2006-11-29 02:35:06 +01001404struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001405{
1406 .l3proto = PF_INET6,
Martin Josefsson605dcad2006-11-29 02:35:06 +01001407 .l4proto = IPPROTO_TCP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001408 .name = "tcp",
1409 .pkt_to_tuple = tcp_pkt_to_tuple,
1410 .invert_tuple = tcp_invert_tuple,
1411 .print_tuple = tcp_print_tuple,
1412 .print_conntrack = tcp_print_conntrack,
1413 .packet = tcp_packet,
1414 .new = tcp_new,
Patrick McHardy96f6bf82006-04-06 14:19:24 -07001415 .error = tcp_error,
Patrick McHardye281db5c2007-03-04 15:57:25 -08001416#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001417 .to_nfattr = tcp_to_nfattr,
1418 .from_nfattr = nfattr_to_tcp,
1419 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1420 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1421#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +01001422#ifdef CONFIG_SYSCTL
1423 .ctl_table_users = &tcp_sysctl_table_users,
1424 .ctl_table_header = &tcp_sysctl_header,
1425 .ctl_table = tcp_sysctl_table,
1426#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001427};
Patrick McHardy13b18332006-12-02 22:11:25 -08001428EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);