1 // SPDX-License-Identifier: GPL-2.0-only
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>
22 #include <linux/netfilter/nfnetlink_osf.h>
23
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
nf_osf_ttl(const struct sk_buff * skb,int ttl_check,unsigned char f_ttl)31 static inline int nf_osf_ttl(const struct sk_buff *skb,
32 int ttl_check, unsigned char f_ttl)
33 {
34 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
35 const struct iphdr *ip = ip_hdr(skb);
36 const struct in_ifaddr *ifa;
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
46 in_dev_for_each_ifa_rcu(ifa, in_dev) {
47 if (inet_ifa_match(ip->saddr, ifa)) {
48 ret = (ip->ttl == f_ttl);
49 break;
50 }
51 }
52
53 return ret;
54 }
55
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
nf_osf_match_one(const struct sk_buff * skb,const struct nf_osf_user_finger * f,int ttl_check,struct nf_osf_hdr_ctx * ctx)64 static bool nf_osf_match_one(const struct sk_buff *skb,
65 const struct nf_osf_user_finger *f,
66 int ttl_check,
67 struct nf_osf_hdr_ctx *ctx)
68 {
69 const __u8 *optpinit = ctx->optp;
70 unsigned int check_WSS = 0;
71 int fmatch = FMATCH_WRONG;
72 int foptsize, optnum;
73 u16 mss = 0;
74
75 if (ctx->totlen != f->ss || !nf_osf_ttl(skb, ttl_check, f->ttl))
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 ||
91 ctx->optsize > MAX_IPOPTLEN ||
92 ctx->optsize != foptsize)
93 return false;
94
95 check_WSS = f->wss.wc;
96
97 for (optnum = 0; optnum < f->opt_num; ++optnum) {
98 if (f->opt[optnum].kind == *ctx->optp) {
99 __u32 len = f->opt[optnum].length;
100 const __u8 *optend = ctx->optp + len;
101
102 fmatch = FMATCH_OK;
103
104 switch (*ctx->optp) {
105 case OSFOPT_MSS:
106 mss = ctx->optp[3];
107 mss <<= 8;
108 mss |= ctx->optp[2];
109
110 mss = ntohs((__force __be16)mss);
111 break;
112 case OSFOPT_TS:
113 break;
114 }
115
116 ctx->optp = optend;
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:
129 if (f->wss.val == 0 || ctx->window == f->wss.val)
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
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)
144 fmatch = FMATCH_OK;
145 break;
146 case OSF_WSS_MTU:
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))
150 fmatch = FMATCH_OK;
151 break;
152 case OSF_WSS_MODULO:
153 if ((ctx->window % f->wss.val) == 0)
154 fmatch = FMATCH_OK;
155 break;
156 }
157 }
158
159 if (fmatch != FMATCH_OK)
160 ctx->optp = optpinit;
161
162 return fmatch == FMATCH_OK;
163 }
164
nf_osf_hdr_ctx_init(struct nf_osf_hdr_ctx * ctx,const struct sk_buff * skb,const struct iphdr * ip,unsigned char * opts,struct tcphdr * _tcph)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,
168 unsigned char *opts,
169 struct tcphdr *_tcph)
170 {
171 const struct tcphdr *tcp;
172
173 tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), _tcph);
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);
189 }
190
191 return tcp;
192 }
193
194 bool
nf_osf_match(const struct sk_buff * skb,u_int8_t family,int hooknum,struct net_device * in,struct net_device * out,const struct nf_osf_info * info,struct net * net,const struct list_head * nf_osf_fingers)195 nf_osf_match(const struct sk_buff *skb, u_int8_t family,
196 int hooknum, struct net_device *in, struct net_device *out,
197 const struct nf_osf_info *info, struct net *net,
198 const struct list_head *nf_osf_fingers)
199 {
200 const struct iphdr *ip = ip_hdr(skb);
201 const struct nf_osf_user_finger *f;
202 unsigned char opts[MAX_IPOPTLEN];
203 const struct nf_osf_finger *kf;
204 int fcount = 0, ttl_check;
205 int fmatch = FMATCH_WRONG;
206 struct nf_osf_hdr_ctx ctx;
207 const struct tcphdr *tcp;
208 struct tcphdr _tcph;
209
210 memset(&ctx, 0, sizeof(ctx));
211
212 tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts, &_tcph);
213 if (!tcp)
214 return false;
215
216 ttl_check = (info->flags & NF_OSF_TTL) ? info->ttl : 0;
217
218 list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) {
219
220 f = &kf->finger;
221
222 if (!(info->flags & NF_OSF_LOG) && strcmp(info->genre, f->genre))
223 continue;
224
225 if (!nf_osf_match_one(skb, f, ttl_check, &ctx))
226 continue;
227
228 fmatch = FMATCH_OK;
229
230 fcount++;
231
232 if (info->flags & NF_OSF_LOG)
233 nf_log_packet(net, family, hooknum, skb,
234 in, out, NULL,
235 "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n",
236 f->genre, f->version, f->subtype,
237 &ip->saddr, ntohs(tcp->source),
238 &ip->daddr, ntohs(tcp->dest),
239 f->ttl - ip->ttl);
240
241 if ((info->flags & NF_OSF_LOG) &&
242 info->loglevel == NF_OSF_LOGLEVEL_FIRST)
243 break;
244 }
245
246 if (!fcount && (info->flags & NF_OSF_LOG))
247 nf_log_packet(net, family, hooknum, skb, in, out, NULL,
248 "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
249 &ip->saddr, ntohs(tcp->source),
250 &ip->daddr, ntohs(tcp->dest));
251
252 if (fcount)
253 fmatch = FMATCH_OK;
254
255 return fmatch == FMATCH_OK;
256 }
257 EXPORT_SYMBOL_GPL(nf_osf_match);
258
nf_osf_find(const struct sk_buff * skb,const struct list_head * nf_osf_fingers,const int ttl_check,struct nf_osf_data * data)259 bool nf_osf_find(const struct sk_buff *skb,
260 const struct list_head *nf_osf_fingers,
261 const int ttl_check, struct nf_osf_data *data)
262 {
263 const struct iphdr *ip = ip_hdr(skb);
264 const struct nf_osf_user_finger *f;
265 unsigned char opts[MAX_IPOPTLEN];
266 const struct nf_osf_finger *kf;
267 struct nf_osf_hdr_ctx ctx;
268 const struct tcphdr *tcp;
269 struct tcphdr _tcph;
270
271 memset(&ctx, 0, sizeof(ctx));
272
273 tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts, &_tcph);
274 if (!tcp)
275 return false;
276
277 list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) {
278 f = &kf->finger;
279 if (!nf_osf_match_one(skb, f, ttl_check, &ctx))
280 continue;
281
282 data->genre = f->genre;
283 data->version = f->version;
284 break;
285 }
286
287 return true;
288 }
289 EXPORT_SYMBOL_GPL(nf_osf_find);
290
291 static const struct nla_policy nfnl_osf_policy[OSF_ATTR_MAX + 1] = {
292 [OSF_ATTR_FINGER] = { .len = sizeof(struct nf_osf_user_finger) },
293 };
294
nfnl_osf_add_callback(struct net * net,struct sock * ctnl,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const osf_attrs[],struct netlink_ext_ack * extack)295 static int nfnl_osf_add_callback(struct net *net, struct sock *ctnl,
296 struct sk_buff *skb, const struct nlmsghdr *nlh,
297 const struct nlattr * const osf_attrs[],
298 struct netlink_ext_ack *extack)
299 {
300 struct nf_osf_user_finger *f;
301 struct nf_osf_finger *kf = NULL, *sf;
302 int err = 0;
303
304 if (!capable(CAP_NET_ADMIN))
305 return -EPERM;
306
307 if (!osf_attrs[OSF_ATTR_FINGER])
308 return -EINVAL;
309
310 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
311 return -EINVAL;
312
313 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
314
315 kf = kmalloc(sizeof(struct nf_osf_finger), GFP_KERNEL);
316 if (!kf)
317 return -ENOMEM;
318
319 memcpy(&kf->finger, f, sizeof(struct nf_osf_user_finger));
320
321 list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) {
322 if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger)))
323 continue;
324
325 kfree(kf);
326 kf = NULL;
327
328 if (nlh->nlmsg_flags & NLM_F_EXCL)
329 err = -EEXIST;
330 break;
331 }
332
333 /*
334 * We are protected by nfnl mutex.
335 */
336 if (kf)
337 list_add_tail_rcu(&kf->finger_entry, &nf_osf_fingers[!!f->df]);
338
339 return err;
340 }
341
nfnl_osf_remove_callback(struct net * net,struct sock * ctnl,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const osf_attrs[],struct netlink_ext_ack * extack)342 static int nfnl_osf_remove_callback(struct net *net, struct sock *ctnl,
343 struct sk_buff *skb,
344 const struct nlmsghdr *nlh,
345 const struct nlattr * const osf_attrs[],
346 struct netlink_ext_ack *extack)
347 {
348 struct nf_osf_user_finger *f;
349 struct nf_osf_finger *sf;
350 int err = -ENOENT;
351
352 if (!capable(CAP_NET_ADMIN))
353 return -EPERM;
354
355 if (!osf_attrs[OSF_ATTR_FINGER])
356 return -EINVAL;
357
358 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
359
360 list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) {
361 if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger)))
362 continue;
363
364 /*
365 * We are protected by nfnl mutex.
366 */
367 list_del_rcu(&sf->finger_entry);
368 kfree_rcu(sf, rcu_head);
369
370 err = 0;
371 break;
372 }
373
374 return err;
375 }
376
377 static const struct nfnl_callback nfnl_osf_callbacks[OSF_MSG_MAX] = {
378 [OSF_MSG_ADD] = {
379 .call = nfnl_osf_add_callback,
380 .attr_count = OSF_ATTR_MAX,
381 .policy = nfnl_osf_policy,
382 },
383 [OSF_MSG_REMOVE] = {
384 .call = nfnl_osf_remove_callback,
385 .attr_count = OSF_ATTR_MAX,
386 .policy = nfnl_osf_policy,
387 },
388 };
389
390 static const struct nfnetlink_subsystem nfnl_osf_subsys = {
391 .name = "osf",
392 .subsys_id = NFNL_SUBSYS_OSF,
393 .cb_count = OSF_MSG_MAX,
394 .cb = nfnl_osf_callbacks,
395 };
396
nfnl_osf_init(void)397 static int __init nfnl_osf_init(void)
398 {
399 int err = -EINVAL;
400 int i;
401
402 for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i)
403 INIT_LIST_HEAD(&nf_osf_fingers[i]);
404
405 err = nfnetlink_subsys_register(&nfnl_osf_subsys);
406 if (err < 0) {
407 pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err);
408 goto err_out_exit;
409 }
410 return 0;
411
412 err_out_exit:
413 return err;
414 }
415
nfnl_osf_fini(void)416 static void __exit nfnl_osf_fini(void)
417 {
418 struct nf_osf_finger *f;
419 int i;
420
421 nfnetlink_subsys_unregister(&nfnl_osf_subsys);
422
423 rcu_read_lock();
424 for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i) {
425 list_for_each_entry_rcu(f, &nf_osf_fingers[i], finger_entry) {
426 list_del_rcu(&f->finger_entry);
427 kfree_rcu(f, rcu_head);
428 }
429 }
430 rcu_read_unlock();
431
432 rcu_barrier();
433 }
434
435 module_init(nfnl_osf_init);
436 module_exit(nfnl_osf_fini);
437
438 MODULE_LICENSE("GPL");
439