2 * xt_u32 - kernel module to match u32 packet content
4 * Original author: Don Cohen <don@isis.cs3-inc.com>
5 * (C) CC Computer Consultants GmbH, 2007
6 * Contact: <jengelh@computergmbh.de>
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;
32 * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
33 * (=IPv4 and (TCP or UDP)). Outer loop runs over the "&&" operands.
35 for (testind = 0; testind < data->ntests; ++testind) {
36 ct = &data->tests[testind];
38 pos = ct->location[0].number;
40 if (skb->len < 4 || pos > skb->len - 4)
43 ret = skb_copy_bits(skb, pos, &n, sizeof(n));
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) {
66 if (at + 4 < at || skb->len < at + 4 ||
67 pos > skb->len - at - 4)
70 ret = skb_copy_bits(skb, at + pos, &n,
78 /* Run over the "," and ":" operands */
80 for (i = 0; i < nvals; ++i)
81 if (ct->value[i].min <= val && val <= ct->value[i].max)
92 u32_mt(const struct sk_buff *skb, const struct net_device *in,
93 const struct net_device *out, const struct xt_match *match,
94 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
96 const struct xt_u32 *data = matchinfo;
99 ret = u32_match_it(data, skb);
100 return ret ^ data->invert;
103 static struct xt_match u32_mt_reg[] __read_mostly = {
108 .matchsize = sizeof(struct xt_u32),
115 .matchsize = sizeof(struct xt_u32),
120 static int __init u32_mt_init(void)
122 return xt_register_matches(u32_mt_reg, ARRAY_SIZE(u32_mt_reg));
125 static void __exit u32_mt_exit(void)
127 xt_unregister_matches(u32_mt_reg, ARRAY_SIZE(u32_mt_reg));
130 module_init(u32_mt_init);
131 module_exit(u32_mt_exit);
132 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
133 MODULE_DESCRIPTION("Xtables: arbitrary byte matching");
134 MODULE_LICENSE("GPL");
135 MODULE_ALIAS("ipt_u32");
136 MODULE_ALIAS("ip6t_u32");