Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
bfb15f2a FFM |
2 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
3 | #include <linux/module.h> | |
4 | #include <linux/kernel.h> | |
5 | ||
6 | #include <linux/capability.h> | |
7 | #include <linux/if.h> | |
8 | #include <linux/inetdevice.h> | |
9 | #include <linux/ip.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/rculist.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/tcp.h> | |
15 | ||
16 | #include <net/ip.h> | |
17 | #include <net/tcp.h> | |
18 | ||
19 | #include <linux/netfilter/nfnetlink.h> | |
20 | #include <linux/netfilter/x_tables.h> | |
21 | #include <net/netfilter/nf_log.h> | |
ddba40be | 22 | #include <linux/netfilter/nfnetlink_osf.h> |
bfb15f2a | 23 | |
f9324952 FFM |
24 | /* |
25 | * Indexed by dont-fragment bit. | |
26 | * It is the only constant value in the fingerprint. | |
27 | */ | |
28 | struct list_head nf_osf_fingers[2]; | |
29 | EXPORT_SYMBOL_GPL(nf_osf_fingers); | |
30 | ||
bfb15f2a | 31 | static inline int nf_osf_ttl(const struct sk_buff *skb, |
06ff4aa2 | 32 | int ttl_check, unsigned char f_ttl) |
bfb15f2a | 33 | { |
a218dc82 | 34 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); |
bfb15f2a | 35 | const struct iphdr *ip = ip_hdr(skb); |
b8d19572 | 36 | const struct in_ifaddr *ifa; |
a218dc82 FFM |
37 | int ret = 0; |
38 | ||
39 | if (ttl_check == NF_OSF_TTL_TRUE) | |
40 | return ip->ttl == f_ttl; | |
41 | if (ttl_check == NF_OSF_TTL_NOCHECK) | |
42 | return 1; | |
43 | else if (ip->ttl <= f_ttl) | |
44 | return 1; | |
45 | ||
b8d19572 | 46 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
a218dc82 FFM |
47 | if (inet_ifa_match(ip->saddr, ifa)) { |
48 | ret = (ip->ttl == f_ttl); | |
49 | break; | |
bfb15f2a FFM |
50 | } |
51 | } | |
52 | ||
a218dc82 | 53 | return ret; |
bfb15f2a FFM |
54 | } |
55 | ||
31a9c292 PNA |
56 | struct nf_osf_hdr_ctx { |
57 | bool df; | |
58 | u16 window; | |
59 | u16 totlen; | |
60 | const unsigned char *optp; | |
61 | unsigned int optsize; | |
62 | }; | |
63 | ||
06ff4aa2 PNA |
64 | static bool nf_osf_match_one(const struct sk_buff *skb, |
65 | const struct nf_osf_user_finger *f, | |
31a9c292 PNA |
66 | int ttl_check, |
67 | struct nf_osf_hdr_ctx *ctx) | |
06ff4aa2 | 68 | { |
1a6a0951 | 69 | const __u8 *optpinit = ctx->optp; |
06ff4aa2 PNA |
70 | unsigned int check_WSS = 0; |
71 | int fmatch = FMATCH_WRONG; | |
72 | int foptsize, optnum; | |
73 | u16 mss = 0; | |
74 | ||
31a9c292 | 75 | if (ctx->totlen != f->ss || !nf_osf_ttl(skb, ttl_check, f->ttl)) |
06ff4aa2 PNA |
76 | return false; |
77 | ||
78 | /* | |
79 | * Should not happen if userspace parser was written correctly. | |
80 | */ | |
81 | if (f->wss.wc >= OSF_WSS_MAX) | |
82 | return false; | |
83 | ||
84 | /* Check options */ | |
85 | ||
86 | foptsize = 0; | |
87 | for (optnum = 0; optnum < f->opt_num; ++optnum) | |
88 | foptsize += f->opt[optnum].length; | |
89 | ||
90 | if (foptsize > MAX_IPOPTLEN || | |
31a9c292 PNA |
91 | ctx->optsize > MAX_IPOPTLEN || |
92 | ctx->optsize != foptsize) | |
06ff4aa2 PNA |
93 | return false; |
94 | ||
95 | check_WSS = f->wss.wc; | |
96 | ||
97 | for (optnum = 0; optnum < f->opt_num; ++optnum) { | |
31a9c292 | 98 | if (f->opt[optnum].kind == *ctx->optp) { |
06ff4aa2 | 99 | __u32 len = f->opt[optnum].length; |
31a9c292 | 100 | const __u8 *optend = ctx->optp + len; |
06ff4aa2 PNA |
101 | |
102 | fmatch = FMATCH_OK; | |
103 | ||
31a9c292 | 104 | switch (*ctx->optp) { |
06ff4aa2 | 105 | case OSFOPT_MSS: |
31a9c292 | 106 | mss = ctx->optp[3]; |
06ff4aa2 | 107 | mss <<= 8; |
31a9c292 | 108 | mss |= ctx->optp[2]; |
06ff4aa2 PNA |
109 | |
110 | mss = ntohs((__force __be16)mss); | |
111 | break; | |
112 | case OSFOPT_TS: | |
113 | break; | |
114 | } | |
115 | ||
31a9c292 | 116 | ctx->optp = optend; |
06ff4aa2 PNA |
117 | } else |
118 | fmatch = FMATCH_OPT_WRONG; | |
119 | ||
120 | if (fmatch != FMATCH_OK) | |
121 | break; | |
122 | } | |
123 | ||
124 | if (fmatch != FMATCH_OPT_WRONG) { | |
125 | fmatch = FMATCH_WRONG; | |
126 | ||
127 | switch (check_WSS) { | |
128 | case OSF_WSS_PLAIN: | |
31a9c292 | 129 | if (f->wss.val == 0 || ctx->window == f->wss.val) |
06ff4aa2 PNA |
130 | fmatch = FMATCH_OK; |
131 | break; | |
132 | case OSF_WSS_MSS: | |
133 | /* | |
134 | * Some smart modems decrease mangle MSS to | |
135 | * SMART_MSS_2, so we check standard, decreased | |
136 | * and the one provided in the fingerprint MSS | |
137 | * values. | |
138 | */ | |
139 | #define SMART_MSS_1 1460 | |
140 | #define SMART_MSS_2 1448 | |
31a9c292 PNA |
141 | if (ctx->window == f->wss.val * mss || |
142 | ctx->window == f->wss.val * SMART_MSS_1 || | |
143 | ctx->window == f->wss.val * SMART_MSS_2) | |
06ff4aa2 PNA |
144 | fmatch = FMATCH_OK; |
145 | break; | |
146 | case OSF_WSS_MTU: | |
31a9c292 PNA |
147 | if (ctx->window == f->wss.val * (mss + 40) || |
148 | ctx->window == f->wss.val * (SMART_MSS_1 + 40) || | |
149 | ctx->window == f->wss.val * (SMART_MSS_2 + 40)) | |
06ff4aa2 PNA |
150 | fmatch = FMATCH_OK; |
151 | break; | |
152 | case OSF_WSS_MODULO: | |
31a9c292 | 153 | if ((ctx->window % f->wss.val) == 0) |
06ff4aa2 PNA |
154 | fmatch = FMATCH_OK; |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
1a6a0951 FFM |
159 | if (fmatch != FMATCH_OK) |
160 | ctx->optp = optpinit; | |
161 | ||
06ff4aa2 PNA |
162 | return fmatch == FMATCH_OK; |
163 | } | |
164 | ||
31a9c292 PNA |
165 | static const struct tcphdr *nf_osf_hdr_ctx_init(struct nf_osf_hdr_ctx *ctx, |
166 | const struct sk_buff *skb, | |
167 | const struct iphdr *ip, | |
c165d57b AB |
168 | unsigned char *opts, |
169 | struct tcphdr *_tcph) | |
31a9c292 PNA |
170 | { |
171 | const struct tcphdr *tcp; | |
31a9c292 | 172 | |
c165d57b | 173 | tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), _tcph); |
31a9c292 PNA |
174 | if (!tcp) |
175 | return NULL; | |
176 | ||
177 | if (!tcp->syn) | |
178 | return NULL; | |
179 | ||
180 | ctx->totlen = ntohs(ip->tot_len); | |
181 | ctx->df = ntohs(ip->frag_off) & IP_DF; | |
182 | ctx->window = ntohs(tcp->window); | |
183 | ||
184 | if (tcp->doff * 4 > sizeof(struct tcphdr)) { | |
185 | ctx->optsize = tcp->doff * 4 - sizeof(struct tcphdr); | |
186 | ||
187 | ctx->optp = skb_header_pointer(skb, ip_hdrlen(skb) + | |
188 | sizeof(struct tcphdr), ctx->optsize, opts); | |
5e024c32 PNA |
189 | if (!ctx->optp) |
190 | return NULL; | |
31a9c292 PNA |
191 | } |
192 | ||
193 | return tcp; | |
194 | } | |
195 | ||
bfb15f2a FFM |
196 | bool |
197 | nf_osf_match(const struct sk_buff *skb, u_int8_t family, | |
198 | int hooknum, struct net_device *in, struct net_device *out, | |
199 | const struct nf_osf_info *info, struct net *net, | |
200 | const struct list_head *nf_osf_fingers) | |
201 | { | |
bfb15f2a FFM |
202 | const struct iphdr *ip = ip_hdr(skb); |
203 | const struct nf_osf_user_finger *f; | |
204 | unsigned char opts[MAX_IPOPTLEN]; | |
205 | const struct nf_osf_finger *kf; | |
06ff4aa2 PNA |
206 | int fcount = 0, ttl_check; |
207 | int fmatch = FMATCH_WRONG; | |
31a9c292 | 208 | struct nf_osf_hdr_ctx ctx; |
bfb15f2a | 209 | const struct tcphdr *tcp; |
c165d57b | 210 | struct tcphdr _tcph; |
bfb15f2a | 211 | |
31a9c292 | 212 | memset(&ctx, 0, sizeof(ctx)); |
bfb15f2a | 213 | |
c165d57b | 214 | tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts, &_tcph); |
31a9c292 | 215 | if (!tcp) |
bfb15f2a FFM |
216 | return false; |
217 | ||
a218dc82 | 218 | ttl_check = (info->flags & NF_OSF_TTL) ? info->ttl : 0; |
06ff4aa2 | 219 | |
31a9c292 | 220 | list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) { |
bfb15f2a FFM |
221 | |
222 | f = &kf->finger; | |
223 | ||
224 | if (!(info->flags & NF_OSF_LOG) && strcmp(info->genre, f->genre)) | |
225 | continue; | |
226 | ||
31a9c292 | 227 | if (!nf_osf_match_one(skb, f, ttl_check, &ctx)) |
bfb15f2a FFM |
228 | continue; |
229 | ||
06ff4aa2 | 230 | fmatch = FMATCH_OK; |
bfb15f2a FFM |
231 | |
232 | fcount++; | |
233 | ||
234 | if (info->flags & NF_OSF_LOG) | |
235 | nf_log_packet(net, family, hooknum, skb, | |
236 | in, out, NULL, | |
237 | "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n", | |
238 | f->genre, f->version, f->subtype, | |
239 | &ip->saddr, ntohs(tcp->source), | |
240 | &ip->daddr, ntohs(tcp->dest), | |
241 | f->ttl - ip->ttl); | |
242 | ||
243 | if ((info->flags & NF_OSF_LOG) && | |
244 | info->loglevel == NF_OSF_LOGLEVEL_FIRST) | |
245 | break; | |
246 | } | |
247 | ||
248 | if (!fcount && (info->flags & NF_OSF_LOG)) | |
249 | nf_log_packet(net, family, hooknum, skb, in, out, NULL, | |
250 | "Remote OS is not known: %pI4:%u -> %pI4:%u\n", | |
251 | &ip->saddr, ntohs(tcp->source), | |
252 | &ip->daddr, ntohs(tcp->dest)); | |
253 | ||
254 | if (fcount) | |
255 | fmatch = FMATCH_OK; | |
256 | ||
257 | return fmatch == FMATCH_OK; | |
258 | } | |
259 | EXPORT_SYMBOL_GPL(nf_osf_match); | |
260 | ||
22c7652c FFM |
261 | bool nf_osf_find(const struct sk_buff *skb, |
262 | const struct list_head *nf_osf_fingers, | |
263 | const int ttl_check, struct nf_osf_data *data) | |
51c23b47 PNA |
264 | { |
265 | const struct iphdr *ip = ip_hdr(skb); | |
266 | const struct nf_osf_user_finger *f; | |
267 | unsigned char opts[MAX_IPOPTLEN]; | |
268 | const struct nf_osf_finger *kf; | |
269 | struct nf_osf_hdr_ctx ctx; | |
270 | const struct tcphdr *tcp; | |
c165d57b | 271 | struct tcphdr _tcph; |
559c36c5 | 272 | bool found = false; |
51c23b47 PNA |
273 | |
274 | memset(&ctx, 0, sizeof(ctx)); | |
275 | ||
c165d57b | 276 | tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts, &_tcph); |
51c23b47 | 277 | if (!tcp) |
22c7652c | 278 | return false; |
51c23b47 PNA |
279 | |
280 | list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) { | |
281 | f = &kf->finger; | |
a218dc82 | 282 | if (!nf_osf_match_one(skb, f, ttl_check, &ctx)) |
51c23b47 PNA |
283 | continue; |
284 | ||
22c7652c FFM |
285 | data->genre = f->genre; |
286 | data->version = f->version; | |
559c36c5 | 287 | found = true; |
51c23b47 PNA |
288 | break; |
289 | } | |
290 | ||
559c36c5 | 291 | return found; |
51c23b47 PNA |
292 | } |
293 | EXPORT_SYMBOL_GPL(nf_osf_find); | |
294 | ||
f9324952 FFM |
295 | static const struct nla_policy nfnl_osf_policy[OSF_ATTR_MAX + 1] = { |
296 | [OSF_ATTR_FINGER] = { .len = sizeof(struct nf_osf_user_finger) }, | |
297 | }; | |
298 | ||
a6555365 PNA |
299 | static int nfnl_osf_add_callback(struct sk_buff *skb, |
300 | const struct nfnl_info *info, | |
301 | const struct nlattr * const osf_attrs[]) | |
f9324952 FFM |
302 | { |
303 | struct nf_osf_user_finger *f; | |
304 | struct nf_osf_finger *kf = NULL, *sf; | |
305 | int err = 0; | |
306 | ||
307 | if (!capable(CAP_NET_ADMIN)) | |
308 | return -EPERM; | |
309 | ||
310 | if (!osf_attrs[OSF_ATTR_FINGER]) | |
311 | return -EINVAL; | |
312 | ||
a6555365 | 313 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
f9324952 FFM |
314 | return -EINVAL; |
315 | ||
316 | f = nla_data(osf_attrs[OSF_ATTR_FINGER]); | |
317 | ||
318 | kf = kmalloc(sizeof(struct nf_osf_finger), GFP_KERNEL); | |
319 | if (!kf) | |
320 | return -ENOMEM; | |
321 | ||
322 | memcpy(&kf->finger, f, sizeof(struct nf_osf_user_finger)); | |
323 | ||
324 | list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) { | |
325 | if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger))) | |
326 | continue; | |
327 | ||
328 | kfree(kf); | |
329 | kf = NULL; | |
330 | ||
a6555365 | 331 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) |
f9324952 FFM |
332 | err = -EEXIST; |
333 | break; | |
334 | } | |
335 | ||
336 | /* | |
337 | * We are protected by nfnl mutex. | |
338 | */ | |
339 | if (kf) | |
340 | list_add_tail_rcu(&kf->finger_entry, &nf_osf_fingers[!!f->df]); | |
341 | ||
342 | return err; | |
343 | } | |
344 | ||
a6555365 PNA |
345 | static int nfnl_osf_remove_callback(struct sk_buff *skb, |
346 | const struct nfnl_info *info, | |
347 | const struct nlattr * const osf_attrs[]) | |
f9324952 FFM |
348 | { |
349 | struct nf_osf_user_finger *f; | |
350 | struct nf_osf_finger *sf; | |
351 | int err = -ENOENT; | |
352 | ||
353 | if (!capable(CAP_NET_ADMIN)) | |
354 | return -EPERM; | |
355 | ||
356 | if (!osf_attrs[OSF_ATTR_FINGER]) | |
357 | return -EINVAL; | |
358 | ||
359 | f = nla_data(osf_attrs[OSF_ATTR_FINGER]); | |
360 | ||
361 | list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) { | |
362 | if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger))) | |
363 | continue; | |
364 | ||
365 | /* | |
366 | * We are protected by nfnl mutex. | |
367 | */ | |
368 | list_del_rcu(&sf->finger_entry); | |
369 | kfree_rcu(sf, rcu_head); | |
370 | ||
371 | err = 0; | |
372 | break; | |
373 | } | |
374 | ||
375 | return err; | |
376 | } | |
377 | ||
378 | static const struct nfnl_callback nfnl_osf_callbacks[OSF_MSG_MAX] = { | |
379 | [OSF_MSG_ADD] = { | |
380 | .call = nfnl_osf_add_callback, | |
50f2db9e | 381 | .type = NFNL_CB_MUTEX, |
f9324952 FFM |
382 | .attr_count = OSF_ATTR_MAX, |
383 | .policy = nfnl_osf_policy, | |
384 | }, | |
385 | [OSF_MSG_REMOVE] = { | |
386 | .call = nfnl_osf_remove_callback, | |
50f2db9e | 387 | .type = NFNL_CB_MUTEX, |
f9324952 FFM |
388 | .attr_count = OSF_ATTR_MAX, |
389 | .policy = nfnl_osf_policy, | |
390 | }, | |
391 | }; | |
392 | ||
393 | static const struct nfnetlink_subsystem nfnl_osf_subsys = { | |
394 | .name = "osf", | |
395 | .subsys_id = NFNL_SUBSYS_OSF, | |
396 | .cb_count = OSF_MSG_MAX, | |
397 | .cb = nfnl_osf_callbacks, | |
398 | }; | |
399 | ||
400 | static int __init nfnl_osf_init(void) | |
401 | { | |
402 | int err = -EINVAL; | |
403 | int i; | |
404 | ||
405 | for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i) | |
406 | INIT_LIST_HEAD(&nf_osf_fingers[i]); | |
407 | ||
408 | err = nfnetlink_subsys_register(&nfnl_osf_subsys); | |
409 | if (err < 0) { | |
410 | pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err); | |
411 | goto err_out_exit; | |
412 | } | |
413 | return 0; | |
414 | ||
415 | err_out_exit: | |
416 | return err; | |
417 | } | |
418 | ||
419 | static void __exit nfnl_osf_fini(void) | |
420 | { | |
421 | struct nf_osf_finger *f; | |
422 | int i; | |
423 | ||
424 | nfnetlink_subsys_unregister(&nfnl_osf_subsys); | |
425 | ||
426 | rcu_read_lock(); | |
427 | for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i) { | |
428 | list_for_each_entry_rcu(f, &nf_osf_fingers[i], finger_entry) { | |
429 | list_del_rcu(&f->finger_entry); | |
430 | kfree_rcu(f, rcu_head); | |
431 | } | |
432 | } | |
433 | rcu_read_unlock(); | |
434 | ||
435 | rcu_barrier(); | |
436 | } | |
437 | ||
438 | module_init(nfnl_osf_init); | |
439 | module_exit(nfnl_osf_fini); | |
440 | ||
bfb15f2a | 441 | MODULE_LICENSE("GPL"); |