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