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