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(struct net
*net
,
147 const struct sk_buff
*skb
,
148 const struct net_device
*indev
)
150 const struct iphdr
*iph
= ip_hdr(skb
);
151 __be32
uninitialized_var(daddr
), uninitialized_var(saddr
);
152 __be16
uninitialized_var(dport
), uninitialized_var(sport
);
153 u8
uninitialized_var(protocol
);
154 #ifdef XT_SOCKET_HAVE_CONNTRACK
155 struct nf_conn
const *ct
;
156 enum ip_conntrack_info ctinfo
;
159 if (iph
->protocol
== IPPROTO_UDP
|| iph
->protocol
== IPPROTO_TCP
) {
160 struct udphdr _hdr
, *hp
;
162 hp
= skb_header_pointer(skb
, ip_hdrlen(skb
),
163 sizeof(_hdr
), &_hdr
);
167 protocol
= iph
->protocol
;
173 } else if (iph
->protocol
== IPPROTO_ICMP
) {
174 if (extract_icmp4_fields(skb
, &protocol
, &saddr
, &daddr
,
181 #ifdef XT_SOCKET_HAVE_CONNTRACK
182 /* Do the lookup with the original socket address in
183 * case this is a reply packet of an established
184 * SNAT-ted connection.
186 ct
= nf_ct_get(skb
, &ctinfo
);
187 if (ct
&& !nf_ct_is_untracked(ct
) &&
188 ((iph
->protocol
!= IPPROTO_ICMP
&&
189 ctinfo
== IP_CT_ESTABLISHED_REPLY
) ||
190 (iph
->protocol
== IPPROTO_ICMP
&&
191 ctinfo
== IP_CT_RELATED_REPLY
)) &&
192 (ct
->status
& IPS_SRC_NAT_DONE
)) {
194 daddr
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u3
.ip
;
195 dport
= (iph
->protocol
== IPPROTO_TCP
) ?
196 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u
.tcp
.port
:
197 ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u
.udp
.port
;
201 return xt_socket_get_sock_v4(net
, protocol
, saddr
, daddr
,
202 sport
, dport
, indev
);
206 socket_match(const struct sk_buff
*skb
, struct xt_action_param
*par
,
207 const struct xt_socket_mtinfo1
*info
)
209 struct sk_buff
*pskb
= (struct sk_buff
*)skb
;
210 struct sock
*sk
= skb
->sk
;
213 sk
= xt_socket_lookup_slow_v4(par
->net
, skb
, par
->in
);
216 bool transparent
= true;
218 /* Ignore sockets listening on INADDR_ANY,
219 * unless XT_SOCKET_NOWILDCARD is set
221 wildcard
= (!(info
->flags
& XT_SOCKET_NOWILDCARD
) &&
223 inet_sk(sk
)->inet_rcv_saddr
== 0);
225 /* Ignore non-transparent sockets,
226 * if XT_SOCKET_TRANSPARENT is used
228 if (info
->flags
& XT_SOCKET_TRANSPARENT
)
229 transparent
= xt_socket_sk_is_transparent(sk
);
231 if (info
->flags
& XT_SOCKET_RESTORESKMARK
&& !wildcard
&&
233 pskb
->mark
= sk
->sk_mark
;
238 if (wildcard
|| !transparent
)
246 socket_mt4_v0(const struct sk_buff
*skb
, struct xt_action_param
*par
)
248 static struct xt_socket_mtinfo1 xt_info_v0
= {
252 return socket_match(skb
, par
, &xt_info_v0
);
256 socket_mt4_v1_v2_v3(const struct sk_buff
*skb
, struct xt_action_param
*par
)
258 return socket_match(skb
, par
, par
->matchinfo
);
261 #ifdef XT_SOCKET_HAVE_IPV6
264 extract_icmp6_fields(const struct sk_buff
*skb
,
265 unsigned int outside_hdrlen
,
267 const struct in6_addr
**raddr
,
268 const struct in6_addr
**laddr
,
271 struct ipv6hdr
*ipv6_var
)
273 const struct ipv6hdr
*inside_iph
;
274 struct icmp6hdr
*icmph
, _icmph
;
275 __be16
*ports
, _ports
[2];
277 __be16 inside_fragoff
;
280 icmph
= skb_header_pointer(skb
, outside_hdrlen
,
281 sizeof(_icmph
), &_icmph
);
285 if (icmph
->icmp6_type
& ICMPV6_INFOMSG_MASK
)
288 inside_iph
= skb_header_pointer(skb
, outside_hdrlen
+ sizeof(_icmph
),
289 sizeof(*ipv6_var
), ipv6_var
);
290 if (inside_iph
== NULL
)
292 inside_nexthdr
= inside_iph
->nexthdr
;
294 inside_hdrlen
= ipv6_skip_exthdr(skb
, outside_hdrlen
+ sizeof(_icmph
) +
296 &inside_nexthdr
, &inside_fragoff
);
297 if (inside_hdrlen
< 0)
298 return 1; /* hjm: Packet has no/incomplete transport layer headers. */
300 if (inside_nexthdr
!= IPPROTO_TCP
&&
301 inside_nexthdr
!= IPPROTO_UDP
)
304 ports
= skb_header_pointer(skb
, inside_hdrlen
,
305 sizeof(_ports
), &_ports
);
309 /* the inside IP packet is the one quoted from our side, thus
310 * its saddr is the local address */
311 *protocol
= inside_nexthdr
;
312 *laddr
= &inside_iph
->saddr
;
314 *raddr
= &inside_iph
->daddr
;
321 xt_socket_get_sock_v6(struct net
*net
, const u8 protocol
,
322 const struct in6_addr
*saddr
, const struct in6_addr
*daddr
,
323 const __be16 sport
, const __be16 dport
,
324 const struct net_device
*in
)
328 return inet6_lookup(net
, &tcp_hashinfo
,
329 saddr
, sport
, daddr
, dport
,
332 return udp6_lib_lookup(net
, saddr
, sport
, daddr
, dport
,
339 static struct sock
*xt_socket_lookup_slow_v6(struct net
*net
,
340 const struct sk_buff
*skb
,
341 const struct net_device
*indev
)
343 __be16
uninitialized_var(dport
), uninitialized_var(sport
);
344 const struct in6_addr
*daddr
= NULL
, *saddr
= NULL
;
345 struct ipv6hdr
*iph
= ipv6_hdr(skb
);
346 int thoff
= 0, tproto
;
348 tproto
= ipv6_find_hdr(skb
, &thoff
, -1, NULL
, NULL
);
350 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
354 if (tproto
== IPPROTO_UDP
|| tproto
== IPPROTO_TCP
) {
355 struct udphdr _hdr
, *hp
;
357 hp
= skb_header_pointer(skb
, thoff
, sizeof(_hdr
), &_hdr
);
366 } else if (tproto
== IPPROTO_ICMPV6
) {
367 struct ipv6hdr ipv6_var
;
369 if (extract_icmp6_fields(skb
, thoff
, &tproto
, &saddr
, &daddr
,
370 &sport
, &dport
, &ipv6_var
))
376 return xt_socket_get_sock_v6(net
, tproto
, saddr
, daddr
,
377 sport
, dport
, indev
);
381 socket_mt6_v1_v2_v3(const struct sk_buff
*skb
, struct xt_action_param
*par
)
383 const struct xt_socket_mtinfo1
*info
= (struct xt_socket_mtinfo1
*) par
->matchinfo
;
384 struct sk_buff
*pskb
= (struct sk_buff
*)skb
;
385 struct sock
*sk
= skb
->sk
;
388 sk
= xt_socket_lookup_slow_v6(par
->net
, skb
, par
->in
);
391 bool transparent
= true;
393 /* Ignore sockets listening on INADDR_ANY
394 * unless XT_SOCKET_NOWILDCARD is set
396 wildcard
= (!(info
->flags
& XT_SOCKET_NOWILDCARD
) &&
398 ipv6_addr_any(&sk
->sk_v6_rcv_saddr
));
400 /* Ignore non-transparent sockets,
401 * if XT_SOCKET_TRANSPARENT is used
403 if (info
->flags
& XT_SOCKET_TRANSPARENT
)
404 transparent
= xt_socket_sk_is_transparent(sk
);
406 if (info
->flags
& XT_SOCKET_RESTORESKMARK
&& !wildcard
&&
408 pskb
->mark
= sk
->sk_mark
;
413 if (wildcard
|| !transparent
)
421 static int socket_mt_v1_check(const struct xt_mtchk_param
*par
)
423 const struct xt_socket_mtinfo1
*info
= (struct xt_socket_mtinfo1
*) par
->matchinfo
;
425 if (info
->flags
& ~XT_SOCKET_FLAGS_V1
) {
426 pr_info("unknown flags 0x%x\n", info
->flags
& ~XT_SOCKET_FLAGS_V1
);
432 static int socket_mt_v2_check(const struct xt_mtchk_param
*par
)
434 const struct xt_socket_mtinfo2
*info
= (struct xt_socket_mtinfo2
*) par
->matchinfo
;
436 if (info
->flags
& ~XT_SOCKET_FLAGS_V2
) {
437 pr_info("unknown flags 0x%x\n", info
->flags
& ~XT_SOCKET_FLAGS_V2
);
443 static int socket_mt_v3_check(const struct xt_mtchk_param
*par
)
445 const struct xt_socket_mtinfo3
*info
=
446 (struct xt_socket_mtinfo3
*)par
->matchinfo
;
448 if (info
->flags
& ~XT_SOCKET_FLAGS_V3
) {
449 pr_info("unknown flags 0x%x\n",
450 info
->flags
& ~XT_SOCKET_FLAGS_V3
);
456 static struct xt_match socket_mt_reg
[] __read_mostly
= {
460 .family
= NFPROTO_IPV4
,
461 .match
= socket_mt4_v0
,
462 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
463 (1 << NF_INET_LOCAL_IN
),
469 .family
= NFPROTO_IPV4
,
470 .match
= socket_mt4_v1_v2_v3
,
471 .checkentry
= socket_mt_v1_check
,
472 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
473 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
474 (1 << NF_INET_LOCAL_IN
),
477 #ifdef XT_SOCKET_HAVE_IPV6
481 .family
= NFPROTO_IPV6
,
482 .match
= socket_mt6_v1_v2_v3
,
483 .checkentry
= socket_mt_v1_check
,
484 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
485 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
486 (1 << NF_INET_LOCAL_IN
),
493 .family
= NFPROTO_IPV4
,
494 .match
= socket_mt4_v1_v2_v3
,
495 .checkentry
= socket_mt_v2_check
,
496 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
497 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
498 (1 << NF_INET_LOCAL_IN
),
501 #ifdef XT_SOCKET_HAVE_IPV6
505 .family
= NFPROTO_IPV6
,
506 .match
= socket_mt6_v1_v2_v3
,
507 .checkentry
= socket_mt_v2_check
,
508 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
509 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
510 (1 << NF_INET_LOCAL_IN
),
517 .family
= NFPROTO_IPV4
,
518 .match
= socket_mt4_v1_v2_v3
,
519 .checkentry
= socket_mt_v3_check
,
520 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
521 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
522 (1 << NF_INET_LOCAL_IN
),
525 #ifdef XT_SOCKET_HAVE_IPV6
529 .family
= NFPROTO_IPV6
,
530 .match
= socket_mt6_v1_v2_v3
,
531 .checkentry
= socket_mt_v3_check
,
532 .matchsize
= sizeof(struct xt_socket_mtinfo1
),
533 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
534 (1 << NF_INET_LOCAL_IN
),
540 static int __init
socket_mt_init(void)
542 nf_defrag_ipv4_enable();
543 #ifdef XT_SOCKET_HAVE_IPV6
544 nf_defrag_ipv6_enable();
547 return xt_register_matches(socket_mt_reg
, ARRAY_SIZE(socket_mt_reg
));
550 static void __exit
socket_mt_exit(void)
552 xt_unregister_matches(socket_mt_reg
, ARRAY_SIZE(socket_mt_reg
));
555 module_init(socket_mt_init
);
556 module_exit(socket_mt_exit
);
558 MODULE_LICENSE("GPL");
559 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
560 MODULE_DESCRIPTION("x_tables socket match module");
561 MODULE_ALIAS("ipt_socket");
562 MODULE_ALIAS("ip6t_socket");