13#include <netlink-private/netlink.h>
14#include <netlink-private/tc.h>
15#include <netlink/netlink.h>
16#include <netlink/route/classifier.h>
17#include <netlink/route/cls/ematch.h>
18#include <netlink/route/cls/ematch/cmp.h>
19#include <linux/tc_ematch/tc_em_cmp.h>
21#include "ematch_syntax.h"
22#include "ematch_grammar.h"
29static NL_LIST_HEAD(ematch_ops_list);
45 NL_DBG(1,
"ematch module \"%s\" registered\n", ops->eo_name);
47 nl_list_add_tail(&ops->eo_list, &ematch_ops_list);
64 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
65 if (ops->eo_kind == kind)
83 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
84 if (!strcasecmp(ops->eo_name, name))
105 struct rtnl_ematch *e;
107 if (!(e = calloc(1,
sizeof(*e))))
110 NL_DBG(2,
"allocated ematch %p\n", e);
112 NL_INIT_LIST_HEAD(&e->e_list);
113 NL_INIT_LIST_HEAD(&e->e_childs);
126 struct rtnl_ematch *child)
128 if (parent->e_kind != TCF_EM_CONTAINER)
129 return -NLE_OPNOTSUPP;
131 NL_DBG(2,
"added ematch %p \"%s\" to container %p\n",
132 child, child->e_ops->eo_name, parent);
134 nl_list_add_tail(&child->e_list, &parent->e_childs);
145 NL_DBG(2,
"unlinked ematch %p from any lists\n", ematch);
147 if (!nl_list_empty(&ematch->e_childs))
148 NL_DBG(1,
"warning: ematch %p with childs was unlinked\n",
151 nl_list_del(&ematch->e_list);
152 nl_init_list_head(&ematch->e_list);
155void rtnl_ematch_free(
struct rtnl_ematch *ematch)
157 NL_DBG(2,
"freed ematch %p\n", ematch);
159 free(ematch->e_data);
163int rtnl_ematch_set_ops(
struct rtnl_ematch *ematch,
struct rtnl_ematch_ops *ops)
169 ematch->e_kind = ops->eo_kind;
171 if (ops->eo_datalen) {
172 ematch->e_data = calloc(1, ops->eo_datalen);
176 ematch->e_datalen = ops->eo_datalen;
182int rtnl_ematch_set_kind(
struct rtnl_ematch *ematch, uint16_t kind)
189 ematch->e_kind = kind;
192 rtnl_ematch_set_ops(ematch, ops);
197int rtnl_ematch_set_name(
struct rtnl_ematch *ematch,
const char *name)
205 return -NLE_OPNOTSUPP;
207 rtnl_ematch_set_ops(ematch, ops);
212void rtnl_ematch_set_flags(
struct rtnl_ematch *ematch, uint16_t flags)
214 ematch->e_flags |= flags;
217void rtnl_ematch_unset_flags(
struct rtnl_ematch *ematch, uint16_t flags)
219 ematch->e_flags &= ~flags;
222uint16_t rtnl_ematch_get_flags(
struct rtnl_ematch *ematch)
224 return ematch->e_flags;
227void *rtnl_ematch_data(
struct rtnl_ematch *ematch)
229 return ematch->e_data;
244 struct rtnl_ematch_tree *tree;
246 if (!(tree = calloc(1,
sizeof(*tree))))
249 NL_INIT_LIST_HEAD(&tree->et_list);
250 tree->et_progid = progid;
252 NL_DBG(2,
"allocated new ematch tree %p, progid=%u\n", tree, progid);
259 struct rtnl_ematch *pos, *next;
261 nl_list_for_each_entry_safe(pos, next, head, e_list) {
262 if (!nl_list_empty(&pos->e_childs))
263 free_ematch_list(&pos->e_childs);
264 rtnl_ematch_free(pos);
279 free_ematch_list(&tree->et_list);
281 NL_DBG(2,
"Freed ematch tree %p\n", tree);
288 struct rtnl_ematch *
new = NULL, *pos = NULL;
290 nl_list_for_each_entry(pos, src, e_list) {
295 new->e_id = pos->e_id;
296 new->e_kind = pos->e_kind;
297 new->e_flags = pos->e_flags;
298 new->e_index = pos->e_index;
299 new->e_datalen = pos->e_datalen;
302 if (rtnl_ematch_set_ops(
new, pos->e_ops))
306 if (!nl_list_empty(&pos->e_childs)) {
307 if (clone_ematch_list(&new->e_childs, &pos->e_childs) < 0)
310 nl_list_add_tail(&new->e_list, dst);
318 free_ematch_list(dst);
330 struct rtnl_ematch_tree *dst = NULL;
338 clone_ematch_list(&dst->et_list, &src->et_list);
349 struct rtnl_ematch *ematch)
351 nl_list_add_tail(&ematch->e_list, &tree->et_list);
354static inline uint32_t container_ref(
struct rtnl_ematch *ematch)
356 return *((uint32_t *) rtnl_ematch_data(ematch));
359static int link_tree(
struct rtnl_ematch *index[],
int nmatches,
int pos,
362 struct rtnl_ematch *ematch;
365 for (i = pos; i < nmatches; i++) {
368 nl_list_add_tail(&ematch->e_list, root);
370 if (ematch->e_kind == TCF_EM_CONTAINER)
371 link_tree(index, nmatches, container_ref(ematch),
374 if (!(ematch->e_flags & TCF_EM_REL_MASK))
382static struct nla_policy tree_policy[TCA_EMATCH_TREE_MAX+1] = {
383 [TCA_EMATCH_TREE_HDR] = { .
minlen=
sizeof(
struct tcf_ematch_tree_hdr) },
384 [TCA_EMATCH_TREE_LIST] = { .type =
NLA_NESTED },
394 struct nlattr *a, *tb[TCA_EMATCH_TREE_MAX+1];
395 struct tcf_ematch_tree_hdr *thdr;
396 struct rtnl_ematch_tree *tree;
397 struct rtnl_ematch **index;
398 int nmatches = 0, err, remaining;
400 NL_DBG(2,
"Parsing attribute %p as ematch tree\n", attr);
406 if (!tb[TCA_EMATCH_TREE_HDR])
407 return -NLE_MISSING_ATTR;
409 thdr =
nla_data(tb[TCA_EMATCH_TREE_HDR]);
412 if (thdr->nmatches == 0) {
413 NL_DBG(2,
"Ignoring empty ematch configuration\n");
417 if (!tb[TCA_EMATCH_TREE_LIST])
418 return -NLE_MISSING_ATTR;
420 NL_DBG(2,
"ematch tree found with nmatches=%u, progid=%u\n",
421 thdr->nmatches, thdr->progid);
428 if (thdr->nmatches > (
nla_len(tb[TCA_EMATCH_TREE_LIST]) /
432 if (!(index = calloc(thdr->nmatches,
sizeof(
struct rtnl_ematch *))))
442 struct tcf_ematch_hdr *hdr;
443 struct rtnl_ematch *ematch;
447 NL_DBG(3,
"parsing ematch attribute %d, len=%u\n",
450 if (
nla_len(a) <
sizeof(*hdr)) {
456 if (nmatches >= thdr->nmatches) {
462 data = (
char *)
nla_data(a) + NLA_ALIGN(
sizeof(*hdr));
463 len =
nla_len(a) - NLA_ALIGN(
sizeof(*hdr));
465 NL_DBG(3,
"ematch attribute matchid=%u, kind=%u, flags=%u\n",
466 hdr->matchid, hdr->kind, hdr->flags);
472 if (hdr->kind == TCF_EM_CONTAINER &&
473 *((uint32_t *) data) >= thdr->nmatches) {
483 ematch->e_id = hdr->matchid;
484 ematch->e_kind = hdr->kind;
485 ematch->e_flags = hdr->flags;
488 if (ops->eo_minlen && len < ops->eo_minlen) {
489 rtnl_ematch_free(ematch);
494 rtnl_ematch_set_ops(ematch, ops);
497 (err = ops->eo_parse(ematch, data, len)) < 0) {
498 rtnl_ematch_free(ematch);
503 NL_DBG(3,
"index[%d] = %p\n", nmatches, ematch);
504 index[nmatches++] = ematch;
507 if (nmatches != thdr->nmatches) {
512 err = link_tree(index, nmatches, 0, &tree->et_list);
527static void dump_ematch_sequence(
struct nl_list_head *head,
530 struct rtnl_ematch *match;
532 nl_list_for_each_entry(match, head, e_list) {
533 if (match->e_flags & TCF_EM_INVERT)
536 if (match->e_kind == TCF_EM_CONTAINER) {
538 dump_ematch_sequence(&match->e_childs, p);
540 }
else if (!match->e_ops) {
541 nl_dump(p,
"[unknown ematch %d]", match->e_kind);
543 if (match->e_ops->eo_dump)
544 match->e_ops->eo_dump(match, p);
549 switch (match->e_flags & TCF_EM_REL_MASK) {
563void rtnl_ematch_tree_dump(
struct rtnl_ematch_tree *tree,
569 dump_ematch_sequence(&tree->et_list, p);
573static int update_container_index(
struct nl_list_head *list,
int *index)
575 struct rtnl_ematch *e;
577 nl_list_for_each_entry(e, list, e_list)
578 e->e_index = (*index)++;
580 nl_list_for_each_entry(e, list, e_list) {
581 if (e->e_kind == TCF_EM_CONTAINER) {
584 if (nl_list_empty(&e->e_childs))
585 return -NLE_OBJ_NOTFOUND;
587 *((uint32_t *) e->e_data) = *index;
589 err = update_container_index(&e->e_childs, index);
598static int fill_ematch_sequence(
struct nl_msg *msg,
struct nl_list_head *list)
600 struct rtnl_ematch *e;
602 nl_list_for_each_entry(e, list, e_list) {
603 struct tcf_ematch_hdr match = {
617 if (e->e_ops->eo_fill)
618 err = e->e_ops->eo_fill(e, msg);
619 else if (e->e_flags & TCF_EM_SIMPLE)
621 else if (e->e_datalen > 0)
624 NL_DBG(3,
"msg %p: added ematch [%d] id=%d kind=%d flags=%d\n",
625 msg, e->e_index, match.matchid, match.kind, match.flags);
633 nl_list_for_each_entry(e, list, e_list) {
634 if (e->e_kind == TCF_EM_CONTAINER &&
635 fill_ematch_sequence(msg, &e->e_childs) < 0)
642int rtnl_ematch_fill_attr(
struct nl_msg *msg,
int attrid,
643 struct rtnl_ematch_tree *tree)
645 struct tcf_ematch_tree_hdr thdr = {
646 .progid = tree->et_progid,
648 struct nlattr *list, *topattr;
653 err = update_container_index(&tree->et_list, &index);
658 goto nla_put_failure;
660 thdr.nmatches = index;
661 NLA_PUT(msg, TCA_EMATCH_TREE_HDR,
sizeof(thdr), &thdr);
664 goto nla_put_failure;
666 if (fill_ematch_sequence(msg, &tree->et_list) < 0)
667 goto nla_put_failure;
681extern int ematch_parse(
void *,
char **,
struct nl_list_head *);
683int rtnl_ematch_parse_expr(
const char *expr,
char **errp,
684 struct rtnl_ematch_tree **result)
686 struct rtnl_ematch_tree *tree;
687 YY_BUFFER_STATE buf = NULL;
688 yyscan_t scanner = NULL;
691 NL_DBG(2,
"Parsing ematch expression \"%s\"\n", expr);
696 if (ematch_lex_init(&scanner) < 0) {
701 buf = ematch__scan_string(expr, scanner);
703 if (ematch_parse(scanner, errp, &tree->et_list) != 0) {
704 ematch__delete_buffer(buf, scanner);
705 err = -NLE_PARSE_ERR;
709 ematch_lex_destroy(scanner);
716 ematch_lex_destroy(scanner);
723static const char *layer_txt[] = {
724 [TCF_LAYER_LINK] =
"eth",
725 [TCF_LAYER_NETWORK] =
"ip",
726 [TCF_LAYER_TRANSPORT] =
"tcp",
729char *rtnl_ematch_offset2txt(uint8_t layer, uint16_t offset,
char *buf,
size_t len)
731 snprintf(buf, len,
"%s+%u",
732 (layer <= TCF_LAYER_MAX) ? layer_txt[layer] :
"?",
738static const char *operand_txt[] = {
739 [TCF_EM_OPND_EQ] =
"=",
740 [TCF_EM_OPND_LT] =
"<",
741 [TCF_EM_OPND_GT] =
">",
744char *rtnl_ematch_opnd2txt(uint8_t opnd,
char *buf,
size_t len)
746 snprintf(buf, len,
"%s",
747 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.