Commit | Line | Data |
---|---|---|
11eeef41 EP |
1 | /* |
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 | |
e664eabd | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
11eeef41 | 17 | */ |
8bee4bad | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11eeef41 EP |
19 | #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 | ||
39 | struct xt_osf_finger { | |
40 | struct rcu_head rcu_head; | |
41 | struct list_head finger_entry; | |
42 | struct xt_osf_user_finger finger; | |
43 | }; | |
44 | ||
45 | enum 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 | */ | |
58 | static struct list_head xt_osf_fingers[2]; | |
59 | ||
60 | static const struct nla_policy xt_osf_policy[OSF_ATTR_MAX + 1] = { | |
61 | [OSF_ATTR_FINGER] = { .len = sizeof(struct xt_osf_user_finger) }, | |
62 | }; | |
63 | ||
7b8002a1 PNA |
64 | static int xt_osf_add_callback(struct net *net, struct sock *ctnl, |
65 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
39938324 | 66 | const struct nlattr * const osf_attrs[]) |
11eeef41 EP |
67 | { |
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 | ||
7b8002a1 PNA |
107 | static int xt_osf_remove_callback(struct net *net, struct sock *ctnl, |
108 | struct sk_buff *skb, | |
39938324 PM |
109 | const struct nlmsghdr *nlh, |
110 | const struct nlattr * const osf_attrs[]) | |
11eeef41 EP |
111 | { |
112 | struct xt_osf_user_finger *f; | |
113 | struct xt_osf_finger *sf; | |
d667b9cf | 114 | int err = -ENOENT; |
11eeef41 EP |
115 | |
116 | if (!osf_attrs[OSF_ATTR_FINGER]) | |
117 | return -EINVAL; | |
118 | ||
119 | f = nla_data(osf_attrs[OSF_ATTR_FINGER]); | |
120 | ||
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))) | |
123 | continue; | |
124 | ||
125 | /* | |
126 | * We are protected by nfnl mutex. | |
127 | */ | |
128 | list_del_rcu(&sf->finger_entry); | |
88b4a034 | 129 | kfree_rcu(sf, rcu_head); |
11eeef41 EP |
130 | |
131 | err = 0; | |
132 | break; | |
133 | } | |
134 | ||
135 | return err; | |
136 | } | |
137 | ||
138 | static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = { | |
139 | [OSF_MSG_ADD] = { | |
140 | .call = xt_osf_add_callback, | |
141 | .attr_count = OSF_ATTR_MAX, | |
142 | .policy = xt_osf_policy, | |
143 | }, | |
144 | [OSF_MSG_REMOVE] = { | |
145 | .call = xt_osf_remove_callback, | |
146 | .attr_count = OSF_ATTR_MAX, | |
147 | .policy = xt_osf_policy, | |
148 | }, | |
149 | }; | |
150 | ||
151 | static const struct nfnetlink_subsystem xt_osf_nfnetlink = { | |
152 | .name = "osf", | |
153 | .subsys_id = NFNL_SUBSYS_OSF, | |
154 | .cb_count = OSF_MSG_MAX, | |
155 | .cb = xt_osf_nfnetlink_callbacks, | |
156 | }; | |
157 | ||
158 | static inline int xt_osf_ttl(const struct sk_buff *skb, const struct xt_osf_info *info, | |
159 | unsigned char f_ttl) | |
160 | { | |
161 | const struct iphdr *ip = ip_hdr(skb); | |
162 | ||
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) | |
167 | return 1; | |
168 | else if (ip->ttl <= f_ttl) | |
169 | return 1; | |
170 | else { | |
171 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); | |
172 | int ret = 0; | |
173 | ||
174 | for_ifa(in_dev) { | |
175 | if (inet_ifa_match(ip->saddr, ifa)) { | |
176 | ret = (ip->ttl == f_ttl); | |
177 | break; | |
178 | } | |
179 | } | |
180 | endfor_ifa(in_dev); | |
181 | ||
182 | return ret; | |
183 | } | |
184 | } | |
185 | ||
186 | return ip->ttl == f_ttl; | |
187 | } | |
188 | ||
4b560b44 | 189 | static bool |
62fc8051 | 190 | xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p) |
11eeef41 EP |
191 | { |
192 | const struct xt_osf_info *info = p->matchinfo; | |
193 | const struct iphdr *ip = ip_hdr(skb); | |
194 | const struct tcphdr *tcp; | |
195 | struct tcphdr _tcph; | |
196 | int fmatch = FMATCH_WRONG, fcount = 0; | |
197 | unsigned int optsize = 0, check_WSS = 0; | |
198 | u16 window, totlen, mss = 0; | |
199 | bool df; | |
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; | |
686c9b50 | 204 | struct net *net = p->net; |
11eeef41 EP |
205 | |
206 | if (!info) | |
207 | return false; | |
208 | ||
209 | tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph); | |
210 | if (!tcp) | |
211 | return false; | |
212 | ||
213 | if (!tcp->syn) | |
214 | return false; | |
215 | ||
216 | totlen = ntohs(ip->tot_len); | |
217 | df = ntohs(ip->frag_off) & IP_DF; | |
218 | window = ntohs(tcp->window); | |
219 | ||
220 | if (tcp->doff * 4 > sizeof(struct tcphdr)) { | |
221 | optsize = tcp->doff * 4 - sizeof(struct tcphdr); | |
222 | ||
223 | _optp = optp = skb_header_pointer(skb, ip_hdrlen(skb) + | |
224 | sizeof(struct tcphdr), optsize, opts); | |
225 | } | |
226 | ||
227 | rcu_read_lock(); | |
228 | list_for_each_entry_rcu(kf, &xt_osf_fingers[df], finger_entry) { | |
372e2866 JP |
229 | int foptsize, optnum; |
230 | ||
11eeef41 EP |
231 | f = &kf->finger; |
232 | ||
233 | if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre)) | |
234 | continue; | |
235 | ||
236 | optp = _optp; | |
237 | fmatch = FMATCH_WRONG; | |
238 | ||
372e2866 JP |
239 | if (totlen != f->ss || !xt_osf_ttl(skb, info, f->ttl)) |
240 | continue; | |
11eeef41 | 241 | |
372e2866 JP |
242 | /* |
243 | * Should not happen if userspace parser was written correctly. | |
244 | */ | |
245 | if (f->wss.wc >= OSF_WSS_MAX) | |
246 | continue; | |
11eeef41 | 247 | |
372e2866 | 248 | /* Check options */ |
11eeef41 | 249 | |
372e2866 JP |
250 | foptsize = 0; |
251 | for (optnum = 0; optnum < f->opt_num; ++optnum) | |
252 | foptsize += f->opt[optnum].length; | |
11eeef41 | 253 | |
372e2866 JP |
254 | if (foptsize > MAX_IPOPTLEN || |
255 | optsize > MAX_IPOPTLEN || | |
256 | optsize != foptsize) | |
257 | continue; | |
11eeef41 | 258 | |
372e2866 | 259 | check_WSS = f->wss.wc; |
11eeef41 | 260 | |
372e2866 JP |
261 | for (optnum = 0; optnum < f->opt_num; ++optnum) { |
262 | if (f->opt[optnum].kind == (*optp)) { | |
263 | __u32 len = f->opt[optnum].length; | |
264 | const __u8 *optend = optp + len; | |
265 | int loop_cont = 0; | |
11eeef41 | 266 | |
372e2866 | 267 | fmatch = FMATCH_OK; |
11eeef41 | 268 | |
372e2866 JP |
269 | switch (*optp) { |
270 | case OSFOPT_MSS: | |
271 | mss = optp[3]; | |
272 | mss <<= 8; | |
273 | mss |= optp[2]; | |
11eeef41 | 274 | |
372e2866 JP |
275 | mss = ntohs((__force __be16)mss); |
276 | break; | |
277 | case OSFOPT_TS: | |
278 | loop_cont = 1; | |
279 | break; | |
280 | } | |
11eeef41 | 281 | |
372e2866 JP |
282 | optp = optend; |
283 | } else | |
284 | fmatch = FMATCH_OPT_WRONG; | |
11eeef41 | 285 | |
372e2866 JP |
286 | if (fmatch != FMATCH_OK) |
287 | break; | |
288 | } | |
11eeef41 | 289 | |
372e2866 JP |
290 | if (fmatch != FMATCH_OPT_WRONG) { |
291 | fmatch = FMATCH_WRONG; | |
11eeef41 | 292 | |
372e2866 JP |
293 | switch (check_WSS) { |
294 | case OSF_WSS_PLAIN: | |
295 | if (f->wss.val == 0 || window == f->wss.val) | |
296 | fmatch = FMATCH_OK; | |
297 | break; | |
298 | case OSF_WSS_MSS: | |
299 | /* | |
300 | * Some smart modems decrease mangle MSS to | |
301 | * SMART_MSS_2, so we check standard, decreased | |
302 | * and the one provided in the fingerprint MSS | |
303 | * values. | |
304 | */ | |
11eeef41 EP |
305 | #define SMART_MSS_1 1460 |
306 | #define SMART_MSS_2 1448 | |
372e2866 JP |
307 | if (window == f->wss.val * mss || |
308 | window == f->wss.val * SMART_MSS_1 || | |
309 | window == f->wss.val * SMART_MSS_2) | |
310 | fmatch = FMATCH_OK; | |
311 | break; | |
312 | case OSF_WSS_MTU: | |
313 | if (window == f->wss.val * (mss + 40) || | |
314 | window == f->wss.val * (SMART_MSS_1 + 40) || | |
315 | window == f->wss.val * (SMART_MSS_2 + 40)) | |
316 | fmatch = FMATCH_OK; | |
317 | break; | |
318 | case OSF_WSS_MODULO: | |
319 | if ((window % f->wss.val) == 0) | |
320 | fmatch = FMATCH_OK; | |
321 | break; | |
11eeef41 | 322 | } |
372e2866 | 323 | } |
11eeef41 | 324 | |
372e2866 JP |
325 | if (fmatch != FMATCH_OK) |
326 | continue; | |
11eeef41 | 327 | |
372e2866 | 328 | fcount++; |
11eeef41 | 329 | |
372e2866 JP |
330 | if (info->flags & XT_OSF_LOG) |
331 | nf_log_packet(net, p->family, p->hooknum, skb, | |
332 | p->in, p->out, NULL, | |
333 | "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n", | |
334 | f->genre, f->version, f->subtype, | |
335 | &ip->saddr, ntohs(tcp->source), | |
336 | &ip->daddr, ntohs(tcp->dest), | |
337 | f->ttl - ip->ttl); | |
11eeef41 | 338 | |
372e2866 JP |
339 | if ((info->flags & XT_OSF_LOG) && |
340 | info->loglevel == XT_OSF_LOGLEVEL_FIRST) | |
341 | break; | |
11eeef41 EP |
342 | } |
343 | rcu_read_unlock(); | |
344 | ||
345 | if (!fcount && (info->flags & XT_OSF_LOG)) | |
30e0c6a6 G |
346 | nf_log_packet(net, p->family, p->hooknum, skb, p->in, |
347 | p->out, NULL, | |
7f635d0d | 348 | "Remote OS is not known: %pI4:%u -> %pI4:%u\n", |
11eeef41 EP |
349 | &ip->saddr, ntohs(tcp->source), |
350 | &ip->daddr, ntohs(tcp->dest)); | |
351 | ||
352 | if (fcount) | |
353 | fmatch = FMATCH_OK; | |
354 | ||
355 | return fmatch == FMATCH_OK; | |
356 | } | |
357 | ||
358 | static struct xt_match xt_osf_match = { | |
359 | .name = "osf", | |
360 | .revision = 0, | |
361 | .family = NFPROTO_IPV4, | |
362 | .proto = IPPROTO_TCP, | |
363 | .hooks = (1 << NF_INET_LOCAL_IN) | | |
364 | (1 << NF_INET_PRE_ROUTING) | | |
365 | (1 << NF_INET_FORWARD), | |
366 | .match = xt_osf_match_packet, | |
367 | .matchsize = sizeof(struct xt_osf_info), | |
368 | .me = THIS_MODULE, | |
369 | }; | |
370 | ||
371 | static int __init xt_osf_init(void) | |
372 | { | |
373 | int err = -EINVAL; | |
374 | int i; | |
375 | ||
376 | for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) | |
377 | INIT_LIST_HEAD(&xt_osf_fingers[i]); | |
378 | ||
379 | err = nfnetlink_subsys_register(&xt_osf_nfnetlink); | |
380 | if (err < 0) { | |
8bee4bad | 381 | pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err); |
11eeef41 EP |
382 | goto err_out_exit; |
383 | } | |
384 | ||
385 | err = xt_register_match(&xt_osf_match); | |
386 | if (err) { | |
8bee4bad JE |
387 | pr_err("Failed to register OS fingerprint " |
388 | "matching module (%d)\n", err); | |
11eeef41 EP |
389 | goto err_out_remove; |
390 | } | |
391 | ||
392 | return 0; | |
393 | ||
394 | err_out_remove: | |
395 | nfnetlink_subsys_unregister(&xt_osf_nfnetlink); | |
396 | err_out_exit: | |
397 | return err; | |
398 | } | |
399 | ||
400 | static void __exit xt_osf_fini(void) | |
401 | { | |
402 | struct xt_osf_finger *f; | |
403 | int i; | |
404 | ||
405 | nfnetlink_subsys_unregister(&xt_osf_nfnetlink); | |
406 | xt_unregister_match(&xt_osf_match); | |
407 | ||
408 | rcu_read_lock(); | |
409 | for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) { | |
410 | ||
411 | list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) { | |
412 | list_del_rcu(&f->finger_entry); | |
88b4a034 | 413 | kfree_rcu(f, rcu_head); |
11eeef41 EP |
414 | } |
415 | } | |
416 | rcu_read_unlock(); | |
417 | ||
418 | rcu_barrier(); | |
419 | } | |
420 | ||
421 | module_init(xt_osf_init); | |
422 | module_exit(xt_osf_fini); | |
423 | ||
424 | MODULE_LICENSE("GPL"); | |
425 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); | |
426 | MODULE_DESCRIPTION("Passive OS fingerprint matching."); | |
b8ddd9ea KT |
427 | MODULE_ALIAS("ipt_osf"); |
428 | MODULE_ALIAS("ip6t_osf"); | |
11eeef41 | 429 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF); |