2 * Transparent proxy support for Linux/iptables
4 * Copyright (C) 2007-2008 BalaBit IT Ltd.
5 * Author: 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/netfilter/x_tables.h>
16 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <net/inet_sock.h>
22 #include <net/netfilter/nf_tproxy_core.h>
23 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
25 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
26 #define XT_SOCKET_HAVE_IPV6 1
27 #include <linux/netfilter_ipv6/ip6_tables.h>
28 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
31 #include <linux/netfilter/xt_socket.h>
33 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
34 #define XT_SOCKET_HAVE_CONNTRACK 1
35 #include <net/netfilter/nf_conntrack.h>
39 extract_icmp4_fields(const struct sk_buff
*skb
,
46 unsigned int outside_hdrlen
= ip_hdrlen(skb
);
47 struct iphdr
*inside_iph
, _inside_iph
;
48 struct icmphdr
*icmph
, _icmph
;
49 __be16
*ports
, _ports
[2];
51 icmph
= skb_header_pointer(skb
, outside_hdrlen
,
52 sizeof(_icmph
), &_icmph
);
56 switch (icmph
->type
) {
57 case ICMP_DEST_UNREACH
:
58 case ICMP_SOURCE_QUENCH
:
60 case ICMP_TIME_EXCEEDED
:
61 case ICMP_PARAMETERPROB
:
67 inside_iph
= skb_header_pointer(skb
, outside_hdrlen
+
68 sizeof(struct icmphdr
),
69 sizeof(_inside_iph
), &_inside_iph
);
70 if (inside_iph
== NULL
)
73 if (inside_iph
->protocol
!= IPPROTO_TCP
&&
74 inside_iph
->protocol
!= IPPROTO_UDP
)
77 ports
= skb_header_pointer(skb
, outside_hdrlen
+
78 sizeof(struct icmphdr
) +
79 (inside_iph
->ihl
<< 2),
80 sizeof(_ports
), &_ports
);
84 /* the inside IP packet is the one quoted from our side, thus
85 * its saddr is the local address */
86 *protocol
= inside_iph
->protocol
;
87 *laddr
= inside_iph
->saddr
;
89 *raddr
= inside_iph
->daddr
;
96 socket_match(const struct sk_buff
*skb
, struct xt_action_param
*par
,
97 const struct xt_socket_mtinfo1
*info
)
99 const struct iphdr
*iph
= ip_hdr(skb
);
100 struct udphdr _hdr
, *hp
= NULL
;
105 #ifdef XT_SOCKET_HAVE_CONNTRACK
106 struct nf_conn
const *ct
;
107 enum ip_conntrack_info ctinfo
;
110 if (iph
->protocol
== IPPROTO_UDP
|| iph
->protocol
== IPPROTO_TCP
) {
111 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
),
112 sizeof(_hdr
), &_hdr
);
116 protocol
= iph
->protocol
;
122 } else if (iph
->protocol
== IPPROTO_ICMP
) {
123 if (extract_icmp4_fields(skb
, &protocol
, &saddr
, &daddr
,
130 #ifdef XT_SOCKET_HAVE_CONNTRACK
131 /* Do the lookup with the original socket address in case this is a
132 * reply packet of an established SNAT-ted connection. */
134 ct
= nf_ct_get(skb
, &ctinfo
);
135 if (ct
&& !nf_ct_is_untracked(ct
) &&
136 ((iph
->protocol
!= IPPROTO_ICMP
&&
137 ctinfo
== IP_CT_IS_REPLY
+ IP_CT_ESTABLISHED
) ||
138 (iph
->protocol
== IPPROTO_ICMP
&&
139 ctinfo
== IP_CT_IS_REPLY
+ IP_CT_RELATED
)) &&
140 (ct
->status
& IPS_SRC_NAT_DONE
)) {
142 daddr
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u3
.ip
;
143 dport
= (iph
->protocol
== IPPROTO_TCP
) ?
144 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u
.tcp
.port
:
145 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u
.udp
.port
;
149 sk
= nf_tproxy_get_sock_v4(dev_net(skb
->dev
), protocol
,
150 saddr
, daddr
, sport
, dport
, par
->in
, NFT_LOOKUP_ANY
);
153 bool transparent
= true;
155 /* Ignore sockets listening on INADDR_ANY */
156 wildcard
= (sk
->sk_state
!= TCP_TIME_WAIT
&&
157 inet_sk(sk
)->inet_rcv_saddr
== 0);
159 /* Ignore non-transparent sockets,
160 if XT_SOCKET_TRANSPARENT is used */
161 if (info
&& info
->flags
& XT_SOCKET_TRANSPARENT
)
162 transparent
= ((sk
->sk_state
!= TCP_TIME_WAIT
&&
163 inet_sk(sk
)->transparent
) ||
164 (sk
->sk_state
== TCP_TIME_WAIT
&&
165 inet_twsk(sk
)->tw_transparent
));
167 nf_tproxy_put_sock(sk
);
169 if (wildcard
|| !transparent
)
173 pr_debug("proto %hhu %pI4:%hu -> %pI4:%hu (orig %pI4:%hu) sock %p\n",
174 protocol
, &saddr
, ntohs(sport
),
175 &daddr
, ntohs(dport
),
176 &iph
->daddr
, hp
? ntohs(hp
->dest
) : 0, sk
);
182 socket_mt4_v0(const struct sk_buff
*skb
, struct xt_action_param
*par
)
184 return socket_match(skb
, par
, NULL
);
188 socket_mt4_v1(const struct sk_buff
*skb
, struct xt_action_param
*par
)
190 return socket_match(skb
, par
, par
->matchinfo
);
193 #ifdef XT_SOCKET_HAVE_IPV6
196 extract_icmp6_fields(const struct sk_buff
*skb
,
197 unsigned int outside_hdrlen
,
199 struct in6_addr
**raddr
,
200 struct in6_addr
**laddr
,
204 struct ipv6hdr
*inside_iph
, _inside_iph
;
205 struct icmp6hdr
*icmph
, _icmph
;
206 __be16
*ports
, _ports
[2];
210 icmph
= skb_header_pointer(skb
, outside_hdrlen
,
211 sizeof(_icmph
), &_icmph
);
215 if (icmph
->icmp6_type
& ICMPV6_INFOMSG_MASK
)
218 inside_iph
= skb_header_pointer(skb
, outside_hdrlen
+ sizeof(_icmph
), sizeof(_inside_iph
), &_inside_iph
);
219 if (inside_iph
== NULL
)
221 inside_nexthdr
= inside_iph
->nexthdr
;
223 inside_hdrlen
= ipv6_skip_exthdr(skb
, outside_hdrlen
+ sizeof(_icmph
) + sizeof(_inside_iph
), &inside_nexthdr
);
224 if (inside_hdrlen
< 0)
225 return 1; /* hjm: Packet has no/incomplete transport layer headers. */
227 if (inside_nexthdr
!= IPPROTO_TCP
&&
228 inside_nexthdr
!= IPPROTO_UDP
)
231 ports
= skb_header_pointer(skb
, inside_hdrlen
,
232 sizeof(_ports
), &_ports
);
236 /* the inside IP packet is the one quoted from our side, thus
237 * its saddr is the local address */
238 *protocol
= inside_nexthdr
;
239 *laddr
= &inside_iph
->saddr
;
241 *raddr
= &inside_iph
->daddr
;
248 socket_mt6_v1(const struct sk_buff
*skb
, struct xt_action_param
*par
)
250 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
251 struct udphdr _hdr
, *hp
= NULL
;
253 struct in6_addr
*daddr
, *saddr
;
256 const struct xt_socket_mtinfo1
*info
= (struct xt_socket_mtinfo1
*) par
->matchinfo
;
258 tproto
= ipv6_find_hdr(skb
, &thoff
, -1, NULL
);
260 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
264 if (tproto
== IPPROTO_UDP
|| tproto
== IPPROTO_TCP
) {
265 hp
= skb_header_pointer(skb
, thoff
,
266 sizeof(_hdr
), &_hdr
);
275 } else if (tproto
== IPPROTO_ICMPV6
) {
276 if (extract_icmp6_fields(skb
, thoff
, &tproto
, &saddr
, &daddr
,
283 sk
= nf_tproxy_get_sock_v6(dev_net(skb
->dev
), tproto
,
284 saddr
, daddr
, sport
, dport
, par
->in
, NFT_LOOKUP_ANY
);
287 bool transparent
= true;
289 /* Ignore sockets listening on INADDR_ANY */
290 wildcard
= (sk
->sk_state
!= TCP_TIME_WAIT
&&
291 ipv6_addr_any(&inet6_sk(sk
)->rcv_saddr
));
293 /* Ignore non-transparent sockets,
294 if XT_SOCKET_TRANSPARENT is used */
295 if (info
&& info
->flags
& XT_SOCKET_TRANSPARENT
)
296 transparent
= ((sk
->sk_state
!= TCP_TIME_WAIT
&&
297 inet_sk(sk
)->transparent
) ||
298 (sk
->sk_state
== TCP_TIME_WAIT
&&
299 inet_twsk(sk
)->tw_transparent
));
301 nf_tproxy_put_sock(sk
);
303 if (wildcard
|| !transparent
)
307 pr_debug("proto %hhd %pI6:%hu -> %pI6:%hu "
308 "(orig %pI6:%hu) sock %p\n",
309 tproto
, saddr
, ntohs(sport
),
311 &iph
->daddr
, hp
? ntohs(hp
->dest
) : 0, sk
);
317 static struct xt_match socket_mt_reg
[] __read_mostly
= {
321 .family
= NFPROTO_IPV4
,
322 .match
= socket_mt4_v0
,
323 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
324 (1 << NF_INET_LOCAL_IN
),
330 .family
= NFPROTO_IPV4
,
331 .match
= socket_mt4_v1
,
332 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
333 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
334 (1 << NF_INET_LOCAL_IN
),
337 #ifdef XT_SOCKET_HAVE_IPV6
341 .family
= NFPROTO_IPV6
,
342 .match
= socket_mt6_v1
,
343 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
344 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
345 (1 << NF_INET_LOCAL_IN
),
351 static int __init
socket_mt_init(void)
353 nf_defrag_ipv4_enable();
354 #ifdef XT_SOCKET_HAVE_IPV6
355 nf_defrag_ipv6_enable();
358 return xt_register_matches(socket_mt_reg
, ARRAY_SIZE(socket_mt_reg
));
361 static void __exit
socket_mt_exit(void)
363 xt_unregister_matches(socket_mt_reg
, ARRAY_SIZE(socket_mt_reg
));
366 module_init(socket_mt_init
);
367 module_exit(socket_mt_exit
);
369 MODULE_LICENSE("GPL");
370 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
371 MODULE_DESCRIPTION("x_tables socket match module");
372 MODULE_ALIAS("ipt_socket");
373 MODULE_ALIAS("ip6t_socket");