2006-12-03 06:09:41 +00:00
|
|
|
/*
|
|
|
|
* ip_conntrack_proto_gre.c - Version 3.0
|
|
|
|
*
|
|
|
|
* Connection tracking protocol helper module for GRE.
|
|
|
|
*
|
|
|
|
* GRE is a generic encapsulation protocol, which is generally not very
|
|
|
|
* suited for NAT, as it has no protocol-specific part as port numbers.
|
|
|
|
*
|
|
|
|
* It has an optional key field, which may help us distinguishing two
|
|
|
|
* connections between the same two hosts.
|
|
|
|
*
|
|
|
|
* GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
|
|
|
|
*
|
|
|
|
* PPTP is built on top of a modified version of GRE, and has a mandatory
|
|
|
|
* field called "CallID", which serves us for the same purpose as the key
|
|
|
|
* field in plain GRE.
|
|
|
|
*
|
|
|
|
* Documentation about PPTP can be found in RFC 2637
|
|
|
|
*
|
|
|
|
* (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
*
|
|
|
|
* Development of this code funded by Astaro AG (http://www.astaro.com/)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/in.h>
|
2008-10-08 09:35:10 +00:00
|
|
|
#include <linux/netdevice.h>
|
2006-12-03 06:09:41 +00:00
|
|
|
#include <linux/skbuff.h>
|
2008-10-08 09:35:10 +00:00
|
|
|
#include <net/dst.h>
|
|
|
|
#include <net/net_namespace.h>
|
|
|
|
#include <net/netns/generic.h>
|
2006-12-03 06:09:41 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_l4proto.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_helper.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_core.h>
|
|
|
|
#include <linux/netfilter/nf_conntrack_proto_gre.h>
|
|
|
|
#include <linux/netfilter/nf_conntrack_pptp.h>
|
|
|
|
|
|
|
|
#define GRE_TIMEOUT (30 * HZ)
|
|
|
|
#define GRE_STREAM_TIMEOUT (180 * HZ)
|
|
|
|
|
2008-10-08 09:35:10 +00:00
|
|
|
static int proto_gre_net_id;
|
|
|
|
struct netns_proto_gre {
|
|
|
|
rwlock_t keymap_lock;
|
|
|
|
struct list_head keymap_list;
|
|
|
|
};
|
2006-12-03 06:09:41 +00:00
|
|
|
|
2008-10-08 09:35:10 +00:00
|
|
|
void nf_ct_gre_keymap_flush(struct net *net)
|
2006-12-03 06:09:41 +00:00
|
|
|
{
|
2008-10-08 09:35:10 +00:00
|
|
|
struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
|
2008-09-08 01:20:36 +00:00
|
|
|
struct nf_ct_gre_keymap *km, *tmp;
|
2006-12-03 06:09:41 +00:00
|
|
|
|
2008-10-08 09:35:10 +00:00
|
|
|
write_lock_bh(&net_gre->keymap_lock);
|
|
|
|
list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) {
|
2008-09-08 01:20:36 +00:00
|
|
|
list_del(&km->list);
|
|
|
|
kfree(km);
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
2008-10-08 09:35:10 +00:00
|
|
|
write_unlock_bh(&net_gre->keymap_lock);
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_ct_gre_keymap_flush);
|
|
|
|
|
|
|
|
static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
|
|
|
|
const struct nf_conntrack_tuple *t)
|
|
|
|
{
|
|
|
|
return km->tuple.src.l3num == t->src.l3num &&
|
|
|
|
!memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
|
|
|
|
!memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
|
|
|
|
km->tuple.dst.protonum == t->dst.protonum &&
|
|
|
|
km->tuple.dst.u.all == t->dst.u.all;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look up the source key for a given tuple */
|
2008-10-08 09:35:10 +00:00
|
|
|
static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
|
2006-12-03 06:09:41 +00:00
|
|
|
{
|
2008-10-08 09:35:10 +00:00
|
|
|
struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
|
2006-12-03 06:09:41 +00:00
|
|
|
struct nf_ct_gre_keymap *km;
|
|
|
|
__be16 key = 0;
|
|
|
|
|
2008-10-08 09:35:10 +00:00
|
|
|
read_lock_bh(&net_gre->keymap_lock);
|
|
|
|
list_for_each_entry(km, &net_gre->keymap_list, list) {
|
2006-12-03 06:09:41 +00:00
|
|
|
if (gre_key_cmpfn(km, t)) {
|
|
|
|
key = km->tuple.src.u.gre.key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-08 09:35:10 +00:00
|
|
|
read_unlock_bh(&net_gre->keymap_lock);
|
2006-12-03 06:09:41 +00:00
|
|
|
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("lookup src key 0x%x for ", key);
|
2008-04-14 09:15:54 +00:00
|
|
|
nf_ct_dump_tuple(t);
|
2006-12-03 06:09:41 +00:00
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a single keymap entry, associate with specified master ct */
|
|
|
|
int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
|
|
|
|
struct nf_conntrack_tuple *t)
|
|
|
|
{
|
2008-10-08 09:35:10 +00:00
|
|
|
struct net *net = nf_ct_net(ct);
|
|
|
|
struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
|
2006-12-03 06:09:41 +00:00
|
|
|
struct nf_conn_help *help = nfct_help(ct);
|
|
|
|
struct nf_ct_gre_keymap **kmp, *km;
|
|
|
|
|
|
|
|
kmp = &help->help.ct_pptp_info.keymap[dir];
|
|
|
|
if (*kmp) {
|
|
|
|
/* check whether it's a retransmission */
|
2008-10-08 09:35:10 +00:00
|
|
|
read_lock_bh(&net_gre->keymap_lock);
|
|
|
|
list_for_each_entry(km, &net_gre->keymap_list, list) {
|
2008-09-08 01:20:08 +00:00
|
|
|
if (gre_key_cmpfn(km, t) && km == *kmp) {
|
2008-10-08 09:35:10 +00:00
|
|
|
read_unlock_bh(&net_gre->keymap_lock);
|
2006-12-03 06:09:41 +00:00
|
|
|
return 0;
|
2008-09-08 01:20:08 +00:00
|
|
|
}
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
2008-10-08 09:35:10 +00:00
|
|
|
read_unlock_bh(&net_gre->keymap_lock);
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("trying to override keymap_%s for ct %p\n",
|
|
|
|
dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
|
2006-12-03 06:09:41 +00:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
km = kmalloc(sizeof(*km), GFP_ATOMIC);
|
|
|
|
if (!km)
|
|
|
|
return -ENOMEM;
|
|
|
|
memcpy(&km->tuple, t, sizeof(*t));
|
|
|
|
*kmp = km;
|
|
|
|
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("adding new entry %p: ", km);
|
2008-04-14 09:15:54 +00:00
|
|
|
nf_ct_dump_tuple(&km->tuple);
|
2006-12-03 06:09:41 +00:00
|
|
|
|
2008-10-08 09:35:10 +00:00
|
|
|
write_lock_bh(&net_gre->keymap_lock);
|
|
|
|
list_add_tail(&km->list, &net_gre->keymap_list);
|
|
|
|
write_unlock_bh(&net_gre->keymap_lock);
|
2006-12-03 06:09:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
|
|
|
|
|
|
|
|
/* destroy the keymap entries associated with specified master ct */
|
|
|
|
void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
|
|
|
|
{
|
2008-10-08 09:35:10 +00:00
|
|
|
struct net *net = nf_ct_net(ct);
|
|
|
|
struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
|
2006-12-03 06:09:41 +00:00
|
|
|
struct nf_conn_help *help = nfct_help(ct);
|
|
|
|
enum ip_conntrack_dir dir;
|
|
|
|
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("entering for ct %p\n", ct);
|
2006-12-03 06:09:41 +00:00
|
|
|
|
2008-10-08 09:35:10 +00:00
|
|
|
write_lock_bh(&net_gre->keymap_lock);
|
2006-12-03 06:09:41 +00:00
|
|
|
for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
|
|
|
|
if (help->help.ct_pptp_info.keymap[dir]) {
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("removing %p from list\n",
|
|
|
|
help->help.ct_pptp_info.keymap[dir]);
|
2006-12-03 06:09:41 +00:00
|
|
|
list_del(&help->help.ct_pptp_info.keymap[dir]->list);
|
|
|
|
kfree(help->help.ct_pptp_info.keymap[dir]);
|
|
|
|
help->help.ct_pptp_info.keymap[dir] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-10-08 09:35:10 +00:00
|
|
|
write_unlock_bh(&net_gre->keymap_lock);
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
|
|
|
|
|
|
|
|
/* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
|
|
|
|
|
|
|
|
/* invert gre part of tuple */
|
2008-04-14 09:15:53 +00:00
|
|
|
static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple,
|
|
|
|
const struct nf_conntrack_tuple *orig)
|
2006-12-03 06:09:41 +00:00
|
|
|
{
|
|
|
|
tuple->dst.u.gre.key = orig->src.u.gre.key;
|
|
|
|
tuple->src.u.gre.key = orig->dst.u.gre.key;
|
2008-04-14 09:15:53 +00:00
|
|
|
return true;
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* gre hdr info to tuple */
|
2008-04-14 09:15:53 +00:00
|
|
|
static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
|
|
|
|
struct nf_conntrack_tuple *tuple)
|
2006-12-03 06:09:41 +00:00
|
|
|
{
|
2009-06-02 05:19:30 +00:00
|
|
|
struct net *net = dev_net(skb->dev ? skb->dev : skb_dst(skb)->dev);
|
2008-01-31 12:52:46 +00:00
|
|
|
const struct gre_hdr_pptp *pgrehdr;
|
|
|
|
struct gre_hdr_pptp _pgrehdr;
|
2006-12-03 06:09:41 +00:00
|
|
|
__be16 srckey;
|
2008-01-31 12:52:46 +00:00
|
|
|
const struct gre_hdr *grehdr;
|
|
|
|
struct gre_hdr _grehdr;
|
2006-12-03 06:09:41 +00:00
|
|
|
|
|
|
|
/* first only delinearize old RFC1701 GRE header */
|
|
|
|
grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
|
|
|
|
if (!grehdr || grehdr->version != GRE_VERSION_PPTP) {
|
|
|
|
/* try to behave like "nf_conntrack_proto_generic" */
|
|
|
|
tuple->src.u.all = 0;
|
|
|
|
tuple->dst.u.all = 0;
|
2008-04-14 09:15:53 +00:00
|
|
|
return true;
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* PPTP header is variable length, only need up to the call_id field */
|
|
|
|
pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
|
|
|
|
if (!pgrehdr)
|
2008-04-14 09:15:53 +00:00
|
|
|
return true;
|
2006-12-03 06:09:41 +00:00
|
|
|
|
|
|
|
if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("GRE_VERSION_PPTP but unknown proto\n");
|
2008-04-14 09:15:53 +00:00
|
|
|
return false;
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tuple->dst.u.gre.key = pgrehdr->call_id;
|
2008-10-08 09:35:10 +00:00
|
|
|
srckey = gre_keymap_lookup(net, tuple);
|
2006-12-03 06:09:41 +00:00
|
|
|
tuple->src.u.gre.key = srckey;
|
|
|
|
|
2008-04-14 09:15:53 +00:00
|
|
|
return true;
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* print gre part of tuple */
|
|
|
|
static int gre_print_tuple(struct seq_file *s,
|
|
|
|
const struct nf_conntrack_tuple *tuple)
|
|
|
|
{
|
|
|
|
return seq_printf(s, "srckey=0x%x dstkey=0x%x ",
|
|
|
|
ntohs(tuple->src.u.gre.key),
|
|
|
|
ntohs(tuple->dst.u.gre.key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print private data for conntrack */
|
2009-06-10 12:32:47 +00:00
|
|
|
static int gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
|
2006-12-03 06:09:41 +00:00
|
|
|
{
|
|
|
|
return seq_printf(s, "timeout=%u, stream_timeout=%u ",
|
|
|
|
(ct->proto.gre.timeout / HZ),
|
|
|
|
(ct->proto.gre.stream_timeout / HZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns verdict for packet, and may modify conntrack */
|
|
|
|
static int gre_packet(struct nf_conn *ct,
|
|
|
|
const struct sk_buff *skb,
|
|
|
|
unsigned int dataoff,
|
|
|
|
enum ip_conntrack_info ctinfo,
|
2008-10-08 09:35:00 +00:00
|
|
|
u_int8_t pf,
|
2006-12-03 06:09:41 +00:00
|
|
|
unsigned int hooknum)
|
|
|
|
{
|
|
|
|
/* If we've seen traffic both ways, this is a GRE connection.
|
|
|
|
* Extend timeout. */
|
|
|
|
if (ct->status & IPS_SEEN_REPLY) {
|
|
|
|
nf_ct_refresh_acct(ct, ctinfo, skb,
|
|
|
|
ct->proto.gre.stream_timeout);
|
|
|
|
/* Also, more likely to be important, and not a probe. */
|
|
|
|
set_bit(IPS_ASSURED_BIT, &ct->status);
|
2008-10-08 09:35:07 +00:00
|
|
|
nf_conntrack_event_cache(IPCT_STATUS, ct);
|
2006-12-03 06:09:41 +00:00
|
|
|
} else
|
|
|
|
nf_ct_refresh_acct(ct, ctinfo, skb,
|
|
|
|
ct->proto.gre.timeout);
|
|
|
|
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when a new connection for this protocol found. */
|
2008-04-14 09:15:53 +00:00
|
|
|
static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb,
|
|
|
|
unsigned int dataoff)
|
2006-12-03 06:09:41 +00:00
|
|
|
{
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug(": ");
|
2008-04-14 09:15:54 +00:00
|
|
|
nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
|
2006-12-03 06:09:41 +00:00
|
|
|
|
|
|
|
/* initialize to sane value. Ideally a conntrack helper
|
|
|
|
* (e.g. in case of pptp) is increasing them */
|
|
|
|
ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
|
|
|
|
ct->proto.gre.timeout = GRE_TIMEOUT;
|
|
|
|
|
2008-04-14 09:15:53 +00:00
|
|
|
return true;
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when a conntrack entry has already been removed from the hashes
|
|
|
|
* and is about to be deleted from memory */
|
|
|
|
static void gre_destroy(struct nf_conn *ct)
|
|
|
|
{
|
|
|
|
struct nf_conn *master = ct->master;
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug(" entering\n");
|
2006-12-03 06:09:41 +00:00
|
|
|
|
|
|
|
if (!master)
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("no master !?!\n");
|
2006-12-03 06:09:41 +00:00
|
|
|
else
|
|
|
|
nf_ct_gre_keymap_destroy(master);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* protocol helper struct */
|
2007-07-15 03:48:19 +00:00
|
|
|
static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = {
|
2006-12-03 06:09:41 +00:00
|
|
|
.l3proto = AF_INET,
|
|
|
|
.l4proto = IPPROTO_GRE,
|
|
|
|
.name = "gre",
|
|
|
|
.pkt_to_tuple = gre_pkt_to_tuple,
|
|
|
|
.invert_tuple = gre_invert_tuple,
|
|
|
|
.print_tuple = gre_print_tuple,
|
|
|
|
.print_conntrack = gre_print_conntrack,
|
|
|
|
.packet = gre_packet,
|
|
|
|
.new = gre_new,
|
|
|
|
.destroy = gre_destroy,
|
|
|
|
.me = THIS_MODULE,
|
2007-03-04 23:57:25 +00:00
|
|
|
#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
|
2007-09-28 21:37:41 +00:00
|
|
|
.tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
|
2009-03-25 20:53:39 +00:00
|
|
|
.nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
|
2007-09-28 21:37:41 +00:00
|
|
|
.nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
|
2007-09-28 21:39:55 +00:00
|
|
|
.nla_policy = nf_ct_port_nla_policy,
|
2006-12-03 06:09:41 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2008-10-08 09:35:10 +00:00
|
|
|
static int proto_gre_net_init(struct net *net)
|
|
|
|
{
|
|
|
|
struct netns_proto_gre *net_gre;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
net_gre = kmalloc(sizeof(struct netns_proto_gre), GFP_KERNEL);
|
|
|
|
if (!net_gre)
|
|
|
|
return -ENOMEM;
|
|
|
|
rwlock_init(&net_gre->keymap_lock);
|
|
|
|
INIT_LIST_HEAD(&net_gre->keymap_list);
|
|
|
|
|
|
|
|
rv = net_assign_generic(net, proto_gre_net_id, net_gre);
|
|
|
|
if (rv < 0)
|
|
|
|
kfree(net_gre);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void proto_gre_net_exit(struct net *net)
|
|
|
|
{
|
|
|
|
struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
|
|
|
|
|
|
|
|
nf_ct_gre_keymap_flush(net);
|
|
|
|
kfree(net_gre);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations proto_gre_net_ops = {
|
|
|
|
.init = proto_gre_net_init,
|
|
|
|
.exit = proto_gre_net_exit,
|
|
|
|
};
|
|
|
|
|
2006-12-03 06:09:41 +00:00
|
|
|
static int __init nf_ct_proto_gre_init(void)
|
|
|
|
{
|
2008-10-08 09:35:10 +00:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_gre4);
|
|
|
|
if (rv < 0)
|
|
|
|
return rv;
|
2008-10-31 06:55:44 +00:00
|
|
|
rv = register_pernet_gen_subsys(&proto_gre_net_id, &proto_gre_net_ops);
|
2008-10-08 09:35:10 +00:00
|
|
|
if (rv < 0)
|
|
|
|
nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4);
|
|
|
|
return rv;
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
|
2008-11-20 09:01:37 +00:00
|
|
|
static void __exit nf_ct_proto_gre_fini(void)
|
2006-12-03 06:09:41 +00:00
|
|
|
{
|
|
|
|
nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4);
|
2008-10-31 06:55:44 +00:00
|
|
|
unregister_pernet_gen_subsys(proto_gre_net_id, &proto_gre_net_ops);
|
2006-12-03 06:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(nf_ct_proto_gre_init);
|
|
|
|
module_exit(nf_ct_proto_gre_fini);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|