[NETFILTER]: replace list_for_each with list_for_each_entry
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / xt_u32.c
CommitLineData
1b50b8a3
JE
1/*
2 * xt_u32 - kernel module to match u32 packet content
3 *
4 * Original author: Don Cohen <don@isis.cs3-inc.com>
ba5dc275
JE
5 * (C) CC Computer Consultants GmbH, 2007
6 * Contact: <jengelh@computergmbh.de>
1b50b8a3
JE
7 */
8
9#include <linux/module.h>
10#include <linux/moduleparam.h>
11#include <linux/spinlock.h>
12#include <linux/skbuff.h>
13#include <linux/types.h>
14#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter/xt_u32.h>
16
17static bool u32_match_it(const struct xt_u32 *data,
18 const struct sk_buff *skb)
19{
20 const struct xt_u32_test *ct;
21 unsigned int testind;
22 unsigned int nnums;
23 unsigned int nvals;
24 unsigned int i;
a34c4589 25 __be32 n;
1b50b8a3
JE
26 u_int32_t pos;
27 u_int32_t val;
28 u_int32_t at;
29 int ret;
30
31 /*
32 * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
33 * (=IPv4 and (TCP or UDP)). Outer loop runs over the "&&" operands.
34 */
35 for (testind = 0; testind < data->ntests; ++testind) {
36 ct = &data->tests[testind];
37 at = 0;
38 pos = ct->location[0].number;
39
35019539 40 if (skb->len < 4 || pos > skb->len - 4)
1b50b8a3
JE
41 return false;
42
a34c4589 43 ret = skb_copy_bits(skb, pos, &n, sizeof(n));
1b50b8a3 44 BUG_ON(ret < 0);
a34c4589 45 val = ntohl(n);
1b50b8a3
JE
46 nnums = ct->nnums;
47
48 /* Inner loop runs over "&", "<<", ">>" and "@" operands */
49 for (i = 1; i < nnums; ++i) {
50 u_int32_t number = ct->location[i].number;
51 switch (ct->location[i].nextop) {
52 case XT_U32_AND:
53 val &= number;
54 break;
55 case XT_U32_LEFTSH:
56 val <<= number;
57 break;
58 case XT_U32_RIGHTSH:
59 val >>= number;
60 break;
61 case XT_U32_AT:
62 if (at + val < at)
63 return false;
64 at += val;
65 pos = number;
66 if (at + 4 < at || skb->len < at + 4 ||
67 pos > skb->len - at - 4)
68 return false;
69
a34c4589
AV
70 ret = skb_copy_bits(skb, at + pos, &n,
71 sizeof(n));
1b50b8a3 72 BUG_ON(ret < 0);
a34c4589 73 val = ntohl(n);
1b50b8a3
JE
74 break;
75 }
76 }
77
78 /* Run over the "," and ":" operands */
79 nvals = ct->nvalues;
80 for (i = 0; i < nvals; ++i)
81 if (ct->value[i].min <= val && val <= ct->value[i].max)
82 break;
83
84 if (i >= ct->nvalues)
85 return false;
86 }
87
88 return true;
89}
90
91static bool u32_match(const struct sk_buff *skb,
92 const struct net_device *in,
93 const struct net_device *out,
94 const struct xt_match *match, const void *matchinfo,
95 int offset, unsigned int protoff, bool *hotdrop)
96{
97 const struct xt_u32 *data = matchinfo;
98 bool ret;
99
100 ret = u32_match_it(data, skb);
101 return ret ^ data->invert;
102}
103
9f15c530 104static struct xt_match u32_reg[] __read_mostly = {
1b50b8a3
JE
105 {
106 .name = "u32",
107 .family = AF_INET,
108 .match = u32_match,
109 .matchsize = sizeof(struct xt_u32),
110 .me = THIS_MODULE,
111 },
112 {
113 .name = "u32",
114 .family = AF_INET6,
115 .match = u32_match,
116 .matchsize = sizeof(struct xt_u32),
117 .me = THIS_MODULE,
118 },
119};
120
121static int __init xt_u32_init(void)
122{
123 return xt_register_matches(u32_reg, ARRAY_SIZE(u32_reg));
124}
125
126static void __exit xt_u32_exit(void)
127{
128 xt_unregister_matches(u32_reg, ARRAY_SIZE(u32_reg));
129}
130
131module_init(xt_u32_init);
132module_exit(xt_u32_exit);
ba5dc275 133MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
1b50b8a3
JE
134MODULE_DESCRIPTION("netfilter u32 match module");
135MODULE_LICENSE("GPL");
136MODULE_ALIAS("ipt_u32");
137MODULE_ALIAS("ip6t_u32");