Commit | Line | Data |
---|---|---|
404bdbfd PM |
1 | /* |
2 | * Copyright (c) 2006 Patrick McHardy <kaber@trash.net> | |
079aa88f | 3 | * Copyright © CC Computer Consultants GmbH, 2007 - 2008 |
404bdbfd PM |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This is a replacement of the old ipt_recent module, which carried the | |
10 | * following copyright notice: | |
11 | * | |
12 | * Author: Stephen Frost <sfrost@snowman.net> | |
13 | * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org | |
14 | */ | |
15 | #include <linux/init.h> | |
6709dbbb | 16 | #include <linux/ip.h> |
079aa88f JE |
17 | #include <linux/ipv6.h> |
18 | #include <linux/module.h> | |
404bdbfd | 19 | #include <linux/moduleparam.h> |
1da177e4 | 20 | #include <linux/proc_fs.h> |
404bdbfd PM |
21 | #include <linux/seq_file.h> |
22 | #include <linux/string.h> | |
1da177e4 | 23 | #include <linux/ctype.h> |
404bdbfd PM |
24 | #include <linux/list.h> |
25 | #include <linux/random.h> | |
26 | #include <linux/jhash.h> | |
27 | #include <linux/bitops.h> | |
28 | #include <linux/skbuff.h> | |
29 | #include <linux/inet.h> | |
457c4cbc | 30 | #include <net/net_namespace.h> |
1da177e4 | 31 | |
6709dbbb | 32 | #include <linux/netfilter/x_tables.h> |
e948b20a | 33 | #include <linux/netfilter/xt_recent.h> |
1da177e4 | 34 | |
404bdbfd | 35 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
079aa88f | 36 | MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>"); |
2ae15b64 | 37 | MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching for IPv4"); |
404bdbfd | 38 | MODULE_LICENSE("GPL"); |
e948b20a | 39 | MODULE_ALIAS("ipt_recent"); |
079aa88f | 40 | MODULE_ALIAS("ip6t_recent"); |
1da177e4 | 41 | |
e7be6994 PM |
42 | static unsigned int ip_list_tot = 100; |
43 | static unsigned int ip_pkt_list_tot = 20; | |
44 | static unsigned int ip_list_hash_size = 0; | |
45 | static unsigned int ip_list_perms = 0644; | |
b93ff783 DDG |
46 | static unsigned int ip_list_uid = 0; |
47 | static unsigned int ip_list_gid = 0; | |
e7be6994 PM |
48 | module_param(ip_list_tot, uint, 0400); |
49 | module_param(ip_pkt_list_tot, uint, 0400); | |
50 | module_param(ip_list_hash_size, uint, 0400); | |
51 | module_param(ip_list_perms, uint, 0400); | |
b93ff783 DDG |
52 | module_param(ip_list_uid, uint, 0400); |
53 | module_param(ip_list_gid, uint, 0400); | |
404bdbfd PM |
54 | MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list"); |
55 | MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP to remember (max. 255)"); | |
56 | MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs"); | |
079aa88f JE |
57 | MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files"); |
58 | MODULE_PARM_DESC(ip_list_uid,"owner of /proc/net/xt_recent/* files"); | |
59 | MODULE_PARM_DESC(ip_list_gid,"owning group of /proc/net/xt_recent/* files"); | |
404bdbfd | 60 | |
404bdbfd PM |
61 | struct recent_entry { |
62 | struct list_head list; | |
63 | struct list_head lru_list; | |
079aa88f JE |
64 | union nf_inet_addr addr; |
65 | u_int16_t family; | |
404bdbfd PM |
66 | u_int8_t ttl; |
67 | u_int8_t index; | |
68 | u_int16_t nstamps; | |
69 | unsigned long stamps[0]; | |
1da177e4 LT |
70 | }; |
71 | ||
404bdbfd PM |
72 | struct recent_table { |
73 | struct list_head list; | |
e948b20a | 74 | char name[XT_RECENT_NAME_LEN]; |
1da177e4 | 75 | #ifdef CONFIG_PROC_FS |
079aa88f | 76 | struct proc_dir_entry *proc_old, *proc; |
404bdbfd PM |
77 | #endif |
78 | unsigned int refcnt; | |
79 | unsigned int entries; | |
80 | struct list_head lru_list; | |
81 | struct list_head iphash[0]; | |
1da177e4 LT |
82 | }; |
83 | ||
404bdbfd | 84 | static LIST_HEAD(tables); |
1da177e4 | 85 | static DEFINE_SPINLOCK(recent_lock); |
a0e889bb | 86 | static DEFINE_MUTEX(recent_mutex); |
1da177e4 LT |
87 | |
88 | #ifdef CONFIG_PROC_FS | |
079aa88f JE |
89 | #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT |
90 | static struct proc_dir_entry *proc_old_dir; | |
91 | #endif | |
92 | static struct proc_dir_entry *recent_proc_dir; | |
93 | static const struct file_operations recent_old_fops, recent_mt_fops; | |
1da177e4 LT |
94 | #endif |
95 | ||
404bdbfd | 96 | static u_int32_t hash_rnd; |
079aa88f JE |
97 | static bool hash_rnd_initted; |
98 | ||
99 | static unsigned int recent_entry_hash4(const union nf_inet_addr *addr) | |
100 | { | |
101 | if (!hash_rnd_initted) { | |
102 | get_random_bytes(&hash_rnd, sizeof(hash_rnd)); | |
103 | hash_rnd_initted = true; | |
104 | } | |
105 | return jhash_1word((__force u32)addr->ip, hash_rnd) & | |
106 | (ip_list_hash_size - 1); | |
107 | } | |
1da177e4 | 108 | |
079aa88f | 109 | static unsigned int recent_entry_hash6(const union nf_inet_addr *addr) |
1da177e4 | 110 | { |
404bdbfd | 111 | if (!hash_rnd_initted) { |
079aa88f JE |
112 | get_random_bytes(&hash_rnd, sizeof(hash_rnd)); |
113 | hash_rnd_initted = true; | |
1da177e4 | 114 | } |
079aa88f JE |
115 | return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) & |
116 | (ip_list_hash_size - 1); | |
1da177e4 LT |
117 | } |
118 | ||
404bdbfd | 119 | static struct recent_entry * |
079aa88f JE |
120 | recent_entry_lookup(const struct recent_table *table, |
121 | const union nf_inet_addr *addrp, u_int16_t family, | |
122 | u_int8_t ttl) | |
1da177e4 | 123 | { |
404bdbfd PM |
124 | struct recent_entry *e; |
125 | unsigned int h; | |
126 | ||
ee999d8b | 127 | if (family == NFPROTO_IPV4) |
079aa88f JE |
128 | h = recent_entry_hash4(addrp); |
129 | else | |
130 | h = recent_entry_hash6(addrp); | |
131 | ||
404bdbfd | 132 | list_for_each_entry(e, &table->iphash[h], list) |
079aa88f JE |
133 | if (e->family == family && |
134 | memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 && | |
135 | (ttl == e->ttl || ttl == 0 || e->ttl == 0)) | |
404bdbfd PM |
136 | return e; |
137 | return NULL; | |
138 | } | |
1da177e4 | 139 | |
404bdbfd PM |
140 | static void recent_entry_remove(struct recent_table *t, struct recent_entry *e) |
141 | { | |
142 | list_del(&e->list); | |
143 | list_del(&e->lru_list); | |
144 | kfree(e); | |
145 | t->entries--; | |
146 | } | |
1da177e4 | 147 | |
404bdbfd | 148 | static struct recent_entry * |
079aa88f JE |
149 | recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr, |
150 | u_int16_t family, u_int8_t ttl) | |
404bdbfd PM |
151 | { |
152 | struct recent_entry *e; | |
1da177e4 | 153 | |
404bdbfd PM |
154 | if (t->entries >= ip_list_tot) { |
155 | e = list_entry(t->lru_list.next, struct recent_entry, lru_list); | |
156 | recent_entry_remove(t, e); | |
1da177e4 | 157 | } |
404bdbfd PM |
158 | e = kmalloc(sizeof(*e) + sizeof(e->stamps[0]) * ip_pkt_list_tot, |
159 | GFP_ATOMIC); | |
160 | if (e == NULL) | |
161 | return NULL; | |
079aa88f | 162 | memcpy(&e->addr, addr, sizeof(e->addr)); |
404bdbfd PM |
163 | e->ttl = ttl; |
164 | e->stamps[0] = jiffies; | |
165 | e->nstamps = 1; | |
166 | e->index = 1; | |
079aa88f | 167 | e->family = family; |
ee999d8b | 168 | if (family == NFPROTO_IPV4) |
079aa88f JE |
169 | list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]); |
170 | else | |
171 | list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]); | |
404bdbfd PM |
172 | list_add_tail(&e->lru_list, &t->lru_list); |
173 | t->entries++; | |
174 | return e; | |
175 | } | |
1da177e4 | 176 | |
404bdbfd PM |
177 | static void recent_entry_update(struct recent_table *t, struct recent_entry *e) |
178 | { | |
179 | e->stamps[e->index++] = jiffies; | |
180 | if (e->index > e->nstamps) | |
181 | e->nstamps = e->index; | |
182 | e->index %= ip_pkt_list_tot; | |
183 | list_move_tail(&e->lru_list, &t->lru_list); | |
184 | } | |
1da177e4 | 185 | |
404bdbfd PM |
186 | static struct recent_table *recent_table_lookup(const char *name) |
187 | { | |
188 | struct recent_table *t; | |
1da177e4 | 189 | |
404bdbfd PM |
190 | list_for_each_entry(t, &tables, list) |
191 | if (!strcmp(t->name, name)) | |
192 | return t; | |
193 | return NULL; | |
194 | } | |
1da177e4 | 195 | |
404bdbfd PM |
196 | static void recent_table_flush(struct recent_table *t) |
197 | { | |
198 | struct recent_entry *e, *next; | |
199 | unsigned int i; | |
1da177e4 | 200 | |
7c4e36bc | 201 | for (i = 0; i < ip_list_hash_size; i++) |
404bdbfd PM |
202 | list_for_each_entry_safe(e, next, &t->iphash[i], list) |
203 | recent_entry_remove(t, e); | |
1da177e4 LT |
204 | } |
205 | ||
1d93a9cb | 206 | static bool |
f7108a20 | 207 | recent_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
1da177e4 | 208 | { |
f7108a20 | 209 | const struct xt_recent_mtinfo *info = par->matchinfo; |
404bdbfd PM |
210 | struct recent_table *t; |
211 | struct recent_entry *e; | |
079aa88f | 212 | union nf_inet_addr addr = {}; |
404bdbfd | 213 | u_int8_t ttl; |
1d93a9cb | 214 | bool ret = info->invert; |
1da177e4 | 215 | |
f7108a20 | 216 | if (par->match->family == NFPROTO_IPV4) { |
079aa88f JE |
217 | const struct iphdr *iph = ip_hdr(skb); |
218 | ||
219 | if (info->side == XT_RECENT_DEST) | |
220 | addr.ip = iph->daddr; | |
221 | else | |
222 | addr.ip = iph->saddr; | |
223 | ||
224 | ttl = iph->ttl; | |
225 | } else { | |
226 | const struct ipv6hdr *iph = ipv6_hdr(skb); | |
227 | ||
228 | if (info->side == XT_RECENT_DEST) | |
229 | memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6)); | |
230 | else | |
231 | memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6)); | |
232 | ||
233 | ttl = iph->hop_limit; | |
234 | } | |
1da177e4 | 235 | |
404bdbfd | 236 | /* use TTL as seen before forwarding */ |
f7108a20 | 237 | if (par->out != NULL && skb->sk == NULL) |
404bdbfd | 238 | ttl++; |
1da177e4 | 239 | |
1da177e4 | 240 | spin_lock_bh(&recent_lock); |
404bdbfd | 241 | t = recent_table_lookup(info->name); |
f7108a20 | 242 | e = recent_entry_lookup(t, &addr, par->match->family, |
079aa88f | 243 | (info->check_set & XT_RECENT_TTL) ? ttl : 0); |
404bdbfd | 244 | if (e == NULL) { |
e948b20a | 245 | if (!(info->check_set & XT_RECENT_SET)) |
404bdbfd | 246 | goto out; |
f7108a20 | 247 | e = recent_entry_init(t, &addr, par->match->family, ttl); |
404bdbfd | 248 | if (e == NULL) |
f7108a20 | 249 | *par->hotdrop = true; |
1d93a9cb | 250 | ret = !ret; |
404bdbfd | 251 | goto out; |
1da177e4 LT |
252 | } |
253 | ||
e948b20a | 254 | if (info->check_set & XT_RECENT_SET) |
1d93a9cb | 255 | ret = !ret; |
e948b20a | 256 | else if (info->check_set & XT_RECENT_REMOVE) { |
404bdbfd | 257 | recent_entry_remove(t, e); |
1d93a9cb | 258 | ret = !ret; |
e948b20a | 259 | } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) { |
855304af | 260 | unsigned long time = jiffies - info->seconds * HZ; |
404bdbfd PM |
261 | unsigned int i, hits = 0; |
262 | ||
263 | for (i = 0; i < e->nstamps; i++) { | |
855304af | 264 | if (info->seconds && time_after(time, e->stamps[i])) |
404bdbfd PM |
265 | continue; |
266 | if (++hits >= info->hit_count) { | |
1d93a9cb | 267 | ret = !ret; |
404bdbfd | 268 | break; |
1da177e4 | 269 | } |
1da177e4 | 270 | } |
1da177e4 LT |
271 | } |
272 | ||
e948b20a JE |
273 | if (info->check_set & XT_RECENT_SET || |
274 | (info->check_set & XT_RECENT_UPDATE && ret)) { | |
404bdbfd PM |
275 | recent_entry_update(t, e); |
276 | e->ttl = ttl; | |
277 | } | |
278 | out: | |
279 | spin_unlock_bh(&recent_lock); | |
280 | return ret; | |
1da177e4 LT |
281 | } |
282 | ||
ccb79bdc | 283 | static bool |
d3c5ee6d JE |
284 | recent_mt_check(const char *tablename, const void *ip, |
285 | const struct xt_match *match, void *matchinfo, | |
286 | unsigned int hook_mask) | |
1da177e4 | 287 | { |
e948b20a | 288 | const struct xt_recent_mtinfo *info = matchinfo; |
404bdbfd PM |
289 | struct recent_table *t; |
290 | unsigned i; | |
ccb79bdc | 291 | bool ret = false; |
1da177e4 | 292 | |
404bdbfd | 293 | if (hweight8(info->check_set & |
e948b20a JE |
294 | (XT_RECENT_SET | XT_RECENT_REMOVE | |
295 | XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1) | |
ccb79bdc | 296 | return false; |
e948b20a | 297 | if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) && |
404bdbfd | 298 | (info->seconds || info->hit_count)) |
ccb79bdc | 299 | return false; |
d0ebf133 DHZ |
300 | if (info->hit_count > ip_pkt_list_tot) |
301 | return false; | |
404bdbfd | 302 | if (info->name[0] == '\0' || |
e948b20a | 303 | strnlen(info->name, XT_RECENT_NAME_LEN) == XT_RECENT_NAME_LEN) |
ccb79bdc | 304 | return false; |
1da177e4 | 305 | |
a0e889bb | 306 | mutex_lock(&recent_mutex); |
404bdbfd PM |
307 | t = recent_table_lookup(info->name); |
308 | if (t != NULL) { | |
309 | t->refcnt++; | |
ccb79bdc | 310 | ret = true; |
404bdbfd | 311 | goto out; |
1da177e4 LT |
312 | } |
313 | ||
404bdbfd | 314 | t = kzalloc(sizeof(*t) + sizeof(t->iphash[0]) * ip_list_hash_size, |
a0e889bb | 315 | GFP_KERNEL); |
404bdbfd PM |
316 | if (t == NULL) |
317 | goto out; | |
2b2283d0 | 318 | t->refcnt = 1; |
404bdbfd PM |
319 | strcpy(t->name, info->name); |
320 | INIT_LIST_HEAD(&t->lru_list); | |
321 | for (i = 0; i < ip_list_hash_size; i++) | |
322 | INIT_LIST_HEAD(&t->iphash[i]); | |
323 | #ifdef CONFIG_PROC_FS | |
079aa88f JE |
324 | t->proc = proc_create(t->name, ip_list_perms, recent_proc_dir, |
325 | &recent_mt_fops); | |
404bdbfd PM |
326 | if (t->proc == NULL) { |
327 | kfree(t); | |
328 | goto out; | |
1da177e4 | 329 | } |
079aa88f JE |
330 | #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT |
331 | t->proc_old = proc_create(t->name, ip_list_perms, proc_old_dir, | |
332 | &recent_old_fops); | |
333 | if (t->proc_old == NULL) { | |
334 | remove_proc_entry(t->name, proc_old_dir); | |
335 | kfree(t); | |
336 | goto out; | |
337 | } | |
338 | t->proc_old->uid = ip_list_uid; | |
339 | t->proc_old->gid = ip_list_gid; | |
340 | t->proc_old->data = t; | |
341 | #endif | |
b93ff783 DDG |
342 | t->proc->uid = ip_list_uid; |
343 | t->proc->gid = ip_list_gid; | |
404bdbfd | 344 | t->proc->data = t; |
1da177e4 | 345 | #endif |
a0e889bb | 346 | spin_lock_bh(&recent_lock); |
404bdbfd | 347 | list_add_tail(&t->list, &tables); |
a0e889bb | 348 | spin_unlock_bh(&recent_lock); |
ccb79bdc | 349 | ret = true; |
404bdbfd | 350 | out: |
a0e889bb | 351 | mutex_unlock(&recent_mutex); |
404bdbfd PM |
352 | return ret; |
353 | } | |
1da177e4 | 354 | |
d3c5ee6d | 355 | static void recent_mt_destroy(const struct xt_match *match, void *matchinfo) |
404bdbfd | 356 | { |
e948b20a | 357 | const struct xt_recent_mtinfo *info = matchinfo; |
404bdbfd | 358 | struct recent_table *t; |
1da177e4 | 359 | |
a0e889bb | 360 | mutex_lock(&recent_mutex); |
404bdbfd PM |
361 | t = recent_table_lookup(info->name); |
362 | if (--t->refcnt == 0) { | |
a0e889bb | 363 | spin_lock_bh(&recent_lock); |
404bdbfd | 364 | list_del(&t->list); |
a0e889bb | 365 | spin_unlock_bh(&recent_lock); |
404bdbfd | 366 | #ifdef CONFIG_PROC_FS |
079aa88f JE |
367 | #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT |
368 | remove_proc_entry(t->name, proc_old_dir); | |
369 | #endif | |
370 | remove_proc_entry(t->name, recent_proc_dir); | |
1da177e4 | 371 | #endif |
a8ddc916 | 372 | recent_table_flush(t); |
404bdbfd | 373 | kfree(t); |
1da177e4 | 374 | } |
a0e889bb | 375 | mutex_unlock(&recent_mutex); |
404bdbfd | 376 | } |
1da177e4 | 377 | |
404bdbfd PM |
378 | #ifdef CONFIG_PROC_FS |
379 | struct recent_iter_state { | |
079aa88f | 380 | const struct recent_table *table; |
404bdbfd PM |
381 | unsigned int bucket; |
382 | }; | |
1da177e4 | 383 | |
404bdbfd | 384 | static void *recent_seq_start(struct seq_file *seq, loff_t *pos) |
855304af | 385 | __acquires(recent_lock) |
404bdbfd PM |
386 | { |
387 | struct recent_iter_state *st = seq->private; | |
a47362a2 | 388 | const struct recent_table *t = st->table; |
404bdbfd PM |
389 | struct recent_entry *e; |
390 | loff_t p = *pos; | |
1da177e4 | 391 | |
1da177e4 | 392 | spin_lock_bh(&recent_lock); |
1da177e4 | 393 | |
7c4e36bc JE |
394 | for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) |
395 | list_for_each_entry(e, &t->iphash[st->bucket], list) | |
404bdbfd PM |
396 | if (p-- == 0) |
397 | return e; | |
404bdbfd PM |
398 | return NULL; |
399 | } | |
1da177e4 | 400 | |
404bdbfd PM |
401 | static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
402 | { | |
403 | struct recent_iter_state *st = seq->private; | |
3cf93c96 | 404 | const struct recent_table *t = st->table; |
079aa88f JE |
405 | const struct recent_entry *e = v; |
406 | const struct list_head *head = e->list.next; | |
404bdbfd PM |
407 | |
408 | while (head == &t->iphash[st->bucket]) { | |
409 | if (++st->bucket >= ip_list_hash_size) | |
410 | return NULL; | |
411 | head = t->iphash[st->bucket].next; | |
412 | } | |
413 | (*pos)++; | |
414 | return list_entry(head, struct recent_entry, list); | |
1da177e4 LT |
415 | } |
416 | ||
404bdbfd | 417 | static void recent_seq_stop(struct seq_file *s, void *v) |
855304af | 418 | __releases(recent_lock) |
1da177e4 | 419 | { |
404bdbfd PM |
420 | spin_unlock_bh(&recent_lock); |
421 | } | |
1da177e4 | 422 | |
404bdbfd PM |
423 | static int recent_seq_show(struct seq_file *seq, void *v) |
424 | { | |
3cf93c96 | 425 | const struct recent_entry *e = v; |
404bdbfd PM |
426 | unsigned int i; |
427 | ||
428 | i = (e->index - 1) % ip_pkt_list_tot; | |
ee999d8b | 429 | if (e->family == NFPROTO_IPV4) |
079aa88f JE |
430 | seq_printf(seq, "src=" NIPQUAD_FMT " ttl: %u last_seen: %lu " |
431 | "oldest_pkt: %u", NIPQUAD(e->addr.ip), e->ttl, | |
432 | e->stamps[i], e->index); | |
433 | else | |
434 | seq_printf(seq, "src=" NIP6_FMT " ttl: %u last_seen: %lu " | |
435 | "oldest_pkt: %u", NIP6(e->addr.in6), e->ttl, | |
436 | e->stamps[i], e->index); | |
404bdbfd PM |
437 | for (i = 0; i < e->nstamps; i++) |
438 | seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]); | |
439 | seq_printf(seq, "\n"); | |
440 | return 0; | |
441 | } | |
1da177e4 | 442 | |
56b3d975 | 443 | static const struct seq_operations recent_seq_ops = { |
404bdbfd PM |
444 | .start = recent_seq_start, |
445 | .next = recent_seq_next, | |
446 | .stop = recent_seq_stop, | |
447 | .show = recent_seq_show, | |
448 | }; | |
1da177e4 | 449 | |
404bdbfd PM |
450 | static int recent_seq_open(struct inode *inode, struct file *file) |
451 | { | |
452 | struct proc_dir_entry *pde = PDE(inode); | |
404bdbfd | 453 | struct recent_iter_state *st; |
404bdbfd | 454 | |
e2da5913 | 455 | st = __seq_open_private(file, &recent_seq_ops, sizeof(*st)); |
404bdbfd PM |
456 | if (st == NULL) |
457 | return -ENOMEM; | |
3af8e31c | 458 | |
404bdbfd | 459 | st->table = pde->data; |
e2da5913 | 460 | return 0; |
404bdbfd | 461 | } |
1da177e4 | 462 | |
079aa88f JE |
463 | #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT |
464 | static int recent_old_seq_open(struct inode *inode, struct file *filp) | |
465 | { | |
466 | static bool warned_of_old; | |
467 | ||
468 | if (unlikely(!warned_of_old)) { | |
469 | printk(KERN_INFO KBUILD_MODNAME ": Use of /proc/net/ipt_recent" | |
470 | " is deprecated; use /proc/net/xt_recent.\n"); | |
471 | warned_of_old = true; | |
472 | } | |
473 | return recent_seq_open(inode, filp); | |
474 | } | |
475 | ||
476 | static ssize_t recent_old_proc_write(struct file *file, | |
477 | const char __user *input, | |
478 | size_t size, loff_t *loff) | |
404bdbfd | 479 | { |
3cf93c96 | 480 | const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); |
404bdbfd PM |
481 | struct recent_table *t = pde->data; |
482 | struct recent_entry *e; | |
483 | char buf[sizeof("+255.255.255.255")], *c = buf; | |
6a19d614 | 484 | __be32 addr; |
404bdbfd PM |
485 | int add; |
486 | ||
487 | if (size > sizeof(buf)) | |
488 | size = sizeof(buf); | |
489 | if (copy_from_user(buf, input, size)) | |
490 | return -EFAULT; | |
079aa88f | 491 | |
404bdbfd PM |
492 | while (isspace(*c)) |
493 | c++; | |
494 | ||
495 | if (size - (c - buf) < 5) | |
496 | return c - buf; | |
497 | if (!strncmp(c, "clear", 5)) { | |
498 | c += 5; | |
499 | spin_lock_bh(&recent_lock); | |
500 | recent_table_flush(t); | |
1da177e4 | 501 | spin_unlock_bh(&recent_lock); |
404bdbfd | 502 | return c - buf; |
1da177e4 | 503 | } |
1da177e4 | 504 | |
404bdbfd PM |
505 | switch (*c) { |
506 | case '-': | |
507 | add = 0; | |
508 | c++; | |
509 | break; | |
510 | case '+': | |
511 | c++; | |
512 | default: | |
513 | add = 1; | |
514 | break; | |
1da177e4 | 515 | } |
404bdbfd | 516 | addr = in_aton(c); |
1da177e4 | 517 | |
404bdbfd | 518 | spin_lock_bh(&recent_lock); |
ee999d8b | 519 | e = recent_entry_lookup(t, (const void *)&addr, NFPROTO_IPV4, 0); |
404bdbfd PM |
520 | if (e == NULL) { |
521 | if (add) | |
ee999d8b JE |
522 | recent_entry_init(t, (const void *)&addr, |
523 | NFPROTO_IPV4, 0); | |
404bdbfd PM |
524 | } else { |
525 | if (add) | |
526 | recent_entry_update(t, e); | |
527 | else | |
528 | recent_entry_remove(t, e); | |
1da177e4 | 529 | } |
1da177e4 | 530 | spin_unlock_bh(&recent_lock); |
404bdbfd PM |
531 | return size; |
532 | } | |
1da177e4 | 533 | |
079aa88f JE |
534 | static const struct file_operations recent_old_fops = { |
535 | .open = recent_old_seq_open, | |
404bdbfd | 536 | .read = seq_read, |
079aa88f | 537 | .write = recent_old_proc_write, |
404bdbfd PM |
538 | .release = seq_release_private, |
539 | .owner = THIS_MODULE, | |
540 | }; | |
079aa88f JE |
541 | #endif |
542 | ||
543 | static ssize_t | |
544 | recent_mt_proc_write(struct file *file, const char __user *input, | |
545 | size_t size, loff_t *loff) | |
546 | { | |
547 | const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
548 | struct recent_table *t = pde->data; | |
549 | struct recent_entry *e; | |
550 | char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")]; | |
551 | const char *c = buf; | |
552 | union nf_inet_addr addr; | |
553 | u_int16_t family; | |
554 | bool add, succ; | |
555 | ||
556 | if (size == 0) | |
557 | return 0; | |
558 | if (size > sizeof(buf)) | |
559 | size = sizeof(buf); | |
560 | if (copy_from_user(buf, input, size) != 0) | |
561 | return -EFAULT; | |
562 | ||
563 | /* Strict protocol! */ | |
564 | if (*loff != 0) | |
565 | return -ESPIPE; | |
566 | switch (*c) { | |
567 | case '/': /* flush table */ | |
568 | spin_lock_bh(&recent_lock); | |
569 | recent_table_flush(t); | |
570 | spin_unlock_bh(&recent_lock); | |
571 | return size; | |
572 | case '-': /* remove address */ | |
573 | add = false; | |
574 | break; | |
575 | case '+': /* add address */ | |
576 | add = true; | |
577 | break; | |
578 | default: | |
579 | printk(KERN_INFO KBUILD_MODNAME ": Need +ip, -ip or /\n"); | |
580 | return -EINVAL; | |
581 | } | |
582 | ||
583 | ++c; | |
584 | --size; | |
585 | if (strnchr(c, size, ':') != NULL) { | |
ee999d8b | 586 | family = NFPROTO_IPV6; |
079aa88f JE |
587 | succ = in6_pton(c, size, (void *)&addr, '\n', NULL); |
588 | } else { | |
ee999d8b | 589 | family = NFPROTO_IPV4; |
079aa88f JE |
590 | succ = in4_pton(c, size, (void *)&addr, '\n', NULL); |
591 | } | |
592 | ||
593 | if (!succ) { | |
594 | printk(KERN_INFO KBUILD_MODNAME ": illegal address written " | |
595 | "to procfs\n"); | |
596 | return -EINVAL; | |
597 | } | |
598 | ||
599 | spin_lock_bh(&recent_lock); | |
600 | e = recent_entry_lookup(t, &addr, family, 0); | |
601 | if (e == NULL) { | |
602 | if (add) | |
603 | recent_entry_init(t, &addr, family, 0); | |
604 | } else { | |
605 | if (add) | |
606 | recent_entry_update(t, e); | |
607 | else | |
608 | recent_entry_remove(t, e); | |
609 | } | |
610 | spin_unlock_bh(&recent_lock); | |
611 | /* Note we removed one above */ | |
612 | *loff += size + 1; | |
613 | return size + 1; | |
614 | } | |
615 | ||
616 | static const struct file_operations recent_mt_fops = { | |
617 | .open = recent_seq_open, | |
618 | .read = seq_read, | |
619 | .write = recent_mt_proc_write, | |
620 | .release = seq_release_private, | |
621 | .owner = THIS_MODULE, | |
622 | }; | |
1da177e4 | 623 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 624 | |
079aa88f JE |
625 | static struct xt_match recent_mt_reg[] __read_mostly = { |
626 | { | |
627 | .name = "recent", | |
628 | .revision = 0, | |
ee999d8b | 629 | .family = NFPROTO_IPV4, |
079aa88f JE |
630 | .match = recent_mt, |
631 | .matchsize = sizeof(struct xt_recent_mtinfo), | |
632 | .checkentry = recent_mt_check, | |
633 | .destroy = recent_mt_destroy, | |
634 | .me = THIS_MODULE, | |
635 | }, | |
636 | { | |
637 | .name = "recent", | |
638 | .revision = 0, | |
ee999d8b | 639 | .family = NFPROTO_IPV6, |
079aa88f JE |
640 | .match = recent_mt, |
641 | .matchsize = sizeof(struct xt_recent_mtinfo), | |
642 | .checkentry = recent_mt_check, | |
643 | .destroy = recent_mt_destroy, | |
644 | .me = THIS_MODULE, | |
645 | }, | |
1da177e4 LT |
646 | }; |
647 | ||
d3c5ee6d | 648 | static int __init recent_mt_init(void) |
1da177e4 | 649 | { |
404bdbfd | 650 | int err; |
1da177e4 | 651 | |
404bdbfd PM |
652 | if (!ip_list_tot || !ip_pkt_list_tot || ip_pkt_list_tot > 255) |
653 | return -EINVAL; | |
654 | ip_list_hash_size = 1 << fls(ip_list_tot); | |
1da177e4 | 655 | |
079aa88f | 656 | err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg)); |
404bdbfd | 657 | #ifdef CONFIG_PROC_FS |
1da177e4 | 658 | if (err) |
404bdbfd | 659 | return err; |
079aa88f JE |
660 | recent_proc_dir = proc_mkdir("xt_recent", init_net.proc_net); |
661 | if (recent_proc_dir == NULL) { | |
662 | xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg)); | |
663 | err = -ENOMEM; | |
664 | } | |
665 | #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT | |
666 | if (err < 0) | |
667 | return err; | |
668 | proc_old_dir = proc_mkdir("ipt_recent", init_net.proc_net); | |
669 | if (proc_old_dir == NULL) { | |
670 | remove_proc_entry("xt_recent", init_net.proc_net); | |
671 | xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg)); | |
404bdbfd PM |
672 | err = -ENOMEM; |
673 | } | |
079aa88f | 674 | #endif |
404bdbfd | 675 | #endif |
1da177e4 LT |
676 | return err; |
677 | } | |
678 | ||
d3c5ee6d | 679 | static void __exit recent_mt_exit(void) |
1da177e4 | 680 | { |
404bdbfd | 681 | BUG_ON(!list_empty(&tables)); |
079aa88f | 682 | xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg)); |
404bdbfd | 683 | #ifdef CONFIG_PROC_FS |
079aa88f | 684 | #ifdef CONFIG_NETFILTER_XT_MATCH_RECENT_PROC_COMPAT |
457c4cbc | 685 | remove_proc_entry("ipt_recent", init_net.proc_net); |
079aa88f JE |
686 | #endif |
687 | remove_proc_entry("xt_recent", init_net.proc_net); | |
404bdbfd | 688 | #endif |
1da177e4 LT |
689 | } |
690 | ||
d3c5ee6d JE |
691 | module_init(recent_mt_init); |
692 | module_exit(recent_mt_exit); |