162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * net/sched/ematch.c Extended Match API 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Authors: Thomas Graf <tgraf@suug.ch> 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * ========================================================================== 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * An extended match (ematch) is a small classification tool not worth 1062306a36Sopenharmony_ci * writing a full classifier for. Ematches can be interconnected to form 1162306a36Sopenharmony_ci * a logic expression and get attached to classifiers to extend their 1262306a36Sopenharmony_ci * functionatlity. 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * The userspace part transforms the logic expressions into an array 1562306a36Sopenharmony_ci * consisting of multiple sequences of interconnected ematches separated 1662306a36Sopenharmony_ci * by markers. Precedence is implemented by a special ematch kind 1762306a36Sopenharmony_ci * referencing a sequence beyond the marker of the current sequence 1862306a36Sopenharmony_ci * causing the current position in the sequence to be pushed onto a stack 1962306a36Sopenharmony_ci * to allow the current position to be overwritten by the position referenced 2062306a36Sopenharmony_ci * in the special ematch. Matching continues in the new sequence until a 2162306a36Sopenharmony_ci * marker is reached causing the position to be restored from the stack. 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * Example: 2462306a36Sopenharmony_ci * A AND (B1 OR B2) AND C AND D 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * ------->-PUSH------- 2762306a36Sopenharmony_ci * -->-- / -->-- \ -->-- 2862306a36Sopenharmony_ci * / \ / / \ \ / \ 2962306a36Sopenharmony_ci * +-------+-------+-------+-------+-------+--------+ 3062306a36Sopenharmony_ci * | A AND | B AND | C AND | D END | B1 OR | B2 END | 3162306a36Sopenharmony_ci * +-------+-------+-------+-------+-------+--------+ 3262306a36Sopenharmony_ci * \ / 3362306a36Sopenharmony_ci * --------<-POP--------- 3462306a36Sopenharmony_ci * 3562306a36Sopenharmony_ci * where B is a virtual ematch referencing to sequence starting with B1. 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * ========================================================================== 3862306a36Sopenharmony_ci * 3962306a36Sopenharmony_ci * How to write an ematch in 60 seconds 4062306a36Sopenharmony_ci * ------------------------------------ 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * 1) Provide a matcher function: 4362306a36Sopenharmony_ci * static int my_match(struct sk_buff *skb, struct tcf_ematch *m, 4462306a36Sopenharmony_ci * struct tcf_pkt_info *info) 4562306a36Sopenharmony_ci * { 4662306a36Sopenharmony_ci * struct mydata *d = (struct mydata *) m->data; 4762306a36Sopenharmony_ci * 4862306a36Sopenharmony_ci * if (...matching goes here...) 4962306a36Sopenharmony_ci * return 1; 5062306a36Sopenharmony_ci * else 5162306a36Sopenharmony_ci * return 0; 5262306a36Sopenharmony_ci * } 5362306a36Sopenharmony_ci * 5462306a36Sopenharmony_ci * 2) Fill out a struct tcf_ematch_ops: 5562306a36Sopenharmony_ci * static struct tcf_ematch_ops my_ops = { 5662306a36Sopenharmony_ci * .kind = unique id, 5762306a36Sopenharmony_ci * .datalen = sizeof(struct mydata), 5862306a36Sopenharmony_ci * .match = my_match, 5962306a36Sopenharmony_ci * .owner = THIS_MODULE, 6062306a36Sopenharmony_ci * }; 6162306a36Sopenharmony_ci * 6262306a36Sopenharmony_ci * 3) Register/Unregister your ematch: 6362306a36Sopenharmony_ci * static int __init init_my_ematch(void) 6462306a36Sopenharmony_ci * { 6562306a36Sopenharmony_ci * return tcf_em_register(&my_ops); 6662306a36Sopenharmony_ci * } 6762306a36Sopenharmony_ci * 6862306a36Sopenharmony_ci * static void __exit exit_my_ematch(void) 6962306a36Sopenharmony_ci * { 7062306a36Sopenharmony_ci * tcf_em_unregister(&my_ops); 7162306a36Sopenharmony_ci * } 7262306a36Sopenharmony_ci * 7362306a36Sopenharmony_ci * module_init(init_my_ematch); 7462306a36Sopenharmony_ci * module_exit(exit_my_ematch); 7562306a36Sopenharmony_ci * 7662306a36Sopenharmony_ci * 4) By now you should have two more seconds left, barely enough to 7762306a36Sopenharmony_ci * open up a beer to watch the compilation going. 7862306a36Sopenharmony_ci */ 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci#include <linux/module.h> 8162306a36Sopenharmony_ci#include <linux/slab.h> 8262306a36Sopenharmony_ci#include <linux/types.h> 8362306a36Sopenharmony_ci#include <linux/kernel.h> 8462306a36Sopenharmony_ci#include <linux/errno.h> 8562306a36Sopenharmony_ci#include <linux/rtnetlink.h> 8662306a36Sopenharmony_ci#include <linux/skbuff.h> 8762306a36Sopenharmony_ci#include <net/pkt_cls.h> 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_cistatic LIST_HEAD(ematch_ops); 9062306a36Sopenharmony_cistatic DEFINE_RWLOCK(ematch_mod_lock); 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_cistatic struct tcf_ematch_ops *tcf_em_lookup(u16 kind) 9362306a36Sopenharmony_ci{ 9462306a36Sopenharmony_ci struct tcf_ematch_ops *e = NULL; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci read_lock(&ematch_mod_lock); 9762306a36Sopenharmony_ci list_for_each_entry(e, &ematch_ops, link) { 9862306a36Sopenharmony_ci if (kind == e->kind) { 9962306a36Sopenharmony_ci if (!try_module_get(e->owner)) 10062306a36Sopenharmony_ci e = NULL; 10162306a36Sopenharmony_ci read_unlock(&ematch_mod_lock); 10262306a36Sopenharmony_ci return e; 10362306a36Sopenharmony_ci } 10462306a36Sopenharmony_ci } 10562306a36Sopenharmony_ci read_unlock(&ematch_mod_lock); 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci return NULL; 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci/** 11162306a36Sopenharmony_ci * tcf_em_register - register an extended match 11262306a36Sopenharmony_ci * 11362306a36Sopenharmony_ci * @ops: ematch operations lookup table 11462306a36Sopenharmony_ci * 11562306a36Sopenharmony_ci * This function must be called by ematches to announce their presence. 11662306a36Sopenharmony_ci * The given @ops must have kind set to a unique identifier and the 11762306a36Sopenharmony_ci * callback match() must be implemented. All other callbacks are optional 11862306a36Sopenharmony_ci * and a fallback implementation is used instead. 11962306a36Sopenharmony_ci * 12062306a36Sopenharmony_ci * Returns -EEXISTS if an ematch of the same kind has already registered. 12162306a36Sopenharmony_ci */ 12262306a36Sopenharmony_ciint tcf_em_register(struct tcf_ematch_ops *ops) 12362306a36Sopenharmony_ci{ 12462306a36Sopenharmony_ci int err = -EEXIST; 12562306a36Sopenharmony_ci struct tcf_ematch_ops *e; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci if (ops->match == NULL) 12862306a36Sopenharmony_ci return -EINVAL; 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci write_lock(&ematch_mod_lock); 13162306a36Sopenharmony_ci list_for_each_entry(e, &ematch_ops, link) 13262306a36Sopenharmony_ci if (ops->kind == e->kind) 13362306a36Sopenharmony_ci goto errout; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci list_add_tail(&ops->link, &ematch_ops); 13662306a36Sopenharmony_ci err = 0; 13762306a36Sopenharmony_cierrout: 13862306a36Sopenharmony_ci write_unlock(&ematch_mod_lock); 13962306a36Sopenharmony_ci return err; 14062306a36Sopenharmony_ci} 14162306a36Sopenharmony_ciEXPORT_SYMBOL(tcf_em_register); 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci/** 14462306a36Sopenharmony_ci * tcf_em_unregister - unregister and extended match 14562306a36Sopenharmony_ci * 14662306a36Sopenharmony_ci * @ops: ematch operations lookup table 14762306a36Sopenharmony_ci * 14862306a36Sopenharmony_ci * This function must be called by ematches to announce their disappearance 14962306a36Sopenharmony_ci * for examples when the module gets unloaded. The @ops parameter must be 15062306a36Sopenharmony_ci * the same as the one used for registration. 15162306a36Sopenharmony_ci * 15262306a36Sopenharmony_ci * Returns -ENOENT if no matching ematch was found. 15362306a36Sopenharmony_ci */ 15462306a36Sopenharmony_civoid tcf_em_unregister(struct tcf_ematch_ops *ops) 15562306a36Sopenharmony_ci{ 15662306a36Sopenharmony_ci write_lock(&ematch_mod_lock); 15762306a36Sopenharmony_ci list_del(&ops->link); 15862306a36Sopenharmony_ci write_unlock(&ematch_mod_lock); 15962306a36Sopenharmony_ci} 16062306a36Sopenharmony_ciEXPORT_SYMBOL(tcf_em_unregister); 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_cistatic inline struct tcf_ematch *tcf_em_get_match(struct tcf_ematch_tree *tree, 16362306a36Sopenharmony_ci int index) 16462306a36Sopenharmony_ci{ 16562306a36Sopenharmony_ci return &tree->matches[index]; 16662306a36Sopenharmony_ci} 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_cistatic int tcf_em_validate(struct tcf_proto *tp, 17062306a36Sopenharmony_ci struct tcf_ematch_tree_hdr *tree_hdr, 17162306a36Sopenharmony_ci struct tcf_ematch *em, struct nlattr *nla, int idx) 17262306a36Sopenharmony_ci{ 17362306a36Sopenharmony_ci int err = -EINVAL; 17462306a36Sopenharmony_ci struct tcf_ematch_hdr *em_hdr = nla_data(nla); 17562306a36Sopenharmony_ci int data_len = nla_len(nla) - sizeof(*em_hdr); 17662306a36Sopenharmony_ci void *data = (void *) em_hdr + sizeof(*em_hdr); 17762306a36Sopenharmony_ci struct net *net = tp->chain->block->net; 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci if (!TCF_EM_REL_VALID(em_hdr->flags)) 18062306a36Sopenharmony_ci goto errout; 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci if (em_hdr->kind == TCF_EM_CONTAINER) { 18362306a36Sopenharmony_ci /* Special ematch called "container", carries an index 18462306a36Sopenharmony_ci * referencing an external ematch sequence. 18562306a36Sopenharmony_ci */ 18662306a36Sopenharmony_ci u32 ref; 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci if (data_len < sizeof(ref)) 18962306a36Sopenharmony_ci goto errout; 19062306a36Sopenharmony_ci ref = *(u32 *) data; 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci if (ref >= tree_hdr->nmatches) 19362306a36Sopenharmony_ci goto errout; 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci /* We do not allow backward jumps to avoid loops and jumps 19662306a36Sopenharmony_ci * to our own position are of course illegal. 19762306a36Sopenharmony_ci */ 19862306a36Sopenharmony_ci if (ref <= idx) 19962306a36Sopenharmony_ci goto errout; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci em->data = ref; 20362306a36Sopenharmony_ci } else { 20462306a36Sopenharmony_ci /* Note: This lookup will increase the module refcnt 20562306a36Sopenharmony_ci * of the ematch module referenced. In case of a failure, 20662306a36Sopenharmony_ci * a destroy function is called by the underlying layer 20762306a36Sopenharmony_ci * which automatically releases the reference again, therefore 20862306a36Sopenharmony_ci * the module MUST not be given back under any circumstances 20962306a36Sopenharmony_ci * here. Be aware, the destroy function assumes that the 21062306a36Sopenharmony_ci * module is held if the ops field is non zero. 21162306a36Sopenharmony_ci */ 21262306a36Sopenharmony_ci em->ops = tcf_em_lookup(em_hdr->kind); 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci if (em->ops == NULL) { 21562306a36Sopenharmony_ci err = -ENOENT; 21662306a36Sopenharmony_ci#ifdef CONFIG_MODULES 21762306a36Sopenharmony_ci __rtnl_unlock(); 21862306a36Sopenharmony_ci request_module("ematch-kind-%u", em_hdr->kind); 21962306a36Sopenharmony_ci rtnl_lock(); 22062306a36Sopenharmony_ci em->ops = tcf_em_lookup(em_hdr->kind); 22162306a36Sopenharmony_ci if (em->ops) { 22262306a36Sopenharmony_ci /* We dropped the RTNL mutex in order to 22362306a36Sopenharmony_ci * perform the module load. Tell the caller 22462306a36Sopenharmony_ci * to replay the request. 22562306a36Sopenharmony_ci */ 22662306a36Sopenharmony_ci module_put(em->ops->owner); 22762306a36Sopenharmony_ci em->ops = NULL; 22862306a36Sopenharmony_ci err = -EAGAIN; 22962306a36Sopenharmony_ci } 23062306a36Sopenharmony_ci#endif 23162306a36Sopenharmony_ci goto errout; 23262306a36Sopenharmony_ci } 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci /* ematch module provides expected length of data, so we 23562306a36Sopenharmony_ci * can do a basic sanity check. 23662306a36Sopenharmony_ci */ 23762306a36Sopenharmony_ci if (em->ops->datalen && data_len < em->ops->datalen) 23862306a36Sopenharmony_ci goto errout; 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci if (em->ops->change) { 24162306a36Sopenharmony_ci err = -EINVAL; 24262306a36Sopenharmony_ci if (em_hdr->flags & TCF_EM_SIMPLE) 24362306a36Sopenharmony_ci goto errout; 24462306a36Sopenharmony_ci err = em->ops->change(net, data, data_len, em); 24562306a36Sopenharmony_ci if (err < 0) 24662306a36Sopenharmony_ci goto errout; 24762306a36Sopenharmony_ci } else if (data_len > 0) { 24862306a36Sopenharmony_ci /* ematch module doesn't provide an own change 24962306a36Sopenharmony_ci * procedure and expects us to allocate and copy 25062306a36Sopenharmony_ci * the ematch data. 25162306a36Sopenharmony_ci * 25262306a36Sopenharmony_ci * TCF_EM_SIMPLE may be specified stating that the 25362306a36Sopenharmony_ci * data only consists of a u32 integer and the module 25462306a36Sopenharmony_ci * does not expected a memory reference but rather 25562306a36Sopenharmony_ci * the value carried. 25662306a36Sopenharmony_ci */ 25762306a36Sopenharmony_ci if (em_hdr->flags & TCF_EM_SIMPLE) { 25862306a36Sopenharmony_ci if (em->ops->datalen > 0) 25962306a36Sopenharmony_ci goto errout; 26062306a36Sopenharmony_ci if (data_len < sizeof(u32)) 26162306a36Sopenharmony_ci goto errout; 26262306a36Sopenharmony_ci em->data = *(u32 *) data; 26362306a36Sopenharmony_ci } else { 26462306a36Sopenharmony_ci void *v = kmemdup(data, data_len, GFP_KERNEL); 26562306a36Sopenharmony_ci if (v == NULL) { 26662306a36Sopenharmony_ci err = -ENOBUFS; 26762306a36Sopenharmony_ci goto errout; 26862306a36Sopenharmony_ci } 26962306a36Sopenharmony_ci em->data = (unsigned long) v; 27062306a36Sopenharmony_ci } 27162306a36Sopenharmony_ci em->datalen = data_len; 27262306a36Sopenharmony_ci } 27362306a36Sopenharmony_ci } 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci em->matchid = em_hdr->matchid; 27662306a36Sopenharmony_ci em->flags = em_hdr->flags; 27762306a36Sopenharmony_ci em->net = net; 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci err = 0; 28062306a36Sopenharmony_cierrout: 28162306a36Sopenharmony_ci return err; 28262306a36Sopenharmony_ci} 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_cistatic const struct nla_policy em_policy[TCA_EMATCH_TREE_MAX + 1] = { 28562306a36Sopenharmony_ci [TCA_EMATCH_TREE_HDR] = { .len = sizeof(struct tcf_ematch_tree_hdr) }, 28662306a36Sopenharmony_ci [TCA_EMATCH_TREE_LIST] = { .type = NLA_NESTED }, 28762306a36Sopenharmony_ci}; 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci/** 29062306a36Sopenharmony_ci * tcf_em_tree_validate - validate ematch config TLV and build ematch tree 29162306a36Sopenharmony_ci * 29262306a36Sopenharmony_ci * @tp: classifier kind handle 29362306a36Sopenharmony_ci * @nla: ematch tree configuration TLV 29462306a36Sopenharmony_ci * @tree: destination ematch tree variable to store the resulting 29562306a36Sopenharmony_ci * ematch tree. 29662306a36Sopenharmony_ci * 29762306a36Sopenharmony_ci * This function validates the given configuration TLV @nla and builds an 29862306a36Sopenharmony_ci * ematch tree in @tree. The resulting tree must later be copied into 29962306a36Sopenharmony_ci * the private classifier data using tcf_em_tree_change(). You MUST NOT 30062306a36Sopenharmony_ci * provide the ematch tree variable of the private classifier data directly, 30162306a36Sopenharmony_ci * the changes would not be locked properly. 30262306a36Sopenharmony_ci * 30362306a36Sopenharmony_ci * Returns a negative error code if the configuration TLV contains errors. 30462306a36Sopenharmony_ci */ 30562306a36Sopenharmony_ciint tcf_em_tree_validate(struct tcf_proto *tp, struct nlattr *nla, 30662306a36Sopenharmony_ci struct tcf_ematch_tree *tree) 30762306a36Sopenharmony_ci{ 30862306a36Sopenharmony_ci int idx, list_len, matches_len, err; 30962306a36Sopenharmony_ci struct nlattr *tb[TCA_EMATCH_TREE_MAX + 1]; 31062306a36Sopenharmony_ci struct nlattr *rt_match, *rt_hdr, *rt_list; 31162306a36Sopenharmony_ci struct tcf_ematch_tree_hdr *tree_hdr; 31262306a36Sopenharmony_ci struct tcf_ematch *em; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci memset(tree, 0, sizeof(*tree)); 31562306a36Sopenharmony_ci if (!nla) 31662306a36Sopenharmony_ci return 0; 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci err = nla_parse_nested_deprecated(tb, TCA_EMATCH_TREE_MAX, nla, 31962306a36Sopenharmony_ci em_policy, NULL); 32062306a36Sopenharmony_ci if (err < 0) 32162306a36Sopenharmony_ci goto errout; 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ci err = -EINVAL; 32462306a36Sopenharmony_ci rt_hdr = tb[TCA_EMATCH_TREE_HDR]; 32562306a36Sopenharmony_ci rt_list = tb[TCA_EMATCH_TREE_LIST]; 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ci if (rt_hdr == NULL || rt_list == NULL) 32862306a36Sopenharmony_ci goto errout; 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci tree_hdr = nla_data(rt_hdr); 33162306a36Sopenharmony_ci memcpy(&tree->hdr, tree_hdr, sizeof(*tree_hdr)); 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci rt_match = nla_data(rt_list); 33462306a36Sopenharmony_ci list_len = nla_len(rt_list); 33562306a36Sopenharmony_ci matches_len = tree_hdr->nmatches * sizeof(*em); 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci tree->matches = kzalloc(matches_len, GFP_KERNEL); 33862306a36Sopenharmony_ci if (tree->matches == NULL) 33962306a36Sopenharmony_ci goto errout; 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci /* We do not use nla_parse_nested here because the maximum 34262306a36Sopenharmony_ci * number of attributes is unknown. This saves us the allocation 34362306a36Sopenharmony_ci * for a tb buffer which would serve no purpose at all. 34462306a36Sopenharmony_ci * 34562306a36Sopenharmony_ci * The array of rt attributes is parsed in the order as they are 34662306a36Sopenharmony_ci * provided, their type must be incremental from 1 to n. Even 34762306a36Sopenharmony_ci * if it does not serve any real purpose, a failure of sticking 34862306a36Sopenharmony_ci * to this policy will result in parsing failure. 34962306a36Sopenharmony_ci */ 35062306a36Sopenharmony_ci for (idx = 0; nla_ok(rt_match, list_len); idx++) { 35162306a36Sopenharmony_ci err = -EINVAL; 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci if (rt_match->nla_type != (idx + 1)) 35462306a36Sopenharmony_ci goto errout_abort; 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci if (idx >= tree_hdr->nmatches) 35762306a36Sopenharmony_ci goto errout_abort; 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci if (nla_len(rt_match) < sizeof(struct tcf_ematch_hdr)) 36062306a36Sopenharmony_ci goto errout_abort; 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci em = tcf_em_get_match(tree, idx); 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci err = tcf_em_validate(tp, tree_hdr, em, rt_match, idx); 36562306a36Sopenharmony_ci if (err < 0) 36662306a36Sopenharmony_ci goto errout_abort; 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci rt_match = nla_next(rt_match, &list_len); 36962306a36Sopenharmony_ci } 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci /* Check if the number of matches provided by userspace actually 37262306a36Sopenharmony_ci * complies with the array of matches. The number was used for 37362306a36Sopenharmony_ci * the validation of references and a mismatch could lead to 37462306a36Sopenharmony_ci * undefined references during the matching process. 37562306a36Sopenharmony_ci */ 37662306a36Sopenharmony_ci if (idx != tree_hdr->nmatches) { 37762306a36Sopenharmony_ci err = -EINVAL; 37862306a36Sopenharmony_ci goto errout_abort; 37962306a36Sopenharmony_ci } 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci err = 0; 38262306a36Sopenharmony_cierrout: 38362306a36Sopenharmony_ci return err; 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_cierrout_abort: 38662306a36Sopenharmony_ci tcf_em_tree_destroy(tree); 38762306a36Sopenharmony_ci return err; 38862306a36Sopenharmony_ci} 38962306a36Sopenharmony_ciEXPORT_SYMBOL(tcf_em_tree_validate); 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ci/** 39262306a36Sopenharmony_ci * tcf_em_tree_destroy - destroy an ematch tree 39362306a36Sopenharmony_ci * 39462306a36Sopenharmony_ci * @tree: ematch tree to be deleted 39562306a36Sopenharmony_ci * 39662306a36Sopenharmony_ci * This functions destroys an ematch tree previously created by 39762306a36Sopenharmony_ci * tcf_em_tree_validate()/tcf_em_tree_change(). You must ensure that 39862306a36Sopenharmony_ci * the ematch tree is not in use before calling this function. 39962306a36Sopenharmony_ci */ 40062306a36Sopenharmony_civoid tcf_em_tree_destroy(struct tcf_ematch_tree *tree) 40162306a36Sopenharmony_ci{ 40262306a36Sopenharmony_ci int i; 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ci if (tree->matches == NULL) 40562306a36Sopenharmony_ci return; 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_ci for (i = 0; i < tree->hdr.nmatches; i++) { 40862306a36Sopenharmony_ci struct tcf_ematch *em = tcf_em_get_match(tree, i); 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ci if (em->ops) { 41162306a36Sopenharmony_ci if (em->ops->destroy) 41262306a36Sopenharmony_ci em->ops->destroy(em); 41362306a36Sopenharmony_ci else if (!tcf_em_is_simple(em)) 41462306a36Sopenharmony_ci kfree((void *) em->data); 41562306a36Sopenharmony_ci module_put(em->ops->owner); 41662306a36Sopenharmony_ci } 41762306a36Sopenharmony_ci } 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci tree->hdr.nmatches = 0; 42062306a36Sopenharmony_ci kfree(tree->matches); 42162306a36Sopenharmony_ci tree->matches = NULL; 42262306a36Sopenharmony_ci} 42362306a36Sopenharmony_ciEXPORT_SYMBOL(tcf_em_tree_destroy); 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ci/** 42662306a36Sopenharmony_ci * tcf_em_tree_dump - dump ematch tree into a rtnl message 42762306a36Sopenharmony_ci * 42862306a36Sopenharmony_ci * @skb: skb holding the rtnl message 42962306a36Sopenharmony_ci * @tree: ematch tree to be dumped 43062306a36Sopenharmony_ci * @tlv: TLV type to be used to encapsulate the tree 43162306a36Sopenharmony_ci * 43262306a36Sopenharmony_ci * This function dumps a ematch tree into a rtnl message. It is valid to 43362306a36Sopenharmony_ci * call this function while the ematch tree is in use. 43462306a36Sopenharmony_ci * 43562306a36Sopenharmony_ci * Returns -1 if the skb tailroom is insufficient. 43662306a36Sopenharmony_ci */ 43762306a36Sopenharmony_ciint tcf_em_tree_dump(struct sk_buff *skb, struct tcf_ematch_tree *tree, int tlv) 43862306a36Sopenharmony_ci{ 43962306a36Sopenharmony_ci int i; 44062306a36Sopenharmony_ci u8 *tail; 44162306a36Sopenharmony_ci struct nlattr *top_start; 44262306a36Sopenharmony_ci struct nlattr *list_start; 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci top_start = nla_nest_start_noflag(skb, tlv); 44562306a36Sopenharmony_ci if (top_start == NULL) 44662306a36Sopenharmony_ci goto nla_put_failure; 44762306a36Sopenharmony_ci 44862306a36Sopenharmony_ci if (nla_put(skb, TCA_EMATCH_TREE_HDR, sizeof(tree->hdr), &tree->hdr)) 44962306a36Sopenharmony_ci goto nla_put_failure; 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci list_start = nla_nest_start_noflag(skb, TCA_EMATCH_TREE_LIST); 45262306a36Sopenharmony_ci if (list_start == NULL) 45362306a36Sopenharmony_ci goto nla_put_failure; 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ci tail = skb_tail_pointer(skb); 45662306a36Sopenharmony_ci for (i = 0; i < tree->hdr.nmatches; i++) { 45762306a36Sopenharmony_ci struct nlattr *match_start = (struct nlattr *)tail; 45862306a36Sopenharmony_ci struct tcf_ematch *em = tcf_em_get_match(tree, i); 45962306a36Sopenharmony_ci struct tcf_ematch_hdr em_hdr = { 46062306a36Sopenharmony_ci .kind = em->ops ? em->ops->kind : TCF_EM_CONTAINER, 46162306a36Sopenharmony_ci .matchid = em->matchid, 46262306a36Sopenharmony_ci .flags = em->flags 46362306a36Sopenharmony_ci }; 46462306a36Sopenharmony_ci 46562306a36Sopenharmony_ci if (nla_put(skb, i + 1, sizeof(em_hdr), &em_hdr)) 46662306a36Sopenharmony_ci goto nla_put_failure; 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ci if (em->ops && em->ops->dump) { 46962306a36Sopenharmony_ci if (em->ops->dump(skb, em) < 0) 47062306a36Sopenharmony_ci goto nla_put_failure; 47162306a36Sopenharmony_ci } else if (tcf_em_is_container(em) || tcf_em_is_simple(em)) { 47262306a36Sopenharmony_ci u32 u = em->data; 47362306a36Sopenharmony_ci nla_put_nohdr(skb, sizeof(u), &u); 47462306a36Sopenharmony_ci } else if (em->datalen > 0) 47562306a36Sopenharmony_ci nla_put_nohdr(skb, em->datalen, (void *) em->data); 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_ci tail = skb_tail_pointer(skb); 47862306a36Sopenharmony_ci match_start->nla_len = tail - (u8 *)match_start; 47962306a36Sopenharmony_ci } 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_ci nla_nest_end(skb, list_start); 48262306a36Sopenharmony_ci nla_nest_end(skb, top_start); 48362306a36Sopenharmony_ci 48462306a36Sopenharmony_ci return 0; 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_cinla_put_failure: 48762306a36Sopenharmony_ci return -1; 48862306a36Sopenharmony_ci} 48962306a36Sopenharmony_ciEXPORT_SYMBOL(tcf_em_tree_dump); 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_cistatic inline int tcf_em_match(struct sk_buff *skb, struct tcf_ematch *em, 49262306a36Sopenharmony_ci struct tcf_pkt_info *info) 49362306a36Sopenharmony_ci{ 49462306a36Sopenharmony_ci int r = em->ops->match(skb, em, info); 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci return tcf_em_is_inverted(em) ? !r : r; 49762306a36Sopenharmony_ci} 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci/* Do not use this function directly, use tcf_em_tree_match instead */ 50062306a36Sopenharmony_ciint __tcf_em_tree_match(struct sk_buff *skb, struct tcf_ematch_tree *tree, 50162306a36Sopenharmony_ci struct tcf_pkt_info *info) 50262306a36Sopenharmony_ci{ 50362306a36Sopenharmony_ci int stackp = 0, match_idx = 0, res = 0; 50462306a36Sopenharmony_ci struct tcf_ematch *cur_match; 50562306a36Sopenharmony_ci int stack[CONFIG_NET_EMATCH_STACK]; 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ciproceed: 50862306a36Sopenharmony_ci while (match_idx < tree->hdr.nmatches) { 50962306a36Sopenharmony_ci cur_match = tcf_em_get_match(tree, match_idx); 51062306a36Sopenharmony_ci 51162306a36Sopenharmony_ci if (tcf_em_is_container(cur_match)) { 51262306a36Sopenharmony_ci if (unlikely(stackp >= CONFIG_NET_EMATCH_STACK)) 51362306a36Sopenharmony_ci goto stack_overflow; 51462306a36Sopenharmony_ci 51562306a36Sopenharmony_ci stack[stackp++] = match_idx; 51662306a36Sopenharmony_ci match_idx = cur_match->data; 51762306a36Sopenharmony_ci goto proceed; 51862306a36Sopenharmony_ci } 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci res = tcf_em_match(skb, cur_match, info); 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci if (tcf_em_early_end(cur_match, res)) 52362306a36Sopenharmony_ci break; 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_ci match_idx++; 52662306a36Sopenharmony_ci } 52762306a36Sopenharmony_ci 52862306a36Sopenharmony_cipop_stack: 52962306a36Sopenharmony_ci if (stackp > 0) { 53062306a36Sopenharmony_ci match_idx = stack[--stackp]; 53162306a36Sopenharmony_ci cur_match = tcf_em_get_match(tree, match_idx); 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci if (tcf_em_is_inverted(cur_match)) 53462306a36Sopenharmony_ci res = !res; 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ci if (tcf_em_early_end(cur_match, res)) { 53762306a36Sopenharmony_ci goto pop_stack; 53862306a36Sopenharmony_ci } else { 53962306a36Sopenharmony_ci match_idx++; 54062306a36Sopenharmony_ci goto proceed; 54162306a36Sopenharmony_ci } 54262306a36Sopenharmony_ci } 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci return res; 54562306a36Sopenharmony_ci 54662306a36Sopenharmony_cistack_overflow: 54762306a36Sopenharmony_ci net_warn_ratelimited("tc ematch: local stack overflow, increase NET_EMATCH_STACK\n"); 54862306a36Sopenharmony_ci return -1; 54962306a36Sopenharmony_ci} 55062306a36Sopenharmony_ciEXPORT_SYMBOL(__tcf_em_tree_match); 551