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