Skip to contentSkip to conversation

ipalter/ipset

PATCH Nagy Zoltan 3 participants 4 messages May 16, 2005 – May 20, 2005

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

hi

i've made a small patch to the ipset/portmap match, it adds an alternate "invert" which made me able to restrict all users to ssh/http/other basic services,
and open all ports when neccessary with an easy step...maybe good for somebody too ;)

the IPALTER target it rewrites the source or dest address of an ip package - i've searched a long time for an alternate solution...but i didn't find anything.. i think it's only useful for extreme situtations in my case: i've an arp proxy firewall, and it wants to notify the
clients that they need to change there MTU to 1440 because the other side can't handle it...the clients doesn't accept
the icmp fragmentation-needed package, because 10.0.0.2 is not their router, so i rewrite it to look like that ;)

best regards, kirk

extensions/.IPALTER-test +3 -0
diff -Naur iptables-1.3.1-orig/extensions/.IPALTER-test iptables-1.3.1/extensions/.IPALTER-test
@@ -0,0 +1,3 @@
+#!/bin/sh
+# True if IPALTER patch is applied.
+[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_IPALTER.h ] && echo IPALTER
extensions/libipt_IPALTER.c +146 -0

146 lines not on this page · raw message

include/linux/netfilter_ipv4/ipt_IPALTER.h +12 -0
diff -Naur linux-2.6.11.9-orig/include/linux/netfilter_ipv4/ipt_IPALTER.h linux-2.6.11.9/include/linux/netfilter_ipv4/ipt_IPALTER.h
@@ -0,0 +1,12 @@
+#ifndef _IPT_IPALTER_H_target
+#define _IPT_IPALTER_H_target
+
+//#include <linux/ip.h>
+#define	IPALTER_VERSION	"0.1"
+
+struct ipt_ipalter_target_info {
+	struct	iphdr	mask;
+	struct	iphdr	alter;
+};
+
+#endif /*_IPT_IPALTER_H_target*/
net/ipv4/netfilter/Kconfig +8 -0
diff -Naur linux-2.6.11.9-orig/net/ipv4/netfilter/Kconfig linux-2.6.11.9/net/ipv4/netfilter/Kconfig
@@ -566,6 +566,14 @@
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP_NF_TARGET_IPALTER
+	tristate "IPALTER target support"
+	depends on IP_NF_MANGLE
+	help
+	  This option adds a `IPALTER' target
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 config IP_NF_TARGET_ECN
 	tristate "ECN target support"
 	depends on IP_NF_MANGLE
net/ipv4/netfilter/Makefile +1 -0
diff -Naur linux-2.6.11.9-orig/net/ipv4/netfilter/Makefile linux-2.6.11.9/net/ipv4/netfilter/Makefile
@@ -63,6 +63,7 @@
 # targets
 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
+obj-$(CONFIG_IP_NF_TARGET_IPALTER) += ipt_IPALTER.o
 obj-$(CONFIG_IP_NF_TARGET_ECN) += ipt_ECN.o
 obj-$(CONFIG_IP_NF_TARGET_DSCP) += ipt_DSCP.o
 obj-$(CONFIG_IP_NF_TARGET_MARK) += ipt_MARK.o
net/ipv4/netfilter/ipt_IPALTER.c +131 -0

131 lines not on this page · raw message

ipset_portmap.c +10 -2
diff -Naur ipset-orig/ipset_portmap.c ipset/ipset_portmap.c
@@ -69,7 +69,9 @@
 		   port_tostring(mydata->to, 0));
 
 		break;
-
+	case '3':
+		mydata->invert=1;
+		break;
 	default:
 		return 0;
 	}
@@ -111,6 +113,7 @@
 static struct option create_opts[] = {
 	{"from", 1, 0, '1'},
 	{"to", 1, 0, '2'},
+	{"invert", 0, 0, '3'},
 	{0}
 };
 
@@ -140,6 +143,7 @@
 	memset(map, 0, sizeof(struct ip_set_portmap));
 	map->first_port = header->from;
 	map->last_port = header->to;
+	map->invert=header->invert;
 }
 
 void printheader(struct set *set, unsigned options)
@@ -149,6 +153,8 @@
 
 	printf(" from: %s", port_tostring(mysetdata->first_port, options));
 	printf(" to: %s\n", port_tostring(mysetdata->last_port, options));
+	printf(" invert: %s\n", mysetdata->invert ? "on" : "off");
+
 }
 
 void printports_sorted(struct set *set, void *data, size_t len, unsigned options)
@@ -176,6 +182,8 @@
 	       port_tostring(mysetdata->first_port, options));
 	printf(" --to %s\n", 
 	       port_tostring(mysetdata->last_port, options));
+	if(mysetdata->invert)
+		printf(" --invert\n");
 }
 
 void saveports(struct set *set, void *data, size_t len, unsigned options)
@@ -196,7 +204,7 @@
 void usage(void)
 {
 	printf
-	    ("-N set portmap --from PORT --to PORT\n"
+	    ("-N set portmap --from PORT --to PORT [--invert]\n"
 	     "-A set PORT\n"
 	     "-D set PORT\n"
 	     "-T set PORT\n");
linux-2.6.11.5/include/linux/netfilter_ipv4/ip_set_portmap.h renamed from linux-2.6.11.5-b2/include/linux/netfilter_ipv4/ip_set_portmap.h +5 -2
@@ -9,17 +9,20 @@
 
 struct ip_set_portmap {
 	void *members;			/* the portmap proper */
-	ip_set_ip_t first_port;		/* host byte order, included in range */
-	ip_set_ip_t last_port;		/* host byte order, included in range */
+	ip_set_ip_t	first_port;	/* host byte order, included in range */
+	ip_set_ip_t	last_port;	/* host byte order, included in range */
+	ip_set_ip_t	invert;		/* invert ports match */	
 };
 
 struct ip_set_req_portmap_create {
 	ip_set_ip_t from;
 	ip_set_ip_t to;
+	ip_set_ip_t invert;
 };
 
 struct ip_set_req_portmap {
 	ip_set_ip_t port;
+	ip_set_ip_t invert;
 };
 
 #endif /* __IP_SET_PORTMAP_H */
linux-2.6.11.5/net/ipv4/netfilter/ip_set_portmap.c renamed from linux-2.6.11.5-b2/net/ipv4/netfilter/ip_set_portmap.c +11 -3
@@ -69,11 +69,17 @@
 	struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
 
 	if (port < map->first_port || port > map->last_port)
-		return -ERANGE;
-		
+	{
+		if(map->invert)
+			return	1;
+		else	return -ERANGE;
+	}	
 	*hash_port = port;
 	DP("set: %s, port:%u, %u", set->name, port, *hash_port);
-	return !!test_bit(port - map->first_port, map->members);
+	if(map->invert)
+		return !test_bit(port - map->first_port, map->members);
+	else
+		return !!test_bit(port - map->first_port, map->members);
 }
 
 static int
@@ -229,6 +235,7 @@
 	}
 	map->first_port = req->from;
 	map->last_port = req->to;
+	map->invert = !!req->invert;
 	newbytes = bitmap_bytes(req->from, req->to);
 	map->members = kmalloc(newbytes, GFP_KERNEL);
 	if (!map->members) {
@@ -268,6 +275,7 @@
 
 	header->from = map->first_port;
 	header->to = map->last_port;
+	header->invert=map->invert;
 }
 
 static int list_members_size(const struct ip_set *set)
#1 lore raw

Hi,

Quoted 4 lines · Nagy Zoltan

i've made a small patch to the ipset/portmap match, it adds an alternate "invert" which made me able to restrict all users to ssh/http/other basic services, and open all ports when neccessary with an easy step...maybe good for somebody too ;)

What's wrong with the normal negation?

Quoted 8 lines

the IPALTER target it rewrites the source or dest address of an ip package - i've searched a long time for an alternate solution...but i didn't find anything.. i think it's only useful for extreme situtations in my case: i've an arp proxy firewall, and it wants to notify the clients that they need to change there MTU to 1440 because the other side can't handle it...the clients doesn't accept the icmp fragmentation-needed package, because 10.0.0.2 is not their router, so i rewrite it to look like that ;)

Heresy - but can work in extreme situations. :-) Actually, it's a stateless NAT as far as I see.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary
#2 lore raw
Quoted 3 lines

Hi,

What's wrong with the normal negation?

example:
ipset -N ports_level_0 --invert --from 1 --to 1
ipset -N ports_level_1 --from 1 --to 1024
ipset -N ports_level_X --from 1 --to 1
ipset -A ports_level_1 22
ipset -A ports_level_1 80
ipset -N in_map ipmap --netwrok 10.0.0.0/24
ipset -A in_map 10.0.0.1
ipset -A in_map 10.0.0.2
[...]
ipset -B in_map 10.0.0.1 ports_level_0
ipset -B in_map 10.0.0.2 ports_level_1
[...]
ipset -B in_map :default: ports_level_X

iptables -P FORWARD DROP
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -i eth0 -s 10.0.0.0/24 -j ACCEPT
iptables -A FORWARD -m set --set in_map dst,dst -j ACCEPT

and if requested i can change 10.0.0.2's visible ports to all,
without having another ipmap to describe the set of those machines that are ok to run services on any port..

Quoted 2 lines

Heresy - but can work in extreme situations. :-) Actually, it's a stateless NAT as far as I see.

yes, i know ;)
but there is any alternative for this kind of problem?

Best regards, kirk

#3 lore raw
Quoted 16 lines · Kirk
Quoted 1 line

What's wrong with the normal negation?

example:
ipset -N ports_level_0 --invert --from 1 --to 1
ipset -N ports_level_1 --from 1 --to 1024
ipset -N ports_level_X --from 1 --to 1
ipset -A ports_level_1 22
ipset -A ports_level_1 80
ipset -N in_map ipmap --netwrok 10.0.0.0/24
ipset -A in_map 10.0.0.1
ipset -A in_map 10.0.0.2
[...]
ipset -B in_map 10.0.0.1 ports_level_0
ipset -B in_map 10.0.0.2 ports_level_1
[...]
ipset -B in_map :default: ports_level_X

Quoted 7 lines

iptables -P FORWARD DROP
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -i eth0 -s 10.0.0.0/24 -j ACCEPT
iptables -A FORWARD -m set --set in_map dst,dst -j ACCEPT

and if requested i can change 10.0.0.2's visible ports to all,
without having another ipmap to describe the set of those machines that are ok to run services on any port..

Nice! I'll add --invert (or better named as --negate?) as a generic feature to any set types in the forthcoming release.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary