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; |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 37 | a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true); |
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 | { |
| 45 | tcf_chain_put(a->goto_chain); |
| 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 | |
Cong Wang | d7fb60b | 2017-09-11 16:33:30 -0700 | [diff] [blame] | 56 | /* XXX: For standalone actions, we don't need a RCU grace period either, because |
| 57 | * actions are always connected to filters and filters are already destroyed in |
| 58 | * RCU callbacks, so after a RCU grace period actions are already disconnected |
| 59 | * from filters. Readers later can not find us. |
| 60 | */ |
| 61 | static void free_tcf(struct tc_action *p) |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 62 | { |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 63 | free_percpu(p->cpu_bstats); |
| 64 | free_percpu(p->cpu_qstats); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 65 | |
| 66 | if (p->act_cookie) { |
| 67 | kfree(p->act_cookie->data); |
| 68 | kfree(p->act_cookie); |
| 69 | } |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 70 | if (p->goto_chain) |
| 71 | tcf_action_goto_chain_fini(p); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 72 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 73 | kfree(p); |
| 74 | } |
| 75 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 76 | static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 77 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 78 | spin_lock_bh(&idrinfo->lock); |
| 79 | idr_remove_ext(&idrinfo->action_idr, p->tcfa_index); |
| 80 | spin_unlock_bh(&idrinfo->lock); |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 81 | gen_kill_estimator(&p->tcfa_rate_est); |
Cong Wang | d7fb60b | 2017-09-11 16:33:30 -0700 | [diff] [blame] | 82 | free_tcf(p); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 83 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 84 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 85 | 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] | 86 | { |
| 87 | int ret = 0; |
| 88 | |
| 89 | if (p) { |
| 90 | if (bind) |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 91 | p->tcfa_bindcnt--; |
| 92 | else if (strict && p->tcfa_bindcnt > 0) |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 93 | return -EPERM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 94 | |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 95 | p->tcfa_refcnt--; |
| 96 | if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) { |
| 97 | if (p->ops->cleanup) |
| 98 | p->ops->cleanup(p, bind); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 99 | tcf_idr_remove(p->idrinfo, p); |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 100 | ret = ACT_P_DELETED; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 103 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 104 | return ret; |
| 105 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 106 | EXPORT_SYMBOL(__tcf_idr_release); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 107 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 108 | 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] | 109 | struct netlink_callback *cb) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 110 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 111 | int err = 0, index = -1, s_i = 0, n_i = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 112 | u32 act_flags = cb->args[2]; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 113 | unsigned long jiffy_since = cb->args[3]; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 114 | struct nlattr *nest; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 115 | struct idr *idr = &idrinfo->action_idr; |
| 116 | struct tc_action *p; |
| 117 | unsigned long id = 1; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 118 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 119 | spin_lock_bh(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 120 | |
| 121 | s_i = cb->args[0]; |
| 122 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 123 | idr_for_each_entry_ext(idr, p, id) { |
| 124 | index++; |
| 125 | if (index < s_i) |
| 126 | continue; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 127 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 128 | if (jiffy_since && |
| 129 | time_after(jiffy_since, |
| 130 | (unsigned long)p->tcfa_tm.lastuse)) |
| 131 | continue; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 132 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 133 | nest = nla_nest_start(skb, n_i); |
| 134 | if (!nest) |
| 135 | goto nla_put_failure; |
| 136 | err = tcf_action_dump_1(skb, p, 0, 0); |
| 137 | if (err < 0) { |
| 138 | index--; |
| 139 | nlmsg_trim(skb, nest); |
| 140 | goto done; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 141 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 142 | nla_nest_end(skb, nest); |
| 143 | n_i++; |
| 144 | if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) && |
| 145 | n_i >= TCA_ACT_MAX_PRIO) |
| 146 | goto done; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 147 | } |
| 148 | done: |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 149 | if (index >= 0) |
| 150 | cb->args[0] = index + 1; |
| 151 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 152 | spin_unlock_bh(&idrinfo->lock); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 153 | if (n_i) { |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 154 | if (act_flags & TCA_FLAG_LARGE_DUMP_ON) |
| 155 | cb->args[1] = n_i; |
| 156 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 157 | return n_i; |
| 158 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 159 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 160 | nla_nest_cancel(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 161 | goto done; |
| 162 | } |
| 163 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 164 | 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] | 165 | const struct tc_action_ops *ops) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 166 | { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 167 | struct nlattr *nest; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 168 | int n_i = 0; |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 169 | int ret = -EINVAL; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 170 | struct idr *idr = &idrinfo->action_idr; |
| 171 | struct tc_action *p; |
| 172 | unsigned long id = 1; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 173 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 174 | nest = nla_nest_start(skb, 0); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 175 | if (nest == NULL) |
| 176 | goto nla_put_failure; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 177 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 178 | goto nla_put_failure; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 179 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 180 | idr_for_each_entry_ext(idr, p, id) { |
| 181 | ret = __tcf_idr_release(p, false, true); |
| 182 | if (ret == ACT_P_DELETED) { |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame^] | 183 | module_put(ops->owner); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 184 | n_i++; |
| 185 | } else if (ret < 0) { |
| 186 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 189 | if (nla_put_u32(skb, TCA_FCNT, n_i)) |
| 190 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 191 | nla_nest_end(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 192 | |
| 193 | return n_i; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 194 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 195 | nla_nest_cancel(skb, nest); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 196 | return ret; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 197 | } |
| 198 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 199 | int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, |
| 200 | struct netlink_callback *cb, int type, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 201 | const struct tc_action_ops *ops) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 202 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 203 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 204 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 205 | if (type == RTM_DELACTION) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 206 | return tcf_del_walker(idrinfo, skb, ops); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 207 | } else if (type == RTM_GETACTION) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 208 | return tcf_dump_walker(idrinfo, skb, cb); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 209 | } else { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 210 | WARN(1, "tcf_generic_walker: unknown action %d\n", type); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | } |
| 213 | } |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 214 | EXPORT_SYMBOL(tcf_generic_walker); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 215 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 216 | static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 217 | { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 218 | struct tc_action *p = NULL; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 219 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 220 | spin_lock_bh(&idrinfo->lock); |
| 221 | p = idr_find_ext(&idrinfo->action_idr, index); |
| 222 | spin_unlock_bh(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 223 | |
| 224 | return p; |
| 225 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 226 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 227 | 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] | 228 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 229 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 230 | struct tc_action *p = tcf_idr_lookup(index, idrinfo); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 231 | |
| 232 | if (p) { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 233 | *a = p; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 234 | return 1; |
| 235 | } |
| 236 | return 0; |
| 237 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 238 | EXPORT_SYMBOL(tcf_idr_search); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 239 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 240 | bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a, |
| 241 | int bind) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 242 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 243 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 244 | struct tc_action *p = tcf_idr_lookup(index, idrinfo); |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 245 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 246 | if (index && p) { |
Jamal Hadi Salim | 76aab2c | 2008-08-07 20:37:22 -0700 | [diff] [blame] | 247 | if (bind) |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 248 | p->tcfa_bindcnt++; |
| 249 | p->tcfa_refcnt++; |
| 250 | *a = p; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 251 | return true; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 252 | } |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 253 | return false; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 254 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 255 | EXPORT_SYMBOL(tcf_idr_check); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 256 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 257 | void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est) |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 258 | { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 259 | if (est) |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 260 | gen_kill_estimator(&a->tcfa_rate_est); |
Cong Wang | d7fb60b | 2017-09-11 16:33:30 -0700 | [diff] [blame] | 261 | free_tcf(a); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 262 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 263 | EXPORT_SYMBOL(tcf_idr_cleanup); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 264 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 265 | int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, |
| 266 | struct tc_action **a, const struct tc_action_ops *ops, |
| 267 | int bind, bool cpustats) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 268 | { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 269 | struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 270 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
| 271 | struct idr *idr = &idrinfo->action_idr; |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 272 | int err = -ENOMEM; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 273 | unsigned long idr_index; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 274 | |
| 275 | if (unlikely(!p)) |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 276 | return -ENOMEM; |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 277 | p->tcfa_refcnt = 1; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 278 | if (bind) |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 279 | p->tcfa_bindcnt = 1; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 280 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 281 | if (cpustats) { |
| 282 | p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); |
| 283 | if (!p->cpu_bstats) { |
| 284 | err1: |
| 285 | kfree(p); |
| 286 | return err; |
| 287 | } |
| 288 | p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); |
| 289 | if (!p->cpu_qstats) { |
| 290 | err2: |
| 291 | free_percpu(p->cpu_bstats); |
| 292 | goto err1; |
| 293 | } |
| 294 | } |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 295 | spin_lock_init(&p->tcfa_lock); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 296 | /* user doesn't specify an index */ |
| 297 | if (!index) { |
Jakub Kicinski | 2c8468d | 2017-09-05 08:31:23 -0700 | [diff] [blame] | 298 | idr_preload(GFP_KERNEL); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 299 | spin_lock_bh(&idrinfo->lock); |
| 300 | err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0, |
Jakub Kicinski | 2c8468d | 2017-09-05 08:31:23 -0700 | [diff] [blame] | 301 | GFP_ATOMIC); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 302 | spin_unlock_bh(&idrinfo->lock); |
Jakub Kicinski | 2c8468d | 2017-09-05 08:31:23 -0700 | [diff] [blame] | 303 | idr_preload_end(); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 304 | if (err) { |
| 305 | err3: |
| 306 | free_percpu(p->cpu_qstats); |
| 307 | goto err2; |
| 308 | } |
| 309 | p->tcfa_index = idr_index; |
| 310 | } else { |
Jakub Kicinski | 2c8468d | 2017-09-05 08:31:23 -0700 | [diff] [blame] | 311 | idr_preload(GFP_KERNEL); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 312 | spin_lock_bh(&idrinfo->lock); |
| 313 | err = idr_alloc_ext(idr, NULL, NULL, index, index + 1, |
Jakub Kicinski | 2c8468d | 2017-09-05 08:31:23 -0700 | [diff] [blame] | 314 | GFP_ATOMIC); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 315 | spin_unlock_bh(&idrinfo->lock); |
Jakub Kicinski | 2c8468d | 2017-09-05 08:31:23 -0700 | [diff] [blame] | 316 | idr_preload_end(); |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 317 | if (err) |
| 318 | goto err3; |
| 319 | p->tcfa_index = index; |
| 320 | } |
| 321 | |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 322 | p->tcfa_tm.install = jiffies; |
| 323 | p->tcfa_tm.lastuse = jiffies; |
| 324 | p->tcfa_tm.firstuse = 0; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 325 | if (est) { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 326 | err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, |
| 327 | &p->tcfa_rate_est, |
| 328 | &p->tcfa_lock, NULL, est); |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 329 | if (err) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 330 | goto err3; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 334 | p->idrinfo = idrinfo; |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 335 | p->ops = ops; |
| 336 | INIT_LIST_HEAD(&p->list); |
| 337 | *a = p; |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 338 | return 0; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 339 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 340 | EXPORT_SYMBOL(tcf_idr_create); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 341 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 342 | 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] | 343 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 344 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 345 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 346 | spin_lock_bh(&idrinfo->lock); |
| 347 | idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index); |
| 348 | spin_unlock_bh(&idrinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 349 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 350 | EXPORT_SYMBOL(tcf_idr_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 352 | void tcf_idrinfo_destroy(const struct tc_action_ops *ops, |
| 353 | struct tcf_idrinfo *idrinfo) |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 354 | { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 355 | struct idr *idr = &idrinfo->action_idr; |
| 356 | struct tc_action *p; |
| 357 | int ret; |
| 358 | unsigned long id = 1; |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 359 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 360 | idr_for_each_entry_ext(idr, p, id) { |
| 361 | ret = __tcf_idr_release(p, false, true); |
| 362 | if (ret == ACT_P_DELETED) |
| 363 | module_put(ops->owner); |
| 364 | else if (ret < 0) |
| 365 | return; |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 366 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 367 | idr_destroy(&idrinfo->action_idr); |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 368 | } |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 369 | EXPORT_SYMBOL(tcf_idrinfo_destroy); |
WANG Cong | 1d4150c | 2016-02-22 15:57:52 -0800 | [diff] [blame] | 370 | |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 371 | static LIST_HEAD(act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | static DEFINE_RWLOCK(act_mod_lock); |
| 373 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 374 | int tcf_register_action(struct tc_action_ops *act, |
| 375 | struct pernet_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 377 | struct tc_action_ops *a; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 378 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 380 | if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 383 | /* We have to register pernet ops before making the action ops visible, |
| 384 | * otherwise tcf_action_init_1() could get a partially initialized |
| 385 | * netns. |
| 386 | */ |
| 387 | ret = register_pernet_subsys(ops); |
| 388 | if (ret) |
| 389 | return ret; |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | write_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 392 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { |
| 394 | write_unlock(&act_mod_lock); |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 395 | unregister_pernet_subsys(ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return -EEXIST; |
| 397 | } |
| 398 | } |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 399 | list_add_tail(&act->head, &act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | write_unlock(&act_mod_lock); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return 0; |
| 403 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 404 | EXPORT_SYMBOL(tcf_register_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 406 | int tcf_unregister_action(struct tc_action_ops *act, |
| 407 | struct pernet_operations *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 409 | struct tc_action_ops *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | int err = -ENOENT; |
| 411 | |
| 412 | write_lock(&act_mod_lock); |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 413 | list_for_each_entry(a, &act_base, head) { |
| 414 | if (a == act) { |
| 415 | list_del(&act->head); |
| 416 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | break; |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | write_unlock(&act_mod_lock); |
WANG Cong | ab102b8 | 2016-10-11 10:56:45 -0700 | [diff] [blame] | 421 | if (!err) |
| 422 | unregister_pernet_subsys(ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return err; |
| 424 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 425 | EXPORT_SYMBOL(tcf_unregister_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | /* lookup by name */ |
| 428 | static struct tc_action_ops *tc_lookup_action_n(char *kind) |
| 429 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 430 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | if (kind) { |
| 433 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 434 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 436 | if (try_module_get(a->owner)) |
| 437 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | } |
| 441 | read_unlock(&act_mod_lock); |
| 442 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 443 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 446 | /* lookup by nlattr */ |
| 447 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 449 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | if (kind) { |
| 452 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 453 | list_for_each_entry(a, &act_base, head) { |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 454 | if (nla_strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 455 | if (try_module_get(a->owner)) |
| 456 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | break; |
| 458 | } |
| 459 | } |
| 460 | read_unlock(&act_mod_lock); |
| 461 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 462 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 465 | /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */ |
| 466 | #define TCA_ACT_MAX_PRIO_MASK 0x1FF |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 467 | int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, |
| 468 | int nr_actions, struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 470 | u32 jmp_prgcnt = 0; |
| 471 | u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ |
Jiri Pirko | ec1a9cc | 2017-08-04 14:29:02 +0200 | [diff] [blame] | 472 | int i; |
| 473 | int ret = TC_ACT_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Willem de Bruijn | e7246e1 | 2017-01-07 17:06:35 -0500 | [diff] [blame] | 475 | if (skb_skip_tc_classify(skb)) |
| 476 | return TC_ACT_OK; |
| 477 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 478 | restart_act_graph: |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 479 | for (i = 0; i < nr_actions; i++) { |
| 480 | const struct tc_action *a = actions[i]; |
| 481 | |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 482 | if (jmp_prgcnt > 0) { |
| 483 | jmp_prgcnt -= 1; |
| 484 | continue; |
| 485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | repeat: |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 487 | ret = a->ops->act(skb, a, res); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 488 | if (ret == TC_ACT_REPEAT) |
| 489 | goto repeat; /* we need a ttl - JHS */ |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 490 | |
Jiri Pirko | 9da3242 | 2017-05-02 10:12:00 +0200 | [diff] [blame] | 491 | if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 492 | jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; |
| 493 | if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { |
| 494 | /* faulty opcode, stop pipeline */ |
| 495 | return TC_ACT_OK; |
| 496 | } else { |
| 497 | jmp_ttl -= 1; |
| 498 | if (jmp_ttl > 0) |
| 499 | goto restart_act_graph; |
| 500 | else /* faulty graph, stop pipeline */ |
| 501 | return TC_ACT_OK; |
| 502 | } |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 503 | } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { |
| 504 | tcf_action_goto_chain_exec(a, res); |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 507 | if (ret != TC_ACT_PIPE) |
Willem de Bruijn | e7246e1 | 2017-01-07 17:06:35 -0500 | [diff] [blame] | 508 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Jamal Hadi Salim | e0ee84d | 2017-04-23 13:17:28 -0400 | [diff] [blame] | 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return ret; |
| 512 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 513 | EXPORT_SYMBOL(tcf_action_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 515 | int tcf_action_destroy(struct list_head *actions, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame^] | 517 | const struct tc_action_ops *ops; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 518 | struct tc_action *a, *tmp; |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 519 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 521 | list_for_each_entry_safe(a, tmp, actions, list) { |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame^] | 522 | ops = a->ops; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 523 | ret = __tcf_idr_release(a, bind, true); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 524 | if (ret == ACT_P_DELETED) |
Jiri Pirko | 255cd50 | 2017-09-13 17:32:37 +0200 | [diff] [blame^] | 525 | module_put(ops->owner); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 526 | else if (ret < 0) |
| 527 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 529 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | int |
| 533 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 534 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | return a->ops->dump(skb, a, bind, ref); |
| 536 | } |
| 537 | |
| 538 | int |
| 539 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 540 | { |
| 541 | int err = -EINVAL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 542 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 543 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 545 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
| 546 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (tcf_action_copy_stats(skb, a, 0)) |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 548 | goto nla_put_failure; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 549 | if (a->act_cookie) { |
| 550 | if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len, |
| 551 | a->act_cookie->data)) |
| 552 | goto nla_put_failure; |
| 553 | } |
| 554 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 555 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 556 | if (nest == NULL) |
| 557 | goto nla_put_failure; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 558 | err = tcf_action_dump_old(skb, a, bind, ref); |
| 559 | if (err > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 560 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return err; |
| 562 | } |
| 563 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 564 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 565 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return -1; |
| 567 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 568 | EXPORT_SYMBOL(tcf_action_dump_1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 570 | int tcf_action_dump(struct sk_buff *skb, struct list_head *actions, |
| 571 | int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
| 573 | struct tc_action *a; |
| 574 | int err = -EINVAL; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 575 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 577 | list_for_each_entry(a, actions, list) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 578 | nest = nla_nest_start(skb, a->order); |
| 579 | if (nest == NULL) |
| 580 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | err = tcf_action_dump_1(skb, a, bind, ref); |
| 582 | if (err < 0) |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 583 | goto errout; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 584 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 589 | nla_put_failure: |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 590 | err = -EINVAL; |
| 591 | errout: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 592 | nla_nest_cancel(skb, nest); |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 593 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 596 | static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 597 | { |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 598 | struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 599 | if (!c) |
| 600 | return NULL; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 601 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 602 | c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); |
| 603 | if (!c->data) { |
| 604 | kfree(c); |
| 605 | return NULL; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 606 | } |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 607 | c->len = nla_len(tb[TCA_ACT_COOKIE]); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 608 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 609 | return c; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 610 | } |
| 611 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 612 | struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, |
| 613 | struct nlattr *nla, struct nlattr *est, |
| 614 | char *name, int ovr, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
| 616 | struct tc_action *a; |
| 617 | struct tc_action_ops *a_o; |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 618 | struct tc_cookie *cookie = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | char act_name[IFNAMSIZ]; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 620 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 621 | struct nlattr *kind; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 622 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (name == NULL) { |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 625 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 626 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | goto err_out; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 628 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 629 | kind = tb[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | if (kind == NULL) |
| 631 | goto err_out; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 632 | if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | goto err_out; |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 634 | if (tb[TCA_ACT_COOKIE]) { |
| 635 | int cklen = nla_len(tb[TCA_ACT_COOKIE]); |
| 636 | |
| 637 | if (cklen > TC_COOKIE_MAX_SIZE) |
| 638 | goto err_out; |
| 639 | |
| 640 | cookie = nla_memdup_cookie(tb); |
| 641 | if (!cookie) { |
| 642 | err = -ENOMEM; |
| 643 | goto err_out; |
| 644 | } |
| 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } else { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 647 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) |
| 649 | goto err_out; |
| 650 | } |
| 651 | |
| 652 | a_o = tc_lookup_action_n(act_name); |
| 653 | if (a_o == NULL) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 654 | #ifdef CONFIG_MODULES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | rtnl_unlock(); |
Patrick McHardy | 4bba392 | 2006-01-08 22:22:14 -0800 | [diff] [blame] | 656 | request_module("act_%s", act_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | rtnl_lock(); |
| 658 | |
| 659 | a_o = tc_lookup_action_n(act_name); |
| 660 | |
| 661 | /* We dropped the RTNL semaphore in order to |
| 662 | * perform the module load. So, even if we |
| 663 | * succeeded in loading the module we have to |
| 664 | * tell the caller to replay the request. We |
| 665 | * indicate this using -EAGAIN. |
| 666 | */ |
| 667 | if (a_o != NULL) { |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 668 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | goto err_mod; |
| 670 | } |
| 671 | #endif |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 672 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | goto err_out; |
| 674 | } |
| 675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | /* backward compatibility for policer */ |
| 677 | if (name == NULL) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 678 | err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | else |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 680 | err = a_o->init(net, nla, est, &a, ovr, bind); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 681 | if (err < 0) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 682 | goto err_mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 684 | if (name == NULL && tb[TCA_ACT_COOKIE]) { |
| 685 | if (a->act_cookie) { |
| 686 | kfree(a->act_cookie->data); |
| 687 | kfree(a->act_cookie); |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 688 | } |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 689 | a->act_cookie = cookie; |
Jamal Hadi Salim | 1045ba7 | 2017-01-24 07:02:41 -0500 | [diff] [blame] | 690 | } |
| 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | /* module count goes up only when brand new policy is created |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 693 | * if it exists and is only bound to in a_o->init() then |
| 694 | * ACT_P_CREATED is not returned (a zero is). |
| 695 | */ |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 696 | if (err != ACT_P_CREATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | module_put(a_o->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 699 | if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) { |
| 700 | err = tcf_action_goto_chain_init(a, tp); |
| 701 | if (err) { |
| 702 | LIST_HEAD(actions); |
| 703 | |
| 704 | list_add_tail(&a->list, &actions); |
| 705 | tcf_action_destroy(&actions, bind); |
| 706 | return ERR_PTR(err); |
| 707 | } |
| 708 | } |
| 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | return a; |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | err_mod: |
| 713 | module_put(a_o->owner); |
| 714 | err_out: |
Wolfgang Bumiller | e0535ce | 2017-04-20 14:08:26 +0200 | [diff] [blame] | 715 | if (cookie) { |
| 716 | kfree(cookie->data); |
| 717 | kfree(cookie); |
| 718 | } |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 719 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Jamal Hadi Salim | aecc5ce | 2016-09-19 19:02:51 -0400 | [diff] [blame] | 722 | static void cleanup_a(struct list_head *actions, int ovr) |
| 723 | { |
| 724 | struct tc_action *a; |
| 725 | |
| 726 | if (!ovr) |
| 727 | return; |
| 728 | |
| 729 | list_for_each_entry(a, actions, list) |
| 730 | a->tcfa_refcnt--; |
| 731 | } |
| 732 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 733 | int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, |
| 734 | struct nlattr *est, char *name, int ovr, int bind, |
| 735 | struct list_head *actions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 737 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 738 | struct tc_action *act; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 739 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | int i; |
| 741 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 742 | err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 743 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 744 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 746 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 747 | act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 748 | if (IS_ERR(act)) { |
| 749 | err = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | goto err; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 751 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 752 | act->order = i; |
Jamal Hadi Salim | aecc5ce | 2016-09-19 19:02:51 -0400 | [diff] [blame] | 753 | if (ovr) |
| 754 | act->tcfa_refcnt++; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 755 | list_add_tail(&act->list, actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
Jamal Hadi Salim | aecc5ce | 2016-09-19 19:02:51 -0400 | [diff] [blame] | 757 | |
| 758 | /* Remove the temp refcnt which was necessary to protect against |
| 759 | * destroying an existing action which was being replaced |
| 760 | */ |
| 761 | cleanup_a(actions, ovr); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 762 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
| 764 | err: |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 765 | tcf_action_destroy(actions, bind); |
| 766 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 769 | 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] | 770 | int compat_mode) |
| 771 | { |
| 772 | int err = 0; |
| 773 | struct gnet_dump d; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 774 | |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 775 | if (p == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | goto errout; |
| 777 | |
| 778 | /* compat_mode being true specifies a call that is supposed |
Dirk Hohndel | 06fe9fb | 2009-09-28 21:43:57 -0400 | [diff] [blame] | 779 | * to add additional backward compatibility statistic TLVs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | */ |
| 781 | if (compat_mode) { |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 782 | if (p->type == TCA_OLD_COMPAT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | err = gnet_stats_start_copy_compat(skb, 0, |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 784 | TCA_STATS, |
| 785 | TCA_XSTATS, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 786 | &p->tcfa_lock, &d, |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 787 | TCA_PAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | else |
| 789 | return 0; |
| 790 | } else |
| 791 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 792 | &p->tcfa_lock, &d, TCA_ACT_PAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | if (err < 0) |
| 795 | goto errout; |
| 796 | |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 797 | if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 || |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 798 | gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 799 | gnet_stats_copy_queue(&d, p->cpu_qstats, |
WANG Cong | ec0595c | 2016-07-25 16:09:42 -0700 | [diff] [blame] | 800 | &p->tcfa_qstats, |
| 801 | p->tcfa_qstats.qlen) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | goto errout; |
| 803 | |
| 804 | if (gnet_stats_finish_copy(&d) < 0) |
| 805 | goto errout; |
| 806 | |
| 807 | return 0; |
| 808 | |
| 809 | errout: |
| 810 | return -1; |
| 811 | } |
| 812 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 813 | static int tca_get_fill(struct sk_buff *skb, struct list_head *actions, |
| 814 | u32 portid, u32 seq, u16 flags, int event, int bind, |
| 815 | int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
| 817 | struct tcamsg *t; |
| 818 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 819 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 820 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 822 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 823 | if (!nlh) |
| 824 | goto out_nlmsg_trim; |
| 825 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 827 | t->tca__pad1 = 0; |
| 828 | t->tca__pad2 = 0; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 829 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 830 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 831 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 832 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 834 | if (tcf_action_dump(skb, actions, bind, ref) < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 835 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 837 | nla_nest_end(skb, nest); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 838 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 839 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | return skb->len; |
| 841 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 842 | out_nlmsg_trim: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 843 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | return -1; |
| 845 | } |
| 846 | |
| 847 | static int |
Roman Mashak | c4c4290 | 2017-07-13 13:12:18 -0400 | [diff] [blame] | 848 | tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 849 | struct list_head *actions, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { |
| 851 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
| 853 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 854 | if (!skb) |
| 855 | return -ENOBUFS; |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 856 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, |
| 857 | 0, 0) <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | kfree_skb(skb); |
| 859 | return -EINVAL; |
| 860 | } |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 861 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 862 | return rtnl_unicast(skb, net, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 865 | static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, |
| 866 | struct nlmsghdr *n, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 868 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 869 | const struct tc_action_ops *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | struct tc_action *a; |
| 871 | int index; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 872 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 874 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 875 | if (err < 0) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 876 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 878 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 879 | if (tb[TCA_ACT_INDEX] == NULL || |
| 880 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 881 | goto err_out; |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 882 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 884 | err = -EINVAL; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 885 | ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
| 886 | if (!ops) /* could happen in batch of actions */ |
| 887 | goto err_out; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 888 | err = -ENOENT; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 889 | if (ops->lookup(net, &a, index) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | goto err_mod; |
| 891 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 892 | module_put(ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return a; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | err_mod: |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 896 | module_put(ops->owner); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 897 | err_out: |
| 898 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 901 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 902 | struct nlmsghdr *n, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
| 904 | struct sk_buff *skb; |
| 905 | unsigned char *b; |
| 906 | struct nlmsghdr *nlh; |
| 907 | struct tcamsg *t; |
| 908 | struct netlink_callback dcb; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 909 | struct nlattr *nest; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 910 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 911 | const struct tc_action_ops *ops; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 912 | struct nlattr *kind; |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 913 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 916 | if (!skb) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 917 | pr_debug("tca_action_flush: failed skb alloc\n"); |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 918 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 921 | b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 923 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 924 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | goto err_out; |
| 926 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 927 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 928 | kind = tb[TCA_ACT_KIND]; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 929 | ops = tc_lookup_action(kind); |
| 930 | if (!ops) /*some idjot trying to flush unknown action */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | goto err_out; |
| 932 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 933 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, |
| 934 | sizeof(*t), 0); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 935 | if (!nlh) |
| 936 | goto out_module_put; |
| 937 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 939 | t->tca__pad1 = 0; |
| 940 | t->tca__pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 942 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 943 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 944 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 946 | err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops); |
Roman Mashak | edb9d1b | 2017-02-24 11:00:32 -0500 | [diff] [blame] | 947 | if (err <= 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 948 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 950 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 952 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | nlh->nlmsg_flags |= NLM_F_ROOT; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 954 | module_put(ops->owner); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 955 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 956 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | if (err > 0) |
| 958 | return 0; |
| 959 | |
| 960 | return err; |
| 961 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 962 | out_module_put: |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 963 | module_put(ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | err_out: |
| 965 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return err; |
| 967 | } |
| 968 | |
| 969 | static int |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 970 | tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, |
| 971 | u32 portid) |
| 972 | { |
| 973 | int ret; |
| 974 | struct sk_buff *skb; |
| 975 | |
| 976 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 977 | if (!skb) |
| 978 | return -ENOBUFS; |
| 979 | |
| 980 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, |
| 981 | 0, 1) <= 0) { |
| 982 | kfree_skb(skb); |
| 983 | return -EINVAL; |
| 984 | } |
| 985 | |
| 986 | /* now do the delete */ |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 987 | ret = tcf_action_destroy(actions, 0); |
| 988 | if (ret < 0) { |
| 989 | kfree_skb(skb); |
| 990 | return ret; |
| 991 | } |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 992 | |
| 993 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 994 | n->nlmsg_flags & NLM_F_ECHO); |
| 995 | if (ret > 0) |
| 996 | return 0; |
| 997 | return ret; |
| 998 | } |
| 999 | |
| 1000 | static int |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1001 | tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1002 | u32 portid, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1004 | int i, ret; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1005 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1006 | struct tc_action *act; |
| 1007 | LIST_HEAD(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1009 | ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 1010 | if (ret < 0) |
| 1011 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1013 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 1014 | if (tb[1] != NULL) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1015 | return tca_action_flush(net, tb[1], n, portid); |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 1016 | else |
| 1017 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1020 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 1021 | act = tcf_action_get_1(net, tb[i], n, portid); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1022 | if (IS_ERR(act)) { |
| 1023 | ret = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | goto err; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1025 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1026 | act->order = i; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1027 | list_add_tail(&act->list, &actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (event == RTM_GETACTION) |
Roman Mashak | c4c4290 | 2017-07-13 13:12:18 -0400 | [diff] [blame] | 1031 | ret = tcf_get_notify(net, portid, n, &actions, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | else { /* delete */ |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1033 | ret = tcf_del_notify(net, n, &actions, portid); |
| 1034 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | return ret; |
| 1037 | } |
| 1038 | err: |
Jamal Hadi Salim | 0faa9cb | 2017-01-15 10:14:06 -0500 | [diff] [blame] | 1039 | if (event != RTM_GETACTION) |
| 1040 | tcf_action_destroy(&actions, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | return ret; |
| 1042 | } |
| 1043 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1044 | static int |
| 1045 | tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, |
| 1046 | u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | int err = 0; |
| 1050 | |
| 1051 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1052 | if (!skb) |
| 1053 | return -ENOBUFS; |
| 1054 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1055 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, |
| 1056 | RTM_NEWACTION, 0, 0) <= 0) { |
| 1057 | kfree_skb(skb); |
| 1058 | return -EINVAL; |
| 1059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 1061 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1062 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | if (err > 0) |
| 1064 | err = 0; |
| 1065 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1068 | static int tcf_action_add(struct net *net, struct nlattr *nla, |
| 1069 | struct nlmsghdr *n, u32 portid, int ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | { |
| 1071 | int ret = 0; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1072 | LIST_HEAD(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 1074 | ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1075 | if (ret) |
WANG Cong | f07fed8 | 2016-08-13 22:34:56 -0700 | [diff] [blame] | 1076 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
WANG Cong | f07fed8 | 2016-08-13 22:34:56 -0700 | [diff] [blame] | 1078 | return tcf_add_notify(net, n, &actions, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1081 | static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON; |
| 1082 | static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { |
| 1083 | [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32, |
| 1084 | .validation_data = &tcaa_root_flags_allowed }, |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1085 | [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1086 | }; |
| 1087 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1088 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, |
| 1089 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1091 | struct net *net = sock_net(skb->sk); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1092 | struct nlattr *tca[TCA_ROOT_MAX + 1]; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1093 | u32 portid = skb ? NETLINK_CB(skb).portid : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | int ret = 0, ovr = 0; |
| 1095 | |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 1096 | if ((n->nlmsg_type != RTM_GETACTION) && |
| 1097 | !netlink_capable(skb, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1098 | return -EPERM; |
| 1099 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1100 | ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1101 | extack); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1102 | if (ret < 0) |
| 1103 | return ret; |
| 1104 | |
| 1105 | if (tca[TCA_ACT_TAB] == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 1106 | pr_notice("tc_ctl_action: received NO action attribs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | return -EINVAL; |
| 1108 | } |
| 1109 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1110 | /* n->nlmsg_flags & NLM_F_CREATE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | switch (n->nlmsg_type) { |
| 1112 | case RTM_NEWACTION: |
| 1113 | /* we are going to assume all other flags |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1114 | * imply create only if it doesn't exist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | * Note that CREATE | EXCL implies that |
| 1116 | * but since we want avoid ambiguity (eg when flags |
| 1117 | * is zero) then just set this |
| 1118 | */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1119 | if (n->nlmsg_flags & NLM_F_REPLACE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | ovr = 1; |
| 1121 | replay: |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1122 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | if (ret == -EAGAIN) |
| 1124 | goto replay; |
| 1125 | break; |
| 1126 | case RTM_DELACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1127 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1128 | portid, RTM_DELACTION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | break; |
| 1130 | case RTM_GETACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1131 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1132 | portid, RTM_GETACTION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | break; |
| 1134 | default: |
| 1135 | BUG(); |
| 1136 | } |
| 1137 | |
| 1138 | return ret; |
| 1139 | } |
| 1140 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1141 | static struct nlattr *find_dump_kind(struct nlattr **nla) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1143 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1144 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1145 | struct nlattr *kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1147 | tb1 = nla[TCA_ACT_TAB]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | if (tb1 == NULL) |
| 1149 | return NULL; |
| 1150 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1151 | if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1152 | NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Patrick McHardy | 6d834e0 | 2008-01-23 20:32:42 -0800 | [diff] [blame] | 1155 | if (tb[1] == NULL) |
| 1156 | return NULL; |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1157 | 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] | 1158 | return NULL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1159 | kind = tb2[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1161 | return kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1164 | 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] | 1165 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 1166 | struct net *net = sock_net(skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1168 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1169 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | struct tc_action_ops *a_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | int ret = 0; |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1172 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1173 | struct nlattr *tb[TCA_ROOT_MAX + 1]; |
| 1174 | struct nlattr *count_attr = NULL; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1175 | unsigned long jiffy_since = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1176 | struct nlattr *kind = NULL; |
| 1177 | struct nla_bitfield32 bf; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1178 | u32 msecs_since = 0; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1179 | u32 act_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1181 | ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX, |
| 1182 | tcaa_policy, NULL); |
| 1183 | if (ret < 0) |
| 1184 | return ret; |
| 1185 | |
| 1186 | kind = find_dump_kind(tb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | if (kind == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 1188 | pr_info("tc_dump_action: action bad kind\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | return 0; |
| 1190 | } |
| 1191 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1192 | a_o = tc_lookup_action(kind); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1193 | if (a_o == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1196 | cb->args[2] = 0; |
| 1197 | if (tb[TCA_ROOT_FLAGS]) { |
| 1198 | bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); |
| 1199 | cb->args[2] = bf.value; |
| 1200 | } |
| 1201 | |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1202 | if (tb[TCA_ROOT_TIME_DELTA]) { |
| 1203 | msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); |
| 1204 | } |
| 1205 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1206 | 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] | 1207 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
| 1208 | if (!nlh) |
| 1209 | goto out_module_put; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1210 | |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1211 | if (msecs_since) |
| 1212 | jiffy_since = jiffies - msecs_to_jiffies(msecs_since); |
| 1213 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1214 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1216 | t->tca__pad1 = 0; |
| 1217 | t->tca__pad2 = 0; |
Jamal Hadi Salim | e62e484 | 2017-07-30 13:24:52 -0400 | [diff] [blame] | 1218 | cb->args[3] = jiffy_since; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1219 | count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); |
| 1220 | if (!count_attr) |
| 1221 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1223 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 1224 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1225 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 1227 | ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | if (ret < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1229 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
| 1231 | if (ret > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1232 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | ret = skb->len; |
Jamal Hadi Salim | 90825b2 | 2017-07-30 13:24:51 -0400 | [diff] [blame] | 1234 | act_count = cb->args[1]; |
| 1235 | memcpy(nla_data(count_attr), &act_count, sizeof(u32)); |
| 1236 | cb->args[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } else |
Jamal Hadi Salim | ebecaa6 | 2016-06-13 18:08:42 -0400 | [diff] [blame] | 1238 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1240 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1241 | if (NETLINK_CB(cb->skb).portid && ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | nlh->nlmsg_flags |= NLM_F_MULTI; |
| 1243 | module_put(a_o->owner); |
| 1244 | return skb->len; |
| 1245 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1246 | out_module_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | module_put(a_o->owner); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1248 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | return skb->len; |
| 1250 | } |
| 1251 | |
| 1252 | static int __init tc_action_init(void) |
| 1253 | { |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1254 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0); |
| 1255 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1256 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1257 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | subsys_initcall(tc_action_init); |