1 // SPDX-License-Identifier: GPL-2.0-only
2 /* FTP extension for TCP NAT alteration. */
4 /* (C) 1999-2001 Paul `Rusty' Russell
5 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/inet.h>
13 #include <linux/tcp.h>
14 #include <linux/netfilter_ipv4.h>
15 #include <net/netfilter/nf_nat.h>
16 #include <net/netfilter/nf_nat_helper.h>
17 #include <net/netfilter/nf_conntrack_helper.h>
18 #include <net/netfilter/nf_conntrack_expect.h>
19 #include <linux/netfilter/nf_conntrack_ftp.h>
21 #define NAT_HELPER_NAME "ftp"
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
25 MODULE_DESCRIPTION("ftp NAT helper");
26 MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME
);
28 /* FIXME: Time out? --RR */
30 static struct nf_conntrack_nat_helper nat_helper_ftp
=
31 NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME
);
33 static int nf_nat_ftp_fmt_cmd(struct nf_conn
*ct
, enum nf_ct_ftp_type type
,
34 char *buffer
, size_t buflen
,
35 union nf_inet_addr
*addr
, u16 port
)
40 return snprintf(buffer
, buflen
, "%u,%u,%u,%u,%u,%u",
41 ((unsigned char *)&addr
->ip
)[0],
42 ((unsigned char *)&addr
->ip
)[1],
43 ((unsigned char *)&addr
->ip
)[2],
44 ((unsigned char *)&addr
->ip
)[3],
48 if (nf_ct_l3num(ct
) == NFPROTO_IPV4
)
49 return snprintf(buffer
, buflen
, "|1|%pI4|%u|",
52 return snprintf(buffer
, buflen
, "|2|%pI6|%u|",
55 return snprintf(buffer
, buflen
, "|||%u|", port
);
61 /* So, this packet has hit the connection tracking matching code.
62 Mangle it, and change the expectation to match the new version. */
63 static unsigned int nf_nat_ftp(struct sk_buff
*skb
,
64 enum ip_conntrack_info ctinfo
,
65 enum nf_ct_ftp_type type
,
67 unsigned int matchoff
,
68 unsigned int matchlen
,
69 struct nf_conntrack_expect
*exp
)
71 union nf_inet_addr newaddr
;
73 int dir
= CTINFO2DIR(ctinfo
);
74 struct nf_conn
*ct
= exp
->master
;
75 char buffer
[sizeof("|1||65535|") + INET6_ADDRSTRLEN
];
78 pr_debug("type %i, off %u len %u\n", type
, matchoff
, matchlen
);
80 /* Connection will come from wherever this packet goes, hence !dir */
81 newaddr
= ct
->tuplehash
[!dir
].tuple
.dst
.u3
;
82 exp
->saved_proto
.tcp
.port
= exp
->tuple
.dst
.u
.tcp
.port
;
85 /* When you see the packet, we need to NAT it the same as the
87 exp
->expectfn
= nf_nat_follow_master
;
89 /* Try to get same port: if not, try to change it. */
90 for (port
= ntohs(exp
->saved_proto
.tcp
.port
); port
!= 0; port
++) {
93 exp
->tuple
.dst
.u
.tcp
.port
= htons(port
);
94 ret
= nf_ct_expect_related(exp
, 0);
97 else if (ret
!= -EBUSY
) {
104 nf_ct_helper_log(skb
, ct
, "all ports in use");
108 buflen
= nf_nat_ftp_fmt_cmd(ct
, type
, buffer
, sizeof(buffer
),
113 pr_debug("calling nf_nat_mangle_tcp_packet\n");
115 if (!nf_nat_mangle_tcp_packet(skb
, ct
, ctinfo
, protoff
, matchoff
,
116 matchlen
, buffer
, buflen
))
122 nf_ct_helper_log(skb
, ct
, "cannot mangle packet");
123 nf_ct_unexpect_related(exp
);
127 static void __exit
nf_nat_ftp_fini(void)
129 nf_nat_helper_unregister(&nat_helper_ftp
);
130 RCU_INIT_POINTER(nf_nat_ftp_hook
, NULL
);
134 static int __init
nf_nat_ftp_init(void)
136 BUG_ON(nf_nat_ftp_hook
!= NULL
);
137 nf_nat_helper_register(&nat_helper_ftp
);
138 RCU_INIT_POINTER(nf_nat_ftp_hook
, nf_nat_ftp
);
142 /* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
143 static int warn_set(const char *val
, const struct kernel_param
*kp
)
145 pr_info("kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
148 module_param_call(ports
, warn_set
, NULL
, NULL
, 0);
150 module_init(nf_nat_ftp_init
);
151 module_exit(nf_nat_ftp_fini
);