Skip to contentSkip to conversation

nf_conntrack_dns: Workaround parallel DNS resolve

RFC New Sebastian Poehn 1 participant 1 message Feb 26, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 nf_conntrack_dns: Workaround parallel DNS resolve

Some versions of glibc make use of parallel DNS lookup in case ipv6 is enabled on the machine. They send out one A and one AAAA query for a domain in short timeframes.
However they use the same port, resulting in same 4-way tuple. As the conntrack is NEW the second packet is dropped resulting in a significant delay on client side (5sec) due to retransmission.

Doing some major changes in kernel for the sake of one protocol does not seem to be a good solution. See reference:
http://www.spinics.net/lists/netfilter-devel/msg15860.html

What I tried in this workaround-fix is to get the desired functionality (AAAA query can pass) with as little change as possible. So I added a conntrack helper for DNS keeping track of A queries in union nf_conntrack_proto. If AAAA query matches, it's associated with the first conntrack and source port is mangled back from 1024 to original value.

Built and tested on 3.12.30. Honestly spoken I don't think that this should go into mainline - but it may be interesting for other people.

include/linux/netfilter/nf_conntrack_dns.h new file +25 -0
new file mode 100644
index 0000000..5c709d2
@@ -0,0 +1,25 @@
+#ifndef _SOP_NF_CONNTRACK_DNS_H
+#define _SOP_NF_CONNTRACK_DNS_H
+
+#define DNS_PORT 53
+#define	DNS_RECORD_TYPE				2
+#define	DNS_RECORD_CLASS			2
+#define	DNS_RECORD_TYPE_AND_CLASS		(DNS_RECORD_TYPE + DNS_RECORD_CLASS)
+#define	DNS_RECORD_MIN				(sizeof("A") + DNS_RECORD_TYPE_AND_CLASS)
+
+struct nf_ct_dns {
+	u8 usage;
+	char query[0];
+};
+
+struct dnshdr {
+	__be16 query_id;
+	__be16 flags;
+	__be16 question_count;
+	__be16 answer_count;
+	__be16 authority_count;
+	__be16 additional_record_count;
+	char query[0];
+};
+
+#endif /* _SOP_NF_CONNTRACK_DNS_H */
include/net/netfilter/nf_conntrack.h +3 -0
index a776541..afeba0a 100644
@@ -18,6 +18,7 @@
 #include <linux/compiler.h>
 #include <linux/atomic.h>
 
+#include <linux/netfilter/nf_conntrack_dns.h>
 #include <linux/netfilter/nf_conntrack_tcp.h>
 #include <linux/netfilter/nf_conntrack_dccp.h>
 #include <linux/netfilter/nf_conntrack_sctp.h>
@@ -33,6 +34,8 @@ union nf_conntrack_proto {
 	struct ip_ct_sctp sctp;
 	struct ip_ct_tcp tcp;
 	struct nf_ct_gre gre;
+//FIXME: Has to be changed! Will do in the very end as it break my build setup
+//	struct nf_ct_dns dns;
 };
 
 union nf_conntrack_expect_proto {
net/netfilter/Kconfig +10 -0
index 91077a6..e6fe611 100644
@@ -320,6 +320,16 @@ config NF_CONNTRACK_TFTP
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NF_CONNTRACK_DNS
+	tristate "DNS protocol support"
+	depends on NETFILTER_ADVANCED
+	help
+	  This is a workaround for dns resolvers sending out A and AAAA requests
+	  in a short timeframe. This will rewrite source port of the second request
+	  so we do not drop the packet due to NEW conntrack.
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 config NF_CT_NETLINK
 	tristate 'Connection tracking netlink interface'
 	select NETFILTER_NETLINK
net/netfilter/Makefile +2 -0
index 4002bb5..cc6edb9 100644
@@ -44,6 +44,8 @@ obj-$(CONFIG_NF_CONNTRACK_SANE) += nf_conntrack_sane.o
 obj-$(CONFIG_NF_CONNTRACK_SIP) += nf_conntrack_sip.o
 obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
 
+obj-$(CONFIG_NF_CONNTRACK_DNS) += nf_conntrack_dns.o
+
 nf_nat-y	:= nf_nat_core.o nf_nat_proto_unknown.o nf_nat_proto_common.o \
 		   nf_nat_proto_udp.o nf_nat_proto_tcp.o nf_nat_helper.o
 
net/netfilter/nf_conntrack_dns.c new file +268 -0

268 lines not on this page · raw message