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