libxt_connlimit.c revision 7ac405297ec38449b30e3b05fd6bf2082fd3d803
1/* Shared library add-on to iptables to add connection limit support. */ 2#include <stdbool.h> 3#include <stdio.h> 4#include <netdb.h> 5#include <string.h> 6#include <stdlib.h> 7#include <stddef.h> 8#include <getopt.h> 9#include <xtables.h> 10#include <linux/netfilter/xt_connlimit.h> 11 12static void connlimit_help(void) 13{ 14 printf( 15"connlimit match options:\n" 16"[!] --connlimit-above n match if the number of existing " 17" connections is (not) above n\n" 18" --connlimit-mask n group hosts using mask\n"); 19} 20 21static const struct option connlimit_opts[] = { 22 {.name = "connlimit-above", .has_arg = true, .val = 'A'}, 23 {.name = "connlimit-mask", .has_arg = true, .val = 'M'}, 24 XT_GETOPT_TABLEEND, 25}; 26 27static void connlimit_init(struct xt_entry_match *match) 28{ 29 struct xt_connlimit_info *info = (void *)match->data; 30 31 /* This will also initialize the v4 mask correctly */ 32 memset(info->v6_mask, 0xFF, sizeof(info->v6_mask)); 33} 34 35static void prefix_to_netmask(uint32_t *mask, unsigned int prefix_len) 36{ 37 if (prefix_len == 0) { 38 mask[0] = mask[1] = mask[2] = mask[3] = 0; 39 } else if (prefix_len <= 32) { 40 mask[0] <<= 32 - prefix_len; 41 mask[1] = mask[2] = mask[3] = 0; 42 } else if (prefix_len <= 64) { 43 mask[1] <<= 32 - (prefix_len - 32); 44 mask[2] = mask[3] = 0; 45 } else if (prefix_len <= 96) { 46 mask[2] <<= 32 - (prefix_len - 64); 47 mask[3] = 0; 48 } else if (prefix_len <= 128) { 49 mask[3] <<= 32 - (prefix_len - 96); 50 } 51 mask[0] = htonl(mask[0]); 52 mask[1] = htonl(mask[1]); 53 mask[2] = htonl(mask[2]); 54 mask[3] = htonl(mask[3]); 55} 56 57static int connlimit_parse(int c, char **argv, int invert, unsigned int *flags, 58 struct xt_connlimit_info *info, unsigned int family) 59{ 60 char *err; 61 int i; 62 63 switch (c) { 64 case 'A': 65 if (*flags & 0x1) 66 xtables_error(PARAMETER_PROBLEM, 67 "--connlimit-above may be given only once"); 68 *flags |= 0x1; 69 xtables_check_inverse(optarg, &invert, &optind, 0, argv); 70 info->limit = strtoul(optarg, NULL, 0); 71 info->inverse = invert; 72 break; 73 case 'M': 74 if (*flags & 0x2) 75 xtables_error(PARAMETER_PROBLEM, 76 "--connlimit-mask may be given only once"); 77 78 *flags |= 0x2; 79 i = strtoul(optarg, &err, 0); 80 if (family == NFPROTO_IPV6) { 81 if (i > 128 || *err != '\0') 82 xtables_error(PARAMETER_PROBLEM, 83 "--connlimit-mask must be between " 84 "0 and 128"); 85 prefix_to_netmask(info->v6_mask, i); 86 } else { 87 if (i > 32 || *err != '\0') 88 xtables_error(PARAMETER_PROBLEM, 89 "--connlimit-mask must be between " 90 "0 and 32"); 91 if (i == 0) 92 info->v4_mask = 0; 93 else 94 info->v4_mask = htonl(0xFFFFFFFF << (32 - i)); 95 } 96 break; 97 default: 98 return 0; 99 } 100 101 return 1; 102} 103 104static int connlimit_parse4(int c, char **argv, int invert, 105 unsigned int *flags, const void *entry, 106 struct xt_entry_match **match) 107{ 108 return connlimit_parse(c, argv, invert, flags, 109 (void *)(*match)->data, NFPROTO_IPV4); 110} 111 112static int connlimit_parse6(int c, char **argv, int invert, 113 unsigned int *flags, const void *entry, 114 struct xt_entry_match **match) 115{ 116 return connlimit_parse(c, argv, invert, flags, 117 (void *)(*match)->data, NFPROTO_IPV6); 118} 119 120static void connlimit_check(unsigned int flags) 121{ 122 if (!(flags & 0x1)) 123 xtables_error(PARAMETER_PROBLEM, 124 "You must specify \"--connlimit-above\""); 125} 126 127static unsigned int count_bits4(uint32_t mask) 128{ 129 unsigned int bits = 0; 130 131 for (mask = ~ntohl(mask); mask != 0; mask >>= 1) 132 ++bits; 133 134 return 32 - bits; 135} 136 137static unsigned int count_bits6(const uint32_t *mask) 138{ 139 unsigned int bits = 0, i; 140 uint32_t tmp[4]; 141 142 for (i = 0; i < 4; ++i) 143 for (tmp[i] = ~ntohl(mask[i]); tmp[i] != 0; tmp[i] >>= 1) 144 ++bits; 145 return 128 - bits; 146} 147 148static void connlimit_print4(const void *ip, 149 const struct xt_entry_match *match, int numeric) 150{ 151 const struct xt_connlimit_info *info = (const void *)match->data; 152 153 printf("#conn/%u %s %u ", count_bits4(info->v4_mask), 154 info->inverse ? "<=" : ">", info->limit); 155} 156 157static void connlimit_print6(const void *ip, 158 const struct xt_entry_match *match, int numeric) 159{ 160 const struct xt_connlimit_info *info = (const void *)match->data; 161 printf("#conn/%u %s %u ", count_bits6(info->v6_mask), 162 info->inverse ? "<=" : ">", info->limit); 163} 164 165static void connlimit_save4(const void *ip, const struct xt_entry_match *match) 166{ 167 const struct xt_connlimit_info *info = (const void *)match->data; 168 169 printf("%s--connlimit-above %u --connlimit-mask %u ", 170 info->inverse ? "! " : "", info->limit, 171 count_bits4(info->v4_mask)); 172} 173 174static void connlimit_save6(const void *ip, const struct xt_entry_match *match) 175{ 176 const struct xt_connlimit_info *info = (const void *)match->data; 177 178 printf("%s--connlimit-above %u --connlimit-mask %u ", 179 info->inverse ? "! " : "", info->limit, 180 count_bits6(info->v6_mask)); 181} 182 183static struct xtables_match connlimit_mt_reg[] = { 184 { 185 .name = "connlimit", 186 .family = NFPROTO_IPV4, 187 .version = XTABLES_VERSION, 188 .size = XT_ALIGN(sizeof(struct xt_connlimit_info)), 189 .userspacesize = offsetof(struct xt_connlimit_info, data), 190 .help = connlimit_help, 191 .init = connlimit_init, 192 .parse = connlimit_parse4, 193 .final_check = connlimit_check, 194 .print = connlimit_print4, 195 .save = connlimit_save4, 196 .extra_opts = connlimit_opts, 197 }, 198 { 199 .name = "connlimit", 200 .family = NFPROTO_IPV6, 201 .version = XTABLES_VERSION, 202 .size = XT_ALIGN(sizeof(struct xt_connlimit_info)), 203 .userspacesize = offsetof(struct xt_connlimit_info, data), 204 .help = connlimit_help, 205 .init = connlimit_init, 206 .parse = connlimit_parse6, 207 .final_check = connlimit_check, 208 .print = connlimit_print6, 209 .save = connlimit_save6, 210 .extra_opts = connlimit_opts, 211 }, 212}; 213 214void _init(void) 215{ 216 xtables_register_matches(connlimit_mt_reg, ARRAY_SIZE(connlimit_mt_reg)); 217} 218