Skip to contentSkip to conversation

conntrack match issues

PATCH Henrik Nordstrom 1 participant 1 message Jul 1, 2002 netfilter

lore
Share & tools
lore
#0 lore raw

I have some small issues with the conntrack match in it's current incarnation

a) Very hard to extend with new features as there is no extra flag bits

b) Missing the ability to match conntrack ports.

c) Some errors in iptables-save output

Attached to this message is an extension patch to the conntrack match to

a) Be able to match conntrack source/destination ports.

b) The flags words extended to 32 bits to make room for the new options and leave some allowing for future extensions such as the ability to match original ICMP, new conntrack state bits, master connection etc without having to totally break binary compability.

c) Corrected iptables-save output

Attached is an incremental diff to the current CVS version.

Note: Due to the extension and addition of new fields in the match structure it is not binary compatible with the current "pending" version.

Regards
Henrik Norström
MARA Systems AB

? extensions/.BALANCE-test
? extensions/.CONFIRMED-test
? extensions/.CONNMARK-test
? extensions/.FTOS-test
? extensions/.IPV4OPTSSTRIP-test
? extensions/.NETLINK.test
? extensions/.NETMAP-test
? extensions/.REJECT-test6
? extensions/.ROUTE-test
? extensions/.ah-test6
? extensions/.confirmed-test
? extensions/.connmark-test
? extensions/.conntrack2-test
? extensions/.esp-test6
? extensions/.frag-test6
? extensions/.helper-test
? extensions/.ipv4options-test
? extensions/.ipv6header-test6
? extensions/.mport-test
? extensions/.nth-test
? extensions/.opts-test6
? extensions/.pool-test
? extensions/.psd-test
? extensions/.quota-test
? extensions/.random-test
? extensions/.realm-test
? extensions/.recent-test
? extensions/.record-rpc-test
? extensions/.rt-test6
? extensions/.string-test
? extensions/.time-test
? patch-o-matic/pending/.conntrack.patch.swp
Index: extensions/libipt_conntrack.c
===================================================================
RCS file: /cvspublic/netfilter/userspace/extensions/libipt_conntrack.c,v retrieving revision 1.4

libipt_conntrack.c +134 -7
diff -u -r1.4 libipt_conntrack.c
@@ -30,6 +30,14 @@
 "				Reply source specification\n"
 "     --ctrepldst  [!] address[/mask]\n"
 "				Reply destination specification\n"
+"     --ctorigsrcport [!] port[:port]\n"
+"				Original source port specification\n"
+"     --ctorigdstport [!] port[:port]\n"
+"				Original destination port specification\n"
+"     --ctreplsrcport [!] port[:port]\n"
+"				Reply source port specification\n"
+"     --ctrepldstport [!] port[:port]\n"
+"				Reply destination port specification\n"
 " [!] --ctstatus [NONE|EXPECTED|SEEN_REPLY|ASSURED][,...]\n"
 "				Status(es) to match\n"
 " [!] --ctexpire time[:time]	Match remaining lifetime in seconds against\n"
@@ -48,6 +56,10 @@
 	{ "ctrepldst", 1, 0, '6' },
 	{ "ctstatus", 1, 0, '7' },
 	{ "ctexpire", 1, 0, '8' },
+	{ "ctorigsrcport", 1, 0, '9' },
+	{ "ctorigdstport", 1, 0, 'a' },
+	{ "ctreplsrcport", 1, 0, 'b' },
+	{ "ctrepldstport", 1, 0, 'c' },
 	{0}
 };
 
@@ -163,6 +175,54 @@
 	
 }
 
+
+static int
+service_to_port(const char *name)
+{
+	struct servent *service;
+
+	if ((service = getservbyname(name, "tcp")) != NULL)
+		return ntohs((unsigned short) service->s_port);
+
+	if ((service = getservbyname(name, "udp")) != NULL)
+		return ntohs((unsigned short) service->s_port);
+
+	return -1;
+}
+
+static u_int16_t
+parse_port(const char *port)
+{
+	unsigned int portnum;
+
+	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
+	    (portnum = service_to_port(port)) != -1)
+		return (u_int16_t)portnum;
+
+	exit_error(PARAMETER_PROBLEM,
+		   "invalid port/service `%s' specified", port);
+}
+
+static void
+parse_ports(const char *portstring, u_int16_t *ports)
+{
+	char *buffer;
+	char *cp;
+
+	buffer = strdup(portstring);
+	if ((cp = strchr(buffer, ':')) == NULL)
+		ports[0] = ports[1] = parse_port(buffer);
+	else {
+		*cp = '\0';
+		cp++;
+
+		ports[0] = buffer[0] ? parse_port(buffer) : 0;
+		ports[1] = cp[0] ? parse_port(cp) : 0xFFFF;
+	}
+	free(buffer);
+}
+
+
 /* Function which parses command options; returns true if it
    ate an option */
 static int
@@ -309,6 +369,46 @@
 		sinfo->flags |= IPT_CONNTRACK_EXPIRES;
 		break;
 
+	case '9':
+		check_inverse(optarg, &invert, &optind, 0);
+
+		parse_ports(argv[optind-1], sinfo->srcports[IP_CT_DIR_ORIGINAL]);
+		if (invert) {
+			sinfo->invflags |= IPT_CONNTRACK_ORIGSRCPORT;
+		}
+		sinfo->flags |= IPT_CONNTRACK_ORIGSRCPORT;
+		break;
+
+	case 'a':
+		check_inverse(optarg, &invert, &optind, 0);
+
+		parse_ports(argv[optind-1], sinfo->dstports[IP_CT_DIR_ORIGINAL]);
+		if (invert) {
+			sinfo->invflags |= IPT_CONNTRACK_ORIGDSTPORT;
+		}
+		sinfo->flags |= IPT_CONNTRACK_ORIGDSTPORT;
+		break;
+
+	case 'b':
+		check_inverse(optarg, &invert, &optind, 0);
+
+		parse_ports(argv[optind-1], sinfo->srcports[IP_CT_DIR_REPLY]);
+		if (invert) {
+			sinfo->invflags |= IPT_CONNTRACK_REPLSRCPORT;
+		}
+		sinfo->flags |= IPT_CONNTRACK_REPLSRCPORT;
+		break;
+
+	case 'c':
+		check_inverse(optarg, &invert, &optind, 0);
+
+		parse_ports(argv[optind-1], sinfo->dstports[IP_CT_DIR_REPLY]);
+		if (invert) {
+			sinfo->invflags |= IPT_CONNTRACK_REPLDSTPORT;
+		}
+		sinfo->flags |= IPT_CONNTRACK_REPLDSTPORT;
+		break;
+
 	default:
 		return 0;
 	}
@@ -386,7 +486,7 @@
 	char buf[BUFSIZ];
 
         if (inv)
-               	fputc('!', stdout);
+               	printf("! ");
 
 	if (mask->s_addr == 0L && !numeric)
 		printf("%s ", "anywhere");
@@ -409,7 +509,7 @@
 	if(sinfo->flags & IPT_CONNTRACK_STATE) {
 		printf("%sctstate ", optpfx);
         	if (sinfo->invflags & IPT_CONNTRACK_STATE)
-                	fputc('!', stdout);
+			printf("! ");
 		print_state(sinfo->statemask);
 	}
 
@@ -434,7 +534,7 @@
 	}
 
 	if(sinfo->flags & IPT_CONNTRACK_REPLSRC) {
-		printf("%sctorigsrc ", optpfx);
+		printf("%sctreplsrc ", optpfx);
 
 		print_addr(
 		    (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].src.ip,
@@ -444,7 +544,7 @@
 	}
 
 	if(sinfo->flags & IPT_CONNTRACK_REPLDST) {
-		printf("%sctorigdst ", optpfx);
+		printf("%sctrepldst ", optpfx);
 
 		print_addr(
 		    (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].dst.ip,
@@ -453,18 +553,45 @@
 		    numeric);
 	}
 
+	if(sinfo->flags & IPT_CONNTRACK_ORIGSRCPORT) {
+		printf("%sctorigsrcport ", optpfx);
+        	if (sinfo->invflags & IPT_CONNTRACK_ORIGSRCPORT)
+			printf("! ");
+		printf("%u:%u ", (unsigned)sinfo->srcports[IP_CT_DIR_ORIGINAL][0], (unsigned)sinfo->srcports[IP_CT_DIR_ORIGINAL][1]);
+	}
+
+	if(sinfo->flags & IPT_CONNTRACK_ORIGDSTPORT) {
+		printf("%sctorigdstport ", optpfx);
+        	if (sinfo->invflags & IPT_CONNTRACK_ORIGDSTPORT)
+			printf("! ");
+		printf("%u:%u ", (unsigned)sinfo->dstports[IP_CT_DIR_ORIGINAL][0], (unsigned)sinfo->dstports[IP_CT_DIR_ORIGINAL][1]);
+	}
+
+	if(sinfo->flags & IPT_CONNTRACK_REPLSRCPORT) {
+		printf("%sctreplsrcport ", optpfx);
+        	if (sinfo->invflags & IPT_CONNTRACK_REPLSRCPORT)
+			printf("! ");
+		printf("%u:%u ", (unsigned)sinfo->srcports[IP_CT_DIR_REPLY][0], (unsigned)sinfo->srcports[IP_CT_DIR_REPLY][1]);
+	}
+
+	if(sinfo->flags & IPT_CONNTRACK_REPLDSTPORT) {
+		printf("%sctrepldstport ", optpfx);
+        	if (sinfo->invflags & IPT_CONNTRACK_REPLDSTPORT)
+			printf("! ");
+		printf("%u:%u ", (unsigned)sinfo->dstports[IP_CT_DIR_REPLY][0], (unsigned)sinfo->dstports[IP_CT_DIR_REPLY][1]);
+	}
+
 	if(sinfo->flags & IPT_CONNTRACK_STATUS) {
 		printf("%sctstatus ", optpfx);
         	if (sinfo->invflags & IPT_CONNTRACK_STATE)
-                	fputc('!', stdout);
+			printf("! ");
 		print_status(sinfo->statusmask);
 	}
 
 	if(sinfo->flags & IPT_CONNTRACK_EXPIRES) {
 		printf("%sctexpire ", optpfx);
         	if (sinfo->invflags & IPT_CONNTRACK_EXPIRES)
-                	fputc('!', stdout);
-
+			printf("! ");
         	if (sinfo->expires_max == sinfo->expires_min)
                 	printf("%lu ", sinfo->expires_min);
         	else

Index: patch-o-matic/pending/conntrack.patch
===================================================================
RCS file: /cvspublic/netfilter/userspace/patch-o-matic/pending/conntrack.patch,v retrieving revision 1.1

pending/conntrack.patch +51 -12

100 lines not on this page · raw message

Index: patch-o-matic/pending/conntrack.patch.help
===================================================================
RCS file: /cvspublic/netfilter/userspace/patch-o-matic/pending/conntrack.patch.help,v retrieving revision 1.1

pending/conntrack.patch.help +6 -1
diff -u -r1.1 conntrack.patch.help
@@ -1,4 +1,4 @@
-Author: Marc Boucher <[email protected]>
+Author: Marc Boucher <[email protected]>, Henrik Nordstrom <[email protected]>
 Status: Works For Me.
 
 This is a general conntrack match module, a superset of the state match.
@@ -23,6 +23,11 @@
                                 Reply source specification
      --ctrepldst  [!] address[/mask]
                                 Reply destination specification
+     --ctorigsrcport [!] port	Original source port specification
+     --ctorigdstport [!] port	Original destination port specification
+     --ctreplsrcport [!] port	Reply source port specification
+     --ctrepldstport [!] port	Reply destination port specification
+
  [!] --ctstatus [NONE|EXPECTED|SEEN_REPLY|ASSURED][,...]
                                 Status(es) to match
  [!] --ctexpire  time[:time]    Match remaining lifetime in seconds against