blob: 7c16d20dfc5bb4700fbd973d9e5c22fc70d2e015 [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"
35,
Harald Welte80fe35d2002-05-29 13:08:15 +000036IPTABLES_VERSION);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000037
38}
39
40static struct option opts[] = {
41 { "set", 0, 0, 201 },
42 { "rcheck", 0, 0, 202 },
43 { "update", 0, 0, 203 },
44 { "seconds", 1, 0, 204 },
45 { "hitcount", 1, 0, 205 },
46 { "remove",0, 0, 206 },
Stephen Frost4fce44c2002-02-04 11:58:22 +000047 { "rttl",0, 0, 207},
48 { "name", 1, 0, 208},
Stephen Frost7fdbc952002-06-21 17:26:33 +000049 { "rsource", 0, 0, 209},
50 { "rdest", 0, 0, 210},
Stephen Frost93c7e5a2001-11-08 22:35:03 +000051 {0}
52};
53
54/* Initialize the match. */
55static void
Stephen Frost7fdbc952002-06-21 17:26:33 +000056init(struct ipt_entry_match *match, unsigned int *nfcache)
Stephen Frost93c7e5a2001-11-08 22:35:03 +000057{
Stephen Frost7fdbc952002-06-21 17:26:33 +000058 struct ipt_recent_info *info = (struct ipt_recent_info *)(match)->data;
59
Stephen Frost93c7e5a2001-11-08 22:35:03 +000060 *nfcache |= NFC_UNKNOWN;
Stephen Frost7fdbc952002-06-21 17:26:33 +000061
62 strncpy(info->name,"DEFAULT",200);
63 info->side = IPT_RECENT_SOURCE;
Stephen Frost93c7e5a2001-11-08 22:35:03 +000064}
65
66/* Function which parses command options; returns true if it
67 ate an option */
68static int
69parse(int c, char **argv, int invert, unsigned int *flags,
70 const struct ipt_entry *entry,
71 unsigned int *nfcache,
72 struct ipt_entry_match **match)
73{
74 struct ipt_recent_info *info = (struct ipt_recent_info *)(*match)->data;
Stephen Frost93c7e5a2001-11-08 22:35:03 +000075 switch (c) {
76 case 201:
77 if (*flags) exit_error(PARAMETER_PROBLEM,
78 "recent: only one of `--set', `--check' "
79 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +000080 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000081 info->check_set |= IPT_RECENT_SET;
82 if (invert) info->invert = 1;
83 *flags = 1;
84 break;
85
86 case 202:
87 if (*flags) exit_error(PARAMETER_PROBLEM,
88 "recent: only one of `--set', `--check' "
89 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +000090 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +000091 info->check_set |= IPT_RECENT_CHECK;
92 if(invert) info->invert = 1;
93 *flags = 1;
94 break;
95
96 case 203:
97 if (*flags) exit_error(PARAMETER_PROBLEM,
98 "recent: only one of `--set', `--check' "
99 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +0000100 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000101 info->check_set |= IPT_RECENT_UPDATE;
102 if (invert) info->invert = 1;
103 *flags = 1;
104 break;
105
106 case 206:
107 if (*flags) exit_error(PARAMETER_PROBLEM,
108 "recent: only one of `--set', `--check' "
109 "`--update' or `--remove' may be set");
Harald Welteb77f1da2002-03-14 11:35:58 +0000110 check_inverse(optarg, &invert, &optind, 0);
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000111 info->check_set |= IPT_RECENT_REMOVE;
112 if (invert) info->invert = 1;
113 *flags = 1;
114 break;
115
116 case 204:
117 info->seconds = atoi(optarg);
118 break;
119
120 case 205:
121 info->hit_count = atoi(optarg);
122 break;
123
Stephen Frost4fce44c2002-02-04 11:58:22 +0000124 case 207:
125 info->check_set |= IPT_RECENT_TTL;
126 break;
127
128 case 208:
129 strncpy(info->name,optarg,200);
130 break;
131
Stephen Frost7fdbc952002-06-21 17:26:33 +0000132 case 209:
133 info->side = IPT_RECENT_SOURCE;
134 break;
135
136 case 210:
137 info->side = IPT_RECENT_DEST;
138 break;
139
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000140 default:
141 return 0;
142 }
Stephen Frost4fce44c2002-02-04 11:58:22 +0000143
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000144 return 1;
145}
146
147/* Final check; must have specified a specific option. */
148static void
149final_check(unsigned int flags)
150{
Stephen Frost7fdbc952002-06-21 17:26:33 +0000151
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000152 if (!flags)
153 exit_error(PARAMETER_PROBLEM,
154 "recent: you must specify one of `--set', `--check' "
155 "`--update' or `--remove'");
156}
157
158/* Prints out the matchinfo. */
159static void
160print(const struct ipt_ip *ip,
161 const struct ipt_entry_match *match,
162 int numeric)
163{
164 struct ipt_recent_info *info = (struct ipt_recent_info *)match->data;
165
166 if (info->invert) fputc('!', stdout);
167
168 printf("recent: ");
169 if(info->check_set & IPT_RECENT_SET) printf("SET ");
170 if(info->check_set & IPT_RECENT_CHECK) printf("CHECK ");
171 if(info->check_set & IPT_RECENT_UPDATE) printf("UPDATE ");
172 if(info->check_set & IPT_RECENT_REMOVE) printf("REMOVE ");
Stephen Frost4fce44c2002-02-04 11:58:22 +0000173 if(info->seconds) printf("seconds: %d ",info->seconds);
174 if(info->hit_count) printf("hit_count: %d ",info->hit_count);
175 if(info->check_set & IPT_RECENT_TTL) printf("TTL-Match ");
Stephen Frost7fdbc952002-06-21 17:26:33 +0000176 if(info->name) printf("name: %s ",info->name);
177 if(info->side == IPT_RECENT_SOURCE) printf("side: source ");
178 if(info->side == IPT_RECENT_DEST) printf("side: dest");
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000179}
180
181/* Saves the union ipt_matchinfo in parsable form to stdout. */
182static void
183save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
184{
185 struct ipt_recent_info *info = (struct ipt_recent_info *)match;
186
187 if (info->invert) fputc('!', stdout);
188
189 printf("recent: ");
190 if(info->check_set & IPT_RECENT_SET) printf("SET ");
191 if(info->check_set & IPT_RECENT_CHECK) printf("CHECK ");
192 if(info->check_set & IPT_RECENT_UPDATE) printf("UPDATE ");
193 if(info->check_set & IPT_RECENT_REMOVE) printf("REMOVE ");
Stephen Frost4fce44c2002-02-04 11:58:22 +0000194 if(info->seconds) printf("seconds: %d ",info->seconds);
195 if(info->hit_count) printf("hit_count: %d ",info->hit_count);
196 if(info->check_set & IPT_RECENT_TTL) printf("TTL-Match ");
Stephen Frost7fdbc952002-06-21 17:26:33 +0000197 if(info->name) printf("name: %s ",info->name);
198 if(info->side == IPT_RECENT_SOURCE) printf("side: source ");
199 if(info->side == IPT_RECENT_DEST) printf("side: dest");
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000200}
201
202static
203struct iptables_match recent
204= { NULL,
205 "recent",
Harald Welte80fe35d2002-05-29 13:08:15 +0000206 IPTABLES_VERSION,
Stephen Frost93c7e5a2001-11-08 22:35:03 +0000207 IPT_ALIGN(sizeof(struct ipt_recent_info)),
208 IPT_ALIGN(sizeof(struct ipt_recent_info)),
209 &help,
210 &init,
211 &parse,
212 &final_check,
213 &print,
214 &save,
215 opts
216};
217
218void _init(void)
219{
220 register_match(&recent);
221}