blob: 87be2b2fc29a666007b93cc8314103d8fb5aaa37 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/kmod.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070025#include <linux/netlink.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110026#include <net/net_namespace.h>
27#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/pkt_sched.h>
30#include <net/pkt_cls.h>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/* The list of all installed classifier types */
33
Patrick McHardy2eb9d752008-01-22 22:10:42 -080034static struct tcf_proto_ops *tcf_proto_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* Protects list of registered TC modules. It is pure SMP lock. */
37static DEFINE_RWLOCK(cls_mod_lock);
38
39/* Find classifier type by string name */
40
Patrick McHardyadd93b62008-01-22 22:11:33 -080041static struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 struct tcf_proto_ops *t = NULL;
44
45 if (kind) {
46 read_lock(&cls_mod_lock);
47 for (t = tcf_proto_base; t; t = t->next) {
Patrick McHardyadd93b62008-01-22 22:11:33 -080048 if (nla_strcmp(kind, t->kind) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!try_module_get(t->owner))
50 t = NULL;
51 break;
52 }
53 }
54 read_unlock(&cls_mod_lock);
55 }
56 return t;
57}
58
59/* Register(unregister) new classifier type */
60
61int register_tcf_proto_ops(struct tcf_proto_ops *ops)
62{
63 struct tcf_proto_ops *t, **tp;
64 int rc = -EEXIST;
65
66 write_lock(&cls_mod_lock);
67 for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
68 if (!strcmp(ops->kind, t->kind))
69 goto out;
70
71 ops->next = NULL;
72 *tp = ops;
73 rc = 0;
74out:
75 write_unlock(&cls_mod_lock);
76 return rc;
77}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -080078EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
81{
82 struct tcf_proto_ops *t, **tp;
83 int rc = -ENOENT;
84
85 write_lock(&cls_mod_lock);
86 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
87 if (t == ops)
88 break;
89
90 if (!t)
91 goto out;
92 *tp = t->next;
93 rc = 0;
94out:
95 write_unlock(&cls_mod_lock);
96 return rc;
97}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -080098EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
101 struct tcf_proto *tp, unsigned long fh, int event);
102
103
104/* Select new prio value from the range, managed by kernel. */
105
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800106static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800108 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (tp)
111 first = tp->prio-1;
112
113 return first;
114}
115
116/* Add/change/delete/get a filter node */
117
118static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
119{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100120 struct net *net = skb->sk->sk_net;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800121 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct tcmsg *t;
123 u32 protocol;
124 u32 prio;
125 u32 nprio;
126 u32 parent;
127 struct net_device *dev;
128 struct Qdisc *q;
129 struct tcf_proto **back, **chain;
130 struct tcf_proto *tp;
131 struct tcf_proto_ops *tp_ops;
Eric Dumazet20fea082007-11-14 01:44:41 -0800132 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned long cl;
134 unsigned long fh;
135 int err;
136
Denis V. Lunevb8542722007-12-01 00:21:31 +1100137 if (net != &init_net)
138 return -EINVAL;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140replay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 t = NLMSG_DATA(n);
142 protocol = TC_H_MIN(t->tcm_info);
143 prio = TC_H_MAJ(t->tcm_info);
144 nprio = prio;
145 parent = t->tcm_parent;
146 cl = 0;
147
148 if (prio == 0) {
149 /* If no priority is given, user wants we allocated it. */
150 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
151 return -ENOENT;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800152 prio = TC_H_MAKE(0x80000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 /* Find head of filter chain. */
156
157 /* Find link */
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800158 dev = __dev_get_by_index(&init_net, t->tcm_ifindex);
159 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -ENODEV;
161
Patrick McHardyadd93b62008-01-22 22:11:33 -0800162 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
163 if (err < 0)
164 return err;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Find qdisc */
167 if (!parent) {
168 q = dev->qdisc_sleeping;
169 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800170 } else {
171 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
172 if (q == NULL)
173 return -EINVAL;
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* Is it classful? */
177 if ((cops = q->ops->cl_ops) == NULL)
178 return -EINVAL;
179
180 /* Do we search for filter, attached to class? */
181 if (TC_H_MIN(parent)) {
182 cl = cops->get(q, parent);
183 if (cl == 0)
184 return -ENOENT;
185 }
186
187 /* And the last stroke */
188 chain = cops->tcf_chain(q, cl);
189 err = -EINVAL;
190 if (chain == NULL)
191 goto errout;
192
193 /* Check the chain for existence of proto-tcf with this priority */
194 for (back = chain; (tp=*back) != NULL; back = &tp->next) {
195 if (tp->prio >= prio) {
196 if (tp->prio == prio) {
197 if (!nprio || (tp->protocol != protocol && protocol))
198 goto errout;
199 } else
200 tp = NULL;
201 break;
202 }
203 }
204
205 if (tp == NULL) {
206 /* Proto-tcf does not exist, create new one */
207
Patrick McHardyadd93b62008-01-22 22:11:33 -0800208 if (tca[TCA_KIND] == NULL || !protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto errout;
210
211 err = -ENOENT;
212 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
213 goto errout;
214
215
216 /* Create new proto tcf */
217
218 err = -ENOBUFS;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800219 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
220 if (tp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto errout;
222 err = -EINVAL;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800223 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (tp_ops == NULL) {
225#ifdef CONFIG_KMOD
Patrick McHardyadd93b62008-01-22 22:11:33 -0800226 struct nlattr *kind = tca[TCA_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 char name[IFNAMSIZ];
228
229 if (kind != NULL &&
Patrick McHardyadd93b62008-01-22 22:11:33 -0800230 nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 rtnl_unlock();
232 request_module("cls_%s", name);
233 rtnl_lock();
234 tp_ops = tcf_proto_lookup_ops(kind);
235 /* We dropped the RTNL semaphore in order to
236 * perform the module load. So, even if we
237 * succeeded in loading the module we have to
238 * replay the request. We indicate this using
239 * -EAGAIN.
240 */
241 if (tp_ops != NULL) {
242 module_put(tp_ops->owner);
243 err = -EAGAIN;
244 }
245 }
246#endif
247 kfree(tp);
248 goto errout;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 tp->ops = tp_ops;
251 tp->protocol = protocol;
252 tp->prio = nprio ? : tcf_auto_prio(*back);
253 tp->q = q;
254 tp->classify = tp_ops->classify;
255 tp->classid = parent;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800256
257 err = tp_ops->init(tp);
258 if (err != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 module_put(tp_ops->owner);
260 kfree(tp);
261 goto errout;
262 }
263
264 qdisc_lock_tree(dev);
265 tp->next = *back;
266 *back = tp;
267 qdisc_unlock_tree(dev);
268
Patrick McHardyadd93b62008-01-22 22:11:33 -0800269 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto errout;
271
272 fh = tp->ops->get(tp, t->tcm_handle);
273
274 if (fh == 0) {
275 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
276 qdisc_lock_tree(dev);
277 *back = tp->next;
278 qdisc_unlock_tree(dev);
279
280 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
281 tcf_destroy(tp);
282 err = 0;
283 goto errout;
284 }
285
286 err = -ENOENT;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800287 if (n->nlmsg_type != RTM_NEWTFILTER ||
288 !(n->nlmsg_flags & NLM_F_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto errout;
290 } else {
291 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900292 case RTM_NEWTFILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 err = -EEXIST;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800294 if (n->nlmsg_flags & NLM_F_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 goto errout;
296 break;
297 case RTM_DELTFILTER:
298 err = tp->ops->delete(tp, fh);
299 if (err == 0)
300 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
301 goto errout;
302 case RTM_GETTFILTER:
303 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
304 goto errout;
305 default:
306 err = -EINVAL;
307 goto errout;
308 }
309 }
310
311 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
312 if (err == 0)
313 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
314
315errout:
316 if (cl)
317 cops->put(q, cl);
318 if (err == -EAGAIN)
319 /* Replay the request. */
320 goto replay;
321 return err;
322}
323
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800324static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp,
325 unsigned long fh, u32 pid, u32 seq, u16 flags, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct tcmsg *tcm;
328 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700329 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700331 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 tcm = NLMSG_DATA(nlh);
333 tcm->tcm_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700334 tcm->tcm__pad1 = 0;
335 tcm->tcm__pad1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 tcm->tcm_ifindex = tp->q->dev->ifindex;
337 tcm->tcm_parent = tp->classid;
338 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800339 NLA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 tcm->tcm_handle = fh;
341 if (RTM_DELTFILTER != event) {
342 tcm->tcm_handle = 0;
343 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800344 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700346 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return skb->len;
348
349nlmsg_failure:
Patrick McHardyadd93b62008-01-22 22:11:33 -0800350nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700351 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -1;
353}
354
355static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
356 struct tcf_proto *tp, unsigned long fh, int event)
357{
358 struct sk_buff *skb;
359 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
360
361 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
362 if (!skb)
363 return -ENOBUFS;
364
365 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
366 kfree_skb(skb);
367 return -EINVAL;
368 }
369
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800370 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
371 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800374struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct tcf_walker w;
376 struct sk_buff *skb;
377 struct netlink_callback *cb;
378};
379
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800380static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
381 struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800383 struct tcf_dump_args *a = (void *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
386 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
387}
388
389static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
390{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100391 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int t;
393 int s_t;
394 struct net_device *dev;
395 struct Qdisc *q;
396 struct tcf_proto *tp, **chain;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800397 struct tcmsg *tcm = (struct tcmsg *)NLMSG_DATA(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -0800399 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct tcf_dump_args arg;
401
Denis V. Lunevb8542722007-12-01 00:21:31 +1100402 if (net != &init_net)
403 return 0;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
406 return skb->len;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700407 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return skb->len;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!tcm->tcm_parent)
411 q = dev->qdisc_sleeping;
412 else
413 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
414 if (!q)
415 goto out;
416 if ((cops = q->ops->cl_ops) == NULL)
417 goto errout;
418 if (TC_H_MIN(tcm->tcm_parent)) {
419 cl = cops->get(q, tcm->tcm_parent);
420 if (cl == 0)
421 goto errout;
422 }
423 chain = cops->tcf_chain(q, cl);
424 if (chain == NULL)
425 goto errout;
426
427 s_t = cb->args[0];
428
429 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
430 if (t < s_t) continue;
431 if (TC_H_MAJ(tcm->tcm_info) &&
432 TC_H_MAJ(tcm->tcm_info) != tp->prio)
433 continue;
434 if (TC_H_MIN(tcm->tcm_info) &&
435 TC_H_MIN(tcm->tcm_info) != tp->protocol)
436 continue;
437 if (t > s_t)
438 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
439 if (cb->args[1] == 0) {
440 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800441 cb->nlh->nlmsg_seq, NLM_F_MULTI,
442 RTM_NEWTFILTER) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 cb->args[1] = 1;
446 }
447 if (tp->ops->walk == NULL)
448 continue;
449 arg.w.fn = tcf_node_dump;
450 arg.skb = skb;
451 arg.cb = cb;
452 arg.w.stop = 0;
453 arg.w.skip = cb->args[1]-1;
454 arg.w.count = 0;
455 tp->ops->walk(tp, &arg.w);
456 cb->args[1] = arg.w.count+1;
457 if (arg.w.stop)
458 break;
459 }
460
461 cb->args[0] = t;
462
463errout:
464 if (cl)
465 cops->put(q, cl);
466out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 dev_put(dev);
468 return skb->len;
469}
470
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800471void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473#ifdef CONFIG_NET_CLS_ACT
474 if (exts->action) {
475 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
476 exts->action = NULL;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#endif
479}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800480EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Patrick McHardyadd93b62008-01-22 22:11:33 -0800482int tcf_exts_validate(struct tcf_proto *tp, struct nlattr **tb,
483 struct nlattr *rate_tlv, struct tcf_exts *exts,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900484 struct tcf_ext_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 memset(exts, 0, sizeof(*exts));
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#ifdef CONFIG_NET_CLS_ACT
489 {
490 int err;
491 struct tc_action *act;
492
Patrick McHardyadd93b62008-01-22 22:11:33 -0800493 if (map->police && tb[map->police]) {
494 act = tcf_action_init_1((struct rtattr *)tb[map->police],
495 (struct rtattr *)rate_tlv,
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800496 "police", TCA_ACT_NOREPLACE,
497 TCA_ACT_BIND, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (act == NULL)
499 return err;
500
501 act->type = TCA_OLD_COMPAT;
502 exts->action = act;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800503 } else if (map->action && tb[map->action]) {
504 act = tcf_action_init((struct rtattr *)tb[map->action],
505 (struct rtattr *)rate_tlv, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
507 if (act == NULL)
508 return err;
509
510 exts->action = act;
511 }
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#else
Patrick McHardyadd93b62008-01-22 22:11:33 -0800514 if ((map->action && tb[map->action]) ||
515 (map->police && tb[map->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -EOPNOTSUPP;
517#endif
518
519 return 0;
520}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800521EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800523void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
524 struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526#ifdef CONFIG_NET_CLS_ACT
527 if (src->action) {
528 struct tc_action *act;
529 tcf_tree_lock(tp);
530 act = xchg(&dst->action, src->action);
531 tcf_tree_unlock(tp);
532 if (act)
533 tcf_action_destroy(act, TCA_ACT_UNBIND);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#endif
536}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800537EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800539int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct tcf_ext_map *map)
541{
542#ifdef CONFIG_NET_CLS_ACT
543 if (map->action && exts->action) {
544 /*
545 * again for backward compatible mode - we want
546 * to work with both old and new modes of entering
547 * tc data even if iproute2 was newer - jhs
548 */
Patrick McHardyadd93b62008-01-22 22:11:33 -0800549 struct nlattr *p_rta = (struct nlattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (exts->action->type != TCA_OLD_COMPAT) {
Patrick McHardyadd93b62008-01-22 22:11:33 -0800552 NLA_PUT(skb, map->action, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800554 goto nla_put_failure;
555 p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 } else if (map->police) {
Patrick McHardyadd93b62008-01-22 22:11:33 -0800557 NLA_PUT(skb, map->police, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800559 goto nla_put_failure;
560 p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#endif
564 return 0;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800565nla_put_failure: __attribute__ ((unused))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -1;
567}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800568EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800570
571int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
572 struct tcf_ext_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574#ifdef CONFIG_NET_CLS_ACT
575 if (exts->action)
576 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800577 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579 return 0;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800580nla_put_failure: __attribute__ ((unused))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return -1;
582}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800583EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585static int __init tc_filter_init(void)
586{
Thomas Graf82623c02007-03-22 11:56:22 -0700587 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL);
588 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL);
589 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
590 tc_dump_tfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593}
594
595subsys_initcall(tc_filter_init);