blob: 4880d44ee978d0df85c90814096998ac4d3dc94d [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 */
Patrick McHardy58f4df42008-01-21 00:11:01 -080025#define DPRINTK(format, args...) printk(KERN_DEBUG format,##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#else
Patrick McHardy58f4df42008-01-21 00:11:01 -080027#define DPRINTK(format, args...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#endif
29
30#if 0 /* data */
Patrick McHardy58f4df42008-01-21 00:11:01 -080031#define D2PRINTK(format, args...) printk(KERN_DEBUG format,##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#else
Patrick McHardy58f4df42008-01-21 00:11:01 -080033#define D2PRINTK(format, args...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#endif
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define PRIV(sch) qdisc_priv(sch)
37
Patrick McHardy58f4df42008-01-21 00:11:01 -080038/* Thanks to Doron Oz for this hack */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#ifndef CONFIG_NET_CLS_ACT
40#ifdef CONFIG_NETFILTER
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090041static int nf_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif
43#endif
44
45struct ingress_qdisc_data {
46 struct Qdisc *q;
47 struct tcf_proto *filter_list;
48};
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* ------------------------- Class/flow operations ------------------------- */
51
Patrick McHardy58f4df42008-01-21 00:11:01 -080052static int ingress_graft(struct Qdisc *sch, unsigned long arg,
53 struct Qdisc *new, struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55#ifdef DEBUG_INGRESS
56 struct ingress_qdisc_data *p = PRIV(sch);
57#endif
58
59 DPRINTK("ingress_graft(sch %p,[qdisc %p],new %p,old %p)\n",
60 sch, p, new, old);
61 DPRINTK("\n ingress_graft: You cannot add qdiscs to classes");
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090062 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
66{
67 return NULL;
68}
69
Patrick McHardy58f4df42008-01-21 00:11:01 -080070static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72#ifdef DEBUG_INGRESS
73 struct ingress_qdisc_data *p = PRIV(sch);
74#endif
Patrick McHardy58f4df42008-01-21 00:11:01 -080075 DPRINTK("ingress_get(sch %p,[qdisc %p],classid %x)\n",
76 sch, p, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return TC_H_MIN(classid) + 1;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080081 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 return ingress_get(sch, classid);
84}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static void ingress_put(struct Qdisc *sch, unsigned long cl)
87{
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
Patrick McHardy58f4df42008-01-21 00:11:01 -080091 struct rtattr **tca, unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93#ifdef DEBUG_INGRESS
94 struct ingress_qdisc_data *p = PRIV(sch);
95#endif
96 DPRINTK("ingress_change(sch %p,[qdisc %p],classid %x,parent %x),"
97 "arg 0x%lx\n", sch, p, classid, parent, *arg);
98 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
99 return 0;
100}
101
Patrick McHardy58f4df42008-01-21 00:11:01 -0800102static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104#ifdef DEBUG_INGRESS
105 struct ingress_qdisc_data *p = PRIV(sch);
106#endif
107 DPRINTK("ingress_walk(sch %p,[qdisc %p],walker %p)\n", sch, p, walker);
108 DPRINTK("No effect. sch_ingress doesn't maintain classes at the moment");
109}
110
Patrick McHardy58f4df42008-01-21 00:11:01 -0800111static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct ingress_qdisc_data *p = PRIV(sch);
114
115 return &p->filter_list;
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* --------------------------- Qdisc operations ---------------------------- */
119
Patrick McHardy58f4df42008-01-21 00:11:01 -0800120static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct ingress_qdisc_data *p = PRIV(sch);
123 struct tcf_result res;
124 int result;
125
126 D2PRINTK("ingress_enqueue(skb %p,sch %p,[qdisc %p])\n", skb, sch, p);
127 result = tc_classify(skb, p->filter_list, &res);
128 D2PRINTK("result %d class 0x%04x\n", result, res.classid);
129 /*
130 * Unlike normal "enqueue" functions, ingress_enqueue returns a
131 * firewall FW_* code.
132 */
133#ifdef CONFIG_NET_CLS_ACT
134 sch->bstats.packets++;
135 sch->bstats.bytes += skb->len;
136 switch (result) {
Patrick McHardy58f4df42008-01-21 00:11:01 -0800137 case TC_ACT_SHOT:
138 result = TC_ACT_SHOT;
139 sch->qstats.drops++;
140 break;
141 case TC_ACT_STOLEN:
142 case TC_ACT_QUEUED:
143 result = TC_ACT_STOLEN;
144 break;
145 case TC_ACT_RECLASSIFY:
146 case TC_ACT_OK:
147 skb->tc_index = TC_H_MIN(res.classid);
148 default:
149 result = TC_ACT_OK;
150 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#else
153 D2PRINTK("Overriding result to ACCEPT\n");
154 result = NF_ACCEPT;
155 sch->bstats.packets++;
156 sch->bstats.bytes += skb->len;
157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return result;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
163{
164/*
165 struct ingress_qdisc_data *p = PRIV(sch);
166 D2PRINTK("ingress_dequeue(sch %p,[qdisc %p])\n",sch,PRIV(p));
167*/
168 return NULL;
169}
170
Patrick McHardy58f4df42008-01-21 00:11:01 -0800171static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173/*
174 struct ingress_qdisc_data *p = PRIV(sch);
175 D2PRINTK("ingress_requeue(skb %p,sch %p,[qdisc %p])\n",skb,sch,PRIV(p));
176*/
177 return 0;
178}
179
180static unsigned int ingress_drop(struct Qdisc *sch)
181{
182#ifdef DEBUG_INGRESS
183 struct ingress_qdisc_data *p = PRIV(sch);
184#endif
185 DPRINTK("ingress_drop(sch %p,[qdisc %p])\n", sch, p);
186 return 0;
187}
188
189#ifndef CONFIG_NET_CLS_ACT
190#ifdef CONFIG_NETFILTER
Patrick McHardy58f4df42008-01-21 00:11:01 -0800191static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900192 const struct net_device *indev,
193 const struct net_device *outdev,
194 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct Qdisc *q;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900198 struct net_device *dev = skb->dev;
Patrick McHardy58f4df42008-01-21 00:11:01 -0800199 int fwres = NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 DPRINTK("ing_hook: skb %s dev=%s len=%u\n",
202 skb->sk ? "(owned)" : "(unowned)",
Herbert Xu3db05fe2007-10-15 00:53:15 -0700203 skb->dev ? skb->dev->name : "(no dev)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 skb->len);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (dev->qdisc_ingress) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700207 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if ((q = dev->qdisc_ingress) != NULL)
209 fwres = q->enqueue(skb, q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700210 spin_unlock(&dev->ingress_lock);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900211 }
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return fwres;
214}
215
216/* after ipt_filter */
Patrick McHardy19994142007-12-05 01:23:00 -0800217static struct nf_hook_ops ing_ops[] __read_mostly = {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800218 {
219 .hook = ing_hook,
220 .owner = THIS_MODULE,
221 .pf = PF_INET,
222 .hooknum = NF_INET_PRE_ROUTING,
223 .priority = NF_IP_PRI_FILTER + 1,
224 },
225 {
226 .hook = ing_hook,
227 .owner = THIS_MODULE,
228 .pf = PF_INET6,
229 .hooknum = NF_INET_PRE_ROUTING,
230 .priority = NF_IP6_PRI_FILTER + 1,
231 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#endif
234#endif
235
Patrick McHardy58f4df42008-01-21 00:11:01 -0800236static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct ingress_qdisc_data *p = PRIV(sch);
239
Patrick McHardy58f4df42008-01-21 00:11:01 -0800240 /* Make sure either netfilter or preferably CLS_ACT is
241 * compiled in */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#ifndef CONFIG_NET_CLS_ACT
243#ifndef CONFIG_NETFILTER
244 printk("You MUST compile classifier actions into the kernel\n");
245 return -EINVAL;
246#else
247 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
248#endif
249#endif
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#ifndef CONFIG_NET_CLS_ACT
252#ifdef CONFIG_NETFILTER
253 if (!nf_registered) {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800254 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 printk("ingress qdisc registration error \n");
256 return -EINVAL;
257 }
258 nf_registered++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260#endif
261#endif
262
Patrick McHardy58f4df42008-01-21 00:11:01 -0800263 DPRINTK("ingress_init(sch %p,[qdisc %p],opt %p)\n", sch, p, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 p->q = &noop_qdisc;
265 return 0;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static void ingress_reset(struct Qdisc *sch)
269{
270 struct ingress_qdisc_data *p = PRIV(sch);
271
272 DPRINTK("ingress_reset(sch %p,[qdisc %p])\n", sch, p);
273
274/*
275#if 0
276*/
277/* for future use */
278 qdisc_reset(p->q);
279/*
280#endif
281*/
282}
283
284/* ------------------------------------------------------------- */
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static void ingress_destroy(struct Qdisc *sch)
287{
288 struct ingress_qdisc_data *p = PRIV(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 DPRINTK("ingress_destroy(sch %p,[qdisc %p])\n", sch, p);
Patrick McHardya48b5a62007-03-23 11:29:43 -0700291 tcf_destroy_chain(p->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#if 0
293/* for future use */
294 qdisc_destroy(p->q);
295#endif
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
299{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700300 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct rtattr *rta;
302
Patrick McHardy58f4df42008-01-21 00:11:01 -0800303 rta = (struct rtattr *)b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700305 rta->rta_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return skb->len;
307
308rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700309 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -1;
311}
312
Eric Dumazet20fea082007-11-14 01:44:41 -0800313static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 .graft = ingress_graft,
315 .leaf = ingress_leaf,
316 .get = ingress_get,
317 .put = ingress_put,
318 .change = ingress_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 .walk = ingress_walk,
320 .tcf_chain = ingress_find_tcf,
321 .bind_tcf = ingress_bind_filter,
322 .unbind_tcf = ingress_put,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324
Eric Dumazet20fea082007-11-14 01:44:41 -0800325static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .cl_ops = &ingress_class_ops,
327 .id = "ingress",
328 .priv_size = sizeof(struct ingress_qdisc_data),
329 .enqueue = ingress_enqueue,
330 .dequeue = ingress_dequeue,
331 .requeue = ingress_requeue,
332 .drop = ingress_drop,
333 .init = ingress_init,
334 .reset = ingress_reset,
335 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .dump = ingress_dump,
337 .owner = THIS_MODULE,
338};
339
340static int __init ingress_module_init(void)
341{
342 int ret = 0;
343
344 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
345 printk("Unable to register Ingress qdisc\n");
346 return ret;
347 }
348
349 return ret;
350}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800351
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900352static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 unregister_qdisc(&ingress_qdisc_ops);
355#ifndef CONFIG_NET_CLS_ACT
356#ifdef CONFIG_NETFILTER
Patrick McHardy41c5b312007-12-05 01:22:43 -0800357 if (nf_registered)
358 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#endif
360#endif
361}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363module_init(ingress_module_init)
364module_exit(ingress_module_exit)
365MODULE_LICENSE("GPL");