xref: /openbmc/linux/net/sched/em_nbyte.c (revision 1da177e4)
1 /*
2  * net/sched/em_nbyte.c	N-Byte ematch
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/string.h>
18 #include <linux/skbuff.h>
19 #include <linux/tc_ematch/tc_em_nbyte.h>
20 #include <net/pkt_cls.h>
21 
22 struct nbyte_data
23 {
24 	struct tcf_em_nbyte	hdr;
25 	char			pattern[0];
26 };
27 
28 static int em_nbyte_change(struct tcf_proto *tp, void *data, int data_len,
29 			   struct tcf_ematch *em)
30 {
31 	struct tcf_em_nbyte *nbyte = data;
32 
33 	if (data_len < sizeof(*nbyte) ||
34 	    data_len < (sizeof(*nbyte) + nbyte->len))
35 		return -EINVAL;
36 
37 	em->datalen = sizeof(*nbyte) + nbyte->len;
38 	em->data = (unsigned long) kmalloc(em->datalen, GFP_KERNEL);
39 	if (em->data == 0UL)
40 		return -ENOBUFS;
41 
42 	memcpy((void *) em->data, data, em->datalen);
43 
44 	return 0;
45 }
46 
47 static int em_nbyte_match(struct sk_buff *skb, struct tcf_ematch *em,
48 			  struct tcf_pkt_info *info)
49 {
50 	struct nbyte_data *nbyte = (struct nbyte_data *) em->data;
51 	unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer);
52 
53 	ptr += nbyte->hdr.off;
54 
55 	if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
56 		return 0;
57 
58 	return !memcmp(ptr + nbyte->hdr.off, nbyte->pattern, nbyte->hdr.len);
59 }
60 
61 static struct tcf_ematch_ops em_nbyte_ops = {
62 	.kind	  = TCF_EM_NBYTE,
63 	.change	  = em_nbyte_change,
64 	.match	  = em_nbyte_match,
65 	.owner	  = THIS_MODULE,
66 	.link	  = LIST_HEAD_INIT(em_nbyte_ops.link)
67 };
68 
69 static int __init init_em_nbyte(void)
70 {
71 	return tcf_em_register(&em_nbyte_ops);
72 }
73 
74 static void __exit exit_em_nbyte(void)
75 {
76 	tcf_em_unregister(&em_nbyte_ops);
77 }
78 
79 MODULE_LICENSE("GPL");
80 
81 module_init(init_em_nbyte);
82 module_exit(exit_em_nbyte);
83