13#include <netlink-private/netlink.h>
14#include <netlink-private/utils.h>
15#include <netlink-private/tc.h>
16#include <netlink/netlink.h>
17#include <netlink/route/cls/ematch.h>
18#include <netlink/route/cls/ematch/text.h>
19#include <linux/tc_ematch/tc_em_text.h>
23 struct tcf_em_text cfg;
27void rtnl_ematch_text_set_from(
struct rtnl_ematch *e, uint8_t layer,
30 struct text_data *t = rtnl_ematch_data(e);
31 t->cfg.from_offset = offset;
32 t->cfg.from_layer = layer;
35uint16_t rtnl_ematch_text_get_from_offset(
struct rtnl_ematch *e)
37 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.from_offset;
40uint8_t rtnl_ematch_text_get_from_layer(
struct rtnl_ematch *e)
42 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.from_layer;
45void rtnl_ematch_text_set_to(
struct rtnl_ematch *e, uint8_t layer,
48 struct text_data *t = rtnl_ematch_data(e);
49 t->cfg.to_offset = offset;
50 t->cfg.to_layer = layer;
53uint16_t rtnl_ematch_text_get_to_offset(
struct rtnl_ematch *e)
55 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.to_offset;
58uint8_t rtnl_ematch_text_get_to_layer(
struct rtnl_ematch *e)
60 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.to_layer;
63void rtnl_ematch_text_set_pattern(
struct rtnl_ematch *e,
64 char *pattern,
size_t len)
66 struct text_data *t = rtnl_ematch_data(e);
72 t->cfg.pattern_len = len;
75char *rtnl_ematch_text_get_pattern(
struct rtnl_ematch *e)
77 return ((
struct text_data *) rtnl_ematch_data(e))->pattern;
80size_t rtnl_ematch_text_get_len(
struct rtnl_ematch *e)
82 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.pattern_len;
85void rtnl_ematch_text_set_algo(
struct rtnl_ematch *e,
const char *algo)
87 struct text_data *t = rtnl_ematch_data(e);
89 _nl_strncpy_trunc(t->cfg.algo, algo,
sizeof(t->cfg.algo));
92char *rtnl_ematch_text_get_algo(
struct rtnl_ematch *e)
94 struct text_data *t = rtnl_ematch_data(e);
96 return t->cfg.algo[0] ? t->cfg.algo : NULL;
99static int text_parse(
struct rtnl_ematch *e,
void *data,
size_t len)
101 struct text_data *t = rtnl_ematch_data(e);
102 size_t hdrlen =
sizeof(
struct tcf_em_text);
103 size_t plen = len - hdrlen;
105 memcpy(&t->cfg, data, hdrlen);
107 if (t->cfg.pattern_len > plen)
110 if (t->cfg.pattern_len > 0) {
111 if (!(t->pattern = calloc(1, t->cfg.pattern_len)))
114 memcpy(t->pattern, (
char *) data + hdrlen, t->cfg.pattern_len);
120static void text_dump(
struct rtnl_ematch *e,
struct nl_dump_params *p)
122 struct text_data *t = rtnl_ematch_data(e);
126 t->cfg.algo[0] ? t->cfg.algo :
"no-algo",
127 t->pattern ? t->pattern :
"no-pattern");
129 if (t->cfg.from_layer || t->cfg.from_offset) {
131 rtnl_ematch_offset2txt(t->cfg.from_layer,
136 if (t->cfg.to_layer || t->cfg.to_offset) {
138 rtnl_ematch_offset2txt(t->cfg.to_layer,
146static int text_fill(
struct rtnl_ematch *e,
struct nl_msg *msg)
148 struct text_data *t = rtnl_ematch_data(e);
151 if ((err =
nlmsg_append(msg, &t->cfg,
sizeof(t->cfg), 0)) < 0)
154 return nlmsg_append(msg, t->pattern, t->cfg.pattern_len, 0);
157static void text_free(
struct rtnl_ematch *e)
159 struct text_data *t = rtnl_ematch_data(e);
164 .eo_kind = TCF_EM_TEXT,
166 .eo_minlen =
sizeof(
struct tcf_em_text),
168 .eo_parse = text_parse,
169 .eo_dump = text_dump,
170 .eo_fill = text_fill,
171 .eo_free = text_free,
174static void __init text_init(
void)
int rtnl_ematch_register(struct rtnl_ematch_ops *ops)
Register ematch module.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Extended Match Operations.