]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/nf_conntrack_proto_tcp.c
[NET] IPV4: Use hton{s,l}() where appropriate.
[net-next-2.6.git] / net / netfilter / nf_conntrack_proto_tcp.c
CommitLineData
9fb9cbb1
YK
1/* (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.
7 *
8 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>:
9 * - Real stateful connection tracking
10 * - Modified state transitions table
11 * - Window scaling support added
12 * - SACK support added
13 *
14 * Willy Tarreau:
15 * - State table bugfixes
16 * - More robust state changes
17 * - Tuning timer parameters
18 *
19 * 27 Oct 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
20 * - genelized Layer 3 protocol part.
21 *
22 * Derived from net/ipv4/netfilter/ip_conntrack_proto_tcp.c
23 *
24 * version 2.2
25 */
26
9fb9cbb1 27#include <linux/types.h>
9fb9cbb1
YK
28#include <linux/timer.h>
29#include <linux/netfilter.h>
30#include <linux/module.h>
31#include <linux/in.h>
32#include <linux/tcp.h>
33#include <linux/spinlock.h>
34#include <linux/skbuff.h>
35#include <linux/ipv6.h>
36#include <net/ip6_checksum.h>
37
38#include <net/tcp.h>
39
40#include <linux/netfilter.h>
41#include <linux/netfilter_ipv4.h>
42#include <linux/netfilter_ipv6.h>
43#include <net/netfilter/nf_conntrack.h>
605dcad6 44#include <net/netfilter/nf_conntrack_l4proto.h>
f6180121 45#include <net/netfilter/nf_conntrack_ecache.h>
9fb9cbb1
YK
46
47#if 0
48#define DEBUGP printk
49#define DEBUGP_VARS
50#else
51#define DEBUGP(format, args...)
52#endif
53
54/* Protects conntrack->proto.tcp */
55static DEFINE_RWLOCK(tcp_lock);
56
601e68e1
YH
57/* "Be conservative in what you do,
58 be liberal in what you accept from others."
9fb9cbb1 59 If it's non-zero, we mark only out of window RST segments as INVALID. */
3aef0fd9 60static int nf_ct_tcp_be_liberal __read_mostly = 0;
9fb9cbb1 61
a09113c2 62/* If it is set to zero, we disable picking up already established
9fb9cbb1 63 connections. */
3aef0fd9 64static int nf_ct_tcp_loose __read_mostly = 1;
9fb9cbb1 65
601e68e1
YH
66/* Max number of the retransmitted packets without receiving an (acceptable)
67 ACK from the destination. If this number is reached, a shorter timer
9fb9cbb1 68 will be started. */
3aef0fd9 69static int nf_ct_tcp_max_retrans __read_mostly = 3;
9fb9cbb1
YK
70
71 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
72 closely. They're more complex. --RR */
73
74static const char *tcp_conntrack_names[] = {
75 "NONE",
76 "SYN_SENT",
77 "SYN_RECV",
78 "ESTABLISHED",
79 "FIN_WAIT",
80 "CLOSE_WAIT",
81 "LAST_ACK",
82 "TIME_WAIT",
83 "CLOSE",
84 "LISTEN"
85};
601e68e1 86
9fb9cbb1
YK
87#define SECS * HZ
88#define MINS * 60 SECS
89#define HOURS * 60 MINS
90#define DAYS * 24 HOURS
91
933a41e7
PM
92static unsigned int nf_ct_tcp_timeout_syn_sent __read_mostly = 2 MINS;
93static unsigned int nf_ct_tcp_timeout_syn_recv __read_mostly = 60 SECS;
94static unsigned int nf_ct_tcp_timeout_established __read_mostly = 5 DAYS;
95static unsigned int nf_ct_tcp_timeout_fin_wait __read_mostly = 2 MINS;
96static unsigned int nf_ct_tcp_timeout_close_wait __read_mostly = 60 SECS;
97static unsigned int nf_ct_tcp_timeout_last_ack __read_mostly = 30 SECS;
98static unsigned int nf_ct_tcp_timeout_time_wait __read_mostly = 2 MINS;
99static unsigned int nf_ct_tcp_timeout_close __read_mostly = 10 SECS;
9fb9cbb1
YK
100
101/* RFC1122 says the R2 limit should be at least 100 seconds.
601e68e1 102 Linux uses 15 packets as limit, which corresponds
9fb9cbb1 103 to ~13-30min depending on RTO. */
933a41e7 104static unsigned int nf_ct_tcp_timeout_max_retrans __read_mostly = 5 MINS;
601e68e1 105
933a41e7
PM
106static unsigned int * tcp_timeouts[] = {
107 NULL, /* TCP_CONNTRACK_NONE */
9fb9cbb1
YK
108 &nf_ct_tcp_timeout_syn_sent, /* TCP_CONNTRACK_SYN_SENT, */
109 &nf_ct_tcp_timeout_syn_recv, /* TCP_CONNTRACK_SYN_RECV, */
110 &nf_ct_tcp_timeout_established, /* TCP_CONNTRACK_ESTABLISHED, */
111 &nf_ct_tcp_timeout_fin_wait, /* TCP_CONNTRACK_FIN_WAIT, */
112 &nf_ct_tcp_timeout_close_wait, /* TCP_CONNTRACK_CLOSE_WAIT, */
113 &nf_ct_tcp_timeout_last_ack, /* TCP_CONNTRACK_LAST_ACK, */
114 &nf_ct_tcp_timeout_time_wait, /* TCP_CONNTRACK_TIME_WAIT, */
115 &nf_ct_tcp_timeout_close, /* TCP_CONNTRACK_CLOSE, */
116 NULL, /* TCP_CONNTRACK_LISTEN */
117 };
601e68e1 118
9fb9cbb1
YK
119#define sNO TCP_CONNTRACK_NONE
120#define sSS TCP_CONNTRACK_SYN_SENT
121#define sSR TCP_CONNTRACK_SYN_RECV
122#define sES TCP_CONNTRACK_ESTABLISHED
123#define sFW TCP_CONNTRACK_FIN_WAIT
124#define sCW TCP_CONNTRACK_CLOSE_WAIT
125#define sLA TCP_CONNTRACK_LAST_ACK
126#define sTW TCP_CONNTRACK_TIME_WAIT
127#define sCL TCP_CONNTRACK_CLOSE
128#define sLI TCP_CONNTRACK_LISTEN
129#define sIV TCP_CONNTRACK_MAX
130#define sIG TCP_CONNTRACK_IGNORE
131
132/* What TCP flags are set from RST/SYN/FIN/ACK. */
133enum tcp_bit_set {
134 TCP_SYN_SET,
135 TCP_SYNACK_SET,
136 TCP_FIN_SET,
137 TCP_ACK_SET,
138 TCP_RST_SET,
139 TCP_NONE_SET,
140};
601e68e1 141
9fb9cbb1
YK
142/*
143 * The TCP state transition table needs a few words...
144 *
145 * We are the man in the middle. All the packets go through us
146 * but might get lost in transit to the destination.
601e68e1 147 * It is assumed that the destinations can't receive segments
9fb9cbb1
YK
148 * we haven't seen.
149 *
150 * The checked segment is in window, but our windows are *not*
151 * equivalent with the ones of the sender/receiver. We always
152 * try to guess the state of the current sender.
153 *
154 * The meaning of the states are:
155 *
156 * NONE: initial state
601e68e1 157 * SYN_SENT: SYN-only packet seen
9fb9cbb1
YK
158 * SYN_RECV: SYN-ACK packet seen
159 * ESTABLISHED: ACK packet seen
160 * FIN_WAIT: FIN packet seen
601e68e1 161 * CLOSE_WAIT: ACK seen (after FIN)
9fb9cbb1
YK
162 * LAST_ACK: FIN seen (after FIN)
163 * TIME_WAIT: last ACK seen
164 * CLOSE: closed connection
165 *
166 * LISTEN state is not used.
167 *
168 * Packets marked as IGNORED (sIG):
601e68e1
YH
169 * if they may be either invalid or valid
170 * and the receiver may send back a connection
9fb9cbb1
YK
171 * closing RST or a SYN/ACK.
172 *
173 * Packets marked as INVALID (sIV):
174 * if they are invalid
175 * or we do not support the request (simultaneous open)
176 */
177static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
178 {
179/* ORIGINAL */
180/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
181/*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV },
182/*
183 * sNO -> sSS Initialize a new connection
184 * sSS -> sSS Retransmitted SYN
185 * sSR -> sIG Late retransmitted SYN?
186 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
601e68e1 187 * are errors. Receiver will reply with RST
9fb9cbb1
YK
188 * and close the connection.
189 * Or we are not in sync and hold a dead connection.
190 * sFW -> sIG
191 * sCW -> sIG
192 * sLA -> sIG
193 * sTW -> sSS Reopened connection (RFC 1122).
194 * sCL -> sSS
195 */
196/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
197/*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
198/*
199 * A SYN/ACK from the client is always invalid:
601e68e1 200 * - either it tries to set up a simultaneous open, which is
9fb9cbb1
YK
201 * not supported;
202 * - or the firewall has just been inserted between the two hosts
601e68e1 203 * during the session set-up. The SYN will be retransmitted
9fb9cbb1
YK
204 * by the true client (or it'll time out).
205 */
206/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
207/*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
208/*
209 * sNO -> sIV Too late and no reason to do anything...
210 * sSS -> sIV Client migth not send FIN in this state:
211 * we enforce waiting for a SYN/ACK reply first.
212 * sSR -> sFW Close started.
213 * sES -> sFW
214 * sFW -> sLA FIN seen in both directions, waiting for
601e68e1 215 * the last ACK.
9fb9cbb1
YK
216 * Migth be a retransmitted FIN as well...
217 * sCW -> sLA
218 * sLA -> sLA Retransmitted FIN. Remain in the same state.
219 * sTW -> sTW
220 * sCL -> sCL
221 */
222/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
223/*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
224/*
225 * sNO -> sES Assumed.
226 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
227 * sSR -> sES Established state is reached.
228 * sES -> sES :-)
229 * sFW -> sCW Normal close request answered by ACK.
230 * sCW -> sCW
231 * sLA -> sTW Last ACK detected.
232 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
233 * sCL -> sCL
234 */
235/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
236/*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
237/*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
238 },
239 {
240/* REPLY */
241/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
242/*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV },
243/*
244 * sNO -> sIV Never reached.
245 * sSS -> sIV Simultaneous open, not supported
246 * sSR -> sIV Simultaneous open, not supported.
247 * sES -> sIV Server may not initiate a connection.
248 * sFW -> sIV
249 * sCW -> sIV
250 * sLA -> sIV
251 * sTW -> sIV Reopened connection, but server may not do it.
252 * sCL -> sIV
253 */
254/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
255/*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV },
256/*
257 * sSS -> sSR Standard open.
258 * sSR -> sSR Retransmitted SYN/ACK.
259 * sES -> sIG Late retransmitted SYN/ACK?
260 * sFW -> sIG Might be SYN/ACK answering ignored SYN
261 * sCW -> sIG
262 * sLA -> sIG
263 * sTW -> sIG
264 * sCL -> sIG
265 */
266/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
267/*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
268/*
269 * sSS -> sIV Server might not send FIN in this state.
270 * sSR -> sFW Close started.
271 * sES -> sFW
272 * sFW -> sLA FIN seen in both directions.
273 * sCW -> sLA
274 * sLA -> sLA Retransmitted FIN.
275 * sTW -> sTW
276 * sCL -> sCL
277 */
278/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
73f30602 279/*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV },
9fb9cbb1 280/*
73f30602 281 * sSS -> sIG Might be a half-open connection.
9fb9cbb1
YK
282 * sSR -> sSR Might answer late resent SYN.
283 * sES -> sES :-)
284 * sFW -> sCW Normal close request answered by ACK.
285 * sCW -> sCW
286 * sLA -> sTW Last ACK detected.
287 * sTW -> sTW Retransmitted last ACK.
288 * sCL -> sCL
289 */
290/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */
291/*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV },
292/*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
601e68e1 293 }
9fb9cbb1
YK
294};
295
296static int tcp_pkt_to_tuple(const struct sk_buff *skb,
297 unsigned int dataoff,
298 struct nf_conntrack_tuple *tuple)
299{
300 struct tcphdr _hdr, *hp;
301
302 /* Actually only need first 8 bytes. */
303 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
304 if (hp == NULL)
305 return 0;
306
307 tuple->src.u.tcp.port = hp->source;
308 tuple->dst.u.tcp.port = hp->dest;
309
310 return 1;
311}
312
313static int tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
314 const struct nf_conntrack_tuple *orig)
315{
316 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
317 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
318 return 1;
319}
320
321/* Print out the per-protocol part of the tuple. */
322static int tcp_print_tuple(struct seq_file *s,
323 const struct nf_conntrack_tuple *tuple)
324{
325 return seq_printf(s, "sport=%hu dport=%hu ",
326 ntohs(tuple->src.u.tcp.port),
327 ntohs(tuple->dst.u.tcp.port));
328}
329
330/* Print out the private part of the conntrack. */
331static int tcp_print_conntrack(struct seq_file *s,
332 const struct nf_conn *conntrack)
333{
334 enum tcp_conntrack state;
335
336 read_lock_bh(&tcp_lock);
337 state = conntrack->proto.tcp.state;
338 read_unlock_bh(&tcp_lock);
339
340 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
341}
342
343static unsigned int get_conntrack_index(const struct tcphdr *tcph)
344{
345 if (tcph->rst) return TCP_RST_SET;
346 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
347 else if (tcph->fin) return TCP_FIN_SET;
348 else if (tcph->ack) return TCP_ACK_SET;
349 else return TCP_NONE_SET;
350}
351
352/* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
353 in IP Filter' by Guido van Rooij.
601e68e1 354
9fb9cbb1
YK
355 http://www.nluug.nl/events/sane2000/papers.html
356 http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz
601e68e1 357
9fb9cbb1
YK
358 The boundaries and the conditions are changed according to RFC793:
359 the packet must intersect the window (i.e. segments may be
360 after the right or before the left edge) and thus receivers may ACK
361 segments after the right edge of the window.
362
601e68e1 363 td_maxend = max(sack + max(win,1)) seen in reply packets
9fb9cbb1
YK
364 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
365 td_maxwin += seq + len - sender.td_maxend
366 if seq + len > sender.td_maxend
367 td_end = max(seq + len) seen in sent packets
601e68e1 368
9fb9cbb1
YK
369 I. Upper bound for valid data: seq <= sender.td_maxend
370 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
371 III. Upper bound for valid ack: sack <= receiver.td_end
372 IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW
373
374 where sack is the highest right edge of sack block found in the packet.
375
601e68e1
YH
376 The upper bound limit for a valid ack is not ignored -
377 we doesn't have to deal with fragments.
9fb9cbb1
YK
378*/
379
380static inline __u32 segment_seq_plus_len(__u32 seq,
381 size_t len,
382 unsigned int dataoff,
383 struct tcphdr *tcph)
384{
385 /* XXX Should I use payload length field in IP/IPv6 header ?
386 * - YK */
387 return (seq + len - dataoff - tcph->doff*4
388 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
389}
601e68e1 390
9fb9cbb1
YK
391/* Fixme: what about big packets? */
392#define MAXACKWINCONST 66000
393#define MAXACKWINDOW(sender) \
394 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
395 : MAXACKWINCONST)
601e68e1 396
9fb9cbb1
YK
397/*
398 * Simplified tcp_parse_options routine from tcp_input.c
399 */
400static void tcp_options(const struct sk_buff *skb,
401 unsigned int dataoff,
601e68e1 402 struct tcphdr *tcph,
9fb9cbb1
YK
403 struct ip_ct_tcp_state *state)
404{
405 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
406 unsigned char *ptr;
407 int length = (tcph->doff*4) - sizeof(struct tcphdr);
408
409 if (!length)
410 return;
411
412 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
413 length, buff);
414 BUG_ON(ptr == NULL);
415
601e68e1 416 state->td_scale =
9fb9cbb1
YK
417 state->flags = 0;
418
419 while (length > 0) {
420 int opcode=*ptr++;
421 int opsize;
422
423 switch (opcode) {
424 case TCPOPT_EOL:
425 return;
426 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
427 length--;
428 continue;
429 default:
430 opsize=*ptr++;
431 if (opsize < 2) /* "silly options" */
432 return;
433 if (opsize > length)
434 break; /* don't parse partial options */
435
601e68e1 436 if (opcode == TCPOPT_SACK_PERM
9fb9cbb1
YK
437 && opsize == TCPOLEN_SACK_PERM)
438 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
439 else if (opcode == TCPOPT_WINDOW
440 && opsize == TCPOLEN_WINDOW) {
441 state->td_scale = *(u_int8_t *)ptr;
442
443 if (state->td_scale > 14) {
444 /* See RFC1323 */
445 state->td_scale = 14;
446 }
447 state->flags |=
448 IP_CT_TCP_FLAG_WINDOW_SCALE;
449 }
450 ptr += opsize - 2;
451 length -= opsize;
452 }
453 }
454}
455
456static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
457 struct tcphdr *tcph, __u32 *sack)
458{
601e68e1 459 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
9fb9cbb1
YK
460 unsigned char *ptr;
461 int length = (tcph->doff*4) - sizeof(struct tcphdr);
462 __u32 tmp;
463
464 if (!length)
465 return;
466
467 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
468 length, buff);
469 BUG_ON(ptr == NULL);
470
471 /* Fast path for timestamp-only option */
472 if (length == TCPOLEN_TSTAMP_ALIGNED*4
bff9a89b 473 && *(__be32 *)ptr ==
601e68e1
YH
474 __constant_htonl((TCPOPT_NOP << 24)
475 | (TCPOPT_NOP << 16)
476 | (TCPOPT_TIMESTAMP << 8)
477 | TCPOLEN_TIMESTAMP))
9fb9cbb1
YK
478 return;
479
480 while (length > 0) {
481 int opcode = *ptr++;
482 int opsize, i;
483
484 switch (opcode) {
485 case TCPOPT_EOL:
486 return;
487 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
488 length--;
489 continue;
490 default:
491 opsize = *ptr++;
492 if (opsize < 2) /* "silly options" */
493 return;
494 if (opsize > length)
495 break; /* don't parse partial options */
496
601e68e1
YH
497 if (opcode == TCPOPT_SACK
498 && opsize >= (TCPOLEN_SACK_BASE
499 + TCPOLEN_SACK_PERBLOCK)
500 && !((opsize - TCPOLEN_SACK_BASE)
501 % TCPOLEN_SACK_PERBLOCK)) {
502 for (i = 0;
503 i < (opsize - TCPOLEN_SACK_BASE);
504 i += TCPOLEN_SACK_PERBLOCK) {
505 tmp = ntohl(*((__be32 *)(ptr+i)+1));
9fb9cbb1
YK
506
507 if (after(tmp, *sack))
508 *sack = tmp;
509 }
510 return;
511 }
512 ptr += opsize - 2;
513 length -= opsize;
514 }
515 }
516}
517
601e68e1
YH
518static int tcp_in_window(struct ip_ct_tcp *state,
519 enum ip_conntrack_dir dir,
520 unsigned int index,
521 const struct sk_buff *skb,
9fb9cbb1 522 unsigned int dataoff,
601e68e1 523 struct tcphdr *tcph,
9fb9cbb1
YK
524 int pf)
525{
526 struct ip_ct_tcp_state *sender = &state->seen[dir];
527 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
528 __u32 seq, ack, sack, end, win, swin;
529 int res;
530
531 /*
532 * Get the required data from the packet.
533 */
534 seq = ntohl(tcph->seq);
535 ack = sack = ntohl(tcph->ack_seq);
536 win = ntohs(tcph->window);
537 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
538
539 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
540 tcp_sack(skb, dataoff, tcph, &sack);
541
542 DEBUGP("tcp_in_window: START\n");
543 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
544 "seq=%u ack=%u sack=%u win=%u end=%u\n",
601e68e1 545 NIPQUAD(iph->saddr), ntohs(tcph->source),
9fb9cbb1
YK
546 NIPQUAD(iph->daddr), ntohs(tcph->dest),
547 seq, ack, sack, win, end);
548 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
549 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
550 sender->td_end, sender->td_maxend, sender->td_maxwin,
601e68e1
YH
551 sender->td_scale,
552 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
9fb9cbb1
YK
553 receiver->td_scale);
554
555 if (sender->td_end == 0) {
556 /*
557 * Initialize sender data.
558 */
559 if (tcph->syn && tcph->ack) {
560 /*
561 * Outgoing SYN-ACK in reply to a SYN.
562 */
601e68e1 563 sender->td_end =
9fb9cbb1
YK
564 sender->td_maxend = end;
565 sender->td_maxwin = (win == 0 ? 1 : win);
566
567 tcp_options(skb, dataoff, tcph, sender);
601e68e1 568 /*
9fb9cbb1
YK
569 * RFC 1323:
570 * Both sides must send the Window Scale option
571 * to enable window scaling in either direction.
572 */
573 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
574 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
601e68e1 575 sender->td_scale =
9fb9cbb1
YK
576 receiver->td_scale = 0;
577 } else {
578 /*
579 * We are in the middle of a connection,
580 * its history is lost for us.
581 * Let's try to use the data from the packet.
601e68e1 582 */
9fb9cbb1
YK
583 sender->td_end = end;
584 sender->td_maxwin = (win == 0 ? 1 : win);
585 sender->td_maxend = end + sender->td_maxwin;
586 }
587 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
588 && dir == IP_CT_DIR_ORIGINAL)
589 || (state->state == TCP_CONNTRACK_SYN_RECV
590 && dir == IP_CT_DIR_REPLY))
591 && after(end, sender->td_end)) {
592 /*
593 * RFC 793: "if a TCP is reinitialized ... then it need
601e68e1 594 * not wait at all; it must only be sure to use sequence
9fb9cbb1
YK
595 * numbers larger than those recently used."
596 */
597 sender->td_end =
598 sender->td_maxend = end;
599 sender->td_maxwin = (win == 0 ? 1 : win);
600
601 tcp_options(skb, dataoff, tcph, sender);
602 }
603
604 if (!(tcph->ack)) {
605 /*
606 * If there is no ACK, just pretend it was set and OK.
607 */
608 ack = sack = receiver->td_end;
601e68e1
YH
609 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
610 (TCP_FLAG_ACK|TCP_FLAG_RST))
9fb9cbb1
YK
611 && (ack == 0)) {
612 /*
613 * Broken TCP stacks, that set ACK in RST packets as well
614 * with zero ack value.
615 */
616 ack = sack = receiver->td_end;
617 }
618
619 if (seq == end
620 && (!tcph->rst
621 || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)))
622 /*
623 * Packets contains no data: we assume it is valid
624 * and check the ack value only.
625 * However RST segments are always validated by their
626 * SEQ number, except when seq == 0 (reset sent answering
627 * SYN.
628 */
629 seq = end = sender->td_end;
630
631 DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
632 "seq=%u ack=%u sack =%u win=%u end=%u\n",
633 NIPQUAD(iph->saddr), ntohs(tcph->source),
634 NIPQUAD(iph->daddr), ntohs(tcph->dest),
635 seq, ack, sack, win, end);
636 DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
637 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
638 sender->td_end, sender->td_maxend, sender->td_maxwin,
601e68e1 639 sender->td_scale,
9fb9cbb1
YK
640 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
641 receiver->td_scale);
642
643 DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
644 before(seq, sender->td_maxend + 1),
645 after(end, sender->td_end - receiver->td_maxwin - 1),
601e68e1
YH
646 before(sack, receiver->td_end + 1),
647 after(ack, receiver->td_end - MAXACKWINDOW(sender)));
9fb9cbb1 648
a09113c2
PM
649 if (before(seq, sender->td_maxend + 1) &&
650 after(end, sender->td_end - receiver->td_maxwin - 1) &&
651 before(sack, receiver->td_end + 1) &&
652 after(ack, receiver->td_end - MAXACKWINDOW(sender))) {
601e68e1 653 /*
9fb9cbb1
YK
654 * Take into account window scaling (RFC 1323).
655 */
656 if (!tcph->syn)
657 win <<= sender->td_scale;
658
659 /*
660 * Update sender data.
661 */
662 swin = win + (sack - ack);
663 if (sender->td_maxwin < swin)
664 sender->td_maxwin = swin;
665 if (after(end, sender->td_end))
666 sender->td_end = end;
667 /*
668 * Update receiver data.
669 */
670 if (after(end, sender->td_maxend))
671 receiver->td_maxwin += end - sender->td_maxend;
672 if (after(sack + win, receiver->td_maxend - 1)) {
673 receiver->td_maxend = sack + win;
674 if (win == 0)
675 receiver->td_maxend++;
676 }
677
601e68e1 678 /*
9fb9cbb1
YK
679 * Check retransmissions.
680 */
681 if (index == TCP_ACK_SET) {
682 if (state->last_dir == dir
683 && state->last_seq == seq
684 && state->last_ack == ack
c1fe3ca5
GH
685 && state->last_end == end
686 && state->last_win == win)
9fb9cbb1
YK
687 state->retrans++;
688 else {
689 state->last_dir = dir;
690 state->last_seq = seq;
691 state->last_ack = ack;
692 state->last_end = end;
c1fe3ca5 693 state->last_win = win;
9fb9cbb1
YK
694 state->retrans = 0;
695 }
696 }
9fb9cbb1
YK
697 res = 1;
698 } else {
a09113c2
PM
699 res = 0;
700 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
701 nf_ct_tcp_be_liberal)
702 res = 1;
703 if (!res && LOG_INVALID(IPPROTO_TCP))
9fb9cbb1
YK
704 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
705 "nf_ct_tcp: %s ",
706 before(seq, sender->td_maxend + 1) ?
707 after(end, sender->td_end - receiver->td_maxwin - 1) ?
708 before(sack, receiver->td_end + 1) ?
709 after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG"
710 : "ACK is under the lower bound (possible overly delayed ACK)"
711 : "ACK is over the upper bound (ACKed data not seen yet)"
712 : "SEQ is under the lower bound (already ACKed data retransmitted)"
713 : "SEQ is over the upper bound (over the window of the receiver)");
601e68e1
YH
714 }
715
9fb9cbb1
YK
716 DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u "
717 "receiver end=%u maxend=%u maxwin=%u\n",
601e68e1 718 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
9fb9cbb1
YK
719 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
720
721 return res;
722}
723
5b1158e9 724#ifdef CONFIG_NF_NAT_NEEDED
9fb9cbb1
YK
725/* Update sender->td_end after NAT successfully mangled the packet */
726/* Caller must linearize skb at tcp header. */
727void nf_conntrack_tcp_update(struct sk_buff *skb,
728 unsigned int dataoff,
601e68e1 729 struct nf_conn *conntrack,
9fb9cbb1
YK
730 int dir)
731{
732 struct tcphdr *tcph = (void *)skb->data + dataoff;
733 __u32 end;
734#ifdef DEBUGP_VARS
735 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir];
736 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir];
737#endif
738
739 end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph);
740
741 write_lock_bh(&tcp_lock);
742 /*
743 * We have to worry for the ack in the reply packet only...
744 */
745 if (after(end, conntrack->proto.tcp.seen[dir].td_end))
746 conntrack->proto.tcp.seen[dir].td_end = end;
747 conntrack->proto.tcp.last_end = end;
748 write_unlock_bh(&tcp_lock);
749 DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i "
750 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
751 sender->td_end, sender->td_maxend, sender->td_maxwin,
601e68e1 752 sender->td_scale,
9fb9cbb1
YK
753 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
754 receiver->td_scale);
755}
13b18339 756EXPORT_SYMBOL_GPL(nf_conntrack_tcp_update);
9fb9cbb1
YK
757#endif
758
759#define TH_FIN 0x01
760#define TH_SYN 0x02
761#define TH_RST 0x04
762#define TH_PUSH 0x08
763#define TH_ACK 0x10
764#define TH_URG 0x20
765#define TH_ECE 0x40
766#define TH_CWR 0x80
767
768/* table of valid flag combinations - ECE and CWR are always valid */
769static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] =
770{
771 [TH_SYN] = 1,
a2d7222f 772 [TH_SYN|TH_PUSH] = 1,
d3ab4298
PM
773 [TH_SYN|TH_URG] = 1,
774 [TH_SYN|TH_PUSH|TH_URG] = 1,
775 [TH_SYN|TH_ACK] = 1,
9fb9cbb1
YK
776 [TH_SYN|TH_ACK|TH_PUSH] = 1,
777 [TH_RST] = 1,
778 [TH_RST|TH_ACK] = 1,
779 [TH_RST|TH_ACK|TH_PUSH] = 1,
780 [TH_FIN|TH_ACK] = 1,
781 [TH_ACK] = 1,
782 [TH_ACK|TH_PUSH] = 1,
783 [TH_ACK|TH_URG] = 1,
784 [TH_ACK|TH_URG|TH_PUSH] = 1,
785 [TH_FIN|TH_ACK|TH_PUSH] = 1,
786 [TH_FIN|TH_ACK|TH_URG] = 1,
787 [TH_FIN|TH_ACK|TH_URG|TH_PUSH] = 1,
788};
789
790/* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
791static int tcp_error(struct sk_buff *skb,
792 unsigned int dataoff,
793 enum ip_conntrack_info *ctinfo,
794 int pf,
96f6bf82 795 unsigned int hooknum)
9fb9cbb1
YK
796{
797 struct tcphdr _tcph, *th;
798 unsigned int tcplen = skb->len - dataoff;
799 u_int8_t tcpflags;
800
801 /* Smaller that minimal TCP header? */
802 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
803 if (th == NULL) {
804 if (LOG_INVALID(IPPROTO_TCP))
805 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
806 "nf_ct_tcp: short packet ");
807 return -NF_ACCEPT;
601e68e1
YH
808 }
809
9fb9cbb1
YK
810 /* Not whole TCP header or malformed packet */
811 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
812 if (LOG_INVALID(IPPROTO_TCP))
813 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
814 "nf_ct_tcp: truncated/malformed packet ");
815 return -NF_ACCEPT;
816 }
601e68e1 817
9fb9cbb1
YK
818 /* Checksum invalid? Ignore.
819 * We skip checking packets on the outgoing path
84fa7933 820 * because the checksum is assumed to be correct.
9fb9cbb1
YK
821 */
822 /* FIXME: Source route IP option packets --RR */
39a27a35
PM
823 if (nf_conntrack_checksum &&
824 ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) ||
825 (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) &&
96f6bf82 826 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
9fb9cbb1
YK
827 if (LOG_INVALID(IPPROTO_TCP))
828 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
829 "nf_ct_tcp: bad TCP checksum ");
830 return -NF_ACCEPT;
831 }
832
833 /* Check TCP flags. */
834 tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR));
835 if (!tcp_valid_flags[tcpflags]) {
836 if (LOG_INVALID(IPPROTO_TCP))
837 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
838 "nf_ct_tcp: invalid TCP flag combination ");
839 return -NF_ACCEPT;
840 }
841
842 return NF_ACCEPT;
843}
844
9fb9cbb1
YK
845/* Returns verdict for packet, or -1 for invalid. */
846static int tcp_packet(struct nf_conn *conntrack,
847 const struct sk_buff *skb,
848 unsigned int dataoff,
849 enum ip_conntrack_info ctinfo,
850 int pf,
851 unsigned int hooknum)
852{
853 enum tcp_conntrack new_state, old_state;
854 enum ip_conntrack_dir dir;
855 struct tcphdr *th, _tcph;
856 unsigned long timeout;
857 unsigned int index;
858
859 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
860 BUG_ON(th == NULL);
861
862 write_lock_bh(&tcp_lock);
863 old_state = conntrack->proto.tcp.state;
864 dir = CTINFO2DIR(ctinfo);
865 index = get_conntrack_index(th);
866 new_state = tcp_conntracks[dir][index][old_state];
867
868 switch (new_state) {
869 case TCP_CONNTRACK_IGNORE:
73f30602
JK
870 /* Ignored packets:
871 *
872 * a) SYN in ORIGINAL
873 * b) SYN/ACK in REPLY
601e68e1 874 * c) ACK in reply direction after initial SYN in original.
73f30602 875 */
9fb9cbb1
YK
876 if (index == TCP_SYNACK_SET
877 && conntrack->proto.tcp.last_index == TCP_SYN_SET
878 && conntrack->proto.tcp.last_dir != dir
879 && ntohl(th->ack_seq) ==
601e68e1
YH
880 conntrack->proto.tcp.last_end) {
881 /* This SYN/ACK acknowledges a SYN that we earlier
9fb9cbb1
YK
882 * ignored as invalid. This means that the client and
883 * the server are both in sync, while the firewall is
884 * not. We kill this session and block the SYN/ACK so
601e68e1 885 * that the client cannot but retransmit its SYN and
9fb9cbb1
YK
886 * thus initiate a clean new session.
887 */
601e68e1 888 write_unlock_bh(&tcp_lock);
9fb9cbb1
YK
889 if (LOG_INVALID(IPPROTO_TCP))
890 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
891 "nf_ct_tcp: killing out of sync session ");
601e68e1
YH
892 if (del_timer(&conntrack->timeout))
893 conntrack->timeout.function((unsigned long)
894 conntrack);
895 return -NF_DROP;
9fb9cbb1
YK
896 }
897 conntrack->proto.tcp.last_index = index;
898 conntrack->proto.tcp.last_dir = dir;
899 conntrack->proto.tcp.last_seq = ntohl(th->seq);
900 conntrack->proto.tcp.last_end =
901 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
902
903 write_unlock_bh(&tcp_lock);
904 if (LOG_INVALID(IPPROTO_TCP))
905 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
906 "nf_ct_tcp: invalid packed ignored ");
907 return NF_ACCEPT;
908 case TCP_CONNTRACK_MAX:
909 /* Invalid packet */
910 DEBUGP("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
911 dir, get_conntrack_index(th),
912 old_state);
913 write_unlock_bh(&tcp_lock);
914 if (LOG_INVALID(IPPROTO_TCP))
915 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
916 "nf_ct_tcp: invalid state ");
917 return -NF_ACCEPT;
918 case TCP_CONNTRACK_SYN_SENT:
919 if (old_state < TCP_CONNTRACK_TIME_WAIT)
920 break;
921 if ((conntrack->proto.tcp.seen[dir].flags &
922 IP_CT_TCP_FLAG_CLOSE_INIT)
923 || after(ntohl(th->seq),
924 conntrack->proto.tcp.seen[dir].td_end)) {
601e68e1
YH
925 /* Attempt to reopen a closed connection.
926 * Delete this connection and look up again. */
927 write_unlock_bh(&tcp_lock);
928 if (del_timer(&conntrack->timeout))
929 conntrack->timeout.function((unsigned long)
930 conntrack);
931 return -NF_REPEAT;
3746a2b1
KK
932 } else {
933 write_unlock_bh(&tcp_lock);
934 if (LOG_INVALID(IPPROTO_TCP))
935 nf_log_packet(pf, 0, skb, NULL, NULL,
936 NULL, "nf_ct_tcp: invalid SYN");
937 return -NF_ACCEPT;
9fb9cbb1
YK
938 }
939 case TCP_CONNTRACK_CLOSE:
940 if (index == TCP_RST_SET
73f30602 941 && ((test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)
601e68e1
YH
942 && conntrack->proto.tcp.last_index == TCP_SYN_SET)
943 || (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
944 && conntrack->proto.tcp.last_index == TCP_ACK_SET))
9fb9cbb1 945 && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) {
93b1fae4 946 /* RST sent to invalid SYN or ACK we had let through
73f30602
JK
947 * at a) and c) above:
948 *
949 * a) SYN was in window then
950 * c) we hold a half-open connection.
951 *
952 * Delete our connection entry.
9fb9cbb1 953 * We skip window checking, because packet might ACK
73f30602 954 * segments we ignored. */
9fb9cbb1
YK
955 goto in_window;
956 }
93b1fae4 957 /* Just fall through */
9fb9cbb1
YK
958 default:
959 /* Keep compilers happy. */
960 break;
961 }
962
963 if (!tcp_in_window(&conntrack->proto.tcp, dir, index,
964 skb, dataoff, th, pf)) {
965 write_unlock_bh(&tcp_lock);
966 return -NF_ACCEPT;
967 }
968 in_window:
969 /* From now on we have got in-window packets */
970 conntrack->proto.tcp.last_index = index;
971
972 DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu "
973 "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
974 NIPQUAD(iph->saddr), ntohs(th->source),
975 NIPQUAD(iph->daddr), ntohs(th->dest),
976 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
977 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
978 old_state, new_state);
979
980 conntrack->proto.tcp.state = new_state;
981 if (old_state != new_state
982 && (new_state == TCP_CONNTRACK_FIN_WAIT
983 || new_state == TCP_CONNTRACK_CLOSE))
984 conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
985 timeout = conntrack->proto.tcp.retrans >= nf_ct_tcp_max_retrans
986 && *tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans
987 ? nf_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state];
988 write_unlock_bh(&tcp_lock);
989
990 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
991 if (new_state != old_state)
992 nf_conntrack_event_cache(IPCT_PROTOINFO, skb);
993
994 if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
995 /* If only reply is a RST, we can consider ourselves not to
996 have an established connection: this is a fairly common
997 problem case, so we can delete the conntrack
998 immediately. --RR */
999 if (th->rst) {
1000 if (del_timer(&conntrack->timeout))
1001 conntrack->timeout.function((unsigned long)
1002 conntrack);
1003 return NF_ACCEPT;
1004 }
1005 } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status)
1006 && (old_state == TCP_CONNTRACK_SYN_RECV
1007 || old_state == TCP_CONNTRACK_ESTABLISHED)
1008 && new_state == TCP_CONNTRACK_ESTABLISHED) {
601e68e1
YH
1009 /* Set ASSURED if we see see valid ack in ESTABLISHED
1010 after SYN_RECV or a valid answer for a picked up
9fb9cbb1
YK
1011 connection. */
1012 set_bit(IPS_ASSURED_BIT, &conntrack->status);
1013 nf_conntrack_event_cache(IPCT_STATUS, skb);
1014 }
1015 nf_ct_refresh_acct(conntrack, ctinfo, skb, timeout);
1016
1017 return NF_ACCEPT;
1018}
601e68e1 1019
9fb9cbb1
YK
1020/* Called when a new connection for this protocol found. */
1021static int tcp_new(struct nf_conn *conntrack,
1022 const struct sk_buff *skb,
1023 unsigned int dataoff)
1024{
1025 enum tcp_conntrack new_state;
1026 struct tcphdr *th, _tcph;
1027#ifdef DEBUGP_VARS
1028 struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0];
1029 struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1];
1030#endif
1031
1032 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1033 BUG_ON(th == NULL);
1034
1035 /* Don't need lock here: this conntrack not in circulation yet */
1036 new_state
1037 = tcp_conntracks[0][get_conntrack_index(th)]
1038 [TCP_CONNTRACK_NONE];
1039
1040 /* Invalid: delete conntrack */
1041 if (new_state >= TCP_CONNTRACK_MAX) {
1042 DEBUGP("nf_ct_tcp: invalid new deleting.\n");
1043 return 0;
1044 }
1045
1046 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1047 /* SYN packet */
1048 conntrack->proto.tcp.seen[0].td_end =
1049 segment_seq_plus_len(ntohl(th->seq), skb->len,
1050 dataoff, th);
1051 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1052 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1053 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1054 conntrack->proto.tcp.seen[0].td_maxend =
1055 conntrack->proto.tcp.seen[0].td_end;
1056
1057 tcp_options(skb, dataoff, th, &conntrack->proto.tcp.seen[0]);
1058 conntrack->proto.tcp.seen[1].flags = 0;
9fb9cbb1
YK
1059 } else if (nf_ct_tcp_loose == 0) {
1060 /* Don't try to pick up connections. */
1061 return 0;
1062 } else {
1063 /*
1064 * We are in the middle of a connection,
1065 * its history is lost for us.
1066 * Let's try to use the data from the packet.
1067 */
1068 conntrack->proto.tcp.seen[0].td_end =
1069 segment_seq_plus_len(ntohl(th->seq), skb->len,
1070 dataoff, th);
1071 conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1072 if (conntrack->proto.tcp.seen[0].td_maxwin == 0)
1073 conntrack->proto.tcp.seen[0].td_maxwin = 1;
1074 conntrack->proto.tcp.seen[0].td_maxend =
601e68e1 1075 conntrack->proto.tcp.seen[0].td_end +
9fb9cbb1
YK
1076 conntrack->proto.tcp.seen[0].td_maxwin;
1077 conntrack->proto.tcp.seen[0].td_scale = 0;
1078
a09113c2
PM
1079 /* We assume SACK and liberal window checking to handle
1080 * window scaling */
9fb9cbb1 1081 conntrack->proto.tcp.seen[0].flags =
a09113c2
PM
1082 conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1083 IP_CT_TCP_FLAG_BE_LIBERAL;
9fb9cbb1 1084 }
601e68e1 1085
9fb9cbb1
YK
1086 conntrack->proto.tcp.seen[1].td_end = 0;
1087 conntrack->proto.tcp.seen[1].td_maxend = 0;
1088 conntrack->proto.tcp.seen[1].td_maxwin = 1;
601e68e1 1089 conntrack->proto.tcp.seen[1].td_scale = 0;
9fb9cbb1
YK
1090
1091 /* tcp_packet will set them */
1092 conntrack->proto.tcp.state = TCP_CONNTRACK_NONE;
1093 conntrack->proto.tcp.last_index = TCP_NONE_SET;
601e68e1 1094
9fb9cbb1
YK
1095 DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1096 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1097 sender->td_end, sender->td_maxend, sender->td_maxwin,
601e68e1 1098 sender->td_scale,
9fb9cbb1
YK
1099 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1100 receiver->td_scale);
1101 return 1;
1102}
c1d10adb 1103
e281db5c 1104#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
1105
1106#include <linux/netfilter/nfnetlink.h>
1107#include <linux/netfilter/nfnetlink_conntrack.h>
1108
1109static int tcp_to_nfattr(struct sk_buff *skb, struct nfattr *nfa,
1110 const struct nf_conn *ct)
1111{
1112 struct nfattr *nest_parms;
601e68e1 1113
c1d10adb
PNA
1114 read_lock_bh(&tcp_lock);
1115 nest_parms = NFA_NEST(skb, CTA_PROTOINFO_TCP);
1116 NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t),
1117 &ct->proto.tcp.state);
1118 read_unlock_bh(&tcp_lock);
1119
1120 NFA_NEST_END(skb, nest_parms);
1121
1122 return 0;
1123
1124nfattr_failure:
1125 read_unlock_bh(&tcp_lock);
1126 return -1;
1127}
1128
1129static const size_t cta_min_tcp[CTA_PROTOINFO_TCP_MAX] = {
1130 [CTA_PROTOINFO_TCP_STATE-1] = sizeof(u_int8_t),
1131};
1132
1133static int nfattr_to_tcp(struct nfattr *cda[], struct nf_conn *ct)
1134{
1135 struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1];
1136 struct nfattr *tb[CTA_PROTOINFO_TCP_MAX];
1137
1138 /* updates could not contain anything about the private
1139 * protocol info, in that case skip the parsing */
1140 if (!attr)
1141 return 0;
1142
601e68e1 1143 nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr);
c1d10adb
PNA
1144
1145 if (nfattr_bad_size(tb, CTA_PROTOINFO_TCP_MAX, cta_min_tcp))
1146 return -EINVAL;
1147
1148 if (!tb[CTA_PROTOINFO_TCP_STATE-1])
1149 return -EINVAL;
1150
1151 write_lock_bh(&tcp_lock);
601e68e1 1152 ct->proto.tcp.state =
c1d10adb
PNA
1153 *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]);
1154 write_unlock_bh(&tcp_lock);
1155
1156 return 0;
1157}
1158#endif
933a41e7
PM
1159
1160#ifdef CONFIG_SYSCTL
1161static unsigned int tcp_sysctl_table_users;
1162static struct ctl_table_header *tcp_sysctl_header;
1163static struct ctl_table tcp_sysctl_table[] = {
1164 {
1165 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1166 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1167 .data = &nf_ct_tcp_timeout_syn_sent,
1168 .maxlen = sizeof(unsigned int),
1169 .mode = 0644,
1170 .proc_handler = &proc_dointvec_jiffies,
1171 },
1172 {
1173 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1174 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1175 .data = &nf_ct_tcp_timeout_syn_recv,
1176 .maxlen = sizeof(unsigned int),
1177 .mode = 0644,
1178 .proc_handler = &proc_dointvec_jiffies,
1179 },
1180 {
1181 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1182 .procname = "nf_conntrack_tcp_timeout_established",
1183 .data = &nf_ct_tcp_timeout_established,
1184 .maxlen = sizeof(unsigned int),
1185 .mode = 0644,
1186 .proc_handler = &proc_dointvec_jiffies,
1187 },
1188 {
1189 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1190 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1191 .data = &nf_ct_tcp_timeout_fin_wait,
1192 .maxlen = sizeof(unsigned int),
1193 .mode = 0644,
1194 .proc_handler = &proc_dointvec_jiffies,
1195 },
1196 {
1197 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1198 .procname = "nf_conntrack_tcp_timeout_close_wait",
1199 .data = &nf_ct_tcp_timeout_close_wait,
1200 .maxlen = sizeof(unsigned int),
1201 .mode = 0644,
1202 .proc_handler = &proc_dointvec_jiffies,
1203 },
1204 {
1205 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1206 .procname = "nf_conntrack_tcp_timeout_last_ack",
1207 .data = &nf_ct_tcp_timeout_last_ack,
1208 .maxlen = sizeof(unsigned int),
1209 .mode = 0644,
1210 .proc_handler = &proc_dointvec_jiffies,
1211 },
1212 {
1213 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1214 .procname = "nf_conntrack_tcp_timeout_time_wait",
1215 .data = &nf_ct_tcp_timeout_time_wait,
1216 .maxlen = sizeof(unsigned int),
1217 .mode = 0644,
1218 .proc_handler = &proc_dointvec_jiffies,
1219 },
1220 {
1221 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1222 .procname = "nf_conntrack_tcp_timeout_close",
1223 .data = &nf_ct_tcp_timeout_close,
1224 .maxlen = sizeof(unsigned int),
1225 .mode = 0644,
1226 .proc_handler = &proc_dointvec_jiffies,
1227 },
1228 {
1229 .ctl_name = NET_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1230 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1231 .data = &nf_ct_tcp_timeout_max_retrans,
1232 .maxlen = sizeof(unsigned int),
1233 .mode = 0644,
1234 .proc_handler = &proc_dointvec_jiffies,
1235 },
1236 {
1237 .ctl_name = NET_NF_CONNTRACK_TCP_LOOSE,
1238 .procname = "nf_conntrack_tcp_loose",
1239 .data = &nf_ct_tcp_loose,
1240 .maxlen = sizeof(unsigned int),
1241 .mode = 0644,
1242 .proc_handler = &proc_dointvec,
1243 },
1244 {
1245 .ctl_name = NET_NF_CONNTRACK_TCP_BE_LIBERAL,
1246 .procname = "nf_conntrack_tcp_be_liberal",
1247 .data = &nf_ct_tcp_be_liberal,
1248 .maxlen = sizeof(unsigned int),
1249 .mode = 0644,
1250 .proc_handler = &proc_dointvec,
1251 },
1252 {
1253 .ctl_name = NET_NF_CONNTRACK_TCP_MAX_RETRANS,
1254 .procname = "nf_conntrack_tcp_max_retrans",
1255 .data = &nf_ct_tcp_max_retrans,
1256 .maxlen = sizeof(unsigned int),
1257 .mode = 0644,
1258 .proc_handler = &proc_dointvec,
1259 },
1260 {
1261 .ctl_name = 0
1262 }
1263};
a999e683
PM
1264
1265#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1266static struct ctl_table tcp_compat_sysctl_table[] = {
1267 {
1268 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_SENT,
1269 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1270 .data = &nf_ct_tcp_timeout_syn_sent,
1271 .maxlen = sizeof(unsigned int),
1272 .mode = 0644,
1273 .proc_handler = &proc_dointvec_jiffies,
1274 },
1275 {
1276 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_SYN_RECV,
1277 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1278 .data = &nf_ct_tcp_timeout_syn_recv,
1279 .maxlen = sizeof(unsigned int),
1280 .mode = 0644,
1281 .proc_handler = &proc_dointvec_jiffies,
1282 },
1283 {
1284 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_ESTABLISHED,
1285 .procname = "ip_conntrack_tcp_timeout_established",
1286 .data = &nf_ct_tcp_timeout_established,
1287 .maxlen = sizeof(unsigned int),
1288 .mode = 0644,
1289 .proc_handler = &proc_dointvec_jiffies,
1290 },
1291 {
1292 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_FIN_WAIT,
1293 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1294 .data = &nf_ct_tcp_timeout_fin_wait,
1295 .maxlen = sizeof(unsigned int),
1296 .mode = 0644,
1297 .proc_handler = &proc_dointvec_jiffies,
1298 },
1299 {
1300 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE_WAIT,
1301 .procname = "ip_conntrack_tcp_timeout_close_wait",
1302 .data = &nf_ct_tcp_timeout_close_wait,
1303 .maxlen = sizeof(unsigned int),
1304 .mode = 0644,
1305 .proc_handler = &proc_dointvec_jiffies,
1306 },
1307 {
1308 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_LAST_ACK,
1309 .procname = "ip_conntrack_tcp_timeout_last_ack",
1310 .data = &nf_ct_tcp_timeout_last_ack,
1311 .maxlen = sizeof(unsigned int),
1312 .mode = 0644,
1313 .proc_handler = &proc_dointvec_jiffies,
1314 },
1315 {
1316 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_TIME_WAIT,
1317 .procname = "ip_conntrack_tcp_timeout_time_wait",
1318 .data = &nf_ct_tcp_timeout_time_wait,
1319 .maxlen = sizeof(unsigned int),
1320 .mode = 0644,
1321 .proc_handler = &proc_dointvec_jiffies,
1322 },
1323 {
1324 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_CLOSE,
1325 .procname = "ip_conntrack_tcp_timeout_close",
1326 .data = &nf_ct_tcp_timeout_close,
1327 .maxlen = sizeof(unsigned int),
1328 .mode = 0644,
1329 .proc_handler = &proc_dointvec_jiffies,
1330 },
1331 {
1332 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_TIMEOUT_MAX_RETRANS,
1333 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1334 .data = &nf_ct_tcp_timeout_max_retrans,
1335 .maxlen = sizeof(unsigned int),
1336 .mode = 0644,
1337 .proc_handler = &proc_dointvec_jiffies,
1338 },
1339 {
1340 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_LOOSE,
1341 .procname = "ip_conntrack_tcp_loose",
1342 .data = &nf_ct_tcp_loose,
1343 .maxlen = sizeof(unsigned int),
1344 .mode = 0644,
1345 .proc_handler = &proc_dointvec,
1346 },
1347 {
1348 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL,
1349 .procname = "ip_conntrack_tcp_be_liberal",
1350 .data = &nf_ct_tcp_be_liberal,
1351 .maxlen = sizeof(unsigned int),
1352 .mode = 0644,
1353 .proc_handler = &proc_dointvec,
1354 },
1355 {
1356 .ctl_name = NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS,
1357 .procname = "ip_conntrack_tcp_max_retrans",
1358 .data = &nf_ct_tcp_max_retrans,
1359 .maxlen = sizeof(unsigned int),
1360 .mode = 0644,
1361 .proc_handler = &proc_dointvec,
1362 },
1363 {
1364 .ctl_name = 0
1365 }
1366};
1367#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7
PM
1368#endif /* CONFIG_SYSCTL */
1369
605dcad6 1370struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 =
9fb9cbb1
YK
1371{
1372 .l3proto = PF_INET,
605dcad6 1373 .l4proto = IPPROTO_TCP,
9fb9cbb1
YK
1374 .name = "tcp",
1375 .pkt_to_tuple = tcp_pkt_to_tuple,
1376 .invert_tuple = tcp_invert_tuple,
1377 .print_tuple = tcp_print_tuple,
1378 .print_conntrack = tcp_print_conntrack,
1379 .packet = tcp_packet,
1380 .new = tcp_new,
96f6bf82 1381 .error = tcp_error,
e281db5c 1382#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
1383 .to_nfattr = tcp_to_nfattr,
1384 .from_nfattr = nfattr_to_tcp,
1385 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1386 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1387#endif
933a41e7
PM
1388#ifdef CONFIG_SYSCTL
1389 .ctl_table_users = &tcp_sysctl_table_users,
1390 .ctl_table_header = &tcp_sysctl_header,
1391 .ctl_table = tcp_sysctl_table,
a999e683
PM
1392#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1393 .ctl_compat_table = tcp_compat_sysctl_table,
1394#endif
933a41e7 1395#endif
9fb9cbb1 1396};
13b18339 1397EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
9fb9cbb1 1398
605dcad6 1399struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 =
9fb9cbb1
YK
1400{
1401 .l3proto = PF_INET6,
605dcad6 1402 .l4proto = IPPROTO_TCP,
9fb9cbb1
YK
1403 .name = "tcp",
1404 .pkt_to_tuple = tcp_pkt_to_tuple,
1405 .invert_tuple = tcp_invert_tuple,
1406 .print_tuple = tcp_print_tuple,
1407 .print_conntrack = tcp_print_conntrack,
1408 .packet = tcp_packet,
1409 .new = tcp_new,
96f6bf82 1410 .error = tcp_error,
e281db5c 1411#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
1412 .to_nfattr = tcp_to_nfattr,
1413 .from_nfattr = nfattr_to_tcp,
1414 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
1415 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
1416#endif
933a41e7
PM
1417#ifdef CONFIG_SYSCTL
1418 .ctl_table_users = &tcp_sysctl_table_users,
1419 .ctl_table_header = &tcp_sysctl_header,
1420 .ctl_table = tcp_sysctl_table,
1421#endif
9fb9cbb1 1422};
13b18339 1423EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);