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_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>
31 #ifdef DEBUG_INGRESS /* control */
32 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
34 #define DPRINTK(format,args...)
38 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
40 #define D2PRINTK(format,args...)
44 #define PRIV(sch) ((struct ingress_qdisc_data *) (sch)->data)
47 /* Thanks to Doron Oz for this hack
49 static int nf_registered = 0;
51 struct ingress_qdisc_data {
53 struct tcf_proto *filter_list;
57 /* ------------------------- Class/flow operations ------------------------- */
60 static int ingress_graft(struct Qdisc *sch,unsigned long arg,
61 struct Qdisc *new,struct Qdisc **old)
64 struct ingress_qdisc_data *p = PRIV(sch);
67 DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
69 DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
74 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
80 static unsigned long ingress_get(struct Qdisc *sch,u32 classid)
83 struct ingress_qdisc_data *p = PRIV(sch);
85 DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
86 return TC_H_MIN(classid) + 1;
90 static unsigned long ingress_bind_filter(struct Qdisc *sch,
91 unsigned long parent, u32 classid)
93 return ingress_get(sch, classid);
97 static void ingress_put(struct Qdisc *sch, unsigned long cl)
102 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
103 struct rtattr **tca, unsigned long *arg)
106 struct ingress_qdisc_data *p = PRIV(sch);
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");
116 static void ingress_walk(struct Qdisc *sch,struct qdisc_walker *walker)
119 struct ingress_qdisc_data *p = PRIV(sch);
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");
126 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch,unsigned long cl)
128 struct ingress_qdisc_data *p = PRIV(sch);
130 return &p->filter_list;
134 /* --------------------------- Qdisc operations ---------------------------- */
137 static int ingress_enqueue(struct sk_buff *skb,struct Qdisc *sch)
139 struct ingress_qdisc_data *p = PRIV(sch);
140 struct tcf_result res;
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);
147 * Unlike normal "enqueue" functions, ingress_enqueue returns a
148 * firewall FW_* code.
150 #ifdef CONFIG_NET_CLS_POLICE
156 case TC_POLICE_RECLASSIFY: /* DSCP remarking here ? */
158 case TC_POLICE_UNSPEC:
160 sch->stats.packets++;
161 sch->stats.bytes += skb->len;
166 sch->stats.packets++;
167 sch->stats.bytes += skb->len;
170 skb->tc_index = TC_H_MIN(res.classid);
175 static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
178 struct ingress_qdisc_data *p = PRIV(sch);
179 D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
185 static int ingress_requeue(struct sk_buff *skb,struct Qdisc *sch)
188 struct ingress_qdisc_data *p = PRIV(sch);
189 D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
194 static unsigned int ingress_drop(struct Qdisc *sch)
197 struct ingress_qdisc_data *p = PRIV(sch);
199 DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch, p);
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 *))
211 struct sk_buff *skb = *pskb;
212 struct net_device *dev = skb->dev;
215 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
216 skb->sk ? "(owned)" : "(unowned)",
217 skb->dev ? (*pskb)->dev->name : "(no dev)",
221 revisit later: Use a private since lock dev->queue_lock is also
222 used on the egress (might slow things for an iota)
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);
235 /* after ipt_filter */
236 static struct nf_hook_ops ing_ops =
245 static struct nf_hook_ops ing6_ops =
251 NF_IP6_PRI_FILTER + 1
254 int ingress_init(struct Qdisc *sch,struct rtattr *opt)
256 struct ingress_qdisc_data *p = PRIV(sch);
258 if (!nf_registered) {
259 if (nf_register_hook(&ing_ops) < 0) {
260 printk("ingress qdisc registration error \n");
264 if (nf_register_hook(&ing6_ops) < 0) {
265 printk("IPv6 ingress qdisc registration error, " \
266 "disabling IPv6 support.\n");
271 DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
280 static void ingress_reset(struct Qdisc *sch)
282 struct ingress_qdisc_data *p = PRIV(sch);
284 DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch, p);
296 /* ------------------------------------------------------------- */
299 /* ------------------------------------------------------------- */
301 static void ingress_destroy(struct Qdisc *sch)
303 struct ingress_qdisc_data *p = PRIV(sch);
304 struct tcf_proto *tp;
306 DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch, p);
307 while (p->filter_list) {
309 p->filter_list = tp->next;
322 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
324 unsigned char *b = skb->tail;
327 rta = (struct rtattr *) b;
328 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
329 rta->rta_len = skb->tail - b;
333 skb_trim(skb, b - skb->data);
337 static struct Qdisc_class_ops ingress_class_ops =
339 ingress_graft, /* graft */
340 ingress_leaf, /* leaf */
341 ingress_get, /* get */
342 ingress_put, /* put */
343 ingress_change, /* change */
345 ingress_walk, /* walk */
347 ingress_find_tcf, /* tcf_chain */
348 ingress_bind_filter, /* bind_tcf */
349 ingress_put, /* unbind_tcf */
354 struct Qdisc_ops ingress_qdisc_ops =
357 &ingress_class_ops, /* cl_ops */
359 sizeof(struct ingress_qdisc_data),
361 ingress_enqueue, /* enqueue */
362 ingress_dequeue, /* dequeue */
363 ingress_requeue, /* requeue */
364 ingress_drop, /* drop */
366 ingress_init, /* init */
367 ingress_reset, /* reset */
368 ingress_destroy, /* destroy */
371 ingress_dump, /* dump */
376 int init_module(void)
380 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
381 printk("Unable to register Ingress qdisc\n");
389 void cleanup_module(void)
391 unregister_qdisc(&ingress_qdisc_ops);
393 nf_unregister_hook(&ing_ops);
394 if (nf_registered > 1)
395 nf_unregister_hook(&ing6_ops);
399 MODULE_LICENSE("GPL");