xref: /openbmc/linux/net/sched/em_nbyte.c (revision 82a470f1119eb7d2e4941b915bf9cd6fd8d54494)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net/sched/em_nbyte.c	N-Byte ematch
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
51da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
61da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
71da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Thomas Graf <tgraf@suug.ch>
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
125a0e3ad6STejun Heo #include <linux/gfp.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/types.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/string.h>
171da177e4SLinus Torvalds #include <linux/skbuff.h>
181da177e4SLinus Torvalds #include <linux/tc_ematch/tc_em_nbyte.h>
191da177e4SLinus Torvalds #include <net/pkt_cls.h>
201da177e4SLinus Torvalds 
21cc7ec456SEric Dumazet struct nbyte_data {
221da177e4SLinus Torvalds 	struct tcf_em_nbyte	hdr;
231da177e4SLinus Torvalds 	char			pattern[0];
241da177e4SLinus Torvalds };
251da177e4SLinus Torvalds 
26*82a470f1SJohn Fastabend static int em_nbyte_change(struct net *net, void *data, int data_len,
271da177e4SLinus Torvalds 			   struct tcf_ematch *em)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	struct tcf_em_nbyte *nbyte = data;
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 	if (data_len < sizeof(*nbyte) ||
321da177e4SLinus Torvalds 	    data_len < (sizeof(*nbyte) + nbyte->len))
331da177e4SLinus Torvalds 		return -EINVAL;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 	em->datalen = sizeof(*nbyte) + nbyte->len;
36c7b1b249SArnaldo Carvalho de Melo 	em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
371da177e4SLinus Torvalds 	if (em->data == 0UL)
381da177e4SLinus Torvalds 		return -ENOBUFS;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	return 0;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds static int em_nbyte_match(struct sk_buff *skb, struct tcf_ematch *em,
441da177e4SLinus Torvalds 			  struct tcf_pkt_info *info)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	struct nbyte_data *nbyte = (struct nbyte_data *) em->data;
471da177e4SLinus Torvalds 	unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer);
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	ptr += nbyte->hdr.off;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
521da177e4SLinus Torvalds 		return 0;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	return !memcmp(ptr + nbyte->hdr.off, nbyte->pattern, nbyte->hdr.len);
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static struct tcf_ematch_ops em_nbyte_ops = {
581da177e4SLinus Torvalds 	.kind	  = TCF_EM_NBYTE,
591da177e4SLinus Torvalds 	.change	  = em_nbyte_change,
601da177e4SLinus Torvalds 	.match	  = em_nbyte_match,
611da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
621da177e4SLinus Torvalds 	.link	  = LIST_HEAD_INIT(em_nbyte_ops.link)
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static int __init init_em_nbyte(void)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	return tcf_em_register(&em_nbyte_ops);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static void __exit exit_em_nbyte(void)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	tcf_em_unregister(&em_nbyte_ops);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds MODULE_LICENSE("GPL");
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds module_init(init_em_nbyte);
781da177e4SLinus Torvalds module_exit(exit_em_nbyte);
79db3d99c0SPatrick McHardy 
80db3d99c0SPatrick McHardy MODULE_ALIAS_TCF_EMATCH(TCF_EM_NBYTE);
81