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/list.h>
13 #include <linux/skbuff.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 <net/netlink.h>
19 #include <net/pkt_sched.h>
22 /* Thanks to Doron Oz for this hack */
23 #ifndef CONFIG_NET_CLS_ACT
24 #ifdef CONFIG_NETFILTER
25 static int nf_registered;
29 struct ingress_qdisc_data {
31 struct tcf_proto *filter_list;
34 /* ------------------------- Class/flow operations ------------------------- */
36 static int ingress_graft(struct Qdisc *sch, unsigned long arg,
37 struct Qdisc *new, struct Qdisc **old)
42 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
47 static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
49 return TC_H_MIN(classid) + 1;
52 static unsigned long ingress_bind_filter(struct Qdisc *sch,
53 unsigned long parent, u32 classid)
55 return ingress_get(sch, classid);
58 static void ingress_put(struct Qdisc *sch, unsigned long cl)
62 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
63 struct rtattr **tca, unsigned long *arg)
68 static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
73 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
75 struct ingress_qdisc_data *p = qdisc_priv(sch);
77 return &p->filter_list;
80 /* --------------------------- Qdisc operations ---------------------------- */
82 static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
84 struct ingress_qdisc_data *p = qdisc_priv(sch);
85 struct tcf_result res;
88 result = tc_classify(skb, p->filter_list, &res);
91 * Unlike normal "enqueue" functions, ingress_enqueue returns a
94 #ifdef CONFIG_NET_CLS_ACT
95 sch->bstats.packets++;
96 sch->bstats.bytes += skb->len;
104 result = TC_ACT_STOLEN;
106 case TC_ACT_RECLASSIFY:
108 skb->tc_index = TC_H_MIN(res.classid);
115 sch->bstats.packets++;
116 sch->bstats.bytes += skb->len;
122 static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
127 static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch)
132 static unsigned int ingress_drop(struct Qdisc *sch)
137 #ifndef CONFIG_NET_CLS_ACT
138 #ifdef CONFIG_NETFILTER
139 static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
140 const struct net_device *indev,
141 const struct net_device *outdev,
142 int (*okfn)(struct sk_buff *))
146 struct net_device *dev = skb->dev;
147 int fwres = NF_ACCEPT;
149 if (dev->qdisc_ingress) {
150 spin_lock(&dev->ingress_lock);
151 if ((q = dev->qdisc_ingress) != NULL)
152 fwres = q->enqueue(skb, q);
153 spin_unlock(&dev->ingress_lock);
159 /* after ipt_filter */
160 static struct nf_hook_ops ing_ops[] __read_mostly = {
163 .owner = THIS_MODULE,
165 .hooknum = NF_INET_PRE_ROUTING,
166 .priority = NF_IP_PRI_FILTER + 1,
170 .owner = THIS_MODULE,
172 .hooknum = NF_INET_PRE_ROUTING,
173 .priority = NF_IP6_PRI_FILTER + 1,
179 static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
181 struct ingress_qdisc_data *p = qdisc_priv(sch);
183 /* Make sure either netfilter or preferably CLS_ACT is
185 #ifndef CONFIG_NET_CLS_ACT
186 #ifndef CONFIG_NETFILTER
187 printk("You MUST compile classifier actions into the kernel\n");
190 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
194 #ifndef CONFIG_NET_CLS_ACT
195 #ifdef CONFIG_NETFILTER
196 if (!nf_registered) {
197 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
198 printk("ingress qdisc registration error \n");
209 static void ingress_reset(struct Qdisc *sch)
214 /* ------------------------------------------------------------- */
216 static void ingress_destroy(struct Qdisc *sch)
218 struct ingress_qdisc_data *p = qdisc_priv(sch);
220 tcf_destroy_chain(p->filter_list);
223 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
225 unsigned char *b = skb_tail_pointer(skb);
228 rta = (struct rtattr *)b;
229 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
230 rta->rta_len = skb_tail_pointer(skb) - b;
238 static const struct Qdisc_class_ops ingress_class_ops = {
239 .graft = ingress_graft,
240 .leaf = ingress_leaf,
243 .change = ingress_change,
244 .walk = ingress_walk,
245 .tcf_chain = ingress_find_tcf,
246 .bind_tcf = ingress_bind_filter,
247 .unbind_tcf = ingress_put,
250 static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
251 .cl_ops = &ingress_class_ops,
253 .priv_size = sizeof(struct ingress_qdisc_data),
254 .enqueue = ingress_enqueue,
255 .dequeue = ingress_dequeue,
256 .requeue = ingress_requeue,
257 .drop = ingress_drop,
258 .init = ingress_init,
259 .reset = ingress_reset,
260 .destroy = ingress_destroy,
261 .dump = ingress_dump,
262 .owner = THIS_MODULE,
265 static int __init ingress_module_init(void)
269 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
270 printk("Unable to register Ingress qdisc\n");
277 static void __exit ingress_module_exit(void)
279 unregister_qdisc(&ingress_qdisc_ops);
280 #ifndef CONFIG_NET_CLS_ACT
281 #ifdef CONFIG_NETFILTER
283 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
288 module_init(ingress_module_init)
289 module_exit(ingress_module_exit)
290 MODULE_LICENSE("GPL");