blob: 76f9771e6ea85217c60cf8d8e53744da9d01e6f9 [file] [log] [blame]
Stephen Frost93c7e5a2001-11-08 22:35:03 +00001/* 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 Frost27e1fa82003-04-14 13:33:15 +000011/* Need these in order to not fail when compiling against an older kernel. */
Harald Welte122e7c02003-03-30 20:26:42 +000012#ifndef RECENT_NAME
13#define RECENT_NAME "ipt_recent"
14#endif /* RECENT_NAME */
Stephen Frost27e1fa82003-04-14 13:33:15 +000015
Harald Welte122e7c02003-03-30 20:26:42 +000016#ifndef RECENT_VER
17#define RECENT_VER "unknown"
18#endif /* RECENT_VER */
Stephen Frost27e1fa82003-04-14 13:33:15 +000019
Harald Welte122e7c02003-03-30 20:26:42 +000020#ifndef IPT_RECENT_NAME_LEN
Stephen Frost27e1fa82003-04-14 13:33:15 +000021#define IPT_RECENT_NAME_LEN 200
Harald Welte122e7c02003-03-30 20:26:42 +000022#endif /* IPT_RECENT_NAME_LEN */
23
Stephen Frost27e1fa82003-04-14 13:33:15 +000024/* Options for this module */
Jan Engelhardt59d16402007-10-04 16:28:39 +000025static const struct option recent_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000026 { .name = "set", .has_arg = 0, .val = 201 },
27 { .name = "rcheck", .has_arg = 0, .val = 202 },
28 { .name = "update", .has_arg = 0, .val = 203 },
29 { .name = "seconds", .has_arg = 1, .val = 204 },
30 { .name = "hitcount", .has_arg = 1, .val = 205 },
31 { .name = "remove", .has_arg = 0, .val = 206 },
32 { .name = "rttl", .has_arg = 0, .val = 207 },
33 { .name = "name", .has_arg = 1, .val = 208 },
34 { .name = "rsource", .has_arg = 0, .val = 209 },
35 { .name = "rdest", .has_arg = 0, .val = 210 },
36 { }
Stephen Frost27e1fa82003-04-14 13:33:15 +000037};
38
Stephen Frost93c7e5a2001-11-08 22:35:03 +000039/* Function which prints out usage message. */
Jan Engelhardt59d16402007-10-04 16:28:39 +000040static void recent_help(void)
Stephen Frost93c7e5a2001-11-08 22:35:03 +000041{
42 printf(
43"recent v%s options:\n"
44"[!] --set Add source address to list, always matches.\n"
45"[!] --rcheck Match if source address in list.\n"
46"[!] --update Match if source address in list, also update last-seen time.\n"
47"[!] --remove Match if source address in list, also removes that address from list.\n"
48" --seconds seconds For check and update commands above.\n"
49" Specifies that the match will only occur if source address last seen within\n"
50" the last 'seconds' seconds.\n"
51" --hitcount hits For check and update commands above.\n"
52" Specifies that the match will only occur if source address seen hits times.\n"
Fabrice MARIEae31bb62002-06-14 07:38:16 +000053" May be used in conjunction with the seconds option.\n"
Stephen Frost4fce44c2002-02-04 11:58:22 +000054" --rttl For check and update commands above.\n"
55" Specifies that the match will only occur if the source address and the TTL\n"
56" match between this packet and the one which was set.\n"
57" Useful if you have problems with people spoofing their source address in order\n"
58" to DoS you via this module.\n"
Stephen Frost7fdbc952002-06-21 17:26:33 +000059" --name name Name of the recent list to be used. DEFAULT used if none given.\n"
Stephen Frost27e1fa82003-04-14 13:33:15 +000060" --rsource Match/Save the source address of each packet in the recent list table (default).\n"
61" --rdest Match/Save the destination address of each packet in the recent list table.\n"
Stephen Frostd5903952003-03-03 07:24:27 +000062RECENT_NAME " " RECENT_VER ": Stephen Frost <sfrost@snowman.net>. http://snowman.net/projects/ipt_recent/\n"
Stephen Frost7fdbc952002-06-21 17:26:33 +000063,
Harald Welte80fe35d2002-05-29 13:08:15 +000064IPTABLES_VERSION);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000065
66}
67
Stephen Frost93c7e5a2001-11-08 22:35:03 +000068/* Initialize the match. */
Jan Engelhardt59d16402007-10-04 16:28:39 +000069static void recent_init(struct xt_entry_match *match)
Stephen Frost93c7e5a2001-11-08 22:35:03 +000070{
Stephen Frost7fdbc952002-06-21 17:26:33 +000071 struct ipt_recent_info *info = (struct ipt_recent_info *)(match)->data;
72
Stephen Frost7fdbc952002-06-21 17:26:33 +000073
Stephen Frostd5903952003-03-03 07:24:27 +000074 strncpy(info->name,"DEFAULT",IPT_RECENT_NAME_LEN);
Karsten Desler073df8f2004-01-31 15:33:55 +000075 /* eventhough IPT_RECENT_NAME_LEN is currently defined as 200,
76 * better be safe, than sorry */
77 info->name[IPT_RECENT_NAME_LEN-1] = '\0';
Stephen Frost7fdbc952002-06-21 17:26:33 +000078 info->side = IPT_RECENT_SOURCE;
Stephen Frost93c7e5a2001-11-08 22:35:03 +000079}
80
81/* Function which parses command options; returns true if it
82 ate an option */
Jan Engelhardt59d16402007-10-04 16:28:39 +000083static int recent_parse(int c, char **argv, int invert, unsigned int *flags,
84 const void *entry, struct xt_entry_match **match)
Stephen Frost93c7e5a2001-11-08 22:35:03 +000085{
86 struct ipt_recent_info *info = (struct ipt_recent_info *)(*match)->data;
Stephen Frost93c7e5a2001-11-08 22:35:03 +000087 switch (c) {
88 case 201:
89 if (*flags) exit_error(PARAMETER_PROBLEM,
Stephen Frostd5903952003-03-03 07:24:27 +000090 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +000091 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +000092 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000093 info->check_set |= IPT_RECENT_SET;
94 if (invert) info->invert = 1;
95 *flags = 1;
96 break;
97
98 case 202:
99 if (*flags) exit_error(PARAMETER_PROBLEM,
Stephen Frostd5903952003-03-03 07:24:27 +0000100 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000101 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +0000102 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000103 info->check_set |= IPT_RECENT_CHECK;
104 if(invert) info->invert = 1;
105 *flags = 1;
106 break;
107
108 case 203:
109 if (*flags) exit_error(PARAMETER_PROBLEM,
Stephen Frostd5903952003-03-03 07:24:27 +0000110 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000111 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +0000112 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000113 info->check_set |= IPT_RECENT_UPDATE;
114 if (invert) info->invert = 1;
115 *flags = 1;
116 break;
117
118 case 206:
119 if (*flags) exit_error(PARAMETER_PROBLEM,
Stephen Frostd5903952003-03-03 07:24:27 +0000120 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000121 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +0000122 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000123 info->check_set |= IPT_RECENT_REMOVE;
124 if (invert) info->invert = 1;
125 *flags = 1;
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 Frost4fce44c2002-02-04 11:58:22 +0000136 case 207:
137 info->check_set |= IPT_RECENT_TTL;
138 break;
139
140 case 208:
Stephen Frostd5903952003-03-03 07:24:27 +0000141 strncpy(info->name,optarg,IPT_RECENT_NAME_LEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000142 info->name[IPT_RECENT_NAME_LEN-1] = '\0';
Stephen Frost4fce44c2002-02-04 11:58:22 +0000143 break;
144
Stephen Frost7fdbc952002-06-21 17:26:33 +0000145 case 209:
146 info->side = IPT_RECENT_SOURCE;
147 break;
148
149 case 210:
150 info->side = IPT_RECENT_DEST;
151 break;
152
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000153 default:
154 return 0;
155 }
Stephen Frost4fce44c2002-02-04 11:58:22 +0000156
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000157 return 1;
158}
159
160/* Final check; must have specified a specific option. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000161static void recent_check(unsigned int flags)
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000162{
Stephen Frost7fdbc952002-06-21 17:26:33 +0000163
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000164 if (!flags)
165 exit_error(PARAMETER_PROBLEM,
Stephen Frostd5903952003-03-03 07:24:27 +0000166 "recent: you must specify one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000167 "`--update' or `--remove'");
168}
169
170/* Prints out the matchinfo. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000171static void recent_print(const void *ip, const struct xt_entry_match *match,
172 int numeric)
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000173{
174 struct ipt_recent_info *info = (struct ipt_recent_info *)match->data;
175
Sven Strickroth0c1b7762003-06-01 10:11:43 +0000176 if (info->invert)
177 fputc('!', stdout);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000178
179 printf("recent: ");
180 if(info->check_set & IPT_RECENT_SET) printf("SET ");
181 if(info->check_set & IPT_RECENT_CHECK) printf("CHECK ");
182 if(info->check_set & IPT_RECENT_UPDATE) printf("UPDATE ");
183 if(info->check_set & IPT_RECENT_REMOVE) printf("REMOVE ");
Stephen Frost4fce44c2002-02-04 11:58:22 +0000184 if(info->seconds) printf("seconds: %d ",info->seconds);
185 if(info->hit_count) printf("hit_count: %d ",info->hit_count);
186 if(info->check_set & IPT_RECENT_TTL) printf("TTL-Match ");
Stephen Frost7fdbc952002-06-21 17:26:33 +0000187 if(info->name) printf("name: %s ",info->name);
188 if(info->side == IPT_RECENT_SOURCE) printf("side: source ");
189 if(info->side == IPT_RECENT_DEST) printf("side: dest");
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000190}
191
192/* Saves the union ipt_matchinfo in parsable form to stdout. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000193static void recent_save(const void *ip, const struct xt_entry_match *match)
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000194{
Sven Strickroth0c1b7762003-06-01 10:11:43 +0000195 struct ipt_recent_info *info = (struct ipt_recent_info *)match->data;
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000196
Sven Strickroth0c1b7762003-06-01 10:11:43 +0000197 if (info->invert)
198 printf("! ");
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000199
Stephen Frostd5903952003-03-03 07:24:27 +0000200 if(info->check_set & IPT_RECENT_SET) printf("--set ");
201 if(info->check_set & IPT_RECENT_CHECK) printf("--rcheck ");
202 if(info->check_set & IPT_RECENT_UPDATE) printf("--update ");
203 if(info->check_set & IPT_RECENT_REMOVE) printf("--remove ");
204 if(info->seconds) printf("--seconds %d ",info->seconds);
205 if(info->hit_count) printf("--hitcount %d ",info->hit_count);
Stephen Frost27e1fa82003-04-14 13:33:15 +0000206 if(info->check_set & IPT_RECENT_TTL) printf("--rttl ");
Stephen Frostd5903952003-03-03 07:24:27 +0000207 if(info->name) printf("--name %s ",info->name);
208 if(info->side == IPT_RECENT_SOURCE) printf("--rsource ");
209 if(info->side == IPT_RECENT_DEST) printf("--rdest ");
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000210}
211
Stephen Frost27e1fa82003-04-14 13:33:15 +0000212/* Structure for iptables to use to communicate with module */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000213static struct iptables_match recent_match = {
Stephen Frost27e1fa82003-04-14 13:33:15 +0000214 .name = "recent",
215 .version = IPTABLES_VERSION,
216 .size = IPT_ALIGN(sizeof(struct ipt_recent_info)),
217 .userspacesize = IPT_ALIGN(sizeof(struct ipt_recent_info)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000218 .help = recent_help,
219 .init = recent_init,
220 .parse = recent_parse,
221 .final_check = recent_check,
222 .print = recent_print,
223 .save = recent_save,
224 .extra_opts = recent_opts,
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000225};
226
227void _init(void)
228{
Jan Engelhardt59d16402007-10-04 16:28:39 +0000229 register_match(&recent_match);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000230}