-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The TPROXY target implements redirection of non-local TCP/UDP traffic to local sockets. Additionally, it's possible to manipulate the packet mark if and only if a socket has been found. (We need this because we cannot use multiple targets in the same iptables rule.) Signed-off-by: KOVACS Krisztian <hidden@sch.bme.hu> Signed-off-by: Patrick McHardy <kaber@trash.net>
- Loading branch information
KOVACS Krisztian
authored and
Patrick McHardy
committed
Oct 8, 2008
1 parent
136cdc7
commit e843927
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef _XT_TPROXY_H_target | ||
#define _XT_TPROXY_H_target | ||
|
||
/* TPROXY target is capable of marking the packet to perform | ||
* redirection. We can get rid of that whenever we get support for | ||
* mutliple targets in the same rule. */ | ||
struct xt_tproxy_target_info { | ||
u_int32_t mark_mask; | ||
u_int32_t mark_value; | ||
__be32 laddr; | ||
__be16 lport; | ||
}; | ||
|
||
#endif /* _XT_TPROXY_H_target */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Transparent proxy support for Linux/iptables | ||
* | ||
* Copyright (c) 2006-2007 BalaBit IT Ltd. | ||
* Author: Balazs Scheidler, Krisztian Kovacs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2 as | ||
* published by the Free Software Foundation. | ||
* | ||
*/ | ||
|
||
#include <linux/module.h> | ||
#include <linux/skbuff.h> | ||
#include <linux/ip.h> | ||
#include <net/checksum.h> | ||
#include <net/udp.h> | ||
#include <net/inet_sock.h> | ||
|
||
#include <linux/netfilter/x_tables.h> | ||
#include <linux/netfilter_ipv4/ip_tables.h> | ||
#include <linux/netfilter/xt_TPROXY.h> | ||
|
||
#include <net/netfilter/ipv4/nf_defrag_ipv4.h> | ||
#include <net/netfilter/nf_tproxy_core.h> | ||
|
||
static unsigned int | ||
tproxy_tg(struct sk_buff *skb, | ||
const struct net_device *in, | ||
const struct net_device *out, | ||
unsigned int hooknum, | ||
const struct xt_target *target, | ||
const void *targinfo) | ||
{ | ||
const struct iphdr *iph = ip_hdr(skb); | ||
const struct xt_tproxy_target_info *tgi = targinfo; | ||
struct udphdr _hdr, *hp; | ||
struct sock *sk; | ||
|
||
hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr); | ||
if (hp == NULL) | ||
return NF_DROP; | ||
|
||
sk = nf_tproxy_get_sock_v4(dev_net(skb->dev), iph->protocol, | ||
iph->saddr, tgi->laddr ? tgi->laddr : iph->daddr, | ||
hp->source, tgi->lport ? tgi->lport : hp->dest, | ||
in, true); | ||
|
||
/* NOTE: assign_sock consumes our sk reference */ | ||
if (sk && nf_tproxy_assign_sock(skb, sk)) { | ||
/* This should be in a separate target, but we don't do multiple | ||
targets on the same rule yet */ | ||
skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value; | ||
|
||
pr_debug("redirecting: proto %u %08x:%u -> %08x:%u, mark: %x\n", | ||
iph->protocol, ntohl(iph->daddr), ntohs(hp->dest), | ||
ntohl(tgi->laddr), ntohs(tgi->lport), skb->mark); | ||
return NF_ACCEPT; | ||
} | ||
|
||
pr_debug("no socket, dropping: proto %u %08x:%u -> %08x:%u, mark: %x\n", | ||
iph->protocol, ntohl(iph->daddr), ntohs(hp->dest), | ||
ntohl(tgi->laddr), ntohs(tgi->lport), skb->mark); | ||
return NF_DROP; | ||
} | ||
|
||
static bool | ||
tproxy_tg_check(const char *tablename, | ||
const void *entry, | ||
const struct xt_target *target, | ||
void *targetinfo, | ||
unsigned int hook_mask) | ||
{ | ||
const struct ipt_ip *i = entry; | ||
|
||
if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) | ||
&& !(i->invflags & IPT_INV_PROTO)) | ||
return true; | ||
|
||
pr_info("xt_TPROXY: Can be used only in combination with " | ||
"either -p tcp or -p udp\n"); | ||
return false; | ||
} | ||
|
||
static struct xt_target tproxy_tg_reg __read_mostly = { | ||
.name = "TPROXY", | ||
.family = AF_INET, | ||
.table = "mangle", | ||
.target = tproxy_tg, | ||
.targetsize = sizeof(struct xt_tproxy_target_info), | ||
.checkentry = tproxy_tg_check, | ||
.hooks = 1 << NF_INET_PRE_ROUTING, | ||
.me = THIS_MODULE, | ||
}; | ||
|
||
static int __init tproxy_tg_init(void) | ||
{ | ||
nf_defrag_ipv4_enable(); | ||
return xt_register_target(&tproxy_tg_reg); | ||
} | ||
|
||
static void __exit tproxy_tg_exit(void) | ||
{ | ||
xt_unregister_target(&tproxy_tg_reg); | ||
} | ||
|
||
module_init(tproxy_tg_init); | ||
module_exit(tproxy_tg_exit); | ||
MODULE_LICENSE("GPL"); | ||
MODULE_AUTHOR("Krisztian Kovacs"); | ||
MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module."); | ||
MODULE_ALIAS("ipt_TPROXY"); |