9 #include <linux/types.h>
11 #include <linux/module.h>
13 #include <linux/tcp.h>
16 #include <linux/ipv6.h>
18 #include <asm/unaligned.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <linux/netfilter_ipv6.h>
49 static const char *
const tcp_conntrack_names[] = {
63 #define MINS * 60 SECS
64 #define HOURS * 60 MINS
65 #define DAYS * 24 HOURS
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
144 {
sSS,
sSS,
sIG,
sIG,
sIG,
sIG,
sIG,
sSS,
sSS,
sS2 },
161 {
sIV,
sIV,
sSR,
sIV,
sIV,
sIV,
sIV,
sIV,
sIV, sSR },
175 {
sIV,
sIV,
sFW,
sFW,
sLA,
sLA,
sLA,
sTW,
sCL, sIV },
192 {
sES,
sIV,
sES,
sES,
sCW,
sCW,
sTW,
sTW,
sCL, sIV },
206 {
sIV,
sCL,
sCL,
sCL,
sCL,
sCL,
sCL,
sCL,
sCL, sCL },
212 {
sIV,
sS2,
sIV,
sIV,
sIV,
sIV,
sIV,
sIV,
sIV, sS2 },
226 {
sIV,
sSR,
sIG,
sIG,
sIG,
sIG,
sIG,
sIG,
sIG, sSR },
239 {
sIV,
sIV,
sFW,
sFW,
sLA,
sLA,
sLA,
sTW,
sCL, sIV },
252 {
sIV,
sIG,
sSR,
sES,
sCW,
sCW,
sTW,
sTW,
sCL, sIG },
265 {
sIV,
sCL,
sCL,
sCL,
sCL,
sCL,
sCL,
sCL,
sCL, sCL },
272 return &net->ct.nf_ct_proto.tcp;
275 static bool tcp_pkt_to_tuple(
const struct sk_buff *
skb,
unsigned int dataoff,
282 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
286 tuple->
src.u.tcp.port = hp->source;
287 tuple->
dst.u.tcp.port = hp->dest;
295 tuple->
src.u.tcp.port = orig->
dst.u.tcp.port;
296 tuple->
dst.u.tcp.port = orig->
src.u.tcp.port;
301 static int tcp_print_tuple(
struct seq_file *
s,
314 spin_lock_bh(&ct->
lock);
316 spin_unlock_bh(&ct->
lock);
318 return seq_printf(s,
"%s ", tcp_conntrack_names[state]);
321 static unsigned int get_conntrack_index(
const struct tcphdr *tcph)
361 unsigned int dataoff,
362 const struct tcphdr *tcph)
366 return (seq + len - dataoff - tcph->doff*4
367 + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0));
371 #define MAXACKWINCONST 66000
372 #define MAXACKWINDOW(sender) \
373 ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \
380 unsigned int dataoff,
381 const struct tcphdr *tcph,
384 unsigned char buff[(15 * 4) -
sizeof(
struct tcphdr)];
385 const unsigned char *
ptr;
391 ptr = skb_header_pointer(skb, dataoff +
sizeof(
struct tcphdr),
435 static void tcp_sack(
const struct sk_buff *skb,
unsigned int dataoff,
438 unsigned char buff[(15 * 4) -
sizeof(
struct tcphdr)];
439 const unsigned char *
ptr;
446 ptr = skb_header_pointer(skb, dataoff +
sizeof(
struct tcphdr),
485 if (
after(tmp, *sack))
496 #ifdef CONFIG_NF_NAT_NEEDED
503 return get_offset !=
NULL ? get_offset(ct, dir, seq) : 0;
505 #define NAT_OFFSET(ct, dir, seq) \
506 (nat_offset(ct, dir, seq))
508 #define NAT_OFFSET(ct, dir, seq) 0
511 static bool tcp_in_window(
const struct nf_conn *ct,
516 unsigned int dataoff,
517 const struct tcphdr *tcph,
520 struct net *
net = nf_ct_net(ct);
535 end = segment_seq_plus_len(seq, skb->
len, dataoff, tcph);
538 tcp_sack(skb, dataoff, tcph, &sack);
541 receiver_offset =
NAT_OFFSET(ct, !dir, ack - 1);
542 ack -= receiver_offset;
543 sack -= receiver_offset;
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",
591 sender->
td_maxwin = (swin == 0 ? 1 : swin);
622 ack = sack = receiver->
td_end;
630 ack = sack = receiver->
td_end;
637 seq = end = sender->
td_end;
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",
650 pr_debug(
"tcp_in_window: I=%i II=%i III=%i IV=%i\n",
653 before(sack, receiver->
td_end + 1),
656 if (before(seq, sender->
td_maxend + 1) &&
658 before(sack, receiver->
td_end + 1) &&
669 swin = win + (sack -
ack);
694 if (ack == receiver->
td_end)
727 before(sack, receiver->
td_end + 1) ?
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)");
735 pr_debug(
"tcp_in_window: res=%u sender end=%u maxend=%u maxwin=%u "
736 "receiver end=%u maxend=%u maxwin=%u\n",
761 unsigned int dataoff,
768 unsigned int tcplen = skb->
len - dataoff;
772 th = skb_header_pointer(skb, dataoff,
sizeof(_tcph), &_tcph);
776 "nf_ct_tcp: short packet ");
781 if (th->doff*4 <
sizeof(
struct tcphdr) || tcplen < th->doff*4) {
784 "nf_ct_tcp: truncated/malformed packet ");
794 nf_checksum(skb, hooknum, dataoff,
IPPROTO_TCP, pf)) {
797 "nf_ct_tcp: bad TCP checksum ");
803 if (!tcp_valid_flags[tcpflags]) {
806 "nf_ct_tcp: invalid TCP flag combination ");
813 static unsigned int *tcp_get_timeouts(
struct net *net)
815 return tcp_pernet(net)->timeouts;
819 static int tcp_packet(
struct nf_conn *ct,
821 unsigned int dataoff,
824 unsigned int hooknum,
825 unsigned int *timeouts)
827 struct net *net = nf_ct_net(ct);
837 th = skb_header_pointer(skb, dataoff,
sizeof(_tcph), &_tcph);
840 spin_lock_bh(&ct->
lock);
843 index = get_conntrack_index(th);
844 new_state = tcp_conntracks[dir][
index][old_state];
870 spin_unlock_bh(&ct->
lock);
896 && ct->
proto.
tcp.last_index == TCP_SYN_SET
926 segment_seq_plus_len(
ntohl(th->
seq), skb->
len, dataoff, th);
950 spin_unlock_bh(&ct->
lock);
953 "nf_ct_tcp: invalid packet ignored in "
954 "state %s ", tcp_conntrack_names[old_state]);
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);
963 "nf_ct_tcp: invalid state ");
970 spin_unlock_bh(&ct->
lock);
973 "nf_ct_tcp: invalid RST ");
978 && ct->
proto.
tcp.last_index == TCP_SYN_SET)
999 if (!tcp_in_window(ct, &ct->
proto.
tcp, dir, index,
1000 skb, dataoff, th, pf)) {
1001 spin_unlock_bh(&ct->
lock);
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);
1017 if (old_state != new_state
1030 spin_unlock_bh(&ct->
lock);
1032 if (new_state != old_state)
1041 nf_ct_kill_acct(ct, ctinfo, skb);
1054 nf_ct_refresh_acct(ct, ctinfo, skb, timeout);
1060 static bool tcp_new(
struct nf_conn *ct,
const struct sk_buff *skb,
1061 unsigned int dataoff,
unsigned int *timeouts)
1066 struct net *net = nf_ct_net(ct);
1071 th = skb_header_pointer(skb, dataoff,
sizeof(_tcph), &_tcph);
1079 pr_debug(
"nf_ct_tcp: invalid new deleting.\n");
1090 if (ct->
proto.
tcp.seen[0].td_maxwin == 0)
1091 ct->
proto.
tcp.seen[0].td_maxwin = 1;
1110 if (ct->
proto.
tcp.seen[0].td_maxwin == 0)
1111 ct->
proto.
tcp.seen[0].td_maxwin = 1;
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",
1135 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1137 #include <linux/netfilter/nfnetlink.h>
1143 struct nlattr *nest_parms;
1146 spin_lock_bh(&ct->
lock);
1149 goto nla_put_failure;
1156 goto nla_put_failure;
1161 goto nla_put_failure;
1166 goto nla_put_failure;
1167 spin_unlock_bh(&ct->
lock);
1169 nla_nest_end(skb, nest_parms);
1174 spin_unlock_bh(&ct->
lock);
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]);
1211 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
1218 nla_data(tb[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
1232 spin_unlock_bh(&ct->
lock);
1237 static int tcp_nlattr_size(
void)
1239 return nla_total_size(0)
1243 static int tcp_nlattr_tuple_size(
void)
1249 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1251 #include <linux/netfilter/nfnetlink.h>
1254 static int tcp_timeout_nlattr_to_obj(
struct nlattr *tb[],
1255 struct net *net,
void *
data)
1257 unsigned int *timeouts =
data;
1267 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT]))*
HZ;
1271 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_RECV]))*
HZ;
1275 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_ESTABLISHED]))*
HZ;
1279 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_FIN_WAIT]))*
HZ;
1283 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE_WAIT]))*
HZ;
1287 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_LAST_ACK]))*
HZ;
1291 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_TIME_WAIT]))*
HZ;
1295 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_CLOSE]))*
HZ;
1299 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_SYN_SENT2]))*
HZ;
1303 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_RETRANS]))*
HZ;
1307 ntohl(nla_get_be32(tb[CTA_TIMEOUT_TCP_UNACK]))*
HZ;
1313 tcp_timeout_obj_to_nlattr(
struct sk_buff *skb,
const void *data)
1315 const unsigned int *timeouts =
data;
1317 if (nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT,
1319 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_RECV,
1321 nla_put_be32(skb, CTA_TIMEOUT_TCP_ESTABLISHED,
1323 nla_put_be32(skb, CTA_TIMEOUT_TCP_FIN_WAIT,
1325 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE_WAIT,
1327 nla_put_be32(skb, CTA_TIMEOUT_TCP_LAST_ACK,
1329 nla_put_be32(skb, CTA_TIMEOUT_TCP_TIME_WAIT,
1331 nla_put_be32(skb, CTA_TIMEOUT_TCP_CLOSE,
1333 nla_put_be32(skb, CTA_TIMEOUT_TCP_SYN_SENT2,
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;
1359 #ifdef CONFIG_SYSCTL
1360 static struct ctl_table tcp_sysctl_table[] = {
1362 .
procname =
"nf_conntrack_tcp_timeout_syn_sent",
1363 .maxlen =
sizeof(
unsigned int),
1368 .procname =
"nf_conntrack_tcp_timeout_syn_recv",
1369 .maxlen =
sizeof(
unsigned int),
1374 .procname =
"nf_conntrack_tcp_timeout_established",
1375 .maxlen =
sizeof(
unsigned int),
1380 .procname =
"nf_conntrack_tcp_timeout_fin_wait",
1381 .maxlen =
sizeof(
unsigned int),
1386 .procname =
"nf_conntrack_tcp_timeout_close_wait",
1387 .maxlen =
sizeof(
unsigned int),
1392 .procname =
"nf_conntrack_tcp_timeout_last_ack",
1393 .maxlen =
sizeof(
unsigned int),
1398 .procname =
"nf_conntrack_tcp_timeout_time_wait",
1399 .maxlen =
sizeof(
unsigned int),
1404 .procname =
"nf_conntrack_tcp_timeout_close",
1405 .maxlen =
sizeof(
unsigned int),
1410 .procname =
"nf_conntrack_tcp_timeout_max_retrans",
1411 .maxlen =
sizeof(
unsigned int),
1416 .procname =
"nf_conntrack_tcp_timeout_unacknowledged",
1417 .maxlen =
sizeof(
unsigned int),
1422 .procname =
"nf_conntrack_tcp_loose",
1423 .maxlen =
sizeof(
unsigned int),
1428 .procname =
"nf_conntrack_tcp_be_liberal",
1429 .maxlen =
sizeof(
unsigned int),
1434 .procname =
"nf_conntrack_tcp_max_retrans",
1435 .maxlen =
sizeof(
unsigned int),
1442 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1443 static struct ctl_table tcp_compat_sysctl_table[] = {
1445 .
procname =
"ip_conntrack_tcp_timeout_syn_sent",
1446 .maxlen =
sizeof(
unsigned int),
1451 .procname =
"ip_conntrack_tcp_timeout_syn_sent2",
1452 .maxlen =
sizeof(
unsigned int),
1457 .procname =
"ip_conntrack_tcp_timeout_syn_recv",
1458 .maxlen =
sizeof(
unsigned int),
1463 .procname =
"ip_conntrack_tcp_timeout_established",
1464 .maxlen =
sizeof(
unsigned int),
1469 .procname =
"ip_conntrack_tcp_timeout_fin_wait",
1470 .maxlen =
sizeof(
unsigned int),
1475 .procname =
"ip_conntrack_tcp_timeout_close_wait",
1476 .maxlen =
sizeof(
unsigned int),
1481 .procname =
"ip_conntrack_tcp_timeout_last_ack",
1482 .maxlen =
sizeof(
unsigned int),
1487 .procname =
"ip_conntrack_tcp_timeout_time_wait",
1488 .maxlen =
sizeof(
unsigned int),
1493 .procname =
"ip_conntrack_tcp_timeout_close",
1494 .maxlen =
sizeof(
unsigned int),
1499 .procname =
"ip_conntrack_tcp_timeout_max_retrans",
1500 .maxlen =
sizeof(
unsigned int),
1505 .procname =
"ip_conntrack_tcp_loose",
1506 .maxlen =
sizeof(
unsigned int),
1511 .procname =
"ip_conntrack_tcp_be_liberal",
1512 .maxlen =
sizeof(
unsigned int),
1517 .procname =
"ip_conntrack_tcp_max_retrans",
1518 .maxlen =
sizeof(
unsigned int),
1530 #ifdef CONFIG_SYSCTL
1534 pn->ctl_table =
kmemdup(tcp_sysctl_table,
1535 sizeof(tcp_sysctl_table),
1550 pn->ctl_table[10].data = &tn->
tcp_loose;
1557 static int tcp_kmemdup_compat_sysctl_table(
struct nf_proto_net *pn,
1560 #ifdef CONFIG_SYSCTL
1561 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
1562 pn->ctl_compat_table =
kmemdup(tcp_compat_sysctl_table,
1563 sizeof(tcp_compat_sysctl_table),
1565 if (!pn->ctl_compat_table)
1578 pn->ctl_compat_table[10].data = &tn->
tcp_loose;
1604 ret = tcp_kmemdup_compat_sysctl_table(pn, tn);
1608 ret = tcp_kmemdup_sysctl_table(pn, tn);
1610 nf_ct_kfree_compat_sysctl_table(pn);
1612 ret = tcp_kmemdup_sysctl_table(pn, tn);
1617 static struct nf_proto_net *tcp_get_net_proto(
struct net *net)
1619 return &net->ct.nf_ct_proto.tcp.pn;
1627 .pkt_to_tuple = tcp_pkt_to_tuple,
1628 .invert_tuple = tcp_invert_tuple,
1629 .print_tuple = tcp_print_tuple,
1630 .print_conntrack = tcp_print_conntrack,
1631 .packet = tcp_packet,
1632 .get_timeouts = tcp_get_timeouts,
1635 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1636 .to_nlattr = tcp_to_nlattr,
1637 .nlattr_size = tcp_nlattr_size,
1638 .from_nlattr = nlattr_to_tcp,
1641 .nlattr_tuple_size = tcp_nlattr_tuple_size,
1644 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1646 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1647 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1649 .obj_size =
sizeof(
unsigned int) *
1650 TCP_CONNTRACK_TIMEOUT_MAX,
1664 .pkt_to_tuple = tcp_pkt_to_tuple,
1665 .invert_tuple = tcp_invert_tuple,
1666 .print_tuple = tcp_print_tuple,
1667 .print_conntrack = tcp_print_conntrack,
1668 .packet = tcp_packet,
1669 .get_timeouts = tcp_get_timeouts,
1672 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1673 .to_nlattr = tcp_to_nlattr,
1674 .nlattr_size = tcp_nlattr_size,
1675 .from_nlattr = nlattr_to_tcp,
1678 .nlattr_tuple_size = tcp_nlattr_tuple_size,
1681 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
1683 .nlattr_to_obj = tcp_timeout_nlattr_to_obj,
1684 .obj_to_nlattr = tcp_timeout_obj_to_nlattr,
1686 .obj_size =
sizeof(
unsigned int) *
1687 TCP_CONNTRACK_TIMEOUT_MAX,