Commit | Line | Data |
---|---|---|
a2978aea PM |
1 | /* |
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> | |
8584d6df | 24 | #include <linux/if_addr.h> |
a2978aea PM |
25 | #include <linux/in.h> |
26 | #include <linux/ip.h> | |
a2978aea PM |
27 | #include <net/route.h> |
28 | ||
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> | |
33 | ||
e7fa1bd9 PM |
34 | #define NMBD_PORT 137 |
35 | ||
a2978aea PM |
36 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
37 | MODULE_DESCRIPTION("NetBIOS name service broadcast connection tracking helper"); | |
38 | MODULE_LICENSE("GPL"); | |
39 | ||
40 | static unsigned int timeout = 3; | |
e7be6994 | 41 | module_param(timeout, uint, 0400); |
a2978aea PM |
42 | MODULE_PARM_DESC(timeout, "timeout for master connection/replies in seconds"); |
43 | ||
44 | static int help(struct sk_buff **pskb, | |
e905a9ed | 45 | struct ip_conntrack *ct, enum ip_conntrack_info ctinfo) |
a2978aea PM |
46 | { |
47 | struct ip_conntrack_expect *exp; | |
eddc9ec5 | 48 | struct iphdr *iph = ip_hdr(*pskb); |
a2978aea PM |
49 | struct rtable *rt = (struct rtable *)(*pskb)->dst; |
50 | struct in_device *in_dev; | |
a144ea4b | 51 | __be32 mask = 0; |
a2978aea PM |
52 | |
53 | /* we're only interested in locally generated packets */ | |
54 | if ((*pskb)->sk == NULL) | |
55 | goto out; | |
56 | if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST)) | |
57 | goto out; | |
58 | if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) | |
59 | goto out; | |
60 | ||
61 | rcu_read_lock(); | |
e5ed6399 | 62 | in_dev = __in_dev_get_rcu(rt->u.dst.dev); |
a2978aea PM |
63 | if (in_dev != NULL) { |
64 | for_primary_ifa(in_dev) { | |
65 | if (ifa->ifa_broadcast == iph->daddr) { | |
66 | mask = ifa->ifa_mask; | |
67 | break; | |
68 | } | |
69 | } endfor_ifa(in_dev); | |
70 | } | |
71 | rcu_read_unlock(); | |
72 | ||
73 | if (mask == 0) | |
74 | goto out; | |
75 | ||
a2978aea PM |
76 | exp = ip_conntrack_expect_alloc(ct); |
77 | if (exp == NULL) | |
78 | goto out; | |
a2978aea | 79 | |
e7fa1bd9 | 80 | exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
cdcb71bf | 81 | exp->tuple.src.u.udp.port = htons(NMBD_PORT); |
e7fa1bd9 | 82 | |
a2978aea | 83 | exp->mask.src.ip = mask; |
cdcb71bf AV |
84 | exp->mask.src.u.udp.port = htons(0xFFFF); |
85 | exp->mask.dst.ip = htonl(0xFFFFFFFF); | |
86 | exp->mask.dst.u.udp.port = htons(0xFFFF); | |
a2978aea PM |
87 | exp->mask.dst.protonum = 0xFF; |
88 | ||
89 | exp->expectfn = NULL; | |
90 | exp->flags = IP_CT_EXPECT_PERMANENT; | |
91 | ||
92 | ip_conntrack_expect_related(exp); | |
93 | ip_conntrack_expect_put(exp); | |
94 | ||
1dfbab59 | 95 | ip_ct_refresh(ct, *pskb, timeout * HZ); |
a2978aea PM |
96 | out: |
97 | return NF_ACCEPT; | |
98 | } | |
99 | ||
100 | static struct ip_conntrack_helper helper = { | |
101 | .name = "netbios-ns", | |
102 | .tuple = { | |
3a934815 AM |
103 | .src = { |
104 | .u = { | |
105 | .udp = { | |
e7fa1bd9 | 106 | .port = __constant_htons(NMBD_PORT), |
3a934815 AM |
107 | } |
108 | } | |
109 | }, | |
110 | .dst = { | |
111 | .protonum = IPPROTO_UDP, | |
112 | }, | |
a2978aea PM |
113 | }, |
114 | .mask = { | |
3a934815 AM |
115 | .src = { |
116 | .u = { | |
117 | .udp = { | |
cdcb71bf | 118 | .port = __constant_htons(0xFFFF), |
3a934815 AM |
119 | } |
120 | } | |
121 | }, | |
122 | .dst = { | |
123 | .protonum = 0xFF, | |
124 | }, | |
a2978aea PM |
125 | }, |
126 | .max_expected = 1, | |
127 | .me = THIS_MODULE, | |
128 | .help = help, | |
129 | }; | |
130 | ||
65b4b4e8 | 131 | static int __init ip_conntrack_netbios_ns_init(void) |
a2978aea PM |
132 | { |
133 | helper.timeout = timeout; | |
134 | return ip_conntrack_helper_register(&helper); | |
135 | } | |
136 | ||
65b4b4e8 | 137 | static void __exit ip_conntrack_netbios_ns_fini(void) |
a2978aea PM |
138 | { |
139 | ip_conntrack_helper_unregister(&helper); | |
140 | } | |
141 | ||
65b4b4e8 AM |
142 | module_init(ip_conntrack_netbios_ns_init); |
143 | module_exit(ip_conntrack_netbios_ns_fini); |