blob: 2d32fd27496ecf8011999827f0450958d0e76486 [file] [log] [blame]
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001/* net/sched/sch_ingress.c - Ingress qdisc
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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.
6 *
7 * Authors: Jamal Hadi Salim 1999
8 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/types.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070012#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/rtnetlink.h>
15#include <linux/netfilter_ipv4.h>
16#include <linux/netfilter_ipv6.h>
17#include <linux/netfilter.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070018#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21
22#undef DEBUG_INGRESS
23
24#ifdef DEBUG_INGRESS /* control */
25#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
26#else
27#define DPRINTK(format,args...)
28#endif
29
30#if 0 /* data */
31#define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
32#else
33#define D2PRINTK(format,args...)
34#endif
35
36
37#define PRIV(sch) qdisc_priv(sch)
38
39
40/* Thanks to Doron Oz for this hack
41*/
42#ifndef CONFIG_NET_CLS_ACT
43#ifdef CONFIG_NETFILTER
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090044static int nf_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#endif
46#endif
47
48struct ingress_qdisc_data {
49 struct Qdisc *q;
50 struct tcf_proto *filter_list;
51};
52
53
54/* ------------------------- Class/flow operations ------------------------- */
55
56
57static int ingress_graft(struct Qdisc *sch,unsigned long arg,
58 struct Qdisc *new,struct Qdisc **old)
59{
60#ifdef DEBUG_INGRESS
61 struct ingress_qdisc_data *p = PRIV(sch);
62#endif
63
64 DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
65 sch, p, new, old);
66 DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090067 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70
71static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
72{
73 return NULL;
74}
75
76
77static unsigned long ingress_get(struct Qdisc *sch,u32 classid)
78{
79#ifdef DEBUG_INGRESS
80 struct ingress_qdisc_data *p = PRIV(sch);
81#endif
82 DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n", sch, p, classid);
83 return TC_H_MIN(classid) + 1;
84}
85
86
87static unsigned long ingress_bind_filter(struct Qdisc *sch,
88 unsigned long parent, u32 classid)
89{
90 return ingress_get(sch, classid);
91}
92
93
94static void ingress_put(struct Qdisc *sch, unsigned long cl)
95{
96}
97
98
99static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
100 struct rtattr **tca, unsigned long *arg)
101{
102#ifdef DEBUG_INGRESS
103 struct ingress_qdisc_data *p = PRIV(sch);
104#endif
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");
108 return 0;
109}
110
111
112
113static void ingress_walk(struct Qdisc *sch,struct qdisc_walker *walker)
114{
115#ifdef DEBUG_INGRESS
116 struct ingress_qdisc_data *p = PRIV(sch);
117#endif
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");
120}
121
122
123static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch,unsigned long cl)
124{
125 struct ingress_qdisc_data *p = PRIV(sch);
126
127 return &p->filter_list;
128}
129
130
131/* --------------------------- Qdisc operations ---------------------------- */
132
133
134static int ingress_enqueue(struct sk_buff *skb,struct Qdisc *sch)
135{
136 struct ingress_qdisc_data *p = PRIV(sch);
137 struct tcf_result res;
138 int result;
139
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);
143 /*
144 * Unlike normal "enqueue" functions, ingress_enqueue returns a
145 * firewall FW_* code.
146 */
147#ifdef CONFIG_NET_CLS_ACT
148 sch->bstats.packets++;
149 sch->bstats.bytes += skb->len;
150 switch (result) {
151 case TC_ACT_SHOT:
152 result = TC_ACT_SHOT;
153 sch->qstats.drops++;
154 break;
155 case TC_ACT_STOLEN:
156 case TC_ACT_QUEUED:
157 result = TC_ACT_STOLEN;
158 break;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900159 case TC_ACT_RECLASSIFY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 case TC_ACT_OK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 skb->tc_index = TC_H_MIN(res.classid);
Patrick McHardybdba91e2007-07-30 17:07:14 -0700162 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 result = TC_ACT_OK;
164 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#else
167 D2PRINTK("Overriding result to ACCEPT\n");
168 result = NF_ACCEPT;
169 sch->bstats.packets++;
170 sch->bstats.bytes += skb->len;
171#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return result;
174}
175
176
177static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
178{
179/*
180 struct ingress_qdisc_data *p = PRIV(sch);
181 D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
182*/
183 return NULL;
184}
185
186
187static int ingress_requeue(struct sk_buff *skb,struct Qdisc *sch)
188{
189/*
190 struct ingress_qdisc_data *p = PRIV(sch);
191 D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
192*/
193 return 0;
194}
195
196static unsigned int ingress_drop(struct Qdisc *sch)
197{
198#ifdef DEBUG_INGRESS
199 struct ingress_qdisc_data *p = PRIV(sch);
200#endif
201 DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch, p);
202 return 0;
203}
204
205#ifndef CONFIG_NET_CLS_ACT
206#ifdef CONFIG_NETFILTER
207static unsigned int
208ing_hook(unsigned int hook, struct sk_buff **pskb,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900209 const struct net_device *indev,
210 const struct net_device *outdev,
211 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct Qdisc *q;
215 struct sk_buff *skb = *pskb;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900216 struct net_device *dev = skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int fwres=NF_ACCEPT;
218
219 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
220 skb->sk ? "(owned)" : "(unowned)",
221 skb->dev ? (*pskb)->dev->name : "(no dev)",
222 skb->len);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (dev->qdisc_ingress) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700225 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if ((q = dev->qdisc_ingress) != NULL)
227 fwres = q->enqueue(skb, q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700228 spin_unlock(&dev->ingress_lock);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900229 }
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return fwres;
232}
233
234/* after ipt_filter */
235static struct nf_hook_ops ing_ops = {
236 .hook = ing_hook,
237 .owner = THIS_MODULE,
238 .pf = PF_INET,
239 .hooknum = NF_IP_PRE_ROUTING,
240 .priority = NF_IP_PRI_FILTER + 1,
241};
242
243static struct nf_hook_ops ing6_ops = {
244 .hook = ing_hook,
245 .owner = THIS_MODULE,
246 .pf = PF_INET6,
247 .hooknum = NF_IP6_PRE_ROUTING,
248 .priority = NF_IP6_PRI_FILTER + 1,
249};
250
251#endif
252#endif
253
254static int ingress_init(struct Qdisc *sch,struct rtattr *opt)
255{
256 struct ingress_qdisc_data *p = PRIV(sch);
257
258/* Make sure either netfilter or preferably CLS_ACT is
259* compiled in */
260#ifndef CONFIG_NET_CLS_ACT
261#ifndef CONFIG_NETFILTER
262 printk("You MUST compile classifier actions into the kernel\n");
263 return -EINVAL;
264#else
265 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
266#endif
267#endif
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#ifndef CONFIG_NET_CLS_ACT
270#ifdef CONFIG_NETFILTER
271 if (!nf_registered) {
272 if (nf_register_hook(&ing_ops) < 0) {
273 printk("ingress qdisc registration error \n");
274 return -EINVAL;
275 }
276 nf_registered++;
277
278 if (nf_register_hook(&ing6_ops) < 0) {
279 printk("IPv6 ingress qdisc registration error, " \
280 "disabling IPv6 support.\n");
281 } else
282 nf_registered++;
283 }
284#endif
285#endif
286
287 DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n",sch,p,opt);
288 p->q = &noop_qdisc;
289 return 0;
290}
291
292
293static void ingress_reset(struct Qdisc *sch)
294{
295 struct ingress_qdisc_data *p = PRIV(sch);
296
297 DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch, p);
298
299/*
300#if 0
301*/
302/* for future use */
303 qdisc_reset(p->q);
304/*
305#endif
306*/
307}
308
309/* ------------------------------------------------------------- */
310
311
312/* ------------------------------------------------------------- */
313
314static void ingress_destroy(struct Qdisc *sch)
315{
316 struct ingress_qdisc_data *p = PRIV(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch, p);
Patrick McHardya48b5a62007-03-23 11:29:43 -0700319 tcf_destroy_chain(p->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#if 0
321/* for future use */
322 qdisc_destroy(p->q);
323#endif
324}
325
326
327static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
328{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700329 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct rtattr *rta;
331
332 rta = (struct rtattr *) b;
333 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700334 rta->rta_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return skb->len;
336
337rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700338 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -1;
340}
341
342static struct Qdisc_class_ops ingress_class_ops = {
343 .graft = ingress_graft,
344 .leaf = ingress_leaf,
345 .get = ingress_get,
346 .put = ingress_put,
347 .change = ingress_change,
348 .delete = NULL,
349 .walk = ingress_walk,
350 .tcf_chain = ingress_find_tcf,
351 .bind_tcf = ingress_bind_filter,
352 .unbind_tcf = ingress_put,
353 .dump = NULL,
354};
355
356static struct Qdisc_ops ingress_qdisc_ops = {
357 .next = NULL,
358 .cl_ops = &ingress_class_ops,
359 .id = "ingress",
360 .priv_size = sizeof(struct ingress_qdisc_data),
361 .enqueue = ingress_enqueue,
362 .dequeue = ingress_dequeue,
363 .requeue = ingress_requeue,
364 .drop = ingress_drop,
365 .init = ingress_init,
366 .reset = ingress_reset,
367 .destroy = ingress_destroy,
368 .change = NULL,
369 .dump = ingress_dump,
370 .owner = THIS_MODULE,
371};
372
373static int __init ingress_module_init(void)
374{
375 int ret = 0;
376
377 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
378 printk("Unable to register Ingress qdisc\n");
379 return ret;
380 }
381
382 return ret;
383}
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900384static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 unregister_qdisc(&ingress_qdisc_ops);
387#ifndef CONFIG_NET_CLS_ACT
388#ifdef CONFIG_NETFILTER
389 if (nf_registered) {
390 nf_unregister_hook(&ing_ops);
391 if (nf_registered > 1)
392 nf_unregister_hook(&ing6_ops);
393 }
394#endif
395#endif
396}
397module_init(ingress_module_init)
398module_exit(ingress_module_exit)
399MODULE_LICENSE("GPL");