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> |
| 27 | #include <net/act_api.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 28 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 30 | void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo) |
| 31 | { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 32 | spin_lock_bh(&hinfo->lock); |
| 33 | hlist_del(&p->tcfc_head); |
| 34 | spin_unlock_bh(&hinfo->lock); |
| 35 | gen_kill_estimator(&p->tcfc_bstats, |
| 36 | &p->tcfc_rate_est); |
| 37 | /* |
| 38 | * gen_estimator est_timer() might access p->tcfc_lock |
| 39 | * or bstats, wait a RCU grace period before freeing p |
| 40 | */ |
| 41 | kfree_rcu(p, tcfc_rcu); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 42 | } |
| 43 | EXPORT_SYMBOL(tcf_hash_destroy); |
| 44 | |
| 45 | int tcf_hash_release(struct tcf_common *p, int bind, |
| 46 | struct tcf_hashinfo *hinfo) |
| 47 | { |
| 48 | int ret = 0; |
| 49 | |
| 50 | if (p) { |
| 51 | if (bind) |
| 52 | p->tcfc_bindcnt--; |
| 53 | |
| 54 | p->tcfc_refcnt--; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 55 | if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 56 | tcf_hash_destroy(p, hinfo); |
| 57 | ret = 1; |
| 58 | } |
| 59 | } |
| 60 | return ret; |
| 61 | } |
| 62 | EXPORT_SYMBOL(tcf_hash_release); |
| 63 | |
| 64 | static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb, |
| 65 | struct tc_action *a, struct tcf_hashinfo *hinfo) |
| 66 | { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 67 | struct hlist_head *head; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 68 | struct tcf_common *p; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 69 | int err = 0, index = -1, i = 0, s_i = 0, n_i = 0; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 70 | struct nlattr *nest; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 71 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 72 | spin_lock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 73 | |
| 74 | s_i = cb->args[0]; |
| 75 | |
| 76 | for (i = 0; i < (hinfo->hmask + 1); i++) { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 77 | head = &hinfo->htab[tcf_hash(i, hinfo->hmask)]; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 78 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 79 | hlist_for_each_entry_rcu(p, head, tcfc_head) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 80 | index++; |
| 81 | if (index < s_i) |
| 82 | continue; |
| 83 | a->priv = p; |
| 84 | a->order = n_i; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 85 | |
| 86 | nest = nla_nest_start(skb, a->order); |
| 87 | if (nest == NULL) |
| 88 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 89 | err = tcf_action_dump_1(skb, a, 0, 0); |
| 90 | if (err < 0) { |
| 91 | index--; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 92 | nlmsg_trim(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 93 | goto done; |
| 94 | } |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 95 | nla_nest_end(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 96 | n_i++; |
| 97 | if (n_i >= TCA_ACT_MAX_PRIO) |
| 98 | goto done; |
| 99 | } |
| 100 | } |
| 101 | done: |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 102 | spin_unlock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 103 | if (n_i) |
| 104 | cb->args[0] += n_i; |
| 105 | return n_i; |
| 106 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 107 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 108 | nla_nest_cancel(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 109 | goto done; |
| 110 | } |
| 111 | |
| 112 | static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a, |
| 113 | struct tcf_hashinfo *hinfo) |
| 114 | { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 115 | struct hlist_head *head; |
| 116 | struct hlist_node *n; |
| 117 | struct tcf_common *p; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 118 | struct nlattr *nest; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 119 | int i = 0, n_i = 0; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 120 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 121 | nest = nla_nest_start(skb, a->order); |
| 122 | if (nest == NULL) |
| 123 | goto nla_put_failure; |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 124 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
| 125 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 126 | for (i = 0; i < (hinfo->hmask + 1); i++) { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 127 | head = &hinfo->htab[tcf_hash(i, hinfo->hmask)]; |
| 128 | hlist_for_each_entry_safe(p, n, head, tcfc_head) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 129 | if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo)) |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 130 | module_put(a->ops->owner); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 131 | n_i++; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 134 | if (nla_put_u32(skb, TCA_FCNT, n_i)) |
| 135 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 136 | nla_nest_end(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 137 | |
| 138 | return n_i; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 139 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 140 | nla_nest_cancel(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb, |
| 145 | int type, struct tc_action *a) |
| 146 | { |
| 147 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
| 148 | |
| 149 | if (type == RTM_DELACTION) { |
| 150 | return tcf_del_walker(skb, a, hinfo); |
| 151 | } else if (type == RTM_GETACTION) { |
| 152 | return tcf_dump_walker(skb, cb, a, hinfo); |
| 153 | } else { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 154 | WARN(1, "tcf_generic_walker: unknown action %d\n", type); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 155 | return -EINVAL; |
| 156 | } |
| 157 | } |
| 158 | EXPORT_SYMBOL(tcf_generic_walker); |
| 159 | |
| 160 | struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo) |
| 161 | { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 162 | struct tcf_common *p = NULL; |
| 163 | struct hlist_head *head; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 164 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 165 | spin_lock_bh(&hinfo->lock); |
| 166 | head = &hinfo->htab[tcf_hash(index, hinfo->hmask)]; |
| 167 | hlist_for_each_entry_rcu(p, head, tcfc_head) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 168 | if (p->tcfc_index == index) |
| 169 | break; |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 170 | spin_unlock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 171 | |
| 172 | return p; |
| 173 | } |
| 174 | EXPORT_SYMBOL(tcf_hash_lookup); |
| 175 | |
| 176 | u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo) |
| 177 | { |
| 178 | u32 val = *idx_gen; |
| 179 | |
| 180 | do { |
| 181 | if (++val == 0) |
| 182 | val = 1; |
| 183 | } while (tcf_hash_lookup(val, hinfo)); |
| 184 | |
Yang Yingliang | 17569fa | 2013-12-10 20:55:29 +0800 | [diff] [blame] | 185 | *idx_gen = val; |
| 186 | return val; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL(tcf_hash_new_index); |
| 189 | |
| 190 | int tcf_hash_search(struct tc_action *a, u32 index) |
| 191 | { |
| 192 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
| 193 | struct tcf_common *p = tcf_hash_lookup(index, hinfo); |
| 194 | |
| 195 | if (p) { |
| 196 | a->priv = p; |
| 197 | return 1; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | EXPORT_SYMBOL(tcf_hash_search); |
| 202 | |
| 203 | struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind, |
| 204 | struct tcf_hashinfo *hinfo) |
| 205 | { |
| 206 | struct tcf_common *p = NULL; |
| 207 | if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) { |
Jamal Hadi Salim | 76aab2c | 2008-08-07 20:37:22 -0700 | [diff] [blame] | 208 | if (bind) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 209 | p->tcfc_bindcnt++; |
Jamal Hadi Salim | 76aab2c | 2008-08-07 20:37:22 -0700 | [diff] [blame] | 210 | p->tcfc_refcnt++; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 211 | a->priv = p; |
| 212 | } |
| 213 | return p; |
| 214 | } |
| 215 | EXPORT_SYMBOL(tcf_hash_check); |
| 216 | |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 217 | struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est, |
| 218 | struct tc_action *a, int size, int bind, |
| 219 | u32 *idx_gen, struct tcf_hashinfo *hinfo) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 220 | { |
| 221 | struct tcf_common *p = kzalloc(size, GFP_KERNEL); |
| 222 | |
| 223 | if (unlikely(!p)) |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 224 | return ERR_PTR(-ENOMEM); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 225 | p->tcfc_refcnt = 1; |
| 226 | if (bind) |
| 227 | p->tcfc_bindcnt = 1; |
| 228 | |
| 229 | spin_lock_init(&p->tcfc_lock); |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 230 | INIT_HLIST_NODE(&p->tcfc_head); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 231 | p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo); |
| 232 | p->tcfc_tm.install = jiffies; |
| 233 | p->tcfc_tm.lastuse = jiffies; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 234 | if (est) { |
| 235 | int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est, |
| 236 | &p->tcfc_lock, est); |
| 237 | if (err) { |
| 238 | kfree(p); |
| 239 | return ERR_PTR(err); |
| 240 | } |
| 241 | } |
| 242 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 243 | a->priv = (void *) p; |
| 244 | return p; |
| 245 | } |
| 246 | EXPORT_SYMBOL(tcf_hash_create); |
| 247 | |
| 248 | void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo) |
| 249 | { |
| 250 | unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask); |
| 251 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 252 | spin_lock_bh(&hinfo->lock); |
| 253 | hlist_add_head(&p->tcfc_head, &hinfo->htab[h]); |
| 254 | spin_unlock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 255 | } |
| 256 | EXPORT_SYMBOL(tcf_hash_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 258 | static LIST_HEAD(act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | static DEFINE_RWLOCK(act_mod_lock); |
| 260 | |
| 261 | int tcf_register_action(struct tc_action_ops *act) |
| 262 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 263 | struct tc_action_ops *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 265 | /* Must supply act, dump, cleanup and init */ |
| 266 | if (!act->act || !act->dump || !act->cleanup || !act->init) |
| 267 | return -EINVAL; |
| 268 | |
Jamal Hadi Salim | 382ca8a | 2013-12-04 09:26:55 -0500 | [diff] [blame] | 269 | /* Supply defaults */ |
Jamal Hadi Salim | 63ef617 | 2013-12-04 09:26:53 -0500 | [diff] [blame] | 270 | if (!act->lookup) |
| 271 | act->lookup = tcf_hash_search; |
Jamal Hadi Salim | 382ca8a | 2013-12-04 09:26:55 -0500 | [diff] [blame] | 272 | if (!act->walk) |
| 273 | act->walk = tcf_generic_walker; |
Jamal Hadi Salim | 63ef617 | 2013-12-04 09:26:53 -0500 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | write_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 276 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { |
| 278 | write_unlock(&act_mod_lock); |
| 279 | return -EEXIST; |
| 280 | } |
| 281 | } |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 282 | list_add_tail(&act->head, &act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | write_unlock(&act_mod_lock); |
| 284 | return 0; |
| 285 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 286 | EXPORT_SYMBOL(tcf_register_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | int tcf_unregister_action(struct tc_action_ops *act) |
| 289 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 290 | struct tc_action_ops *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | int err = -ENOENT; |
| 292 | |
| 293 | write_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 294 | list_for_each_entry(a, &act_base, head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (a == act) |
| 296 | break; |
| 297 | if (a) { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 298 | list_del(&act->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | err = 0; |
| 300 | } |
| 301 | write_unlock(&act_mod_lock); |
| 302 | return err; |
| 303 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 304 | EXPORT_SYMBOL(tcf_unregister_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | /* lookup by name */ |
| 307 | static struct tc_action_ops *tc_lookup_action_n(char *kind) |
| 308 | { |
| 309 | struct tc_action_ops *a = NULL; |
| 310 | |
| 311 | if (kind) { |
| 312 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 313 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if (strcmp(kind, a->kind) == 0) { |
| 315 | if (!try_module_get(a->owner)) { |
| 316 | read_unlock(&act_mod_lock); |
| 317 | return NULL; |
| 318 | } |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | read_unlock(&act_mod_lock); |
| 323 | } |
| 324 | return a; |
| 325 | } |
| 326 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 327 | /* lookup by nlattr */ |
| 328 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | struct tc_action_ops *a = NULL; |
| 331 | |
| 332 | if (kind) { |
| 333 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame^] | 334 | list_for_each_entry(a, &act_base, head) { |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 335 | if (nla_strcmp(kind, a->kind) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (!try_module_get(a->owner)) { |
| 337 | read_unlock(&act_mod_lock); |
| 338 | return NULL; |
| 339 | } |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | read_unlock(&act_mod_lock); |
| 344 | } |
| 345 | return a; |
| 346 | } |
| 347 | |
| 348 | #if 0 |
| 349 | /* lookup by id */ |
| 350 | static struct tc_action_ops *tc_lookup_action_id(u32 type) |
| 351 | { |
| 352 | struct tc_action_ops *a = NULL; |
| 353 | |
| 354 | if (type) { |
| 355 | read_lock(&act_mod_lock); |
| 356 | for (a = act_base; a; a = a->next) { |
| 357 | if (a->type == type) { |
| 358 | if (!try_module_get(a->owner)) { |
| 359 | read_unlock(&act_mod_lock); |
| 360 | return NULL; |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | read_unlock(&act_mod_lock); |
| 366 | } |
| 367 | return a; |
| 368 | } |
| 369 | #endif |
| 370 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 371 | int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 372 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 374 | const struct tc_action *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | int ret = -1; |
| 376 | |
| 377 | if (skb->tc_verd & TC_NCLS) { |
| 378 | skb->tc_verd = CLR_TC_NCLS(skb->tc_verd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | ret = TC_ACT_OK; |
| 380 | goto exec_done; |
| 381 | } |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 382 | list_for_each_entry(a, actions, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | repeat: |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 384 | if (a->ops) { |
Patrick McHardy | f43c5a0 | 2006-01-08 22:15:34 -0800 | [diff] [blame] | 385 | ret = a->ops->act(skb, a, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | if (TC_MUNGED & skb->tc_verd) { |
| 387 | /* copied already, allow trampling */ |
| 388 | skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd); |
| 389 | skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd); |
| 390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (ret == TC_ACT_REPEAT) |
| 392 | goto repeat; /* we need a ttl - JHS */ |
J Hadi Salim | 14d50e7 | 2005-05-03 16:29:13 -0700 | [diff] [blame] | 393 | if (ret != TC_ACT_PIPE) |
| 394 | goto exec_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | exec_done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return ret; |
| 399 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 400 | EXPORT_SYMBOL(tcf_action_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 402 | void tcf_action_destroy(struct list_head *actions, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 404 | struct tc_action *a, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 406 | list_for_each_entry_safe(a, tmp, actions, list) { |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 407 | if (a->ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (a->ops->cleanup(a, bind) == ACT_P_DELETED) |
| 409 | module_put(a->ops->owner); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 410 | list_del(&a->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | kfree(a); |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 412 | } else { |
| 413 | /*FIXME: Remove later - catch insertion bugs*/ |
| 414 | WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n"); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 415 | list_del(&a->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | kfree(a); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | int |
| 422 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 423 | { |
| 424 | int err = -EINVAL; |
| 425 | |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 426 | if (a->ops == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return err; |
| 428 | return a->ops->dump(skb, a, bind, ref); |
| 429 | } |
| 430 | |
| 431 | int |
| 432 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 433 | { |
| 434 | int err = -EINVAL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 435 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 436 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 438 | if (a->ops == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | return err; |
| 440 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 441 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
| 442 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (tcf_action_copy_stats(skb, a, 0)) |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 444 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 445 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 446 | if (nest == NULL) |
| 447 | goto nla_put_failure; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 448 | err = tcf_action_dump_old(skb, a, bind, ref); |
| 449 | if (err > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 450 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | return err; |
| 452 | } |
| 453 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 454 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 455 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return -1; |
| 457 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 458 | EXPORT_SYMBOL(tcf_action_dump_1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | int |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 461 | tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
| 463 | struct tc_action *a; |
| 464 | int err = -EINVAL; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 465 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 467 | list_for_each_entry(a, actions, list) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 468 | nest = nla_nest_start(skb, a->order); |
| 469 | if (nest == NULL) |
| 470 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | err = tcf_action_dump_1(skb, a, bind, ref); |
| 472 | if (err < 0) |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 473 | goto errout; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 474 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 479 | nla_put_failure: |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 480 | err = -EINVAL; |
| 481 | errout: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 482 | nla_nest_cancel(skb, nest); |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 483 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 486 | struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla, |
| 487 | struct nlattr *est, char *name, int ovr, |
| 488 | int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | struct tc_action *a; |
| 491 | struct tc_action_ops *a_o; |
| 492 | char act_name[IFNAMSIZ]; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 493 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 494 | struct nlattr *kind; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 495 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (name == NULL) { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 498 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
| 499 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | goto err_out; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 501 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 502 | kind = tb[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | if (kind == NULL) |
| 504 | goto err_out; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 505 | if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | goto err_out; |
| 507 | } else { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 508 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) |
| 510 | goto err_out; |
| 511 | } |
| 512 | |
| 513 | a_o = tc_lookup_action_n(act_name); |
| 514 | if (a_o == NULL) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 515 | #ifdef CONFIG_MODULES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | rtnl_unlock(); |
Patrick McHardy | 4bba392 | 2006-01-08 22:22:14 -0800 | [diff] [blame] | 517 | request_module("act_%s", act_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | rtnl_lock(); |
| 519 | |
| 520 | a_o = tc_lookup_action_n(act_name); |
| 521 | |
| 522 | /* We dropped the RTNL semaphore in order to |
| 523 | * perform the module load. So, even if we |
| 524 | * succeeded in loading the module we have to |
| 525 | * tell the caller to replay the request. We |
| 526 | * indicate this using -EAGAIN. |
| 527 | */ |
| 528 | if (a_o != NULL) { |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 529 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | goto err_mod; |
| 531 | } |
| 532 | #endif |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 533 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | goto err_out; |
| 535 | } |
| 536 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 537 | err = -ENOMEM; |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 538 | a = kzalloc(sizeof(*a), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | if (a == NULL) |
| 540 | goto err_mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 542 | INIT_LIST_HEAD(&a->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /* backward compatibility for policer */ |
| 544 | if (name == NULL) |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 545 | 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] | 546 | else |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 547 | err = a_o->init(net, nla, est, a, ovr, bind); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 548 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | goto err_free; |
| 550 | |
| 551 | /* module count goes up only when brand new policy is created |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 552 | * if it exists and is only bound to in a_o->init() then |
| 553 | * ACT_P_CREATED is not returned (a zero is). |
| 554 | */ |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 555 | if (err != ACT_P_CREATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | module_put(a_o->owner); |
| 557 | a->ops = a_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return a; |
| 560 | |
| 561 | err_free: |
| 562 | kfree(a); |
| 563 | err_mod: |
| 564 | module_put(a_o->owner); |
| 565 | err_out: |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 566 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 569 | int tcf_action_init(struct net *net, struct nlattr *nla, |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 570 | struct nlattr *est, char *name, int ovr, |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 571 | int bind, struct list_head *actions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 573 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 574 | struct tc_action *act; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 575 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | int i; |
| 577 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 578 | err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL); |
| 579 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 580 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 582 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 583 | act = tcf_action_init_1(net, tb[i], est, name, ovr, bind); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 584 | if (IS_ERR(act)) { |
| 585 | err = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | goto err; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 587 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 588 | act->order = i; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 589 | list_add_tail(&act->list, actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 591 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | err: |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 594 | tcf_action_destroy(actions, bind); |
| 595 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a, |
| 599 | int compat_mode) |
| 600 | { |
| 601 | int err = 0; |
| 602 | struct gnet_dump d; |
| 603 | struct tcf_act_hdr *h = a->priv; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | if (h == NULL) |
| 606 | goto errout; |
| 607 | |
| 608 | /* compat_mode being true specifies a call that is supposed |
Dirk Hohndel | 06fe9fb | 2009-09-28 21:43:57 -0400 | [diff] [blame] | 609 | * to add additional backward compatibility statistic TLVs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | */ |
| 611 | if (compat_mode) { |
| 612 | if (a->type == TCA_OLD_COMPAT) |
| 613 | err = gnet_stats_start_copy_compat(skb, 0, |
Patrick McHardy | 4bdf399 | 2007-07-02 22:47:37 -0700 | [diff] [blame] | 614 | TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | else |
| 616 | return 0; |
| 617 | } else |
| 618 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, |
Patrick McHardy | 4bdf399 | 2007-07-02 22:47:37 -0700 | [diff] [blame] | 619 | &h->tcf_lock, &d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | if (err < 0) |
| 622 | goto errout; |
| 623 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 624 | if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 || |
Eric Dumazet | d250a5f | 2009-10-02 10:32:18 +0000 | [diff] [blame] | 625 | gnet_stats_copy_rate_est(&d, &h->tcf_bstats, |
| 626 | &h->tcf_rate_est) < 0 || |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 627 | gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | goto errout; |
| 629 | |
| 630 | if (gnet_stats_finish_copy(&d) < 0) |
| 631 | goto errout; |
| 632 | |
| 633 | return 0; |
| 634 | |
| 635 | errout: |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | static int |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 640 | tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 641 | u16 flags, int event, int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
| 643 | struct tcamsg *t; |
| 644 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 645 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 646 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 648 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 649 | if (!nlh) |
| 650 | goto out_nlmsg_trim; |
| 651 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 653 | t->tca__pad1 = 0; |
| 654 | t->tca__pad2 = 0; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 655 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 656 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 657 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 658 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 660 | if (tcf_action_dump(skb, actions, bind, ref) < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 661 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 663 | nla_nest_end(skb, nest); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 664 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 665 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | return skb->len; |
| 667 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 668 | out_nlmsg_trim: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 669 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | return -1; |
| 671 | } |
| 672 | |
| 673 | static int |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 674 | act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 675 | struct list_head *actions, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
| 677 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
| 679 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 680 | if (!skb) |
| 681 | return -ENOBUFS; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 682 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | kfree_skb(skb); |
| 684 | return -EINVAL; |
| 685 | } |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 686 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 687 | return rtnl_unicast(skb, net, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static struct tc_action * |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 691 | tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 693 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | struct tc_action *a; |
| 695 | int index; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 696 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 698 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
| 699 | if (err < 0) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 700 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 702 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 703 | if (tb[TCA_ACT_INDEX] == NULL || |
| 704 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 705 | goto err_out; |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 706 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 708 | err = -ENOMEM; |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 709 | a = kzalloc(sizeof(struct tc_action), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | if (a == NULL) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 711 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 713 | INIT_LIST_HEAD(&a->list); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 714 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 715 | a->ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | if (a->ops == NULL) |
| 717 | goto err_free; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 718 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | if (a->ops->lookup(a, index) == 0) |
| 720 | goto err_mod; |
| 721 | |
| 722 | module_put(a->ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | return a; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | err_mod: |
| 726 | module_put(a->ops->owner); |
| 727 | err_free: |
| 728 | kfree(a); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 729 | err_out: |
| 730 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 733 | static void cleanup_a(struct list_head *actions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 735 | struct tc_action *a, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 737 | list_for_each_entry_safe(a, tmp, actions, list) { |
| 738 | list_del(&a->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | kfree(a); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static struct tc_action *create_a(int i) |
| 744 | { |
| 745 | struct tc_action *act; |
| 746 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 747 | act = kzalloc(sizeof(*act), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | if (act == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 749 | pr_debug("create_a: failed to alloc!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | return NULL; |
| 751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | act->order = i; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 753 | INIT_LIST_HEAD(&act->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | return act; |
| 755 | } |
| 756 | |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 757 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 758 | struct nlmsghdr *n, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
| 760 | struct sk_buff *skb; |
| 761 | unsigned char *b; |
| 762 | struct nlmsghdr *nlh; |
| 763 | struct tcamsg *t; |
| 764 | struct netlink_callback dcb; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 765 | struct nlattr *nest; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 766 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 767 | struct nlattr *kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | struct tc_action *a = create_a(0); |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 769 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | if (a == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 772 | pr_debug("tca_action_flush: couldnt create tc_action\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | return err; |
| 774 | } |
| 775 | |
| 776 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 777 | if (!skb) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 778 | pr_debug("tca_action_flush: failed skb alloc\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | kfree(a); |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 780 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 783 | b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 785 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
| 786 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | goto err_out; |
| 788 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 789 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 790 | kind = tb[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | a->ops = tc_lookup_action(kind); |
| 792 | if (a->ops == NULL) |
| 793 | goto err_out; |
| 794 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 795 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 796 | if (!nlh) |
| 797 | goto out_module_put; |
| 798 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 800 | t->tca__pad1 = 0; |
| 801 | t->tca__pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 803 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 804 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 805 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | err = a->ops->walk(skb, &dcb, RTM_DELACTION, a); |
| 808 | if (err < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 809 | goto out_module_put; |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 810 | if (err == 0) |
| 811 | goto noflush_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 813 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 815 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | nlh->nlmsg_flags |= NLM_F_ROOT; |
| 817 | module_put(a->ops->owner); |
| 818 | kfree(a); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 819 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 820 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | if (err > 0) |
| 822 | return 0; |
| 823 | |
| 824 | return err; |
| 825 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 826 | out_module_put: |
Thomas Graf | ebbaeab | 2006-07-09 11:36:23 -0700 | [diff] [blame] | 827 | module_put(a->ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | err_out: |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 829 | noflush_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | kfree_skb(skb); |
| 831 | kfree(a); |
| 832 | return err; |
| 833 | } |
| 834 | |
| 835 | static int |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 836 | 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] | 837 | u32 portid, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 839 | int i, ret; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 840 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 841 | struct tc_action *act; |
| 842 | LIST_HEAD(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 844 | ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL); |
| 845 | if (ret < 0) |
| 846 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 848 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 849 | if (tb[1] != NULL) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 850 | return tca_action_flush(net, tb[1], n, portid); |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 851 | else |
| 852 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 855 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 856 | act = tcf_action_get_1(tb[i], n, portid); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 857 | if (IS_ERR(act)) { |
| 858 | ret = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | goto err; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 860 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 861 | act->order = i; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 862 | list_add_tail(&act->list, &actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if (event == RTM_GETACTION) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 866 | ret = act_get_notify(net, portid, n, &actions, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | else { /* delete */ |
| 868 | struct sk_buff *skb; |
| 869 | |
| 870 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 871 | if (!skb) { |
| 872 | ret = -ENOBUFS; |
| 873 | goto err; |
| 874 | } |
| 875 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 876 | if (tca_get_fill(skb, &actions, portid, n->nlmsg_seq, 0, event, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 877 | 0, 1) <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | kfree_skb(skb); |
| 879 | ret = -EINVAL; |
| 880 | goto err; |
| 881 | } |
| 882 | |
| 883 | /* now do the delete */ |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 884 | tcf_action_destroy(&actions, 0); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 885 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 886 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | if (ret > 0) |
| 888 | return 0; |
| 889 | return ret; |
| 890 | } |
| 891 | err: |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 892 | cleanup_a(&actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return ret; |
| 894 | } |
| 895 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 896 | static int tcf_add_notify(struct net *net, struct list_head *actions, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 897 | u32 portid, u32 seq, int event, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | { |
| 899 | struct tcamsg *t; |
| 900 | struct nlmsghdr *nlh; |
| 901 | struct sk_buff *skb; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 902 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | unsigned char *b; |
| 904 | int err = 0; |
| 905 | |
| 906 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 907 | if (!skb) |
| 908 | return -ENOBUFS; |
| 909 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 910 | b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 912 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 913 | if (!nlh) |
| 914 | goto out_kfree_skb; |
| 915 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 917 | t->tca__pad1 = 0; |
| 918 | t->tca__pad2 = 0; |
| 919 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 920 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 921 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 922 | goto out_kfree_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 924 | if (tcf_action_dump(skb, actions, 0, 0) < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 925 | goto out_kfree_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 927 | nla_nest_end(skb, nest); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 928 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 929 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 930 | NETLINK_CB(skb).dst_group = RTNLGRP_TC; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 931 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 932 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | if (err > 0) |
| 934 | err = 0; |
| 935 | return err; |
| 936 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 937 | out_kfree_skb: |
Patrick McHardy | f6e5746 | 2006-03-12 20:33:22 -0800 | [diff] [blame] | 938 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | return -1; |
| 940 | } |
| 941 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | static int |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 944 | tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 945 | u32 portid, int ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | { |
| 947 | int ret = 0; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 948 | LIST_HEAD(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | u32 seq = n->nlmsg_seq; |
| 950 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 951 | ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions); |
| 952 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | goto done; |
| 954 | |
| 955 | /* dump then free all the actions after update; inserted policy |
| 956 | * stays intact |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 957 | */ |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 958 | ret = tcf_add_notify(net, &actions, portid, seq, RTM_NEWACTION, n->nlmsg_flags); |
| 959 | cleanup_a(&actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | done: |
| 961 | return ret; |
| 962 | } |
| 963 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 964 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 966 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 967 | struct nlattr *tca[TCA_ACT_MAX + 1]; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 968 | u32 portid = skb ? NETLINK_CB(skb).portid : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | int ret = 0, ovr = 0; |
| 970 | |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 971 | if ((n->nlmsg_type != RTM_GETACTION) && !capable(CAP_NET_ADMIN)) |
| 972 | return -EPERM; |
| 973 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 974 | ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL); |
| 975 | if (ret < 0) |
| 976 | return ret; |
| 977 | |
| 978 | if (tca[TCA_ACT_TAB] == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 979 | pr_notice("tc_ctl_action: received NO action attribs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | return -EINVAL; |
| 981 | } |
| 982 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 983 | /* n->nlmsg_flags & NLM_F_CREATE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | switch (n->nlmsg_type) { |
| 985 | case RTM_NEWACTION: |
| 986 | /* we are going to assume all other flags |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 987 | * imply create only if it doesn't exist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | * Note that CREATE | EXCL implies that |
| 989 | * but since we want avoid ambiguity (eg when flags |
| 990 | * is zero) then just set this |
| 991 | */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 992 | if (n->nlmsg_flags & NLM_F_REPLACE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | ovr = 1; |
| 994 | replay: |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 995 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | if (ret == -EAGAIN) |
| 997 | goto replay; |
| 998 | break; |
| 999 | case RTM_DELACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1000 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1001 | portid, RTM_DELACTION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | break; |
| 1003 | case RTM_GETACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1004 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1005 | portid, RTM_GETACTION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | break; |
| 1007 | default: |
| 1008 | BUG(); |
| 1009 | } |
| 1010 | |
| 1011 | return ret; |
| 1012 | } |
| 1013 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1014 | static struct nlattr * |
Patrick McHardy | 3a6c2b4 | 2009-08-25 16:07:40 +0200 | [diff] [blame] | 1015 | find_dump_kind(const struct nlmsghdr *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1017 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1018 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
| 1019 | struct nlattr *nla[TCAA_MAX + 1]; |
| 1020 | struct nlattr *kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
Patrick McHardy | c96c947 | 2008-01-23 20:32:58 -0800 | [diff] [blame] | 1022 | if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | return NULL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1024 | tb1 = nla[TCA_ACT_TAB]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | if (tb1 == NULL) |
| 1026 | return NULL; |
| 1027 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1028 | if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), |
| 1029 | NLMSG_ALIGN(nla_len(tb1)), NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Patrick McHardy | 6d834e0 | 2008-01-23 20:32:42 -0800 | [diff] [blame] | 1032 | if (tb[1] == NULL) |
| 1033 | return NULL; |
| 1034 | if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]), |
| 1035 | nla_len(tb[1]), NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | return NULL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1037 | kind = tb2[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1039 | return kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static int |
| 1043 | tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) |
| 1044 | { |
| 1045 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1046 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1047 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | struct tc_action_ops *a_o; |
| 1049 | struct tc_action a; |
| 1050 | int ret = 0; |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1051 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1052 | struct nlattr *kind = find_dump_kind(cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | if (kind == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 1055 | pr_info("tc_dump_action: action bad kind\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return 0; |
| 1057 | } |
| 1058 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1059 | a_o = tc_lookup_action(kind); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1060 | if (a_o == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
| 1063 | memset(&a, 0, sizeof(struct tc_action)); |
| 1064 | a.ops = a_o; |
| 1065 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1066 | 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] | 1067 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
| 1068 | if (!nlh) |
| 1069 | goto out_module_put; |
| 1070 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1072 | t->tca__pad1 = 0; |
| 1073 | t->tca__pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1075 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 1076 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1077 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
| 1079 | ret = a_o->walk(skb, cb, RTM_GETACTION, &a); |
| 1080 | if (ret < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1081 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
| 1083 | if (ret > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1084 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | ret = skb->len; |
| 1086 | } else |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1087 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1089 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1090 | if (NETLINK_CB(cb->skb).portid && ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | nlh->nlmsg_flags |= NLM_F_MULTI; |
| 1092 | module_put(a_o->owner); |
| 1093 | return skb->len; |
| 1094 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1095 | out_module_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | module_put(a_o->owner); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1097 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | return skb->len; |
| 1099 | } |
| 1100 | |
| 1101 | static int __init tc_action_init(void) |
| 1102 | { |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1103 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL); |
| 1104 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL); |
| 1105 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, |
| 1106 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | subsys_initcall(tc_action_init); |