0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/skbuff.h>
0013 #include <linux/types.h>
0014 #include <linux/netfilter/x_tables.h>
0015 #include <linux/netfilter/xt_u32.h>
0016
0017 static bool u32_match_it(const struct xt_u32 *data,
0018 const struct sk_buff *skb)
0019 {
0020 const struct xt_u32_test *ct;
0021 unsigned int testind;
0022 unsigned int nnums;
0023 unsigned int nvals;
0024 unsigned int i;
0025 __be32 n;
0026 u_int32_t pos;
0027 u_int32_t val;
0028 u_int32_t at;
0029
0030
0031
0032
0033
0034 for (testind = 0; testind < data->ntests; ++testind) {
0035 ct = &data->tests[testind];
0036 at = 0;
0037 pos = ct->location[0].number;
0038
0039 if (skb->len < 4 || pos > skb->len - 4)
0040 return false;
0041
0042 if (skb_copy_bits(skb, pos, &n, sizeof(n)) < 0)
0043 BUG();
0044 val = ntohl(n);
0045 nnums = ct->nnums;
0046
0047
0048 for (i = 1; i < nnums; ++i) {
0049 u_int32_t number = ct->location[i].number;
0050 switch (ct->location[i].nextop) {
0051 case XT_U32_AND:
0052 val &= number;
0053 break;
0054 case XT_U32_LEFTSH:
0055 val <<= number;
0056 break;
0057 case XT_U32_RIGHTSH:
0058 val >>= number;
0059 break;
0060 case XT_U32_AT:
0061 if (at + val < at)
0062 return false;
0063 at += val;
0064 pos = number;
0065 if (at + 4 < at || skb->len < at + 4 ||
0066 pos > skb->len - at - 4)
0067 return false;
0068
0069 if (skb_copy_bits(skb, at + pos, &n,
0070 sizeof(n)) < 0)
0071 BUG();
0072 val = ntohl(n);
0073 break;
0074 }
0075 }
0076
0077
0078 nvals = ct->nvalues;
0079 for (i = 0; i < nvals; ++i)
0080 if (ct->value[i].min <= val && val <= ct->value[i].max)
0081 break;
0082
0083 if (i >= ct->nvalues)
0084 return false;
0085 }
0086
0087 return true;
0088 }
0089
0090 static bool u32_mt(const struct sk_buff *skb, struct xt_action_param *par)
0091 {
0092 const struct xt_u32 *data = par->matchinfo;
0093 bool ret;
0094
0095 ret = u32_match_it(data, skb);
0096 return ret ^ data->invert;
0097 }
0098
0099 static struct xt_match xt_u32_mt_reg __read_mostly = {
0100 .name = "u32",
0101 .revision = 0,
0102 .family = NFPROTO_UNSPEC,
0103 .match = u32_mt,
0104 .matchsize = sizeof(struct xt_u32),
0105 .me = THIS_MODULE,
0106 };
0107
0108 static int __init u32_mt_init(void)
0109 {
0110 return xt_register_match(&xt_u32_mt_reg);
0111 }
0112
0113 static void __exit u32_mt_exit(void)
0114 {
0115 xt_unregister_match(&xt_u32_mt_reg);
0116 }
0117
0118 module_init(u32_mt_init);
0119 module_exit(u32_mt_exit);
0120 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
0121 MODULE_DESCRIPTION("Xtables: arbitrary byte matching");
0122 MODULE_LICENSE("GPL");
0123 MODULE_ALIAS("ipt_u32");
0124 MODULE_ALIAS("ip6t_u32");