^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * net/sched/em_cmp.c Simple packet data comparison ematch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Authors: Thomas Graf <tgraf@suug.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/tc_ematch/tc_em_cmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static inline int cmp_needs_transformation(struct tcf_em_cmp *cmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return unlikely(cmp->flags & TCF_EM_CMP_TRANS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int em_cmp_match(struct sk_buff *skb, struct tcf_ematch *em,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct tcf_pkt_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct tcf_em_cmp *cmp = (struct tcf_em_cmp *) em->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned char *ptr = tcf_get_base_ptr(skb, cmp->layer) + cmp->off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!tcf_valid_offset(skb, ptr, cmp->align))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) switch (cmp->align) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) case TCF_EM_ALIGN_U8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) val = *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) case TCF_EM_ALIGN_U16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) val = get_unaligned_be16(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (cmp_needs_transformation(cmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) val = be16_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case TCF_EM_ALIGN_U32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* Worth checking boundries? The branching seems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * to get worse. Visit again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) val = get_unaligned_be32(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (cmp_needs_transformation(cmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) val = be32_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (cmp->mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) val &= cmp->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) switch (cmp->opnd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) case TCF_EM_OPND_EQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return val == cmp->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) case TCF_EM_OPND_LT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return val < cmp->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) case TCF_EM_OPND_GT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return val > cmp->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static struct tcf_ematch_ops em_cmp_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .kind = TCF_EM_CMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .datalen = sizeof(struct tcf_em_cmp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .match = em_cmp_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .link = LIST_HEAD_INIT(em_cmp_ops.link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int __init init_em_cmp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return tcf_em_register(&em_cmp_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void __exit exit_em_cmp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) tcf_em_unregister(&em_cmp_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) module_init(init_em_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) module_exit(exit_em_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) MODULE_ALIAS_TCF_EMATCH(TCF_EM_CMP);