13#include "nl-default.h"
15#include <linux/tc_ematch/tc_em_cmp.h>
17#include <netlink/netlink.h>
18#include <netlink/route/classifier.h>
19#include <netlink/route/cls/ematch.h>
20#include <netlink/route/cls/ematch/cmp.h>
23#include "nl-aux-core/nl-core.h"
25#include "ematch_syntax.h"
26#include "ematch_grammar.h"
33static NL_LIST_HEAD(ematch_ops_list);
49 NL_DBG(1,
"ematch module \"%s\" registered\n", ops->eo_name);
51 nl_list_add_tail(&ops->eo_list, &ematch_ops_list);
68 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
69 if (ops->eo_kind == kind)
87 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
88 if (!strcasecmp(ops->eo_name, name))
109 struct rtnl_ematch *e;
111 if (!(e = calloc(1,
sizeof(*e))))
114 NL_DBG(2,
"allocated ematch %p\n", e);
116 NL_INIT_LIST_HEAD(&e->e_list);
117 NL_INIT_LIST_HEAD(&e->e_childs);
130 struct rtnl_ematch *child)
132 if (parent->e_kind != TCF_EM_CONTAINER)
133 return -NLE_OPNOTSUPP;
135 NL_DBG(2,
"added ematch %p \"%s\" to container %p\n",
136 child, child->e_ops->eo_name, parent);
138 nl_list_add_tail(&child->e_list, &parent->e_childs);
149 NL_DBG(2,
"unlinked ematch %p from any lists\n", ematch);
151 if (!nl_list_empty(&ematch->e_childs))
152 NL_DBG(1,
"warning: ematch %p with childs was unlinked\n",
155 nl_list_del(&ematch->e_list);
156 nl_init_list_head(&ematch->e_list);
159void rtnl_ematch_free(
struct rtnl_ematch *ematch)
161 NL_DBG(2,
"freed ematch %p\n", ematch);
163 free(ematch->e_data);
167int rtnl_ematch_set_ops(
struct rtnl_ematch *ematch,
struct rtnl_ematch_ops *ops)
173 ematch->e_kind = ops->eo_kind;
175 if (ops->eo_datalen) {
176 ematch->e_data = calloc(1, ops->eo_datalen);
180 ematch->e_datalen = ops->eo_datalen;
186int rtnl_ematch_set_kind(
struct rtnl_ematch *ematch, uint16_t kind)
193 ematch->e_kind = kind;
196 rtnl_ematch_set_ops(ematch, ops);
201int rtnl_ematch_set_name(
struct rtnl_ematch *ematch,
const char *name)
209 return -NLE_OPNOTSUPP;
211 rtnl_ematch_set_ops(ematch, ops);
216void rtnl_ematch_set_flags(
struct rtnl_ematch *ematch, uint16_t flags)
218 ematch->e_flags |= flags;
221void rtnl_ematch_unset_flags(
struct rtnl_ematch *ematch, uint16_t flags)
223 ematch->e_flags &= ~flags;
226uint16_t rtnl_ematch_get_flags(
struct rtnl_ematch *ematch)
228 return ematch->e_flags;
231void *rtnl_ematch_data(
struct rtnl_ematch *ematch)
233 return ematch->e_data;
248 struct rtnl_ematch_tree *tree;
250 if (!(tree = calloc(1,
sizeof(*tree))))
253 NL_INIT_LIST_HEAD(&tree->et_list);
254 tree->et_progid = progid;
256 NL_DBG(2,
"allocated new ematch tree %p, progid=%u\n", tree, progid);
263 struct rtnl_ematch *pos, *next;
265 nl_list_for_each_entry_safe(pos, next, head, e_list) {
266 if (!nl_list_empty(&pos->e_childs))
267 free_ematch_list(&pos->e_childs);
268 rtnl_ematch_free(pos);
283 free_ematch_list(&tree->et_list);
285 NL_DBG(2,
"Freed ematch tree %p\n", tree);
292 struct rtnl_ematch *
new = NULL, *pos = NULL;
294 nl_list_for_each_entry(pos, src, e_list) {
299 new->e_id = pos->e_id;
300 new->e_kind = pos->e_kind;
301 new->e_flags = pos->e_flags;
302 new->e_index = pos->e_index;
303 new->e_datalen = pos->e_datalen;
306 if (rtnl_ematch_set_ops(
new, pos->e_ops))
310 if (!nl_list_empty(&pos->e_childs)) {
311 if (clone_ematch_list(&new->e_childs, &pos->e_childs) < 0)
314 nl_list_add_tail(&new->e_list, dst);
322 free_ematch_list(dst);
334 struct rtnl_ematch_tree *dst = NULL;
342 clone_ematch_list(&dst->et_list, &src->et_list);
353 struct rtnl_ematch *ematch)
355 nl_list_add_tail(&ematch->e_list, &tree->et_list);
358static inline uint32_t container_ref(
struct rtnl_ematch *ematch)
360 return *((uint32_t *) rtnl_ematch_data(ematch));
363static int link_tree(
struct rtnl_ematch *index[],
int nmatches,
int pos,
366 struct rtnl_ematch *ematch;
369 for (i = pos; i < nmatches; i++) {
372 nl_list_add_tail(&ematch->e_list, root);
374 if (ematch->e_kind == TCF_EM_CONTAINER)
375 link_tree(index, nmatches, container_ref(ematch),
378 if (!(ematch->e_flags & TCF_EM_REL_MASK))
386static struct nla_policy tree_policy[TCA_EMATCH_TREE_MAX+1] = {
387 [TCA_EMATCH_TREE_HDR] = { .
minlen=
sizeof(
struct tcf_ematch_tree_hdr) },
388 [TCA_EMATCH_TREE_LIST] = { .type =
NLA_NESTED },
398 struct nlattr *a, *tb[TCA_EMATCH_TREE_MAX+1];
399 struct tcf_ematch_tree_hdr *thdr;
400 struct rtnl_ematch_tree *tree;
401 struct rtnl_ematch **index;
402 int nmatches = 0, err, remaining;
404 NL_DBG(2,
"Parsing attribute %p as ematch tree\n", attr);
410 if (!tb[TCA_EMATCH_TREE_HDR])
411 return -NLE_MISSING_ATTR;
413 thdr =
nla_data(tb[TCA_EMATCH_TREE_HDR]);
416 if (thdr->nmatches == 0) {
417 NL_DBG(2,
"Ignoring empty ematch configuration\n");
421 if (!tb[TCA_EMATCH_TREE_LIST])
422 return -NLE_MISSING_ATTR;
424 NL_DBG(2,
"ematch tree found with nmatches=%u, progid=%u\n",
425 thdr->nmatches, thdr->progid);
432 if (thdr->nmatches > (
nla_len(tb[TCA_EMATCH_TREE_LIST]) /
436 if (!(index = calloc(thdr->nmatches,
sizeof(
struct rtnl_ematch *))))
446 struct tcf_ematch_hdr *hdr;
447 struct rtnl_ematch *ematch;
451 NL_DBG(3,
"parsing ematch attribute %d, len=%u\n",
454 if (
nla_len(a) <
sizeof(*hdr)) {
460 if (nmatches >= thdr->nmatches) {
466 data = (
char *)
nla_data(a) + NLA_ALIGN(
sizeof(*hdr));
467 len =
nla_len(a) - NLA_ALIGN(
sizeof(*hdr));
469 NL_DBG(3,
"ematch attribute matchid=%u, kind=%u, flags=%u\n",
470 hdr->matchid, hdr->kind, hdr->flags);
476 if (hdr->kind == TCF_EM_CONTAINER &&
477 *((uint32_t *) data) >= thdr->nmatches) {
487 ematch->e_id = hdr->matchid;
488 ematch->e_kind = hdr->kind;
489 ematch->e_flags = hdr->flags;
492 if (ops->eo_minlen && len < ops->eo_minlen) {
493 rtnl_ematch_free(ematch);
498 rtnl_ematch_set_ops(ematch, ops);
501 (err = ops->eo_parse(ematch, data, len)) < 0) {
502 rtnl_ematch_free(ematch);
507 NL_DBG(3,
"index[%d] = %p\n", nmatches, ematch);
508 index[nmatches++] = ematch;
511 if (nmatches != thdr->nmatches) {
516 err = link_tree(index, nmatches, 0, &tree->et_list);
531static void dump_ematch_sequence(
struct nl_list_head *head,
534 struct rtnl_ematch *match;
536 nl_list_for_each_entry(match, head, e_list) {
537 if (match->e_flags & TCF_EM_INVERT)
540 if (match->e_kind == TCF_EM_CONTAINER) {
542 dump_ematch_sequence(&match->e_childs, p);
544 }
else if (!match->e_ops) {
545 nl_dump(p,
"[unknown ematch %d]", match->e_kind);
547 if (match->e_ops->eo_dump)
548 match->e_ops->eo_dump(match, p);
553 switch (match->e_flags & TCF_EM_REL_MASK) {
567void rtnl_ematch_tree_dump(
struct rtnl_ematch_tree *tree,
573 dump_ematch_sequence(&tree->et_list, p);
577static int update_container_index(
struct nl_list_head *list,
int *index)
579 struct rtnl_ematch *e;
581 nl_list_for_each_entry(e, list, e_list)
582 e->e_index = (*index)++;
584 nl_list_for_each_entry(e, list, e_list) {
585 if (e->e_kind == TCF_EM_CONTAINER) {
588 if (nl_list_empty(&e->e_childs))
589 return -NLE_OBJ_NOTFOUND;
591 *((uint32_t *) e->e_data) = *index;
593 err = update_container_index(&e->e_childs, index);
602static int fill_ematch_sequence(
struct nl_msg *msg,
struct nl_list_head *list)
604 struct rtnl_ematch *e;
606 nl_list_for_each_entry(e, list, e_list) {
607 struct tcf_ematch_hdr match = {
621 if (e->e_ops->eo_fill)
622 err = e->e_ops->eo_fill(e, msg);
623 else if (e->e_flags & TCF_EM_SIMPLE)
625 else if (e->e_datalen > 0)
628 NL_DBG(3,
"msg %p: added ematch [%d] id=%d kind=%d flags=%d\n",
629 msg, e->e_index, match.matchid, match.kind, match.flags);
637 nl_list_for_each_entry(e, list, e_list) {
638 if (e->e_kind == TCF_EM_CONTAINER &&
639 fill_ematch_sequence(msg, &e->e_childs) < 0)
646int rtnl_ematch_fill_attr(
struct nl_msg *msg,
int attrid,
647 struct rtnl_ematch_tree *tree)
649 struct tcf_ematch_tree_hdr thdr = {
650 .progid = tree->et_progid,
652 struct nlattr *list, *topattr;
657 err = update_container_index(&tree->et_list, &index);
662 goto nla_put_failure;
664 thdr.nmatches = index;
665 NLA_PUT(msg, TCA_EMATCH_TREE_HDR,
sizeof(thdr), &thdr);
668 goto nla_put_failure;
670 if (fill_ematch_sequence(msg, &tree->et_list) < 0)
671 goto nla_put_failure;
685extern int ematch_parse(
void *,
char **,
struct nl_list_head *);
687int rtnl_ematch_parse_expr(
const char *expr,
char **errp,
688 struct rtnl_ematch_tree **result)
690 struct rtnl_ematch_tree *tree;
692 yyscan_t scanner = NULL;
695 NL_DBG(2,
"Parsing ematch expression \"%s\"\n", expr);
700 if (ematch_lex_init(&scanner) < 0) {
705 buf = ematch__scan_string(expr, scanner);
707 if (ematch_parse(scanner, errp, &tree->et_list) != 0) {
708 ematch__delete_buffer(buf, scanner);
709 err = -NLE_PARSE_ERR;
713 ematch_lex_destroy(scanner);
720 ematch_lex_destroy(scanner);
727static const char *layer_txt[] = {
728 [TCF_LAYER_LINK] =
"eth",
729 [TCF_LAYER_NETWORK] =
"ip",
730 [TCF_LAYER_TRANSPORT] =
"tcp",
733char *rtnl_ematch_offset2txt(uint8_t layer, uint16_t offset,
char *buf,
size_t len)
735 snprintf(buf, len,
"%s+%u",
736 (layer <= TCF_LAYER_MAX) ? layer_txt[layer] :
"?",
742static const char *operand_txt[] = {
743 [TCF_EM_OPND_EQ] =
"=",
744 [TCF_EM_OPND_LT] =
"<",
745 [TCF_EM_OPND_GT] =
">",
748char *rtnl_ematch_opnd2txt(uint8_t opnd,
char *buf,
size_t len)
750 snprintf(buf, len,
"%s",
751 opnd < ARRAY_SIZE(operand_txt) ? operand_txt[opnd] :
"?");
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
#define nla_for_each_nested(pos, nla, rem)
Iterate over a stream of nested attributes.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nla_total_size(int payload)
Return size of attribute including padding.
@ NLA_NESTED
Nested attributes.
struct rtnl_ematch_ops * rtnl_ematch_lookup_ops(int kind)
Lookup ematch module by identification number.
void rtnl_ematch_tree_add(struct rtnl_ematch_tree *tree, struct rtnl_ematch *ematch)
Add ematch object to the end of the ematch tree.
void rtnl_ematch_tree_free(struct rtnl_ematch_tree *tree)
Free ematch tree object.
struct rtnl_ematch_tree * rtnl_ematch_tree_alloc(uint16_t progid)
Allocate ematch tree object.
struct rtnl_ematch * rtnl_ematch_alloc(void)
Allocate ematch object.
int rtnl_ematch_register(struct rtnl_ematch_ops *ops)
Register ematch module.
int rtnl_ematch_parse_attr(struct nlattr *attr, struct rtnl_ematch_tree **result)
Parse ematch netlink attributes.
void rtnl_ematch_unlink(struct rtnl_ematch *ematch)
Remove ematch from the list of ematches it is linked to.
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.
struct rtnl_ematch_ops * rtnl_ematch_lookup_ops_by_name(const char *name)
Lookup ematch module by name.
struct rtnl_ematch_tree * rtnl_ematch_tree_clone(struct rtnl_ematch_tree *src)
Clone ematch tree object.
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.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.
Extended Match Operations.