Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <netdb.h> |
| 3 | #include <string.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 4 | #include <xtables.h> |
Jan Engelhardt | a2a7f2b | 2008-09-01 14:20:13 +0200 | [diff] [blame] | 5 | #include <linux/netfilter/xt_connlimit.h> |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 6 | |
Jan Engelhardt | 2cae533 | 2011-01-18 18:04:57 +0100 | [diff] [blame] | 7 | enum { |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 8 | O_UPTO = 0, |
| 9 | O_ABOVE, |
| 10 | O_MASK, |
| 11 | O_SADDR, |
| 12 | O_DADDR, |
| 13 | F_UPTO = 1 << O_UPTO, |
| 14 | F_ABOVE = 1 << O_ABOVE, |
| 15 | F_MASK = 1 << O_MASK, |
| 16 | F_SADDR = 1 << O_SADDR, |
| 17 | F_DADDR = 1 << O_DADDR, |
Jan Engelhardt | 2cae533 | 2011-01-18 18:04:57 +0100 | [diff] [blame] | 18 | }; |
| 19 | |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 20 | static void connlimit_help(void) |
| 21 | { |
| 22 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 23 | "connlimit match options:\n" |
Jan Engelhardt | 2cae533 | 2011-01-18 18:04:57 +0100 | [diff] [blame] | 24 | " --connlimit-upto n match if the number of existing connections is 0..n\n" |
| 25 | " --connlimit-above n match if the number of existing connections is >n\n" |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 26 | " --connlimit-mask n group hosts using prefix length (default: max len)\n" |
| 27 | " --connlimit-saddr select source address for grouping\n" |
| 28 | " --connlimit-daddr select destination addresses for grouping\n"); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 31 | #define s struct xt_connlimit_info |
| 32 | static const struct xt_option_entry connlimit_opts[] = { |
| 33 | {.name = "connlimit-upto", .id = O_UPTO, .excl = F_ABOVE, |
| 34 | .type = XTTYPE_UINT32, .flags = XTOPT_INVERT | XTOPT_PUT, |
| 35 | XTOPT_POINTER(s, limit)}, |
| 36 | {.name = "connlimit-above", .id = O_ABOVE, .excl = F_UPTO, |
| 37 | .type = XTTYPE_UINT32, .flags = XTOPT_INVERT | XTOPT_PUT, |
| 38 | XTOPT_POINTER(s, limit)}, |
| 39 | {.name = "connlimit-mask", .id = O_MASK, .type = XTTYPE_PLENMASK, |
| 40 | .flags = XTOPT_PUT, XTOPT_POINTER(s, mask)}, |
| 41 | {.name = "connlimit-saddr", .id = O_SADDR, .excl = F_DADDR, |
| 42 | .type = XTTYPE_NONE}, |
| 43 | {.name = "connlimit-daddr", .id = O_DADDR, .excl = F_SADDR, |
| 44 | .type = XTTYPE_NONE}, |
| 45 | XTOPT_TABLEEND, |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 46 | }; |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 47 | #undef s |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 48 | |
Jan Engelhardt | 926bde8 | 2007-10-04 16:26:33 +0000 | [diff] [blame] | 49 | static void connlimit_init(struct xt_entry_match *match) |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 50 | { |
| 51 | struct xt_connlimit_info *info = (void *)match->data; |
kd6lvw | a372681 | 2009-06-07 14:23:00 +0200 | [diff] [blame] | 52 | |
| 53 | /* This will also initialize the v4 mask correctly */ |
| 54 | memset(info->v6_mask, 0xFF, sizeof(info->v6_mask)); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 57 | static void connlimit_parse(struct xt_option_call *cb, uint8_t family) |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 58 | { |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 59 | struct xt_connlimit_info *info = cb->data; |
| 60 | const unsigned int revision = (*cb->match)->u.user.revision; |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 61 | |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 62 | xtables_option_parse(cb); |
| 63 | switch (cb->entry->id) { |
| 64 | case O_ABOVE: |
| 65 | if (cb->invert) |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 66 | info->flags |= XT_CONNLIMIT_INVERT; |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 67 | break; |
| 68 | case O_UPTO: |
| 69 | if (!cb->invert) |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 70 | info->flags |= XT_CONNLIMIT_INVERT; |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 71 | break; |
| 72 | case O_SADDR: |
| 73 | if (revision < 1) |
| 74 | xtables_error(PARAMETER_PROBLEM, |
| 75 | "xt_connlimit.0 does not support " |
| 76 | "--connlimit-daddr"); |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 77 | info->flags &= ~XT_CONNLIMIT_DADDR; |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 78 | break; |
| 79 | case O_DADDR: |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 80 | if (revision < 1) |
| 81 | xtables_error(PARAMETER_PROBLEM, |
| 82 | "xt_connlimit.0 does not support " |
| 83 | "--connlimit-daddr"); |
| 84 | info->flags |= XT_CONNLIMIT_DADDR; |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 85 | break; |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 86 | } |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 89 | static void connlimit_parse4(struct xt_option_call *cb) |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 90 | { |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 91 | return connlimit_parse(cb, NFPROTO_IPV4); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 94 | static void connlimit_parse6(struct xt_option_call *cb) |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 95 | { |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 96 | return connlimit_parse(cb, NFPROTO_IPV6); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 99 | static void connlimit_check(struct xt_fcheck_call *cb) |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 100 | { |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 101 | if ((cb->xflags & (F_UPTO | F_ABOVE)) == 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 102 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 103 | "You must specify \"--connlimit-above\" or " |
| 104 | "\"--connlimit-upto\"."); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 107 | static unsigned int count_bits4(uint32_t mask) |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 108 | { |
| 109 | unsigned int bits = 0; |
| 110 | |
| 111 | for (mask = ~ntohl(mask); mask != 0; mask >>= 1) |
| 112 | ++bits; |
| 113 | |
| 114 | return 32 - bits; |
| 115 | } |
| 116 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 117 | static unsigned int count_bits6(const uint32_t *mask) |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 118 | { |
| 119 | unsigned int bits = 0, i; |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 120 | uint32_t tmp[4]; |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 121 | |
| 122 | for (i = 0; i < 4; ++i) |
| 123 | for (tmp[i] = ~ntohl(mask[i]); tmp[i] != 0; tmp[i] >>= 1) |
| 124 | ++bits; |
| 125 | return 128 - bits; |
| 126 | } |
| 127 | |
| 128 | static void connlimit_print4(const void *ip, |
| 129 | const struct xt_entry_match *match, int numeric) |
| 130 | { |
| 131 | const struct xt_connlimit_info *info = (const void *)match->data; |
| 132 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 133 | printf(" #conn %s/%u %s %u", |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 134 | (info->flags & XT_CONNLIMIT_DADDR) ? "dst" : "src", |
| 135 | count_bits4(info->v4_mask), |
| 136 | (info->flags & XT_CONNLIMIT_INVERT) ? "<=" : ">", info->limit); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void connlimit_print6(const void *ip, |
| 140 | const struct xt_entry_match *match, int numeric) |
| 141 | { |
| 142 | const struct xt_connlimit_info *info = (const void *)match->data; |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 143 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 144 | printf(" #conn %s/%u %s %u", |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 145 | (info->flags & XT_CONNLIMIT_DADDR) ? "dst" : "src", |
| 146 | count_bits6(info->v6_mask), |
| 147 | (info->flags & XT_CONNLIMIT_INVERT) ? "<=" : ">", info->limit); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void connlimit_save4(const void *ip, const struct xt_entry_match *match) |
| 151 | { |
| 152 | const struct xt_connlimit_info *info = (const void *)match->data; |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 153 | const int revision = match->u.user.revision; |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 154 | |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 155 | if (info->flags & XT_CONNLIMIT_INVERT) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 156 | printf(" --connlimit-upto %u", info->limit); |
Jan Engelhardt | 2cae533 | 2011-01-18 18:04:57 +0100 | [diff] [blame] | 157 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 158 | printf(" --connlimit-above %u", info->limit); |
| 159 | printf(" --connlimit-mask %u", count_bits4(info->v4_mask)); |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 160 | if (revision >= 1) { |
| 161 | if (info->flags & XT_CONNLIMIT_DADDR) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 162 | printf(" --connlimit-daddr"); |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 163 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 164 | printf(" --connlimit-saddr"); |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 165 | } |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void connlimit_save6(const void *ip, const struct xt_entry_match *match) |
| 169 | { |
| 170 | const struct xt_connlimit_info *info = (const void *)match->data; |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 171 | const int revision = match->u.user.revision; |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 172 | |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 173 | if (info->flags & XT_CONNLIMIT_INVERT) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 174 | printf(" --connlimit-upto %u", info->limit); |
Jan Engelhardt | 2cae533 | 2011-01-18 18:04:57 +0100 | [diff] [blame] | 175 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 176 | printf(" --connlimit-above %u", info->limit); |
| 177 | printf(" --connlimit-mask %u", count_bits6(info->v6_mask)); |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 178 | if (revision >= 1) { |
| 179 | if (info->flags & XT_CONNLIMIT_DADDR) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 180 | printf(" --connlimit-daddr"); |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 181 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 182 | printf(" --connlimit-saddr"); |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 183 | } |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 186 | static struct xtables_match connlimit_mt_reg[] = { |
| 187 | { |
| 188 | .name = "connlimit", |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 189 | .revision = 0, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 190 | .family = NFPROTO_IPV4, |
| 191 | .version = XTABLES_VERSION, |
| 192 | .size = XT_ALIGN(sizeof(struct xt_connlimit_info)), |
| 193 | .userspacesize = offsetof(struct xt_connlimit_info, data), |
| 194 | .help = connlimit_help, |
| 195 | .init = connlimit_init, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 196 | .x6_parse = connlimit_parse4, |
| 197 | .x6_fcheck = connlimit_check, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 198 | .print = connlimit_print4, |
| 199 | .save = connlimit_save4, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 200 | .x6_options = connlimit_opts, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 201 | }, |
| 202 | { |
| 203 | .name = "connlimit", |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 204 | .revision = 0, |
| 205 | .family = NFPROTO_IPV6, |
| 206 | .version = XTABLES_VERSION, |
| 207 | .size = XT_ALIGN(sizeof(struct xt_connlimit_info)), |
| 208 | .userspacesize = offsetof(struct xt_connlimit_info, data), |
| 209 | .help = connlimit_help, |
| 210 | .init = connlimit_init, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 211 | .x6_parse = connlimit_parse6, |
| 212 | .x6_fcheck = connlimit_check, |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 213 | .print = connlimit_print6, |
| 214 | .save = connlimit_save6, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 215 | .x6_options = connlimit_opts, |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 216 | }, |
| 217 | { |
| 218 | .name = "connlimit", |
| 219 | .revision = 1, |
| 220 | .family = NFPROTO_IPV4, |
| 221 | .version = XTABLES_VERSION, |
| 222 | .size = XT_ALIGN(sizeof(struct xt_connlimit_info)), |
| 223 | .userspacesize = offsetof(struct xt_connlimit_info, data), |
| 224 | .help = connlimit_help, |
| 225 | .init = connlimit_init, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 226 | .x6_parse = connlimit_parse4, |
| 227 | .x6_fcheck = connlimit_check, |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 228 | .print = connlimit_print4, |
| 229 | .save = connlimit_save4, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 230 | .x6_options = connlimit_opts, |
Jan Engelhardt | 5da9e63 | 2011-01-19 02:09:39 +0100 | [diff] [blame] | 231 | }, |
| 232 | { |
| 233 | .name = "connlimit", |
| 234 | .revision = 1, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 235 | .family = NFPROTO_IPV6, |
| 236 | .version = XTABLES_VERSION, |
| 237 | .size = XT_ALIGN(sizeof(struct xt_connlimit_info)), |
| 238 | .userspacesize = offsetof(struct xt_connlimit_info, data), |
| 239 | .help = connlimit_help, |
| 240 | .init = connlimit_init, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 241 | .x6_parse = connlimit_parse6, |
| 242 | .x6_fcheck = connlimit_check, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 243 | .print = connlimit_print6, |
| 244 | .save = connlimit_save6, |
Jan Engelhardt | 27adf1e | 2011-05-01 21:52:25 +0200 | [diff] [blame] | 245 | .x6_options = connlimit_opts, |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 246 | }, |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | void _init(void) |
| 250 | { |
Jan Engelhardt | f2a7752 | 2009-06-25 20:12:12 +0200 | [diff] [blame] | 251 | xtables_register_matches(connlimit_mt_reg, ARRAY_SIZE(connlimit_mt_reg)); |
Jan Engelhardt | 6053fe0 | 2007-07-31 16:47:38 +0000 | [diff] [blame] | 252 | } |