blob: 08404e6755fb15064ab086d3229a34cc9139ce3d [file] [log] [blame]
Patrick McHardy92703ee2006-12-02 22:09:24 -08001/*
2 * NetBIOS name service broadcast connection tracking helper
3 *
4 * (c) 2005 Patrick McHardy <kaber@trash.net>
5 *
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.
10 */
11/*
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.
17 */
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>
24#include <linux/if_addr.h>
25#include <linux/in.h>
26#include <linux/ip.h>
Yasuyuki Kozakai1863f092006-12-02 22:12:54 -080027#include <linux/netfilter.h>
Patrick McHardy92703ee2006-12-02 22:09:24 -080028#include <net/route.h>
29
30#include <net/netfilter/nf_conntrack.h>
31#include <net/netfilter/nf_conntrack_helper.h>
32#include <net/netfilter/nf_conntrack_expect.h>
33
34#define NMBD_PORT 137
35
36MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
37MODULE_DESCRIPTION("NetBIOS name service broadcast connection tracking helper");
38MODULE_LICENSE("GPL");
39MODULE_ALIAS("ip_conntrack_netbios_ns");
40
41static unsigned int timeout __read_mostly = 3;
42module_param(timeout, uint, 0400);
43MODULE_PARM_DESC(timeout, "timeout for master connection/replies in seconds");
44
Herbert Xu3db05fe2007-10-15 00:53:15 -070045static int help(struct sk_buff *skb, unsigned int protoff,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080046 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
Patrick McHardy92703ee2006-12-02 22:09:24 -080047{
48 struct nf_conntrack_expect *exp;
Herbert Xu3db05fe2007-10-15 00:53:15 -070049 struct iphdr *iph = ip_hdr(skb);
Eric Dumazetee6b9672008-03-05 18:30:47 -080050 struct rtable *rt = skb->rtable;
Patrick McHardy92703ee2006-12-02 22:09:24 -080051 struct in_device *in_dev;
52 __be32 mask = 0;
53
54 /* we're only interested in locally generated packets */
Herbert Xu3db05fe2007-10-15 00:53:15 -070055 if (skb->sk == NULL)
Patrick McHardy92703ee2006-12-02 22:09:24 -080056 goto out;
57 if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST))
58 goto out;
59 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
60 goto out;
61
62 rcu_read_lock();
63 in_dev = __in_dev_get_rcu(rt->u.dst.dev);
64 if (in_dev != NULL) {
65 for_primary_ifa(in_dev) {
66 if (ifa->ifa_broadcast == iph->daddr) {
67 mask = ifa->ifa_mask;
68 break;
69 }
70 } endfor_ifa(in_dev);
71 }
72 rcu_read_unlock();
73
74 if (mask == 0)
75 goto out;
76
Patrick McHardy68236452007-07-07 22:30:49 -070077 exp = nf_ct_expect_alloc(ct);
Patrick McHardy92703ee2006-12-02 22:09:24 -080078 if (exp == NULL)
79 goto out;
80
81 exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
82 exp->tuple.src.u.udp.port = htons(NMBD_PORT);
83
84 exp->mask.src.u3.ip = mask;
85 exp->mask.src.u.udp.port = htons(0xFFFF);
Patrick McHardy92703ee2006-12-02 22:09:24 -080086
87 exp->expectfn = NULL;
88 exp->flags = NF_CT_EXPECT_PERMANENT;
Patrick McHardy6002f2662008-03-25 20:09:15 -070089 exp->class = NF_CT_EXPECT_CLASS_DEFAULT;
Patrick McHardy88044c82007-01-09 00:03:26 -080090 exp->helper = NULL;
Patrick McHardy92703ee2006-12-02 22:09:24 -080091
Patrick McHardy68236452007-07-07 22:30:49 -070092 nf_ct_expect_related(exp);
93 nf_ct_expect_put(exp);
Patrick McHardy92703ee2006-12-02 22:09:24 -080094
Herbert Xu3db05fe2007-10-15 00:53:15 -070095 nf_ct_refresh(ct, skb, timeout * HZ);
Patrick McHardy92703ee2006-12-02 22:09:24 -080096out:
97 return NF_ACCEPT;
98}
99
Patrick McHardy6002f2662008-03-25 20:09:15 -0700100static struct nf_conntrack_expect_policy exp_policy = {
101 .max_expected = 1,
102};
103
Patrick McHardy92703ee2006-12-02 22:09:24 -0800104static struct nf_conntrack_helper helper __read_mostly = {
105 .name = "netbios-ns",
106 .tuple.src.l3num = AF_INET,
107 .tuple.src.u.udp.port = __constant_htons(NMBD_PORT),
108 .tuple.dst.protonum = IPPROTO_UDP,
Patrick McHardy92703ee2006-12-02 22:09:24 -0800109 .me = THIS_MODULE,
110 .help = help,
Patrick McHardy6002f2662008-03-25 20:09:15 -0700111 .expect_policy = &exp_policy,
Patrick McHardy92703ee2006-12-02 22:09:24 -0800112};
113
114static int __init nf_conntrack_netbios_ns_init(void)
115{
Patrick McHardy6002f2662008-03-25 20:09:15 -0700116 exp_policy.timeout = timeout;
Patrick McHardy92703ee2006-12-02 22:09:24 -0800117 return nf_conntrack_helper_register(&helper);
118}
119
120static void __exit nf_conntrack_netbios_ns_fini(void)
121{
122 nf_conntrack_helper_unregister(&helper);
123}
124
125module_init(nf_conntrack_netbios_ns_init);
126module_exit(nf_conntrack_netbios_ns_fini);