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