changes to libipt_connlimit (1.3.4) for new connlimit cli options
PATCH 1/3 changes to libipt_connlimit (1.3.4) for new connlimit cli options
[PoM PATCH 1/3] changes to libipt_connlimit (1.3.4) for new connlimit cli options
patched patch-o-matic-ng-20051109
Adds new options to connlimit iptables module and new man page content for these options.
Signed-off-by Damon Gray <[email protected]>
patchlets/connlimit/iptables.patch +202 -0
@@ -0,0 +1,202 @@ +diff -uprN 1.3.4/extensions/libipt_connlimit.c 1.3.4-changed/extensions/libipt_connlimit.c +--- 1.3.4/extensions/libipt_connlimit.c Sat Feb 19 14:19:17 2005 ++++ 1.3.4-changed/extensions/libipt_connlimit.c Thu Nov 10 00:38:50 2005 +@@ -15,14 +15,18 @@ help(void) + { + printf( + "connlimit v%s options:\n" +-"[!] --connlimit-above n match if the number of existing tcp connections is (not) above n\n" +-" --connlimit-mask n group hosts using mask\n" ++"[!] --connlimit-above n match if the number of existing tcp connections is (not) above n\n" ++" --connlimit-mask n[:hashsize] group hosts using mask, default 32\n" ++" --connlimit-sharedid n shared connection data id\n" ++" --connlimit-timeout secs[:mark[/mask]] idle connections should timeout when over limit, and optionally set connmark\n" + "\n", IPTABLES_VERSION); + } + + static struct option opts[] = { +- { "connlimit-above", 1, 0, '1' }, +- { "connlimit-mask", 1, 0, '2' }, ++ { "connlimit-above", 1, 0, '1' }, ++ { "connlimit-mask", 1, 0, '2' }, ++ { "connlimit-sharedid", 1, 0, '3' }, ++ { "connlimit-timeout", 1, 0, '4' }, + {0} + }; + +@@ -36,10 +40,21 @@ parse(int c, char **argv, int invert, un + { + struct ipt_connlimit_info *info = (struct ipt_connlimit_info*)(*match)->data; + int i; ++ unsigned long int ul; ++ char *endptr=NULL; + + if (0 == (*flags & 2)) { + /* set default mask unless we've already seen a mask option */ + info->mask = htonl(0xFFFFFFFF); ++ info->hash_size = 0; ++ } ++ if (0 == (*flags & 4)) { ++ info->shared_id = 0; ++ } ++ if (0 == (*flags & 8)) { ++ info->timeout = 0; ++ info->connmark = 0; ++ info->connmark_mask = 0; + } + + switch (c) { +@@ -51,18 +66,65 @@ parse(int c, char **argv, int invert, un + break; + + case '2': +- i = atoi(argv[optind-1]); ++ i = strtol(argv[optind-1], &endptr, 0); + if ((i < 0) || (i > 32)) + exit_error(PARAMETER_PROBLEM, + "--connlimit-mask must be between 0 and 32"); + +- if (i == 0) +- info->mask = 0; +- else +- info->mask = htonl(0xFFFFFFFF << (32 - i)); ++ /* change cidr to mask */ ++ info->mask = ( ( i == 0 ) ? 0 : htonl(0xFFFFFFFF << (32 - i)) ); ++ ++ if (*endptr == '\0') { ++ info->hash_size = 0; ++ } else if(*endptr == ':') { ++ i = strtol(endptr+1, &endptr, 0); ++ if ( i < 0) ++ exit_error(PARAMETER_PROBLEM, ++ "--connlimit-mask hash size must be >= 0"); ++ info->hash_size = i; ++ } ++ ++ if(*endptr != '\0') ++ exit_error(PARAMETER_PROBLEM, ++ "--connlimit-mask had invalid character in number"); ++ + *flags |= 2; + break; + ++ case '3': ++ ul = strtoul(argv[optind-1],&endptr, 0); ++ if(*endptr != '\0') ++ exit_error(PARAMETER_PROBLEM, ++ "--connlimit-sharedid had invalid character in number"); ++ ++ info->shared_id = ul; ++ ++ *flags |= 4; ++ break; ++ ++ case '4': ++ ul = strtoul(argv[optind-1], &endptr, 0); ++ ++ info->timeout = ul; ++ ++ if(*endptr == '\0') { ++ info->connmark = 0; ++ } else if(*endptr == ':') { ++ ul = strtoul(endptr+1, &endptr, 0); ++ info->connmark = ul; ++ if(*endptr == '/') { ++ ul = strtoul(endptr+1, &endptr, 0); ++ info->connmark_mask = ul; ++ } ++ } ++ ++ if(*endptr != '\0') ++ exit_error(PARAMETER_PROBLEM, ++ "--connlimit-timeout had invalid character in number"); ++ ++ *flags |= 8; ++ break; ++ + default: + return 0; + } +@@ -100,8 +162,8 @@ print(const struct ipt_ip *ip, + { + struct ipt_connlimit_info *info = (struct ipt_connlimit_info*)match->data; + +- printf("#conn/%d %s %d ", count_bits(info->mask), +- info->inverse ? "<" : ">", info->limit); ++ printf("#conn(%u)/%d %s %d(%us:%lu/%lu) ", info->shared_id, count_bits(info->mask), ++ info->inverse ? "<" : ">", info->limit, info->timeout, info->connmark, info->connmark_mask); + } + + /* Saves the matchinfo in parsable form to stdout. */ +@@ -111,6 +173,8 @@ static void save(const struct ipt_ip *ip + + printf("%s--connlimit-above %d ",info->inverse ? "! " : "",info->limit); + printf("--connlimit-mask %d ",count_bits(info->mask)); ++ printf("--connlimit-sharedid %u ",info->shared_id); ++ printf("--connlimit-timeout %u:%lu/%lu ",info->timeout, info->connmark, info->connmark_mask); + } + + static struct iptables_match connlimit = { +diff -uprN 1.3.4/extensions/libipt_connlimit.man 1.3.4-changed/extensions/libipt_connlimit.man +--- 1.3.4/extensions/libipt_connlimit.man Fri Apr 1 01:49:12 2005 ++++ 1.3.4-changed/extensions/libipt_connlimit.man Thu Nov 10 00:38:51 2005 +@@ -1,11 +1,40 @@ + Allows you to restrict the number of parallel TCP connections to a +-server per client IP address (or address block). ++server per client IP address (or address block). [All numbers can be ++given in the following formats ## (decimal), 0x## (hex), 0## (octal)] + .TP + [\fB!\fR] \fB--connlimit-above \fIn\fR +-match if the number of existing tcp connections is (not) above n ++Match if the number of existing tcp connections is (not) above n + .TP +-.BI "--connlimit-mask " "bits" +-group hosts using mask ++.BI "--connlimit-mask " "bits[:hashsize]" ++Group hosts using mask, default is 32 which means limit on a per host ++basis. Another useful option is 0 which will mean total connections from ++any source. Hash size is optional, if not entered (or 0) the default is 256 unless ++"bits" is 0 and then it will be 1 no matter what you put hashsize to. Hash size ++will be rounded up to the nearest power of 2. ++.TP ++.BI "--connlimit-sharedid " "number" ++Use a shared connection tracker. This has two major advantages, the first is ++that the currently tracked connections will persist through a change as long as ++you make a change by adding a new rule and then deleting the old rule but using ++the same sharedid. The second advantage is that multiple rules can use the same ++connection tracking data. The first rule that uses a shared id sets the mask, any ++subsequent rule that has the same shared id must have the same mask. Hash size is ++ignored for subsequent rules as well, it will be automatically set to whatever the ++first rule set it to. The default value of 0 means it uses a local data ++store and not a shared one. ++.TP ++.BI "--connlimit-timeout " "seconds[:connmark[/mask]]" ++This will set a timeout for connections that have been idle for at least this ++many seconds. It will only timeout these connections if the connlimit rule will ++match (meaning it is over the limit). Only the conntrack entry for the ++connection will be timed out. The default value of 0 means that no timeouts are ++performed. You will want another rule that doesn't allow new conntrack entries ++to be made if the packet isn't a SYN packet. If "connmark" is supplied then ++instead of the conntrack entry being timed out it will have the conntrack entry ++marked. The connmark feature depends on the IP_NF_CONNTRACK_MARK kernel feature ++being set. If connmark is left empty or set to the default 0 then the ++conntrack entry will be timed out. The connmark functionality works just like the ++CONNMARK target, including the masking of the connmark with /mask. + .P + Examples: + .TP +@@ -19,3 +48,14 @@ iptables -p tcp --syn --dport 23 -m conn + network (24 bit netmask) + iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 + --connlimit-mask 24 -j REJECT ++.TP ++# limit the total connections to the host 10.1.1.1 to 100 ++iptables -p tcp --syn -d 10.1.1.1 -m connlimit --connlimit-above 100 ++--connlimit-mask 0 -j REJECT --reject-with tcp-reset ++.TP ++# timeout connections that have been idle for 5 mins when over the limit (10) and don't allow new connections unless it is a SYN packet ++ ++iptables -p tcp ! --syn -m state --state NEW,RELATED -j REJECT ++ ++iptables -p tcp --syn -m connlimit --connlimit-above 10 ++--connlimit-mask 0 --connlimit-timeout 300 -j REJECT --reject-with tcp-reset