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>
24 #ifdef DEBUG_INGRESS /* control */
25 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
27 #define DPRINTK(format,args...)
31 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
33 #define D2PRINTK(format,args...)
37 #define PRIV(sch) qdisc_priv(sch)
40 /* Thanks to Doron Oz for this hack
42 #ifndef CONFIG_NET_CLS_ACT
43 #ifdef CONFIG_NETFILTER
44 static int nf_registered
;
48 struct ingress_qdisc_data
{
50 struct tcf_proto
*filter_list
;
54 /* ------------------------- Class/flow operations ------------------------- */
57 static int ingress_graft(struct Qdisc
*sch
,unsigned long arg
,
58 struct Qdisc
*new,struct Qdisc
**old
)
61 struct ingress_qdisc_data
*p
= PRIV(sch
);
64 DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
66 DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
71 static struct Qdisc
*ingress_leaf(struct Qdisc
*sch
, unsigned long arg
)
77 static unsigned long ingress_get(struct Qdisc
*sch
,u32 classid
)
80 struct ingress_qdisc_data
*p
= PRIV(sch
);
82 DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch
, p
, classid
);
83 return TC_H_MIN(classid
) + 1;
87 static unsigned long ingress_bind_filter(struct Qdisc
*sch
,
88 unsigned long parent
, u32 classid
)
90 return ingress_get(sch
, classid
);
94 static void ingress_put(struct Qdisc
*sch
, unsigned long cl
)
99 static int ingress_change(struct Qdisc
*sch
, u32 classid
, u32 parent
,
100 struct rtattr
**tca
, unsigned long *arg
)
103 struct ingress_qdisc_data
*p
= PRIV(sch
);
105 DPRINTK("ingress_change(sch %p,[qdisc %p],classid %x,parent %x),"
106 "arg 0x%lx\n", sch
, p
, classid
, parent
, *arg
);
107 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
113 static void ingress_walk(struct Qdisc
*sch
,struct qdisc_walker
*walker
)
116 struct ingress_qdisc_data
*p
= PRIV(sch
);
118 DPRINTK("ingress_walk(sch %p,[qdisc %p],walker %p)\n", sch
, p
, walker
);
119 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
123 static struct tcf_proto
**ingress_find_tcf(struct Qdisc
*sch
,unsigned long cl
)
125 struct ingress_qdisc_data
*p
= PRIV(sch
);
127 return &p
->filter_list
;
131 /* --------------------------- Qdisc operations ---------------------------- */
134 static int ingress_enqueue(struct sk_buff
*skb
,struct Qdisc
*sch
)
136 struct ingress_qdisc_data
*p
= PRIV(sch
);
137 struct tcf_result res
;
140 D2PRINTK("ingress_enqueue(skb %p,sch %p,[qdisc %p])\n", skb
, sch
, p
);
141 result
= tc_classify(skb
, p
->filter_list
, &res
);
142 D2PRINTK("result %d class 0x%04x\n", result
, res
.classid
);
144 * Unlike normal "enqueue" functions, ingress_enqueue returns a
145 * firewall FW_* code.
147 #ifdef CONFIG_NET_CLS_ACT
148 sch
->bstats
.packets
++;
149 sch
->bstats
.bytes
+= skb
->len
;
152 result
= TC_ACT_SHOT
;
157 result
= TC_ACT_STOLEN
;
159 case TC_ACT_RECLASSIFY
:
163 skb
->tc_index
= TC_H_MIN(res
.classid
);
168 D2PRINTK("Overriding result to ACCEPT\n");
170 sch
->bstats
.packets
++;
171 sch
->bstats
.bytes
+= skb
->len
;
178 static struct sk_buff
*ingress_dequeue(struct Qdisc
*sch
)
181 struct ingress_qdisc_data *p = PRIV(sch);
182 D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
188 static int ingress_requeue(struct sk_buff
*skb
,struct Qdisc
*sch
)
191 struct ingress_qdisc_data *p = PRIV(sch);
192 D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
197 static unsigned int ingress_drop(struct Qdisc
*sch
)
200 struct ingress_qdisc_data
*p
= PRIV(sch
);
202 DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch
, p
);
206 #ifndef CONFIG_NET_CLS_ACT
207 #ifdef CONFIG_NETFILTER
209 ing_hook(unsigned int hook
, struct sk_buff
**pskb
,
210 const struct net_device
*indev
,
211 const struct net_device
*outdev
,
212 int (*okfn
)(struct sk_buff
*))
216 struct sk_buff
*skb
= *pskb
;
217 struct net_device
*dev
= skb
->dev
;
220 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
221 skb
->sk
? "(owned)" : "(unowned)",
222 skb
->dev
? (*pskb
)->dev
->name
: "(no dev)",
225 if (dev
->qdisc_ingress
) {
226 spin_lock(&dev
->ingress_lock
);
227 if ((q
= dev
->qdisc_ingress
) != NULL
)
228 fwres
= q
->enqueue(skb
, q
);
229 spin_unlock(&dev
->ingress_lock
);
235 /* after ipt_filter */
236 static struct nf_hook_ops ing_ops
= {
238 .owner
= THIS_MODULE
,
240 .hooknum
= NF_IP_PRE_ROUTING
,
241 .priority
= NF_IP_PRI_FILTER
+ 1,
244 static struct nf_hook_ops ing6_ops
= {
246 .owner
= THIS_MODULE
,
248 .hooknum
= NF_IP6_PRE_ROUTING
,
249 .priority
= NF_IP6_PRI_FILTER
+ 1,
255 static int ingress_init(struct Qdisc
*sch
,struct rtattr
*opt
)
257 struct ingress_qdisc_data
*p
= PRIV(sch
);
259 /* Make sure either netfilter or preferably CLS_ACT is
261 #ifndef CONFIG_NET_CLS_ACT
262 #ifndef CONFIG_NETFILTER
263 printk("You MUST compile classifier actions into the kernel\n");
266 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
270 #ifndef CONFIG_NET_CLS_ACT
271 #ifdef CONFIG_NETFILTER
272 if (!nf_registered
) {
273 if (nf_register_hook(&ing_ops
) < 0) {
274 printk("ingress qdisc registration error \n");
279 if (nf_register_hook(&ing6_ops
) < 0) {
280 printk("IPv6 ingress qdisc registration error, " \
281 "disabling IPv6 support.\n");
288 DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch
,p
,opt
);
294 static void ingress_reset(struct Qdisc
*sch
)
296 struct ingress_qdisc_data
*p
= PRIV(sch
);
298 DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch
, p
);
310 /* ------------------------------------------------------------- */
313 /* ------------------------------------------------------------- */
315 static void ingress_destroy(struct Qdisc
*sch
)
317 struct ingress_qdisc_data
*p
= PRIV(sch
);
319 DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch
, p
);
320 tcf_destroy_chain(p
->filter_list
);
328 static int ingress_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
330 unsigned char *b
= skb_tail_pointer(skb
);
333 rta
= (struct rtattr
*) b
;
334 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
335 rta
->rta_len
= skb_tail_pointer(skb
) - b
;
343 static struct Qdisc_class_ops ingress_class_ops
= {
344 .graft
= ingress_graft
,
345 .leaf
= ingress_leaf
,
348 .change
= ingress_change
,
350 .walk
= ingress_walk
,
351 .tcf_chain
= ingress_find_tcf
,
352 .bind_tcf
= ingress_bind_filter
,
353 .unbind_tcf
= ingress_put
,
357 static struct Qdisc_ops ingress_qdisc_ops
= {
359 .cl_ops
= &ingress_class_ops
,
361 .priv_size
= sizeof(struct ingress_qdisc_data
),
362 .enqueue
= ingress_enqueue
,
363 .dequeue
= ingress_dequeue
,
364 .requeue
= ingress_requeue
,
365 .drop
= ingress_drop
,
366 .init
= ingress_init
,
367 .reset
= ingress_reset
,
368 .destroy
= ingress_destroy
,
370 .dump
= ingress_dump
,
371 .owner
= THIS_MODULE
,
374 static int __init
ingress_module_init(void)
378 if ((ret
= register_qdisc(&ingress_qdisc_ops
)) < 0) {
379 printk("Unable to register Ingress qdisc\n");
385 static void __exit
ingress_module_exit(void)
387 unregister_qdisc(&ingress_qdisc_ops
);
388 #ifndef CONFIG_NET_CLS_ACT
389 #ifdef CONFIG_NETFILTER
391 nf_unregister_hook(&ing_ops
);
392 if (nf_registered
> 1)
393 nf_unregister_hook(&ing6_ops
);
398 module_init(ingress_module_init
)
399 module_exit(ingress_module_exit
)
400 MODULE_LICENSE("GPL");