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