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 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "recent v%s options:\n" |
| 17 | "[!] --set Add source address to list, always matches.\n" |
| 18 | "[!] --rcheck Match if source address in list.\n" |
| 19 | "[!] --update Match if source address in list, also update last-seen time.\n" |
| 20 | "[!] --remove Match if source address in list, also removes that address from list.\n" |
| 21 | " --seconds seconds For check and update commands above.\n" |
| 22 | " Specifies that the match will only occur if source address last seen within\n" |
| 23 | " the last 'seconds' seconds.\n" |
| 24 | " --hitcount hits For check and update commands above.\n" |
| 25 | " 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] | 26 | " May be used in conjunction with the seconds option.\n" |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 27 | " --rttl For check and update commands above.\n" |
| 28 | " Specifies that the match will only occur if the source address and the TTL\n" |
| 29 | " match between this packet and the one which was set.\n" |
| 30 | " Useful if you have problems with people spoofing their source address in order\n" |
| 31 | " to DoS you via this module.\n" |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 32 | " --name name Name of the recent list to be used. DEFAULT used if none given.\n" |
| 33 | " --rsource Save the source address of each packet in the recent list table (default).\n" |
| 34 | " --rdest Save the destination address of each packet in the recent list table.\n" |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 35 | RECENT_NAME " " RECENT_VER ": Stephen Frost <sfrost@snowman.net>. http://snowman.net/projects/ipt_recent/\n" |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 36 | , |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 37 | IPTABLES_VERSION); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 38 | |
| 39 | } |
| 40 | |
| 41 | static struct option opts[] = { |
| 42 | { "set", 0, 0, 201 }, |
| 43 | { "rcheck", 0, 0, 202 }, |
| 44 | { "update", 0, 0, 203 }, |
| 45 | { "seconds", 1, 0, 204 }, |
| 46 | { "hitcount", 1, 0, 205 }, |
| 47 | { "remove",0, 0, 206 }, |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 48 | { "rttl",0, 0, 207}, |
| 49 | { "name", 1, 0, 208}, |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 50 | { "rsource", 0, 0, 209}, |
| 51 | { "rdest", 0, 0, 210}, |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 52 | {0} |
| 53 | }; |
| 54 | |
| 55 | /* Initialize the match. */ |
| 56 | static void |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 57 | init(struct ipt_entry_match *match, unsigned int *nfcache) |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 58 | { |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 59 | struct ipt_recent_info *info = (struct ipt_recent_info *)(match)->data; |
| 60 | |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 61 | *nfcache |= NFC_UNKNOWN; |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 62 | |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 63 | strncpy(info->name,"DEFAULT",IPT_RECENT_NAME_LEN); |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 64 | info->side = IPT_RECENT_SOURCE; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* Function which parses command options; returns true if it |
| 68 | ate an option */ |
| 69 | static int |
| 70 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 71 | const struct ipt_entry *entry, |
| 72 | unsigned int *nfcache, |
| 73 | struct ipt_entry_match **match) |
| 74 | { |
| 75 | struct ipt_recent_info *info = (struct ipt_recent_info *)(*match)->data; |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 76 | switch (c) { |
| 77 | case 201: |
| 78 | if (*flags) exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 79 | "recent: only one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 80 | "`--update' or `--remove' may be set"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 81 | check_inverse(optarg, &invert, &optind, 0); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 82 | info->check_set |= IPT_RECENT_SET; |
| 83 | if (invert) info->invert = 1; |
| 84 | *flags = 1; |
| 85 | break; |
| 86 | |
| 87 | case 202: |
| 88 | if (*flags) exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 89 | "recent: only one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 90 | "`--update' or `--remove' may be set"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 91 | check_inverse(optarg, &invert, &optind, 0); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 92 | info->check_set |= IPT_RECENT_CHECK; |
| 93 | if(invert) info->invert = 1; |
| 94 | *flags = 1; |
| 95 | break; |
| 96 | |
| 97 | case 203: |
| 98 | if (*flags) exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 99 | "recent: only one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 100 | "`--update' or `--remove' may be set"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 101 | check_inverse(optarg, &invert, &optind, 0); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 102 | info->check_set |= IPT_RECENT_UPDATE; |
| 103 | if (invert) info->invert = 1; |
| 104 | *flags = 1; |
| 105 | break; |
| 106 | |
| 107 | case 206: |
| 108 | if (*flags) 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_REMOVE; |
| 113 | if (invert) info->invert = 1; |
| 114 | *flags = 1; |
| 115 | break; |
| 116 | |
| 117 | case 204: |
| 118 | info->seconds = atoi(optarg); |
| 119 | break; |
| 120 | |
| 121 | case 205: |
| 122 | info->hit_count = atoi(optarg); |
| 123 | break; |
| 124 | |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 125 | case 207: |
| 126 | info->check_set |= IPT_RECENT_TTL; |
| 127 | break; |
| 128 | |
| 129 | case 208: |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 130 | strncpy(info->name,optarg,IPT_RECENT_NAME_LEN); |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 131 | break; |
| 132 | |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 133 | case 209: |
| 134 | info->side = IPT_RECENT_SOURCE; |
| 135 | break; |
| 136 | |
| 137 | case 210: |
| 138 | info->side = IPT_RECENT_DEST; |
| 139 | break; |
| 140 | |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 141 | default: |
| 142 | return 0; |
| 143 | } |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 144 | |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | /* Final check; must have specified a specific option. */ |
| 149 | static void |
| 150 | final_check(unsigned int flags) |
| 151 | { |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 152 | |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 153 | if (!flags) |
| 154 | exit_error(PARAMETER_PROBLEM, |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 155 | "recent: you must specify one of `--set', `--rcheck' " |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 156 | "`--update' or `--remove'"); |
| 157 | } |
| 158 | |
| 159 | /* Prints out the matchinfo. */ |
| 160 | static void |
| 161 | print(const struct ipt_ip *ip, |
| 162 | const struct ipt_entry_match *match, |
| 163 | int numeric) |
| 164 | { |
| 165 | struct ipt_recent_info *info = (struct ipt_recent_info *)match->data; |
| 166 | |
| 167 | if (info->invert) fputc('!', stdout); |
| 168 | |
| 169 | printf("recent: "); |
| 170 | if(info->check_set & IPT_RECENT_SET) printf("SET "); |
| 171 | if(info->check_set & IPT_RECENT_CHECK) printf("CHECK "); |
| 172 | if(info->check_set & IPT_RECENT_UPDATE) printf("UPDATE "); |
| 173 | if(info->check_set & IPT_RECENT_REMOVE) printf("REMOVE "); |
Stephen Frost | 4fce44c | 2002-02-04 11:58:22 +0000 | [diff] [blame] | 174 | if(info->seconds) printf("seconds: %d ",info->seconds); |
| 175 | if(info->hit_count) printf("hit_count: %d ",info->hit_count); |
| 176 | if(info->check_set & IPT_RECENT_TTL) printf("TTL-Match "); |
Stephen Frost | 7fdbc95 | 2002-06-21 17:26:33 +0000 | [diff] [blame] | 177 | if(info->name) printf("name: %s ",info->name); |
| 178 | if(info->side == IPT_RECENT_SOURCE) printf("side: source "); |
| 179 | if(info->side == IPT_RECENT_DEST) printf("side: dest"); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 183 | static void |
| 184 | save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 185 | { |
| 186 | struct ipt_recent_info *info = (struct ipt_recent_info *)match; |
| 187 | |
| 188 | if (info->invert) fputc('!', stdout); |
| 189 | |
| 190 | printf("recent: "); |
Stephen Frost | d590395 | 2003-03-03 07:24:27 +0000 | [diff] [blame^] | 191 | if(info->check_set & IPT_RECENT_SET) printf("--set "); |
| 192 | if(info->check_set & IPT_RECENT_CHECK) printf("--rcheck "); |
| 193 | if(info->check_set & IPT_RECENT_UPDATE) printf("--update "); |
| 194 | if(info->check_set & IPT_RECENT_REMOVE) printf("--remove "); |
| 195 | if(info->seconds) printf("--seconds %d ",info->seconds); |
| 196 | if(info->hit_count) printf("--hitcount %d ",info->hit_count); |
| 197 | if(info->check_set & IPT_RECENT_TTL) printf("-rttl "); |
| 198 | if(info->name) printf("--name %s ",info->name); |
| 199 | if(info->side == IPT_RECENT_SOURCE) printf("--rsource "); |
| 200 | if(info->side == IPT_RECENT_DEST) printf("--rdest "); |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static |
| 204 | struct iptables_match recent |
| 205 | = { NULL, |
| 206 | "recent", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 207 | IPTABLES_VERSION, |
Stephen Frost | 93c7e5a | 2001-11-08 22:35:03 +0000 | [diff] [blame] | 208 | IPT_ALIGN(sizeof(struct ipt_recent_info)), |
| 209 | IPT_ALIGN(sizeof(struct ipt_recent_info)), |
| 210 | &help, |
| 211 | &init, |
| 212 | &parse, |
| 213 | &final_check, |
| 214 | &print, |
| 215 | &save, |
| 216 | opts |
| 217 | }; |
| 218 | |
| 219 | void _init(void) |
| 220 | { |
| 221 | register_match(&recent); |
| 222 | } |