This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / net / sched / cls_u32.c
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  */
31
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <asm/bitops.h>
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/string.h>
41 #include <linux/mm.h>
42 #include <linux/socket.h>
43 #include <linux/sockios.h>
44 #include <linux/in.h>
45 #include <linux/errno.h>
46 #include <linux/interrupt.h>
47 #include <linux/if_ether.h>
48 #include <linux/inet.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include <linux/notifier.h>
52 #include <linux/rtnetlink.h>
53 #include <net/ip.h>
54 #include <net/route.h>
55 #include <linux/skbuff.h>
56 #include <net/sock.h>
57 #include <net/pkt_sched.h>
58
59
60 struct tc_u_knode
61 {
62         struct tc_u_knode       *next;
63         u32                     handle;
64         struct tc_u_hnode       *ht_up;
65 #ifdef CONFIG_NET_CLS_ACT
66         struct tc_action        *action;
67 #ifdef CONFIG_NET_CLS_IND
68         char                     indev[IFNAMSIZ];
69 #endif
70 #else
71 #ifdef CONFIG_NET_CLS_POLICE
72         struct tcf_police       *police;
73 #endif
74 #endif
75         u8                      fshift;
76         struct tcf_result       res;
77         struct tc_u_hnode       *ht_down;
78         struct tc_u32_sel       sel;
79 };
80
81 struct tc_u_hnode
82 {
83         struct tc_u_hnode       *next;
84         u32                     handle;
85         struct tc_u_common      *tp_c;
86         int                     refcnt;
87         unsigned                divisor;
88         u32                     hgenerator;
89         struct tc_u_knode       *ht[1];
90 };
91
92 struct tc_u_common
93 {
94         struct tc_u_common      *next;
95         struct tc_u_hnode       *hlist;
96         struct Qdisc            *q;
97         int                     refcnt;
98         u32                     hgenerator;
99 };
100
101 static struct tc_u_common *u32_list;
102
103 static __inline__ unsigned u32_hash_fold(u32 key, struct tc_u32_sel *sel, u8 fshift)
104 {
105         unsigned h = (key & sel->hmask)>>fshift;
106
107         return h;
108 }
109
110 static int u32_classify(struct sk_buff *skb, struct tcf_proto *tp, struct tcf_result *res)
111 {
112         struct {
113                 struct tc_u_knode *knode;
114                 u8                *ptr;
115         } stack[TC_U32_MAXDEPTH];
116
117         struct tc_u_hnode *ht = (struct tc_u_hnode*)tp->root;
118         u8 *ptr = skb->nh.raw;
119         struct tc_u_knode *n;
120         int sdepth = 0;
121         int off2 = 0;
122         int sel = 0;
123         int i;
124
125 next_ht:
126         n = ht->ht[sel];
127
128 next_knode:
129         if (n) {
130                 struct tc_u32_key *key = n->sel.keys;
131
132 #ifdef CONFIG_CLS_U32_PERF
133                 n->sel.rcnt +=1;
134 #endif
135                 for (i = n->sel.nkeys; i>0; i--, key++) {
136
137                         if ((*(u32*)(ptr+key->off+(off2&key->offmask))^key->val)&key->mask) {
138                                 n = n->next;
139                                 goto next_knode;
140                         }
141 #ifdef CONFIG_CLS_U32_PERF
142                         key->kcnt +=1;
143 #endif
144                 }
145                 if (n->ht_down == NULL) {
146 check_terminal:
147                         if (n->sel.flags&TC_U32_TERMINAL) {
148
149                                 *res = n->res;
150 #ifdef CONFIG_NET_CLS_ACT
151 #ifdef CONFIG_NET_CLS_IND
152                                 /* yes, i know it sucks but the feature is 
153                                 ** optional dammit! - JHS */
154                                 if (0 != n->indev[0]) {
155                                         if  (NULL == skb->input_dev) {
156                                                 n = n->next;
157                                                 goto next_knode;
158                                         } else {
159                                                 if (0 != strcmp(n->indev, skb->input_dev->name)) {
160                                                         n = n->next;
161                                                         goto next_knode;
162                                                 }
163                                         }
164                                 }
165 #endif
166 #ifdef CONFIG_CLS_U32_PERF
167                 n->sel.rhit +=1;
168 #endif
169                                 if (n->action) {
170                                         int pol_res = tcf_action_exec(skb, n->action);
171                                         if (skb->tc_classid > 0) {
172                                                 res->classid = skb->tc_classid;
173                                                 skb->tc_classid = 0;
174                                         }
175
176                                         if (pol_res >= 0)
177                                                 return pol_res;
178                                 } else
179 #else
180 #ifdef CONFIG_NET_CLS_POLICE
181                                 if (n->police) {
182                                         int pol_res = tcf_police(skb, n->police);
183                                         if (pol_res >= 0)
184                                                 return pol_res;
185                                 } else
186 #endif
187 #endif
188                                         return 0;
189                         }
190                         n = n->next;
191                         goto next_knode;
192                 }
193
194                 /* PUSH */
195                 if (sdepth >= TC_U32_MAXDEPTH)
196                         goto deadloop;
197                 stack[sdepth].knode = n;
198                 stack[sdepth].ptr = ptr;
199                 sdepth++;
200
201                 ht = n->ht_down;
202                 sel = 0;
203                 if (ht->divisor)
204                         sel = ht->divisor&u32_hash_fold(*(u32*)(ptr+n->sel.hoff), &n->sel,n->fshift);
205
206                 if (!(n->sel.flags&(TC_U32_VAROFFSET|TC_U32_OFFSET|TC_U32_EAT)))
207                         goto next_ht;
208
209                 if (n->sel.flags&(TC_U32_OFFSET|TC_U32_VAROFFSET)) {
210                         off2 = n->sel.off + 3;
211                         if (n->sel.flags&TC_U32_VAROFFSET)
212                                 off2 += ntohs(n->sel.offmask & *(u16*)(ptr+n->sel.offoff)) >>n->sel.offshift;
213                         off2 &= ~3;
214                 }
215                 if (n->sel.flags&TC_U32_EAT) {
216                         ptr += off2;
217                         off2 = 0;
218                 }
219
220                 if (ptr < skb->tail)
221                         goto next_ht;
222         }
223
224         /* POP */
225         if (sdepth--) {
226                 n = stack[sdepth].knode;
227                 ht = n->ht_up;
228                 ptr = stack[sdepth].ptr;
229                 goto check_terminal;
230         }
231         return -1;
232
233 deadloop:
234         if (net_ratelimit())
235                 printk("cls_u32: dead loop\n");
236         return -1;
237 }
238
239 static __inline__ struct tc_u_hnode *
240 u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
241 {
242         struct tc_u_hnode *ht;
243
244         for (ht = tp_c->hlist; ht; ht = ht->next)
245                 if (ht->handle == handle)
246                         break;
247
248         return ht;
249 }
250
251 static __inline__ struct tc_u_knode *
252 u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
253 {
254         unsigned sel;
255         struct tc_u_knode *n = NULL;
256
257         sel = TC_U32_HASH(handle);
258         if (sel > ht->divisor)
259                 goto out;
260
261         for (n = ht->ht[sel]; n; n = n->next)
262                 if (n->handle == handle)
263                         break;
264 out:
265         return n;
266 }
267
268
269 static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
270 {
271         struct tc_u_hnode *ht;
272         struct tc_u_common *tp_c = tp->data;
273
274         if (TC_U32_HTID(handle) == TC_U32_ROOT)
275                 ht = tp->root;
276         else
277                 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
278
279         if (!ht)
280                 return 0;
281
282         if (TC_U32_KEY(handle) == 0)
283                 return (unsigned long)ht;
284
285         return (unsigned long)u32_lookup_key(ht, handle);
286 }
287
288 static void u32_put(struct tcf_proto *tp, unsigned long f)
289 {
290 }
291
292 static u32 gen_new_htid(struct tc_u_common *tp_c)
293 {
294         int i = 0x800;
295
296         do {
297                 if (++tp_c->hgenerator == 0x7FF)
298                         tp_c->hgenerator = 1;
299         } while (--i>0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
300
301         return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
302 }
303
304 static int u32_init(struct tcf_proto *tp)
305 {
306         struct tc_u_hnode *root_ht;
307         struct tc_u_common *tp_c;
308
309         for (tp_c = u32_list; tp_c; tp_c = tp_c->next)
310                 if (tp_c->q == tp->q)
311                         break;
312
313         root_ht = kmalloc(sizeof(*root_ht), GFP_KERNEL);
314         if (root_ht == NULL)
315                 return -ENOBUFS;
316
317         memset(root_ht, 0, sizeof(*root_ht));
318         root_ht->divisor = 0;
319         root_ht->refcnt++;
320         root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
321
322         if (tp_c == NULL) {
323                 tp_c = kmalloc(sizeof(*tp_c), GFP_KERNEL);
324                 if (tp_c == NULL) {
325                         kfree(root_ht);
326                         return -ENOBUFS;
327                 }
328                 memset(tp_c, 0, sizeof(*tp_c));
329                 tp_c->q = tp->q;
330                 tp_c->next = u32_list;
331                 u32_list = tp_c;
332         }
333
334         tp_c->refcnt++;
335         root_ht->next = tp_c->hlist;
336         tp_c->hlist = root_ht;
337         root_ht->tp_c = tp_c;
338
339         tp->root = root_ht;
340         tp->data = tp_c;
341         return 0;
342 }
343
344 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
345 {
346         unsigned long cl;
347
348         if ((cl = __cls_set_class(&n->res.class, 0)) != 0)
349                 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
350 #ifdef CONFIG_NET_CLS_ACT
351         if (n->action) {
352                 tcf_action_destroy(n->action, TCA_ACT_UNBIND);
353         }
354 #else
355 #ifdef CONFIG_NET_CLS_POLICE
356         tcf_police_release(n->police, TCA_ACT_UNBIND);
357 #endif
358 #endif
359         if (n->ht_down)
360                 n->ht_down->refcnt--;
361         kfree(n);
362         return 0;
363 }
364
365 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode* key)
366 {
367         struct tc_u_knode **kp;
368         struct tc_u_hnode *ht = key->ht_up;
369
370         if (ht) {
371                 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
372                         if (*kp == key) {
373                                 tcf_tree_lock(tp);
374                                 *kp = key->next;
375                                 tcf_tree_unlock(tp);
376
377                                 u32_destroy_key(tp, key);
378                                 return 0;
379                         }
380                 }
381         }
382         BUG_TRAP(0);
383         return 0;
384 }
385
386 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
387 {
388         struct tc_u_knode *n;
389         unsigned h;
390
391         for (h=0; h<=ht->divisor; h++) {
392                 while ((n = ht->ht[h]) != NULL) {
393                         ht->ht[h] = n->next;
394
395                         u32_destroy_key(tp, n);
396                 }
397         }
398 }
399
400 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
401 {
402         struct tc_u_common *tp_c = tp->data;
403         struct tc_u_hnode **hn;
404
405         BUG_TRAP(!ht->refcnt);
406
407         u32_clear_hnode(tp, ht);
408
409         for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
410                 if (*hn == ht) {
411                         *hn = ht->next;
412                         kfree(ht);
413                         return 0;
414                 }
415         }
416
417         BUG_TRAP(0);
418         return -ENOENT;
419 }
420
421 static void u32_destroy(struct tcf_proto *tp)
422 {
423         struct tc_u_common *tp_c = tp->data;
424         struct tc_u_hnode *root_ht = xchg(&tp->root, NULL);
425
426         BUG_TRAP(root_ht != NULL);
427
428         if (root_ht && --root_ht->refcnt == 0)
429                 u32_destroy_hnode(tp, root_ht);
430
431         if (--tp_c->refcnt == 0) {
432                 struct tc_u_hnode *ht;
433                 struct tc_u_common **tp_cp;
434
435                 for (tp_cp = &u32_list; *tp_cp; tp_cp = &(*tp_cp)->next) {
436                         if (*tp_cp == tp_c) {
437                                 *tp_cp = tp_c->next;
438                                 break;
439                         }
440                 }
441
442                 for (ht=tp_c->hlist; ht; ht = ht->next)
443                         u32_clear_hnode(tp, ht);
444
445                 while ((ht = tp_c->hlist) != NULL) {
446                         tp_c->hlist = ht->next;
447
448                         BUG_TRAP(ht->refcnt == 0);
449
450                         kfree(ht);
451                 };
452
453                 kfree(tp_c);
454         }
455
456         tp->data = NULL;
457 }
458
459 static int u32_delete(struct tcf_proto *tp, unsigned long arg)
460 {
461         struct tc_u_hnode *ht = (struct tc_u_hnode*)arg;
462
463         if (ht == NULL)
464                 return 0;
465
466         if (TC_U32_KEY(ht->handle))
467                 return u32_delete_key(tp, (struct tc_u_knode*)ht);
468
469         if (tp->root == ht)
470                 return -EINVAL;
471
472         if (--ht->refcnt == 0)
473                 u32_destroy_hnode(tp, ht);
474
475         return 0;
476 }
477
478 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
479 {
480         struct tc_u_knode *n;
481         unsigned i = 0x7FF;
482
483         for (n=ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
484                 if (i < TC_U32_NODE(n->handle))
485                         i = TC_U32_NODE(n->handle);
486         i++;
487
488         return handle|(i>0xFFF ? 0xFFF : i);
489 }
490
491 static int u32_set_parms(struct Qdisc *q, unsigned long base,
492                          struct tc_u_hnode *ht,
493                          struct tc_u_knode *n, struct rtattr **tb,
494                          struct rtattr *est)
495 {
496 #ifdef CONFIG_NET_CLS_ACT
497         struct tc_action *act = NULL;
498         int ret;
499 #endif
500         if (tb[TCA_U32_LINK-1]) {
501                 u32 handle = *(u32*)RTA_DATA(tb[TCA_U32_LINK-1]);
502                 struct tc_u_hnode *ht_down = NULL;
503
504                 if (TC_U32_KEY(handle))
505                         return -EINVAL;
506
507                 if (handle) {
508                         ht_down = u32_lookup_ht(ht->tp_c, handle);
509
510                         if (ht_down == NULL)
511                                 return -EINVAL;
512                         ht_down->refcnt++;
513                 }
514
515                 sch_tree_lock(q);
516                 ht_down = xchg(&n->ht_down, ht_down);
517                 sch_tree_unlock(q);
518
519                 if (ht_down)
520                         ht_down->refcnt--;
521         }
522         if (tb[TCA_U32_CLASSID-1]) {
523                 unsigned long cl;
524
525                 n->res.classid = *(u32*)RTA_DATA(tb[TCA_U32_CLASSID-1]);
526                 sch_tree_lock(q);
527                 cl = __cls_set_class(&n->res.class, q->ops->cl_ops->bind_tcf(q, base, n->res.classid));
528                 sch_tree_unlock(q);
529                 if (cl)
530                         q->ops->cl_ops->unbind_tcf(q, cl);
531         }
532 #ifdef CONFIG_NET_CLS_ACT
533         /*backward compatibility */
534         if (tb[TCA_U32_POLICE-1])
535         {
536                 act = kmalloc(sizeof(*act),GFP_KERNEL);
537                 if (NULL == act)
538                         return -ENOMEM;
539
540                 memset(act,0,sizeof(*act));
541                 ret = tcf_action_init_1(tb[TCA_U32_POLICE-1], est,act,"police", TCA_ACT_NOREPLACE, TCA_ACT_BIND);
542                 if (0 > ret){
543                         tcf_action_destroy(act, TCA_ACT_UNBIND);
544                         return ret;
545                 }
546                 act->type = TCA_OLD_COMPAT;
547
548                 sch_tree_lock(q);
549                 act = xchg(&n->action, act);
550                 sch_tree_unlock(q);
551
552                 tcf_action_destroy(act, TCA_ACT_UNBIND);
553
554         }
555
556         if(tb[TCA_U32_ACT-1]) {
557                 act = kmalloc(sizeof(*act),GFP_KERNEL);
558                 if (NULL == act)
559                         return -ENOMEM;
560                 memset(act,0,sizeof(*act));
561                 ret = tcf_action_init(tb[TCA_U32_ACT-1], est,act,NULL,TCA_ACT_NOREPLACE, TCA_ACT_BIND);
562                 if (0 > ret) {
563                         tcf_action_destroy(act, TCA_ACT_UNBIND);
564                         return ret;
565                 }
566
567                 sch_tree_lock(q);
568                 act = xchg(&n->action, act);
569                 sch_tree_unlock(q);
570
571                 tcf_action_destroy(act, TCA_ACT_UNBIND);
572         }
573
574 #ifdef CONFIG_NET_CLS_IND
575         n->indev[0] = 0;
576         if(tb[TCA_U32_INDEV-1]) {
577                 struct rtattr *input_dev = tb[TCA_U32_INDEV-1];
578                 if (RTA_PAYLOAD(input_dev) >= IFNAMSIZ) {
579                         printk("cls_u32: bad indev name %s\n",(char*)RTA_DATA(input_dev));
580                         /* should we clear state first? */
581                         return  -EINVAL;
582                 }
583                 sprintf(n->indev, "%s", (char*)RTA_DATA(input_dev));
584         }
585 #endif
586
587 #else
588 #ifdef CONFIG_NET_CLS_POLICE
589         if (tb[TCA_U32_POLICE-1]) {
590                 struct tcf_police *police = tcf_police_locate(tb[TCA_U32_POLICE-1], est);
591                 sch_tree_lock(q);
592                 police = xchg(&n->police, police);
593                 sch_tree_unlock(q);
594                 tcf_police_release(police, TCA_ACT_UNBIND);
595         }
596 #endif
597 #endif
598
599         return 0;
600 }
601
602 static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
603                       struct rtattr **tca,
604                       unsigned long *arg)
605 {
606         struct tc_u_common *tp_c = tp->data;
607         struct tc_u_hnode *ht;
608         struct tc_u_knode *n;
609         struct tc_u32_sel *s;
610         struct rtattr *opt = tca[TCA_OPTIONS-1];
611         struct rtattr *tb[TCA_U32_MAX];
612         u32 htid;
613         int err;
614
615         if (opt == NULL)
616                 return handle ? -EINVAL : 0;
617
618         if (rtattr_parse(tb, TCA_U32_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
619                 return -EINVAL;
620
621         if ((n = (struct tc_u_knode*)*arg) != NULL) {
622                 if (TC_U32_KEY(n->handle) == 0)
623                         return -EINVAL;
624
625                 return u32_set_parms(tp->q, base, n->ht_up, n, tb, tca[TCA_RATE-1]);
626         }
627
628         if (tb[TCA_U32_DIVISOR-1]) {
629                 unsigned divisor = *(unsigned*)RTA_DATA(tb[TCA_U32_DIVISOR-1]);
630
631                 if (--divisor > 0x100)
632                         return -EINVAL;
633                 if (TC_U32_KEY(handle))
634                         return -EINVAL;
635                 if (handle == 0) {
636                         handle = gen_new_htid(tp->data);
637                         if (handle == 0)
638                                 return -ENOMEM;
639                 }
640                 ht = kmalloc(sizeof(*ht) + divisor*sizeof(void*), GFP_KERNEL);
641                 if (ht == NULL)
642                         return -ENOBUFS;
643                 memset(ht, 0, sizeof(*ht) + divisor*sizeof(void*));
644                 ht->tp_c = tp_c;
645                 ht->refcnt = 0;
646                 ht->divisor = divisor;
647                 ht->handle = handle;
648                 ht->next = tp_c->hlist;
649                 tp_c->hlist = ht;
650                 *arg = (unsigned long)ht;
651                 return 0;
652         }
653
654         if (tb[TCA_U32_HASH-1]) {
655                 htid = *(unsigned*)RTA_DATA(tb[TCA_U32_HASH-1]);
656                 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
657                         ht = tp->root;
658                         htid = ht->handle;
659                 } else {
660                         ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
661                         if (ht == NULL)
662                                 return -EINVAL;
663                 }
664         } else {
665                 ht = tp->root;
666                 htid = ht->handle;
667         }
668
669         if (ht->divisor < TC_U32_HASH(htid))
670                 return -EINVAL;
671
672         if (handle) {
673                 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
674                         return -EINVAL;
675                 handle = htid | TC_U32_NODE(handle);
676         } else
677                 handle = gen_new_kid(ht, htid);
678
679         if (tb[TCA_U32_SEL-1] == 0 ||
680             RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < sizeof(struct tc_u32_sel))
681                 return -EINVAL;
682
683         s = RTA_DATA(tb[TCA_U32_SEL-1]);
684
685 #ifdef CONFIG_CLS_U32_PERF
686         if (RTA_PAYLOAD(tb[TCA_U32_SEL-1]) < 
687                 (s->nkeys*sizeof(struct tc_u32_key)) + sizeof(struct tc_u32_sel)) {
688                         printk("Please upgrade your iproute2 tools or compile proper options in!\n");
689                         return -EINVAL;
690 }
691 #endif
692         n = kmalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
693         if (n == NULL)
694                 return -ENOBUFS;
695         memset(n, 0, sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key));
696         memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
697         n->ht_up = ht;
698         n->handle = handle;
699 {
700         u8 i = 0;
701         u32 mask = s->hmask;
702         if (mask) {
703                 while (!(mask & 1)) {
704                         i++;
705                         mask>>=1;
706                 }
707         }
708         n->fshift = i;
709 }
710         err = u32_set_parms(tp->q, base, ht, n, tb, tca[TCA_RATE-1]);
711         if (err == 0) {
712                 struct tc_u_knode **ins;
713                 for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
714                         if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
715                                 break;
716
717                 n->next = *ins;
718                 wmb();
719                 *ins = n;
720
721                 *arg = (unsigned long)n;
722                 return 0;
723         }
724         kfree(n);
725         return err;
726 }
727
728 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
729 {
730         struct tc_u_common *tp_c = tp->data;
731         struct tc_u_hnode *ht;
732         struct tc_u_knode *n;
733         unsigned h;
734
735         if (arg->stop)
736                 return;
737
738         for (ht = tp_c->hlist; ht; ht = ht->next) {
739                 if (arg->count >= arg->skip) {
740                         if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
741                                 arg->stop = 1;
742                                 return;
743                         }
744                 }
745                 arg->count++;
746                 for (h = 0; h <= ht->divisor; h++) {
747                         for (n = ht->ht[h]; n; n = n->next) {
748                                 if (arg->count < arg->skip) {
749                                         arg->count++;
750                                         continue;
751                                 }
752                                 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
753                                         arg->stop = 1;
754                                         return;
755                                 }
756                                 arg->count++;
757                         }
758                 }
759         }
760 }
761
762 static int u32_dump(struct tcf_proto *tp, unsigned long fh,
763                      struct sk_buff *skb, struct tcmsg *t)
764 {
765         struct tc_u_knode *n = (struct tc_u_knode*)fh;
766         unsigned char    *b = skb->tail;
767         struct rtattr *rta;
768
769         if (n == NULL)
770                 return skb->len;
771
772         t->tcm_handle = n->handle;
773
774         rta = (struct rtattr*)b;
775         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
776
777         if (TC_U32_KEY(n->handle) == 0) {
778                 struct tc_u_hnode *ht = (struct tc_u_hnode*)fh;
779                 u32 divisor = ht->divisor+1;
780                 RTA_PUT(skb, TCA_U32_DIVISOR, 4, &divisor);
781         } else {
782                 RTA_PUT(skb, TCA_U32_SEL,
783                         sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
784                         &n->sel);
785                 if (n->ht_up) {
786                         u32 htid = n->handle & 0xFFFFF000;
787                         RTA_PUT(skb, TCA_U32_HASH, 4, &htid);
788                 }
789                 if (n->res.classid)
790                         RTA_PUT(skb, TCA_U32_CLASSID, 4, &n->res.classid);
791                 if (n->ht_down)
792                         RTA_PUT(skb, TCA_U32_LINK, 4, &n->ht_down->handle);
793 #ifdef CONFIG_NET_CLS_ACT
794                 /* again for backward compatible mode - we want
795                 *  to work with both old and new modes of entering
796                 *  tc data even if iproute2  was newer - jhs 
797                 */
798                 if (n->action) {
799                         struct rtattr * p_rta = (struct rtattr*)skb->tail;
800
801                         if (n->action->type != TCA_OLD_COMPAT) {
802                                 RTA_PUT(skb, TCA_U32_ACT, 0, NULL);
803                                 if (tcf_action_dump(skb,n->action, 0, 0) < 0) {
804                                         goto rtattr_failure;
805                                 }
806                         } else {
807                                 RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
808                                 if (tcf_action_dump_old(skb,n->action,0,0) < 0) {
809                                         goto rtattr_failure;
810                                 }
811                         }
812
813                         p_rta->rta_len = skb->tail - (u8*)p_rta;
814                 }
815 #ifdef CONFIG_NET_CLS_IND
816                 if(strlen(n->indev)) {
817                         struct rtattr * p_rta = (struct rtattr*)skb->tail;
818                         RTA_PUT(skb, TCA_U32_INDEV, IFNAMSIZ, n->indev);
819                         p_rta->rta_len = skb->tail - (u8*)p_rta;
820                 }
821 #endif
822
823 #else
824 #ifdef CONFIG_NET_CLS_POLICE
825                 if (n->police) {
826                         struct rtattr * p_rta = (struct rtattr*)skb->tail;
827                         RTA_PUT(skb, TCA_U32_POLICE, 0, NULL);
828          
829                         if (tcf_police_dump(skb, n->police) < 0)
830                                 goto rtattr_failure;
831
832                         p_rta->rta_len = skb->tail - (u8*)p_rta;
833
834                 }
835 #endif
836 #endif
837         }
838
839         rta->rta_len = skb->tail - b;
840 #ifdef CONFIG_NET_CLS_ACT
841         if (TC_U32_KEY(n->handle) && n->action && n->action->type == TCA_OLD_COMPAT) {
842                 if (tcf_action_copy_stats(skb,n->action))
843                         goto rtattr_failure;
844         }
845 #else
846 #ifdef CONFIG_NET_CLS_POLICE
847         if (TC_U32_KEY(n->handle) && n->police) {
848                 if (qdisc_copy_stats(skb, &n->police->stats,
849                                      n->police->stats_lock))
850                         goto rtattr_failure;
851         }
852 #endif
853 #endif
854         return skb->len;
855
856 rtattr_failure:
857         skb_trim(skb, b - skb->data);
858         return -1;
859 }
860
861 static struct tcf_proto_ops cls_u32_ops = {
862         .next           =       NULL,
863         .kind           =       "u32",
864         .classify       =       u32_classify,
865         .init           =       u32_init,
866         .destroy        =       u32_destroy,
867         .get            =       u32_get,
868         .put            =       u32_put,
869         .change         =       u32_change,
870         .delete         =       u32_delete,
871         .walk           =       u32_walk,
872         .dump           =       u32_dump,
873         .owner          =       THIS_MODULE,
874 };
875
876 static int __init init_u32(void)
877 {
878         return register_tcf_proto_ops(&cls_u32_ops);
879 }
880
881 static void __exit exit_u32(void) 
882 {
883         unregister_tcf_proto_ops(&cls_u32_ops);
884 }
885
886 module_init(init_u32)
887 module_exit(exit_u32)
888 MODULE_LICENSE("GPL");