blob: 1bbc648f06ee087d37e50b6f64500e9d088ce6f7 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct tcf_proto *filter_list;
31};
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* ------------------------- Class/flow operations ------------------------- */
34
Patrick McHardy58f4df42008-01-21 00:11:01 -080035static int ingress_graft(struct Qdisc *sch, unsigned long arg,
36 struct Qdisc *new, struct Qdisc **old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Patrick McHardye0378342008-01-21 00:12:32 -080038 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
42{
43 return NULL;
44}
45
Patrick McHardy58f4df42008-01-21 00:11:01 -080046static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return TC_H_MIN(classid) + 1;
49}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static unsigned long ingress_bind_filter(struct Qdisc *sch,
Patrick McHardy58f4df42008-01-21 00:11:01 -080052 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 return ingress_get(sch, classid);
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static void ingress_put(struct Qdisc *sch, unsigned long cl)
58{
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent,
Patrick McHardy58f4df42008-01-21 00:11:01 -080062 struct rtattr **tca, unsigned long *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
65}
66
Patrick McHardy58f4df42008-01-21 00:11:01 -080067static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Patrick McHardya4781222008-01-21 00:11:21 -080069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Patrick McHardy58f4df42008-01-21 00:11:01 -080072static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Patrick McHardycb53c042008-01-21 00:11:48 -080074 struct ingress_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return &p->filter_list;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* --------------------------- Qdisc operations ---------------------------- */
80
Patrick McHardy58f4df42008-01-21 00:11:01 -080081static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Patrick McHardycb53c042008-01-21 00:11:48 -080083 struct ingress_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct tcf_result res;
85 int result;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 result = tc_classify(skb, p->filter_list, &res);
Patrick McHardya4781222008-01-21 00:11:21 -080088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /*
90 * Unlike normal "enqueue" functions, ingress_enqueue returns a
91 * firewall FW_* code.
92 */
93#ifdef CONFIG_NET_CLS_ACT
94 sch->bstats.packets++;
95 sch->bstats.bytes += skb->len;
96 switch (result) {
Patrick McHardy58f4df42008-01-21 00:11:01 -080097 case TC_ACT_SHOT:
98 result = TC_ACT_SHOT;
99 sch->qstats.drops++;
100 break;
101 case TC_ACT_STOLEN:
102 case TC_ACT_QUEUED:
103 result = TC_ACT_STOLEN;
104 break;
105 case TC_ACT_RECLASSIFY:
106 case TC_ACT_OK:
107 skb->tc_index = TC_H_MIN(res.classid);
108 default:
109 result = TC_ACT_OK;
110 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 result = NF_ACCEPT;
114 sch->bstats.packets++;
115 sch->bstats.bytes += skb->len;
116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return result;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct sk_buff *ingress_dequeue(struct Qdisc *sch)
122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return NULL;
124}
125
Patrick McHardy58f4df42008-01-21 00:11:01 -0800126static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return 0;
129}
130
131static unsigned int ingress_drop(struct Qdisc *sch)
132{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
134}
135
136#ifndef CONFIG_NET_CLS_ACT
137#ifdef CONFIG_NETFILTER
Patrick McHardy58f4df42008-01-21 00:11:01 -0800138static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900139 const struct net_device *indev,
140 const struct net_device *outdev,
141 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct Qdisc *q;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900145 struct net_device *dev = skb->dev;
Patrick McHardy58f4df42008-01-21 00:11:01 -0800146 int fwres = NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (dev->qdisc_ingress) {
Patrick McHardyfd44de72007-04-16 17:07:08 -0700149 spin_lock(&dev->ingress_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if ((q = dev->qdisc_ingress) != NULL)
151 fwres = q->enqueue(skb, q);
Patrick McHardyfd44de72007-04-16 17:07:08 -0700152 spin_unlock(&dev->ingress_lock);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900153 }
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return fwres;
156}
157
158/* after ipt_filter */
Patrick McHardy19994142007-12-05 01:23:00 -0800159static struct nf_hook_ops ing_ops[] __read_mostly = {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800160 {
161 .hook = ing_hook,
162 .owner = THIS_MODULE,
163 .pf = PF_INET,
164 .hooknum = NF_INET_PRE_ROUTING,
165 .priority = NF_IP_PRI_FILTER + 1,
166 },
167 {
168 .hook = ing_hook,
169 .owner = THIS_MODULE,
170 .pf = PF_INET6,
171 .hooknum = NF_INET_PRE_ROUTING,
172 .priority = NF_IP6_PRI_FILTER + 1,
173 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#endif
176#endif
177
Patrick McHardy58f4df42008-01-21 00:11:01 -0800178static int ingress_init(struct Qdisc *sch, struct rtattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Patrick McHardy58f4df42008-01-21 00:11:01 -0800180 /* Make sure either netfilter or preferably CLS_ACT is
181 * compiled in */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#ifndef CONFIG_NET_CLS_ACT
183#ifndef CONFIG_NETFILTER
184 printk("You MUST compile classifier actions into the kernel\n");
185 return -EINVAL;
186#else
187 printk("Ingress scheduler: Classifier actions prefered over netfilter\n");
188#endif
189#endif
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#ifndef CONFIG_NET_CLS_ACT
192#ifdef CONFIG_NETFILTER
193 if (!nf_registered) {
Patrick McHardy41c5b312007-12-05 01:22:43 -0800194 if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 printk("ingress qdisc registration error \n");
196 return -EINVAL;
197 }
198 nf_registered++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200#endif
201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 0;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static void ingress_reset(struct Qdisc *sch)
206{
Patrick McHardya4781222008-01-21 00:11:21 -0800207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/* ------------------------------------------------------------- */
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static void ingress_destroy(struct Qdisc *sch)
213{
Patrick McHardycb53c042008-01-21 00:11:48 -0800214 struct ingress_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Patrick McHardya48b5a62007-03-23 11:29:43 -0700216 tcf_destroy_chain(p->filter_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
220{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700221 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct rtattr *rta;
223
Patrick McHardy58f4df42008-01-21 00:11:01 -0800224 rta = (struct rtattr *)b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700226 rta->rta_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return skb->len;
228
229rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700230 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -1;
232}
233
Eric Dumazet20fea082007-11-14 01:44:41 -0800234static const struct Qdisc_class_ops ingress_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .graft = ingress_graft,
236 .leaf = ingress_leaf,
237 .get = ingress_get,
238 .put = ingress_put,
239 .change = ingress_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .walk = ingress_walk,
241 .tcf_chain = ingress_find_tcf,
242 .bind_tcf = ingress_bind_filter,
243 .unbind_tcf = ingress_put,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
Eric Dumazet20fea082007-11-14 01:44:41 -0800246static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .cl_ops = &ingress_class_ops,
248 .id = "ingress",
249 .priv_size = sizeof(struct ingress_qdisc_data),
250 .enqueue = ingress_enqueue,
251 .dequeue = ingress_dequeue,
252 .requeue = ingress_requeue,
253 .drop = ingress_drop,
254 .init = ingress_init,
255 .reset = ingress_reset,
256 .destroy = ingress_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .dump = ingress_dump,
258 .owner = THIS_MODULE,
259};
260
261static int __init ingress_module_init(void)
262{
263 int ret = 0;
264
265 if ((ret = register_qdisc(&ingress_qdisc_ops)) < 0) {
266 printk("Unable to register Ingress qdisc\n");
267 return ret;
268 }
269
270 return ret;
271}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800272
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900273static void __exit ingress_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 unregister_qdisc(&ingress_qdisc_ops);
276#ifndef CONFIG_NET_CLS_ACT
277#ifdef CONFIG_NETFILTER
Patrick McHardy41c5b312007-12-05 01:22:43 -0800278 if (nf_registered)
279 nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif
281#endif
282}
Patrick McHardy58f4df42008-01-21 00:11:01 -0800283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284module_init(ingress_module_init)
285module_exit(ingress_module_exit)
286MODULE_LICENSE("GPL");