Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add recent matching support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | |
| 8 | #include <iptables.h> |
| 9 | #include <linux/netfilter_ipv4/ipt_recent.h> |
| 10 | |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 11 | /* Need these in order to not fail when compiling against an older kernel. */ |
Harald Welte | 122e7c0 | 2003-03-30 20:26:42 +0000 | [diff] [blame] | 12 | #ifndef RECENT_NAME |
| 13 | #define RECENT_NAME "ipt_recent" |
| 14 | #endif /* RECENT_NAME */ |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 15 | |
Harald Welte | 122e7c0 | 2003-03-30 20:26:42 +0000 | [diff] [blame] | 16 | #ifndef RECENT_VER |
| 17 | #define RECENT_VER "unknown" |
| 18 | #endif /* RECENT_VER */ |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 19 | |
Harald Welte | 122e7c0 | 2003-03-30 20:26:42 +0000 | [diff] [blame] | 20 | #ifndef IPT_RECENT_NAME_LEN |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 21 | #define IPT_RECENT_NAME_LEN 200 |
Harald Welte | 122e7c0 | 2003-03-30 20:26:42 +0000 | [diff] [blame] | 22 | #endif /* IPT_RECENT_NAME_LEN */ |
| 23 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 24 | static const struct option recent_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 25 | { .name = "set", .has_arg = 0, .val = 201 }, |
| 26 | { .name = "rcheck", .has_arg = 0, .val = 202 }, |
| 27 | { .name = "update", .has_arg = 0, .val = 203 }, |
| 28 | { .name = "seconds", .has_arg = 1, .val = 204 }, |
| 29 | { .name = "hitcount", .has_arg = 1, .val = 205 }, |
| 30 | { .name = "remove", .has_arg = 0, .val = 206 }, |
| 31 | { .name = "rttl", .has_arg = 0, .val = 207 }, |
| 32 | { .name = "name", .has_arg = 1, .val = 208 }, |
| 33 | { .name = "rsource", .has_arg = 0, .val = 209 }, |
| 34 | { .name = "rdest", .has_arg = 0, .val = 210 }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 35 | { .name = NULL } |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 38 | static void recent_help(void) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 39 | { |
| 40 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 41 | "recent match options:\n" |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 42 | "[!] --set Add source address to list, always matches.\n" |
| 43 | "[!] --rcheck Match if source address in list.\n" |
| 44 | "[!] --update Match if source address in list, also update last-seen time.\n" |
| 45 | "[!] --remove Match if source address in list, also removes that address from list.\n" |
| 46 | " --seconds seconds For check and update commands above.\n" |
| 47 | " Specifies that the match will only occur if source address last seen within\n" |
| 48 | " the last 'seconds' seconds.\n" |
| 49 | " --hitcount hits For check and update commands above.\n" |
| 50 | " Specifies that the match will only occur if source address seen hits times.\n" |
Fabrice MARIE | ae31bb6 | 2002-06-14 07:38:16 +0000 | [diff] [blame] | 51 | " May be used in conjunction with the seconds option.\n" |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 52 | " --rttl For check and update commands above.\n" |
| 53 | " Specifies that the match will only occur if the source address and the TTL\n" |
| 54 | " match between this packet and the one which was set.\n" |
| 55 | " Useful if you have problems with people spoofing their source address in order\n" |
| 56 | " to DoS you via this module.\n" |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 57 | " --name name Name of the recent list to be used. DEFAULT used if none given.\n" |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 58 | " --rsource Match/Save the source address of each packet in the recent list table (default).\n" |
| 59 | " --rdest Match/Save the destination address of each packet in the recent list table.\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 60 | RECENT_NAME " " RECENT_VER ": Stephen Frost <sfrost@snowman.net>. http://snowman.net/projects/ipt_recent/\n"); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 61 | } |
Jan Engelhardt | ddac6c5 | 2008-09-01 14:22:19 +0200 | [diff] [blame^] | 62 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 63 | static void recent_init(struct xt_entry_match *match) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 64 | { |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 65 | struct ipt_recent_info *info = (struct ipt_recent_info *)(match)->data; |
| 66 | |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 67 | |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 68 | strncpy(info->name,"DEFAULT",IPT_RECENT_NAME_LEN); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 69 | /* eventhough IPT_RECENT_NAME_LEN is currently defined as 200, |
| 70 | * better be safe, than sorry */ |
| 71 | info->name[IPT_RECENT_NAME_LEN-1] = '\0'; |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 72 | info->side = IPT_RECENT_SOURCE; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 75 | #define RECENT_CMDS \ |
| 76 | (IPT_RECENT_SET | IPT_RECENT_CHECK | \ |
| 77 | IPT_RECENT_UPDATE | IPT_RECENT_REMOVE) |
| 78 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 79 | static int recent_parse(int c, char **argv, int invert, unsigned int *flags, |
| 80 | const void *entry, struct xt_entry_match **match) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 81 | { |
| 82 | struct ipt_recent_info *info = (struct ipt_recent_info *)(*match)->data; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 83 | switch (c) { |
| 84 | case 201: |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 85 | if (*flags & RECENT_CMDS) |
| 86 | exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 87 | "recent: only one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 88 | "`--update' or `--remove' may be set"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 89 | check_inverse(optarg, &invert, &optind, 0); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 90 | info->check_set |= IPT_RECENT_SET; |
| 91 | if (invert) info->invert = 1; |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 92 | *flags |= IPT_RECENT_SET; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 93 | break; |
| 94 | |
| 95 | case 202: |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 96 | if (*flags & RECENT_CMDS) |
| 97 | exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 98 | "recent: only one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 99 | "`--update' or `--remove' may be set"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 100 | check_inverse(optarg, &invert, &optind, 0); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 101 | info->check_set |= IPT_RECENT_CHECK; |
| 102 | if(invert) info->invert = 1; |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 103 | *flags |= IPT_RECENT_CHECK; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | case 203: |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 107 | if (*flags & RECENT_CMDS) |
| 108 | exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 109 | "recent: only one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 110 | "`--update' or `--remove' may be set"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 111 | check_inverse(optarg, &invert, &optind, 0); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 112 | info->check_set |= IPT_RECENT_UPDATE; |
| 113 | if (invert) info->invert = 1; |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 114 | *flags |= IPT_RECENT_UPDATE; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 115 | break; |
| 116 | |
| 117 | case 206: |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 118 | if (*flags & RECENT_CMDS) |
| 119 | exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 120 | "recent: only one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 121 | "`--update' or `--remove' may be set"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 122 | check_inverse(optarg, &invert, &optind, 0); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 123 | info->check_set |= IPT_RECENT_REMOVE; |
| 124 | if (invert) info->invert = 1; |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 125 | *flags |= IPT_RECENT_REMOVE; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 126 | break; |
| 127 | |
| 128 | case 204: |
| 129 | info->seconds = atoi(optarg); |
| 130 | break; |
| 131 | |
| 132 | case 205: |
| 133 | info->hit_count = atoi(optarg); |
| 134 | break; |
| 135 | |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 136 | case 207: |
| 137 | info->check_set |= IPT_RECENT_TTL; |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 138 | *flags |= IPT_RECENT_TTL; |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 139 | break; |
| 140 | |
| 141 | case 208: |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 142 | strncpy(info->name,optarg,IPT_RECENT_NAME_LEN); |
Karsten Desler | 073df8f | 2004-01-31 15:33:55 +0000 | [diff] [blame] | 143 | info->name[IPT_RECENT_NAME_LEN-1] = '\0'; |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 144 | break; |
| 145 | |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 146 | case 209: |
| 147 | info->side = IPT_RECENT_SOURCE; |
| 148 | break; |
| 149 | |
| 150 | case 210: |
| 151 | info->side = IPT_RECENT_DEST; |
| 152 | break; |
| 153 | |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 154 | default: |
| 155 | return 0; |
| 156 | } |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 157 | |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 158 | return 1; |
| 159 | } |
| 160 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 161 | static void recent_check(unsigned int flags) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 162 | { |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 163 | if (!(flags & RECENT_CMDS)) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 164 | exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 165 | "recent: you must specify one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 166 | "`--update' or `--remove'"); |
Jan Engelhardt | 3c5d15c | 2008-08-04 12:52:27 +0200 | [diff] [blame] | 167 | if ((flags & IPT_RECENT_TTL) && |
| 168 | (flags & (IPT_RECENT_SET | IPT_RECENT_REMOVE | IPT_RECENT_UPDATE))) |
| 169 | exit_error(PARAMETER_PROBLEM, |
| 170 | "recent: --rttl may only be used with --rcheck or " |
| 171 | "--update"); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 174 | static void recent_print(const void *ip, const struct xt_entry_match *match, |
| 175 | int numeric) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 176 | { |
| 177 | struct ipt_recent_info *info = (struct ipt_recent_info *)match->data; |
| 178 | |
Sven Strickroth | 0c1b776 | 2003-06-01 10:11:43 +0000 | [diff] [blame] | 179 | if (info->invert) |
| 180 | fputc('!', stdout); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 181 | |
| 182 | printf("recent: "); |
| 183 | if(info->check_set & IPT_RECENT_SET) printf("SET "); |
| 184 | if(info->check_set & IPT_RECENT_CHECK) printf("CHECK "); |
| 185 | if(info->check_set & IPT_RECENT_UPDATE) printf("UPDATE "); |
| 186 | if(info->check_set & IPT_RECENT_REMOVE) printf("REMOVE "); |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 187 | if(info->seconds) printf("seconds: %d ",info->seconds); |
| 188 | if(info->hit_count) printf("hit_count: %d ",info->hit_count); |
| 189 | if(info->check_set & IPT_RECENT_TTL) printf("TTL-Match "); |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 190 | if(info->name) printf("name: %s ",info->name); |
| 191 | if(info->side == IPT_RECENT_SOURCE) printf("side: source "); |
| 192 | if(info->side == IPT_RECENT_DEST) printf("side: dest"); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 195 | static void recent_save(const void *ip, const struct xt_entry_match *match) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 196 | { |
Sven Strickroth | 0c1b776 | 2003-06-01 10:11:43 +0000 | [diff] [blame] | 197 | struct ipt_recent_info *info = (struct ipt_recent_info *)match->data; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 198 | |
Sven Strickroth | 0c1b776 | 2003-06-01 10:11:43 +0000 | [diff] [blame] | 199 | if (info->invert) |
| 200 | printf("! "); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 201 | |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 202 | if(info->check_set & IPT_RECENT_SET) printf("--set "); |
| 203 | if(info->check_set & IPT_RECENT_CHECK) printf("--rcheck "); |
| 204 | if(info->check_set & IPT_RECENT_UPDATE) printf("--update "); |
| 205 | if(info->check_set & IPT_RECENT_REMOVE) printf("--remove "); |
| 206 | if(info->seconds) printf("--seconds %d ",info->seconds); |
| 207 | if(info->hit_count) printf("--hitcount %d ",info->hit_count); |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 208 | if(info->check_set & IPT_RECENT_TTL) printf("--rttl "); |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame] | 209 | if(info->name) printf("--name %s ",info->name); |
| 210 | if(info->side == IPT_RECENT_SOURCE) printf("--rsource "); |
| 211 | if(info->side == IPT_RECENT_DEST) printf("--rdest "); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 214 | static struct xtables_match recent_mt_reg = { |
Stephen Frost | 27e1fa8 | 2003-04-14 13:33:15 +0000 | [diff] [blame] | 215 | .name = "recent", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 216 | .version = XTABLES_VERSION, |
| 217 | .family = PF_INET, |
| 218 | .size = XT_ALIGN(sizeof(struct ipt_recent_info)), |
| 219 | .userspacesize = XT_ALIGN(sizeof(struct ipt_recent_info)), |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 220 | .help = recent_help, |
| 221 | .init = recent_init, |
| 222 | .parse = recent_parse, |
| 223 | .final_check = recent_check, |
| 224 | .print = recent_print, |
| 225 | .save = recent_save, |
| 226 | .extra_opts = recent_opts, |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | void _init(void) |
| 230 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 231 | xtables_register_match(&recent_mt_reg); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 232 | } |