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