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.
7 * Authors: Jamal Hadi Salim 1999
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/netdevice.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 <linux/smp.h>
19 #include <net/pkt_sched.h>
20 #include <asm/byteorder.h>
21 #include <asm/uaccess.h>
22 #include <linux/kmod.h>
23 #include <linux/stat.h>
24 #include <linux/interrupt.h>
25 #include <linux/list.h>
30 #ifdef DEBUG_INGRESS /* control */
31 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
33 #define DPRINTK(format,args...)
37 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
39 #define D2PRINTK(format,args...)
43 #define PRIV(sch) qdisc_priv(sch)
46 /* Thanks to Doron Oz for this hack
48 #ifndef CONFIG_NET_CLS_ACT
49 #ifdef CONFIG_NETFILTER
50 static int nf_registered;
54 struct ingress_qdisc_data {
56 struct tcf_proto *filter_list;
60 /* ------------------------- Class/flow operations ------------------------- */
63 static int ingress_graft(struct Qdisc *sch,unsigned long arg,
64 struct Qdisc *new,struct Qdisc **old)
67 struct ingress_qdisc_data *p = PRIV(sch);
70 DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
72 DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
77 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
83 static unsigned long ingress_get(struct Qdisc *sch,u32 classid)
86 struct ingress_qdisc_data *p = PRIV(sch);
88 DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
89 return TC_H_MIN(classid) + 1;
93 static unsigned long ingress_bind_filter(struct Qdisc *sch,
94 unsigned long parent, u32 classid)
96 return ingress_get(sch, classid);
100 static void ingress_put(struct Qdisc *sch, unsigned long cl)
105 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
106 struct rtattr **tca, unsigned long *arg)
109 struct ingress_qdisc_data *p = PRIV(sch);
111 DPRINTK("ingress_change(sch %p,[qdisc %p],classid %x,parent %x),"
112 "arg 0x%lx\n", sch, p, classid, parent, *arg);
113 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
119 static void ingress_walk(struct Qdisc *sch,struct qdisc_walker *walker)
122 struct ingress_qdisc_data *p = PRIV(sch);
124 DPRINTK("ingress_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
125 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
129 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch,unsigned long cl)
131 struct ingress_qdisc_data *p = PRIV(sch);
133 return &p->filter_list;
137 /* --------------------------- Qdisc operations ---------------------------- */
140 static int ingress_enqueue(struct sk_buff *skb,struct Qdisc *sch)
142 struct ingress_qdisc_data *p = PRIV(sch);
143 struct tcf_result res;
146 D2PRINTK("ingress_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
147 result = tc_classify(skb, p->filter_list, &res);
148 D2PRINTK("result %d class 0x%04x\n", result, res.classid);
150 * Unlike normal "enqueue" functions, ingress_enqueue returns a
151 * firewall FW_* code.
153 #ifdef CONFIG_NET_CLS_ACT
154 sch->bstats.packets++;
155 sch->bstats.bytes += skb->len;
158 result = TC_ACT_SHOT;
163 result = TC_ACT_STOLEN;
165 case TC_ACT_RECLASSIFY:
169 skb->tc_index = TC_H_MIN(res.classid);
173 /* backward compat */
175 #ifdef CONFIG_NET_CLS_POLICE
181 case TC_POLICE_RECLASSIFY: /* DSCP remarking here ? */
183 case TC_POLICE_UNSPEC:
185 sch->bstats.packets++;
186 sch->bstats.bytes += skb->len;
192 D2PRINTK("Overriding result to ACCEPT\n");
194 sch->bstats.packets++;
195 sch->bstats.bytes += skb->len;
203 static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
206 struct ingress_qdisc_data *p = PRIV(sch);
207 D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
213 static int ingress_requeue(struct sk_buff *skb,struct Qdisc *sch)
216 struct ingress_qdisc_data *p = PRIV(sch);
217 D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
222 static unsigned int ingress_drop(struct Qdisc *sch)
225 struct ingress_qdisc_data *p = PRIV(sch);
227 DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch, p);
231 #ifndef CONFIG_NET_CLS_ACT
232 #ifdef CONFIG_NETFILTER
234 ing_hook(unsigned int hook, struct sk_buff **pskb,
235 const struct net_device *indev,
236 const struct net_device *outdev,
237 int (*okfn)(struct sk_buff *))
241 struct sk_buff *skb = *pskb;
242 struct net_device *dev = skb->dev;
245 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
246 skb->sk ? "(owned)" : "(unowned)",
247 skb->dev ? (*pskb)->dev->name : "(no dev)",
251 revisit later: Use a private since lock dev->queue_lock is also
252 used on the egress (might slow things for an iota)
255 if (dev->qdisc_ingress) {
256 spin_lock(&dev->queue_lock);
257 if ((q = dev->qdisc_ingress) != NULL)
258 fwres = q->enqueue(skb, q);
259 spin_unlock(&dev->queue_lock);
265 /* after ipt_filter */
266 static struct nf_hook_ops ing_ops = {
268 .owner = THIS_MODULE,
270 .hooknum = NF_IP_PRE_ROUTING,
271 .priority = NF_IP_PRI_FILTER + 1,
274 static struct nf_hook_ops ing6_ops = {
276 .owner = THIS_MODULE,
278 .hooknum = NF_IP6_PRE_ROUTING,
279 .priority = NF_IP6_PRI_FILTER + 1,
285 static int ingress_init(struct Qdisc *sch,struct rtattr *opt)
287 struct ingress_qdisc_data *p = PRIV(sch);
289 /* Make sure either netfilter or preferably CLS_ACT is
291 #ifndef CONFIG_NET_CLS_ACT
292 #ifndef CONFIG_NETFILTER
293 printk("You MUST compile classifier actions into the kernel\n");
296 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
300 #ifndef CONFIG_NET_CLS_ACT
301 #ifdef CONFIG_NETFILTER
302 if (!nf_registered) {
303 if (nf_register_hook(&ing_ops) < 0) {
304 printk("ingress qdisc registration error \n");
309 if (nf_register_hook(&ing6_ops) < 0) {
310 printk("IPv6 ingress qdisc registration error, " \
311 "disabling IPv6 support.\n");
318 DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
324 static void ingress_reset(struct Qdisc *sch)
326 struct ingress_qdisc_data *p = PRIV(sch);
328 DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch, p);
340 /* ------------------------------------------------------------- */
343 /* ------------------------------------------------------------- */
345 static void ingress_destroy(struct Qdisc *sch)
347 struct ingress_qdisc_data *p = PRIV(sch);
348 struct tcf_proto *tp;
350 DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch, p);
351 while (p->filter_list) {
353 p->filter_list = tp->next;
363 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
365 unsigned char *b = skb->tail;
368 rta = (struct rtattr *) b;
369 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
370 rta->rta_len = skb->tail - b;
374 skb_trim(skb, b - skb->data);
378 static struct Qdisc_class_ops ingress_class_ops = {
379 .graft = ingress_graft,
380 .leaf = ingress_leaf,
383 .change = ingress_change,
385 .walk = ingress_walk,
386 .tcf_chain = ingress_find_tcf,
387 .bind_tcf = ingress_bind_filter,
388 .unbind_tcf = ingress_put,
392 static struct Qdisc_ops ingress_qdisc_ops = {
394 .cl_ops = &ingress_class_ops,
396 .priv_size = sizeof(struct ingress_qdisc_data),
397 .enqueue = ingress_enqueue,
398 .dequeue = ingress_dequeue,
399 .requeue = ingress_requeue,
400 .drop = ingress_drop,
401 .init = ingress_init,
402 .reset = ingress_reset,
403 .destroy = ingress_destroy,
405 .dump = ingress_dump,
406 .owner = THIS_MODULE,
409 static int __init ingress_module_init(void)
413 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
414 printk("Unable to register Ingress qdisc\n");
420 static void __exit ingress_module_exit(void)
422 unregister_qdisc(&ingress_qdisc_ops);
423 #ifndef CONFIG_NET_CLS_ACT
424 #ifdef CONFIG_NETFILTER
426 nf_unregister_hook(&ing_ops);
427 if (nf_registered > 1)
428 nf_unregister_hook(&ing6_ops);
433 module_init(ingress_module_init)
434 module_exit(ingress_module_exit)
435 MODULE_LICENSE("GPL");