blob: 43ec92680ae8fe30d14496f5cf81a1baf53b49e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/act_api.c Packet action 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 * Author: Jamal Hadi Salim
10 *
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/kmod.h>
Patrick McHardyab27cfb2008-01-23 20:33:13 -080022#include <linux/err.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040023#include <linux/module.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110024#include <net/net_namespace.h>
25#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sch_generic.h>
27#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
WANG Cong86062032014-02-11 17:07:31 -080030void tcf_hash_destroy(struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070031{
WANG Cong86062032014-02-11 17:07:31 -080032 struct tcf_common *p = a->priv;
33 struct tcf_hashinfo *hinfo = a->ops->hinfo;
34
WANG Cong89819dc2013-12-15 20:15:09 -080035 spin_lock_bh(&hinfo->lock);
36 hlist_del(&p->tcfc_head);
37 spin_unlock_bh(&hinfo->lock);
38 gen_kill_estimator(&p->tcfc_bstats,
39 &p->tcfc_rate_est);
40 /*
41 * gen_estimator est_timer() might access p->tcfc_lock
42 * or bstats, wait a RCU grace period before freeing p
43 */
44 kfree_rcu(p, tcfc_rcu);
David S. Millere9ce1cd2006-08-21 23:54:55 -070045}
46EXPORT_SYMBOL(tcf_hash_destroy);
47
Daniel Borkmann28e6b672015-07-29 23:35:25 +020048int __tcf_hash_release(struct tc_action *a, bool bind, bool strict)
David S. Millere9ce1cd2006-08-21 23:54:55 -070049{
WANG Cong86062032014-02-11 17:07:31 -080050 struct tcf_common *p = a->priv;
David S. Millere9ce1cd2006-08-21 23:54:55 -070051 int ret = 0;
52
53 if (p) {
54 if (bind)
55 p->tcfc_bindcnt--;
Daniel Borkmann28e6b672015-07-29 23:35:25 +020056 else if (strict && p->tcfc_bindcnt > 0)
WANG Cong55334a52014-02-11 17:07:34 -080057 return -EPERM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070058
59 p->tcfc_refcnt--;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090060 if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
WANG Conga5b5c952014-02-11 17:07:32 -080061 if (a->ops->cleanup)
62 a->ops->cleanup(a, bind);
WANG Cong86062032014-02-11 17:07:31 -080063 tcf_hash_destroy(a);
David S. Millere9ce1cd2006-08-21 23:54:55 -070064 ret = 1;
65 }
66 }
Daniel Borkmann28e6b672015-07-29 23:35:25 +020067
David S. Millere9ce1cd2006-08-21 23:54:55 -070068 return ret;
69}
Daniel Borkmann28e6b672015-07-29 23:35:25 +020070EXPORT_SYMBOL(__tcf_hash_release);
David S. Millere9ce1cd2006-08-21 23:54:55 -070071
72static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
WANG Congc779f7a2014-01-17 11:37:02 -080073 struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -070074{
WANG Congc779f7a2014-01-17 11:37:02 -080075 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -080076 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -070077 struct tcf_common *p;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000078 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080079 struct nlattr *nest;
David S. Millere9ce1cd2006-08-21 23:54:55 -070080
WANG Cong89819dc2013-12-15 20:15:09 -080081 spin_lock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -070082
83 s_i = cb->args[0];
84
85 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -080086 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
David S. Millere9ce1cd2006-08-21 23:54:55 -070087
WANG Cong89819dc2013-12-15 20:15:09 -080088 hlist_for_each_entry_rcu(p, head, tcfc_head) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070089 index++;
90 if (index < s_i)
91 continue;
92 a->priv = p;
93 a->order = n_i;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -080094
95 nest = nla_nest_start(skb, a->order);
96 if (nest == NULL)
97 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -070098 err = tcf_action_dump_1(skb, a, 0, 0);
99 if (err < 0) {
100 index--;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800101 nlmsg_trim(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700102 goto done;
103 }
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800104 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105 n_i++;
106 if (n_i >= TCA_ACT_MAX_PRIO)
107 goto done;
108 }
109 }
110done:
WANG Cong89819dc2013-12-15 20:15:09 -0800111 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700112 if (n_i)
113 cb->args[0] += n_i;
114 return n_i;
115
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800116nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800117 nla_nest_cancel(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700118 goto done;
119}
120
WANG Congc779f7a2014-01-17 11:37:02 -0800121static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122{
WANG Congc779f7a2014-01-17 11:37:02 -0800123 struct tcf_hashinfo *hinfo = a->ops->hinfo;
WANG Cong89819dc2013-12-15 20:15:09 -0800124 struct hlist_head *head;
125 struct hlist_node *n;
126 struct tcf_common *p;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800127 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000128 int i = 0, n_i = 0;
WANG Cong55334a52014-02-11 17:07:34 -0800129 int ret = -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700130
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800131 nest = nla_nest_start(skb, a->order);
132 if (nest == NULL)
133 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400134 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
135 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700136 for (i = 0; i < (hinfo->hmask + 1); i++) {
WANG Cong89819dc2013-12-15 20:15:09 -0800137 head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
138 hlist_for_each_entry_safe(p, n, head, tcfc_head) {
WANG Cong86062032014-02-11 17:07:31 -0800139 a->priv = p;
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200140 ret = __tcf_hash_release(a, false, true);
WANG Cong55334a52014-02-11 17:07:34 -0800141 if (ret == ACT_P_DELETED) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000142 module_put(a->ops->owner);
Jamal Hadi Salim805c1f42013-12-23 08:02:13 -0500143 n_i++;
WANG Cong55334a52014-02-11 17:07:34 -0800144 } else if (ret < 0)
145 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700146 }
147 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400148 if (nla_put_u32(skb, TCA_FCNT, n_i))
149 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800150 nla_nest_end(skb, nest);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700151
152 return n_i;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800153nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800154 nla_nest_cancel(skb, nest);
WANG Cong55334a52014-02-11 17:07:34 -0800155 return ret;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700156}
157
stephen hemminger9c75f402013-12-31 11:54:00 -0800158static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
159 int type, struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700160{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700161 if (type == RTM_DELACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800162 return tcf_del_walker(skb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700163 } else if (type == RTM_GETACTION) {
WANG Congc779f7a2014-01-17 11:37:02 -0800164 return tcf_dump_walker(skb, cb, a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700165 } else {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000166 WARN(1, "tcf_generic_walker: unknown action %d\n", type);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700167 return -EINVAL;
168 }
169}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170
WANG Cong6e6a50c2014-01-17 11:37:03 -0800171static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700172{
WANG Cong89819dc2013-12-15 20:15:09 -0800173 struct tcf_common *p = NULL;
174 struct hlist_head *head;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700175
WANG Cong89819dc2013-12-15 20:15:09 -0800176 spin_lock_bh(&hinfo->lock);
177 head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
178 hlist_for_each_entry_rcu(p, head, tcfc_head)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700179 if (p->tcfc_index == index)
180 break;
WANG Cong89819dc2013-12-15 20:15:09 -0800181 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700182
183 return p;
184}
David S. Millere9ce1cd2006-08-21 23:54:55 -0700185
WANG Congddafd342014-01-09 16:13:59 -0800186u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187{
WANG Congddafd342014-01-09 16:13:59 -0800188 u32 val = hinfo->index;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189
190 do {
191 if (++val == 0)
192 val = 1;
193 } while (tcf_hash_lookup(val, hinfo));
194
WANG Congddafd342014-01-09 16:13:59 -0800195 hinfo->index = val;
Yang Yingliang17569fa2013-12-10 20:55:29 +0800196 return val;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197}
198EXPORT_SYMBOL(tcf_hash_new_index);
199
WANG Cong6e6a50c2014-01-17 11:37:03 -0800200int tcf_hash_search(struct tc_action *a, u32 index)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201{
202 struct tcf_hashinfo *hinfo = a->ops->hinfo;
203 struct tcf_common *p = tcf_hash_lookup(index, hinfo);
204
205 if (p) {
206 a->priv = p;
207 return 1;
208 }
209 return 0;
210}
WANG Cong6e6a50c2014-01-17 11:37:03 -0800211EXPORT_SYMBOL(tcf_hash_search);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700212
WANG Cong86062032014-02-11 17:07:31 -0800213int tcf_hash_check(u32 index, struct tc_action *a, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214{
WANG Congc779f7a2014-01-17 11:37:02 -0800215 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700216 struct tcf_common *p = NULL;
217 if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700218 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700219 p->tcfc_bindcnt++;
Jamal Hadi Salim76aab2c2008-08-07 20:37:22 -0700220 p->tcfc_refcnt++;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221 a->priv = p;
WANG Cong86062032014-02-11 17:07:31 -0800222 return 1;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700223 }
WANG Cong86062032014-02-11 17:07:31 -0800224 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700225}
226EXPORT_SYMBOL(tcf_hash_check);
227
WANG Cong86062032014-02-11 17:07:31 -0800228void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
229{
230 struct tcf_common *pc = a->priv;
231 if (est)
232 gen_kill_estimator(&pc->tcfc_bstats,
233 &pc->tcfc_rate_est);
234 kfree_rcu(pc, tcfc_rcu);
235}
236EXPORT_SYMBOL(tcf_hash_cleanup);
237
238int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a,
239 int size, int bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700240{
WANG Congc779f7a2014-01-17 11:37:02 -0800241 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700242 struct tcf_common *p = kzalloc(size, GFP_KERNEL);
243
244 if (unlikely(!p))
WANG Cong86062032014-02-11 17:07:31 -0800245 return -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700246 p->tcfc_refcnt = 1;
247 if (bind)
248 p->tcfc_bindcnt = 1;
249
250 spin_lock_init(&p->tcfc_lock);
WANG Cong89819dc2013-12-15 20:15:09 -0800251 INIT_HLIST_NODE(&p->tcfc_head);
WANG Congddafd342014-01-09 16:13:59 -0800252 p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700253 p->tcfc_tm.install = jiffies;
254 p->tcfc_tm.lastuse = jiffies;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800255 if (est) {
John Fastabend22e0f8b2014-09-28 11:52:56 -0700256 int err = gen_new_estimator(&p->tcfc_bstats, NULL,
257 &p->tcfc_rate_est,
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800258 &p->tcfc_lock, est);
259 if (err) {
260 kfree(p);
WANG Cong86062032014-02-11 17:07:31 -0800261 return err;
Stephen Hemminger0e991ec2008-11-25 21:12:32 -0800262 }
263 }
264
David S. Millere9ce1cd2006-08-21 23:54:55 -0700265 a->priv = (void *) p;
WANG Cong86062032014-02-11 17:07:31 -0800266 return 0;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700267}
268EXPORT_SYMBOL(tcf_hash_create);
269
WANG Cong86062032014-02-11 17:07:31 -0800270void tcf_hash_insert(struct tc_action *a)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700271{
WANG Cong86062032014-02-11 17:07:31 -0800272 struct tcf_common *p = a->priv;
273 struct tcf_hashinfo *hinfo = a->ops->hinfo;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700274 unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
275
WANG Cong89819dc2013-12-15 20:15:09 -0800276 spin_lock_bh(&hinfo->lock);
277 hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
278 spin_unlock_bh(&hinfo->lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700279}
280EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
WANG Cong1f747c22013-12-15 20:15:10 -0800282static LIST_HEAD(act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static DEFINE_RWLOCK(act_mod_lock);
284
WANG Cong4f1e9d82014-02-11 17:07:33 -0800285int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
WANG Cong1f747c22013-12-15 20:15:10 -0800287 struct tc_action_ops *a;
WANG Cong4f1e9d82014-02-11 17:07:33 -0800288 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
WANG Conga5b5c952014-02-11 17:07:32 -0800290 /* Must supply act, dump and init */
291 if (!act->act || !act->dump || !act->init)
Jamal Hadi Salim76c82d72013-12-04 09:26:52 -0500292 return -EINVAL;
293
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500294 /* Supply defaults */
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500295 if (!act->lookup)
296 act->lookup = tcf_hash_search;
Jamal Hadi Salim382ca8a2013-12-04 09:26:55 -0500297 if (!act->walk)
298 act->walk = tcf_generic_walker;
Jamal Hadi Salim63ef6172013-12-04 09:26:53 -0500299
WANG Cong4f1e9d82014-02-11 17:07:33 -0800300 act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
301 if (!act->hinfo)
302 return -ENOMEM;
303 err = tcf_hashinfo_init(act->hinfo, mask);
304 if (err) {
305 kfree(act->hinfo);
306 return err;
307 }
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 write_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800310 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
312 write_unlock(&act_mod_lock);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800313 tcf_hashinfo_destroy(act->hinfo);
314 kfree(act->hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -EEXIST;
316 }
317 }
WANG Cong1f747c22013-12-15 20:15:10 -0800318 list_add_tail(&act->head, &act_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 write_unlock(&act_mod_lock);
320 return 0;
321}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800322EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324int tcf_unregister_action(struct tc_action_ops *act)
325{
WANG Cong1f747c22013-12-15 20:15:10 -0800326 struct tc_action_ops *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int err = -ENOENT;
328
329 write_lock(&act_mod_lock);
Eric Dumazeta7928662013-12-20 12:32:32 -0800330 list_for_each_entry(a, &act_base, head) {
331 if (a == act) {
332 list_del(&act->head);
WANG Cong4f1e9d82014-02-11 17:07:33 -0800333 tcf_hashinfo_destroy(act->hinfo);
334 kfree(act->hinfo);
Eric Dumazeta7928662013-12-20 12:32:32 -0800335 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
Eric Dumazeta7928662013-12-20 12:32:32 -0800337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 write_unlock(&act_mod_lock);
340 return err;
341}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800342EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344/* lookup by name */
345static struct tc_action_ops *tc_lookup_action_n(char *kind)
346{
Eric Dumazeta7928662013-12-20 12:32:32 -0800347 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (kind) {
350 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800351 list_for_each_entry(a, &act_base, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800353 if (try_module_get(a->owner))
354 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 }
357 }
358 read_unlock(&act_mod_lock);
359 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800360 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800363/* lookup by nlattr */
364static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Eric Dumazeta7928662013-12-20 12:32:32 -0800366 struct tc_action_ops *a, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (kind) {
369 read_lock(&act_mod_lock);
WANG Cong1f747c22013-12-15 20:15:10 -0800370 list_for_each_entry(a, &act_base, head) {
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800371 if (nla_strcmp(kind, a->kind) == 0) {
Eric Dumazeta7928662013-12-20 12:32:32 -0800372 if (try_module_get(a->owner))
373 res = a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375 }
376 }
377 read_unlock(&act_mod_lock);
378 }
Eric Dumazeta7928662013-12-20 12:32:32 -0800379 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
WANG Cong33be6272013-12-15 20:15:05 -0800382int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900383 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000385 const struct tc_action *a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int ret = -1;
387
388 if (skb->tc_verd & TC_NCLS) {
389 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 ret = TC_ACT_OK;
391 goto exec_done;
392 }
WANG Cong33be6272013-12-15 20:15:05 -0800393 list_for_each_entry(a, actions, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394repeat:
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500395 ret = a->ops->act(skb, a, res);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500396 if (ret == TC_ACT_REPEAT)
397 goto repeat; /* we need a ttl - JHS */
398 if (ret != TC_ACT_PIPE)
399 goto exec_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401exec_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return ret;
403}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800404EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
WANG Cong55334a52014-02-11 17:07:34 -0800406int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
WANG Cong33be6272013-12-15 20:15:05 -0800408 struct tc_action *a, *tmp;
WANG Cong55334a52014-02-11 17:07:34 -0800409 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
WANG Cong33be6272013-12-15 20:15:05 -0800411 list_for_each_entry_safe(a, tmp, actions, list) {
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200412 ret = __tcf_hash_release(a, bind, true);
WANG Cong55334a52014-02-11 17:07:34 -0800413 if (ret == ACT_P_DELETED)
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500414 module_put(a->ops->owner);
WANG Cong55334a52014-02-11 17:07:34 -0800415 else if (ret < 0)
416 return ret;
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500417 list_del(&a->list);
418 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
WANG Cong55334a52014-02-11 17:07:34 -0800420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423int
424tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
425{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return a->ops->dump(skb, a, bind, ref);
427}
428
429int
430tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
431{
432 int err = -EINVAL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700433 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800434 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David S. Miller1b34ec42012-03-29 05:11:39 -0400436 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
437 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (tcf_action_copy_stats(skb, a, 0))
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800439 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800440 nest = nla_nest_start(skb, TCA_OPTIONS);
441 if (nest == NULL)
442 goto nla_put_failure;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000443 err = tcf_action_dump_old(skb, a, bind, ref);
444 if (err > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800445 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return err;
447 }
448
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800449nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700450 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -1;
452}
Patrick McHardy62e3ba12008-01-22 22:10:23 -0800453EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455int
WANG Cong33be6272013-12-15 20:15:05 -0800456tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct tc_action *a;
459 int err = -EINVAL;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800460 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
WANG Cong33be6272013-12-15 20:15:05 -0800462 list_for_each_entry(a, actions, list) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800463 nest = nla_nest_start(skb, a->order);
464 if (nest == NULL)
465 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 err = tcf_action_dump_1(skb, a, bind, ref);
467 if (err < 0)
Thomas Graf4fe683f2006-07-05 20:47:28 -0700468 goto errout;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800469 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 return 0;
473
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800474nla_put_failure:
Thomas Graf4fe683f2006-07-05 20:47:28 -0700475 err = -EINVAL;
476errout:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800477 nla_nest_cancel(skb, nest);
Thomas Graf4fe683f2006-07-05 20:47:28 -0700478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000481struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
482 struct nlattr *est, char *name, int ovr,
483 int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct tc_action *a;
486 struct tc_action_ops *a_o;
487 char act_name[IFNAMSIZ];
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000488 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800489 struct nlattr *kind;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800490 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (name == NULL) {
Patrick McHardycee63722008-01-23 20:33:32 -0800493 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
494 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto err_out;
Patrick McHardycee63722008-01-23 20:33:32 -0800496 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800497 kind = tb[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (kind == NULL)
499 goto err_out;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800500 if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto err_out;
502 } else {
Patrick McHardycee63722008-01-23 20:33:32 -0800503 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
505 goto err_out;
506 }
507
508 a_o = tc_lookup_action_n(act_name);
509 if (a_o == NULL) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700510#ifdef CONFIG_MODULES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 rtnl_unlock();
Patrick McHardy4bba3922006-01-08 22:22:14 -0800512 request_module("act_%s", act_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 rtnl_lock();
514
515 a_o = tc_lookup_action_n(act_name);
516
517 /* We dropped the RTNL semaphore in order to
518 * perform the module load. So, even if we
519 * succeeded in loading the module we have to
520 * tell the caller to replay the request. We
521 * indicate this using -EAGAIN.
522 */
523 if (a_o != NULL) {
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800524 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto err_mod;
526 }
527#endif
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800528 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto err_out;
530 }
531
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800532 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700533 a = kzalloc(sizeof(*a), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (a == NULL)
535 goto err_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
WANG Congc779f7a2014-01-17 11:37:02 -0800537 a->ops = a_o;
WANG Cong33be6272013-12-15 20:15:05 -0800538 INIT_LIST_HEAD(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* backward compatibility for policer */
540 if (name == NULL)
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000541 err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 else
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000543 err = a_o->init(net, nla, est, a, ovr, bind);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800544 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto err_free;
546
547 /* module count goes up only when brand new policy is created
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000548 * if it exists and is only bound to in a_o->init() then
549 * ACT_P_CREATED is not returned (a zero is).
550 */
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800551 if (err != ACT_P_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 module_put(a_o->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return a;
555
556err_free:
557 kfree(a);
558err_mod:
559 module_put(a_o->owner);
560err_out:
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800561 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
WANG Cong33be6272013-12-15 20:15:05 -0800564int tcf_action_init(struct net *net, struct nlattr *nla,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000565 struct nlattr *est, char *name, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800566 int bind, struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000568 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800569 struct tc_action *act;
Patrick McHardycee63722008-01-23 20:33:32 -0800570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int i;
572
Patrick McHardycee63722008-01-23 20:33:32 -0800573 err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
574 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -0800575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800577 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000578 act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
WANG Cong33be6272013-12-15 20:15:05 -0800579 if (IS_ERR(act)) {
580 err = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto err;
WANG Cong33be6272013-12-15 20:15:05 -0800582 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800583 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800584 list_add_tail(&act->list, actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
WANG Cong33be6272013-12-15 20:15:05 -0800586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588err:
WANG Cong33be6272013-12-15 20:15:05 -0800589 tcf_action_destroy(actions, bind);
590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
594 int compat_mode)
595{
596 int err = 0;
597 struct gnet_dump d;
WANG Cong7eb88962014-01-09 16:14:05 -0800598 struct tcf_common *p = a->priv;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900599
WANG Cong7eb88962014-01-09 16:14:05 -0800600 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 goto errout;
602
603 /* compat_mode being true specifies a call that is supposed
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -0400604 * to add additional backward compatibility statistic TLVs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
606 if (compat_mode) {
607 if (a->type == TCA_OLD_COMPAT)
608 err = gnet_stats_start_copy_compat(skb, 0,
WANG Cong7eb88962014-01-09 16:14:05 -0800609 TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 else
611 return 0;
612 } else
613 err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
WANG Cong7eb88962014-01-09 16:14:05 -0800614 &p->tcfc_lock, &d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (err < 0)
617 goto errout;
618
John Fastabend22e0f8b2014-09-28 11:52:56 -0700619 if (gnet_stats_copy_basic(&d, NULL, &p->tcfc_bstats) < 0 ||
WANG Cong7eb88962014-01-09 16:14:05 -0800620 gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
621 &p->tcfc_rate_est) < 0 ||
John Fastabendb0ab6f92014-09-28 11:54:24 -0700622 gnet_stats_copy_queue(&d, NULL,
John Fastabend64015852014-09-28 11:53:57 -0700623 &p->tcfc_qstats,
624 p->tcfc_qstats.qlen) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto errout;
626
627 if (gnet_stats_finish_copy(&d) < 0)
628 goto errout;
629
630 return 0;
631
632errout:
633 return -1;
634}
635
636static int
WANG Cong33be6272013-12-15 20:15:05 -0800637tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900638 u16 flags, int event, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct tcamsg *t;
641 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700642 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800643 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Eric W. Biederman15e47302012-09-07 20:12:54 +0000645 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
David S. Miller8b00a532012-06-26 21:39:32 -0700646 if (!nlh)
647 goto out_nlmsg_trim;
648 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700650 t->tca__pad1 = 0;
651 t->tca__pad2 = 0;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900652
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800653 nest = nla_nest_start(skb, TCA_ACT_TAB);
654 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700655 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
WANG Cong33be6272013-12-15 20:15:05 -0800657 if (tcf_action_dump(skb, actions, bind, ref) < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700658 goto out_nlmsg_trim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800660 nla_nest_end(skb, nest);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900661
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700662 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return skb->len;
664
David S. Miller8b00a532012-06-26 21:39:32 -0700665out_nlmsg_trim:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700666 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -1;
668}
669
670static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000671act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
WANG Cong33be6272013-12-15 20:15:05 -0800672 struct list_head *actions, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
677 if (!skb)
678 return -ENOBUFS;
WANG Cong33be6272013-12-15 20:15:05 -0800679 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 kfree_skb(skb);
681 return -EINVAL;
682 }
Thomas Graf2942e902006-08-15 00:30:25 -0700683
Eric W. Biederman15e47302012-09-07 20:12:54 +0000684 return rtnl_unicast(skb, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
WANG Cong03701d62014-02-11 17:07:35 -0800687static struct tc_action *create_a(int i)
688{
689 struct tc_action *act;
690
691 act = kzalloc(sizeof(*act), GFP_KERNEL);
692 if (act == NULL) {
693 pr_debug("create_a: failed to alloc!\n");
694 return NULL;
695 }
696 act->order = i;
697 INIT_LIST_HEAD(&act->list);
698 return act;
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static struct tc_action *
Eric W. Biederman15e47302012-09-07 20:12:54 +0000702tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000704 struct nlattr *tb[TCA_ACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct tc_action *a;
706 int index;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800707 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Patrick McHardycee63722008-01-23 20:33:32 -0800709 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
710 if (err < 0)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800711 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick McHardycee63722008-01-23 20:33:32 -0800713 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800714 if (tb[TCA_ACT_INDEX] == NULL ||
715 nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800716 goto err_out;
Patrick McHardy1587bac2008-01-23 20:35:03 -0800717 index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800719 err = -ENOMEM;
WANG Cong03701d62014-02-11 17:07:35 -0800720 a = create_a(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (a == NULL)
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800722 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800724 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800725 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -0500726 if (a->ops == NULL) /* could happen in batch of actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 goto err_free;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800728 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (a->ops->lookup(a, index) == 0)
730 goto err_mod;
731
732 module_put(a->ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return a;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735err_mod:
736 module_put(a->ops->owner);
737err_free:
738 kfree(a);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800739err_out:
740 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
WANG Cong33be6272013-12-15 20:15:05 -0800743static void cleanup_a(struct list_head *actions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
WANG Cong33be6272013-12-15 20:15:05 -0800745 struct tc_action *a, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
WANG Cong33be6272013-12-15 20:15:05 -0800747 list_for_each_entry_safe(a, tmp, actions, list) {
748 list_del(&a->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 kfree(a);
750 }
751}
752
Tom Goff7316ae82010-03-19 15:40:13 +0000753static int tca_action_flush(struct net *net, struct nlattr *nla,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000754 struct nlmsghdr *n, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct sk_buff *skb;
757 unsigned char *b;
758 struct nlmsghdr *nlh;
759 struct tcamsg *t;
760 struct netlink_callback dcb;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800761 struct nlattr *nest;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000762 struct nlattr *tb[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800763 struct nlattr *kind;
WANG Cong03701d62014-02-11 17:07:35 -0800764 struct tc_action a;
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700765 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
768 if (!skb) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000769 pr_debug("tca_action_flush: failed skb alloc\n");
Jamal Hadi Salim36723872008-08-13 02:41:45 -0700770 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700773 b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick McHardycee63722008-01-23 20:33:32 -0800775 err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
776 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 goto err_out;
778
Patrick McHardycee63722008-01-23 20:33:32 -0800779 err = -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800780 kind = tb[TCA_ACT_KIND];
WANG Cong03701d62014-02-11 17:07:35 -0800781 memset(&a, 0, sizeof(struct tc_action));
782 INIT_LIST_HEAD(&a.list);
783 a.ops = tc_lookup_action(kind);
784 if (a.ops == NULL) /*some idjot trying to flush unknown action */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto err_out;
786
Eric W. Biederman15e47302012-09-07 20:12:54 +0000787 nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
David S. Miller8b00a532012-06-26 21:39:32 -0700788 if (!nlh)
789 goto out_module_put;
790 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700792 t->tca__pad1 = 0;
793 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800795 nest = nla_nest_start(skb, TCA_ACT_TAB);
796 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -0700797 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
WANG Cong03701d62014-02-11 17:07:35 -0800799 err = a.ops->walk(skb, &dcb, RTM_DELACTION, &a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (err < 0)
David S. Miller8b00a532012-06-26 21:39:32 -0700801 goto out_module_put;
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700802 if (err == 0)
803 goto noflush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800805 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700807 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 nlh->nlmsg_flags |= NLM_F_ROOT;
WANG Cong03701d62014-02-11 17:07:35 -0800809 module_put(a.ops->owner);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000810 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000811 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (err > 0)
813 return 0;
814
815 return err;
816
David S. Miller8b00a532012-06-26 21:39:32 -0700817out_module_put:
WANG Cong03701d62014-02-11 17:07:35 -0800818 module_put(a.ops->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819err_out:
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700820noflush_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return err;
823}
824
825static int
WANG Conga56e1952014-01-09 16:14:00 -0800826tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
827 u32 portid)
828{
829 int ret;
830 struct sk_buff *skb;
831
832 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
833 if (!skb)
834 return -ENOBUFS;
835
836 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
837 0, 1) <= 0) {
838 kfree_skb(skb);
839 return -EINVAL;
840 }
841
842 /* now do the delete */
WANG Cong55334a52014-02-11 17:07:34 -0800843 ret = tcf_action_destroy(actions, 0);
844 if (ret < 0) {
845 kfree_skb(skb);
846 return ret;
847 }
WANG Conga56e1952014-01-09 16:14:00 -0800848
849 ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
850 n->nlmsg_flags & NLM_F_ECHO);
851 if (ret > 0)
852 return 0;
853 return ret;
854}
855
856static int
Tom Goff7316ae82010-03-19 15:40:13 +0000857tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000858 u32 portid, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Patrick McHardycee63722008-01-23 20:33:32 -0800860 int i, ret;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000861 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
WANG Cong33be6272013-12-15 20:15:05 -0800862 struct tc_action *act;
863 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Patrick McHardycee63722008-01-23 20:33:32 -0800865 ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
866 if (ret < 0)
867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000869 if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700870 if (tb[1] != NULL)
Eric W. Biederman15e47302012-09-07 20:12:54 +0000871 return tca_action_flush(net, tb[1], n, portid);
Jamal Hadi Salimf97017c2008-08-13 02:41:22 -0700872 else
873 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800876 for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000877 act = tcf_action_get_1(tb[i], n, portid);
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800878 if (IS_ERR(act)) {
879 ret = PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto err;
Patrick McHardyab27cfb2008-01-23 20:33:13 -0800881 }
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800882 act->order = i;
WANG Cong33be6272013-12-15 20:15:05 -0800883 list_add_tail(&act->list, &actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
886 if (event == RTM_GETACTION)
WANG Cong33be6272013-12-15 20:15:05 -0800887 ret = act_get_notify(net, portid, n, &actions, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 else { /* delete */
WANG Conga56e1952014-01-09 16:14:00 -0800889 ret = tcf_del_notify(net, n, &actions, portid);
890 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return ret;
893 }
894err:
WANG Cong33be6272013-12-15 20:15:05 -0800895 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return ret;
897}
898
WANG Conga56e1952014-01-09 16:14:00 -0800899static int
900tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
901 u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int err = 0;
905
906 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
907 if (!skb)
908 return -ENOBUFS;
909
WANG Conga56e1952014-01-09 16:14:00 -0800910 if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
911 RTM_NEWACTION, 0, 0) <= 0) {
912 kfree_skb(skb);
913 return -EINVAL;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
WANG Conga56e1952014-01-09 16:14:00 -0800916 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
917 n->nlmsg_flags & NLM_F_ECHO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (err > 0)
919 err = 0;
920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static int
Tom Goff7316ae82010-03-19 15:40:13 +0000924tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000925 u32 portid, int ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 int ret = 0;
WANG Cong33be6272013-12-15 20:15:05 -0800928 LIST_HEAD(actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
WANG Cong33be6272013-12-15 20:15:05 -0800930 ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
931 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto done;
933
934 /* dump then free all the actions after update; inserted policy
935 * stays intact
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000936 */
WANG Conga56e1952014-01-09 16:14:00 -0800937 ret = tcf_add_notify(net, n, &actions, portid);
WANG Cong33be6272013-12-15 20:15:05 -0800938 cleanup_a(&actions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939done:
940 return ret;
941}
942
Thomas Graf661d2962013-03-21 07:45:29 +0000943static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900945 struct net *net = sock_net(skb->sk);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800946 struct nlattr *tca[TCA_ACT_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +0000947 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int ret = 0, ovr = 0;
949
Eric W. Biederman90f62cf2014-04-23 14:29:27 -0700950 if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000951 return -EPERM;
952
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800953 ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
954 if (ret < 0)
955 return ret;
956
957 if (tca[TCA_ACT_TAB] == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000958 pr_notice("tc_ctl_action: received NO action attribs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -EINVAL;
960 }
961
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000962 /* n->nlmsg_flags & NLM_F_CREATE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 switch (n->nlmsg_type) {
964 case RTM_NEWACTION:
965 /* we are going to assume all other flags
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300966 * imply create only if it doesn't exist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * Note that CREATE | EXCL implies that
968 * but since we want avoid ambiguity (eg when flags
969 * is zero) then just set this
970 */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000971 if (n->nlmsg_flags & NLM_F_REPLACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ovr = 1;
973replay:
Eric W. Biederman15e47302012-09-07 20:12:54 +0000974 ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (ret == -EAGAIN)
976 goto replay;
977 break;
978 case RTM_DELACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000979 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000980 portid, RTM_DELACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 case RTM_GETACTION:
Tom Goff7316ae82010-03-19 15:40:13 +0000983 ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000984 portid, RTM_GETACTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986 default:
987 BUG();
988 }
989
990 return ret;
991}
992
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800993static struct nlattr *
Patrick McHardy3a6c2b42009-08-25 16:07:40 +0200994find_dump_kind(const struct nlmsghdr *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000996 struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800997 struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
998 struct nlattr *nla[TCAA_MAX + 1];
999 struct nlattr *kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Patrick McHardyc96c9472008-01-23 20:32:58 -08001001 if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001003 tb1 = nla[TCA_ACT_TAB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (tb1 == NULL)
1005 return NULL;
1006
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001007 if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
1008 NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Patrick McHardy6d834e02008-01-23 20:32:42 -08001011 if (tb[1] == NULL)
1012 return NULL;
1013 if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
1014 nla_len(tb[1]), NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return NULL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001016 kind = tb2[TCA_ACT_KIND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Thomas Graf26dab892006-07-05 20:45:06 -07001018 return kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
1021static int
1022tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1023{
1024 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001025 unsigned char *b = skb_tail_pointer(skb);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001026 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct tc_action_ops *a_o;
1028 struct tc_action a;
1029 int ret = 0;
David S. Miller8b00a532012-06-26 21:39:32 -07001030 struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
Patrick McHardy7ba699c2008-01-22 22:11:50 -08001031 struct nlattr *kind = find_dump_kind(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 if (kind == NULL) {
stephen hemminger6ff9c362010-05-12 06:37:05 +00001034 pr_info("tc_dump_action: action bad kind\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return 0;
1036 }
1037
Thomas Graf26dab892006-07-05 20:45:06 -07001038 a_o = tc_lookup_action(kind);
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001039 if (a_o == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 memset(&a, 0, sizeof(struct tc_action));
1043 a.ops = a_o;
1044
Eric W. Biederman15e47302012-09-07 20:12:54 +00001045 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
David S. Miller8b00a532012-06-26 21:39:32 -07001046 cb->nlh->nlmsg_type, sizeof(*t), 0);
1047 if (!nlh)
1048 goto out_module_put;
1049 t = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 t->tca_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001051 t->tca__pad1 = 0;
1052 t->tca__pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001054 nest = nla_nest_start(skb, TCA_ACT_TAB);
1055 if (nest == NULL)
David S. Miller8b00a532012-06-26 21:39:32 -07001056 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
1059 if (ret < 0)
David S. Miller8b00a532012-06-26 21:39:32 -07001060 goto out_module_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (ret > 0) {
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001063 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 ret = skb->len;
1065 } else
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001066 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001068 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001069 if (NETLINK_CB(cb->skb).portid && ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 nlh->nlmsg_flags |= NLM_F_MULTI;
1071 module_put(a_o->owner);
1072 return skb->len;
1073
David S. Miller8b00a532012-06-26 21:39:32 -07001074out_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 module_put(a_o->owner);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001076 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return skb->len;
1078}
1079
1080static int __init tc_action_init(void)
1081{
Greg Rosec7ac8672011-06-10 01:27:09 +00001082 rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
1083 rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
1084 rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
1085 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return 0;
1088}
1089
1090subsys_initcall(tc_action_init);