import PULS_20160108
[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>
f229f6ce
PM
3 * (C) 2002-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
9fb9cbb1
YK
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9fb9cbb1
YK
9 */
10
9fb9cbb1 11#include <linux/types.h>
9fb9cbb1 12#include <linux/timer.h>
9fb9cbb1
YK
13#include <linux/module.h>
14#include <linux/in.h>
15#include <linux/tcp.h>
16#include <linux/spinlock.h>
17#include <linux/skbuff.h>
18#include <linux/ipv6.h>
19#include <net/ip6_checksum.h>
534f81a5 20#include <asm/unaligned.h>
9fb9cbb1
YK
21
22#include <net/tcp.h>
23
24#include <linux/netfilter.h>
25#include <linux/netfilter_ipv4.h>
26#include <linux/netfilter_ipv6.h>
27#include <net/netfilter/nf_conntrack.h>
605dcad6 28#include <net/netfilter/nf_conntrack_l4proto.h>
f6180121 29#include <net/netfilter/nf_conntrack_ecache.h>
f01ffbd6 30#include <net/netfilter/nf_log.h>
9d2493f8
CP
31#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
32#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
9fb9cbb1 33
601e68e1
YH
34/* "Be conservative in what you do,
35 be liberal in what you accept from others."
9fb9cbb1 36 If it's non-zero, we mark only out of window RST segments as INVALID. */
3aef0fd9 37static int nf_ct_tcp_be_liberal __read_mostly = 0;
9fb9cbb1 38
a09113c2 39/* If it is set to zero, we disable picking up already established
9fb9cbb1 40 connections. */
3aef0fd9 41static int nf_ct_tcp_loose __read_mostly = 1;
9fb9cbb1 42
601e68e1
YH
43/* Max number of the retransmitted packets without receiving an (acceptable)
44 ACK from the destination. If this number is reached, a shorter timer
9fb9cbb1 45 will be started. */
3aef0fd9 46static int nf_ct_tcp_max_retrans __read_mostly = 3;
9fb9cbb1 47
6fa3eb70
S
48/*if it is set to one,we disable check tcp ack or sequence number is in window*/
49static int nf_ct_tcp_no_window_check __read_mostly = 0;
9fb9cbb1
YK
50 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
51 closely. They're more complex. --RR */
52
82f568fc 53static const char *const tcp_conntrack_names[] = {
9fb9cbb1
YK
54 "NONE",
55 "SYN_SENT",
56 "SYN_RECV",
57 "ESTABLISHED",
58 "FIN_WAIT",
59 "CLOSE_WAIT",
60 "LAST_ACK",
61 "TIME_WAIT",
62 "CLOSE",
874ab923 63 "SYN_SENT2",
9fb9cbb1 64};
601e68e1 65
9fb9cbb1
YK
66#define SECS * HZ
67#define MINS * 60 SECS
68#define HOURS * 60 MINS
69#define DAYS * 24 HOURS
70
33ee4464 71static unsigned int tcp_timeouts[TCP_CONNTRACK_TIMEOUT_MAX] __read_mostly = {
2d646286
PM
72 [TCP_CONNTRACK_SYN_SENT] = 2 MINS,
73 [TCP_CONNTRACK_SYN_RECV] = 60 SECS,
74 [TCP_CONNTRACK_ESTABLISHED] = 5 DAYS,
75 [TCP_CONNTRACK_FIN_WAIT] = 2 MINS,
76 [TCP_CONNTRACK_CLOSE_WAIT] = 60 SECS,
77 [TCP_CONNTRACK_LAST_ACK] = 30 SECS,
78 [TCP_CONNTRACK_TIME_WAIT] = 2 MINS,
79 [TCP_CONNTRACK_CLOSE] = 10 SECS,
874ab923 80 [TCP_CONNTRACK_SYN_SENT2] = 2 MINS,
33ee4464
PNA
81/* RFC1122 says the R2 limit should be at least 100 seconds.
82 Linux uses 15 packets as limit, which corresponds
83 to ~13-30min depending on RTO. */
84 [TCP_CONNTRACK_RETRANS] = 5 MINS,
85 [TCP_CONNTRACK_UNACK] = 5 MINS,
2d646286 86};
601e68e1 87
9fb9cbb1
YK
88#define sNO TCP_CONNTRACK_NONE
89#define sSS TCP_CONNTRACK_SYN_SENT
90#define sSR TCP_CONNTRACK_SYN_RECV
91#define sES TCP_CONNTRACK_ESTABLISHED
92#define sFW TCP_CONNTRACK_FIN_WAIT
93#define sCW TCP_CONNTRACK_CLOSE_WAIT
94#define sLA TCP_CONNTRACK_LAST_ACK
95#define sTW TCP_CONNTRACK_TIME_WAIT
96#define sCL TCP_CONNTRACK_CLOSE
874ab923 97#define sS2 TCP_CONNTRACK_SYN_SENT2
9fb9cbb1
YK
98#define sIV TCP_CONNTRACK_MAX
99#define sIG TCP_CONNTRACK_IGNORE
100
101/* What TCP flags are set from RST/SYN/FIN/ACK. */
102enum tcp_bit_set {
103 TCP_SYN_SET,
104 TCP_SYNACK_SET,
105 TCP_FIN_SET,
106 TCP_ACK_SET,
107 TCP_RST_SET,
108 TCP_NONE_SET,
109};
601e68e1 110
9fb9cbb1
YK
111/*
112 * The TCP state transition table needs a few words...
113 *
114 * We are the man in the middle. All the packets go through us
115 * but might get lost in transit to the destination.
601e68e1 116 * It is assumed that the destinations can't receive segments
9fb9cbb1
YK
117 * we haven't seen.
118 *
119 * The checked segment is in window, but our windows are *not*
120 * equivalent with the ones of the sender/receiver. We always
121 * try to guess the state of the current sender.
122 *
123 * The meaning of the states are:
124 *
125 * NONE: initial state
601e68e1 126 * SYN_SENT: SYN-only packet seen
874ab923 127 * SYN_SENT2: SYN-only packet seen from reply dir, simultaneous open
9fb9cbb1
YK
128 * SYN_RECV: SYN-ACK packet seen
129 * ESTABLISHED: ACK packet seen
130 * FIN_WAIT: FIN packet seen
601e68e1 131 * CLOSE_WAIT: ACK seen (after FIN)
9fb9cbb1
YK
132 * LAST_ACK: FIN seen (after FIN)
133 * TIME_WAIT: last ACK seen
b2155e7f 134 * CLOSE: closed connection (RST)
9fb9cbb1 135 *
9fb9cbb1 136 * Packets marked as IGNORED (sIG):
601e68e1
YH
137 * if they may be either invalid or valid
138 * and the receiver may send back a connection
9fb9cbb1
YK
139 * closing RST or a SYN/ACK.
140 *
141 * Packets marked as INVALID (sIV):
874ab923 142 * if we regard them as truly invalid packets
9fb9cbb1 143 */
a5e73c29 144static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
9fb9cbb1
YK
145 {
146/* ORIGINAL */
874ab923
JK
147/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
148/*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sS2 },
9fb9cbb1
YK
149/*
150 * sNO -> sSS Initialize a new connection
151 * sSS -> sSS Retransmitted SYN
874ab923
JK
152 * sS2 -> sS2 Late retransmitted SYN
153 * sSR -> sIG
9fb9cbb1 154 * sES -> sIG Error: SYNs in window outside the SYN_SENT state
601e68e1 155 * are errors. Receiver will reply with RST
9fb9cbb1
YK
156 * and close the connection.
157 * Or we are not in sync and hold a dead connection.
158 * sFW -> sIG
159 * sCW -> sIG
160 * sLA -> sIG
161 * sTW -> sSS Reopened connection (RFC 1122).
162 * sCL -> sSS
163 */
874ab923 164/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
64f509ce 165/*synack*/ { sIV, sIV, sSR, sIV, sIV, sIV, sIV, sIV, sIV, sSR },
9fb9cbb1 166/*
874ab923
JK
167 * sNO -> sIV Too late and no reason to do anything
168 * sSS -> sIV Client can't send SYN and then SYN/ACK
169 * sS2 -> sSR SYN/ACK sent to SYN2 in simultaneous open
64f509ce
JK
170 * sSR -> sSR Late retransmitted SYN/ACK in simultaneous open
171 * sES -> sIV Invalid SYN/ACK packets sent by the client
172 * sFW -> sIV
173 * sCW -> sIV
174 * sLA -> sIV
175 * sTW -> sIV
176 * sCL -> sIV
9fb9cbb1 177 */
874ab923 178/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
9fb9cbb1
YK
179/*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
180/*
181 * sNO -> sIV Too late and no reason to do anything...
182 * sSS -> sIV Client migth not send FIN in this state:
183 * we enforce waiting for a SYN/ACK reply first.
874ab923 184 * sS2 -> sIV
9fb9cbb1
YK
185 * sSR -> sFW Close started.
186 * sES -> sFW
187 * sFW -> sLA FIN seen in both directions, waiting for
601e68e1 188 * the last ACK.
9fb9cbb1
YK
189 * Migth be a retransmitted FIN as well...
190 * sCW -> sLA
191 * sLA -> sLA Retransmitted FIN. Remain in the same state.
192 * sTW -> sTW
193 * sCL -> sCL
194 */
874ab923 195/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
9fb9cbb1
YK
196/*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
197/*
198 * sNO -> sES Assumed.
199 * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet.
874ab923 200 * sS2 -> sIV
9fb9cbb1
YK
201 * sSR -> sES Established state is reached.
202 * sES -> sES :-)
203 * sFW -> sCW Normal close request answered by ACK.
204 * sCW -> sCW
205 * sLA -> sTW Last ACK detected.
206 * sTW -> sTW Retransmitted last ACK. Remain in the same state.
207 * sCL -> sCL
208 */
874ab923
JK
209/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
210/*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
9fb9cbb1
YK
211/*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
212 },
213 {
214/* REPLY */
874ab923
JK
215/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
216/*syn*/ { sIV, sS2, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sS2 },
9fb9cbb1
YK
217/*
218 * sNO -> sIV Never reached.
874ab923
JK
219 * sSS -> sS2 Simultaneous open
220 * sS2 -> sS2 Retransmitted simultaneous SYN
221 * sSR -> sIV Invalid SYN packets sent by the server
222 * sES -> sIV
9fb9cbb1
YK
223 * sFW -> sIV
224 * sCW -> sIV
225 * sLA -> sIV
226 * sTW -> sIV Reopened connection, but server may not do it.
227 * sCL -> sIV
228 */
874ab923 229/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
8a80c79a 230/*synack*/ { sIV, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIG, sSR },
9fb9cbb1
YK
231/*
232 * sSS -> sSR Standard open.
874ab923 233 * sS2 -> sSR Simultaneous open
8a80c79a 234 * sSR -> sIG Retransmitted SYN/ACK, ignore it.
9fb9cbb1
YK
235 * sES -> sIG Late retransmitted SYN/ACK?
236 * sFW -> sIG Might be SYN/ACK answering ignored SYN
237 * sCW -> sIG
238 * sLA -> sIG
239 * sTW -> sIG
240 * sCL -> sIG
241 */
874ab923 242/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
9fb9cbb1
YK
243/*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
244/*
245 * sSS -> sIV Server might not send FIN in this state.
874ab923 246 * sS2 -> sIV
9fb9cbb1
YK
247 * sSR -> sFW Close started.
248 * sES -> sFW
249 * sFW -> sLA FIN seen in both directions.
250 * sCW -> sLA
251 * sLA -> sLA Retransmitted FIN.
252 * sTW -> sTW
253 * sCL -> sCL
254 */
874ab923
JK
255/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
256/*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIG },
9fb9cbb1 257/*
73f30602 258 * sSS -> sIG Might be a half-open connection.
874ab923 259 * sS2 -> sIG
9fb9cbb1
YK
260 * sSR -> sSR Might answer late resent SYN.
261 * sES -> sES :-)
262 * sFW -> sCW Normal close request answered by ACK.
263 * sCW -> sCW
264 * sLA -> sTW Last ACK detected.
265 * sTW -> sTW Retransmitted last ACK.
266 * sCL -> sCL
267 */
874ab923
JK
268/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
269/*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
9fb9cbb1 270/*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
601e68e1 271 }
9fb9cbb1
YK
272};
273
d2ba1fde
G
274static inline struct nf_tcp_net *tcp_pernet(struct net *net)
275{
276 return &net->ct.nf_ct_proto.tcp;
277}
278
09f263cd
JE
279static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
280 struct nf_conntrack_tuple *tuple)
9fb9cbb1 281{
82f568fc
JE
282 const struct tcphdr *hp;
283 struct tcphdr _hdr;
9fb9cbb1
YK
284
285 /* Actually only need first 8 bytes. */
286 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
287 if (hp == NULL)
09f263cd 288 return false;
9fb9cbb1
YK
289
290 tuple->src.u.tcp.port = hp->source;
291 tuple->dst.u.tcp.port = hp->dest;
292
09f263cd 293 return true;
9fb9cbb1
YK
294}
295
09f263cd
JE
296static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
297 const struct nf_conntrack_tuple *orig)
9fb9cbb1
YK
298{
299 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
300 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
09f263cd 301 return true;
9fb9cbb1
YK
302}
303
304/* Print out the per-protocol part of the tuple. */
305static int tcp_print_tuple(struct seq_file *s,
306 const struct nf_conntrack_tuple *tuple)
307{
308 return seq_printf(s, "sport=%hu dport=%hu ",
309 ntohs(tuple->src.u.tcp.port),
310 ntohs(tuple->dst.u.tcp.port));
311}
312
313/* Print out the private part of the conntrack. */
440f0d58 314static int tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
9fb9cbb1
YK
315{
316 enum tcp_conntrack state;
317
440f0d58 318 spin_lock_bh(&ct->lock);
c88130bc 319 state = ct->proto.tcp.state;
440f0d58 320 spin_unlock_bh(&ct->lock);
9fb9cbb1
YK
321
322 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
323}
324
325static unsigned int get_conntrack_index(const struct tcphdr *tcph)
326{
327 if (tcph->rst) return TCP_RST_SET;
328 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
329 else if (tcph->fin) return TCP_FIN_SET;
330 else if (tcph->ack) return TCP_ACK_SET;
331 else return TCP_NONE_SET;
332}
333
334/* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
335 in IP Filter' by Guido van Rooij.
601e68e1 336
631dd1a8
JM
337 http://www.sane.nl/events/sane2000/papers.html
338 http://www.darkart.com/mirrors/www.obfuscation.org/ipf/
601e68e1 339
9fb9cbb1
YK
340 The boundaries and the conditions are changed according to RFC793:
341 the packet must intersect the window (i.e. segments may be
342 after the right or before the left edge) and thus receivers may ACK
343 segments after the right edge of the window.
344
601e68e1 345 td_maxend = max(sack + max(win,1)) seen in reply packets
9fb9cbb1
YK
346 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
347 td_maxwin += seq + len - sender.td_maxend
348 if seq + len > sender.td_maxend
349 td_end = max(seq + len) seen in sent packets
601e68e1 350
9fb9cbb1
YK
351 I. Upper bound for valid data: seq <= sender.td_maxend
352 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
84ebe1cd
JK
353 III. Upper bound for valid (s)ack: sack <= receiver.td_end
354 IV. Lower bound for valid (s)ack: sack >= receiver.td_end - MAXACKWINDOW
9fb9cbb1 355
84ebe1cd
JK
356 where sack is the highest right edge of sack block found in the packet
357 or ack in the case of packet without SACK option.
9fb9cbb1 358
84ebe1cd 359 The upper bound limit for a valid (s)ack is not ignored -
601e68e1 360 we doesn't have to deal with fragments.
9fb9cbb1
YK
361*/
362
363static inline __u32 segment_seq_plus_len(__u32 seq,
364 size_t len,
365 unsigned int dataoff,
82f568fc 366 const struct tcphdr *tcph)
9fb9cbb1
YK
367{
368 /* XXX Should I use payload length field in IP/IPv6 header ?
369 * - YK */
370 return (seq + len - dataoff - tcph->doff*4
371 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
372}
601e68e1 373
9fb9cbb1
YK
374/* Fixme: what about big packets? */
375#define MAXACKWINCONST 66000
376#define MAXACKWINDOW(sender) \
377 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
378 : MAXACKWINCONST)
601e68e1 379
9fb9cbb1
YK
380/*
381 * Simplified tcp_parse_options routine from tcp_input.c
382 */
383static void tcp_options(const struct sk_buff *skb,
384 unsigned int dataoff,
82f568fc 385 const struct tcphdr *tcph,
9fb9cbb1
YK
386 struct ip_ct_tcp_state *state)
387{
388 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
82f568fc 389 const unsigned char *ptr;
9fb9cbb1
YK
390 int length = (tcph->doff*4) - sizeof(struct tcphdr);
391
392 if (!length)
393 return;
394
395 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
396 length, buff);
397 BUG_ON(ptr == NULL);
398
601e68e1 399 state->td_scale =
9fb9cbb1
YK
400 state->flags = 0;
401
402 while (length > 0) {
403 int opcode=*ptr++;
404 int opsize;
405
406 switch (opcode) {
407 case TCPOPT_EOL:
408 return;
409 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
410 length--;
411 continue;
412 default:
413 opsize=*ptr++;
414 if (opsize < 2) /* "silly options" */
415 return;
416 if (opsize > length)
4a5cc84a 417 return; /* don't parse partial options */
9fb9cbb1 418
601e68e1 419 if (opcode == TCPOPT_SACK_PERM
9fb9cbb1
YK
420 && opsize == TCPOLEN_SACK_PERM)
421 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
422 else if (opcode == TCPOPT_WINDOW
423 && opsize == TCPOLEN_WINDOW) {
424 state->td_scale = *(u_int8_t *)ptr;
425
426 if (state->td_scale > 14) {
427 /* See RFC1323 */
428 state->td_scale = 14;
429 }
430 state->flags |=
431 IP_CT_TCP_FLAG_WINDOW_SCALE;
432 }
433 ptr += opsize - 2;
434 length -= opsize;
435 }
436 }
437}
438
439static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
82f568fc 440 const struct tcphdr *tcph, __u32 *sack)
9fb9cbb1 441{
601e68e1 442 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
82f568fc 443 const unsigned char *ptr;
9fb9cbb1
YK
444 int length = (tcph->doff*4) - sizeof(struct tcphdr);
445 __u32 tmp;
446
447 if (!length)
448 return;
449
450 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
451 length, buff);
452 BUG_ON(ptr == NULL);
453
454 /* Fast path for timestamp-only option */
bb9fc373 455 if (length == TCPOLEN_TSTAMP_ALIGNED
8f05ce91
YH
456 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
457 | (TCPOPT_NOP << 16)
458 | (TCPOPT_TIMESTAMP << 8)
459 | TCPOLEN_TIMESTAMP))
9fb9cbb1
YK
460 return;
461
462 while (length > 0) {
463 int opcode = *ptr++;
464 int opsize, i;
465
466 switch (opcode) {
467 case TCPOPT_EOL:
468 return;
469 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
470 length--;
471 continue;
472 default:
473 opsize = *ptr++;
474 if (opsize < 2) /* "silly options" */
475 return;
476 if (opsize > length)
4a5cc84a 477 return; /* don't parse partial options */
9fb9cbb1 478
601e68e1
YH
479 if (opcode == TCPOPT_SACK
480 && opsize >= (TCPOLEN_SACK_BASE
481 + TCPOLEN_SACK_PERBLOCK)
482 && !((opsize - TCPOLEN_SACK_BASE)
483 % TCPOLEN_SACK_PERBLOCK)) {
484 for (i = 0;
485 i < (opsize - TCPOLEN_SACK_BASE);
486 i += TCPOLEN_SACK_PERBLOCK) {
534f81a5 487 tmp = get_unaligned_be32((__be32 *)(ptr+i)+1);
9fb9cbb1
YK
488
489 if (after(tmp, *sack))
490 *sack = tmp;
491 }
492 return;
493 }
494 ptr += opsize - 2;
495 length -= opsize;
496 }
497 }
498}
499
f9dd09c7
JK
500#ifdef CONFIG_NF_NAT_NEEDED
501static inline s16 nat_offset(const struct nf_conn *ct,
502 enum ip_conntrack_dir dir,
503 u32 seq)
504{
505 typeof(nf_ct_nat_offset) get_offset = rcu_dereference(nf_ct_nat_offset);
506
507 return get_offset != NULL ? get_offset(ct, dir, seq) : 0;
508}
c7232c99
PM
509#define NAT_OFFSET(ct, dir, seq) \
510 (nat_offset(ct, dir, seq))
f9dd09c7 511#else
c7232c99 512#define NAT_OFFSET(ct, dir, seq) 0
f9dd09c7
JK
513#endif
514
09f263cd
JE
515static bool tcp_in_window(const struct nf_conn *ct,
516 struct ip_ct_tcp *state,
517 enum ip_conntrack_dir dir,
518 unsigned int index,
519 const struct sk_buff *skb,
520 unsigned int dataoff,
521 const struct tcphdr *tcph,
76108cea 522 u_int8_t pf)
9fb9cbb1 523{
c2a2c7e0 524 struct net *net = nf_ct_net(ct);
d2ba1fde 525 struct nf_tcp_net *tn = tcp_pernet(net);
9fb9cbb1
YK
526 struct ip_ct_tcp_state *sender = &state->seen[dir];
527 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
82f568fc 528 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
9fb9cbb1 529 __u32 seq, ack, sack, end, win, swin;
f9dd09c7 530 s16 receiver_offset;
09f263cd 531 bool res;
9fb9cbb1 532
6fa3eb70
S
533 if( nf_ct_tcp_no_window_check )
534 {
535 return true;
536 }
9fb9cbb1
YK
537 /*
538 * Get the required data from the packet.
539 */
540 seq = ntohl(tcph->seq);
541 ack = sack = ntohl(tcph->ack_seq);
542 win = ntohs(tcph->window);
543 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
544
545 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
546 tcp_sack(skb, dataoff, tcph, &sack);
547
f9dd09c7 548 /* Take into account NAT sequence number mangling */
c7232c99 549 receiver_offset = NAT_OFFSET(ct, !dir, ack - 1);
f9dd09c7
JK
550 ack -= receiver_offset;
551 sack -= receiver_offset;
552
0d53778e
PM
553 pr_debug("tcp_in_window: START\n");
554 pr_debug("tcp_in_window: ");
3c9fba65 555 nf_ct_dump_tuple(tuple);
f9dd09c7
JK
556 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
557 seq, ack, receiver_offset, sack, receiver_offset, win, end);
0d53778e
PM
558 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
559 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
560 sender->td_end, sender->td_maxend, sender->td_maxwin,
561 sender->td_scale,
562 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
563 receiver->td_scale);
9fb9cbb1 564
874ab923 565 if (sender->td_maxwin == 0) {
9fb9cbb1
YK
566 /*
567 * Initialize sender data.
568 */
874ab923 569 if (tcph->syn) {
9fb9cbb1 570 /*
874ab923
JK
571 * SYN-ACK in reply to a SYN
572 * or SYN from reply direction in simultaneous open.
9fb9cbb1 573 */
601e68e1 574 sender->td_end =
9fb9cbb1
YK
575 sender->td_maxend = end;
576 sender->td_maxwin = (win == 0 ? 1 : win);
577
578 tcp_options(skb, dataoff, tcph, sender);
601e68e1 579 /*
9fb9cbb1
YK
580 * RFC 1323:
581 * Both sides must send the Window Scale option
582 * to enable window scaling in either direction.
583 */
584 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
585 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
601e68e1 586 sender->td_scale =
9fb9cbb1 587 receiver->td_scale = 0;
874ab923
JK
588 if (!tcph->ack)
589 /* Simultaneous open */
590 return true;
9fb9cbb1
YK
591 } else {
592 /*
593 * We are in the middle of a connection,
594 * its history is lost for us.
595 * Let's try to use the data from the packet.
601e68e1 596 */
9fb9cbb1 597 sender->td_end = end;
6ee0b693
CG
598 swin = win << sender->td_scale;
599 sender->td_maxwin = (swin == 0 ? 1 : swin);
9fb9cbb1 600 sender->td_maxend = end + sender->td_maxwin;
fac42a9a
PNA
601 /*
602 * We haven't seen traffic in the other direction yet
603 * but we have to tweak window tracking to pass III
604 * and IV until that happens.
605 */
606 if (receiver->td_maxwin == 0)
607 receiver->td_end = receiver->td_maxend = sack;
9fb9cbb1
YK
608 }
609 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
610 && dir == IP_CT_DIR_ORIGINAL)
611 || (state->state == TCP_CONNTRACK_SYN_RECV
612 && dir == IP_CT_DIR_REPLY))
613 && after(end, sender->td_end)) {
614 /*
615 * RFC 793: "if a TCP is reinitialized ... then it need
601e68e1 616 * not wait at all; it must only be sure to use sequence
9fb9cbb1
YK
617 * numbers larger than those recently used."
618 */
619 sender->td_end =
620 sender->td_maxend = end;
621 sender->td_maxwin = (win == 0 ? 1 : win);
622
623 tcp_options(skb, dataoff, tcph, sender);
624 }
625
626 if (!(tcph->ack)) {
627 /*
628 * If there is no ACK, just pretend it was set and OK.
629 */
630 ack = sack = receiver->td_end;
601e68e1
YH
631 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
632 (TCP_FLAG_ACK|TCP_FLAG_RST))
9fb9cbb1
YK
633 && (ack == 0)) {
634 /*
635 * Broken TCP stacks, that set ACK in RST packets as well
636 * with zero ack value.
637 */
638 ack = sack = receiver->td_end;
639 }
640
4a70bbfa 641 if (tcph->rst && seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)
9fb9cbb1 642 /*
4a70bbfa 643 * RST sent answering SYN.
9fb9cbb1
YK
644 */
645 seq = end = sender->td_end;
646
0d53778e 647 pr_debug("tcp_in_window: ");
3c9fba65 648 nf_ct_dump_tuple(tuple);
f9dd09c7
JK
649 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
650 seq, ack, receiver_offset, sack, receiver_offset, win, end);
0d53778e
PM
651 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
652 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
653 sender->td_end, sender->td_maxend, sender->td_maxwin,
654 sender->td_scale,
655 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
656 receiver->td_scale);
657
658 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
659 before(seq, sender->td_maxend + 1),
660 after(end, sender->td_end - receiver->td_maxwin - 1),
661 before(sack, receiver->td_end + 1),
84ebe1cd 662 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
9fb9cbb1 663
a09113c2
PM
664 if (before(seq, sender->td_maxend + 1) &&
665 after(end, sender->td_end - receiver->td_maxwin - 1) &&
666 before(sack, receiver->td_end + 1) &&
84ebe1cd 667 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
601e68e1 668 /*
9fb9cbb1
YK
669 * Take into account window scaling (RFC 1323).
670 */
671 if (!tcph->syn)
672 win <<= sender->td_scale;
673
674 /*
675 * Update sender data.
676 */
677 swin = win + (sack - ack);
678 if (sender->td_maxwin < swin)
679 sender->td_maxwin = swin;
ae375044 680 if (after(end, sender->td_end)) {
9fb9cbb1 681 sender->td_end = end;
ae375044
PM
682 sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
683 }
bfcaa502
JK
684 if (tcph->ack) {
685 if (!(sender->flags & IP_CT_TCP_FLAG_MAXACK_SET)) {
686 sender->td_maxack = ack;
687 sender->flags |= IP_CT_TCP_FLAG_MAXACK_SET;
688 } else if (after(ack, sender->td_maxack))
689 sender->td_maxack = ack;
690 }
691
9fb9cbb1
YK
692 /*
693 * Update receiver data.
694 */
fac42a9a 695 if (receiver->td_maxwin != 0 && after(end, sender->td_maxend))
9fb9cbb1
YK
696 receiver->td_maxwin += end - sender->td_maxend;
697 if (after(sack + win, receiver->td_maxend - 1)) {
698 receiver->td_maxend = sack + win;
699 if (win == 0)
700 receiver->td_maxend++;
701 }
ae375044
PM
702 if (ack == receiver->td_end)
703 receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
9fb9cbb1 704
601e68e1 705 /*
9fb9cbb1
YK
706 * Check retransmissions.
707 */
708 if (index == TCP_ACK_SET) {
709 if (state->last_dir == dir
710 && state->last_seq == seq
711 && state->last_ack == ack
c1fe3ca5
GH
712 && state->last_end == end
713 && state->last_win == win)
9fb9cbb1
YK
714 state->retrans++;
715 else {
716 state->last_dir = dir;
717 state->last_seq = seq;
718 state->last_ack = ack;
719 state->last_end = end;
c1fe3ca5 720 state->last_win = win;
9fb9cbb1
YK
721 state->retrans = 0;
722 }
723 }
09f263cd 724 res = true;
9fb9cbb1 725 } else {
09f263cd 726 res = false;
a09113c2 727 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
d2ba1fde 728 tn->tcp_be_liberal)
09f263cd 729 res = true;
c2a2c7e0 730 if (!res && LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 731 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
732 "nf_ct_tcp: %s ",
733 before(seq, sender->td_maxend + 1) ?
734 after(end, sender->td_end - receiver->td_maxwin - 1) ?
735 before(sack, receiver->td_end + 1) ?
f9dd09c7 736 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1) ? "BUG"
9fb9cbb1
YK
737 : "ACK is under the lower bound (possible overly delayed ACK)"
738 : "ACK is over the upper bound (ACKed data not seen yet)"
739 : "SEQ is under the lower bound (already ACKed data retransmitted)"
740 : "SEQ is over the upper bound (over the window of the receiver)");
601e68e1
YH
741 }
742
09f263cd 743 pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
0d53778e
PM
744 "receiver end=%u maxend=%u maxwin=%u\n",
745 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
746 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
9fb9cbb1
YK
747
748 return res;
749}
750
5c8ce7c9 751/* table of valid flag combinations - PUSH, ECE and CWR are always valid */
a3433f35
CG
752static const u8 tcp_valid_flags[(TCPHDR_FIN|TCPHDR_SYN|TCPHDR_RST|TCPHDR_ACK|
753 TCPHDR_URG) + 1] =
9fb9cbb1 754{
a3433f35
CG
755 [TCPHDR_SYN] = 1,
756 [TCPHDR_SYN|TCPHDR_URG] = 1,
757 [TCPHDR_SYN|TCPHDR_ACK] = 1,
758 [TCPHDR_RST] = 1,
759 [TCPHDR_RST|TCPHDR_ACK] = 1,
760 [TCPHDR_FIN|TCPHDR_ACK] = 1,
761 [TCPHDR_FIN|TCPHDR_ACK|TCPHDR_URG] = 1,
762 [TCPHDR_ACK] = 1,
763 [TCPHDR_ACK|TCPHDR_URG] = 1,
9fb9cbb1
YK
764};
765
766/* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
8fea97ec 767static int tcp_error(struct net *net, struct nf_conn *tmpl,
74c51a14 768 struct sk_buff *skb,
9fb9cbb1
YK
769 unsigned int dataoff,
770 enum ip_conntrack_info *ctinfo,
76108cea 771 u_int8_t pf,
96f6bf82 772 unsigned int hooknum)
9fb9cbb1 773{
82f568fc
JE
774 const struct tcphdr *th;
775 struct tcphdr _tcph;
9fb9cbb1
YK
776 unsigned int tcplen = skb->len - dataoff;
777 u_int8_t tcpflags;
778
779 /* Smaller that minimal TCP header? */
780 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
781 if (th == NULL) {
c2a2c7e0 782 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 783 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
784 "nf_ct_tcp: short packet ");
785 return -NF_ACCEPT;
601e68e1
YH
786 }
787
9fb9cbb1
YK
788 /* Not whole TCP header or malformed packet */
789 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
c2a2c7e0 790 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 791 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
792 "nf_ct_tcp: truncated/malformed packet ");
793 return -NF_ACCEPT;
794 }
601e68e1 795
9fb9cbb1
YK
796 /* Checksum invalid? Ignore.
797 * We skip checking packets on the outgoing path
84fa7933 798 * because the checksum is assumed to be correct.
9fb9cbb1
YK
799 */
800 /* FIXME: Source route IP option packets --RR */
c04d0552 801 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
96f6bf82 802 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
c2a2c7e0 803 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 804 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
805 "nf_ct_tcp: bad TCP checksum ");
806 return -NF_ACCEPT;
807 }
808
809 /* Check TCP flags. */
a3433f35 810 tcpflags = (tcp_flag_byte(th) & ~(TCPHDR_ECE|TCPHDR_CWR|TCPHDR_PSH));
9fb9cbb1 811 if (!tcp_valid_flags[tcpflags]) {
c2a2c7e0 812 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 813 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
814 "nf_ct_tcp: invalid TCP flag combination ");
815 return -NF_ACCEPT;
816 }
817
818 return NF_ACCEPT;
819}
820
2c8503f5
PNA
821static unsigned int *tcp_get_timeouts(struct net *net)
822{
be0593c6 823 return tcp_pernet(net)->timeouts;
2c8503f5
PNA
824}
825
9fb9cbb1 826/* Returns verdict for packet, or -1 for invalid. */
c88130bc 827static int tcp_packet(struct nf_conn *ct,
9fb9cbb1
YK
828 const struct sk_buff *skb,
829 unsigned int dataoff,
830 enum ip_conntrack_info ctinfo,
76108cea 831 u_int8_t pf,
2c8503f5
PNA
832 unsigned int hooknum,
833 unsigned int *timeouts)
9fb9cbb1 834{
c2a2c7e0 835 struct net *net = nf_ct_net(ct);
d2ba1fde 836 struct nf_tcp_net *tn = tcp_pernet(net);
0d53778e 837 struct nf_conntrack_tuple *tuple;
9fb9cbb1
YK
838 enum tcp_conntrack new_state, old_state;
839 enum ip_conntrack_dir dir;
82f568fc
JE
840 const struct tcphdr *th;
841 struct tcphdr _tcph;
9fb9cbb1
YK
842 unsigned long timeout;
843 unsigned int index;
844
845 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
846 BUG_ON(th == NULL);
847
440f0d58 848 spin_lock_bh(&ct->lock);
c88130bc 849 old_state = ct->proto.tcp.state;
9fb9cbb1
YK
850 dir = CTINFO2DIR(ctinfo);
851 index = get_conntrack_index(th);
852 new_state = tcp_conntracks[dir][index][old_state];
c88130bc 853 tuple = &ct->tuplehash[dir].tuple;
9fb9cbb1
YK
854
855 switch (new_state) {
17311393
JK
856 case TCP_CONNTRACK_SYN_SENT:
857 if (old_state < TCP_CONNTRACK_TIME_WAIT)
858 break;
b2155e7f
JK
859 /* RFC 1122: "When a connection is closed actively,
860 * it MUST linger in TIME-WAIT state for a time 2xMSL
861 * (Maximum Segment Lifetime). However, it MAY accept
862 * a new SYN from the remote TCP to reopen the connection
863 * directly from TIME-WAIT state, if..."
864 * We ignore the conditions because we are in the
865 * TIME-WAIT state anyway.
866 *
867 * Handle aborted connections: we and the server
868 * think there is an existing connection but the client
869 * aborts it and starts a new one.
870 */
871 if (((ct->proto.tcp.seen[dir].flags
872 | ct->proto.tcp.seen[!dir].flags)
873 & IP_CT_TCP_FLAG_CLOSE_INIT)
c88130bc
PM
874 || (ct->proto.tcp.last_dir == dir
875 && ct->proto.tcp.last_index == TCP_RST_SET)) {
bc34b841
JK
876 /* Attempt to reopen a closed/aborted connection.
877 * Delete this connection and look up again. */
440f0d58 878 spin_unlock_bh(&ct->lock);
2aec609f 879
6b69fe0c
PM
880 /* Only repeat if we can actually remove the timer.
881 * Destruction may already be in progress in process
882 * context and we must give it a chance to terminate.
883 */
2aec609f 884 if (nf_ct_kill(ct))
6b69fe0c 885 return -NF_REPEAT;
ec8d5409 886 return NF_DROP;
17311393
JK
887 }
888 /* Fall through */
9fb9cbb1 889 case TCP_CONNTRACK_IGNORE:
73f30602 890 /* Ignored packets:
b2155e7f
JK
891 *
892 * Our connection entry may be out of sync, so ignore
893 * packets which may signal the real connection between
894 * the client and the server.
73f30602
JK
895 *
896 * a) SYN in ORIGINAL
897 * b) SYN/ACK in REPLY
601e68e1 898 * c) ACK in reply direction after initial SYN in original.
b2155e7f
JK
899 *
900 * If the ignored packet is invalid, the receiver will send
901 * a RST we'll catch below.
73f30602 902 */
9fb9cbb1 903 if (index == TCP_SYNACK_SET
c88130bc
PM
904 && ct->proto.tcp.last_index == TCP_SYN_SET
905 && ct->proto.tcp.last_dir != dir
906 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
b2155e7f 907 /* b) This SYN/ACK acknowledges a SYN that we earlier
9fb9cbb1
YK
908 * ignored as invalid. This means that the client and
909 * the server are both in sync, while the firewall is
c4832c7b
PNA
910 * not. We get in sync from the previously annotated
911 * values.
9fb9cbb1 912 */
c4832c7b
PNA
913 old_state = TCP_CONNTRACK_SYN_SENT;
914 new_state = TCP_CONNTRACK_SYN_RECV;
915 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_end =
916 ct->proto.tcp.last_end;
917 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxend =
918 ct->proto.tcp.last_end;
919 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxwin =
920 ct->proto.tcp.last_win == 0 ?
921 1 : ct->proto.tcp.last_win;
922 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_scale =
923 ct->proto.tcp.last_wscale;
924 ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
925 ct->proto.tcp.last_flags;
926 memset(&ct->proto.tcp.seen[dir], 0,
927 sizeof(struct ip_ct_tcp_state));
928 break;
9fb9cbb1 929 }
c88130bc
PM
930 ct->proto.tcp.last_index = index;
931 ct->proto.tcp.last_dir = dir;
932 ct->proto.tcp.last_seq = ntohl(th->seq);
933 ct->proto.tcp.last_end =
9fb9cbb1 934 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
c4832c7b
PNA
935 ct->proto.tcp.last_win = ntohs(th->window);
936
937 /* a) This is a SYN in ORIGINAL. The client and the server
938 * may be in sync but we are not. In that case, we annotate
939 * the TCP options and let the packet go through. If it is a
940 * valid SYN packet, the server will reply with a SYN/ACK, and
941 * then we'll get in sync. Otherwise, the server ignores it. */
942 if (index == TCP_SYN_SET && dir == IP_CT_DIR_ORIGINAL) {
943 struct ip_ct_tcp_state seen = {};
944
945 ct->proto.tcp.last_flags =
946 ct->proto.tcp.last_wscale = 0;
947 tcp_options(skb, dataoff, th, &seen);
948 if (seen.flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
949 ct->proto.tcp.last_flags |=
950 IP_CT_TCP_FLAG_WINDOW_SCALE;
951 ct->proto.tcp.last_wscale = seen.td_scale;
952 }
953 if (seen.flags & IP_CT_TCP_FLAG_SACK_PERM) {
954 ct->proto.tcp.last_flags |=
955 IP_CT_TCP_FLAG_SACK_PERM;
956 }
957 }
440f0d58 958 spin_unlock_bh(&ct->lock);
c2a2c7e0 959 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 960 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
1a4ac987
PNA
961 "nf_ct_tcp: invalid packet ignored in "
962 "state %s ", tcp_conntrack_names[old_state]);
9fb9cbb1
YK
963 return NF_ACCEPT;
964 case TCP_CONNTRACK_MAX:
965 /* Invalid packet */
0d53778e
PM
966 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
967 dir, get_conntrack_index(th), old_state);
440f0d58 968 spin_unlock_bh(&ct->lock);
c2a2c7e0 969 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 970 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
971 "nf_ct_tcp: invalid state ");
972 return -NF_ACCEPT;
9fb9cbb1 973 case TCP_CONNTRACK_CLOSE:
bfcaa502
JK
974 if (index == TCP_RST_SET
975 && (ct->proto.tcp.seen[!dir].flags & IP_CT_TCP_FLAG_MAXACK_SET)
976 && before(ntohl(th->seq), ct->proto.tcp.seen[!dir].td_maxack)) {
977 /* Invalid RST */
334a47f6 978 spin_unlock_bh(&ct->lock);
bfcaa502 979 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6
G
980 nf_log_packet(net, pf, 0, skb, NULL, NULL,
981 NULL, "nf_ct_tcp: invalid RST ");
bfcaa502
JK
982 return -NF_ACCEPT;
983 }
9fb9cbb1 984 if (index == TCP_RST_SET
c88130bc
PM
985 && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
986 && ct->proto.tcp.last_index == TCP_SYN_SET)
987 || (!test_bit(IPS_ASSURED_BIT, &ct->status)
988 && ct->proto.tcp.last_index == TCP_ACK_SET))
989 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
93b1fae4 990 /* RST sent to invalid SYN or ACK we had let through
73f30602
JK
991 * at a) and c) above:
992 *
993 * a) SYN was in window then
994 * c) we hold a half-open connection.
995 *
996 * Delete our connection entry.
9fb9cbb1 997 * We skip window checking, because packet might ACK
73f30602 998 * segments we ignored. */
9fb9cbb1
YK
999 goto in_window;
1000 }
93b1fae4 1001 /* Just fall through */
9fb9cbb1
YK
1002 default:
1003 /* Keep compilers happy. */
1004 break;
1005 }
1006
c88130bc 1007 if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
9fb9cbb1 1008 skb, dataoff, th, pf)) {
440f0d58 1009 spin_unlock_bh(&ct->lock);
9fb9cbb1
YK
1010 return -NF_ACCEPT;
1011 }
1012 in_window:
1013 /* From now on we have got in-window packets */
c88130bc
PM
1014 ct->proto.tcp.last_index = index;
1015 ct->proto.tcp.last_dir = dir;
9fb9cbb1 1016
0d53778e 1017 pr_debug("tcp_conntracks: ");
3c9fba65 1018 nf_ct_dump_tuple(tuple);
0d53778e
PM
1019 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1020 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1021 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1022 old_state, new_state);
9fb9cbb1 1023
c88130bc 1024 ct->proto.tcp.state = new_state;
9fb9cbb1 1025 if (old_state != new_state
d0c1fd7a 1026 && new_state == TCP_CONNTRACK_FIN_WAIT)
c88130bc 1027 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
ae375044 1028
d2ba1fde 1029 if (ct->proto.tcp.retrans >= tn->tcp_max_retrans &&
2c8503f5
PNA
1030 timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS])
1031 timeout = timeouts[TCP_CONNTRACK_RETRANS];
ae375044
PM
1032 else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) &
1033 IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED &&
2c8503f5
PNA
1034 timeouts[new_state] > timeouts[TCP_CONNTRACK_UNACK])
1035 timeout = timeouts[TCP_CONNTRACK_UNACK];
ae375044 1036 else
2c8503f5 1037 timeout = timeouts[new_state];
440f0d58 1038 spin_unlock_bh(&ct->lock);
9fb9cbb1 1039
9fb9cbb1 1040 if (new_state != old_state)
a71996fc 1041 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
9fb9cbb1 1042
c88130bc 1043 if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
9fb9cbb1
YK
1044 /* If only reply is a RST, we can consider ourselves not to
1045 have an established connection: this is a fairly common
1046 problem case, so we can delete the conntrack
1047 immediately. --RR */
1048 if (th->rst) {
718d4ad9 1049 nf_ct_kill_acct(ct, ctinfo, skb);
9fb9cbb1
YK
1050 return NF_ACCEPT;
1051 }
c88130bc 1052 } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
9fb9cbb1
YK
1053 && (old_state == TCP_CONNTRACK_SYN_RECV
1054 || old_state == TCP_CONNTRACK_ESTABLISHED)
1055 && new_state == TCP_CONNTRACK_ESTABLISHED) {
601e68e1
YH
1056 /* Set ASSURED if we see see valid ack in ESTABLISHED
1057 after SYN_RECV or a valid answer for a picked up
9fb9cbb1 1058 connection. */
c88130bc 1059 set_bit(IPS_ASSURED_BIT, &ct->status);
858b3133 1060 nf_conntrack_event_cache(IPCT_ASSURED, ct);
9fb9cbb1 1061 }
c88130bc 1062 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
9fb9cbb1
YK
1063
1064 return NF_ACCEPT;
1065}
601e68e1 1066
9fb9cbb1 1067/* Called when a new connection for this protocol found. */
09f263cd 1068static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
2c8503f5 1069 unsigned int dataoff, unsigned int *timeouts)
9fb9cbb1
YK
1070{
1071 enum tcp_conntrack new_state;
82f568fc
JE
1072 const struct tcphdr *th;
1073 struct tcphdr _tcph;
d2ba1fde
G
1074 struct net *net = nf_ct_net(ct);
1075 struct nf_tcp_net *tn = tcp_pernet(net);
82f568fc
JE
1076 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
1077 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
9fb9cbb1
YK
1078
1079 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1080 BUG_ON(th == NULL);
1081
1082 /* Don't need lock here: this conntrack not in circulation yet */
e5fc9e7a 1083 new_state = tcp_conntracks[0][get_conntrack_index(th)][TCP_CONNTRACK_NONE];
9fb9cbb1
YK
1084
1085 /* Invalid: delete conntrack */
1086 if (new_state >= TCP_CONNTRACK_MAX) {
0d53778e 1087 pr_debug("nf_ct_tcp: invalid new deleting.\n");
09f263cd 1088 return false;
9fb9cbb1
YK
1089 }
1090
1091 if (new_state == TCP_CONNTRACK_SYN_SENT) {
e5fc9e7a 1092 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
9fb9cbb1 1093 /* SYN packet */
c88130bc 1094 ct->proto.tcp.seen[0].td_end =
9fb9cbb1
YK
1095 segment_seq_plus_len(ntohl(th->seq), skb->len,
1096 dataoff, th);
c88130bc
PM
1097 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1098 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1099 ct->proto.tcp.seen[0].td_maxwin = 1;
1100 ct->proto.tcp.seen[0].td_maxend =
1101 ct->proto.tcp.seen[0].td_end;
1102
1103 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
d2ba1fde 1104 } else if (tn->tcp_loose == 0) {
9fb9cbb1 1105 /* Don't try to pick up connections. */
09f263cd 1106 return false;
9fb9cbb1 1107 } else {
e5fc9e7a 1108 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
9fb9cbb1
YK
1109 /*
1110 * We are in the middle of a connection,
1111 * its history is lost for us.
1112 * Let's try to use the data from the packet.
1113 */
c88130bc 1114 ct->proto.tcp.seen[0].td_end =
9fb9cbb1
YK
1115 segment_seq_plus_len(ntohl(th->seq), skb->len,
1116 dataoff, th);
c88130bc
PM
1117 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1118 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1119 ct->proto.tcp.seen[0].td_maxwin = 1;
1120 ct->proto.tcp.seen[0].td_maxend =
1121 ct->proto.tcp.seen[0].td_end +
1122 ct->proto.tcp.seen[0].td_maxwin;
9fb9cbb1 1123
a09113c2
PM
1124 /* We assume SACK and liberal window checking to handle
1125 * window scaling */
c88130bc
PM
1126 ct->proto.tcp.seen[0].flags =
1127 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1128 IP_CT_TCP_FLAG_BE_LIBERAL;
9fb9cbb1 1129 }
601e68e1 1130
9fb9cbb1 1131 /* tcp_packet will set them */
c88130bc 1132 ct->proto.tcp.last_index = TCP_NONE_SET;
601e68e1 1133
0d53778e
PM
1134 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1135 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1136 sender->td_end, sender->td_maxend, sender->td_maxwin,
1137 sender->td_scale,
1138 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1139 receiver->td_scale);
09f263cd 1140 return true;
9fb9cbb1 1141}
c1d10adb 1142
c0cd1156 1143#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
c1d10adb
PNA
1144
1145#include <linux/netfilter/nfnetlink.h>
1146#include <linux/netfilter/nfnetlink_conntrack.h>
1147
fdf70832 1148static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
440f0d58 1149 struct nf_conn *ct)
c1d10adb 1150{
df6fb868 1151 struct nlattr *nest_parms;
c8e2078c 1152 struct nf_ct_tcp_flags tmp = {};
601e68e1 1153
440f0d58 1154 spin_lock_bh(&ct->lock);
df6fb868
PM
1155 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1156 if (!nest_parms)
1157 goto nla_put_failure;
1158
4925a459
DM
1159 if (nla_put_u8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state) ||
1160 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1161 ct->proto.tcp.seen[0].td_scale) ||
1162 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1163 ct->proto.tcp.seen[1].td_scale))
1164 goto nla_put_failure;
c8e2078c
PNA
1165
1166 tmp.flags = ct->proto.tcp.seen[0].flags;
4925a459
DM
1167 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1168 sizeof(struct nf_ct_tcp_flags), &tmp))
1169 goto nla_put_failure;
c8e2078c
PNA
1170
1171 tmp.flags = ct->proto.tcp.seen[1].flags;
4925a459
DM
1172 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1173 sizeof(struct nf_ct_tcp_flags), &tmp))
1174 goto nla_put_failure;
440f0d58 1175 spin_unlock_bh(&ct->lock);
c1d10adb 1176
df6fb868 1177 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
1178
1179 return 0;
1180
df6fb868 1181nla_put_failure:
440f0d58 1182 spin_unlock_bh(&ct->lock);
c1d10adb
PNA
1183 return -1;
1184}
1185
f73e924c
PM
1186static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1187 [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1188 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1189 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1190 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
1191 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
c1d10adb
PNA
1192};
1193
fdf70832 1194static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
c1d10adb 1195{
2f0d2f10 1196 struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
df6fb868 1197 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
f73e924c 1198 int err;
c1d10adb
PNA
1199
1200 /* updates could not contain anything about the private
1201 * protocol info, in that case skip the parsing */
2f0d2f10 1202 if (!pattr)
c1d10adb
PNA
1203 return 0;
1204
2f0d2f10 1205 err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr, tcp_nla_policy);
f73e924c
PM
1206 if (err < 0)
1207 return err;
c1d10adb 1208
5f7da4d2
PM
1209 if (tb[CTA_PROTOINFO_TCP_STATE] &&
1210 nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
c1d10adb
PNA
1211 return -EINVAL;
1212
440f0d58 1213 spin_lock_bh(&ct->lock);
5f7da4d2
PM
1214 if (tb[CTA_PROTOINFO_TCP_STATE])
1215 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
c8e2078c 1216
df6fb868 1217 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
c8e2078c 1218 struct nf_ct_tcp_flags *attr =
df6fb868 1219 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
c8e2078c
PNA
1220 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1221 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1222 }
1223
df6fb868 1224 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
c8e2078c 1225 struct nf_ct_tcp_flags *attr =
df6fb868 1226 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
c8e2078c
PNA
1227 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1228 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1229 }
1230
df6fb868
PM
1231 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1232 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
c8e2078c
PNA
1233 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1234 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
77236b6e
PM
1235 ct->proto.tcp.seen[0].td_scale =
1236 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]);
1237 ct->proto.tcp.seen[1].td_scale =
1238 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]);
c8e2078c 1239 }
440f0d58 1240 spin_unlock_bh(&ct->lock);
c1d10adb
PNA
1241
1242 return 0;
1243}
a400c30e
HE
1244
1245static int tcp_nlattr_size(void)
1246{
1247 return nla_total_size(0) /* CTA_PROTOINFO_TCP */
1248 + nla_policy_len(tcp_nla_policy, CTA_PROTOINFO_TCP_MAX + 1);
1249}
1250
1251static int tcp_nlattr_tuple_size(void)
1252{
1253 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1254}
c1d10adb 1255#endif
933a41e7 1256
50978462
PNA
1257#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1258
1259#include <linux/netfilter/nfnetlink.h>
1260#include <linux/netfilter/nfnetlink_cttimeout.h>
1261
8264deb8
G
1262static int tcp_timeout_nlattr_to_obj(struct nlattr *tb[],
1263 struct net *net, void *data)
50978462
PNA
1264{
1265 unsigned int *timeouts = data;
8264deb8 1266 struct nf_tcp_net *tn = tcp_pernet(net);
50978462
PNA
1267 int i;
1268
1269 /* set default TCP timeouts. */
1270 for (i=0; i<TCP_CONNTRACK_TIMEOUT_MAX; i++)
8264deb8 1271 timeouts[i] = tn->timeouts[i];
50978462
PNA
1272
1273 if (tb[CTA_TIMEOUT_TCP_SYN_SENT]) {
1274 timeouts[TCP_CONNTRACK_SYN_SENT] =
1275 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT]))*HZ;
1276 }
1277 if (tb[CTA_TIMEOUT_TCP_SYN_RECV]) {
1278 timeouts[TCP_CONNTRACK_SYN_RECV] =
1279 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_RECV]))*HZ;
1280 }
1281 if (tb[CTA_TIMEOUT_TCP_ESTABLISHED]) {
1282 timeouts[TCP_CONNTRACK_ESTABLISHED] =
1283 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_ESTABLISHED]))*HZ;
1284 }
1285 if (tb[CTA_TIMEOUT_TCP_FIN_WAIT]) {
1286 timeouts[TCP_CONNTRACK_FIN_WAIT] =
1287 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_FIN_WAIT]))*HZ;
1288 }
1289 if (tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]) {
1290 timeouts[TCP_CONNTRACK_CLOSE_WAIT] =
1291 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]))*HZ;
1292 }
1293 if (tb[CTA_TIMEOUT_TCP_LAST_ACK]) {
1294 timeouts[TCP_CONNTRACK_LAST_ACK] =
1295 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_LAST_ACK]))*HZ;
1296 }
1297 if (tb[CTA_TIMEOUT_TCP_TIME_WAIT]) {
1298 timeouts[TCP_CONNTRACK_TIME_WAIT] =
1299 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_TIME_WAIT]))*HZ;
1300 }
1301 if (tb[CTA_TIMEOUT_TCP_CLOSE]) {
1302 timeouts[TCP_CONNTRACK_CLOSE] =
1303 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE]))*HZ;
1304 }
1305 if (tb[CTA_TIMEOUT_TCP_SYN_SENT2]) {
1306 timeouts[TCP_CONNTRACK_SYN_SENT2] =
1307 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT2]))*HZ;
1308 }
1309 if (tb[CTA_TIMEOUT_TCP_RETRANS]) {
1310 timeouts[TCP_CONNTRACK_RETRANS] =
1311 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_RETRANS]))*HZ;
1312 }
1313 if (tb[CTA_TIMEOUT_TCP_UNACK]) {
1314 timeouts[TCP_CONNTRACK_UNACK] =
1315 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_UNACK]))*HZ;
1316 }
1317 return 0;
1318}
1319
1320static int
1321tcp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
1322{
1323 const unsigned int *timeouts = data;
1324
4925a459
DM
1325 if (nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT,
1326 htonl(timeouts[TCP_CONNTRACK_SYN_SENT] / HZ)) ||
1327 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_RECV,
1328 htonl(timeouts[TCP_CONNTRACK_SYN_RECV] / HZ)) ||
1329 nla_put_be32(skb, CTA_TIMEOUT_TCP_ESTABLISHED,
1330 htonl(timeouts[TCP_CONNTRACK_ESTABLISHED] / HZ)) ||
1331 nla_put_be32(skb, CTA_TIMEOUT_TCP_FIN_WAIT,
1332 htonl(timeouts[TCP_CONNTRACK_FIN_WAIT] / HZ)) ||
1333 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE_WAIT,
1334 htonl(timeouts[TCP_CONNTRACK_CLOSE_WAIT] / HZ)) ||
1335 nla_put_be32(skb, CTA_TIMEOUT_TCP_LAST_ACK,
1336 htonl(timeouts[TCP_CONNTRACK_LAST_ACK] / HZ)) ||
1337 nla_put_be32(skb, CTA_TIMEOUT_TCP_TIME_WAIT,
1338 htonl(timeouts[TCP_CONNTRACK_TIME_WAIT] / HZ)) ||
1339 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE,
1340 htonl(timeouts[TCP_CONNTRACK_CLOSE] / HZ)) ||
1341 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT2,
1342 htonl(timeouts[TCP_CONNTRACK_SYN_SENT2] / HZ)) ||
1343 nla_put_be32(skb, CTA_TIMEOUT_TCP_RETRANS,
1344 htonl(timeouts[TCP_CONNTRACK_RETRANS] / HZ)) ||
1345 nla_put_be32(skb, CTA_TIMEOUT_TCP_UNACK,
1346 htonl(timeouts[TCP_CONNTRACK_UNACK] / HZ)))
1347 goto nla_put_failure;
50978462
PNA
1348 return 0;
1349
1350nla_put_failure:
1351 return -ENOSPC;
1352}
1353
1354static const struct nla_policy tcp_timeout_nla_policy[CTA_TIMEOUT_TCP_MAX+1] = {
1355 [CTA_TIMEOUT_TCP_SYN_SENT] = { .type = NLA_U32 },
1356 [CTA_TIMEOUT_TCP_SYN_RECV] = { .type = NLA_U32 },
1357 [CTA_TIMEOUT_TCP_ESTABLISHED] = { .type = NLA_U32 },
1358 [CTA_TIMEOUT_TCP_FIN_WAIT] = { .type = NLA_U32 },
1359 [CTA_TIMEOUT_TCP_CLOSE_WAIT] = { .type = NLA_U32 },
1360 [CTA_TIMEOUT_TCP_LAST_ACK] = { .type = NLA_U32 },
1361 [CTA_TIMEOUT_TCP_TIME_WAIT] = { .type = NLA_U32 },
1362 [CTA_TIMEOUT_TCP_CLOSE] = { .type = NLA_U32 },
1363 [CTA_TIMEOUT_TCP_SYN_SENT2] = { .type = NLA_U32 },
6d1fafca
FW
1364 [CTA_TIMEOUT_TCP_RETRANS] = { .type = NLA_U32 },
1365 [CTA_TIMEOUT_TCP_UNACK] = { .type = NLA_U32 },
50978462
PNA
1366};
1367#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1368
933a41e7 1369#ifdef CONFIG_SYSCTL
933a41e7
PM
1370static struct ctl_table tcp_sysctl_table[] = {
1371 {
933a41e7 1372 .procname = "nf_conntrack_tcp_timeout_syn_sent",
933a41e7
PM
1373 .maxlen = sizeof(unsigned int),
1374 .mode = 0644,
6d9f239a 1375 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1376 },
1377 {
933a41e7 1378 .procname = "nf_conntrack_tcp_timeout_syn_recv",
933a41e7
PM
1379 .maxlen = sizeof(unsigned int),
1380 .mode = 0644,
6d9f239a 1381 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1382 },
1383 {
933a41e7 1384 .procname = "nf_conntrack_tcp_timeout_established",
933a41e7
PM
1385 .maxlen = sizeof(unsigned int),
1386 .mode = 0644,
6d9f239a 1387 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1388 },
1389 {
933a41e7 1390 .procname = "nf_conntrack_tcp_timeout_fin_wait",
933a41e7
PM
1391 .maxlen = sizeof(unsigned int),
1392 .mode = 0644,
6d9f239a 1393 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1394 },
1395 {
933a41e7 1396 .procname = "nf_conntrack_tcp_timeout_close_wait",
933a41e7
PM
1397 .maxlen = sizeof(unsigned int),
1398 .mode = 0644,
6d9f239a 1399 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1400 },
1401 {
933a41e7 1402 .procname = "nf_conntrack_tcp_timeout_last_ack",
933a41e7
PM
1403 .maxlen = sizeof(unsigned int),
1404 .mode = 0644,
6d9f239a 1405 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1406 },
1407 {
933a41e7 1408 .procname = "nf_conntrack_tcp_timeout_time_wait",
933a41e7
PM
1409 .maxlen = sizeof(unsigned int),
1410 .mode = 0644,
6d9f239a 1411 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1412 },
1413 {
933a41e7 1414 .procname = "nf_conntrack_tcp_timeout_close",
933a41e7
PM
1415 .maxlen = sizeof(unsigned int),
1416 .mode = 0644,
6d9f239a 1417 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1418 },
1419 {
933a41e7 1420 .procname = "nf_conntrack_tcp_timeout_max_retrans",
933a41e7
PM
1421 .maxlen = sizeof(unsigned int),
1422 .mode = 0644,
6d9f239a 1423 .proc_handler = proc_dointvec_jiffies,
933a41e7 1424 },
ae375044
PM
1425 {
1426 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
ae375044
PM
1427 .maxlen = sizeof(unsigned int),
1428 .mode = 0644,
6d9f239a 1429 .proc_handler = proc_dointvec_jiffies,
ae375044 1430 },
933a41e7 1431 {
933a41e7 1432 .procname = "nf_conntrack_tcp_loose",
933a41e7
PM
1433 .maxlen = sizeof(unsigned int),
1434 .mode = 0644,
6d9f239a 1435 .proc_handler = proc_dointvec,
933a41e7
PM
1436 },
1437 {
933a41e7 1438 .procname = "nf_conntrack_tcp_be_liberal",
933a41e7
PM
1439 .maxlen = sizeof(unsigned int),
1440 .mode = 0644,
6d9f239a 1441 .proc_handler = proc_dointvec,
933a41e7
PM
1442 },
1443 {
933a41e7 1444 .procname = "nf_conntrack_tcp_max_retrans",
933a41e7
PM
1445 .maxlen = sizeof(unsigned int),
1446 .mode = 0644,
6d9f239a 1447 .proc_handler = proc_dointvec,
933a41e7 1448 },
6fa3eb70
S
1449 {
1450 .procname = "nf_conntrack_tcp_no_window_check",
1451 .maxlen = sizeof(unsigned int),
1452 .mode = 0644,
1453 .proc_handler = proc_dointvec,
1454 },
f8572d8f 1455 { }
933a41e7 1456};
a999e683
PM
1457
1458#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1459static struct ctl_table tcp_compat_sysctl_table[] = {
1460 {
a999e683 1461 .procname = "ip_conntrack_tcp_timeout_syn_sent",
a999e683
PM
1462 .maxlen = sizeof(unsigned int),
1463 .mode = 0644,
6d9f239a 1464 .proc_handler = proc_dointvec_jiffies,
a999e683 1465 },
874ab923
JK
1466 {
1467 .procname = "ip_conntrack_tcp_timeout_syn_sent2",
874ab923
JK
1468 .maxlen = sizeof(unsigned int),
1469 .mode = 0644,
1470 .proc_handler = proc_dointvec_jiffies,
1471 },
a999e683 1472 {
a999e683 1473 .procname = "ip_conntrack_tcp_timeout_syn_recv",
a999e683
PM
1474 .maxlen = sizeof(unsigned int),
1475 .mode = 0644,
6d9f239a 1476 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1477 },
1478 {
a999e683 1479 .procname = "ip_conntrack_tcp_timeout_established",
a999e683
PM
1480 .maxlen = sizeof(unsigned int),
1481 .mode = 0644,
6d9f239a 1482 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1483 },
1484 {
a999e683 1485 .procname = "ip_conntrack_tcp_timeout_fin_wait",
a999e683
PM
1486 .maxlen = sizeof(unsigned int),
1487 .mode = 0644,
6d9f239a 1488 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1489 },
1490 {
a999e683 1491 .procname = "ip_conntrack_tcp_timeout_close_wait",
a999e683
PM
1492 .maxlen = sizeof(unsigned int),
1493 .mode = 0644,
6d9f239a 1494 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1495 },
1496 {
a999e683 1497 .procname = "ip_conntrack_tcp_timeout_last_ack",
a999e683
PM
1498 .maxlen = sizeof(unsigned int),
1499 .mode = 0644,
6d9f239a 1500 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1501 },
1502 {
a999e683 1503 .procname = "ip_conntrack_tcp_timeout_time_wait",
a999e683
PM
1504 .maxlen = sizeof(unsigned int),
1505 .mode = 0644,
6d9f239a 1506 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1507 },
1508 {
a999e683 1509 .procname = "ip_conntrack_tcp_timeout_close",
a999e683
PM
1510 .maxlen = sizeof(unsigned int),
1511 .mode = 0644,
6d9f239a 1512 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1513 },
1514 {
a999e683 1515 .procname = "ip_conntrack_tcp_timeout_max_retrans",
a999e683
PM
1516 .maxlen = sizeof(unsigned int),
1517 .mode = 0644,
6d9f239a 1518 .proc_handler = proc_dointvec_jiffies,
a999e683
PM
1519 },
1520 {
a999e683 1521 .procname = "ip_conntrack_tcp_loose",
a999e683
PM
1522 .maxlen = sizeof(unsigned int),
1523 .mode = 0644,
6d9f239a 1524 .proc_handler = proc_dointvec,
a999e683
PM
1525 },
1526 {
a999e683 1527 .procname = "ip_conntrack_tcp_be_liberal",
a999e683
PM
1528 .maxlen = sizeof(unsigned int),
1529 .mode = 0644,
6d9f239a 1530 .proc_handler = proc_dointvec,
a999e683
PM
1531 },
1532 {
a999e683 1533 .procname = "ip_conntrack_tcp_max_retrans",
a999e683
PM
1534 .maxlen = sizeof(unsigned int),
1535 .mode = 0644,
6d9f239a 1536 .proc_handler = proc_dointvec,
a999e683 1537 },
6fa3eb70
S
1538 {
1539 .procname = "ip_conntrack_tcp_no_window_check",
1540 .maxlen = sizeof(unsigned int),
1541 .mode = 0644,
1542 .proc_handler = proc_dointvec,
1543 },
f8572d8f 1544 { }
a999e683
PM
1545};
1546#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7
PM
1547#endif /* CONFIG_SYSCTL */
1548
efa758fe
G
1549static int tcp_kmemdup_sysctl_table(struct nf_proto_net *pn,
1550 struct nf_tcp_net *tn)
d2ba1fde
G
1551{
1552#ifdef CONFIG_SYSCTL
d2ba1fde
G
1553 if (pn->ctl_table)
1554 return 0;
1555
1556 pn->ctl_table = kmemdup(tcp_sysctl_table,
1557 sizeof(tcp_sysctl_table),
1558 GFP_KERNEL);
1559 if (!pn->ctl_table)
1560 return -ENOMEM;
1561
1562 pn->ctl_table[0].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT];
1563 pn->ctl_table[1].data = &tn->timeouts[TCP_CONNTRACK_SYN_RECV];
1564 pn->ctl_table[2].data = &tn->timeouts[TCP_CONNTRACK_ESTABLISHED];
1565 pn->ctl_table[3].data = &tn->timeouts[TCP_CONNTRACK_FIN_WAIT];
1566 pn->ctl_table[4].data = &tn->timeouts[TCP_CONNTRACK_CLOSE_WAIT];
1567 pn->ctl_table[5].data = &tn->timeouts[TCP_CONNTRACK_LAST_ACK];
1568 pn->ctl_table[6].data = &tn->timeouts[TCP_CONNTRACK_TIME_WAIT];
1569 pn->ctl_table[7].data = &tn->timeouts[TCP_CONNTRACK_CLOSE];
1570 pn->ctl_table[8].data = &tn->timeouts[TCP_CONNTRACK_RETRANS];
1571 pn->ctl_table[9].data = &tn->timeouts[TCP_CONNTRACK_UNACK];
1572 pn->ctl_table[10].data = &tn->tcp_loose;
1573 pn->ctl_table[11].data = &tn->tcp_be_liberal;
1574 pn->ctl_table[12].data = &tn->tcp_max_retrans;
6fa3eb70 1575 pn->ctl_table[13].data = &nf_ct_tcp_no_window_check;
d2ba1fde
G
1576#endif
1577 return 0;
1578}
1579
efa758fe
G
1580static int tcp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
1581 struct nf_tcp_net *tn)
d2ba1fde
G
1582{
1583#ifdef CONFIG_SYSCTL
1584#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
d2ba1fde
G
1585 pn->ctl_compat_table = kmemdup(tcp_compat_sysctl_table,
1586 sizeof(tcp_compat_sysctl_table),
1587 GFP_KERNEL);
1588 if (!pn->ctl_compat_table)
1589 return -ENOMEM;
1590
1591 pn->ctl_compat_table[0].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT];
1592 pn->ctl_compat_table[1].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT2];
1593 pn->ctl_compat_table[2].data = &tn->timeouts[TCP_CONNTRACK_SYN_RECV];
1594 pn->ctl_compat_table[3].data = &tn->timeouts[TCP_CONNTRACK_ESTABLISHED];
1595 pn->ctl_compat_table[4].data = &tn->timeouts[TCP_CONNTRACK_FIN_WAIT];
1596 pn->ctl_compat_table[5].data = &tn->timeouts[TCP_CONNTRACK_CLOSE_WAIT];
1597 pn->ctl_compat_table[6].data = &tn->timeouts[TCP_CONNTRACK_LAST_ACK];
1598 pn->ctl_compat_table[7].data = &tn->timeouts[TCP_CONNTRACK_TIME_WAIT];
1599 pn->ctl_compat_table[8].data = &tn->timeouts[TCP_CONNTRACK_CLOSE];
1600 pn->ctl_compat_table[9].data = &tn->timeouts[TCP_CONNTRACK_RETRANS];
1601 pn->ctl_compat_table[10].data = &tn->tcp_loose;
1602 pn->ctl_compat_table[11].data = &tn->tcp_be_liberal;
1603 pn->ctl_compat_table[12].data = &tn->tcp_max_retrans;
6fa3eb70 1604 pn->ctl_compat_table[13].data = &nf_ct_tcp_no_window_check;
d2ba1fde
G
1605#endif
1606#endif
1607 return 0;
1608}
1609
efa758fe 1610static int tcp_init_net(struct net *net, u_int16_t proto)
d2ba1fde 1611{
efa758fe 1612 int ret;
d2ba1fde 1613 struct nf_tcp_net *tn = tcp_pernet(net);
efa758fe
G
1614 struct nf_proto_net *pn = &tn->pn;
1615
1616 if (!pn->users) {
1617 int i;
d2ba1fde 1618
d2ba1fde
G
1619 for (i = 0; i < TCP_CONNTRACK_TIMEOUT_MAX; i++)
1620 tn->timeouts[i] = tcp_timeouts[i];
1621
1622 tn->tcp_loose = nf_ct_tcp_loose;
1623 tn->tcp_be_liberal = nf_ct_tcp_be_liberal;
1624 tn->tcp_max_retrans = nf_ct_tcp_max_retrans;
1625 }
1626
efa758fe
G
1627 if (proto == AF_INET) {
1628 ret = tcp_kmemdup_compat_sysctl_table(pn, tn);
1629 if (ret < 0)
1630 return ret;
d2ba1fde 1631
efa758fe
G
1632 ret = tcp_kmemdup_sysctl_table(pn, tn);
1633 if (ret < 0)
1634 nf_ct_kfree_compat_sysctl_table(pn);
1635 } else
1636 ret = tcp_kmemdup_sysctl_table(pn, tn);
d2ba1fde 1637
d2ba1fde
G
1638 return ret;
1639}
1640
08911475
PNA
1641static struct nf_proto_net *tcp_get_net_proto(struct net *net)
1642{
1643 return &net->ct.nf_ct_proto.tcp.pn;
1644}
1645
61075af5 1646struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
9fb9cbb1
YK
1647{
1648 .l3proto = PF_INET,
605dcad6 1649 .l4proto = IPPROTO_TCP,
9fb9cbb1
YK
1650 .name = "tcp",
1651 .pkt_to_tuple = tcp_pkt_to_tuple,
1652 .invert_tuple = tcp_invert_tuple,
1653 .print_tuple = tcp_print_tuple,
1654 .print_conntrack = tcp_print_conntrack,
1655 .packet = tcp_packet,
2c8503f5 1656 .get_timeouts = tcp_get_timeouts,
9fb9cbb1 1657 .new = tcp_new,
96f6bf82 1658 .error = tcp_error,
c0cd1156 1659#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 1660 .to_nlattr = tcp_to_nlattr,
a400c30e 1661 .nlattr_size = tcp_nlattr_size,
fdf70832
PM
1662 .from_nlattr = nlattr_to_tcp,
1663 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1664 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
a400c30e 1665 .nlattr_tuple_size = tcp_nlattr_tuple_size,
f73e924c 1666 .nla_policy = nf_ct_port_nla_policy,
c1d10adb 1667#endif
50978462
PNA
1668#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1669 .ctnl_timeout = {
1670 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1671 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1672 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1673 .obj_size = sizeof(unsigned int) *
1674 TCP_CONNTRACK_TIMEOUT_MAX,
1675 .nla_policy = tcp_timeout_nla_policy,
1676 },
1677#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
efa758fe 1678 .init_net = tcp_init_net,
08911475 1679 .get_net_proto = tcp_get_net_proto,
9fb9cbb1 1680};
13b18339 1681EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
9fb9cbb1 1682
61075af5 1683struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
9fb9cbb1
YK
1684{
1685 .l3proto = PF_INET6,
605dcad6 1686 .l4proto = IPPROTO_TCP,
9fb9cbb1
YK
1687 .name = "tcp",
1688 .pkt_to_tuple = tcp_pkt_to_tuple,
1689 .invert_tuple = tcp_invert_tuple,
1690 .print_tuple = tcp_print_tuple,
1691 .print_conntrack = tcp_print_conntrack,
1692 .packet = tcp_packet,
2c8503f5 1693 .get_timeouts = tcp_get_timeouts,
9fb9cbb1 1694 .new = tcp_new,
96f6bf82 1695 .error = tcp_error,
c0cd1156 1696#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 1697 .to_nlattr = tcp_to_nlattr,
a400c30e 1698 .nlattr_size = tcp_nlattr_size,
fdf70832
PM
1699 .from_nlattr = nlattr_to_tcp,
1700 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1701 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
a400c30e 1702 .nlattr_tuple_size = tcp_nlattr_tuple_size,
f73e924c 1703 .nla_policy = nf_ct_port_nla_policy,
c1d10adb 1704#endif
50978462
PNA
1705#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1706 .ctnl_timeout = {
1707 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1708 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1709 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1710 .obj_size = sizeof(unsigned int) *
1711 TCP_CONNTRACK_TIMEOUT_MAX,
1712 .nla_policy = tcp_timeout_nla_policy,
1713 },
1714#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
efa758fe 1715 .init_net = tcp_init_net,
08911475 1716 .get_net_proto = tcp_get_net_proto,
9fb9cbb1 1717};
13b18339 1718EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);