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