blob: bdda28d9ebf6669d10526ccd8a4c161b3e85dbc1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define PRIV(sch) qdisc_priv(sch)
23
Patrick McHardy58f4df42008-01-21 00:11:01 -080024/* Thanks to Doron Oz for this hack */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#ifndef CONFIG_NET_CLS_ACT
26#ifdef CONFIG_NETFILTER
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090027static int nf_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#endif
29#endif
30
31struct ingress_qdisc_data {
32 struct Qdisc *q;
33 struct tcf_proto *filter_list;
34};
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* ------------------------- Class/flow operations ------------------------- */
37
Patrick McHardy58f4df42008-01-21 00:11:01 -080038static int ingress_graft(struct Qdisc *sch, unsigned long arg,
39 struct Qdisc *new, struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090041 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
45{
46 return NULL;
47}
48
Patrick McHardy58f4df42008-01-21 00:11:01 -080049static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return TC_H_MIN(classid) + 1;
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080055 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 return ingress_get(sch, classid);
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void ingress_put(struct Qdisc *sch, unsigned long cl)
61{
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
Patrick McHardy58f4df42008-01-21 00:11:01 -080065 struct rtattr **tca, unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return 0;
68}
69
Patrick McHardy58f4df42008-01-21 00:11:01 -080070static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Patrick McHardya4781222008-01-21 00:11:21 -080072 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Patrick McHardy58f4df42008-01-21 00:11:01 -080075static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 struct ingress_qdisc_data *p = PRIV(sch);
78
79 return &p->filter_list;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* --------------------------- Qdisc operations ---------------------------- */
83
Patrick McHardy58f4df42008-01-21 00:11:01 -080084static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct ingress_qdisc_data *p = PRIV(sch);
87 struct tcf_result res;
88 int result;
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 result = tc_classify(skb, p->filter_list, &res);
Patrick McHardya4781222008-01-21 00:11:21 -080091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /*
93 * Unlike normal "enqueue" functions, ingress_enqueue returns a
94 * firewall FW_* code.
95 */
96#ifdef CONFIG_NET_CLS_ACT
97 sch->bstats.packets++;
98 sch->bstats.bytes += skb->len;
99 switch (result) {
Patrick McHardy58f4df42008-01-21 00:11:01 -0800100 case TC_ACT_SHOT:
101 result = TC_ACT_SHOT;
102 sch->qstats.drops++;
103 break;
104 case TC_ACT_STOLEN:
105 case TC_ACT_QUEUED:
106 result = TC_ACT_STOLEN;
107 break;
108 case TC_ACT_RECLASSIFY:
109 case TC_ACT_OK:
110 skb->tc_index = TC_H_MIN(res.classid);
111 default:
112 result = TC_ACT_OK;
113 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 result = NF_ACCEPT;
117 sch->bstats.packets++;
118 sch->bstats.bytes += skb->len;
119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 return result;
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
125{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return NULL;
127}
128
Patrick McHardy58f4df42008-01-21 00:11:01 -0800129static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132}
133
134static unsigned int ingress_drop(struct Qdisc *sch)
135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
137}
138
139#ifndef CONFIG_NET_CLS_ACT
140#ifdef CONFIG_NETFILTER
Patrick McHardy58f4df42008-01-21 00:11:01 -0800141static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900142 const struct net_device *indev,
143 const struct net_device *outdev,
144 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct Qdisc *q;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900148 struct net_device *dev = skb->dev;
Patrick McHardy58f4df42008-01-21 00:11:01 -0800149 int fwres = NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (dev->qdisc_ingress) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700152 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if ((q = dev->qdisc_ingress) != NULL)
154 fwres = q->enqueue(skb, q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700155 spin_unlock(&dev->ingress_lock);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900156 }
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return fwres;
159}
160
161/* after ipt_filter */
Patrick McHardy19994142007-12-05 01:23:00 -0800162static struct nf_hook_ops ing_ops[] __read_mostly = {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800163 {
164 .hook = ing_hook,
165 .owner = THIS_MODULE,
166 .pf = PF_INET,
167 .hooknum = NF_INET_PRE_ROUTING,
168 .priority = NF_IP_PRI_FILTER + 1,
169 },
170 {
171 .hook = ing_hook,
172 .owner = THIS_MODULE,
173 .pf = PF_INET6,
174 .hooknum = NF_INET_PRE_ROUTING,
175 .priority = NF_IP6_PRI_FILTER + 1,
176 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
179#endif
180
Patrick McHardy58f4df42008-01-21 00:11:01 -0800181static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct ingress_qdisc_data *p = PRIV(sch);
184
Patrick McHardy58f4df42008-01-21 00:11:01 -0800185 /* Make sure either netfilter or preferably CLS_ACT is
186 * compiled in */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#ifndef CONFIG_NET_CLS_ACT
188#ifndef CONFIG_NETFILTER
189 printk("You MUST compile classifier actions into the kernel\n");
190 return -EINVAL;
191#else
192 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
193#endif
194#endif
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#ifndef CONFIG_NET_CLS_ACT
197#ifdef CONFIG_NETFILTER
198 if (!nf_registered) {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800199 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 printk("ingress qdisc registration error \n");
201 return -EINVAL;
202 }
203 nf_registered++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205#endif
206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 p->q = &noop_qdisc;
208 return 0;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static void ingress_reset(struct Qdisc *sch)
212{
Patrick McHardya4781222008-01-21 00:11:21 -0800213 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/* ------------------------------------------------------------- */
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static void ingress_destroy(struct Qdisc *sch)
219{
220 struct ingress_qdisc_data *p = PRIV(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Patrick McHardya48b5a62007-03-23 11:29:43 -0700222 tcf_destroy_chain(p->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
226{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700227 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct rtattr *rta;
229
Patrick McHardy58f4df42008-01-21 00:11:01 -0800230 rta = (struct rtattr *)b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700232 rta->rta_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return skb->len;
234
235rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700236 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -1;
238}
239
Eric Dumazet20fea082007-11-14 01:44:41 -0800240static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .graft = ingress_graft,
242 .leaf = ingress_leaf,
243 .get = ingress_get,
244 .put = ingress_put,
245 .change = ingress_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .walk = ingress_walk,
247 .tcf_chain = ingress_find_tcf,
248 .bind_tcf = ingress_bind_filter,
249 .unbind_tcf = ingress_put,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Eric Dumazet20fea082007-11-14 01:44:41 -0800252static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .cl_ops = &ingress_class_ops,
254 .id = "ingress",
255 .priv_size = sizeof(struct ingress_qdisc_data),
256 .enqueue = ingress_enqueue,
257 .dequeue = ingress_dequeue,
258 .requeue = ingress_requeue,
259 .drop = ingress_drop,
260 .init = ingress_init,
261 .reset = ingress_reset,
262 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .dump = ingress_dump,
264 .owner = THIS_MODULE,
265};
266
267static int __init ingress_module_init(void)
268{
269 int ret = 0;
270
271 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
272 printk("Unable to register Ingress qdisc\n");
273 return ret;
274 }
275
276 return ret;
277}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800278
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900279static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unregister_qdisc(&ingress_qdisc_ops);
282#ifndef CONFIG_NET_CLS_ACT
283#ifdef CONFIG_NETFILTER
Patrick McHardy41c5b312007-12-05 01:22:43 -0800284 if (nf_registered)
285 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#endif
287#endif
288}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290module_init(ingress_module_init)
291module_exit(ingress_module_exit)
292MODULE_LICENSE("GPL");