0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/gfp.h>
0009 #include <linux/module.h>
0010 #include <linux/types.h>
0011 #include <linux/kernel.h>
0012 #include <linux/string.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/tc_ematch/tc_em_nbyte.h>
0015 #include <net/pkt_cls.h>
0016
0017 struct nbyte_data {
0018 struct tcf_em_nbyte hdr;
0019 char pattern[];
0020 };
0021
0022 static int em_nbyte_change(struct net *net, void *data, int data_len,
0023 struct tcf_ematch *em)
0024 {
0025 struct tcf_em_nbyte *nbyte = data;
0026
0027 if (data_len < sizeof(*nbyte) ||
0028 data_len < (sizeof(*nbyte) + nbyte->len))
0029 return -EINVAL;
0030
0031 em->datalen = sizeof(*nbyte) + nbyte->len;
0032 em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
0033 if (em->data == 0UL)
0034 return -ENOMEM;
0035
0036 return 0;
0037 }
0038
0039 static int em_nbyte_match(struct sk_buff *skb, struct tcf_ematch *em,
0040 struct tcf_pkt_info *info)
0041 {
0042 struct nbyte_data *nbyte = (struct nbyte_data *) em->data;
0043 unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer);
0044
0045 ptr += nbyte->hdr.off;
0046
0047 if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
0048 return 0;
0049
0050 return !memcmp(ptr, nbyte->pattern, nbyte->hdr.len);
0051 }
0052
0053 static struct tcf_ematch_ops em_nbyte_ops = {
0054 .kind = TCF_EM_NBYTE,
0055 .change = em_nbyte_change,
0056 .match = em_nbyte_match,
0057 .owner = THIS_MODULE,
0058 .link = LIST_HEAD_INIT(em_nbyte_ops.link)
0059 };
0060
0061 static int __init init_em_nbyte(void)
0062 {
0063 return tcf_em_register(&em_nbyte_ops);
0064 }
0065
0066 static void __exit exit_em_nbyte(void)
0067 {
0068 tcf_em_unregister(&em_nbyte_ops);
0069 }
0070
0071 MODULE_LICENSE("GPL");
0072
0073 module_init(init_em_nbyte);
0074 module_exit(exit_em_nbyte);
0075
0076 MODULE_ALIAS_TCF_EMATCH(TCF_EM_NBYTE);