Linux Audio

Check our new training course

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