Introduce flag facilities to take over TCP connections
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/3 Introduce flag facilities to take over TCP connections
Hi Patrick,
I reworked the previous patch based on some of your suggestions. Let me know what you think.
Description:
This patch introduces two new flags called IPS_PICKUP that forces the protocol handler to pick up the window of valid TCP packets and IPS_IN_WINDOW to by pass window checkings.
Moreover, four new attributes to inject the window scale factor and internal TCP flags handling. These new facilities provide the appropiate mechanisms to take over TCP connections in failover settings with TCP tracking enabled.
Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
[CONNTRACK] Introduce flag facilities to take over TCP connections
This patch introduces two new flags called IPS_PICKUP that forces the protocol handler to pick up the window of valid TCP packets and IPS_IN_WINDOW to by pass window checkings.
Moreover, four new attributes to inject the window scale factor and internal TCP flags handling. These new facilities provide the appropiate mechanisms to take over TCP connections in failover settings with TCP tracking enabled.
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +74 -16
@@ -345,8 +345,25 @@ static int tcp_to_nfattr(struct sk_buff nest_parms =NFA_NEST (skb,CTA_PROTOINFO_TCP );NFA_PUT (skb,CTA_PROTOINFO_TCP_STATE ,sizeof (u_int8_t), &ct->proto.tcp.state); -read_unlock_bh (&tcp_lock); +/* skip window scale and flags dump if hard tracking is by passed */ +if (test_bit (IPS_IN_WINDOW , &ct->status)) + goto out; + +NFA_PUT (skb,CTA_PROTOINFO_TCP_WSCALE_ORIGINAL ,sizeof (u_int8_t), + &ct->proto.tcp.seen[0 ].td_scale); + +NFA_PUT (skb,CTA_PROTOINFO_TCP_WSCALE_REPLY ,sizeof (u_int8_t), + &ct->proto.tcp.seen[1 ].td_scale); + +NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_ORIGINAL ,sizeof (u_int8_t), + &ct->proto.tcp.seen[0 ].flags); + +NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_REPLY ,sizeof (u_int8_t), + &ct->proto.tcp.seen[1 ].flags); + +out: +read_unlock_bh (&tcp_lock);NFA_NEST_END (skb, nest_parms);return 0 ;
@@ -357,7 +374,11 @@ nfattr_failure: }static const size_t cta_min_tcp[CTA_PROTOINFO_TCP_MAX ] = { - [CTA_PROTOINFO_TCP_STATE -1 ] =sizeof (u_int8_t), + [CTA_PROTOINFO_TCP_STATE -1 ] =sizeof (u_int8_t), + [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL -1 ] =sizeof (u_int8_t), + [CTA_PROTOINFO_TCP_WSCALE_REPLY -1 ] =sizeof (u_int8_t), + [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL -1 ] =sizeof (struct nf_ct_tcp_flags ), + [CTA_PROTOINFO_TCP_FLAGS_REPLY -1 ] =sizeof (struct nf_ct_tcp_flags ) };static int nfattr_to_tcp (struct nfattr *cda[],struct ip_conntrack *ct)
@@ -381,6 +402,30 @@ static int nfattr_to_tcp(struct nfattr *write_lock_bh (&tcp_lock); ct->proto.tcp.state = *(u_int8_t *)NFA_DATA (tb[CTA_PROTOINFO_TCP_STATE -1 ]); + +if (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL -1 ]) { +struct nf_ct_tcp_flags *attr = +NFA_DATA (tb[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL -1 ]); + ct->proto.tcp.seen[0 ].flags &= ~attr->mask; + ct->proto.tcp.seen[0 ].flags |= attr->flags & attr->mask; + } + +if (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY -1 ]) { +struct nf_ct_tcp_flags *attr = +NFA_DATA (tb[CTA_PROTOINFO_TCP_FLAGS_REPLY -1 ]); + ct->proto.tcp.seen[1 ].flags &= ~attr->mask; + ct->proto.tcp.seen[1 ].flags |= attr->flags & attr->mask; + } + +if (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL -1 ] && + tb[CTA_PROTOINFO_TCP_WSCALE_REPLY -1 ] && + ct->proto.tcp.seen[0 ].flags &IP_CT_TCP_FLAG_WINDOW_SCALE && + ct->proto.tcp.seen[1 ].flags &IP_CT_TCP_FLAG_WINDOW_SCALE ) { + ct->proto.tcp.seen[0 ].td_scale = *(u_int8_t *) +NFA_DATA (tb[CTA_PROTOINFO_TCP_WSCALE_ORIGINAL -1 ]); + ct->proto.tcp.seen[1 ].td_scale = *(u_int8_t *) +NFA_DATA (tb[CTA_PROTOINFO_TCP_WSCALE_REPLY -1 ]); + }write_unlock_bh (&tcp_lock);return 0 ;
@@ -424,10 +469,10 @@ static unsigned int get_conntrack_index(we doesn't have to deal with fragments. */ -static inline __u32 segment_seq_plus_len (__u32 seq, -size_t len, -struct iphdr *iph, -struct tcphdr *tcph) +static inline __u32 segment_seq_plus_len (const __u32 seq, +const size_t len, +const struct iphdr *iph, +const struct tcphdr *tcph) {return (seq + len - (iph->ihl + tcph->doff)*4 + (tcph->syn ?1 :0 ) + (tcph->fin ?1 :0 ));
@@ -890,6 +935,22 @@ static int tcp_error(struct sk_buff *skbreturn NF_ACCEPT ; } +static inline void tcp_pickup_window (struct ip_conntrack *conntrack, +const struct sk_buff *skb, +const struct iphdr *iph, +const struct tcphdr *th) +{ + conntrack->proto.tcp.seen[0 ].td_end = +segment_seq_plus_len (ntohl (th->seq), skb->len, iph, th); + conntrack->proto.tcp.seen[0 ].td_maxwin =ntohs (th->window); +if (conntrack->proto.tcp.seen[0 ].td_maxwin ==0 ) + conntrack->proto.tcp.seen[0 ].td_maxwin =1 ; + conntrack->proto.tcp.seen[0 ].td_maxend = + conntrack->proto.tcp.seen[0 ].td_end + + conntrack->proto.tcp.seen[0 ].td_maxwin; + conntrack->proto.tcp.seen[0 ].td_scale =0 ; +} +/* Returns verdict for packet, or -1 for invalid. */ static int tcp_packet(struct ip_conntrack *conntrack, const struct sk_buff *skb,
@@ -912,6 +973,12 @@ static int tcp_packet(struct ip_conntrac index =get_conntrack_index (th); new_state = tcp_conntracks[dir][index][old_state]; +/* pick up or by-pass window tracking */ +if (test_bit (IPS_IN_WINDOW , &conntrack->status)) + goto in_window; +else if (test_and_clear_bit (IPS_PICKUP , &conntrack->status)) +tcp_pickup_window (conntrack, skb, iph, th); +switch (new_state) {case TCP_CONNTRACK_IGNORE :/* Ignored packets:
@@ -1116,16 +1183,7 @@ static int tcp_new(struct ip_conntrack ** its history is lost for us. * Let's try to use the data from the packet. */ - conntrack->proto.tcp.seen[0 ].td_end = -segment_seq_plus_len (ntohl (th->seq), skb->len, - iph, th); - conntrack->proto.tcp.seen[0 ].td_maxwin =ntohs (th->window); -if (conntrack->proto.tcp.seen[0 ].td_maxwin ==0 ) - conntrack->proto.tcp.seen[0 ].td_maxwin =1 ; - conntrack->proto.tcp.seen[0 ].td_maxend = - conntrack->proto.tcp.seen[0 ].td_end + - conntrack->proto.tcp.seen[0 ].td_maxwin; - conntrack->proto.tcp.seen[0 ].td_scale =0 ; +tcp_pickup_window (conntrack, skb, iph, th);/* We assume SACK. Should we assume window scaling too? */ conntrack->proto.tcp.seen[0].flags =
include/linux/netfilter/nf_conntrack_common.h +8 -0
@@ -73,6 +73,14 @@ enum ip_conntrack_status {/* Connection has fixed timeout. */ IPS_FIXED_TIMEOUT_BIT =10 ,IPS_FIXED_TIMEOUT = (1 <<IPS_FIXED_TIMEOUT_BIT ), + +/* Pick up connection information. */ +IPS_PICKUP_BIT =11 , +IPS_PICKUP = (1 <<IPS_PICKUP_BIT ), + +/* Ignore in-window checkings. */ +IPS_IN_WINDOW_BIT =12 , + IPS_IN_WINDOW = (1 << IPS_IN_WINDOW_BIT), };/* Connection tracking event bits */
net/netfilter/nf_conntrack_proto_tcp.c +74 -16
133 lines not on this page · raw message
include/linux/netfilter/nfnetlink_conntrack.h +4 -0
@@ -83,6 +83,10 @@ enum ctattr_protoinfo {enum ctattr_protoinfo_tcp {CTA_PROTOINFO_TCP_UNSPEC ,CTA_PROTOINFO_TCP_STATE , +CTA_PROTOINFO_TCP_WSCALE_ORIGINAL , +CTA_PROTOINFO_TCP_WSCALE_REPLY , +CTA_PROTOINFO_TCP_FLAGS_ORIGINAL , +CTA_PROTOINFO_TCP_FLAGS_REPLY , __CTA_PROTOINFO_TCP_MAX };#define CTA_PROTOINFO_TCP_MAX (__CTA_PROTOINFO_TCP_MAX - 1)
include/linux/netfilter/nf_conntrack_tcp.h +5 -0
@@ -27,6 +27,11 @@ enum tcp_conntrack {/* This sender sent FIN first */ #define IP_CT_TCP_FLAG_CLOSE_INIT 0x03 +struct nf_ct_tcp_flags { +u_int8_t flags; +u_int8_t mask; +}; +#ifdef __KERNEL__ struct ip_ct_tcp_state {
Quoted 4 lines · Pablo Neira Ayuso
Hi Patrick,
I reworked the previous patch based on some of your suggestions. Let me know what you think.
We're almost there :)
net/ipv4/netfilter/ip_conntrack_proto_tcp.c static int tcp_to_nfattr(struct sk_buff · line 349
Quoted 1 line
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +1
-0+/* skip window scale and flags dump if hard tracking is by passed */
minor nitpick: bypassed
net/ipv4/netfilter/ip_conntrack_proto_tcp.c static int tcp_to_nfattr(struct sk_buff · line 363
Quoted 5 lines
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +5
-0+NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_ORIGINAL ,sizeof (u_int8_t), + &ct->proto.tcp.seen[0 ].flags); + +NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_REPLY ,sizeof (u_int8_t), + &ct->proto.tcp.seen[1 ].flags);
The attributes should contain the same data type in both directions, the receive side expects a structure.
Pablo Neira Ayuso
maintainerQuoted 11 lines · Patrick McHardy
Quoted 4 lines · Pablo Neira Ayuso
Hi Patrick,
I reworked the previous patch based on some of your suggestions. Let me know what you think.
We're almost there :)
net/ipv4/netfilter/ip_conntrack_proto_tcp.c static int tcp_to_nfattr(struct sk_buff · line 349
Quoted 1 line
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +1
-0+/* skip window scale and flags dump if hard tracking is by passed */ minor nitpick: bypassed
OK ;)
Quoted 8 lines
net/ipv4/netfilter/ip_conntrack_proto_tcp.c static int tcp_to_nfattr(struct sk_buff · line 363
Quoted 5 lines
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +5
-0+NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_ORIGINAL ,sizeof (u_int8_t), + &ct->proto.tcp.seen[0 ].flags); + +NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_REPLY ,sizeof (u_int8_t), + &ct->proto.tcp.seen[1 ].flags);The attributes should contain the same data type in both directions, the receive side expects a structure.
how could the mask field make sense for the dumping?
Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
Quoted 12 lines · Pablo Neira Ayuso
Quoted 8 lines · Patrick McHardy
net/ipv4/netfilter/ip_conntrack_proto_tcp.c static int tcp_to_nfattr(struct sk_buff · line 363
Quoted 5 lines
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +5
-0+NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_ORIGINAL ,sizeof (u_int8_t), + &ct->proto.tcp.seen[0 ].flags); + +NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_REPLY ,sizeof (u_int8_t), + &ct->proto.tcp.seen[1 ].flags);The attributes should contain the same data type in both directions, the receive side expects a structure.
Quoted 1 line
how could the mask field make sense for the dumping?
It doesn't really make sense, but an attribute encapsulates one type of information and should always use the same representation. If this is sent over the wire to another host for example it would need to distinguish whether it contains the kernel->user representation or the user->kernel representation, which IMO is bad design.
Pablo Neira Ayuso
maintainerQuoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso
Quoted 7 lines · Patrick McHardy
net/ipv4/netfilter/ip_conntrack_proto_tcp.c static int tcp_to_nfattr(struct sk_buff · line 363
Quoted 5 lines
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +5
-0+NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_ORIGINAL ,sizeof (u_int8_t), + &ct->proto.tcp.seen[0 ].flags); + +NFA_PUT (skb,CTA_PROTOINFO_TCP_FLAGS_REPLY ,sizeof (u_int8_t), + &ct->proto.tcp.seen[1 ].flags);The attributes should contain the same data type in both directions, the receive side expects a structure.
Quoted 8 lines
Quoted 1 line
how could the mask field make sense for the dumping?
It doesn't really make sense, but an attribute encapsulates one type of information and should always use the same representation. If this is sent over the wire to another host for example it would need to distinguish whether it contains the
kernel->userrepresentation or theuser->kernelrepresentation, which IMO is bad design.
Totally right, I'll rework this minor issue and get back to you.
Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris