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));
97 char *rtnl_ematch_text_get_algo(
struct rtnl_ematch *e)
99 struct text_data *t = rtnl_ematch_data(e);
101 return t->cfg.algo[0] ? t->cfg.algo : NULL;
104 static int text_parse(
struct rtnl_ematch *e,
void *data,
size_t len)
106 struct text_data *t = rtnl_ematch_data(e);
107 size_t hdrlen =
sizeof(
struct tcf_em_text);
108 size_t plen = len - hdrlen;
110 memcpy(&t->cfg, data, hdrlen);
112 if (t->cfg.pattern_len > plen)
115 if (t->cfg.pattern_len > 0) {
116 if (!(t->pattern = calloc(1, t->cfg.pattern_len)))
119 memcpy(t->pattern, data + hdrlen, t->cfg.pattern_len);
125 static void text_dump(
struct rtnl_ematch *e,
struct nl_dump_params *p)
127 struct text_data *t = rtnl_ematch_data(e);
131 t->cfg.algo[0] ? t->cfg.algo :
"no-algo",
132 t->pattern ? :
"no-pattern");
134 if (t->cfg.from_layer || t->cfg.from_offset) {
136 rtnl_ematch_offset2txt(t->cfg.from_layer,
141 if (t->cfg.to_layer || t->cfg.to_offset) {
143 rtnl_ematch_offset2txt(t->cfg.to_layer,
151 static int text_fill(
struct rtnl_ematch *e,
struct nl_msg *msg)
153 struct text_data *t = rtnl_ematch_data(e);
156 if ((err =
nlmsg_append(msg, &t->cfg,
sizeof(t->cfg), 0)) < 0)
159 return nlmsg_append(msg, t->pattern, t->cfg.pattern_len, 0);
162 static void text_free(
struct rtnl_ematch *e)
164 struct text_data *t = rtnl_ematch_data(e);
169 .eo_kind = TCF_EM_TEXT,
171 .eo_minlen =
sizeof(
struct tcf_em_text),
173 .eo_parse = text_parse,
174 .eo_dump = text_dump,
175 .eo_fill = text_fill,
176 .eo_free = text_free,
179 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.