Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
404bdbfd PM |
2 | /* |
3 | * Copyright (c) 2006 Patrick McHardy <kaber@trash.net> | |
079aa88f | 4 | * Copyright © CC Computer Consultants GmbH, 2007 - 2008 |
404bdbfd | 5 | * |
404bdbfd PM |
6 | * This is a replacement of the old ipt_recent module, which carried the |
7 | * following copyright notice: | |
8 | * | |
9 | * Author: Stephen Frost <sfrost@snowman.net> | |
10 | * Copyright 2002-2003, Stephen Frost, 2.5.x port by laforge@netfilter.org | |
11 | */ | |
8bee4bad | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
404bdbfd | 13 | #include <linux/init.h> |
6709dbbb | 14 | #include <linux/ip.h> |
079aa88f JE |
15 | #include <linux/ipv6.h> |
16 | #include <linux/module.h> | |
404bdbfd | 17 | #include <linux/moduleparam.h> |
1da177e4 | 18 | #include <linux/proc_fs.h> |
404bdbfd PM |
19 | #include <linux/seq_file.h> |
20 | #include <linux/string.h> | |
1da177e4 | 21 | #include <linux/ctype.h> |
404bdbfd PM |
22 | #include <linux/list.h> |
23 | #include <linux/random.h> | |
24 | #include <linux/jhash.h> | |
25 | #include <linux/bitops.h> | |
26 | #include <linux/skbuff.h> | |
27 | #include <linux/inet.h> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
2727de76 | 29 | #include <linux/vmalloc.h> |
457c4cbc | 30 | #include <net/net_namespace.h> |
7d07d563 | 31 | #include <net/netns/generic.h> |
1da177e4 | 32 | |
6709dbbb | 33 | #include <linux/netfilter/x_tables.h> |
e948b20a | 34 | #include <linux/netfilter/xt_recent.h> |
1da177e4 | 35 | |
404bdbfd | 36 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
408ffaa4 | 37 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
06bf514e | 38 | MODULE_DESCRIPTION("Xtables: \"recently-seen\" host matching"); |
404bdbfd | 39 | MODULE_LICENSE("GPL"); |
e948b20a | 40 | MODULE_ALIAS("ipt_recent"); |
079aa88f | 41 | MODULE_ALIAS("ip6t_recent"); |
1da177e4 | 42 | |
abc86d0f FW |
43 | static unsigned int ip_list_tot __read_mostly = 100; |
44 | static unsigned int ip_list_hash_size __read_mostly; | |
45 | static unsigned int ip_list_perms __read_mostly = 0644; | |
46 | static unsigned int ip_list_uid __read_mostly; | |
47 | static unsigned int ip_list_gid __read_mostly; | |
e7be6994 | 48 | module_param(ip_list_tot, uint, 0400); |
e7be6994 PM |
49 | module_param(ip_list_hash_size, uint, 0400); |
50 | module_param(ip_list_perms, uint, 0400); | |
d6444062 JP |
51 | module_param(ip_list_uid, uint, 0644); |
52 | module_param(ip_list_gid, uint, 0644); | |
404bdbfd | 53 | MODULE_PARM_DESC(ip_list_tot, "number of IPs to remember per list"); |
404bdbfd | 54 | MODULE_PARM_DESC(ip_list_hash_size, "size of hash table used to look up IPs"); |
079aa88f | 55 | MODULE_PARM_DESC(ip_list_perms, "permissions on /proc/net/xt_recent/* files"); |
5dc7a6d5 JE |
56 | MODULE_PARM_DESC(ip_list_uid, "default owner of /proc/net/xt_recent/* files"); |
57 | MODULE_PARM_DESC(ip_list_gid, "default owning group of /proc/net/xt_recent/* files"); | |
404bdbfd | 58 | |
abc86d0f FW |
59 | /* retained for backwards compatibility */ |
60 | static unsigned int ip_pkt_list_tot __read_mostly; | |
61 | module_param(ip_pkt_list_tot, uint, 0400); | |
62 | MODULE_PARM_DESC(ip_pkt_list_tot, "number of packets per IP address to remember (max. 255)"); | |
63 | ||
64 | #define XT_RECENT_MAX_NSTAMPS 256 | |
65 | ||
404bdbfd PM |
66 | struct recent_entry { |
67 | struct list_head list; | |
68 | struct list_head lru_list; | |
079aa88f JE |
69 | union nf_inet_addr addr; |
70 | u_int16_t family; | |
404bdbfd PM |
71 | u_int8_t ttl; |
72 | u_int8_t index; | |
73 | u_int16_t nstamps; | |
6daf1414 | 74 | unsigned long stamps[]; |
1da177e4 LT |
75 | }; |
76 | ||
404bdbfd PM |
77 | struct recent_table { |
78 | struct list_head list; | |
e948b20a | 79 | char name[XT_RECENT_NAME_LEN]; |
efdedd54 | 80 | union nf_inet_addr mask; |
404bdbfd PM |
81 | unsigned int refcnt; |
82 | unsigned int entries; | |
abc86d0f | 83 | u8 nstamps_max_mask; |
404bdbfd | 84 | struct list_head lru_list; |
6daf1414 | 85 | struct list_head iphash[]; |
1da177e4 LT |
86 | }; |
87 | ||
7d07d563 AD |
88 | struct recent_net { |
89 | struct list_head tables; | |
90 | #ifdef CONFIG_PROC_FS | |
91 | struct proc_dir_entry *xt_recent; | |
7d07d563 AD |
92 | #endif |
93 | }; | |
94 | ||
c7d03a00 | 95 | static unsigned int recent_net_id __read_mostly; |
abc86d0f | 96 | |
7d07d563 AD |
97 | static inline struct recent_net *recent_pernet(struct net *net) |
98 | { | |
99 | return net_generic(net, recent_net_id); | |
100 | } | |
101 | ||
1da177e4 | 102 | static DEFINE_SPINLOCK(recent_lock); |
a0e889bb | 103 | static DEFINE_MUTEX(recent_mutex); |
1da177e4 LT |
104 | |
105 | #ifdef CONFIG_PROC_FS | |
97a32539 | 106 | static const struct proc_ops recent_mt_proc_ops; |
1da177e4 LT |
107 | #endif |
108 | ||
294188ae | 109 | static u_int32_t hash_rnd __read_mostly; |
079aa88f | 110 | |
294188ae | 111 | static inline unsigned int recent_entry_hash4(const union nf_inet_addr *addr) |
079aa88f | 112 | { |
079aa88f JE |
113 | return jhash_1word((__force u32)addr->ip, hash_rnd) & |
114 | (ip_list_hash_size - 1); | |
115 | } | |
1da177e4 | 116 | |
294188ae | 117 | static inline unsigned int recent_entry_hash6(const union nf_inet_addr *addr) |
1da177e4 | 118 | { |
079aa88f JE |
119 | return jhash2((u32 *)addr->ip6, ARRAY_SIZE(addr->ip6), hash_rnd) & |
120 | (ip_list_hash_size - 1); | |
1da177e4 LT |
121 | } |
122 | ||
404bdbfd | 123 | static struct recent_entry * |
079aa88f JE |
124 | recent_entry_lookup(const struct recent_table *table, |
125 | const union nf_inet_addr *addrp, u_int16_t family, | |
126 | u_int8_t ttl) | |
1da177e4 | 127 | { |
404bdbfd PM |
128 | struct recent_entry *e; |
129 | unsigned int h; | |
130 | ||
ee999d8b | 131 | if (family == NFPROTO_IPV4) |
079aa88f JE |
132 | h = recent_entry_hash4(addrp); |
133 | else | |
134 | h = recent_entry_hash6(addrp); | |
135 | ||
404bdbfd | 136 | list_for_each_entry(e, &table->iphash[h], list) |
079aa88f JE |
137 | if (e->family == family && |
138 | memcmp(&e->addr, addrp, sizeof(e->addr)) == 0 && | |
139 | (ttl == e->ttl || ttl == 0 || e->ttl == 0)) | |
404bdbfd PM |
140 | return e; |
141 | return NULL; | |
142 | } | |
1da177e4 | 143 | |
404bdbfd PM |
144 | static void recent_entry_remove(struct recent_table *t, struct recent_entry *e) |
145 | { | |
146 | list_del(&e->list); | |
147 | list_del(&e->lru_list); | |
148 | kfree(e); | |
149 | t->entries--; | |
150 | } | |
1da177e4 | 151 | |
0079c5ae TG |
152 | /* |
153 | * Drop entries with timestamps older then 'time'. | |
154 | */ | |
b1bdde33 JK |
155 | static void recent_entry_reap(struct recent_table *t, unsigned long time, |
156 | struct recent_entry *working, bool update) | |
0079c5ae TG |
157 | { |
158 | struct recent_entry *e; | |
159 | ||
160 | /* | |
161 | * The head of the LRU list is always the oldest entry. | |
162 | */ | |
163 | e = list_entry(t->lru_list.next, struct recent_entry, lru_list); | |
164 | ||
b1bdde33 JK |
165 | /* |
166 | * Do not reap the entry which are going to be updated. | |
167 | */ | |
168 | if (e == working && update) | |
169 | return; | |
170 | ||
0079c5ae TG |
171 | /* |
172 | * The last time stamp is the most recent. | |
173 | */ | |
174 | if (time_after(time, e->stamps[e->index-1])) | |
175 | recent_entry_remove(t, e); | |
176 | } | |
177 | ||
404bdbfd | 178 | static struct recent_entry * |
079aa88f JE |
179 | recent_entry_init(struct recent_table *t, const union nf_inet_addr *addr, |
180 | u_int16_t family, u_int8_t ttl) | |
404bdbfd PM |
181 | { |
182 | struct recent_entry *e; | |
abc86d0f | 183 | unsigned int nstamps_max = t->nstamps_max_mask; |
1da177e4 | 184 | |
404bdbfd PM |
185 | if (t->entries >= ip_list_tot) { |
186 | e = list_entry(t->lru_list.next, struct recent_entry, lru_list); | |
187 | recent_entry_remove(t, e); | |
1da177e4 | 188 | } |
abc86d0f FW |
189 | |
190 | nstamps_max += 1; | |
acafe7e3 | 191 | e = kmalloc(struct_size(e, stamps, nstamps_max), GFP_ATOMIC); |
404bdbfd PM |
192 | if (e == NULL) |
193 | return NULL; | |
079aa88f | 194 | memcpy(&e->addr, addr, sizeof(e->addr)); |
404bdbfd PM |
195 | e->ttl = ttl; |
196 | e->stamps[0] = jiffies; | |
197 | e->nstamps = 1; | |
198 | e->index = 1; | |
079aa88f | 199 | e->family = family; |
ee999d8b | 200 | if (family == NFPROTO_IPV4) |
079aa88f JE |
201 | list_add_tail(&e->list, &t->iphash[recent_entry_hash4(addr)]); |
202 | else | |
203 | list_add_tail(&e->list, &t->iphash[recent_entry_hash6(addr)]); | |
404bdbfd PM |
204 | list_add_tail(&e->lru_list, &t->lru_list); |
205 | t->entries++; | |
206 | return e; | |
207 | } | |
1da177e4 | 208 | |
404bdbfd PM |
209 | static void recent_entry_update(struct recent_table *t, struct recent_entry *e) |
210 | { | |
abc86d0f | 211 | e->index &= t->nstamps_max_mask; |
404bdbfd PM |
212 | e->stamps[e->index++] = jiffies; |
213 | if (e->index > e->nstamps) | |
214 | e->nstamps = e->index; | |
404bdbfd PM |
215 | list_move_tail(&e->lru_list, &t->lru_list); |
216 | } | |
1da177e4 | 217 | |
7d07d563 AD |
218 | static struct recent_table *recent_table_lookup(struct recent_net *recent_net, |
219 | const char *name) | |
404bdbfd PM |
220 | { |
221 | struct recent_table *t; | |
1da177e4 | 222 | |
7d07d563 | 223 | list_for_each_entry(t, &recent_net->tables, list) |
404bdbfd PM |
224 | if (!strcmp(t->name, name)) |
225 | return t; | |
226 | return NULL; | |
227 | } | |
1da177e4 | 228 | |
404bdbfd PM |
229 | static void recent_table_flush(struct recent_table *t) |
230 | { | |
231 | struct recent_entry *e, *next; | |
232 | unsigned int i; | |
1da177e4 | 233 | |
7c4e36bc | 234 | for (i = 0; i < ip_list_hash_size; i++) |
404bdbfd PM |
235 | list_for_each_entry_safe(e, next, &t->iphash[i], list) |
236 | recent_entry_remove(t, e); | |
1da177e4 LT |
237 | } |
238 | ||
1d93a9cb | 239 | static bool |
62fc8051 | 240 | recent_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 241 | { |
613dbd95 | 242 | struct net *net = xt_net(par); |
7d07d563 | 243 | struct recent_net *recent_net = recent_pernet(net); |
efdedd54 | 244 | const struct xt_recent_mtinfo_v1 *info = par->matchinfo; |
404bdbfd PM |
245 | struct recent_table *t; |
246 | struct recent_entry *e; | |
efdedd54 | 247 | union nf_inet_addr addr = {}, addr_mask; |
404bdbfd | 248 | u_int8_t ttl; |
1d93a9cb | 249 | bool ret = info->invert; |
1da177e4 | 250 | |
613dbd95 | 251 | if (xt_family(par) == NFPROTO_IPV4) { |
079aa88f JE |
252 | const struct iphdr *iph = ip_hdr(skb); |
253 | ||
254 | if (info->side == XT_RECENT_DEST) | |
255 | addr.ip = iph->daddr; | |
256 | else | |
257 | addr.ip = iph->saddr; | |
258 | ||
259 | ttl = iph->ttl; | |
260 | } else { | |
261 | const struct ipv6hdr *iph = ipv6_hdr(skb); | |
262 | ||
263 | if (info->side == XT_RECENT_DEST) | |
264 | memcpy(&addr.in6, &iph->daddr, sizeof(addr.in6)); | |
265 | else | |
266 | memcpy(&addr.in6, &iph->saddr, sizeof(addr.in6)); | |
267 | ||
268 | ttl = iph->hop_limit; | |
269 | } | |
1da177e4 | 270 | |
404bdbfd | 271 | /* use TTL as seen before forwarding */ |
f5646501 FL |
272 | if (xt_out(par) != NULL && |
273 | (!skb->sk || !net_eq(net, sock_net(skb->sk)))) | |
404bdbfd | 274 | ttl++; |
1da177e4 | 275 | |
1da177e4 | 276 | spin_lock_bh(&recent_lock); |
7d07d563 | 277 | t = recent_table_lookup(recent_net, info->name); |
efdedd54 DF |
278 | |
279 | nf_inet_addr_mask(&addr, &addr_mask, &t->mask); | |
280 | ||
613dbd95 | 281 | e = recent_entry_lookup(t, &addr_mask, xt_family(par), |
079aa88f | 282 | (info->check_set & XT_RECENT_TTL) ? ttl : 0); |
404bdbfd | 283 | if (e == NULL) { |
e948b20a | 284 | if (!(info->check_set & XT_RECENT_SET)) |
404bdbfd | 285 | goto out; |
613dbd95 | 286 | e = recent_entry_init(t, &addr_mask, xt_family(par), ttl); |
404bdbfd | 287 | if (e == NULL) |
b4ba2611 | 288 | par->hotdrop = true; |
1d93a9cb | 289 | ret = !ret; |
404bdbfd | 290 | goto out; |
1da177e4 LT |
291 | } |
292 | ||
e948b20a | 293 | if (info->check_set & XT_RECENT_SET) |
1d93a9cb | 294 | ret = !ret; |
e948b20a | 295 | else if (info->check_set & XT_RECENT_REMOVE) { |
404bdbfd | 296 | recent_entry_remove(t, e); |
1d93a9cb | 297 | ret = !ret; |
e948b20a | 298 | } else if (info->check_set & (XT_RECENT_CHECK | XT_RECENT_UPDATE)) { |
855304af | 299 | unsigned long time = jiffies - info->seconds * HZ; |
404bdbfd PM |
300 | unsigned int i, hits = 0; |
301 | ||
302 | for (i = 0; i < e->nstamps; i++) { | |
855304af | 303 | if (info->seconds && time_after(time, e->stamps[i])) |
404bdbfd | 304 | continue; |
ef169150 | 305 | if (!info->hit_count || ++hits >= info->hit_count) { |
1d93a9cb | 306 | ret = !ret; |
404bdbfd | 307 | break; |
1da177e4 | 308 | } |
1da177e4 | 309 | } |
0079c5ae TG |
310 | |
311 | /* info->seconds must be non-zero */ | |
312 | if (info->check_set & XT_RECENT_REAP) | |
b1bdde33 JK |
313 | recent_entry_reap(t, time, e, |
314 | info->check_set & XT_RECENT_UPDATE && ret); | |
1da177e4 LT |
315 | } |
316 | ||
e948b20a JE |
317 | if (info->check_set & XT_RECENT_SET || |
318 | (info->check_set & XT_RECENT_UPDATE && ret)) { | |
404bdbfd PM |
319 | recent_entry_update(t, e); |
320 | e->ttl = ttl; | |
321 | } | |
322 | out: | |
323 | spin_unlock_bh(&recent_lock); | |
324 | return ret; | |
1da177e4 LT |
325 | } |
326 | ||
2727de76 ED |
327 | static void recent_table_free(void *addr) |
328 | { | |
4cb28970 | 329 | kvfree(addr); |
2727de76 ED |
330 | } |
331 | ||
efdedd54 DF |
332 | static int recent_mt_check(const struct xt_mtchk_param *par, |
333 | const struct xt_recent_mtinfo_v1 *info) | |
1da177e4 | 334 | { |
7d07d563 | 335 | struct recent_net *recent_net = recent_pernet(par->net); |
404bdbfd | 336 | struct recent_table *t; |
b0ceb560 AD |
337 | #ifdef CONFIG_PROC_FS |
338 | struct proc_dir_entry *pde; | |
da742808 EB |
339 | kuid_t uid; |
340 | kgid_t gid; | |
b0ceb560 | 341 | #endif |
abc86d0f | 342 | unsigned int nstamp_mask; |
95c96174 | 343 | unsigned int i; |
bd414ee6 | 344 | int ret = -EINVAL; |
1da177e4 | 345 | |
7bdc6624 GF |
346 | net_get_random_once(&hash_rnd, sizeof(hash_rnd)); |
347 | ||
606a9a02 | 348 | if (info->check_set & ~XT_RECENT_VALID_FLAGS) { |
b2606644 FW |
349 | pr_info_ratelimited("Unsupported userspace flags (%08x)\n", |
350 | info->check_set); | |
bd414ee6 | 351 | return -EINVAL; |
606a9a02 | 352 | } |
404bdbfd | 353 | if (hweight8(info->check_set & |
e948b20a JE |
354 | (XT_RECENT_SET | XT_RECENT_REMOVE | |
355 | XT_RECENT_CHECK | XT_RECENT_UPDATE)) != 1) | |
bd414ee6 | 356 | return -EINVAL; |
e948b20a | 357 | if ((info->check_set & (XT_RECENT_SET | XT_RECENT_REMOVE)) && |
0079c5ae TG |
358 | (info->seconds || info->hit_count || |
359 | (info->check_set & XT_RECENT_MODIFIERS))) | |
bd414ee6 | 360 | return -EINVAL; |
0079c5ae | 361 | if ((info->check_set & XT_RECENT_REAP) && !info->seconds) |
bd414ee6 | 362 | return -EINVAL; |
abc86d0f | 363 | if (info->hit_count >= XT_RECENT_MAX_NSTAMPS) { |
b2606644 FW |
364 | pr_info_ratelimited("hitcount (%u) is larger than allowed maximum (%u)\n", |
365 | info->hit_count, XT_RECENT_MAX_NSTAMPS - 1); | |
bd414ee6 | 366 | return -EINVAL; |
98e6d2d5 | 367 | } |
b1d0a5d0 FW |
368 | ret = xt_check_proc_name(info->name, sizeof(info->name)); |
369 | if (ret) | |
370 | return ret; | |
1da177e4 | 371 | |
abc86d0f FW |
372 | if (ip_pkt_list_tot && info->hit_count < ip_pkt_list_tot) |
373 | nstamp_mask = roundup_pow_of_two(ip_pkt_list_tot) - 1; | |
374 | else if (info->hit_count) | |
375 | nstamp_mask = roundup_pow_of_two(info->hit_count) - 1; | |
376 | else | |
377 | nstamp_mask = 32 - 1; | |
378 | ||
a0e889bb | 379 | mutex_lock(&recent_mutex); |
7d07d563 | 380 | t = recent_table_lookup(recent_net, info->name); |
404bdbfd | 381 | if (t != NULL) { |
cef9ed86 FW |
382 | if (nstamp_mask > t->nstamps_max_mask) { |
383 | spin_lock_bh(&recent_lock); | |
384 | recent_table_flush(t); | |
385 | t->nstamps_max_mask = nstamp_mask; | |
386 | spin_unlock_bh(&recent_lock); | |
abc86d0f FW |
387 | } |
388 | ||
404bdbfd | 389 | t->refcnt++; |
bd414ee6 | 390 | ret = 0; |
404bdbfd | 391 | goto out; |
1da177e4 LT |
392 | } |
393 | ||
6ca64ef3 | 394 | t = kvzalloc(struct_size(t, iphash, ip_list_hash_size), GFP_KERNEL); |
4a5a5c73 JE |
395 | if (t == NULL) { |
396 | ret = -ENOMEM; | |
404bdbfd | 397 | goto out; |
4a5a5c73 | 398 | } |
2b2283d0 | 399 | t->refcnt = 1; |
abc86d0f | 400 | t->nstamps_max_mask = nstamp_mask; |
efdedd54 DF |
401 | |
402 | memcpy(&t->mask, &info->mask, sizeof(t->mask)); | |
404bdbfd PM |
403 | strcpy(t->name, info->name); |
404 | INIT_LIST_HEAD(&t->lru_list); | |
405 | for (i = 0; i < ip_list_hash_size; i++) | |
406 | INIT_LIST_HEAD(&t->iphash[i]); | |
407 | #ifdef CONFIG_PROC_FS | |
da742808 EB |
408 | uid = make_kuid(&init_user_ns, ip_list_uid); |
409 | gid = make_kgid(&init_user_ns, ip_list_gid); | |
410 | if (!uid_valid(uid) || !gid_valid(gid)) { | |
2727de76 | 411 | recent_table_free(t); |
da742808 EB |
412 | ret = -EINVAL; |
413 | goto out; | |
414 | } | |
7d07d563 | 415 | pde = proc_create_data(t->name, ip_list_perms, recent_net->xt_recent, |
97a32539 | 416 | &recent_mt_proc_ops, t); |
b0ceb560 | 417 | if (pde == NULL) { |
2727de76 | 418 | recent_table_free(t); |
4a5a5c73 | 419 | ret = -ENOMEM; |
404bdbfd | 420 | goto out; |
1da177e4 | 421 | } |
271a15ea | 422 | proc_set_user(pde, uid, gid); |
1da177e4 | 423 | #endif |
a0e889bb | 424 | spin_lock_bh(&recent_lock); |
7d07d563 | 425 | list_add_tail(&t->list, &recent_net->tables); |
a0e889bb | 426 | spin_unlock_bh(&recent_lock); |
bd414ee6 | 427 | ret = 0; |
404bdbfd | 428 | out: |
a0e889bb | 429 | mutex_unlock(&recent_mutex); |
404bdbfd PM |
430 | return ret; |
431 | } | |
1da177e4 | 432 | |
efdedd54 DF |
433 | static int recent_mt_check_v0(const struct xt_mtchk_param *par) |
434 | { | |
435 | const struct xt_recent_mtinfo_v0 *info_v0 = par->matchinfo; | |
436 | struct xt_recent_mtinfo_v1 info_v1; | |
437 | ||
438 | /* Copy revision 0 structure to revision 1 */ | |
439 | memcpy(&info_v1, info_v0, sizeof(struct xt_recent_mtinfo)); | |
440 | /* Set default mask to ensure backward compatible behaviour */ | |
441 | memset(info_v1.mask.all, 0xFF, sizeof(info_v1.mask.all)); | |
442 | ||
443 | return recent_mt_check(par, &info_v1); | |
444 | } | |
445 | ||
446 | static int recent_mt_check_v1(const struct xt_mtchk_param *par) | |
447 | { | |
448 | return recent_mt_check(par, par->matchinfo); | |
449 | } | |
450 | ||
6be3d859 | 451 | static void recent_mt_destroy(const struct xt_mtdtor_param *par) |
404bdbfd | 452 | { |
7d07d563 | 453 | struct recent_net *recent_net = recent_pernet(par->net); |
efdedd54 | 454 | const struct xt_recent_mtinfo_v1 *info = par->matchinfo; |
404bdbfd | 455 | struct recent_table *t; |
1da177e4 | 456 | |
a0e889bb | 457 | mutex_lock(&recent_mutex); |
7d07d563 | 458 | t = recent_table_lookup(recent_net, info->name); |
404bdbfd | 459 | if (--t->refcnt == 0) { |
a0e889bb | 460 | spin_lock_bh(&recent_lock); |
404bdbfd | 461 | list_del(&t->list); |
a0e889bb | 462 | spin_unlock_bh(&recent_lock); |
404bdbfd | 463 | #ifdef CONFIG_PROC_FS |
665e205c VL |
464 | if (recent_net->xt_recent != NULL) |
465 | remove_proc_entry(t->name, recent_net->xt_recent); | |
1da177e4 | 466 | #endif |
a8ddc916 | 467 | recent_table_flush(t); |
2727de76 | 468 | recent_table_free(t); |
1da177e4 | 469 | } |
a0e889bb | 470 | mutex_unlock(&recent_mutex); |
404bdbfd | 471 | } |
1da177e4 | 472 | |
404bdbfd PM |
473 | #ifdef CONFIG_PROC_FS |
474 | struct recent_iter_state { | |
079aa88f | 475 | const struct recent_table *table; |
404bdbfd PM |
476 | unsigned int bucket; |
477 | }; | |
1da177e4 | 478 | |
404bdbfd | 479 | static void *recent_seq_start(struct seq_file *seq, loff_t *pos) |
855304af | 480 | __acquires(recent_lock) |
404bdbfd PM |
481 | { |
482 | struct recent_iter_state *st = seq->private; | |
a47362a2 | 483 | const struct recent_table *t = st->table; |
404bdbfd PM |
484 | struct recent_entry *e; |
485 | loff_t p = *pos; | |
1da177e4 | 486 | |
1da177e4 | 487 | spin_lock_bh(&recent_lock); |
1da177e4 | 488 | |
7c4e36bc JE |
489 | for (st->bucket = 0; st->bucket < ip_list_hash_size; st->bucket++) |
490 | list_for_each_entry(e, &t->iphash[st->bucket], list) | |
404bdbfd PM |
491 | if (p-- == 0) |
492 | return e; | |
404bdbfd PM |
493 | return NULL; |
494 | } | |
1da177e4 | 495 | |
404bdbfd PM |
496 | static void *recent_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
497 | { | |
498 | struct recent_iter_state *st = seq->private; | |
3cf93c96 | 499 | const struct recent_table *t = st->table; |
079aa88f JE |
500 | const struct recent_entry *e = v; |
501 | const struct list_head *head = e->list.next; | |
404bdbfd | 502 | |
db25517a | 503 | (*pos)++; |
404bdbfd PM |
504 | while (head == &t->iphash[st->bucket]) { |
505 | if (++st->bucket >= ip_list_hash_size) | |
506 | return NULL; | |
507 | head = t->iphash[st->bucket].next; | |
508 | } | |
404bdbfd | 509 | return list_entry(head, struct recent_entry, list); |
1da177e4 LT |
510 | } |
511 | ||
404bdbfd | 512 | static void recent_seq_stop(struct seq_file *s, void *v) |
855304af | 513 | __releases(recent_lock) |
1da177e4 | 514 | { |
404bdbfd PM |
515 | spin_unlock_bh(&recent_lock); |
516 | } | |
1da177e4 | 517 | |
404bdbfd PM |
518 | static int recent_seq_show(struct seq_file *seq, void *v) |
519 | { | |
3cf93c96 | 520 | const struct recent_entry *e = v; |
abc86d0f FW |
521 | struct recent_iter_state *st = seq->private; |
522 | const struct recent_table *t = st->table; | |
404bdbfd PM |
523 | unsigned int i; |
524 | ||
abc86d0f FW |
525 | i = (e->index - 1) & t->nstamps_max_mask; |
526 | ||
ee999d8b | 527 | if (e->family == NFPROTO_IPV4) |
14d5e834 HH |
528 | seq_printf(seq, "src=%pI4 ttl: %u last_seen: %lu oldest_pkt: %u", |
529 | &e->addr.ip, e->ttl, e->stamps[i], e->index); | |
079aa88f | 530 | else |
5b095d98 | 531 | seq_printf(seq, "src=%pI6 ttl: %u last_seen: %lu oldest_pkt: %u", |
38ff4fa4 | 532 | &e->addr.in6, e->ttl, e->stamps[i], e->index); |
404bdbfd PM |
533 | for (i = 0; i < e->nstamps; i++) |
534 | seq_printf(seq, "%s %lu", i ? "," : "", e->stamps[i]); | |
cdec2685 | 535 | seq_putc(seq, '\n'); |
404bdbfd PM |
536 | return 0; |
537 | } | |
1da177e4 | 538 | |
56b3d975 | 539 | static const struct seq_operations recent_seq_ops = { |
404bdbfd PM |
540 | .start = recent_seq_start, |
541 | .next = recent_seq_next, | |
542 | .stop = recent_seq_stop, | |
543 | .show = recent_seq_show, | |
544 | }; | |
1da177e4 | 545 | |
404bdbfd PM |
546 | static int recent_seq_open(struct inode *inode, struct file *file) |
547 | { | |
404bdbfd | 548 | struct recent_iter_state *st; |
404bdbfd | 549 | |
e2da5913 | 550 | st = __seq_open_private(file, &recent_seq_ops, sizeof(*st)); |
404bdbfd PM |
551 | if (st == NULL) |
552 | return -ENOMEM; | |
3af8e31c | 553 | |
d9dda78b | 554 | st->table = PDE_DATA(inode); |
e2da5913 | 555 | return 0; |
404bdbfd | 556 | } |
1da177e4 | 557 | |
079aa88f JE |
558 | static ssize_t |
559 | recent_mt_proc_write(struct file *file, const char __user *input, | |
560 | size_t size, loff_t *loff) | |
561 | { | |
d9dda78b | 562 | struct recent_table *t = PDE_DATA(file_inode(file)); |
079aa88f JE |
563 | struct recent_entry *e; |
564 | char buf[sizeof("+b335:1d35:1e55:dead:c0de:1715:5afe:c0de")]; | |
565 | const char *c = buf; | |
325fb5b4 | 566 | union nf_inet_addr addr = {}; |
079aa88f JE |
567 | u_int16_t family; |
568 | bool add, succ; | |
569 | ||
570 | if (size == 0) | |
571 | return 0; | |
572 | if (size > sizeof(buf)) | |
573 | size = sizeof(buf); | |
574 | if (copy_from_user(buf, input, size) != 0) | |
575 | return -EFAULT; | |
576 | ||
577 | /* Strict protocol! */ | |
578 | if (*loff != 0) | |
579 | return -ESPIPE; | |
580 | switch (*c) { | |
581 | case '/': /* flush table */ | |
582 | spin_lock_bh(&recent_lock); | |
583 | recent_table_flush(t); | |
584 | spin_unlock_bh(&recent_lock); | |
585 | return size; | |
586 | case '-': /* remove address */ | |
587 | add = false; | |
588 | break; | |
589 | case '+': /* add address */ | |
590 | add = true; | |
591 | break; | |
592 | default: | |
b2606644 | 593 | pr_info_ratelimited("Need \"+ip\", \"-ip\" or \"/\"\n"); |
079aa88f JE |
594 | return -EINVAL; |
595 | } | |
596 | ||
597 | ++c; | |
598 | --size; | |
599 | if (strnchr(c, size, ':') != NULL) { | |
ee999d8b | 600 | family = NFPROTO_IPV6; |
079aa88f JE |
601 | succ = in6_pton(c, size, (void *)&addr, '\n', NULL); |
602 | } else { | |
ee999d8b | 603 | family = NFPROTO_IPV4; |
079aa88f JE |
604 | succ = in4_pton(c, size, (void *)&addr, '\n', NULL); |
605 | } | |
606 | ||
b2606644 | 607 | if (!succ) |
079aa88f | 608 | return -EINVAL; |
079aa88f JE |
609 | |
610 | spin_lock_bh(&recent_lock); | |
611 | e = recent_entry_lookup(t, &addr, family, 0); | |
612 | if (e == NULL) { | |
613 | if (add) | |
614 | recent_entry_init(t, &addr, family, 0); | |
615 | } else { | |
616 | if (add) | |
617 | recent_entry_update(t, e); | |
618 | else | |
619 | recent_entry_remove(t, e); | |
620 | } | |
621 | spin_unlock_bh(&recent_lock); | |
622 | /* Note we removed one above */ | |
623 | *loff += size + 1; | |
624 | return size + 1; | |
625 | } | |
626 | ||
97a32539 AD |
627 | static const struct proc_ops recent_mt_proc_ops = { |
628 | .proc_open = recent_seq_open, | |
629 | .proc_read = seq_read, | |
630 | .proc_write = recent_mt_proc_write, | |
631 | .proc_release = seq_release_private, | |
632 | .proc_lseek = seq_lseek, | |
079aa88f | 633 | }; |
7d07d563 AD |
634 | |
635 | static int __net_init recent_proc_net_init(struct net *net) | |
636 | { | |
637 | struct recent_net *recent_net = recent_pernet(net); | |
638 | ||
639 | recent_net->xt_recent = proc_mkdir("xt_recent", net->proc_net); | |
640 | if (!recent_net->xt_recent) | |
641 | return -ENOMEM; | |
7d07d563 AD |
642 | return 0; |
643 | } | |
644 | ||
645 | static void __net_exit recent_proc_net_exit(struct net *net) | |
646 | { | |
665e205c VL |
647 | struct recent_net *recent_net = recent_pernet(net); |
648 | struct recent_table *t; | |
649 | ||
650 | /* recent_net_exit() is called before recent_mt_destroy(). Make sure | |
4b7ddc58 | 651 | * that the parent xt_recent proc entry is empty before trying to |
665e205c VL |
652 | * remove it. |
653 | */ | |
654 | spin_lock_bh(&recent_lock); | |
655 | list_for_each_entry(t, &recent_net->tables, list) | |
656 | remove_proc_entry(t->name, recent_net->xt_recent); | |
657 | ||
658 | recent_net->xt_recent = NULL; | |
659 | spin_unlock_bh(&recent_lock); | |
660 | ||
ece31ffd | 661 | remove_proc_entry("xt_recent", net->proc_net); |
7d07d563 AD |
662 | } |
663 | #else | |
664 | static inline int recent_proc_net_init(struct net *net) | |
665 | { | |
666 | return 0; | |
667 | } | |
668 | ||
669 | static inline void recent_proc_net_exit(struct net *net) | |
670 | { | |
671 | } | |
1da177e4 | 672 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 673 | |
7d07d563 AD |
674 | static int __net_init recent_net_init(struct net *net) |
675 | { | |
676 | struct recent_net *recent_net = recent_pernet(net); | |
677 | ||
678 | INIT_LIST_HEAD(&recent_net->tables); | |
679 | return recent_proc_net_init(net); | |
680 | } | |
681 | ||
682 | static void __net_exit recent_net_exit(struct net *net) | |
683 | { | |
7d07d563 AD |
684 | recent_proc_net_exit(net); |
685 | } | |
686 | ||
687 | static struct pernet_operations recent_net_ops = { | |
688 | .init = recent_net_init, | |
689 | .exit = recent_net_exit, | |
690 | .id = &recent_net_id, | |
691 | .size = sizeof(struct recent_net), | |
692 | }; | |
693 | ||
079aa88f JE |
694 | static struct xt_match recent_mt_reg[] __read_mostly = { |
695 | { | |
696 | .name = "recent", | |
697 | .revision = 0, | |
ee999d8b | 698 | .family = NFPROTO_IPV4, |
079aa88f JE |
699 | .match = recent_mt, |
700 | .matchsize = sizeof(struct xt_recent_mtinfo), | |
efdedd54 | 701 | .checkentry = recent_mt_check_v0, |
079aa88f JE |
702 | .destroy = recent_mt_destroy, |
703 | .me = THIS_MODULE, | |
704 | }, | |
705 | { | |
706 | .name = "recent", | |
707 | .revision = 0, | |
ee999d8b | 708 | .family = NFPROTO_IPV6, |
079aa88f JE |
709 | .match = recent_mt, |
710 | .matchsize = sizeof(struct xt_recent_mtinfo), | |
efdedd54 DF |
711 | .checkentry = recent_mt_check_v0, |
712 | .destroy = recent_mt_destroy, | |
713 | .me = THIS_MODULE, | |
714 | }, | |
715 | { | |
716 | .name = "recent", | |
717 | .revision = 1, | |
718 | .family = NFPROTO_IPV4, | |
719 | .match = recent_mt, | |
720 | .matchsize = sizeof(struct xt_recent_mtinfo_v1), | |
721 | .checkentry = recent_mt_check_v1, | |
079aa88f JE |
722 | .destroy = recent_mt_destroy, |
723 | .me = THIS_MODULE, | |
724 | }, | |
efdedd54 DF |
725 | { |
726 | .name = "recent", | |
727 | .revision = 1, | |
728 | .family = NFPROTO_IPV6, | |
729 | .match = recent_mt, | |
730 | .matchsize = sizeof(struct xt_recent_mtinfo_v1), | |
731 | .checkentry = recent_mt_check_v1, | |
732 | .destroy = recent_mt_destroy, | |
733 | .me = THIS_MODULE, | |
734 | } | |
1da177e4 LT |
735 | }; |
736 | ||
d3c5ee6d | 737 | static int __init recent_mt_init(void) |
1da177e4 | 738 | { |
404bdbfd | 739 | int err; |
1da177e4 | 740 | |
abc86d0f FW |
741 | BUILD_BUG_ON_NOT_POWER_OF_2(XT_RECENT_MAX_NSTAMPS); |
742 | ||
743 | if (!ip_list_tot || ip_pkt_list_tot >= XT_RECENT_MAX_NSTAMPS) | |
404bdbfd PM |
744 | return -EINVAL; |
745 | ip_list_hash_size = 1 << fls(ip_list_tot); | |
1da177e4 | 746 | |
7d07d563 | 747 | err = register_pernet_subsys(&recent_net_ops); |
1da177e4 | 748 | if (err) |
404bdbfd | 749 | return err; |
7d07d563 AD |
750 | err = xt_register_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg)); |
751 | if (err) | |
752 | unregister_pernet_subsys(&recent_net_ops); | |
1da177e4 LT |
753 | return err; |
754 | } | |
755 | ||
d3c5ee6d | 756 | static void __exit recent_mt_exit(void) |
1da177e4 | 757 | { |
079aa88f | 758 | xt_unregister_matches(recent_mt_reg, ARRAY_SIZE(recent_mt_reg)); |
7d07d563 | 759 | unregister_pernet_subsys(&recent_net_ops); |
1da177e4 LT |
760 | } |
761 | ||
d3c5ee6d JE |
762 | module_init(recent_mt_init); |
763 | module_exit(recent_mt_exit); |