blob: e38fa319581f6799250584184d83264e28d7007b [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
11/* Function which prints out usage message. */
12static void
13help(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 MARIEae31bb62002-06-14 07:38:16 +000026" May be used in conjunction with the seconds option.\n"
Stephen Frost4fce44c2002-02-04 11:58:22 +000027" --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 Frost7fdbc952002-06-21 17:26:33 +000032" --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 Frostd5903952003-03-03 07:24:27 +000035RECENT_NAME " " RECENT_VER ": Stephen Frost <sfrost@snowman.net>. http://snowman.net/projects/ipt_recent/\n"
Stephen Frost7fdbc952002-06-21 17:26:33 +000036,
Harald Welte80fe35d2002-05-29 13:08:15 +000037IPTABLES_VERSION);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000038
39}
40
41static 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 Frost4fce44c2002-02-04 11:58:22 +000048 { "rttl",0, 0, 207},
49 { "name", 1, 0, 208},
Stephen Frost7fdbc952002-06-21 17:26:33 +000050 { "rsource", 0, 0, 209},
51 { "rdest", 0, 0, 210},
Stephen Frost93c7e5a2001-11-08 22:35:03 +000052 {0}
53};
54
55/* Initialize the match. */
56static void
Stephen Frost7fdbc952002-06-21 17:26:33 +000057init(struct ipt_entry_match *match, unsigned int *nfcache)
Stephen Frost93c7e5a2001-11-08 22:35:03 +000058{
Stephen Frost7fdbc952002-06-21 17:26:33 +000059 struct ipt_recent_info *info = (struct ipt_recent_info *)(match)->data;
60
Stephen Frost93c7e5a2001-11-08 22:35:03 +000061 *nfcache |= NFC_UNKNOWN;
Stephen Frost7fdbc952002-06-21 17:26:33 +000062
Stephen Frostd5903952003-03-03 07:24:27 +000063 strncpy(info->name,"DEFAULT",IPT_RECENT_NAME_LEN);
Stephen Frost7fdbc952002-06-21 17:26:33 +000064 info->side = IPT_RECENT_SOURCE;
Stephen Frost93c7e5a2001-11-08 22:35:03 +000065}
66
67/* Function which parses command options; returns true if it
68 ate an option */
69static int
70parse(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 Frost93c7e5a2001-11-08 22:35:03 +000076 switch (c) {
77 case 201:
78 if (*flags) exit_error(PARAMETER_PROBLEM,
Stephen Frostd5903952003-03-03 07:24:27 +000079 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +000080 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +000081 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000082 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 Frostd5903952003-03-03 07:24:27 +000089 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +000090 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +000091 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000092 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 Frostd5903952003-03-03 07:24:27 +000099 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000100 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +0000101 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000102 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 Frostd5903952003-03-03 07:24:27 +0000109 "recent: only one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000110 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +0000111 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000112 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 Frost4fce44c2002-02-04 11:58:22 +0000125 case 207:
126 info->check_set |= IPT_RECENT_TTL;
127 break;
128
129 case 208:
Stephen Frostd5903952003-03-03 07:24:27 +0000130 strncpy(info->name,optarg,IPT_RECENT_NAME_LEN);
Stephen Frost4fce44c2002-02-04 11:58:22 +0000131 break;
132
Stephen Frost7fdbc952002-06-21 17:26:33 +0000133 case 209:
134 info->side = IPT_RECENT_SOURCE;
135 break;
136
137 case 210:
138 info->side = IPT_RECENT_DEST;
139 break;
140
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000141 default:
142 return 0;
143 }
Stephen Frost4fce44c2002-02-04 11:58:22 +0000144
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000145 return 1;
146}
147
148/* Final check; must have specified a specific option. */
149static void
150final_check(unsigned int flags)
151{
Stephen Frost7fdbc952002-06-21 17:26:33 +0000152
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000153 if (!flags)
154 exit_error(PARAMETER_PROBLEM,
Stephen Frostd5903952003-03-03 07:24:27 +0000155 "recent: you must specify one of `--set', `--rcheck' "
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000156 "`--update' or `--remove'");
157}
158
159/* Prints out the matchinfo. */
160static void
161print(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 Frost4fce44c2002-02-04 11:58:22 +0000174 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 Frost7fdbc952002-06-21 17:26:33 +0000177 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 Frost93c7e5a2001-11-08 22:35:03 +0000180}
181
182/* Saves the union ipt_matchinfo in parsable form to stdout. */
183static void
184save(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 Frostd5903952003-03-03 07:24:27 +0000191 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 Frost93c7e5a2001-11-08 22:35:03 +0000201}
202
203static
204struct iptables_match recent
205= { NULL,
206 "recent",
Harald Welte80fe35d2002-05-29 13:08:15 +0000207 IPTABLES_VERSION,
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000208 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
219void _init(void)
220{
221 register_match(&recent);
222}