1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * xt_u32 - kernel module to match u32 packet content
4 *
5 * Original author: Don Cohen <don@isis.cs3-inc.com>
6 * (C) CC Computer Consultants GmbH, 2007
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
u32_match_it(const struct xt_u32 * data,const struct sk_buff * skb)17 static int 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;
25 __be32 n;
26 u_int32_t pos;
27 u_int32_t val;
28 u_int32_t at;
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
42 if (skb_copy_bits(skb, pos, &n, sizeof(n)) < 0)
43 return -1;
44
45 val = ntohl(n);
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
70 if (skb_copy_bits(skb, at + pos, &n,
71 sizeof(n)) < 0)
72 return -1;
73 val = ntohl(n);
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
u32_mt(const struct sk_buff * skb,struct xt_action_param * par)91 static bool u32_mt(const struct sk_buff *skb, struct xt_action_param *par)
92 {
93 const struct xt_u32 *data = par->matchinfo;
94 int ret;
95
96 ret = u32_match_it(data, skb);
97 if (ret < 0) {
98 par->hotdrop = true;
99 return false;
100 }
101
102 return ret ^ data->invert;
103 }
104
u32_mt_checkentry(const struct xt_mtchk_param * par)105 static int u32_mt_checkentry(const struct xt_mtchk_param *par)
106 {
107 const struct xt_u32 *data = par->matchinfo;
108 const struct xt_u32_test *ct;
109 unsigned int i, j;
110
111 if (data->ntests > ARRAY_SIZE(data->tests))
112 return -EINVAL;
113
114 for (i = 0; i < data->ntests; ++i) {
115 ct = &data->tests[i];
116
117 if (ct->nnums > ARRAY_SIZE(ct->location) ||
118 ct->nvalues > ARRAY_SIZE(ct->value))
119 return -EINVAL;
120
121 for (j = 1; j < ct->nnums; ++j) {
122 switch (ct->location[j].nextop) {
123 case XT_U32_LEFTSH:
124 case XT_U32_RIGHTSH:
125 if (ct->location[j].number >= 32)
126 return -EINVAL;
127 break;
128 }
129 }
130 }
131
132 return 0;
133 }
134
135 static struct xt_match xt_u32_mt_reg __read_mostly = {
136 .name = "u32",
137 .revision = 0,
138 .family = NFPROTO_UNSPEC,
139 .match = u32_mt,
140 .checkentry = u32_mt_checkentry,
141 .matchsize = sizeof(struct xt_u32),
142 .me = THIS_MODULE,
143 };
144
u32_mt_init(void)145 static int __init u32_mt_init(void)
146 {
147 return xt_register_match(&xt_u32_mt_reg);
148 }
149
u32_mt_exit(void)150 static void __exit u32_mt_exit(void)
151 {
152 xt_unregister_match(&xt_u32_mt_reg);
153 }
154
155 module_init(u32_mt_init);
156 module_exit(u32_mt_exit);
157 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
158 MODULE_DESCRIPTION("Xtables: arbitrary byte matching");
159 MODULE_LICENSE("GPL");
160 MODULE_ALIAS("ipt_u32");
161 MODULE_ALIAS("ip6t_u32");
162