1 /*
2 * Transparent proxy support for Linux/iptables
3 *
4 * Copyright (C) 2007-2008 BalaBit IT Ltd.
5 * Author: Krisztian Kovacs
6 *
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.
10 *
11 */
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>
17 #include <net/tcp.h>
18 #include <net/udp.h>
19 #include <net/icmp.h>
20 #include <net/sock.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>
29 #endif
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>
36 #endif
38 static int
39 extract_icmp4_fields(const struct sk_buff *skb,
40 u8 *protocol,
41 __be32 *raddr,
42 __be32 *laddr,
43 __be16 *rport,
44 __be16 *lport)
45 {
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);
53 if (icmph == NULL)
54 return 1;
56 switch (icmph->type) {
57 case ICMP_DEST_UNREACH:
58 case ICMP_SOURCE_QUENCH:
59 case ICMP_REDIRECT:
60 case ICMP_TIME_EXCEEDED:
61 case ICMP_PARAMETERPROB:
62 break;
63 default:
64 return 1;
65 }
67 inside_iph = skb_header_pointer(skb, outside_hdrlen +
68 sizeof(struct icmphdr),
69 sizeof(_inside_iph), &_inside_iph);
70 if (inside_iph == NULL)
71 return 1;
73 if (inside_iph->protocol != IPPROTO_TCP &&
74 inside_iph->protocol != IPPROTO_UDP)
75 return 1;
77 ports = skb_header_pointer(skb, outside_hdrlen +
78 sizeof(struct icmphdr) +
79 (inside_iph->ihl << 2),
80 sizeof(_ports), &_ports);
81 if (ports == NULL)
82 return 1;
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;
88 *lport = ports[0];
89 *raddr = inside_iph->daddr;
90 *rport = ports[1];
92 return 0;
93 }
95 /* "socket" match based redirection (no specific rule)
96 * ===================================================
97 *
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:
105 *
106 * - match: if there's a fully established connection matching the
107 * _packet_ tuple
108 *
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
112 * box.
113 */
114 static struct sock *
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)
119 {
120 switch (protocol) {
121 case IPPROTO_TCP:
122 return __inet_lookup(net, &tcp_hashinfo,
123 saddr, sport, daddr, dport,
124 in->ifindex);
125 case IPPROTO_UDP:
126 return udp4_lib_lookup(net, saddr, sport, daddr, dport,
127 in->ifindex);
128 }
129 return NULL;
130 }
134 struct sock*
135 xt_socket_get4_sk(const struct sk_buff *skb, struct xt_action_param *par)
136 {
137 const struct iphdr *iph = ip_hdr(skb);
138 struct udphdr _hdr, *hp = NULL;
139 struct sock *sk = skb->sk;
140 __be32 uninitialized_var(daddr), uninitialized_var(saddr);
141 __be16 uninitialized_var(dport), uninitialized_var(sport);
142 u8 uninitialized_var(protocol);
143 #ifdef XT_SOCKET_HAVE_CONNTRACK
144 struct nf_conn const *ct;
145 enum ip_conntrack_info ctinfo;
146 #endif
148 if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
149 hp = skb_header_pointer(skb, ip_hdrlen(skb),
150 sizeof(_hdr), &_hdr);
151 if (hp == NULL)
152 return NULL;
154 protocol = iph->protocol;
155 saddr = iph->saddr;
156 sport = hp->source;
157 daddr = iph->daddr;
158 dport = hp->dest;
160 } else if (iph->protocol == IPPROTO_ICMP) {
161 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
162 &sport, &dport))
163 return NULL;
164 } else {
165 return NULL;
166 }
168 #ifdef XT_SOCKET_HAVE_CONNTRACK
169 /* Do the lookup with the original socket address in case this is a
170 * reply packet of an established SNAT-ted connection. */
172 ct = nf_ct_get(skb, &ctinfo);
173 if (ct && !nf_ct_is_untracked(ct) &&
174 ((iph->protocol != IPPROTO_ICMP &&
175 ctinfo == IP_CT_ESTABLISHED_REPLY) ||
176 (iph->protocol == IPPROTO_ICMP &&
177 ctinfo == IP_CT_RELATED_REPLY)) &&
178 (ct->status & IPS_SRC_NAT_DONE)) {
180 daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
181 dport = (iph->protocol == IPPROTO_TCP) ?
182 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
183 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
184 }
185 #endif
187 if (!sk)
188 sk = xt_socket_get_sock_v4(dev_net(skb->dev), protocol,
189 saddr, daddr, sport, dport,
190 par->in);
192 pr_debug("proto %hhu %pI4:%hu -> %pI4:%hu (orig %pI4:%hu) sock %p\n",
193 protocol, &saddr, ntohs(sport),
194 &daddr, ntohs(dport),
195 &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
197 return sk;
198 }
199 EXPORT_SYMBOL(xt_socket_get4_sk);
201 static bool
202 socket_match(const struct sk_buff *skb, struct xt_action_param *par,
203 const struct xt_socket_mtinfo1 *info)
204 {
205 struct sock *sk;
207 sk = xt_socket_get4_sk(skb, par);
209 if (sk) {
210 bool wildcard;
211 bool transparent = true;
213 /* Ignore sockets listening on INADDR_ANY,
214 * unless XT_SOCKET_NOWILDCARD is set
215 */
216 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
217 sk->sk_state != TCP_TIME_WAIT &&
218 inet_sk(sk)->inet_rcv_saddr == 0);
220 /* Ignore non-transparent sockets,
221 if XT_SOCKET_TRANSPARENT is used */
222 if (info->flags & XT_SOCKET_TRANSPARENT)
223 transparent = ((sk->sk_state != TCP_TIME_WAIT &&
224 inet_sk(sk)->transparent) ||
225 (sk->sk_state == TCP_TIME_WAIT &&
226 inet_twsk(sk)->tw_transparent));
228 if (sk != skb->sk)
229 sock_gen_put(sk);
231 if (wildcard || !transparent)
232 sk = NULL;
233 }
235 return (sk != NULL);
236 }
238 static bool
239 socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
240 {
241 static struct xt_socket_mtinfo1 xt_info_v0 = {
242 .flags = 0,
243 };
245 return socket_match(skb, par, &xt_info_v0);
246 }
248 static bool
249 socket_mt4_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
250 {
251 return socket_match(skb, par, par->matchinfo);
252 }
254 #ifdef XT_SOCKET_HAVE_IPV6
256 static int
257 extract_icmp6_fields(const struct sk_buff *skb,
258 unsigned int outside_hdrlen,
259 int *protocol,
260 const struct in6_addr **raddr,
261 const struct in6_addr **laddr,
262 __be16 *rport,
263 __be16 *lport,
264 struct ipv6hdr *ipv6_var)
265 {
266 const struct ipv6hdr *inside_iph;
267 struct icmp6hdr *icmph, _icmph;
268 __be16 *ports, _ports[2];
269 u8 inside_nexthdr;
270 __be16 inside_fragoff;
271 int inside_hdrlen;
273 icmph = skb_header_pointer(skb, outside_hdrlen,
274 sizeof(_icmph), &_icmph);
275 if (icmph == NULL)
276 return 1;
278 if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
279 return 1;
281 inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph),
282 sizeof(*ipv6_var), ipv6_var);
283 if (inside_iph == NULL)
284 return 1;
285 inside_nexthdr = inside_iph->nexthdr;
287 inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) +
288 sizeof(*ipv6_var),
289 &inside_nexthdr, &inside_fragoff);
290 if (inside_hdrlen < 0)
291 return 1; /* hjm: Packet has no/incomplete transport layer headers. */
293 if (inside_nexthdr != IPPROTO_TCP &&
294 inside_nexthdr != IPPROTO_UDP)
295 return 1;
297 ports = skb_header_pointer(skb, inside_hdrlen,
298 sizeof(_ports), &_ports);
299 if (ports == NULL)
300 return 1;
302 /* the inside IP packet is the one quoted from our side, thus
303 * its saddr is the local address */
304 *protocol = inside_nexthdr;
305 *laddr = &inside_iph->saddr;
306 *lport = ports[0];
307 *raddr = &inside_iph->daddr;
308 *rport = ports[1];
310 return 0;
311 }
313 static struct sock *
314 xt_socket_get_sock_v6(struct net *net, const u8 protocol,
315 const struct in6_addr *saddr, const struct in6_addr *daddr,
316 const __be16 sport, const __be16 dport,
317 const struct net_device *in)
318 {
319 switch (protocol) {
320 case IPPROTO_TCP:
321 return inet6_lookup(net, &tcp_hashinfo,
322 saddr, sport, daddr, dport,
323 in->ifindex);
324 case IPPROTO_UDP:
325 return udp6_lib_lookup(net, saddr, sport, daddr, dport,
326 in->ifindex);
327 }
329 return NULL;
330 }
332 struct sock*
333 xt_socket_get6_sk(const struct sk_buff *skb, struct xt_action_param *par)
334 {
335 struct ipv6hdr ipv6_var, *iph = ipv6_hdr(skb);
336 struct udphdr _hdr, *hp = NULL;
337 struct sock *sk = skb->sk;
338 const struct in6_addr *daddr = NULL, *saddr = NULL;
339 __be16 uninitialized_var(dport), uninitialized_var(sport);
340 int thoff = 0, uninitialized_var(tproto);
342 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
343 if (tproto < 0) {
344 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
345 return NF_DROP;
346 }
348 if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
349 hp = skb_header_pointer(skb, thoff,
350 sizeof(_hdr), &_hdr);
351 if (hp == NULL)
352 return NULL;
354 saddr = &iph->saddr;
355 sport = hp->source;
356 daddr = &iph->daddr;
357 dport = hp->dest;
359 } else if (tproto == IPPROTO_ICMPV6) {
360 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
361 &sport, &dport, &ipv6_var))
362 return NULL;
363 } else {
364 return NULL;
365 }
367 if (!sk)
368 sk = xt_socket_get_sock_v6(dev_net(skb->dev), tproto,
369 saddr, daddr, sport, dport,
370 par->in);
372 pr_debug("proto %hhd %pI6:%hu -> %pI6:%hu "
373 "(orig %pI6:%hu) sock %p\n",
374 tproto, saddr, ntohs(sport),
375 daddr, ntohs(dport),
376 &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
377 return sk;
378 }
379 EXPORT_SYMBOL(xt_socket_get6_sk);
381 static bool
382 socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
383 {
384 struct sock *sk;
385 const struct xt_socket_mtinfo1 *info;
387 info = (struct xt_socket_mtinfo1 *) par->matchinfo;
389 sk = xt_socket_get6_sk(skb, par);
391 if (sk) {
392 bool wildcard;
393 bool transparent = true;
395 /* Ignore sockets listening on INADDR_ANY
396 * unless XT_SOCKET_NOWILDCARD is set
397 */
398 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
399 sk->sk_state != TCP_TIME_WAIT &&
400 ipv6_addr_any(&sk->sk_v6_rcv_saddr));
402 /* Ignore non-transparent sockets,
403 if XT_SOCKET_TRANSPARENT is used */
404 if (info->flags & XT_SOCKET_TRANSPARENT)
405 transparent = ((sk->sk_state != TCP_TIME_WAIT &&
406 inet_sk(sk)->transparent) ||
407 (sk->sk_state == TCP_TIME_WAIT &&
408 inet_twsk(sk)->tw_transparent));
410 if (sk != skb->sk)
411 sock_gen_put(sk);
413 if (wildcard || !transparent)
414 sk = NULL;
415 }
417 return (sk != NULL);
418 }
419 #endif
421 static int socket_mt_v1_check(const struct xt_mtchk_param *par)
422 {
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);
427 return -EINVAL;
428 }
429 return 0;
430 }
432 static int socket_mt_v2_check(const struct xt_mtchk_param *par)
433 {
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);
438 return -EINVAL;
439 }
440 return 0;
441 }
443 static struct xt_match socket_mt_reg[] __read_mostly = {
444 {
445 .name = "socket",
446 .revision = 0,
447 .family = NFPROTO_IPV4,
448 .match = socket_mt4_v0,
449 .hooks = (1 << NF_INET_PRE_ROUTING) |
450 (1 << NF_INET_LOCAL_IN),
451 .me = THIS_MODULE,
452 },
453 {
454 .name = "socket",
455 .revision = 1,
456 .family = NFPROTO_IPV4,
457 .match = socket_mt4_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),
462 .me = THIS_MODULE,
463 },
464 #ifdef XT_SOCKET_HAVE_IPV6
465 {
466 .name = "socket",
467 .revision = 1,
468 .family = NFPROTO_IPV6,
469 .match = socket_mt6_v1_v2,
470 .checkentry = socket_mt_v1_check,
471 .matchsize = sizeof(struct xt_socket_mtinfo1),
472 .hooks = (1 << NF_INET_PRE_ROUTING) |
473 (1 << NF_INET_LOCAL_IN),
474 .me = THIS_MODULE,
475 },
476 #endif
477 {
478 .name = "socket",
479 .revision = 2,
480 .family = NFPROTO_IPV4,
481 .match = socket_mt4_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),
486 .me = THIS_MODULE,
487 },
488 #ifdef XT_SOCKET_HAVE_IPV6
489 {
490 .name = "socket",
491 .revision = 2,
492 .family = NFPROTO_IPV6,
493 .match = socket_mt6_v1_v2,
494 .checkentry = socket_mt_v2_check,
495 .matchsize = sizeof(struct xt_socket_mtinfo1),
496 .hooks = (1 << NF_INET_PRE_ROUTING) |
497 (1 << NF_INET_LOCAL_IN),
498 .me = THIS_MODULE,
499 },
500 #endif
501 };
503 static int __init socket_mt_init(void)
504 {
505 nf_defrag_ipv4_enable();
506 #ifdef XT_SOCKET_HAVE_IPV6
507 nf_defrag_ipv6_enable();
508 #endif
510 return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
511 }
513 static void __exit socket_mt_exit(void)
514 {
515 xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
516 }
518 module_init(socket_mt_init);
519 module_exit(socket_mt_exit);
521 MODULE_LICENSE("GPL");
522 MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
523 MODULE_DESCRIPTION("x_tables socket match module");
524 MODULE_ALIAS("ipt_socket");
525 MODULE_ALIAS("ip6t_socket");