Skip to contentSkip to conversation

TCPFLAG <New>

DISCUSSION Craig Shelley 3 participants 3 messages Jun 29, 2003 – Jun 30, 2003 netfilter

lore
Share & tools
lore
#0 lore raw

Hi All,
About a year ago, I made an iptables module/library to manipulate the
flags on TCP packets. Well... I have tested it on my server, and it has been running without any errors for nearly a year. I have enclosed the source code (sorry no docs). There is a little script in there to patch the kernel source, and make TCPFLAG appear in menuconfig.

I use this module to open connections through a packet filter firewall that is unfortunately between two of my boxes. The firewall drops all inbound packets with the SYN flag set, so I use TCPFLAG masquerade SYN packets as RST packets. (A bit crude, but it does the job!) At the receiving end, I convert the RST packets that I sent from my host back to SYN packets.

The target manipulates the flags using two parameters. The first parameter being the mask (the flags that will be modified), and the second parameter is the flags in the mask that will be turned on. This method provides flexibility,
and is similar to the syntax of --tcp-flags

Here is an example of what I run on the machine outside the firewall...

iptables -t mangle -A POSTROUTING -d 123.123.123.123 -p tcp --tcp-flags FIN,SYN,RST,ACK SYN -j TCPFLAG --mask SYN,RST --comp RST

And here's what I run on the machine behind the firewall...

iptables -t mangle -A PREROUTING -s 195.137.47.43 -p tcp --tcp-flags FIN,SYN,RST,ACK RST -j TCPFLAG --mask SYN,RST --comp SYN

This module probably has some other uses, but I haven't thought of any. The only bug I know of is that it doesn't handle connection tracking at all :( so one of the machines (I can't remember which) does not see the connection being made. (nor does the firewall for that matter) Since I am only using this code to do a temporary job, I don't anticipate maintaining it. This is the reason I have not submitted it before.

Well.... Enjoy, Let me know what you think
I am not on this list, so please CC me

Signature · 1 line
Craig Shelley <[email protected]>
#1 lore raw

Hi Craig,

I took privilege to make a propoper CVS patch for the netfilter tree out of the tarball you sent:

 patch-o-matic/extra/ipt_TCPFLAG.patch                |  137 ++++++++++++
 patch-o-matic/extra/ipt_TCPFLAG.patch.config.in      |    2
 patch-o-matic/extra/ipt_TCPFLAG.patch.configure.help |    7
 patch-o-matic/extra/ipt_TCPFLAG.patch.help           |   38 +++
 patch-o-matic/extra/ipt_TCPFLAG.patch.makefile       |    2
 userspace/extensions/.ipt_TCPFLAG-test               |    3
 userspace/extensions/libipt_TCPFLAG.c                |  212 +++++++++++++++++++

Craig, please look into the patch, correct ipt_TCPFLAG.patch.help and ipt_TCPFLAG.patch.configure.help.

This is the best way of sending patches.

Note that this email does not express my judgement nor opinion about this patch. Here goes:

patch-o-matic/extra/ipt_TCPFLAG.patch +137 -0
diff -Nru netfilter.orig/patch-o-matic/extra/ipt_TCPFLAG.patch netfilter/patch-o-matic/extra/ipt_TCPFLAG.patch
@@ -0,0 +1,137 @@
+diff -Nru linux-2.4.21.orig/include/linux/netfilter_ipv4/ipt_TCPFLAG.h linux-2.4.21/include/linux/netfilter_ipv4/ipt_TCPFLAG.h
+--- linux-2.4.21.orig/include/linux/netfilter_ipv4/ipt_TCPFLAG.h	1970-01-01 01:00:00.000000000 +0100
++++ linux-2.4.21/include/linux/netfilter_ipv4/ipt_TCPFLAG.h	2002-11-01 11:01:49.000000000 +0100
+@@ -0,0 +1,17 @@
++/*
++ * TCP FLAG modification (Craig Shelley)
++ */
++
++#define TCPFLAG_FIN 1
++#define TCPFLAG_SYN 2
++#define TCPFLAG_RST 4
++#define TCPFLAG_PSH 8
++#define TCPFLAG_ACK 16
++#define TCPFLAG_URG 32
++#define TCPFLAG_ALL 63
++
++struct ipt_TCPFLAG_info
++{
++        u_int8_t mask;
++	u_int8_t comp;
++};
+diff -Nru linux-2.4.21.orig/net/ipv4/netfilter/ipt_TCPFLAG.c linux-2.4.21/net/ipv4/netfilter/ipt_TCPFLAG.c
+--- linux-2.4.21.orig/net/ipv4/netfilter/ipt_TCPFLAG.c	1970-01-01 01:00:00.000000000 +0100
++++ linux-2.4.21/net/ipv4/netfilter/ipt_TCPFLAG.c	2003-05-02 21:05:54.000000000 +0200
+@@ -0,0 +1,112 @@
++/*
++ * This is a module which is used to modify the TCP flags of packets.
++ * by Craig Shelley ([email protected])
++ * based on TTL modification module by Harald Welte
++ * To be distributed under the terms of the GNU GPL
++ */
++
++#include <linux/config.h>
++#include <linux/module.h>
++#include <linux/skbuff.h>
++#include <linux/ip.h>
++#include <net/icmp.h>
++#include <net/ip.h>
++#include <net/tcp.h>
++struct in_device;
++#include <net/route.h>
++#include <linux/netfilter_ipv4/ip_tables.h>
++#include <linux/netfilter_ipv4/ipt_TCPFLAG.h>
++
++MODULE_AUTHOR("Craig Shelley <[email protected]>");
++MODULE_DESCRIPTION("IP tables TCPFLAG modification module");
++MODULE_LICENSE("GPL");
++
++static unsigned int ipt_tcpflag_target(struct sk_buff **pskb, unsigned int hooknum,
++		const struct net_device *in, const struct net_device *out,
++		const void *targinfo, void *userinfo)
++{
++	const struct ipt_TCPFLAG_info *info = targinfo;
++	struct iphdr *iph;
++	struct tcphdr *tcph;
++	void *th;
++
++	iph = (*pskb)->nh.iph;
++	tcph = (struct tcphdr *)((u_int32_t*)(*pskb)->nh.iph + (*pskb)->nh.iph->ihl);
++	th = (u_int32_t *)iph + iph->ihl;
++
++	/*Modify the TCP FLAGS to the masks and comps*/
++
++
++	if (info->mask & TCPFLAG_FIN) tcph->fin = ((info->comp & TCPFLAG_FIN) == TCPFLAG_FIN);
++	if (info->mask & TCPFLAG_SYN) tcph->syn = ((info->comp & TCPFLAG_SYN) == TCPFLAG_SYN);
++	if (info->mask & TCPFLAG_RST) tcph->rst = ((info->comp & TCPFLAG_RST) == TCPFLAG_RST);
++	if (info->mask & TCPFLAG_PSH) tcph->psh = ((info->comp & TCPFLAG_PSH) == TCPFLAG_PSH);
++	if (info->mask & TCPFLAG_ACK) tcph->ack = ((info->comp & TCPFLAG_ACK) == TCPFLAG_ACK);
++	if (info->mask & TCPFLAG_URG) tcph->urg = ((info->comp & TCPFLAG_URG) == TCPFLAG_URG);
++
++
++	/*Produce a new TCP checksum*/
++
++	tcph->check = 0;
++	tcph->check = tcp_v4_check(
++		th,
++		(*pskb)->len - 4*iph->ihl,
++		iph->saddr,
++		iph->daddr,
++		csum_partial(
++			(char *)th,
++			(*pskb)->len - 4*iph->ihl,
++			0));
++
++
++	return IPT_CONTINUE;
++
++}
++
++
++static int ipt_tcpflag_checkentry(const char *tablename,
++				const struct ipt_entry *e,
++				void *targinfo,
++				unsigned int targinfosize,
++				unsigned int hook_mask)
++{
++	struct ipt_TCPFLAG_info *info = targinfo;
++
++	if (strcmp(tablename, "mangle"))
++	{
++		printk(KERN_WARNING "TCPFLAG: can only be called from \"mangle\" table, not \"%s\"\n", tablename);
++		return 0;
++	}
++
++	if (info->mask == 0)
++	{
++		printk(KERN_WARNING "TCPFLAG: NULL mask not allowed\n");
++		return 0;
++	}
++
++	if ( (~ info->mask) & info->comp)
++	{
++		printk(KERN_WARNING "TCPFLAG: Unmasked flags not allowed\n");
++		return 0;
++	}
++
++
++	return 1;
++}
++
++static struct ipt_target ipt_TCPFLAG = { { NULL, NULL }, "TCPFLAG",
++	ipt_tcpflag_target, ipt_tcpflag_checkentry, NULL, THIS_MODULE };
++
++static int __init init(void)
++{
++	return ipt_register_target(&ipt_TCPFLAG);
++}
++
++static void __exit fini(void)
++{
++	ipt_unregister_target(&ipt_TCPFLAG);
++}
++
++module_init(init);
++module_exit(fini);
++
patch-o-matic/extra/ipt_TCPFLAG.patch.config.in +2 -0
diff -Nru netfilter.orig/patch-o-matic/extra/ipt_TCPFLAG.patch.config.in netfilter/patch-o-matic/extra/ipt_TCPFLAG.patch.config.in
@@ -0,0 +1,2 @@
+  dep_tristate '  LOG target support' CONFIG_IP_NF_TARGET_LOG $CONFIG_IP_NF_IPTABLES
+  dep_tristate '  TCPFLAG target support' CONFIG_IP_NF_TARGET_TCPFLAG $CONFIG_IP_NF_IPTABLES
patch-o-matic/extra/ipt_TCPFLAG.patch.configure.help +7 -0
diff -Nru netfilter.orig/patch-o-matic/extra/ipt_TCPFLAG.patch.configure.help netfilter/patch-o-matic/extra/ipt_TCPFLAG.patch.configure.help
@@ -0,0 +1,7 @@
+CONFIG_IP_NF_TARGET_LOG
+TCPFLAG target support
+CONFIG_IP_NF_TARGET_TCPFLAG
+  This option adds a `TCPFLAG' target.
+
+  If you want to compile it as a module, say M here and read
+  Documentation/modules.txt.  If unsure, say `N'.
patch-o-matic/extra/ipt_TCPFLAG.patch.makefile +2 -0
diff -Nru netfilter.orig/patch-o-matic/extra/ipt_TCPFLAG.patch.makefile netfilter/patch-o-matic/extra/ipt_TCPFLAG.patch.makefile
@@ -0,0 +1,2 @@
+obj-$(CONFIG_IP_NF_TARGET_LOG) += ipt_LOG.o
+obj-$(CONFIG_IP_NF_TARGET_TCPFLAG) += ipt_TCPFLAG.o
patch-o-matic/extra/ipt_TCPFLAG.patch.help +38 -0
diff -Nru netfilter.orig/patch-o-matic/extra/ipt_TCPFLAG.patch.help netfilter/patch-o-matic/extra/ipt_TCPFLAG.patch.help
@@ -0,0 +1,38 @@
+Author: Craig Shelley <[email protected]>
+Status: Working, has a bug noted below.
+
+This adds CONFIG_IP_NF_TARGET_TCPFLAG option.
+
+Quote from the author:
+I use this module to open connections through a packet filter firewall
+that is unfortunately between two of my boxes. The firewall drops all
+inbound packets with the SYN flag set, so I use TCPFLAG masquerade SYN
+packets as RST packets. (A bit crude, but it does the job!)
+At the receiving end, I convert the RST packets that I sent from my host
+back to SYN packets.
+
+The target manipulates the flags using two parameters. The first
+parameter being the mask (the flags that will be modified), and the
+second parameter is the flags in the mask that will be turned on.
+This method provides flexibility,
+and is similar to the syntax of --tcp-flags
+
+Here is an example of what I run on the machine outside the firewall...
+
+iptables -t mangle -A POSTROUTING -d 123.123.123.123 -p tcp --tcp-flags
+FIN,SYN,RST,ACK SYN -j TCPFLAG --mask SYN,RST --comp RST
+
+And here's what I run on the machine behind the firewall...
+
+iptables -t mangle -A PREROUTING -s 195.137.47.43 -p tcp --tcp-flags
+FIN,SYN,RST,ACK RST -j TCPFLAG --mask SYN,RST --comp SYN
+
+This module probably has some other uses, but I haven't thought of any.
+
+The only bug I know of is that it doesn't handle connection tracking at
+all :( so one of the machines (I can't remember which) does not see the
+connection being made.  (nor does the firewall for that matter)
+Since I am only using this code to do a temporary job, I don't
+anticipate maintaining it. This is the reason I have not submitted it
+before.
+
userspace/extensions/.ipt_TCPFLAG-test +3 -0
diff -Nru netfilter.orig/userspace/extensions/.ipt_TCPFLAG-test netfilter/userspace/extensions/.ipt_TCPFLAG-test
@@ -0,0 +1,3 @@
+#!/bin/sh
+# True if TCPFLAG is applied.
+[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_TCPFLAG.h ] && echo TCPFLAG
userspace/extensions/libipt_TCPFLAG.c +212 -0

212 lines not on this page · raw message

#2 lore raw
In reply to Craig Shelley
Quoted 7 lines · Craig Shelley

Hi All,
About a year ago, I made an iptables module/library to manipulate the
flags on TCP packets. Well... I have tested it on my server, and it has been running without any errors for nearly a year. I have enclosed the source code (sorry no docs). There is a little script in there to patch the kernel source, and make TCPFLAG appear in menuconfig.

Quoted 6 lines

I use this module to open connections through a packet filter firewall that is unfortunately between two of my boxes. The firewall drops all inbound packets with the SYN flag set, so I use TCPFLAG masquerade SYN packets as RST packets. (A bit crude, but it does the job!) At the receiving end, I convert the RST packets that I sent from my host back to SYN packets.

Well, I regard this as an interesting 'hack', which may solve your particular problem. However, I don't think that it does something 'desired' that lots of users would like to use.

Quoted 3 lines

Since I am only using this code to do a temporary job, I don't anticipate maintaining it. This is the reason I have not submitted it before.

Since you don't want to maintain it, and the netfiltere coreteam doesn't have any particular interest in adding such functionality, I will not put it into the patch-o-matic system.

For anybody who disagrees: Please feel free to convince me of the opposite :)

Quoted 1 line

Craig Shelley <[email protected]>

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie