1 // SPDX-License-Identifier: GPL-2.0-only
3 * Transparent proxy support for Linux/iptables
5 * Copyright (c) 2006-2010 BalaBit IT Ltd.
6 * Author: Balazs Scheidler, Krisztian Kovacs
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
12 #include <net/checksum.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>
21 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
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>
32 #include <net/netfilter/nf_tproxy.h>
33 #include <linux/netfilter/xt_TPROXY.h>
36 tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
37 u_int32_t mark_mask, u_int32_t mark_value)
39 const struct iphdr *iph = ip_hdr(skb);
40 struct udphdr _hdr, *hp;
43 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
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
51 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
52 iph->saddr, iph->daddr,
54 skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
56 laddr = nf_tproxy_laddr4(skb, laddr, iph->daddr);
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);
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,
70 skb->dev, NF_TPROXY_LOOKUP_LISTENER);
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;
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);
82 nf_tproxy_assign_sock(skb, sk);
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);
93 tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
95 const struct xt_tproxy_target_info *tgi = par->targinfo;
97 return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
98 tgi->mark_mask, tgi->mark_value);
102 tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
104 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
106 return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
107 tgi->mark_mask, tgi->mark_value);
110 #ifdef XT_TPROXY_HAVE_IPV6
113 tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
115 const struct ipv6hdr *iph = ipv6_hdr(skb);
116 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
117 struct udphdr _hdr, *hp;
119 const struct in6_addr *laddr;
124 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
126 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
130 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
132 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
136 /* check if there's an ongoing connection on the packet
137 * addresses, this happens if the redirect already happened
138 * and the current packet belongs to an already established
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);
145 laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
146 lport = tgi->lport ? tgi->lport : hp->dest;
148 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
149 if (sk && sk->sk_state == TCP_TIME_WAIT) {
150 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
151 /* reopening a TIME_WAIT connection needs special handling */
152 sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
159 /* no there's no established connection, check if
160 * there's a listener on the redirected addr/port */
161 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
162 tproto, &iph->saddr, laddr,
164 xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
166 /* NOTE: assign_sock consumes our sk reference */
167 if (sk && nf_tproxy_sk_is_transparent(sk)) {
168 /* This should be in a separate target, but we don't do multiple
169 targets on the same rule yet */
170 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
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);
176 nf_tproxy_assign_sock(skb, sk);
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);
187 static int tproxy_tg6_check(const struct xt_tgchk_param *par)
189 const struct ip6t_ip6 *i = par->entryinfo;
192 err = nf_defrag_ipv6_enable(par->net);
196 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
197 !(i->invflags & IP6T_INV_PROTO))
200 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
204 static void tproxy_tg6_destroy(const struct xt_tgdtor_param *par)
206 nf_defrag_ipv6_disable(par->net);
210 static int tproxy_tg4_check(const struct xt_tgchk_param *par)
212 const struct ipt_ip *i = par->entryinfo;
215 err = nf_defrag_ipv4_enable(par->net);
219 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
220 && !(i->invflags & IPT_INV_PROTO))
223 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
227 static void tproxy_tg4_destroy(const struct xt_tgdtor_param *par)
229 nf_defrag_ipv4_disable(par->net);
232 static struct xt_target tproxy_tg_reg[] __read_mostly = {
235 .family = NFPROTO_IPV4,
237 .target = tproxy_tg4_v0,
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,
247 .family = NFPROTO_IPV4,
249 .target = tproxy_tg4_v1,
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,
257 #ifdef XT_TPROXY_HAVE_IPV6
260 .family = NFPROTO_IPV6,
262 .target = tproxy_tg6_v1,
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,
274 static int __init tproxy_tg_init(void)
276 return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
279 static void __exit tproxy_tg_exit(void)
281 xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
284 module_init(tproxy_tg_init);
285 module_exit(tproxy_tg_exit);
286 MODULE_LICENSE("GPL");
287 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
288 MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
289 MODULE_ALIAS("ipt_TPROXY");
290 MODULE_ALIAS("ip6t_TPROXY");