Commit | Line | Data |
---|---|---|
10297b99 | 1 | /* net/sched/sch_ingress.c - Ingress qdisc |
1da177e4 LT |
2 | * 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 | ||
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/types.h> | |
0ba48053 | 12 | #include <linux/list.h> |
1da177e4 | 13 | #include <linux/skbuff.h> |
1da177e4 LT |
14 | #include <linux/rtnetlink.h> |
15 | #include <linux/netfilter_ipv4.h> | |
16 | #include <linux/netfilter_ipv6.h> | |
17 | #include <linux/netfilter.h> | |
dc5fc579 | 18 | #include <net/netlink.h> |
1da177e4 | 19 | #include <net/pkt_sched.h> |
1da177e4 LT |
20 | |
21 | ||
58f4df42 | 22 | /* Thanks to Doron Oz for this hack */ |
1da177e4 LT |
23 | #ifndef CONFIG_NET_CLS_ACT |
24 | #ifdef CONFIG_NETFILTER | |
10297b99 | 25 | static int nf_registered; |
1da177e4 LT |
26 | #endif |
27 | #endif | |
28 | ||
29 | struct ingress_qdisc_data { | |
1da177e4 LT |
30 | struct tcf_proto *filter_list; |
31 | }; | |
32 | ||
1da177e4 LT |
33 | /* ------------------------- Class/flow operations ------------------------- */ |
34 | ||
58f4df42 PM |
35 | static int ingress_graft(struct Qdisc *sch, unsigned long arg, |
36 | struct Qdisc *new, struct Qdisc **old) | |
1da177e4 | 37 | { |
10297b99 | 38 | return 1; |
1da177e4 LT |
39 | } |
40 | ||
1da177e4 LT |
41 | static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg) |
42 | { | |
43 | return NULL; | |
44 | } | |
45 | ||
58f4df42 | 46 | static unsigned long ingress_get(struct Qdisc *sch, u32 classid) |
1da177e4 | 47 | { |
1da177e4 LT |
48 | return TC_H_MIN(classid) + 1; |
49 | } | |
50 | ||
1da177e4 | 51 | static unsigned long ingress_bind_filter(struct Qdisc *sch, |
58f4df42 | 52 | unsigned long parent, u32 classid) |
1da177e4 LT |
53 | { |
54 | return ingress_get(sch, classid); | |
55 | } | |
56 | ||
1da177e4 LT |
57 | static void ingress_put(struct Qdisc *sch, unsigned long cl) |
58 | { | |
59 | } | |
60 | ||
1da177e4 | 61 | static int ingress_change(struct Qdisc *sch, u32 classid, u32 parent, |
58f4df42 | 62 | struct rtattr **tca, unsigned long *arg) |
1da177e4 | 63 | { |
1da177e4 LT |
64 | return 0; |
65 | } | |
66 | ||
58f4df42 | 67 | static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker) |
1da177e4 | 68 | { |
a4781221 | 69 | return; |
1da177e4 LT |
70 | } |
71 | ||
58f4df42 | 72 | static struct tcf_proto **ingress_find_tcf(struct Qdisc *sch, unsigned long cl) |
1da177e4 | 73 | { |
cb53c048 | 74 | struct ingress_qdisc_data *p = qdisc_priv(sch); |
1da177e4 LT |
75 | |
76 | return &p->filter_list; | |
77 | } | |
78 | ||
1da177e4 LT |
79 | /* --------------------------- Qdisc operations ---------------------------- */ |
80 | ||
58f4df42 | 81 | static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch) |
1da177e4 | 82 | { |
cb53c048 | 83 | struct ingress_qdisc_data *p = qdisc_priv(sch); |
1da177e4 LT |
84 | struct tcf_result res; |
85 | int result; | |
86 | ||
1da177e4 | 87 | result = tc_classify(skb, p->filter_list, &res); |
a4781221 | 88 | |
1da177e4 LT |
89 | /* |
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) { | |
58f4df42 PM |
97 | 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; | |
3ff50b79 | 111 | } |
1da177e4 | 112 | #else |
1da177e4 LT |
113 | result = NF_ACCEPT; |
114 | sch->bstats.packets++; | |
115 | sch->bstats.bytes += skb->len; | |
1da177e4 LT |
116 | #endif |
117 | ||
118 | return result; | |
119 | } | |
120 | ||
1da177e4 LT |
121 | static struct sk_buff *ingress_dequeue(struct Qdisc *sch) |
122 | { | |
1da177e4 LT |
123 | return NULL; |
124 | } | |
125 | ||
58f4df42 | 126 | static int ingress_requeue(struct sk_buff *skb, struct Qdisc *sch) |
1da177e4 | 127 | { |
1da177e4 LT |
128 | return 0; |
129 | } | |
130 | ||
131 | static unsigned int ingress_drop(struct Qdisc *sch) | |
132 | { | |
1da177e4 LT |
133 | return 0; |
134 | } | |
135 | ||
136 | #ifndef CONFIG_NET_CLS_ACT | |
137 | #ifdef CONFIG_NETFILTER | |
58f4df42 | 138 | static unsigned int ing_hook(unsigned int hook, struct sk_buff *skb, |
10297b99 YH |
139 | const struct net_device *indev, |
140 | const struct net_device *outdev, | |
141 | int (*okfn)(struct sk_buff *)) | |
1da177e4 | 142 | { |
10297b99 | 143 | |
1da177e4 | 144 | struct Qdisc *q; |
10297b99 | 145 | struct net_device *dev = skb->dev; |
58f4df42 | 146 | int fwres = NF_ACCEPT; |
1da177e4 | 147 | |
1da177e4 | 148 | if (dev->qdisc_ingress) { |
fd44de7c | 149 | spin_lock(&dev->ingress_lock); |
1da177e4 LT |
150 | if ((q = dev->qdisc_ingress) != NULL) |
151 | fwres = q->enqueue(skb, q); | |
fd44de7c | 152 | spin_unlock(&dev->ingress_lock); |
10297b99 YH |
153 | } |
154 | ||
1da177e4 LT |
155 | return fwres; |
156 | } | |
157 | ||
158 | /* after ipt_filter */ | |
1999414a | 159 | static struct nf_hook_ops ing_ops[] __read_mostly = { |
41c5b317 PM |
160 | { |
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 | }, | |
1da177e4 | 174 | }; |
1da177e4 LT |
175 | #endif |
176 | #endif | |
177 | ||
58f4df42 | 178 | static int ingress_init(struct Qdisc *sch, struct rtattr *opt) |
1da177e4 | 179 | { |
58f4df42 PM |
180 | /* Make sure either netfilter or preferably CLS_ACT is |
181 | * compiled in */ | |
1da177e4 LT |
182 | #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 | |
10297b99 | 190 | |
1da177e4 LT |
191 | #ifndef CONFIG_NET_CLS_ACT |
192 | #ifdef CONFIG_NETFILTER | |
193 | if (!nf_registered) { | |
41c5b317 | 194 | if (nf_register_hooks(ing_ops, ARRAY_SIZE(ing_ops)) < 0) { |
1da177e4 LT |
195 | printk("ingress qdisc registration error \n"); |
196 | return -EINVAL; | |
197 | } | |
198 | nf_registered++; | |
1da177e4 LT |
199 | } |
200 | #endif | |
201 | #endif | |
1da177e4 LT |
202 | return 0; |
203 | } | |
204 | ||
1da177e4 LT |
205 | static void ingress_reset(struct Qdisc *sch) |
206 | { | |
a4781221 | 207 | return; |
1da177e4 LT |
208 | } |
209 | ||
1da177e4 LT |
210 | /* ------------------------------------------------------------- */ |
211 | ||
212 | static void ingress_destroy(struct Qdisc *sch) | |
213 | { | |
cb53c048 | 214 | struct ingress_qdisc_data *p = qdisc_priv(sch); |
1da177e4 | 215 | |
a48b5a61 | 216 | tcf_destroy_chain(p->filter_list); |
1da177e4 LT |
217 | } |
218 | ||
1da177e4 LT |
219 | static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb) |
220 | { | |
27a884dc | 221 | unsigned char *b = skb_tail_pointer(skb); |
1da177e4 LT |
222 | struct rtattr *rta; |
223 | ||
58f4df42 | 224 | rta = (struct rtattr *)b; |
1da177e4 | 225 | RTA_PUT(skb, TCA_OPTIONS, 0, NULL); |
27a884dc | 226 | rta->rta_len = skb_tail_pointer(skb) - b; |
1da177e4 LT |
227 | return skb->len; |
228 | ||
229 | rtattr_failure: | |
dc5fc579 | 230 | nlmsg_trim(skb, b); |
1da177e4 LT |
231 | return -1; |
232 | } | |
233 | ||
20fea08b | 234 | static const struct Qdisc_class_ops ingress_class_ops = { |
1da177e4 LT |
235 | .graft = ingress_graft, |
236 | .leaf = ingress_leaf, | |
237 | .get = ingress_get, | |
238 | .put = ingress_put, | |
239 | .change = ingress_change, | |
1da177e4 LT |
240 | .walk = ingress_walk, |
241 | .tcf_chain = ingress_find_tcf, | |
242 | .bind_tcf = ingress_bind_filter, | |
243 | .unbind_tcf = ingress_put, | |
1da177e4 LT |
244 | }; |
245 | ||
20fea08b | 246 | static struct Qdisc_ops ingress_qdisc_ops __read_mostly = { |
1da177e4 LT |
247 | .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, | |
1da177e4 LT |
257 | .dump = ingress_dump, |
258 | .owner = THIS_MODULE, | |
259 | }; | |
260 | ||
261 | static 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 | } | |
58f4df42 | 272 | |
10297b99 | 273 | static void __exit ingress_module_exit(void) |
1da177e4 LT |
274 | { |
275 | unregister_qdisc(&ingress_qdisc_ops); | |
276 | #ifndef CONFIG_NET_CLS_ACT | |
277 | #ifdef CONFIG_NETFILTER | |
41c5b317 PM |
278 | if (nf_registered) |
279 | nf_unregister_hooks(ing_ops, ARRAY_SIZE(ing_ops)); | |
1da177e4 LT |
280 | #endif |
281 | #endif | |
282 | } | |
58f4df42 | 283 | |
1da177e4 LT |
284 | module_init(ingress_module_init) |
285 | module_exit(ingress_module_exit) | |
286 | MODULE_LICENSE("GPL"); |