From: Felix Fietkau Subject: netfilter: add support for flushing conntrack via /proc lede-commit 8193bbe59a74d34d6a26d4a8cb857b1952905314 Signed-off-by: Felix Fietkau --- net/netfilter/nf_conntrack_standalone.c | 59 ++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) --- a/net/netfilter/nf_conntrack_standalone.c +++ b/net/netfilter/nf_conntrack_standalone.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef CONFIG_SYSCTL #include @@ -382,10 +383,66 @@ static int ct_open(struct inode *inode, sizeof(struct ct_iter_state)); } +struct kill_request { + u16 family; + union nf_inet_addr addr; +}; + +static int kill_matching(struct nf_conn *i, void *data) +{ + struct kill_request *kr = data; + struct nf_conntrack_tuple *t1 = &i->tuplehash[IP_CT_DIR_ORIGINAL].tuple; + struct nf_conntrack_tuple *t2 = &i->tuplehash[IP_CT_DIR_REPLY].tuple; + + if (!kr->family) + return 1; + + if (t1->src.l3num != kr->family) + return 0; + + return (nf_inet_addr_cmp(&kr->addr, &t1->src.u3) || + nf_inet_addr_cmp(&kr->addr, &t1->dst.u3) || + nf_inet_addr_cmp(&kr->addr, &t2->src.u3) || + nf_inet_addr_cmp(&kr->addr, &t2->dst.u3)); +} + +static ssize_t ct_file_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct seq_file *seq = file->private_data; + struct net *net = seq_file_net(seq); + struct kill_request kr = { }; + char req[INET6_ADDRSTRLEN] = { }; + + if (count == 0) + return 0; + + if (count >= INET6_ADDRSTRLEN) + count = INET6_ADDRSTRLEN - 1; + + if (copy_from_user(req, buf, count)) + return -EFAULT; + + if (strnchr(req, count, ':')) { + kr.family = AF_INET6; + if (!in6_pton(req, count, (void *)&kr.addr, '\n', NULL)) + return -EINVAL; + } else if (strnchr(req, count, '.')) { + kr.family = AF_INET; + if (!in4_pton(req, count, (void *)&kr.addr, '\n', NULL)) + return -EINVAL; + } + + nf_ct_iterate_cleanup_net(net, kill_matching, &kr, 0, 0); + + return count; +} + static const struct file_operations ct_file_ops = { .owner = THIS_MODULE, .open = ct_open, .read = seq_read, + .write = ct_file_write, .llseek = seq_lseek, .release = seq_release_net, }; @@ -489,7 +546,7 @@ static int nf_conntrack_standalone_init_ kuid_t root_uid; kgid_t root_gid; - pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops); + pde = proc_create("nf_conntrack", 0660, net->proc_net, &ct_file_ops); if (!pde) goto out_nf_conntrack;