Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/kmod.h> |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 22 | #include <linux/err.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 24 | #include <net/net_namespace.h> |
| 25 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <net/sch_generic.h> |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 27 | #include <net/pkt_cls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <net/act_api.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 29 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 31 | static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp) |
| 32 | { |
| 33 | u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK; |
| 34 | |
| 35 | if (!tp) |
| 36 | return -EINVAL; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 37 | a->goto_chain = tcf_chain_get_by_act(tp->chain->block, chain_index); |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 38 | if (!a->goto_chain) |
| 39 | return -ENOMEM; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static void tcf_action_goto_chain_fini(struct tc_action *a) |
| 44 | { |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 45 | tcf_chain_put_by_act(a->goto_chain); |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void tcf_action_goto_chain_exec(const struct tc_action *a, |
| 49 | struct tcf_result *res) |
| 50 | { |
| 51 | const struct tcf_chain *chain = a->goto_chain; |
| 52 | |
| 53 | res->goto_tp = rcu_dereference_bh(chain->filter_chain); |
| 54 | } |
| 55 | |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 56 | static void tcf_free_cookie_rcu(struct rcu_head *p) |
| 57 | { |
| 58 | struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu); |
| 59 | |
| 60 | kfree(cookie->data); |
| 61 | kfree(cookie); |
| 62 | } |
| 63 | |
| 64 | static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie, |
| 65 | struct tc_cookie *new_cookie) |
| 66 | { |
| 67 | struct tc_cookie *old; |
| 68 | |
David S. Miller | 0dbc81e | 2018-07-08 17:02:59 +0900 | [diff] [blame] | 69 | old = xchg((__force struct tc_cookie **)old_cookie, new_cookie); |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 70 | if (old) |
| 71 | call_rcu(&old->rcu, tcf_free_cookie_rcu); |
| 72 | } |
| 73 | |
Cong Wang | d7fb60b | 2017-09-11 16:33:30 -0700 | [diff] [blame] | 74 | /* XXX: For standalone actions, we don't need a RCU grace period either, because |
| 75 | * actions are always connected to filters and filters are already destroyed in |
| 76 | * RCU callbacks, so after a RCU grace period actions are already disconnected |
| 77 | * from filters. Readers later can not find us. |
| 78 | */ |
| 79 | static void free_tcf(struct tc_action *p) |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 80 | { |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 81 | free_percpu(p->cpu_bstats); |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 82 | free_percpu(p->cpu_bstats_hw); |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 83 | free_percpu(p->cpu_qstats); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 84 | |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 85 | tcf_set_action_cookie(&p->act_cookie, NULL); |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 86 | if (p->goto_chain) |
| 87 | tcf_action_goto_chain_fini(p); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 88 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 89 | kfree(p); |
| 90 | } |
| 91 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 92 | static void tcf_action_cleanup(struct tc_action *p) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 93 | { |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 94 | if (p->ops->cleanup) |
| 95 | p->ops->cleanup(p); |
| 96 | |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 97 | gen_kill_estimator(&p->tcfa_rate_est); |
Cong Wang | d7fb60b | 2017-09-11 16:33:30 -0700 | [diff] [blame] | 98 | free_tcf(p); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 99 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 100 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 101 | static int __tcf_action_put(struct tc_action *p, bool bind) |
| 102 | { |
| 103 | struct tcf_idrinfo *idrinfo = p->idrinfo; |
| 104 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 105 | if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) { |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 106 | if (bind) |
| 107 | atomic_dec(&p->tcfa_bindcnt); |
| 108 | idr_remove(&idrinfo->action_idr, p->tcfa_index); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 109 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 110 | |
| 111 | tcf_action_cleanup(p); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | if (bind) |
| 116 | atomic_dec(&p->tcfa_bindcnt); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 121 | int __tcf_idr_release(struct tc_action *p, bool bind, bool strict) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 122 | { |
| 123 | int ret = 0; |
| 124 | |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 125 | /* Release with strict==1 and bind==0 is only called through act API |
| 126 | * interface (classifiers always bind). Only case when action with |
| 127 | * positive reference count and zero bind count can exist is when it was |
| 128 | * also created with act API (unbinding last classifier will destroy the |
| 129 | * action if it was created by classifier). So only case when bind count |
| 130 | * can be changed after initial check is when unbound action is |
| 131 | * destroyed by act API while classifier binds to action with same id |
| 132 | * concurrently. This result either creation of new action(same behavior |
| 133 | * as before), or reusing existing action if concurrent process |
| 134 | * increments reference count before action is deleted. Both scenarios |
| 135 | * are acceptable. |
| 136 | */ |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 137 | if (p) { |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 138 | if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0) |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 139 | return -EPERM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 140 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 141 | if (__tcf_action_put(p, bind)) |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 142 | ret = ACT_P_DELETED; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 143 | } |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 144 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 145 | return ret; |
| 146 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 147 | EXPORT_SYMBOL(__tcf_idr_release); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 148 | |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 149 | static size_t tcf_action_shared_attrs_size(const struct tc_action *act) |
| 150 | { |
Vlad Buslov | e0479b6 | 2018-07-09 20:26:47 +0300 | [diff] [blame] | 151 | struct tc_cookie *act_cookie; |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 152 | u32 cookie_len = 0; |
| 153 | |
Vlad Buslov | e0479b6 | 2018-07-09 20:26:47 +0300 | [diff] [blame] | 154 | rcu_read_lock(); |
| 155 | act_cookie = rcu_dereference(act->act_cookie); |
| 156 | |
| 157 | if (act_cookie) |
| 158 | cookie_len = nla_total_size(act_cookie->len); |
| 159 | rcu_read_unlock(); |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 160 | |
| 161 | return nla_total_size(0) /* action number nested */ |
| 162 | + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */ |
| 163 | + cookie_len /* TCA_ACT_COOKIE */ |
| 164 | + nla_total_size(0) /* TCA_ACT_STATS nested */ |
| 165 | /* TCA_STATS_BASIC */ |
| 166 | + nla_total_size_64bit(sizeof(struct gnet_stats_basic)) |
| 167 | /* TCA_STATS_QUEUE */ |
| 168 | + nla_total_size_64bit(sizeof(struct gnet_stats_queue)) |
| 169 | + nla_total_size(0) /* TCA_OPTIONS nested */ |
| 170 | + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */ |
| 171 | } |
| 172 | |
| 173 | static size_t tcf_action_full_attrs_size(size_t sz) |
| 174 | { |
| 175 | return NLMSG_HDRLEN /* struct nlmsghdr */ |
| 176 | + sizeof(struct tcamsg) |
| 177 | + nla_total_size(0) /* TCA_ACT_TAB nested */ |
| 178 | + sz; |
| 179 | } |
| 180 | |
| 181 | static size_t tcf_action_fill_size(const struct tc_action *act) |
| 182 | { |
| 183 | size_t sz = tcf_action_shared_attrs_size(act); |
| 184 | |
| 185 | if (act->ops->get_fill_size) |
| 186 | return act->ops->get_fill_size(act) + sz; |
| 187 | return sz; |
| 188 | } |
| 189 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 190 | static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 191 | struct netlink_callback *cb) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 192 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 193 | int err = 0, index = -1, s_i = 0, n_i = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 194 | u32 act_flags = cb->args[2]; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 195 | unsigned long jiffy_since = cb->args[3]; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 196 | struct nlattr *nest; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 197 | struct idr *idr = &idrinfo->action_idr; |
| 198 | struct tc_action *p; |
| 199 | unsigned long id = 1; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 200 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 201 | mutex_lock(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 202 | |
| 203 | s_i = cb->args[0]; |
| 204 | |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 205 | idr_for_each_entry_ul(idr, p, id) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 206 | index++; |
| 207 | if (index < s_i) |
| 208 | continue; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 209 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 210 | if (jiffy_since && |
| 211 | time_after(jiffy_since, |
| 212 | (unsigned long)p->tcfa_tm.lastuse)) |
| 213 | continue; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 214 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 215 | nest = nla_nest_start(skb, n_i); |
Craig Dillabaugh | 734549e | 2018-03-26 14:58:32 -0400 | [diff] [blame] | 216 | if (!nest) { |
| 217 | index--; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 218 | goto nla_put_failure; |
Craig Dillabaugh | 734549e | 2018-03-26 14:58:32 -0400 | [diff] [blame] | 219 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 220 | err = tcf_action_dump_1(skb, p, 0, 0); |
| 221 | if (err < 0) { |
| 222 | index--; |
| 223 | nlmsg_trim(skb, nest); |
| 224 | goto done; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 225 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 226 | nla_nest_end(skb, nest); |
| 227 | n_i++; |
| 228 | if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) && |
| 229 | n_i >= TCA_ACT_MAX_PRIO) |
| 230 | goto done; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 231 | } |
| 232 | done: |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 233 | if (index >= 0) |
| 234 | cb->args[0] = index + 1; |
| 235 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 236 | mutex_unlock(&idrinfo->lock); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 237 | if (n_i) { |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 238 | if (act_flags & TCA_FLAG_LARGE_DUMP_ON) |
| 239 | cb->args[1] = n_i; |
| 240 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 241 | return n_i; |
| 242 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 243 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 244 | nla_nest_cancel(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 245 | goto done; |
| 246 | } |
| 247 | |
Vlad Buslov | ec3ed29 | 2018-09-19 16:37:29 -0700 | [diff] [blame] | 248 | static int tcf_idr_release_unsafe(struct tc_action *p) |
| 249 | { |
| 250 | if (atomic_read(&p->tcfa_bindcnt) > 0) |
| 251 | return -EPERM; |
| 252 | |
| 253 | if (refcount_dec_and_test(&p->tcfa_refcnt)) { |
| 254 | idr_remove(&p->idrinfo->action_idr, p->tcfa_index); |
| 255 | tcf_action_cleanup(p); |
| 256 | return ACT_P_DELETED; |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 262 | static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 263 | const struct tc_action_ops *ops) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 264 | { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 265 | struct nlattr *nest; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 266 | int n_i = 0; |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 267 | int ret = -EINVAL; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 268 | struct idr *idr = &idrinfo->action_idr; |
| 269 | struct tc_action *p; |
| 270 | unsigned long id = 1; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 271 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 272 | nest = nla_nest_start(skb, 0); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 273 | if (nest == NULL) |
| 274 | goto nla_put_failure; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 275 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 276 | goto nla_put_failure; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 277 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 278 | mutex_lock(&idrinfo->lock); |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 279 | idr_for_each_entry_ul(idr, p, id) { |
Vlad Buslov | ec3ed29 | 2018-09-19 16:37:29 -0700 | [diff] [blame] | 280 | ret = tcf_idr_release_unsafe(p); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 281 | if (ret == ACT_P_DELETED) { |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 282 | module_put(ops->owner); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 283 | n_i++; |
| 284 | } else if (ret < 0) { |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 285 | mutex_unlock(&idrinfo->lock); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 286 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 289 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | ec3ed29 | 2018-09-19 16:37:29 -0700 | [diff] [blame] | 290 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 291 | if (nla_put_u32(skb, TCA_FCNT, n_i)) |
| 292 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 293 | nla_nest_end(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 294 | |
| 295 | return n_i; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 296 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 297 | nla_nest_cancel(skb, nest); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 298 | return ret; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 299 | } |
| 300 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 301 | int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, |
| 302 | struct netlink_callback *cb, int type, |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 303 | const struct tc_action_ops *ops, |
| 304 | struct netlink_ext_ack *extack) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 305 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 306 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 307 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 308 | if (type == RTM_DELACTION) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 309 | return tcf_del_walker(idrinfo, skb, ops); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 310 | } else if (type == RTM_GETACTION) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 311 | return tcf_dump_walker(idrinfo, skb, cb); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 312 | } else { |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 313 | WARN(1, "tcf_generic_walker: unknown command %d\n", type); |
| 314 | NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command"); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 315 | return -EINVAL; |
| 316 | } |
| 317 | } |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 318 | EXPORT_SYMBOL(tcf_generic_walker); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 319 | |
Cong Wang | 7d485c4 | 2018-08-19 12:22:08 -0700 | [diff] [blame] | 320 | int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 321 | { |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 322 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 323 | struct tc_action *p; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 324 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 325 | mutex_lock(&idrinfo->lock); |
Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 326 | p = idr_find(&idrinfo->action_idr, index); |
Cong Wang | 7d485c4 | 2018-08-19 12:22:08 -0700 | [diff] [blame] | 327 | if (IS_ERR(p)) |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 328 | p = NULL; |
Cong Wang | 7d485c4 | 2018-08-19 12:22:08 -0700 | [diff] [blame] | 329 | else if (p) |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 330 | refcount_inc(&p->tcfa_refcnt); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 331 | mutex_unlock(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 332 | |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 333 | if (p) { |
| 334 | *a = p; |
| 335 | return true; |
| 336 | } |
| 337 | return false; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 338 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 339 | EXPORT_SYMBOL(tcf_idr_search); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 340 | |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 341 | static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index) |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 342 | { |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 343 | struct tc_action *p; |
| 344 | int ret = 0; |
| 345 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 346 | mutex_lock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 347 | p = idr_find(&idrinfo->action_idr, index); |
| 348 | if (!p) { |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 349 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 350 | return -ENOENT; |
| 351 | } |
| 352 | |
| 353 | if (!atomic_read(&p->tcfa_bindcnt)) { |
| 354 | if (refcount_dec_and_test(&p->tcfa_refcnt)) { |
| 355 | struct module *owner = p->ops->owner; |
| 356 | |
| 357 | WARN_ON(p != idr_remove(&idrinfo->action_idr, |
| 358 | p->tcfa_index)); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 359 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 360 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 361 | tcf_action_cleanup(p); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 362 | module_put(owner); |
| 363 | return 0; |
| 364 | } |
| 365 | ret = 0; |
| 366 | } else { |
| 367 | ret = -EPERM; |
| 368 | } |
| 369 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 370 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 371 | return ret; |
| 372 | } |
Vlad Buslov | 2a2ea34 | 2018-07-05 17:24:27 +0300 | [diff] [blame] | 373 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 374 | int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, |
| 375 | struct tc_action **a, const struct tc_action_ops *ops, |
| 376 | int bind, bool cpustats) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 377 | { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 378 | struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 379 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 380 | int err = -ENOMEM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 381 | |
| 382 | if (unlikely(!p)) |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 383 | return -ENOMEM; |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 384 | refcount_set(&p->tcfa_refcnt, 1); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 385 | if (bind) |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 386 | atomic_set(&p->tcfa_bindcnt, 1); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 387 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 388 | if (cpustats) { |
| 389 | p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 390 | if (!p->cpu_bstats) |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 391 | goto err1; |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 392 | p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); |
| 393 | if (!p->cpu_bstats_hw) |
| 394 | goto err2; |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 395 | p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); |
| 396 | if (!p->cpu_qstats) |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 397 | goto err3; |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 398 | } |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 399 | spin_lock_init(&p->tcfa_lock); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 400 | p->tcfa_index = index; |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 401 | p->tcfa_tm.install = jiffies; |
| 402 | p->tcfa_tm.lastuse = jiffies; |
| 403 | p->tcfa_tm.firstuse = 0; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 404 | if (est) { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 405 | err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, |
| 406 | &p->tcfa_rate_est, |
| 407 | &p->tcfa_lock, NULL, est); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 408 | if (err) |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 409 | goto err4; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 412 | p->idrinfo = idrinfo; |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 413 | p->ops = ops; |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 414 | *a = p; |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 415 | return 0; |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 416 | err4: |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 417 | free_percpu(p->cpu_qstats); |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 418 | err3: |
| 419 | free_percpu(p->cpu_bstats_hw); |
Matthew Wilcox | 339913a | 2017-11-28 10:28:15 -0500 | [diff] [blame] | 420 | err2: |
| 421 | free_percpu(p->cpu_bstats); |
| 422 | err1: |
| 423 | kfree(p); |
| 424 | return err; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 425 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 426 | EXPORT_SYMBOL(tcf_idr_create); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 427 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 428 | void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 429 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 430 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 431 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 432 | mutex_lock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 433 | /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */ |
| 434 | WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index))); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 435 | mutex_unlock(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 436 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 437 | EXPORT_SYMBOL(tcf_idr_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 439 | /* Cleanup idr index that was allocated but not initialized. */ |
| 440 | |
| 441 | void tcf_idr_cleanup(struct tc_action_net *tn, u32 index) |
| 442 | { |
| 443 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 444 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 445 | mutex_lock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 446 | /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */ |
| 447 | WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index))); |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 448 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 449 | } |
| 450 | EXPORT_SYMBOL(tcf_idr_cleanup); |
| 451 | |
| 452 | /* Check if action with specified index exists. If actions is found, increments |
| 453 | * its reference and bind counters, and return 1. Otherwise insert temporary |
| 454 | * error pointer (to prevent concurrent users from inserting actions with same |
| 455 | * index) and return 0. |
| 456 | */ |
| 457 | |
| 458 | int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index, |
| 459 | struct tc_action **a, int bind) |
| 460 | { |
| 461 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 462 | struct tc_action *p; |
| 463 | int ret; |
| 464 | |
| 465 | again: |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 466 | mutex_lock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 467 | if (*index) { |
| 468 | p = idr_find(&idrinfo->action_idr, *index); |
| 469 | if (IS_ERR(p)) { |
| 470 | /* This means that another process allocated |
| 471 | * index but did not assign the pointer yet. |
| 472 | */ |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 473 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 474 | goto again; |
| 475 | } |
| 476 | |
| 477 | if (p) { |
| 478 | refcount_inc(&p->tcfa_refcnt); |
| 479 | if (bind) |
| 480 | atomic_inc(&p->tcfa_bindcnt); |
| 481 | *a = p; |
| 482 | ret = 1; |
| 483 | } else { |
| 484 | *a = NULL; |
| 485 | ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index, |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 486 | *index, GFP_KERNEL); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 487 | if (!ret) |
| 488 | idr_replace(&idrinfo->action_idr, |
| 489 | ERR_PTR(-EBUSY), *index); |
| 490 | } |
| 491 | } else { |
| 492 | *index = 1; |
| 493 | *a = NULL; |
| 494 | ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index, |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 495 | UINT_MAX, GFP_KERNEL); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 496 | if (!ret) |
| 497 | idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY), |
| 498 | *index); |
| 499 | } |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 500 | mutex_unlock(&idrinfo->lock); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 501 | return ret; |
| 502 | } |
| 503 | EXPORT_SYMBOL(tcf_idr_check_alloc); |
| 504 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 505 | void tcf_idrinfo_destroy(const struct tc_action_ops *ops, |
| 506 | struct tcf_idrinfo *idrinfo) |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 507 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 508 | struct idr *idr = &idrinfo->action_idr; |
| 509 | struct tc_action *p; |
| 510 | int ret; |
| 511 | unsigned long id = 1; |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 512 | |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 513 | idr_for_each_entry_ul(idr, p, id) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 514 | ret = __tcf_idr_release(p, false, true); |
| 515 | if (ret == ACT_P_DELETED) |
| 516 | module_put(ops->owner); |
| 517 | else if (ret < 0) |
| 518 | return; |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 519 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 520 | idr_destroy(&idrinfo->action_idr); |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 521 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 522 | EXPORT_SYMBOL(tcf_idrinfo_destroy); |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 523 | |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 524 | static LIST_HEAD(act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | static DEFINE_RWLOCK(act_mod_lock); |
| 526 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 527 | int tcf_register_action(struct tc_action_ops *act, |
| 528 | struct pernet_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 530 | struct tc_action_ops *a; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 531 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 533 | if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 534 | return -EINVAL; |
| 535 | |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 536 | /* We have to register pernet ops before making the action ops visible, |
| 537 | * otherwise tcf_action_init_1() could get a partially initialized |
| 538 | * netns. |
| 539 | */ |
| 540 | ret = register_pernet_subsys(ops); |
| 541 | if (ret) |
| 542 | return ret; |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | write_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 545 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { |
| 547 | write_unlock(&act_mod_lock); |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 548 | unregister_pernet_subsys(ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return -EEXIST; |
| 550 | } |
| 551 | } |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 552 | list_add_tail(&act->head, &act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | write_unlock(&act_mod_lock); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | return 0; |
| 556 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 557 | EXPORT_SYMBOL(tcf_register_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 559 | int tcf_unregister_action(struct tc_action_ops *act, |
| 560 | struct pernet_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 562 | struct tc_action_ops *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | int err = -ENOENT; |
| 564 | |
| 565 | write_lock(&act_mod_lock); |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 566 | list_for_each_entry(a, &act_base, head) { |
| 567 | if (a == act) { |
| 568 | list_del(&act->head); |
| 569 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | break; |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | write_unlock(&act_mod_lock); |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 574 | if (!err) |
| 575 | unregister_pernet_subsys(ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return err; |
| 577 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 578 | EXPORT_SYMBOL(tcf_unregister_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
| 580 | /* lookup by name */ |
| 581 | static struct tc_action_ops *tc_lookup_action_n(char *kind) |
| 582 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 583 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
| 585 | if (kind) { |
| 586 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 587 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 589 | if (try_module_get(a->owner)) |
| 590 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
| 594 | read_unlock(&act_mod_lock); |
| 595 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 596 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 599 | /* lookup by nlattr */ |
| 600 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 602 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | if (kind) { |
| 605 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 606 | list_for_each_entry(a, &act_base, head) { |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 607 | if (nla_strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 608 | if (try_module_get(a->owner)) |
| 609 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | break; |
| 611 | } |
| 612 | } |
| 613 | read_unlock(&act_mod_lock); |
| 614 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 615 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 618 | /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */ |
| 619 | #define TCA_ACT_MAX_PRIO_MASK 0x1FF |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 620 | int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, |
| 621 | int nr_actions, struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 623 | u32 jmp_prgcnt = 0; |
| 624 | u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ |
Jiri Pirko | ec1a9cc | 2017-08-04 14:29:02 +0200 | [diff] [blame] | 625 | int i; |
| 626 | int ret = TC_ACT_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Willem de Bruijn | e7246e1 | 2017-01-07 17:06:35 -0500 | [diff] [blame] | 628 | if (skb_skip_tc_classify(skb)) |
| 629 | return TC_ACT_OK; |
| 630 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 631 | restart_act_graph: |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 632 | for (i = 0; i < nr_actions; i++) { |
| 633 | const struct tc_action *a = actions[i]; |
| 634 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 635 | if (jmp_prgcnt > 0) { |
| 636 | jmp_prgcnt -= 1; |
| 637 | continue; |
| 638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | repeat: |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 640 | ret = a->ops->act(skb, a, res); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 641 | if (ret == TC_ACT_REPEAT) |
| 642 | goto repeat; /* we need a ttl - JHS */ |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 643 | |
Jiri Pirko | 9da3242 | 2017-05-02 10:12:00 +0200 | [diff] [blame] | 644 | if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 645 | jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; |
| 646 | if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { |
| 647 | /* faulty opcode, stop pipeline */ |
| 648 | return TC_ACT_OK; |
| 649 | } else { |
| 650 | jmp_ttl -= 1; |
| 651 | if (jmp_ttl > 0) |
| 652 | goto restart_act_graph; |
| 653 | else /* faulty graph, stop pipeline */ |
| 654 | return TC_ACT_OK; |
| 655 | } |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 656 | } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { |
| 657 | tcf_action_goto_chain_exec(a, res); |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 658 | } |
| 659 | |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 660 | if (ret != TC_ACT_PIPE) |
Willem de Bruijn | e7246e1 | 2017-01-07 17:06:35 -0500 | [diff] [blame] | 661 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return ret; |
| 665 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 666 | EXPORT_SYMBOL(tcf_action_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 668 | int tcf_action_destroy(struct tc_action *actions[], int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 670 | const struct tc_action_ops *ops; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 671 | struct tc_action *a; |
| 672 | int ret = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 674 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
| 675 | a = actions[i]; |
| 676 | actions[i] = NULL; |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 677 | ops = a->ops; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 678 | ret = __tcf_idr_release(a, bind, true); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 679 | if (ret == ACT_P_DELETED) |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame] | 680 | module_put(ops->owner); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 681 | else if (ret < 0) |
| 682 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 684 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Paolo Abeni | 97763dc | 2018-08-29 10:22:33 +0200 | [diff] [blame] | 687 | static int tcf_action_destroy_1(struct tc_action *a, int bind) |
| 688 | { |
| 689 | struct tc_action *actions[] = { a, NULL }; |
| 690 | |
| 691 | return tcf_action_destroy(actions, bind); |
| 692 | } |
| 693 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 694 | static int tcf_action_put(struct tc_action *p) |
| 695 | { |
| 696 | return __tcf_action_put(p, false); |
| 697 | } |
| 698 | |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 699 | /* Put all actions in this array, skip those NULL's. */ |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 700 | static void tcf_action_put_many(struct tc_action *actions[]) |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 701 | { |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 702 | int i; |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 703 | |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 704 | for (i = 0; i < TCA_ACT_MAX_PRIO; i++) { |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 705 | struct tc_action *a = actions[i]; |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 706 | const struct tc_action_ops *ops; |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 707 | |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 708 | if (!a) |
| 709 | continue; |
| 710 | ops = a->ops; |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 711 | if (tcf_action_put(a)) |
| 712 | module_put(ops->owner); |
| 713 | } |
| 714 | } |
| 715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | int |
| 717 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 718 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return a->ops->dump(skb, a, bind, ref); |
| 720 | } |
| 721 | |
| 722 | int |
| 723 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 724 | { |
| 725 | int err = -EINVAL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 726 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 727 | struct nlattr *nest; |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 728 | struct tc_cookie *cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 730 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
| 731 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | if (tcf_action_copy_stats(skb, a, 0)) |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 733 | goto nla_put_failure; |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 734 | |
| 735 | rcu_read_lock(); |
| 736 | cookie = rcu_dereference(a->act_cookie); |
| 737 | if (cookie) { |
| 738 | if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) { |
| 739 | rcu_read_unlock(); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 740 | goto nla_put_failure; |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 741 | } |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 742 | } |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 743 | rcu_read_unlock(); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 744 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 745 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 746 | if (nest == NULL) |
| 747 | goto nla_put_failure; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 748 | err = tcf_action_dump_old(skb, a, bind, ref); |
| 749 | if (err > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 750 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return err; |
| 752 | } |
| 753 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 754 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 755 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | return -1; |
| 757 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 758 | EXPORT_SYMBOL(tcf_action_dump_1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 760 | int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 761 | int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
| 763 | struct tc_action *a; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 764 | int err = -EINVAL, i; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 765 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 767 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
| 768 | a = actions[i]; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 769 | nest = nla_nest_start(skb, a->order); |
| 770 | if (nest == NULL) |
| 771 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | err = tcf_action_dump_1(skb, a, bind, ref); |
| 773 | if (err < 0) |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 774 | goto errout; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 775 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 780 | nla_put_failure: |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 781 | err = -EINVAL; |
| 782 | errout: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 783 | nla_nest_cancel(skb, nest); |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 784 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 787 | static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 788 | { |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 789 | struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 790 | if (!c) |
| 791 | return NULL; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 792 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 793 | c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); |
| 794 | if (!c->data) { |
| 795 | kfree(c); |
| 796 | return NULL; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 797 | } |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 798 | c->len = nla_len(tb[TCA_ACT_COOKIE]); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 799 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 800 | return c; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 801 | } |
| 802 | |
Paolo Abeni | 802bfb1 | 2018-07-30 14:30:42 +0200 | [diff] [blame] | 803 | static bool tcf_action_valid(int action) |
| 804 | { |
| 805 | int opcode = TC_ACT_EXT_OPCODE(action); |
| 806 | |
| 807 | if (!opcode) |
| 808 | return action <= TC_ACT_VALUE_MAX; |
| 809 | return opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC; |
| 810 | } |
| 811 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 812 | struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, |
| 813 | struct nlattr *nla, struct nlattr *est, |
Alexander Aring | aea0d72 | 2018-02-15 10:54:54 -0500 | [diff] [blame] | 814 | char *name, int ovr, int bind, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 815 | bool rtnl_held, |
Alexander Aring | aea0d72 | 2018-02-15 10:54:54 -0500 | [diff] [blame] | 816 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
| 818 | struct tc_action *a; |
| 819 | struct tc_action_ops *a_o; |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 820 | struct tc_cookie *cookie = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | char act_name[IFNAMSIZ]; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 822 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 823 | struct nlattr *kind; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 824 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if (name == NULL) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 827 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 828 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | goto err_out; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 830 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 831 | kind = tb[TCA_ACT_KIND]; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 832 | if (!kind) { |
| 833 | NL_SET_ERR_MSG(extack, "TC action kind must be specified"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 835 | } |
| 836 | if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) { |
| 837 | NL_SET_ERR_MSG(extack, "TC action name too long"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 839 | } |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 840 | if (tb[TCA_ACT_COOKIE]) { |
| 841 | int cklen = nla_len(tb[TCA_ACT_COOKIE]); |
| 842 | |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 843 | if (cklen > TC_COOKIE_MAX_SIZE) { |
| 844 | NL_SET_ERR_MSG(extack, "TC cookie size above the maximum"); |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 845 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 846 | } |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 847 | |
| 848 | cookie = nla_memdup_cookie(tb); |
| 849 | if (!cookie) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 850 | NL_SET_ERR_MSG(extack, "No memory to generate TC cookie"); |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 851 | err = -ENOMEM; |
| 852 | goto err_out; |
| 853 | } |
| 854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } else { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 856 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) { |
| 857 | NL_SET_ERR_MSG(extack, "TC action name too long"); |
| 858 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | a_o = tc_lookup_action_n(act_name); |
| 864 | if (a_o == NULL) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 865 | #ifdef CONFIG_MODULES |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 866 | if (rtnl_held) |
| 867 | rtnl_unlock(); |
Patrick McHardy | 4bba392 | 2006-01-08 22:22:14 -0800 | [diff] [blame] | 868 | request_module("act_%s", act_name); |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 869 | if (rtnl_held) |
| 870 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | a_o = tc_lookup_action_n(act_name); |
| 873 | |
| 874 | /* We dropped the RTNL semaphore in order to |
| 875 | * perform the module load. So, even if we |
| 876 | * succeeded in loading the module we have to |
| 877 | * tell the caller to replay the request. We |
| 878 | * indicate this using -EAGAIN. |
| 879 | */ |
| 880 | if (a_o != NULL) { |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 881 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | goto err_mod; |
| 883 | } |
| 884 | #endif |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 885 | NL_SET_ERR_MSG(extack, "Failed to load TC action module"); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 886 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | goto err_out; |
| 888 | } |
| 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | /* backward compatibility for policer */ |
| 891 | if (name == NULL) |
Alexander Aring | 589dad6 | 2018-02-15 10:54:56 -0500 | [diff] [blame] | 892 | err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 893 | rtnl_held, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | else |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 895 | err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held, |
| 896 | extack); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 897 | if (err < 0) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 898 | goto err_mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Vlad Buslov | eec94fd | 2018-07-05 17:24:23 +0300 | [diff] [blame] | 900 | if (!name && tb[TCA_ACT_COOKIE]) |
| 901 | tcf_set_action_cookie(&a->act_cookie, cookie); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | /* module count goes up only when brand new policy is created |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 904 | * if it exists and is only bound to in a_o->init() then |
| 905 | * ACT_P_CREATED is not returned (a zero is). |
| 906 | */ |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 907 | if (err != ACT_P_CREATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | module_put(a_o->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 910 | if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) { |
| 911 | err = tcf_action_goto_chain_init(a, tp); |
| 912 | if (err) { |
Paolo Abeni | 97763dc | 2018-08-29 10:22:33 +0200 | [diff] [blame] | 913 | tcf_action_destroy_1(a, bind); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 914 | NL_SET_ERR_MSG(extack, "Failed to init TC action chain"); |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 915 | return ERR_PTR(err); |
| 916 | } |
| 917 | } |
| 918 | |
Paolo Abeni | 802bfb1 | 2018-07-30 14:30:42 +0200 | [diff] [blame] | 919 | if (!tcf_action_valid(a->tcfa_action)) { |
Paolo Abeni | 97763dc | 2018-08-29 10:22:33 +0200 | [diff] [blame] | 920 | tcf_action_destroy_1(a, bind); |
| 921 | NL_SET_ERR_MSG(extack, "Invalid control action value"); |
| 922 | return ERR_PTR(-EINVAL); |
Paolo Abeni | 802bfb1 | 2018-07-30 14:30:42 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | return a; |
| 926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | err_mod: |
| 928 | module_put(a_o->owner); |
| 929 | err_out: |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 930 | if (cookie) { |
| 931 | kfree(cookie->data); |
| 932 | kfree(cookie); |
| 933 | } |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 934 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 937 | /* Returns numbers of initialized actions or negative error. */ |
| 938 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 939 | int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, |
| 940 | struct nlattr *est, char *name, int ovr, int bind, |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 941 | struct tc_action *actions[], size_t *attr_size, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 942 | bool rtnl_held, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 944 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 945 | struct tc_action *act; |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 946 | size_t sz = 0; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 947 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | int i; |
| 949 | |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 950 | err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 951 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 952 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 954 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Alexander Aring | aea0d72 | 2018-02-15 10:54:54 -0500 | [diff] [blame] | 955 | act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 956 | rtnl_held, extack); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 957 | if (IS_ERR(act)) { |
| 958 | err = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | goto err; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 960 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 961 | act->order = i; |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 962 | sz += tcf_action_fill_size(act); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 963 | /* Start from index 0 */ |
| 964 | actions[i - 1] = act; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
Jamal Hadi Salim | aecc5ce | 2016-09-19 19:02:51 -0400 | [diff] [blame] | 966 | |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 967 | *attr_size = tcf_action_full_attrs_size(sz); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 968 | return i - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | err: |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 971 | tcf_action_destroy(actions, bind); |
| 972 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
| 974 | |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 975 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | int compat_mode) |
| 977 | { |
| 978 | int err = 0; |
| 979 | struct gnet_dump d; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 980 | |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 981 | if (p == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | goto errout; |
| 983 | |
| 984 | /* compat_mode being true specifies a call that is supposed |
Dirk Hohndel | 06fe9fb | 2009-09-28 21:43:57 -0400 | [diff] [blame] | 985 | * to add additional backward compatibility statistic TLVs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | */ |
| 987 | if (compat_mode) { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 988 | if (p->type == TCA_OLD_COMPAT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | err = gnet_stats_start_copy_compat(skb, 0, |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 990 | TCA_STATS, |
| 991 | TCA_XSTATS, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 992 | &p->tcfa_lock, &d, |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 993 | TCA_PAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | else |
| 995 | return 0; |
| 996 | } else |
| 997 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 998 | &p->tcfa_lock, &d, TCA_ACT_PAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | if (err < 0) |
| 1001 | goto errout; |
| 1002 | |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 1003 | if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 || |
Eelco Chaudron | 28169ab | 2018-09-21 07:14:02 -0400 | [diff] [blame] | 1004 | gnet_stats_copy_basic_hw(NULL, &d, p->cpu_bstats_hw, |
| 1005 | &p->tcfa_bstats_hw) < 0 || |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 1006 | gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 1007 | gnet_stats_copy_queue(&d, p->cpu_qstats, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 1008 | &p->tcfa_qstats, |
| 1009 | p->tcfa_qstats.qlen) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | goto errout; |
| 1011 | |
| 1012 | if (gnet_stats_finish_copy(&d) < 0) |
| 1013 | goto errout; |
| 1014 | |
| 1015 | return 0; |
| 1016 | |
| 1017 | errout: |
| 1018 | return -1; |
| 1019 | } |
| 1020 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1021 | static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[], |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1022 | u32 portid, u32 seq, u16 flags, int event, int bind, |
| 1023 | int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | { |
| 1025 | struct tcamsg *t; |
| 1026 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1027 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1028 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1030 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1031 | if (!nlh) |
| 1032 | goto out_nlmsg_trim; |
| 1033 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1035 | t->tca__pad1 = 0; |
| 1036 | t->tca__pad2 = 0; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 1037 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1038 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1039 | if (!nest) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1040 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1042 | if (tcf_action_dump(skb, actions, bind, ref) < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1043 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1045 | nla_nest_end(skb, nest); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 1046 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1047 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | return skb->len; |
| 1049 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1050 | out_nlmsg_trim: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1051 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return -1; |
| 1053 | } |
| 1054 | |
| 1055 | static int |
Roman Mashak | c4c4290 | 2017-07-13 13:12:18 -0400 | [diff] [blame] | 1056 | tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1057 | struct tc_action *actions[], int event, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1058 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
| 1060 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1063 | if (!skb) |
| 1064 | return -ENOBUFS; |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1065 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 1066 | 0, 1) <= 0) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1067 | NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | kfree_skb(skb); |
| 1069 | return -EINVAL; |
| 1070 | } |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 1071 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1072 | return rtnl_unicast(skb, net, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 1075 | static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1076 | struct nlmsghdr *n, u32 portid, |
| 1077 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1079 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1080 | const struct tc_action_ops *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | struct tc_action *a; |
| 1082 | int index; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1083 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1085 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1086 | if (err < 0) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1087 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1089 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1090 | if (tb[TCA_ACT_INDEX] == NULL || |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1091 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) { |
| 1092 | NL_SET_ERR_MSG(extack, "Invalid TC action index value"); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1093 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1094 | } |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 1095 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1097 | err = -EINVAL; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1098 | ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1099 | if (!ops) { /* could happen in batch of actions */ |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 1100 | NL_SET_ERR_MSG(extack, "Specified TC action kind not found"); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1101 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1102 | } |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1103 | err = -ENOENT; |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 1104 | if (ops->lookup(net, &a, index) == 0) { |
| 1105 | NL_SET_ERR_MSG(extack, "TC action with specified index not found"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | goto err_mod; |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 1107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1109 | module_put(ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return a; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | err_mod: |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1113 | module_put(ops->owner); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1114 | err_out: |
| 1115 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1118 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1119 | struct nlmsghdr *n, u32 portid, |
| 1120 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | { |
| 1122 | struct sk_buff *skb; |
| 1123 | unsigned char *b; |
| 1124 | struct nlmsghdr *nlh; |
| 1125 | struct tcamsg *t; |
| 1126 | struct netlink_callback dcb; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1127 | struct nlattr *nest; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1128 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1129 | const struct tc_action_ops *ops; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1130 | struct nlattr *kind; |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 1131 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1134 | if (!skb) |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 1135 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1137 | b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1139 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1140 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | goto err_out; |
| 1142 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1143 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1144 | kind = tb[TCA_ACT_KIND]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1145 | ops = tc_lookup_action(kind); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1146 | if (!ops) { /*some idjot trying to flush unknown action */ |
| 1147 | NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | goto err_out; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1151 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, |
| 1152 | sizeof(*t), 0); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1153 | if (!nlh) { |
| 1154 | NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification"); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1155 | goto out_module_put; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1156 | } |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1157 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1159 | t->tca__pad1 = 0; |
| 1160 | t->tca__pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1162 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1163 | if (!nest) { |
| 1164 | NL_SET_ERR_MSG(extack, "Failed to add new netlink message"); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1165 | goto out_module_put; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 1168 | err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack); |
Davide Caratti | 66dede2 | 2018-02-15 15:50:57 +0100 | [diff] [blame] | 1169 | if (err <= 0) { |
| 1170 | nla_nest_cancel(skb, nest); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1171 | goto out_module_put; |
Davide Caratti | 66dede2 | 2018-02-15 15:50:57 +0100 | [diff] [blame] | 1172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1174 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1176 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | nlh->nlmsg_flags |= NLM_F_ROOT; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1178 | module_put(ops->owner); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1179 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1180 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | if (err > 0) |
| 1182 | return 0; |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1183 | if (err < 0) |
| 1184 | NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| 1186 | return err; |
| 1187 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1188 | out_module_put: |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1189 | module_put(ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | err_out: |
| 1191 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | return err; |
| 1193 | } |
| 1194 | |
Cong Wang | b144e7e | 2018-08-19 12:22:07 -0700 | [diff] [blame] | 1195 | static int tcf_action_delete(struct net *net, struct tc_action *actions[]) |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1196 | { |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1197 | int i; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1198 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1199 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
| 1200 | struct tc_action *a = actions[i]; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1201 | const struct tc_action_ops *ops = a->ops; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1202 | /* Actions can be deleted concurrently so we must save their |
| 1203 | * type and id to search again after reference is released. |
| 1204 | */ |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1205 | struct tcf_idrinfo *idrinfo = a->idrinfo; |
| 1206 | u32 act_index = a->tcfa_index; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1207 | |
Vlad Buslov | c10bbfa | 2018-09-03 10:04:55 +0300 | [diff] [blame] | 1208 | actions[i] = NULL; |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1209 | if (tcf_action_put(a)) { |
| 1210 | /* last reference, action was deleted concurrently */ |
| 1211 | module_put(ops->owner); |
| 1212 | } else { |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1213 | int ret; |
| 1214 | |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1215 | /* now do the delete */ |
Cong Wang | 97a3f84f | 2018-08-19 12:22:06 -0700 | [diff] [blame] | 1216 | ret = tcf_idr_delete_index(idrinfo, act_index); |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1217 | if (ret < 0) |
Vlad Buslov | 16af606 | 2018-07-05 17:24:29 +0300 | [diff] [blame] | 1218 | return ret; |
| 1219 | } |
| 1220 | } |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | static int |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1225 | tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[], |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1226 | u32 portid, size_t attr_size, struct netlink_ext_ack *extack) |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1227 | { |
| 1228 | int ret; |
| 1229 | struct sk_buff *skb; |
| 1230 | |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1231 | skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, |
| 1232 | GFP_KERNEL); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1233 | if (!skb) |
| 1234 | return -ENOBUFS; |
| 1235 | |
| 1236 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, |
Vlad Buslov | 3f7c72b | 2018-07-05 17:24:26 +0300 | [diff] [blame] | 1237 | 0, 2) <= 0) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1238 | NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes"); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1239 | kfree_skb(skb); |
| 1240 | return -EINVAL; |
| 1241 | } |
| 1242 | |
| 1243 | /* now do the delete */ |
Cong Wang | b144e7e | 2018-08-19 12:22:07 -0700 | [diff] [blame] | 1244 | ret = tcf_action_delete(net, actions); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 1245 | if (ret < 0) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1246 | NL_SET_ERR_MSG(extack, "Failed to delete TC action"); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 1247 | kfree_skb(skb); |
| 1248 | return ret; |
| 1249 | } |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1250 | |
| 1251 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1252 | n->nlmsg_flags & NLM_F_ECHO); |
| 1253 | if (ret > 0) |
| 1254 | return 0; |
| 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | static int |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1259 | tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1260 | u32 portid, int event, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1262 | int i, ret; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1263 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1264 | struct tc_action *act; |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1265 | size_t attr_size = 0; |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1266 | struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1268 | ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1269 | if (ret < 0) |
| 1270 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1272 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1273 | if (tb[1]) |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1274 | return tca_action_flush(net, tb[1], n, portid, extack); |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1275 | |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1276 | NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action"); |
Alexander Aring | 1af851558 | 2018-02-15 10:54:53 -0500 | [diff] [blame] | 1277 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1280 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1281 | act = tcf_action_get_1(net, tb[i], n, portid, extack); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1282 | if (IS_ERR(act)) { |
| 1283 | ret = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | goto err; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1285 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1286 | act->order = i; |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 1287 | attr_size += tcf_action_fill_size(act); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1288 | actions[i - 1] = act; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Roman Mashak | 4e76e75 | 2018-03-08 16:59:19 -0500 | [diff] [blame] | 1291 | attr_size = tcf_action_full_attrs_size(attr_size); |
| 1292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | if (event == RTM_GETACTION) |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1294 | ret = tcf_get_notify(net, portid, n, actions, event, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | else { /* delete */ |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1296 | ret = tcf_del_notify(net, n, actions, portid, attr_size, extack); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1297 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | goto err; |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1299 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
| 1301 | err: |
Cong Wang | edfaf94 | 2018-08-19 12:22:05 -0700 | [diff] [blame] | 1302 | tcf_action_put_many(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | return ret; |
| 1304 | } |
| 1305 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1306 | static int |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1307 | tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[], |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1308 | u32 portid, size_t attr_size, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | int err = 0; |
| 1312 | |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1313 | skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, |
| 1314 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (!skb) |
| 1316 | return -ENOBUFS; |
| 1317 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1318 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, |
| 1319 | RTM_NEWACTION, 0, 0) <= 0) { |
Roman Mashak | d143b9e | 2018-03-02 20:52:01 -0500 | [diff] [blame] | 1320 | NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1321 | kfree_skb(skb); |
| 1322 | return -EINVAL; |
| 1323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1325 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1326 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | if (err > 0) |
| 1328 | err = 0; |
| 1329 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1332 | static int tcf_action_add(struct net *net, struct nlattr *nla, |
Alexander Aring | aea0d72 | 2018-02-15 10:54:54 -0500 | [diff] [blame] | 1333 | struct nlmsghdr *n, u32 portid, int ovr, |
| 1334 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | { |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1336 | size_t attr_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | int ret = 0; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1338 | struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1340 | ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 1341 | &attr_size, true, extack); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1342 | if (ret < 0) |
WANG Cong | f07fed8 | 2016-08-13 22:34:56 -0700 | [diff] [blame] | 1343 | return ret; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1344 | ret = tcf_add_notify(net, n, actions, portid, attr_size, extack); |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 1345 | if (ovr) |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1346 | tcf_action_put_many(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Vlad Buslov | cae422f | 2018-07-05 17:24:31 +0300 | [diff] [blame] | 1348 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1351 | static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON; |
| 1352 | static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { |
| 1353 | [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32, |
| 1354 | .validation_data = &tcaa_root_flags_allowed }, |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1355 | [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1356 | }; |
| 1357 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1358 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, |
| 1359 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1361 | struct net *net = sock_net(skb->sk); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1362 | struct nlattr *tca[TCA_ROOT_MAX + 1]; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1363 | u32 portid = skb ? NETLINK_CB(skb).portid : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | int ret = 0, ovr = 0; |
| 1365 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1366 | if ((n->nlmsg_type != RTM_GETACTION) && |
| 1367 | !netlink_capable(skb, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1368 | return -EPERM; |
| 1369 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1370 | ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1371 | extack); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1372 | if (ret < 0) |
| 1373 | return ret; |
| 1374 | |
| 1375 | if (tca[TCA_ACT_TAB] == NULL) { |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1376 | NL_SET_ERR_MSG(extack, "Netlink action attributes missing"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | return -EINVAL; |
| 1378 | } |
| 1379 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1380 | /* n->nlmsg_flags & NLM_F_CREATE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | switch (n->nlmsg_type) { |
| 1382 | case RTM_NEWACTION: |
| 1383 | /* we are going to assume all other flags |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1384 | * imply create only if it doesn't exist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | * Note that CREATE | EXCL implies that |
| 1386 | * but since we want avoid ambiguity (eg when flags |
| 1387 | * is zero) then just set this |
| 1388 | */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1389 | if (n->nlmsg_flags & NLM_F_REPLACE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | ovr = 1; |
| 1391 | replay: |
Alexander Aring | aea0d72 | 2018-02-15 10:54:54 -0500 | [diff] [blame] | 1392 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr, |
| 1393 | extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | if (ret == -EAGAIN) |
| 1395 | goto replay; |
| 1396 | break; |
| 1397 | case RTM_DELACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1398 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1399 | portid, RTM_DELACTION, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | break; |
| 1401 | case RTM_GETACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1402 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Alexander Aring | 84ae017 | 2018-02-15 10:54:55 -0500 | [diff] [blame] | 1403 | portid, RTM_GETACTION, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | break; |
| 1405 | default: |
| 1406 | BUG(); |
| 1407 | } |
| 1408 | |
| 1409 | return ret; |
| 1410 | } |
| 1411 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1412 | static struct nlattr *find_dump_kind(struct nlattr **nla) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1414 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1415 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1416 | struct nlattr *kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1418 | tb1 = nla[TCA_ACT_TAB]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if (tb1 == NULL) |
| 1420 | return NULL; |
| 1421 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1422 | if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1423 | NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
Patrick McHardy | 6d834e0 | 2008-01-23 20:32:42 -0800 | [diff] [blame] | 1426 | if (tb[1] == NULL) |
| 1427 | return NULL; |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1428 | if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | return NULL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1430 | kind = tb2[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1432 | return kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1435 | static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 1437 | struct net *net = sock_net(skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1439 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1440 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | struct tc_action_ops *a_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | int ret = 0; |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1443 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1444 | struct nlattr *tb[TCA_ROOT_MAX + 1]; |
| 1445 | struct nlattr *count_attr = NULL; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1446 | unsigned long jiffy_since = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1447 | struct nlattr *kind = NULL; |
| 1448 | struct nla_bitfield32 bf; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1449 | u32 msecs_since = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1450 | u32 act_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1452 | ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX, |
David Ahern | dac9c97 | 2018-10-07 20:16:24 -0700 | [diff] [blame] | 1453 | tcaa_policy, cb->extack); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1454 | if (ret < 0) |
| 1455 | return ret; |
| 1456 | |
| 1457 | kind = find_dump_kind(tb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | if (kind == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 1459 | pr_info("tc_dump_action: action bad kind\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | return 0; |
| 1461 | } |
| 1462 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1463 | a_o = tc_lookup_action(kind); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1464 | if (a_o == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1467 | cb->args[2] = 0; |
| 1468 | if (tb[TCA_ROOT_FLAGS]) { |
| 1469 | bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); |
| 1470 | cb->args[2] = bf.value; |
| 1471 | } |
| 1472 | |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1473 | if (tb[TCA_ROOT_TIME_DELTA]) { |
| 1474 | msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); |
| 1475 | } |
| 1476 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1477 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1478 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
| 1479 | if (!nlh) |
| 1480 | goto out_module_put; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1481 | |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1482 | if (msecs_since) |
| 1483 | jiffy_since = jiffies - msecs_to_jiffies(msecs_since); |
| 1484 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1485 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1487 | t->tca__pad1 = 0; |
| 1488 | t->tca__pad2 = 0; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1489 | cb->args[3] = jiffy_since; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1490 | count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); |
| 1491 | if (!count_attr) |
| 1492 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1494 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 1495 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1496 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 1498 | ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (ret < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1500 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (ret > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1503 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | ret = skb->len; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1505 | act_count = cb->args[1]; |
| 1506 | memcpy(nla_data(count_attr), &act_count, sizeof(u32)); |
| 1507 | cb->args[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } else |
Jamal Hadi Salim | ebecaa6 | 2016-06-13 18:08:42 -0400 | [diff] [blame] | 1509 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1511 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1512 | if (NETLINK_CB(cb->skb).portid && ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | nlh->nlmsg_flags |= NLM_F_MULTI; |
| 1514 | module_put(a_o->owner); |
| 1515 | return skb->len; |
| 1516 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1517 | out_module_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | module_put(a_o->owner); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1519 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | return skb->len; |
| 1521 | } |
| 1522 | |
| 1523 | static int __init tc_action_init(void) |
| 1524 | { |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1525 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0); |
| 1526 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1527 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1528 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | subsys_initcall(tc_action_init); |