Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
0003  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
0004  */
0005 
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007 
0008 #include <linux/module.h>
0009 #include <linux/moduleparam.h>
0010 #include <linux/in.h>
0011 #include <linux/udp.h>
0012 #include <linux/netfilter.h>
0013 
0014 #include <net/netfilter/nf_conntrack.h>
0015 #include <net/netfilter/nf_conntrack_tuple.h>
0016 #include <net/netfilter/nf_conntrack_expect.h>
0017 #include <net/netfilter/nf_conntrack_ecache.h>
0018 #include <net/netfilter/nf_conntrack_helper.h>
0019 #include <linux/netfilter/nf_conntrack_tftp.h>
0020 
0021 #define HELPER_NAME "tftp"
0022 
0023 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
0024 MODULE_DESCRIPTION("TFTP connection tracking helper");
0025 MODULE_LICENSE("GPL");
0026 MODULE_ALIAS("ip_conntrack_tftp");
0027 MODULE_ALIAS_NFCT_HELPER(HELPER_NAME);
0028 
0029 #define MAX_PORTS 8
0030 static unsigned short ports[MAX_PORTS];
0031 static unsigned int ports_c;
0032 module_param_array(ports, ushort, &ports_c, 0400);
0033 MODULE_PARM_DESC(ports, "Port numbers of TFTP servers");
0034 
0035 unsigned int (*nf_nat_tftp_hook)(struct sk_buff *skb,
0036                  enum ip_conntrack_info ctinfo,
0037                  struct nf_conntrack_expect *exp) __read_mostly;
0038 EXPORT_SYMBOL_GPL(nf_nat_tftp_hook);
0039 
0040 static int tftp_help(struct sk_buff *skb,
0041              unsigned int protoff,
0042              struct nf_conn *ct,
0043              enum ip_conntrack_info ctinfo)
0044 {
0045     const struct tftphdr *tfh;
0046     struct tftphdr _tftph;
0047     struct nf_conntrack_expect *exp;
0048     struct nf_conntrack_tuple *tuple;
0049     unsigned int ret = NF_ACCEPT;
0050     typeof(nf_nat_tftp_hook) nf_nat_tftp;
0051 
0052     tfh = skb_header_pointer(skb, protoff + sizeof(struct udphdr),
0053                  sizeof(_tftph), &_tftph);
0054     if (tfh == NULL)
0055         return NF_ACCEPT;
0056 
0057     switch (ntohs(tfh->opcode)) {
0058     case TFTP_OPCODE_READ:
0059     case TFTP_OPCODE_WRITE:
0060         /* RRQ and WRQ works the same way */
0061         nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
0062         nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
0063 
0064         exp = nf_ct_expect_alloc(ct);
0065         if (exp == NULL) {
0066             nf_ct_helper_log(skb, ct, "cannot alloc expectation");
0067             return NF_DROP;
0068         }
0069         tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
0070         nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
0071                   nf_ct_l3num(ct),
0072                   &tuple->src.u3, &tuple->dst.u3,
0073                   IPPROTO_UDP, NULL, &tuple->dst.u.udp.port);
0074 
0075         pr_debug("expect: ");
0076         nf_ct_dump_tuple(&exp->tuple);
0077 
0078         nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook);
0079         if (nf_nat_tftp && ct->status & IPS_NAT_MASK)
0080             ret = nf_nat_tftp(skb, ctinfo, exp);
0081         else if (nf_ct_expect_related(exp, 0) != 0) {
0082             nf_ct_helper_log(skb, ct, "cannot add expectation");
0083             ret = NF_DROP;
0084         }
0085         nf_ct_expect_put(exp);
0086         break;
0087     case TFTP_OPCODE_DATA:
0088     case TFTP_OPCODE_ACK:
0089         pr_debug("Data/ACK opcode\n");
0090         break;
0091     case TFTP_OPCODE_ERROR:
0092         pr_debug("Error opcode\n");
0093         break;
0094     default:
0095         pr_debug("Unknown opcode\n");
0096     }
0097     return ret;
0098 }
0099 
0100 static struct nf_conntrack_helper tftp[MAX_PORTS * 2] __read_mostly;
0101 
0102 static const struct nf_conntrack_expect_policy tftp_exp_policy = {
0103     .max_expected   = 1,
0104     .timeout    = 5 * 60,
0105 };
0106 
0107 static void __exit nf_conntrack_tftp_fini(void)
0108 {
0109     nf_conntrack_helpers_unregister(tftp, ports_c * 2);
0110 }
0111 
0112 static int __init nf_conntrack_tftp_init(void)
0113 {
0114     int i, ret;
0115 
0116     NF_CT_HELPER_BUILD_BUG_ON(0);
0117 
0118     if (ports_c == 0)
0119         ports[ports_c++] = TFTP_PORT;
0120 
0121     for (i = 0; i < ports_c; i++) {
0122         nf_ct_helper_init(&tftp[2 * i], AF_INET, IPPROTO_UDP,
0123                   HELPER_NAME, TFTP_PORT, ports[i], i,
0124                   &tftp_exp_policy, 0, tftp_help, NULL,
0125                   THIS_MODULE);
0126         nf_ct_helper_init(&tftp[2 * i + 1], AF_INET6, IPPROTO_UDP,
0127                   HELPER_NAME, TFTP_PORT, ports[i], i,
0128                   &tftp_exp_policy, 0, tftp_help, NULL,
0129                   THIS_MODULE);
0130     }
0131 
0132     ret = nf_conntrack_helpers_register(tftp, ports_c * 2);
0133     if (ret < 0) {
0134         pr_err("failed to register helpers\n");
0135         return ret;
0136     }
0137     return 0;
0138 }
0139 
0140 module_init(nf_conntrack_tftp_init);
0141 module_exit(nf_conntrack_tftp_fini);