davinci: audio clocks: use struct device instead of clock names
[linux-ginger.git] / net / netfilter / xt_osf.c
blob0f482e2440b432434ac96bbf38a942340ef56350
1 /*
2 * Copyright (c) 2003+ Evgeniy Polyakov <zbr@ioremap.net>
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/kernel.h>
23 #include <linux/if.h>
24 #include <linux/inetdevice.h>
25 #include <linux/ip.h>
26 #include <linux/list.h>
27 #include <linux/rculist.h>
28 #include <linux/skbuff.h>
29 #include <linux/slab.h>
30 #include <linux/tcp.h>
32 #include <net/ip.h>
33 #include <net/tcp.h>
35 #include <linux/netfilter/nfnetlink.h>
36 #include <linux/netfilter/x_tables.h>
37 #include <net/netfilter/nf_log.h>
38 #include <linux/netfilter/xt_osf.h>
40 struct xt_osf_finger {
41 struct rcu_head rcu_head;
42 struct list_head finger_entry;
43 struct xt_osf_user_finger finger;
46 enum osf_fmatch_states {
47 /* Packet does not match the fingerprint */
48 FMATCH_WRONG = 0,
49 /* Packet matches the fingerprint */
50 FMATCH_OK,
51 /* Options do not match the fingerprint, but header does */
52 FMATCH_OPT_WRONG,
56 * Indexed by dont-fragment bit.
57 * It is the only constant value in the fingerprint.
59 static struct list_head xt_osf_fingers[2];
61 static const struct nla_policy xt_osf_policy[OSF_ATTR_MAX + 1] = {
62 [OSF_ATTR_FINGER] = { .len = sizeof(struct xt_osf_user_finger) },
65 static void xt_osf_finger_free_rcu(struct rcu_head *rcu_head)
67 struct xt_osf_finger *f = container_of(rcu_head, struct xt_osf_finger, rcu_head);
69 kfree(f);
72 static int xt_osf_add_callback(struct sock *ctnl, struct sk_buff *skb,
73 struct nlmsghdr *nlh, struct nlattr *osf_attrs[])
75 struct xt_osf_user_finger *f;
76 struct xt_osf_finger *kf = NULL, *sf;
77 int err = 0;
79 if (!osf_attrs[OSF_ATTR_FINGER])
80 return -EINVAL;
82 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
83 return -EINVAL;
85 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
87 kf = kmalloc(sizeof(struct xt_osf_finger), GFP_KERNEL);
88 if (!kf)
89 return -ENOMEM;
91 memcpy(&kf->finger, f, sizeof(struct xt_osf_user_finger));
93 list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
94 if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
95 continue;
97 kfree(kf);
98 kf = NULL;
100 if (nlh->nlmsg_flags & NLM_F_EXCL)
101 err = -EEXIST;
102 break;
106 * We are protected by nfnl mutex.
108 if (kf)
109 list_add_tail_rcu(&kf->finger_entry, &xt_osf_fingers[!!f->df]);
111 return err;
114 static int xt_osf_remove_callback(struct sock *ctnl, struct sk_buff *skb,
115 struct nlmsghdr *nlh, struct nlattr *osf_attrs[])
117 struct xt_osf_user_finger *f;
118 struct xt_osf_finger *sf;
119 int err = ENOENT;
121 if (!osf_attrs[OSF_ATTR_FINGER])
122 return -EINVAL;
124 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
126 list_for_each_entry(sf, &xt_osf_fingers[!!f->df], finger_entry) {
127 if (memcmp(&sf->finger, f, sizeof(struct xt_osf_user_finger)))
128 continue;
131 * We are protected by nfnl mutex.
133 list_del_rcu(&sf->finger_entry);
134 call_rcu(&sf->rcu_head, xt_osf_finger_free_rcu);
136 err = 0;
137 break;
140 return err;
143 static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = {
144 [OSF_MSG_ADD] = {
145 .call = xt_osf_add_callback,
146 .attr_count = OSF_ATTR_MAX,
147 .policy = xt_osf_policy,
149 [OSF_MSG_REMOVE] = {
150 .call = xt_osf_remove_callback,
151 .attr_count = OSF_ATTR_MAX,
152 .policy = xt_osf_policy,
156 static const struct nfnetlink_subsystem xt_osf_nfnetlink = {
157 .name = "osf",
158 .subsys_id = NFNL_SUBSYS_OSF,
159 .cb_count = OSF_MSG_MAX,
160 .cb = xt_osf_nfnetlink_callbacks,
163 static inline int xt_osf_ttl(const struct sk_buff *skb, const struct xt_osf_info *info,
164 unsigned char f_ttl)
166 const struct iphdr *ip = ip_hdr(skb);
168 if (info->flags & XT_OSF_TTL) {
169 if (info->ttl == XT_OSF_TTL_TRUE)
170 return ip->ttl == f_ttl;
171 if (info->ttl == XT_OSF_TTL_NOCHECK)
172 return 1;
173 else if (ip->ttl <= f_ttl)
174 return 1;
175 else {
176 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
177 int ret = 0;
179 for_ifa(in_dev) {
180 if (inet_ifa_match(ip->saddr, ifa)) {
181 ret = (ip->ttl == f_ttl);
182 break;
185 endfor_ifa(in_dev);
187 return ret;
191 return ip->ttl == f_ttl;
194 static bool xt_osf_match_packet(const struct sk_buff *skb,
195 const struct xt_match_param *p)
197 const struct xt_osf_info *info = p->matchinfo;
198 const struct iphdr *ip = ip_hdr(skb);
199 const struct tcphdr *tcp;
200 struct tcphdr _tcph;
201 int fmatch = FMATCH_WRONG, fcount = 0;
202 unsigned int optsize = 0, check_WSS = 0;
203 u16 window, totlen, mss = 0;
204 bool df;
205 const unsigned char *optp = NULL, *_optp = NULL;
206 unsigned char opts[MAX_IPOPTLEN];
207 const struct xt_osf_finger *kf;
208 const struct xt_osf_user_finger *f;
210 if (!info)
211 return false;
213 tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph);
214 if (!tcp)
215 return false;
217 if (!tcp->syn)
218 return false;
220 totlen = ntohs(ip->tot_len);
221 df = ntohs(ip->frag_off) & IP_DF;
222 window = ntohs(tcp->window);
224 if (tcp->doff * 4 > sizeof(struct tcphdr)) {
225 optsize = tcp->doff * 4 - sizeof(struct tcphdr);
227 _optp = optp = skb_header_pointer(skb, ip_hdrlen(skb) +
228 sizeof(struct tcphdr), optsize, opts);
231 rcu_read_lock();
232 list_for_each_entry_rcu(kf, &xt_osf_fingers[df], finger_entry) {
233 f = &kf->finger;
235 if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre))
236 continue;
238 optp = _optp;
239 fmatch = FMATCH_WRONG;
241 if (totlen == f->ss && xt_osf_ttl(skb, info, f->ttl)) {
242 int foptsize, optnum;
245 * Should not happen if userspace parser was written correctly.
247 if (f->wss.wc >= OSF_WSS_MAX)
248 continue;
250 /* Check options */
252 foptsize = 0;
253 for (optnum = 0; optnum < f->opt_num; ++optnum)
254 foptsize += f->opt[optnum].length;
256 if (foptsize > MAX_IPOPTLEN ||
257 optsize > MAX_IPOPTLEN ||
258 optsize != foptsize)
259 continue;
261 check_WSS = f->wss.wc;
263 for (optnum = 0; optnum < f->opt_num; ++optnum) {
264 if (f->opt[optnum].kind == (*optp)) {
265 __u32 len = f->opt[optnum].length;
266 const __u8 *optend = optp + len;
267 int loop_cont = 0;
269 fmatch = FMATCH_OK;
271 switch (*optp) {
272 case OSFOPT_MSS:
273 mss = optp[3];
274 mss <<= 8;
275 mss |= optp[2];
277 mss = ntohs(mss);
278 break;
279 case OSFOPT_TS:
280 loop_cont = 1;
281 break;
284 optp = optend;
285 } else
286 fmatch = FMATCH_OPT_WRONG;
288 if (fmatch != FMATCH_OK)
289 break;
292 if (fmatch != FMATCH_OPT_WRONG) {
293 fmatch = FMATCH_WRONG;
295 switch (check_WSS) {
296 case OSF_WSS_PLAIN:
297 if (f->wss.val == 0 || window == f->wss.val)
298 fmatch = FMATCH_OK;
299 break;
300 case OSF_WSS_MSS:
302 * Some smart modems decrease mangle MSS to
303 * SMART_MSS_2, so we check standard, decreased
304 * and the one provided in the fingerprint MSS
305 * values.
307 #define SMART_MSS_1 1460
308 #define SMART_MSS_2 1448
309 if (window == f->wss.val * mss ||
310 window == f->wss.val * SMART_MSS_1 ||
311 window == f->wss.val * SMART_MSS_2)
312 fmatch = FMATCH_OK;
313 break;
314 case OSF_WSS_MTU:
315 if (window == f->wss.val * (mss + 40) ||
316 window == f->wss.val * (SMART_MSS_1 + 40) ||
317 window == f->wss.val * (SMART_MSS_2 + 40))
318 fmatch = FMATCH_OK;
319 break;
320 case OSF_WSS_MODULO:
321 if ((window % f->wss.val) == 0)
322 fmatch = FMATCH_OK;
323 break;
327 if (fmatch != FMATCH_OK)
328 continue;
330 fcount++;
332 if (info->flags & XT_OSF_LOG)
333 nf_log_packet(p->family, p->hooknum, skb,
334 p->in, p->out, NULL,
335 "%s [%s:%s] : %pi4:%d -> %pi4:%d hops=%d\n",
336 f->genre, f->version, f->subtype,
337 &ip->saddr, ntohs(tcp->source),
338 &ip->daddr, ntohs(tcp->dest),
339 f->ttl - ip->ttl);
341 if ((info->flags & XT_OSF_LOG) &&
342 info->loglevel == XT_OSF_LOGLEVEL_FIRST)
343 break;
346 rcu_read_unlock();
348 if (!fcount && (info->flags & XT_OSF_LOG))
349 nf_log_packet(p->family, p->hooknum, skb, p->in, p->out, NULL,
350 "Remote OS is not known: %pi4:%u -> %pi4:%u\n",
351 &ip->saddr, ntohs(tcp->source),
352 &ip->daddr, ntohs(tcp->dest));
354 if (fcount)
355 fmatch = FMATCH_OK;
357 return fmatch == FMATCH_OK;
360 static struct xt_match xt_osf_match = {
361 .name = "osf",
362 .revision = 0,
363 .family = NFPROTO_IPV4,
364 .proto = IPPROTO_TCP,
365 .hooks = (1 << NF_INET_LOCAL_IN) |
366 (1 << NF_INET_PRE_ROUTING) |
367 (1 << NF_INET_FORWARD),
368 .match = xt_osf_match_packet,
369 .matchsize = sizeof(struct xt_osf_info),
370 .me = THIS_MODULE,
373 static int __init xt_osf_init(void)
375 int err = -EINVAL;
376 int i;
378 for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i)
379 INIT_LIST_HEAD(&xt_osf_fingers[i]);
381 err = nfnetlink_subsys_register(&xt_osf_nfnetlink);
382 if (err < 0) {
383 printk(KERN_ERR "Failed (%d) to register OSF nsfnetlink helper.\n", err);
384 goto err_out_exit;
387 err = xt_register_match(&xt_osf_match);
388 if (err) {
389 printk(KERN_ERR "Failed (%d) to register OS fingerprint "
390 "matching module.\n", err);
391 goto err_out_remove;
394 return 0;
396 err_out_remove:
397 nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
398 err_out_exit:
399 return err;
402 static void __exit xt_osf_fini(void)
404 struct xt_osf_finger *f;
405 int i;
407 nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
408 xt_unregister_match(&xt_osf_match);
410 rcu_read_lock();
411 for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) {
413 list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) {
414 list_del_rcu(&f->finger_entry);
415 call_rcu(&f->rcu_head, xt_osf_finger_free_rcu);
418 rcu_read_unlock();
420 rcu_barrier();
423 module_init(xt_osf_init);
424 module_exit(xt_osf_fini);
426 MODULE_LICENSE("GPL");
427 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
428 MODULE_DESCRIPTION("Passive OS fingerprint matching.");
429 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF);