1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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#include <linux/module.h>
85#include <linux/slab.h>
86#include <linux/types.h>
87#include <linux/kernel.h>
88#include <linux/errno.h>
89#include <linux/rtnetlink.h>
90#include <linux/skbuff.h>
91#include <net/pkt_cls.h>
92
93static LIST_HEAD(ematch_ops);
94static DEFINE_RWLOCK(ematch_mod_lock);
95
96static inline struct tcf_ematch_ops * tcf_em_lookup(u16 kind)
97{
98 struct tcf_ematch_ops *e = NULL;
99
100 read_lock(&ematch_mod_lock);
101 list_for_each_entry(e, &ematch_ops, link) {
102 if (kind == e->kind) {
103 if (!try_module_get(e->owner))
104 e = NULL;
105 read_unlock(&ematch_mod_lock);
106 return e;
107 }
108 }
109 read_unlock(&ematch_mod_lock);
110
111 return NULL;
112}
113
114
115
116
117
118
119
120
121
122
123
124
125
126int tcf_em_register(struct tcf_ematch_ops *ops)
127{
128 int err = -EEXIST;
129 struct tcf_ematch_ops *e;
130
131 if (ops->match == NULL)
132 return -EINVAL;
133
134 write_lock(&ematch_mod_lock);
135 list_for_each_entry(e, &ematch_ops, link)
136 if (ops->kind == e->kind)
137 goto errout;
138
139 list_add_tail(&ops->link, &ematch_ops);
140 err = 0;
141errout:
142 write_unlock(&ematch_mod_lock);
143 return err;
144}
145EXPORT_SYMBOL(tcf_em_register);
146
147
148
149
150
151
152
153
154
155
156
157
158void tcf_em_unregister(struct tcf_ematch_ops *ops)
159{
160 write_lock(&ematch_mod_lock);
161 list_del(&ops->link);
162 write_unlock(&ematch_mod_lock);
163}
164EXPORT_SYMBOL(tcf_em_unregister);
165
166static inline struct tcf_ematch * tcf_em_get_match(struct tcf_ematch_tree *tree,
167 int index)
168{
169 return &tree->matches[index];
170}
171
172
173static int tcf_em_validate(struct tcf_proto *tp,
174 struct tcf_ematch_tree_hdr *tree_hdr,
175 struct tcf_ematch *em, struct nlattr *nla, int idx)
176{
177 int err = -EINVAL;
178 struct tcf_ematch_hdr *em_hdr = nla_data(nla);
179 int data_len = nla_len(nla) - sizeof(*em_hdr);
180 void *data = (void *) em_hdr + sizeof(*em_hdr);
181
182 if (!TCF_EM_REL_VALID(em_hdr->flags))
183 goto errout;
184
185 if (em_hdr->kind == TCF_EM_CONTAINER) {
186
187
188 u32 ref;
189
190 if (data_len < sizeof(ref))
191 goto errout;
192 ref = *(u32 *) data;
193
194 if (ref >= tree_hdr->nmatches)
195 goto errout;
196
197
198
199 if (ref <= idx)
200 goto errout;
201
202
203 em->data = ref;
204 } else {
205
206
207
208
209
210
211
212 em->ops = tcf_em_lookup(em_hdr->kind);
213
214 if (em->ops == NULL) {
215 err = -ENOENT;
216#ifdef CONFIG_MODULES
217 __rtnl_unlock();
218 request_module("ematch-kind-%u", em_hdr->kind);
219 rtnl_lock();
220 em->ops = tcf_em_lookup(em_hdr->kind);
221 if (em->ops) {
222
223
224
225 module_put(em->ops->owner);
226 err = -EAGAIN;
227 }
228#endif
229 goto errout;
230 }
231
232
233
234 if (em->ops->datalen && data_len < em->ops->datalen)
235 goto errout;
236
237 if (em->ops->change) {
238 err = em->ops->change(tp, data, data_len, em);
239 if (err < 0)
240 goto errout;
241 } else if (data_len > 0) {
242
243
244
245
246
247
248
249
250 if (em_hdr->flags & TCF_EM_SIMPLE) {
251 if (data_len < sizeof(u32))
252 goto errout;
253 em->data = *(u32 *) data;
254 } else {
255 void *v = kmemdup(data, data_len, GFP_KERNEL);
256 if (v == NULL) {
257 err = -ENOBUFS;
258 goto errout;
259 }
260 em->data = (unsigned long) v;
261 }
262 }
263 }
264
265 em->matchid = em_hdr->matchid;
266 em->flags = em_hdr->flags;
267 em->datalen = data_len;
268
269 err = 0;
270errout:
271 return err;
272}
273
274static const struct nla_policy em_policy[TCA_EMATCH_TREE_MAX + 1] = {
275 [TCA_EMATCH_TREE_HDR] = { .len = sizeof(struct tcf_ematch_tree_hdr) },
276 [TCA_EMATCH_TREE_LIST] = { .type = NLA_NESTED },
277};
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295int tcf_em_tree_validate(struct tcf_proto *tp, struct nlattr *nla,
296 struct tcf_ematch_tree *tree)
297{
298 int idx, list_len, matches_len, err;
299 struct nlattr *tb[TCA_EMATCH_TREE_MAX + 1];
300 struct nlattr *rt_match, *rt_hdr, *rt_list;
301 struct tcf_ematch_tree_hdr *tree_hdr;
302 struct tcf_ematch *em;
303
304 memset(tree, 0, sizeof(*tree));
305 if (!nla)
306 return 0;
307
308 err = nla_parse_nested(tb, TCA_EMATCH_TREE_MAX, nla, em_policy);
309 if (err < 0)
310 goto errout;
311
312 err = -EINVAL;
313 rt_hdr = tb[TCA_EMATCH_TREE_HDR];
314 rt_list = tb[TCA_EMATCH_TREE_LIST];
315
316 if (rt_hdr == NULL || rt_list == NULL)
317 goto errout;
318
319 tree_hdr = nla_data(rt_hdr);
320 memcpy(&tree->hdr, tree_hdr, sizeof(*tree_hdr));
321
322 rt_match = nla_data(rt_list);
323 list_len = nla_len(rt_list);
324 matches_len = tree_hdr->nmatches * sizeof(*em);
325
326 tree->matches = kzalloc(matches_len, GFP_KERNEL);
327 if (tree->matches == NULL)
328 goto errout;
329
330
331
332
333
334
335
336
337
338 for (idx = 0; nla_ok(rt_match, list_len); idx++) {
339 err = -EINVAL;
340
341 if (rt_match->nla_type != (idx + 1))
342 goto errout_abort;
343
344 if (idx >= tree_hdr->nmatches)
345 goto errout_abort;
346
347 if (nla_len(rt_match) < sizeof(struct tcf_ematch_hdr))
348 goto errout_abort;
349
350 em = tcf_em_get_match(tree, idx);
351
352 err = tcf_em_validate(tp, tree_hdr, em, rt_match, idx);
353 if (err < 0)
354 goto errout_abort;
355
356 rt_match = nla_next(rt_match, &list_len);
357 }
358
359
360
361
362
363 if (idx != tree_hdr->nmatches) {
364 err = -EINVAL;
365 goto errout_abort;
366 }
367
368 err = 0;
369errout:
370 return err;
371
372errout_abort:
373 tcf_em_tree_destroy(tp, tree);
374 return err;
375}
376EXPORT_SYMBOL(tcf_em_tree_validate);
377
378
379
380
381
382
383
384
385
386
387
388void tcf_em_tree_destroy(struct tcf_proto *tp, struct tcf_ematch_tree *tree)
389{
390 int i;
391
392 if (tree->matches == NULL)
393 return;
394
395 for (i = 0; i < tree->hdr.nmatches; i++) {
396 struct tcf_ematch *em = tcf_em_get_match(tree, i);
397
398 if (em->ops) {
399 if (em->ops->destroy)
400 em->ops->destroy(tp, em);
401 else if (!tcf_em_is_simple(em))
402 kfree((void *) em->data);
403 module_put(em->ops->owner);
404 }
405 }
406
407 tree->hdr.nmatches = 0;
408 kfree(tree->matches);
409 tree->matches = NULL;
410}
411EXPORT_SYMBOL(tcf_em_tree_destroy);
412
413
414
415
416
417
418
419
420
421
422
423
424
425int tcf_em_tree_dump(struct sk_buff *skb, struct tcf_ematch_tree *tree, int tlv)
426{
427 int i;
428 u8 *tail;
429 struct nlattr *top_start;
430 struct nlattr *list_start;
431
432 top_start = nla_nest_start(skb, tlv);
433 if (top_start == NULL)
434 goto nla_put_failure;
435
436 NLA_PUT(skb, TCA_EMATCH_TREE_HDR, sizeof(tree->hdr), &tree->hdr);
437
438 list_start = nla_nest_start(skb, TCA_EMATCH_TREE_LIST);
439 if (list_start == NULL)
440 goto nla_put_failure;
441
442 tail = skb_tail_pointer(skb);
443 for (i = 0; i < tree->hdr.nmatches; i++) {
444 struct nlattr *match_start = (struct nlattr *)tail;
445 struct tcf_ematch *em = tcf_em_get_match(tree, i);
446 struct tcf_ematch_hdr em_hdr = {
447 .kind = em->ops ? em->ops->kind : TCF_EM_CONTAINER,
448 .matchid = em->matchid,
449 .flags = em->flags
450 };
451
452 NLA_PUT(skb, i+1, sizeof(em_hdr), &em_hdr);
453
454 if (em->ops && em->ops->dump) {
455 if (em->ops->dump(skb, em) < 0)
456 goto nla_put_failure;
457 } else if (tcf_em_is_container(em) || tcf_em_is_simple(em)) {
458 u32 u = em->data;
459 nla_put_nohdr(skb, sizeof(u), &u);
460 } else if (em->datalen > 0)
461 nla_put_nohdr(skb, em->datalen, (void *) em->data);
462
463 tail = skb_tail_pointer(skb);
464 match_start->nla_len = tail - (u8 *)match_start;
465 }
466
467 nla_nest_end(skb, list_start);
468 nla_nest_end(skb, top_start);
469
470 return 0;
471
472nla_put_failure:
473 return -1;
474}
475EXPORT_SYMBOL(tcf_em_tree_dump);
476
477static inline int tcf_em_match(struct sk_buff *skb, struct tcf_ematch *em,
478 struct tcf_pkt_info *info)
479{
480 int r = em->ops->match(skb, em, info);
481 return tcf_em_is_inverted(em) ? !r : r;
482}
483
484
485int __tcf_em_tree_match(struct sk_buff *skb, struct tcf_ematch_tree *tree,
486 struct tcf_pkt_info *info)
487{
488 int stackp = 0, match_idx = 0, res = 0;
489 struct tcf_ematch *cur_match;
490 int stack[CONFIG_NET_EMATCH_STACK];
491
492proceed:
493 while (match_idx < tree->hdr.nmatches) {
494 cur_match = tcf_em_get_match(tree, match_idx);
495
496 if (tcf_em_is_container(cur_match)) {
497 if (unlikely(stackp >= CONFIG_NET_EMATCH_STACK))
498 goto stack_overflow;
499
500 stack[stackp++] = match_idx;
501 match_idx = cur_match->data;
502 goto proceed;
503 }
504
505 res = tcf_em_match(skb, cur_match, info);
506
507 if (tcf_em_early_end(cur_match, res))
508 break;
509
510 match_idx++;
511 }
512
513pop_stack:
514 if (stackp > 0) {
515 match_idx = stack[--stackp];
516 cur_match = tcf_em_get_match(tree, match_idx);
517
518 if (tcf_em_early_end(cur_match, res))
519 goto pop_stack;
520 else {
521 match_idx++;
522 goto proceed;
523 }
524 }
525
526 return res;
527
528stack_overflow:
529 if (net_ratelimit())
530 printk(KERN_WARNING "tc ematch: local stack overflow,"
531 " increase NET_EMATCH_STACK\n");
532 return -1;
533}
534EXPORT_SYMBOL(__tcf_em_tree_match);
535