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/ipv4/nf_defrag_ipv4.h>
24 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
25 #define XT_SOCKET_HAVE_IPV6 1
26 #include <linux/netfilter_ipv6/ip6_tables.h>
27 #include <net/inet6_hashtables.h>
28 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
31 #include <linux/netfilter/xt_socket.h>
33 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
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
;
95 /* "socket" match based redirection (no specific rule)
96 * ===================================================
98 * There are connections with dynamic endpoints (e.g. FTP data
99 * connection) that the user is unable to add explicit rules
100 * for. These are taken care of by a generic "socket" rule. It is
101 * assumed that the proxy application is trusted to open such
102 * connections without explicit iptables rule (except of course the
103 * generic 'socket' rule). In this case the following sockets are
104 * matched in preference order:
106 * - match: if there's a fully established connection matching the
109 * - match: if there's a non-zero bound listener (possibly with a
110 * non-local address) We don't accept zero-bound listeners, since
111 * then local services could intercept traffic going through the
115 xt_socket_get_sock_v4(struct net
*net
, const u8 protocol
,
116 const __be32 saddr
, const __be32 daddr
,
117 const __be16 sport
, const __be16 dport
,
118 const struct net_device
*in
)
122 return __inet_lookup(net
, &tcp_hashinfo
,
123 saddr
, sport
, daddr
, dport
,
126 return udp4_lib_lookup(net
, saddr
, sport
, daddr
, dport
,
132 static bool xt_socket_sk_is_transparent(struct sock
*sk
)
134 switch (sk
->sk_state
) {
136 return inet_twsk(sk
)->tw_transparent
;
138 case TCP_NEW_SYN_RECV
:
139 return inet_rsk(inet_reqsk(sk
))->no_srccheck
;
142 return inet_sk(sk
)->transparent
;
146 static struct sock
*xt_socket_lookup_slow_v4(const struct sk_buff
*skb
,
147 const struct net_device
*indev
)
149 const struct iphdr
*iph
= ip_hdr(skb
);
150 __be32
uninitialized_var(daddr
), uninitialized_var(saddr
);
151 __be16
uninitialized_var(dport
), uninitialized_var(sport
);
152 u8
uninitialized_var(protocol
);
153 #ifdef XT_SOCKET_HAVE_CONNTRACK
154 struct nf_conn
const *ct
;
155 enum ip_conntrack_info ctinfo
;
158 if (iph
->protocol
== IPPROTO_UDP
|| iph
->protocol
== IPPROTO_TCP
) {
159 struct udphdr _hdr
, *hp
;
161 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
),
162 sizeof(_hdr
), &_hdr
);
166 protocol
= iph
->protocol
;
172 } else if (iph
->protocol
== IPPROTO_ICMP
) {
173 if (extract_icmp4_fields(skb
, &protocol
, &saddr
, &daddr
,
180 #ifdef XT_SOCKET_HAVE_CONNTRACK
181 /* Do the lookup with the original socket address in
182 * case this is a reply packet of an established
183 * SNAT-ted connection.
185 ct
= nf_ct_get(skb
, &ctinfo
);
186 if (ct
&& !nf_ct_is_untracked(ct
) &&
187 ((iph
->protocol
!= IPPROTO_ICMP
&&
188 ctinfo
== IP_CT_ESTABLISHED_REPLY
) ||
189 (iph
->protocol
== IPPROTO_ICMP
&&
190 ctinfo
== IP_CT_RELATED_REPLY
)) &&
191 (ct
->status
& IPS_SRC_NAT_DONE
)) {
193 daddr
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u3
.ip
;
194 dport
= (iph
->protocol
== IPPROTO_TCP
) ?
195 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u
.tcp
.port
:
196 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u
.udp
.port
;
200 return xt_socket_get_sock_v4(dev_net(skb
->dev
), protocol
, saddr
, daddr
,
201 sport
, dport
, indev
);
205 socket_match(const struct sk_buff
*skb
, struct xt_action_param
*par
,
206 const struct xt_socket_mtinfo1
*info
)
208 struct sock
*sk
= skb
->sk
;
211 sk
= xt_socket_lookup_slow_v4(skb
, par
->in
);
214 bool transparent
= true;
216 /* Ignore sockets listening on INADDR_ANY,
217 * unless XT_SOCKET_NOWILDCARD is set
219 wildcard
= (!(info
->flags
& XT_SOCKET_NOWILDCARD
) &&
221 inet_sk(sk
)->inet_rcv_saddr
== 0);
223 /* Ignore non-transparent sockets,
224 * if XT_SOCKET_TRANSPARENT is used
226 if (info
->flags
& XT_SOCKET_TRANSPARENT
)
227 transparent
= xt_socket_sk_is_transparent(sk
);
232 if (wildcard
|| !transparent
)
240 socket_mt4_v0(const struct sk_buff
*skb
, struct xt_action_param
*par
)
242 static struct xt_socket_mtinfo1 xt_info_v0
= {
246 return socket_match(skb
, par
, &xt_info_v0
);
250 socket_mt4_v1_v2(const struct sk_buff
*skb
, struct xt_action_param
*par
)
252 return socket_match(skb
, par
, par
->matchinfo
);
255 #ifdef XT_SOCKET_HAVE_IPV6
258 extract_icmp6_fields(const struct sk_buff
*skb
,
259 unsigned int outside_hdrlen
,
261 const struct in6_addr
**raddr
,
262 const struct in6_addr
**laddr
,
265 struct ipv6hdr
*ipv6_var
)
267 const struct ipv6hdr
*inside_iph
;
268 struct icmp6hdr
*icmph
, _icmph
;
269 __be16
*ports
, _ports
[2];
271 __be16 inside_fragoff
;
274 icmph
= skb_header_pointer(skb
, outside_hdrlen
,
275 sizeof(_icmph
), &_icmph
);
279 if (icmph
->icmp6_type
& ICMPV6_INFOMSG_MASK
)
282 inside_iph
= skb_header_pointer(skb
, outside_hdrlen
+ sizeof(_icmph
),
283 sizeof(*ipv6_var
), ipv6_var
);
284 if (inside_iph
== NULL
)
286 inside_nexthdr
= inside_iph
->nexthdr
;
288 inside_hdrlen
= ipv6_skip_exthdr(skb
, outside_hdrlen
+ sizeof(_icmph
) +
290 &inside_nexthdr
, &inside_fragoff
);
291 if (inside_hdrlen
< 0)
292 return 1; /* hjm: Packet has no/incomplete transport layer headers. */
294 if (inside_nexthdr
!= IPPROTO_TCP
&&
295 inside_nexthdr
!= IPPROTO_UDP
)
298 ports
= skb_header_pointer(skb
, inside_hdrlen
,
299 sizeof(_ports
), &_ports
);
303 /* the inside IP packet is the one quoted from our side, thus
304 * its saddr is the local address */
305 *protocol
= inside_nexthdr
;
306 *laddr
= &inside_iph
->saddr
;
308 *raddr
= &inside_iph
->daddr
;
315 xt_socket_get_sock_v6(struct net
*net
, const u8 protocol
,
316 const struct in6_addr
*saddr
, const struct in6_addr
*daddr
,
317 const __be16 sport
, const __be16 dport
,
318 const struct net_device
*in
)
322 return inet6_lookup(net
, &tcp_hashinfo
,
323 saddr
, sport
, daddr
, dport
,
326 return udp6_lib_lookup(net
, saddr
, sport
, daddr
, dport
,
333 static struct sock
*xt_socket_lookup_slow_v6(const struct sk_buff
*skb
,
334 const struct net_device
*indev
)
336 __be16
uninitialized_var(dport
), uninitialized_var(sport
);
337 const struct in6_addr
*daddr
= NULL
, *saddr
= NULL
;
338 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
339 int thoff
= 0, tproto
;
341 tproto
= ipv6_find_hdr(skb
, &thoff
, -1, NULL
, NULL
);
343 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
347 if (tproto
== IPPROTO_UDP
|| tproto
== IPPROTO_TCP
) {
348 struct udphdr _hdr
, *hp
;
350 hp
= skb_header_pointer(skb
, thoff
, sizeof(_hdr
), &_hdr
);
359 } else if (tproto
== IPPROTO_ICMPV6
) {
360 struct ipv6hdr ipv6_var
;
362 if (extract_icmp6_fields(skb
, thoff
, &tproto
, &saddr
, &daddr
,
363 &sport
, &dport
, &ipv6_var
))
369 return xt_socket_get_sock_v6(dev_net(skb
->dev
), tproto
, saddr
, daddr
,
370 sport
, dport
, indev
);
374 socket_mt6_v1_v2(const struct sk_buff
*skb
, struct xt_action_param
*par
)
376 const struct xt_socket_mtinfo1
*info
= (struct xt_socket_mtinfo1
*) par
->matchinfo
;
377 struct sock
*sk
= skb
->sk
;
380 sk
= xt_socket_lookup_slow_v6(skb
, par
->in
);
383 bool transparent
= true;
385 /* Ignore sockets listening on INADDR_ANY
386 * unless XT_SOCKET_NOWILDCARD is set
388 wildcard
= (!(info
->flags
& XT_SOCKET_NOWILDCARD
) &&
390 ipv6_addr_any(&sk
->sk_v6_rcv_saddr
));
392 /* Ignore non-transparent sockets,
393 * if XT_SOCKET_TRANSPARENT is used
395 if (info
->flags
& XT_SOCKET_TRANSPARENT
)
396 transparent
= xt_socket_sk_is_transparent(sk
);
401 if (wildcard
|| !transparent
)
409 static int socket_mt_v1_check(const struct xt_mtchk_param
*par
)
411 const struct xt_socket_mtinfo1
*info
= (struct xt_socket_mtinfo1
*) par
->matchinfo
;
413 if (info
->flags
& ~XT_SOCKET_FLAGS_V1
) {
414 pr_info("unknown flags 0x%x\n", info
->flags
& ~XT_SOCKET_FLAGS_V1
);
420 static int socket_mt_v2_check(const struct xt_mtchk_param
*par
)
422 const struct xt_socket_mtinfo2
*info
= (struct xt_socket_mtinfo2
*) par
->matchinfo
;
424 if (info
->flags
& ~XT_SOCKET_FLAGS_V2
) {
425 pr_info("unknown flags 0x%x\n", info
->flags
& ~XT_SOCKET_FLAGS_V2
);
431 static struct xt_match socket_mt_reg
[] __read_mostly
= {
435 .family
= NFPROTO_IPV4
,
436 .match
= socket_mt4_v0
,
437 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
438 (1 << NF_INET_LOCAL_IN
),
444 .family
= NFPROTO_IPV4
,
445 .match
= socket_mt4_v1_v2
,
446 .checkentry
= socket_mt_v1_check
,
447 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
448 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
449 (1 << NF_INET_LOCAL_IN
),
452 #ifdef XT_SOCKET_HAVE_IPV6
456 .family
= NFPROTO_IPV6
,
457 .match
= socket_mt6_v1_v2
,
458 .checkentry
= socket_mt_v1_check
,
459 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
460 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
461 (1 << NF_INET_LOCAL_IN
),
468 .family
= NFPROTO_IPV4
,
469 .match
= socket_mt4_v1_v2
,
470 .checkentry
= socket_mt_v2_check
,
471 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
472 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
473 (1 << NF_INET_LOCAL_IN
),
476 #ifdef XT_SOCKET_HAVE_IPV6
480 .family
= NFPROTO_IPV6
,
481 .match
= socket_mt6_v1_v2
,
482 .checkentry
= socket_mt_v2_check
,
483 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
484 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
485 (1 << NF_INET_LOCAL_IN
),
491 static int __init
socket_mt_init(void)
493 nf_defrag_ipv4_enable();
494 #ifdef XT_SOCKET_HAVE_IPV6
495 nf_defrag_ipv6_enable();
498 return xt_register_matches(socket_mt_reg
, ARRAY_SIZE(socket_mt_reg
));
501 static void __exit
socket_mt_exit(void)
503 xt_unregister_matches(socket_mt_reg
, ARRAY_SIZE(socket_mt_reg
));
506 module_init(socket_mt_init
);
507 module_exit(socket_mt_exit
);
509 MODULE_LICENSE("GPL");
510 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
511 MODULE_DESCRIPTION("x_tables socket match module");
512 MODULE_ALIAS("ipt_socket");
513 MODULE_ALIAS("ip6t_socket");