projects/linux·blob master · refs

276 lines · 7.5 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Transparent proxy support for Linux/iptables
4 *
5 * Copyright (c) 2006-2010 BalaBit IT Ltd.
6 * Author: Balazs Scheidler, Krisztian Kovacs
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 /* check if there's an ongoing connection on the packet
48 * addresses, this happens if the redirect already happened
49 * and the current packet belongs to an already established
50 * connection */
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 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
61 if (sk && sk->sk_state == TCP_TIME_WAIT)
62 /* reopening a TIME_WAIT connection needs special handling */
63 sk = nf_tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
64 else if (!sk)
65 /* no, there's no established connection, check if
66 * there's a listener on the redirected addr/port */
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 /* NOTE: assign_sock consumes our sk reference */
73 if (sk && nf_tproxy_sk_is_transparent(sk)) {
74 /* This should be in a separate target, but we don't do multiple
75 targets on the same rule yet */
76 skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
77 nf_tproxy_assign_sock(skb, sk);
78 return NF_ACCEPT;
79 }
80
81 return NF_DROP;
82}
83
84static unsigned int
85tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
86{
87 const struct xt_tproxy_target_info *tgi = par->targinfo;
88
89 if (par->fragoff)
90 return NF_DROP;
91
92 return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
93 tgi->mark_mask, tgi->mark_value);
94}
95
96static unsigned int
97tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
98{
99 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
100
101 if (par->fragoff)
102 return NF_DROP;
103
104 return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
105 tgi->mark_mask, tgi->mark_value);
106}
107
108#ifdef XT_TPROXY_HAVE_IPV6
109
110static unsigned int
111tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
112{
113 const struct ipv6hdr *iph = ipv6_hdr(skb);
114 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
115 unsigned short fragoff = 0;
116 struct udphdr _hdr, *hp;
117 struct sock *sk;
118 const struct in6_addr *laddr;
119 __be16 lport;
120 int thoff = 0;
121 int tproto;
122
123 tproto = ipv6_find_hdr(skb, &thoff, -1, &fragoff, NULL);
124 if (tproto < 0 || fragoff)
125 return NF_DROP;
126
127 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
128 if (!hp)
129 return NF_DROP;
130
131 /* check if there's an ongoing connection on the packet
132 * addresses, this happens if the redirect already happened
133 * and the current packet belongs to an already established
134 * connection */
135 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, tproto,
136 &iph->saddr, &iph->daddr,
137 hp->source, hp->dest,
138 xt_in(par), NF_TPROXY_LOOKUP_ESTABLISHED);
139
140 laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
141 lport = tgi->lport ? tgi->lport : hp->dest;
142
143 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
144 if (sk && sk->sk_state == TCP_TIME_WAIT) {
145 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
146 /* reopening a TIME_WAIT connection needs special handling */
147 sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
148 xt_net(par),
149 &tgi->laddr.in6,
150 tgi->lport,
151 sk);
152 }
153 else if (!sk)
154 /* no there's no established connection, check if
155 * there's a listener on the redirected addr/port */
156 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
157 tproto, &iph->saddr, laddr,
158 hp->source, lport,
159 xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
160
161 /* NOTE: assign_sock consumes our sk reference */
162 if (sk && nf_tproxy_sk_is_transparent(sk)) {
163 /* This should be in a separate target, but we don't do multiple
164 targets on the same rule yet */
165 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
166 nf_tproxy_assign_sock(skb, sk);
167 return NF_ACCEPT;
168 }
169
170 return NF_DROP;
171}
172
173static int tproxy_tg6_check(const struct xt_tgchk_param *par)
174{
175 const struct ip6t_ip6 *i = par->entryinfo;
176 int err;
177
178 err = nf_defrag_ipv6_enable(par->net);
179 if (err)
180 return err;
181
182 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
183 !(i->invflags & IP6T_INV_PROTO))
184 return 0;
185
186 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
187 return -EINVAL;
188}
189
190static void tproxy_tg6_destroy(const struct xt_tgdtor_param *par)
191{
192 nf_defrag_ipv6_disable(par->net);
193}
194#endif
195
196static int tproxy_tg4_check(const struct xt_tgchk_param *par)
197{
198 const struct ipt_ip *i = par->entryinfo;
199 int err;
200
201 err = nf_defrag_ipv4_enable(par->net);
202 if (err)
203 return err;
204
205 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
206 && !(i->invflags & IPT_INV_PROTO))
207 return 0;
208
209 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
210 return -EINVAL;
211}
212
213static void tproxy_tg4_destroy(const struct xt_tgdtor_param *par)
214{
215 nf_defrag_ipv4_disable(par->net);
216}
217
218static struct xt_target tproxy_tg_reg[] __read_mostly = {
219 {
220 .name = "TPROXY",
221 .family = NFPROTO_IPV4,
222 .table = "mangle",
223 .target = tproxy_tg4_v0,
224 .revision = 0,
225 .targetsize = sizeof(struct xt_tproxy_target_info),
226 .checkentry = tproxy_tg4_check,
227 .destroy = tproxy_tg4_destroy,
228 .hooks = 1 << NF_INET_PRE_ROUTING,
229 .me = THIS_MODULE,
230 },
231 {
232 .name = "TPROXY",
233 .family = NFPROTO_IPV4,
234 .table = "mangle",
235 .target = tproxy_tg4_v1,
236 .revision = 1,
237 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
238 .checkentry = tproxy_tg4_check,
239 .destroy = tproxy_tg4_destroy,
240 .hooks = 1 << NF_INET_PRE_ROUTING,
241 .me = THIS_MODULE,
242 },
243#ifdef XT_TPROXY_HAVE_IPV6
244 {
245 .name = "TPROXY",
246 .family = NFPROTO_IPV6,
247 .table = "mangle",
248 .target = tproxy_tg6_v1,
249 .revision = 1,
250 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
251 .checkentry = tproxy_tg6_check,
252 .destroy = tproxy_tg6_destroy,
253 .hooks = 1 << NF_INET_PRE_ROUTING,
254 .me = THIS_MODULE,
255 },
256#endif
257
258};
259
260static int __init tproxy_tg_init(void)
261{
262 return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
263}
264
265static void __exit tproxy_tg_exit(void)
266{
267 xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
268}
269
270module_init(tproxy_tg_init);
271module_exit(tproxy_tg_exit);
272MODULE_LICENSE("GPL");
273MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
274MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
275MODULE_ALIAS("ipt_TPROXY");
276MODULE_ALIAS("ip6t_TPROXY");