]>
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, | |
2190d1d0 | 109 | struct tcf_chain *chain, int event) |
ea7f8277 | 110 | { |
ea7f8277 DB |
111 | struct tcf_proto *tp; |
112 | ||
2190d1d0 JP |
113 | for (tp = rtnl_dereference(chain->filter_chain); |
114 | tp; tp = rtnl_dereference(tp->next)) | |
19a8bb28 | 115 | tfilter_notify(net, oskb, n, tp, 0, event, false); |
ea7f8277 | 116 | } |
1da177e4 LT |
117 | |
118 | /* Select new prio value from the range, managed by kernel. */ | |
119 | ||
aa767bfe | 120 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
1da177e4 | 121 | { |
aa767bfe | 122 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
1da177e4 LT |
123 | |
124 | if (tp) | |
cc7ec456 | 125 | first = tp->prio - 1; |
1da177e4 | 126 | |
7961973a | 127 | return TC_H_MAJ(first); |
1da177e4 LT |
128 | } |
129 | ||
33a48927 | 130 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
6529eaba JP |
131 | u32 prio, u32 parent, struct Qdisc *q, |
132 | struct tcf_block *block) | |
33a48927 JP |
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; | |
6529eaba | 168 | tp->block = block; |
33a48927 JP |
169 | |
170 | err = tp->ops->init(tp); | |
171 | if (err) { | |
172 | module_put(tp->ops->owner); | |
173 | goto errout; | |
174 | } | |
175 | return tp; | |
176 | ||
177 | errout: | |
178 | kfree(tp); | |
179 | return ERR_PTR(err); | |
180 | } | |
181 | ||
763dbf63 | 182 | static void tcf_proto_destroy(struct tcf_proto *tp) |
cf1facda | 183 | { |
763dbf63 WC |
184 | tp->ops->destroy(tp); |
185 | module_put(tp->ops->owner); | |
186 | kfree_rcu(tp, rcu); | |
cf1facda JP |
187 | } |
188 | ||
2190d1d0 JP |
189 | static struct tcf_chain *tcf_chain_create(void) |
190 | { | |
191 | return kzalloc(sizeof(struct tcf_chain), GFP_KERNEL); | |
192 | } | |
193 | ||
194 | static void tcf_chain_destroy(struct tcf_chain *chain) | |
cf1facda JP |
195 | { |
196 | struct tcf_proto *tp; | |
197 | ||
2190d1d0 JP |
198 | while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) { |
199 | RCU_INIT_POINTER(chain->filter_chain, tp->next); | |
763dbf63 | 200 | tcf_proto_destroy(tp); |
cf1facda | 201 | } |
2190d1d0 JP |
202 | kfree(chain); |
203 | } | |
204 | ||
205 | static void | |
206 | tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain, | |
207 | struct tcf_proto __rcu **p_filter_chain) | |
208 | { | |
209 | chain->p_filter_chain = p_filter_chain; | |
cf1facda | 210 | } |
6529eaba JP |
211 | |
212 | int tcf_block_get(struct tcf_block **p_block, | |
213 | struct tcf_proto __rcu **p_filter_chain) | |
214 | { | |
215 | struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL); | |
2190d1d0 | 216 | int err; |
6529eaba JP |
217 | |
218 | if (!block) | |
219 | return -ENOMEM; | |
2190d1d0 JP |
220 | block->chain = tcf_chain_create(); |
221 | if (!block->chain) { | |
222 | err = -ENOMEM; | |
223 | goto err_chain_create; | |
224 | } | |
225 | tcf_chain_filter_chain_ptr_set(block->chain, p_filter_chain); | |
6529eaba JP |
226 | *p_block = block; |
227 | return 0; | |
2190d1d0 JP |
228 | |
229 | err_chain_create: | |
230 | kfree(block); | |
231 | return err; | |
6529eaba JP |
232 | } |
233 | EXPORT_SYMBOL(tcf_block_get); | |
234 | ||
235 | void tcf_block_put(struct tcf_block *block) | |
236 | { | |
237 | if (!block) | |
238 | return; | |
2190d1d0 | 239 | tcf_chain_destroy(block->chain); |
6529eaba JP |
240 | kfree(block); |
241 | } | |
242 | EXPORT_SYMBOL(tcf_block_put); | |
cf1facda | 243 | |
87d83093 JP |
244 | /* Main classifier routine: scans classifier chain attached |
245 | * to this qdisc, (optionally) tests for protocol and asks | |
246 | * specific classifiers. | |
247 | */ | |
248 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, | |
249 | struct tcf_result *res, bool compat_mode) | |
250 | { | |
251 | __be16 protocol = tc_skb_protocol(skb); | |
252 | #ifdef CONFIG_NET_CLS_ACT | |
253 | const int max_reclassify_loop = 4; | |
254 | const struct tcf_proto *old_tp = tp; | |
255 | int limit = 0; | |
256 | ||
257 | reclassify: | |
258 | #endif | |
259 | for (; tp; tp = rcu_dereference_bh(tp->next)) { | |
260 | int err; | |
261 | ||
262 | if (tp->protocol != protocol && | |
263 | tp->protocol != htons(ETH_P_ALL)) | |
264 | continue; | |
265 | ||
266 | err = tp->classify(skb, tp, res); | |
267 | #ifdef CONFIG_NET_CLS_ACT | |
268 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) | |
269 | goto reset; | |
270 | #endif | |
271 | if (err >= 0) | |
272 | return err; | |
273 | } | |
274 | ||
275 | return TC_ACT_UNSPEC; /* signal: continue lookup */ | |
276 | #ifdef CONFIG_NET_CLS_ACT | |
277 | reset: | |
278 | if (unlikely(limit++ >= max_reclassify_loop)) { | |
279 | net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n", | |
280 | tp->q->ops->id, tp->prio & 0xffff, | |
281 | ntohs(tp->protocol)); | |
282 | return TC_ACT_SHOT; | |
283 | } | |
284 | ||
285 | tp = old_tp; | |
286 | protocol = tc_skb_protocol(skb); | |
287 | goto reclassify; | |
288 | #endif | |
289 | } | |
290 | EXPORT_SYMBOL(tcf_classify); | |
291 | ||
2190d1d0 JP |
292 | struct tcf_chain_info { |
293 | struct tcf_proto __rcu **pprev; | |
294 | struct tcf_proto __rcu *next; | |
295 | }; | |
296 | ||
297 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) | |
298 | { | |
299 | return rtnl_dereference(*chain_info->pprev); | |
300 | } | |
301 | ||
302 | static void tcf_chain_tp_insert(struct tcf_chain *chain, | |
303 | struct tcf_chain_info *chain_info, | |
304 | struct tcf_proto *tp) | |
305 | { | |
306 | if (chain->p_filter_chain && | |
307 | *chain_info->pprev == chain->filter_chain) | |
308 | *chain->p_filter_chain = tp; | |
309 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); | |
310 | rcu_assign_pointer(*chain_info->pprev, tp); | |
311 | } | |
312 | ||
313 | static void tcf_chain_tp_remove(struct tcf_chain *chain, | |
314 | struct tcf_chain_info *chain_info, | |
315 | struct tcf_proto *tp) | |
316 | { | |
317 | struct tcf_proto *next = rtnl_dereference(chain_info->next); | |
318 | ||
319 | if (chain->p_filter_chain && tp == chain->filter_chain) | |
320 | *chain->p_filter_chain = next; | |
321 | RCU_INIT_POINTER(*chain_info->pprev, next); | |
322 | } | |
323 | ||
324 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, | |
325 | struct tcf_chain_info *chain_info, | |
326 | u32 protocol, u32 prio, | |
327 | bool prio_allocate) | |
328 | { | |
329 | struct tcf_proto **pprev; | |
330 | struct tcf_proto *tp; | |
331 | ||
332 | /* Check the chain for existence of proto-tcf with this priority */ | |
333 | for (pprev = &chain->filter_chain; | |
334 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { | |
335 | if (tp->prio >= prio) { | |
336 | if (tp->prio == prio) { | |
337 | if (prio_allocate || | |
338 | (tp->protocol != protocol && protocol)) | |
339 | return ERR_PTR(-EINVAL); | |
340 | } else { | |
341 | tp = NULL; | |
342 | } | |
343 | break; | |
344 | } | |
345 | } | |
346 | chain_info->pprev = pprev; | |
347 | chain_info->next = tp ? tp->next : NULL; | |
348 | return tp; | |
349 | } | |
350 | ||
1da177e4 LT |
351 | /* Add/change/delete/get a filter node */ |
352 | ||
c21ef3e3 DA |
353 | static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
354 | struct netlink_ext_ack *extack) | |
1da177e4 | 355 | { |
3b1e0a65 | 356 | struct net *net = sock_net(skb->sk); |
add93b61 | 357 | struct nlattr *tca[TCA_MAX + 1]; |
1da177e4 LT |
358 | struct tcmsg *t; |
359 | u32 protocol; | |
360 | u32 prio; | |
9d36d9e5 | 361 | bool prio_allocate; |
1da177e4 LT |
362 | u32 parent; |
363 | struct net_device *dev; | |
364 | struct Qdisc *q; | |
2190d1d0 JP |
365 | struct tcf_chain_info chain_info; |
366 | struct tcf_chain *chain; | |
6529eaba | 367 | struct tcf_block *block; |
1da177e4 | 368 | struct tcf_proto *tp; |
20fea08b | 369 | const struct Qdisc_class_ops *cops; |
1da177e4 LT |
370 | unsigned long cl; |
371 | unsigned long fh; | |
372 | int err; | |
628185cf | 373 | int tp_created; |
1da177e4 | 374 | |
4e8bbb81 | 375 | if ((n->nlmsg_type != RTM_GETTFILTER) && |
5f013c9b | 376 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
dfc47ef8 | 377 | return -EPERM; |
de179c8c | 378 | |
1da177e4 | 379 | replay: |
628185cf DB |
380 | tp_created = 0; |
381 | ||
c21ef3e3 | 382 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
de179c8c H |
383 | if (err < 0) |
384 | return err; | |
385 | ||
942b8165 | 386 | t = nlmsg_data(n); |
1da177e4 LT |
387 | protocol = TC_H_MIN(t->tcm_info); |
388 | prio = TC_H_MAJ(t->tcm_info); | |
9d36d9e5 | 389 | prio_allocate = false; |
1da177e4 LT |
390 | parent = t->tcm_parent; |
391 | cl = 0; | |
392 | ||
393 | if (prio == 0) { | |
ea7f8277 DB |
394 | switch (n->nlmsg_type) { |
395 | case RTM_DELTFILTER: | |
9f6ed032 | 396 | if (protocol || t->tcm_handle || tca[TCA_KIND]) |
ea7f8277 DB |
397 | return -ENOENT; |
398 | break; | |
399 | case RTM_NEWTFILTER: | |
400 | /* If no priority is provided by the user, | |
401 | * we allocate one. | |
402 | */ | |
403 | if (n->nlmsg_flags & NLM_F_CREATE) { | |
404 | prio = TC_H_MAKE(0x80000000U, 0U); | |
9d36d9e5 | 405 | prio_allocate = true; |
ea7f8277 DB |
406 | break; |
407 | } | |
408 | /* fall-through */ | |
409 | default: | |
1da177e4 | 410 | return -ENOENT; |
ea7f8277 | 411 | } |
1da177e4 LT |
412 | } |
413 | ||
414 | /* Find head of filter chain. */ | |
415 | ||
416 | /* Find link */ | |
7316ae88 | 417 | dev = __dev_get_by_index(net, t->tcm_ifindex); |
aa767bfe | 418 | if (dev == NULL) |
1da177e4 LT |
419 | return -ENODEV; |
420 | ||
421 | /* Find qdisc */ | |
422 | if (!parent) { | |
af356afa | 423 | q = dev->qdisc; |
1da177e4 | 424 | parent = q->handle; |
aa767bfe SH |
425 | } else { |
426 | q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent)); | |
427 | if (q == NULL) | |
428 | return -EINVAL; | |
429 | } | |
1da177e4 LT |
430 | |
431 | /* Is it classful? */ | |
cc7ec456 ED |
432 | cops = q->ops->cl_ops; |
433 | if (!cops) | |
1da177e4 LT |
434 | return -EINVAL; |
435 | ||
6529eaba | 436 | if (!cops->tcf_block) |
71ebe5e9 PM |
437 | return -EOPNOTSUPP; |
438 | ||
1da177e4 LT |
439 | /* Do we search for filter, attached to class? */ |
440 | if (TC_H_MIN(parent)) { | |
441 | cl = cops->get(q, parent); | |
442 | if (cl == 0) | |
443 | return -ENOENT; | |
444 | } | |
445 | ||
446 | /* And the last stroke */ | |
6529eaba JP |
447 | block = cops->tcf_block(q, cl); |
448 | if (!block) { | |
6bb16e7a | 449 | err = -EINVAL; |
1da177e4 | 450 | goto errout; |
6bb16e7a | 451 | } |
2190d1d0 | 452 | chain = block->chain; |
6529eaba | 453 | |
ea7f8277 DB |
454 | if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) { |
455 | tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER); | |
fbe9c5b0 | 456 | tcf_chain_destroy(chain); |
ea7f8277 DB |
457 | err = 0; |
458 | goto errout; | |
459 | } | |
1da177e4 | 460 | |
2190d1d0 JP |
461 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
462 | prio, prio_allocate); | |
463 | if (IS_ERR(tp)) { | |
464 | err = PTR_ERR(tp); | |
465 | goto errout; | |
1da177e4 LT |
466 | } |
467 | ||
468 | if (tp == NULL) { | |
469 | /* Proto-tcf does not exist, create new one */ | |
470 | ||
6bb16e7a JP |
471 | if (tca[TCA_KIND] == NULL || !protocol) { |
472 | err = -EINVAL; | |
1da177e4 | 473 | goto errout; |
6bb16e7a | 474 | } |
1da177e4 | 475 | |
cc7ec456 | 476 | if (n->nlmsg_type != RTM_NEWTFILTER || |
6bb16e7a JP |
477 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
478 | err = -ENOENT; | |
1da177e4 | 479 | goto errout; |
6bb16e7a | 480 | } |
1da177e4 | 481 | |
9d36d9e5 | 482 | if (prio_allocate) |
2190d1d0 | 483 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
1da177e4 | 484 | |
33a48927 | 485 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
9d36d9e5 | 486 | protocol, prio, parent, q, block); |
33a48927 JP |
487 | if (IS_ERR(tp)) { |
488 | err = PTR_ERR(tp); | |
1da177e4 LT |
489 | goto errout; |
490 | } | |
12186be7 | 491 | tp_created = 1; |
6bb16e7a JP |
492 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
493 | err = -EINVAL; | |
1da177e4 | 494 | goto errout; |
6bb16e7a | 495 | } |
1da177e4 LT |
496 | |
497 | fh = tp->ops->get(tp, t->tcm_handle); | |
498 | ||
499 | if (fh == 0) { | |
500 | if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) { | |
2190d1d0 | 501 | tcf_chain_tp_remove(chain, &chain_info, tp); |
fa59b27c ED |
502 | tfilter_notify(net, skb, n, tp, fh, |
503 | RTM_DELTFILTER, false); | |
763dbf63 | 504 | tcf_proto_destroy(tp); |
1da177e4 LT |
505 | err = 0; |
506 | goto errout; | |
507 | } | |
508 | ||
aa767bfe | 509 | if (n->nlmsg_type != RTM_NEWTFILTER || |
6bb16e7a JP |
510 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
511 | err = -ENOENT; | |
1da177e4 | 512 | goto errout; |
6bb16e7a | 513 | } |
1da177e4 | 514 | } else { |
763dbf63 WC |
515 | bool last; |
516 | ||
1da177e4 | 517 | switch (n->nlmsg_type) { |
10297b99 | 518 | case RTM_NEWTFILTER: |
12186be7 MU |
519 | if (n->nlmsg_flags & NLM_F_EXCL) { |
520 | if (tp_created) | |
763dbf63 | 521 | tcf_proto_destroy(tp); |
6bb16e7a | 522 | err = -EEXIST; |
1da177e4 | 523 | goto errout; |
12186be7 | 524 | } |
1da177e4 LT |
525 | break; |
526 | case RTM_DELTFILTER: | |
763dbf63 | 527 | err = tp->ops->delete(tp, fh, &last); |
40c81b25 JP |
528 | if (err) |
529 | goto errout; | |
40c81b25 JP |
530 | tfilter_notify(net, skb, n, tp, t->tcm_handle, |
531 | RTM_DELTFILTER, false); | |
763dbf63 | 532 | if (last) { |
2190d1d0 | 533 | tcf_chain_tp_remove(chain, &chain_info, tp); |
763dbf63 WC |
534 | tcf_proto_destroy(tp); |
535 | } | |
d7cf52c2 | 536 | goto errout; |
1da177e4 | 537 | case RTM_GETTFILTER: |
5a7a5555 | 538 | err = tfilter_notify(net, skb, n, tp, fh, |
fa59b27c | 539 | RTM_NEWTFILTER, true); |
1da177e4 LT |
540 | goto errout; |
541 | default: | |
542 | err = -EINVAL; | |
543 | goto errout; | |
544 | } | |
545 | } | |
546 | ||
2f7ef2f8 CW |
547 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
548 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE); | |
12186be7 | 549 | if (err == 0) { |
2190d1d0 JP |
550 | if (tp_created) |
551 | tcf_chain_tp_insert(chain, &chain_info, tp); | |
fa59b27c | 552 | tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false); |
12186be7 MU |
553 | } else { |
554 | if (tp_created) | |
763dbf63 | 555 | tcf_proto_destroy(tp); |
12186be7 | 556 | } |
1da177e4 LT |
557 | |
558 | errout: | |
559 | if (cl) | |
560 | cops->put(q, cl); | |
561 | if (err == -EAGAIN) | |
562 | /* Replay the request. */ | |
563 | goto replay; | |
564 | return err; | |
565 | } | |
566 | ||
0b0f43fe JHS |
567 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
568 | struct tcf_proto *tp, unsigned long fh, u32 portid, | |
569 | u32 seq, u16 flags, int event) | |
1da177e4 LT |
570 | { |
571 | struct tcmsg *tcm; | |
572 | struct nlmsghdr *nlh; | |
27a884dc | 573 | unsigned char *b = skb_tail_pointer(skb); |
1da177e4 | 574 | |
15e47304 | 575 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
942b8165 DM |
576 | if (!nlh) |
577 | goto out_nlmsg_trim; | |
578 | tcm = nlmsg_data(nlh); | |
1da177e4 | 579 | tcm->tcm_family = AF_UNSPEC; |
9ef1d4c7 | 580 | tcm->tcm__pad1 = 0; |
ad61df91 | 581 | tcm->tcm__pad2 = 0; |
5ce2d488 | 582 | tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex; |
1da177e4 LT |
583 | tcm->tcm_parent = tp->classid; |
584 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); | |
1b34ec43 DM |
585 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
586 | goto nla_put_failure; | |
1da177e4 LT |
587 | tcm->tcm_handle = fh; |
588 | if (RTM_DELTFILTER != event) { | |
589 | tcm->tcm_handle = 0; | |
832d1d5b | 590 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
add93b61 | 591 | goto nla_put_failure; |
1da177e4 | 592 | } |
27a884dc | 593 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 LT |
594 | return skb->len; |
595 | ||
942b8165 | 596 | out_nlmsg_trim: |
add93b61 | 597 | nla_put_failure: |
dc5fc579 | 598 | nlmsg_trim(skb, b); |
1da177e4 LT |
599 | return -1; |
600 | } | |
601 | ||
7316ae88 TG |
602 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
603 | struct nlmsghdr *n, struct tcf_proto *tp, | |
fa59b27c | 604 | unsigned long fh, int event, bool unicast) |
1da177e4 LT |
605 | { |
606 | struct sk_buff *skb; | |
15e47304 | 607 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
1da177e4 LT |
608 | |
609 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
610 | if (!skb) | |
611 | return -ENOBUFS; | |
612 | ||
30a391a1 RM |
613 | if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, |
614 | n->nlmsg_flags, event) <= 0) { | |
1da177e4 LT |
615 | kfree_skb(skb); |
616 | return -EINVAL; | |
617 | } | |
618 | ||
fa59b27c ED |
619 | if (unicast) |
620 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
621 | ||
15e47304 | 622 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
aa767bfe | 623 | n->nlmsg_flags & NLM_F_ECHO); |
1da177e4 LT |
624 | } |
625 | ||
aa767bfe | 626 | struct tcf_dump_args { |
1da177e4 LT |
627 | struct tcf_walker w; |
628 | struct sk_buff *skb; | |
629 | struct netlink_callback *cb; | |
630 | }; | |
631 | ||
aa767bfe SH |
632 | static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, |
633 | struct tcf_walker *arg) | |
1da177e4 | 634 | { |
aa767bfe | 635 | struct tcf_dump_args *a = (void *)arg; |
832d1d5b | 636 | struct net *net = sock_net(a->skb->sk); |
1da177e4 | 637 | |
832d1d5b | 638 | return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid, |
5a7a5555 JHS |
639 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
640 | RTM_NEWTFILTER); | |
1da177e4 LT |
641 | } |
642 | ||
bd27a875 | 643 | /* called with RTNL */ |
1da177e4 LT |
644 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
645 | { | |
3b1e0a65 | 646 | struct net *net = sock_net(skb->sk); |
1da177e4 LT |
647 | int t; |
648 | int s_t; | |
649 | struct net_device *dev; | |
650 | struct Qdisc *q; | |
6529eaba | 651 | struct tcf_block *block; |
2190d1d0 JP |
652 | struct tcf_proto *tp; |
653 | struct tcf_chain *chain; | |
942b8165 | 654 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
1da177e4 | 655 | unsigned long cl = 0; |
20fea08b | 656 | const struct Qdisc_class_ops *cops; |
1da177e4 LT |
657 | struct tcf_dump_args arg; |
658 | ||
573ce260 | 659 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
1da177e4 | 660 | return skb->len; |
cc7ec456 ED |
661 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
662 | if (!dev) | |
1da177e4 LT |
663 | return skb->len; |
664 | ||
1da177e4 | 665 | if (!tcm->tcm_parent) |
af356afa | 666 | q = dev->qdisc; |
1da177e4 LT |
667 | else |
668 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); | |
669 | if (!q) | |
670 | goto out; | |
cc7ec456 ED |
671 | cops = q->ops->cl_ops; |
672 | if (!cops) | |
1da177e4 | 673 | goto errout; |
6529eaba | 674 | if (!cops->tcf_block) |
71ebe5e9 | 675 | goto errout; |
1da177e4 LT |
676 | if (TC_H_MIN(tcm->tcm_parent)) { |
677 | cl = cops->get(q, tcm->tcm_parent); | |
678 | if (cl == 0) | |
679 | goto errout; | |
680 | } | |
6529eaba JP |
681 | block = cops->tcf_block(q, cl); |
682 | if (!block) | |
1da177e4 | 683 | goto errout; |
2190d1d0 | 684 | chain = block->chain; |
1da177e4 LT |
685 | |
686 | s_t = cb->args[0]; | |
687 | ||
2190d1d0 | 688 | for (tp = rtnl_dereference(chain->filter_chain), t = 0; |
25d8c0d5 | 689 | tp; tp = rtnl_dereference(tp->next), t++) { |
cc7ec456 ED |
690 | if (t < s_t) |
691 | continue; | |
1da177e4 LT |
692 | if (TC_H_MAJ(tcm->tcm_info) && |
693 | TC_H_MAJ(tcm->tcm_info) != tp->prio) | |
694 | continue; | |
695 | if (TC_H_MIN(tcm->tcm_info) && | |
696 | TC_H_MIN(tcm->tcm_info) != tp->protocol) | |
697 | continue; | |
698 | if (t > s_t) | |
0b0f43fe JHS |
699 | memset(&cb->args[1], 0, |
700 | sizeof(cb->args)-sizeof(cb->args[0])); | |
1da177e4 | 701 | if (cb->args[1] == 0) { |
0b0f43fe JHS |
702 | if (tcf_fill_node(net, skb, tp, 0, |
703 | NETLINK_CB(cb->skb).portid, | |
aa767bfe SH |
704 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
705 | RTM_NEWTFILTER) <= 0) | |
1da177e4 | 706 | break; |
aa767bfe | 707 | |
1da177e4 LT |
708 | cb->args[1] = 1; |
709 | } | |
710 | if (tp->ops->walk == NULL) | |
711 | continue; | |
712 | arg.w.fn = tcf_node_dump; | |
713 | arg.skb = skb; | |
714 | arg.cb = cb; | |
715 | arg.w.stop = 0; | |
cc7ec456 | 716 | arg.w.skip = cb->args[1] - 1; |
1da177e4 LT |
717 | arg.w.count = 0; |
718 | tp->ops->walk(tp, &arg.w); | |
cc7ec456 | 719 | cb->args[1] = arg.w.count + 1; |
1da177e4 LT |
720 | if (arg.w.stop) |
721 | break; | |
722 | } | |
723 | ||
724 | cb->args[0] = t; | |
725 | ||
726 | errout: | |
727 | if (cl) | |
728 | cops->put(q, cl); | |
729 | out: | |
1da177e4 LT |
730 | return skb->len; |
731 | } | |
732 | ||
18d0264f | 733 | void tcf_exts_destroy(struct tcf_exts *exts) |
1da177e4 LT |
734 | { |
735 | #ifdef CONFIG_NET_CLS_ACT | |
22dc13c8 WC |
736 | LIST_HEAD(actions); |
737 | ||
738 | tcf_exts_to_list(exts, &actions); | |
739 | tcf_action_destroy(&actions, TCA_ACT_UNBIND); | |
740 | kfree(exts->actions); | |
741 | exts->nr_actions = 0; | |
1da177e4 LT |
742 | #endif |
743 | } | |
aa767bfe | 744 | EXPORT_SYMBOL(tcf_exts_destroy); |
1da177e4 | 745 | |
c1b52739 | 746 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, |
5a7a5555 | 747 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr) |
1da177e4 | 748 | { |
1da177e4 LT |
749 | #ifdef CONFIG_NET_CLS_ACT |
750 | { | |
1da177e4 LT |
751 | struct tc_action *act; |
752 | ||
5da57f42 WC |
753 | if (exts->police && tb[exts->police]) { |
754 | act = tcf_action_init_1(net, tb[exts->police], rate_tlv, | |
5a7a5555 | 755 | "police", ovr, TCA_ACT_BIND); |
ab27cfb8 PM |
756 | if (IS_ERR(act)) |
757 | return PTR_ERR(act); | |
1da177e4 | 758 | |
33be6271 | 759 | act->type = exts->type = TCA_OLD_COMPAT; |
22dc13c8 WC |
760 | exts->actions[0] = act; |
761 | exts->nr_actions = 1; | |
5da57f42 | 762 | } else if (exts->action && tb[exts->action]) { |
22dc13c8 WC |
763 | LIST_HEAD(actions); |
764 | int err, i = 0; | |
765 | ||
5da57f42 | 766 | err = tcf_action_init(net, tb[exts->action], rate_tlv, |
5a7a5555 JHS |
767 | NULL, ovr, TCA_ACT_BIND, |
768 | &actions); | |
33be6271 WC |
769 | if (err) |
770 | return err; | |
22dc13c8 WC |
771 | list_for_each_entry(act, &actions, list) |
772 | exts->actions[i++] = act; | |
773 | exts->nr_actions = i; | |
1da177e4 LT |
774 | } |
775 | } | |
1da177e4 | 776 | #else |
5da57f42 WC |
777 | if ((exts->action && tb[exts->action]) || |
778 | (exts->police && tb[exts->police])) | |
1da177e4 LT |
779 | return -EOPNOTSUPP; |
780 | #endif | |
781 | ||
782 | return 0; | |
783 | } | |
aa767bfe | 784 | EXPORT_SYMBOL(tcf_exts_validate); |
1da177e4 | 785 | |
aa767bfe SH |
786 | void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst, |
787 | struct tcf_exts *src) | |
1da177e4 LT |
788 | { |
789 | #ifdef CONFIG_NET_CLS_ACT | |
22dc13c8 WC |
790 | struct tcf_exts old = *dst; |
791 | ||
a49eb42a | 792 | tcf_tree_lock(tp); |
22dc13c8 WC |
793 | dst->nr_actions = src->nr_actions; |
794 | dst->actions = src->actions; | |
5301e3e1 | 795 | dst->type = src->type; |
a49eb42a | 796 | tcf_tree_unlock(tp); |
22dc13c8 WC |
797 | |
798 | tcf_exts_destroy(&old); | |
1da177e4 LT |
799 | #endif |
800 | } | |
aa767bfe | 801 | EXPORT_SYMBOL(tcf_exts_change); |
1da177e4 | 802 | |
22dc13c8 WC |
803 | #ifdef CONFIG_NET_CLS_ACT |
804 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) | |
805 | { | |
806 | if (exts->nr_actions == 0) | |
807 | return NULL; | |
808 | else | |
809 | return exts->actions[0]; | |
810 | } | |
811 | #endif | |
33be6271 | 812 | |
5da57f42 | 813 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
814 | { |
815 | #ifdef CONFIG_NET_CLS_ACT | |
9cc63db5 CW |
816 | struct nlattr *nest; |
817 | ||
22dc13c8 | 818 | if (exts->action && exts->nr_actions) { |
1da177e4 LT |
819 | /* |
820 | * again for backward compatible mode - we want | |
821 | * to work with both old and new modes of entering | |
822 | * tc data even if iproute2 was newer - jhs | |
823 | */ | |
33be6271 | 824 | if (exts->type != TCA_OLD_COMPAT) { |
22dc13c8 WC |
825 | LIST_HEAD(actions); |
826 | ||
5da57f42 | 827 | nest = nla_nest_start(skb, exts->action); |
4b3550ef PM |
828 | if (nest == NULL) |
829 | goto nla_put_failure; | |
22dc13c8 WC |
830 | |
831 | tcf_exts_to_list(exts, &actions); | |
832 | if (tcf_action_dump(skb, &actions, 0, 0) < 0) | |
add93b61 | 833 | goto nla_put_failure; |
4b3550ef | 834 | nla_nest_end(skb, nest); |
5da57f42 | 835 | } else if (exts->police) { |
33be6271 | 836 | struct tc_action *act = tcf_exts_first_act(exts); |
5da57f42 | 837 | nest = nla_nest_start(skb, exts->police); |
63acd680 | 838 | if (nest == NULL || !act) |
4b3550ef | 839 | goto nla_put_failure; |
33be6271 | 840 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
add93b61 | 841 | goto nla_put_failure; |
4b3550ef | 842 | nla_nest_end(skb, nest); |
1da177e4 LT |
843 | } |
844 | } | |
1da177e4 | 845 | return 0; |
9cc63db5 CW |
846 | |
847 | nla_put_failure: | |
848 | nla_nest_cancel(skb, nest); | |
1da177e4 | 849 | return -1; |
9cc63db5 CW |
850 | #else |
851 | return 0; | |
852 | #endif | |
1da177e4 | 853 | } |
aa767bfe | 854 | EXPORT_SYMBOL(tcf_exts_dump); |
1da177e4 | 855 | |
aa767bfe | 856 | |
5da57f42 | 857 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
858 | { |
859 | #ifdef CONFIG_NET_CLS_ACT | |
33be6271 | 860 | struct tc_action *a = tcf_exts_first_act(exts); |
b057df24 | 861 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
33be6271 | 862 | return -1; |
1da177e4 LT |
863 | #endif |
864 | return 0; | |
1da177e4 | 865 | } |
aa767bfe | 866 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
1da177e4 | 867 | |
7091d8c7 HHZ |
868 | int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts, |
869 | struct net_device **hw_dev) | |
870 | { | |
871 | #ifdef CONFIG_NET_CLS_ACT | |
872 | const struct tc_action *a; | |
873 | LIST_HEAD(actions); | |
874 | ||
875 | if (tc_no_actions(exts)) | |
876 | return -EINVAL; | |
877 | ||
878 | tcf_exts_to_list(exts, &actions); | |
879 | list_for_each_entry(a, &actions, list) { | |
880 | if (a->ops->get_dev) { | |
881 | a->ops->get_dev(a, dev_net(dev), hw_dev); | |
882 | break; | |
883 | } | |
884 | } | |
885 | if (*hw_dev) | |
886 | return 0; | |
887 | #endif | |
888 | return -EOPNOTSUPP; | |
889 | } | |
890 | EXPORT_SYMBOL(tcf_exts_get_dev); | |
891 | ||
1da177e4 LT |
892 | static int __init tc_filter_init(void) |
893 | { | |
c7ac8679 GR |
894 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL); |
895 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL); | |
82623c0d | 896 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter, |
c7ac8679 | 897 | tc_dump_tfilter, NULL); |
1da177e4 | 898 | |
1da177e4 LT |
899 | return 0; |
900 | } | |
901 | ||
902 | subsys_initcall(tc_filter_init); |