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 #ifndef CONFIG_NET_CLS_ACT
166 #ifdef CONFIG_NETFILTER
167 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
169 if (!nf_registered
) {
170 if (nf_register_hooks(ing_ops
, ARRAY_SIZE(ing_ops
)) < 0) {
171 printk("ingress qdisc registration error \n");
181 /* ------------------------------------------------------------- */
183 static void ingress_destroy(struct Qdisc
*sch
)
185 struct ingress_qdisc_data
*p
= qdisc_priv(sch
);
187 tcf_destroy_chain(p
->filter_list
);
190 static int ingress_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
192 unsigned char *b
= skb_tail_pointer(skb
);
195 rta
= (struct rtattr
*)b
;
196 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
197 rta
->rta_len
= skb_tail_pointer(skb
) - b
;
205 static const struct Qdisc_class_ops ingress_class_ops
= {
206 .graft
= ingress_graft
,
207 .leaf
= ingress_leaf
,
210 .change
= ingress_change
,
211 .walk
= ingress_walk
,
212 .tcf_chain
= ingress_find_tcf
,
213 .bind_tcf
= ingress_bind_filter
,
214 .unbind_tcf
= ingress_put
,
217 static struct Qdisc_ops ingress_qdisc_ops __read_mostly
= {
218 .cl_ops
= &ingress_class_ops
,
220 .priv_size
= sizeof(struct ingress_qdisc_data
),
221 .enqueue
= ingress_enqueue
,
222 .init
= ingress_init
,
223 .destroy
= ingress_destroy
,
224 .dump
= ingress_dump
,
225 .owner
= THIS_MODULE
,
228 static int __init
ingress_module_init(void)
232 if ((ret
= register_qdisc(&ingress_qdisc_ops
)) < 0) {
233 printk("Unable to register Ingress qdisc\n");
240 static void __exit
ingress_module_exit(void)
242 unregister_qdisc(&ingress_qdisc_ops
);
243 #ifndef CONFIG_NET_CLS_ACT
244 #ifdef CONFIG_NETFILTER
246 nf_unregister_hooks(ing_ops
, ARRAY_SIZE(ing_ops
));
251 module_init(ingress_module_init
)
252 module_exit(ingress_module_exit
)
253 MODULE_LICENSE("GPL");