Skip to contentSkip to conversation

iptables should prevent --rttl together with --set in recent module

PATCH Erich Schubert 1 participant 1 message Feb 13, 2006 netfilter

lore
Share & tools
lore
#0 lore raw

Hi,
iptables should prevent the use of --rttl together with --set in the recent module.
By mistake I had the --rttl flag also on my --set rule, which resulted in iptables blocking almost any connections in my case (instead of only those ssh connections coming in at more than 5/sec from one host) A simple check should be able to prevent that from happening to others.

Attached is a patch to add such a check; this is just how I'd add this by looking at your code... I don't know if there is maybe a nicer way to do this kind of sanity check. Feel free to do it entirely different. Or maybe it's actually nicer to fix this issue within the kernel...

Note that this also is a deficiency in nfsim-testsuite/01iptables/40ipt_recent.sh:
test #3 uses --rttl outside of the spec (i.e. together with --set), and doesn't verify that a sixth packet by a different source would still pass.
This might also explain the issues with Test #4?

The second thing I'd like to bring forward to you is a clarification request WRT to the string match (maybe document it in the man page):
the "from" and "to" range - what is it on? is it within the packet? does it include Packet headers? Is it on stream data?
I'd like to use the string match for blocking BitTorrent traffic, but in order to minimize performance impact, I'd like to keep the search range as low as possible, or maybe only scan the first few packets of a connection (bypass via fragmentation isn't a big problem, this is just about filtering people having bittorrent running on their laptop in background by accident...)

best regards,
Erich Schubert

Signature · 5 lines
     erich@(vitavonni.de|debian.org)    --    GPG Key ID: 4B3A135C    (o_
  There is no branch of mathematics, however abstract, which may not  //\
 some day be applied to phenomena of the real world. --- Lobatchevsky V_/_
   Es ist nicht wenig Zeit, die wir haben, sondern es ist viel Zeit,
                   die wir nicht nutzen. --- Seneca
libipt_recent.c +24 -9
Index: libipt_recent.c
===================================================================
@@ -21,6 +21,12 @@
 #define IPT_RECENT_NAME_LEN	200
 #endif /* IPT_RECENT_NAME_LEN */
 
+#ifndef IPT_RECENT_FLAGS_ACTION
+#define IPT_RECENT_FLAGS_ACTION 1
+#define IPT_RECENT_FLAGS_HAS_RTTL 2
+#define IPT_RECENT_FLAGS_MAY_RTTL 4
+#endif /* IPT_RECENT_FLAGS_ACTION
+
 /* Options for this module */
 static struct option opts[] = {
 	{ .name = "set",      .has_arg = 0, .flag = 0, .val = 201 }, 
@@ -91,43 +97,47 @@
 	struct ipt_recent_info *info = (struct ipt_recent_info *)(*match)->data;
 	switch (c) {
 		case 201:
-			if (*flags) exit_error(PARAMETER_PROBLEM,
+			if (*flags & IPT_RECENT_FLAGS_ACTION)
+				exit_error(PARAMETER_PROBLEM,
 					"recent: only one of `--set', `--rcheck' "
 					"`--update' or `--remove' may be set");
 			check_inverse(optarg, &invert, &optind, 0);
 			info->check_set |= IPT_RECENT_SET;
 			if (invert) info->invert = 1;
-			*flags = 1;
+			*flags |= IPT_RECENT_FLAGS_ACTION;
 			break;
 			
 		case 202:
-			if (*flags) exit_error(PARAMETER_PROBLEM,
+			if (*flags & IPT_RECENT_FLAGS_ACTION)
+				exit_error(PARAMETER_PROBLEM,
 					"recent: only one of `--set', `--rcheck' "
 					"`--update' or `--remove' may be set");
 			check_inverse(optarg, &invert, &optind, 0);
 			info->check_set |= IPT_RECENT_CHECK;
 			if(invert) info->invert = 1;
-			*flags = 1;
+			*flags |= IPT_RECENT_FLAGS_ACTION | IPT_RECENT_FLAGS_MAY_RTTL;
 			break;
 
 		case 203:
-			if (*flags) exit_error(PARAMETER_PROBLEM,
+			if (*flags & IPT_RECENT_FLAGS_ACTION)
+				exit_error(PARAMETER_PROBLEM,
 					"recent: only one of `--set', `--rcheck' "
 					"`--update' or `--remove' may be set");
 			check_inverse(optarg, &invert, &optind, 0);
 			info->check_set |= IPT_RECENT_UPDATE;
 			if (invert) info->invert = 1;
-			*flags = 1;
+			*flags |= IPT_RECENT_FLAGS_ACTION | IPT_RECENT_FLAGS_MAY_RTTL;
 			break;
 
 		case 206:
-			if (*flags) exit_error(PARAMETER_PROBLEM,
+			if (*flags & IPT_RECENT_FLAGS_ACTION)
+				exit_error(PARAMETER_PROBLEM,
 					"recent: only one of `--set', `--rcheck' "
 					"`--update' or `--remove' may be set");
 			check_inverse(optarg, &invert, &optind, 0);
 			info->check_set |= IPT_RECENT_REMOVE;
 			if (invert) info->invert = 1;
-			*flags = 1;
+			*flags |= IPT_RECENT_FLAGS_ACTION;
 			break;
 
 		case 204:
@@ -140,6 +150,7 @@
 
 		case 207:
 			info->check_set |= IPT_RECENT_TTL;
+			*flags |= IPT_RECENT_FLAGS_HAS_RTTL;
 			break;
 
 		case 208:
@@ -167,10 +178,14 @@
 final_check(unsigned int flags)
 {
 
-	if (!flags)
+	if (!flags & IPT_RECENT_FLAGS_ACTION)
 		exit_error(PARAMETER_PROBLEM,
 			"recent: you must specify one of `--set', `--rcheck' "
 			"`--update' or `--remove'");
+	if ((flags & IPT_RECENT_FLAGS_HAS_RTTL) && (flags & IPT_RECENT_FLAGS_MAY_RTTL == 0))
+		exit_error(PARAMETER_PROBLEM,
+			"recent: you may only use `--rttl' with either `--rcheck' "
+			"or `--update'");
 }
 
 /* Prints out the matchinfo. */