Root/
1 | /* |
2 | * Copyright (c) 2003+ Evgeniy Polyakov <zbr@ioremap.net> |
3 | * |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
19 | #include <linux/module.h> |
20 | #include <linux/kernel.h> |
21 | |
22 | #include <linux/if.h> |
23 | #include <linux/inetdevice.h> |
24 | #include <linux/ip.h> |
25 | #include <linux/list.h> |
26 | #include <linux/rculist.h> |
27 | #include <linux/skbuff.h> |
28 | #include <linux/slab.h> |
29 | #include <linux/tcp.h> |
30 | |
31 | #include <net/ip.h> |
32 | #include <net/tcp.h> |
33 | |
34 | #include <linux/netfilter/nfnetlink.h> |
35 | #include <linux/netfilter/x_tables.h> |
36 | #include <net/netfilter/nf_log.h> |
37 | #include <linux/netfilter/xt_osf.h> |
38 | |
39 | struct xt_osf_finger { |
40 | struct rcu_head rcu_head; |
41 | struct list_head finger_entry; |
42 | struct xt_osf_user_finger finger; |
43 | }; |
44 | |
45 | enum osf_fmatch_states { |
46 | /* Packet does not match the fingerprint */ |
47 | FMATCH_WRONG = 0, |
48 | /* Packet matches the fingerprint */ |
49 | FMATCH_OK, |
50 | /* Options do not match the fingerprint, but header does */ |
51 | FMATCH_OPT_WRONG, |
52 | }; |
53 | |
54 | /* |
55 | * Indexed by dont-fragment bit. |
56 | * It is the only constant value in the fingerprint. |
57 | */ |
58 | static struct list_head xt_osf_fingers[2]; |
59 | |
60 | static const struct nla_policy xt_osf_policy[OSF_ATTR_MAX + 1] = { |
61 | [OSF_ATTR_FINGER] = { .len = sizeof(struct xt_osf_user_finger) }, |
62 | }; |
63 | |
64 | static int xt_osf_add_callback(struct sock *ctnl, struct sk_buff *skb, |
65 | const struct nlmsghdr *nlh, |
66 | const struct nlattr * const osf_attrs[]) |
67 | { |
68 | struct xt_osf_user_finger *f; |
69 | struct xt_osf_finger *kf = NULL, *sf; |
70 | int err = 0; |
71 | |
72 | if (!osf_attrs[OSF_ATTR_FINGER]) |
73 | return -EINVAL; |
74 | |
75 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) |
76 | return -EINVAL; |
77 | |
78 | f = nla_data(osf_attrs[OSF_ATTR_FINGER]); |
79 | |
80 | kf = kmalloc(sizeof(struct xt_osf_finger), GFP_KERNEL); |
81 | if (!kf) |
82 | return -ENOMEM; |
83 | |
84 | memcpy(&kf->finger, f, sizeof(struct xt_osf_user_finger)); |
85 | |
86 | list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) { |
87 | if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger))) |
88 | continue; |
89 | |
90 | kfree(kf); |
91 | kf = NULL; |
92 | |
93 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
94 | err = -EEXIST; |
95 | break; |
96 | } |
97 | |
98 | /* |
99 | * We are protected by nfnl mutex. |
100 | */ |
101 | if (kf) |
102 | list_add_tail_rcu(&kf->finger_entry, &xt_osf_fingers[!!f->df]); |
103 | |
104 | return err; |
105 | } |
106 | |
107 | static int xt_osf_remove_callback(struct sock *ctnl, struct sk_buff *skb, |
108 | const struct nlmsghdr *nlh, |
109 | const struct nlattr * const osf_attrs[]) |
110 | { |
111 | struct xt_osf_user_finger *f; |
112 | struct xt_osf_finger *sf; |
113 | int err = -ENOENT; |
114 | |
115 | if (!osf_attrs[OSF_ATTR_FINGER]) |
116 | return -EINVAL; |
117 | |
118 | f = nla_data(osf_attrs[OSF_ATTR_FINGER]); |
119 | |
120 | list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) { |
121 | if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger))) |
122 | continue; |
123 | |
124 | /* |
125 | * We are protected by nfnl mutex. |
126 | */ |
127 | list_del_rcu(&sf->finger_entry); |
128 | kfree_rcu(sf, rcu_head); |
129 | |
130 | err = 0; |
131 | break; |
132 | } |
133 | |
134 | return err; |
135 | } |
136 | |
137 | static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = { |
138 | [OSF_MSG_ADD] = { |
139 | .call = xt_osf_add_callback, |
140 | .attr_count = OSF_ATTR_MAX, |
141 | .policy = xt_osf_policy, |
142 | }, |
143 | [OSF_MSG_REMOVE] = { |
144 | .call = xt_osf_remove_callback, |
145 | .attr_count = OSF_ATTR_MAX, |
146 | .policy = xt_osf_policy, |
147 | }, |
148 | }; |
149 | |
150 | static const struct nfnetlink_subsystem xt_osf_nfnetlink = { |
151 | .name = "osf", |
152 | .subsys_id = NFNL_SUBSYS_OSF, |
153 | .cb_count = OSF_MSG_MAX, |
154 | .cb = xt_osf_nfnetlink_callbacks, |
155 | }; |
156 | |
157 | static inline int xt_osf_ttl(const struct sk_buff *skb, const struct xt_osf_info *info, |
158 | unsigned char f_ttl) |
159 | { |
160 | const struct iphdr *ip = ip_hdr(skb); |
161 | |
162 | if (info->flags & XT_OSF_TTL) { |
163 | if (info->ttl == XT_OSF_TTL_TRUE) |
164 | return ip->ttl == f_ttl; |
165 | if (info->ttl == XT_OSF_TTL_NOCHECK) |
166 | return 1; |
167 | else if (ip->ttl <= f_ttl) |
168 | return 1; |
169 | else { |
170 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); |
171 | int ret = 0; |
172 | |
173 | for_ifa(in_dev) { |
174 | if (inet_ifa_match(ip->saddr, ifa)) { |
175 | ret = (ip->ttl == f_ttl); |
176 | break; |
177 | } |
178 | } |
179 | endfor_ifa(in_dev); |
180 | |
181 | return ret; |
182 | } |
183 | } |
184 | |
185 | return ip->ttl == f_ttl; |
186 | } |
187 | |
188 | static bool |
189 | xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p) |
190 | { |
191 | const struct xt_osf_info *info = p->matchinfo; |
192 | const struct iphdr *ip = ip_hdr(skb); |
193 | const struct tcphdr *tcp; |
194 | struct tcphdr _tcph; |
195 | int fmatch = FMATCH_WRONG, fcount = 0; |
196 | unsigned int optsize = 0, check_WSS = 0; |
197 | u16 window, totlen, mss = 0; |
198 | bool df; |
199 | const unsigned char *optp = NULL, *_optp = NULL; |
200 | unsigned char opts[MAX_IPOPTLEN]; |
201 | const struct xt_osf_finger *kf; |
202 | const struct xt_osf_user_finger *f; |
203 | struct net *net = dev_net(p->in ? p->in : p->out); |
204 | |
205 | if (!info) |
206 | return false; |
207 | |
208 | tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph); |
209 | if (!tcp) |
210 | return false; |
211 | |
212 | if (!tcp->syn) |
213 | return false; |
214 | |
215 | totlen = ntohs(ip->tot_len); |
216 | df = ntohs(ip->frag_off) & IP_DF; |
217 | window = ntohs(tcp->window); |
218 | |
219 | if (tcp->doff * 4 > sizeof(struct tcphdr)) { |
220 | optsize = tcp->doff * 4 - sizeof(struct tcphdr); |
221 | |
222 | _optp = optp = skb_header_pointer(skb, ip_hdrlen(skb) + |
223 | sizeof(struct tcphdr), optsize, opts); |
224 | } |
225 | |
226 | rcu_read_lock(); |
227 | list_for_each_entry_rcu(kf, &xt_osf_fingers[df], finger_entry) { |
228 | f = &kf->finger; |
229 | |
230 | if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre)) |
231 | continue; |
232 | |
233 | optp = _optp; |
234 | fmatch = FMATCH_WRONG; |
235 | |
236 | if (totlen == f->ss && xt_osf_ttl(skb, info, f->ttl)) { |
237 | int foptsize, optnum; |
238 | |
239 | /* |
240 | * Should not happen if userspace parser was written correctly. |
241 | */ |
242 | if (f->wss.wc >= OSF_WSS_MAX) |
243 | continue; |
244 | |
245 | /* Check options */ |
246 | |
247 | foptsize = 0; |
248 | for (optnum = 0; optnum < f->opt_num; ++optnum) |
249 | foptsize += f->opt[optnum].length; |
250 | |
251 | if (foptsize > MAX_IPOPTLEN || |
252 | optsize > MAX_IPOPTLEN || |
253 | optsize != foptsize) |
254 | continue; |
255 | |
256 | check_WSS = f->wss.wc; |
257 | |
258 | for (optnum = 0; optnum < f->opt_num; ++optnum) { |
259 | if (f->opt[optnum].kind == (*optp)) { |
260 | __u32 len = f->opt[optnum].length; |
261 | const __u8 *optend = optp + len; |
262 | int loop_cont = 0; |
263 | |
264 | fmatch = FMATCH_OK; |
265 | |
266 | switch (*optp) { |
267 | case OSFOPT_MSS: |
268 | mss = optp[3]; |
269 | mss <<= 8; |
270 | mss |= optp[2]; |
271 | |
272 | mss = ntohs((__force __be16)mss); |
273 | break; |
274 | case OSFOPT_TS: |
275 | loop_cont = 1; |
276 | break; |
277 | } |
278 | |
279 | optp = optend; |
280 | } else |
281 | fmatch = FMATCH_OPT_WRONG; |
282 | |
283 | if (fmatch != FMATCH_OK) |
284 | break; |
285 | } |
286 | |
287 | if (fmatch != FMATCH_OPT_WRONG) { |
288 | fmatch = FMATCH_WRONG; |
289 | |
290 | switch (check_WSS) { |
291 | case OSF_WSS_PLAIN: |
292 | if (f->wss.val == 0 || window == f->wss.val) |
293 | fmatch = FMATCH_OK; |
294 | break; |
295 | case OSF_WSS_MSS: |
296 | /* |
297 | * Some smart modems decrease mangle MSS to |
298 | * SMART_MSS_2, so we check standard, decreased |
299 | * and the one provided in the fingerprint MSS |
300 | * values. |
301 | */ |
302 | #define SMART_MSS_1 1460 |
303 | #define SMART_MSS_2 1448 |
304 | if (window == f->wss.val * mss || |
305 | window == f->wss.val * SMART_MSS_1 || |
306 | window == f->wss.val * SMART_MSS_2) |
307 | fmatch = FMATCH_OK; |
308 | break; |
309 | case OSF_WSS_MTU: |
310 | if (window == f->wss.val * (mss + 40) || |
311 | window == f->wss.val * (SMART_MSS_1 + 40) || |
312 | window == f->wss.val * (SMART_MSS_2 + 40)) |
313 | fmatch = FMATCH_OK; |
314 | break; |
315 | case OSF_WSS_MODULO: |
316 | if ((window % f->wss.val) == 0) |
317 | fmatch = FMATCH_OK; |
318 | break; |
319 | } |
320 | } |
321 | |
322 | if (fmatch != FMATCH_OK) |
323 | continue; |
324 | |
325 | fcount++; |
326 | |
327 | if (info->flags & XT_OSF_LOG) |
328 | nf_log_packet(net, p->family, p->hooknum, skb, |
329 | p->in, p->out, NULL, |
330 | "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n", |
331 | f->genre, f->version, f->subtype, |
332 | &ip->saddr, ntohs(tcp->source), |
333 | &ip->daddr, ntohs(tcp->dest), |
334 | f->ttl - ip->ttl); |
335 | |
336 | if ((info->flags & XT_OSF_LOG) && |
337 | info->loglevel == XT_OSF_LOGLEVEL_FIRST) |
338 | break; |
339 | } |
340 | } |
341 | rcu_read_unlock(); |
342 | |
343 | if (!fcount && (info->flags & XT_OSF_LOG)) |
344 | nf_log_packet(net, p->family, p->hooknum, skb, p->in, |
345 | p->out, NULL, |
346 | "Remote OS is not known: %pI4:%u -> %pI4:%u\n", |
347 | &ip->saddr, ntohs(tcp->source), |
348 | &ip->daddr, ntohs(tcp->dest)); |
349 | |
350 | if (fcount) |
351 | fmatch = FMATCH_OK; |
352 | |
353 | return fmatch == FMATCH_OK; |
354 | } |
355 | |
356 | static struct xt_match xt_osf_match = { |
357 | .name = "osf", |
358 | .revision = 0, |
359 | .family = NFPROTO_IPV4, |
360 | .proto = IPPROTO_TCP, |
361 | .hooks = (1 << NF_INET_LOCAL_IN) | |
362 | (1 << NF_INET_PRE_ROUTING) | |
363 | (1 << NF_INET_FORWARD), |
364 | .match = xt_osf_match_packet, |
365 | .matchsize = sizeof(struct xt_osf_info), |
366 | .me = THIS_MODULE, |
367 | }; |
368 | |
369 | static int __init xt_osf_init(void) |
370 | { |
371 | int err = -EINVAL; |
372 | int i; |
373 | |
374 | for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) |
375 | INIT_LIST_HEAD(&xt_osf_fingers[i]); |
376 | |
377 | err = nfnetlink_subsys_register(&xt_osf_nfnetlink); |
378 | if (err < 0) { |
379 | pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err); |
380 | goto err_out_exit; |
381 | } |
382 | |
383 | err = xt_register_match(&xt_osf_match); |
384 | if (err) { |
385 | pr_err("Failed to register OS fingerprint " |
386 | "matching module (%d)\n", err); |
387 | goto err_out_remove; |
388 | } |
389 | |
390 | return 0; |
391 | |
392 | err_out_remove: |
393 | nfnetlink_subsys_unregister(&xt_osf_nfnetlink); |
394 | err_out_exit: |
395 | return err; |
396 | } |
397 | |
398 | static void __exit xt_osf_fini(void) |
399 | { |
400 | struct xt_osf_finger *f; |
401 | int i; |
402 | |
403 | nfnetlink_subsys_unregister(&xt_osf_nfnetlink); |
404 | xt_unregister_match(&xt_osf_match); |
405 | |
406 | rcu_read_lock(); |
407 | for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) { |
408 | |
409 | list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) { |
410 | list_del_rcu(&f->finger_entry); |
411 | kfree_rcu(f, rcu_head); |
412 | } |
413 | } |
414 | rcu_read_unlock(); |
415 | |
416 | rcu_barrier(); |
417 | } |
418 | |
419 | module_init(xt_osf_init); |
420 | module_exit(xt_osf_fini); |
421 | |
422 | MODULE_LICENSE("GPL"); |
423 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); |
424 | MODULE_DESCRIPTION("Passive OS fingerprint matching."); |
425 | MODULE_ALIAS("ipt_osf"); |
426 | MODULE_ALIAS("ip6t_osf"); |
427 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF); |
428 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9