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