Resend [patch 2/2] iptables: add random option to SNAT
Hi,
This is a resend of :
http://patchwork.netfilter.org/netfilter-devel/patch.pl?id=36
Merge window for 2.6.21 is open and kernel part of the feature should reach kernel tree soon...
BR,
Signature · 3 lines
Éric Leblond, [email protected] Téléphone : 01 44 89 46 39, Fax : 01 44 89 45 01 INL, http://www.inl.fr
libipt_MASQUERADE.c +25 -3
@@ -14,7 +14,7 @@ {printf ("MASQUERADE v%s options:\n" -" --to-ports <port>[-<port>]\n" +" --to-ports [<port>[-<port>]][:random]\n" " Port (range) to map to.\n\n" ,IPTABLES_VERSION ); }
@@ -40,14 +40,30 @@ parse_ports(const char *arg, struct ip_nat_multi_range *mr) { const char *dash; +char *random;int port; +if (*arg =='r' ){ + mr->range[0 ].flags |=IP_NAT_RANGE_PROTO_RANDOM ; +return ; + } + mr->range[0 ].flags |=IP_NAT_RANGE_PROTO_SPECIFIED ; port =atoi (arg);if (port <=0 || port >65535 )exit_error (PARAMETER_PROBLEM ,"Port `%s' not valid\n" , arg); + random =strchr (arg,':' ); +if (random) { +if (*(random+1 ) =='r' ) { + mr->range[0 ].flags |=IP_NAT_RANGE_PROTO_RANDOM ; + *random ='\0' ; + }else { +exit_error (PARAMETER_PROBLEM ,"Random specification `%s' not valid\n" , arg); + } + } + dash =strchr (arg,'-' );if (!dash) { mr->range[0 ].min.tcp.port
@@ -125,8 +141,11 @@printf ("%hu" ,ntohs (r->min.tcp.port));if (r->max.tcp.port != r->min.tcp.port)printf ("-%hu" ,ntohs (r->max.tcp.port)); +if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) +printf (":random" );printf (" " ); - } + }else if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) +printf ("random " ); }/* Saves the union ipt_targinfo in parsable form to stdout. */
@@ -141,8 +160,11 @@printf ("--to-ports %hu" ,ntohs (r->min.tcp.port));if (r->max.tcp.port != r->min.tcp.port)printf ("-%hu" ,ntohs (r->max.tcp.port)); +if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) +printf (":random" );printf (" " ); - } + }else if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) +printf ("--to-ports random " ); }static struct iptables_target masq = {NULL ,
libipt_SNAT.c +51 -35
117 lines not on this page · raw message
libipt_SAME.c +26 -9
@@ -16,7 +16,7 @@ {printf ("SAME v%s options:\n" -" --to <ipaddr>-<ipaddr>\n" +" --to <ipaddr>-<ipaddr>[:random]\n" " Addresses to map source to.\n" " May be specified more than\n" " once for multiple ranges.\n"
@@ -49,10 +49,21 @@static void parse_to (char *arg,struct ip_nat_range *range) { -char *dash; +char *dash, *random;struct in_addr *ip; range->flags |=IP_NAT_RANGE_MAP_IPS ; + + random =strchr (arg,':' ); +if (random) { +if (*(random+1 ) =='r' ) { + range->flags |=IP_NAT_RANGE_PROTO_RANDOM ; + *random ='\0' ; + }else { +exit_error (PARAMETER_PROBLEM ,"Random specification `%s' not valid\n" , random+1 ); + } + } + dash =strchr (arg,'-' );if (dash)
@@ -90,7 +101,7 @@struct ipt_same_info *mr = (struct ipt_same_info *)(*target)->data; -switch (c) { +switch (c) {case '1' :if (mr->rangesize ==IPT_SAME_MAX_RANGE ) exit_error(PARAMETER_PROBLEM,
@@ -151,10 +162,13 @@printf ("%s" ,addr_to_dotted (&a)); a.s_addr = r->max_ip; -if (r->min_ip == r->max_ip) +if (r->min_ip != r->max_ip) +printf ("-%s" ,addr_to_dotted (&a)); + +if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) +printf (":random " ); +else printf (" " ); -else -printf ("-%s " ,addr_to_dotted (&a)); }if (mr->info &IPT_SAME_NODST )
@@ -177,10 +191,13 @@printf ("--to %s" ,addr_to_dotted (&a)); a.s_addr = r->max_ip; -if (r->min_ip == r->max_ip) +if (r->min_ip != r->max_ip) +printf ("-%s" ,addr_to_dotted (&a)); + +if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) +printf (":random " ); +else printf (" " ); -else -printf ("-%s " ,addr_to_dotted (&a)); }if (mr->info &IPT_SAME_NODST )
Quoted 7 lines · Eric Leblond
Hi,
This is a resend of :
http://patchwork.netfilter.org/netfilter-devel/patch.pl?id=36Merge window for 2.6.21 is open and kernel part of the feature should reach kernel tree soon...
I'm in the process of preparing my patches for upstream submission (which include your random patch). I'll look into the userspace patch after that, I'm not a big fan of the :random syntax.
Hi,
Quoted 3 lines · Patrick McHardy
Eric Leblond wrote:
I'm in the process of preparing my patches for upstream submission (which include your random patch).
Great.
Quoted 2 lines
I'll look into the userspace patch
after that, I'm not a big fan of the :random syntax.
I make this choice because iptables had the capabilities to have multiple NAT ranges and hence randomization has to be linked with the range.
But we can also assume to have a per iptables rule switch. I can rewrite my patch in this way if you want.
BR,
Signature · 3 lines
Éric Leblond, [email protected] Téléphone : 01 44 89 46 39, Fax : 01 44 89 45 01 INL, http://www.inl.fr
Quoted 7 lines · Eric Leblond
Quoted 2 lines
I'll look into the userspace patch
after that, I'm not a big fan of the :random syntax.I make this choice because iptables had the capabilities to have multiple NAT ranges and hence randomization has to be linked with the range.
Not anymore since since rustynat changes (~2.6.12).
Quoted 3 lines
But we can also assume to have a per iptables rule switch. I can rewrite my patch in this way if you want.
I think I would prefer that.
Quoted 10 lines · Patrick McHardy
Quoted 7 lines · Eric Leblond
Quoted 2 lines
I'll look into the userspace patch
after that, I'm not a big fan of the :random syntax.I make this choice because iptables had the capabilities to have multiple NAT ranges and hence randomization has to be linked with the range.
Not anymore since since rustynat changes (~2.6.12).
So what's the proper way to do that then?
Jan
Signature · 1 line
ft: http://freshmeat.net/p/chaostables/
Quoted 4 lines · Jan Engelhardt
Quoted 2 lines · Patrick McHardy
Eric Leblond wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
I make this choice because iptables had the capabilities to have multiple NAT ranges and hence randomization has to be linked with the range.
Not anymore since since rustynat changes (~2.6.12).
So what's the proper way to do that then?
There is none, nobody was using it.
Hi,
Quoted 4 lines · Patrick McHardy
Quoted 2 lines
But we can also assume to have a per iptables rule switch. I can rewrite my patch in this way if you want.
I think I would prefer that.
Here's the corresponding patch. It adds a --random flags to SNAT, SAME and MASQUERADE targets.
I've also updated man pages by adding this --random option.
BR,
Signature · 2 lines
Eric Leblond <[email protected]> INL
libipt_MASQUERADE.c +22 -1
@@ -15,12 +15,21 @@printf ("MASQUERADE v%s options:\n" " --to-ports <port>[-<port>]\n" -" Port (range) to map to.\n\n" , +" Port (range) to map to.\n" +#ifdef IP_NAT_RANGE_PROTO_RANDOM +" --random\n" +" Randomize source port.\n" +#endif +"\n" +,IPTABLES_VERSION ); }static struct option opts[] = { {"to-ports" ,1 ,0 ,'1' }, +#ifdef IP_NAT_RANGE_PROTO_RANDOM + {"random" ,0 ,0 ,'2' }, +#endif {0 } };
@@ -100,6 +109,12 @@parse_ports (optarg, mr);return 1 ; +#ifdef IP_NAT_RANGE_PROTO_RANDOM +case '2' : + mr->range[0 ].flags |=IP_NAT_RANGE_PROTO_RANDOM ; +return 1 ; +#endif +default :return 0 ; }
@@ -127,6 +142,12 @@printf ("-%hu" ,ntohs (r->max.tcp.port));printf (" " ); } + +#ifdef IP_NAT_RANGE_PROTO_RANDOM +if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) { +printf ("random" ); + } +#endif }/* Saves the union ipt_targinfo in parsable form to stdout. */
libipt_SNAT.man +5 -2
@@ -7,7 +7,7 @@ mangled), and rules should cease being examined. It takes one type of option: .TP -.BR "--to-source " "\fIipaddr\fP[-\fIipaddr\fP][:\fIport\fP-\fIport\fP]" +.BR "--to-source " "\fIipaddr\fP[-\fIipaddr\fP][:\fIport\fP-\fIport\fP]" [ "--random" ] which can specify a single new source IP address, an inclusive range of IP addresses, and optionally, a port range (which is only valid if the rule also specifies
@@ -17,7 +17,10 @@ If no port range is specified, then source ports below 512 will be mapped to other ports below 512: those between 512 and 1023 inclusive will be mapped to ports below 1024, and other ports will be mapped to -1024 or above. Where possible, no port alteration will occur. +1024 or above. Where possible, no port alteration will If option +.B "--random" +is used then port mapping will be forcely randomized to avoid +attacks based on port prediction (kernel >= 2.6.21). .RS .PP In Kernels up to 2.6.10, you can add several --to-source options. For those
libipt_SNAT.c +35 -4
@@ -8,6 +8,11 @@#include <linux/netfilter_ipv4/ip_tables.h> #include <linux/netfilter_ipv4/ip_nat_rule.h> +#define IPT_SNAT_OPT_SOURCE 0x01 +#ifdef IP_NAT_RANGE_PROTO_RANDOM +# define IPT_SNAT_OPT_RANDOM 0x02 +#endif +/* Source NAT data consists of a multi-range, indicating where to map to. */ struct ipt_natinfo
@@ -22,7 +27,11 @@ {printf ( "SNAT v%s options:\n" -" --to-source <ipaddr>[-<ipaddr>][:port-port]\n" +" --to-source <ipaddr>[-<ipaddr>][:port-port]" +#ifdef IP_NAT_RANGE_PROTO_RANDOM +"[--random]" +#endif +"\n" " Address to map source to.\n" " (You can use this more than once)\n\n" ,IPTABLES_VERSION );
@@ -30,6 +39,9 @@static struct option opts[] = { {"to-source" ,1 ,0 ,'1' }, +#ifdef IP_NAT_RANGE_PROTO_RANDOM + {"random" ,0 ,0 ,'2' }, +#endif {0 } };
@@ -155,7 +167,7 @@exit_error (PARAMETER_PROBLEM ,"Unexpected `!' after --to-source" ); -if (*flags) { +if (*flags &IPT_SNAT_OPT_SOURCE ) {if (!kernel_version)get_kernel_version ();if (kernel_version >LINUX_VERSION (2 ,6 ,10 ))
@@ -163,9 +175,23 @@ "Multiple --to-source not supported"); } *target =parse_to (optarg, portok, info); - *flags =1 ; +#ifdef IP_NAT_RANGE_PROTO_RANDOM +if (*flags &IPT_SNAT_OPT_RANDOM ) + info->mr.range[0 ].flags |=IP_NAT_RANGE_PROTO_RANDOM ; +#endif + *flags =IPT_SNAT_OPT_SOURCE ;return 1 ; +#ifdef IP_NAT_RANGE_PROTO_RANDOM +case '2' : +if (*flags &IPT_SNAT_OPT_SOURCE ) { + info->mr.range[0 ].flags |=IP_NAT_RANGE_PROTO_RANDOM ; + *flags |=IPT_SNAT_OPT_RANDOM ; + }else + *flags |=IPT_SNAT_OPT_RANDOM ; +return 1 ; +#endif +default :return 0 ; }
@@ -174,7 +200,7 @@/* Final check; must have specfied --to-source. */ static void final_check (unsigned int flags) { -if (!flags) +if (!(flags &IPT_SNAT_OPT_SOURCE ))exit_error (PARAMETER_PROBLEM ,"You must specify --to-source" ); }
@@ -197,6 +223,11 @@if (r->max.tcp.port != r->min.tcp.port)printf ("-%hu" ,ntohs (r->max.tcp.port)); } +#ifdef IP_NAT_RANGE_PROTO_RANDOM +if (r->flags &IP_NAT_RANGE_PROTO_RANDOM ) { +printf (" random" ); + } +#endif }/* Prints out the targinfo. */
libipt_SAME.man +4 -0
@@ -9,3 +9,7 @@ .B "--nodst" Don't use the destination-ip in the calculations when selecting the new source-ip +.TP +.B "--random" +Port mapping will be forcely randomized to avoid attacks based on +port prediction (kernel >= 2.6.21).
libipt_SAME.c +41 -2
95 lines not on this page · raw message
libipt_MASQUERADE.man +10 -0
@@ -14,9 +14,19 @@ .TP .BR "--to-ports " "\fIport\fP[-\fIport\fP]" This specifies a range of source ports to use, overriding the default +.TP +.BR "--random" +Randomize source port mapping +.TP .B SNAT source port-selection heuristics (see above). This is only valid if the rule also specifies .B "-p tcp" or .BR "-p udp" . +If option +.B "--random" +is used then port mapping will be forcely randomized to avoid +attacks based on port prediction (kernel >= 2.6.21). + +
Patrick McHardy
AppliedQuoted 4 lines · Eric Leblond
Here's the corresponding patch. It adds a --random flags to SNAT, SAME and MASQUERADE targets.
I've also updated man pages by adding this --random option.
Applied, thanks Eric.