Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add limit support. |
| 2 | * |
Jan Engelhardt | 81bd588 | 2008-09-04 17:49:18 +0200 | [diff] [blame] | 3 | * Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr> |
| 4 | * Hervé Eychenne <rv@wallfire.org> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 5 | */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 6 | #include <stdbool.h> |
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 */ |
Jan Engelhardt | a2a7f2b | 2008-09-01 14:20:13 +0200 | [diff] [blame] | 15 | #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 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 20 | static void limit_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 21 | { |
| 22 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 23 | "limit match options:\n" |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 24 | "--limit avg max average match rate: default "XT_LIMIT_AVG"\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 25 | " [Packets per second unless followed by \n" |
| 26 | " /sec /minute /hour /day postfixes]\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 27 | "--limit-burst number number to match in a burst, default %u\n", |
| 28 | XT_LIMIT_BURST); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 31 | static const struct option limit_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 32 | {.name = "limit", .has_arg = true, .val = '%'}, |
| 33 | {.name = "limit-burst", .has_arg = true, .val = '$'}, |
| 34 | XT_GETOPT_TABLEEND, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 38 | int parse_rate(const char *rate, uint32_t *val) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 39 | { |
| 40 | const char *delim; |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 41 | uint32_t r; |
| 42 | uint32_t mult = 1; /* Seconds by default. */ |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 43 | |
| 44 | delim = strchr(rate, '/'); |
| 45 | if (delim) { |
| 46 | if (strlen(delim+1) == 0) |
| 47 | return 0; |
| 48 | |
| 49 | if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0) |
| 50 | mult = 1; |
| 51 | else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0) |
| 52 | mult = 60; |
| 53 | else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0) |
| 54 | mult = 60*60; |
| 55 | else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0) |
| 56 | mult = 24*60*60; |
| 57 | else |
| 58 | return 0; |
| 59 | } |
| 60 | r = atoi(rate); |
| 61 | if (!r) |
| 62 | return 0; |
| 63 | |
| 64 | /* This would get mapped to infinite (1/day is minimum they |
| 65 | can specify, so we're ok at that end). */ |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 66 | if (r / mult > XT_LIMIT_SCALE) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 67 | xtables_error(PARAMETER_PROBLEM, "Rate too fast \"%s\"\n", rate); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 68 | |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 69 | *val = XT_LIMIT_SCALE * mult / r; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 70 | return 1; |
| 71 | } |
| 72 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 73 | static void limit_init(struct xt_entry_match *m) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 74 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 75 | struct xt_rateinfo *r = (struct xt_rateinfo *)m->data; |
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 | parse_rate(XT_LIMIT_AVG, &r->avg); |
| 78 | r->burst = XT_LIMIT_BURST; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 79 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* FIXME: handle overflow: |
| 83 | if (r->avg*r->burst/r->burst != r->avg) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 84 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 85 | "Sorry: burst too large for that avg rate.\n"); |
| 86 | */ |
| 87 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 88 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 89 | limit_parse(int c, char **argv, int invert, unsigned int *flags, |
| 90 | const void *entry, struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 91 | { |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 92 | struct xt_rateinfo *r = (struct xt_rateinfo *)(*match)->data; |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 93 | unsigned int num; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 94 | |
| 95 | switch(c) { |
| 96 | case '%': |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 97 | if (xtables_check_inverse(optarg, &invert, &optind, 0, argv)) break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 98 | if (!parse_rate(optarg, &r->avg)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 99 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 100 | "bad rate `%s'", optarg); |
Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 101 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 102 | |
| 103 | case '$': |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 104 | if (xtables_check_inverse(optarg, &invert, &optind, 0, argv)) break; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 105 | if (!xtables_strtoui(optarg, NULL, &num, 0, 10000)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 106 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 107 | "bad --limit-burst `%s'", optarg); |
| 108 | r->burst = num; |
| 109 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 112 | if (invert) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 113 | xtables_error(PARAMETER_PROBLEM, |
Phil Oester | 35160ee | 2004-09-21 10:43:45 +0000 | [diff] [blame] | 114 | "limit does not support invert"); |
| 115 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 116 | return 1; |
| 117 | } |
| 118 | |
Jan Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 119 | static const struct rates |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 120 | { |
| 121 | const char *name; |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 122 | uint32_t mult; |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 123 | } rates[] = { { "day", XT_LIMIT_SCALE*24*60*60 }, |
| 124 | { "hour", XT_LIMIT_SCALE*60*60 }, |
| 125 | { "min", XT_LIMIT_SCALE*60 }, |
| 126 | { "sec", XT_LIMIT_SCALE } }; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 128 | static void print_rate(uint32_t period) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 129 | { |
| 130 | unsigned int i; |
| 131 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 132 | for (i = 1; i < ARRAY_SIZE(rates); ++i) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 133 | if (period > rates[i].mult |
Harald Welte | 1412e45 | 2001-10-16 08:26:37 +0000 | [diff] [blame] | 134 | || rates[i].mult/period < rates[i].mult%period) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 135 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 136 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 137 | printf(" %u/%s", rates[i-1].mult / period, rates[i-1].name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 140 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 141 | 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] | 142 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 143 | const struct xt_rateinfo *r = (const void *)match->data; |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 144 | printf(" limit: avg"); print_rate(r->avg); |
| 145 | printf(" burst %u", r->burst); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 148 | 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] | 149 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 150 | const struct xt_rateinfo *r = (const void *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 151 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 152 | printf(" --limit"); print_rate(r->avg); |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 153 | if (r->burst != XT_LIMIT_BURST) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 154 | printf(" --limit-burst %u", r->burst); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 157 | static struct xtables_match limit_match = { |
Jan Engelhardt | 4297936 | 2009-06-01 11:56:23 +0200 | [diff] [blame] | 158 | .family = NFPROTO_UNSPEC, |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 159 | .name = "limit", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 160 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 4489c0d | 2007-07-24 07:11:26 +0000 | [diff] [blame] | 161 | .size = XT_ALIGN(sizeof(struct xt_rateinfo)), |
| 162 | .userspacesize = offsetof(struct xt_rateinfo, prev), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 163 | .help = limit_help, |
| 164 | .init = limit_init, |
| 165 | .parse = limit_parse, |
| 166 | .print = limit_print, |
| 167 | .save = limit_save, |
| 168 | .extra_opts = limit_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | void _init(void) |
| 172 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 173 | xtables_register_match(&limit_match); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 174 | } |