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/text.h> 24 #include <linux/tc_ematch/tc_em_text.h> 28 struct tcf_em_text cfg;
32 void rtnl_ematch_text_set_from(
struct rtnl_ematch *e, uint8_t layer,
35 struct text_data *t = rtnl_ematch_data(e);
36 t->cfg.from_offset = offset;
37 t->cfg.from_layer = layer;
40 uint16_t rtnl_ematch_text_get_from_offset(
struct rtnl_ematch *e)
42 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.from_offset;
45 uint8_t rtnl_ematch_text_get_from_layer(
struct rtnl_ematch *e)
47 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.from_layer;
50 void rtnl_ematch_text_set_to(
struct rtnl_ematch *e, uint8_t layer,
53 struct text_data *t = rtnl_ematch_data(e);
54 t->cfg.to_offset = offset;
55 t->cfg.to_layer = layer;
58 uint16_t rtnl_ematch_text_get_to_offset(
struct rtnl_ematch *e)
60 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.to_offset;
63 uint8_t rtnl_ematch_text_get_to_layer(
struct rtnl_ematch *e)
65 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.to_layer;
68 void rtnl_ematch_text_set_pattern(
struct rtnl_ematch *e,
69 char *pattern,
size_t len)
71 struct text_data *t = rtnl_ematch_data(e);
77 t->cfg.pattern_len = len;
80 char *rtnl_ematch_text_get_pattern(
struct rtnl_ematch *e)
82 return ((
struct text_data *) rtnl_ematch_data(e))->pattern;
85 size_t rtnl_ematch_text_get_len(
struct rtnl_ematch *e)
87 return ((
struct text_data *) rtnl_ematch_data(e))->cfg.pattern_len;
90 void rtnl_ematch_text_set_algo(
struct rtnl_ematch *e,
const char *algo)
92 struct text_data *t = rtnl_ematch_data(e);
94 strncpy(t->cfg.algo, algo,
sizeof(t->cfg.algo));
95 t->cfg.algo[
sizeof(t->cfg.algo) - 1] =
'\0';
98 char *rtnl_ematch_text_get_algo(
struct rtnl_ematch *e)
100 struct text_data *t = rtnl_ematch_data(e);
102 return t->cfg.algo[0] ? t->cfg.algo : NULL;
105 static int text_parse(
struct rtnl_ematch *e,
void *data,
size_t len)
107 struct text_data *t = rtnl_ematch_data(e);
108 size_t hdrlen =
sizeof(
struct tcf_em_text);
109 size_t plen = len - hdrlen;
111 memcpy(&t->cfg, data, hdrlen);
113 if (t->cfg.pattern_len > plen)
116 if (t->cfg.pattern_len > 0) {
117 if (!(t->pattern = calloc(1, t->cfg.pattern_len)))
120 memcpy(t->pattern, (
char *) data + hdrlen, t->cfg.pattern_len);
126 static void text_dump(
struct rtnl_ematch *e,
struct nl_dump_params *p)
128 struct text_data *t = rtnl_ematch_data(e);
132 t->cfg.algo[0] ? t->cfg.algo :
"no-algo",
133 t->pattern ? t->pattern :
"no-pattern");
135 if (t->cfg.from_layer || t->cfg.from_offset) {
137 rtnl_ematch_offset2txt(t->cfg.from_layer,
142 if (t->cfg.to_layer || t->cfg.to_offset) {
144 rtnl_ematch_offset2txt(t->cfg.to_layer,
152 static int text_fill(
struct rtnl_ematch *e,
struct nl_msg *msg)
154 struct text_data *t = rtnl_ematch_data(e);
157 if ((err =
nlmsg_append(msg, &t->cfg,
sizeof(t->cfg), 0)) < 0)
160 return nlmsg_append(msg, t->pattern, t->cfg.pattern_len, 0);
163 static void text_free(
struct rtnl_ematch *e)
165 struct text_data *t = rtnl_ematch_data(e);
170 .eo_kind = TCF_EM_TEXT,
172 .eo_minlen =
sizeof(
struct tcf_em_text),
174 .eo_parse = text_parse,
175 .eo_dump = text_dump,
176 .eo_fill = text_fill,
177 .eo_free = text_free,
180 static 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.