2 * Transparent proxy support for Linux/iptables
4 * Copyright (c) 2006-2010 BalaBit IT Ltd.
5 * Author: Balazs Scheidler, Krisztian Kovacs
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
16 #include <net/checksum.h>
19 #include <net/inet_sock.h>
20 #include <net/inet_hashtables.h>
21 #include <linux/inetdevice.h>
22 #include <linux/netfilter/x_tables.h>
23 #include <linux/netfilter_ipv4/ip_tables.h>
25 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
27 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
28 #define XT_TPROXY_HAVE_IPV6 1
29 #include <net/if_inet6.h>
30 #include <net/addrconf.h>
31 #include <net/inet6_hashtables.h>
32 #include <linux/netfilter_ipv6/ip6_tables.h>
33 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
36 #include <net/netfilter/nf_tproxy.h>
37 #include <linux/netfilter/xt_TPROXY.h>
39 /* assign a socket to the skb -- consumes sk */
41 nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk)
45 skb->destructor = sock_edemux;
49 tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
50 u_int32_t mark_mask, u_int32_t mark_value)
52 const struct iphdr *iph = ip_hdr(skb);
53 struct udphdr _hdr, *hp;
56 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
60 /* check if there's an ongoing connection on the packet
61 * addresses, this happens if the redirect already happened
62 * and the current packet belongs to an already established
64 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
65 iph->saddr, iph->daddr,
67 skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
69 laddr = nf_tproxy_laddr4(skb, laddr, iph->daddr);
73 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
74 if (sk && sk->sk_state == TCP_TIME_WAIT)
75 /* reopening a TIME_WAIT connection needs special handling */
76 sk = nf_tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
78 /* no, there's no established connection, check if
79 * there's a listener on the redirected addr/port */
80 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
83 skb->dev, NF_TPROXY_LOOKUP_LISTENER);
85 /* NOTE: assign_sock consumes our sk reference */
86 if (sk && nf_tproxy_sk_is_transparent(sk)) {
87 /* This should be in a separate target, but we don't do multiple
88 targets on the same rule yet */
89 skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
91 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
92 iph->protocol, &iph->daddr, ntohs(hp->dest),
93 &laddr, ntohs(lport), skb->mark);
95 nf_tproxy_assign_sock(skb, sk);
99 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
100 iph->protocol, &iph->saddr, ntohs(hp->source),
101 &iph->daddr, ntohs(hp->dest), skb->mark);
106 tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
108 const struct xt_tproxy_target_info *tgi = par->targinfo;
110 return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
111 tgi->mark_mask, tgi->mark_value);
115 tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
117 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
119 return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
120 tgi->mark_mask, tgi->mark_value);
123 #ifdef XT_TPROXY_HAVE_IPV6
126 tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
128 const struct ipv6hdr *iph = ipv6_hdr(skb);
129 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
130 struct udphdr _hdr, *hp;
132 const struct in6_addr *laddr;
137 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
139 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
143 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
145 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
149 /* check if there's an ongoing connection on the packet
150 * addresses, this happens if the redirect already happened
151 * and the current packet belongs to an already established
153 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, tproto,
154 &iph->saddr, &iph->daddr,
155 hp->source, hp->dest,
156 xt_in(par), NF_TPROXY_LOOKUP_ESTABLISHED);
158 laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
159 lport = tgi->lport ? tgi->lport : hp->dest;
161 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
162 if (sk && sk->sk_state == TCP_TIME_WAIT) {
163 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
164 /* reopening a TIME_WAIT connection needs special handling */
165 sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
172 /* no there's no established connection, check if
173 * there's a listener on the redirected addr/port */
174 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
175 tproto, &iph->saddr, laddr,
177 xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
179 /* NOTE: assign_sock consumes our sk reference */
180 if (sk && nf_tproxy_sk_is_transparent(sk)) {
181 /* This should be in a separate target, but we don't do multiple
182 targets on the same rule yet */
183 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
185 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
186 tproto, &iph->saddr, ntohs(hp->source),
187 laddr, ntohs(lport), skb->mark);
189 nf_tproxy_assign_sock(skb, sk);
193 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
194 tproto, &iph->saddr, ntohs(hp->source),
195 &iph->daddr, ntohs(hp->dest), skb->mark);
200 static int tproxy_tg6_check(const struct xt_tgchk_param *par)
202 const struct ip6t_ip6 *i = par->entryinfo;
205 err = nf_defrag_ipv6_enable(par->net);
209 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
210 !(i->invflags & IP6T_INV_PROTO))
213 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
218 static int tproxy_tg4_check(const struct xt_tgchk_param *par)
220 const struct ipt_ip *i = par->entryinfo;
223 err = nf_defrag_ipv4_enable(par->net);
227 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
228 && !(i->invflags & IPT_INV_PROTO))
231 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
235 static struct xt_target tproxy_tg_reg[] __read_mostly = {
238 .family = NFPROTO_IPV4,
240 .target = tproxy_tg4_v0,
242 .targetsize = sizeof(struct xt_tproxy_target_info),
243 .checkentry = tproxy_tg4_check,
244 .hooks = 1 << NF_INET_PRE_ROUTING,
249 .family = NFPROTO_IPV4,
251 .target = tproxy_tg4_v1,
253 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
254 .checkentry = tproxy_tg4_check,
255 .hooks = 1 << NF_INET_PRE_ROUTING,
258 #ifdef XT_TPROXY_HAVE_IPV6
261 .family = NFPROTO_IPV6,
263 .target = tproxy_tg6_v1,
265 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
266 .checkentry = tproxy_tg6_check,
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");