summaryrefslogtreecommitdiffstats
path: root/lib/route/cls/ematch/cmp.c
blob: ec25320fd97bb437853da2fec603565407f904c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * lib/route/cls/ematch/cmp.c	Simple packet data comparison ematch
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup ematch
 * @defgroup em_cmp Simple packet data comparison
 *
 * @{
 */

#include <netlink-local.h>
#include <netlink-tc.h>
#include <netlink/netlink.h>
#include <netlink/route/cls/ematch.h>
#include <linux/tc_ematch/tc_em_cmp.h>

void rtnl_ematch_cmp_set(struct rtnl_ematch *ematch,
			 struct tcf_em_cmp *cfg)
{
	memcpy(rtnl_ematch_data(ematch), cfg, sizeof(*cfg));
}

struct tcf_em_cmp *rtnl_ematch_cmp_get(struct rtnl_ematch *ematch)
{
	return rtnl_ematch_data(ematch);
}

static const char *align_txt(struct tcf_em_cmp *cmp)
{
	switch (cmp->align) {
	case TCF_EM_ALIGN_U8:
		return "u8";
	case TCF_EM_ALIGN_U16:
		return (cmp->flags & TCF_EM_CMP_TRANS) ? "h16" : "u16";
	case TCF_EM_ALIGN_U32:
		return (cmp->flags & TCF_EM_CMP_TRANS) ? "h32" : "u32";
	default:
		return (cmp->flags & TCF_EM_CMP_TRANS) ? "h?" : "u?";
	}
}

static const char *layer_txt(struct tcf_em_cmp *cmp)
{
	switch (cmp->layer) {
	case TCF_LAYER_LINK:
		return "link";
	case TCF_LAYER_NETWORK:
		return "network";
	case TCF_LAYER_TRANSPORT:
		return "transport";
	default:
		return "?";
	}
}

static const char *relation_txt(struct tcf_em_cmp *cmp)
{
	switch (cmp->opnd) {
	case TCF_EM_OPND_EQ:
		return "eq";
	case TCF_EM_OPND_LT:
		return "lt";
	case TCF_EM_OPND_GT:
		return "gt";
	default:
		return "?";
	}
}

static int cmp_parse(struct rtnl_ematch *m, void *data, size_t len)
{
	memcpy(rtnl_ematch_data(m), data, len);

	return 0;
}

static void cmp_dump(struct rtnl_ematch *m, struct nl_dump_params *p)
{
	struct tcf_em_cmp *cmp = rtnl_ematch_data(m);

	nl_dump(p, "%s at %s+%u ",
		align_txt(cmp), layer_txt(cmp), cmp->off);

	if (cmp->mask)
		nl_dump(p, "& 0x%x ", cmp->mask);

	nl_dump(p, "%s %u", relation_txt(cmp), cmp->val);
}

static struct rtnl_ematch_ops cmp_ops = {
	.eo_kind	= TCF_EM_CMP,
	.eo_name	= "cmp",
	.eo_datalen	= sizeof(struct tcf_em_cmp),
	.eo_parse	= cmp_parse,
	.eo_dump	= cmp_dump,
};

static void __init cmp_init(void)
{
	rtnl_ematch_register(&cmp_ops);
}

static void __exit cmp_exit(void)
{
	rtnl_ematch_unregister(&cmp_ops);
}

/** @} */