ip_conntrack_netbios_ns.c 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/*
 *      NetBIOS name service broadcast connection tracking helper
 *
 *      (c) 2005 Patrick McHardy <kaber@trash.net>
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */
/*
 *      This helper tracks locally originating NetBIOS name service
 *      requests by issuing permanent expectations (valid until
 *      timing out) matching all reply connections from the
 *      destination network. The only NetBIOS specific thing is
 *      actually the port number.
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/inetdevice.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <net/route.h>

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv4/ip_conntrack.h>
#include <linux/netfilter_ipv4/ip_conntrack_helper.h>

MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
MODULE_DESCRIPTION("NetBIOS name service broadcast connection tracking helper");
MODULE_LICENSE("GPL");

static unsigned int timeout = 3;
module_param(timeout, int, 0600);
MODULE_PARM_DESC(timeout, "timeout for master connection/replies in seconds");

static int help(struct sk_buff **pskb,
                struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
{
	struct ip_conntrack_expect *exp;
	struct iphdr *iph = (*pskb)->nh.iph;
	struct udphdr _uh, *uh;
	struct rtable *rt = (struct rtable *)(*pskb)->dst;
	struct in_device *in_dev;
	u_int32_t mask = 0;

	/* we're only interested in locally generated packets */
	if ((*pskb)->sk == NULL)
		goto out;
	if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST))
		goto out;
	if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
		goto out;

	rcu_read_lock();
	in_dev = __in_dev_get(rt->u.dst.dev);
	if (in_dev != NULL) {
		for_primary_ifa(in_dev) {
			if (ifa->ifa_broadcast == iph->daddr) {
				mask = ifa->ifa_mask;
				break;
			}
		} endfor_ifa(in_dev);
	}
	rcu_read_unlock();

	if (mask == 0)
		goto out;

	uh = skb_header_pointer(*pskb, iph->ihl * 4, sizeof(_uh), &_uh);
	BUG_ON(uh == NULL);

	exp = ip_conntrack_expect_alloc(ct);
	if (exp == NULL)
		goto out;
	memset(&exp->tuple, 0, sizeof(exp->tuple));
	exp->tuple.src.ip         = iph->daddr & mask;
	exp->tuple.dst.ip         = iph->saddr;
	exp->tuple.dst.u.udp.port = uh->source;
	exp->tuple.dst.protonum   = IPPROTO_UDP;

	memset(&exp->mask, 0, sizeof(exp->mask));
	exp->mask.src.ip          = mask;
	exp->mask.dst.ip          = 0xFFFFFFFF;
	exp->mask.dst.u.udp.port  = 0xFFFF;
	exp->mask.dst.protonum    = 0xFF;

	exp->expectfn             = NULL;
	exp->flags                = IP_CT_EXPECT_PERMANENT;

	ip_conntrack_expect_related(exp);
	ip_conntrack_expect_put(exp);

	ip_ct_refresh_acct(ct, ctinfo, NULL, timeout * HZ);
out:
	return NF_ACCEPT;
}

static struct ip_conntrack_helper helper = {
	.name			= "netbios-ns",
	.tuple = {
		.src.u.udp.port	= __constant_htons(137),
		.dst.protonum	= IPPROTO_UDP,
	},
	.mask = {
		.src.u.udp.port	= 0xFFFF,
		.dst.protonum	= 0xFF,
	},
	.max_expected		= 1,
	.me			= THIS_MODULE,
	.help			= help,
};

static int __init init(void)
{
	helper.timeout = timeout;
	return ip_conntrack_helper_register(&helper);
}

static void __exit fini(void)
{
	ip_conntrack_helper_unregister(&helper);
}

module_init(init);
module_exit(fini);