]>
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 | ||
d74b514a | 22 | #include <linux/capability.h> |
11eeef41 EP |
23 | #include <linux/if.h> |
24 | #include <linux/inetdevice.h> | |
25 | #include <linux/ip.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> | |
31 | ||
32 | #include <net/ip.h> | |
33 | #include <net/tcp.h> | |
34 | ||
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> | |
39 | ||
40 | struct xt_osf_finger { | |
41 | struct rcu_head rcu_head; | |
42 | struct list_head finger_entry; | |
43 | struct xt_osf_user_finger finger; | |
44 | }; | |
45 | ||
46 | enum osf_fmatch_states { | |
47 | /* Packet does not match the fingerprint */ | |
48 | FMATCH_WRONG = 0, | |
49 | /* Packet matches the fingerprint */ | |
50 | FMATCH_OK, | |
51 | /* Options do not match the fingerprint, but header does */ | |
52 | FMATCH_OPT_WRONG, | |
53 | }; | |
54 | ||
55 | /* | |
56 | * Indexed by dont-fragment bit. | |
57 | * It is the only constant value in the fingerprint. | |
58 | */ | |
59 | static struct list_head xt_osf_fingers[2]; | |
60 | ||
61 | static const struct nla_policy xt_osf_policy[OSF_ATTR_MAX + 1] = { | |
62 | [OSF_ATTR_FINGER] = { .len = sizeof(struct xt_osf_user_finger) }, | |
63 | }; | |
64 | ||
7b8002a1 PNA |
65 | static int xt_osf_add_callback(struct net *net, struct sock *ctnl, |
66 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
67 | const struct nlattr * const osf_attrs[], |
68 | struct netlink_ext_ack *extack) | |
11eeef41 EP |
69 | { |
70 | struct xt_osf_user_finger *f; | |
71 | struct xt_osf_finger *kf = NULL, *sf; | |
72 | int err = 0; | |
73 | ||
d74b514a KC |
74 | if (!capable(CAP_NET_ADMIN)) |
75 | return -EPERM; | |
76 | ||
11eeef41 EP |
77 | if (!osf_attrs[OSF_ATTR_FINGER]) |
78 | return -EINVAL; | |
79 | ||
80 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
81 | return -EINVAL; | |
82 | ||
83 | f = nla_data(osf_attrs[OSF_ATTR_FINGER]); | |
84 | ||
85 | kf = kmalloc(sizeof(struct xt_osf_finger), GFP_KERNEL); | |
86 | if (!kf) | |
87 | return -ENOMEM; | |
88 | ||
89 | memcpy(&kf->finger, f, sizeof(struct xt_osf_user_finger)); | |
90 | ||
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))) | |
93 | continue; | |
94 | ||
95 | kfree(kf); | |
96 | kf = NULL; | |
97 | ||
98 | if (nlh->nlmsg_flags & NLM_F_EXCL) | |
99 | err = -EEXIST; | |
100 | break; | |
101 | } | |
102 | ||
103 | /* | |
104 | * We are protected by nfnl mutex. | |
105 | */ | |
106 | if (kf) | |
107 | list_add_tail_rcu(&kf->finger_entry, &xt_osf_fingers[!!f->df]); | |
108 | ||
109 | return err; | |
110 | } | |
111 | ||
7b8002a1 PNA |
112 | static int xt_osf_remove_callback(struct net *net, struct sock *ctnl, |
113 | struct sk_buff *skb, | |
39938324 | 114 | const struct nlmsghdr *nlh, |
04ba724b PNA |
115 | const struct nlattr * const osf_attrs[], |
116 | struct netlink_ext_ack *extack) | |
11eeef41 EP |
117 | { |
118 | struct xt_osf_user_finger *f; | |
119 | struct xt_osf_finger *sf; | |
d667b9cf | 120 | int err = -ENOENT; |
11eeef41 | 121 | |
d74b514a KC |
122 | if (!capable(CAP_NET_ADMIN)) |
123 | return -EPERM; | |
124 | ||
11eeef41 EP |
125 | if (!osf_attrs[OSF_ATTR_FINGER]) |
126 | return -EINVAL; | |
127 | ||
128 | f = nla_data(osf_attrs[OSF_ATTR_FINGER]); | |
129 | ||
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))) | |
132 | continue; | |
133 | ||
134 | /* | |
135 | * We are protected by nfnl mutex. | |
136 | */ | |
137 | list_del_rcu(&sf->finger_entry); | |
88b4a034 | 138 | kfree_rcu(sf, rcu_head); |
11eeef41 EP |
139 | |
140 | err = 0; | |
141 | break; | |
142 | } | |
143 | ||
144 | return err; | |
145 | } | |
146 | ||
147 | static const struct nfnl_callback xt_osf_nfnetlink_callbacks[OSF_MSG_MAX] = { | |
148 | [OSF_MSG_ADD] = { | |
149 | .call = xt_osf_add_callback, | |
150 | .attr_count = OSF_ATTR_MAX, | |
151 | .policy = xt_osf_policy, | |
152 | }, | |
153 | [OSF_MSG_REMOVE] = { | |
154 | .call = xt_osf_remove_callback, | |
155 | .attr_count = OSF_ATTR_MAX, | |
156 | .policy = xt_osf_policy, | |
157 | }, | |
158 | }; | |
159 | ||
160 | static const struct nfnetlink_subsystem xt_osf_nfnetlink = { | |
161 | .name = "osf", | |
162 | .subsys_id = NFNL_SUBSYS_OSF, | |
163 | .cb_count = OSF_MSG_MAX, | |
164 | .cb = xt_osf_nfnetlink_callbacks, | |
165 | }; | |
166 | ||
167 | static inline int xt_osf_ttl(const struct sk_buff *skb, const struct xt_osf_info *info, | |
168 | unsigned char f_ttl) | |
169 | { | |
170 | const struct iphdr *ip = ip_hdr(skb); | |
171 | ||
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) | |
176 | return 1; | |
177 | else if (ip->ttl <= f_ttl) | |
178 | return 1; | |
179 | else { | |
180 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); | |
181 | int ret = 0; | |
182 | ||
183 | for_ifa(in_dev) { | |
184 | if (inet_ifa_match(ip->saddr, ifa)) { | |
185 | ret = (ip->ttl == f_ttl); | |
186 | break; | |
187 | } | |
188 | } | |
189 | endfor_ifa(in_dev); | |
190 | ||
191 | return ret; | |
192 | } | |
193 | } | |
194 | ||
195 | return ip->ttl == f_ttl; | |
196 | } | |
197 | ||
4b560b44 | 198 | static bool |
62fc8051 | 199 | xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p) |
11eeef41 EP |
200 | { |
201 | const struct xt_osf_info *info = p->matchinfo; | |
202 | const struct iphdr *ip = ip_hdr(skb); | |
203 | const struct tcphdr *tcp; | |
204 | struct tcphdr _tcph; | |
205 | int fmatch = FMATCH_WRONG, fcount = 0; | |
206 | unsigned int optsize = 0, check_WSS = 0; | |
207 | u16 window, totlen, mss = 0; | |
208 | bool df; | |
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; | |
613dbd95 | 213 | struct net *net = xt_net(p); |
11eeef41 EP |
214 | |
215 | if (!info) | |
216 | return false; | |
217 | ||
218 | tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph); | |
219 | if (!tcp) | |
220 | return false; | |
221 | ||
222 | if (!tcp->syn) | |
223 | return false; | |
224 | ||
225 | totlen = ntohs(ip->tot_len); | |
226 | df = ntohs(ip->frag_off) & IP_DF; | |
227 | window = ntohs(tcp->window); | |
228 | ||
229 | if (tcp->doff * 4 > sizeof(struct tcphdr)) { | |
230 | optsize = tcp->doff * 4 - sizeof(struct tcphdr); | |
231 | ||
232 | _optp = optp = skb_header_pointer(skb, ip_hdrlen(skb) + | |
233 | sizeof(struct tcphdr), optsize, opts); | |
234 | } | |
235 | ||
236 | rcu_read_lock(); | |
237 | list_for_each_entry_rcu(kf, &xt_osf_fingers[df], finger_entry) { | |
372e2866 JP |
238 | int foptsize, optnum; |
239 | ||
11eeef41 EP |
240 | f = &kf->finger; |
241 | ||
242 | if (!(info->flags & XT_OSF_LOG) && strcmp(info->genre, f->genre)) | |
243 | continue; | |
244 | ||
245 | optp = _optp; | |
246 | fmatch = FMATCH_WRONG; | |
247 | ||
372e2866 JP |
248 | if (totlen != f->ss || !xt_osf_ttl(skb, info, f->ttl)) |
249 | continue; | |
11eeef41 | 250 | |
372e2866 JP |
251 | /* |
252 | * Should not happen if userspace parser was written correctly. | |
253 | */ | |
254 | if (f->wss.wc >= OSF_WSS_MAX) | |
255 | continue; | |
11eeef41 | 256 | |
372e2866 | 257 | /* Check options */ |
11eeef41 | 258 | |
372e2866 JP |
259 | foptsize = 0; |
260 | for (optnum = 0; optnum < f->opt_num; ++optnum) | |
261 | foptsize += f->opt[optnum].length; | |
11eeef41 | 262 | |
372e2866 JP |
263 | if (foptsize > MAX_IPOPTLEN || |
264 | optsize > MAX_IPOPTLEN || | |
265 | optsize != foptsize) | |
266 | continue; | |
11eeef41 | 267 | |
372e2866 | 268 | check_WSS = f->wss.wc; |
11eeef41 | 269 | |
372e2866 JP |
270 | for (optnum = 0; optnum < f->opt_num; ++optnum) { |
271 | if (f->opt[optnum].kind == (*optp)) { | |
272 | __u32 len = f->opt[optnum].length; | |
273 | const __u8 *optend = optp + len; | |
11eeef41 | 274 | |
372e2866 | 275 | fmatch = FMATCH_OK; |
11eeef41 | 276 | |
372e2866 JP |
277 | switch (*optp) { |
278 | case OSFOPT_MSS: | |
279 | mss = optp[3]; | |
280 | mss <<= 8; | |
281 | mss |= optp[2]; | |
11eeef41 | 282 | |
372e2866 JP |
283 | mss = ntohs((__force __be16)mss); |
284 | break; | |
285 | case OSFOPT_TS: | |
372e2866 JP |
286 | break; |
287 | } | |
11eeef41 | 288 | |
372e2866 JP |
289 | optp = optend; |
290 | } else | |
291 | fmatch = FMATCH_OPT_WRONG; | |
11eeef41 | 292 | |
372e2866 JP |
293 | if (fmatch != FMATCH_OK) |
294 | break; | |
295 | } | |
11eeef41 | 296 | |
372e2866 JP |
297 | if (fmatch != FMATCH_OPT_WRONG) { |
298 | fmatch = FMATCH_WRONG; | |
11eeef41 | 299 | |
372e2866 JP |
300 | switch (check_WSS) { |
301 | case OSF_WSS_PLAIN: | |
302 | if (f->wss.val == 0 || window == f->wss.val) | |
303 | fmatch = FMATCH_OK; | |
304 | break; | |
305 | case OSF_WSS_MSS: | |
306 | /* | |
307 | * Some smart modems decrease mangle MSS to | |
308 | * SMART_MSS_2, so we check standard, decreased | |
309 | * and the one provided in the fingerprint MSS | |
310 | * values. | |
311 | */ | |
11eeef41 EP |
312 | #define SMART_MSS_1 1460 |
313 | #define SMART_MSS_2 1448 | |
372e2866 JP |
314 | if (window == f->wss.val * mss || |
315 | window == f->wss.val * SMART_MSS_1 || | |
316 | window == f->wss.val * SMART_MSS_2) | |
317 | fmatch = FMATCH_OK; | |
318 | break; | |
319 | case OSF_WSS_MTU: | |
320 | if (window == f->wss.val * (mss + 40) || | |
321 | window == f->wss.val * (SMART_MSS_1 + 40) || | |
322 | window == f->wss.val * (SMART_MSS_2 + 40)) | |
323 | fmatch = FMATCH_OK; | |
324 | break; | |
325 | case OSF_WSS_MODULO: | |
326 | if ((window % f->wss.val) == 0) | |
327 | fmatch = FMATCH_OK; | |
328 | break; | |
11eeef41 | 329 | } |
372e2866 | 330 | } |
11eeef41 | 331 | |
372e2866 JP |
332 | if (fmatch != FMATCH_OK) |
333 | continue; | |
11eeef41 | 334 | |
372e2866 | 335 | fcount++; |
11eeef41 | 336 | |
372e2866 | 337 | if (info->flags & XT_OSF_LOG) |
613dbd95 PNA |
338 | nf_log_packet(net, xt_family(p), xt_hooknum(p), skb, |
339 | xt_in(p), xt_out(p), NULL, | |
372e2866 JP |
340 | "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n", |
341 | f->genre, f->version, f->subtype, | |
342 | &ip->saddr, ntohs(tcp->source), | |
343 | &ip->daddr, ntohs(tcp->dest), | |
344 | f->ttl - ip->ttl); | |
11eeef41 | 345 | |
372e2866 JP |
346 | if ((info->flags & XT_OSF_LOG) && |
347 | info->loglevel == XT_OSF_LOGLEVEL_FIRST) | |
348 | break; | |
11eeef41 EP |
349 | } |
350 | rcu_read_unlock(); | |
351 | ||
352 | if (!fcount && (info->flags & XT_OSF_LOG)) | |
613dbd95 PNA |
353 | nf_log_packet(net, xt_family(p), xt_hooknum(p), skb, xt_in(p), |
354 | xt_out(p), NULL, | |
7f635d0d | 355 | "Remote OS is not known: %pI4:%u -> %pI4:%u\n", |
11eeef41 EP |
356 | &ip->saddr, ntohs(tcp->source), |
357 | &ip->daddr, ntohs(tcp->dest)); | |
358 | ||
359 | if (fcount) | |
360 | fmatch = FMATCH_OK; | |
361 | ||
362 | return fmatch == FMATCH_OK; | |
363 | } | |
364 | ||
365 | static struct xt_match xt_osf_match = { | |
366 | .name = "osf", | |
367 | .revision = 0, | |
368 | .family = NFPROTO_IPV4, | |
369 | .proto = IPPROTO_TCP, | |
370 | .hooks = (1 << NF_INET_LOCAL_IN) | | |
371 | (1 << NF_INET_PRE_ROUTING) | | |
372 | (1 << NF_INET_FORWARD), | |
373 | .match = xt_osf_match_packet, | |
374 | .matchsize = sizeof(struct xt_osf_info), | |
375 | .me = THIS_MODULE, | |
376 | }; | |
377 | ||
378 | static int __init xt_osf_init(void) | |
379 | { | |
380 | int err = -EINVAL; | |
381 | int i; | |
382 | ||
383 | for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) | |
384 | INIT_LIST_HEAD(&xt_osf_fingers[i]); | |
385 | ||
386 | err = nfnetlink_subsys_register(&xt_osf_nfnetlink); | |
387 | if (err < 0) { | |
8bee4bad | 388 | pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err); |
11eeef41 EP |
389 | goto err_out_exit; |
390 | } | |
391 | ||
392 | err = xt_register_match(&xt_osf_match); | |
393 | if (err) { | |
8bee4bad JE |
394 | pr_err("Failed to register OS fingerprint " |
395 | "matching module (%d)\n", err); | |
11eeef41 EP |
396 | goto err_out_remove; |
397 | } | |
398 | ||
399 | return 0; | |
400 | ||
401 | err_out_remove: | |
402 | nfnetlink_subsys_unregister(&xt_osf_nfnetlink); | |
403 | err_out_exit: | |
404 | return err; | |
405 | } | |
406 | ||
407 | static void __exit xt_osf_fini(void) | |
408 | { | |
409 | struct xt_osf_finger *f; | |
410 | int i; | |
411 | ||
412 | nfnetlink_subsys_unregister(&xt_osf_nfnetlink); | |
413 | xt_unregister_match(&xt_osf_match); | |
414 | ||
415 | rcu_read_lock(); | |
416 | for (i=0; i<ARRAY_SIZE(xt_osf_fingers); ++i) { | |
417 | ||
418 | list_for_each_entry_rcu(f, &xt_osf_fingers[i], finger_entry) { | |
419 | list_del_rcu(&f->finger_entry); | |
88b4a034 | 420 | kfree_rcu(f, rcu_head); |
11eeef41 EP |
421 | } |
422 | } | |
423 | rcu_read_unlock(); | |
424 | ||
425 | rcu_barrier(); | |
426 | } | |
427 | ||
428 | module_init(xt_osf_init); | |
429 | module_exit(xt_osf_fini); | |
430 | ||
431 | MODULE_LICENSE("GPL"); | |
432 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); | |
433 | MODULE_DESCRIPTION("Passive OS fingerprint matching."); | |
b8ddd9ea KT |
434 | MODULE_ALIAS("ipt_osf"); |
435 | MODULE_ALIAS("ip6t_osf"); | |
11eeef41 | 436 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_OSF); |