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