1 // SPDX-License-Identifier: GPL-2.0-only
3 * xt_u32 - kernel module to match u32 packet content
5 * Original author: Don Cohen <don@isis.cs3-inc.com>
6 * (C) CC Computer Consultants GmbH, 2007
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>
17 static bool u32_match_it(const struct xt_u32 *data,
18 const struct sk_buff *skb)
20 const struct xt_u32_test *ct;
31 * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
32 * (=IPv4 and (TCP or UDP)). Outer loop runs over the "&&" operands.
34 for (testind = 0; testind < data->ntests; ++testind) {
35 ct = &data->tests[testind];
37 pos = ct->location[0].number;
39 if (skb->len < 4 || pos > skb->len - 4)
42 if (skb_copy_bits(skb, pos, &n, sizeof(n)) < 0)
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) {
65 if (at + 4 < at || skb->len < at + 4 ||
66 pos > skb->len - at - 4)
69 if (skb_copy_bits(skb, at + pos, &n,
77 /* Run over the "," and ":" operands */
79 for (i = 0; i < nvals; ++i)
80 if (ct->value[i].min <= val && val <= ct->value[i].max)
90 static bool u32_mt(const struct sk_buff *skb, struct xt_action_param *par)
92 const struct xt_u32 *data = par->matchinfo;
95 ret = u32_match_it(data, skb);
96 return ret ^ data->invert;
99 static int u32_mt_checkentry(const struct xt_mtchk_param *par)
101 const struct xt_u32 *data = par->matchinfo;
102 const struct xt_u32_test *ct;
105 if (data->ntests > ARRAY_SIZE(data->tests))
108 for (i = 0; i < data->ntests; ++i) {
109 ct = &data->tests[i];
111 if (ct->nnums > ARRAY_SIZE(ct->location) ||
112 ct->nvalues > ARRAY_SIZE(ct->value))
119 static struct xt_match xt_u32_mt_reg __read_mostly = {
122 .family = NFPROTO_UNSPEC,
124 .checkentry = u32_mt_checkentry,
125 .matchsize = sizeof(struct xt_u32),
129 static int __init u32_mt_init(void)
131 return xt_register_match(&xt_u32_mt_reg);
134 static void __exit u32_mt_exit(void)
136 xt_unregister_match(&xt_u32_mt_reg);
139 module_init(u32_mt_init);
140 module_exit(u32_mt_exit);
141 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
142 MODULE_DESCRIPTION("Xtables: arbitrary byte matching");
143 MODULE_LICENSE("GPL");
144 MODULE_ALIAS("ipt_u32");
145 MODULE_ALIAS("ip6t_u32");