]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier. | |
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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
11 | * The filters are packed to hash tables of key nodes | |
12 | * with a set of 32bit key/mask pairs at every node. | |
13 | * Nodes reference next level hash tables etc. | |
14 | * | |
15 | * This scheme is the best universal classifier I managed to | |
16 | * invent; it is not super-fast, but it is not slow (provided you | |
17 | * program it correctly), and general enough. And its relative | |
18 | * speed grows as the number of rules becomes larger. | |
19 | * | |
20 | * It seems that it represents the best middle point between | |
21 | * speed and manageability both by human and by machine. | |
22 | * | |
23 | * It is especially useful for link sharing combined with QoS; | |
24 | * pure RSVP doesn't need such a general approach and can use | |
25 | * much simpler (and faster) schemes, sort of cls_rsvp.c. | |
26 | * | |
27 | * JHS: We should remove the CONFIG_NET_CLS_IND from here | |
28 | * eventually when the meta match extension is made available | |
29 | * | |
30 | * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro> | |
31 | */ | |
32 | ||
1da177e4 | 33 | #include <linux/module.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
1da177e4 LT |
35 | #include <linux/types.h> |
36 | #include <linux/kernel.h> | |
1da177e4 | 37 | #include <linux/string.h> |
1da177e4 | 38 | #include <linux/errno.h> |
1da177e4 | 39 | #include <linux/rtnetlink.h> |
1da177e4 | 40 | #include <linux/skbuff.h> |
7801db8a | 41 | #include <linux/bitmap.h> |
0ba48053 | 42 | #include <net/netlink.h> |
1da177e4 LT |
43 | #include <net/act_api.h> |
44 | #include <net/pkt_cls.h> | |
45 | ||
cc7ec456 | 46 | struct tc_u_knode { |
1da177e4 LT |
47 | struct tc_u_knode *next; |
48 | u32 handle; | |
49 | struct tc_u_hnode *ht_up; | |
50 | struct tcf_exts exts; | |
51 | #ifdef CONFIG_NET_CLS_IND | |
2519a602 | 52 | int ifindex; |
1da177e4 LT |
53 | #endif |
54 | u8 fshift; | |
55 | struct tcf_result res; | |
56 | struct tc_u_hnode *ht_down; | |
57 | #ifdef CONFIG_CLS_U32_PERF | |
58 | struct tc_u32_pcnt *pf; | |
59 | #endif | |
60 | #ifdef CONFIG_CLS_U32_MARK | |
61 | struct tc_u32_mark mark; | |
62 | #endif | |
63 | struct tc_u32_sel sel; | |
64 | }; | |
65 | ||
cc7ec456 | 66 | struct tc_u_hnode { |
1da177e4 LT |
67 | struct tc_u_hnode *next; |
68 | u32 handle; | |
69 | u32 prio; | |
70 | struct tc_u_common *tp_c; | |
71 | int refcnt; | |
cc7ec456 | 72 | unsigned int divisor; |
1da177e4 LT |
73 | struct tc_u_knode *ht[1]; |
74 | }; | |
75 | ||
cc7ec456 | 76 | struct tc_u_common { |
1da177e4 LT |
77 | struct tc_u_hnode *hlist; |
78 | struct Qdisc *q; | |
79 | int refcnt; | |
80 | u32 hgenerator; | |
81 | }; | |
82 | ||
cc7ec456 ED |
83 | static inline unsigned int u32_hash_fold(__be32 key, |
84 | const struct tc_u32_sel *sel, | |
85 | u8 fshift) | |
1da177e4 | 86 | { |
cc7ec456 | 87 | unsigned int h = ntohl(key & sel->hmask) >> fshift; |
1da177e4 LT |
88 | |
89 | return h; | |
90 | } | |
91 | ||
dc7f9f6e | 92 | static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp, struct tcf_result *res) |
1da177e4 LT |
93 | { |
94 | struct { | |
95 | struct tc_u_knode *knode; | |
fbc2e7d9 | 96 | unsigned int off; |
1da177e4 LT |
97 | } stack[TC_U32_MAXDEPTH]; |
98 | ||
a8701a6c | 99 | struct tc_u_hnode *ht = tp->root; |
fbc2e7d9 | 100 | unsigned int off = skb_network_offset(skb); |
1da177e4 LT |
101 | struct tc_u_knode *n; |
102 | int sdepth = 0; | |
103 | int off2 = 0; | |
104 | int sel = 0; | |
105 | #ifdef CONFIG_CLS_U32_PERF | |
106 | int j; | |
107 | #endif | |
108 | int i, r; | |
109 | ||
110 | next_ht: | |
111 | n = ht->ht[sel]; | |
112 | ||
113 | next_knode: | |
114 | if (n) { | |
115 | struct tc_u32_key *key = n->sel.keys; | |
116 | ||
117 | #ifdef CONFIG_CLS_U32_PERF | |
cc7ec456 | 118 | n->pf->rcnt += 1; |
1da177e4 LT |
119 | j = 0; |
120 | #endif | |
121 | ||
122 | #ifdef CONFIG_CLS_U32_MARK | |
82e91ffe | 123 | if ((skb->mark & n->mark.mask) != n->mark.val) { |
1da177e4 LT |
124 | n = n->next; |
125 | goto next_knode; | |
126 | } else { | |
127 | n->mark.success++; | |
128 | } | |
129 | #endif | |
130 | ||
cc7ec456 | 131 | for (i = n->sel.nkeys; i > 0; i--, key++) { |
66d50d25 | 132 | int toff = off + key->off + (off2 & key->offmask); |
86fce3ba | 133 | __be32 *data, hdata; |
fbc2e7d9 | 134 | |
4e18b3ed | 135 | if (skb_headroom(skb) + toff > INT_MAX) |
66d50d25 | 136 | goto out; |
137 | ||
86fce3ba | 138 | data = skb_header_pointer(skb, toff, 4, &hdata); |
fbc2e7d9 CG |
139 | if (!data) |
140 | goto out; | |
141 | if ((*data ^ key->val) & key->mask) { | |
1da177e4 LT |
142 | n = n->next; |
143 | goto next_knode; | |
144 | } | |
145 | #ifdef CONFIG_CLS_U32_PERF | |
cc7ec456 | 146 | n->pf->kcnts[j] += 1; |
1da177e4 LT |
147 | j++; |
148 | #endif | |
149 | } | |
150 | if (n->ht_down == NULL) { | |
151 | check_terminal: | |
cc7ec456 | 152 | if (n->sel.flags & TC_U32_TERMINAL) { |
1da177e4 LT |
153 | |
154 | *res = n->res; | |
155 | #ifdef CONFIG_NET_CLS_IND | |
2519a602 | 156 | if (!tcf_match_indev(skb, n->ifindex)) { |
1da177e4 LT |
157 | n = n->next; |
158 | goto next_knode; | |
159 | } | |
160 | #endif | |
161 | #ifdef CONFIG_CLS_U32_PERF | |
cc7ec456 | 162 | n->pf->rhit += 1; |
1da177e4 LT |
163 | #endif |
164 | r = tcf_exts_exec(skb, &n->exts, res); | |
165 | if (r < 0) { | |
166 | n = n->next; | |
167 | goto next_knode; | |
168 | } | |
169 | ||
170 | return r; | |
171 | } | |
172 | n = n->next; | |
173 | goto next_knode; | |
174 | } | |
175 | ||
176 | /* PUSH */ | |
177 | if (sdepth >= TC_U32_MAXDEPTH) | |
178 | goto deadloop; | |
179 | stack[sdepth].knode = n; | |
fbc2e7d9 | 180 | stack[sdepth].off = off; |
1da177e4 LT |
181 | sdepth++; |
182 | ||
183 | ht = n->ht_down; | |
184 | sel = 0; | |
fbc2e7d9 | 185 | if (ht->divisor) { |
86fce3ba | 186 | __be32 *data, hdata; |
fbc2e7d9 CG |
187 | |
188 | data = skb_header_pointer(skb, off + n->sel.hoff, 4, | |
86fce3ba | 189 | &hdata); |
fbc2e7d9 CG |
190 | if (!data) |
191 | goto out; | |
192 | sel = ht->divisor & u32_hash_fold(*data, &n->sel, | |
193 | n->fshift); | |
194 | } | |
cc7ec456 | 195 | if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT))) |
1da177e4 LT |
196 | goto next_ht; |
197 | ||
cc7ec456 | 198 | if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) { |
1da177e4 | 199 | off2 = n->sel.off + 3; |
fbc2e7d9 | 200 | if (n->sel.flags & TC_U32_VAROFFSET) { |
86fce3ba | 201 | __be16 *data, hdata; |
fbc2e7d9 CG |
202 | |
203 | data = skb_header_pointer(skb, | |
204 | off + n->sel.offoff, | |
86fce3ba | 205 | 2, &hdata); |
fbc2e7d9 CG |
206 | if (!data) |
207 | goto out; | |
208 | off2 += ntohs(n->sel.offmask & *data) >> | |
209 | n->sel.offshift; | |
210 | } | |
1da177e4 LT |
211 | off2 &= ~3; |
212 | } | |
cc7ec456 | 213 | if (n->sel.flags & TC_U32_EAT) { |
fbc2e7d9 | 214 | off += off2; |
1da177e4 LT |
215 | off2 = 0; |
216 | } | |
217 | ||
fbc2e7d9 | 218 | if (off < skb->len) |
1da177e4 LT |
219 | goto next_ht; |
220 | } | |
221 | ||
222 | /* POP */ | |
223 | if (sdepth--) { | |
224 | n = stack[sdepth].knode; | |
225 | ht = n->ht_up; | |
fbc2e7d9 | 226 | off = stack[sdepth].off; |
1da177e4 LT |
227 | goto check_terminal; |
228 | } | |
fbc2e7d9 | 229 | out: |
1da177e4 LT |
230 | return -1; |
231 | ||
232 | deadloop: | |
e87cc472 | 233 | net_warn_ratelimited("cls_u32: dead loop\n"); |
1da177e4 LT |
234 | return -1; |
235 | } | |
236 | ||
cc7ec456 | 237 | static struct tc_u_hnode * |
1da177e4 LT |
238 | u32_lookup_ht(struct tc_u_common *tp_c, u32 handle) |
239 | { | |
240 | struct tc_u_hnode *ht; | |
241 | ||
242 | for (ht = tp_c->hlist; ht; ht = ht->next) | |
243 | if (ht->handle == handle) | |
244 | break; | |
245 | ||
246 | return ht; | |
247 | } | |
248 | ||
cc7ec456 | 249 | static struct tc_u_knode * |
1da177e4 LT |
250 | u32_lookup_key(struct tc_u_hnode *ht, u32 handle) |
251 | { | |
cc7ec456 | 252 | unsigned int sel; |
1da177e4 LT |
253 | struct tc_u_knode *n = NULL; |
254 | ||
255 | sel = TC_U32_HASH(handle); | |
256 | if (sel > ht->divisor) | |
257 | goto out; | |
258 | ||
259 | for (n = ht->ht[sel]; n; n = n->next) | |
260 | if (n->handle == handle) | |
261 | break; | |
262 | out: | |
263 | return n; | |
264 | } | |
265 | ||
266 | ||
267 | static unsigned long u32_get(struct tcf_proto *tp, u32 handle) | |
268 | { | |
269 | struct tc_u_hnode *ht; | |
270 | struct tc_u_common *tp_c = tp->data; | |
271 | ||
272 | if (TC_U32_HTID(handle) == TC_U32_ROOT) | |
273 | ht = tp->root; | |
274 | else | |
275 | ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle)); | |
276 | ||
277 | if (!ht) | |
278 | return 0; | |
279 | ||
280 | if (TC_U32_KEY(handle) == 0) | |
281 | return (unsigned long)ht; | |
282 | ||
283 | return (unsigned long)u32_lookup_key(ht, handle); | |
284 | } | |
285 | ||
286 | static void u32_put(struct tcf_proto *tp, unsigned long f) | |
287 | { | |
288 | } | |
289 | ||
290 | static u32 gen_new_htid(struct tc_u_common *tp_c) | |
291 | { | |
292 | int i = 0x800; | |
293 | ||
294 | do { | |
295 | if (++tp_c->hgenerator == 0x7FF) | |
296 | tp_c->hgenerator = 1; | |
cc7ec456 | 297 | } while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20)); |
1da177e4 LT |
298 | |
299 | return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0; | |
300 | } | |
301 | ||
302 | static int u32_init(struct tcf_proto *tp) | |
303 | { | |
304 | struct tc_u_hnode *root_ht; | |
305 | struct tc_u_common *tp_c; | |
306 | ||
72b25a91 | 307 | tp_c = tp->q->u32_node; |
1da177e4 | 308 | |
0da974f4 | 309 | root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL); |
1da177e4 LT |
310 | if (root_ht == NULL) |
311 | return -ENOBUFS; | |
312 | ||
1da177e4 LT |
313 | root_ht->divisor = 0; |
314 | root_ht->refcnt++; | |
315 | root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000; | |
316 | root_ht->prio = tp->prio; | |
317 | ||
318 | if (tp_c == NULL) { | |
0da974f4 | 319 | tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL); |
1da177e4 LT |
320 | if (tp_c == NULL) { |
321 | kfree(root_ht); | |
322 | return -ENOBUFS; | |
323 | } | |
1da177e4 | 324 | tp_c->q = tp->q; |
72b25a91 | 325 | tp->q->u32_node = tp_c; |
1da177e4 LT |
326 | } |
327 | ||
328 | tp_c->refcnt++; | |
329 | root_ht->next = tp_c->hlist; | |
330 | tp_c->hlist = root_ht; | |
331 | root_ht->tp_c = tp_c; | |
332 | ||
333 | tp->root = root_ht; | |
334 | tp->data = tp_c; | |
335 | return 0; | |
336 | } | |
337 | ||
338 | static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n) | |
339 | { | |
340 | tcf_unbind_filter(tp, &n->res); | |
341 | tcf_exts_destroy(tp, &n->exts); | |
342 | if (n->ht_down) | |
343 | n->ht_down->refcnt--; | |
344 | #ifdef CONFIG_CLS_U32_PERF | |
1ae39a43 | 345 | kfree(n->pf); |
1da177e4 LT |
346 | #endif |
347 | kfree(n); | |
348 | return 0; | |
349 | } | |
350 | ||
82d567c2 | 351 | static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key) |
1da177e4 LT |
352 | { |
353 | struct tc_u_knode **kp; | |
354 | struct tc_u_hnode *ht = key->ht_up; | |
355 | ||
356 | if (ht) { | |
357 | for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) { | |
358 | if (*kp == key) { | |
359 | tcf_tree_lock(tp); | |
360 | *kp = key->next; | |
361 | tcf_tree_unlock(tp); | |
362 | ||
363 | u32_destroy_key(tp, key); | |
364 | return 0; | |
365 | } | |
366 | } | |
367 | } | |
547b792c | 368 | WARN_ON(1); |
1da177e4 LT |
369 | return 0; |
370 | } | |
371 | ||
372 | static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) | |
373 | { | |
374 | struct tc_u_knode *n; | |
cc7ec456 | 375 | unsigned int h; |
1da177e4 | 376 | |
cc7ec456 | 377 | for (h = 0; h <= ht->divisor; h++) { |
1da177e4 LT |
378 | while ((n = ht->ht[h]) != NULL) { |
379 | ht->ht[h] = n->next; | |
380 | ||
381 | u32_destroy_key(tp, n); | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) | |
387 | { | |
388 | struct tc_u_common *tp_c = tp->data; | |
389 | struct tc_u_hnode **hn; | |
390 | ||
547b792c | 391 | WARN_ON(ht->refcnt); |
1da177e4 LT |
392 | |
393 | u32_clear_hnode(tp, ht); | |
394 | ||
395 | for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) { | |
396 | if (*hn == ht) { | |
397 | *hn = ht->next; | |
398 | kfree(ht); | |
399 | return 0; | |
400 | } | |
401 | } | |
402 | ||
547b792c | 403 | WARN_ON(1); |
1da177e4 LT |
404 | return -ENOENT; |
405 | } | |
406 | ||
407 | static void u32_destroy(struct tcf_proto *tp) | |
408 | { | |
409 | struct tc_u_common *tp_c = tp->data; | |
47a1a1d4 | 410 | struct tc_u_hnode *root_ht = tp->root; |
1da177e4 | 411 | |
547b792c | 412 | WARN_ON(root_ht == NULL); |
1da177e4 LT |
413 | |
414 | if (root_ht && --root_ht->refcnt == 0) | |
415 | u32_destroy_hnode(tp, root_ht); | |
416 | ||
417 | if (--tp_c->refcnt == 0) { | |
418 | struct tc_u_hnode *ht; | |
1da177e4 | 419 | |
72b25a91 | 420 | tp->q->u32_node = NULL; |
1da177e4 | 421 | |
e56cfad1 JP |
422 | for (ht = tp_c->hlist; ht; ht = ht->next) { |
423 | ht->refcnt--; | |
1da177e4 | 424 | u32_clear_hnode(tp, ht); |
e56cfad1 | 425 | } |
1da177e4 LT |
426 | |
427 | while ((ht = tp_c->hlist) != NULL) { | |
428 | tp_c->hlist = ht->next; | |
429 | ||
547b792c | 430 | WARN_ON(ht->refcnt != 0); |
1da177e4 LT |
431 | |
432 | kfree(ht); | |
3ff50b79 | 433 | } |
1da177e4 LT |
434 | |
435 | kfree(tp_c); | |
436 | } | |
437 | ||
438 | tp->data = NULL; | |
439 | } | |
440 | ||
441 | static int u32_delete(struct tcf_proto *tp, unsigned long arg) | |
442 | { | |
cc7ec456 | 443 | struct tc_u_hnode *ht = (struct tc_u_hnode *)arg; |
1da177e4 LT |
444 | |
445 | if (ht == NULL) | |
446 | return 0; | |
447 | ||
448 | if (TC_U32_KEY(ht->handle)) | |
cc7ec456 | 449 | return u32_delete_key(tp, (struct tc_u_knode *)ht); |
1da177e4 LT |
450 | |
451 | if (tp->root == ht) | |
452 | return -EINVAL; | |
453 | ||
e56cfad1 JP |
454 | if (ht->refcnt == 1) { |
455 | ht->refcnt--; | |
1da177e4 | 456 | u32_destroy_hnode(tp, ht); |
e56cfad1 JP |
457 | } else { |
458 | return -EBUSY; | |
459 | } | |
1da177e4 LT |
460 | |
461 | return 0; | |
462 | } | |
463 | ||
7801db8a | 464 | #define NR_U32_NODE (1<<12) |
1da177e4 LT |
465 | static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle) |
466 | { | |
467 | struct tc_u_knode *n; | |
7801db8a CW |
468 | unsigned long i; |
469 | unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long), | |
470 | GFP_KERNEL); | |
471 | if (!bitmap) | |
472 | return handle | 0xFFF; | |
1da177e4 | 473 | |
cc7ec456 | 474 | for (n = ht->ht[TC_U32_HASH(handle)]; n; n = n->next) |
7801db8a | 475 | set_bit(TC_U32_NODE(n->handle), bitmap); |
1da177e4 | 476 | |
7801db8a CW |
477 | i = find_next_zero_bit(bitmap, NR_U32_NODE, 0x800); |
478 | if (i >= NR_U32_NODE) | |
479 | i = find_next_zero_bit(bitmap, NR_U32_NODE, 1); | |
480 | ||
481 | kfree(bitmap); | |
482 | return handle | (i >= NR_U32_NODE ? 0xFFF : i); | |
1da177e4 LT |
483 | } |
484 | ||
6fa8c014 PM |
485 | static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = { |
486 | [TCA_U32_CLASSID] = { .type = NLA_U32 }, | |
487 | [TCA_U32_HASH] = { .type = NLA_U32 }, | |
488 | [TCA_U32_LINK] = { .type = NLA_U32 }, | |
489 | [TCA_U32_DIVISOR] = { .type = NLA_U32 }, | |
490 | [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) }, | |
491 | [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, | |
492 | [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) }, | |
493 | }; | |
494 | ||
c1b52739 BL |
495 | static int u32_set_parms(struct net *net, struct tcf_proto *tp, |
496 | unsigned long base, struct tc_u_hnode *ht, | |
add93b61 | 497 | struct tc_u_knode *n, struct nlattr **tb, |
2f7ef2f8 | 498 | struct nlattr *est, bool ovr) |
1da177e4 LT |
499 | { |
500 | int err; | |
501 | struct tcf_exts e; | |
502 | ||
5da57f42 | 503 | tcf_exts_init(&e, TCA_U32_ACT, TCA_U32_POLICE); |
2f7ef2f8 | 504 | err = tcf_exts_validate(net, tp, tb, est, &e, ovr); |
1da177e4 LT |
505 | if (err < 0) |
506 | return err; | |
507 | ||
508 | err = -EINVAL; | |
add93b61 | 509 | if (tb[TCA_U32_LINK]) { |
1587bac4 | 510 | u32 handle = nla_get_u32(tb[TCA_U32_LINK]); |
47a1a1d4 | 511 | struct tc_u_hnode *ht_down = NULL, *ht_old; |
1da177e4 LT |
512 | |
513 | if (TC_U32_KEY(handle)) | |
514 | goto errout; | |
515 | ||
516 | if (handle) { | |
517 | ht_down = u32_lookup_ht(ht->tp_c, handle); | |
518 | ||
519 | if (ht_down == NULL) | |
520 | goto errout; | |
521 | ht_down->refcnt++; | |
522 | } | |
523 | ||
524 | tcf_tree_lock(tp); | |
47a1a1d4 PM |
525 | ht_old = n->ht_down; |
526 | n->ht_down = ht_down; | |
1da177e4 LT |
527 | tcf_tree_unlock(tp); |
528 | ||
47a1a1d4 PM |
529 | if (ht_old) |
530 | ht_old->refcnt--; | |
1da177e4 | 531 | } |
add93b61 | 532 | if (tb[TCA_U32_CLASSID]) { |
1587bac4 | 533 | n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]); |
1da177e4 LT |
534 | tcf_bind_filter(tp, &n->res, base); |
535 | } | |
536 | ||
537 | #ifdef CONFIG_NET_CLS_IND | |
add93b61 | 538 | if (tb[TCA_U32_INDEV]) { |
2519a602 WC |
539 | int ret; |
540 | ret = tcf_change_indev(net, tb[TCA_U32_INDEV]); | |
541 | if (ret < 0) | |
1da177e4 | 542 | goto errout; |
2519a602 | 543 | n->ifindex = ret; |
1da177e4 LT |
544 | } |
545 | #endif | |
546 | tcf_exts_change(tp, &n->exts, &e); | |
547 | ||
548 | return 0; | |
549 | errout: | |
550 | tcf_exts_destroy(tp, &e); | |
551 | return err; | |
552 | } | |
553 | ||
c1b52739 | 554 | static int u32_change(struct net *net, struct sk_buff *in_skb, |
af4c6641 | 555 | struct tcf_proto *tp, unsigned long base, u32 handle, |
add93b61 | 556 | struct nlattr **tca, |
2f7ef2f8 | 557 | unsigned long *arg, bool ovr) |
1da177e4 LT |
558 | { |
559 | struct tc_u_common *tp_c = tp->data; | |
560 | struct tc_u_hnode *ht; | |
561 | struct tc_u_knode *n; | |
562 | struct tc_u32_sel *s; | |
add93b61 PM |
563 | struct nlattr *opt = tca[TCA_OPTIONS]; |
564 | struct nlattr *tb[TCA_U32_MAX + 1]; | |
1da177e4 LT |
565 | u32 htid; |
566 | int err; | |
567 | ||
568 | if (opt == NULL) | |
569 | return handle ? -EINVAL : 0; | |
570 | ||
6fa8c014 | 571 | err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy); |
cee63723 PM |
572 | if (err < 0) |
573 | return err; | |
1da177e4 | 574 | |
cc7ec456 ED |
575 | n = (struct tc_u_knode *)*arg; |
576 | if (n) { | |
1da177e4 LT |
577 | if (TC_U32_KEY(n->handle) == 0) |
578 | return -EINVAL; | |
579 | ||
c1b52739 | 580 | return u32_set_parms(net, tp, base, n->ht_up, n, tb, |
2f7ef2f8 | 581 | tca[TCA_RATE], ovr); |
1da177e4 LT |
582 | } |
583 | ||
add93b61 | 584 | if (tb[TCA_U32_DIVISOR]) { |
cc7ec456 | 585 | unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]); |
1da177e4 LT |
586 | |
587 | if (--divisor > 0x100) | |
588 | return -EINVAL; | |
589 | if (TC_U32_KEY(handle)) | |
590 | return -EINVAL; | |
591 | if (handle == 0) { | |
592 | handle = gen_new_htid(tp->data); | |
593 | if (handle == 0) | |
594 | return -ENOMEM; | |
595 | } | |
cc7ec456 | 596 | ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL); |
1da177e4 LT |
597 | if (ht == NULL) |
598 | return -ENOBUFS; | |
1da177e4 | 599 | ht->tp_c = tp_c; |
e56cfad1 | 600 | ht->refcnt = 1; |
1da177e4 LT |
601 | ht->divisor = divisor; |
602 | ht->handle = handle; | |
603 | ht->prio = tp->prio; | |
604 | ht->next = tp_c->hlist; | |
605 | tp_c->hlist = ht; | |
606 | *arg = (unsigned long)ht; | |
607 | return 0; | |
608 | } | |
609 | ||
add93b61 | 610 | if (tb[TCA_U32_HASH]) { |
1587bac4 | 611 | htid = nla_get_u32(tb[TCA_U32_HASH]); |
1da177e4 LT |
612 | if (TC_U32_HTID(htid) == TC_U32_ROOT) { |
613 | ht = tp->root; | |
614 | htid = ht->handle; | |
615 | } else { | |
616 | ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid)); | |
617 | if (ht == NULL) | |
618 | return -EINVAL; | |
619 | } | |
620 | } else { | |
621 | ht = tp->root; | |
622 | htid = ht->handle; | |
623 | } | |
624 | ||
625 | if (ht->divisor < TC_U32_HASH(htid)) | |
626 | return -EINVAL; | |
627 | ||
628 | if (handle) { | |
629 | if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid)) | |
630 | return -EINVAL; | |
631 | handle = htid | TC_U32_NODE(handle); | |
632 | } else | |
633 | handle = gen_new_kid(ht, htid); | |
634 | ||
6fa8c014 | 635 | if (tb[TCA_U32_SEL] == NULL) |
1da177e4 LT |
636 | return -EINVAL; |
637 | ||
add93b61 | 638 | s = nla_data(tb[TCA_U32_SEL]); |
1da177e4 | 639 | |
0da974f4 | 640 | n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL); |
1da177e4 LT |
641 | if (n == NULL) |
642 | return -ENOBUFS; | |
643 | ||
1da177e4 | 644 | #ifdef CONFIG_CLS_U32_PERF |
0da974f4 | 645 | n->pf = kzalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL); |
1da177e4 LT |
646 | if (n->pf == NULL) { |
647 | kfree(n); | |
648 | return -ENOBUFS; | |
649 | } | |
1da177e4 LT |
650 | #endif |
651 | ||
652 | memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key)); | |
653 | n->ht_up = ht; | |
654 | n->handle = handle; | |
b2268016 | 655 | n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0; |
5da57f42 | 656 | tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE); |
1da177e4 LT |
657 | |
658 | #ifdef CONFIG_CLS_U32_MARK | |
add93b61 | 659 | if (tb[TCA_U32_MARK]) { |
1da177e4 LT |
660 | struct tc_u32_mark *mark; |
661 | ||
add93b61 | 662 | mark = nla_data(tb[TCA_U32_MARK]); |
1da177e4 LT |
663 | memcpy(&n->mark, mark, sizeof(struct tc_u32_mark)); |
664 | n->mark.success = 0; | |
665 | } | |
666 | #endif | |
667 | ||
2f7ef2f8 | 668 | err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr); |
1da177e4 LT |
669 | if (err == 0) { |
670 | struct tc_u_knode **ins; | |
671 | for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next) | |
672 | if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle)) | |
673 | break; | |
674 | ||
675 | n->next = *ins; | |
6f573214 | 676 | tcf_tree_lock(tp); |
1da177e4 | 677 | *ins = n; |
6f573214 | 678 | tcf_tree_unlock(tp); |
1da177e4 LT |
679 | |
680 | *arg = (unsigned long)n; | |
681 | return 0; | |
682 | } | |
683 | #ifdef CONFIG_CLS_U32_PERF | |
1ae39a43 | 684 | kfree(n->pf); |
1da177e4 LT |
685 | #endif |
686 | kfree(n); | |
687 | return err; | |
688 | } | |
689 | ||
690 | static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
691 | { | |
692 | struct tc_u_common *tp_c = tp->data; | |
693 | struct tc_u_hnode *ht; | |
694 | struct tc_u_knode *n; | |
cc7ec456 | 695 | unsigned int h; |
1da177e4 LT |
696 | |
697 | if (arg->stop) | |
698 | return; | |
699 | ||
700 | for (ht = tp_c->hlist; ht; ht = ht->next) { | |
701 | if (ht->prio != tp->prio) | |
702 | continue; | |
703 | if (arg->count >= arg->skip) { | |
704 | if (arg->fn(tp, (unsigned long)ht, arg) < 0) { | |
705 | arg->stop = 1; | |
706 | return; | |
707 | } | |
708 | } | |
709 | arg->count++; | |
710 | for (h = 0; h <= ht->divisor; h++) { | |
711 | for (n = ht->ht[h]; n; n = n->next) { | |
712 | if (arg->count < arg->skip) { | |
713 | arg->count++; | |
714 | continue; | |
715 | } | |
716 | if (arg->fn(tp, (unsigned long)n, arg) < 0) { | |
717 | arg->stop = 1; | |
718 | return; | |
719 | } | |
720 | arg->count++; | |
721 | } | |
722 | } | |
723 | } | |
724 | } | |
725 | ||
832d1d5b | 726 | static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
1da177e4 LT |
727 | struct sk_buff *skb, struct tcmsg *t) |
728 | { | |
cc7ec456 | 729 | struct tc_u_knode *n = (struct tc_u_knode *)fh; |
4b3550ef | 730 | struct nlattr *nest; |
1da177e4 LT |
731 | |
732 | if (n == NULL) | |
733 | return skb->len; | |
734 | ||
735 | t->tcm_handle = n->handle; | |
736 | ||
4b3550ef PM |
737 | nest = nla_nest_start(skb, TCA_OPTIONS); |
738 | if (nest == NULL) | |
739 | goto nla_put_failure; | |
1da177e4 LT |
740 | |
741 | if (TC_U32_KEY(n->handle) == 0) { | |
cc7ec456 ED |
742 | struct tc_u_hnode *ht = (struct tc_u_hnode *)fh; |
743 | u32 divisor = ht->divisor + 1; | |
744 | ||
1b34ec43 DM |
745 | if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor)) |
746 | goto nla_put_failure; | |
1da177e4 | 747 | } else { |
1b34ec43 DM |
748 | if (nla_put(skb, TCA_U32_SEL, |
749 | sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key), | |
750 | &n->sel)) | |
751 | goto nla_put_failure; | |
1da177e4 LT |
752 | if (n->ht_up) { |
753 | u32 htid = n->handle & 0xFFFFF000; | |
1b34ec43 DM |
754 | if (nla_put_u32(skb, TCA_U32_HASH, htid)) |
755 | goto nla_put_failure; | |
1da177e4 | 756 | } |
1b34ec43 DM |
757 | if (n->res.classid && |
758 | nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid)) | |
759 | goto nla_put_failure; | |
760 | if (n->ht_down && | |
761 | nla_put_u32(skb, TCA_U32_LINK, n->ht_down->handle)) | |
762 | goto nla_put_failure; | |
1da177e4 LT |
763 | |
764 | #ifdef CONFIG_CLS_U32_MARK | |
1b34ec43 DM |
765 | if ((n->mark.val || n->mark.mask) && |
766 | nla_put(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark)) | |
767 | goto nla_put_failure; | |
1da177e4 LT |
768 | #endif |
769 | ||
5da57f42 | 770 | if (tcf_exts_dump(skb, &n->exts) < 0) |
add93b61 | 771 | goto nla_put_failure; |
1da177e4 LT |
772 | |
773 | #ifdef CONFIG_NET_CLS_IND | |
2519a602 WC |
774 | if (n->ifindex) { |
775 | struct net_device *dev; | |
776 | dev = __dev_get_by_index(net, n->ifindex); | |
777 | if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name)) | |
778 | goto nla_put_failure; | |
779 | } | |
1da177e4 LT |
780 | #endif |
781 | #ifdef CONFIG_CLS_U32_PERF | |
1b34ec43 DM |
782 | if (nla_put(skb, TCA_U32_PCNT, |
783 | sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64), | |
784 | n->pf)) | |
785 | goto nla_put_failure; | |
1da177e4 LT |
786 | #endif |
787 | } | |
788 | ||
4b3550ef PM |
789 | nla_nest_end(skb, nest); |
790 | ||
1da177e4 | 791 | if (TC_U32_KEY(n->handle)) |
5da57f42 | 792 | if (tcf_exts_dump_stats(skb, &n->exts) < 0) |
add93b61 | 793 | goto nla_put_failure; |
1da177e4 LT |
794 | return skb->len; |
795 | ||
add93b61 | 796 | nla_put_failure: |
4b3550ef | 797 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
798 | return -1; |
799 | } | |
800 | ||
2eb9d75c | 801 | static struct tcf_proto_ops cls_u32_ops __read_mostly = { |
1da177e4 LT |
802 | .kind = "u32", |
803 | .classify = u32_classify, | |
804 | .init = u32_init, | |
805 | .destroy = u32_destroy, | |
806 | .get = u32_get, | |
807 | .put = u32_put, | |
808 | .change = u32_change, | |
809 | .delete = u32_delete, | |
810 | .walk = u32_walk, | |
811 | .dump = u32_dump, | |
812 | .owner = THIS_MODULE, | |
813 | }; | |
814 | ||
815 | static int __init init_u32(void) | |
816 | { | |
6ff9c364 | 817 | pr_info("u32 classifier\n"); |
1da177e4 | 818 | #ifdef CONFIG_CLS_U32_PERF |
6ff9c364 | 819 | pr_info(" Performance counters on\n"); |
1da177e4 | 820 | #endif |
1da177e4 | 821 | #ifdef CONFIG_NET_CLS_IND |
6ff9c364 | 822 | pr_info(" input device check on\n"); |
1da177e4 LT |
823 | #endif |
824 | #ifdef CONFIG_NET_CLS_ACT | |
6ff9c364 | 825 | pr_info(" Actions configured\n"); |
1da177e4 LT |
826 | #endif |
827 | return register_tcf_proto_ops(&cls_u32_ops); | |
828 | } | |
829 | ||
10297b99 | 830 | static void __exit exit_u32(void) |
1da177e4 LT |
831 | { |
832 | unregister_tcf_proto_ops(&cls_u32_ops); | |
833 | } | |
834 | ||
835 | module_init(init_u32) | |
836 | module_exit(exit_u32) | |
837 | MODULE_LICENSE("GPL"); |