2 * xt_u32 - kernel module to match u32 packet content
4 * Original author: Don Cohen <don@isis.cs3-inc.com>
5 * © Jan Engelhardt <jengelh@gmx.de>, 2007
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>
16 static bool u32_match_it(const struct xt_u32 *data,
17 const struct sk_buff *skb)
19 const struct xt_u32_test *ct;
30 * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
31 * (=IPv4 and (TCP or UDP)). Outer loop runs over the "&&" operands.
33 for (testind = 0; testind < data->ntests; ++testind) {
34 ct = &data->tests[testind];
36 pos = ct->location[0].number;
38 if (skb->len < 4 || pos > skb->len - 4);
41 ret = skb_copy_bits(skb, pos, &val, sizeof(val));
46 /* Inner loop runs over "&", "<<", ">>" and "@" operands */
47 for (i = 1; i < nnums; ++i) {
48 u_int32_t number = ct->location[i].number;
49 switch (ct->location[i].nextop) {
64 if (at + 4 < at || skb->len < at + 4 ||
65 pos > skb->len - at - 4)
68 ret = skb_copy_bits(skb, at + pos, &val,
76 /* Run over the "," and ":" operands */
78 for (i = 0; i < nvals; ++i)
79 if (ct->value[i].min <= val && val <= ct->value[i].max)
89 static bool u32_match(const struct sk_buff *skb,
90 const struct net_device *in,
91 const struct net_device *out,
92 const struct xt_match *match, const void *matchinfo,
93 int offset, unsigned int protoff, bool *hotdrop)
95 const struct xt_u32 *data = matchinfo;
98 ret = u32_match_it(data, skb);
99 return ret ^ data->invert;
102 static struct xt_match u32_reg[] __read_mostly = {
107 .matchsize = sizeof(struct xt_u32),
114 .matchsize = sizeof(struct xt_u32),
119 static int __init xt_u32_init(void)
121 return xt_register_matches(u32_reg, ARRAY_SIZE(u32_reg));
124 static void __exit xt_u32_exit(void)
126 xt_unregister_matches(u32_reg, ARRAY_SIZE(u32_reg));
129 module_init(xt_u32_init);
130 module_exit(xt_u32_exit);
131 MODULE_AUTHOR("Jan Engelhardt <jengelh@gmx.de>");
132 MODULE_DESCRIPTION("netfilter u32 match module");
133 MODULE_LICENSE("GPL");
134 MODULE_ALIAS("ipt_u32");
135 MODULE_ALIAS("ip6t_u32");