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/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>
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) ((struct ingress_qdisc_data *) (sch)->data)
46 /* Thanks to Doron Oz for this hack
48 static int nf_registered = 0;
50 struct ingress_qdisc_data {
52 struct tcf_proto *filter_list;
56 /* ------------------------- Class/flow operations ------------------------- */
59 static int ingress_graft(struct Qdisc *sch,unsigned long arg,
60 struct Qdisc *new,struct Qdisc **old)
63 struct ingress_qdisc_data *p = PRIV(sch);
66 DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
68 DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
73 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
79 static unsigned long ingress_get(struct Qdisc *sch,u32 classid)
82 struct ingress_qdisc_data *p = PRIV(sch);
84 DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
85 return TC_H_MIN(classid) + 1;
89 static unsigned long ingress_bind_filter(struct Qdisc *sch,
90 unsigned long parent, u32 classid)
92 return ingress_get(sch, classid);
96 static void ingress_put(struct Qdisc *sch, unsigned long cl)
101 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
102 struct rtattr **tca, unsigned long *arg)
105 struct ingress_qdisc_data *p = PRIV(sch);
107 DPRINTK("ingress_change(sch %p,[qdisc %p],classid %x,parent %x),"
108 "arg 0x%lx\n", sch, p, classid, parent, *arg);
109 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
115 static void ingress_walk(struct Qdisc *sch,struct qdisc_walker *walker)
118 struct ingress_qdisc_data *p = PRIV(sch);
120 DPRINTK("ingress_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
121 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
125 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch,unsigned long cl)
127 struct ingress_qdisc_data *p = PRIV(sch);
129 return &p->filter_list;
133 /* --------------------------- Qdisc operations ---------------------------- */
136 static int ingress_enqueue(struct sk_buff *skb,struct Qdisc *sch)
138 struct ingress_qdisc_data *p = PRIV(sch);
139 struct tcf_result res;
142 D2PRINTK("ingress_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
143 result = tc_classify(skb, p->filter_list, &res);
144 D2PRINTK("result %d class 0x%04x\n", result, res.classid);
146 * Unlike normal "enqueue" functions, ingress_enqueue returns a
147 * firewall FW_* code.
149 #ifdef CONFIG_NET_CLS_POLICE
155 case TC_POLICE_RECLASSIFY: /* DSCP remarking here ? */
157 case TC_POLICE_UNSPEC:
159 sch->stats.packets++;
160 sch->stats.bytes += skb->len;
165 sch->stats.packets++;
166 sch->stats.bytes += skb->len;
169 skb->tc_index = TC_H_MIN(res.classid);
174 static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
177 struct ingress_qdisc_data *p = PRIV(sch);
178 D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
184 static int ingress_requeue(struct sk_buff *skb,struct Qdisc *sch)
187 struct ingress_qdisc_data *p = PRIV(sch);
188 D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
193 static int ingress_drop(struct Qdisc *sch)
196 struct ingress_qdisc_data *p = PRIV(sch);
198 DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch, p);
203 ing_hook(unsigned int hook, struct sk_buff **pskb,
204 const struct net_device *indev,
205 const struct net_device *outdev,
206 int (*okfn)(struct sk_buff *))
210 struct sk_buff *skb = *pskb;
211 struct net_device *dev = skb->dev;
214 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
215 skb->sk ? "(owned)" : "(unowned)",
216 skb->dev ? (*pskb)->dev->name : "(no dev)",
220 revisit later: Use a private since lock dev->queue_lock is also
221 used on the egress (might slow things for an iota)
224 if (dev->qdisc_ingress) {
225 spin_lock(&dev->queue_lock);
226 if ((q = dev->qdisc_ingress) != NULL)
227 fwres = q->enqueue(skb, q);
228 spin_unlock(&dev->queue_lock);
234 /* after ipt_filter */
235 static struct nf_hook_ops ing_ops =
244 int ingress_init(struct Qdisc *sch,struct rtattr *opt)
246 struct ingress_qdisc_data *p = PRIV(sch);
248 if (!nf_registered) {
249 if (nf_register_hook(&ing_ops) < 0) {
250 printk("ingress qdisc registration error \n");
256 DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
257 memset(p, 0, sizeof(*p));
258 p->filter_list = NULL;
267 static void ingress_reset(struct Qdisc *sch)
269 struct ingress_qdisc_data *p = PRIV(sch);
271 DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch, p);
283 /* ------------------------------------------------------------- */
286 /* ------------------------------------------------------------- */
288 static void ingress_destroy(struct Qdisc *sch)
290 struct ingress_qdisc_data *p = PRIV(sch);
291 struct tcf_proto *tp;
293 DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch, p);
294 while (p->filter_list) {
296 p->filter_list = tp->next;
297 tp->ops->destroy(tp);
299 memset(p, 0, sizeof(*p));
300 p->filter_list = NULL;
312 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
314 unsigned char *b = skb->tail;
317 rta = (struct rtattr *) b;
318 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
319 rta->rta_len = skb->tail - b;
323 skb_trim(skb, b - skb->data);
327 static struct Qdisc_class_ops ingress_class_ops =
329 ingress_graft, /* graft */
330 ingress_leaf, /* leaf */
331 ingress_get, /* get */
332 ingress_put, /* put */
333 ingress_change, /* change */
335 ingress_walk, /* walk */
337 ingress_find_tcf, /* tcf_chain */
338 ingress_bind_filter, /* bind_tcf */
339 ingress_put, /* unbind_tcf */
344 struct Qdisc_ops ingress_qdisc_ops =
347 &ingress_class_ops, /* cl_ops */
349 sizeof(struct ingress_qdisc_data),
351 ingress_enqueue, /* enqueue */
352 ingress_dequeue, /* dequeue */
353 ingress_requeue, /* requeue */
354 ingress_drop, /* drop */
356 ingress_init, /* init */
357 ingress_reset, /* reset */
358 ingress_destroy, /* destroy */
361 ingress_dump, /* dump */
366 int init_module(void)
370 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
371 printk("Unable to register Ingress qdisc\n");
379 void cleanup_module(void)
381 unregister_qdisc(&ingress_qdisc_ops);
383 nf_unregister_hook(&ing_ops);
386 MODULE_LICENSE("GPL");