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, see <http://www.gnu.org/licenses/>.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/kernel.h>
22 #include <linux/capability.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 net
*net
, struct sock
*ctnl
,
66 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
67 const struct nlattr
* const osf_attrs
[],
68 struct netlink_ext_ack
*extack
)
70 struct xt_osf_user_finger
*f
;
71 struct xt_osf_finger
*kf
= NULL
, *sf
;
74 if (!capable(CAP_NET_ADMIN
))
77 if (!osf_attrs
[OSF_ATTR_FINGER
])
80 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
83 f
= nla_data(osf_attrs
[OSF_ATTR_FINGER
]);
85 kf
= kmalloc(sizeof(struct xt_osf_finger
), GFP_KERNEL
);
89 memcpy(&kf
->finger
, f
, sizeof(struct xt_osf_user_finger
));
91 list_for_each_entry(sf
, &xt_osf_fingers
[!!f
->df
], finger_entry
) {
92 if (memcmp(&sf
->finger
, f
, sizeof(struct xt_osf_user_finger
)))
98 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
104 * We are protected by nfnl mutex.
107 list_add_tail_rcu(&kf
->finger_entry
, &xt_osf_fingers
[!!f
->df
]);
112 static int xt_osf_remove_callback(struct net
*net
, struct sock
*ctnl
,
114 const struct nlmsghdr
*nlh
,
115 const struct nlattr
* const osf_attrs
[],
116 struct netlink_ext_ack
*extack
)
118 struct xt_osf_user_finger
*f
;
119 struct xt_osf_finger
*sf
;
122 if (!capable(CAP_NET_ADMIN
))
125 if (!osf_attrs
[OSF_ATTR_FINGER
])
128 f
= nla_data(osf_attrs
[OSF_ATTR_FINGER
]);
130 list_for_each_entry(sf
, &xt_osf_fingers
[!!f
->df
], finger_entry
) {
131 if (memcmp(&sf
->finger
, f
, sizeof(struct xt_osf_user_finger
)))
135 * We are protected by nfnl mutex.
137 list_del_rcu(&sf
->finger_entry
);
138 kfree_rcu(sf
, rcu_head
);
147 static const struct nfnl_callback xt_osf_nfnetlink_callbacks
[OSF_MSG_MAX
] = {
149 .call
= xt_osf_add_callback
,
150 .attr_count
= OSF_ATTR_MAX
,
151 .policy
= xt_osf_policy
,
154 .call
= xt_osf_remove_callback
,
155 .attr_count
= OSF_ATTR_MAX
,
156 .policy
= xt_osf_policy
,
160 static const struct nfnetlink_subsystem xt_osf_nfnetlink
= {
162 .subsys_id
= NFNL_SUBSYS_OSF
,
163 .cb_count
= OSF_MSG_MAX
,
164 .cb
= xt_osf_nfnetlink_callbacks
,
167 static inline int xt_osf_ttl(const struct sk_buff
*skb
, const struct xt_osf_info
*info
,
170 const struct iphdr
*ip
= ip_hdr(skb
);
172 if (info
->flags
& XT_OSF_TTL
) {
173 if (info
->ttl
== XT_OSF_TTL_TRUE
)
174 return ip
->ttl
== f_ttl
;
175 if (info
->ttl
== XT_OSF_TTL_NOCHECK
)
177 else if (ip
->ttl
<= f_ttl
)
180 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
184 if (inet_ifa_match(ip
->saddr
, ifa
)) {
185 ret
= (ip
->ttl
== f_ttl
);
195 return ip
->ttl
== f_ttl
;
199 xt_osf_match_packet(const struct sk_buff
*skb
, struct xt_action_param
*p
)
201 const struct xt_osf_info
*info
= p
->matchinfo
;
202 const struct iphdr
*ip
= ip_hdr(skb
);
203 const struct tcphdr
*tcp
;
205 int fmatch
= FMATCH_WRONG
, fcount
= 0;
206 unsigned int optsize
= 0, check_WSS
= 0;
207 u16 window
, totlen
, mss
= 0;
209 const unsigned char *optp
= NULL
, *_optp
= NULL
;
210 unsigned char opts
[MAX_IPOPTLEN
];
211 const struct xt_osf_finger
*kf
;
212 const struct xt_osf_user_finger
*f
;
213 struct net
*net
= xt_net(p
);
218 tcp
= skb_header_pointer(skb
, ip_hdrlen(skb
), sizeof(struct tcphdr
), &_tcph
);
225 totlen
= ntohs(ip
->tot_len
);
226 df
= ntohs(ip
->frag_off
) & IP_DF
;
227 window
= ntohs(tcp
->window
);
229 if (tcp
->doff
* 4 > sizeof(struct tcphdr
)) {
230 optsize
= tcp
->doff
* 4 - sizeof(struct tcphdr
);
232 _optp
= optp
= skb_header_pointer(skb
, ip_hdrlen(skb
) +
233 sizeof(struct tcphdr
), optsize
, opts
);
236 list_for_each_entry_rcu(kf
, &xt_osf_fingers
[df
], finger_entry
) {
237 int foptsize
, optnum
;
241 if (!(info
->flags
& XT_OSF_LOG
) && strcmp(info
->genre
, f
->genre
))
245 fmatch
= FMATCH_WRONG
;
247 if (totlen
!= f
->ss
|| !xt_osf_ttl(skb
, info
, f
->ttl
))
251 * Should not happen if userspace parser was written correctly.
253 if (f
->wss
.wc
>= OSF_WSS_MAX
)
259 for (optnum
= 0; optnum
< f
->opt_num
; ++optnum
)
260 foptsize
+= f
->opt
[optnum
].length
;
262 if (foptsize
> MAX_IPOPTLEN
||
263 optsize
> MAX_IPOPTLEN
||
267 check_WSS
= f
->wss
.wc
;
269 for (optnum
= 0; optnum
< f
->opt_num
; ++optnum
) {
270 if (f
->opt
[optnum
].kind
== (*optp
)) {
271 __u32 len
= f
->opt
[optnum
].length
;
272 const __u8
*optend
= optp
+ len
;
282 mss
= ntohs((__force __be16
)mss
);
290 fmatch
= FMATCH_OPT_WRONG
;
292 if (fmatch
!= FMATCH_OK
)
296 if (fmatch
!= FMATCH_OPT_WRONG
) {
297 fmatch
= FMATCH_WRONG
;
301 if (f
->wss
.val
== 0 || window
== f
->wss
.val
)
306 * Some smart modems decrease mangle MSS to
307 * SMART_MSS_2, so we check standard, decreased
308 * and the one provided in the fingerprint MSS
311 #define SMART_MSS_1 1460
312 #define SMART_MSS_2 1448
313 if (window
== f
->wss
.val
* mss
||
314 window
== f
->wss
.val
* SMART_MSS_1
||
315 window
== f
->wss
.val
* SMART_MSS_2
)
319 if (window
== f
->wss
.val
* (mss
+ 40) ||
320 window
== f
->wss
.val
* (SMART_MSS_1
+ 40) ||
321 window
== f
->wss
.val
* (SMART_MSS_2
+ 40))
325 if ((window
% f
->wss
.val
) == 0)
331 if (fmatch
!= FMATCH_OK
)
336 if (info
->flags
& XT_OSF_LOG
)
337 nf_log_packet(net
, xt_family(p
), xt_hooknum(p
), skb
,
338 xt_in(p
), xt_out(p
), NULL
,
339 "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n",
340 f
->genre
, f
->version
, f
->subtype
,
341 &ip
->saddr
, ntohs(tcp
->source
),
342 &ip
->daddr
, ntohs(tcp
->dest
),
345 if ((info
->flags
& XT_OSF_LOG
) &&
346 info
->loglevel
== XT_OSF_LOGLEVEL_FIRST
)
350 if (!fcount
&& (info
->flags
& XT_OSF_LOG
))
351 nf_log_packet(net
, xt_family(p
), xt_hooknum(p
), skb
, xt_in(p
),
353 "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
354 &ip
->saddr
, ntohs(tcp
->source
),
355 &ip
->daddr
, ntohs(tcp
->dest
));
360 return fmatch
== FMATCH_OK
;
363 static struct xt_match xt_osf_match
= {
366 .family
= NFPROTO_IPV4
,
367 .proto
= IPPROTO_TCP
,
368 .hooks
= (1 << NF_INET_LOCAL_IN
) |
369 (1 << NF_INET_PRE_ROUTING
) |
370 (1 << NF_INET_FORWARD
),
371 .match
= xt_osf_match_packet
,
372 .matchsize
= sizeof(struct xt_osf_info
),
376 static int __init
xt_osf_init(void)
381 for (i
=0; i
<ARRAY_SIZE(xt_osf_fingers
); ++i
)
382 INIT_LIST_HEAD(&xt_osf_fingers
[i
]);
384 err
= nfnetlink_subsys_register(&xt_osf_nfnetlink
);
386 pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err
);
390 err
= xt_register_match(&xt_osf_match
);
392 pr_err("Failed to register OS fingerprint "
393 "matching module (%d)\n", err
);
400 nfnetlink_subsys_unregister(&xt_osf_nfnetlink
);
405 static void __exit
xt_osf_fini(void)
407 struct xt_osf_finger
*f
;
410 nfnetlink_subsys_unregister(&xt_osf_nfnetlink
);
411 xt_unregister_match(&xt_osf_match
);
414 for (i
=0; i
<ARRAY_SIZE(xt_osf_fingers
); ++i
) {
416 list_for_each_entry_rcu(f
, &xt_osf_fingers
[i
], finger_entry
) {
417 list_del_rcu(&f
->finger_entry
);
418 kfree_rcu(f
, rcu_head
);
426 module_init(xt_osf_init
);
427 module_exit(xt_osf_fini
);
429 MODULE_LICENSE("GPL");
430 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
431 MODULE_DESCRIPTION("Passive OS fingerprint matching.");
432 MODULE_ALIAS("ipt_osf");
433 MODULE_ALIAS("ip6t_osf");
434 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF
);