generic: 6.1: fixup QCA807x upstream PHY
[openwrt/openwrt.git] / target / linux / generic / pending-6.1 / 600-netfilter_conntrack_flush.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Subject: netfilter: add support for flushing conntrack via /proc
3
4 lede-commit 8193bbe59a74d34d6a26d4a8cb857b1952905314
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
6 ---
7 net/netfilter/nf_conntrack_standalone.c | 59 ++++++++++++++++++++++++++++++++-
8 1 file changed, 58 insertions(+), 1 deletion(-)
9
10 --- a/net/netfilter/nf_conntrack_standalone.c
11 +++ b/net/netfilter/nf_conntrack_standalone.c
12 @@ -9,6 +9,7 @@
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>
18 #ifdef CONFIG_SYSCTL
19 #include <linux/sysctl.h>
20 @@ -465,6 +466,58 @@ static int ct_cpu_seq_show(struct seq_fi
21 return 0;
22 }
23
24 +struct kill_request {
25 + u16 family;
26 + union nf_inet_addr addr;
27 +};
28 +
29 +static int kill_matching(struct nf_conn *i, void *data)
30 +{
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;
34 +
35 + if (!kr->family)
36 + return 1;
37 +
38 + if (t1->src.l3num != kr->family)
39 + return 0;
40 +
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));
45 +}
46 +
47 +static int ct_file_write(struct file *file, char *buf, size_t count)
48 +{
49 + struct seq_file *seq = file->private_data;
50 + struct nf_ct_iter_data iter_data;
51 + struct kill_request kr = { };
52 +
53 + if (count == 0)
54 + return 0;
55 +
56 + if (count >= INET6_ADDRSTRLEN)
57 + count = INET6_ADDRSTRLEN - 1;
58 +
59 + if (strnchr(buf, count, ':')) {
60 + kr.family = AF_INET6;
61 + if (!in6_pton(buf, count, (void *)&kr.addr, '\n', NULL))
62 + return -EINVAL;
63 + } else if (strnchr(buf, count, '.')) {
64 + kr.family = AF_INET;
65 + if (!in4_pton(buf, count, (void *)&kr.addr, '\n', NULL))
66 + return -EINVAL;
67 + }
68 +
69 + iter_data.net = seq_file_net(seq);
70 + iter_data.data = &kr;
71 + nf_ct_iterate_cleanup_net(kill_matching, &iter_data);
72 +
73 + return 0;
74 +}
75 +
76 static const struct seq_operations ct_cpu_seq_ops = {
77 .start = ct_cpu_seq_start,
78 .next = ct_cpu_seq_next,
79 @@ -478,8 +531,9 @@ static int nf_conntrack_standalone_init_
80 kuid_t root_uid;
81 kgid_t root_gid;
82
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);
88 if (!pde)
89 goto out_nf_conntrack;
90