Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[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 #undef DEBUG_INGRESS
23
24 #ifdef DEBUG_INGRESS  /* control */
25 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
26 #else
27 #define DPRINTK(format,args...)
28 #endif
29
30 #if 0  /* data */
31 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
32 #else
33 #define D2PRINTK(format,args...)
34 #endif
35
36
37 #define PRIV(sch) qdisc_priv(sch)
38
39
40 /* Thanks to Doron Oz for this hack
41 */
42 #ifndef CONFIG_NET_CLS_ACT
43 #ifdef CONFIG_NETFILTER
44 static int nf_registered;
45 #endif
46 #endif
47
48 struct ingress_qdisc_data {
49         struct Qdisc            *q;
50         struct tcf_proto        *filter_list;
51 };
52
53
54 /* ------------------------- Class/flow operations ------------------------- */
55
56
57 static int ingress_graft(struct Qdisc *sch,unsigned long arg,
58     struct Qdisc *new,struct Qdisc **old)
59 {
60 #ifdef DEBUG_INGRESS
61         struct ingress_qdisc_data *p = PRIV(sch);
62 #endif
63
64         DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
65                 sch, p, new, old);
66         DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
67         return 1;
68 }
69
70
71 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
72 {
73         return NULL;
74 }
75
76
77 static unsigned long ingress_get(struct Qdisc *sch,u32 classid)
78 {
79 #ifdef DEBUG_INGRESS
80         struct ingress_qdisc_data *p = PRIV(sch);
81 #endif
82         DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
83         return TC_H_MIN(classid) + 1;
84 }
85
86
87 static unsigned long ingress_bind_filter(struct Qdisc *sch,
88     unsigned long parent, u32 classid)
89 {
90         return ingress_get(sch, classid);
91 }
92
93
94 static void ingress_put(struct Qdisc *sch, unsigned long cl)
95 {
96 }
97
98
99 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
100     struct rtattr **tca, unsigned long *arg)
101 {
102 #ifdef DEBUG_INGRESS
103         struct ingress_qdisc_data *p = PRIV(sch);
104 #endif
105         DPRINTK("ingress_change(sch %p,[qdisc %p],classid %x,parent %x),"
106                 "arg 0x%lx\n", sch, p, classid, parent, *arg);
107         DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
108         return 0;
109 }
110
111
112
113 static void ingress_walk(struct Qdisc *sch,struct qdisc_walker *walker)
114 {
115 #ifdef DEBUG_INGRESS
116         struct ingress_qdisc_data *p = PRIV(sch);
117 #endif
118         DPRINTK("ingress_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
119         DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
120 }
121
122
123 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch,unsigned long cl)
124 {
125         struct ingress_qdisc_data *p = PRIV(sch);
126
127         return &p->filter_list;
128 }
129
130
131 /* --------------------------- Qdisc operations ---------------------------- */
132
133
134 static int ingress_enqueue(struct sk_buff *skb,struct Qdisc *sch)
135 {
136         struct ingress_qdisc_data *p = PRIV(sch);
137         struct tcf_result res;
138         int result;
139
140         D2PRINTK("ingress_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
141         result = tc_classify(skb, p->filter_list, &res);
142         D2PRINTK("result %d class 0x%04x\n", result, res.classid);
143         /*
144          * Unlike normal "enqueue" functions, ingress_enqueue returns a
145          * firewall FW_* code.
146          */
147 #ifdef CONFIG_NET_CLS_ACT
148         sch->bstats.packets++;
149         sch->bstats.bytes += skb->len;
150         switch (result) {
151                 case TC_ACT_SHOT:
152                         result = TC_ACT_SHOT;
153                         sch->qstats.drops++;
154                         break;
155                 case TC_ACT_STOLEN:
156                 case TC_ACT_QUEUED:
157                         result = TC_ACT_STOLEN;
158                         break;
159                 case TC_ACT_RECLASSIFY:
160                 case TC_ACT_OK:
161                 case TC_ACT_UNSPEC:
162                 default:
163                         skb->tc_index = TC_H_MIN(res.classid);
164                         result = TC_ACT_OK;
165                         break;
166         }
167 /* backward compat */
168 #else
169 #ifdef  CONFIG_NET_CLS_POLICE
170         switch (result) {
171                 case TC_POLICE_SHOT:
172                 result = NF_DROP;
173                 sch->qstats.drops++;
174                 break;
175                 case TC_POLICE_RECLASSIFY: /* DSCP remarking here ? */
176                 case TC_POLICE_OK:
177                 case TC_POLICE_UNSPEC:
178                 default:
179                 sch->bstats.packets++;
180                 sch->bstats.bytes += skb->len;
181                 result = NF_ACCEPT;
182                 break;
183         }
184
185 #else
186         D2PRINTK("Overriding result to ACCEPT\n");
187         result = NF_ACCEPT;
188         sch->bstats.packets++;
189         sch->bstats.bytes += skb->len;
190 #endif
191 #endif
192
193         return result;
194 }
195
196
197 static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
198 {
199 /*
200         struct ingress_qdisc_data *p = PRIV(sch);
201         D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
202 */
203         return NULL;
204 }
205
206
207 static int ingress_requeue(struct sk_buff *skb,struct Qdisc *sch)
208 {
209 /*
210         struct ingress_qdisc_data *p = PRIV(sch);
211         D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
212 */
213         return 0;
214 }
215
216 static unsigned int ingress_drop(struct Qdisc *sch)
217 {
218 #ifdef DEBUG_INGRESS
219         struct ingress_qdisc_data *p = PRIV(sch);
220 #endif
221         DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch, p);
222         return 0;
223 }
224
225 #ifndef CONFIG_NET_CLS_ACT
226 #ifdef CONFIG_NETFILTER
227 static unsigned int
228 ing_hook(unsigned int hook, struct sk_buff **pskb,
229                              const struct net_device *indev,
230                              const struct net_device *outdev,
231                              int (*okfn)(struct sk_buff *))
232 {
233
234         struct Qdisc *q;
235         struct sk_buff *skb = *pskb;
236         struct net_device *dev = skb->dev;
237         int fwres=NF_ACCEPT;
238
239         DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
240                 skb->sk ? "(owned)" : "(unowned)",
241                 skb->dev ? (*pskb)->dev->name : "(no dev)",
242                 skb->len);
243
244         if (dev->qdisc_ingress) {
245                 spin_lock(&dev->ingress_lock);
246                 if ((q = dev->qdisc_ingress) != NULL)
247                         fwres = q->enqueue(skb, q);
248                 spin_unlock(&dev->ingress_lock);
249         }
250
251         return fwres;
252 }
253
254 /* after ipt_filter */
255 static struct nf_hook_ops ing_ops = {
256         .hook           = ing_hook,
257         .owner          = THIS_MODULE,
258         .pf             = PF_INET,
259         .hooknum        = NF_IP_PRE_ROUTING,
260         .priority       = NF_IP_PRI_FILTER + 1,
261 };
262
263 static struct nf_hook_ops ing6_ops = {
264         .hook           = ing_hook,
265         .owner          = THIS_MODULE,
266         .pf             = PF_INET6,
267         .hooknum        = NF_IP6_PRE_ROUTING,
268         .priority       = NF_IP6_PRI_FILTER + 1,
269 };
270
271 #endif
272 #endif
273
274 static int ingress_init(struct Qdisc *sch,struct rtattr *opt)
275 {
276         struct ingress_qdisc_data *p = PRIV(sch);
277
278 /* Make sure either netfilter or preferably CLS_ACT is
279 * compiled in */
280 #ifndef CONFIG_NET_CLS_ACT
281 #ifndef CONFIG_NETFILTER
282         printk("You MUST compile classifier actions into the kernel\n");
283         return -EINVAL;
284 #else
285         printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
286 #endif
287 #endif
288
289 #ifndef CONFIG_NET_CLS_ACT
290 #ifdef CONFIG_NETFILTER
291         if (!nf_registered) {
292                 if (nf_register_hook(&ing_ops) < 0) {
293                         printk("ingress qdisc registration error \n");
294                         return -EINVAL;
295                 }
296                 nf_registered++;
297
298                 if (nf_register_hook(&ing6_ops) < 0) {
299                         printk("IPv6 ingress qdisc registration error, " \
300                             "disabling IPv6 support.\n");
301                 } else
302                         nf_registered++;
303         }
304 #endif
305 #endif
306
307         DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
308         p->q = &noop_qdisc;
309         return 0;
310 }
311
312
313 static void ingress_reset(struct Qdisc *sch)
314 {
315         struct ingress_qdisc_data *p = PRIV(sch);
316
317         DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch, p);
318
319 /*
320 #if 0
321 */
322 /* for future use */
323         qdisc_reset(p->q);
324 /*
325 #endif
326 */
327 }
328
329 /* ------------------------------------------------------------- */
330
331
332 /* ------------------------------------------------------------- */
333
334 static void ingress_destroy(struct Qdisc *sch)
335 {
336         struct ingress_qdisc_data *p = PRIV(sch);
337
338         DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch, p);
339         tcf_destroy_chain(p->filter_list);
340 #if 0
341 /* for future use */
342         qdisc_destroy(p->q);
343 #endif
344 }
345
346
347 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
348 {
349         unsigned char *b = skb_tail_pointer(skb);
350         struct rtattr *rta;
351
352         rta = (struct rtattr *) b;
353         RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
354         rta->rta_len = skb_tail_pointer(skb) - b;
355         return skb->len;
356
357 rtattr_failure:
358         nlmsg_trim(skb, b);
359         return -1;
360 }
361
362 static struct Qdisc_class_ops ingress_class_ops = {
363         .graft          =       ingress_graft,
364         .leaf           =       ingress_leaf,
365         .get            =       ingress_get,
366         .put            =       ingress_put,
367         .change         =       ingress_change,
368         .delete         =       NULL,
369         .walk           =       ingress_walk,
370         .tcf_chain      =       ingress_find_tcf,
371         .bind_tcf       =       ingress_bind_filter,
372         .unbind_tcf     =       ingress_put,
373         .dump           =       NULL,
374 };
375
376 static struct Qdisc_ops ingress_qdisc_ops = {
377         .next           =       NULL,
378         .cl_ops         =       &ingress_class_ops,
379         .id             =       "ingress",
380         .priv_size      =       sizeof(struct ingress_qdisc_data),
381         .enqueue        =       ingress_enqueue,
382         .dequeue        =       ingress_dequeue,
383         .requeue        =       ingress_requeue,
384         .drop           =       ingress_drop,
385         .init           =       ingress_init,
386         .reset          =       ingress_reset,
387         .destroy        =       ingress_destroy,
388         .change         =       NULL,
389         .dump           =       ingress_dump,
390         .owner          =       THIS_MODULE,
391 };
392
393 static int __init ingress_module_init(void)
394 {
395         int ret = 0;
396
397         if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
398                 printk("Unable to register Ingress qdisc\n");
399                 return ret;
400         }
401
402         return ret;
403 }
404 static void __exit ingress_module_exit(void)
405 {
406         unregister_qdisc(&ingress_qdisc_ops);
407 #ifndef CONFIG_NET_CLS_ACT
408 #ifdef CONFIG_NETFILTER
409         if (nf_registered) {
410                 nf_unregister_hook(&ing_ops);
411                 if (nf_registered > 1)
412                         nf_unregister_hook(&ing6_ops);
413         }
414 #endif
415 #endif
416 }
417 module_init(ingress_module_init)
418 module_exit(ingress_module_exit)
419 MODULE_LICENSE("GPL");