12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * net/sched/em_nbyte.c N-Byte ematch
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Authors: Thomas Graf <tgraf@suug.ch>
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
85a0e3ad6STejun Heo #include <linux/gfp.h>
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/string.h>
131da177e4SLinus Torvalds #include <linux/skbuff.h>
141da177e4SLinus Torvalds #include <linux/tc_ematch/tc_em_nbyte.h>
151da177e4SLinus Torvalds #include <net/pkt_cls.h>
161da177e4SLinus Torvalds
17cc7ec456SEric Dumazet struct nbyte_data {
181da177e4SLinus Torvalds struct tcf_em_nbyte hdr;
19b90feaffSGustavo A. R. Silva char pattern[];
201da177e4SLinus Torvalds };
211da177e4SLinus Torvalds
em_nbyte_change(struct net * net,void * data,int data_len,struct tcf_ematch * em)2282a470f1SJohn Fastabend static int em_nbyte_change(struct net *net, void *data, int data_len,
231da177e4SLinus Torvalds struct tcf_ematch *em)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds struct tcf_em_nbyte *nbyte = data;
261da177e4SLinus Torvalds
271da177e4SLinus Torvalds if (data_len < sizeof(*nbyte) ||
281da177e4SLinus Torvalds data_len < (sizeof(*nbyte) + nbyte->len))
291da177e4SLinus Torvalds return -EINVAL;
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds em->datalen = sizeof(*nbyte) + nbyte->len;
32c7b1b249SArnaldo Carvalho de Melo em->data = (unsigned long)kmemdup(data, em->datalen, GFP_KERNEL);
331da177e4SLinus Torvalds if (em->data == 0UL)
34*a64566a2SZheng Yongjun return -ENOMEM;
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds return 0;
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds
em_nbyte_match(struct sk_buff * skb,struct tcf_ematch * em,struct tcf_pkt_info * info)391da177e4SLinus Torvalds static int em_nbyte_match(struct sk_buff *skb, struct tcf_ematch *em,
401da177e4SLinus Torvalds struct tcf_pkt_info *info)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds struct nbyte_data *nbyte = (struct nbyte_data *) em->data;
431da177e4SLinus Torvalds unsigned char *ptr = tcf_get_base_ptr(skb, nbyte->hdr.layer);
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds ptr += nbyte->hdr.off;
461da177e4SLinus Torvalds
471da177e4SLinus Torvalds if (!tcf_valid_offset(skb, ptr, nbyte->hdr.len))
481da177e4SLinus Torvalds return 0;
491da177e4SLinus Torvalds
50560a6607SWolfgang Bumiller return !memcmp(ptr, nbyte->pattern, nbyte->hdr.len);
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds static struct tcf_ematch_ops em_nbyte_ops = {
541da177e4SLinus Torvalds .kind = TCF_EM_NBYTE,
551da177e4SLinus Torvalds .change = em_nbyte_change,
561da177e4SLinus Torvalds .match = em_nbyte_match,
571da177e4SLinus Torvalds .owner = THIS_MODULE,
581da177e4SLinus Torvalds .link = LIST_HEAD_INIT(em_nbyte_ops.link)
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds
init_em_nbyte(void)611da177e4SLinus Torvalds static int __init init_em_nbyte(void)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds return tcf_em_register(&em_nbyte_ops);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds
exit_em_nbyte(void)661da177e4SLinus Torvalds static void __exit exit_em_nbyte(void)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds tcf_em_unregister(&em_nbyte_ops);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds MODULE_LICENSE("GPL");
721da177e4SLinus Torvalds
731da177e4SLinus Torvalds module_init(init_em_nbyte);
741da177e4SLinus Torvalds module_exit(exit_em_nbyte);
75db3d99c0SPatrick McHardy
76db3d99c0SPatrick McHardy MODULE_ALIAS_TCF_EMATCH(TCF_EM_NBYTE);
77