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/skbuff.h>
13 #include <linux/netdevice.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 <linux/smp.h>
19 #include <net/netlink.h>
20 #include <net/pkt_sched.h>
21 #include <asm/byteorder.h>
22 #include <asm/uaccess.h>
23 #include <linux/kmod.h>
24 #include <linux/stat.h>
25 #include <linux/interrupt.h>
26 #include <linux/list.h>
31 #ifdef DEBUG_INGRESS /* control */
32 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
34 #define DPRINTK(format,args...)
38 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
40 #define D2PRINTK(format,args...)
44 #define PRIV(sch) qdisc_priv(sch)
47 /* Thanks to Doron Oz for this hack
49 #ifndef CONFIG_NET_CLS_ACT
50 #ifdef CONFIG_NETFILTER
51 static int nf_registered
;
55 struct ingress_qdisc_data
{
57 struct tcf_proto
*filter_list
;
61 /* ------------------------- Class/flow operations ------------------------- */
64 static int ingress_graft(struct Qdisc
*sch
,unsigned long arg
,
65 struct Qdisc
*new,struct Qdisc
**old
)
68 struct ingress_qdisc_data
*p
= PRIV(sch
);
71 DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
73 DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
78 static struct Qdisc
*ingress_leaf(struct Qdisc
*sch
, unsigned long arg
)
84 static unsigned long ingress_get(struct Qdisc
*sch
,u32 classid
)
87 struct ingress_qdisc_data
*p
= PRIV(sch
);
89 DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch
, p
, classid
);
90 return TC_H_MIN(classid
) + 1;
94 static unsigned long ingress_bind_filter(struct Qdisc
*sch
,
95 unsigned long parent
, u32 classid
)
97 return ingress_get(sch
, classid
);
101 static void ingress_put(struct Qdisc
*sch
, unsigned long cl
)
106 static int ingress_change(struct Qdisc
*sch
, u32 classid
, u32 parent
,
107 struct rtattr
**tca
, unsigned long *arg
)
110 struct ingress_qdisc_data
*p
= PRIV(sch
);
112 DPRINTK("ingress_change(sch %p,[qdisc %p],classid %x,parent %x),"
113 "arg 0x%lx\n", sch
, p
, classid
, parent
, *arg
);
114 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
120 static void ingress_walk(struct Qdisc
*sch
,struct qdisc_walker
*walker
)
123 struct ingress_qdisc_data
*p
= PRIV(sch
);
125 DPRINTK("ingress_walk(sch %p,[qdisc %p],walker %p)\n", sch
, p
, walker
);
126 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
130 static struct tcf_proto
**ingress_find_tcf(struct Qdisc
*sch
,unsigned long cl
)
132 struct ingress_qdisc_data
*p
= PRIV(sch
);
134 return &p
->filter_list
;
138 /* --------------------------- Qdisc operations ---------------------------- */
141 static int ingress_enqueue(struct sk_buff
*skb
,struct Qdisc
*sch
)
143 struct ingress_qdisc_data
*p
= PRIV(sch
);
144 struct tcf_result res
;
147 D2PRINTK("ingress_enqueue(skb %p,sch %p,[qdisc %p])\n", skb
, sch
, p
);
148 result
= tc_classify(skb
, p
->filter_list
, &res
);
149 D2PRINTK("result %d class 0x%04x\n", result
, res
.classid
);
151 * Unlike normal "enqueue" functions, ingress_enqueue returns a
152 * firewall FW_* code.
154 #ifdef CONFIG_NET_CLS_ACT
155 sch
->bstats
.packets
++;
156 sch
->bstats
.bytes
+= skb
->len
;
159 result
= TC_ACT_SHOT
;
164 result
= TC_ACT_STOLEN
;
166 case TC_ACT_RECLASSIFY
:
170 skb
->tc_index
= TC_H_MIN(res
.classid
);
174 /* backward compat */
176 #ifdef CONFIG_NET_CLS_POLICE
182 case TC_POLICE_RECLASSIFY
: /* DSCP remarking here ? */
184 case TC_POLICE_UNSPEC
:
186 sch
->bstats
.packets
++;
187 sch
->bstats
.bytes
+= skb
->len
;
193 D2PRINTK("Overriding result to ACCEPT\n");
195 sch
->bstats
.packets
++;
196 sch
->bstats
.bytes
+= skb
->len
;
204 static struct sk_buff
*ingress_dequeue(struct Qdisc
*sch
)
207 struct ingress_qdisc_data *p = PRIV(sch);
208 D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
214 static int ingress_requeue(struct sk_buff
*skb
,struct Qdisc
*sch
)
217 struct ingress_qdisc_data *p = PRIV(sch);
218 D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
223 static unsigned int ingress_drop(struct Qdisc
*sch
)
226 struct ingress_qdisc_data
*p
= PRIV(sch
);
228 DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch
, p
);
232 #ifndef CONFIG_NET_CLS_ACT
233 #ifdef CONFIG_NETFILTER
235 ing_hook(unsigned int hook
, struct sk_buff
**pskb
,
236 const struct net_device
*indev
,
237 const struct net_device
*outdev
,
238 int (*okfn
)(struct sk_buff
*))
242 struct sk_buff
*skb
= *pskb
;
243 struct net_device
*dev
= skb
->dev
;
246 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
247 skb
->sk
? "(owned)" : "(unowned)",
248 skb
->dev
? (*pskb
)->dev
->name
: "(no dev)",
251 if (dev
->qdisc_ingress
) {
252 spin_lock(&dev
->ingress_lock
);
253 if ((q
= dev
->qdisc_ingress
) != NULL
)
254 fwres
= q
->enqueue(skb
, q
);
255 spin_unlock(&dev
->ingress_lock
);
261 /* after ipt_filter */
262 static struct nf_hook_ops ing_ops
= {
264 .owner
= THIS_MODULE
,
266 .hooknum
= NF_IP_PRE_ROUTING
,
267 .priority
= NF_IP_PRI_FILTER
+ 1,
270 static struct nf_hook_ops ing6_ops
= {
272 .owner
= THIS_MODULE
,
274 .hooknum
= NF_IP6_PRE_ROUTING
,
275 .priority
= NF_IP6_PRI_FILTER
+ 1,
281 static int ingress_init(struct Qdisc
*sch
,struct rtattr
*opt
)
283 struct ingress_qdisc_data
*p
= PRIV(sch
);
285 /* Make sure either netfilter or preferably CLS_ACT is
287 #ifndef CONFIG_NET_CLS_ACT
288 #ifndef CONFIG_NETFILTER
289 printk("You MUST compile classifier actions into the kernel\n");
292 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
296 #ifndef CONFIG_NET_CLS_ACT
297 #ifdef CONFIG_NETFILTER
298 if (!nf_registered
) {
299 if (nf_register_hook(&ing_ops
) < 0) {
300 printk("ingress qdisc registration error \n");
305 if (nf_register_hook(&ing6_ops
) < 0) {
306 printk("IPv6 ingress qdisc registration error, " \
307 "disabling IPv6 support.\n");
314 DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch
,p
,opt
);
320 static void ingress_reset(struct Qdisc
*sch
)
322 struct ingress_qdisc_data
*p
= PRIV(sch
);
324 DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch
, p
);
336 /* ------------------------------------------------------------- */
339 /* ------------------------------------------------------------- */
341 static void ingress_destroy(struct Qdisc
*sch
)
343 struct ingress_qdisc_data
*p
= PRIV(sch
);
345 DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch
, p
);
346 tcf_destroy_chain(p
->filter_list
);
354 static int ingress_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
356 unsigned char *b
= skb_tail_pointer(skb
);
359 rta
= (struct rtattr
*) b
;
360 RTA_PUT(skb
, TCA_OPTIONS
, 0, NULL
);
361 rta
->rta_len
= skb_tail_pointer(skb
) - b
;
369 static struct Qdisc_class_ops ingress_class_ops
= {
370 .graft
= ingress_graft
,
371 .leaf
= ingress_leaf
,
374 .change
= ingress_change
,
376 .walk
= ingress_walk
,
377 .tcf_chain
= ingress_find_tcf
,
378 .bind_tcf
= ingress_bind_filter
,
379 .unbind_tcf
= ingress_put
,
383 static struct Qdisc_ops ingress_qdisc_ops
= {
385 .cl_ops
= &ingress_class_ops
,
387 .priv_size
= sizeof(struct ingress_qdisc_data
),
388 .enqueue
= ingress_enqueue
,
389 .dequeue
= ingress_dequeue
,
390 .requeue
= ingress_requeue
,
391 .drop
= ingress_drop
,
392 .init
= ingress_init
,
393 .reset
= ingress_reset
,
394 .destroy
= ingress_destroy
,
396 .dump
= ingress_dump
,
397 .owner
= THIS_MODULE
,
400 static int __init
ingress_module_init(void)
404 if ((ret
= register_qdisc(&ingress_qdisc_ops
)) < 0) {
405 printk("Unable to register Ingress qdisc\n");
411 static void __exit
ingress_module_exit(void)
413 unregister_qdisc(&ingress_qdisc_ops
);
414 #ifndef CONFIG_NET_CLS_ACT
415 #ifdef CONFIG_NETFILTER
417 nf_unregister_hook(&ing_ops
);
418 if (nf_registered
> 1)
419 nf_unregister_hook(&ing6_ops
);
424 module_init(ingress_module_init
)
425 module_exit(ingress_module_exit
)
426 MODULE_LICENSE("GPL");