2 * Transparent proxy support for Linux/iptables
4 * Copyright (c) 2006-2007 BalaBit IT Ltd.
5 * Author: Balazs Scheidler, 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.
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
16 #include <net/checksum.h>
18 #include <net/inet_sock.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter_ipv4/ip_tables.h>
22 #include <linux/netfilter/xt_TPROXY.h>
24 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
25 #include <net/netfilter/nf_tproxy_core.h>
28 tproxy_tg(struct sk_buff *skb,
29 const struct net_device *in,
30 const struct net_device *out,
32 const struct xt_target *target,
35 const struct iphdr *iph = ip_hdr(skb);
36 const struct xt_tproxy_target_info *tgi = targinfo;
37 struct udphdr _hdr, *hp;
40 hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
44 sk = nf_tproxy_get_sock_v4(dev_net(skb->dev), iph->protocol,
45 iph->saddr, tgi->laddr ? tgi->laddr : iph->daddr,
46 hp->source, tgi->lport ? tgi->lport : hp->dest,
49 /* NOTE: assign_sock consumes our sk reference */
50 if (sk && nf_tproxy_assign_sock(skb, sk)) {
51 /* This should be in a separate target, but we don't do multiple
52 targets on the same rule yet */
53 skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;
55 pr_debug("redirecting: proto %u %08x:%u -> %08x:%u, mark: %x\n",
56 iph->protocol, ntohl(iph->daddr), ntohs(hp->dest),
57 ntohl(tgi->laddr), ntohs(tgi->lport), skb->mark);
61 pr_debug("no socket, dropping: proto %u %08x:%u -> %08x:%u, mark: %x\n",
62 iph->protocol, ntohl(iph->daddr), ntohs(hp->dest),
63 ntohl(tgi->laddr), ntohs(tgi->lport), skb->mark);
68 tproxy_tg_check(const char *tablename,
70 const struct xt_target *target,
72 unsigned int hook_mask)
74 const struct ipt_ip *i = entry;
76 if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
77 && !(i->invflags & IPT_INV_PROTO))
80 pr_info("xt_TPROXY: Can be used only in combination with "
81 "either -p tcp or -p udp\n");
85 static struct xt_target tproxy_tg_reg __read_mostly = {
90 .targetsize = sizeof(struct xt_tproxy_target_info),
91 .checkentry = tproxy_tg_check,
92 .hooks = 1 << NF_INET_PRE_ROUTING,
96 static int __init tproxy_tg_init(void)
98 nf_defrag_ipv4_enable();
99 return xt_register_target(&tproxy_tg_reg);
102 static void __exit tproxy_tg_exit(void)
104 xt_unregister_target(&tproxy_tg_reg);
107 module_init(tproxy_tg_init);
108 module_exit(tproxy_tg_exit);
109 MODULE_LICENSE("GPL");
110 MODULE_AUTHOR("Krisztian Kovacs");
111 MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
112 MODULE_ALIAS("ipt_TPROXY");