1
2
3
4
5
6
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/ip.h>
12#include <net/checksum.h>
13#include <net/udp.h>
14#include <net/tcp.h>
15#include <net/inet_sock.h>
16#include <net/inet_hashtables.h>
17#include <linux/inetdevice.h>
18#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter_ipv4/ip_tables.h>
20
21#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
22
23#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
24#define XT_TPROXY_HAVE_IPV6 1
25#include <net/if_inet6.h>
26#include <net/addrconf.h>
27#include <net/inet6_hashtables.h>
28#include <linux/netfilter_ipv6/ip6_tables.h>
29#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
30#endif
31
32#include <net/netfilter/nf_tproxy.h>
33#include <linux/netfilter/xt_TPROXY.h>
34
35static unsigned int
36tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
37 u_int32_t mark_mask, u_int32_t mark_value)
38{
39 const struct iphdr *iph = ip_hdr(skb);
40 struct udphdr _hdr, *hp;
41 struct sock *sk;
42
43 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
44 if (hp == NULL)
45 return NF_DROP;
46
47
48
49
50
51 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
52 iph->saddr, iph->daddr,
53 hp->source, hp->dest,
54 skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
55
56 laddr = nf_tproxy_laddr4(skb, laddr, iph->daddr);
57 if (!lport)
58 lport = hp->dest;
59
60
61 if (sk && sk->sk_state == TCP_TIME_WAIT)
62
63 sk = nf_tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
64 else if (!sk)
65
66
67 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
68 iph->saddr, laddr,
69 hp->source, lport,
70 skb->dev, NF_TPROXY_LOOKUP_LISTENER);
71
72
73 if (sk && nf_tproxy_sk_is_transparent(sk)) {
74
75
76 skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
77
78 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
79 iph->protocol, &iph->daddr, ntohs(hp->dest),
80 &laddr, ntohs(lport), skb->mark);
81
82 nf_tproxy_assign_sock(skb, sk);
83 return NF_ACCEPT;
84 }
85
86 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
87 iph->protocol, &iph->saddr, ntohs(hp->source),
88 &iph->daddr, ntohs(hp->dest), skb->mark);
89 return NF_DROP;
90}
91
92static unsigned int
93tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
94{
95 const struct xt_tproxy_target_info *tgi = par->targinfo;
96
97 return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
98 tgi->mark_mask, tgi->mark_value);
99}
100
101static unsigned int
102tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
103{
104 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
105
106 return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
107 tgi->mark_mask, tgi->mark_value);
108}
109
110#ifdef XT_TPROXY_HAVE_IPV6
111
112static unsigned int
113tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
114{
115 const struct ipv6hdr *iph = ipv6_hdr(skb);
116 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
117 struct udphdr _hdr, *hp;
118 struct sock *sk;
119 const struct in6_addr *laddr;
120 __be16 lport;
121 int thoff = 0;
122 int tproto;
123
124 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
125 if (tproto < 0) {
126 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
127 return NF_DROP;
128 }
129
130 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
131 if (hp == NULL) {
132 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
133 return NF_DROP;
134 }
135
136
137
138
139
140 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, tproto,
141 &iph->saddr, &iph->daddr,
142 hp->source, hp->dest,
143 xt_in(par), NF_TPROXY_LOOKUP_ESTABLISHED);
144
145 laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
146 lport = tgi->lport ? tgi->lport : hp->dest;
147
148
149 if (sk && sk->sk_state == TCP_TIME_WAIT) {
150 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
151
152 sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
153 xt_net(par),
154 &tgi->laddr.in6,
155 tgi->lport,
156 sk);
157 }
158 else if (!sk)
159
160
161 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
162 tproto, &iph->saddr, laddr,
163 hp->source, lport,
164 xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
165
166
167 if (sk && nf_tproxy_sk_is_transparent(sk)) {
168
169
170 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
171
172 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
173 tproto, &iph->saddr, ntohs(hp->source),
174 laddr, ntohs(lport), skb->mark);
175
176 nf_tproxy_assign_sock(skb, sk);
177 return NF_ACCEPT;
178 }
179
180 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
181 tproto, &iph->saddr, ntohs(hp->source),
182 &iph->daddr, ntohs(hp->dest), skb->mark);
183
184 return NF_DROP;
185}
186
187static int tproxy_tg6_check(const struct xt_tgchk_param *par)
188{
189 const struct ip6t_ip6 *i = par->entryinfo;
190 int err;
191
192 err = nf_defrag_ipv6_enable(par->net);
193 if (err)
194 return err;
195
196 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
197 !(i->invflags & IP6T_INV_PROTO))
198 return 0;
199
200 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
201 return -EINVAL;
202}
203
204static void tproxy_tg6_destroy(const struct xt_tgdtor_param *par)
205{
206 nf_defrag_ipv6_disable(par->net);
207}
208#endif
209
210static int tproxy_tg4_check(const struct xt_tgchk_param *par)
211{
212 const struct ipt_ip *i = par->entryinfo;
213 int err;
214
215 err = nf_defrag_ipv4_enable(par->net);
216 if (err)
217 return err;
218
219 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
220 && !(i->invflags & IPT_INV_PROTO))
221 return 0;
222
223 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
224 return -EINVAL;
225}
226
227static void tproxy_tg4_destroy(const struct xt_tgdtor_param *par)
228{
229 nf_defrag_ipv4_disable(par->net);
230}
231
232static struct xt_target tproxy_tg_reg[] __read_mostly = {
233 {
234 .name = "TPROXY",
235 .family = NFPROTO_IPV4,
236 .table = "mangle",
237 .target = tproxy_tg4_v0,
238 .revision = 0,
239 .targetsize = sizeof(struct xt_tproxy_target_info),
240 .checkentry = tproxy_tg4_check,
241 .destroy = tproxy_tg4_destroy,
242 .hooks = 1 << NF_INET_PRE_ROUTING,
243 .me = THIS_MODULE,
244 },
245 {
246 .name = "TPROXY",
247 .family = NFPROTO_IPV4,
248 .table = "mangle",
249 .target = tproxy_tg4_v1,
250 .revision = 1,
251 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
252 .checkentry = tproxy_tg4_check,
253 .destroy = tproxy_tg4_destroy,
254 .hooks = 1 << NF_INET_PRE_ROUTING,
255 .me = THIS_MODULE,
256 },
257#ifdef XT_TPROXY_HAVE_IPV6
258 {
259 .name = "TPROXY",
260 .family = NFPROTO_IPV6,
261 .table = "mangle",
262 .target = tproxy_tg6_v1,
263 .revision = 1,
264 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
265 .checkentry = tproxy_tg6_check,
266 .destroy = tproxy_tg6_destroy,
267 .hooks = 1 << NF_INET_PRE_ROUTING,
268 .me = THIS_MODULE,
269 },
270#endif
271
272};
273
274static int __init tproxy_tg_init(void)
275{
276 return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
277}
278
279static void __exit tproxy_tg_exit(void)
280{
281 xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
282}
283
284module_init(tproxy_tg_init);
285module_exit(tproxy_tg_exit);
286MODULE_LICENSE("GPL");
287MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
288MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
289MODULE_ALIAS("ipt_TPROXY");
290MODULE_ALIAS("ip6t_TPROXY");
291