| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add byte tracking support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | #include <iptables.h> |
| 8 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 9 | #include <linux/netfilter_ipv4/ipt_connbytes.h> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "connbytes v%s options:\n" |
| 17 | " [!] --connbytes from:[to]\n" |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 18 | " --connbytes-dir [original, reply, both]\n" |
| 19 | " --connbytes-mode [packets, bytes, avgpkt]\n" |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 20 | "\n", IPTABLES_VERSION); |
| 21 | } |
| 22 | |
| 23 | static struct option opts[] = { |
| 24 | { "connbytes", 1, 0, '1' }, |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 25 | { "connbytes-dir", 1, 0, '2' }, |
| 26 | { "connbytes-mode", 1, 0, '3' }, |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 27 | {0} |
| 28 | }; |
| 29 | |
| 30 | /* Initialize the match. */ |
| 31 | static void |
| 32 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 33 | { |
| 34 | /* Can't cache this */ |
| 35 | *nfcache |= NFC_UNKNOWN; |
| 36 | } |
| 37 | |
| 38 | static void |
| 39 | parse_range(const char *arg, struct ipt_connbytes_info *si) |
| 40 | { |
| 41 | char *colon,*p; |
| 42 | |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 43 | si->count.from = strtoul(arg,&colon,10); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 44 | if (*colon != ':') |
| 45 | exit_error(PARAMETER_PROBLEM, "Bad range `%s'", arg); |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 46 | si->count.to = strtoul(colon+1,&p,10); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 47 | if (p == colon+1) { |
| 48 | /* second number omited */ |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 49 | si->count.to = 0xffffffff; |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 50 | } |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 51 | if (si->count.from > si->count.to) |
| 52 | exit_error(PARAMETER_PROBLEM, "%llu should be less than %llu", |
| 53 | si->count.from, si->count.to); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* Function which parses command options; returns true if it |
| 57 | ate an option */ |
| 58 | static int |
| 59 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 60 | const struct ipt_entry *entry, |
| 61 | unsigned int *nfcache, |
| 62 | struct ipt_entry_match **match) |
| 63 | { |
| 64 | struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)(*match)->data; |
| Martin Josefsson | 1146088 | 2004-05-08 14:02:36 +0000 | [diff] [blame] | 65 | unsigned long i; |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 66 | |
| 67 | switch (c) { |
| 68 | case '1': |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 69 | if (check_inverse(optarg, &invert, &optind, 0)) |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 70 | optind++; |
| 71 | |
| 72 | parse_range(argv[optind-1], sinfo); |
| 73 | if (invert) { |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 74 | i = sinfo->count.from; |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 75 | sinfo->count.from = sinfo->count.to; |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 76 | sinfo->count.to = i; |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 77 | } |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 78 | *flags |= 1; |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 79 | break; |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 80 | case '2': |
| 81 | if (!strcmp(optarg, "original")) |
| 82 | sinfo->direction = IPT_CONNBYTES_DIR_ORIGINAL; |
| 83 | else if (!strcmp(optarg, "reply")) |
| 84 | sinfo->direction = IPT_CONNBYTES_DIR_REPLY; |
| 85 | else if (!strcmp(optarg, "both")) |
| 86 | sinfo->direction = IPT_CONNBYTES_DIR_BOTH; |
| 87 | else |
| 88 | exit_error(PARAMETER_PROBLEM, |
| 89 | "Unknown --connbytes-dir `%s'", optarg); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 90 | |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 91 | *flags |= 2; |
| 92 | break; |
| 93 | case '3': |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 94 | if (!strcmp(optarg, "packets")) |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 95 | sinfo->what = IPT_CONNBYTES_WHAT_PKTS; |
| 96 | else if (!strcmp(optarg, "bytes")) |
| 97 | sinfo->what = IPT_CONNBYTES_WHAT_BYTES; |
| 98 | else if (!strcmp(optarg, "avgpkt")) |
| 99 | sinfo->what = IPT_CONNBYTES_WHAT_AVGPKT; |
| 100 | else |
| 101 | exit_error(PARAMETER_PROBLEM, |
| 102 | "Unknown --connbytes-mode `%s'", optarg); |
| 103 | *flags |= 4; |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 104 | default: |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | static void final_check(unsigned int flags) |
| 112 | { |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 113 | if (flags != 7) |
| 114 | exit_error(PARAMETER_PROBLEM, "You must specify `--connbytes'" |
| 115 | "`--connbytes-direction' and `--connbytes-mode'"); |
| 116 | } |
| 117 | |
| 118 | static void print_mode(struct ipt_connbytes_info *sinfo) |
| 119 | { |
| 120 | switch (sinfo->what) { |
| 121 | case IPT_CONNBYTES_WHAT_PKTS: |
| 122 | fputs("packets ", stdout); |
| 123 | break; |
| 124 | case IPT_CONNBYTES_WHAT_BYTES: |
| 125 | fputs("bytes ", stdout); |
| 126 | break; |
| 127 | case IPT_CONNBYTES_WHAT_AVGPKT: |
| 128 | fputs("avgpkt ", stdout); |
| 129 | break; |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 130 | default: |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 131 | fputs("unknown ", stdout); |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 132 | break; |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | static void print_direction(struct ipt_connbytes_info *sinfo) |
| 137 | { |
| 138 | switch (sinfo->direction) { |
| 139 | case IPT_CONNBYTES_DIR_ORIGINAL: |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 140 | fputs("original ", stdout); |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 141 | break; |
| 142 | case IPT_CONNBYTES_DIR_REPLY: |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 143 | fputs("reply ", stdout); |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 144 | break; |
| 145 | case IPT_CONNBYTES_DIR_BOTH: |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 146 | fputs("both ", stdout); |
| 147 | break; |
| 148 | default: |
| 149 | fputs("unknown ", stdout); |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* Prints out the matchinfo. */ |
| 155 | static void |
| 156 | print(const struct ipt_ip *ip, |
| 157 | const struct ipt_entry_match *match, |
| 158 | int numeric) |
| 159 | { |
| 160 | struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)match->data; |
| 161 | |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 162 | if (sinfo->count.from > sinfo->count.to) |
| 163 | printf("connbytes ! %llu:%llu ", sinfo->count.to, |
| 164 | sinfo->count.from); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 165 | else |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 166 | printf("connbytes %llu:%llu ",sinfo->count.from, |
| 167 | sinfo->count.to); |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 168 | |
| 169 | fputs("connbytes mode ", stdout); |
| 170 | print_mode(sinfo); |
| 171 | |
| 172 | fputs("connbytes direction ", stdout); |
| 173 | print_direction(sinfo); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* Saves the matchinfo in parsable form to stdout. */ |
| 177 | static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 178 | { |
| 179 | struct ipt_connbytes_info *sinfo = (struct ipt_connbytes_info *)match->data; |
| 180 | |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 181 | if (sinfo->count.from > sinfo->count.to) |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 182 | printf("! --connbytes %llu:%llu ", sinfo->count.to, |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 183 | sinfo->count.from); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 184 | else |
| Harald Welte | 7dc57e2 | 2004-11-18 22:59:36 +0000 | [diff] [blame] | 185 | printf("--connbytes %llu:%llu ", sinfo->count.from, |
| Harald Welte | 93f4a3d | 2004-11-18 22:50:01 +0000 | [diff] [blame] | 186 | sinfo->count.to); |
| 187 | |
| 188 | fputs("--connbytes-mode ", stdout); |
| 189 | print_mode(sinfo); |
| 190 | |
| 191 | fputs("--connbytes-direction ", stdout); |
| 192 | print_direction(sinfo); |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame^] | 195 | static struct iptables_match state = { |
| 196 | .next = NULL, |
| 197 | .name = "connbytes", |
| 198 | .version = IPTABLES_VERSION, |
| 199 | .size = IPT_ALIGN(sizeof(struct ipt_connbytes_info)), |
| 200 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_connbytes_info)), |
| 201 | .help = &help, |
| 202 | .init = &init, |
| 203 | .parse = &parse, |
| 204 | .final_check = &final_check, |
| 205 | .print = &print, |
| 206 | .save = &save, |
| 207 | .extra_opts = opts |
| Martin Devera | 766113a | 2003-06-19 12:23:37 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | void _init(void) |
| 211 | { |
| 212 | register_match(&state); |
| 213 | } |