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