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