Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / netfilter / xt_u32.c
blob8625ad4f34e08d20025b2e169f522ad431cc4910
1 /*
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>
7 */
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;
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 <<<<<<< HEAD:net/netfilter/xt_u32.c
30 int ret;
31 =======
32 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/xt_u32.c
35 * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
36 * (=IPv4 and (TCP or UDP)). Outer loop runs over the "&&" operands.
38 for (testind = 0; testind < data->ntests; ++testind) {
39 ct = &data->tests[testind];
40 at = 0;
41 pos = ct->location[0].number;
43 if (skb->len < 4 || pos > skb->len - 4)
44 return false;
46 <<<<<<< HEAD:net/netfilter/xt_u32.c
47 ret = skb_copy_bits(skb, pos, &n, sizeof(n));
48 BUG_ON(ret < 0);
49 =======
50 if (skb_copy_bits(skb, pos, &n, sizeof(n)) < 0)
51 BUG();
52 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/xt_u32.c
53 val = ntohl(n);
54 nnums = ct->nnums;
56 /* Inner loop runs over "&", "<<", ">>" and "@" operands */
57 for (i = 1; i < nnums; ++i) {
58 u_int32_t number = ct->location[i].number;
59 switch (ct->location[i].nextop) {
60 case XT_U32_AND:
61 val &= number;
62 break;
63 case XT_U32_LEFTSH:
64 val <<= number;
65 break;
66 case XT_U32_RIGHTSH:
67 val >>= number;
68 break;
69 case XT_U32_AT:
70 if (at + val < at)
71 return false;
72 at += val;
73 pos = number;
74 if (at + 4 < at || skb->len < at + 4 ||
75 pos > skb->len - at - 4)
76 return false;
78 <<<<<<< HEAD:net/netfilter/xt_u32.c
79 ret = skb_copy_bits(skb, at + pos, &n,
80 sizeof(n));
81 BUG_ON(ret < 0);
82 =======
83 if (skb_copy_bits(skb, at + pos, &n,
84 sizeof(n)) < 0)
85 BUG();
86 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netfilter/xt_u32.c
87 val = ntohl(n);
88 break;
92 /* Run over the "," and ":" operands */
93 nvals = ct->nvalues;
94 for (i = 0; i < nvals; ++i)
95 if (ct->value[i].min <= val && val <= ct->value[i].max)
96 break;
98 if (i >= ct->nvalues)
99 return false;
102 return true;
105 static bool
106 u32_mt(const struct sk_buff *skb, const struct net_device *in,
107 const struct net_device *out, const struct xt_match *match,
108 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
110 const struct xt_u32 *data = matchinfo;
111 bool ret;
113 ret = u32_match_it(data, skb);
114 return ret ^ data->invert;
117 static struct xt_match u32_mt_reg[] __read_mostly = {
119 .name = "u32",
120 .family = AF_INET,
121 .match = u32_mt,
122 .matchsize = sizeof(struct xt_u32),
123 .me = THIS_MODULE,
126 .name = "u32",
127 .family = AF_INET6,
128 .match = u32_mt,
129 .matchsize = sizeof(struct xt_u32),
130 .me = THIS_MODULE,
134 static int __init u32_mt_init(void)
136 return xt_register_matches(u32_mt_reg, ARRAY_SIZE(u32_mt_reg));
139 static void __exit u32_mt_exit(void)
141 xt_unregister_matches(u32_mt_reg, ARRAY_SIZE(u32_mt_reg));
144 module_init(u32_mt_init);
145 module_exit(u32_mt_exit);
146 MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
147 MODULE_DESCRIPTION("Xtables: arbitrary byte matching");
148 MODULE_LICENSE("GPL");
149 MODULE_ALIAS("ipt_u32");
150 MODULE_ALIAS("ip6t_u32");