Merge remote-tracking branches 'asoc/topic/mc13783', 'asoc/topic/msm8916', 'asoc...
[linux-2.6-block.git] / net / netfilter / nf_conntrack_proto_tcp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2002-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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.
9  */
10
11 #include <linux/types.h>
12 #include <linux/timer.h>
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>
20 #include <asm/unaligned.h>
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>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_ecache.h>
30 #include <net/netfilter/nf_conntrack_seqadj.h>
31 #include <net/netfilter/nf_conntrack_synproxy.h>
32 #include <net/netfilter/nf_log.h>
33 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
34 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
35
36 /* "Be conservative in what you do,
37     be liberal in what you accept from others."
38     If it's non-zero, we mark only out of window RST segments as INVALID. */
39 static int nf_ct_tcp_be_liberal __read_mostly = 0;
40
41 /* If it is set to zero, we disable picking up already established
42    connections. */
43 static int nf_ct_tcp_loose __read_mostly = 1;
44
45 /* Max number of the retransmitted packets without receiving an (acceptable)
46    ACK from the destination. If this number is reached, a shorter timer
47    will be started. */
48 static int nf_ct_tcp_max_retrans __read_mostly = 3;
49
50   /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
51      closely.  They're more complex. --RR */
52
53 static const char *const tcp_conntrack_names[] = {
54         "NONE",
55         "SYN_SENT",
56         "SYN_RECV",
57         "ESTABLISHED",
58         "FIN_WAIT",
59         "CLOSE_WAIT",
60         "LAST_ACK",
61         "TIME_WAIT",
62         "CLOSE",
63         "SYN_SENT2",
64 };
65
66 #define SECS * HZ
67 #define MINS * 60 SECS
68 #define HOURS * 60 MINS
69 #define DAYS * 24 HOURS
70
71 static unsigned int tcp_timeouts[TCP_CONNTRACK_TIMEOUT_MAX] __read_mostly = {
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,
80         [TCP_CONNTRACK_SYN_SENT2]       = 2 MINS,
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,
86 };
87
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
97 #define sS2 TCP_CONNTRACK_SYN_SENT2
98 #define sIV TCP_CONNTRACK_MAX
99 #define sIG TCP_CONNTRACK_IGNORE
100
101 /* What TCP flags are set from RST/SYN/FIN/ACK. */
102 enum 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 };
110
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.
116  * It is assumed that the destinations can't receive segments
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
126  * SYN_SENT:    SYN-only packet seen
127  * SYN_SENT2:   SYN-only packet seen from reply dir, simultaneous open
128  * SYN_RECV:    SYN-ACK packet seen
129  * ESTABLISHED: ACK packet seen
130  * FIN_WAIT:    FIN packet seen
131  * CLOSE_WAIT:  ACK seen (after FIN)
132  * LAST_ACK:    FIN seen (after FIN)
133  * TIME_WAIT:   last ACK seen
134  * CLOSE:       closed connection (RST)
135  *
136  * Packets marked as IGNORED (sIG):
137  *      if they may be either invalid or valid
138  *      and the receiver may send back a connection
139  *      closing RST or a SYN/ACK.
140  *
141  * Packets marked as INVALID (sIV):
142  *      if we regard them as truly invalid packets
143  */
144 static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
145         {
146 /* ORIGINAL */
147 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
148 /*syn*/    { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sS2 },
149 /*
150  *      sNO -> sSS      Initialize a new connection
151  *      sSS -> sSS      Retransmitted SYN
152  *      sS2 -> sS2      Late retransmitted SYN
153  *      sSR -> sIG
154  *      sES -> sIG      Error: SYNs in window outside the SYN_SENT state
155  *                      are errors. Receiver will reply with RST
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  */
164 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
165 /*synack*/ { sIV, sIV, sSR, sIV, sIV, sIV, sIV, sIV, sIV, sSR },
166 /*
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
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
177  */
178 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
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.
184  *      sS2 -> sIV
185  *      sSR -> sFW      Close started.
186  *      sES -> sFW
187  *      sFW -> sLA      FIN seen in both directions, waiting for
188  *                      the last ACK.
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  */
195 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
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.
200  *      sS2 -> sIV
201  *      sSR -> sES      Established state is reached.
202  *      sES -> sES      :-)
203  *      sFW -> sCW      Normal close request answered by ACK.
204  *      sCW -> sCW
205  *      sLA -> sTW      Last ACK detected (RFC5961 challenged)
206  *      sTW -> sTW      Retransmitted last ACK. Remain in the same state.
207  *      sCL -> sCL
208  */
209 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
210 /*rst*/    { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
211 /*none*/   { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
212         },
213         {
214 /* REPLY */
215 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
216 /*syn*/    { sIV, sS2, sIV, sIV, sIV, sIV, sIV, sSS, sIV, sS2 },
217 /*
218  *      sNO -> sIV      Never reached.
219  *      sSS -> sS2      Simultaneous open
220  *      sS2 -> sS2      Retransmitted simultaneous SYN
221  *      sSR -> sIV      Invalid SYN packets sent by the server
222  *      sES -> sIV
223  *      sFW -> sIV
224  *      sCW -> sIV
225  *      sLA -> sIV
226  *      sTW -> sSS      Reopened connection, but server may have switched role
227  *      sCL -> sIV
228  */
229 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
230 /*synack*/ { sIV, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIG, sSR },
231 /*
232  *      sSS -> sSR      Standard open.
233  *      sS2 -> sSR      Simultaneous open
234  *      sSR -> sIG      Retransmitted SYN/ACK, ignore it.
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  */
242 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
243 /*fin*/    { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
244 /*
245  *      sSS -> sIV      Server might not send FIN in this state.
246  *      sS2 -> sIV
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  */
255 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
256 /*ack*/    { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIG },
257 /*
258  *      sSS -> sIG      Might be a half-open connection.
259  *      sS2 -> sIG
260  *      sSR -> sSR      Might answer late resent SYN.
261  *      sES -> sES      :-)
262  *      sFW -> sCW      Normal close request answered by ACK.
263  *      sCW -> sCW
264  *      sLA -> sTW      Last ACK detected (RFC5961 challenged)
265  *      sTW -> sTW      Retransmitted last ACK.
266  *      sCL -> sCL
267  */
268 /*           sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sS2   */
269 /*rst*/    { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
270 /*none*/   { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
271         }
272 };
273
274 static inline struct nf_tcp_net *tcp_pernet(struct net *net)
275 {
276         return &net->ct.nf_ct_proto.tcp;
277 }
278
279 static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
280                              struct net *net, struct nf_conntrack_tuple *tuple)
281 {
282         const struct tcphdr *hp;
283         struct tcphdr _hdr;
284
285         /* Actually only need first 4 bytes to get ports. */
286         hp = skb_header_pointer(skb, dataoff, 4, &_hdr);
287         if (hp == NULL)
288                 return false;
289
290         tuple->src.u.tcp.port = hp->source;
291         tuple->dst.u.tcp.port = hp->dest;
292
293         return true;
294 }
295
296 static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
297                              const struct nf_conntrack_tuple *orig)
298 {
299         tuple->src.u.tcp.port = orig->dst.u.tcp.port;
300         tuple->dst.u.tcp.port = orig->src.u.tcp.port;
301         return true;
302 }
303
304 #ifdef CONFIG_NF_CONNTRACK_PROCFS
305 /* Print out the private part of the conntrack. */
306 static void tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
307 {
308         seq_printf(s, "%s ", tcp_conntrack_names[ct->proto.tcp.state]);
309 }
310 #endif
311
312 static 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.
323
324    http://www.sane.nl/events/sane2000/papers.html
325    http://www.darkart.com/mirrors/www.obfuscation.org/ipf/
326
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
332         td_maxend = max(sack + max(win,1)) seen in reply packets
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
337
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
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
342
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.
345
346    The upper bound limit for a valid (s)ack is not ignored -
347    we doesn't have to deal with fragments.
348 */
349
350 static inline __u32 segment_seq_plus_len(__u32 seq,
351                                          size_t len,
352                                          unsigned int dataoff,
353                                          const struct tcphdr *tcph)
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 }
360
361 /* Fixme: what about big packets? */
362 #define MAXACKWINCONST                  66000
363 #define MAXACKWINDOW(sender)                                            \
364         ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin     \
365                                               : MAXACKWINCONST)
366
367 /*
368  * Simplified tcp_parse_options routine from tcp_input.c
369  */
370 static void tcp_options(const struct sk_buff *skb,
371                         unsigned int dataoff,
372                         const struct tcphdr *tcph,
373                         struct ip_ct_tcp_state *state)
374 {
375         unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
376         const unsigned char *ptr;
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
386         state->td_scale =
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:
400                         if (length < 2)
401                                 return;
402                         opsize=*ptr++;
403                         if (opsize < 2) /* "silly options" */
404                                 return;
405                         if (opsize > length)
406                                 return; /* don't parse partial options */
407
408                         if (opcode == TCPOPT_SACK_PERM
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
415                                 if (state->td_scale > TCP_MAX_WSCALE)
416                                         state->td_scale = TCP_MAX_WSCALE;
417
418                                 state->flags |=
419                                         IP_CT_TCP_FLAG_WINDOW_SCALE;
420                         }
421                         ptr += opsize - 2;
422                         length -= opsize;
423                 }
424         }
425 }
426
427 static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
428                      const struct tcphdr *tcph, __u32 *sack)
429 {
430         unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
431         const unsigned char *ptr;
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 */
443         if (length == TCPOLEN_TSTAMP_ALIGNED
444             && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
445                                        | (TCPOPT_NOP << 16)
446                                        | (TCPOPT_TIMESTAMP << 8)
447                                        | TCPOLEN_TIMESTAMP))
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:
461                         if (length < 2)
462                                 return;
463                         opsize = *ptr++;
464                         if (opsize < 2) /* "silly options" */
465                                 return;
466                         if (opsize > length)
467                                 return; /* don't parse partial options */
468
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) {
477                                         tmp = get_unaligned_be32((__be32 *)(ptr+i)+1);
478
479                                         if (after(tmp, *sack))
480                                                 *sack = tmp;
481                                 }
482                                 return;
483                         }
484                         ptr += opsize - 2;
485                         length -= opsize;
486                 }
487         }
488 }
489
490 static 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,
496                           const struct tcphdr *tcph)
497 {
498         struct net *net = nf_ct_net(ct);
499         struct nf_tcp_net *tn = tcp_pernet(net);
500         struct ip_ct_tcp_state *sender = &state->seen[dir];
501         struct ip_ct_tcp_state *receiver = &state->seen[!dir];
502         const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
503         __u32 seq, ack, sack, end, win, swin;
504         s32 receiver_offset;
505         bool res, in_recv_win;
506
507         /*
508          * Get the required data from the packet.
509          */
510         seq = ntohl(tcph->seq);
511         ack = sack = ntohl(tcph->ack_seq);
512         win = ntohs(tcph->window);
513         end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
514
515         if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
516                 tcp_sack(skb, dataoff, tcph, &sack);
517
518         /* Take into account NAT sequence number mangling */
519         receiver_offset = nf_ct_seq_offset(ct, !dir, ack - 1);
520         ack -= receiver_offset;
521         sack -= receiver_offset;
522
523         pr_debug("tcp_in_window: START\n");
524         pr_debug("tcp_in_window: ");
525         nf_ct_dump_tuple(tuple);
526         pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
527                  seq, ack, receiver_offset, sack, receiver_offset, win, end);
528         pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
529                  "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
530                  sender->td_end, sender->td_maxend, sender->td_maxwin,
531                  sender->td_scale,
532                  receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
533                  receiver->td_scale);
534
535         if (sender->td_maxwin == 0) {
536                 /*
537                  * Initialize sender data.
538                  */
539                 if (tcph->syn) {
540                         /*
541                          * SYN-ACK in reply to a SYN
542                          * or SYN from reply direction in simultaneous open.
543                          */
544                         sender->td_end =
545                         sender->td_maxend = end;
546                         sender->td_maxwin = (win == 0 ? 1 : win);
547
548                         tcp_options(skb, dataoff, tcph, sender);
549                         /*
550                          * RFC 1323:
551                          * Both sides must send the Window Scale option
552                          * to enable window scaling in either direction.
553                          */
554                         if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
555                               && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
556                                 sender->td_scale =
557                                 receiver->td_scale = 0;
558                         if (!tcph->ack)
559                                 /* Simultaneous open */
560                                 return true;
561                 } else {
562                         /*
563                          * We are in the middle of a connection,
564                          * its history is lost for us.
565                          * Let's try to use the data from the packet.
566                          */
567                         sender->td_end = end;
568                         swin = win << sender->td_scale;
569                         sender->td_maxwin = (swin == 0 ? 1 : swin);
570                         sender->td_maxend = end + sender->td_maxwin;
571                         /*
572                          * We haven't seen traffic in the other direction yet
573                          * but we have to tweak window tracking to pass III
574                          * and IV until that happens.
575                          */
576                         if (receiver->td_maxwin == 0)
577                                 receiver->td_end = receiver->td_maxend = sack;
578                 }
579         } else if (((state->state == TCP_CONNTRACK_SYN_SENT
580                      && dir == IP_CT_DIR_ORIGINAL)
581                    || (state->state == TCP_CONNTRACK_SYN_RECV
582                      && dir == IP_CT_DIR_REPLY))
583                    && after(end, sender->td_end)) {
584                 /*
585                  * RFC 793: "if a TCP is reinitialized ... then it need
586                  * not wait at all; it must only be sure to use sequence
587                  * numbers larger than those recently used."
588                  */
589                 sender->td_end =
590                 sender->td_maxend = end;
591                 sender->td_maxwin = (win == 0 ? 1 : win);
592
593                 tcp_options(skb, dataoff, tcph, sender);
594         }
595
596         if (!(tcph->ack)) {
597                 /*
598                  * If there is no ACK, just pretend it was set and OK.
599                  */
600                 ack = sack = receiver->td_end;
601         } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
602                     (TCP_FLAG_ACK|TCP_FLAG_RST))
603                    && (ack == 0)) {
604                 /*
605                  * Broken TCP stacks, that set ACK in RST packets as well
606                  * with zero ack value.
607                  */
608                 ack = sack = receiver->td_end;
609         }
610
611         if (tcph->rst && seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)
612                 /*
613                  * RST sent answering SYN.
614                  */
615                 seq = end = sender->td_end;
616
617         pr_debug("tcp_in_window: ");
618         nf_ct_dump_tuple(tuple);
619         pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
620                  seq, ack, receiver_offset, sack, receiver_offset, win, end);
621         pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
622                  "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
623                  sender->td_end, sender->td_maxend, sender->td_maxwin,
624                  sender->td_scale,
625                  receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
626                  receiver->td_scale);
627
628         /* Is the ending sequence in the receive window (if available)? */
629         in_recv_win = !receiver->td_maxwin ||
630                       after(end, sender->td_end - receiver->td_maxwin - 1);
631
632         pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
633                  before(seq, sender->td_maxend + 1),
634                  (in_recv_win ? 1 : 0),
635                  before(sack, receiver->td_end + 1),
636                  after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
637
638         if (before(seq, sender->td_maxend + 1) &&
639             in_recv_win &&
640             before(sack, receiver->td_end + 1) &&
641             after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
642                 /*
643                  * Take into account window scaling (RFC 1323).
644                  */
645                 if (!tcph->syn)
646                         win <<= sender->td_scale;
647
648                 /*
649                  * Update sender data.
650                  */
651                 swin = win + (sack - ack);
652                 if (sender->td_maxwin < swin)
653                         sender->td_maxwin = swin;
654                 if (after(end, sender->td_end)) {
655                         sender->td_end = end;
656                         sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
657                 }
658                 if (tcph->ack) {
659                         if (!(sender->flags & IP_CT_TCP_FLAG_MAXACK_SET)) {
660                                 sender->td_maxack = ack;
661                                 sender->flags |= IP_CT_TCP_FLAG_MAXACK_SET;
662                         } else if (after(ack, sender->td_maxack))
663                                 sender->td_maxack = ack;
664                 }
665
666                 /*
667                  * Update receiver data.
668                  */
669                 if (receiver->td_maxwin != 0 && after(end, sender->td_maxend))
670                         receiver->td_maxwin += end - sender->td_maxend;
671                 if (after(sack + win, receiver->td_maxend - 1)) {
672                         receiver->td_maxend = sack + win;
673                         if (win == 0)
674                                 receiver->td_maxend++;
675                 }
676                 if (ack == receiver->td_end)
677                         receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
678
679                 /*
680                  * Check retransmissions.
681                  */
682                 if (index == TCP_ACK_SET) {
683                         if (state->last_dir == dir
684                             && state->last_seq == seq
685                             && state->last_ack == ack
686                             && state->last_end == end
687                             && state->last_win == win)
688                                 state->retrans++;
689                         else {
690                                 state->last_dir = dir;
691                                 state->last_seq = seq;
692                                 state->last_ack = ack;
693                                 state->last_end = end;
694                                 state->last_win = win;
695                                 state->retrans = 0;
696                         }
697                 }
698                 res = true;
699         } else {
700                 res = false;
701                 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
702                     tn->tcp_be_liberal)
703                         res = true;
704                 if (!res) {
705                         nf_ct_l4proto_log_invalid(skb, ct,
706                         "%s",
707                         before(seq, sender->td_maxend + 1) ?
708                         in_recv_win ?
709                         before(sack, receiver->td_end + 1) ?
710                         after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1) ? "BUG"
711                         : "ACK is under the lower bound (possible overly delayed ACK)"
712                         : "ACK is over the upper bound (ACKed data not seen yet)"
713                         : "SEQ is under the lower bound (already ACKed data retransmitted)"
714                         : "SEQ is over the upper bound (over the window of the receiver)");
715                 }
716         }
717
718         pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
719                  "receiver end=%u maxend=%u maxwin=%u\n",
720                  res, sender->td_end, sender->td_maxend, sender->td_maxwin,
721                  receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
722
723         return res;
724 }
725
726 /* table of valid flag combinations - PUSH, ECE and CWR are always valid */
727 static const u8 tcp_valid_flags[(TCPHDR_FIN|TCPHDR_SYN|TCPHDR_RST|TCPHDR_ACK|
728                                  TCPHDR_URG) + 1] =
729 {
730         [TCPHDR_SYN]                            = 1,
731         [TCPHDR_SYN|TCPHDR_URG]                 = 1,
732         [TCPHDR_SYN|TCPHDR_ACK]                 = 1,
733         [TCPHDR_RST]                            = 1,
734         [TCPHDR_RST|TCPHDR_ACK]                 = 1,
735         [TCPHDR_FIN|TCPHDR_ACK]                 = 1,
736         [TCPHDR_FIN|TCPHDR_ACK|TCPHDR_URG]      = 1,
737         [TCPHDR_ACK]                            = 1,
738         [TCPHDR_ACK|TCPHDR_URG]                 = 1,
739 };
740
741 static void tcp_error_log(const struct sk_buff *skb, struct net *net,
742                           u8 pf, const char *msg)
743 {
744         nf_l4proto_log_invalid(skb, net, pf, IPPROTO_TCP, "%s", msg);
745 }
746
747 /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c.  */
748 static int tcp_error(struct net *net, struct nf_conn *tmpl,
749                      struct sk_buff *skb,
750                      unsigned int dataoff,
751                      u_int8_t pf,
752                      unsigned int hooknum)
753 {
754         const struct tcphdr *th;
755         struct tcphdr _tcph;
756         unsigned int tcplen = skb->len - dataoff;
757         u_int8_t tcpflags;
758
759         /* Smaller that minimal TCP header? */
760         th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
761         if (th == NULL) {
762                 tcp_error_log(skb, net, pf, "short packet");
763                 return -NF_ACCEPT;
764         }
765
766         /* Not whole TCP header or malformed packet */
767         if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
768                 tcp_error_log(skb, net, pf, "truncated packet");
769                 return -NF_ACCEPT;
770         }
771
772         /* Checksum invalid? Ignore.
773          * We skip checking packets on the outgoing path
774          * because the checksum is assumed to be correct.
775          */
776         /* FIXME: Source route IP option packets --RR */
777         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
778             nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
779                 tcp_error_log(skb, net, pf, "bad checksum");
780                 return -NF_ACCEPT;
781         }
782
783         /* Check TCP flags. */
784         tcpflags = (tcp_flag_byte(th) & ~(TCPHDR_ECE|TCPHDR_CWR|TCPHDR_PSH));
785         if (!tcp_valid_flags[tcpflags]) {
786                 tcp_error_log(skb, net, pf, "invalid tcp flag combination");
787                 return -NF_ACCEPT;
788         }
789
790         return NF_ACCEPT;
791 }
792
793 static unsigned int *tcp_get_timeouts(struct net *net)
794 {
795         return tcp_pernet(net)->timeouts;
796 }
797
798 /* Returns verdict for packet, or -1 for invalid. */
799 static int tcp_packet(struct nf_conn *ct,
800                       const struct sk_buff *skb,
801                       unsigned int dataoff,
802                       enum ip_conntrack_info ctinfo,
803                       unsigned int *timeouts)
804 {
805         struct net *net = nf_ct_net(ct);
806         struct nf_tcp_net *tn = tcp_pernet(net);
807         struct nf_conntrack_tuple *tuple;
808         enum tcp_conntrack new_state, old_state;
809         enum ip_conntrack_dir dir;
810         const struct tcphdr *th;
811         struct tcphdr _tcph;
812         unsigned long timeout;
813         unsigned int index;
814
815         th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
816         BUG_ON(th == NULL);
817
818         spin_lock_bh(&ct->lock);
819         old_state = ct->proto.tcp.state;
820         dir = CTINFO2DIR(ctinfo);
821         index = get_conntrack_index(th);
822         new_state = tcp_conntracks[dir][index][old_state];
823         tuple = &ct->tuplehash[dir].tuple;
824
825         switch (new_state) {
826         case TCP_CONNTRACK_SYN_SENT:
827                 if (old_state < TCP_CONNTRACK_TIME_WAIT)
828                         break;
829                 /* RFC 1122: "When a connection is closed actively,
830                  * it MUST linger in TIME-WAIT state for a time 2xMSL
831                  * (Maximum Segment Lifetime). However, it MAY accept
832                  * a new SYN from the remote TCP to reopen the connection
833                  * directly from TIME-WAIT state, if..."
834                  * We ignore the conditions because we are in the
835                  * TIME-WAIT state anyway.
836                  *
837                  * Handle aborted connections: we and the server
838                  * think there is an existing connection but the client
839                  * aborts it and starts a new one.
840                  */
841                 if (((ct->proto.tcp.seen[dir].flags
842                       | ct->proto.tcp.seen[!dir].flags)
843                      & IP_CT_TCP_FLAG_CLOSE_INIT)
844                     || (ct->proto.tcp.last_dir == dir
845                         && ct->proto.tcp.last_index == TCP_RST_SET)) {
846                         /* Attempt to reopen a closed/aborted connection.
847                          * Delete this connection and look up again. */
848                         spin_unlock_bh(&ct->lock);
849
850                         /* Only repeat if we can actually remove the timer.
851                          * Destruction may already be in progress in process
852                          * context and we must give it a chance to terminate.
853                          */
854                         if (nf_ct_kill(ct))
855                                 return -NF_REPEAT;
856                         return NF_DROP;
857                 }
858                 /* Fall through */
859         case TCP_CONNTRACK_IGNORE:
860                 /* Ignored packets:
861                  *
862                  * Our connection entry may be out of sync, so ignore
863                  * packets which may signal the real connection between
864                  * the client and the server.
865                  *
866                  * a) SYN in ORIGINAL
867                  * b) SYN/ACK in REPLY
868                  * c) ACK in reply direction after initial SYN in original.
869                  *
870                  * If the ignored packet is invalid, the receiver will send
871                  * a RST we'll catch below.
872                  */
873                 if (index == TCP_SYNACK_SET
874                     && ct->proto.tcp.last_index == TCP_SYN_SET
875                     && ct->proto.tcp.last_dir != dir
876                     && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
877                         /* b) This SYN/ACK acknowledges a SYN that we earlier
878                          * ignored as invalid. This means that the client and
879                          * the server are both in sync, while the firewall is
880                          * not. We get in sync from the previously annotated
881                          * values.
882                          */
883                         old_state = TCP_CONNTRACK_SYN_SENT;
884                         new_state = TCP_CONNTRACK_SYN_RECV;
885                         ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_end =
886                                 ct->proto.tcp.last_end;
887                         ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxend =
888                                 ct->proto.tcp.last_end;
889                         ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxwin =
890                                 ct->proto.tcp.last_win == 0 ?
891                                         1 : ct->proto.tcp.last_win;
892                         ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_scale =
893                                 ct->proto.tcp.last_wscale;
894                         ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
895                         ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
896                                 ct->proto.tcp.last_flags;
897                         memset(&ct->proto.tcp.seen[dir], 0,
898                                sizeof(struct ip_ct_tcp_state));
899                         break;
900                 }
901                 ct->proto.tcp.last_index = index;
902                 ct->proto.tcp.last_dir = dir;
903                 ct->proto.tcp.last_seq = ntohl(th->seq);
904                 ct->proto.tcp.last_end =
905                     segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
906                 ct->proto.tcp.last_win = ntohs(th->window);
907
908                 /* a) This is a SYN in ORIGINAL. The client and the server
909                  * may be in sync but we are not. In that case, we annotate
910                  * the TCP options and let the packet go through. If it is a
911                  * valid SYN packet, the server will reply with a SYN/ACK, and
912                  * then we'll get in sync. Otherwise, the server potentially
913                  * responds with a challenge ACK if implementing RFC5961.
914                  */
915                 if (index == TCP_SYN_SET && dir == IP_CT_DIR_ORIGINAL) {
916                         struct ip_ct_tcp_state seen = {};
917
918                         ct->proto.tcp.last_flags =
919                         ct->proto.tcp.last_wscale = 0;
920                         tcp_options(skb, dataoff, th, &seen);
921                         if (seen.flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
922                                 ct->proto.tcp.last_flags |=
923                                         IP_CT_TCP_FLAG_WINDOW_SCALE;
924                                 ct->proto.tcp.last_wscale = seen.td_scale;
925                         }
926                         if (seen.flags & IP_CT_TCP_FLAG_SACK_PERM) {
927                                 ct->proto.tcp.last_flags |=
928                                         IP_CT_TCP_FLAG_SACK_PERM;
929                         }
930                         /* Mark the potential for RFC5961 challenge ACK,
931                          * this pose a special problem for LAST_ACK state
932                          * as ACK is intrepretated as ACKing last FIN.
933                          */
934                         if (old_state == TCP_CONNTRACK_LAST_ACK)
935                                 ct->proto.tcp.last_flags |=
936                                         IP_CT_EXP_CHALLENGE_ACK;
937                 }
938                 spin_unlock_bh(&ct->lock);
939                 nf_ct_l4proto_log_invalid(skb, ct, "invalid packet ignored in "
940                                           "state %s ", tcp_conntrack_names[old_state]);
941                 return NF_ACCEPT;
942         case TCP_CONNTRACK_MAX:
943                 /* Special case for SYN proxy: when the SYN to the server or
944                  * the SYN/ACK from the server is lost, the client may transmit
945                  * a keep-alive packet while in SYN_SENT state. This needs to
946                  * be associated with the original conntrack entry in order to
947                  * generate a new SYN with the correct sequence number.
948                  */
949                 if (nfct_synproxy(ct) && old_state == TCP_CONNTRACK_SYN_SENT &&
950                     index == TCP_ACK_SET && dir == IP_CT_DIR_ORIGINAL &&
951                     ct->proto.tcp.last_dir == IP_CT_DIR_ORIGINAL &&
952                     ct->proto.tcp.seen[dir].td_end - 1 == ntohl(th->seq)) {
953                         pr_debug("nf_ct_tcp: SYN proxy client keep alive\n");
954                         spin_unlock_bh(&ct->lock);
955                         return NF_ACCEPT;
956                 }
957
958                 /* Invalid packet */
959                 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
960                          dir, get_conntrack_index(th), old_state);
961                 spin_unlock_bh(&ct->lock);
962                 nf_ct_l4proto_log_invalid(skb, ct, "invalid state");
963                 return -NF_ACCEPT;
964         case TCP_CONNTRACK_TIME_WAIT:
965                 /* RFC5961 compliance cause stack to send "challenge-ACK"
966                  * e.g. in response to spurious SYNs.  Conntrack MUST
967                  * not believe this ACK is acking last FIN.
968                  */
969                 if (old_state == TCP_CONNTRACK_LAST_ACK &&
970                     index == TCP_ACK_SET &&
971                     ct->proto.tcp.last_dir != dir &&
972                     ct->proto.tcp.last_index == TCP_SYN_SET &&
973                     (ct->proto.tcp.last_flags & IP_CT_EXP_CHALLENGE_ACK)) {
974                         /* Detected RFC5961 challenge ACK */
975                         ct->proto.tcp.last_flags &= ~IP_CT_EXP_CHALLENGE_ACK;
976                         spin_unlock_bh(&ct->lock);
977                         nf_ct_l4proto_log_invalid(skb, ct, "challenge-ack ignored");
978                         return NF_ACCEPT; /* Don't change state */
979                 }
980                 break;
981         case TCP_CONNTRACK_CLOSE:
982                 if (index == TCP_RST_SET
983                     && (ct->proto.tcp.seen[!dir].flags & IP_CT_TCP_FLAG_MAXACK_SET)
984                     && before(ntohl(th->seq), ct->proto.tcp.seen[!dir].td_maxack)) {
985                         /* Invalid RST  */
986                         spin_unlock_bh(&ct->lock);
987                         nf_ct_l4proto_log_invalid(skb, ct, "invalid rst");
988                         return -NF_ACCEPT;
989                 }
990                 if (index == TCP_RST_SET
991                     && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
992                          && ct->proto.tcp.last_index == TCP_SYN_SET)
993                         || (!test_bit(IPS_ASSURED_BIT, &ct->status)
994                             && ct->proto.tcp.last_index == TCP_ACK_SET))
995                     && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
996                         /* RST sent to invalid SYN or ACK we had let through
997                          * at a) and c) above:
998                          *
999                          * a) SYN was in window then
1000                          * c) we hold a half-open connection.
1001                          *
1002                          * Delete our connection entry.
1003                          * We skip window checking, because packet might ACK
1004                          * segments we ignored. */
1005                         goto in_window;
1006                 }
1007                 /* Just fall through */
1008         default:
1009                 /* Keep compilers happy. */
1010                 break;
1011         }
1012
1013         if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
1014                            skb, dataoff, th)) {
1015                 spin_unlock_bh(&ct->lock);
1016                 return -NF_ACCEPT;
1017         }
1018      in_window:
1019         /* From now on we have got in-window packets */
1020         ct->proto.tcp.last_index = index;
1021         ct->proto.tcp.last_dir = dir;
1022
1023         pr_debug("tcp_conntracks: ");
1024         nf_ct_dump_tuple(tuple);
1025         pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1026                  (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1027                  (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1028                  old_state, new_state);
1029
1030         ct->proto.tcp.state = new_state;
1031         if (old_state != new_state
1032             && new_state == TCP_CONNTRACK_FIN_WAIT)
1033                 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
1034
1035         if (ct->proto.tcp.retrans >= tn->tcp_max_retrans &&
1036             timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS])
1037                 timeout = timeouts[TCP_CONNTRACK_RETRANS];
1038         else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) &
1039                  IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED &&
1040                  timeouts[new_state] > timeouts[TCP_CONNTRACK_UNACK])
1041                 timeout = timeouts[TCP_CONNTRACK_UNACK];
1042         else if (ct->proto.tcp.last_win == 0 &&
1043                  timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS])
1044                 timeout = timeouts[TCP_CONNTRACK_RETRANS];
1045         else
1046                 timeout = timeouts[new_state];
1047         spin_unlock_bh(&ct->lock);
1048
1049         if (new_state != old_state)
1050                 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
1051
1052         if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1053                 /* If only reply is a RST, we can consider ourselves not to
1054                    have an established connection: this is a fairly common
1055                    problem case, so we can delete the conntrack
1056                    immediately.  --RR */
1057                 if (th->rst) {
1058                         nf_ct_kill_acct(ct, ctinfo, skb);
1059                         return NF_ACCEPT;
1060                 }
1061                 /* ESTABLISHED without SEEN_REPLY, i.e. mid-connection
1062                  * pickup with loose=1. Avoid large ESTABLISHED timeout.
1063                  */
1064                 if (new_state == TCP_CONNTRACK_ESTABLISHED &&
1065                     timeout > timeouts[TCP_CONNTRACK_UNACK])
1066                         timeout = timeouts[TCP_CONNTRACK_UNACK];
1067         } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
1068                    && (old_state == TCP_CONNTRACK_SYN_RECV
1069                        || old_state == TCP_CONNTRACK_ESTABLISHED)
1070                    && new_state == TCP_CONNTRACK_ESTABLISHED) {
1071                 /* Set ASSURED if we see see valid ack in ESTABLISHED
1072                    after SYN_RECV or a valid answer for a picked up
1073                    connection. */
1074                 set_bit(IPS_ASSURED_BIT, &ct->status);
1075                 nf_conntrack_event_cache(IPCT_ASSURED, ct);
1076         }
1077         nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
1078
1079         return NF_ACCEPT;
1080 }
1081
1082 /* Called when a new connection for this protocol found. */
1083 static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
1084                     unsigned int dataoff, unsigned int *timeouts)
1085 {
1086         enum tcp_conntrack new_state;
1087         const struct tcphdr *th;
1088         struct tcphdr _tcph;
1089         struct net *net = nf_ct_net(ct);
1090         struct nf_tcp_net *tn = tcp_pernet(net);
1091         const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
1092         const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
1093
1094         th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1095         BUG_ON(th == NULL);
1096
1097         /* Don't need lock here: this conntrack not in circulation yet */
1098         new_state = tcp_conntracks[0][get_conntrack_index(th)][TCP_CONNTRACK_NONE];
1099
1100         /* Invalid: delete conntrack */
1101         if (new_state >= TCP_CONNTRACK_MAX) {
1102                 pr_debug("nf_ct_tcp: invalid new deleting.\n");
1103                 return false;
1104         }
1105
1106         if (new_state == TCP_CONNTRACK_SYN_SENT) {
1107                 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
1108                 /* SYN packet */
1109                 ct->proto.tcp.seen[0].td_end =
1110                         segment_seq_plus_len(ntohl(th->seq), skb->len,
1111                                              dataoff, th);
1112                 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1113                 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1114                         ct->proto.tcp.seen[0].td_maxwin = 1;
1115                 ct->proto.tcp.seen[0].td_maxend =
1116                         ct->proto.tcp.seen[0].td_end;
1117
1118                 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
1119         } else if (tn->tcp_loose == 0) {
1120                 /* Don't try to pick up connections. */
1121                 return false;
1122         } else {
1123                 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
1124                 /*
1125                  * We are in the middle of a connection,
1126                  * its history is lost for us.
1127                  * Let's try to use the data from the packet.
1128                  */
1129                 ct->proto.tcp.seen[0].td_end =
1130                         segment_seq_plus_len(ntohl(th->seq), skb->len,
1131                                              dataoff, th);
1132                 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1133                 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1134                         ct->proto.tcp.seen[0].td_maxwin = 1;
1135                 ct->proto.tcp.seen[0].td_maxend =
1136                         ct->proto.tcp.seen[0].td_end +
1137                         ct->proto.tcp.seen[0].td_maxwin;
1138
1139                 /* We assume SACK and liberal window checking to handle
1140                  * window scaling */
1141                 ct->proto.tcp.seen[0].flags =
1142                 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1143                                               IP_CT_TCP_FLAG_BE_LIBERAL;
1144         }
1145
1146         /* tcp_packet will set them */
1147         ct->proto.tcp.last_index = TCP_NONE_SET;
1148
1149         pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1150                  "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1151                  sender->td_end, sender->td_maxend, sender->td_maxwin,
1152                  sender->td_scale,
1153                  receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1154                  receiver->td_scale);
1155         return true;
1156 }
1157
1158 static bool tcp_can_early_drop(const struct nf_conn *ct)
1159 {
1160         switch (ct->proto.tcp.state) {
1161         case TCP_CONNTRACK_FIN_WAIT:
1162         case TCP_CONNTRACK_LAST_ACK:
1163         case TCP_CONNTRACK_TIME_WAIT:
1164         case TCP_CONNTRACK_CLOSE:
1165         case TCP_CONNTRACK_CLOSE_WAIT:
1166                 return true;
1167         default:
1168                 break;
1169         }
1170
1171         return false;
1172 }
1173
1174 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1175
1176 #include <linux/netfilter/nfnetlink.h>
1177 #include <linux/netfilter/nfnetlink_conntrack.h>
1178
1179 static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
1180                          struct nf_conn *ct)
1181 {
1182         struct nlattr *nest_parms;
1183         struct nf_ct_tcp_flags tmp = {};
1184
1185         spin_lock_bh(&ct->lock);
1186         nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1187         if (!nest_parms)
1188                 goto nla_put_failure;
1189
1190         if (nla_put_u8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state) ||
1191             nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1192                        ct->proto.tcp.seen[0].td_scale) ||
1193             nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1194                        ct->proto.tcp.seen[1].td_scale))
1195                 goto nla_put_failure;
1196
1197         tmp.flags = ct->proto.tcp.seen[0].flags;
1198         if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1199                     sizeof(struct nf_ct_tcp_flags), &tmp))
1200                 goto nla_put_failure;
1201
1202         tmp.flags = ct->proto.tcp.seen[1].flags;
1203         if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1204                     sizeof(struct nf_ct_tcp_flags), &tmp))
1205                 goto nla_put_failure;
1206         spin_unlock_bh(&ct->lock);
1207
1208         nla_nest_end(skb, nest_parms);
1209
1210         return 0;
1211
1212 nla_put_failure:
1213         spin_unlock_bh(&ct->lock);
1214         return -1;
1215 }
1216
1217 static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1218         [CTA_PROTOINFO_TCP_STATE]           = { .type = NLA_U8 },
1219         [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1220         [CTA_PROTOINFO_TCP_WSCALE_REPLY]    = { .type = NLA_U8 },
1221         [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]  = { .len = sizeof(struct nf_ct_tcp_flags) },
1222         [CTA_PROTOINFO_TCP_FLAGS_REPLY]     = { .len =  sizeof(struct nf_ct_tcp_flags) },
1223 };
1224
1225 static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
1226 {
1227         struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
1228         struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
1229         int err;
1230
1231         /* updates could not contain anything about the private
1232          * protocol info, in that case skip the parsing */
1233         if (!pattr)
1234                 return 0;
1235
1236         err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr,
1237                                tcp_nla_policy, NULL);
1238         if (err < 0)
1239                 return err;
1240
1241         if (tb[CTA_PROTOINFO_TCP_STATE] &&
1242             nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
1243                 return -EINVAL;
1244
1245         spin_lock_bh(&ct->lock);
1246         if (tb[CTA_PROTOINFO_TCP_STATE])
1247                 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
1248
1249         if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
1250                 struct nf_ct_tcp_flags *attr =
1251                         nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
1252                 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1253                 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1254         }
1255
1256         if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
1257                 struct nf_ct_tcp_flags *attr =
1258                         nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
1259                 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1260                 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1261         }
1262
1263         if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1264             tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
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) {
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]);
1271         }
1272         spin_unlock_bh(&ct->lock);
1273
1274         return 0;
1275 }
1276
1277 static 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
1283 static unsigned int tcp_nlattr_tuple_size(void)
1284 {
1285         static unsigned int size __read_mostly;
1286
1287         if (!size)
1288                 size = nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1289
1290         return size;
1291 }
1292 #endif
1293
1294 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1295
1296 #include <linux/netfilter/nfnetlink.h>
1297 #include <linux/netfilter/nfnetlink_cttimeout.h>
1298
1299 static int tcp_timeout_nlattr_to_obj(struct nlattr *tb[],
1300                                      struct net *net, void *data)
1301 {
1302         unsigned int *timeouts = data;
1303         struct nf_tcp_net *tn = tcp_pernet(net);
1304         int i;
1305
1306         /* set default TCP timeouts. */
1307         for (i=0; i<TCP_CONNTRACK_TIMEOUT_MAX; i++)
1308                 timeouts[i] = tn->timeouts[i];
1309
1310         if (tb[CTA_TIMEOUT_TCP_SYN_SENT]) {
1311                 timeouts[TCP_CONNTRACK_SYN_SENT] =
1312                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT]))*HZ;
1313         }
1314         if (tb[CTA_TIMEOUT_TCP_SYN_RECV]) {
1315                 timeouts[TCP_CONNTRACK_SYN_RECV] =
1316                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_RECV]))*HZ;
1317         }
1318         if (tb[CTA_TIMEOUT_TCP_ESTABLISHED]) {
1319                 timeouts[TCP_CONNTRACK_ESTABLISHED] =
1320                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_ESTABLISHED]))*HZ;
1321         }
1322         if (tb[CTA_TIMEOUT_TCP_FIN_WAIT]) {
1323                 timeouts[TCP_CONNTRACK_FIN_WAIT] =
1324                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_FIN_WAIT]))*HZ;
1325         }
1326         if (tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]) {
1327                 timeouts[TCP_CONNTRACK_CLOSE_WAIT] =
1328                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]))*HZ;
1329         }
1330         if (tb[CTA_TIMEOUT_TCP_LAST_ACK]) {
1331                 timeouts[TCP_CONNTRACK_LAST_ACK] =
1332                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_LAST_ACK]))*HZ;
1333         }
1334         if (tb[CTA_TIMEOUT_TCP_TIME_WAIT]) {
1335                 timeouts[TCP_CONNTRACK_TIME_WAIT] =
1336                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_TIME_WAIT]))*HZ;
1337         }
1338         if (tb[CTA_TIMEOUT_TCP_CLOSE]) {
1339                 timeouts[TCP_CONNTRACK_CLOSE] =
1340                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE]))*HZ;
1341         }
1342         if (tb[CTA_TIMEOUT_TCP_SYN_SENT2]) {
1343                 timeouts[TCP_CONNTRACK_SYN_SENT2] =
1344                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT2]))*HZ;
1345         }
1346         if (tb[CTA_TIMEOUT_TCP_RETRANS]) {
1347                 timeouts[TCP_CONNTRACK_RETRANS] =
1348                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_RETRANS]))*HZ;
1349         }
1350         if (tb[CTA_TIMEOUT_TCP_UNACK]) {
1351                 timeouts[TCP_CONNTRACK_UNACK] =
1352                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_UNACK]))*HZ;
1353         }
1354         return 0;
1355 }
1356
1357 static int
1358 tcp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
1359 {
1360         const unsigned int *timeouts = data;
1361
1362         if (nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT,
1363                         htonl(timeouts[TCP_CONNTRACK_SYN_SENT] / HZ)) ||
1364             nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_RECV,
1365                          htonl(timeouts[TCP_CONNTRACK_SYN_RECV] / HZ)) ||
1366             nla_put_be32(skb, CTA_TIMEOUT_TCP_ESTABLISHED,
1367                          htonl(timeouts[TCP_CONNTRACK_ESTABLISHED] / HZ)) ||
1368             nla_put_be32(skb, CTA_TIMEOUT_TCP_FIN_WAIT,
1369                          htonl(timeouts[TCP_CONNTRACK_FIN_WAIT] / HZ)) ||
1370             nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE_WAIT,
1371                          htonl(timeouts[TCP_CONNTRACK_CLOSE_WAIT] / HZ)) ||
1372             nla_put_be32(skb, CTA_TIMEOUT_TCP_LAST_ACK,
1373                          htonl(timeouts[TCP_CONNTRACK_LAST_ACK] / HZ)) ||
1374             nla_put_be32(skb, CTA_TIMEOUT_TCP_TIME_WAIT,
1375                          htonl(timeouts[TCP_CONNTRACK_TIME_WAIT] / HZ)) ||
1376             nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE,
1377                          htonl(timeouts[TCP_CONNTRACK_CLOSE] / HZ)) ||
1378             nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT2,
1379                          htonl(timeouts[TCP_CONNTRACK_SYN_SENT2] / HZ)) ||
1380             nla_put_be32(skb, CTA_TIMEOUT_TCP_RETRANS,
1381                          htonl(timeouts[TCP_CONNTRACK_RETRANS] / HZ)) ||
1382             nla_put_be32(skb, CTA_TIMEOUT_TCP_UNACK,
1383                          htonl(timeouts[TCP_CONNTRACK_UNACK] / HZ)))
1384                 goto nla_put_failure;
1385         return 0;
1386
1387 nla_put_failure:
1388         return -ENOSPC;
1389 }
1390
1391 static const struct nla_policy tcp_timeout_nla_policy[CTA_TIMEOUT_TCP_MAX+1] = {
1392         [CTA_TIMEOUT_TCP_SYN_SENT]      = { .type = NLA_U32 },
1393         [CTA_TIMEOUT_TCP_SYN_RECV]      = { .type = NLA_U32 },
1394         [CTA_TIMEOUT_TCP_ESTABLISHED]   = { .type = NLA_U32 },
1395         [CTA_TIMEOUT_TCP_FIN_WAIT]      = { .type = NLA_U32 },
1396         [CTA_TIMEOUT_TCP_CLOSE_WAIT]    = { .type = NLA_U32 },
1397         [CTA_TIMEOUT_TCP_LAST_ACK]      = { .type = NLA_U32 },
1398         [CTA_TIMEOUT_TCP_TIME_WAIT]     = { .type = NLA_U32 },
1399         [CTA_TIMEOUT_TCP_CLOSE]         = { .type = NLA_U32 },
1400         [CTA_TIMEOUT_TCP_SYN_SENT2]     = { .type = NLA_U32 },
1401         [CTA_TIMEOUT_TCP_RETRANS]       = { .type = NLA_U32 },
1402         [CTA_TIMEOUT_TCP_UNACK]         = { .type = NLA_U32 },
1403 };
1404 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1405
1406 #ifdef CONFIG_SYSCTL
1407 static struct ctl_table tcp_sysctl_table[] = {
1408         {
1409                 .procname       = "nf_conntrack_tcp_timeout_syn_sent",
1410                 .maxlen         = sizeof(unsigned int),
1411                 .mode           = 0644,
1412                 .proc_handler   = proc_dointvec_jiffies,
1413         },
1414         {
1415                 .procname       = "nf_conntrack_tcp_timeout_syn_recv",
1416                 .maxlen         = sizeof(unsigned int),
1417                 .mode           = 0644,
1418                 .proc_handler   = proc_dointvec_jiffies,
1419         },
1420         {
1421                 .procname       = "nf_conntrack_tcp_timeout_established",
1422                 .maxlen         = sizeof(unsigned int),
1423                 .mode           = 0644,
1424                 .proc_handler   = proc_dointvec_jiffies,
1425         },
1426         {
1427                 .procname       = "nf_conntrack_tcp_timeout_fin_wait",
1428                 .maxlen         = sizeof(unsigned int),
1429                 .mode           = 0644,
1430                 .proc_handler   = proc_dointvec_jiffies,
1431         },
1432         {
1433                 .procname       = "nf_conntrack_tcp_timeout_close_wait",
1434                 .maxlen         = sizeof(unsigned int),
1435                 .mode           = 0644,
1436                 .proc_handler   = proc_dointvec_jiffies,
1437         },
1438         {
1439                 .procname       = "nf_conntrack_tcp_timeout_last_ack",
1440                 .maxlen         = sizeof(unsigned int),
1441                 .mode           = 0644,
1442                 .proc_handler   = proc_dointvec_jiffies,
1443         },
1444         {
1445                 .procname       = "nf_conntrack_tcp_timeout_time_wait",
1446                 .maxlen         = sizeof(unsigned int),
1447                 .mode           = 0644,
1448                 .proc_handler   = proc_dointvec_jiffies,
1449         },
1450         {
1451                 .procname       = "nf_conntrack_tcp_timeout_close",
1452                 .maxlen         = sizeof(unsigned int),
1453                 .mode           = 0644,
1454                 .proc_handler   = proc_dointvec_jiffies,
1455         },
1456         {
1457                 .procname       = "nf_conntrack_tcp_timeout_max_retrans",
1458                 .maxlen         = sizeof(unsigned int),
1459                 .mode           = 0644,
1460                 .proc_handler   = proc_dointvec_jiffies,
1461         },
1462         {
1463                 .procname       = "nf_conntrack_tcp_timeout_unacknowledged",
1464                 .maxlen         = sizeof(unsigned int),
1465                 .mode           = 0644,
1466                 .proc_handler   = proc_dointvec_jiffies,
1467         },
1468         {
1469                 .procname       = "nf_conntrack_tcp_loose",
1470                 .maxlen         = sizeof(unsigned int),
1471                 .mode           = 0644,
1472                 .proc_handler   = proc_dointvec,
1473         },
1474         {
1475                 .procname       = "nf_conntrack_tcp_be_liberal",
1476                 .maxlen         = sizeof(unsigned int),
1477                 .mode           = 0644,
1478                 .proc_handler   = proc_dointvec,
1479         },
1480         {
1481                 .procname       = "nf_conntrack_tcp_max_retrans",
1482                 .maxlen         = sizeof(unsigned int),
1483                 .mode           = 0644,
1484                 .proc_handler   = proc_dointvec,
1485         },
1486         { }
1487 };
1488 #endif /* CONFIG_SYSCTL */
1489
1490 static int tcp_kmemdup_sysctl_table(struct nf_proto_net *pn,
1491                                     struct nf_tcp_net *tn)
1492 {
1493 #ifdef CONFIG_SYSCTL
1494         if (pn->ctl_table)
1495                 return 0;
1496
1497         pn->ctl_table = kmemdup(tcp_sysctl_table,
1498                                 sizeof(tcp_sysctl_table),
1499                                 GFP_KERNEL);
1500         if (!pn->ctl_table)
1501                 return -ENOMEM;
1502
1503         pn->ctl_table[0].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT];
1504         pn->ctl_table[1].data = &tn->timeouts[TCP_CONNTRACK_SYN_RECV];
1505         pn->ctl_table[2].data = &tn->timeouts[TCP_CONNTRACK_ESTABLISHED];
1506         pn->ctl_table[3].data = &tn->timeouts[TCP_CONNTRACK_FIN_WAIT];
1507         pn->ctl_table[4].data = &tn->timeouts[TCP_CONNTRACK_CLOSE_WAIT];
1508         pn->ctl_table[5].data = &tn->timeouts[TCP_CONNTRACK_LAST_ACK];
1509         pn->ctl_table[6].data = &tn->timeouts[TCP_CONNTRACK_TIME_WAIT];
1510         pn->ctl_table[7].data = &tn->timeouts[TCP_CONNTRACK_CLOSE];
1511         pn->ctl_table[8].data = &tn->timeouts[TCP_CONNTRACK_RETRANS];
1512         pn->ctl_table[9].data = &tn->timeouts[TCP_CONNTRACK_UNACK];
1513         pn->ctl_table[10].data = &tn->tcp_loose;
1514         pn->ctl_table[11].data = &tn->tcp_be_liberal;
1515         pn->ctl_table[12].data = &tn->tcp_max_retrans;
1516 #endif
1517         return 0;
1518 }
1519
1520 static int tcp_init_net(struct net *net, u_int16_t proto)
1521 {
1522         struct nf_tcp_net *tn = tcp_pernet(net);
1523         struct nf_proto_net *pn = &tn->pn;
1524
1525         if (!pn->users) {
1526                 int i;
1527
1528                 for (i = 0; i < TCP_CONNTRACK_TIMEOUT_MAX; i++)
1529                         tn->timeouts[i] = tcp_timeouts[i];
1530
1531                 tn->tcp_loose = nf_ct_tcp_loose;
1532                 tn->tcp_be_liberal = nf_ct_tcp_be_liberal;
1533                 tn->tcp_max_retrans = nf_ct_tcp_max_retrans;
1534         }
1535
1536         return tcp_kmemdup_sysctl_table(pn, tn);
1537 }
1538
1539 static struct nf_proto_net *tcp_get_net_proto(struct net *net)
1540 {
1541         return &net->ct.nf_ct_proto.tcp.pn;
1542 }
1543
1544 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
1545 {
1546         .l3proto                = PF_INET,
1547         .l4proto                = IPPROTO_TCP,
1548         .pkt_to_tuple           = tcp_pkt_to_tuple,
1549         .invert_tuple           = tcp_invert_tuple,
1550 #ifdef CONFIG_NF_CONNTRACK_PROCFS
1551         .print_conntrack        = tcp_print_conntrack,
1552 #endif
1553         .packet                 = tcp_packet,
1554         .get_timeouts           = tcp_get_timeouts,
1555         .new                    = tcp_new,
1556         .error                  = tcp_error,
1557         .can_early_drop         = tcp_can_early_drop,
1558 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1559         .to_nlattr              = tcp_to_nlattr,
1560         .nlattr_size            = tcp_nlattr_size,
1561         .from_nlattr            = nlattr_to_tcp,
1562         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
1563         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
1564         .nlattr_tuple_size      = tcp_nlattr_tuple_size,
1565         .nla_policy             = nf_ct_port_nla_policy,
1566 #endif
1567 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1568         .ctnl_timeout           = {
1569                 .nlattr_to_obj  = tcp_timeout_nlattr_to_obj,
1570                 .obj_to_nlattr  = tcp_timeout_obj_to_nlattr,
1571                 .nlattr_max     = CTA_TIMEOUT_TCP_MAX,
1572                 .obj_size       = sizeof(unsigned int) *
1573                                         TCP_CONNTRACK_TIMEOUT_MAX,
1574                 .nla_policy     = tcp_timeout_nla_policy,
1575         },
1576 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1577         .init_net               = tcp_init_net,
1578         .get_net_proto          = tcp_get_net_proto,
1579 };
1580 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1581
1582 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
1583 {
1584         .l3proto                = PF_INET6,
1585         .l4proto                = IPPROTO_TCP,
1586         .pkt_to_tuple           = tcp_pkt_to_tuple,
1587         .invert_tuple           = tcp_invert_tuple,
1588 #ifdef CONFIG_NF_CONNTRACK_PROCFS
1589         .print_conntrack        = tcp_print_conntrack,
1590 #endif
1591         .packet                 = tcp_packet,
1592         .get_timeouts           = tcp_get_timeouts,
1593         .new                    = tcp_new,
1594         .error                  = tcp_error,
1595         .can_early_drop         = tcp_can_early_drop,
1596 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1597         .to_nlattr              = tcp_to_nlattr,
1598         .nlattr_size            = tcp_nlattr_size,
1599         .from_nlattr            = nlattr_to_tcp,
1600         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
1601         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
1602         .nlattr_tuple_size      = tcp_nlattr_tuple_size,
1603         .nla_policy             = nf_ct_port_nla_policy,
1604 #endif
1605 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1606         .ctnl_timeout           = {
1607                 .nlattr_to_obj  = tcp_timeout_nlattr_to_obj,
1608                 .obj_to_nlattr  = tcp_timeout_obj_to_nlattr,
1609                 .nlattr_max     = CTA_TIMEOUT_TCP_MAX,
1610                 .obj_size       = sizeof(unsigned int) *
1611                                         TCP_CONNTRACK_TIMEOUT_MAX,
1612                 .nla_policy     = tcp_timeout_nla_policy,
1613         },
1614 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
1615         .init_net               = tcp_init_net,
1616         .get_net_proto          = tcp_get_net_proto,
1617 };
1618 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);