--- zzzz-none-000/linux-4.4.271/net/netfilter/nf_conntrack_standalone.c 2021-06-03 06:22:09.000000000 +0000 +++ maple-fsgw-759/linux-4.4.271/net/netfilter/nf_conntrack_standalone.c 2023-12-20 10:37:40.000000000 +0000 @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef CONFIG_SYSCTL #include @@ -288,10 +289,66 @@ 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, + .write = ct_file_write, .llseek = seq_lseek, .release = seq_release_net, }; @@ -393,7 +450,7 @@ { struct proc_dir_entry *pde; - 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;