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