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