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