[NET_SCHED]: sch_ingress: remove unnecessary ops
[safe/jmp/linux-2.6] / net / sched / sch_ingress.c
1 /* net/sched/sch_ingress.c - Ingress qdisc
2  *              This program is free software; you can redistribute it and/or
3  *              modify it under the terms of the GNU General Public License
4  *              as published by the Free Software Foundation; either version
5  *              2 of the License, or (at your option) any later version.
6  *
7  * Authors:     Jamal Hadi Salim 1999
8  */
9
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/list.h>
13 #include <linux/skbuff.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/netfilter_ipv4.h>
16 #include <linux/netfilter_ipv6.h>
17 #include <linux/netfilter.h>
18 #include <net/netlink.h>
19 #include <net/pkt_sched.h>
20
21
22 /* Thanks to Doron Oz for this hack */
23 #ifndef CONFIG_NET_CLS_ACT
24 #ifdef CONFIG_NETFILTER
25 static int nf_registered;
26 #endif
27 #endif
28
29 struct ingress_qdisc_data {
30         struct tcf_proto        *filter_list;
31 };
32
33 /* ------------------------- Class/flow operations ------------------------- */
34
35 static int ingress_graft(struct Qdisc *sch, unsigned long arg,
36                          struct Qdisc *new, struct Qdisc **old)
37 {
38         return -EOPNOTSUPP;
39 }
40
41 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
42 {
43         return NULL;
44 }
45
46 static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
47 {
48         return TC_H_MIN(classid) + 1;
49 }
50
51 static unsigned long ingress_bind_filter(struct Qdisc *sch,
52                                          unsigned long parent, u32 classid)
53 {
54         return ingress_get(sch, classid);
55 }
56
57 static void ingress_put(struct Qdisc *sch, unsigned long cl)
58 {
59 }
60
61 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
62                           struct rtattr **tca, unsigned long *arg)
63 {
64         return 0;
65 }
66
67 static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
68 {
69         return;
70 }
71
72 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
73 {
74         struct ingress_qdisc_data *p = qdisc_priv(sch);
75
76         return &p->filter_list;
77 }
78
79 /* --------------------------- Qdisc operations ---------------------------- */
80
81 static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
82 {
83         struct ingress_qdisc_data *p = qdisc_priv(sch);
84         struct tcf_result res;
85         int result;
86
87         result = tc_classify(skb, p->filter_list, &res);
88
89         /*
90          * Unlike normal "enqueue" functions, ingress_enqueue returns a
91          * firewall FW_* code.
92          */
93 #ifdef CONFIG_NET_CLS_ACT
94         sch->bstats.packets++;
95         sch->bstats.bytes += skb->len;
96         switch (result) {
97         case TC_ACT_SHOT:
98                 result = TC_ACT_SHOT;
99                 sch->qstats.drops++;
100                 break;
101         case TC_ACT_STOLEN:
102         case TC_ACT_QUEUED:
103                 result = TC_ACT_STOLEN;
104                 break;
105         case TC_ACT_RECLASSIFY:
106         case TC_ACT_OK:
107                 skb->tc_index = TC_H_MIN(res.classid);
108         default:
109                 result = TC_ACT_OK;
110                 break;
111         }
112 #else
113         result = NF_ACCEPT;
114         sch->bstats.packets++;
115         sch->bstats.bytes += skb->len;
116 #endif
117
118         return result;
119 }
120
121 #ifndef CONFIG_NET_CLS_ACT
122 #ifdef CONFIG_NETFILTER
123 static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
124                              const struct net_device *indev,
125                              const struct net_device *outdev,
126                              int (*okfn)(struct sk_buff *))
127 {
128
129         struct Qdisc *q;
130         struct net_device *dev = skb->dev;
131         int fwres = NF_ACCEPT;
132
133         if (dev->qdisc_ingress) {
134                 spin_lock(&dev->ingress_lock);
135                 if ((q = dev->qdisc_ingress) != NULL)
136                         fwres = q->enqueue(skb, q);
137                 spin_unlock(&dev->ingress_lock);
138         }
139
140         return fwres;
141 }
142
143 /* after ipt_filter */
144 static struct nf_hook_ops ing_ops[] __read_mostly = {
145         {
146                 .hook           = ing_hook,
147                 .owner          = THIS_MODULE,
148                 .pf             = PF_INET,
149                 .hooknum        = NF_INET_PRE_ROUTING,
150                 .priority       = NF_IP_PRI_FILTER + 1,
151         },
152         {
153                 .hook           = ing_hook,
154                 .owner          = THIS_MODULE,
155                 .pf             = PF_INET6,
156                 .hooknum        = NF_INET_PRE_ROUTING,
157                 .priority       = NF_IP6_PRI_FILTER + 1,
158         },
159 };
160 #endif
161 #endif
162
163 static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
164 {
165         /* Make sure either netfilter or preferably CLS_ACT is
166          * compiled in */
167 #ifndef CONFIG_NET_CLS_ACT
168 #ifndef CONFIG_NETFILTER
169         printk("You MUST compile classifier actions into the kernel\n");
170         return -EINVAL;
171 #else
172         printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
173 #endif
174 #endif
175
176 #ifndef CONFIG_NET_CLS_ACT
177 #ifdef CONFIG_NETFILTER
178         if (!nf_registered) {
179                 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
180                         printk("ingress qdisc registration error \n");
181                         return -EINVAL;
182                 }
183                 nf_registered++;
184         }
185 #endif
186 #endif
187         return 0;
188 }
189
190 /* ------------------------------------------------------------- */
191
192 static void ingress_destroy(struct Qdisc *sch)
193 {
194         struct ingress_qdisc_data *p = qdisc_priv(sch);
195
196         tcf_destroy_chain(p->filter_list);
197 }
198
199 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
200 {
201         unsigned char *b = skb_tail_pointer(skb);
202         struct rtattr *rta;
203
204         rta = (struct rtattr *)b;
205         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
206         rta->rta_len = skb_tail_pointer(skb) - b;
207         return skb->len;
208
209 rtattr_failure:
210         nlmsg_trim(skb, b);
211         return -1;
212 }
213
214 static const struct Qdisc_class_ops ingress_class_ops = {
215         .graft          =       ingress_graft,
216         .leaf           =       ingress_leaf,
217         .get            =       ingress_get,
218         .put            =       ingress_put,
219         .change         =       ingress_change,
220         .walk           =       ingress_walk,
221         .tcf_chain      =       ingress_find_tcf,
222         .bind_tcf       =       ingress_bind_filter,
223         .unbind_tcf     =       ingress_put,
224 };
225
226 static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
227         .cl_ops         =       &ingress_class_ops,
228         .id             =       "ingress",
229         .priv_size      =       sizeof(struct ingress_qdisc_data),
230         .enqueue        =       ingress_enqueue,
231         .init           =       ingress_init,
232         .destroy        =       ingress_destroy,
233         .dump           =       ingress_dump,
234         .owner          =       THIS_MODULE,
235 };
236
237 static int __init ingress_module_init(void)
238 {
239         int ret = 0;
240
241         if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
242                 printk("Unable to register Ingress qdisc\n");
243                 return ret;
244         }
245
246         return ret;
247 }
248
249 static void __exit ingress_module_exit(void)
250 {
251         unregister_qdisc(&ingress_qdisc_ops);
252 #ifndef CONFIG_NET_CLS_ACT
253 #ifdef CONFIG_NETFILTER
254         if (nf_registered)
255                 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
256 #endif
257 #endif
258 }
259
260 module_init(ingress_module_init)
261 module_exit(ingress_module_exit)
262 MODULE_LICENSE("GPL");