kernel: add support for kernel 5.4
[openwrt/openwrt.git] / target / linux / generic / pending-5.4 / 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 @@ -454,6 +455,56 @@ 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 net *net = seq_file_net(seq);
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 + nf_ct_iterate_cleanup_net(net, kill_matching, &kr, 0, 0);
70 +
71 + return 0;
72 +}
73 +
74 static const struct seq_operations ct_cpu_seq_ops = {
75 .start = ct_cpu_seq_start,
76 .next = ct_cpu_seq_next,
77 @@ -467,8 +518,9 @@ static int nf_conntrack_standalone_init_
78 kuid_t root_uid;
79 kgid_t root_gid;
80
81 - pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
82 - sizeof(struct ct_iter_state));
83 + pde = proc_create_net_data_write("nf_conntrack", 0440, net->proc_net,
84 + &ct_seq_ops, &ct_file_write,
85 + sizeof(struct ct_iter_state), NULL);
86 if (!pde)
87 goto out_nf_conntrack;
88