]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sched/cls_api.c Packet classifier API. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
11 | * Changes: | |
12 | * | |
13 | * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support | |
14 | * | |
15 | */ | |
16 | ||
1da177e4 LT |
17 | #include <linux/module.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/kernel.h> | |
1da177e4 | 20 | #include <linux/string.h> |
1da177e4 | 21 | #include <linux/errno.h> |
33a48927 | 22 | #include <linux/err.h> |
1da177e4 | 23 | #include <linux/skbuff.h> |
1da177e4 LT |
24 | #include <linux/init.h> |
25 | #include <linux/kmod.h> | |
ab27cfb8 | 26 | #include <linux/err.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
b854272b DL |
28 | #include <net/net_namespace.h> |
29 | #include <net/sock.h> | |
dc5fc579 | 30 | #include <net/netlink.h> |
1da177e4 LT |
31 | #include <net/pkt_sched.h> |
32 | #include <net/pkt_cls.h> | |
33 | ||
1da177e4 | 34 | /* The list of all installed classifier types */ |
36272874 | 35 | static LIST_HEAD(tcf_proto_base); |
1da177e4 LT |
36 | |
37 | /* Protects list of registered TC modules. It is pure SMP lock. */ | |
38 | static DEFINE_RWLOCK(cls_mod_lock); | |
39 | ||
40 | /* Find classifier type by string name */ | |
41 | ||
33a48927 | 42 | static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind) |
1da177e4 | 43 | { |
dcd76081 | 44 | const struct tcf_proto_ops *t, *res = NULL; |
1da177e4 LT |
45 | |
46 | if (kind) { | |
47 | read_lock(&cls_mod_lock); | |
36272874 | 48 | list_for_each_entry(t, &tcf_proto_base, head) { |
33a48927 | 49 | if (strcmp(kind, t->kind) == 0) { |
dcd76081 ED |
50 | if (try_module_get(t->owner)) |
51 | res = t; | |
1da177e4 LT |
52 | break; |
53 | } | |
54 | } | |
55 | read_unlock(&cls_mod_lock); | |
56 | } | |
dcd76081 | 57 | return res; |
1da177e4 LT |
58 | } |
59 | ||
60 | /* Register(unregister) new classifier type */ | |
61 | ||
62 | int register_tcf_proto_ops(struct tcf_proto_ops *ops) | |
63 | { | |
36272874 | 64 | struct tcf_proto_ops *t; |
1da177e4 LT |
65 | int rc = -EEXIST; |
66 | ||
67 | write_lock(&cls_mod_lock); | |
36272874 | 68 | list_for_each_entry(t, &tcf_proto_base, head) |
1da177e4 LT |
69 | if (!strcmp(ops->kind, t->kind)) |
70 | goto out; | |
71 | ||
36272874 | 72 | list_add_tail(&ops->head, &tcf_proto_base); |
1da177e4 LT |
73 | rc = 0; |
74 | out: | |
75 | write_unlock(&cls_mod_lock); | |
76 | return rc; | |
77 | } | |
aa767bfe | 78 | EXPORT_SYMBOL(register_tcf_proto_ops); |
1da177e4 LT |
79 | |
80 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) | |
81 | { | |
36272874 | 82 | struct tcf_proto_ops *t; |
1da177e4 LT |
83 | int rc = -ENOENT; |
84 | ||
c78e1746 DB |
85 | /* Wait for outstanding call_rcu()s, if any, from a |
86 | * tcf_proto_ops's destroy() handler. | |
87 | */ | |
88 | rcu_barrier(); | |
89 | ||
1da177e4 | 90 | write_lock(&cls_mod_lock); |
dcd76081 ED |
91 | list_for_each_entry(t, &tcf_proto_base, head) { |
92 | if (t == ops) { | |
93 | list_del(&t->head); | |
94 | rc = 0; | |
1da177e4 | 95 | break; |
dcd76081 ED |
96 | } |
97 | } | |
1da177e4 LT |
98 | write_unlock(&cls_mod_lock); |
99 | return rc; | |
100 | } | |
aa767bfe | 101 | EXPORT_SYMBOL(unregister_tcf_proto_ops); |
1da177e4 | 102 | |
7316ae88 TG |
103 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
104 | struct nlmsghdr *n, struct tcf_proto *tp, | |
fa59b27c | 105 | unsigned long fh, int event, bool unicast); |
1da177e4 | 106 | |
ea7f8277 DB |
107 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
108 | struct nlmsghdr *n, | |
109 | struct tcf_proto __rcu **chain, int event) | |
110 | { | |
111 | struct tcf_proto __rcu **it_chain; | |
112 | struct tcf_proto *tp; | |
113 | ||
114 | for (it_chain = chain; (tp = rtnl_dereference(*it_chain)) != NULL; | |
115 | it_chain = &tp->next) | |
19a8bb28 | 116 | tfilter_notify(net, oskb, n, tp, 0, event, false); |
ea7f8277 | 117 | } |
1da177e4 LT |
118 | |
119 | /* Select new prio value from the range, managed by kernel. */ | |
120 | ||
aa767bfe | 121 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
1da177e4 | 122 | { |
aa767bfe | 123 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
1da177e4 LT |
124 | |
125 | if (tp) | |
cc7ec456 | 126 | first = tp->prio - 1; |
1da177e4 LT |
127 | |
128 | return first; | |
129 | } | |
130 | ||
33a48927 JP |
131 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
132 | u32 prio, u32 parent, struct Qdisc *q) | |
133 | { | |
134 | struct tcf_proto *tp; | |
135 | int err; | |
136 | ||
137 | tp = kzalloc(sizeof(*tp), GFP_KERNEL); | |
138 | if (!tp) | |
139 | return ERR_PTR(-ENOBUFS); | |
140 | ||
141 | err = -ENOENT; | |
142 | tp->ops = tcf_proto_lookup_ops(kind); | |
143 | if (!tp->ops) { | |
144 | #ifdef CONFIG_MODULES | |
145 | rtnl_unlock(); | |
146 | request_module("cls_%s", kind); | |
147 | rtnl_lock(); | |
148 | tp->ops = tcf_proto_lookup_ops(kind); | |
149 | /* We dropped the RTNL semaphore in order to perform | |
150 | * the module load. So, even if we succeeded in loading | |
151 | * the module we have to replay the request. We indicate | |
152 | * this using -EAGAIN. | |
153 | */ | |
154 | if (tp->ops) { | |
155 | module_put(tp->ops->owner); | |
156 | err = -EAGAIN; | |
157 | } else { | |
158 | err = -ENOENT; | |
159 | } | |
160 | goto errout; | |
161 | #endif | |
162 | } | |
163 | tp->classify = tp->ops->classify; | |
164 | tp->protocol = protocol; | |
165 | tp->prio = prio; | |
166 | tp->classid = parent; | |
167 | tp->q = q; | |
168 | ||
169 | err = tp->ops->init(tp); | |
170 | if (err) { | |
171 | module_put(tp->ops->owner); | |
172 | goto errout; | |
173 | } | |
174 | return tp; | |
175 | ||
176 | errout: | |
177 | kfree(tp); | |
178 | return ERR_PTR(err); | |
179 | } | |
180 | ||
cf1facda JP |
181 | static bool tcf_proto_destroy(struct tcf_proto *tp, bool force) |
182 | { | |
183 | if (tp->ops->destroy(tp, force)) { | |
184 | module_put(tp->ops->owner); | |
185 | kfree_rcu(tp, rcu); | |
186 | return true; | |
187 | } | |
188 | return false; | |
189 | } | |
190 | ||
191 | void tcf_destroy_chain(struct tcf_proto __rcu **fl) | |
192 | { | |
193 | struct tcf_proto *tp; | |
194 | ||
195 | while ((tp = rtnl_dereference(*fl)) != NULL) { | |
196 | RCU_INIT_POINTER(*fl, tp->next); | |
197 | tcf_proto_destroy(tp, true); | |
198 | } | |
199 | } | |
200 | EXPORT_SYMBOL(tcf_destroy_chain); | |
201 | ||
1da177e4 LT |
202 | /* Add/change/delete/get a filter node */ |
203 | ||
c21ef3e3 DA |
204 | static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
205 | struct netlink_ext_ack *extack) | |
1da177e4 | 206 | { |
3b1e0a65 | 207 | struct net *net = sock_net(skb->sk); |
add93b61 | 208 | struct nlattr *tca[TCA_MAX + 1]; |
1da177e4 LT |
209 | struct tcmsg *t; |
210 | u32 protocol; | |
211 | u32 prio; | |
212 | u32 nprio; | |
213 | u32 parent; | |
214 | struct net_device *dev; | |
215 | struct Qdisc *q; | |
25d8c0d5 JF |
216 | struct tcf_proto __rcu **back; |
217 | struct tcf_proto __rcu **chain; | |
40c81b25 | 218 | struct tcf_proto *next; |
1da177e4 | 219 | struct tcf_proto *tp; |
20fea08b | 220 | const struct Qdisc_class_ops *cops; |
1da177e4 LT |
221 | unsigned long cl; |
222 | unsigned long fh; | |
223 | int err; | |
628185cf | 224 | int tp_created; |
1da177e4 | 225 | |
4e8bbb81 | 226 | if ((n->nlmsg_type != RTM_GETTFILTER) && |
5f013c9b | 227 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
dfc47ef8 | 228 | return -EPERM; |
de179c8c | 229 | |
1da177e4 | 230 | replay: |
628185cf DB |
231 | tp_created = 0; |
232 | ||
c21ef3e3 | 233 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
de179c8c H |
234 | if (err < 0) |
235 | return err; | |
236 | ||
942b8165 | 237 | t = nlmsg_data(n); |
1da177e4 LT |
238 | protocol = TC_H_MIN(t->tcm_info); |
239 | prio = TC_H_MAJ(t->tcm_info); | |
240 | nprio = prio; | |
241 | parent = t->tcm_parent; | |
242 | cl = 0; | |
243 | ||
244 | if (prio == 0) { | |
ea7f8277 DB |
245 | switch (n->nlmsg_type) { |
246 | case RTM_DELTFILTER: | |
9f6ed032 | 247 | if (protocol || t->tcm_handle || tca[TCA_KIND]) |
ea7f8277 DB |
248 | return -ENOENT; |
249 | break; | |
250 | case RTM_NEWTFILTER: | |
251 | /* If no priority is provided by the user, | |
252 | * we allocate one. | |
253 | */ | |
254 | if (n->nlmsg_flags & NLM_F_CREATE) { | |
255 | prio = TC_H_MAKE(0x80000000U, 0U); | |
256 | break; | |
257 | } | |
258 | /* fall-through */ | |
259 | default: | |
1da177e4 | 260 | return -ENOENT; |
ea7f8277 | 261 | } |
1da177e4 LT |
262 | } |
263 | ||
264 | /* Find head of filter chain. */ | |
265 | ||
266 | /* Find link */ | |
7316ae88 | 267 | dev = __dev_get_by_index(net, t->tcm_ifindex); |
aa767bfe | 268 | if (dev == NULL) |
1da177e4 LT |
269 | return -ENODEV; |
270 | ||
271 | /* Find qdisc */ | |
272 | if (!parent) { | |
af356afa | 273 | q = dev->qdisc; |
1da177e4 | 274 | parent = q->handle; |
aa767bfe SH |
275 | } else { |
276 | q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent)); | |
277 | if (q == NULL) | |
278 | return -EINVAL; | |
279 | } | |
1da177e4 LT |
280 | |
281 | /* Is it classful? */ | |
cc7ec456 ED |
282 | cops = q->ops->cl_ops; |
283 | if (!cops) | |
1da177e4 LT |
284 | return -EINVAL; |
285 | ||
71ebe5e9 PM |
286 | if (cops->tcf_chain == NULL) |
287 | return -EOPNOTSUPP; | |
288 | ||
1da177e4 LT |
289 | /* Do we search for filter, attached to class? */ |
290 | if (TC_H_MIN(parent)) { | |
291 | cl = cops->get(q, parent); | |
292 | if (cl == 0) | |
293 | return -ENOENT; | |
294 | } | |
295 | ||
296 | /* And the last stroke */ | |
297 | chain = cops->tcf_chain(q, cl); | |
6bb16e7a JP |
298 | if (chain == NULL) { |
299 | err = -EINVAL; | |
1da177e4 | 300 | goto errout; |
6bb16e7a | 301 | } |
ea7f8277 DB |
302 | if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) { |
303 | tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER); | |
304 | tcf_destroy_chain(chain); | |
305 | err = 0; | |
306 | goto errout; | |
307 | } | |
1da177e4 LT |
308 | |
309 | /* Check the chain for existence of proto-tcf with this priority */ | |
25d8c0d5 JF |
310 | for (back = chain; |
311 | (tp = rtnl_dereference(*back)) != NULL; | |
312 | back = &tp->next) { | |
1da177e4 LT |
313 | if (tp->prio >= prio) { |
314 | if (tp->prio == prio) { | |
cc7ec456 | 315 | if (!nprio || |
6bb16e7a JP |
316 | (tp->protocol != protocol && protocol)) { |
317 | err = -EINVAL; | |
1da177e4 | 318 | goto errout; |
6bb16e7a | 319 | } |
7215032c | 320 | } else { |
1da177e4 | 321 | tp = NULL; |
7215032c | 322 | } |
1da177e4 LT |
323 | break; |
324 | } | |
325 | } | |
326 | ||
327 | if (tp == NULL) { | |
328 | /* Proto-tcf does not exist, create new one */ | |
329 | ||
6bb16e7a JP |
330 | if (tca[TCA_KIND] == NULL || !protocol) { |
331 | err = -EINVAL; | |
1da177e4 | 332 | goto errout; |
6bb16e7a | 333 | } |
1da177e4 | 334 | |
cc7ec456 | 335 | if (n->nlmsg_type != RTM_NEWTFILTER || |
6bb16e7a JP |
336 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
337 | err = -ENOENT; | |
1da177e4 | 338 | goto errout; |
6bb16e7a | 339 | } |
1da177e4 | 340 | |
33a48927 JP |
341 | if (!nprio) |
342 | nprio = TC_H_MAJ(tcf_auto_prio(rtnl_dereference(*back))); | |
1da177e4 | 343 | |
33a48927 JP |
344 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
345 | protocol, nprio, parent, q); | |
346 | if (IS_ERR(tp)) { | |
347 | err = PTR_ERR(tp); | |
1da177e4 LT |
348 | goto errout; |
349 | } | |
12186be7 | 350 | tp_created = 1; |
6bb16e7a JP |
351 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
352 | err = -EINVAL; | |
1da177e4 | 353 | goto errout; |
6bb16e7a | 354 | } |
1da177e4 LT |
355 | |
356 | fh = tp->ops->get(tp, t->tcm_handle); | |
357 | ||
358 | if (fh == 0) { | |
359 | if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) { | |
40c81b25 | 360 | next = rtnl_dereference(tp->next); |
25d8c0d5 | 361 | RCU_INIT_POINTER(*back, next); |
fa59b27c ED |
362 | tfilter_notify(net, skb, n, tp, fh, |
363 | RTM_DELTFILTER, false); | |
79112c26 | 364 | tcf_proto_destroy(tp, true); |
1da177e4 LT |
365 | err = 0; |
366 | goto errout; | |
367 | } | |
368 | ||
aa767bfe | 369 | if (n->nlmsg_type != RTM_NEWTFILTER || |
6bb16e7a JP |
370 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
371 | err = -ENOENT; | |
1da177e4 | 372 | goto errout; |
6bb16e7a | 373 | } |
1da177e4 LT |
374 | } else { |
375 | switch (n->nlmsg_type) { | |
10297b99 | 376 | case RTM_NEWTFILTER: |
12186be7 MU |
377 | if (n->nlmsg_flags & NLM_F_EXCL) { |
378 | if (tp_created) | |
79112c26 | 379 | tcf_proto_destroy(tp, true); |
6bb16e7a | 380 | err = -EEXIST; |
1da177e4 | 381 | goto errout; |
12186be7 | 382 | } |
1da177e4 LT |
383 | break; |
384 | case RTM_DELTFILTER: | |
385 | err = tp->ops->delete(tp, fh); | |
40c81b25 JP |
386 | if (err) |
387 | goto errout; | |
388 | next = rtnl_dereference(tp->next); | |
389 | tfilter_notify(net, skb, n, tp, t->tcm_handle, | |
390 | RTM_DELTFILTER, false); | |
391 | if (tcf_proto_destroy(tp, false)) | |
392 | RCU_INIT_POINTER(*back, next); | |
d7cf52c2 | 393 | goto errout; |
1da177e4 | 394 | case RTM_GETTFILTER: |
5a7a5555 | 395 | err = tfilter_notify(net, skb, n, tp, fh, |
fa59b27c | 396 | RTM_NEWTFILTER, true); |
1da177e4 LT |
397 | goto errout; |
398 | default: | |
399 | err = -EINVAL; | |
400 | goto errout; | |
401 | } | |
402 | } | |
403 | ||
2f7ef2f8 CW |
404 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
405 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE); | |
12186be7 MU |
406 | if (err == 0) { |
407 | if (tp_created) { | |
25d8c0d5 JF |
408 | RCU_INIT_POINTER(tp->next, rtnl_dereference(*back)); |
409 | rcu_assign_pointer(*back, tp); | |
12186be7 | 410 | } |
fa59b27c | 411 | tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false); |
12186be7 MU |
412 | } else { |
413 | if (tp_created) | |
79112c26 | 414 | tcf_proto_destroy(tp, true); |
12186be7 | 415 | } |
1da177e4 LT |
416 | |
417 | errout: | |
418 | if (cl) | |
419 | cops->put(q, cl); | |
420 | if (err == -EAGAIN) | |
421 | /* Replay the request. */ | |
422 | goto replay; | |
423 | return err; | |
424 | } | |
425 | ||
0b0f43fe JHS |
426 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
427 | struct tcf_proto *tp, unsigned long fh, u32 portid, | |
428 | u32 seq, u16 flags, int event) | |
1da177e4 LT |
429 | { |
430 | struct tcmsg *tcm; | |
431 | struct nlmsghdr *nlh; | |
27a884dc | 432 | unsigned char *b = skb_tail_pointer(skb); |
1da177e4 | 433 | |
15e47304 | 434 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
942b8165 DM |
435 | if (!nlh) |
436 | goto out_nlmsg_trim; | |
437 | tcm = nlmsg_data(nlh); | |
1da177e4 | 438 | tcm->tcm_family = AF_UNSPEC; |
9ef1d4c7 | 439 | tcm->tcm__pad1 = 0; |
ad61df91 | 440 | tcm->tcm__pad2 = 0; |
5ce2d488 | 441 | tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex; |
1da177e4 LT |
442 | tcm->tcm_parent = tp->classid; |
443 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); | |
1b34ec43 DM |
444 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
445 | goto nla_put_failure; | |
1da177e4 LT |
446 | tcm->tcm_handle = fh; |
447 | if (RTM_DELTFILTER != event) { | |
448 | tcm->tcm_handle = 0; | |
832d1d5b | 449 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
add93b61 | 450 | goto nla_put_failure; |
1da177e4 | 451 | } |
27a884dc | 452 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 LT |
453 | return skb->len; |
454 | ||
942b8165 | 455 | out_nlmsg_trim: |
add93b61 | 456 | nla_put_failure: |
dc5fc579 | 457 | nlmsg_trim(skb, b); |
1da177e4 LT |
458 | return -1; |
459 | } | |
460 | ||
7316ae88 TG |
461 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
462 | struct nlmsghdr *n, struct tcf_proto *tp, | |
fa59b27c | 463 | unsigned long fh, int event, bool unicast) |
1da177e4 LT |
464 | { |
465 | struct sk_buff *skb; | |
15e47304 | 466 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
1da177e4 LT |
467 | |
468 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
469 | if (!skb) | |
470 | return -ENOBUFS; | |
471 | ||
30a391a1 RM |
472 | if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, |
473 | n->nlmsg_flags, event) <= 0) { | |
1da177e4 LT |
474 | kfree_skb(skb); |
475 | return -EINVAL; | |
476 | } | |
477 | ||
fa59b27c ED |
478 | if (unicast) |
479 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
480 | ||
15e47304 | 481 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
aa767bfe | 482 | n->nlmsg_flags & NLM_F_ECHO); |
1da177e4 LT |
483 | } |
484 | ||
aa767bfe | 485 | struct tcf_dump_args { |
1da177e4 LT |
486 | struct tcf_walker w; |
487 | struct sk_buff *skb; | |
488 | struct netlink_callback *cb; | |
489 | }; | |
490 | ||
aa767bfe SH |
491 | static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, |
492 | struct tcf_walker *arg) | |
1da177e4 | 493 | { |
aa767bfe | 494 | struct tcf_dump_args *a = (void *)arg; |
832d1d5b | 495 | struct net *net = sock_net(a->skb->sk); |
1da177e4 | 496 | |
832d1d5b | 497 | return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid, |
5a7a5555 JHS |
498 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
499 | RTM_NEWTFILTER); | |
1da177e4 LT |
500 | } |
501 | ||
bd27a875 | 502 | /* called with RTNL */ |
1da177e4 LT |
503 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
504 | { | |
3b1e0a65 | 505 | struct net *net = sock_net(skb->sk); |
1da177e4 LT |
506 | int t; |
507 | int s_t; | |
508 | struct net_device *dev; | |
509 | struct Qdisc *q; | |
25d8c0d5 | 510 | struct tcf_proto *tp, __rcu **chain; |
942b8165 | 511 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
1da177e4 | 512 | unsigned long cl = 0; |
20fea08b | 513 | const struct Qdisc_class_ops *cops; |
1da177e4 LT |
514 | struct tcf_dump_args arg; |
515 | ||
573ce260 | 516 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
1da177e4 | 517 | return skb->len; |
cc7ec456 ED |
518 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
519 | if (!dev) | |
1da177e4 LT |
520 | return skb->len; |
521 | ||
1da177e4 | 522 | if (!tcm->tcm_parent) |
af356afa | 523 | q = dev->qdisc; |
1da177e4 LT |
524 | else |
525 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); | |
526 | if (!q) | |
527 | goto out; | |
cc7ec456 ED |
528 | cops = q->ops->cl_ops; |
529 | if (!cops) | |
1da177e4 | 530 | goto errout; |
71ebe5e9 PM |
531 | if (cops->tcf_chain == NULL) |
532 | goto errout; | |
1da177e4 LT |
533 | if (TC_H_MIN(tcm->tcm_parent)) { |
534 | cl = cops->get(q, tcm->tcm_parent); | |
535 | if (cl == 0) | |
536 | goto errout; | |
537 | } | |
538 | chain = cops->tcf_chain(q, cl); | |
539 | if (chain == NULL) | |
540 | goto errout; | |
541 | ||
542 | s_t = cb->args[0]; | |
543 | ||
25d8c0d5 JF |
544 | for (tp = rtnl_dereference(*chain), t = 0; |
545 | tp; tp = rtnl_dereference(tp->next), t++) { | |
cc7ec456 ED |
546 | if (t < s_t) |
547 | continue; | |
1da177e4 LT |
548 | if (TC_H_MAJ(tcm->tcm_info) && |
549 | TC_H_MAJ(tcm->tcm_info) != tp->prio) | |
550 | continue; | |
551 | if (TC_H_MIN(tcm->tcm_info) && | |
552 | TC_H_MIN(tcm->tcm_info) != tp->protocol) | |
553 | continue; | |
554 | if (t > s_t) | |
0b0f43fe JHS |
555 | memset(&cb->args[1], 0, |
556 | sizeof(cb->args)-sizeof(cb->args[0])); | |
1da177e4 | 557 | if (cb->args[1] == 0) { |
0b0f43fe JHS |
558 | if (tcf_fill_node(net, skb, tp, 0, |
559 | NETLINK_CB(cb->skb).portid, | |
aa767bfe SH |
560 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
561 | RTM_NEWTFILTER) <= 0) | |
1da177e4 | 562 | break; |
aa767bfe | 563 | |
1da177e4 LT |
564 | cb->args[1] = 1; |
565 | } | |
566 | if (tp->ops->walk == NULL) | |
567 | continue; | |
568 | arg.w.fn = tcf_node_dump; | |
569 | arg.skb = skb; | |
570 | arg.cb = cb; | |
571 | arg.w.stop = 0; | |
cc7ec456 | 572 | arg.w.skip = cb->args[1] - 1; |
1da177e4 LT |
573 | arg.w.count = 0; |
574 | tp->ops->walk(tp, &arg.w); | |
cc7ec456 | 575 | cb->args[1] = arg.w.count + 1; |
1da177e4 LT |
576 | if (arg.w.stop) |
577 | break; | |
578 | } | |
579 | ||
580 | cb->args[0] = t; | |
581 | ||
582 | errout: | |
583 | if (cl) | |
584 | cops->put(q, cl); | |
585 | out: | |
1da177e4 LT |
586 | return skb->len; |
587 | } | |
588 | ||
18d0264f | 589 | void tcf_exts_destroy(struct tcf_exts *exts) |
1da177e4 LT |
590 | { |
591 | #ifdef CONFIG_NET_CLS_ACT | |
22dc13c8 WC |
592 | LIST_HEAD(actions); |
593 | ||
594 | tcf_exts_to_list(exts, &actions); | |
595 | tcf_action_destroy(&actions, TCA_ACT_UNBIND); | |
596 | kfree(exts->actions); | |
597 | exts->nr_actions = 0; | |
1da177e4 LT |
598 | #endif |
599 | } | |
aa767bfe | 600 | EXPORT_SYMBOL(tcf_exts_destroy); |
1da177e4 | 601 | |
c1b52739 | 602 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, |
5a7a5555 | 603 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr) |
1da177e4 | 604 | { |
1da177e4 LT |
605 | #ifdef CONFIG_NET_CLS_ACT |
606 | { | |
1da177e4 LT |
607 | struct tc_action *act; |
608 | ||
5da57f42 WC |
609 | if (exts->police && tb[exts->police]) { |
610 | act = tcf_action_init_1(net, tb[exts->police], rate_tlv, | |
5a7a5555 | 611 | "police", ovr, TCA_ACT_BIND); |
ab27cfb8 PM |
612 | if (IS_ERR(act)) |
613 | return PTR_ERR(act); | |
1da177e4 | 614 | |
33be6271 | 615 | act->type = exts->type = TCA_OLD_COMPAT; |
22dc13c8 WC |
616 | exts->actions[0] = act; |
617 | exts->nr_actions = 1; | |
5da57f42 | 618 | } else if (exts->action && tb[exts->action]) { |
22dc13c8 WC |
619 | LIST_HEAD(actions); |
620 | int err, i = 0; | |
621 | ||
5da57f42 | 622 | err = tcf_action_init(net, tb[exts->action], rate_tlv, |
5a7a5555 JHS |
623 | NULL, ovr, TCA_ACT_BIND, |
624 | &actions); | |
33be6271 WC |
625 | if (err) |
626 | return err; | |
22dc13c8 WC |
627 | list_for_each_entry(act, &actions, list) |
628 | exts->actions[i++] = act; | |
629 | exts->nr_actions = i; | |
1da177e4 LT |
630 | } |
631 | } | |
1da177e4 | 632 | #else |
5da57f42 WC |
633 | if ((exts->action && tb[exts->action]) || |
634 | (exts->police && tb[exts->police])) | |
1da177e4 LT |
635 | return -EOPNOTSUPP; |
636 | #endif | |
637 | ||
638 | return 0; | |
639 | } | |
aa767bfe | 640 | EXPORT_SYMBOL(tcf_exts_validate); |
1da177e4 | 641 | |
aa767bfe SH |
642 | void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst, |
643 | struct tcf_exts *src) | |
1da177e4 LT |
644 | { |
645 | #ifdef CONFIG_NET_CLS_ACT | |
22dc13c8 WC |
646 | struct tcf_exts old = *dst; |
647 | ||
a49eb42a | 648 | tcf_tree_lock(tp); |
22dc13c8 WC |
649 | dst->nr_actions = src->nr_actions; |
650 | dst->actions = src->actions; | |
5301e3e1 | 651 | dst->type = src->type; |
a49eb42a | 652 | tcf_tree_unlock(tp); |
22dc13c8 WC |
653 | |
654 | tcf_exts_destroy(&old); | |
1da177e4 LT |
655 | #endif |
656 | } | |
aa767bfe | 657 | EXPORT_SYMBOL(tcf_exts_change); |
1da177e4 | 658 | |
22dc13c8 WC |
659 | #ifdef CONFIG_NET_CLS_ACT |
660 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) | |
661 | { | |
662 | if (exts->nr_actions == 0) | |
663 | return NULL; | |
664 | else | |
665 | return exts->actions[0]; | |
666 | } | |
667 | #endif | |
33be6271 | 668 | |
5da57f42 | 669 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
670 | { |
671 | #ifdef CONFIG_NET_CLS_ACT | |
9cc63db5 CW |
672 | struct nlattr *nest; |
673 | ||
22dc13c8 | 674 | if (exts->action && exts->nr_actions) { |
1da177e4 LT |
675 | /* |
676 | * again for backward compatible mode - we want | |
677 | * to work with both old and new modes of entering | |
678 | * tc data even if iproute2 was newer - jhs | |
679 | */ | |
33be6271 | 680 | if (exts->type != TCA_OLD_COMPAT) { |
22dc13c8 WC |
681 | LIST_HEAD(actions); |
682 | ||
5da57f42 | 683 | nest = nla_nest_start(skb, exts->action); |
4b3550ef PM |
684 | if (nest == NULL) |
685 | goto nla_put_failure; | |
22dc13c8 WC |
686 | |
687 | tcf_exts_to_list(exts, &actions); | |
688 | if (tcf_action_dump(skb, &actions, 0, 0) < 0) | |
add93b61 | 689 | goto nla_put_failure; |
4b3550ef | 690 | nla_nest_end(skb, nest); |
5da57f42 | 691 | } else if (exts->police) { |
33be6271 | 692 | struct tc_action *act = tcf_exts_first_act(exts); |
5da57f42 | 693 | nest = nla_nest_start(skb, exts->police); |
63acd680 | 694 | if (nest == NULL || !act) |
4b3550ef | 695 | goto nla_put_failure; |
33be6271 | 696 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
add93b61 | 697 | goto nla_put_failure; |
4b3550ef | 698 | nla_nest_end(skb, nest); |
1da177e4 LT |
699 | } |
700 | } | |
1da177e4 | 701 | return 0; |
9cc63db5 CW |
702 | |
703 | nla_put_failure: | |
704 | nla_nest_cancel(skb, nest); | |
1da177e4 | 705 | return -1; |
9cc63db5 CW |
706 | #else |
707 | return 0; | |
708 | #endif | |
1da177e4 | 709 | } |
aa767bfe | 710 | EXPORT_SYMBOL(tcf_exts_dump); |
1da177e4 | 711 | |
aa767bfe | 712 | |
5da57f42 | 713 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
714 | { |
715 | #ifdef CONFIG_NET_CLS_ACT | |
33be6271 | 716 | struct tc_action *a = tcf_exts_first_act(exts); |
b057df24 | 717 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
33be6271 | 718 | return -1; |
1da177e4 LT |
719 | #endif |
720 | return 0; | |
1da177e4 | 721 | } |
aa767bfe | 722 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
1da177e4 | 723 | |
7091d8c7 HHZ |
724 | int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts, |
725 | struct net_device **hw_dev) | |
726 | { | |
727 | #ifdef CONFIG_NET_CLS_ACT | |
728 | const struct tc_action *a; | |
729 | LIST_HEAD(actions); | |
730 | ||
731 | if (tc_no_actions(exts)) | |
732 | return -EINVAL; | |
733 | ||
734 | tcf_exts_to_list(exts, &actions); | |
735 | list_for_each_entry(a, &actions, list) { | |
736 | if (a->ops->get_dev) { | |
737 | a->ops->get_dev(a, dev_net(dev), hw_dev); | |
738 | break; | |
739 | } | |
740 | } | |
741 | if (*hw_dev) | |
742 | return 0; | |
743 | #endif | |
744 | return -EOPNOTSUPP; | |
745 | } | |
746 | EXPORT_SYMBOL(tcf_exts_get_dev); | |
747 | ||
1da177e4 LT |
748 | static int __init tc_filter_init(void) |
749 | { | |
c7ac8679 GR |
750 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL); |
751 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL); | |
82623c0d | 752 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter, |
c7ac8679 | 753 | tc_dump_tfilter, NULL); |
1da177e4 | 754 | |
1da177e4 LT |
755 | return 0; |
756 | } | |
757 | ||
758 | subsys_initcall(tc_filter_init); |