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. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 21 | static void limit_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | { |
| 23 | printf( |
| 24 | "limit v%s options:\n" |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 25 | "--limit avg max average match rate: default "XT_LIMIT_AVG"\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 26 | " [Packets per second unless followed by \n" |
| 27 | " /sec /minute /hour /day postfixes]\n" |
| 28 | "--limit-burst number number to match in a burst, default %u\n" |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 29 | "\n", IPTABLES_VERSION, XT_LIMIT_BURST); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 32 | static const struct option limit_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 33 | { "limit", 1, NULL, '%' }, |
| 34 | { "limit-burst", 1, NULL, '$' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 35 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static |
| 39 | int parse_rate(const char *rate, u_int32_t *val) |
| 40 | { |
| 41 | const char *delim; |
| 42 | u_int32_t r; |
| 43 | u_int32_t mult = 1; /* Seconds by default. */ |
| 44 | |
| 45 | delim = strchr(rate, '/'); |
| 46 | if (delim) { |
| 47 | if (strlen(delim+1) == 0) |
| 48 | return 0; |
| 49 | |
| 50 | if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0) |
| 51 | mult = 1; |
| 52 | else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0) |
| 53 | mult = 60; |
| 54 | else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0) |
| 55 | mult = 60*60; |
| 56 | else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0) |
| 57 | mult = 24*60*60; |
| 58 | else |
| 59 | return 0; |
| 60 | } |
| 61 | r = atoi(rate); |
| 62 | if (!r) |
| 63 | return 0; |
| 64 | |
| 65 | /* This would get mapped to infinite (1/day is minimum they |
| 66 | can specify, so we're ok at that end). */ |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 67 | if (r / mult > XT_LIMIT_SCALE) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 68 | exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate); |
| 69 | |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 70 | *val = XT_LIMIT_SCALE * mult / r; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | /* Initialize the match. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 75 | static void limit_init(struct xt_entry_match *m) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 76 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 77 | struct xt_rateinfo *r = (struct xt_rateinfo *)m->data; |
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 | parse_rate(XT_LIMIT_AVG, &r->avg); |
| 80 | r->burst = XT_LIMIT_BURST; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 81 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* FIXME: handle overflow: |
| 85 | if (r->avg*r->burst/r->burst != r->avg) |
| 86 | exit_error(PARAMETER_PROBLEM, |
| 87 | "Sorry: burst too large for that avg rate.\n"); |
| 88 | */ |
| 89 | |
| 90 | /* Function which parses command options; returns true if it |
| 91 | ate an option */ |
| 92 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 93 | limit_parse(int c, char **argv, int invert, unsigned int *flags, |
| 94 | const void *entry, struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 95 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 96 | struct xt_rateinfo *r = (struct xt_rateinfo *)(*match)->data; |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 97 | unsigned int num; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 98 | |
| 99 | switch(c) { |
| 100 | case '%': |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 101 | if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 102 | if (!parse_rate(optarg, &r->avg)) |
| 103 | exit_error(PARAMETER_PROBLEM, |
| 104 | "bad rate `%s'", optarg); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 105 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 106 | |
| 107 | case '$': |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 108 | if (check_inverse(argv[optind-1], &invert, &optind, 0)) break; |
Marc Boucher | 06e6394 | 2001-07-23 15:34:46 +0000 | [diff] [blame] | 109 | if (string_to_number(optarg, 0, 10000, &num) == -1) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 110 | exit_error(PARAMETER_PROBLEM, |
| 111 | "bad --limit-burst `%s'", optarg); |
| 112 | r->burst = num; |
| 113 | break; |
| 114 | |
| 115 | default: |
| 116 | return 0; |
| 117 | } |
| 118 | |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 119 | if (invert) |
| 120 | exit_error(PARAMETER_PROBLEM, |
| 121 | "limit does not support invert"); |
| 122 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 123 | return 1; |
| 124 | } |
| 125 | |
Jan Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 126 | static const struct rates |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | { |
| 128 | const char *name; |
| 129 | u_int32_t mult; |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 130 | } rates[] = { { "day", XT_LIMIT_SCALE*24*60*60 }, |
| 131 | { "hour", XT_LIMIT_SCALE*60*60 }, |
| 132 | { "min", XT_LIMIT_SCALE*60 }, |
| 133 | { "sec", XT_LIMIT_SCALE } }; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 134 | |
| 135 | static void print_rate(u_int32_t period) |
| 136 | { |
| 137 | unsigned int i; |
| 138 | |
| 139 | for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) { |
| 140 | if (period > rates[i].mult |
Harald Welte | 1412e45 | 2001-10-16 08:26:37 +0000 | [diff] [blame] | 141 | || rates[i].mult/period < rates[i].mult%period) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | |
| 145 | printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name); |
| 146 | } |
| 147 | |
| 148 | /* Prints out the matchinfo. */ |
| 149 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 150 | limit_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 151 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 152 | struct xt_rateinfo *r = (struct xt_rateinfo *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 153 | printf("limit: avg "); print_rate(r->avg); |
| 154 | printf("burst %u ", r->burst); |
| 155 | } |
| 156 | |
| 157 | /* FIXME: Make minimalist: only print rate if not default --RR */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 158 | static void limit_save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 159 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 160 | struct xt_rateinfo *r = (struct xt_rateinfo *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 161 | |
| 162 | printf("--limit "); print_rate(r->avg); |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 163 | if (r->burst != XT_LIMIT_BURST) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 164 | printf("--limit-burst %u ", r->burst); |
| 165 | } |
| 166 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 167 | static struct xtables_match limit_match = { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 168 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 169 | .name = "limit", |
| 170 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 171 | .size = XT_ALIGN(sizeof(struct xt_rateinfo)), |
| 172 | .userspacesize = offsetof(struct xt_rateinfo, prev), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 173 | .help = limit_help, |
| 174 | .init = limit_init, |
| 175 | .parse = limit_parse, |
| 176 | .print = limit_print, |
| 177 | .save = limit_save, |
| 178 | .extra_opts = limit_opts, |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 181 | static struct xtables_match limit_match6 = { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 182 | .family = AF_INET6, |
| 183 | .name = "limit", |
| 184 | .version = IPTABLES_VERSION, |
| 185 | .size = XT_ALIGN(sizeof(struct xt_rateinfo)), |
| 186 | .userspacesize = offsetof(struct xt_rateinfo, prev), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 187 | .help = limit_help, |
| 188 | .init = limit_init, |
| 189 | .parse = limit_parse, |
| 190 | .print = limit_print, |
| 191 | .save = limit_save, |
| 192 | .extra_opts = limit_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | void _init(void) |
| 196 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 197 | xtables_register_match(&limit_match); |
| 198 | xtables_register_match(&limit_match6); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 199 | } |