Linux 4.19.133
[linux/fpc-iii.git] / net / netfilter / xt_TPROXY.c
blobad7420cdc4395c0852bf8ea0a215ebb7cbc0c053
1 /*
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>
15 #include <linux/ip.h>
16 #include <net/checksum.h>
17 #include <net/udp.h>
18 #include <net/tcp.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>
34 #endif
36 #include <net/netfilter/nf_tproxy.h>
37 #include <linux/netfilter/xt_TPROXY.h>
39 static unsigned int
40 tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
41 u_int32_t mark_mask, u_int32_t mark_value)
43 const struct iphdr *iph = ip_hdr(skb);
44 struct udphdr _hdr, *hp;
45 struct sock *sk;
47 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
48 if (hp == NULL)
49 return NF_DROP;
51 /* check if there's an ongoing connection on the packet
52 * addresses, this happens if the redirect already happened
53 * and the current packet belongs to an already established
54 * connection */
55 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
56 iph->saddr, iph->daddr,
57 hp->source, hp->dest,
58 skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
60 laddr = nf_tproxy_laddr4(skb, laddr, iph->daddr);
61 if (!lport)
62 lport = hp->dest;
64 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
65 if (sk && sk->sk_state == TCP_TIME_WAIT)
66 /* reopening a TIME_WAIT connection needs special handling */
67 sk = nf_tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
68 else if (!sk)
69 /* no, there's no established connection, check if
70 * there's a listener on the redirected addr/port */
71 sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
72 iph->saddr, laddr,
73 hp->source, lport,
74 skb->dev, NF_TPROXY_LOOKUP_LISTENER);
76 /* NOTE: assign_sock consumes our sk reference */
77 if (sk && nf_tproxy_sk_is_transparent(sk)) {
78 /* This should be in a separate target, but we don't do multiple
79 targets on the same rule yet */
80 skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
82 pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
83 iph->protocol, &iph->daddr, ntohs(hp->dest),
84 &laddr, ntohs(lport), skb->mark);
86 nf_tproxy_assign_sock(skb, sk);
87 return NF_ACCEPT;
90 pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
91 iph->protocol, &iph->saddr, ntohs(hp->source),
92 &iph->daddr, ntohs(hp->dest), skb->mark);
93 return NF_DROP;
96 static unsigned int
97 tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
99 const struct xt_tproxy_target_info *tgi = par->targinfo;
101 return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
102 tgi->mark_mask, tgi->mark_value);
105 static unsigned int
106 tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
108 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
110 return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
111 tgi->mark_mask, tgi->mark_value);
114 #ifdef XT_TPROXY_HAVE_IPV6
116 static unsigned int
117 tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
119 const struct ipv6hdr *iph = ipv6_hdr(skb);
120 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
121 struct udphdr _hdr, *hp;
122 struct sock *sk;
123 const struct in6_addr *laddr;
124 __be16 lport;
125 int thoff = 0;
126 int tproto;
128 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
129 if (tproto < 0) {
130 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
131 return NF_DROP;
134 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
135 if (hp == NULL) {
136 pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
137 return NF_DROP;
140 /* check if there's an ongoing connection on the packet
141 * addresses, this happens if the redirect already happened
142 * and the current packet belongs to an already established
143 * connection */
144 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, tproto,
145 &iph->saddr, &iph->daddr,
146 hp->source, hp->dest,
147 xt_in(par), NF_TPROXY_LOOKUP_ESTABLISHED);
149 laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
150 lport = tgi->lport ? tgi->lport : hp->dest;
152 /* UDP has no TCP_TIME_WAIT state, so we never enter here */
153 if (sk && sk->sk_state == TCP_TIME_WAIT) {
154 const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
155 /* reopening a TIME_WAIT connection needs special handling */
156 sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
157 xt_net(par),
158 &tgi->laddr.in6,
159 tgi->lport,
160 sk);
162 else if (!sk)
163 /* no there's no established connection, check if
164 * there's a listener on the redirected addr/port */
165 sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
166 tproto, &iph->saddr, laddr,
167 hp->source, lport,
168 xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
170 /* NOTE: assign_sock consumes our sk reference */
171 if (sk && nf_tproxy_sk_is_transparent(sk)) {
172 /* This should be in a separate target, but we don't do multiple
173 targets on the same rule yet */
174 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
176 pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
177 tproto, &iph->saddr, ntohs(hp->source),
178 laddr, ntohs(lport), skb->mark);
180 nf_tproxy_assign_sock(skb, sk);
181 return NF_ACCEPT;
184 pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
185 tproto, &iph->saddr, ntohs(hp->source),
186 &iph->daddr, ntohs(hp->dest), skb->mark);
188 return NF_DROP;
191 static int tproxy_tg6_check(const struct xt_tgchk_param *par)
193 const struct ip6t_ip6 *i = par->entryinfo;
194 int err;
196 err = nf_defrag_ipv6_enable(par->net);
197 if (err)
198 return err;
200 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
201 !(i->invflags & IP6T_INV_PROTO))
202 return 0;
204 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
205 return -EINVAL;
207 #endif
209 static int tproxy_tg4_check(const struct xt_tgchk_param *par)
211 const struct ipt_ip *i = par->entryinfo;
212 int err;
214 err = nf_defrag_ipv4_enable(par->net);
215 if (err)
216 return err;
218 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
219 && !(i->invflags & IPT_INV_PROTO))
220 return 0;
222 pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
223 return -EINVAL;
226 static struct xt_target tproxy_tg_reg[] __read_mostly = {
228 .name = "TPROXY",
229 .family = NFPROTO_IPV4,
230 .table = "mangle",
231 .target = tproxy_tg4_v0,
232 .revision = 0,
233 .targetsize = sizeof(struct xt_tproxy_target_info),
234 .checkentry = tproxy_tg4_check,
235 .hooks = 1 << NF_INET_PRE_ROUTING,
236 .me = THIS_MODULE,
239 .name = "TPROXY",
240 .family = NFPROTO_IPV4,
241 .table = "mangle",
242 .target = tproxy_tg4_v1,
243 .revision = 1,
244 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
245 .checkentry = tproxy_tg4_check,
246 .hooks = 1 << NF_INET_PRE_ROUTING,
247 .me = THIS_MODULE,
249 #ifdef XT_TPROXY_HAVE_IPV6
251 .name = "TPROXY",
252 .family = NFPROTO_IPV6,
253 .table = "mangle",
254 .target = tproxy_tg6_v1,
255 .revision = 1,
256 .targetsize = sizeof(struct xt_tproxy_target_info_v1),
257 .checkentry = tproxy_tg6_check,
258 .hooks = 1 << NF_INET_PRE_ROUTING,
259 .me = THIS_MODULE,
261 #endif
265 static int __init tproxy_tg_init(void)
267 return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
270 static void __exit tproxy_tg_exit(void)
272 xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
275 module_init(tproxy_tg_init);
276 module_exit(tproxy_tg_exit);
277 MODULE_LICENSE("GPL");
278 MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
279 MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
280 MODULE_ALIAS("ipt_TPROXY");
281 MODULE_ALIAS("ip6t_TPROXY");