ASoC: SOF: Drop superfluous snd_pcm_sgbuf_ops_page
[linux-2.6-block.git] / net / netfilter / nf_nat_ftp.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
55a73324
JK
2/* FTP extension for TCP NAT alteration. */
3
4/* (C) 1999-2001 Paul `Rusty' Russell
5 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
55a73324
JK
6 */
7
5191d70f
AS
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
55a73324
JK
10#include <linux/module.h>
11#include <linux/moduleparam.h>
d33cbeeb 12#include <linux/inet.h>
55a73324
JK
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>
55a73324
JK
17#include <net/netfilter/nf_conntrack_helper.h>
18#include <net/netfilter/nf_conntrack_expect.h>
19#include <linux/netfilter/nf_conntrack_ftp.h>
20
53b11308
FL
21#define NAT_HELPER_NAME "ftp"
22
55a73324
JK
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
25MODULE_DESCRIPTION("ftp NAT helper");
53b11308 26MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME);
55a73324 27
55a73324
JK
28/* FIXME: Time out? --RR */
29
53b11308
FL
30static struct nf_conntrack_nat_helper nat_helper_ftp =
31 NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME);
32
d33cbeeb 33static int nf_nat_ftp_fmt_cmd(struct nf_conn *ct, enum nf_ct_ftp_type type,
c299bd53 34 char *buffer, size_t buflen,
d33cbeeb 35 union nf_inet_addr *addr, u16 port)
55a73324 36{
c299bd53
JP
37 switch (type) {
38 case NF_CT_FTP_PORT:
39 case NF_CT_FTP_PASV:
40 return snprintf(buffer, buflen, "%u,%u,%u,%u,%u,%u",
d33cbeeb
PM
41 ((unsigned char *)&addr->ip)[0],
42 ((unsigned char *)&addr->ip)[1],
43 ((unsigned char *)&addr->ip)[2],
44 ((unsigned char *)&addr->ip)[3],
c299bd53
JP
45 port >> 8,
46 port & 0xFF);
47 case NF_CT_FTP_EPRT:
d33cbeeb
PM
48 if (nf_ct_l3num(ct) == NFPROTO_IPV4)
49 return snprintf(buffer, buflen, "|1|%pI4|%u|",
50 &addr->ip, port);
51 else
52 return snprintf(buffer, buflen, "|2|%pI6|%u|",
53 &addr->ip6, port);
c299bd53
JP
54 case NF_CT_FTP_EPSV:
55 return snprintf(buffer, buflen, "|||%u|", port);
56 }
55a73324 57
c299bd53 58 return 0;
55a73324
JK
59}
60
55a73324
JK
61/* So, this packet has hit the connection tracking matching code.
62 Mangle it, and change the expectation to match the new version. */
3db05fea 63static unsigned int nf_nat_ftp(struct sk_buff *skb,
55a73324
JK
64 enum ip_conntrack_info ctinfo,
65 enum nf_ct_ftp_type type,
051966c0 66 unsigned int protoff,
55a73324
JK
67 unsigned int matchoff,
68 unsigned int matchlen,
25b86e05 69 struct nf_conntrack_expect *exp)
55a73324 70{
d33cbeeb 71 union nf_inet_addr newaddr;
55a73324
JK
72 u_int16_t port;
73 int dir = CTINFO2DIR(ctinfo);
74 struct nf_conn *ct = exp->master;
d33cbeeb 75 char buffer[sizeof("|1||65535|") + INET6_ADDRSTRLEN];
c299bd53 76 unsigned int buflen;
55a73324 77
5191d70f 78 pr_debug("type %i, off %u len %u\n", type, matchoff, matchlen);
55a73324
JK
79
80 /* Connection will come from wherever this packet goes, hence !dir */
d33cbeeb 81 newaddr = ct->tuplehash[!dir].tuple.dst.u3;
55a73324
JK
82 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
83 exp->dir = !dir;
84
85 /* When you see the packet, we need to NAT it the same as the
86 * this one. */
87 exp->expectfn = nf_nat_follow_master;
88
89 /* Try to get same port: if not, try to change it. */
90 for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
5b92b61f
PNA
91 int ret;
92
55a73324 93 exp->tuple.dst.u.tcp.port = htons(port);
3c00fb0b 94 ret = nf_ct_expect_related(exp, 0);
5b92b61f
PNA
95 if (ret == 0)
96 break;
97 else if (ret != -EBUSY) {
98 port = 0;
55a73324 99 break;
5b92b61f 100 }
55a73324
JK
101 }
102
b20ab9cc
PNA
103 if (port == 0) {
104 nf_ct_helper_log(skb, ct, "all ports in use");
55a73324 105 return NF_DROP;
b20ab9cc 106 }
55a73324 107
d33cbeeb
PM
108 buflen = nf_nat_ftp_fmt_cmd(ct, type, buffer, sizeof(buffer),
109 &newaddr, port);
c299bd53
JP
110 if (!buflen)
111 goto out;
112
113 pr_debug("calling nf_nat_mangle_tcp_packet\n");
114
051966c0 115 if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff,
c299bd53
JP
116 matchlen, buffer, buflen))
117 goto out;
118
55a73324 119 return NF_ACCEPT;
c299bd53
JP
120
121out:
b20ab9cc 122 nf_ct_helper_log(skb, ct, "cannot mangle packet");
c299bd53
JP
123 nf_ct_unexpect_related(exp);
124 return NF_DROP;
55a73324
JK
125}
126
127static void __exit nf_nat_ftp_fini(void)
128{
53b11308 129 nf_nat_helper_unregister(&nat_helper_ftp);
a9b3cd7f 130 RCU_INIT_POINTER(nf_nat_ftp_hook, NULL);
55a73324
JK
131 synchronize_rcu();
132}
133
134static int __init nf_nat_ftp_init(void)
135{
d1332e0a 136 BUG_ON(nf_nat_ftp_hook != NULL);
53b11308 137 nf_nat_helper_register(&nat_helper_ftp);
a9b3cd7f 138 RCU_INIT_POINTER(nf_nat_ftp_hook, nf_nat_ftp);
55a73324
JK
139 return 0;
140}
141
142/* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
e4dca7b7 143static int warn_set(const char *val, const struct kernel_param *kp)
55a73324 144{
5191d70f 145 pr_info("kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
55a73324
JK
146 return 0;
147}
148module_param_call(ports, warn_set, NULL, NULL, 0);
149
150module_init(nf_nat_ftp_init);
151module_exit(nf_nat_ftp_fini);