1
2
3
4
5
6
7
8
9#include <linux/types.h>
10#include <linux/timer.h>
11#include <linux/module.h>
12#include <linux/in.h>
13#include <linux/tcp.h>
14#include <linux/spinlock.h>
15#include <linux/skbuff.h>
16#include <linux/ipv6.h>
17#include <net/ip6_checksum.h>
18#include <asm/unaligned.h>
19
20#include <net/tcp.h>
21
22#include <linux/netfilter.h>
23#include <linux/netfilter_ipv4.h>
24#include <linux/netfilter_ipv6.h>
25#include <net/netfilter/nf_conntrack.h>
26#include <net/netfilter/nf_conntrack_l4proto.h>
27#include <net/netfilter/nf_conntrack_ecache.h>
28#include <net/netfilter/nf_log.h>
29#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
30#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
31
32
33
34
35static int nf_ct_tcp_be_liberal __read_mostly = 0;
36
37
38
39static int nf_ct_tcp_loose __read_mostly = 1;
40
41
42
43
44static int nf_ct_tcp_max_retrans __read_mostly = 3;
45
46
47
48
49static const char *const tcp_conntrack_names[] = {
50 "NONE",
51 "SYN_SENT",
52 "SYN_RECV",
53 "ESTABLISHED",
54 "FIN_WAIT",
55 "CLOSE_WAIT",
56 "LAST_ACK",
57 "TIME_WAIT",
58 "CLOSE",
59 "SYN_SENT2",
60};
61
62#define SECS * HZ
63#define MINS * 60 SECS
64#define HOURS * 60 MINS
65#define DAYS * 24 HOURS
66
67static unsigned int tcp_timeouts[TCP_CONNTRACK_TIMEOUT_MAX] __read_mostly = {
68 [TCP_CONNTRACK_SYN_SENT] = 2 MINS,
69 [TCP_CONNTRACK_SYN_RECV] = 60 SECS,
70 [TCP_CONNTRACK_ESTABLISHED] = 5 DAYS,
71 [TCP_CONNTRACK_FIN_WAIT] = 2 MINS,
72 [TCP_CONNTRACK_CLOSE_WAIT] = 60 SECS,
73 [TCP_CONNTRACK_LAST_ACK] = 30 SECS,
74 [TCP_CONNTRACK_TIME_WAIT] = 2 MINS,
75 [TCP_CONNTRACK_CLOSE] = 10 SECS,
76 [TCP_CONNTRACK_SYN_SENT2] = 2 MINS,
77
78
79
80 [TCP_CONNTRACK_RETRANS] = 5 MINS,
81 [TCP_CONNTRACK_UNACK] = 5 MINS,
82};
83
84#define sNO TCP_CONNTRACK_NONE
85#define sSS TCP_CONNTRACK_SYN_SENT
86#define sSR TCP_CONNTRACK_SYN_RECV
87#define sES TCP_CONNTRACK_ESTABLISHED
88#define sFW TCP_CONNTRACK_FIN_WAIT
89#define sCW TCP_CONNTRACK_CLOSE_WAIT
90#define sLA TCP_CONNTRACK_LAST_ACK
91#define sTW TCP_CONNTRACK_TIME_WAIT
92#define sCL TCP_CONNTRACK_CLOSE
93#define sS2 TCP_CONNTRACK_SYN_SENT2
94#define sIV TCP_CONNTRACK_MAX
95#define sIG TCP_CONNTRACK_IGNORE
96
97
98enum tcp_bit_set {
99 TCP_SYN_SET,
100 TCP_SYNACK_SET,
101 TCP_FIN_SET,
102 TCP_ACK_SET,
103 TCP_RST_SET,
104 TCP_NONE_SET,
105};
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140static const u8 tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = {
141 {
142
143
144 { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sS2 },
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 { sIV, sIV, sSR, sIV, sIV, sIV, sIV, sIV, sIV, sSR },
162
163
164
165
166
167
168
169
170
171
172
173
174
175 { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV },
193
194
195
196
197
198
199
200
201
202
203
204
205
206 { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
207 { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
208 },
209 {
210
211
212 { sIV, sS2, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sS2 },
213
214
215
216
217
218
219
220
221
222
223
224
225
226 { sIV, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIG, sSR },
227
228
229
230
231
232
233
234
235
236
237
238
239 { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV },
240
241
242
243
244
245
246
247
248
249
250
251
252 { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIG },
253
254
255
256
257
258
259
260
261
262
263
264
265 { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL },
266 { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }
267 }
268};
269
270static inline struct nf_tcp_net *tcp_pernet(struct net *net)
271{
272 return &net->ct.nf_ct_proto.tcp;
273}
274
275static bool tcp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
276 struct nf_conntrack_tuple *tuple)
277{
278 const struct tcphdr *hp;
279 struct tcphdr _hdr;
280
281
282 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
283 if (hp == NULL)
284 return false;
285
286 tuple->src.u.tcp.port = hp->source;
287 tuple->dst.u.tcp.port = hp->dest;
288
289 return true;
290}
291
292static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
293 const struct nf_conntrack_tuple *orig)
294{
295 tuple->src.u.tcp.port = orig->dst.u.tcp.port;
296 tuple->dst.u.tcp.port = orig->src.u.tcp.port;
297 return true;
298}
299
300
301static int tcp_print_tuple(struct seq_file *s,
302 const struct nf_conntrack_tuple *tuple)
303{
304 return seq_printf(s, "sport=%hu dport=%hu ",
305 ntohs(tuple->src.u.tcp.port),
306 ntohs(tuple->dst.u.tcp.port));
307}
308
309
310static int tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
311{
312 enum tcp_conntrack state;
313
314 spin_lock_bh(&ct->lock);
315 state = ct->proto.tcp.state;
316 spin_unlock_bh(&ct->lock);
317
318 return seq_printf(s, "%s ", tcp_conntrack_names[state]);
319}
320
321static unsigned int get_conntrack_index(const struct tcphdr *tcph)
322{
323 if (tcph->rst) return TCP_RST_SET;
324 else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET);
325 else if (tcph->fin) return TCP_FIN_SET;
326 else if (tcph->ack) return TCP_ACK_SET;
327 else return TCP_NONE_SET;
328}
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359static inline __u32 segment_seq_plus_len(__u32 seq,
360 size_t len,
361 unsigned int dataoff,
362 const struct tcphdr *tcph)
363{
364
365
366 return (seq + len - dataoff - tcph->doff*4
367 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
368}
369
370
371#define MAXACKWINCONST 66000
372#define MAXACKWINDOW(sender) \
373 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
374 : MAXACKWINCONST)
375
376
377
378
379static void tcp_options(const struct sk_buff *skb,
380 unsigned int dataoff,
381 const struct tcphdr *tcph,
382 struct ip_ct_tcp_state *state)
383{
384 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
385 const unsigned char *ptr;
386 int length = (tcph->doff*4) - sizeof(struct tcphdr);
387
388 if (!length)
389 return;
390
391 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
392 length, buff);
393 BUG_ON(ptr == NULL);
394
395 state->td_scale =
396 state->flags = 0;
397
398 while (length > 0) {
399 int opcode=*ptr++;
400 int opsize;
401
402 switch (opcode) {
403 case TCPOPT_EOL:
404 return;
405 case TCPOPT_NOP:
406 length--;
407 continue;
408 default:
409 opsize=*ptr++;
410 if (opsize < 2)
411 return;
412 if (opsize > length)
413 return;
414
415 if (opcode == TCPOPT_SACK_PERM
416 && opsize == TCPOLEN_SACK_PERM)
417 state->flags |= IP_CT_TCP_FLAG_SACK_PERM;
418 else if (opcode == TCPOPT_WINDOW
419 && opsize == TCPOLEN_WINDOW) {
420 state->td_scale = *(u_int8_t *)ptr;
421
422 if (state->td_scale > 14) {
423
424 state->td_scale = 14;
425 }
426 state->flags |=
427 IP_CT_TCP_FLAG_WINDOW_SCALE;
428 }
429 ptr += opsize - 2;
430 length -= opsize;
431 }
432 }
433}
434
435static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
436 const struct tcphdr *tcph, __u32 *sack)
437{
438 unsigned char buff[(15 * 4) - sizeof(struct tcphdr)];
439 const unsigned char *ptr;
440 int length = (tcph->doff*4) - sizeof(struct tcphdr);
441 __u32 tmp;
442
443 if (!length)
444 return;
445
446 ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr),
447 length, buff);
448 BUG_ON(ptr == NULL);
449
450
451 if (length == TCPOLEN_TSTAMP_ALIGNED
452 && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
453 | (TCPOPT_NOP << 16)
454 | (TCPOPT_TIMESTAMP << 8)
455 | TCPOLEN_TIMESTAMP))
456 return;
457
458 while (length > 0) {
459 int opcode = *ptr++;
460 int opsize, i;
461
462 switch (opcode) {
463 case TCPOPT_EOL:
464 return;
465 case TCPOPT_NOP:
466 length--;
467 continue;
468 default:
469 opsize = *ptr++;
470 if (opsize < 2)
471 return;
472 if (opsize > length)
473 return;
474
475 if (opcode == TCPOPT_SACK
476 && opsize >= (TCPOLEN_SACK_BASE
477 + TCPOLEN_SACK_PERBLOCK)
478 && !((opsize - TCPOLEN_SACK_BASE)
479 % TCPOLEN_SACK_PERBLOCK)) {
480 for (i = 0;
481 i < (opsize - TCPOLEN_SACK_BASE);
482 i += TCPOLEN_SACK_PERBLOCK) {
483 tmp = get_unaligned_be32((__be32 *)(ptr+i)+1);
484
485 if (after(tmp, *sack))
486 *sack = tmp;
487 }
488 return;
489 }
490 ptr += opsize - 2;
491 length -= opsize;
492 }
493 }
494}
495
496#ifdef CONFIG_NF_NAT_NEEDED
497static inline s16 nat_offset(const struct nf_conn *ct,
498 enum ip_conntrack_dir dir,
499 u32 seq)
500{
501 typeof(nf_ct_nat_offset) get_offset = rcu_dereference(nf_ct_nat_offset);
502
503 return get_offset != NULL ? get_offset(ct, dir, seq) : 0;
504}
505#define NAT_OFFSET(ct, dir, seq) \
506 (nat_offset(ct, dir, seq))
507#else
508#define NAT_OFFSET(ct, dir, seq) 0
509#endif
510
511static bool tcp_in_window(const struct nf_conn *ct,
512 struct ip_ct_tcp *state,
513 enum ip_conntrack_dir dir,
514 unsigned int index,
515 const struct sk_buff *skb,
516 unsigned int dataoff,
517 const struct tcphdr *tcph,
518 u_int8_t pf)
519{
520 struct net *net = nf_ct_net(ct);
521 struct nf_tcp_net *tn = tcp_pernet(net);
522 struct ip_ct_tcp_state *sender = &state->seen[dir];
523 struct ip_ct_tcp_state *receiver = &state->seen[!dir];
524 const struct nf_conntrack_tuple *tuple = &ct->tuplehash[dir].tuple;
525 __u32 seq, ack, sack, end, win, swin;
526 s16 receiver_offset;
527 bool res;
528
529
530
531
532 seq = ntohl(tcph->seq);
533 ack = sack = ntohl(tcph->ack_seq);
534 win = ntohs(tcph->window);
535 end = segment_seq_plus_len(seq, skb->len, dataoff, tcph);
536
537 if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM)
538 tcp_sack(skb, dataoff, tcph, &sack);
539
540
541 receiver_offset = NAT_OFFSET(ct, !dir, ack - 1);
542 ack -= receiver_offset;
543 sack -= receiver_offset;
544
545 pr_debug("tcp_in_window: START\n");
546 pr_debug("tcp_in_window: ");
547 nf_ct_dump_tuple(tuple);
548 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
549 seq, ack, receiver_offset, sack, receiver_offset, win, end);
550 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
551 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
552 sender->td_end, sender->td_maxend, sender->td_maxwin,
553 sender->td_scale,
554 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
555 receiver->td_scale);
556
557 if (sender->td_maxwin == 0) {
558
559
560
561 if (tcph->syn) {
562
563
564
565
566 sender->td_end =
567 sender->td_maxend = end;
568 sender->td_maxwin = (win == 0 ? 1 : win);
569
570 tcp_options(skb, dataoff, tcph, sender);
571
572
573
574
575
576 if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE
577 && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE))
578 sender->td_scale =
579 receiver->td_scale = 0;
580 if (!tcph->ack)
581
582 return true;
583 } else {
584
585
586
587
588
589 sender->td_end = end;
590 swin = win << sender->td_scale;
591 sender->td_maxwin = (swin == 0 ? 1 : swin);
592 sender->td_maxend = end + sender->td_maxwin;
593
594
595
596
597
598 if (receiver->td_maxwin == 0)
599 receiver->td_end = receiver->td_maxend = sack;
600 }
601 } else if (((state->state == TCP_CONNTRACK_SYN_SENT
602 && dir == IP_CT_DIR_ORIGINAL)
603 || (state->state == TCP_CONNTRACK_SYN_RECV
604 && dir == IP_CT_DIR_REPLY))
605 && after(end, sender->td_end)) {
606
607
608
609
610
611 sender->td_end =
612 sender->td_maxend = end;
613 sender->td_maxwin = (win == 0 ? 1 : win);
614
615 tcp_options(skb, dataoff, tcph, sender);
616 }
617
618 if (!(tcph->ack)) {
619
620
621
622 ack = sack = receiver->td_end;
623 } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) ==
624 (TCP_FLAG_ACK|TCP_FLAG_RST))
625 && (ack == 0)) {
626
627
628
629
630 ack = sack = receiver->td_end;
631 }
632
633 if (tcph->rst && seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT)
634
635
636
637 seq = end = sender->td_end;
638
639 pr_debug("tcp_in_window: ");
640 nf_ct_dump_tuple(tuple);
641 pr_debug("seq=%u ack=%u+(%d) sack=%u+(%d) win=%u end=%u\n",
642 seq, ack, receiver_offset, sack, receiver_offset, win, end);
643 pr_debug("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i "
644 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
645 sender->td_end, sender->td_maxend, sender->td_maxwin,
646 sender->td_scale,
647 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
648 receiver->td_scale);
649
650 pr_debug("tcp_in_window: I=%i II=%i III=%i IV=%i\n",
651 before(seq, sender->td_maxend + 1),
652 after(end, sender->td_end - receiver->td_maxwin - 1),
653 before(sack, receiver->td_end + 1),
654 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1));
655
656 if (before(seq, sender->td_maxend + 1) &&
657 after(end, sender->td_end - receiver->td_maxwin - 1) &&
658 before(sack, receiver->td_end + 1) &&
659 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1)) {
660
661
662
663 if (!tcph->syn)
664 win <<= sender->td_scale;
665
666
667
668
669 swin = win + (sack - ack);
670 if (sender->td_maxwin < swin)
671 sender->td_maxwin = swin;
672 if (after(end, sender->td_end)) {
673 sender->td_end = end;
674 sender->flags |= IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
675 }
676 if (tcph->ack) {
677 if (!(sender->flags & IP_CT_TCP_FLAG_MAXACK_SET)) {
678 sender->td_maxack = ack;
679 sender->flags |= IP_CT_TCP_FLAG_MAXACK_SET;
680 } else if (after(ack, sender->td_maxack))
681 sender->td_maxack = ack;
682 }
683
684
685
686
687 if (receiver->td_maxwin != 0 && after(end, sender->td_maxend))
688 receiver->td_maxwin += end - sender->td_maxend;
689 if (after(sack + win, receiver->td_maxend - 1)) {
690 receiver->td_maxend = sack + win;
691 if (win == 0)
692 receiver->td_maxend++;
693 }
694 if (ack == receiver->td_end)
695 receiver->flags &= ~IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED;
696
697
698
699
700 if (index == TCP_ACK_SET) {
701 if (state->last_dir == dir
702 && state->last_seq == seq
703 && state->last_ack == ack
704 && state->last_end == end
705 && state->last_win == win)
706 state->retrans++;
707 else {
708 state->last_dir = dir;
709 state->last_seq = seq;
710 state->last_ack = ack;
711 state->last_end = end;
712 state->last_win = win;
713 state->retrans = 0;
714 }
715 }
716 res = true;
717 } else {
718 res = false;
719 if (sender->flags & IP_CT_TCP_FLAG_BE_LIBERAL ||
720 tn->tcp_be_liberal)
721 res = true;
722 if (!res && LOG_INVALID(net, IPPROTO_TCP))
723 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
724 "nf_ct_tcp: %s ",
725 before(seq, sender->td_maxend + 1) ?
726 after(end, sender->td_end - receiver->td_maxwin - 1) ?
727 before(sack, receiver->td_end + 1) ?
728 after(sack, receiver->td_end - MAXACKWINDOW(sender) - 1) ? "BUG"
729 : "ACK is under the lower bound (possible overly delayed ACK)"
730 : "ACK is over the upper bound (ACKed data not seen yet)"
731 : "SEQ is under the lower bound (already ACKed data retransmitted)"
732 : "SEQ is over the upper bound (over the window of the receiver)");
733 }
734
735 pr_debug("tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
736 "receiver end=%u maxend=%u maxwin=%u\n",
737 res, sender->td_end, sender->td_maxend, sender->td_maxwin,
738 receiver->td_end, receiver->td_maxend, receiver->td_maxwin);
739
740 return res;
741}
742
743
744static const u8 tcp_valid_flags[(TCPHDR_FIN|TCPHDR_SYN|TCPHDR_RST|TCPHDR_ACK|
745 TCPHDR_URG) + 1] =
746{
747 [TCPHDR_SYN] = 1,
748 [TCPHDR_SYN|TCPHDR_URG] = 1,
749 [TCPHDR_SYN|TCPHDR_ACK] = 1,
750 [TCPHDR_RST] = 1,
751 [TCPHDR_RST|TCPHDR_ACK] = 1,
752 [TCPHDR_FIN|TCPHDR_ACK] = 1,
753 [TCPHDR_FIN|TCPHDR_ACK|TCPHDR_URG] = 1,
754 [TCPHDR_ACK] = 1,
755 [TCPHDR_ACK|TCPHDR_URG] = 1,
756};
757
758
759static int tcp_error(struct net *net, struct nf_conn *tmpl,
760 struct sk_buff *skb,
761 unsigned int dataoff,
762 enum ip_conntrack_info *ctinfo,
763 u_int8_t pf,
764 unsigned int hooknum)
765{
766 const struct tcphdr *th;
767 struct tcphdr _tcph;
768 unsigned int tcplen = skb->len - dataoff;
769 u_int8_t tcpflags;
770
771
772 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
773 if (th == NULL) {
774 if (LOG_INVALID(net, IPPROTO_TCP))
775 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
776 "nf_ct_tcp: short packet ");
777 return -NF_ACCEPT;
778 }
779
780
781 if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) {
782 if (LOG_INVALID(net, IPPROTO_TCP))
783 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
784 "nf_ct_tcp: truncated/malformed packet ");
785 return -NF_ACCEPT;
786 }
787
788
789
790
791
792
793 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
794 nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) {
795 if (LOG_INVALID(net, IPPROTO_TCP))
796 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
797 "nf_ct_tcp: bad TCP checksum ");
798 return -NF_ACCEPT;
799 }
800
801
802 tcpflags = (tcp_flag_byte(th) & ~(TCPHDR_ECE|TCPHDR_CWR|TCPHDR_PSH));
803 if (!tcp_valid_flags[tcpflags]) {
804 if (LOG_INVALID(net, IPPROTO_TCP))
805 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
806 "nf_ct_tcp: invalid TCP flag combination ");
807 return -NF_ACCEPT;
808 }
809
810 return NF_ACCEPT;
811}
812
813static unsigned int *tcp_get_timeouts(struct net *net)
814{
815 return tcp_pernet(net)->timeouts;
816}
817
818
819static int tcp_packet(struct nf_conn *ct,
820 const struct sk_buff *skb,
821 unsigned int dataoff,
822 enum ip_conntrack_info ctinfo,
823 u_int8_t pf,
824 unsigned int hooknum,
825 unsigned int *timeouts)
826{
827 struct net *net = nf_ct_net(ct);
828 struct nf_tcp_net *tn = tcp_pernet(net);
829 struct nf_conntrack_tuple *tuple;
830 enum tcp_conntrack new_state, old_state;
831 enum ip_conntrack_dir dir;
832 const struct tcphdr *th;
833 struct tcphdr _tcph;
834 unsigned long timeout;
835 unsigned int index;
836
837 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
838 BUG_ON(th == NULL);
839
840 spin_lock_bh(&ct->lock);
841 old_state = ct->proto.tcp.state;
842 dir = CTINFO2DIR(ctinfo);
843 index = get_conntrack_index(th);
844 new_state = tcp_conntracks[dir][index][old_state];
845 tuple = &ct->tuplehash[dir].tuple;
846
847 switch (new_state) {
848 case TCP_CONNTRACK_SYN_SENT:
849 if (old_state < TCP_CONNTRACK_TIME_WAIT)
850 break;
851
852
853
854
855
856
857
858
859
860
861
862
863 if (((ct->proto.tcp.seen[dir].flags
864 | ct->proto.tcp.seen[!dir].flags)
865 & IP_CT_TCP_FLAG_CLOSE_INIT)
866 || (ct->proto.tcp.last_dir == dir
867 && ct->proto.tcp.last_index == TCP_RST_SET)) {
868
869
870 spin_unlock_bh(&ct->lock);
871
872
873
874
875
876 if (nf_ct_kill(ct))
877 return -NF_REPEAT;
878 return NF_DROP;
879 }
880
881 case TCP_CONNTRACK_IGNORE:
882
883
884
885
886
887
888
889
890
891
892
893
894
895 if (index == TCP_SYNACK_SET
896 && ct->proto.tcp.last_index == TCP_SYN_SET
897 && ct->proto.tcp.last_dir != dir
898 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
899
900
901
902
903
904
905 old_state = TCP_CONNTRACK_SYN_SENT;
906 new_state = TCP_CONNTRACK_SYN_RECV;
907 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_end =
908 ct->proto.tcp.last_end;
909 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxend =
910 ct->proto.tcp.last_end;
911 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_maxwin =
912 ct->proto.tcp.last_win == 0 ?
913 1 : ct->proto.tcp.last_win;
914 ct->proto.tcp.seen[ct->proto.tcp.last_dir].td_scale =
915 ct->proto.tcp.last_wscale;
916 ct->proto.tcp.seen[ct->proto.tcp.last_dir].flags =
917 ct->proto.tcp.last_flags;
918 memset(&ct->proto.tcp.seen[dir], 0,
919 sizeof(struct ip_ct_tcp_state));
920 break;
921 }
922 ct->proto.tcp.last_index = index;
923 ct->proto.tcp.last_dir = dir;
924 ct->proto.tcp.last_seq = ntohl(th->seq);
925 ct->proto.tcp.last_end =
926 segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th);
927 ct->proto.tcp.last_win = ntohs(th->window);
928
929
930
931
932
933
934 if (index == TCP_SYN_SET && dir == IP_CT_DIR_ORIGINAL) {
935 struct ip_ct_tcp_state seen = {};
936
937 ct->proto.tcp.last_flags =
938 ct->proto.tcp.last_wscale = 0;
939 tcp_options(skb, dataoff, th, &seen);
940 if (seen.flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
941 ct->proto.tcp.last_flags |=
942 IP_CT_TCP_FLAG_WINDOW_SCALE;
943 ct->proto.tcp.last_wscale = seen.td_scale;
944 }
945 if (seen.flags & IP_CT_TCP_FLAG_SACK_PERM) {
946 ct->proto.tcp.last_flags |=
947 IP_CT_TCP_FLAG_SACK_PERM;
948 }
949 }
950 spin_unlock_bh(&ct->lock);
951 if (LOG_INVALID(net, IPPROTO_TCP))
952 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
953 "nf_ct_tcp: invalid packet ignored in "
954 "state %s ", tcp_conntrack_names[old_state]);
955 return NF_ACCEPT;
956 case TCP_CONNTRACK_MAX:
957
958 pr_debug("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n",
959 dir, get_conntrack_index(th), old_state);
960 spin_unlock_bh(&ct->lock);
961 if (LOG_INVALID(net, IPPROTO_TCP))
962 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
963 "nf_ct_tcp: invalid state ");
964 return -NF_ACCEPT;
965 case TCP_CONNTRACK_CLOSE:
966 if (index == TCP_RST_SET
967 && (ct->proto.tcp.seen[!dir].flags & IP_CT_TCP_FLAG_MAXACK_SET)
968 && before(ntohl(th->seq), ct->proto.tcp.seen[!dir].td_maxack)) {
969
970 spin_unlock_bh(&ct->lock);
971 if (LOG_INVALID(net, IPPROTO_TCP))
972 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
973 "nf_ct_tcp: invalid RST ");
974 return -NF_ACCEPT;
975 }
976 if (index == TCP_RST_SET
977 && ((test_bit(IPS_SEEN_REPLY_BIT, &ct->status)
978 && ct->proto.tcp.last_index == TCP_SYN_SET)
979 || (!test_bit(IPS_ASSURED_BIT, &ct->status)
980 && ct->proto.tcp.last_index == TCP_ACK_SET))
981 && ntohl(th->ack_seq) == ct->proto.tcp.last_end) {
982
983
984
985
986
987
988
989
990
991 goto in_window;
992 }
993
994 default:
995
996 break;
997 }
998
999 if (!tcp_in_window(ct, &ct->proto.tcp, dir, index,
1000 skb, dataoff, th, pf)) {
1001 spin_unlock_bh(&ct->lock);
1002 return -NF_ACCEPT;
1003 }
1004 in_window:
1005
1006 ct->proto.tcp.last_index = index;
1007 ct->proto.tcp.last_dir = dir;
1008
1009 pr_debug("tcp_conntracks: ");
1010 nf_ct_dump_tuple(tuple);
1011 pr_debug("syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n",
1012 (th->syn ? 1 : 0), (th->ack ? 1 : 0),
1013 (th->fin ? 1 : 0), (th->rst ? 1 : 0),
1014 old_state, new_state);
1015
1016 ct->proto.tcp.state = new_state;
1017 if (old_state != new_state
1018 && new_state == TCP_CONNTRACK_FIN_WAIT)
1019 ct->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT;
1020
1021 if (ct->proto.tcp.retrans >= tn->tcp_max_retrans &&
1022 timeouts[new_state] > timeouts[TCP_CONNTRACK_RETRANS])
1023 timeout = timeouts[TCP_CONNTRACK_RETRANS];
1024 else if ((ct->proto.tcp.seen[0].flags | ct->proto.tcp.seen[1].flags) &
1025 IP_CT_TCP_FLAG_DATA_UNACKNOWLEDGED &&
1026 timeouts[new_state] > timeouts[TCP_CONNTRACK_UNACK])
1027 timeout = timeouts[TCP_CONNTRACK_UNACK];
1028 else
1029 timeout = timeouts[new_state];
1030 spin_unlock_bh(&ct->lock);
1031
1032 if (new_state != old_state)
1033 nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
1034
1035 if (!test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1036
1037
1038
1039
1040 if (th->rst) {
1041 nf_ct_kill_acct(ct, ctinfo, skb);
1042 return NF_ACCEPT;
1043 }
1044 } else if (!test_bit(IPS_ASSURED_BIT, &ct->status)
1045 && (old_state == TCP_CONNTRACK_SYN_RECV
1046 || old_state == TCP_CONNTRACK_ESTABLISHED)
1047 && new_state == TCP_CONNTRACK_ESTABLISHED) {
1048
1049
1050
1051 set_bit(IPS_ASSURED_BIT, &ct->status);
1052 nf_conntrack_event_cache(IPCT_ASSURED, ct);
1053 }
1054 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
1055
1056 return NF_ACCEPT;
1057}
1058
1059
1060static bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
1061 unsigned int dataoff, unsigned int *timeouts)
1062{
1063 enum tcp_conntrack new_state;
1064 const struct tcphdr *th;
1065 struct tcphdr _tcph;
1066 struct net *net = nf_ct_net(ct);
1067 struct nf_tcp_net *tn = tcp_pernet(net);
1068 const struct ip_ct_tcp_state *sender = &ct->proto.tcp.seen[0];
1069 const struct ip_ct_tcp_state *receiver = &ct->proto.tcp.seen[1];
1070
1071 th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph);
1072 BUG_ON(th == NULL);
1073
1074
1075 new_state = tcp_conntracks[0][get_conntrack_index(th)][TCP_CONNTRACK_NONE];
1076
1077
1078 if (new_state >= TCP_CONNTRACK_MAX) {
1079 pr_debug("nf_ct_tcp: invalid new deleting.\n");
1080 return false;
1081 }
1082
1083 if (new_state == TCP_CONNTRACK_SYN_SENT) {
1084 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
1085
1086 ct->proto.tcp.seen[0].td_end =
1087 segment_seq_plus_len(ntohl(th->seq), skb->len,
1088 dataoff, th);
1089 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1090 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1091 ct->proto.tcp.seen[0].td_maxwin = 1;
1092 ct->proto.tcp.seen[0].td_maxend =
1093 ct->proto.tcp.seen[0].td_end;
1094
1095 tcp_options(skb, dataoff, th, &ct->proto.tcp.seen[0]);
1096 } else if (tn->tcp_loose == 0) {
1097
1098 return false;
1099 } else {
1100 memset(&ct->proto.tcp, 0, sizeof(ct->proto.tcp));
1101
1102
1103
1104
1105
1106 ct->proto.tcp.seen[0].td_end =
1107 segment_seq_plus_len(ntohl(th->seq), skb->len,
1108 dataoff, th);
1109 ct->proto.tcp.seen[0].td_maxwin = ntohs(th->window);
1110 if (ct->proto.tcp.seen[0].td_maxwin == 0)
1111 ct->proto.tcp.seen[0].td_maxwin = 1;
1112 ct->proto.tcp.seen[0].td_maxend =
1113 ct->proto.tcp.seen[0].td_end +
1114 ct->proto.tcp.seen[0].td_maxwin;
1115
1116
1117
1118 ct->proto.tcp.seen[0].flags =
1119 ct->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM |
1120 IP_CT_TCP_FLAG_BE_LIBERAL;
1121 }
1122
1123
1124 ct->proto.tcp.last_index = TCP_NONE_SET;
1125
1126 pr_debug("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i "
1127 "receiver end=%u maxend=%u maxwin=%u scale=%i\n",
1128 sender->td_end, sender->td_maxend, sender->td_maxwin,
1129 sender->td_scale,
1130 receiver->td_end, receiver->td_maxend, receiver->td_maxwin,
1131 receiver->td_scale);
1132 return true;
1133}
1134
1135#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1136
1137#include <linux/netfilter/nfnetlink.h>
1138#include <linux/netfilter/nfnetlink_conntrack.h>
1139
1140static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
1141 struct nf_conn *ct)
1142{
1143 struct nlattr *nest_parms;
1144 struct nf_ct_tcp_flags tmp = {};
1145
1146 spin_lock_bh(&ct->lock);
1147 nest_parms = nla_nest_start(skb, CTA_PROTOINFO_TCP | NLA_F_NESTED);
1148 if (!nest_parms)
1149 goto nla_put_failure;
1150
1151 if (nla_put_u8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state) ||
1152 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
1153 ct->proto.tcp.seen[0].td_scale) ||
1154 nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_REPLY,
1155 ct->proto.tcp.seen[1].td_scale))
1156 goto nla_put_failure;
1157
1158 tmp.flags = ct->proto.tcp.seen[0].flags;
1159 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
1160 sizeof(struct nf_ct_tcp_flags), &tmp))
1161 goto nla_put_failure;
1162
1163 tmp.flags = ct->proto.tcp.seen[1].flags;
1164 if (nla_put(skb, CTA_PROTOINFO_TCP_FLAGS_REPLY,
1165 sizeof(struct nf_ct_tcp_flags), &tmp))
1166 goto nla_put_failure;
1167 spin_unlock_bh(&ct->lock);
1168
1169 nla_nest_end(skb, nest_parms);
1170
1171 return 0;
1172
1173nla_put_failure:
1174 spin_unlock_bh(&ct->lock);
1175 return -1;
1176}
1177
1178static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX+1] = {
1179 [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
1180 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
1181 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
1182 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .len = sizeof(struct nf_ct_tcp_flags) },
1183 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .len = sizeof(struct nf_ct_tcp_flags) },
1184};
1185
1186static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)
1187{
1188 struct nlattr *pattr = cda[CTA_PROTOINFO_TCP];
1189 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
1190 int err;
1191
1192
1193
1194 if (!pattr)
1195 return 0;
1196
1197 err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, pattr, tcp_nla_policy);
1198 if (err < 0)
1199 return err;
1200
1201 if (tb[CTA_PROTOINFO_TCP_STATE] &&
1202 nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]) >= TCP_CONNTRACK_MAX)
1203 return -EINVAL;
1204
1205 spin_lock_bh(&ct->lock);
1206 if (tb[CTA_PROTOINFO_TCP_STATE])
1207 ct->proto.tcp.state = nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]);
1208
1209 if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
1210 struct nf_ct_tcp_flags *attr =
1211 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
1212 ct->proto.tcp.seen[0].flags &= ~attr->mask;
1213 ct->proto.tcp.seen[0].flags |= attr->flags & attr->mask;
1214 }
1215
1216 if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
1217 struct nf_ct_tcp_flags *attr =
1218 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
1219 ct->proto.tcp.seen[1].flags &= ~attr->mask;
1220 ct->proto.tcp.seen[1].flags |= attr->flags & attr->mask;
1221 }
1222
1223 if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] &&
1224 tb[CTA_PROTOINFO_TCP_WSCALE_REPLY] &&
1225 ct->proto.tcp.seen[0].flags & IP_CT_TCP_FLAG_WINDOW_SCALE &&
1226 ct->proto.tcp.seen[1].flags & IP_CT_TCP_FLAG_WINDOW_SCALE) {
1227 ct->proto.tcp.seen[0].td_scale =
1228 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL]);
1229 ct->proto.tcp.seen[1].td_scale =
1230 nla_get_u8(tb[CTA_PROTOINFO_TCP_WSCALE_REPLY]);
1231 }
1232 spin_unlock_bh(&ct->lock);
1233
1234 return 0;
1235}
1236
1237static int tcp_nlattr_size(void)
1238{
1239 return nla_total_size(0)
1240 + nla_policy_len(tcp_nla_policy, CTA_PROTOINFO_TCP_MAX + 1);
1241}
1242
1243static int tcp_nlattr_tuple_size(void)
1244{
1245 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1246}
1247#endif
1248
1249#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1250
1251#include <linux/netfilter/nfnetlink.h>
1252#include <linux/netfilter/nfnetlink_cttimeout.h>
1253
1254static int tcp_timeout_nlattr_to_obj(struct nlattr *tb[],
1255 struct net *net, void *data)
1256{
1257 unsigned int *timeouts = data;
1258 struct nf_tcp_net *tn = tcp_pernet(net);
1259 int i;
1260
1261
1262 for (i=0; i<TCP_CONNTRACK_TIMEOUT_MAX; i++)
1263 timeouts[i] = tn->timeouts[i];
1264
1265 if (tb[CTA_TIMEOUT_TCP_SYN_SENT]) {
1266 timeouts[TCP_CONNTRACK_SYN_SENT] =
1267 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT]))*HZ;
1268 }
1269 if (tb[CTA_TIMEOUT_TCP_SYN_RECV]) {
1270 timeouts[TCP_CONNTRACK_SYN_RECV] =
1271 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_RECV]))*HZ;
1272 }
1273 if (tb[CTA_TIMEOUT_TCP_ESTABLISHED]) {
1274 timeouts[TCP_CONNTRACK_ESTABLISHED] =
1275 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_ESTABLISHED]))*HZ;
1276 }
1277 if (tb[CTA_TIMEOUT_TCP_FIN_WAIT]) {
1278 timeouts[TCP_CONNTRACK_FIN_WAIT] =
1279 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_FIN_WAIT]))*HZ;
1280 }
1281 if (tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]) {
1282 timeouts[TCP_CONNTRACK_CLOSE_WAIT] =
1283 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]))*HZ;
1284 }
1285 if (tb[CTA_TIMEOUT_TCP_LAST_ACK]) {
1286 timeouts[TCP_CONNTRACK_LAST_ACK] =
1287 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_LAST_ACK]))*HZ;
1288 }
1289 if (tb[CTA_TIMEOUT_TCP_TIME_WAIT]) {
1290 timeouts[TCP_CONNTRACK_TIME_WAIT] =
1291 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_TIME_WAIT]))*HZ;
1292 }
1293 if (tb[CTA_TIMEOUT_TCP_CLOSE]) {
1294 timeouts[TCP_CONNTRACK_CLOSE] =
1295 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE]))*HZ;
1296 }
1297 if (tb[CTA_TIMEOUT_TCP_SYN_SENT2]) {
1298 timeouts[TCP_CONNTRACK_SYN_SENT2] =
1299 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT2]))*HZ;
1300 }
1301 if (tb[CTA_TIMEOUT_TCP_RETRANS]) {
1302 timeouts[TCP_CONNTRACK_RETRANS] =
1303 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_RETRANS]))*HZ;
1304 }
1305 if (tb[CTA_TIMEOUT_TCP_UNACK]) {
1306 timeouts[TCP_CONNTRACK_UNACK] =
1307 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_UNACK]))*HZ;
1308 }
1309 return 0;
1310}
1311
1312static int
1313tcp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
1314{
1315 const unsigned int *timeouts = data;
1316
1317 if (nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT,
1318 htonl(timeouts[TCP_CONNTRACK_SYN_SENT] / HZ)) ||
1319 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_RECV,
1320 htonl(timeouts[TCP_CONNTRACK_SYN_RECV] / HZ)) ||
1321 nla_put_be32(skb, CTA_TIMEOUT_TCP_ESTABLISHED,
1322 htonl(timeouts[TCP_CONNTRACK_ESTABLISHED] / HZ)) ||
1323 nla_put_be32(skb, CTA_TIMEOUT_TCP_FIN_WAIT,
1324 htonl(timeouts[TCP_CONNTRACK_FIN_WAIT] / HZ)) ||
1325 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE_WAIT,
1326 htonl(timeouts[TCP_CONNTRACK_CLOSE_WAIT] / HZ)) ||
1327 nla_put_be32(skb, CTA_TIMEOUT_TCP_LAST_ACK,
1328 htonl(timeouts[TCP_CONNTRACK_LAST_ACK] / HZ)) ||
1329 nla_put_be32(skb, CTA_TIMEOUT_TCP_TIME_WAIT,
1330 htonl(timeouts[TCP_CONNTRACK_TIME_WAIT] / HZ)) ||
1331 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE,
1332 htonl(timeouts[TCP_CONNTRACK_CLOSE] / HZ)) ||
1333 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT2,
1334 htonl(timeouts[TCP_CONNTRACK_SYN_SENT2] / HZ)) ||
1335 nla_put_be32(skb, CTA_TIMEOUT_TCP_RETRANS,
1336 htonl(timeouts[TCP_CONNTRACK_RETRANS] / HZ)) ||
1337 nla_put_be32(skb, CTA_TIMEOUT_TCP_UNACK,
1338 htonl(timeouts[TCP_CONNTRACK_UNACK] / HZ)))
1339 goto nla_put_failure;
1340 return 0;
1341
1342nla_put_failure:
1343 return -ENOSPC;
1344}
1345
1346static const struct nla_policy tcp_timeout_nla_policy[CTA_TIMEOUT_TCP_MAX+1] = {
1347 [CTA_TIMEOUT_TCP_SYN_SENT] = { .type = NLA_U32 },
1348 [CTA_TIMEOUT_TCP_SYN_RECV] = { .type = NLA_U32 },
1349 [CTA_TIMEOUT_TCP_ESTABLISHED] = { .type = NLA_U32 },
1350 [CTA_TIMEOUT_TCP_FIN_WAIT] = { .type = NLA_U32 },
1351 [CTA_TIMEOUT_TCP_CLOSE_WAIT] = { .type = NLA_U32 },
1352 [CTA_TIMEOUT_TCP_LAST_ACK] = { .type = NLA_U32 },
1353 [CTA_TIMEOUT_TCP_TIME_WAIT] = { .type = NLA_U32 },
1354 [CTA_TIMEOUT_TCP_CLOSE] = { .type = NLA_U32 },
1355 [CTA_TIMEOUT_TCP_SYN_SENT2] = { .type = NLA_U32 },
1356 [CTA_TIMEOUT_TCP_RETRANS] = { .type = NLA_U32 },
1357 [CTA_TIMEOUT_TCP_UNACK] = { .type = NLA_U32 },
1358};
1359#endif
1360
1361#ifdef CONFIG_SYSCTL
1362static struct ctl_table tcp_sysctl_table[] = {
1363 {
1364 .procname = "nf_conntrack_tcp_timeout_syn_sent",
1365 .maxlen = sizeof(unsigned int),
1366 .mode = 0644,
1367 .proc_handler = proc_dointvec_jiffies,
1368 },
1369 {
1370 .procname = "nf_conntrack_tcp_timeout_syn_recv",
1371 .maxlen = sizeof(unsigned int),
1372 .mode = 0644,
1373 .proc_handler = proc_dointvec_jiffies,
1374 },
1375 {
1376 .procname = "nf_conntrack_tcp_timeout_established",
1377 .maxlen = sizeof(unsigned int),
1378 .mode = 0644,
1379 .proc_handler = proc_dointvec_jiffies,
1380 },
1381 {
1382 .procname = "nf_conntrack_tcp_timeout_fin_wait",
1383 .maxlen = sizeof(unsigned int),
1384 .mode = 0644,
1385 .proc_handler = proc_dointvec_jiffies,
1386 },
1387 {
1388 .procname = "nf_conntrack_tcp_timeout_close_wait",
1389 .maxlen = sizeof(unsigned int),
1390 .mode = 0644,
1391 .proc_handler = proc_dointvec_jiffies,
1392 },
1393 {
1394 .procname = "nf_conntrack_tcp_timeout_last_ack",
1395 .maxlen = sizeof(unsigned int),
1396 .mode = 0644,
1397 .proc_handler = proc_dointvec_jiffies,
1398 },
1399 {
1400 .procname = "nf_conntrack_tcp_timeout_time_wait",
1401 .maxlen = sizeof(unsigned int),
1402 .mode = 0644,
1403 .proc_handler = proc_dointvec_jiffies,
1404 },
1405 {
1406 .procname = "nf_conntrack_tcp_timeout_close",
1407 .maxlen = sizeof(unsigned int),
1408 .mode = 0644,
1409 .proc_handler = proc_dointvec_jiffies,
1410 },
1411 {
1412 .procname = "nf_conntrack_tcp_timeout_max_retrans",
1413 .maxlen = sizeof(unsigned int),
1414 .mode = 0644,
1415 .proc_handler = proc_dointvec_jiffies,
1416 },
1417 {
1418 .procname = "nf_conntrack_tcp_timeout_unacknowledged",
1419 .maxlen = sizeof(unsigned int),
1420 .mode = 0644,
1421 .proc_handler = proc_dointvec_jiffies,
1422 },
1423 {
1424 .procname = "nf_conntrack_tcp_loose",
1425 .maxlen = sizeof(unsigned int),
1426 .mode = 0644,
1427 .proc_handler = proc_dointvec,
1428 },
1429 {
1430 .procname = "nf_conntrack_tcp_be_liberal",
1431 .maxlen = sizeof(unsigned int),
1432 .mode = 0644,
1433 .proc_handler = proc_dointvec,
1434 },
1435 {
1436 .procname = "nf_conntrack_tcp_max_retrans",
1437 .maxlen = sizeof(unsigned int),
1438 .mode = 0644,
1439 .proc_handler = proc_dointvec,
1440 },
1441 { }
1442};
1443
1444#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1445static struct ctl_table tcp_compat_sysctl_table[] = {
1446 {
1447 .procname = "ip_conntrack_tcp_timeout_syn_sent",
1448 .maxlen = sizeof(unsigned int),
1449 .mode = 0644,
1450 .proc_handler = proc_dointvec_jiffies,
1451 },
1452 {
1453 .procname = "ip_conntrack_tcp_timeout_syn_sent2",
1454 .maxlen = sizeof(unsigned int),
1455 .mode = 0644,
1456 .proc_handler = proc_dointvec_jiffies,
1457 },
1458 {
1459 .procname = "ip_conntrack_tcp_timeout_syn_recv",
1460 .maxlen = sizeof(unsigned int),
1461 .mode = 0644,
1462 .proc_handler = proc_dointvec_jiffies,
1463 },
1464 {
1465 .procname = "ip_conntrack_tcp_timeout_established",
1466 .maxlen = sizeof(unsigned int),
1467 .mode = 0644,
1468 .proc_handler = proc_dointvec_jiffies,
1469 },
1470 {
1471 .procname = "ip_conntrack_tcp_timeout_fin_wait",
1472 .maxlen = sizeof(unsigned int),
1473 .mode = 0644,
1474 .proc_handler = proc_dointvec_jiffies,
1475 },
1476 {
1477 .procname = "ip_conntrack_tcp_timeout_close_wait",
1478 .maxlen = sizeof(unsigned int),
1479 .mode = 0644,
1480 .proc_handler = proc_dointvec_jiffies,
1481 },
1482 {
1483 .procname = "ip_conntrack_tcp_timeout_last_ack",
1484 .maxlen = sizeof(unsigned int),
1485 .mode = 0644,
1486 .proc_handler = proc_dointvec_jiffies,
1487 },
1488 {
1489 .procname = "ip_conntrack_tcp_timeout_time_wait",
1490 .maxlen = sizeof(unsigned int),
1491 .mode = 0644,
1492 .proc_handler = proc_dointvec_jiffies,
1493 },
1494 {
1495 .procname = "ip_conntrack_tcp_timeout_close",
1496 .maxlen = sizeof(unsigned int),
1497 .mode = 0644,
1498 .proc_handler = proc_dointvec_jiffies,
1499 },
1500 {
1501 .procname = "ip_conntrack_tcp_timeout_max_retrans",
1502 .maxlen = sizeof(unsigned int),
1503 .mode = 0644,
1504 .proc_handler = proc_dointvec_jiffies,
1505 },
1506 {
1507 .procname = "ip_conntrack_tcp_loose",
1508 .maxlen = sizeof(unsigned int),
1509 .mode = 0644,
1510 .proc_handler = proc_dointvec,
1511 },
1512 {
1513 .procname = "ip_conntrack_tcp_be_liberal",
1514 .maxlen = sizeof(unsigned int),
1515 .mode = 0644,
1516 .proc_handler = proc_dointvec,
1517 },
1518 {
1519 .procname = "ip_conntrack_tcp_max_retrans",
1520 .maxlen = sizeof(unsigned int),
1521 .mode = 0644,
1522 .proc_handler = proc_dointvec,
1523 },
1524 { }
1525};
1526#endif
1527#endif
1528
1529static int tcp_kmemdup_sysctl_table(struct nf_proto_net *pn,
1530 struct nf_tcp_net *tn)
1531{
1532#ifdef CONFIG_SYSCTL
1533 if (pn->ctl_table)
1534 return 0;
1535
1536 pn->ctl_table = kmemdup(tcp_sysctl_table,
1537 sizeof(tcp_sysctl_table),
1538 GFP_KERNEL);
1539 if (!pn->ctl_table)
1540 return -ENOMEM;
1541
1542 pn->ctl_table[0].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT];
1543 pn->ctl_table[1].data = &tn->timeouts[TCP_CONNTRACK_SYN_RECV];
1544 pn->ctl_table[2].data = &tn->timeouts[TCP_CONNTRACK_ESTABLISHED];
1545 pn->ctl_table[3].data = &tn->timeouts[TCP_CONNTRACK_FIN_WAIT];
1546 pn->ctl_table[4].data = &tn->timeouts[TCP_CONNTRACK_CLOSE_WAIT];
1547 pn->ctl_table[5].data = &tn->timeouts[TCP_CONNTRACK_LAST_ACK];
1548 pn->ctl_table[6].data = &tn->timeouts[TCP_CONNTRACK_TIME_WAIT];
1549 pn->ctl_table[7].data = &tn->timeouts[TCP_CONNTRACK_CLOSE];
1550 pn->ctl_table[8].data = &tn->timeouts[TCP_CONNTRACK_RETRANS];
1551 pn->ctl_table[9].data = &tn->timeouts[TCP_CONNTRACK_UNACK];
1552 pn->ctl_table[10].data = &tn->tcp_loose;
1553 pn->ctl_table[11].data = &tn->tcp_be_liberal;
1554 pn->ctl_table[12].data = &tn->tcp_max_retrans;
1555#endif
1556 return 0;
1557}
1558
1559static int tcp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
1560 struct nf_tcp_net *tn)
1561{
1562#ifdef CONFIG_SYSCTL
1563#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1564 pn->ctl_compat_table = kmemdup(tcp_compat_sysctl_table,
1565 sizeof(tcp_compat_sysctl_table),
1566 GFP_KERNEL);
1567 if (!pn->ctl_compat_table)
1568 return -ENOMEM;
1569
1570 pn->ctl_compat_table[0].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT];
1571 pn->ctl_compat_table[1].data = &tn->timeouts[TCP_CONNTRACK_SYN_SENT2];
1572 pn->ctl_compat_table[2].data = &tn->timeouts[TCP_CONNTRACK_SYN_RECV];
1573 pn->ctl_compat_table[3].data = &tn->timeouts[TCP_CONNTRACK_ESTABLISHED];
1574 pn->ctl_compat_table[4].data = &tn->timeouts[TCP_CONNTRACK_FIN_WAIT];
1575 pn->ctl_compat_table[5].data = &tn->timeouts[TCP_CONNTRACK_CLOSE_WAIT];
1576 pn->ctl_compat_table[6].data = &tn->timeouts[TCP_CONNTRACK_LAST_ACK];
1577 pn->ctl_compat_table[7].data = &tn->timeouts[TCP_CONNTRACK_TIME_WAIT];
1578 pn->ctl_compat_table[8].data = &tn->timeouts[TCP_CONNTRACK_CLOSE];
1579 pn->ctl_compat_table[9].data = &tn->timeouts[TCP_CONNTRACK_RETRANS];
1580 pn->ctl_compat_table[10].data = &tn->tcp_loose;
1581 pn->ctl_compat_table[11].data = &tn->tcp_be_liberal;
1582 pn->ctl_compat_table[12].data = &tn->tcp_max_retrans;
1583#endif
1584#endif
1585 return 0;
1586}
1587
1588static int tcp_init_net(struct net *net, u_int16_t proto)
1589{
1590 int ret;
1591 struct nf_tcp_net *tn = tcp_pernet(net);
1592 struct nf_proto_net *pn = &tn->pn;
1593
1594 if (!pn->users) {
1595 int i;
1596
1597 for (i = 0; i < TCP_CONNTRACK_TIMEOUT_MAX; i++)
1598 tn->timeouts[i] = tcp_timeouts[i];
1599
1600 tn->tcp_loose = nf_ct_tcp_loose;
1601 tn->tcp_be_liberal = nf_ct_tcp_be_liberal;
1602 tn->tcp_max_retrans = nf_ct_tcp_max_retrans;
1603 }
1604
1605 if (proto == AF_INET) {
1606 ret = tcp_kmemdup_compat_sysctl_table(pn, tn);
1607 if (ret < 0)
1608 return ret;
1609
1610 ret = tcp_kmemdup_sysctl_table(pn, tn);
1611 if (ret < 0)
1612 nf_ct_kfree_compat_sysctl_table(pn);
1613 } else
1614 ret = tcp_kmemdup_sysctl_table(pn, tn);
1615
1616 return ret;
1617}
1618
1619static struct nf_proto_net *tcp_get_net_proto(struct net *net)
1620{
1621 return &net->ct.nf_ct_proto.tcp.pn;
1622}
1623
1624struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4 __read_mostly =
1625{
1626 .l3proto = PF_INET,
1627 .l4proto = IPPROTO_TCP,
1628 .name = "tcp",
1629 .pkt_to_tuple = tcp_pkt_to_tuple,
1630 .invert_tuple = tcp_invert_tuple,
1631 .print_tuple = tcp_print_tuple,
1632 .print_conntrack = tcp_print_conntrack,
1633 .packet = tcp_packet,
1634 .get_timeouts = tcp_get_timeouts,
1635 .new = tcp_new,
1636 .error = tcp_error,
1637#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1638 .to_nlattr = tcp_to_nlattr,
1639 .nlattr_size = tcp_nlattr_size,
1640 .from_nlattr = nlattr_to_tcp,
1641 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1642 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1643 .nlattr_tuple_size = tcp_nlattr_tuple_size,
1644 .nla_policy = nf_ct_port_nla_policy,
1645#endif
1646#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1647 .ctnl_timeout = {
1648 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1649 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1650 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1651 .obj_size = sizeof(unsigned int) *
1652 TCP_CONNTRACK_TIMEOUT_MAX,
1653 .nla_policy = tcp_timeout_nla_policy,
1654 },
1655#endif
1656 .init_net = tcp_init_net,
1657 .get_net_proto = tcp_get_net_proto,
1658};
1659EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp4);
1660
1661struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6 __read_mostly =
1662{
1663 .l3proto = PF_INET6,
1664 .l4proto = IPPROTO_TCP,
1665 .name = "tcp",
1666 .pkt_to_tuple = tcp_pkt_to_tuple,
1667 .invert_tuple = tcp_invert_tuple,
1668 .print_tuple = tcp_print_tuple,
1669 .print_conntrack = tcp_print_conntrack,
1670 .packet = tcp_packet,
1671 .get_timeouts = tcp_get_timeouts,
1672 .new = tcp_new,
1673 .error = tcp_error,
1674#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1675 .to_nlattr = tcp_to_nlattr,
1676 .nlattr_size = tcp_nlattr_size,
1677 .from_nlattr = nlattr_to_tcp,
1678 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
1679 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
1680 .nlattr_tuple_size = tcp_nlattr_tuple_size,
1681 .nla_policy = nf_ct_port_nla_policy,
1682#endif
1683#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1684 .ctnl_timeout = {
1685 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1686 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1687 .nlattr_max = CTA_TIMEOUT_TCP_MAX,
1688 .obj_size = sizeof(unsigned int) *
1689 TCP_CONNTRACK_TIMEOUT_MAX,
1690 .nla_policy = tcp_timeout_nla_policy,
1691 },
1692#endif
1693 .init_net = tcp_init_net,
1694 .get_net_proto = tcp_get_net_proto,
1695};
1696EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_tcp6);
1697