Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add limit support. |
| 2 | * |
| 3 | * Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr> |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 4 | * Hervé Eychenne <rv@wallfire.org> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 5 | */ |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 6 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <getopt.h> |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 11 | #include <xtables.h> |
Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 12 | #include <stddef.h> |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 13 | #include <linux/netfilter/x_tables.h> |
Martin Josefsson | 1da399c | 2004-05-26 15:50:57 +0000 | [diff] [blame] | 14 | /* For 64bit kernel / 32bit userspace */ |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 15 | #include "../include/linux/netfilter/xt_limit.h" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 16 | |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 17 | #define XT_LIMIT_AVG "3/hour" |
| 18 | #define XT_LIMIT_BURST 5 |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 19 | |
| 20 | /* Function which prints out usage message. */ |
| 21 | static void |
| 22 | help(void) |
| 23 | { |
| 24 | printf( |
| 25 | "limit v%s options:\n" |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 26 | "--limit avg max average match rate: default "XT_LIMIT_AVG"\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 27 | " [Packets per second unless followed by \n" |
| 28 | " /sec /minute /hour /day postfixes]\n" |
| 29 | "--limit-burst number number to match in a burst, default %u\n" |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 30 | "\n", IPTABLES_VERSION, XT_LIMIT_BURST); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Jan Engelhardt | 661f112 | 2007-07-30 14:46:51 +0000 | [diff] [blame] | 33 | static const struct option opts[] = { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 34 | { "limit", 1, 0, '%' }, |
| 35 | { "limit-burst", 1, 0, '$' }, |
| 36 | { 0 } |
| 37 | }; |
| 38 | |
| 39 | static |
| 40 | int parse_rate(const char *rate, u_int32_t *val) |
| 41 | { |
| 42 | const char *delim; |
| 43 | u_int32_t r; |
| 44 | u_int32_t mult = 1; /* Seconds by default. */ |
| 45 | |
| 46 | delim = strchr(rate, '/'); |
| 47 | if (delim) { |
| 48 | if (strlen(delim+1) == 0) |
| 49 | return 0; |
| 50 | |
| 51 | if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0) |
| 52 | mult = 1; |
| 53 | else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0) |
| 54 | mult = 60; |
| 55 | else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0) |
| 56 | mult = 60*60; |
| 57 | else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0) |
| 58 | mult = 24*60*60; |
| 59 | else |
| 60 | return 0; |
| 61 | } |
| 62 | r = atoi(rate); |
| 63 | if (!r) |
| 64 | return 0; |
| 65 | |
| 66 | /* This would get mapped to infinite (1/day is minimum they |
| 67 | can specify, so we're ok at that end). */ |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 68 | if (r / mult > XT_LIMIT_SCALE) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 69 | exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate); |
| 70 | |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 71 | *val = XT_LIMIT_SCALE * mult / r; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | /* Initialize the match. */ |
| 76 | static void |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 77 | init(struct xt_entry_match *m) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 78 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 79 | struct xt_rateinfo *r = (struct xt_rateinfo *)m->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 80 | |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 81 | parse_rate(XT_LIMIT_AVG, &r->avg); |
| 82 | r->burst = XT_LIMIT_BURST; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 83 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* FIXME: handle overflow: |
| 87 | if (r->avg*r->burst/r->burst != r->avg) |
| 88 | exit_error(PARAMETER_PROBLEM, |
| 89 | "Sorry: burst too large for that avg rate.\n"); |
| 90 | */ |
| 91 | |
| 92 | /* Function which parses command options; returns true if it |
| 93 | ate an option */ |
| 94 | static int |
| 95 | parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 96 | const void *entry, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 97 | struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 98 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 99 | struct xt_rateinfo *r = (struct xt_rateinfo *)(*match)->data; |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 100 | unsigned int num; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 101 | |
| 102 | switch(c) { |
| 103 | case '%': |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 104 | if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 105 | if (!parse_rate(optarg, &r->avg)) |
| 106 | exit_error(PARAMETER_PROBLEM, |
| 107 | "bad rate `%s'", optarg); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 108 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 109 | |
| 110 | case '$': |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 111 | if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; |
Marc Boucher | 06e6394 | 2001-07-23 15:34:46 +0000 | [diff] [blame] | 112 | if (string_to_number(optarg, 0, 10000, &num) == -1) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 113 | exit_error(PARAMETER_PROBLEM, |
| 114 | "bad --limit-burst `%s'", optarg); |
| 115 | r->burst = num; |
| 116 | break; |
| 117 | |
| 118 | default: |
| 119 | return 0; |
| 120 | } |
| 121 | |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 122 | if (invert) |
| 123 | exit_error(PARAMETER_PROBLEM, |
| 124 | "limit does not support invert"); |
| 125 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | /* Final check; nothing. */ |
| 130 | static void final_check(unsigned int flags) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | static struct rates |
| 135 | { |
| 136 | const char *name; |
| 137 | u_int32_t mult; |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 138 | } rates[] = { { "day", XT_LIMIT_SCALE*24*60*60 }, |
| 139 | { "hour", XT_LIMIT_SCALE*60*60 }, |
| 140 | { "min", XT_LIMIT_SCALE*60 }, |
| 141 | { "sec", XT_LIMIT_SCALE } }; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 142 | |
| 143 | static void print_rate(u_int32_t period) |
| 144 | { |
| 145 | unsigned int i; |
| 146 | |
| 147 | for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) { |
| 148 | if (period > rates[i].mult |
Harald Welte | 1412e45 | 2001-10-16 08:26:37 +0000 | [diff] [blame] | 149 | || rates[i].mult/period < rates[i].mult%period) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | |
| 153 | printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name); |
| 154 | } |
| 155 | |
| 156 | /* Prints out the matchinfo. */ |
| 157 | static void |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 158 | print(const void *ip, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 159 | const struct xt_entry_match *match, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 160 | int numeric) |
| 161 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 162 | struct xt_rateinfo *r = (struct xt_rateinfo *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 163 | printf("limit: avg "); print_rate(r->avg); |
| 164 | printf("burst %u ", r->burst); |
| 165 | } |
| 166 | |
| 167 | /* FIXME: Make minimalist: only print rate if not default --RR */ |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 168 | static void save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 169 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 170 | struct xt_rateinfo *r = (struct xt_rateinfo *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 171 | |
| 172 | printf("--limit "); print_rate(r->avg); |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 173 | if (r->burst != XT_LIMIT_BURST) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 174 | printf("--limit-burst %u ", r->burst); |
| 175 | } |
| 176 | |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 177 | static struct xtables_match limit = { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 178 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 179 | .name = "limit", |
| 180 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 181 | .size = XT_ALIGN(sizeof(struct xt_rateinfo)), |
| 182 | .userspacesize = offsetof(struct xt_rateinfo, prev), |
| 183 | .help = &help, |
| 184 | .init = &init, |
| 185 | .parse = &parse, |
| 186 | .final_check = &final_check, |
| 187 | .print = &print, |
| 188 | .save = &save, |
| 189 | .extra_opts = opts |
| 190 | }; |
| 191 | |
| 192 | static struct xtables_match limit6 = { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 193 | .family = AF_INET6, |
| 194 | .name = "limit", |
| 195 | .version = IPTABLES_VERSION, |
| 196 | .size = XT_ALIGN(sizeof(struct xt_rateinfo)), |
| 197 | .userspacesize = offsetof(struct xt_rateinfo, prev), |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 198 | .help = &help, |
| 199 | .init = &init, |
| 200 | .parse = &parse, |
| 201 | .final_check = &final_check, |
| 202 | .print = &print, |
| 203 | .save = &save, |
| 204 | .extra_opts = opts |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | void _init(void) |
| 208 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 209 | xtables_register_match(&limit); |
| 210 | xtables_register_match(&limit6); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 211 | } |