blob: d76afafdc6993ba2f8e4a03f377b8cad3a34c145 [file] [log] [blame]
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -08001/* FTP extension for TCP NAT alteration. */
2
3/* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
Patrick McHardyd33cbee2012-08-26 19:14:20 +020013#include <linux/inet.h>
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080014#include <linux/tcp.h>
15#include <linux/netfilter_ipv4.h>
16#include <net/netfilter/nf_nat.h>
17#include <net/netfilter/nf_nat_helper.h>
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080018#include <net/netfilter/nf_conntrack_helper.h>
19#include <net/netfilter/nf_conntrack_expect.h>
20#include <linux/netfilter/nf_conntrack_ftp.h>
21
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
24MODULE_DESCRIPTION("ftp NAT helper");
25MODULE_ALIAS("ip_nat_ftp");
26
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080027/* FIXME: Time out? --RR */
28
Patrick McHardyd33cbee2012-08-26 19:14:20 +020029static int nf_nat_ftp_fmt_cmd(struct nf_conn *ct, enum nf_ct_ftp_type type,
Joe Perchesc299bd52010-01-11 11:49:51 +010030 char *buffer, size_t buflen,
Patrick McHardyd33cbee2012-08-26 19:14:20 +020031 union nf_inet_addr *addr, u16 port)
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080032{
Joe Perchesc299bd52010-01-11 11:49:51 +010033 switch (type) {
34 case NF_CT_FTP_PORT:
35 case NF_CT_FTP_PASV:
36 return snprintf(buffer, buflen, "%u,%u,%u,%u,%u,%u",
Patrick McHardyd33cbee2012-08-26 19:14:20 +020037 ((unsigned char *)&addr->ip)[0],
38 ((unsigned char *)&addr->ip)[1],
39 ((unsigned char *)&addr->ip)[2],
40 ((unsigned char *)&addr->ip)[3],
Joe Perchesc299bd52010-01-11 11:49:51 +010041 port >> 8,
42 port & 0xFF);
43 case NF_CT_FTP_EPRT:
Patrick McHardyd33cbee2012-08-26 19:14:20 +020044 if (nf_ct_l3num(ct) == NFPROTO_IPV4)
45 return snprintf(buffer, buflen, "|1|%pI4|%u|",
46 &addr->ip, port);
47 else
48 return snprintf(buffer, buflen, "|2|%pI6|%u|",
49 &addr->ip6, port);
Joe Perchesc299bd52010-01-11 11:49:51 +010050 case NF_CT_FTP_EPSV:
51 return snprintf(buffer, buflen, "|||%u|", port);
52 }
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080053
Joe Perchesc299bd52010-01-11 11:49:51 +010054 return 0;
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080055}
56
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080057/* So, this packet has hit the connection tracking matching code.
58 Mangle it, and change the expectation to match the new version. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070059static unsigned int nf_nat_ftp(struct sk_buff *skb,
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080060 enum ip_conntrack_info ctinfo,
61 enum nf_ct_ftp_type type,
Patrick McHardy051966c2012-08-26 19:14:04 +020062 unsigned int protoff,
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080063 unsigned int matchoff,
64 unsigned int matchlen,
Patrick McHardy25b86e02007-05-24 16:41:50 -070065 struct nf_conntrack_expect *exp)
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080066{
Patrick McHardyd33cbee2012-08-26 19:14:20 +020067 union nf_inet_addr newaddr;
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080068 u_int16_t port;
69 int dir = CTINFO2DIR(ctinfo);
70 struct nf_conn *ct = exp->master;
Patrick McHardyd33cbee2012-08-26 19:14:20 +020071 char buffer[sizeof("|1||65535|") + INET6_ADDRSTRLEN];
Joe Perchesc299bd52010-01-11 11:49:51 +010072 unsigned int buflen;
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080073
Patrick McHardy0d537782007-07-07 22:39:38 -070074 pr_debug("FTP_NAT: type %i, off %u len %u\n", type, matchoff, matchlen);
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080075
76 /* Connection will come from wherever this packet goes, hence !dir */
Patrick McHardyd33cbee2012-08-26 19:14:20 +020077 newaddr = ct->tuplehash[!dir].tuple.dst.u3;
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080078 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
79 exp->dir = !dir;
80
81 /* When you see the packet, we need to NAT it the same as the
82 * this one. */
83 exp->expectfn = nf_nat_follow_master;
84
85 /* Try to get same port: if not, try to change it. */
86 for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +020087 int ret;
88
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080089 exp->tuple.dst.u.tcp.port = htons(port);
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +020090 ret = nf_ct_expect_related(exp);
91 if (ret == 0)
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080092 break;
Pablo Neira Ayuso5b92b612010-09-22 08:34:12 +020093 else if (ret != -EBUSY) {
94 port = 0;
95 break;
96 }
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080097 }
98
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +010099 if (port == 0) {
100 nf_ct_helper_log(skb, ct, "all ports in use");
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800101 return NF_DROP;
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100102 }
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800103
Patrick McHardyd33cbee2012-08-26 19:14:20 +0200104 buflen = nf_nat_ftp_fmt_cmd(ct, type, buffer, sizeof(buffer),
105 &newaddr, port);
Joe Perchesc299bd52010-01-11 11:49:51 +0100106 if (!buflen)
107 goto out;
108
109 pr_debug("calling nf_nat_mangle_tcp_packet\n");
110
Patrick McHardy051966c2012-08-26 19:14:04 +0200111 if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff,
Joe Perchesc299bd52010-01-11 11:49:51 +0100112 matchlen, buffer, buflen))
113 goto out;
114
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800115 return NF_ACCEPT;
Joe Perchesc299bd52010-01-11 11:49:51 +0100116
117out:
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100118 nf_ct_helper_log(skb, ct, "cannot mangle packet");
Joe Perchesc299bd52010-01-11 11:49:51 +0100119 nf_ct_unexpect_related(exp);
120 return NF_DROP;
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800121}
122
123static void __exit nf_nat_ftp_fini(void)
124{
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000125 RCU_INIT_POINTER(nf_nat_ftp_hook, NULL);
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800126 synchronize_rcu();
127}
128
129static int __init nf_nat_ftp_init(void)
130{
Patrick McHardyd1332e02007-11-05 20:43:30 -0800131 BUG_ON(nf_nat_ftp_hook != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000132 RCU_INIT_POINTER(nf_nat_ftp_hook, nf_nat_ftp);
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800133 return 0;
134}
135
136/* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
Kees Cook24da2c82017-10-17 19:04:42 -0700137static int warn_set(const char *val, const struct kernel_param *kp)
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -0800138{
139 printk(KERN_INFO KBUILD_MODNAME
140 ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
141 return 0;
142}
143module_param_call(ports, warn_set, NULL, NULL, 0);
144
145module_init(nf_nat_ftp_init);
146module_exit(nf_nat_ftp_fini);