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