crypto: s5p-sss - Use AES_BLOCK_SIZE define instead of number
[linux/fpc-iii.git] / net / netfilter / nfnetlink_osf.c
blob6f41dd74729d9ff1b6a8984004174ae1935258e2
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2 #include <linux/module.h>
3 #include <linux/kernel.h>
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>
15 #include <net/ip.h>
16 #include <net/tcp.h>
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>
24 * Indexed by dont-fragment bit.
25 * It is the only constant value in the fingerprint.
27 struct list_head nf_osf_fingers[2];
28 EXPORT_SYMBOL_GPL(nf_osf_fingers);
30 static inline int nf_osf_ttl(const struct sk_buff *skb,
31 int ttl_check, unsigned char f_ttl)
33 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
34 const struct iphdr *ip = ip_hdr(skb);
35 int ret = 0;
37 if (ttl_check == NF_OSF_TTL_TRUE)
38 return ip->ttl == f_ttl;
39 if (ttl_check == NF_OSF_TTL_NOCHECK)
40 return 1;
41 else if (ip->ttl <= f_ttl)
42 return 1;
44 for_ifa(in_dev) {
45 if (inet_ifa_match(ip->saddr, ifa)) {
46 ret = (ip->ttl == f_ttl);
47 break;
51 endfor_ifa(in_dev);
53 return ret;
56 struct nf_osf_hdr_ctx {
57 bool df;
58 u16 window;
59 u16 totlen;
60 const unsigned char *optp;
61 unsigned int optsize;
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)
69 unsigned int check_WSS = 0;
70 int fmatch = FMATCH_WRONG;
71 int foptsize, optnum;
72 u16 mss = 0;
74 if (ctx->totlen != f->ss || !nf_osf_ttl(skb, ttl_check, f->ttl))
75 return false;
78 * Should not happen if userspace parser was written correctly.
80 if (f->wss.wc >= OSF_WSS_MAX)
81 return false;
83 /* Check options */
85 foptsize = 0;
86 for (optnum = 0; optnum < f->opt_num; ++optnum)
87 foptsize += f->opt[optnum].length;
89 if (foptsize > MAX_IPOPTLEN ||
90 ctx->optsize > MAX_IPOPTLEN ||
91 ctx->optsize != foptsize)
92 return false;
94 check_WSS = f->wss.wc;
96 for (optnum = 0; optnum < f->opt_num; ++optnum) {
97 if (f->opt[optnum].kind == *ctx->optp) {
98 __u32 len = f->opt[optnum].length;
99 const __u8 *optend = ctx->optp + len;
101 fmatch = FMATCH_OK;
103 switch (*ctx->optp) {
104 case OSFOPT_MSS:
105 mss = ctx->optp[3];
106 mss <<= 8;
107 mss |= ctx->optp[2];
109 mss = ntohs((__force __be16)mss);
110 break;
111 case OSFOPT_TS:
112 break;
115 ctx->optp = optend;
116 } else
117 fmatch = FMATCH_OPT_WRONG;
119 if (fmatch != FMATCH_OK)
120 break;
123 if (fmatch != FMATCH_OPT_WRONG) {
124 fmatch = FMATCH_WRONG;
126 switch (check_WSS) {
127 case OSF_WSS_PLAIN:
128 if (f->wss.val == 0 || ctx->window == f->wss.val)
129 fmatch = FMATCH_OK;
130 break;
131 case OSF_WSS_MSS:
133 * Some smart modems decrease mangle MSS to
134 * SMART_MSS_2, so we check standard, decreased
135 * and the one provided in the fingerprint MSS
136 * values.
138 #define SMART_MSS_1 1460
139 #define SMART_MSS_2 1448
140 if (ctx->window == f->wss.val * mss ||
141 ctx->window == f->wss.val * SMART_MSS_1 ||
142 ctx->window == f->wss.val * SMART_MSS_2)
143 fmatch = FMATCH_OK;
144 break;
145 case OSF_WSS_MTU:
146 if (ctx->window == f->wss.val * (mss + 40) ||
147 ctx->window == f->wss.val * (SMART_MSS_1 + 40) ||
148 ctx->window == f->wss.val * (SMART_MSS_2 + 40))
149 fmatch = FMATCH_OK;
150 break;
151 case OSF_WSS_MODULO:
152 if ((ctx->window % f->wss.val) == 0)
153 fmatch = FMATCH_OK;
154 break;
158 return fmatch == FMATCH_OK;
161 static const struct tcphdr *nf_osf_hdr_ctx_init(struct nf_osf_hdr_ctx *ctx,
162 const struct sk_buff *skb,
163 const struct iphdr *ip,
164 unsigned char *opts)
166 const struct tcphdr *tcp;
167 struct tcphdr _tcph;
169 tcp = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(struct tcphdr), &_tcph);
170 if (!tcp)
171 return NULL;
173 if (!tcp->syn)
174 return NULL;
176 ctx->totlen = ntohs(ip->tot_len);
177 ctx->df = ntohs(ip->frag_off) & IP_DF;
178 ctx->window = ntohs(tcp->window);
180 if (tcp->doff * 4 > sizeof(struct tcphdr)) {
181 ctx->optsize = tcp->doff * 4 - sizeof(struct tcphdr);
183 ctx->optp = skb_header_pointer(skb, ip_hdrlen(skb) +
184 sizeof(struct tcphdr), ctx->optsize, opts);
187 return tcp;
190 bool
191 nf_osf_match(const struct sk_buff *skb, u_int8_t family,
192 int hooknum, struct net_device *in, struct net_device *out,
193 const struct nf_osf_info *info, struct net *net,
194 const struct list_head *nf_osf_fingers)
196 const struct iphdr *ip = ip_hdr(skb);
197 const struct nf_osf_user_finger *f;
198 unsigned char opts[MAX_IPOPTLEN];
199 const struct nf_osf_finger *kf;
200 int fcount = 0, ttl_check;
201 int fmatch = FMATCH_WRONG;
202 struct nf_osf_hdr_ctx ctx;
203 const struct tcphdr *tcp;
205 memset(&ctx, 0, sizeof(ctx));
207 tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts);
208 if (!tcp)
209 return false;
211 ttl_check = (info->flags & NF_OSF_TTL) ? info->ttl : 0;
213 list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) {
215 f = &kf->finger;
217 if (!(info->flags & NF_OSF_LOG) && strcmp(info->genre, f->genre))
218 continue;
220 if (!nf_osf_match_one(skb, f, ttl_check, &ctx))
221 continue;
223 fmatch = FMATCH_OK;
225 fcount++;
227 if (info->flags & NF_OSF_LOG)
228 nf_log_packet(net, family, hooknum, skb,
229 in, out, NULL,
230 "%s [%s:%s] : %pI4:%d -> %pI4:%d hops=%d\n",
231 f->genre, f->version, f->subtype,
232 &ip->saddr, ntohs(tcp->source),
233 &ip->daddr, ntohs(tcp->dest),
234 f->ttl - ip->ttl);
236 if ((info->flags & NF_OSF_LOG) &&
237 info->loglevel == NF_OSF_LOGLEVEL_FIRST)
238 break;
241 if (!fcount && (info->flags & NF_OSF_LOG))
242 nf_log_packet(net, family, hooknum, skb, in, out, NULL,
243 "Remote OS is not known: %pI4:%u -> %pI4:%u\n",
244 &ip->saddr, ntohs(tcp->source),
245 &ip->daddr, ntohs(tcp->dest));
247 if (fcount)
248 fmatch = FMATCH_OK;
250 return fmatch == FMATCH_OK;
252 EXPORT_SYMBOL_GPL(nf_osf_match);
254 const char *nf_osf_find(const struct sk_buff *skb,
255 const struct list_head *nf_osf_fingers,
256 const int ttl_check)
258 const struct iphdr *ip = ip_hdr(skb);
259 const struct nf_osf_user_finger *f;
260 unsigned char opts[MAX_IPOPTLEN];
261 const struct nf_osf_finger *kf;
262 struct nf_osf_hdr_ctx ctx;
263 const struct tcphdr *tcp;
264 const char *genre = NULL;
266 memset(&ctx, 0, sizeof(ctx));
268 tcp = nf_osf_hdr_ctx_init(&ctx, skb, ip, opts);
269 if (!tcp)
270 return NULL;
272 list_for_each_entry_rcu(kf, &nf_osf_fingers[ctx.df], finger_entry) {
273 f = &kf->finger;
274 if (!nf_osf_match_one(skb, f, ttl_check, &ctx))
275 continue;
277 genre = f->genre;
278 break;
281 return genre;
283 EXPORT_SYMBOL_GPL(nf_osf_find);
285 static const struct nla_policy nfnl_osf_policy[OSF_ATTR_MAX + 1] = {
286 [OSF_ATTR_FINGER] = { .len = sizeof(struct nf_osf_user_finger) },
289 static int nfnl_osf_add_callback(struct net *net, struct sock *ctnl,
290 struct sk_buff *skb, const struct nlmsghdr *nlh,
291 const struct nlattr * const osf_attrs[],
292 struct netlink_ext_ack *extack)
294 struct nf_osf_user_finger *f;
295 struct nf_osf_finger *kf = NULL, *sf;
296 int err = 0;
298 if (!capable(CAP_NET_ADMIN))
299 return -EPERM;
301 if (!osf_attrs[OSF_ATTR_FINGER])
302 return -EINVAL;
304 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
305 return -EINVAL;
307 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
309 kf = kmalloc(sizeof(struct nf_osf_finger), GFP_KERNEL);
310 if (!kf)
311 return -ENOMEM;
313 memcpy(&kf->finger, f, sizeof(struct nf_osf_user_finger));
315 list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) {
316 if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger)))
317 continue;
319 kfree(kf);
320 kf = NULL;
322 if (nlh->nlmsg_flags & NLM_F_EXCL)
323 err = -EEXIST;
324 break;
328 * We are protected by nfnl mutex.
330 if (kf)
331 list_add_tail_rcu(&kf->finger_entry, &nf_osf_fingers[!!f->df]);
333 return err;
336 static int nfnl_osf_remove_callback(struct net *net, struct sock *ctnl,
337 struct sk_buff *skb,
338 const struct nlmsghdr *nlh,
339 const struct nlattr * const osf_attrs[],
340 struct netlink_ext_ack *extack)
342 struct nf_osf_user_finger *f;
343 struct nf_osf_finger *sf;
344 int err = -ENOENT;
346 if (!capable(CAP_NET_ADMIN))
347 return -EPERM;
349 if (!osf_attrs[OSF_ATTR_FINGER])
350 return -EINVAL;
352 f = nla_data(osf_attrs[OSF_ATTR_FINGER]);
354 list_for_each_entry(sf, &nf_osf_fingers[!!f->df], finger_entry) {
355 if (memcmp(&sf->finger, f, sizeof(struct nf_osf_user_finger)))
356 continue;
359 * We are protected by nfnl mutex.
361 list_del_rcu(&sf->finger_entry);
362 kfree_rcu(sf, rcu_head);
364 err = 0;
365 break;
368 return err;
371 static const struct nfnl_callback nfnl_osf_callbacks[OSF_MSG_MAX] = {
372 [OSF_MSG_ADD] = {
373 .call = nfnl_osf_add_callback,
374 .attr_count = OSF_ATTR_MAX,
375 .policy = nfnl_osf_policy,
377 [OSF_MSG_REMOVE] = {
378 .call = nfnl_osf_remove_callback,
379 .attr_count = OSF_ATTR_MAX,
380 .policy = nfnl_osf_policy,
384 static const struct nfnetlink_subsystem nfnl_osf_subsys = {
385 .name = "osf",
386 .subsys_id = NFNL_SUBSYS_OSF,
387 .cb_count = OSF_MSG_MAX,
388 .cb = nfnl_osf_callbacks,
391 static int __init nfnl_osf_init(void)
393 int err = -EINVAL;
394 int i;
396 for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i)
397 INIT_LIST_HEAD(&nf_osf_fingers[i]);
399 err = nfnetlink_subsys_register(&nfnl_osf_subsys);
400 if (err < 0) {
401 pr_err("Failed to register OSF nsfnetlink helper (%d)\n", err);
402 goto err_out_exit;
404 return 0;
406 err_out_exit:
407 return err;
410 static void __exit nfnl_osf_fini(void)
412 struct nf_osf_finger *f;
413 int i;
415 nfnetlink_subsys_unregister(&nfnl_osf_subsys);
417 rcu_read_lock();
418 for (i = 0; i < ARRAY_SIZE(nf_osf_fingers); ++i) {
419 list_for_each_entry_rcu(f, &nf_osf_fingers[i], finger_entry) {
420 list_del_rcu(&f->finger_entry);
421 kfree_rcu(f, rcu_head);
424 rcu_read_unlock();
426 rcu_barrier();
429 module_init(nfnl_osf_init);
430 module_exit(nfnl_osf_fini);
432 MODULE_LICENSE("GPL");