blob: 9b8ed390a8e087ae4d24ae7ff872a4d5fb93d322 [file] [log] [blame]
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -07001/*
2 * xt_u32 - kernel module to match u32 packet content
3 *
4 * Original author: Don Cohen <don@isis.cs3-inc.com>
Jan Engelhardtba5dc272007-11-05 20:35:56 -08005 * (C) CC Computer Consultants GmbH, 2007
6 * Contact: <jengelh@computergmbh.de>
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -07007 */
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
17static bool 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;
Al Viroa34c4582007-07-26 17:33:19 +010025 __be32 n;
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -070026 u_int32_t pos;
27 u_int32_t val;
28 u_int32_t at;
29 int ret;
30
31 /*
32 * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
33 * (=IPv4 and (TCP or UDP)). Outer loop runs over the "&&" operands.
34 */
35 for (testind = 0; testind < data->ntests; ++testind) {
36 ct = &data->tests[testind];
37 at = 0;
38 pos = ct->location[0].number;
39
Eric Dumazet35019532007-08-14 13:13:28 -070040 if (skb->len < 4 || pos > skb->len - 4)
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -070041 return false;
42
Al Viroa34c4582007-07-26 17:33:19 +010043 ret = skb_copy_bits(skb, pos, &n, sizeof(n));
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -070044 BUG_ON(ret < 0);
Al Viroa34c4582007-07-26 17:33:19 +010045 val = ntohl(n);
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -070046 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
Al Viroa34c4582007-07-26 17:33:19 +010070 ret = skb_copy_bits(skb, at + pos, &n,
71 sizeof(n));
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -070072 BUG_ON(ret < 0);
Al Viroa34c4582007-07-26 17:33:19 +010073 val = ntohl(n);
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -070074 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
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080091static bool
92u32_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)
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -070095{
96 const struct xt_u32 *data = matchinfo;
97 bool ret;
98
99 ret = u32_match_it(data, skb);
100 return ret ^ data->invert;
101}
102
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800103static struct xt_match u32_mt_reg[] __read_mostly = {
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700104 {
105 .name = "u32",
106 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800107 .match = u32_mt,
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700108 .matchsize = sizeof(struct xt_u32),
109 .me = THIS_MODULE,
110 },
111 {
112 .name = "u32",
113 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800114 .match = u32_mt,
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700115 .matchsize = sizeof(struct xt_u32),
116 .me = THIS_MODULE,
117 },
118};
119
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800120static int __init u32_mt_init(void)
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700121{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800122 return xt_register_matches(u32_mt_reg, ARRAY_SIZE(u32_mt_reg));
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700123}
124
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800125static void __exit u32_mt_exit(void)
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700126{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800127 xt_unregister_matches(u32_mt_reg, ARRAY_SIZE(u32_mt_reg));
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700128}
129
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800130module_init(u32_mt_init);
131module_exit(u32_mt_exit);
Jan Engelhardtba5dc272007-11-05 20:35:56 -0800132MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -0800133MODULE_DESCRIPTION("Xtables: arbitrary byte matching");
Jan Engelhardt1b50b8a2007-07-07 22:20:36 -0700134MODULE_LICENSE("GPL");
135MODULE_ALIAS("ipt_u32");
136MODULE_ALIAS("ip6t_u32");