netfilter: support /proc conntrack flushing of specific ip addresses
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-3.9 / 604-netfilter_conntrack_flush.patch
1 --- a/net/netfilter/nf_conntrack_standalone.c
2 +++ b/net/netfilter/nf_conntrack_standalone.c
3 @@ -16,6 +16,7 @@
4 #include <linux/percpu.h>
5 #include <linux/netdevice.h>
6 #include <linux/security.h>
7 +#include <linux/inet.h>
8 #include <net/net_namespace.h>
9 #ifdef CONFIG_SYSCTL
10 #include <linux/sysctl.h>
11 @@ -267,10 +268,63 @@ static int ct_open(struct inode *inode,
12 sizeof(struct ct_iter_state));
13 }
14
15 +struct kill_request {
16 + u16 family;
17 + union nf_inet_addr addr;
18 +};
19 +
20 +static int kill_matching(struct nf_conn *i, void *data)
21 +{
22 + struct kill_request *kr = data;
23 + struct nf_conntrack_tuple *t = &i->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
24 +
25 + if (!kr->family)
26 + return 1;
27 +
28 + if (t->src.l3num != kr->family)
29 + return 0;
30 +
31 + return (nf_inet_addr_cmp(&kr->addr, &t->src.u3) ||
32 + nf_inet_addr_cmp(&kr->addr, &t->dst.u3));
33 +}
34 +
35 +static ssize_t ct_file_write(struct file *file, const char __user *buf,
36 + size_t count, loff_t *ppos)
37 +{
38 + struct seq_file *seq = file->private_data;
39 + struct net *net = seq_file_net(seq);
40 + struct kill_request kr = { };
41 + char req[INET6_ADDRSTRLEN] = { };
42 +
43 + if (count == 0)
44 + return 0;
45 +
46 + if (count >= INET6_ADDRSTRLEN)
47 + count = INET6_ADDRSTRLEN - 1;
48 +
49 + if (copy_from_user(req, buf, count))
50 + return -EFAULT;
51 +
52 + if (strnchr(req, count, ':')) {
53 + kr.family = AF_INET6;
54 + if (!in6_pton(req, count, (void *)&kr.addr, '\n', NULL))
55 + return -EINVAL;
56 + } else if (strnchr(req, count, '.')) {
57 + kr.family = AF_INET;
58 + if (!in4_pton(req, count, (void *)&kr.addr, '\n', NULL))
59 + return -EINVAL;
60 + }
61 +
62 + nf_ct_iterate_cleanup(net, kill_matching, &kr);
63 +
64 + return count;
65 +}
66 +
67 static const struct file_operations ct_file_ops = {
68 .owner = THIS_MODULE,
69 .open = ct_open,
70 .read = seq_read,
71 + .write = ct_file_write,
72 .llseek = seq_lseek,
73 .release = seq_release_net,
74 };
75 @@ -372,7 +426,7 @@ static int nf_conntrack_standalone_init_
76 {
77 struct proc_dir_entry *pde;
78
79 - pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
80 + pde = proc_create("nf_conntrack", 0660, net->proc_net, &ct_file_ops);
81 if (!pde)
82 goto out_nf_conntrack;
83