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>
24 #include <linux/inetdevice.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>
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 */
49 /* Packet matches the fingerprint */
51 /* Options do not match the fingerprint, but header does */
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);
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;
79 if (!osf_attrs[OSF_ATTR_FINGER])
82 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
85 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
87 kf = kmalloc(sizeof(struct xt_osf_finger), GFP_KERNEL);
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)))
100 if (nlh->nlmsg_flags & NLM_F_EXCL)
106 * We are protected by nfnl mutex.
109 list_add_tail_rcu(&kf->finger_entry, &xt_osf_fingers[!!f->df]);
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;
121 if (!osf_attrs[OSF_ATTR_FINGER])
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)))
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);
143 static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = {
145 .call = xt_osf_add_callback,
146 .attr_count = OSF_ATTR_MAX,
147 .policy = xt_osf_policy,
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 = {
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,
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)
173 else if (ip->ttl <= f_ttl)
176 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
180 if (inet_ifa_match(ip->saddr, ifa)) {
181 ret = (ip->ttl == f_ttl);
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;
201 int fmatch = FMATCH_WRONG, fcount = 0;
202 unsigned int optsize = 0, check_WSS = 0;
203 u16 window, totlen, mss = 0;
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;
213 tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph);
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);
232 list_for_each_entry_rcu(kf, &xt_osf_fingers[df], finger_entry) {
235 if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre))
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)
253 for (optnum = 0; optnum < f->opt_num; ++optnum)
254 foptsize += f->opt[optnum].length;
256 if (foptsize > MAX_IPOPTLEN ||
257 optsize > MAX_IPOPTLEN ||
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;
286 fmatch = FMATCH_OPT_WRONG;
288 if (fmatch != FMATCH_OK)
292 if (fmatch != FMATCH_OPT_WRONG) {
293 fmatch = FMATCH_WRONG;
297 if (f->wss.val == 0 || window == f->wss.val)
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
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)
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))
321 if ((window % f->wss.val) == 0)
327 if (fmatch != FMATCH_OK)
332 if (info->flags & XT_OSF_LOG)
333 nf_log_packet(p->hooknum, 0, skb, p->in, p->out, NULL,
334 "%s [%s:%s] : %pi4:%d -> %pi4:%d hops=%d\n",
335 f->genre, f->version, f->subtype,
336 &ip->saddr, ntohs(tcp->source),
337 &ip->daddr, ntohs(tcp->dest),
340 if ((info->flags & XT_OSF_LOG) &&
341 info->loglevel == XT_OSF_LOGLEVEL_FIRST)
347 if (!fcount && (info->flags & XT_OSF_LOG))
348 nf_log_packet(p->hooknum, 0, skb, p->in, p->out, NULL,
349 "Remote OS is not known: %pi4:%u -> %pi4:%u\n",
350 &ip->saddr, ntohs(tcp->source),
351 &ip->daddr, ntohs(tcp->dest));
356 return fmatch == FMATCH_OK;
359 static struct xt_match xt_osf_match = {
362 .family = NFPROTO_IPV4,
363 .proto = IPPROTO_TCP,
364 .hooks = (1 << NF_INET_LOCAL_IN) |
365 (1 << NF_INET_PRE_ROUTING) |
366 (1 << NF_INET_FORWARD),
367 .match = xt_osf_match_packet,
368 .matchsize = sizeof(struct xt_osf_info),
372 static int __init xt_osf_init(void)
377 for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i)
378 INIT_LIST_HEAD(&xt_osf_fingers[i]);
380 err = nfnetlink_subsys_register(&xt_osf_nfnetlink);
382 printk(KERN_ERR "Failed (%d) to register OSF nsfnetlink helper.\n", err);
386 err = xt_register_match(&xt_osf_match);
388 printk(KERN_ERR "Failed (%d) to register OS fingerprint "
389 "matching module.\n", err);
396 nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
401 static void __exit xt_osf_fini(void)
403 struct xt_osf_finger *f;
406 nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
407 xt_unregister_match(&xt_osf_match);
410 for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) {
412 list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) {
413 list_del_rcu(&f->finger_entry);
414 call_rcu(&f->rcu_head, xt_osf_finger_free_rcu);
422 module_init(xt_osf_init);
423 module_exit(xt_osf_fini);
425 MODULE_LICENSE("GPL");
426 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
427 MODULE_DESCRIPTION("Passive OS fingerprint matching.");
428 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF);