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