]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nf_conntrack_proto_tcp.c
netfilter: conntrack: place print_tuple in procfs part
[mirror_ubuntu-bionic-kernel.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>
41d73ec0 30#include <net/netfilter/nf_conntrack_seqadj.h>
48b1de4c 31#include <net/netfilter/nf_conntrack_synproxy.h>
f01ffbd6 32#include <net/netfilter/nf_log.h>
9d2493f8
CP
33#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
34#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
9fb9cbb1 35
601e68e1
YH
36/* "Be conservative in what you do,
37 be liberal in what you accept from others."
9fb9cbb1 38 If it's non-zero, we mark only out of window RST segments as INVALID. */
3aef0fd9 39static int nf_ct_tcp_be_liberal __read_mostly = 0;
9fb9cbb1 40
a09113c2 41/* If it is set to zero, we disable picking up already established
9fb9cbb1 42 connections. */
3aef0fd9 43static int nf_ct_tcp_loose __read_mostly = 1;
9fb9cbb1 44
601e68e1
YH
45/* Max number of the retransmitted packets without receiving an (acceptable)
46 ACK from the destination. If this number is reached, a shorter timer
9fb9cbb1 47 will be started. */
3aef0fd9 48static int nf_ct_tcp_max_retrans __read_mostly = 3;
9fb9cbb1
YK
49
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
b3cad287 205 * sLA -> sTW Last ACK detected (RFC5961 challenged)
9fb9cbb1
YK
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 215/* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2 */
e37ad9fd 216/*syn*/ { sIV, sS2, sIV, sIV, sIV, sIV, sIV, sSS, 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
e37ad9fd 226 * sTW -> sSS Reopened connection, but server may have switched role
9fb9cbb1
YK
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
b3cad287 264 * sLA -> sTW Last ACK detected (RFC5961 challenged)
9fb9cbb1
YK
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 279static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
a31f1adc 280 struct net *net, struct nf_conntrack_tuple *tuple)
9fb9cbb1 281{
82f568fc
JE
282 const struct tcphdr *hp;
283 struct tcphdr _hdr;
9fb9cbb1 284
e5e693ab
GF
285 /* Actually only need first 4 bytes to get ports. */
286 hp = skb_header_pointer(skb, dataoff, 4, &_hdr);
9fb9cbb1 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
9fb9cbb1 304/* Print out the private part of the conntrack. */
37246a58 305static void tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
9fb9cbb1 306{
a163f2cb 307 seq_printf(s, "%s ", tcp_conntrack_names[ct->proto.tcp.state]);
9fb9cbb1
YK
308}
309
310static unsigned int get_conntrack_index(const struct tcphdr *tcph)
311{
312 if (tcph->rst) return TCP_RST_SET;
313 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
314 else if (tcph->fin) return TCP_FIN_SET;
315 else if (tcph->ack) return TCP_ACK_SET;
316 else return TCP_NONE_SET;
317}
318
319/* TCP connection tracking based on 'Real Stateful TCP Packet Filtering
320 in IP Filter' by Guido van Rooij.
601e68e1 321
631dd1a8
JM
322 http://www.sane.nl/events/sane2000/papers.html
323 http://www.darkart.com/mirrors/www.obfuscation.org/ipf/
601e68e1 324
9fb9cbb1
YK
325 The boundaries and the conditions are changed according to RFC793:
326 the packet must intersect the window (i.e. segments may be
327 after the right or before the left edge) and thus receivers may ACK
328 segments after the right edge of the window.
329
601e68e1 330 td_maxend = max(sack + max(win,1)) seen in reply packets
9fb9cbb1
YK
331 td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets
332 td_maxwin += seq + len - sender.td_maxend
333 if seq + len > sender.td_maxend
334 td_end = max(seq + len) seen in sent packets
601e68e1 335
9fb9cbb1
YK
336 I. Upper bound for valid data: seq <= sender.td_maxend
337 II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin
84ebe1cd
JK
338 III. Upper bound for valid (s)ack: sack <= receiver.td_end
339 IV. Lower bound for valid (s)ack: sack >= receiver.td_end - MAXACKWINDOW
9fb9cbb1 340
84ebe1cd
JK
341 where sack is the highest right edge of sack block found in the packet
342 or ack in the case of packet without SACK option.
9fb9cbb1 343
84ebe1cd 344 The upper bound limit for a valid (s)ack is not ignored -
601e68e1 345 we doesn't have to deal with fragments.
9fb9cbb1
YK
346*/
347
348static inline __u32 segment_seq_plus_len(__u32 seq,
349 size_t len,
350 unsigned int dataoff,
82f568fc 351 const struct tcphdr *tcph)
9fb9cbb1
YK
352{
353 /* XXX Should I use payload length field in IP/IPv6 header ?
354 * - YK */
355 return (seq + len - dataoff - tcph->doff*4
356 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
357}
601e68e1 358
9fb9cbb1
YK
359/* Fixme: what about big packets? */
360#define MAXACKWINCONST 66000
361#define MAXACKWINDOW(sender) \
362 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
363 : MAXACKWINCONST)
601e68e1 364
9fb9cbb1
YK
365/*
366 * Simplified tcp_parse_options routine from tcp_input.c
367 */
368static void tcp_options(const struct sk_buff *skb,
369 unsigned int dataoff,
82f568fc 370 const struct tcphdr *tcph,
9fb9cbb1
YK
371 struct ip_ct_tcp_state *state)
372{
373 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
82f568fc 374 const unsigned char *ptr;
9fb9cbb1
YK
375 int length = (tcph->doff*4) - sizeof(struct tcphdr);
376
377 if (!length)
378 return;
379
380 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
381 length, buff);
382 BUG_ON(ptr == NULL);
383
601e68e1 384 state->td_scale =
9fb9cbb1
YK
385 state->flags = 0;
386
387 while (length > 0) {
388 int opcode=*ptr++;
389 int opsize;
390
391 switch (opcode) {
392 case TCPOPT_EOL:
393 return;
394 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
395 length--;
396 continue;
397 default:
644c7e48
JK
398 if (length < 2)
399 return;
9fb9cbb1
YK
400 opsize=*ptr++;
401 if (opsize < 2) /* "silly options" */
402 return;
403 if (opsize > length)
4a5cc84a 404 return; /* don't parse partial options */
9fb9cbb1 405
601e68e1 406 if (opcode == TCPOPT_SACK_PERM
9fb9cbb1
YK
407 && opsize == TCPOLEN_SACK_PERM)
408 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
409 else if (opcode == TCPOPT_WINDOW
410 && opsize == TCPOLEN_WINDOW) {
411 state->td_scale = *(u_int8_t *)ptr;
412
122868b3
GF
413 if (state->td_scale > TCP_MAX_WSCALE)
414 state->td_scale = TCP_MAX_WSCALE;
415
9fb9cbb1
YK
416 state->flags |=
417 IP_CT_TCP_FLAG_WINDOW_SCALE;
418 }
419 ptr += opsize - 2;
420 length -= opsize;
421 }
422 }
423}
424
425static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
82f568fc 426 const struct tcphdr *tcph, __u32 *sack)
9fb9cbb1 427{
601e68e1 428 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
82f568fc 429 const unsigned char *ptr;
9fb9cbb1
YK
430 int length = (tcph->doff*4) - sizeof(struct tcphdr);
431 __u32 tmp;
432
433 if (!length)
434 return;
435
436 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
437 length, buff);
438 BUG_ON(ptr == NULL);
439
440 /* Fast path for timestamp-only option */
bb9fc373 441 if (length == TCPOLEN_TSTAMP_ALIGNED
8f05ce91
YH
442 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
443 | (TCPOPT_NOP << 16)
444 | (TCPOPT_TIMESTAMP << 8)
445 | TCPOLEN_TIMESTAMP))
9fb9cbb1
YK
446 return;
447
448 while (length > 0) {
449 int opcode = *ptr++;
450 int opsize, i;
451
452 switch (opcode) {
453 case TCPOPT_EOL:
454 return;
455 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
456 length--;
457 continue;
458 default:
644c7e48
JK
459 if (length < 2)
460 return;
9fb9cbb1
YK
461 opsize = *ptr++;
462 if (opsize < 2) /* "silly options" */
463 return;
464 if (opsize > length)
4a5cc84a 465 return; /* don't parse partial options */
9fb9cbb1 466
601e68e1
YH
467 if (opcode == TCPOPT_SACK
468 && opsize >= (TCPOLEN_SACK_BASE
469 + TCPOLEN_SACK_PERBLOCK)
470 && !((opsize - TCPOLEN_SACK_BASE)
471 % TCPOLEN_SACK_PERBLOCK)) {
472 for (i = 0;
473 i < (opsize - TCPOLEN_SACK_BASE);
474 i += TCPOLEN_SACK_PERBLOCK) {
534f81a5 475 tmp = get_unaligned_be32((__be32 *)(ptr+i)+1);
9fb9cbb1
YK
476
477 if (after(tmp, *sack))
478 *sack = tmp;
479 }
480 return;
481 }
482 ptr += opsize - 2;
483 length -= opsize;
484 }
485 }
486}
487
09f263cd
JE
488static bool tcp_in_window(const struct nf_conn *ct,
489 struct ip_ct_tcp *state,
490 enum ip_conntrack_dir dir,
491 unsigned int index,
492 const struct sk_buff *skb,
493 unsigned int dataoff,
494 const struct tcphdr *tcph,
76108cea 495 u_int8_t pf)
9fb9cbb1 496{
c2a2c7e0 497 struct net *net = nf_ct_net(ct);
d2ba1fde 498 struct nf_tcp_net *tn = tcp_pernet(net);
9fb9cbb1
YK
499 struct ip_ct_tcp_state *sender = &state->seen[dir];
500 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
82f568fc 501 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
9fb9cbb1 502 __u32 seq, ack, sack, end, win, swin;
2d89c68a 503 s32 receiver_offset;
356d7d88 504 bool res, in_recv_win;
9fb9cbb1
YK
505
506 /*
507 * Get the required data from the packet.
508 */
509 seq = ntohl(tcph->seq);
510 ack = sack = ntohl(tcph->ack_seq);
511 win = ntohs(tcph->window);
512 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
513
514 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
515 tcp_sack(skb, dataoff, tcph, &sack);
516
f9dd09c7 517 /* Take into account NAT sequence number mangling */
41d73ec0 518 receiver_offset = nf_ct_seq_offset(ct, !dir, ack - 1);
f9dd09c7
JK
519 ack -= receiver_offset;
520 sack -= receiver_offset;
521
0d53778e
PM
522 pr_debug("tcp_in_window: START\n");
523 pr_debug("tcp_in_window: ");
3c9fba65 524 nf_ct_dump_tuple(tuple);
f9dd09c7
JK
525 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
526 seq, ack, receiver_offset, sack, receiver_offset, win, end);
0d53778e
PM
527 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
528 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
529 sender->td_end, sender->td_maxend, sender->td_maxwin,
530 sender->td_scale,
531 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
532 receiver->td_scale);
9fb9cbb1 533
874ab923 534 if (sender->td_maxwin == 0) {
9fb9cbb1
YK
535 /*
536 * Initialize sender data.
537 */
874ab923 538 if (tcph->syn) {
9fb9cbb1 539 /*
874ab923
JK
540 * SYN-ACK in reply to a SYN
541 * or SYN from reply direction in simultaneous open.
9fb9cbb1 542 */
601e68e1 543 sender->td_end =
9fb9cbb1
YK
544 sender->td_maxend = end;
545 sender->td_maxwin = (win == 0 ? 1 : win);
546
547 tcp_options(skb, dataoff, tcph, sender);
601e68e1 548 /*
9fb9cbb1
YK
549 * RFC 1323:
550 * Both sides must send the Window Scale option
551 * to enable window scaling in either direction.
552 */
553 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
554 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
601e68e1 555 sender->td_scale =
9fb9cbb1 556 receiver->td_scale = 0;
874ab923
JK
557 if (!tcph->ack)
558 /* Simultaneous open */
559 return true;
9fb9cbb1
YK
560 } else {
561 /*
562 * We are in the middle of a connection,
563 * its history is lost for us.
564 * Let's try to use the data from the packet.
601e68e1 565 */
9fb9cbb1 566 sender->td_end = end;
6ee0b693
CG
567 swin = win << sender->td_scale;
568 sender->td_maxwin = (swin == 0 ? 1 : swin);
9fb9cbb1 569 sender->td_maxend = end + sender->td_maxwin;
fac42a9a
PNA
570 /*
571 * We haven't seen traffic in the other direction yet
572 * but we have to tweak window tracking to pass III
573 * and IV until that happens.
574 */
575 if (receiver->td_maxwin == 0)
576 receiver->td_end = receiver->td_maxend = sack;
9fb9cbb1
YK
577 }
578 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
579 && dir == IP_CT_DIR_ORIGINAL)
580 || (state->state == TCP_CONNTRACK_SYN_RECV
581 && dir == IP_CT_DIR_REPLY))
582 && after(end, sender->td_end)) {
583 /*
584 * RFC 793: "if a TCP is reinitialized ... then it need
601e68e1 585 * not wait at all; it must only be sure to use sequence
9fb9cbb1
YK
586 * numbers larger than those recently used."
587 */
588 sender->td_end =
589 sender->td_maxend = end;
590 sender->td_maxwin = (win == 0 ? 1 : win);
591
592 tcp_options(skb, dataoff, tcph, sender);
593 }
594
595 if (!(tcph->ack)) {
596 /*
597 * If there is no ACK, just pretend it was set and OK.
598 */
599 ack = sack = receiver->td_end;
601e68e1
YH
600 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
601 (TCP_FLAG_ACK|TCP_FLAG_RST))
9fb9cbb1
YK
602 && (ack == 0)) {
603 /*
604 * Broken TCP stacks, that set ACK in RST packets as well
605 * with zero ack value.
606 */
607 ack = sack = receiver->td_end;
608 }
609
4a70bbfa 610 if (tcph->rst && seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)
9fb9cbb1 611 /*
4a70bbfa 612 * RST sent answering SYN.
9fb9cbb1
YK
613 */
614 seq = end = sender->td_end;
615
0d53778e 616 pr_debug("tcp_in_window: ");
3c9fba65 617 nf_ct_dump_tuple(tuple);
f9dd09c7
JK
618 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
619 seq, ack, receiver_offset, sack, receiver_offset, win, end);
0d53778e
PM
620 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
621 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
622 sender->td_end, sender->td_maxend, sender->td_maxwin,
623 sender->td_scale,
624 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
625 receiver->td_scale);
626
356d7d88
YC
627 /* Is the ending sequence in the receive window (if available)? */
628 in_recv_win = !receiver->td_maxwin ||
629 after(end, sender->td_end - receiver->td_maxwin - 1);
630
0d53778e
PM
631 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
632 before(seq, sender->td_maxend + 1),
356d7d88 633 (in_recv_win ? 1 : 0),
0d53778e 634 before(sack, receiver->td_end + 1),
84ebe1cd 635 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
9fb9cbb1 636
a09113c2 637 if (before(seq, sender->td_maxend + 1) &&
356d7d88 638 in_recv_win &&
a09113c2 639 before(sack, receiver->td_end + 1) &&
84ebe1cd 640 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
601e68e1 641 /*
9fb9cbb1
YK
642 * Take into account window scaling (RFC 1323).
643 */
644 if (!tcph->syn)
645 win <<= sender->td_scale;
646
647 /*
648 * Update sender data.
649 */
650 swin = win + (sack - ack);
651 if (sender->td_maxwin < swin)
652 sender->td_maxwin = swin;
ae375044 653 if (after(end, sender->td_end)) {
9fb9cbb1 654 sender->td_end = end;
ae375044
PM
655 sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
656 }
bfcaa502
JK
657 if (tcph->ack) {
658 if (!(sender->flags & IP_CT_TCP_FLAG_MAXACK_SET)) {
659 sender->td_maxack = ack;
660 sender->flags |= IP_CT_TCP_FLAG_MAXACK_SET;
661 } else if (after(ack, sender->td_maxack))
662 sender->td_maxack = ack;
663 }
664
9fb9cbb1
YK
665 /*
666 * Update receiver data.
667 */
fac42a9a 668 if (receiver->td_maxwin != 0 && after(end, sender->td_maxend))
9fb9cbb1
YK
669 receiver->td_maxwin += end - sender->td_maxend;
670 if (after(sack + win, receiver->td_maxend - 1)) {
671 receiver->td_maxend = sack + win;
672 if (win == 0)
673 receiver->td_maxend++;
674 }
ae375044
PM
675 if (ack == receiver->td_end)
676 receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
9fb9cbb1 677
601e68e1 678 /*
9fb9cbb1
YK
679 * Check retransmissions.
680 */
681 if (index == TCP_ACK_SET) {
682 if (state->last_dir == dir
683 && state->last_seq == seq
684 && state->last_ack == ack
c1fe3ca5
GH
685 && state->last_end == end
686 && state->last_win == win)
9fb9cbb1
YK
687 state->retrans++;
688 else {
689 state->last_dir = dir;
690 state->last_seq = seq;
691 state->last_ack = ack;
692 state->last_end = end;
c1fe3ca5 693 state->last_win = win;
9fb9cbb1
YK
694 state->retrans = 0;
695 }
696 }
09f263cd 697 res = true;
9fb9cbb1 698 } else {
09f263cd 699 res = false;
a09113c2 700 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
d2ba1fde 701 tn->tcp_be_liberal)
09f263cd 702 res = true;
c2a2c7e0 703 if (!res && LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 704 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
705 "nf_ct_tcp: %s ",
706 before(seq, sender->td_maxend + 1) ?
356d7d88 707 in_recv_win ?
9fb9cbb1 708 before(sack, receiver->td_end + 1) ?
f9dd09c7 709 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1) ? "BUG"
9fb9cbb1
YK
710 : "ACK is under the lower bound (possible overly delayed ACK)"
711 : "ACK is over the upper bound (ACKed data not seen yet)"
712 : "SEQ is under the lower bound (already ACKed data retransmitted)"
713 : "SEQ is over the upper bound (over the window of the receiver)");
601e68e1
YH
714 }
715
09f263cd 716 pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
0d53778e
PM
717 "receiver end=%u maxend=%u maxwin=%u\n",
718 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
719 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
9fb9cbb1
YK
720
721 return res;
722}
723
5c8ce7c9 724/* table of valid flag combinations - PUSH, ECE and CWR are always valid */
a3433f35
CG
725static const u8 tcp_valid_flags[(TCPHDR_FIN|TCPHDR_SYN|TCPHDR_RST|TCPHDR_ACK|
726 TCPHDR_URG) + 1] =
9fb9cbb1 727{
a3433f35
CG
728 [TCPHDR_SYN] = 1,
729 [TCPHDR_SYN|TCPHDR_URG] = 1,
730 [TCPHDR_SYN|TCPHDR_ACK] = 1,
731 [TCPHDR_RST] = 1,
732 [TCPHDR_RST|TCPHDR_ACK] = 1,
733 [TCPHDR_FIN|TCPHDR_ACK] = 1,
734 [TCPHDR_FIN|TCPHDR_ACK|TCPHDR_URG] = 1,
735 [TCPHDR_ACK] = 1,
736 [TCPHDR_ACK|TCPHDR_URG] = 1,
9fb9cbb1
YK
737};
738
739/* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */
8fea97ec 740static int tcp_error(struct net *net, struct nf_conn *tmpl,
74c51a14 741 struct sk_buff *skb,
9fb9cbb1 742 unsigned int dataoff,
76108cea 743 u_int8_t pf,
96f6bf82 744 unsigned int hooknum)
9fb9cbb1 745{
82f568fc
JE
746 const struct tcphdr *th;
747 struct tcphdr _tcph;
9fb9cbb1
YK
748 unsigned int tcplen = skb->len - dataoff;
749 u_int8_t tcpflags;
750
751 /* Smaller that minimal TCP header? */
752 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
753 if (th == NULL) {
c2a2c7e0 754 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 755 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
756 "nf_ct_tcp: short packet ");
757 return -NF_ACCEPT;
601e68e1
YH
758 }
759
9fb9cbb1
YK
760 /* Not whole TCP header or malformed packet */
761 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
c2a2c7e0 762 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 763 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
764 "nf_ct_tcp: truncated/malformed packet ");
765 return -NF_ACCEPT;
766 }
601e68e1 767
9fb9cbb1
YK
768 /* Checksum invalid? Ignore.
769 * We skip checking packets on the outgoing path
84fa7933 770 * because the checksum is assumed to be correct.
9fb9cbb1
YK
771 */
772 /* FIXME: Source route IP option packets --RR */
c04d0552 773 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
96f6bf82 774 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
c2a2c7e0 775 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 776 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
777 "nf_ct_tcp: bad TCP checksum ");
778 return -NF_ACCEPT;
779 }
780
781 /* Check TCP flags. */
a3433f35 782 tcpflags = (tcp_flag_byte(th) & ~(TCPHDR_ECE|TCPHDR_CWR|TCPHDR_PSH));
9fb9cbb1 783 if (!tcp_valid_flags[tcpflags]) {
c2a2c7e0 784 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 785 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
786 "nf_ct_tcp: invalid TCP flag combination ");
787 return -NF_ACCEPT;
788 }
789
790 return NF_ACCEPT;
791}
792
2c8503f5
PNA
793static unsigned int *tcp_get_timeouts(struct net *net)
794{
be0593c6 795 return tcp_pernet(net)->timeouts;
2c8503f5
PNA
796}
797
9fb9cbb1 798/* Returns verdict for packet, or -1 for invalid. */
c88130bc 799static int tcp_packet(struct nf_conn *ct,
9fb9cbb1
YK
800 const struct sk_buff *skb,
801 unsigned int dataoff,
802 enum ip_conntrack_info ctinfo,
76108cea 803 u_int8_t pf,
2c8503f5
PNA
804 unsigned int hooknum,
805 unsigned int *timeouts)
9fb9cbb1 806{
c2a2c7e0 807 struct net *net = nf_ct_net(ct);
d2ba1fde 808 struct nf_tcp_net *tn = tcp_pernet(net);
0d53778e 809 struct nf_conntrack_tuple *tuple;
9fb9cbb1
YK
810 enum tcp_conntrack new_state, old_state;
811 enum ip_conntrack_dir dir;
82f568fc
JE
812 const struct tcphdr *th;
813 struct tcphdr _tcph;
9fb9cbb1
YK
814 unsigned long timeout;
815 unsigned int index;
816
817 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
818 BUG_ON(th == NULL);
819
440f0d58 820 spin_lock_bh(&ct->lock);
c88130bc 821 old_state = ct->proto.tcp.state;
9fb9cbb1
YK
822 dir = CTINFO2DIR(ctinfo);
823 index = get_conntrack_index(th);
824 new_state = tcp_conntracks[dir][index][old_state];
c88130bc 825 tuple = &ct->tuplehash[dir].tuple;
9fb9cbb1
YK
826
827 switch (new_state) {
17311393
JK
828 case TCP_CONNTRACK_SYN_SENT:
829 if (old_state < TCP_CONNTRACK_TIME_WAIT)
830 break;
b2155e7f
JK
831 /* RFC 1122: "When a connection is closed actively,
832 * it MUST linger in TIME-WAIT state for a time 2xMSL
833 * (Maximum Segment Lifetime). However, it MAY accept
834 * a new SYN from the remote TCP to reopen the connection
835 * directly from TIME-WAIT state, if..."
836 * We ignore the conditions because we are in the
837 * TIME-WAIT state anyway.
838 *
839 * Handle aborted connections: we and the server
840 * think there is an existing connection but the client
841 * aborts it and starts a new one.
842 */
843 if (((ct->proto.tcp.seen[dir].flags
844 | ct->proto.tcp.seen[!dir].flags)
845 & IP_CT_TCP_FLAG_CLOSE_INIT)
c88130bc
PM
846 || (ct->proto.tcp.last_dir == dir
847 && ct->proto.tcp.last_index == TCP_RST_SET)) {
bc34b841
JK
848 /* Attempt to reopen a closed/aborted connection.
849 * Delete this connection and look up again. */
440f0d58 850 spin_unlock_bh(&ct->lock);
2aec609f 851
6b69fe0c
PM
852 /* Only repeat if we can actually remove the timer.
853 * Destruction may already be in progress in process
854 * context and we must give it a chance to terminate.
855 */
2aec609f 856 if (nf_ct_kill(ct))
6b69fe0c 857 return -NF_REPEAT;
ec8d5409 858 return NF_DROP;
17311393
JK
859 }
860 /* Fall through */
9fb9cbb1 861 case TCP_CONNTRACK_IGNORE:
73f30602 862 /* Ignored packets:
b2155e7f
JK
863 *
864 * Our connection entry may be out of sync, so ignore
865 * packets which may signal the real connection between
866 * the client and the server.
73f30602
JK
867 *
868 * a) SYN in ORIGINAL
869 * b) SYN/ACK in REPLY
601e68e1 870 * c) ACK in reply direction after initial SYN in original.
b2155e7f
JK
871 *
872 * If the ignored packet is invalid, the receiver will send
873 * a RST we'll catch below.
73f30602 874 */
9fb9cbb1 875 if (index == TCP_SYNACK_SET
c88130bc
PM
876 && ct->proto.tcp.last_index == TCP_SYN_SET
877 && ct->proto.tcp.last_dir != dir
878 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
b2155e7f 879 /* b) This SYN/ACK acknowledges a SYN that we earlier
9fb9cbb1
YK
880 * ignored as invalid. This means that the client and
881 * the server are both in sync, while the firewall is
c4832c7b
PNA
882 * not. We get in sync from the previously annotated
883 * values.
9fb9cbb1 884 */
c4832c7b
PNA
885 old_state = TCP_CONNTRACK_SYN_SENT;
886 new_state = TCP_CONNTRACK_SYN_RECV;
887 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_end =
888 ct->proto.tcp.last_end;
889 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxend =
890 ct->proto.tcp.last_end;
891 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxwin =
892 ct->proto.tcp.last_win == 0 ?
893 1 : ct->proto.tcp.last_win;
894 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_scale =
895 ct->proto.tcp.last_wscale;
b3cad287 896 ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
c4832c7b
PNA
897 ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
898 ct->proto.tcp.last_flags;
899 memset(&ct->proto.tcp.seen[dir], 0,
900 sizeof(struct ip_ct_tcp_state));
901 break;
9fb9cbb1 902 }
c88130bc
PM
903 ct->proto.tcp.last_index = index;
904 ct->proto.tcp.last_dir = dir;
905 ct->proto.tcp.last_seq = ntohl(th->seq);
906 ct->proto.tcp.last_end =
9fb9cbb1 907 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
c4832c7b
PNA
908 ct->proto.tcp.last_win = ntohs(th->window);
909
910 /* a) This is a SYN in ORIGINAL. The client and the server
911 * may be in sync but we are not. In that case, we annotate
912 * the TCP options and let the packet go through. If it is a
913 * valid SYN packet, the server will reply with a SYN/ACK, and
b3cad287
JDB
914 * then we'll get in sync. Otherwise, the server potentially
915 * responds with a challenge ACK if implementing RFC5961.
916 */
c4832c7b
PNA
917 if (index == TCP_SYN_SET && dir == IP_CT_DIR_ORIGINAL) {
918 struct ip_ct_tcp_state seen = {};
919
920 ct->proto.tcp.last_flags =
921 ct->proto.tcp.last_wscale = 0;
922 tcp_options(skb, dataoff, th, &seen);
923 if (seen.flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
924 ct->proto.tcp.last_flags |=
925 IP_CT_TCP_FLAG_WINDOW_SCALE;
926 ct->proto.tcp.last_wscale = seen.td_scale;
927 }
928 if (seen.flags & IP_CT_TCP_FLAG_SACK_PERM) {
929 ct->proto.tcp.last_flags |=
930 IP_CT_TCP_FLAG_SACK_PERM;
931 }
b3cad287
JDB
932 /* Mark the potential for RFC5961 challenge ACK,
933 * this pose a special problem for LAST_ACK state
934 * as ACK is intrepretated as ACKing last FIN.
935 */
936 if (old_state == TCP_CONNTRACK_LAST_ACK)
937 ct->proto.tcp.last_flags |=
938 IP_CT_EXP_CHALLENGE_ACK;
c4832c7b 939 }
440f0d58 940 spin_unlock_bh(&ct->lock);
c2a2c7e0 941 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 942 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
1a4ac987
PNA
943 "nf_ct_tcp: invalid packet ignored in "
944 "state %s ", tcp_conntrack_names[old_state]);
9fb9cbb1
YK
945 return NF_ACCEPT;
946 case TCP_CONNTRACK_MAX:
48b1de4c
PM
947 /* Special case for SYN proxy: when the SYN to the server or
948 * the SYN/ACK from the server is lost, the client may transmit
949 * a keep-alive packet while in SYN_SENT state. This needs to
950 * be associated with the original conntrack entry in order to
951 * generate a new SYN with the correct sequence number.
952 */
953 if (nfct_synproxy(ct) && old_state == TCP_CONNTRACK_SYN_SENT &&
954 index == TCP_ACK_SET && dir == IP_CT_DIR_ORIGINAL &&
955 ct->proto.tcp.last_dir == IP_CT_DIR_ORIGINAL &&
956 ct->proto.tcp.seen[dir].td_end - 1 == ntohl(th->seq)) {
957 pr_debug("nf_ct_tcp: SYN proxy client keep alive\n");
958 spin_unlock_bh(&ct->lock);
959 return NF_ACCEPT;
960 }
961
9fb9cbb1 962 /* Invalid packet */
0d53778e
PM
963 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
964 dir, get_conntrack_index(th), old_state);
440f0d58 965 spin_unlock_bh(&ct->lock);
c2a2c7e0 966 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6 967 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
9fb9cbb1
YK
968 "nf_ct_tcp: invalid state ");
969 return -NF_ACCEPT;
b3cad287
JDB
970 case TCP_CONNTRACK_TIME_WAIT:
971 /* RFC5961 compliance cause stack to send "challenge-ACK"
972 * e.g. in response to spurious SYNs. Conntrack MUST
973 * not believe this ACK is acking last FIN.
974 */
975 if (old_state == TCP_CONNTRACK_LAST_ACK &&
976 index == TCP_ACK_SET &&
977 ct->proto.tcp.last_dir != dir &&
978 ct->proto.tcp.last_index == TCP_SYN_SET &&
979 (ct->proto.tcp.last_flags & IP_CT_EXP_CHALLENGE_ACK)) {
980 /* Detected RFC5961 challenge ACK */
981 ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
982 spin_unlock_bh(&ct->lock);
983 if (LOG_INVALID(net, IPPROTO_TCP))
984 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
985 "nf_ct_tcp: challenge-ACK ignored ");
986 return NF_ACCEPT; /* Don't change state */
987 }
988 break;
9fb9cbb1 989 case TCP_CONNTRACK_CLOSE:
bfcaa502
JK
990 if (index == TCP_RST_SET
991 && (ct->proto.tcp.seen[!dir].flags & IP_CT_TCP_FLAG_MAXACK_SET)
992 && before(ntohl(th->seq), ct->proto.tcp.seen[!dir].td_maxack)) {
993 /* Invalid RST */
334a47f6 994 spin_unlock_bh(&ct->lock);
bfcaa502 995 if (LOG_INVALID(net, IPPROTO_TCP))
30e0c6a6
G
996 nf_log_packet(net, pf, 0, skb, NULL, NULL,
997 NULL, "nf_ct_tcp: invalid RST ");
bfcaa502
JK
998 return -NF_ACCEPT;
999 }
9fb9cbb1 1000 if (index == TCP_RST_SET
c88130bc
PM
1001 && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
1002 && ct->proto.tcp.last_index == TCP_SYN_SET)
1003 || (!test_bit(IPS_ASSURED_BIT, &ct->status)
1004 && ct->proto.tcp.last_index == TCP_ACK_SET))
1005 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
93b1fae4 1006 /* RST sent to invalid SYN or ACK we had let through
73f30602
JK
1007 * at a) and c) above:
1008 *
1009 * a) SYN was in window then
1010 * c) we hold a half-open connection.
1011 *
1012 * Delete our connection entry.
9fb9cbb1 1013 * We skip window checking, because packet might ACK
73f30602 1014 * segments we ignored. */
9fb9cbb1
YK
1015 goto in_window;
1016 }
93b1fae4 1017 /* Just fall through */
9fb9cbb1
YK
1018 default:
1019 /* Keep compilers happy. */
1020 break;
1021 }
1022
c88130bc 1023 if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
9fb9cbb1 1024 skb, dataoff, th, pf)) {
440f0d58 1025 spin_unlock_bh(&ct->lock);
9fb9cbb1
YK
1026 return -NF_ACCEPT;
1027 }
1028 in_window:
1029 /* From now on we have got in-window packets */
c88130bc
PM
1030 ct->proto.tcp.last_index = index;
1031 ct->proto.tcp.last_dir = dir;
9fb9cbb1 1032
0d53778e 1033 pr_debug("tcp_conntracks: ");
3c9fba65 1034 nf_ct_dump_tuple(tuple);
0d53778e
PM
1035 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1036 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1037 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1038 old_state, new_state);
9fb9cbb1 1039
c88130bc 1040 ct->proto.tcp.state = new_state;
9fb9cbb1 1041 if (old_state != new_state
d0c1fd7a 1042 && new_state == TCP_CONNTRACK_FIN_WAIT)
c88130bc 1043 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
ae375044 1044
d2ba1fde 1045 if (ct->proto.tcp.retrans >= tn->tcp_max_retrans &&
2c8503f5
PNA
1046 timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS])
1047 timeout = timeouts[TCP_CONNTRACK_RETRANS];
ae375044
PM
1048 else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) &
1049 IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED &&
2c8503f5
PNA
1050 timeouts[new_state] > timeouts[TCP_CONNTRACK_UNACK])
1051 timeout = timeouts[TCP_CONNTRACK_UNACK];
ae375044 1052 else
2c8503f5 1053 timeout = timeouts[new_state];
440f0d58 1054 spin_unlock_bh(&ct->lock);
9fb9cbb1 1055
9fb9cbb1 1056 if (new_state != old_state)
a71996fc 1057 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
9fb9cbb1 1058
c88130bc 1059 if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
9fb9cbb1
YK
1060 /* If only reply is a RST, we can consider ourselves not to
1061 have an established connection: this is a fairly common
1062 problem case, so we can delete the conntrack
1063 immediately. --RR */
1064 if (th->rst) {
718d4ad9 1065 nf_ct_kill_acct(ct, ctinfo, skb);
9fb9cbb1
YK
1066 return NF_ACCEPT;
1067 }
6547a221
FW
1068 /* ESTABLISHED without SEEN_REPLY, i.e. mid-connection
1069 * pickup with loose=1. Avoid large ESTABLISHED timeout.
1070 */
1071 if (new_state == TCP_CONNTRACK_ESTABLISHED &&
1072 timeout > timeouts[TCP_CONNTRACK_UNACK])
1073 timeout = timeouts[TCP_CONNTRACK_UNACK];
c88130bc 1074 } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
9fb9cbb1
YK
1075 && (old_state == TCP_CONNTRACK_SYN_RECV
1076 || old_state == TCP_CONNTRACK_ESTABLISHED)
1077 && new_state == TCP_CONNTRACK_ESTABLISHED) {
601e68e1
YH
1078 /* Set ASSURED if we see see valid ack in ESTABLISHED
1079 after SYN_RECV or a valid answer for a picked up
9fb9cbb1 1080 connection. */
c88130bc 1081 set_bit(IPS_ASSURED_BIT, &ct->status);
858b3133 1082 nf_conntrack_event_cache(IPCT_ASSURED, ct);
9fb9cbb1 1083 }
c88130bc 1084 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
9fb9cbb1
YK
1085
1086 return NF_ACCEPT;
1087}
601e68e1 1088
9fb9cbb1 1089/* Called when a new connection for this protocol found. */
09f263cd 1090static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
2c8503f5 1091 unsigned int dataoff, unsigned int *timeouts)
9fb9cbb1
YK
1092{
1093 enum tcp_conntrack new_state;
82f568fc
JE
1094 const struct tcphdr *th;
1095 struct tcphdr _tcph;
d2ba1fde
G
1096 struct net *net = nf_ct_net(ct);
1097 struct nf_tcp_net *tn = tcp_pernet(net);
82f568fc
JE
1098 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
1099 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
9fb9cbb1
YK
1100
1101 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1102 BUG_ON(th == NULL);
1103
1104 /* Don't need lock here: this conntrack not in circulation yet */
e5fc9e7a 1105 new_state = tcp_conntracks[0][get_conntrack_index(th)][TCP_CONNTRACK_NONE];
9fb9cbb1
YK
1106
1107 /* Invalid: delete conntrack */
1108 if (new_state >= TCP_CONNTRACK_MAX) {
0d53778e 1109 pr_debug("nf_ct_tcp: invalid new deleting.\n");
09f263cd 1110 return false;
9fb9cbb1
YK
1111 }
1112
1113 if (new_state == TCP_CONNTRACK_SYN_SENT) {
e5fc9e7a 1114 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
9fb9cbb1 1115 /* SYN packet */
c88130bc 1116 ct->proto.tcp.seen[0].td_end =
9fb9cbb1
YK
1117 segment_seq_plus_len(ntohl(th->seq), skb->len,
1118 dataoff, th);
c88130bc
PM
1119 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1120 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1121 ct->proto.tcp.seen[0].td_maxwin = 1;
1122 ct->proto.tcp.seen[0].td_maxend =
1123 ct->proto.tcp.seen[0].td_end;
1124
1125 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
d2ba1fde 1126 } else if (tn->tcp_loose == 0) {
9fb9cbb1 1127 /* Don't try to pick up connections. */
09f263cd 1128 return false;
9fb9cbb1 1129 } else {
e5fc9e7a 1130 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
9fb9cbb1
YK
1131 /*
1132 * We are in the middle of a connection,
1133 * its history is lost for us.
1134 * Let's try to use the data from the packet.
1135 */
c88130bc 1136 ct->proto.tcp.seen[0].td_end =
9fb9cbb1
YK
1137 segment_seq_plus_len(ntohl(th->seq), skb->len,
1138 dataoff, th);
c88130bc
PM
1139 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1140 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1141 ct->proto.tcp.seen[0].td_maxwin = 1;
1142 ct->proto.tcp.seen[0].td_maxend =
1143 ct->proto.tcp.seen[0].td_end +
1144 ct->proto.tcp.seen[0].td_maxwin;
9fb9cbb1 1145
a09113c2
PM
1146 /* We assume SACK and liberal window checking to handle
1147 * window scaling */
c88130bc
PM
1148 ct->proto.tcp.seen[0].flags =
1149 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1150 IP_CT_TCP_FLAG_BE_LIBERAL;
9fb9cbb1 1151 }
601e68e1 1152
9fb9cbb1 1153 /* tcp_packet will set them */
c88130bc 1154 ct->proto.tcp.last_index = TCP_NONE_SET;
601e68e1 1155
0d53778e
PM
1156 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1157 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1158 sender->td_end, sender->td_maxend, sender->td_maxwin,
1159 sender->td_scale,
1160 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1161 receiver->td_scale);
09f263cd 1162 return true;
9fb9cbb1 1163}
c1d10adb 1164
c6dd940b
FW
1165static bool tcp_can_early_drop(const struct nf_conn *ct)
1166{
1167 switch (ct->proto.tcp.state) {
1168 case TCP_CONNTRACK_FIN_WAIT:
1169 case TCP_CONNTRACK_LAST_ACK:
1170 case TCP_CONNTRACK_TIME_WAIT:
1171 case TCP_CONNTRACK_CLOSE:
1172 case TCP_CONNTRACK_CLOSE_WAIT:
1173 return true;
1174 default:
1175 break;
1176 }
1177
1178 return false;
1179}
1180
c0cd1156 1181#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
c1d10adb
PNA
1182
1183#include <linux/netfilter/nfnetlink.h>
1184#include <linux/netfilter/nfnetlink_conntrack.h>
1185
fdf70832 1186static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
440f0d58 1187 struct nf_conn *ct)
c1d10adb 1188{
df6fb868 1189 struct nlattr *nest_parms;
c8e2078c 1190 struct nf_ct_tcp_flags tmp = {};
601e68e1 1191
440f0d58 1192 spin_lock_bh(&ct->lock);
df6fb868
PM
1193 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1194 if (!nest_parms)
1195 goto nla_put_failure;
1196
4925a459
DM
1197 if (nla_put_u8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state) ||
1198 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1199 ct->proto.tcp.seen[0].td_scale) ||
1200 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1201 ct->proto.tcp.seen[1].td_scale))
1202 goto nla_put_failure;
c8e2078c
PNA
1203
1204 tmp.flags = ct->proto.tcp.seen[0].flags;
4925a459
DM
1205 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1206 sizeof(struct nf_ct_tcp_flags), &tmp))
1207 goto nla_put_failure;
c8e2078c
PNA
1208
1209 tmp.flags = ct->proto.tcp.seen[1].flags;
4925a459
DM
1210 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1211 sizeof(struct nf_ct_tcp_flags), &tmp))
1212 goto nla_put_failure;
440f0d58 1213 spin_unlock_bh(&ct->lock);
c1d10adb 1214
df6fb868 1215 nla_nest_end(skb, nest_parms);
c1d10adb
PNA
1216
1217 return 0;
1218
df6fb868 1219nla_put_failure:
440f0d58 1220 spin_unlock_bh(&ct->lock);
c1d10adb
PNA
1221 return -1;
1222}
1223
f73e924c
PM
1224static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1225 [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1226 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1227 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1228 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
1229 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
c1d10adb
PNA
1230};
1231
fdf70832 1232static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
c1d10adb 1233{
2f0d2f10 1234 struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
df6fb868 1235 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
f73e924c 1236 int err;
c1d10adb
PNA
1237
1238 /* updates could not contain anything about the private
1239 * protocol info, in that case skip the parsing */
2f0d2f10 1240 if (!pattr)
c1d10adb
PNA
1241 return 0;
1242
fceb6435
JB
1243 err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr,
1244 tcp_nla_policy, NULL);
f73e924c
PM
1245 if (err < 0)
1246 return err;
c1d10adb 1247
5f7da4d2
PM
1248 if (tb[CTA_PROTOINFO_TCP_STATE] &&
1249 nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
c1d10adb
PNA
1250 return -EINVAL;
1251
440f0d58 1252 spin_lock_bh(&ct->lock);
5f7da4d2
PM
1253 if (tb[CTA_PROTOINFO_TCP_STATE])
1254 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
c8e2078c 1255
df6fb868 1256 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
c8e2078c 1257 struct nf_ct_tcp_flags *attr =
df6fb868 1258 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
c8e2078c
PNA
1259 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1260 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1261 }
1262
df6fb868 1263 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
c8e2078c 1264 struct nf_ct_tcp_flags *attr =
df6fb868 1265 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
c8e2078c
PNA
1266 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1267 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1268 }
1269
df6fb868
PM
1270 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1271 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
c8e2078c
PNA
1272 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1273 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
77236b6e
PM
1274 ct->proto.tcp.seen[0].td_scale =
1275 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]);
1276 ct->proto.tcp.seen[1].td_scale =
1277 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]);
c8e2078c 1278 }
440f0d58 1279 spin_unlock_bh(&ct->lock);
c1d10adb
PNA
1280
1281 return 0;
1282}
a400c30e
HE
1283
1284static int tcp_nlattr_size(void)
1285{
1286 return nla_total_size(0) /* CTA_PROTOINFO_TCP */
1287 + nla_policy_len(tcp_nla_policy, CTA_PROTOINFO_TCP_MAX + 1);
1288}
1289
1290static int tcp_nlattr_tuple_size(void)
1291{
1292 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1293}
c1d10adb 1294#endif
933a41e7 1295
50978462
PNA
1296#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1297
1298#include <linux/netfilter/nfnetlink.h>
1299#include <linux/netfilter/nfnetlink_cttimeout.h>
1300
8264deb8
G
1301static int tcp_timeout_nlattr_to_obj(struct nlattr *tb[],
1302 struct net *net, void *data)
50978462
PNA
1303{
1304 unsigned int *timeouts = data;
8264deb8 1305 struct nf_tcp_net *tn = tcp_pernet(net);
50978462
PNA
1306 int i;
1307
1308 /* set default TCP timeouts. */
1309 for (i=0; i<TCP_CONNTRACK_TIMEOUT_MAX; i++)
8264deb8 1310 timeouts[i] = tn->timeouts[i];
50978462
PNA
1311
1312 if (tb[CTA_TIMEOUT_TCP_SYN_SENT]) {
1313 timeouts[TCP_CONNTRACK_SYN_SENT] =
1314 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT]))*HZ;
1315 }
1316 if (tb[CTA_TIMEOUT_TCP_SYN_RECV]) {
1317 timeouts[TCP_CONNTRACK_SYN_RECV] =
1318 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_RECV]))*HZ;
1319 }
1320 if (tb[CTA_TIMEOUT_TCP_ESTABLISHED]) {
1321 timeouts[TCP_CONNTRACK_ESTABLISHED] =
1322 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_ESTABLISHED]))*HZ;
1323 }
1324 if (tb[CTA_TIMEOUT_TCP_FIN_WAIT]) {
1325 timeouts[TCP_CONNTRACK_FIN_WAIT] =
1326 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_FIN_WAIT]))*HZ;
1327 }
1328 if (tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]) {
1329 timeouts[TCP_CONNTRACK_CLOSE_WAIT] =
1330 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]))*HZ;
1331 }
1332 if (tb[CTA_TIMEOUT_TCP_LAST_ACK]) {
1333 timeouts[TCP_CONNTRACK_LAST_ACK] =
1334 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_LAST_ACK]))*HZ;
1335 }
1336 if (tb[CTA_TIMEOUT_TCP_TIME_WAIT]) {
1337 timeouts[TCP_CONNTRACK_TIME_WAIT] =
1338 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_TIME_WAIT]))*HZ;
1339 }
1340 if (tb[CTA_TIMEOUT_TCP_CLOSE]) {
1341 timeouts[TCP_CONNTRACK_CLOSE] =
1342 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE]))*HZ;
1343 }
1344 if (tb[CTA_TIMEOUT_TCP_SYN_SENT2]) {
1345 timeouts[TCP_CONNTRACK_SYN_SENT2] =
1346 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT2]))*HZ;
1347 }
1348 if (tb[CTA_TIMEOUT_TCP_RETRANS]) {
1349 timeouts[TCP_CONNTRACK_RETRANS] =
1350 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_RETRANS]))*HZ;
1351 }
1352 if (tb[CTA_TIMEOUT_TCP_UNACK]) {
1353 timeouts[TCP_CONNTRACK_UNACK] =
1354 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_UNACK]))*HZ;
1355 }
1356 return 0;
1357}
1358
1359static int
1360tcp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
1361{
1362 const unsigned int *timeouts = data;
1363
4925a459
DM
1364 if (nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT,
1365 htonl(timeouts[TCP_CONNTRACK_SYN_SENT] / HZ)) ||
1366 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_RECV,
1367 htonl(timeouts[TCP_CONNTRACK_SYN_RECV] / HZ)) ||
1368 nla_put_be32(skb, CTA_TIMEOUT_TCP_ESTABLISHED,
1369 htonl(timeouts[TCP_CONNTRACK_ESTABLISHED] / HZ)) ||
1370 nla_put_be32(skb, CTA_TIMEOUT_TCP_FIN_WAIT,
1371 htonl(timeouts[TCP_CONNTRACK_FIN_WAIT] / HZ)) ||
1372 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE_WAIT,
1373 htonl(timeouts[TCP_CONNTRACK_CLOSE_WAIT] / HZ)) ||
1374 nla_put_be32(skb, CTA_TIMEOUT_TCP_LAST_ACK,
1375 htonl(timeouts[TCP_CONNTRACK_LAST_ACK] / HZ)) ||
1376 nla_put_be32(skb, CTA_TIMEOUT_TCP_TIME_WAIT,
1377 htonl(timeouts[TCP_CONNTRACK_TIME_WAIT] / HZ)) ||
1378 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE,
1379 htonl(timeouts[TCP_CONNTRACK_CLOSE] / HZ)) ||
1380 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT2,
1381 htonl(timeouts[TCP_CONNTRACK_SYN_SENT2] / HZ)) ||
1382 nla_put_be32(skb, CTA_TIMEOUT_TCP_RETRANS,
1383 htonl(timeouts[TCP_CONNTRACK_RETRANS] / HZ)) ||
1384 nla_put_be32(skb, CTA_TIMEOUT_TCP_UNACK,
1385 htonl(timeouts[TCP_CONNTRACK_UNACK] / HZ)))
1386 goto nla_put_failure;
50978462
PNA
1387 return 0;
1388
1389nla_put_failure:
1390 return -ENOSPC;
1391}
1392
1393static const struct nla_policy tcp_timeout_nla_policy[CTA_TIMEOUT_TCP_MAX+1] = {
1394 [CTA_TIMEOUT_TCP_SYN_SENT] = { .type = NLA_U32 },
1395 [CTA_TIMEOUT_TCP_SYN_RECV] = { .type = NLA_U32 },
1396 [CTA_TIMEOUT_TCP_ESTABLISHED] = { .type = NLA_U32 },
1397 [CTA_TIMEOUT_TCP_FIN_WAIT] = { .type = NLA_U32 },
1398 [CTA_TIMEOUT_TCP_CLOSE_WAIT] = { .type = NLA_U32 },
1399 [CTA_TIMEOUT_TCP_LAST_ACK] = { .type = NLA_U32 },
1400 [CTA_TIMEOUT_TCP_TIME_WAIT] = { .type = NLA_U32 },
1401 [CTA_TIMEOUT_TCP_CLOSE] = { .type = NLA_U32 },
1402 [CTA_TIMEOUT_TCP_SYN_SENT2] = { .type = NLA_U32 },
6d1fafca
FW
1403 [CTA_TIMEOUT_TCP_RETRANS] = { .type = NLA_U32 },
1404 [CTA_TIMEOUT_TCP_UNACK] = { .type = NLA_U32 },
50978462
PNA
1405};
1406#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1407
933a41e7 1408#ifdef CONFIG_SYSCTL
933a41e7
PM
1409static struct ctl_table tcp_sysctl_table[] = {
1410 {
933a41e7 1411 .procname = "nf_conntrack_tcp_timeout_syn_sent",
933a41e7
PM
1412 .maxlen = sizeof(unsigned int),
1413 .mode = 0644,
6d9f239a 1414 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1415 },
1416 {
933a41e7 1417 .procname = "nf_conntrack_tcp_timeout_syn_recv",
933a41e7
PM
1418 .maxlen = sizeof(unsigned int),
1419 .mode = 0644,
6d9f239a 1420 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1421 },
1422 {
933a41e7 1423 .procname = "nf_conntrack_tcp_timeout_established",
933a41e7
PM
1424 .maxlen = sizeof(unsigned int),
1425 .mode = 0644,
6d9f239a 1426 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1427 },
1428 {
933a41e7 1429 .procname = "nf_conntrack_tcp_timeout_fin_wait",
933a41e7
PM
1430 .maxlen = sizeof(unsigned int),
1431 .mode = 0644,
6d9f239a 1432 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1433 },
1434 {
933a41e7 1435 .procname = "nf_conntrack_tcp_timeout_close_wait",
933a41e7
PM
1436 .maxlen = sizeof(unsigned int),
1437 .mode = 0644,
6d9f239a 1438 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1439 },
1440 {
933a41e7 1441 .procname = "nf_conntrack_tcp_timeout_last_ack",
933a41e7
PM
1442 .maxlen = sizeof(unsigned int),
1443 .mode = 0644,
6d9f239a 1444 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1445 },
1446 {
933a41e7 1447 .procname = "nf_conntrack_tcp_timeout_time_wait",
933a41e7
PM
1448 .maxlen = sizeof(unsigned int),
1449 .mode = 0644,
6d9f239a 1450 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1451 },
1452 {
933a41e7 1453 .procname = "nf_conntrack_tcp_timeout_close",
933a41e7
PM
1454 .maxlen = sizeof(unsigned int),
1455 .mode = 0644,
6d9f239a 1456 .proc_handler = proc_dointvec_jiffies,
933a41e7
PM
1457 },
1458 {
933a41e7 1459 .procname = "nf_conntrack_tcp_timeout_max_retrans",
933a41e7
PM
1460 .maxlen = sizeof(unsigned int),
1461 .mode = 0644,
6d9f239a 1462 .proc_handler = proc_dointvec_jiffies,
933a41e7 1463 },
ae375044
PM
1464 {
1465 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
ae375044
PM
1466 .maxlen = sizeof(unsigned int),
1467 .mode = 0644,
6d9f239a 1468 .proc_handler = proc_dointvec_jiffies,
ae375044 1469 },
933a41e7 1470 {
933a41e7 1471 .procname = "nf_conntrack_tcp_loose",
933a41e7
PM
1472 .maxlen = sizeof(unsigned int),
1473 .mode = 0644,
6d9f239a 1474 .proc_handler = proc_dointvec,
933a41e7
PM
1475 },
1476 {
933a41e7 1477 .procname = "nf_conntrack_tcp_be_liberal",
933a41e7
PM
1478 .maxlen = sizeof(unsigned int),
1479 .mode = 0644,
6d9f239a 1480 .proc_handler = proc_dointvec,
933a41e7
PM
1481 },
1482 {
933a41e7 1483 .procname = "nf_conntrack_tcp_max_retrans",
933a41e7
PM
1484 .maxlen = sizeof(unsigned int),
1485 .mode = 0644,
6d9f239a 1486 .proc_handler = proc_dointvec,
933a41e7 1487 },
f8572d8f 1488 { }
933a41e7
PM
1489};
1490#endif /* CONFIG_SYSCTL */
1491
efa758fe
G
1492static int tcp_kmemdup_sysctl_table(struct nf_proto_net *pn,
1493 struct nf_tcp_net *tn)
d2ba1fde
G
1494{
1495#ifdef CONFIG_SYSCTL
d2ba1fde
G
1496 if (pn->ctl_table)
1497 return 0;
1498
1499 pn->ctl_table = kmemdup(tcp_sysctl_table,
1500 sizeof(tcp_sysctl_table),
1501 GFP_KERNEL);
1502 if (!pn->ctl_table)
1503 return -ENOMEM;
1504
1505 pn->ctl_table[0].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT];
1506 pn->ctl_table[1].data = &tn->timeouts[TCP_CONNTRACK_SYN_RECV];
1507 pn->ctl_table[2].data = &tn->timeouts[TCP_CONNTRACK_ESTABLISHED];
1508 pn->ctl_table[3].data = &tn->timeouts[TCP_CONNTRACK_FIN_WAIT];
1509 pn->ctl_table[4].data = &tn->timeouts[TCP_CONNTRACK_CLOSE_WAIT];
1510 pn->ctl_table[5].data = &tn->timeouts[TCP_CONNTRACK_LAST_ACK];
1511 pn->ctl_table[6].data = &tn->timeouts[TCP_CONNTRACK_TIME_WAIT];
1512 pn->ctl_table[7].data = &tn->timeouts[TCP_CONNTRACK_CLOSE];
1513 pn->ctl_table[8].data = &tn->timeouts[TCP_CONNTRACK_RETRANS];
1514 pn->ctl_table[9].data = &tn->timeouts[TCP_CONNTRACK_UNACK];
1515 pn->ctl_table[10].data = &tn->tcp_loose;
1516 pn->ctl_table[11].data = &tn->tcp_be_liberal;
1517 pn->ctl_table[12].data = &tn->tcp_max_retrans;
1518#endif
1519 return 0;
1520}
1521
efa758fe 1522static int tcp_init_net(struct net *net, u_int16_t proto)
d2ba1fde 1523{
d2ba1fde 1524 struct nf_tcp_net *tn = tcp_pernet(net);
efa758fe
G
1525 struct nf_proto_net *pn = &tn->pn;
1526
1527 if (!pn->users) {
1528 int i;
d2ba1fde 1529
d2ba1fde
G
1530 for (i = 0; i < TCP_CONNTRACK_TIMEOUT_MAX; i++)
1531 tn->timeouts[i] = tcp_timeouts[i];
1532
1533 tn->tcp_loose = nf_ct_tcp_loose;
1534 tn->tcp_be_liberal = nf_ct_tcp_be_liberal;
1535 tn->tcp_max_retrans = nf_ct_tcp_max_retrans;
1536 }
1537
adf05168 1538 return tcp_kmemdup_sysctl_table(pn, tn);
d2ba1fde
G
1539}
1540
08911475
PNA
1541static struct nf_proto_net *tcp_get_net_proto(struct net *net)
1542{
1543 return &net->ct.nf_ct_proto.tcp.pn;
1544}
1545
61075af5 1546struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
9fb9cbb1
YK
1547{
1548 .l3proto = PF_INET,
605dcad6 1549 .l4proto = IPPROTO_TCP,
9fb9cbb1
YK
1550 .pkt_to_tuple = tcp_pkt_to_tuple,
1551 .invert_tuple = tcp_invert_tuple,
9fb9cbb1
YK
1552 .print_conntrack = tcp_print_conntrack,
1553 .packet = tcp_packet,
2c8503f5 1554 .get_timeouts = tcp_get_timeouts,
9fb9cbb1 1555 .new = tcp_new,
96f6bf82 1556 .error = tcp_error,
c6dd940b 1557 .can_early_drop = tcp_can_early_drop,
c0cd1156 1558#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 1559 .to_nlattr = tcp_to_nlattr,
a400c30e 1560 .nlattr_size = tcp_nlattr_size,
fdf70832
PM
1561 .from_nlattr = nlattr_to_tcp,
1562 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1563 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
a400c30e 1564 .nlattr_tuple_size = tcp_nlattr_tuple_size,
f73e924c 1565 .nla_policy = nf_ct_port_nla_policy,
c1d10adb 1566#endif
50978462
PNA
1567#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1568 .ctnl_timeout = {
1569 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1570 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1571 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1572 .obj_size = sizeof(unsigned int) *
1573 TCP_CONNTRACK_TIMEOUT_MAX,
1574 .nla_policy = tcp_timeout_nla_policy,
1575 },
1576#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
efa758fe 1577 .init_net = tcp_init_net,
08911475 1578 .get_net_proto = tcp_get_net_proto,
9fb9cbb1 1579};
13b18339 1580EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
9fb9cbb1 1581
61075af5 1582struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
9fb9cbb1
YK
1583{
1584 .l3proto = PF_INET6,
605dcad6 1585 .l4proto = IPPROTO_TCP,
9fb9cbb1
YK
1586 .pkt_to_tuple = tcp_pkt_to_tuple,
1587 .invert_tuple = tcp_invert_tuple,
9fb9cbb1
YK
1588 .print_conntrack = tcp_print_conntrack,
1589 .packet = tcp_packet,
2c8503f5 1590 .get_timeouts = tcp_get_timeouts,
9fb9cbb1 1591 .new = tcp_new,
96f6bf82 1592 .error = tcp_error,
c6dd940b 1593 .can_early_drop = tcp_can_early_drop,
c0cd1156 1594#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 1595 .to_nlattr = tcp_to_nlattr,
a400c30e 1596 .nlattr_size = tcp_nlattr_size,
fdf70832
PM
1597 .from_nlattr = nlattr_to_tcp,
1598 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1599 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
a400c30e 1600 .nlattr_tuple_size = tcp_nlattr_tuple_size,
f73e924c 1601 .nla_policy = nf_ct_port_nla_policy,
c1d10adb 1602#endif
50978462
PNA
1603#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1604 .ctnl_timeout = {
1605 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1606 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1607 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1608 .obj_size = sizeof(unsigned int) *
1609 TCP_CONNTRACK_TIMEOUT_MAX,
1610 .nla_policy = tcp_timeout_nla_policy,
1611 },
1612#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
efa758fe 1613 .init_net = tcp_init_net,
08911475 1614 .get_net_proto = tcp_get_net_proto,
9fb9cbb1 1615};
13b18339 1616EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);