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 #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
24 static int nf_registered;
27 struct ingress_qdisc_data {
28 struct tcf_proto *filter_list;
31 /* ------------------------- Class/flow operations ------------------------- */
33 static int ingress_graft(struct Qdisc *sch, unsigned long arg,
34 struct Qdisc *new, struct Qdisc **old)
39 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
44 static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
46 return TC_H_MIN(classid) + 1;
49 static unsigned long ingress_bind_filter(struct Qdisc *sch,
50 unsigned long parent, u32 classid)
52 return ingress_get(sch, classid);
55 static void ingress_put(struct Qdisc *sch, unsigned long cl)
59 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
60 struct nlattr **tca, unsigned long *arg)
65 static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
70 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
72 struct ingress_qdisc_data *p = qdisc_priv(sch);
74 return &p->filter_list;
77 /* --------------------------- Qdisc operations ---------------------------- */
79 static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
81 struct ingress_qdisc_data *p = qdisc_priv(sch);
82 struct tcf_result res;
85 result = tc_classify(skb, p->filter_list, &res);
88 * Unlike normal "enqueue" functions, ingress_enqueue returns a
91 #ifdef CONFIG_NET_CLS_ACT
92 sch->bstats.packets++;
93 sch->bstats.bytes += skb->len;
101 result = TC_ACT_STOLEN;
103 case TC_ACT_RECLASSIFY:
105 skb->tc_index = TC_H_MIN(res.classid);
112 sch->bstats.packets++;
113 sch->bstats.bytes += skb->len;
119 #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
120 static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
121 const struct net_device *indev,
122 const struct net_device *outdev,
123 int (*okfn)(struct sk_buff *))
127 struct net_device *dev = skb->dev;
128 int fwres = NF_ACCEPT;
130 if (dev->qdisc_ingress) {
131 spin_lock(&dev->ingress_lock);
132 if ((q = dev->qdisc_ingress) != NULL)
133 fwres = q->enqueue(skb, q);
134 spin_unlock(&dev->ingress_lock);
140 /* after ipt_filter */
141 static struct nf_hook_ops ing_ops[] __read_mostly = {
144 .owner = THIS_MODULE,
146 .hooknum = NF_INET_PRE_ROUTING,
147 .priority = NF_IP_PRI_FILTER + 1,
151 .owner = THIS_MODULE,
153 .hooknum = NF_INET_PRE_ROUTING,
154 .priority = NF_IP6_PRI_FILTER + 1,
159 static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
161 #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
162 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
164 if (!nf_registered) {
165 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
166 printk("ingress qdisc registration error \n");
175 /* ------------------------------------------------------------- */
177 static void ingress_destroy(struct Qdisc *sch)
179 struct ingress_qdisc_data *p = qdisc_priv(sch);
181 tcf_destroy_chain(p->filter_list);
184 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
188 nest = nla_nest_start(skb, TCA_OPTIONS);
190 goto nla_put_failure;
191 nla_nest_end(skb, nest);
195 nla_nest_cancel(skb, nest);
199 static const struct Qdisc_class_ops ingress_class_ops = {
200 .graft = ingress_graft,
201 .leaf = ingress_leaf,
204 .change = ingress_change,
205 .walk = ingress_walk,
206 .tcf_chain = ingress_find_tcf,
207 .bind_tcf = ingress_bind_filter,
208 .unbind_tcf = ingress_put,
211 static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
212 .cl_ops = &ingress_class_ops,
214 .priv_size = sizeof(struct ingress_qdisc_data),
215 .enqueue = ingress_enqueue,
216 .init = ingress_init,
217 .destroy = ingress_destroy,
218 .dump = ingress_dump,
219 .owner = THIS_MODULE,
222 static int __init ingress_module_init(void)
224 return register_qdisc(&ingress_qdisc_ops);
227 static void __exit ingress_module_exit(void)
229 unregister_qdisc(&ingress_qdisc_ops);
230 #if !defined(CONFIG_NET_CLS_ACT) && defined(CONFIG_NETFILTER)
232 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
236 module_init(ingress_module_init)
237 module_exit(ingress_module_exit)
238 MODULE_LICENSE("GPL");