0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/inet.h>
0013 #include <linux/tcp.h>
0014 #include <linux/netfilter_ipv4.h>
0015 #include <net/netfilter/nf_nat.h>
0016 #include <net/netfilter/nf_nat_helper.h>
0017 #include <net/netfilter/nf_conntrack_helper.h>
0018 #include <net/netfilter/nf_conntrack_expect.h>
0019 #include <linux/netfilter/nf_conntrack_ftp.h>
0020
0021 #define NAT_HELPER_NAME "ftp"
0022
0023 MODULE_LICENSE("GPL");
0024 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
0025 MODULE_DESCRIPTION("ftp NAT helper");
0026 MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME);
0027
0028
0029
0030 static struct nf_conntrack_nat_helper nat_helper_ftp =
0031 NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME);
0032
0033 static int nf_nat_ftp_fmt_cmd(struct nf_conn *ct, enum nf_ct_ftp_type type,
0034 char *buffer, size_t buflen,
0035 union nf_inet_addr *addr, u16 port)
0036 {
0037 switch (type) {
0038 case NF_CT_FTP_PORT:
0039 case NF_CT_FTP_PASV:
0040 return snprintf(buffer, buflen, "%u,%u,%u,%u,%u,%u",
0041 ((unsigned char *)&addr->ip)[0],
0042 ((unsigned char *)&addr->ip)[1],
0043 ((unsigned char *)&addr->ip)[2],
0044 ((unsigned char *)&addr->ip)[3],
0045 port >> 8,
0046 port & 0xFF);
0047 case NF_CT_FTP_EPRT:
0048 if (nf_ct_l3num(ct) == NFPROTO_IPV4)
0049 return snprintf(buffer, buflen, "|1|%pI4|%u|",
0050 &addr->ip, port);
0051 else
0052 return snprintf(buffer, buflen, "|2|%pI6|%u|",
0053 &addr->ip6, port);
0054 case NF_CT_FTP_EPSV:
0055 return snprintf(buffer, buflen, "|||%u|", port);
0056 }
0057
0058 return 0;
0059 }
0060
0061
0062
0063 static unsigned int nf_nat_ftp(struct sk_buff *skb,
0064 enum ip_conntrack_info ctinfo,
0065 enum nf_ct_ftp_type type,
0066 unsigned int protoff,
0067 unsigned int matchoff,
0068 unsigned int matchlen,
0069 struct nf_conntrack_expect *exp)
0070 {
0071 union nf_inet_addr newaddr;
0072 u_int16_t port;
0073 int dir = CTINFO2DIR(ctinfo);
0074 struct nf_conn *ct = exp->master;
0075 char buffer[sizeof("|1||65535|") + INET6_ADDRSTRLEN];
0076 unsigned int buflen;
0077
0078 pr_debug("type %i, off %u len %u\n", type, matchoff, matchlen);
0079
0080
0081 newaddr = ct->tuplehash[!dir].tuple.dst.u3;
0082 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
0083 exp->dir = !dir;
0084
0085
0086
0087 exp->expectfn = nf_nat_follow_master;
0088
0089
0090 for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
0091 int ret;
0092
0093 exp->tuple.dst.u.tcp.port = htons(port);
0094 ret = nf_ct_expect_related(exp, 0);
0095 if (ret == 0)
0096 break;
0097 else if (ret != -EBUSY) {
0098 port = 0;
0099 break;
0100 }
0101 }
0102
0103 if (port == 0) {
0104 nf_ct_helper_log(skb, ct, "all ports in use");
0105 return NF_DROP;
0106 }
0107
0108 buflen = nf_nat_ftp_fmt_cmd(ct, type, buffer, sizeof(buffer),
0109 &newaddr, port);
0110 if (!buflen)
0111 goto out;
0112
0113 pr_debug("calling nf_nat_mangle_tcp_packet\n");
0114
0115 if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff,
0116 matchlen, buffer, buflen))
0117 goto out;
0118
0119 return NF_ACCEPT;
0120
0121 out:
0122 nf_ct_helper_log(skb, ct, "cannot mangle packet");
0123 nf_ct_unexpect_related(exp);
0124 return NF_DROP;
0125 }
0126
0127 static void __exit nf_nat_ftp_fini(void)
0128 {
0129 nf_nat_helper_unregister(&nat_helper_ftp);
0130 RCU_INIT_POINTER(nf_nat_ftp_hook, NULL);
0131 synchronize_rcu();
0132 }
0133
0134 static int __init nf_nat_ftp_init(void)
0135 {
0136 BUG_ON(nf_nat_ftp_hook != NULL);
0137 nf_nat_helper_register(&nat_helper_ftp);
0138 RCU_INIT_POINTER(nf_nat_ftp_hook, nf_nat_ftp);
0139 return 0;
0140 }
0141
0142
0143 static int warn_set(const char *val, const struct kernel_param *kp)
0144 {
0145 pr_info("kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
0146 return 0;
0147 }
0148 module_param_call(ports, warn_set, NULL, NULL, 0);
0149
0150 module_init(nf_nat_ftp_init);
0151 module_exit(nf_nat_ftp_fini);