Skip to contentSkip to conversation

new disable command to effectively disable all filters

PATCH New Jens Laas 1 participant 1 message Oct 6, 2006 netfilter

lore
Share & tools
lore

Jens Laas

author
#0 lore raw

PATCH 1/1 new disable command to effectively disable all filters

My usage of iptables mainly consists of turning everything off.
In the event that Im not alone in this and others also would prefer a quick way to accomplish this I implemented this command.

Its pretty straightforward but could probably benefit from the eyes of experienced iptables developers.

Cheers,
Jens

The -Q --disable command removes all user-defined chains, flushes all remaining rules and sets the default policies to ACCEPT.

iptables.c +25 -6
@@ -80,9 +80,10 @@
 #define CMD_DELETE_CHAIN	0x0200U
 #define CMD_SET_POLICY		0x0400U
 #define CMD_RENAME_CHAIN	0x0800U
-#define NUMBER_OF_CMD	13
+#define CMD_DISABLE		0x1000U
+#define NUMBER_OF_CMD	14
 static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
-				 'N', 'X', 'P', 'E' };
+				 'N', 'X', 'P', 'E', 'Q' };
 
 #define OPTION_OFFSET 256
 
@@ -111,6 +112,7 @@
 	{ "list", 2, 0,  'L' },
 	{ "flush", 2, 0,  'F' },
 	{ "zero", 2, 0,  'Z' },
+	{ "disable", 0, 0,  'Q' },
 	{ "new-chain", 1, 0,  'N' },
 	{ "delete-chain", 2, 0,  'X' },
 	{ "rename-chain", 1, 0,  'E' },
@@ -172,7 +174,8 @@
 /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
 /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
 /*CHECK*/     {'x','+','+','+','x',' ','x',' ',' ',' ','x','x'},
-/*RENAME*/    {'x','x','x','x','x',' ','x','x','x','x','x','x'}
+/*RENAME*/    {'x','x','x','x','x',' ','x','x','x','x','x','x'},
+/*DISABLE*/   {'x','x','x','x','x',' ','x','x','x','x','x','x'}
 };
 
 static int inverse_for_options[NUMBER_OF_OPT] =
@@ -386,12 +389,13 @@
 "       %s -D chain rulenum [options]\n"
 "       %s -[LFZ] [chain] [options]\n"
 "       %s -[NX] chain\n"
+"       %s -Q\n"
 "       %s -E old-chain-name new-chain-name\n"
 "       %s -P chain target [options]\n"
 "       %s -h (print this help information)\n\n",
 	       program_name, program_version, program_name, program_name,
 	       program_name, program_name, program_name, program_name,
-	       program_name, program_name);
+	       program_name, program_name, program_name);
 
 	printf(
 "Commands:\n"
@@ -415,7 +419,9 @@
 "  --rename-chain\n"
 "            -E old-chain new-chain\n"
 "				Change chain name, (moving any references)\n"
-
+"  --disable -Q                 Removes all user-defined chains.\n"
+"                               Flushes all rules on the remaining chains.\n"
+"                               Sets policy for INPUT,FORWARD,OUPUT to ACCEPT.\n"
 "Options:\n"
 "  --proto	-p [!] proto	protocol: by number or name, eg. `tcp'\n"
 "  --source	-s [!] address[/mask]\n"
@@ -1962,7 +1968,7 @@
 	opterr = 0;
 
 	while ((c = getopt_long(argc, argv,
-	   "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
+	   "-A:D:R:I:L::M:F::Z::N:X::E:P:QVh::o:p:s:d:j:i:fbvnt:m:xc:g:",
 					   opts, NULL)) != -1) {
 		switch (c) {
 			/*
@@ -2049,6 +2055,11 @@
 			chain = optarg;
 			break;
 
+		case 'Q':
+			add_command(&command, CMD_DISABLE, CMD_NONE,
+				    invert);
+			break;
+
 		case 'X':
 			add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
 				    invert);
@@ -2545,6 +2556,14 @@
 	case CMD_FLUSH:
 		ret = flush_entries(chain, options&OPT_VERBOSE, handle);
 		break;
+	case CMD_DISABLE:
+		ret = delete_chain(NULL, options&OPT_VERBOSE, handle);
+		if(ret) ret = flush_entries(NULL, options&OPT_VERBOSE, handle);
+		if(ret) ret = iptc_set_policy("INPUT", "ACCEPT", NULL, handle);
+		if(ret) ret = iptc_set_policy("OUTPUT", "ACCEPT", NULL, handle);
+		if(ret) ret = iptc_set_policy("FORWARD", "ACCEPT", NULL, handle);
+		if(ret) ret = zero_entries(NULL, options&OPT_VERBOSE, handle);
+		break;
 	case CMD_ZERO:
 		ret = zero_entries(chain, options&OPT_VERBOSE, handle);
 		break;

-----------------------------------------------------------------------
'In theory, there is no difference between theory and practice.
But, in practice, there is.'
-----------------------------------------------------------------------
Jens Låås Email: [email protected]
Department of Computer Services, SLU Phone: +46 18 67 35 15
Vindbrovägen 1
P.O. Box 7079
S-750 07 Uppsala
SWEDEN
-----------------------------------------------------------------------