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> 25 #include <linux/tc_ematch/tc_em_cmp.h> 27 #include "ematch_syntax.h" 28 #include "ematch_grammar.h" 35 static NL_LIST_HEAD(ematch_ops_list);
51 NL_DBG(1,
"ematch module \"%s\" registered\n", ops->eo_name);
53 nl_list_add_tail(&ops->eo_list, &ematch_ops_list);
70 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
71 if (ops->eo_kind == kind)
89 nl_list_for_each_entry(ops, &ematch_ops_list, eo_list)
90 if (!strcasecmp(ops->eo_name, name))
111 struct rtnl_ematch *e;
113 if (!(e = calloc(1,
sizeof(*e))))
116 NL_DBG(2,
"allocated ematch %p\n", e);
118 NL_INIT_LIST_HEAD(&e->e_list);
119 NL_INIT_LIST_HEAD(&e->e_childs);
132 struct rtnl_ematch *child)
134 if (parent->e_kind != TCF_EM_CONTAINER)
135 return -NLE_OPNOTSUPP;
137 NL_DBG(2,
"added ematch %p \"%s\" to container %p\n",
138 child, child->e_ops->eo_name, parent);
140 nl_list_add_tail(&child->e_list, &parent->e_childs);
151 NL_DBG(2,
"unlinked ematch %p from any lists\n", ematch);
153 if (!nl_list_empty(&ematch->e_childs))
154 NL_DBG(1,
"warning: ematch %p with childs was unlinked\n",
157 nl_list_del(&ematch->e_list);
158 nl_init_list_head(&ematch->e_list);
161 void rtnl_ematch_free(
struct rtnl_ematch *ematch)
163 NL_DBG(2,
"freed ematch %p\n", ematch);
165 free(ematch->e_data);
169 int rtnl_ematch_set_ops(
struct rtnl_ematch *ematch,
struct rtnl_ematch_ops *ops)
175 ematch->e_kind = ops->eo_kind;
177 if (ops->eo_datalen) {
178 ematch->e_data = calloc(1, ops->eo_datalen);
182 ematch->e_datalen = ops->eo_datalen;
188 int rtnl_ematch_set_kind(
struct rtnl_ematch *ematch, uint16_t kind)
195 ematch->e_kind = kind;
198 rtnl_ematch_set_ops(ematch, ops);
203 int rtnl_ematch_set_name(
struct rtnl_ematch *ematch,
const char *name)
211 return -NLE_OPNOTSUPP;
213 rtnl_ematch_set_ops(ematch, ops);
218 void rtnl_ematch_set_flags(
struct rtnl_ematch *ematch, uint16_t flags)
220 ematch->e_flags |= flags;
223 void rtnl_ematch_unset_flags(
struct rtnl_ematch *ematch, uint16_t flags)
225 ematch->e_flags &= ~flags;
228 uint16_t rtnl_ematch_get_flags(
struct rtnl_ematch *ematch)
230 return ematch->e_flags;
233 void *rtnl_ematch_data(
struct rtnl_ematch *ematch)
235 return ematch->e_data;
250 struct rtnl_ematch_tree *tree;
252 if (!(tree = calloc(1,
sizeof(*tree))))
255 NL_INIT_LIST_HEAD(&tree->et_list);
256 tree->et_progid = progid;
258 NL_DBG(2,
"allocated new ematch tree %p, progid=%u\n", tree, progid);
265 struct rtnl_ematch *pos, *next;
267 nl_list_for_each_entry_safe(pos, next, head, e_list) {
268 if (!nl_list_empty(&pos->e_childs))
269 free_ematch_list(&pos->e_childs);
270 rtnl_ematch_free(pos);
285 free_ematch_list(&tree->et_list);
287 NL_DBG(2,
"Freed ematch tree %p\n", tree);
298 struct rtnl_ematch *ematch)
300 nl_list_add_tail(&ematch->e_list, &tree->et_list);
303 static inline uint32_t container_ref(
struct rtnl_ematch *ematch)
305 return *((uint32_t *) rtnl_ematch_data(ematch));
308 static int link_tree(
struct rtnl_ematch *index[],
int nmatches,
int pos,
311 struct rtnl_ematch *ematch;
314 for (i = pos; i < nmatches; i++) {
317 nl_list_add_tail(&ematch->e_list, root);
319 if (ematch->e_kind == TCF_EM_CONTAINER)
320 link_tree(index, nmatches, container_ref(ematch),
323 if (!(ematch->e_flags & TCF_EM_REL_MASK))
331 static struct nla_policy tree_policy[TCA_EMATCH_TREE_MAX+1] = {
332 [TCA_EMATCH_TREE_HDR] = { .
minlen=
sizeof(
struct tcf_ematch_tree_hdr) },
333 [TCA_EMATCH_TREE_LIST] = { .type =
NLA_NESTED },
343 struct nlattr *a, *tb[TCA_EMATCH_TREE_MAX+1];
344 struct tcf_ematch_tree_hdr *thdr;
345 struct rtnl_ematch_tree *tree;
346 struct rtnl_ematch **index;
347 int nmatches = 0, err, remaining;
349 NL_DBG(2,
"Parsing attribute %p as ematch tree\n", attr);
355 if (!tb[TCA_EMATCH_TREE_HDR])
356 return -NLE_MISSING_ATTR;
358 thdr =
nla_data(tb[TCA_EMATCH_TREE_HDR]);
361 if (thdr->nmatches == 0) {
362 NL_DBG(2,
"Ignoring empty ematch configuration\n");
366 if (!tb[TCA_EMATCH_TREE_LIST])
367 return -NLE_MISSING_ATTR;
369 NL_DBG(2,
"ematch tree found with nmatches=%u, progid=%u\n",
370 thdr->nmatches, thdr->progid);
377 if (thdr->nmatches > (
nla_len(tb[TCA_EMATCH_TREE_LIST]) /
381 if (!(index = calloc(thdr->nmatches,
sizeof(
struct rtnl_ematch *))))
391 struct tcf_ematch_hdr *hdr;
392 struct rtnl_ematch *ematch;
396 NL_DBG(3,
"parsing ematch attribute %d, len=%u\n",
399 if (
nla_len(a) <
sizeof(*hdr)) {
405 if (nmatches >= thdr->nmatches) {
411 data =
nla_data(a) + NLA_ALIGN(
sizeof(*hdr));
412 len =
nla_len(a) - NLA_ALIGN(
sizeof(*hdr));
414 NL_DBG(3,
"ematch attribute matchid=%u, kind=%u, flags=%u\n",
415 hdr->matchid, hdr->kind, hdr->flags);
421 if (hdr->kind == TCF_EM_CONTAINER &&
422 *((uint32_t *) data) >= thdr->nmatches) {
432 ematch->e_id = hdr->matchid;
433 ematch->e_kind = hdr->kind;
434 ematch->e_flags = hdr->flags;
437 if (ops->eo_minlen && len < ops->eo_minlen) {
438 rtnl_ematch_free(ematch);
443 rtnl_ematch_set_ops(ematch, ops);
446 (err = ops->eo_parse(ematch, data, len)) < 0) {
447 rtnl_ematch_free(ematch);
452 NL_DBG(3,
"index[%d] = %p\n", nmatches, ematch);
453 index[nmatches++] = ematch;
456 if (nmatches != thdr->nmatches) {
461 err = link_tree(index, nmatches, 0, &tree->et_list);
476 static void dump_ematch_sequence(
struct nl_list_head *head,
479 struct rtnl_ematch *match;
481 nl_list_for_each_entry(match, head, e_list) {
482 if (match->e_flags & TCF_EM_INVERT)
485 if (match->e_kind == TCF_EM_CONTAINER) {
487 dump_ematch_sequence(&match->e_childs, p);
489 }
else if (!match->e_ops) {
490 nl_dump(p,
"[unknown ematch %d]", match->e_kind);
492 if (match->e_ops->eo_dump)
493 match->e_ops->eo_dump(match, p);
498 switch (match->e_flags & TCF_EM_REL_MASK) {
512 void rtnl_ematch_tree_dump(
struct rtnl_ematch_tree *tree,
518 dump_ematch_sequence(&tree->et_list, p);
522 static int update_container_index(
struct nl_list_head *list,
int *index)
524 struct rtnl_ematch *e;
526 nl_list_for_each_entry(e, list, e_list)
527 e->e_index = (*index)++;
529 nl_list_for_each_entry(e, list, e_list) {
530 if (e->e_kind == TCF_EM_CONTAINER) {
533 if (nl_list_empty(&e->e_childs))
534 return -NLE_OBJ_NOTFOUND;
536 *((uint32_t *) e->e_data) = *index;
538 err = update_container_index(&e->e_childs, index);
547 static int fill_ematch_sequence(
struct nl_msg *msg,
struct nl_list_head *list)
549 struct rtnl_ematch *e;
551 nl_list_for_each_entry(e, list, e_list) {
552 struct tcf_ematch_hdr match = {
566 if (e->e_ops->eo_fill)
567 err = e->e_ops->eo_fill(e, msg);
568 else if (e->e_flags & TCF_EM_SIMPLE)
570 else if (e->e_datalen > 0)
573 NL_DBG(3,
"msg %p: added ematch [%d] id=%d kind=%d flags=%d\n",
574 msg, e->e_index, match.matchid, match.kind, match.flags);
582 nl_list_for_each_entry(e, list, e_list) {
583 if (e->e_kind == TCF_EM_CONTAINER &&
584 fill_ematch_sequence(msg, &e->e_childs) < 0)
591 int rtnl_ematch_fill_attr(
struct nl_msg *msg,
int attrid,
592 struct rtnl_ematch_tree *tree)
594 struct tcf_ematch_tree_hdr thdr = {
595 .progid = tree->et_progid,
597 struct nlattr *list, *topattr;
602 err = update_container_index(&tree->et_list, &index);
607 goto nla_put_failure;
609 thdr.nmatches = index;
610 NLA_PUT(msg, TCA_EMATCH_TREE_HDR,
sizeof(thdr), &thdr);
613 goto nla_put_failure;
615 if (fill_ematch_sequence(msg, &tree->et_list) < 0)
616 goto nla_put_failure;
630 extern int ematch_parse(
void *,
char **,
struct nl_list_head *);
632 int rtnl_ematch_parse_expr(
const char *expr,
char **errp,
633 struct rtnl_ematch_tree **result)
635 struct rtnl_ematch_tree *tree;
637 yyscan_t scanner = NULL;
640 NL_DBG(2,
"Parsing ematch expression \"%s\"\n", expr);
645 if ((err = ematch_lex_init(&scanner)) < 0) {
650 buf = ematch__scan_string(expr, scanner);
652 if ((err = ematch_parse(scanner, errp, &tree->et_list)) != 0) {
653 ematch__delete_buffer(buf, scanner);
654 err = -NLE_PARSE_ERR;
658 ematch_lex_destroy(scanner);
665 ematch_lex_destroy(scanner);
672 static const char *layer_txt[] = {
673 [TCF_LAYER_LINK] =
"eth",
674 [TCF_LAYER_NETWORK] =
"ip",
675 [TCF_LAYER_TRANSPORT] =
"tcp",
678 char *rtnl_ematch_offset2txt(uint8_t layer, uint16_t offset,
char *buf,
size_t len)
680 snprintf(buf, len,
"%s+%u",
681 (layer <= TCF_LAYER_MAX) ? layer_txt[layer] :
"?",
687 static const char *operand_txt[] = {
688 [TCF_EM_OPND_EQ] =
"=",
689 [TCF_EM_OPND_LT] =
"<",
690 [TCF_EM_OPND_GT] =
">",
693 char *rtnl_ematch_opnd2txt(uint8_t opnd,
char *buf,
size_t len)
695 snprintf(buf, len,
"%s",
696 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.