1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2008-2013 Thomas Graf <tgraf@suug.ch>
4 */
5
6 /**
7 * @ingroup ematch
8 * @defgroup em_cmp Simple packet data comparison
9 *
10 * @{
11 */
12
13 #include <netlink-private/netlink.h>
14 #include <netlink-private/tc.h>
15 #include <netlink/netlink.h>
16 #include <netlink/route/cls/ematch.h>
17 #include <netlink/route/cls/ematch/cmp.h>
18 #include <linux/tc_ematch/tc_em_cmp.h>
19
rtnl_ematch_cmp_set(struct rtnl_ematch * e,struct tcf_em_cmp * cfg)20 void rtnl_ematch_cmp_set(struct rtnl_ematch *e, struct tcf_em_cmp *cfg)
21 {
22 memcpy(rtnl_ematch_data(e), cfg, sizeof(*cfg));
23 }
24
rtnl_ematch_cmp_get(struct rtnl_ematch * e)25 struct tcf_em_cmp *rtnl_ematch_cmp_get(struct rtnl_ematch *e)
26 {
27 return rtnl_ematch_data(e);
28 }
29
cmp_parse(struct rtnl_ematch * e,void * data,size_t len)30 static int cmp_parse(struct rtnl_ematch *e, void *data, size_t len)
31 {
32 memcpy(rtnl_ematch_data(e), data, len);
33
34 return 0;
35 }
36
37 static const char *align_txt[] = {
38 [TCF_EM_ALIGN_U8] = "u8",
39 [TCF_EM_ALIGN_U16] = "u16",
40 [TCF_EM_ALIGN_U32] = "u32"
41 };
42
43 static const char *layer_txt[] = {
44 [TCF_LAYER_LINK] = "eth",
45 [TCF_LAYER_NETWORK] = "ip",
46 [TCF_LAYER_TRANSPORT] = "tcp"
47 };
48
49 static const char *operand_txt[] = {
50 [TCF_EM_OPND_EQ] = "=",
51 [TCF_EM_OPND_LT] = "<",
52 [TCF_EM_OPND_GT] = ">",
53 };
54
cmp_dump(struct rtnl_ematch * e,struct nl_dump_params * p)55 static void cmp_dump(struct rtnl_ematch *e, struct nl_dump_params *p)
56 {
57 struct tcf_em_cmp *cmp = rtnl_ematch_data(e);
58
59 if (cmp->flags & TCF_EM_CMP_TRANS)
60 nl_dump(p, "ntoh%c(", (cmp->align == TCF_EM_ALIGN_U32) ? 'l' : 's');
61
62 nl_dump(p, "%s at %s+%u",
63 align_txt[cmp->align], layer_txt[cmp->layer], cmp->off);
64
65 if (cmp->mask)
66 nl_dump(p, " & 0x%x", cmp->mask);
67
68 if (cmp->flags & TCF_EM_CMP_TRANS)
69 nl_dump(p, ")");
70
71 nl_dump(p, " %s %u", operand_txt[cmp->opnd], cmp->val);
72 }
73
74 static struct rtnl_ematch_ops cmp_ops = {
75 .eo_kind = TCF_EM_CMP,
76 .eo_name = "cmp",
77 .eo_minlen = sizeof(struct tcf_em_cmp),
78 .eo_datalen = sizeof(struct tcf_em_cmp),
79 .eo_parse = cmp_parse,
80 .eo_dump = cmp_dump,
81 };
82
cmp_init(void)83 static void __init cmp_init(void)
84 {
85 rtnl_ematch_register(&cmp_ops);
86 }
87
88 /** @} */
89