From 4489c0d66d9a0e6033c9472fd54df155788010b7 Mon Sep 17 00:00:00 2001 From: Yasuyuki KOZAKAI Date: Tue, 24 Jul 2007 07:11:26 +0000 Subject: Unifies libip[6]t_limit.c into libxt_limit.c. --- extensions/Makefile | 6 +- extensions/libip6t_limit.c | 195 --------------------------- extensions/libipt_limit.c | 196 --------------------------- extensions/libxt_limit.c | 214 ++++++++++++++++++++++++++++++ include/linux/netfilter/xt_limit.h | 21 +++ include/linux/netfilter_ipv4/ipt_limit.h | 21 --- include/linux/netfilter_ipv6/ip6t_limit.h | 21 --- 7 files changed, 238 insertions(+), 436 deletions(-) delete mode 100644 extensions/libip6t_limit.c delete mode 100644 extensions/libipt_limit.c create mode 100644 extensions/libxt_limit.c create mode 100644 include/linux/netfilter/xt_limit.h delete mode 100644 include/linux/netfilter_ipv4/ipt_limit.h delete mode 100644 include/linux/netfilter_ipv6/ip6t_limit.h diff --git a/extensions/Makefile b/extensions/Makefile index 3fbb1b4c..b81d7d6b 100644 --- a/extensions/Makefile +++ b/extensions/Makefile @@ -5,9 +5,9 @@ # header files are present in the include/linux directory of this iptables # package (HW) # -PF_EXT_SLIB:=ah addrtype comment connlimit connmark conntrack dscp ecn esp hashlimit helper icmp iprange length limit owner policy realm state tos ttl unclean CLASSIFY CONNMARK DNAT DSCP ECN LOG MARK MASQUERADE MIRROR NETMAP NFQUEUE REDIRECT REJECT SAME SNAT TCPMSS TOS TTL TRACE ULOG -PF6_EXT_SLIB:=connlimit connmark eui64 hl icmp6 length limit owner policy state CONNMARK HL LOG NFQUEUE MARK TCPMSS TRACE -PFX_EXT_SLIB:=mac mark multiport physdev pkttype sctp standard tcp tcpmss udp NOTRACK +PF_EXT_SLIB:=ah addrtype comment connlimit connmark conntrack dscp ecn esp hashlimit helper icmp iprange length owner policy realm state tos ttl unclean CLASSIFY CONNMARK DNAT DSCP ECN LOG MARK MASQUERADE MIRROR NETMAP NFQUEUE REDIRECT REJECT SAME SNAT TCPMSS TOS TTL TRACE ULOG +PF6_EXT_SLIB:=connlimit connmark eui64 hl icmp6 length owner policy state CONNMARK HL LOG NFQUEUE MARK TCPMSS TRACE +PFX_EXT_SLIB:=limit mac mark multiport physdev pkttype sctp standard tcp tcpmss udp NOTRACK ifeq ($(DO_SELINUX), 1) PF_EXT_SE_SLIB:=SECMARK CONNSECMARK diff --git a/extensions/libip6t_limit.c b/extensions/libip6t_limit.c deleted file mode 100644 index c1f70143..00000000 --- a/extensions/libip6t_limit.c +++ /dev/null @@ -1,195 +0,0 @@ -/* Shared library add-on to iptables to add limit support. - * - * Jérôme de Vivie - * Hervé Eychenne - */ - -#include -#include -#include -#include -#include -#include -#include -/* For 64bit kernel / 32bit userspace */ -#include "../include/linux/netfilter_ipv6/ip6t_limit.h" - -#define IP6T_LIMIT_AVG "3/hour" -#define IP6T_LIMIT_BURST 5 - -/* Function which prints out usage message. */ -static void -help(void) -{ - printf( -"limit v%s options:\n" -"--limit avg max average match rate: default "IP6T_LIMIT_AVG"\n" -" [Packets per second unless followed by \n" -" /sec /minute /hour /day postfixes]\n" -"--limit-burst number number to match in a burst, default %u\n" -"\n", IPTABLES_VERSION, IP6T_LIMIT_BURST); -} - -static struct option opts[] = { - { "limit", 1, 0, '%' }, - { "limit-burst", 1, 0, '$' }, - { 0 } -}; - -static -int parse_rate(const char *rate, u_int32_t *val) -{ - const char *delim; - u_int32_t r; - u_int32_t mult = 1; /* Seconds by default. */ - - delim = strchr(rate, '/'); - if (delim) { - if (strlen(delim+1) == 0) - return 0; - - if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0) - mult = 1; - else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0) - mult = 60; - else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0) - mult = 60*60; - else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0) - mult = 24*60*60; - else - return 0; - } - r = atoi(rate); - if (!r) - return 0; - - /* This would get mapped to infinite (1/day is minimum they - can specify, so we're ok at that end). */ - if (r / mult > IP6T_LIMIT_SCALE) - exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate); - - *val = IP6T_LIMIT_SCALE * mult / r; - return 1; -} - -/* Initialize the match. */ -static void -init(struct xt_entry_match *m, unsigned int *nfcache) -{ - struct ip6t_rateinfo *r = (struct ip6t_rateinfo *)m->data; - - parse_rate(IP6T_LIMIT_AVG, &r->avg); - r->burst = IP6T_LIMIT_BURST; - -} - -/* FIXME: handle overflow: - if (r->avg*r->burst/r->burst != r->avg) - exit_error(PARAMETER_PROBLEM, - "Sorry: burst too large for that avg rate.\n"); -*/ - -/* Function which parses command options; returns true if it - ate an option */ -static int -parse(int c, char **argv, int invert, unsigned int *flags, - const void *entry, - unsigned int *nfcache, - struct xt_entry_match **match) -{ - struct ip6t_rateinfo *r = (struct ip6t_rateinfo *)(*match)->data; - unsigned int num; - - switch(c) { - case '%': - if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; - if (!parse_rate(optarg, &r->avg)) - exit_error(PARAMETER_PROBLEM, - "bad rate `%s'", optarg); - break; - - case '$': - if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; - if (string_to_number(optarg, 0, 10000, &num) == -1) - exit_error(PARAMETER_PROBLEM, - "bad --limit-burst `%s'", optarg); - r->burst = num; - break; - - default: - return 0; - } - - if (invert) - exit_error(PARAMETER_PROBLEM, - "limit does not support invert"); - - return 1; -} - -/* Final check; nothing. */ -static void final_check(unsigned int flags) -{ -} - -static struct rates -{ - const char *name; - u_int32_t mult; -} rates[] = { { "day", IP6T_LIMIT_SCALE*24*60*60 }, - { "hour", IP6T_LIMIT_SCALE*60*60 }, - { "min", IP6T_LIMIT_SCALE*60 }, - { "sec", IP6T_LIMIT_SCALE } }; - -static void print_rate(u_int32_t period) -{ - unsigned int i; - - for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) { - if (period > rates[i].mult - || rates[i].mult % period != 0) - break; - } - - printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name); -} - -/* Prints out the matchinfo. */ -static void -print(const void *ip, - const struct xt_entry_match *match, - int numeric) -{ - struct ip6t_rateinfo *r = (struct ip6t_rateinfo *)match->data; - printf("limit: avg "); print_rate(r->avg); - printf("burst %u ", r->burst); -} - -/* FIXME: Make minimalist: only print rate if not default --RR */ -static void save(const void *ip, const struct xt_entry_match *match) -{ - struct ip6t_rateinfo *r = (struct ip6t_rateinfo *)match->data; - - printf("--limit "); print_rate(r->avg); - if (r->burst != IP6T_LIMIT_BURST) - printf("--limit-burst %u ", r->burst); -} - -static struct ip6tables_match limit = { - .name = "limit", - .version = IPTABLES_VERSION, - .size = IP6T_ALIGN(sizeof(struct ip6t_rateinfo)), - .userspacesize = offsetof(struct ip6t_rateinfo, prev), - .help = &help, - .init = &init, - .parse = &parse, - .final_check = &final_check, - .print = &print, - .save = &save, - .extra_opts = opts, -}; - -void _init(void) -{ - register_match6(&limit); -} diff --git a/extensions/libipt_limit.c b/extensions/libipt_limit.c deleted file mode 100644 index 92d47ae0..00000000 --- a/extensions/libipt_limit.c +++ /dev/null @@ -1,196 +0,0 @@ -/* Shared library add-on to iptables to add limit support. - * - * Jérôme de Vivie - * Hervé Eychenne - */ - -#include -#include -#include -#include -#include -#include -#include -/* For 64bit kernel / 32bit userspace */ -#include "../include/linux/netfilter_ipv4/ipt_limit.h" - -#define IPT_LIMIT_AVG "3/hour" -#define IPT_LIMIT_BURST 5 - -/* Function which prints out usage message. */ -static void -help(void) -{ - printf( -"limit v%s options:\n" -"--limit avg max average match rate: default "IPT_LIMIT_AVG"\n" -" [Packets per second unless followed by \n" -" /sec /minute /hour /day postfixes]\n" -"--limit-burst number number to match in a burst, default %u\n" -"\n", IPTABLES_VERSION, IPT_LIMIT_BURST); -} - -static struct option opts[] = { - { "limit", 1, 0, '%' }, - { "limit-burst", 1, 0, '$' }, - { 0 } -}; - -static -int parse_rate(const char *rate, u_int32_t *val) -{ - const char *delim; - u_int32_t r; - u_int32_t mult = 1; /* Seconds by default. */ - - delim = strchr(rate, '/'); - if (delim) { - if (strlen(delim+1) == 0) - return 0; - - if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0) - mult = 1; - else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0) - mult = 60; - else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0) - mult = 60*60; - else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0) - mult = 24*60*60; - else - return 0; - } - r = atoi(rate); - if (!r) - return 0; - - /* This would get mapped to infinite (1/day is minimum they - can specify, so we're ok at that end). */ - if (r / mult > IPT_LIMIT_SCALE) - exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate); - - *val = IPT_LIMIT_SCALE * mult / r; - return 1; -} - -/* Initialize the match. */ -static void -init(struct xt_entry_match *m, unsigned int *nfcache) -{ - struct ipt_rateinfo *r = (struct ipt_rateinfo *)m->data; - - parse_rate(IPT_LIMIT_AVG, &r->avg); - r->burst = IPT_LIMIT_BURST; - -} - -/* FIXME: handle overflow: - if (r->avg*r->burst/r->burst != r->avg) - exit_error(PARAMETER_PROBLEM, - "Sorry: burst too large for that avg rate.\n"); -*/ - -/* Function which parses command options; returns true if it - ate an option */ -static int -parse(int c, char **argv, int invert, unsigned int *flags, - const void *entry, - unsigned int *nfcache, - struct xt_entry_match **match) -{ - struct ipt_rateinfo *r = (struct ipt_rateinfo *)(*match)->data; - unsigned int num; - - switch(c) { - case '%': - if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; - if (!parse_rate(optarg, &r->avg)) - exit_error(PARAMETER_PROBLEM, - "bad rate `%s'", optarg); - break; - - case '$': - if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; - if (string_to_number(optarg, 0, 10000, &num) == -1) - exit_error(PARAMETER_PROBLEM, - "bad --limit-burst `%s'", optarg); - r->burst = num; - break; - - default: - return 0; - } - - if (invert) - exit_error(PARAMETER_PROBLEM, - "limit does not support invert"); - - return 1; -} - -/* Final check; nothing. */ -static void final_check(unsigned int flags) -{ -} - -static struct rates -{ - const char *name; - u_int32_t mult; -} rates[] = { { "day", IPT_LIMIT_SCALE*24*60*60 }, - { "hour", IPT_LIMIT_SCALE*60*60 }, - { "min", IPT_LIMIT_SCALE*60 }, - { "sec", IPT_LIMIT_SCALE } }; - -static void print_rate(u_int32_t period) -{ - unsigned int i; - - for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) { - if (period > rates[i].mult - || rates[i].mult/period < rates[i].mult%period) - break; - } - - printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name); -} - -/* Prints out the matchinfo. */ -static void -print(const void *ip, - const struct xt_entry_match *match, - int numeric) -{ - struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data; - printf("limit: avg "); print_rate(r->avg); - printf("burst %u ", r->burst); -} - -/* FIXME: Make minimalist: only print rate if not default --RR */ -static void save(const void *ip, const struct xt_entry_match *match) -{ - struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data; - - printf("--limit "); print_rate(r->avg); - if (r->burst != IPT_LIMIT_BURST) - printf("--limit-burst %u ", r->burst); -} - -static struct iptables_match limit = { - .next = NULL, - .name = "limit", - .version = IPTABLES_VERSION, - .size = IPT_ALIGN(sizeof(struct ipt_rateinfo)), - .userspacesize = offsetof(struct ipt_rateinfo, prev), - .help = &help, - .init = &init, - .parse = &parse, - .final_check = &final_check, - .print = &print, - .save = &save, - .extra_opts = opts -}; - -void _init(void) -{ - register_match(&limit); -} diff --git a/extensions/libxt_limit.c b/extensions/libxt_limit.c new file mode 100644 index 00000000..aa7131d6 --- /dev/null +++ b/extensions/libxt_limit.c @@ -0,0 +1,214 @@ +/* Shared library add-on to iptables to add limit support. + * + * Jérôme de Vivie + * Hervé Eychenne + */ + +#include +#include +#include +#include +#include +#include +#include +/* For 64bit kernel / 32bit userspace */ +#include "../include/linux/netfilter/xt_limit.h" + +#define XT_LIMIT_AVG "3/hour" +#define XT_LIMIT_BURST 5 + +/* Function which prints out usage message. */ +static void +help(void) +{ + printf( +"limit v%s options:\n" +"--limit avg max average match rate: default "XT_LIMIT_AVG"\n" +" [Packets per second unless followed by \n" +" /sec /minute /hour /day postfixes]\n" +"--limit-burst number number to match in a burst, default %u\n" +"\n", IPTABLES_VERSION, XT_LIMIT_BURST); +} + +static struct option opts[] = { + { "limit", 1, 0, '%' }, + { "limit-burst", 1, 0, '$' }, + { 0 } +}; + +static +int parse_rate(const char *rate, u_int32_t *val) +{ + const char *delim; + u_int32_t r; + u_int32_t mult = 1; /* Seconds by default. */ + + delim = strchr(rate, '/'); + if (delim) { + if (strlen(delim+1) == 0) + return 0; + + if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0) + mult = 1; + else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0) + mult = 60; + else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0) + mult = 60*60; + else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0) + mult = 24*60*60; + else + return 0; + } + r = atoi(rate); + if (!r) + return 0; + + /* This would get mapped to infinite (1/day is minimum they + can specify, so we're ok at that end). */ + if (r / mult > XT_LIMIT_SCALE) + exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate); + + *val = XT_LIMIT_SCALE * mult / r; + return 1; +} + +/* Initialize the match. */ +static void +init(struct xt_entry_match *m, unsigned int *nfcache) +{ + struct xt_rateinfo *r = (struct xt_rateinfo *)m->data; + + parse_rate(XT_LIMIT_AVG, &r->avg); + r->burst = XT_LIMIT_BURST; + +} + +/* FIXME: handle overflow: + if (r->avg*r->burst/r->burst != r->avg) + exit_error(PARAMETER_PROBLEM, + "Sorry: burst too large for that avg rate.\n"); +*/ + +/* Function which parses command options; returns true if it + ate an option */ +static int +parse(int c, char **argv, int invert, unsigned int *flags, + const void *entry, + unsigned int *nfcache, + struct xt_entry_match **match) +{ + struct xt_rateinfo *r = (struct xt_rateinfo *)(*match)->data; + unsigned int num; + + switch(c) { + case '%': + if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; + if (!parse_rate(optarg, &r->avg)) + exit_error(PARAMETER_PROBLEM, + "bad rate `%s'", optarg); + break; + + case '$': + if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; + if (string_to_number(optarg, 0, 10000, &num) == -1) + exit_error(PARAMETER_PROBLEM, + "bad --limit-burst `%s'", optarg); + r->burst = num; + break; + + default: + return 0; + } + + if (invert) + exit_error(PARAMETER_PROBLEM, + "limit does not support invert"); + + return 1; +} + +/* Final check; nothing. */ +static void final_check(unsigned int flags) +{ +} + +static struct rates +{ + const char *name; + u_int32_t mult; +} rates[] = { { "day", XT_LIMIT_SCALE*24*60*60 }, + { "hour", XT_LIMIT_SCALE*60*60 }, + { "min", XT_LIMIT_SCALE*60 }, + { "sec", XT_LIMIT_SCALE } }; + +static void print_rate(u_int32_t period) +{ + unsigned int i; + + for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) { + if (period > rates[i].mult + || rates[i].mult/period < rates[i].mult%period) + break; + } + + printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name); +} + +/* Prints out the matchinfo. */ +static void +print(const void *ip, + const struct xt_entry_match *match, + int numeric) +{ + struct xt_rateinfo *r = (struct xt_rateinfo *)match->data; + printf("limit: avg "); print_rate(r->avg); + printf("burst %u ", r->burst); +} + +/* FIXME: Make minimalist: only print rate if not default --RR */ +static void save(const void *ip, const struct xt_entry_match *match) +{ + struct xt_rateinfo *r = (struct xt_rateinfo *)match->data; + + printf("--limit "); print_rate(r->avg); + if (r->burst != XT_LIMIT_BURST) + printf("--limit-burst %u ", r->burst); +} + +static struct xtables_match limit = { + .next = NULL, + .family = AF_INET, + .name = "limit", + .version = IPTABLES_VERSION, + .size = XT_ALIGN(sizeof(struct xt_rateinfo)), + .userspacesize = offsetof(struct xt_rateinfo, prev), + .help = &help, + .init = &init, + .parse = &parse, + .final_check = &final_check, + .print = &print, + .save = &save, + .extra_opts = opts +}; + +static struct xtables_match limit6 = { + .next = NULL, + .family = AF_INET6, + .name = "limit", + .version = IPTABLES_VERSION, + .size = XT_ALIGN(sizeof(struct xt_rateinfo)), + .userspacesize = offsetof(struct xt_rateinfo, prev), + .help = &help, + .init = &init, + .parse = &parse, + .final_check = &final_check, + .print = &print, + .save = &save, + .extra_opts = opts +}; + +void _init(void) +{ + xtables_register_match(&limit); + xtables_register_match(&limit6); +} diff --git a/include/linux/netfilter/xt_limit.h b/include/linux/netfilter/xt_limit.h new file mode 100644 index 00000000..c0aa6d9f --- /dev/null +++ b/include/linux/netfilter/xt_limit.h @@ -0,0 +1,21 @@ +#ifndef _XT_RATE_H +#define _XT_RATE_H + +/* timings are in milliseconds. */ +#define XT_LIMIT_SCALE 10000 + +/* 1/10,000 sec period => max of 10,000/sec. Min rate is then 429490 + seconds, or one every 59 hours. */ +struct xt_rateinfo { + u_int32_t avg; /* Average secs between packets * scale */ + u_int32_t burst; /* Period multiplier for upper limit. */ + + /* Used internally by the kernel */ + unsigned long prev; + /* Ugly, ugly fucker. */ + struct xt_rateinfo *master; + + u_int32_t credit; + u_int32_t credit_cap, cost; +}; +#endif /*_XT_RATE_H*/ diff --git a/include/linux/netfilter_ipv4/ipt_limit.h b/include/linux/netfilter_ipv4/ipt_limit.h deleted file mode 100644 index f46d4b4e..00000000 --- a/include/linux/netfilter_ipv4/ipt_limit.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _IPT_RATE_H -#define _IPT_RATE_H - -/* timings are in milliseconds. */ -#define IPT_LIMIT_SCALE 10000 - -/* 1/10,000 sec period => max of 10,000/sec. Min rate is then 429490 - seconds, or one every 59 hours. */ -struct ipt_rateinfo { - u_int32_t avg; /* Average secs between packets * scale */ - u_int32_t burst; /* Period multiplier for upper limit. */ - - /* Used internally by the kernel */ - unsigned long prev; - /* Ugly, ugly fucker. */ - struct ipt_rateinfo *master; - - u_int32_t credit; - u_int32_t credit_cap, cost; -}; -#endif /*_IPT_RATE_H*/ diff --git a/include/linux/netfilter_ipv6/ip6t_limit.h b/include/linux/netfilter_ipv6/ip6t_limit.h deleted file mode 100644 index 6e17a677..00000000 --- a/include/linux/netfilter_ipv6/ip6t_limit.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _IP6T_RATE_H -#define _IP6T_RATE_H - -/* timings are in milliseconds. */ -#define IP6T_LIMIT_SCALE 10000 - -/* 1/10,000 sec period => max of 10,000/sec. Min rate is then 429490 - seconds, or one every 59 hours. */ -struct ip6t_rateinfo { - u_int32_t avg; /* Average secs between packets * scale */ - u_int32_t burst; /* Period multiplier for upper limit. */ - - /* Used internally by the kernel */ - unsigned long prev; - /* Ugly, ugly fucker. */ - struct ip6t_rateinfo *master; - - u_int32_t credit; - u_int32_t credit_cap, cost; -}; -#endif /*_IPT_RATE_H*/ -- cgit v1.2.3