blob: 0778855ea5e75dff20f77f58a23c1f751c5ae45d [file] [log] [blame]
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +02001/*
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
Jeff Kirshere664eab2013-12-06 09:13:42 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +020017 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +010018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +020019#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
39struct xt_osf_finger {
40 struct rcu_head rcu_head;
41 struct list_head finger_entry;
42 struct xt_osf_user_finger finger;
43};
44
45enum 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 */
58static struct list_head xt_osf_fingers[2];
59
60static const struct nla_policy xt_osf_policy[OSF_ATTR_MAX + 1] = {
61 [OSF_ATTR_FINGER] = { .len = sizeof(struct xt_osf_user_finger) },
62};
63
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +020064static int xt_osf_add_callback(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +020065 const struct nlmsghdr *nlh,
66 const struct nlattr * const osf_attrs[])
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +020067{
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
107static int xt_osf_remove_callback(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200108 const struct nlmsghdr *nlh,
109 const struct nlattr * const osf_attrs[])
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200110{
111 struct xt_osf_user_finger *f;
112 struct xt_osf_finger *sf;
Patrick McHardyd667b9c2009-11-19 04:59:03 +0000113 int err = -ENOENT;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200114
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);
Lai Jiangshan88b4a032011-03-18 12:15:02 +0800128 kfree_rcu(sf, rcu_head);
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200129
130 err = 0;
131 break;
132 }
133
134 return err;
135}
136
137static 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
150static 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
157static 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
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200188static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200189xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p)
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200190{
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;
Gao feng30e0c6a2013-03-24 23:50:40 +0000203 struct net *net = dev_net(p->in ? p->in : p->out);
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200204
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) {
Joe Perches372e2862014-12-16 12:17:13 -0800228 int foptsize, optnum;
229
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200230 f = &kf->finger;
231
232 if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre))
233 continue;
234
235 optp = _optp;
236 fmatch = FMATCH_WRONG;
237
Joe Perches372e2862014-12-16 12:17:13 -0800238 if (totlen != f->ss || !xt_osf_ttl(skb, info, f->ttl))
239 continue;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200240
Joe Perches372e2862014-12-16 12:17:13 -0800241 /*
242 * Should not happen if userspace parser was written correctly.
243 */
244 if (f->wss.wc >= OSF_WSS_MAX)
245 continue;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200246
Joe Perches372e2862014-12-16 12:17:13 -0800247 /* Check options */
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200248
Joe Perches372e2862014-12-16 12:17:13 -0800249 foptsize = 0;
250 for (optnum = 0; optnum < f->opt_num; ++optnum)
251 foptsize += f->opt[optnum].length;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200252
Joe Perches372e2862014-12-16 12:17:13 -0800253 if (foptsize > MAX_IPOPTLEN ||
254 optsize > MAX_IPOPTLEN ||
255 optsize != foptsize)
256 continue;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200257
Joe Perches372e2862014-12-16 12:17:13 -0800258 check_WSS = f->wss.wc;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200259
Joe Perches372e2862014-12-16 12:17:13 -0800260 for (optnum = 0; optnum < f->opt_num; ++optnum) {
261 if (f->opt[optnum].kind == (*optp)) {
262 __u32 len = f->opt[optnum].length;
263 const __u8 *optend = optp + len;
264 int loop_cont = 0;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200265
Joe Perches372e2862014-12-16 12:17:13 -0800266 fmatch = FMATCH_OK;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200267
Joe Perches372e2862014-12-16 12:17:13 -0800268 switch (*optp) {
269 case OSFOPT_MSS:
270 mss = optp[3];
271 mss <<= 8;
272 mss |= optp[2];
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200273
Joe Perches372e2862014-12-16 12:17:13 -0800274 mss = ntohs((__force __be16)mss);
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200275 break;
Joe Perches372e2862014-12-16 12:17:13 -0800276 case OSFOPT_TS:
277 loop_cont = 1;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200278 break;
279 }
Joe Perches372e2862014-12-16 12:17:13 -0800280
281 optp = optend;
282 } else
283 fmatch = FMATCH_OPT_WRONG;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200284
285 if (fmatch != FMATCH_OK)
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200286 break;
287 }
Joe Perches372e2862014-12-16 12:17:13 -0800288
289 if (fmatch != FMATCH_OPT_WRONG) {
290 fmatch = FMATCH_WRONG;
291
292 switch (check_WSS) {
293 case OSF_WSS_PLAIN:
294 if (f->wss.val == 0 || window == f->wss.val)
295 fmatch = FMATCH_OK;
296 break;
297 case OSF_WSS_MSS:
298 /*
299 * Some smart modems decrease mangle MSS to
300 * SMART_MSS_2, so we check standard, decreased
301 * and the one provided in the fingerprint MSS
302 * values.
303 */
304#define SMART_MSS_1 1460
305#define SMART_MSS_2 1448
306 if (window == f->wss.val * mss ||
307 window == f->wss.val * SMART_MSS_1 ||
308 window == f->wss.val * SMART_MSS_2)
309 fmatch = FMATCH_OK;
310 break;
311 case OSF_WSS_MTU:
312 if (window == f->wss.val * (mss + 40) ||
313 window == f->wss.val * (SMART_MSS_1 + 40) ||
314 window == f->wss.val * (SMART_MSS_2 + 40))
315 fmatch = FMATCH_OK;
316 break;
317 case OSF_WSS_MODULO:
318 if ((window % f->wss.val) == 0)
319 fmatch = FMATCH_OK;
320 break;
321 }
322 }
323
324 if (fmatch != FMATCH_OK)
325 continue;
326
327 fcount++;
328
329 if (info->flags & XT_OSF_LOG)
330 nf_log_packet(net, p->family, p->hooknum, skb,
331 p->in, p->out, NULL,
332 "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n",
333 f->genre, f->version, f->subtype,
334 &ip->saddr, ntohs(tcp->source),
335 &ip->daddr, ntohs(tcp->dest),
336 f->ttl - ip->ttl);
337
338 if ((info->flags & XT_OSF_LOG) &&
339 info->loglevel == XT_OSF_LOGLEVEL_FIRST)
340 break;
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200341 }
342 rcu_read_unlock();
343
344 if (!fcount && (info->flags & XT_OSF_LOG))
Gao feng30e0c6a2013-03-24 23:50:40 +0000345 nf_log_packet(net, p->family, p->hooknum, skb, p->in,
346 p->out, NULL,
Joe Perches7f635d02010-01-11 11:55:36 +0100347 "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200348 &ip->saddr, ntohs(tcp->source),
349 &ip->daddr, ntohs(tcp->dest));
350
351 if (fcount)
352 fmatch = FMATCH_OK;
353
354 return fmatch == FMATCH_OK;
355}
356
357static struct xt_match xt_osf_match = {
358 .name = "osf",
359 .revision = 0,
360 .family = NFPROTO_IPV4,
361 .proto = IPPROTO_TCP,
362 .hooks = (1 << NF_INET_LOCAL_IN) |
363 (1 << NF_INET_PRE_ROUTING) |
364 (1 << NF_INET_FORWARD),
365 .match = xt_osf_match_packet,
366 .matchsize = sizeof(struct xt_osf_info),
367 .me = THIS_MODULE,
368};
369
370static int __init xt_osf_init(void)
371{
372 int err = -EINVAL;
373 int i;
374
375 for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i)
376 INIT_LIST_HEAD(&xt_osf_fingers[i]);
377
378 err = nfnetlink_subsys_register(&xt_osf_nfnetlink);
379 if (err < 0) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100380 pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err);
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200381 goto err_out_exit;
382 }
383
384 err = xt_register_match(&xt_osf_match);
385 if (err) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100386 pr_err("Failed to register OS fingerprint "
387 "matching module (%d)\n", err);
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200388 goto err_out_remove;
389 }
390
391 return 0;
392
393err_out_remove:
394 nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
395err_out_exit:
396 return err;
397}
398
399static void __exit xt_osf_fini(void)
400{
401 struct xt_osf_finger *f;
402 int i;
403
404 nfnetlink_subsys_unregister(&xt_osf_nfnetlink);
405 xt_unregister_match(&xt_osf_match);
406
407 rcu_read_lock();
408 for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) {
409
410 list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) {
411 list_del_rcu(&f->finger_entry);
Lai Jiangshan88b4a032011-03-18 12:15:02 +0800412 kfree_rcu(f, rcu_head);
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200413 }
414 }
415 rcu_read_unlock();
416
417 rcu_barrier();
418}
419
420module_init(xt_osf_init);
421module_exit(xt_osf_fini);
422
423MODULE_LICENSE("GPL");
424MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
425MODULE_DESCRIPTION("Passive OS fingerprint matching.");
Kirill Tkhaib8ddd9e2014-03-26 14:37:59 +0400426MODULE_ALIAS("ipt_osf");
427MODULE_ALIAS("ip6t_osf");
Evgeniy Polyakov11eeef42009-06-08 17:01:51 +0200428MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF);