19 #include <netlink-private/netlink.h> 20 #include <netlink-private/tc.h> 21 #include <netlink/netlink.h> 22 #include <netlink/route/classifier.h> 23 #include <netlink/route/cls/ematch.h> 24 #include <netlink/route/cls/ematch/cmp.h> 26 #include "ematch_syntax.h" 27 #include "ematch_grammar.h" 34 static NL_LIST_HEAD(ematch_ops_list);
50 NL_DBG(1,
"ematch module \"%s\" registered\n", ops->eo_name);
52 nl_list_add_tail(&ops->eo_list, &ematch_ops_list);
69 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
70 if (ops->eo_kind == kind)
88 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
89 if (!strcasecmp(ops->eo_name, name))
110 struct rtnl_ematch *e;
112 if (!(e = calloc(1,
sizeof(*e))))
115 NL_DBG(2,
"allocated ematch %p\n", e);
117 NL_INIT_LIST_HEAD(&e->e_list);
118 NL_INIT_LIST_HEAD(&e->e_childs);
131 struct rtnl_ematch *child)
133 if (parent->e_kind != TCF_EM_CONTAINER)
134 return -NLE_OPNOTSUPP;
136 NL_DBG(2,
"added ematch %p \"%s\" to container %p\n",
137 child, child->e_ops->eo_name, parent);
139 nl_list_add_tail(&child->e_list, &parent->e_childs);
150 NL_DBG(2,
"unlinked ematch %p from any lists\n", ematch);
152 if (!nl_list_empty(&ematch->e_childs))
153 NL_DBG(1,
"warning: ematch %p with childs was unlinked\n",
156 nl_list_del(&ematch->e_list);
157 nl_init_list_head(&ematch->e_list);
160 void rtnl_ematch_free(
struct rtnl_ematch *ematch)
162 NL_DBG(2,
"freed ematch %p\n", ematch);
164 free(ematch->e_data);
168 int rtnl_ematch_set_ops(
struct rtnl_ematch *ematch,
struct rtnl_ematch_ops *ops)
174 ematch->e_kind = ops->eo_kind;
176 if (ops->eo_datalen) {
177 ematch->e_data = calloc(1, ops->eo_datalen);
181 ematch->e_datalen = ops->eo_datalen;
187 int rtnl_ematch_set_kind(
struct rtnl_ematch *ematch, uint16_t kind)
194 ematch->e_kind = kind;
197 rtnl_ematch_set_ops(ematch, ops);
202 int rtnl_ematch_set_name(
struct rtnl_ematch *ematch,
const char *name)
210 return -NLE_OPNOTSUPP;
212 rtnl_ematch_set_ops(ematch, ops);
217 void rtnl_ematch_set_flags(
struct rtnl_ematch *ematch, uint16_t flags)
219 ematch->e_flags |= flags;
222 void rtnl_ematch_unset_flags(
struct rtnl_ematch *ematch, uint16_t flags)
224 ematch->e_flags &= ~flags;
227 uint16_t rtnl_ematch_get_flags(
struct rtnl_ematch *ematch)
229 return ematch->e_flags;
232 void *rtnl_ematch_data(
struct rtnl_ematch *ematch)
234 return ematch->e_data;
249 struct rtnl_ematch_tree *tree;
251 if (!(tree = calloc(1,
sizeof(*tree))))
254 NL_INIT_LIST_HEAD(&tree->et_list);
255 tree->et_progid = progid;
257 NL_DBG(2,
"allocated new ematch tree %p, progid=%u\n", tree, progid);
264 struct rtnl_ematch *pos, *next;
266 nl_list_for_each_entry_safe(pos, next, head, e_list) {
267 if (!nl_list_empty(&pos->e_childs))
268 free_ematch_list(&pos->e_childs);
269 rtnl_ematch_free(pos);
284 free_ematch_list(&tree->et_list);
286 NL_DBG(2,
"Freed ematch tree %p\n", tree);
297 struct rtnl_ematch *ematch)
299 nl_list_add_tail(&ematch->e_list, &tree->et_list);
302 static inline uint32_t container_ref(
struct rtnl_ematch *ematch)
304 return *((uint32_t *) rtnl_ematch_data(ematch));
307 static int link_tree(
struct rtnl_ematch *index[],
int nmatches,
int pos,
310 struct rtnl_ematch *ematch;
313 for (i = pos; i < nmatches; i++) {
316 nl_list_add_tail(&ematch->e_list, root);
318 if (ematch->e_kind == TCF_EM_CONTAINER)
319 link_tree(index, nmatches, container_ref(ematch),
322 if (!(ematch->e_flags & TCF_EM_REL_MASK))
330 static struct nla_policy tree_policy[TCA_EMATCH_TREE_MAX+1] = {
331 [TCA_EMATCH_TREE_HDR] = { .
minlen=
sizeof(
struct tcf_ematch_tree_hdr) },
332 [TCA_EMATCH_TREE_LIST] = { .type =
NLA_NESTED },
342 struct nlattr *a, *tb[TCA_EMATCH_TREE_MAX+1];
343 struct tcf_ematch_tree_hdr *thdr;
344 struct rtnl_ematch_tree *tree;
345 struct rtnl_ematch **index;
346 int nmatches = 0, err, remaining;
348 NL_DBG(2,
"Parsing attribute %p as ematch tree\n", attr);
354 if (!tb[TCA_EMATCH_TREE_HDR])
355 return -NLE_MISSING_ATTR;
357 thdr =
nla_data(tb[TCA_EMATCH_TREE_HDR]);
360 if (thdr->nmatches == 0) {
361 NL_DBG(2,
"Ignoring empty ematch configuration\n");
365 if (!tb[TCA_EMATCH_TREE_LIST])
366 return -NLE_MISSING_ATTR;
368 NL_DBG(2,
"ematch tree found with nmatches=%u, progid=%u\n",
369 thdr->nmatches, thdr->progid);
376 if (thdr->nmatches > (
nla_len(tb[TCA_EMATCH_TREE_LIST]) /
380 if (!(index = calloc(thdr->nmatches,
sizeof(
struct rtnl_ematch *))))
390 struct tcf_ematch_hdr *hdr;
391 struct rtnl_ematch *ematch;
395 NL_DBG(3,
"parsing ematch attribute %d, len=%u\n",
398 if (
nla_len(a) <
sizeof(*hdr)) {
404 if (nmatches >= thdr->nmatches) {
410 data =
nla_data(a) + NLA_ALIGN(
sizeof(*hdr));
411 len =
nla_len(a) - NLA_ALIGN(
sizeof(*hdr));
413 NL_DBG(3,
"ematch attribute matchid=%u, kind=%u, flags=%u\n",
414 hdr->matchid, hdr->kind, hdr->flags);
420 if (hdr->kind == TCF_EM_CONTAINER &&
421 *((uint32_t *) data) >= thdr->nmatches) {
431 ematch->e_id = hdr->matchid;
432 ematch->e_kind = hdr->kind;
433 ematch->e_flags = hdr->flags;
436 if (ops->eo_minlen && len < ops->eo_minlen) {
437 rtnl_ematch_free(ematch);
442 rtnl_ematch_set_ops(ematch, ops);
445 (err = ops->eo_parse(ematch, data, len)) < 0) {
446 rtnl_ematch_free(ematch);
451 NL_DBG(3,
"index[%d] = %p\n", nmatches, ematch);
452 index[nmatches++] = ematch;
455 if (nmatches != thdr->nmatches) {
460 err = link_tree(index, nmatches, 0, &tree->et_list);
475 static void dump_ematch_sequence(
struct nl_list_head *head,
478 struct rtnl_ematch *match;
480 nl_list_for_each_entry(match, head, e_list) {
481 if (match->e_flags & TCF_EM_INVERT)
484 if (match->e_kind == TCF_EM_CONTAINER) {
486 dump_ematch_sequence(&match->e_childs, p);
488 }
else if (!match->e_ops) {
489 nl_dump(p,
"[unknown ematch %d]", match->e_kind);
491 if (match->e_ops->eo_dump)
492 match->e_ops->eo_dump(match, p);
497 switch (match->e_flags & TCF_EM_REL_MASK) {
511 void rtnl_ematch_tree_dump(
struct rtnl_ematch_tree *tree,
517 dump_ematch_sequence(&tree->et_list, p);
521 static int update_container_index(
struct nl_list_head *list,
int *index)
523 struct rtnl_ematch *e;
525 nl_list_for_each_entry(e, list, e_list)
526 e->e_index = (*index)++;
528 nl_list_for_each_entry(e, list, e_list) {
529 if (e->e_kind == TCF_EM_CONTAINER) {
532 if (nl_list_empty(&e->e_childs))
533 return -NLE_OBJ_NOTFOUND;
535 *((uint32_t *) e->e_data) = *index;
537 err = update_container_index(&e->e_childs, index);
546 static int fill_ematch_sequence(
struct nl_msg *msg,
struct nl_list_head *list)
548 struct rtnl_ematch *e;
550 nl_list_for_each_entry(e, list, e_list) {
551 struct tcf_ematch_hdr match = {
565 if (e->e_ops->eo_fill)
566 err = e->e_ops->eo_fill(e, msg);
567 else if (e->e_flags & TCF_EM_SIMPLE)
569 else if (e->e_datalen > 0)
572 NL_DBG(3,
"msg %p: added ematch [%d] id=%d kind=%d flags=%d\n",
573 msg, e->e_index, match.matchid, match.kind, match.flags);
581 nl_list_for_each_entry(e, list, e_list) {
582 if (e->e_kind == TCF_EM_CONTAINER &&
583 fill_ematch_sequence(msg, &e->e_childs) < 0)
590 int rtnl_ematch_fill_attr(
struct nl_msg *msg,
int attrid,
591 struct rtnl_ematch_tree *tree)
593 struct tcf_ematch_tree_hdr thdr = {
594 .progid = tree->et_progid,
596 struct nlattr *list, *topattr;
601 err = update_container_index(&tree->et_list, &index);
606 goto nla_put_failure;
608 thdr.nmatches = index;
609 NLA_PUT(msg, TCA_EMATCH_TREE_HDR,
sizeof(thdr), &thdr);
612 goto nla_put_failure;
614 if (fill_ematch_sequence(msg, &tree->et_list) < 0)
615 goto nla_put_failure;
629 extern int ematch_parse(
void *,
char **,
struct nl_list_head *);
631 int rtnl_ematch_parse_expr(
const char *expr,
char **errp,
632 struct rtnl_ematch_tree **result)
634 struct rtnl_ematch_tree *tree;
636 yyscan_t scanner = NULL;
639 NL_DBG(2,
"Parsing ematch expression \"%s\"\n", expr);
644 if ((err = ematch_lex_init(&scanner)) < 0) {
649 buf = ematch__scan_string(expr, scanner);
651 if ((err = ematch_parse(scanner, errp, &tree->et_list)) != 0) {
652 ematch__delete_buffer(buf, scanner);
653 err = -NLE_PARSE_ERR;
657 ematch_lex_destroy(scanner);
664 ematch_lex_destroy(scanner);
671 static const char *layer_txt[] = {
672 [TCF_LAYER_LINK] =
"eth",
673 [TCF_LAYER_NETWORK] =
"ip",
674 [TCF_LAYER_TRANSPORT] =
"tcp",
677 char *rtnl_ematch_offset2txt(uint8_t layer, uint16_t offset,
char *buf,
size_t len)
679 snprintf(buf, len,
"%s+%u",
680 (layer <= TCF_LAYER_MAX) ? layer_txt[layer] :
"?",
686 static const char *operand_txt[] = {
687 [TCF_EM_OPND_EQ] =
"=",
688 [TCF_EM_OPND_LT] =
"<",
689 [TCF_EM_OPND_GT] =
">",
692 char *rtnl_ematch_opnd2txt(uint8_t opnd,
char *buf,
size_t len)
694 snprintf(buf, len,
"%s",
695 opnd < ARRAY_SIZE(operand_txt) ? operand_txt[opnd] :
"?");
Attribute validation policy.
void rtnl_ematch_tree_add(struct rtnl_ematch_tree *tree, struct rtnl_ematch *ematch)
Add ematch object to the end of the ematch tree.
struct rtnl_ematch_tree * rtnl_ematch_tree_alloc(uint16_t progid)
Allocate ematch tree object.
void rtnl_ematch_tree_free(struct rtnl_ematch_tree *tree)
Free ematch tree object.
struct rtnl_ematch_ops * rtnl_ematch_lookup_ops(int kind)
Lookup ematch module by identification number.
struct rtnl_ematch_ops * rtnl_ematch_lookup_ops_by_name(const char *name)
Lookup ematch module by name.
int nla_total_size(int payload)
Return size of attribute including padding.
struct rtnl_ematch * rtnl_ematch_alloc(void)
Allocate ematch object.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int rtnl_ematch_parse_attr(struct nlattr *attr, struct rtnl_ematch_tree **result)
Parse ematch netlink attributes.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int rtnl_ematch_register(struct rtnl_ematch_ops *ops)
Register ematch module.
int nla_len(const struct nlattr *nla)
Return length of the payload .
uint16_t minlen
Minimal length of payload required.
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
void rtnl_ematch_unlink(struct rtnl_ematch *ematch)
Remove ematch from the list of ematches it is linked to.
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.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int rtnl_ematch_add_child(struct rtnl_ematch *parent, struct rtnl_ematch *child)
Add ematch to the end of the parent's list of children.