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