2 * NetBIOS name service broadcast connection tracking helper
4 * (c) 2005 Patrick McHardy <kaber@trash.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 * This helper tracks locally originating NetBIOS name service
13 * requests by issuing permanent expectations (valid until
14 * timing out) matching all reply connections from the
15 * destination network. The only NetBIOS specific thing is
16 * actually the port number.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/inetdevice.h>
26 #include <linux/udp.h>
27 #include <net/route.h>
29 #include <linux/netfilter.h>
30 #include <linux/netfilter_ipv4.h>
31 #include <linux/netfilter_ipv4/ip_conntrack.h>
32 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
34 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
35 MODULE_DESCRIPTION("NetBIOS name service broadcast connection tracking helper");
36 MODULE_LICENSE("GPL");
38 static unsigned int timeout = 3;
39 module_param(timeout, int, 0600);
40 MODULE_PARM_DESC(timeout, "timeout for master connection/replies in seconds");
42 static int help(struct sk_buff **pskb,
43 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
45 struct ip_conntrack_expect *exp;
46 struct iphdr *iph = (*pskb)->nh.iph;
47 struct udphdr _uh, *uh;
48 struct rtable *rt = (struct rtable *)(*pskb)->dst;
49 struct in_device *in_dev;
52 /* we're only interested in locally generated packets */
53 if ((*pskb)->sk == NULL)
55 if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST))
57 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
61 in_dev = __in_dev_get(rt->u.dst.dev);
63 for_primary_ifa(in_dev) {
64 if (ifa->ifa_broadcast == iph->daddr) {
75 uh = skb_header_pointer(*pskb, iph->ihl * 4, sizeof(_uh), &_uh);
78 exp = ip_conntrack_expect_alloc(ct);
81 memset(&exp->tuple, 0, sizeof(exp->tuple));
82 exp->tuple.src.ip = iph->daddr & mask;
83 exp->tuple.dst.ip = iph->saddr;
84 exp->tuple.dst.u.udp.port = uh->source;
85 exp->tuple.dst.protonum = IPPROTO_UDP;
87 memset(&exp->mask, 0, sizeof(exp->mask));
88 exp->mask.src.ip = mask;
89 exp->mask.dst.ip = 0xFFFFFFFF;
90 exp->mask.dst.u.udp.port = 0xFFFF;
91 exp->mask.dst.protonum = 0xFF;
94 exp->flags = IP_CT_EXPECT_PERMANENT;
96 ip_conntrack_expect_related(exp);
97 ip_conntrack_expect_put(exp);
99 ip_ct_refresh_acct(ct, ctinfo, NULL, timeout * HZ);
104 static struct ip_conntrack_helper helper = {
105 .name = "netbios-ns",
110 .port = __constant_htons(137),
115 .protonum = IPPROTO_UDP,
135 static int __init init(void)
137 helper.timeout = timeout;
138 return ip_conntrack_helper_register(&helper);
141 static void __exit fini(void)
143 ip_conntrack_helper_unregister(&helper);