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 #ifndef CONFIG_NET_CLS_ACT
122 #ifdef CONFIG_NETFILTER
123 static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
124 const struct net_device *indev,
125 const struct net_device *outdev,
126 int (*okfn)(struct sk_buff *))
130 struct net_device *dev = skb->dev;
131 int fwres = NF_ACCEPT;
133 if (dev->qdisc_ingress) {
134 spin_lock(&dev->ingress_lock);
135 if ((q = dev->qdisc_ingress) != NULL)
136 fwres = q->enqueue(skb, q);
137 spin_unlock(&dev->ingress_lock);
143 /* after ipt_filter */
144 static struct nf_hook_ops ing_ops[] __read_mostly = {
147 .owner = THIS_MODULE,
149 .hooknum = NF_INET_PRE_ROUTING,
150 .priority = NF_IP_PRI_FILTER + 1,
154 .owner = THIS_MODULE,
156 .hooknum = NF_INET_PRE_ROUTING,
157 .priority = NF_IP6_PRI_FILTER + 1,
163 static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
165 /* Make sure either netfilter or preferably CLS_ACT is
167 #ifndef CONFIG_NET_CLS_ACT
168 #ifndef CONFIG_NETFILTER
169 printk("You MUST compile classifier actions into the kernel\n");
172 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
176 #ifndef CONFIG_NET_CLS_ACT
177 #ifdef CONFIG_NETFILTER
178 if (!nf_registered) {
179 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
180 printk("ingress qdisc registration error \n");
190 /* ------------------------------------------------------------- */
192 static void ingress_destroy(struct Qdisc *sch)
194 struct ingress_qdisc_data *p = qdisc_priv(sch);
196 tcf_destroy_chain(p->filter_list);
199 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
201 unsigned char *b = skb_tail_pointer(skb);
204 rta = (struct rtattr *)b;
205 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
206 rta->rta_len = skb_tail_pointer(skb) - b;
214 static const struct Qdisc_class_ops ingress_class_ops = {
215 .graft = ingress_graft,
216 .leaf = ingress_leaf,
219 .change = ingress_change,
220 .walk = ingress_walk,
221 .tcf_chain = ingress_find_tcf,
222 .bind_tcf = ingress_bind_filter,
223 .unbind_tcf = ingress_put,
226 static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
227 .cl_ops = &ingress_class_ops,
229 .priv_size = sizeof(struct ingress_qdisc_data),
230 .enqueue = ingress_enqueue,
231 .init = ingress_init,
232 .destroy = ingress_destroy,
233 .dump = ingress_dump,
234 .owner = THIS_MODULE,
237 static int __init ingress_module_init(void)
241 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
242 printk("Unable to register Ingress qdisc\n");
249 static void __exit ingress_module_exit(void)
251 unregister_qdisc(&ingress_qdisc_ops);
252 #ifndef CONFIG_NET_CLS_ACT
253 #ifdef CONFIG_NETFILTER
255 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
260 module_init(ingress_module_init)
261 module_exit(ingress_module_exit)
262 MODULE_LICENSE("GPL");