1d675c989SThomas Graf /* 2d675c989SThomas Graf * net/sched/em_text.c Textsearch ematch 3d675c989SThomas Graf * 4d675c989SThomas Graf * This program is free software; you can redistribute it and/or 5d675c989SThomas Graf * modify it under the terms of the GNU General Public License 6d675c989SThomas Graf * as published by the Free Software Foundation; either version 7d675c989SThomas Graf * 2 of the License, or (at your option) any later version. 8d675c989SThomas Graf * 9d675c989SThomas Graf * Authors: Thomas Graf <tgraf@suug.ch> 10d675c989SThomas Graf */ 11d675c989SThomas Graf 125a0e3ad6STejun Heo #include <linux/slab.h> 13d675c989SThomas Graf #include <linux/module.h> 14d675c989SThomas Graf #include <linux/types.h> 15d675c989SThomas Graf #include <linux/kernel.h> 16d675c989SThomas Graf #include <linux/string.h> 17d675c989SThomas Graf #include <linux/skbuff.h> 18d675c989SThomas Graf #include <linux/textsearch.h> 19d675c989SThomas Graf #include <linux/tc_ematch/tc_em_text.h> 20d675c989SThomas Graf #include <net/pkt_cls.h> 21d675c989SThomas Graf 22cc7ec456SEric Dumazet struct text_match { 23d675c989SThomas Graf u16 from_offset; 24d675c989SThomas Graf u16 to_offset; 25d675c989SThomas Graf u8 from_layer; 26d675c989SThomas Graf u8 to_layer; 27d675c989SThomas Graf struct ts_config *config; 28d675c989SThomas Graf }; 29d675c989SThomas Graf 30d675c989SThomas Graf #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data) 31d675c989SThomas Graf 32d675c989SThomas Graf static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m, 33d675c989SThomas Graf struct tcf_pkt_info *info) 34d675c989SThomas Graf { 35d675c989SThomas Graf struct text_match *tm = EM_TEXT_PRIV(m); 36d675c989SThomas Graf int from, to; 37d675c989SThomas Graf 38d675c989SThomas Graf from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data; 39d675c989SThomas Graf from += tm->from_offset; 40d675c989SThomas Graf 41d675c989SThomas Graf to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data; 42d675c989SThomas Graf to += tm->to_offset; 43d675c989SThomas Graf 44059a2440SBojan Prtvar return skb_find_text(skb, from, to, tm->config) != UINT_MAX; 45d675c989SThomas Graf } 46d675c989SThomas Graf 4782a470f1SJohn Fastabend static int em_text_change(struct net *net, void *data, int len, 48d675c989SThomas Graf struct tcf_ematch *m) 49d675c989SThomas Graf { 50d675c989SThomas Graf struct text_match *tm; 51d675c989SThomas Graf struct tcf_em_text *conf = data; 52d675c989SThomas Graf struct ts_config *ts_conf; 53d675c989SThomas Graf int flags = 0; 54d675c989SThomas Graf 55d675c989SThomas Graf if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len)) 56d675c989SThomas Graf return -EINVAL; 57d675c989SThomas Graf 58d675c989SThomas Graf if (conf->from_layer > conf->to_layer) 59d675c989SThomas Graf return -EINVAL; 60d675c989SThomas Graf 61d675c989SThomas Graf if (conf->from_layer == conf->to_layer && 62d675c989SThomas Graf conf->from_offset > conf->to_offset) 63d675c989SThomas Graf return -EINVAL; 64d675c989SThomas Graf 65d675c989SThomas Graf retry: 66d675c989SThomas Graf ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf), 67d675c989SThomas Graf conf->pattern_len, GFP_KERNEL, flags); 68d675c989SThomas Graf 69d675c989SThomas Graf if (flags & TS_AUTOLOAD) 70d675c989SThomas Graf rtnl_lock(); 71d675c989SThomas Graf 72d675c989SThomas Graf if (IS_ERR(ts_conf)) { 73d675c989SThomas Graf if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) { 74d675c989SThomas Graf rtnl_unlock(); 75d675c989SThomas Graf flags |= TS_AUTOLOAD; 76d675c989SThomas Graf goto retry; 77d675c989SThomas Graf } else 78d675c989SThomas Graf return PTR_ERR(ts_conf); 79d675c989SThomas Graf } else if (flags & TS_AUTOLOAD) { 80d675c989SThomas Graf textsearch_destroy(ts_conf); 81d675c989SThomas Graf return -EAGAIN; 82d675c989SThomas Graf } 83d675c989SThomas Graf 84d675c989SThomas Graf tm = kmalloc(sizeof(*tm), GFP_KERNEL); 85d675c989SThomas Graf if (tm == NULL) { 86d675c989SThomas Graf textsearch_destroy(ts_conf); 87d675c989SThomas Graf return -ENOBUFS; 88d675c989SThomas Graf } 89d675c989SThomas Graf 90d675c989SThomas Graf tm->from_offset = conf->from_offset; 91d675c989SThomas Graf tm->to_offset = conf->to_offset; 92d675c989SThomas Graf tm->from_layer = conf->from_layer; 93d675c989SThomas Graf tm->to_layer = conf->to_layer; 94d675c989SThomas Graf tm->config = ts_conf; 95d675c989SThomas Graf 96d675c989SThomas Graf m->datalen = sizeof(*tm); 97d675c989SThomas Graf m->data = (unsigned long) tm; 98d675c989SThomas Graf 99d675c989SThomas Graf return 0; 100d675c989SThomas Graf } 101d675c989SThomas Graf 10282a470f1SJohn Fastabend static void em_text_destroy(struct tcf_ematch *m) 103d675c989SThomas Graf { 1045ec1cea0SThomas Graf if (EM_TEXT_PRIV(m) && EM_TEXT_PRIV(m)->config) 105d675c989SThomas Graf textsearch_destroy(EM_TEXT_PRIV(m)->config); 106d675c989SThomas Graf } 107d675c989SThomas Graf 108d675c989SThomas Graf static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m) 109d675c989SThomas Graf { 110d675c989SThomas Graf struct text_match *tm = EM_TEXT_PRIV(m); 111d675c989SThomas Graf struct tcf_em_text conf; 112d675c989SThomas Graf 113d675c989SThomas Graf strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1); 114d675c989SThomas Graf conf.from_offset = tm->from_offset; 115d675c989SThomas Graf conf.to_offset = tm->to_offset; 116d675c989SThomas Graf conf.from_layer = tm->from_layer; 117d675c989SThomas Graf conf.to_layer = tm->to_layer; 118d675c989SThomas Graf conf.pattern_len = textsearch_get_pattern_len(tm->config); 119d675c989SThomas Graf conf.pad = 0; 120d675c989SThomas Graf 121add93b61SPatrick McHardy if (nla_put_nohdr(skb, sizeof(conf), &conf) < 0) 122add93b61SPatrick McHardy goto nla_put_failure; 123add93b61SPatrick McHardy if (nla_append(skb, conf.pattern_len, 124add93b61SPatrick McHardy textsearch_get_pattern(tm->config)) < 0) 125add93b61SPatrick McHardy goto nla_put_failure; 126d675c989SThomas Graf return 0; 127d675c989SThomas Graf 128add93b61SPatrick McHardy nla_put_failure: 129d675c989SThomas Graf return -1; 130d675c989SThomas Graf } 131d675c989SThomas Graf 132d675c989SThomas Graf static struct tcf_ematch_ops em_text_ops = { 133d675c989SThomas Graf .kind = TCF_EM_TEXT, 134d675c989SThomas Graf .change = em_text_change, 135d675c989SThomas Graf .match = em_text_match, 136d675c989SThomas Graf .destroy = em_text_destroy, 137d675c989SThomas Graf .dump = em_text_dump, 138d675c989SThomas Graf .owner = THIS_MODULE, 139d675c989SThomas Graf .link = LIST_HEAD_INIT(em_text_ops.link) 140d675c989SThomas Graf }; 141d675c989SThomas Graf 142d675c989SThomas Graf static int __init init_em_text(void) 143d675c989SThomas Graf { 144d675c989SThomas Graf return tcf_em_register(&em_text_ops); 145d675c989SThomas Graf } 146d675c989SThomas Graf 147d675c989SThomas Graf static void __exit exit_em_text(void) 148d675c989SThomas Graf { 149d675c989SThomas Graf tcf_em_unregister(&em_text_ops); 150d675c989SThomas Graf } 151d675c989SThomas Graf 152d675c989SThomas Graf MODULE_LICENSE("GPL"); 153d675c989SThomas Graf 154d675c989SThomas Graf module_init(init_em_text); 155d675c989SThomas Graf module_exit(exit_em_text); 156db3d99c0SPatrick McHardy 157db3d99c0SPatrick McHardy MODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT); 158