Blame view

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