Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * net/sched/em_cmp.c Simple packet data comparison ematch |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Authors: Thomas Graf <tgraf@suug.ch> |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Harvey Harrison | d48abfe | 2008-09-22 19:20:51 -0700 | [diff] [blame] | 13 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <net/pkt_cls.h> |
| 15 | |
| 16 | static inline int cmp_needs_transformation(struct tcf_em_cmp *cmp) |
| 17 | { |
| 18 | return unlikely(cmp->flags & TCF_EM_CMP_TRANS); |
| 19 | } |
| 20 | |
| 21 | static 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) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 32 | case TCF_EM_ALIGN_U8: |
| 33 | val = *ptr; |
| 34 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 36 | case TCF_EM_ALIGN_U16: |
| 37 | val = get_unaligned_be16(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 39 | if (cmp_needs_transformation(cmp)) |
| 40 | val = be16_to_cpu(val); |
| 41 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 43 | case TCF_EM_ALIGN_U32: |
Menglong Dong | e5a4b17 | 2020-11-09 02:02:17 -0500 | [diff] [blame] | 44 | /* Worth checking boundaries? The branching seems |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 45 | * to get worse. Visit again. |
| 46 | */ |
| 47 | val = get_unaligned_be32(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 49 | if (cmp_needs_transformation(cmp)) |
| 50 | val = be32_to_cpu(val); |
| 51 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 53 | default: |
| 54 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (cmp->mask) |
| 58 | val &= cmp->mask; |
| 59 | |
| 60 | switch (cmp->opnd) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static 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 | |
| 80 | static int __init init_em_cmp(void) |
| 81 | { |
| 82 | return tcf_em_register(&em_cmp_ops); |
| 83 | } |
| 84 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 85 | static void __exit exit_em_cmp(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | tcf_em_unregister(&em_cmp_ops); |
| 88 | } |
| 89 | |
| 90 | MODULE_LICENSE("GPL"); |
| 91 | |
| 92 | module_init(init_em_cmp); |
| 93 | module_exit(exit_em_cmp); |
| 94 | |
Patrick McHardy | db3d99c | 2007-07-11 19:46:26 -0700 | [diff] [blame] | 95 | MODULE_ALIAS_TCF_EMATCH(TCF_EM_CMP); |