1 // SPDX-License-Identifier: GPL-2.0-only
2 /* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
3  */
4 
5 #include <linux/module.h>
6 #include <linux/udp.h>
7 
8 #include <net/netfilter/nf_conntrack_helper.h>
9 #include <net/netfilter/nf_conntrack_expect.h>
10 #include <net/netfilter/nf_nat_helper.h>
11 #include <linux/netfilter/nf_conntrack_tftp.h>
12 
13 #define NAT_HELPER_NAME "tftp"
14 
15 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
16 MODULE_DESCRIPTION("TFTP NAT helper");
17 MODULE_LICENSE("GPL");
18 MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME);
19 
20 static struct nf_conntrack_nat_helper nat_helper_tftp =
21 	NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME);
22 
help(struct sk_buff * skb,enum ip_conntrack_info ctinfo,struct nf_conntrack_expect * exp)23 static unsigned int help(struct sk_buff *skb,
24 			 enum ip_conntrack_info ctinfo,
25 			 struct nf_conntrack_expect *exp)
26 {
27 	const struct nf_conn *ct = exp->master;
28 
29 	exp->saved_proto.udp.port
30 		= ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
31 	exp->dir = IP_CT_DIR_REPLY;
32 	exp->expectfn = nf_nat_follow_master;
33 	if (nf_ct_expect_related(exp, 0) != 0) {
34 		nf_ct_helper_log(skb, exp->master, "cannot add expectation");
35 		return NF_DROP;
36 	}
37 	return NF_ACCEPT;
38 }
39 
nf_nat_tftp_fini(void)40 static void __exit nf_nat_tftp_fini(void)
41 {
42 	nf_nat_helper_unregister(&nat_helper_tftp);
43 	RCU_INIT_POINTER(nf_nat_tftp_hook, NULL);
44 	synchronize_rcu();
45 }
46 
nf_nat_tftp_init(void)47 static int __init nf_nat_tftp_init(void)
48 {
49 	BUG_ON(nf_nat_tftp_hook != NULL);
50 	nf_nat_helper_register(&nat_helper_tftp);
51 	RCU_INIT_POINTER(nf_nat_tftp_hook, help);
52 	return 0;
53 }
54 
55 module_init(nf_nat_tftp_init);
56 module_exit(nf_nat_tftp_fini);
57