blob: cb8ba8b1ad39ac37168816e3fe8372a4d06150ed [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
Patrick McHardy58f4df42008-01-21 00:11:01 -080022/* Thanks to Doron Oz for this hack */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#ifndef CONFIG_NET_CLS_ACT
24#ifdef CONFIG_NETFILTER
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090025static int nf_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#endif
27#endif
28
29struct ingress_qdisc_data {
30 struct Qdisc *q;
31 struct tcf_proto *filter_list;
32};
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* ------------------------- Class/flow operations ------------------------- */
35
Patrick McHardy58f4df42008-01-21 00:11:01 -080036static int ingress_graft(struct Qdisc *sch, unsigned long arg,
37 struct Qdisc *new, struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090039 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
43{
44 return NULL;
45}
46
Patrick McHardy58f4df42008-01-21 00:11:01 -080047static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return TC_H_MIN(classid) + 1;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080053 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 return ingress_get(sch, classid);
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void ingress_put(struct Qdisc *sch, unsigned long cl)
59{
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
Patrick McHardy58f4df42008-01-21 00:11:01 -080063 struct rtattr **tca, unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
66}
67
Patrick McHardy58f4df42008-01-21 00:11:01 -080068static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Patrick McHardya4781222008-01-21 00:11:21 -080070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Patrick McHardy58f4df42008-01-21 00:11:01 -080073static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Patrick McHardycb53c042008-01-21 00:11:48 -080075 struct ingress_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 return &p->filter_list;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* --------------------------- Qdisc operations ---------------------------- */
81
Patrick McHardy58f4df42008-01-21 00:11:01 -080082static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Patrick McHardycb53c042008-01-21 00:11:48 -080084 struct ingress_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct tcf_result res;
86 int result;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 result = tc_classify(skb, p->filter_list, &res);
Patrick McHardya4781222008-01-21 00:11:21 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /*
91 * Unlike normal "enqueue" functions, ingress_enqueue returns a
92 * firewall FW_* code.
93 */
94#ifdef CONFIG_NET_CLS_ACT
95 sch->bstats.packets++;
96 sch->bstats.bytes += skb->len;
97 switch (result) {
Patrick McHardy58f4df42008-01-21 00:11:01 -080098 case TC_ACT_SHOT:
99 result = TC_ACT_SHOT;
100 sch->qstats.drops++;
101 break;
102 case TC_ACT_STOLEN:
103 case TC_ACT_QUEUED:
104 result = TC_ACT_STOLEN;
105 break;
106 case TC_ACT_RECLASSIFY:
107 case TC_ACT_OK:
108 skb->tc_index = TC_H_MIN(res.classid);
109 default:
110 result = TC_ACT_OK;
111 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 result = NF_ACCEPT;
115 sch->bstats.packets++;
116 sch->bstats.bytes += skb->len;
117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 return result;
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
123{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return NULL;
125}
126
Patrick McHardy58f4df42008-01-21 00:11:01 -0800127static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130}
131
132static unsigned int ingress_drop(struct Qdisc *sch)
133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 0;
135}
136
137#ifndef CONFIG_NET_CLS_ACT
138#ifdef CONFIG_NETFILTER
Patrick McHardy58f4df42008-01-21 00:11:01 -0800139static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900140 const struct net_device *indev,
141 const struct net_device *outdev,
142 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct Qdisc *q;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900146 struct net_device *dev = skb->dev;
Patrick McHardy58f4df42008-01-21 00:11:01 -0800147 int fwres = NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (dev->qdisc_ingress) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700150 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if ((q = dev->qdisc_ingress) != NULL)
152 fwres = q->enqueue(skb, q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700153 spin_unlock(&dev->ingress_lock);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900154 }
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return fwres;
157}
158
159/* after ipt_filter */
Patrick McHardy19994142007-12-05 01:23:00 -0800160static struct nf_hook_ops ing_ops[] __read_mostly = {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800161 {
162 .hook = ing_hook,
163 .owner = THIS_MODULE,
164 .pf = PF_INET,
165 .hooknum = NF_INET_PRE_ROUTING,
166 .priority = NF_IP_PRI_FILTER + 1,
167 },
168 {
169 .hook = ing_hook,
170 .owner = THIS_MODULE,
171 .pf = PF_INET6,
172 .hooknum = NF_INET_PRE_ROUTING,
173 .priority = NF_IP6_PRI_FILTER + 1,
174 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#endif
177#endif
178
Patrick McHardy58f4df42008-01-21 00:11:01 -0800179static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Patrick McHardycb53c042008-01-21 00:11:48 -0800181 struct ingress_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Patrick McHardy58f4df42008-01-21 00:11:01 -0800183 /* Make sure either netfilter or preferably CLS_ACT is
184 * compiled in */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#ifndef CONFIG_NET_CLS_ACT
186#ifndef CONFIG_NETFILTER
187 printk("You MUST compile classifier actions into the kernel\n");
188 return -EINVAL;
189#else
190 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
191#endif
192#endif
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#ifndef CONFIG_NET_CLS_ACT
195#ifdef CONFIG_NETFILTER
196 if (!nf_registered) {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800197 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 printk("ingress qdisc registration error \n");
199 return -EINVAL;
200 }
201 nf_registered++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203#endif
204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 p->q = &noop_qdisc;
206 return 0;
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static void ingress_reset(struct Qdisc *sch)
210{
Patrick McHardya4781222008-01-21 00:11:21 -0800211 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/* ------------------------------------------------------------- */
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void ingress_destroy(struct Qdisc *sch)
217{
Patrick McHardycb53c042008-01-21 00:11:48 -0800218 struct ingress_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Patrick McHardya48b5a62007-03-23 11:29:43 -0700220 tcf_destroy_chain(p->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
224{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700225 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct rtattr *rta;
227
Patrick McHardy58f4df42008-01-21 00:11:01 -0800228 rta = (struct rtattr *)b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700230 rta->rta_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return skb->len;
232
233rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700234 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -1;
236}
237
Eric Dumazet20fea082007-11-14 01:44:41 -0800238static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .graft = ingress_graft,
240 .leaf = ingress_leaf,
241 .get = ingress_get,
242 .put = ingress_put,
243 .change = ingress_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .walk = ingress_walk,
245 .tcf_chain = ingress_find_tcf,
246 .bind_tcf = ingress_bind_filter,
247 .unbind_tcf = ingress_put,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Eric Dumazet20fea082007-11-14 01:44:41 -0800250static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .cl_ops = &ingress_class_ops,
252 .id = "ingress",
253 .priv_size = sizeof(struct ingress_qdisc_data),
254 .enqueue = ingress_enqueue,
255 .dequeue = ingress_dequeue,
256 .requeue = ingress_requeue,
257 .drop = ingress_drop,
258 .init = ingress_init,
259 .reset = ingress_reset,
260 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .dump = ingress_dump,
262 .owner = THIS_MODULE,
263};
264
265static int __init ingress_module_init(void)
266{
267 int ret = 0;
268
269 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
270 printk("Unable to register Ingress qdisc\n");
271 return ret;
272 }
273
274 return ret;
275}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800276
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900277static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 unregister_qdisc(&ingress_qdisc_ops);
280#ifndef CONFIG_NET_CLS_ACT
281#ifdef CONFIG_NETFILTER
Patrick McHardy41c5b312007-12-05 01:22:43 -0800282 if (nf_registered)
283 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
285#endif
286}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288module_init(ingress_module_init)
289module_exit(ingress_module_exit)
290MODULE_LICENSE("GPL");