Release 4.18 net/netfilter/xt_TPROXY.c
/*
* Transparent proxy support for Linux/iptables
*
* Copyright (c) 2006-2010 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.
*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <net/checksum.h>
#include <net/udp.h>
#include <net/tcp.h>
#include <net/inet_sock.h>
#include <net/inet_hashtables.h>
#include <linux/inetdevice.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
#define XT_TPROXY_HAVE_IPV6 1
#include <net/if_inet6.h>
#include <net/addrconf.h>
#include <net/inet6_hashtables.h>
#include <linux/netfilter_ipv6/ip6_tables.h>
#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
#endif
#include <net/netfilter/nf_tproxy.h>
#include <linux/netfilter/xt_TPROXY.h>
/* assign a socket to the skb -- consumes sk */
static void
nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk)
{
skb_orphan(skb);
skb->sk = sk;
skb->destructor = sock_edemux;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| Florian Westphal | 33 | 100.00% | 1 | 100.00% |
| Total | 33 | 100.00% | 1 | 100.00% |
static unsigned int
tproxy_tg4(struct net *net, struct sk_buff *skb, __be32 laddr, __be16 lport,
u_int32_t mark_mask, u_int32_t mark_value)
{
const struct iphdr *iph = ip_hdr(skb);
struct udphdr _hdr, *hp;
struct sock *sk;
hp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_hdr), &_hdr);
if (hp == NULL)
return NF_DROP;
/* check if there's an ongoing connection on the packet
* addresses, this happens if the redirect already happened
* and the current packet belongs to an already established
* connection */
sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
iph->saddr, iph->daddr,
hp->source, hp->dest,
skb->dev, NF_TPROXY_LOOKUP_ESTABLISHED);
laddr = nf_tproxy_laddr4(skb, laddr, iph->daddr);
if (!lport)
lport = hp->dest;
/* UDP has no TCP_TIME_WAIT state, so we never enter here */
if (sk && sk->sk_state == TCP_TIME_WAIT)
/* reopening a TIME_WAIT connection needs special handling */
sk = nf_tproxy_handle_time_wait4(net, skb, laddr, lport, sk);
else if (!sk)
/* no, there's no established connection, check if
* there's a listener on the redirected addr/port */
sk = nf_tproxy_get_sock_v4(net, skb, iph->protocol,
iph->saddr, laddr,
hp->source, lport,
skb->dev, NF_TPROXY_LOOKUP_LISTENER);
/* NOTE: assign_sock consumes our sk reference */
if (sk && nf_tproxy_sk_is_transparent(sk)) {
/* This should be in a separate target, but we don't do multiple
targets on the same rule yet */
skb->mark = (skb->mark & ~mark_mask) ^ mark_value;
pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
iph->protocol, &iph->daddr, ntohs(hp->dest),
&laddr, ntohs(lport), skb->mark);
nf_tproxy_assign_sock(skb, sk);
return NF_ACCEPT;
}
pr_debug("no socket, dropping: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
iph->protocol, &iph->saddr, ntohs(hp->source),
&iph->daddr, ntohs(hp->dest), skb->mark);
return NF_DROP;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| Balazs Scheidler | 246 | 77.60% | 2 | 25.00% |
| Máté Eckl | 28 | 8.83% | 1 | 12.50% |
| KOVACS Krisztian | 18 | 5.68% | 1 | 12.50% |
| Eric W. Biedermann | 9 | 2.84% | 1 | 12.50% |
| Florian Westphal | 7 | 2.21% | 1 | 12.50% |
| Liping Zhang | 5 | 1.58% | 1 | 12.50% |
| Craig Gallek | 4 | 1.26% | 1 | 12.50% |
| Total | 317 | 100.00% | 8 | 100.00% |
static unsigned int
tproxy_tg4_v0(struct sk_buff *skb, const struct xt_action_param *par)
{
const struct xt_tproxy_target_info *tgi = par->targinfo;
return tproxy_tg4(xt_net(par), skb, tgi->laddr, tgi->lport,
tgi->mark_mask, tgi->mark_value);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| Balazs Scheidler | 31 | 56.36% | 2 | 66.67% |
| Máté Eckl | 24 | 43.64% | 1 | 33.33% |
| Total | 55 | 100.00% | 3 | 100.00% |
static unsigned int
tproxy_tg4_v1(struct sk_buff *skb, const struct xt_action_param *par)
{
const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
return tproxy_tg4(xt_net(par), skb, tgi->laddr.ip, tgi->lport,
tgi->mark_mask, tgi->mark_value);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| Máté Eckl | 30 | 52.63% | 1 | 16.67% |
| Balazs Scheidler | 21 | 36.84% | 2 | 33.33% |
| Pablo Neira Ayuso | 3 | 5.26% | 1 | 16.67% |
| Craig Gallek | 2 | 3.51% | 1 | 16.67% |
| Eric W. Biedermann | 1 | 1.75% | 1 | 16.67% |
| Total | 57 | 100.00% | 6 | 100.00% |
#ifdef XT_TPROXY_HAVE_IPV6
static unsigned int
tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
{
const struct ipv6hdr *iph = ipv6_hdr(skb);
const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
struct udphdr _hdr, *hp;
struct sock *sk;
const struct in6_addr *laddr;
__be16 lport;
int thoff = 0;
int tproto;
tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
if (tproto < 0) {
pr_debug("unable to find transport header in IPv6 packet, dropping\n");
return NF_DROP;
}
hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
if (hp == NULL) {
pr_debug("unable to grab transport header contents in IPv6 packet, dropping\n");
return NF_DROP;
}
/* check if there's an ongoing connection on the packet
* addresses, this happens if the redirect already happened
* and the current packet belongs to an already established
* connection */
sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff, tproto,
&iph->saddr, &iph->daddr,
hp->source, hp->dest,
xt_in(par), NF_TPROXY_LOOKUP_ESTABLISHED);
laddr = nf_tproxy_laddr6(skb, &tgi->laddr.in6, &iph->daddr);
lport = tgi->lport ? tgi->lport : hp->dest;
/* UDP has no TCP_TIME_WAIT state, so we never enter here */
if (sk && sk->sk_state == TCP_TIME_WAIT) {
const struct xt_tproxy_target_info_v1 *tgi = par->targinfo;
/* reopening a TIME_WAIT connection needs special handling */
sk = nf_tproxy_handle_time_wait6(skb, tproto, thoff,
xt_net(par),
&tgi->laddr.in6,
tgi->lport,
sk);
}
else if (!sk)
/* no there's no established connection, check if
* there's a listener on the redirected addr/port */
sk = nf_tproxy_get_sock_v6(xt_net(par), skb, thoff,
tproto, &iph->saddr, laddr,
hp->source, lport,
xt_in(par), NF_TPROXY_LOOKUP_LISTENER);
/* NOTE: assign_sock consumes our sk reference */
if (sk && nf_tproxy_sk_is_transparent(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 %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
tproto, &iph->saddr, ntohs(hp->source),
laddr, ntohs(lport), skb->mark);
nf_tproxy_assign_sock(skb, sk);
return NF_ACCEPT;
}
pr_debug("no socket, dropping: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
tproto, &iph->saddr, ntohs(hp->source),
&iph->daddr, ntohs(hp->dest), skb->mark);
return NF_DROP;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| Balazs Scheidler | 223 | 53.73% | 3 | 27.27% |
| KOVACS Krisztian | 127 | 30.60% | 1 | 9.09% |
| Máté Eckl | 31 | 7.47% | 1 | 9.09% |
| Pablo Neira Ayuso | 12 | 2.89% | 1 | 9.09% |
| Craig Gallek | 8 | 1.93% | 1 | 9.09% |
| Florian Westphal | 7 | 1.69% | 1 | 9.09% |
| Hans Schillstrom | 4 | 0.96% | 1 | 9.09% |
| Eric W. Biedermann | 2 | 0.48% | 1 | 9.09% |
| Jan Engelhardt | 1 | 0.24% | 1 | 9.09% |
| Total | 415 | 100.00% | 11 | 100.00% |
static int tproxy_tg6_check(const struct xt_tgchk_param *par)
{
const struct ip6t_ip6 *i = par->entryinfo;
int err;
err = nf_defrag_ipv6_enable(par->net);
if (err)
return err;
if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP) &&
!(i->invflags & IP6T_INV_PROTO))
return 0;
pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
return -EINVAL;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| Balazs Scheidler | 56 | 71.79% | 1 | 25.00% |
| Florian Westphal | 21 | 26.92% | 2 | 50.00% |
| Pablo Neira Ayuso | 1 | 1.28% | 1 | 25.00% |
| Total | 78 | 100.00% | 4 | 100.00% |
#endif
static int tproxy_tg4_check(const struct xt_tgchk_param *par)
{
const struct ipt_ip *i = par->entryinfo;
int err;
err = nf_defrag_ipv4_enable(par->net);
if (err)
return err;
if ((i->proto == IPPROTO_TCP || i->proto == IPPROTO_UDP)
&& !(i->invflags & IPT_INV_PROTO))
return 0;
pr_info_ratelimited("Can be used only with -p tcp or -p udp\n");
return -EINVAL;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| KOVACS Krisztian | 47 | 60.26% | 1 | 14.29% |
| Florian Westphal | 21 | 26.92% | 2 | 28.57% |
| Jan Engelhardt | 9 | 11.54% | 3 | 42.86% |
| Balazs Scheidler | 1 | 1.28% | 1 | 14.29% |
| Total | 78 | 100.00% | 7 | 100.00% |
static struct xt_target tproxy_tg_reg[] __read_mostly = {
{
.name = "TPROXY",
.family = NFPROTO_IPV4,
.table = "mangle",
.target = tproxy_tg4_v0,
.revision = 0,
.targetsize = sizeof(struct xt_tproxy_target_info),
.checkentry = tproxy_tg4_check,
.hooks = 1 << NF_INET_PRE_ROUTING,
.me = THIS_MODULE,
},
{
.name = "TPROXY",
.family = NFPROTO_IPV4,
.table = "mangle",
.target = tproxy_tg4_v1,
.revision = 1,
.targetsize = sizeof(struct xt_tproxy_target_info_v1),
.checkentry = tproxy_tg4_check,
.hooks = 1 << NF_INET_PRE_ROUTING,
.me = THIS_MODULE,
},
#ifdef XT_TPROXY_HAVE_IPV6
{
.name = "TPROXY",
.family = NFPROTO_IPV6,
.table = "mangle",
.target = tproxy_tg6_v1,
.revision = 1,
.targetsize = sizeof(struct xt_tproxy_target_info_v1),
.checkentry = tproxy_tg6_check,
.hooks = 1 << NF_INET_PRE_ROUTING,
.me = THIS_MODULE,
},
#endif
};
static int __init tproxy_tg_init(void)
{
return xt_register_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| KOVACS Krisztian | 14 | 70.00% | 1 | 50.00% |
| Balazs Scheidler | 6 | 30.00% | 1 | 50.00% |
| Total | 20 | 100.00% | 2 | 100.00% |
static void __exit tproxy_tg_exit(void)
{
xt_unregister_targets(tproxy_tg_reg, ARRAY_SIZE(tproxy_tg_reg));
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| KOVACS Krisztian | 13 | 68.42% | 1 | 50.00% |
| Balazs Scheidler | 6 | 31.58% | 1 | 50.00% |
| Total | 19 | 100.00% | 2 | 100.00% |
module_init(tproxy_tg_init);
module_exit(tproxy_tg_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Balazs Scheidler, Krisztian Kovacs");
MODULE_DESCRIPTION("Netfilter transparent proxy (TPROXY) target module.");
MODULE_ALIAS("ipt_TPROXY");
MODULE_ALIAS("ip6t_TPROXY");
Overall Contributors
| Person | Tokens | Prop | Commits | CommitProp |
| Balazs Scheidler | 739 | 54.18% | 3 | 12.50% |
| KOVACS Krisztian | 336 | 24.63% | 2 | 8.33% |
| Máté Eckl | 120 | 8.80% | 1 | 4.17% |
| Florian Westphal | 99 | 7.26% | 5 | 20.83% |
| Jan Engelhardt | 17 | 1.25% | 5 | 20.83% |
| Pablo Neira Ayuso | 16 | 1.17% | 2 | 8.33% |
| Craig Gallek | 14 | 1.03% | 1 | 4.17% |
| Eric W. Biedermann | 12 | 0.88% | 1 | 4.17% |
| Liping Zhang | 5 | 0.37% | 1 | 4.17% |
| Hans Schillstrom | 4 | 0.29% | 1 | 4.17% |
| Igor Maravić | 1 | 0.07% | 1 | 4.17% |
| Changli Gao | 1 | 0.07% | 1 | 4.17% |
| Total | 1364 | 100.00% | 24 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.