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