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  */
0004 
0005 #include <linux/module.h>
0006 #include <linux/udp.h>
0007 
0008 #include <net/netfilter/nf_conntrack_helper.h>
0009 #include <net/netfilter/nf_conntrack_expect.h>
0010 #include <net/netfilter/nf_nat_helper.h>
0011 #include <linux/netfilter/nf_conntrack_tftp.h>
0012 
0013 #define NAT_HELPER_NAME "tftp"
0014 
0015 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
0016 MODULE_DESCRIPTION("TFTP NAT helper");
0017 MODULE_LICENSE("GPL");
0018 MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME);
0019 
0020 static struct nf_conntrack_nat_helper nat_helper_tftp =
0021     NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME);
0022 
0023 static unsigned int help(struct sk_buff *skb,
0024              enum ip_conntrack_info ctinfo,
0025              struct nf_conntrack_expect *exp)
0026 {
0027     const struct nf_conn *ct = exp->master;
0028 
0029     exp->saved_proto.udp.port
0030         = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
0031     exp->dir = IP_CT_DIR_REPLY;
0032     exp->expectfn = nf_nat_follow_master;
0033     if (nf_ct_expect_related(exp, 0) != 0) {
0034         nf_ct_helper_log(skb, exp->master, "cannot add expectation");
0035         return NF_DROP;
0036     }
0037     return NF_ACCEPT;
0038 }
0039 
0040 static void __exit nf_nat_tftp_fini(void)
0041 {
0042     nf_nat_helper_unregister(&nat_helper_tftp);
0043     RCU_INIT_POINTER(nf_nat_tftp_hook, NULL);
0044     synchronize_rcu();
0045 }
0046 
0047 static int __init nf_nat_tftp_init(void)
0048 {
0049     BUG_ON(nf_nat_tftp_hook != NULL);
0050     nf_nat_helper_register(&nat_helper_tftp);
0051     RCU_INIT_POINTER(nf_nat_tftp_hook, help);
0052     return 0;
0053 }
0054 
0055 module_init(nf_nat_tftp_init);
0056 module_exit(nf_nat_tftp_fini);