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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 int xt_osf_add_callback(struct sock
*ctnl
, struct sk_buff
*skb
,
66 const struct nlmsghdr
*nlh
,
67 const struct nlattr
* const osf_attrs
[])
69 struct xt_osf_user_finger
*f
;
70 struct xt_osf_finger
*kf
= NULL
, *sf
;
73 if (!osf_attrs
[OSF_ATTR_FINGER
])
76 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
79 f
= nla_data(osf_attrs
[OSF_ATTR_FINGER
]);
81 kf
= kmalloc(sizeof(struct xt_osf_finger
), GFP_KERNEL
);
85 memcpy(&kf
->finger
, f
, sizeof(struct xt_osf_user_finger
));
87 list_for_each_entry(sf
, &xt_osf_fingers
[!!f
->df
], finger_entry
) {
88 if (memcmp(&sf
->finger
, f
, sizeof(struct xt_osf_user_finger
)))
94 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
100 * We are protected by nfnl mutex.
103 list_add_tail_rcu(&kf
->finger_entry
, &xt_osf_fingers
[!!f
->df
]);
108 static int xt_osf_remove_callback(struct sock
*ctnl
, struct sk_buff
*skb
,
109 const struct nlmsghdr
*nlh
,
110 const struct nlattr
* const osf_attrs
[])
112 struct xt_osf_user_finger
*f
;
113 struct xt_osf_finger
*sf
;
116 if (!osf_attrs
[OSF_ATTR_FINGER
])
119 f
= nla_data(osf_attrs
[OSF_ATTR_FINGER
]);
121 list_for_each_entry(sf
, &xt_osf_fingers
[!!f
->df
], finger_entry
) {
122 if (memcmp(&sf
->finger
, f
, sizeof(struct xt_osf_user_finger
)))
126 * We are protected by nfnl mutex.
128 list_del_rcu(&sf
->finger_entry
);
129 kfree_rcu(sf
, rcu_head
);
138 static const struct nfnl_callback xt_osf_nfnetlink_callbacks
[OSF_MSG_MAX
] = {
140 .call
= xt_osf_add_callback
,
141 .attr_count
= OSF_ATTR_MAX
,
142 .policy
= xt_osf_policy
,
145 .call
= xt_osf_remove_callback
,
146 .attr_count
= OSF_ATTR_MAX
,
147 .policy
= xt_osf_policy
,
151 static const struct nfnetlink_subsystem xt_osf_nfnetlink
= {
153 .subsys_id
= NFNL_SUBSYS_OSF
,
154 .cb_count
= OSF_MSG_MAX
,
155 .cb
= xt_osf_nfnetlink_callbacks
,
158 static inline int xt_osf_ttl(const struct sk_buff
*skb
, const struct xt_osf_info
*info
,
161 const struct iphdr
*ip
= ip_hdr(skb
);
163 if (info
->flags
& XT_OSF_TTL
) {
164 if (info
->ttl
== XT_OSF_TTL_TRUE
)
165 return ip
->ttl
== f_ttl
;
166 if (info
->ttl
== XT_OSF_TTL_NOCHECK
)
168 else if (ip
->ttl
<= f_ttl
)
171 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
175 if (inet_ifa_match(ip
->saddr
, ifa
)) {
176 ret
= (ip
->ttl
== f_ttl
);
186 return ip
->ttl
== f_ttl
;
190 xt_osf_match_packet(const struct sk_buff
*skb
, struct xt_action_param
*p
)
192 const struct xt_osf_info
*info
= p
->matchinfo
;
193 const struct iphdr
*ip
= ip_hdr(skb
);
194 const struct tcphdr
*tcp
;
196 int fmatch
= FMATCH_WRONG
, fcount
= 0;
197 unsigned int optsize
= 0, check_WSS
= 0;
198 u16 window
, totlen
, mss
= 0;
200 const unsigned char *optp
= NULL
, *_optp
= NULL
;
201 unsigned char opts
[MAX_IPOPTLEN
];
202 const struct xt_osf_finger
*kf
;
203 const struct xt_osf_user_finger
*f
;
208 tcp
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(struct tcphdr
), &_tcph
);
215 totlen
= ntohs(ip
->tot_len
);
216 df
= ntohs(ip
->frag_off
) & IP_DF
;
217 window
= ntohs(tcp
->window
);
219 if (tcp
->doff
* 4 > sizeof(struct tcphdr
)) {
220 optsize
= tcp
->doff
* 4 - sizeof(struct tcphdr
);
222 _optp
= optp
= skb_header_pointer(skb
, ip_hdrlen(skb
) +
223 sizeof(struct tcphdr
), optsize
, opts
);
227 list_for_each_entry_rcu(kf
, &xt_osf_fingers
[df
], finger_entry
) {
230 if (!(info
->flags
& XT_OSF_LOG
) && strcmp(info
->genre
, f
->genre
))
234 fmatch
= FMATCH_WRONG
;
236 if (totlen
== f
->ss
&& xt_osf_ttl(skb
, info
, f
->ttl
)) {
237 int foptsize
, optnum
;
240 * Should not happen if userspace parser was written correctly.
242 if (f
->wss
.wc
>= OSF_WSS_MAX
)
248 for (optnum
= 0; optnum
< f
->opt_num
; ++optnum
)
249 foptsize
+= f
->opt
[optnum
].length
;
251 if (foptsize
> MAX_IPOPTLEN
||
252 optsize
> MAX_IPOPTLEN
||
256 check_WSS
= f
->wss
.wc
;
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
;
272 mss
= ntohs((__force __be16
)mss
);
281 fmatch
= FMATCH_OPT_WRONG
;
283 if (fmatch
!= FMATCH_OK
)
287 if (fmatch
!= FMATCH_OPT_WRONG
) {
288 fmatch
= FMATCH_WRONG
;
292 if (f
->wss
.val
== 0 || window
== f
->wss
.val
)
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
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
)
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))
316 if ((window
% f
->wss
.val
) == 0)
322 if (fmatch
!= FMATCH_OK
)
327 if (info
->flags
& XT_OSF_LOG
)
328 nf_log_packet(p
->family
, p
->hooknum
, skb
,
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
),
336 if ((info
->flags
& XT_OSF_LOG
) &&
337 info
->loglevel
== XT_OSF_LOGLEVEL_FIRST
)
343 if (!fcount
&& (info
->flags
& XT_OSF_LOG
))
344 nf_log_packet(p
->family
, p
->hooknum
, skb
, p
->in
, p
->out
, NULL
,
345 "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
346 &ip
->saddr
, ntohs(tcp
->source
),
347 &ip
->daddr
, ntohs(tcp
->dest
));
352 return fmatch
== FMATCH_OK
;
355 static struct xt_match xt_osf_match
= {
358 .family
= NFPROTO_IPV4
,
359 .proto
= IPPROTO_TCP
,
360 .hooks
= (1 << NF_INET_LOCAL_IN
) |
361 (1 << NF_INET_PRE_ROUTING
) |
362 (1 << NF_INET_FORWARD
),
363 .match
= xt_osf_match_packet
,
364 .matchsize
= sizeof(struct xt_osf_info
),
368 static int __init
xt_osf_init(void)
373 for (i
=0; i
<ARRAY_SIZE(xt_osf_fingers
); ++i
)
374 INIT_LIST_HEAD(&xt_osf_fingers
[i
]);
376 err
= nfnetlink_subsys_register(&xt_osf_nfnetlink
);
378 pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err
);
382 err
= xt_register_match(&xt_osf_match
);
384 pr_err("Failed to register OS fingerprint "
385 "matching module (%d)\n", err
);
392 nfnetlink_subsys_unregister(&xt_osf_nfnetlink
);
397 static void __exit
xt_osf_fini(void)
399 struct xt_osf_finger
*f
;
402 nfnetlink_subsys_unregister(&xt_osf_nfnetlink
);
403 xt_unregister_match(&xt_osf_match
);
406 for (i
=0; i
<ARRAY_SIZE(xt_osf_fingers
); ++i
) {
408 list_for_each_entry_rcu(f
, &xt_osf_fingers
[i
], finger_entry
) {
409 list_del_rcu(&f
->finger_entry
);
410 kfree_rcu(f
, rcu_head
);
418 module_init(xt_osf_init
);
419 module_exit(xt_osf_fini
);
421 MODULE_LICENSE("GPL");
422 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
423 MODULE_DESCRIPTION("Passive OS fingerprint matching.");
424 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF
);