--- zzzz-none-000/linux-4.9.218/net/netfilter/nf_conntrack_standalone.c 2020-04-02 15:20:41.000000000 +0000 +++ seale-7590ac-750/linux-4.9.218/net/netfilter/nf_conntrack_standalone.c 2022-11-30 09:46:20.000000000 +0000 @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef CONFIG_SYSCTL #include @@ -298,11 +299,67 @@ 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, kill_matching, &kr, 0, 0); + + return count; +} + static const struct file_operations ct_file_ops = { - .owner = THIS_MODULE, - .open = ct_open, - .read = seq_read, - .llseek = seq_lseek, + .owner = THIS_MODULE, + .open = ct_open, + .read = seq_read, + .write = ct_file_write, + .llseek = seq_lseek, .release = seq_release_net, }; @@ -405,7 +462,7 @@ 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;