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 {
30 struct tcf_proto *filter_list;
33 /* ------------------------- Class/flow operations ------------------------- */
35 static int ingress_graft(struct Qdisc *sch, unsigned long arg,
36 struct Qdisc *new, struct Qdisc **old)
41 static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
46 static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
48 return TC_H_MIN(classid) + 1;
51 static unsigned long ingress_bind_filter(struct Qdisc *sch,
52 unsigned long parent, u32 classid)
54 return ingress_get(sch, classid);
57 static void ingress_put(struct Qdisc *sch, unsigned long cl)
61 static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
62 struct rtattr **tca, unsigned long *arg)
67 static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
72 static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
74 struct ingress_qdisc_data *p = qdisc_priv(sch);
76 return &p->filter_list;
79 /* --------------------------- Qdisc operations ---------------------------- */
81 static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
83 struct ingress_qdisc_data *p = qdisc_priv(sch);
84 struct tcf_result res;
87 result = tc_classify(skb, p->filter_list, &res);
90 * Unlike normal "enqueue" functions, ingress_enqueue returns a
93 #ifdef CONFIG_NET_CLS_ACT
94 sch->bstats.packets++;
95 sch->bstats.bytes += skb->len;
103 result = TC_ACT_STOLEN;
105 case TC_ACT_RECLASSIFY:
107 skb->tc_index = TC_H_MIN(res.classid);
114 sch->bstats.packets++;
115 sch->bstats.bytes += skb->len;
121 static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
126 static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch)
131 static unsigned int ingress_drop(struct Qdisc *sch)
136 #ifndef CONFIG_NET_CLS_ACT
137 #ifdef CONFIG_NETFILTER
138 static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
139 const struct net_device *indev,
140 const struct net_device *outdev,
141 int (*okfn)(struct sk_buff *))
145 struct net_device *dev = skb->dev;
146 int fwres = NF_ACCEPT;
148 if (dev->qdisc_ingress) {
149 spin_lock(&dev->ingress_lock);
150 if ((q = dev->qdisc_ingress) != NULL)
151 fwres = q->enqueue(skb, q);
152 spin_unlock(&dev->ingress_lock);
158 /* after ipt_filter */
159 static struct nf_hook_ops ing_ops[] __read_mostly = {
162 .owner = THIS_MODULE,
164 .hooknum = NF_INET_PRE_ROUTING,
165 .priority = NF_IP_PRI_FILTER + 1,
169 .owner = THIS_MODULE,
171 .hooknum = NF_INET_PRE_ROUTING,
172 .priority = NF_IP6_PRI_FILTER + 1,
178 static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
180 /* Make sure either netfilter or preferably CLS_ACT is
182 #ifndef CONFIG_NET_CLS_ACT
183 #ifndef CONFIG_NETFILTER
184 printk("You MUST compile classifier actions into the kernel\n");
187 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
191 #ifndef CONFIG_NET_CLS_ACT
192 #ifdef CONFIG_NETFILTER
193 if (!nf_registered) {
194 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
195 printk("ingress qdisc registration error \n");
205 static void ingress_reset(struct Qdisc *sch)
210 /* ------------------------------------------------------------- */
212 static void ingress_destroy(struct Qdisc *sch)
214 struct ingress_qdisc_data *p = qdisc_priv(sch);
216 tcf_destroy_chain(p->filter_list);
219 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
221 unsigned char *b = skb_tail_pointer(skb);
224 rta = (struct rtattr *)b;
225 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
226 rta->rta_len = skb_tail_pointer(skb) - b;
234 static const struct Qdisc_class_ops ingress_class_ops = {
235 .graft = ingress_graft,
236 .leaf = ingress_leaf,
239 .change = ingress_change,
240 .walk = ingress_walk,
241 .tcf_chain = ingress_find_tcf,
242 .bind_tcf = ingress_bind_filter,
243 .unbind_tcf = ingress_put,
246 static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
247 .cl_ops = &ingress_class_ops,
249 .priv_size = sizeof(struct ingress_qdisc_data),
250 .enqueue = ingress_enqueue,
251 .dequeue = ingress_dequeue,
252 .requeue = ingress_requeue,
253 .drop = ingress_drop,
254 .init = ingress_init,
255 .reset = ingress_reset,
256 .destroy = ingress_destroy,
257 .dump = ingress_dump,
258 .owner = THIS_MODULE,
261 static int __init ingress_module_init(void)
265 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
266 printk("Unable to register Ingress qdisc\n");
273 static void __exit ingress_module_exit(void)
275 unregister_qdisc(&ingress_qdisc_ops);
276 #ifndef CONFIG_NET_CLS_ACT
277 #ifdef CONFIG_NETFILTER
279 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
284 module_init(ingress_module_init)
285 module_exit(ingress_module_exit)
286 MODULE_LICENSE("GPL");