blob: a569f86aa6b223e007c4473d918cd0adb9b44a24 [file] [log] [blame]
Jan Engelhardt6053fe02007-07-31 16:47:38 +00001#include <stdio.h>
2#include <netdb.h>
3#include <string.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01004#include <xtables.h>
Jan Engelhardta2a7f2b2008-09-01 14:20:13 +02005#include <linux/netfilter/xt_connlimit.h>
Jan Engelhardt6053fe02007-07-31 16:47:38 +00006
Jan Engelhardt2cae5332011-01-18 18:04:57 +01007enum {
Jan Engelhardt27adf1e2011-05-01 21:52:25 +02008 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 Engelhardt2cae5332011-01-18 18:04:57 +010018};
19
Jan Engelhardt6053fe02007-07-31 16:47:38 +000020static void connlimit_help(void)
21{
22 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020023"connlimit match options:\n"
Jan Engelhardt2cae5332011-01-18 18:04:57 +010024" --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 Engelhardt5da9e632011-01-19 02:09:39 +010026" --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 Engelhardt6053fe02007-07-31 16:47:38 +000029}
30
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020031#define s struct xt_connlimit_info
32static 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 Engelhardt6053fe02007-07-31 16:47:38 +000046};
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020047#undef s
Jan Engelhardt6053fe02007-07-31 16:47:38 +000048
Jan Engelhardt926bde82007-10-04 16:26:33 +000049static void connlimit_init(struct xt_entry_match *match)
Jan Engelhardt6053fe02007-07-31 16:47:38 +000050{
51 struct xt_connlimit_info *info = (void *)match->data;
kd6lvwa3726812009-06-07 14:23:00 +020052
53 /* This will also initialize the v4 mask correctly */
54 memset(info->v6_mask, 0xFF, sizeof(info->v6_mask));
Jan Engelhardt6053fe02007-07-31 16:47:38 +000055}
56
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020057static void connlimit_parse(struct xt_option_call *cb, uint8_t family)
Jan Engelhardt6053fe02007-07-31 16:47:38 +000058{
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020059 struct xt_connlimit_info *info = cb->data;
60 const unsigned int revision = (*cb->match)->u.user.revision;
Jan Engelhardt6053fe02007-07-31 16:47:38 +000061
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020062 xtables_option_parse(cb);
63 switch (cb->entry->id) {
64 case O_ABOVE:
65 if (cb->invert)
Jan Engelhardt5da9e632011-01-19 02:09:39 +010066 info->flags |= XT_CONNLIMIT_INVERT;
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020067 break;
68 case O_UPTO:
69 if (!cb->invert)
Jan Engelhardt5da9e632011-01-19 02:09:39 +010070 info->flags |= XT_CONNLIMIT_INVERT;
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020071 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 Engelhardt5da9e632011-01-19 02:09:39 +010077 info->flags &= ~XT_CONNLIMIT_DADDR;
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020078 break;
79 case O_DADDR:
Jan Engelhardt5da9e632011-01-19 02:09:39 +010080 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 Engelhardt27adf1e2011-05-01 21:52:25 +020085 break;
Jan Engelhardt6053fe02007-07-31 16:47:38 +000086 }
Jan Engelhardt6053fe02007-07-31 16:47:38 +000087}
88
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020089static void connlimit_parse4(struct xt_option_call *cb)
Jan Engelhardt6053fe02007-07-31 16:47:38 +000090{
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020091 return connlimit_parse(cb, NFPROTO_IPV4);
Jan Engelhardt6053fe02007-07-31 16:47:38 +000092}
93
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020094static void connlimit_parse6(struct xt_option_call *cb)
Jan Engelhardt6053fe02007-07-31 16:47:38 +000095{
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020096 return connlimit_parse(cb, NFPROTO_IPV6);
Jan Engelhardt6053fe02007-07-31 16:47:38 +000097}
98
Jan Engelhardt27adf1e2011-05-01 21:52:25 +020099static void connlimit_check(struct xt_fcheck_call *cb)
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000100{
Jan Engelhardt27adf1e2011-05-01 21:52:25 +0200101 if ((cb->xflags & (F_UPTO | F_ABOVE)) == 0)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100102 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt27adf1e2011-05-01 21:52:25 +0200103 "You must specify \"--connlimit-above\" or "
104 "\"--connlimit-upto\".");
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000105}
106
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100107static unsigned int count_bits4(uint32_t mask)
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000108{
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 Engelhardt7ac40522011-01-07 12:34:04 +0100117static unsigned int count_bits6(const uint32_t *mask)
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000118{
119 unsigned int bits = 0, i;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100120 uint32_t tmp[4];
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000121
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
128static 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 Engelhardt73866352010-12-18 02:04:59 +0100133 printf(" #conn %s/%u %s %u",
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100134 (info->flags & XT_CONNLIMIT_DADDR) ? "dst" : "src",
135 count_bits4(info->v4_mask),
136 (info->flags & XT_CONNLIMIT_INVERT) ? "<=" : ">", info->limit);
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000137}
138
139static 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 Engelhardt5da9e632011-01-19 02:09:39 +0100143
Jan Engelhardt73866352010-12-18 02:04:59 +0100144 printf(" #conn %s/%u %s %u",
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100145 (info->flags & XT_CONNLIMIT_DADDR) ? "dst" : "src",
146 count_bits6(info->v6_mask),
147 (info->flags & XT_CONNLIMIT_INVERT) ? "<=" : ">", info->limit);
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000148}
149
150static 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 Engelhardt5da9e632011-01-19 02:09:39 +0100153 const int revision = match->u.user.revision;
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000154
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100155 if (info->flags & XT_CONNLIMIT_INVERT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100156 printf(" --connlimit-upto %u", info->limit);
Jan Engelhardt2cae5332011-01-18 18:04:57 +0100157 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100158 printf(" --connlimit-above %u", info->limit);
159 printf(" --connlimit-mask %u", count_bits4(info->v4_mask));
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100160 if (revision >= 1) {
161 if (info->flags & XT_CONNLIMIT_DADDR)
Jan Engelhardt73866352010-12-18 02:04:59 +0100162 printf(" --connlimit-daddr");
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100163 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100164 printf(" --connlimit-saddr");
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100165 }
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000166}
167
168static 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 Engelhardt5da9e632011-01-19 02:09:39 +0100171 const int revision = match->u.user.revision;
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000172
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100173 if (info->flags & XT_CONNLIMIT_INVERT)
Jan Engelhardt73866352010-12-18 02:04:59 +0100174 printf(" --connlimit-upto %u", info->limit);
Jan Engelhardt2cae5332011-01-18 18:04:57 +0100175 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100176 printf(" --connlimit-above %u", info->limit);
177 printf(" --connlimit-mask %u", count_bits6(info->v6_mask));
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100178 if (revision >= 1) {
179 if (info->flags & XT_CONNLIMIT_DADDR)
Jan Engelhardt73866352010-12-18 02:04:59 +0100180 printf(" --connlimit-daddr");
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100181 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100182 printf(" --connlimit-saddr");
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100183 }
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000184}
185
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200186static struct xtables_match connlimit_mt_reg[] = {
187 {
188 .name = "connlimit",
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100189 .revision = 0,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200190 .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 Engelhardt27adf1e2011-05-01 21:52:25 +0200196 .x6_parse = connlimit_parse4,
197 .x6_fcheck = connlimit_check,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200198 .print = connlimit_print4,
199 .save = connlimit_save4,
Jan Engelhardt27adf1e2011-05-01 21:52:25 +0200200 .x6_options = connlimit_opts,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200201 },
202 {
203 .name = "connlimit",
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100204 .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 Engelhardt27adf1e2011-05-01 21:52:25 +0200211 .x6_parse = connlimit_parse6,
212 .x6_fcheck = connlimit_check,
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100213 .print = connlimit_print6,
214 .save = connlimit_save6,
Jan Engelhardt27adf1e2011-05-01 21:52:25 +0200215 .x6_options = connlimit_opts,
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100216 },
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 Engelhardt27adf1e2011-05-01 21:52:25 +0200226 .x6_parse = connlimit_parse4,
227 .x6_fcheck = connlimit_check,
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100228 .print = connlimit_print4,
229 .save = connlimit_save4,
Jan Engelhardt27adf1e2011-05-01 21:52:25 +0200230 .x6_options = connlimit_opts,
Jan Engelhardt5da9e632011-01-19 02:09:39 +0100231 },
232 {
233 .name = "connlimit",
234 .revision = 1,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200235 .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 Engelhardt27adf1e2011-05-01 21:52:25 +0200241 .x6_parse = connlimit_parse6,
242 .x6_fcheck = connlimit_check,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200243 .print = connlimit_print6,
244 .save = connlimit_save6,
Jan Engelhardt27adf1e2011-05-01 21:52:25 +0200245 .x6_options = connlimit_opts,
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200246 },
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000247};
248
249void _init(void)
250{
Jan Engelhardtf2a77522009-06-25 20:12:12 +0200251 xtables_register_matches(connlimit_mt_reg, ARRAY_SIZE(connlimit_mt_reg));
Jan Engelhardt6053fe02007-07-31 16:47:38 +0000252}