1 From: Felix Fietkau <nbd@nbd.name>
2 Subject: netfilter: add support for flushing conntrack via /proc
4 lede-commit 8193bbe59a74d34d6a26d4a8cb857b1952905314
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 net/netfilter/nf_conntrack_standalone.c | 59 ++++++++++++++++++++++++++++++++-
8 1 file changed, 58 insertions(+), 1 deletion(-)
10 --- a/net/netfilter/nf_conntrack_standalone.c
11 +++ b/net/netfilter/nf_conntrack_standalone.c
13 #include <linux/percpu.h>
14 #include <linux/netdevice.h>
15 #include <linux/security.h>
16 +#include <linux/inet.h>
17 #include <net/net_namespace.h>
19 #include <linux/sysctl.h>
20 @@ -461,6 +462,58 @@ static int ct_cpu_seq_show(struct seq_fi
24 +struct kill_request {
26 + union nf_inet_addr addr;
29 +static int kill_matching(struct nf_conn *i, void *data)
31 + struct kill_request *kr = data;
32 + struct nf_conntrack_tuple *t1 = &i->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
33 + struct nf_conntrack_tuple *t2 = &i->tuplehash[IP_CT_DIR_REPLY].tuple;
38 + if (t1->src.l3num != kr->family)
41 + return (nf_inet_addr_cmp(&kr->addr, &t1->src.u3) ||
42 + nf_inet_addr_cmp(&kr->addr, &t1->dst.u3) ||
43 + nf_inet_addr_cmp(&kr->addr, &t2->src.u3) ||
44 + nf_inet_addr_cmp(&kr->addr, &t2->dst.u3));
47 +static int ct_file_write(struct file *file, char *buf, size_t count)
49 + struct seq_file *seq = file->private_data;
50 + struct nf_ct_iter_data iter_data;
51 + struct kill_request kr = { };
56 + if (count >= INET6_ADDRSTRLEN)
57 + count = INET6_ADDRSTRLEN - 1;
59 + if (strnchr(buf, count, ':')) {
60 + kr.family = AF_INET6;
61 + if (!in6_pton(buf, count, (void *)&kr.addr, '\n', NULL))
63 + } else if (strnchr(buf, count, '.')) {
64 + kr.family = AF_INET;
65 + if (!in4_pton(buf, count, (void *)&kr.addr, '\n', NULL))
69 + iter_data.net = seq_file_net(seq);
70 + iter_data.data = &kr;
71 + nf_ct_iterate_cleanup_net(kill_matching, &iter_data);
76 static const struct seq_operations ct_cpu_seq_ops = {
77 .start = ct_cpu_seq_start,
78 .next = ct_cpu_seq_next,
79 @@ -474,8 +527,9 @@ static int nf_conntrack_standalone_init_
83 - pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
84 - sizeof(struct ct_iter_state));
85 + pde = proc_create_net_data_write("nf_conntrack", 0440, net->proc_net,
86 + &ct_seq_ops, &ct_file_write,
87 + sizeof(struct ct_iter_state), NULL);
89 goto out_nf_conntrack;